Annotation of src/usr.bin/tmux/tty-keys.c, Revision 1.94
1.94 ! nicm 1: /* $OpenBSD: tty-keys.c,v 1.93 2017/02/10 12:59:18 nicm Exp $ */
1.1 nicm 2:
3: /*
1.84 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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: #include <sys/time.h>
21:
1.35 nicm 22: #include <limits.h>
23: #include <stdlib.h>
1.1 nicm 24: #include <string.h>
1.3 nicm 25: #include <termios.h>
26: #include <unistd.h>
1.1 nicm 27:
28: #include "tmux.h"
29:
1.9 nicm 30: /*
1.50 nicm 31: * Handle keys input from the outside terminal. tty_default_*_keys[] are a base
32: * table of supported keys which are looked up in terminfo(5) and translated
33: * into a ternary tree.
1.9 nicm 34: */
35:
1.86 nicm 36: static void tty_keys_add1(struct tty_key **, const char *, key_code);
37: static void tty_keys_add(struct tty *, const char *, key_code);
38: static void tty_keys_free1(struct tty_key *);
39: static struct tty_key *tty_keys_find1(struct tty_key *, const char *, size_t,
1.76 nicm 40: size_t *);
1.86 nicm 41: static struct tty_key *tty_keys_find(struct tty *, const char *, size_t,
42: size_t *);
43: static int tty_keys_next1(struct tty *, const char *, size_t, key_code *,
1.87 nicm 44: size_t *, int);
1.86 nicm 45: static void tty_keys_callback(int, short, void *);
46: static int tty_keys_mouse(struct tty *, const char *, size_t, size_t *);
1.91 nicm 47: static int tty_keys_device_attributes(struct tty *, const char *, size_t,
48: size_t *);
1.1 nicm 49:
1.48 nicm 50: /* Default raw keys. */
51: struct tty_default_key_raw {
1.1 nicm 52: const char *string;
1.76 nicm 53: key_code key;
1.1 nicm 54: };
1.90 nicm 55: static const struct tty_default_key_raw tty_default_raw_keys[] = {
1.1 nicm 56: /*
1.8 nicm 57: * Numeric keypad. Just use the vt100 escape sequences here and always
58: * put the terminal into keypad_xmit mode. Translation of numbers
59: * mode/applications mode is done in input-keys.c.
1.1 nicm 60: */
1.48 nicm 61: { "\033Oo", KEYC_KP_SLASH },
62: { "\033Oj", KEYC_KP_STAR },
63: { "\033Om", KEYC_KP_MINUS },
64: { "\033Ow", KEYC_KP_SEVEN },
65: { "\033Ox", KEYC_KP_EIGHT },
66: { "\033Oy", KEYC_KP_NINE },
67: { "\033Ok", KEYC_KP_PLUS },
68: { "\033Ot", KEYC_KP_FOUR },
69: { "\033Ou", KEYC_KP_FIVE },
70: { "\033Ov", KEYC_KP_SIX },
71: { "\033Oq", KEYC_KP_ONE },
72: { "\033Or", KEYC_KP_TWO },
73: { "\033Os", KEYC_KP_THREE },
74: { "\033OM", KEYC_KP_ENTER },
75: { "\033Op", KEYC_KP_ZERO },
76: { "\033On", KEYC_KP_PERIOD },
1.10 nicm 77:
1.29 nicm 78: /* Arrow keys. */
1.48 nicm 79: { "\033OA", KEYC_UP },
80: { "\033OB", KEYC_DOWN },
81: { "\033OC", KEYC_RIGHT },
82: { "\033OD", KEYC_LEFT },
83:
84: { "\033[A", KEYC_UP },
85: { "\033[B", KEYC_DOWN },
86: { "\033[C", KEYC_RIGHT },
87: { "\033[D", KEYC_LEFT },
1.57 nicm 88:
89: /* Other (xterm) "cursor" keys. */
90: { "\033OH", KEYC_HOME },
91: { "\033OF", KEYC_END },
92:
93: { "\033[H", KEYC_HOME },
94: { "\033[F", KEYC_END },
1.21 nicm 95:
96: /* rxvt-style arrow + modifier keys. */
1.48 nicm 97: { "\033Oa", KEYC_UP|KEYC_CTRL },
98: { "\033Ob", KEYC_DOWN|KEYC_CTRL },
99: { "\033Oc", KEYC_RIGHT|KEYC_CTRL },
100: { "\033Od", KEYC_LEFT|KEYC_CTRL },
101:
102: { "\033[a", KEYC_UP|KEYC_SHIFT },
103: { "\033[b", KEYC_DOWN|KEYC_SHIFT },
104: { "\033[c", KEYC_RIGHT|KEYC_SHIFT },
105: { "\033[d", KEYC_LEFT|KEYC_SHIFT },
106:
107: /* rxvt-style function + modifier keys (C = ^, S = $, C-S = @). */
108: { "\033[11^", KEYC_F1|KEYC_CTRL },
109: { "\033[12^", KEYC_F2|KEYC_CTRL },
110: { "\033[13^", KEYC_F3|KEYC_CTRL },
111: { "\033[14^", KEYC_F4|KEYC_CTRL },
112: { "\033[15^", KEYC_F5|KEYC_CTRL },
113: { "\033[17^", KEYC_F6|KEYC_CTRL },
114: { "\033[18^", KEYC_F7|KEYC_CTRL },
115: { "\033[19^", KEYC_F8|KEYC_CTRL },
116: { "\033[20^", KEYC_F9|KEYC_CTRL },
117: { "\033[21^", KEYC_F10|KEYC_CTRL },
118: { "\033[23^", KEYC_F11|KEYC_CTRL },
119: { "\033[24^", KEYC_F12|KEYC_CTRL },
120: { "\033[2^", KEYC_IC|KEYC_CTRL },
121: { "\033[3^", KEYC_DC|KEYC_CTRL },
122: { "\033[7^", KEYC_HOME|KEYC_CTRL },
123: { "\033[8^", KEYC_END|KEYC_CTRL },
124: { "\033[6^", KEYC_NPAGE|KEYC_CTRL },
125: { "\033[5^", KEYC_PPAGE|KEYC_CTRL },
126:
127: { "\033[11$", KEYC_F1|KEYC_SHIFT },
128: { "\033[12$", KEYC_F2|KEYC_SHIFT },
129: { "\033[13$", KEYC_F3|KEYC_SHIFT },
130: { "\033[14$", KEYC_F4|KEYC_SHIFT },
131: { "\033[15$", KEYC_F5|KEYC_SHIFT },
132: { "\033[17$", KEYC_F6|KEYC_SHIFT },
133: { "\033[18$", KEYC_F7|KEYC_SHIFT },
134: { "\033[19$", KEYC_F8|KEYC_SHIFT },
135: { "\033[20$", KEYC_F9|KEYC_SHIFT },
136: { "\033[21$", KEYC_F10|KEYC_SHIFT },
137: { "\033[23$", KEYC_F11|KEYC_SHIFT },
138: { "\033[24$", KEYC_F12|KEYC_SHIFT },
139: { "\033[2$", KEYC_IC|KEYC_SHIFT },
140: { "\033[3$", KEYC_DC|KEYC_SHIFT },
141: { "\033[7$", KEYC_HOME|KEYC_SHIFT },
142: { "\033[8$", KEYC_END|KEYC_SHIFT },
143: { "\033[6$", KEYC_NPAGE|KEYC_SHIFT },
144: { "\033[5$", KEYC_PPAGE|KEYC_SHIFT },
145:
146: { "\033[11@", KEYC_F1|KEYC_CTRL|KEYC_SHIFT },
147: { "\033[12@", KEYC_F2|KEYC_CTRL|KEYC_SHIFT },
148: { "\033[13@", KEYC_F3|KEYC_CTRL|KEYC_SHIFT },
149: { "\033[14@", KEYC_F4|KEYC_CTRL|KEYC_SHIFT },
150: { "\033[15@", KEYC_F5|KEYC_CTRL|KEYC_SHIFT },
151: { "\033[17@", KEYC_F6|KEYC_CTRL|KEYC_SHIFT },
152: { "\033[18@", KEYC_F7|KEYC_CTRL|KEYC_SHIFT },
153: { "\033[19@", KEYC_F8|KEYC_CTRL|KEYC_SHIFT },
154: { "\033[20@", KEYC_F9|KEYC_CTRL|KEYC_SHIFT },
155: { "\033[21@", KEYC_F10|KEYC_CTRL|KEYC_SHIFT },
156: { "\033[23@", KEYC_F11|KEYC_CTRL|KEYC_SHIFT },
157: { "\033[24@", KEYC_F12|KEYC_CTRL|KEYC_SHIFT },
158: { "\033[2@", KEYC_IC|KEYC_CTRL|KEYC_SHIFT },
159: { "\033[3@", KEYC_DC|KEYC_CTRL|KEYC_SHIFT },
160: { "\033[7@", KEYC_HOME|KEYC_CTRL|KEYC_SHIFT },
161: { "\033[8@", KEYC_END|KEYC_CTRL|KEYC_SHIFT },
162: { "\033[6@", KEYC_NPAGE|KEYC_CTRL|KEYC_SHIFT },
163: { "\033[5@", KEYC_PPAGE|KEYC_CTRL|KEYC_SHIFT },
1.56 nicm 164:
165: /* Focus tracking. */
166: { "\033[I", KEYC_FOCUS_IN },
167: { "\033[O", KEYC_FOCUS_OUT },
1.48 nicm 168: };
1.29 nicm 169:
1.48 nicm 170: /* Default terminfo(5) keys. */
171: struct tty_default_key_code {
172: enum tty_code_code code;
1.76 nicm 173: key_code key;
1.48 nicm 174: };
1.90 nicm 175: static const struct tty_default_key_code tty_default_code_keys[] = {
1.29 nicm 176: /* Function keys. */
1.48 nicm 177: { TTYC_KF1, KEYC_F1 },
178: { TTYC_KF2, KEYC_F2 },
179: { TTYC_KF3, KEYC_F3 },
180: { TTYC_KF4, KEYC_F4 },
181: { TTYC_KF5, KEYC_F5 },
182: { TTYC_KF6, KEYC_F6 },
183: { TTYC_KF7, KEYC_F7 },
184: { TTYC_KF8, KEYC_F8 },
185: { TTYC_KF9, KEYC_F9 },
186: { TTYC_KF10, KEYC_F10 },
187: { TTYC_KF11, KEYC_F11 },
188: { TTYC_KF12, KEYC_F12 },
1.70 nicm 189:
190: { TTYC_KF13, KEYC_F1|KEYC_SHIFT },
191: { TTYC_KF14, KEYC_F2|KEYC_SHIFT },
192: { TTYC_KF15, KEYC_F3|KEYC_SHIFT },
193: { TTYC_KF16, KEYC_F4|KEYC_SHIFT },
194: { TTYC_KF17, KEYC_F5|KEYC_SHIFT },
195: { TTYC_KF18, KEYC_F6|KEYC_SHIFT },
196: { TTYC_KF19, KEYC_F7|KEYC_SHIFT },
197: { TTYC_KF20, KEYC_F8|KEYC_SHIFT },
198: { TTYC_KF21, KEYC_F9|KEYC_SHIFT },
199: { TTYC_KF22, KEYC_F10|KEYC_SHIFT },
200: { TTYC_KF23, KEYC_F11|KEYC_SHIFT },
201: { TTYC_KF24, KEYC_F12|KEYC_SHIFT },
202:
203: { TTYC_KF25, KEYC_F1|KEYC_CTRL },
204: { TTYC_KF26, KEYC_F2|KEYC_CTRL },
205: { TTYC_KF27, KEYC_F3|KEYC_CTRL },
206: { TTYC_KF28, KEYC_F4|KEYC_CTRL },
207: { TTYC_KF29, KEYC_F5|KEYC_CTRL },
208: { TTYC_KF30, KEYC_F6|KEYC_CTRL },
209: { TTYC_KF31, KEYC_F7|KEYC_CTRL },
210: { TTYC_KF32, KEYC_F8|KEYC_CTRL },
211: { TTYC_KF33, KEYC_F9|KEYC_CTRL },
212: { TTYC_KF34, KEYC_F10|KEYC_CTRL },
213: { TTYC_KF35, KEYC_F11|KEYC_CTRL },
214: { TTYC_KF36, KEYC_F12|KEYC_CTRL },
215:
216: { TTYC_KF37, KEYC_F1|KEYC_SHIFT|KEYC_CTRL },
217: { TTYC_KF38, KEYC_F2|KEYC_SHIFT|KEYC_CTRL },
218: { TTYC_KF39, KEYC_F3|KEYC_SHIFT|KEYC_CTRL },
219: { TTYC_KF40, KEYC_F4|KEYC_SHIFT|KEYC_CTRL },
220: { TTYC_KF41, KEYC_F5|KEYC_SHIFT|KEYC_CTRL },
221: { TTYC_KF42, KEYC_F6|KEYC_SHIFT|KEYC_CTRL },
222: { TTYC_KF43, KEYC_F7|KEYC_SHIFT|KEYC_CTRL },
223: { TTYC_KF44, KEYC_F8|KEYC_SHIFT|KEYC_CTRL },
224: { TTYC_KF45, KEYC_F9|KEYC_SHIFT|KEYC_CTRL },
225: { TTYC_KF46, KEYC_F10|KEYC_SHIFT|KEYC_CTRL },
226: { TTYC_KF47, KEYC_F11|KEYC_SHIFT|KEYC_CTRL },
227: { TTYC_KF48, KEYC_F12|KEYC_SHIFT|KEYC_CTRL },
228:
229: { TTYC_KF49, KEYC_F1|KEYC_ESCAPE },
230: { TTYC_KF50, KEYC_F2|KEYC_ESCAPE },
231: { TTYC_KF51, KEYC_F3|KEYC_ESCAPE },
232: { TTYC_KF52, KEYC_F4|KEYC_ESCAPE },
233: { TTYC_KF53, KEYC_F5|KEYC_ESCAPE },
234: { TTYC_KF54, KEYC_F6|KEYC_ESCAPE },
235: { TTYC_KF55, KEYC_F7|KEYC_ESCAPE },
236: { TTYC_KF56, KEYC_F8|KEYC_ESCAPE },
237: { TTYC_KF57, KEYC_F9|KEYC_ESCAPE },
238: { TTYC_KF58, KEYC_F10|KEYC_ESCAPE },
239: { TTYC_KF59, KEYC_F11|KEYC_ESCAPE },
240: { TTYC_KF60, KEYC_F12|KEYC_ESCAPE },
241:
242: { TTYC_KF61, KEYC_F1|KEYC_ESCAPE|KEYC_SHIFT },
243: { TTYC_KF62, KEYC_F2|KEYC_ESCAPE|KEYC_SHIFT },
244: { TTYC_KF63, KEYC_F3|KEYC_ESCAPE|KEYC_SHIFT },
245:
1.48 nicm 246: { TTYC_KICH1, KEYC_IC },
247: { TTYC_KDCH1, KEYC_DC },
248: { TTYC_KHOME, KEYC_HOME },
249: { TTYC_KEND, KEYC_END },
250: { TTYC_KNP, KEYC_NPAGE },
251: { TTYC_KPP, KEYC_PPAGE },
252: { TTYC_KCBT, KEYC_BTAB },
1.29 nicm 253:
254: /* Arrow keys from terminfo. */
1.48 nicm 255: { TTYC_KCUU1, KEYC_UP },
256: { TTYC_KCUD1, KEYC_DOWN },
257: { TTYC_KCUB1, KEYC_LEFT },
258: { TTYC_KCUF1, KEYC_RIGHT },
1.29 nicm 259:
260: /* Key and modifier capabilities. */
1.48 nicm 261: { TTYC_KDC2, KEYC_DC|KEYC_SHIFT },
262: { TTYC_KDC3, KEYC_DC|KEYC_ESCAPE },
263: { TTYC_KDC4, KEYC_DC|KEYC_SHIFT|KEYC_ESCAPE },
264: { TTYC_KDC5, KEYC_DC|KEYC_CTRL },
265: { TTYC_KDC6, KEYC_DC|KEYC_SHIFT|KEYC_CTRL },
266: { TTYC_KDC7, KEYC_DC|KEYC_ESCAPE|KEYC_CTRL },
267: { TTYC_KDN2, KEYC_DOWN|KEYC_SHIFT },
268: { TTYC_KDN3, KEYC_DOWN|KEYC_ESCAPE },
269: { TTYC_KDN4, KEYC_DOWN|KEYC_SHIFT|KEYC_ESCAPE },
270: { TTYC_KDN5, KEYC_DOWN|KEYC_CTRL },
271: { TTYC_KDN6, KEYC_DOWN|KEYC_SHIFT|KEYC_CTRL },
272: { TTYC_KDN7, KEYC_DOWN|KEYC_ESCAPE|KEYC_CTRL },
273: { TTYC_KEND2, KEYC_END|KEYC_SHIFT },
274: { TTYC_KEND3, KEYC_END|KEYC_ESCAPE },
275: { TTYC_KEND4, KEYC_END|KEYC_SHIFT|KEYC_ESCAPE },
276: { TTYC_KEND5, KEYC_END|KEYC_CTRL },
277: { TTYC_KEND6, KEYC_END|KEYC_SHIFT|KEYC_CTRL },
278: { TTYC_KEND7, KEYC_END|KEYC_ESCAPE|KEYC_CTRL },
279: { TTYC_KHOM2, KEYC_HOME|KEYC_SHIFT },
280: { TTYC_KHOM3, KEYC_HOME|KEYC_ESCAPE },
281: { TTYC_KHOM4, KEYC_HOME|KEYC_SHIFT|KEYC_ESCAPE },
282: { TTYC_KHOM5, KEYC_HOME|KEYC_CTRL },
283: { TTYC_KHOM6, KEYC_HOME|KEYC_SHIFT|KEYC_CTRL },
284: { TTYC_KHOM7, KEYC_HOME|KEYC_ESCAPE|KEYC_CTRL },
285: { TTYC_KIC2, KEYC_IC|KEYC_SHIFT },
286: { TTYC_KIC3, KEYC_IC|KEYC_ESCAPE },
287: { TTYC_KIC4, KEYC_IC|KEYC_SHIFT|KEYC_ESCAPE },
288: { TTYC_KIC5, KEYC_IC|KEYC_CTRL },
289: { TTYC_KIC6, KEYC_IC|KEYC_SHIFT|KEYC_CTRL },
290: { TTYC_KIC7, KEYC_IC|KEYC_ESCAPE|KEYC_CTRL },
291: { TTYC_KLFT2, KEYC_LEFT|KEYC_SHIFT },
292: { TTYC_KLFT3, KEYC_LEFT|KEYC_ESCAPE },
293: { TTYC_KLFT4, KEYC_LEFT|KEYC_SHIFT|KEYC_ESCAPE },
294: { TTYC_KLFT5, KEYC_LEFT|KEYC_CTRL },
295: { TTYC_KLFT6, KEYC_LEFT|KEYC_SHIFT|KEYC_CTRL },
296: { TTYC_KLFT7, KEYC_LEFT|KEYC_ESCAPE|KEYC_CTRL },
297: { TTYC_KNXT2, KEYC_NPAGE|KEYC_SHIFT },
298: { TTYC_KNXT3, KEYC_NPAGE|KEYC_ESCAPE },
299: { TTYC_KNXT4, KEYC_NPAGE|KEYC_SHIFT|KEYC_ESCAPE },
300: { TTYC_KNXT5, KEYC_NPAGE|KEYC_CTRL },
301: { TTYC_KNXT6, KEYC_NPAGE|KEYC_SHIFT|KEYC_CTRL },
302: { TTYC_KNXT7, KEYC_NPAGE|KEYC_ESCAPE|KEYC_CTRL },
303: { TTYC_KPRV2, KEYC_PPAGE|KEYC_SHIFT },
304: { TTYC_KPRV3, KEYC_PPAGE|KEYC_ESCAPE },
305: { TTYC_KPRV4, KEYC_PPAGE|KEYC_SHIFT|KEYC_ESCAPE },
306: { TTYC_KPRV5, KEYC_PPAGE|KEYC_CTRL },
307: { TTYC_KPRV6, KEYC_PPAGE|KEYC_SHIFT|KEYC_CTRL },
308: { TTYC_KPRV7, KEYC_PPAGE|KEYC_ESCAPE|KEYC_CTRL },
309: { TTYC_KRIT2, KEYC_RIGHT|KEYC_SHIFT },
310: { TTYC_KRIT3, KEYC_RIGHT|KEYC_ESCAPE },
311: { TTYC_KRIT4, KEYC_RIGHT|KEYC_SHIFT|KEYC_ESCAPE },
312: { TTYC_KRIT5, KEYC_RIGHT|KEYC_CTRL },
313: { TTYC_KRIT6, KEYC_RIGHT|KEYC_SHIFT|KEYC_CTRL },
314: { TTYC_KRIT7, KEYC_RIGHT|KEYC_ESCAPE|KEYC_CTRL },
315: { TTYC_KUP2, KEYC_UP|KEYC_SHIFT },
316: { TTYC_KUP3, KEYC_UP|KEYC_ESCAPE },
317: { TTYC_KUP4, KEYC_UP|KEYC_SHIFT|KEYC_ESCAPE },
318: { TTYC_KUP5, KEYC_UP|KEYC_CTRL },
319: { TTYC_KUP6, KEYC_UP|KEYC_SHIFT|KEYC_CTRL },
320: { TTYC_KUP7, KEYC_UP|KEYC_ESCAPE|KEYC_CTRL },
1.1 nicm 321: };
322:
1.48 nicm 323: /* Add key to tree. */
1.86 nicm 324: static void
1.76 nicm 325: tty_keys_add(struct tty *tty, const char *s, key_code key)
1.16 nicm 326: {
1.29 nicm 327: struct tty_key *tk;
328: size_t size;
329: const char *keystr;
1.1 nicm 330:
1.29 nicm 331: keystr = key_string_lookup_key(key);
332: if ((tk = tty_keys_find(tty, s, strlen(s), &size)) == NULL) {
1.76 nicm 333: log_debug("new key %s: 0x%llx (%s)", s, key, keystr);
1.16 nicm 334: tty_keys_add1(&tty->key_tree, s, key);
1.29 nicm 335: } else {
1.76 nicm 336: log_debug("replacing key %s: 0x%llx (%s)", s, key, keystr);
1.29 nicm 337: tk->key = key;
1.16 nicm 338: }
1.1 nicm 339: }
340:
1.16 nicm 341: /* Add next node to the tree. */
1.86 nicm 342: static void
1.76 nicm 343: tty_keys_add1(struct tty_key **tkp, const char *s, key_code key)
1.1 nicm 344: {
1.16 nicm 345: struct tty_key *tk;
1.1 nicm 346:
1.16 nicm 347: /* Allocate a tree entry if there isn't one already. */
348: tk = *tkp;
349: if (tk == NULL) {
350: tk = *tkp = xcalloc(1, sizeof *tk);
351: tk->ch = *s;
1.83 nicm 352: tk->key = KEYC_UNKNOWN;
1.16 nicm 353: }
354:
355: /* Find the next entry. */
356: if (*s == tk->ch) {
357: /* Move forward in string. */
358: s++;
359:
360: /* If this is the end of the string, no more is necessary. */
361: if (*s == '\0') {
362: tk->key = key;
363: return;
364: }
365:
366: /* Use the child tree for the next character. */
367: tkp = &tk->next;
1.27 nicm 368: } else {
1.16 nicm 369: if (*s < tk->ch)
370: tkp = &tk->left;
371: else if (*s > tk->ch)
372: tkp = &tk->right;
373: }
374:
375: /* And recurse to add it. */
376: tty_keys_add1(tkp, s, key);
1.1 nicm 377: }
378:
1.16 nicm 379: /* Initialise a key tree from the table. */
1.1 nicm 380: void
1.48 nicm 381: tty_keys_build(struct tty *tty)
1.1 nicm 382: {
1.48 nicm 383: const struct tty_default_key_raw *tdkr;
384: const struct tty_default_key_code *tdkc;
385: u_int i;
386: const char *s;
1.1 nicm 387:
1.48 nicm 388: if (tty->key_tree != NULL)
1.71 nicm 389: tty_keys_free(tty);
1.16 nicm 390: tty->key_tree = NULL;
1.1 nicm 391:
1.48 nicm 392: for (i = 0; i < nitems(tty_default_raw_keys); i++) {
393: tdkr = &tty_default_raw_keys[i];
394:
395: s = tdkr->string;
1.51 nicm 396: if (*s != '\0')
397: tty_keys_add(tty, s, tdkr->key);
1.48 nicm 398: }
399: for (i = 0; i < nitems(tty_default_code_keys); i++) {
400: tdkc = &tty_default_code_keys[i];
401:
402: s = tty_term_string(tty->term, tdkc->code);
1.51 nicm 403: if (*s != '\0')
404: tty_keys_add(tty, s, tdkc->key);
1.1 nicm 405:
406: }
407: }
408:
1.16 nicm 409: /* Free the entire key tree. */
1.1 nicm 410: void
411: tty_keys_free(struct tty *tty)
412: {
1.16 nicm 413: tty_keys_free1(tty->key_tree);
414: }
1.1 nicm 415:
1.16 nicm 416: /* Free a single key. */
1.86 nicm 417: static void
1.16 nicm 418: tty_keys_free1(struct tty_key *tk)
419: {
420: if (tk->next != NULL)
421: tty_keys_free1(tk->next);
422: if (tk->left != NULL)
423: tty_keys_free1(tk->left);
424: if (tk->right != NULL)
425: tty_keys_free1(tk->right);
1.42 nicm 426: free(tk);
1.1 nicm 427: }
428:
1.16 nicm 429: /* Lookup a key in the tree. */
1.86 nicm 430: static struct tty_key *
1.16 nicm 431: tty_keys_find(struct tty *tty, const char *buf, size_t len, size_t *size)
1.1 nicm 432: {
1.16 nicm 433: *size = 0;
434: return (tty_keys_find1(tty->key_tree, buf, len, size));
435: }
1.1 nicm 436:
1.16 nicm 437: /* Find the next node. */
1.86 nicm 438: static struct tty_key *
1.16 nicm 439: tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size)
440: {
441: /* If the node is NULL, this is the end of the tree. No match. */
442: if (tk == NULL)
1.1 nicm 443: return (NULL);
444:
1.16 nicm 445: /* Pick the next in the sequence. */
446: if (tk->ch == *buf) {
447: /* Move forward in the string. */
448: buf++; len--;
449: (*size)++;
1.1 nicm 450:
1.16 nicm 451: /* At the end of the string, return the current node. */
1.83 nicm 452: if (len == 0 || (tk->next == NULL && tk->key != KEYC_UNKNOWN))
1.16 nicm 453: return (tk);
1.1 nicm 454:
1.16 nicm 455: /* Move into the next tree for the following character. */
456: tk = tk->next;
457: } else {
458: if (*buf < tk->ch)
459: tk = tk->left;
460: else if (*buf > tk->ch)
461: tk = tk->right;
1.1 nicm 462: }
463:
1.16 nicm 464: /* Move to the next in the tree. */
465: return (tty_keys_find1(tk, buf, len, size));
1.1 nicm 466: }
467:
1.86 nicm 468: /* Look up part of the next key. */
469: static int
470: tty_keys_next1(struct tty *tty, const char *buf, size_t len, key_code *key,
1.87 nicm 471: size_t *size, int expired)
1.86 nicm 472: {
1.94 ! nicm 473: struct client *c = tty->client;
1.86 nicm 474: struct tty_key *tk, *tk1;
475: struct utf8_data ud;
476: enum utf8_state more;
477: u_int i;
478: wchar_t wc;
479:
1.94 ! nicm 480: log_debug("%s: next key is %zu (%.*s) (expired=%d)", c->name, len,
! 481: (int)len, buf, expired);
1.86 nicm 482:
483: /* Is this a known key? */
484: tk = tty_keys_find(tty, buf, len, size);
1.87 nicm 485: if (tk != NULL && tk->key != KEYC_UNKNOWN) {
1.86 nicm 486: tk1 = tk;
487: do
1.94 ! nicm 488: log_debug("%s: keys in list: %#llx", c->name, tk1->key);
1.86 nicm 489: while ((tk1 = tk1->next) != NULL);
1.87 nicm 490: if (tk->next != NULL && !expired)
491: return (1);
1.86 nicm 492: *key = tk->key;
1.87 nicm 493: return (0);
1.86 nicm 494: }
495:
496: /* Is this valid UTF-8? */
497: more = utf8_open(&ud, (u_char)*buf);
498: if (more == UTF8_MORE) {
499: *size = ud.size;
1.87 nicm 500: if (len < ud.size) {
501: if (!expired)
502: return (1);
503: return (-1);
504: }
1.86 nicm 505: for (i = 1; i < ud.size; i++)
506: more = utf8_append(&ud, (u_char)buf[i]);
507: if (more != UTF8_DONE)
1.87 nicm 508: return (-1);
1.86 nicm 509:
510: if (utf8_combine(&ud, &wc) != UTF8_DONE)
1.87 nicm 511: return (-1);
1.86 nicm 512: *key = wc;
513:
1.94 ! nicm 514: log_debug("%s: UTF-8 key %.*s %#llx", c->name, (int)ud.size,
! 515: buf, *key);
1.86 nicm 516: return (0);
517: }
518:
519: return (-1);
520: }
521:
1.16 nicm 522: /*
523: * Process at least one key in the buffer and invoke tty->key_callback. Return
1.22 nicm 524: * 0 if there are no further keys, or 1 if there could be more in the buffer.
1.16 nicm 525: */
1.76 nicm 526: key_code
1.14 nicm 527: tty_keys_next(struct tty *tty)
1.1 nicm 528: {
1.94 ! nicm 529: struct client *c = tty->client;
1.86 nicm 530: struct timeval tv;
531: const char *buf;
532: size_t len, size;
533: cc_t bspace;
1.87 nicm 534: int delay, expired = 0, n;
1.86 nicm 535: key_code key;
1.1 nicm 536:
1.51 nicm 537: /* Get key buffer. */
1.93 nicm 538: buf = EVBUFFER_DATA(tty->in);
539: len = EVBUFFER_LENGTH(tty->in);
1.82 nicm 540:
1.1 nicm 541: if (len == 0)
1.14 nicm 542: return (0);
1.94 ! nicm 543: log_debug("%s: keys are %zu (%.*s)", c->name, len, (int)len, buf);
1.1 nicm 544:
1.91 nicm 545: /* Is this a device attributes response? */
546: switch (tty_keys_device_attributes(tty, buf, len, &size)) {
547: case 0: /* yes */
548: key = KEYC_UNKNOWN;
549: goto complete_key;
550: case -1: /* no, or not valid */
551: break;
552: case 1: /* partial */
553: goto partial_key;
554: }
555:
1.26 nicm 556: /* Is this a mouse key press? */
1.40 nicm 557: switch (tty_keys_mouse(tty, buf, len, &size)) {
1.22 nicm 558: case 0: /* yes */
559: key = KEYC_MOUSE;
1.51 nicm 560: goto complete_key;
1.22 nicm 561: case -1: /* no, or not valid */
1.24 nicm 562: break;
1.67 nicm 563: case -2: /* yes, but we don't care. */
1.73 nicm 564: key = KEYC_MOUSE;
1.67 nicm 565: goto discard_key;
1.22 nicm 566: case 1: /* partial */
567: goto partial_key;
1.11 nicm 568: }
569:
1.86 nicm 570: first_key:
1.87 nicm 571: /* Handle keys starting with escape. */
1.86 nicm 572: if (*buf == '\033') {
1.87 nicm 573: /* Look for a key without the escape. */
574: n = tty_keys_next1(tty, buf + 1, len - 1, &key, &size, expired);
575: if (n == 0) { /* found */
576: key |= KEYC_ESCAPE;
577: size++;
578: goto complete_key;
579: }
580: if (n == 1) /* partial */
1.62 nicm 581: goto partial_key;
1.89 nicm 582: }
1.86 nicm 583:
1.89 nicm 584: /* Try to lookup key. */
585: n = tty_keys_next1(tty, buf, len, &key, &size, expired);
586: if (n == 0) /* found */
587: goto complete_key;
588: if (n == 1)
589: goto partial_key;
1.62 nicm 590:
1.89 nicm 591: /* Is this an an xterm(1) key? */
592: n = xterm_keys_find(buf, len, &size, &key);
593: if (n == 0)
594: goto complete_key;
595: if (n == 1 && !expired)
596: goto partial_key;
1.1 nicm 597:
1.89 nicm 598: /*
599: * At this point, we know the key is not partial (with or without
600: * escape). So pass it through even if the timer has not expired.
601: */
602: if (*buf == '\033' && len >= 2) {
603: key = (u_char)buf[1] | KEYC_ESCAPE;
604: size = 2;
605: } else {
606: key = (u_char)buf[0];
607: size = 1;
1.76 nicm 608: }
1.51 nicm 609: goto complete_key;
610:
611: partial_key:
1.94 ! nicm 612: log_debug("%s: partial key %.*s", c->name, (int)len, buf);
1.14 nicm 613:
1.51 nicm 614: /* If timer is going, check for expiration. */
615: if (tty->flags & TTY_TIMER) {
616: if (evtimer_initialized(&tty->key_timer) &&
1.59 nicm 617: !evtimer_pending(&tty->key_timer, NULL)) {
618: expired = 1;
1.51 nicm 619: goto first_key;
1.59 nicm 620: }
1.39 nicm 621: return (0);
1.51 nicm 622: }
1.39 nicm 623:
1.51 nicm 624: /* Get the time period. */
1.75 nicm 625: delay = options_get_number(global_options, "escape-time");
1.28 nicm 626: tv.tv_sec = delay / 1000;
627: tv.tv_usec = (delay % 1000) * 1000L;
1.27 nicm 628:
1.51 nicm 629: /* Start the timer. */
1.36 nicm 630: if (event_initialized(&tty->key_timer))
631: evtimer_del(&tty->key_timer);
1.14 nicm 632: evtimer_set(&tty->key_timer, tty_keys_callback, tty);
633: evtimer_add(&tty->key_timer, &tv);
1.27 nicm 634:
1.51 nicm 635: tty->flags |= TTY_TIMER;
1.14 nicm 636: return (0);
1.1 nicm 637:
1.51 nicm 638: complete_key:
1.94 ! nicm 639: log_debug("%s: complete key %.*s %#llx", c->name, (int)size, buf, key);
1.88 nicm 640:
641: /*
642: * Check for backspace key using termios VERASE - the terminfo
643: * kbs entry is extremely unreliable, so cannot be safely
644: * used. termios should have a better idea.
645: */
646: bspace = tty->tio.c_cc[VERASE];
647: if (bspace != _POSIX_VDISABLE && (key & KEYC_MASK_KEY) == bspace)
648: key = (key & KEYC_MASK_MOD) | KEYC_BSPACE;
1.16 nicm 649:
1.51 nicm 650: /* Remove data from buffer. */
1.93 nicm 651: evbuffer_drain(tty->in, size);
1.16 nicm 652:
1.51 nicm 653: /* Remove key timer. */
1.36 nicm 654: if (event_initialized(&tty->key_timer))
655: evtimer_del(&tty->key_timer);
1.51 nicm 656: tty->flags &= ~TTY_TIMER;
1.56 nicm 657:
658: /* Check for focus events. */
659: if (key == KEYC_FOCUS_OUT) {
660: tty->client->flags &= ~CLIENT_FOCUSED;
661: return (1);
662: } else if (key == KEYC_FOCUS_IN) {
663: tty->client->flags |= CLIENT_FOCUSED;
664: return (1);
665: }
1.16 nicm 666:
1.51 nicm 667: /* Fire the key. */
1.83 nicm 668: if (key != KEYC_UNKNOWN)
1.41 nicm 669: server_client_handle_key(tty->client, key);
1.16 nicm 670:
1.14 nicm 671: return (1);
1.67 nicm 672:
673: discard_key:
1.94 ! nicm 674: log_debug("%s: discard key %.*s %#llx", c->name, (int)size, buf, key);
1.67 nicm 675:
676: /* Remove data from buffer. */
1.93 nicm 677: evbuffer_drain(tty->in, size);
1.67 nicm 678:
679: return (1);
1.14 nicm 680: }
681:
1.16 nicm 682: /* Key timer callback. */
1.86 nicm 683: static void
1.81 nicm 684: tty_keys_callback(__unused int fd, __unused short events, void *data)
1.14 nicm 685: {
686: struct tty *tty = data;
687:
1.51 nicm 688: if (tty->flags & TTY_TIMER) {
689: while (tty_keys_next(tty))
690: ;
691: }
1.1 nicm 692: }
693:
1.27 nicm 694: /*
1.22 nicm 695: * Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
696: * (probably a mouse sequence but need more data).
697: */
1.86 nicm 698: static int
1.40 nicm 699: tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size)
1.1 nicm 700: {
1.94 ! nicm 701: struct client *c = tty->client;
1.41 nicm 702: struct mouse_event *m = &tty->mouse;
1.82 nicm 703: u_int i, x, y, b, sgr_b;
1.94 ! nicm 704: u_char sgr_type, ch;
1.33 nicm 705:
1.1 nicm 706: /*
1.33 nicm 707: * Standard mouse sequences are \033[M followed by three characters
1.55 nicm 708: * indicating button, X and Y, all based at 32 with 1,1 top-left.
1.33 nicm 709: *
710: * UTF-8 mouse sequences are similar but the three are expressed as
711: * UTF-8 characters.
1.55 nicm 712: *
713: * SGR extended mouse sequences are \033[< followed by three numbers in
714: * decimal and separated by semicolons indicating button, X and Y. A
715: * trailing 'M' is click or scroll and trailing 'm' release. All are
716: * based at 0 with 1,1 top-left.
1.1 nicm 717: */
718:
1.22 nicm 719: *size = 0;
1.72 nicm 720: x = y = b = sgr_b = 0;
721: sgr_type = ' ';
1.22 nicm 722:
1.55 nicm 723: /* First two bytes are always \033[. */
1.22 nicm 724: if (buf[0] != '\033')
725: return (-1);
726: if (len == 1)
727: return (1);
728: if (buf[1] != '[')
729: return (-1);
730: if (len == 2)
731: return (1);
732:
1.55 nicm 733: /*
1.82 nicm 734: * Third byte is M in old standard (and UTF-8 extension which we do not
735: * support), < in SGR extension.
1.55 nicm 736: */
737: if (buf[2] == 'M') {
738: /* Read the three inputs. */
739: *size = 3;
740: for (i = 0; i < 3; i++) {
741: if (len <= *size)
742: return (1);
1.94 ! nicm 743: ch = (u_char)buf[(*size)++];
1.55 nicm 744: if (i == 0)
1.94 ! nicm 745: b = ch;
1.55 nicm 746: else if (i == 1)
1.94 ! nicm 747: x = ch;
1.55 nicm 748: else
1.94 ! nicm 749: y = ch;
1.33 nicm 750: }
1.94 ! nicm 751: log_debug("%s: mouse input: %.*s", c->name, (int)*size, buf);
1.16 nicm 752:
1.55 nicm 753: /* Check and return the mouse input. */
1.60 nicm 754: if (b < 32)
1.55 nicm 755: return (-1);
756: b -= 32;
1.60 nicm 757: if (x >= 33)
758: x -= 33;
759: else
760: x = 256 - x;
761: if (y >= 33)
762: y -= 33;
763: else
764: y = 256 - y;
1.55 nicm 765: } else if (buf[2] == '<') {
766: /* Read the three inputs. */
767: *size = 3;
768: while (1) {
769: if (len <= *size)
770: return (1);
1.94 ! nicm 771: ch = (u_char)buf[(*size)++];
! 772: if (ch == ';')
1.55 nicm 773: break;
1.94 ! nicm 774: if (ch < '0' || ch > '9')
1.55 nicm 775: return (-1);
1.94 ! nicm 776: sgr_b = 10 * sgr_b + (ch - '0');
1.55 nicm 777: }
778: while (1) {
779: if (len <= *size)
780: return (1);
1.94 ! nicm 781: ch = (u_char)buf[(*size)++];
! 782: if (ch == ';')
1.55 nicm 783: break;
1.94 ! nicm 784: if (ch < '0' || ch > '9')
1.55 nicm 785: return (-1);
1.94 ! nicm 786: x = 10 * x + (ch - '0');
1.55 nicm 787: }
788: while (1) {
789: if (len <= *size)
790: return (1);
1.94 ! nicm 791: ch = (u_char)buf[(*size)++];
! 792: if (ch == 'M' || ch == 'm')
1.55 nicm 793: break;
1.94 ! nicm 794: if (ch < '0' || ch > '9')
1.55 nicm 795: return (-1);
1.94 ! nicm 796: y = 10 * y + (ch - '0');
1.55 nicm 797: }
1.94 ! nicm 798: log_debug("%s: mouse input (SGR): %.*s", c->name, (int)*size,
! 799: buf);
1.1 nicm 800:
1.55 nicm 801: /* Check and return the mouse input. */
802: if (x < 1 || y < 1)
803: return (-1);
804: x--;
805: y--;
1.72 nicm 806: b = sgr_b;
807:
808: /* Type is M for press, m for release. */
1.94 ! nicm 809: sgr_type = ch;
1.72 nicm 810: if (sgr_type == 'm')
811: b |= 3;
1.67 nicm 812:
813: /*
814: * Some terminals (like PuTTY 0.63) mistakenly send
815: * button-release events for scroll-wheel button-press event.
816: * Discard it before it reaches any program running inside
817: * tmux.
818: */
1.72 nicm 819: if (sgr_type == 'm' && (sgr_b & 64))
1.67 nicm 820: return (-2);
1.55 nicm 821: } else
1.22 nicm 822: return (-1);
1.45 nicm 823:
1.72 nicm 824: /* Fill mouse event. */
825: m->lx = m->x;
1.61 nicm 826: m->x = x;
1.72 nicm 827: m->ly = m->y;
1.61 nicm 828: m->y = y;
1.72 nicm 829: m->lb = m->b;
830: m->b = b;
831: m->sgr_type = sgr_type;
832: m->sgr_b = sgr_b;
1.91 nicm 833:
834: return (0);
835: }
836:
837: /*
838: * Handle device attributes input. Returns 0 for success, -1 for failure, 1 for
839: * partial.
840: */
841: static int
842: tty_keys_device_attributes(struct tty *tty, const char *buf, size_t len,
843: size_t *size)
844: {
1.94 ! nicm 845: struct client *c = tty->client;
1.92 nicm 846: u_int i, a, b;
847: char tmp[64], *endptr;
848: static const char *types[] = TTY_TYPES;
849: int type;
1.91 nicm 850:
851: *size = 0;
852:
853: /* First three bytes are always \033[?. */
854: if (buf[0] != '\033')
855: return (-1);
856: if (len == 1)
857: return (1);
858: if (buf[1] != '[')
859: return (-1);
860: if (len == 2)
861: return (1);
862: if (buf[2] != '?')
863: return (-1);
864: if (len == 3)
865: return (1);
866:
867: /* Copy the rest up to a 'c'. */
868: for (i = 0; i < (sizeof tmp) - 1 && buf[3 + i] != 'c'; i++) {
869: if (3 + i == len)
870: return (1);
871: tmp[i] = buf[3 + i];
872: }
873: if (i == (sizeof tmp) - 1)
874: return (-1);
875: tmp[i] = '\0';
876: *size = 4 + i;
877:
878: /* Convert version numbers. */
879: a = strtoul(tmp, &endptr, 10);
880: if (*endptr == ';') {
881: b = strtoul(endptr + 1, &endptr, 10);
882: if (*endptr != '\0' && *endptr != ';')
883: b = 0;
884: } else
885: a = b = 0;
886:
1.94 ! nicm 887: /* Store terminal type. */
1.92 nicm 888: type = TTY_UNKNOWN;
1.91 nicm 889: switch (a) {
890: case 1:
1.92 nicm 891: if (b == 2)
892: type = TTY_VT100;
893: else if (b == 0)
894: type = TTY_VT101;
1.91 nicm 895: break;
896: case 6:
1.92 nicm 897: type = TTY_VT102;
1.91 nicm 898: break;
899: case 62:
1.92 nicm 900: type = TTY_VT220;
1.91 nicm 901: break;
902: case 63:
1.92 nicm 903: type = TTY_VT320;
1.91 nicm 904: break;
905: case 64:
1.92 nicm 906: type = TTY_VT420;
1.91 nicm 907: break;
908: }
1.92 nicm 909: tty_set_type(tty, type);
1.35 nicm 910:
1.94 ! nicm 911: log_debug("%s: received DA %.*s (%s)", c->name, (int)*size, buf,
! 912: types[type]);
1.22 nicm 913: return (0);
1.1 nicm 914: }