video: Remove SDL_GetFocusWindow()

It was rarely used and performed an identical function to SDL_GetKeyboardFocus(), but with worse time complexity.
This commit is contained in:
Frank Praznik 2023-09-16 15:36:01 -04:00
parent 39c2f97373
commit e5739d7d1f
9 changed files with 27 additions and 37 deletions

View file

@ -211,7 +211,7 @@ static SDL_AssertState SDLCALL SDL_PromptAssertion(const SDL_AssertData *data, v
} }
/* Leave fullscreen mode, if possible (scary!) */ /* Leave fullscreen mode, if possible (scary!) */
window = SDL_GetFocusWindow(); window = SDL_GetToplevelForKeyboardFocus();
if (window) { if (window) {
if (window->fullscreen_exclusive) { if (window->fullscreen_exclusive) {
SDL_MinimizeWindow(window); SDL_MinimizeWindow(window);

View file

@ -544,7 +544,7 @@ extern void SDL_OnWindowFocusGained(SDL_Window *window);
extern void SDL_OnWindowFocusLost(SDL_Window *window); extern void SDL_OnWindowFocusLost(SDL_Window *window);
extern void SDL_OnWindowDisplayChanged(SDL_Window *window); extern void SDL_OnWindowDisplayChanged(SDL_Window *window);
extern void SDL_UpdateWindowGrab(SDL_Window *window); extern void SDL_UpdateWindowGrab(SDL_Window *window);
extern SDL_Window *SDL_GetFocusWindow(void); extern SDL_Window *SDL_GetToplevelForKeyboardFocus();
extern SDL_bool SDL_ShouldAllowTopmost(void); extern SDL_bool SDL_ShouldAllowTopmost(void);

View file

@ -3561,21 +3561,18 @@ void SDL_OnWindowFocusLost(SDL_Window *window)
} }
} }
/* !!! FIXME: is this different than SDL_GetKeyboardFocus()? SDL_Window *SDL_GetToplevelForKeyboardFocus()
!!! FIXME: Also, SDL_GetKeyboardFocus() is O(1), this isn't. */
SDL_Window *SDL_GetFocusWindow(void)
{ {
SDL_Window *window; SDL_Window *focus = SDL_GetKeyboardFocus();
if (_this == NULL) { if (focus) {
return NULL; /* Get the toplevel parent window. */
} while (focus->parent) {
for (window = _this->windows; window; window = window->next) { focus = focus->parent;
if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
return window;
} }
} }
return NULL;
return focus;
} }
void SDL_DestroyWindow(SDL_Window *window) void SDL_DestroyWindow(SDL_Window *window)
@ -4759,7 +4756,7 @@ void SDL_StartTextInput(void)
/* Then show the on-screen keyboard, if any */ /* Then show the on-screen keyboard, if any */
if (SDL_GetHintBoolean(SDL_HINT_ENABLE_SCREEN_KEYBOARD, SDL_TRUE)) { if (SDL_GetHintBoolean(SDL_HINT_ENABLE_SCREEN_KEYBOARD, SDL_TRUE)) {
window = SDL_GetFocusWindow(); window = SDL_GetKeyboardFocus();
if (window && _this && _this->ShowScreenKeyboard) { if (window && _this && _this->ShowScreenKeyboard) {
_this->ShowScreenKeyboard(_this, window); _this->ShowScreenKeyboard(_this, window);
} }
@ -4803,7 +4800,7 @@ void SDL_StopTextInput(void)
/* Hide the on-screen keyboard, if any */ /* Hide the on-screen keyboard, if any */
if (SDL_GetHintBoolean(SDL_HINT_ENABLE_SCREEN_KEYBOARD, SDL_TRUE)) { if (SDL_GetHintBoolean(SDL_HINT_ENABLE_SCREEN_KEYBOARD, SDL_TRUE)) {
window = SDL_GetFocusWindow(); window = SDL_GetKeyboardFocus();
if (window && _this && _this->HideScreenKeyboard) { if (window && _this && _this->HideScreenKeyboard) {
_this->HideScreenKeyboard(_this, window); _this->HideScreenKeyboard(_this, window);
} }
@ -5119,9 +5116,9 @@ void SDL_OnApplicationWillResignActive(void)
if (_this) { if (_this) {
SDL_Window *window; SDL_Window *window;
for (window = _this->windows; window != NULL; window = window->next) { for (window = _this->windows; window != NULL; window = window->next) {
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_FOCUS_LOST, 0, 0);
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MINIMIZED, 0, 0); SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_MINIMIZED, 0, 0);
} }
SDL_SetKeyboardFocus(NULL);
} }
SDL_SendAppEvent(SDL_EVENT_WILL_ENTER_BACKGROUND); SDL_SendAppEvent(SDL_EVENT_WILL_ENTER_BACKGROUND);
} }
@ -5143,7 +5140,7 @@ void SDL_OnApplicationDidBecomeActive(void)
if (_this) { if (_this) {
SDL_Window *window; SDL_Window *window;
for (window = _this->windows; window != NULL; window = window->next) { for (window = _this->windows; window != NULL; window = window->next) {
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_FOCUS_GAINED, 0, 0); SDL_SetKeyboardFocus(window);
SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESTORED, 0, 0); SDL_SendWindowEvent(window, SDL_EVENT_WINDOW_RESTORED, 0, 0);
} }
} }

