aboutsummaryrefslogtreecommitdiff
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
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
-rw-r--r--liblux.c84
-rw-r--r--lux.h11
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)
{
@@ -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
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 */