dwm

personal fork of dwm (rnpnr branch)
git clone anongit@rnpnr.xyz:dwm.git
Log | Files | Refs | Feed | README | LICENSE

Commit: 17d72fecc7d49de2ea0d2c17181308ce2e20ea49
Parent: de6594ba9da43e935254ef9b7b954ea8fbdbf6ef
Author: Randy Palamar
Date:   Tue, 24 Dec 2024 13:19:50 -0700

add custom.c for custom c code

I'm kind of sick of having shell scripts all over the place that
dwm calls. As much as is possible I'm going to be moving those
here where I can use a real programming language that can complete
the task in a fraction of the cpu cycles.

Diffstat:
MMakefile | 2+-
Acustom.c | 61+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile @@ -11,7 +11,7 @@ all: dwm .c.o: ${CC} -c ${CFLAGS} $< -${OBJ}: config.h config.mk +${OBJ}: config.h config.mk custom.c config.h: cp config.def.h $@ diff --git a/custom.c b/custom.c @@ -0,0 +1,61 @@ +#include <fcntl.h> + +#define KEYBIND_FN(name) void name(const Arg *a) + +#define CLAMP(x, a, b) ((x) < (a) ? (a) : (x) > (b) ? (b) : (x)) +#define CLAMP01(x) CLAMP(x, 0, 1) + +typedef float f32; + +struct backlight_arg { + const char *backlight; /* /sys/class/backlight/XXX */ + float delta; /* [%] */ +}; + +static long +read_long(int fd) +{ + char buffer[64]; + ptrdiff_t rlen = read(fd, buffer, sizeof(buffer)); + close(fd); + if (rlen == -1) + return 0; + if (rlen >= sizeof(buffer)) + rlen = sizeof(buffer) - 1; + buffer[rlen] = 0; + return strtol(buffer, 0, 10); +} + +static KEYBIND_FN(backlight) +{ + const struct backlight_arg *ba = a->v; + static f32 max_brightness; + + char path[1024]; + /* NOTE(rnp): this could simplified using the my stream system */ + if (!max_brightness) { + snprintf(path, sizeof(path), "/sys/class/backlight/%s/max_brightness", ba->backlight); + int fd = open(path, O_RDONLY); + if (fd != -1) + max_brightness = read_long(fd); + } + + long current_brightness; + snprintf(path, sizeof(path), "/sys/class/backlight/%s/brightness", ba->backlight); + int fd = open(path, O_RDONLY); + if (fd != -1) { + current_brightness = read_long(fd); + close(fd); + fd = open(path, O_WRONLY); + if (fd != -1 && max_brightness) { + f32 current_percent = 100 * current_brightness / max_brightness; + current_percent += ba->delta; + current_percent = CLAMP(current_percent, 1, 100); + current_percent /= 100; + long len = snprintf(path, sizeof(path), "%ld\n", + (long)(current_percent * max_brightness)); + write(fd, path, len); + close(fd); + } + } +}