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