aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: 0f93e188f6d677d40140ca7136843406ed3b0eef (plain)
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <locale.h>
#include <getopt.h>
#include <poll.h>
#include <errno.h>

#include <sys/timerfd.h>
#include <sys/sysinfo.h>
#include <sys/prctl.h>
#include <sys/wait.h>
#include <sys/time.h>

#include <wayland-client.h>
#include <wayland-cursor.h>
#include <xdg-shell.h>
#include <xkbcommon/xkbcommon-compose.h>

#include <xdg-output-unstable-v1.h>
#include <xdg-decoration-unstable-v1.h>

#define LOG_MODULE "main"
#define LOG_ENABLE_DBG 0
#include "log.h"

#include "config.h"
#include "font.h"
#include "grid.h"
#include "input.h"
#include "render.h"
#include "selection.h"
#include "shm.h"
#include "slave.h"
#include "terminal.h"
#include "tokenize.h"
#include "vt.h"

#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))

static void
shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
{
    struct terminal *term = data;
    if (format == WL_SHM_FORMAT_ARGB8888)
        term->wl.have_argb8888 = true;
}

static const struct wl_shm_listener shm_listener = {
    .format = &shm_format,
};

static void
xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
{
    LOG_DBG("wm base ping");
    xdg_wm_base_pong(shell, serial);
}

static const struct xdg_wm_base_listener xdg_wm_base_listener = {
    .ping = &xdg_wm_base_ping,
};

static void
seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
                         enum wl_seat_capability caps)
{
    struct terminal *term = data;

    if (term->wl.keyboard != NULL) {
        wl_keyboard_release(term->wl.keyboard);
        term->wl.keyboard = NULL;
    }

    if (term->wl.pointer.pointer != NULL) {
        wl_pointer_release(term->wl.pointer.pointer);
        term->wl.pointer.pointer = NULL;
    }

    if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
        term->wl.keyboard = wl_seat_get_keyboard(wl_seat);
        wl_keyboard_add_listener(term->wl.keyboard, &keyboard_listener, term);
    }

    if (caps & WL_SEAT_CAPABILITY_POINTER) {
        term->wl.pointer.pointer = wl_seat_get_pointer(wl_seat);
        wl_pointer_add_listener(term->wl.pointer.pointer, &pointer_listener, term);
    }
}

static void
seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)
{
}

static const struct wl_seat_listener seat_listener = {
    .capabilities = seat_handle_capabilities,
    .name = seat_handle_name,
};

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 monitor *mon = data;
    mon->width_mm = physical_width;
    mon->height_mm = physical_height;
}

static void
output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
            int32_t width, int32_t height, int32_t refresh)
{
    if ((flags & WL_OUTPUT_MODE_CURRENT) == 0)
        return;

    struct monitor *mon = data;
    mon->refresh = (float)refresh / 1000;
}

static void
output_done(void *data, struct wl_output *wl_output)
{
}

static void
output_scale(void *data, struct wl_output *wl_output, int32_t factor)
{
    struct monitor *mon = data;
    mon->scale = factor;

    render_resize(mon->term, mon->term->width / mon->term->scale, mon->term->height / mon->term->scale);
    render_reload_cursor_theme(mon->term);
}

static const struct wl_output_listener output_listener = {
    .geometry = &output_geometry,
    .mode = &output_mode,
    .done = &output_done,
    .scale = &output_scale,
};

static void
xdg_output_handle_logical_position(
    void *data, struct zxdg_output_v1 *xdg_output, int32_t x, int32_t y)
{
    struct monitor *mon = data;
    mon->x = x;
    mon->y = y;
}

static void
xdg_output_handle_logical_size(void *data, struct zxdg_output_v1 *xdg_output,
                               int32_t width, int32_t height)
{
    struct monitor *mon = data;
    mon->width_px = width;
    mon->height_px = height;
}

static void
xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output)
{
}

static void
xdg_output_handle_name(void *data, struct zxdg_output_v1 *xdg_output,
                       const char *name)
{
    struct monitor *mon = data;
    mon->name = strdup(name);
}

static void
xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output,
                              const char *description)
{
}

static struct zxdg_output_v1_listener xdg_output_listener = {
    .logical_position = xdg_output_handle_logical_position,
    .logical_size = xdg_output_handle_logical_size,
    .done = xdg_output_handle_done,
    .name = xdg_output_handle_name,
    .description = xdg_output_handle_description,
};

