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.
This commit is contained in:
Romain Vimont 2022-02-09 21:06:16 +01:00
parent 43ae418752
commit 7848a387c8
4 changed files with 34 additions and 27 deletions

View file

@ -163,14 +163,21 @@ sc_screen_is_relative_mode(struct sc_screen *screen) {
} }
static void static void
sc_screen_capture_mouse(struct sc_screen *screen, bool capture) { sc_screen_set_mouse_capture(bool capture) {
if (SDL_SetRelativeMouseMode(capture)) { if (SDL_SetRelativeMouseMode(capture)) {
LOGE("Could not set relative mouse mode to %s: %s", LOGE("Could not set relative mouse mode to %s: %s",
capture ? "true" : "false", SDL_GetError()); 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 static void
@ -372,7 +379,6 @@ sc_screen_init(struct sc_screen *screen,
screen->fullscreen = false; screen->fullscreen = false;
screen->maximized = false; screen->maximized = false;
screen->event_failed = false; screen->event_failed = false;
screen->mouse_captured = false;
screen->mouse_capture_key_pressed = 0; screen->mouse_capture_key_pressed = 0;
screen->req.x = params->window_x; 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)) { if (sc_screen_is_relative_mode(screen)) {
// Capture mouse on start // 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; break;
case SDL_WINDOWEVENT_FOCUS_LOST: case SDL_WINDOWEVENT_FOCUS_LOST:
if (relative_mode) { if (relative_mode) {
sc_screen_capture_mouse(screen, false); sc_screen_set_mouse_capture(false);
} }
break; break;
} }
@ -853,8 +859,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
if (key == cap) { if (key == cap) {
// A mouse capture key has been pressed then released: // A mouse capture key has been pressed then released:
// toggle the capture mouse mode // toggle the capture mouse mode
sc_screen_capture_mouse(screen, sc_screen_toggle_mouse_capture();
!screen->mouse_captured);
} }
// Mouse capture keys are never forwarded to the device // Mouse capture keys are never forwarded to the device
return; return;
@ -864,7 +869,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
case SDL_MOUSEWHEEL: case SDL_MOUSEWHEEL:
case SDL_MOUSEMOTION: case SDL_MOUSEMOTION:
case SDL_MOUSEBUTTONDOWN: 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 // Do not forward to input manager, the mouse will be captured
// on SDL_MOUSEBUTTONUP // on SDL_MOUSEBUTTONUP
return; return;
@ -880,8 +885,8 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
} }
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
if (relative_mode && !screen->mouse_captured) { if (relative_mode && !sc_screen_get_mouse_capture()) {
sc_screen_capture_mouse(screen, true); sc_screen_set_mouse_capture(true);
return; return;
} }
break; break;

View file

@ -60,7 +60,6 @@ struct sc_screen {
bool event_failed; // in case SDL_PushEvent() returned an error 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 // To enable/disable mouse capture, a mouse capture key (LALT, LGUI or
// RGUI) must be pressed. This variable tracks the pressed capture key. // RGUI) must be pressed. This variable tracks the pressed capture key.
SDL_Keycode mouse_capture_key_pressed; SDL_Keycode mouse_capture_key_pressed;

View file

@ -5,15 +5,21 @@
#include "util/log.h" #include "util/log.h"
static void static void
sc_screen_otg_capture_mouse(struct sc_screen_otg *screen, bool capture) { sc_screen_otg_set_mouse_capture(bool capture) {
assert(screen->mouse);
if (SDL_SetRelativeMouseMode(capture)) { if (SDL_SetRelativeMouseMode(capture)) {
LOGE("Could not set relative mouse mode to %s: %s", LOGE("Could not set relative mouse mode to %s: %s",
capture ? "true" : "false", SDL_GetError()); 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 static void
@ -31,7 +37,6 @@ sc_screen_otg_init(struct sc_screen_otg *screen,
screen->keyboard = params->keyboard; screen->keyboard = params->keyboard;
screen->mouse = params->mouse; screen->mouse = params->mouse;
screen->mouse_captured = false;
screen->mouse_capture_key_pressed = 0; screen->mouse_capture_key_pressed = 0;
const char *title = params->window_title; const char *title = params->window_title;
@ -81,7 +86,7 @@ sc_screen_otg_init(struct sc_screen_otg *screen,
if (screen->mouse) { if (screen->mouse) {
// Capture mouse on start // Capture mouse on start
sc_screen_otg_capture_mouse(screen, true); sc_screen_otg_set_mouse_capture(true);
} }
return true; return true;
@ -193,7 +198,7 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) {
break; break;
case SDL_WINDOWEVENT_FOCUS_LOST: case SDL_WINDOWEVENT_FOCUS_LOST:
if (screen->mouse) { if (screen->mouse) {
sc_screen_otg_capture_mouse(screen, false); sc_screen_otg_set_mouse_capture(false);
} }
break; break;
} }
@ -227,8 +232,7 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) {
if (key == cap) { if (key == cap) {
// A mouse capture key has been pressed then released: // A mouse capture key has been pressed then released:
// toggle the capture mouse mode // toggle the capture mouse mode
sc_screen_otg_capture_mouse(screen, sc_screen_otg_toggle_mouse_capture();
!screen->mouse_captured);
} }
// Mouse capture keys are never forwarded to the device // Mouse capture keys are never forwarded to the device
return; return;
@ -240,26 +244,26 @@ sc_screen_otg_handle_event(struct sc_screen_otg *screen, SDL_Event *event) {
} }
break; break;
case SDL_MOUSEMOTION: 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); sc_screen_otg_process_mouse_motion(screen, &event->motion);
} }
break; break;
case SDL_MOUSEBUTTONDOWN: 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); sc_screen_otg_process_mouse_button(screen, &event->button);
} }
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
if (screen->mouse) { if (screen->mouse) {
if (screen->mouse_captured) { if (sc_screen_otg_get_mouse_capture()) {
sc_screen_otg_process_mouse_button(screen, &event->button); sc_screen_otg_process_mouse_button(screen, &event->button);
} else { } else {
sc_screen_otg_capture_mouse(screen, true); sc_screen_otg_set_mouse_capture(true);
} }
} }
break; break;
case SDL_MOUSEWHEEL: 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); sc_screen_otg_process_mouse_wheel(screen, &event->wheel);
} }
break; break;

View file

@ -18,7 +18,6 @@ struct sc_screen_otg {
SDL_Texture *texture; SDL_Texture *texture;
// See equivalent mechanism in screen.h // See equivalent mechanism in screen.h
bool mouse_captured;
SDL_Keycode mouse_capture_key_pressed; SDL_Keycode mouse_capture_key_pressed;
}; };