Codebergdnkl

foot

master
ply5 git calls824 ms53 KiB2 cachedprotocol v2
3687 lines124 KiBRaw
1#include "input.h"
3#include <string.h>
4#include <unistd.h>
5#include <signal.h>
6#include <threads.h>
7#include <locale.h>
8#include <errno.h>
9#include <wctype.h>
10#include <sys/mman.h>
11#include <sys/timerfd.h>
12#include <sys/epoll.h>
13#include <fcntl.h>
15#include <linux/input-event-codes.h>
17#include <xkbcommon/xkbcommon.h>
18#include <xkbcommon/xkbcommon-keysyms.h>
19#include <xkbcommon/xkbcommon-compose.h>
21#include <xdg-shell.h>
23#define LOG_MODULE "input"
24#define LOG_ENABLE_DBG 0
25#include "log.h"
26#include "commands.h"
27#include "config.h"
28#include "grid.h"
29#include "keymap.h"
30#include "kitty-keymap.h"
31#include "macros.h"
32#include "quirks.h"
33#include "render.h"
34#include "search.h"
35#include "selection.h"
36#include "spawn.h"
37#include "terminal.h"
38#include "tokenize.h"
39#include "unicode-mode.h"
40#include "url-mode.h"
41#include "util.h"
42#include "vt.h"
43#include "xkbcommon-vmod.h"
44#include "xmalloc.h"
45#include "xsnprintf.h"
47struct pipe_context {
48 char *text;
49 size_t idx;
50 size_t left;
51};
53static bool
54fdm_write_pipe(struct fdm *fdm, int fd, int events, void *data)
56 struct pipe_context *ctx = data;
58 if (events & EPOLLHUP)
59 goto pipe_closed;
61 xassert(events & EPOLLOUT);
62 ssize_t written = write(fd, &ctx->text[ctx->idx], ctx->left);
64 if (written < 0) {
65 LOG_WARN("failed to write to pipe: %s", strerror(errno));
66 goto pipe_closed;
67 }
69 xassert(written <= ctx->left);
70 ctx->idx += written;
71 ctx->left -= written;
73 if (ctx->left == 0)
74 goto pipe_closed;
76 return true;
78pipe_closed:
79 free(ctx->text);
80 free(ctx);
81 fdm_del(fdm, fd);
82 return true;
85static void alternate_scroll(struct seat *seat, int amount, int button);
87static bool
88execute_binding(struct seat *seat, struct terminal *term,
89 const struct key_binding *binding, uint32_t serial, int amount)
91 const enum bind_action_normal action = binding->action;
93 switch (action) {
94 case BIND_ACTION_NONE:
95 return true;
97 case BIND_ACTION_NOOP:
98 return true;
100 case BIND_ACTION_SCROLLBACK_UP_PAGE:
101 if (term->grid == &term->normal) {
102 cmd_scrollback_up(term, term->rows);
103 return true;
104 }
105 break;
107 case BIND_ACTION_SCROLLBACK_UP_HALF_PAGE:
108 if (term->grid == &term->normal) {
109 cmd_scrollback_up(term, max(term->rows / 2, 1));
110 return true;
111 }
112 break;
114 case BIND_ACTION_SCROLLBACK_UP_LINE:
115 if (term->grid == &term->normal) {
116 cmd_scrollback_up(term, 1);
117 return true;
118 }
119 break;
121 case BIND_ACTION_SCROLLBACK_UP_MOUSE:
122 if (term->grid == &term->alt) {
123 if (term->alt_scrolling) {
124 alternate_scroll(seat, amount, BTN_BACK);
125 return true;
126 }
127 } else {
128 cmd_scrollback_up(term, amount);
129 return true;
130 }
131 break;
133 case BIND_ACTION_SCROLLBACK_DOWN_PAGE:
134 if (term->grid == &term->normal) {
135 cmd_scrollback_down(term, term->rows);
136 return true;
137 }
138 break;
140 case BIND_ACTION_SCROLLBACK_DOWN_HALF_PAGE:
141 if (term->grid == &term->normal) {
142 cmd_scrollback_down(term, max(term->rows / 2, 1));
143 return true;
144 }
145 break;
147 case BIND_ACTION_SCROLLBACK_DOWN_LINE:
148 if (term->grid == &term->normal) {
149 cmd_scrollback_down(term, 1);
150 return true;
151 }
152 break;
154 case BIND_ACTION_SCROLLBACK_DOWN_MOUSE:
155 if (term->grid == &term->alt) {
156 if (term->alt_scrolling) {
157 alternate_scroll(seat, amount, BTN_FORWARD);
158 return true;
159 }
160 } else {
161 cmd_scrollback_down(term, amount);
162 return true;
163 }
164 break;
166 case BIND_ACTION_SCROLLBACK_HOME:
167 if (term->grid == &term->normal) {
168 cmd_scrollback_up(term, term->grid->num_rows);
169 return true;
170 }
171 break;
173 case BIND_ACTION_SCROLLBACK_END:
174 if (term->grid == &term->normal) {
175 cmd_scrollback_down(term, term->grid->num_rows);
176 return true;
177 }
178 break;
180 case BIND_ACTION_CLIPBOARD_COPY:
181 selection_to_clipboard(seat, term, serial);
182 return true;
184 case BIND_ACTION_CLIPBOARD_PASTE:
185 selection_from_clipboard(seat, term, serial);
186 term_reset_view(term);
187 return true;
189 case BIND_ACTION_PRIMARY_PASTE:
190 selection_from_primary(seat, term);
191 term_reset_view(term);
192 return true;
194 case BIND_ACTION_SEARCH_START:
195 search_begin(term);
196 return true;
198 case BIND_ACTION_FONT_SIZE_UP:
199 term_font_size_increase(term);
200 return true;
202 case BIND_ACTION_FONT_SIZE_DOWN:
203 term_font_size_decrease(term);
204 return true;
206 case BIND_ACTION_FONT_SIZE_RESET:
207 term_font_size_reset(term);
208 return true;
210 case BIND_ACTION_SPAWN_TERMINAL:
211 term_spawn_new(term);
212 return true;
214 case BIND_ACTION_MINIMIZE:
215 xdg_toplevel_set_minimized(term->window->xdg_toplevel);
216 return true;
218 case BIND_ACTION_MAXIMIZE:
219 if (term->window->is_fullscreen)
220 xdg_toplevel_unset_fullscreen(term->window->xdg_toplevel);
221 if (term->window->is_maximized)
222 xdg_toplevel_unset_maximized(term->window->xdg_toplevel);
223 else
224 xdg_toplevel_set_maximized(term->window->xdg_toplevel);
225 return true;
227 case BIND_ACTION_FULLSCREEN:
228 if (term->window->is_fullscreen)
229 xdg_toplevel_unset_fullscreen(term->window->xdg_toplevel);
230 else
231 xdg_toplevel_set_fullscreen(term->window->xdg_toplevel, NULL);
232 return true;
234 case BIND_ACTION_PIPE_SCROLLBACK:
235 if (term->grid == &term->alt)
236 break;
237 /* FALLTHROUGH */
238 case BIND_ACTION_PIPE_VIEW:
239 case BIND_ACTION_PIPE_SELECTED:
240 case BIND_ACTION_PIPE_COMMAND_OUTPUT: {
241 if (binding->aux->type != BINDING_AUX_PIPE)
242 return true;
244 struct pipe_context *ctx = NULL;
246 int pipe_fd[2] = {-1, -1};
247 int stdout_fd = -1;
248 int stderr_fd = -1;
250 char *text = NULL;
251 size_t len = 0;
253 if (pipe(pipe_fd) < 0) {
254 LOG_ERRNO("failed to create pipe");
255 goto pipe_err;
256 }
258 stdout_fd = open("/dev/null", O_WRONLY);
259 stderr_fd = open("/dev/null", O_WRONLY);
261 if (stdout_fd < 0 || stderr_fd < 0) {
262 LOG_ERRNO("failed to open /dev/null");
263 goto pipe_err;
264 }
266 bool success;
267 switch (action) {
268 case BIND_ACTION_PIPE_SCROLLBACK:
269 success = term_scrollback_to_text(term, &text, &len);
270 break;
272 case BIND_ACTION_PIPE_VIEW:
273 success = term_view_to_text(term, &text, &len);
274 break;
276 case BIND_ACTION_PIPE_SELECTED:
277 text = selection_to_text(term);
278 success = text != NULL;
279 len = text != NULL ? strlen(text) : 0;
280 break;
282 case BIND_ACTION_PIPE_COMMAND_OUTPUT:
283 success = term_command_output_to_text(term, &text, &len);
284 break;
286 default:
287 BUG("Unhandled action type");
288 success = false;
289 break;
290 }
292 if (!success)
293 goto pipe_err;
295 /* Make write-end non-blocking; required by the FDM */
296 {
297 int flags = fcntl(pipe_fd[1], F_GETFL);
298 if (flags < 0 ||
299 fcntl(pipe_fd[1], F_SETFL, flags | O_NONBLOCK) < 0)
300 {
301 LOG_ERRNO("failed to make write-end of pipe non-blocking");
302 goto pipe_err;
303 }
304 }
306 /* Make sure write-end is closed on exec() - or the spawned
307 * program may not terminate*/
308 {
309 int flags = fcntl(pipe_fd[1], F_GETFD);
310 if (flags < 0 ||
311 fcntl(pipe_fd[1], F_SETFD, flags | FD_CLOEXEC) < 0)
312 {
313 LOG_ERRNO("failed to set FD_CLOEXEC on writeend of pipe");
314 goto pipe_err;
315 }
316 }
318 if (spawn(term->reaper, term->cwd, binding->aux->pipe.args,
319 pipe_fd[0], stdout_fd, stderr_fd, NULL, NULL, NULL) < 0)
320 goto pipe_err;
322 /* Close read end */
323 close(pipe_fd[0]);
325 ctx = xmalloc(sizeof(*ctx));
326 *ctx = (struct pipe_context){
327 .text = text,
328 .left = len,
329 };
331 /* Asynchronously write the output to the pipe */
332 if (!fdm_add(term->fdm, pipe_fd[1], EPOLLOUT, &fdm_write_pipe, ctx))
333 goto pipe_err;
335 return true;
337 pipe_err:
338 if (stdout_fd >= 0)
339 close(stdout_fd);
340 if (stderr_fd >= 0)
341 close(stderr_fd);
342 if (pipe_fd[0] >= 0)
343 close(pipe_fd[0]);
344 if (pipe_fd[1] >= 0)
345 close(pipe_fd[1]);
346 free(text);
347 free(ctx);
348 return true;
349 }
351 case BIND_ACTION_SHOW_URLS_COPY:
352 case BIND_ACTION_SHOW_URLS_LAUNCH:
353 case BIND_ACTION_SHOW_URLS_PERSISTENT: {
354 xassert(!urls_mode_is_active(term));
356 enum url_action url_action =
357 action == BIND_ACTION_SHOW_URLS_COPY ? URL_ACTION_COPY :
358 action == BIND_ACTION_SHOW_URLS_LAUNCH ? URL_ACTION_LAUNCH :
359 URL_ACTION_PERSISTENT;
361 urls_collect(term, url_action, &term->conf->url.preg, true, &term->url.list);
362 urls_assign_key_combos(term->conf, &term->url.list);
363 urls_render(term, &term->conf->url.launch);
364 return true;
365 }
367 case BIND_ACTION_TEXT_BINDING:
368 xassert(binding->aux->type == BINDING_AUX_TEXT);
369 term_to_slave(term, binding->aux->text.data, binding->aux->text.len);
370 return true;
372 case BIND_ACTION_PROMPT_PREV: {
373 if (term->grid != &term->normal)
374 return false;
376 struct grid *grid = term->grid;
377 const int sb_start =
378 grid_sb_start_ignore_uninitialized(grid, term->rows);
380 /* Check each row from current view-1 (that is, the first
381 * currently not visible row), up to, and including, the
382 * scrollback start */
383 for (int r_sb_rel =
384 grid_row_abs_to_sb_precalc_sb_start(
385 grid, sb_start, grid->view) - 1;
386 r_sb_rel >= 0; r_sb_rel--)
387 {
388 const int r_abs =
389 grid_row_sb_to_abs_precalc_sb_start(grid, sb_start, r_sb_rel);
391 const struct row *row = grid->rows[r_abs];
392 xassert(row != NULL);
394 if (!row->shell_integration.prompt_marker)
395 continue;
397 grid->view = r_abs;
398 term_damage_view(term);
399 render_refresh(term);
400 break;
401 }
403 return true;
404 }
406 case BIND_ACTION_PROMPT_NEXT: {
407 if (term->grid != &term->normal)
408 return false;
410 struct grid *grid = term->grid;
411 const int num_rows = grid->num_rows;
413 if (grid->view == grid->offset) {
414 /* Already at the bottom */
415 return true;
416 }
418 /* Check each row from view+1, to the bottom of the scrollback */
419 for (int r_abs = (grid->view + 1) & (num_rows - 1);
420 ;
421 r_abs = (r_abs + 1) & (num_rows - 1))
422 {
423 const struct row *row = grid->rows[r_abs];
424 xassert(row != NULL);
426 if (!row->shell_integration.prompt_marker) {
427 if (r_abs == grid->offset + term->rows - 1) {
428 /* We've reached the bottom of the scrollback */
429 break;
430 }
431 continue;
432 }
434 int sb_start = grid_sb_start_ignore_uninitialized(grid, term->rows);
435 int ofs_sb_rel =
436 grid_row_abs_to_sb_precalc_sb_start(grid, sb_start, grid->offset);
437 int new_view_sb_rel =
438 grid_row_abs_to_sb_precalc_sb_start(grid, sb_start, r_abs);
440 new_view_sb_rel = min(ofs_sb_rel, new_view_sb_rel);
441 grid->view = grid_row_sb_to_abs_precalc_sb_start(
442 grid, sb_start, new_view_sb_rel);
444 term_damage_view(term);
445 render_refresh(term);
446 break;
447 }
449 return true;
450 }
452 case BIND_ACTION_UNICODE_INPUT:
453 unicode_mode_activate(term);
454 return true;
456 case BIND_ACTION_QUIT:
457 term_shutdown(term);
458 return true;
460 case BIND_ACTION_REGEX_LAUNCH:
461 case BIND_ACTION_REGEX_COPY:
462 if (binding->aux->type != BINDING_AUX_REGEX)
463 return true;
465 tll_foreach(term->conf->custom_regexes, it) {
466 const struct custom_regex *regex = &it->item;
468 if (streq(regex->name, binding->aux->regex_name)) {
469 xassert(!urls_mode_is_active(term));
471 enum url_action url_action = action == BIND_ACTION_REGEX_LAUNCH
472 ? URL_ACTION_LAUNCH : URL_ACTION_COPY;
474 if (regex->regex == NULL) {
475 LOG_ERR("regex:%s has no regex defined", regex->name);
476 return true;
477 }
478 if (url_action == URL_ACTION_LAUNCH && regex->launch.argv.args == NULL) {
479 LOG_ERR("regex:%s has no launch command defined", regex->name);
480 return true;
481 }
483 urls_collect(term, url_action, &regex->preg, false, &term->url.list);
484 urls_assign_key_combos(term->conf, &term->url.list);
485 urls_render(term, &regex->launch);
486 return true;
487 }
488 }
490 LOG_ERR(
491 "no regex section named '%s' defined in the configuration",
492 binding->aux->regex_name);
494 return true;
496 case BIND_ACTION_THEME_SWITCH_DARK:
497 term_theme_switch_to_dark(term);
498 return true;
500 case BIND_ACTION_THEME_SWITCH_LIGHT:
501 term_theme_switch_to_light(term);
502 return true;
504 case BIND_ACTION_THEME_TOGGLE:
505 term_theme_toggle(term);
506 return true;
508 case BIND_ACTION_SELECT_BEGIN:
509 selection_start(
510 term, seat->mouse.col, seat->mouse.row, SELECTION_CHAR_WISE, false);
511 return true;
513 case BIND_ACTION_SELECT_BEGIN_BLOCK:
514 selection_start(
515 term, seat->mouse.col, seat->mouse.row, SELECTION_BLOCK, false);
516 return true;
518 case BIND_ACTION_SELECT_EXTEND:
519 selection_extend(
520 seat, term, seat->mouse.col, seat->mouse.row, term->selection.kind);
521 return true;
523 case BIND_ACTION_SELECT_EXTEND_CHAR_WISE:
524 if (term->selection.kind != SELECTION_BLOCK) {
525 selection_extend(
526 seat, term, seat->mouse.col, seat->mouse.row, SELECTION_CHAR_WISE);
527 return true;
528 }
529 return false;
531 case BIND_ACTION_SELECT_WORD:
532 selection_start(
533 term, seat->mouse.col, seat->mouse.row, SELECTION_WORD_WISE, false);
534 return true;
536 case BIND_ACTION_SELECT_WORD_WS:
537 selection_start(
538 term, seat->mouse.col, seat->mouse.row, SELECTION_WORD_WISE, true);
539 return true;
541 case BIND_ACTION_SELECT_QUOTE:
542 selection_start(
543 term, seat->mouse.col, seat->mouse.row, SELECTION_QUOTE_WISE, false);
544 return true;
546 case BIND_ACTION_SELECT_ROW:
547 selection_start(
548 term, seat->mouse.col, seat->mouse.row, SELECTION_LINE_WISE, false);
549 return true;
551 case BIND_ACTION_COUNT:
552 BUG("Invalid action type");
553 return false;
554 }
556 return false;
559static void
560keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,
561 uint32_t format, int32_t fd, uint32_t size)
563 LOG_DBG("keyboard_keymap: keyboard=%p (format=%u, size=%u)",
564 (void *)wl_keyboard, format, size);
566 struct seat *seat = data;
567 struct wayland *wayl = seat->wayl;
569 /*
570 * Free old keymap state
571 */
573 if (seat->kbd.xkb_keymap != NULL) {
574 xkb_keymap_unref(seat->kbd.xkb_keymap);
575 seat->kbd.xkb_keymap = NULL;
576 }
577 if (seat->kbd.xkb_state != NULL) {
578 xkb_state_unref(seat->kbd.xkb_state);
579 seat->kbd.xkb_state = NULL;
580 }
582 key_binding_unload_keymap(wayl->key_binding_manager, seat);
584 /* Verify keymap is in a format we understand */
585 switch ((enum wl_keyboard_keymap_format)format) {
586 case WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP:
587 goto err;
589 case WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1:
590 break;
592 default:
593 LOG_WARN("unrecognized keymap format: %u", format);
594 goto err;
595 }
597 char *map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
598 if (map_str == MAP_FAILED) {
599 LOG_ERRNO("failed to mmap keyboard keymap");
600 goto err;
601 }
603 if (seat->kbd.xkb != NULL) {
604 seat->kbd.xkb_keymap = xkb_keymap_new_from_buffer(
605 seat->kbd.xkb, map_str, size, XKB_KEYMAP_FORMAT_TEXT_V1,
606 XKB_KEYMAP_COMPILE_NO_FLAGS);
608 }
610 munmap(map_str, size);
612 if (seat->kbd.xkb_keymap != NULL) {
613 seat->kbd.xkb_state = xkb_state_new(seat->kbd.xkb_keymap);
615 seat->kbd.mod_shift = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_SHIFT);
616 seat->kbd.mod_alt = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_ALT) ;
617 seat->kbd.mod_ctrl = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_CTRL);
618 seat->kbd.mod_super = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_LOGO);
619 seat->kbd.mod_caps = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_CAPS);
620 seat->kbd.mod_num = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_MOD_NAME_NUM);
622 /* Significant modifiers in the legacy keyboard protocol */
623 seat->kbd.legacy_significant = 0;
624 if (seat->kbd.mod_shift != XKB_MOD_INVALID)
625 seat->kbd.legacy_significant |= 1 << seat->kbd.mod_shift;
626 if (seat->kbd.mod_alt != XKB_MOD_INVALID)
627 seat->kbd.legacy_significant |= 1 << seat->kbd.mod_alt;
628 if (seat->kbd.mod_ctrl != XKB_MOD_INVALID)
629 seat->kbd.legacy_significant |= 1 << seat->kbd.mod_ctrl;
630 if (seat->kbd.mod_super != XKB_MOD_INVALID)
631 seat->kbd.legacy_significant |= 1 << seat->kbd.mod_super;
633 /* Significant modifiers in the kitty keyboard protocol */
634 seat->kbd.kitty_significant = seat->kbd.legacy_significant;
635 if (seat->kbd.mod_caps != XKB_MOD_INVALID)
636 seat->kbd.kitty_significant |= 1 << seat->kbd.mod_caps;
637 if (seat->kbd.mod_num != XKB_MOD_INVALID)
638 seat->kbd.kitty_significant |= 1 << seat->kbd.mod_num;
641 /*
642 * Create a mask of all "virtual" modifiers. Some compositors
643 * add these *in addition* to the "real" modifiers (Mod1,
644 * Mod2, etc).
645 *
646 * Since our modifier logic (both for internal shortcut
647 * processing, and e.g. the kitty keyboard protocol) makes
648 * very few assumptions on available modifiers, which keys map
649 * to which modifier etc, the presence of virtual modifiers
650 * causes various things to break.
651 *
652 * For example, if a foot shortcut is Mod1+b (i.e. Alt+b), it
653 * won't match if the compositor _also_ sets the Alt modifier
654 * (the corresponding shortcut in foot would be Alt+Mod1+b).
655 *
656 * See https://codeberg.org/dnkl/foot/issues/2009
657 *
658 * Mutter (GNOME) is known to set the virtual modifiers in
659 * addtiion to the real modifiers.
660 *
661 * As far as I know, there's no compositor that _only_ sets
662 * virtual modifiers (don't think that's even legal...?)
663 */
664 {
665 xkb_mod_index_t alt = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_VMOD_NAME_ALT);
666 xkb_mod_index_t meta = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_VMOD_NAME_META);
667 xkb_mod_index_t super = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_VMOD_NAME_SUPER);
668 xkb_mod_index_t hyper = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_VMOD_NAME_HYPER);
669 xkb_mod_index_t num_lock = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_VMOD_NAME_NUM);
670 xkb_mod_index_t scroll_lock = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_VMOD_NAME_SCROLL);
671 xkb_mod_index_t level_three = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_VMOD_NAME_LEVEL3);
672 xkb_mod_index_t level_five = xkb_keymap_mod_get_index(seat->kbd.xkb_keymap, XKB_VMOD_NAME_LEVEL5);
674 xkb_mod_index_t ignore = 0;
676 if (alt != XKB_MOD_INVALID) ignore |= 1 << alt;
677 if (meta != XKB_MOD_INVALID) ignore |= 1 << meta;
678 if (super != XKB_MOD_INVALID) ignore |= 1 << super;
679 if (hyper != XKB_MOD_INVALID) ignore |= 1 << hyper;
680 if (num_lock != XKB_MOD_INVALID) ignore |= 1 << num_lock;
681 if (scroll_lock != XKB_MOD_INVALID) ignore |= 1 << scroll_lock;
682 if (level_three != XKB_MOD_INVALID) ignore |= 1 << level_three;
683 if (level_five != XKB_MOD_INVALID) ignore |= 1 << level_five;
685 seat->kbd.virtual_modifiers = ignore;
686 }
688 seat->kbd.key_arrow_up = xkb_keymap_key_by_name(seat->kbd.xkb_keymap, "UP");
689 seat->kbd.key_arrow_down = xkb_keymap_key_by_name(seat->kbd.xkb_keymap, "DOWN");
690 }
692 key_binding_load_keymap(wayl->key_binding_manager, seat);
694err:
695 close(fd);
698static void
699keyboard_enter(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,
700 struct wl_surface *surface, struct wl_array *keys)
702 xassert(serial != 0);
704 if (unlikely(surface == NULL)) {
705 /* Seen on Hyprland */
706 LOG_WARN("compositor sent keyboard_enter event with a NULL surface");
707 return;
708 }
710 struct seat *seat = data;
711 struct wl_window *win = wl_surface_get_user_data(surface);
712 struct terminal *term = win->term;
714 LOG_DBG("%s: keyboard_enter: keyboard=%p, serial=%u, surface=%p",
715 seat->name, (void *)wl_keyboard, serial, (void *)surface);
717 term_kbd_focus_in(term);
718 seat->kbd_focus = term;
719 seat->kbd.serial = serial;
722static bool
723start_repeater(struct seat *seat, uint32_t key)
725 if (seat->kbd.repeat.dont_re_repeat)
726 return true;
728 if (seat->kbd.repeat.rate == 0)
729 return true;
731 struct itimerspec t = {
732 .it_value = {.tv_sec = 0, .tv_nsec = seat->kbd.repeat.delay * 1000000},
733 .it_interval = {.tv_sec = 0, .tv_nsec = 1000000000 / seat->kbd.repeat.rate},
734 };
736 if (t.it_value.tv_nsec >= 1000000000) {
737 t.it_value.tv_sec += t.it_value.tv_nsec / 1000000000;
738 t.it_value.tv_nsec %= 1000000000;
739 }
740 if (t.it_interval.tv_nsec >= 1000000000) {
741 t.it_interval.tv_sec += t.it_interval.tv_nsec / 1000000000;
742 t.it_interval.tv_nsec %= 1000000000;
743 }
744 if (timerfd_settime(seat->kbd.repeat.fd, 0, &t, NULL) < 0) {
745 LOG_ERRNO("%s: failed to arm keyboard repeat timer", seat->name);
746 return false;
747 }
749 seat->kbd.repeat.key = key;
750 return true;
753static bool
754stop_repeater(struct seat *seat, uint32_t key)
756 if (key != -1 && key != seat->kbd.repeat.key)
757 return true;
759 if (timerfd_settime(seat->kbd.repeat.fd, 0, &(struct itimerspec){{0}}, NULL) < 0) {
760 LOG_ERRNO("%s: failed to disarm keyboard repeat timer", seat->name);
761 return false;
762 }
764 return true;
767static void
768keyboard_leave(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,
769 struct wl_surface *surface)
771 struct seat *seat = data;
773 LOG_DBG("keyboard_leave: keyboard=%p, serial=%u, surface=%p",
774 (void *)wl_keyboard, serial, (void *)surface);
776 xassert(
777 seat->kbd_focus == NULL ||
778 surface == NULL || /* Seen on Sway 1.2 */
779 ((const struct wl_window *)wl_surface_get_user_data(surface))->term == seat->kbd_focus
780 );
782 struct terminal *old_focused = seat->kbd_focus;
783 seat->kbd_focus = NULL;
785 stop_repeater(seat, -1);
786 seat->kbd.shift = false;
787 seat->kbd.alt = false;
788 seat->kbd.ctrl = false;
789 seat->kbd.super = false;
791 if (seat->kbd.xkb_compose_state != NULL)
792 xkb_compose_state_reset(seat->kbd.xkb_compose_state);
794 if (seat->kbd.xkb_state != NULL && seat->kbd.xkb_keymap != NULL) {
795 const xkb_layout_index_t layout_count = xkb_keymap_num_layouts(seat->kbd.xkb_keymap);
797 for (xkb_layout_index_t i = 0; i < layout_count; i++)
798 xkb_state_update_mask(seat->kbd.xkb_state, 0, 0, 0, i, i, i);
799 }
801 if (old_focused != NULL) {
802 seat->pointer.hidden = false;
803 term_xcursor_update_for_seat(old_focused, seat);
804 term_kbd_focus_out(old_focused);
805 } else {
806 /*
807 * Sway bug - under certain conditions we get a
808 * keyboard_leave() (and keyboard_key()) without first having
809 * received a keyboard_enter()
810 */
811 LOG_WARN(
812 "compositor sent keyboard_leave event without a keyboard_enter "
813 "event: surface=%p", (void *)surface);
814 }
817static const struct key_data *
818keymap_data_for_sym(xkb_keysym_t sym, size_t *count)
820 switch (sym) {
821 case XKB_KEY_Escape: *count = ALEN(key_escape); return key_escape;
822 case XKB_KEY_Return: *count = ALEN(key_return); return key_return;
823 case XKB_KEY_ISO_Left_Tab: *count = ALEN(key_iso_left_tab); return key_iso_left_tab;
824 case XKB_KEY_Tab: *count = ALEN(key_tab); return key_tab;
825 case XKB_KEY_BackSpace: *count = ALEN(key_backspace); return key_backspace;
826 case XKB_KEY_Up: *count = ALEN(key_up); return key_up;
827 case XKB_KEY_Down: *count = ALEN(key_down); return key_down;
828 case XKB_KEY_Right: *count = ALEN(key_right); return key_right;
829 case XKB_KEY_Left: *count = ALEN(key_left); return key_left;
830 case XKB_KEY_Home: *count = ALEN(key_home); return key_home;
831 case XKB_KEY_End: *count = ALEN(key_end); return key_end;
832 case XKB_KEY_Insert: *count = ALEN(key_insert); return key_insert;
833 case XKB_KEY_Delete: *count = ALEN(key_delete); return key_delete;
834 case XKB_KEY_Page_Up: *count = ALEN(key_pageup); return key_pageup;
835 case XKB_KEY_Page_Down: *count = ALEN(key_pagedown); return key_pagedown;
836 case XKB_KEY_F1: *count = ALEN(key_f1); return key_f1;
837 case XKB_KEY_F2: *count = ALEN(key_f2); return key_f2;
838 case XKB_KEY_F3: *count = ALEN(key_f3); return key_f3;
839 case XKB_KEY_F4: *count = ALEN(key_f4); return key_f4;
840 case XKB_KEY_F5: *count = ALEN(key_f5); return key_f5;
841 case XKB_KEY_F6: *count = ALEN(key_f6); return key_f6;
842 case XKB_KEY_F7: *count = ALEN(key_f7); return key_f7;
843 case XKB_KEY_F8: *count = ALEN(key_f8); return key_f8;
844 case XKB_KEY_F9: *count = ALEN(key_f9); return key_f9;
845 case XKB_KEY_F10: *count = ALEN(key_f10); return key_f10;
846 case XKB_KEY_F11: *count = ALEN(key_f11); return key_f11;
847 case XKB_KEY_F12: *count = ALEN(key_f12); return key_f12;
848 case XKB_KEY_F13: *count = ALEN(key_f13); return key_f13;
849 case XKB_KEY_F14: *count = ALEN(key_f14); return key_f14;
850 case XKB_KEY_F15: *count = ALEN(key_f15); return key_f15;
851 case XKB_KEY_F16: *count = ALEN(key_f16); return key_f16;
852 case XKB_KEY_F17: *count = ALEN(key_f17); return key_f17;
853 case XKB_KEY_F18: *count = ALEN(key_f18); return key_f18;
854 case XKB_KEY_F19: *count = ALEN(key_f19); return key_f19;
855 case XKB_KEY_F20: *count = ALEN(key_f20); return key_f20;
856 case XKB_KEY_F21: *count = ALEN(key_f21); return key_f21;
857 case XKB_KEY_F22: *count = ALEN(key_f22); return key_f22;
858 case XKB_KEY_F23: *count = ALEN(key_f23); return key_f23;
859 case XKB_KEY_F24: *count = ALEN(key_f24); return key_f24;
860 case XKB_KEY_F25: *count = ALEN(key_f25); return key_f25;
861 case XKB_KEY_F26: *count = ALEN(key_f26); return key_f26;
862 case XKB_KEY_F27: *count = ALEN(key_f27); return key_f27;
863 case XKB_KEY_F28: *count = ALEN(key_f28); return key_f28;
864 case XKB_KEY_F29: *count = ALEN(key_f29); return key_f29;
865 case XKB_KEY_F30: *count = ALEN(key_f30); return key_f30;
866 case XKB_KEY_F31: *count = ALEN(key_f31); return key_f31;
867 case XKB_KEY_F32: *count = ALEN(key_f32); return key_f32;
868 case XKB_KEY_F33: *count = ALEN(key_f33); return key_f33;
869 case XKB_KEY_F34: *count = ALEN(key_f34); return key_f34;
870 case XKB_KEY_F35: *count = ALEN(key_f35); return key_f35;
871 case XKB_KEY_KP_Up: *count = ALEN(key_kp_up); return key_kp_up;
872 case XKB_KEY_KP_Down: *count = ALEN(key_kp_down); return key_kp_down;
873 case XKB_KEY_KP_Right: *count = ALEN(key_kp_right); return key_kp_right;
874 case XKB_KEY_KP_Left: *count = ALEN(key_kp_left); return key_kp_left;
875 case XKB_KEY_KP_Begin: *count = ALEN(key_kp_begin); return key_kp_begin;
876 case XKB_KEY_KP_Home: *count = ALEN(key_kp_home); return key_kp_home;
877 case XKB_KEY_KP_End: *count = ALEN(key_kp_end); return key_kp_end;
878 case XKB_KEY_KP_Insert: *count = ALEN(key_kp_insert); return key_kp_insert;
879 case XKB_KEY_KP_Delete: *count = ALEN(key_kp_delete); return key_kp_delete;
880 case XKB_KEY_KP_Page_Up: *count = ALEN(key_kp_pageup); return key_kp_pageup;
881 case XKB_KEY_KP_Page_Down: *count = ALEN(key_kp_pagedown); return key_kp_pagedown;
882 case XKB_KEY_KP_Enter: *count = ALEN(key_kp_enter); return key_kp_enter;
883 case XKB_KEY_KP_Divide: *count = ALEN(key_kp_divide); return key_kp_divide;
884 case XKB_KEY_KP_Multiply: *count = ALEN(key_kp_multiply); return key_kp_multiply;
885 case XKB_KEY_KP_Subtract: *count = ALEN(key_kp_subtract); return key_kp_subtract;
886 case XKB_KEY_KP_Add: *count = ALEN(key_kp_add); return key_kp_add;
887 case XKB_KEY_KP_Separator: *count = ALEN(key_kp_separator); return key_kp_separator;
888 case XKB_KEY_KP_Decimal: *count = ALEN(key_kp_decimal); return key_kp_decimal;
889 case XKB_KEY_KP_0: *count = ALEN(key_kp_0); return key_kp_0;
890 case XKB_KEY_KP_1: *count = ALEN(key_kp_1); return key_kp_1;
891 case XKB_KEY_KP_2: *count = ALEN(key_kp_2); return key_kp_2;
892 case XKB_KEY_KP_3: *count = ALEN(key_kp_3); return key_kp_3;
893 case XKB_KEY_KP_4: *count = ALEN(key_kp_4); return key_kp_4;
894 case XKB_KEY_KP_5: *count = ALEN(key_kp_5); return key_kp_5;
895 case XKB_KEY_KP_6: *count = ALEN(key_kp_6); return key_kp_6;
896 case XKB_KEY_KP_7: *count = ALEN(key_kp_7); return key_kp_7;
897 case XKB_KEY_KP_8: *count = ALEN(key_kp_8); return key_kp_8;
898 case XKB_KEY_KP_9: *count = ALEN(key_kp_9); return key_kp_9;
899 }
901 return NULL;
904static const struct key_data *
905keymap_lookup(struct terminal *term, xkb_keysym_t sym, enum modifier mods)
907 size_t count;
908 const struct key_data *info = keymap_data_for_sym(sym, &count);
910 if (info == NULL)
911 return NULL;
913 const enum cursor_keys cursor_keys_mode = term->cursor_keys_mode;
914 const enum keypad_keys keypad_keys_mode
915 = term->num_lock_modifier ? KEYPAD_NUMERICAL : term->keypad_keys_mode;
917 LOG_DBG("keypad mode: %d", keypad_keys_mode);
919 for (size_t j = 0; j < count; j++) {
920 enum modifier modifiers = info[j].modifiers;
922 if (modifiers & MOD_MODIFY_OTHER_KEYS_STATE1) {
923 if (term->modify_other_keys_2)
924 continue;
925 modifiers &= ~MOD_MODIFY_OTHER_KEYS_STATE1;
926 }
927 if (modifiers & MOD_MODIFY_OTHER_KEYS_STATE2) {
928 if (!term->modify_other_keys_2)
929 continue;
930 modifiers &= ~MOD_MODIFY_OTHER_KEYS_STATE2;
931 }
933 if (modifiers != MOD_ANY && modifiers != mods)
934 continue;
936 if (info[j].cursor_keys_mode != CURSOR_KEYS_DONTCARE &&
937 info[j].cursor_keys_mode != cursor_keys_mode)
938 continue;
940 if (info[j].keypad_keys_mode != KEYPAD_DONTCARE &&
941 info[j].keypad_keys_mode != keypad_keys_mode)
942 continue;
944 return &info[j];
945 }
947 return NULL;
950UNITTEST
952 struct terminal term = {
953 .num_lock_modifier = false,
954 .keypad_keys_mode = KEYPAD_NUMERICAL,
955 .cursor_keys_mode = CURSOR_KEYS_NORMAL,
956 };
958 const struct key_data *info = keymap_lookup(&term, XKB_KEY_ISO_Left_Tab, MOD_SHIFT | MOD_CTRL);
959 xassert(info != NULL);
960 xassert(streq(info->seq, "\033[27;6;9~"));
963UNITTEST
965 struct terminal term = {
966 .modify_other_keys_2 = false,
967 };
969 const struct key_data *info = keymap_lookup(&term, XKB_KEY_Return, MOD_ALT);
970 xassert(info != NULL);
971 xassert(streq(info->seq, "\033\r"));
973 term.modify_other_keys_2 = true;
974 info = keymap_lookup(&term, XKB_KEY_Return, MOD_ALT);
975 xassert(info != NULL);
976 xassert(streq(info->seq, "\033[27;3;13~"));
979void
980get_current_modifiers(const struct seat *seat,
981 xkb_mod_mask_t *effective,
982 xkb_mod_mask_t *consumed, uint32_t key,
983 bool filter_locked)
985 if (unlikely(seat->kbd.xkb_state == NULL)) {
986 if (effective != NULL)
987 *effective = 0;
988 if (consumed != NULL)
989 *consumed = 0;
990 }
992 else {
993 const xkb_mod_mask_t locked =
994 xkb_state_serialize_mods(seat->kbd.xkb_state, XKB_STATE_MODS_LOCKED);
996 if (effective != NULL) {
997 *effective = xkb_state_serialize_mods(
998 seat->kbd.xkb_state, XKB_STATE_MODS_EFFECTIVE);
1000 if (filter_locked)
1001 *effective &= ~locked;
1004 if (consumed != NULL) {
1005 *consumed = xkb_state_key_get_consumed_mods2(
1006 seat->kbd.xkb_state, key, XKB_CONSUMED_MODE_XKB);
1008 if (filter_locked)
1009 *consumed &= ~locked;
1014struct kbd_ctx {
1015 xkb_layout_index_t layout;
1016 xkb_keycode_t key;
1017 xkb_keysym_t sym;
1019 struct {
1020 const xkb_keysym_t *syms;
1021 size_t count;
1022 } level0_syms;
1024 xkb_mod_mask_t mods;
1025 xkb_mod_mask_t consumed;
1027 struct {
1028 const uint8_t *buf;
1029 size_t count;
1030 } utf8;
1031 uint32_t *utf32;
1033 enum xkb_compose_status compose_status;
1034 enum wl_keyboard_key_state key_state;
1037static bool
1038legacy_kbd_protocol(struct seat *seat, struct terminal *term,
1039 const struct kbd_ctx *ctx)
1041 if (ctx->key_state != WL_KEYBOARD_KEY_STATE_PRESSED)
1042 return false;
1043 if (ctx->compose_status == XKB_COMPOSE_COMPOSING)
1044 return false;
1046 enum modifier keymap_mods = MOD_NONE;
1047 keymap_mods |= seat->kbd.shift ? MOD_SHIFT : MOD_NONE;
1048 keymap_mods |= seat->kbd.alt ? MOD_ALT : MOD_NONE;
1049 keymap_mods |= seat->kbd.ctrl ? MOD_CTRL : MOD_NONE;
1050 keymap_mods |= seat->kbd.super ? MOD_META : MOD_NONE;
1052 const xkb_keysym_t sym = ctx->sym;
1053 const size_t count = ctx->utf8.count;
1054 const uint8_t *const utf8 = ctx->utf8.buf;
1056 const struct key_data *keymap = keymap_lookup(term, sym, keymap_mods);
1057 if (keymap != NULL) {
1058 term_to_slave(term, keymap->seq, strlen(keymap->seq));
1059 return true;
1062 if (count == 0)
1063 return false;
1065#define is_control_key(x) ((x) >= 0x40 && (x) <= 0x7f)
1066#define IS_CTRL(x) ((x) < 0x20 || ((x) >= 0x7f && (x) <= 0x9f))
1068 //LOG_DBG("term->modify_other_keys=%d, count=%zu, is_ctrl=%d (utf8=0x%02x), sym=%d",
1069 //term->modify_other_keys_2, count, IS_CTRL(utf8[0]), utf8[0], sym);
1071 bool ctrl_is_in_effect = (keymap_mods & MOD_CTRL) != 0;
1072 bool ctrl_seq = is_control_key(sym) || (count == 1 && IS_CTRL(utf8[0]));
1074 bool modify_other_keys2_in_effect = false;
1076 if (term->modify_other_keys_2) {
1077 /*
1078 * Try to mimic XTerm's behavior, when holding shift:
1080 * - if other modifiers are pressed (e.g. Alt), emit a CSI escape
1081 * - upper-case symbols A-Z are encoded as an CSI escape
1082 * - other upper-case symbols (e.g 'Ö') or emitted as is
1083 * - non-upper cased symbols are _mostly_ emitted as is (foot
1084 * always emits as is)
1086 * Examples (assuming Swedish layout):
1087 * - Shift-a ('A') emits a CSI
1088 * - Shift-, (';') emits ';'
1089 * - Shift-Alt-, (Alt-;) emits a CSI
1090 * - Shift-ö ('Ö') emits 'Ö'
1091 */
1093 /* Any modifiers, besides shift active? */
1094 const xkb_mod_mask_t shift_mask = 1 << seat->kbd.mod_shift;
1095 if ((ctx->mods & ~shift_mask & seat->kbd.legacy_significant) != 0)
1096 modify_other_keys2_in_effect = true;
1098 else {
1099 const xkb_layout_index_t layout_idx = xkb_state_key_get_layout(
1100 seat->kbd.xkb_state, ctx->key);
1102 /*
1103 * Get pressed key's base symbol.
1104 * - for 'A' (shift-a), that's 'a'
1105 * - for ';' (shift-,), that's ','
1106 */
1107 const xkb_keysym_t *base_syms = NULL;
1108 size_t base_count = xkb_keymap_key_get_syms_by_level(
1109 seat->kbd.xkb_keymap, ctx->key, layout_idx, 0, &base_syms);
1111 /* Check if base symbol(s) is a-z. If so, emit CSI */
1112 const xkb_keysym_t lower_cased_sym = xkb_keysym_to_lower(ctx->sym);
1113 for (size_t i = 0; i < base_count; i++) {
1114 const xkb_keysym_t s = base_syms[i];
1115 if (lower_cased_sym == s && s >= XKB_KEY_a && s <= XKB_KEY_z) {
1116 modify_other_keys2_in_effect = true;
1117 break;
1123 if (keymap_mods != MOD_NONE && (modify_other_keys2_in_effect ||
1124 (ctrl_is_in_effect && !ctrl_seq)))
1126 static const int mod_param_map[32] = {
1127 [MOD_SHIFT] = 2,
1128 [MOD_ALT] = 3,
1129 [MOD_SHIFT | MOD_ALT] = 4,
1130 [MOD_CTRL] = 5,
1131 [MOD_SHIFT | MOD_CTRL] = 6,
1132 [MOD_ALT | MOD_CTRL] = 7,
1133 [MOD_SHIFT | MOD_ALT | MOD_CTRL] = 8,
1134 [MOD_META] = 9,
1135 [MOD_META | MOD_SHIFT] = 10,
1136 [MOD_META | MOD_ALT] = 11,
1137 [MOD_META | MOD_SHIFT | MOD_ALT] = 12,
1138 [MOD_META | MOD_CTRL] = 13,
1139 [MOD_META | MOD_SHIFT | MOD_CTRL] = 14,
1140 [MOD_META | MOD_ALT | MOD_CTRL] = 15,
1141 [MOD_META | MOD_SHIFT | MOD_ALT | MOD_CTRL] = 16,
1142 };
1144 xassert(keymap_mods < ALEN(mod_param_map));
1145 int modify_param = mod_param_map[keymap_mods];
1146 xassert(modify_param != 0);
1148 char reply[32];
1149 size_t n = xsnprintf(reply, sizeof(reply), "\x1b[27;%d;%d~", modify_param, sym);
1150 term_to_slave(term, reply, n);
1153 else if (keymap_mods & MOD_ALT) {
1154 /*
1155 * When the alt modifier is pressed, we do one out of three things:
1157 * 1. we prefix the output bytes with ESC
1158 * 2. we set the 8:th bit in the output byte
1159 * 3. we ignore the alt modifier
1161 * #1 is configured with \E[?1036, and is on by default
1163 * If #1 has been disabled, we use #2, *if* it's a single byte
1164 * we're emitting. Since this is a UTF-8 terminal, we then
1165 * UTF8-encode the 8-bit character. #2 is configured with
1166 * \E[?1034, and is on by default.
1168 * Lastly, if both #1 and #2 have been disabled, the alt
1169 * modifier is ignored.
1170 */
1171 if (term->meta.esc_prefix) {
1172 term_to_slave(term, "\x1b", 1);
1173 term_to_slave(term, utf8, count);
1176 else if (term->meta.eight_bit && count == 1) {
1177 const char32_t wc = 0x80 | utf8[0];
1179 char utf8_meta[MB_CUR_MAX];
1180 size_t chars = c32rtomb(utf8_meta, wc, &(mbstate_t){0});
1182 if (chars != (size_t)-1)
1183 term_to_slave(term, utf8_meta, chars);
1184 else
1185 term_to_slave(term, utf8, count);
1188 else {
1189 /* Alt ignored */
1190 term_to_slave(term, utf8, count);
1192 } else
1193 term_to_slave(term, utf8, count);
1195 return true;
1198UNITTEST
1200 /* Verify the kitty keymap is sorted */
1201 xkb_keysym_t last = 0;
1202 for (size_t i = 0; i < ALEN(kitty_keymap); i++) {
1203 const struct kitty_key_data *e = &kitty_keymap[i];
1204 xassert(e->sym > last);
1205 last = e->sym;
1209static int
1210kitty_search(const void *_key, const void *_e)
1212 const xkb_keysym_t *key = _key;
1213 const struct kitty_key_data *e = _e;
1214 return *key - e->sym;
1217static bool
1218kitty_kbd_protocol(struct seat *seat, struct terminal *term,
1219 const struct kbd_ctx *ctx)
1221 const bool repeating = seat->kbd.repeat.dont_re_repeat;
1222 const bool pressed = ctx->key_state == WL_KEYBOARD_KEY_STATE_PRESSED && !repeating;
1223 const bool released = ctx->key_state == WL_KEYBOARD_KEY_STATE_RELEASED;
1224 const bool composing = ctx->compose_status == XKB_COMPOSE_COMPOSING;
1225 const bool composed = ctx->compose_status == XKB_COMPOSE_COMPOSED;
1227 const enum kitty_kbd_flags flags =
1228 term->grid->kitty_kbd.flags[term->grid->kitty_kbd.idx];
1230 const bool disambiguate = flags & KITTY_KBD_DISAMBIGUATE;
1231 const bool report_events = flags & KITTY_KBD_REPORT_EVENT;
1232 const bool report_alternate = flags & KITTY_KBD_REPORT_ALTERNATE;
1233 const bool report_all_as_escapes = flags & KITTY_KBD_REPORT_ALL;
1235 if (!report_events && released)
1236 return false;
1238 /* TODO: should we even bother with this, or just say it's not supported? */
1239 if (!disambiguate && !report_all_as_escapes && pressed)
1240 return legacy_kbd_protocol(seat, term, ctx);
1242 const xkb_keysym_t sym = ctx->sym;
1243 const uint32_t *utf32 = ctx->utf32;
1244 const uint8_t *const utf8 = ctx->utf8.buf;
1245 const size_t count = ctx->utf8.count;
1247 /* Lookup sym in the pre-defined keysym table */
1248 const struct kitty_key_data *info = bsearch(
1249 &sym, kitty_keymap, ALEN(kitty_keymap), sizeof(kitty_keymap[0]),
1250 &kitty_search);
1251 xassert(info == NULL || info->sym == sym);
1253 xkb_mod_mask_t mods = 0;
1254 xkb_mod_mask_t locked = 0;
1255 xkb_mod_mask_t consumed = ctx->consumed;
1257 if (info != NULL && info->is_modifier) {
1258 /*
1259 * Special-case modifier keys.
1261 * Normally, the "current" XKB state reflects the state
1262 * *before* the current key event. In other words, the
1263 * modifiers for key events that affect the modifier state
1264 * (e.g. one of the control keys, or shift keys etc) does
1265 * *not* include the key itself.
1267 * Put another way, if you press "control", the modifier set
1268 * is empty in the key press event, but contains "ctrl" in the
1269 * release event.
1271 * The kitty protocol mandates the modifier list contain the
1272 * key itself, in *both* the press and release event.
1274 * We handle this by updating the XKB state to *include* the
1275 * current key, retrieve the set of modifiers (including the
1276 * set of consumed modifiers), and then revert the XKB update.
1277 */
1278 xkb_state_update_key(
1279 seat->kbd.xkb_state, ctx->key, pressed ? XKB_KEY_DOWN : XKB_KEY_UP);
1281 get_current_modifiers(seat, &mods, NULL, 0, false);
1283 locked = xkb_state_serialize_mods(
1284 seat->kbd.xkb_state, XKB_STATE_MODS_LOCKED);
1285 consumed = xkb_state_key_get_consumed_mods2(
1286 seat->kbd.xkb_state, ctx->key, XKB_CONSUMED_MODE_XKB);
1288#if 0
1289 /*
1290 * TODO: according to the XKB docs, state updates should
1291 * always be in pairs: each press should be followed by a
1292 * release. However, doing this just breaks the xkb state.
1294 * *Not* pairing the above press/release with a corresponding
1295 * release/press appears to do exactly what we want.
1296 */
1297 xkb_state_update_key(
1298 seat->kbd.xkb_state, ctx->key, pressed ? XKB_KEY_UP : XKB_KEY_DOWN);
1299#endif
1300 } else {
1301 /* Same as ctx->mods, but *without* filtering locked modifiers */
1302 get_current_modifiers(seat, &mods, NULL, 0, false);
1303 locked = xkb_state_serialize_mods(
1304 seat->kbd.xkb_state, XKB_STATE_MODS_LOCKED);
1307 mods &= seat->kbd.kitty_significant;
1308 consumed &= seat->kbd.kitty_significant;
1310 /*
1311 * A note on locked modifiers; they *are* a part of the protocol,
1312 * and *should* be included in the modifier set reported in the
1313 * key event.
1315 * However, *only* if the key would result in a CSIu *without* the
1316 * locked modifier being enabled
1318 * Translated: if *another* modifier is active, or if
1319 * report-all-keys-as-escapes is enabled, then we include the
1320 * locked modifier in the key event.
1322 * But, if the key event would result in plain text output without
1323 * the locked modifier, then we "ignore" the locked modifier and
1324 * emit plain text anyway.
1325 */
1327 bool is_text = count > 0 && utf32 != NULL && (mods & ~locked & ~consumed) == 0;
1328 for (size_t i = 0; utf32[i] != U'\0'; i++) {
1329 if (!isc32print(utf32[i])) {
1330 is_text = false;
1331 break;
1335 const bool report_associated_text =
1336 (flags & KITTY_KBD_REPORT_ASSOCIATED) && is_text && !released;
1338 if (composing) {
1339 /* We never emit anything while composing, *except* modifiers
1340 * (and only in report-all-keys-as-escape-codes mode) */
1341 if (info != NULL && info->is_modifier)
1342 goto emit_escapes;
1344 return false;
1347 if (report_all_as_escapes)
1348 goto emit_escapes;
1350 if ((mods & ~locked & ~consumed) == 0) {
1351 switch (sym) {
1352 case XKB_KEY_Return:
1353 if (!released)
1354 term_to_slave(term, "\r", 1);
1355 return true;
1357 case XKB_KEY_BackSpace:
1358 if (!released)
1359 term_to_slave(term, "\x7f", 1);
1360 return true;
1362 case XKB_KEY_Tab:
1363 if (!released)
1364 term_to_slave(term, "\t", 1);
1365 return true;
1369 /* Plain-text without modifiers, or commposed text, is emitted as-is */
1370 if (is_text && !released) {
1371 term_to_slave(term, utf8, count);
1372 return true;
1375emit_escapes:
1377 unsigned int encoded_mods = 0;
1378 if (seat->kbd.mod_shift != XKB_MOD_INVALID)
1379 encoded_mods |= mods & (1 << seat->kbd.mod_shift) ? (1 << 0) : 0;
1380 if (seat->kbd.mod_alt != XKB_MOD_INVALID)
1381 encoded_mods |= mods & (1 << seat->kbd.mod_alt) ? (1 << 1) : 0;
1382 if (seat->kbd.mod_ctrl != XKB_MOD_INVALID)
1383 encoded_mods |= mods & (1 << seat->kbd.mod_ctrl) ? (1 << 2) : 0;
1384 if (seat->kbd.mod_super != XKB_MOD_INVALID)
1385 encoded_mods |= mods & (1 << seat->kbd.mod_super) ? (1 << 3) : 0;
1386 if (seat->kbd.mod_caps != XKB_MOD_INVALID)
1387 encoded_mods |= mods & (1 << seat->kbd.mod_caps) ? (1 << 6) : 0;
1388 if (seat->kbd.mod_num != XKB_MOD_INVALID)
1389 encoded_mods |= mods & (1 << seat->kbd.mod_num) ? (1 << 7) : 0;
1390 encoded_mods++;
1392 /*
1393 * Figure out the main, alternate and base key codes.
1395 * The main key is the unshifted version of the generated symbol,
1396 * the alternate key is the shifted version, and base is the
1397 * (unshifted) key assuming the default layout.
1399 * For example, the user presses shift+a, then:
1400 * - unshifted = 'a'
1401 * - shifted = 'A'
1402 * - base = 'a'
1404 * Base will in many cases be the same as the unshifted key, but
1405 * may differ if the active keyboard layout is non-ASCII (examples
1406 * would be russian, or alternative layouts like neo etc).
1408 * The shifted key is what we get from XKB, i.e. the resulting key
1409 * from all active modifiers, plus the pressed key.
1410 */
1411 int unshifted = -1, shifted = -1, base = -1;
1412 char final;
1414 if (info != NULL) {
1415 /* Use code from lookup table (cursor keys, enter, tab etc)*/
1416 if (!info->is_modifier || report_all_as_escapes) {
1417 shifted = info->key;
1418 final = info->final;
1420 } else {
1421 /* Use keysym (typically its Unicode codepoint value) */
1423 if (composed)
1424 shifted = utf32[0]; /* TODO: what if there are multiple codepoints? */
1425 else
1426 shifted = xkb_keysym_to_utf32(sym);
1428 final = 'u';
1431 if (shifted <= 0)
1432 return false;
1434 /* Base layout key. I.e the symbol the pressed key produces in
1435 * the base/default layout (layout idx 0) */
1436 const xkb_keysym_t *base_syms;
1437 int base_sym_count = xkb_keymap_key_get_syms_by_level(
1438 seat->kbd.xkb_keymap, ctx->key, 0, 0, &base_syms);
1440 if (base_sym_count > 0)
1441 base = xkb_keysym_to_utf32(base_syms[0]);
1443 /*
1444 * If the keysym is shifted, use its unshifted codepoint
1445 * instead. In other words, ctrl+a and ctrl+shift+a should both
1446 * use the same value for 'key' (97 - i.a. 'a').
1448 * However, don't do this if a non-significant modifier was used
1449 * to generate the symbol. This is needed since we cannot encode
1450 * non-significant modifiers, and thus the "extra" modifier(s)
1451 * would get lost.
1453 * Example:
1455 * the Swedish layout has '2', QUOTATION MARK ("double quote"),
1456 * '@', and '²' on the same key. '2' is the base symbol.
1458 * Shift+2 results in QUOTATION MARK
1459 * AltGr+2 results in '@'
1460 * AltGr+Shift+2 results in '²'
1462 * The kitty kbd protocol can't encode AltGr. So, if we always
1463 * used the base symbol ('2'), Alt+Shift+2 would result in the
1464 * same escape sequence as AltGr+Alt+Shift+2.
1466 * (yes, this matches what kitty does, as of 0.23.1)
1467 */
1468 const bool use_level0_sym =
1469 (ctx->mods & ~seat->kbd.kitty_significant) == 0 && ctx->level0_syms.count > 0;
1471 unshifted = use_level0_sym ? xkb_keysym_to_utf32(ctx->level0_syms.syms[0]) : 0;
1473 xassert(encoded_mods >= 1);
1475 char event[4];
1476 if (report_events /*&& !pressed*/) {
1477 /* Note: this deviates slightly from Kitty, which omits the
1478 * ":1" subparameter for key press events */
1479 event[0] = ':';
1480 event[1] = '0' + (pressed ? 1 : repeating ? 2 : 3);
1481 event[2] = '\0';
1482 } else
1483 event[0] = '\0';
1485 char buf[128], *p = buf;
1486 size_t left = sizeof(buf);
1487 size_t bytes;
1489 const int key = unshifted > 0 && isc32print(unshifted) && !composed ? unshifted : shifted;
1490 const int alternate = shifted;
1492 if (final == 'u' || final == '~') {
1493 bytes = snprintf(p, left, "\x1b[%u", key);
1494 p += bytes; left -= bytes;
1496 if (report_alternate) {
1497 bool emit_alternate = alternate > 0 && alternate != key;
1498 bool emit_base = base > 0 && base != key && base != alternate && isc32print(base);
1500 if (emit_alternate) {
1501 bytes = snprintf(p, left, ":%u", alternate);
1502 p += bytes; left -= bytes;
1505 if (emit_base) {
1506 bytes = snprintf(
1507 p, left, "%s:%u", !emit_alternate ? ":" : "", base);
1508 p += bytes; left -= bytes;
1512 bool emit_mods = encoded_mods > 1 || event[0] != '\0';
1514 if (emit_mods) {
1515 bytes = snprintf(p, left, ";%u%s", encoded_mods, event);
1516 p += bytes; left -= bytes;
1519 if (report_associated_text) {
1520 bytes = snprintf(p, left, "%s;%u", !emit_mods ? ";" : "", utf32[0]);
1521 p += bytes; left -= bytes;
1523 /* Additional text codepoints */
1524 if (utf32[0] != U'\0') {
1525 for (size_t i = 1; utf32[i] != U'\0'; i++) {
1526 bytes = snprintf(p, left, ":%u", utf32[i]);
1527 p += bytes; left -= bytes;
1532 bytes = snprintf(p, left, "%c", final);
1533 p += bytes; left -= bytes;
1534 } else {
1535 if (encoded_mods > 1 || event[0] != '\0') {
1536 bytes = snprintf(p, left, "\x1b[1;%u%s%c", encoded_mods, event, final);
1537 p += bytes; left -= bytes;
1538 } else {
1539 bytes = snprintf(p, left, "\x1b[%c", final);
1540 p += bytes; left -= bytes;
1544 return term_to_slave(term, buf, sizeof(buf) - left);
1547/* Copied from libxkbcommon (internal function) */
1548static bool
1549keysym_is_modifier(xkb_keysym_t keysym)
1551 return
1552 (keysym >= XKB_KEY_Shift_L && keysym <= XKB_KEY_Hyper_R) ||
1553 /* libX11 only goes up to XKB_KEY_ISO_Level5_Lock. */
1554 (keysym >= XKB_KEY_ISO_Lock && keysym <= XKB_KEY_ISO_Last_Group_Lock) ||
1555 keysym == XKB_KEY_Mode_switch ||
1556 keysym == XKB_KEY_Num_Lock;
1559#if defined(_DEBUG)
1560static void
1561modifier_string(xkb_mod_mask_t mods, size_t sz, char mod_str[static sz], const struct seat *seat)
1563 if (sz == 0)
1564 return;
1566 mod_str[0] = '\0';
1568 for (size_t i = 0; i < sizeof(xkb_mod_mask_t) * 8; i++) {
1569 if (!(mods & (1u << i)))
1570 continue;
1572 strcat(mod_str, xkb_keymap_mod_get_name(seat->kbd.xkb_keymap, i));
1573 strcat(mod_str, "+");
1576 if (mod_str[0] != '\0') {
1577 /* Strip the last '+' */
1578 mod_str[strlen(mod_str) - 1] = '\0';
1581 if (mod_str[0] == '\0') {
1582 strcpy(mod_str, "<none>");
1585#endif
1587static void
1588key_press_release(struct seat *seat, struct terminal *term, uint32_t serial,
1589 uint32_t key, uint32_t state)
1591 xassert(serial != 0);
1593 seat->kbd.serial = serial;
1594 if (seat->kbd.xkb == NULL ||
1595 seat->kbd.xkb_keymap == NULL ||
1596 seat->kbd.xkb_state == NULL)
1598 return;
1601 const bool pressed = state == WL_KEYBOARD_KEY_STATE_PRESSED;
1602 //const bool repeated = pressed && seat->kbd.repeat.dont_re_repeat;
1603 const bool released = state == WL_KEYBOARD_KEY_STATE_RELEASED;
1605 if (released)
1606 stop_repeater(seat, key);
1608 if (pressed)
1609 seat->kbd.last_shortcut_sym = XKB_KEYSYM_MAX + 1;
1611 bool should_repeat =
1612 pressed && xkb_keymap_key_repeats(seat->kbd.xkb_keymap, key);
1614 xkb_keysym_t sym = xkb_state_key_get_one_sym(seat->kbd.xkb_state, key);
1616 if (pressed && term->conf->mouse.hide_when_typing && !keysym_is_modifier(sym)) {
1617 seat->pointer.hidden = true;
1618 term_xcursor_update_for_seat(term, seat);
1621 enum xkb_compose_status compose_status = XKB_COMPOSE_NOTHING;
1623 if (seat->kbd.xkb_compose_state != NULL) {
1624 if (pressed)
1625 xkb_compose_state_feed(seat->kbd.xkb_compose_state, sym);
1626 compose_status = xkb_compose_state_get_status(
1627 seat->kbd.xkb_compose_state);
1630 const bool composed = compose_status == XKB_COMPOSE_COMPOSED;
1632 xkb_mod_mask_t mods, consumed;
1633 get_current_modifiers(seat, &mods, &consumed, key, true);
1635 xkb_layout_index_t layout_idx =
1636 xkb_state_key_get_layout(seat->kbd.xkb_state, key);
1638 const xkb_keysym_t *raw_syms = NULL;
1639 size_t raw_count = xkb_keymap_key_get_syms_by_level(
1640 seat->kbd.xkb_keymap, key, layout_idx, 0, &raw_syms);
1642 const struct key_binding_set *bindings = key_binding_for(
1643 seat->wayl->key_binding_manager, term->conf, seat);
1644 xassert(bindings != NULL);
1646 if (term->unicode_mode.active) {
1647 if (pressed)
1648 unicode_mode_input(seat, term, sym);
1649 return;
1652 else if (term->is_searching) {
1653 if (pressed) {
1654 if (should_repeat)
1655 start_repeater(seat, key);
1657 search_input(
1658 seat, term, bindings, key, sym, mods, consumed,
1659 raw_syms, raw_count, serial);
1661 return;
1664 else if (urls_mode_is_active(term)) {
1665 if (pressed) {
1666 if (should_repeat)
1667 start_repeater(seat, key);
1669 urls_input(
1670 seat, term, bindings, key, sym, mods, consumed,
1671 raw_syms, raw_count, serial);
1673 return;
1676#if defined(_DEBUG) && defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
1677 char sym_name[100];
1678 xkb_keysym_get_name(sym, sym_name, sizeof(sym_name));
1680 char active_mods_str[256] = {0};
1681 char consumed_mods_str[256] = {0};
1682 char locked_mods_str[256] = {0};
1684 const xkb_mod_mask_t locked =
1685 xkb_state_serialize_mods(seat->kbd.xkb_state, XKB_STATE_MODS_LOCKED);
1687 modifier_string(mods, sizeof(active_mods_str), active_mods_str, seat);
1688 modifier_string(consumed, sizeof(consumed_mods_str), consumed_mods_str, seat);
1689 modifier_string(locked, sizeof(locked_mods_str), locked_mods_str, seat);
1691 LOG_DBG("%s: %s (%u/0x%x), seat=%s, term=%p, serial=%u, "
1692 "mods=%s (0x%08x), consumed=%s (0x%08x), locked=%s (0x%08x), "
1693 "repeats=%d",
1694 pressed ? "pressed" : "released", sym_name, sym, sym,
1695 seat->name, (void *)term, serial,
1696 active_mods_str, mods, consumed_mods_str, consumed,
1697 locked_mods_str, locked, should_repeat);
1698#endif
1700 /*
1701 * User configurable bindings
1702 */
1703 if (pressed) {
1704 /* Match untranslated symbols */
1705 tll_foreach(bindings->key, it) {
1706 const struct key_binding *bind = &it->item;
1708 if (bind->mods != mods || bind->mods == 0)
1709 continue;
1711 for (size_t i = 0; i < raw_count; i++) {
1712 if (bind->k.sym == raw_syms[i] &&
1713 execute_binding(seat, term, bind, serial, 1))
1715 seat->kbd.last_shortcut_sym = sym;
1716 goto maybe_repeat;
1721 /* Match translated symbol */
1722 tll_foreach(bindings->key, it) {
1723 const struct key_binding *bind = &it->item;
1725 if (bind->k.sym == sym &&
1726 bind->mods == (mods & ~consumed) &&
1727 execute_binding(seat, term, bind, serial, 1))
1729 seat->kbd.last_shortcut_sym = sym;
1730 goto maybe_repeat;
1734 /* Match raw key code */
1735 tll_foreach(bindings->key, it) {
1736 const struct key_binding *bind = &it->item;
1738 if (bind->mods != mods || bind->mods == 0)
1739 continue;
1741 tll_foreach(bind->k.key_codes, code) {
1742 if (code->item == key &&
1743 execute_binding(seat, term, bind, serial, 1))
1745 seat->kbd.last_shortcut_sym = sym;
1746 goto maybe_repeat;
1752 if (released && seat->kbd.last_shortcut_sym == sym) {
1753 /*
1754 * Don't process a release event, if it corresponds to a
1755 * triggered shortcut.
1757 * 1. If we consumed a key (press) event, we shouldn't emit an
1758 * escape for its release event.
1759 * 2. Ignoring the incorrectness of doing so; this also caused
1760 * us to reset the viewport.
1762 * Background: if the kitty keyboard protocol was enabled,
1763 * then the viewport was instantly reset to the bottom, after
1764 * scrolling up.
1765 */
1766 //seat->kbd.last_shortcut_sym = XKB_KEYSYM_MAX + 1;
1767 goto maybe_repeat;
1770 /*
1771 * Keys generating escape sequences
1772 */
1775 /*
1776 * Compose, and maybe emit "normal" character
1777 */
1779 xassert(seat->kbd.xkb_compose_state != NULL || !composed);
1781 if (compose_status == XKB_COMPOSE_CANCELLED)
1782 goto maybe_repeat;
1784 int count = composed
1785 ? xkb_compose_state_get_utf8(seat->kbd.xkb_compose_state, NULL, 0)
1786 : xkb_state_key_get_utf8(seat->kbd.xkb_state, key, NULL, 0);
1788 /* Buffer for translated key. Use a static buffer in most cases,
1789 * and use a malloc:ed buffer when necessary */
1790 uint8_t buf[32];
1791 uint8_t *utf8 = count < sizeof(buf) ? buf : xmalloc(count + 1);
1792 uint32_t *utf32 = NULL;
1794 if (composed) {
1795 xkb_compose_state_get_utf8(
1796 seat->kbd.xkb_compose_state, (char *)utf8, count + 1);
1798 if (count > 0)
1799 utf32 = ambstoc32((const char *)utf8);
1800 } else {
1801 xkb_state_key_get_utf8(
1802 seat->kbd.xkb_state, key, (char *)utf8, count + 1);
1804 utf32 = xcalloc(2, sizeof(utf32[0]));
1805 utf32[0] = xkb_state_key_get_utf32(seat->kbd.xkb_state, key);
1808 struct kbd_ctx ctx = {
1809 .layout = layout_idx,
1810 .key = key,
1811 .sym = sym,
1812 .level0_syms = {
1813 .syms = raw_syms,
1814 .count = raw_count,
1815 },
1816 .mods = mods,
1817 .consumed = consumed,
1818 .utf8 = {
1819 .buf = utf8,
1820 .count = count,
1821 },
1822 .utf32 = utf32,
1823 .compose_status = compose_status,
1824 .key_state = state,
1825 };
1827 bool handled = term->grid->kitty_kbd.flags[term->grid->kitty_kbd.idx] != 0
1828 ? kitty_kbd_protocol(seat, term, &ctx)
1829 : legacy_kbd_protocol(seat, term, &ctx);
1831 if (composed && released)
1832 xkb_compose_state_reset(seat->kbd.xkb_compose_state);
1834 if (utf8 != buf)
1835 free(utf8);
1837 if (handled && !keysym_is_modifier(sym)) {
1838 term_reset_view(term);
1839 selection_cancel(term);
1842 free(utf32);
1844maybe_repeat:
1845 clock_gettime(
1846 term->wl->presentation_clock_id, &term->render.input_time);
1848 if (should_repeat)
1849 start_repeater(seat, key);
1852static void
1853keyboard_key(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,
1854 uint32_t time, uint32_t key, uint32_t state)
1856 struct seat *seat = data;
1857 key_press_release(seat, seat->kbd_focus, serial, key + 8, state);
1860static void
1861keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard, uint32_t serial,
1862 uint32_t mods_depressed, uint32_t mods_latched,
1863 uint32_t mods_locked, uint32_t group)
1865 struct seat *seat = data;
1867 mods_depressed &= ~seat->kbd.virtual_modifiers;
1868 mods_latched &= ~seat->kbd.virtual_modifiers;
1869 mods_locked &= ~seat->kbd.virtual_modifiers;
1871#if defined(_DEBUG)
1872 char depressed[256];
1873 char latched[256];
1874 char locked[256];
1876 modifier_string(mods_depressed, sizeof(depressed), depressed, seat);
1877 modifier_string(mods_latched, sizeof(latched), latched, seat);
1878 modifier_string(mods_locked, sizeof(locked), locked, seat);
1880 LOG_DBG(
1881 "modifiers: depressed=%s (0x%x), latched=%s (0x%x), locked=%s (0x%x), "
1882 "group=%u",
1883 depressed, mods_depressed, latched, mods_latched, locked, mods_locked,
1884 group);
1885#endif
1887 if (seat->kbd.xkb_state != NULL) {
1888 xkb_state_update_mask(
1889 seat->kbd.xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);
1891 /* Update state of modifiers we're interested in for e.g mouse events */
1892 seat->kbd.shift = seat->kbd.mod_shift != XKB_MOD_INVALID
1893 ? xkb_state_mod_index_is_active(
1894 seat->kbd.xkb_state, seat->kbd.mod_shift, XKB_STATE_MODS_EFFECTIVE)
1895 : false;
1896 seat->kbd.alt = seat->kbd.mod_alt != XKB_MOD_INVALID
1897 ? xkb_state_mod_index_is_active(
1898 seat->kbd.xkb_state, seat->kbd.mod_alt, XKB_STATE_MODS_EFFECTIVE)
1899 : false;
1900 seat->kbd.ctrl = seat->kbd.mod_ctrl != XKB_MOD_INVALID
1901 ? xkb_state_mod_index_is_active(
1902 seat->kbd.xkb_state, seat->kbd.mod_ctrl, XKB_STATE_MODS_EFFECTIVE)
1903 : false;
1904 seat->kbd.super = seat->kbd.mod_super != XKB_MOD_INVALID
1905 ? xkb_state_mod_index_is_active(
1906 seat->kbd.xkb_state, seat->kbd.mod_super, XKB_STATE_MODS_EFFECTIVE)
1907 : false;
1910 if (seat->kbd_focus && seat->kbd_focus->active_surface == TERM_SURF_GRID)
1911 term_xcursor_update_for_seat(seat->kbd_focus, seat);
1914UNITTEST
1916 int chan[2];
1917 xassert(pipe2(chan, O_CLOEXEC) == 0);
1919 xassert(chan[0] >= 0);
1920 xassert(chan[1] >= 0);
1922 struct config conf = {0};
1923 struct grid grid = {0};
1925 struct terminal term = {
1926 .conf = &conf,
1927 .grid = &grid,
1928 .ptmx = chan[1],
1929 .selection = {
1930 .coords = {
1931 .start = {-1, -1},
1932 .end = {-1, -1},
1933 },
1934 .auto_scroll = {
1935 .fd = -1,
1936 },
1937 },
1938 };
1940 struct key_binding_manager *key_binding_manager = key_binding_manager_new();
1942 struct wayland wayl = {
1943 .key_binding_manager = key_binding_manager,
1944 .terms = tll_init(),
1945 };
1947 struct seat seat = {
1948 .wayl = &wayl,
1949 .name = "unittest",
1950 };
1952 tll_push_back(wayl.terms, &term);
1953 term.wl = &wayl;
1955 seat.kbd.xkb = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
1956 xassert(seat.kbd.xkb != NULL);
1958 grid.kitty_kbd.flags[0] = KITTY_KBD_DISAMBIGUATE | KITTY_KBD_REPORT_ALTERNATE;
1960 /* Swedish keymap */
1962 seat.kbd.xkb_keymap = xkb_keymap_new_from_names(
1963 seat.kbd.xkb, &(struct xkb_rule_names){.layout = "se"}, XKB_KEYMAP_COMPILE_NO_FLAGS);
1964 if (seat.kbd.xkb_keymap == NULL) {
1965 /* Skip test */
1966 goto no_keymap;
1969 seat.kbd.xkb_state = xkb_state_new(seat.kbd.xkb_keymap);
1970 xassert(seat.kbd.xkb_state != NULL);
1972 seat.kbd.mod_shift = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_SHIFT);
1973 seat.kbd.mod_alt = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_ALT) ;
1974 seat.kbd.mod_ctrl = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_CTRL);
1975 seat.kbd.mod_super = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_LOGO);
1976 seat.kbd.mod_caps = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_CAPS);
1977 seat.kbd.mod_num = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_NUM);
1979 /* Significant modifiers in the legacy keyboard protocol */
1980 seat.kbd.legacy_significant = 0;
1981 if (seat.kbd.mod_shift != XKB_MOD_INVALID)
1982 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_shift;
1983 if (seat.kbd.mod_alt != XKB_MOD_INVALID)
1984 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_alt;
1985 if (seat.kbd.mod_ctrl != XKB_MOD_INVALID)
1986 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_ctrl;
1987 if (seat.kbd.mod_super != XKB_MOD_INVALID)
1988 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_super;
1990 /* Significant modifiers in the kitty keyboard protocol */
1991 seat.kbd.kitty_significant = seat.kbd.legacy_significant;
1992 if (seat.kbd.mod_caps != XKB_MOD_INVALID)
1993 seat.kbd.kitty_significant |= 1 << seat.kbd.mod_caps;
1994 if (seat.kbd.mod_num != XKB_MOD_INVALID)
1995 seat.kbd.kitty_significant |= 1 << seat.kbd.mod_num;
1997 key_binding_new_for_seat(key_binding_manager, &seat);
1998 key_binding_load_keymap(key_binding_manager, &seat);
2001 xkb_mod_mask_t mods = 1u << seat.kbd.mod_shift | 1u << seat.kbd.mod_ctrl;
2002 keyboard_modifiers(&seat, NULL, 1337, mods, 0, 0, 0);
2003 key_press_release(&seat, &term, 1337, KEY_A + 8, WL_KEYBOARD_KEY_STATE_PRESSED);
2005 char escape[64] = {0};
2006 ssize_t count = read(chan[0], escape, sizeof(escape));
2008 /* key: 97 = 'a', alternate: 65 = 'A', base: N/A, mods: 6 = ctrl+shift */
2009 const char expected_ctrl_shift_a[] = "\033[97:65;6u";
2010 xassert(count == strlen(expected_ctrl_shift_a));
2011 xassert(streq(escape, expected_ctrl_shift_a));
2013 key_press_release(&seat, &term, 1337, KEY_A + 8, WL_KEYBOARD_KEY_STATE_RELEASED);
2017 xkb_mod_mask_t mods = 1u << seat.kbd.mod_shift | 1u << seat.kbd.mod_alt;
2018 keyboard_modifiers(&seat, NULL, 1337, mods, 0, 0, 0);
2019 key_press_release(&seat, &term, 1337, KEY_2 + 8, WL_KEYBOARD_KEY_STATE_PRESSED);
2021 char escape[64] = {0};
2022 ssize_t count = read(chan[0], escape, sizeof(escape));
2024 /* key;. 50 = '2', alternate: 34 = '"', base: N/A, 4 = alt+shift */
2025 const char expected_alt_shift_2[] = "\033[50:34;4u";
2026 xassert(count == strlen(expected_alt_shift_2));
2027 xassert(streq(escape, expected_alt_shift_2));
2029 key_press_release(&seat, &term, 1337, KEY_2 + 8, WL_KEYBOARD_KEY_STATE_RELEASED);
2033 xkb_mod_index_t alt_gr = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, "Mod5");
2034 xassert(alt_gr != XKB_MOD_INVALID);
2036 xkb_mod_mask_t mods = 1u << seat.kbd.mod_shift | 1u << seat.kbd.mod_alt | 1u << alt_gr;
2037 keyboard_modifiers(&seat, NULL, 1337, mods, 0, 0, 0);
2038 key_press_release(&seat, &term, 1337, KEY_2 + 8, WL_KEYBOARD_KEY_STATE_PRESSED);
2040 char escape[64] = {0};
2041 ssize_t count = read(chan[0], escape, sizeof(escape));
2043 /* key; 178 = '²', alternate: N/A, base: 50 = '2', 4 = alt+shift (AltGr not part of the protocol) */
2044 const char expected_altgr_alt_shift_2[] = "\033[178::50;4u";
2045 xassert(count == strlen(expected_altgr_alt_shift_2));
2046 xassert(streq(escape, expected_altgr_alt_shift_2));
2048 key_press_release(&seat, &term, 1337, KEY_2 + 8, WL_KEYBOARD_KEY_STATE_RELEASED);
2052 xkb_mod_mask_t mods = 1u << seat.kbd.mod_alt;
2053 keyboard_modifiers(&seat, NULL, 1337, mods, 0, 0, 0);
2054 key_press_release(&seat, &term, 1337, KEY_BACKSPACE + 8, WL_KEYBOARD_KEY_STATE_PRESSED);
2056 char escape[64] = {0};
2057 ssize_t count = read(chan[0], escape, sizeof(escape));
2059 /* key; 127 = <backspace>, alternate: N/A, base: N/A, 3 = alt */
2060 const char expected_alt_backspace[] = "\033[127;3u";
2061 xassert(count == strlen(expected_alt_backspace));
2062 xassert(streq(escape, expected_alt_backspace));
2064 key_press_release(&seat, &term, 1337, KEY_BACKSPACE + 8, WL_KEYBOARD_KEY_STATE_RELEASED);
2068 xkb_mod_mask_t mods = 1u << seat.kbd.mod_ctrl;
2069 keyboard_modifiers(&seat, NULL, 1337, mods, 0, 0, 0);
2070 key_press_release(&seat, &term, 1337, KEY_ENTER + 8, WL_KEYBOARD_KEY_STATE_PRESSED);
2072 char escape[64] = {0};
2073 ssize_t count = read(chan[0], escape, sizeof(escape));
2075 /* key; 13 = <enter>, alternate: N/A, base: N/A, 5 = ctrl */
2076 const char expected_ctrl_enter[] = "\033[13;5u";
2077 xassert(count == strlen(expected_ctrl_enter));
2078 xassert(streq(escape, expected_ctrl_enter));
2080 key_press_release(&seat, &term, 1337, KEY_ENTER + 8, WL_KEYBOARD_KEY_STATE_RELEASED);
2084 xkb_mod_mask_t mods = 1u << seat.kbd.mod_ctrl;
2085 keyboard_modifiers(&seat, NULL, 1337, mods, 0, 0, 0);
2086 key_press_release(&seat, &term, 1337, KEY_TAB + 8, WL_KEYBOARD_KEY_STATE_PRESSED);
2088 char escape[64] = {0};
2089 ssize_t count = read(chan[0], escape, sizeof(escape));
2091 /* key; 9 = <tab>, alternate: N/A, base: N/A, 5 = ctrl */
2092 const char expected_ctrl_tab[] = "\033[9;5u";
2093 xassert(count == strlen(expected_ctrl_tab));
2094 xassert(streq(escape, expected_ctrl_tab));
2096 key_press_release(&seat, &term, 1337, KEY_TAB + 8, WL_KEYBOARD_KEY_STATE_RELEASED);
2100 xkb_mod_mask_t mods = 1u << seat.kbd.mod_ctrl | 1u << seat.kbd.mod_shift;
2101 keyboard_modifiers(&seat, NULL, 1337, mods, 0, 0, 0);
2102 key_press_release(&seat, &term, 1337, KEY_LEFT + 8, WL_KEYBOARD_KEY_STATE_PRESSED);
2104 char escape[64] = {0};
2105 ssize_t count = read(chan[0], escape, sizeof(escape));
2107 const char expected_ctrl_shift_left[] = "\033[1;6D";
2108 xassert(count == strlen(expected_ctrl_shift_left));
2109 xassert(streq(escape, expected_ctrl_shift_left));
2111 key_press_release(&seat, &term, 1337, KEY_LEFT + 8, WL_KEYBOARD_KEY_STATE_RELEASED);
2113 key_binding_unload_keymap(key_binding_manager, &seat);
2114 key_binding_remove_seat(key_binding_manager, &seat);
2116 xkb_state_unref(seat.kbd.xkb_state);
2117 xkb_keymap_unref(seat.kbd.xkb_keymap);
2119 seat.kbd.xkb_state = NULL;
2120 seat.kbd.xkb_keymap = NULL;
2123 /* de(neo) keymap */
2125 seat.kbd.xkb_keymap = xkb_keymap_new_from_names(
2126 seat.kbd.xkb, &(struct xkb_rule_names){.layout = "us,de(neo)"},
2127 XKB_KEYMAP_COMPILE_NO_FLAGS);
2129 if (seat.kbd.xkb_keymap == NULL) {
2130 /* Skip test */
2131 goto no_keymap;
2134 seat.kbd.xkb_state = xkb_state_new(seat.kbd.xkb_keymap);
2135 xassert(seat.kbd.xkb_state != NULL);
2137 seat.kbd.mod_shift = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_SHIFT);
2138 seat.kbd.mod_alt = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_ALT) ;
2139 seat.kbd.mod_ctrl = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_CTRL);
2140 seat.kbd.mod_super = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_LOGO);
2141 seat.kbd.mod_caps = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_CAPS);
2142 seat.kbd.mod_num = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_NUM);
2144 /* Significant modifiers in the legacy keyboard protocol */
2145 seat.kbd.legacy_significant = 0;
2146 if (seat.kbd.mod_shift != XKB_MOD_INVALID)
2147 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_shift;
2148 if (seat.kbd.mod_alt != XKB_MOD_INVALID)
2149 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_alt;
2150 if (seat.kbd.mod_ctrl != XKB_MOD_INVALID)
2151 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_ctrl;
2152 if (seat.kbd.mod_super != XKB_MOD_INVALID)
2153 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_super;
2155 /* Significant modifiers in the kitty keyboard protocol */
2156 seat.kbd.kitty_significant = seat.kbd.legacy_significant;
2157 if (seat.kbd.mod_caps != XKB_MOD_INVALID)
2158 seat.kbd.kitty_significant |= 1 << seat.kbd.mod_caps;
2159 if (seat.kbd.mod_num != XKB_MOD_INVALID)
2160 seat.kbd.kitty_significant |= 1 << seat.kbd.mod_num;
2162 key_binding_new_for_seat(key_binding_manager, &seat);
2163 key_binding_load_keymap(key_binding_manager, &seat);
2166 /*
2167 * In the de(neo) layout, the Y key generates 'k'. This
2168 * means we should get a key+alternate that indicates 'k',
2169 * but a base key that is 'y'.
2170 */
2171 xkb_mod_mask_t mods = 1u << seat.kbd.mod_shift | 1u << seat.kbd.mod_alt;
2172 keyboard_modifiers(&seat, NULL, 1337, mods, 0, 0, 1);
2173 key_press_release(&seat, &term, 1337, KEY_Y + 8, WL_KEYBOARD_KEY_STATE_PRESSED);
2175 char escape[64] = {0};
2176 ssize_t count = read(chan[0], escape, sizeof(escape));
2178 /* key: 107 = 'k', alternate: 75 = 'K', base: 121 = 'y', mods: 4 = alt+shift */
2179 const char expected_alt_shift_y[] = "\033[107:75:121;4u";
2180 xassert(count == strlen(expected_alt_shift_y));
2181 xassert(streq(escape, expected_alt_shift_y));
2183 key_press_release(&seat, &term, 1337, KEY_Y + 8, WL_KEYBOARD_KEY_STATE_RELEASED);
2186 key_binding_unload_keymap(key_binding_manager, &seat);
2187 key_binding_remove_seat(key_binding_manager, &seat);
2189 xkb_state_unref(seat.kbd.xkb_state);
2190 xkb_keymap_unref(seat.kbd.xkb_keymap);
2192 seat.kbd.xkb_state = NULL;
2193 seat.kbd.xkb_keymap = NULL;
2196 /* us(intl) keymap */
2198 seat.kbd.xkb_keymap = xkb_keymap_new_from_names(
2199 seat.kbd.xkb, &(struct xkb_rule_names){.layout = "us", .variant = "intl"},
2200 XKB_KEYMAP_COMPILE_NO_FLAGS);
2202 if (seat.kbd.xkb_keymap == NULL) {
2203 /* Skip test */
2204 goto no_keymap;
2207 seat.kbd.xkb_state = xkb_state_new(seat.kbd.xkb_keymap);
2208 xassert(seat.kbd.xkb_state != NULL);
2210 seat.kbd.xkb_compose_table = xkb_compose_table_new_from_locale(
2211 seat.kbd.xkb, setlocale(LC_CTYPE, NULL), XKB_COMPOSE_COMPILE_NO_FLAGS);
2212 if (seat.kbd.xkb_compose_table == NULL)
2213 goto no_keymap;
2215 seat.kbd.xkb_compose_state = xkb_compose_state_new(
2216 seat.kbd.xkb_compose_table, XKB_COMPOSE_STATE_NO_FLAGS);
2217 if (seat.kbd.xkb_compose_state == NULL) {
2218 xkb_compose_table_unref(seat.kbd.xkb_compose_table);
2219 goto no_keymap;
2222 seat.kbd.mod_shift = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_SHIFT);
2223 seat.kbd.mod_alt = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_ALT) ;
2224 seat.kbd.mod_ctrl = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_CTRL);
2225 seat.kbd.mod_super = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_LOGO);
2226 seat.kbd.mod_caps = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_CAPS);
2227 seat.kbd.mod_num = xkb_keymap_mod_get_index(seat.kbd.xkb_keymap, XKB_MOD_NAME_NUM);
2229 /* Significant modifiers in the legacy keyboard protocol */
2230 seat.kbd.legacy_significant = 0;
2231 if (seat.kbd.mod_shift != XKB_MOD_INVALID)
2232 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_shift;
2233 if (seat.kbd.mod_alt != XKB_MOD_INVALID)
2234 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_alt;
2235 if (seat.kbd.mod_ctrl != XKB_MOD_INVALID)
2236 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_ctrl;
2237 if (seat.kbd.mod_super != XKB_MOD_INVALID)
2238 seat.kbd.legacy_significant |= 1 << seat.kbd.mod_super;
2240 /* Significant modifiers in the kitty keyboard protocol */
2241 seat.kbd.kitty_significant = seat.kbd.legacy_significant;
2242 if (seat.kbd.mod_caps != XKB_MOD_INVALID)
2243 seat.kbd.kitty_significant |= 1 << seat.kbd.mod_caps;
2244 if (seat.kbd.mod_num != XKB_MOD_INVALID)
2245 seat.kbd.kitty_significant |= 1 << seat.kbd.mod_num;
2247 key_binding_new_for_seat(key_binding_manager, &seat);
2248 key_binding_load_keymap(key_binding_manager, &seat);
2251 /*
2252 * Test the compose sequence "shift+', shift+space"
2254 * Should result in a double quote, but a regression
2255 * caused it to instead emit a space. See #1987
2257 * Note: "shift+', space" also results in a double quote,
2258 * but never regressed to a space.
2259 */
2260 grid.kitty_kbd.flags[0] = KITTY_KBD_DISAMBIGUATE;
2261 xkb_compose_state_reset(seat.kbd.xkb_compose_state);
2263 xkb_mod_mask_t mods = 1u << seat.kbd.mod_shift;
2264 keyboard_modifiers(&seat, NULL, 1337, mods, 0, 0, 1);
2266 key_press_release(&seat, &term, 1337, KEY_APOSTROPHE + 8, WL_KEYBOARD_KEY_STATE_PRESSED);
2267 key_press_release(&seat, &term, 1337, KEY_APOSTROPHE + 8, WL_KEYBOARD_KEY_STATE_RELEASED);
2269 key_press_release(&seat, &term, 1337, KEY_SPACE + 8, WL_KEYBOARD_KEY_STATE_PRESSED);
2271 char escape[64] = {0};
2272 ssize_t count = read(chan[0], escape, sizeof(escape));
2274 /* key: 34 = '"', alternate: N/A, base: N/A, mods: 2 = shift */
2275 const char expected_shift_apostrophe[] = "\033[34;2u";
2276 xassert(count == strlen(expected_shift_apostrophe));
2277 xassert(streq(escape, expected_shift_apostrophe));
2279 key_press_release(&seat, &term, 1337, KEY_SPACE + 8, WL_KEYBOARD_KEY_STATE_RELEASED);
2281 grid.kitty_kbd.flags[0] = KITTY_KBD_DISAMBIGUATE | KITTY_KBD_REPORT_ALTERNATE;
2284 key_binding_unload_keymap(key_binding_manager, &seat);
2285 key_binding_remove_seat(key_binding_manager, &seat);
2287 xkb_compose_state_unref(seat.kbd.xkb_compose_state);
2288 xkb_compose_table_unref(seat.kbd.xkb_compose_table);
2290 xkb_state_unref(seat.kbd.xkb_state);
2291 xkb_keymap_unref(seat.kbd.xkb_keymap);
2293 seat.kbd.xkb_state = NULL;
2294 seat.kbd.xkb_keymap = NULL;
2297no_keymap:
2298 xkb_context_unref(seat.kbd.xkb);
2299 key_binding_manager_destroy(key_binding_manager);
2301 tll_free(wayl.terms);
2302 close(chan[0]);
2303 close(chan[1]);
2306static void
2307keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
2308 int32_t rate, int32_t delay)
2310 struct seat *seat = data;
2311 LOG_DBG("keyboard repeat: rate=%d, delay=%d", rate, delay);
2312 seat->kbd.repeat.rate = rate;
2313 seat->kbd.repeat.delay = delay;
2316const struct wl_keyboard_listener keyboard_listener = {
2317 .keymap = &keyboard_keymap,
2318 .enter = &keyboard_enter,
2319 .leave = &keyboard_leave,
2320 .key = &keyboard_key,
2321 .modifiers = &keyboard_modifiers,
2322 .repeat_info = &keyboard_repeat_info,
2325void
2326input_repeat(struct seat *seat, uint32_t key)
2328 /* Should be cleared as soon as we loose focus */
2329 xassert(seat->kbd_focus != NULL);
2330 struct terminal *term = seat->kbd_focus;
2332 key_press_release(seat, term, seat->kbd.serial, key, XKB_KEY_DOWN);
2335static bool
2336is_top_left(const struct terminal *term, int x, int y)
2338 int csd_border_size = term->conf->csd.border_width;
2339 return (
2340 (!term->window->is_constrained_top && !term->window->is_constrained_left) &&
2341 ((term->active_surface == TERM_SURF_BORDER_LEFT && y < 10 * term->scale) ||
2342 (term->active_surface == TERM_SURF_BORDER_TOP && x < (10 + csd_border_size) * term->scale)));
2345static bool
2346is_top_right(const struct terminal *term, int x, int y)
2348 int csd_border_size = term->conf->csd.border_width;
2349 return (
2350 (!term->window->is_constrained_top && !term->window->is_constrained_right) &&
2351 ((term->active_surface == TERM_SURF_BORDER_RIGHT && y < 10 * term->scale) ||
2352 (term->active_surface == TERM_SURF_BORDER_TOP && x > term->width + 1 * csd_border_size * term->scale - 10 * term->scale)));
2355static bool
2356is_bottom_left(const struct terminal *term, int x, int y)
2358 int csd_title_size = term->conf->csd.title_height;
2359 int csd_border_size = term->conf->csd.border_width;
2360 return (
2361 (!term->window->is_constrained_bottom && !term->window->is_constrained_left) &&
2362 ((term->active_surface == TERM_SURF_BORDER_LEFT && y > csd_title_size * term->scale + term->height) ||
2363 (term->active_surface == TERM_SURF_BORDER_BOTTOM && x < (10 + csd_border_size) * term->scale)));
2366static bool
2367is_bottom_right(const struct terminal *term, int x, int y)
2369 int csd_title_size = term->conf->csd.title_height;
2370 int csd_border_size = term->conf->csd.border_width;
2371 return (
2372 (!term->window->is_constrained_bottom && !term->window->is_constrained_right) &&
2373 ((term->active_surface == TERM_SURF_BORDER_RIGHT && y > csd_title_size * term->scale + term->height) ||
2374 (term->active_surface == TERM_SURF_BORDER_BOTTOM && x > term->width + 1 * csd_border_size * term->scale - 10 * term->scale)));
2377enum cursor_shape
2378xcursor_for_csd_border(struct terminal *term, int x, int y)
2380 if (is_top_left(term, x, y)) return CURSOR_SHAPE_TOP_LEFT_CORNER;
2381 else if (is_top_right(term, x, y)) return CURSOR_SHAPE_TOP_RIGHT_CORNER;
2382 else if (is_bottom_left(term, x, y)) return CURSOR_SHAPE_BOTTOM_LEFT_CORNER;
2383 else if (is_bottom_right(term, x, y)) return CURSOR_SHAPE_BOTTOM_RIGHT_CORNER;
2385 else if (term->active_surface == TERM_SURF_BORDER_LEFT)
2386 return !term->window->is_constrained_left
2387 ? CURSOR_SHAPE_LEFT_SIDE : CURSOR_SHAPE_LEFT_PTR;
2389 else if (term->active_surface == TERM_SURF_BORDER_RIGHT)
2390 return !term->window->is_constrained_right
2391 ? CURSOR_SHAPE_RIGHT_SIDE : CURSOR_SHAPE_LEFT_PTR;
2393 else if (term->active_surface == TERM_SURF_BORDER_TOP)
2394 return !term->window->is_constrained_top
2395 ? CURSOR_SHAPE_TOP_SIDE : CURSOR_SHAPE_LEFT_PTR;
2397 else if (term->active_surface == TERM_SURF_BORDER_BOTTOM)
2398 return !term->window->is_constrained_bottom
2399 ? CURSOR_SHAPE_BOTTOM_SIDE : CURSOR_SHAPE_LEFT_PTR;
2401 else {
2402 BUG("Unreachable");
2403 return CURSOR_SHAPE_NONE;
2407static void
2408mouse_button_state_reset(struct seat *seat)
2410 tll_free(seat->mouse.buttons);
2411 seat->mouse.count = 0;
2412 seat->mouse.last_released_button = 0;
2413 memset(&seat->mouse.last_time, 0, sizeof(seat->mouse.last_time));
2416 void
2417mouse_coord_pixel_to_cell(struct seat *seat, const struct terminal *term,
2418 int x, int y)
2420 /*
2421 * Translate x,y pixel coordinate to a cell coordinate, or clamp
2422 * if the cursor is outside the grid. I.e. if it is inside the
2423 * margins.
2424 */
2425 if (x < term->margins.left)
2426 seat->mouse.col = 0;
2427 else if (x >= term->width - term->margins.right)
2428 seat->mouse.col = term->cols - 1;
2429 else
2430 seat->mouse.col = (x - term->margins.left) / term->cell_width;
2432 if (y < term->margins.top)
2433 seat->mouse.row = 0;
2434 else if (y >= term->height - term->margins.bottom)
2435 seat->mouse.row = term->rows - 1;
2436 else
2437 seat->mouse.row = (y - term->margins.top) / term->cell_height;
2440static bool
2441touch_is_active(const struct seat *seat)
2443 if (seat->wl_touch == NULL) {
2444 return false;
2447 switch (seat->touch.state) {
2448 case TOUCH_STATE_IDLE:
2449 case TOUCH_STATE_INHIBITED:
2450 return false;
2452 case TOUCH_STATE_HELD:
2453 case TOUCH_STATE_DRAGGING:
2454 case TOUCH_STATE_SCROLLING:
2455 return true;
2458 BUG("Bad touch state: %d", seat->touch.state);
2459 return false;
2462static void
2463wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
2464 uint32_t serial, struct wl_surface *surface,
2465 wl_fixed_t surface_x, wl_fixed_t surface_y)
2467 if (unlikely(surface == NULL)) {
2468 /* Seen on mutter-3.38 */
2469 LOG_WARN("compositor sent pointer_enter event with a NULL surface");
2470 return;
2473 struct seat *seat = data;
2475 struct wl_window *win = wl_surface_get_user_data(surface);
2476 struct terminal *term = win->term;
2478 seat->mouse_focus = term;
2479 term->active_surface = term_surface_kind(term, surface);
2481 if (touch_is_active(seat))
2482 return;
2484 int x = wl_fixed_to_int(surface_x) * term->scale;
2485 int y = wl_fixed_to_int(surface_y) * term->scale;
2487 seat->pointer.serial = serial;
2488 seat->pointer.hidden = false;
2489 seat->mouse.x = x;
2490 seat->mouse.y = y;
2492 LOG_DBG("pointer-enter: pointer=%p, serial=%u, surface = %p, new-moused = %p, "
2493 "x=%d, y=%d",
2494 (void *)wl_pointer, serial, (void *)surface, (void *)term,
2495 x, y);
2497 xassert(tll_length(seat->mouse.buttons) == 0);
2499 wayl_reload_xcursor_theme(seat, term->scale); /* Scale may have changed */
2500 term_xcursor_update_for_seat(term, seat);
2502 switch (term->active_surface) {
2503 case TERM_SURF_GRID: {
2504 mouse_coord_pixel_to_cell(seat, term, x, y);
2505 break;
2508 case TERM_SURF_TITLE:
2509 case TERM_SURF_BORDER_LEFT:
2510 case TERM_SURF_BORDER_RIGHT:
2511 case TERM_SURF_BORDER_TOP:
2512 case TERM_SURF_BORDER_BOTTOM:
2513 break;
2515 case TERM_SURF_BUTTON_MINIMIZE:
2516 case TERM_SURF_BUTTON_MAXIMIZE:
2517 case TERM_SURF_BUTTON_CLOSE:
2518 render_refresh_csd(term);
2519 break;
2521 case TERM_SURF_NONE:
2522 BUG("Invalid surface type");
2523 break;
2527static void
2528wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
2529 uint32_t serial, struct wl_surface *surface)
2531 struct seat *seat = data;
2533 if (seat->wl_touch != NULL) {
2534 switch (seat->touch.state) {
2535 case TOUCH_STATE_IDLE:
2536 break;
2538 case TOUCH_STATE_INHIBITED:
2539 seat->touch.state = TOUCH_STATE_IDLE;
2540 break;
2542 case TOUCH_STATE_HELD:
2543 case TOUCH_STATE_DRAGGING:
2544 case TOUCH_STATE_SCROLLING:
2545 return;
2549 struct terminal *old_moused = seat->mouse_focus;
2551 LOG_DBG(
2552 "%s: pointer-leave: pointer=%p, serial=%u, surface = %p, old-moused = %p",
2553 seat->name, (void *)wl_pointer, serial, (void *)surface,
2554 (void *)old_moused);
2556 seat->pointer.hidden = false;
2558 if (seat->pointer.xcursor_callback != NULL) {
2559 /* A cursor frame callback may never be called if the pointer leaves our surface */
2560 wl_callback_destroy(seat->pointer.xcursor_callback);
2561 seat->pointer.xcursor_callback = NULL;
2562 seat->pointer.xcursor_pending = false;
2565 /* Reset last-set-xcursor, to ensure we update it on a pointer-enter event */
2566 seat->pointer.shape = CURSOR_SHAPE_NONE;
2568 /* Reset mouse state */
2569 seat->mouse.x = seat->mouse.y = 0;
2570 seat->mouse.col = seat->mouse.row = 0;
2571 mouse_button_state_reset(seat);
2572 for (size_t i = 0; i < ALEN(seat->mouse.aggregated); i++)
2573 seat->mouse.aggregated[i] = 0.0;
2574 seat->mouse.have_discrete = false;
2576 seat->mouse_focus = NULL;
2577 if (old_moused == NULL) {
2578 LOG_WARN(
2579 "compositor sent pointer_leave event without a pointer_enter "
2580 "event: surface=%p", (void *)surface);
2581 } else {
2582 if (surface != NULL) {
2583 /* Sway 1.4 sends this event with a NULL surface when we destroy the window */
2584 const struct wl_window UNUSED *win = wl_surface_get_user_data(surface);
2585 xassert(old_moused == win->term);
2588 enum term_surface active_surface = old_moused->active_surface;
2590 old_moused->active_surface = TERM_SURF_NONE;
2592 switch (active_surface) {
2593 case TERM_SURF_BUTTON_MINIMIZE:
2594 case TERM_SURF_BUTTON_MAXIMIZE:
2595 case TERM_SURF_BUTTON_CLOSE:
2596 if (old_moused->shutdown.in_progress)
2597 break;
2599 render_refresh_csd(old_moused);
2600 break;
2602 case TERM_SURF_GRID:
2603 selection_finalize(seat, old_moused, seat->pointer.serial);
2604 break;
2606 case TERM_SURF_NONE:
2607 case TERM_SURF_TITLE:
2608 case TERM_SURF_BORDER_LEFT:
2609 case TERM_SURF_BORDER_RIGHT:
2610 case TERM_SURF_BORDER_TOP:
2611 case TERM_SURF_BORDER_BOTTOM:
2612 break;
2618static bool
2619pointer_is_on_button(const struct terminal *term, const struct seat *seat,
2620 enum csd_surface csd_surface)
2622 if (seat->mouse.x < 0)
2623 return false;
2624 if (seat->mouse.y < 0)
2625 return false;
2627 struct csd_data info = get_csd_data(term, csd_surface);
2628 if (seat->mouse.x > info.width)
2629 return false;
2631 if (seat->mouse.y > info.height)
2632 return false;
2634 return true;
2637static void
2638wl_pointer_motion(void *data, struct wl_pointer *wl_pointer,
2639 uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y)
2641 struct seat *seat = data;
2643 /* Touch-emulated pointer events have wl_pointer == NULL. */
2644 if (wl_pointer != NULL && touch_is_active(seat))
2645 return;
2647 struct wayland *wayl = seat->wayl;
2648 struct terminal *term = seat->mouse_focus;
2650 if (unlikely(term == NULL)) {
2651 /* Typically happens when the compositor sent a pointer enter
2652 * event with a NULL surface - see wl_pointer_enter().
2654 * In this case, we never set seat->mouse_focus (since we
2655 * can't map the enter event to a specific window). */
2656 return;
2659 struct wl_window *win = term->window;
2661 LOG_DBG("pointer_motion: pointer=%p, x=%d, y=%d", (void *)wl_pointer,
2662 wl_fixed_to_int(surface_x), wl_fixed_to_int(surface_y));
2664 xassert(term != NULL);
2666 int x = wl_fixed_to_int(surface_x) * term->scale;
2667 int y = wl_fixed_to_int(surface_y) * term->scale;
2669 enum term_surface surf_kind = term->active_surface;
2670 int button = 0;
2671 bool send_to_client = false;
2672 bool is_on_button = false;
2674 /* If current surface is a button, check if pointer was on it
2675 *before* the motion event */
2676 switch (surf_kind) {
2677 case TERM_SURF_BUTTON_MINIMIZE:
2678 is_on_button = pointer_is_on_button(term, seat, CSD_SURF_MINIMIZE);
2679 break;
2681 case TERM_SURF_BUTTON_MAXIMIZE:
2682 is_on_button = pointer_is_on_button(term, seat, CSD_SURF_MAXIMIZE);
2683 break;
2685 case TERM_SURF_BUTTON_CLOSE:
2686 is_on_button = pointer_is_on_button(term, seat, CSD_SURF_CLOSE);
2687 break;
2689 case TERM_SURF_NONE:
2690 case TERM_SURF_GRID:
2691 case TERM_SURF_TITLE:
2692 case TERM_SURF_BORDER_LEFT:
2693 case TERM_SURF_BORDER_RIGHT:
2694 case TERM_SURF_BORDER_TOP:
2695 case TERM_SURF_BORDER_BOTTOM:
2696 break;
2699 seat->pointer.hidden = false;
2700 seat->mouse.x = x;
2701 seat->mouse.y = y;
2703 term_xcursor_update_for_seat(term, seat);
2705 if (tll_length(seat->mouse.buttons) > 0) {
2706 const struct button_tracker *tracker = &tll_front(seat->mouse.buttons);
2707 surf_kind = tracker->surf_kind;
2708 button = tracker->button;
2709 send_to_client = tracker->send_to_client;
2712 switch (surf_kind) {
2713 case TERM_SURF_NONE:
2714 break;
2716 case TERM_SURF_BUTTON_MINIMIZE:
2717 if (pointer_is_on_button(term, seat, CSD_SURF_MINIMIZE) != is_on_button)
2718 render_refresh_csd(term);
2719 break;
2721 case TERM_SURF_BUTTON_MAXIMIZE:
2722 if (pointer_is_on_button(term, seat, CSD_SURF_MAXIMIZE) != is_on_button)
2723 render_refresh_csd(term);
2724 break;
2726 case TERM_SURF_BUTTON_CLOSE:
2727 if (pointer_is_on_button(term, seat, CSD_SURF_CLOSE) != is_on_button)
2728 render_refresh_csd(term);
2729 break;
2731 case TERM_SURF_TITLE:
2732 /* We've started a 'move' timer, but user started dragging
2733 * right away - abort the timer and initiate the actual move
2734 * right away */
2735 if (button == BTN_LEFT && win->csd.move_timeout_fd != -1) {
2736 fdm_del(wayl->fdm, win->csd.move_timeout_fd);
2737 win->csd.move_timeout_fd = -1;
2738 xdg_toplevel_move(win->xdg_toplevel, seat->wl_seat, win->csd.serial);
2740 break;
2742 case TERM_SURF_BORDER_LEFT:
2743 case TERM_SURF_BORDER_RIGHT:
2744 case TERM_SURF_BORDER_TOP:
2745 case TERM_SURF_BORDER_BOTTOM:
2746 break;
2748 case TERM_SURF_GRID: {
2749 int old_col = seat->mouse.col;
2750 int old_row = seat->mouse.row;
2752 mouse_coord_pixel_to_cell(seat, term, seat->mouse.x, seat->mouse.y);
2754 xassert(seat->mouse.col >= 0 && seat->mouse.col < term->cols);
2755 xassert(seat->mouse.row >= 0 && seat->mouse.row < term->rows);
2757 /* Cursor has moved to a different cell since last time */
2758 bool cursor_is_on_new_cell
2759 = old_col != seat->mouse.col || old_row != seat->mouse.row;
2761 if (cursor_is_on_new_cell) {
2762 /* Prevent multiple/different mouse bindings from
2763 * triggering if the mouse has moved "too much" (to
2764 * another cell) */
2765 seat->mouse.count = 0;
2768 /* Cursor is inside the grid, i.e. *not* in the margins */
2769 const bool cursor_is_on_grid = seat->mouse.col >= 0 && seat->mouse.row >= 0;
2771 enum selection_scroll_direction auto_scroll_direction
2772 = term->selection.coords.end.row < 0
2773 ? SELECTION_SCROLL_NOT
2774 : y < term->margins.top
2775 ? SELECTION_SCROLL_UP
2776 : y > term->height - term->margins.bottom
2777 ? SELECTION_SCROLL_DOWN
2778 : SELECTION_SCROLL_NOT;
2780 if (auto_scroll_direction == SELECTION_SCROLL_NOT)
2781 selection_stop_scroll_timer(term);
2783 /* Update selection */
2784 if (!term->is_searching) {
2785 if (auto_scroll_direction != SELECTION_SCROLL_NOT) {
2786 /*
2787 * Start 'selection auto-scrolling'
2789 * The speed of the scrolling is proportional to the
2790 * distance between the mouse and the grid; the
2791 * further away the mouse is, the faster we scroll.
2793 * Note that the speed is measured in 'intervals (in
2794 * ns) between each timed scroll of a single line'.
2796 * Thus, the further away the mouse is, the smaller
2797 * interval value we use.
2798 */
2800 int distance = auto_scroll_direction == SELECTION_SCROLL_UP
2801 ? term->margins.top - y
2802 : y - (term->height - term->margins.bottom);
2804 xassert(distance > 0);
2805 int divisor
2806 = distance * term->conf->scrollback.multiplier / term->scale;
2808 selection_start_scroll_timer(
2809 term, 400000000 / (divisor > 0 ? divisor : 1),
2810 auto_scroll_direction, seat->mouse.col);
2813 if (term->selection.ongoing &&
2814 (cursor_is_on_new_cell ||
2815 (term->selection.coords.end.row < 0 &&
2816 seat->mouse.x >= term->margins.left &&
2817 seat->mouse.x < term->width - term->margins.right &&
2818 seat->mouse.y >= term->margins.top &&
2819 seat->mouse.y < term->height - term->margins.bottom)))
2821 selection_update(term, seat->mouse.col, seat->mouse.row);
2825 /* Send mouse event to client application */
2826 if (!term_mouse_grabbed(term, seat) &&
2827 (cursor_is_on_new_cell ||
2828 term->mouse_reporting == MOUSE_SGR_PIXELS) &&
2829 ((button == 0 && cursor_is_on_grid) ||
2830 (button != 0 && send_to_client)))
2832 xassert(seat->mouse.col < term->cols);
2833 xassert(seat->mouse.row < term->rows);
2835 term_mouse_motion(
2836 term, button,
2837 seat->mouse.row, seat->mouse.col,
2838 seat->mouse.y - term->margins.top,
2839 seat->mouse.x - term->margins.left,
2840 seat->kbd.shift, seat->kbd.alt, seat->kbd.ctrl);
2842 break;
2847static bool
2848fdm_csd_move(struct fdm *fdm, int fd, int events, void *data)
2850 struct seat *seat = data;
2851 fdm_del(fdm, fd);
2853 if (seat->mouse_focus == NULL) {
2854 LOG_WARN(
2855 "%s: CSD move timeout triggered, but seat's has no mouse focused terminal",
2856 seat->name);
2857 return true;
2860 struct wl_window *win = seat->mouse_focus->window;
2862 win->csd.move_timeout_fd = -1;
2863 xdg_toplevel_move(win->xdg_toplevel, seat->wl_seat, win->csd.serial);
2864 return true;
2867static const struct key_binding *
2868match_mouse_binding(const struct seat *seat, const struct terminal *term,
2869 int button)
2871 if (seat->wl_keyboard != NULL && seat->kbd.xkb_state != NULL) {
2872 /* Seat has keyboard - use mouse bindings *with* modifiers */
2874 const struct key_binding_set *bindings =
2875 key_binding_for(term->wl->key_binding_manager, term->conf, seat);
2876 xassert(bindings != NULL);
2878 xkb_mod_mask_t mods;
2879 get_current_modifiers(seat, &mods, NULL, 0, true);
2881 /* Ignore selection override modifiers when
2882 * matching modifiers */
2883 mods &= ~bindings->selection_overrides;
2885 const struct key_binding *match = NULL;
2887 tll_foreach(bindings->mouse, it) {
2888 const struct key_binding *binding = &it->item;
2890 if (binding->m.button != button) {
2891 /* Wrong button */
2892 continue;
2895 if (binding->mods != mods) {
2896 /* Modifier mismatch */
2897 continue;
2900 if (binding->m.count > seat->mouse.count) {
2901 /* Not correct click count */
2902 continue;
2905 if (match == NULL || binding->m.count > match->m.count)
2906 match = binding;
2909 return match;
2912 else {
2913 /* Seat does NOT have a keyboard - use mouse bindings *without*
2914 * modifiers */
2915 const struct config_key_binding *match = NULL;
2916 const struct config *conf = term->conf;
2918 for (size_t i = 0; i < conf->bindings.mouse.count; i++) {
2919 const struct config_key_binding *binding =
2920 &conf->bindings.mouse.arr[i];
2922 if (binding->m.button != button) {
2923 /* Wrong button */
2924 continue;
2927 if (binding->m.count > seat->mouse.count) {
2928 /* Incorrect click count */
2929 continue;
2932 if (tll_length(binding->modifiers) > 0) {
2933 /* Binding has modifiers */
2934 continue;
2937 if (match == NULL || binding->m.count > match->m.count)
2938 match = binding;
2941 if (match != NULL) {
2942 static struct key_binding bind;
2943 bind.action = match->action;
2944 bind.aux = &match->aux;
2945 return &bind;
2948 return NULL;
2951 BUG("should not get here");
2954static void
2955wl_pointer_button(void *data, struct wl_pointer *wl_pointer,
2956 uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
2958 LOG_DBG("BUTTON: pointer=%p, serial=%u, button=%x, state=%u",
2959 (void *)wl_pointer, serial, button, state);
2961 xassert(serial != 0);
2963 struct seat *seat = data;
2965 /* Touch-emulated pointer events have wl_pointer == NULL. */
2966 if (wl_pointer != NULL && touch_is_active(seat))
2967 return;
2969 struct wayland *wayl = seat->wayl;
2970 struct terminal *term = seat->mouse_focus;
2972 seat->pointer.serial = serial;
2973 seat->pointer.hidden = false;
2975 xassert(term != NULL);
2977 enum term_surface surf_kind = TERM_SURF_NONE;
2978 bool send_to_client = false;
2980 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
2981 if (seat->wl_touch != NULL && seat->touch.state == TOUCH_STATE_IDLE) {
2982 seat->touch.state = TOUCH_STATE_INHIBITED;
2985 /* Time since last click */
2986 struct timespec now, since_last;
2987 clock_gettime(CLOCK_MONOTONIC, &now);
2988 timespec_sub(&now, &seat->mouse.last_time, &since_last);
2990 if (seat->mouse.last_released_button == button &&
2991 since_last.tv_sec == 0 && since_last.tv_nsec <= 300 * 1000 * 1000)
2993 seat->mouse.count++;
2994 } else
2995 seat->mouse.count = 1;
2997 /*
2998 * Workaround GNOME bug
3000 * Dragging the window, then stopping the drag (releasing the
3001 * mouse button), *without* moving the mouse, and then
3002 * clicking twice, waiting for the CSD timer, and finally
3003 * clicking once more, results in the following sequence
3004 * (keyboard and other irrelevant events filtered out, unless
3005 * they're needed to prove a point):
3007 * dbg: input.c:1551: cancelling drag timer, moving window
3008 * dbg: input.c:759: keyboard_leave: keyboard=0x607000003580, serial=873, surface=0x6070000036d0
3009 * dbg: input.c:1432: seat0: pointer-leave: pointer=0x607000003660, serial=874, surface = 0x6070000396e0, old-moused = 0x622000006100
3011 * --> drag stopped here
3013 * --> LMB clicked first time after the drag (generates the
3014 * enter event on *release*, but no button events)
3015 * dbg: input.c:1360: pointer-enter: pointer=0x607000003660, serial=876, surface = 0x6070000396e0, new-moused = 0x622000006100
3017 * --> LMB clicked, and held until the timer times out, second
3018 * time after the drag
3019 * dbg: input.c:1712: BUTTON: pointer=0x607000003660, serial=877, button=110, state=1
3020 * dbg: input.c:1806: starting move timer
3021 * dbg: input.c:1692: move timer timed out
3022 * dbg: input.c:759: keyboard_leave: keyboard=0x607000003580, serial=878, surface=0x6070000036d0
3024 * --> NOTE: ^^ no pointer leave event this time, only the
3025 * keyboard leave
3027 * --> LMB clicked one last time
3028 * dbg: input.c:697: seat0: keyboard_enter: keyboard=0x607000003580, serial=879, surface=0x6070000036d0
3029 * dbg: input.c:1712: BUTTON: pointer=0x607000003660, serial=880, button=110, state=1
3030 * err: input.c:1741: BUG in wl_pointer_button(): assertion failed: 'it->item.button != button'
3032 * What are we seeing?
3034 * - GNOME does *not* send a pointer *enter* event after the drag
3035 * has stopped
3036 * - The second drag does *not* generate a pointer *leave* event
3037 * - The missing leave event means we're still tracking LMB as
3038 * being held down in our seat struct.
3039 * - This leads to an assert (debug builds) when LMB is clicked
3040 * again (seat's button list already contains LMB).
3042 * Note: I've also observed variants of the above
3043 */
3044 tll_foreach(seat->mouse.buttons, it) {
3045 if (it->item.button == button) {
3046 LOG_WARN("multiple button press events for button %d "
3047 "(compositor bug?)", button);
3048 tll_remove(seat->mouse.buttons, it);
3049 break;
3053#if defined(_DEBUG)
3054 tll_foreach(seat->mouse.buttons, it)
3055 xassert(it->item.button != button);
3056#endif
3058 /*
3059 * Remember which surface "owns" this button, so that we can
3060 * send motion and button release events to that surface, even
3061 * if the pointer is no longer over it.
3062 */
3063 tll_push_back(
3064 seat->mouse.buttons,
3065 ((struct button_tracker){
3066 .button = button,
3067 .surf_kind = term->active_surface,
3068 .send_to_client = false}));
3070 seat->mouse.last_time = now;
3072 surf_kind = term->active_surface;
3073 send_to_client = false; /* For now, may be set to true if a binding consumes the button */
3074 } else {
3075 bool UNUSED have_button = false;
3076 tll_foreach(seat->mouse.buttons, it) {
3077 if (it->item.button == button) {
3078 have_button = true;
3079 surf_kind = it->item.surf_kind;
3080 send_to_client = it->item.send_to_client;
3081 tll_remove(seat->mouse.buttons, it);
3082 break;
3086 if (seat->wl_touch != NULL && seat->touch.state == TOUCH_STATE_INHIBITED) {
3087 if (tll_length(seat->mouse.buttons) == 0) {
3088 seat->touch.state = TOUCH_STATE_IDLE;
3092 if (!have_button) {
3093 /*
3094 * Seen on Sway with slurp
3096 * 1. Run slurp
3097 * 2. Press, and hold left mouse button
3098 * 3. Press escape, to cancel slurp
3099 * 4. Release mouse button
3100 * 5. BAM!
3101 */
3102 LOG_WARN("stray button release event (compositor bug?)");
3103 return;
3106 seat->mouse.last_released_button = button;
3109 switch (surf_kind) {
3110 case TERM_SURF_TITLE:
3111 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
3113 struct wl_window *win = term->window;
3115 /* Toggle maximized state on double-click */
3116 if (term->conf->csd.double_click_to_maximize &&
3117 button == BTN_LEFT &&
3118 seat->mouse.count == 2)
3120 if (win->is_maximized)
3121 xdg_toplevel_unset_maximized(win->xdg_toplevel);
3122 else
3123 xdg_toplevel_set_maximized(win->xdg_toplevel);
3126 else if (button == BTN_LEFT && win->csd.move_timeout_fd < 0) {
3127 const struct itimerspec timeout = {
3128 .it_value = {.tv_nsec = 200000000},
3129 };
3131 int fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
3132 if (fd >= 0 &&
3133 timerfd_settime(fd, 0, &timeout, NULL) == 0 &&
3134 fdm_add(wayl->fdm, fd, EPOLLIN, &fdm_csd_move, seat))
3136 win->csd.move_timeout_fd = fd;
3137 win->csd.serial = serial;
3138 } else {
3139 LOG_ERRNO("failed to configure XDG toplevel move timer FD");
3140 if (fd >= 0)
3141 close(fd);
3145 if (button == BTN_RIGHT && tll_length(seat->mouse.buttons) == 1) {
3146 const struct csd_data info = get_csd_data(term, CSD_SURF_TITLE);
3147 xdg_toplevel_show_window_menu(
3148 win->xdg_toplevel,
3149 seat->wl_seat,
3150 seat->pointer.serial,
3151 seat->mouse.x + info.x, seat->mouse.y + info.y);
3155 else if (state == WL_POINTER_BUTTON_STATE_RELEASED) {
3156 struct wl_window *win = term->window;
3157 if (win->csd.move_timeout_fd >= 0) {
3158 fdm_del(wayl->fdm, win->csd.move_timeout_fd);
3159 win->csd.move_timeout_fd = -1;
3162 return;
3164 case TERM_SURF_BORDER_LEFT:
3165 case TERM_SURF_BORDER_RIGHT:
3166 case TERM_SURF_BORDER_TOP:
3167 case TERM_SURF_BORDER_BOTTOM: {
3168 if (button == BTN_LEFT && state == WL_POINTER_BUTTON_STATE_PRESSED) {
3169 enum xdg_toplevel_resize_edge resize_type = XDG_TOPLEVEL_RESIZE_EDGE_NONE;
3171 int x = seat->mouse.x;
3172 int y = seat->mouse.y;
3174 if (is_top_left(term, x, y))
3175 resize_type = XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT;
3176 else if (is_top_right(term, x, y))
3177 resize_type = XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT;
3178 else if (is_bottom_left(term, x, y))
3179 resize_type = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT;
3180 else if (is_bottom_right(term, x, y))
3181 resize_type = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT;
3182 else {
3183 if (term->active_surface == TERM_SURF_BORDER_LEFT &&
3184 !term->window->is_constrained_left)
3186 resize_type = XDG_TOPLEVEL_RESIZE_EDGE_LEFT;
3189 else if (term->active_surface == TERM_SURF_BORDER_RIGHT &&
3190 !term->window->is_constrained_right)
3192 resize_type = XDG_TOPLEVEL_RESIZE_EDGE_RIGHT;
3195 else if (term->active_surface == TERM_SURF_BORDER_TOP &&
3196 !term->window->is_constrained_top)
3198 resize_type = XDG_TOPLEVEL_RESIZE_EDGE_TOP;
3201 else if (term->active_surface == TERM_SURF_BORDER_BOTTOM &&
3202 !term->window->is_constrained_bottom)
3204 resize_type = XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM;
3208 if (resize_type != XDG_TOPLEVEL_RESIZE_EDGE_NONE) {
3209 xdg_toplevel_resize(
3210 term->window->xdg_toplevel, seat->wl_seat, serial, resize_type);
3213 return;
3216 case TERM_SURF_BUTTON_MINIMIZE:
3217 if (button == BTN_LEFT &&
3218 pointer_is_on_button(term, seat, CSD_SURF_MINIMIZE) &&
3219 state == WL_POINTER_BUTTON_STATE_RELEASED)
3221 xdg_toplevel_set_minimized(term->window->xdg_toplevel);
3223 break;
3225 case TERM_SURF_BUTTON_MAXIMIZE:
3226 if (button == BTN_LEFT &&
3227 pointer_is_on_button(term, seat, CSD_SURF_MAXIMIZE) &&
3228 state == WL_POINTER_BUTTON_STATE_RELEASED)
3230 if (term->window->is_maximized)
3231 xdg_toplevel_unset_maximized(term->window->xdg_toplevel);
3232 else
3233 xdg_toplevel_set_maximized(term->window->xdg_toplevel);
3235 break;
3237 case TERM_SURF_BUTTON_CLOSE:
3238 if (button == BTN_LEFT &&
3239 pointer_is_on_button(term, seat, CSD_SURF_CLOSE) &&
3240 state == WL_POINTER_BUTTON_STATE_RELEASED)
3242 term_shutdown(term);
3244 break;
3246 case TERM_SURF_GRID: {
3247 search_cancel(term);
3248 urls_reset(term);
3250 bool cursor_is_on_grid = seat->mouse.col >= 0 && seat->mouse.row >= 0;
3252 switch (state) {
3253 case WL_POINTER_BUTTON_STATE_PRESSED: {
3254 bool consumed = false;
3256 if (cursor_is_on_grid && term_mouse_grabbed(term, seat)) {
3257 const struct key_binding *match =
3258 match_mouse_binding(seat, term, button);
3260 if (match != NULL)
3261 consumed = execute_binding(seat, term, match, serial, 1);
3264 send_to_client = !consumed && cursor_is_on_grid;
3266 if (send_to_client)
3267 tll_back(seat->mouse.buttons).send_to_client = true;
3269 if (send_to_client &&
3270 !term_mouse_grabbed(term, seat) &&
3271 cursor_is_on_grid)
3273 term_mouse_down(
3274 term, button, seat->mouse.row, seat->mouse.col,
3275 seat->mouse.y - term->margins.top,
3276 seat->mouse.x - term->margins.left,
3277 seat->kbd.shift, seat->kbd.alt, seat->kbd.ctrl);
3279 break;
3282 case WL_POINTER_BUTTON_STATE_RELEASED:
3283 selection_finalize(seat, term, serial);
3285 if (send_to_client && !term_mouse_grabbed(term, seat)) {
3286 term_mouse_up(
3287 term, button, seat->mouse.row, seat->mouse.col,
3288 seat->mouse.y - term->margins.top,
3289 seat->mouse.x - term->margins.left,
3290 seat->kbd.shift, seat->kbd.alt, seat->kbd.ctrl);
3292 break;
3294 break;
3297 case TERM_SURF_NONE:
3298 BUG("Invalid surface type");
3299 break;
3304static void
3305alternate_scroll(struct seat *seat, int amount, int button)
3307 if (seat->wl_keyboard == NULL)
3308 return;
3310 /* Should be cleared in leave event */
3311 xassert(seat->mouse_focus != NULL);
3312 struct terminal *term = seat->mouse_focus;
3314 assert(button == BTN_BACK || button == BTN_FORWARD);
3316 xkb_keycode_t key = button == BTN_BACK
3317 ? seat->kbd.key_arrow_up : seat->kbd.key_arrow_down;
3319 for (int i = 0; i < amount; i++)
3320 key_press_release(seat, term, seat->kbd.serial, key, XKB_KEY_DOWN);
3321 key_press_release(seat, term, seat->kbd.serial, key, XKB_KEY_UP);
3324static void
3325mouse_scroll(struct seat *seat, int amount, enum wl_pointer_axis axis)
3327 struct terminal *term = seat->mouse_focus;
3328 xassert(term != NULL);
3330 int button = axis == WL_POINTER_AXIS_VERTICAL_SCROLL
3331 ? amount < 0 ? BTN_WHEEL_BACK : BTN_WHEEL_FORWARD
3332 : amount < 0 ? BTN_WHEEL_LEFT : BTN_WHEEL_RIGHT;
3333 amount = abs(amount);
3335 if (term_mouse_grabbed(term, seat)) {
3336 seat->mouse.count = 1;
3338 const struct key_binding *match =
3339 match_mouse_binding(seat, term, button);
3341 if (match != NULL)
3342 execute_binding(seat, term, match, seat->pointer.serial, amount);
3344 seat->mouse.last_released_button = button;
3347 else if (seat->mouse.col >= 0 && seat->mouse.row >= 0) {
3348 xassert(seat->mouse.col < term->cols);
3349 xassert(seat->mouse.row < term->rows);
3351 for (int i = 0; i < amount; i++) {
3352 term_mouse_down(
3353 term, button, seat->mouse.row, seat->mouse.col,
3354 seat->mouse.y - term->margins.top,
3355 seat->mouse.x - term->margins.left,
3356 seat->kbd.shift, seat->kbd.alt, seat->kbd.ctrl);
3359 term_mouse_up(
3360 term, button, seat->mouse.row, seat->mouse.col,
3361 seat->mouse.y - term->margins.top,
3362 seat->mouse.x - term->margins.left,
3363 seat->kbd.shift, seat->kbd.alt, seat->kbd.ctrl);
3367static double
3368mouse_scroll_multiplier(const struct terminal *term, const struct seat *seat)
3370 return (term->grid == &term->normal ||
3371 (term_mouse_grabbed(term, seat) && term->alt_scrolling))
3372 ? term->conf->scrollback.multiplier
3373 : 1.0;
3376static void
3377wl_pointer_axis(void *data, struct wl_pointer *wl_pointer,
3378 uint32_t time, uint32_t axis, wl_fixed_t value)
3380 struct seat *seat = data;
3382 if (touch_is_active(seat))
3383 return;
3385 if (seat->mouse.have_discrete)
3386 return;
3388 xassert(seat->mouse_focus != NULL);
3389 xassert(axis < ALEN(seat->mouse.aggregated));
3391 const struct terminal *term = seat->mouse_focus;
3393 /*
3394 * Aggregate scrolled amount until we get at least 1.0
3396 * Without this, very slow scrolling will never actually scroll
3397 * anything.
3398 */
3399 seat->mouse.aggregated[axis] +=
3400 mouse_scroll_multiplier(term, seat) * wl_fixed_to_double(value);
3401 if (fabs(seat->mouse.aggregated[axis]) < seat->mouse_focus->cell_height)
3402 return;
3404 int lines = seat->mouse.aggregated[axis] / seat->mouse_focus->cell_height;
3405 mouse_scroll(seat, lines, axis);
3406 seat->mouse.aggregated[axis] -= (double)lines * seat->mouse_focus->cell_height;
3409static void
3410wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
3411 enum wl_pointer_axis axis, int32_t discrete)
3413 LOG_DBG("axis_discrete: %d", discrete);
3414 struct seat *seat = data;
3416 if (touch_is_active(seat))
3417 return;
3419 seat->mouse.have_discrete = true;
3420 int amount = discrete;
3422 if (axis == WL_POINTER_AXIS_HORIZONTAL_SCROLL) {
3423 /* Treat mouse wheel left/right as regular buttons */
3424 } else
3425 amount *= mouse_scroll_multiplier(seat->mouse_focus, seat);
3427 mouse_scroll(seat, amount, axis);
3430#if defined(WL_POINTER_AXIS_VALUE120_SINCE_VERSION)
3431static void
3432wl_pointer_axis_value120(void *data, struct wl_pointer *wl_pointer,
3433 enum wl_pointer_axis axis, int32_t value120)
3435 LOG_DBG("axis_value120: %d -> %.2f", value120, (float)value120 / 120.);
3437 struct seat *seat = data;
3439 if (touch_is_active(seat))
3440 return;
3442 seat->mouse.have_discrete = true;
3444 /*
3445 * 120 corresponds to a single "low-res" scroll step.
3447 * When doing high-res scrolling, take the scrollback.multiplier,
3448 * and calculate how many degrees there are per line.
3450 * For example, with scrollback.multiplier = 3, we have 120 / 3 == 40.
3452 * Then, accumulate high-res scroll events, until we have *at
3453 * least* that much. Translate the accumulated value to number of
3454 * lines, and scroll.
3456 * Subtract the "used" degrees from the accumulated value, and
3457 * keep what's left (this value will always be less than the
3458 * per-line value).
3459 */
3460 const double multiplier = mouse_scroll_multiplier(seat->mouse_focus, seat);
3461 const double per_line = 120. / multiplier;
3463 seat->mouse.aggregated_120[axis] += (double)value120;
3465 if (fabs(seat->mouse.aggregated_120[axis]) < per_line)
3466 return;
3468 int lines = (int)(seat->mouse.aggregated_120[axis] / per_line);
3469 mouse_scroll(seat, lines, axis);
3470 seat->mouse.aggregated_120[axis] -= (double)lines * per_line;
3472#endif
3474static void
3475wl_pointer_frame(void *data, struct wl_pointer *wl_pointer)
3477 struct seat *seat = data;
3479 if (touch_is_active(seat))
3480 return;
3482 seat->mouse.have_discrete = false;
3485static void
3486wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer,
3487 uint32_t axis_source)
3491static void
3492wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,
3493 uint32_t time, uint32_t axis)
3495 struct seat *seat = data;
3497 if (touch_is_active(seat))
3498 return;
3500 xassert(axis < ALEN(seat->mouse.aggregated));
3501 seat->mouse.aggregated[axis] = 0.;
3504const struct wl_pointer_listener pointer_listener = {
3505 .enter = &wl_pointer_enter,
3506 .leave = &wl_pointer_leave,
3507 .motion = &wl_pointer_motion,
3508 .button = &wl_pointer_button,
3509 .axis = &wl_pointer_axis,
3510 .frame = &wl_pointer_frame,
3511 .axis_source = &wl_pointer_axis_source,
3512 .axis_stop = &wl_pointer_axis_stop,
3513 .axis_discrete = &wl_pointer_axis_discrete,
3514#if defined(WL_POINTER_AXIS_VALUE120_SINCE_VERSION)
3515 .axis_value120 = &wl_pointer_axis_value120,
3516#endif
3519static bool
3520touch_to_scroll(struct seat *seat, struct terminal *term,
3521 wl_fixed_t surface_x, wl_fixed_t surface_y)
3523 bool coord_updated = false;
3525 int y = wl_fixed_to_int(surface_y) * term->scale;
3526 int rows = (y - seat->mouse.y) / term->cell_height;
3527 if (rows != 0) {
3528 mouse_scroll(seat, -rows, WL_POINTER_AXIS_VERTICAL_SCROLL);
3529 seat->mouse.y += rows * term->cell_height;
3530 coord_updated = true;
3533 int x = wl_fixed_to_int(surface_x) * term->scale;
3534 int cols = (x - seat->mouse.x) / term->cell_width;
3535 if (cols != 0) {
3536 mouse_scroll(seat, -cols, WL_POINTER_AXIS_HORIZONTAL_SCROLL);
3537 seat->mouse.x += cols * term->cell_width;
3538 coord_updated = true;
3541 return coord_updated;
3544static void
3545wl_touch_down(void *data, struct wl_touch *wl_touch, uint32_t serial,
3546 uint32_t time, struct wl_surface *surface, int32_t id,
3547 wl_fixed_t surface_x, wl_fixed_t surface_y)
3549 struct seat *seat = data;
3551 if (seat->touch.state != TOUCH_STATE_IDLE)
3552 return;
3554 struct wl_window *win = wl_surface_get_user_data(surface);
3555 struct terminal *term = win->term;
3557 LOG_DBG("touch_down: touch=%p, x=%d, y=%d", (void *)wl_touch,
3558 wl_fixed_to_int(surface_x), wl_fixed_to_int(surface_y));
3560 int x = wl_fixed_to_int(surface_x) * term->scale;
3561 int y = wl_fixed_to_int(surface_y) * term->scale;
3563 seat->mouse.x = x;
3564 seat->mouse.y = y;
3565 mouse_coord_pixel_to_cell(seat, term, x, y);
3567 seat->touch.state = TOUCH_STATE_HELD;
3568 seat->touch.serial = serial;
3569 seat->touch.time = time + term->conf->touch.long_press_delay;
3570 seat->touch.surface = surface;
3571 seat->touch.surface_kind = term_surface_kind(term, surface);
3572 seat->touch.id = id;
3575static void
3576wl_touch_up(void *data, struct wl_touch *wl_touch, uint32_t serial,
3577 uint32_t time, int32_t id)
3579 struct seat *seat = data;
3581 if (seat->touch.state <= TOUCH_STATE_IDLE || id != seat->touch.id)
3582 return;
3584 LOG_DBG("touch_up: touch=%p", (void *)wl_touch);
3586 struct wl_window *win = wl_surface_get_user_data(seat->touch.surface);
3587 struct terminal *term = win->term;
3589 struct terminal *old_term = seat->mouse_focus;
3590 enum term_surface old_active_surface = term->active_surface;
3591 seat->mouse_focus = term;
3592 term->active_surface = seat->touch.surface_kind;
3594 switch (seat->touch.state) {
3595 case TOUCH_STATE_HELD:
3596 wl_pointer_button(seat, NULL, seat->touch.serial, time, BTN_LEFT,
3597 WL_POINTER_BUTTON_STATE_PRESSED);
3598 /* fallthrough */
3599 case TOUCH_STATE_DRAGGING:
3600 wl_pointer_button(seat, NULL, serial, time, BTN_LEFT,
3601 WL_POINTER_BUTTON_STATE_RELEASED);
3602 /* fallthrough */
3603 case TOUCH_STATE_SCROLLING:
3604 term->active_surface = TERM_SURF_NONE;
3605 seat->touch.state = TOUCH_STATE_IDLE;
3606 break;
3608 case TOUCH_STATE_INHIBITED:
3609 case TOUCH_STATE_IDLE:
3610 BUG("Bad touch state: %d", seat->touch.state);
3611 break;
3614 seat->mouse_focus = old_term;
3615 term->active_surface = old_active_surface;
3618static void
3619wl_touch_motion(void *data, struct wl_touch *wl_touch, uint32_t time,
3620 int32_t id, wl_fixed_t surface_x, wl_fixed_t surface_y)
3622 struct seat *seat = data;
3623 if (seat->touch.state <= TOUCH_STATE_IDLE || id != seat->touch.id)
3624 return;
3626 LOG_DBG("touch_motion: touch=%p, x=%d, y=%d", (void *)wl_touch,
3627 wl_fixed_to_int(surface_x), wl_fixed_to_int(surface_y));
3629 struct wl_window *win = wl_surface_get_user_data(seat->touch.surface);
3630 struct terminal *term = win->term;
3632 struct terminal *old_term = seat->mouse_focus;
3633 enum term_surface old_active_surface = term->active_surface;
3634 seat->mouse_focus = term;
3635 term->active_surface = seat->touch.surface_kind;
3637 switch (seat->touch.state) {
3638 case TOUCH_STATE_HELD:
3639 if (time <= seat->touch.time && term->active_surface == TERM_SURF_GRID) {
3640 if (touch_to_scroll(seat, term, surface_x, surface_y))
3641 seat->touch.state = TOUCH_STATE_SCROLLING;
3642 break;
3643 } else {
3644 wl_pointer_button(seat, NULL, seat->touch.serial, time, BTN_LEFT,
3645 WL_POINTER_BUTTON_STATE_PRESSED);
3646 seat->touch.state = TOUCH_STATE_DRAGGING;
3647 /* fallthrough */
3649 case TOUCH_STATE_DRAGGING:
3650 wl_pointer_motion(seat, NULL, time, surface_x, surface_y);
3651 break;
3652 case TOUCH_STATE_SCROLLING:
3653 touch_to_scroll(seat, term, surface_x, surface_y);
3654 break;
3656 case TOUCH_STATE_INHIBITED:
3657 case TOUCH_STATE_IDLE:
3658 BUG("Bad touch state: %d", seat->touch.state);
3659 break;
3662 seat->mouse_focus = old_term;
3663 term->active_surface = old_active_surface;
3666static void
3667wl_touch_frame(void *data, struct wl_touch *wl_touch)
3671static void
3672wl_touch_cancel(void *data, struct wl_touch *wl_touch)
3674 struct seat *seat = data;
3675 if (seat->touch.state == TOUCH_STATE_INHIBITED)
3676 return;
3678 seat->touch.state = TOUCH_STATE_IDLE;
3681const struct wl_touch_listener touch_listener = {
3682 .down = wl_touch_down,
3683 .up = wl_touch_up,
3684 .motion = wl_touch_motion,
3685 .frame = wl_touch_frame,
3686 .cancel = wl_touch_cancel,
Diagnostics5 git calls · 824 ms
CallStatusSizeTime0824 ms
discover200178 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=178
git.server.agent=git/2.56.1-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=178
git.pkt.payload.max=35
ls-refs20010 KiB462 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=402
git.response.bytes=10322
git.response.cache=EXPIRED
git.response.duration_ms=462
git.protocol.version=2
git.pkt.frames=108
git.pkt.data=107
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=10322
git.pkt.payload.max=106
git.ls_refs.count=107
git.ls_refs.head=refs/heads/master
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=8830
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=8830
git.pkt.payload.max=8730
git.sideband.band1.bytes=8730
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=14
git.pack.bytes=8730
git.fetch.wide.objects=14
git.fetch.wide.extra_bytes=9909
git.fetch.wide.blobs=0
fetchtree:0 · deepen 102009.3 KiB · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=9489
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=9489
git.pkt.payload.max=9389
git.sideband.band1.bytes=9389
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=9389
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a
git.pack.full=10
Open pack map
fetch20025 KiB358 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=358
git.response.bytes=25657
git.response.cache=MISS
git.response.duration_ms=358
git.fetch.wants=ed85dbb81af747e8c06df0d26d350862db2caf80
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=25657
git.pkt.payload.max=25630
git.sideband.band1.bytes=25630
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://codeberg.org/dnkl/foot
Ref
refs/heads/master
Commit
c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
cf-ray
a5e1f3f328dbefe9
Request
bdff8bfd…
Protocol
v2 · sha1
Agent
git/2.56.1-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format