Post not yet marked as solved
Post marked as unsolved with 0 replies, 217 views
I am seeing an issue where CGDisplayCopyDisplayMode returns NULL for a monitor that was recently plugged in.
We call CGGetOnlineDisplayList to get the list of displays. We then call CGDisplayCopyDisplayMode with the CGDirectDisplayId provided. However, in the case of a monitor that was recently plugged in it returns NULL. This is strange because CGDisplayIsOnline returns true and CGDisplayBounds returns correct values.
The following sample app reproduces the issue:
`void printDisplays() {
[NSApplication sharedApplication];
while (true) {
sleep(1);
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false);
CGDirectDisplayID displays[16];
CGDisplayCount displayCount;
CGError err = CGGetOnlineDisplayList(16, displays, &displayCount);
if (err != kCGErrorSuccess) {
return;
}
printf("cgdisplays cgdisplaycount \n\n");
for (uint32_t i = 0; i < displayCount; i++) {
CGDirectDisplayID cgDisplayID = displays[i];
printf("cgdisplay id %d \n", cgDisplayID);
CGRect rect = CGDisplayBounds(cgDisplayID);
printf("cgrect origin x %.2f, y %.2f \n", rect.origin.x, rect.origin.y);
printf("cgrect size width %.2f, height %.2f \n", rect.size.width, rect.size.height);
CGDisplayModeRef displayMode = CGDisplayCopyDisplayMode(cgDisplayID);
int pixelWidth = CGDisplayModeGetPixelWidth(displayMode);
int pixelHeight = CGDisplayModeGetPixelHeight(displayMode);
printf("cgdisplaymode pixelWidth %d, pixelHeight %d \n\n", pixelWidth, pixelHeight);
}
printf("\n\n\n");
}
}`
However the addition of
[NSApplication sharedApplication];
and
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, false);
resolved the issue in the sample app, but did not resolve the issue in my real application. That is, the above code works because those lines were added, but adding the above lines in my application still return NULL.