Apple Developer Connection
Member Login Log In | Not a Member? Contact ADC

< Previous PageNext Page > Hide TOC

Displays and Display Modes

Quartz Display Services functions allow you to enumerate all displays as well as the supported modes for each display. The Quartz Display Services functions CGDisplaySwitchToMode and CGDisplayBestModeForParameters use on the Core Foundation CFDictionary data type. Each display mode has a dictionary whose key-value pairs you can query. You can use accessor functions to query the properties of the current display mode. You can also use Core Foundation functions to access the dictionary associated with a display mode. See CFDictionary Reference.

You can enumerate the display modes for a display by using its display ID. You can obtain an array of display IDs that correspond to all displays in the system by calling the function CGGetActiveDisplayList. The first display in the list is always the main display. The main display is also represented by the constant kCGDirectMainDisplay.

These functions also obtain an array of display IDs:

Typically you use the functions CGGetDisplaysWithPoint and CGGetDisplaysWithRect when tracking user interactions. You choose which display to capture based on where the user places the pointer. After capturing the display, you can the obtain the supported modes by calling the function CGDisplayAvailableModes.

Listing C-1 shows how to switch the last display in a display list into its first mode and then print the height and width of the display. A detailed explanation for each numbered line of code appears following the listing.

Listing C-1  Switching modes for a display in a list

#define MAX_DISPLAYS 32
 
CGDirectDisplayID lastDisplay, displayArray[MAX_DISPLAYS] ;
CGDisplayCount numDisplays;
 
CFArrayRef displayModeArray;
CFDictionaryRef displayMode;
 
CFNumberRef number;
long height, width;
 
CGGetActiveDisplayList (MAX_DISPLAYS, displayArray, &numDisplays); // 1
lastDisplay = displayArray [numDisplays - 1]; // 2
CGDisplayCapture (lastDisplay); // 3
displayModeArray = CGDisplayAvailableModes (lastDisplay); // 4
displayMode = (CFDictionaryRef) CFArrayGetValueAtIndex (displayModeArray, 0); // 5
CGDisplaySwitchToMode (lastDisplay, displayMode); // 6
/* Run the event loop. */
CGReleaseAllDisplays(); // 7

Here's what the code does:

  1. Gets the array of active displays, which are the ones available for drawing.

  2. Gets the display ID of the last display in the array. The array is zero-based.

  3. Captures the display associated with the last display in the array.

  4. Gets all the display modes for the display.

  5. Gets the first display mode for the display. Recall that the display mode is stored as a CFDictionary object that contains key-value pairs for the attributes of the display mode.

  6. Switches the display mode.

  7. Before the application quits, releases all displays.

Quartz Display Services provides simple accessor functions for many properties of the current display mode. For these properties, you don't need to call CFDictionaryGetValue. Listing C-2 shows how to obtain the properties of the current mode of every display (up to 32) in the system.

Listing C-2  Getting display properties

#define MAX_DISPLAYS 32
 
CGDirectDisplayID displayArray [MAX_DISPLAYS];
CGDisplayCount numDisplays;
CFNumberRef number;
CFBoolean booleanValue;
long    height, width, refresh, mode,
        bpp, bps, spp, rowBytes, gui, ioflags;
int     i;
 
CGGetActiveDisplayList (MAX_DISPLAYS, displayArray, &numDisplays); // 1
printf ("Displays installed: %d\n", numDisplays); // 2
for(i = 0; i < numDisplays; i++) { // 3
    width = CGDisplayPixelsWide (displayArray[i]);
    height = CGDisplayPixelsHigh (displayArray[i]);
    bpp = CGDisplayBitsPerPixel (displayArray[i]);
    bps = CGDisplayBitsPerSample (displayArray[i]);
    spp = CGDisplaySamplesPerPixel (displayArray[i]);
    rowBytes = CGDisplayBytesPerRow (displayArray[i]);
    number = CFDictionaryGetValue (CGDisplayCurrentMode (displayArray[i]),
                                    kCGDisplayMode);
    CFNumberGetValue (number, kCFNumberLongType, &mode);
    number = CFDictionaryGetValue (CGDisplayCurrentMode (displayArray[i]),
                                    kCGDisplayRefreshRate);
    CFNumberGetValue (number, kCFNumberLongType, &refresh);
    booleanValue = CFDictionaryGetValue (CGDisplayCurrentMode(displayArray[i]),
                                kCGDisplayModeUsableForDesktopGUI);
    CFNumberGetValue (number, kCFNumberLongType, &gui);
    number = CFDictionaryGetValue (CGDisplayCurrentMode (displayArray[i]),
                                kCGDisplayIOFlags);
    CFNumberGetValue (number, kCFNumberLongType, &ioflags);
}

Here's what the code does:

  1. Gets the array of displays.

  2. Prints out the number of displays.

  3. Gets the properties for the current mode of each display. Note that Quartz Display Services provides several functions that obtain properties. For information about the current display mode, you must use the function CFDictionaryGetValue, along with the appropriate key, to retrieve a value from the display mode dictionary returned by the function CGDisplayCurrentMode.



< Previous PageNext Page > Hide TOC


Last updated: 2008-06-09




Did this document help you?
Yes: Tell us what works for you.

It’s good, but: Report typos, inaccuracies, and so forth.

It wasn’t helpful: Tell us what would have helped.
Get information on Apple products.
Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Copyright © 2007 Apple Inc.
All rights reserved. | Terms of use | Privacy Notice