aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2022-10-13 00:03:37 +0200
committerThomas Voss <mail@thomasvoss.com> 2022-10-13 00:03:37 +0200
commit7118692e506f9698a77c7cceea3b1dd47bb4b99a (patch)
treed37a10abde76a7fb063e57135d4408973e99cc0d
parentcd3b1a5ba87b804fb7f8b620460d49a5d305a730 (diff)
Move functions to their own files
Also we move the source code to a src/ directory. We also move from using `struct luxdisp` to `lux_t` to signify that we don’t want the user to be messing with the struct members.
-rw-r--r--liblux.c208
-rw-r--r--src/common.c27
-rw-r--r--src/common.h31
-rw-r--r--src/lux.h (renamed from lux.h)38
-rw-r--r--src/luxdec.c24
-rw-r--r--src/luxdecp.c24
-rw-r--r--src/luxfree.c30
-rw-r--r--src/luxget.c39
-rw-r--r--src/luxgetp.c27
-rw-r--r--src/luxinc.c24
-rw-r--r--src/luxincp.c24
-rw-r--r--src/luxinit.c68
-rw-r--r--src/luxmax.c45
-rw-r--r--src/luxset.c58
-rw-r--r--src/luxsetp.c25
15 files changed, 468 insertions, 224 deletions
diff --git a/liblux.c b/liblux.c
deleted file mode 100644
index 8061eec..0000000
--- a/liblux.c
+++ /dev/null
@@ -1,208 +0,0 @@
-/*
- * BSD Zero Clause License
- *
- * Copyright (c) 2022 Thomas Voss
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
- * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
- * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
- * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- * PERFORMANCE OF THIS SOFTWARE.
- */
-
-#define _POSIX_C_SOURCE 200809L
-
-#include <sys/types.h>
-
-#include <dirent.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <unistd.h>
-
-#include "lux.h"
-
-#ifdef O_PATH
- #define GETDIR_FLAGS O_PATH
-#else
- #define GETDIR_FLAGS (O_RDONLY | O_DIRECTORY)
-#endif
-
-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)
-{
- /* Set the default values for the luxdisp struct. We want to be as lazy
- * as possible, so we don't bother with __bstream and __max yet. We do
- * need __dirfd though.
- */
- *disp = (struct luxdisp) {
- .__bstream = NULL,
- .__max = -1,
- .__dirfd = getdir()
- };
- 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)
-{
- /* If we don't have an open stream with the brightness file yet then
- * open one. If we do, we instead rewind to the start of the file so
- * that we don't just read an EOF.
- */
- if (disp->__bstream == NULL) {
- if ((disp->__bstream = getbstream(disp->__dirfd)) == NULL)
- return -1;
- } else
- rewind(disp->__bstream);
-
- int n;
- fscanf(disp->__bstream, "%d", &n);
- return n;
-}
-
-int
-luxset(struct luxdisp *disp, int n)
-{
- /* The same deal as in luxget() */
- if (disp->__bstream == NULL) {
- if ((disp->__bstream = getbstream(disp->__dirfd)) == NULL)
- return -1;
- } else
- rewind(disp->__bstream);
-
- /* After writing the new brightness value to the brightness file, we
- * need to truncate it so that luxset(disp, 1234); luxset(disp, 9);
- * gives us a final brightness of 2 and not 9234.
- */
- fprintf(disp->__bstream, "%d", n);
- ftruncate(fileno(disp->__bstream), intlen(n));
- return n;
-}
-
-int
-luxinc(struct luxdisp *disp, int n)
-{
- return luxset(disp, luxget(disp) + n);
-}
-
-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;
-}
-
-double
-luxsetp(struct luxdisp *disp, double p)
-{
- int max = luxmax(disp);
- return max == -1 ? -1 : luxset(disp, (int) (p / 100 * max));
-}
-
-double
-luxincp(struct luxdisp *disp, double p)
-{
- return luxsetp(disp, luxgetp(disp) + p);
-}
-
-double
-luxdecp(struct luxdisp *disp, double p)
-{
- return luxincp(disp, -p);
-}
-
-int
-intlen(int n)
-{
- return n < 10 ? 1
- : n < 100 ? 2
- : n < 1000 ? 3
- : n < 10000 ? 4
- : n < 100000 ? 5
- : n < 1000000 ? 6
- : n < 10000000 ? 7
- : n < 100000000 ? 8
- : n < 1000000000 ? 9
- : 10;
-}
-
-int
-getdir(void)
-{
- int fd, dfd;
- struct dirent *dp;
- DIR *dir;
-
- /* To get the file descriptor for the directory where all the brightness
- * files are located, we first need to open BACKLIGHT_DIR which is the
- * directory where the backlight directories are located. Then we need
- * to get a DIR* of that directory and get the 3rd entry. We need the
- * third because the fist and second are the . and .. folders. Finally
- * we open the folder so that we have a file descriptor we can return.
- */
- if ((dfd = open(BACKLIGHT_DIR, O_RDONLY)) == -1
- || (dir = fdopendir(dfd)) == NULL
- || (dp = readdir(dir)) == NULL
- || (dp = readdir(dir)) == NULL
- || (dp = readdir(dir)) == NULL
- || (fd = openat(dfd, dp->d_name, GETDIR_FLAGS)) == -1)
- return -1;
-
- closedir(dir);
- return fd;
-}
-
-FILE *
-getbstream(int dirfd)
-{
- return fdopen(openat(dirfd, "brightness", O_RDWR), "r+");
-}
diff --git a/src/common.c b/src/common.c
new file mode 100644
index 0000000..6ae23a7
--- /dev/null
+++ b/src/common.c
@@ -0,0 +1,27 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <fcntl.h>
+#include <stdio.h>
+
+#include "common.h"
+
+FILE *
+getbstream(int dirfd)
+{
+ return fdopen(openat(dirfd, "brightness", O_RDWR), "r+");
+}
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..a7739c2
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,31 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#ifndef LUX_COMMON_H
+#define LUX_COMMON_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+FILE *getbstream(int);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* !LUX_COMMON_H */
diff --git a/lux.h b/src/lux.h
index 2ac1637..9296bc5 100644
--- a/lux.h
+++ b/src/lux.h
@@ -20,29 +20,35 @@
#include <stdio.h>
-#define BACKLIGHT_DIR "/sys/class/backlight"
+#define LUX_BACKLIGHT_DIR "/sys/class/backlight"
+#ifdef O_PATH
+ #define LUX_GDIR_FLAGS O_PATH
+#else
+ #define LUX_GDIR_FLAGS (O_RDONLY | O_DIRECTORY)
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
-struct luxdisp {
+typedef struct {
int __dirfd, __max;
FILE *__bstream;
-};
-
-void luxfree(struct luxdisp *);
-int luxinit(struct luxdisp *);
-int luxmax(struct luxdisp *);
-int luxget(struct luxdisp *);
-int luxset(struct luxdisp *, int);
-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);
+} lux_t;
+
+void luxfree(lux_t *);
+int luxinit(lux_t *);
+int luxmax(lux_t *);
+int luxget(lux_t *);
+int luxset(lux_t *, int);
+int luxinc(lux_t *, int);
+int luxdec(lux_t *, int);
+
+double luxgetp(lux_t *);
+double luxsetp(lux_t *, double);
+double luxincp(lux_t *, double);
+double luxdecp(lux_t *, double);
#ifdef __cplusplus
}
diff --git a/src/luxdec.c b/src/luxdec.c
new file mode 100644
index 0000000..2554bfd
--- /dev/null
+++ b/src/luxdec.c
@@ -0,0 +1,24 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "lux.h"
+
+int
+luxdec(lux_t *disp, int n)
+{
+ return luxinc(disp, -n);
+}
diff --git a/src/luxdecp.c b/src/luxdecp.c
new file mode 100644
index 0000000..6bf2c4c
--- /dev/null
+++ b/src/luxdecp.c
@@ -0,0 +1,24 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "lux.h"
+
+double
+luxdecp(lux_t *disp, double p)
+{
+ return luxincp(disp, -p);
+}
diff --git a/src/luxfree.c b/src/luxfree.c
new file mode 100644
index 0000000..579f3b5
--- /dev/null
+++ b/src/luxfree.c
@@ -0,0 +1,30 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include "lux.h"
+
+void
+luxfree(lux_t *disp)
+{
+ if (disp->__dirfd != -1)
+ close(disp->__dirfd);
+ if (disp->__bstream != NULL)
+ fclose(disp->__bstream);
+}
diff --git a/src/luxget.c b/src/luxget.c
new file mode 100644
index 0000000..39c3b6f
--- /dev/null
+++ b/src/luxget.c
@@ -0,0 +1,39 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio.h>
+
+#include "common.h"
+#include "lux.h"
+
+int
+luxget(lux_t *disp)
+{
+ /* If we don't have an open stream with the brightness file yet then
+ * open one. If we do, we instead rewind to the start of the file so
+ * that we don't just read an EOF.
+ */
+ if (disp->__bstream == NULL) {
+ if ((disp->__bstream = getbstream(disp->__dirfd)) == NULL)
+ return -1;
+ } else
+ rewind(disp->__bstream);
+
+ int n;
+ fscanf(disp->__bstream, "%d", &n);
+ return n;
+}
diff --git a/src/luxgetp.c b/src/luxgetp.c
new file mode 100644
index 0000000..8304bb5
--- /dev/null
+++ b/src/luxgetp.c
@@ -0,0 +1,27 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "lux.h"
+
+double
+luxgetp(lux_t *disp)
+{
+ int cur, max;
+ if ((cur = luxget(disp)) == -1 || (max = luxmax(disp)) == -1)
+ return -1;
+ return (double) cur / max * 100;
+}
diff --git a/src/luxinc.c b/src/luxinc.c
new file mode 100644
index 0000000..0849205
--- /dev/null
+++ b/src/luxinc.c
@@ -0,0 +1,24 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "lux.h"
+
+int
+luxinc(lux_t *disp, int n)
+{
+ return luxset(disp, luxget(disp) + n);
+}
diff --git a/src/luxincp.c b/src/luxincp.c
new file mode 100644
index 0000000..d8ff3cb
--- /dev/null
+++ b/src/luxincp.c
@@ -0,0 +1,24 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "lux.h"
+
+double
+luxincp(lux_t *disp, double p)
+{
+ return luxsetp(disp, luxgetp(disp) + p);
+}
diff --git a/src/luxinit.c b/src/luxinit.c
new file mode 100644
index 0000000..ae25925
--- /dev/null
+++ b/src/luxinit.c
@@ -0,0 +1,68 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+
+#include <dirent.h>
+#include <fcntl.h>
+
+#include "lux.h"
+
+static int getdir(void);
+
+int
+luxinit(lux_t *disp)
+{
+ /* Set the default values for the luxdisp struct. We want to be as lazy
+ * as possible, so we don't bother with __bstream and __max yet. We do
+ * need __dirfd though.
+ */
+ *disp = (lux_t) {
+ .__bstream = NULL,
+ .__max = -1,
+ .__dirfd = getdir()
+ };
+ return disp->__dirfd;
+}
+
+int
+getdir(void)
+{
+ int fd, dfd;
+ struct dirent *dp;
+ DIR *dir;
+
+ /* To get the file descriptor for the directory where all the brightness
+ * files are located, we first need to open LUX_BACKLIGHT_DIR which is
+ * the directory where the backlight directories are located. Then we
+ * need to get a DIR* of that directory and get the 3rd entry. We need
+ * the third because the fist and second are the . and .. folders.
+ * Finally we open the folder so that we have a file descriptor we can
+ * return.
+ */
+ if ((dfd = open(LUX_BACKLIGHT_DIR, O_RDONLY)) == -1
+ || (dir = fdopendir(dfd)) == NULL)
+ return -1;
+ if ((dp = readdir(dir)) == NULL
+ || (dp = readdir(dir)) == NULL
+ || (dp = readdir(dir)) == NULL
+ || (fd = openat(dfd, dp->d_name, LUX_GDIR_FLAGS)) == -1)
+ fd = -1;
+
+ closedir(dir);
+ return fd;
+}
diff --git a/src/luxmax.c b/src/luxmax.c
new file mode 100644
index 0000000..b30f02b
--- /dev/null
+++ b/src/luxmax.c
@@ -0,0 +1,45 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "lux.h"
+
+int
+luxmax(lux_t *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;
+}
diff --git a/src/luxset.c b/src/luxset.c
new file mode 100644
index 0000000..b707efd
--- /dev/null
+++ b/src/luxset.c
@@ -0,0 +1,58 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include "common.h"
+#include "lux.h"
+
+static int intlen(int);
+
+int
+luxset(lux_t *disp, int n)
+{
+ /* The same deal as in luxget() */
+ if (disp->__bstream == NULL) {
+ if ((disp->__bstream = getbstream(disp->__dirfd)) == NULL)
+ return -1;
+ } else
+ rewind(disp->__bstream);
+
+ /* After writing the new brightness value to the brightness file, we
+ * need to truncate it so that luxset(disp, 1234); luxset(disp, 9);
+ * gives us a final brightness of 2 and not 9234.
+ */
+ fprintf(disp->__bstream, "%d", n);
+ ftruncate(fileno(disp->__bstream), intlen(n));
+ return n;
+}
+
+int
+intlen(int n)
+{
+ return n < 10 ? 1
+ : n < 100 ? 2
+ : n < 1000 ? 3
+ : n < 10000 ? 4
+ : n < 100000 ? 5
+ : n < 1000000 ? 6
+ : n < 10000000 ? 7
+ : n < 100000000 ? 8
+ : n < 1000000000 ? 9
+ : 10;
+}
diff --git a/src/luxsetp.c b/src/luxsetp.c
new file mode 100644
index 0000000..1ec1d61
--- /dev/null
+++ b/src/luxsetp.c
@@ -0,0 +1,25 @@
+/*
+ * BSD Zero Clause License
+ *
+ * Copyright (c) 2022 Thomas Voss
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
+ * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "lux.h"
+
+double
+luxsetp(lux_t *disp, double p)
+{
+ int max = luxmax(disp);
+ return max == -1 ? -1 : luxset(disp, (int) (p / 100 * max));
+}