Commit | Line | Data |
---|---|---|
a1751622 | 1 | /* |
c9a04fdd | 2 | * SGK version of dwmstatus by 20h. |
a1751622 CL |
3 | */ |
4 | ||
5 | #define _BSD_SOURCE | |
6 | #include <unistd.h> | |
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
9 | #include <stdarg.h> | |
a1751622 | 10 | #include <time.h> |
a1751622 CL |
11 | #include <X11/Xlib.h> |
12 | ||
a1751622 CL |
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 | ||
a1751622 CL |
39 | char * |
40 | mktimes(char *fmt, char *tzname) | |
41 | { | |
42 | char buf[129]; | |
43 | time_t tim; | |
44 | struct tm *timtm; | |
45 | ||
c9a04fdd | 46 | setenv("TZ", tzname, 1); |
a1751622 CL |
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 | ||
c9a04fdd | 75 | return smprintf("%.2f", avgs[0]); |
19953a7d CL |
76 | } |
77 | ||
a1751622 CL |
78 | int |
79 | main(void) | |
80 | { | |
c9a04fdd | 81 | char *load, *datetime, *status; |
a1751622 CL |
82 | |
83 | if (!(dpy = XOpenDisplay(NULL))) { | |
84 | fprintf(stderr, "dwmstatus: cannot open display.\n"); | |
85 | return 1; | |
86 | } | |
87 | ||
c9a04fdd AT |
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); | |
a1751622 CL |
93 | setstatus(status); |
94 | ||
c9a04fdd AT |
95 | free(load); |
96 | free(datetime); | |
a1751622 CL |
97 | free(status); |
98 | } | |
99 | ||
100 | XCloseDisplay(dpy); | |
101 | ||
102 | return 0; | |
103 | } | |
104 |