Reuse Wayland connection from availability check
authorM Stoeckl
Tue, 14 Jul 2020 19:18:16 -0400
changeset 13948830a4ce7456e
parent 13947 084ffca603ae
child 13949 cee5dca9fad6
Reuse Wayland connection from availability check
src/video/wayland/SDL_waylandvideo.c
     1.1 --- a/src/video/wayland/SDL_waylandvideo.c	Tue Jul 14 21:13:27 2020 -0400
     1.2 +++ b/src/video/wayland/SDL_waylandvideo.c	Tue Jul 14 19:18:16 2020 -0400
     1.3 @@ -120,25 +120,15 @@
     1.4      return SDL_strdup("SDL_App");
     1.5  }
     1.6  
     1.7 -/* Wayland driver bootstrap functions */
     1.8 -static int
     1.9 -Wayland_Available(void)
    1.10 -{
    1.11 -    struct wl_display *display = NULL;
    1.12 -    if (SDL_WAYLAND_LoadSymbols()) {
    1.13 -        display = WAYLAND_wl_display_connect(NULL);
    1.14 -        if (display != NULL) {
    1.15 -            WAYLAND_wl_display_disconnect(display);
    1.16 -        }
    1.17 -        SDL_WAYLAND_UnloadSymbols();
    1.18 -    }
    1.19 -
    1.20 -    return (display != NULL);
    1.21 -}
    1.22 -
    1.23  static void
    1.24  Wayland_DeleteDevice(SDL_VideoDevice *device)
    1.25  {
    1.26 +    SDL_VideoData *data = (SDL_VideoData *)device->driverdata;
    1.27 +    if (data->display) {
    1.28 +        WAYLAND_wl_display_flush(data->display);
    1.29 +        WAYLAND_wl_display_disconnect(data->display);
    1.30 +    }
    1.31 +    SDL_free(data);
    1.32      SDL_free(device);
    1.33      SDL_WAYLAND_UnloadSymbols();
    1.34  }
    1.35 @@ -147,23 +137,41 @@
    1.36  Wayland_CreateDevice(int devindex)
    1.37  {
    1.38      SDL_VideoDevice *device;
    1.39 -
    1.40 -    if (!Wayland_Available()) {
    1.41 -        return NULL;
    1.42 -    }
    1.43 +    SDL_VideoData *data;
    1.44 +    struct wl_display *display;
    1.45  
    1.46      if (!SDL_WAYLAND_LoadSymbols()) {
    1.47          return NULL;
    1.48      }
    1.49  
    1.50 -    /* Initialize all variables that we clean on shutdown */
    1.51 -    device = SDL_calloc(1, sizeof(SDL_VideoDevice));
    1.52 -    if (!device) {
    1.53 +    display = WAYLAND_wl_display_connect(NULL);
    1.54 +    if (display == NULL) {
    1.55 +        SDL_WAYLAND_UnloadSymbols();
    1.56 +        return NULL;
    1.57 +    }
    1.58 +
    1.59 +    data = SDL_calloc(1, sizeof(*data));
    1.60 +    if (data == NULL) {
    1.61 +        WAYLAND_wl_display_disconnect(display);
    1.62          SDL_WAYLAND_UnloadSymbols();
    1.63          SDL_OutOfMemory();
    1.64          return NULL;
    1.65      }
    1.66  
    1.67 +    data->display = display;
    1.68 +
    1.69 +    /* Initialize all variables that we clean on shutdown */
    1.70 +    device = SDL_calloc(1, sizeof(SDL_VideoDevice));
    1.71 +    if (!device) {
    1.72 +        SDL_free(data);
    1.73 +        WAYLAND_wl_display_disconnect(display);
    1.74 +        SDL_WAYLAND_UnloadSymbols();
    1.75 +        SDL_OutOfMemory();
    1.76 +        return NULL;
    1.77 +    }
    1.78 +
    1.79 +    device->driverdata = data;
    1.80 +
    1.81      /* Set the function pointers */
    1.82      device->VideoInit = Wayland_VideoInit;
    1.83      device->VideoQuit = Wayland_VideoQuit;
    1.84 @@ -417,22 +425,13 @@
    1.85  int
    1.86  Wayland_VideoInit(_THIS)
    1.87  {
    1.88 -    SDL_VideoData *data = SDL_calloc(1, sizeof(*data));
    1.89 -    if (data == NULL)
    1.90 -        return SDL_OutOfMemory();
    1.91 -
    1.92 -    _this->driverdata = data;
    1.93 +    SDL_VideoData *data = (SDL_VideoData*)_this->driverdata;
    1.94  
    1.95      data->xkb_context = WAYLAND_xkb_context_new(0);
    1.96      if (!data->xkb_context) {
    1.97          return SDL_SetError("Failed to create XKB context");
    1.98      }
    1.99  
   1.100 -    data->display = WAYLAND_wl_display_connect(NULL);
   1.101 -    if (data->display == NULL) {
   1.102 -        return SDL_SetError("Failed to connect to a Wayland display");
   1.103 -    }
   1.104 -
   1.105      data->registry = wl_display_get_registry(data->display);
   1.106      if (data->registry == NULL) {
   1.107          return SDL_SetError("Failed to get the Wayland registry");
   1.108 @@ -529,14 +528,7 @@
   1.109      if (data->registry)
   1.110          wl_registry_destroy(data->registry);
   1.111  
   1.112 -    if (data->display) {
   1.113 -        WAYLAND_wl_display_flush(data->display);
   1.114 -        WAYLAND_wl_display_disconnect(data->display);
   1.115 -    }
   1.116 -
   1.117      SDL_free(data->classname);
   1.118 -    SDL_free(data);
   1.119 -    _this->driverdata = NULL;
   1.120  }
   1.121  
   1.122  #endif /* SDL_VIDEO_DRIVER_WAYLAND */