Browse Source

Reuse Wayland connection from availability check

M Stoeckl 5 years ago
parent
commit
a78b9763f1
1 changed files with 27 additions and 35 deletions
  1. 27 35
      src/video/wayland/SDL_waylandvideo.c

+ 27 - 35
src/video/wayland/SDL_waylandvideo.c

@@ -120,25 +120,15 @@ get_classname()
     return SDL_strdup("SDL_App");
 }
 
-/* Wayland driver bootstrap functions */
-static int
-Wayland_Available(void)
-{
-    struct wl_display *display = NULL;
-    if (SDL_WAYLAND_LoadSymbols()) {
-        display = WAYLAND_wl_display_connect(NULL);
-        if (display != NULL) {
-            WAYLAND_wl_display_disconnect(display);
-        }
-        SDL_WAYLAND_UnloadSymbols();
-    }
-
-    return (display != NULL);
-}
-
 static void
 Wayland_DeleteDevice(SDL_VideoDevice *device)
 {
+    SDL_VideoData *data = (SDL_VideoData *)device->driverdata;
+    if (data->display) {
+        WAYLAND_wl_display_flush(data->display);
+        WAYLAND_wl_display_disconnect(data->display);
+    }
+    SDL_free(data);
     SDL_free(device);
     SDL_WAYLAND_UnloadSymbols();
 }
@@ -147,23 +137,41 @@ static SDL_VideoDevice *
 Wayland_CreateDevice(int devindex)
 {
     SDL_VideoDevice *device;
+    SDL_VideoData *data;
+    struct wl_display *display;
 
-    if (!Wayland_Available()) {
+    if (!SDL_WAYLAND_LoadSymbols()) {
         return NULL;
     }
 
-    if (!SDL_WAYLAND_LoadSymbols()) {
+    display = WAYLAND_wl_display_connect(NULL);
+    if (display == NULL) {
+        SDL_WAYLAND_UnloadSymbols();
         return NULL;
     }
 
+    data = SDL_calloc(1, sizeof(*data));
+    if (data == NULL) {
+        WAYLAND_wl_display_disconnect(display);
+        SDL_WAYLAND_UnloadSymbols();
+        SDL_OutOfMemory();
+        return NULL;
+    }
+
+    data->display = display;
+
     /* Initialize all variables that we clean on shutdown */
     device = SDL_calloc(1, sizeof(SDL_VideoDevice));
     if (!device) {
+        SDL_free(data);
+        WAYLAND_wl_display_disconnect(display);
         SDL_WAYLAND_UnloadSymbols();
         SDL_OutOfMemory();
         return NULL;
     }
 
+    device->driverdata = data;
+
     /* Set the function pointers */
     device->VideoInit = Wayland_VideoInit;
     device->VideoQuit = Wayland_VideoQuit;
@@ -417,22 +425,13 @@ static const struct wl_registry_listener registry_listener = {
 int
 Wayland_VideoInit(_THIS)
 {
-    SDL_VideoData *data = SDL_calloc(1, sizeof(*data));
-    if (data == NULL)
-        return SDL_OutOfMemory();
-
-    _this->driverdata = data;
+    SDL_VideoData *data = (SDL_VideoData*)_this->driverdata;
 
     data->xkb_context = WAYLAND_xkb_context_new(0);
     if (!data->xkb_context) {
         return SDL_SetError("Failed to create XKB context");
     }
 
-    data->display = WAYLAND_wl_display_connect(NULL);
-    if (data->display == NULL) {
-        return SDL_SetError("Failed to connect to a Wayland display");
-    }
-
     data->registry = wl_display_get_registry(data->display);
     if (data->registry == NULL) {
         return SDL_SetError("Failed to get the Wayland registry");
@@ -529,14 +528,7 @@ Wayland_VideoQuit(_THIS)
     if (data->registry)
         wl_registry_destroy(data->registry);
 
-    if (data->display) {
-        WAYLAND_wl_display_flush(data->display);
-        WAYLAND_wl_display_disconnect(data->display);
-    }
-
     SDL_free(data->classname);
-    SDL_free(data);
-    _this->driverdata = NULL;
 }
 
 #endif /* SDL_VIDEO_DRIVER_WAYLAND */