/
main.c
339 lines (289 loc) · 6.31 KB
/
main.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
* Copyright (C) 2015-2016 Omar Sandoval
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <sys/signalfd.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <X11/Xlib.h>
#include "verbar_internal.h"
static const char *progname = "verbar";
extern char **environ;
static Display *dpy;
static Window root;
static const char *config[] = {
"dropbox",
"net",
"volume",
"cpu",
"mem",
"power",
"clock",
};
static bool quit, update, wordy;
static struct str status_str;
void request_update(void)
{
update = true;
}
static int update_statusbar(void)
{
status_str.len = 0;
if (str_append(&status_str, " "))
return -1;
append_sections(&status_str, wordy);
if (str_null_terminate(&status_str))
return -1;
XStoreName(dpy, root, status_str.buf);
XFlush(dpy);
return 0;
}
static int signal_fd_callback(int fd, void *data, uint32_t events)
{
struct signalfd_siginfo ssi;
ssize_t ssret;
ssret = read(fd, &ssi, sizeof(ssi));
if (ssret == -1) {
perror("read(signalfd)");
return -1;
}
assert(ssret == sizeof(ssi));
if (ssi.ssi_signo == SIGUSR1) {
wordy = !wordy;
update = true;
} else {
fprintf(stderr, "got signal %s; exiting\n",
strsignal(ssi.ssi_signo));
quit = true;
}
return 0;
}
static struct epoll_callback signal_cb = {
.callback = signal_fd_callback,
.fd = -1,
};
static int signal_fd_init(int epoll_fd)
{
struct epoll_event ev;
sigset_t mask;
int ret;
int fd;
sigemptyset(&mask);
sigaddset(&mask, SIGHUP);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGUSR1);
ret = sigprocmask(SIG_BLOCK, &mask, NULL);
if (ret == -1) {
perror("sigprocmask");
return -1;
}
fd = signalfd(-1, &mask, SFD_CLOEXEC);
if (fd == -1) {
perror("signalfd");
return -1;
}
signal_cb.fd = fd;
ev.events = EPOLLIN;
ev.data.ptr = &signal_cb;
return epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
}
static int timer_fd_callback(int fd, void *data, uint32_t events)
{
uint64_t times;
ssize_t ssret;
int ret;
ssret = read(fd, ×, sizeof(times));
if (ssret == -1) {
perror("read(timerfd)");
return -1;
}
assert(ssret == sizeof(times));
if (times > 1) {
fprintf(stderr, "warning: missed %" PRIu64 " ticks\n",
times - 1);
}
ret = update_timer_sections();
if (ret)
return -1;
update = true;
return 0;
}
static struct epoll_callback timer_cb = {
.callback = timer_fd_callback,
.fd = -1,
};
static int timer_fd_init(int epoll_fd)
{
struct epoll_event ev;
int fd;
fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
if (fd == -1) {
perror("timerfd_create");
return -1;
}
timer_cb.fd = fd;
ev.events = EPOLLIN;
ev.data.ptr = &timer_cb;
return epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
}
static void usage(bool error)
{
fprintf(error ? stderr : stdout,
"usage: %s [--icons PATH] [--wordy]\n"
"\n"
"Gather system information and set the root window name\n"
"\n"
"Options:\n"
" -i, --icons PATH directory containing icon files\n"
" -w, --wordy enable wordy output on startup\n"
"\n"
"Miscellaneous:\n"
" -h, --help display this help message and exit\n",
progname);
exit(error ? EXIT_FAILURE : EXIT_SUCCESS);
}
int main(int argc, char **argv)
{
struct option long_options[] = {
{"icons", required_argument, NULL, 'i'},
{"wordy", no_argument, NULL, 'w'},
{"help", no_argument, NULL, 'h'},
};
int epoll_fd = -1;
struct itimerspec it;
int ret;
int status = EXIT_SUCCESS;
if (argc > 0)
progname = argv[0];
for (;;) {
int c;
c = getopt_long(argc, argv, "i:wh", long_options, NULL);
if (c == -1)
break;
switch (c) {
case 'i':
icon_path = optarg;
break;
case 'w':
wordy = true;
break;
case 'h':
usage(false);
default:
usage(true);
}
}
if (optind != argc)
usage(true);
dpy = XOpenDisplay(NULL);
if (!dpy) {
fprintf(stderr, "unable to open display '%s'\n",
XDisplayName(NULL));
status = EXIT_FAILURE;
goto out;
}
root = DefaultRootWindow(dpy);
epoll_fd = epoll_create1(EPOLL_CLOEXEC);
if (epoll_fd == -1) {
perror("epoll_create1");
status = EXIT_FAILURE;
goto out;
}
ret = signal_fd_init(epoll_fd);
if (ret == -1) {
status = EXIT_FAILURE;
goto out;
}
ret = timer_fd_init(epoll_fd);
if (ret == -1) {
status = EXIT_FAILURE;
goto out;
}
if (init_sections(epoll_fd, config, sizeof(config) / sizeof(*config))) {
status = EXIT_FAILURE;
goto out;
}
if (update_timer_sections()) {
status = EXIT_FAILURE;
goto out;
}
it.it_interval.tv_sec = 1;
it.it_interval.tv_nsec = 0;
it.it_value.tv_sec = 1;
it.it_value.tv_nsec = 0;
ret = timerfd_settime(timer_cb.fd, 0, &it, NULL);
if (ret == -1) {
perror("timerfd_settime");
status = EXIT_FAILURE;
goto out;
}
while (!quit) {
struct epoll_event events[10];
int i;
update = false;
ret = epoll_wait(epoll_fd, events,
sizeof(events) / sizeof(events[0]),
-1);
if (ret == -1) {
if (errno == EINTR)
continue;
perror("epoll_wait");
status = EXIT_FAILURE;
goto out;
}
for (i = 0; i < ret; i++) {
struct epoll_callback *cb = events[i].data.ptr;
ret = cb->callback(cb->fd, cb->data, events[i].events);
if (ret) {
status = EXIT_FAILURE;
goto out;
}
}
if (update) {
ret = update_statusbar();
if (ret) {
status = EXIT_FAILURE;
goto out;
}
}
}
status = EXIT_SUCCESS;
out:
if (epoll_fd != -1)
close(epoll_fd);
free_sections();
if (timer_cb.fd != -1)
close(timer_cb.fd);
if (signal_cb.fd != -1)
close(signal_cb.fd);
str_free(&status_str);
if (dpy) {
XStoreName(dpy, root, "");
XFlush(dpy);
XCloseDisplay(dpy);
}
return status;
}