aboutsummaryrefslogtreecommitdiff
path: root/liblux.c
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2022-08-27 22:15:40 +0200
committerThomas Voss <thomasvoss@live.com> 2022-08-27 22:15:40 +0200
commit5a055fafadbee61260649db3d482dcde798aaac4 (patch)
treee0ea03625dd1509e7f67c5328ee8a819da4f4e61 /liblux.c
parent3793bfc12d603b9f1c9495428c8b1a33f1e32d99 (diff)
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
Diffstat (limited to 'liblux.c')
-rw-r--r--liblux.c84
1 files changed, 62 insertions, 22 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)
{
@@ -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