Fix crash on --no-control

Relative mouse mode assumed that a mouse processor was always available,
but this is not the case if --no-control is passed.
This commit is contained in:
Romain Vimont 2022-01-23 11:34:00 +01:00
parent 81ff7ebd06
commit 0ec3361bc9

View file

@ -773,6 +773,9 @@ sc_screen_is_mouse_capture_key(SDL_Keycode key) {
void void
sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) { sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
// screen->im.mp may be NULL if --no-control
bool relative_mode = screen->im.mp && screen->im.mp->relative_mode;
switch (event->type) { switch (event->type) {
case EVENT_NEW_FRAME: { case EVENT_NEW_FRAME: {
bool ok = sc_screen_update_frame(screen); bool ok = sc_screen_update_frame(screen);
@ -810,14 +813,14 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
sc_screen_render(screen, true); sc_screen_render(screen, true);
break; break;
case SDL_WINDOWEVENT_FOCUS_LOST: case SDL_WINDOWEVENT_FOCUS_LOST:
if (screen->im.mp->relative_mode) { if (relative_mode) {
sc_screen_capture_mouse(screen, false); sc_screen_capture_mouse(screen, false);
} }
break; break;
} }
return; return;
case SDL_KEYDOWN: case SDL_KEYDOWN:
if (screen->im.mp->relative_mode) { if (relative_mode) {
SDL_Keycode key = event->key.keysym.sym; SDL_Keycode key = event->key.keysym.sym;
if (sc_screen_is_mouse_capture_key(key)) { if (sc_screen_is_mouse_capture_key(key)) {
if (!screen->mouse_capture_key_pressed) { if (!screen->mouse_capture_key_pressed) {
@ -834,7 +837,7 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
} }
break; break;
case SDL_KEYUP: case SDL_KEYUP:
if (screen->im.mp->relative_mode) { if (relative_mode) {
SDL_Keycode key = event->key.keysym.sym; SDL_Keycode key = event->key.keysym.sym;
SDL_Keycode cap = screen->mouse_capture_key_pressed; SDL_Keycode cap = screen->mouse_capture_key_pressed;
screen->mouse_capture_key_pressed = 0; screen->mouse_capture_key_pressed = 0;
@ -851,7 +854,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 (screen->im.mp->relative_mode && !screen->mouse_captured) { if (relative_mode && !screen->mouse_captured) {
// 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;
@ -860,14 +863,14 @@ sc_screen_handle_event(struct sc_screen *screen, SDL_Event *event) {
case SDL_FINGERMOTION: case SDL_FINGERMOTION:
case SDL_FINGERDOWN: case SDL_FINGERDOWN:
case SDL_FINGERUP: case SDL_FINGERUP:
if (screen->im.mp->relative_mode) { if (relative_mode) {
// Touch events are not compatible with relative mode // Touch events are not compatible with relative mode
// (coordinates are not relative) // (coordinates are not relative)
return; return;
} }
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
if (screen->im.mp->relative_mode && !screen->mouse_captured) { if (relative_mode && !screen->mouse_captured) {
sc_screen_capture_mouse(screen, true); sc_screen_capture_mouse(screen, true);
return; return;
} }