From 9db42341e4934989921099c60e307c847eb625b6 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 17 Feb 2022 21:37:14 +0100 Subject: [PATCH] Pass screen instance to mouse capture functions Using the screen instance or not in these functions is an implementation detail. Further changes will require the screen instance. Refs 7848a387c8281cb156f0e0da6bbbb05cda31db22 PR #3031 --- app/src/screen.c | 24 ++++++++++++++---------- app/src/usb/screen_otg.c | 28 ++++++++++++++++------------ 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/app/src/screen.c b/app/src/screen.c index c233cf6e..7ca9bfb7 100644 --- a/app/src/screen.c +++ b/app/src/screen.c @@ -163,7 +163,8 @@ sc_screen_is_relative_mode(struct sc_screen *screen) { } static void -sc_screen_set_mouse_capture(bool capture) { +sc_screen_set_mouse_capture(struct sc_screen *screen, bool capture) { + (void) screen; if (SDL_SetRelativeMouseMode(capture)) { LOGE("Could not set relative mouse mode to %s: %s", capture ? "true" : "false", SDL_GetError()); @@ -171,13 +172,16 @@ sc_screen_set_mouse_capture(bool capture) { } static inline bool -sc_screen_get_mouse_capture(void) { +sc_screen_get_mouse_capture(struct sc_screen *screen) { + (void) screen; return SDL_GetRelativeMouseMode(); } static inline void -sc_screen_toggle_mouse_capture(void) { - sc_screen_set_mouse_capture(!sc_screen_get_mouse_capture()); +sc_screen_toggle_mouse_capture(struct sc_screen *screen) { + (void) screen; + bool new_value = !sc_screen_get_mouse_capture(screen); + sc_screen_set_mouse_capture(screen, new_value); } static void @@ -721,7 +725,7 @@ sc_screen_update_frame(struct sc_screen *screen) { if (sc_screen_is_relative_mode(screen)) { // Capture mouse on start - sc_screen_set_mouse_capture(true); + sc_screen_set_mouse_capture(screen, true); } } @@ -834,7 +838,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) { break; case SDL_WINDOWEVENT_FOCUS_LOST: if (relative_mode) { - sc_screen_set_mouse_capture(false); + sc_screen_set_mouse_capture(screen, false); } break; } @@ -864,7 +868,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_toggle_mouse_capture(); + sc_screen_toggle_mouse_capture(screen); } // Mouse capture keys are never forwarded to the device return; @@ -874,7 +878,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) { case SDL_MOUSEWHEEL: case SDL_MOUSEMOTION: case SDL_MOUSEBUTTONDOWN: - if (relative_mode && !sc_screen_get_mouse_capture()) { + if (relative_mode && !sc_screen_get_mouse_capture(screen)) { // Do not forward to input manager, the mouse will be captured // on SDL_MOUSEBUTTONUP return; @@ -890,8 +894,8 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) { } break; case SDL_MOUSEBUTTONUP: - if (relative_mode && !sc_screen_get_mouse_capture()) { - sc_screen_set_mouse_capture(true); + if (relative_mode && !sc_screen_get_mouse_capture(screen)) { + sc_screen_set_mouse_capture(screen, true); return; } break; diff --git a/app/src/usb/screen_otg.c b/app/src/usb/screen_otg.c index f32bc946..96d7eaff 100644 --- a/app/src/usb/screen_otg.c +++ b/app/src/usb/screen_otg.c @@ -5,7 +5,8 @@ #include "util/log.h" static void -sc_screen_otg_set_mouse_capture(bool capture) { +sc_screen_otg_set_mouse_capture(struct sc_screen_otg *screen, bool capture) { + (void) screen; if (SDL_SetRelativeMouseMode(capture)) { LOGE("Could not set relative mouse mode to %s: %s", capture ? "true" : "false", SDL_GetError()); @@ -13,13 +14,16 @@ sc_screen_otg_set_mouse_capture(bool capture) { } static inline bool -sc_screen_otg_get_mouse_capture(void) { +sc_screen_otg_get_mouse_capture(struct sc_screen_otg *screen) { + (void) screen; 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()); +sc_screen_otg_toggle_mouse_capture(struct sc_screen_otg *screen) { + (void) screen; + bool new_value = !sc_screen_otg_get_mouse_capture(screen); + sc_screen_otg_set_mouse_capture(screen, new_value); } static void @@ -86,7 +90,7 @@ sc_screen_otg_init(struct sc_screen_otg *screen, if (screen->mouse) { // Capture mouse on start - sc_screen_otg_set_mouse_capture(true); + sc_screen_otg_set_mouse_capture(screen, true); } return true; @@ -198,7 +202,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_set_mouse_capture(false); + sc_screen_otg_set_mouse_capture(screen, false); } break; } @@ -232,7 +236,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_toggle_mouse_capture(); + sc_screen_otg_toggle_mouse_capture(screen); } // Mouse capture keys are never forwarded to the device return; @@ -244,26 +248,26 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) { } break; case SDL_MOUSEMOTION: - if (screen->mouse && sc_screen_otg_get_mouse_capture()) { + if (screen->mouse && sc_screen_otg_get_mouse_capture(screen)) { sc_screen_otg_process_mouse_motion(screen, &event->motion); } break; case SDL_MOUSEBUTTONDOWN: - if (screen->mouse && sc_screen_otg_get_mouse_capture()) { + if (screen->mouse && sc_screen_otg_get_mouse_capture(screen)) { sc_screen_otg_process_mouse_button(screen, &event->button); } break; case SDL_MOUSEBUTTONUP: if (screen->mouse) { - if (sc_screen_otg_get_mouse_capture()) { + if (sc_screen_otg_get_mouse_capture(screen)) { sc_screen_otg_process_mouse_button(screen, &event->button); } else { - sc_screen_otg_set_mouse_capture(true); + sc_screen_otg_set_mouse_capture(screen, true); } } break; case SDL_MOUSEWHEEL: - if (screen->mouse && sc_screen_otg_get_mouse_capture()) { + if (screen->mouse && sc_screen_otg_get_mouse_capture(screen)) { sc_screen_otg_process_mouse_wheel(screen, &event->wheel); } break;