commit 2d3f54a9198b808b786f4e4135e1cc373206e1b7 Author: Kaan Barmore-Genc Date: Wed Apr 26 20:20:26 2023 -0400 Initialize diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f5c14c7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +backlight-control diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..872e970 --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ +CFLAGS=-Wall -Wextra -Werror -std=c99 -pedantic -O2 + +backlight-control: main.c + $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) diff --git a/README.md b/README.md new file mode 100644 index 0000000..42f511d --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +A basic tool to increase and decrease intel backlight percent. + +## Build + +You'll need `make` and some C compiler installed. + +``` +make +``` + +That's it. + +## Usage + +Copy the `backlight-control` file to `/usr/local/bin/` or somewhere similar. +Then run `backlight-control` to get the current brightness as a percentage, or +`backlight-control +1` to increase brightness by 1 percent, or +`backlight-control -1` to decrease brightness by 1 percent. Or higher numbers +for higher percentages at a time. diff --git a/main.c b/main.c new file mode 100644 index 0000000..15f7bfd --- /dev/null +++ b/main.c @@ -0,0 +1,97 @@ +#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); +}