GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/tmux/tty-keys.c Lines: 0 241 0.0 %
Date: 2016-12-06 Branches: 0 160 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: tty-keys.c,v 1.89 2016/04/29 10:42:16 nicm Exp $ */
2
3
/*
4
 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/types.h>
20
#include <sys/time.h>
21
22
#include <limits.h>
23
#include <stdlib.h>
24
#include <string.h>
25
#include <termios.h>
26
#include <unistd.h>
27
28
#include "tmux.h"
29
30
/*
31
 * Handle keys input from the outside terminal. tty_default_*_keys[] are a base
32
 * table of supported keys which are looked up in terminfo(5) and translated
33
 * into a ternary tree.
34
 */
35
36
static void	tty_keys_add1(struct tty_key **, const char *, key_code);
37
static void	tty_keys_add(struct tty *, const char *, key_code);
38
static void	tty_keys_free1(struct tty_key *);
39
static struct tty_key *tty_keys_find1(struct tty_key *, const char *, size_t,
40
		    size_t *);
41
static struct tty_key *tty_keys_find(struct tty *, const char *, size_t,
42
		    size_t *);
43
static int	tty_keys_next1(struct tty *, const char *, size_t, key_code *,
44
		    size_t *, int);
45
static void	tty_keys_callback(int, short, void *);
46
static int	tty_keys_mouse(struct tty *, const char *, size_t, size_t *);
47
48
/* Default raw keys. */
49
struct tty_default_key_raw {
50
	const char	       *string;
51
	key_code	 	key;
52
};
53
const struct tty_default_key_raw tty_default_raw_keys[] = {
54
	/*
55
	 * Numeric keypad. Just use the vt100 escape sequences here and always
56
	 * put the terminal into keypad_xmit mode. Translation of numbers
57
	 * mode/applications mode is done in input-keys.c.
58
	 */
59
	{ "\033Oo", KEYC_KP_SLASH },
60
	{ "\033Oj", KEYC_KP_STAR },
61
	{ "\033Om", KEYC_KP_MINUS },
62
	{ "\033Ow", KEYC_KP_SEVEN },
63
	{ "\033Ox", KEYC_KP_EIGHT },
64
	{ "\033Oy", KEYC_KP_NINE },
65
	{ "\033Ok", KEYC_KP_PLUS },
66
	{ "\033Ot", KEYC_KP_FOUR },
67
	{ "\033Ou", KEYC_KP_FIVE },
68
	{ "\033Ov", KEYC_KP_SIX },
69
	{ "\033Oq", KEYC_KP_ONE },
70
	{ "\033Or", KEYC_KP_TWO },
71
	{ "\033Os", KEYC_KP_THREE },
72
	{ "\033OM", KEYC_KP_ENTER },
73
	{ "\033Op", KEYC_KP_ZERO },
74
	{ "\033On", KEYC_KP_PERIOD },
75
76
	/* Arrow keys. */
77
	{ "\033OA", KEYC_UP },
78
	{ "\033OB", KEYC_DOWN },
79
	{ "\033OC", KEYC_RIGHT },
80
	{ "\033OD", KEYC_LEFT },
81
82
	{ "\033[A", KEYC_UP },
83
	{ "\033[B", KEYC_DOWN },
84
	{ "\033[C", KEYC_RIGHT },
85
	{ "\033[D", KEYC_LEFT },
86
87
	/* Other (xterm) "cursor" keys. */
88
	{ "\033OH", KEYC_HOME },
89
	{ "\033OF", KEYC_END },
90
91
	{ "\033[H", KEYC_HOME },
92
	{ "\033[F", KEYC_END },
93
94
	/* rxvt-style arrow + modifier keys. */
95
	{ "\033Oa", KEYC_UP|KEYC_CTRL },
96
	{ "\033Ob", KEYC_DOWN|KEYC_CTRL },
97
	{ "\033Oc", KEYC_RIGHT|KEYC_CTRL },
98
	{ "\033Od", KEYC_LEFT|KEYC_CTRL },
99
100
	{ "\033[a", KEYC_UP|KEYC_SHIFT },
101
	{ "\033[b", KEYC_DOWN|KEYC_SHIFT },
102
	{ "\033[c", KEYC_RIGHT|KEYC_SHIFT },
103
	{ "\033[d", KEYC_LEFT|KEYC_SHIFT },
104
105
	/* rxvt-style function + modifier keys (C = ^, S = $, C-S = @). */
106
	{ "\033[11^", KEYC_F1|KEYC_CTRL },
107
	{ "\033[12^", KEYC_F2|KEYC_CTRL },
108
	{ "\033[13^", KEYC_F3|KEYC_CTRL },
109
	{ "\033[14^", KEYC_F4|KEYC_CTRL },
110
	{ "\033[15^", KEYC_F5|KEYC_CTRL },
111
	{ "\033[17^", KEYC_F6|KEYC_CTRL },
112
	{ "\033[18^", KEYC_F7|KEYC_CTRL },
113
	{ "\033[19^", KEYC_F8|KEYC_CTRL },
114
	{ "\033[20^", KEYC_F9|KEYC_CTRL },
115
	{ "\033[21^", KEYC_F10|KEYC_CTRL },
116
	{ "\033[23^", KEYC_F11|KEYC_CTRL },
117
	{ "\033[24^", KEYC_F12|KEYC_CTRL },
118
	{ "\033[2^", KEYC_IC|KEYC_CTRL },
119
	{ "\033[3^", KEYC_DC|KEYC_CTRL },
120
	{ "\033[7^", KEYC_HOME|KEYC_CTRL },
121
	{ "\033[8^", KEYC_END|KEYC_CTRL },
122
	{ "\033[6^", KEYC_NPAGE|KEYC_CTRL },
123
	{ "\033[5^", KEYC_PPAGE|KEYC_CTRL },
124
125
	{ "\033[11$", KEYC_F1|KEYC_SHIFT },
126
	{ "\033[12$", KEYC_F2|KEYC_SHIFT },
127
	{ "\033[13$", KEYC_F3|KEYC_SHIFT },
128
	{ "\033[14$", KEYC_F4|KEYC_SHIFT },
129
	{ "\033[15$", KEYC_F5|KEYC_SHIFT },
130
	{ "\033[17$", KEYC_F6|KEYC_SHIFT },
131
	{ "\033[18$", KEYC_F7|KEYC_SHIFT },
132
	{ "\033[19$", KEYC_F8|KEYC_SHIFT },
133
	{ "\033[20$", KEYC_F9|KEYC_SHIFT },
134
	{ "\033[21$", KEYC_F10|KEYC_SHIFT },
135
	{ "\033[23$", KEYC_F11|KEYC_SHIFT },
136
	{ "\033[24$", KEYC_F12|KEYC_SHIFT },
137
	{ "\033[2$", KEYC_IC|KEYC_SHIFT },
138
	{ "\033[3$", KEYC_DC|KEYC_SHIFT },
139
	{ "\033[7$", KEYC_HOME|KEYC_SHIFT },
140
	{ "\033[8$", KEYC_END|KEYC_SHIFT },
141
	{ "\033[6$", KEYC_NPAGE|KEYC_SHIFT },
142
	{ "\033[5$", KEYC_PPAGE|KEYC_SHIFT },
143
144
	{ "\033[11@", KEYC_F1|KEYC_CTRL|KEYC_SHIFT },
145
	{ "\033[12@", KEYC_F2|KEYC_CTRL|KEYC_SHIFT },
146
	{ "\033[13@", KEYC_F3|KEYC_CTRL|KEYC_SHIFT },
147
	{ "\033[14@", KEYC_F4|KEYC_CTRL|KEYC_SHIFT },
148
	{ "\033[15@", KEYC_F5|KEYC_CTRL|KEYC_SHIFT },
149
	{ "\033[17@", KEYC_F6|KEYC_CTRL|KEYC_SHIFT },
150
	{ "\033[18@", KEYC_F7|KEYC_CTRL|KEYC_SHIFT },
151
	{ "\033[19@", KEYC_F8|KEYC_CTRL|KEYC_SHIFT },
152
	{ "\033[20@", KEYC_F9|KEYC_CTRL|KEYC_SHIFT },
153
	{ "\033[21@", KEYC_F10|KEYC_CTRL|KEYC_SHIFT },
154
	{ "\033[23@", KEYC_F11|KEYC_CTRL|KEYC_SHIFT },
155
	{ "\033[24@", KEYC_F12|KEYC_CTRL|KEYC_SHIFT },
156
	{ "\033[2@", KEYC_IC|KEYC_CTRL|KEYC_SHIFT },
157
	{ "\033[3@", KEYC_DC|KEYC_CTRL|KEYC_SHIFT },
158
	{ "\033[7@", KEYC_HOME|KEYC_CTRL|KEYC_SHIFT },
159
	{ "\033[8@", KEYC_END|KEYC_CTRL|KEYC_SHIFT },
160
	{ "\033[6@", KEYC_NPAGE|KEYC_CTRL|KEYC_SHIFT },
161
	{ "\033[5@", KEYC_PPAGE|KEYC_CTRL|KEYC_SHIFT },
162
163
	/* Focus tracking. */
164
	{ "\033[I", KEYC_FOCUS_IN },
165
	{ "\033[O", KEYC_FOCUS_OUT },
166
};
167
168
/* Default terminfo(5) keys. */
169
struct tty_default_key_code {
170
	enum tty_code_code	code;
171
	key_code	 	key;
172
};
173
const struct tty_default_key_code tty_default_code_keys[] = {
174
	/* Function keys. */
175
	{ TTYC_KF1, KEYC_F1 },
176
	{ TTYC_KF2, KEYC_F2 },
177
	{ TTYC_KF3, KEYC_F3 },
178
	{ TTYC_KF4, KEYC_F4 },
179
	{ TTYC_KF5, KEYC_F5 },
180
	{ TTYC_KF6, KEYC_F6 },
181
	{ TTYC_KF7, KEYC_F7 },
182
	{ TTYC_KF8, KEYC_F8 },
183
	{ TTYC_KF9, KEYC_F9 },
184
	{ TTYC_KF10, KEYC_F10 },
185
	{ TTYC_KF11, KEYC_F11 },
186
	{ TTYC_KF12, KEYC_F12 },
187
188
	{ TTYC_KF13, KEYC_F1|KEYC_SHIFT },
189
	{ TTYC_KF14, KEYC_F2|KEYC_SHIFT },
190
	{ TTYC_KF15, KEYC_F3|KEYC_SHIFT },
191
	{ TTYC_KF16, KEYC_F4|KEYC_SHIFT },
192
	{ TTYC_KF17, KEYC_F5|KEYC_SHIFT },
193
	{ TTYC_KF18, KEYC_F6|KEYC_SHIFT },
194
	{ TTYC_KF19, KEYC_F7|KEYC_SHIFT },
195
	{ TTYC_KF20, KEYC_F8|KEYC_SHIFT },
196
	{ TTYC_KF21, KEYC_F9|KEYC_SHIFT },
197
	{ TTYC_KF22, KEYC_F10|KEYC_SHIFT },
198
	{ TTYC_KF23, KEYC_F11|KEYC_SHIFT },
199
	{ TTYC_KF24, KEYC_F12|KEYC_SHIFT },
200
201
	{ TTYC_KF25, KEYC_F1|KEYC_CTRL },
202
	{ TTYC_KF26, KEYC_F2|KEYC_CTRL },
203
	{ TTYC_KF27, KEYC_F3|KEYC_CTRL },
204
	{ TTYC_KF28, KEYC_F4|KEYC_CTRL },
205
	{ TTYC_KF29, KEYC_F5|KEYC_CTRL },
206
	{ TTYC_KF30, KEYC_F6|KEYC_CTRL },
207
	{ TTYC_KF31, KEYC_F7|KEYC_CTRL },
208
	{ TTYC_KF32, KEYC_F8|KEYC_CTRL },
209
	{ TTYC_KF33, KEYC_F9|KEYC_CTRL },
210
	{ TTYC_KF34, KEYC_F10|KEYC_CTRL },
211
	{ TTYC_KF35, KEYC_F11|KEYC_CTRL },
212
	{ TTYC_KF36, KEYC_F12|KEYC_CTRL },
213
214
	{ TTYC_KF37, KEYC_F1|KEYC_SHIFT|KEYC_CTRL },
215
	{ TTYC_KF38, KEYC_F2|KEYC_SHIFT|KEYC_CTRL },
216
	{ TTYC_KF39, KEYC_F3|KEYC_SHIFT|KEYC_CTRL },
217
	{ TTYC_KF40, KEYC_F4|KEYC_SHIFT|KEYC_CTRL },
218
	{ TTYC_KF41, KEYC_F5|KEYC_SHIFT|KEYC_CTRL },
219
	{ TTYC_KF42, KEYC_F6|KEYC_SHIFT|KEYC_CTRL },
220
	{ TTYC_KF43, KEYC_F7|KEYC_SHIFT|KEYC_CTRL },
221
	{ TTYC_KF44, KEYC_F8|KEYC_SHIFT|KEYC_CTRL },
222
	{ TTYC_KF45, KEYC_F9|KEYC_SHIFT|KEYC_CTRL },
223
	{ TTYC_KF46, KEYC_F10|KEYC_SHIFT|KEYC_CTRL },
224
	{ TTYC_KF47, KEYC_F11|KEYC_SHIFT|KEYC_CTRL },
225
	{ TTYC_KF48, KEYC_F12|KEYC_SHIFT|KEYC_CTRL },
226
227
	{ TTYC_KF49, KEYC_F1|KEYC_ESCAPE },
228
	{ TTYC_KF50, KEYC_F2|KEYC_ESCAPE },
229
	{ TTYC_KF51, KEYC_F3|KEYC_ESCAPE },
230
	{ TTYC_KF52, KEYC_F4|KEYC_ESCAPE },
231
	{ TTYC_KF53, KEYC_F5|KEYC_ESCAPE },
232
	{ TTYC_KF54, KEYC_F6|KEYC_ESCAPE },
233
	{ TTYC_KF55, KEYC_F7|KEYC_ESCAPE },
234
	{ TTYC_KF56, KEYC_F8|KEYC_ESCAPE },
235
	{ TTYC_KF57, KEYC_F9|KEYC_ESCAPE },
236
	{ TTYC_KF58, KEYC_F10|KEYC_ESCAPE },
237
	{ TTYC_KF59, KEYC_F11|KEYC_ESCAPE },
238
	{ TTYC_KF60, KEYC_F12|KEYC_ESCAPE },
239
240
	{ TTYC_KF61, KEYC_F1|KEYC_ESCAPE|KEYC_SHIFT },
241
	{ TTYC_KF62, KEYC_F2|KEYC_ESCAPE|KEYC_SHIFT },
242
	{ TTYC_KF63, KEYC_F3|KEYC_ESCAPE|KEYC_SHIFT },
243
244
	{ TTYC_KICH1, KEYC_IC },
245
	{ TTYC_KDCH1, KEYC_DC },
246
	{ TTYC_KHOME, KEYC_HOME },
247
	{ TTYC_KEND, KEYC_END },
248
	{ TTYC_KNP, KEYC_NPAGE },
249
	{ TTYC_KPP, KEYC_PPAGE },
250
	{ TTYC_KCBT, KEYC_BTAB },
251
252
	/* Arrow keys from terminfo. */
253
	{ TTYC_KCUU1, KEYC_UP },
254
	{ TTYC_KCUD1, KEYC_DOWN },
255
	{ TTYC_KCUB1, KEYC_LEFT },
256
	{ TTYC_KCUF1, KEYC_RIGHT },
257
258
	/* Key and modifier capabilities. */
259
	{ TTYC_KDC2, KEYC_DC|KEYC_SHIFT },
260
	{ TTYC_KDC3, KEYC_DC|KEYC_ESCAPE },
261
	{ TTYC_KDC4, KEYC_DC|KEYC_SHIFT|KEYC_ESCAPE },
262
	{ TTYC_KDC5, KEYC_DC|KEYC_CTRL },
263
	{ TTYC_KDC6, KEYC_DC|KEYC_SHIFT|KEYC_CTRL },
264
	{ TTYC_KDC7, KEYC_DC|KEYC_ESCAPE|KEYC_CTRL },
265
	{ TTYC_KDN2, KEYC_DOWN|KEYC_SHIFT },
266
	{ TTYC_KDN3, KEYC_DOWN|KEYC_ESCAPE },
267
	{ TTYC_KDN4, KEYC_DOWN|KEYC_SHIFT|KEYC_ESCAPE },
268
	{ TTYC_KDN5, KEYC_DOWN|KEYC_CTRL },
269
	{ TTYC_KDN6, KEYC_DOWN|KEYC_SHIFT|KEYC_CTRL },
270
	{ TTYC_KDN7, KEYC_DOWN|KEYC_ESCAPE|KEYC_CTRL },
271
	{ TTYC_KEND2, KEYC_END|KEYC_SHIFT },
272
	{ TTYC_KEND3, KEYC_END|KEYC_ESCAPE },
273
	{ TTYC_KEND4, KEYC_END|KEYC_SHIFT|KEYC_ESCAPE },
274
	{ TTYC_KEND5, KEYC_END|KEYC_CTRL },
275
	{ TTYC_KEND6, KEYC_END|KEYC_SHIFT|KEYC_CTRL },
276
	{ TTYC_KEND7, KEYC_END|KEYC_ESCAPE|KEYC_CTRL },
277
	{ TTYC_KHOM2, KEYC_HOME|KEYC_SHIFT },
278
	{ TTYC_KHOM3, KEYC_HOME|KEYC_ESCAPE },
279
	{ TTYC_KHOM4, KEYC_HOME|KEYC_SHIFT|KEYC_ESCAPE },
280
	{ TTYC_KHOM5, KEYC_HOME|KEYC_CTRL },
281
	{ TTYC_KHOM6, KEYC_HOME|KEYC_SHIFT|KEYC_CTRL },
282
	{ TTYC_KHOM7, KEYC_HOME|KEYC_ESCAPE|KEYC_CTRL },
283
	{ TTYC_KIC2, KEYC_IC|KEYC_SHIFT },
284
	{ TTYC_KIC3, KEYC_IC|KEYC_ESCAPE },
285
	{ TTYC_KIC4, KEYC_IC|KEYC_SHIFT|KEYC_ESCAPE },
286
	{ TTYC_KIC5, KEYC_IC|KEYC_CTRL },
287
	{ TTYC_KIC6, KEYC_IC|KEYC_SHIFT|KEYC_CTRL },
288
	{ TTYC_KIC7, KEYC_IC|KEYC_ESCAPE|KEYC_CTRL },
289
	{ TTYC_KLFT2, KEYC_LEFT|KEYC_SHIFT },
290
	{ TTYC_KLFT3, KEYC_LEFT|KEYC_ESCAPE },
291
	{ TTYC_KLFT4, KEYC_LEFT|KEYC_SHIFT|KEYC_ESCAPE },
292
	{ TTYC_KLFT5, KEYC_LEFT|KEYC_CTRL },
293
	{ TTYC_KLFT6, KEYC_LEFT|KEYC_SHIFT|KEYC_CTRL },
294
	{ TTYC_KLFT7, KEYC_LEFT|KEYC_ESCAPE|KEYC_CTRL },
295
	{ TTYC_KNXT2, KEYC_NPAGE|KEYC_SHIFT },
296
	{ TTYC_KNXT3, KEYC_NPAGE|KEYC_ESCAPE },
297
	{ TTYC_KNXT4, KEYC_NPAGE|KEYC_SHIFT|KEYC_ESCAPE },
298
	{ TTYC_KNXT5, KEYC_NPAGE|KEYC_CTRL },
299
	{ TTYC_KNXT6, KEYC_NPAGE|KEYC_SHIFT|KEYC_CTRL },
300
	{ TTYC_KNXT7, KEYC_NPAGE|KEYC_ESCAPE|KEYC_CTRL },
301
	{ TTYC_KPRV2, KEYC_PPAGE|KEYC_SHIFT },
302
	{ TTYC_KPRV3, KEYC_PPAGE|KEYC_ESCAPE },
303
	{ TTYC_KPRV4, KEYC_PPAGE|KEYC_SHIFT|KEYC_ESCAPE },
304
	{ TTYC_KPRV5, KEYC_PPAGE|KEYC_CTRL },
305
	{ TTYC_KPRV6, KEYC_PPAGE|KEYC_SHIFT|KEYC_CTRL },
306
	{ TTYC_KPRV7, KEYC_PPAGE|KEYC_ESCAPE|KEYC_CTRL },
307
	{ TTYC_KRIT2, KEYC_RIGHT|KEYC_SHIFT },
308
	{ TTYC_KRIT3, KEYC_RIGHT|KEYC_ESCAPE },
309
	{ TTYC_KRIT4, KEYC_RIGHT|KEYC_SHIFT|KEYC_ESCAPE },
310
	{ TTYC_KRIT5, KEYC_RIGHT|KEYC_CTRL },
311
	{ TTYC_KRIT6, KEYC_RIGHT|KEYC_SHIFT|KEYC_CTRL },
312
	{ TTYC_KRIT7, KEYC_RIGHT|KEYC_ESCAPE|KEYC_CTRL },
313
	{ TTYC_KUP2, KEYC_UP|KEYC_SHIFT },
314
	{ TTYC_KUP3, KEYC_UP|KEYC_ESCAPE },
315
	{ TTYC_KUP4, KEYC_UP|KEYC_SHIFT|KEYC_ESCAPE },
316
	{ TTYC_KUP5, KEYC_UP|KEYC_CTRL },
317
	{ TTYC_KUP6, KEYC_UP|KEYC_SHIFT|KEYC_CTRL },
318
	{ TTYC_KUP7, KEYC_UP|KEYC_ESCAPE|KEYC_CTRL },
319
};
320
321
/* Add key to tree. */
322
static void
323
tty_keys_add(struct tty *tty, const char *s, key_code key)
324
{
325
	struct tty_key	*tk;
326
	size_t		 size;
327
	const char     	*keystr;
328
329
	keystr = key_string_lookup_key(key);
330
	if ((tk = tty_keys_find(tty, s, strlen(s), &size)) == NULL) {
331
		log_debug("new key %s: 0x%llx (%s)", s, key, keystr);
332
		tty_keys_add1(&tty->key_tree, s, key);
333
	} else {
334
		log_debug("replacing key %s: 0x%llx (%s)", s, key, keystr);
335
		tk->key = key;
336
	}
337
}
338
339
/* Add next node to the tree. */
340
static void
341
tty_keys_add1(struct tty_key **tkp, const char *s, key_code key)
342
{
343
	struct tty_key	*tk;
344
345
	/* Allocate a tree entry if there isn't one already. */
346
	tk = *tkp;
347
	if (tk == NULL) {
348
		tk = *tkp = xcalloc(1, sizeof *tk);
349
		tk->ch = *s;
350
		tk->key = KEYC_UNKNOWN;
351
	}
352
353
	/* Find the next entry. */
354
	if (*s == tk->ch) {
355
		/* Move forward in string. */
356
		s++;
357
358
		/* If this is the end of the string, no more is necessary. */
359
		if (*s == '\0') {
360
			tk->key = key;
361
			return;
362
		}
363
364
		/* Use the child tree for the next character. */
365
		tkp = &tk->next;
366
	} else {
367
		if (*s < tk->ch)
368
			tkp = &tk->left;
369
		else if (*s > tk->ch)
370
			tkp = &tk->right;
371
	}
372
373
	/* And recurse to add it. */
374
	tty_keys_add1(tkp, s, key);
375
}
376
377
/* Initialise a key tree from the table. */
378
void
379
tty_keys_build(struct tty *tty)
380
{
381
	const struct tty_default_key_raw	*tdkr;
382
	const struct tty_default_key_code	*tdkc;
383
	u_int		 			 i;
384
	const char				*s;
385
386
	if (tty->key_tree != NULL)
387
		tty_keys_free(tty);
388
	tty->key_tree = NULL;
389
390
	for (i = 0; i < nitems(tty_default_raw_keys); i++) {
391
		tdkr = &tty_default_raw_keys[i];
392
393
		s = tdkr->string;
394
		if (*s != '\0')
395
			tty_keys_add(tty, s, tdkr->key);
396
	}
397
	for (i = 0; i < nitems(tty_default_code_keys); i++) {
398
		tdkc = &tty_default_code_keys[i];
399
400
		s = tty_term_string(tty->term, tdkc->code);
401
		if (*s != '\0')
402
			tty_keys_add(tty, s, tdkc->key);
403
404
	}
405
}
406
407
/* Free the entire key tree. */
408
void
409
tty_keys_free(struct tty *tty)
410
{
411
	tty_keys_free1(tty->key_tree);
412
}
413
414
/* Free a single key. */
415
static void
416
tty_keys_free1(struct tty_key *tk)
417
{
418
	if (tk->next != NULL)
419
		tty_keys_free1(tk->next);
420
	if (tk->left != NULL)
421
		tty_keys_free1(tk->left);
422
	if (tk->right != NULL)
423
		tty_keys_free1(tk->right);
424
	free(tk);
425
}
426
427
/* Lookup a key in the tree. */
428
static struct tty_key *
429
tty_keys_find(struct tty *tty, const char *buf, size_t len, size_t *size)
430
{
431
	*size = 0;
432
	return (tty_keys_find1(tty->key_tree, buf, len, size));
433
}
434
435
/* Find the next node. */
436
static struct tty_key *
437
tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size)
438
{
439
	/* If the node is NULL, this is the end of the tree. No match. */
440
	if (tk == NULL)
441
		return (NULL);
442
443
	/* Pick the next in the sequence. */
444
	if (tk->ch == *buf) {
445
		/* Move forward in the string. */
446
		buf++; len--;
447
		(*size)++;
448
449
		/* At the end of the string, return the current node. */
450
		if (len == 0 || (tk->next == NULL && tk->key != KEYC_UNKNOWN))
451
			return (tk);
452
453
		/* Move into the next tree for the following character. */
454
		tk = tk->next;
455
	} else {
456
		if (*buf < tk->ch)
457
			tk = tk->left;
458
		else if (*buf > tk->ch)
459
			tk = tk->right;
460
	}
461
462
	/* Move to the next in the tree. */
463
	return (tty_keys_find1(tk, buf, len, size));
464
}
465
466
/* Look up part of the next key. */
467
static int
468
tty_keys_next1(struct tty *tty, const char *buf, size_t len, key_code *key,
469
    size_t *size, int expired)
