以编程方式触发“检测显示”。

时间:2010-01-17 04:34:51

标签: objective-c macos resolution iokit

当我点击“显示”系统偏好设置面板中的“检测显示”时,我试图触发系统执行的操作。有没有办法以编程方式执行此操作?我的目标是创建一个LaunchAgent,在登录时执行此操作以重置显示分辨率,以防用户混淆。

2 个答案:

答案 0 :(得分:4)

由于OS X 10.9 Mavericks中已弃用CGDisplayIOServicePort调用,因此通过适当的匹配调用获取IOFramebuffer服务可能更好:

void triggerDetectDisplays()
{
    // loop over all IOFramebuffer services
    CFMutableDictionaryRef matchingDict = IOServiceMatching("IOFramebuffer");

    mach_port_t masterPort;
    IOMasterPort(MACH_PORT_NULL, &masterPort);
    io_iterator_t serviceIterator;
    IOServiceGetMatchingServices(masterPort, matchingDict, &serviceIterator);

    io_service_t obj = IOIteratorNext(serviceIterator);
    while (obj)
    {
        kern_return_t kr = IOServiceRequestProbe(obj, 0);
        obj = IOIteratorNext(serviceIterator);
    }
}

答案 1 :(得分:3)

您需要使用私有CoreGraphics例程来获取所有显示的列表,包括非活动的显示,然后请求重新扫描总线。试试这样:

#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);
  }
 }
}

并链接到ApplicationServices和IOKit。