SDL: Only send display events for hotplugged displays, not the initial state

From 57366285d8a7121abc7d6ebdac2edb29da2ac928 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 23 Nov 2021 20:14:18 +0000
Subject: [PATCH] Only send display events for hotplugged displays, not the
 initial state

---
 src/video/wayland/SDL_waylandvideo.c | 14 +++++++++-----
 src/video/wayland/SDL_waylandvideo.h |  2 ++
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index 1938cd6c859..f9109f1d9f5 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -202,6 +202,7 @@ Wayland_CreateDevice(int devindex)
         return NULL;
     }
 
+    data->initializing = SDL_TRUE;
     data->display = display;
 
     /* Initialize all variables that we clean on shutdown */
@@ -449,15 +450,15 @@ display_handle_done(void *data,
 
     if (driverdata->index == -1) {
         /* First time getting display info, create the VideoDisplay */
+        SDL_bool send_event = driverdata->videodata->initializing ? SDL_FALSE : SDL_TRUE;
+        driverdata->placeholder.orientation = driverdata->orientation;
         driverdata->placeholder.driverdata = driverdata;
-        driverdata->index = SDL_AddVideoDisplay(&driverdata->placeholder, SDL_TRUE);
+        driverdata->index = SDL_AddVideoDisplay(&driverdata->placeholder, send_event);
         SDL_free(driverdata->placeholder.name);
         SDL_zero(driverdata->placeholder);
-
-        dpy = SDL_GetDisplay(driverdata->index);
+    } else {
+        SDL_SendDisplayEvent(dpy, SDL_DISPLAYEVENT_ORIENTATION, driverdata->orientation);
     }
-
-    SDL_SendDisplayEvent(dpy, SDL_DISPLAYEVENT_ORIENTATION, driverdata->orientation);
 }
 
 static void
@@ -489,6 +490,7 @@ Wayland_add_display(SDL_VideoData *d, uint32_t id)
     }
     data = SDL_malloc(sizeof *data);
     SDL_zerop(data);
+    data->videodata = d;
     data->output = output;
     data->registry_id = id;
     data->scale_factor = 1.0;
@@ -679,6 +681,8 @@ Wayland_VideoInit(_THIS)
 
     Wayland_InitKeyboard(_this);
 
+    data->initializing = SDL_FALSE;
+
     return 0;
 }
 
diff --git a/src/video/wayland/SDL_waylandvideo.h b/src/video/wayland/SDL_waylandvideo.h
index 4e1d8b2eec3..e82c7ec0b8a 100644
--- a/src/video/wayland/SDL_waylandvideo.h
+++ b/src/video/wayland/SDL_waylandvideo.h
@@ -47,6 +47,7 @@ typedef struct {
 } SDL_WaylandCursorTheme;
 
 typedef struct {
+    SDL_bool initializing;
     struct wl_display *display;
     int display_disconnected;
     struct wl_registry *registry;
@@ -89,6 +90,7 @@ typedef struct {
 } SDL_VideoData;
 
 typedef struct {
+    SDL_VideoData *videodata;
     struct wl_output *output;
     uint32_t registry_id;
     float scale_factor;