2014-11-08 12:27:43 +00:00
|
|
|
/* $OpenBSD$ */
|
2007-09-28 22:47:22 +00:00
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2007-09-28 22:47:22 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-10-28 22:54:54 +00:00
|
|
|
/*
|
|
|
|
* This file is rather misleadingly named, it contains the code which takes a
|
|
|
|
* key code and translates it into something suitable to be sent to the
|
|
|
|
* application running in a pane (similar to input.c does in the other
|
|
|
|
* direction with output).
|
|
|
|
*/
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static void input_key_mouse(struct window_pane *, struct mouse_event *);
|
2015-04-19 21:34:21 +00:00
|
|
|
|
2020-05-15 09:31:54 +00:00
|
|
|
/* Entry in the key tree. */
|
|
|
|
struct input_key_entry {
|
|
|
|
key_code key;
|
|
|
|
const char *data;
|
2009-01-09 16:45:58 +00:00
|
|
|
|
2020-05-15 09:31:54 +00:00
|
|
|
RB_ENTRY(input_key_entry) entry;
|
2009-01-09 16:45:58 +00:00
|
|
|
};
|
2020-05-15 09:31:54 +00:00
|
|
|
RB_HEAD(input_key_tree, input_key_entry);
|
2009-01-09 16:45:58 +00:00
|
|
|
|
2020-05-15 09:31:54 +00:00
|
|
|
/* Tree of input keys. */
|
|
|
|
static int input_key_cmp(struct input_key_entry *,
|
|
|
|
struct input_key_entry *);
|
|
|
|
RB_GENERATE_STATIC(input_key_tree, input_key_entry, entry, input_key_cmp);
|
|
|
|
struct input_key_tree input_key_tree = RB_INITIALIZER(&input_key_tree);
|
|
|
|
|
|
|
|
/* List of default keys, the tree is built from this. */
|
|
|
|
static struct input_key_entry input_key_defaults[] = {
|
2017-06-12 07:04:24 +00:00
|
|
|
/* Paste keys. */
|
2020-05-15 09:31:54 +00:00
|
|
|
{ .key = KEYC_PASTE_START,
|
|
|
|
.data = "\033[200~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_PASTE_END,
|
|
|
|
.data = "\033[201~"
|
|
|
|
},
|
2017-06-12 07:04:24 +00:00
|
|
|
|
2009-01-08 22:28:02 +00:00
|
|
|
/* Function keys. */
|
2020-05-15 09:31:54 +00:00
|
|
|
{ .key = KEYC_F1,
|
|
|
|
.data = "\033OP"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_F2,
|
|
|
|
.data = "\033OQ"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_F3,
|
|
|
|
.data = "\033OR"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_F4,
|
|
|
|
.data = "\033OS"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_F5,
|
|
|
|
.data = "\033[15~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_F6,
|
|
|
|
.data = "\033[17~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_F7,
|
|
|
|
.data = "\033[18~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_F8,
|
|
|
|
.data = "\033[19~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_F9,
|
|
|
|
.data = "\033[20~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_F10,
|
|
|
|
.data = "\033[21~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_F11,
|
|
|
|
.data = "\033[23~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_F12,
|
|
|
|
.data = "\033[24~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_IC,
|
|
|
|
.data = "\033[2~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_DC,
|
|
|
|
.data = "\033[3~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_HOME,
|
|
|
|
.data = "\033[1~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_END,
|
|
|
|
.data = "\033[4~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_NPAGE,
|
|
|
|
.data = "\033[6~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_PPAGE,
|
|
|
|
.data = "\033[5~"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_BTAB,
|
|
|
|
.data = "\033[Z"
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Arrow keys. */
|
|
|
|
{ .key = KEYC_UP|KEYC_CURSOR,
|
|
|
|
.data = "\033OA"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_DOWN|KEYC_CURSOR,
|
|
|
|
.data = "\033OB"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_RIGHT|KEYC_CURSOR,
|
|
|
|
.data = "\033OC"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_LEFT|KEYC_CURSOR,
|
|
|
|
.data = "\033OD"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_UP,
|
|
|
|
.data = "\033[A"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_DOWN,
|
|
|
|
.data = "\033[B"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_RIGHT,
|
|
|
|
.data = "\033[C"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_LEFT,
|
|
|
|
.data = "\033[D"
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Keypad keys. */
|
|
|
|
{ .key = KEYC_KP_SLASH|KEYC_KEYPAD,
|
|
|
|
.data = "\033Oo"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_STAR|KEYC_KEYPAD,
|
|
|
|
.data = "\033Oj"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_MINUS|KEYC_KEYPAD,
|
|
|
|
.data = "\033Om"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_SEVEN|KEYC_KEYPAD,
|
|
|
|
.data = "\033Ow"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_EIGHT|KEYC_KEYPAD,
|
|
|
|
.data = "\033Ox"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_NINE|KEYC_KEYPAD,
|
|
|
|
.data = "\033Oy"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_PLUS|KEYC_KEYPAD,
|
|
|
|
.data = "\033Ok"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_FOUR|KEYC_KEYPAD,
|
|
|
|
.data = "\033Ot"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_FIVE|KEYC_KEYPAD,
|
|
|
|
.data = "\033Ou"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_SIX|KEYC_KEYPAD,
|
|
|
|
.data = "\033Ov"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_ONE|KEYC_KEYPAD,
|
|
|
|
.data = "\033Oq"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_TWO|KEYC_KEYPAD,
|
|
|
|
.data = "\033Or"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_THREE|KEYC_KEYPAD,
|
|
|
|
.data = "\033Os"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_ENTER|KEYC_KEYPAD,
|
|
|
|
.data = "\033OM"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_ZERO|KEYC_KEYPAD,
|
|
|
|
.data = "\033Op"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_PERIOD|KEYC_KEYPAD,
|
|
|
|
.data = "\033On"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_SLASH,
|
|
|
|
.data = "/"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_STAR,
|
|
|
|
.data = "*"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_MINUS,
|
|
|
|
.data = "-"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_SEVEN,
|
|
|
|
.data = "7"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_EIGHT,
|
|
|
|
.data = "8"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_NINE,
|
|
|
|
.data = "9"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_PLUS,
|
|
|
|
.data = "+"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_FOUR,
|
|
|
|
.data = "4"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_FIVE,
|
|
|
|
.data = "5"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_SIX,
|
|
|
|
.data = "6"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_ONE,
|
|
|
|
.data = "1"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_TWO,
|
|
|
|
.data = "2"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_THREE,
|
|
|
|
.data = "3"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_ENTER,
|
|
|
|
.data = "\n"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_ZERO,
|
|
|
|
.data = "0"
|
|
|
|
},
|
|
|
|
{ .key = KEYC_KP_PERIOD,
|
|
|
|
.data = "."
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Keys with an embedded modifier. */
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F1|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[1;_P"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F2|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[1;_Q"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F3|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[1;_R"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F4|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[1;_S"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F5|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[15;_~"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F6|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[17;_~"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F7|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[18;_~"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F8|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[19;_~"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F9|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[20;_~"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F10|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[21;_~"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F11|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[23;_~"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_F12|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[24;_~"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_UP|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[1;_A"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_DOWN|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[1;_B"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_RIGHT|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[1;_C"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_LEFT|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[1;_D"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_HOME|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[1;_H"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_END|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[1;_F"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_PPAGE|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[5;_~"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_NPAGE|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[6;_~"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_IC|KEYC_BUILD_MODIFIERS,
|
2020-05-15 09:31:54 +00:00
|
|
|
.data = "\033[2;_~"
|
|
|
|
},
|
2020-05-15 11:16:41 +00:00
|
|
|
{ .key = KEYC_DC|KEYC_BUILD_MODIFIERS,
|
2020-11-17 08:13:35 +00:00
|
|
|
.data = "\033[3;_~"
|
2022-11-01 09:54:13 +00:00
|
|
|
},
|
2007-09-28 22:47:22 +00:00
|
|
|
};
|
2020-05-15 09:31:54 +00:00
|
|
|
static const key_code input_key_modifiers[] = {
|
|
|
|
0,
|
|
|
|
0,
|
2020-05-15 11:16:41 +00:00
|
|
|
KEYC_SHIFT,
|
|
|
|
KEYC_META|KEYC_IMPLIED_META,
|
|
|
|
KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META,
|
|
|
|
KEYC_CTRL,
|
|
|
|
KEYC_SHIFT|KEYC_CTRL,
|
|
|
|
KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL,
|
2020-05-15 14:11:08 +00:00
|
|
|
KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL
|
2020-05-15 09:31:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Input key comparison function. */
|
|
|
|
static int
|
|
|
|
input_key_cmp(struct input_key_entry *ike1, struct input_key_entry *ike2)
|
|
|
|
{
|
|
|
|
if (ike1->key < ike2->key)
|
|
|
|
return (-1);
|
|
|
|
if (ike1->key > ike2->key)
|
|
|
|
return (1);
|
|
|
|
return (0);
|
|
|
|
}
|
2007-09-28 22:47:22 +00:00
|
|
|
|
2020-05-15 11:16:41 +00:00
|
|
|
/* Look for key in tree. */
|
|
|
|
static struct input_key_entry *
|
2021-08-20 17:50:42 +00:00
|
|
|
input_key_get(key_code key)
|
2020-05-15 11:16:41 +00:00
|
|
|
{
|
|
|
|
struct input_key_entry entry = { .key = key };
|
|
|
|
|
|
|
|
return (RB_FIND(input_key_tree, &input_key_tree, &entry));
|
|
|
|
}
|
|
|
|
|
2016-03-01 12:02:08 +00:00
|
|
|
/* Split a character into two UTF-8 bytes. */
|
|
|
|
static size_t
|
2020-05-15 09:31:54 +00:00
|
|
|
input_key_split2(u_int c, u_char *dst)
|
2016-03-01 12:02:08 +00:00
|
|
|
{
|
|
|
|
if (c > 0x7f) {
|
|
|
|
dst[0] = (c >> 6) | 0xc0;
|
|
|
|
dst[1] = (c & 0x3f) | 0x80;
|
|
|
|
return (2);
|
|
|
|
}
|
|
|
|
dst[0] = c;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2020-05-15 09:31:54 +00:00
|
|
|
/* Build input key tree. */
|
|
|
|
void
|
|
|
|
input_key_build(void)
|
|
|
|
{
|
|
|
|
struct input_key_entry *ike, *new;
|
|
|
|
u_int i, j;
|
|
|
|
char *data;
|
2020-05-15 11:16:41 +00:00
|
|
|
key_code key;
|
2020-05-15 09:31:54 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nitems(input_key_defaults); i++) {
|
|
|
|
ike = &input_key_defaults[i];
|
2020-05-15 11:16:41 +00:00
|
|
|
if (~ike->key & KEYC_BUILD_MODIFIERS) {
|
2020-05-15 09:31:54 +00:00
|
|
|
RB_INSERT(input_key_tree, &input_key_tree, ike);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 2; j < nitems(input_key_modifiers); j++) {
|
2020-05-15 11:16:41 +00:00
|
|
|
key = (ike->key & ~KEYC_BUILD_MODIFIERS);
|
2020-05-15 09:31:54 +00:00
|
|
|
data = xstrdup(ike->data);
|
|
|
|
data[strcspn(data, "_")] = '0' + j;
|
|
|
|
|
|
|
|
new = xcalloc(1, sizeof *new);
|
2020-05-15 11:16:41 +00:00
|
|
|
new->key = key|input_key_modifiers[j];
|
2020-05-15 09:31:54 +00:00
|
|
|
new->data = data;
|
|
|
|
RB_INSERT(input_key_tree, &input_key_tree, new);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RB_FOREACH(ike, input_key_tree, &input_key_tree) {
|
|
|
|
log_debug("%s: 0x%llx (%s) is %s", __func__, ike->key,
|
2020-05-15 11:16:41 +00:00
|
|
|
key_string_lookup_key(ike->key, 1), ike->data);
|
2020-05-15 09:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 14:03:48 +00:00
|
|
|
/* Translate a key code into an output key sequence for a pane. */
|
|
|
|
int
|
|
|
|
input_key_pane(struct window_pane *wp, key_code key, struct mouse_event *m)
|
|
|
|
{
|
2020-05-15 09:31:54 +00:00
|
|
|
if (log_get_level() != 0) {
|
|
|
|
log_debug("writing key 0x%llx (%s) to %%%u", key,
|
2020-05-15 11:16:41 +00:00
|
|
|
key_string_lookup_key(key, 1), wp->id);
|
2020-05-15 09:31:54 +00:00
|
|
|
}
|
2020-03-19 14:03:48 +00:00
|
|
|
|
|
|
|
if (KEYC_IS_MOUSE(key)) {
|
|
|
|
if (m != NULL && m->wp != -1 && (u_int)m->wp == wp->id)
|
|
|
|
input_key_mouse(wp, m);
|
|
|
|
return (0);
|
|
|
|
}
|
2020-05-15 09:31:54 +00:00
|
|
|
return (input_key(wp->screen, wp->event, key));
|
2020-03-19 14:03:48 +00:00
|
|
|
}
|
|
|
|
|
2021-04-08 14:16:12 +00:00
|
|
|
static void
|
2021-04-09 07:02:00 +00:00
|
|
|
input_key_write(const char *from, struct bufferevent *bev, const char *data,
|
2021-04-08 14:16:12 +00:00
|
|
|
size_t size)
|
|
|
|
{
|
|
|
|
log_debug("%s: %.*s", from, (int)size, data);
|
|
|
|
bufferevent_write(bev, data, size);
|
|
|
|
}
|
|
|
|
|
2024-08-21 04:55:57 +00:00
|
|
|
/*
|
|
|
|
* Encode and write an extended key escape sequence in one of the two
|
|
|
|
* possible formats, depending on the configured output mode.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
input_key_extended(struct bufferevent *bev, key_code key)
|
|
|
|
{
|
|
|
|
char tmp[64], modifier;
|
|
|
|
struct utf8_data ud;
|
|
|
|
wchar_t wc;
|
|
|
|
|
|
|
|
switch (key & KEYC_MASK_MODIFIERS) {
|
|
|
|
case KEYC_SHIFT:
|
|
|
|
modifier = '2';
|
|
|
|
break;
|
|
|
|
case KEYC_META:
|
|
|
|
modifier = '3';
|
|
|
|
break;
|
|
|
|
case KEYC_SHIFT|KEYC_META:
|
|
|
|
modifier = '4';
|
|
|
|
break;
|
|
|
|
case KEYC_CTRL:
|
|
|
|
modifier = '5';
|
|
|
|
break;
|
|
|
|
case KEYC_SHIFT|KEYC_CTRL:
|
|
|
|
modifier = '6';
|
|
|
|
break;
|
|
|
|
case KEYC_META|KEYC_CTRL:
|
|
|
|
modifier = '7';
|
|
|
|
break;
|
|
|
|
case KEYC_SHIFT|KEYC_META|KEYC_CTRL:
|
|
|
|
modifier = '8';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (KEYC_IS_UNICODE(key)) {
|
|
|
|
utf8_to_data(key & KEYC_MASK_KEY, &ud);
|
|
|
|
if (utf8_towc(&ud, &wc) == UTF8_DONE)
|
|
|
|
key = wc;
|
|
|
|
else
|
|
|
|
return (-1);
|
|
|
|
} else
|
|
|
|
key &= KEYC_MASK_KEY;
|
|
|
|
|
|
|
|
if (options_get_number(global_options, "extended-keys-format") == 1)
|
|
|
|
xsnprintf(tmp, sizeof tmp, "\033[27;%c;%llu~", modifier, key);
|
|
|
|
else
|
|
|
|
xsnprintf(tmp, sizeof tmp, "\033[%llu;%cu", key, modifier);
|
|
|
|
|
|
|
|
input_key_write(__func__, bev, tmp, strlen(tmp));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Outputs the key in the "standard" mode. This is by far the most
|
|
|
|
* complicated output mode, with a lot of remapping in order to
|
|
|
|
* emulate quirks of terminals that today can be only found in museums.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
input_key_vt10x(struct bufferevent *bev, key_code key)
|
|
|
|
{
|
|
|
|
struct utf8_data ud;
|
|
|
|
key_code onlykey;
|
|
|
|
char *p;
|
|
|
|
static const char *standard_map[2] = {
|
|
|
|
"1!9(0)=+;:'\",<.>/-8? 2",
|
|
|
|
"119900=+;;'',,..\x1f\x1f\x7f\x7f\0\0",
|
|
|
|
};
|
|
|
|
|
|
|
|
log_debug("%s: key in %llx", __func__, key);
|
|
|
|
|
|
|
|
if (key & KEYC_META)
|
|
|
|
input_key_write(__func__, bev, "\033", 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There's no way to report modifiers for unicode keys in standard mode
|
|
|
|
* so lose the modifiers.
|
|
|
|
*/
|
|
|
|
if (KEYC_IS_UNICODE(key)) {
|
|
|
|
utf8_to_data(key, &ud);
|
|
|
|
input_key_write(__func__, bev, ud.data, ud.size);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prevent TAB and RET from being swallowed by C0 remapping logic. */
|
2024-08-23 13:25:39 +00:00
|
|
|
onlykey = key & KEYC_MASK_KEY;
|
2024-08-21 04:55:57 +00:00
|
|
|
if (onlykey == '\r' || onlykey == '\t')
|
|
|
|
key &= ~KEYC_CTRL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert keys with Ctrl modifier into corresponding C0 control codes,
|
|
|
|
* with the exception of *some* keys, which are remapped into printable
|
|
|
|
* ASCII characters.
|
|
|
|
*
|
|
|
|
* There is no special handling for Shift modifier, which is pretty
|
|
|
|
* much redundant anyway, as no terminal will send <base key>|SHIFT,
|
|
|
|
* but only <shifted key>|SHIFT.
|
|
|
|
*/
|
|
|
|
if (key & KEYC_CTRL) {
|
|
|
|
p = strchr(standard_map[0], onlykey);
|
|
|
|
if (p != NULL)
|
|
|
|
key = standard_map[1][p - standard_map[0]];
|
|
|
|
else if (onlykey >= '3' && onlykey <= '7')
|
|
|
|
key = onlykey - '\030';
|
|
|
|
else if (onlykey >= '@' && onlykey <= '~')
|
|
|
|
key = onlykey & 0x1f;
|
|
|
|
else
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
log_debug("%s: key out %llx", __func__, key);
|
|
|
|
|
|
|
|
ud.data[0] = key & 0x7f;
|
|
|
|
input_key_write(__func__, bev, &ud.data[0], 1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Pick keys that are reported as vt10x keys in modifyOtherKeys=1 mode. */
|
|
|
|
static int
|
|
|
|
input_key_mode1(struct bufferevent *bev, key_code key)
|
|
|
|
{
|
|
|
|
key_code onlykey;
|
|
|
|
|
|
|
|
log_debug("%s: key in %llx", __func__, key);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As per
|
|
|
|
* https://invisible-island.net/xterm/modified-keys-us-pc105.html.
|
|
|
|
*/
|
|
|
|
onlykey = key & KEYC_MASK_KEY;
|
|
|
|
if ((key & (KEYC_META | KEYC_CTRL)) == KEYC_CTRL &&
|
|
|
|
(onlykey == ' ' ||
|
|
|
|
onlykey == '/' ||
|
|
|
|
onlykey == '@' ||
|
|
|
|
onlykey == '^' ||
|
|
|
|
(onlykey >= '2' && onlykey <= '8') ||
|
|
|
|
(onlykey >= '@' && onlykey <= '~')))
|
|
|
|
return (input_key_vt10x(bev, key));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A regular key + Meta. In the absence of a standard to back this, we
|
|
|
|
* mimic what iTerm 2 does.
|
|
|
|
*/
|
|
|
|
if ((key & (KEYC_CTRL | KEYC_META)) == KEYC_META)
|
|
|
|
return (input_key_vt10x(bev, key));
|
|
|
|
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2009-10-28 22:54:54 +00:00
|
|
|
/* Translate a key code into an output key sequence. */
|
2020-01-13 07:51:54 +00:00
|
|
|
int
|
2020-05-15 09:31:54 +00:00
|
|
|
input_key(struct screen *s, struct bufferevent *bev, key_code key)
|
2007-09-28 22:47:22 +00:00
|
|
|
{
|
2022-11-02 07:36:07 +00:00
|
|
|
struct input_key_entry *ike = NULL;
|
2024-08-21 04:55:57 +00:00
|
|
|
key_code newkey;
|
2020-05-15 09:31:54 +00:00
|
|
|
struct utf8_data ud;
|
2015-11-12 11:05:34 +00:00
|
|
|
|
2020-03-19 14:03:48 +00:00
|
|
|
/* Mouse keys need a pane. */
|
|
|
|
if (KEYC_IS_MOUSE(key))
|
2020-01-13 07:51:54 +00:00
|
|
|
return (0);
|
2008-06-25 07:30:08 +00:00
|
|
|
|
2019-07-09 14:03:12 +00:00
|
|
|
/* Literal keys go as themselves (can't be more than eight bits). */
|
|
|
|
if (key & KEYC_LITERAL) {
|
|
|
|
ud.data[0] = (u_char)key;
|
2021-04-08 14:16:12 +00:00
|
|
|
input_key_write(__func__, bev, &ud.data[0], 1);
|
2020-01-13 07:51:54 +00:00
|
|
|
return (0);
|
2019-07-09 14:03:12 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 07:55:01 +00:00
|
|
|
/* Is this backspace? */
|
|
|
|
if ((key & KEYC_MASK_KEY) == KEYC_BSPACE) {
|
2019-11-18 09:42:09 +00:00
|
|
|
newkey = options_get_number(global_options, "backspace");
|
|
|
|
if (newkey >= 0x7f)
|
|
|
|
newkey = '\177';
|
2020-05-15 11:16:41 +00:00
|
|
|
key = newkey|(key & (KEYC_MASK_MODIFIERS|KEYC_MASK_FLAGS));
|
2019-11-14 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-21 04:55:57 +00:00
|
|
|
/* Is this backtab? */
|
|
|
|
if ((key & KEYC_MASK_KEY) == KEYC_BTAB) {
|
2024-08-23 13:25:39 +00:00
|
|
|
if ((s->mode & EXTENDED_KEY_MODES) != 0) {
|
2024-08-21 04:55:57 +00:00
|
|
|
/* When in xterm extended mode, remap into S-Tab. */
|
|
|
|
key = '\011' | (key & ~KEYC_MASK_KEY) | KEYC_SHIFT;
|
|
|
|
} else {
|
|
|
|
/* Otherwise clear modifiers. */
|
|
|
|
key &= ~KEYC_MASK_MODIFIERS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-28 22:54:54 +00:00
|
|
|
/*
|
2024-08-21 04:55:57 +00:00
|
|
|
* A trivial case, that is a 7-bit key, excluding C0 control characters
|
|
|
|
* that can't be entered from the keyboard, and no modifiers; or a UTF-8
|
|
|
|
* key and no modifiers.
|
2009-10-28 22:54:54 +00:00
|
|
|
*/
|
2024-08-21 04:55:57 +00:00
|
|
|
if (!(key & ~KEYC_MASK_KEY)) {
|
2024-08-26 07:45:05 +00:00
|
|
|
if (key == C0_HT ||
|
|
|
|
key == C0_CR ||
|
|
|
|
key == C0_ESC ||
|
2024-08-21 04:55:57 +00:00
|
|
|
(key >= 0x20 && key <= 0x7f)) {
|
|
|
|
ud.data[0] = key;
|
|
|
|
input_key_write(__func__, bev, &ud.data[0], 1);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (KEYC_IS_UNICODE(key)) {
|
|
|
|
utf8_to_data(key, &ud);
|
|
|
|
input_key_write(__func__, bev, ud.data, ud.size);
|
|
|
|
return (0);
|
|
|
|
}
|
2007-09-28 22:47:22 +00:00
|
|
|
}
|
|
|
|
|
2009-12-04 22:14:47 +00:00
|
|
|
/*
|
2024-08-21 04:55:57 +00:00
|
|
|
* Look up the standard VT10x keys in the tree. If not in application
|
|
|
|
* keypad or cursor mode, remove the respective flags from the key.
|
2009-10-28 23:03:51 +00:00
|
|
|
*/
|
2020-05-15 09:31:54 +00:00
|
|
|
if (~s->mode & MODE_KKEYPAD)
|
|
|
|
key &= ~KEYC_KEYPAD;
|
|
|
|
if (~s->mode & MODE_KCURSOR)
|
|
|
|
key &= ~KEYC_CURSOR;
|
2022-11-02 07:36:07 +00:00
|
|
|
if (ike == NULL)
|
|
|
|
ike = input_key_get(key);
|
2020-05-15 11:16:41 +00:00
|
|
|
if (ike == NULL && (key & KEYC_META) && (~key & KEYC_IMPLIED_META))
|
|
|
|
ike = input_key_get(key & ~KEYC_META);
|
2020-06-01 16:09:35 +00:00
|
|
|
if (ike == NULL && (key & KEYC_CURSOR))
|
|
|
|
ike = input_key_get(key & ~KEYC_CURSOR);
|
|
|
|
if (ike == NULL && (key & KEYC_KEYPAD))
|
|
|
|
ike = input_key_get(key & ~KEYC_KEYPAD);
|
2020-05-15 11:39:37 +00:00
|
|
|
if (ike != NULL) {
|
2024-08-21 04:55:57 +00:00
|
|
|
log_debug("%s: found key 0x%llx: \"%s\"", __func__, key,
|
|
|
|
ike->data);
|
2023-01-12 18:49:11 +00:00
|
|
|
if ((key == KEYC_PASTE_START || key == KEYC_PASTE_END) &&
|
|
|
|
(~s->mode & MODE_BRACKETPASTE))
|
|
|
|
return (0);
|
2020-06-01 16:09:35 +00:00
|
|
|
if ((key & KEYC_META) && (~key & KEYC_IMPLIED_META))
|
2021-04-08 14:16:12 +00:00
|
|
|
input_key_write(__func__, bev, "\033", 1);
|
|
|
|
input_key_write(__func__, bev, ike->data, strlen(ike->data));
|
2020-05-15 11:39:37 +00:00
|
|
|
return (0);
|
2008-06-06 17:20:30 +00:00
|
|
|
}
|
2009-01-09 16:45:58 +00:00
|
|
|
|
2024-08-23 13:25:39 +00:00
|
|
|
/* Ignore internal function key codes. */
|
|
|
|
if ((key >= KEYC_BASE && key < KEYC_BASE_END) ||
|
|
|
|
(key >= KEYC_USER && key < KEYC_USER_END)) {
|
|
|
|
log_debug("%s: ignoring key 0x%llx", __func__, key);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2024-08-21 04:55:57 +00:00
|
|
|
/*
|
|
|
|
* No builtin key sequence; construct an extended key sequence
|
|
|
|
* depending on the client mode.
|
|
|
|
*
|
|
|
|
* If something invalid reaches here, an invalid output may be
|
|
|
|
* produced. For example Ctrl-Shift-2 is invalid (as there's
|
|
|
|
* no way to enter it). The correct form is Ctrl-Shift-@, at
|
|
|
|
* least in US English keyboard layout.
|
|
|
|
*/
|
|
|
|
switch (s->mode & EXTENDED_KEY_MODES) {
|
|
|
|
case MODE_KEYS_EXTENDED_2:
|
|
|
|
/*
|
|
|
|
* The simplest mode to handle - *all* modified keys are
|
|
|
|
* reported in the extended form.
|
|
|
|
*/
|
|
|
|
return (input_key_extended(bev, key));
|
|
|
|
case MODE_KEYS_EXTENDED:
|
|
|
|
/*
|
|
|
|
* Some keys are still reported in standard mode, to maintain
|
|
|
|
* compatibility with applications unaware of extended keys.
|
|
|
|
*/
|
|
|
|
if (input_key_mode1(bev, key) == -1)
|
|
|
|
return (input_key_extended(bev, key));
|
|
|
|
return (0);
|
2020-09-18 11:20:59 +00:00
|
|
|
default:
|
2024-08-21 04:55:57 +00:00
|
|
|
/* The standard mode. */
|
|
|
|
return (input_key_vt10x(bev, key));
|
2020-05-15 11:39:37 +00:00
|
|
|
}
|
2007-09-28 22:47:22 +00:00
|
|
|
}
|
2009-01-28 19:52:21 +00:00
|
|
|
|
2020-04-01 09:05:27 +00:00
|
|
|
/* Get mouse event string. */
|
|
|
|
int
|
|
|
|
input_key_get_mouse(struct screen *s, struct mouse_event *m, u_int x, u_int y,
|
|
|
|
const char **rbuf, size_t *rlen)
|
2009-01-28 19:52:21 +00:00
|
|
|
{
|
2020-04-01 09:05:27 +00:00
|
|
|
static char buf[40];
|
2017-02-01 09:55:07 +00:00
|
|
|
size_t len;
|
2015-04-19 21:34:21 +00:00
|
|
|
|
2020-04-01 09:05:27 +00:00
|
|
|
*rbuf = NULL;
|
|
|
|
*rlen = 0;
|
2012-04-10 09:54:59 +00:00
|
|
|
|
2017-02-01 09:55:07 +00:00
|
|
|
/* If this pane is not in button or all mode, discard motion events. */
|
2020-03-16 06:12:42 +00:00
|
|
|
if (MOUSE_DRAG(m->b) && (s->mode & MOTION_MOUSE_MODES) == 0)
|
2020-04-01 09:05:27 +00:00
|
|
|
return (0);
|
2020-04-07 13:38:30 +00:00
|
|
|
if ((s->mode & ALL_MOUSE_MODES) == 0)
|
|
|
|
return (0);
|
2017-02-01 09:55:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If this event is a release event and not in all mode, discard it.
|
|
|
|
* In SGR mode we can tell absolutely because a release is normally
|
|
|
|
* shown by the last character. Without SGR, we check if the last
|
|
|
|
* buttons was also a release.
|
|
|
|
*/
|
|
|
|
if (m->sgr_type != ' ') {
|
|
|
|
if (MOUSE_DRAG(m->sgr_b) &&
|
2022-02-16 18:55:05 +00:00
|
|
|
MOUSE_RELEASE(m->sgr_b) &&
|
2020-03-16 06:12:42 +00:00
|
|
|
(~s->mode & MODE_MOUSE_ALL))
|
2020-04-01 09:05:27 +00:00
|
|
|
return (0);
|
2017-02-01 09:55:07 +00:00
|
|
|
} else {
|
|
|
|
if (MOUSE_DRAG(m->b) &&
|
2022-02-16 18:55:05 +00:00
|
|
|
MOUSE_RELEASE(m->b) &&
|
|
|
|
MOUSE_RELEASE(m->lb) &&
|
2020-03-16 06:12:42 +00:00
|
|
|
(~s->mode & MODE_MOUSE_ALL))
|
2020-04-01 09:05:27 +00:00
|
|
|
return (0);
|
2017-02-01 09:55:07 +00:00
|
|
|
}
|
2015-04-19 21:34:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Use the SGR (1006) extension only if the application requested it
|
|
|
|
* and the underlying terminal also sent the event in this format (this
|
|
|
|
* is because an old style mouse release event cannot be converted into
|
|
|
|
* the new SGR format, since the released button is unknown). Otherwise
|
|
|
|
* pretend that tmux doesn't speak this extension, and fall back to the
|
2015-11-23 23:47:57 +00:00
|
|
|
* UTF-8 (1005) extension if the application requested, or to the
|
2015-04-19 21:34:21 +00:00
|
|
|
* legacy format.
|
|
|
|
*/
|
2017-02-01 09:55:07 +00:00
|
|
|
if (m->sgr_type != ' ' && (s->mode & MODE_MOUSE_SGR)) {
|
2015-04-19 21:34:21 +00:00
|
|
|
len = xsnprintf(buf, sizeof buf, "\033[<%u;%u;%u%c",
|
|
|
|
m->sgr_b, x + 1, y + 1, m->sgr_type);
|
2017-02-01 09:55:07 +00:00
|
|
|
} else if (s->mode & MODE_MOUSE_UTF8) {
|
2022-04-27 10:34:08 +00:00
|
|
|
if (m->b > MOUSE_PARAM_UTF8_MAX - MOUSE_PARAM_BTN_OFF ||
|
|
|
|
x > MOUSE_PARAM_UTF8_MAX - MOUSE_PARAM_POS_OFF ||
|
|
|
|
y > MOUSE_PARAM_UTF8_MAX - MOUSE_PARAM_POS_OFF)
|
2020-04-01 09:05:27 +00:00
|
|
|
return (0);
|
2015-11-23 23:47:57 +00:00
|
|
|
len = xsnprintf(buf, sizeof buf, "\033[M");
|
2022-04-27 10:34:08 +00:00
|
|
|
len += input_key_split2(m->b + MOUSE_PARAM_BTN_OFF, &buf[len]);
|
|
|
|
len += input_key_split2(x + MOUSE_PARAM_POS_OFF, &buf[len]);
|
|
|
|
len += input_key_split2(y + MOUSE_PARAM_POS_OFF, &buf[len]);
|
2015-04-19 21:34:21 +00:00
|
|
|
} else {
|
2022-04-27 10:34:08 +00:00
|
|
|
if (m->b + MOUSE_PARAM_BTN_OFF > MOUSE_PARAM_MAX)
|
2020-04-01 09:05:27 +00:00
|
|
|
return (0);
|
2022-04-27 10:34:08 +00:00
|
|
|
|
2015-04-19 21:34:21 +00:00
|
|
|
len = xsnprintf(buf, sizeof buf, "\033[M");
|
2022-04-27 10:34:08 +00:00
|
|
|
buf[len++] = m->b + MOUSE_PARAM_BTN_OFF;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The incoming x and y may be out of the range which can be
|
|
|
|
* supported by the "normal" mouse protocol. Clamp the
|
|
|
|
* coordinates to the supported range.
|
|
|
|
*/
|
|
|
|
if (x + MOUSE_PARAM_POS_OFF > MOUSE_PARAM_MAX)
|
|
|
|
buf[len++] = MOUSE_PARAM_MAX;
|
|
|
|
else
|
|
|
|
buf[len++] = x + MOUSE_PARAM_POS_OFF;
|
|
|
|
if (y + MOUSE_PARAM_POS_OFF > MOUSE_PARAM_MAX)
|
|
|
|
buf[len++] = MOUSE_PARAM_MAX;
|
|
|
|
else
|
|
|
|
buf[len++] = y + MOUSE_PARAM_POS_OFF;
|
2009-01-28 19:52:21 +00:00
|
|
|
}
|
2020-04-01 09:05:27 +00:00
|
|
|
|
|
|
|
*rbuf = buf;
|
|
|
|
*rlen = len;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Translate mouse and output. */
|
|
|
|
static void
|
|
|
|
input_key_mouse(struct window_pane *wp, struct mouse_event *m)
|
|
|
|
{
|
|
|
|
struct screen *s = wp->screen;
|
|
|
|
u_int x, y;
|
|
|
|
const char *buf;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Ignore events if no mouse mode or the pane is not visible. */
|
|
|
|
if (m->ignore || (s->mode & ALL_MOUSE_MODES) == 0)
|
|
|
|
return;
|
|
|
|
if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
|
|
|
|
return;
|
|
|
|
if (!window_pane_visible(wp))
|
|
|
|
return;
|
|
|
|
if (!input_key_get_mouse(s, m, x, y, &buf, &len))
|
|
|
|
return;
|
2015-10-26 17:17:06 +00:00
|
|
|
log_debug("writing mouse %.*s to %%%u", (int)len, buf, wp->id);
|
2021-04-08 14:16:12 +00:00
|
|
|
input_key_write(__func__, wp->event, buf, len);
|
2009-01-28 19:52:21 +00:00
|
|
|
}
|