This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
forked from LemonBoy/bar
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bar.c
468 lines (392 loc) · 13.8 KB
/
bar.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <poll.h>
#include <getopt.h>
#include <unistd.h>
#include <xcb/xcb.h>
#include "config.h"
// Here be dragons
#define MAX(a,b) ((a > b) ? a : b)
typedef struct fontset_item_t {
xcb_font_t xcb_ft;
xcb_charinfo_t *table;
int descent;
int avg_height;
unsigned short char_max;
unsigned short char_min;
} fontset_item_t;
enum {
FONT_MAIN,
FONT_FALLBACK,
FONT_MAX
};
enum {
ALIGN_L = 0,
ALIGN_C,
ALIGN_R
};
static xcb_connection_t *c;
static xcb_window_t win;
static xcb_drawable_t canvas;
static xcb_gcontext_t draw_gc;
static xcb_gcontext_t clear_gc;
static xcb_gcontext_t underl_gc;
static int bar_width;
static int bar_bottom = BAR_BOTTOM;
static int force_docking = 0;
static fontset_item_t fontset[FONT_MAX];
static fontset_item_t *sel_font = NULL;
static const unsigned palette[] = {COLOR0,COLOR1,COLOR2,COLOR3,COLOR4,COLOR5,COLOR6,COLOR7,COLOR8,COLOR9,BACKGROUND,FOREGROUND};
static inline void
xcb_set_bg (int i)
{
xcb_change_gc (c, draw_gc , XCB_GC_BACKGROUND, (const unsigned []){ palette[i] });
xcb_change_gc (c, clear_gc , XCB_GC_FOREGROUND, (const unsigned []){ palette[i] });
}
static inline void
xcb_set_fg (int i)
{
xcb_change_gc (c, draw_gc , XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] });
}
static inline void
xcb_set_ud (int i)
{
xcb_change_gc (c, underl_gc, XCB_GC_FOREGROUND, (const uint32_t []){ palette[i] });
}
static inline void
xcb_fill_rect (xcb_gcontext_t gc, int x, int y, int width, int height)
{
xcb_poly_fill_rectangle (c, canvas, gc, 1, (const xcb_rectangle_t []){ { x, y, width, height } });
}
static inline void
xcb_set_fontset (int i)
{
if (sel_font != &fontset[i]) {
sel_font = &fontset[i];
xcb_change_gc (c, draw_gc , XCB_GC_FONT, (const uint32_t []){ sel_font->xcb_ft });
}
}
int
draw_char (int x, int align, wchar_t ch)
{
int ch_width;
ch_width = (ch > sel_font->char_min && ch < sel_font->char_max) ?
sel_font->table[ch - sel_font->char_min].character_width :
0;
/* Some fonts (such as anorexia) have the space char with the width set to 0 */
if (ch_width == 0)
ch_width = BAR_FONT_FALLBACK_WIDTH;
switch (align) {
case ALIGN_C:
xcb_copy_area (c, canvas, canvas, draw_gc, bar_width / 2 - x / 2, 0,
bar_width / 2 - (x + ch_width) / 2, 0, x, BAR_HEIGHT);
x = bar_width / 2 - (x + ch_width) / 2 + x;
break;
case ALIGN_R:
xcb_copy_area (c, canvas, canvas, draw_gc, bar_width - x, 0,
bar_width - x - ch_width, 0, x, BAR_HEIGHT);
x = bar_width - ch_width;
break;
}
/* Draw the background first */
xcb_fill_rect (clear_gc, x, 0, ch_width, BAR_HEIGHT);
/* xcb accepts string in UCS-2 BE, so swap */
ch = (ch >> 8) | (ch << 8);
/* String baseline coordinates */
xcb_image_text_16 (c, 1, canvas, draw_gc, x, BAR_HEIGHT / 2 + sel_font->avg_height / 2 - sel_font->descent,
(xcb_char2b_t *)&ch);
/* Draw the underline */
if (BAR_UNDERLINE_HEIGHT)
xcb_fill_rect (underl_gc, x, BAR_UNDERLINE*(BAR_HEIGHT-BAR_UNDERLINE_HEIGHT), ch_width, BAR_UNDERLINE_HEIGHT);
return ch_width;
}
void
parse (char *text)
{
char *p = text;
int pos_x = 0;
int align = 0;
xcb_fill_rect (clear_gc, 0, 0, bar_width, BAR_HEIGHT);
for (;;) {
if (*p == '\0')
return;
if (*p == '\n')
return;
if (*p == '\\' && p++ && *p != '\\' && strchr ("fbulcr", *p)) {
switch (*p++) {
case 'f':
xcb_set_fg (isdigit(*p) ? (*p)-'0' : 11);
p++;
break;
case 'b':
xcb_set_bg (isdigit(*p) ? (*p)-'0' : 10);
p++;
break;
case 'u':
xcb_set_ud (isdigit(*p) ? (*p)-'0' : 10);
p++;
break;
case 'l':
align = ALIGN_L;
pos_x = 0;
break;
case 'c':
align = ALIGN_C;
pos_x = 0;
break;
case 'r':
align = ALIGN_R;
pos_x = 0;
break;
}
} else { /* utf-8 -> ucs-2 */
wchar_t t;
if (!(p[0] & 0x80)) {
t = p[0];
p += 1;
}
else if ((p[0] & 0xe0) == 0xc0 && (p[1] & 0xc0) == 0x80) {
t = (p[0] & 0x1f) << 6 | (p[1] & 0x3f);
p += 2;
}
else if ((p[0] & 0xf0) == 0xe0 && (p[1] & 0xc0) == 0x80 && (p[2] & 0xc0) == 0x80) {
t = (p[0] & 0xf) << 12 | (p[1] & 0x3f) << 6 | (p[2] & 0x3f);
p += 3;
}
else { /* ASCII chars > 127 go in the extended latin range */
t = 0xc200 + p[0];
p += 1;
}
/* The character is outside the main font charset, use the fallback */
if (t < fontset[FONT_MAIN].char_min || t > fontset[FONT_MAIN].char_max)
xcb_set_fontset (FONT_FALLBACK);
else
xcb_set_fontset (FONT_MAIN);
pos_x += draw_char (pos_x, align, t);
}
}
}
int
font_load (const char **font_list)
{
xcb_query_font_cookie_t queryreq;
xcb_query_font_reply_t *font_info;
xcb_void_cookie_t cookie;
xcb_font_t font;
int max_height;
max_height = -1;
for (int i = 0; i < FONT_MAX; i++) {
font = xcb_generate_id (c);
cookie = xcb_open_font_checked (c, font, strlen (font_list[i]), font_list[i]);
if (xcb_request_check (c, cookie)) {
fprintf (stderr, "Could not load font %s\n", font_list[i]);
return 1;
}
queryreq = xcb_query_font (c, font);
font_info = xcb_query_font_reply (c, queryreq, NULL);
fontset[i].xcb_ft = font;
fontset[i].table = xcb_query_font_char_infos (font_info);
fontset[i].descent = font_info->font_descent;
fontset[i].char_max= font_info->max_byte1 << 8 | font_info->max_char_or_byte2;
fontset[i].char_min= font_info->min_byte1 << 8 | font_info->min_char_or_byte2;
max_height = MAX(font_info->font_ascent + font_info->font_descent, max_height);
}
/* To have an uniform alignment */
for (int i = 0; i < FONT_MAX; i++)
fontset[i].avg_height = max_height;
return 0;
}
enum {
NET_WM_WINDOW_TYPE,
NET_WM_WINDOW_TYPE_DOCK,
NET_WM_DESKTOP,
NET_WM_STRUT_PARTIAL,
NET_WM_STRUT,
NET_WM_STATE,
NET_WM_WINDOW_OPACITY,
NET_WM_STATE_STICKY,
NET_WM_STATE_ABOVE,
};
void
set_ewmh_atoms ()
{
const char *atom_names[] = {
"_NET_WM_WINDOW_TYPE",
"_NET_WM_WINDOW_TYPE_DOCK",
"_NET_WM_DESKTOP",
"_NET_WM_STRUT_PARTIAL",
"_NET_WM_STRUT",
"_NET_WM_STATE",
"_NET_WM_WINDOW_OPACITY",
/* Leave those at the end since are batch-set */
"_NET_WM_STATE_STICKY",
"_NET_WM_STATE_ABOVE",
};
const int atoms = sizeof(atom_names)/sizeof(char *);
xcb_intern_atom_cookie_t atom_cookie[atoms];
xcb_atom_t atom_list[atoms];
xcb_intern_atom_reply_t *atom_reply;
int strut[12] = {0};
/* As suggested fetch all the cookies first (yum!) and then retrieve the
* atoms to exploit the async'ness */
for (int i = 0; i < atoms; i++)
atom_cookie[i] = xcb_intern_atom(c, 0, strlen(atom_names[i]), atom_names[i]);
for (int i = 0; i < atoms; i++) {
atom_reply = xcb_intern_atom_reply(c, atom_cookie[i], NULL);
if (!atom_reply)
return;
atom_list[i] = atom_reply->atom;
free(atom_reply);
}
/* Prepare the strut array */
if (bar_bottom) {
strut[3] = BAR_HEIGHT;
strut[11] = bar_width;
} else {
strut[2] = BAR_HEIGHT;
strut[9] = bar_width;
}
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atom_list[NET_WM_WINDOW_OPACITY], XCB_ATOM_CARDINAL, 32, 1, (const uint32_t []){ (uint32_t)(BAR_OPACITY * 0xffffffff) } );
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atom_list[NET_WM_WINDOW_TYPE], XCB_ATOM_ATOM, 32, 1, &atom_list[NET_WM_WINDOW_TYPE_DOCK]);
xcb_change_property (c, XCB_PROP_MODE_APPEND, win, atom_list[NET_WM_STATE], XCB_ATOM_ATOM, 32, 2, &atom_list[NET_WM_STATE_STICKY]);
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atom_list[NET_WM_DESKTOP], XCB_ATOM_CARDINAL, 32, 1, (const uint32_t []){ -1 } );
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atom_list[NET_WM_STRUT_PARTIAL], XCB_ATOM_CARDINAL, 32, 12, strut);
xcb_change_property (c, XCB_PROP_MODE_REPLACE, win, atom_list[NET_WM_STRUT], XCB_ATOM_CARDINAL, 32, 4, strut);
}
void
init (void)
{
xcb_screen_t *scr;
xcb_window_t root;
int y;
/* Connect to X */
c = xcb_connect (NULL, NULL);
if (xcb_connection_has_error (c)) {
fprintf (stderr, "Couldn't connect to X\n");
exit (1);
}
/* Grab infos from the first screen */
scr = xcb_setup_roots_iterator (xcb_get_setup (c)).data;
root = scr->root;
/* where to place the window */
y = (bar_bottom) ? (scr->height_in_pixels - BAR_HEIGHT) : 0;
bar_width = (BAR_WIDTH < 0) ? (scr->width_in_pixels - BAR_OFFSET) : BAR_WIDTH;
/* Load the font */
if (font_load ((const char* []){ BAR_FONT }))
exit (1);
/* Create the main window */
win = xcb_generate_id (c);
xcb_create_window (c, XCB_COPY_FROM_PARENT, win, root, BAR_OFFSET, y, bar_width,
BAR_HEIGHT, 0, XCB_WINDOW_CLASS_INPUT_OUTPUT, scr->root_visual,
XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, (const uint32_t []){ palette[10], XCB_EVENT_MASK_EXPOSURE });
/* For WM that support EWMH atoms */
set_ewmh_atoms();
xcb_change_window_attributes (c, win, XCB_CW_OVERRIDE_REDIRECT, (const uint32_t []){ force_docking });
/* Create a temporary canvas */
canvas = xcb_generate_id (c);
xcb_create_pixmap (c, scr->root_depth, canvas, root, bar_width, BAR_HEIGHT);
/* Create the gc for drawing */
draw_gc = xcb_generate_id (c);
xcb_create_gc (c, draw_gc, root, XCB_GC_FOREGROUND | XCB_GC_BACKGROUND, (const uint32_t []){ palette[11], palette[10] });
clear_gc = xcb_generate_id (c);
xcb_create_gc (c, clear_gc, root, XCB_GC_FOREGROUND, (const uint32_t []){ palette[10] });
underl_gc = xcb_generate_id (c);
xcb_create_gc (c, underl_gc, root, XCB_GC_FOREGROUND, (const uint32_t []){ palette[10] });
/* Make the bar visible */
xcb_map_window (c, win);
xcb_flush (c);
}
void
cleanup (void)
{
int i;
for (i = 0; i < FONT_MAX; i++) {
if (fontset[i].xcb_ft)
xcb_close_font (c, fontset[i].xcb_ft);
}
if (canvas)
xcb_free_pixmap (c, canvas);
if (win)
xcb_destroy_window (c, win);
if (draw_gc)
xcb_free_gc (c, draw_gc);
if (clear_gc)
xcb_free_gc (c, clear_gc);
if (underl_gc)
xcb_free_gc (c, underl_gc);
if (c)
xcb_disconnect (c);
}
void
sighandle (int signal)
{
if (signal == SIGINT || signal == SIGTERM)
exit (0);
}
int
main (int argc, char **argv)
{
char input[1024] = {0, };
struct pollfd pollin[2] = {
{ .fd = STDIN_FILENO, .events = POLLIN },
{ .fd = -1 , .events = POLLIN },
};
xcb_generic_event_t *ev;
xcb_expose_event_t *expose_ev;
int permanent = 0;
char ch;
while ((ch = getopt (argc, argv, "phbf")) != -1) {
switch (ch) {
case 'h':
printf ("usage: %s [-p | -h] [-b]\n"
"\t-h Show this help\n"
"\t-b Put bar at the bottom of the screen\n"
"\t-f Force docking (use this if your WM isn't EWMH compliant)\n"
"\t-p Don't close after the data ends\n", argv[0]);
exit (0);
case 'p': permanent = 1; break;
case 'b': bar_bottom = 1; break;
case 'f': force_docking = 1; break;
}
}
atexit (cleanup);
signal (SIGINT, sighandle);
signal (SIGTERM, sighandle);
init ();
/* Get the fd to Xserver */
pollin[1].fd = xcb_get_file_descriptor (c);
xcb_fill_rect (clear_gc, 0, 0, bar_width, BAR_HEIGHT);
for (;;) {
int redraw = 0;
if (poll (pollin, 2, -1) > 0) {
if (pollin[0].revents & POLLHUP) { /* No more data... */
if (permanent) pollin[0].fd = -1; /* ...null the fd and continue polling :D */
else break; /* ...bail out */
}
if (pollin[0].revents & POLLIN) { /* New input, process it */
fgets (input, sizeof(input), stdin);
parse (input);
redraw = 1;
}
if (pollin[1].revents & POLLIN) { /* Xserver broadcasted an event */
while ((ev = xcb_poll_for_event (c))) {
expose_ev = (xcb_expose_event_t *)ev;
switch (ev->response_type & 0x7F) {
case XCB_EXPOSE:
if (expose_ev->count == 0) redraw = 1;
break;
}
free (ev);
}
}
}
if (redraw) /* Copy our temporary pixmap onto the window */
xcb_copy_area (c, canvas, win, draw_gc, 0, 0, 0, 0, bar_width, BAR_HEIGHT);
xcb_flush (c);
}
return 0;
}