Linux › Section N · The Linux desktop and printing › Topic N.2
Wayland and the X Window System
Wayland now runs most Linux desktops, while the X Window System lives on through compatibility layers and lighter desktops. A few command-line tools let you see each one at work: which compositor owns your screen, how input reaches a window, and how X programs survive in a Wayland world.
What Wayland is, and is not
"Desktop components" described the two designs. Look closer at Wayland and the first surprise is that there is no Wayland server package. Wayland is a protocol, plus the libwayland library most programs use to speak it. The program that actually runs your screen is a compositor: Mutter under GNOME, KWin under KDE Plasma, or wlroots-based compositors such as Sway. The Wayland project also ships Weston, a reference compositor. It works but is deliberately plain: its purpose is to show compositor authors how to implement the protocol. Protocol extensions, such as xdg-decoration for agreeing who draws title bars, are negotiated between client and compositor.
Finding the compositor
Clients reach the compositor through a Unix domain socket named by WAYLAND_DISPLAY, usually wayland-0, inside your runtime directory, XDG_RUNTIME_DIR, normally /run/user/ followed by your numeric user ID. A second compositor on another virtual terminal would use wayland-1. To learn which process owns the socket, list listening Unix sockets with their processes: ss -x selects Unix sockets, -l listening ones and -p the owning process (run it as root to see every process). The wayland-info tool, from the wayland-utils package, then lists the protocol interfaces the compositor offers, including outputs and input seats.
Worked example: who owns the screen?
1. Check the socket name: echo $WAYLAND_DISPLAY prints wayland-0. 2. Check where it lives: echo $XDG_RUNTIME_DIR prints /run/user/1000. 3. Ask the kernel which process listens there: sudo ss -xlp | grep wayland. 4. Read the result. Illustrative output, trimmed for width: u_str LISTEN 0 4096 /run/user/1000/wayland-0 ... users:(("gnome-shell",pid=1630,fd=35)). 5. Interpret: the listener is gnome-shell, process 1630. GNOME Shell uses Mutter as a library, so Mutter is the compositor doing the work. On KDE Plasma the same steps would show kwin_wayland; on Sway, sway.
libinput: from device to window
Keyboards, mice and touchpads appear to the kernel as evdev devices under /dev/input, such as /dev/input/event3. A compositor needs that raw input in a consistent form, with pointer acceleration, tap-to-click and scrolling handled the same way on every device. Most Wayland compositors, and many X servers, use the libinput library for this. The compositor then translates each event into the Wayland protocol and sends it only to the window that should receive it. The libinput command shows the layer below: sudo libinput list-devices lists each device and its capabilities, and sudo libinput debug-events --show-keycodes prints events live as you type and move the mouse.
X programs inside Wayland
Thousands of programs were written for X, so a Wayland desktop must still run them. There are two routes. Most applications use GTK or Qt, and since those toolkits speak Wayland natively, the application usually needs only small changes to become a native Wayland client. Programs that still need X run through Xwayland, a complete X server that is itself a Wayland client; the compositor starts it automatically, often only when an X program first appears. The extra layer costs little. The reverse is awkward: a compositor such as Weston can run inside an X window, but this confuses programs that look for wayland-0 first.
The X server and displays
On an X desktop the server process is called Xorg. Its first argument is usually the display, such as :0: one or more monitors that share a keyboard and mouse. X clients find it through DISPLAY, and on Linux they normally connect through the Unix socket /tmp/.X11-unix/X0. Each X server runs on a virtual terminal, and several can run at once on different terminals with different display numbers; Ctrl+Alt+F-keys or chvt switch between them. One weakness remains by design: any X client can watch other clients' input, which Wayland's per-window routing prevents.
Display managers and starting a session
Starting a bare X server gives an empty screen, because no clients have been told to run. In practice a display manager starts the graphical login: GDM for GNOME, SDDM for KDE Plasma, or LightDM, which can start many desktops. After you log in, it launches your chosen session, Wayland or X, with its window manager, panels and services. From a text console, startx or xinit starts a plain X session from your own startup files, which is why it often looks nothing like the display manager's version. Wayland compositors such as Sway can usually be started directly from a console by typing their name.
Network transparency and remote use
Because X clients talk to the server through a protocol, a program can run on one machine and display on another. The original method sent that protocol over TCP port 6000 without encryption, so current X servers no longer listen on the network by default. The safe method is SSH forwarding: ssh -X host tunnels X traffic through the encrypted connection, provided the server's sshd_config allows X11Forwarding. Wayland has no built-in equivalent, because clients hand the compositor whole pixel buffers. Remote use instead relies on tools that work with the compositor: remote-desktop protocols such as RDP, which GNOME supports, or waypipe, which forwards individual Wayland programs over SSH.
Worked example: a remote X program
An administrator wants to run a graphical log viewer installed on server lab2 and see it on a laptop. 1. Confirm that lab2's /etc/ssh/sshd_config permits X11Forwarding yes. 2. From the laptop, connect with ssh -X lab2. 3. On lab2, echo $DISPLAY now shows something like localhost:10.0: sshd created a proxy display, numbered from its default offset of 10, rather than a real screen. 4. Start the program; its window opens on the laptop, inside Xwayland if the laptop runs Wayland. 5. If it fails with a message that it cannot open a display, check steps 1 and 3 before reinstalling anything. The traffic is encrypted end to end.
Exploring X with its own tools
Several classic utilities inspect a running X server. xwininfo asks you to click a window and prints its ID, position and size; xlsclients -l lists clients. xev opens a test window and prints every event it receives, such as MotionNotify for pointer movement and KeyPress with each key's keycode, which helps when remapping keys. xinput list shows input devices with their IDs, and xinput set-button-map with the buttons 3 2 1 swaps the buttons of a three-button mouse. For keyboards, setxkbmap -option ctrl:nocaps turns Caps Lock into Ctrl using the XKB extension. Under Wayland these tools see only Xwayland clients, and the desktop's own settings normally control input.
Common mistakes to avoid
Running xev or xwininfo under Wayland and concluding a native window does not exist. Re-enabling X's TCP listener instead of using ssh -X. Setting DISPLAY by hand in startup files, which breaks sessions that set it themselves. Running a second compositor inside an X session and then wondering why new programs open in the wrong place. Changing keyboard settings with an X tool on a Wayland desktop, where the compositor ignores them; use the desktop's settings, which apply to both native and Xwayland programs.
Key takeaways
Wayland is a protocol; the compositor (Mutter, KWin, Sway, or the reference Weston) is the program, reached through WAYLAND_DISPLAY in XDG_RUNTIME_DIR, and ss -xlp shows who owns it. libinput turns kernel evdev events into consistent input. Xwayland runs X programs as Wayland clients. Xorg serves a display such as :0 on a virtual terminal, started by a display manager (GDM, SDDM, LightDM) or startx. Use ssh -X, not TCP, for remote X; Wayland uses RDP or waypipe. xev, xwininfo, xinput and setxkbmap explore X.
Flashcards
Flashcard
Library most programs use to speak the Wayland protocol
Test your understanding
Question 1 of 5
An administrator searches a GNOME Wayland desktop for a process named wayland and finds none. Why?
Question 2 of 5
Which command shows the process listening on the Wayland socket?
Question 3 of 5
How does an old X-only program run on a Wayland desktop?
Question 4 of 5
What is the safe modern way to display a remote X program on your own screen?
Question 5 of 5
After ssh -X lab2, what does DISPLAY usually look like on lab2?
