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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
|
#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 *foreground_mask,
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_mask_fill;
pixman_color_t *cur_fg_color;
pixman_color_t *cur_bg_color;
if (draw_fg) {
cur_fg_color = fg_color;
fg_mask_fill= pixman_image_create_solid_fill(&(pixman_color_t){0xFFFF,0xFFFF,0xFFFF,0xFFFF});
}
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) {
cur_fg_color = &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) {
pixman_image_composite32(
PIXMAN_OP_OVER, glyph->pix, NULL, foreground, 0, 0, 0, 0,
x + glyph->x, y - glyph->y, glyph->width, glyph->height);
} else {
pixman_image_fill_boxes(PIXMAN_OP_OVER, foreground,
cur_fg_color, 1, &(pixman_box32_t){
.x1 = x, .x2 = nx,
.y1 = 0, .y2 = buf_height
});
}
pixman_image_composite32(
PIXMAN_OP_OVER, glyph->pix, fg_mask_fill, foreground_mask, 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_mask_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, 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 *foreground_mask = pixman_image_create_bits(PIXMAN_a8, 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
});
pixman_image_fill_boxes(PIXMAN_OP_SRC, foreground_mask,
&(pixman_color_t){0xFFFF,0xFFFF,0xFFFF,0xFFFF},
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
});
pixman_image_fill_boxes(PIXMAN_OP_SRC, foreground_mask,
&(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, foreground_mask, background, fg_color, bg_color,
bar->width, bar->height, bar->textpadding, NULL, 0);
}
x = draw_text(bar->layout, x, y, foreground, foreground_mask, 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, foreground_mask,
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, foreground_mask, 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_set_alpha_map(foreground, foreground_mask, 0, 0);
pixman_image_composite32(PIXMAN_OP_OVER, foreground, foreground_mask, final, 0, 0, 0, 0, 0, 0, bar->width, bar->height);
pixman_image_unref(foreground);
pixman_image_unref(foreground_mask);
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);
if (!bar->dwl_wm_output)
DIE("Could not create dwl_wm_output");
zdwl_ipc_output_v2_add_listener(bar->dwl_wm_output, &dwl_wm_output_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, zdwl_ipc_manager_v2_interface.name)) {
if (ipc) {
dwl_wm = wl_registry_bind(registry, name, &zdwl_ipc_manager_v2_interface, 2);
zdwl_ipc_manager_v2_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 (bar->window_title)
free(bar->window_title);
if (!ipc && bar->layout)
free(bar->layout);
if (ipc)
zdwl_ipc_output_v2_destroy(bar->dwl_wm_output);
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);
free(seat);
}
static void
handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
{
Bar *bar;
Seat *seat;
wl_list_for_each(bar, &bar_list, link) {
if (bar->registry_name == name) {
wl_list_remove(&bar->link);
teardown_bar(bar);
return;
}
}
wl_list_for_each(seat, &seat_list, link) {
if (seat->registry_name == name) {
wl_list_remove(&seat->link);
teardown_seat(seat);
return;
}
}
}
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_RET() if (ADVANCE() == -1) return
static void
read_stdin(void)
{
size_t len = 0;
for (;;) {
ssize_t rv = read(STDIN_FILENO, stdinbuf + len, stdinbuf_cap - len);
if (rv == -1) {
if (errno == EWOULDBLOCK)
break;
EDIE("read");
}
if (rv == 0) {
run_display = false;
return;
}
if ((len += rv) > stdinbuf_cap / 2)
if (!(stdinbuf = realloc(stdinbuf, (stdinbuf_cap *= 2))))
EDIE("realloc");
}
char *linebeg, *lineend;
char *wordbeg, *wordend;
for (linebeg = stdinbuf;
(lineend = memchr(linebeg, '\n', stdinbuf + len - linebeg));
linebeg = lineend) {
*lineend++ = '\0';
wordend = linebeg;
ADVANCE_IF_LAST_CONT();
Bar *it, *bar = NULL;
wl_list_for_each(it, &bar_list, link) {
if (it->xdg_output_name && !strcmp(wordbeg, it->xdg_output_name)) {
bar = it;
break;
}
}
if (!bar)
continue;
ADVANCE_IF_LAST_CONT();
uint32_t val;
if (!strcmp(wordbeg, "tags")) {
ADVANCE_IF_LAST_CONT();
if ((val = atoi(wordbeg)) != bar->ctags) {
bar->ctags = val;
bar->redraw = true;
}
ADVANCE_IF_LAST_CONT();
if ((val = atoi(wordbeg)) != bar->mtags) {
bar->mtags = val;
bar->redraw = true;
}
ADVANCE_IF_LAST_CONT();
/* skip sel */
ADVANCE();
if ((val = atoi(wordbeg)) != bar->urg) {
bar->urg = val;
bar->redraw = true;
}
} else if (!strcmp(wordbeg, "layout")) {
if (bar->layout)
free(bar->layout);
if (!(bar->layout = strdup(wordend)))
EDIE("strdup");
bar->redraw = true;
} else if (!strcmp(wordbeg, "title")) {
if (custom_title)
continue;
if (bar->window_title)
free(bar->window_title);
if (!(bar->window_title = strdup(wordend)))
EDIE("strdup");
bar->redraw = true;
} else if (!strcmp(wordbeg, "selmon")) {
ADVANCE();
if ((val = atoi(wordbeg)) != bar->sel) {
bar->sel = val;
bar->redraw = true;
}
}
}
}
static void
set_top(Bar *bar)
{
if (!bar->hidden) {
zwlr_layer_surface_v1_set_anchor(bar->layer_surface,
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
bar->redraw = true;
}
bar->bottom = false;
}
static void
set_bottom(Bar *bar)
{
if (!bar->hidden) {
zwlr_layer_surface_v1_set_anchor(bar->layer_surface,
ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
bar->redraw = true;
}
bar->bottom = true;
}
/* 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;
}
static void
parse_into_customtext(CustomText *ct, char *text)
{
ct->colors_l = ct->buttons_l = 0;
if (status_commands) {
uint32_t codepoint;
uint32_t state = UTF8_ACCEPT;
uint32_t last_cp = 0;
uint32_t x = 0;
size_t str_pos = 0;
Button *left_button = NULL;
Button *middle_button = NULL;
Button *right_button = NULL;
Button *scrollup_button = NULL;
Button *scrolldown_button = NULL;
for (char *p = text; *p && str_pos < sizeof(ct->text) - 1; p++) {
if (state == UTF8_ACCEPT && *p == '^') {
p++;
if (*p != '^') {
char *arg, *end;
if (!(arg = strchr(p, '(')) || !(end = strchr(arg + 1, ')')))
continue;
*arg++ = '\0';
*end = '\0';
if (!strcmp(p, "bg")) {
Color *color;
ARRAY_APPEND(ct->colors, ct->colors_l, ct->colors_c, color);
if (!*arg)
color->color = inactive_bg_color;
else
parse_color(arg, &color->color);
color->bg = true;
color->start = ct->text + str_pos;
} else if (!strcmp(p, "fg")) {
Color *color;
ARRAY_APPEND(ct->colors, ct->colors_l, ct->colors_c, color);
if (!*arg)
color->color = inactive_fg_color;
else
parse_color(arg, &color->color);
color->bg = false;
color->start = ct->text + str_pos;
} else if (!strcmp(p, "lm")) {
if (left_button) {
left_button->x2 = x;
left_button = NULL;
} else if (*arg) {
ARRAY_APPEND(ct->buttons, ct->buttons_l, ct->buttons_c, left_button);
left_button->btn = BTN_LEFT;
snprintf(left_button->command, sizeof left_button->command, "%s", arg);
left_button->x1 = x;
}
} else if (!strcmp(p, "mm")) {
if (middle_button) {
middle_button->x2 = x;
middle_button = NULL;
} else if (*arg) {
ARRAY_APPEND(ct->buttons, ct->buttons_l, ct->buttons_c, middle_button);
middle_button->btn = BTN_MIDDLE;
snprintf(middle_button->command, sizeof middle_button->command, "%s", arg);
middle_button->x1 = x;
}
} else if (!strcmp(p, "rm")) {
if (right_button) {
right_button->x2 = x;
right_button = NULL;
} else if (*arg) {
ARRAY_APPEND(ct->buttons, ct->buttons_l, ct->buttons_c, right_button);
right_button->btn = BTN_RIGHT;
snprintf(right_button->command, sizeof right_button->command, "%s", arg);
right_button->x1 = x;
}
} else if (!strcmp(p, "us")) {
if (scrollup_button) {
scrollup_button->x2 = x;
scrollup_button = NULL;
} else if (*arg) {
ARRAY_APPEND(ct->buttons, ct->buttons_l, ct->buttons_c, scrollup_button);
scrollup_button->btn = WheelUp;
snprintf(scrollup_button->command, sizeof scrollup_button->command, "%s", arg);
scrollup_button->x1 = x;
}
} else if (!strcmp(p, "ds")) {
if (scrolldown_button) {
scrolldown_button->x2 = x;
scrolldown_button = NULL;
} else if (*arg) {
ARRAY_APPEND(ct->buttons, ct->buttons_l, ct->buttons_c, scrolldown_button);
scrolldown_button->btn = WheelDown;
snprintf(scrolldown_button->command, sizeof scrolldown_button->command, "%s", arg);
scrolldown_button->x1 = x;
}
}
*--arg = '(';
*end = ')';
p = end;
continue;
}
}
ct->text[str_pos++] = *p;
if (utf8decode(&state, &codepoint, *p))
continue;
const struct fcft_glyph *glyph = fcft_rasterize_char_utf32(font, codepoint, FCFT_SUBPIXEL_NONE);
if (!glyph)
continue;
long kern = 0;
if (last_cp)
fcft_kerning(font, last_cp, codepoint, &kern, NULL);
last_cp = codepoint;
x += kern + glyph->advance.x;
}
if (left_button)
left_button->x2 = x;
if (middle_button)
middle_button->x2 = x;
if (right_button)
right_button->x2 = x;
if (scrollup_button)
scrollup_button->x2 = x;
if (scrolldown_button)
scrolldown_button->x2 = x;
ct->text[str_pos] = '\0';
} else {
snprintf(ct->text, sizeof ct->text, "%s", text);
}
}
static void
copy_customtext(CustomText *from, CustomText *to)
{
snprintf(to->text, sizeof to->text, "%s", from->text);
to->colors_l = to->buttons_l = 0;
for (uint32_t i = 0; i < from->colors_l; i++) {
Color *color;
ARRAY_APPEND(to->colors, to->colors_l, to->colors_c, color);
color->color = from->colors[i].color;
color->bg = from->colors[i].bg;
color->start = from->colors[i].start - (char *)&from->text + (char *)&to->text;
}
for (uint32_t i = 0; i < from->buttons_l; i++) {
Button *button;
ARRAY_APPEND(to->buttons, to->buttons_l, to->buttons_c, button);
*button = from->buttons[i];
}
}
static void
read_socket(void)
{
int cli_fd;
if ((cli_fd = accept(sock_fd, NULL, 0)) == -1)
EDIE("accept");
ssize_t len = recv(cli_fd, sockbuf, sizeof sockbuf - 1, 0);
if (len == -1)
EDIE("recv");
close(cli_fd);
if (len == 0)
return;
sockbuf[len] = '\0';
char *wordbeg, *wordend;
wordend = (char *)&sockbuf;
ADVANCE_IF_LAST_RET();
Bar *bar = NULL, *it;
bool all = false;
if (!strcmp(wordbeg, "all")) {
all = true;
} else if (!strcmp(wordbeg, "selected")) {
wl_list_for_each(it, &bar_list, link) {
if (it->sel) {
bar = it;
break;
}
}
} else {
wl_list_for_each(it, &bar_list, link) {
if (it->xdg_output_name && !strcmp(wordbeg, it->xdg_output_name)) {
bar = it;
break;
}
}
}
if (!all && !bar)
return;
ADVANCE();
if (!strcmp(wordbeg, "status")) {
if (!*wordend)
return;
if (all) {
Bar *first = NULL;
wl_list_for_each(bar, &bar_list, link) {
if (first) {
copy_customtext(&first->status, &bar->status);
} else {
parse_into_customtext(&bar->status, wordend);
first = bar;
}
bar->redraw = true;
}
} else {
parse_into_customtext(&bar->status, wordend);
bar->redraw = true;
}
} else if (!strcmp(wordbeg, "title")) {
if (!custom_title || !*wordend)
return;
if (all) {
Bar *first = NULL;
wl_list_for_each(bar, &bar_list, link) {
if (first) {
copy_customtext(&first->title, &bar->title);
} else {
parse_into_customtext(&bar->title, wordend);
first = bar;
}
bar->redraw = true;
}
} else {
parse_into_customtext(&bar->title, wordend);
bar->redraw = true;
}
} else if (!strcmp(wordbeg, "show")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (bar->hidden)
show_bar(bar);
} else {
if (bar->hidden)
show_bar(bar);
}
} else if (!strcmp(wordbeg, "hide")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (!bar->hidden)
hide_bar(bar);
} else {
if (!bar->hidden)
hide_bar(bar);
}
} else if (!strcmp(wordbeg, "toggle-visibility")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (bar->hidden)
show_bar(bar);
else
hide_bar(bar);
} else {
if (bar->hidden)
show_bar(bar);
else
hide_bar(bar);
}
} else if (!strcmp(wordbeg, "set-top")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (bar->bottom)
set_top(bar);
} else {
if (bar->bottom)
set_top(bar);
}
} else if (!strcmp(wordbeg, "set-bottom")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (!bar->bottom)
set_bottom(bar);
} else {
if (!bar->bottom)
set_bottom(bar);
}
} else if (!strcmp(wordbeg, "toggle-location")) {
if (all) {
wl_list_for_each(bar, &bar_list, link)
if (bar->bottom)
set_top(bar);
else
set_bottom(bar);
} else {
if (bar->bottom)
set_top(bar);
else
set_bottom(bar);
}
}
}
static void
event_loop(void)
{
int wl_fd = wl_display_get_fd(display);
while (run_display) {
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(wl_fd, &rfds);
FD_SET(sock_fd, &rfds);
if (!ipc)
FD_SET(STDIN_FILENO, &rfds);
wl_display_flush(display);
if (select(MAX(sock_fd, wl_fd) + 1, &rfds, NULL, NULL, NULL) == -1) {
if (errno == EINTR)
continue;
else
EDIE("select");
}
if (FD_ISSET(wl_fd, &rfds))
if (wl_display_dispatch(display) == -1)
break;
if (FD_ISSET(sock_fd, &rfds))
read_socket();
if (!ipc && FD_ISSET(STDIN_FILENO, &rfds))
read_stdin();
Bar *bar;
wl_list_for_each(bar, &bar_list, link) {
if (bar->redraw) {
if (!bar->hidden)
draw_frame(bar);
bar->redraw = false;
}
}
}
}
static void
client_send_command(struct sockaddr_un *sock_address, const char *output,
const char *cmd, const char *data, const char *target_socket)
{
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;
/* Send data to all dwlb instances */
while ((de = readdir(dir))) {
if (!strncmp(de->d_name, "dwlb-", 5)) {
if (!target_socket || !strncmp(de -> d_name, target_socket, 6)){
if (newfd && (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);
}
void
sig_handler(int sig)
{
if (sig == SIGINT || sig == SIGHUP || sig == SIGTERM)
run_display = false;
}
int
main(int argc, char **argv)
{
char *xdgruntimedir;
struct sockaddr_un sock_address;
Bar *bar, *bar2;
Seat *seat, *seat2;
/* 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 */
char *target_socket = NULL;
int i = 1;
if (argc > 1 && !strcmp(argv[1], "-target-socket")) {
if (2 >= argc) {
DIE("Option -socket requires an argument");
}
target_socket = argv[2];
i += 2;
}
for (; 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], target_socket);
return 0;
} else if (!strcmp(argv[i], "-status-stdin")) {
if (++i >= argc)
DIE("Option -status-stdin requires an argument");
char *status = malloc(TEXT_MAX * sizeof(char));
while (fgets(status, TEXT_MAX-1, stdin)) {
status[strlen(status)-1] = '\0';
client_send_command(&sock_address, argv[i], "status", status, target_socket);
}
free(status);
return 0;
} else if (!strcmp(argv[i], "-title")) {
if (++i + 1 >= argc)
DIE("Option -title requires two arguments");
client_send_command(&sock_address, argv[i], "title", argv[i + 1], target_socket);
return 0;
} else if (!strcmp(argv[i], "-show")) {
if (++i >= argc)
DIE("Option -show requires an argument");
client_send_command(&sock_address, argv[i], "show", NULL, target_socket);
return 0;
} else if (!strcmp(argv[i], "-hide")) {
if (++i >= argc)
DIE("Option -hide requires an argument");
client_send_command(&sock_address, argv[i], "hide", NULL, target_socket);
return 0;
} else if (!strcmp(argv[i], "-toggle-visibility")) {
if (++i >= argc)
DIE("Option -toggle requires an argument");
client_send_command(&sock_address, argv[i], "toggle-visibility", NULL, target_socket);
return 0;
} else if (!strcmp(argv[i], "-set-top")) {
if (++i >= argc)
DIE("Option -set-top requires an argument");
client_send_command(&sock_address, argv[i], "set-top", NULL, target_socket);
return 0;
} else if (!strcmp(argv[i], "-set-bottom")) {
if (++i >= argc)
DIE("Option -set-bottom requires an argument");
client_send_command(&sock_address, argv[i], "set-bottom", NULL, target_socket);
return 0;
} else if (!strcmp(argv[i], "-toggle-location")) {
if (++i >= argc)
DIE("Option -toggle-location requires an argument");
client_send_command(&sock_address, argv[i], "toggle-location", NULL, target_socket);
return 0;
} else if (!strcmp(argv[i], "-ipc")) {
ipc = true;
} else if (!strcmp(argv[i], "-no-ipc")) {
ipc = false;
} else if (!strcmp(argv[i], "-hide-vacant-tags")) {
hide_vacant = true;
} else if (!strcmp(argv[i], "-no-hide-vacant-tags")) {
hide_vacant = false;
} else if (!strcmp(argv[i], "-bottom")) {
bottom = true;
} else if (!strcmp(argv[i], "-no-bottom")) {
bottom = false;
} else if (!strcmp(argv[i], "-hidden")) {
hidden = true;
} else if (!strcmp(argv[i], "-no-hidden")) {
hidden = false;
} else if (!strcmp(argv[i], "-status-commands")) {
status_commands = true;
} else if (!strcmp(argv[i], "-no-status-commands")) {
status_commands = false;
} else if (!strcmp(argv[i], "-center-title")) {
center_title = true;
} else if (!strcmp(argv[i], "-no-center-title")) {
center_title = false;
} else if (!strcmp(argv[i], "-custom-title")) {
custom_title = true;
} else if (!strcmp(argv[i], "-no-custom-title")) {
custom_title = false;
} else if (!strcmp(argv[i], "-active-color-title")) {
active_color_title = true;
} else if (!strcmp(argv[i], "-no-active-color-title")) {
active_color_title = false;
} else if (!strcmp(argv[i], "-font")) {
if (++i >= argc)
DIE("Option -font requires an argument");
fontstr = argv[i];
} else if (!strcmp(argv[i], "-vertical-padding")) {
if (++i >= argc)
DIE("Option -vertical-padding requires an argument");
vertical_padding = MAX(MIN(atoi(argv[i]), 100), 0);
} else if (!strcmp(argv[i], "-active-fg-color")) {
if (++i >= argc)
DIE("Option -active-fg-color requires an argument");
if (parse_color(argv[i], &active_fg_color) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-active-bg-color")) {
if (++i >= argc)
DIE("Option -active-bg-color requires an argument");
if (parse_color(argv[i], &active_bg_color) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-occupied-fg-color")) {
if (++i >= argc)
DIE("Option -occupied-fg-color requires an argument");
if (parse_color(argv[i], &occupied_fg_color) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-occupied-bg-color")) {
if (++i >= argc)
DIE("Option -occupied-bg-color requires an argument");
if (parse_color(argv[i], &occupied_bg_color) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-inactive-fg-color")) {
if (++i >= argc)
DIE("Option -inactive-fg-color requires an argument");
if (parse_color(argv[i], &inactive_fg_color) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-inactive-bg-color")) {
if (++i >= argc)
DIE("Option -inactive-bg-color requires an argument");
if (parse_color(argv[i], &inactive_bg_color) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-urgent-fg-color")) {
if (++i >= argc)
DIE("Option -urgent-fg-color requires an argument");
if (parse_color(argv[i], &urgent_fg_color) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-urgent-bg-color")) {
if (++i >= argc)
DIE("Option -urgent-bg-color requires an argument");
if (parse_color(argv[i], &urgent_bg_color) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-middle-bg-color-selected")) {
if (++i >= argc)
DIE("Option -middle-bg-color-selected requires an argument");
if (parse_color(argv[i], &middle_bg_color_selected) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-middle-bg-color")) {
if (++i >= argc)
DIE("Option -middle-bg-color requires an argument");
if (parse_color(argv[i], &middle_bg_color) == -1)
DIE("malformed color string");
} else if (!strcmp(argv[i], "-tags")) {
if (++i >= argc)
DIE("Option -tags requires at least one argument");
int v;
if ((v = atoi(argv[i])) < 0 || i + v >= argc)
DIE("-tags: invalid arguments");
if (tags) {
for (uint32_t j = 0; j < tags_l; j++)
free(tags[j]);
free(tags);
}
if (!(tags = malloc(v * sizeof(char *))))
EDIE("malloc");
for (int j = 0; j < v; j++)
if (!(tags[j] = strdup(argv[i + 1 + j])))
EDIE("strdup");
tags_l = tags_c = v;
i += v;
} else if (!strcmp(argv[i], "-scale")) {
if (++i >= argc)
DIE("Option -scale requires an argument");
buffer_scale = strtoul(argv[i], &argv[i] + strlen(argv[i]), 10);
} 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");
wl_list_init(&bar_list);
wl_list_init(&seat_list);
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 || (ipc && !dwl_wm))
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);
unsigned int dpi = 96 * buffer_scale;
char buf[10];
snprintf(buf, sizeof buf, "dpi=%u", dpi);
if (!(font = fcft_from_name(1, (const char *[]) {fontstr}, buf)))
DIE("Could not load font");
textpadding = font->height / 2;
height = font->height / buffer_scale + vertical_padding * 2;
/* Configure tag names */
if (!ipc && !tags) {
if (!(tags = malloc(LENGTH(tags_names) * sizeof(char *))))
EDIE("malloc");
tags_l = tags_c = LENGTH(tags_names);
for (uint32_t i = 0; i < tags_l; i++)
if (!(tags[i] = strdup(tags_names[i])))
EDIE("strdup");
}
/* Setup bars */
wl_list_for_each(bar, &bar_list, link)
setup_bar(bar);
wl_display_roundtrip(display);
if (!ipc) {
/* Configure stdin */
if (fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK) == -1)
EDIE("fcntl");
/* Allocate stdin buffer */
if (!(stdinbuf = malloc(1024)))
EDIE("malloc");
stdinbuf_cap = 1024;
}
/* Set up socket */
bool found = false;
for (uint32_t i = 0; i < 50; 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) {
found = true;
break;
}
close(sock_fd);
}
if (!found)
DIE("Could not secure a socket path");
socketpath = (char *)&sock_address.sun_path;
unlink(socketpath);
if (bind(sock_fd, (struct sockaddr *)&sock_address, sizeof sock_address) == -1)
EDIE("bind");
if (listen(sock_fd, SOMAXCONN) == -1)
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(SIGCHLD, SIG_IGN);
/* Run */
run_display = true;
event_loop();
/* Clean everything up */
close(sock_fd);
unlink(socketpath);
if (!ipc)
free(stdinbuf);
if (tags) {
for (uint32_t i = 0; i < tags_l; i++)
free(tags[i]);
free(tags);
}
if (layouts) {
for (uint32_t i = 0; i < layouts_l; i++)
free(layouts[i]);
free(layouts);
}
wl_list_for_each_safe(bar, bar2, &bar_list, link)
teardown_bar(bar);
wl_list_for_each_safe(seat, seat2, &seat_list, link)
teardown_seat(seat);
zwlr_layer_shell_v1_destroy(layer_shell);
zxdg_output_manager_v1_destroy(output_manager);
if (ipc)
zdwl_ipc_manager_v2_destroy(dwl_wm);
fcft_destroy(font);
fcft_fini();
wl_shm_destroy(shm);
wl_compositor_destroy(compositor);
wl_registry_destroy(registry);
wl_display_disconnect(display);
return 0;
}
|