Browse Source

video: Remove SDL_GetFocusWindow()

It was rarely used and performed an identical function to SDL_GetKeyboardFocus(), but with worse time complexity.
Frank Praznik 1 year ago
parent
commit
e5739d7d1f

+ 1 - 1
src/SDL_assert.c

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

+ 1 - 1
src/video/SDL_sysvideo.h

@@ -544,7 +544,7 @@ extern void SDL_OnWindowFocusGained(SDL_Window *window);
 extern void SDL_OnWindowFocusLost(SDL_Window *window);
 extern void SDL_OnWindowDisplayChanged(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);
 

+ 12 - 15
src/video/SDL_video.c

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

+ 1 - 1
src/video/emscripten/SDL_emscriptenevents.c

@@ -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_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);
 }
 

+ 1 - 1
src/video/uikit/SDL_uikitvideo.m

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

+ 1 - 1
src/video/uikit/SDL_uikitviewcontroller.m

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

+ 1 - 1
src/video/wayland/SDL_waylandvideo.c

@@ -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. */
     if (display->fullscreen_window &&
         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.h != 0) {
         rect->w = display->fullscreen_window->current_fullscreen_mode.w;

+ 8 - 16
src/video/wayland/SDL_waylandwindow.c

@@ -639,7 +639,7 @@ static void handle_configure_xdg_toplevel(void *data,
     SDL_bool fullscreen = SDL_FALSE;
     SDL_bool maximized = SDL_FALSE;
     SDL_bool floating = SDL_TRUE;
-    SDL_bool focused = SDL_FALSE;
+    SDL_bool active = SDL_FALSE;
     SDL_bool suspended = SDL_FALSE;
     wl_array_for_each (state, states) {
         switch (*state) {
@@ -652,7 +652,7 @@ static void handle_configure_xdg_toplevel(void *data,
             floating = SDL_FALSE;
             break;
         case XDG_TOPLEVEL_STATE_ACTIVATED:
-            focused = SDL_TRUE;
+            active = SDL_TRUE;
             break;
         case XDG_TOPLEVEL_STATE_TILED_LEFT:
         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
          * the next focused configure event occurs.
          */
-        if (focused || !(window->flags & SDL_WINDOW_MINIMIZED)) {
+        if (active || !(window->flags & SDL_WINDOW_MINIMIZED)) {
             SDL_SendWindowEvent(window,
                                 maximized ? SDL_EVENT_WINDOW_MAXIMIZED : SDL_EVENT_WINDOW_RESTORED,
                                 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_height = height;
     wind->floating = floating;
     wind->suspended = suspended;
+    wind->active = active;
     if (wind->surface_status == WAYLAND_SURFACE_STATUS_WAITING_FOR_CONFIGURE) {
         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;
 
     SDL_bool prev_fullscreen = wind->is_fullscreen;
-    SDL_bool focused = SDL_FALSE;
+    SDL_bool active = SDL_FALSE;
     SDL_bool fullscreen = SDL_FALSE;
     SDL_bool maximized = 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)) {
         fullscreen = (window_state & LIBDECOR_WINDOW_STATE_FULLSCREEN) != 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;
 #ifdef SDL_HAVE_LIBDECOR_VER_0_1_2
         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
          * the next focused configure event occurs.
          */
-        if (focused || !(window->flags & SDL_WINDOW_MINIMIZED)) {
+        if (active || !(window->flags & SDL_WINDOW_MINIMIZED)) {
             SDL_SendWindowEvent(window,
                                 maximized ? SDL_EVENT_WINDOW_MAXIMIZED : SDL_EVENT_WINDOW_RESTORED,
                                 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.
      * Always assume the configure is wrong.
      */
@@ -1054,6 +1045,7 @@ static void decoration_frame_configure(struct libdecor_frame *frame,
     /* Store the new state. */
     wind->floating = floating;
     wind->suspended = suspended;
+    wind->active = active;
 
     /* Calculate the new window geometry */
     wind->requested_window_width = width;

+ 1 - 0
src/video/wayland/SDL_waylandwindow.h

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