Skip to content

Commit

Permalink
Reuse Wayland connection from availability check
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoeckl committed Jul 14, 2020
1 parent 8669a87 commit a78b976
Showing 1 changed file with 27 additions and 35 deletions.
62 changes: 27 additions & 35 deletions src/video/wayland/SDL_waylandvideo.c
Expand Up @@ -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();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit a78b976

Please sign in to comment.