Annotation of src/usr.bin/tmux/key-string.c, Revision 1.62
1.62 ! nicm 1: /* $OpenBSD: key-string.c,v 1.61 2020/05/25 18:57:25 nicm Exp $ */
1.1 nicm 2:
3: /*
1.34 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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:
1.62 ! nicm 21: #include <stdlib.h>
1.1 nicm 22: #include <string.h>
1.62 ! nicm 23: #include <wchar.h>
1.1 nicm 24:
25: #include "tmux.h"
26:
1.33 nicm 27: static key_code key_string_search_table(const char *);
28: static key_code key_string_get_modifiers(const char **);
1.1 nicm 29:
1.39 nicm 30: static const struct {
1.14 nicm 31: const char *string;
1.28 nicm 32: key_code key;
1.1 nicm 33: } key_string_table[] = {
34: /* Function keys. */
35: { "F1", KEYC_F1 },
36: { "F2", KEYC_F2 },
37: { "F3", KEYC_F3 },
38: { "F4", KEYC_F4 },
39: { "F5", KEYC_F5 },
40: { "F6", KEYC_F6 },
41: { "F7", KEYC_F7 },
42: { "F8", KEYC_F8 },
43: { "F9", KEYC_F9 },
44: { "F10", KEYC_F10 },
45: { "F11", KEYC_F11 },
46: { "F12", KEYC_F12 },
47: { "IC", KEYC_IC },
1.50 nicm 48: { "Insert", KEYC_IC },
1.1 nicm 49: { "DC", KEYC_DC },
1.50 nicm 50: { "Delete", KEYC_DC },
1.1 nicm 51: { "Home", KEYC_HOME },
52: { "End", KEYC_END },
53: { "NPage", KEYC_NPAGE },
1.21 nicm 54: { "PageDown", KEYC_NPAGE },
55: { "PgDn", KEYC_NPAGE },
1.1 nicm 56: { "PPage", KEYC_PPAGE },
1.21 nicm 57: { "PageUp", KEYC_PPAGE },
58: { "PgUp", KEYC_PPAGE },
1.1 nicm 59: { "Tab", '\011' },
60: { "BTab", KEYC_BTAB },
1.7 nicm 61: { "Space", ' ' },
1.5 nicm 62: { "BSpace", KEYC_BSPACE },
1.6 nicm 63: { "Enter", '\r' },
64: { "Escape", '\033' },
1.1 nicm 65:
66: /* Arrow keys. */
1.59 nicm 67: { "Up", KEYC_UP|KEYC_CURSOR },
68: { "Down", KEYC_DOWN|KEYC_CURSOR },
69: { "Left", KEYC_LEFT|KEYC_CURSOR },
70: { "Right", KEYC_RIGHT|KEYC_CURSOR },
1.1 nicm 71:
72: /* Numeric keypad. */
1.59 nicm 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 },
1.26 nicm 89:
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),
1.36 nicm 100: KEYC_MOUSE_STRING(MOUSEDRAGEND1, MouseDragEnd1),
101: KEYC_MOUSE_STRING(MOUSEDRAGEND2, MouseDragEnd2),
102: KEYC_MOUSE_STRING(MOUSEDRAGEND3, MouseDragEnd3),
1.26 nicm 103: KEYC_MOUSE_STRING(WHEELUP, WheelUp),
104: KEYC_MOUSE_STRING(WHEELDOWN, WheelDown),
1.55 nicm 105: KEYC_MOUSE_STRING(SECONDCLICK1, SecondClick1),
106: KEYC_MOUSE_STRING(SECONDCLICK2, SecondClick2),
107: KEYC_MOUSE_STRING(SECONDCLICK3, SecondClick3),
1.40 nicm 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),
1.1 nicm 114: };
115:
1.10 nicm 116: /* Find key string in table. */
1.33 nicm 117: static key_code
1.1 nicm 118: key_string_search_table(const char *string)
119: {
1.47 nicm 120: u_int i, user;
1.1 nicm 121:
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);
125: }
1.47 nicm 126:
127: if (sscanf(string, "User%u", &user) == 1 && user < KEYC_NUSER)
128: return (KEYC_USER + user);
129:
1.33 nicm 130: return (KEYC_UNKNOWN);
1.1 nicm 131: }
132:
1.14 nicm 133: /* Find modifiers. */
1.33 nicm 134: static key_code
1.14 nicm 135: key_string_get_modifiers(const char **string)
136: {
1.28 nicm 137: key_code modifiers;
1.14 nicm 138:
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':
1.57 nicm 148: modifiers |= KEYC_META;
1.14 nicm 149: break;
150: case 'S':
151: case 's':
152: modifiers |= KEYC_SHIFT;
153: break;
1.41 nicm 154: default:
155: *string = NULL;
1.42 nicm 156: return (0);
1.14 nicm 157: }
158: *string += 2;
159: }
160: return (modifiers);
161: }
162:
163: /* Lookup a string and convert to a key value. */
1.28 nicm 164: key_code
1.1 nicm 165: key_string_lookup_string(const char *string)
166: {
1.52 nicm 167: static const char *other = "!#()+,-.0123456789:;<=>'\r\t";
1.62 ! nicm 168: key_code key, modifiers;
! 169: u_int u, i;
! 170: struct utf8_data ud, *udp;
1.32 nicm 171: enum utf8_state more;
1.61 nicm 172: utf8_char uc;
1.62 ! nicm 173: char m[MB_LEN_MAX + 1];
! 174: int mlen;
1.23 nicm 175:
1.48 nicm 176: /* Is this no key or any key? */
1.33 nicm 177: if (strcasecmp(string, "None") == 0)
178: return (KEYC_NONE);
1.48 nicm 179: if (strcasecmp(string, "Any") == 0)
180: return (KEYC_ANY);
1.33 nicm 181:
1.23 nicm 182: /* Is this a hexadecimal value? */
183: if (string[0] == '0' && string[1] == 'x') {
1.38 nicm 184: if (sscanf(string + 2, "%x", &u) != 1)
185: return (KEYC_UNKNOWN);
1.62 ! nicm 186: mlen = wctomb(m, u);
! 187: if (mlen <= 0 || mlen > MB_LEN_MAX)
! 188: return (KEYC_UNKNOWN);
! 189: m[mlen] = '\0';
! 190:
! 191: udp = utf8_fromcstr(m);
! 192: if (udp == NULL ||
! 193: udp[0].size == 0 ||
! 194: udp[1].size != 0 ||
! 195: utf8_from_data(&udp[0], &uc) != UTF8_DONE) {
! 196: free(udp);
! 197: return (KEYC_UNKNOWN);
! 198: }
! 199: free(udp);
! 200: return (uc);
1.23 nicm 201: }
1.1 nicm 202:
1.14 nicm 203: /* Check for modifiers. */
204: modifiers = 0;
205: if (string[0] == '^' && string[1] != '\0') {
206: modifiers |= KEYC_CTRL;
207: string++;
208: }
209: modifiers |= key_string_get_modifiers(&string);
1.41 nicm 210: if (string == NULL || string[0] == '\0')
1.33 nicm 211: return (KEYC_UNKNOWN);
1.1 nicm 212:
1.14 nicm 213: /* Is this a standard ASCII key? */
1.28 nicm 214: if (string[1] == '\0' && (u_char)string[0] <= 127) {
215: key = (u_char)string[0];
1.52 nicm 216: if (key < 32)
1.33 nicm 217: return (KEYC_UNKNOWN);
1.17 nicm 218: } else {
1.28 nicm 219: /* Try as a UTF-8 key. */
1.32 nicm 220: if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) {
1.29 nicm 221: if (strlen(string) != ud.size)
1.33 nicm 222: return (KEYC_UNKNOWN);
1.29 nicm 223: for (i = 1; i < ud.size; i++)
1.30 nicm 224: more = utf8_append(&ud, (u_char)string[i]);
1.32 nicm 225: if (more != UTF8_DONE)
1.33 nicm 226: return (KEYC_UNKNOWN);
1.61 nicm 227: if (utf8_from_data(&ud, &uc) != UTF8_DONE)
1.35 nicm 228: return (KEYC_UNKNOWN);
1.61 nicm 229: return (uc|modifiers);
1.28 nicm 230: }
231:
1.17 nicm 232: /* Otherwise look the key up in the table. */
233: key = key_string_search_table(string);
1.33 nicm 234: if (key == KEYC_UNKNOWN)
235: return (KEYC_UNKNOWN);
1.17 nicm 236: }
1.12 nicm 237:
1.17 nicm 238: /* Convert the standard control keys. */
1.24 nicm 239: if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
1.17 nicm 240: if (key >= 97 && key <= 122)
241: key -= 96;
242: else if (key >= 64 && key <= 95)
243: key -= 64;
244: else if (key == 32)
245: key = 0;
1.56 nicm 246: else if (key == 63)
1.52 nicm 247: key = 127;
1.17 nicm 248: else
1.33 nicm 249: return (KEYC_UNKNOWN);
1.17 nicm 250: modifiers &= ~KEYC_CTRL;
1.13 nicm 251: }
252:
1.58 nicm 253: return (key|modifiers);
1.1 nicm 254: }
255:
1.10 nicm 256: /* Convert a key code into string format, with prefix if necessary. */
1.1 nicm 257: const char *
1.58 nicm 258: key_string_lookup_key(key_code key, int with_flags)
1.1 nicm 259: {
1.58 nicm 260: key_code saved = key;
261: static char out[64];
1.53 nicm 262: char tmp[8];
263: const char *s;
264: u_int i;
265: struct utf8_data ud;
266: size_t off;
1.14 nicm 267:
268: *out = '\0';
1.22 nicm 269:
1.53 nicm 270: /* Literal keys are themselves. */
271: if (key & KEYC_LITERAL) {
272: snprintf(out, sizeof out, "%c", (int)(key & 0xff));
1.58 nicm 273: goto out;
1.53 nicm 274: }
275:
1.54 nicm 276: /* Display C-@ as C-Space. */
1.58 nicm 277: if ((key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)) == 0)
278: key = ' '|KEYC_CTRL;
1.54 nicm 279:
1.53 nicm 280: /* Fill in the modifiers. */
281: if (key & KEYC_CTRL)
282: strlcat(out, "C-", sizeof out);
1.57 nicm 283: if (key & KEYC_META)
1.53 nicm 284: strlcat(out, "M-", sizeof out);
285: if (key & KEYC_SHIFT)
286: strlcat(out, "S-", sizeof out);
287: key &= KEYC_MASK_KEY;
288:
1.22 nicm 289: /* Handle no key. */
1.58 nicm 290: if (key == KEYC_NONE) {
291: s = "None";
292: goto append;
293: }
1.33 nicm 294:
295: /* Handle special keys. */
1.53 nicm 296: if (key == KEYC_UNKNOWN) {
297: s = "Unknown";
298: goto append;
299: }
300: if (key == KEYC_ANY) {
301: s = "Any";
302: goto append;
303: }
304: if (key == KEYC_FOCUS_IN) {
305: s = "FocusIn";
306: goto append;
307: }
308: if (key == KEYC_FOCUS_OUT) {
309: s = "FocusOut";
310: goto append;
311: }
312: if (key == KEYC_PASTE_START) {
313: s = "PasteStart";
314: goto append;
315: }
316: if (key == KEYC_PASTE_END) {
317: s = "PasteEnd";
318: goto append;
319: }
320: if (key == KEYC_MOUSE) {
321: s = "Mouse";
322: goto append;
323: }
324: if (key == KEYC_DRAGGING) {
325: s = "Dragging";
326: goto append;
327: }
328: if (key == KEYC_MOUSEMOVE_PANE) {
329: s = "MouseMovePane";
330: goto append;
331: }
332: if (key == KEYC_MOUSEMOVE_STATUS) {
333: s = "MouseMoveStatus";
334: goto append;
335: }
336: if (key == KEYC_MOUSEMOVE_STATUS_LEFT) {
337: s = "MouseMoveStatusLeft";
338: goto append;
339: }
340: if (key == KEYC_MOUSEMOVE_STATUS_RIGHT) {
341: s = "MouseMoveStatusRight";
342: goto append;
343: }
344: if (key == KEYC_MOUSEMOVE_BORDER) {
345: s = "MouseMoveBorder";
346: goto append;
347: }
1.47 nicm 348: if (key >= KEYC_USER && key < KEYC_USER + KEYC_NUSER) {
1.53 nicm 349: snprintf(tmp, sizeof tmp, "User%u", (u_int)(key - KEYC_USER));
350: strlcat(out, tmp, sizeof out);
1.58 nicm 351: goto out;
1.47 nicm 352: }
1.18 nicm 353:
1.14 nicm 354: /* Try the key against the string table. */
1.6 nicm 355: for (i = 0; i < nitems(key_string_table); i++) {
1.60 nicm 356: if (key == (key_string_table[i].key & KEYC_MASK_KEY))
1.14 nicm 357: break;
358: }
359: if (i != nitems(key_string_table)) {
360: strlcat(out, key_string_table[i].string, sizeof out);
1.58 nicm 361: goto out;
1.6 nicm 362: }
363:
1.28 nicm 364: /* Is this a UTF-8 key? */
365: if (key > 127 && key < KEYC_BASE) {
1.61 nicm 366: utf8_to_data(key, &ud);
367: off = strlen(out);
368: memcpy(out + off, ud.data, ud.size);
369: out[off + ud.size] = '\0';
370: goto out;
1.28 nicm 371: }
372:
1.14 nicm 373: /* Invalid keys are errors. */
1.52 nicm 374: if (key > 255) {
1.60 nicm 375: snprintf(out, sizeof out, "Invalid#%llx", saved);
1.58 nicm 376: goto out;
1.27 nicm 377: }
1.1 nicm 378:
1.14 nicm 379: /* Check for standard or control key. */
1.28 nicm 380: if (key <= 32) {
1.1 nicm 381: if (key == 0 || key > 26)
1.28 nicm 382: xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key));
1.1 nicm 383: else
1.28 nicm 384: xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key));
1.14 nicm 385: } else if (key >= 32 && key <= 126) {
386: tmp[0] = key;
387: tmp[1] = '\0';
1.52 nicm 388: } else if (key == 127)
389: xsnprintf(tmp, sizeof tmp, "C-?");
390: else if (key >= 128)
1.28 nicm 391: xsnprintf(tmp, sizeof tmp, "\\%llo", key);
1.20 nicm 392:
1.14 nicm 393: strlcat(out, tmp, sizeof out);
1.58 nicm 394: goto out;
1.53 nicm 395:
396: append:
397: strlcat(out, s, sizeof out);
1.58 nicm 398:
399: out:
400: if (with_flags && (saved & KEYC_MASK_FLAGS) != 0) {
401: strlcat(out, "[", sizeof out);
402: if (saved & KEYC_LITERAL)
403: strlcat(out, "L", sizeof out);
404: if (saved & KEYC_KEYPAD)
405: strlcat(out, "K", sizeof out);
406: if (saved & KEYC_CURSOR)
407: strlcat(out, "C", sizeof out);
408: if (saved & KEYC_IMPLIED_META)
409: strlcat(out, "I", sizeof out);
410: if (saved & KEYC_BUILD_MODIFIERS)
411: strlcat(out, "B", sizeof out);
412: strlcat(out, "]", sizeof out);
413: }
1.14 nicm 414: return (out);
1.1 nicm 415: }