470
{
471
	struct tty_key		*tk, *tk1;
472
	struct utf8_data	 ud;
473
	enum utf8_state		 more;
474
	u_int			 i;
475
	wchar_t			 wc;
476
477
	log_debug("next key is %zu (%.*s) (expired=%d)", len, (int)len, buf,
478
	    expired);
479
480
	/* Is this a known key? */
481
	tk = tty_keys_find(tty, buf, len, size);
482
	if (tk != NULL && tk->key != KEYC_UNKNOWN) {
483
		tk1 = tk;
484
		do
485
			log_debug("keys in list: %#llx", tk1->key);
486
		while ((tk1 = tk1->next) != NULL);
487
		if (tk->next != NULL && !expired)
488
			return (1);
489
		*key = tk->key;
490
		return (0);
491
	}
492
493
	/* Is this valid UTF-8? */
494
	more = utf8_open(&ud, (u_char)*buf);
495
	if (more == UTF8_MORE) {
496
		*size = ud.size;
497
		if (len < ud.size) {
498
			if (!expired)
499
				return (1);
500
			return (-1);
501
		}
502
		for (i = 1; i < ud.size; i++)
503
			more = utf8_append(&ud, (u_char)buf[i]);
504
		if (more != UTF8_DONE)
505
			return (-1);
506
507
		if (utf8_combine(&ud, &wc) != UTF8_DONE)
508
			return (-1);
509
		*key = wc;
510
511
		log_debug("UTF-8 key %.*s %#llx", (int)ud.size, buf, *key);
512
		return (0);
513
	}
514
515
	return (-1);
516
}
517
518
/*
519
 * Process at least one key in the buffer and invoke tty->key_callback. Return
520
 * 0 if there are no further keys, or 1 if there could be more in the buffer.
521
 */
