From 501ce2a4ad7aff9a3445f7a038c1305d376e9476 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Tue, 19 Jun 2018 13:32:55 +0200 Subject: [PATCH] Make keyboard event work with SDL2 --- src/input.c | 27 +++++++++++++++------------ src/keyboard.c | 35 ++++++++++++++++++++++++++++++++--- src/keyboard.h | 4 ++++ 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/input.c b/src/input.c index 898820c4..9b13ca8d 100644 --- a/src/input.c +++ b/src/input.c @@ -42,7 +42,9 @@ #include "loadsave.h" +#if defined(USE_SDL) #define RSUPER_EMULATES_META_MOD +#endif // Keyboards with a Super key never seem to have a Meta key at the same time. // This setting allows the right 'Super' key (the one with a 'Windows' or // 'Amiga' label to be used as a modifier instead of a normal key. @@ -181,7 +183,7 @@ int Is_shortcut(word key, word function) { if (key == 0 || function == 0xFFFF) return 0; - + if (function & 0x100) { if (Buttons_Pool[function&0xFF].Left_shortcut[0]==key) @@ -391,7 +393,6 @@ int Handle_mouse_release(SDL_MouseButtonEvent event) // Keyboard management -#if defined(USE_SDL) int Handle_key_press(SDL_KeyboardEvent event) { //Appui sur une touche du clavier @@ -399,8 +400,10 @@ int Handle_key_press(SDL_KeyboardEvent event) Key = Keysym_to_keycode(event.keysym); Key_ANSI = Keysym_to_ANSI(event.keysym); +#if defined(USE_SDL) Key_UNICODE = event.keysym.unicode; if (Key_UNICODE == 0) +#endif Key_UNICODE = Key_ANSI; switch(event.keysym.sym) { @@ -420,8 +423,13 @@ int Handle_key_press(SDL_KeyboardEvent event) modifier=MOD_ALT; break; +#if defined(USE_SDL2) + case SDLK_RGUI: + case SDLK_LGUI: +#else case SDLK_RMETA: case SDLK_LMETA: +#endif modifier=MOD_META; break; @@ -484,7 +492,6 @@ int Handle_key_press(SDL_KeyboardEvent event) } return 0; } -#endif int Release_control(int key_code, int modifier) { @@ -560,7 +567,6 @@ int Release_control(int key_code, int modifier) } -#if defined(USE_SDL) int Handle_key_release(SDL_KeyboardEvent event) { int modifier; @@ -591,8 +597,13 @@ int Handle_key_release(SDL_KeyboardEvent event) break; #endif +#if defined(USE_SDL2) + case SDLK_RGUI: + case SDLK_LGUI: +#else case SDLK_RMETA: case SDLK_LMETA: +#endif modifier=MOD_META; break; @@ -601,7 +612,6 @@ int Handle_key_release(SDL_KeyboardEvent event) } return Release_control(released_key, modifier); } -#endif // Joystick management @@ -939,19 +949,12 @@ int Get_input(int sleep_time) #endif case SDL_KEYDOWN: -#if defined(USE_SDL) Handle_key_press(event.key); -#else - //TODO SDL2 -#endif user_feedback_required = 1; break; case SDL_KEYUP: -#if defined(USE_SDL) Handle_key_release(event.key); - //TODO SDL2 -#endif break; // Start of Joystik handling diff --git a/src/keyboard.c b/src/keyboard.c index 82d9ac8e..516e2712 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -23,7 +23,7 @@ #include #include #include -#if defined(USE_SDL) +#if defined(USE_SDL) || defined(USE_SDL2) #include #endif #include "global.h" @@ -346,8 +346,15 @@ word Get_Key_modifiers(void) return modifiers; } +#endif +#if defined(USE_SDL) || defined(USE_SDL2) + +#if defined(USE_SDL) word Keysym_to_keycode(SDL_keysym keysym) +#elif defined(USE_SDL2) +word Keysym_to_keycode(SDL_Keysym keysym) +#endif { word key_code = 0; word mod; @@ -356,23 +363,39 @@ word Keysym_to_keycode(SDL_keysym keysym) if (keysym.sym == SDLK_RSHIFT || keysym.sym == SDLK_LSHIFT || keysym.sym == SDLK_RCTRL || keysym.sym == SDLK_LCTRL || keysym.sym == SDLK_RALT || keysym.sym == SDLK_LALT || +#if defined(USE_SDL2) + keysym.sym == SDLK_RGUI || keysym.sym == SDLK_LGUI || +#else keysym.sym == SDLK_RMETA || keysym.sym == SDLK_LMETA || +#endif keysym.sym == SDLK_MODE) // AltGr return 0; // Les touches qui n'ont qu'une valeur unicode (très rares) // seront codées sur 11 bits, le 12e bit est mis à 1 (0x0800) if (keysym.sym != 0) - key_code = keysym.sym; + key_code = K2K(keysym.sym); else if (keysym.scancode != 0) { key_code = (keysym.scancode & 0x07FF) | 0x0800; } +#if defined(USE_SDL) // Normally I should test keysym.mod here, but on windows the implementation // is buggy: if you release a modifier key, the following keys (when they repeat) // still name the original modifiers. mod = Get_Key_modifiers(); +#else + mod = 0; + if (keysym.mod & KMOD_CTRL) + mod |= MOD_CTRL; + if (keysym.mod & KMOD_SHIFT ) + mod |= MOD_SHIFT; + if (keysym.mod & (KMOD_ALT|KMOD_MODE)) + mod |= MOD_ALT; + if (keysym.mod & (KMOD_GUI)) + mod |= MOD_META; +#endif // SDL_GetModState() seems to get the right up-to-date info. key_code |= mod; @@ -764,7 +787,13 @@ word Keysym_to_ANSI(SDL_keysym keysym) return keysym.sym; } #elif defined(USE_SDL2) -// SDL2 TODO +word Keysym_to_ANSI(SDL_Keysym keysym) +{ + if (keysym.sym < 128) + return (word)keysym.sym; + return K2K(keysym.sym); +} + word Key_for_scancode(word scancode) { return scancode; diff --git a/src/keyboard.h b/src/keyboard.h index 3cc7fdc1..3397190e 100644 --- a/src/keyboard.h +++ b/src/keyboard.h @@ -51,6 +51,8 @@ */ #if defined(USE_SDL) word Keysym_to_ANSI(SDL_keysym keysym); +#elif defined(USE_SDL2) +word Keysym_to_ANSI(SDL_Keysym keysym); #endif /*! @@ -62,6 +64,8 @@ word Keysym_to_ANSI(SDL_keysym keysym); */ #if defined(USE_SDL) word Keysym_to_keycode(SDL_keysym keysym); +#elif defined(USE_SDL2) +word Keysym_to_keycode(SDL_Keysym keysym); #endif /*!