#include #include void print_usage(char *argv[]) { fprintf(stderr, "Usage: %s \n" "\n" "value:\n" "\t +percent - increase brightness by percent\n" "\t -percent - decrease brightness by percent\n", argv[0]); } int read_file(const char *path) { FILE *file = fopen(path, "r"); int value; int read = fscanf(file, "%d", &value); fclose(file); if (read != 1) { printf("Error reading %s\n", path); exit(1); } return value; } int max_brightness() { return read_file("/sys/class/backlight/intel_backlight/max_brightness"); } int brightness() { return read_file("/sys/class/backlight/intel_backlight/brightness"); } int brightness_percent() { return (brightness() * 100) / max_brightness(); } int main(int argc, char *argv[]) { if (argc < 2) { printf("%d\n", brightness_percent()); return 0; } if (argc > 3) { print_usage(argv); exit(2); } int positive = 0; if (argv[1][0] == '-') { positive = 0; } else if (argv[1][0] == '+') { positive = 1; } else { print_usage(argv); exit(2); } int percent; char *number_str = argv[1] + 1; int read = sscanf(number_str, "%d", &percent); if (read != 1) { print_usage(argv); exit(3); } int current = brightness(); int max = max_brightness(); int change = (max * percent) / 100 * (positive ? 1 : -1); FILE *file = fopen("/sys/class/backlight/intel_backlight/brightness", "w"); int set_to = current + change; // clamp if (set_to > max) { set_to = max; } if (set_to < 0) { set_to = 1; } fprintf(file, "%d", set_to); }