key-string.c revision 1.53
1/* $OpenBSD: key-string.c,v 1.53 2020/02/19 14:25:00 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 25static key_code key_string_search_table(const char *); 26static key_code key_string_get_modifiers(const char **); 27 28static 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(DOUBLECLICK1, DoubleClick1), 104 KEYC_MOUSE_STRING(DOUBLECLICK2, DoubleClick2), 105 KEYC_MOUSE_STRING(DOUBLECLICK3, DoubleClick3), 106 KEYC_MOUSE_STRING(TRIPLECLICK1, TripleClick1), 107 KEYC_MOUSE_STRING(TRIPLECLICK2, TripleClick2), 108 KEYC_MOUSE_STRING(TRIPLECLICK3, TripleClick3), 109}; 110 111/* Find key string in table. */ 112static key_code 113key_string_search_table(const char *string) 114{ 115 u_int i, user; 116 117 for (i = 0; i < nitems(key_string_table); i++) { 118 if (strcasecmp(string, key_string_table[i].string) == 0) 119 return (key_string_table[i].key); 120 } 121 122 if (sscanf(string, "User%u", &user) == 1 && user < KEYC_NUSER) 123 return (KEYC_USER + user); 124 125 return (KEYC_UNKNOWN); 126} 127 128/* Find modifiers. */ 129static key_code 130key_string_get_modifiers(const char **string) 131{ 132 key_code modifiers; 133 134 modifiers = 0; 135 while (((*string)[0] != '\0') && (*string)[1] == '-') { 136 switch ((*string)[0]) { 137 case 'C': 138 case 'c': 139 modifiers |= KEYC_CTRL; 140 break; 141 case 'M': 142 case 'm': 143 modifiers |= KEYC_ESCAPE; 144 break; 145 case 'S': 146 case 's': 147 modifiers |= KEYC_SHIFT; 148 break; 149 default: 150 *string = NULL; 151 return (0); 152 } 153 *string += 2; 154 } 155 return (modifiers); 156} 157 158/* Lookup a string and convert to a key value. */ 159key_code 160key_string_lookup_string(const char *string) 161{ 162 static const char *other = "!#()+,-.0123456789:;<=>'\r\t"; 163 key_code key; 164 u_int u; 165 key_code modifiers; 166 struct utf8_data ud; 167 u_int i; 168 enum utf8_state more; 169 wchar_t wc; 170 171 /* Is this no key or any key? */ 172 if (strcasecmp(string, "None") == 0) 173 return (KEYC_NONE); 174 if (strcasecmp(string, "Any") == 0) 175 return (KEYC_ANY); 176 177 /* Is this a hexadecimal value? */ 178 if (string[0] == '0' && string[1] == 'x') { 179 if (sscanf(string + 2, "%x", &u) != 1) 180 return (KEYC_UNKNOWN); 181 if (u > 0x1fffff) 182 return (KEYC_UNKNOWN); 183 return (u); 184 } 185 186 /* Check for modifiers. */ 187 modifiers = 0; 188 if (string[0] == '^' && string[1] != '\0') { 189 modifiers |= KEYC_CTRL; 190 string++; 191 } 192 modifiers |= key_string_get_modifiers(&string); 193 if (string == NULL || string[0] == '\0') 194 return (KEYC_UNKNOWN); 195 196 /* Is this a standard ASCII key? */ 197 if (string[1] == '\0' && (u_char)string[0] <= 127) { 198 key = (u_char)string[0]; 199 if (key < 32) 200 return (KEYC_UNKNOWN); 201 } else { 202 /* Try as a UTF-8 key. */ 203 if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) { 204 if (strlen(string) != ud.size) 205 return (KEYC_UNKNOWN); 206 for (i = 1; i < ud.size; i++) 207 more = utf8_append(&ud, (u_char)string[i]); 208 if (more != UTF8_DONE) 209 return (KEYC_UNKNOWN); 210 if (utf8_combine(&ud, &wc) != UTF8_DONE) 211 return (KEYC_UNKNOWN); 212 return (wc | modifiers); 213 } 214 215 /* Otherwise look the key up in the table. */ 216 key = key_string_search_table(string); 217 if (key == KEYC_UNKNOWN) 218 return (KEYC_UNKNOWN); 219 } 220 221 /* Convert the standard control keys. */ 222 if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) { 223 if (key >= 97 && key <= 122) 224 key -= 96; 225 else if (key >= 64 && key <= 95) 226 key -= 64; 227 else if (key == 32) 228 key = 0; 229 else if (key == '?') 230 key = 127; 231 else if (key == 63) 232 key = KEYC_BSPACE; 233 else 234 return (KEYC_UNKNOWN); 235 modifiers &= ~KEYC_CTRL; 236 } 237 238 return (key | modifiers); 239} 240 241/* Convert a key code into string format, with prefix if necessary. */ 242const char * 243key_string_lookup_key(key_code key) 244{ 245 static char out[32]; 246 char tmp[8]; 247 const char *s; 248 u_int i; 249 struct utf8_data ud; 250 size_t off; 251 252 *out = '\0'; 253 254 /* Literal keys are themselves. */ 255 if (key & KEYC_LITERAL) { 256 snprintf(out, sizeof out, "%c", (int)(key & 0xff)); 257 return (out); 258 } 259 260 /* Fill in the modifiers. */ 261 if (key & KEYC_CTRL) 262 strlcat(out, "C-", sizeof out); 263 if (key & KEYC_ESCAPE) 264 strlcat(out, "M-", sizeof out); 265 if (key & KEYC_SHIFT) 266 strlcat(out, "S-", sizeof out); 267 key &= KEYC_MASK_KEY; 268 269 /* Handle no key. */ 270 if (key == KEYC_NONE) 271 return ("None"); 272 273 /* Handle special keys. */ 274 if (key == KEYC_UNKNOWN) { 275 s = "Unknown"; 276 goto append; 277 } 278 if (key == KEYC_ANY) { 279 s = "Any"; 280 goto append; 281 } 282 if (key == KEYC_FOCUS_IN) { 283 s = "FocusIn"; 284 goto append; 285 } 286 if (key == KEYC_FOCUS_OUT) { 287 s = "FocusOut"; 288 goto append; 289 } 290 if (key == KEYC_PASTE_START) { 291 s = "PasteStart"; 292 goto append; 293 } 294 if (key == KEYC_PASTE_END) { 295 s = "PasteEnd"; 296 goto append; 297 } 298 if (key == KEYC_MOUSE) { 299 s = "Mouse"; 300 goto append; 301 } 302 if (key == KEYC_DRAGGING) { 303 s = "Dragging"; 304 goto append; 305 } 306 if (key == KEYC_MOUSEMOVE_PANE) { 307 s = "MouseMovePane"; 308 goto append; 309 } 310 if (key == KEYC_MOUSEMOVE_STATUS) { 311 s = "MouseMoveStatus"; 312 goto append; 313 } 314 if (key == KEYC_MOUSEMOVE_STATUS_LEFT) { 315 s = "MouseMoveStatusLeft"; 316 goto append; 317 } 318 if (key == KEYC_MOUSEMOVE_STATUS_RIGHT) { 319 s = "MouseMoveStatusRight"; 320 goto append; 321 } 322 if (key == KEYC_MOUSEMOVE_BORDER) { 323 s = "MouseMoveBorder"; 324 goto append; 325 } 326 if (key >= KEYC_USER && key < KEYC_USER + KEYC_NUSER) { 327 snprintf(tmp, sizeof tmp, "User%u", (u_int)(key - KEYC_USER)); 328 strlcat(out, tmp, sizeof out); 329 return (out); 330 } 331 332 /* 333 * Special case: display C-@ as C-Space. Could do this below in 334 * the (key >= 0 && key <= 32), but this way we let it be found 335 * in key_string_table, for the unlikely chance that we might 336 * change its name. 337 */ 338 if ((key & KEYC_MASK_KEY) == 0) 339 key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD); 340 341 /* Try the key against the string table. */ 342 for (i = 0; i < nitems(key_string_table); i++) { 343 if (key == key_string_table[i].key) 344 break; 345 } 346 if (i != nitems(key_string_table)) { 347 strlcat(out, key_string_table[i].string, sizeof out); 348 return (out); 349 } 350 351 /* Is this a UTF-8 key? */ 352 if (key > 127 && key < KEYC_BASE) { 353 if (utf8_split(key, &ud) == UTF8_DONE) { 354 off = strlen(out); 355 memcpy(out + off, ud.data, ud.size); 356 out[off + ud.size] = '\0'; 357 return (out); 358 } 359 } 360 361 /* Invalid keys are errors. */ 362 if (key > 255) { 363 snprintf(out, sizeof out, "Invalid#%llx", key); 364 return (out); 365 } 366 367 /* Check for standard or control key. */ 368 if (key <= 32) { 369 if (key == 0 || key > 26) 370 xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key)); 371 else 372 xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key)); 373 } else if (key >= 32 && key <= 126) { 374 tmp[0] = key; 375 tmp[1] = '\0'; 376 } else if (key == 127) 377 xsnprintf(tmp, sizeof tmp, "C-?"); 378 else if (key >= 128) 379 xsnprintf(tmp, sizeof tmp, "\\%llo", key); 380 381 strlcat(out, tmp, sizeof out); 382 return (out); 383 384append: 385 strlcat(out, s, sizeof out); 386 return (out); 387} 388