From bf97f90b74e3c9383005d63ef61a82512b118cff Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 19 Dec 2019 10:45:03 +0100 Subject: [PATCH] First functional version --- xbrightness.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ xbrightness.h | 8 ++++++++ 2 files changed, 65 insertions(+) diff --git a/xbrightness.c b/xbrightness.c index e69de29..5dc857d 100644 --- a/xbrightness.c +++ b/xbrightness.c @@ -0,0 +1,57 @@ +#include "xbrightness.h" + +void cmd(char *command, char *output) { + FILE *fp; + + fp = popen(command, "r"); + if (fp == NULL) { + output[0] = 0; + } else { + fgets(output, sizeof(output), fp); + pclose(fp); + } +} + +float current() { + char out[1028]; + cmd("xrandr --verbose | grep Brightness: | cut -f2 -d\" \"", out); + return strtof(out, NULL); +} + +void set(float brightness) { + char screen[1028]; + char command[1028] = "xrandr --output "; + char command2[] = " --brightness "; + char b[3]; + cmd("xrandr | grep \" connected\" | cut -f1 -d\" \"", screen); + screen[strlen(screen)-1] = 0; + strcat(command, screen); + strcat(command, command2); + gcvt(brightness, 3, b); + strcat(command, b); + cmd(command, NULL); +} + +int main(int argc, char **argv) { + if (argc == 1) { + printf("Current: %.2f\n", current()); + return 0; + } else if (argc > 2) { + printf("Too many arguments !\n"); + return 1; + } + + char *entPrt; + float b = strtof(argv[1], &entPrt); + if (*entPrt != '\0') { + printf("Invalid brightness level !\n"); + return 1; + } else { + if (argv[1][0] == '+' ||argv[1][0] == '-') { + set(current()+b); + } else { + set(b); + } + } + return 0; +} diff --git a/xbrightness.h b/xbrightness.h index e69de29..c1238a3 100644 --- a/xbrightness.h +++ b/xbrightness.h @@ -0,0 +1,8 @@ +#ifndef XBRIGHTNESS_H +#define XBRIGHTNESS_H + +#include +#include +#include + +#endif \ No newline at end of file