Merge branch 'obsd-master'
[tmux.git] / tty-keys.c
blob8d703fc03aa821e1018687de34aa59c269f73623
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_device_attributes2(struct tty *, const char *, size_t,
59 size_t *);
60 static int tty_keys_extended_device_attributes(struct tty *, const char *,
61 size_t, size_t *);
63 /* A key tree entry. */
64 struct tty_key {
65 char ch;
66 key_code key;
68 struct tty_key *left;
69 struct tty_key *right;
71 struct tty_key *next;
74 /* Default raw keys. */
75 struct tty_default_key_raw {
76 const char *string;
77 key_code key;
79 static const struct tty_default_key_raw tty_default_raw_keys[] = {
80 /* Application escape. */
81 { "\033O[", '\033' },
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.
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 },
105 /* Arrow keys. */
106 { "\033OA", KEYC_UP|KEYC_CURSOR },
107 { "\033OB", KEYC_DOWN|KEYC_CURSOR },
108 { "\033OC", KEYC_RIGHT|KEYC_CURSOR },
109 { "\033OD", KEYC_LEFT|KEYC_CURSOR },
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 },
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.
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 },
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 },
131 /* Other xterm keys. */
132 { "\033OH", KEYC_HOME },
133 { "\033OF", KEYC_END },
135 { "\033\033OH", KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
136 { "\033\033OF", KEYC_END|KEYC_META|KEYC_IMPLIED_META },
138 { "\033[H", KEYC_HOME },
139 { "\033[F", KEYC_END },
141 { "\033\033[H", KEYC_HOME|KEYC_META|KEYC_IMPLIED_META },
142 { "\033\033[F", KEYC_END|KEYC_META|KEYC_IMPLIED_META },
144 /* rxvt arrow keys. */
145 { "\033Oa", KEYC_UP|KEYC_CTRL },
146 { "\033Ob", KEYC_DOWN|KEYC_CTRL },
147 { "\033Oc", KEYC_RIGHT|KEYC_CTRL },
148 { "\033Od", KEYC_LEFT|KEYC_CTRL },
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 },
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 },
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 },
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 },
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 },
206 /* Focus tracking. */
207 { "\033[I", KEYC_FOCUS_IN },
208 { "\033[O", KEYC_FOCUS_OUT },
210 /* Paste keys. */
211 { "\033[200~", KEYC_PASTE_START },
212 { "\033[201~", KEYC_PASTE_END },
214 /* Extended keys. */
215 { "\033[1;5Z", '\011'|KEYC_CTRL|KEYC_SHIFT },
218 /* Default xterm keys. */
219 struct tty_default_key_xterm {
220 const char *template;
221 key_code key;
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 },
255 static const key_code tty_default_xterm_modifiers[] = {
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,
264 KEYC_SHIFT|KEYC_META|KEYC_IMPLIED_META|KEYC_CTRL,
265 KEYC_META|KEYC_IMPLIED_META
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).
273 struct tty_default_key_code {
274 enum tty_code_code code;
275 key_code key;
277 static const struct tty_default_key_code tty_default_code_keys[] = {
278 /* Function keys. */
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 },
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 },
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 },
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 },
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 },
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 },
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 },
356 /* Arrow keys from terminfo. */
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 },
362 /* Key and modifier capabilities. */
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 },
427 /* Add key to tree. */
428 static void
429 tty_keys_add(struct tty *tty, const char *s, key_code key)
431 struct tty_key *tk;
432 size_t size;
433 const char *keystr;
435 keystr = key_string_lookup_key(key, 1);
436 if ((tk = tty_keys_find(tty, s, strlen(s), &size)) == NULL) {
437 log_debug("new key %s: 0x%llx (%s)", s, key, keystr);
438 tty_keys_add1(&tty->key_tree, s, key);
439 } else {
440 log_debug("replacing key %s: 0x%llx (%s)", s, key, keystr);
441 tk->key = key;
445 /* Add next node to the tree. */
446 static void
447 tty_keys_add1(struct tty_key **tkp, const char *s, key_code key)
449 struct tty_key *tk;
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;
456 tk->key = KEYC_UNKNOWN;
459 /* Find the next entry. */
460 if (*s == tk->ch) {
461 /* Move forward in string. */
462 s++;
464 /* If this is the end of the string, no more is necessary. */
465 if (*s == '\0') {
466 tk->key = key;
467 return;
470 /* Use the child tree for the next character. */
471 tkp = &tk->next;
472 } else {
473 if (*s < tk->ch)
474 tkp = &tk->left;
475 else if (*s > tk->ch)
476 tkp = &tk->right;
479 /* And recurse to add it. */
480 tty_keys_add1(tkp, s, key);
483 /* Initialise a key tree from the table. */
484 void
485 tty_keys_build(struct tty *tty)
487 const struct tty_default_key_raw *tdkr;
488 const struct tty_default_key_xterm *tdkx;
489 const struct tty_default_key_code *tdkc;
490 u_int i, j;
491 const char *s;
492 struct options_entry *o;
493 struct options_array_item *a;
494 union options_value *ov;
495 char copy[16];
496 key_code key;
498 if (tty->key_tree != NULL)
499 tty_keys_free(tty);
500 tty->key_tree = NULL;
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;
508 key = tdkx->key|tty_default_xterm_modifiers[j];
509 tty_keys_add(tty, copy, key);
512 for (i = 0; i < nitems(tty_default_raw_keys); i++) {
513 tdkr = &tty_default_raw_keys[i];
515 s = tdkr->string;
516 if (*s != '\0')
517 tty_keys_add(tty, s, tdkr->key);
519 for (i = 0; i < nitems(tty_default_code_keys); i++) {
520 tdkc = &tty_default_code_keys[i];
522 s = tty_term_string(tty->term, tdkc->code);
523 if (*s != '\0')
524 tty_keys_add(tty, s, tdkc->key);
528 o = options_get(global_options, "user-keys");
529 if (o != NULL) {
530 a = options_array_first(o);
531 while (a != NULL) {
532 i = options_array_item_index(a);
533 ov = options_array_item_value(a);
534 tty_keys_add(tty, ov->string, KEYC_USER + i);
535 a = options_array_next(a);
540 /* Free the entire key tree. */
541 void
542 tty_keys_free(struct tty *tty)
544 tty_keys_free1(tty->key_tree);
547 /* Free a single key. */
548 static void
549 tty_keys_free1(struct tty_key *tk)
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);
557 free(tk);
560 /* Lookup a key in the tree. */
561 static struct tty_key *
562 tty_keys_find(struct tty *tty, const char *buf, size_t len, size_t *size)
564 *size = 0;
565 return (tty_keys_find1(tty->key_tree, buf, len, size));
568 /* Find the next node. */
569 static struct tty_key *
570 tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size)
572 /* If no data, no match. */
573 if (len == 0)
574 return (NULL);
576 /* If the node is NULL, this is the end of the tree. No match. */
577 if (tk == NULL)
578 return (NULL);
580 /* Pick the next in the sequence. */
581 if (tk->ch == *buf) {
582 /* Move forward in the string. */
583 buf++; len--;
584 (*size)++;
586 /* At the end of the string, return the current node. */
587 if (len == 0 || (tk->next == NULL && tk->key != KEYC_UNKNOWN))
588 return (tk);
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;
599 /* Move to the next in the tree. */
600 return (tty_keys_find1(tk, buf, len, size));
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,
606 size_t *size, int expired)
608 struct client *c = tty->client;
609 struct tty_key *tk, *tk1;
610 struct utf8_data ud;
611 enum utf8_state more;
612 utf8_char uc;
613 u_int i;
615 log_debug("%s: next key is %zu (%.*s) (expired=%d)", c->name, len,
616 (int)len, buf, expired);
618 /* Is this a known key? */
619 tk = tty_keys_find(tty, buf, len, size);
620 if (tk != NULL && tk->key != KEYC_UNKNOWN) {
621 tk1 = tk;
623 log_debug("%s: keys in list: %#llx", c->name, tk1->key);
624 while ((tk1 = tk1->next) != NULL);
625 if (tk->next != NULL && !expired)
626 return (1);
627 *key = tk->key;
628 return (0);
631 /* Is this valid UTF-8? */
632 more = utf8_open(&ud, (u_char)*buf);
633 if (more == UTF8_MORE) {
634 *size = ud.size;
635 if (len < ud.size) {
636 if (!expired)
637 return (1);
638 return (-1);
640 for (i = 1; i < ud.size; i++)
641 more = utf8_append(&ud, (u_char)buf[i]);
642 if (more != UTF8_DONE)
643 return (-1);
645 if (utf8_from_data(&ud, &uc) != UTF8_DONE)
646 return (-1);
647 *key = uc;
649 log_debug("%s: UTF-8 key %.*s %#llx", c->name, (int)ud.size,
650 ud.data, *key);
651 return (0);
654 return (-1);
657 /* Process window size change escape sequences. */
658 static int
659 tty_keys_winsz(struct tty *tty, const char *buf, size_t len, size_t *size)
661 struct client *c = tty->client;
662 size_t end;
663 char tmp[64];
664 u_int sx, sy, xpixel, ypixel, char_x, char_y;
666 *size = 0;
668 /* If we did not request this, ignore it. */
669 if (!(tty->flags & TTY_WINSIZEQUERY))
670 return (-1);
672 /* First two bytes are always \033[. */
673 if (buf[0] != '\033')
674 return (-1);
675 if (len == 1)
676 return (1);
677 if (buf[1] != '[')
678 return (-1);
679 if (len == 2)
680 return (1);
683 * Stop at either 't' or anything that isn't a
684 * number or ';'.
686 for (end = 2; end < len && end != sizeof tmp; end++) {
687 if (buf[end] == 't')
688 break;
689 if (!isdigit((u_char)buf[end]) && buf[end] != ';')
690 break;
692 if (end == len)
693 return (1);
694 if (end == sizeof tmp || buf[end] != 't')
695 return (-1);
697 /* Copy to the buffer. */
698 memcpy(tmp, buf + 2, end - 2);
699 tmp[end - 2] = '\0';
701 /* Try to parse the window size sequence. */
702 if (sscanf(tmp, "8;%u;%u", &sy, &sx) == 2) {
703 /* Window size in characters. */
704 tty_set_size(tty, sx, sy, tty->xpixel, tty->ypixel);
706 *size = end + 1;
707 return (0);
708 } else if (sscanf(tmp, "4;%u;%u", &ypixel, &xpixel) == 2) {
709 /* Window size in pixels. */
710 char_x = (xpixel && tty->sx) ? xpixel / tty->sx : 0;
711 char_y = (ypixel && tty->sy) ? ypixel / tty->sy : 0;
712 tty_set_size(tty, tty->sx, tty->sy, char_x, char_y);
713 tty_invalidate(tty);
715 tty->flags &= ~TTY_WINSIZEQUERY;
716 *size = end + 1;
717 return (0);
720 log_debug("%s: unrecognized window size sequence: %s", c->name, tmp);
721 return (-1);
725 /* Process at least one key in the buffer. Return 0 if no keys present. */
727 tty_keys_next(struct tty *tty)
729 struct client *c = tty->client;
730 struct timeval tv;
731 const char *buf;
732 size_t len, size;
733 cc_t bspace;
734 int delay, expired = 0, n;
735 key_code key, onlykey;
736 struct mouse_event m = { 0 };
737 struct key_event *event;
739 /* Get key buffer. */
740 buf = EVBUFFER_DATA(tty->in);
741 len = EVBUFFER_LENGTH(tty->in);
742 if (len == 0)
743 return (0);
744 log_debug("%s: keys are %zu (%.*s)", c->name, len, (int)len, buf);
746 /* Is this a clipboard response? */
747 switch (tty_keys_clipboard(tty, buf, len, &size)) {
748 case 0: /* yes */
749 key = KEYC_UNKNOWN;
750 goto complete_key;
751 case -1: /* no, or not valid */
752 break;
753 case 1: /* partial */
754 goto partial_key;
757 /* Is this a primary device attributes response? */
758 switch (tty_keys_device_attributes(tty, buf, len, &size)) {
759 case 0: /* yes */
760 key = KEYC_UNKNOWN;
761 goto complete_key;
762 case -1: /* no, or not valid */
763 break;
764 case 1: /* partial */
765 goto partial_key;
768 /* Is this a secondary device attributes response? */
769 switch (tty_keys_device_attributes2(tty, buf, len, &size)) {
770 case 0: /* yes */
771 key = KEYC_UNKNOWN;
772 goto complete_key;
773 case -1: /* no, or not valid */
774 break;
775 case 1: /* partial */
776 goto partial_key;
779 /* Is this an extended device attributes response? */
780 switch (tty_keys_extended_device_attributes(tty, buf, len, &size)) {
781 case 0: /* yes */
782 key = KEYC_UNKNOWN;
783 goto complete_key;
784 case -1: /* no, or not valid */
785 break;
786 case 1: /* partial */
787 goto partial_key;
790 /* Is this a colours response? */
791 switch (tty_keys_colours(tty, buf, len, &size, &tty->fg, &tty->bg)) {
792 case 0: /* yes */
793 key = KEYC_UNKNOWN;
794 goto complete_key;
795 case -1: /* no, or not valid */
796 break;
797 case 1: /* partial */
798 goto partial_key;
801 /* Is this a mouse key press? */
802 switch (tty_keys_mouse(tty, buf, len, &size, &m)) {
803 case 0: /* yes */
804 key = KEYC_MOUSE;
805 goto complete_key;
806 case -1: /* no, or not valid */
807 break;
808 case -2: /* yes, but we don't care. */
809 key = KEYC_MOUSE;
810 goto discard_key;
811 case 1: /* partial */
812 goto partial_key;
815 /* Is this an extended key press? */
816 switch (tty_keys_extended_key(tty, buf, len, &size, &key)) {
817 case 0: /* yes */
818 goto complete_key;
819 case -1: /* no, or not valid */
820 break;
821 case 1: /* partial */
822 goto partial_key;
825 /* Check for window size query */
826 switch (tty_keys_winsz(tty, buf, len, &size)) {
827 case 0: /* yes */
828 key = KEYC_UNKNOWN;
829 goto complete_key;
830 case -1: /* no, or not valid */
831 break;
832 case 1: /* partial */
833 goto partial_key;
836 first_key:
837 /* Try to lookup complete key. */
838 n = tty_keys_next1(tty, buf, len, &key, &size, expired);
839 if (n == 0) /* found */
840 goto complete_key;
841 if (n == 1)
842 goto partial_key;
845 * If not a complete key, look for key with an escape prefix (meta
846 * modifier).
848 if (*buf == '\033' && len > 1) {
849 /* Look for a key without the escape. */
850 n = tty_keys_next1(tty, buf + 1, len - 1, &key, &size, expired);
851 if (n == 0) { /* found */
852 if (key & KEYC_IMPLIED_META) {
854 * We want the escape key as well as the xterm
855 * key, because the xterm sequence implicitly
856 * includes the escape (so if we see
857 * \033\033[1;3D we know it is an Escape
858 * followed by M-Left, not just M-Left).
860 key = '\033';
861 size = 1;
862 goto complete_key;
864 key |= KEYC_META;
865 size++;
866 goto complete_key;
868 if (n == 1) /* partial */
869 goto partial_key;
873 * At this point, we know the key is not partial (with or without
874 * escape). So pass it through even if the timer has not expired.
876 if (*buf == '\033' && len >= 2) {
877 key = (u_char)buf[1] | KEYC_META;
878 size = 2;
879 } else {
880 key = (u_char)buf[0];
881 size = 1;
884 /* C-Space is special. */
885 if ((key & KEYC_MASK_KEY) == C0_NUL)
886 key = ' ' | KEYC_CTRL | (key & KEYC_META);
889 * Fix up all C0 control codes that don't have a dedicated key into
890 * corresponding Ctrl keys. Convert characters in the A-Z range into
891 * lowercase, so ^A becomes a|CTRL.
893 onlykey = key & KEYC_MASK_KEY;
894 if (onlykey < 0x20 &&
895 onlykey != C0_HT &&
896 onlykey != C0_CR &&
897 onlykey != C0_ESC) {
898 onlykey |= 0x40;
899 if (onlykey >= 'A' && onlykey <= 'Z')
900 onlykey |= 0x20;
901 key = onlykey | KEYC_CTRL | (key & KEYC_META);
904 goto complete_key;
906 partial_key:
907 log_debug("%s: partial key %.*s", c->name, (int)len, buf);
909 /* If timer is going, check for expiration. */
910 if (tty->flags & TTY_TIMER) {
911 if (evtimer_initialized(&tty->key_timer) &&
912 !evtimer_pending(&tty->key_timer, NULL)) {
913 expired = 1;
914 goto first_key;
916 return (0);
919 /* Get the time period. */
920 delay = options_get_number(global_options, "escape-time");
921 if (delay == 0)
922 delay = 1;
923 tv.tv_sec = delay / 1000;
924 tv.tv_usec = (delay % 1000) * 1000L;
926 /* Start the timer. */
927 if (event_initialized(&tty->key_timer))
928 evtimer_del(&tty->key_timer);
929 evtimer_set(&tty->key_timer, tty_keys_callback, tty);
930 evtimer_add(&tty->key_timer, &tv);
932 tty->flags |= TTY_TIMER;
933 return (0);
935 complete_key:
936 log_debug("%s: complete key %.*s %#llx", c->name, (int)size, buf, key);
939 * Check for backspace key using termios VERASE - the terminfo
940 * kbs entry is extremely unreliable, so cannot be safely
941 * used. termios should have a better idea.
943 bspace = tty->tio.c_cc[VERASE];
944 if (bspace != _POSIX_VDISABLE && (key & KEYC_MASK_KEY) == bspace)
945 key = (key & KEYC_MASK_MODIFIERS)|KEYC_BSPACE;
947 /* Remove key timer. */
948 if (event_initialized(&tty->key_timer))
949 evtimer_del(&tty->key_timer);
950 tty->flags &= ~TTY_TIMER;
952 /* Check for focus events. */
953 if (key == KEYC_FOCUS_OUT) {
954 c->flags &= ~CLIENT_FOCUSED;
955 window_update_focus(c->session->curw->window);
956 notify_client("client-focus-out", c);
957 } else if (key == KEYC_FOCUS_IN) {
958 c->flags |= CLIENT_FOCUSED;
959 notify_client("client-focus-in", c);
960 window_update_focus(c->session->curw->window);
963 /* Fire the key. */
964 if (key != KEYC_UNKNOWN) {
965 event = xcalloc(1, sizeof *event);
966 event->key = key;
967 memcpy(&event->m, &m, sizeof event->m);
969 event->buf = xmalloc(size);
970 event->len = size;
971 memcpy (event->buf, buf, event->len);
973 if (!server_client_handle_key(c, event)) {
974 free(event->buf);
975 free(event);
979 /* Remove data from buffer. */
980 evbuffer_drain(tty->in, size);
982 return (1);
984 discard_key:
985 log_debug("%s: discard key %.*s %#llx", c->name, (int)size, buf, key);
987 /* Remove data from buffer. */
988 evbuffer_drain(tty->in, size);
990 return (1);
993 /* Key timer callback. */
994 static void
995 tty_keys_callback(__unused int fd, __unused short events, void *data)
997 struct tty *tty = data;
999 if (tty->flags & TTY_TIMER) {
1000 while (tty_keys_next(tty))
1006 * Handle extended key input. This has two forms: \033[27;m;k~ and \033[k;mu,
1007 * where k is key as a number and m is a modifier. Returns 0 for success, -1
1008 * for failure, 1 for partial;
1010 static int
1011 tty_keys_extended_key(struct tty *tty, const char *buf, size_t len,
1012 size_t *size, key_code *key)
1014 struct client *c = tty->client;
1015 size_t end;
1016 u_int number, modifiers;
1017 char tmp[64];
1018 cc_t bspace;
1019 key_code nkey, onlykey;
1020 struct utf8_data ud;
1021 utf8_char uc;
1023 *size = 0;
1025 /* First two bytes are always \033[. */
1026 if (buf[0] != '\033')
1027 return (-1);
1028 if (len == 1)
1029 return (1);
1030 if (buf[1] != '[')
1031 return (-1);
1032 if (len == 2)
1033 return (1);
1036 * Look for a terminator. Stop at either '~' or anything that isn't a
1037 * number or ';'.
1039 for (end = 2; end < len && end != sizeof tmp; end++) {
1040 if (buf[end] == '~')
1041 break;
1042 if (!isdigit((u_char)buf[end]) && buf[end] != ';')
1043 break;
1045 if (end == len)
1046 return (1);
1047 if (end == sizeof tmp || (buf[end] != '~' && buf[end] != 'u'))
1048 return (-1);
1050 /* Copy to the buffer. */
1051 memcpy(tmp, buf + 2, end);
1052 tmp[end] = '\0';
1054 /* Try to parse either form of key. */
1055 if (buf[end] == '~') {
1056 if (sscanf(tmp, "27;%u;%u", &modifiers, &number) != 2)
1057 return (-1);
1058 } else {
1059 if (sscanf(tmp ,"%u;%u", &number, &modifiers) != 2)
1060 return (-1);
1062 *size = end + 1;
1064 /* Store the key. */
1065 bspace = tty->tio.c_cc[VERASE];
1066 if (bspace != _POSIX_VDISABLE && number == bspace)
1067 nkey = KEYC_BSPACE;
1068 else
1069 nkey = number;
1071 /* Convert UTF-32 codepoint into internal representation. */
1072 if (nkey != KEYC_BSPACE && nkey & ~0x7f) {
1073 if (utf8_fromwc(nkey, &ud) == UTF8_DONE &&
1074 utf8_from_data(&ud, &uc) == UTF8_DONE)
1075 nkey = uc;
1076 else
1077 return (-1);
1080 /* Update the modifiers. */
1081 if (modifiers > 0) {
1082 modifiers--;
1083 if (modifiers & 1)
1084 nkey |= KEYC_SHIFT;
1085 if (modifiers & 2)
1086 nkey |= (KEYC_META|KEYC_IMPLIED_META); /* Alt */
1087 if (modifiers & 4)
1088 nkey |= KEYC_CTRL;
1089 if (modifiers & 8)
1090 nkey |= (KEYC_META|KEYC_IMPLIED_META); /* Meta */
1093 /* Convert S-Tab into Backtab. */
1094 if ((nkey & KEYC_MASK_KEY) == '\011' && (nkey & KEYC_SHIFT))
1095 nkey = KEYC_BTAB | (nkey & ~KEYC_MASK_KEY & ~KEYC_SHIFT);
1098 * Deal with the Shift modifier when present alone. The problem is that
1099 * in mode 2 some terminals would report shifted keys, like S-a, as
1100 * just A, and some as S-A.
1102 * Because we need an unambiguous internal representation, and because
1103 * restoring the Shift modifier when it's missing would require knowing
1104 * the keyboard layout, and because S-A would cause a lot of issues
1105 * downstream, we choose to lose the Shift for all printable
1106 * characters.
1108 * That still leaves some ambiguity, such as C-S-A vs. C-A, but that's
1109 * OK, and applications can handle that.
1111 onlykey = nkey & KEYC_MASK_KEY;
1112 if (((onlykey > 0x20 && onlykey < 0x7f) ||
1113 KEYC_IS_UNICODE(nkey)) &&
1114 (nkey & KEYC_MASK_MODIFIERS) == KEYC_SHIFT)
1115 nkey &= ~KEYC_SHIFT;
1117 if (log_get_level() != 0) {
1118 log_debug("%s: extended key %.*s is %llx (%s)", c->name,
1119 (int)*size, buf, nkey, key_string_lookup_key(nkey, 1));
1122 *key = nkey;
1123 return (0);
1127 * Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
1128 * (probably a mouse sequence but need more data), -2 if an invalid mouse
1129 * sequence.
1131 static int
1132 tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size,
1133 struct mouse_event *m)
1135 struct client *c = tty->client;
1136 u_int i, x, y, b, sgr_b;
1137 u_char sgr_type, ch;
1140 * Standard mouse sequences are \033[M followed by three characters
1141 * indicating button, X and Y, all based at 32 with 1,1 top-left.
1143 * UTF-8 mouse sequences are similar but the three are expressed as
1144 * UTF-8 characters.
1146 * SGR extended mouse sequences are \033[< followed by three numbers in
1147 * decimal and separated by semicolons indicating button, X and Y. A
1148 * trailing 'M' is click or scroll and trailing 'm' release. All are
1149 * based at 0 with 1,1 top-left.
1152 *size = 0;
1153 x = y = b = sgr_b = 0;
1154 sgr_type = ' ';
1156 /* First two bytes are always \033[. */
1157 if (buf[0] != '\033')
1158 return (-1);
1159 if (len == 1)
1160 return (1);
1161 if (buf[1] != '[')
1162 return (-1);
1163 if (len == 2)
1164 return (1);
1167 * Third byte is M in old standard (and UTF-8 extension which we do not
1168 * support), < in SGR extension.
1170 if (buf[2] == 'M') {
1171 /* Read the three inputs. */
1172 *size = 3;
1173 for (i = 0; i < 3; i++) {
1174 if (len <= *size)
1175 return (1);
1176 ch = (u_char)buf[(*size)++];
1177 if (i == 0)
1178 b = ch;
1179 else if (i == 1)
1180 x = ch;
1181 else
1182 y = ch;
1184 log_debug("%s: mouse input: %.*s", c->name, (int)*size, buf);
1186 /* Check and return the mouse input. */
1187 if (b < MOUSE_PARAM_BTN_OFF ||
1188 x < MOUSE_PARAM_POS_OFF ||
1189 y < MOUSE_PARAM_POS_OFF)
1190 return (-2);
1191 b -= MOUSE_PARAM_BTN_OFF;
1192 x -= MOUSE_PARAM_POS_OFF;
1193 y -= MOUSE_PARAM_POS_OFF;
1194 } else if (buf[2] == '<') {
1195 /* Read the three inputs. */
1196 *size = 3;
1197 while (1) {
1198 if (len <= *size)
1199 return (1);
1200 ch = (u_char)buf[(*size)++];
1201 if (ch == ';')
1202 break;
1203 if (ch < '0' || ch > '9')
1204 return (-1);
1205 sgr_b = 10 * sgr_b + (ch - '0');
1207 while (1) {
1208 if (len <= *size)
1209 return (1);
1210 ch = (u_char)buf[(*size)++];
1211 if (ch == ';')
1212 break;
1213 if (ch < '0' || ch > '9')
1214 return (-1);
1215 x = 10 * x + (ch - '0');
1217 while (1) {
1218 if (len <= *size)
1219 return (1);
1220 ch = (u_char)buf[(*size)++];
1221 if (ch == 'M' || ch == 'm')
1222 break;
1223 if (ch < '0' || ch > '9')
1224 return (-1);
1225 y = 10 * y + (ch - '0');
1227 log_debug("%s: mouse input (SGR): %.*s", c->name, (int)*size,
1228 buf);
1230 /* Check and return the mouse input. */
1231 if (x < 1 || y < 1)
1232 return (-2);
1233 x--;
1234 y--;
1235 b = sgr_b;
1237 /* Type is M for press, m for release. */
1238 sgr_type = ch;
1239 if (sgr_type == 'm')
1240 b = 3;
1243 * Some terminals (like PuTTY 0.63) mistakenly send
1244 * button-release events for scroll-wheel button-press event.
1245 * Discard it before it reaches any program running inside
1246 * tmux.
1248 if (sgr_type == 'm' && MOUSE_WHEEL(sgr_b))
1249 return (-2);
1250 } else
1251 return (-1);
1253 /* Fill mouse event. */
1254 m->lx = tty->mouse_last_x;
1255 m->x = x;
1256 m->ly = tty->mouse_last_y;
1257 m->y = y;
1258 m->lb = tty->mouse_last_b;
1259 m->b = b;
1260 m->sgr_type = sgr_type;
1261 m->sgr_b = sgr_b;
1263 /* Update last mouse state. */
1264 tty->mouse_last_x = x;
1265 tty->mouse_last_y = y;
1266 tty->mouse_last_b = b;
1268 return (0);
1272 * Handle OSC 52 clipboard input. Returns 0 for success, -1 for failure, 1 for
1273 * partial.
1275 static int
1276 tty_keys_clipboard(struct tty *tty, const char *buf, size_t len, size_t *size)
1278 struct client *c = tty->client;
1279 struct window_pane *wp;
1280 size_t end, terminator = 0, needed;
1281 char *copy, *out;
1282 int outlen;
1283 u_int i;
1285 *size = 0;
1287 /* First five bytes are always \033]52;. */
1288 if (buf[0] != '\033')
1289 return (-1);
1290 if (len == 1)
1291 return (1);
1292 if (buf[1] != ']')
1293 return (-1);
1294 if (len == 2)
1295 return (1);
1296 if (buf[2] != '5')
1297 return (-1);
1298 if (len == 3)
1299 return (1);
1300 if (buf[3] != '2')
1301 return (-1);
1302 if (len == 4)
1303 return (1);
1304 if (buf[4] != ';')
1305 return (-1);
1306 if (len == 5)
1307 return (1);
1309 /* Find the terminator if any. */
1310 for (end = 5; end < len; end++) {
1311 if (buf[end] == '\007') {
1312 terminator = 1;
1313 break;
1315 if (end > 5 && buf[end - 1] == '\033' && buf[end] == '\\') {
1316 terminator = 2;
1317 break;
1320 if (end == len)
1321 return (1);
1322 *size = end + 1;
1324 /* Skip the initial part. */
1325 buf += 5;
1326 end -= 5;
1328 /* Adjust end so that it points to the start of the terminator. */
1329 end -= terminator - 1;
1331 /* Get the second argument. */
1332 while (end != 0 && *buf != ';') {
1333 buf++;
1334 end--;
1336 if (end == 0 || end == 1)
1337 return (0);
1338 buf++;
1339 end--;
1341 /* If we did not request this, ignore it. */
1342 if (~tty->flags & TTY_OSC52QUERY)
1343 return (0);
1344 tty->flags &= ~TTY_OSC52QUERY;
1345 evtimer_del(&tty->clipboard_timer);
1347 /* It has to be a string so copy it. */
1348 copy = xmalloc(end + 1);
1349 memcpy(copy, buf, end);
1350 copy[end] = '\0';
1352 /* Convert from base64. */
1353 needed = (end / 4) * 3;
1354 out = xmalloc(needed);
1355 if ((outlen = b64_pton(copy, out, len)) == -1) {
1356 free(out);
1357 free(copy);
1358 return (0);
1360 free(copy);
1362 /* Create a new paste buffer and forward to panes. */
1363 log_debug("%s: %.*s", __func__, outlen, out);
1364 if (c->flags & CLIENT_CLIPBOARDBUFFER) {
1365 paste_add(NULL, out, outlen);
1366 c->flags &= ~CLIENT_CLIPBOARDBUFFER;
1368 for (i = 0; i < c->clipboard_npanes; i++) {
1369 wp = window_pane_find_by_id(c->clipboard_panes[i]);
1370 if (wp != NULL)
1371 input_reply_clipboard(wp->event, out, outlen, "\033\\");
1373 free(c->clipboard_panes);
1374 c->clipboard_panes = NULL;
1375 c->clipboard_npanes = 0;
1377 return (0);
1381 * Handle primary device attributes input. Returns 0 for success, -1 for
1382 * failure, 1 for partial.
1384 static int
1385 tty_keys_device_attributes(struct tty *tty, const char *buf, size_t len,
1386 size_t *size)
1388 struct client *c = tty->client;
1389 int *features = &c->term_features;
1390 u_int i, n = 0;
1391 char tmp[128], *endptr, p[32] = { 0 }, *cp, *next;
1393 *size = 0;
1394 if (tty->flags & TTY_HAVEDA)
1395 return (-1);
1397 /* First three bytes are always \033[?. */
1398 if (buf[0] != '\033')
1399 return (-1);
1400 if (len == 1)
1401 return (1);
1402 if (buf[1] != '[')
1403 return (-1);
1404 if (len == 2)
1405 return (1);
1406 if (buf[2] != '?')
1407 return (-1);
1408 if (len == 3)
1409 return (1);
1411 /* Copy the rest up to a c. */
1412 for (i = 0; i < (sizeof tmp); i++) {
1413 if (3 + i == len)
1414 return (1);
1415 if (buf[3 + i] == 'c')
1416 break;
1417 tmp[i] = buf[3 + i];
1419 if (i == (sizeof tmp))
1420 return (-1);
1421 tmp[i] = '\0';
1422 *size = 4 + i;
1424 /* Convert all arguments to numbers. */
1425 cp = tmp;
1426 while ((next = strsep(&cp, ";")) != NULL) {
1427 p[n] = strtoul(next, &endptr, 10);
1428 if (*endptr != '\0')
1429 p[n] = 0;
1430 if (++n == nitems(p))
1431 break;
1434 /* Add terminal features. */
1435 switch (p[0]) {
1436 case 61: /* level 1 */
1437 case 62: /* level 2 */
1438 case 63: /* level 3 */
1439 case 64: /* level 4 */
1440 case 65: /* level 5 */
1441 for (i = 1; i < n; i++) {
1442 log_debug("%s: DA feature: %d", c->name, p[i]);
1443 if (p[i] == 4)
1444 tty_add_features(features, "sixel", ",");
1445 if (p[i] == 21)
1446 tty_add_features(features, "margins", ",");
1447 if (p[i] == 28)
1448 tty_add_features(features, "rectfill", ",");
1450 break;
1452 log_debug("%s: received primary DA %.*s", c->name, (int)*size, buf);
1454 tty_update_features(tty);
1455 tty->flags |= TTY_HAVEDA;
1457 return (0);
1461 * Handle secondary device attributes input. Returns 0 for success, -1 for
1462 * failure, 1 for partial.
1464 static int
1465 tty_keys_device_attributes2(struct tty *tty, const char *buf, size_t len,
1466 size_t *size)
1468 struct client *c = tty->client;
1469 int *features = &c->term_features;
1470 u_int i, n = 0;
1471 char tmp[128], *endptr, p[32] = { 0 }, *cp, *next;
1473 *size = 0;
1474 if (tty->flags & TTY_HAVEDA2)
1475 return (-1);
1477 /* First three bytes are always \033[>. */
1478 if (buf[0] != '\033')
1479 return (-1);
1480 if (len == 1)
1481 return (1);
1482 if (buf[1] != '[')
1483 return (-1);
1484 if (len == 2)
1485 return (1);
1486 if (buf[2] != '>')
1487 return (-1);
1488 if (len == 3)
1489 return (1);
1491 /* Copy the rest up to a c. */
1492 for (i = 0; i < (sizeof tmp); i++) {
1493 if (3 + i == len)
1494 return (1);
1495 if (buf[3 + i] == 'c')
1496 break;
1497 tmp[i] = buf[3 + i];
1499 if (i == (sizeof tmp))
1500 return (-1);
1501 tmp[i] = '\0';
1502 *size = 4 + i;
1504 /* Convert all arguments to numbers. */
1505 cp = tmp;
1506 while ((next = strsep(&cp, ";")) != NULL) {
1507 p[n] = strtoul(next, &endptr, 10);
1508 if (*endptr != '\0')
1509 p[n] = 0;
1510 if (++n == nitems(p))
1511 break;
1515 * Add terminal features. We add DECSLRM and DECFRA for some
1516 * identification codes here, notably 64 will catch VT520, even though
1517 * we can't use level 5 from DA because of VTE.
1519 switch (p[0]) {
1520 case 'M': /* mintty */
1521 tty_default_features(features, "mintty", 0);
1522 break;
1523 case 'T': /* tmux */
1524 tty_default_features(features, "tmux", 0);
1525 break;
1526 case 'U': /* rxvt-unicode */
1527 tty_default_features(features, "rxvt-unicode", 0);
1528 break;
1530 log_debug("%s: received secondary DA %.*s", c->name, (int)*size, buf);
1532 tty_update_features(tty);
1533 tty->flags |= TTY_HAVEDA2;
1535 return (0);
1539 * Handle extended device attributes input. Returns 0 for success, -1 for
1540 * failure, 1 for partial.
1542 static int
1543 tty_keys_extended_device_attributes(struct tty *tty, const char *buf,
1544 size_t len, size_t *size)
1546 struct client *c = tty->client;
1547 int *features = &c->term_features;
1548 u_int i;
1549 char tmp[128];
1551 *size = 0;
1552 if (tty->flags & TTY_HAVEXDA)
1553 return (-1);
1555 /* First four bytes are always \033P>|. */
1556 if (buf[0] != '\033')
1557 return (-1);
1558 if (len == 1)
1559 return (1);
1560 if (buf[1] != 'P')
1561 return (-1);
1562 if (len == 2)
1563 return (1);
1564 if (buf[2] != '>')
1565 return (-1);
1566 if (len == 3)
1567 return (1);
1568 if (buf[3] != '|')
1569 return (-1);
1570 if (len == 4)
1571 return (1);
1573 /* Copy the rest up to \033\. */
1574 for (i = 0; i < (sizeof tmp) - 1; i++) {
1575 if (4 + i == len)
1576 return (1);
1577 if (buf[4 + i - 1] == '\033' && buf[4 + i] == '\\')
1578 break;
1579 tmp[i] = buf[4 + i];
1581 if (i == (sizeof tmp) - 1)
1582 return (-1);
1583 tmp[i - 1] = '\0';
1584 *size = 5 + i;
1586 /* Add terminal features. */
1587 if (strncmp(tmp, "iTerm2 ", 7) == 0)
1588 tty_default_features(features, "iTerm2", 0);
1589 else if (strncmp(tmp, "tmux ", 5) == 0)
1590 tty_default_features(features, "tmux", 0);
1591 else if (strncmp(tmp, "XTerm(", 6) == 0)
1592 tty_default_features(features, "XTerm", 0);
1593 else if (strncmp(tmp, "mintty ", 7) == 0)
1594 tty_default_features(features, "mintty", 0);
1595 log_debug("%s: received extended DA %.*s", c->name, (int)*size, buf);
1597 free(c->term_type);
1598 c->term_type = xstrdup(tmp);
1600 tty_update_features(tty);
1601 tty->flags |= TTY_HAVEXDA;
1603 return (0);
1607 * Handle foreground or background input. Returns 0 for success, -1 for
1608 * failure, 1 for partial.
1611 tty_keys_colours(struct tty *tty, const char *buf, size_t len, size_t *size,
1612 int *fg, int *bg)
1614 struct client *c = tty->client;
1615 u_int i;
1616 char tmp[128];
1617 int n;
1619 *size = 0;
1621 /* First four bytes are always \033]1 and 0 or 1 and ;. */
1622 if (buf[0] != '\033')
1623 return (-1);
1624 if (len == 1)
1625 return (1);
1626 if (buf[1] != ']')
1627 return (-1);
1628 if (len == 2)
1629 return (1);
1630 if (buf[2] != '1')
1631 return (-1);
1632 if (len == 3)
1633 return (1);
1634 if (buf[3] != '0' && buf[3] != '1')
1635 return (-1);
1636 if (len == 4)
1637 return (1);
1638 if (buf[4] != ';')
1639 return (-1);
1640 if (len == 5)
1641 return (1);
1643 /* Copy the rest up to \033\ or \007. */
1644 for (i = 0; i < (sizeof tmp) - 1; i++) {
1645 if (5 + i == len)
1646 return (1);
1647 if (buf[5 + i - 1] == '\033' && buf[5 + i] == '\\')
1648 break;
1649 if (buf[5 + i] == '\007')
1650 break;
1651 tmp[i] = buf[5 + i];
1653 if (i == (sizeof tmp) - 1)
1654 return (-1);
1655 if (tmp[i - 1] == '\033')
1656 tmp[i - 1] = '\0';
1657 else
1658 tmp[i] = '\0';
1659 *size = 6 + i;
1661 n = colour_parseX11(tmp);
1662 if (n != -1 && buf[3] == '0') {
1663 if (c != NULL)
1664 log_debug("%s fg is %s", c->name, colour_tostring(n));
1665 else
1666 log_debug("fg is %s", colour_tostring(n));
1667 *fg = n;
1668 } else if (n != -1) {
1669 if (c != NULL)
1670 log_debug("%s bg is %s", c->name, colour_tostring(n));
1671 else
1672 log_debug("bg is %s", colour_tostring(n));
1673 *bg = n;
1676 return (0);