I am currently working with C and SDL2, and I need to know whether Wayland is currently used as the windowing system (Obviously because I want to use Wayland, but SDL2 defaults to Xwayland). While SDL_VIDEODRIVER=wayland does work, it won't work if you are in X11, saying that the video driver is unavailable. So, what I am looking for, is a low-level way of getting the current windowing system (Possibly by asking the compositor?) on GNU/Linux. It also needs to be unmodifiable, that is, no application or user will be able to change it unless the session ends.
1 Answer
Initialize SDL with no subsystem via SDL_Init(0) then ask SDL to connect to whatever Wayland session is running via SDL_VideoInit("wayland"); if that call succeeds you're good to go with the usual SDL_Init(SDL_INIT_EVERYTHING) & window creation.
Though for more robustness you should iterate over the SDL_GetNumVideoDrivers()/SDL_GetVideoDriver() string list to verify the SDL install in use was even built with Wayland support.
See the test program here for video driver enumeration & testing.
-
If
SDL_VideoInit("wayland")refers to environment variables, can you really say this is "WITHOUT environment variables"? Commented Jul 23, 2022 at 19:51 -
Nope, that should actually try to connect to the Wayland session, not just check some ennvars. Commented Jul 23, 2022 at 19:52
-
Right, but my point is that the actually trying to connect involves environment variables, because that's how the library being invoked finds the socket to connect to. (There's a default location, but there's an environment variable that's checked to determine whether it should be overridden, and that default is defined relative to the location pointed at by a different environment variable). Commented Jul 23, 2022 at 19:54
-
@genpfault What happens on failure though? What if there is no Wayland session at all, or it simply fails to detect one?– AggelosTCommented Jul 23, 2022 at 20:31
-
@AggelosT:
SDL_VideoInit()returns an error value and you keep moving down theSDL_GetNumVideoDrivers()/SDL_GetVideoDriver()list trying other drivers until one (or none) of them work. Commented Jul 23, 2022 at 21:50
WAYLAND_DISPLAYis unset, it usesXDG_RUNTIME_DIRto find the directory relative to which to locate the socket to use, or if that's unset, calculates a default relative toHOME.