From ccebe78c6caa4f05dcb24cba4f146fc6c95ab487 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Thu, 10 Aug 2023 03:01:18 +0300 Subject: rename source file --- intel_backlight_ctl.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 137 -------------------------------------------------- 2 files changed, 137 insertions(+), 137 deletions(-) create mode 100644 intel_backlight_ctl.c delete mode 100644 main.c diff --git a/intel_backlight_ctl.c b/intel_backlight_ctl.c new file mode 100644 index 0000000..70f6822 --- /dev/null +++ b/intel_backlight_ctl.c @@ -0,0 +1,137 @@ +/* SPDX-License-Identifier: BSD-2-Clause */ + +#include +#include +#include +#include + +#define CUR_PATH "/sys/class/backlight/intel_backlight/brightness" +#define MAX_PATH "/sys/class/backlight/intel_backlight/max_brightness" +#define STEP 10 + +struct brightness { + int max_raw; + int raw; + int percents; +}; + + + +int readval(const char *path, int *dst) +{ + FILE *f = fopen(path, "r"); + int retval = 1; + + if (f == NULL) { + perror(path); + goto end; + } + + int raw; + if (fscanf(f, "%d", &raw) != 1) { + perror(path); + goto end; + } + + *dst = raw; + retval = 0; + +end: + if (f != NULL) + fclose(f); + return retval; +} + + +int get(struct brightness *dst) +{ + int retval = 1; + if (readval(CUR_PATH, &dst->raw)) + goto end; + if (readval(MAX_PATH, &dst->max_raw)) + goto end; + + dst->percents = (int)round((double)dst->raw*100/dst->max_raw); + retval = 0; + +end: + return retval; +} + +int set(struct brightness *src) +{ + int retval = 1; + FILE *f = fopen(CUR_PATH, "w"); + if (!f) { + perror(CUR_PATH); + goto end; + } + + fprintf(f, "%d", src->raw); + retval = 0; + +end: + if (f != NULL) + fclose(f); + return retval; +} + +void adjust(struct brightness *b, int value) +{ + b->percents += value; + if (b->percents > 100) + b->percents = 100; + else if (b->percents < 0) + b->percents = 0; + b->raw = (int)ceil((double)b->percents*b->max_raw/100); +} + + +void dump(struct brightness *b) +{ + printf("%d (%d%%)\n", b->raw, b->percents); +} + + +int main(int argc, char** argv) +{ + int retval = 0; + struct brightness b; + int adjust_val = STEP; + bool decr = false; + + if (argc > 1 && strcmp(argv[1], "-h") == 0) { + printf("usage: %s [-h] [+/-]\n", argv[0]); + goto end; + } + + if (get(&b)) + goto end; + + if (argc <= 1) { + dump(&b); + goto end; + } + + if (strcmp(argv[1], "-") == 0) + decr = true; + else if (strcmp(argv[1], "+") != 0) { + fprintf(stderr, "%s: unknown argument\n", argv[1]); + retval = 1; + goto end; + } + + if (decr) + adjust_val *= -1; + + adjust(&b, adjust_val); + if (set(&b)) { + retval = 1; + goto end; + } + + dump(&b); + +end: + return retval; +} diff --git a/main.c b/main.c deleted file mode 100644 index 70f6822..0000000 --- a/main.c +++ /dev/null @@ -1,137 +0,0 @@ -/* SPDX-License-Identifier: BSD-2-Clause */ - -#include -#include -#include -#include - -#define CUR_PATH "/sys/class/backlight/intel_backlight/brightness" -#define MAX_PATH "/sys/class/backlight/intel_backlight/max_brightness" -#define STEP 10 - -struct brightness { - int max_raw; - int raw; - int percents; -}; - - - -int readval(const char *path, int *dst) -{ - FILE *f = fopen(path, "r"); - int retval = 1; - - if (f == NULL) { - perror(path); - goto end; - } - - int raw; - if (fscanf(f, "%d", &raw) != 1) { - perror(path); - goto end; - } - - *dst = raw; - retval = 0; - -end: - if (f != NULL) - fclose(f); - return retval; -} - - -int get(struct brightness *dst) -{ - int retval = 1; - if (readval(CUR_PATH, &dst->raw)) - goto end; - if (readval(MAX_PATH, &dst->max_raw)) - goto end; - - dst->percents = (int)round((double)dst->raw*100/dst->max_raw); - retval = 0; - -end: - return retval; -} - -int set(struct brightness *src) -{ - int retval = 1; - FILE *f = fopen(CUR_PATH, "w"); - if (!f) { - perror(CUR_PATH); - goto end; - } - - fprintf(f, "%d", src->raw); - retval = 0; - -end: - if (f != NULL) - fclose(f); - return retval; -} - -void adjust(struct brightness *b, int value) -{ - b->percents += value; - if (b->percents > 100) - b->percents = 100; - else if (b->percents < 0) - b->percents = 0; - b->raw = (int)ceil((double)b->percents*b->max_raw/100); -} - - -void dump(struct brightness *b) -{ - printf("%d (%d%%)\n", b->raw, b->percents); -} - - -int main(int argc, char** argv) -{ - int retval = 0; - struct brightness b; - int adjust_val = STEP; - bool decr = false; - - if (argc > 1 && strcmp(argv[1], "-h") == 0) { - printf("usage: %s [-h] [+/-]\n", argv[0]); - goto end; - } - - if (get(&b)) - goto end; - - if (argc <= 1) { - dump(&b); - goto end; - } - - if (strcmp(argv[1], "-") == 0) - decr = true; - else if (strcmp(argv[1], "+") != 0) { - fprintf(stderr, "%s: unknown argument\n", argv[1]); - retval = 1; - goto end; - } - - if (decr) - adjust_val *= -1; - - adjust(&b, adjust_val); - if (set(&b)) { - retval = 1; - goto end; - } - - dump(&b); - -end: - return retval; -} -- cgit v1.2.3