From 124225899294723040ff0b4dc2805c6ff8902c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 25 Feb 2020 11:21:59 +0100 Subject: [PATCH] [ion] Handle key events in all simulators as in EPSILON_SDL_SCREEN_ONLY This way, physical keyboard events can be caught by python on all simulators --- ion/src/simulator/shared/events_keyboard.cpp | 4 - ion/src/simulator/shared/keyboard_sdl.cpp | 82 +++++++++----------- ion/src/simulator/shared/platform.h | 7 +- 3 files changed, 39 insertions(+), 54 deletions(-) diff --git a/ion/src/simulator/shared/events_keyboard.cpp b/ion/src/simulator/shared/events_keyboard.cpp index 0ba9dff6a..81638970d 100644 --- a/ion/src/simulator/shared/events_keyboard.cpp +++ b/ion/src/simulator/shared/events_keyboard.cpp @@ -188,14 +188,12 @@ static Event eventFromSDLTextInputEvent(SDL_TextInputEvent event) { } char character = event.text[0]; if (character >= 32 && character < 127) { -#if EPSILON_SDL_SCREEN_ONLY /* We remove the shift, otherwise it might stay activated when it shouldn't. * For instance on a French keyboard, to input "1", we first press "Shift" * (which activates the Shift modifier on the calculator), then we press * "&", transformed by eventFromSDLTextInputEvent into the text "1". If we * do not remove the Shift here, it would still be pressed afterwards. */ Ion::Events::removeShift(); -#endif return sEventForASCIICharAbove32[character-32]; } return None; @@ -220,11 +218,9 @@ Event getPlatformEvent() { return Termination; } if (event.type == SDL_KEYDOWN) { -#if EPSILON_SDL_SCREEN_ONLY if (IonSimulatorSDLKeyDetectedByScan(event.key.keysym.scancode)) { continue; } -#endif return eventFromSDLKeyboardEvent(event.key); } if (event.type == SDL_TEXTINPUT) { diff --git a/ion/src/simulator/shared/keyboard_sdl.cpp b/ion/src/simulator/shared/keyboard_sdl.cpp index 589d21a49..a1b173719 100644 --- a/ion/src/simulator/shared/keyboard_sdl.cpp +++ b/ion/src/simulator/shared/keyboard_sdl.cpp @@ -17,6 +17,7 @@ void IonSimulatorKeyboardKeyUp(int keyNumber) { Ion::Keyboard::Key key = static_cast(keyNumber); sKeyboardState.clearKey(key); } +#endif namespace Ion { namespace Keyboard { @@ -49,6 +50,42 @@ constexpr static KeySDLKeyPair sKeyPairs[] = { constexpr int sNumberOfKeyPairs = sizeof(sKeyPairs)/sizeof(KeySDLKeyPair); +State scan() { + // We need to tell SDL to get new state from the host OS + SDL_PumpEvents(); + + // Notify callbacks in case we need to do something + IonSimulatorCallbackDidScanKeyboard(); + + // Grab this opportunity to refresh the display if needed + Simulator::Main::refresh(); + + // Start with a "clean" state + State state; +#if EPSILON_SDL_SCREEN_ONLY + // In this case, keyboard states are sent over another channel. + state = sKeyboardState; +#else + // Register a key for the mouse, if any + SDL_Point p; + Uint32 mouseState = SDL_GetMouseState(&p.x, &p.y); + if (mouseState & SDL_BUTTON(SDL_BUTTON_LEFT)) { + Key k = Simulator::Layout::keyAt(&p); + state.setKey(k); + } +#endif + + // Catch the physical keyboard events + const uint8_t * SDLstate = SDL_GetKeyboardState(NULL); + for (int i = 0; i < sNumberOfKeyPairs; i++) { + KeySDLKeyPair pair = sKeyPairs[i]; + if (SDLstate[pair.SDLKey()]) { + state.setKey(pair.key()); + } + } + return state; +} + } } @@ -60,48 +97,3 @@ bool IonSimulatorSDLKeyDetectedByScan(SDL_Scancode key) { } return false; } -#endif - -namespace Ion { -namespace Keyboard { - -State scan() { - // We need to tell SDL to get new state from the host OS - SDL_PumpEvents(); - - // Notify callbacks in case we need to do something - IonSimulatorCallbackDidScanKeyboard(); - - // Grab this opportunity to refresh the display if needed - Simulator::Main::refresh(); - -#if EPSILON_SDL_SCREEN_ONLY - /* In this case, keyboard states will be sent over another channel, but we - * still need to catch the physical keyboard events */ - const uint8_t * SDLstate = SDL_GetKeyboardState(NULL); - State state = sKeyboardState; - for (int i = 0; i < sNumberOfKeyPairs; i++) { - KeySDLKeyPair pair = sKeyPairs[i]; - if (SDLstate[pair.SDLKey()]) { - state.setKey(pair.key()); - } - } - return state; -#else - // Start with a "clean" state - State state; - - // Register a key for the mouse, if any - SDL_Point p; - Uint32 mouseState = SDL_GetMouseState(&p.x, &p.y); - if (mouseState & SDL_BUTTON(SDL_BUTTON_LEFT)) { - Key k = Simulator::Layout::keyAt(&p); - state.setKey(k); - } - - return state; -#endif -} - -} -} diff --git a/ion/src/simulator/shared/platform.h b/ion/src/simulator/shared/platform.h index c6e27bbee..494c83c23 100644 --- a/ion/src/simulator/shared/platform.h +++ b/ion/src/simulator/shared/platform.h @@ -2,6 +2,7 @@ #define ION_SIMULATOR_PLATFORM_H #include +#include #ifdef __cplusplus extern "C" { @@ -14,16 +15,12 @@ SDL_Texture * IonSimulatorLoadImage(SDL_Renderer * renderer, const char * identi char * IonSimulatorGetLanguageCode(); #if EPSILON_SDL_SCREEN_ONLY - void IonSimulatorKeyboardKeyDown(int keyNumber); void IonSimulatorKeyboardKeyUp(int keyNumber); - void IonSimulatorEventsPushEvent(int eventNumber); - -bool IonSimulatorSDLKeyDetectedByScan(SDL_Scancode key); - #endif +bool IonSimulatorSDLKeyDetectedByScan(SDL_Scancode key); void IonSimulatorCallbackDidRefresh(); void IonSimulatorCallbackDidScanKeyboard();