This commit is contained in:
DarthMooMancer
2025-08-09 23:46:28 -04:00
parent 4ab3f10ad2
commit 4d0d0b5c0a
2 changed files with 35 additions and 61 deletions
+3 -9
View File
@@ -1,21 +1,15 @@
.POSIX: .POSIX:
PREFIX = /usr/local 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
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
slstatus: slstatus.c 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: clean:
rm -f slstatus rm -f slstatus
install: all install: slstatus
install -Dm755 slstatus "$(DESTDIR)$(PREFIX)/bin/slstatus" install -Dm755 slstatus "$(DESTDIR)$(PREFIX)/bin/slstatus"
uninstall: uninstall:
+31 -51
View File
@@ -1,51 +1,32 @@
#define _GNU_SOURCE #define _GNU_SOURCE
#include <fcntl.h> // for AT_FDCWD #define AT_FDCWD -100
#include <sys/syscall.h> #include <sys/syscall.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <time.h> #include <time.h>
#include <X11/Xlib.h>
#include <stdarg.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <X11/Xlib.h>
static volatile sig_atomic_t done; void die(const char *msg) {
struct arg { fprintf(stderr, "%s\n", msg);
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);
exit(1); exit(1);
} }
static void terminate() { const char* bgt() {
done = 1;
}
const char* bgt(const char *_) {
(void)_;
static char buf[5]; static char buf[5];
time_t now = time(NULL), last = now; time_t now = time(NULL), last = now;
FILE *f = fopen("/tmp/last_wallpaper_time", "r"); FILE *f = fopen("/tmp/last_wallpaper_time", "r");
if (!f || fscanf(f, "%ld", &last) != 1) last = now; if(fscanf(f, "%ld", &last) != 1) last = now;
if (f) fclose(f); fclose(f);
int rem = 600 - (int)(now - last); int rem = 600 - (int)(now - last);
if (rem < 0) rem = 0; if (rem < 0) rem = 0;
if (snprintf(buf, sizeof(buf), "%d:%02d", (rem / 60), (rem % 60)) < 0) return NULL; if (snprintf(buf, sizeof(buf), "%d:%02d", (rem / 60), (rem % 60)) < 0) return NULL;
return buf; return buf;
} }
const char* vol(const char *_) { const char* vol() {
(void)_;
static char buf[5]; static char buf[5];
float v; float v;
FILE *fp = popen("wpctl get-volume @DEFAULT_AUDIO_SINK@", "r"); FILE *fp = popen("wpctl get-volume @DEFAULT_AUDIO_SINK@", "r");
@@ -55,43 +36,46 @@ const char* vol(const char *_) {
return buf; return buf;
} }
const char* age(const char *_) { const char* age() {
(void)_;
static char buf[4]; static char buf[4];
static time_t last; static time_t last;
time_t now = time(NULL); time_t now = time(NULL);
if (now - last < 900) return buf; if (now - last < 900) return buf;
time_t created;
struct statx stx; 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)); snprintf(buf, sizeof(buf), "%d", (int)((now - created) / 86400));
last = now; last = now;
return buf; return buf;
} }
int main(int argc, char *argv[]) { volatile sig_atomic_t done = 0;
static Display *dpy; void terminate() { done = 1; }
struct sigaction act;
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; size_t i, len;
int ret; int ret;
char status[36]; char status[32];
static const struct arg args[] = { const char *res;
{ bgt, "W: %s · ", NULL },
{ vol, "V: %s · ", NULL },
{ age, "A: %sd", NULL },
};
memset(&act, 0, sizeof(act));
act.sa_handler = terminate;
sigaction(SIGINT, &act, NULL); sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL); sigaction(SIGTERM, &act, NULL);
act.sa_flags |= SA_RESTART;
if (!(dpy = XOpenDisplay(NULL))) die("XOpenDisplay: Failed to open display"); if (!(dpy = XOpenDisplay(NULL))) die("XOpenDisplay: Failed to open display");
do { while(!done) {
status[0] = '\0'; status[0] = '\0';
for (i = len = 0; i < (sizeof(args) / sizeof(args[0])); i++) { for (i = len = 0; i < 3; i++) {
const char *res = args[i].func(args[i].args); res = args[i].func();
if (!res) res = "n/a"; if (!res) res = "n/a";
if ((ret = snprintf(status + len, sizeof(status) - len, args[i].fmt, res)) < 0) break; if ((ret = snprintf(status + len, sizeof(status) - len, args[i].fmt, res)) < 0) break;
len += ret; len += ret;
@@ -99,12 +83,8 @@ int main(int argc, char *argv[]) {
if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) die("XStoreName: Allocation failed"); if (XStoreName(dpy, DefaultRootWindow(dpy), status) < 0) die("XStoreName: Allocation failed");
XFlush(dpy); XFlush(dpy);
nanosleep(&(struct timespec){ .tv_sec = 0, .tv_nsec = 500 * 1000000 }, NULL); // 500ms
if (!done) {
struct timespec ts = { .tv_sec = 0, .tv_nsec = 100 * 1000000 }; // 100ms
nanosleep(&ts, NULL);
} }
} while (!done);
XStoreName(dpy, DefaultRootWindow(dpy), NULL); XStoreName(dpy, DefaultRootWindow(dpy), NULL);
if (XCloseDisplay(dpy) < 0) die("XCloseDisplay: Failed to close display"); if (XCloseDisplay(dpy) < 0) die("XCloseDisplay: Failed to close display");
return 0; return 0;