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