1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21#include "SDL_internal.h"
22
23#if defined(SDL_VIDEO_DRIVER_WAYLAND) || defined(SDL_VIDEO_DRIVER_X11)
24
25#include "SDL_keyboard_c.h"
26#include "SDL_keysym_to_scancode_c.h"
27#include "imKStoUCS.h"
28
29
30// Extended key code mappings
31static const struct
32{
33 Uint32 keysym;
34 SDL_Keycode keycode;
35} keysym_to_keycode_table[] = {
36 { 0xfe03, SDLK_MODE }, // XK_ISO_Level3_Shift
37 { 0xfe11, SDLK_LEVEL5_SHIFT }, // XK_ISO_Level5_Shift
38 { 0xfe20, SDLK_LEFT_TAB }, // XK_ISO_Left_Tab
39 { 0xff20, SDLK_MULTI_KEY_COMPOSE }, // XK_Multi_key
40 { 0xffe7, SDLK_LMETA }, // XK_Meta_L
41 { 0xffe8, SDLK_RMETA }, // XK_Meta_R
42 { 0xffed, SDLK_LHYPER }, // XK_Hyper_L
43 { 0xffee, SDLK_RHYPER }, // XK_Hyper_R
44};
45
46SDL_Keycode SDL_GetKeyCodeFromKeySym(Uint32 keysym, Uint32 keycode, SDL_Keymod modifiers)
47{
48 SDL_Keycode sdl_keycode = SDL_KeySymToUcs4(keysym);
49
50 if (!sdl_keycode) {
51 for (int i = 0; i < SDL_arraysize(keysym_to_keycode_table); ++i) {
52 if (keysym == keysym_to_keycode_table[i].keysym) {
53 return keysym_to_keycode_table[i].keycode;
54 }
55 }
56 }
57
58 if (!sdl_keycode) {
59 const SDL_Scancode scancode = SDL_GetScancodeFromKeySym(keysym, keycode);
60 if (scancode != SDL_SCANCODE_UNKNOWN) {
61 sdl_keycode = SDL_GetKeymapKeycode(NULL, scancode, modifiers);
62 }
63 }
64
65 return sdl_keycode;
66}
67
68#endif // SDL_VIDEO_DRIVER_WAYLAND || SDL_VIDEO_DRIVER_X11
69