forked from krypt-n/bar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lemonbar.c
1592 lines (1329 loc) · 45.8 KB
/
lemonbar.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// vim:sw=4:ts=4:et:
#define _POSIX_C_SOURCE 200809L
#include <stdbool.h>
#include <stddef.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 <errno.h>
#include <xcb/xcb.h>
#include <xcb/xcbext.h>
#if WITH_XINERAMA
#include <xcb/xinerama.h>
#endif
#include <xcb/randr.h>
#include <X11/Xft/Xft.h>
#include <X11/Xlib-xcb.h>
// Here bet dragons
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#define indexof(c,s) (strchr((s),(c))-(s))
typedef struct font_t {
xcb_font_t ptr;
xcb_charinfo_t *width_lut;
XftFont *xft_ft;
int ascent;
int descent, height, width;
uint16_t char_max;
uint16_t char_min;
} font_t;
typedef struct monitor_t {
int x, y, width;
xcb_window_t window;
xcb_pixmap_t pixmap;
struct monitor_t *prev, *next;
} monitor_t;
typedef struct area_t {
unsigned int begin:16;
unsigned int end:16;
bool active:1;
int align:3;
unsigned int button:3;
xcb_window_t window;
char *cmd;
} area_t;
typedef union rgba_t {
struct {
uint8_t b;
uint8_t g;
uint8_t r;
uint8_t a;
};
uint32_t v;
} rgba_t;
typedef struct area_stack_t {
int at, max;
area_t *area;
} area_stack_t;
enum {
ATTR_OVERL = (1<<0),
ATTR_UNDERL = (1<<1),
};
enum { ALIGN_L = 0,
ALIGN_C,
ALIGN_R
};
enum {
GC_DRAW = 0,
GC_CLEAR,
GC_ATTR,
GC_MAX
};
#define MAX_FONT_COUNT 5
static Display *dpy;
static xcb_connection_t *c;
static xcb_screen_t *scr;
static int scr_nbr = 0;
static xcb_gcontext_t gc[GC_MAX];
static xcb_visualid_t visual;
static Visual *visual_ptr;
static xcb_colormap_t colormap;
static monitor_t *monhead, *montail;
static font_t *font_list[MAX_FONT_COUNT];
static int font_count = 0;
static int font_index = -1;
static int offsets_y[MAX_FONT_COUNT];
static int offset_y_count = 0;
static int offset_y_index = 0;
static uint32_t attrs = 0;
static bool dock = false;
static bool topbar = true;
static int bw = -1, bh = -1, bx = 0, by = 0;
static int bu = 1; // Underline height
static rgba_t fgc, bgc, ugc;
static rgba_t dfgc, dbgc, dugc;
static area_stack_t area_stack;
static XftColor sel_fg;
static XftDraw *xft_draw;
//char width lookuptable
#define MAX_WIDTHS (1 << 16)
static wchar_t xft_char[MAX_WIDTHS];
static char xft_width[MAX_WIDTHS];
void
update_gc (void)
{
xcb_change_gc(c, gc[GC_DRAW], XCB_GC_FOREGROUND, (const uint32_t []){ fgc.v });
xcb_change_gc(c, gc[GC_CLEAR], XCB_GC_FOREGROUND, (const uint32_t []){ bgc.v });
xcb_change_gc(c, gc[GC_ATTR], XCB_GC_FOREGROUND, (const uint32_t []){ ugc.v });
XftColorFree(dpy, visual_ptr, colormap , &sel_fg);
char color[] = "#ffffff";
uint32_t nfgc = fgc.v & 0x00ffffff;
snprintf(color, sizeof(color), "#%06X", nfgc);
if (!XftColorAllocName (dpy, visual_ptr, colormap, color, &sel_fg)) {
fprintf(stderr, "Couldn't allocate xft font color '%s'\n", color);
}
}
void
fill_gradient (xcb_drawable_t d, int x, int y, int width, int height, rgba_t start, rgba_t stop)
{
float i;
const int K = 25; // The number of steps
for (i = 0.; i < 1.; i += (1. / K)) {
// Perform the linear interpolation magic
unsigned int rr = i * stop.r + (1. - i) * start.r;
unsigned int gg = i * stop.g + (1. - i) * start.g;
unsigned int bb = i * stop.b + (1. - i) * start.b;
// The alpha is ignored here
rgba_t step = {
.r = rr,
.g = gg,
.b = bb,
.a = 255,
};
xcb_change_gc(c, gc[GC_DRAW], XCB_GC_FOREGROUND, (const uint32_t []){ step.v });
xcb_poly_fill_rectangle(c, d, gc[GC_DRAW], 1,
(const xcb_rectangle_t []){ { x, i * bh, width, bh / K + 1 } });
}
xcb_change_gc(c, gc[GC_DRAW], XCB_GC_FOREGROUND, (const uint32_t []){ fgc.v });
}
void
fill_rect (xcb_drawable_t d, xcb_gcontext_t _gc, int x, int y, int width, int height)
{
xcb_poly_fill_rectangle(c, d, _gc, 1, (const xcb_rectangle_t []){ { x, y, width, height } });
}
// Apparently xcb cannot seem to compose the right request for this call, hence we have to do it by
// ourselves.
// The funcion is taken from 'wmdia' (http://wmdia.sourceforge.net/)
xcb_void_cookie_t xcb_poly_text_16_simple(xcb_connection_t * c,
xcb_drawable_t drawable, xcb_gcontext_t gc, int16_t x, int16_t y,
uint32_t len, const uint16_t *str)
{
static const xcb_protocol_request_t xcb_req = {
5, // count
0, // ext
XCB_POLY_TEXT_16, // opcode
1 // isvoid
};
struct iovec xcb_parts[7];
uint8_t xcb_lendelta[2];
xcb_void_cookie_t xcb_ret;
xcb_poly_text_8_request_t xcb_out;
xcb_out.pad0 = 0;
xcb_out.drawable = drawable;
xcb_out.gc = gc;
xcb_out.x = x;
xcb_out.y = y;
xcb_lendelta[0] = len;
xcb_lendelta[1] = 0;
xcb_parts[2].iov_base = (char *)&xcb_out;
xcb_parts[2].iov_len = sizeof(xcb_out);
xcb_parts[3].iov_base = 0;
xcb_parts[3].iov_len = -xcb_parts[2].iov_len & 3;
xcb_parts[4].iov_base = xcb_lendelta;
xcb_parts[4].iov_len = sizeof(xcb_lendelta);
xcb_parts[5].iov_base = (char *)str;
xcb_parts[5].iov_len = len * sizeof(int16_t);
xcb_parts[6].iov_base = 0;
xcb_parts[6].iov_len = -(xcb_parts[4].iov_len + xcb_parts[5].iov_len) & 3;
xcb_ret.sequence = xcb_send_request(c, 0, xcb_parts + 2, &xcb_req);
return xcb_ret;
}
int
xft_char_width_slot (uint16_t ch)
{
int slot = ch % MAX_WIDTHS;
while (xft_char[slot] != 0 && xft_char[slot] != ch)
{
slot = (slot + 1) % MAX_WIDTHS;
}
return slot;
}
int xft_char_width (uint16_t ch, font_t *cur_font)
{
int slot = xft_char_width_slot(ch);
if (!xft_char[slot]) {
XGlyphInfo gi;
FT_UInt glyph = XftCharIndex (dpy, cur_font->xft_ft, (FcChar32) ch);
XftFontLoadGlyphs (dpy, cur_font->xft_ft, FcFalse, &glyph, 1);
XftGlyphExtents (dpy, cur_font->xft_ft, &glyph, 1, &gi);
XftFontUnloadGlyphs (dpy, cur_font->xft_ft, &glyph, 1);
xft_char[slot] = ch;
xft_width[slot] = gi.xOff;
return gi.xOff;
} else if (xft_char[slot] == ch)
return xft_width[slot];
else
return 0;
}
int
shift (monitor_t *mon, int x, int align, int ch_width)
{
switch (align) {
case ALIGN_C:
xcb_copy_area(c, mon->pixmap, mon->pixmap, gc[GC_DRAW],
mon->width / 2 - x / 2, 0,
mon->width / 2 - (x + ch_width) / 2, 0,
x, bh);
x = mon->width / 2 - (x + ch_width) / 2 + x;
break;
case ALIGN_R:
xcb_copy_area(c, mon->pixmap, mon->pixmap, gc[GC_DRAW],
mon->width - x, 0,
mon->width - x - ch_width, 0,
x, bh);
x = mon->width - ch_width;
break;
}
/* Draw the background first */
fill_rect(mon->pixmap, gc[GC_CLEAR], x, 0, ch_width, bh);
return x;
}
void
draw_lines (monitor_t *mon, int x, int w)
{
/* We can render both at the same time */
if (attrs & ATTR_OVERL)
fill_rect(mon->pixmap, gc[GC_ATTR], x, 0, w, bu);
if (attrs & ATTR_UNDERL)
fill_rect(mon->pixmap, gc[GC_ATTR], x, bh - bu, w, bu);
}
void
draw_shift (monitor_t *mon, int x, int align, int w)
{
x = shift(mon, x, align, w);
draw_lines(mon, x, w);
}
int
draw_char (monitor_t *mon, font_t *cur_font, int x, int align, uint16_t ch)
{
int ch_width;
if (cur_font->xft_ft) {
ch_width = xft_char_width(ch, cur_font);
} else {
ch_width = (cur_font->width_lut) ?
cur_font->width_lut[ch - cur_font->char_min].character_width:
cur_font->width;
}
x = shift(mon, x, align, ch_width);
int y = bh / 2 + cur_font->height / 2- cur_font->descent + offsets_y[offset_y_index];
if (cur_font->xft_ft) {
XftDrawString16 (xft_draw, &sel_fg, cur_font->xft_ft, x,y, &ch, 1);
} else {
/* xcb accepts string in UCS-2 BE, so swap */
ch = (ch >> 8) | (ch << 8);
// The coordinates here are those of the baseline
xcb_poly_text_16_simple(c, mon->pixmap, gc[GC_DRAW],
x, y,
1, &ch);
}
draw_lines(mon, x, ch_width);
return ch_width;
}
rgba_t
parse_color (const char *str, char **end, const rgba_t def)
{
int string_len;
char *ep;
if (!str)
return def;
// Reset
if (str[0] == '-') {
if (end)
*end = (char *)str + 1;
return def;
}
// Hex representation
if (str[0] != '#') {
if (end)
*end = (char *)str;
fprintf(stderr, "Invalid color specified\n");
return def;
}
errno = 0;
rgba_t tmp = (rgba_t)(uint32_t)strtoul(str + 1, &ep, 16);
if (end)
*end = ep;
// Some error checking is definitely good
if (errno) {
fprintf(stderr, "Invalid color specified\n");
return def;
}
string_len = ep - (str + 1);
switch (string_len) {
case 3:
// Expand the #rgb format into #rrggbb (aa is set to 0xff)
tmp.v = (tmp.v & 0xf00) * 0x1100
| (tmp.v & 0x0f0) * 0x0110
| (tmp.v & 0x00f) * 0x0011;
case 6:
// If the code is in #rrggbb form then assume it's opaque
tmp.a = 255;
break;
case 7:
case 8:
// Colors in #aarrggbb format, those need no adjustments
break;
default:
fprintf(stderr, "Invalid color specified\n");
return def;
}
// Premultiply the alpha in
if (tmp.a) {
// The components are clamped automagically as the rgba_t is made of uint8_t
return (rgba_t){
.r = (tmp.r * tmp.a) / 255,
.g = (tmp.g * tmp.a) / 255,
.b = (tmp.b * tmp.a) / 255,
.a = tmp.a,
};
}
return (rgba_t)0U;
}
void
set_attribute (const char modifier, const char attribute)
{
int pos = indexof(attribute, "ou");
if (pos < 0) {
fprintf(stderr, "Invalid attribute \"%c\" found\n", attribute);
return;
}
switch (modifier) {
case '+':
attrs |= (1u<<pos);
break;
case '-':
attrs &=~(1u<<pos);
break;
case '!':
attrs ^= (1u<<pos);
break;
}
}
area_t *
area_get (xcb_window_t win, const int btn, const int x)
{
// Looping backwards ensures that we get the innermost area first
for (int i = area_stack.at - 1; i >= 0; i--) {
area_t *a = &area_stack.area[i];
if (a->window == win && a->button == btn && x >= a->begin && x < a->end)
return a;
}
return NULL;
}
void
area_shift (xcb_window_t win, const int align, int delta)
{
if (align == ALIGN_L)
return;
if (align == ALIGN_C)
delta /= 2;
for (int i = 0; i < area_stack.at; i++) {
area_t *a = &area_stack.area[i];
if (a->window == win && a->align == align && !a->active) {
a->begin -= delta;
a->end -= delta;
}
}
}
bool
area_add (char *str, const char *optend, char **end, monitor_t *mon, const int x, const int align, const int button)
{
int i;
char *trail;
area_t *a;
// A wild close area tag appeared!
if (*str != ':') {
*end = str;
// Find most recent unclosed area.
for (i = area_stack.at - 1; i >= 0 && !area_stack.area[i].active; i--)
;
a = &area_stack.area[i];
// Basic safety checks
if (!a->cmd || a->align != align || a->window != mon->window) {
fprintf(stderr, "Invalid geometry for the clickable area\n");
return false;
}
const int size = x - a->begin;
switch (align) {
case ALIGN_L:
a->end = x;
break;
case ALIGN_C:
a->begin = mon->width / 2 - size / 2 + a->begin / 2;
a->end = a->begin + size;
break;
case ALIGN_R:
// The newest is the rightmost one
a->begin = mon->width - size;
a->end = mon->width;
break;
}
a->active = false;
return true;
}
if (area_stack.at + 1 > area_stack.max) {
fprintf(stderr, "Cannot add any more clickable areas (used %d/%d)\n",
area_stack.at, area_stack.max);
return false;
}
a = &area_stack.area[area_stack.at++];
// Found the closing : and check if it's just an escaped one
for (trail = strchr(++str, ':'); trail && trail[-1] == '\\'; trail = strchr(trail + 1, ':'))
;
// Find the trailing : and make sure it's within the formatting block, also reject empty commands
if (!trail || str == trail || trail > optend) {
*end = str;
return false;
}
*trail = '\0';
// Sanitize the user command by unescaping all the :
for (char *needle = str; *needle; needle++) {
int delta = trail - &needle[1];
if (needle[0] == '\\' && needle[1] == ':') {
memmove(&needle[0], &needle[1], delta);
needle[delta] = 0;
}
}
// This is a pointer to the string buffer allocated in the main
a->cmd = str;
a->active = true;
a->align = align;
a->begin = x;
a->window = mon->window;
a->button = button;
*end = trail + 1;
return true;
}
bool
font_has_glyph (font_t *font, const uint16_t c)
{
if (font->xft_ft) {
if (XftCharExists(dpy, font->xft_ft, (FcChar32) c)) {
return true;
} else {
return false;
}
}
if (c < font->char_min || c > font->char_max)
return false;
if (font->width_lut && font->width_lut[c - font->char_min].character_width == 0)
return false;
return true;
}
font_t *
select_drawable_font (const uint16_t c)
{
// If the user has specified a font to use, try that first.
if (font_index != -1 && font_has_glyph(font_list[font_index - 1], c)) {
offset_y_index = font_index - 1;
return font_list[font_index - 1];
}
// If the end is reached without finding an appropriate font, return NULL.
// If the font can draw the character, return it.
for (int i = 0; i < font_count; i++) {
if (font_has_glyph(font_list[i], c)) {
offset_y_index = i;
return font_list[i];
}
}
return NULL;
}
void
parse (char *text)
{
font_t *cur_font;
monitor_t *cur_mon;
int pos_x, align, button;
char *p = text, *block_end, *ep;
rgba_t tmp;
pos_x = 0;
align = ALIGN_L;
cur_mon = monhead;
// Reset the stack position
area_stack.at = 0;
for (monitor_t *m = monhead; m != NULL; m = m->next)
fill_rect(m->pixmap, gc[GC_CLEAR], 0, 0, m->width, bh);
/* Create xft drawable */
if (!(xft_draw = XftDrawCreate (dpy, cur_mon->pixmap, visual_ptr , colormap))) {
fprintf(stderr, "Couldn't create xft drawable\n");
}
for (;;) {
if (*p == '\0' || *p == '\n')
break;
if (p[0] == '%' && p[1] == '{' && (block_end = strchr(p++, '}'))) {
p++;
while (p < block_end) {
int w;
while (isspace(*p))
p++;
switch (*p++) {
case '+': set_attribute('+', *p++); break;
case '-': set_attribute('-', *p++); break;
case '!': set_attribute('!', *p++); break;
case 'R':
tmp = fgc;
fgc = bgc;
bgc = tmp;
update_gc();
break;
case 'l': pos_x = 0; align = ALIGN_L; break;
case 'c': pos_x = 0; align = ALIGN_C; break;
case 'r': pos_x = 0; align = ALIGN_R; break;
case 'A':
button = XCB_BUTTON_INDEX_1;
// The range is 1-5
if (isdigit(*p) && (*p > '0' && *p < '6'))
button = *p++ - '0';
if (!area_add(p, block_end, &p, cur_mon, pos_x, align, button))
return;
break;
case 'B': bgc = parse_color(p, &p, dbgc); update_gc(); break;
case 'F': fgc = parse_color(p, &p, dfgc); update_gc(); break;
case 'U': ugc = parse_color(p, &p, dugc); update_gc(); break;
case 'S':
if (*p == '+' && cur_mon->next)
{ cur_mon = cur_mon->next; }
else if (*p == '-' && cur_mon->prev)
{ cur_mon = cur_mon->prev; }
else if (*p == 'f')
{ cur_mon = monhead; }
else if (*p == 'l')
{ cur_mon = montail ? montail : monhead; }
else if (isdigit(*p))
{ cur_mon = monhead;
for (int i = 0; i != *p-'0' && cur_mon->next; i++)
cur_mon = cur_mon->next;
}
else
{ p++; continue; }
XftDrawDestroy (xft_draw);
if (!(xft_draw = XftDrawCreate (dpy, cur_mon->pixmap, visual_ptr , colormap ))) {
fprintf(stderr, "Couldn't create xft drawable\n");
}
p++;
pos_x = 0;
break;
case 'O':
errno = 0;
w = (int) strtoul(p, &p, 10);
if (errno)
continue;
draw_shift(cur_mon, pos_x, align, w);
pos_x += w;
area_shift(cur_mon->window, align, w);
break;
case 'T':
if (*p == '-') { //Reset to automatic font selection
font_index = -1;
p++;
break;
} else if (isdigit(*p)) {
font_index = (int)strtoul(p, &ep, 10);
// User-specified 'font_index' ∊ (0,font_count]
// Otherwise just fallback to the automatic font selection
if (!font_index || font_index > font_count)
font_index = -1;
p = ep;
break;
} else {
fprintf(stderr, "Invalid font slot \"%c\"\n", *p++); //Swallow the token
break;
}
// In case of error keep parsing after the closing }
default:
p = block_end;
}
}
// Eat the trailing }
p++;
} else { // utf-8 -> ucs-2
uint8_t *utf = (uint8_t *)p;
uint16_t ucs;
// ASCII
if (utf[0] < 0x80) {
ucs = utf[0];
p += 1;
}
// Two byte utf8 sequence
else if ((utf[0] & 0xe0) == 0xc0) {
ucs = (utf[0] & 0x1f) << 6 | (utf[1] & 0x3f);
p += 2;
}
// Three byte utf8 sequence
else if ((utf[0] & 0xf0) == 0xe0) {
ucs = (utf[0] & 0xf) << 12 | (utf[1] & 0x3f) << 6 | (utf[2] & 0x3f);
p += 3;
}
// Four byte utf8 sequence
else if ((utf[0] & 0xf8) == 0xf0) {
ucs = 0xfffd;
p += 4;
}
// Five byte utf8 sequence
else if ((utf[0] & 0xfc) == 0xf8) {
ucs = 0xfffd;
p += 5;
}
// Six byte utf8 sequence
else if ((utf[0] & 0xfe) == 0xfc) {
ucs = 0xfffd;
p += 6;
}
// Not a valid utf-8 sequence
else {
ucs = utf[0];
p += 1;
}
cur_font = select_drawable_font(ucs);
if (!cur_font)
continue;
if(cur_font->ptr)
xcb_change_gc(c, gc[GC_DRAW] , XCB_GC_FONT, (const uint32_t []) {
cur_font->ptr
});
int w = draw_char(cur_mon, cur_font, pos_x, align, ucs);
pos_x += w;
area_shift(cur_mon->window, align, w);
}
}
XftDrawDestroy (xft_draw);
}
void
font_load (const char *pattern)
{
if (font_count >= MAX_FONT_COUNT) {
fprintf(stderr, "Max font count reached. Could not load font \"%s\"\n", pattern);
return;
}
xcb_query_font_cookie_t queryreq;
xcb_query_font_reply_t *font_info;
xcb_void_cookie_t cookie;
xcb_font_t font;
font = xcb_generate_id(c);
font_t *ret = calloc(1, sizeof(font_t));
if (!ret)
return;
cookie = xcb_open_font_checked(c, font, strlen(pattern), pattern);
if (!xcb_request_check (c, cookie)) {
queryreq = xcb_query_font(c, font);
font_info = xcb_query_font_reply(c, queryreq, NULL);
ret->xft_ft = NULL;
ret->ptr = font;
ret->descent = font_info->font_descent;
ret->height = font_info->font_ascent + font_info->font_descent;
ret->width = font_info->max_bounds.character_width;
ret->char_max = font_info->max_byte1 << 8 | font_info->max_char_or_byte2;
ret->char_min = font_info->min_byte1 << 8 | font_info->min_char_or_byte2;
// Copy over the width lut as it's part of font_info
int lut_size = sizeof(xcb_charinfo_t) * xcb_query_font_char_infos_length(font_info);
if (lut_size) {
ret->width_lut = malloc(lut_size);
memcpy(ret->width_lut, xcb_query_font_char_infos(font_info), lut_size);
}
free(font_info);
} else if ((ret->xft_ft = XftFontOpenName (dpy, scr_nbr, pattern))) {
ret->ptr = 0;
ret->ascent = ret->xft_ft->ascent;
ret->descent = ret->xft_ft->descent;
ret->height = ret->ascent + ret->descent;
} else {
fprintf(stderr, "Could not load font %s\n", pattern);
free(ret);
return;
}
font_list[font_count++] = ret;
}
void add_y_offset(int offset) {
if (offset_y_count >= MAX_FONT_COUNT) {
fprintf(stderr, "Max offset count reached. Could not set offset \"%d\"\n", offset);
return;
}
offsets_y[offset_y_count] = strtol(optarg, NULL, 10);
if (offset_y_count == 0) {
for (int i = 1; i < MAX_FONT_COUNT; ++i) {
offsets_y[i] = offsets_y[0];
}
}
++offset_y_count;
}
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_STATE_STICKY,
NET_WM_STATE_ABOVE,
};
void
set_ewmh_atoms (void)
{
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",
// 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;
// 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
for (monitor_t *mon = monhead; mon; mon = mon->next) {
int strut[12] = {0};
if (topbar) {
strut[2] = bh;
strut[8] = mon->x;
strut[9] = mon->x + mon->width;
} else {
strut[3] = bh;
strut[10] = mon->x;
strut[11] = mon->x + mon->width;
}
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, 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, mon->window, atom_list[NET_WM_STATE], XCB_ATOM_ATOM, 32, 2, &atom_list[NET_WM_STATE_STICKY]);
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_DESKTOP], XCB_ATOM_CARDINAL, 32, 1, (const uint32_t []) {
0u - 1u
} );
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_STRUT_PARTIAL], XCB_ATOM_CARDINAL, 32, 12, strut);
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, atom_list[NET_WM_STRUT], XCB_ATOM_CARDINAL, 32, 4, strut);
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8, 3, "bar");
xcb_change_property(c, XCB_PROP_MODE_REPLACE, mon->window, XCB_ATOM_WM_CLASS, XCB_ATOM_STRING, 8, 12, "lemonbar\0Bar");
}
}
monitor_t *
monitor_new (int x, int y, int width, int height)
{
monitor_t *ret;
ret = calloc(1, sizeof(monitor_t));
if (!ret) {
fprintf(stderr, "Failed to allocate new monitor\n");
exit(EXIT_FAILURE);
}
ret->x = x;
ret->y = (topbar ? by : height - bh - by) + y;
ret->width = width;
ret->next = ret->prev = NULL;
ret->window = xcb_generate_id(c);
int depth = (visual == scr->root_visual) ? XCB_COPY_FROM_PARENT : 32;
xcb_create_window(c, depth, ret->window, scr->root,
ret->x, ret->y, width, bh, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT, visual,
XCB_CW_BACK_PIXEL | XCB_CW_BORDER_PIXEL | XCB_CW_OVERRIDE_REDIRECT | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP,
(const uint32_t []) {
bgc.v, bgc.v, dock, XCB_EVENT_MASK_EXPOSURE | XCB_EVENT_MASK_BUTTON_PRESS, colormap
});
ret->pixmap = xcb_generate_id(c);
xcb_create_pixmap(c, depth, ret->pixmap, ret->window, width, bh);
return ret;
}
void
monitor_add (monitor_t *mon)
{
if (!monhead) {
monhead = mon;
} else if (!montail) {
montail = mon;
monhead->next = mon;
mon->prev = monhead;
} else {
mon->prev = montail;
montail->next = mon;
montail = montail->next;
}
}
int
rect_sort_cb (const void *p1, const void *p2)
{
const xcb_rectangle_t *r1 = (xcb_rectangle_t *)p1;
const xcb_rectangle_t *r2 = (xcb_rectangle_t *)p2;
if (r1->x < r2->x || r1->y + r1->height <= r2->y)
{
return -1;
}
if (r1->x > r2->x || r1->y + r1->height > r2->y)
{
return 1;
}
return 0;
}
void
monitor_create_chain (xcb_rectangle_t *rects, const int num)
{
int i;
int width = 0, height = 0;
int left = bx;
// Sort before use
qsort(rects, num, sizeof(xcb_rectangle_t), rect_sort_cb);
for (i = 0; i < num; i++) {
int h = rects[i].y + rects[i].height;
// Accumulated width of all monitors
width += rects[i].width;
// Get height of screen from y_offset + height of lowest monitor
if (h >= height)
height = h;
}
if (bw < 0)
bw = width - bx;
// Use the first font height as all the font heights have been set to the biggest of the set
if (bh < 0 || bh > height)
bh = font_list[0]->height + bu + 2;
// Check the geometry
if (bx + bw > width || by + bh > height) {
fprintf(stderr, "The geometry specified doesn't fit the screen!\n");
exit(EXIT_FAILURE);
}
// Left is a positive number or zero therefore monitors with zero width are excluded
width = bw;