diff options
-rw-r--r-- | liblux.c | 84 | ||||
-rw-r--r-- | lux.h | 11 |
2 files changed, 71 insertions, 24 deletions
@@ -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) { @@ -52,6 +61,29 @@ luxinit(struct luxdisp *disp) } 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) { /* If we don't have an open stream with the brightness file yet then @@ -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 @@ -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 */ |