-
Notifications
You must be signed in to change notification settings - Fork 1
/
statusbar.c
77 lines (62 loc) · 1.69 KB
/
statusbar.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
#include <X11/X.h>
#include <X11/Xlib.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* macros */
#define LEN(X) (sizeof X / sizeof X[0])
/* In theory max 8 modules allowed */
#define MAX_STATUS_LEN 256
#define MAX_MODULE_TEXT_LEN 32
/* config file */
#include "config.h"
static volatile sig_atomic_t keep_running = 1;
static Display *dpy;
static void
sig_handler(int sig)
{
puts("stopping statusbar\n");
(void) sig;
keep_running = 0;
XCloseDisplay(dpy);
}
/* function implementations */
static void
setroot(Display *dpy, Window root)
{
char status[MAX_STATUS_LEN] = {0};
char text[MAX_MODULE_TEXT_LEN];
unsigned int i;
for (i = 0; i < LEN(modules); i++) {
Module m = modules[i];
m.func(&m.args, text, LEN(text) -1);
if (i > 0) {
strncat(status, delimiter,
MAX_STATUS_LEN - strlen(status) - 1);
}
strncat(status, text, MAX_STATUS_LEN - strlen(status) - 1);
/* clear text buffer */
text[0] = '\0';
}
XStoreName(dpy, root, status);
XFlush(dpy);
}
int
main(void)
{
signal(SIGINT | SIGKILL, sig_handler);
dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
fprintf(stderr, "could not open display\n");
exit(-1);
}
int screen = DefaultScreen(dpy);
Window root = RootWindow(dpy, screen);
while (keep_running) {
setroot(dpy, root);
sleep(refresh_interval_s);
}
return -1;
}