diff options
Diffstat (limited to 'bin/backlight.c')
| -rw-r--r-- | bin/backlight.c | 158 |
1 files changed, 158 insertions, 0 deletions
diff --git a/bin/backlight.c b/bin/backlight.c new file mode 100644 index 0000000..5894cd8 --- /dev/null +++ b/bin/backlight.c @@ -0,0 +1,158 @@ +#include <stdio.h> +#include <unistd.h> +#include <stdlib.h> +#include <fcntl.h> +#include <string.h> +#include <stdlib.h> +#include <limits.h> +#include <errno.h> + +const char *backlight_f[] = { + "/sys/class/backlight/intel_backlight/", + "/sys/class/backlight/amdgpu_bl0/", +}; + +void +die(const char *msg) +{ + puts(msg); + exit(1); +} + +void +err(const char *msg) +{ + perror(msg); + exit(1); +} + +void* +ecalloc(size_t nmemb, size_t size) +{ + void *ret = calloc(nmemb, size); + if (!ret) + err("ecalloc"); + return ret; +} + +char* +appendStr(size_t len, const char *a, const char *b) +{ + char *buf = ecalloc(len, sizeof(char)); + int ret = snprintf(buf, len, "%s%s", a, b); + if (ret == -1 || ret > len) + err("snprintf"); + return buf; +} + +/* returns errno */ +int +maxBrightness(const char *prefix, long *max) +{ + int e; + char *buf = appendStr(256, prefix, "/max_brightness"); + + int fd = open(buf, O_RDONLY); + if (fd == -1) { + e = errno; + free(buf); + return e; + } + + memset(buf, '\0', 256); + + int b = read(fd, buf, 256); + if (b==-1) { + e = errno; + free(buf); + close(fd); + return e; + } + + for (int i=0; i<256 ; i++) { + if (buf[i] == '\n') { + buf[i] = '\0'; + break; + } + } + + long n = strtol(buf, NULL, 10); + if (n == LONG_MIN || n == LONG_MAX || n == 0) + err("strtol"); + + free(buf); + buf = NULL; + close(fd); + *max = n; + + return 0; +} + +int +setBacklight(const char *prefix, int percent) +{ + char *fn = appendStr(256, prefix, "/brightness"); + + int fd = open(fn, O_WRONLY); + if (fd == -1) { + free(fn); + return -1; + } + + int n = dprintf(fd, "%d\n", percent); + if (n == -1) { + close(fd); + free(fn); + return -1; + } + + close(fd); + free(fn); + fn = NULL; +} + +int +main(int argc, char **argv) +{ + uid_t uid = getuid(); + gid_t gid = getgid(); + uid_t euid = geteuid(); + char **a = argv+1; + pid_t pid; + int ret; + + if (euid != 0) + die("Program must be run as root/setuid"); + + if (!*a) + die("Brightness percentage must be supplied"); + + + long percent = strtol(*a, NULL, 10); + if (percent <= LONG_MIN || percent >= LONG_MAX) + err("invalid percent"); + if (percent < 0 || percent > 100) + die("percent must be between 0 and 100"); + + + for (int i=0; i < sizeof(backlight_f)/sizeof(backlight_f[0]); i++) { + int ret; + long max = 0; + ret = maxBrightness(backlight_f[i], &max); + if (ret != 0) { + const char *msg = strerror(ret); + continue; + } + + float p = (double)max*((double)percent/100.0); + + ret = setBacklight(backlight_f[i], (int)p); + if (ret != 0) { + perror("setBacklight"); + } + } + + + + +} |
