From 5a055fafadbee61260649db3d482dcde798aaac4 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 27 Aug 2022 22:15:40 +0200 Subject: Add more library functions With this commit, the following functions have been added to the library: - int luxinc(struct luxdisp *, int) | Increment brightness by a raw value - int luxdec(struct luxdisp *, int) | Decrement brightness by a raw value - double luxgetp(struct luxdisp *) | Get the current brightness as a percentage - double luxsetp(struct luxdisp *, double) | Set the current brightness with a percentage - double luxincp(struct luxdisp *, double) | Increment brightness by a percentage - double luxdecp(struct luxdisp *, double) | Decrement brightness by a percentage --- liblux.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++----------------- lux.h | 11 +++++++-- 2 files changed, 71 insertions(+), 24 deletions(-) diff --git a/liblux.c b/liblux.c index 3195342..8061eec 100644 --- a/liblux.c +++ b/liblux.c @@ -36,6 +36,15 @@ static int intlen(int); static int getdir(void); static FILE *getbstream(int); +void +luxfree(struct luxdisp *disp) +{ + if (disp->__dirfd != -1) + close(disp->__dirfd); + if (disp->__bstream != NULL) + fclose(disp->__bstream); +} + int luxinit(struct luxdisp *disp) { @@ -51,6 +60,29 @@ luxinit(struct luxdisp *disp) return disp->__dirfd; } +int +luxmax(struct luxdisp *disp) +{ + /* The maximum brightness shouldn't change, so we can cache it in the + * struct. + */ + if (disp->__max != -1) + return disp->__max; + + int fd = openat(disp->__dirfd, "max_brightness", O_RDONLY); + if (fd == -1) + return -1; + FILE *stream = fdopen(fd, "r"); + if (stream == NULL) { + close(fd); + return -1; + } + + fscanf(stream, "%d", &disp->__max); + fclose(stream); + return disp->__max; +} + int luxget(struct luxdisp *disp) { @@ -89,35 +121,43 @@ luxset(struct luxdisp *disp, int n) } int -luxmax(struct luxdisp *disp) +luxinc(struct luxdisp *disp, int n) { - /* The maximum brightness shouldn't change, so we can cache it in the - * struct. - */ - if (disp->__max != -1) - return disp->__max; + return luxset(disp, luxget(disp) + n); +} - int fd = openat(disp->__dirfd, "max_brightness", O_RDONLY); - if (fd == -1) - return -1; - FILE *stream = fdopen(fd, "r"); - if (stream == NULL) { - close(fd); +int +luxdec(struct luxdisp *disp, int n) +{ + return luxinc(disp, -n); +} + +double +luxgetp(struct luxdisp *disp) +{ + int cur, max; + if ((cur = luxget(disp)) == -1 || (max = luxmax(disp)) == -1) return -1; - } + return (double) cur / max * 100; +} - fscanf(stream, "%d", &disp->__max); - fclose(stream); - return disp->__max; +double +luxsetp(struct luxdisp *disp, double p) +{ + int max = luxmax(disp); + return max == -1 ? -1 : luxset(disp, (int) (p / 100 * max)); } -void -luxfree(struct luxdisp *disp) +double +luxincp(struct luxdisp *disp, double p) { - if (disp->__dirfd != -1) - close(disp->__dirfd); - if (disp->__bstream != NULL) - fclose(disp->__bstream); + return luxsetp(disp, luxgetp(disp) + p); +} + +double +luxdecp(struct luxdisp *disp, double p) +{ + return luxincp(disp, -p); } int diff --git a/lux.h b/lux.h index 12198f9..9b21450 100644 --- a/lux.h +++ b/lux.h @@ -27,10 +27,17 @@ struct luxdisp { FILE *__bstream; }; +void luxfree(struct luxdisp *); int luxinit(struct luxdisp *); +int luxmax(struct luxdisp *); int luxget(struct luxdisp *); int luxset(struct luxdisp *, int); -int luxmax(struct luxdisp *); -void luxfree(struct luxdisp *); +int luxinc(struct luxdisp *, int); +int luxdec(struct luxdisp *, int); + +double luxgetp(struct luxdisp *); +double luxsetp(struct luxdisp *, double); +double luxincp(struct luxdisp *, double); +double luxdecp(struct luxdisp *, double); #endif /* !LUX_H */ -- cgit v1.2.3