ShadowDragon ShadowDragon - 8 months ago 129
C++ Question

How I can find out if a Linux system uses Wayland or X11?

Used language



I am using C++14 with cmake for my program.

Problem:



I would like to know how I can find out if a Linux system uses Wayland or X11 as a window system to be able to use both APIs in my source code without conflict.
Thus creating a window with Wayland when Wayland is available and otherwise use
the X11 API.

Note: I know there is XWayland, but I want to use native X11 and native Wayland without something like XWayland.

EDIT: To clarify some things: I don't want to check for X11 or Wayland at compile-time, but instead at runtime, because then I just have to compile the code once and it doesn't require the user to think about which version to use.

Answer Source

X11 uses the DISPLAY environment variable to find the X server. Wayland uses WAYLAND_DISPLAY. Look for the Wayland variable first. Then if you don't find it or you can't connect go on to using X11.

Do not skip checking the WAYLAND_DISPLAY variable or assume Wayland is running on "wayland-0". Some people want to use nested compositors, which you would bypass. Other people may be running Wayland but want to force X11 rendering by deleting the WAYLAND_DISPLAY variable.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download