522
key_code
523
tty_keys_next(struct tty *tty)
524
{
525
	struct timeval	 tv;
526
	const char	*buf;
527
	size_t		 len, size;
528
	cc_t		 bspace;
529
	int		 delay, expired = 0, n;
530
	key_code	 key;
531
532
	/* Get key buffer. */
533
	buf = EVBUFFER_DATA(tty->event->input);
534
	len = EVBUFFER_LENGTH(tty->event->input);
535
536
	if (len == 0)
537
		return (0);
538
	log_debug("keys are %zu (%.*s)", len, (int)len, buf);
539
540
	/* Is this a mouse key press? */
541
	switch (tty_keys_mouse(tty, buf, len, &size)) {
542
	case 0:		/* yes */
543
		key = KEYC_MOUSE;
544
		goto complete_key;
545
	case -1:	/* no, or not valid */
546
		break;
547
	case -2:	/* yes, but we don't care. */
548
		key = KEYC_MOUSE;
549
		goto discard_key;
550
	case 1:		/* partial */
551
		goto partial_key;
552
	}
553
554
first_key:
555
	/* Handle keys starting with escape. */
556
	if (*buf == '\033') {
557
		/* Look for a key without the escape. */
558
		n = tty_keys_next1(tty, buf + 1, len - 1, &key, &size, expired);
559
		if (n == 0) {	/* found */
560
			key |= KEYC_ESCAPE;
561
			size++;
562
			goto complete_key;
563
		}
564
		if (n == 1)	/* partial */
565
			goto partial_key;
566
	}
567
568
	/* Try to lookup key. */
569
	n = tty_keys_next1(tty, buf, len, &key, &size, expired);
570
	if (n == 0)	/* found */
571
		goto complete_key;
572
	if (n == 1)
573
		goto partial_key;
574
575
	/* Is this an an xterm(1) key? */
576
	n = xterm_keys_find(buf, len, &size, &key);
577
	if (n == 0)
578
		goto complete_key;
579
	if (n == 1 && !expired)
580
		goto partial_key;
581
582
	/*
583
	 * At this point, we know the key is not partial (with or without
584
	 * escape). So pass it through even if the timer has not expired.
585
	 */
586
	if (*buf == '\033' && len >= 2) {
587
		key = (u_char)buf[1] | KEYC_ESCAPE;
588
		size = 2;
589
	} else {
590
		key = (u_char)buf[0];
591
		size = 1;
592
	}
593
	goto complete_key;
594
595
partial_key:
596
	log_debug("partial key %.*s", (int)len, buf);
597
598
	/* If timer is going, check for expiration. */
599
	if (tty->flags & TTY_TIMER) {
600
		if (evtimer_initialized(&tty->key_timer) &&
601
		    !evtimer_pending(&tty->key_timer, NULL)) {
602
			expired = 1;
603
			goto first_key;
604
		}
605
		return (0);
606
	}
607
608
	/* Get the time period. */
609
	delay = options_get_number(global_options, "escape-time");
610
	tv.tv_sec = delay / 1000;
611
	tv.tv_usec = (delay % 1000) * 1000L;
612
613
	/* Start the timer. */
614
	if (event_initialized(&tty->key_timer))
615
		evtimer_del(&tty->key_timer);
616
	evtimer_set(&tty->key_timer, tty_keys_callback, tty);
617
	evtimer_add(&tty->key_timer, &tv);
618
619
	tty->flags |= TTY_TIMER;
620
	return (0);
621
622
complete_key:
623
	log_debug("complete key %.*s %#llx", (int)size, buf, key);
624
625
	/*
626
	 * Check for backspace key using termios VERASE - the terminfo
627
	 * kbs entry is extremely unreliable, so cannot be safely
628
	 * used. termios should have a better idea.
629
	 */
630
	bspace = tty->tio.c_cc[VERASE];
631
	if (bspace != _POSIX_VDISABLE && (key & KEYC_MASK_KEY) == bspace)
632
		key = (key & KEYC_MASK_MOD) | KEYC_BSPACE;
633
634
	/* Remove data from buffer. */
635
	evbuffer_drain(tty->event->input, size);
636
637
	/* Remove key timer. */
638
	if (event_initialized(&tty->key_timer))
639
		evtimer_del(&tty->key_timer);
640
	tty->flags &= ~TTY_TIMER;
641
642
	/* Check for focus events. */
643
	if (key == KEYC_FOCUS_OUT) {
644
		tty->client->flags &= ~CLIENT_FOCUSED;
645
		return (1);
646
	} else if (key == KEYC_FOCUS_IN) {
647
		tty->client->flags |= CLIENT_FOCUSED;
648
		return (1);
649
	}
650
651
	/* Fire the key. */
652
	if (key != KEYC_UNKNOWN)
653
		server_client_handle_key(tty->client, key);
654
655
	return (1);
656
657
discard_key:
658
	log_debug("discard key %.*s %#llx", (int)size, buf, key);
659
660
	/* Remove data from buffer. */
661
	evbuffer_drain(tty->event->input, size);
662
663
	return (1);
664
}
665
666
/* Key timer callback. */
667
static void
668
tty_keys_callback(__unused int fd, __unused short events, void *data)
669
{
670
	struct tty	*tty = data;
671
672
	if (tty->flags & TTY_TIMER) {
673
		while (tty_keys_next(tty))
674
			;
675
	}
676
}
677
678
/*
679
 * Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
680
 * (probably a mouse sequence but need more data).
681
 */
