diff --git a/.config/suckless/slstatus/slstatus.c b/.config/suckless/slstatus/slstatus.c index 046fea3..7008f0d 100644 --- a/.config/suckless/slstatus/slstatus.c +++ b/.config/suckless/slstatus/slstatus.c @@ -12,6 +12,11 @@ #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; @@ -22,57 +27,46 @@ void die(const char *fmt, ...) { exit(1); } -struct arg { - const char *(*func)(const char *); - const char *fmt; - const char *args; -}; - static void terminate() { done = 1; } -const char* bg_timer(const char *unused) { - (void)unused; - static char buf[6]; +const char* bg_timer(const char *_) { + (void)_; + static char buf[5]; time_t now = time(NULL), last = now; FILE *f = fopen("/tmp/last_wallpaper_time", "r"); - if (f) { - if (fscanf(f, "%ld", &last) != 1) last = now; - fclose(f); - } - + if (!f || fscanf(f, "%ld", &last) != 1) last = now; + if (f) fclose(f); int rem = 600 - (int)(now - last); 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; } -const char* run_command(const char *cmd) { - static char buf[8]; - FILE *fp = popen(cmd, "r"); - if (!fp) return NULL; - char *p = fgets(buf, sizeof(buf) - 1, fp); +const char* vol(const char *_) { + (void)_; + static char buf[5]; + float v; + FILE *fp = popen("wpctl get-volume @DEFAULT_AUDIO_SINK@", "r"); + if (!fp || fscanf(fp, "Volume: %f", &v) != 1) return NULL; pclose(fp); - if (!p) return NULL; - if ((p = strrchr(buf, '\n'))) *p = '\0'; - return *buf ? buf : NULL; + snprintf(buf, sizeof(buf), "%d%%", (int)(v * 100)); + return buf; } const char* age(const char *_) { - static char buf[4]; - static time_t last; - time_t now = time(NULL); - if (now - last < 3600) return buf; - - time_t created = 0; - struct statx stx; - if (!syscall(SYS_statx, AT_FDCWD, "/", 0, STATX_BTIME, &stx) && (stx.stx_mask & STATX_BTIME)) created = stx.stx_btime.tv_sec; - else created = now; - - snprintf(buf, sizeof(buf), "%d", (int)((now - created) / 86400)); - last = now; - return buf; + (void)_; + 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; + snprintf(buf, sizeof(buf), "%d", (int)((now - created) / 86400)); + last = now; + return buf; } int main(int argc, char *argv[]) { @@ -80,12 +74,11 @@ int main(int argc, char *argv[]) { struct sigaction act; size_t i, len; int ret; - char status[64]; - const char *res; + char status[36]; static const struct arg args[] = { - { bg_timer, " %s till swap · ", NULL }, - { run_command, "VOL at %s · ", "wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk '{printf \"%d%%\", $2 * 100}'" }, - { age, "%s / 731 installed ", NULL }, + { bg_timer, " W: %s · ", NULL }, + { vol, "V: %s · ", NULL }, + { age, "A: %s / 731 ", NULL }, }; memset(&act, 0, sizeof(act)); @@ -98,7 +91,7 @@ int main(int argc, char *argv[]) { do { status[0] = '\0'; 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 ((ret = snprintf(status + len, sizeof(status) - len, args[i].fmt, res)) < 0) break; len += ret;