Leopard CGWindowList* APIs

Leopard brings new APIs at the CoreGraphics layer to gather information about windows on the system. These functions let you find windows relative to a known window, find all windows on the system, including those offscreen, and obtain images of one or more windows as composited by the Window Server.

However, these API’s have an interesting peculiarity: when the documentation says “a CFArray of CGWindowID values” they mean a CFArray of uint32s, not CFNumbers as programmers familiar with CoreFoundation might expect. This holds true for the CGWindowListCreate(), CGWindowListCreateDescriptionFromArray(), and CGWindowListCreateImageFromArray functions, but the two functions that return window information (CGWindowListCopyWindowInfo() and CGWindowListCreateDescriptionFromArray()) wrap all numeric values in CFNumbers.

To create such an array, you’ll have to create it with CFArrayCreateMutable(), such as the following:

/* Make an array with no callbacks */
CFMutableArrayRef windows = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);

/* myWindow is a NSWindow */
CFArrayAppendValue(windows, (void *)[myWindow windowNumber]);

To get the CGWindowID of a NSWindow, you’ll want to use -[NSWindow windowNumber]. Even though the documentation specifies that this isn’t the same number as assigned by the WindowServer, it turns out that it works fine with the CGWindow* APIs.

Posted in Software | 1 Comment

One Response to Leopard CGWindowList* APIs

  1. greg smolinger says:

    So we could settle the OS X focus-follows-mouse debate using CGWindowList* APIs?
    Or could we just use http://www.controllermate.com as a convenience API?

    http://steve-yegge.blogspot.com/2008/04/settling-osx-focus-follows-mouse-debate.html

Leave a Reply

Your email address will not be published.