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