Last change erroneously used the target argument for looking up the
[tmux-openbsd.git] / tty-keys.c
blob59002fb2fc5b9e1172bd5b170e01ea40e04e1358
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 <string.h>
23 #include <termios.h>
24 #include <unistd.h>
26 #include "tmux.h"
29 * Handle keys input from the outside terminal. tty_keys[] is a base table of
30 * supported keys which are looked up in terminfo(5) and translated into a
31 * ternary tree (a binary tree of binary trees).
34 void tty_keys_add1(struct tty_key **, const char *, int);
35 void tty_keys_add(struct tty *, const char *, int);
36 void tty_keys_free1(struct tty_key *);
37 struct tty_key *tty_keys_find1(
38 struct tty_key *, const char *, size_t, size_t *);
39 struct tty_key *tty_keys_find(struct tty *, const char *, size_t, size_t *);
40 void tty_keys_callback(int, short, void *);
41 int tty_keys_mouse(
42 const char *, size_t, size_t *, struct mouse_event *);
44 struct tty_key_ent {
45 enum tty_code_code code;
46 const char *string;
48 int key;
49 int flags;
50 #define TTYKEY_RAW 0x1
54 * Default key tables. Those flagged with TTYKEY_RAW are inserted directly,
55 * otherwise they are looked up in terminfo(5).
57 struct tty_key_ent tty_keys[] = {
59 * Numeric keypad. Just use the vt100 escape sequences here and always
60 * put the terminal into keypad_xmit mode. Translation of numbers
61 * mode/applications mode is done in input-keys.c.
63 { 0, "\033Oo", KEYC_KP_SLASH, TTYKEY_RAW },
64 { 0, "\033Oj", KEYC_KP_STAR, TTYKEY_RAW },
65 { 0, "\033Om", KEYC_KP_MINUS, TTYKEY_RAW },
66 { 0, "\033Ow", KEYC_KP_SEVEN, TTYKEY_RAW },
67 { 0, "\033Ox", KEYC_KP_EIGHT, TTYKEY_RAW },
68 { 0, "\033Oy", KEYC_KP_NINE, TTYKEY_RAW },
69 { 0, "\033Ok", KEYC_KP_PLUS, TTYKEY_RAW },
70 { 0, "\033Ot", KEYC_KP_FOUR, TTYKEY_RAW },
71 { 0, "\033Ou", KEYC_KP_FIVE, TTYKEY_RAW },
72 { 0, "\033Ov", KEYC_KP_SIX, TTYKEY_RAW },
73 { 0, "\033Oq", KEYC_KP_ONE, TTYKEY_RAW },
74 { 0, "\033Or", KEYC_KP_TWO, TTYKEY_RAW },
75 { 0, "\033Os", KEYC_KP_THREE, TTYKEY_RAW },
76 { 0, "\033OM", KEYC_KP_ENTER, TTYKEY_RAW },
77 { 0, "\033Op", KEYC_KP_ZERO, TTYKEY_RAW },
78 { 0, "\033On", KEYC_KP_PERIOD, TTYKEY_RAW },
80 /* Arrow keys. */
81 { 0, "\033OA", KEYC_UP, TTYKEY_RAW },
82 { 0, "\033OB", KEYC_DOWN, TTYKEY_RAW },
83 { 0, "\033OC", KEYC_RIGHT, TTYKEY_RAW },
84 { 0, "\033OD", KEYC_LEFT, TTYKEY_RAW },
86 { 0, "\033[A", KEYC_UP, TTYKEY_RAW },
87 { 0, "\033[B", KEYC_DOWN, TTYKEY_RAW },
88 { 0, "\033[C", KEYC_RIGHT, TTYKEY_RAW },
89 { 0, "\033[D", KEYC_LEFT, TTYKEY_RAW },
91 /* rxvt-style arrow + modifier keys. */
92 { 0, "\033Oa", KEYC_UP|KEYC_CTRL, TTYKEY_RAW },
93 { 0, "\033Ob", KEYC_DOWN|KEYC_CTRL, TTYKEY_RAW },
94 { 0, "\033Oc", KEYC_RIGHT|KEYC_CTRL, TTYKEY_RAW },
95 { 0, "\033Od", KEYC_LEFT|KEYC_CTRL, TTYKEY_RAW },
97 { 0, "\033[a", KEYC_UP|KEYC_SHIFT, TTYKEY_RAW },
98 { 0, "\033[b", KEYC_DOWN|KEYC_SHIFT, TTYKEY_RAW },
99 { 0, "\033[c", KEYC_RIGHT|KEYC_SHIFT, TTYKEY_RAW },
100 { 0, "\033[d", KEYC_LEFT|KEYC_SHIFT, TTYKEY_RAW },
103 * rxvt-style function + modifier keys:
104 * Ctrl = ^, Shift = $, Ctrl+Shift = @
106 { 0, "\033[11^", KEYC_F1|KEYC_CTRL, TTYKEY_RAW },
107 { 0, "\033[12^", KEYC_F2|KEYC_CTRL, TTYKEY_RAW },
108 { 0, "\033[13^", KEYC_F3|KEYC_CTRL, TTYKEY_RAW },
109 { 0, "\033[14^", KEYC_F4|KEYC_CTRL, TTYKEY_RAW },
110 { 0, "\033[15^", KEYC_F5|KEYC_CTRL, TTYKEY_RAW },
111 { 0, "\033[17^", KEYC_F6|KEYC_CTRL, TTYKEY_RAW },
112 { 0, "\033[18^", KEYC_F7|KEYC_CTRL, TTYKEY_RAW },
113 { 0, "\033[19^", KEYC_F8|KEYC_CTRL, TTYKEY_RAW },
114 { 0, "\033[20^", KEYC_F9|KEYC_CTRL, TTYKEY_RAW },
115 { 0, "\033[21^", KEYC_F10|KEYC_CTRL, TTYKEY_RAW },
116 { 0, "\033[23^", KEYC_F11|KEYC_CTRL, TTYKEY_RAW },
117 { 0, "\033[24^", KEYC_F12|KEYC_CTRL, TTYKEY_RAW },
118 { 0, "\033[25^", KEYC_F13|KEYC_CTRL, TTYKEY_RAW },
119 { 0, "\033[26^", KEYC_F14|KEYC_CTRL, TTYKEY_RAW },
120 { 0, "\033[28^", KEYC_F15|KEYC_CTRL, TTYKEY_RAW },
121 { 0, "\033[29^", KEYC_F16|KEYC_CTRL, TTYKEY_RAW },
122 { 0, "\033[31^", KEYC_F17|KEYC_CTRL, TTYKEY_RAW },
123 { 0, "\033[32^", KEYC_F18|KEYC_CTRL, TTYKEY_RAW },
124 { 0, "\033[33^", KEYC_F19|KEYC_CTRL, TTYKEY_RAW },
125 { 0, "\033[34^", KEYC_F20|KEYC_CTRL, TTYKEY_RAW },
126 { 0, "\033[2^", KEYC_IC|KEYC_CTRL, TTYKEY_RAW },
127 { 0, "\033[3^", KEYC_DC|KEYC_CTRL, TTYKEY_RAW },
128 { 0, "\033[7^", KEYC_HOME|KEYC_CTRL, TTYKEY_RAW },
129 { 0, "\033[8^", KEYC_END|KEYC_CTRL, TTYKEY_RAW },
130 { 0, "\033[6^", KEYC_NPAGE|KEYC_CTRL, TTYKEY_RAW },
131 { 0, "\033[5^", KEYC_PPAGE|KEYC_CTRL, TTYKEY_RAW },
133 { 0, "\033[11$", KEYC_F1|KEYC_SHIFT, TTYKEY_RAW },
134 { 0, "\033[12$", KEYC_F2|KEYC_SHIFT, TTYKEY_RAW },
135 { 0, "\033[13$", KEYC_F3|KEYC_SHIFT, TTYKEY_RAW },
136 { 0, "\033[14$", KEYC_F4|KEYC_SHIFT, TTYKEY_RAW },
137 { 0, "\033[15$", KEYC_F5|KEYC_SHIFT, TTYKEY_RAW },
138 { 0, "\033[17$", KEYC_F6|KEYC_SHIFT, TTYKEY_RAW },
139 { 0, "\033[18$", KEYC_F7|KEYC_SHIFT, TTYKEY_RAW },
140 { 0, "\033[19$", KEYC_F8|KEYC_SHIFT, TTYKEY_RAW },
141 { 0, "\033[20$", KEYC_F9|KEYC_SHIFT, TTYKEY_RAW },
142 { 0, "\033[21$", KEYC_F10|KEYC_SHIFT, TTYKEY_RAW },
143 { 0, "\033[23$", KEYC_F11|KEYC_SHIFT, TTYKEY_RAW },
144 { 0, "\033[24$", KEYC_F12|KEYC_SHIFT, TTYKEY_RAW },
145 { 0, "\033[25$", KEYC_F13|KEYC_SHIFT, TTYKEY_RAW },
146 { 0, "\033[26$", KEYC_F14|KEYC_SHIFT, TTYKEY_RAW },
147 { 0, "\033[28$", KEYC_F15|KEYC_SHIFT, TTYKEY_RAW },
148 { 0, "\033[29$", KEYC_F16|KEYC_SHIFT, TTYKEY_RAW },
149 { 0, "\033[31$", KEYC_F17|KEYC_SHIFT, TTYKEY_RAW },
150 { 0, "\033[32$", KEYC_F18|KEYC_SHIFT, TTYKEY_RAW },
151 { 0, "\033[33$", KEYC_F19|KEYC_SHIFT, TTYKEY_RAW },
152 { 0, "\033[34$", KEYC_F20|KEYC_SHIFT, TTYKEY_RAW },
153 { 0, "\033[2$", KEYC_IC|KEYC_SHIFT, TTYKEY_RAW },
154 { 0, "\033[3$", KEYC_DC|KEYC_SHIFT, TTYKEY_RAW },
155 { 0, "\033[7$", KEYC_HOME|KEYC_SHIFT, TTYKEY_RAW },
156 { 0, "\033[8$", KEYC_END|KEYC_SHIFT, TTYKEY_RAW },
157 { 0, "\033[6$", KEYC_NPAGE|KEYC_SHIFT, TTYKEY_RAW },
158 { 0, "\033[5$", KEYC_PPAGE|KEYC_SHIFT, TTYKEY_RAW },
160 { 0, "\033[11@", KEYC_F1|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
161 { 0, "\033[12@", KEYC_F2|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
162 { 0, "\033[13@", KEYC_F3|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
163 { 0, "\033[14@", KEYC_F4|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
164 { 0, "\033[15@", KEYC_F5|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
165 { 0, "\033[17@", KEYC_F6|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
166 { 0, "\033[18@", KEYC_F7|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
167 { 0, "\033[19@", KEYC_F8|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
168 { 0, "\033[20@", KEYC_F9|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
169 { 0, "\033[21@", KEYC_F10|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
170 { 0, "\033[23@", KEYC_F11|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
171 { 0, "\033[24@", KEYC_F12|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
172 { 0, "\033[25@", KEYC_F13|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
173 { 0, "\033[26@", KEYC_F14|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
174 { 0, "\033[28@", KEYC_F15|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
175 { 0, "\033[29@", KEYC_F16|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
176 { 0, "\033[31@", KEYC_F17|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
177 { 0, "\033[32@", KEYC_F18|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
178 { 0, "\033[33@", KEYC_F19|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
179 { 0, "\033[34@", KEYC_F20|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
180 { 0, "\033[2@", KEYC_IC|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
181 { 0, "\033[3@", KEYC_DC|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
182 { 0, "\033[7@", KEYC_HOME|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
183 { 0, "\033[8@", KEYC_END|KEYC_CTRL|KEYC_SHIFT, TTYKEY_RAW },
184 { 0, "\033[6@", KEYC_NPAGE|KEYC_CTRL|KEYC_SHIFT,TTYKEY_RAW },
185 { 0, "\033[5@", KEYC_PPAGE|KEYC_CTRL|KEYC_SHIFT,TTYKEY_RAW },
187 /* terminfo lookups below this line so they can override raw keys. */
189 /* Function keys. */
190 { TTYC_KF1, NULL, KEYC_F1, 0 },
191 { TTYC_KF2, NULL, KEYC_F2, 0 },
192 { TTYC_KF3, NULL, KEYC_F3, 0 },
193 { TTYC_KF4, NULL, KEYC_F4, 0 },
194 { TTYC_KF5, NULL, KEYC_F5, 0 },
195 { TTYC_KF6, NULL, KEYC_F6, 0 },
196 { TTYC_KF7, NULL, KEYC_F7, 0 },
197 { TTYC_KF8, NULL, KEYC_F8, 0 },
198 { TTYC_KF9, NULL, KEYC_F9, 0 },
199 { TTYC_KF10, NULL, KEYC_F10, 0 },
200 { TTYC_KF11, NULL, KEYC_F11, 0 },
201 { TTYC_KF12, NULL, KEYC_F12, 0 },
202 { TTYC_KF13, NULL, KEYC_F13, 0 },
203 { TTYC_KF14, NULL, KEYC_F14, 0 },
204 { TTYC_KF15, NULL, KEYC_F15, 0 },
205 { TTYC_KF16, NULL, KEYC_F16, 0 },
206 { TTYC_KF17, NULL, KEYC_F17, 0 },
207 { TTYC_KF18, NULL, KEYC_F18, 0 },
208 { TTYC_KF19, NULL, KEYC_F19, 0 },
209 { TTYC_KF20, NULL, KEYC_F20, 0 },
210 { TTYC_KICH1, NULL, KEYC_IC, 0 },
211 { TTYC_KDCH1, NULL, KEYC_DC, 0 },
212 { TTYC_KHOME, NULL, KEYC_HOME, 0 },
213 { TTYC_KEND, NULL, KEYC_END, 0 },
214 { TTYC_KNP, NULL, KEYC_NPAGE, 0 },
215 { TTYC_KPP, NULL, KEYC_PPAGE, 0 },
216 { TTYC_KCBT, NULL, KEYC_BTAB, 0 },
218 /* Arrow keys from terminfo. */
219 { TTYC_KCUU1, NULL, KEYC_UP, 0 },
220 { TTYC_KCUD1, NULL, KEYC_DOWN, 0 },
221 { TTYC_KCUB1, NULL, KEYC_LEFT, 0 },
222 { TTYC_KCUF1, NULL, KEYC_RIGHT, 0 },
224 /* Key and modifier capabilities. */
225 { TTYC_KDC2, NULL, KEYC_DC|KEYC_SHIFT, 0 },
226 { TTYC_KDC3, NULL, KEYC_DC|KEYC_ESCAPE, 0 },
227 { TTYC_KDC4, NULL, KEYC_DC|KEYC_SHIFT|KEYC_ESCAPE, 0 },
228 { TTYC_KDC5, NULL, KEYC_DC|KEYC_CTRL, 0 },
229 { TTYC_KDC6, NULL, KEYC_DC|KEYC_SHIFT|KEYC_CTRL, 0 },
230 { TTYC_KDC7, NULL, KEYC_DC|KEYC_ESCAPE|KEYC_CTRL, 0 },
231 { TTYC_KDN2, NULL, KEYC_DOWN|KEYC_SHIFT, 0 },
232 { TTYC_KDN3, NULL, KEYC_DOWN|KEYC_ESCAPE, 0 },
233 { TTYC_KDN4, NULL, KEYC_DOWN|KEYC_SHIFT|KEYC_ESCAPE, 0 },
234 { TTYC_KDN5, NULL, KEYC_DOWN|KEYC_CTRL, 0 },
235 { TTYC_KDN6, NULL, KEYC_DOWN|KEYC_SHIFT|KEYC_CTRL, 0 },
236 { TTYC_KDN7, NULL, KEYC_DOWN|KEYC_ESCAPE|KEYC_CTRL, 0 },
237 { TTYC_KEND2, NULL, KEYC_END|KEYC_SHIFT, 0 },
238 { TTYC_KEND3, NULL, KEYC_END|KEYC_ESCAPE, 0 },
239 { TTYC_KEND4, NULL, KEYC_END|KEYC_SHIFT|KEYC_ESCAPE, 0 },
240 { TTYC_KEND5, NULL, KEYC_END|KEYC_CTRL, 0 },
241 { TTYC_KEND6, NULL, KEYC_END|KEYC_SHIFT|KEYC_CTRL, 0 },
242 { TTYC_KEND7, NULL, KEYC_END|KEYC_ESCAPE|KEYC_CTRL, 0 },
243 { TTYC_KHOM2, NULL, KEYC_HOME|KEYC_SHIFT, 0 },
244 { TTYC_KHOM3, NULL, KEYC_HOME|KEYC_ESCAPE, 0 },
245 { TTYC_KHOM4, NULL, KEYC_HOME|KEYC_SHIFT|KEYC_ESCAPE, 0 },
246 { TTYC_KHOM5, NULL, KEYC_HOME|KEYC_CTRL, 0 },
247 { TTYC_KHOM6, NULL, KEYC_HOME|KEYC_SHIFT|KEYC_CTRL, 0 },
248 { TTYC_KHOM7, NULL, KEYC_HOME|KEYC_ESCAPE|KEYC_CTRL, 0 },
249 { TTYC_KIC2, NULL, KEYC_IC|KEYC_SHIFT, 0 },
250 { TTYC_KIC3, NULL, KEYC_IC|KEYC_ESCAPE, 0 },
251 { TTYC_KIC4, NULL, KEYC_IC|KEYC_SHIFT|KEYC_ESCAPE, 0 },
252 { TTYC_KIC5, NULL, KEYC_IC|KEYC_CTRL, 0 },
253 { TTYC_KIC6, NULL, KEYC_IC|KEYC_SHIFT|KEYC_CTRL, 0 },
254 { TTYC_KIC7, NULL, KEYC_IC|KEYC_ESCAPE|KEYC_CTRL, 0 },
255 { TTYC_KLFT2, NULL, KEYC_LEFT|KEYC_SHIFT, 0 },
256 { TTYC_KLFT3, NULL, KEYC_LEFT|KEYC_ESCAPE, 0 },
257 { TTYC_KLFT4, NULL, KEYC_LEFT|KEYC_SHIFT|KEYC_ESCAPE, 0 },
258 { TTYC_KLFT5, NULL, KEYC_LEFT|KEYC_CTRL, 0 },
259 { TTYC_KLFT6, NULL, KEYC_LEFT|KEYC_SHIFT|KEYC_CTRL, 0 },
260 { TTYC_KLFT7, NULL, KEYC_LEFT|KEYC_ESCAPE|KEYC_CTRL, 0 },
261 { TTYC_KNXT2, NULL, KEYC_NPAGE|KEYC_SHIFT, 0 },
262 { TTYC_KNXT3, NULL, KEYC_NPAGE|KEYC_ESCAPE, 0 },
263 { TTYC_KNXT4, NULL, KEYC_NPAGE|KEYC_SHIFT|KEYC_ESCAPE, 0 },
264 { TTYC_KNXT5, NULL, KEYC_NPAGE|KEYC_CTRL, 0 },
265 { TTYC_KNXT6, NULL, KEYC_NPAGE|KEYC_SHIFT|KEYC_CTRL, 0 },
266 { TTYC_KNXT7, NULL, KEYC_NPAGE|KEYC_ESCAPE|KEYC_CTRL, 0 },
267 { TTYC_KPRV2, NULL, KEYC_PPAGE|KEYC_SHIFT, 0 },
268 { TTYC_KPRV3, NULL, KEYC_PPAGE|KEYC_ESCAPE, 0 },
269 { TTYC_KPRV4, NULL, KEYC_PPAGE|KEYC_SHIFT|KEYC_ESCAPE, 0 },
270 { TTYC_KPRV5, NULL, KEYC_PPAGE|KEYC_CTRL, 0 },
271 { TTYC_KPRV6, NULL, KEYC_PPAGE|KEYC_SHIFT|KEYC_CTRL, 0 },
272 { TTYC_KPRV7, NULL, KEYC_PPAGE|KEYC_ESCAPE|KEYC_CTRL, 0 },
273 { TTYC_KRIT2, NULL, KEYC_RIGHT|KEYC_SHIFT, 0 },
274 { TTYC_KRIT3, NULL, KEYC_RIGHT|KEYC_ESCAPE, 0 },
275 { TTYC_KRIT4, NULL, KEYC_RIGHT|KEYC_SHIFT|KEYC_ESCAPE, 0 },
276 { TTYC_KRIT5, NULL, KEYC_RIGHT|KEYC_CTRL, 0 },
277 { TTYC_KRIT6, NULL, KEYC_RIGHT|KEYC_SHIFT|KEYC_CTRL, 0 },
278 { TTYC_KRIT7, NULL, KEYC_RIGHT|KEYC_ESCAPE|KEYC_CTRL, 0 },
279 { TTYC_KUP2, NULL, KEYC_UP|KEYC_SHIFT, 0 },
280 { TTYC_KUP3, NULL, KEYC_UP|KEYC_ESCAPE, 0 },
281 { TTYC_KUP4, NULL, KEYC_UP|KEYC_SHIFT|KEYC_ESCAPE, 0 },
282 { TTYC_KUP5, NULL, KEYC_UP|KEYC_CTRL, 0 },
283 { TTYC_KUP6, NULL, KEYC_UP|KEYC_SHIFT|KEYC_CTRL, 0 },
284 { TTYC_KUP7, NULL, KEYC_UP|KEYC_ESCAPE|KEYC_CTRL, 0 },
287 void
288 tty_keys_add(struct tty *tty, const char *s, int key)
290 struct tty_key *tk;
291 size_t size;
292 const char *keystr;
294 keystr = key_string_lookup_key(key);
295 if ((tk = tty_keys_find(tty, s, strlen(s), &size)) == NULL) {
296 log_debug("new key %s: 0x%x (%s)", s, key, keystr);
297 tty_keys_add1(&tty->key_tree, s, key);
298 } else {
299 log_debug("replacing key %s: 0x%x (%s)", s, key, keystr);
300 tk->key = key;
304 /* Add next node to the tree. */
305 void
306 tty_keys_add1(struct tty_key **tkp, const char *s, int key)
308 struct tty_key *tk;
310 /* Allocate a tree entry if there isn't one already. */
311 tk = *tkp;
312 if (tk == NULL) {
313 tk = *tkp = xcalloc(1, sizeof *tk);
314 tk->ch = *s;
315 tk->key = KEYC_NONE;
318 /* Find the next entry. */
319 if (*s == tk->ch) {
320 /* Move forward in string. */
321 s++;
323 /* If this is the end of the string, no more is necessary. */
324 if (*s == '\0') {
325 tk->key = key;
326 return;
329 /* Use the child tree for the next character. */
330 tkp = &tk->next;
331 } else {
332 if (*s < tk->ch)
333 tkp = &tk->left;
334 else if (*s > tk->ch)
335 tkp = &tk->right;
338 /* And recurse to add it. */
339 tty_keys_add1(tkp, s, key);
342 /* Initialise a key tree from the table. */
343 void
344 tty_keys_init(struct tty *tty)
346 struct tty_key_ent *tke;
347 u_int i;
348 const char *s;
350 tty->key_tree = NULL;
351 for (i = 0; i < nitems(tty_keys); i++) {
352 tke = &tty_keys[i];
354 if (tke->flags & TTYKEY_RAW)
355 s = tke->string;
356 else {
357 if (!tty_term_has(tty->term, tke->code))
358 continue;
359 s = tty_term_string(tty->term, tke->code);
361 if (s[0] != '\033' || s[1] == '\0')
362 continue;
364 tty_keys_add(tty, s + 1, tke->key);
368 /* Free the entire key tree. */
369 void
370 tty_keys_free(struct tty *tty)
372 tty_keys_free1(tty->key_tree);
375 /* Free a single key. */
376 void
377 tty_keys_free1(struct tty_key *tk)
379 if (tk->next != NULL)
380 tty_keys_free1(tk->next);
381 if (tk->left != NULL)
382 tty_keys_free1(tk->left);
383 if (tk->right != NULL)
384 tty_keys_free1(tk->right);
385 xfree(tk);
388 /* Lookup a key in the tree. */
389 struct tty_key *
390 tty_keys_find(struct tty *tty, const char *buf, size_t len, size_t *size)
392 *size = 0;
393 return (tty_keys_find1(tty->key_tree, buf, len, size));
396 /* Find the next node. */
397 struct tty_key *
398 tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size)
400 /* If the node is NULL, this is the end of the tree. No match. */
401 if (tk == NULL)
402 return (NULL);
404 /* Pick the next in the sequence. */
405 if (tk->ch == *buf) {
406 /* Move forward in the string. */
407 buf++; len--;
408 (*size)++;
410 /* At the end of the string, return the current node. */
411 if (len == 0 || (tk->next == NULL && tk->key != KEYC_NONE))
412 return (tk);
414 /* Move into the next tree for the following character. */
415 tk = tk->next;
416 } else {
417 if (*buf < tk->ch)
418 tk = tk->left;
419 else if (*buf > tk->ch)
420 tk = tk->right;
423 /* Move to the next in the tree. */
424 return (tty_keys_find1(tk, buf, len, size));
428 * Process at least one key in the buffer and invoke tty->key_callback. Return
429 * 0 if there are no further keys, or 1 if there could be more in the buffer.
432 tty_keys_next(struct tty *tty)
434 struct tty_key *tk;
435 struct timeval tv;
436 struct mouse_event mouse;
437 const char *buf;
438 size_t len, size;
439 cc_t bspace;
440 int key, delay;
442 buf = EVBUFFER_DATA(tty->event->input);
443 len = EVBUFFER_LENGTH(tty->event->input);
444 if (len == 0)
445 return (0);
446 log_debug("keys are %zu (%.*s)", len, (int) len, buf);
448 /* If a normal key, return it. */
449 if (*buf != '\033') {
450 key = (u_char) *buf;
451 evbuffer_drain(tty->event->input, 1);
454 * Check for backspace key using termios VERASE - the terminfo
455 * kbs entry is extremely unreliable, so cannot be safely
456 * used. termios should have a better idea.
458 bspace = tty->tio.c_cc[VERASE];
459 if (bspace != _POSIX_VDISABLE && key == bspace)
460 key = KEYC_BSPACE;
461 goto handle_key;
464 /* Is this a mouse key press? */
465 switch (tty_keys_mouse(buf, len, &size, &mouse)) {
466 case 0: /* yes */
467 evbuffer_drain(tty->event->input, size);
468 key = KEYC_MOUSE;
469 goto handle_key;
470 case -1: /* no, or not valid */
471 break;
472 case 1: /* partial */
473 goto partial_key;
476 /* Try to parse a key with an xterm-style modifier. */
477 switch (xterm_keys_find(buf, len, &size, &key)) {
478 case 0: /* found */
479 evbuffer_drain(tty->event->input, size);
480 goto handle_key;
481 case -1: /* not found */
482 break;
483 case 1:
484 goto partial_key;
487 /* Look for matching key string and return if found. */
488 tk = tty_keys_find(tty, buf + 1, len - 1, &size);
489 if (tk != NULL) {
490 key = tk->key;
491 goto found_key;
494 /* Skip the escape. */
495 buf++;
496 len--;
498 /* Is there a normal key following? */
499 if (len != 0 && *buf != '\033') {
500 key = *buf | KEYC_ESCAPE;
501 evbuffer_drain(tty->event->input, 2);
502 goto handle_key;
505 /* Or a key string? */
506 if (len > 1) {
507 tk = tty_keys_find(tty, buf + 1, len - 1, &size);
508 if (tk != NULL) {
509 key = tk->key | KEYC_ESCAPE;
510 size++; /* include escape */
511 goto found_key;
515 /* Escape and then nothing useful - fall through. */
517 partial_key:
519 * Escape but no key string. If have already seen an escape, then the
520 * timer must have expired, so give up waiting and send the escape.
522 if (tty->flags & TTY_ESCAPE) {
523 evbuffer_drain(tty->event->input, 1);
524 key = '\033';
525 goto handle_key;
528 /* Fall through to start the timer. */
530 start_timer:
531 /* Start the timer and wait for expiry or more data. */
532 delay = options_get_number(&global_options, "escape-time");
533 tv.tv_sec = delay / 1000;
534 tv.tv_usec = (delay % 1000) * 1000L;
536 evtimer_del(&tty->key_timer);
537 evtimer_set(&tty->key_timer, tty_keys_callback, tty);
538 evtimer_add(&tty->key_timer, &tv);
540 tty->flags |= TTY_ESCAPE;
541 return (0);
543 found_key:
544 if (tk->next != NULL) {
545 /* Partial key. Start the timer if not already expired. */
546 if (!(tty->flags & TTY_ESCAPE))
547 goto start_timer;
549 /* Otherwise, if no key, send the escape alone. */
550 if (tk->key == KEYC_NONE)
551 goto partial_key;
553 /* Or fall through to send the partial key found. */
555 evbuffer_drain(tty->event->input, size + 1);
557 goto handle_key;
559 handle_key:
560 evtimer_del(&tty->key_timer);
562 tty->key_callback(key, &mouse, tty->key_data);
564 tty->flags &= ~TTY_ESCAPE;
565 return (1);
568 /* Key timer callback. */
569 /* ARGSUSED */
570 void
571 tty_keys_callback(unused int fd, unused short events, void *data)
573 struct tty *tty = data;
575 if (!(tty->flags & TTY_ESCAPE))
576 return;
578 while (tty_keys_next(tty))
583 * Handle mouse key input. Returns 0 for success, -1 for failure, 1 for partial
584 * (probably a mouse sequence but need more data).
587 tty_keys_mouse(const char *buf, size_t len, size_t *size, struct mouse_event *m)
590 * Mouse sequences are \033[M followed by three characters indicating
591 * buttons, X and Y, all based at 32 with 1,1 top-left.
594 *size = 0;
596 if (buf[0] != '\033')
597 return (-1);
598 if (len == 1)
599 return (1);
601 if (buf[1] != '[')
602 return (-1);
603 if (len == 2)
604 return (1);
606 if (buf[2] != 'M')
607 return (-1);
608 if (len == 3)
609 return (1);
611 if (len < 6)
612 return (1);
613 *size = 6;
615 log_debug(
616 "mouse input: %.6s (%hhu,%hhu/%hhu)", buf, buf[4], buf[5], buf[3]);
618 m->b = buf[3];
619 m->x = buf[4];
620 m->y = buf[5];
621 if (m->b < 32 || m->x < 33 || m->y < 33)
622 return (-1);
623 m->b -= 32;
624 m->x -= 33;
625 m->y -= 33;
626 return (0);