19

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.

apaderno
  • 28,547
  • 16
  • 75
  • 90
ShadowDragon
  • 2,238
  • 5
  • 20
  • 32
  • 2
    What's the use case? For your average Linux desktop application, you don't need to know this -- you write it with a high level toolkit (Qt, GTK+, Wx, etc.) and that toolkit takes care of the X11/Wayland support for you. – MrEricSir Aug 06 '17 at 20:09
  • 1
    I want to develope a software which renderes direclty to the window screen using OpenGL. The software itself doesn't really rely on knowing a lot of things I am doing, but I want to create the technical backend of the software myself, thus I have to know how to do such things. – ShadowDragon Aug 06 '17 at 20:36
  • @shadowdragon every self respecting windowing toolkit allows you to render your opengl to its windows. No need to write your own for this. – rubenvb Aug 06 '17 at 20:53
  • Possible duplicate of [Effective way of detecting X11 vs Wayland, preferrably with CMake](https://stackoverflow.com/questions/45135228/effective-way-of-detecting-x11-vs-wayland-preferrably-with-cmake) – nobody Aug 06 '17 at 22:36
  • 5
    I don't think its a duplicated question as I don't want to detect the used window system at compile-time, but instead at runtime. – ShadowDragon Aug 07 '17 at 13:22
  • One motivation for Wayland is getting rid of cruft needed to implement the X protocol; no surprise to me Wayland attracts developers who are interested in minimalism and want to avoid dependencies - or just learn about the next lower layer. When doing DRM coding, I found the "low level" userspace libraries useful as sample code (along with the kernel source), but I didn't use them. Abstraction is powerful in SW development, but sometimes it presents an interface that's as ill-suited to purpose, or worse, than the one it abstracts away. –  Oct 19 '22 at 02:40

2 Answers2

20

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.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • You explained in a comment earlier that the `XDG_SESSION_TYPE` may not always be set. Could you explain why it isn't always set to the correct window system and why the `DISPLAY` variable is? I am also wondering what the `localhost:10.0` is for. As far as my understanding goes, it has something to do with the X window system, but correct me if I am wrong. – ShadowDragon Aug 07 '17 at 13:33
  • 2
    @ShadowDragon: The XDG variables are not set because I'm not in a desktop session. I'm coming in remotely using SSH tunneling. SSH can provide an X11 proxy, and it puts the proxy address at `localhost:10.0` – Zan Lynx Aug 07 '17 at 15:33
17

use the environment variable XDG_SESSION_TYPE

on x11

echo $XDG_SESSION_TYPE
x11

on wayland

$ echo $XDG_SESSION_TYPE
wayland
skr
  • 2,146
  • 19
  • 22
  • 4
    That isn't what I'd call reliable. – Zan Lynx Aug 06 '17 at 22:29
  • 4
    For example, I'm logged in to my Linux system using PuTTY on Windows and my X11 `DISPLAY` is set to `localhost:10.0`. It works just fine with XMing running. But `XDG_SESSION_TYPE` is not set to anything. I don't have any XDG variables of any kind set. – Zan Lynx Aug 06 '17 at 22:33
  • 3
    This doesn't work, sometimes you get "tty" which doesn't help matters. – Owl Jan 23 '22 at 08:34
  • 2
    Incorrect! You will get "tty" when using either if you don't use a display manager. – Maciej Krawczyk Nov 27 '22 at 12:27