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
|
#define _GNU_SOURCE
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcft/fcft.h>
#include <fcntl.h>
#include <pixman-1/pixman.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#include <utlist.h>
#include <wayland-client.h>
#include "utf8.h"
#include "wlr-layer-shell-unstable-v1-protocol.h"
#include "xdg-shell-protocol.h"
#include "xdg-output-protocol.h"
#define DIE(fmt, ...) \
do { \
fprintf(stderr, fmt "\n", ##__VA_ARGS__); \
exit(1); \
} while (0)
#define EDIE(fmt, ...) \
DIE(fmt ": %s", ##__VA_ARGS__, strerror(errno));
#define CLEANUP_DIE(why) \
do { \
cleanup(); \
DIE(why); \
} while(0)
#define CLEANUP_EDIE(why) \
do { \
cleanup(); \
EDIE(why); \
} while(0)
#define MIN(a, b) \
((a) < (b) ? (a) : (b))
#define MAX(a, b) \
((a) > (b) ? (a) : (b))
#define PROGRAM "dwlb"
#define VERSION "0.1"
#define USAGE \
"usage: dwlb [OPTIONS]\n" \
" -status [OUTPUT] [TEXT] send status text to dwlb\n" \
" -hide-vacant-tags do not display empty and inactive tags\n" \
" -font [FONT] specify a font\n" \
" -text-color [COLOR] specify text color\n" \
" -active-color [COLOR] specify color to indicate active tags or monitors\n" \
" -inactive-color [COLOR] specify color to indicate inactive tags or monitors\n" \
" -urg-text-color [COLOR] specify text color on urgent tags\n" \
" -urg-bg-color [COLOR] specify color of urgent tags\n" \
" -v get version information\n" \
" -h view this help text\n"
typedef struct Bar Bar;
struct Bar {
struct zxdg_output_v1 *xdg_output;
struct wl_output *wl_output;
struct wl_surface *wl_surface;
struct zwlr_layer_surface_v1 *layer_surface;
uint32_t registry_name;
char *xdg_output_name;
uint32_t width;
uint32_t height;
uint32_t textpadding;
uint32_t stride;
uint32_t bufsize;
uint32_t mtags;
uint32_t ctags;
uint32_t urg;
int selmon;
char *layout;
char *title;
char status[256];
bool redraw;
Bar *prev, *next;
};
static int sock_fd;
static char socketdir[256];
static char *socketpath = NULL;
static char sockbuf[512];
static char stdinbuf[BUFSIZ];
static struct wl_display *display;
static struct wl_compositor *compositor;
static struct wl_shm *shm;
static struct zwlr_layer_shell_v1 *layer_shell;
static struct zxdg_output_manager_v1 *output_manager;
static uint32_t height;
static uint32_t textpadding;
static bool run_display = true;
static bool ready = false;
static bool hide_vacant = false;
static Bar *bars = NULL;
static char *tags[9] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
static struct fcft_font *font;
static pixman_color_t activecolor = { .red = 0x0000, .green = 0x5555, .blue = 0x7777, .alpha = 0xffff, };
static pixman_color_t inactivecolor = { .red = 0x2222, .green = 0x2222, .blue = 0x2222, .alpha = 0xffff, };
static pixman_color_t textcolor = { .red = 0xeeee, .green = 0xeeee, .blue = 0xeeee, .alpha = 0xffff, };
static pixman_color_t urgbgcolor = { .red = 0xeeee, .green = 0xeeee, .blue = 0xeeee, .alpha = 0xffff, };
static pixman_color_t urgtextcolor = { .red = 2222, .green = 0x2222, .blue = 0x2222, .alpha = 0xffff, };
static void
wl_buffer_release(void *data, struct wl_buffer *wl_buffer)
{
/* Sent by the compositor when it's no longer using this buffer */
wl_buffer_destroy(wl_buffer);
}
static const struct wl_buffer_listener wl_buffer_listener = {
.release = wl_buffer_release,
};
/* Shared memory support function adapted from [wayland-book] */
static int
allocate_shm_file(size_t size)
{
int fd = memfd_create("surface", MFD_CLOEXEC);
if (fd < 0)
return -1;
int ret;
do {
ret = ftruncate(fd, size);
} while (ret < 0 && errno == EINTR);
if (ret < 0) {
close(fd);
return -1;
}
return fd;
}
static uint32_t
draw_text(char *text,
uint32_t xpos,
uint32_t ypos,
pixman_image_t *foreground,
pixman_image_t *background,
pixman_color_t *fgcolor,
pixman_color_t *bgcolor,
uint32_t height,
uint32_t padding)
{
uint32_t codepoint;
uint32_t state = UTF8_ACCEPT;
uint32_t ixpos = xpos;
uint32_t lastcp = 0;
pixman_image_t *fgfill = pixman_image_create_solid_fill(fgcolor);
if (!*text)
return 0;
xpos += padding;
for (char *p = text; *p; p++) {
/* Returns nonzero if more bytes are needed */
if (utf8decode(&state, &codepoint, *p))
continue;
/* Turn off subpixel rendering, which complicates things when
* mixed with alpha channels */
const struct fcft_glyph *glyph = fcft_rasterize_char_utf32(font, codepoint, FCFT_SUBPIXEL_NONE);
if (!glyph)
continue;
/* Adjust x position based on kerning with previous glyph */
long x_kern = 0;
if (lastcp)
fcft_kerning(font, lastcp, codepoint, &x_kern, NULL);
xpos += x_kern;
lastcp = codepoint;
/* Detect and handle pre-rendered glyphs (e.g. emoji) */
if (pixman_image_get_format(glyph->pix) == PIXMAN_a8r8g8b8) {
/* Only the alpha channel of the mask is used, so we can
* use fgfill here to blend prerendered glyphs with the
* same opacity */
pixman_image_composite32(
PIXMAN_OP_OVER, glyph->pix, fgfill, foreground, 0, 0, 0, 0,
xpos + glyph->x, ypos - glyph->y, glyph->width, glyph->height);
} else {
/* Applying the foreground color here would mess up
* component alphas for subpixel-rendered text, so we
* apply it when blending. */
pixman_image_composite32(
PIXMAN_OP_OVER, fgfill, glyph->pix, foreground, 0, 0, 0, 0,
xpos + glyph->x, ypos - glyph->y, glyph->width, glyph->height);
}
/* increment pen position */
xpos += glyph->advance.x;
ypos += glyph->advance.y;
}
if (state != UTF8_ACCEPT)
fprintf(stderr, "malformed UTF-8 sequence\n");
xpos += padding;
if (background && bgcolor)
pixman_image_fill_boxes(PIXMAN_OP_OVER, background,
bgcolor, 1, &(pixman_box32_t){
.x1 = ixpos,
.x2 = xpos,
.y1 = 0,
.y2 = height
});
pixman_image_unref(fgfill);
return xpos;
}
static int
draw_frame(Bar *b)
{
/* Allocate buffer to be attached to the surface */
int fd = allocate_shm_file(b->bufsize);
if (fd == -1)
return -1;
uint32_t *data = mmap(NULL, b->bufsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
close(fd);
return -1;
}
struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, b->bufsize);
struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, b->width, b->height, b->stride, WL_SHM_FORMAT_ARGB8888);
wl_buffer_add_listener(buffer, &wl_buffer_listener, NULL);
wl_shm_pool_destroy(pool);
close(fd);
/* Pixman image corresponding to main buffer */
pixman_image_t *bar = pixman_image_create_bits(PIXMAN_a8r8g8b8, b->width, b->height, data, b->width * 4);
/* Fill bar with background color */
pixman_image_fill_boxes(PIXMAN_OP_SRC, bar, b->selmon ? &activecolor : &inactivecolor, 1,
&(pixman_box32_t) {.x1 = 0, .x2 = b->width, .y1 = 0, .y2 = b->height});
/* Text background and foreground layers */
pixman_image_t *background_left = pixman_image_create_bits(PIXMAN_a8r8g8b8, b->width, b->height, NULL, b->width * 4);
pixman_image_t *foreground_left = pixman_image_create_bits(PIXMAN_a8r8g8b8, b->width, b->height, NULL, b->width * 4);
pixman_image_t *background_right = pixman_image_create_bits(PIXMAN_a8r8g8b8, b->width, b->height, NULL, b->width * 4);
pixman_image_t *foreground_right = pixman_image_create_bits(PIXMAN_a8r8g8b8, b->width, b->height, NULL, b->width * 4);
pixman_image_t *foreground_title = pixman_image_create_bits(PIXMAN_a8r8g8b8, b->width, b->height, NULL, b->width * 4);
/* Draw on images */
uint32_t xpos_left = 0;
uint32_t xpos_right;
uint32_t ypos = (b->height + font->ascent - font->descent) / 2;
uint32_t boxs = font->height / 9;
uint32_t boxw = font->height / 6 + 2;
for (uint32_t i = 0; i < 9; i++) {
bool active = b->mtags & 1 << i;
bool occupied = b->ctags & 1 << i;
bool urgent = b->urg & 1 << i;
if (hide_vacant && !active && !occupied && !urgent)
continue;
if (!hide_vacant && occupied)
pixman_image_fill_boxes(PIXMAN_OP_SRC, foreground_left,
&textcolor, 1, &(pixman_box32_t){
.x1 = xpos_left + boxs,
.x2 = xpos_left + boxs + boxw,
.y1 = boxs,
.y2 = boxs + boxw
});
if (urgent)
xpos_left = draw_text(tags[i], xpos_left, ypos, foreground_left, background_left,
&urgtextcolor, &urgbgcolor, b->height, b->textpadding);
else
xpos_left = draw_text(tags[i], xpos_left, ypos, foreground_left, background_left,
&textcolor, active ? &activecolor : &inactivecolor, b->height, b->textpadding);
}
xpos_left = draw_text(b->layout, xpos_left, ypos, foreground_left, background_left,
&textcolor, &inactivecolor, b->height, b->textpadding);
xpos_right = draw_text(b->status, 0, ypos, foreground_right, background_right,
&textcolor, &inactivecolor, b->height, b->textpadding);
if (xpos_right > b->width)
xpos_right = b->width;
draw_text(b->title, 0, ypos, foreground_title, NULL,
&textcolor, NULL, b->height, b->textpadding);
/* Draw background and foreground on bar */
pixman_image_composite32(PIXMAN_OP_OVER, foreground_title, NULL, bar, 0, 0, 0, 0, xpos_left, 0, b->width, b->height);
pixman_image_composite32(PIXMAN_OP_OVER, background_right, NULL, bar, 0, 0, 0, 0, b->width - xpos_right, 0, b->width, b->height);
pixman_image_composite32(PIXMAN_OP_OVER, foreground_right, NULL, bar, 0, 0, 0, 0, b->width - xpos_right, 0, b->width, b->height);
pixman_image_composite32(PIXMAN_OP_OVER, background_left, NULL, bar, 0, 0, 0, 0, 0, 0, b->width, b->height);
pixman_image_composite32(PIXMAN_OP_OVER, foreground_left, NULL, bar, 0, 0, 0, 0, 0, 0, b->width, b->height);
pixman_image_unref(foreground_left);
pixman_image_unref(background_left);
pixman_image_unref(foreground_right);
pixman_image_unref(background_right);
pixman_image_unref(foreground_title);
pixman_image_unref(bar);
munmap(data, b->bufsize);
wl_surface_attach(b->wl_surface, buffer, 0, 0);
wl_surface_damage_buffer(b->wl_surface, 0, 0, b->width, b->height);
wl_surface_commit(b->wl_surface);
return 0;
}
/* Layer-surface setup adapted from layer-shell example in [wlroots] */
static void
layer_surface_configure(void *data, struct zwlr_layer_surface_v1 *surface,
uint32_t serial, uint32_t w, uint32_t h)
{
Bar *b = (Bar *)data;
b->width = w;
b->height = h;
b->stride = b->width * 4;
b->bufsize = b->stride * b->height;
zwlr_layer_surface_v1_set_exclusive_zone(b->layer_surface, b->height);
zwlr_layer_surface_v1_ack_configure(surface, serial);
draw_frame(b);
}
static void
layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *surface)
{
Bar *b = (Bar *)data;
zwlr_layer_surface_v1_destroy(surface);
wl_surface_destroy(b->wl_surface);
run_display = false;
}
static void
cleanup(void)
{
if (socketpath)
unlink(socketpath);
}
static struct zwlr_layer_surface_v1_listener layer_surface_listener = {
.configure = layer_surface_configure,
.closed = layer_surface_closed,
};
static void
output_name(void *data, struct zxdg_output_v1 *xdg_output, const char *name)
{
Bar *b = (Bar *)data;
/* Is this necessary? */
if (b->xdg_output_name)
free(b->xdg_output_name);
if (!(b->xdg_output_name = strdup(name)))
CLEANUP_EDIE("strdup");
}
static void
output_logical_position(void *data, struct zxdg_output_v1 *xdg_output,
int32_t x, int32_t y)
{
}
static void
output_logical_size(void *data, struct zxdg_output_v1 *xdg_output,
int32_t width, int32_t height)
{
}
static void
output_done(void *data, struct zxdg_output_v1 *xdg_output)
{
}
static void
output_description(void *data, struct zxdg_output_v1 *xdg_output,
const char *description)
{
}
static struct zxdg_output_v1_listener output_listener = {
.name = output_name,
.description = output_description,
.done = output_done,
.logical_position = output_logical_position,
.logical_size = output_logical_size
};
static void
setup_bar(Bar *b)
{
b->height = height;
b->textpadding = textpadding;
b->layout = "[]=";
b->title = "";
b->xdg_output = zxdg_output_manager_v1_get_xdg_output(output_manager, b->wl_output);
if (!b->xdg_output)
CLEANUP_DIE("Could not create xdg_output");
zxdg_output_v1_add_listener(b->xdg_output, &output_listener, b);
b->wl_surface = wl_compositor_create_surface(compositor);
if (!b->wl_surface)
CLEANUP_DIE("Could not create wl_surface");
b->layer_surface = zwlr_layer_shell_v1_get_layer_surface(layer_shell, b->wl_surface, b->wl_output,
ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, PROGRAM);
if (!b->layer_surface)
CLEANUP_DIE("Could not create layer_surface");
zwlr_layer_surface_v1_add_listener(b->layer_surface, &layer_surface_listener, b);
zwlr_layer_surface_v1_set_size(b->layer_surface, 0, b->height);
zwlr_layer_surface_v1_set_anchor(b->layer_surface,
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
wl_surface_commit(b->wl_surface);
}
static void
handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version)
{
if (strcmp(interface, wl_compositor_interface.name) == 0) {
compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 4);
} else if (strcmp(interface, wl_shm_interface.name) == 0) {
shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
} else if (strcmp(interface, wl_output_interface.name) == 0) {
Bar *b = malloc(sizeof(Bar));
if (!b)
CLEANUP_EDIE("malloc");
memset(b, 0, sizeof(Bar));
b->registry_name = name;
b->wl_output = wl_registry_bind(registry, name, &wl_output_interface, 1);
DL_APPEND(bars, b);
if (ready)
setup_bar(b);
} else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) {
layer_shell = wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 1);
} else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
output_manager = wl_registry_bind(registry, name, &zxdg_output_manager_v1_interface, 2);
}
}
static void
teardown_bar(Bar *b)
{
zxdg_output_v1_destroy(b->xdg_output);
wl_surface_destroy(b->wl_surface);
zwlr_layer_surface_v1_destroy(b->layer_surface);
if (b->xdg_output_name)
free(b->xdg_output_name);
free(b);
}
static void
handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
{
Bar *b;
DL_FOREACH(bars, b)
if (b->registry_name == name)
break;
if (!b)
return;
DL_DELETE(bars, b);
teardown_bar(b);
}
static const struct wl_registry_listener registry_listener = {
.global = handle_global,
.global_remove = handle_global_remove
};
static int
advance_word(char **beg, char **end)
{
for (*beg = *end; **beg == ' '; (*beg)++);
for (*end = *beg; **end && **end != ' '; (*end)++);
if (!**end)
/* last word */
return -1;
**end = '\0';
(*end)++;
return 0;
}
#define ADVANCE() advance_word(&wordbeg, &wordend)
#define ADVANCE_IF_LAST_CONT() if (ADVANCE() == -1) continue
#define ADVANCE_IF_LAST_BREAK() if (ADVANCE() == -1) break
static void
read_stdin(void)
{
ssize_t len = read(STDIN_FILENO, stdinbuf, sizeof stdinbuf);
if (len == -1)
CLEANUP_EDIE("read");
if (len == 0) {
run_display = 0;
return;
}
char *end = (char *)&stdinbuf + len;
char *linebeg, *lineend;
char *wordbeg, *wordend;
for (linebeg = (char *)&stdinbuf;
(lineend = memchr(linebeg, '\n', end - linebeg));
linebeg = lineend) {
*lineend++ = '\0';
wordend = linebeg;
ADVANCE_IF_LAST_CONT();
Bar *b;
DL_FOREACH(bars, b)
if (b->xdg_output_name)
if (!strcmp(wordbeg, b->xdg_output_name))
break;
if (!b)
continue;
ADVANCE_IF_LAST_CONT();
if (!strcmp(wordbeg, "tags")) {
ADVANCE_IF_LAST_CONT();
b->ctags = atoi(wordbeg);
ADVANCE_IF_LAST_CONT();
b->mtags = atoi(wordbeg);
ADVANCE_IF_LAST_CONT();
/* skip sel */
ADVANCE();
b->urg = atoi(wordbeg);
b->redraw = true;
} else if (!strcmp(wordbeg, "layout")) {
b->layout = wordend;
b->redraw = true;
} else if (!strcmp(wordbeg, "title")) {
b->title = wordend;
b->redraw = true;
} else if (!strcmp(wordbeg, "selmon")) {
ADVANCE();
b->selmon = atoi(wordbeg);
b->redraw = true;
}
}
}
static void
read_socket(void)
{
int cli_fd;
if ((cli_fd = accept(sock_fd, NULL, 0)) == -1)
CLEANUP_EDIE("accept");
ssize_t len = recv(cli_fd, sockbuf, sizeof sockbuf - 1, 0);
if (len == -1)
CLEANUP_EDIE("recv");
close(cli_fd);
if (len == 0)
return;
sockbuf[len] = '\0';
do {
char *wordbeg, *wordend;
wordend = (char *)&sockbuf;
ADVANCE_IF_LAST_BREAK();
Bar *b;
bool all = false;
if (!strcmp(wordbeg, "all")) {
all = true;
} else if (!strcmp(wordbeg, "selected")) {
DL_FOREACH(bars, b)
if (b->selmon)
break;
} else {
DL_FOREACH(bars, b)
if (b->xdg_output_name)
if (!strcmp(wordbeg, b->xdg_output_name))
break;
}
if (!all && !b)
break;
ADVANCE_IF_LAST_BREAK();
if (!strcmp(wordbeg, "status")) {
if (all) {
DL_FOREACH(bars, b) {
snprintf(b->status, sizeof b->status, "%s", wordend);
b->redraw = true;
}
} else {
snprintf(b->status, sizeof b->status, "%s", wordend);
b->redraw = true;
}
}
} while (0);
}
static void
event_loop(void)
{
int wl_fd = wl_display_get_fd(display);
while (run_display) {
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
FD_SET(sock_fd, &rfds);
FD_SET(wl_fd, &rfds);
/* Does this need to be inside the loop? */
wl_display_flush(display);
if (select(MAX(sock_fd, wl_fd) + 1, &rfds, NULL, NULL, NULL) == -1)
continue;
if (FD_ISSET(STDIN_FILENO, &rfds))
read_stdin();
if (FD_ISSET(sock_fd, &rfds))
read_socket();
if (FD_ISSET(wl_fd, &rfds))
if (wl_display_dispatch(display) == -1)
break;
Bar *b;
DL_FOREACH(bars, b) {
if (b->redraw) {
draw_frame(b);
b->redraw = false;
}
}
}
}
static void
client_send_command(struct sockaddr_un *sock_address,
const char *output,
const char *cmd,
const char *data)
{
DIR *dir;
if (!(dir = opendir(socketdir)))
EDIE("Could not open directory '%s'", socketdir);
if (data)
snprintf(sockbuf, sizeof sockbuf, "%s %s %s", output, cmd, data);
else
snprintf(sockbuf, sizeof sockbuf, "%s %s", output, cmd);
size_t len = strlen(sockbuf);
struct dirent *de;
bool newfd = true;
while ((de = readdir(dir))) {
if (!strncmp(de->d_name, "dwlb-", 5)) {
if (newfd)
if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 1)) == -1)
EDIE("socket");
snprintf(sock_address->sun_path, sizeof sock_address->sun_path, "%s/%s", socketdir, de->d_name);
if (connect(sock_fd, (struct sockaddr *) sock_address, sizeof(*sock_address)) == -1) {
newfd = false;
continue;
}
if (send(sock_fd, sockbuf, len, 0) == -1)
fprintf(stderr, "Could not send status data to '%s'\n", sock_address->sun_path);
close(sock_fd);
newfd = true;
}
}
closedir(dir);
}
/* Color parsing logic adapted from [sway] */
static int
parse_color(const char *str, pixman_color_t *clr)
{
if (*str == '#')
str++;
int len = strlen(str);
// Disallows "0x" prefix that strtoul would ignore
if ((len != 6 && len != 8) || !isxdigit(str[0]) || !isxdigit(str[1]))
return -1;
char *ptr;
uint32_t parsed = strtoul(str, &ptr, 16);
if (*ptr)
return -1;
if (len == 8) {
clr->alpha = (parsed & 0xff) * 0x101;
parsed >>= 8;
} else {
clr->alpha = 0xffff;
}
clr->red = ((parsed >> 16) & 0xff) * 0x101;
clr->green = ((parsed >> 8) & 0xff) * 0x101;
clr->blue = ((parsed >> 0) & 0xff) * 0x101;
return 0;
}
void
sig_handler(int sig)
{
if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM)
run_display = false;
}
int
main(int argc, char **argv)
{
char *fontstr = "";
char *xdgruntimedir;
struct sockaddr_un sock_address;
/* Establish socket directory */
if (!(xdgruntimedir = getenv("XDG_RUNTIME_DIR")))
DIE("Could not retrieve XDG_RUNTIME_DIR");
snprintf(socketdir, sizeof socketdir, "%s/dwlb", xdgruntimedir);
if (mkdir(socketdir, S_IRWXU) == -1)
if (errno != EEXIST)
EDIE("Could not create directory '%s'", socketdir);
sock_address.sun_family = AF_UNIX;
/* Parse options */
for (int i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-status")) {
if (++i + 1 >= argc)
DIE("Option -status requires two arguments");
client_send_command(&sock_address, argv[i], "status", argv[i + 1]);
return 0;
} else if (!strcmp(argv[i], "-hide-vacant-tags")) {
hide_vacant = true;
} else if (!strcmp(argv[i], "-font")) {
if (++i >= argc)
DIE("Option -font requires an argument");
fontstr = argv[i];
} else if (!strcmp(argv[i], "-text-color")) {
if (++i >= argc)
DIE("Option -text-color requires an argument");
if (parse_color(argv[i], &textcolor) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-active-color")) {
if (++i >= argc)
DIE("Option -active-color requires an argument");
if (parse_color(argv[i], &activecolor) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-inactive-color")) {
if (++i >= argc)
DIE("Option -inactive-color requires an argument");
if (parse_color(argv[i], &inactivecolor) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-urg-text-color")) {
if (++i >= argc)
DIE("Option -urg-text-color requires an argument");
if (parse_color(argv[i], &urgtextcolor) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-urg-bg-color")) {
if (++i >= argc)
DIE("Option -urg-bg-color requires an argument");
if (parse_color(argv[i], &urgbgcolor) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-v")) {
fprintf(stderr, PROGRAM " " VERSION "\n");
return 0;
} else if (!strcmp(argv[i], "-h")) {
fprintf(stderr, USAGE);
return 0;
} else {
DIE("Option '%s' not recognized\n" USAGE, argv[i]);
}
}
/* Set up display and protocols */
display = wl_display_connect(NULL);
if (!display)
DIE("Failed to create display");
struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, ®istry_listener, NULL);
wl_display_roundtrip(display);
if (!compositor || !shm || !layer_shell || !output_manager)
DIE("Compositor does not support all needed protocols");
/* Load selected font */
fcft_init(FCFT_LOG_COLORIZE_AUTO, 0, FCFT_LOG_CLASS_ERROR);
fcft_set_scaling_filter(FCFT_SCALING_FILTER_LANCZOS3);
font = fcft_from_name(1, (const char *[]) {fontstr}, NULL);
if (!font)
DIE("Could not load font");
textpadding = font->height / 2;
height = font->ascent + font->descent;
/* Setup bars */
Bar *b;
DL_FOREACH(bars, b)
setup_bar(b);
wl_display_roundtrip(display);
/* Set up socket */
for (uint32_t i = 0;; i++) {
if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 1)) == -1)
DIE("socket");
snprintf(sock_address.sun_path, sizeof sock_address.sun_path, "%s/dwlb-%i", socketdir, i);
if (connect(sock_fd, (struct sockaddr *) &sock_address, sizeof sock_address) == -1)
break;
close(sock_fd);
}
socketpath = (char *)&sock_address.sun_path;
unlink(socketpath);
if (bind(sock_fd, (struct sockaddr *) &sock_address, sizeof sock_address) == -1)
CLEANUP_EDIE("bind");
if (listen(sock_fd, SOMAXCONN) == -1)
CLEANUP_EDIE("listen");
fcntl(sock_fd, F_SETFD, FD_CLOEXEC | fcntl(sock_fd, F_GETFD));
/* Set up signals */
signal(SIGINT, sig_handler);
signal(SIGHUP, sig_handler);
signal(SIGTERM, sig_handler);
signal(SIGPIPE, SIG_IGN);
/* Run */
ready = true;
event_loop();
/* Clean everything up */
close(sock_fd);
unlink(socketpath);
zwlr_layer_shell_v1_destroy(layer_shell);
zxdg_output_manager_v1_destroy(output_manager);
Bar *t;
DL_FOREACH_SAFE(bars, b, t)
teardown_bar(b);
fcft_destroy(font);
fcft_fini();
wl_shm_destroy(shm);
wl_compositor_destroy(compositor);
wl_registry_destroy(registry);
wl_display_disconnect(display);
return 0;
}
|