Annotation of src/usr.bin/tmux/tty-keys.c, Revision 1.138
1.138 ! nicm 1: /* $OpenBSD: tty-keys.c,v 1.137 2020/05/22 15:43:38 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;
639:
640: gettimeofday(&tv, NULL);
1.1 nicm 641:
1.51 nicm 642: /* Get key buffer. */
1.93 nicm 643: buf = EVBUFFER_DATA(tty->in);
644: len = EVBUFFER_LENGTH(tty->in);
1.1 nicm 645: if (len == 0)
1.14 nicm 646: return (0);
1.94 nicm 647: log_debug("%s: keys are %zu (%.*s)", c->name, len, (int)len, buf);
1.1 nicm 648:
1.104 nicm 649: /* Is this a clipboard response? */
650: switch (tty_keys_clipboard(tty, buf, len, &size)) {
651: case 0: /* yes */
652: key = KEYC_UNKNOWN;
653: goto complete_key;
654: case -1: /* no, or not valid */
655: break;
656: case 1: /* partial */
657: goto partial_key;
658: }
659:
1.91 nicm 660: /* Is this a device attributes response? */
661: switch (tty_keys_device_attributes(tty, buf, len, &size)) {
662: case 0: /* yes */
663: key = KEYC_UNKNOWN;
664: goto complete_key;
665: case -1: /* no, or not valid */
666: break;
667: case 1: /* partial */
668: goto partial_key;
669: }
670:
1.128 nicm 671: /* Is this an extended device attributes response? */
672: switch (tty_keys_extended_device_attributes(tty, buf, len, &size)) {
1.119 nicm 673: case 0: /* yes */
674: key = KEYC_UNKNOWN;
675: goto complete_key;
676: case -1: /* no, or not valid */
677: break;
678: case 1: /* partial */
679: goto partial_key;
680: }
681:
1.26 nicm 682: /* Is this a mouse key press? */
1.111 nicm 683: switch (tty_keys_mouse(tty, buf, len, &size, &m)) {
1.22 nicm 684: case 0: /* yes */
685: key = KEYC_MOUSE;
1.51 nicm 686: goto complete_key;
1.22 nicm 687: case -1: /* no, or not valid */
1.24 nicm 688: break;
1.67 nicm 689: case -2: /* yes, but we don't care. */
1.73 nicm 690: key = KEYC_MOUSE;
1.67 nicm 691: goto discard_key;
1.22 nicm 692: case 1: /* partial */
693: goto partial_key;
1.11 nicm 694: }
695:
1.133 nicm 696: /* Is this an extended key press? */
697: switch (tty_keys_extended_key(tty, buf, len, &size, &key)) {
698: case 0: /* yes */
699: goto complete_key;
700: case -1: /* no, or not valid */
701: break;
702: case 1: /* partial */
703: goto partial_key;
704: }
705:
1.86 nicm 706: first_key:
1.101 nicm 707: /* Try to lookup complete key. */
708: n = tty_keys_next1(tty, buf, len, &key, &size, expired);
709: if (n == 0) /* found */
710: goto complete_key;
711: if (n == 1)
712: goto partial_key;
713:
714: /*
715: * If not a complete key, look for key with an escape prefix (meta
716: * modifier).
717: */
1.105 nicm 718: if (*buf == '\033' && len > 1) {
1.87 nicm 719: /* Look for a key without the escape. */
720: n = tty_keys_next1(tty, buf + 1, len - 1, &key, &size, expired);
721: if (n == 0) { /* found */
1.135 nicm 722: if (key & KEYC_IMPLIED_META) {
1.97 nicm 723: /*
724: * We want the escape key as well as the xterm
725: * key, because the xterm sequence implicitly
726: * includes the escape (so if we see
727: * \033\033[1;3D we know it is an Escape
728: * followed by M-Left, not just M-Left).
729: */
730: key = '\033';
731: size = 1;
732: goto complete_key;
733: }
1.134 nicm 734: key |= KEYC_META;
1.87 nicm 735: size++;
736: goto complete_key;
737: }
738: if (n == 1) /* partial */
1.62 nicm 739: goto partial_key;
1.89 nicm 740: }
1.1 nicm 741:
1.89 nicm 742: /*
743: * At this point, we know the key is not partial (with or without
744: * escape). So pass it through even if the timer has not expired.
745: */
746: if (*buf == '\033' && len >= 2) {
1.134 nicm 747: key = (u_char)buf[1] | KEYC_META;
1.89 nicm 748: size = 2;
749: } else {
750: key = (u_char)buf[0];
751: size = 1;
1.76 nicm 752: }
1.51 nicm 753: goto complete_key;
754:
755: partial_key:
1.94 nicm 756: log_debug("%s: partial key %.*s", c->name, (int)len, buf);
1.14 nicm 757:
1.51 nicm 758: /* If timer is going, check for expiration. */
759: if (tty->flags & TTY_TIMER) {
760: if (evtimer_initialized(&tty->key_timer) &&
1.59 nicm 761: !evtimer_pending(&tty->key_timer, NULL)) {
762: expired = 1;
1.51 nicm 763: goto first_key;
1.59 nicm 764: }
1.39 nicm 765: return (0);
1.51 nicm 766: }
1.39 nicm 767:
1.51 nicm 768: /* Get the time period. */
1.75 nicm 769: delay = options_get_number(global_options, "escape-time");
1.28 nicm 770: tv.tv_sec = delay / 1000;
771: tv.tv_usec = (delay % 1000) * 1000L;
1.27 nicm 772:
1.51 nicm 773: /* Start the timer. */
1.36 nicm 774: if (event_initialized(&tty->key_timer))
775: evtimer_del(&tty->key_timer);
1.14 nicm 776: evtimer_set(&tty->key_timer, tty_keys_callback, tty);
777: evtimer_add(&tty->key_timer, &tv);
1.27 nicm 778:
1.51 nicm 779: tty->flags |= TTY_TIMER;
1.14 nicm 780: return (0);
1.1 nicm 781:
1.51 nicm 782: complete_key:
1.94 nicm 783: log_debug("%s: complete key %.*s %#llx", c->name, (int)size, buf, key);
1.88 nicm 784:
785: /*
786: * Check for backspace key using termios VERASE - the terminfo
787: * kbs entry is extremely unreliable, so cannot be safely
788: * used. termios should have a better idea.
789: */
790: bspace = tty->tio.c_cc[VERASE];
791: if (bspace != _POSIX_VDISABLE && (key & KEYC_MASK_KEY) == bspace)
1.135 nicm 792: key = (key & KEYC_MASK_MODIFIERS)|KEYC_BSPACE;
1.16 nicm 793:
1.51 nicm 794: /* Remove data from buffer. */
1.93 nicm 795: evbuffer_drain(tty->in, size);
1.16 nicm 796:
1.51 nicm 797: /* Remove key timer. */
1.36 nicm 798: if (event_initialized(&tty->key_timer))
799: evtimer_del(&tty->key_timer);
1.51 nicm 800: tty->flags &= ~TTY_TIMER;
1.56 nicm 801:
802: /* Check for focus events. */
1.137 nicm 803: if (key == KEYC_FOCUS_OUT)
1.56 nicm 804: tty->client->flags &= ~CLIENT_FOCUSED;
1.137 nicm 805: else if (key == KEYC_FOCUS_IN)
1.56 nicm 806: tty->client->flags |= CLIENT_FOCUSED;
1.16 nicm 807:
1.51 nicm 808: /* Fire the key. */
1.111 nicm 809: if (key != KEYC_UNKNOWN) {
810: event = xmalloc(sizeof *event);
811: event->key = key;
812: memcpy(&event->m, &m, sizeof event->m);
1.113 nicm 813: if (!server_client_handle_key(c, event))
814: free(event);
1.111 nicm 815: }
1.16 nicm 816:
1.14 nicm 817: return (1);
1.67 nicm 818:
819: discard_key:
1.94 nicm 820: log_debug("%s: discard key %.*s %#llx", c->name, (int)size, buf, key);
1.67 nicm 821:
822: /* Remove data from buffer. */
1.93 nicm 823: evbuffer_drain(tty->in, size);
1.67 nicm 824:
825: return (1);
1.14 nicm 826: }
827:
1.16 nicm 828: /* Key timer callback. */
1.86 nicm 829: static void
1.81 nicm 830: tty_keys_callback(__unused int fd, __unused short events, void *data)
1.14 nicm 831: {
832: struct tty *tty = data;
833:
1.51 nicm 834: if (tty->flags & TTY_TIMER) {
835: while (tty_keys_next(tty))
836: ;
837: }
1.133 nicm 838: }
839:
840: /*
841: * Handle extended key input. This has two forms: \033[27;m;k~ and \033[k;mu,
842: * where k is key as a number and m is a modifier. Returns 0 for success, -1
843: * for failure, 1 for partial;
844: */
845: static int
846: tty_keys_extended_key(struct tty *tty, const char *buf, size_t len,
847: size_t *size, key_code *key)
848: {
849: struct client *c = tty->client;
850: size_t end;
851: u_int number, modifiers;
852: char tmp[64];
853:
854: *size = 0;
855:
856: /* First two bytes are always \033[. */
857: if (buf[0] != '\033')
858: return (-1);
859: if (len == 1)
860: return (1);
861: if (buf[1] != '[')
862: return (-1);
863: if (len == 2)
864: return (1);
865:
866: /*
867: * Look for a terminator. Stop at either '~' or anything that isn't a
868: * number or ';'.
869: */
870: for (end = 2; end < len && end != sizeof tmp; end++) {
871: if (buf[end] == '~')
872: break;
873: if (!isdigit((u_char)buf[end]) && buf[end] != ';')
874: break;
875: }
876: if (end == len)
877: return (1);
878: if (end == sizeof tmp || (buf[end] != '~' && buf[end] != 'u'))
879: return (-1);
880:
881: /* Copy to the buffer. */
882: memcpy(tmp, buf + 2, end);
883: tmp[end] = '\0';
884:
885: /* Try to parse either form of key. */
886: if (buf[end] == '~') {
887: if (sscanf(tmp, "27;%u;%u", &modifiers, &number) != 2)
888: return (-1);
889: } else {
890: if (sscanf(tmp ,"%u;%u", &number, &modifiers) != 2)
891: return (-1);
892: }
893: *size = end + 1;
894:
895: /* Store the key and modifiers. */
1.135 nicm 896: *key = number;
1.133 nicm 897: switch (modifiers) {
898: case 2:
899: (*key) |= KEYC_SHIFT;
900: break;
901: case 3:
1.135 nicm 902: (*key) |= (KEYC_META|KEYC_IMPLIED_META);
1.133 nicm 903: break;
904: case 4:
1.135 nicm 905: (*key) |= (KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META);
1.133 nicm 906: break;
907: case 5:
908: (*key) |= KEYC_CTRL;
909: break;
910: case 6:
911: (*key) |= (KEYC_SHIFT|KEYC_CTRL);
912: break;
913: case 7:
1.134 nicm 914: (*key) |= (KEYC_META|KEYC_CTRL);
1.133 nicm 915: break;
916: case 8:
1.135 nicm 917: (*key) |= (KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL);
1.133 nicm 918: break;
919: default:
920: *key = KEYC_NONE;
921: break;
922: }
923: if (log_get_level() != 0) {
924: log_debug("%s: extended key %.*s is %llx (%s)", c->name,
1.135 nicm 925: (int)*size, buf, *key, key_string_lookup_key(*key, 1));
1.133 nicm 926: }
927: return (0);
1.1 nicm 928: }
929:
1.27 nicm 930: /*
1.22 nicm 931: * Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
932: * (probably a mouse sequence but need more data).
933: */
1.86 nicm 934: static int
1.111 nicm 935: tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size,
936: struct mouse_event *m)
1.1 nicm 937: {
1.111 nicm 938: struct client *c = tty->client;
939: u_int i, x, y, b, sgr_b;
940: u_char sgr_type, ch;
1.33 nicm 941:
1.1 nicm 942: /*
1.33 nicm 943: * Standard mouse sequences are \033[M followed by three characters
1.55 nicm 944: * indicating button, X and Y, all based at 32 with 1,1 top-left.
1.33 nicm 945: *
946: * UTF-8 mouse sequences are similar but the three are expressed as
947: * UTF-8 characters.
1.55 nicm 948: *
949: * SGR extended mouse sequences are \033[< followed by three numbers in
950: * decimal and separated by semicolons indicating button, X and Y. A
951: * trailing 'M' is click or scroll and trailing 'm' release. All are
952: * based at 0 with 1,1 top-left.
1.1 nicm 953: */
954:
1.22 nicm 955: *size = 0;
1.72 nicm 956: x = y = b = sgr_b = 0;
957: sgr_type = ' ';
1.22 nicm 958:
1.55 nicm 959: /* First two bytes are always \033[. */
1.22 nicm 960: if (buf[0] != '\033')
961: return (-1);
962: if (len == 1)
963: return (1);
964: if (buf[1] != '[')
965: return (-1);
966: if (len == 2)
967: return (1);
968:
1.55 nicm 969: /*
1.82 nicm 970: * Third byte is M in old standard (and UTF-8 extension which we do not
971: * support), < in SGR extension.
1.55 nicm 972: */
973: if (buf[2] == 'M') {
974: /* Read the three inputs. */
975: *size = 3;
976: for (i = 0; i < 3; i++) {
977: if (len <= *size)
978: return (1);
1.94 nicm 979: ch = (u_char)buf[(*size)++];
1.55 nicm 980: if (i == 0)
1.94 nicm 981: b = ch;
1.55 nicm 982: else if (i == 1)
1.94 nicm 983: x = ch;
1.55 nicm 984: else
1.94 nicm 985: y = ch;
1.33 nicm 986: }
1.94 nicm 987: log_debug("%s: mouse input: %.*s", c->name, (int)*size, buf);
1.16 nicm 988:
1.55 nicm 989: /* Check and return the mouse input. */
1.60 nicm 990: if (b < 32)
1.55 nicm 991: return (-1);
992: b -= 32;
1.60 nicm 993: if (x >= 33)
994: x -= 33;
995: else
996: x = 256 - x;
997: if (y >= 33)
998: y -= 33;
999: else
1000: y = 256 - y;
1.55 nicm 1001: } else if (buf[2] == '<') {
1002: /* Read the three inputs. */
1003: *size = 3;
1004: while (1) {
1005: if (len <= *size)
1006: return (1);
1.94 nicm 1007: ch = (u_char)buf[(*size)++];
1008: if (ch == ';')
1.55 nicm 1009: break;
1.94 nicm 1010: if (ch < '0' || ch > '9')
1.55 nicm 1011: return (-1);
1.94 nicm 1012: sgr_b = 10 * sgr_b + (ch - '0');
1.55 nicm 1013: }
1014: while (1) {
1015: if (len <= *size)
1016: return (1);
1.94 nicm 1017: ch = (u_char)buf[(*size)++];
1018: if (ch == ';')
1.55 nicm 1019: break;
1.94 nicm 1020: if (ch < '0' || ch > '9')
1.55 nicm 1021: return (-1);
1.94 nicm 1022: x = 10 * x + (ch - '0');
1.55 nicm 1023: }
1024: while (1) {
1025: if (len <= *size)
1026: return (1);
1.94 nicm 1027: ch = (u_char)buf[(*size)++];
1028: if (ch == 'M' || ch == 'm')
1.55 nicm 1029: break;
1.94 nicm 1030: if (ch < '0' || ch > '9')
1.55 nicm 1031: return (-1);
1.94 nicm 1032: y = 10 * y + (ch - '0');
1.55 nicm 1033: }
1.94 nicm 1034: log_debug("%s: mouse input (SGR): %.*s", c->name, (int)*size,
1035: buf);
1.1 nicm 1036:
1.55 nicm 1037: /* Check and return the mouse input. */
1038: if (x < 1 || y < 1)
1039: return (-1);
1040: x--;
1041: y--;
1.72 nicm 1042: b = sgr_b;
1043:
1044: /* Type is M for press, m for release. */
1.94 nicm 1045: sgr_type = ch;
1.72 nicm 1046: if (sgr_type == 'm')
1047: b |= 3;
1.67 nicm 1048:
1049: /*
1050: * Some terminals (like PuTTY 0.63) mistakenly send
1051: * button-release events for scroll-wheel button-press event.
1052: * Discard it before it reaches any program running inside
1053: * tmux.
1054: */
1.72 nicm 1055: if (sgr_type == 'm' && (sgr_b & 64))
1.67 nicm 1056: return (-2);
1.55 nicm 1057: } else
1.22 nicm 1058: return (-1);
1.45 nicm 1059:
1.72 nicm 1060: /* Fill mouse event. */
1.111 nicm 1061: m->lx = tty->mouse_last_x;
1.61 nicm 1062: m->x = x;
1.111 nicm 1063: m->ly = tty->mouse_last_y;
1.61 nicm 1064: m->y = y;
1.114 nicm 1065: m->lb = tty->mouse_last_b;
1.72 nicm 1066: m->b = b;
1067: m->sgr_type = sgr_type;
1068: m->sgr_b = sgr_b;
1.111 nicm 1069:
1070: /* Update last mouse state. */
1071: tty->mouse_last_x = x;
1072: tty->mouse_last_y = y;
1.114 nicm 1073: tty->mouse_last_b = b;
1.104 nicm 1074:
1075: return (0);
1076: }
1077:
1078: /*
1079: * Handle OSC 52 clipboard input. Returns 0 for success, -1 for failure, 1 for
1080: * partial.
1081: */
1082: static int
1083: tty_keys_clipboard(__unused struct tty *tty, const char *buf, size_t len,
1084: size_t *size)
1085: {
1086: size_t end, terminator, needed;
1087: char *copy, *out;
1088: int outlen;
1089:
1090: *size = 0;
1091:
1.128 nicm 1092: /* First five bytes are always \033]52;. */
1.104 nicm 1093: if (buf[0] != '\033')
1094: return (-1);
1095: if (len == 1)
1096: return (1);
1097: if (buf[1] != ']')
1098: return (-1);
1099: if (len == 2)
1100: return (1);
1101: if (buf[2] != '5')
1102: return (-1);
1103: if (len == 3)
1104: return (1);
1105: if (buf[3] != '2')
1106: return (-1);
1107: if (len == 4)
1108: return (1);
1109: if (buf[4] != ';')
1110: return (-1);
1111: if (len == 5)
1112: return (1);
1113:
1114: /* Find the terminator if any. */
1115: for (end = 5; end < len; end++) {
1116: if (buf[end] == '\007') {
1117: terminator = 1;
1118: break;
1119: }
1120: if (end > 5 && buf[end - 1] == '\033' && buf[end] == '\\') {
1121: terminator = 2;
1122: break;
1123: }
1124: }
1125: if (end == len)
1126: return (1);
1127: *size = end + terminator;
1128:
1129: /* Skip the initial part. */
1130: buf += 5;
1131: end -= 5;
1132:
1133: /* Get the second argument. */
1134: while (end != 0 && *buf != ';') {
1135: buf++;
1136: end--;
1137: }
1138: if (end == 0 || end == 1)
1139: return (0);
1140: buf++;
1141: end--;
1142:
1143: /* It has to be a string so copy it. */
1144: copy = xmalloc(end + 1);
1145: memcpy(copy, buf, end);
1146: copy[end] = '\0';
1147:
1148: /* Convert from base64. */
1149: needed = (end / 4) * 3;
1150: out = xmalloc(needed);
1151: if ((outlen = b64_pton(copy, out, len)) == -1) {
1152: free(out);
1153: free(copy);
1154: return (0);
1155: }
1156: free(copy);
1157:
1158: /* Create a new paste buffer. */
1159: log_debug("%s: %.*s", __func__, outlen, out);
1.108 nicm 1160: paste_add(NULL, out, outlen);
1.91 nicm 1161:
1162: return (0);
1163: }
1164:
1165: /*
1.124 nicm 1166: * Handle secondary device attributes input. Returns 0 for success, -1 for
1167: * failure, 1 for partial.
1.91 nicm 1168: */
1169: static int
1170: tty_keys_device_attributes(struct tty *tty, const char *buf, size_t len,
1171: size_t *size)
1172: {
1.118 nicm 1173: struct client *c = tty->client;
1174: u_int i, n = 0;
1175: char tmp[64], *endptr, p[32] = { 0 }, *cp, *next;
1.91 nicm 1176:
1177: *size = 0;
1.120 nicm 1178: if (tty->flags & TTY_HAVEDA)
1179: return (-1);
1.91 nicm 1180:
1181: /* First three bytes are always \033[?. */
1182: if (buf[0] != '\033')
1183: return (-1);
1184: if (len == 1)
1185: return (1);
1186: if (buf[1] != '[')
1187: return (-1);
1188: if (len == 2)
1189: return (1);
1.124 nicm 1190: if (buf[2] != '>')
1.91 nicm 1191: return (-1);
1192: if (len == 3)
1193: return (1);
1194:
1195: /* Copy the rest up to a 'c'. */
1.128 nicm 1196: for (i = 0; i < (sizeof tmp) - 1; i++) {
1.91 nicm 1197: if (3 + i == len)
1198: return (1);
1.128 nicm 1199: if (buf[3 + i] == 'c')
1200: break;
1.91 nicm 1201: tmp[i] = buf[3 + i];
1202: }
1203: if (i == (sizeof tmp) - 1)
1204: return (-1);
1205: tmp[i] = '\0';
1206: *size = 4 + i;
1207:
1.124 nicm 1208: /* Convert all arguments to numbers. */
1.116 nicm 1209: cp = tmp;
1210: while ((next = strsep(&cp, ";")) != NULL) {
1211: p[n] = strtoul(next, &endptr, 10);
1.117 nicm 1212: if (*endptr != '\0')
1.116 nicm 1213: p[n] = 0;
1214: n++;
1215: }
1.91 nicm 1216:
1.126 nicm 1217: /* Add terminal features. */
1.116 nicm 1218: switch (p[0]) {
1.124 nicm 1219: case 41: /* VT420 */
1.131 nicm 1220: tty_add_features(&c->term_features, "margins,rectfill", ",");
1.91 nicm 1221: break;
1.124 nicm 1222: case 'M': /* mintty */
1.130 nicm 1223: tty_default_features(&c->term_features, "mintty", 0);
1.124 nicm 1224: break;
1.126 nicm 1225: case 'T': /* tmux */
1.130 nicm 1226: tty_default_features(&c->term_features, "tmux", 0);
1.124 nicm 1227: break;
1228: case 'U': /* rxvt-unicode */
1.130 nicm 1229: tty_default_features(&c->term_features, "rxvt-unicode", 0);
1.124 nicm 1230: break;
1.91 nicm 1231: }
1.124 nicm 1232: log_debug("%s: received secondary DA %.*s", c->name, (int)*size, buf);
1.120 nicm 1233:
1.126 nicm 1234: tty_update_features(tty);
1.120 nicm 1235: tty->flags |= TTY_HAVEDA;
1236:
1.119 nicm 1237: return (0);
1238: }
1239:
1240: /*
1.128 nicm 1241: * Handle extended device attributes input. Returns 0 for success, -1 for
1242: * failure, 1 for partial.
1.119 nicm 1243: */
1244: static int
1.128 nicm 1245: tty_keys_extended_device_attributes(struct tty *tty, const char *buf,
1246: size_t len, size_t *size)
1.119 nicm 1247: {
1248: struct client *c = tty->client;
1249: u_int i;
1.130 nicm 1250: char tmp[128];
1.119 nicm 1251:
1252: *size = 0;
1.128 nicm 1253: if (tty->flags & TTY_HAVEXDA)
1.120 nicm 1254: return (-1);
1.119 nicm 1255:
1.128 nicm 1256: /* First four bytes are always \033P>|. */
1.119 nicm 1257: if (buf[0] != '\033')
1258: return (-1);
1259: if (len == 1)
1260: return (1);
1.128 nicm 1261: if (buf[1] != 'P')
1.119 nicm 1262: return (-1);
1263: if (len == 2)
1264: return (1);
1.128 nicm 1265: if (buf[2] != '>')
1.120 nicm 1266: return (-1);
1267: if (len == 3)
1268: return (1);
1.128 nicm 1269: if (buf[3] != '|')
1270: return (-1);
1271: if (len == 4)
1272: return (1);
1.119 nicm 1273:
1.128 nicm 1274: /* Copy the rest up to a '\033\\'. */
1275: for (i = 0; i < (sizeof tmp) - 1; i++) {
1276: if (4 + i == len)
1.119 nicm 1277: return (1);
1.128 nicm 1278: if (buf[4 + i - 1] == '\033' && buf[4 + i] == '\\')
1279: break;
1280: tmp[i] = buf[4 + i];
1.119 nicm 1281: }
1282: if (i == (sizeof tmp) - 1)
1283: return (-1);
1.128 nicm 1284: tmp[i - 1] = '\0';
1285: *size = 5 + i;
1.119 nicm 1286:
1.126 nicm 1287: /* Add terminal features. */
1.130 nicm 1288: if (strncmp(tmp, "iTerm2 ", 7) == 0)
1289: tty_default_features(&c->term_features, "iTerm2", 0);
1290: else if (strncmp(tmp, "tmux ", 5) == 0)
1291: tty_default_features(&c->term_features, "tmux", 0);
1292: else if (strncmp(tmp, "XTerm(", 6) == 0)
1.136 nicm 1293: tty_default_features(&c->term_features, "XTerm", 0);
1.130 nicm 1294: else if (strncmp(tmp, "mintty ", 7) == 0)
1295: tty_default_features(&c->term_features, "mintty", 0);
1.128 nicm 1296: log_debug("%s: received extended DA %.*s", c->name, (int)*size, buf);
1.130 nicm 1297:
1298: free(c->term_type);
1299: c->term_type = xstrdup(tmp);
1.120 nicm 1300:
1.126 nicm 1301: tty_update_features(tty);
1.128 nicm 1302: tty->flags |= TTY_HAVEXDA;
1.120 nicm 1303:
1.22 nicm 1304: return (0);
1.1 nicm 1305: }