This commit is contained in:
DarthMooMancer
2025-08-05 13:58:07 -04:00
parent 0f1d0c0b4c
commit 0331d4a1b0
+35 -42
View File
@@ -12,6 +12,11 @@
#include <sys/stat.h> #include <sys/stat.h>
static volatile sig_atomic_t done; static volatile sig_atomic_t done;
struct arg {
const char *(*func)(const char *);
const char *fmt;
const char *args;
};
void die(const char *fmt, ...) { void die(const char *fmt, ...) {
va_list ap; va_list ap;
@@ -22,57 +27,46 @@ void die(const char *fmt, ...) {
exit(1); exit(1);
} }
struct arg {
const char *(*func)(const char *);
const char *fmt;
const char *args;
};
static void terminate() { static void terminate() {
done = 1; done = 1;
} }
const char* bg_timer(const char *unused) { const char* bg_timer(const char *_) {
(void)unused; (void)_;
static char buf[6]; 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) { 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), "%02d:%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* run_command(const char *cmd) { const char* vol(const char *_) {
static char buf[8]; (void)_;
FILE *fp = popen(cmd, "r"); static char buf[5];
if (!fp) return NULL; float v;
char *p = fgets(buf, sizeof(buf) - 1, fp); FILE *fp = popen("wpctl get-volume @DEFAULT_AUDIO_SINK@", "r");
if (!fp || fscanf(fp, "Volume: %f", &v) != 1) return NULL;
pclose(fp); pclose(fp);
if (!p) return NULL; snprintf(buf, sizeof(buf), "%d%%", (int)(v * 100));
if ((p = strrchr(buf, '\n'))) *p = '\0'; return buf;
return *buf ? buf : NULL;
} }
const char* age(const char *_) { const char* age(const char *_) {
static char buf[4]; (void)_;
static time_t last; static char buf[4];
time_t now = time(NULL); static time_t last;
if (now - last < 3600) return buf; time_t now = time(NULL);
if (now - last < 900) return buf;
time_t created = 0; time_t created;
struct statx stx; struct statx stx;
if (!syscall(SYS_statx, AT_FDCWD, "/", 0, STATX_BTIME, &stx) && (stx.stx_mask & STATX_BTIME)) created = stx.stx_btime.tv_sec; created = (!syscall(SYS_statx, AT_FDCWD, "/", 0, STATX_BTIME, &stx) && (stx.stx_mask & STATX_BTIME)) ? stx.stx_btime.tv_sec : now;
else created = now; snprintf(buf, sizeof(buf), "%d", (int)((now - created) / 86400));
last = now;
snprintf(buf, sizeof(buf), "%d", (int)((now - created) / 86400)); return buf;
last = now;
return buf;
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
@@ -80,12 +74,11 @@ int main(int argc, char *argv[]) {
struct sigaction act; struct sigaction act;
size_t i, len; size_t i, len;
int ret; int ret;
char status[64]; char status[36];
const char *res;
static const struct arg args[] = { static const struct arg args[] = {
{ bg_timer, " %s till swap · ", NULL }, { bg_timer, " W: %s · ", NULL },
{ run_command, "VOL at %s · ", "wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk '{printf \"%d%%\", $2 * 100}'" }, { vol, "V: %s · ", NULL },
{ age, "%s / 731 installed ", NULL }, { age, "A: %s / 731 ", NULL },
}; };
memset(&act, 0, sizeof(act)); memset(&act, 0, sizeof(act));
@@ -98,7 +91,7 @@ int main(int argc, char *argv[]) {
do { do {
status[0] = '\0'; status[0] = '\0';
for (i = len = 0; i < (sizeof(args) / sizeof(args[0])); i++) { for (i = len = 0; i < (sizeof(args) / sizeof(args[0])); i++) {
res = args[i].func(args[i].args); const char *res = args[i].func(args[i].args);
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;