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:
CGGetDisplaysWithPoint obtains the display IDs for online displays whose bounds include a specified point.
CGGetDisplaysWithRect obtains the display IDs for online displays whose bounds include a specified rectangle.
CGGetDisplaysWithOpenGLDisplayMask obtains the display IDs for online displays that correspond to the bits set in an OpenGL display mask.
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:
Gets the array of active displays, which are the ones available for drawing.
Gets the display ID of the last display in the array. The array is zero-based.
Captures the display associated with the last display in the array.
Gets all the display modes for the display.
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.
Switches the display mode.
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:
Gets the array of displays.
Prints out the number of displays.
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.
Last updated: 2008-06-09