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
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
|
const std = @import("std");
const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const builtin = @import("builtin");
const adw = @import("adw");
const gdk = @import("gdk");
const gio = @import("gio");
const glib = @import("glib");
const gobject = @import("gobject");
const gtk = @import("gtk");
const build_config = @import("../../../build_config.zig");
const i18n = @import("../../../os/main.zig").i18n;
const apprt = @import("../../../apprt.zig");
const cgroup = @import("../cgroup.zig");
const CoreApp = @import("../../../App.zig");
const configpkg = @import("../../../config.zig");
const input = @import("../../../input.zig");
const internal_os = @import("../../../os/main.zig");
const systemd = @import("../../../os/systemd.zig");
const terminal = @import("../../../terminal/main.zig");
const xev = @import("../../../global.zig").xev;
const Binding = @import("../../../input.zig").Binding;
const CoreConfig = configpkg.Config;
const CoreSurface = @import("../../../Surface.zig");
const ext = @import("../ext.zig");
const key = @import("../key.zig");
const adw_version = @import("../adw_version.zig");
const gtk_version = @import("../gtk_version.zig");
const winprotopkg = @import("../winproto.zig");
const ApprtApp = @import("../App.zig");
const Common = @import("../class.zig").Common;
const WeakRef = @import("../weak_ref.zig").WeakRef;
const Config = @import("config.zig").Config;
const Surface = @import("surface.zig").Surface;
const SplitTree = @import("split_tree.zig").SplitTree;
const Window = @import("window.zig").Window;
const CloseConfirmationDialog = @import("close_confirmation_dialog.zig").CloseConfirmationDialog;
const ConfigErrorsDialog = @import("config_errors_dialog.zig").ConfigErrorsDialog;
const GlobalShortcuts = @import("global_shortcuts.zig").GlobalShortcuts;
const log = std.log.scoped(.gtk_ghostty_application);
/// Function used to funnel GLib/GObject/GTK log messages into Zig's logging
/// system rather than just getting dumped directly to stderr.
fn glibLogWriterFunction(
level: glib.LogLevelFlags,
fields: [*]const glib.LogField,
n_fields: usize,
_: ?*anyopaque,
) callconv(.c) glib.LogWriterOutput {
const glib_log = std.log.scoped(.glib);
var message_: ?[]const u8 = null;
var domain_: ?[]const u8 = null;
for (0..n_fields) |i| {
const field = fields[i];
const k = std.mem.span(field.f_key orelse continue);
const v: []const u8 = v: {
if (field.f_length >= 0) {
const v: [*]const u8 = @ptrCast(field.f_value orelse continue);
break :v v[0..@intCast(field.f_length)];
}
const v: [*:0]const u8 = @ptrCast(field.f_value orelse continue);
break :v std.mem.span(v);
};
if (std.mem.eql(u8, k, "MESSAGE")) {
message_ = v;
continue;
}
if (std.mem.eql(u8, k, "GLIB_DOMAIN")) {
domain_ = v;
continue;
}
}
const message = message_ orelse return .unhandled;
const domain = domain_ orelse "«unknown»";
if (level.level_error) {
glib_log.err("ERROR: {s}: {s}", .{ domain, message });
return .handled;
}
if (level.level_critical) {
glib_log.err("CRITICAL: {s}: {s}", .{ domain, message });
return .handled;
}
if (level.level_warning) {
glib_log.warn("WARNING: {s}: {s}", .{ domain, message });
return .handled;
}
if (level.level_message) {
glib_log.info("MESSAGE: {s}: {s}", .{ domain, message });
return .handled;
}
if (level.level_info) {
glib_log.info("INFO: {s}: {s}", .{ domain, message });
return .handled;
}
if (level.level_debug) {
glib_log.debug("DEBUG: {s}: {s}", .{ domain, message });
return .handled;
}
glib_log.debug("UNKNOWN: {s}: {s}", .{ domain, message });
return .handled;
}
/// The primary entrypoint for the Ghostty GTK application.
///
/// This requires a `ghostty.App` and `ghostty.Config` and takes
/// care of the rest. Call `run` to run the application to completion.
pub const Application = extern struct {
/// This type creates a new GObject class. Since the Application is
/// the primary entrypoint I'm going to use this as a place to document
/// how this all works and where you can find resources for it, but
/// this applies to any other GObject class within this apprt.
///
/// The various fields (parent_instance) and constants (Parent,
/// getGObjectType, etc.) are mandatory "interfaces" for zig-gobject
/// to create a GObject class.
///
/// I found these to be the best resources:
///
/// * https://github.com/ianprime0509/zig-gobject/blob/d7f1edaf50193d49b56c60568dfaa9f23195565b/extensions/gobject2.zig
/// * https://github.com/ianprime0509/zig-gobject/blob/d7f1edaf50193d49b56c60568dfaa9f23195565b/example/src/custom_class.zig
///
const Self = @This();
parent_instance: Parent,
pub const Parent = adw.Application;
pub const getGObjectType = gobject.ext.defineClass(Self, .{
.name = "GhosttyApplication",
.classInit = &Class.init,
.parent_class = &Class.parent,
.private = .{ .Type = Private, .offset = &Private.offset },
});
pub const properties = struct {
pub const config = struct {
pub const name = "config";
const impl = gobject.ext.defineProperty(
"config",
Self,
?*Config,
.{
.accessor = gobject.ext.typedAccessor(
Self,
?*Config,
.{
.getter = Self.getConfig,
.getter_transfer = .full,
},
),
},
);
};
};
const Private = struct {
/// The apprt App. This is annoying that we need this it'd be
/// nicer to just make THIS the apprt app but the current libghostty
/// API doesn't allow that.
rt_app: *ApprtApp,
/// The libghostty App instance.
core_app: *CoreApp,
/// The configuration for the application.
config: *Config,
/// State and logic for the underlying windowing protocol.
winproto: winprotopkg.App,
/// The global shortcut logic.
global_shortcuts: *GlobalShortcuts,
/// The base path of the transient cgroup used to put all surfaces
/// into their own cgroup. This is only set if cgroups are enabled
/// and initialization was successful.
transient_cgroup_base: ?[]const u8 = null,
/// This is set to true so long as we request a window exactly
/// once. This prevents quitting the app before we've shown one
/// window.
requested_window: bool = false,
/// This is set to false internally when the event loop
/// should exit and the application should quit. This must
/// only be set by the main loop thread.
running: bool = false,
/// The timer used to quit the application after the last window is
/// closed. Even if there is no quit delay set, this is the state
/// used to determine to close the app.
quit_timer: union(enum) {
off,
active: c_uint,
expired,
} = .off,
/// If non-null, we're currently showing a config errors dialog.
/// This is a WeakRef because the dialog can close on its own
/// outside of our own lifecycle and that's okay.
config_errors_dialog: WeakRef(ConfigErrorsDialog) = .empty,
/// glib source for our signal handler.
signal_source: ?c_uint = null,
/// CSS Provider for any styles based on Ghostty configuration values.
css_provider: *gtk.CssProvider,
/// Providers for loading custom stylesheets defined by user
custom_css_providers: std.ArrayListUnmanaged(*gtk.CssProvider) = .empty,
pub var offset: c_int = 0;
};
/// Get this application as the default, allowing access to its
/// properties globally.
///
/// This asserts that there is a default application and that the
/// default application is a GhosttyApplication. The program would have
/// to be in a very bad state for this to be violated.
pub fn default() *Self {
const app = gio.Application.getDefault().?;
return gobject.ext.cast(Self, app).?;
}
/// Creates a new Application instance.
///
/// This does a lot more work than a typical class instantiation,
/// because we expect that this is the main program entrypoint.
///
/// The only failure mode of initializing the application is early OOM.
/// Early OOM can't be recovered from. Every other error is mapped to
/// some degraded state where we can at least show a window with an error.
pub fn new(
rt_app: *ApprtApp,
core_app: *CoreApp,
) Allocator.Error!*Self {
const alloc = core_app.alloc;
// Capture GLib/GObject/GTK log messages and funnel them through Zig's
// logging system rather than just getting dumped directly to stderr.
_ = glib.logSetWriterFunc(glibLogWriterFunction, null, null);
// Log our GTK versions
gtk_version.logVersion();
adw_version.logVersion();
// Set gettext global domain to be our app so that our unqualified
// translations map to our translations.
internal_os.i18n.initGlobalDomain() catch |err| {
// Failures shuldn't stop application startup. Our app may
// not translate correctly but it should still work. In the
// future we may want to add this to the GUI to show.
log.warn("i18n initialization failed error={}", .{err});
};
// Load our configuration.
var config = CoreConfig.load(alloc) catch |err| err: {
// If we fail to load the configuration, then we should log
// the error in the diagnostics so it can be shown to the user.
// We can still load a default which only fails for OOM, allowing
// us to startup.
var def: CoreConfig = try .default(alloc);
errdefer def.deinit();
try def.addDiagnosticFmt(
"error loading user configuration: {}",
.{err},
);
break :err def;
};
defer config.deinit();
// Setup our GTK init env vars
setGtkEnv(&config) catch |err| switch (err) {
error.NoSpaceLeft => {
// If we fail to set GTK environment variables then we still
// try to start the application...
log.warn(
"error setting GTK environment variables err={}",
.{err},
);
},
};
adw.init();
const single_instance = switch (config.@"gtk-single-instance") {
.true => true,
.false => false,
// This should have been resolved to true/false during config loading.
.detect => unreachable,
};
// Setup the flags for our application.
const app_flags: gio.ApplicationFlags = app_flags: {
var flags: gio.ApplicationFlags = .flags_default_flags;
if (!single_instance) flags.non_unique = true;
break :app_flags flags;
};
// Our app ID determines uniqueness and maps to our desktop file.
// We append "-debug" to the ID if we're in debug mode so that we
// can develop Ghostty in Ghostty.
const app_id: [:0]const u8 = app_id: {
if (config.class) |class| {
if (gio.Application.idIsValid(class) != 0) {
break :app_id class;
} else {
log.warn("invalid 'class' in config, ignoring", .{});
}
}
break :app_id ApprtApp.application_id;
};
const display: *gdk.Display = gdk.Display.getDefault() orelse {
// I'm unsure of any scenario where this happens. Because we don't
// want to litter null checks everywhere, we just exit here.
log.warn("gdk display is null, exiting", .{});
std.posix.exit(1);
};
// Setup our windowing protocol logic
var wp: winprotopkg.App = winprotopkg.App.init(
alloc,
display,
app_id,
&config,
) catch |err| wp: {
// If we fail to detect or setup the windowing protocol
// specifies, we fallback to a noop implementation so we can
// still launch.
log.warn("error initializing windowing protocol err={}", .{err});
break :wp .{ .none = .{} };
};
errdefer wp.deinit(alloc);
log.debug("windowing protocol={s}", .{@tagName(wp)});
// Create our GTK Application which encapsulates our process.
log.debug("creating GTK application id={s} single-instance={}", .{
app_id,
single_instance,
});
// Wrap our configuration in a GObject.
const config_obj: *Config = try .new(alloc, &config);
errdefer config_obj.unref();
// Internally, GTK ensures that only one instance of this provider
// exists in the provider list for the display.
const css_provider = gtk.CssProvider.new();
gtk.StyleContext.addProviderForDisplay(
display,
css_provider.as(gtk.StyleProvider),
gtk.STYLE_PROVIDER_PRIORITY_APPLICATION + 3,
);
errdefer css_provider.unref();
// Initialize the app.
const self = gobject.ext.newInstance(Self, .{
.application_id = app_id.ptr,
.flags = app_flags,
// Force the resource path to a known value so it doesn't depend
// on the app id (which changes between debug/release and can be
// user-configured) and force it to load in compiled resources.
.resource_base_path = "/com/mitchellh/ghostty",
});
// Setup our private state. More setup is done in the init
// callback that GObject calls, but we can't pass this data through
// to there (and we don't need it there directly) so this is here.
const priv = self.private();
priv.* = .{
.rt_app = rt_app,
.core_app = core_app,
.config = config_obj,
.winproto = wp,
.css_provider = css_provider,
.custom_css_providers = .empty,
.global_shortcuts = gobject.ext.newInstance(GlobalShortcuts, .{}),
};
// Signals
_ = gobject.Object.signals.notify.connect(
self,
*Self,
propConfig,
self,
.{ .detail = "config" },
);
_ = gtk.CssProvider.signals.parsing_error.connect(
css_provider,
*Self,
signalCssParsingError,
self,
.{},
);
// Trigger initial config changes
self.as(gobject.Object).notifyByPspec(properties.config.impl.param_spec);
return self;
}
/// Force deinitialize the application.
///
/// Normally in a GObject lifecycle, this would be called by the
/// finalizer. But applications are never fully unreferenced so this
/// ensures that our memory is cleaned up properly.
pub fn deinit(self: *Self) void {
const alloc = self.allocator();
const priv = self.private();
priv.config.unref();
priv.winproto.deinit(alloc);
priv.global_shortcuts.unref();
if (priv.transient_cgroup_base) |base| alloc.free(base);
if (gdk.Display.getDefault()) |display| {
gtk.StyleContext.removeProviderForDisplay(
display,
priv.css_provider.as(gtk.StyleProvider),
);
for (priv.custom_css_providers.items) |provider| {
gtk.StyleContext.removeProviderForDisplay(
display,
provider.as(gtk.StyleProvider),
);
}
}
priv.css_provider.unref();
for (priv.custom_css_providers.items) |provider| provider.unref();
priv.custom_css_providers.deinit(alloc);
}
/// The global allocator that all other classes should use by
/// calling `Application.default().allocator()`. Zig code should prefer
/// this wherever possible so we get leak detection in debug/tests.
pub fn allocator(self: *Self) std.mem.Allocator {
return self.private().core_app.alloc;
}
/// Run the application. This is a replacement for `gio.Application.run`
/// because we want more tight control over our event loop so we can
/// integrate it with libghostty.
pub fn run(self: *Self) !void {
// Based on the actual `gio.Application.run` implementation:
// https://github.com/GNOME/glib/blob/a8e8b742e7926e33eb635a8edceac74cf239d6ed/gio/gapplication.c#L2533
// Acquire the default context for the application
const ctx = glib.MainContext.default();
if (glib.MainContext.acquire(ctx) == 0) return error.ContextAcquireFailed;
// The final cleanup that is always required at the end of running.
defer {
// Ensure our timer source is removed
self.stopQuitTimer();
// Sync any remaining settings
gio.Settings.sync();
// Clear out the event loop, don't block.
while (glib.MainContext.iteration(ctx, 0) != 0) {}
// Release the context so something else can use it.
defer glib.MainContext.release(ctx);
}
// Register the application
var err_: ?*glib.Error = null;
if (self.as(gio.Application).register(
null,
&err_,
) == 0) {
if (err_) |err| {
defer err.free();
log.warn(
"error registering application: {s}",
.{err.f_message orelse "(unknown)"},
);
}
return error.ApplicationRegisterFailed;
}
assert(err_ == null);
// This just calls the `activate` signal but its part of the normal startup
// routine so we just call it, but only if the config allows it (this allows
// for launching Ghostty in the "background" without immediately opening
// a window).
//
// https://gitlab.gnome.org/GNOME/glib/-/blob/bd2ccc2f69ecfd78ca3f34ab59e42e2b462bad65/gio/gapplication.c#L2302
const priv = self.private();
{
// We need to scope any config access because once we run our
// event loop, this can change out from underneath us.
const config = priv.config.get();
if (config.@"initial-window") self.as(gio.Application).activate();
}
// If we are NOT the primary instance, then we never want to run.
// This means that another instance of the GTK app is running.
if (self.as(gio.Application).getIsRemote() != 0) {
log.debug(
"application is remote, exiting run loop after activation",
.{},
);
return;
}
// Tell systemd that we are ready.
systemd.notify.ready();
log.debug("entering runloop", .{});
defer log.debug("exiting runloop", .{});
priv.running = true;
while (priv.running) {
_ = glib.MainContext.iteration(ctx, 1);
// Tick the core Ghostty terminal app
try priv.core_app.tick(priv.rt_app);
// Check if we must quit based on the current state.
const must_quit = q: {
// If we are configured to always stay running, don't quit.
const config = priv.config.get();
if (!config.@"quit-after-last-window-closed") break :q false;
// If the quit timer has expired, quit.
if (priv.quit_timer == .expired) {
log.debug("must_quit due to quit timer expired", .{});
break :q true;
}
// If we have no windows attached to our app, also quit.
// We only do this if we don't have the closed delay set,
// because with the closed delay set we'll exit eventually.
if (config.@"quit-after-last-window-closed-delay" == null) {
if (priv.requested_window and @as(
?*glib.List,
self.as(gtk.Application).getWindows(),
) == null) {
log.debug("must_quit due to no app windows", .{});
break :q true;
}
}
// No quit conditions met
break :q false;
};
if (must_quit) {
// All must quit scenarios do not need confirmation.
// Furthermore, must quit scenarios may result in a situation
// where its unsafe to even access the app/surface memory
// since its in the process of being freed. We must simply
// begin our exit immediately.
self.quitNow();
}
}
}
/// Quit the application. This will start the process to stop the
/// run loop. It will not `posix.exit`.
pub fn quit(self: *Self) void {
const priv = self.private();
// If our run loop has already exited then we are done.
if (!priv.running) return;
// If our core app doesn't need to confirm quit then we
// can exit immediately.
if (!priv.core_app.needsConfirmQuit()) {
self.quitNow();
return;
}
// Get the parent for our dialog
const parent: ?*gtk.Widget = parent: {
const list = gtk.Window.listToplevels();
defer list.free();
const focused = @as(?*glib.List, list.findCustom(
null,
findActiveWindow,
)) orelse {
// If we have an active surface then we should have
// a window available but in the rare case we don't we
// should exit so we don't crash.
break :parent null;
};
break :parent @ptrCast(@alignCast(focused.f_data));
};
// Show a confirmation dialog
const dialog: *CloseConfirmationDialog = .new(.app);
_ = CloseConfirmationDialog.signals.@"close-request".connect(
dialog,
*Application,
handleCloseConfirmation,
self,
.{},
);
// Show it
dialog.present(parent);
}
fn quitNow(self: *Self) void {
// Get all our windows and destroy them, forcing them to free.
const list = gtk.Window.listToplevels();
defer list.free();
list.foreach(struct {
fn callback(data: ?*anyopaque, _: ?*anyopaque) callconv(.c) void {
const ptr = data orelse return;
const window: *gtk.Window = @ptrCast(@alignCast(ptr));
// We only want to destroy our windows. These windows own
// every other type of window that is possible so this will
// trigger a proper shutdown sequence.
//
// We previously just destroyed ALL windows but this leads to
// a double-free with the fcitx ime, because it has a nested
// gtk.Window as a property that we don't own and it later
// tries to free on its own. I think this is probably a bug in
// the fcitx ime widget but still, we don't want a double free!
if (gobject.ext.isA(window, Window)) {
window.destroy();
}
}
}.callback, null);
// Trigger our runloop exit.
self.private().running = false;
}
/// apprt API to perform an action.
pub fn performAction(
self: *Self,
target: apprt.Target,
comptime action: apprt.Action.Key,
value: apprt.Action.Value(action),
) !bool {
switch (action) {
.close_tab => return Action.closeTab(target, value),
.close_window => return Action.closeWindow(target),
.config_change => try Action.configChange(
self,
target,
value.config,
),
.desktop_notification => Action.desktopNotification(self, target, value),
.equalize_splits => return Action.equalizeSplits(target),
.goto_split => return Action.gotoSplit(target, value),
.goto_tab => return Action.gotoTab(target, value),
.initial_size => return Action.initialSize(target, value),
.inspector => return Action.controlInspector(target, value),
.mouse_over_link => Action.mouseOverLink(target, value),
.mouse_shape => Action.mouseShape(target, value),
.mouse_visibility => Action.mouseVisibility(target, value),
.move_tab => return Action.moveTab(target, value),
.new_split => return Action.newSplit(target, value),
.new_tab => return Action.newTab(target),
.new_window => try Action.newWindow(
self,
switch (target) {
.app => null,
.surface => |v| v,
},
),
.open_config => return Action.openConfig(self),
.open_url => Action.openUrl(self, value),
.pwd => Action.pwd(target, value),
.present_terminal => return Action.presentTerminal(target),
.progress_report => return Action.progressReport(target, value),
.prompt_title => return Action.promptTitle(target),
.quit => self.quit(),
.quit_timer => try Action.quitTimer(self, value),
.reload_config => try Action.reloadConfig(self, target, value),
.render => Action.render(target),
.resize_split => return Action.resizeSplit(target, value),
.ring_bell => Action.ringBell(target),
.set_title => Action.setTitle(target, value),
.show_child_exited => return Action.showChildExited(target, value),
.show_gtk_inspector => Action.showGtkInspector(),
.size_limit => return Action.sizeLimit(target, value),
.toggle_maximize => Action.toggleMaximize(target),
.toggle_fullscreen => Action.toggleFullscreen(target),
.toggle_quick_terminal => return Action.toggleQuickTerminal(self),
.toggle_tab_overview => return Action.toggleTabOverview(target),
.toggle_window_decorations => return Action.toggleWindowDecorations(target),
.toggle_command_palette => return Action.toggleCommandPalette(target),
.toggle_split_zoom => return Action.toggleSplitZoom(target),
.show_on_screen_keyboard => return Action.showOnScreenKeyboard(target),
.command_finished => return Action.commandFinished(target, value),
// Unimplemented
.secure_input,
.close_all_windows,
.float_window,
.toggle_visibility,
.cell_size,
.key_sequence,
.render_inspector,
.renderer_health,
.color_change,
.reset_window_size,
.check_for_updates,
.undo,
.redo,
=> {
log.warn("unimplemented action={}", .{action});
return false;
},
}
// Assume it was handled. The unhandled case must be explicit
// in the switch above.
return true;
}
/// Returns the core app associated with this application. This is
/// not a reference-counted type so you should not store this.
pub fn core(self: *Self) *CoreApp {
return self.private().core_app;
}
/// Returns the apprt application associated with this application.
pub fn rt(self: *Self) *ApprtApp {
return self.private().rt_app;
}
/// Returns the app winproto implementation.
pub fn winproto(self: *Self) *winprotopkg.App {
return &self.private().winproto;
}
/// Returns the cgroup base (if any).
pub fn cgroupBase(self: *Self) ?[]const u8 {
return self.private().transient_cgroup_base;
}
/// This will get called when there are no more open surfaces.
fn startQuitTimer(self: *Self) void {
const priv = self.private();
const config = priv.config.get();
// Cancel any previous timer.
self.stopQuitTimer();
// This is a no-op unless we are configured to quit after last window is closed.
if (!config.@"quit-after-last-window-closed") return;
// If a delay is configured, set a timeout function to quit after the delay.
if (config.@"quit-after-last-window-closed-delay") |v| {
priv.quit_timer = .{
.active = glib.timeoutAdd(
v.asMilliseconds(),
handleQuitTimerExpired,
self,
),
};
} else {
// If no delay is configured, treat it as expired.
priv.quit_timer = .expired;
}
}
/// This will get called when a new surface gets opened.
fn stopQuitTimer(self: *Self) void {
const priv = self.private();
switch (priv.quit_timer) {
.off => {},
.expired => priv.quit_timer = .off,
.active => |source| {
if (glib.Source.remove(source) == 0) {
log.warn(
"unable to remove quit timer source={d}",
.{source},
);
}
priv.quit_timer = .off;
},
}
}
fn loadRuntimeCss(self: *Self) (Allocator.Error || std.Io.Writer.Error)!void {
const alloc = self.allocator();
const priv: *Private = self.private();
const config = priv.config.get();
var buf: std.Io.Writer.Allocating = try .initCapacity(alloc, 2048);
defer buf.deinit();
const writer = &buf.writer;
// Load standard css first as it can override some of the user configured styling.
try loadRuntimeCss414(config, writer);
try loadRuntimeCss416(config, writer);
const unfocused_fill: CoreConfig.Color = config.@"unfocused-split-fill" orelse config.background;
try writer.print(
\\widget.unfocused-split {{
\\ opacity: {d:.2};
\\ background-color: rgb({d},{d},{d});
\\}}
\\
, .{
1.0 - config.@"unfocused-split-opacity",
unfocused_fill.r,
unfocused_fill.g,
unfocused_fill.b,
});
if (config.@"split-divider-color") |color| {
try writer.print(
\\.window .split paned > separator {{
\\ color: rgb({[r]d},{[g]d},{[b]d});
\\ background: rgb({[r]d},{[g]d},{[b]d});
\\}}
\\
, .{
.r = color.r,
.g = color.g,
.b = color.b,
});
}
if (config.@"window-title-font-family") |font_family| {
try writer.print(
\\.window headerbar {{
\\ font-family: "{[font_family]s}";
\\}}
\\
, .{ .font_family = font_family });
}
const contents = buf.written();
log.debug("runtime CSS is {d} bytes", .{contents.len});
const bytes = glib.Bytes.new(contents.ptr, contents.len);
defer bytes.unref();
// Clears any previously loaded CSS from this provider
priv.css_provider.loadFromBytes(bytes);
}
/// Load runtime CSS for older than GTK 4.16
fn loadRuntimeCss414(
config: *const CoreConfig,
writer: *std.Io.Writer,
) std.Io.Writer.Error!void {
if (gtk_version.runtimeAtLeast(4, 16, 0)) return;
const window_theme = config.@"window-theme";
const headerbar_background = config.@"window-titlebar-background" orelse config.background;
const headerbar_foreground = config.@"window-titlebar-foreground" orelse config.foreground;
switch (window_theme) {
.ghostty => try writer.print(
\\windowhandle {{
\\ background-color: rgb({d},{d},{d});
\\ color: rgb({d},{d},{d});
\\}}
\\windowhandle:backdrop {{
\\ background-color: oklab(from rgb({d},{d},{d}) calc(l * 0.9) a b / alpha);
\\}}
\\
, .{
headerbar_background.r,
headerbar_background.g,
headerbar_background.b,
headerbar_foreground.r,
headerbar_foreground.g,
headerbar_foreground.b,
headerbar_background.r,
headerbar_background.g,
headerbar_background.b,
}),
else => {},
}
}
/// Load runtime for GTK 4.16 and newer
fn loadRuntimeCss416(
config: *const CoreConfig,
writer: *std.Io.Writer,
) std.Io.Writer.Error!void {
if (gtk_version.runtimeUntil(4, 16, 0)) return;
const window_theme = config.@"window-theme";
const headerbar_background = config.@"window-titlebar-background" orelse config.background;
const headerbar_foreground = config.@"window-titlebar-foreground" orelse config.foreground;
try writer.writeAll(
\\/*
\\ * Child Exited Overlay
\\ */
\\
\\.child-exited.normal revealer widget {
\\ background-color: color-mix(
\\ in srgb,
\\ var(--success-bg-color),
\\ transparent 50%
\\ );
\\}
\\
\\.child-exited.abnormal revealer widget {
\\ background-color: color-mix(
\\ in srgb,
\\ var(--error-bg-color),
\\ transparent 50%
\\ );
\\}
\\
\\/*
\\ * Surface
\\ */
\\
\\.surface progressbar.error trough progress {
\\ background-color: color-mix(
\\ in srgb,
\\ var(--error-bg-color),
\\ transparent 50%
\\ );
\\}
\\
\\.surface .bell-overlay {
\\ border-color: color-mix(
\\ in srgb,
\\ var(--accent-color),
\\ transparent 50%
\\ );
\\}
\\
\\/*
\\ * Splits
\\ */
\\
\\.window .split paned > separator {
\\ background-color: color-mix(
\\ in srgb,
\\ var(--window-bg-color),
\\ transparent 0%
\\ );
\\}
\\
);
switch (window_theme) {
.ghostty => try writer.print(
\\:root {{
\\ --ghostty-fg: rgb({d},{d},{d});
\\ --ghostty-bg: rgb({d},{d},{d});
\\ --headerbar-fg-color: var(--ghostty-fg);
\\ --headerbar-bg-color: var(--ghostty-bg);
\\ --headerbar-backdrop-color: oklab(from var(--headerbar-bg-color) calc(l * 0.9) a b / alpha);
\\ --overview-fg-color: var(--ghostty-fg);
\\ --overview-bg-color: var(--ghostty-bg);
\\ --popover-fg-color: var(--ghostty-fg);
\\ --popover-bg-color: var(--ghostty-bg);
\\ --window-fg-color: var(--ghostty-fg);
\\ --window-bg-color: var(--ghostty-bg);
\\}}
\\windowhandle {{
\\ background-color: var(--headerbar-bg-color);
\\ color: var(--headerbar-fg-color);
\\}}
\\windowhandle:backdrop {{
\\ background-color: var(--headerbar-backdrop-color);
\\}}
, .{
headerbar_foreground.r,
headerbar_foreground.g,
headerbar_foreground.b,
headerbar_background.r,
headerbar_background.g,
headerbar_background.b,
}),
else => {},
}
}
fn loadCustomCss(self: *Self) (std.fs.File.ReadError || Allocator.Error)!void {
const priv: *Private = self.private();
const alloc = self.allocator();
const display = gdk.Display.getDefault() orelse {
log.warn("unable to get display", .{});
return;
};
// unload the previously loaded style providers
for (priv.custom_css_providers.items) |provider| {
gtk.StyleContext.removeProviderForDisplay(
display,
provider.as(gtk.StyleProvider),
);
provider.unref();
}
priv.custom_css_providers.clearRetainingCapacity();
const config = priv.config.get();
for (config.@"gtk-custom-css".value.items) |p| {
const path, const optional = switch (p) {
.optional => |path| .{ path, true },
.required => |path| .{ path, false },
};
const file = std.fs.openFileAbsolute(path, .{}) catch |err| {
if (err != error.FileNotFound or !optional) {
log.warn(
"error opening gtk-custom-css file {s}: {}",
.{ path, err },
);
}
continue;
};
defer file.close();
const css_file_size_limit = 5 * 1024 * 1024; // 5MB
log.info("loading gtk-custom-css path={s}", .{path});
const contents = file.readToEndAlloc(
alloc,
css_file_size_limit,
) catch |err| switch (err) {
error.FileTooBig => {
log.warn("gtk-custom-css file {s} was larger than {Bi}", .{ path, css_file_size_limit });
continue;
},
else => |e| return e,
};
defer alloc.free(contents);
const bytes = glib.Bytes.new(contents.ptr, contents.len);
defer bytes.unref();
const css_provider = gtk.CssProvider.new();
errdefer css_provider.unref();
_ = gtk.CssProvider.signals.parsing_error.connect(
css_provider,
*Self,
signalCssParsingError,
self,
.{},
);
try priv.custom_css_providers.append(alloc, css_provider);
css_provider.loadFromBytes(bytes);
gtk.StyleContext.addProviderForDisplay(
display,
css_provider.as(gtk.StyleProvider),
gtk.STYLE_PROVIDER_PRIORITY_USER,
);
}
}
fn syncActionAccelerators(self: *Self) void {
self.syncActionAccelerator("app.quit", .{ .quit = {} });
self.syncActionAccelerator("app.open-config", .{ .open_config = {} });
self.syncActionAccelerator("app.reload-config", .{ .reload_config = {} });
self.syncActionAccelerator("win.toggle-inspector", .{ .inspector = .toggle });
self.syncActionAccelerator("app.show-gtk-inspector", .show_gtk_inspector);
self.syncActionAccelerator("win.toggle-command-palette", .toggle_command_palette);
self.syncActionAccelerator("win.close", .{ .close_window = {} });
self.syncActionAccelerator("win.new-window", .{ .new_window = {} });
self.syncActionAccelerator("win.new-tab", .{ .new_tab = {} });
self.syncActionAccelerator("win.close-tab::this", .{ .close_tab = .this });
self.syncActionAccelerator("tab.close::this", .{ .close_tab = .this });
self.syncActionAccelerator("win.split-right", .{ .new_split = .right });
self.syncActionAccelerator("win.split-down", .{ .new_split = .down });
self.syncActionAccelerator("win.split-left", .{ .new_split = .left });
self.syncActionAccelerator("win.split-up", .{ .new_split = .up });
self.syncActionAccelerator("win.copy", .{ .copy_to_clipboard = {} });
self.syncActionAccelerator("win.paste", .{ .paste_from_clipboard = {} });
self.syncActionAccelerator("win.reset", .{ .reset = {} });
self.syncActionAccelerator("win.clear", .{ .clear_screen = {} });
self.syncActionAccelerator("win.prompt-title", .{ .prompt_surface_title = {} });
self.syncActionAccelerator("split-tree.new-split::left", .{ .new_split = .left });
self.syncActionAccelerator("split-tree.new-split::right", .{ .new_split = .right });
self.syncActionAccelerator("split-tree.new-split::up", .{ .new_split = .up });
self.syncActionAccelerator("split-tree.new-split::down", .{ .new_split = .down });
}
fn syncActionAccelerator(
self: *Self,
gtk_action: [:0]const u8,
action: input.Binding.Action,
) void {
const gtk_app = self.as(gtk.Application);
// Reset it initially
const zero = [_:null]?[*:0]const u8{};
gtk_app.setAccelsForAction(gtk_action, &zero);
const config = self.private().config.get();
const trigger = config.keybind.set.getTrigger(action) orelse return;
var buf: [1024]u8 = undefined;
const accel = if (key.accelFromTrigger(
&buf,
trigger,
)) |accel_|
accel_ orelse return
else |err| switch (err) {
// This should really never, never happen. Its not critical enough
// to actually crash, but this is a bug somewhere. An accelerator
// for a trigger can't possibly be more than 1024 bytes.
error.WriteFailed => {
log.warn("accelerator somehow longer than 1024 bytes: {f}", .{trigger});
return;
},
};
const accels = [_:null]?[*:0]const u8{accel};
gtk_app.setAccelsForAction(gtk_action, &accels);
}
//---------------------------------------------------------------
// Properties
/// Returns the configuration for this application.
///
/// The reference count is increased.
pub fn getConfig(self: *Self) *Config {
return self.private().config.ref();
}
/// Set the configuration for this application. The reference count
/// is increased on the new configuration and the old one is
/// unreferenced.
///
/// If the config has errors this may show the config errors dialog.
fn setConfig(self: *Self, config: *Config) void {
const priv = self.private();
priv.config.unref();
priv.config = config.ref();
self.as(gobject.Object).notifyByPspec(properties.config.impl.param_spec);
// Show our errors if we have any
self.showConfigErrorsDialog();
}
fn propConfig(
_: *Application,
_: *gobject.ParamSpec,
self: *Self,
) callconv(.c) void {
// Sync our accelerators for menu items.
self.syncActionAccelerators();
// Load our runtime and custom CSS. If this fails then our window is
// just stuck with the old CSS but we don't want to fail the entire
// config change operation.
self.loadRuntimeCss() catch |err| switch (err) {
error.WriteFailed, error.OutOfMemory => log.warn(
"out of memory loading runtime CSS, no runtime CSS applied",
.{},
),
};
self.loadCustomCss() catch |err| {
log.warn(
"failed to load custom CSS, no custom CSS applied, err={}",
.{err},
);
};
}
/// Log CSS parsing error
fn signalCssParsingError(
_: *gtk.CssProvider,
css_section: *gtk.CssSection,
err: *glib.Error,
_: *Self,
) callconv(.c) void {
const location = css_section.toString();
defer glib.free(location);
if (comptime gtk_version.atLeast(4, 16, 0)) bytes: {
const bytes = css_section.getBytes() orelse break :bytes;
var len: usize = undefined;
const ptr = bytes.getData(&len) orelse break :bytes;
const data = ptr[0..len];
log.warn("css parsing failed at {s}: {s} {d} {s}\n{s}", .{
location,
glib.quarkToString(err.f_domain),
err.f_code,
err.f_message orelse "«unknown»",
data,
});
return;
}
log.warn("css parsing failed at {s}: {s} {d} {s}", .{
location,
glib.quarkToString(err.f_domain),
err.f_code,
err.f_message orelse "«unknown»",
});
}
//---------------------------------------------------------------
// Libghostty Callbacks
pub fn wakeup(self: *Self) void {
_ = self;
glib.MainContext.wakeup(null);
}
//---------------------------------------------------------------
// Virtual Methods
fn startup(self: *Self) callconv(.c) void {
log.debug("startup", .{});
gio.Application.virtual_methods.startup.call(
Class.parent,
self.as(Parent),
);
// Set ourselves as the default application.
gio.Application.setDefault(self.as(gio.Application));
// Setup our event loop
self.startupXev();
// Setup our style manager (light/dark mode)
self.startupStyleManager();
// Setup some signal handlers
self.startupSignals();
// Setup our action map
self.startupActionMap();
// Setup our global shortcuts
self.startupGlobalShortcuts();
// Setup our cgroup for the application.
self.startupCgroup() catch |err| {
log.warn("cgroup initialization failed err={}", .{err});
// Add it to our config diagnostics so it shows up in a GUI dialog.
// Admittedly this has two issues: (1) we shuldn't be using the
// config errors dialog for this long term and (2) using a mut
// ref to the config wouldn't propagate changes to UI properly,
// but we're in startup mode so its okay.
const config = self.private().config.getMut();
config.addDiagnosticFmt(
"cgroup initialization failed: {}",
.{err},
) catch {};
};
// If we have any config diagnostics from loading, then we
// show the diagnostics dialog. We show this one as a general
// modal (not to any specific window) because we don't even
// know if the window will load.
self.showConfigErrorsDialog();
}
/// Configure libxev to use a specific backend.
///
/// This must be called before any other xev APIs are used.
fn startupXev(self: *Self) void {
const priv = self.private();
const config = priv.config.get();
// If our backend is auto then we have no setup to do.
if (config.@"async-backend" == .auto) return;
// Setup our event loop backend to the preferred method
const result: bool = switch (config.@"async-backend") {
.auto => unreachable,
.epoll => if (comptime xev.dynamic) xev.prefer(.epoll) else false,
.io_uring => if (comptime xev.dynamic) xev.prefer(.io_uring) else false,
};
if (result) {
log.info(
"libxev manual backend={s}",
.{@tagName(xev.backend)},
);
} else {
log.warn(
"libxev manual backend failed, using default={s}",
.{@tagName(xev.backend)},
);
}
}
/// Setup the style manager on startup. The primary task here is to
/// setup our initial light/dark mode based on the configuration and
/// setup listeners for changes to the style manager.
fn startupStyleManager(self: *Self) void {
const priv = self.private();
const config = priv.config.get();
// Setup our initial light/dark
const style = self.as(adw.Application).getStyleManager();
style.setColorScheme(switch (config.@"window-theme") {
.auto, .ghostty => auto: {
const lum = config.background.toTerminalRGB().perceivedLuminance();
break :auto if (lum > 0.5)
.prefer_light
else
.prefer_dark;
},
.system => .prefer_light,
.dark => .force_dark,
.light => .force_light,
});
// Setup color change notifications
_ = gobject.Object.signals.notify.connect(
style,
*Self,
handleStyleManagerDark,
self,
.{ .detail = "dark" },
);
// Do an initial color scheme sync. This is idempotent and does nothing
// if our current theme matches what libghostty has so its safe to
// call.
handleStyleManagerDark(style, undefined, self);
}
/// Setup signal handlers
fn startupSignals(self: *Self) void {
const priv = self.private();
assert(priv.signal_source == null);
priv.signal_source = glib.unixSignalAdd(
std.posix.SIG.USR2,
handleSigusr2,
self,
);
}
/// Setup our action map.
fn startupActionMap(self: *Self) void {
const t_variant_type = glib.ext.VariantType.newFor(u64);
defer t_variant_type.free();
const as_variant_type = glib.VariantType.new("as");
defer as_variant_type.free();
const actions = [_]ext.actions.Action(Self){
.init("new-window", actionNewWindow, null),
.init("new-window-command", actionNewWindow, as_variant_type),
.init("open-config", actionOpenConfig, null),
.init("present-surface", actionPresentSurface, t_variant_type),
.init("quit", actionQuit, null),
.init("reload-config", actionReloadConfig, null),
};
ext.actions.add(Self, self, &actions);
}
/// Setup our global shortcuts.
fn startupGlobalShortcuts(self: *Self) void {
const priv = self.private();
// On startup, our dbus connection should be available.
priv.global_shortcuts.setDbusConnection(
self.as(gio.Application).getDbusConnection(),
);
// Setup a binding so that the shortcut config always matches the app.
_ = gobject.Object.bindProperty(
self.as(gobject.Object),
"config",
priv.global_shortcuts.as(gobject.Object),
"config",
.{ .sync_create = true },
);
// Setup the signal handler for global shortcut triggers
_ = GlobalShortcuts.signals.trigger.connect(
priv.global_shortcuts,
*Application,
globalShortcutTrigger,
self,
.{},
);
}
const CgroupError = error{
DbusConnectionFailed,
CgroupInitFailed,
};
/// Setup our cgroup for the application, if enabled.
///
/// The setup for cgroups involves creating the cgroup for our
/// application, moving ourselves into it, and storing the base path
/// so that created surfaces can also have their own cgroups.
fn startupCgroup(self: *Self) CgroupError!void {
const priv = self.private();
const config = priv.config.get();
// If cgroup isolation isn't enabled then we don't do this.
if (!switch (config.@"linux-cgroup") {
.never => false,
.always => true,
.@"single-instance" => single: {
const flags = self.as(gio.Application).getFlags();
break :single !flags.non_unique;
},
}) {
log.info(
"cgroup isolation disabled via config={}",
.{config.@"linux-cgroup"},
);
return;
}
// We need a dbus connection to do anything else
const dbus = self.as(gio.Application).getDbusConnection() orelse {
if (config.@"linux-cgroup-hard-fail") {
log.err("dbus connection required for cgroup isolation, exiting", .{});
return error.DbusConnectionFailed;
}
return;
};
const alloc = priv.core_app.alloc;
const path = cgroup.init(alloc, dbus, .{
.memory_high = config.@"linux-cgroup-memory-limit",
.pids_max = config.@"linux-cgroup-processes-limit",
}) catch |err| {
// If we can't initialize cgroups then that's okay. We
// want to continue to run so we just won't isolate surfaces.
// NOTE(mitchellh): do we want a config to force it?
log.warn(
"failed to initialize cgroups, terminals will not be isolated err={}",
.{err},
);
// If we have hard fail enabled then we exit now.
if (config.@"linux-cgroup-hard-fail") {
log.err("linux-cgroup-hard-fail enabled, exiting", .{});
return error.CgroupInitFailed;
}
return;
};
log.info("cgroup isolation enabled base={s}", .{path});
priv.transient_cgroup_base = path;
}
fn activate(self: *Self) callconv(.c) void {
log.debug("activate", .{});
// Queue a new window
const priv = self.private();
_ = priv.core_app.mailbox.push(.{
.new_window = .{},
}, .{ .forever = {} });
// Call the parent activate method.
gio.Application.virtual_methods.activate.call(
Class.parent,
self.as(Parent),
);
}
fn dispose(self: *Self) callconv(.c) void {
const priv = self.private();
if (priv.config_errors_dialog.get()) |diag| {
diag.close();
diag.unref(); // strong ref from get()
}
priv.config_errors_dialog.set(null);
if (priv.signal_source) |v| {
if (glib.Source.remove(v) == 0) {
log.warn("unable to remove signal source", .{});
}
priv.signal_source = null;
}
gobject.Object.virtual_methods.dispose.call(
Class.parent,
self.as(Parent),
);
}
fn finalize(self: *Self) callconv(.c) void {
self.deinit();
gobject.Object.virtual_methods.finalize.call(
Class.parent,
self.as(Parent),
);
}
//---------------------------------------------------------------
// Signal Handlers
/// SIGUSR2 signal handler via g_unix_signal_add
fn handleSigusr2(ud: ?*anyopaque) callconv(.c) c_int {
const self: *Self = @ptrCast(@alignCast(ud orelse
return @intFromBool(glib.SOURCE_CONTINUE)));
log.info("received SIGUSR2, reloading configuration", .{});
Action.reloadConfig(
self,
.app,
.{},
) catch |err| {
// If we fail to reload the configuration, then we want the
// user to know it. For now we log but we should show another
// GUI.
log.warn("error reloading config: {}", .{err});
};
return @intFromBool(glib.SOURCE_CONTINUE);
}
fn handleCloseConfirmation(
_: *CloseConfirmationDialog,
self: *Self,
) callconv(.c) void {
self.quitNow();
}
fn handleQuitTimerExpired(ud: ?*anyopaque) callconv(.c) c_int {
const self: *Self = @ptrCast(@alignCast(ud));
const priv = self.private();
priv.quit_timer = .expired;
return 0;
}
fn handleStyleManagerDark(
style: *adw.StyleManager,
_: *gobject.ParamSpec,
self: *Self,
) callconv(.c) void {
const scheme: apprt.ColorScheme = if (style.getDark() == 0)
.light
else
.dark;
log.debug("style manager changed scheme={}", .{scheme});
const priv = self.private();
const core_app = priv.core_app;
core_app.colorSchemeEvent(self.rt(), scheme) catch |err| {
log.warn("error updating app color scheme err={}", .{err});
};
for (core_app.surfaces.items) |surface| {
surface.core().colorSchemeCallback(scheme) catch |err| {
log.warn(
"unable to tell surface about color scheme change err={}",
.{err},
);
};
}
}
fn handleReloadConfig(
_: *ConfigErrorsDialog,
self: *Self,
) callconv(.c) void {
// We clear our dialog reference because its going to close
// after response handling and we don't want to reuse it.
const priv = self.private();
priv.config_errors_dialog.set(null);
// Reload our config as if the app reloaded.
Action.reloadConfig(
self,
.app,
.{},
) catch |err| {
// If we fail to reload the configuration, then we want the
// user to know it. For now we log but we should show another
// GUI.
log.warn("error reloading config: {}", .{err});
};
}
/// Show the config errors dialog if the config on our application
/// has diagnostics.
fn showConfigErrorsDialog(self: *Self) void {
const priv = self.private();
// If we already have a dialog, just update the config.
if (priv.config_errors_dialog.get()) |diag| {
defer diag.unref(); // get gets a strong ref
var value = gobject.ext.Value.newFrom(priv.config);
defer value.unset();
gobject.Object.setProperty(
diag.as(gobject.Object),
"config",
&value,
);
if (!priv.config.hasDiagnostics()) {
diag.close();
} else {
diag.present(null);
}
return;
}
// No diagnostics, do nothing.
if (!priv.config.hasDiagnostics()) return;
// No dialog yet, initialize a new one. There's no need to unref
// here because the widget that it becomes a part of takes ownership.
const dialog: *ConfigErrorsDialog = .new(priv.config);
priv.config_errors_dialog.set(dialog);
// Connect to the reload signal so we know to reload our config.
_ = ConfigErrorsDialog.signals.@"reload-config".connect(
dialog,
*Application,
handleReloadConfig,
self,
.{},
);
// Show it
dialog.present(null);
}
fn globalShortcutTrigger(
_: *GlobalShortcuts,
action: *const Binding.Action,
self: *Self,
) callconv(.c) void {
self.core().performAllAction(self.rt(), action.*) catch |err| {
log.warn("failed to perform action={}", .{err});
};
}
fn actionReloadConfig(
_: *gio.SimpleAction,
_: ?*glib.Variant,
self: *Self,
) callconv(.c) void {
const priv = self.private();
priv.core_app.performAction(self.rt(), .reload_config) catch |err| {
log.warn("error reloading config err={}", .{err});
};
}
fn actionQuit(
_: *gio.SimpleAction,
_: ?*glib.Variant,
self: *Self,
) callconv(.c) void {
const priv = self.private();
priv.core_app.performAction(self.rt(), .quit) catch |err| {
log.warn("error quitting err={}", .{err});
};
}
/// Handle `app.new-window` and `app.new-window-command` GTK actions
pub fn actionNewWindow(
_: *gio.SimpleAction,
parameter_: ?*glib.Variant,
self: *Self,
) callconv(.c) void {
log.debug("received new window action", .{});
parameter: {
// were we given a parameter?
const parameter = parameter_ orelse break :parameter;
const as_variant_type = glib.VariantType.new("as");
defer as_variant_type.free();
// ensure that the supplied parameter is an array of strings
if (glib.Variant.isOfType(parameter, as_variant_type) == 0) {
log.warn("parameter is of type {s}", .{parameter.getTypeString()});
break :parameter;
}
const s_variant_type = glib.VariantType.new("s");
defer s_variant_type.free();
var it: glib.VariantIter = undefined;
_ = it.init(parameter);
while (it.nextValue()) |value| {
defer value.unref();
// just to be sure
if (value.isOfType(s_variant_type) == 0) continue;
var len: usize = undefined;
const buf = value.getString(&len);
const str = buf[0..len];
log.debug("new-window command argument: {s}", .{str});
}
}
_ = self.core().mailbox.push(.{
.new_window = .{},
}, .{ .forever = {} });
}
pub fn actionOpenConfig(
_: *gio.SimpleAction,
_: ?*glib.Variant,
self: *Self,
) callconv(.c) void {
_ = self.core().mailbox.push(.open_config, .forever);
}
fn actionPresentSurface(
_: *gio.SimpleAction,
parameter_: ?*glib.Variant,
self: *Self,
) callconv(.c) void {
const parameter = parameter_ orelse return;
const t = glib.ext.VariantType.newFor(u64);
defer glib.VariantType.free(t);
// Make sure that we've receiived a u64 from the system.
if (glib.Variant.isOfType(parameter, t) == 0) {
return;
}
// Convert that u64 to pointer to a core surface. A value of zero
// means that there was no target surface for the notification so
// we don't focus any surface.
//
// This is admittedly SUPER SUS and we should instead do what we
// do on macOS which is generate a UUID per surface and then pass
// that around. But, we do validate the pointer below so at worst
// this may result in focusing the wrong surface if the pointer was
// reused for a surface.
const ptr_int = parameter.getUint64();
if (ptr_int == 0) return;
const surface: *CoreSurface = @ptrFromInt(ptr_int);
// Send a message through the core app mailbox rather than presenting the
// surface directly so that it can validate that the surface pointer is
// valid. We could get an invalid pointer if a desktop notification outlives
// a Ghostty instance and a new one starts up, or there are multiple Ghostty
// instances running.
_ = self.core().mailbox.push(
.{
.surface_message = .{
.surface = surface,
.message = .present_surface,
},
},
.forever,
);
}
//----------------------------------------------------------------
// Boilerplate/Noise
const C = Common(Self, Private);
pub const as = C.as;
pub const ref = C.ref;
pub const unref = C.unref;
const private = C.private;
pub const Class = extern struct {
parent_class: Parent.Class,
var parent: *Parent.Class = undefined;
pub const Instance = Self;
fn init(class: *Class) callconv(.c) void {
// Register our compiled resources exactly once.
{
const c = @cImport({
// generated header files
@cInclude("ghostty_resources.h");
});
if (c.ghostty_get_resource()) |ptr| {
gio.resourcesRegister(@ptrCast(@alignCast(ptr)));
} else {
// If we fail to load resources then things will
// probably look really bad but it shouldn't stop our
// app from loading.
log.warn("unable to load resources", .{});
}
}
// Properties
gobject.ext.registerProperties(class, &.{
properties.config.impl,
});
// Virtual methods
gio.Application.virtual_methods.activate.implement(class, &activate);
gio.Application.virtual_methods.startup.implement(class, &startup);
gobject.Object.virtual_methods.dispose.implement(class, &dispose);
gobject.Object.virtual_methods.finalize.implement(class, &finalize);
}
};
};
/// All apprt action handlers
const Action = struct {
pub fn closeTab(target: apprt.Target, value: apprt.Action.Value(.close_tab)) bool {
switch (target) {
.app => return false,
.surface => |core| {
const surface = core.rt_surface.surface;
return surface.as(gtk.Widget).activateAction(
"tab.close",
glib.ext.VariantType.stringFor([:0]const u8),
@as([*:0]const u8, @tagName(value)),
) != 0;
},
}
}
pub fn closeWindow(target: apprt.Target) bool {
switch (target) {
.app => return false,
.surface => |core| {
const surface = core.rt_surface.surface;
return surface.as(gtk.Widget).activateAction("win.close", null) != 0;
},
}
}
pub fn configChange(
self: *Application,
target: apprt.Target,
new_config: *const CoreConfig,
) !void {
// Wrap our config in a GObject. This will clone it.
const alloc = self.allocator();
const config_obj: *Config = try .new(alloc, new_config);
defer config_obj.unref();
switch (target) {
.surface => |core| core.rt_surface.surface.setConfig(config_obj),
.app => self.setConfig(config_obj),
}
}
pub fn desktopNotification(
self: *Application,
target: apprt.Target,
n: apprt.action.DesktopNotification,
) void {
switch (target) {
.app => {},
.surface => |v| {
v.rt_surface.gobj().sendDesktopNotification(n.title, n.body);
return;
},
}
// Set a default title if we don't already have one
const t = switch (n.title.len) {
0 => "Ghostty",
else => n.title,
};
const notification = gio.Notification.new(t);
defer notification.unref();
notification.setBody(n.body);
const icon = gio.ThemedIcon.new("com.mitchellh.ghostty");
defer icon.unref();
notification.setIcon(icon.as(gio.Icon));
notification.setDefaultActionAndTargetValue(
"app.present-surface",
glib.Variant.newUint64(0),
);
// We set the notification ID to the body content. If the content is the
// same, this notification may replace a previous notification
const gio_app = self.as(gio.Application);
gio_app.sendNotification(n.body, notification);
}
pub fn equalizeSplits(target: apprt.Target) bool {
switch (target) {
.app => {
log.warn("equalize splits to app is unexpected", .{});
return false;
},
.surface => |core| {
const surface = core.rt_surface.surface;
return surface.as(gtk.Widget).activateAction("split-tree.equalize", null) != 0;
},
}
}
pub fn gotoSplit(
target: apprt.Target,
to: apprt.action.GotoSplit,
) bool {
switch (target) {
.app => return false,
.surface => |core| {
// Design note: we can't use widget actions here because
// we need to know whether there is a goto target for returning
// the proper perform result (boolean).
const surface = core.rt_surface.surface;
const tree = ext.getAncestor(
SplitTree,
surface.as(gtk.Widget),
) orelse {
log.warn("surface is not in a split tree, ignoring goto_split", .{});
return false;
};
return tree.goto(switch (to) {
.previous => .previous_wrapped,
.next => .next_wrapped,
.up => .{ .spatial = .up },
.down => .{ .spatial = .down },
.left => .{ .spatial = .left },
.right => .{ .spatial = .right },
});
},
}
}
pub fn gotoTab(
target: apprt.Target,
tab: apprt.action.GotoTab,
) bool {
switch (target) {
.app => return false,
.surface => |core| {
const surface = core.rt_surface.surface;
const window = ext.getAncestor(
Window,
surface.as(gtk.Widget),
) orelse {
log.warn("surface is not in a window, ignoring new_tab", .{});
return false;
};
return window.selectTab(switch (tab) {
.previous => .previous,
.next => .next,
.last => .last,
else => .{ .n = @intCast(@intFromEnum(tab)) },
});
},
}
}
pub fn initialSize(
target: apprt.Target,
value: apprt.action.InitialSize,
) bool {
switch (target) {
.app => return false,
.surface => |core| {
const surface = core.rt_surface.surface;
surface.setDefaultSize(.{
.width = value.width,
.height = value.height,
});
return true;
},
}
}
pub fn mouseOverLink(
target: apprt.Target,
value: apprt.action.MouseOverLink,
) void {
switch (target) {
.app => log.warn("mouse over link to app is unexpected", .{}),
.surface => |surface| surface.rt_surface.gobj().setMouseHoverUrl(
if (value.url.len > 0) value.url else null,
),
}
}
pub fn mouseShape(
target: apprt.Target,
shape: terminal.MouseShape,
) void {
switch (target) {
.app => log.warn("mouse shape to app is unexpected", .{}),
.surface => |surface| surface.rt_surface.gobj().setMouseShape(shape),
}
}
pub fn mouseVisibility(
target: apprt.Target,
visibility: apprt.action.MouseVisibility,
) void {
switch (target) {
.app => log.warn("mouse visibility to app is unexpected", .{}),
.surface => |surface| surface.rt_surface.gobj().setMouseHidden(switch (visibility) {
.visible => false,
.hidden => true,
}),
}
}
pub fn moveTab(
target: apprt.Target,
value: apprt.action.MoveTab,
) bool {
switch (target) {
.app => return false,
.surface => |core| {
const surface = core.rt_surface.surface;
const window = ext.getAncestor(
Window,
surface.as(gtk.Widget),
) orelse {
log.warn("surface is not in a window, ignoring new_tab", .{});
return false;
};
return window.moveTab(
surface,
@intCast(value.amount),
);
},
}
}
pub fn newSplit(
target: apprt.Target,
direction: apprt.action.SplitDirection,
) bool {
switch (target) {
.app => {
log.warn("new split to app is unexpected", .{});
return false;
},
.surface => |core| {
const surface = core.rt_surface.surface;
return surface.as(gtk.Widget).activateAction(
"split-tree.new-split",
"&s",
@tagName(direction).ptr,
) != 0;
},
}
}
pub fn newTab(target: apprt.Target) bool {
switch (target) {
.app => {
log.warn("new tab to app is unexpected", .{});
return false;
},
.surface => |core| {
// Get the window ancestor of the surface. Surfaces shouldn't
// be aware they might be in windows but at the app level we
// can do this.
const surface = core.rt_surface.surface;
const window = ext.getAncestor(
Window,
surface.as(gtk.Widget),
) orelse {
log.warn("surface is not in a window, ignoring new_tab", .{});
return false;
};
window.newTab(core);
return true;
},
}
}
pub fn newWindow(
self: *Application,
parent: ?*CoreSurface,
) !void {
// Note that we've requested a window at least once. This is used
// to trigger quit on no windows. Note I'm not sure if this is REALLY
// necessary, but I don't want to risk a bug where on a slow machine
// or something we quit immediately after starting up because there
// was a delay in the event loop before we created a Window.
self.private().requested_window = true;
const win = Window.new(self);
initAndShowWindow(self, win, parent);
}
fn initAndShowWindow(
self: *Application,
win: *Window,
parent: ?*CoreSurface,
) void {
// Setup a binding so that whenever our config changes so does the
// window. There's never a time when the window config should be out
// of sync with the application config.
_ = gobject.Object.bindProperty(
self.as(gobject.Object),
"config",
win.as(gobject.Object),
"config",
.{},
);
// Create a new tab
win.newTab(parent);
// Show the window
gtk.Window.present(win.as(gtk.Window));
}
pub fn openConfig(self: *Application) bool {
// Get the config file path
const alloc = self.allocator();
const path = configpkg.edit.openPath(alloc) catch |err| {
log.warn("error getting config file path: {}", .{err});
return false;
};
defer alloc.free(path);
// Open it using openURL. "path" isn't actually a URL but
// at the time of writing that works just fine for GTK.
openUrl(self, .{ .kind = .text, .url = path });
return true;
}
pub fn openUrl(
self: *Application,
value: apprt.action.OpenUrl,
) void {
// TODO: use https://flatpak.github.io/xdg-desktop-portal/docs/doc-org.freedesktop.portal.OpenURI.html
// Fallback to the minimal cross-platform way of opening a URL.
// This is always a safe fallback and enables for example Windows
// to open URLs (GTK on Windows via WSL is a thing).
internal_os.open(
self.allocator(),
value.kind,
value.url,
) catch |err| log.warn("unable to open url: {}", .{err});
}
pub fn pwd(
target: apprt.Target,
value: apprt.action.Pwd,
) void {
switch (target) {
.app => log.warn("pwd to app is unexpected", .{}),
.surface => |surface| surface.rt_surface.gobj().setPwd(value.pwd),
}
}
pub fn quitTimer(
self: *Application,
mode: apprt.action.QuitTimer,
) !void {
switch (mode) {
.start => self.startQuitTimer(),
.stop => self.stopQuitTimer(),
}
}
pub fn presentTerminal(
target: apprt.Target,
) bool {
return switch (target) {
.app => false,
.surface => |v| surface: {
v.rt_surface.surface.present();
break :surface true;
},
};
}
pub fn progressReport(
target: apprt.Target,
value: terminal.osc.Command.ProgressReport,
) bool {
return switch (target) {
.app => false,
.surface => |v| surface: {
v.rt_surface.surface.setProgressReport(value);
break :surface true;
},
};
}
pub fn promptTitle(target: apprt.Target) bool {
switch (target) {
.app => return false,
.surface => |v| {
v.rt_surface.surface.promptTitle();
return true;
},
}
}
/// Reload the configuration for the application and propagate it
/// across the entire application and all terminals.
pub fn reloadConfig(
self: *Application,
target: apprt.Target,
opts: apprt.action.ReloadConfig,
) !void {
// Tell systemd that reloading has started.
systemd.notify.reloading();
// When we exit this function tell systemd that reloading has finished.
defer systemd.notify.ready();
// Get our config object.
const config: *Config = config: {
// Soft-reloading applies conditional logic to the existing loaded
// config so we return that as-is (but take a reference).
if (opts.soft) {
break :config self.private().config.ref();
}
// Hard reload, load a new config completely.
const alloc = self.allocator();
var config = try CoreConfig.load(alloc);
defer config.deinit();
break :config try .new(alloc, &config);
};
defer config.unref();
// Update the proper target. This will trigger a `confige_change`
// apprt action which will propagate the config properly to our
// property system.
switch (target) {
.app => try self.core().updateConfig(
self.rt(),
config.get(),
),
.surface => |core| try core.updateConfig(config.get()),
}
}
pub fn render(target: apprt.Target) void {
switch (target) {
.app => {},
.surface => |v| v.rt_surface.surface.redraw(),
}
}
pub fn resizeSplit(
target: apprt.Target,
value: apprt.action.ResizeSplit,
) bool {
switch (target) {
.app => {
log.warn("resize_split to app is unexpected", .{});
return false;
},
.surface => |core| {
const surface = core.rt_surface.surface;
const tree = ext.getAncestor(
SplitTree,
surface.as(gtk.Widget),
) orelse {
log.warn("surface is not in a split tree, ignoring goto_split", .{});
return false;
};
return tree.resize(
switch (value.direction) {
.up => .up,
.down => .down,
.left => .left,
.right => .right,
},
value.amount,
) catch |err| switch (err) {
error.OutOfMemory => {
log.warn("unable to resize split, out of memory", .{});
return false;
},
};
},
}
}
pub fn ringBell(target: apprt.Target) void {
switch (target) {
.app => {},
.surface => |v| v.rt_surface.surface.setBellRinging(true),
}
}
pub fn setTitle(
target: apprt.Target,
value: apprt.action.SetTitle,
) void {
switch (target) {
.app => log.warn("set_title to app is unexpected", .{}),
.surface => |surface| surface.rt_surface.gobj().setTitle(value.title),
}
}
pub fn showChildExited(
target: apprt.Target,
value: apprt.surface.Message.ChildExited,
) bool {
return switch (target) {
.app => false,
.surface => |v| v.rt_surface.surface.childExited(value),
};
}
pub fn showGtkInspector() void {
gtk.Window.setInteractiveDebugging(@intFromBool(true));
}
pub fn sizeLimit(
target: apprt.Target,
value: apprt.action.SizeLimit,
) bool {
switch (target) {
.app => return false,
.surface => |core| {
// Note: we ignore the max size currently because we have
// no mechanism to enforce it.
const surface = core.rt_surface.surface;
surface.setMinSize(.{
.width = value.min_width,
.height = value.min_height,
});
return true;
},
}
}
pub fn toggleFullscreen(target: apprt.Target) void {
switch (target) {
.app => {},
.surface => |v| v.rt_surface.surface.toggleFullscreen(),
}
}
pub fn toggleQuickTerminal(self: *Application) bool {
// If we already have a quick terminal window, we just toggle the
// visibility of it.
if (getQuickTerminalWindow()) |win| {
win.toggleVisibility();
return true;
}
// If we don't support quick terminals then we do nothing.
const priv = self.private();
if (!priv.winproto.supportsQuickTerminal()) return false;
// Create our new window as a quick terminal
const win = gobject.ext.newInstance(Window, .{
.application = self,
.@"quick-terminal" = true,
});
assert(win.isQuickTerminal());
initAndShowWindow(self, win, null);
return true;
}
pub fn toggleSplitZoom(target: apprt.Target) bool {
switch (target) {
.app => {
log.warn("toggle_split_zoom to app is unexpected", .{});
return false;
},
.surface => |core| {
// TODO: pass surface ID when we have that
const surface = core.rt_surface.surface;
return surface.as(gtk.Widget).activateAction("split-tree.zoom", null) != 0;
},
}
}
pub fn showOnScreenKeyboard(target: apprt.Target) bool {
switch (target) {
.app => {
log.warn("show_on_screen_keyboard to app is unexpected", .{});
return false;
},
// NOTE: Even though `activateOsk` takes a gdk.Event, it's currently
// unused by all implementations of `activateOsk` as of GTK 4.18.
// The commit that introduced the method (ce6aa73c) clarifies that
// the event *may* be used by other IM backends, but for Linux desktop
// environments this doesn't matter.
.surface => |v| return v.rt_surface.surface.showOnScreenKeyboard(null),
}
}
fn getQuickTerminalWindow() ?*Window {
// Find a quick terminal window.
const list = gtk.Window.listToplevels();
defer list.free();
if (ext.listFind(gtk.Window, list, struct {
fn find(gtk_win: *gtk.Window) bool {
const win = gobject.ext.cast(
Window,
gtk_win,
) orelse return false;
return win.isQuickTerminal();
}
}.find)) |w| return gobject.ext.cast(
Window,
w,
).?;
return null;
}
pub fn toggleMaximize(target: apprt.Target) void {
switch (target) {
.app => {},
.surface => |v| v.rt_surface.surface.toggleMaximize(),
}
}
pub fn toggleTabOverview(target: apprt.Target) bool {
switch (target) {
.app => return false,
.surface => |core| {
const surface = core.rt_surface.surface;
const window = ext.getAncestor(
Window,
surface.as(gtk.Widget),
) orelse {
log.warn("surface is not in a window, ignoring new_tab", .{});
return false;
};
window.toggleTabOverview();
return true;
},
}
}
pub fn toggleWindowDecorations(target: apprt.Target) bool {
switch (target) {
.app => return false,
.surface => |core| {
const surface = core.rt_surface.surface;
const window = ext.getAncestor(
Window,
surface.as(gtk.Widget),
) orelse {
log.warn("surface is not in a window, ignoring toggle_window_decorations", .{});
return false;
};
window.toggleWindowDecorations();
return true;
},
}
}
pub fn toggleCommandPalette(target: apprt.Target) bool {
switch (target) {
.app => return false,
.surface => |surface| {
return surface.rt_surface.gobj().toggleCommandPalette();
},
}
}
pub fn controlInspector(target: apprt.Target, value: apprt.Action.Value(.inspector)) bool {
switch (target) {
.app => return false,
.surface => |surface| {
return surface.rt_surface.gobj().controlInspector(value);
},
}
}
pub fn commandFinished(target: apprt.Target, value: apprt.Action.Value(.command_finished)) bool {
switch (target) {
.app => return false,
.surface => |surface| {
return surface.rt_surface.gobj().commandFinished(value);
},
}
}
};
/// This sets various GTK-related environment variables as necessary
/// given the runtime environment or configuration.
///
/// This must be called BEFORE GTK initialization.
fn setGtkEnv(config: *const CoreConfig) error{NoSpaceLeft}!void {
assert(gtk.isInitialized() == 0);
var gdk_debug: struct {
/// output OpenGL debug information
opengl: bool = false,
/// disable GLES, Ghostty can't use GLES
@"gl-disable-gles": bool = false,
// GTK's new renderer can cause blurry font when using fractional scaling.
@"gl-no-fractional": bool = false,
/// Disabling Vulkan can improve startup times by hundreds of
/// milliseconds on some systems. We don't use Vulkan so we can just
/// disable it.
@"vulkan-disable": bool = false,
} = .{
.opengl = config.@"gtk-opengl-debug",
};
var gdk_disable: struct {
@"gles-api": bool = false,
/// current gtk implementation for color management is not good enough.
/// see: https://bugs.kde.org/show_bug.cgi?id=495647
/// gtk issue: https://gitlab.gnome.org/GNOME/gtk/-/issues/6864
@"color-mgmt": bool = true,
/// Disabling Vulkan can improve startup times by hundreds of
/// milliseconds on some systems. We don't use Vulkan so we can just
/// disable it.
vulkan: bool = false,
} = .{};
environment: {
if (gtk_version.runtimeAtLeast(4, 18, 0)) {
gdk_disable.@"color-mgmt" = false;
}
if (gtk_version.runtimeAtLeast(4, 16, 0)) {
// From gtk 4.16, GDK_DEBUG is split into GDK_DEBUG and GDK_DISABLE.
// For the remainder of "why" see the 4.14 comment below.
gdk_disable.@"gles-api" = true;
gdk_disable.vulkan = true;
break :environment;
}
if (gtk_version.runtimeAtLeast(4, 14, 0)) {
// We need to export GDK_DEBUG to run on Wayland after GTK 4.14.
// Older versions of GTK do not support these values so it is safe
// to always set this. Forwards versions are uncertain so we'll have
// to reassess...
//
// Upstream issue: https://gitlab.gnome.org/GNOME/gtk/-/issues/6589
gdk_debug.@"gl-disable-gles" = true;
gdk_debug.@"vulkan-disable" = true;
if (gtk_version.runtimeUntil(4, 17, 5)) {
// Removed at GTK v4.17.5
gdk_debug.@"gl-no-fractional" = true;
}
break :environment;
}
// Versions prior to 4.14 are a bit of an unknown for Ghostty. It
// is an environment that isn't tested well and we don't have a
// good understanding of what we may need to do.
gdk_debug.@"vulkan-disable" = true;
}
{
var buf: [1024]u8 = undefined;
var fmt = std.io.fixedBufferStream(&buf);
const writer = fmt.writer();
var first: bool = true;
inline for (@typeInfo(@TypeOf(gdk_debug)).@"struct".fields) |field| {
if (@field(gdk_debug, field.name)) {
if (!first) try writer.writeAll(",");
try writer.writeAll(field.name);
first = false;
}
}
try writer.writeByte(0);
const value = fmt.getWritten();
log.warn("setting GDK_DEBUG={s}", .{value[0 .. value.len - 1]});
_ = internal_os.setenv("GDK_DEBUG", value[0 .. value.len - 1 :0]);
}
{
var buf: [1024]u8 = undefined;
var fmt = std.io.fixedBufferStream(&buf);
const writer = fmt.writer();
var first: bool = true;
inline for (@typeInfo(@TypeOf(gdk_disable)).@"struct".fields) |field| {
if (@field(gdk_disable, field.name)) {
if (!first) try writer.writeAll(",");
try writer.writeAll(field.name);
first = false;
}
}
try writer.writeByte(0);
const value = fmt.getWritten();
log.warn("setting GDK_DISABLE={s}", .{value[0 .. value.len - 1]});
_ = internal_os.setenv("GDK_DISABLE", value[0 .. value.len - 1 :0]);
}
}
fn findActiveWindow(data: ?*const anyopaque, _: ?*const anyopaque) callconv(.c) c_int {
const window: *gtk.Window = @ptrCast(@alignCast(@constCast(data orelse return -1)));
// Confusingly, `isActive` returns 1 when active,
// but we want to return 0 to indicate equality.
// Abusing integers to be enums and booleans is a terrible idea, C.
return if (window.isActive() != 0) 0 else -1;
}
|