Search code examples
objective-cmacosresolutioniokit

Programmatically trigger "detect displays."


I’m trying to trigger the same thing that the system does when you click on “Detect Displays” in the “Displays” System Preferences pane. Is there a way to do this programmatically? My goal is to create a LaunchAgent that does this at login to reset the display resolution in case a user messes with it.


Solution

  • You will need to use a private CoreGraphics routine to get the list of all displays including inactive ones, and then request a rescan of the bus. Try it like this:

    #include <IOKit/IOKitLib.h>
    #include <IOKit/IOTypes.h>
    
    CGDisplayErr CGSGetDisplayList(CGDisplayCount maxDisplays,
                                        CGDirectDisplayID * onlineDspys,
                                        CGDisplayCount * dspyCnt);
    static void DetectDisplays()
    {
     CGDirectDisplayID    displays[8];
        CGDisplayCount  dspCount = 0;
    
     if (CGSGetDisplayList(8, displays, &dspCount) == noErr)
     {
      for(int i = 0; i < dspCount; i++)
      {
       io_service_t service = CGDisplayIOServicePort(displays[i]);
       if (service)
        IOServiceRequestProbe(service, kIOFBUserRequestProbe);
      }
     }
    }
    

    And link to ApplicationServices and IOKit.