View file

@ -732,7 +732,7 @@ static EM_BOOL Emscripten_HandleFocus(int eventType, const EmscriptenFocusEvent
} }
sdl_event_type = (eventType == EMSCRIPTEN_EVENT_FOCUS) ? SDL_EVENT_WINDOW_FOCUS_GAINED : SDL_EVENT_WINDOW_FOCUS_LOST; sdl_event_type = (eventType == EMSCRIPTEN_EVENT_FOCUS) ? SDL_EVENT_WINDOW_FOCUS_GAINED : SDL_EVENT_WINDOW_FOCUS_LOST;
SDL_SendWindowEvent(window_data->window, sdl_event_type, 0, 0); SDL_SetKeyboardFocus(sdl_event_type == SDL_EVENT_WINDOW_FOCUS_GAINED ? window_data->window : NULL);
return SDL_EventEnabled(sdl_event_type); return SDL_EventEnabled(sdl_event_type);
} }

View file

@ -249,7 +249,7 @@ void UIKit_ForceUpdateHomeIndicator(void)
{ {
#if !TARGET_OS_TV #if !TARGET_OS_TV
/* Force the main SDL window to re-evaluate home indicator state */ /* Force the main SDL window to re-evaluate home indicator state */
SDL_Window *focus = SDL_GetFocusWindow(); SDL_Window *focus = SDL_GetKeyboardFocus();
if (focus) { if (focus) {
SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)focus->driverdata; SDL_UIKitWindowData *data = (__bridge SDL_UIKitWindowData *)focus->driverdata;
if (data != nil) { if (data != nil) {

View file

@ -624,7 +624,7 @@ SDL_bool UIKit_IsScreenKeyboardShown(SDL_VideoDevice *_this, SDL_Window *window)
int UIKit_SetTextInputRect(SDL_VideoDevice *_this, const SDL_Rect *rect) int UIKit_SetTextInputRect(SDL_VideoDevice *_this, const SDL_Rect *rect)
{ {
@autoreleasepool { @autoreleasepool {
SDL_uikitviewcontroller *vc = GetWindowViewController(SDL_GetFocusWindow()); SDL_uikitviewcontroller *vc = GetWindowViewController(SDL_GetKeyboardFocus());
if (vc != nil) { if (vc != nil) {
vc.textInputRect = *rect; vc.textInputRect = *rect;

View file

@ -916,7 +916,7 @@ static int Wayland_GetDisplayBounds(SDL_VideoDevice *_this, SDL_VideoDisplay *di
/* When an emulated, exclusive fullscreen window has focus, treat the mode dimensions as the display bounds. */ /* When an emulated, exclusive fullscreen window has focus, treat the mode dimensions as the display bounds. */
if (display->fullscreen_window && if (display->fullscreen_window &&
display->fullscreen_window->fullscreen_exclusive && display->fullscreen_window->fullscreen_exclusive &&
display->fullscreen_window == SDL_GetFocusWindow() && display->fullscreen_window->driverdata->active &&
display->fullscreen_window->current_fullscreen_mode.w != 0 && display->fullscreen_window->current_fullscreen_mode.w != 0 &&
display->fullscreen_window->current_fullscreen_mode.h != 0) { display->fullscreen_window->current_fullscreen_mode.h != 0) {
rect->w = display->fullscreen_window->current_fullscreen_mode.w; rect->w = display->fullscreen_window->current_fullscreen_mode.w;

View file

@ -639,7 +639,7 @@ static void handle_configure_xdg_toplevel(void *data,
SDL_bool fullscreen = SDL_FALSE; SDL_bool fullscreen = SDL_FALSE;
SDL_bool maximized = SDL_FALSE; SDL_bool maximized = SDL_FALSE;
SDL_bool floating = SDL_TRUE; SDL_bool floating = SDL_TRUE;
SDL_bool focused = SDL_FALSE; SDL_bool active = SDL_FALSE;
SDL_bool suspended = SDL_FALSE; SDL_bool suspended = SDL_FALSE;
wl_array_for_each (state, states) { wl_array_for_each (state, states) {
switch (*state) { switch (*state) {
@ -652,7 +652,7 @@ static void handle_configure_xdg_toplevel(void *data,
floating = SDL_FALSE; floating = SDL_FALSE;
break; break;
case XDG_TOPLEVEL_STATE_ACTIVATED: case XDG_TOPLEVEL_STATE_ACTIVATED:
focused = SDL_TRUE; active = SDL_TRUE;
break; break;
case XDG_TOPLEVEL_STATE_TILED_LEFT: case XDG_TOPLEVEL_STATE_TILED_LEFT:
case XDG_TOPLEVEL_STATE_TILED_RIGHT: case XDG_TOPLEVEL_STATE_TILED_RIGHT:
@ -715,7 +715,7 @@ static void handle_configure_xdg_toplevel(void *data,
* dependent, but in general, we can assume that the flag should remain set until * dependent, but in general, we can assume that the flag should remain set until
* the next focused configure event occurs. * the next focused configure event occurs.
*/ */
if (focused || !(window->flags & SDL_WINDOW_MINIMIZED)) { if (active || !(window->flags & SDL_WINDOW_MINIMIZED)) {
SDL_SendWindowEvent(window, SDL_SendWindowEvent(window,
maximized ? SDL_EVENT_WINDOW_MAXIMIZED : SDL_EVENT_WINDOW_RESTORED, maximized ? SDL_EVENT_WINDOW_MAXIMIZED : SDL_EVENT_WINDOW_RESTORED,
0, 0); 0, 0);
@ -745,15 +745,11 @@ static void handle_configure_xdg_toplevel(void *data,
} }
} }
/* Similar to maximized/restore events above, send focus events too! */
SDL_SendWindowEvent(window,
focused ? SDL_EVENT_WINDOW_FOCUS_GAINED : SDL_EVENT_WINDOW_FOCUS_LOST,
0, 0);
wind->requested_window_width = width; wind->requested_window_width = width;
wind->requested_window_height = height; wind->requested_window_height = height;
wind->floating = floating; wind->floating = floating;
wind->suspended = suspended; wind->suspended = suspended;
wind->active = active;
if (wind->surface_status == WAYLAND_SURFACE_STATUS_WAITING_FOR_CONFIGURE) { if (wind->surface_status == WAYLAND_SURFACE_STATUS_WAITING_FOR_CONFIGURE) {
wind->surface_status = WAYLAND_SURFACE_STATUS_WAITING_FOR_FRAME; wind->surface_status = WAYLAND_SURFACE_STATUS_WAITING_FOR_FRAME;
} }
@ -920,7 +916,7 @@ static void decoration_frame_configure(struct libdecor_frame *frame,
int width, height; int width, height;
SDL_bool prev_fullscreen = wind->is_fullscreen; SDL_bool prev_fullscreen = wind->is_fullscreen;
SDL_bool focused = SDL_FALSE; SDL_bool active = SDL_FALSE;
SDL_bool fullscreen = SDL_FALSE; SDL_bool fullscreen = SDL_FALSE;
SDL_bool maximized = SDL_FALSE; SDL_bool maximized = SDL_FALSE;
SDL_bool tiled = SDL_FALSE; SDL_bool tiled = SDL_FALSE;
@ -934,7 +930,7 @@ static void decoration_frame_configure(struct libdecor_frame *frame,
if (libdecor_configuration_get_window_state(configuration, &window_state)) { if (libdecor_configuration_get_window_state(configuration, &window_state)) {
fullscreen = (window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN) != 0; fullscreen = (window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN) != 0;
maximized = (window_state & LIBDECOR_WINDOW_STATE_MAXIMIZED) != 0; maximized = (window_state & LIBDECOR_WINDOW_STATE_MAXIMIZED) != 0;
focused = (window_state & LIBDECOR_WINDOW_STATE_ACTIVE) != 0; active = (window_state & LIBDECOR_WINDOW_STATE_ACTIVE) != 0;
tiled = (window_state & tiled_states) != 0; tiled = (window_state & tiled_states) != 0;
#ifdef SDL_HAVE_LIBDECOR_VER_0_1_2 #ifdef SDL_HAVE_LIBDECOR_VER_0_1_2
suspended = (window_state & LIBDECOR_WINDOW_STATE_SUSPENDED) != 0; suspended = (window_state & LIBDECOR_WINDOW_STATE_SUSPENDED) != 0;
@ -953,18 +949,13 @@ static void decoration_frame_configure(struct libdecor_frame *frame,
* dependent, but in general, we can assume that the flag should remain set until * dependent, but in general, we can assume that the flag should remain set until
* the next focused configure event occurs. * the next focused configure event occurs.
*/ */
if (focused || !(window->flags & SDL_WINDOW_MINIMIZED)) { if (active || !(window->flags & SDL_WINDOW_MINIMIZED)) {
SDL_SendWindowEvent(window, SDL_SendWindowEvent(window,
maximized ? SDL_EVENT_WINDOW_MAXIMIZED : SDL_EVENT_WINDOW_RESTORED, maximized ? SDL_EVENT_WINDOW_MAXIMIZED : SDL_EVENT_WINDOW_RESTORED,
0, 0); 0, 0);
} }
} }
/* Similar to maximized/restore events above, send focus events too! */
SDL_SendWindowEvent(window,
focused ? SDL_EVENT_WINDOW_FOCUS_GAINED : SDL_EVENT_WINDOW_FOCUS_LOST,
0, 0);
/* For fullscreen or fixed-size windows we know our size. /* For fullscreen or fixed-size windows we know our size.
* Always assume the configure is wrong. * Always assume the configure is wrong.
*/ */
@ -1054,6 +1045,7 @@ static void decoration_frame_configure(struct libdecor_frame *frame,
/* Store the new state. */ /* Store the new state. */
wind->floating = floating; wind->floating = floating;
wind->suspended = suspended; wind->suspended = suspended;
wind->active = active;
/* Calculate the new window geometry */ /* Calculate the new window geometry */
wind->requested_window_width = width; wind->requested_window_width = width;

View file

@ -123,6 +123,7 @@ struct SDL_WindowData
SDL_DisplayID last_displayID; SDL_DisplayID last_displayID;
SDL_bool floating; SDL_bool floating;
SDL_bool suspended; SDL_bool suspended;
SDL_bool active;
SDL_bool is_fullscreen; SDL_bool is_fullscreen;
SDL_bool in_fullscreen_transition; SDL_bool in_fullscreen_transition;
SDL_bool fullscreen_was_positioned; SDL_bool fullscreen_was_positioned;