Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Change Display Rotation on Mac OS using Xcode

I'm trying to write a small Objective-C command line tool that adjusts the rotation of a given display.

Sadly, I can not find any reference in Apple's documentation that tells how to change the rotation (aside from getting it). I think it has to be done with using CGDisplayModeRef but I can't find out how. Any help would be appreciated!

like image 390
Nicolas Avatar asked Oct 03 '22 17:10

Nicolas


1 Answers

Extracted from https://github.com/CdLbB/fb-rotate - it seems IOKit can do the job although the behavior seems a bit odd to me. In a nutshell:

io_service_t service = CGDisplayIOServicePort(myDisplayID); //IOKit Handle (get myDisplayID yourself)
IOOptionBits options = (0x00000400 | (kIOScaleRotate90)  << 16) //Some undocumented hex'ing
IOServiceRequestProbe(service, options); //Set rotation to display

Don't forget to link against IOKit.

like image 190
Nicolas Avatar answered Oct 12 '22 10:10

Nicolas