682
static int
683
tty_keys_mouse(struct tty *tty, const char *buf, size_t len, size_t *size)
684
{
685
	struct mouse_event	*m = &tty->mouse;
686
	u_int			 i, x, y, b, sgr_b;
687
	u_char			 sgr_type, c;
688
689
	/*
690
	 * Standard mouse sequences are \033[M followed by three characters
691
	 * indicating button, X and Y, all based at 32 with 1,1 top-left.
692
	 *
693
	 * UTF-8 mouse sequences are similar but the three are expressed as
694
	 * UTF-8 characters.
695
	 *
696
	 * SGR extended mouse sequences are \033[< followed by three numbers in
697
	 * decimal and separated by semicolons indicating button, X and Y. A
698
	 * trailing 'M' is click or scroll and trailing 'm' release. All are
699
	 * based at 0 with 1,1 top-left.
700
	 */
701
702
	*size = 0;
703
	x = y = b = sgr_b = 0;
704
	sgr_type = ' ';
705
706
	/* First two bytes are always \033[. */
707
	if (buf[0] != '\033')
708
		return (-1);
709
	if (len == 1)
710
		return (1);
711
	if (buf[1] != '[')
712
		return (-1);
713
	if (len == 2)
714
		return (1);
715
716
	/*
717
	 * Third byte is M in old standard (and UTF-8 extension which we do not
718
	 * support), < in SGR extension.
719
	 */
720
	if (buf[2] == 'M') {
721
		/* Read the three inputs. */
722
		*size = 3;
723
		for (i = 0; i < 3; i++) {
724
			if (len <= *size)
725
				return (1);
726
			c = (u_char)buf[(*size)++];
727
			if (i == 0)
728
				b = c;
729
			else if (i == 1)
730
				x = c;
731
			else
732
				y = c;
733
		}
734
		log_debug("mouse input: %.*s", (int)*size, buf);
735
736
		/* Check and return the mouse input. */
737
		if (b < 32)
738
			return (-1);
739
		b -= 32;
740
		if (x >= 33)
741
			x -= 33;
742
		else
743
			x = 256 - x;
744
		if (y >= 33)
745
			y -= 33;
746
		else
747
			y = 256 - y;
748
	} else if (buf[2] == '<') {
749
		/* Read the three inputs. */
750
		*size = 3;
751
		while (1) {
752
			if (len <= *size)
753
				return (1);
754
			c = (u_char)buf[(*size)++];
755
			if (c == ';')
756
				break;
757
			if (c < '0' || c > '9')
758
				return (-1);
759
			sgr_b = 10 * sgr_b + (c - '0');
760
		}
761
		while (1) {
762
			if (len <= *size)
763
				return (1);
764
			c = (u_char)buf[(*size)++];
765
			if (c == ';')
766
				break;
767
			if (c < '0' || c > '9')
768
				return (-1);
769
			x = 10 * x + (c - '0');
770
		}
771
		while (1) {
772
			if (len <= *size)
773
				return (1);
774
			c = (u_char)buf[(*size)++];
775
			if (c == 'M' || c == 'm')
776
				break;
777
			if (c < '0' || c > '9')
778
				return (-1);
779
			y = 10 * y + (c - '0');
780
		}
781
		log_debug("mouse input (SGR): %.*s", (int)*size, buf);
782
783
		/* Check and return the mouse input. */
784
		if (x < 1 || y < 1)
785
			return (-1);
786
		x--;
787
		y--;
788
		b = sgr_b;
789
790
		/* Type is M for press, m for release. */
791
		sgr_type = c;
792
		if (sgr_type == 'm')
793
			b |= 3;
794
795
		/*
796
		 * Some terminals (like PuTTY 0.63) mistakenly send
797
		 * button-release events for scroll-wheel button-press event.
798
		 * Discard it before it reaches any program running inside
799
		 * tmux.
800
		 */
801
		if (sgr_type == 'm' && (sgr_b & 64))
802
		    return (-2);
803
	} else
804
		return (-1);
805
806
	/* Fill mouse event. */
807
	m->lx = m->x;
808
	m->x = x;
809
	m->ly = m->y;
810
	m->y = y;
811
	m->lb = m->b;
812
	m->b = b;
813
	m->sgr_type = sgr_type;
814
	m->sgr_b = sgr_b;
815
816
	return (0);
817
}