Add support for OSC 8 hyperlinks (a VTE extension now supported by other
[tmux.git] / input-keys.c
blob038003df422a3eb8a81a8edfe07c3723dc36c8b8
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include "tmux.h"
28 * This file is rather misleadingly named, it contains the code which takes a
29 * key code and translates it into something suitable to be sent to the
30 * application running in a pane (similar to input.c does in the other
31 * direction with output).
34 static void input_key_mouse(struct window_pane *, struct mouse_event *);
36 /* Entry in the key tree. */
37 struct input_key_entry {
38 key_code key;
39 const char *data;
41 RB_ENTRY(input_key_entry) entry;
43 RB_HEAD(input_key_tree, input_key_entry);
45 /* Tree of input keys. */
46 static int input_key_cmp(struct input_key_entry *,
47 struct input_key_entry *);
48 RB_GENERATE_STATIC(input_key_tree, input_key_entry, entry, input_key_cmp);
49 struct input_key_tree input_key_tree = RB_INITIALIZER(&input_key_tree);
51 /* List of default keys, the tree is built from this. */
52 static struct input_key_entry input_key_defaults[] = {
53 /* Paste keys. */
54 { .key = KEYC_PASTE_START,
55 .data = "\033[200~"
57 { .key = KEYC_PASTE_END,
58 .data = "\033[201~"
61 /* Function keys. */
62 { .key = KEYC_F1,
63 .data = "\033OP"
65 { .key = KEYC_F2,
66 .data = "\033OQ"
68 { .key = KEYC_F3,
69 .data = "\033OR"
71 { .key = KEYC_F4,
72 .data = "\033OS"
74 { .key = KEYC_F5,
75 .data = "\033[15~"
77 { .key = KEYC_F6,
78 .data = "\033[17~"
80 { .key = KEYC_F7,
81 .data = "\033[18~"
83 { .key = KEYC_F8,
84 .data = "\033[19~"
86 { .key = KEYC_F9,
87 .data = "\033[20~"
89 { .key = KEYC_F10,
90 .data = "\033[21~"
92 { .key = KEYC_F11,
93 .data = "\033[23~"
95 { .key = KEYC_F12,
96 .data = "\033[24~"
98 { .key = KEYC_IC,
99 .data = "\033[2~"
101 { .key = KEYC_DC,
102 .data = "\033[3~"
104 { .key = KEYC_HOME,
105 .data = "\033[1~"
107 { .key = KEYC_END,
108 .data = "\033[4~"
110 { .key = KEYC_NPAGE,
111 .data = "\033[6~"
113 { .key = KEYC_PPAGE,
114 .data = "\033[5~"
116 { .key = KEYC_BTAB,
117 .data = "\033[Z"
120 /* Arrow keys. */
121 { .key = KEYC_UP|KEYC_CURSOR,
122 .data = "\033OA"
124 { .key = KEYC_DOWN|KEYC_CURSOR,
125 .data = "\033OB"
127 { .key = KEYC_RIGHT|KEYC_CURSOR,
128 .data = "\033OC"
130 { .key = KEYC_LEFT|KEYC_CURSOR,
131 .data = "\033OD"
133 { .key = KEYC_UP,
134 .data = "\033[A"
136 { .key = KEYC_DOWN,
137 .data = "\033[B"
139 { .key = KEYC_RIGHT,
140 .data = "\033[C"
142 { .key = KEYC_LEFT,
143 .data = "\033[D"
146 /* Keypad keys. */
147 { .key = KEYC_KP_SLASH|KEYC_KEYPAD,
148 .data = "\033Oo"
150 { .key = KEYC_KP_STAR|KEYC_KEYPAD,
151 .data = "\033Oj"
153 { .key = KEYC_KP_MINUS|KEYC_KEYPAD,
154 .data = "\033Om"
156 { .key = KEYC_KP_SEVEN|KEYC_KEYPAD,
157 .data = "\033Ow"
159 { .key = KEYC_KP_EIGHT|KEYC_KEYPAD,
160 .data = "\033Ox"
162 { .key = KEYC_KP_NINE|KEYC_KEYPAD,
163 .data = "\033Oy"
165 { .key = KEYC_KP_PLUS|KEYC_KEYPAD,
166 .data = "\033Ok"
168 { .key = KEYC_KP_FOUR|KEYC_KEYPAD,
169 .data = "\033Ot"
171 { .key = KEYC_KP_FIVE|KEYC_KEYPAD,
172 .data = "\033Ou"
174 { .key = KEYC_KP_SIX|KEYC_KEYPAD,
175 .data = "\033Ov"
177 { .key = KEYC_KP_ONE|KEYC_KEYPAD,
178 .data = "\033Oq"
180 { .key = KEYC_KP_TWO|KEYC_KEYPAD,
181 .data = "\033Or"
183 { .key = KEYC_KP_THREE|KEYC_KEYPAD,
184 .data = "\033Os"
186 { .key = KEYC_KP_ENTER|KEYC_KEYPAD,
187 .data = "\033OM"
189 { .key = KEYC_KP_ZERO|KEYC_KEYPAD,
190 .data = "\033Op"
192 { .key = KEYC_KP_PERIOD|KEYC_KEYPAD,
193 .data = "\033On"
195 { .key = KEYC_KP_SLASH,
196 .data = "/"
198 { .key = KEYC_KP_STAR,
199 .data = "*"
201 { .key = KEYC_KP_MINUS,
202 .data = "-"
204 { .key = KEYC_KP_SEVEN,
205 .data = "7"
207 { .key = KEYC_KP_EIGHT,
208 .data = "8"
210 { .key = KEYC_KP_NINE,
211 .data = "9"
213 { .key = KEYC_KP_PLUS,
214 .data = "+"
216 { .key = KEYC_KP_FOUR,
217 .data = "4"
219 { .key = KEYC_KP_FIVE,
220 .data = "5"
222 { .key = KEYC_KP_SIX,
223 .data = "6"
225 { .key = KEYC_KP_ONE,
226 .data = "1"
228 { .key = KEYC_KP_TWO,
229 .data = "2"
231 { .key = KEYC_KP_THREE,
232 .data = "3"
234 { .key = KEYC_KP_ENTER,
235 .data = "\n"
237 { .key = KEYC_KP_ZERO,
238 .data = "0"
240 { .key = KEYC_KP_PERIOD,
241 .data = "."
244 /* Keys with an embedded modifier. */
245 { .key = KEYC_F1|KEYC_BUILD_MODIFIERS,
246 .data = "\033[1;_P"
248 { .key = KEYC_F2|KEYC_BUILD_MODIFIERS,
249 .data = "\033[1;_Q"
251 { .key = KEYC_F3|KEYC_BUILD_MODIFIERS,
252 .data = "\033[1;_R"
254 { .key = KEYC_F4|KEYC_BUILD_MODIFIERS,
255 .data = "\033[1;_S"
257 { .key = KEYC_F5|KEYC_BUILD_MODIFIERS,
258 .data = "\033[15;_~"
260 { .key = KEYC_F6|KEYC_BUILD_MODIFIERS,
261 .data = "\033[17;_~"
263 { .key = KEYC_F7|KEYC_BUILD_MODIFIERS,
264 .data = "\033[18;_~"
266 { .key = KEYC_F8|KEYC_BUILD_MODIFIERS,
267 .data = "\033[19;_~"
269 { .key = KEYC_F9|KEYC_BUILD_MODIFIERS,
270 .data = "\033[20;_~"
272 { .key = KEYC_F10|KEYC_BUILD_MODIFIERS,
273 .data = "\033[21;_~"
275 { .key = KEYC_F11|KEYC_BUILD_MODIFIERS,
276 .data = "\033[23;_~"
278 { .key = KEYC_F12|KEYC_BUILD_MODIFIERS,
279 .data = "\033[24;_~"
281 { .key = KEYC_UP|KEYC_BUILD_MODIFIERS,
282 .data = "\033[1;_A"
284 { .key = KEYC_DOWN|KEYC_BUILD_MODIFIERS,
285 .data = "\033[1;_B"
287 { .key = KEYC_RIGHT|KEYC_BUILD_MODIFIERS,
288 .data = "\033[1;_C"
290 { .key = KEYC_LEFT|KEYC_BUILD_MODIFIERS,
291 .data = "\033[1;_D"
293 { .key = KEYC_HOME|KEYC_BUILD_MODIFIERS,
294 .data = "\033[1;_H"
296 { .key = KEYC_END|KEYC_BUILD_MODIFIERS,
297 .data = "\033[1;_F"
299 { .key = KEYC_PPAGE|KEYC_BUILD_MODIFIERS,
300 .data = "\033[5;_~"
302 { .key = KEYC_NPAGE|KEYC_BUILD_MODIFIERS,
303 .data = "\033[6;_~"
305 { .key = KEYC_IC|KEYC_BUILD_MODIFIERS,
306 .data = "\033[2;_~"
308 { .key = KEYC_DC|KEYC_BUILD_MODIFIERS,
309 .data = "\033[3;_~"
312 static const key_code input_key_modifiers[] = {
315 KEYC_SHIFT,
316 KEYC_META|KEYC_IMPLIED_META,
317 KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META,
318 KEYC_CTRL,
319 KEYC_SHIFT|KEYC_CTRL,
320 KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL,
321 KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL
324 /* Input key comparison function. */
325 static int
326 input_key_cmp(struct input_key_entry *ike1, struct input_key_entry *ike2)
328 if (ike1->key < ike2->key)
329 return (-1);
330 if (ike1->key > ike2->key)
331 return (1);
332 return (0);
335 /* Look for key in tree. */
336 static struct input_key_entry *
337 input_key_get(key_code key)
339 struct input_key_entry entry = { .key = key };
341 return (RB_FIND(input_key_tree, &input_key_tree, &entry));
344 /* Split a character into two UTF-8 bytes. */
345 static size_t
346 input_key_split2(u_int c, u_char *dst)
348 if (c > 0x7f) {
349 dst[0] = (c >> 6) | 0xc0;
350 dst[1] = (c & 0x3f) | 0x80;
351 return (2);
353 dst[0] = c;
354 return (1);
357 /* Build input key tree. */
358 void
359 input_key_build(void)
361 struct input_key_entry *ike, *new;
362 u_int i, j;
363 char *data;
364 key_code key;
366 for (i = 0; i < nitems(input_key_defaults); i++) {
367 ike = &input_key_defaults[i];
368 if (~ike->key & KEYC_BUILD_MODIFIERS) {
369 RB_INSERT(input_key_tree, &input_key_tree, ike);
370 continue;
373 for (j = 2; j < nitems(input_key_modifiers); j++) {
374 key = (ike->key & ~KEYC_BUILD_MODIFIERS);
375 data = xstrdup(ike->data);
376 data[strcspn(data, "_")] = '0' + j;
378 new = xcalloc(1, sizeof *new);
379 new->key = key|input_key_modifiers[j];
380 new->data = data;
381 RB_INSERT(input_key_tree, &input_key_tree, new);
385 RB_FOREACH(ike, input_key_tree, &input_key_tree) {
386 log_debug("%s: 0x%llx (%s) is %s", __func__, ike->key,
387 key_string_lookup_key(ike->key, 1), ike->data);
391 /* Translate a key code into an output key sequence for a pane. */
393 input_key_pane(struct window_pane *wp, key_code key, struct mouse_event *m)
395 if (log_get_level() != 0) {
396 log_debug("writing key 0x%llx (%s) to %%%u", key,
397 key_string_lookup_key(key, 1), wp->id);
400 if (KEYC_IS_MOUSE(key)) {
401 if (m != NULL && m->wp != -1 && (u_int)m->wp == wp->id)
402 input_key_mouse(wp, m);
403 return (0);
405 return (input_key(wp->screen, wp->event, key));
408 static void
409 input_key_write(const char *from, struct bufferevent *bev, const char *data,
410 size_t size)
412 log_debug("%s: %.*s", from, (int)size, data);
413 bufferevent_write(bev, data, size);
416 /* Translate a key code into an output key sequence. */
418 input_key(struct screen *s, struct bufferevent *bev, key_code key)
420 struct input_key_entry *ike;
421 key_code justkey, newkey, outkey, modifiers;
422 struct utf8_data ud;
423 char tmp[64], modifier;
425 /* Mouse keys need a pane. */
426 if (KEYC_IS_MOUSE(key))
427 return (0);
429 /* Literal keys go as themselves (can't be more than eight bits). */
430 if (key & KEYC_LITERAL) {
431 ud.data[0] = (u_char)key;
432 input_key_write(__func__, bev, &ud.data[0], 1);
433 return (0);
436 /* Is this backspace? */
437 if ((key & KEYC_MASK_KEY) == KEYC_BSPACE) {
438 newkey = options_get_number(global_options, "backspace");
439 if (newkey >= 0x7f)
440 newkey = '\177';
441 key = newkey|(key & (KEYC_MASK_MODIFIERS|KEYC_MASK_FLAGS));
445 * If this is a normal 7-bit key, just send it, with a leading escape
446 * if necessary. If it is a UTF-8 key, split it and send it.
448 justkey = (key & ~(KEYC_META|KEYC_IMPLIED_META));
449 if (justkey <= 0x7f) {
450 if (key & KEYC_META)
451 input_key_write(__func__, bev, "\033", 1);
452 ud.data[0] = justkey;
453 input_key_write(__func__, bev, &ud.data[0], 1);
454 return (0);
456 if (KEYC_IS_UNICODE(justkey)) {
457 if (key & KEYC_META)
458 input_key_write(__func__, bev, "\033", 1);
459 utf8_to_data(justkey, &ud);
460 input_key_write(__func__, bev, ud.data, ud.size);
461 return (0);
465 * Look up in the tree. If not in application keypad or cursor mode,
466 * remove the flags from the key.
468 if (~s->mode & MODE_KKEYPAD)
469 key &= ~KEYC_KEYPAD;
470 if (~s->mode & MODE_KCURSOR)
471 key &= ~KEYC_CURSOR;
472 ike = input_key_get(key);
473 if (ike == NULL && (key & KEYC_META) && (~key & KEYC_IMPLIED_META))
474 ike = input_key_get(key & ~KEYC_META);
475 if (ike == NULL && (key & KEYC_CURSOR))
476 ike = input_key_get(key & ~KEYC_CURSOR);
477 if (ike == NULL && (key & KEYC_KEYPAD))
478 ike = input_key_get(key & ~KEYC_KEYPAD);
479 if (ike != NULL) {
480 log_debug("found key 0x%llx: \"%s\"", key, ike->data);
481 if ((key & KEYC_META) && (~key & KEYC_IMPLIED_META))
482 input_key_write(__func__, bev, "\033", 1);
483 input_key_write(__func__, bev, ike->data, strlen(ike->data));
484 return (0);
487 /* No builtin key sequence; construct an extended key sequence. */
488 if (~s->mode & MODE_KEXTENDED) {
489 if ((key & KEYC_MASK_MODIFIERS) != KEYC_CTRL)
490 goto missing;
491 justkey = (key & KEYC_MASK_KEY);
492 switch (justkey) {
493 case ' ':
494 case '2':
495 key = 0|(key & ~KEYC_MASK_KEY);
496 break;
497 case '|':
498 key = 28|(key & ~KEYC_MASK_KEY);
499 break;
500 case '6':
501 key = 30|(key & ~KEYC_MASK_KEY);
502 break;
503 case '-':
504 case '/':
505 key = 31|(key & ~KEYC_MASK_KEY);
506 break;
507 case '?':
508 key = 127|(key & ~KEYC_MASK_KEY);
509 break;
510 default:
511 if (justkey >= 'A' && justkey <= '_')
512 key = (justkey - 'A')|(key & ~KEYC_MASK_KEY);
513 else if (justkey >= 'a' && justkey <= '~')
514 key = (justkey - 96)|(key & ~KEYC_MASK_KEY);
515 else
516 return (0);
517 break;
519 return (input_key(s, bev, key & ~KEYC_CTRL));
521 outkey = (key & KEYC_MASK_KEY);
522 modifiers = (key & KEYC_MASK_MODIFIERS);
523 if (outkey < 32 && outkey != 9 && outkey != 13 && outkey != 27) {
524 outkey = 64 + outkey;
525 modifiers |= KEYC_CTRL;
527 switch (modifiers) {
528 case KEYC_SHIFT:
529 modifier = '2';
530 break;
531 case KEYC_META:
532 modifier = '3';
533 break;
534 case KEYC_SHIFT|KEYC_META:
535 modifier = '4';
536 break;
537 case KEYC_CTRL:
538 modifier = '5';
539 break;
540 case KEYC_SHIFT|KEYC_CTRL:
541 modifier = '6';
542 break;
543 case KEYC_META|KEYC_CTRL:
544 modifier = '7';
545 break;
546 case KEYC_SHIFT|KEYC_META|KEYC_CTRL:
547 modifier = '8';
548 break;
549 default:
550 goto missing;
552 xsnprintf(tmp, sizeof tmp, "\033[%llu;%cu", outkey, modifier);
553 input_key_write(__func__, bev, tmp, strlen(tmp));
554 return (0);
556 missing:
557 log_debug("key 0x%llx missing", key);
558 return (-1);
561 /* Get mouse event string. */
563 input_key_get_mouse(struct screen *s, struct mouse_event *m, u_int x, u_int y,
564 const char **rbuf, size_t *rlen)
566 static char buf[40];
567 size_t len;
569 *rbuf = NULL;
570 *rlen = 0;
572 /* If this pane is not in button or all mode, discard motion events. */
573 if (MOUSE_DRAG(m->b) && (s->mode & MOTION_MOUSE_MODES) == 0)
574 return (0);
575 if ((s->mode & ALL_MOUSE_MODES) == 0)
576 return (0);
579 * If this event is a release event and not in all mode, discard it.
580 * In SGR mode we can tell absolutely because a release is normally
581 * shown by the last character. Without SGR, we check if the last
582 * buttons was also a release.
584 if (m->sgr_type != ' ') {
585 if (MOUSE_DRAG(m->sgr_b) &&
586 MOUSE_RELEASE(m->sgr_b) &&
587 (~s->mode & MODE_MOUSE_ALL))
588 return (0);
589 } else {
590 if (MOUSE_DRAG(m->b) &&
591 MOUSE_RELEASE(m->b) &&
592 MOUSE_RELEASE(m->lb) &&
593 (~s->mode & MODE_MOUSE_ALL))
594 return (0);
598 * Use the SGR (1006) extension only if the application requested it
599 * and the underlying terminal also sent the event in this format (this
600 * is because an old style mouse release event cannot be converted into
601 * the new SGR format, since the released button is unknown). Otherwise
602 * pretend that tmux doesn't speak this extension, and fall back to the
603 * UTF-8 (1005) extension if the application requested, or to the
604 * legacy format.
606 if (m->sgr_type != ' ' && (s->mode & MODE_MOUSE_SGR)) {
607 len = xsnprintf(buf, sizeof buf, "\033[<%u;%u;%u%c",
608 m->sgr_b, x + 1, y + 1, m->sgr_type);
609 } else if (s->mode & MODE_MOUSE_UTF8) {
610 if (m->b > MOUSE_PARAM_UTF8_MAX - MOUSE_PARAM_BTN_OFF ||
611 x > MOUSE_PARAM_UTF8_MAX - MOUSE_PARAM_POS_OFF ||
612 y > MOUSE_PARAM_UTF8_MAX - MOUSE_PARAM_POS_OFF)
613 return (0);
614 len = xsnprintf(buf, sizeof buf, "\033[M");
615 len += input_key_split2(m->b + MOUSE_PARAM_BTN_OFF, &buf[len]);
616 len += input_key_split2(x + MOUSE_PARAM_POS_OFF, &buf[len]);
617 len += input_key_split2(y + MOUSE_PARAM_POS_OFF, &buf[len]);
618 } else {
619 if (m->b + MOUSE_PARAM_BTN_OFF > MOUSE_PARAM_MAX)
620 return (0);
622 len = xsnprintf(buf, sizeof buf, "\033[M");
623 buf[len++] = m->b + MOUSE_PARAM_BTN_OFF;
626 * The incoming x and y may be out of the range which can be
627 * supported by the "normal" mouse protocol. Clamp the
628 * coordinates to the supported range.
630 if (x + MOUSE_PARAM_POS_OFF > MOUSE_PARAM_MAX)
631 buf[len++] = MOUSE_PARAM_MAX;
632 else
633 buf[len++] = x + MOUSE_PARAM_POS_OFF;
634 if (y + MOUSE_PARAM_POS_OFF > MOUSE_PARAM_MAX)
635 buf[len++] = MOUSE_PARAM_MAX;
636 else
637 buf[len++] = y + MOUSE_PARAM_POS_OFF;
640 *rbuf = buf;
641 *rlen = len;
642 return (1);
645 /* Translate mouse and output. */
646 static void
647 input_key_mouse(struct window_pane *wp, struct mouse_event *m)
649 struct screen *s = wp->screen;
650 u_int x, y;
651 const char *buf;
652 size_t len;
654 /* Ignore events if no mouse mode or the pane is not visible. */
655 if (m->ignore || (s->mode & ALL_MOUSE_MODES) == 0)
656 return;
657 if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
658 return;
659 if (!window_pane_visible(wp))
660 return;
661 if (!input_key_get_mouse(s, m, x, y, &buf, &len))
662 return;
663 log_debug("writing mouse %.*s to %%%u", (int)len, buf, wp->id);
664 input_key_write(__func__, wp->event, buf, len);