Keep cursor on selected item on menu (useful for blind people), GitHub
[tmux-openbsd.git] / tty-keys.c
blob4a712d42dc9e71e7599c7f6988a505dc4615dd07
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 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 arrow 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 function keys. */
154 { "\033[11~", KEYC_F1 },
155 { "\033[12~", KEYC_F2 },
156 { "\033[13~", KEYC_F3 },
157 { "\033[14~", KEYC_F4 },
158 { "\033[15~", KEYC_F5 },
159 { "\033[17~", KEYC_F6 },
160 { "\033[18~", KEYC_F7 },
161 { "\033[19~", KEYC_F8 },
162 { "\033[20~", KEYC_F9 },
163 { "\033[21~", KEYC_F10 },
165 { "\033[23~", KEYC_F1|KEYC_SHIFT },
166 { "\033[24~", KEYC_F2|KEYC_SHIFT },
167 { "\033[25~", KEYC_F3|KEYC_SHIFT },
168 { "\033[26~", KEYC_F4|KEYC_SHIFT },
169 { "\033[28~", KEYC_F5|KEYC_SHIFT },
170 { "\033[29~", KEYC_F6|KEYC_SHIFT },
171 { "\033[31~", KEYC_F7|KEYC_SHIFT },
172 { "\033[32~", KEYC_F8|KEYC_SHIFT },
173 { "\033[33~", KEYC_F9|KEYC_SHIFT },
174 { "\033[34~", KEYC_F10|KEYC_SHIFT },
175 { "\033[23$", KEYC_F11|KEYC_SHIFT },
176 { "\033[24$", KEYC_F12|KEYC_SHIFT },
178 { "\033[11^", KEYC_F1|KEYC_CTRL },
179 { "\033[12^", KEYC_F2|KEYC_CTRL },
180 { "\033[13^", KEYC_F3|KEYC_CTRL },
181 { "\033[14^", KEYC_F4|KEYC_CTRL },
182 { "\033[15^", KEYC_F5|KEYC_CTRL },
183 { "\033[17^", KEYC_F6|KEYC_CTRL },
184 { "\033[18^", KEYC_F7|KEYC_CTRL },
185 { "\033[19^", KEYC_F8|KEYC_CTRL },
186 { "\033[20^", KEYC_F9|KEYC_CTRL },
187 { "\033[21^", KEYC_F10|KEYC_CTRL },
188 { "\033[23^", KEYC_F11|KEYC_CTRL },
189 { "\033[24^", KEYC_F12|KEYC_CTRL },
191 { "\033[11@", KEYC_F1|KEYC_CTRL|KEYC_SHIFT },
192 { "\033[12@", KEYC_F2|KEYC_CTRL|KEYC_SHIFT },
193 { "\033[13@", KEYC_F3|KEYC_CTRL|KEYC_SHIFT },
194 { "\033[14@", KEYC_F4|KEYC_CTRL|KEYC_SHIFT },
195 { "\033[15@", KEYC_F5|KEYC_CTRL|KEYC_SHIFT },
196 { "\033[17@", KEYC_F6|KEYC_CTRL|KEYC_SHIFT },
197 { "\033[18@", KEYC_F7|KEYC_CTRL|KEYC_SHIFT },
198 { "\033[19@", KEYC_F8|KEYC_CTRL|KEYC_SHIFT },
199 { "\033[20@", KEYC_F9|KEYC_CTRL|KEYC_SHIFT },
200 { "\033[21@", KEYC_F10|KEYC_CTRL|KEYC_SHIFT },
201 { "\033[23@", KEYC_F11|KEYC_CTRL|KEYC_SHIFT },
202 { "\033[24@", KEYC_F12|KEYC_CTRL|KEYC_SHIFT },
204 /* Focus tracking. */
205 { "\033[I", KEYC_FOCUS_IN },
206 { "\033[O", KEYC_FOCUS_OUT },
208 /* Paste keys. */
209 { "\033[200~", KEYC_PASTE_START },
210 { "\033[201~", KEYC_PASTE_END },
213 /* Default xterm keys. */
214 struct tty_default_key_xterm {
215 const char *template;
216 key_code key;
218 static const struct tty_default_key_xterm tty_default_xterm_keys[] = {
219 { "\033[1;_P", KEYC_F1 },
220 { "\033O1;_P", KEYC_F1 },
221 { "\033O_P", KEYC_F1 },
222 { "\033[1;_Q", KEYC_F2 },
223 { "\033O1;_Q", KEYC_F2 },
224 { "\033O_Q", KEYC_F2 },
225 { "\033[1;_R", KEYC_F3 },
226 { "\033O1;_R", KEYC_F3 },
227 { "\033O_R", KEYC_F3 },
228 { "\033[1;_S", KEYC_F4 },
229 { "\033O1;_S", KEYC_F4 },
230 { "\033O_S", KEYC_F4 },
231 { "\033[15;_~", KEYC_F5 },
232 { "\033[17;_~", KEYC_F6 },
233 { "\033[18;_~", KEYC_F7 },
234 { "\033[19;_~", KEYC_F8 },
235 { "\033[20;_~", KEYC_F9 },
236 { "\033[21;_~", KEYC_F10 },
237 { "\033[23;_~", KEYC_F11 },
238 { "\033[24;_~", KEYC_F12 },
239 { "\033[1;_A", KEYC_UP },
240 { "\033[1;_B", KEYC_DOWN },
241 { "\033[1;_C", KEYC_RIGHT },
242 { "\033[1;_D", KEYC_LEFT },
243 { "\033[1;_H", KEYC_HOME },
244 { "\033[1;_F", KEYC_END },
245 { "\033[5;_~", KEYC_PPAGE },
246 { "\033[6;_~", KEYC_NPAGE },
247 { "\033[2;_~", KEYC_IC },
248 { "\033[3;_~", KEYC_DC },
250 static const key_code tty_default_xterm_modifiers[] = {
253 KEYC_SHIFT,
254 KEYC_META|KEYC_IMPLIED_META,
255 KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META,
256 KEYC_CTRL,
257 KEYC_SHIFT|KEYC_CTRL,
258 KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL,
259 KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL,
260 KEYC_META|KEYC_IMPLIED_META
264 * Default terminfo(5) keys. Any keys that have builtin modifiers (that is,
265 * where the key itself contains the modifiers) has the KEYC_XTERM flag set so
266 * a leading escape is not treated as meta (and probably removed).
268 struct tty_default_key_code {
269 enum tty_code_code code;
270 key_code key;
272 static const struct tty_default_key_code tty_default_code_keys[] = {
273 /* Function keys. */
274 { TTYC_KF1, KEYC_F1 },
275 { TTYC_KF2, KEYC_F2 },
276 { TTYC_KF3, KEYC_F3 },
277 { TTYC_KF4, KEYC_F4 },
278 { TTYC_KF5, KEYC_F5 },
279 { TTYC_KF6, KEYC_F6 },
280 { TTYC_KF7, KEYC_F7 },
281 { TTYC_KF8, KEYC_F8 },
282 { TTYC_KF9, KEYC_F9 },
283 { TTYC_KF10, KEYC_F10 },
284 { TTYC_KF11, KEYC_F11 },
285 { TTYC_KF12, KEYC_F12 },
287 { TTYC_KF13, KEYC_F1|KEYC_SHIFT },
288 { TTYC_KF14, KEYC_F2|KEYC_SHIFT },
289 { TTYC_KF15, KEYC_F3|KEYC_SHIFT },
290 { TTYC_KF16, KEYC_F4|KEYC_SHIFT },
291 { TTYC_KF17, KEYC_F5|KEYC_SHIFT },
292 { TTYC_KF18, KEYC_F6|KEYC_SHIFT },
293 { TTYC_KF19, KEYC_F7|KEYC_SHIFT },
294 { TTYC_KF20, KEYC_F8|KEYC_SHIFT },
295 { TTYC_KF21, KEYC_F9|KEYC_SHIFT },
296 { TTYC_KF22, KEYC_F10|KEYC_SHIFT },
297 { TTYC_KF23, KEYC_F11|KEYC_SHIFT },
298 { TTYC_KF24, KEYC_F12|KEYC_SHIFT },
300 { TTYC_KF25, KEYC_F1|KEYC_CTRL },
301 { TTYC_KF26, KEYC_F2|KEYC_CTRL },
302 { TTYC_KF27, KEYC_F3|KEYC_CTRL },
303 { TTYC_KF28, KEYC_F4|KEYC_CTRL },
304 { TTYC_KF29, KEYC_F5|KEYC_CTRL },
305 { TTYC_KF30, KEYC_F6|KEYC_CTRL },
306 { TTYC_KF31, KEYC_F7|KEYC_CTRL },
307 { TTYC_KF32, KEYC_F8|KEYC_CTRL },
308 { TTYC_KF33, KEYC_F9|KEYC_CTRL },
309 { TTYC_KF34, KEYC_F10|KEYC_CTRL },
310 { TTYC_KF35, KEYC_F11|KEYC_CTRL },
311 { TTYC_KF36, KEYC_F12|KEYC_CTRL },
313 { TTYC_KF37, KEYC_F1|KEYC_SHIFT|KEYC_CTRL },
314 { TTYC_KF38, KEYC_F2|KEYC_SHIFT|KEYC_CTRL },
315 { TTYC_KF39, KEYC_F3|KEYC_SHIFT|KEYC_CTRL },
316 { TTYC_KF40, KEYC_F4|KEYC_SHIFT|KEYC_CTRL },
317 { TTYC_KF41, KEYC_F5|KEYC_SHIFT|KEYC_CTRL },
318 { TTYC_KF42, KEYC_F6|KEYC_SHIFT|KEYC_CTRL },
319 { TTYC_KF43, KEYC_F7|KEYC_SHIFT|KEYC_CTRL },
320 { TTYC_KF44, KEYC_F8|KEYC_SHIFT|KEYC_CTRL },
321 { TTYC_KF45, KEYC_F9|KEYC_SHIFT|KEYC_CTRL },
322 { TTYC_KF46, KEYC_F10|KEYC_SHIFT|KEYC_CTRL },
323 { TTYC_KF47, KEYC_F11|KEYC_SHIFT|KEYC_CTRL },
324 { TTYC_KF48, KEYC_F12|KEYC_SHIFT|KEYC_CTRL },
326 { TTYC_KF49, KEYC_F1|KEYC_META|KEYC_IMPLIED_META },
327 { TTYC_KF50, KEYC_F2|KEYC_META|KEYC_IMPLIED_META },
328 { TTYC_KF51, KEYC_F3|KEYC_META|KEYC_IMPLIED_META },
329 { TTYC_KF52, KEYC_F4|KEYC_META|KEYC_IMPLIED_META },
330 { TTYC_KF53, KEYC_F5|KEYC_META|KEYC_IMPLIED_META },
331 { TTYC_KF54, KEYC_F6|KEYC_META|KEYC_IMPLIED_META },
332 { TTYC_KF55, KEYC_F7|KEYC_META|KEYC_IMPLIED_META },
333 { TTYC_KF56, KEYC_F8|KEYC_META|KEYC_IMPLIED_META },
334 { TTYC_KF57, KEYC_F9|KEYC_META|KEYC_IMPLIED_META },
335 { TTYC_KF58, KEYC_F10|KEYC_META|KEYC_IMPLIED_META },
336 { TTYC_KF59, KEYC_F11|KEYC_META|KEYC_IMPLIED_META },
337 { TTYC_KF60, KEYC_F12|KEYC_META|KEYC_IMPLIED_META },
339 { TTYC_KF61, KEYC_F1|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
340 { TTYC_KF62, KEYC_F2|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
341 { TTYC_KF63, KEYC_F3|KEYC_META|KEYC_IMPLIED_META|KEYC_SHIFT },
343 { TTYC_KICH1, KEYC_IC },
344 { TTYC_KDCH1, KEYC_DC },
345 { TTYC_KHOME, KEYC_HOME },
346 { TTYC_KEND, KEYC_END },
347 { TTYC_KNP, KEYC_NPAGE },
348 { TTYC_KPP, KEYC_PPAGE },
349 { TTYC_KCBT, KEYC_BTAB },
351 /* Arrow keys from terminfo. */
352 { TTYC_KCUU1, KEYC_UP|KEYC_CURSOR },
353 { TTYC_KCUD1, KEYC_DOWN|KEYC_CURSOR },
354 { TTYC_KCUB1, KEYC_LEFT|KEYC_CURSOR },
355 { TTYC_KCUF1, KEYC_RIGHT|KEYC_CURSOR },
357 /* Key and modifier capabilities. */
358 { TTYC_KDC2, KEYC_DC|KEYC_SHIFT },
359 { TTYC_KDC3, KEYC_DC|KEYC_META|KEYC_IMPLIED_META },
360 { TTYC_KDC4, KEYC_DC|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
361 { TTYC_KDC5, KEYC_DC|KEYC_CTRL },
362 { TTYC_KDC6, KEYC_DC|KEYC_SHIFT|KEYC_CTRL },
363 { TTYC_KDC7, KEYC_DC|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
364 { TTYC_KIND, KEYC_DOWN|KEYC_SHIFT },
365 { TTYC_KDN2, KEYC_DOWN|KEYC_SHIFT },
366 { TTYC_KDN3, KEYC_DOWN|KEYC_META|KEYC_IMPLIED_META },
367 { TTYC_KDN4, KEYC_DOWN|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
368 { TTYC_KDN5, KEYC_DOWN|KEYC_CTRL },
369 { TTYC_KDN6, KEYC_DOWN|KEYC_SHIFT|KEYC_CTRL },
370 { TTYC_KDN7, KEYC_DOWN|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
371 { TTYC_KEND2, KEYC_END|KEYC_SHIFT },
372 { TTYC_KEND3, KEYC_END|KEYC_META|KEYC_IMPLIED_META },
373 { TTYC_KEND4, KEYC_END|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
374 { TTYC_KEND5, KEYC_END|KEYC_CTRL },
375 { TTYC_KEND6, KEYC_END|KEYC_SHIFT|KEYC_CTRL },
376 { TTYC_KEND7, KEYC_END|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
377 { TTYC_KHOM2, KEYC_HOME|KEYC_SHIFT },
378 { TTYC_KHOM3, KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
379 { TTYC_KHOM4, KEYC_HOME|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
380 { TTYC_KHOM5, KEYC_HOME|KEYC_CTRL },
381 { TTYC_KHOM6, KEYC_HOME|KEYC_SHIFT|KEYC_CTRL },
382 { TTYC_KHOM7, KEYC_HOME|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
383 { TTYC_KIC2, KEYC_IC|KEYC_SHIFT },
384 { TTYC_KIC3, KEYC_IC|KEYC_META|KEYC_IMPLIED_META },
385 { TTYC_KIC4, KEYC_IC|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
386 { TTYC_KIC5, KEYC_IC|KEYC_CTRL },
387 { TTYC_KIC6, KEYC_IC|KEYC_SHIFT|KEYC_CTRL },
388 { TTYC_KIC7, KEYC_IC|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
389 { TTYC_KLFT2, KEYC_LEFT|KEYC_SHIFT },
390 { TTYC_KLFT3, KEYC_LEFT|KEYC_META|KEYC_IMPLIED_META },
391 { TTYC_KLFT4, KEYC_LEFT|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
392 { TTYC_KLFT5, KEYC_LEFT|KEYC_CTRL },
393 { TTYC_KLFT6, KEYC_LEFT|KEYC_SHIFT|KEYC_CTRL },
394 { TTYC_KLFT7, KEYC_LEFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
395 { TTYC_KNXT2, KEYC_NPAGE|KEYC_SHIFT },
396 { TTYC_KNXT3, KEYC_NPAGE|KEYC_META|KEYC_IMPLIED_META },
397 { TTYC_KNXT4, KEYC_NPAGE|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
398 { TTYC_KNXT5, KEYC_NPAGE|KEYC_CTRL },
399 { TTYC_KNXT6, KEYC_NPAGE|KEYC_SHIFT|KEYC_CTRL },
400 { TTYC_KNXT7, KEYC_NPAGE|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
401 { TTYC_KPRV2, KEYC_PPAGE|KEYC_SHIFT },
402 { TTYC_KPRV3, KEYC_PPAGE|KEYC_META|KEYC_IMPLIED_META },
403 { TTYC_KPRV4, KEYC_PPAGE|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
404 { TTYC_KPRV5, KEYC_PPAGE|KEYC_CTRL },
405 { TTYC_KPRV6, KEYC_PPAGE|KEYC_SHIFT|KEYC_CTRL },
406 { TTYC_KPRV7, KEYC_PPAGE|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
407 { TTYC_KRIT2, KEYC_RIGHT|KEYC_SHIFT },
408 { TTYC_KRIT3, KEYC_RIGHT|KEYC_META|KEYC_IMPLIED_META },
409 { TTYC_KRIT4, KEYC_RIGHT|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
410 { TTYC_KRIT5, KEYC_RIGHT|KEYC_CTRL },
411 { TTYC_KRIT6, KEYC_RIGHT|KEYC_SHIFT|KEYC_CTRL },
412 { TTYC_KRIT7, KEYC_RIGHT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
413 { TTYC_KRI, KEYC_UP|KEYC_SHIFT },
414 { TTYC_KUP2, KEYC_UP|KEYC_SHIFT },
415 { TTYC_KUP3, KEYC_UP|KEYC_META|KEYC_IMPLIED_META },
416 { TTYC_KUP4, KEYC_UP|KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META },
417 { TTYC_KUP5, KEYC_UP|KEYC_CTRL },
418 { TTYC_KUP6, KEYC_UP|KEYC_SHIFT|KEYC_CTRL },
419 { TTYC_KUP7, KEYC_UP|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL },
422 /* Add key to tree. */
423 static void
424 tty_keys_add(struct tty *tty, const char *s, key_code key)
426 struct tty_key *tk;
427 size_t size;
428 const char *keystr;
430 keystr = key_string_lookup_key(key, 1);
431 if ((tk = tty_keys_find(tty, s, strlen(s), &size)) == NULL) {
432 log_debug("new key %s: 0x%llx (%s)", s, key, keystr);
433 tty_keys_add1(&tty->key_tree, s, key);
434 } else {
435 log_debug("replacing key %s: 0x%llx (%s)", s, key, keystr);
436 tk->key = key;
440 /* Add next node to the tree. */
441 static void
442 tty_keys_add1(struct tty_key **tkp, const char *s, key_code key)
444 struct tty_key *tk;
446 /* Allocate a tree entry if there isn't one already. */
447 tk = *tkp;
448 if (tk == NULL) {
449 tk = *tkp = xcalloc(1, sizeof *tk);
450 tk->ch = *s;
451 tk->key = KEYC_UNKNOWN;
454 /* Find the next entry. */
455 if (*s == tk->ch) {
456 /* Move forward in string. */
457 s++;
459 /* If this is the end of the string, no more is necessary. */
460 if (*s == '\0') {
461 tk->key = key;
462 return;
465 /* Use the child tree for the next character. */
466 tkp = &tk->next;
467 } else {
468 if (*s < tk->ch)
469 tkp = &tk->left;
470 else if (*s > tk->ch)
471 tkp = &tk->right;
474 /* And recurse to add it. */
475 tty_keys_add1(tkp, s, key);
478 /* Initialise a key tree from the table. */
479 void
480 tty_keys_build(struct tty *tty)
482 const struct tty_default_key_raw *tdkr;
483 const struct tty_default_key_xterm *tdkx;
484 const struct tty_default_key_code *tdkc;
485 u_int i, j;
486 const char *s;
487 struct options_entry *o;
488 struct options_array_item *a;
489 union options_value *ov;
490 char copy[16];
491 key_code key;
493 if (tty->key_tree != NULL)
494 tty_keys_free(tty);
495 tty->key_tree = NULL;
497 for (i = 0; i < nitems(tty_default_xterm_keys); i++) {
498 tdkx = &tty_default_xterm_keys[i];
499 for (j = 2; j < nitems(tty_default_xterm_modifiers); j++) {
500 strlcpy(copy, tdkx->template, sizeof copy);
501 copy[strcspn(copy, "_")] = '0' + j;
503 key = tdkx->key|tty_default_xterm_modifiers[j];
504 tty_keys_add(tty, copy, key);
507 for (i = 0; i < nitems(tty_default_raw_keys); i++) {
508 tdkr = &tty_default_raw_keys[i];
510 s = tdkr->string;
511 if (*s != '\0')
512 tty_keys_add(tty, s, tdkr->key);
514 for (i = 0; i < nitems(tty_default_code_keys); i++) {
515 tdkc = &tty_default_code_keys[i];
517 s = tty_term_string(tty->term, tdkc->code);
518 if (*s != '\0')
519 tty_keys_add(tty, s, tdkc->key);
523 o = options_get(global_options, "user-keys");
524 if (o != NULL) {
525 a = options_array_first(o);
526 while (a != NULL) {
527 i = options_array_item_index(a);
528 ov = options_array_item_value(a);
529 tty_keys_add(tty, ov->string, KEYC_USER + i);
530 a = options_array_next(a);
535 /* Free the entire key tree. */
536 void
537 tty_keys_free(struct tty *tty)
539 tty_keys_free1(tty->key_tree);
542 /* Free a single key. */
543 static void
544 tty_keys_free1(struct tty_key *tk)
546 if (tk->next != NULL)
547 tty_keys_free1(tk->next);
548 if (tk->left != NULL)
549 tty_keys_free1(tk->left);
550 if (tk->right != NULL)
551 tty_keys_free1(tk->right);
552 free(tk);
555 /* Lookup a key in the tree. */
556 static struct tty_key *
557 tty_keys_find(struct tty *tty, const char *buf, size_t len, size_t *size)
559 *size = 0;
560 return (tty_keys_find1(tty->key_tree, buf, len, size));
563 /* Find the next node. */
564 static struct tty_key *
565 tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size)
567 /* If no data, no match. */
568 if (len == 0)
569 return (NULL);
571 /* If the node is NULL, this is the end of the tree. No match. */
572 if (tk == NULL)
573 return (NULL);
575 /* Pick the next in the sequence. */
576 if (tk->ch == *buf) {
577 /* Move forward in the string. */
578 buf++; len--;
579 (*size)++;
581 /* At the end of the string, return the current node. */
582 if (len == 0 || (tk->next == NULL && tk->key != KEYC_UNKNOWN))
583 return (tk);
585 /* Move into the next tree for the following character. */
586 tk = tk->next;
587 } else {
588 if (*buf < tk->ch)
589 tk = tk->left;
590 else if (*buf > tk->ch)
591 tk = tk->right;
594 /* Move to the next in the tree. */
595 return (tty_keys_find1(tk, buf, len, size));
598 /* Look up part of the next key. */
599 static int
600 tty_keys_next1(struct tty *tty, const char *buf, size_t len, key_code *key,
601 size_t *size, int expired)
603 struct client *c = tty->client;
604 struct tty_key *tk, *tk1;
605 struct utf8_data ud;
606 enum utf8_state more;
607 utf8_char uc;
608 u_int i;
610 log_debug("%s: next key is %zu (%.*s) (expired=%d)", c->name, len,
611 (int)len, buf, expired);
613 /* Is this a known key? */
614 tk = tty_keys_find(tty, buf, len, size);
615 if (tk != NULL && tk->key != KEYC_UNKNOWN) {
616 tk1 = tk;
618 log_debug("%s: keys in list: %#llx", c->name, tk1->key);
619 while ((tk1 = tk1->next) != NULL);
620 if (tk->next != NULL && !expired)
621 return (1);
622 *key = tk->key;
623 return (0);
626 /* Is this valid UTF-8? */
627 more = utf8_open(&ud, (u_char)*buf);
628 if (more == UTF8_MORE) {
629 *size = ud.size;
630 if (len < ud.size) {
631 if (!expired)
632 return (1);
633 return (-1);
635 for (i = 1; i < ud.size; i++)
636 more = utf8_append(&ud, (u_char)buf[i]);
637 if (more != UTF8_DONE)
638 return (-1);
640 if (utf8_from_data(&ud, &uc) != UTF8_DONE)
641 return (-1);
642 *key = uc;
644 log_debug("%s: UTF-8 key %.*s %#llx", c->name, (int)ud.size,
645 ud.data, *key);
646 return (0);
649 return (-1);
652 /* Process at least one key in the buffer. Return 0 if no keys present. */
654 tty_keys_next(struct tty *tty)
656 struct client *c = tty->client;
657 struct timeval tv;
658 const char *buf;
659 size_t len, size;
660 cc_t bspace;
661 int delay, expired = 0, n;
662 key_code key;
663 struct mouse_event m = { 0 };
664 struct key_event *event;
666 /* Get key buffer. */
667 buf = EVBUFFER_DATA(tty->in);
668 len = EVBUFFER_LENGTH(tty->in);
669 if (len == 0)
670 return (0);
671 log_debug("%s: keys are %zu (%.*s)", c->name, len, (int)len, buf);
673 /* Is this a clipboard response? */
674 switch (tty_keys_clipboard(tty, buf, len, &size)) {
675 case 0: /* yes */
676 key = KEYC_UNKNOWN;
677 goto complete_key;
678 case -1: /* no, or not valid */
679 break;
680 case 1: /* partial */
681 goto partial_key;
684 /* Is this a device attributes response? */
685 switch (tty_keys_device_attributes(tty, buf, len, &size)) {
686 case 0: /* yes */
687 key = KEYC_UNKNOWN;
688 goto complete_key;
689 case -1: /* no, or not valid */
690 break;
691 case 1: /* partial */
692 goto partial_key;
695 /* Is this an extended device attributes response? */
696 switch (tty_keys_extended_device_attributes(tty, buf, len, &size)) {
697 case 0: /* yes */
698 key = KEYC_UNKNOWN;
699 goto complete_key;
700 case -1: /* no, or not valid */
701 break;
702 case 1: /* partial */
703 goto partial_key;
706 /* Is this a mouse key press? */
707 switch (tty_keys_mouse(tty, buf, len, &size, &m)) {
708 case 0: /* yes */
709 key = KEYC_MOUSE;
710 goto complete_key;
711 case -1: /* no, or not valid */
712 break;
713 case -2: /* yes, but we don't care. */
714 key = KEYC_MOUSE;
715 goto discard_key;
716 case 1: /* partial */
717 goto partial_key;
720 /* Is this an extended key press? */
721 switch (tty_keys_extended_key(tty, buf, len, &size, &key)) {
722 case 0: /* yes */
723 goto complete_key;
724 case -1: /* no, or not valid */
725 break;
726 case 1: /* partial */
727 goto partial_key;
730 first_key:
731 /* Try to lookup complete key. */
732 n = tty_keys_next1(tty, buf, len, &key, &size, expired);
733 if (n == 0) /* found */
734 goto complete_key;
735 if (n == 1)
736 goto partial_key;
739 * If not a complete key, look for key with an escape prefix (meta
740 * modifier).
742 if (*buf == '\033' && len > 1) {
743 /* Look for a key without the escape. */
744 n = tty_keys_next1(tty, buf + 1, len - 1, &key, &size, expired);
745 if (n == 0) { /* found */
746 if (key & KEYC_IMPLIED_META) {
748 * We want the escape key as well as the xterm
749 * key, because the xterm sequence implicitly
750 * includes the escape (so if we see
751 * \033\033[1;3D we know it is an Escape
752 * followed by M-Left, not just M-Left).
754 key = '\033';
755 size = 1;
756 goto complete_key;
758 key |= KEYC_META;
759 size++;
760 goto complete_key;
762 if (n == 1) /* partial */
763 goto partial_key;
767 * At this point, we know the key is not partial (with or without
768 * escape). So pass it through even if the timer has not expired.
770 if (*buf == '\033' && len >= 2) {
771 key = (u_char)buf[1] | KEYC_META;
772 size = 2;
773 } else {
774 key = (u_char)buf[0];
775 size = 1;
777 goto complete_key;
779 partial_key:
780 log_debug("%s: partial key %.*s", c->name, (int)len, buf);
782 /* If timer is going, check for expiration. */
783 if (tty->flags & TTY_TIMER) {
784 if (evtimer_initialized(&tty->key_timer) &&
785 !evtimer_pending(&tty->key_timer, NULL)) {
786 expired = 1;
787 goto first_key;
789 return (0);
792 /* Get the time period. */
793 delay = options_get_number(global_options, "escape-time");
794 if (delay == 0)
795 delay = 1;
796 tv.tv_sec = delay / 1000;
797 tv.tv_usec = (delay % 1000) * 1000L;
799 /* Start the timer. */
800 if (event_initialized(&tty->key_timer))
801 evtimer_del(&tty->key_timer);
802 evtimer_set(&tty->key_timer, tty_keys_callback, tty);
803 evtimer_add(&tty->key_timer, &tv);
805 tty->flags |= TTY_TIMER;
806 return (0);
808 complete_key:
809 log_debug("%s: complete key %.*s %#llx", c->name, (int)size, buf, key);
812 * Check for backspace key using termios VERASE - the terminfo
813 * kbs entry is extremely unreliable, so cannot be safely
814 * used. termios should have a better idea.
816 bspace = tty->tio.c_cc[VERASE];
817 if (bspace != _POSIX_VDISABLE && (key & KEYC_MASK_KEY) == bspace)
818 key = (key & KEYC_MASK_MODIFIERS)|KEYC_BSPACE;
820 /* Remove data from buffer. */
821 evbuffer_drain(tty->in, size);
823 /* Remove key timer. */
824 if (event_initialized(&tty->key_timer))
825 evtimer_del(&tty->key_timer);
826 tty->flags &= ~TTY_TIMER;
828 /* Check for focus events. */
829 if (key == KEYC_FOCUS_OUT) {
830 c->flags &= ~CLIENT_FOCUSED;
831 window_update_focus(c->session->curw->window);
832 notify_client("client-focus-out", c);
833 } else if (key == KEYC_FOCUS_IN) {
834 c->flags |= CLIENT_FOCUSED;
835 notify_client("client-focus-in", c);
836 window_update_focus(c->session->curw->window);
839 /* Fire the key. */
840 if (key != KEYC_UNKNOWN) {
841 event = xmalloc(sizeof *event);
842 event->key = key;
843 memcpy(&event->m, &m, sizeof event->m);
844 if (!server_client_handle_key(c, event))
845 free(event);
848 return (1);
850 discard_key:
851 log_debug("%s: discard key %.*s %#llx", c->name, (int)size, buf, key);
853 /* Remove data from buffer. */
854 evbuffer_drain(tty->in, size);
856 return (1);
859 /* Key timer callback. */
860 static void
861 tty_keys_callback(__unused int fd, __unused short events, void *data)
863 struct tty *tty = data;
865 if (tty->flags & TTY_TIMER) {
866 while (tty_keys_next(tty))
872 * Handle extended key input. This has two forms: \033[27;m;k~ and \033[k;mu,
873 * where k is key as a number and m is a modifier. Returns 0 for success, -1
874 * for failure, 1 for partial;
876 static int
877 tty_keys_extended_key(struct tty *tty, const char *buf, size_t len,
878 size_t *size, key_code *key)
880 struct client *c = tty->client;
881 size_t end;
882 u_int number, modifiers;
883 char tmp[64];
884 cc_t bspace;
885 key_code nkey;
886 key_code onlykey;
888 *size = 0;
890 /* First two bytes are always \033[. */
891 if (buf[0] != '\033')
892 return (-1);
893 if (len == 1)
894 return (1);
895 if (buf[1] != '[')
896 return (-1);
897 if (len == 2)
898 return (1);
901 * Look for a terminator. Stop at either '~' or anything that isn't a
902 * number or ';'.
904 for (end = 2; end < len && end != sizeof tmp; end++) {
905 if (buf[end] == '~')
906 break;
907 if (!isdigit((u_char)buf[end]) && buf[end] != ';')
908 break;
910 if (end == len)
911 return (1);
912 if (end == sizeof tmp || (buf[end] != '~' && buf[end] != 'u'))
913 return (-1);
915 /* Copy to the buffer. */
916 memcpy(tmp, buf + 2, end);
917 tmp[end] = '\0';
919 /* Try to parse either form of key. */
920 if (buf[end] == '~') {
921 if (sscanf(tmp, "27;%u;%u", &modifiers, &number) != 2)
922 return (-1);
923 } else {
924 if (sscanf(tmp ,"%u;%u", &number, &modifiers) != 2)
925 return (-1);
927 *size = end + 1;
929 /* Store the key. */
930 bspace = tty->tio.c_cc[VERASE];
931 if (bspace != _POSIX_VDISABLE && number == bspace)
932 nkey = KEYC_BSPACE;
933 else
934 nkey = number;
936 /* Update the modifiers. */
937 switch (modifiers) {
938 case 2:
939 nkey |= KEYC_SHIFT;
940 break;
941 case 3:
942 nkey |= (KEYC_META|KEYC_IMPLIED_META);
943 break;
944 case 4:
945 nkey |= (KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META);
946 break;
947 case 5:
948 nkey |= KEYC_CTRL;
949 break;
950 case 6:
951 nkey |= (KEYC_SHIFT|KEYC_CTRL);
952 break;
953 case 7:
954 nkey |= (KEYC_META|KEYC_CTRL);
955 break;
956 case 8:
957 nkey |= (KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL);
958 break;
959 case 9:
960 nkey |= (KEYC_META|KEYC_IMPLIED_META);
961 break;
962 default:
963 *key = KEYC_NONE;
964 break;
968 * Don't allow both KEYC_CTRL and as an implied modifier. Also convert
969 * C-X into C-x and so on.
971 if (nkey & KEYC_CTRL) {
972 onlykey = (nkey & KEYC_MASK_KEY);
973 if (onlykey < 32 &&
974 onlykey != 9 &&
975 onlykey != 13 &&
976 onlykey != 27)
977 /* nothing */;
978 else if (onlykey >= 97 && onlykey <= 122)
979 onlykey -= 96;
980 else if (onlykey >= 64 && onlykey <= 95)
981 onlykey -= 64;
982 else if (onlykey == 32)
983 onlykey = 0;
984 else if (onlykey == 63)
985 onlykey = 127;
986 else
987 onlykey |= KEYC_CTRL;
988 nkey = onlykey|((nkey & KEYC_MASK_MODIFIERS) & ~KEYC_CTRL);
991 if (log_get_level() != 0) {
992 log_debug("%s: extended key %.*s is %llx (%s)", c->name,
993 (int)*size, buf, nkey, key_string_lookup_key(nkey, 1));
995 *key = nkey;
996 return (0);
1000 * Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
1001 * (probably a mouse sequence but need more data).
1003 static int
1004 tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size,
1005 struct mouse_event *m)
1007 struct client *c = tty->client;
1008 u_int i, x, y, b, sgr_b;
1009 u_char sgr_type, ch;
1012 * Standard mouse sequences are \033[M followed by three characters
1013 * indicating button, X and Y, all based at 32 with 1,1 top-left.
1015 * UTF-8 mouse sequences are similar but the three are expressed as
1016 * UTF-8 characters.
1018 * SGR extended mouse sequences are \033[< followed by three numbers in
1019 * decimal and separated by semicolons indicating button, X and Y. A
1020 * trailing 'M' is click or scroll and trailing 'm' release. All are
1021 * based at 0 with 1,1 top-left.
1024 *size = 0;
1025 x = y = b = sgr_b = 0;
1026 sgr_type = ' ';
1028 /* First two bytes are always \033[. */
1029 if (buf[0] != '\033')
1030 return (-1);
1031 if (len == 1)
1032 return (1);
1033 if (buf[1] != '[')
1034 return (-1);
1035 if (len == 2)
1036 return (1);
1039 * Third byte is M in old standard (and UTF-8 extension which we do not
1040 * support), < in SGR extension.
1042 if (buf[2] == 'M') {
1043 /* Read the three inputs. */
1044 *size = 3;
1045 for (i = 0; i < 3; i++) {
1046 if (len <= *size)
1047 return (1);
1048 ch = (u_char)buf[(*size)++];
1049 if (i == 0)
1050 b = ch;
1051 else if (i == 1)
1052 x = ch;
1053 else
1054 y = ch;
1056 log_debug("%s: mouse input: %.*s", c->name, (int)*size, buf);
1058 /* Check and return the mouse input. */
1059 if (b < MOUSE_PARAM_BTN_OFF ||
1060 x < MOUSE_PARAM_POS_OFF ||
1061 y < MOUSE_PARAM_POS_OFF)
1062 return (-1);
1063 b -= MOUSE_PARAM_BTN_OFF;
1064 x -= MOUSE_PARAM_POS_OFF;
1065 y -= MOUSE_PARAM_POS_OFF;
1066 } else if (buf[2] == '<') {
1067 /* Read the three inputs. */
1068 *size = 3;
1069 while (1) {
1070 if (len <= *size)
1071 return (1);
1072 ch = (u_char)buf[(*size)++];
1073 if (ch == ';')
1074 break;
1075 if (ch < '0' || ch > '9')
1076 return (-1);
1077 sgr_b = 10 * sgr_b + (ch - '0');
1079 while (1) {
1080 if (len <= *size)
1081 return (1);
1082 ch = (u_char)buf[(*size)++];
1083 if (ch == ';')
1084 break;
1085 if (ch < '0' || ch > '9')
1086 return (-1);
1087 x = 10 * x + (ch - '0');
1089 while (1) {
1090 if (len <= *size)
1091 return (1);
1092 ch = (u_char)buf[(*size)++];
1093 if (ch == 'M' || ch == 'm')
1094 break;
1095 if (ch < '0' || ch > '9')
1096 return (-1);
1097 y = 10 * y + (ch - '0');
1099 log_debug("%s: mouse input (SGR): %.*s", c->name, (int)*size,
1100 buf);
1102 /* Check and return the mouse input. */
1103 if (x < 1 || y < 1)
1104 return (-1);
1105 x--;
1106 y--;
1107 b = sgr_b;
1109 /* Type is M for press, m for release. */
1110 sgr_type = ch;
1111 if (sgr_type == 'm')
1112 b = 3;
1115 * Some terminals (like PuTTY 0.63) mistakenly send
1116 * button-release events for scroll-wheel button-press event.
1117 * Discard it before it reaches any program running inside
1118 * tmux.
1120 if (sgr_type == 'm' && MOUSE_WHEEL(sgr_b))
1121 return (-2);
1122 } else
1123 return (-1);
1125 /* Fill mouse event. */
1126 m->lx = tty->mouse_last_x;
1127 m->x = x;
1128 m->ly = tty->mouse_last_y;
1129 m->y = y;
1130 m->lb = tty->mouse_last_b;
1131 m->b = b;
1132 m->sgr_type = sgr_type;
1133 m->sgr_b = sgr_b;
1135 /* Update last mouse state. */
1136 tty->mouse_last_x = x;
1137 tty->mouse_last_y = y;
1138 tty->mouse_last_b = b;
1140 return (0);
1144 * Handle OSC 52 clipboard input. Returns 0 for success, -1 for failure, 1 for
1145 * partial.
1147 static int
1148 tty_keys_clipboard(struct tty *tty, const char *buf, size_t len, size_t *size)
1150 struct client *c = tty->client;
1151 struct window_pane *wp;
1152 size_t end, terminator, needed;
1153 char *copy, *out;
1154 int outlen;
1155 u_int i;
1157 *size = 0;
1159 /* First five bytes are always \033]52;. */
1160 if (buf[0] != '\033')
1161 return (-1);
1162 if (len == 1)
1163 return (1);
1164 if (buf[1] != ']')
1165 return (-1);
1166 if (len == 2)
1167 return (1);
1168 if (buf[2] != '5')
1169 return (-1);
1170 if (len == 3)
1171 return (1);
1172 if (buf[3] != '2')
1173 return (-1);
1174 if (len == 4)
1175 return (1);
1176 if (buf[4] != ';')
1177 return (-1);
1178 if (len == 5)
1179 return (1);
1181 /* Find the terminator if any. */
1182 for (end = 5; end < len; end++) {
1183 if (buf[end] == '\007') {
1184 terminator = 1;
1185 break;
1187 if (end > 5 && buf[end - 1] == '\033' && buf[end] == '\\') {
1188 terminator = 2;
1189 break;
1192 if (end == len)
1193 return (1);
1194 *size = end + terminator;
1196 /* Skip the initial part. */
1197 buf += 5;
1198 end -= 5;
1200 /* Adjust end so that it points to the start of the terminator. */
1201 end -= terminator - 1;
1203 /* Get the second argument. */
1204 while (end != 0 && *buf != ';') {
1205 buf++;
1206 end--;
1208 if (end == 0 || end == 1)
1209 return (0);
1210 buf++;
1211 end--;
1213 /* If we did not request this, ignore it. */
1214 if (~tty->flags & TTY_OSC52QUERY)
1215 return (0);
1216 tty->flags &= ~TTY_OSC52QUERY;
1217 evtimer_del(&tty->clipboard_timer);
1219 /* It has to be a string so copy it. */
1220 copy = xmalloc(end + 1);
1221 memcpy(copy, buf, end);
1222 copy[end] = '\0';
1224 /* Convert from base64. */
1225 needed = (end / 4) * 3;
1226 out = xmalloc(needed);
1227 if ((outlen = b64_pton(copy, out, len)) == -1) {
1228 free(out);
1229 free(copy);
1230 return (0);
1232 free(copy);
1234 /* Create a new paste buffer and forward to panes. */
1235 log_debug("%s: %.*s", __func__, outlen, out);
1236 if (c->flags & CLIENT_CLIPBOARDBUFFER) {
1237 paste_add(NULL, out, outlen);
1238 c->flags &= ~CLIENT_CLIPBOARDBUFFER;
1240 for (i = 0; i < c->clipboard_npanes; i++) {
1241 wp = window_pane_find_by_id(c->clipboard_panes[i]);
1242 if (wp != NULL)
1243 input_reply_clipboard(wp->event, out, outlen, "\033\\");
1245 free(c->clipboard_panes);
1246 c->clipboard_panes = NULL;
1247 c->clipboard_npanes = 0;
1249 return (0);
1253 * Handle secondary device attributes input. Returns 0 for success, -1 for
1254 * failure, 1 for partial.
1256 static int
1257 tty_keys_device_attributes(struct tty *tty, const char *buf, size_t len,
1258 size_t *size)
1260 struct client *c = tty->client;
1261 u_int i, n = 0;
1262 char tmp[64], *endptr, p[32] = { 0 }, *cp, *next;
1264 *size = 0;
1265 if (tty->flags & TTY_HAVEDA)
1266 return (-1);
1269 * First three bytes are always \033[>. Some older Terminal.app
1270 * versions respond as for DA (\033[?) so accept and ignore that.
1272 if (buf[0] != '\033')
1273 return (-1);
1274 if (len == 1)
1275 return (1);
1276 if (buf[1] != '[')
1277 return (-1);
1278 if (len == 2)
1279 return (1);
1280 if (buf[2] != '>' && buf[2] != '?')
1281 return (-1);
1282 if (len == 3)
1283 return (1);
1285 /* Copy the rest up to a 'c'. */
1286 for (i = 0; i < (sizeof tmp) - 1; i++) {
1287 if (3 + i == len)
1288 return (1);
1289 if (buf[3 + i] == 'c')
1290 break;
1291 tmp[i] = buf[3 + i];
1293 if (i == (sizeof tmp) - 1)
1294 return (-1);
1295 tmp[i] = '\0';
1296 *size = 4 + i;
1298 /* Ignore DA response. */
1299 if (buf[2] == '?')
1300 return (0);
1302 /* Convert all arguments to numbers. */
1303 cp = tmp;
1304 while ((next = strsep(&cp, ";")) != NULL) {
1305 p[n] = strtoul(next, &endptr, 10);
1306 if (*endptr != '\0')
1307 p[n] = 0;
1308 n++;
1311 /* Add terminal features. */
1312 switch (p[0]) {
1313 case 41: /* VT420 */
1314 tty_add_features(&c->term_features, "margins,rectfill", ",");
1315 break;
1316 case 'M': /* mintty */
1317 tty_default_features(&c->term_features, "mintty", 0);
1318 break;
1319 case 'T': /* tmux */
1320 tty_default_features(&c->term_features, "tmux", 0);
1321 break;
1322 case 'U': /* rxvt-unicode */
1323 tty_default_features(&c->term_features, "rxvt-unicode", 0);
1324 break;
1326 log_debug("%s: received secondary DA %.*s", c->name, (int)*size, buf);
1328 tty_update_features(tty);
1329 tty->flags |= TTY_HAVEDA;
1331 return (0);
1335 * Handle extended device attributes input. Returns 0 for success, -1 for
1336 * failure, 1 for partial.
1338 static int
1339 tty_keys_extended_device_attributes(struct tty *tty, const char *buf,
1340 size_t len, size_t *size)
1342 struct client *c = tty->client;
1343 u_int i;
1344 char tmp[128];
1346 *size = 0;
1347 if (tty->flags & TTY_HAVEXDA)
1348 return (-1);
1350 /* First four bytes are always \033P>|. */
1351 if (buf[0] != '\033')
1352 return (-1);
1353 if (len == 1)
1354 return (1);
1355 if (buf[1] != 'P')
1356 return (-1);
1357 if (len == 2)
1358 return (1);
1359 if (buf[2] != '>')
1360 return (-1);
1361 if (len == 3)
1362 return (1);
1363 if (buf[3] != '|')
1364 return (-1);
1365 if (len == 4)
1366 return (1);
1368 /* Copy the rest up to a '\033\\'. */
1369 for (i = 0; i < (sizeof tmp) - 1; i++) {
1370 if (4 + i == len)
1371 return (1);
1372 if (buf[4 + i - 1] == '\033' && buf[4 + i] == '\\')
1373 break;
1374 tmp[i] = buf[4 + i];
1376 if (i == (sizeof tmp) - 1)
1377 return (-1);
1378 tmp[i - 1] = '\0';
1379 *size = 5 + i;
1381 /* Add terminal features. */
1382 if (strncmp(tmp, "iTerm2 ", 7) == 0)
1383 tty_default_features(&c->term_features, "iTerm2", 0);
1384 else if (strncmp(tmp, "tmux ", 5) == 0)
1385 tty_default_features(&c->term_features, "tmux", 0);
1386 else if (strncmp(tmp, "XTerm(", 6) == 0)
1387 tty_default_features(&c->term_features, "XTerm", 0);
1388 else if (strncmp(tmp, "mintty ", 7) == 0)
1389 tty_default_features(&c->term_features, "mintty", 0);
1390 log_debug("%s: received extended DA %.*s", c->name, (int)*size, buf);
1392 free(c->term_type);
1393 c->term_type = xstrdup(tmp);
1395 tty_update_features(tty);
1396 tty->flags |= TTY_HAVEXDA;
1398 return (0);