SDL: wayland: Small optimization for output removal function

From 945162c6d90ea8c76ae5dd994413c4ec7b73d6d8 Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Wed, 28 Feb 2024 09:57:54 -0500
Subject: [PATCH] wayland: Small optimization for output removal function

In the case where an output is being removed, the SDL_DisplayData is already known, so no need to retrieve it from the wl_output user data.
---
 src/video/wayland/SDL_waylandvideo.c  |  2 +-
 src/video/wayland/SDL_waylandwindow.c | 21 +++++++++------------
 src/video/wayland/SDL_waylandwindow.h |  2 +-
 3 files changed, 11 insertions(+), 14 deletions(-)

diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index bd7fd44f407d..090458503d9e 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -951,7 +951,7 @@ static void Wayland_free_display(SDL_VideoDisplay *display)
          * so ensure that no window continues to hold a reference to a removed output.
          */
         for (SDL_Window *window = SDL_GetVideoDevice()->windows; window; window = window->next) {
-            Wayland_RemoveOutputFromWindow(window->driverdata, display_data->output);
+            Wayland_RemoveOutputFromWindow(window->driverdata, display_data);
         }
 
         SDL_free(display_data->wl_output_name);
diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index 1aa64b22a301..b4e45398737d 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -1304,16 +1304,15 @@ static void Wayland_move_window(SDL_Window *window, SDL_DisplayData *driverdata)
     }
 }
 
-void Wayland_RemoveOutputFromWindow(SDL_WindowData *window, struct wl_output *output)
+void Wayland_RemoveOutputFromWindow(SDL_WindowData *window, SDL_DisplayData *display_data)
 {
-    int i, send_move_event = 0;
-    SDL_DisplayData *driverdata = wl_output_get_user_data(output);
+    SDL_bool send_move_event = SDL_FALSE;
 
-    for (i = 0; i < window->num_outputs; i++) {
-        if (window->outputs[i] == driverdata) { /* remove this one */
+    for (int i = 0; i < window->num_outputs; i++) {
+        if (window->outputs[i] == display_data) { /* remove this one */
             if (i == (window->num_outputs - 1)) {
                 window->outputs[i] = NULL;
-                send_move_event = 1;
+                send_move_event = SDL_TRUE;
             } else {
                 SDL_memmove(&window->outputs[i],
                             &window->outputs[i + 1],
@@ -1335,8 +1334,7 @@ void Wayland_RemoveOutputFromWindow(SDL_WindowData *window, struct wl_output *ou
     Wayland_MaybeUpdateScaleFactor(window);
 }
 
-static void handle_surface_enter(void *data, struct wl_surface *surface,
-                                 struct wl_output *output)
+static void handle_surface_enter(void *data, struct wl_surface *surface, struct wl_output *output)
 {
     SDL_WindowData *window = data;
     SDL_DisplayData *driverdata = wl_output_get_user_data(output);
@@ -1359,16 +1357,15 @@ static void handle_surface_enter(void *data, struct wl_surface *surface,
     Wayland_MaybeUpdateScaleFactor(window);
 }
 
-static void handle_surface_leave(void *data, struct wl_surface *surface,
-                                 struct wl_output *output)
+static void handle_surface_leave(void *data, struct wl_surface *surface, struct wl_output *output)
 {
-    SDL_WindowData *window = data;
+    SDL_WindowData *window = (SDL_WindowData *)data;
 
     if (!SDL_WAYLAND_own_output(output) || !SDL_WAYLAND_own_surface(surface)) {
         return;
     }
 
-    Wayland_RemoveOutputFromWindow(window, output);
+    Wayland_RemoveOutputFromWindow(window, (SDL_DisplayData *)wl_output_get_user_data(output));
 }
 
 static void handle_preferred_buffer_scale(void *data, struct wl_surface *wl_surface, int32_t factor)
diff --git a/src/video/wayland/SDL_waylandwindow.h b/src/video/wayland/SDL_waylandwindow.h
index cf8e7ea2a888..c607c2e80347 100644
--- a/src/video/wayland/SDL_waylandwindow.h
+++ b/src/video/wayland/SDL_waylandwindow.h
@@ -206,6 +206,6 @@ extern int Wayland_SetWindowHitTest(SDL_Window *window, SDL_bool enabled);
 extern int Wayland_FlashWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_FlashOperation operation);
 extern int Wayland_SyncWindow(SDL_VideoDevice *_this, SDL_Window *window);
 
-extern void Wayland_RemoveOutputFromWindow(SDL_WindowData *window, struct wl_output *output);
+extern void Wayland_RemoveOutputFromWindow(SDL_WindowData *window, SDL_DisplayData *display_data);
 
 #endif /* SDL_waylandwindow_h_ */