key-string.c revision 1.1.1.5.2.1 1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm (at) 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 int key_string_search_table(const char *);
26 int key_string_get_modifiers(const char **);
27
28 static const struct {
29 const char *string;
30 int key;
31 } key_string_table[] = {
32 /* Function keys. */
33 { "F1", KEYC_F1 },
34 { "F2", KEYC_F2 },
35 { "F3", KEYC_F3 },
36 { "F4", KEYC_F4 },
37 { "F5", KEYC_F5 },
38 { "F6", KEYC_F6 },
39 { "F7", KEYC_F7 },
40 { "F8", KEYC_F8 },
41 { "F9", KEYC_F9 },
42 { "F10", KEYC_F10 },
43 { "F11", KEYC_F11 },
44 { "F12", KEYC_F12 },
45 { "IC", KEYC_IC },
46 { "DC", KEYC_DC },
47 { "Home", KEYC_HOME },
48 { "End", KEYC_END },
49 { "NPage", KEYC_NPAGE },
50 { "PageDown", KEYC_NPAGE },
51 { "PgDn", KEYC_NPAGE },
52 { "PPage", KEYC_PPAGE },
53 { "PageUp", KEYC_PPAGE },
54 { "PgUp", KEYC_PPAGE },
55 { "Tab", '\011' },
56 { "BTab", KEYC_BTAB },
57 { "Space", ' ' },
58 { "BSpace", KEYC_BSPACE },
59 { "Enter", '\r' },
60 { "Escape", '\033' },
61
62 /* Arrow keys. */
63 { "Up", KEYC_UP },
64 { "Down", KEYC_DOWN },
65 { "Left", KEYC_LEFT },
66 { "Right", KEYC_RIGHT },
67
68 /* Numeric keypad. */
69 { "KP/", KEYC_KP_SLASH },
70 { "KP*", KEYC_KP_STAR },
71 { "KP-", KEYC_KP_MINUS },
72 { "KP7", KEYC_KP_SEVEN },
73 { "KP8", KEYC_KP_EIGHT },
74 { "KP9", KEYC_KP_NINE },
75 { "KP+", KEYC_KP_PLUS },
76 { "KP4", KEYC_KP_FOUR },
77 { "KP5", KEYC_KP_FIVE },
78 { "KP6", KEYC_KP_SIX },
79 { "KP1", KEYC_KP_ONE },
80 { "KP2", KEYC_KP_TWO },
81 { "KP3", KEYC_KP_THREE },
82 { "KPEnter", KEYC_KP_ENTER },
83 { "KP0", KEYC_KP_ZERO },
84 { "KP.", KEYC_KP_PERIOD },
85
86 /* Mouse keys. */
87 KEYC_MOUSE_STRING(MOUSEDOWN1, MouseDown1),
88 KEYC_MOUSE_STRING(MOUSEDOWN2, MouseDown2),
89 KEYC_MOUSE_STRING(MOUSEDOWN3, MouseDown3),
90 KEYC_MOUSE_STRING(MOUSEUP1, MouseUp1),
91 KEYC_MOUSE_STRING(MOUSEUP2, MouseUp2),
92 KEYC_MOUSE_STRING(MOUSEUP3, MouseUp3),
93 KEYC_MOUSE_STRING(MOUSEDRAG1, MouseDrag1),
94 KEYC_MOUSE_STRING(MOUSEDRAG2, MouseDrag2),
95 KEYC_MOUSE_STRING(MOUSEDRAG3, MouseDrag3),
96 KEYC_MOUSE_STRING(WHEELUP, WheelUp),
97 KEYC_MOUSE_STRING(WHEELDOWN, WheelDown),
98 KEYC_MOUSE_STRING(DOUBLECLICK1, DoubleClick1),
99 KEYC_MOUSE_STRING(DOUBLECLICK2, DoubleClick2),
100 KEYC_MOUSE_STRING(DOUBLECLICK3, DoubleClick3),
101 KEYC_MOUSE_STRING(TRIPLECLICK1, TripleClick1),
102 KEYC_MOUSE_STRING(TRIPLECLICK2, TripleClick2),
103 KEYC_MOUSE_STRING(TRIPLECLICK3, TripleClick3),
104 };
105
106 /* Find key string in table. */
107 int
108 key_string_search_table(const char *string)
109 {
110 u_int i;
111
112 for (i = 0; i < nitems(key_string_table); i++) {
113 if (strcasecmp(string, key_string_table[i].string) == 0)
114 return (key_string_table[i].key);
115 }
116 return (KEYC_NONE);
117 }
118
119 /* Find modifiers. */
120 int
121 key_string_get_modifiers(const char **string)
122 {
123 int modifiers;
124
125 modifiers = 0;
126 while (((*string)[0] != '\0') && (*string)[1] == '-') {
127 switch ((*string)[0]) {
128 case 'C':
129 case 'c':
130 modifiers |= KEYC_CTRL;
131 break;
132 case 'M':
133 case 'm':
134 modifiers |= KEYC_ESCAPE;
135 break;
136 case 'S':
137 case 's':
138 modifiers |= KEYC_SHIFT;
139 break;
140 default:
141 *string = NULL;
142 return (0);
143 }
144 *string += 2;
145 }
146 return (modifiers);
147 }
148
149 /* Lookup a string and convert to a key value. */
150 int
151 key_string_lookup_string(const char *string)
152 {
153 static const char *other = "!#()+,-.0123456789:;<=>?'\r\t";
154 key_code key;
155 u_int u;
156 key_code modifiers;
157 struct utf8_data ud;
158 u_int i;
159 enum utf8_state more;
160 wchar_t wc;
161
162 /* Is this no key? */
163 if (strcasecmp(string, "None") == 0)
164 return (KEYC_NONE);
165
166 /* Is this a hexadecimal value? */
167 if (string[0] == '0' && string[1] == 'x') {
168 if (sscanf(string + 2, "%x", &u) != 1)
169 return (KEYC_UNKNOWN);
170 if (u > 0x1fffff)
171 return (KEYC_UNKNOWN);
172 return (u);
173 }
174
175 /* Check for modifiers. */
176 modifiers = 0;
177 if (string[0] == '^' && string[1] != '\0') {
178 modifiers |= KEYC_CTRL;
179 string++;
180 }
181 modifiers |= key_string_get_modifiers(&string);
182 if (string == NULL || string[0] == '\0')
183 return (KEYC_UNKNOWN);
184
185 /* Is this a standard ASCII key? */
186 if (string[1] == '\0') {
187 key = (u_char) string[0];
188 if (key < 32 || key == 127 || key > 255)
189 return (KEYC_NONE);
190 } else {
191 /* Otherwise look the key up in the table. */
192 key = key_string_search_table(string);
193 if (key == KEYC_NONE)
194 return (KEYC_NONE);
195 }
196
197 /* Convert the standard control keys. */
198 if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
199 if (key >= 97 && key <= 122)
200 key -= 96;
201 else if (key >= 64 && key <= 95)
202 key -= 64;
203 else if (key == 32)
204 key = 0;
205 else if (key == 63)
206 key = KEYC_BSPACE;
207 else
208 return (KEYC_NONE);
209 modifiers &= ~KEYC_CTRL;
210 }
211
212 return (key | modifiers);
213 }
214
215 /* Convert a key code into string format, with prefix if necessary. */
216 const char *
217 key_string_lookup_key(int key)
218 {
219 static char out[32];
220 char tmp[8];
221 u_int i;
222 struct utf8_data ud;
223 size_t off;
224
225 *out = '\0';
226
227 /* Handle no key. */
228 if (key == KEYC_NONE)
229 return ("None");
230
231 /* Handle special keys. */
232 if (key == KEYC_UNKNOWN)
233 return ("Unknown");
234 if (key == KEYC_FOCUS_IN)
235 return ("FocusIn");
236 if (key == KEYC_FOCUS_OUT)
237 return ("FocusOut");
238 if (key == KEYC_MOUSE)
239 return ("Mouse");
240 if (key == KEYC_DRAGGING)
241 return ("Dragging");
242 if (key == KEYC_MOUSEMOVE_PANE)
243 return ("MouseMovePane");
244 if (key == KEYC_MOUSEMOVE_STATUS)
245 return ("MouseMoveStatus");
246 if (key == KEYC_MOUSEMOVE_BORDER)
247 return ("MouseMoveBorder");
248
249 /*
250 * Special case: display C-@ as C-Space. Could do this below in
251 * the (key >= 0 && key <= 32), but this way we let it be found
252 * in key_string_table, for the unlikely chance that we might
253 * change its name.
254 */
255 if ((key & KEYC_MASK_KEY) == 0)
256 key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
257
258 /* Fill in the modifiers. */
259 if (key & KEYC_CTRL)
260 strlcat(out, "C-", sizeof out);
261 if (key & KEYC_ESCAPE)
262 strlcat(out, "M-", sizeof out);
263 if (key & KEYC_SHIFT)
264 strlcat(out, "S-", sizeof out);
265 key &= KEYC_MASK_KEY;
266
267 /* Try the key against the string table. */
268 for (i = 0; i < nitems(key_string_table); i++) {
269 if (key == key_string_table[i].key)
270 break;
271 }
272 if (i != nitems(key_string_table)) {
273 strlcat(out, key_string_table[i].string, sizeof out);
274 return (out);
275 }
276
277 /* Is this a UTF-8 key? */
278 if (key > 127 && key < KEYC_BASE) {
279 if (utf8_split(key, &ud) == UTF8_DONE) {
280 off = strlen(out);
281 memcpy(out + off, ud.data, ud.size);
282 out[off + ud.size] = '\0';
283 return (out);
284 }
285 }
286
287 /* Invalid keys are errors. */
288 if (key == 127 || key > 255)
289 return (NULL);
290
291 /* Check for standard or control key. */
292 if (key >= 0 && key <= 32) {
293 if (key == 0 || key > 26)
294 xsnprintf(tmp, sizeof tmp, "C-%c", 64 + key);
295 else
296 xsnprintf(tmp, sizeof tmp, "C-%c", 96 + key);
297 } else if (key >= 32 && key <= 126) {
298 tmp[0] = key;
299 tmp[1] = '\0';
300 } else if (key >= 128)
301 xsnprintf(tmp, sizeof tmp, "\\%o", key);
302
303 strlcat(out, tmp, sizeof out);
304 return (out);
305 }
306