mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-12-01 04:34:11 +01:00
feat(kwin): read the desktop through a script in KWin
KWin has no protocol that lets another process read or set the geometry of a window, but its scripting API has both, and a signal for every change the window manager follows. So the KWin backend is in two parts: bridge.js, a script the daemon loads into KWin, which reports what KWin sees and holds no window manager logic, and a D-Bus service in the daemon, org.komorebi.Bridge, which the script calls with every event as a JSON string. Every decision about what an event means is made by the table in kwin::state, which is pure and is unit-tested with the JSON the script sends: which windows may be managed, the ids komorebi gives KWin's windows, and the Show, Destroy, FocusChange, Minimize and Restore events of the window manager. Events reach the window manager in the order KWin raised them: the Event interface handles its calls inline (zbus reordered them otherwise), and one channel and one thread, window_manager_event_listener and listen_for_events, carry them on. Commands go to the script as the answer to a Poll call that the script always has open and the daemon holds until it has something to send: in the spikes this was as fast as any other channel, and it makes one call every 10 seconds when nothing happens. Here the only command is the cursor query. Every command of the Backend trait is refused with "the KWin backend only reads the desktop", so the daemon follows KWin and moves nothing. The bridge is JavaScript that KWin runs as it is, and it gets types and tests without a JavaScript build chain. tsc checks it as strict TypeScript through JSDoc types of every message, against kwin-api.d.ts, a declaration of the part of KWin's scripting API it uses, in which every property is read-only and callDBus takes one string. bridge.test.js runs it under node --test against a fake KWin in a node:vm context. The messages of a session are JSON fixtures that both sides' tests read: the bridge's tests check that it sends those events, and the table's and the backend's tests read the same events and commands, so a message cannot change on one side alone. just check-js and the flake's komorebi-bridge-js check run both, with nodejs and typescript from nixpkgs; there is no package.json. The daemon now connects to KWin by default, and --fake-backend is hidden. The VM suite vm-kwin-events starts it against a headless KWin and checks, from its debug log, that the script loads and reports what was open, that KWin's signals arrive as events and in order, and that stopping the daemon unloads the script. docs/dev/PLATFORM.md starts with what komorebi needs from Plasma. zbus 5's zvariant, zbus_names and zvariant_derive are flagged unmaintained, since their code moved into zbus 6; deny.toml ignores the three advisories.
This commit is contained in:
1 parent
02231d232c
commit
15ec3c1274
39 files changed
+3815
-95
No files matched your search
+114
-17
@@ -6,9 +6,9 @@ compositor to put it there. Everything it needs from the compositor is behind on
|
||||
tested, without one.
|
||||
|
||||
This page describes that trait, who implements and calls it, where each of its rules is
|
||||
enforced, the model of the desktop the window manager tiles, and how an event about a
|
||||
window becomes a tile. It grows with the code: each part of the daemon that is added has
|
||||
its place here.
|
||||
enforced, the model of the desktop the window manager tiles, the backend that reaches KWin,
|
||||
and how an event about a window becomes a tile. It grows with the code: each part of the
|
||||
daemon that is added has its place here.
|
||||
|
||||
## Vocabulary
|
||||
|
||||
@@ -30,6 +30,12 @@ its place here.
|
||||
trait that talks to a compositor.
|
||||
- **Fake**: [`FakeBackend`](../../komorebi/src/backend/fake.rs), the in-memory backend
|
||||
the tests use.
|
||||
- **Bridge**: the script komorebi loads into KWin ([`bridge.js`]). It reports what KWin
|
||||
tells it and carries out what the daemon asks.
|
||||
- **Table**: the KWin backend's model of the desktop ([`Table`]), which the bridge's events
|
||||
keep up to date and the queries are answered from.
|
||||
- **Held call**: the `Poll` call the bridge always has open, which the daemon answers when it
|
||||
has commands for the bridge, or after 10 seconds with none.
|
||||
|
||||
## Design
|
||||
|
||||
@@ -84,6 +90,52 @@ monitor, and every open window that is normal, alive and not minimized, on the f
|
||||
workspace of the monitor its middle is on. After that it changes only when an event comes
|
||||
in, and each event ends with the workspace in front tiled again.
|
||||
|
||||
## The KWin backend
|
||||
|
||||
[`KWinBackend`] is the backend of a real Plasma session. KWin has no protocol that lets
|
||||
another process read and set window geometry, so the backend is in two processes:
|
||||
|
||||
- **The bridge**, [`bridge.js`], a script KWin runs inside itself. It subscribes to KWin's
|
||||
signals and sends each as a JSON `Event` call over the session bus, and it carries out the
|
||||
commands it gets as the answer to its held `Poll` call. When it starts it sends a snapshot
|
||||
of the desktop. It holds no window manager logic.
|
||||
- **The daemon's side**, [`kwin`], owns `org.komorebi.Bridge` on the session bus, with one
|
||||
object for `Event` and one for `Poll`. `Event` calls are handled one at a time, in the order
|
||||
they arrive, and never wait. A `Poll` call is held until there are commands, so that the
|
||||
bridge gets them as soon as they are queued, without a timer and without a call from the
|
||||
daemon into KWin.
|
||||
|
||||
Every decision about what an event means is made in the daemon, by the table
|
||||
([`kwin::state`]): an event goes in, the table is updated, and what follows comes out, a
|
||||
window event for the window manager or the answer to a query. The table is pure (no D-Bus,
|
||||
no threads), so the unit tests drive it with the JSON the bridge sends.
|
||||
|
||||
The bridge is JavaScript, which no Rust build compiles, and KWin loads the file as it is, so
|
||||
it is checked without a build step. `tsc` checks it as strict TypeScript through its JSDoc
|
||||
comments, which type every message, against [`kwin-api.d.ts`], the part of KWin's scripting
|
||||
API the bridge uses. [`bridge.test.js`] runs it with `node --test` against a fake of that API.
|
||||
The two sides share the messages of a session in [`fixtures`]: the bridge's tests check that
|
||||
it sends exactly those events, and answer it with those commands, and the Rust tests feed
|
||||
the events to the table and check that the backend sends those commands. A change to a
|
||||
message on one side fails a test on the other.
|
||||
|
||||
The path of an event: a KWin signal, the bridge's handler, `Event(json)` on the session bus,
|
||||
[`Table::handle`], a [`WindowManagerEvent`] on the channel of
|
||||
[`window_manager_event_listener`], the thread that [`listen_for_events`] starts, and
|
||||
[`WindowManager::process_event`]. The queries (`windows`, `window`, `foreground_window`,
|
||||
`monitors`) are answered from the table. `cursor_pos` is the exception: only the bridge can
|
||||
read the pointer, so it is asked through the held call, and given 250 ms to answer before
|
||||
the table's last known position is returned.
|
||||
|
||||
The KWin backend only reads the desktop: every command is an error, "the KWin backend only
|
||||
reads the desktop", which the window manager logs. When komorebi stops, the backend asks KWin
|
||||
to unload the bridge.
|
||||
|
||||
[`connect`] makes the backend: it connects to the session bus, loads the bridge into KWin,
|
||||
and returns once the snapshot has arrived, so that the window manager reads a full table when
|
||||
it starts. [PLATFORM.md](PLATFORM.md) has the D-Bus names, the files and the environment
|
||||
variables; [DESIGN.md](DESIGN.md#why-does-komorebi-run-a-script-inside-kwin) has the reasons.
|
||||
|
||||
## The parts
|
||||
|
||||
- [`backend`](../../komorebi/src/backend/mod.rs): the trait, the two data types, and the
|
||||
@@ -105,9 +157,15 @@ in, and each event ends with the workspace in front tiled again.
|
||||
- [`window_manager_event`](../../komorebi/src/window_manager_event.rs) and
|
||||
[`process_event`](../../komorebi/src/process_event.rs): the events, and what the window
|
||||
manager does with each.
|
||||
- [`main.rs`](../../komorebi/src/main.rs): the daemon. It installs the backend chosen on the
|
||||
command line (`--fake-backend` is the only one), loads and tiles the desktop, and waits to
|
||||
be stopped.
|
||||
- [`window_manager_event_listener`](../../komorebi/src/window_manager_event_listener.rs): the
|
||||
channel that carries events from the backend to the window manager, and
|
||||
[`listen_for_events`], the thread that takes them off it.
|
||||
- [`kwin`](../../komorebi/src/kwin/mod.rs): the KWin backend, its D-Bus service and the held
|
||||
call; [`kwin::state`](../../komorebi/src/kwin/state.rs), the table; and
|
||||
[`bridge.js`](../../komorebi/src/kwin/bridge.js), the bridge.
|
||||
- [`main.rs`](../../komorebi/src/main.rs): the daemon. It connects to KWin, or installs the
|
||||
fake with `--fake-backend`, loads and tiles the desktop, starts the thread that handles
|
||||
events, and waits to be stopped.
|
||||
|
||||
The handle is a process-wide singleton so that a window can be a small `Copy` value
|
||||
that carries only its id, and ask the backend for what it needs. A test replaces it for
|
||||
@@ -120,7 +178,8 @@ done, also if it panics.
|
||||
the fake. The window manager is its only caller, through [`backend`]. The contract:
|
||||
|
||||
- Queries (`windows`, `window`, `foreground_window`, `cursor_pos`) answer from the
|
||||
backend's own table and never wait for the compositor. `monitors` may fail.
|
||||
backend's own table and never wait for the compositor, except the KWin backend's
|
||||
`cursor_pos`, which waits up to 250 ms for the bridge. `monitors` may fail.
|
||||
- A command returns once the compositor has accepted it, not once the client has acted on
|
||||
it. The frame a window ends up with is what `window_rect` says afterwards, because a
|
||||
client may refuse a size.
|
||||
@@ -142,21 +201,36 @@ the fake. The window manager is its only caller, through [`backend`]. The contra
|
||||
| A locked container keeps its index | framework path: a [`Workspace`] inserts, removes and swaps its containers through `LockableSequence` | `test_locked_containers_with_new_window` and `test_locked_containers_remove_window` | `containers_mut()` is the `VecDeque` itself |
|
||||
| A window komorebi does not manage is never moved | framework path: `handle_event` asks [`Window::should_manage`] before it acts on any event but a destroy | `dialogs_and_other_non_normal_windows_are_left_alone` | none |
|
||||
| A destroyed window leaves whichever workspace has it | framework path: the destroy is looked up in the index of managed windows, which is rebuilt at startup and before and after every event | `a_window_that_was_open_before_komorebi_started_is_removed_when_it_closes` | none |
|
||||
| The daemon does not start without a backend | startup check: a usage error, exit code 2 | `without_a_backend_komorebi_exits_with_an_error_naming_the_flag` | none |
|
||||
| Queries never block on the compositor | convention only | none | none |
|
||||
| The daemon does not start without KWin, unless it is told to run against the fake | startup check: [`connect`] fails, and the daemon exits with the error, code 1 | `without_a_session_bus_komorebi_exits_with_an_error_naming_it` | `--fake-backend` |
|
||||
| One komorebi per session | startup check: the session bus gives `org.komorebi.Bridge` to one connection | none | none |
|
||||
| Events reach the window manager in the order KWin raised them | framework path: the `Event` interface is `spawn = false`, so the calls are handled inline, and one channel and one thread carry the events to the window manager | `vm-kwin-events` ("the Show came first") | none |
|
||||
| No event names a window before the event that announces it | convention in the bridge, which announces a window it has not seen when it is activated; the table shows a window once | `a_new_window_raises_show_and_then_focus_in_that_order`, `announcing_the_same_window_twice_shows_it_once`, `vm-kwin-events` | none |
|
||||
| A window's id names one window while the daemon runs | framework path: the table numbers each KWin window the first time it sees it, and never gives a number out twice | `ids_are_assigned_once_per_window_and_survive_a_new_snapshot`, `tombstones_are_dropped_after_a_while_and_their_ids_are_not_reused` | none |
|
||||
| A backend never waits for the window manager | framework path: an event that does not fit in the channel (20 waiting) is dropped and logged | none | none |
|
||||
| The KWin backend changes nothing on the desktop | runtime error: every command returns "the KWin backend only reads the desktop"; type checker: [`kwin-api.d.ts`] declares every property the bridge could set read-only | `vm-kwin-events` ("nothing was moved"), `tsc` | none |
|
||||
| The bridge holds no window manager logic | convention, with every decision about an event in the table, which the `kwin::state` unit tests pin | the unit tests in `komorebi/src/kwin/state.rs` | none |
|
||||
| What the bridge sends is what the table reads | type checker: the JSDoc types of the messages in [`bridge.js`]; tests: the events of [`fixtures`] are what the bridge's tests check it sends and what the table's tests feed it | `what the bridge sends for a session is what the daemon's tests read`, `the_bridges_events_for_a_session_are_the_window_events_komorebi_expects` | none |
|
||||
| What the backend sends is what the bridge carries out | tests: the commands of [`fixtures`] are what the backend's tests check it sends and what the bridge's tests answer it with | `the_commands_of_a_session_go_to_the_bridge_as_the_bridges_tests_answer_it`, `what the bridge sends for a session is what the daemon's tests read` | none |
|
||||
| Everything the bridge sends over D-Bus is one string | type checker: [`kwin-api.d.ts`] gives `callDBus` one string argument | `tsc`, `every call goes to the daemon's names, with one JSON string` | none |
|
||||
| The bridge uses nothing a KWin script does not have | type checker: the library is ECMAScript 2016 alone, and KWin's API is what [`kwin-api.d.ts`] declares | `tsc` | a declaration KWin does not match: the VM tests run the bridge in KWin |
|
||||
| Queries never block on the compositor | convention only | none | the KWin backend's `cursor_pos` waits up to 250 ms |
|
||||
| Only the backend module reaches the compositor | convention only | none | none |
|
||||
|
||||
The last two rows are rules nothing checks. A real backend that waits inside a query
|
||||
breaks the first, and a module that talks to D-Bus directly breaks the second.
|
||||
The rows enforced by convention are rules nothing checks. A real backend that waits inside a
|
||||
query breaks one of them, a module that talks to D-Bus directly breaks another, and a bridge
|
||||
that decides something in JavaScript breaks a third: the bridge's tests check what it sends,
|
||||
not that it decides nothing.
|
||||
|
||||
## One event, end to end
|
||||
|
||||
A window is shown, and ends up on a tile:
|
||||
|
||||
1. The event is a [`WindowManagerEvent`]`::Show` carrying the window and the compositor
|
||||
signal it came from. The end-to-end tests build it and call
|
||||
[`WindowManager::process_event`], which hands it to `handle_event` and logs how long
|
||||
that took.
|
||||
1. KWin raises `windowAdded`. The bridge sends an `added` event with what KWin says about
|
||||
the window, and [`Table::handle`] gives the window an id, records it, and returns a
|
||||
[`WindowManagerEvent`]`::Show` carrying the window and the compositor signal it came
|
||||
from. The event goes on the channel, and the event thread calls
|
||||
[`WindowManager::process_event`], which hands it to `handle_event` and logs how long that
|
||||
took. The end-to-end tests build the event themselves and call `process_event`.
|
||||
2. `handle_event` asks [`Window::should_manage`], which asks the backend's `window(id)`
|
||||
whether the window is alive and normal. If it is not, the event changes nothing.
|
||||
3. The index of managed windows is rebuilt from the containers. If the workspace in front
|
||||
@@ -171,7 +245,8 @@ A window is shown, and ends up on a tile:
|
||||
6. `Workspace::update` asks the layout for one tile per container, clamps each to the work
|
||||
area, and calls [`Window::set_position`] for each window, which is the backend's
|
||||
`set_rect`. The fake puts the window there at once; the frame a window ends up with is
|
||||
what `window_rect` says afterwards.
|
||||
what `window_rect` says afterwards. The KWin backend refuses, and the window stays where
|
||||
KWin put it.
|
||||
7. If the new container is the only one, the window is given the focus with the backend's
|
||||
`focus`, and the pointer follows it.
|
||||
8. The index is rebuilt again, so the next event finds the window.
|
||||
@@ -188,7 +263,17 @@ A window is shown, and ends up on a tile:
|
||||
- [`komorebi/src/monitor.rs`](../../komorebi/src/monitor.rs): the work area, padding and
|
||||
offset a monitor gives the workspace it shows.
|
||||
- [`komorebi/src/process_event.rs`](../../komorebi/src/process_event.rs): what each event
|
||||
does.
|
||||
does, and the thread that handles them.
|
||||
- [`komorebi/src/kwin/mod.rs`](../../komorebi/src/kwin/mod.rs): the KWin backend, its D-Bus
|
||||
service and the held call.
|
||||
- [`komorebi/src/kwin/state.rs`](../../komorebi/src/kwin/state.rs): the table, with the
|
||||
shapes of the bridge's events, and its tests.
|
||||
- [`komorebi/src/kwin/bridge.js`](../../komorebi/src/kwin/bridge.js): the bridge, with the
|
||||
types of its messages; [`kwin-api.d.ts`] and
|
||||
[`tsconfig.json`](../../komorebi/src/kwin/tsconfig.json), what `tsc` checks it against;
|
||||
[`bridge.test.js`], its tests; [`fixtures`], the messages both sides' tests read.
|
||||
- [`nix/tests/vm/suites/kwin-events`](../../nix/tests/vm/suites/kwin-events/test.py): the
|
||||
daemon against a real KWin.
|
||||
- [`komorebi/tests/wm_events.rs`](../../komorebi/tests/wm_events.rs): the window manager
|
||||
driven end to end against the fake.
|
||||
|
||||
@@ -212,3 +297,15 @@ A window is shown, and ends up on a tile:
|
||||
[`WindowManager::process_event`]: ../../komorebi/src/process_event.rs
|
||||
[`WindowManager::update_focused_workspace`]: ../../komorebi/src/window_manager.rs
|
||||
[`WindowManagerEvent`]: ../../komorebi/src/window_manager_event.rs
|
||||
[`window_manager_event_listener`]: ../../komorebi/src/window_manager_event_listener.rs
|
||||
[`listen_for_events`]: ../../komorebi/src/process_event.rs
|
||||
[`KWinBackend`]: ../../komorebi/src/kwin/mod.rs
|
||||
[`connect`]: ../../komorebi/src/kwin/mod.rs
|
||||
[`kwin`]: ../../komorebi/src/kwin/mod.rs
|
||||
[`kwin::state`]: ../../komorebi/src/kwin/state.rs
|
||||
[`Table`]: ../../komorebi/src/kwin/state.rs
|
||||
[`Table::handle`]: ../../komorebi/src/kwin/state.rs
|
||||
[`bridge.js`]: ../../komorebi/src/kwin/bridge.js
|
||||
[`kwin-api.d.ts`]: ../../komorebi/src/kwin/kwin-api.d.ts
|
||||
[`bridge.test.js`]: ../../komorebi/src/kwin/bridge.test.js
|
||||
[`fixtures`]: ../../komorebi/src/kwin/fixtures
|
||||
@@ -58,3 +58,78 @@ it considers next and reports that as a focus change, which komorebi follows. If
|
||||
also focused a window of its own choosing, the two would fight over the focus. komorebi
|
||||
focuses the window of its focused container only when the compositor has left nothing
|
||||
focused.
|
||||
|
||||
## Why does komorebi run a script inside KWin?
|
||||
|
||||
KWin has no protocol that lets another process read and set window geometry. Its scripting
|
||||
API has both, and a signal for each change komorebi follows: a window added, removed,
|
||||
activated, minimized, moved or resized. So the part of the backend that sees and touches
|
||||
windows is a script KWin runs, the bridge, and the daemon talks to it over the session bus.
|
||||
|
||||
## Why does the bridge hold no logic?
|
||||
|
||||
So that everything the backend decides can be tested without KWin. A script runs only inside
|
||||
KWin: its own tests run it against a fake of KWin's API, which is only as right as the fake,
|
||||
and a test of it against KWin needs a virtual machine. The bridge reports what KWin says, as
|
||||
it says it, and carries out what it is told; what an event means (is this a window komorebi
|
||||
manages, has it been announced already, which id does it have) is decided by the table in
|
||||
`kwin::state`, which is plain Rust that takes the bridge's JSON and is unit-tested with it.
|
||||
|
||||
## Why is the bridge type-checked and tested without a JavaScript build chain?
|
||||
|
||||
I wanted safety, tests and typing around the bridge that do not need a JavaScript build
|
||||
chain. KWin loads `bridge.js` as it is, so the checks read that file and produce nothing:
|
||||
|
||||
- `tsc` checks it as strict TypeScript through its JSDoc comments (`checkJs`, `noEmit`),
|
||||
against `kwin-api.d.ts`, a hand-written declaration of the part of KWin's scripting API the
|
||||
bridge uses. KWin publishes no declarations, and the declarations are narrower than the API
|
||||
where that catches a mistake: a property the bridge has no reason to set is read-only, and
|
||||
`callDBus` takes one string.
|
||||
- `bridge.test.js` runs under `node --test`, node's own test runner, and loads the bridge into
|
||||
a `node:vm` context whose globals are a fake KWin.
|
||||
- The messages of a session are JSON files that the bridge's tests and the Rust tests both
|
||||
read, so neither side can change a message alone.
|
||||
|
||||
`tsc` and `node` come from nixpkgs, through the dev shell and the flake's
|
||||
`komorebi-bridge-js` check. There is no package.json, no npm dependency and no bundler.
|
||||
|
||||
## Why do commands go to the bridge as the answer to a held call?
|
||||
|
||||
Because it was the fastest channel measured, and it costs nothing while nothing happens. Four
|
||||
ways for the daemon to get commands to a script in KWin were measured in October 2026, on
|
||||
KWin 6.8.1 in a VM under HVF: twenty windows retiled in one batch, twenty times, timed from
|
||||
the send until every window reported its new frame.
|
||||
|
||||
| Channel | p50 / p95 / max |
|
||||
|---|---|
|
||||
| a held `Poll` call, answered when there are commands | 16 / 19 / 20 ms |
|
||||
| the script polls on a 16 ms timer | 16 / 20 / 32 ms |
|
||||
| the script polls on a 50 ms timer | 49 / 57 / 65 ms |
|
||||
| a new script per batch (`loadScript`, `run`, `unloadScript`) | 17 / 18 / 20 ms |
|
||||
|
||||
A timer is as fast only at about 60 D-Bus calls a second, forever. A new script per batch is
|
||||
as fast, but 7 of its 17 ms (p50) are spent in the daemon's calls to KWin. A held call is
|
||||
answered as soon as there is a batch, and an idle bridge makes one call every 10 seconds.
|
||||
Rounds after idle gaps of 15 seconds to 10 minutes all completed, the slowest in 49 ms.
|
||||
|
||||
The hold is 10 seconds because QtDBus fails a call after 25 seconds. A call that fails is
|
||||
never answered (the callback of `callDBus` runs on success only), so the bridge has a
|
||||
one-second watchdog that calls `Poll` again when the last call is older than the hold plus 3
|
||||
seconds, which is also how it finds a daemon that has restarted.
|
||||
|
||||
## Why are events and the held call on two D-Bus objects?
|
||||
|
||||
Because events must arrive in order and a held call waits. zbus handles each method call in a
|
||||
task of its own by default, and two pairs of events were seen swapped. The `Event` interface
|
||||
is therefore `spawn = false`: zbus handles its calls inline, one after the other, in the
|
||||
order they arrive. The price is that a handler that waits holds up every call after it, so
|
||||
`Event` never waits, and `Poll`, which waits by design, is a separate object with the default
|
||||
spawning.
|
||||
|
||||
## Why does everything cross D-Bus as a JSON string?
|
||||
|
||||
Because a script cannot choose the D-Bus type of what it sends. `callDBus` sends a JavaScript
|
||||
number as a 32-bit integer, and a call whose arguments do not match the method's signature
|
||||
fails with no error the script can see. One string argument always matches, and the daemon
|
||||
parses it. The declaration of `callDBus` the bridge is type-checked against takes one string,
|
||||
so `tsc` refuses anything else.
|
||||
@@ -0,0 +1,200 @@
|
||||
# Platform
|
||||
|
||||
komorebi for KDE runs in a KDE Plasma session, as a client of KWin. Most of the details on
|
||||
this page are also written down next to the code that depends on them; this page
|
||||
centralizes them: which KWin it needs, how it finds it, what it creates in the session and
|
||||
leaves behind, every environment variable it reads, and what happens when something is
|
||||
missing.
|
||||
|
||||
Facts that change with KWin and Plasma releases are dated. Unless a fact says otherwise, it
|
||||
was measured with KWin 6.8.1, Plasma 6.8.1 and Qt 6.12.0 from nixpkgs `d3b13ad`
|
||||
(2026-10-24), in NixOS virtual machines under QEMU on an Apple silicon Mac (HVF).
|
||||
|
||||
## Vocabulary
|
||||
|
||||
- **Session**: one user's Plasma login: KWin, the Plasma shell, and a D-Bus session bus they
|
||||
share.
|
||||
- **Session bus**: the D-Bus bus of the session. komorebi and KWin talk over it.
|
||||
- **KWin**: the compositor and window manager of Plasma, `kwin_wayland`.
|
||||
- **Script**: a JavaScript program that KWin runs inside itself, through its scripting API.
|
||||
komorebi's is the **bridge**, [`bridge.js`](../../komorebi/src/kwin/bridge.js).
|
||||
- **Output**: KWin's name for a monitor.
|
||||
- **Headless KWin**: `kwin_wayland --virtual`, a KWin with virtual outputs and no display,
|
||||
which the VM tests run ([vm-tests.md](vm-tests.md)).
|
||||
|
||||
## What komorebi needs
|
||||
|
||||
### KWin 6.8 or newer
|
||||
|
||||
KWin 6.8 is the oldest KWin komorebi supports. The bridge reads the work area of each output
|
||||
for the virtual desktop that output shows (`workspace.currentDesktopForScreen`), which is part
|
||||
of the per-output virtual desktops KWin 6.8 introduced. At time of writing (2026-10-27) KWin
|
||||
6.8.1 is the only version komorebi has been run against; 6.7 and older have not been tried.
|
||||
|
||||
### Wayland
|
||||
|
||||
komorebi runs in a Plasma Wayland session. There is no X11 session of Plasma 6.8 to run it in.
|
||||
Programs that run on XWayland inside the Wayland session are windows like any other; KWin
|
||||
reports their X11 class (`XTerm`) where a Wayland client has its app id (`foot`,
|
||||
`org.kde.konsole`).
|
||||
|
||||
### The session bus
|
||||
|
||||
komorebi finds KWin on the session bus, and nowhere else. It needs to own the name
|
||||
`org.komorebi.Bridge` there, and KWin has to own `org.kde.KWin`. See
|
||||
[`DBUS_SESSION_BUS_ADDRESS`](#dbus_session_bus_address) for how the bus is found.
|
||||
|
||||
## Environments
|
||||
|
||||
The same questions, for each place the daemon can be started:
|
||||
|
||||
1. How it finds KWin.
|
||||
2. What it does there.
|
||||
3. What it leaves behind.
|
||||
|
||||
### A Plasma session
|
||||
|
||||
#### How komorebi finds KWin
|
||||
|
||||
Through the session bus. When the daemon is started from the session (a terminal, an
|
||||
autostart entry, a user unit of the session), the bus is already in its environment.
|
||||
|
||||
#### What komorebi does
|
||||
|
||||
It connects to the session bus as `org.komorebi.Bridge`, writes the bridge to
|
||||
`$XDG_RUNTIME_DIR/komorebi/bridge.js`, and asks KWin to load it under the name
|
||||
`komorebi-bridge`, replacing a script of that name left by an earlier run. The script reports
|
||||
the windows and outputs it finds, and from then on every window that opens, closes, takes the
|
||||
focus, is minimized or brought back, moves or changes size. The daemon waits up to 10 seconds
|
||||
for the first report and does not start without it.
|
||||
|
||||
The KWin backend only reads the desktop. Every command the window manager gives it (move,
|
||||
focus, raise, minimize, close a window, move the pointer) fails with "the KWin backend only
|
||||
reads the desktop", which the window manager logs.
|
||||
|
||||
#### What komorebi leaves behind
|
||||
|
||||
When it is stopped with `SIGINT` or `SIGTERM` it asks KWin to unload the script. The file
|
||||
`bridge.js` stays in the runtime directory, which is removed at logout. A daemon that is
|
||||
killed leaves the script running in KWin, reporting to nobody, until the next start replaces
|
||||
it or the session ends.
|
||||
|
||||
### A headless KWin
|
||||
|
||||
`kwin_wayland --virtual` behaves like the KWin of a session for everything komorebi does:
|
||||
the VM tests run the daemon against one, on a private session bus of its own. It cannot draw
|
||||
(see [Rendering](#rendering-in-virtual-machines-and-over-rdp)), so nothing that depends on
|
||||
pixels can be tested against it.
|
||||
|
||||
### No KWin
|
||||
|
||||
Anywhere else (a session that is not Plasma, a Linux console, a Mac), the daemon exits with an
|
||||
error that names what is missing:
|
||||
|
||||
- "could not connect to the session bus as org.komorebi.Bridge": there is no session bus at
|
||||
the address the environment gives, or another komorebi owns the name.
|
||||
- "KWin is not on the session bus": the bus is there and KWin is not.
|
||||
- "KWin loaded the bridge script but it never reported in": KWin is there, and the script did
|
||||
not run. KWin logs a script's errors to the journal (`journalctl --user -t kwin_wayland`).
|
||||
|
||||
`komorebi --fake-backend` runs the window manager against an in-memory desktop instead, which
|
||||
works anywhere, for development and testing.
|
||||
|
||||
## D-Bus
|
||||
|
||||
What komorebi owns, on the session bus:
|
||||
|
||||
| Name | Object | Interface | Member | Called by |
|
||||
|---|---|---|---|---|
|
||||
| `org.komorebi.Bridge` | `/org/komorebi/Bridge` | `org.komorebi.Bridge` | `Event(s)` | the bridge, for every event |
|
||||
| `org.komorebi.Bridge` | `/org/komorebi/Bridge/Poll` | `org.komorebi.Bridge.Poll` | `Poll(s) -> s` | the bridge, for commands |
|
||||
|
||||
Both take a JSON string. `Poll` is held: the daemon answers it when it has commands for the
|
||||
bridge, or with an empty list after 10 seconds, and the bridge then calls it again.
|
||||
|
||||
What komorebi calls:
|
||||
|
||||
| Name | Object | Interface | Member | When |
|
||||
|---|---|---|---|---|
|
||||
| `org.kde.KWin` | `/Scripting` | `org.kde.kwin.Scripting` | `unloadScript(s)`, `loadScript(ss) -> i` | at start; `unloadScript` again at exit |
|
||||
| `org.kde.KWin` | `/Scripting/Script<id>` | `org.kde.kwin.Script` | `run()` | at start |
|
||||
|
||||
## Files and directories
|
||||
|
||||
| Path | What | Lifetime |
|
||||
|---|---|---|
|
||||
| `$XDG_RUNTIME_DIR/komorebi/bridge.js` | the script KWin loads, with its configuration (the D-Bus names and the hold time of `Poll`) on its first line | written at every start; the directory is removed at logout |
|
||||
|
||||
Without `XDG_RUNTIME_DIR` the directory is `komorebi` in the local data directory instead
|
||||
(see [`XDG_DATA_HOME`](#xdg_data_home)).
|
||||
|
||||
## Environment variables
|
||||
|
||||
### `DBUS_SESSION_BUS_ADDRESS`
|
||||
|
||||
The address of the session bus. If it is not set, komorebi uses `$XDG_RUNTIME_DIR/bus`, where
|
||||
systemd puts the session bus, and `/run/user/<uid>/bus` when `XDG_RUNTIME_DIR` is not set
|
||||
either. An address with no bus behind it is the first error under [No KWin](#no-kwin).
|
||||
|
||||
### `XDG_RUNTIME_DIR`
|
||||
|
||||
The runtime directory of the user, normally `/run/user/<uid>`: komorebi writes the bridge to
|
||||
the `komorebi` directory in it.
|
||||
|
||||
### `XDG_DATA_HOME`
|
||||
|
||||
Read only when `XDG_RUNTIME_DIR` is not set: the bridge is then written to
|
||||
`$XDG_DATA_HOME/komorebi` (`~/.local/share/komorebi` if `XDG_DATA_HOME` is not set either).
|
||||
|
||||
### `RUST_LOG`
|
||||
|
||||
Which log lines the daemon prints, in the syntax of `tracing-subscriber`'s `EnvFilter`
|
||||
(`komorebi=debug`, `komorebi::kwin=trace`). If it is not set, `--log-level` sets it to
|
||||
`komorebi=<level>`; `--log-level` defaults to `info`.
|
||||
|
||||
### `RUST_LIB_BACKTRACE`
|
||||
|
||||
Whether an error carries a backtrace. komorebi sets it to `1` when it is not set.
|
||||
|
||||
### `NO_COLOR`
|
||||
|
||||
When set to anything but the empty string, the log has no ANSI colors, which is what a log
|
||||
written to a file wants. The VM tests set it.
|
||||
|
||||
## The bridge's environment
|
||||
|
||||
What the KWin scripting API offers a script, and what it does not, at time of writing
|
||||
(2026-10-27). The bridge is written around these, and
|
||||
[`kwin-api.d.ts`](../../komorebi/src/kwin/kwin-api.d.ts) declares the part of the API it
|
||||
uses, which `tsc` checks it against with ECMAScript 2016's library and nothing else. Node and
|
||||
TypeScript are needed only for those checks: in the session the bridge needs KWin alone.
|
||||
|
||||
- `callDBus` sends a JavaScript number as a 32-bit integer, and a call whose arguments do not
|
||||
match the method's signature fails without a word. The bridge sends every argument as a
|
||||
JSON string.
|
||||
- The callback of `callDBus` is called on success only. A call that fails (the daemon has
|
||||
exited) is never answered, so the bridge re-issues `Poll` from a one-second `QTimer` when
|
||||
the last one is older than the hold time plus 3 seconds.
|
||||
- There is a `QTimer`, but no `setTimeout`, and no `Qt` global: a rectangle is assigned as a
|
||||
plain `{x, y, width, height}` object.
|
||||
- KWin activates a new window before it announces it (`windowActivated` comes before
|
||||
`windowAdded`). The bridge announces a window it has not seen when it is activated, so that
|
||||
no event names a window before the event that announces it.
|
||||
- A script's `print()` goes to KWin's log, which shows it only when the `kwin_scripting`
|
||||
logging category is enabled (`QT_LOGGING_RULES="kwin_scripting.debug=true"`, as the VM
|
||||
tests do).
|
||||
|
||||
## Rendering in virtual machines and over RDP
|
||||
|
||||
At time of writing (2026-10-27):
|
||||
|
||||
- A headless KWin cannot render. Mesa's software rasterizer needs `CREATE_DUMB` on the
|
||||
primary DRM node and the virtual backend offers only the render node, so KWin reports that
|
||||
compositing is active while every frame fails, and screenshots are black. KWin does not start at all with no DRM
|
||||
device. The VM nodes give the guest a `virtio-gpu` device for that reason.
|
||||
- In a QEMU guest everything is drawn in software (llvmpipe on the guest's CPUs): the
|
||||
nixpkgs QEMU has only the 2D `virtio-gpu`. KWin then refuses to load animating effects
|
||||
unless `KWIN_EFFECTS_FORCE_ANIMATIONS=1` is in its environment, which the VM nodes set.
|
||||
- KRDP 6.8.1, Plasma's RDP server, does not start without a TLS certificate and key, although
|
||||
its `--help` says it generates one; it logs the error and keeps running with nothing
|
||||
listening. `xrdp` is not an option, since there is no X11 session to start.
|
||||
+17
-2
@@ -52,6 +52,9 @@ D-Bus session bus) and `kwin-virtual@NAME` (`kwin_wayland --virtual` with its
|
||||
own Wayland socket), so a test can run several sessions side by side and none
|
||||
touches another. A virtual KWin has no real output, so it cannot draw: it can
|
||||
place windows and answer questions about them, but it cannot take a screenshot.
|
||||
A third unit, `komorebi@NAME`, runs the daemon in a session, built from the
|
||||
commit under test and installed on the node; it is not bound to KWin's unit, so
|
||||
a test can restart KWin under a running daemon.
|
||||
|
||||
KWin wants a DRM render node, which needs a GPU. The test driver gives every aarch64 guest one
|
||||
`virtio-gpu-pci`; on x86_64 the node adds it, unless `komorebi.vm.renderNode` is false.
|
||||
@@ -92,6 +95,15 @@ the run.
|
||||
- `Kde.session(machine, scale=)` attaches to the session node's own Plasma
|
||||
session once its desktop is up, and `shot(name)` returns a screenshot as a
|
||||
Pillow image, keeping `NAME.png` in the output directory.
|
||||
- `activate`, `minimize` and `close` act on a window through `kdotool`, as the
|
||||
user would.
|
||||
- `start_komorebi(*args)` starts the daemon in the session, `stop_komorebi()`
|
||||
stops it with `SIGTERM`, `komorebi_log()` returns its log since it was last
|
||||
started, and `script_loaded()` asks KWin whether it runs the bridge.
|
||||
- `daemon` reads the daemon's view of the desktop from that log, started with
|
||||
`--log-level debug`: `daemon.events(log)` lists the window events it handled,
|
||||
in order, and `daemon.window_ids(log)` maps a window's title to the daemon's id
|
||||
for it.
|
||||
|
||||
Poll for what the test needs with `wait_for`. A fixed sleep is too long on
|
||||
KVM and too short on TCG.
|
||||
@@ -133,7 +145,9 @@ The script prints where the artifacts (screenshots, logs) are, under
|
||||
## Writing a test
|
||||
|
||||
1. Create `nix/tests/vm/suites/NAME/default.nix` and `test.py`. Start from
|
||||
[`suites/kwin`](../../nix/tests/vm/suites/kwin).
|
||||
[`suites/kwin`](../../nix/tests/vm/suites/kwin), or from
|
||||
[`suites/kwin-events`](../../nix/tests/vm/suites/kwin-events) for a test that
|
||||
runs the daemon.
|
||||
2. Pick the tier. `fast` suites are the pull request gate and should take
|
||||
minutes. `slow` suites run on `master` and every night.
|
||||
3. Run it: `just vm NAME`.
|
||||
@@ -150,4 +164,5 @@ Add `git add -N` for new files first: a flake only sees files git knows about.
|
||||
build `driverInteractive` instead of `driver`, run it, and call `start_all()`
|
||||
and `machine.shell_interact()` at its prompt. In the guest, KWin's own log is
|
||||
`journalctl --user -u kwin-virtual@NAME` as the test user, and a KWin script's
|
||||
`print()` goes to the same journal.
|
||||
`print()` goes to the same journal. The daemon's log is
|
||||
`/run/user/1000/komorebi-NAME.log`.
|
||||
Reference in new issue
Block a user