CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Moderators: LCMark, LCfraser

Post Reply
trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 882
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Post by trevordevore » Fri May 11, 2018 2:37 pm

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?
Trevor DeVore
ScreenSteps - http://www.screensteps.com

Levure Application Framework: https://github.com/trevordevore/levure
LiveCode Resources for Developers: http://livecode.bluemangolearning.com

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 996
Joined: Thu Apr 11, 2013 11:27 am

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Post by LCMark » Fri May 11, 2018 5:17 pm

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

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 882
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Post by trevordevore » Fri May 11, 2018 5:38 pm

Fantastic!
Trevor DeVore
ScreenSteps - http://www.screensteps.com

Levure Application Framework: https://github.com/trevordevore/levure
LiveCode Resources for Developers: http://livecode.bluemangolearning.com

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 882
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Post by trevordevore » Sat May 12, 2018 7:22 pm

@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"
Trevor DeVore
ScreenSteps - http://www.screensteps.com

Levure Application Framework: https://github.com/trevordevore/levure
LiveCode Resources for Developers: http://livecode.bluemangolearning.com

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 882
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Post by trevordevore » 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"
Trevor DeVore
ScreenSteps - http://www.screensteps.com

Levure Application Framework: https://github.com/trevordevore/levure
LiveCode Resources for Developers: http://livecode.bluemangolearning.com

PaulDaMacMan
Posts: 190
Joined: Wed Apr 24, 2013 4:53 pm

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Post by PaulDaMacMan » Tue May 15, 2018 7:46 pm

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!

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 882
Joined: Sat Apr 08, 2006 3:06 pm
Location: Overland Park, Kansas
Contact:

Re: CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

Post by trevordevore » Fri Jun 15, 2018 2:36 pm

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
Trevor DeVore
ScreenSteps - http://www.screensteps.com

Levure Application Framework: https://github.com/trevordevore/levure
LiveCode Resources for Developers: http://livecode.bluemangolearning.com

Post Reply

Return to “LiveCode Builder”