Skip to content

Commit

Permalink
video: Support multiple video driver entries having the same name string
Browse files Browse the repository at this point in the history
Allow multiple bootstrap entries for a single video driver with the same name, which internally allows preferential and fallback init conditions while hiding the implementation details from applications (e.g. applications will just see "wayland", regardless of whether it's using the preferred or fallback driver list entry).

If a driver is requested, all instances of it in the list will be tried before reporting failure, and client applications programmatically enumerating the video drivers will be presented with a deduplicated list of entries.
  • Loading branch information
Kontrabant committed Mar 30, 2024
1 parent 98a8f8e commit 4083c58
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
34 changes: 31 additions & 3 deletions src/video/SDL_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,41 @@ static int SDL_UninitializedVideo(void)
return SDL_SetError("Video subsystem has not been initialized");
}

/* Deduplicated list of video bootstrap drivers. */
static const VideoBootStrap *deduped_bootstrap[SDL_arraysize(bootstrap) - 1];

int SDL_GetNumVideoDrivers(void)
{
return SDL_arraysize(bootstrap) - 1;
static int num_drivers = -1;

if (num_drivers >= 0) {
return num_drivers;
}

num_drivers = 0;

/* Build a list of unique video drivers. */
for (int i = 0; bootstrap[i] != NULL; ++i) {
SDL_bool duplicate = SDL_FALSE;
for (int j = 0; j < i; ++j) {
if (SDL_strcmp(bootstrap[i]->name, bootstrap[j]->name) == 0) {
duplicate = SDL_TRUE;
break;
}
}

if (!duplicate) {
deduped_bootstrap[num_drivers++] = bootstrap[i];
}
}

return num_drivers;
}

const char *SDL_GetVideoDriver(int index)
{
if (index >= 0 && index < SDL_GetNumVideoDrivers()) {
return bootstrap[index]->name;
return deduped_bootstrap[index]->name;
}
return NULL;
}
Expand Down Expand Up @@ -507,7 +533,9 @@ int SDL_VideoInit(const char *driver_name)
if ((driver_attempt_len == SDL_strlen(bootstrap[i]->name)) &&
(SDL_strncasecmp(bootstrap[i]->name, driver_attempt, driver_attempt_len) == 0)) {
video = bootstrap[i]->create();
break;
if (video) {
break;
}
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/video/wayland/SDL_waylandvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
#include <libdecor.h>
#endif

#define WAYLANDVID_PREFERRED_DRIVER_NAME "wayland_preferred"
#define WAYLANDVID_DRIVER_NAME "wayland"

/* Clamp certain core protocol versions on older versions of libwayland. */
Expand Down Expand Up @@ -594,7 +593,7 @@ static SDL_VideoDevice *Wayland_Fallback_CreateDevice(void)
}

VideoBootStrap Wayland_preferred_bootstrap = {
WAYLANDVID_PREFERRED_DRIVER_NAME, "SDL Wayland video driver",
WAYLANDVID_DRIVER_NAME, "SDL Wayland video driver",
Wayland_Preferred_CreateDevice,
Wayland_ShowMessageBox
};
Expand Down

0 comments on commit 4083c58

Please sign in to comment.