CGWindowListCopyWindowInfo, CFArrayRef, NSArray, and __bridge

LiveCode Builder is a language for extending LiveCode's capabilities, creating new object types as Widgets, and libraries that access lower-level APIs in OSes, applications, and DLLs.

Moderators: LCMark, LCfraser

Post Reply
trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
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 - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1206
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: 1005
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 - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
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 - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
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 - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

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

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!
My GitHub Repos: https://github.com/PaulMcClernan/
Related YouTube Videos: PlayList

trevordevore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1005
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 - https://www.screensteps.com

LiveCode Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode
LiveCode Builder Repos - https://github.com/search?q=user%3Atrevordevore+topic:livecode-builder

Post Reply

Return to “LiveCode Builder”