xref: /openbsd-src/usr.bin/tmux/key-string.c (revision a7a9c343b801c7ebb2ee9c8b2218e3d75c8c0d2d)
1 /* $OpenBSD: key-string.c,v 1.55 2020/03/31 11:38:35 nicm Exp $ */
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 static 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 	{ "Insert",     KEYC_IC },
47 	{ "DC",		KEYC_DC },
48 	{ "Delete",     KEYC_DC },
49 	{ "Home",	KEYC_HOME },
50 	{ "End",	KEYC_END },
51 	{ "NPage",	KEYC_NPAGE },
52 	{ "PageDown",	KEYC_NPAGE },
53 	{ "PgDn",	KEYC_NPAGE },
54 	{ "PPage",	KEYC_PPAGE },
55 	{ "PageUp",	KEYC_PPAGE },
56 	{ "PgUp",	KEYC_PPAGE },
57 	{ "Tab",	'\011' },
58 	{ "BTab",	KEYC_BTAB },
59 	{ "Space",	' ' },
60 	{ "BSpace",	KEYC_BSPACE },
61 	{ "Enter",	'\r' },
62 	{ "Escape",	'\033' },
63 
64 	/* Arrow keys. */
65 	{ "Up",		KEYC_UP },
66 	{ "Down",	KEYC_DOWN },
67 	{ "Left",	KEYC_LEFT },
68 	{ "Right",	KEYC_RIGHT },
69 
70 	/* Numeric keypad. */
71 	{ "KP/", 	KEYC_KP_SLASH },
72 	{ "KP*",	KEYC_KP_STAR },
73 	{ "KP-",	KEYC_KP_MINUS },
74 	{ "KP7",	KEYC_KP_SEVEN },
75 	{ "KP8",	KEYC_KP_EIGHT },
76 	{ "KP9",	KEYC_KP_NINE },
77 	{ "KP+",	KEYC_KP_PLUS },
78 	{ "KP4",	KEYC_KP_FOUR },
79 	{ "KP5",	KEYC_KP_FIVE },
80 	{ "KP6",	KEYC_KP_SIX },
81 	{ "KP1",	KEYC_KP_ONE },
82 	{ "KP2",	KEYC_KP_TWO },
83 	{ "KP3",	KEYC_KP_THREE },
84 	{ "KPEnter",	KEYC_KP_ENTER },
85 	{ "KP0",	KEYC_KP_ZERO },
86 	{ "KP.",	KEYC_KP_PERIOD },
87 
88 	/* Mouse keys. */
89 	KEYC_MOUSE_STRING(MOUSEDOWN1, MouseDown1),
90 	KEYC_MOUSE_STRING(MOUSEDOWN2, MouseDown2),
91 	KEYC_MOUSE_STRING(MOUSEDOWN3, MouseDown3),
92 	KEYC_MOUSE_STRING(MOUSEUP1, MouseUp1),
93 	KEYC_MOUSE_STRING(MOUSEUP2, MouseUp2),
94 	KEYC_MOUSE_STRING(MOUSEUP3, MouseUp3),
95 	KEYC_MOUSE_STRING(MOUSEDRAG1, MouseDrag1),
96 	KEYC_MOUSE_STRING(MOUSEDRAG2, MouseDrag2),
97 	KEYC_MOUSE_STRING(MOUSEDRAG3, MouseDrag3),
98 	KEYC_MOUSE_STRING(MOUSEDRAGEND1, MouseDragEnd1),
99 	KEYC_MOUSE_STRING(MOUSEDRAGEND2, MouseDragEnd2),
100 	KEYC_MOUSE_STRING(MOUSEDRAGEND3, MouseDragEnd3),
101 	KEYC_MOUSE_STRING(WHEELUP, WheelUp),
102 	KEYC_MOUSE_STRING(WHEELDOWN, WheelDown),
103 	KEYC_MOUSE_STRING(SECONDCLICK1, SecondClick1),
104 	KEYC_MOUSE_STRING(SECONDCLICK2, SecondClick2),
105 	KEYC_MOUSE_STRING(SECONDCLICK3, SecondClick3),
106 	KEYC_MOUSE_STRING(DOUBLECLICK1, DoubleClick1),
107 	KEYC_MOUSE_STRING(DOUBLECLICK2, DoubleClick2),
108 	KEYC_MOUSE_STRING(DOUBLECLICK3, DoubleClick3),
109 	KEYC_MOUSE_STRING(TRIPLECLICK1, TripleClick1),
110 	KEYC_MOUSE_STRING(TRIPLECLICK2, TripleClick2),
111 	KEYC_MOUSE_STRING(TRIPLECLICK3, TripleClick3),
112 };
113 
114 /* Find key string in table. */
115 static key_code
116 key_string_search_table(const char *string)
117 {
118 	u_int	i, user;
119 
120 	for (i = 0; i < nitems(key_string_table); i++) {
121 		if (strcasecmp(string, key_string_table[i].string) == 0)
122 			return (key_string_table[i].key);
123 	}
124 
125 	if (sscanf(string, "User%u", &user) == 1 && user < KEYC_NUSER)
126 		return (KEYC_USER + user);
127 
128 	return (KEYC_UNKNOWN);
129 }
130 
131 /* Find modifiers. */
132 static key_code
133 key_string_get_modifiers(const char **string)
134 {
135 	key_code	modifiers;
136 
137 	modifiers = 0;
138 	while (((*string)[0] != '\0') && (*string)[1] == '-') {
139 		switch ((*string)[0]) {
140 		case 'C':
141 		case 'c':
142 			modifiers |= KEYC_CTRL;
143 			break;
144 		case 'M':
145 		case 'm':
146 			modifiers |= KEYC_ESCAPE;
147 			break;
148 		case 'S':
149 		case 's':
150 			modifiers |= KEYC_SHIFT;
151 			break;
152 		default:
153 			*string = NULL;
154 			return (0);
155 		}
156 		*string += 2;
157 	}
158 	return (modifiers);
159 }
160 
161 /* Lookup a string and convert to a key value. */
162 key_code
163 key_string_lookup_string(const char *string)
164 {
165 	static const char	*other = "!#()+,-.0123456789:;<=>'\r\t";
166 	key_code		 key;
167 	u_int			 u;
168 	key_code		 modifiers;
169 	struct utf8_data	 ud;
170 	u_int			 i;
171 	enum utf8_state		 more;
172 	wchar_t			 wc;
173 
174 	/* Is this no key or any key? */
175 	if (strcasecmp(string, "None") == 0)
176 		return (KEYC_NONE);
177 	if (strcasecmp(string, "Any") == 0)
178 		return (KEYC_ANY);
179 
180 	/* Is this a hexadecimal value? */
181 	if (string[0] == '0' && string[1] == 'x') {
182 	        if (sscanf(string + 2, "%x", &u) != 1)
183 	                return (KEYC_UNKNOWN);
184 		if (u > 0x1fffff)
185 	                return (KEYC_UNKNOWN);
186 	        return (u);
187 	}
188 
189 	/* Check for modifiers. */
190 	modifiers = 0;
191 	if (string[0] == '^' && string[1] != '\0') {
192 		modifiers |= KEYC_CTRL;
193 		string++;
194 	}
195 	modifiers |= key_string_get_modifiers(&string);
196 	if (string == NULL || string[0] == '\0')
197 		return (KEYC_UNKNOWN);
198 
199 	/* Is this a standard ASCII key? */
200 	if (string[1] == '\0' && (u_char)string[0] <= 127) {
201 		key = (u_char)string[0];
202 		if (key < 32)
203 			return (KEYC_UNKNOWN);
204 	} else {
205 		/* Try as a UTF-8 key. */
206 		if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) {
207 			if (strlen(string) != ud.size)
208 				return (KEYC_UNKNOWN);
209 			for (i = 1; i < ud.size; i++)
210 				more = utf8_append(&ud, (u_char)string[i]);
211 			if (more != UTF8_DONE)
212 				return (KEYC_UNKNOWN);
213 			if (utf8_combine(&ud, &wc) != UTF8_DONE)
214 				return (KEYC_UNKNOWN);
215 			return (wc | modifiers);
216 		}
217 
218 		/* Otherwise look the key up in the table. */
219 		key = key_string_search_table(string);
220 		if (key == KEYC_UNKNOWN)
221 			return (KEYC_UNKNOWN);
222 	}
223 
224 	/* Convert the standard control keys. */
225 	if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
226 		if (key >= 97 && key <= 122)
227 			key -= 96;
228 		else if (key >= 64 && key <= 95)
229 			key -= 64;
230 		else if (key == 32)
231 			key = 0;
232 		else if (key == '?')
233 			key = 127;
234 		else if (key == 63)
235 			key = KEYC_BSPACE;
236 		else
237 			return (KEYC_UNKNOWN);
238 		modifiers &= ~KEYC_CTRL;
239 	}
240 
241 	return (key | modifiers);
242 }
243 
244 /* Convert a key code into string format, with prefix if necessary. */
245 const char *
246 key_string_lookup_key(key_code key)
247 {
248 	static char		 out[32];
249 	char			 tmp[8];
250 	const char		*s;
251 	u_int			 i;
252 	struct utf8_data	 ud;
253 	size_t			 off;
254 
255 	*out = '\0';
256 
257 	/* Literal keys are themselves. */
258 	if (key & KEYC_LITERAL) {
259 		snprintf(out, sizeof out, "%c", (int)(key & 0xff));
260 		return (out);
261 	}
262 
263 	/* Display C-@ as C-Space. */
264 	if ((key & KEYC_MASK_KEY) == 0)
265 		key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
266 
267 	/* Fill in the modifiers. */
268 	if (key & KEYC_CTRL)
269 		strlcat(out, "C-", sizeof out);
270 	if (key & KEYC_ESCAPE)
271 		strlcat(out, "M-", sizeof out);
272 	if (key & KEYC_SHIFT)
273 		strlcat(out, "S-", sizeof out);
274 	key &= KEYC_MASK_KEY;
275 
276 	/* Handle no key. */
277 	if (key == KEYC_NONE)
278 		return ("None");
279 
280 	/* Handle special keys. */
281 	if (key == KEYC_UNKNOWN) {
282 		s = "Unknown";
283 		goto append;
284 	}
285 	if (key == KEYC_ANY) {
286 		s = "Any";
287 		goto append;
288 	}
289 	if (key == KEYC_FOCUS_IN) {
290 		s = "FocusIn";
291 		goto append;
292 	}
293 	if (key == KEYC_FOCUS_OUT) {
294 		s = "FocusOut";
295 		goto append;
296 	}
297 	if (key == KEYC_PASTE_START) {
298 		s = "PasteStart";
299 		goto append;
300 	}
301 	if (key == KEYC_PASTE_END) {
302 		s = "PasteEnd";
303 		goto append;
304 	}
305 	if (key == KEYC_MOUSE) {
306 		s = "Mouse";
307 		goto append;
308 	}
309 	if (key == KEYC_DRAGGING) {
310 		s = "Dragging";
311 		goto append;
312 	}
313 	if (key == KEYC_MOUSEMOVE_PANE) {
314 		s = "MouseMovePane";
315 		goto append;
316 	}
317 	if (key == KEYC_MOUSEMOVE_STATUS) {
318 		s = "MouseMoveStatus";
319 		goto append;
320 	}
321 	if (key == KEYC_MOUSEMOVE_STATUS_LEFT) {
322 		s = "MouseMoveStatusLeft";
323 		goto append;
324 	}
325 	if (key == KEYC_MOUSEMOVE_STATUS_RIGHT) {
326 		s = "MouseMoveStatusRight";
327 		goto append;
328 	}
329 	if (key == KEYC_MOUSEMOVE_BORDER) {
330 		s = "MouseMoveBorder";
331 		goto append;
332 	}
333 	if (key >= KEYC_USER && key < KEYC_USER + KEYC_NUSER) {
334 		snprintf(tmp, sizeof tmp, "User%u", (u_int)(key - KEYC_USER));
335 		strlcat(out, tmp, sizeof out);
336 		return (out);
337 	}
338 
339 	/* Try the key against the string table. */
340 	for (i = 0; i < nitems(key_string_table); i++) {
341 		if (key == key_string_table[i].key)
342 			break;
343 	}
344 	if (i != nitems(key_string_table)) {
345 		strlcat(out, key_string_table[i].string, sizeof out);
346 		return (out);
347 	}
348 
349 	/* Is this a UTF-8 key? */
350 	if (key > 127 && key < KEYC_BASE) {
351 		if (utf8_split(key, &ud) == UTF8_DONE) {
352 			off = strlen(out);
353 			memcpy(out + off, ud.data, ud.size);
354 			out[off + ud.size] = '\0';
355 			return (out);
356 		}
357 	}
358 
359 	/* Invalid keys are errors. */
360 	if (key > 255) {
361 		snprintf(out, sizeof out, "Invalid#%llx", key);
362 		return (out);
363 	}
364 
365 	/* Check for standard or control key. */
366 	if (key <= 32) {
367 		if (key == 0 || key > 26)
368 			xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key));
369 		else
370 			xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key));
371 	} else if (key >= 32 && key <= 126) {
372 		tmp[0] = key;
373 		tmp[1] = '\0';
374 	} else if (key == 127)
375 		xsnprintf(tmp, sizeof tmp, "C-?");
376 	else if (key >= 128)
377 		xsnprintf(tmp, sizeof tmp, "\\%llo", key);
378 
379 	strlcat(out, tmp, sizeof out);
380 	return (out);
381 
382 append:
383 	strlcat(out, s, sizeof out);
384 	return (out);
385 }
386