2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +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 <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-10-26 13:29:24 +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
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
struct input_key_ent {
|
2015-11-12 11:05:34 +00:00
|
|
|
key_code key;
|
2009-06-01 22:58:49 +00:00
|
|
|
const char *data;
|
|
|
|
|
|
|
|
int flags;
|
|
|
|
#define INPUTKEY_KEYPAD 0x1 /* keypad key */
|
|
|
|
#define INPUTKEY_CURSOR 0x2 /* cursor key */
|
|
|
|
};
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static const struct input_key_ent input_keys[] = {
|
2009-07-26 21:42:08 +00:00
|
|
|
/* Backspace key. */
|
2009-10-26 13:22:30 +00:00
|
|
|
{ KEYC_BSPACE, "\177", 0 },
|
2009-07-26 21:42:08 +00:00
|
|
|
|
2017-06-12 07:04:24 +00:00
|
|
|
/* Paste keys. */
|
|
|
|
{ KEYC_PASTE_START, "\033[200~", 0 },
|
|
|
|
{ KEYC_PASTE_END, "\033[201~", 0 },
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
/* Function keys. */
|
2009-10-26 13:34:26 +00:00
|
|
|
{ KEYC_F1, "\033OP", 0 },
|
|
|
|
{ KEYC_F2, "\033OQ", 0 },
|
|
|
|
{ KEYC_F3, "\033OR", 0 },
|
|
|
|
{ KEYC_F4, "\033OS", 0 },
|
|
|
|
{ KEYC_F5, "\033[15~", 0 },
|
|
|
|
{ KEYC_F6, "\033[17~", 0 },
|
|
|
|
{ KEYC_F7, "\033[18~", 0 },
|
|
|
|
{ KEYC_F8, "\033[19~", 0 },
|
|
|
|
{ KEYC_F9, "\033[20~", 0 },
|
|
|
|
{ KEYC_F10, "\033[21~", 0 },
|
|
|
|
{ KEYC_F11, "\033[23~", 0 },
|
|
|
|
{ KEYC_F12, "\033[24~", 0 },
|
2014-07-21 10:25:48 +00:00
|
|
|
{ KEYC_F1|KEYC_SHIFT, "\033[25~", 0 },
|
|
|
|
{ KEYC_F2|KEYC_SHIFT, "\033[26~", 0 },
|
|
|
|
{ KEYC_F3|KEYC_SHIFT, "\033[28~", 0 },
|
|
|
|
{ KEYC_F4|KEYC_SHIFT, "\033[29~", 0 },
|
|
|
|
{ KEYC_F5|KEYC_SHIFT, "\033[31~", 0 },
|
|
|
|
{ KEYC_F6|KEYC_SHIFT, "\033[32~", 0 },
|
|
|
|
{ KEYC_F7|KEYC_SHIFT, "\033[33~", 0 },
|
|
|
|
{ KEYC_F8|KEYC_SHIFT, "\033[34~", 0 },
|
2009-10-26 13:34:26 +00:00
|
|
|
{ KEYC_IC, "\033[2~", 0 },
|
|
|
|
{ KEYC_DC, "\033[3~", 0 },
|
|
|
|
{ KEYC_HOME, "\033[1~", 0 },
|
|
|
|
{ KEYC_END, "\033[4~", 0 },
|
|
|
|
{ KEYC_NPAGE, "\033[6~", 0 },
|
|
|
|
{ KEYC_PPAGE, "\033[5~", 0 },
|
|
|
|
{ KEYC_BTAB, "\033[Z", 0 },
|
|
|
|
|
2009-10-26 17:59:46 +00:00
|
|
|
/*
|
|
|
|
* Arrow keys. Cursor versions must come first. The codes are toggled
|
|
|
|
* between CSI and SS3 versions when ctrl is pressed.
|
|
|
|
*/
|
2009-10-26 14:27:13 +00:00
|
|
|
{ KEYC_UP|KEYC_CTRL, "\033[A", INPUTKEY_CURSOR },
|
|
|
|
{ KEYC_DOWN|KEYC_CTRL, "\033[B", INPUTKEY_CURSOR },
|
|
|
|
{ KEYC_RIGHT|KEYC_CTRL, "\033[C", INPUTKEY_CURSOR },
|
|
|
|
{ KEYC_LEFT|KEYC_CTRL, "\033[D", INPUTKEY_CURSOR },
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-10-26 13:22:30 +00:00
|
|
|
{ KEYC_UP, "\033OA", INPUTKEY_CURSOR },
|
|
|
|
{ KEYC_DOWN, "\033OB", INPUTKEY_CURSOR },
|
|
|
|
{ KEYC_RIGHT, "\033OC", INPUTKEY_CURSOR },
|
|
|
|
{ KEYC_LEFT, "\033OD", INPUTKEY_CURSOR },
|
|
|
|
|
2009-10-26 13:34:26 +00:00
|
|
|
{ KEYC_UP|KEYC_CTRL, "\033OA", 0 },
|
|
|
|
{ KEYC_DOWN|KEYC_CTRL, "\033OB", 0 },
|
|
|
|
{ KEYC_RIGHT|KEYC_CTRL, "\033OC", 0 },
|
|
|
|
{ KEYC_LEFT|KEYC_CTRL, "\033OD", 0 },
|
|
|
|
|
2009-10-26 13:22:30 +00:00
|
|
|
{ KEYC_UP, "\033[A", 0 },
|
|
|
|
{ KEYC_DOWN, "\033[B", 0 },
|
|
|
|
{ KEYC_RIGHT, "\033[C", 0 },
|
|
|
|
{ KEYC_LEFT, "\033[D", 0 },
|
|
|
|
|
2009-10-26 13:29:24 +00:00
|
|
|
/* Keypad keys. Keypad versions must come first. */
|
2009-11-25 12:24:31 +00:00
|
|
|
{ KEYC_KP_SLASH, "\033Oo", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_STAR, "\033Oj", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_MINUS, "\033Om", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_SEVEN, "\033Ow", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_EIGHT, "\033Ox", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_NINE, "\033Oy", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_PLUS, "\033Ok", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_FOUR, "\033Ot", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_FIVE, "\033Ou", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_SIX, "\033Ov", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_ONE, "\033Oq", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_TWO, "\033Or", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_THREE, "\033Os", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_ENTER, "\033OM", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_ZERO, "\033Op", INPUTKEY_KEYPAD },
|
|
|
|
{ KEYC_KP_PERIOD, "\033On", INPUTKEY_KEYPAD },
|
|
|
|
|
|
|
|
{ KEYC_KP_SLASH, "/", 0 },
|
|
|
|
{ KEYC_KP_STAR, "*", 0 },
|
|
|
|
{ KEYC_KP_MINUS, "-", 0 },
|
|
|
|
{ KEYC_KP_SEVEN, "7", 0 },
|
|
|
|
{ KEYC_KP_EIGHT, "8", 0 },
|
|
|
|
{ KEYC_KP_NINE, "9", 0 },
|
|
|
|
{ KEYC_KP_PLUS, "+", 0 },
|
2009-12-03 22:50:09 +00:00
|
|
|
{ KEYC_KP_FOUR, "4", 0 },
|
2009-11-25 12:24:31 +00:00
|
|
|
{ KEYC_KP_FIVE, "5", 0 },
|
|
|
|
{ KEYC_KP_SIX, "6", 0 },
|
|
|
|
{ KEYC_KP_ONE, "1", 0 },
|
|
|
|
{ KEYC_KP_TWO, "2", 0 },
|
|
|
|
{ KEYC_KP_THREE, "3", 0 },
|
|
|
|
{ KEYC_KP_ENTER, "\n", 0 },
|
|
|
|
{ KEYC_KP_ZERO, "0", 0 },
|
|
|
|
{ KEYC_KP_PERIOD, ".", 0 },
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2016-03-01 12:02:08 +00:00
|
|
|
/* Split a character into two UTF-8 bytes. */
|
|
|
|
static size_t
|
|
|
|
input_split2(u_int c, u_char *dst)
|
|
|
|
{
|
|
|
|
if (c > 0x7f) {
|
|
|
|
dst[0] = (c >> 6) | 0xc0;
|
|
|
|
dst[1] = (c & 0x3f) | 0x80;
|
|
|
|
return (2);
|
|
|
|
}
|
|
|
|
dst[0] = c;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2009-10-26 13:29:24 +00:00
|
|
|
/* Translate a key code into an output key sequence. */
|
2009-06-01 22:58:49 +00:00
|
|
|
void
|
2015-11-12 11:05:34 +00:00
|
|
|
input_key(struct window_pane *wp, key_code key, struct mouse_event *m)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2015-11-12 11:05:34 +00:00
|
|
|
const struct input_key_ent *ike;
|
|
|
|
u_int i;
|
|
|
|
size_t dlen;
|
|
|
|
char *out;
|
|
|
|
key_code justkey;
|
2015-11-12 22:04:37 +00:00
|
|
|
struct utf8_data ud;
|
2015-11-12 11:05:34 +00:00
|
|
|
|
|
|
|
log_debug("writing key 0x%llx (%s) to %%%u", key,
|
2015-10-26 17:17:06 +00:00
|
|
|
key_string_lookup_key(key), wp->id);
|
2015-04-19 21:34:21 +00:00
|
|
|
|
|
|
|
/* If this is a mouse key, pass off to mouse function. */
|
|
|
|
if (KEYC_IS_MOUSE(key)) {
|
|
|
|
if (m != NULL && m->wp != -1 && (u_int)m->wp == wp->id)
|
|
|
|
input_key_mouse(wp, m);
|
|
|
|
return;
|
|
|
|
}
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-10-26 13:29:24 +00:00
|
|
|
/*
|
|
|
|
* If this is a normal 7-bit key, just send it, with a leading escape
|
2015-11-12 11:05:34 +00:00
|
|
|
* if necessary. If it is a UTF-8 key, split it and send it.
|
2009-10-26 13:29:24 +00:00
|
|
|
*/
|
2017-06-28 11:36:39 +00:00
|
|
|
justkey = (key & ~(KEYC_XTERM|KEYC_ESCAPE));
|
2015-12-12 18:19:00 +00:00
|
|
|
if (justkey <= 0x7f) {
|
2015-11-12 11:05:34 +00:00
|
|
|
if (key & KEYC_ESCAPE)
|
|
|
|
bufferevent_write(wp->event, "\033", 1);
|
2015-11-12 22:04:37 +00:00
|
|
|
ud.data[0] = justkey;
|
|
|
|
bufferevent_write(wp->event, &ud.data[0], 1);
|
2015-11-12 11:05:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-12-12 18:19:00 +00:00
|
|
|
if (justkey > 0x7f && justkey < KEYC_BASE) {
|
2015-11-14 11:45:43 +00:00
|
|
|
if (utf8_split(justkey, &ud) != UTF8_DONE)
|
2015-11-12 11:05:34 +00:00
|
|
|
return;
|
2009-07-21 17:57:29 +00:00
|
|
|
if (key & KEYC_ESCAPE)
|
2009-11-04 23:00:22 +00:00
|
|
|
bufferevent_write(wp->event, "\033", 1);
|
2015-11-12 22:04:37 +00:00
|
|
|
bufferevent_write(wp->event, ud.data, ud.size);
|
2009-06-01 22:58:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-12-03 22:50:09 +00:00
|
|
|
/*
|
2009-10-26 17:46:33 +00:00
|
|
|
* Then try to look this up as an xterm key, if the flag to output them
|
|
|
|
* is set.
|
|
|
|
*/
|
2015-10-27 15:58:42 +00:00
|
|
|
if (options_get_number(wp->window->options, "xterm-keys")) {
|
2017-01-25 14:36:08 +00:00
|
|
|
if ((out = xterm_keys_lookup(key)) != NULL) {
|
2009-11-04 22:43:11 +00:00
|
|
|
bufferevent_write(wp->event, out, strlen(out));
|
2012-07-10 11:53:01 +00:00
|
|
|
free(out);
|
2009-10-26 17:46:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-05-07 21:25:59 +00:00
|
|
|
key &= ~KEYC_XTERM;
|
2009-10-26 17:46:33 +00:00
|
|
|
|
2009-10-26 13:29:24 +00:00
|
|
|
/* Otherwise look the key up in the table. */
|
2009-06-01 22:58:49 +00:00
|
|
|
for (i = 0; i < nitems(input_keys); i++) {
|
|
|
|
ike = &input_keys[i];
|
|
|
|
|
2017-01-25 14:36:08 +00:00
|
|
|
if ((ike->flags & INPUTKEY_KEYPAD) &&
|
|
|
|
!(wp->screen->mode & MODE_KKEYPAD))
|
2009-06-01 22:58:49 +00:00
|
|
|
continue;
|
2017-01-25 14:36:08 +00:00
|
|
|
if ((ike->flags & INPUTKEY_CURSOR) &&
|
|
|
|
!(wp->screen->mode & MODE_KCURSOR))
|
2009-06-01 22:58:49 +00:00
|
|
|
continue;
|
|
|
|
|
2009-07-21 17:57:29 +00:00
|
|
|
if ((key & KEYC_ESCAPE) && (ike->key | KEYC_ESCAPE) == key)
|
2009-06-01 22:58:49 +00:00
|
|
|
break;
|
|
|
|
if (ike->key == key)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == nitems(input_keys)) {
|
2015-11-12 11:05:34 +00:00
|
|
|
log_debug("key 0x%llx missing", key);
|
2009-06-01 22:58:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
dlen = strlen(ike->data);
|
2015-11-12 11:05:34 +00:00
|
|
|
log_debug("found key 0x%llx: \"%s\"", key, ike->data);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2009-10-26 13:34:26 +00:00
|
|
|
/* Prefix a \033 for escape. */
|
2009-07-21 17:57:29 +00:00
|
|
|
if (key & KEYC_ESCAPE)
|
2009-11-04 22:43:11 +00:00
|
|
|
bufferevent_write(wp->event, "\033", 1);
|
|
|
|
bufferevent_write(wp->event, ike->data, dlen);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-10-26 13:29:24 +00:00
|
|
|
/* Translate mouse and output. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static void
|
2015-04-19 21:34:21 +00:00
|
|
|
input_key_mouse(struct window_pane *wp, struct mouse_event *m)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2017-02-01 09:55:07 +00:00
|
|
|
struct screen *s = wp->screen;
|
|
|
|
int mode = s->mode;
|
|
|
|
char buf[40];
|
|
|
|
size_t len;
|
|
|
|
u_int x, y;
|
2015-04-19 21:34:21 +00:00
|
|
|
|
2017-02-01 09:55:07 +00:00
|
|
|
if ((mode & ALL_MOUSE_MODES) == 0)
|
2015-04-19 21:34:21 +00:00
|
|
|
return;
|
|
|
|
if (!window_pane_visible(wp))
|
|
|
|
return;
|
|
|
|
if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
|
2012-04-01 20:56:47 +00:00
|
|
|
return;
|
|
|
|
|
2017-02-01 09:55:07 +00:00
|
|
|
/* If this pane is not in button or all mode, discard motion events. */
|
|
|
|
if (MOUSE_DRAG(m->b) &&
|
|
|
|
(mode & (MODE_MOUSE_BUTTON|MODE_MOUSE_ALL)) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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) &&
|
|
|
|
MOUSE_BUTTONS(m->sgr_b) == 3 &&
|
|
|
|
(~mode & MODE_MOUSE_ALL))
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (MOUSE_DRAG(m->b) &&
|
|
|
|
MOUSE_BUTTONS(m->b) == 3 &&
|
|
|
|
MOUSE_BUTTONS(m->lb) == 3 &&
|
|
|
|
(~mode & MODE_MOUSE_ALL))
|
|
|
|
return;
|
|
|
|
}
|
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) {
|
2016-03-02 15:33:36 +00:00
|
|
|
if (m->b > 0x7ff - 32 || x > 0x7ff - 33 || y > 0x7ff - 33)
|
|
|
|
return;
|
2015-11-23 23:47:57 +00:00
|
|
|
len = xsnprintf(buf, sizeof buf, "\033[M");
|
2016-03-01 12:02:08 +00:00
|
|
|
len += input_split2(m->b + 32, &buf[len]);
|
|
|
|
len += input_split2(x + 33, &buf[len]);
|
|
|
|
len += input_split2(y + 33, &buf[len]);
|
2015-04-19 21:34:21 +00:00
|
|
|
} else {
|
|
|
|
if (m->b > 223)
|
|
|
|
return;
|
|
|
|
len = xsnprintf(buf, sizeof buf, "\033[M");
|
|
|
|
buf[len++] = m->b + 32;
|
|
|
|
buf[len++] = x + 33;
|
|
|
|
buf[len++] = y + 33;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2015-10-26 17:17:06 +00:00
|
|
|
log_debug("writing mouse %.*s to %%%u", (int)len, buf, wp->id);
|
2015-04-19 21:34:21 +00:00
|
|
|
bufferevent_write(wp->event, buf, len);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|