From 7848a387c8281cb156f0e0da6bbbb05cda31db22 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 9 Feb 2022 21:06:16 +0100 Subject: [PATCH] Do not duplicate relative mouse mode state The relative mouse mode is tracked by SDL, and accessible via SDL_GetRelativeMouseMode(). This is more robust in case SDL changes the relative mouse mode on its own. --- app/src/screen.c | 27 ++++++++++++++++----------- app/src/screen.h | 1 - app/src/usb/screen_otg.c | 32 ++++++++++++++++++-------------- app/src/usb/screen_otg.h | 1 - 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/app/src/screen.c b/app/src/screen.c index 98626909..9e9ff3ff 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -163,14 +163,21 @@ sc_screen_is_relative_mode(struct sc_screen *screen) { } static void -sc_screen_capture_mouse(struct sc_screen *screen, bool capture) { +sc_screen_set_mouse_capture(bool capture) { if (SDL_SetRelativeMouseMode(capture)) { LOGE("Could not set relative mouse mode to %s: %s", capture ? "true" : "false", SDL_GetError()); - return; } +} - screen->mouse_captured = capture; +static inline bool +sc_screen_get_mouse_capture(void) { + return SDL_GetRelativeMouseMode(); +} + +static inline void +sc_screen_toggle_mouse_capture(void) { + sc_screen_set_mouse_capture(!sc_screen_get_mouse_capture()); } static void @@ -372,7 +379,6 @@ sc_screen_init(struct sc_screen *screen, screen->fullscreen = false; screen->maximized = false; screen->event_failed = false; - screen->mouse_captured = false; screen->mouse_capture_key_pressed = 0; screen->req.x = params->window_x; @@ -710,7 +716,7 @@ sc_screen_update_frame(struct sc_screen *screen) { if (sc_screen_is_relative_mode(screen)) { // Capture mouse on start - sc_screen_capture_mouse(screen, true); + sc_screen_set_mouse_capture(true); } } @@ -823,7 +829,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) { break; case SDL_WINDOWEVENT_FOCUS_LOST: if (relative_mode) { - sc_screen_capture_mouse(screen, false); + sc_screen_set_mouse_capture(false); } break; } @@ -853,8 +859,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) { if (key == cap) { // A mouse capture key has been pressed then released: // toggle the capture mouse mode - sc_screen_capture_mouse(screen, - !screen->mouse_captured); + sc_screen_toggle_mouse_capture(); } // Mouse capture keys are never forwarded to the device return; @@ -864,7 +869,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) { case SDL_MOUSEWHEEL: case SDL_MOUSEMOTION: case SDL_MOUSEBUTTONDOWN: - if (relative_mode && !screen->mouse_captured) { + if (relative_mode && !sc_screen_get_mouse_capture()) { // Do not forward to input manager, the mouse will be captured // on SDL_MOUSEBUTTONUP return; @@ -880,8 +885,8 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) { } break; case SDL_MOUSEBUTTONUP: - if (relative_mode && !screen->mouse_captured) { - sc_screen_capture_mouse(screen, true); + if (relative_mode && !sc_screen_get_mouse_capture()) { + sc_screen_set_mouse_capture(true); return; } break; diff --git a/app/src/screen.h b/app/src/screen.h index 13bd4d99..0ddefc43 100644 --- a/app/src/screen.h +++ b/app/src/screen.h @@ -60,7 +60,6 @@ struct sc_screen { bool event_failed; // in case SDL_PushEvent() returned an error - bool mouse_captured; // only relevant in relative mouse mode // To enable/disable mouse capture, a mouse capture key (LALT, LGUI or // RGUI) must be pressed. This variable tracks the pressed capture key. SDL_Keycode mouse_capture_key_pressed; diff --git a/app/src/usb/screen_otg.c b/app/src/usb/screen_otg.c index cda0da5e..f32bc946 100644 --- a/app/src/usb/screen_otg.c +++ b/app/src/usb/screen_otg.c @@ -5,15 +5,21 @@ #include "util/log.h" static void -sc_screen_otg_capture_mouse(struct sc_screen_otg *screen, bool capture) { - assert(screen->mouse); +sc_screen_otg_set_mouse_capture(bool capture) { if (SDL_SetRelativeMouseMode(capture)) { LOGE("Could not set relative mouse mode to %s: %s", capture ? "true" : "false", SDL_GetError()); - return; } +} - screen->mouse_captured = capture; +static inline bool +sc_screen_otg_get_mouse_capture(void) { + return SDL_GetRelativeMouseMode(); +} + +static inline void +sc_screen_otg_toggle_mouse_capture(void) { + sc_screen_otg_set_mouse_capture(!sc_screen_otg_get_mouse_capture()); } static void @@ -31,7 +37,6 @@ sc_screen_otg_init(struct sc_screen_otg *screen, screen->keyboard = params->keyboard; screen->mouse = params->mouse; - screen->mouse_captured = false; screen->mouse_capture_key_pressed = 0; const char *title = params->window_title; @@ -81,7 +86,7 @@ sc_screen_otg_init(struct sc_screen_otg *screen, if (screen->mouse) { // Capture mouse on start - sc_screen_otg_capture_mouse(screen, true); + sc_screen_otg_set_mouse_capture(true); } return true; @@ -193,7 +198,7 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) { break; case SDL_WINDOWEVENT_FOCUS_LOST: if (screen->mouse) { - sc_screen_otg_capture_mouse(screen, false); + sc_screen_otg_set_mouse_capture(false); } break; } @@ -227,8 +232,7 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) { if (key == cap) { // A mouse capture key has been pressed then released: // toggle the capture mouse mode - sc_screen_otg_capture_mouse(screen, - !screen->mouse_captured); + sc_screen_otg_toggle_mouse_capture(); } // Mouse capture keys are never forwarded to the device return; @@ -240,26 +244,26 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) { } break; case SDL_MOUSEMOTION: - if (screen->mouse && screen->mouse_captured) { + if (screen->mouse && sc_screen_otg_get_mouse_capture()) { sc_screen_otg_process_mouse_motion(screen, &event->motion); } break; case SDL_MOUSEBUTTONDOWN: - if (screen->mouse && screen->mouse_captured) { + if (screen->mouse && sc_screen_otg_get_mouse_capture()) { sc_screen_otg_process_mouse_button(screen, &event->button); } break; case SDL_MOUSEBUTTONUP: if (screen->mouse) { - if (screen->mouse_captured) { + if (sc_screen_otg_get_mouse_capture()) { sc_screen_otg_process_mouse_button(screen, &event->button); } else { - sc_screen_otg_capture_mouse(screen, true); + sc_screen_otg_set_mouse_capture(true); } } break; case SDL_MOUSEWHEEL: - if (screen->mouse && screen->mouse_captured) { + if (screen->mouse && sc_screen_otg_get_mouse_capture()) { sc_screen_otg_process_mouse_wheel(screen, &event->wheel); } break; diff --git a/app/src/usb/screen_otg.h b/app/src/usb/screen_otg.h index 3fa1c4ad..0973ce59 100644 --- a/app/src/usb/screen_otg.h +++ b/app/src/usb/screen_otg.h @@ -18,7 +18,6 @@ struct sc_screen_otg { SDL_Texture *texture; // See equivalent mechanism in screen.h - bool mouse_captured; SDL_Keycode mouse_capture_key_pressed; };