aboutsummaryrefslogtreecommitdiff
path: root/liblux.c
diff options
context:
space:
mode:
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