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

Language used

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 in order 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 the native X11 and the native Wayland without something like XWayland.

EDIT: To clarify some things: I do not want to check X11 or Wayland at compile time, but instead at run time , because then I just have to compile the code once and the user does not need to think about which version to use.

+6
source share
3 answers

X11 uses the DISPLAY environment variable to search for the X server. Wayland uses WAYLAND_DISPLAY . First find the Wayland variable. Then if you do not find it or you cannot connect, continue to use X11.

Do not skip checking the WAYLAND_DISPLAY variable or assume Wayland is running on "wayland-0". Some people want to use nested composers that you get around. Other people may run Wayland, but want to force X11 to be rendered by removing the WAYLAND_DISPLAY variable.

+7
source

use the environment variable XDG_SESSION_TYPE

on x11

 echo $XDG_SESSION_TYPE x11 

on wayland

 $ echo $XDG_SESSION_TYPE wayland 
+4
source

I'm at runlevel 3, but in the graphical shell, and my $ XDG_SESSION_TYPE is equal to 'tty'

0
source

Source: https://habr.com/ru/post/1270635/


All Articles