Annotation of src/usr.bin/tmux/key-string.c, Revision 1.26
1.26 ! nicm 1: /* $OpenBSD: key-string.c,v 1.25 2014/07/21 10:25:48 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:
21: #include <string.h>
22:
23: #include "tmux.h"
24:
25: int key_string_search_table(const char *);
1.14 nicm 26: int key_string_get_modifiers(const char **);
1.1 nicm 27:
1.19 nicm 28: const struct {
1.14 nicm 29: const char *string;
30: int key;
1.1 nicm 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 },
1.21 nicm 50: { "PageDown", KEYC_NPAGE },
51: { "PgDn", KEYC_NPAGE },
1.1 nicm 52: { "PPage", KEYC_PPAGE },
1.21 nicm 53: { "PageUp", KEYC_PPAGE },
54: { "PgUp", KEYC_PPAGE },
1.1 nicm 55: { "Tab", '\011' },
56: { "BTab", KEYC_BTAB },
1.7 nicm 57: { "Space", ' ' },
1.5 nicm 58: { "BSpace", KEYC_BSPACE },
1.6 nicm 59: { "Enter", '\r' },
60: { "Escape", '\033' },
1.1 nicm 61:
62: /* Arrow keys. */
63: { "Up", KEYC_UP },
64: { "Down", KEYC_DOWN },
65: { "Left", KEYC_LEFT },
66: { "Right", KEYC_RIGHT },
67:
68: /* Numeric keypad. */
1.9 nicm 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 },
1.26 ! nicm 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),
1.1 nicm 98: };
99:
1.10 nicm 100: /* Find key string in table. */
1.1 nicm 101: int
102: key_string_search_table(const char *string)
103: {
104: u_int i;
105:
106: for (i = 0; i < nitems(key_string_table); i++) {
107: if (strcasecmp(string, key_string_table[i].string) == 0)
108: return (key_string_table[i].key);
109: }
110: return (KEYC_NONE);
111: }
112:
1.14 nicm 113: /* Find modifiers. */
114: int
115: key_string_get_modifiers(const char **string)
116: {
117: int modifiers;
118:
119: modifiers = 0;
120: while (((*string)[0] != '\0') && (*string)[1] == '-') {
121: switch ((*string)[0]) {
122: case 'C':
123: case 'c':
124: modifiers |= KEYC_CTRL;
125: break;
126: case 'M':
127: case 'm':
128: modifiers |= KEYC_ESCAPE;
129: break;
130: case 'S':
131: case 's':
132: modifiers |= KEYC_SHIFT;
133: break;
134: }
135: *string += 2;
136: }
137: return (modifiers);
138: }
139:
140: /* Lookup a string and convert to a key value. */
1.1 nicm 141: int
142: key_string_lookup_string(const char *string)
143: {
1.24 nicm 144: static const char *other = "!#()+,-.0123456789:;<=>?'\r\t";
145: int key, modifiers;
146: u_short u;
147: int size;
1.23 nicm 148:
149: /* Is this a hexadecimal value? */
150: if (string[0] == '0' && string[1] == 'x') {
151: if (sscanf(string + 2, "%hx%n", &u, &size) != 1 || size > 4)
152: return (KEYC_NONE);
153: return (u);
154: }
1.1 nicm 155:
1.14 nicm 156: /* Check for modifiers. */
157: modifiers = 0;
158: if (string[0] == '^' && string[1] != '\0') {
159: modifiers |= KEYC_CTRL;
160: string++;
161: }
162: modifiers |= key_string_get_modifiers(&string);
1.1 nicm 163: if (string[0] == '\0')
164: return (KEYC_NONE);
165:
1.14 nicm 166: /* Is this a standard ASCII key? */
167: if (string[1] == '\0') {
168: key = (u_char) string[0];
1.20 nicm 169: if (key < 32 || key == 127 || key > 255)
1.1 nicm 170: return (KEYC_NONE);
1.17 nicm 171: } else {
172: /* Otherwise look the key up in the table. */
173: key = key_string_search_table(string);
174: if (key == KEYC_NONE)
175: return (KEYC_NONE);
176: }
1.12 nicm 177:
1.17 nicm 178: /* Convert the standard control keys. */
1.24 nicm 179: if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
1.17 nicm 180: if (key >= 97 && key <= 122)
181: key -= 96;
182: else if (key >= 64 && key <= 95)
183: key -= 64;
184: else if (key == 32)
185: key = 0;
186: else if (key == 63)
187: key = KEYC_BSPACE;
188: else
189: return (KEYC_NONE);
190: modifiers &= ~KEYC_CTRL;
1.13 nicm 191: }
192:
1.14 nicm 193: return (key | modifiers);
1.1 nicm 194: }
195:
1.10 nicm 196: /* Convert a key code into string format, with prefix if necessary. */
1.1 nicm 197: const char *
198: key_string_lookup_key(int key)
199: {
1.14 nicm 200: static char out[24];
201: char tmp[8];
1.24 nicm 202: u_int i;
1.14 nicm 203:
204: *out = '\0';
1.22 nicm 205:
206: /* Handle no key. */
207: if (key == KEYC_NONE)
1.26 ! nicm 208: return ("<NONE>");
! 209: if (key == KEYC_MOUSE)
! 210: return ("<MOUSE>");
1.14 nicm 211:
1.18 nicm 212: /*
213: * Special case: display C-@ as C-Space. Could do this below in
214: * the (key >= 0 && key <= 32), but this way we let it be found
215: * in key_string_table, for the unlikely chance that we might
216: * change its name.
217: */
218: if ((key & KEYC_MASK_KEY) == 0)
219: key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
220:
1.14 nicm 221: /* Fill in the modifiers. */
222: if (key & KEYC_CTRL)
223: strlcat(out, "C-", sizeof out);
224: if (key & KEYC_ESCAPE)
225: strlcat(out, "M-", sizeof out);
226: if (key & KEYC_SHIFT)
227: strlcat(out, "S-", sizeof out);
1.18 nicm 228: key &= KEYC_MASK_KEY;
1.1 nicm 229:
1.14 nicm 230: /* Try the key against the string table. */
1.6 nicm 231: for (i = 0; i < nitems(key_string_table); i++) {
232: if (key == key_string_table[i].key)
1.14 nicm 233: break;
234: }
235: if (i != nitems(key_string_table)) {
236: strlcat(out, key_string_table[i].string, sizeof out);
237: return (out);
1.6 nicm 238: }
239:
1.14 nicm 240: /* Invalid keys are errors. */
1.20 nicm 241: if (key == 127 || key > 255)
1.14 nicm 242: return (NULL);
1.1 nicm 243:
1.14 nicm 244: /* Check for standard or control key. */
1.1 nicm 245: if (key >= 0 && key <= 32) {
246: if (key == 0 || key > 26)
247: xsnprintf(tmp, sizeof tmp, "C-%c", 64 + key);
248: else
249: xsnprintf(tmp, sizeof tmp, "C-%c", 96 + key);
1.14 nicm 250: } else if (key >= 32 && key <= 126) {
251: tmp[0] = key;
252: tmp[1] = '\0';
1.20 nicm 253: } else if (key >= 128)
254: xsnprintf(tmp, sizeof tmp, "\\%o", key);
255:
1.14 nicm 256: strlcat(out, tmp, sizeof out);
257: return (out);
1.1 nicm 258: }