Created
December 12, 2010 09:57
-
-
Save atr000/737965 to your computer and use it in GitHub Desktop.
iokit / applicationservices cli to change the display brightness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
display-brightness.c | |
gcc -o display-brightness display-brightness.c \ | |
-framework IOKit -framework ApplicationServices | |
*/ | |
#include <stdio.h> | |
#include <IOKit/graphics/IOGraphicsLib.h> | |
#include <ApplicationServices/ApplicationServices.h> | |
#define PROGNAME "display-brightness" | |
FILE *displayData; | |
void | |
usage(void) | |
{ | |
fprintf(stderr, "usage: %s <brightness>\n" | |
" where 0.0 <= brightness <= 1.0\n", PROGNAME); | |
exit(1); | |
} | |
int | |
main(int argc, char **argv) | |
{ | |
CGDisplayErr dErr; | |
io_service_t service; | |
CGDirectDisplayID targetDisplay; | |
CFStringRef key = CFSTR(kIODisplayBrightnessKey); | |
float brightness = HUGE_VALF; | |
switch (argc) { | |
case 1: | |
break; | |
case 2: | |
brightness = strtof(argv[1], NULL); | |
break; | |
default: | |
usage(); | |
break; | |
} | |
targetDisplay = CGMainDisplayID(); | |
service = CGDisplayIOServicePort(targetDisplay); | |
if (brightness != HUGE_VALF) { // set the brightness, if requested | |
dErr = IODisplaySetFloatParameter(service, kNilOptions, key, brightness); | |
} | |
// now get the brightness | |
dErr = IODisplayGetFloatParameter(service, kNilOptions, key, &brightness); | |
if (dErr != kIOReturnSuccess) { | |
fprintf(stderr, "operation failed\n"); | |
} else { | |
displayData = fopen("/Library/Application Support/EyeSaver/DisplayBrightness.txt", "w"); | |
fprintf(displayData, "%f", brightness, ""); | |
fclose(displayData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment