Skip to content
Advertisement

Wayland client get compositor name

Is it possible for a c application using libwayland-client.so to get the name of the compositor / display server it opened a connection to (e.g. KWin, Sway, …)? I fail to find it in the docs.

For reference, in X11 this is possible using XProps specified by EWMH: _NET_SUPPORTING_WM_CHECK to get the window id of the window manager and then using _NET_WM_NAME.

Im fine with anything giving me a way to identify it, for example a pretty name, the process name, the pid or similar.

Current solution is to detect which socket file wayland will be using (${XDG_RUNTIME_DIR}/${WAYLAND_DISPLAY:-wayland-0}), detecting which process are listening on it and picking the one which is most probably the compositor (similar to what neofetch does in bash). But since i need to open a connection anyway, and this method is very bug prone, i think you can see why i want to have a cleaner solution.

Advertisement

Answer

Requirements:

  • determine the PID of the peer compositor process for a display connection on the client side
  • must run under Linux
  • optionally determines the process name

Since this is not directly supported by the API, you can

  • get the file descriptor of the display context (wl_display_get_fd)
  • use the file descriptor to read the associated PID of the peer process (getsockopt with the SO_PEERCRED option, see e.g. this nice SO answer)
  • finally, you can get the process name by reading /proc/<pid>/comm.

You could also retrieve the process command line if you need more information.

However, the output of the following test program would look like this under Ubuntu 22.04 LTS:

JavaScript

Self-contained Example in C

JavaScript
Advertisement