Page 1 of 1

CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Posted: Fri May 11, 2018 2:37 pm
by trevordevore
I'm trying to convert some code that lists all windows in a session on macOS. In order to do that I need to wrap CGWindowListCopyWindowInfo() <https://developer.apple.com/documentati ... guage=objc>.

The definition is as follows:

Code: Select all

CFArrayRef CGWindowListCopyWindowInfo(CGWindowListOption option, CGWindowID relativeToWindow);
A CFArrayRef can be bridge toll-free to NSArray but I'm guessing that I can't return an ObjcId for a CFArrayRef and have it work properly. In the Objective-C version of the code the bridge as handled as follows:

Code: Select all

(__bridge NSArray *)CGWindowListCopyWindowInfo(listOptions, kCGNullWindowID);
How should I handle this in FFI?

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Posted: Fri May 11, 2018 5:17 pm
by LCMark
@trevordevore: The CF types and the corresponding NS types are all compatible at the pointer level - the only thing which differs is that the CF APIs never returned autoreleased objects - so in this case it would return an ObjcRetainedId. Once put into an ObjcObject you can use it like you would the NS object..

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Posted: Fri May 11, 2018 5:38 pm
by trevordevore
Fantastic!

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Posted: Sat May 12, 2018 7:22 pm
by trevordevore
@LCMark - how does one go about wrapping up CoreGraphics handlers? I see on Windows we use the `userlib>` syntax and parameters are passed directly to the function. In `objc:` we define the property names that are set in the "binds" syntax. My guess is that it behaves like Windows but either of the following syntax just gives me a crash.

Code: Select all

private foreign handler CGWindowListCopyWindowInfo(in pListOptions as Integer, in pRelativeToWindow as optional CUint) returns ObjcRetainedId binds to "CGWindowListCopyWindowInfo"

Code: Select all

private foreign handler CGWindowListCopyWindowInfo(in pListOptions as Integer, in pRelativeToWindow as optional CUint) returns ObjcRetainedId binds to "CoreGraphics>CGWindowListCopyWindowInfo"

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Posted: Tue May 15, 2018 3:22 am
by trevordevore
Monte helped me figure this out. Here is the correct FFI syntax:

Code: Select all

private foreign handler CGWindowListCopyWindowInfo(in pListOptions as Integer, in pRelativeToWindow as CUint) returns ObjcRetainedId binds to "c:CoreGraphics.framework>CGWindowListCopyWindowInfo"

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Posted: Tue May 15, 2018 7:46 pm
by PaulDaMacMan
trevordevore wrote:
Tue May 15, 2018 3:22 am
Monte helped me figure this out. Here is the correct FFI syntax:

Code: Select all

private foreign handler CGWindowListCopyWindowInfo(in pListOptions as Integer, in pRelativeToWindow as CUint) returns ObjcRetainedId binds to "c:CoreGraphics.framework>CGWindowListCopyWindowInfo"
So for CF CoreFoundation / C APIs that are part of the macOS system the binding string formula is c:FrameWorkBundleName.framework>functionName. This answers one of my questions too, thanks for the update!

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Posted: Fri Jun 15, 2018 2:36 pm
by trevordevore
For anyone interested, you can see the final implementation of CGWindowListCopyWindowInfo in the `NSWindowGetInfoForOpenWindows` handler in the following code:

https://github.com/trevordevore/lc-maco ... window.lcb