Saturday, June 30, 2012

Compile Updated SleepDisplay for OSX Lion

NOTE: I didn't write any of this code. I just found it and put it together.

Git here:

copied the code below from the file SleepDisplay.m found here:

forum discussion here:
last post

This is an updated version of the code I described in this post. it is updated to wake the screen also, with a -wake argument, since Lion doesn't seem to do that now with scripted keystrokes

open Textedit, paste whats between the lines below into it:

// gcc -Wall SleepDisplay.c -o SleepDisplay -framework CoreFoundation -framework IOKit
// I just put that above line in to remind me what command to run in terminal
 * SleepDisplay
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <>. 

#import <Foundation/Foundation.h>
#import <IOKit/IOKitLib.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSArray *params = [[NSProcessInfo processInfo] arguments];
    BOOL shouldWake = NO;

    for (NSString *arg in params)
        if([arg isEqualToString:@"-wake"] || [arg isEqualToString:@"--wake"] || [arg isEqualToString:@"-w"])
            shouldWake = YES;
        if([arg isEqualToString:@"-help"] || [arg isEqualToString:@"--help"] || [arg isEqualToString:@"-h"])
            printf("usage: SleepDisplay [-w]\n");
            printf("Without options, sleeps the display (not system sleep)\n");
            printf("use with the -w (--wake) option to wake\n");
            // quit.
            [pool drain];
            return 0;
    io_registry_entry_t entry = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler");
        IORegistryEntrySetCFProperty(entry, CFSTR("IORequestIdle"), shouldWake ? kCFBooleanFalse : kCFBooleanTrue);
    [pool drain];
    return 0;

if you're using text edit make sure you go under format menu "Make Plain Text"
Save this file as "SleepDisplay.txt"


change the extention .txt to .c so now the file name is "SleepDisplay.c" to do this I ran in terminal:

mv SleepDisplay.txt SleepDisplay.c


(because apple removed it or something form Lion, see:

run terminal in the same folder as the file and copy the next line into it & hit return:
gcc -Wall SleepDisplay.c -o SleepDisplay -framework CoreFoundation -framework IOKit

if it doesn't work you might have to use "su" first and type in your admin password then run the command again.

--- still testing but moving on to the next post, this bores me now. seems to work tho...

No comments:

Post a Comment