Macos – Command to Sleep Display OSX

macos

Is there a command/API on OS X to put the monitor into sleep mode?

By sleep mode, I mean totally kill the output to the monitor. I know in the power settings one can configure this but that is time based.

Best Solution

This can be done with a semi-undocumented api.

#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>

/* Returns 0 on success and 1 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 1;
        }
        return 0;
}

GCC Flags: -framework CoreFoundation -framework IOKIT

IOKit Documentation