2

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.

AggelosT
  • 108
  • 1
  • 9
  • Determining how to reach the compositor _always_ involves environment variables. If `WAYLAND_DISPLAY` is unset, it uses `XDG_RUNTIME_DIR` to find the directory relative to which to locate the socket to use, or if that's unset, calculates a default relative to `HOME`. – Charles Duffy Jul 23 '22 at 19:53
  • Mind, you can ask the kernel which environment variables a process was _started with_, so walk up the process tree to the first process within a user's session and the "unmodifiable" criteria can be met even with environment variables in play. – Charles Duffy Jul 23 '22 at 19:59

1 Answers1

1

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • If `SDL_VideoInit("wayland")` refers to environment variables, can you really say this is "WITHOUT environment variables"? – Charles Duffy Jul 23 '22 at 19:51
  • Nope, that should actually try to connect to the Wayland session, not *just* check some ennvars. – genpfault Jul 23 '22 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). – Charles Duffy Jul 23 '22 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? – AggelosT Jul 23 '22 at 20:31
  • @AggelosT: `SDL_VideoInit()` returns an error value and you keep moving down the `SDL_GetNumVideoDrivers()`/`SDL_GetVideoDriver()` list trying other drivers until one (or none) of them work. – genpfault Jul 23 '22 at 21:50