Annotation of src/usr.bin/tmux/tty-keys.c, Revision 1.143
1.143 ! nicm 1: /* $OpenBSD: tty-keys.c,v 1.142 2020/09/23 14:57:33 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.104 nicm 22: #include <netinet/in.h>
23:
1.133 nicm 24: #include <ctype.h>
1.35 nicm 25: #include <limits.h>
1.104 nicm 26: #include <resolv.h>
1.35 nicm 27: #include <stdlib.h>
1.1 nicm 28: #include <string.h>
1.3 nicm 29: #include <termios.h>
30: #include <unistd.h>
1.1 nicm 31:
32: #include "tmux.h"
33:
1.9 nicm 34: /*
1.50 nicm 35: * Handle keys input from the outside terminal. tty_default_*_keys[] are a base
36: * table of supported keys which are looked up in terminfo(5) and translated
37: * into a ternary tree.
1.9 nicm 38: */
39:
1.86 nicm 40: static void tty_keys_add1(struct tty_key **, const char *, key_code);
41: static void tty_keys_add(struct tty *, const char *, key_code);
42: static void tty_keys_free1(struct tty_key *);
43: static struct tty_key *tty_keys_find1(struct tty_key *, const char *, size_t,
1.76 nicm 44: size_t *);
1.86 nicm 45: static struct tty_key *tty_keys_find(struct tty *, const char *, size_t,
46: size_t *);
47: static int tty_keys_next1(struct tty *, const char *, size_t, key_code *,
1.87 nicm 48: size_t *, int);
1.86 nicm 49: static void tty_keys_callback(int, short, void *);
1.133 nicm 50: static int tty_keys_extended_key(struct tty *, const char *, size_t,
51: size_t *, key_code *);
1.111 nicm 52: static int tty_keys_mouse(struct tty *, const char *, size_t, size_t *,
53: struct mouse_event *);
1.104 nicm 54: static int tty_keys_clipboard(struct tty *, const char *, size_t,
55: size_t *);
1.91 nicm 56: static int tty_keys_device_attributes(struct tty *, const char *, size_t,
57: size_t *);
1.128 nicm 58: static int tty_keys_extended_device_attributes(struct tty *, const char *,
1.119 nicm 59: size_t, size_t *);
1.1 nicm 60:
1.48 nicm 61: /* Default raw keys. */
62: struct tty_default_key_raw {
1.1 nicm 63: const char *string;
1.143 ! nicm 64: key_code key;
1.1 nicm 65: };
1.90 nicm 66: static const struct tty_default_key_raw tty_default_raw_keys[] = {
1.125 nicm 67: /* Application escape. */
68: { "\033O[", '\033' },
69:
1.1 nicm 70: /*
1.8 nicm 71: * Numeric keypad. Just use the vt100 escape sequences here and always
72: * put the terminal into keypad_xmit mode. Translation of numbers
73: * mode/applications mode is done in input-keys.c.
1.1 nicm 74: */
1.132 nicm 75: { "\033Oo", KEYC_KP_SLASH|KEYC_KEYPAD },
76: { "\033Oj", KEYC_KP_STAR|KEYC_KEYPAD },
77: { "\033Om", KEYC_KP_MINUS|KEYC_KEYPAD },
78: { "\033Ow", KEYC_KP_SEVEN|KEYC_KEYPAD },
79: { "\033Ox", KEYC_KP_EIGHT|KEYC_KEYPAD },
80: { "\033Oy", KEYC_KP_NINE|KEYC_KEYPAD },
81: { "\033Ok", KEYC_KP_PLUS|KEYC_KEYPAD },
82: { "\033Ot", KEYC_KP_FOUR|KEYC_KEYPAD },
83: { "\033Ou", KEYC_KP_FIVE|KEYC_KEYPAD },
84: { "\033Ov", KEYC_KP_SIX|KEYC_KEYPAD },
85: { "\033Oq", KEYC_KP_ONE|KEYC_KEYPAD },
86: { "\033Or", KEYC_KP_TWO|KEYC_KEYPAD },
87: { "\033Os", KEYC_KP_THREE|KEYC_KEYPAD },
88: { "\033OM", KEYC_KP_ENTER|KEYC_KEYPAD },
89: { "\033Op", KEYC_KP_ZERO|KEYC_KEYPAD },
90: { "\033On", KEYC_KP_PERIOD|KEYC_KEYPAD },
1.10 nicm 91:
1.29 nicm 92: /* Arrow keys. */
1.132 nicm 93: { "\033OA", KEYC_UP|KEYC_CURSOR },
94: { "\033OB", KEYC_DOWN|KEYC_CURSOR },
95: { "\033OC", KEYC_RIGHT|KEYC_CURSOR },
96: { "\033OD", KEYC_LEFT|KEYC_CURSOR },
97:
98: { "\033[A", KEYC_UP|KEYC_CURSOR },
99: { "\033[B", KEYC_DOWN|KEYC_CURSOR },
100: { "\033[C", KEYC_RIGHT|KEYC_CURSOR },
101: { "\033[D", KEYC_LEFT|KEYC_CURSOR },
1.57 nicm 102:
1.142 nicm 103: /*
104: * Meta arrow keys. These do not get the IMPLIED_META flag so they
105: * don't match the xterm-style meta keys in the output tree - Escape+Up
106: * should stay as Escape+Up and not become M-Up.
107: */
108: { "\033\033OA", KEYC_UP|KEYC_CURSOR|KEYC_META },
109: { "\033\033OB", KEYC_DOWN|KEYC_CURSOR|KEYC_META },
110: { "\033\033OC", KEYC_RIGHT|KEYC_CURSOR|KEYC_META },
111: { "\033\033OD", KEYC_LEFT|KEYC_CURSOR|KEYC_META },
112:
113: { "\033\033[A", KEYC_UP|KEYC_CURSOR|KEYC_META },
114: { "\033\033[B", KEYC_DOWN|KEYC_CURSOR|KEYC_META },
115: { "\033\033[C", KEYC_RIGHT|KEYC_CURSOR|KEYC_META },
116: { "\033\033[D", KEYC_LEFT|KEYC_CURSOR|KEYC_META },
1.140 nicm 117:
1.57 nicm 118: /* Other (xterm) "cursor" keys. */
119: { "\033OH", KEYC_HOME },
120: { "\033OF", KEYC_END },
121:
1.140 nicm 122: { "\033\033OH", KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
123: { "\033\033OF", KEYC_END|KEYC_META|KEYC_IMPLIED_META },
124:
1.57 nicm 125: { "\033[H", KEYC_HOME },
126: { "\033[F", KEYC_END },
1.140 nicm 127:
128: { "\033\033[H", KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
129: { "\033\033[F", KEYC_END|KEYC_META|KEYC_IMPLIED_META },
1.21 nicm 130:
131: /* rxvt-style arrow + modifier keys. */
1.48 nicm 132: { "\033Oa", KEYC_UP|KEYC_CTRL },
133: { "\033Ob", KEYC_DOWN|KEYC_CTRL },
134: { "\033Oc", KEYC_RIGHT|KEYC_CTRL },
135: { "\033Od", KEYC_LEFT|KEYC_CTRL },
136:
137: { "\033[a", KEYC_UP|KEYC_SHIFT },
138: { "\033[b", KEYC_DOWN|KEYC_SHIFT },
139: { "\033[c", KEYC_RIGHT|KEYC_SHIFT },
140: { "\033[d", KEYC_LEFT|KEYC_SHIFT },
141:
142: /* rxvt-style function + modifier keys (C = ^, S = $, C-S = @). */
143: { "\033[11^", KEYC_F1|KEYC_CTRL },
144: { "\033[12^", KEYC_F2|KEYC_CTRL },
145: { "\033[13^", KEYC_F3|KEYC_CTRL },
146: { "\033[14^", KEYC_F4|KEYC_CTRL },
147: { "\033[15^", KEYC_F5|KEYC_CTRL },
148: { "\033[17^", KEYC_F6|KEYC_CTRL },
149: { "\033[18^", KEYC_F7|KEYC_CTRL },
150: { "\033[19^", KEYC_F8|KEYC_CTRL },
151: { "\033[20^", KEYC_F9|KEYC_CTRL },
152: { "\033[21^", KEYC_F10|KEYC_CTRL },
153: { "\033[23^", KEYC_F11|KEYC_CTRL },
154: { "\033[24^", KEYC_F12|KEYC_CTRL },
155: { "\033[2^", KEYC_IC|KEYC_CTRL },
156: { "\033[3^", KEYC_DC|KEYC_CTRL },
157: { "\033[7^", KEYC_HOME|KEYC_CTRL },
158: { "\033[8^", KEYC_END|KEYC_CTRL },
159: { "\033[6^", KEYC_NPAGE|KEYC_CTRL },
160: { "\033[5^", KEYC_PPAGE|KEYC_CTRL },
161:
162: { "\033[11$", KEYC_F1|KEYC_SHIFT },
163: { "\033[12$", KEYC_F2|KEYC_SHIFT },
164: { "\033[13$", KEYC_F3|KEYC_SHIFT },
165: { "\033[14$", KEYC_F4|KEYC_SHIFT },
166: { "\033[15$", KEYC_F5|KEYC_SHIFT },
167: { "\033[17$", KEYC_F6|KEYC_SHIFT },
168: { "\033[18$", KEYC_F7|KEYC_SHIFT },
169: { "\033[19$", KEYC_F8|KEYC_SHIFT },
170: { "\033[20$", KEYC_F9|KEYC_SHIFT },
171: { "\033[21$", KEYC_F10|KEYC_SHIFT },
172: { "\033[23$", KEYC_F11|KEYC_SHIFT },
173: { "\033[24$", KEYC_F12|KEYC_SHIFT },
174: { "\033[2$", KEYC_IC|KEYC_SHIFT },
175: { "\033[3$", KEYC_DC|KEYC_SHIFT },
176: { "\033[7$", KEYC_HOME|KEYC_SHIFT },
177: { "\033[8$", KEYC_END|KEYC_SHIFT },
178: { "\033[6$", KEYC_NPAGE|KEYC_SHIFT },
179: { "\033[5$", KEYC_PPAGE|KEYC_SHIFT },
180:
181: { "\033[11@", KEYC_F1|KEYC_CTRL|KEYC_SHIFT },
182: { "\033[12@", KEYC_F2|KEYC_CTRL|KEYC_SHIFT },
183: { "\033[13@", KEYC_F3|KEYC_CTRL|KEYC_SHIFT },
184: { "\033[14@", KEYC_F4|KEYC_CTRL|KEYC_SHIFT },
185: { "\033[15@", KEYC_F5|KEYC_CTRL|KEYC_SHIFT },
186: { "\033[17@", KEYC_F6|KEYC_CTRL|KEYC_SHIFT },
187: { "\033[18@", KEYC_F7|KEYC_CTRL|KEYC_SHIFT },
188: { "\033[19@", KEYC_F8|KEYC_CTRL|KEYC_SHIFT },
189: { "\033[20@", KEYC_F9|KEYC_CTRL|KEYC_SHIFT },
190: { "\033[21@", KEYC_F10|KEYC_CTRL|KEYC_SHIFT },
191: { "\033[23@", KEYC_F11|KEYC_CTRL|KEYC_SHIFT },
192: { "\033[24@", KEYC_F12|KEYC_CTRL|KEYC_SHIFT },
193: { "\033[2@", KEYC_IC|KEYC_CTRL|KEYC_SHIFT },
194: { "\033[3@", KEYC_DC|KEYC_CTRL|KEYC_SHIFT },
195: { "\033[7@", KEYC_HOME|KEYC_CTRL|KEYC_SHIFT },
196: { "\033[8@", KEYC_END|KEYC_CTRL|KEYC_SHIFT },
197: { "\033[6@", KEYC_NPAGE|KEYC_CTRL|KEYC_SHIFT },
198: { "\033[5@", KEYC_PPAGE|KEYC_CTRL|KEYC_SHIFT },
1.56 nicm 199:
200: /* Focus tracking. */
201: { "\033[I", KEYC_FOCUS_IN },
202: { "\033[O", KEYC_FOCUS_OUT },
1.98 nicm 203:
204: /* Paste keys. */
205: { "\033[200~", KEYC_PASTE_START },
206: { "\033[201~", KEYC_PASTE_END },
1.48 nicm 207: };
1.29 nicm 208:
1.132 nicm 209: /* Default xterm keys. */
210: struct tty_default_key_xterm {
211: const char *template;
212: key_code key;
213: };
214: static const struct tty_default_key_xterm tty_default_xterm_keys[] = {
215: { "\033[1;_P", KEYC_F1 },
216: { "\033O1;_P", KEYC_F1 },
217: { "\033O_P", KEYC_F1 },
218: { "\033[1;_Q", KEYC_F2 },
219: { "\033O1;_Q", KEYC_F2 },
220: { "\033O_Q", KEYC_F2 },
221: { "\033[1;_R", KEYC_F3 },
222: { "\033O1;_R", KEYC_F3 },
223: { "\033O_R", KEYC_F3 },
224: { "\033[1;_S", KEYC_F4 },
225: { "\033O1;_S", KEYC_F4 },
226: { "\033O_S", KEYC_F4 },
227: { "\033[15;_~", KEYC_F5 },
228: { "\033[17;_~", KEYC_F6 },
229: { "\033[18;_~", KEYC_F7 },
230: { "\033[19;_~", KEYC_F8 },
231: { "\033[20;_~", KEYC_F9 },
232: { "\033[21;_~", KEYC_F10 },
233: { "\033[23;_~", KEYC_F11 },
234: { "\033[24;_~", KEYC_F12 },
235: { "\033[1;_A", KEYC_UP },
236: { "\033[1;_B", KEYC_DOWN },
237: { "\033[1;_C", KEYC_RIGHT },
238: { "\033[1;_D", KEYC_LEFT },
239: { "\033[1;_H", KEYC_HOME },
240: { "\033[1;_F", KEYC_END },
241: { "\033[5;_~", KEYC_PPAGE },
242: { "\033[6;_~", KEYC_NPAGE },
243: { "\033[2;_~", KEYC_IC },
244: { "\033[3;_~", KEYC_DC },
245: };
246: static const key_code tty_default_xterm_modifiers[] = {
247: 0,
248: 0,
1.135 nicm 249: KEYC_SHIFT,
250: KEYC_META|KEYC_IMPLIED_META,
251: KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META,
252: KEYC_CTRL,
253: KEYC_SHIFT|KEYC_CTRL,
254: KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL,
255: KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL
1.132 nicm 256: };
257:
1.103 nicm 258: /*
1.132 nicm 259: * Default terminfo(5) keys. Any keys that have builtin modifiers (that is,
260: * where the key itself contains the modifiers) has the KEYC_XTERM flag set so
261: * a leading escape is not treated as meta (and probably removed).
1.103 nicm 262: */
1.48 nicm 263: struct tty_default_key_code {
264: enum tty_code_code code;
1.143 ! nicm 265: key_code key;
1.48 nicm 266: };
1.90 nicm 267: static const struct tty_default_key_code tty_default_code_keys[] = {
1.29 nicm 268: /* Function keys. */
1.48 nicm 269: { TTYC_KF1, KEYC_F1 },
270: { TTYC_KF2, KEYC_F2 },
271: { TTYC_KF3, KEYC_F3 },
272: { TTYC_KF4, KEYC_F4 },
273: { TTYC_KF5, KEYC_F5 },
274: { TTYC_KF6, KEYC_F6 },
275: { TTYC_KF7, KEYC_F7 },
276: { TTYC_KF8, KEYC_F8 },
277: { TTYC_KF9, KEYC_F9 },
278: { TTYC_KF10, KEYC_F10 },
279: { TTYC_KF11, KEYC_F11 },
280: { TTYC_KF12, KEYC_F12 },
1.70 nicm 281:
1.135 nicm 282: { TTYC_KF13, KEYC_F1|KEYC_SHIFT },
283: { TTYC_KF14, KEYC_F2|KEYC_SHIFT },
284: { TTYC_KF15, KEYC_F3|KEYC_SHIFT },
285: { TTYC_KF16, KEYC_F4|KEYC_SHIFT },
286: { TTYC_KF17, KEYC_F5|KEYC_SHIFT },
287: { TTYC_KF18, KEYC_F6|KEYC_SHIFT },
288: { TTYC_KF19, KEYC_F7|KEYC_SHIFT },
289: { TTYC_KF20, KEYC_F8|KEYC_SHIFT },
290: { TTYC_KF21, KEYC_F9|KEYC_SHIFT },
291: { TTYC_KF22, KEYC_F10|KEYC_SHIFT },
292: { TTYC_KF23, KEYC_F11|KEYC_SHIFT },
293: { TTYC_KF24, KEYC_F12|KEYC_SHIFT },
294:
295: { TTYC_KF25, KEYC_F1|KEYC_CTRL },
296: { TTYC_KF26, KEYC_F2|KEYC_CTRL },
297: { TTYC_KF27, KEYC_F3|KEYC_CTRL },
298: { TTYC_KF28, KEYC_F4|KEYC_CTRL },
299: { TTYC_KF29, KEYC_F5|KEYC_CTRL },
300: { TTYC_KF30, KEYC_F6|KEYC_CTRL },
301: { TTYC_KF31, KEYC_F7|KEYC_CTRL },
302: { TTYC_KF32, KEYC_F8|KEYC_CTRL },
303: { TTYC_KF33, KEYC_F9|KEYC_CTRL },
304: { TTYC_KF34, KEYC_F10|KEYC_CTRL },
305: { TTYC_KF35, KEYC_F11|KEYC_CTRL },
306: { TTYC_KF36, KEYC_F12|KEYC_CTRL },
307:
308: { TTYC_KF37, KEYC_F1|KEYC_SHIFT|KEYC_CTRL },
309: { TTYC_KF38, KEYC_F2|KEYC_SHIFT|KEYC_CTRL },
310: { TTYC_KF39, KEYC_F3|KEYC_SHIFT|KEYC_CTRL },
311: { TTYC_KF40, KEYC_F4|KEYC_SHIFT|KEYC_CTRL },
312: { TTYC_KF41, KEYC_F5|KEYC_SHIFT|KEYC_CTRL },
313: { TTYC_KF42, KEYC_F6|KEYC_SHIFT|KEYC_CTRL },
314: { TTYC_KF43, KEYC_F7|KEYC_SHIFT|KEYC_CTRL },
315: { TTYC_KF44, KEYC_F8|KEYC_SHIFT|KEYC_CTRL },
316: { TTYC_KF45, KEYC_F9|KEYC_SHIFT|KEYC_CTRL },
317: { TTYC_KF46, KEYC_F10|KEYC_SHIFT|KEYC_CTRL },
318: { TTYC_KF47, KEYC_F11|KEYC_SHIFT|KEYC_CTRL },
319: { TTYC_KF48, KEYC_F12|KEYC_SHIFT|KEYC_CTRL },
320:
321: { TTYC_KF49, KEYC_F1|KEYC_META|KEYC_IMPLIED_META },
322: { TTYC_KF50, KEYC_F2|KEYC_META|KEYC_IMPLIED_META },
323: { TTYC_KF51, KEYC_F3|KEYC_META|KEYC_IMPLIED_META },
324: { TTYC_KF52, KEYC_F4|KEYC_META|KEYC_IMPLIED_META },
325: { TTYC_KF53, KEYC_F5|KEYC_META|KEYC_IMPLIED_META },
326: { TTYC_KF54, KEYC_F6|KEYC_META|KEYC_IMPLIED_META },
327: { TTYC_KF55, KEYC_F7|KEYC_META|KEYC_IMPLIED_META },
328: { TTYC_KF56, KEYC_F8|KEYC_META|KEYC_IMPLIED_META },
329: { TTYC_KF57, KEYC_F9|KEYC_META|KEYC_IMPLIED_META },
330: { TTYC_KF58, KEYC_F10|KEYC_META|KEYC_IMPLIED_META },
331: { TTYC_KF59, KEYC_F11|KEYC_META|KEYC_IMPLIED_META },
332: { TTYC_KF60, KEYC_F12|KEYC_META|KEYC_IMPLIED_META },
333:
334: { TTYC_KF61, KEYC_F1|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
335: { TTYC_KF62, KEYC_F2|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
336: { TTYC_KF63, KEYC_F3|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
1.70 nicm 337:
1.48 nicm 338: { TTYC_KICH1, KEYC_IC },
339: { TTYC_KDCH1, KEYC_DC },
340: { TTYC_KHOME, KEYC_HOME },
341: { TTYC_KEND, KEYC_END },
342: { TTYC_KNP, KEYC_NPAGE },
343: { TTYC_KPP, KEYC_PPAGE },
344: { TTYC_KCBT, KEYC_BTAB },
1.29 nicm 345:
346: /* Arrow keys from terminfo. */
1.132 nicm 347: { TTYC_KCUU1, KEYC_UP|KEYC_CURSOR },
348: { TTYC_KCUD1, KEYC_DOWN|KEYC_CURSOR },
349: { TTYC_KCUB1, KEYC_LEFT|KEYC_CURSOR },
350: { TTYC_KCUF1, KEYC_RIGHT|KEYC_CURSOR },
1.29 nicm 351:
1.103 nicm 352: /* Key and modifier capabilities. */
1.135 nicm 353: { TTYC_KDC2, KEYC_DC|KEYC_SHIFT },
354: { TTYC_KDC3, KEYC_DC|KEYC_META|KEYC_IMPLIED_META },
355: { TTYC_KDC4, KEYC_DC|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
356: { TTYC_KDC5, KEYC_DC|KEYC_CTRL },
357: { TTYC_KDC6, KEYC_DC|KEYC_SHIFT|KEYC_CTRL },
358: { TTYC_KDC7, KEYC_DC|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
359: { TTYC_KIND, KEYC_DOWN|KEYC_SHIFT },
360: { TTYC_KDN2, KEYC_DOWN|KEYC_SHIFT },
361: { TTYC_KDN3, KEYC_DOWN|KEYC_META|KEYC_IMPLIED_META },
362: { TTYC_KDN4, KEYC_DOWN|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
363: { TTYC_KDN5, KEYC_DOWN|KEYC_CTRL },
364: { TTYC_KDN6, KEYC_DOWN|KEYC_SHIFT|KEYC_CTRL },
365: { TTYC_KDN7, KEYC_DOWN|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
366: { TTYC_KEND2, KEYC_END|KEYC_SHIFT },
367: { TTYC_KEND3, KEYC_END|KEYC_META|KEYC_IMPLIED_META },
368: { TTYC_KEND4, KEYC_END|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
369: { TTYC_KEND5, KEYC_END|KEYC_CTRL },
370: { TTYC_KEND6, KEYC_END|KEYC_SHIFT|KEYC_CTRL },
371: { TTYC_KEND7, KEYC_END|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
372: { TTYC_KHOM2, KEYC_HOME|KEYC_SHIFT },
373: { TTYC_KHOM3, KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
374: { TTYC_KHOM4, KEYC_HOME|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
375: { TTYC_KHOM5, KEYC_HOME|KEYC_CTRL },
376: { TTYC_KHOM6, KEYC_HOME|KEYC_SHIFT|KEYC_CTRL },
377: { TTYC_KHOM7, KEYC_HOME|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
378: { TTYC_KIC2, KEYC_IC|KEYC_SHIFT },
379: { TTYC_KIC3, KEYC_IC|KEYC_META|KEYC_IMPLIED_META },
380: { TTYC_KIC4, KEYC_IC|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
381: { TTYC_KIC5, KEYC_IC|KEYC_CTRL },
382: { TTYC_KIC6, KEYC_IC|KEYC_SHIFT|KEYC_CTRL },
383: { TTYC_KIC7, KEYC_IC|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
384: { TTYC_KLFT2, KEYC_LEFT|KEYC_SHIFT },
385: { TTYC_KLFT3, KEYC_LEFT|KEYC_META|KEYC_IMPLIED_META },
386: { TTYC_KLFT4, KEYC_LEFT|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
387: { TTYC_KLFT5, KEYC_LEFT|KEYC_CTRL },
388: { TTYC_KLFT6, KEYC_LEFT|KEYC_SHIFT|KEYC_CTRL },
389: { TTYC_KLFT7, KEYC_LEFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
390: { TTYC_KNXT2, KEYC_NPAGE|KEYC_SHIFT },
391: { TTYC_KNXT3, KEYC_NPAGE|KEYC_META|KEYC_IMPLIED_META },
392: { TTYC_KNXT4, KEYC_NPAGE|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
393: { TTYC_KNXT5, KEYC_NPAGE|KEYC_CTRL },
394: { TTYC_KNXT6, KEYC_NPAGE|KEYC_SHIFT|KEYC_CTRL },
395: { TTYC_KNXT7, KEYC_NPAGE|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
396: { TTYC_KPRV2, KEYC_PPAGE|KEYC_SHIFT },
397: { TTYC_KPRV3, KEYC_PPAGE|KEYC_META|KEYC_IMPLIED_META },
398: { TTYC_KPRV4, KEYC_PPAGE|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
399: { TTYC_KPRV5, KEYC_PPAGE|KEYC_CTRL },
400: { TTYC_KPRV6, KEYC_PPAGE|KEYC_SHIFT|KEYC_CTRL },
401: { TTYC_KPRV7, KEYC_PPAGE|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
402: { TTYC_KRIT2, KEYC_RIGHT|KEYC_SHIFT },
403: { TTYC_KRIT3, KEYC_RIGHT|KEYC_META|KEYC_IMPLIED_META },
404: { TTYC_KRIT4, KEYC_RIGHT|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
405: { TTYC_KRIT5, KEYC_RIGHT|KEYC_CTRL },
406: { TTYC_KRIT6, KEYC_RIGHT|KEYC_SHIFT|KEYC_CTRL },
407: { TTYC_KRIT7, KEYC_RIGHT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
408: { TTYC_KRI, KEYC_UP|KEYC_SHIFT },
409: { TTYC_KUP2, KEYC_UP|KEYC_SHIFT },
410: { TTYC_KUP3, KEYC_UP|KEYC_META|KEYC_IMPLIED_META },
411: { TTYC_KUP4, KEYC_UP|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
412: { TTYC_KUP5, KEYC_UP|KEYC_CTRL },
413: { TTYC_KUP6, KEYC_UP|KEYC_SHIFT|KEYC_CTRL },
414: { TTYC_KUP7, KEYC_UP|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
1.1 nicm 415: };
416:
1.48 nicm 417: /* Add key to tree. */
1.86 nicm 418: static void
1.76 nicm 419: tty_keys_add(struct tty *tty, const char *s, key_code key)
1.16 nicm 420: {
1.29 nicm 421: struct tty_key *tk;
422: size_t size;
1.143 ! nicm 423: const char *keystr;
1.1 nicm 424:
1.135 nicm 425: keystr = key_string_lookup_key(key, 1);
1.29 nicm 426: if ((tk = tty_keys_find(tty, s, strlen(s), &size)) == NULL) {
1.76 nicm 427: log_debug("new key %s: 0x%llx (%s)", s, key, keystr);
1.16 nicm 428: tty_keys_add1(&tty->key_tree, s, key);
1.29 nicm 429: } else {
1.76 nicm 430: log_debug("replacing key %s: 0x%llx (%s)", s, key, keystr);
1.29 nicm 431: tk->key = key;
1.16 nicm 432: }
1.1 nicm 433: }
434:
1.16 nicm 435: /* Add next node to the tree. */
1.86 nicm 436: static void
1.76 nicm 437: tty_keys_add1(struct tty_key **tkp, const char *s, key_code key)
1.1 nicm 438: {
1.16 nicm 439: struct tty_key *tk;
1.1 nicm 440:
1.16 nicm 441: /* Allocate a tree entry if there isn't one already. */
442: tk = *tkp;
443: if (tk == NULL) {
444: tk = *tkp = xcalloc(1, sizeof *tk);
445: tk->ch = *s;
1.83 nicm 446: tk->key = KEYC_UNKNOWN;
1.16 nicm 447: }
448:
449: /* Find the next entry. */
450: if (*s == tk->ch) {
451: /* Move forward in string. */
452: s++;
453:
454: /* If this is the end of the string, no more is necessary. */
455: if (*s == '\0') {
456: tk->key = key;
457: return;
458: }
459:
460: /* Use the child tree for the next character. */
461: tkp = &tk->next;
1.27 nicm 462: } else {
1.16 nicm 463: if (*s < tk->ch)
464: tkp = &tk->left;
465: else if (*s > tk->ch)
466: tkp = &tk->right;
467: }
468:
469: /* And recurse to add it. */
470: tty_keys_add1(tkp, s, key);
1.1 nicm 471: }
472:
1.16 nicm 473: /* Initialise a key tree from the table. */
1.1 nicm 474: void
1.48 nicm 475: tty_keys_build(struct tty *tty)
1.1 nicm 476: {
1.48 nicm 477: const struct tty_default_key_raw *tdkr;
1.132 nicm 478: const struct tty_default_key_xterm *tdkx;
1.48 nicm 479: const struct tty_default_key_code *tdkc;
1.143 ! nicm 480: u_int i, j;
1.109 nicm 481: const char *s;
1.99 nicm 482: struct options_entry *o;
1.107 nicm 483: struct options_array_item *a;
1.109 nicm 484: union options_value *ov;
1.132 nicm 485: char copy[16];
486: key_code key;
1.1 nicm 487:
1.48 nicm 488: if (tty->key_tree != NULL)
1.71 nicm 489: tty_keys_free(tty);
1.16 nicm 490: tty->key_tree = NULL;
1.1 nicm 491:
1.132 nicm 492: for (i = 0; i < nitems(tty_default_xterm_keys); i++) {
493: tdkx = &tty_default_xterm_keys[i];
494: for (j = 2; j < nitems(tty_default_xterm_modifiers); j++) {
495: strlcpy(copy, tdkx->template, sizeof copy);
496: copy[strcspn(copy, "_")] = '0' + j;
497:
498: key = tdkx->key|tty_default_xterm_modifiers[j];
499: tty_keys_add(tty, copy, key);
500: }
501: }
1.48 nicm 502: for (i = 0; i < nitems(tty_default_raw_keys); i++) {
503: tdkr = &tty_default_raw_keys[i];
504:
505: s = tdkr->string;
1.51 nicm 506: if (*s != '\0')
507: tty_keys_add(tty, s, tdkr->key);
1.48 nicm 508: }
509: for (i = 0; i < nitems(tty_default_code_keys); i++) {
510: tdkc = &tty_default_code_keys[i];
511:
512: s = tty_term_string(tty->term, tdkc->code);
1.51 nicm 513: if (*s != '\0')
514: tty_keys_add(tty, s, tdkc->key);
1.1 nicm 515:
1.99 nicm 516: }
517:
518: o = options_get(global_options, "user-keys");
1.107 nicm 519: if (o != NULL) {
520: a = options_array_first(o);
521: while (a != NULL) {
1.112 nicm 522: i = options_array_item_index(a);
1.109 nicm 523: ov = options_array_item_value(a);
1.110 nicm 524: tty_keys_add(tty, ov->string, KEYC_USER + i);
1.107 nicm 525: a = options_array_next(a);
1.99 nicm 526: }
1.1 nicm 527: }
528: }
529:
1.16 nicm 530: /* Free the entire key tree. */
1.1 nicm 531: void
532: tty_keys_free(struct tty *tty)
533: {
1.16 nicm 534: tty_keys_free1(tty->key_tree);
535: }
1.1 nicm 536:
1.16 nicm 537: /* Free a single key. */
1.86 nicm 538: static void
1.16 nicm 539: tty_keys_free1(struct tty_key *tk)
540: {
541: if (tk->next != NULL)
542: tty_keys_free1(tk->next);
543: if (tk->left != NULL)
544: tty_keys_free1(tk->left);
545: if (tk->right != NULL)
546: tty_keys_free1(tk->right);
1.42 nicm 547: free(tk);
1.1 nicm 548: }
549:
1.16 nicm 550: /* Lookup a key in the tree. */
1.86 nicm 551: static struct tty_key *
1.16 nicm 552: tty_keys_find(struct tty *tty, const char *buf, size_t len, size_t *size)
1.1 nicm 553: {
1.16 nicm 554: *size = 0;
555: return (tty_keys_find1(tty->key_tree, buf, len, size));
556: }
1.1 nicm 557:
1.16 nicm 558: /* Find the next node. */
1.86 nicm 559: static struct tty_key *
1.16 nicm 560: tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size)
561: {
1.106 nicm 562: /* If no data, no match. */
563: if (len == 0)
564: return (NULL);
565:
1.16 nicm 566: /* If the node is NULL, this is the end of the tree. No match. */
567: if (tk == NULL)
1.1 nicm 568: return (NULL);
569:
1.16 nicm 570: /* Pick the next in the sequence. */
571: if (tk->ch == *buf) {
572: /* Move forward in the string. */
573: buf++; len--;
574: (*size)++;
1.1 nicm 575:
1.16 nicm 576: /* At the end of the string, return the current node. */
1.83 nicm 577: if (len == 0 || (tk->next == NULL && tk->key != KEYC_UNKNOWN))
1.16 nicm 578: return (tk);
1.1 nicm 579:
1.16 nicm 580: /* Move into the next tree for the following character. */
581: tk = tk->next;
582: } else {
583: if (*buf < tk->ch)
584: tk = tk->left;
585: else if (*buf > tk->ch)
586: tk = tk->right;
1.1 nicm 587: }
588:
1.16 nicm 589: /* Move to the next in the tree. */
590: return (tty_keys_find1(tk, buf, len, size));
1.1 nicm 591: }
592:
1.86 nicm 593: /* Look up part of the next key. */
594: static int
595: tty_keys_next1(struct tty *tty, const char *buf, size_t len, key_code *key,
1.87 nicm 596: size_t *size, int expired)
1.86 nicm 597: {
1.94 nicm 598: struct client *c = tty->client;
1.86 nicm 599: struct tty_key *tk, *tk1;
600: struct utf8_data ud;
601: enum utf8_state more;
1.138 nicm 602: utf8_char uc;
1.86 nicm 603: u_int i;
604:
1.94 nicm 605: log_debug("%s: next key is %zu (%.*s) (expired=%d)", c->name, len,
606: (int)len, buf, expired);
1.86 nicm 607:
608: /* Is this a known key? */
609: tk = tty_keys_find(tty, buf, len, size);
1.87 nicm 610: if (tk != NULL && tk->key != KEYC_UNKNOWN) {
1.86 nicm 611: tk1 = tk;
612: do
1.94 nicm 613: log_debug("%s: keys in list: %#llx", c->name, tk1->key);
1.86 nicm 614: while ((tk1 = tk1->next) != NULL);
1.87 nicm 615: if (tk->next != NULL && !expired)
616: return (1);
1.86 nicm 617: *key = tk->key;
1.87 nicm 618: return (0);
1.86 nicm 619: }
1.97 nicm 620:
1.86 nicm 621: /* Is this valid UTF-8? */
622: more = utf8_open(&ud, (u_char)*buf);
623: if (more == UTF8_MORE) {
624: *size = ud.size;
1.87 nicm 625: if (len < ud.size) {
626: if (!expired)
627: return (1);
628: return (-1);
629: }
1.86 nicm 630: for (i = 1; i < ud.size; i++)
631: more = utf8_append(&ud, (u_char)buf[i]);
632: if (more != UTF8_DONE)
1.87 nicm 633: return (-1);
1.86 nicm 634:
1.138 nicm 635: if (utf8_from_data(&ud, &uc) != UTF8_DONE)
1.87 nicm 636: return (-1);
1.138 nicm 637: *key = uc;
1.86 nicm 638:
1.94 nicm 639: log_debug("%s: UTF-8 key %.*s %#llx", c->name, (int)ud.size,
1.138 nicm 640: ud.data, *key);
1.86 nicm 641: return (0);
642: }
643:
644: return (-1);
645: }
646:
1.111 nicm 647: /* Process at least one key in the buffer. Return 0 if no keys present. */
648: int
1.14 nicm 649: tty_keys_next(struct tty *tty)
1.1 nicm 650: {
1.111 nicm 651: struct client *c = tty->client;
652: struct timeval tv;
653: const char *buf;
654: size_t len, size;
655: cc_t bspace;
656: int delay, expired = 0, n;
657: key_code key;
658: struct mouse_event m = { 0 };
659: struct key_event *event;
1.1 nicm 660:
1.51 nicm 661: /* Get key buffer. */
1.93 nicm 662: buf = EVBUFFER_DATA(tty->in);
663: len = EVBUFFER_LENGTH(tty->in);
1.1 nicm 664: if (len == 0)
1.14 nicm 665: return (0);
1.94 nicm 666: log_debug("%s: keys are %zu (%.*s)", c->name, len, (int)len, buf);
1.1 nicm 667:
1.104 nicm 668: /* Is this a clipboard response? */
669: switch (tty_keys_clipboard(tty, buf, len, &size)) {
670: case 0: /* yes */
671: key = KEYC_UNKNOWN;
672: goto complete_key;
673: case -1: /* no, or not valid */
674: break;
675: case 1: /* partial */
676: goto partial_key;
677: }
678:
1.91 nicm 679: /* Is this a device attributes response? */
680: switch (tty_keys_device_attributes(tty, buf, len, &size)) {
681: case 0: /* yes */
682: key = KEYC_UNKNOWN;
683: goto complete_key;
684: case -1: /* no, or not valid */
685: break;
686: case 1: /* partial */
687: goto partial_key;
688: }
689:
1.128 nicm 690: /* Is this an extended device attributes response? */
691: switch (tty_keys_extended_device_attributes(tty, buf, len, &size)) {
1.119 nicm 692: case 0: /* yes */
693: key = KEYC_UNKNOWN;
694: goto complete_key;
695: case -1: /* no, or not valid */
696: break;
697: case 1: /* partial */
698: goto partial_key;
699: }
700:
1.26 nicm 701: /* Is this a mouse key press? */
1.111 nicm 702: switch (tty_keys_mouse(tty, buf, len, &size, &m)) {
1.22 nicm 703: case 0: /* yes */
704: key = KEYC_MOUSE;
1.51 nicm 705: goto complete_key;
1.22 nicm 706: case -1: /* no, or not valid */
1.24 nicm 707: break;
1.67 nicm 708: case -2: /* yes, but we don't care. */
1.73 nicm 709: key = KEYC_MOUSE;
1.67 nicm 710: goto discard_key;
1.22 nicm 711: case 1: /* partial */
712: goto partial_key;
1.11 nicm 713: }
714:
1.133 nicm 715: /* Is this an extended key press? */
716: switch (tty_keys_extended_key(tty, buf, len, &size, &key)) {
717: case 0: /* yes */
718: goto complete_key;
719: case -1: /* no, or not valid */
720: break;
721: case 1: /* partial */
722: goto partial_key;
723: }
724:
1.86 nicm 725: first_key:
1.101 nicm 726: /* Try to lookup complete key. */
727: n = tty_keys_next1(tty, buf, len, &key, &size, expired);
728: if (n == 0) /* found */
729: goto complete_key;
730: if (n == 1)
731: goto partial_key;
732:
733: /*
734: * If not a complete key, look for key with an escape prefix (meta
735: * modifier).
736: */
1.105 nicm 737: if (*buf == '\033' && len > 1) {
1.87 nicm 738: /* Look for a key without the escape. */
739: n = tty_keys_next1(tty, buf + 1, len - 1, &key, &size, expired);
740: if (n == 0) { /* found */
1.135 nicm 741: if (key & KEYC_IMPLIED_META) {
1.97 nicm 742: /*
743: * We want the escape key as well as the xterm
744: * key, because the xterm sequence implicitly
745: * includes the escape (so if we see
746: * \033\033[1;3D we know it is an Escape
747: * followed by M-Left, not just M-Left).
748: */
749: key = '\033';
750: size = 1;
751: goto complete_key;
752: }
1.134 nicm 753: key |= KEYC_META;
1.87 nicm 754: size++;
755: goto complete_key;
756: }
757: if (n == 1) /* partial */
1.62 nicm 758: goto partial_key;
1.89 nicm 759: }
1.1 nicm 760:
1.89 nicm 761: /*
762: * At this point, we know the key is not partial (with or without
763: * escape). So pass it through even if the timer has not expired.
764: */
765: if (*buf == '\033' && len >= 2) {
1.134 nicm 766: key = (u_char)buf[1] | KEYC_META;
1.89 nicm 767: size = 2;
768: } else {
769: key = (u_char)buf[0];
770: size = 1;
1.76 nicm 771: }
1.51 nicm 772: goto complete_key;
773:
774: partial_key:
1.94 nicm 775: log_debug("%s: partial key %.*s", c->name, (int)len, buf);
1.14 nicm 776:
1.51 nicm 777: /* If timer is going, check for expiration. */
778: if (tty->flags & TTY_TIMER) {
779: if (evtimer_initialized(&tty->key_timer) &&
1.59 nicm 780: !evtimer_pending(&tty->key_timer, NULL)) {
781: expired = 1;
1.51 nicm 782: goto first_key;
1.59 nicm 783: }
1.39 nicm 784: return (0);
1.51 nicm 785: }
1.39 nicm 786:
1.51 nicm 787: /* Get the time period. */
1.75 nicm 788: delay = options_get_number(global_options, "escape-time");
1.28 nicm 789: tv.tv_sec = delay / 1000;
790: tv.tv_usec = (delay % 1000) * 1000L;
1.27 nicm 791:
1.51 nicm 792: /* Start the timer. */
1.36 nicm 793: if (event_initialized(&tty->key_timer))
794: evtimer_del(&tty->key_timer);
1.14 nicm 795: evtimer_set(&tty->key_timer, tty_keys_callback, tty);
796: evtimer_add(&tty->key_timer, &tv);
1.27 nicm 797:
1.51 nicm 798: tty->flags |= TTY_TIMER;
1.14 nicm 799: return (0);
1.1 nicm 800:
1.51 nicm 801: complete_key:
1.94 nicm 802: log_debug("%s: complete key %.*s %#llx", c->name, (int)size, buf, key);
1.88 nicm 803:
804: /*
805: * Check for backspace key using termios VERASE - the terminfo
806: * kbs entry is extremely unreliable, so cannot be safely
807: * used. termios should have a better idea.
808: */
809: bspace = tty->tio.c_cc[VERASE];
810: if (bspace != _POSIX_VDISABLE && (key & KEYC_MASK_KEY) == bspace)
1.135 nicm 811: key = (key & KEYC_MASK_MODIFIERS)|KEYC_BSPACE;
1.16 nicm 812:
1.51 nicm 813: /* Remove data from buffer. */
1.93 nicm 814: evbuffer_drain(tty->in, size);
1.16 nicm 815:
1.51 nicm 816: /* Remove key timer. */
1.36 nicm 817: if (event_initialized(&tty->key_timer))
818: evtimer_del(&tty->key_timer);
1.51 nicm 819: tty->flags &= ~TTY_TIMER;
1.56 nicm 820:
821: /* Check for focus events. */
1.137 nicm 822: if (key == KEYC_FOCUS_OUT)
1.56 nicm 823: tty->client->flags &= ~CLIENT_FOCUSED;
1.137 nicm 824: else if (key == KEYC_FOCUS_IN)
1.56 nicm 825: tty->client->flags |= CLIENT_FOCUSED;
1.16 nicm 826:
1.51 nicm 827: /* Fire the key. */
1.111 nicm 828: if (key != KEYC_UNKNOWN) {
829: event = xmalloc(sizeof *event);
830: event->key = key;
831: memcpy(&event->m, &m, sizeof event->m);
1.113 nicm 832: if (!server_client_handle_key(c, event))
833: free(event);
1.111 nicm 834: }
1.16 nicm 835:
1.14 nicm 836: return (1);
1.67 nicm 837:
838: discard_key:
1.94 nicm 839: log_debug("%s: discard key %.*s %#llx", c->name, (int)size, buf, key);
1.67 nicm 840:
841: /* Remove data from buffer. */
1.93 nicm 842: evbuffer_drain(tty->in, size);
1.67 nicm 843:
844: return (1);
1.14 nicm 845: }
846:
1.16 nicm 847: /* Key timer callback. */
1.86 nicm 848: static void
1.81 nicm 849: tty_keys_callback(__unused int fd, __unused short events, void *data)
1.14 nicm 850: {
851: struct tty *tty = data;
852:
1.51 nicm 853: if (tty->flags & TTY_TIMER) {
854: while (tty_keys_next(tty))
855: ;
856: }
1.133 nicm 857: }
858:
859: /*
860: * Handle extended key input. This has two forms: \033[27;m;k~ and \033[k;mu,
861: * where k is key as a number and m is a modifier. Returns 0 for success, -1
862: * for failure, 1 for partial;
863: */
864: static int
865: tty_keys_extended_key(struct tty *tty, const char *buf, size_t len,
866: size_t *size, key_code *key)
867: {
868: struct client *c = tty->client;
869: size_t end;
870: u_int number, modifiers;
871: char tmp[64];
1.143 ! nicm 872: cc_t bspace;
! 873: key_code nkey;
1.133 nicm 874:
875: *size = 0;
876:
877: /* First two bytes are always \033[. */
878: if (buf[0] != '\033')
879: return (-1);
880: if (len == 1)
881: return (1);
882: if (buf[1] != '[')
883: return (-1);
884: if (len == 2)
885: return (1);
886:
887: /*
888: * Look for a terminator. Stop at either '~' or anything that isn't a
889: * number or ';'.
890: */
891: for (end = 2; end < len && end != sizeof tmp; end++) {
892: if (buf[end] == '~')
893: break;
894: if (!isdigit((u_char)buf[end]) && buf[end] != ';')
895: break;
896: }
897: if (end == len)
898: return (1);
899: if (end == sizeof tmp || (buf[end] != '~' && buf[end] != 'u'))
900: return (-1);
901:
902: /* Copy to the buffer. */
903: memcpy(tmp, buf + 2, end);
904: tmp[end] = '\0';
905:
906: /* Try to parse either form of key. */
907: if (buf[end] == '~') {
908: if (sscanf(tmp, "27;%u;%u", &modifiers, &number) != 2)
909: return (-1);
910: } else {
911: if (sscanf(tmp ,"%u;%u", &number, &modifiers) != 2)
912: return (-1);
913: }
914: *size = end + 1;
915:
1.143 ! nicm 916: /* Store the key. */
! 917: bspace = tty->tio.c_cc[VERASE];
! 918: if (bspace != _POSIX_VDISABLE && number == bspace)
! 919: nkey = KEYC_BSPACE;
! 920: else
! 921: nkey = number;
! 922:
! 923: /* Update the modifiers. */
1.133 nicm 924: switch (modifiers) {
925: case 2:
1.143 ! nicm 926: nkey |= KEYC_SHIFT;
1.133 nicm 927: break;
928: case 3:
1.143 ! nicm 929: nkey |= (KEYC_META|KEYC_IMPLIED_META);
1.133 nicm 930: break;
931: case 4:
1.143 ! nicm 932: nkey |= (KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META);
1.133 nicm 933: break;
934: case 5:
1.143 ! nicm 935: nkey |= KEYC_CTRL;
1.133 nicm 936: break;
937: case 6:
1.143 ! nicm 938: nkey |= (KEYC_SHIFT|KEYC_CTRL);
1.133 nicm 939: break;
940: case 7:
1.143 ! nicm 941: nkey |= (KEYC_META|KEYC_CTRL);
1.133 nicm 942: break;
943: case 8:
1.143 ! nicm 944: nkey |= (KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL);
1.133 nicm 945: break;
946: default:
947: *key = KEYC_NONE;
948: break;
949: }
1.143 ! nicm 950:
! 951: /* Don't allow both KEYC_CTRL and implied. */
! 952: if ((nkey & KEYC_CTRL) && (nkey & KEYC_MASK_KEY) < 32)
! 953: nkey &= ~KEYC_CTRL;
! 954: if ((nkey & KEYC_MASK_MODIFIERS) == KEYC_CTRL) {
! 955: nkey &= KEYC_MASK_KEY;
! 956: if (nkey >= 97 && nkey <= 122)
! 957: nkey -= 96;
! 958: else if (nkey == 32)
! 959: nkey = 0;
! 960: else if (nkey == 63)
! 961: nkey = 127;
! 962: else
! 963: nkey |= KEYC_CTRL;
! 964: }
! 965:
1.133 nicm 966: if (log_get_level() != 0) {
967: log_debug("%s: extended key %.*s is %llx (%s)", c->name,
1.143 ! nicm 968: (int)*size, buf, nkey, key_string_lookup_key(nkey, 1));
1.133 nicm 969: }
1.143 ! nicm 970: *key = nkey;
1.133 nicm 971: return (0);
1.1 nicm 972: }
973:
1.27 nicm 974: /*
1.22 nicm 975: * Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
976: * (probably a mouse sequence but need more data).
977: */
1.86 nicm 978: static int
1.111 nicm 979: tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size,
980: struct mouse_event *m)
1.1 nicm 981: {
1.111 nicm 982: struct client *c = tty->client;
983: u_int i, x, y, b, sgr_b;
984: u_char sgr_type, ch;
1.33 nicm 985:
1.1 nicm 986: /*
1.33 nicm 987: * Standard mouse sequences are \033[M followed by three characters
1.55 nicm 988: * indicating button, X and Y, all based at 32 with 1,1 top-left.
1.33 nicm 989: *
990: * UTF-8 mouse sequences are similar but the three are expressed as
991: * UTF-8 characters.
1.55 nicm 992: *
993: * SGR extended mouse sequences are \033[< followed by three numbers in
994: * decimal and separated by semicolons indicating button, X and Y. A
995: * trailing 'M' is click or scroll and trailing 'm' release. All are
996: * based at 0 with 1,1 top-left.
1.1 nicm 997: */
998:
1.22 nicm 999: *size = 0;
1.72 nicm 1000: x = y = b = sgr_b = 0;
1001: sgr_type = ' ';
1.22 nicm 1002:
1.55 nicm 1003: /* First two bytes are always \033[. */
1.22 nicm 1004: if (buf[0] != '\033')
1005: return (-1);
1006: if (len == 1)
1007: return (1);
1008: if (buf[1] != '[')
1009: return (-1);
1010: if (len == 2)
1011: return (1);
1012:
1.55 nicm 1013: /*
1.82 nicm 1014: * Third byte is M in old standard (and UTF-8 extension which we do not
1015: * support), < in SGR extension.
1.55 nicm 1016: */
1017: if (buf[2] == 'M') {
1018: /* Read the three inputs. */
1019: *size = 3;
1020: for (i = 0; i < 3; i++) {
1021: if (len <= *size)
1022: return (1);
1.94 nicm 1023: ch = (u_char)buf[(*size)++];
1.55 nicm 1024: if (i == 0)
1.94 nicm 1025: b = ch;
1.55 nicm 1026: else if (i == 1)
1.94 nicm 1027: x = ch;
1.55 nicm 1028: else
1.94 nicm 1029: y = ch;
1.33 nicm 1030: }
1.94 nicm 1031: log_debug("%s: mouse input: %.*s", c->name, (int)*size, buf);
1.16 nicm 1032:
1.55 nicm 1033: /* Check and return the mouse input. */
1.60 nicm 1034: if (b < 32)
1.55 nicm 1035: return (-1);
1036: b -= 32;
1.60 nicm 1037: if (x >= 33)
1038: x -= 33;
1039: else
1040: x = 256 - x;
1041: if (y >= 33)
1042: y -= 33;
1043: else
1044: y = 256 - y;
1.55 nicm 1045: } else if (buf[2] == '<') {
1046: /* Read the three inputs. */
1047: *size = 3;
1048: while (1) {
1049: if (len <= *size)
1050: return (1);
1.94 nicm 1051: ch = (u_char)buf[(*size)++];
1052: if (ch == ';')
1.55 nicm 1053: break;
1.94 nicm 1054: if (ch < '0' || ch > '9')
1.55 nicm 1055: return (-1);
1.94 nicm 1056: sgr_b = 10 * sgr_b + (ch - '0');
1.55 nicm 1057: }
1058: while (1) {
1059: if (len <= *size)
1060: return (1);
1.94 nicm 1061: ch = (u_char)buf[(*size)++];
1062: if (ch == ';')
1.55 nicm 1063: break;
1.94 nicm 1064: if (ch < '0' || ch > '9')
1.55 nicm 1065: return (-1);
1.94 nicm 1066: x = 10 * x + (ch - '0');
1.55 nicm 1067: }
1068: while (1) {
1069: if (len <= *size)
1070: return (1);
1.94 nicm 1071: ch = (u_char)buf[(*size)++];
1072: if (ch == 'M' || ch == 'm')
1.55 nicm 1073: break;
1.94 nicm 1074: if (ch < '0' || ch > '9')
1.55 nicm 1075: return (-1);
1.94 nicm 1076: y = 10 * y + (ch - '0');
1.55 nicm 1077: }
1.94 nicm 1078: log_debug("%s: mouse input (SGR): %.*s", c->name, (int)*size,
1079: buf);
1.1 nicm 1080:
1.55 nicm 1081: /* Check and return the mouse input. */
1082: if (x < 1 || y < 1)
1083: return (-1);
1084: x--;
1085: y--;
1.72 nicm 1086: b = sgr_b;
1087:
1088: /* Type is M for press, m for release. */
1.94 nicm 1089: sgr_type = ch;
1.72 nicm 1090: if (sgr_type == 'm')
1091: b |= 3;
1.67 nicm 1092:
1093: /*
1094: * Some terminals (like PuTTY 0.63) mistakenly send
1095: * button-release events for scroll-wheel button-press event.
1096: * Discard it before it reaches any program running inside
1097: * tmux.
1098: */
1.72 nicm 1099: if (sgr_type == 'm' && (sgr_b & 64))
1.67 nicm 1100: return (-2);
1.55 nicm 1101: } else
1.22 nicm 1102: return (-1);
1.45 nicm 1103:
1.72 nicm 1104: /* Fill mouse event. */
1.111 nicm 1105: m->lx = tty->mouse_last_x;
1.61 nicm 1106: m->x = x;
1.111 nicm 1107: m->ly = tty->mouse_last_y;
1.61 nicm 1108: m->y = y;
1.114 nicm 1109: m->lb = tty->mouse_last_b;
1.72 nicm 1110: m->b = b;
1111: m->sgr_type = sgr_type;
1112: m->sgr_b = sgr_b;
1.111 nicm 1113:
1114: /* Update last mouse state. */
1115: tty->mouse_last_x = x;
1116: tty->mouse_last_y = y;
1.114 nicm 1117: tty->mouse_last_b = b;
1.104 nicm 1118:
1119: return (0);
1120: }
1121:
1122: /*
1123: * Handle OSC 52 clipboard input. Returns 0 for success, -1 for failure, 1 for
1124: * partial.
1125: */
1126: static int
1127: tty_keys_clipboard(__unused struct tty *tty, const char *buf, size_t len,
1128: size_t *size)
1129: {
1130: size_t end, terminator, needed;
1131: char *copy, *out;
1132: int outlen;
1133:
1134: *size = 0;
1135:
1.128 nicm 1136: /* First five bytes are always \033]52;. */
1.104 nicm 1137: if (buf[0] != '\033')
1138: return (-1);
1139: if (len == 1)
1140: return (1);
1141: if (buf[1] != ']')
1142: return (-1);
1143: if (len == 2)
1144: return (1);
1145: if (buf[2] != '5')
1146: return (-1);
1147: if (len == 3)
1148: return (1);
1149: if (buf[3] != '2')
1150: return (-1);
1151: if (len == 4)
1152: return (1);
1153: if (buf[4] != ';')
1154: return (-1);
1155: if (len == 5)
1156: return (1);
1157:
1158: /* Find the terminator if any. */
1159: for (end = 5; end < len; end++) {
1160: if (buf[end] == '\007') {
1161: terminator = 1;
1162: break;
1163: }
1164: if (end > 5 && buf[end - 1] == '\033' && buf[end] == '\\') {
1165: terminator = 2;
1166: break;
1167: }
1168: }
1169: if (end == len)
1170: return (1);
1171: *size = end + terminator;
1172:
1173: /* Skip the initial part. */
1174: buf += 5;
1175: end -= 5;
1176:
1177: /* Get the second argument. */
1178: while (end != 0 && *buf != ';') {
1179: buf++;
1180: end--;
1181: }
1182: if (end == 0 || end == 1)
1183: return (0);
1184: buf++;
1185: end--;
1186:
1187: /* It has to be a string so copy it. */
1188: copy = xmalloc(end + 1);
1189: memcpy(copy, buf, end);
1190: copy[end] = '\0';
1191:
1192: /* Convert from base64. */
1193: needed = (end / 4) * 3;
1194: out = xmalloc(needed);
1195: if ((outlen = b64_pton(copy, out, len)) == -1) {
1196: free(out);
1197: free(copy);
1198: return (0);
1199: }
1200: free(copy);
1201:
1202: /* Create a new paste buffer. */
1203: log_debug("%s: %.*s", __func__, outlen, out);
1.108 nicm 1204: paste_add(NULL, out, outlen);
1.91 nicm 1205:
1206: return (0);
1207: }
1208:
1209: /*
1.124 nicm 1210: * Handle secondary device attributes input. Returns 0 for success, -1 for
1211: * failure, 1 for partial.
1.91 nicm 1212: */
1213: static int
1214: tty_keys_device_attributes(struct tty *tty, const char *buf, size_t len,
1215: size_t *size)
1216: {
1.118 nicm 1217: struct client *c = tty->client;
1218: u_int i, n = 0;
1219: char tmp[64], *endptr, p[32] = { 0 }, *cp, *next;
1.91 nicm 1220:
1221: *size = 0;
1.120 nicm 1222: if (tty->flags & TTY_HAVEDA)
1223: return (-1);
1.91 nicm 1224:
1.141 nicm 1225: /*
1226: * First three bytes are always \033[>. Some older Terminal.app
1227: * versions respond as for DA (\033[?) so accept and ignore that.
1228: */
1.91 nicm 1229: if (buf[0] != '\033')
1230: return (-1);
1231: if (len == 1)
1232: return (1);
1233: if (buf[1] != '[')
1234: return (-1);
1235: if (len == 2)
1236: return (1);
1.141 nicm 1237: if (buf[2] != '>' && buf[2] != '?')
1.91 nicm 1238: return (-1);
1239: if (len == 3)
1240: return (1);
1241:
1242: /* Copy the rest up to a 'c'. */
1.128 nicm 1243: for (i = 0; i < (sizeof tmp) - 1; i++) {
1.91 nicm 1244: if (3 + i == len)
1245: return (1);
1.128 nicm 1246: if (buf[3 + i] == 'c')
1247: break;
1.91 nicm 1248: tmp[i] = buf[3 + i];
1249: }
1250: if (i == (sizeof tmp) - 1)
1251: return (-1);
1252: tmp[i] = '\0';
1253: *size = 4 + i;
1.141 nicm 1254:
1255: /* Ignore DA response. */
1256: if (buf[2] == '?')
1257: return (0);
1.91 nicm 1258:
1.124 nicm 1259: /* Convert all arguments to numbers. */
1.116 nicm 1260: cp = tmp;
1261: while ((next = strsep(&cp, ";")) != NULL) {
1262: p[n] = strtoul(next, &endptr, 10);
1.117 nicm 1263: if (*endptr != '\0')
1.116 nicm 1264: p[n] = 0;
1265: n++;
1266: }
1.91 nicm 1267:
1.126 nicm 1268: /* Add terminal features. */
1.116 nicm 1269: switch (p[0]) {
1.124 nicm 1270: case 41: /* VT420 */
1.131 nicm 1271: tty_add_features(&c->term_features, "margins,rectfill", ",");
1.91 nicm 1272: break;
1.124 nicm 1273: case 'M': /* mintty */
1.130 nicm 1274: tty_default_features(&c->term_features, "mintty", 0);
1.124 nicm 1275: break;
1.126 nicm 1276: case 'T': /* tmux */
1.130 nicm 1277: tty_default_features(&c->term_features, "tmux", 0);
1.124 nicm 1278: break;
1279: case 'U': /* rxvt-unicode */
1.130 nicm 1280: tty_default_features(&c->term_features, "rxvt-unicode", 0);
1.124 nicm 1281: break;
1.91 nicm 1282: }
1.124 nicm 1283: log_debug("%s: received secondary DA %.*s", c->name, (int)*size, buf);
1.120 nicm 1284:
1.126 nicm 1285: tty_update_features(tty);
1.120 nicm 1286: tty->flags |= TTY_HAVEDA;
1287:
1.119 nicm 1288: return (0);
1289: }
1290:
1291: /*
1.128 nicm 1292: * Handle extended device attributes input. Returns 0 for success, -1 for
1293: * failure, 1 for partial.
1.119 nicm 1294: */
1295: static int
1.128 nicm 1296: tty_keys_extended_device_attributes(struct tty *tty, const char *buf,
1297: size_t len, size_t *size)
1.119 nicm 1298: {
1299: struct client *c = tty->client;
1300: u_int i;
1.130 nicm 1301: char tmp[128];
1.119 nicm 1302:
1303: *size = 0;
1.128 nicm 1304: if (tty->flags & TTY_HAVEXDA)
1.120 nicm 1305: return (-1);
1.119 nicm 1306:
1.128 nicm 1307: /* First four bytes are always \033P>|. */
1.119 nicm 1308: if (buf[0] != '\033')
1309: return (-1);
1310: if (len == 1)
1311: return (1);
1.128 nicm 1312: if (buf[1] != 'P')
1.119 nicm 1313: return (-1);
1314: if (len == 2)
1315: return (1);
1.128 nicm 1316: if (buf[2] != '>')
1.120 nicm 1317: return (-1);
1318: if (len == 3)
1319: return (1);
1.128 nicm 1320: if (buf[3] != '|')
1321: return (-1);
1322: if (len == 4)
1323: return (1);
1.119 nicm 1324:
1.128 nicm 1325: /* Copy the rest up to a '\033\\'. */
1326: for (i = 0; i < (sizeof tmp) - 1; i++) {
1327: if (4 + i == len)
1.119 nicm 1328: return (1);
1.128 nicm 1329: if (buf[4 + i - 1] == '\033' && buf[4 + i] == '\\')
1330: break;
1331: tmp[i] = buf[4 + i];
1.119 nicm 1332: }
1333: if (i == (sizeof tmp) - 1)
1334: return (-1);
1.128 nicm 1335: tmp[i - 1] = '\0';
1336: *size = 5 + i;
1.119 nicm 1337:
1.126 nicm 1338: /* Add terminal features. */
1.130 nicm 1339: if (strncmp(tmp, "iTerm2 ", 7) == 0)
1340: tty_default_features(&c->term_features, "iTerm2", 0);
1341: else if (strncmp(tmp, "tmux ", 5) == 0)
1342: tty_default_features(&c->term_features, "tmux", 0);
1343: else if (strncmp(tmp, "XTerm(", 6) == 0)
1.136 nicm 1344: tty_default_features(&c->term_features, "XTerm", 0);
1.130 nicm 1345: else if (strncmp(tmp, "mintty ", 7) == 0)
1346: tty_default_features(&c->term_features, "mintty", 0);
1.128 nicm 1347: log_debug("%s: received extended DA %.*s", c->name, (int)*size, buf);
1.130 nicm 1348:
1349: free(c->term_type);
1350: c->term_type = xstrdup(tmp);
1.120 nicm 1351:
1.126 nicm 1352: tty_update_features(tty);
1.128 nicm 1353: tty->flags |= TTY_HAVEXDA;
1.120 nicm 1354:
1.22 nicm 1355: return (0);
1.1 nicm 1356: }