SDL_keymap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "SDL_keymap_c.h"
  20. #include "../SDL_hashtable.h"
  21. struct SDL_Keymap
  22. {
  23. SDL_HashTable *scancode_to_keycode;
  24. SDL_HashTable *keycode_to_scancode;
  25. };
  26. SDL_Keymap *SDL_CreateKeymap(void)
  27. {
  28. SDL_Keymap *keymap = (SDL_Keymap *)SDL_malloc(sizeof(*keymap));
  29. if (!keymap) {
  30. return NULL;
  31. }
  32. keymap->scancode_to_keycode = SDL_CreateHashTable(NULL, 64, SDL_HashID, SDL_KeyMatchID, NULL, SDL_FALSE);
  33. keymap->keycode_to_scancode = SDL_CreateHashTable(NULL, 64, SDL_HashID, SDL_KeyMatchID, NULL, SDL_FALSE);
  34. if (!keymap->scancode_to_keycode || !keymap->keycode_to_scancode) {
  35. SDL_DestroyKeymap(keymap);
  36. return NULL;
  37. }
  38. return keymap;
  39. }
  40. static SDL_Keymod NormalizeModifierStateForKeymap(SDL_Keymod modstate)
  41. {
  42. // The modifiers that affect the keymap are: SHIFT, CAPS, ALT, and MODE
  43. modstate &= (SDL_KMOD_SHIFT | SDL_KMOD_CAPS | SDL_KMOD_ALT | SDL_KMOD_MODE);
  44. // If either right or left Shift are set, set both in the output
  45. if (modstate & SDL_KMOD_SHIFT) {
  46. modstate |= SDL_KMOD_SHIFT;
  47. }
  48. // If either right or left Alt are set, set both in the output
  49. if (modstate & SDL_KMOD_ALT) {
  50. modstate |= SDL_KMOD_ALT;
  51. }
  52. return modstate;
  53. }
  54. void SDL_SetKeymapEntry(SDL_Keymap *keymap, SDL_Scancode scancode, SDL_Keymod modstate, SDL_Keycode keycode)
  55. {
  56. if (!keymap) {
  57. return;
  58. }
  59. if (keycode == SDL_GetKeymapKeycode(keymap, scancode, modstate)) {
  60. return;
  61. }
  62. Uint32 key = ((Uint32)NormalizeModifierStateForKeymap(modstate) << 16) | scancode;
  63. const void *value;
  64. if (SDL_FindInHashTable(keymap->scancode_to_keycode, (void *)(uintptr_t)key, &value)) {
  65. // Changing the mapping, need to remove the existing entry from the keymap
  66. SDL_RemoveFromHashTable(keymap->scancode_to_keycode, (void *)(uintptr_t)key);
  67. SDL_RemoveFromHashTable(keymap->keycode_to_scancode, value);
  68. }
  69. SDL_InsertIntoHashTable(keymap->scancode_to_keycode, (void *)(uintptr_t)key, (void *)(uintptr_t)keycode);
  70. SDL_InsertIntoHashTable(keymap->keycode_to_scancode, (void *)(uintptr_t)keycode, (void *)(uintptr_t)key);
  71. }
  72. SDL_Keycode SDL_GetKeymapKeycode(SDL_Keymap *keymap, SDL_Scancode scancode, SDL_Keymod modstate)
  73. {
  74. SDL_Keycode keycode;
  75. Uint32 key = ((Uint32)NormalizeModifierStateForKeymap(modstate) << 16) | scancode;
  76. const void *value;
  77. if (keymap && SDL_FindInHashTable(keymap->scancode_to_keycode, (void *)(uintptr_t)key, &value)) {
  78. keycode = (SDL_Keycode)(uintptr_t)value;
  79. } else {
  80. keycode = SDL_GetDefaultKeyFromScancode(scancode, modstate);
  81. }
  82. return keycode;
  83. }
  84. SDL_Scancode SDL_GetKeymapScancode(SDL_Keymap *keymap, SDL_Keycode keycode, SDL_Keymod *modstate)
  85. {
  86. SDL_Scancode scancode;
  87. const void *value;
  88. if (keymap && SDL_FindInHashTable(keymap->keycode_to_scancode, (void *)(uintptr_t)keycode, &value)) {
  89. scancode = (SDL_Scancode)((uintptr_t)value & 0xFFFF);
  90. if (modstate) {
  91. *modstate = (SDL_Keymod)((uintptr_t)value >> 16);
  92. }
  93. } else {
  94. scancode = SDL_GetDefaultScancodeFromKey(keycode, modstate);
  95. }
  96. return scancode;
  97. }
  98. void SDL_ResetKeymap(SDL_Keymap *keymap)
  99. {
  100. if (keymap) {
  101. SDL_EmptyHashTable(keymap->scancode_to_keycode);
  102. SDL_EmptyHashTable(keymap->keycode_to_scancode);
  103. }
  104. }
  105. void SDL_DestroyKeymap(SDL_Keymap *keymap)
  106. {
  107. if (keymap) {
  108. SDL_DestroyHashTable(keymap->scancode_to_keycode);
  109. SDL_DestroyHashTable(keymap->keycode_to_scancode);
  110. SDL_free(keymap);
  111. }
  112. }
  113. static const SDL_Keycode normal_default_symbols[] = {
  114. SDLK_1,
  115. SDLK_2,
  116. SDLK_3,
  117. SDLK_4,
  118. SDLK_5,
  119. SDLK_6,
  120. SDLK_7,
  121. SDLK_8,
  122. SDLK_9,
  123. SDLK_0,
  124. SDLK_RETURN,
  125. SDLK_ESCAPE,
  126. SDLK_BACKSPACE,
  127. SDLK_TAB,
  128. SDLK_SPACE,
  129. SDLK_MINUS,
  130. SDLK_EQUALS,
  131. SDLK_LEFTBRACKET,
  132. SDLK_RIGHTBRACKET,
  133. SDLK_BACKSLASH,
  134. SDLK_HASH,
  135. SDLK_SEMICOLON,
  136. SDLK_APOSTROPHE,
  137. SDLK_GRAVE,
  138. SDLK_COMMA,
  139. SDLK_PERIOD,
  140. SDLK_SLASH,
  141. };
  142. static const SDL_Keycode shifted_default_symbols[] = {
  143. SDLK_EXCLAIM,
  144. SDLK_AT,
  145. SDLK_HASH,
  146. SDLK_DOLLAR,
  147. SDLK_PERCENT,
  148. SDLK_CARET,
  149. SDLK_AMPERSAND,
  150. SDLK_ASTERISK,
  151. SDLK_LEFTPAREN,
  152. SDLK_RIGHTPAREN,
  153. SDLK_RETURN,
  154. SDLK_ESCAPE,
  155. SDLK_BACKSPACE,
  156. SDLK_TAB,
  157. SDLK_SPACE,
  158. SDLK_UNDERSCORE,
  159. SDLK_PLUS,
  160. SDLK_LEFTBRACE,
  161. SDLK_RIGHTBRACE,
  162. SDLK_PIPE,
  163. SDLK_HASH,
  164. SDLK_COLON,
  165. SDLK_DBLAPOSTROPHE,
  166. SDLK_TILDE,
  167. SDLK_LESS,
  168. SDLK_GREATER,
  169. SDLK_QUESTION
  170. };
  171. SDL_Keycode SDL_GetDefaultKeyFromScancode(SDL_Scancode scancode, SDL_Keymod modstate)
  172. {
  173. if (((int)scancode) < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
  174. SDL_InvalidParamError("scancode");
  175. return SDLK_UNKNOWN;
  176. }
  177. if (modstate & SDL_KMOD_MODE) {
  178. return SDLK_UNKNOWN;
  179. }
  180. if (scancode < SDL_SCANCODE_A) {
  181. return SDLK_UNKNOWN;
  182. }
  183. if (scancode < SDL_SCANCODE_1) {
  184. SDL_bool shifted = (modstate & SDL_KMOD_SHIFT) ? SDL_TRUE : SDL_FALSE;
  185. #ifdef SDL_PLATFORM_APPLE
  186. // Apple maps to upper case for either shift or capslock inclusive
  187. if (modstate & SDL_KMOD_CAPS) {
  188. shifted = SDL_TRUE;
  189. }
  190. #else
  191. if (modstate & SDL_KMOD_CAPS) {
  192. shifted = !shifted;
  193. }
  194. #endif
  195. if (!shifted) {
  196. return (SDL_Keycode)('a' + scancode - SDL_SCANCODE_A);
  197. } else {
  198. return (SDL_Keycode)('A' + scancode - SDL_SCANCODE_A);
  199. }
  200. }
  201. if (scancode < SDL_SCANCODE_CAPSLOCK) {
  202. SDL_bool shifted = (modstate & SDL_KMOD_SHIFT) ? SDL_TRUE : SDL_FALSE;
  203. if (!shifted) {
  204. return normal_default_symbols[scancode - SDL_SCANCODE_1];
  205. } else {
  206. return shifted_default_symbols[scancode - SDL_SCANCODE_1];
  207. }
  208. }
  209. if (scancode == SDL_SCANCODE_DELETE) {
  210. return SDLK_DELETE;
  211. }
  212. return SDL_SCANCODE_TO_KEYCODE(scancode);
  213. }
  214. SDL_Scancode SDL_GetDefaultScancodeFromKey(SDL_Keycode key, SDL_Keymod *modstate)
  215. {
  216. if (modstate) {
  217. *modstate = SDL_KMOD_NONE;
  218. }
  219. if (key == SDLK_UNKNOWN) {
  220. return SDL_SCANCODE_UNKNOWN;
  221. }
  222. if (key & SDLK_SCANCODE_MASK) {
  223. return (SDL_Scancode)(key & ~SDLK_SCANCODE_MASK);
  224. }
  225. if (key >= SDLK_A && key <= SDLK_Z) {
  226. return (SDL_Scancode)(SDL_SCANCODE_A + key - SDLK_A);
  227. }
  228. if (key >= 'A' && key <= 'Z') {
  229. if (modstate) {
  230. *modstate = SDL_KMOD_SHIFT;
  231. }
  232. return (SDL_Scancode)(SDL_SCANCODE_A + key - 'Z');
  233. }
  234. for (int i = 0; i < SDL_arraysize(normal_default_symbols); ++i) {
  235. if (key == normal_default_symbols[i]) {
  236. return(SDL_Scancode)(SDL_SCANCODE_1 + i);
  237. }
  238. }
  239. for (int i = 0; i < SDL_arraysize(shifted_default_symbols); ++i) {
  240. if (key == shifted_default_symbols[i]) {
  241. if (modstate) {
  242. *modstate = SDL_KMOD_SHIFT;
  243. }
  244. return(SDL_Scancode)(SDL_SCANCODE_1 + i);
  245. }
  246. }
  247. if (key == SDLK_DELETE) {
  248. return SDL_SCANCODE_DELETE;
  249. }
  250. return SDL_SCANCODE_UNKNOWN;
  251. }
  252. static const char *SDL_scancode_names[SDL_NUM_SCANCODES] =
  253. {
  254. /* 0 */ NULL,
  255. /* 1 */ NULL,
  256. /* 2 */ NULL,
  257. /* 3 */ NULL,
  258. /* 4 */ "A",
  259. /* 5 */ "B",
  260. /* 6 */ "C",
  261. /* 7 */ "D",
  262. /* 8 */ "E",
  263. /* 9 */ "F",
  264. /* 10 */ "G",
  265. /* 11 */ "H",
  266. /* 12 */ "I",
  267. /* 13 */ "J",
  268. /* 14 */ "K",
  269. /* 15 */ "L",
  270. /* 16 */ "M",
  271. /* 17 */ "N",
  272. /* 18 */ "O",
  273. /* 19 */ "P",
  274. /* 20 */ "Q",
  275. /* 21 */ "R",
  276. /* 22 */ "S",
  277. /* 23 */ "T",
  278. /* 24 */ "U",
  279. /* 25 */ "V",
  280. /* 26 */ "W",
  281. /* 27 */ "X",
  282. /* 28 */ "Y",
  283. /* 29 */ "Z",
  284. /* 30 */ "1",
  285. /* 31 */ "2",
  286. /* 32 */ "3",
  287. /* 33 */ "4",
  288. /* 34 */ "5",
  289. /* 35 */ "6",
  290. /* 36 */ "7",
  291. /* 37 */ "8",
  292. /* 38 */ "9",
  293. /* 39 */ "0",
  294. /* 40 */ "Return",
  295. /* 41 */ "Escape",
  296. /* 42 */ "Backspace",
  297. /* 43 */ "Tab",
  298. /* 44 */ "Space",
  299. /* 45 */ "-",
  300. /* 46 */ "=",
  301. /* 47 */ "[",
  302. /* 48 */ "]",
  303. /* 49 */ "\\",
  304. /* 50 */ "#",
  305. /* 51 */ ";",
  306. /* 52 */ "'",
  307. /* 53 */ "`",
  308. /* 54 */ ",",
  309. /* 55 */ ".",
  310. /* 56 */ "/",
  311. /* 57 */ "CapsLock",
  312. /* 58 */ "F1",
  313. /* 59 */ "F2",
  314. /* 60 */ "F3",
  315. /* 61 */ "F4",
  316. /* 62 */ "F5",
  317. /* 63 */ "F6",
  318. /* 64 */ "F7",
  319. /* 65 */ "F8",
  320. /* 66 */ "F9",
  321. /* 67 */ "F10",
  322. /* 68 */ "F11",
  323. /* 69 */ "F12",
  324. /* 70 */ "PrintScreen",
  325. /* 71 */ "ScrollLock",
  326. /* 72 */ "Pause",
  327. /* 73 */ "Insert",
  328. /* 74 */ "Home",
  329. /* 75 */ "PageUp",
  330. /* 76 */ "Delete",
  331. /* 77 */ "End",
  332. /* 78 */ "PageDown",
  333. /* 79 */ "Right",
  334. /* 80 */ "Left",
  335. /* 81 */ "Down",
  336. /* 82 */ "Up",
  337. /* 83 */ "Numlock",
  338. /* 84 */ "Keypad /",
  339. /* 85 */ "Keypad *",
  340. /* 86 */ "Keypad -",
  341. /* 87 */ "Keypad +",
  342. /* 88 */ "Keypad Enter",
  343. /* 89 */ "Keypad 1",
  344. /* 90 */ "Keypad 2",
  345. /* 91 */ "Keypad 3",
  346. /* 92 */ "Keypad 4",
  347. /* 93 */ "Keypad 5",
  348. /* 94 */ "Keypad 6",
  349. /* 95 */ "Keypad 7",
  350. /* 96 */ "Keypad 8",
  351. /* 97 */ "Keypad 9",
  352. /* 98 */ "Keypad 0",
  353. /* 99 */ "Keypad .",
  354. /* 100 */ NULL,
  355. /* 101 */ "Application",
  356. /* 102 */ "Power",
  357. /* 103 */ "Keypad =",
  358. /* 104 */ "F13",
  359. /* 105 */ "F14",
  360. /* 106 */ "F15",
  361. /* 107 */ "F16",
  362. /* 108 */ "F17",
  363. /* 109 */ "F18",
  364. /* 110 */ "F19",
  365. /* 111 */ "F20",
  366. /* 112 */ "F21",
  367. /* 113 */ "F22",
  368. /* 114 */ "F23",
  369. /* 115 */ "F24",
  370. /* 116 */ "Execute",
  371. /* 117 */ "Help",
  372. /* 118 */ "Menu",
  373. /* 119 */ "Select",
  374. /* 120 */ "Stop",
  375. /* 121 */ "Again",
  376. /* 122 */ "Undo",
  377. /* 123 */ "Cut",
  378. /* 124 */ "Copy",
  379. /* 125 */ "Paste",
  380. /* 126 */ "Find",
  381. /* 127 */ "Mute",
  382. /* 128 */ "VolumeUp",
  383. /* 129 */ "VolumeDown",
  384. /* 130 */ NULL,
  385. /* 131 */ NULL,
  386. /* 132 */ NULL,
  387. /* 133 */ "Keypad ,",
  388. /* 134 */ "Keypad = (AS400)",
  389. /* 135 */ NULL,
  390. /* 136 */ NULL,
  391. /* 137 */ NULL,
  392. /* 138 */ NULL,
  393. /* 139 */ NULL,
  394. /* 140 */ NULL,
  395. /* 141 */ NULL,
  396. /* 142 */ NULL,
  397. /* 143 */ NULL,
  398. /* 144 */ NULL,
  399. /* 145 */ NULL,
  400. /* 146 */ NULL,
  401. /* 147 */ NULL,
  402. /* 148 */ NULL,
  403. /* 149 */ NULL,
  404. /* 150 */ NULL,
  405. /* 151 */ NULL,
  406. /* 152 */ NULL,
  407. /* 153 */ "AltErase",
  408. /* 154 */ "SysReq",
  409. /* 155 */ "Cancel",
  410. /* 156 */ "Clear",
  411. /* 157 */ "Prior",
  412. /* 158 */ "Return",
  413. /* 159 */ "Separator",
  414. /* 160 */ "Out",
  415. /* 161 */ "Oper",
  416. /* 162 */ "Clear / Again",
  417. /* 163 */ "CrSel",
  418. /* 164 */ "ExSel",
  419. /* 165 */ NULL,
  420. /* 166 */ NULL,
  421. /* 167 */ NULL,
  422. /* 168 */ NULL,
  423. /* 169 */ NULL,
  424. /* 170 */ NULL,
  425. /* 171 */ NULL,
  426. /* 172 */ NULL,
  427. /* 173 */ NULL,
  428. /* 174 */ NULL,
  429. /* 175 */ NULL,
  430. /* 176 */ "Keypad 00",
  431. /* 177 */ "Keypad 000",
  432. /* 178 */ "ThousandsSeparator",
  433. /* 179 */ "DecimalSeparator",
  434. /* 180 */ "CurrencyUnit",
  435. /* 181 */ "CurrencySubUnit",
  436. /* 182 */ "Keypad (",
  437. /* 183 */ "Keypad )",
  438. /* 184 */ "Keypad {",
  439. /* 185 */ "Keypad }",
  440. /* 186 */ "Keypad Tab",
  441. /* 187 */ "Keypad Backspace",
  442. /* 188 */ "Keypad A",
  443. /* 189 */ "Keypad B",
  444. /* 190 */ "Keypad C",
  445. /* 191 */ "Keypad D",
  446. /* 192 */ "Keypad E",
  447. /* 193 */ "Keypad F",
  448. /* 194 */ "Keypad XOR",
  449. /* 195 */ "Keypad ^",
  450. /* 196 */ "Keypad %",
  451. /* 197 */ "Keypad <",
  452. /* 198 */ "Keypad >",
  453. /* 199 */ "Keypad &",
  454. /* 200 */ "Keypad &&",
  455. /* 201 */ "Keypad |",
  456. /* 202 */ "Keypad ||",
  457. /* 203 */ "Keypad :",
  458. /* 204 */ "Keypad #",
  459. /* 205 */ "Keypad Space",
  460. /* 206 */ "Keypad @",
  461. /* 207 */ "Keypad !",
  462. /* 208 */ "Keypad MemStore",
  463. /* 209 */ "Keypad MemRecall",
  464. /* 210 */ "Keypad MemClear",
  465. /* 211 */ "Keypad MemAdd",
  466. /* 212 */ "Keypad MemSubtract",
  467. /* 213 */ "Keypad MemMultiply",
  468. /* 214 */ "Keypad MemDivide",
  469. /* 215 */ "Keypad +/-",
  470. /* 216 */ "Keypad Clear",
  471. /* 217 */ "Keypad ClearEntry",
  472. /* 218 */ "Keypad Binary",
  473. /* 219 */ "Keypad Octal",
  474. /* 220 */ "Keypad Decimal",
  475. /* 221 */ "Keypad Hexadecimal",
  476. /* 222 */ NULL,
  477. /* 223 */ NULL,
  478. /* 224 */ "Left Ctrl",
  479. /* 225 */ "Left Shift",
  480. /* 226 */ "Left Alt",
  481. /* 227 */ "Left GUI",
  482. /* 228 */ "Right Ctrl",
  483. /* 229 */ "Right Shift",
  484. /* 230 */ "Right Alt",
  485. /* 231 */ "Right GUI",
  486. /* 232 */ NULL,
  487. /* 233 */ NULL,
  488. /* 234 */ NULL,
  489. /* 235 */ NULL,
  490. /* 236 */ NULL,
  491. /* 237 */ NULL,
  492. /* 238 */ NULL,
  493. /* 239 */ NULL,
  494. /* 240 */ NULL,
  495. /* 241 */ NULL,
  496. /* 242 */ NULL,
  497. /* 243 */ NULL,
  498. /* 244 */ NULL,
  499. /* 245 */ NULL,
  500. /* 246 */ NULL,
  501. /* 247 */ NULL,
  502. /* 248 */ NULL,
  503. /* 249 */ NULL,
  504. /* 250 */ NULL,
  505. /* 251 */ NULL,
  506. /* 252 */ NULL,
  507. /* 253 */ NULL,
  508. /* 254 */ NULL,
  509. /* 255 */ NULL,
  510. /* 256 */ NULL,
  511. /* 257 */ "ModeSwitch",
  512. /* 258 */ "Sleep",
  513. /* 259 */ "Wake",
  514. /* 260 */ "ChannelUp",
  515. /* 261 */ "ChannelDown",
  516. /* 262 */ "MediaPlay",
  517. /* 263 */ "MediaPause",
  518. /* 264 */ "MediaRecord",
  519. /* 265 */ "MediaFastForward",
  520. /* 266 */ "MediaRewind",
  521. /* 267 */ "MediaTrackNext",
  522. /* 268 */ "MediaTrackPrevious",
  523. /* 269 */ "MediaStop",
  524. /* 270 */ "Eject",
  525. /* 271 */ "MediaPlayPause",
  526. /* 272 */ "MediaSelect",
  527. /* 273 */ "AC New",
  528. /* 274 */ "AC Open",
  529. /* 275 */ "AC Close",
  530. /* 276 */ "AC Exit",
  531. /* 277 */ "AC Save",
  532. /* 278 */ "AC Print",
  533. /* 279 */ "AC Properties",
  534. /* 280 */ "AC Search",
  535. /* 281 */ "AC Home",
  536. /* 282 */ "AC Back",
  537. /* 283 */ "AC Forward",
  538. /* 284 */ "AC Stop",
  539. /* 285 */ "AC Refresh",
  540. /* 286 */ "AC Bookmarks",
  541. /* 287 */ "SoftLeft",
  542. /* 288 */ "SoftRight",
  543. /* 289 */ "Call",
  544. /* 290 */ "EndCall",
  545. };
  546. int SDL_SetScancodeName(SDL_Scancode scancode, const char *name)
  547. {
  548. if (((int)scancode) < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
  549. return SDL_InvalidParamError("scancode");
  550. }
  551. SDL_scancode_names[scancode] = name;
  552. return 0;
  553. }
  554. // these are static memory, so we don't use SDL_FreeLater on them.
  555. const char *SDL_GetScancodeName(SDL_Scancode scancode)
  556. {
  557. const char *name;
  558. if (((int)scancode) < SDL_SCANCODE_UNKNOWN || scancode >= SDL_NUM_SCANCODES) {
  559. SDL_InvalidParamError("scancode");
  560. return "";
  561. }
  562. name = SDL_scancode_names[scancode];
  563. if (name) {
  564. return name;
  565. }
  566. return "";
  567. }
  568. SDL_Scancode SDL_GetScancodeFromName(const char *name)
  569. {
  570. int i;
  571. if (!name || !*name) {
  572. SDL_InvalidParamError("name");
  573. return SDL_SCANCODE_UNKNOWN;
  574. }
  575. for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
  576. if (!SDL_scancode_names[i]) {
  577. continue;
  578. }
  579. if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
  580. return (SDL_Scancode)i;
  581. }
  582. }
  583. SDL_InvalidParamError("name");
  584. return SDL_SCANCODE_UNKNOWN;
  585. }
  586. const char *SDL_GetKeyName(SDL_Keycode key)
  587. {
  588. char name[8];
  589. char *end;
  590. if (key & SDLK_SCANCODE_MASK) {
  591. return SDL_GetScancodeName((SDL_Scancode)(key & ~SDLK_SCANCODE_MASK));
  592. }
  593. switch (key) {
  594. case SDLK_RETURN:
  595. return SDL_GetScancodeName(SDL_SCANCODE_RETURN);
  596. case SDLK_ESCAPE:
  597. return SDL_GetScancodeName(SDL_SCANCODE_ESCAPE);
  598. case SDLK_BACKSPACE:
  599. return SDL_GetScancodeName(SDL_SCANCODE_BACKSPACE);
  600. case SDLK_TAB:
  601. return SDL_GetScancodeName(SDL_SCANCODE_TAB);
  602. case SDLK_SPACE:
  603. return SDL_GetScancodeName(SDL_SCANCODE_SPACE);
  604. case SDLK_DELETE:
  605. return SDL_GetScancodeName(SDL_SCANCODE_DELETE);
  606. default:
  607. /* Unaccented letter keys on latin keyboards are normally
  608. labeled in upper case (and probably on others like Greek or
  609. Cyrillic too, so if you happen to know for sure, please
  610. adapt this). */
  611. if (key >= 'a' && key <= 'z') {
  612. key -= 32;
  613. }
  614. end = SDL_UCS4ToUTF8(key, name);
  615. *end = '\0';
  616. return SDL_FreeLater(SDL_strdup(name));
  617. }
  618. }
  619. SDL_Keycode SDL_GetKeyFromName(const char *name)
  620. {
  621. SDL_Keycode key;
  622. /* Check input */
  623. if (!name) {
  624. return SDLK_UNKNOWN;
  625. }
  626. /* If it's a single UTF-8 character, then that's the keycode itself */
  627. key = *(const unsigned char *)name;
  628. if (key >= 0xF0) {
  629. if (SDL_strlen(name) == 4) {
  630. int i = 0;
  631. key = (Uint16)(name[i] & 0x07) << 18;
  632. key |= (Uint16)(name[++i] & 0x3F) << 12;
  633. key |= (Uint16)(name[++i] & 0x3F) << 6;
  634. key |= (Uint16)(name[++i] & 0x3F);
  635. return key;
  636. }
  637. return SDLK_UNKNOWN;
  638. } else if (key >= 0xE0) {
  639. if (SDL_strlen(name) == 3) {
  640. int i = 0;
  641. key = (Uint16)(name[i] & 0x0F) << 12;
  642. key |= (Uint16)(name[++i] & 0x3F) << 6;
  643. key |= (Uint16)(name[++i] & 0x3F);
  644. return key;
  645. }
  646. return SDLK_UNKNOWN;
  647. } else if (key >= 0xC0) {
  648. if (SDL_strlen(name) == 2) {
  649. int i = 0;
  650. key = (Uint16)(name[i] & 0x1F) << 6;
  651. key |= (Uint16)(name[++i] & 0x3F);
  652. return key;
  653. }
  654. return SDLK_UNKNOWN;
  655. } else {
  656. if (SDL_strlen(name) == 1) {
  657. if (key >= 'A' && key <= 'Z') {
  658. key += 32;
  659. }
  660. return key;
  661. }
  662. /* Get the scancode for this name, and the associated keycode */
  663. return SDL_GetKeyFromScancode(SDL_GetScancodeFromName(name), SDL_KMOD_NONE);
  664. }
  665. }