Skip to content

Instantly share code, notes, and snippets.

@yukirii
Created November 30, 2013 11:50
Show Gist options
  • Save yukirii/7718067 to your computer and use it in GitHub Desktop.
Save yukirii/7718067 to your computer and use it in GitHub Desktop.
// displaysleep
//
// Command to Sleep Display OSX
// http://stackoverflow.com/questions/1239439/command-to-sleep-display-osx
//
// Compile: gcc -o displaysleep -framework CoreFoundation -framework IOKIT displaysleep.c
//
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
/* Returns 1 on success and 0 on failure. */
int display_sleep(void)
{
io_registry_entry_t reg = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler");
if (reg) {
IORegistryEntrySetCFProperty(reg, CFSTR("IORequestIdle"), kCFBooleanTrue);
IOObjectRelease(reg);
} else {
return 0;
}
return 1;
}
int main(void)
{
if ( display_sleep() == 1 ) {
printf("Display sleeping...\n");
}
else {
printf("Failure sleep display...\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment