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.

2
  • 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. Commented Jul 23, 2022 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. Commented Jul 23, 2022 at 19:59

1 Answer 1

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.

5
  • 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.
    – genpfault
    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?
    – AggelosT
    Commented Jul 23, 2022 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
    Commented Jul 23, 2022 at 21:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.