NSScreen not updating monitor count when new monitors are plugged in

I am finding that NSScreen is returning the same number of monitors even after additional monitors are plugged in.

Made a simple test app that can replicate the issue. Basically infinite loops and prints NSScreen counts and CGDisplay counts.

  1. Start the app
  2. printing “NSScreen = 1 CGDisplay = 1”
  3. Without stopping the app, plug in an additional monitor
  4. printing “NSScreen = 1 CGDisplay = 2”

However the code should be printing “NSScreen = 2 CGDisplay = 2”

On OS X 11, we see the same issue (NSScreen = 1 CGDisplay = 2) after plugging in the additional monitor.

Test Code is here:

main.cpp

#include <iostream>
#include "macScreen.h"
int main(int argc, const char * argv[]) {
  getNSScreenCoordinates();
  return 0;
}

macScreen.h

#ifndef macScreen_h
#define macScreen_h
float getNSScreenCoordinates();
#endif /* macScreen_h */

macScreen.mm

#import <Foundation/Foundation.h>
#include <iostream>
#include <Cocoa/Cocoa.h>
#import <AppKit/NSScreen.h>
#define MAX_NUM_DISPLAYS 255
float getNSScreenCoordinates() {

  NSArray<NSScreen *> *arr = [NSScreen screens];
  NSUInteger numScreens = [arr count];

  CGDirectDisplayID displays[MAX_NUM_DISPLAYS];
  CGDisplayCount displayCount;
  CGGetOnlineDisplayList(MAX_NUM_DISPLAYS, displays, &displayCount);
  
  while(1) {
    std::cout << "cg num displays " << displayCount << "\n";
    std::cout << "numscreens " << numScreens << "\n";

    arr = [NSScreen screens];
    numScreens = [arr count];

    CGGetOnlineDisplayList(MAX_NUM_DISPLAYS, displays, &displayCount);
    
  }
  return 1;
}

Made a simple test app that can replicate the issue.

This isn’t a valid test. NSScreen is part of AppKit and AppKit expects you to run the run loop. I recommend that you test this from a proper app.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Made a simple test app that can replicate the issue.

This isn’t a valid test. NSScreen is part of AppKit and AppKit expects you to run the run loop. I recommend that you test this from a proper app.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

NSScreen not updating monitor count when new monitors are plugged in
 
 
Q