From 4d0d0b5c0a37ae9e9bfaaf8fd7474d09a64ce451 Mon Sep 17 00:00:00 2001 From: DarthMooMancer Date: Sat, 9 Aug 2025 23:46:28 -0400 Subject: [PATCH] wip --- .config/suckless/slstatus/Makefile | 12 +--- .config/suckless/slstatus/slstatus.c | 84 +++++++++++----------------- 2 files changed, 35 insertions(+), 61 deletions(-) diff --git a/.config/suckless/slstatus/Makefile b/.config/suckless/slstatus/Makefile index e21ba0c..eb9f425 100644 --- a/.config/suckless/slstatus/Makefile +++ b/.config/suckless/slstatus/Makefile @@ -1,21 +1,15 @@ .POSIX: PREFIX = /usr/local -CC = gcc -CFLAGS = -std=c17 -pedantic -Wall -Wextra -Wno-unused-parameter -O2 -march=native \ - $(shell pkg-config --cflags x11) -D_DEFAULT_SOURCE -LDFLAGS = -s $(shell pkg-config --libs-only-L x11) -LDLIBS = $(shell pkg-config --libs-only-l x11) - -all: slstatus +CFLAGS = -std=c17 -pedantic -Wall -Wextra -Wno-unused-parameter -O2 -march=native $(shell pkg-config --cflags x11) -D_DEFAULT_SOURCE slstatus: slstatus.c - $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(LDLIBS) + gcc $(CFLAGS) -o $@ $< -s $(shell pkg-config --libs-only-L x11) $(shell pkg-config --libs-only-l x11) clean: rm -f slstatus -install: all +install: slstatus install -Dm755 slstatus "$(DESTDIR)$(PREFIX)/bin/slstatus" uninstall: diff --git a/.config/suckless/slstatus/slstatus.c b/.config/suckless/slstatus/slstatus.c index 6b08954..1047506 100644 --- a/.config/suckless/slstatus/slstatus.c +++ b/.config/suckless/slstatus/slstatus.c @@ -1,51 +1,32 @@ #define _GNU_SOURCE -#include // for AT_FDCWD +#define AT_FDCWD -100 #include #include #include #include #include -#include #include -#include -#include #include +#include -static volatile sig_atomic_t done; -struct arg { - const char *(*func)(const char *); - const char *fmt; - const char *args; -}; - -void die(const char *fmt, ...) { - va_list ap; - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - fputc('\n', stderr); +void die(const char *msg) { + fprintf(stderr, "%s\n", msg); exit(1); } -static void terminate() { - done = 1; -} - -const char* bgt(const char *_) { - (void)_; +const char* bgt() { static char buf[5]; time_t now = time(NULL), last = now; FILE *f = fopen("/tmp/last_wallpaper_time", "r"); - if (!f || fscanf(f, "%ld", &last) != 1) last = now; - if (f) fclose(f); + if(fscanf(f, "%ld", &last) != 1) last = now; + fclose(f); int rem = 600 - (int)(now - last); if (rem < 0) rem = 0; if (snprintf(buf, sizeof(buf), "%d:%02d", (rem / 60), (rem % 60)) < 0) return NULL; return buf; } -const char* vol(const char *_) { - (void)_; +const char* vol() { static char buf[5]; float v; FILE *fp = popen("wpctl get-volume @DEFAULT_AUDIO_SINK@", "r"); @@ -55,43 +36,46 @@ const char* vol(const char *_) { return buf; } -const char* age(const char *_) { - (void)_; +const char* age() { static char buf[4]; static time_t last; time_t now = time(NULL); if (now - last < 900) return buf; - time_t created; struct statx stx; - created = (!syscall(SYS_statx, AT_FDCWD, "/", 0, STATX_BTIME, &stx) && (stx.stx_mask & STATX_BTIME)) ? stx.stx_btime.tv_sec : now; + time_t created = (!syscall(SYS_statx, AT_FDCWD, "/", 0, STATX_BTIME, &stx) && (stx.stx_mask & STATX_BTIME)) ? stx.stx_btime.tv_sec : now; snprintf(buf, sizeof(buf), "%d", (int)((now - created) / 86400)); last = now; return buf; } -int main(int argc, char *argv[]) { - static Display *dpy; - struct sigaction act; +volatile sig_atomic_t done = 0; +void terminate() { done = 1; } + +int main() { + Display *dpy; + struct sigaction act = { .sa_handler = terminate, .sa_flags = SA_RESTART }; + struct arg { + const char *(*func)(void); + const char *fmt; + }; + const struct arg args[] = { + { bgt, "W: %s " }, // 9 bytes + { vol, "V: %s " }, // 9 bytes + { age, "A: %sd" }, // 8 bytes + }; size_t i, len; int ret; - char status[36]; - static const struct arg args[] = { - { bgt, "W: %s · ", NULL }, - { vol, "V: %s · ", NULL }, - { age, "A: %sd", NULL }, - }; + char status[32]; + const char *res; - memset(&act, 0, sizeof(act)); - act.sa_handler = terminate; sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); - act.sa_flags |= SA_RESTART; if (!(dpy = XOpenDisplay(NULL))) die("XOpenDisplay: Failed to open display"); - do { + while(!done) { status[0] = '\0'; - for (i = len = 0; i < (sizeof(args) / sizeof(args[0])); i++) { - const char *res = args[i].func(args[i].args); + for (i = len = 0; i < 3; i++) { + res = args[i].func(); if (!res) res = "n/a"; if ((ret = snprintf(status + len, sizeof(status) - len, args[i].fmt, res)) < 0) break; len += ret; @@ -99,12 +83,8 @@ int main(int argc, char *argv[]) { if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) die("XStoreName: Allocation failed"); XFlush(dpy); - - if (!done) { - struct timespec ts = { .tv_sec = 0, .tv_nsec = 100 * 1000000 }; // 100ms - nanosleep(&ts, NULL); - } - } while (!done); + nanosleep(&(struct timespec){ .tv_sec = 0, .tv_nsec = 500 * 1000000 }, NULL); // 500ms + } XStoreName(dpy, DefaultRootWindow(dpy), NULL); if (XCloseDisplay(dpy) < 0) die("XCloseDisplay: Failed to close display"); return 0;