/
dwm-status.c
201 lines (159 loc) · 3.82 KB
/
dwm-status.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* See LICENSE for license details. */
#include <assert.h>
#include <err.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <limits.h>
#include <unistd.h>
#include <sys/types.h>
#include <X11/Xlib.h>
#include <tinyalsa/asoundlib.h>
static size_t alsavol(char*, size_t);
static size_t batcap(char*, size_t);
static size_t loadavg(char*, size_t);
static size_t curtime(char*, size_t);
static size_t separator(char*, size_t);
#include "config.h"
enum {
STATUSSZ = 128,
};
static char ststr[STATUSSZ];
static int
xsnprintf(char *restrict s, size_t n, const char *restrict fmt, ...)
{
int ret;
va_list ap;
va_start(ap, fmt);
ret = vsnprintf(s, n, fmt, ap);
va_end(ap);
if (ret < 0) {
err(EXIT_FAILURE, "snprintf failed");
} else if ((size_t)ret >= n) {
warnx("snprintf: insufficient buffer size");
ret = n;
}
return ret;
}
static double
readnum(char *bfp, char *fn)
{
int ret;
FILE *file;
size_t rclen;
char buf[16], fp[PATH_MAX + 1], *rc;
rc = NULL;
if ((ret = snprintf(fp, sizeof(fp), "%s/%s", bfp, fn)) < 0)
errx(EXIT_FAILURE, "snprintf failed");
else if ((size_t)ret >= sizeof(fp))
errx(EXIT_FAILURE, "buffer 'fp' is too short");
if (!(file = fopen(fp, "r")))
err(EXIT_FAILURE, "couldn't open '%s'", fp);
if (!(rc = fgets(buf, 16, file))) {
fclose(file);
errx(EXIT_FAILURE, "'%s' seems to be empty", fp);
}
rclen = strlen(buf);
if (rc[rclen - 1] == '\n')
rc[rclen - 1] = '\0';
fclose(file);
return atof(rc);
}
static size_t
actlstr(char *buf, size_t n, char *ch, struct mixer *mx) {
size_t ret;
char *status;
struct mixer_ctl *ctl;
if (!(ctl = mixer_get_ctl_by_name(mx, ch))) {
mixer_close(mx);
errx(EXIT_FAILURE, "couldn't find mixer ctl '%s'\n", ch);
}
switch (mixer_ctl_get_type(ctl)) {
case MIXER_CTL_TYPE_INT:
return xsnprintf(buf, n, "%d%%",
mixer_ctl_get_percent(ctl, 0));
case MIXER_CTL_TYPE_BOOL:
status = mixer_ctl_get_value(ctl, 0) ? "On" : "Off";
ret = stpncpy(buf, status, n) - buf;
break;
default:
mixer_close(mx);
errx(EXIT_FAILURE, "unsupported ctl type '%s'\n",
mixer_ctl_get_type_string(ctl));
};
return ret;
}
static size_t
batcap(char *dest, size_t n)
{
double res, curc, maxc;
curc = readnum((char*)sysbat, (char*)syscur);
maxc = readnum((char*)sysbat, (char*)sysfull);
res = 100.0 * (curc / maxc);
return xsnprintf(dest, n, "%.2f%%", res);
}
static size_t
alsavol(char *dest, size_t n)
{
size_t ret;
struct mixer *mx;
if (!(mx = mixer_open(sndcrd)))
errx(EXIT_FAILURE, "couldn't open mixer for card %d\n", sndcrd);
ret = actlstr(dest, n, (char*)swtchname, mx);
if (strcmp(dest, "Off"))
ret = actlstr(dest, n, (char*)volumname, mx);
mixer_close(mx);
return ret;
}
static size_t
loadavg(char* dest, size_t n)
{
double avgs[3];
if (!getloadavg(avgs, 3))
err(EXIT_FAILURE, "getloadavg failed");
return xsnprintf(dest, n, "%.2f %.2f %.2f",
avgs[0], avgs[1], avgs[2]);
}
static size_t
curtime(char *dest, size_t n)
{
time_t tim;
struct tm *timtm;
if ((tim = time(NULL)) == (time_t)-1)
err(EXIT_FAILURE, "time failed");
if (!(timtm = localtime(&tim)))
err(EXIT_FAILURE, "localtime failed");
return strftime(dest, n, timefmt, timtm);
}
static size_t
separator(char *dest, size_t n)
{
return stpncpy(dest, statsep, n) - dest;
}
int
main(void)
{
Display *dpy;
Window root;
size_t i, x, fns, max;
if (!(dpy = XOpenDisplay(NULL)))
errx(EXIT_FAILURE, "couldn't open display '%s'", XDisplayName(NULL));
root = DefaultRootWindow(dpy);
max = STATUSSZ - 1;
fns = sizeof(sfuncs) / sizeof(sfuncs[0]);
for (;;) {
for (i = 0, x = 0; i < fns && x < max; i++) {
assert(max >= x);
x += (*sfuncs[i])(&(ststr[x]), max - x);
}
assert(x < STATUSSZ);
ststr[x] = '\0';
XStoreName(dpy, root, ststr);
XSync(dpy, False);
sleep(delay);
}
XCloseDisplay(dpy);
return EXIT_SUCCESS;
}