(((1 << 3) & (~0xF | (3 >> 1) & (~0xFFED << 4))) | ~(7 + 0xDEADBEEF << 7) >> 5) >> 5 ^ (0xDE ^ (~0xF32 >> (5 | 74 >> 2)))

Leopard CGWindowList* APIs

Devin Lane | Leopard, Programming | Thursday, November 8th, 2007

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.

1 Comment »

  1. 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

    Comment by greg smolinger — April 28, 2008 @ 10:01 am

RSS feed for comments on this post.

Leave a comment

Powered by WordPress | Theme by Roy Tanck, modifications by Devin