Respond to OSC 4 query.
[tmux-openbsd.git] / key-string.c
blob4f7be858efdd016d1341a765dc07e3c86cea840f
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <wchar.h>
25 #include "tmux.h"
27 static key_code key_string_search_table(const char *);
28 static key_code key_string_get_modifiers(const char **);
30 static const struct {
31 const char *string;
32 key_code key;
33 } key_string_table[] = {
34 /* Function keys. */
35 { "F1", KEYC_F1|KEYC_IMPLIED_META },
36 { "F2", KEYC_F2|KEYC_IMPLIED_META },
37 { "F3", KEYC_F3|KEYC_IMPLIED_META },
38 { "F4", KEYC_F4|KEYC_IMPLIED_META },
39 { "F5", KEYC_F5|KEYC_IMPLIED_META },
40 { "F6", KEYC_F6|KEYC_IMPLIED_META },
41 { "F7", KEYC_F7|KEYC_IMPLIED_META },
42 { "F8", KEYC_F8|KEYC_IMPLIED_META },
43 { "F9", KEYC_F9|KEYC_IMPLIED_META },
44 { "F10", KEYC_F10|KEYC_IMPLIED_META },
45 { "F11", KEYC_F11|KEYC_IMPLIED_META },
46 { "F12", KEYC_F12|KEYC_IMPLIED_META },
47 { "IC", KEYC_IC|KEYC_IMPLIED_META },
48 { "Insert", KEYC_IC|KEYC_IMPLIED_META },
49 { "DC", KEYC_DC|KEYC_IMPLIED_META },
50 { "Delete", KEYC_DC|KEYC_IMPLIED_META },
51 { "Home", KEYC_HOME|KEYC_IMPLIED_META },
52 { "End", KEYC_END|KEYC_IMPLIED_META },
53 { "NPage", KEYC_NPAGE|KEYC_IMPLIED_META },
54 { "PageDown", KEYC_NPAGE|KEYC_IMPLIED_META },
55 { "PgDn", KEYC_NPAGE|KEYC_IMPLIED_META },
56 { "PPage", KEYC_PPAGE|KEYC_IMPLIED_META },
57 { "PageUp", KEYC_PPAGE|KEYC_IMPLIED_META },
58 { "PgUp", KEYC_PPAGE|KEYC_IMPLIED_META },
59 { "Tab", '\011' },
60 { "BTab", KEYC_BTAB },
61 { "Space", ' ' },
62 { "BSpace", KEYC_BSPACE },
63 { "Enter", '\r' },
64 { "Escape", '\033' },
66 /* Arrow keys. */
67 { "Up", KEYC_UP|KEYC_CURSOR|KEYC_IMPLIED_META },
68 { "Down", KEYC_DOWN|KEYC_CURSOR|KEYC_IMPLIED_META },
69 { "Left", KEYC_LEFT|KEYC_CURSOR|KEYC_IMPLIED_META },
70 { "Right", KEYC_RIGHT|KEYC_CURSOR|KEYC_IMPLIED_META },
72 /* Numeric keypad. */
73 { "KP/", KEYC_KP_SLASH|KEYC_KEYPAD },
74 { "KP*", KEYC_KP_STAR|KEYC_KEYPAD },
75 { "KP-", KEYC_KP_MINUS|KEYC_KEYPAD },
76 { "KP7", KEYC_KP_SEVEN|KEYC_KEYPAD },
77 { "KP8", KEYC_KP_EIGHT|KEYC_KEYPAD },
78 { "KP9", KEYC_KP_NINE|KEYC_KEYPAD },
79 { "KP+", KEYC_KP_PLUS|KEYC_KEYPAD },
80 { "KP4", KEYC_KP_FOUR|KEYC_KEYPAD },
81 { "KP5", KEYC_KP_FIVE|KEYC_KEYPAD },
82 { "KP6", KEYC_KP_SIX|KEYC_KEYPAD },
83 { "KP1", KEYC_KP_ONE|KEYC_KEYPAD },
84 { "KP2", KEYC_KP_TWO|KEYC_KEYPAD },
85 { "KP3", KEYC_KP_THREE|KEYC_KEYPAD },
86 { "KPEnter", KEYC_KP_ENTER|KEYC_KEYPAD },
87 { "KP0", KEYC_KP_ZERO|KEYC_KEYPAD },
88 { "KP.", KEYC_KP_PERIOD|KEYC_KEYPAD },
90 /* Mouse keys. */
91 KEYC_MOUSE_STRING(MOUSEDOWN1, MouseDown1),
92 KEYC_MOUSE_STRING(MOUSEDOWN2, MouseDown2),
93 KEYC_MOUSE_STRING(MOUSEDOWN3, MouseDown3),
94 KEYC_MOUSE_STRING(MOUSEUP1, MouseUp1),
95 KEYC_MOUSE_STRING(MOUSEUP2, MouseUp2),
96 KEYC_MOUSE_STRING(MOUSEUP3, MouseUp3),
97 KEYC_MOUSE_STRING(MOUSEDRAG1, MouseDrag1),
98 KEYC_MOUSE_STRING(MOUSEDRAG2, MouseDrag2),
99 KEYC_MOUSE_STRING(MOUSEDRAG3, MouseDrag3),
100 KEYC_MOUSE_STRING(MOUSEDRAGEND1, MouseDragEnd1),
101 KEYC_MOUSE_STRING(MOUSEDRAGEND2, MouseDragEnd2),
102 KEYC_MOUSE_STRING(MOUSEDRAGEND3, MouseDragEnd3),
103 KEYC_MOUSE_STRING(WHEELUP, WheelUp),
104 KEYC_MOUSE_STRING(WHEELDOWN, WheelDown),
105 KEYC_MOUSE_STRING(SECONDCLICK1, SecondClick1),
106 KEYC_MOUSE_STRING(SECONDCLICK2, SecondClick2),
107 KEYC_MOUSE_STRING(SECONDCLICK3, SecondClick3),
108 KEYC_MOUSE_STRING(DOUBLECLICK1, DoubleClick1),
109 KEYC_MOUSE_STRING(DOUBLECLICK2, DoubleClick2),
110 KEYC_MOUSE_STRING(DOUBLECLICK3, DoubleClick3),
111 KEYC_MOUSE_STRING(TRIPLECLICK1, TripleClick1),
112 KEYC_MOUSE_STRING(TRIPLECLICK2, TripleClick2),
113 KEYC_MOUSE_STRING(TRIPLECLICK3, TripleClick3),
116 /* Find key string in table. */
117 static key_code
118 key_string_search_table(const char *string)
120 u_int i, user;
122 for (i = 0; i < nitems(key_string_table); i++) {
123 if (strcasecmp(string, key_string_table[i].string) == 0)
124 return (key_string_table[i].key);
127 if (sscanf(string, "User%u", &user) == 1 && user < KEYC_NUSER)
128 return (KEYC_USER + user);
130 return (KEYC_UNKNOWN);
133 /* Find modifiers. */
134 static key_code
135 key_string_get_modifiers(const char **string)
137 key_code modifiers;
139 modifiers = 0;
140 while (((*string)[0] != '\0') && (*string)[1] == '-') {
141 switch ((*string)[0]) {
142 case 'C':
143 case 'c':
144 modifiers |= KEYC_CTRL;
145 break;
146 case 'M':
147 case 'm':
148 modifiers |= KEYC_META;
149 break;
150 case 'S':
151 case 's':
152 modifiers |= KEYC_SHIFT;
153 break;
154 default:
155 *string = NULL;
156 return (0);
158 *string += 2;
160 return (modifiers);
163 /* Lookup a string and convert to a key value. */
164 key_code
165 key_string_lookup_string(const char *string)
167 static const char *other = "!#()+,-.0123456789:;<=>'\r\t\177`/";
168 key_code key, modifiers;
169 u_int u, i;
170 struct utf8_data ud, *udp;
171 enum utf8_state more;
172 utf8_char uc;
173 char m[MB_LEN_MAX + 1];
174 int mlen;
176 /* Is this no key or any key? */
177 if (strcasecmp(string, "None") == 0)
178 return (KEYC_NONE);
179 if (strcasecmp(string, "Any") == 0)
180 return (KEYC_ANY);
182 /* Is this a hexadecimal value? */
183 if (string[0] == '0' && string[1] == 'x') {
184 if (sscanf(string + 2, "%x", &u) != 1)
185 return (KEYC_UNKNOWN);
186 if (u < 32)
187 return (u);
188 mlen = wctomb(m, u);
189 if (mlen <= 0 || mlen > MB_LEN_MAX)
190 return (KEYC_UNKNOWN);
191 m[mlen] = '\0';
193 udp = utf8_fromcstr(m);
194 if (udp == NULL ||
195 udp[0].size == 0 ||
196 udp[1].size != 0 ||
197 utf8_from_data(&udp[0], &uc) != UTF8_DONE) {
198 free(udp);
199 return (KEYC_UNKNOWN);
201 free(udp);
202 return (uc);
205 /* Check for modifiers. */
206 modifiers = 0;
207 if (string[0] == '^' && string[1] != '\0') {
208 modifiers |= KEYC_CTRL;
209 string++;
211 modifiers |= key_string_get_modifiers(&string);
212 if (string == NULL || string[0] == '\0')
213 return (KEYC_UNKNOWN);
215 /* Is this a standard ASCII key? */
216 if (string[1] == '\0' && (u_char)string[0] <= 127) {
217 key = (u_char)string[0];
218 if (key < 32)
219 return (KEYC_UNKNOWN);
220 } else {
221 /* Try as a UTF-8 key. */
222 if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) {
223 if (strlen(string) != ud.size)
224 return (KEYC_UNKNOWN);
225 for (i = 1; i < ud.size; i++)
226 more = utf8_append(&ud, (u_char)string[i]);
227 if (more != UTF8_DONE)
228 return (KEYC_UNKNOWN);
229 if (utf8_from_data(&ud, &uc) != UTF8_DONE)
230 return (KEYC_UNKNOWN);
231 return (uc|modifiers);
234 /* Otherwise look the key up in the table. */
235 key = key_string_search_table(string);
236 if (key == KEYC_UNKNOWN)
237 return (KEYC_UNKNOWN);
238 if (~modifiers & KEYC_META)
239 key &= ~KEYC_IMPLIED_META;
242 /* Convert the standard control keys. */
243 if (key <= 127 &&
244 (modifiers & KEYC_CTRL) &&
245 strchr(other, key) == NULL &&
246 key != 9 &&
247 key != 13 &&
248 key != 27) {
249 if (key >= 97 && key <= 122)
250 key -= 96;
251 else if (key >= 64 && key <= 95)
252 key -= 64;
253 else if (key == 32)
254 key = 0;
255 else if (key == 63)
256 key = 127;
257 else
258 return (KEYC_UNKNOWN);
259 modifiers &= ~KEYC_CTRL;
262 return (key|modifiers);
265 /* Convert a key code into string format, with prefix if necessary. */
266 const char *
267 key_string_lookup_key(key_code key, int with_flags)
269 key_code saved = key;
270 static char out[64];
271 char tmp[8];
272 const char *s;
273 u_int i;
274 struct utf8_data ud;
275 size_t off;
277 *out = '\0';
279 /* Literal keys are themselves. */
280 if (key & KEYC_LITERAL) {
281 snprintf(out, sizeof out, "%c", (int)(key & 0xff));
282 goto out;
285 /* Display C-@ as C-Space. */
286 if ((key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)) == 0)
287 key = ' '|KEYC_CTRL;
289 /* Fill in the modifiers. */
290 if (key & KEYC_CTRL)
291 strlcat(out, "C-", sizeof out);
292 if (key & KEYC_META)
293 strlcat(out, "M-", sizeof out);
294 if (key & KEYC_SHIFT)
295 strlcat(out, "S-", sizeof out);
296 key &= KEYC_MASK_KEY;
298 /* Handle no key. */
299 if (key == KEYC_NONE) {
300 s = "None";
301 goto append;
304 /* Handle special keys. */
305 if (key == KEYC_UNKNOWN) {
306 s = "Unknown";
307 goto append;
309 if (key == KEYC_ANY) {
310 s = "Any";
311 goto append;
313 if (key == KEYC_FOCUS_IN) {
314 s = "FocusIn";
315 goto append;
317 if (key == KEYC_FOCUS_OUT) {
318 s = "FocusOut";
319 goto append;
321 if (key == KEYC_PASTE_START) {
322 s = "PasteStart";
323 goto append;
325 if (key == KEYC_PASTE_END) {
326 s = "PasteEnd";
327 goto append;
329 if (key == KEYC_MOUSE) {
330 s = "Mouse";
331 goto append;
333 if (key == KEYC_DRAGGING) {
334 s = "Dragging";
335 goto append;
337 if (key == KEYC_MOUSEMOVE_PANE) {
338 s = "MouseMovePane";
339 goto append;
341 if (key == KEYC_MOUSEMOVE_STATUS) {
342 s = "MouseMoveStatus";
343 goto append;
345 if (key == KEYC_MOUSEMOVE_STATUS_LEFT) {
346 s = "MouseMoveStatusLeft";
347 goto append;
349 if (key == KEYC_MOUSEMOVE_STATUS_RIGHT) {
350 s = "MouseMoveStatusRight";
351 goto append;
353 if (key == KEYC_MOUSEMOVE_BORDER) {
354 s = "MouseMoveBorder";
355 goto append;
357 if (key >= KEYC_USER && key < KEYC_USER + KEYC_NUSER) {
358 snprintf(tmp, sizeof tmp, "User%u", (u_int)(key - KEYC_USER));
359 strlcat(out, tmp, sizeof out);
360 goto out;
363 /* Try the key against the string table. */
364 for (i = 0; i < nitems(key_string_table); i++) {
365 if (key == (key_string_table[i].key & KEYC_MASK_KEY))
366 break;
368 if (i != nitems(key_string_table)) {
369 strlcat(out, key_string_table[i].string, sizeof out);
370 goto out;
373 /* Is this a Unicode key? */
374 if (KEYC_IS_UNICODE(key)) {
375 utf8_to_data(key, &ud);
376 off = strlen(out);
377 memcpy(out + off, ud.data, ud.size);
378 out[off + ud.size] = '\0';
379 goto out;
382 /* Invalid keys are errors. */
383 if (key > 255) {
384 snprintf(out, sizeof out, "Invalid#%llx", saved);
385 goto out;
388 /* Check for standard or control key. */
389 if (key <= 32) {
390 if (key == 0 || key > 26)
391 xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key));
392 else
393 xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key));
394 } else if (key >= 32 && key <= 126) {
395 tmp[0] = key;
396 tmp[1] = '\0';
397 } else if (key == 127)
398 xsnprintf(tmp, sizeof tmp, "C-?");
399 else if (key >= 128)
400 xsnprintf(tmp, sizeof tmp, "\\%llo", key);
402 strlcat(out, tmp, sizeof out);
403 goto out;
405 append:
406 strlcat(out, s, sizeof out);
408 out:
409 if (with_flags && (saved & KEYC_MASK_FLAGS) != 0) {
410 strlcat(out, "[", sizeof out);
411 if (saved & KEYC_LITERAL)
412 strlcat(out, "L", sizeof out);
413 if (saved & KEYC_KEYPAD)
414 strlcat(out, "K", sizeof out);
415 if (saved & KEYC_CURSOR)
416 strlcat(out, "C", sizeof out);
417 if (saved & KEYC_IMPLIED_META)
418 strlcat(out, "I", sizeof out);
419 if (saved & KEYC_BUILD_MODIFIERS)
420 strlcat(out, "B", sizeof out);
421 strlcat(out, "]", sizeof out);
423 return (out);