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