What does kCGWindowSharingState actually * do *?

If you call CGWindowListCopyWindowInfo () , the CFDictionaryRef that you return contains a series of keys and values . One of them is kCGWindowSharingState , which has three possible values:

Window Sharing Constants Specifies whether and how windows are shared between applications. enum { kCGWindowSharingNone = 0, kCGWindowSharingReadOnly = 1, kCGWindowSharingReadWrite = 2 }; 

Almost all the windows on my system are kCGWindowSharingReadOnly , and the ScreenShot SonOfGrab screen trial program avoids capturing kCGWindowSharingNone windows, but I could not find a good description of what these conditions are for.

A simple test seems to show that some applications shipped with OS X have windows that are set to kCGWindowSharingNone , in particular Notes and iBooks. As far as I can tell from a quick test, the presence of the kCGWindowSharingNone window actually does not allow CGWindowListCreateImage () to capture the image of this window. There are no windows that I could find to set kCGWindowSharingReadWrite as the sharing mode.

Is this all explained somewhere in the documentation, and did I just skip this, or is it just more or less documented Core Graphics functionality? Is there any good reason not to try to capture the windows of kCGWindowSharingNone , and will I be setting myself up for trouble in the future if I try to do this?


Further research showed that when a Cocoa application calls

 [NSWindow setSharingType:] 

This sets kCGWindowSharingStateNone in the window and prevents it from being captured using CGWindowListCreateImage () . There are also some other windows that have kCGWindowSharingStateNone , but which can be successfully captured - in particular, iBooks creates such a window.

This is supposedly a bug in iBooks or in any API that it calls (since it does not call the NSWindow API).

+7
objective-c cocoa core-graphics quartz-graphics macos
source share
1 answer

If you ignore the status of window sharing, you should be prepared for some windows to behave exactly as the API indicates, and, apparently, according to your own research, some of them are not.

Higher in the Cocoa API, NSWindow has the sharingType property. By setting NSWindowSharingTypeNone , the corresponding CGWindow has the partition type kCGWindowSharingNone .

When I change this property in the window of my application, it prevents other applications from using the CGWindow API to capture the contents of windows. The documentation for CGWindowListCreateImage also correct.

Any windows that are on the screen, but whose sharing settings are set to kCGWindowSharingNone, are skipped and not included in the resulting image.

When I take a screenshot with several windows that should include a private window, this window is missing from the screenshot.


The reason this can happen in your own application is to prevent the copying of graphic content.

I do not know why iBooks and Notes exhibit the behavior that you describe. I could imagine that Apple did not want the user to be able to copy content from the iBook, but this obviously does not happen, and does not make sense for Notes (by the way, the Notes window looks fine in 10.10.2).

I don’t know how to use read / write type for Windows content.

+5
source share

All Articles