SDL: linux: (de)initialize d-bus at init and quit.

From dbdbae44c57dcc2caf5e2b1ed81cc31a98860d8e Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Fri, 2 Apr 2021 14:35:11 -0400
Subject: [PATCH] linux: (de)initialize d-bus at init and quit.

Previously we had different subsystems quitting it, in conflict, and risked
multiple threads racing to init it at the same time.

Fixes #3643.
---
 src/SDL.c                            | 11 +++++++++++
 src/video/wayland/SDL_waylandvideo.c |  6 ------
 src/video/x11/SDL_x11video.c         |  6 ------
 3 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/src/SDL.c b/src/SDL.c
index c5cf06730..73f1cd7f0 100644
--- a/src/SDL.c
+++ b/src/SDL.c
@@ -34,6 +34,9 @@
 #include "thread/os2/SDL_systls_c.h"
 #endif
 
+/* this checks for HAVE_DBUS_DBUS_H internally. */
+#include "core/linux/SDL_dbus.h"
+
 #if defined(__EMSCRIPTEN__)
 #include <emscripten.h>
 #endif
@@ -155,6 +158,10 @@ SDL_InitSubSystem(Uint32 flags)
     /* Clear the error message */
     SDL_ClearError();
 
+#if SDL_USE_LIBDBUS
+    SDL_DBus_Init();
+#endif
+
     if ((flags & SDL_INIT_GAMECONTROLLER)) {
         /* game controller implies joystick */
         flags |= SDL_INIT_JOYSTICK;
@@ -442,6 +449,10 @@ SDL_Quit(void)
     SDL_AssertionsQuit();
     SDL_LogResetPriorities();
 
+#if SDL_USE_LIBDBUS
+    SDL_DBus_Quit();
+#endif
+
     /* Now that every subsystem has been quit, we reset the subsystem refcount
      * and the list of initialized subsystems.
      */
diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index e43fb4cce..12203111e 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -575,12 +575,6 @@ Wayland_VideoQuit(_THIS)
 
     Wayland_QuitKeyboard(_this);
 
-/* !!! FIXME: other subsystems use D-Bus, so we shouldn't quit it here;
-       have SDL.c do this at a higher level, or add refcounting. */
-#if SDL_USE_LIBDBUS
-    SDL_DBus_Quit();
-#endif
-
     SDL_free(data->classname);
 }
 
diff --git a/src/video/x11/SDL_x11video.c b/src/video/x11/SDL_x11video.c
index 03fcaa5a1..3d601231b 100644
--- a/src/video/x11/SDL_x11video.c
+++ b/src/video/x11/SDL_x11video.c
@@ -457,12 +457,6 @@ X11_VideoQuit(_THIS)
     X11_QuitKeyboard(_this);
     X11_QuitMouse(_this);
     X11_QuitTouch(_this);
-
-/* !!! FIXME: other subsystems use D-Bus, so we shouldn't quit it here;
-       have SDL.c do this at a higher level, or add refcounting. */
-#if SDL_USE_LIBDBUS
-    SDL_DBus_Quit();
-#endif
 }
 
 SDL_bool