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
|
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <locale.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <threads.h>
#include <unistd.h>
#include <wayland-client.h>
#include <wayland-util.h>
#include <xkbcommon/xkbcommon.h>
#include "tofi.h"
#include "compgen.h"
#include "drun.h"
#include "config.h"
#include "entry.h"
#include "input.h"
#include "log.h"
#include "nelem.h"
#include "lock.h"
#include "scale.h"
#include "shm.h"
#include "string_vec.h"
#include "string_vec.h"
#include "unicode.h"
#include "viewporter.h"
#include "xmalloc.h"
#undef MAX
#undef MIN
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
static const char *mime_type_text_plain = "text/plain";
static const char *mime_type_text_plain_utf8 = "text/plain;charset=utf-8";
static uint32_t gettime_ms() {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
uint32_t ms = t.tv_sec * 1000;
ms += t.tv_nsec / 1000000;
return ms;
}
/* Read all of stdin into a buffer. */
static char *read_stdin(bool normalize) {
const size_t block_size = BUFSIZ;
size_t num_blocks = 1;
size_t buf_size = block_size;
char *buf = xmalloc(buf_size);
for (size_t block = 0; ; block++) {
if (block == num_blocks) {
num_blocks *= 2;
buf = xrealloc(buf, num_blocks * block_size);
}
size_t bytes_read = fread(
&buf[block * block_size],
1,
block_size,
stdin);
if (bytes_read != block_size) {
if (!feof(stdin) && ferror(stdin)) {
log_error("Error reading stdin.\n");
}
buf[block * block_size + bytes_read] = '\0';
break;
}
}
if (normalize) {
if (utf8_validate(buf)) {
char *tmp = utf8_normalize(buf);
free(buf);
buf = tmp;
} else {
log_error("Invalid UTF-8 in stdin.\n");
}
}
return buf;
}
static void zwlr_layer_surface_configure(
void *data,
struct zwlr_layer_surface_v1 *zwlr_layer_surface,
uint32_t serial,
uint32_t width,
uint32_t height)
{
struct tofi *tofi = data;
if (width == 0 || height == 0) {
/* Compositor is deferring to us, so don't do anything. */
log_debug("Layer surface configure with no width or height.\n");
return;
}
log_debug("Layer surface configure, %u x %u.\n", width, height);
/*
* Resize the main window.
* We want actual pixel width / height, so we have to scale the
* values provided by Wayland.
*/
if (tofi->window.fractional_scale != 0) {
tofi->window.surface.width = scale_apply(width, tofi->window.fractional_scale);
tofi->window.surface.height = scale_apply(height, tofi->window.fractional_scale);
} else {
tofi->window.surface.width = width * tofi->window.scale;
tofi->window.surface.height = height * tofi->window.scale;
}
zwlr_layer_surface_v1_ack_configure(
tofi->window.zwlr_layer_surface,
serial);
}
static void zwlr_layer_surface_close(
void *data,
struct zwlr_layer_surface_v1 *zwlr_layer_surface)
{
struct tofi *tofi = data;
tofi->closed = true;
log_debug("Layer surface close.\n");
}
static const struct zwlr_layer_surface_v1_listener zwlr_layer_surface_listener = {
.configure = zwlr_layer_surface_configure,
.closed = zwlr_layer_surface_close
};
static void wl_keyboard_keymap(
void *data,
struct wl_keyboard *wl_keyboard,
uint32_t format,
int32_t fd,
uint32_t size)
{
struct tofi *tofi = data;
assert(format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1);
char *map_shm = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
assert(map_shm != MAP_FAILED);
if (tofi->late_keyboard_init) {
log_debug("Delaying keyboard configuration.\n");
tofi->xkb_keymap_string = xstrdup(map_shm);
} else {
log_debug("Configuring keyboard.\n");
struct xkb_keymap *xkb_keymap = xkb_keymap_new_from_string(
tofi->xkb_context,
map_shm,
XKB_KEYMAP_FORMAT_TEXT_V1,
XKB_KEYMAP_COMPILE_NO_FLAGS);
munmap(map_shm, size);
close(fd);
struct xkb_state *xkb_state = xkb_state_new(xkb_keymap);
xkb_keymap_unref(tofi->xkb_keymap);
xkb_state_unref(tofi->xkb_state);
tofi->xkb_keymap = xkb_keymap;
tofi->xkb_state = xkb_state;
log_debug("Keyboard configured.\n");
}
munmap(map_shm, size);
close(fd);
}
static void wl_keyboard_enter(
void *data,
struct wl_keyboard *wl_keyboard,
uint32_t serial,
struct wl_surface *surface,
struct wl_array *keys)
{
/* Deliberately left blank */
}
static void wl_keyboard_leave(
void *data,
struct wl_keyboard *wl_keyboard,
uint32_t serial,
struct wl_surface *surface)
{
/* Deliberately left blank */
}
static void wl_keyboard_key(
void *data,
struct wl_keyboard *wl_keyboard,
uint32_t serial,
uint32_t time,
uint32_t key,
uint32_t state)
{
struct tofi *tofi = data;
/*
* If this wasn't a keypress (i.e. was a key release), just update key
* repeat info and return.
*/
uint32_t keycode = key + 8;
if (state != WL_KEYBOARD_KEY_STATE_PRESSED) {
if (keycode == tofi->repeat.keycode) {
tofi->repeat.active = false;
} else {
tofi->repeat.next = gettime_ms() + tofi->repeat.delay;
}
return;
}
/* A rate of 0 disables key repeat */
if (xkb_keymap_key_repeats(tofi->xkb_keymap, keycode) && tofi->repeat.rate != 0) {
tofi->repeat.active = true;
tofi->repeat.keycode = keycode;
tofi->repeat.next = gettime_ms() + tofi->repeat.delay;
}
input_handle_keypress(tofi, keycode);
}
static void wl_keyboard_modifiers(
void *data,
struct wl_keyboard *wl_keyboard,
uint32_t serial,
uint32_t mods_depressed,
uint32_t mods_latched,
uint32_t mods_locked,
uint32_t group)
{
struct tofi *tofi = data;
if (tofi->xkb_state == NULL) {
return;
}
xkb_state_update_mask(
tofi->xkb_state,
mods_depressed,
mods_latched,
mods_locked,
0,
0,
group);
}
static void wl_keyboard_repeat_info(
void *data,
struct wl_keyboard *wl_keyboard,
int32_t rate,
int32_t delay)
{
struct tofi *tofi = data;
tofi->repeat.rate = rate;
tofi->repeat.delay = delay;
if (rate > 0) {
log_debug("Key repeat every %u ms after %u ms.\n",
1000 / rate,
delay);
} else {
log_debug("Key repeat disabled.\n");
}
}
static const struct wl_keyboard_listener wl_keyboard_listener = {
.keymap = wl_keyboard_keymap,
.enter = wl_keyboard_enter,
.leave = wl_keyboard_leave,
.key = wl_keyboard_key,
.modifiers = wl_keyboard_modifiers,
.repeat_info = wl_keyboard_repeat_info,
};
static void wl_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)
{
struct tofi *tofi = data;
if (tofi->hide_cursor) {
/* Hide the cursor by setting its surface to NULL. */
wl_pointer_set_cursor(tofi->wl_pointer, serial, NULL, 0, 0);
}
}
static void wl_pointer_leave(
void *data,
struct wl_pointer *pointer,
uint32_t serial,
struct wl_surface *surface)
{
/* Deliberately left blank */
}
static void wl_pointer_motion(
void *data,
struct wl_pointer *pointer,
uint32_t time,
wl_fixed_t surface_x,
wl_fixed_t surface_y)
{
/* Deliberately left blank */
}
static void wl_pointer_button(
void *data,
struct wl_pointer *pointer,
uint32_t serial,
uint32_t time,
uint32_t button,
enum wl_pointer_button_state state)
{
/* Deliberately left blank */
}
static void wl_pointer_axis(
void *data,
struct wl_pointer *pointer,
uint32_t time,
enum wl_pointer_axis axis,
wl_fixed_t value)
{
/* Deliberately left blank */
}
static void wl_pointer_frame(void *data, struct wl_pointer *pointer)
{
/* Deliberately left blank */
}
static void wl_pointer_axis_source(
void *data,
struct wl_pointer *pointer,
enum wl_pointer_axis_source axis_source)
{
/* Deliberately left blank */
}
static void wl_pointer_axis_stop(
void *data,
struct wl_pointer *pointer,
uint32_t time,
enum wl_pointer_axis axis)
{
/* Deliberately left blank */
}
static void wl_pointer_axis_discrete(
void *data,
struct wl_pointer *pointer,
enum wl_pointer_axis axis,
int32_t discrete)
{
/* Deliberately left blank */
}
static const struct wl_pointer_listener wl_pointer_listener = {
.enter = wl_pointer_enter,
.leave = wl_pointer_leave,
.motion = wl_pointer_motion,
.button = wl_pointer_button,
.axis = wl_pointer_axis,
.frame = wl_pointer_frame,
.axis_source = wl_pointer_axis_source,
.axis_stop = wl_pointer_axis_stop,
.axis_discrete = wl_pointer_axis_discrete
};
static void wl_seat_capabilities(
void *data,
struct wl_seat *wl_seat,
uint32_t capabilities)
{
struct tofi *tofi = data;
bool have_keyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD;
bool have_pointer = capabilities & WL_SEAT_CAPABILITY_POINTER;
if (have_keyboard && tofi->wl_keyboard == NULL) {
tofi->wl_keyboard = wl_seat_get_keyboard(tofi->wl_seat);
wl_keyboard_add_listener(
tofi->wl_keyboard,
&wl_keyboard_listener,
tofi);
log_debug("Got keyboard from seat.\n");
} else if (!have_keyboard && tofi->wl_keyboard != NULL) {
wl_keyboard_release(tofi->wl_keyboard);
tofi->wl_keyboard = NULL;
log_debug("Released keyboard.\n");
}
if (have_pointer && tofi->wl_pointer == NULL) {
tofi->wl_pointer = wl_seat_get_pointer(tofi->wl_seat);
wl_pointer_add_listener(
tofi->wl_pointer,
&wl_pointer_listener,
tofi);
log_debug("Got pointer from seat.\n");
} else if (!have_pointer && tofi->wl_pointer != NULL) {
wl_pointer_release(tofi->wl_pointer);
tofi->wl_pointer = NULL;
log_debug("Released pointer.\n");
}
}
static void wl_seat_name(void *data, struct wl_seat *wl_seat, const char *name)
{
/* Deliberately left blank */
}
static const struct wl_seat_listener wl_seat_listener = {
.capabilities = wl_seat_capabilities,
.name = wl_seat_name,
};
static void wl_data_offer_offer(
void *data,
struct wl_data_offer *wl_data_offer,
const char *mime_type)
{
struct clipboard *clipboard = data;
/* Only accept plain text, and prefer utf-8. */
if (!strcmp(mime_type, mime_type_text_plain)) {
if (clipboard->mime_type != NULL) {
clipboard->mime_type = mime_type_text_plain;
}
} else if (!strcmp(mime_type, mime_type_text_plain_utf8)) {
clipboard->mime_type = mime_type_text_plain_utf8;
}
}
static void wl_data_offer_source_actions(
void *data,
struct wl_data_offer *wl_data_offer,
uint32_t source_actions)
{
/* Deliberately left blank */
}
static void wl_data_offer_action(
void *data,
struct wl_data_offer *wl_data_offer,
uint32_t action)
{
/* Deliberately left blank */
}
static const struct wl_data_offer_listener wl_data_offer_listener = {
.offer = wl_data_offer_offer,
.source_actions = wl_data_offer_source_actions,
.action = wl_data_offer_action
};
static void wl_data_device_data_offer(
void *data,
struct wl_data_device *wl_data_device,
struct wl_data_offer *wl_data_offer)
{
struct clipboard *clipboard = data;
clipboard_reset(clipboard);
clipboard->wl_data_offer = wl_data_offer;
wl_data_offer_add_listener(
wl_data_offer,
&wl_data_offer_listener,
clipboard);
}
static void wl_data_device_enter(
void *data,
struct wl_data_device *wl_data_device,
uint32_t serial,
struct wl_surface *wl_surface,
int32_t x,
int32_t y,
struct wl_data_offer *wl_data_offer)
{
/* Drag-and-drop is just ignored for now. */
wl_data_offer_accept(
wl_data_offer,
serial,
NULL);
wl_data_offer_set_actions(
wl_data_offer,
WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE,
WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE);
}
static void wl_data_device_leave(
void *data,
struct wl_data_device *wl_data_device)
{
/* Deliberately left blank */
}
static void wl_data_device_motion(
void *data,
struct wl_data_device *wl_data_device,
uint32_t time,
int32_t x,
int32_t y)
{
/* Deliberately left blank */
}
static void wl_data_device_drop(
void *data,
struct wl_data_device *wl_data_device)
{
/* Deliberately left blank */
}
static void wl_data_device_selection(
void *data,
struct wl_data_device *wl_data_device,
struct wl_data_offer *wl_data_offer)
{
struct clipboard *clipboard = data;
if (wl_data_offer == NULL) {
clipboard_reset(clipboard);
}
}
static const struct wl_data_device_listener wl_data_device_listener = {
.data_offer = wl_data_device_data_offer,
.enter = wl_data_device_enter,
.leave = wl_data_device_leave,
.motion = wl_data_device_motion,
.drop = wl_data_device_drop,
.selection = wl_data_device_selection
};
static void output_geometry(
void *data,
struct wl_output *wl_output,
int32_t x,
int32_t y,
int32_t physical_width,
int32_t physical_height,
int32_t subpixel,
const char *make,
const char *model,
int32_t transform)
{
struct tofi *tofi = data;
struct output_list_element *el;
wl_list_for_each(el, &tofi->output_list, link) {
if (el->wl_output == wl_output) {
el->transform = transform;
}
}
}
static void output_mode(
void *data,
struct wl_output *wl_output,
uint32_t flags,
int32_t width,
int32_t height,
int32_t refresh)
{
struct tofi *tofi = data;
struct output_list_element *el;
wl_list_for_each(el, &tofi->output_list, link) {
if (el->wl_output == wl_output) {
if (flags & WL_OUTPUT_MODE_CURRENT) {
el->width = width;
el->height = height;
}
}
}
}
static void output_scale(
void *data,
struct wl_output *wl_output,
int32_t factor)
{
struct tofi *tofi = data;
struct output_list_element *el;
wl_list_for_each(el, &tofi->output_list, link) {
if (el->wl_output == wl_output) {
el->scale = factor;
}
}
}
static void output_name(
void *data,
struct wl_output *wl_output,
const char *name)
{
struct tofi *tofi = data;
struct output_list_element *el;
wl_list_for_each(el, &tofi->output_list, link) {
if (el->wl_output == wl_output) {
el->name = xstrdup(name);
}
}
}
static void output_description(
void *data,
struct wl_output *wl_output,
const char *description)
{
/* Deliberately left blank */
}
static void output_done(void *data, struct wl_output *wl_output)
{
log_debug("Output configuration done.\n");
}
static const struct wl_output_listener wl_output_listener = {
.geometry = output_geometry,
.mode = output_mode,
.done = output_done,
.scale = output_scale,
#ifndef NO_WL_OUTPUT_NAME
.name = output_name,
.description = output_description,
#endif
};
static void registry_global(
void *data,
struct wl_registry *wl_registry,
uint32_t name,
const char *interface,
uint32_t version)
{
struct tofi *tofi = data;
//log_debug("Registry %u: %s v%u.\n", name, interface, version);
if (!strcmp(interface, wl_compositor_interface.name)) {
tofi->wl_compositor = wl_registry_bind(
wl_registry,
name,
&wl_compositor_interface,
4);
log_debug("Bound to compositor %u.\n", name);
} else if (!strcmp(interface, wl_seat_interface.name)) {
tofi->wl_seat = wl_registry_bind(
wl_registry,
name,
&wl_seat_interface,
7);
wl_seat_add_listener(
tofi->wl_seat,
&wl_seat_listener,
tofi);
log_debug("Bound to seat %u.\n", name);
} else if (!strcmp(interface, wl_output_interface.name)) {
struct output_list_element *el = xmalloc(sizeof(*el));
if (version < 4) {
el->name = xstrdup("");
log_warning("Using an outdated compositor, "
"output selection will not work.\n");
} else {
version = 4;
}
el->wl_output = wl_registry_bind(
wl_registry,
name,
&wl_output_interface,
version);
wl_output_add_listener(
el->wl_output,
&wl_output_listener,
tofi);
wl_list_insert(&tofi->output_list, &el->link);
log_debug("Bound to output %u.\n", name);
} else if (!strcmp(interface, wl_shm_interface.name)) {
tofi->wl_shm = wl_registry_bind(
wl_registry,
name,
&wl_shm_interface,
1);
log_debug("Bound to shm %u.\n", name);
} else if (!strcmp(interface, wl_data_device_manager_interface.name)) {
tofi->wl_data_device_manager = wl_registry_bind(
wl_registry,
name,
&wl_data_device_manager_interface,
3);
log_debug("Bound to data device manager %u.\n", name);
} else if (!strcmp(interface, zwlr_layer_shell_v1_interface.name)) {
if (version < 3) {
log_warning("Using an outdated compositor, "
"screen anchoring may not work.\n");
} else {
version = 3;
}
tofi->zwlr_layer_shell = wl_registry_bind(
wl_registry,
name,
&zwlr_layer_shell_v1_interface,
version);
log_debug("Bound to zwlr_layer_shell_v1 %u.\n", name);
} else if (!strcmp(interface, wp_viewporter_interface.name)) {
tofi->wp_viewporter = wl_registry_bind(
wl_registry,
name,
&wp_viewporter_interface,
1);
log_debug("Bound to wp_viewporter %u.\n", name);
} else if (!strcmp(interface, wp_fractional_scale_manager_v1_interface.name)) {
tofi->wp_fractional_scale_manager = wl_registry_bind(
wl_registry,
name,
&wp_fractional_scale_manager_v1_interface,
1);
log_debug("Bound to wp_fractional_scale_manager_v1 %u.\n", name);
}
}
static void registry_global_remove(
void *data,
struct wl_registry *wl_registry,
uint32_t name)
{
/* Deliberately left blank */
}
static const struct wl_registry_listener wl_registry_listener = {
.global = registry_global,
.global_remove = registry_global_remove,
};
static void surface_enter(
void *data,
struct wl_surface *wl_surface,
struct wl_output *wl_output)
{
log_debug("Surface entered output.\n");
}
static void surface_leave(
void *data,
struct wl_surface *wl_surface,
struct wl_output *wl_output)
{
/* Deliberately left blank */
}
static const struct wl_surface_listener wl_surface_listener = {
.enter = surface_enter,
.leave = surface_leave
};
/*
* These "dummy_*" functions are callbacks just for the dummy surface used to
* select the default output if there's more than one.
*/
static void dummy_layer_surface_configure(
void *data,
struct zwlr_layer_surface_v1 *zwlr_layer_surface,
uint32_t serial,
uint32_t width,
uint32_t height)
{
zwlr_layer_surface_v1_ack_configure(
zwlr_layer_surface,
serial);
}
static void dummy_layer_surface_close(
void *data,
struct zwlr_layer_surface_v1 *zwlr_layer_surface)
{
}
static const struct zwlr_layer_surface_v1_listener dummy_layer_surface_listener = {
.configure = dummy_layer_surface_configure,
.closed = dummy_layer_surface_close
};
static void dummy_fractional_scale_preferred_scale(
void *data,
struct wp_fractional_scale_v1 *wp_fractional_scale,
uint32_t scale)
{
struct tofi *tofi = data;
tofi->window.fractional_scale = scale;
}
static const struct wp_fractional_scale_v1_listener dummy_fractional_scale_listener = {
.preferred_scale = dummy_fractional_scale_preferred_scale
};
static void dummy_surface_enter(
void *data,
struct wl_surface *wl_surface,
struct wl_output *wl_output)
{
struct tofi *tofi = data;
struct output_list_element *el;
wl_list_for_each(el, &tofi->output_list, link) {
if (el->wl_output == wl_output) {
tofi->default_output = el;
break;
}
}
}
static void dummy_surface_leave(
void *data,
struct wl_surface *wl_surface,
struct wl_output *wl_output)
{
/* Deliberately left blank */
}
static const struct wl_surface_listener dummy_surface_listener = {
.enter = dummy_surface_enter,
.leave = dummy_surface_leave
};
static void usage(bool err)
{
fprintf(err ? stderr : stdout, "%s",
"Usage: tofi [options]\n"
"\n"
"Basic options:\n"
" -h, --help Print this message and exit.\n"
" -c, --config <path> Specify a config file.\n"
" --prompt-text <string> Prompt text.\n"
" --width <px|%> Width of the window.\n"
" --height <px|%> Height of the window.\n"
" --output <name> Name of output to display window on.\n"
" --anchor <position> Location on screen to anchor window.\n"
" --horizontal <true|false> List results horizontally.\n"
" --fuzzy-match <true|false> Use fuzzy matching for searching.\n"
"\n"
"All options listed in \"man 5 tofi\" are also accpted in the form \"--key=value\".\n"
);
}
/* Option parsing with getopt. */
const struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"config", required_argument, NULL, 'c'},
{"include", required_argument, NULL, 0},
{"anchor", required_argument, NULL, 0},
{"exclusive-zone", required_argument, NULL, 0},
{"background-color", required_argument, NULL, 0},
{"corner-radius", required_argument, NULL, 0},
{"font", required_argument, NULL, 0},
{"font-size", required_argument, NULL, 0},
{"font-features", required_argument, NULL, 0},
{"font-variations", required_argument, NULL, 0},
{"num-results", required_argument, NULL, 0},
{"selection-color", required_argument, NULL, 0},
{"selection-match-color", required_argument, NULL, 0},
{"selection-padding", required_argument, NULL, 0},
{"selection-background", required_argument, NULL, 0},
{"selection-background-padding", required_argument, NULL, 0},
{"selection-background-corner-radius", required_argument, NULL, 0},
{"outline-width", required_argument, NULL, 0},
{"outline-color", required_argument, NULL, 0},
{"text-cursor", required_argument, NULL, 0},
{"text-cursor-style", required_argument, NULL, 0},
{"text-cursor-color", required_argument, NULL, 0},
{"text-cursor-background", required_argument, NULL, 0},
{"text-cursor-corner-radius", required_argument, NULL, 0},
{"text-cursor-thickness", required_argument, NULL, 0},
{"prompt-text", required_argument, NULL, 0},
{"prompt-padding", required_argument, NULL, 0},
{"prompt-color", required_argument, NULL, 0},
{"prompt-background", required_argument, NULL, 0},
{"prompt-background-padding", required_argument, NULL, 0},
{"prompt-background-corner-radius", required_argument, NULL, 0},
{"placeholder-text", required_argument, NULL, 0},
{"placeholder-color", required_argument, NULL, 0},
{"placeholder-background", required_argument, NULL, 0},
{"placeholder-background-padding", required_argument, NULL, 0},
{"placeholder-background-corner-radius", required_argument, NULL, 0},
{"input-color", required_argument, NULL, 0},
{"input-background", required_argument, NULL, 0},
{"input-background-padding", required_argument, NULL, 0},
{"input-background-corner-radius", required_argument, NULL, 0},
{"default-result-color", required_argument, NULL, 0},
{"default-result-background", required_argument, NULL, 0},
{"default-result-background-padding", required_argument, NULL, 0},
{"default-result-background-corner-radius", required_argument, NULL, 0},
{"alternate-result-color", required_argument, NULL, 0},
{"alternate-result-background", required_argument, NULL, 0},
{"alternate-result-background-padding", required_argument, NULL, 0},
{"alternate-result-background-corner-radius", required_argument, NULL, 0},
{"result-spacing", required_argument, NULL, 0},
{"min-input-width", required_argument, NULL, 0},
{"border-width", required_argument, NULL, 0},
{"border-color", required_argument, NULL, 0},
{"text-color", required_argument, NULL, 0},
{"width", required_argument, NULL, 0},
{"height", required_argument, NULL, 0},
{"margin-top", required_argument, NULL, 0},
{"margin-bottom", required_argument, NULL, 0},
{"margin-left", required_argument, NULL, 0},
{"margin-right", required_argument, NULL, 0},
{"padding-top", required_argument, NULL, 0},
{"padding-bottom", required_argument, NULL, 0},
{"padding-left", required_argument, NULL, 0},
{"padding-right", required_argument, NULL, 0},
{"clip-to-padding", required_argument, NULL, 0},
{"horizontal", required_argument, NULL, 0},
{"hide-cursor", required_argument, NULL, 0},
{"history", required_argument, NULL, 0},
{"history-file", required_argument, NULL, 0},
{"fuzzy-match", required_argument, NULL, 0},
{"require-match", required_argument, NULL, 0},
{"auto-accept-single", required_argument, NULL, 0},
{"hide-input", required_argument, NULL, 0},
{"hidden-character", required_argument, NULL, 0},
{"drun-launch", required_argument, NULL, 0},
{"drun-print-exec", required_argument, NULL, 0},
{"terminal", required_argument, NULL, 0},
{"hint-font", required_argument, NULL, 0},
{"multi-instance", required_argument, NULL, 0},
{"ascii-input", required_argument, NULL, 0},
{"output", required_argument, NULL, 0},
{"scale", required_argument, NULL, 0},
{"late-keyboard-init", optional_argument, NULL, 'k'},
{NULL, 0, NULL, 0}
};
const char *short_options = ":hc:";
static void parse_args(struct tofi *tofi, int argc, char *argv[])
{
bool load_default_config = true;
int option_index = 0;
/* Handle errors ourselves. */
opterr = 0;
/* First pass, just check for config file, help, and errors. */
optind = 1;
int opt = getopt_long(argc, argv, short_options, long_options, &option_index);
while (opt != -1) {
if (opt == 'h') {
usage(false);
exit(EXIT_SUCCESS);
} else if (opt == 'c') {
config_load(tofi, optarg);
load_default_config = false;
} else if (opt == ':') {
log_error("Option %s requires an argument.\n", argv[optind - 1]);
usage(true);
exit(EXIT_FAILURE);
} else if (opt == '?') {
if (optopt) {
log_error("Unknown option -%c.\n", optopt);
} else {
log_error("Unknown option %s.\n", argv[optind - 1]);
}
usage(true);
exit(EXIT_FAILURE);
}
opt = getopt_long(argc, argv, short_options, long_options, &option_index);
}
if (load_default_config) {
config_load(tofi, NULL);
}
/* Second pass, parse everything else. */
optind = 1;
opt = getopt_long(argc, argv, short_options, long_options, &option_index);
while (opt != -1) {
if (opt == 0) {
if (!config_apply(tofi, long_options[option_index].name, optarg)) {
exit(EXIT_FAILURE);
}
} else if (opt == 'k') {
/*
* Backwards compatibility for --late-keyboard-init not
* taking an argument.
*/
if (optarg) {
if (!config_apply(tofi, long_options[option_index].name, optarg)) {
exit(EXIT_FAILURE);
}
} else {
tofi->late_keyboard_init = true;
}
}
opt = getopt_long(argc, argv, short_options, long_options, &option_index);
}
if (optind < argc) {
log_error("Unexpected non-option argument '%s'.\n", argv[optind]);
usage(true);
exit(EXIT_FAILURE);
}
}
static bool do_submit(struct tofi *tofi)
{
struct entry *entry = &tofi->window.entry;
uint32_t selection = entry->selection + entry->first_result;
char *res = entry->results.buf[selection].string;
if (tofi->window.entry.results.count == 0) {
/* Always require a match in drun mode. */
if (tofi->require_match || entry->drun) {
return false;
} else {
printf("%s\n", entry->input_utf8);
return true;
}
}
if (entry->drun) {
/*
* At this point, the list of apps is history sorted rather
* than alphabetically sorted, so we can't use
* desktop_vec_find_sorted().
*/
struct desktop_entry *app = NULL;
for (size_t i = 0; i < entry->apps.count; i++) {
if (!strcmp(res, entry->apps.buf[i].name)) {
app = &entry->apps.buf[i];
break;
}
}
if (app == NULL) {
log_error("Couldn't find application file! This shouldn't happen.\n");
return false;
}
char *path = app->path;
if (tofi->drun_launch) {
drun_launch(path);
} else {
drun_print(path, tofi->default_terminal);
}
} else {
printf("%s\n", res);
}
if (tofi->use_history) {
history_add(
&entry->history,
entry->results.buf[selection].string);
if (tofi->history_file[0] == 0) {
history_save_default_file(&entry->history, entry->drun);
} else {
history_save(&entry->history, tofi->history_file);
}
}
return true;
}
static void read_clipboard(struct tofi *tofi)
{
struct entry *entry = &tofi->window.entry;
/* Make a copy of any text after the cursor. */
uint32_t *end_text = NULL;
size_t end_text_length = entry->input_utf32_length - entry->cursor_position;
if (end_text_length > 0) {
end_text = xcalloc(end_text_length, sizeof(*entry->input_utf32));
memcpy(end_text,
&entry->input_utf32[entry->cursor_position],
end_text_length * sizeof(*entry->input_utf32));
}
/* Buffer for 4 UTF-8 bytes plus a null terminator. */
char buffer[5];
memset(buffer, 0, N_ELEM(buffer));
errno = 0;
bool eof = false;
while (entry->cursor_position < N_ELEM(entry->input_utf32)) {
for (size_t i = 0; i < 4; i++) {
/*
* Read input 1 byte at a time. This is slow, but easy,
* and speed of pasting shouldn't really matter.
*/
int res = read(tofi->clipboard.fd, &buffer[i], 1);
if (res == 0) {
eof = true;
break;
} else if (res == -1) {
if (errno == EAGAIN) {
/*
* There was no more data to be read,
* but EOF hasn't been reached yet.
*
* This could mean more than a pipe's
* capacity (64k) of data was sent, in
* which case we'd potentially skip
* a character, but we should hit the
* input length limit long before that.
*/
input_refresh_results(tofi);
tofi->window.surface.redraw = true;
return;
}
log_error("Failed to read clipboard: %s\n", strerror(errno));
clipboard_finish_paste(&tofi->clipboard);
return;
}
uint32_t unichar = utf8_to_utf32_validate(buffer);
if (unichar == (uint32_t)-2) {
/* The current character isn't complete yet. */
continue;
} else if (unichar == (uint32_t)-1) {
log_error("Invalid UTF-8 character in clipboard: %s\n", buffer);
break;
} else {
entry->input_utf32[entry->cursor_position] = unichar;
entry->cursor_position++;
break;
}
}
memset(buffer, 0, N_ELEM(buffer));
if (eof) {
break;
}
}
entry->input_utf32_length = entry->cursor_position;
/* If there was any text after the cursor, re-insert it now. */
if (end_text != NULL) {
for (size_t i = 0; i < end_text_length; i++) {
if (entry->input_utf32_length == N_ELEM(entry->input_utf32)) {
break;
}
entry->input_utf32[entry->input_utf32_length] = end_text[i];
entry->input_utf32_length++;
}
free(end_text);
}
entry->input_utf32[MIN(entry->input_utf32_length, N_ELEM(entry->input_utf32) - 1)] = U'\0';
clipboard_finish_paste(&tofi->clipboard);
input_refresh_results(tofi);
tofi->window.surface.redraw = true;
}
int main(int argc, char *argv[])
{
/* Call log_debug to initialise the timers we use for perf checking. */
log_debug("This is tofi.\n");
/*
* Set the locale to the user's default, so we can deal with non-ASCII
* characters.
*/
setlocale(LC_ALL, "");
/* Default options. */
struct tofi tofi = {
.window = {
.scale = 1,
.width = 1280,
.height = 720,
.exclusive_zone = -1,
.entry = {
.font_name = "Sans",
.font_size = 24,
.prompt_text = "run: ",
.hidden_character_utf8 = u8"*",
.padding_top = 8,
.padding_bottom = 8,
.padding_left = 8,
.padding_right = 8,
.clip_to_padding = true,
.border_width = 12,
.outline_width = 4,
.background_color = {0.106f, 0.114f, 0.118f, 1.0f},
.foreground_color = {1.0f, 1.0f, 1.0f, 1.0f},
.border_color = {0.976f, 0.149f, 0.447f, 1.0f},
.outline_color = {0.031f, 0.031f, 0.0f, 1.0f},
.placeholder_theme.foreground_color = {1.0f, 1.0f, 1.0f, 0.66f},
.placeholder_theme.foreground_specified = true,
.selection_theme.foreground_color = {0.976f, 0.149f, 0.447f, 1.0f},
.selection_theme.foreground_specified = true,
.cursor_theme.thickness = 2
}
},
.anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP
| ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM
| ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT
| ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT,
.use_history = true,
.require_match = true,
.use_scale = true,
};
wl_list_init(&tofi.output_list);
if (getenv("TERMINAL") != NULL) {
snprintf(
tofi.default_terminal,
N_ELEM(tofi.default_terminal),
"%s",
getenv("TERMINAL"));
}
parse_args(&tofi, argc, argv);
log_debug("Config done.\n");
if (!tofi.multiple_instance && lock_check()) {
log_error("Another instance of tofi is already running.\n");
exit(EXIT_FAILURE);
}
/*
* Initial Wayland & XKB setup.
* The first thing to do is connect a listener to the global registry,
* so that we can bind to the various global objects and start talking
* to Wayland.
*/
log_debug("Connecting to Wayland display.\n");
tofi.wl_display = wl_display_connect(NULL);
if (tofi.wl_display == NULL) {
log_error("Couldn't connect to Wayland display.\n");
exit(EXIT_FAILURE);
}
tofi.wl_registry = wl_display_get_registry(tofi.wl_display);
if (!tofi.late_keyboard_init) {
log_debug("Creating xkb context.\n");
tofi.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (tofi.xkb_context == NULL) {
log_error("Couldn't create an XKB context.\n");
exit(EXIT_FAILURE);
}
}
wl_registry_add_listener(
tofi.wl_registry,
&wl_registry_listener,
&tofi);
/*
* After this first roundtrip, the only thing that should have happened
* is our registry_global() function being called and setting up the
* various global object bindings.
*/
log_debug("First roundtrip start.\n");
log_indent();
wl_display_roundtrip(tofi.wl_display);
log_unindent();
log_debug("First roundtrip done.\n");
/*
* The next roundtrip causes the listeners we set up in
* registry_global() to be called. Notably, the output should be
* configured, telling us the scale factor and size.
*/
log_debug("Second roundtrip start.\n");
log_indent();
wl_display_roundtrip(tofi.wl_display);
log_unindent();
log_debug("Second roundtrip done.\n");
{
/*
* Determine the output we're going to appear on, and get its
* fractional scale if supported.
*
* This seems like an ugly solution, but as far as I know
* there's no way to determine the default output other than to
* call get_layer_surface with NULL as the output and see which
* output our surface turns up on.
*
* Additionally, determining fractional scale factors can
* currently only be done by attaching a wp_fractional_scale to
* a surface and displaying it.
*
* Here we set up a single pixel surface, perform the required
* two roundtrips, then tear it down. tofi.default_output
* should then contain the output our surface was assigned to,
* and tofi.window.fractional_scale should have the scale
* factor.
*/
log_debug("Determining output.\n");
log_indent();
struct surface surface = {
.width = 1,
.height = 1
};
surface.wl_surface =
wl_compositor_create_surface(tofi.wl_compositor);
wl_surface_add_listener(
surface.wl_surface,
&dummy_surface_listener,
&tofi);
struct wp_fractional_scale_v1 *wp_fractional_scale = NULL;
if (tofi.wp_fractional_scale_manager != NULL) {
wp_fractional_scale =
wp_fractional_scale_manager_v1_get_fractional_scale(
tofi.wp_fractional_scale_manager,
surface.wl_surface);
wp_fractional_scale_v1_add_listener(
wp_fractional_scale,
&dummy_fractional_scale_listener,
&tofi);
}
/*
* If we have a desired output, make sure we appear on it so we
* can determine the correct fractional scale.
*/
struct wl_output *wl_output = NULL;
if (tofi.target_output_name[0] != '\0') {
struct output_list_element *el;
wl_list_for_each(el, &tofi.output_list, link) {
if (!strcmp(tofi.target_output_name, el->name)) {
wl_output = el->wl_output;
break;
}
}
}
struct zwlr_layer_surface_v1 *zwlr_layer_surface =
zwlr_layer_shell_v1_get_layer_surface(
tofi.zwlr_layer_shell,
surface.wl_surface,
wl_output,
ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND,
"dummy");
/*
* Workaround for Hyprland, where if this is not set the dummy
* surface never enters an output for some reason.
*/
zwlr_layer_surface_v1_set_keyboard_interactivity(
zwlr_layer_surface,
ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE
);
zwlr_layer_surface_v1_add_listener(
zwlr_layer_surface,
&dummy_layer_surface_listener,
&tofi);
zwlr_layer_surface_v1_set_size(
zwlr_layer_surface,
1,
1);
wl_surface_commit(surface.wl_surface);
log_debug("First dummy roundtrip start.\n");
log_indent();
wl_display_roundtrip(tofi.wl_display);
log_unindent();
log_debug("First dummy roundtrip done.\n");
log_debug("Initialising dummy surface.\n");
log_indent();
surface_init(&surface, tofi.wl_shm);
surface_draw(&surface);
log_unindent();
log_debug("Dummy surface initialised.\n");
log_debug("Second dummy roundtrip start.\n");
log_indent();
wl_display_roundtrip(tofi.wl_display);
log_unindent();
log_debug("Second dummy roundtrip done.\n");
surface_destroy(&surface);
zwlr_layer_surface_v1_destroy(zwlr_layer_surface);
if (wp_fractional_scale != NULL) {
wp_fractional_scale_v1_destroy(wp_fractional_scale);
}
wl_surface_destroy(surface.wl_surface);
/*
* Walk through our output list and select the one we want if
* the user's asked for a specific one, otherwise just get the
* default one.
*/
bool found_target = false;
struct output_list_element *head;
head = wl_container_of(tofi.output_list.next, head, link);
struct output_list_element *el;
struct output_list_element *tmp;
if (tofi.target_output_name[0] != 0) {
log_debug("Looking for output %s.\n", tofi.target_output_name);
} else if (tofi.default_output != NULL) {
snprintf(
tofi.target_output_name,
N_ELEM(tofi.target_output_name),
"%s",
tofi.default_output->name);
/* We don't need this anymore. */
tofi.default_output = NULL;
}
wl_list_for_each_reverse_safe(el, tmp, &tofi.output_list, link) {
if (!strcmp(tofi.target_output_name, el->name)) {
found_target = true;
continue;
}
/*
* If we've already found the output we're looking for
* or this isn't the first output in the list, remove
* it.
*/
if (found_target || el != head) {
wl_list_remove(&el->link);
wl_output_release(el->wl_output);
free(el->name);
free(el);
}
}
/*
* The only output left should either be the one we want, or
* the first that was advertised.
*/
el = wl_container_of(tofi.output_list.next, el, link);
/*
* If we're rotated 90 degrees, we need to swap width and
* height to calculate percentages.
*/
switch (el->transform) {
case WL_OUTPUT_TRANSFORM_90:
case WL_OUTPUT_TRANSFORM_270:
case WL_OUTPUT_TRANSFORM_FLIPPED_90:
case WL_OUTPUT_TRANSFORM_FLIPPED_270:
tofi.output_width = el->height;
tofi.output_height = el->width;
break;
default:
tofi.output_width = el->width;
tofi.output_height = el->height;
}
tofi.window.scale = el->scale;
tofi.window.transform = el->transform;
log_unindent();
log_debug("Selected output %s.\n", el->name);
}
/*
* We can now scale values and calculate any percentages, as we know
* the output size and scale.
*/
config_fixup_values(&tofi);
/*
* If we were invoked as tofi-run, generate the command list.
* If we were invoked as tofi-drun, generate the desktop app list.
* Otherwise, just read standard input.
*/
if (strstr(argv[0], "-run")) {
log_debug("Generating command list.\n");
log_indent();
tofi.window.entry.command_buffer = compgen_cached();
struct string_ref_vec commands = string_ref_vec_from_buffer(tofi.window.entry.command_buffer);
if (tofi.use_history) {
if (tofi.history_file[0] == 0) {
tofi.window.entry.history = history_load_default_file(tofi.window.entry.drun);
} else {
tofi.window.entry.history = history_load(tofi.history_file);
}
tofi.window.entry.commands = compgen_history_sort(&commands, &tofi.window.entry.history);
string_ref_vec_destroy(&commands);
} else {
tofi.window.entry.commands = commands;
}
log_unindent();
log_debug("Command list generated.\n");
} else if (strstr(argv[0], "-drun")) {
log_debug("Generating desktop app list.\n");
log_indent();
tofi.window.entry.drun = true;
struct desktop_vec apps = drun_generate_cached();
if (tofi.use_history) {
if (tofi.history_file[0] == 0) {
tofi.window.entry.history = history_load_default_file(tofi.window.entry.drun);
} else {
tofi.window.entry.history = history_load(tofi.history_file);
}
if (tofi.window.entry.drun) {
drun_history_sort(&apps, &tofi.window.entry.history);
}
}
struct string_ref_vec commands = string_ref_vec_create();
for (size_t i = 0; i < apps.count; i++) {
string_ref_vec_add(&commands, apps.buf[i].name);
}
tofi.window.entry.commands = commands;
tofi.window.entry.apps = apps;
log_unindent();
log_debug("App list generated.\n");
} else {
log_debug("Reading stdin.\n");
char *buf = read_stdin(!tofi.ascii_input);
tofi.window.entry.command_buffer = buf;
tofi.window.entry.commands = string_ref_vec_from_buffer(buf);
if (tofi.use_history) {
if (tofi.history_file[0] == 0) {
tofi.use_history = false;
} else {
tofi.window.entry.history = history_load(tofi.history_file);
string_ref_vec_history_sort(&tofi.window.entry.commands, &tofi.window.entry.history);
}
}
log_debug("Result list generated.\n");
}
tofi.window.entry.results = string_ref_vec_copy(&tofi.window.entry.commands);
if (tofi.auto_accept_single && tofi.window.entry.results.count == 1) {
log_debug("Only one result, exiting.\n");
do_submit(&tofi);
return EXIT_SUCCESS;
}
/*
* Next, we create the Wayland surface, which takes on the
* layer shell role.
*/
log_debug("Creating main window surface.\n");
tofi.window.surface.wl_surface =
wl_compositor_create_surface(tofi.wl_compositor);
wl_surface_add_listener(
tofi.window.surface.wl_surface,
&wl_surface_listener,
&tofi);
if (tofi.window.width == 0 || tofi.window.height == 0) {
/*
* Workaround for compatibility with legacy behaviour.
*
* Before the fractional_scale protocol was released, there was
* no way for a client to know whether a fractional scale
* factor had been set, meaning percentage-based dimensions
* were incorrect. As a workaround for full-size windows, we
* allowed specifying 0 for the width / height, which caused
* zwlr_layer_shell to tell us the correct size to use.
*
* To make fractional scaling work, we have to use
* wp_viewporter, and no longer need to set the buffer scale.
* However, viewporter doesn't allow specifying 0 for
* destination width or height. As a workaround, if 0 size is
* set, don't use viewporter, warn the user and set the buffer
* scale here.
*/
log_warning("Width or height set to 0, disabling fractional scaling support.\n");
log_warning("If your compositor supports the fractional scale protocol, percentages are preferred.\n");
tofi.window.fractional_scale = 0;
wl_surface_set_buffer_scale(
tofi.window.surface.wl_surface,
tofi.window.scale);
} else if (tofi.wp_viewporter == NULL) {
/*
* We also could be running on a Wayland compositor which
* doesn't support wp_viewporter, in which case we need to use
* the old scaling method.
*/
log_warning("Using an outdated compositor, "
"fractional scaling will not work properly.\n");
tofi.window.fractional_scale = 0;
wl_surface_set_buffer_scale(
tofi.window.surface.wl_surface,
tofi.window.scale);
}
/* Grab the first (and only remaining) output from our list. */
struct wl_output *wl_output;
{
struct output_list_element *el;
el = wl_container_of(tofi.output_list.next, el, link);
wl_output = el->wl_output;
}
tofi.window.zwlr_layer_surface = zwlr_layer_shell_v1_get_layer_surface(
tofi.zwlr_layer_shell,
tofi.window.surface.wl_surface,
wl_output,
ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY,
"launcher");
zwlr_layer_surface_v1_set_keyboard_interactivity(
tofi.window.zwlr_layer_surface,
ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE);
zwlr_layer_surface_v1_add_listener(
tofi.window.zwlr_layer_surface,
&zwlr_layer_surface_listener,
&tofi);
zwlr_layer_surface_v1_set_anchor(
tofi.window.zwlr_layer_surface,
tofi.anchor);
zwlr_layer_surface_v1_set_exclusive_zone(
tofi.window.zwlr_layer_surface,
tofi.window.exclusive_zone);
zwlr_layer_surface_v1_set_margin(
tofi.window.zwlr_layer_surface,
tofi.window.margin_top,
tofi.window.margin_right,
tofi.window.margin_bottom,
tofi.window.margin_left);
/*
* No matter whether we're scaling via Cairo or not, we're presenting a
* scaled buffer to Wayland, so scale the window size here if we
* haven't already done so.
*/
zwlr_layer_surface_v1_set_size(
tofi.window.zwlr_layer_surface,
tofi.window.width,
tofi.window.height);
/*
* Set up a viewport for our surface, necessary for fractional scaling.
*/
if (tofi.wp_viewporter != NULL) {
tofi.window.wp_viewport = wp_viewporter_get_viewport(
tofi.wp_viewporter,
tofi.window.surface.wl_surface);
if (tofi.window.width > 0 && tofi.window.height > 0) {
wp_viewport_set_destination(
tofi.window.wp_viewport,
tofi.window.width,
tofi.window.height);
}
}
/* Commit the surface to finalise setup. */
wl_surface_commit(tofi.window.surface.wl_surface);
/*
* Create a data device and setup a listener for data offers. This is
* required for clipboard support.
*/
tofi.wl_data_device = wl_data_device_manager_get_data_device(
tofi.wl_data_device_manager,
tofi.wl_seat);
wl_data_device_add_listener(
tofi.wl_data_device,
&wl_data_device_listener,
&tofi.clipboard);
/*
* Now that we've done all our Wayland-related setup, we do another
* roundtrip. This should cause the layer surface window to be
* configured, after which we're ready to start drawing to the screen.
*/
log_debug("Third roundtrip start.\n");
log_indent();
wl_display_roundtrip(tofi.wl_display);
log_unindent();
log_debug("Third roundtrip done.\n");
/*
* Create the various structures for our window surface. This needs to
* be done before calling entry_init as that performs some initial
* drawing, and surface_init allocates the buffers we'll be drawing to.
*/
log_debug("Initialising window surface.\n");
log_indent();
surface_init(&tofi.window.surface, tofi.wl_shm);
log_unindent();
log_debug("Window surface initialised.\n");
/*
* Initialise the structures for rendering the entry.
* Cairo needs to know the size of the surface it's creating, and
* there's no way to resize it aside from tearing everything down and
* starting again, so we make sure to do this after we've determined
* our output's scale factor. This stops us being able to change the
* scale factor after startup, but this is just a launcher, which
* shouldn't be moving between outputs while running.
*/
log_debug("Initialising renderer.\n");
log_indent();
{
/*
* No matter how we're scaling (with fractions, integers or not
* at all), we pass a fractional scale factor (the numerator of
* a fraction with denominator 120) to our setup function for
* ease.
*/
uint32_t scale = 120;
if (tofi.use_scale) {
if (tofi.window.fractional_scale != 0) {
scale = tofi.window.fractional_scale;
} else {
scale = tofi.window.scale * 120;
}
}
entry_init(
&tofi.window.entry,
tofi.window.surface.shm_pool_data,
tofi.window.surface.width,
tofi.window.surface.height,
scale);
}
log_unindent();
log_debug("Renderer initialised.\n");
/* Perform an initial render. */
surface_draw(&tofi.window.surface);
/*
* entry_init() left the second of the two buffers we use for
* double-buffering unpainted to lower startup time, as described
* there. Here, we flush our first, finished buffer to the screen, then
* copy over the image to the second buffer before we need to use it in
* the main loop. This ensures we paint to the screen as quickly as
* possible after startup.
*/
wl_display_roundtrip(tofi.wl_display);
log_debug("Initialising second buffer.\n");
memcpy(
cairo_image_surface_get_data(tofi.window.entry.cairo[1].surface),
cairo_image_surface_get_data(tofi.window.entry.cairo[0].surface),
tofi.window.surface.width * tofi.window.surface.height * sizeof(uint32_t)
);
log_debug("Second buffer initialised.\n");
/* We've just rendered, so we don't need to do it again right now. */
tofi.window.surface.redraw = false;
/* If we delayed keyboard initialisation, do it now */
if (tofi.late_keyboard_init) {
log_debug("Creating xkb context.\n");
tofi.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
if (tofi.xkb_context == NULL) {
log_error("Couldn't create an XKB context.\n");
exit(EXIT_FAILURE);
}
log_debug("Configuring keyboard.\n");
struct xkb_keymap *xkb_keymap = xkb_keymap_new_from_string(
tofi.xkb_context,
tofi.xkb_keymap_string,
XKB_KEYMAP_FORMAT_TEXT_V1,
XKB_KEYMAP_COMPILE_NO_FLAGS);
struct xkb_state *xkb_state = xkb_state_new(xkb_keymap);
xkb_keymap_unref(tofi.xkb_keymap);
xkb_state_unref(tofi.xkb_state);
tofi.xkb_keymap = xkb_keymap;
tofi.xkb_state = xkb_state;
free(tofi.xkb_keymap_string);
tofi.late_keyboard_init = false;
log_debug("Keyboard configured.\n");
}
/*
* Main event loop.
* See the wl_display(3) man page for an explanation of the
* order of the various functions called here.
*/
while (!tofi.closed) {
struct pollfd pollfds[2] = {{0}, {0}};
pollfds[0].fd = wl_display_get_fd(tofi.wl_display);
/* Make sure we're ready to receive events on the main queue. */
while (wl_display_prepare_read(tofi.wl_display) != 0) {
wl_display_dispatch_pending(tofi.wl_display);
}
/* Make sure all our requests have been sent to the server. */
while (wl_display_flush(tofi.wl_display) != 0) {
pollfds[0].events = POLLOUT;
poll(&pollfds[0], 1, -1);
}
/*
* Set time to wait for poll() to -1 (unlimited), unless
* there's some key repeating going on.
*/
int timeout = -1;
if (tofi.repeat.active) {
int64_t wait = (int64_t)tofi.repeat.next - (int64_t)gettime_ms();
if (wait >= 0) {
timeout = wait;
} else {
timeout = 0;
}
}
pollfds[0].events = POLLIN | POLLPRI;
int res;
if (tofi.clipboard.fd == 0) {
res = poll(&pollfds[0], 1, timeout);
} else {
/*
* We're trying to paste from the clipboard, which is
* done by reading from a pipe, so poll that file
* descriptor as well.
*/
pollfds[1].fd = tofi.clipboard.fd;
pollfds[1].events = POLLIN | POLLPRI;
res = poll(pollfds, 2, timeout);
}
if (res == 0) {
/*
* No events to process and no error - we presumably
* have a key repeat to handle.
*/
wl_display_cancel_read(tofi.wl_display);
if (tofi.repeat.active) {
int64_t wait = (int64_t)tofi.repeat.next - (int64_t)gettime_ms();
if (wait <= 0) {
input_handle_keypress(&tofi, tofi.repeat.keycode);
tofi.repeat.next += 1000 / tofi.repeat.rate;
}
}
} else if (res < 0) {
/* There was an error polling the display. */
wl_display_cancel_read(tofi.wl_display);
} else {
if (pollfds[0].revents & (POLLIN | POLLPRI)) {
/* Events to read, so put them on the queue. */
wl_display_read_events(tofi.wl_display);
} else {
/*
* No events to read - we were woken up to
* handle clipboard data.
*/
wl_display_cancel_read(tofi.wl_display);
}
if (pollfds[1].revents & (POLLIN | POLLPRI)) {
/* Read clipboard data. */
if (tofi.clipboard.fd > 0) {
read_clipboard(&tofi);
}
}
if (pollfds[1].revents & POLLHUP) {
/*
* The other end of the clipboard pipe has
* closed, cleanup.
*/
clipboard_finish_paste(&tofi.clipboard);
}
}
/* Handle any events we read. */
wl_display_dispatch_pending(tofi.wl_display);
if (tofi.window.surface.redraw) {
entry_update(&tofi.window.entry);
surface_draw(&tofi.window.surface);
tofi.window.surface.redraw = false;
}
if (tofi.submit) {
tofi.submit = false;
if (do_submit(&tofi)) {
break;
}
}
}
log_debug("Window closed, performing cleanup.\n");
#ifdef DEBUG
/*
* For debug builds, try to cleanup as much as possible, to make using
* e.g. Valgrind easier. There's still a few unavoidable leaks though,
* mostly from Pango, and Cairo holds onto quite a bit of cached data
* (without leaking it)
*/
surface_destroy(&tofi.window.surface);
entry_destroy(&tofi.window.entry);
if (tofi.window.wp_viewport != NULL) {
wp_viewport_destroy(tofi.window.wp_viewport);
}
zwlr_layer_surface_v1_destroy(tofi.window.zwlr_layer_surface);
wl_surface_destroy(tofi.window.surface.wl_surface);
if (tofi.wl_keyboard != NULL) {
wl_keyboard_release(tofi.wl_keyboard);
}
if (tofi.wl_pointer != NULL) {
wl_pointer_release(tofi.wl_pointer);
}
wl_compositor_destroy(tofi.wl_compositor);
if (tofi.clipboard.wl_data_offer != NULL) {
wl_data_offer_destroy(tofi.clipboard.wl_data_offer);
}
wl_data_device_release(tofi.wl_data_device);
wl_data_device_manager_destroy(tofi.wl_data_device_manager);
wl_seat_release(tofi.wl_seat);
{
struct output_list_element *el;
struct output_list_element *tmp;
wl_list_for_each_safe(el, tmp, &tofi.output_list, link) {
wl_list_remove(&el->link);
wl_output_release(el->wl_output);
free(el->name);
free(el);
}
}
wl_shm_destroy(tofi.wl_shm);
if (tofi.wp_fractional_scale_manager != NULL) {
wp_fractional_scale_manager_v1_destroy(tofi.wp_fractional_scale_manager);
}
if (tofi.wp_viewporter != NULL) {
wp_viewporter_destroy(tofi.wp_viewporter);
}
zwlr_layer_shell_v1_destroy(tofi.zwlr_layer_shell);
xkb_state_unref(tofi.xkb_state);
xkb_keymap_unref(tofi.xkb_keymap);
xkb_context_unref(tofi.xkb_context);
wl_registry_destroy(tofi.wl_registry);
if (tofi.window.entry.drun) {
desktop_vec_destroy(&tofi.window.entry.apps);
}
if (tofi.window.entry.command_buffer != NULL) {
free(tofi.window.entry.command_buffer);
}
string_ref_vec_destroy(&tofi.window.entry.commands);
string_ref_vec_destroy(&tofi.window.entry.results);
if (tofi.use_history) {
history_destroy(&tofi.window.entry.history);
}
#endif
/*
* For release builds, skip straight to display disconnection and quit.
*/
wl_display_roundtrip(tofi.wl_display);
wl_display_disconnect(tofi.wl_display);
log_debug("Finished, exiting.\n");
return EXIT_SUCCESS;
}
|