From 51acccaec2bad0bb53caecb57875ef766fa460d9 Mon Sep 17 00:00:00 2001 From: Alessio Caiazza Date: Sun, 28 Nov 2021 18:45:24 +0100 Subject: [PATCH] Don't pump SDL events outside from the main thread --- openrtx/src/threads.c | 8 ++ platform/drivers/display/display_libSDL.c | 2 +- platform/drivers/keyboard/keyboard_linux.c | 28 +----- platform/targets/linux/emulator/emulator.c | 106 ++++++++++++++++++++- platform/targets/linux/emulator/emulator.h | 2 + platform/targets/linux/platform.c | 7 +- 6 files changed, 123 insertions(+), 30 deletions(-) diff --git a/openrtx/src/threads.c b/openrtx/src/threads.c index 8e9a27a3..0eeaa9ba 100644 --- a/openrtx/src/threads.c +++ b/openrtx/src/threads.c @@ -38,6 +38,10 @@ #include #include #endif +#ifdef PLATFORM_LINUX +#include +#endif + /* Mutex for concurrent access to state variable */ pthread_mutex_t state_mutex; @@ -73,6 +77,10 @@ void *ui_task(void *arg) while(1) { + #ifdef PLATFORM_LINUX + emulator_process_sdl_events(); + #endif + // Read from the keyboard queue (returns 0 if no message is present) // Copy keyboard_t keys from received void * pointer msg event_t event; diff --git a/platform/drivers/display/display_libSDL.c b/platform/drivers/display/display_libSDL.c index b1a76159..95837ce2 100644 --- a/platform/drivers/display/display_libSDL.c +++ b/platform/drivers/display/display_libSDL.c @@ -197,7 +197,7 @@ uint32_t fetchPixelFromFb(unsigned int x, unsigned int y) void display_init() { - if (SDL_Init(SDL_INIT_VIDEO) < 0) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0) { printf("SDL video init error!!\n"); diff --git a/platform/drivers/keyboard/keyboard_linux.c b/platform/drivers/keyboard/keyboard_linux.c index ea27557b..1452832b 100644 --- a/platform/drivers/keyboard/keyboard_linux.c +++ b/platform/drivers/keyboard/keyboard_linux.c @@ -21,43 +21,21 @@ #include #include #include -#include extern keyboard_t shellkeyq_get(); +extern keyboard_t sdl_getKeys(); void kbd_init() { } + keyboard_t kbd_getKeys() { keyboard_t keys = 0; - SDL_PumpEvents(); //this pulls in emulated keypresses from the command shell keys |= shellkeyq_get(); + keys |= sdl_getKeys(); - const uint8_t *state = SDL_GetKeyboardState(NULL); - if (state[SDL_SCANCODE_0]) keys |= KEY_0; - if (state[SDL_SCANCODE_1]) keys |= KEY_1; - if (state[SDL_SCANCODE_2]) keys |= KEY_2; - if (state[SDL_SCANCODE_3]) keys |= KEY_3; - if (state[SDL_SCANCODE_4]) keys |= KEY_4; - if (state[SDL_SCANCODE_5]) keys |= KEY_5; - if (state[SDL_SCANCODE_6]) keys |= KEY_6; - if (state[SDL_SCANCODE_7]) keys |= KEY_7; - if (state[SDL_SCANCODE_8]) keys |= KEY_8; - if (state[SDL_SCANCODE_9]) keys |= KEY_9; - if (state[SDLK_ASTERISK]) keys |= KEY_STAR; - if (state[SDL_SCANCODE_ESCAPE]) keys |= KEY_ESC; - if (state[SDL_SCANCODE_DOWN]) keys |= KEY_DOWN; - if (state[SDL_SCANCODE_UP]) keys |= KEY_UP; - if (state[SDL_SCANCODE_LEFT]) keys |= KEY_LEFT; - if (state[SDL_SCANCODE_RIGHT]) keys |= KEY_RIGHT; - if (state[SDL_SCANCODE_RETURN]) keys |= KEY_ENTER; - if (state[SDL_SCANCODE_NONUSHASH]) keys |= KEY_HASH; - if (state[SDL_SCANCODE_N]) keys |= KEY_F1; - if (state[SDL_SCANCODE_M]) keys |= KEY_MONI; - if (state[SDL_SCANCODE_PAGEUP]) keys |= KNOB_LEFT; - if (state[SDL_SCANCODE_PAGEDOWN]) keys |= KNOB_RIGHT; return keys; } diff --git a/platform/targets/linux/emulator/emulator.c b/platform/targets/linux/emulator/emulator.c index 65d5cc22..4d5f64d3 100644 --- a/platform/targets/linux/emulator/emulator.c +++ b/platform/targets/linux/emulator/emulator.c @@ -32,7 +32,7 @@ #include -radio_state Radio_State = {12, 8.2f, 3, 4, 1, false}; +radio_state Radio_State = {12, 8.2f, 3, 4, 1, false, false}; extern int screenshot_display(const char *filename); @@ -525,7 +525,7 @@ void *startCLIMenu() while(ret == SH_CONTINUE); fflush(stdout); write_history(histfile); - exit(0); + Radio_State.PowerOff = true; } @@ -539,3 +539,105 @@ void emulator_start() printf("An error occurred starting the emulator thread: %d\n", err); } } + +keyboard_t sdl_keys; +keyboard_t sdl_getKeys() { return sdl_keys; } + +bool sdk_key_code_to_key(SDL_KeyCode sym, keyboard_t *key) +{ + switch (sym) { + case SDLK_0: + *key = KEY_0; + return true; + case SDLK_1: + *key = KEY_1; + return true; + case SDLK_2: + *key = KEY_2; + return true; + case SDLK_3: + *key = KEY_3; + return true; + case SDLK_4: + *key = KEY_4; + return true; + case SDLK_5: + *key = KEY_5; + return true; + case SDLK_6: + *key = KEY_6; + return true; + case SDLK_7: + *key = KEY_7; + return true; + case SDLK_8: + *key = KEY_8; + return true; + case SDLK_9: + *key = KEY_9; + return true; + case SDLK_ASTERISK: + *key = KEY_STAR; + return true; + case SDLK_ESCAPE: + *key = KEY_ESC; + return true; + case SDLK_LEFT: + *key = KEY_LEFT; + return true; + case SDLK_RIGHT: + *key = KEY_RIGHT; + return true; + case SDLK_RETURN: + *key = KEY_ENTER; + return true; + case SDLK_HASH: + *key = KEY_HASH; + return true; + case SDLK_n: + *key = KEY_F1; + return true; + case SDLK_m: + *key = KEY_MONI; + return true; + case SDLK_PAGEUP: + *key = KNOB_LEFT; + return true; + case SDLK_PAGEDOWN: + *key = KNOB_RIGHT; + return true; + case SDLK_UP: + *key = KEY_UP; + return true; + case SDLK_DOWN: + *key = KEY_DOWN; + return true; + default: + return false; + } +} + +void emulator_process_sdl_events() +{ + Uint32 now = SDL_GetTicks(); + + SDL_Event ev = { 0 }; + keyboard_t key = 0; + + SDL_PollEvent( &ev); + + switch (ev.type) { + case SDL_QUIT: + Radio_State.PowerOff = true; + break; + case SDL_KEYDOWN: + if (sdk_key_code_to_key(ev.key.keysym.sym, &key)) + sdl_keys |= key; + break; + case SDL_KEYUP: + if (sdk_key_code_to_key(ev.key.keysym.sym, &key)) + sdl_keys ^= key; + + break; + } +} diff --git a/platform/targets/linux/emulator/emulator.h b/platform/targets/linux/emulator/emulator.h index f2c209c8..e0f129ae 100644 --- a/platform/targets/linux/emulator/emulator.h +++ b/platform/targets/linux/emulator/emulator.h @@ -52,10 +52,12 @@ typedef struct float volumeLevel; float chSelector; bool PttStatus; + bool PowerOff; } radio_state; extern radio_state Radio_State; void emulator_start(); +void emulator_process_sdl_events(); #endif /* EMULATOR_H */ diff --git a/platform/targets/linux/platform.c b/platform/targets/linux/platform.c index 06ac8896..22e8dbc9 100644 --- a/platform/targets/linux/platform.c +++ b/platform/targets/linux/platform.c @@ -16,6 +16,7 @@ ***************************************************************************/ #include +#include #include #include #include "emulator.h" @@ -43,6 +44,9 @@ void platform_init() void platform_terminate() { printf("Platform terminate\n"); + gfx_terminate(); + + exit(0); } void platform_setBacklightLevel(__attribute__((unused)) uint8_t level) @@ -89,7 +93,6 @@ int8_t platform_getChSelector() bool platform_getPttStatus() { // Read P key status from SDL - SDL_PumpEvents(); const uint8_t *state = SDL_GetKeyboardState(NULL); if (state[SDL_SCANCODE_P]) return true; @@ -100,7 +103,7 @@ bool platform_getPttStatus() bool platform_pwrButtonStatus() { /* Suppose radio is always on */ - return true; + return !Radio_State.PowerOff; } void platform_ledOn(__attribute__((unused)) led_t led)