Annotation of src/usr.bin/tmux/tty-keys.c, Revision 1.7
1.7 ! nicm 1: /* $OpenBSD: tty-keys.c,v 1.6 2009/10/26 13:02:53 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 <string.h>
1.3 nicm 23: #include <termios.h>
24: #include <unistd.h>
1.1 nicm 25:
26: #include "tmux.h"
27:
28: void tty_keys_add(struct tty *, const char *, int, int);
1.6 nicm 29: int tty_keys_mouse(char *, size_t, size_t *, struct mouse_event *);
1.1 nicm 30:
31: struct tty_key_ent {
32: enum tty_code_code code;
33: const char *string;
34:
35: int key;
36: int flags;
37: };
38:
39: struct tty_key_ent tty_keys[] = {
40: /* Function keys. */
41: { TTYC_KF1, NULL, KEYC_F1, TTYKEY_CTRL },
42: { TTYC_KF2, NULL, KEYC_F2, TTYKEY_CTRL },
43: { TTYC_KF3, NULL, KEYC_F3, TTYKEY_CTRL },
44: { TTYC_KF4, NULL, KEYC_F4, TTYKEY_CTRL },
45: { TTYC_KF5, NULL, KEYC_F5, TTYKEY_CTRL },
46: { TTYC_KF6, NULL, KEYC_F6, TTYKEY_CTRL },
47: { TTYC_KF7, NULL, KEYC_F7, TTYKEY_CTRL },
48: { TTYC_KF8, NULL, KEYC_F8, TTYKEY_CTRL },
49: { TTYC_KF9, NULL, KEYC_F9, TTYKEY_CTRL },
50: { TTYC_KF10, NULL, KEYC_F10, TTYKEY_CTRL },
51: { TTYC_KF11, NULL, KEYC_F11, TTYKEY_CTRL },
52: { TTYC_KF12, NULL, KEYC_F12, TTYKEY_CTRL },
53: { TTYC_KF13, NULL, KEYC_F13, TTYKEY_CTRL },
54: { TTYC_KF14, NULL, KEYC_F14, TTYKEY_CTRL },
55: { TTYC_KF15, NULL, KEYC_F15, TTYKEY_CTRL },
56: { TTYC_KF16, NULL, KEYC_F16, TTYKEY_CTRL },
57: { TTYC_KF17, NULL, KEYC_F17, TTYKEY_CTRL },
58: { TTYC_KF18, NULL, KEYC_F18, TTYKEY_CTRL },
59: { TTYC_KF19, NULL, KEYC_F19, TTYKEY_CTRL },
60: { TTYC_KF20, NULL, KEYC_F20, TTYKEY_CTRL },
61: { TTYC_KICH1, NULL, KEYC_IC, TTYKEY_CTRL },
62: { TTYC_KDCH1, NULL, KEYC_DC, TTYKEY_CTRL },
63: { TTYC_KHOME, NULL, KEYC_HOME, TTYKEY_CTRL },
64: { TTYC_KEND, NULL, KEYC_END, TTYKEY_CTRL },
65: { TTYC_KNP, NULL, KEYC_NPAGE, TTYKEY_CTRL },
66: { TTYC_KPP, NULL, KEYC_PPAGE, TTYKEY_CTRL },
67: { TTYC_KCBT, NULL, KEYC_BTAB, TTYKEY_CTRL },
68:
69: /* Arrow keys. */
70: { 0, "\033OA", KEYC_UP, TTYKEY_RAW },
71: { 0, "\033OB", KEYC_DOWN, TTYKEY_RAW },
72: { 0, "\033OC", KEYC_RIGHT, TTYKEY_RAW },
73: { 0, "\033OD", KEYC_LEFT, TTYKEY_RAW },
74:
75: { 0, "\033[A", KEYC_UP, TTYKEY_RAW },
76: { 0, "\033[B", KEYC_DOWN, TTYKEY_RAW },
77: { 0, "\033[C", KEYC_RIGHT, TTYKEY_RAW },
78: { 0, "\033[D", KEYC_LEFT, TTYKEY_RAW },
79:
1.2 nicm 80: { 0, "\033Oa", KEYC_UP | KEYC_CTRL, TTYKEY_RAW },
81: { 0, "\033Ob", KEYC_DOWN | KEYC_CTRL, TTYKEY_RAW },
82: { 0, "\033Oc", KEYC_RIGHT | KEYC_CTRL, TTYKEY_RAW },
83: { 0, "\033Od", KEYC_LEFT | KEYC_CTRL, TTYKEY_RAW },
84: { 0, "\033[a", KEYC_UP | KEYC_SHIFT, TTYKEY_RAW },
85: { 0, "\033[b", KEYC_DOWN | KEYC_SHIFT, TTYKEY_RAW },
86: { 0, "\033[c", KEYC_RIGHT | KEYC_SHIFT, TTYKEY_RAW },
87: { 0, "\033[d", KEYC_LEFT | KEYC_SHIFT, TTYKEY_RAW },
1.1 nicm 88:
89: { TTYC_KCUU1, NULL, KEYC_UP, TTYKEY_CTRL },
90: { TTYC_KCUD1, NULL, KEYC_DOWN, TTYKEY_CTRL },
91: { TTYC_KCUB1, NULL, KEYC_LEFT, TTYKEY_CTRL },
92: { TTYC_KCUF1, NULL, KEYC_RIGHT, TTYKEY_CTRL },
93:
94: /*
95: * Numeric keypad. termcap and terminfo are totally confusing for this.
96: * There are definitions for some keypad keys and for function keys,
97: * but these seem to now be used for the real function keys rather than
98: * for the keypad keys in application mode (which is different from
99: * what it says in the termcap file). So, we just hardcode the vt100
100: * escape sequences here and always put the terminal into keypad_xmit
101: * mode. Translation of numbers mode/applications mode is done in
102: * input-keys.c.
103: */
1.7 ! nicm 104: { 0, "\033Oo", KEYC_KP_SLASH, TTYKEY_RAW },
! 105: { 0, "\033Oj", KEYC_KP_STAR, TTYKEY_RAW },
! 106: { 0, "\033Om", KEYC_KP_MINUS, TTYKEY_RAW },
! 107: { 0, "\033Ow", KEYC_KP_SEVEN, TTYKEY_RAW },
! 108: { 0, "\033Ox", KEYC_KP_EIGHT, TTYKEY_RAW },
! 109: { 0, "\033Oy", KEYC_KP_NINE, TTYKEY_RAW },
! 110: { 0, "\033Ok", KEYC_KP_PLUS, TTYKEY_RAW },
! 111: { 0, "\033Ot", KEYC_KP_FOUR, TTYKEY_RAW },
! 112: { 0, "\033Ou", KEYC_KP_FIVE, TTYKEY_RAW },
! 113: { 0, "\033Ov", KEYC_KP_SIX, TTYKEY_RAW },
! 114: { 0, "\033Oq", KEYC_KP_ONE, TTYKEY_RAW },
! 115: { 0, "\033Or", KEYC_KP_TWO, TTYKEY_RAW },
! 116: { 0, "\033Os", KEYC_KP_THREE, TTYKEY_RAW },
! 117: { 0, "\033OM", KEYC_KP_ENTER, TTYKEY_RAW },
! 118: { 0, "\033Op", KEYC_KP_ZERO, TTYKEY_RAW },
! 119: { 0, "\033On", KEYC_KP_PERIOD, TTYKEY_RAW },
1.1 nicm 120: };
121:
122: RB_GENERATE(tty_keys, tty_key, entry, tty_keys_cmp);
123:
124: struct tty_key *tty_keys_find(struct tty *, char *, size_t, size_t *);
125:
126: int
127: tty_keys_cmp(struct tty_key *k1, struct tty_key *k2)
128: {
129: return (strcmp(k1->string, k2->string));
130: }
131:
132: void
133: tty_keys_add(struct tty *tty, const char *s, int key, int flags)
134: {
135: struct tty_key *tk, *tl;
136:
137: tk = xmalloc(sizeof *tk);
138: tk->string = xstrdup(s);
139: tk->key = key;
140: tk->flags = flags;
141:
142: if ((tl = RB_INSERT(tty_keys, &tty->ktree, tk)) != NULL) {
143: xfree(tk->string);
144: xfree(tk);
145: log_debug("key exists: %s (old %x, new %x)", s, tl->key, key);
146: return;
147: }
148:
149: if (strlen(tk->string) > tty->ksize)
150: tty->ksize = strlen(tk->string);
151: log_debug("new key %x: size now %zu (%s)", key, tty->ksize, tk->string);
152: }
153:
154: void
155: tty_keys_init(struct tty *tty)
156: {
157: struct tty_key_ent *tke;
158: u_int i;
159: const char *s;
160: char tmp[64];
161:
162: RB_INIT(&tty->ktree);
163:
164: tty->ksize = 0;
165: for (i = 0; i < nitems(tty_keys); i++) {
166: tke = &tty_keys[i];
167:
168: if (tke->flags & TTYKEY_RAW)
169: s = tke->string;
170: else {
171: if (!tty_term_has(tty->term, tke->code))
172: continue;
173: s = tty_term_string(tty->term, tke->code);
174: if (s[0] != '\033' || s[1] == '\0')
175: continue;
176: }
177:
178: tty_keys_add(tty, s + 1, tke->key, tke->flags);
179: if (tke->flags & TTYKEY_CTRL) {
180: if (strlcpy(tmp, s, sizeof tmp) >= sizeof tmp)
181: continue;
182: tmp[strlen(tmp) - 1] ^= 0x20;
1.2 nicm 183: tty_keys_add(tty, tmp + 1, tke->key | KEYC_CTRL, 0);
1.1 nicm 184: }
185: }
186: }
187:
188: void
189: tty_keys_free(struct tty *tty)
190: {
191: struct tty_key *tk;
192:
193: while (!RB_EMPTY(&tty->ktree)) {
194: tk = RB_ROOT(&tty->ktree);
195: RB_REMOVE(tty_keys, &tty->ktree, tk);
196: xfree(tk->string);
197: xfree(tk);
198: }
199: }
200:
201: struct tty_key *
202: tty_keys_find(struct tty *tty, char *buf, size_t len, size_t *size)
203: {
204: struct tty_key *tk, tl;
205: char *s;
206:
207: if (len == 0)
208: return (NULL);
209:
210: s = xmalloc(tty->ksize + 1);
211: for (*size = tty->ksize; (*size) > 0; (*size)--) {
212: if ((*size) > len)
213: continue;
214: memcpy(s, buf, *size);
215: s[*size] = '\0';
216:
217: log_debug2("looking for key: %s", s);
218:
219: tl.string = s;
220: tk = RB_FIND(tty_keys, &tty->ktree, &tl);
221: if (tk != NULL) {
222: log_debug2("got key: 0x%x", tk->key);
223: xfree(s);
224: return (tk);
225: }
226: }
227: xfree(s);
228:
229: return (NULL);
230: }
231:
232: int
1.5 nicm 233: tty_keys_next(struct tty *tty, int *key, struct mouse_event *mouse)
1.1 nicm 234: {
235: struct tty_key *tk;
236: struct timeval tv;
237: char *buf;
238: size_t len, size;
1.3 nicm 239: cc_t bspace;
1.1 nicm 240:
241: buf = BUFFER_OUT(tty->in);
242: len = BUFFER_USED(tty->in);
243: if (len == 0)
244: return (1);
245: log_debug("keys are %zu (%.*s)", len, (int) len, buf);
246:
247: /* If a normal key, return it. */
248: if (*buf != '\033') {
249: *key = buffer_read8(tty->in);
1.3 nicm 250:
251: /*
252: * Check for backspace key using termios VERASE - the terminfo
253: * kbs entry is extremely unreliable, so cannot be safely
254: * used. termios should have a better idea.
255: */
256: bspace = tty->tio.c_cc[VERASE];
257: if (bspace != _POSIX_VDISABLE && *key == bspace)
258: *key = KEYC_BSPACE;
1.1 nicm 259: goto found;
260: }
261:
262: /* Look for matching key string and return if found. */
263: tk = tty_keys_find(tty, buf + 1, len - 1, &size);
264: if (tk != NULL) {
265: buffer_remove(tty->in, size + 1);
266: *key = tk->key;
267: goto found;
268: }
269:
270: /* Not found. Is this a mouse key press? */
1.6 nicm 271: *key = tty_keys_mouse(buf, len, &size, mouse);
1.1 nicm 272: if (*key != KEYC_NONE) {
273: buffer_remove(tty->in, size);
274: goto found;
275: }
276:
277: /* Escape but no key string. If the timer isn't started, start it. */
278: if (!(tty->flags & TTY_ESCAPE)) {
279: tv.tv_sec = 0;
280: tv.tv_usec = ESCAPE_PERIOD * 1000L;
281: if (gettimeofday(&tty->key_timer, NULL) != 0)
1.4 nicm 282: fatal("gettimeofday failed");
1.1 nicm 283: timeradd(&tty->key_timer, &tv, &tty->key_timer);
284:
285: tty->flags |= TTY_ESCAPE;
286: return (1);
287: }
288:
289: /* Skip the escape. */
290: buf++;
291: len--;
292:
293: /* Is there a normal key following? */
294: if (len != 0 && *buf != '\033') {
295: buffer_remove(tty->in, 1);
1.2 nicm 296: *key = buffer_read8(tty->in) | KEYC_ESCAPE;
1.1 nicm 297: goto found;
298: }
299:
300: /* Or a key string? */
301: if (len > 1) {
302: tk = tty_keys_find(tty, buf + 1, len - 1, &size);
303: if (tk != NULL) {
304: buffer_remove(tty->in, size + 2);
1.2 nicm 305: *key = tk->key | KEYC_ESCAPE;
1.1 nicm 306: goto found;
307: }
308: }
309:
310: /* If the timer hasn't expired, keep waiting. */
311: if (gettimeofday(&tv, NULL) != 0)
1.4 nicm 312: fatal("gettimeofday failed");
1.1 nicm 313: if (timercmp(&tty->key_timer, &tv, >))
314: return (1);
315:
316: /* Give up and return the escape. */
317: buffer_remove(tty->in, 1);
318: *key = '\033';
319:
320: found:
321: tty->flags &= ~TTY_ESCAPE;
322: return (0);
323: }
324:
325: int
1.6 nicm 326: tty_keys_mouse(char *buf, size_t len, size_t *size, struct mouse_event *m)
1.1 nicm 327: {
328: /*
329: * Mouse sequences are \033[M followed by three characters indicating
330: * buttons, X and Y, all based at 32 with 1,1 top-left.
331: */
332:
333: log_debug("mouse input is: %.*s", (int) len, buf);
334: if (len != 6 || memcmp(buf, "\033[M", 3) != 0)
335: return (KEYC_NONE);
336: *size = 6;
337:
1.5 nicm 338: m->b = buf[3];
339: m->x = buf[4];
340: m->y = buf[5];
341: if (m->b < 32 || m->x < 33 || m->y < 33)
1.1 nicm 342: return (KEYC_NONE);
1.5 nicm 343: m->b -= 32;
344: m->x -= 33;
345: m->y -= 33;
1.1 nicm 346: return (KEYC_MOUSE);
347: }