Giles
Giles

Reputation: 1667

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?

Upvotes: 5

Views: 1028

Answers (1)

LaPolly
LaPolly

Reputation: 51

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

Upvotes: 3

Related Questions