SDL_x11keyboard.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. #ifdef SDL_VIDEO_DRIVER_X11
  20. #include "SDL_x11video.h"
  21. #include "../../events/SDL_keyboard_c.h"
  22. #include "../../events/SDL_scancode_tables_c.h"
  23. #include <X11/keysym.h>
  24. #include <X11/XKBlib.h>
  25. #include "../../events/imKStoUCS.h"
  26. #include "../../events/SDL_keysym_to_scancode_c.h"
  27. #ifdef X_HAVE_UTF8_STRING
  28. #include <locale.h>
  29. #endif
  30. static SDL_ScancodeTable scancode_set[] = {
  31. SDL_SCANCODE_TABLE_DARWIN,
  32. SDL_SCANCODE_TABLE_XFREE86_1,
  33. SDL_SCANCODE_TABLE_XFREE86_2,
  34. SDL_SCANCODE_TABLE_XVNC,
  35. };
  36. static SDL_bool X11_ScancodeIsRemappable(SDL_Scancode scancode)
  37. {
  38. /*
  39. * XKB remappings can assign different keysyms for these scancodes, but
  40. * as these keys are in fixed positions, the scancodes themselves shouldn't
  41. * be switched. Mark them as not being remappable.
  42. */
  43. switch (scancode) {
  44. case SDL_SCANCODE_ESCAPE:
  45. case SDL_SCANCODE_CAPSLOCK:
  46. case SDL_SCANCODE_NUMLOCKCLEAR:
  47. case SDL_SCANCODE_LSHIFT:
  48. case SDL_SCANCODE_RSHIFT:
  49. case SDL_SCANCODE_LCTRL:
  50. case SDL_SCANCODE_RCTRL:
  51. case SDL_SCANCODE_LALT:
  52. case SDL_SCANCODE_RALT:
  53. case SDL_SCANCODE_LGUI:
  54. case SDL_SCANCODE_RGUI:
  55. return SDL_FALSE;
  56. default:
  57. return SDL_TRUE;
  58. }
  59. }
  60. /* This function only correctly maps letters and numbers for keyboards in US QWERTY layout */
  61. static SDL_Scancode X11_KeyCodeToSDLScancode(SDL_VideoDevice *_this, KeyCode keycode)
  62. {
  63. const KeySym keysym = X11_KeyCodeToSym(_this, keycode, 0, 0);
  64. if (keysym == NoSymbol) {
  65. return SDL_SCANCODE_UNKNOWN;
  66. }
  67. return SDL_GetScancodeFromKeySym(keysym, keycode);
  68. }
  69. KeySym X11_KeyCodeToSym(SDL_VideoDevice *_this, KeyCode keycode, unsigned char group, unsigned int mod_mask)
  70. {
  71. SDL_VideoData *data = _this->driverdata;
  72. KeySym keysym;
  73. unsigned int mods_ret[16];
  74. SDL_zero(mods_ret);
  75. #ifdef SDL_VIDEO_DRIVER_X11_HAS_XKBLOOKUPKEYSYM
  76. if (data->xkb) {
  77. int num_groups = XkbKeyNumGroups(data->xkb, keycode);
  78. unsigned char info = XkbKeyGroupInfo(data->xkb, keycode);
  79. if (num_groups && group >= num_groups) {
  80. int action = XkbOutOfRangeGroupAction(info);
  81. if (action == XkbRedirectIntoRange) {
  82. group = XkbOutOfRangeGroupNumber(info);
  83. if (group >= num_groups) {
  84. group = 0;
  85. }
  86. } else if (action == XkbClampIntoRange) {
  87. group = num_groups - 1;
  88. } else {
  89. group %= num_groups;
  90. }
  91. }
  92. if (X11_XkbLookupKeySym(data->display, keycode, XkbBuildCoreState(mod_mask, group), mods_ret, &keysym) == NoSymbol) {
  93. keysym = NoSymbol;
  94. }
  95. } else
  96. #endif
  97. {
  98. /* TODO: Handle groups and modifiers on the legacy path. */
  99. keysym = X11_XKeycodeToKeysym(data->display, keycode, 0);
  100. }
  101. return keysym;
  102. }
  103. int X11_InitKeyboard(SDL_VideoDevice *_this)
  104. {
  105. SDL_VideoData *data = _this->driverdata;
  106. int i = 0;
  107. int j = 0;
  108. int min_keycode, max_keycode;
  109. struct
  110. {
  111. SDL_Scancode scancode;
  112. KeySym keysym;
  113. int value;
  114. } fingerprint[] = {
  115. { SDL_SCANCODE_HOME, XK_Home, 0 },
  116. { SDL_SCANCODE_PAGEUP, XK_Prior, 0 },
  117. { SDL_SCANCODE_UP, XK_Up, 0 },
  118. { SDL_SCANCODE_LEFT, XK_Left, 0 },
  119. { SDL_SCANCODE_DELETE, XK_Delete, 0 },
  120. { SDL_SCANCODE_KP_ENTER, XK_KP_Enter, 0 },
  121. };
  122. int best_distance;
  123. int best_index;
  124. int distance;
  125. Bool xkb_repeat = 0;
  126. #ifdef SDL_VIDEO_DRIVER_X11_HAS_XKBLOOKUPKEYSYM
  127. {
  128. int xkb_major = XkbMajorVersion;
  129. int xkb_minor = XkbMinorVersion;
  130. if (X11_XkbQueryExtension(data->display, NULL, &data->xkb_event, NULL, &xkb_major, &xkb_minor)) {
  131. data->xkb = X11_XkbGetMap(data->display, XkbAllClientInfoMask, XkbUseCoreKbd);
  132. }
  133. /* This will remove KeyRelease events for held keys */
  134. X11_XkbSetDetectableAutoRepeat(data->display, True, &xkb_repeat);
  135. }
  136. #endif
  137. /* Open a connection to the X input manager */
  138. #ifdef X_HAVE_UTF8_STRING
  139. if (SDL_X11_HAVE_UTF8) {
  140. /* Set the locale, and call XSetLocaleModifiers before XOpenIM so that
  141. Compose keys will work correctly. */
  142. char *prev_locale = setlocale(LC_ALL, NULL);
  143. char *prev_xmods = X11_XSetLocaleModifiers(NULL);
  144. const char *new_xmods = "";
  145. const char *env_xmods = SDL_getenv("XMODIFIERS");
  146. SDL_bool has_dbus_ime_support = SDL_FALSE;
  147. if (prev_locale) {
  148. prev_locale = SDL_strdup(prev_locale);
  149. }
  150. if (prev_xmods) {
  151. prev_xmods = SDL_strdup(prev_xmods);
  152. }
  153. /* IBus resends some key events that were filtered by XFilterEvents
  154. when it is used via XIM which causes issues. Prevent this by forcing
  155. @im=none if XMODIFIERS contains @im=ibus. IBus can still be used via
  156. the DBus implementation, which also has support for pre-editing. */
  157. if (env_xmods && SDL_strstr(env_xmods, "@im=ibus") != NULL) {
  158. has_dbus_ime_support = SDL_TRUE;
  159. }
  160. if (env_xmods && SDL_strstr(env_xmods, "@im=fcitx") != NULL) {
  161. has_dbus_ime_support = SDL_TRUE;
  162. }
  163. if (has_dbus_ime_support || !xkb_repeat) {
  164. new_xmods = "@im=none";
  165. }
  166. (void)setlocale(LC_ALL, "");
  167. X11_XSetLocaleModifiers(new_xmods);
  168. data->im = X11_XOpenIM(data->display, NULL, NULL, NULL);
  169. /* Reset the locale + X locale modifiers back to how they were,
  170. locale first because the X locale modifiers depend on it. */
  171. (void)setlocale(LC_ALL, prev_locale);
  172. X11_XSetLocaleModifiers(prev_xmods);
  173. if (prev_locale) {
  174. SDL_free(prev_locale);
  175. }
  176. if (prev_xmods) {
  177. SDL_free(prev_xmods);
  178. }
  179. }
  180. #endif
  181. /* Try to determine which scancodes are being used based on fingerprint */
  182. best_distance = SDL_arraysize(fingerprint) + 1;
  183. best_index = -1;
  184. X11_XDisplayKeycodes(data->display, &min_keycode, &max_keycode);
  185. for (i = 0; i < SDL_arraysize(fingerprint); ++i) {
  186. fingerprint[i].value = X11_XKeysymToKeycode(data->display, fingerprint[i].keysym) - min_keycode;
  187. }
  188. for (i = 0; i < SDL_arraysize(scancode_set); ++i) {
  189. int table_size;
  190. const SDL_Scancode *table = SDL_GetScancodeTable(scancode_set[i], &table_size);
  191. distance = 0;
  192. for (j = 0; j < SDL_arraysize(fingerprint); ++j) {
  193. if (fingerprint[j].value < 0 || fingerprint[j].value >= table_size) {
  194. distance += 1;
  195. } else if (table[fingerprint[j].value] != fingerprint[j].scancode) {
  196. distance += 1;
  197. }
  198. }
  199. if (distance < best_distance) {
  200. best_distance = distance;
  201. best_index = i;
  202. }
  203. }
  204. if (best_index < 0 || best_distance > 2) {
  205. /* This is likely to be SDL_SCANCODE_TABLE_XFREE86_2 with remapped keys, double check a rarely remapped value */
  206. int fingerprint_value = X11_XKeysymToKeycode(data->display, 0x1008FF5B /* XF86Documents */) - min_keycode;
  207. if (fingerprint_value == 235) {
  208. for (i = 0; i < SDL_arraysize(scancode_set); ++i) {
  209. if (scancode_set[i] == SDL_SCANCODE_TABLE_XFREE86_2) {
  210. best_index = i;
  211. best_distance = 0;
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. if (best_index >= 0 && best_distance <= 2) {
  218. int table_size;
  219. const SDL_Scancode *table = SDL_GetScancodeTable(scancode_set[best_index], &table_size);
  220. #ifdef DEBUG_KEYBOARD
  221. SDL_Log("Using scancode set %d, min_keycode = %d, max_keycode = %d, table_size = %d\n", best_index, min_keycode, max_keycode, table_size);
  222. #endif
  223. /* This should never happen, but just in case... */
  224. if (table_size > (SDL_arraysize(data->key_layout) - min_keycode)) {
  225. table_size = (SDL_arraysize(data->key_layout) - min_keycode);
  226. }
  227. SDL_memcpy(&data->key_layout[min_keycode], table, sizeof(SDL_Scancode) * table_size);
  228. /* Scancodes represent physical locations on the keyboard, unaffected by keyboard mapping.
  229. However, there are a number of extended scancodes that have no standard location, so use
  230. the X11 mapping for all non-character keys.
  231. */
  232. for (i = min_keycode; i <= max_keycode; ++i) {
  233. SDL_Scancode scancode = X11_KeyCodeToSDLScancode(_this, i);
  234. #ifdef DEBUG_KEYBOARD
  235. {
  236. KeySym sym;
  237. sym = X11_KeyCodeToSym(_this, (KeyCode)i, 0);
  238. SDL_Log("code = %d, sym = 0x%X (%s) ", i - min_keycode,
  239. (unsigned int)sym, sym == NoSymbol ? "NoSymbol" : X11_XKeysymToString(sym));
  240. }
  241. #endif
  242. if (scancode == data->key_layout[i]) {
  243. continue;
  244. }
  245. if ((SDL_GetDefaultKeyFromScancode(scancode, SDL_KMOD_NONE) & SDLK_SCANCODE_MASK) && X11_ScancodeIsRemappable(scancode)) {
  246. /* Not a character key and the scancode is safe to remap */
  247. #ifdef DEBUG_KEYBOARD
  248. SDL_Log("Changing scancode, was %d (%s), now %d (%s)\n", data->key_layout[i], SDL_GetScancodeName(data->key_layout[i]), scancode, SDL_GetScancodeName(scancode));
  249. #endif
  250. data->key_layout[i] = scancode;
  251. }
  252. }
  253. } else {
  254. #ifdef DEBUG_SCANCODES
  255. SDL_Log("Keyboard layout unknown, please report the following to the SDL forums/mailing list (https://discourse.libsdl.org/):\n");
  256. #endif
  257. /* Determine key_layout - only works on US QWERTY layout */
  258. for (i = min_keycode; i <= max_keycode; ++i) {
  259. SDL_Scancode scancode = X11_KeyCodeToSDLScancode(_this, i);
  260. #ifdef DEBUG_SCANCODES
  261. {
  262. KeySym sym;
  263. sym = X11_KeyCodeToSym(_this, (KeyCode)i, 0);
  264. SDL_Log("code = %d, sym = 0x%X (%s) ", i - min_keycode,
  265. (unsigned int)sym, sym == NoSymbol ? "NoSymbol" : X11_XKeysymToString(sym));
  266. }
  267. if (scancode == SDL_SCANCODE_UNKNOWN) {
  268. SDL_Log("scancode not found\n");
  269. } else {
  270. SDL_Log("scancode = %d (%s)\n", scancode, SDL_GetScancodeName(scancode));
  271. }
  272. #endif
  273. data->key_layout[i] = scancode;
  274. }
  275. }
  276. X11_UpdateKeymap(_this, SDL_FALSE);
  277. SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
  278. #ifdef SDL_USE_IME
  279. SDL_IME_Init();
  280. #endif
  281. X11_ReconcileKeyboardState(_this);
  282. return 0;
  283. }
  284. void X11_UpdateKeymap(SDL_VideoDevice *_this, SDL_bool send_event)
  285. {
  286. struct Keymod_masks
  287. {
  288. SDL_Keymod sdl_mask;
  289. unsigned int xkb_mask;
  290. } const keymod_masks[] = {
  291. { SDL_KMOD_NONE, 0 },
  292. { SDL_KMOD_SHIFT, ShiftMask },
  293. { SDL_KMOD_CAPS, LockMask },
  294. { SDL_KMOD_SHIFT | SDL_KMOD_CAPS, ShiftMask | LockMask },
  295. { SDL_KMOD_MODE, Mod5Mask },
  296. { SDL_KMOD_MODE | SDL_KMOD_SHIFT, Mod5Mask | ShiftMask },
  297. { SDL_KMOD_MODE | SDL_KMOD_CAPS, Mod5Mask | LockMask },
  298. { SDL_KMOD_MODE | SDL_KMOD_SHIFT | SDL_KMOD_CAPS, Mod5Mask | ShiftMask | LockMask }
  299. };
  300. SDL_VideoData *data = _this->driverdata;
  301. int i;
  302. SDL_Scancode scancode;
  303. SDL_Keymap *keymap;
  304. keymap = SDL_CreateKeymap();
  305. #ifdef SDL_VIDEO_DRIVER_X11_HAS_XKBLOOKUPKEYSYM
  306. if (data->xkb) {
  307. XkbStateRec state;
  308. X11_XkbGetUpdatedMap(data->display, XkbAllClientInfoMask, data->xkb);
  309. if (X11_XkbGetState(data->display, XkbUseCoreKbd, &state) == Success) {
  310. data->xkb_group = state.group;
  311. }
  312. }
  313. #endif
  314. for (int m = 0; m < SDL_arraysize(keymod_masks); ++m) {
  315. for (i = 0; i < SDL_arraysize(data->key_layout); i++) {
  316. SDL_Keycode keycode;
  317. /* Make sure this is a valid scancode */
  318. scancode = data->key_layout[i];
  319. if (scancode == SDL_SCANCODE_UNKNOWN) {
  320. continue;
  321. }
  322. KeySym keysym = X11_KeyCodeToSym(_this, i, data->xkb_group, keymod_masks[m].xkb_mask);
  323. /* Note: The default SDL scancode table sets this to right alt instead of AltGr/Mode, so handle it separately. */
  324. if (keysym != XK_ISO_Level3_Shift) {
  325. keycode = SDL_KeySymToUcs4(keysym);
  326. } else {
  327. keycode = SDLK_MODE;
  328. }
  329. if (!keycode) {
  330. SDL_Scancode keyScancode = SDL_GetScancodeFromKeySym(keysym, (KeyCode)i);
  331. keycode = SDL_GetDefaultKeyFromScancode(keyScancode, keymod_masks[m].sdl_mask);
  332. }
  333. SDL_SetKeymapEntry(keymap, scancode, keymod_masks[m].sdl_mask, keycode);
  334. }
  335. }
  336. SDL_SetKeymap(keymap, send_event);
  337. }
  338. void X11_QuitKeyboard(SDL_VideoDevice *_this)
  339. {
  340. SDL_VideoData *data = _this->driverdata;
  341. #ifdef SDL_VIDEO_DRIVER_X11_HAS_XKBLOOKUPKEYSYM
  342. if (data->xkb) {
  343. X11_XkbFreeKeyboard(data->xkb, 0, True);
  344. data->xkb = NULL;
  345. }
  346. #endif
  347. #ifdef SDL_USE_IME
  348. SDL_IME_Quit();
  349. #endif
  350. }
  351. static void X11_ResetXIM(SDL_VideoDevice *_this, SDL_Window *window)
  352. {
  353. #ifdef X_HAVE_UTF8_STRING
  354. SDL_WindowData *data = window->driverdata;
  355. if (data && data->ic) {
  356. /* Clear any partially entered dead keys */
  357. char *contents = X11_Xutf8ResetIC(data->ic);
  358. if (contents) {
  359. X11_XFree(contents);
  360. }
  361. }
  362. #endif
  363. }
  364. int X11_StartTextInput(SDL_VideoDevice *_this, SDL_Window *window)
  365. {
  366. X11_ResetXIM(_this, window);
  367. return X11_UpdateTextInputArea(_this, window);
  368. }
  369. int X11_StopTextInput(SDL_VideoDevice *_this, SDL_Window *window)
  370. {
  371. X11_ResetXIM(_this, window);
  372. #ifdef SDL_USE_IME
  373. SDL_IME_Reset();
  374. #endif
  375. return 0;
  376. }
  377. int X11_UpdateTextInputArea(SDL_VideoDevice *_this, SDL_Window *window)
  378. {
  379. #ifdef SDL_USE_IME
  380. SDL_IME_UpdateTextInputArea(window);
  381. #endif
  382. return 0;
  383. }
  384. SDL_bool X11_HasScreenKeyboardSupport(SDL_VideoDevice *_this)
  385. {
  386. SDL_VideoData *videodata = _this->driverdata;
  387. return videodata->is_steam_deck;
  388. }
  389. void X11_ShowScreenKeyboard(SDL_VideoDevice *_this, SDL_Window *window)
  390. {
  391. SDL_VideoData *videodata = _this->driverdata;
  392. if (videodata->is_steam_deck) {
  393. /* For more documentation of the URL parameters, see:
  394. * https://partner.steamgames.com/doc/api/ISteamUtils#ShowFloatingGamepadTextInput
  395. */
  396. char deeplink[128];
  397. (void)SDL_snprintf(deeplink, sizeof(deeplink),
  398. "steam://open/keyboard?XPosition=0&YPosition=0&Width=0&Height=0&Mode=%d",
  399. SDL_GetHintBoolean(SDL_HINT_RETURN_KEY_HIDES_IME, SDL_FALSE) ? 0 : 1);
  400. SDL_OpenURL(deeplink);
  401. videodata->steam_keyboard_open = SDL_TRUE;
  402. }
  403. }
  404. void X11_HideScreenKeyboard(SDL_VideoDevice *_this, SDL_Window *window)
  405. {
  406. SDL_VideoData *videodata = _this->driverdata;
  407. if (videodata->is_steam_deck) {
  408. SDL_OpenURL("steam://close/keyboard");
  409. videodata->steam_keyboard_open = SDL_FALSE;
  410. }
  411. }
  412. SDL_bool X11_IsScreenKeyboardShown(SDL_VideoDevice *_this, SDL_Window *window)
  413. {
  414. SDL_VideoData *videodata = _this->driverdata;
  415. return videodata->steam_keyboard_open;
  416. }
  417. #endif /* SDL_VIDEO_DRIVER_X11 */