1 /* $OpenBSD$ */
2
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 static key_code key_string_search_table(const char *);
26 static key_code key_string_get_modifiers(const char **);
27
28 const struct {
29 const char *string;
30 key_code 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(MOUSEDRAGEND1, MouseDragEnd1),
97 KEYC_MOUSE_STRING(MOUSEDRAGEND2, MouseDragEnd2),
98 KEYC_MOUSE_STRING(MOUSEDRAGEND3, MouseDragEnd3),
99 KEYC_MOUSE_STRING(WHEELUP, WheelUp),
100 KEYC_MOUSE_STRING(WHEELDOWN, WheelDown),
101 };
102
103 /* Find key string in table. */
104 static key_code
key_string_search_table(const char * string)105 key_string_search_table(const char *string)
106 {
107 u_int i;
108
109 for (i = 0; i < nitems(key_string_table); i++) {
110 if (strcasecmp(string, key_string_table[i].string) == 0)
111 return (key_string_table[i].key);
112 }
113 return (KEYC_UNKNOWN);
114 }
115
116 /* Find modifiers. */
117 static key_code
key_string_get_modifiers(const char ** string)118 key_string_get_modifiers(const char **string)
119 {
120 key_code modifiers;
121
122 modifiers = 0;
123 while (((*string)[0] != '\0') && (*string)[1] == '-') {
124 switch ((*string)[0]) {
125 case 'C':
126 case 'c':
127 modifiers |= KEYC_CTRL;
128 break;
129 case 'M':
130 case 'm':
131 modifiers |= KEYC_ESCAPE;
132 break;
133 case 'S':
134 case 's':
135 modifiers |= KEYC_SHIFT;
136 break;
137 }
138 *string += 2;
139 }
140 return (modifiers);
141 }
142
143 /* Lookup a string and convert to a key value. */
144 key_code
key_string_lookup_string(const char * string)145 key_string_lookup_string(const char *string)
146 {
147 static const char *other = "!#()+,-.0123456789:;<=>?'\r\t";
148 key_code key;
149 u_short u;
150 int size;
151 key_code modifiers;
152 struct utf8_data ud;
153 u_int i;
154 enum utf8_state more;
155 wchar_t wc;
156
157 /* Is this no key? */
158 if (strcasecmp(string, "None") == 0)
159 return (KEYC_NONE);
160
161 /* Is this a hexadecimal value? */
162 if (string[0] == '0' && string[1] == 'x') {
163 if (sscanf(string + 2, "%hx%n", &u, &size) != 1 || size > 4)
164 return (KEYC_UNKNOWN);
165 return (u);
166 }
167
168 /* Check for modifiers. */
169 modifiers = 0;
170 if (string[0] == '^' && string[1] != '\0') {
171 modifiers |= KEYC_CTRL;
172 string++;
173 }
174 modifiers |= key_string_get_modifiers(&string);
175 if (string[0] == '\0')
176 return (KEYC_UNKNOWN);
177
178 /* Is this a standard ASCII key? */
179 if (string[1] == '\0' && (u_char)string[0] <= 127) {
180 key = (u_char)string[0];
181 if (key < 32 || key == 127)
182 return (KEYC_UNKNOWN);
183 } else {
184 /* Try as a UTF-8 key. */
185 if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) {
186 if (strlen(string) != ud.size)
187 return (KEYC_UNKNOWN);
188 for (i = 1; i < ud.size; i++)
189 more = utf8_append(&ud, (u_char)string[i]);
190 if (more != UTF8_DONE)
191 return (KEYC_UNKNOWN);
192 if (utf8_combine(&ud, &wc) != UTF8_DONE)
193 return (KEYC_UNKNOWN);
194 return (wc | modifiers);
195 }
196
197 /* Otherwise look the key up in the table. */
198 key = key_string_search_table(string);
199 if (key == KEYC_UNKNOWN)
200 return (KEYC_UNKNOWN);
201 }
202
203 /* Convert the standard control keys. */
204 if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
205 if (key >= 97 && key <= 122)
206 key -= 96;
207 else if (key >= 64 && key <= 95)
208 key -= 64;
209 else if (key == 32)
210 key = 0;
211 else if (key == 63)
212 key = KEYC_BSPACE;
213 else
214 return (KEYC_UNKNOWN);
215 modifiers &= ~KEYC_CTRL;
216 }
217
218 return (key | modifiers);
219 }
220
221 /* Convert a key code into string format, with prefix if necessary. */
222 const char *
key_string_lookup_key(key_code key)223 key_string_lookup_key(key_code key)
224 {
225 static char out[24];
226 char tmp[8];
227 u_int i;
228 struct utf8_data ud;
229
230 *out = '\0';
231
232 /* Handle no key. */
233 if (key == KEYC_NONE)
234 return ("None");
235
236 /* Handle special keys. */
237 if (key == KEYC_UNKNOWN)
238 return ("Unknown");
239 if (key == KEYC_MOUSE)
240 return ("Mouse");
241
242 /*
243 * Special case: display C-@ as C-Space. Could do this below in
244 * the (key >= 0 && key <= 32), but this way we let it be found
245 * in key_string_table, for the unlikely chance that we might
246 * change its name.
247 */
248 if ((key & KEYC_MASK_KEY) == 0)
249 key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
250
251 /* Fill in the modifiers. */
252 if (key & KEYC_CTRL)
253 strlcat(out, "C-", sizeof out);
254 if (key & KEYC_ESCAPE)
255 strlcat(out, "M-", sizeof out);
256 if (key & KEYC_SHIFT)
257 strlcat(out, "S-", sizeof out);
258 key &= KEYC_MASK_KEY;
259
260 /* Try the key against the string table. */
261 for (i = 0; i < nitems(key_string_table); i++) {
262 if (key == key_string_table[i].key)
263 break;
264 }
265 if (i != nitems(key_string_table)) {
266 strlcat(out, key_string_table[i].string, sizeof out);
267 return (out);
268 }
269
270 /* Is this a UTF-8 key? */
271 if (key > 127 && key < KEYC_BASE) {
272 if (utf8_split(key, &ud) == UTF8_DONE) {
273 memcpy(out, ud.data, ud.size);
274 out[ud.size] = '\0';
275 return (out);
276 }
277 }
278
279 /* Invalid keys are errors. */
280 if (key == 127 || key > 255) {
281 snprintf(out, sizeof out, "Invalid#%llx", key);
282 return (out);
283 }
284
285 /* Check for standard or control key. */
286 if (key <= 32) {
287 if (key == 0 || key > 26)
288 xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key));
289 else
290 xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key));
291 } else if (key >= 32 && key <= 126) {
292 tmp[0] = key;
293 tmp[1] = '\0';
294 } else if (key >= 128)
295 xsnprintf(tmp, sizeof tmp, "\\%llo", key);
296
297 strlcat(out, tmp, sizeof out);
298 return (out);
299 }
300