Annotation of src/usr.bin/tmux/tty-keys.c, Revision 1.146
1.146 ! nicm 1: /* $OpenBSD: tty-keys.c,v 1.145 2021/04/13 05:23:34 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.144 nicm 874: key_code onlykey;
1.133 nicm 875:
876: *size = 0;
877:
878: /* First two bytes are always \033[. */
879: if (buf[0] != '\033')
880: return (-1);
881: if (len == 1)
882: return (1);
883: if (buf[1] != '[')
884: return (-1);
885: if (len == 2)
886: return (1);
887:
888: /*
889: * Look for a terminator. Stop at either '~' or anything that isn't a
890: * number or ';'.
891: */
892: for (end = 2; end < len && end != sizeof tmp; end++) {
893: if (buf[end] == '~')
894: break;
895: if (!isdigit((u_char)buf[end]) && buf[end] != ';')
896: break;
897: }
898: if (end == len)
899: return (1);
900: if (end == sizeof tmp || (buf[end] != '~' && buf[end] != 'u'))
901: return (-1);
902:
903: /* Copy to the buffer. */
904: memcpy(tmp, buf + 2, end);
905: tmp[end] = '\0';
906:
907: /* Try to parse either form of key. */
908: if (buf[end] == '~') {
909: if (sscanf(tmp, "27;%u;%u", &modifiers, &number) != 2)
910: return (-1);
911: } else {
912: if (sscanf(tmp ,"%u;%u", &number, &modifiers) != 2)
913: return (-1);
914: }
915: *size = end + 1;
916:
1.143 nicm 917: /* Store the key. */
918: bspace = tty->tio.c_cc[VERASE];
919: if (bspace != _POSIX_VDISABLE && number == bspace)
920: nkey = KEYC_BSPACE;
921: else
922: nkey = number;
923:
924: /* Update the modifiers. */
1.133 nicm 925: switch (modifiers) {
926: case 2:
1.143 nicm 927: nkey |= KEYC_SHIFT;
1.133 nicm 928: break;
929: case 3:
1.143 nicm 930: nkey |= (KEYC_META|KEYC_IMPLIED_META);
1.133 nicm 931: break;
932: case 4:
1.143 nicm 933: nkey |= (KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META);
1.133 nicm 934: break;
935: case 5:
1.143 nicm 936: nkey |= KEYC_CTRL;
1.133 nicm 937: break;
938: case 6:
1.143 nicm 939: nkey |= (KEYC_SHIFT|KEYC_CTRL);
1.133 nicm 940: break;
941: case 7:
1.143 nicm 942: nkey |= (KEYC_META|KEYC_CTRL);
1.133 nicm 943: break;
944: case 8:
1.143 nicm 945: nkey |= (KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL);
1.133 nicm 946: break;
947: default:
948: *key = KEYC_NONE;
949: break;
950: }
1.143 nicm 951:
1.144 nicm 952: /*
953: * Don't allow both KEYC_CTRL and as an implied modifier. Also convert
954: * C-X into C-x and so on.
955: */
1.145 nicm 956: if (nkey & KEYC_CTRL) {
1.144 nicm 957: onlykey = (nkey & KEYC_MASK_KEY);
1.146 ! nicm 958: if (onlykey < 32 && onlykey != 9)
! 959: /* nothing */;
! 960: else if (onlykey >= 97 && onlykey <= 122)
! 961: onlykey -= 96;
! 962: else if (onlykey >= 64 && onlykey <= 95)
! 963: onlykey -= 64;
! 964: else if (onlykey == 32)
! 965: onlykey = 0;
! 966: else if (onlykey == 63)
! 967: onlykey = 127;
! 968: else
! 969: onlykey |= KEYC_CTRL;
! 970: nkey = onlykey|((nkey & KEYC_MASK_MODIFIERS) & ~KEYC_CTRL);
1.143 nicm 971: }
972:
1.133 nicm 973: if (log_get_level() != 0) {
974: log_debug("%s: extended key %.*s is %llx (%s)", c->name,
1.143 nicm 975: (int)*size, buf, nkey, key_string_lookup_key(nkey, 1));
1.133 nicm 976: }
1.143 nicm 977: *key = nkey;
1.133 nicm 978: return (0);
1.1 nicm 979: }
980:
1.27 nicm 981: /*
1.22 nicm 982: * Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
983: * (probably a mouse sequence but need more data).
984: */
1.86 nicm 985: static int
1.111 nicm 986: tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size,
987: struct mouse_event *m)
1.1 nicm 988: {
1.111 nicm 989: struct client *c = tty->client;
990: u_int i, x, y, b, sgr_b;
991: u_char sgr_type, ch;
1.33 nicm 992:
1.1 nicm 993: /*
1.33 nicm 994: * Standard mouse sequences are \033[M followed by three characters
1.55 nicm 995: * indicating button, X and Y, all based at 32 with 1,1 top-left.
1.33 nicm 996: *
997: * UTF-8 mouse sequences are similar but the three are expressed as
998: * UTF-8 characters.
1.55 nicm 999: *
1000: * SGR extended mouse sequences are \033[< followed by three numbers in
1001: * decimal and separated by semicolons indicating button, X and Y. A
1002: * trailing 'M' is click or scroll and trailing 'm' release. All are
1003: * based at 0 with 1,1 top-left.
1.1 nicm 1004: */
1005:
1.22 nicm 1006: *size = 0;
1.72 nicm 1007: x = y = b = sgr_b = 0;
1008: sgr_type = ' ';
1.22 nicm 1009:
1.55 nicm 1010: /* First two bytes are always \033[. */
1.22 nicm 1011: if (buf[0] != '\033')
1012: return (-1);
1013: if (len == 1)
1014: return (1);
1015: if (buf[1] != '[')
1016: return (-1);
1017: if (len == 2)
1018: return (1);
1019:
1.55 nicm 1020: /*
1.82 nicm 1021: * Third byte is M in old standard (and UTF-8 extension which we do not
1022: * support), < in SGR extension.
1.55 nicm 1023: */
1024: if (buf[2] == 'M') {
1025: /* Read the three inputs. */
1026: *size = 3;
1027: for (i = 0; i < 3; i++) {
1028: if (len <= *size)
1029: return (1);
1.94 nicm 1030: ch = (u_char)buf[(*size)++];
1.55 nicm 1031: if (i == 0)
1.94 nicm 1032: b = ch;
1.55 nicm 1033: else if (i == 1)
1.94 nicm 1034: x = ch;
1.55 nicm 1035: else
1.94 nicm 1036: y = ch;
1.33 nicm 1037: }
1.94 nicm 1038: log_debug("%s: mouse input: %.*s", c->name, (int)*size, buf);
1.16 nicm 1039:
1.55 nicm 1040: /* Check and return the mouse input. */
1.60 nicm 1041: if (b < 32)
1.55 nicm 1042: return (-1);
1043: b -= 32;
1.60 nicm 1044: if (x >= 33)
1045: x -= 33;
1046: else
1047: x = 256 - x;
1048: if (y >= 33)
1049: y -= 33;
1050: else
1051: y = 256 - y;
1.55 nicm 1052: } else if (buf[2] == '<') {
1053: /* Read the three inputs. */
1054: *size = 3;
1055: while (1) {
1056: if (len <= *size)
1057: return (1);
1.94 nicm 1058: ch = (u_char)buf[(*size)++];
1059: if (ch == ';')
1.55 nicm 1060: break;
1.94 nicm 1061: if (ch < '0' || ch > '9')
1.55 nicm 1062: return (-1);
1.94 nicm 1063: sgr_b = 10 * sgr_b + (ch - '0');
1.55 nicm 1064: }
1065: while (1) {
1066: if (len <= *size)
1067: return (1);
1.94 nicm 1068: ch = (u_char)buf[(*size)++];
1069: if (ch == ';')
1.55 nicm 1070: break;
1.94 nicm 1071: if (ch < '0' || ch > '9')
1.55 nicm 1072: return (-1);
1.94 nicm 1073: x = 10 * x + (ch - '0');
1.55 nicm 1074: }
1075: while (1) {
1076: if (len <= *size)
1077: return (1);
1.94 nicm 1078: ch = (u_char)buf[(*size)++];
1079: if (ch == 'M' || ch == 'm')
1.55 nicm 1080: break;
1.94 nicm 1081: if (ch < '0' || ch > '9')
1.55 nicm 1082: return (-1);
1.94 nicm 1083: y = 10 * y + (ch - '0');
1.55 nicm 1084: }
1.94 nicm 1085: log_debug("%s: mouse input (SGR): %.*s", c->name, (int)*size,
1086: buf);
1.1 nicm 1087:
1.55 nicm 1088: /* Check and return the mouse input. */
1089: if (x < 1 || y < 1)
1090: return (-1);
1091: x--;
1092: y--;
1.72 nicm 1093: b = sgr_b;
1094:
1095: /* Type is M for press, m for release. */
1.94 nicm 1096: sgr_type = ch;
1.72 nicm 1097: if (sgr_type == 'm')
1098: b |= 3;
1.67 nicm 1099:
1100: /*
1101: * Some terminals (like PuTTY 0.63) mistakenly send
1102: * button-release events for scroll-wheel button-press event.
1103: * Discard it before it reaches any program running inside
1104: * tmux.
1105: */
1.72 nicm 1106: if (sgr_type == 'm' && (sgr_b & 64))
1.67 nicm 1107: return (-2);
1.55 nicm 1108: } else
1.22 nicm 1109: return (-1);
1.45 nicm 1110:
1.72 nicm 1111: /* Fill mouse event. */
1.111 nicm 1112: m->lx = tty->mouse_last_x;
1.61 nicm 1113: m->x = x;
1.111 nicm 1114: m->ly = tty->mouse_last_y;
1.61 nicm 1115: m->y = y;
1.114 nicm 1116: m->lb = tty->mouse_last_b;
1.72 nicm 1117: m->b = b;
1118: m->sgr_type = sgr_type;
1119: m->sgr_b = sgr_b;
1.111 nicm 1120:
1121: /* Update last mouse state. */
1122: tty->mouse_last_x = x;
1123: tty->mouse_last_y = y;
1.114 nicm 1124: tty->mouse_last_b = b;
1.104 nicm 1125:
1126: return (0);
1127: }
1128:
1129: /*
1130: * Handle OSC 52 clipboard input. Returns 0 for success, -1 for failure, 1 for
1131: * partial.
1132: */
1133: static int
1134: tty_keys_clipboard(__unused struct tty *tty, const char *buf, size_t len,
1135: size_t *size)
1136: {
1137: size_t end, terminator, needed;
1138: char *copy, *out;
1139: int outlen;
1140:
1141: *size = 0;
1142:
1.128 nicm 1143: /* First five bytes are always \033]52;. */
1.104 nicm 1144: if (buf[0] != '\033')
1145: return (-1);
1146: if (len == 1)
1147: return (1);
1148: if (buf[1] != ']')
1149: return (-1);
1150: if (len == 2)
1151: return (1);
1152: if (buf[2] != '5')
1153: return (-1);
1154: if (len == 3)
1155: return (1);
1156: if (buf[3] != '2')
1157: return (-1);
1158: if (len == 4)
1159: return (1);
1160: if (buf[4] != ';')
1161: return (-1);
1162: if (len == 5)
1163: return (1);
1164:
1165: /* Find the terminator if any. */
1166: for (end = 5; end < len; end++) {
1167: if (buf[end] == '\007') {
1168: terminator = 1;
1169: break;
1170: }
1171: if (end > 5 && buf[end - 1] == '\033' && buf[end] == '\\') {
1172: terminator = 2;
1173: break;
1174: }
1175: }
1176: if (end == len)
1177: return (1);
1178: *size = end + terminator;
1179:
1180: /* Skip the initial part. */
1181: buf += 5;
1182: end -= 5;
1183:
1184: /* Get the second argument. */
1185: while (end != 0 && *buf != ';') {
1186: buf++;
1187: end--;
1188: }
1189: if (end == 0 || end == 1)
1190: return (0);
1191: buf++;
1192: end--;
1193:
1194: /* It has to be a string so copy it. */
1195: copy = xmalloc(end + 1);
1196: memcpy(copy, buf, end);
1197: copy[end] = '\0';
1198:
1199: /* Convert from base64. */
1200: needed = (end / 4) * 3;
1201: out = xmalloc(needed);
1202: if ((outlen = b64_pton(copy, out, len)) == -1) {
1203: free(out);
1204: free(copy);
1205: return (0);
1206: }
1207: free(copy);
1208:
1209: /* Create a new paste buffer. */
1210: log_debug("%s: %.*s", __func__, outlen, out);
1.108 nicm 1211: paste_add(NULL, out, outlen);
1.91 nicm 1212:
1213: return (0);
1214: }
1215:
1216: /*
1.124 nicm 1217: * Handle secondary device attributes input. Returns 0 for success, -1 for
1218: * failure, 1 for partial.
1.91 nicm 1219: */
1220: static int
1221: tty_keys_device_attributes(struct tty *tty, const char *buf, size_t len,
1222: size_t *size)
1223: {
1.118 nicm 1224: struct client *c = tty->client;
1225: u_int i, n = 0;
1226: char tmp[64], *endptr, p[32] = { 0 }, *cp, *next;
1.91 nicm 1227:
1228: *size = 0;
1.120 nicm 1229: if (tty->flags & TTY_HAVEDA)
1230: return (-1);
1.91 nicm 1231:
1.141 nicm 1232: /*
1233: * First three bytes are always \033[>. Some older Terminal.app
1234: * versions respond as for DA (\033[?) so accept and ignore that.
1235: */
1.91 nicm 1236: if (buf[0] != '\033')
1237: return (-1);
1238: if (len == 1)
1239: return (1);
1240: if (buf[1] != '[')
1241: return (-1);
1242: if (len == 2)
1243: return (1);
1.141 nicm 1244: if (buf[2] != '>' && buf[2] != '?')
1.91 nicm 1245: return (-1);
1246: if (len == 3)
1247: return (1);
1248:
1249: /* Copy the rest up to a 'c'. */
1.128 nicm 1250: for (i = 0; i < (sizeof tmp) - 1; i++) {
1.91 nicm 1251: if (3 + i == len)
1252: return (1);
1.128 nicm 1253: if (buf[3 + i] == 'c')
1254: break;
1.91 nicm 1255: tmp[i] = buf[3 + i];
1256: }
1257: if (i == (sizeof tmp) - 1)
1258: return (-1);
1259: tmp[i] = '\0';
1260: *size = 4 + i;
1.141 nicm 1261:
1262: /* Ignore DA response. */
1263: if (buf[2] == '?')
1264: return (0);
1.91 nicm 1265:
1.124 nicm 1266: /* Convert all arguments to numbers. */
1.116 nicm 1267: cp = tmp;
1268: while ((next = strsep(&cp, ";")) != NULL) {
1269: p[n] = strtoul(next, &endptr, 10);
1.117 nicm 1270: if (*endptr != '\0')
1.116 nicm 1271: p[n] = 0;
1272: n++;
1273: }
1.91 nicm 1274:
1.126 nicm 1275: /* Add terminal features. */
1.116 nicm 1276: switch (p[0]) {
1.124 nicm 1277: case 41: /* VT420 */
1.131 nicm 1278: tty_add_features(&c->term_features, "margins,rectfill", ",");
1.91 nicm 1279: break;
1.124 nicm 1280: case 'M': /* mintty */
1.130 nicm 1281: tty_default_features(&c->term_features, "mintty", 0);
1.124 nicm 1282: break;
1.126 nicm 1283: case 'T': /* tmux */
1.130 nicm 1284: tty_default_features(&c->term_features, "tmux", 0);
1.124 nicm 1285: break;
1286: case 'U': /* rxvt-unicode */
1.130 nicm 1287: tty_default_features(&c->term_features, "rxvt-unicode", 0);
1.124 nicm 1288: break;
1.91 nicm 1289: }
1.124 nicm 1290: log_debug("%s: received secondary DA %.*s", c->name, (int)*size, buf);
1.120 nicm 1291:
1.126 nicm 1292: tty_update_features(tty);
1.120 nicm 1293: tty->flags |= TTY_HAVEDA;
1294:
1.119 nicm 1295: return (0);
1296: }
1297:
1298: /*
1.128 nicm 1299: * Handle extended device attributes input. Returns 0 for success, -1 for
1300: * failure, 1 for partial.
1.119 nicm 1301: */
1302: static int
1.128 nicm 1303: tty_keys_extended_device_attributes(struct tty *tty, const char *buf,
1304: size_t len, size_t *size)
1.119 nicm 1305: {
1306: struct client *c = tty->client;
1307: u_int i;
1.130 nicm 1308: char tmp[128];
1.119 nicm 1309:
1310: *size = 0;
1.128 nicm 1311: if (tty->flags & TTY_HAVEXDA)
1.120 nicm 1312: return (-1);
1.119 nicm 1313:
1.128 nicm 1314: /* First four bytes are always \033P>|. */
1.119 nicm 1315: if (buf[0] != '\033')
1316: return (-1);
1317: if (len == 1)
1318: return (1);
1.128 nicm 1319: if (buf[1] != 'P')
1.119 nicm 1320: return (-1);
1321: if (len == 2)
1322: return (1);
1.128 nicm 1323: if (buf[2] != '>')
1.120 nicm 1324: return (-1);
1325: if (len == 3)
1326: return (1);
1.128 nicm 1327: if (buf[3] != '|')
1328: return (-1);
1329: if (len == 4)
1330: return (1);
1.119 nicm 1331:
1.128 nicm 1332: /* Copy the rest up to a '\033\\'. */
1333: for (i = 0; i < (sizeof tmp) - 1; i++) {
1334: if (4 + i == len)
1.119 nicm 1335: return (1);
1.128 nicm 1336: if (buf[4 + i - 1] == '\033' && buf[4 + i] == '\\')
1337: break;
1338: tmp[i] = buf[4 + i];
1.119 nicm 1339: }
1340: if (i == (sizeof tmp) - 1)
1341: return (-1);
1.128 nicm 1342: tmp[i - 1] = '\0';
1343: *size = 5 + i;
1.119 nicm 1344:
1.126 nicm 1345: /* Add terminal features. */
1.130 nicm 1346: if (strncmp(tmp, "iTerm2 ", 7) == 0)
1347: tty_default_features(&c->term_features, "iTerm2", 0);
1348: else if (strncmp(tmp, "tmux ", 5) == 0)
1349: tty_default_features(&c->term_features, "tmux", 0);
1350: else if (strncmp(tmp, "XTerm(", 6) == 0)
1.136 nicm 1351: tty_default_features(&c->term_features, "XTerm", 0);
1.130 nicm 1352: else if (strncmp(tmp, "mintty ", 7) == 0)
1353: tty_default_features(&c->term_features, "mintty", 0);
1.128 nicm 1354: log_debug("%s: received extended DA %.*s", c->name, (int)*size, buf);
1.130 nicm 1355:
1356: free(c->term_type);
1357: c->term_type = xstrdup(tmp);
1.120 nicm 1358:
1.126 nicm 1359: tty_update_features(tty);
1.128 nicm 1360: tty->flags |= TTY_HAVEXDA;
1.120 nicm 1361:
1.22 nicm 1362: return (0);
1.1 nicm 1363: }