forked from kolunmi/dwlb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwlb.c
1843 lines (1627 loc) · 49.6 KB
/
dwlb.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
#define _GNU_SOURCE
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcft/fcft.h>
#include <fcntl.h>
#include <linux/input-event-codes.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/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#include <wayland-client.h>
#include <wayland-cursor.h>
#include <wayland-util.h>
#include "utf8.h"
#include "xdg-shell-protocol.h"
#include "xdg-output-unstable-v1-protocol.h"
#include "wlr-layer-shell-unstable-v1-protocol.h"
#include "net-tapesoftware-dwl-wm-unstable-v1-protocol.h"
#define DIE(fmt, ...) \
do { \
cleanup(); \
fprintf(stderr, fmt "\n", ##__VA_ARGS__); \
exit(1); \
} while (0)
#define EDIE(fmt, ...) \
DIE(fmt ": %s", ##__VA_ARGS__, strerror(errno));
#define MIN(a, b) \
((a) < (b) ? (a) : (b))
#define MAX(a, b) \
((a) > (b) ? (a) : (b))
#define LENGTH(x) \
(sizeof x / sizeof x[0])
#define ARRAY_INIT_CAP 16
#define ARRAY_EXPAND(arr, len, cap, inc) \
do { \
uint32_t new_len, new_cap; \
new_len = (len) + (inc); \
if (new_len > (cap)) { \
new_cap = new_len * 2; \
if (new_cap < ARRAY_INIT_CAP) \
new_cap = ARRAY_INIT_CAP; \
if (!((arr) = realloc((arr), sizeof(*(arr)) * new_cap))) \
EDIE("realloc"); \
(cap) = new_cap; \
} \
(len) = new_len; \
} while (0)
#define ARRAY_APPEND(arr, len, cap, ptr) \
do { \
ARRAY_EXPAND((arr), (len), (cap), 1); \
(ptr) = &(arr)[(len) - 1]; \
} while (0)
#define PROGRAM "dwlb"
#define VERSION "0.2"
#define USAGE \
"usage: dwlb [OPTIONS]\n" \
"Ipc\n" \
" -ipc allow commands to be sent to dwl (dwl must be patched)\n" \
" -no-ipc disable ipc\n" \
"Bar Config\n" \
" -hidden bars will initially be hidden\n" \
" -no-hidden bars will not initially be hidden\n" \
" -bottom bars will initially be drawn at the bottom\n" \
" -no-bottom bars will initially be drawn at the top\n" \
" -hide-vacant-tags do not display empty and inactive tags\n" \
" -no-hide-vacant-tags display empty and inactive tags\n" \
" -status-commands enable in-line commands in status text\n" \
" -no-status-commands disable in-line commands in status text\n" \
" -center-title center title text on bar\n" \
" -no-center-title do not center title text on bar\n" \
" -custom-title do not display window title and treat the area as another status text element; see -title command\n" \
" -no-custom-title display current window title as normal\n" \
" -font [FONT] specify a font\n" \
" -tags [NUMBER] [FIRST]...[LAST] if ipc is disabled, specify custom tag names\n" \
" -vertical-padding [PIXELS] specify vertical pixel padding above and below text\n" \
" -active-fg-color [COLOR] specify text color of active tags or monitors\n" \
" -active-bg-color [COLOR] specify background color of active tags or monitors\n" \
" -inactive-fg-color [COLOR] specify text color of inactive tags or monitors\n" \
" -inactive-fg-color [COLOR] specify background color of inactive tags or monitors\n" \
" -urgent-fg-color [COLOR] specify text color of urgent tags\n" \
" -urgent-bg-color [COLOR] specify background color of urgent tags\n" \
" -scale [BUFFER_SCALE] specify buffer scale value for integer scaling\n" \
"Commands\n" \
" -status [OUTPUT] [TEXT] set status text\n" \
" -title [OUTPUT] [TEXT] set title text, if -custom-title is enabled\n" \
" -show [OUTPUT] show bar\n" \
" -hide [OUTPUT] hide bar\n" \
" -toggle-visibility [OUTPUT] toggle bar visibility\n" \
" -set-top [OUTPUT] draw bar at the top\n" \
" -set-bottom [OUTPUT] draw bar at the bottom\n" \
" -toggle-location [OUTPUT] toggle bar location\n" \
"Other\n" \
" -v get version information\n" \
" -h view this help text\n"
typedef struct {
pixman_color_t color;
bool bg;
char *start;
} Color;
typedef struct {
uint32_t btn;
uint32_t x1;
uint32_t x2;
char command[128];
} Button;
typedef struct {
char text[2048];
Color *colors;
uint32_t colors_l, colors_c;
Button *buttons;
uint32_t buttons_l, buttons_c;
} CustomText;
typedef struct {
struct wl_output *wl_output;
struct wl_surface *wl_surface;
struct zwlr_layer_surface_v1 *layer_surface;
struct zxdg_output_v1 *xdg_output;
struct znet_tapesoftware_dwl_wm_monitor_v1 *dwl_wm_monitor;
uint32_t registry_name;
char *xdg_output_name;
bool configured;
uint32_t width, height;
uint32_t textpadding;
uint32_t stride, bufsize;
uint32_t mtags, ctags, urg, sel;
char *layout, *window_title;
uint32_t layout_idx, last_layout_idx;
CustomText title, status;
bool hidden, bottom;
bool redraw;
struct wl_list link;
} Bar;
typedef struct {
struct wl_seat *wl_seat;
struct wl_pointer *wl_pointer;
uint32_t registry_name;
Bar *bar;
uint32_t pointer_x, pointer_y;
uint32_t pointer_button;
struct wl_list link;
} Seat;
static int sock_fd;
static char socketdir[256];
static char *socketpath;
static char sockbuf[4096];
static char *stdinbuf;
static size_t stdinbuf_cap;
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 struct znet_tapesoftware_dwl_wm_v1 *dwl_wm;
static struct wl_cursor_image *cursor_image;
static struct wl_surface *cursor_surface;
static struct wl_list bar_list, seat_list;
static char **tags;
static uint32_t tags_l, tags_c;
static char **layouts;
static uint32_t layouts_l, layouts_c;
static struct fcft_font *font;
static uint32_t height, textpadding, buffer_scale;
static bool run_display;
#include "config.h"
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 == -1)
return -1;
int ret;
do {
ret = ftruncate(fd, size);
} while (ret == -1 && errno == EINTR);
if (ret == -1) {
close(fd);
return -1;
}
return fd;
}
static uint32_t
draw_text(char *text,
uint32_t x,
uint32_t y,
pixman_image_t *foreground,
pixman_image_t *background,
pixman_color_t *fg_color,
pixman_color_t *bg_color,
uint32_t max_x,
uint32_t buf_height,
uint32_t padding,
Color *colors,
uint32_t colors_l)
{
if (!text || !*text || !max_x)
return x;
uint32_t ix = x, nx;
if ((nx = x + padding) + padding >= max_x)
return x;
x = nx;
bool draw_fg = foreground && fg_color;
bool draw_bg = background && bg_color;
pixman_image_t *fg_fill;
pixman_color_t *cur_bg_color;
if (draw_fg)
fg_fill = pixman_image_create_solid_fill(fg_color);
if (draw_bg)
cur_bg_color = bg_color;
uint32_t color_ind = 0, codepoint, state = UTF8_ACCEPT, last_cp = 0;
for (char *p = text; *p; p++) {
/* Check for new colors */
if (state == UTF8_ACCEPT && colors && (draw_fg || draw_bg)) {
while (color_ind < colors_l && p == colors[color_ind].start) {
if (colors[color_ind].bg) {
if (draw_bg)
cur_bg_color = &colors[color_ind].color;
} else if (draw_fg) {
pixman_image_unref(fg_fill);
fg_fill = pixman_image_create_solid_fill(&colors[color_ind].color);
}
color_ind++;
}
}
/* 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 kern = 0;
if (last_cp)
fcft_kerning(font, last_cp, codepoint, &kern, NULL);
if ((nx = x + kern + glyph->advance.x) + padding > max_x)
break;
last_cp = codepoint;
x += kern;
if (draw_fg) {
/* 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, fg_fill, foreground, 0, 0, 0, 0,
x + glyph->x, y - 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, fg_fill, glyph->pix, foreground, 0, 0, 0, 0,
x + glyph->x, y - glyph->y, glyph->width, glyph->height);
}
}
if (draw_bg) {
pixman_image_fill_boxes(PIXMAN_OP_OVER, background,
cur_bg_color, 1, &(pixman_box32_t){
.x1 = x, .x2 = nx,
.y1 = 0, .y2 = buf_height
});
}
/* increment pen position */
x = nx;
}
if (draw_fg)
pixman_image_unref(fg_fill);
if (!last_cp)
return ix;
nx = x + padding;
if (draw_bg) {
/* Fill padding background */
pixman_image_fill_boxes(PIXMAN_OP_OVER, background,
bg_color, 1, &(pixman_box32_t){
.x1 = ix, .x2 = ix + padding,
.y1 = 0, .y2 = buf_height
});
pixman_image_fill_boxes(PIXMAN_OP_OVER, background,
bg_color, 1, &(pixman_box32_t){
.x1 = x, .x2 = nx,
.y1 = 0, .y2 = buf_height
});
}
return nx;
}
#define TEXT_WIDTH(text, maxwidth, padding) \
draw_text(text, 0, 0, NULL, NULL, NULL, NULL, maxwidth, 0, padding, NULL, 0)
static int
draw_frame(Bar *bar)
{
/* Allocate buffer to be attached to the surface */
int fd = allocate_shm_file(bar->bufsize);
if (fd == -1)
return -1;
uint32_t *data = mmap(NULL, bar->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, bar->bufsize);
struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0, bar->width, bar->height, bar->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 *final = pixman_image_create_bits(PIXMAN_a8r8g8b8, bar->width, bar->height, data, bar->width * 4);
/* Text background and foreground layers */
pixman_image_t *foreground = pixman_image_create_bits(PIXMAN_a8r8g8b8, bar->width, bar->height, NULL, bar->width * 4);
pixman_image_t *background = pixman_image_create_bits(PIXMAN_a8r8g8b8, bar->width, bar->height, NULL, bar->width * 4);
/* Draw on images */
uint32_t x = 0;
uint32_t y = (bar->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 < tags_l; i++) {
const bool active = bar->mtags & 1 << i;
const bool occupied = bar->ctags & 1 << i;
const bool urgent = bar->urg & 1 << i;
if (hide_vacant && !active && !occupied && !urgent)
continue;
pixman_color_t *fg_color = urgent ? &urgent_fg_color : (active ? &active_fg_color : &inactive_fg_color);
pixman_color_t *bg_color = urgent ? &urgent_bg_color : (active ? &active_bg_color : &inactive_bg_color);
if (!hide_vacant && occupied) {
pixman_image_fill_boxes(PIXMAN_OP_SRC, foreground,
fg_color, 1, &(pixman_box32_t){
.x1 = x + boxs, .x2 = x + boxs + boxw,
.y1 = boxs, .y2 = boxs + boxw
});
if ((!bar->sel || !active) && boxw >= 3) {
/* Make box hollow */
pixman_image_fill_boxes(PIXMAN_OP_SRC, foreground,
&(pixman_color_t){ 0 },
1, &(pixman_box32_t){
.x1 = x + boxs + 1, .x2 = x + boxs + boxw - 1,
.y1 = boxs + 1, .y2 = boxs + boxw - 1
});
}
}
x = draw_text(tags[i], x, y, foreground, background, fg_color, bg_color,
bar->width, bar->height, bar->textpadding, NULL, 0);
}
x = draw_text(bar->layout, x, y, foreground, background,
&inactive_fg_color, &inactive_bg_color, bar->width,
bar->height, bar->textpadding, NULL, 0);
uint32_t status_width = TEXT_WIDTH(bar->status.text, bar->width - x, bar->textpadding);
draw_text(bar->status.text, bar->width - status_width, y, foreground,
background, &inactive_fg_color, &inactive_bg_color,
bar->width, bar->height, bar->textpadding,
bar->status.colors, bar->status.colors_l);
uint32_t nx;
if (center_title) {
uint32_t title_width = TEXT_WIDTH(custom_title ? bar->title.text : bar->window_title, bar->width - status_width - x, 0);
nx = MAX(x, MIN((bar->width - title_width) / 2, bar->width - status_width - title_width));
} else {
nx = MIN(x + bar->textpadding, bar->width - status_width);
}
pixman_image_fill_boxes(PIXMAN_OP_SRC, background,
bar->sel ? &active_bg_color : &inactive_bg_color, 1,
&(pixman_box32_t){
.x1 = x, .x2 = nx,
.y1 = 0, .y2 = bar->height
});
x = nx;
x = draw_text(custom_title ? bar->title.text : bar->window_title,
x, y, foreground, background,
bar->sel ? &active_fg_color : &inactive_fg_color,
bar->sel ? &active_bg_color : &inactive_bg_color,
bar->width - status_width, bar->height, 0,
custom_title ? bar->title.colors : NULL,
custom_title ? bar->title.colors_l : 0);
pixman_image_fill_boxes(PIXMAN_OP_SRC, background,
bar->sel ? &active_bg_color : &inactive_bg_color, 1,
&(pixman_box32_t){
.x1 = x, .x2 = bar->width - status_width,
.y1 = 0, .y2 = bar->height
});
/* Draw background and foreground on bar */
pixman_image_composite32(PIXMAN_OP_OVER, background, NULL, final, 0, 0, 0, 0, 0, 0, bar->width, bar->height);
pixman_image_composite32(PIXMAN_OP_OVER, foreground, NULL, final, 0, 0, 0, 0, 0, 0, bar->width, bar->height);
pixman_image_unref(foreground);
pixman_image_unref(background);
pixman_image_unref(final);
munmap(data, bar->bufsize);
wl_surface_set_buffer_scale(bar->wl_surface, buffer_scale);
wl_surface_attach(bar->wl_surface, buffer, 0, 0);
wl_surface_damage_buffer(bar->wl_surface, 0, 0, bar->width, bar->height);
wl_surface_commit(bar->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)
{
w = w * buffer_scale;
h = h * buffer_scale;
zwlr_layer_surface_v1_ack_configure(surface, serial);
Bar *bar = (Bar *)data;
if (bar->configured && w == bar->width && h == bar->height)
return;
bar->width = w;
bar->height = h;
bar->stride = bar->width * 4;
bar->bufsize = bar->stride * bar->height;
bar->configured = true;
draw_frame(bar);
}
static void
layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *surface)
{
run_display = false;
}
static const struct zwlr_layer_surface_v1_listener layer_surface_listener = {
.configure = layer_surface_configure,
.closed = layer_surface_closed,
};
static void
cleanup(void)
{
if (socketpath)
unlink(socketpath);
}
static void
output_name(void *data, struct zxdg_output_v1 *xdg_output, const char *name)
{
Bar *bar = (Bar *)data;
if (bar->xdg_output_name)
free(bar->xdg_output_name);
if (!(bar->xdg_output_name = strdup(name)))
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 const struct zxdg_output_v1_listener output_listener = {
.name = output_name,
.logical_position = output_logical_position,
.logical_size = output_logical_size,
.done = output_done,
.description = output_description
};
static void
shell_command(char *command)
{
if (fork() == 0) {
setsid();
execl("/bin/sh", "sh", "-c", command, NULL);
exit(EXIT_SUCCESS);
}
}
static void
pointer_enter(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface,
wl_fixed_t surface_x, wl_fixed_t surface_y)
{
Seat *seat = (Seat *)data;
seat->bar = NULL;
Bar *bar;
wl_list_for_each(bar, &bar_list, link) {
if (bar->wl_surface == surface) {
seat->bar = bar;
break;
}
}
if (!cursor_image) {
struct wl_cursor_theme *cursor_theme = wl_cursor_theme_load(NULL, 24 * buffer_scale, shm);
cursor_image = wl_cursor_theme_get_cursor(cursor_theme, "left_ptr")->images[0];
cursor_surface = wl_compositor_create_surface(compositor);
wl_surface_set_buffer_scale(cursor_surface, buffer_scale);
wl_surface_attach(cursor_surface, wl_cursor_image_get_buffer(cursor_image), 0, 0);
wl_surface_commit(cursor_surface);
}
wl_pointer_set_cursor(pointer, serial, cursor_surface,
cursor_image->hotspot_x,
cursor_image->hotspot_y);
}
static void
pointer_leave(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface)
{
Seat *seat = (Seat *)data;
seat->bar = NULL;
}
static void
pointer_button(void *data, struct wl_pointer *pointer, uint32_t serial,
uint32_t time, uint32_t button, uint32_t state)
{
Seat *seat = (Seat *)data;
seat->pointer_button = state == WL_POINTER_BUTTON_STATE_PRESSED ? button : 0;
}
static void
pointer_motion(void *data, struct wl_pointer *pointer, uint32_t time,
wl_fixed_t surface_x, wl_fixed_t surface_y)
{
Seat *seat = (Seat *)data;
seat->pointer_x = wl_fixed_to_int(surface_x);
seat->pointer_y = wl_fixed_to_int(surface_y);
}
static void
pointer_frame(void *data, struct wl_pointer *pointer)
{
Seat *seat = (Seat *)data;
if (!seat->pointer_button || !seat->bar)
return;
uint32_t x = 0, i = 0;
do {
if (hide_vacant) {
const bool active = seat->bar->mtags & 1 << i;
const bool occupied = seat->bar->ctags & 1 << i;
const bool urgent = seat->bar->urg & 1 << i;
if (!active && !occupied && !urgent)
continue;
}
x += TEXT_WIDTH(tags[i], seat->bar->width - x, seat->bar->textpadding) / buffer_scale;
} while (seat->pointer_x >= x && ++i < tags_l);
if (i < tags_l) {
/* Clicked on tags */
if (ipc) {
if (seat->pointer_button == BTN_LEFT)
znet_tapesoftware_dwl_wm_monitor_v1_set_tags(seat->bar->dwl_wm_monitor, 1 << i, 1);
else if (seat->pointer_button == BTN_MIDDLE)
znet_tapesoftware_dwl_wm_monitor_v1_set_tags(seat->bar->dwl_wm_monitor, ~0, 1 << i);
else if (seat->pointer_button == BTN_RIGHT)
znet_tapesoftware_dwl_wm_monitor_v1_set_tags(seat->bar->dwl_wm_monitor, 0, 1 << i);
}
} else if (seat->pointer_x < (x += TEXT_WIDTH(seat->bar->layout, seat->bar->width - x, seat->bar->textpadding))) {
/* Clicked on layout */
if (ipc) {
if (seat->pointer_button == BTN_LEFT)
znet_tapesoftware_dwl_wm_monitor_v1_set_layout(seat->bar->dwl_wm_monitor, seat->bar->last_layout_idx);
else if (seat->pointer_button == BTN_RIGHT)
znet_tapesoftware_dwl_wm_monitor_v1_set_layout(seat->bar->dwl_wm_monitor, 2);
}
} else {
uint32_t status_x = seat->bar->width / buffer_scale - TEXT_WIDTH(seat->bar->status.text, seat->bar->width - x, seat->bar->textpadding) / buffer_scale;
if (seat->pointer_x < status_x) {
/* Clicked on title */
if (custom_title) {
if (center_title) {
uint32_t title_width = TEXT_WIDTH(seat->bar->title.text, status_x - x, 0);
x = MAX(x, MIN((seat->bar->width - title_width) / 2, status_x - title_width));
} else {
x = MIN(x + seat->bar->textpadding, status_x);
}
for (i = 0; i < seat->bar->title.buttons_l; i++) {
if (seat->pointer_button == seat->bar->title.buttons[i].btn
&& seat->pointer_x >= x + seat->bar->title.buttons[i].x1
&& seat->pointer_x < x + seat->bar->title.buttons[i].x2) {
shell_command(seat->bar->title.buttons[i].command);
break;
}
}
}
} else {
/* Clicked on status */
for (i = 0; i < seat->bar->status.buttons_l; i++) {
if (seat->pointer_button == seat->bar->status.buttons[i].btn
&& seat->pointer_x >= status_x + seat->bar->textpadding + seat->bar->status.buttons[i].x1 / buffer_scale
&& seat->pointer_x < status_x + seat->bar->textpadding + seat->bar->status.buttons[i].x2 / buffer_scale) {
shell_command(seat->bar->status.buttons[i].command);
break;
}
}
}
}
seat->pointer_button = 0;
}
static void
pointer_axis(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis, wl_fixed_t value)
{
}
static void
pointer_axis_discrete(void *data, struct wl_pointer *pointer,
uint32_t axis, int32_t discrete)
{
}
static void
pointer_axis_source(void *data, struct wl_pointer *pointer,
uint32_t axis_source)
{
}
static void
pointer_axis_stop(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis)
{
}
static void
pointer_axis_value120(void *data, struct wl_pointer *pointer,
uint32_t axis, int32_t discrete)
{
}
static const struct wl_pointer_listener pointer_listener = {
.axis = pointer_axis,
.axis_discrete = pointer_axis_discrete,
.axis_source = pointer_axis_source,
.axis_stop = pointer_axis_stop,
.axis_value120 = pointer_axis_value120,
.button = pointer_button,
.enter = pointer_enter,
.frame = pointer_frame,
.leave = pointer_leave,
.motion = pointer_motion,
};
static void
seat_capabilities(void *data, struct wl_seat *wl_seat,
uint32_t capabilities)
{
Seat *seat = (Seat *)data;
uint32_t has_pointer = capabilities & WL_SEAT_CAPABILITY_POINTER;
if (has_pointer && !seat->wl_pointer) {
seat->wl_pointer = wl_seat_get_pointer(seat->wl_seat);
wl_pointer_add_listener(seat->wl_pointer, &pointer_listener, seat);
} else if (!has_pointer && seat->wl_pointer) {
wl_pointer_destroy(seat->wl_pointer);
seat->wl_pointer = NULL;
}
}
static void
seat_name(void *data, struct wl_seat *wl_seat, const char *name)
{
}
static const struct wl_seat_listener seat_listener = {
.capabilities = seat_capabilities,
.name = seat_name,
};
static void
dwl_wm_tag(void *data, struct znet_tapesoftware_dwl_wm_v1 *dwl_wm, const char *name)
{
char **ptr;
ARRAY_APPEND(tags, tags_l, tags_c, ptr);
if (!(*ptr = strdup(name)))
EDIE("strdup");
}
static void
dwl_wm_layout(void *data, struct znet_tapesoftware_dwl_wm_v1 *dwl_wm, const char *name)
{
char **ptr;
ARRAY_APPEND(layouts, layouts_l, layouts_c, ptr);
if (!(*ptr = strdup(name)))
EDIE("strdup");
}
static const struct znet_tapesoftware_dwl_wm_v1_listener dwl_wm_listener = {
.tag = dwl_wm_tag,
.layout = dwl_wm_layout
};
static void
dwl_wm_monitor_selected(void *data, struct znet_tapesoftware_dwl_wm_monitor_v1 *dwl_wm_monitor,
uint32_t selected)
{
Bar *bar = (Bar *)data;
if (selected != bar->sel) {
bar->sel = selected;
bar->redraw = true;
}
}
static void
dwl_wm_monitor_tag(void *data, struct znet_tapesoftware_dwl_wm_monitor_v1 *dwl_wm_monitor,
uint32_t tag, uint32_t state, uint32_t clients, int32_t focused_client)
{
Bar *bar = (Bar *)data;
uint32_t imtags = bar->mtags;
uint32_t ictags = bar->ctags;
uint32_t iurg = bar->urg;
if (state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_ACTIVE)
bar->mtags |= 1 << tag;
else
bar->mtags &= ~(1 << tag);
if (clients > 0)
bar->ctags |= 1 << tag;
else
bar->ctags &= ~(1 << tag);
if (state & ZNET_TAPESOFTWARE_DWL_WM_MONITOR_V1_TAG_STATE_URGENT)
bar->urg |= 1 << tag;
else
bar->urg &= ~(1 << tag);
if (bar->mtags != imtags || bar->ctags != ictags || bar->urg != iurg)
bar->redraw = true;
}
static void
dwl_wm_monitor_layout(void *data, struct znet_tapesoftware_dwl_wm_monitor_v1 *dwl_wm_monitor,
uint32_t layout)
{
Bar *bar = (Bar *)data;
bar->layout = layouts[layout];
bar->last_layout_idx = bar->layout_idx;
bar->layout_idx = layout;
bar->redraw = true;
}
static void
dwl_wm_monitor_title(void *data, struct znet_tapesoftware_dwl_wm_monitor_v1 *dwl_wm_monitor,
const char *title)
{
if (custom_title)
return;
Bar *bar = (Bar *)data;
if (bar->window_title)
free(bar->window_title);
if (!(bar->window_title = strdup(title)))
EDIE("strdup");
bar->redraw = true;
}
static void
dwl_wm_monitor_frame(void *data, struct znet_tapesoftware_dwl_wm_monitor_v1 *dwl_wm_monitor)
{
}
static const struct znet_tapesoftware_dwl_wm_monitor_v1_listener dwl_wm_monitor_listener = {
.selected = dwl_wm_monitor_selected,
.tag = dwl_wm_monitor_tag,
.layout = dwl_wm_monitor_layout,
.title = dwl_wm_monitor_title,
.frame = dwl_wm_monitor_frame
};
static void
show_bar(Bar *bar)
{
bar->wl_surface = wl_compositor_create_surface(compositor);
if (!bar->wl_surface)
DIE("Could not create wl_surface");
bar->layer_surface = zwlr_layer_shell_v1_get_layer_surface(layer_shell, bar->wl_surface, bar->wl_output,
ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM, PROGRAM);
if (!bar->layer_surface)
DIE("Could not create layer_surface");
zwlr_layer_surface_v1_add_listener(bar->layer_surface, &layer_surface_listener, bar);
zwlr_layer_surface_v1_set_size(bar->layer_surface, 0, bar->height / buffer_scale);
zwlr_layer_surface_v1_set_anchor(bar->layer_surface,
(bar->bottom ? ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM : ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP)
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
zwlr_layer_surface_v1_set_exclusive_zone(bar->layer_surface, bar->height / buffer_scale);
wl_surface_commit(bar->wl_surface);
bar->hidden = false;
}
static void
hide_bar(Bar *bar)
{
zwlr_layer_surface_v1_destroy(bar->layer_surface);
wl_surface_destroy(bar->wl_surface);
bar->configured = false;
bar->hidden = true;
}
static void
setup_bar(Bar *bar)
{
bar->height = height * buffer_scale;
bar->textpadding = textpadding;
bar->bottom = bottom;
bar->hidden = hidden;
bar->xdg_output = zxdg_output_manager_v1_get_xdg_output(output_manager, bar->wl_output);
if (!bar->xdg_output)
DIE("Could not create xdg_output");
zxdg_output_v1_add_listener(bar->xdg_output, &output_listener, bar);
if (ipc) {
bar->dwl_wm_monitor = znet_tapesoftware_dwl_wm_v1_get_monitor(dwl_wm, bar->wl_output);
if (!bar->dwl_wm_monitor)
DIE("Could not create dwl_wm_monitor");
znet_tapesoftware_dwl_wm_monitor_v1_add_listener(bar->dwl_wm_monitor, &dwl_wm_monitor_listener, bar);
}
if (!bar->hidden)
show_bar(bar);
}
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)) {
compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 4);
} else if (!strcmp(interface, wl_shm_interface.name)) {
shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
} else if (!strcmp(interface, zwlr_layer_shell_v1_interface.name)) {
layer_shell = wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 1);
} else if (!strcmp(interface, zxdg_output_manager_v1_interface.name)) {
output_manager = wl_registry_bind(registry, name, &zxdg_output_manager_v1_interface, 2);
} else if (!strcmp(interface, znet_tapesoftware_dwl_wm_v1_interface.name)) {
if (ipc) {
dwl_wm = wl_registry_bind(registry, name, &znet_tapesoftware_dwl_wm_v1_interface, 1);
znet_tapesoftware_dwl_wm_v1_add_listener(dwl_wm, &dwl_wm_listener, NULL);
}
} else if (!strcmp(interface, wl_output_interface.name)) {
Bar *bar = calloc(1, sizeof(Bar));
if (!bar)
EDIE("calloc");
bar->registry_name = name;
bar->wl_output = wl_registry_bind(registry, name, &wl_output_interface, 1);
if (run_display)
setup_bar(bar);
wl_list_insert(&bar_list, &bar->link);
} else if (!strcmp(interface, wl_seat_interface.name)) {
Seat *seat = calloc(1, sizeof(Seat));
if (!seat)
EDIE("calloc");
seat->registry_name = name;
seat->wl_seat = wl_registry_bind(registry, name, &wl_seat_interface, 7);
wl_seat_add_listener(seat->wl_seat, &seat_listener, seat);
wl_list_insert(&seat_list, &seat->link);
}
}
static void
teardown_bar(Bar *bar)
{
if (bar->status.colors)
free(bar->status.colors);
if (bar->status.buttons)
free(bar->status.buttons);
if (bar->title.colors)
free(bar->title.colors);
if (bar->title.buttons)
free(bar->title.buttons);
if (!ipc && bar->layout)
free(bar->layout);
if (ipc)
znet_tapesoftware_dwl_wm_monitor_v1_destroy(bar->dwl_wm_monitor);
if (bar->xdg_output_name)
free(bar->xdg_output_name);
if (!bar->hidden) {
zwlr_layer_surface_v1_destroy(bar->layer_surface);
wl_surface_destroy(bar->wl_surface);
}
zxdg_output_v1_destroy(bar->xdg_output);
wl_output_destroy(bar->wl_output);
free(bar);
}
static void
teardown_seat(Seat *seat)
{
if (seat->wl_pointer)
wl_pointer_destroy(seat->wl_pointer);
wl_seat_destroy(seat->wl_seat);