static void
handle_global(void *data, struct wl_registry *registry,
              uint32_t name, const char *interface, uint32_t version)
{
    LOG_DBG("global: %s, version=%u", interface, version);
    struct terminal *term = data;

    if (strcmp(interface, wl_compositor_interface.name) == 0) {
        term->wl.compositor = wl_registry_bind(
            term->wl.registry, name, &wl_compositor_interface, 4);
    }

    else if (strcmp(interface, wl_subcompositor_interface.name) == 0) {
        term->wl.sub_compositor = wl_registry_bind(
            term->wl.registry, name, &wl_subcompositor_interface, 1);
    }

    else if (strcmp(interface, wl_shm_interface.name) == 0) {
        term->wl.shm = wl_registry_bind(
            term->wl.registry, name, &wl_shm_interface, 1);
        wl_shm_add_listener(term->wl.shm, &shm_listener, term);
        wl_display_roundtrip(term->wl.display);
    }

    else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
        term->wl.shell = wl_registry_bind(
            term->wl.registry, name, &xdg_wm_base_interface, 1);
        xdg_wm_base_add_listener(term->wl.shell, &xdg_wm_base_listener, term);
    }

    else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0)
        term->wl.xdg_decoration_manager = wl_registry_bind(
            term->wl.registry, name, &zxdg_decoration_manager_v1_interface, 1);

    else if (strcmp(interface, wl_seat_interface.name) == 0) {
        term->wl.seat = wl_registry_bind(
            term->wl.registry, name, &wl_seat_interface, 5);
        wl_seat_add_listener(term->wl.seat, &seat_listener, term);
        wl_display_roundtrip(term->wl.display);
    }

    else if (strcmp(interface, zxdg_output_manager_v1_interface.name) == 0) {
        term->wl.xdg_output_manager = wl_registry_bind(
            term->wl.registry, name, &zxdg_output_manager_v1_interface, min(version, 2));
    }

    else if (strcmp(interface, wl_output_interface.name) == 0) {
        struct wl_output *output = wl_registry_bind(
            term->wl.registry, name, &wl_output_interface, 3);

        tll_push_back(
            term->wl.monitors, ((struct monitor){
                    .term = term, .output = output}));

        struct monitor *mon = &tll_back(term->wl.monitors);
        wl_output_add_listener(output, &output_listener, mon);

        mon->xdg = zxdg_output_manager_v1_get_xdg_output(
            term->wl.xdg_output_manager, mon->output);
        zxdg_output_v1_add_listener(mon->xdg, &xdg_output_listener, mon);
        wl_display_roundtrip(term->wl.display);
    }

    else if (strcmp(interface, wl_data_device_manager_interface.name) == 0) {
        term->wl.data_device_manager = wl_registry_bind(
            term->wl.registry, name, &wl_data_device_manager_interface, 1);
    }

    else if (strcmp(interface, zwp_primary_selection_device_manager_v1_interface.name) == 0) {
        term->wl.primary_selection_device_manager = wl_registry_bind(
            term->wl.registry, name, &zwp_primary_selection_device_manager_v1_interface, 1);
    }
}

static void
surface_enter(void *data, struct wl_surface *wl_surface,
              struct wl_output *wl_output)
{
    struct terminal *term = data;
    tll_foreach(term->wl.monitors, it) {
        if (it->item.output == wl_output) {
            LOG_DBG("mapped on %s", it->item.name);
            tll_push_back(term->wl.on_outputs, &it->item);

            /* Resize, since scale-to-use may have changed */
            render_resize(term, term->width / term->scale, term->height / term->scale);
            render_reload_cursor_theme(term);
            return;
        }
    }

    LOG_ERR("mapped on unknown output");
}

static void
surface_leave(void *data, struct wl_surface *wl_surface,
              struct wl_output *wl_output)
{
    struct terminal *term = data;
    tll_foreach(term->wl.on_outputs, it) {
        if (it->item->output != wl_output)
            continue;

        LOG_DBG("unmapped from %s", it->item->name);
        tll_remove(term->wl.on_outputs, it);

        /* Resize, since scale-to-use may have changed */
        render_resize(term, term->width / term->scale, term->height / term->scale);
        render_reload_cursor_theme(term);
        return;
    }

    LOG_ERR("unmapped from unknown output");
}

static const struct wl_surface_listener surface_listener = {
    .enter = &surface_enter,
    .leave = &surface_leave,
};

static void
xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
                       int32_t width, int32_t height, struct wl_array *states)
{
    LOG_DBG("xdg-toplevel: configure: %dx%d", width, height);

    if (width <= 0 || height <= 0)
        return;

    struct terminal *term = data;
    render_resize(term, width, height);
}

static void
xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
{
    struct terminal *term = data;
    LOG_DBG("xdg-toplevel: close");
    term->quit = true;
    wl_display_roundtrip(term->wl.display);
}

static const struct xdg_toplevel_listener xdg_toplevel_listener = {
    .configure = &xdg_toplevel_configure,
    .close = &xdg_toplevel_close,
};

static void
xdg_surface_configure(void *data, struct xdg_surface *xdg_surface,
                      uint32_t serial)
{
    //LOG_DBG("xdg-surface: configure");
    xdg_surface_ack_configure(xdg_surface, serial);
}

static const struct xdg_surface_listener xdg_surface_listener = {
    .configure = &xdg_surface_configure,
};

static void
xdg_toplevel_decoration_configure(void *data,
                                  struct zxdg_toplevel_decoration_v1 *zxdg_toplevel_decoration_v1,
                                  uint32_t mode)
{
    switch (mode) {
    case ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE:
        LOG_ERR("unimplemented: client-side decorations");
        break;

    case ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE:
        LOG_DBG("using server-side decorations");
        break;

    default:
        LOG_ERR("unimplemented: unknown XDG toplevel decoration mode: %u", mode);
        break;
    }
}

static const struct zxdg_toplevel_decoration_v1_listener xdg_toplevel_decoration_listener = {
    .configure = &xdg_toplevel_decoration_configure,
};

static void
handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
{
    LOG_WARN("global removed: %u", name);
    assert(false);
}

static const struct wl_registry_listener registry_listener = {
    .global = &handle_global,
    .global_remove = &handle_global_remove,
};

static void
print_usage(const char *prog_name)
{
    printf("Usage: %s [OPTION]...\n", prog_name);
    printf("\n");
    printf("Options:\n");
    printf("  -f,--font=FONT              comma separated list of fonts in fontconfig format (monospace)\n"
           "  -t,--term=TERM              value to set the environment variable TERM to (foot)\n"
           "  -g,--geometry=WIDTHxHEIGHT  set initial width and height\n"
           "  -v,--version                show the version number and quit\n");
    printf("\n");
}

int
main(int argc, char *const *argv)
{
    int ret = EXIT_FAILURE;

    /* Startup notifications; we don't support it, but must ensure we
     * don't pass this on to programs launched by us */
    unsetenv("DESKTOP_STARTUP_ID");

    struct config conf = {NULL};
    if (!config_load(&conf))
        return ret;

    const char *const prog_name = argv[0];

    static const struct option longopts[] =  {
        {"term",     required_argument, 0, 't'},
        {"font",     required_argument, 0, 'f'},
        {"geometry", required_argument, 0, 'g'},
        {"version",  no_argument,       0, 'v'},
        {"help",     no_argument,       0, 'h'},
        {NULL,       no_argument,       0,   0},
    };

    while (true) {
        int c = getopt_long(argc, argv, ":t:f:g:vh", longopts, NULL);
        if (c == -1)
            break;

        switch (c) {
        case 't':
            free(conf.term);
            conf.term = strdup(optarg);
            break;

        case 'f':
            tll_free_and_free(conf.fonts, free);
            //tll_push_back(conf.fonts, strdup(optarg));
            for (char *font = strtok(optarg, ","); font != NULL; font = strtok(NULL, ",")) {

                /* Strip leading spaces */
                while (*font != '\0' && isspace(*font))
                    font++;

                /* Strip trailing spaces */
                char *end = font + strlen(font);
                assert(*end == '\0');
                end--;
                while (end > font && isspace(*end))
                    *(end--) = '\0';

                if (strlen(font) == 0)
                    continue;

                tll_push_back(conf.fonts, strdup(font));
            }
            break;

        case 'g': {
            unsigned width, height;
            if (sscanf(optarg, "%ux%u", &width, &height) != 2 || width == 0 || height == 0) {
                fprintf(stderr, "error: invalid geometry: %s\n", optarg);
                return EXIT_FAILURE;
            }

            conf.width = width;
            conf.height = height;
            break;
        }

        case 'v':
            printf("foot version %s\n", FOOT_VERSION);
            return EXIT_SUCCESS;

        case 'h':
            print_usage(prog_name);
            return EXIT_SUCCESS;

        case ':':
            fprintf(stderr, "error: -%c: missing required argument\n", optopt);
            return EXIT_FAILURE;

        case '?':
            fprintf(stderr, "error: -%c: invalid option\n", optopt);
            return EXIT_FAILURE;
        }
    }

    argc -= optind;
    argv += optind;

    setlocale(LC_ALL, "");
    setenv("TERM", conf.term, 1);

    struct terminal term = {
        .quit = false,
        .ptmx = posix_openpt(O_RDWR | O_NOCTTY),
        .cursor_keys_mode = CURSOR_KEYS_NORMAL,
        .keypad_keys_mode = KEYPAD_NUMERICAL,
        .auto_margin = true,
        .window_title_stack = tll_init(),
        .scale = 1,
        .flash = {
            .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK),
        },
        .blink = {
            .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK),
        },
        .vt = {
            .state = 1,  /* STATE_GROUND */
            .attrs = {
                //.foreground = conf.colors.fg,
                //.background = conf.colors.bg
            },
        },
        .kbd = {
            .repeat = {
                .fd = timerfd_create(CLOCK_BOOTTIME, TFD_CLOEXEC | TFD_NONBLOCK),
            },
        },
        .colors = {
            .default_fg = conf.colors.fg,
            .default_bg = conf.colors.bg,
            .default_table = {
                conf.colors.regular[0],
                conf.colors.regular[1],
                conf.colors.regular[2],
                conf.colors.regular[3],
                conf.colors.regular[4],
                conf.colors.regular[5],
                conf.colors.regular[6],
                conf.colors.regular[7],

                conf.colors.bright[0],
                conf.colors.bright[1],
                conf.colors.bright[2],
                conf.colors.bright[3],
                conf.colors.bright[4],
                conf.colors.bright[5],
                conf.colors.bright[6],
                conf.colors.bright[7],
            },
            .alpha = conf.colors.alpha,
        },
        .default_cursor_style = conf.cursor.style,
        .cursor_style = conf.cursor.style,
        .default_cursor_color = {
            .text = conf.cursor.color.text,
            .cursor = conf.cursor.color.cursor,
        },
        .cursor_color = {
            .text = conf.cursor.color.text,
            .cursor = conf.cursor.color.cursor,
        },
        .selection = {
            .start = {-1, -1},
            .end = {-1, -1},
        },
        .normal = {.damage = tll_init(), .scroll_damage = tll_init()},
        .alt = {.damage = tll_init(), .scroll_damage = tll_init()},
        .grid = &term.normal,
        .render = {
            .scrollback_lines = conf.scrollback_lines,
            .workers = {
                .count = conf.render_worker_count,
                .queue = tll_init(),
            },
        },
    };

    LOG_INFO("using %zu rendering threads", term.render.workers.count);

    struct render_worker_context worker_context[term.render.workers.count];

    /* Initialize 'current' colors from the default colors */
    term.colors.fg = term.colors.default_fg;
    term.colors.bg = term.colors.default_bg;

    /* Initialize the 256 gray-scale color cube */
    {
        /* First 16 entries have already been initialized from conf */
        for (size_t r = 0; r < 6; r++) {
            for (size_t g = 0; g < 6; g++) {
                for (size_t b = 0; b < 6; b++) {
                    term.colors.default_table[16 + r * 6 * 6 + g * 6 + b]
                        = r * 51 << 16 | g * 51 << 8 | b * 51;
                }
            }
        }

        for (size_t i = 0; i < 24; i++)
            term.colors.default_table[232 + i] = i * 11 << 16 | i * 11 << 8 | i * 11;

        memcpy(term.colors.table, term.colors.default_table, sizeof(term.colors.table));
    }

    if (term.ptmx == -1) {
        LOG_ERR("failed to open pseudo terminal");
        goto out;
    }

    if (term.flash.fd == -1 || term.blink.fd == -1 || term.kbd.repeat.fd == -1) {
        LOG_ERR("failed to create timers");
        goto out;
    }

    sem_init(&term.render.workers.start, 0, 0);
    sem_init(&term.render.workers.done, 0, 0);
    mtx_init(&term.render.workers.lock, mtx_plain);
    cnd_init(&term.render.workers.cond);

    term.render.workers.threads = calloc(term.render.workers.count, sizeof(term.render.workers.threads[0]));
    for (size_t i = 0; i < term.render.workers.count; i++) {
        worker_context[i].term = &term;
        worker_context[i].my_id = 1 + i;
        thrd_create(&term.render.workers.threads[i], &render_worker_thread, &worker_context[i]);
    }

    font_list_t font_names = tll_init();
    tll_foreach(conf.fonts, it)
        tll_push_back(font_names, it->item);

    if (!font_from_name(font_names, "", &term.fonts[0])) {
        tll_free(font_names);
        goto out;
    }

    font_from_name(font_names, "style=bold", &term.fonts[1]);
    font_from_name(font_names, "style=italic", &term.fonts[2]);
    font_from_name(font_names, "style=bold italic", &term.fonts[3]);

    tll_free(font_names);

    {
        FT_Face ft_face = term.fonts[0].face;
        int max_x_advance = ft_face->size->metrics.max_advance / 64;
        int height = ft_face->size->metrics.height / 64;
        int descent = ft_face->size->metrics.descender / 64;
        int ascent = ft_face->size->metrics.ascender / 64;

        term.fextents.height = height * term.fonts[0].pixel_size_fixup;
        term.fextents.descent = -descent * term.fonts[0].pixel_size_fixup;
        term.fextents.ascent = ascent * term.fonts[0].pixel_size_fixup;
        term.fextents.max_x_advance = max_x_advance * term.fonts[0].pixel_size_fixup;

        LOG_DBG("metrics: height: %d, descent: %d, ascent: %d, x-advance: %d",
                height, descent, ascent, max_x_advance);
    }

    term.cell_width = (int)ceil(term.fextents.max_x_advance);
    term.cell_height = (int)ceil(term.fextents.height);
    LOG_INFO("cell width=%d, height=%d", term.cell_width, term.cell_height);

    term.wl.display = wl_display_connect(NULL);
    if (term.wl.display == NULL) {
        LOG_ERR("failed to connect to wayland; no compositor running?");
        goto out;
    }

    term.wl.registry = wl_display_get_registry(term.wl.display);
    if (term.wl.registry == NULL) {
        LOG_ERR("failed to get wayland registry");
        goto out;
    }

    wl_registry_add_listener(term.wl.registry, &registry_listener, &term);
    wl_display_roundtrip(term.wl.display);

    if (term.wl.compositor == NULL) {
        LOG_ERR("no compositor");
        goto out;
    }
    if (term.wl.shm == NULL) {
        LOG_ERR("no shared memory buffers interface");
        goto out;
    }
    if (term.wl.shell == NULL) {
        LOG_ERR("no XDG shell interface");
        goto out;
    }
    if (!term.wl.have_argb8888) {
        LOG_ERR("compositor does not support ARGB surfaces");
        goto out;
    }
    if (term.wl.seat == NULL) {
        LOG_ERR("no seat available");
        goto out;
    }
    if (term.wl.data_device_manager == NULL) {
        LOG_ERR("no clipboard available "
                "(wl_data_device_manager not implemented by server)");
        goto out;
    }
    if (term.wl.primary_selection_device_manager == NULL)
        LOG_WARN("no primary selection available");

    tll_foreach(term.wl.monitors, it) {
        LOG_INFO("%s: %dx%d+%dx%d (scale=%d, refresh=%.2fHz)",
                 it->item.name, it->item.width_px, it->item.height_px,
                 it->item.x, it->item.y, it->item.scale, it->item.refresh);
    }

    /* Clipboard */
    term.wl.data_device = wl_data_device_manager_get_data_device(
        term.wl.data_device_manager, term.wl.seat);
    wl_data_device_add_listener(term.wl.data_device, &data_device_listener, &term);

    /* Primary selection */
    if (term.wl.primary_selection_device_manager != NULL) {
        term.wl.primary_selection_device = zwp_primary_selection_device_manager_v1_get_device(
            term.wl.primary_selection_device_manager, term.wl.seat);
        zwp_primary_selection_device_v1_add_listener(
            term.wl.primary_selection_device, &primary_selection_device_listener, &term);
    }

    /* Cursor */
    unsigned cursor_size = 24;
    const char *cursor_theme = getenv("XCURSOR_THEME");

    {
        const char *env_cursor_size = getenv("XCURSOR_SIZE");
        if (env_cursor_size != NULL) {
            unsigned size;
            if (sscanf(env_cursor_size, "%u", &size) == 1)
                cursor_size = size;
        }
    }

    /* Note: theme is (re)loaded on scale and output changes */
    LOG_INFO("cursor theme: %s, size: %u", cursor_theme, cursor_size);
    term.wl.pointer.size = cursor_size;
    term.wl.pointer.theme_name = cursor_theme != NULL ? strdup(cursor_theme) : NULL;

    term.wl.pointer.surface = wl_compositor_create_surface(term.wl.compositor);
    if (term.wl.pointer.surface == NULL) {
        LOG_ERR("failed to create cursor surface");
        goto out;
    }

    /* Main window */
    term.wl.surface = wl_compositor_create_surface(term.wl.compositor);
    if (term.wl.surface == NULL) {
        LOG_ERR("failed to create wayland surface");
        goto out;
    }

    wl_surface_add_listener(term.wl.surface, &surface_listener, &term);

    term.wl.xdg_surface = xdg_wm_base_get_xdg_surface(term.wl.shell, term.wl.surface);
    xdg_surface_add_listener(term.wl.xdg_surface, &xdg_surface_listener, &term);

    term.wl.xdg_toplevel = xdg_surface_get_toplevel(term.wl.xdg_surface);
    xdg_toplevel_add_listener(term.wl.xdg_toplevel, &xdg_toplevel_listener, &term);

    xdg_toplevel_set_app_id(term.wl.xdg_toplevel, "foot");
    term_set_window_title(&term, "foot");

    /* Request server-side decorations */
    term.wl.xdg_toplevel_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(
        term.wl.xdg_decoration_manager, term.wl.xdg_toplevel);
    zxdg_toplevel_decoration_v1_set_mode(
        term.wl.xdg_toplevel_decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
    zxdg_toplevel_decoration_v1_add_listener(
        term.wl.xdg_toplevel_decoration, &xdg_toplevel_decoration_listener, &term);

    /* Scrollback search box */
    term.wl.search_surface = wl_compositor_create_surface(term.wl.compositor);
    term.wl.search_sub_surface = wl_subcompositor_get_subsurface(
        term.wl.sub_compositor, term.wl.search_surface, term.wl.surface);
    wl_subsurface_set_desync(term.wl.search_sub_surface);

    wl_surface_commit(term.wl.surface);
    wl_display_roundtrip(term.wl.display);

    if (conf.width == -1) {
        assert(conf.height == -1);
        conf.width = 80 * term.cell_width;
        conf.height = 24 * term.cell_height;
    }
    conf.width = max(conf.width, term.cell_width);
    conf.height = max(conf.height, term.cell_height);
    render_resize(&term, conf.width, conf.height);

    wl_display_dispatch_pending(term.wl.display);

    {
        int fork_pipe[2];
        if (pipe2(fork_pipe, O_CLOEXEC) < 0) {
            LOG_ERRNO("failed to create pipe");
            goto out;
        }

        term.slave = fork();
        switch (term.slave) {
        case -1:
            LOG_ERRNO("failed to fork");
            close(fork_pipe[0]);
            close(fork_pipe[1]);
            goto out;

        case 0:
            /* Child */
            close(fork_pipe[0]);  /* Close read end */

            char **_shell_argv = NULL;
            char *const *shell_argv = argv;

            if (argc == 0) {
                if (!tokenize_cmdline(conf.shell, &_shell_argv)) {
                    (void)!write(fork_pipe[1], &errno, sizeof(errno));
                    _exit(0);
                }
                shell_argv = _shell_argv;
            }

            slave_spawn(term.ptmx, shell_argv, fork_pipe[1]);
            assert(false);
            break;

        default: {
            close(fork_pipe[1]); /* Close write end */
            LOG_DBG("slave has PID %d", term.slave);

            int _errno;
            static_assert(sizeof(errno) == sizeof(_errno), "errno size mismatch");

            ssize_t ret = read(fork_pipe[0], &_errno, sizeof(_errno));
            close(fork_pipe[0]);

            if (ret < 0) {
                LOG_ERRNO("failed to read from pipe");
                goto out;
            } else if (ret == sizeof(_errno)) {
                LOG_ERRNO(
                    "%s: failed to execute", argc == 0 ? conf.shell : argv[0]);
                goto out;
            } else
                LOG_DBG("%s: successfully started", conf.shell);
            break;
        }
        }
    }

    /* Read logic requires non-blocking mode */
    {
        int fd_flags = fcntl(term.ptmx, F_GETFL);
        if (fd_flags == -1) {
            LOG_ERRNO("failed to set non blocking mode on PTY master");
            goto out;
        }

        if (fcntl(term.ptmx, F_SETFL, fd_flags | O_NONBLOCK) == -1) {
            LOG_ERRNO("failed to set non blocking mode on PTY master");
            goto out;
        }
    }


    {
        int fd = wl_display_get_fd(term.wl.display);
        int fd_flags = fcntl(fd, F_GETFL);
        if (fd_flags == -1) {
            LOG_ERRNO("failed to set non blocking mode on Wayland display connection");
            goto out;
        }
        if (fcntl(fd, F_SETFL, fd_flags | O_NONBLOCK) == -1) {
            LOG_ERRNO("failed to set non blocking mode on Wayland display connection");
            goto out;
        }
    }

    bool timeout_is_armed = false;
    int delay_render_timer_lower = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC);
    int delay_render_timer_upper = timerfd_create(CLOCK_REALTIME, TFD_NONBLOCK | TFD_CLOEXEC);

    while (true) {
        struct pollfd fds[] = {
            {.fd = wl_display_get_fd(term.wl.display), .events = POLLIN},
            {.fd = term.ptmx,                     .events = POLLIN},
            {.fd = term.kbd.repeat.fd,            .events = POLLIN},
            {.fd = term.flash.fd,                 .events = POLLIN},
            {.fd = term.blink.fd,                 .events = POLLIN},
            {.fd = delay_render_timer_lower,      .events = POLLIN},
            {.fd = delay_render_timer_upper,      .events = POLLIN},
        };

        const size_t WL_FD = 0;
        const size_t PTMX_FD = 1;
        const size_t KBD_REPEAT_FD = 2;
        const size_t FLASH_FD = 3;
        const size_t BLINK_FD = 4;
        const size_t DELAY_LOWER_FD = 5;
        const size_t DELAY_UPPER_FD = 6;

        wl_display_flush(term.wl.display);
        int pret = poll(fds, sizeof(fds) / sizeof(fds[0]), -1);

        if (pret == -1) {
            if (errno == EINTR)
                continue;

            LOG_ERRNO("failed to poll file descriptors");
            break;
        }

        /* Delayed rendering timers (created when we receive input) */
        if (fds[DELAY_LOWER_FD].revents & POLLIN ||
            fds[DELAY_UPPER_FD].revents & POLLIN)
        {
            assert(timeout_is_armed);

            uint64_t unused;
            ssize_t ret1 = 0;
            ssize_t ret2 = 0;

            if (fds[DELAY_LOWER_FD].revents & POLLIN)
                ret1 = read(delay_render_timer_lower, &unused, sizeof(unused));
            if (fds[DELAY_UPPER_FD].revents & POLLIN)
                ret2 = read(delay_render_timer_upper, &unused, sizeof(unused));

            if ((ret1 < 0 || ret2 < 0) && errno != EAGAIN)
                LOG_ERRNO("failed to read timeout timer");
            else if (ret1 > 0 || ret2 > 0) {
                render_refresh(&term);

                /* Reset timers */
                timeout_is_armed = false;
                timerfd_settime(delay_render_timer_lower, 0, &(struct itimerspec){.it_value = {0}}, NULL);
                timerfd_settime(delay_render_timer_upper, 0, &(struct itimerspec){.it_value = {0}}, NULL);
            } else
                assert(false);
        }

        if (fds[WL_FD].revents & POLLIN) {
            wl_display_dispatch(term.wl.display);
            if (term.quit) {
                ret = EXIT_SUCCESS;
                break;
            }
        }

        if (fds[WL_FD].revents & POLLHUP) {
            LOG_WARN("disconnected from wayland");
            break;
        }

        if (fds[PTMX_FD].revents & POLLIN) {
            uint8_t data[24 * 1024];
            ssize_t count = read(term.ptmx, data, sizeof(data));
            if (count < 0 && errno != EAGAIN) {
                LOG_ERRNO("failed to read from pseudo terminal");
                break;
            }

            if (count > 0) {
                vt_from_slave(&term, data, count);

                /*
                 * We likely need to re-render. But, we don't want to
                 * do it immediately. Often, a single client operation
                 * is done through multiple writes. Many times, we're
                 * so fast that we render mid-operation frames.
                 *
                 * For example, we might end up rendering a frame
                 * where the client just erased a line, while in the
                 * next frame, the client wrote to the same line. This
                 * causes screen "flashes".
                 *
                 * Mitigate by always incuring a small delay before
                 * rendering the next frame. This gives the client
                 * some time to finish the operation (and thus gives
                 * us time to receive the last writes before doing any
                 * actual rendering).
                 *
                 * We incur this delay *every* time we receive
                 * input. To ensure we don't delay rendering
                 * indefinitely, we start a second timer that is only
                 * reset when we render.
                 *
                 * Note that when the client is producing data at a
                 * very high pace, we're rate limited by the wayland
                 * compositor anyway. The delay we introduce here only
                 * has any effect when the renderer is idle.
                 *
                 * TODO: this adds input latency. Can we somehow hint
                 * ourselves we just received keyboard input, and in
                 * this case *not* delay rendering?
                 */
                if (term.render.frame_callback == NULL) {
                    /* First timeout - reset each time we receive input. */
                    timerfd_settime(
                        delay_render_timer_lower, 0,
                        &(struct itimerspec){.it_value = {.tv_nsec = 1000000}},
                        NULL);

                    /* Second timeout - only reset when we render. Set to one frame (assuming 60Hz) */
                    if (!timeout_is_armed) {
                        timerfd_settime(
                            delay_render_timer_upper, 0,
                            &(struct itimerspec){.it_value = {.tv_nsec = 16666666}},
                            NULL);
                        timeout_is_armed = true;
                    }
                }
            }
        }

        if (fds[PTMX_FD].revents & POLLHUP) {
            ret = EXIT_SUCCESS;
            break;
        }

        if (fds[KBD_REPEAT_FD].revents & POLLIN) {
            uint64_t expiration_count;
            ssize_t ret = read(
                term.kbd.repeat.fd, &expiration_count, sizeof(expiration_count));

            if (ret < 0 && errno != EAGAIN)
                LOG_ERRNO("failed to read repeat key from repeat timer fd");
            else if (ret > 0) {
                term.kbd.repeat.dont_re_repeat = true;
                for (size_t i = 0; i < expiration_count; i++)
                    input_repeat(&term, term.kbd.repeat.key);
                term.kbd.repeat.dont_re_repeat = false;
            }
        }

        if (fds[FLASH_FD].revents & POLLIN) {
            uint64_t expiration_count;
            ssize_t ret = read(
                term.flash.fd, &expiration_count, sizeof(expiration_count));

            if (ret < 0 && errno != EAGAIN)
                LOG_ERRNO("failed to read flash timer");
            else if (ret > 0) {
                LOG_DBG("flash timer expired %llu times",
                        (unsigned long long)expiration_count);

                term.flash.active = false;
                term_damage_view(&term);
                render_refresh(&term);
            }
        }

        if (fds[BLINK_FD].revents & POLLIN) {
            uint64_t expiration_count;
            ssize_t ret = read(
                term.blink.fd, &expiration_count, sizeof(expiration_count));

            if (ret < 0 && errno != EAGAIN)
                LOG_ERRNO("failed to read blink timer");
            else if (ret > 0) {
                LOG_DBG("blink timer expired %llu times",
                        (unsigned long long)expiration_count);

                term.blink.state = term.blink.state == BLINK_ON
                    ? BLINK_OFF : BLINK_ON;

                /* Scan all visible cells and mark rows with blinking cells dirty */
                for (int r = 0; r < term.rows; r++) {
                    struct row *row = grid_row_in_view(term.grid, r);
                    for (int col = 0; col < term.cols; col++) {
                        struct cell *cell = &row->cells[col];

                        if (cell->attrs.blink) {
                            cell->attrs.clean = 0;
                            row->dirty = true;
                        }
                    }
                }

                render_refresh(&term);
            }
        }
    }

    close(delay_render_timer_lower);
    close(delay_render_timer_upper);

out:
    mtx_lock(&term.render.workers.lock);
    assert(tll_length(term.render.workers.queue) == 0);
    for (size_t i = 0; i < term.render.workers.count; i++) {
        sem_post(&term.render.workers.start);
        tll_push_back(term.render.workers.queue, -2);
    }
    cnd_broadcast(&term.render.workers.cond);
    mtx_unlock(&term.render.workers.lock);

    shm_fini();

    tll_free(term.wl.on_outputs);
    tll_foreach(term.wl.monitors, it) {
        free(it->item.name);
        if (it->item.xdg != NULL)
            zxdg_output_v1_destroy(it->item.xdg);
        if (it->item.output != NULL)
            wl_output_destroy(it->item.output);
        tll_remove(term.wl.monitors, it);
    }

    if (term.wl.xdg_output_manager != NULL)
        zxdg_output_manager_v1_destroy(term.wl.xdg_output_manager);

    free(term.wl.pointer.theme_name);
    if (term.wl.pointer.theme != NULL)
        wl_cursor_theme_destroy(term.wl.pointer.theme);
    if (term.wl.pointer.pointer != NULL)
        wl_pointer_destroy(term.wl.pointer.pointer);
    if (term.wl.pointer.surface != NULL)
        wl_surface_destroy(term.wl.pointer.surface);
    if (term.wl.keyboard != NULL)
        wl_keyboard_destroy(term.wl.keyboard);
    if (term.selection.clipboard.data_source != NULL)
        wl_data_source_destroy(term.selection.clipboard.data_source);
    if (term.selection.clipboard.data_offer != NULL)
        wl_data_offer_destroy(term.selection.clipboard.data_offer);
    free(term.selection.clipboard.text);
    if (term.wl.data_device != NULL)
        wl_data_device_destroy(term.wl.data_device);
    if (term.wl.data_device_manager != NULL)
        wl_data_device_manager_destroy(term.wl.data_device_manager);
    if (term.selection.primary.data_source != NULL)
        zwp_primary_selection_source_v1_destroy(term.selection.primary.data_source);
    if (term.selection.primary.data_offer != NULL)
        zwp_primary_selection_offer_v1_destroy(term.selection.primary.data_offer);
    free(term.selection.primary.text);
    if (term.wl.primary_selection_device != NULL)
        zwp_primary_selection_device_v1_destroy(term.wl.primary_selection_device);
    if (term.wl.primary_selection_device_manager != NULL)
        zwp_primary_selection_device_manager_v1_destroy(term.wl.primary_selection_device_manager);
    if (term.wl.seat != NULL)
        wl_seat_destroy(term.wl.seat);
    if (term.wl.search_sub_surface != NULL)
        wl_subsurface_destroy(term.wl.search_sub_surface);
    if (term.wl.search_surface != NULL)
        wl_surface_destroy(term.wl.search_surface);
    if (term.render.frame_callback != NULL)
        wl_callback_destroy(term.render.frame_callback);
    if (term.wl.xdg_toplevel_decoration != NULL)
        zxdg_toplevel_decoration_v1_destroy(term.wl.xdg_toplevel_decoration);
    if (term.wl.xdg_decoration_manager != NULL)
        zxdg_decoration_manager_v1_destroy(term.wl.xdg_decoration_manager);
    if (term.wl.xdg_toplevel != NULL)
        xdg_toplevel_destroy(term.wl.xdg_toplevel);
    if (term.wl.xdg_surface != NULL)
        xdg_surface_destroy(term.wl.xdg_surface);
    if (term.wl.shell != NULL)
        xdg_wm_base_destroy(term.wl.shell);
    if (term.wl.surface != NULL)
        wl_surface_destroy(term.wl.surface);
    if (term.wl.shm != NULL)
        wl_shm_destroy(term.wl.shm);
    if (term.wl.sub_compositor != NULL)
        wl_subcompositor_destroy(term.wl.sub_compositor);
    if (term.wl.compositor != NULL)
        wl_compositor_destroy(term.wl.compositor);
    if (term.wl.registry != NULL)
        wl_registry_destroy(term.wl.registry);
    if (term.wl.display != NULL)
        wl_display_disconnect(term.wl.display);
    if (term.kbd.xkb_compose_state != NULL)
        xkb_compose_state_unref(term.kbd.xkb_compose_state);
    if (term.kbd.xkb_compose_table != NULL)
        xkb_compose_table_unref(term.kbd.xkb_compose_table);
    if (term.kbd.xkb_keymap != NULL)
        xkb_keymap_unref(term.kbd.xkb_keymap);
    if (term.kbd.xkb_state != NULL)
        xkb_state_unref(term.kbd.xkb_state);
    if (term.kbd.xkb != NULL)
        xkb_context_unref(term.kbd.xkb);

    free(term.vt.osc.data);
    for (int row = 0; row < term.normal.num_rows; row++)
        grid_row_free(term.normal.rows[row]);
    free(term.normal.rows);
    for (int row = 0; row < term.alt.num_rows; row++)
        grid_row_free(term.alt.rows[row]);
    free(term.alt.rows);

    free(term.window_title);
    tll_free_and_free(term.window_title_stack, free);

    for (size_t i = 0; i < sizeof(term.fonts) / sizeof(term.fonts[0]); i++)
        font_destroy(&term.fonts[i]);

    free(term.search.buf);

    if (term.flash.fd != -1)
        close(term.flash.fd);
    if (term.blink.fd != -1)
        close(term.blink.fd);
    if (term.kbd.repeat.fd != -1)
        close(term.kbd.repeat.fd);

    if (term.ptmx != -1)
        close(term.ptmx);

    for (size_t i = 0; i < term.render.workers.count; i++)
        thrd_join(term.render.workers.threads[i], NULL);
    free(term.render.workers.threads);
    cnd_destroy(&term.render.workers.cond);
    mtx_destroy(&term.render.workers.lock);
    sem_destroy(&term.render.workers.start);
    sem_destroy(&term.render.workers.done);
    assert(tll_length(term.render.workers.queue) == 0);
    tll_free(term.render.workers.queue);

    if (term.slave > 0) {
        /* Note: we've closed ptmx, so the slave *should* exit... */
        int status;
        waitpid(term.slave, &status, 0);

        int child_ret = EXIT_FAILURE;
        if (WIFEXITED(status)) {
            child_ret = WEXITSTATUS(status);
            LOG_DBG("slave exited with code %d", child_ret);
        } else if (WIFSIGNALED(status)) {
            child_ret = WTERMSIG(status);
            LOG_WARN("slave exited with signal %d", child_ret);
        } else {
            LOG_WARN("slave exited for unknown reason (status = 0x%08x)", status);
        }

        if (ret == EXIT_SUCCESS)
            ret = child_ret;
    }

    config_free(conf);
    return ret;

}