-
Notifications
You must be signed in to change notification settings - Fork 5
/
input.c
3107 lines (2582 loc) · 101 KB
/
input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "input.h"
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <threads.h>
#include <locale.h>
#include <errno.h>
#include <wctype.h>
#include <sys/mman.h>
#include <sys/timerfd.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <linux/input-event-codes.h>
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-keysyms.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <xdg-shell.h>
#define LOG_MODULE "input"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "commands.h"
#include "config.h"
#include "grid.h"
#include "keymap.h"
#include "kitty-keymap.h"
#include "macros.h"
#include "quirks.h"
#include "render.h"
#include "search.h"
#include "selection.h"
#include "spawn.h"
#include "terminal.h"
#include "tokenize.h"
#include "unicode-mode.h"
#include "url-mode.h"
#include "util.h"
#include "vt.h"
#include "xmalloc.h"
#include "xsnprintf.h"
struct pipe_context {
char *text;
size_t idx;
size_t left;
};
static bool
fdm_write_pipe(struct fdm *fdm, int fd, int events, void *data)
{
struct pipe_context *ctx = data;
if (events & EPOLLHUP)
goto pipe_closed;
xassert(events & EPOLLOUT);
ssize_t written = write(fd, &ctx->text[ctx->idx], ctx->left);
if (written < 0) {
LOG_WARN("failed to write to pipe: %s", strerror(errno));
goto pipe_closed;
}
xassert(written <= ctx->left);
ctx->idx += written;
ctx->left -= written;
if (ctx->left == 0)
goto pipe_closed;
return true;
pipe_closed:
free(ctx->text);
free(ctx);
fdm_del(fdm, fd);
return true;
}
static void alternate_scroll(struct seat *seat, int amount, int button);
static bool
execute_binding(struct seat *seat, struct terminal *term,
const struct key_binding *binding, uint32_t serial, int amount)
{
const enum bind_action_normal action = binding->action;
switch (action) {
case BIND_ACTION_NONE:
return true;
case BIND_ACTION_NOOP:
return true;
case BIND_ACTION_SCROLLBACK_UP_PAGE:
if (term->grid == &term->normal) {
cmd_scrollback_up(term, term->rows);
return true;
}
break;
case BIND_ACTION_SCROLLBACK_UP_HALF_PAGE:
if (term->grid == &term->normal) {
cmd_scrollback_up(term, max(term->rows / 2, 1));
return true;
}
break;
case BIND_ACTION_SCROLLBACK_UP_LINE:
if (term->grid == &term->normal) {
cmd_scrollback_up(term, 1);
return true;
}
break;
case BIND_ACTION_SCROLLBACK_UP_MOUSE:
if (term->grid == &term->alt) {
if (term->alt_scrolling)
alternate_scroll(seat, amount, BTN_BACK);
} else
cmd_scrollback_up(term, amount);
break;
case BIND_ACTION_SCROLLBACK_DOWN_PAGE:
if (term->grid == &term->normal) {
cmd_scrollback_down(term, term->rows);
return true;
}
break;
case BIND_ACTION_SCROLLBACK_DOWN_HALF_PAGE:
if (term->grid == &term->normal) {
cmd_scrollback_down(term, max(term->rows / 2, 1));
return true;
}
break;
case BIND_ACTION_SCROLLBACK_DOWN_LINE:
if (term->grid == &term->normal) {
cmd_scrollback_down(term, 1);
return true;
}
break;
case BIND_ACTION_SCROLLBACK_DOWN_MOUSE:
if (term->grid == &term->alt) {
if (term->alt_scrolling)
alternate_scroll(seat, amount, BTN_FORWARD);
} else
cmd_scrollback_down(term, amount);
break;
case BIND_ACTION_SCROLLBACK_HOME:
if (term->grid == &term->normal) {
cmd_scrollback_up(term, term->grid->num_rows);
return true;
}
break;
case BIND_ACTION_SCROLLBACK_END:
if (term->grid == &term->normal) {
cmd_scrollback_down(term, term->grid->num_rows);
return true;
}
break;
case BIND_ACTION_CLIPBOARD_COPY:
selection_to_clipboard(seat, term, serial);
return true;
case BIND_ACTION_CLIPBOARD_PASTE:
selection_from_clipboard(seat, term, serial);
term_reset_view(term);
return true;
case BIND_ACTION_PRIMARY_PASTE:
selection_from_primary(seat, term);
term_reset_view(term);
return true;
case BIND_ACTION_SEARCH_START:
search_begin(term);
return true;
case BIND_ACTION_FONT_SIZE_UP:
term_font_size_increase(term);
return true;
case BIND_ACTION_FONT_SIZE_DOWN:
term_font_size_decrease(term);
return true;
case BIND_ACTION_FONT_SIZE_RESET:
term_font_size_reset(term);
return true;
case BIND_ACTION_SPAWN_TERMINAL:
term_spawn_new(term);
return true;
case BIND_ACTION_MINIMIZE:
xdg_toplevel_set_minimized(term->window->xdg_toplevel);
return true;
case BIND_ACTION_MAXIMIZE:
if (term->window->is_fullscreen)
xdg_toplevel_unset_fullscreen(term->window->xdg_toplevel);
if (term->window->is_maximized)
xdg_toplevel_unset_maximized(term->window->xdg_toplevel);
else
xdg_toplevel_set_maximized(term->window->xdg_toplevel);
return true;
case BIND_ACTION_FULLSCREEN:
if (term->window->is_fullscreen)
xdg_toplevel_unset_fullscreen(term->window->xdg_toplevel);
else
xdg_toplevel_set_fullscreen(term->window->xdg_toplevel, NULL);
return true;
case BIND_ACTION_PIPE_SCROLLBACK:
if (term->grid == &term->alt)
break;
/* FALLTHROUGH */
case BIND_ACTION_PIPE_VIEW:
case BIND_ACTION_PIPE_SELECTED:
case BIND_ACTION_PIPE_COMMAND_OUTPUT: {
if (binding->aux->type != BINDING_AUX_PIPE)
return true;
struct pipe_context *ctx = NULL;
int pipe_fd[2] = {-1, -1};
int stdout_fd = -1;
int stderr_fd = -1;
char *text = NULL;
size_t len = 0;
if (pipe(pipe_fd) < 0) {
LOG_ERRNO("failed to create pipe");
goto pipe_err;
}
stdout_fd = open("/dev/null", O_WRONLY);
stderr_fd = open("/dev/null", O_WRONLY);
if (stdout_fd < 0 || stderr_fd < 0) {
LOG_ERRNO("failed to open /dev/null");
goto pipe_err;
}
bool success;
switch (action) {
case BIND_ACTION_PIPE_SCROLLBACK:
success = term_scrollback_to_text(term, &text, &len);
break;
case BIND_ACTION_PIPE_VIEW:
success = term_view_to_text(term, &text, &len);
break;
case BIND_ACTION_PIPE_SELECTED:
text = selection_to_text(term);
success = text != NULL;
len = text != NULL ? strlen(text) : 0;
break;
case BIND_ACTION_PIPE_COMMAND_OUTPUT:
success = term_command_output_to_text(term, &text, &len);
break;
default:
BUG("Unhandled action type");
success = false;
break;
}
if (!success)
goto pipe_err;
/* Make write-end non-blocking; required by the FDM */
{
int flags = fcntl(pipe_fd[1], F_GETFL);
if (flags < 0 ||
fcntl(pipe_fd[1], F_SETFL, flags | O_NONBLOCK) < 0)
{
LOG_ERRNO("failed to make write-end of pipe non-blocking");
goto pipe_err;
}
}
/* Make sure write-end is closed on exec() - or the spawned
* program may not terminate*/
{
int flags = fcntl(pipe_fd[1], F_GETFD);
if (flags < 0 ||
fcntl(pipe_fd[1], F_SETFD, flags | FD_CLOEXEC) < 0)
{
LOG_ERRNO("failed to set FD_CLOEXEC on writeend of pipe");
goto pipe_err;
}
}
if (spawn(term->reaper, term->cwd, binding->aux->pipe.args,
pipe_fd[0], stdout_fd, stderr_fd, NULL, NULL, NULL) < 0)
goto pipe_err;
/* Close read end */
close(pipe_fd[0]);
ctx = xmalloc(sizeof(*ctx));
*ctx = (struct pipe_context){
.text = text,
.left = len,
};
/* Asynchronously write the output to the pipe */
if (!fdm_add(term->fdm, pipe_fd[1], EPOLLOUT, &fdm_write_pipe, ctx))
goto pipe_err;
return true;
pipe_err:
if (stdout_fd >= 0)
close(stdout_fd);
if (stderr_fd >= 0)
close(stderr_fd);
if (pipe_fd[0] >= 0)
close(pipe_fd[0]);
if (pipe_fd[1] >= 0)
close(pipe_fd[1]);
free(text);
free(ctx);
return true;
}
case BIND_ACTION_SHOW_URLS_COPY:
case BIND_ACTION_SHOW_URLS_LAUNCH:
case BIND_ACTION_SHOW_URLS_PERSISTENT: {
xassert(!urls_mode_is_active(term));
enum url_action url_action =
action == BIND_ACTION_SHOW_URLS_COPY ? URL_ACTION_COPY :
action == BIND_ACTION_SHOW_URLS_LAUNCH ? URL_ACTION_LAUNCH :
URL_ACTION_PERSISTENT;
urls_collect(term, url_action, &term->urls);
urls_assign_key_combos(term->conf, &term->urls);
urls_render(term);
return true;
}
case BIND_ACTION_TEXT_BINDING:
xassert(binding->aux->type == BINDING_AUX_TEXT);
term_to_slave(term, binding->aux->text.data, binding->aux->text.len);
return true;
case BIND_ACTION_PROMPT_PREV: {
if (term->grid != &term->normal)
return false;
struct grid *grid = term->grid;
const int sb_start =
grid_sb_start_ignore_uninitialized(grid, term->rows);
/* Check each row from current view-1 (that is, the first
* currently not visible row), up to, and including, the
* scrollback start */
for (int r_sb_rel =
grid_row_abs_to_sb_precalc_sb_start(
grid, sb_start, grid->view) - 1;
r_sb_rel >= 0; r_sb_rel--)
{
const int r_abs =
grid_row_sb_to_abs_precalc_sb_start(grid, sb_start, r_sb_rel);
const struct row *row = grid->rows[r_abs];
xassert(row != NULL);
if (!row->shell_integration.prompt_marker)
continue;
grid->view = r_abs;
term_damage_view(term);
render_refresh(term);
break;
}
return true;
}
case BIND_ACTION_PROMPT_NEXT: {
if (term->grid != &term->normal)
return false;
struct grid *grid = term->grid;
const int num_rows = grid->num_rows;
if (grid->view == grid->offset) {
/* Already at the bottom */
return true;
}
/* Check each row from view+1, to the bottom of the scrollback */
for (int r_abs = (grid->view + 1) & (num_rows - 1);
;
r_abs = (r_abs + 1) & (num_rows - 1))
{
const struct row *row = grid->rows[r_abs];
xassert(row != NULL);
if (!row->shell_integration.prompt_marker) {
if (r_abs == grid->offset + term->rows - 1) {
/* We've reached the bottom of the scrollback */
break;
}
continue;
}
int sb_start = grid_sb_start_ignore_uninitialized(grid, term->rows);
int ofs_sb_rel =
grid_row_abs_to_sb_precalc_sb_start(grid, sb_start, grid->offset);
int new_view_sb_rel =
grid_row_abs_to_sb_precalc_sb_start(grid, sb_start, r_abs);
new_view_sb_rel = min(ofs_sb_rel, new_view_sb_rel);
grid->view = grid_row_sb_to_abs_precalc_sb_start(
grid, sb_start, new_view_sb_rel);
term_damage_view(term);
render_refresh(term);
break;
}
return true;
}
case BIND_ACTION_UNICODE_INPUT:
unicode_mode_activate(term);
return true;
case BIND_ACTION_QUIT:
term_shutdown(term);
return true;
case BIND_ACTION_SELECT_BEGIN:
selection_start(
term, seat->mouse.col, seat->mouse.row, SELECTION_CHAR_WISE, false);
return true;
case BIND_ACTION_SELECT_BEGIN_BLOCK:
selection_start(
term, seat->mouse.col, seat->mouse.row, SELECTION_BLOCK, false);
return true;
case BIND_ACTION_SELECT_EXTEND:
selection_extend(
seat, term, seat->mouse.col, seat->mouse.row, term->selection.kind);
return true;
case BIND_ACTION_SELECT_EXTEND_CHAR_WISE:
if (term->selection.kind != SELECTION_BLOCK) {
selection_extend(
seat, term, seat->mouse.col, seat->mouse.row, SELECTION_CHAR_WISE);
return true;
}
return false;
case BIND_ACTION_SELECT_WORD:
selection_start(
term, seat->mouse.col, seat->mouse.row, SELECTION_WORD_WISE, false);
return true;
case BIND_ACTION_SELECT_WORD_WS:
selection_start(
term, seat->mouse.col, seat->mouse.row, SELECTION_WORD_WISE, true);
return true;
case BIND_ACTION_SELECT_QUOTE:
selection_start(
term, seat->mouse.col, seat->mouse.row, SELECTION_QUOTE_WISE, false);
break;
case BIND_ACTION_SELECT_ROW:
selection_start(
term, seat->mouse.col, seat->mouse.row, SELECTION_LINE_WISE, false);
return true;
case BIND_ACTION_COUNT:
BUG("Invalid action type");
return false;
}
return false;
}
static void
keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,
uint32_t format, int32_t fd, uint32_t size)
{
LOG_DBG("keyboard_keymap: keyboard=%p (format=%u, size=%u)",
(void *)wl_keyboard, format, size);
struct seat *seat = data;
struct wayland *wayl = seat->wayl;
/*
* Free old keymap state
*/
if (seat->kbd.xkb_keymap != NULL) {
xkb_keymap_unref(seat->kbd.xkb_keymap);
seat->kbd.xkb_keymap = NULL;
}
if (seat->kbd.xkb_state != NULL) {
xkb_state_unref(seat->kbd.xkb_state);
seat->kbd.xkb_state = NULL;
}
key_binding_unload_keymap(wayl->key_binding_manager, seat);
/* Verify keymap is in a format we understand */
switch ((enum wl_keyboard_keymap_format)format) {
case WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP:
return;
case WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1:
break;
default:
LOG_WARN("unrecognized keymap format: %u", format);
return;
}
char *map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
if (map_str == MAP_FAILED) {
LOG_ERRNO("failed to mmap keyboard keymap");
close(fd);
return;
}
while (map_str[size - 1] == '\0')
size--;
if (seat->kbd.xkb != NULL) {
seat->kbd.xkb_keymap = xkb_keymap_new_from_buffer(
seat->kbd.xkb, map_str, size, XKB_KEYMAP_FORMAT_TEXT_V1,
XKB_KEYMAP_COMPILE_NO_FLAGS);
}
if (seat->kbd.xkb_keymap != NULL) {
seat->kbd.xkb_state = xkb_state_new(seat->kbd.xkb_keymap);
seat->kbd.mod_shift = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_SHIFT);
seat->kbd.mod_alt = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_ALT) ;
seat->kbd.mod_ctrl = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_CTRL);
seat->kbd.mod_super = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_LOGO);
seat->kbd.mod_caps = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_CAPS);
seat->kbd.mod_num = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_NUM);
/* Significant modifiers in the legacy keyboard protocol */
seat->kbd.legacy_significant = 0;
if (seat->kbd.mod_shift != XKB_MOD_INVALID)
seat->kbd.legacy_significant |= 1 << seat->kbd.mod_shift;
if (seat->kbd.mod_alt != XKB_MOD_INVALID)
seat->kbd.legacy_significant |= 1 << seat->kbd.mod_alt;
if (seat->kbd.mod_ctrl != XKB_MOD_INVALID)
seat->kbd.legacy_significant |= 1 << seat->kbd.mod_ctrl;
if (seat->kbd.mod_super != XKB_MOD_INVALID)
seat->kbd.legacy_significant |= 1 << seat->kbd.mod_super;
/* Significant modifiers in the kitty keyboard protocol */
seat->kbd.kitty_significant = seat->kbd.legacy_significant;
if (seat->kbd.mod_caps != XKB_MOD_INVALID)
seat->kbd.kitty_significant |= 1 << seat->kbd.mod_caps;
if (seat->kbd.mod_num != XKB_MOD_INVALID)
seat->kbd.kitty_significant |= 1 << seat->kbd.mod_num;
seat->kbd.key_arrow_up = xkb_keymap_key_by_name(seat->kbd.xkb_keymap, "UP");
seat->kbd.key_arrow_down = xkb_keymap_key_by_name(seat->kbd.xkb_keymap, "DOWN");
}
munmap(map_str, size);
close(fd);
key_binding_load_keymap(wayl->key_binding_manager, seat);
}
static void
keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,
struct wl_surface *surface, struct wl_array *keys)
{
xassert(surface != NULL);
xassert(serial != 0);
struct seat *seat = data;
struct wl_window *win = wl_surface_get_user_data(surface);
struct terminal *term = win->term;
LOG_DBG("%s: keyboard_enter: keyboard=%p, serial=%u, surface=%p",
seat->name, (void *)wl_keyboard, serial, (void *)surface);
term_kbd_focus_in(term);
seat->kbd_focus = term;
seat->kbd.serial = serial;
}
static bool
start_repeater(struct seat *seat, uint32_t key)
{
if (seat->kbd.repeat.dont_re_repeat)
return true;
if (seat->kbd.repeat.rate == 0)
return true;
struct itimerspec t = {
.it_value = {.tv_sec = 0, .tv_nsec = seat->kbd.repeat.delay * 1000000},
.it_interval = {.tv_sec = 0, .tv_nsec = 1000000000 / seat->kbd.repeat.rate},
};
if (t.it_value.tv_nsec >= 1000000000) {
t.it_value.tv_sec += t.it_value.tv_nsec / 1000000000;
t.it_value.tv_nsec %= 1000000000;
}
if (t.it_interval.tv_nsec >= 1000000000) {
t.it_interval.tv_sec += t.it_interval.tv_nsec / 1000000000;
t.it_interval.tv_nsec %= 1000000000;
}
if (timerfd_settime(seat->kbd.repeat.fd, 0, &t, NULL) < 0) {
LOG_ERRNO("%s: failed to arm keyboard repeat timer", seat->name);
return false;
}
seat->kbd.repeat.key = key;
return true;
}
static bool
stop_repeater(struct seat *seat, uint32_t key)
{
if (key != -1 && key != seat->kbd.repeat.key)
return true;
if (timerfd_settime(seat->kbd.repeat.fd, 0, &(struct itimerspec){{0}}, NULL) < 0) {
LOG_ERRNO("%s: failed to disarm keyboard repeat timer", seat->name);
return false;
}
return true;
}
static void
keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,
struct wl_surface *surface)
{
struct seat *seat = data;
LOG_DBG("keyboard_leave: keyboard=%p, serial=%u, surface=%p",
(void *)wl_keyboard, serial, (void *)surface);
xassert(
seat->kbd_focus == NULL ||
surface == NULL || /* Seen on Sway 1.2 */
((const struct wl_window *)wl_surface_get_user_data(surface))->term == seat->kbd_focus
);
struct terminal *old_focused = seat->kbd_focus;
seat->kbd_focus = NULL;
stop_repeater(seat, -1);
seat->kbd.shift = false;
seat->kbd.alt = false;
seat->kbd.ctrl = false;
seat->kbd.super = false;
if (seat->kbd.xkb_compose_state != NULL)
xkb_compose_state_reset(seat->kbd.xkb_compose_state);
if (old_focused != NULL) {
seat->pointer.hidden = false;
term_xcursor_update_for_seat(old_focused, seat);
term_kbd_focus_out(old_focused);
} else {
/*
* Sway bug - under certain conditions we get a
* keyboard_leave() (and keyboard_key()) without first having
* received a keyboard_enter()
*/
LOG_WARN(
"compositor sent keyboard_leave event without a keyboard_enter "
"event: surface=%p", (void *)surface);
}
}
static const struct key_data *
keymap_data_for_sym(xkb_keysym_t sym, size_t *count)
{
switch (sym) {
case XKB_KEY_Escape: *count = ALEN(key_escape); return key_escape;
case XKB_KEY_Return: *count = ALEN(key_return); return key_return;
case XKB_KEY_ISO_Left_Tab: *count = ALEN(key_iso_left_tab); return key_iso_left_tab;
case XKB_KEY_Tab: *count = ALEN(key_tab); return key_tab;
case XKB_KEY_BackSpace: *count = ALEN(key_backspace); return key_backspace;
case XKB_KEY_Up: *count = ALEN(key_up); return key_up;
case XKB_KEY_Down: *count = ALEN(key_down); return key_down;
case XKB_KEY_Right: *count = ALEN(key_right); return key_right;
case XKB_KEY_Left: *count = ALEN(key_left); return key_left;
case XKB_KEY_Home: *count = ALEN(key_home); return key_home;
case XKB_KEY_End: *count = ALEN(key_end); return key_end;
case XKB_KEY_Insert: *count = ALEN(key_insert); return key_insert;
case XKB_KEY_Delete: *count = ALEN(key_delete); return key_delete;
case XKB_KEY_Page_Up: *count = ALEN(key_pageup); return key_pageup;
case XKB_KEY_Page_Down: *count = ALEN(key_pagedown); return key_pagedown;
case XKB_KEY_F1: *count = ALEN(key_f1); return key_f1;
case XKB_KEY_F2: *count = ALEN(key_f2); return key_f2;
case XKB_KEY_F3: *count = ALEN(key_f3); return key_f3;
case XKB_KEY_F4: *count = ALEN(key_f4); return key_f4;
case XKB_KEY_F5: *count = ALEN(key_f5); return key_f5;
case XKB_KEY_F6: *count = ALEN(key_f6); return key_f6;
case XKB_KEY_F7: *count = ALEN(key_f7); return key_f7;
case XKB_KEY_F8: *count = ALEN(key_f8); return key_f8;
case XKB_KEY_F9: *count = ALEN(key_f9); return key_f9;
case XKB_KEY_F10: *count = ALEN(key_f10); return key_f10;
case XKB_KEY_F11: *count = ALEN(key_f11); return key_f11;
case XKB_KEY_F12: *count = ALEN(key_f12); return key_f12;
case XKB_KEY_F13: *count = ALEN(key_f13); return key_f13;
case XKB_KEY_F14: *count = ALEN(key_f14); return key_f14;
case XKB_KEY_F15: *count = ALEN(key_f15); return key_f15;
case XKB_KEY_F16: *count = ALEN(key_f16); return key_f16;
case XKB_KEY_F17: *count = ALEN(key_f17); return key_f17;
case XKB_KEY_F18: *count = ALEN(key_f18); return key_f18;
case XKB_KEY_F19: *count = ALEN(key_f19); return key_f19;
case XKB_KEY_F20: *count = ALEN(key_f20); return key_f20;
case XKB_KEY_F21: *count = ALEN(key_f21); return key_f21;
case XKB_KEY_F22: *count = ALEN(key_f22); return key_f22;
case XKB_KEY_F23: *count = ALEN(key_f23); return key_f23;
case XKB_KEY_F24: *count = ALEN(key_f24); return key_f24;
case XKB_KEY_F25: *count = ALEN(key_f25); return key_f25;
case XKB_KEY_F26: *count = ALEN(key_f26); return key_f26;
case XKB_KEY_F27: *count = ALEN(key_f27); return key_f27;
case XKB_KEY_F28: *count = ALEN(key_f28); return key_f28;
case XKB_KEY_F29: *count = ALEN(key_f29); return key_f29;
case XKB_KEY_F30: *count = ALEN(key_f30); return key_f30;
case XKB_KEY_F31: *count = ALEN(key_f31); return key_f31;
case XKB_KEY_F32: *count = ALEN(key_f32); return key_f32;
case XKB_KEY_F33: *count = ALEN(key_f33); return key_f33;
case XKB_KEY_F34: *count = ALEN(key_f34); return key_f34;
case XKB_KEY_F35: *count = ALEN(key_f35); return key_f35;
case XKB_KEY_KP_Up: *count = ALEN(key_kp_up); return key_kp_up;
case XKB_KEY_KP_Down: *count = ALEN(key_kp_down); return key_kp_down;
case XKB_KEY_KP_Right: *count = ALEN(key_kp_right); return key_kp_right;
case XKB_KEY_KP_Left: *count = ALEN(key_kp_left); return key_kp_left;
case XKB_KEY_KP_Begin: *count = ALEN(key_kp_begin); return key_kp_begin;
case XKB_KEY_KP_Home: *count = ALEN(key_kp_home); return key_kp_home;
case XKB_KEY_KP_End: *count = ALEN(key_kp_end); return key_kp_end;
case XKB_KEY_KP_Insert: *count = ALEN(key_kp_insert); return key_kp_insert;
case XKB_KEY_KP_Delete: *count = ALEN(key_kp_delete); return key_kp_delete;
case XKB_KEY_KP_Page_Up: *count = ALEN(key_kp_pageup); return key_kp_pageup;
case XKB_KEY_KP_Page_Down: *count = ALEN(key_kp_pagedown); return key_kp_pagedown;
case XKB_KEY_KP_Enter: *count = ALEN(key_kp_enter); return key_kp_enter;
case XKB_KEY_KP_Divide: *count = ALEN(key_kp_divide); return key_kp_divide;
case XKB_KEY_KP_Multiply: *count = ALEN(key_kp_multiply); return key_kp_multiply;
case XKB_KEY_KP_Subtract: *count = ALEN(key_kp_subtract); return key_kp_subtract;
case XKB_KEY_KP_Add: *count = ALEN(key_kp_add); return key_kp_add;
case XKB_KEY_KP_Separator: *count = ALEN(key_kp_separator); return key_kp_separator;
case XKB_KEY_KP_Decimal: *count = ALEN(key_kp_decimal); return key_kp_decimal;
case XKB_KEY_KP_0: *count = ALEN(key_kp_0); return key_kp_0;
case XKB_KEY_KP_1: *count = ALEN(key_kp_1); return key_kp_1;
case XKB_KEY_KP_2: *count = ALEN(key_kp_2); return key_kp_2;
case XKB_KEY_KP_3: *count = ALEN(key_kp_3); return key_kp_3;
case XKB_KEY_KP_4: *count = ALEN(key_kp_4); return key_kp_4;
case XKB_KEY_KP_5: *count = ALEN(key_kp_5); return key_kp_5;
case XKB_KEY_KP_6: *count = ALEN(key_kp_6); return key_kp_6;
case XKB_KEY_KP_7: *count = ALEN(key_kp_7); return key_kp_7;
case XKB_KEY_KP_8: *count = ALEN(key_kp_8); return key_kp_8;
case XKB_KEY_KP_9: *count = ALEN(key_kp_9); return key_kp_9;
}
return NULL;
}
static const struct key_data *
keymap_lookup(struct terminal *term, xkb_keysym_t sym, enum modifier mods)
{
size_t count;
const struct key_data *info = keymap_data_for_sym(sym, &count);
if (info == NULL)
return NULL;
const enum cursor_keys cursor_keys_mode = term->cursor_keys_mode;
const enum keypad_keys keypad_keys_mode
= term->num_lock_modifier ? KEYPAD_NUMERICAL : term->keypad_keys_mode;
LOG_DBG("keypad mode: %d", keypad_keys_mode);
for (size_t j = 0; j < count; j++) {
enum modifier modifiers = info[j].modifiers;
if (modifiers & MOD_MODIFY_OTHER_KEYS_STATE1) {
if (term->modify_other_keys_2)
continue;
modifiers &= ~MOD_MODIFY_OTHER_KEYS_STATE1;
}
if (modifiers & MOD_MODIFY_OTHER_KEYS_STATE2) {
if (!term->modify_other_keys_2)
continue;
modifiers &= ~MOD_MODIFY_OTHER_KEYS_STATE2;
}
if (modifiers != MOD_ANY && modifiers != mods)
continue;
if (info[j].cursor_keys_mode != CURSOR_KEYS_DONTCARE &&
info[j].cursor_keys_mode != cursor_keys_mode)
continue;
if (info[j].keypad_keys_mode != KEYPAD_DONTCARE &&
info[j].keypad_keys_mode != keypad_keys_mode)
continue;
return &info[j];
}
return NULL;
}
UNITTEST
{
struct terminal term = {
.num_lock_modifier = false,
.keypad_keys_mode = KEYPAD_NUMERICAL,
.cursor_keys_mode = CURSOR_KEYS_NORMAL,
};
const struct key_data *info = keymap_lookup(&term, XKB_KEY_ISO_Left_Tab, MOD_SHIFT | MOD_CTRL);
xassert(info != NULL);
xassert(streq(info->seq, "\033[27;6;9~"));
}
UNITTEST
{
struct terminal term = {
.modify_other_keys_2 = false,
};
const struct key_data *info = keymap_lookup(&term, XKB_KEY_Return, MOD_ALT);
xassert(info != NULL);
xassert(streq(info->seq, "\033\r"));
term.modify_other_keys_2 = true;
info = keymap_lookup(&term, XKB_KEY_Return, MOD_ALT);
xassert(info != NULL);
xassert(streq(info->seq, "\033[27;3;13~"));
}
void
get_current_modifiers(const struct seat *seat,
xkb_mod_mask_t *effective,
xkb_mod_mask_t *consumed, uint32_t key,
bool filter_locked)
{
if (unlikely(seat->kbd.xkb_state == NULL)) {
if (effective != NULL)
*effective = 0;
if (consumed != NULL)
*consumed = 0;
}
else {
const xkb_mod_mask_t locked =
xkb_state_serialize_mods(seat->kbd.xkb_state, XKB_STATE_MODS_LOCKED);
if (effective != NULL) {
*effective = xkb_state_serialize_mods(
seat->kbd.xkb_state, XKB_STATE_MODS_EFFECTIVE);
if (filter_locked)
*effective &= ~locked;
}
if (consumed != NULL) {
*consumed = xkb_state_key_get_consumed_mods2(
seat->kbd.xkb_state, key, XKB_CONSUMED_MODE_XKB);
if (filter_locked)
*consumed &= ~locked;
}
}
}
struct kbd_ctx {
xkb_layout_index_t layout;
xkb_keycode_t key;
xkb_keysym_t sym;
struct {
const xkb_keysym_t *syms;
size_t count;
} level0_syms;
xkb_mod_mask_t mods;
xkb_mod_mask_t consumed;
struct {
const uint8_t *buf;
size_t count;
} utf8;
uint32_t *utf32;
enum xkb_compose_status compose_status;
enum wl_keyboard_key_state key_state;
};
static bool
legacy_kbd_protocol(struct seat *seat, struct terminal *term,
const struct kbd_ctx *ctx)
{
if (ctx->key_state != WL_KEYBOARD_KEY_STATE_PRESSED)
return false;
if (ctx->compose_status == XKB_COMPOSE_COMPOSING)
return false;
enum modifier keymap_mods = MOD_NONE;
keymap_mods |= seat->kbd.shift ? MOD_SHIFT : MOD_NONE;
keymap_mods |= seat->kbd.alt ? MOD_ALT : MOD_NONE;
keymap_mods |= seat->kbd.ctrl ? MOD_CTRL : MOD_NONE;
keymap_mods |= seat->kbd.super ? MOD_META : MOD_NONE;
const xkb_keysym_t sym = ctx->sym;
const size_t count = ctx->utf8.count;
const uint8_t *const utf8 = ctx->utf8.buf;
const struct key_data *keymap = keymap_lookup(term, sym, keymap_mods);
if (keymap != NULL) {
term_to_slave(term, keymap->seq, strlen(keymap->seq));
return true;
}
if (count == 0)
return false;
#define is_control_key(x) ((x) >= 0x40 && (x) <= 0x7f)
#define IS_CTRL(x) ((x) < 0x20 || ((x) >= 0x7f && (x) <= 0x9f))
//LOG_DBG("term->modify_other_keys=%d, count=%zu, is_ctrl=%d (utf8=0x%02x), sym=%d",
//term->modify_other_keys_2, count, IS_CTRL(utf8[0]), utf8[0], sym);
bool ctrl_is_in_effect = (keymap_mods & MOD_CTRL) != 0;
bool ctrl_seq = is_control_key(sym) || (count == 1 && IS_CTRL(utf8[0]));
bool modify_other_keys2_in_effect = false;
if (term->modify_other_keys_2) {
/*
* Try to mimic XTerm's behavior, when holding shift:
*
* - if other modifiers are pressed (e.g. Alt), emit a CSI escape
* - upper-case symbols A-Z are encoded as an CSI escape
* - other upper-case symbols (e.g 'Ö') or emitted as is
* - non-upper cased symbols are _mostly_ emitted as is (foot
* always emits as is)
*
* Examples (assuming Swedish layout):
* - Shift-a ('A') emits a CSI
* - Shift-, (';') emits ';'
* - Shift-Alt-, (Alt-;) emits a CSI
* - Shift-ö ('Ö') emits 'Ö'
*/
/* Any modifiers, besides shift active? */
const xkb_mod_mask_t shift_mask = 1 << seat->kbd.mod_shift;
if ((ctx->mods & ~shift_mask & seat->kbd.legacy_significant) != 0)
modify_other_keys2_in_effect = true;
else {
const xkb_layout_index_t layout_idx = xkb_state_key_get_layout(
seat->kbd.xkb_state, ctx->key);
/*
* Get pressed key's base symbol.
* - for 'A' (shift-a), that's 'a'
* - for ';' (shift-,), that's ','
*/
const xkb_keysym_t *base_syms = NULL;
size_t base_count = xkb_keymap_key_get_syms_by_level(
seat->kbd.xkb_keymap, ctx->key, layout_idx, 0, &base_syms);
/* Check if base symbol(s) is a-z. If so, emit CSI */
const xkb_keysym_t lower_cased_sym = xkb_keysym_to_lower(ctx->sym);
for (size_t i = 0; i < base_count; i++) {
const xkb_keysym_t s = base_syms[i];
if (lower_cased_sym == s && s >= XKB_KEY_a && s <= XKB_KEY_z) {
modify_other_keys2_in_effect = true;