Annotation of src/usr.bin/tmux/input-keys.c, Revision 1.51
1.51 ! nicm 1: /* $OpenBSD: input-keys.c,v 1.50 2015/11/23 20:53:09 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
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.
9: *
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.
17: */
18:
19: #include <sys/types.h>
20:
21: #include <stdint.h>
22: #include <stdlib.h>
23: #include <string.h>
24:
25: #include "tmux.h"
26:
1.8 nicm 27: /*
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).
32: */
33:
1.42 nicm 34: void input_key_mouse(struct window_pane *, struct mouse_event *);
35:
1.1 nicm 36: struct input_key_ent {
1.46 nicm 37: key_code key;
1.1 nicm 38: const char *data;
39:
40: int flags;
41: #define INPUTKEY_KEYPAD 0x1 /* keypad key */
42: #define INPUTKEY_CURSOR 0x2 /* cursor key */
43: };
44:
1.20 nicm 45: const struct input_key_ent input_keys[] = {
1.3 nicm 46: /* Backspace key. */
1.7 nicm 47: { KEYC_BSPACE, "\177", 0 },
1.3 nicm 48:
1.1 nicm 49: /* Function keys. */
1.9 nicm 50: { KEYC_F1, "\033OP", 0 },
51: { KEYC_F2, "\033OQ", 0 },
52: { KEYC_F3, "\033OR", 0 },
53: { KEYC_F4, "\033OS", 0 },
54: { KEYC_F5, "\033[15~", 0 },
55: { KEYC_F6, "\033[17~", 0 },
56: { KEYC_F7, "\033[18~", 0 },
57: { KEYC_F8, "\033[19~", 0 },
58: { KEYC_F9, "\033[20~", 0 },
59: { KEYC_F10, "\033[21~", 0 },
60: { KEYC_F11, "\033[23~", 0 },
61: { KEYC_F12, "\033[24~", 0 },
1.38 nicm 62: { KEYC_F1|KEYC_SHIFT, "\033[25~", 0 },
63: { KEYC_F2|KEYC_SHIFT, "\033[26~", 0 },
64: { KEYC_F3|KEYC_SHIFT, "\033[28~", 0 },
65: { KEYC_F4|KEYC_SHIFT, "\033[29~", 0 },
66: { KEYC_F5|KEYC_SHIFT, "\033[31~", 0 },
67: { KEYC_F6|KEYC_SHIFT, "\033[32~", 0 },
68: { KEYC_F7|KEYC_SHIFT, "\033[33~", 0 },
69: { KEYC_F8|KEYC_SHIFT, "\033[34~", 0 },
1.9 nicm 70: { KEYC_IC, "\033[2~", 0 },
71: { KEYC_DC, "\033[3~", 0 },
72: { KEYC_HOME, "\033[1~", 0 },
73: { KEYC_END, "\033[4~", 0 },
74: { KEYC_NPAGE, "\033[6~", 0 },
75: { KEYC_PPAGE, "\033[5~", 0 },
76: { KEYC_BTAB, "\033[Z", 0 },
1.7 nicm 77:
1.13 nicm 78: /*
79: * Arrow keys. Cursor versions must come first. The codes are toggled
80: * between CSI and SS3 versions when ctrl is pressed.
81: */
1.10 nicm 82: { KEYC_UP|KEYC_CTRL, "\033[A", INPUTKEY_CURSOR },
83: { KEYC_DOWN|KEYC_CTRL, "\033[B", INPUTKEY_CURSOR },
84: { KEYC_RIGHT|KEYC_CTRL, "\033[C", INPUTKEY_CURSOR },
85: { KEYC_LEFT|KEYC_CTRL, "\033[D", INPUTKEY_CURSOR },
1.18 nicm 86:
1.7 nicm 87: { KEYC_UP, "\033OA", INPUTKEY_CURSOR },
88: { KEYC_DOWN, "\033OB", INPUTKEY_CURSOR },
89: { KEYC_RIGHT, "\033OC", INPUTKEY_CURSOR },
90: { KEYC_LEFT, "\033OD", INPUTKEY_CURSOR },
91:
1.9 nicm 92: { KEYC_UP|KEYC_CTRL, "\033OA", 0 },
93: { KEYC_DOWN|KEYC_CTRL, "\033OB", 0 },
94: { KEYC_RIGHT|KEYC_CTRL, "\033OC", 0 },
95: { KEYC_LEFT|KEYC_CTRL, "\033OD", 0 },
96:
1.7 nicm 97: { KEYC_UP, "\033[A", 0 },
98: { KEYC_DOWN, "\033[B", 0 },
99: { KEYC_RIGHT, "\033[C", 0 },
100: { KEYC_LEFT, "\033[D", 0 },
101:
1.8 nicm 102: /* Keypad keys. Keypad versions must come first. */
1.17 nicm 103: { KEYC_KP_SLASH, "\033Oo", INPUTKEY_KEYPAD },
104: { KEYC_KP_STAR, "\033Oj", INPUTKEY_KEYPAD },
105: { KEYC_KP_MINUS, "\033Om", INPUTKEY_KEYPAD },
106: { KEYC_KP_SEVEN, "\033Ow", INPUTKEY_KEYPAD },
107: { KEYC_KP_EIGHT, "\033Ox", INPUTKEY_KEYPAD },
108: { KEYC_KP_NINE, "\033Oy", INPUTKEY_KEYPAD },
109: { KEYC_KP_PLUS, "\033Ok", INPUTKEY_KEYPAD },
110: { KEYC_KP_FOUR, "\033Ot", INPUTKEY_KEYPAD },
111: { KEYC_KP_FIVE, "\033Ou", INPUTKEY_KEYPAD },
112: { KEYC_KP_SIX, "\033Ov", INPUTKEY_KEYPAD },
113: { KEYC_KP_ONE, "\033Oq", INPUTKEY_KEYPAD },
114: { KEYC_KP_TWO, "\033Or", INPUTKEY_KEYPAD },
115: { KEYC_KP_THREE, "\033Os", INPUTKEY_KEYPAD },
116: { KEYC_KP_ENTER, "\033OM", INPUTKEY_KEYPAD },
117: { KEYC_KP_ZERO, "\033Op", INPUTKEY_KEYPAD },
118: { KEYC_KP_PERIOD, "\033On", INPUTKEY_KEYPAD },
119:
120: { KEYC_KP_SLASH, "/", 0 },
121: { KEYC_KP_STAR, "*", 0 },
122: { KEYC_KP_MINUS, "-", 0 },
123: { KEYC_KP_SEVEN, "7", 0 },
124: { KEYC_KP_EIGHT, "8", 0 },
125: { KEYC_KP_NINE, "9", 0 },
126: { KEYC_KP_PLUS, "+", 0 },
1.18 nicm 127: { KEYC_KP_FOUR, "4", 0 },
1.17 nicm 128: { KEYC_KP_FIVE, "5", 0 },
129: { KEYC_KP_SIX, "6", 0 },
130: { KEYC_KP_ONE, "1", 0 },
131: { KEYC_KP_TWO, "2", 0 },
132: { KEYC_KP_THREE, "3", 0 },
133: { KEYC_KP_ENTER, "\n", 0 },
134: { KEYC_KP_ZERO, "0", 0 },
135: { KEYC_KP_PERIOD, ".", 0 },
1.1 nicm 136: };
137:
1.8 nicm 138: /* Translate a key code into an output key sequence. */
1.1 nicm 139: void
1.46 nicm 140: input_key(struct window_pane *wp, key_code key, struct mouse_event *m)
1.1 nicm 141: {
1.46 nicm 142: const struct input_key_ent *ike;
143: u_int i;
144: size_t dlen;
145: char *out;
146: key_code justkey;
1.47 nicm 147: struct utf8_data ud;
1.1 nicm 148:
1.46 nicm 149: log_debug("writing key 0x%llx (%s) to %%%u", key,
1.44 nicm 150: key_string_lookup_key(key), wp->id);
1.42 nicm 151:
152: /* If this is a mouse key, pass off to mouse function. */
153: if (KEYC_IS_MOUSE(key)) {
154: if (m != NULL && m->wp != -1 && (u_int)m->wp == wp->id)
155: input_key_mouse(wp, m);
156: return;
157: }
1.1 nicm 158:
1.8 nicm 159: /*
160: * If this is a normal 7-bit key, just send it, with a leading escape
1.46 nicm 161: * if necessary. If it is a UTF-8 key, split it and send it.
1.8 nicm 162: */
1.46 nicm 163: justkey = (key & ~KEYC_ESCAPE);
1.49 nicm 164: if (key != KEYC_NONE && justkey <= 0x7f) {
1.2 nicm 165: if (key & KEYC_ESCAPE)
1.15 nicm 166: bufferevent_write(wp->event, "\033", 1);
1.47 nicm 167: ud.data[0] = justkey;
168: bufferevent_write(wp->event, &ud.data[0], 1);
1.46 nicm 169: return;
170: }
171: if (key != KEYC_NONE && justkey > 0x7f && justkey < KEYC_BASE) {
1.48 nicm 172: if (utf8_split(justkey, &ud) != UTF8_DONE)
1.46 nicm 173: return;
174: if (key & KEYC_ESCAPE)
175: bufferevent_write(wp->event, "\033", 1);
1.47 nicm 176: bufferevent_write(wp->event, ud.data, ud.size);
1.1 nicm 177: return;
1.12 nicm 178: }
179:
1.18 nicm 180: /*
1.12 nicm 181: * Then try to look this up as an xterm key, if the flag to output them
182: * is set.
183: */
1.45 nicm 184: if (options_get_number(wp->window->options, "xterm-keys")) {
1.12 nicm 185: if ((out = xterm_keys_lookup(key)) != NULL) {
1.14 nicm 186: bufferevent_write(wp->event, out, strlen(out));
1.26 nicm 187: free(out);
1.12 nicm 188: return;
189: }
1.1 nicm 190: }
191:
1.8 nicm 192: /* Otherwise look the key up in the table. */
1.1 nicm 193: for (i = 0; i < nitems(input_keys); i++) {
194: ike = &input_keys[i];
195:
196: if ((ike->flags & INPUTKEY_KEYPAD) &&
197: !(wp->screen->mode & MODE_KKEYPAD))
198: continue;
199: if ((ike->flags & INPUTKEY_CURSOR) &&
200: !(wp->screen->mode & MODE_KCURSOR))
201: continue;
202:
1.2 nicm 203: if ((key & KEYC_ESCAPE) && (ike->key | KEYC_ESCAPE) == key)
1.1 nicm 204: break;
205: if (ike->key == key)
206: break;
207: }
208: if (i == nitems(input_keys)) {
1.46 nicm 209: log_debug("key 0x%llx missing", key);
1.1 nicm 210: return;
211: }
212: dlen = strlen(ike->data);
1.46 nicm 213: log_debug("found key 0x%llx: \"%s\"", key, ike->data);
1.1 nicm 214:
1.9 nicm 215: /* Prefix a \033 for escape. */
1.2 nicm 216: if (key & KEYC_ESCAPE)
1.14 nicm 217: bufferevent_write(wp->event, "\033", 1);
218: bufferevent_write(wp->event, ike->data, dlen);
1.1 nicm 219: }
220:
1.8 nicm 221: /* Translate mouse and output. */
1.1 nicm 222: void
1.42 nicm 223: input_key_mouse(struct window_pane *wp, struct mouse_event *m)
1.1 nicm 224: {
1.42 nicm 225: char buf[40];
226: size_t len;
227: u_int x, y;
228:
229: if ((wp->screen->mode & ALL_MOUSE_MODES) == 0)
230: return;
231: if (!window_pane_visible(wp))
232: return;
233: if (cmd_mouse_at(wp, m, &x, &y, 0) != 0)
1.24 nicm 234: return;
235:
1.42 nicm 236: /* If this pane is not in button mode, discard motion events. */
237: if (!(wp->screen->mode & MODE_MOUSE_BUTTON) && (m->b & MOUSE_MASK_DRAG))
1.40 nicm 238: return;
1.42 nicm 239:
240: /*
241: * Use the SGR (1006) extension only if the application requested it
242: * and the underlying terminal also sent the event in this format (this
243: * is because an old style mouse release event cannot be converted into
244: * the new SGR format, since the released button is unknown). Otherwise
245: * pretend that tmux doesn't speak this extension, and fall back to the
1.51 ! nicm 246: * UTF-8 (1005) extension if the application requested, or to the
1.42 nicm 247: * legacy format.
248: */
249: if (m->sgr_type != ' ' && (wp->screen->mode & MODE_MOUSE_SGR)) {
250: len = xsnprintf(buf, sizeof buf, "\033[<%u;%u;%u%c",
251: m->sgr_b, x + 1, y + 1, m->sgr_type);
1.51 ! nicm 252: } else if (wp->screen->mode & MODE_MOUSE_UTF8) {
! 253: len = xsnprintf(buf, sizeof buf, "\033[M");
! 254: len += utf8_split2(m->b + 32, &buf[len]);
! 255: len += utf8_split2(x + 33, &buf[len]);
! 256: len += utf8_split2(y + 33, &buf[len]);
1.42 nicm 257: } else {
258: if (m->b > 223)
259: return;
260: len = xsnprintf(buf, sizeof buf, "\033[M");
261: buf[len++] = m->b + 32;
262: buf[len++] = x + 33;
263: buf[len++] = y + 33;
1.1 nicm 264: }
1.44 nicm 265: log_debug("writing mouse %.*s to %%%u", (int)len, buf, wp->id);
1.42 nicm 266: bufferevent_write(wp->event, buf, len);
1.1 nicm 267: }