| 1 | /* |
| 2 | * SGK version of dwmstatus by 20h. |
| 3 | */ |
| 4 | |
| 5 | #define _BSD_SOURCE |
| 6 | #include <unistd.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <stdarg.h> |
| 10 | #include <time.h> |
| 11 | #include <X11/Xlib.h> |
| 12 | |
| 13 | static Display *dpy; |
| 14 | |
| 15 | char * |
| 16 | smprintf(char *fmt, ...) |
| 17 | { |
| 18 | va_list fmtargs; |
| 19 | char *ret; |
| 20 | int len; |
| 21 | |
| 22 | va_start(fmtargs, fmt); |
| 23 | len = vsnprintf(NULL, 0, fmt, fmtargs); |
| 24 | va_end(fmtargs); |
| 25 | |
| 26 | ret = malloc(++len); |
| 27 | if (ret == NULL) { |
| 28 | perror("malloc"); |
| 29 | exit(1); |
| 30 | } |
| 31 | |
| 32 | va_start(fmtargs, fmt); |
| 33 | vsnprintf(ret, len, fmt, fmtargs); |
| 34 | va_end(fmtargs); |
| 35 | |
| 36 | return ret; |
| 37 | } |
| 38 | |
| 39 | char * |
| 40 | mktimes(char *fmt, char *tzname) |
| 41 | { |
| 42 | char buf[129]; |
| 43 | time_t tim; |
| 44 | struct tm *timtm; |
| 45 | |
| 46 | setenv("TZ", tzname, 1); |
| 47 | tim = time(NULL); |
| 48 | timtm = localtime(&tim); |
| 49 | if (timtm == NULL) |
| 50 | return smprintf(""); |
| 51 | |
| 52 | if (!strftime(buf, sizeof(buf)-1, fmt, timtm)) { |
| 53 | fprintf(stderr, "strftime == 0\n"); |
| 54 | return smprintf(""); |
| 55 | } |
| 56 | |
| 57 | return smprintf("%s", buf); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | setstatus(char *str) |
| 62 | { |
| 63 | XStoreName(dpy, DefaultRootWindow(dpy), str); |
| 64 | XSync(dpy, False); |
| 65 | } |
| 66 | |
| 67 | char * |
| 68 | loadavg(void) |
| 69 | { |
| 70 | double avgs[3]; |
| 71 | |
| 72 | if (getloadavg(avgs, 3) < 0) |
| 73 | return smprintf(""); |
| 74 | |
| 75 | return smprintf("%.2f", avgs[0]); |
| 76 | } |
| 77 | |
| 78 | int |
| 79 | main(void) |
| 80 | { |
| 81 | char *load, *datetime, *status; |
| 82 | |
| 83 | if (!(dpy = XOpenDisplay(NULL))) { |
| 84 | fprintf(stderr, "dwmstatus: cannot open display.\n"); |
| 85 | return 1; |
| 86 | } |
| 87 | |
| 88 | for (;;sleep(5)) { |
| 89 | load = loadavg(); |
| 90 | datetime = mktimes("%a %d %b %Y ][ %H:%M", "America/Los_Angeles"); |
| 91 | |
| 92 | status = smprintf("[ %s ][ %s ]", load, datetime); |
| 93 | setstatus(status); |
| 94 | |
| 95 | free(load); |
| 96 | free(datetime); |
| 97 | free(status); |
| 98 | } |
| 99 | |
| 100 | XCloseDisplay(dpy); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |