NSScreen ID Unreliable
NSScreen ID Unreliable
- Subject: NSScreen ID Unreliable
- From: Gordon Apple <email@hidden>
- Date: Tue, 09 Oct 2012 11:38:16 -0600
- Thread-topic: NSScreen ID Unreliable
I added a category to NSScreen containing the following:
- (CGDirectDisplayID) screenID {
NSDictionary *dict = self.deviceDescription;
CGDirectDisplayID theID = [dict[@"NSScreenNumber"] unsignedIntValue];
return theID;
}
+ (NSScreen*) menuScreen {
return [[self screens] objectAtIndex:0];
}
+ (CGDirectDisplayID) menuScreenID {
return [[self menuScreen] screenID];
}
When I lazily instantiate a controller when accessing a relevant menu, I
call [NSScreen menuScreenID] in my init to set an ivar (CGDirectDisplayID).
Later, it gets called again and (from then on) gives a totally different ID
(which still works for the intended purpose). There is code which get
called if these values differ (for multi-screen use). This is not a case of
anything in my code changing the ivar, because for testing, I log it
immediately on each access. I tried to reproduce this in a test app, to no
avail, so I¹m not filing a bug report (yet). The test app always gives the
first ID returned in the problem app. Both are tested on a single-screen
MacBookPro. What could cause me to get two different results?
--------------------------
Just found the following by Googling
<http://stackoverflow.com/questions/8661001/nsscreennumber-changes-randomly
>:
³For Mac's that have built-in graphics and discrete graphics cards (such as
MacBook Pro models with on-board Intel graphics and a separate graphics
card), the display ID can change when the system automatically switches
between the two. You can disable "Automatic graphics switching" in the
Energy Saver prefs panel to test whether this is the cause of your screen
number changes (when disabled, will always use the discrete graphics card).
On such systems, the choice of which graphics is in use at a particular time
is tied to the applications that are currently running and their needs. I
believe any use of OpenGL by an application would cause a switch to the
discrete graphics card, for instance.
If you need to notice when such a switch occurs while your application is
running, you can register a callback
(CGDisplayRegisterReconfigurationCallback) and examine the changes that
occur (kCGDisplayAddFlag, kCGDisplayRemoveFlag, etc). If you're trying to
match a display to one previously used/encountered, you would need to go
beyond just comparing display id's.²
--------------------------
OK, this sucks. So how does one get a reliable (and persistent storage
capable) way to identify a screen?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden