Skip to content

Instantly share code, notes, and snippets.

@alexwebr
Last active August 29, 2015 14:14
Show Gist options
  • Save alexwebr/242fbf9c99619de6a8ef to your computer and use it in GitHub Desktop.
Save alexwebr/242fbf9c99619de6a8ef to your computer and use it in GitHub Desktop.
Mark this as setuid and put in your path, call with 'up' or 'down' to adjust your laptop's brightness.
#include <stdio.h>
const char *path = "/sys/devices/LNXSYSTM:00/LNXSYBUS:00/TOS6208:00/backlight/toshiba/brightness";
int main(int argc, char *argv[])
{
if (argc != 2) {
fputs("Need exactly one argument", stderr);
return 1;
}
FILE *f = fopen(path, "r+");
if (f == NULL) {
perror("Couldn't open the backlight special file");
return 1;
}
// On this particular laptop, valid values are a single ASCII digit between
// 0 and 7, inclusive. So we just read one character. And by the magic of
// ASCII, incrementing the character ASCII code increments the digit.
int current = fgetc(f);
if (current == EOF) {
fputs("Got EOF when reading from the backlight special file", stderr);
goto err;
}
if (strcmp(argv[1], "up") == 0) {
current++;
} else if (strcmp(argv[1], "down") == 0) {
current--;
} else {
fputs("Invalid argument, need 'up' or 'down'", stderr);
goto err;
}
if (fputc(current, f) == EOF) {
fputs("Got EOF when writing to the backlight special file", stderr);
goto err;
}
fclose(f);
return 0;
err:
fclose(f);
return 1;
}
"brightness up"
XF86MonBrightnessUp
"brightness down"
XF86MonBrightnessDown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment