Skip to content

Instantly share code, notes, and snippets.

@atr000
Created December 12, 2010 09:57
Show Gist options
  • Save atr000/737965 to your computer and use it in GitHub Desktop.
Save atr000/737965 to your computer and use it in GitHub Desktop.
iokit / applicationservices cli to change the display brightness
/*
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