From 7118692e506f9698a77c7cceea3b1dd47bb4b99a Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 13 Oct 2022 00:03:37 +0200 Subject: Move functions to their own files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- liblux.c | 208 ---------------------------------------------------------- lux.h | 51 -------------- src/common.c | 27 ++++++++ src/common.h | 31 +++++++++ src/lux.h | 57 ++++++++++++++++ src/luxdec.c | 24 +++++++ src/luxdecp.c | 24 +++++++ src/luxfree.c | 30 +++++++++ src/luxget.c | 39 +++++++++++ src/luxgetp.c | 27 ++++++++ src/luxinc.c | 24 +++++++ src/luxincp.c | 24 +++++++ src/luxinit.c | 68 +++++++++++++++++++ src/luxmax.c | 45 +++++++++++++ src/luxset.c | 58 ++++++++++++++++ src/luxsetp.c | 25 +++++++ 16 files changed, 503 insertions(+), 259 deletions(-) delete mode 100644 liblux.c delete mode 100644 lux.h create mode 100644 src/common.c create mode 100644 src/common.h create mode 100644 src/lux.h create mode 100644 src/luxdec.c create mode 100644 src/luxdecp.c create mode 100644 src/luxfree.c create mode 100644 src/luxget.c create mode 100644 src/luxgetp.c create mode 100644 src/luxinc.c create mode 100644 src/luxincp.c create mode 100644 src/luxinit.c create mode 100644 src/luxmax.c create mode 100644 src/luxset.c create mode 100644 src/luxsetp.c 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 - -#include -#include -#include -#include - -#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/lux.h b/lux.h deleted file mode 100644 index 2ac1637..0000000 --- a/lux.h +++ /dev/null @@ -1,51 +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. - */ - -#ifndef LUX_H -#define LUX_H - -#include - -#define BACKLIGHT_DIR "/sys/class/backlight" - -#ifdef __cplusplus -extern "C" { -#endif - -struct luxdisp { - 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); - -#ifdef __cplusplus -} -#endif - -#endif /* !LUX_H */ 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 +#include + +#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/src/lux.h b/src/lux.h new file mode 100644 index 0000000..9296bc5 --- /dev/null +++ b/src/lux.h @@ -0,0 +1,57 @@ +/* + * 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_H +#define LUX_H + +#include + +#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 + +typedef struct { + int __dirfd, __max; + FILE *__bstream; +} 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 +} +#endif + +#endif /* !LUX_H */ 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 +#include + +#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 + +#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 + +#include +#include + +#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 +#include +#include + +#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 +#include + +#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)); +} -- cgit v1.2.3