sdl: improve window resizing on macOS

This enables smooth resizing of the window using a
fixed aspect ratio, instead of snapping into the
correct aspect ratio only when the resize operation
has finished, by using an SDL event filter that gets
events delivered during the resize operation
(whereas SDL_PollEvent blocks until done on macOS).

Change-Id: Ie6614e4b6f49a24469c5ee6a69721c9fbd440dae
This commit is contained in:
Christian Soffke 2024-12-29 15:29:29 +01:00
parent 8ce9d9e39e
commit 9ba59477a1
5 changed files with 14 additions and 8 deletions

View file

@ -23,7 +23,7 @@
#include "kernel.h" #include "kernel.h"
#include "button.h" #include "button.h"
#ifdef HAVE_SDL #ifdef HAVE_SDL
#include "button-sdl.h" #include "SDL.h"
#include "window-sdl.h" #include "window-sdl.h"
#endif #endif
@ -101,7 +101,7 @@ static inline void button_queue_wait(struct queue_event *evp, int timeout)
unsigned long curr_tick, remaining; unsigned long curr_tick, remaining;
while(true) while(true)
{ {
handle_sdl_events(); /* Includes window updates after resize events */ SDL_PumpEvents();
queue_wait_w_tmo(&button_queue, evp, TIMEOUT_NOBLOCK); queue_wait_w_tmo(&button_queue, evp, TIMEOUT_NOBLOCK);
if (evp->id != SYS_TIMEOUT || timeout == TIMEOUT_NOBLOCK) if (evp->id != SYS_TIMEOUT || timeout == TIMEOUT_NOBLOCK)
return; return;

View file

@ -382,11 +382,11 @@ static bool event_handler(SDL_Event *event)
} }
#ifdef __APPLE__ #ifdef __APPLE__
void handle_sdl_events(void) int sdl_event_filter(void *userdata, SDL_Event * event)
{ {
SDL_Event event; (void) userdata;
while(SDL_PollEvent(&event)) event_handler(event);
event_handler(&event); return 0;
} }
#endif #endif

View file

@ -25,11 +25,12 @@
#include <stdbool.h> #include <stdbool.h>
#include "config.h" #include "config.h"
#include "SDL.h"
extern int sdl_app_has_input_focus; extern int sdl_app_has_input_focus;
#ifdef __APPLE__ #ifdef __APPLE__
void handle_sdl_events(void); int sdl_event_filter(void *userdata, SDL_Event * event);
#endif #endif
bool button_hold(void); bool button_hold(void);

View file

@ -33,6 +33,7 @@
#include "system-sdl.h" #include "system-sdl.h"
#include "sim-ui-defines.h" #include "sim-ui-defines.h"
#include "window-sdl.h" #include "window-sdl.h"
#include "button-sdl.h"
#include "lcd-bitmap.h" #include "lcd-bitmap.h"
#ifdef HAVE_REMOTE_LCD #ifdef HAVE_REMOTE_LCD
#include "lcd-remote-bitmap.h" #include "lcd-remote-bitmap.h"
@ -235,6 +236,8 @@ void system_init(void)
SDL_SemWait(s); SDL_SemWait(s);
/* cleanup */ /* cleanup */
SDL_DestroySemaphore(s); SDL_DestroySemaphore(s);
#else
SDL_AddEventWatch(sdl_event_filter, NULL);
#endif #endif
} }

View file

@ -81,8 +81,10 @@ static void restore_aspect_ratio(int w, int h)
SDL_GetWindowSize(sdlWindow, &w, &h); SDL_GetWindowSize(sdlWindow, &w, &h);
if (w != original_width || h != original_height) if (w != original_width || h != original_height)
{ {
SDL_DisplayMode sdl_dm;
h = w * aspect_ratio; h = w * aspect_ratio;
SDL_SetWindowSize(sdlWindow, w, h); if (SDL_GetCurrentDisplayMode(0, &sdl_dm) || h <= sdl_dm.h)
SDL_SetWindowSize(sdlWindow, w, h);
} }
} }
#endif #endif