Pregunta

Estoy tratando de desencadenar la misma cosa que hace el sistema cuando se hace clic en “Detectar pantallas” en el panel “Muestra” Preferencias del sistema. ¿Hay una manera de hacer esto mediante programación? Mi objetivo es crear un LaunchAgent que hace esto al iniciar la sesión para restablecer la resolución de pantalla en caso de que un usuario se mete con él.

¿Fue útil?

Solución

Usted tendrá que utilizar una rutina CoreGraphics privado para obtener la lista de todas las pantallas, incluyendo los inactivos, y luego solicitar a volver a examinar el bus. Trate de esta manera:

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

y enlace a ApplicationServices y IOKit.

Otros consejos

Desde la llamada CGDisplayIOServicePort ha sido desaprobados en OS X 10.9 Mavericks, que puede ser mejor para adquirir el servicio IOFramebuffer a través de una llamada coincidente apropiada de esta manera:

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);
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top