-
Notifications
You must be signed in to change notification settings - Fork 23
/
dwlb.c
1989 lines (1761 loc) · 54.7 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 "dwl-ipc-unstable-v2-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" \
" -active-color-title title colors will use active colors\n" \
" -no-active-color-title title colors will use inactive colors\n" \
" -font [FONT] specify a font\n" \
" -tags [NUMBER] [FIRST]...[LAST] if ipc is disabled, specify custom tag names. If NUMBER is 0, then no tag names should be given \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" \
" -occupied-fg-color [COLOR] specify text color of occupied tags\n" \
" -occupied-bg-color [COLOR] specify background color of occupied tags\n" \
" -inactive-fg-color [COLOR] specify text color of inactive tags or monitors\n" \
" -inactive-bg-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" \
" -middle-bg-color [COLOR] specify background color of the color in the middle of the bar\n" \
" -middle-bg-color-selected [COLOR] specify background color of the color in the middle of the bar, when selected\n" \
" -scale [BUFFER_SCALE] specify buffer scale value for integer scaling\n" \
"Commands\n" \
" -target-socket [SOCKET-NAME] set the socket to send command to. Sockets can be found in `$XDG_RUNTIME_DIR/dwlb/`\n"\
" -status [OUTPUT] [TEXT] set status text\n" \
" -status-stdin [OUTPUT] set status text from stdin\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"
#define TEXT_MAX 2048
enum { WheelUp, WheelDown };
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[TEXT_MAX];
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 zdwl_ipc_output_v2 *dwl_wm_output;
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 zdwl_ipc_manager_v2 *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 : (occupied ? &occupied_fg_color : &inactive_fg_color));
pixman_color_t *bg_color = urgent ? &urgent_bg_color : (active ? &active_bg_color : (occupied ? &occupied_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 ? &middle_bg_color_selected : &middle_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_color_title) ? &active_fg_color : &inactive_fg_color,
(bar->sel && active_color_title) ? &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 ? &middle_bg_color_selected : &middle_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)
{
}
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) {
const char *size_str = getenv("XCURSOR_SIZE");
int size = size_str ? atoi(size_str) : 0;
if (size == 0)
size = 24;
struct wl_cursor_theme *cursor_theme = wl_cursor_theme_load(getenv("XCURSOR_THEME"), size * 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)
zdwl_ipc_output_v2_set_tags(seat->bar->dwl_wm_output, 1 << i, 1);
else if (seat->pointer_button == BTN_MIDDLE)
zdwl_ipc_output_v2_set_tags(seat->bar->dwl_wm_output, ~0, 1);
else if (seat->pointer_button == BTN_RIGHT)
zdwl_ipc_output_v2_set_tags(seat->bar->dwl_wm_output, seat->bar->mtags ^ (1 << i), 0);
}
} 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)
zdwl_ipc_output_v2_set_layout(seat->bar->dwl_wm_output, seat->bar->last_layout_idx);
else if (seat->pointer_button == BTN_RIGHT)
zdwl_ipc_output_v2_set_layout(seat->bar->dwl_wm_output, 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)
{
uint32_t i;
uint32_t btn = discrete < 0 ? WheelUp : WheelDown;
Seat *seat = (Seat *)data;
if (!seat->bar)
return;
uint32_t status_x = seat->bar->width / buffer_scale - TEXT_WIDTH(seat->bar->status.text, seat->bar->width, seat->bar->textpadding) / buffer_scale;
if (seat->pointer_x > status_x) {
/* Clicked on status */
for (i = 0; i < seat->bar->status.buttons_l; i++) {
if (btn == 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;
}
}
}
}
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
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
dwl_wm_tags(void *data, struct zdwl_ipc_manager_v2 *dwl_wm,
uint32_t amount)
{
if (!tags && !(tags = malloc(amount * sizeof(char *))))
EDIE("malloc");
uint32_t i = tags_l;
ARRAY_EXPAND(tags, tags_l, tags_c, MAX(0, (int)amount - (int)tags_l));
for (; i < amount; i++)
if (!(tags[i] = strdup(tags_names[MIN(i, LENGTH(tags_names)-1)])))
EDIE("strdup");
}
static void
dwl_wm_layout(void *data, struct zdwl_ipc_manager_v2 *dwl_wm,
const char *name)
{
char **ptr;
ARRAY_APPEND(layouts, layouts_l, layouts_c, ptr);
if (!(*ptr = strdup(name)))
EDIE("strdup");
}
static const struct zdwl_ipc_manager_v2_listener dwl_wm_listener = {
.tags = dwl_wm_tags,
.layout = dwl_wm_layout
};
static void
dwl_wm_output_toggle_visibility(void *data, struct zdwl_ipc_output_v2 *dwl_wm_output)
{
Bar *bar = (Bar *)data;
if (bar->hidden)
show_bar(bar);
else
hide_bar(bar);
}
static void
dwl_wm_output_active(void *data, struct zdwl_ipc_output_v2 *dwl_wm_output,
uint32_t active)
{
Bar *bar = (Bar *)data;
if (active != bar->sel)
bar->sel = active;
}
static void
dwl_wm_output_tag(void *data, struct zdwl_ipc_output_v2 *dwl_wm_output,
uint32_t tag, uint32_t state, uint32_t clients, uint32_t focused)
{
Bar *bar = (Bar *)data;
if (state & ZDWL_IPC_OUTPUT_V2_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 & ZDWL_IPC_OUTPUT_V2_TAG_STATE_URGENT)
bar->urg |= 1 << tag;
else
bar->urg &= ~(1 << tag);
}
static void
dwl_wm_output_layout(void *data, struct zdwl_ipc_output_v2 *dwl_wm_output,
uint32_t layout)
{
Bar *bar = (Bar *)data;
bar->last_layout_idx = bar->layout_idx;
bar->layout_idx = layout;
}
static void
dwl_wm_output_title(void *data, struct zdwl_ipc_output_v2 *dwl_wm_output,
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");
}
static void
dwl_wm_output_appid(void *data, struct zdwl_ipc_output_v2 *dwl_wm_output,
const char *appid)
{
}
static void
dwl_wm_output_layout_symbol(void *data, struct zdwl_ipc_output_v2 *dwl_wm_output,
const char *layout)
{
Bar *bar = (Bar *)data;
if (layouts[bar->layout_idx])
free(layouts[bar->layout_idx]);
if (!(layouts[bar->layout_idx] = strdup(layout)))
EDIE("strdup");
bar->layout = layouts[bar->layout_idx];
}
static void
dwl_wm_output_frame(void *data, struct zdwl_ipc_output_v2 *dwl_wm_output)
{
Bar *bar = (Bar *)data;
bar->redraw = true;
}
static void
dwl_wm_output_fullscreen(void *data, struct zdwl_ipc_output_v2 *dwl_wm_output,
uint32_t is_fullscreen)
{
}
static void
dwl_wm_output_floating(void *data, struct zdwl_ipc_output_v2 *dwl_wm_output,
uint32_t is_floating)
{
}
static const struct zdwl_ipc_output_v2_listener dwl_wm_output_listener = {
.toggle_visibility = dwl_wm_output_toggle_visibility,
.active = dwl_wm_output_active,
.tag = dwl_wm_output_tag,
.layout = dwl_wm_output_layout,
.title = dwl_wm_output_title,
.appid = dwl_wm_output_appid,
.layout_symbol = dwl_wm_output_layout_symbol,
.frame = dwl_wm_output_frame,
.fullscreen = dwl_wm_output_fullscreen,
.floating = dwl_wm_output_floating
};
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_output = zdwl_ipc_manager_v2_get_output(dwl_wm, bar->wl_output);