-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkissbar.c
223 lines (186 loc) · 6.1 KB
/
kissbar.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <unistd.h>
#include <X11/Xlib.h>
#ifdef __linux__
#include <bsd/string.h>
#endif
// Maximum size of the status message from one element (script)
enum { SSIZE = 64 };
// One Element (script) of the Statusbar
typedef struct {
char *cmd;
unsigned long interval;
char status[SSIZE];
} Element;
// The complete Statusbar
typedef struct {
char *text;
size_t text_len;
uint32_t update_cycle;
} Statusbar;
// X11 variables
Display *dpy;
Window root;
// Options
// Add newline to the output
bool newline = 0;
// Determines the output destination of the statusbar
bool tostdout = 0;
bool towin = 0;
// Defines custom elements
#include "kissbar.h"
// Total number of elements (scripts)
const size_t elements_num = sizeof(elements)/sizeof(Element);
// Function definitions
void element_update(Element*);
bool statusbar_update(Statusbar*);
void statusbar_build(Statusbar*);
void statusbar_output(Statusbar*);
void setup_X(void);
int ignore_X_error(Display*, XErrorEvent*);
void usage(char*);
void
element_update(Element *element)
{
// Execute statusbar script/command
FILE *status_cmd = popen(element->cmd, "r");
if (status_cmd == NULL) {
fprintf(stderr, "Kissbar: Could not execute command/script: %s\n", element->cmd);
exit(3);
}
// Get return value of the script/command
if (fgets(element->status, SSIZE, status_cmd) == NULL)
element->status[0] = '\0';
// Remove newline if present
element->status[strcspn(element->status, "\n")] = '\0';
// Close the fd
pclose(status_cmd);
}
bool
statusbar_update(Statusbar *statusbar)
{
// Returned and set to true when an element status updated
bool changed = false;
// Store old status of every element to determine an update
char prev_status[SSIZE];
for (int i = 0; i < elements_num; ++i) {
// Only pull new status ever n cycles
if (elements[i].interval &&
statusbar->update_cycle % elements[i].interval)
continue;
// Copy the current status to a buffer
strlcpy(prev_status, elements[i].status, SSIZE);
// Update status of current element
element_update(elements + i);
// If the previous status differs from the current
// we will set the return value changed to true
if (strncmp(elements[i].status, prev_status, SSIZE))
changed = true;
}
++statusbar->update_cycle;
return changed;
}
void
statusbar_build(Statusbar *statusbar)
{
// "clear" the statusbar text
statusbar->text[0] = '\0';
// Build full statusbar text from all elements
for (int i = 0; i < elements_num; ++i) {
// Ignore empty status
if (strnlen(elements[i].status, SSIZE) == 0)
continue;
// Copy the first elements status to the beginning
if (statusbar->text[0] == '\0') {
strlcpy(statusbar->text, elements[i].status, SSIZE);
continue;
}
// Copy delimiter and next elements status the the statusbar
strlcat(statusbar->text, delim, statusbar->text_len);
strlcat(statusbar->text, elements[i].status, statusbar->text_len);
}
// Add newline
if (newline) strcat(statusbar->text, "\n");
// Be sure to be nul terminated
statusbar->text[statusbar->text_len - 1] = '\0';
}
void
statusbar_output(Statusbar *statusbar)
{
if (tostdout) {
printf("%s", statusbar->text);
fflush(stdout);
}
if (towin) {
XStoreName(dpy, root, statusbar->text);
XFlush(dpy);
}
}
int
ignore_X_error(Display *dpy, XErrorEvent *ee)
{
// Output X errors to stderr but ignore them
char error[256] = {0};
XGetErrorText(dpy, ee->error_code, error, 128);
fprintf(stderr,
"Kissbar: Encountered unexpected X error: %s\n",
error);
return 0;
}
void
setup_X(void)
{
// Connect to X to set the root windows name
if (!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "Kissbar: Could not open X11 display\n");
exit(2);
}
root = DefaultRootWindow(dpy);
XSetErrorHandler(ignore_X_error);
XSync(dpy, 0);
}
void
usage(char *name)
{
fprintf(stderr, "Usage: %s [-own]\n", name);
exit(1);
}
int
main(int argc, char **argv)
{
// Parse arguments
int opt;
while ((opt = getopt(argc, argv, "own")) != -1) {
switch(opt) {
case 'o': tostdout = true; break;
case 'w': towin = true; break;
case 'n': newline = true; break;
default: usage(argv[0]);
}
}
// Print to stdout by default except only -w (xsetroot) option is given
if (!towin) tostdout = 1;
else setup_X();
// Create the statusbar
Statusbar *statusbar = (Statusbar*) malloc(sizeof(Statusbar));
// Allocate enough space for the whole statusbar text
statusbar->text_len = (SSIZE * elements_num) +
(strlen(delim) * (elements_num - 1)) + 1;
statusbar->text = (char*) malloc(statusbar->text_len);
statusbar->update_cycle = 0;
while (1) {
// Build and output statusbar if an elements status changed
if (statusbar_update(statusbar)) {
statusbar_build(statusbar);
statusbar_output(statusbar);
}
// Checking for updates every second
sleep(1);
}
if (towin) XCloseDisplay(dpy);
return 0;
}