Files
komorebi/nix/tests/vm/suites/kwin-contract/test.py
T
LGUG2Z 62e37e44cf feat(kwin): workspaces are KWin's own desktops
komorebi's workspaces exist on Windows and macOS because neither has a virtual
desktop API: switching hides every window of the workspace being left. KWin has
virtual desktops, per output since 6.8, so here workspace i is KWin's desktop i
on every monitor, and each monitor shows one. KWin shows, hides and focuses the
windows of a desktop; komorebi asks for a switch, puts a window on a desktop,
and follows what anything else does.

The Backend gets the workspace methods (ensure_workspaces, switch_workspace,
set_window_workspace, window_workspace, current_workspaces), and the fake its
desktops. The bridge reports the desktops, the desktop each output shows and a
window's desktops, and carries out output_desktop and desktop commands, typed
and tested with the fixtures like the rest. The table turns a switch or a move
komorebi did not make into a WorkspaceChange or a WorkspaceMove, and records its
own when it sends them, so that KWin's report of those raises nothing. The
window manager follows a switch with no command in reply
(adopt_workspace_change), puts windows on their desktops before a desktop is
shown (focusing a window first would take the monitor back), and loads the
windows open at startup onto the workspace of their desktop.

Per-output desktops are a kwinrc setting that is off by default, and
kwriteconfig6 was not on the PATH of the user service in a live session, so
komorebi edits kwinrc itself (kwin::kwinrc) and puts the user's value back when
it stops, also after a crash.

focus-workspace, move-to-workspace and send-to-workspace come with this commit,
since the tests of switching and moving, and vm-workspace-switch, need them; the
named and cycling workspace commands follow. vm-workspace-switch runs the daemon
against a real KWin; vm-kwin-contract checks what komorebi relies on from KWin's
per-output desktops, on a headless KWin with two outputs and no komorebi.
2026-10-28 07:20:37 -07:00

173 lines
6.6 KiB
Python

