
# Wayland Windows

> The guest's desktop apps as windows on your page.

A Linux GUI app draws into windows that a display server gives it. With [wland](https://github.com/pithings/wland), the guest's Wayland apps, and its X apps through Xwayland, get their windows on your page: each window is an element you can place, with the keyboard, pointer, touch, clipboard and drag and drop going back to the app.

wland comes in two halves. Its relay, `wlandr`, runs in the guest and is the Wayland server that the apps connect to. Its compositor (wasm) runs on your page and draws each window. They talk over [vsock](/sdk/vsock), and the pixels don't go over that connection. The compositor reads each app's buffer from guest memory ([Shared Memory](/sdk/shm)). GL and Vulkan frames come from the page's own GPU renderers ([GL, Vulkan and Video](/sdk/graphics)).

::note
The compositor needs a DOM, so this is for browsers. The relay is in the Debian image; Alpine and the base shell have none (see [Put the relay in an image](#put-the-relay-in-an-image)).
::

## Show the windows

```js
import { createKernel } from "unkernel";
import { createStreamLink, createWland } from "wland/client";

const vm = await createKernel({ distro: "/distros/debian.erofs", shm: true });
const wl = await createWland({
  container: document.getElementById("desktop"), // positioned, with a fixed size: the apps' screen
  link: createStreamLink(vm.vsock.listen(1023)), // the relay dials the host on port 1023
  buffers: {
    open: (handle) => vm.shm.openId(handle.id), // each buffer is a /dev/kwshm export
    view(file, offset, length) {
      try { return file.inPlace ? file.views(offset, length) : null; } // no copy
      catch { return null; } // EAGAIN: the guest is replacing the pages
    },
    read: async (file, offset, dst) => dst.set(await file.read(offset, dst.length)),
    close: (file) => void file.close(),
  },
});
vm.exited.then(() => wl.dispose());

vm.write("sudo apt-get install -y foot && wland foot\n");
```

- `shm: true` gives the guest `/dev/kwshm`, through which the relay exports each buffer. vsock is on by default.
- `createStreamLink()` takes the listener as it is: the compositor serves the relay's connection, and starts fresh each time the relay reconnects (after a snapshot restore, or after the relay restarts). `wl.dispose()` closes the listener.
- `view()` hands wland the guest's own pages where `vm.shm.inPlace` is true (inline, or a worker with several CPUs and shared memory). When it returns null, wland calls `read()`, which copies the damaged spans and costs a round trip to the kernel's worker in worker mode.
- Use the wland release whose relay the image carries: they speak one protocol. `/usr/lib/kw/wlandr --version` prints it.

wland manages the windows itself by default: moves, resizes, focus, and Alt+F9/F10/F11 to minimize, maximize and make fullscreen. `wl.windows()` and `onWindowsChange` are what you need for a taskbar. To put the windows into your own window manager instead, see the `mount`, `interactive`, `autoFocus` and `manageDisplay` options in wland's README.

## Start apps

`wland CMD` (in the guest, on every distro's `PATH` as `/usr/lib/kw/bin/wland`) starts the relay for the user if it is not running, then runs CMD with `XDG_RUNTIME_DIR`, `WAYLAND_DISPLAY` and the hints that make GTK, Qt, SDL, Firefox, Chromium and Electron pick Wayland. Without CMD it prints them as shell assignments (`eval "$(wland)"`).

With `kw.wayland=1` on the kernel command line, the guest starts the relay at boot for the image's user (`kw.user=`, else `user`), and every login and interactive shell has the Wayland session, so `foot` started plainly opens a window. That relay connects only when the first app does, so a page that never runs a Wayland app loads nothing of wland. Pass it in `cmdline`, which replaces the bundled command line, so include the rest of it.

An app started while nothing listens on port 1023 fails at once with its own "cannot connect" error. When the link drops, the apps on it close, and the relay dials again for the next app. The relay logs to `$XDG_RUNTIME_DIR/wlandr.log`. Root's shells (`sudo -s`, `su`) use the user's relay; `sudo CMD` clears the variables, so use `sudo /usr/lib/kw/bin/wland CMD` instead.

## GL and Vulkan apps

By default, GL apps render in software (llvmpipe) and Vulkan apps copy each frame into a shared-memory buffer. Both can instead hand the page a frame that is already on its GPU. wland asks for that through `gpu`, and a resolver you leave out is a feature it does not offer to the apps:

```js
const XRGB8888 = 0x34325258, XBGR8888 = 0x34324258; // DRM fourccs without alpha

const vm = await createKernel({ distro: "/distros/debian.erofs", shm: true, gl: true, gpu: true, vk: true });
const wl = await createWland({
  container,
  link: createStreamLink(vm.vsock.listen(1023)),
  buffers, // as above
  gpu: {
    // GL apps: a virtio-gpu resource, blitted by the GL renderer
    ...(vm.gl?.gpu && {
      resolve: (ref) => ref.offset ? null : vm.gl.gpu.image(ref.resource, {
        width: ref.width,
        height: ref.height,
        opaque: ref.format === XRGB8888 || ref.format === XBGR8888,
        flipY: (ref.flags & 1) !== 0, // y_invert
      }),
    }),
    // Vulkan apps: a swapchain image, copied on the WebGPU device
    ...(vm.vk && { resolveExternal: (ref) => vm.vk.externalImage(ref) }),
    release: (image) => image.close(),
  },
});
```

- **GL** needs `gl` and `gpu` (the guest's `/dev/dri/renderD128`), and Mesa in the guest (`sudo apt-get install -y libegl-mesa0 libgles2 libgl1-mesa-dri`). The relay takes the apps' buffers by GPU resource (`--dmabuf virtgpu`) only if it started after the render node existed. With `gpu` the node is there at boot, but a relay started without it keeps rendering in software until it is restarted (`pkill -x wlandr`, which closes its apps). `vm.gl.gpu.image()` returns an `ImageBitmap`, or a promise of one while the renderer loads. It returns null for a buffer at an offset in its resource, and then the window keeps its last frame. Mesa's virgl driver always uses offset 0.
- **Vulkan** needs `vk` and a relay with external buffers (`--external-buffers`; the Debian image's has them). The guest's Vulkan driver then sends the page the id of each swapchain image instead of its pixels. `resolveExternal` must return synchronously, because a promise there would hold up every window on the link. `vm.vk.externalImage()` is synchronous.
- Each image you return is drawn during its commit, then handed to `release()`.

## X apps

There are three ways to run X apps, depending on what they need:

- **Each X window a page window.** The Debian image carries [xwayland-satellite](https://github.com/Supreeeme/xwayland-satellite) at `/usr/lib/kw/xwayland-satellite`. Its relay holds X display `:1` and starts satellite, which runs Xwayland rootless, when the first X client connects. Install Xwayland first (`sudo apt-get install -y xwayland x11-xserver-utils`). Shells started after that get `DISPLAY=:1`. X windows are drawn at scale 1 and the page upscales them on HiDPI screens, so X apps that know nothing of DPI come out the right size.
- **A whole X desktop in one window.** Run Xwayland rootful, such as `Xwayland :2 -geometry 1280x800 -decorate -shm` (with `libdecor-0-plugin-1-cairo` installed for `-decorate`), then run the session with `DISPLAY=:2`. This suits a client that brings its own window manager, such as Xfce. `-shm` keeps its buffers in shared memory, so GL inside it is software.
- **X on the GPU display, without Wayland.** Xorg's modesetting driver on the guest's virtio-gpu `card0`, shown through `vm.gl.gpu.drawFrame()` ([virtio-gpu](/sdk/graphics#virtio-gpu)). This is for apps that need GLX on the GPU. There is no window manager.

## Put the relay in an image

The scripts that start the relay are in the kw layer that unkernel puts on top of every distro. They look for:

| Path | What |
| --- | --- |
| `/usr/lib/kw/wlandr` | the relay: a static aarch64 binary, so it runs on any distro |
| `/usr/lib/kw/xwayland-satellite` | optional: X apps through the relay |
| `/usr/lib/kw-distro` | the image's name, written by unkernel's image builder: needed for the boot to start the relay |

Take the relay from a [wland release](https://github.com/pithings/wland/releases): `wlandr-link-linux-arm64.tar.gz` holds the build without wland's HTTP server, as `wlandr`. The Debian image pins it, with its SHA-256, in `guest/distro/debian/pins.sh`, and `guest/distro/debian/build.sh` installs it; another image built with `kernel/build.sh distro` can do the same (see `guest/README.md`). For a development build, `KW_WLANDR=<file> kernel/build.sh distro debian` uses a local relay instead.

The scripts pass flags only for the features the relay has (the `features:` line of `wlandr --version`). The relay needs `link:vsock` and `buffers:kwshm`, and the boot needs `link-on-demand`. `dmabuf:virtgpu`, `external-buffers`, and `xwayland` with `lodpi` turn on GL apps, Vulkan apps and X apps.

The guest's root is writable, so you can also copy a relay into a running guest's `/usr/lib/kw/`, through a [mount](/sdk/mounts) for example. It lasts until the guest stops, or across boots with a data disk. Once it is there, `wland CMD` works.
