Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Programmatically Set Screen Rotation Configuration in Cocoa?

In Systems Preferences -> Displays on OS X it is possible to set the rotation of a connected screen.

Is there anyway I can set this value programmatically?

I can get the current setting using CGDisplayRotation. I seem to be able to set many of the screen properties such as resolution in a simple transaction:

CGDisplayConfigRef config;
CGError error = CGBeginDisplayConfiguration(&config);
...
error = CGCompleteDisplayConfiguration(config, kCGConfigurePermanently);

...but I cannot find anyway to set this specific property.

Has anyone know of a way of doing this?

like image 861
Giles Avatar asked Oct 21 '22 04:10

Giles


1 Answers

This can be done by undocumented way. Example sets display orientation on 90 degrease:

CGDirectDisplayID display = CGMainDisplayID();
io_service_t service = CGDisplayIOServicePort(display);
IOOptionBits options = (0x00000400 | (kIOScaleRotate90)  << 16);
IOServiceRequestProbe(service, options);

Constants are defined in IOKit.framework IOGraphicsTypes.h

like image 190
LaPolly Avatar answered Oct 31 '22 18:10

LaPolly