Annotation of src/usr.bin/tmux/xterm-keys.c, Revision 1.16
1.16 ! nicm 1: /* $OpenBSD: xterm-keys.c,v 1.15 2014/07/21 10:25:48 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2009 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:
21: #include <string.h>
22:
23: #include "tmux.h"
24:
25: /*
26: * xterm-style function keys append one of the following values before the last
27: * character:
28: *
29: * 2 Shift
1.5 nicm 30: * 3 Alt
31: * 4 Shift + Alt
32: * 5 Ctrl
33: * 6 Shift + Ctrl
34: * 7 Alt + Ctrl
1.1 nicm 35: * 8 Shift + Alt + Ctrl
36: *
37: * Rather than parsing them, just match against a table.
38: *
1.10 nicm 39: * There are three forms for F1-F4 (\\033O_P and \\033O1;_P and \\033[1;_P).
40: * We accept any but always output the latter (it comes first in the table).
1.1 nicm 41: */
42:
1.14 nicm 43: int xterm_keys_match(const char *, const char *, size_t, size_t *, u_int *);
44: int xterm_keys_modifiers(const char *, size_t, size_t *, u_int *);
1.1 nicm 45:
46: struct xterm_keys_entry {
47: int key;
48: const char *template;
49: };
50:
1.8 nicm 51: const struct xterm_keys_entry xterm_keys_table[] = {
1.1 nicm 52: { KEYC_F1, "\033[1;_P" },
1.10 nicm 53: { KEYC_F1, "\033O1;_P" },
1.3 nicm 54: { KEYC_F1, "\033O_P" },
1.1 nicm 55: { KEYC_F2, "\033[1;_Q" },
1.10 nicm 56: { KEYC_F2, "\033O1;_Q" },
1.3 nicm 57: { KEYC_F2, "\033O_Q" },
1.1 nicm 58: { KEYC_F3, "\033[1;_R" },
1.10 nicm 59: { KEYC_F3, "\033O1;_R" },
1.3 nicm 60: { KEYC_F3, "\033O_R" },
1.1 nicm 61: { KEYC_F4, "\033[1;_S" },
1.10 nicm 62: { KEYC_F4, "\033O1;_S" },
1.3 nicm 63: { KEYC_F4, "\033O_S" },
1.1 nicm 64: { KEYC_F5, "\033[15;_~" },
65: { KEYC_F6, "\033[17;_~" },
66: { KEYC_F7, "\033[18;_~" },
67: { KEYC_F8, "\033[19;_~" },
68: { KEYC_F9, "\033[20;_~" },
69: { KEYC_F10, "\033[21;_~" },
70: { KEYC_F11, "\033[23;_~" },
71: { KEYC_F12, "\033[24;_~" },
72: { KEYC_UP, "\033[1;_A" },
73: { KEYC_DOWN, "\033[1;_B" },
74: { KEYC_RIGHT, "\033[1;_C" },
75: { KEYC_LEFT, "\033[1;_D" },
76: { KEYC_HOME, "\033[1;_H" },
77: { KEYC_END, "\033[1;_F" },
78: { KEYC_PPAGE, "\033[5;_~" },
79: { KEYC_NPAGE, "\033[6;_~" },
80: { KEYC_IC, "\033[2;_~" },
81: { KEYC_DC, "\033[3;_~" },
1.11 nicm 82:
83: { '!', "\033[27;_;33~" },
84: { '#', "\033[27;_;35~" },
85: { '(', "\033[27;_;40~" },
86: { ')', "\033[27;_;41~" },
87: { '+', "\033[27;_;43~" },
88: { ',', "\033[27;_;44~" },
89: { '-', "\033[27;_;45~" },
90: { '.', "\033[27;_;46~" },
91: { '0', "\033[27;_;48~" },
92: { '1', "\033[27;_;49~" },
93: { '2', "\033[27;_;50~" },
94: { '3', "\033[27;_;51~" },
95: { '4', "\033[27;_;52~" },
96: { '5', "\033[27;_;53~" },
97: { '6', "\033[27;_;54~" },
98: { '7', "\033[27;_;55~" },
99: { '8', "\033[27;_;56~" },
100: { '9', "\033[27;_;57~" },
101: { ':', "\033[27;_;58~" },
102: { ';', "\033[27;_;59~" },
103: { '<', "\033[27;_;60~" },
104: { '=', "\033[27;_;61~" },
105: { '>', "\033[27;_;62~" },
106: { '?', "\033[27;_;63~" },
107: { '\'', "\033[27;_;39~" },
108: { '\r', "\033[27;_;13~" },
109: { '\t', "\033[27;_;9~" },
1.1 nicm 110: };
111:
1.5 nicm 112: /*
1.4 nicm 113: * Match key against buffer, treating _ as a wildcard. Return -1 for no match,
114: * 0 for match, 1 if the end of the buffer is reached (need more data).
115: */
1.1 nicm 116: int
1.14 nicm 117: xterm_keys_match(const char *template, const char *buf, size_t len,
118: size_t *size, u_int *modifiers)
1.1 nicm 119: {
120: size_t pos;
1.14 nicm 121: int retval;
1.16 ! nicm 122:
! 123: *modifiers = 0;
1.1 nicm 124:
1.4 nicm 125: if (len == 0)
1.1 nicm 126: return (0);
127:
128: pos = 0;
129: do {
1.14 nicm 130: if (*template == '_') {
131: retval = xterm_keys_modifiers(buf, len, &pos,
132: modifiers);
133: if (retval != 0)
134: return (retval);
1.13 nicm 135: continue;
1.14 nicm 136: }
1.13 nicm 137: if (buf[pos] != *template)
1.4 nicm 138: return (-1);
1.14 nicm 139: pos++;
140: } while (*++template != '\0' && pos != len);
1.1 nicm 141:
1.4 nicm 142: if (*template != '\0') /* partial */
143: return (1);
144:
1.14 nicm 145: *size = pos;
1.4 nicm 146: return (0);
1.1 nicm 147: }
148:
1.14 nicm 149: /* Find modifiers from buffer. */
1.1 nicm 150: int
1.14 nicm 151: xterm_keys_modifiers(const char *buf, size_t len, size_t *pos, u_int *modifiers)
1.1 nicm 152: {
1.14 nicm 153: u_int flags;
1.1 nicm 154:
1.14 nicm 155: if (len - *pos < 2)
156: return (1);
1.6 nicm 157:
1.14 nicm 158: if (buf[*pos] < '0' || buf[*pos] > '9')
159: return (-1);
160: flags = buf[(*pos)++] - '0';
161: if (buf[*pos] >= '0' && buf[*pos] <= '9')
162: flags = (flags * 10) + (buf[(*pos)++] - '0');
163: flags -= 1;
164:
165: *modifiers = 0;
166: if (flags & 1)
167: *modifiers |= KEYC_SHIFT;
168: if (flags & 2)
169: *modifiers |= KEYC_ESCAPE;
170: if (flags & 4)
171: *modifiers |= KEYC_CTRL;
172: if (flags & 8)
173: *modifiers |= KEYC_ESCAPE;
174: return (0);
1.1 nicm 175: }
176:
1.4 nicm 177: /*
178: * Lookup key from a buffer against the table. Returns 0 for found (and the
179: * key), -1 for not found, 1 for partial match.
180: */
1.1 nicm 181: int
1.4 nicm 182: xterm_keys_find(const char *buf, size_t len, size_t *size, int *key)
1.1 nicm 183: {
1.8 nicm 184: const struct xterm_keys_entry *entry;
1.14 nicm 185: u_int i, modifiers;
186: int matched;
1.1 nicm 187:
188: for (i = 0; i < nitems(xterm_keys_table); i++) {
189: entry = &xterm_keys_table[i];
1.14 nicm 190:
191: matched = xterm_keys_match(entry->template, buf, len, size,
192: &modifiers);
193: if (matched == -1)
194: continue;
195: if (matched == 0)
196: *key = entry->key | modifiers;
197: return (matched);
1.1 nicm 198: }
1.4 nicm 199: return (-1);
1.1 nicm 200: }
201:
202: /* Lookup a key number from the table. */
203: char *
204: xterm_keys_lookup(int key)
205: {
1.8 nicm 206: const struct xterm_keys_entry *entry;
207: u_int i;
208: int modifiers;
209: char *out;
1.1 nicm 210:
1.6 nicm 211: modifiers = 1;
212: if (key & KEYC_SHIFT)
213: modifiers += 1;
214: if (key & KEYC_ESCAPE)
215: modifiers += 2;
216: if (key & KEYC_CTRL)
217: modifiers += 4;
1.1 nicm 218:
219: /*
220: * If the key has no modifiers, return NULL and let it fall through to
221: * the normal lookup.
222: */
1.6 nicm 223: if (modifiers == 1)
1.1 nicm 224: return (NULL);
225:
226: /* Otherwise, find the key in the table. */
227: key &= ~(KEYC_SHIFT|KEYC_ESCAPE|KEYC_CTRL);
228: for (i = 0; i < nitems(xterm_keys_table); i++) {
229: entry = &xterm_keys_table[i];
230: if (key == entry->key)
231: break;
232: }
233: if (i == nitems(xterm_keys_table))
234: return (NULL);
1.5 nicm 235:
1.1 nicm 236: /* Copy the template and replace the modifier. */
237: out = xstrdup(entry->template);
238: out[strcspn(out, "_")] = '0' + modifiers;
239: return (out);
240: }