# The contract of KWin's per-output virtual desktops that komorebi's workspaces rely on, checked
# against a headless KWin with two outputs and no komorebi: workspace `i` is desktop `i`, and each
# output shows a desktop of its own. What has to hold, and what breaks if it does not:
#
# - with `[Windows] PerOutputVirtualDesktops=true` in kwinrc, a script can show a desktop on one
# output and the other output keeps its own (otherwise a workspace switch switches every monitor);
# - KWin says so with `workspace.currentDesktopChanged`, which the bridge listens to;
# - KWin focuses a window of the desktop it switched an output to, by itself (komorebi asks for no
# focus after a switch it did not make);
# - activating a window on another desktop of its output (an app raising itself) makes KWin switch
# that output to the window's desktop (which komorebi then follows);
# - a window moved by a script so that its middle is on the other output keeps its desktops: KWin
# moves a window to the new output's desktop only during an interactive move;
# - a window put on another desktop from outside raises `desktopsChanged` on the window, which the
# bridge listens to.
#
# docs/dev/PLATFORM.md records these as the facts of KWin that komorebi depends on.
from itertools import count
from komorebi_vm import Checks, Kde
start_all()
machine.wait_for_unit("multi-user.target")
machine.wait_for_unit("user@1000.service")
checks = Checks()
# per-output desktops are read when KWin starts, as komorebi's users have them after their first run
machine.succeed(
"runuser -u komorebi -- sh -c 'mkdir -p ~/.config"
' && printf "[Windows]\\nPerOutputVirtualDesktops=true\\n" > ~/.config/kwinrc\''
)
kde = Kde.headless(machine, "main", size=(1920, 1080), outputs=2, scale=SCALE)
# what every script is given: the desktop each output shows, and a window by its title
HELPERS = """
function cur() {
return workspace.screens.map(function (o) {
return workspace.currentDesktopForScreen(o).name;
}).join("/");
}
function win(title) {
return workspace.windowList().filter(function (w) { return w.caption === title; })[0];
}
function names(ds) {
return ds.map(function (d) { return d.name; }).join(",");
}
"""
def script(name: str, body: str, keep: bool = False) -> list[str]:
return kde.kwin_script(name, HELPERS + body + '\nreport("done");\n', keep=keep)
def shows() -> str:
"""What each output shows, as `first/second`."""
lines = script(f"shows-{next(counter)}", "report(cur());")
return lines[0] if lines else ""
counter = count(1)
print("== 1. two outputs, one desktop, and per-output desktops on")
outputs = script(
"outputs", 'report(workspace.screens.map(function (o) { return o.name; }).join(" "));'
)
print(f" outputs: {outputs}")
names_of = outputs[0].split() if outputs else []
checks.check("KWin has two outputs", len(names_of) == 2)
checks.check("and one desktop", kde.desktops() == 1)
checks.check(
"kwinrc has per-output desktops on",
kde.kwinrc("Windows", "PerOutputVirtualDesktops") == "true",
)
print("== 2. a desktop created over D-Bus is a desktop of every output")
kde.sh(
"busctl --user call org.kde.KWin /VirtualDesktopManager"
" org.kde.KWin.VirtualDesktopManager createDesktop us 1 two > /dev/null"
)
checks.check("KWin has two desktops", kde.wait_for(lambda: kde.desktops() == 2))
desktops = script("desktops", "report(names(workspace.desktops));")
checks.check(f"the script sees them in order: {desktops}", desktops[-1:] == ["Desktop 1,two"])
checks.check("both outputs show the first", shows() == "Desktop 1/Desktop 1")
print("== 3. one output shows another desktop, and the other keeps its own")
script(
"listen",
'workspace.currentDesktopChanged.connect(function () { report("changed", cur()); });',
keep=True,
)
script(
"switch", "workspace.setCurrentDesktopForScreen(workspace.desktops[1], workspace.screens[0]);"
)
checks.check(
"the first output shows the second desktop, and the second output the first",
kde.wait_for(lambda: shows() == "two/Desktop 1"),
)
checks.check(
"KWin said so with currentDesktopChanged",
kde.wait_for(lambda: "changed two/Desktop 1" in kde.kwin_reports("listen")),
)
script("back", "workspace.setCurrentDesktopForScreen(workspace.desktops[0], workspace.screens[0]);")
checks.check("and back", kde.wait_for(lambda: shows() == "Desktop 1/Desktop 1"))
print("== 4. KWin focuses a window of the desktop it switched an output to")
for title in ["w1", "w2"]:
kde.spawn(f"foot -T {title} sleep 3000")
kde.wait_for(lambda title=title: kde.wid(title) != "", timeout=30)
script(
"place",
"""
var d = workspace.desktops;
var w1 = win("w1"), w2 = win("w2");
w1.desktops = [d[0]];
w1.frameGeometry = { x: 100, y: 100, width: 600, height: 400 };
w2.desktops = [d[1]];
w2.frameGeometry = { x: 800, y: 100, width: 600, height: 400 };
workspace.activeWindow = w1;
""",
)
checks.check(
"w1 has the focus, on the first desktop",
kde.wait_for(lambda: kde.title(kde.active()) == "w1"),
)
script(
"to-two", "workspace.setCurrentDesktopForScreen(workspace.desktops[1], workspace.screens[0]);"
)
checks.check(
"after the first output shows the second desktop, w2, which is on it, has the focus",
kde.wait_for(lambda: kde.active() != "" and kde.title(kde.active()) == "w2"),
)
print("== 5. activating a window on another desktop of its output switches that output to it")
script("raise", 'workspace.activeWindow = win("w1");')
checks.check(
"the first output shows the first desktop again",
kde.wait_for(lambda: shows() == "Desktop 1/Desktop 1"),
)
checks.check("and w1 has the focus", kde.wait_for(lambda: kde.title(kde.active()) == "w1"))
print("== 6. a window a script moves onto the other output keeps its desktops")
script("cross", 'win("w1").frameGeometry = { x: 2300, y: 100, width: 600, height: 400 };')
def where_w1() -> str:
lines = script(
f"where-{next(counter)}",
'var w = win("w1"); report(w.output.name + " " + names(w.desktops));',
)
return lines[0] if lines else ""
second = names_of[1] if len(names_of) == 2 else "?"
checks.check(
f"w1 is on {second} now, and still on the first desktop only",
kde.wait_for(lambda: where_w1() == f"{second} Desktop 1"),
)
print("== 7. a window put on another desktop from outside says so")
script(
"watch",
'var w = win("w1"); w.desktopsChanged.connect(function () { report("moved", names(w.desktops)); });',
keep=True,
)
kde.sh(f"kdotool set_desktop_for_window {kde.wid('w1')} 2")
checks.check(
"w1's desktopsChanged was raised, with the second desktop",
kde.wait_for(lambda: "moved two" in kde.kwin_reports("watch")),
)
kde.stop()
checks.finish()