X11 dock bridge: show and control host EWMH windows under --x11
CI / test (push) Failing after 2m37s

Under --x11 with a host EWMH window manager (e.g. Openbox in Gamerig),
Chandra is one top-level X11 client while other apps run directly on Xorg,
so the dock only saw Chandra's Wayland windows.

- compositor/x11: intern EWMH/ICCCM atoms and track _NET_CLIENT_LIST,
  _NET_ACTIVE_WINDOW, titles, and window states via PropertyChangeMask,
  filtering non-taskbar/child windows and adding focus, close, minimize,
  and toggle-maximize EWMH/ICCCM actions.
- compositor/shell: add resolve_external_app_id (matching WM_CLASS and
  StartupWMClass against installed .desktop files) and publish_windows.
- compositor/host: bridge ExternalWindow entries with bit-30 tagged IDs
  into shell.windows/win_list while keeping Wayland wm.windows and session
  snapshots untouched, and route activate_task / window_command actions.
- compositor/app, compositor/bin, docs/x11.md: wire external window sync
  before X11 input handling and register ExternalWindowHandler.

Verified: moon test --target native compositor/x11 compositor/shell (38/38),
external_windows_test.mbt + dock_test.mbt (3/3), build-compositor-wasm.sh,
and moon build --target native compositor/bin.
This commit is contained in:
nandi committed 2026-11-27 14:33:26 -08:00
1 parent 15b7c3f0e3
commit 1871d50a3e
12 files changed
+949 -68

No files matched your search

+27 -40
View File
@@ -309,6 +309,9 @@ pub(all) struct ChandraCompositor {
/// 1 minimize, 2 maximize, 3 close. A press shows pressed; the release over it acts.
mut deco_hover : (UInt64, Int)?
mut deco_pressed : (UInt64, Int)?
/// External windows from the host X11 WM (`--x11`), bridged into the dock/panel only.
mut ext_windows : Array[ExternalWindow]
mut ext_handler : ExternalWindowHandler?
}
///|
@@ -443,6 +446,8 @@ pub fn ChandraCompositor::new(
rec_press: false,
last_recording: "",
kiosk: None,
ext_windows: [],
ext_handler: None,
}
// Client popups (menus) are placed to stay on screen: the server asks for the output's
// rectangle relative to the parent's content origin when it applies constraint adjustment.
@@ -1118,46 +1123,7 @@ fn ChandraCompositor::build_scene(
@wm.LayoutMode::Monocle => 1
@wm.LayoutMode::Floating => 2
}
// The task list shows every window, minimized ones too (so they can be restored).
let wins : Array[@shell.WinInfo] = []
for wid in order {
match self.windows.get(wid) {
Some(win) =>
wins.push({
id: win.id,
title: if win.title.length() > 0 { win.title } else { win.app_id },
focused: win.focused,
minimized: false,
maximized: win.maximized,
})
None => ()
}
}
for id, r in self.wm.windows {
if r.minimized {
match self.windows.get(id) {
Some(win) =>
wins.push({
id,
title: if win.title.length() > 0 { win.title } else { win.app_id },
focused: false,
minimized: true,
maximized: win.maximized,
})
None => ()
}
}
}
self.shell.windows = wins
// The window list the `windows` interface serves to wasm widgets (task-bar order).
let entries : Array[@wasm.WindowEntry] = []
for w in wins {
match self.windows.get(w.id) {
Some(win) => entries.push({ id: w.id, title: w.title, app_id: win.app_id, focused: w.focused })
None => ()
}
}
self.shell.win_list = entries
self.sync_shell_windows()
self.shell.screenshot_state = (if self.rec is Some(_) { 1 } else { 0 }) |
(if self.rec_pointer { 2 } else { 0 })
// A window over the whole desktop (maximized, fullscreen, monocle) pauses a desktop module's
@@ -1714,6 +1680,16 @@ fn ChandraCompositor::activate_window(self : ChandraCompositor, wid : UInt64) ->
///| Restore a minimized window (or focus a visible one) from its task-bar pill.
fn ChandraCompositor::activate_task(self : ChandraCompositor, wid : UInt64) -> Unit {
match external_host_id(wid) {
Some(xid) => {
match self.ext_handler {
Some(h) => (h.focus)(xid)
None => ()
}
return
}
None => ()
}
let minimized = match self.wm.windows.get(wid) {
Some(r) => r.minimized
None => return
@@ -1735,6 +1711,17 @@ fn ChandraCompositor::window_command(
wid : UInt64,
cmd : @shell.WindowCommand,
) -> Unit {
match external_host_id(wid) {
Some(xid) => {
match self.ext_handler {
Some(h) => (h.command)(xid, cmd)
None => ()
}
self.damage.mark_full()
return
}
None => ()
}
if self.wm.windows.get(wid) is None {
return
}
+125
View File
@@ -0,0 +1,125 @@
///| Bridge external windows (from a host X11 EWMH window manager under `--x11`) into the
/// shell's task/dock window list without mixing them into Chandra's own Wayland window table.
///
/// X11 window IDs (XIDs) are 29-bit unsigned values (`0..0x1FFF_FFFF`). Tagging them with bit 30
/// (`0x4000_0000`) keeps them disjoint from Chandra's sequential Wayland window IDs while staying
/// strictly positive in signed 32-bit ints, as required by the wasm `windows` / `mp_wasm_take_focus`
/// boundary.
///| Bit 30 tag distinguishing host-managed external windows from Wayland client windows.
pub let external_window_tag : UInt64 = 0x4000_0000UL
///| Encode a 29-bit host X11 window ID into a synthetic compositor window ID.
pub fn external_wid(host_id : Int) -> UInt64 {
external_window_tag | (host_id & 0x1FFF_FFFF).reinterpret_as_uint().to_uint64()
}
///| Extract the host X11 window ID from a synthetic external window ID, or None for Wayland windows.
pub fn external_host_id(wid : UInt64) -> Int? {
if (wid & external_window_tag) != 0UL {
Some((wid & 0x1FFF_FFFFUL).to_int())
} else {
None
}
}
///| External top-level window reported by the host display server / WM.
pub(all) struct ExternalWindow {
host_id : Int
wm_class : String
wm_instance : String
title : String
focused : Bool
minimized : Bool
maximized : Bool
} derive(Eq, Debug)
///| Callbacks executed when the dock or panel acts on an external window.
pub(all) struct ExternalWindowHandler {
focus : (Int) -> Unit
command : (Int, @shell.WindowCommand) -> Unit
}
///| Register the handler for focus/close/minimize/maximize requests on external windows.
pub fn ChandraCompositor::set_external_window_handler(
self : ChandraCompositor,
handler : ExternalWindowHandler,
) -> Unit {
self.ext_handler = Some(handler)
}
///| Rebuild `shell.windows` and `shell.win_list` from Chandra's Wayland windows followed by any
/// external windows from the host window manager.
pub fn ChandraCompositor::sync_shell_windows(self : ChandraCompositor) -> Unit {
let order = self.wm.stacking_order()
// The task list shows every window, minimized ones too (so they can be restored).
let wins : Array[@shell.WinInfo] = []
for wid in order {
match self.windows.get(wid) {
Some(win) =>
wins.push({
id: win.id,
title: if win.title.length() > 0 { win.title } else { win.app_id },
focused: win.focused,
minimized: false,
maximized: win.maximized,
})
None => ()
}
}
for id, r in self.wm.windows {
if r.minimized {
match self.windows.get(id) {
Some(win) =>
wins.push({
id,
title: if win.title.length() > 0 { win.title } else { win.app_id },
focused: false,
minimized: true,
maximized: win.maximized,
})
None => ()
}
}
}
// The window list the `windows` interface serves to wasm widgets (task-bar order).
let entries : Array[@wasm.WindowEntry] = []
for w in wins {
match self.windows.get(w.id) {
Some(win) => entries.push({ id: w.id, title: w.title, app_id: win.app_id, focused: w.focused })
None => ()
}
}
for ew in self.ext_windows {
let wid = external_wid(ew.host_id)
let app_id = self.shell.resolve_external_app_id(ew.wm_class, ew.wm_instance, ew.title)
let title = if ew.title.length() > 0 { ew.title } else { app_id }
wins.push({
id: wid,
title,
focused: ew.focused,
minimized: ew.minimized,
maximized: ew.maximized,
})
entries.push({ id: wid, title, app_id, focused: ew.focused })
}
self.shell.windows = wins
self.shell.win_list = entries
}
///| Replace the external window list, immediately refresh shell tables if booted, and mark dirty.
pub fn ChandraCompositor::set_external_windows(
self : ChandraCompositor,
wins : Array[ExternalWindow],
) -> Unit {
if wins == self.ext_windows {
return
}
self.ext_windows = wins
self.sync_shell_windows()
if self.shell.booted {
self.shell.publish_windows()
}
self.damage.mark_full()
self.last_sig = None
}
+152
View File
@@ -0,0 +1,152 @@
///| Host integration tests for external X11 EWMH windows bridged into the dock/panel.
/// Needs the dock module built: scripts/build-modules.sh.
///| Pick row `label` of the open right-click menu (26 px title, then 28 px per row).
fn ext_dock_pick(comp : @host.ChandraCompositor, label : String, t : UInt) -> Unit raise {
let i = comp.shell.menu_rows.map(r => r.label).search(label).unwrap()
let m = comp.shell.menu_rect
click_at(comp, m.x + m.w / 2, m.y + 26 + 28 * i + 14, t)
}
///| Centres of the dock's titled task pills in panel order, from its rendered FillRect pills.
fn ext_pill_centers(comp : @host.ChandraCompositor, slot : String) -> Array[(Int, Int)] {
let r = comp.shell.slot_rect(slot)
let out : Array[(Int, Int)] = []
for op in comp.shell.slot_ops(slot) {
match op {
FillRect(f) if f.rect.y == 3 && f.rect.h.reinterpret_as_int() == r.h - 6 && f.rect.w >= 60U =>
out.push((r.x + f.rect.x + f.rect.w.reinterpret_as_int() / 2, r.y + r.h / 2))
_ => ()
}
}
out
}
///|
test "external X11 windows show in the dock and route clicks, shortcuts, and menu commands" {
let socket_path = "/tmp/wayland-moon-ext-" + @sys.getpid().to_string() + ".sock"
let comp = @host.ChandraCompositor::new(socket_path, @scene.ThemeTokens::moon_night(), 1280, 720).unwrap()
comp.shell.apps.clear()
comp.shell.exec.clear()
comp.shell.wm_classes.clear()
for a in [
("gamerig-astra", "Space Rage: Astra Incognita", "Game", "S", "gamerig-open astra", "astra"),
("com.mitchellh.ghostty", "Ghostty", "System", "G", "gamerig-open terminal", "com.mitchellh.ghostty"),
("steam", "Steam", "Game", "S", "gamerig-open steam", "steam"),
] {
comp.shell.apps.push({ id: a.0, name: a.1, comment: "", category: a.2, glyph: a.3 })
comp.shell.exec.set(a.0, a.4)
comp.shell.wm_classes.set(a.5, a.0)
}
comp.shell.apps_dirty = true
ignore(comp.compose_frame())
let slot = comp.shell.slot_of("dock")
assert_true(slot.length() > 0)
for s in comp.shell.slots() {
let m = comp.shell.slot_module(s)
if m == "inspector" || m == "layout" || m == "screenshot" {
ignore(comp.shell.unload_slot(s))
}
}
comp.shell.set_module_setting(slot, "pinned", "gamerig-astra;com.mitchellh.ghostty;steam")
ignore(comp.compose_frame())
let focuses : Array[Int] = []
let cmds : Array[(Int, @shell.WindowCommand)] = []
comp.set_external_window_handler({
focus: fn(xid) { focuses.push(xid) },
command: fn(xid, cmd) { cmds.push((xid, cmd)) },
})
let ghostty_xid = 0x0120_0007
let astra_xid = 0x0140_0003
let hades_xid = 0x0160_0011
let ext_wins : Array[@host.ExternalWindow] = [
{
host_id: ghostty_xid,
wm_class: "com.mitchellh.ghostty",
wm_instance: "ghostty",
title: "ubuntu@rig: ~",
focused: false,
minimized: false,
maximized: false,
},
{
host_id: astra_xid,
wm_class: "astra",
wm_instance: "astra",
title: "Space Rage: Astra Incognita",
focused: false,
minimized: false,
maximized: true,
},
{
host_id: hades_xid,
wm_class: "steam_app_1145360",
wm_instance: "steam_app_1145360",
title: "Hades",
focused: false,
minimized: false,
maximized: false,
},
]
comp.set_external_windows(ext_wins)
ignore(comp.compose_frame())
// Idle pinned icon (steam) comes first, then the three running task pills:
// gamerig-astra (merged with its pin via StartupWMClass "astra"), com.mitchellh.ghostty, steam_app_1145360.
assert_eq(comp.shell.dock_item_ids(), ["steam", "gamerig-astra", "com.mitchellh.ghostty", "steam_app_1145360"])
// Left-clicking Ghostty's pill routes its tagged XID cleanly through dock.wasm -> activate_task -> focus handler.
let pills = ext_pill_centers(comp, slot)
assert_eq(pills.length(), 3)
let (gx, gy) = pills[1]
click_at(comp, gx, gy, 10U)
assert_eq(focuses, [ghostty_xid])
// Super+2 targets the 2nd dock item (gamerig-astra) and routes to astra_xid.
comp.handle_input(@nested.InputEvent::Key(125U, true), 20U)
comp.handle_input(@nested.InputEvent::Key(3U, true), 21U)
comp.handle_input(@nested.InputEvent::Key(3U, false), 22U)
comp.handle_input(@nested.InputEvent::Key(125U, false), 23U)
ignore(comp.compose_frame())
assert_eq(focuses, [ghostty_xid, astra_xid])
// Right-clicking Ghostty's pill offers Minimize, Maximize, Quit and routes each command.
click_at(comp, gx, gy, 30U, btn=273U)
assert_true(comp.shell.menu_open)
assert_true(comp.shell.menu_rows.map(r => r.label).contains("Minimize"))
assert_true(comp.shell.menu_rows.map(r => r.label).contains("Maximize"))
assert_true(comp.shell.menu_rows.map(r => r.label).contains("Quit"))
ext_dock_pick(comp, "Minimize", 35U)
click_at(comp, gx, gy, 40U, btn=273U)
ext_dock_pick(comp, "Maximize", 45U)
click_at(comp, gx, gy, 50U, btn=273U)
ext_dock_pick(comp, "Quit", 55U)
assert_eq(cmds, [
(ghostty_xid, @shell.WindowCommand::Minimize),
(ghostty_xid, @shell.WindowCommand::ToggleMaximize),
(ghostty_xid, @shell.WindowCommand::Close),
])
// When Ghostty is minimized, its menu changes to Restore and picking it sends Restore.
comp.set_external_windows([
{ ..ext_wins[0], minimized: true },
ext_wins[1],
ext_wins[2],
])
ignore(comp.compose_frame())
click_at(comp, gx, gy, 60U, btn=273U)
assert_true(comp.shell.menu_rows.map(r => r.label).contains("Restore"))
ext_dock_pick(comp, "Restore", 65U)
assert_eq(cmds[cmds.length() - 1], (ghostty_xid, @shell.WindowCommand::Restore))
// External X11 windows are never persisted in Chandra's Wayland session snapshot.
assert_eq(comp.session_snapshot(0UL).length(), 0)
// Closing external windows removes the running pills and unpinned item from the dock.
comp.set_external_windows([])
ignore(comp.compose_frame())
assert_eq(comp.shell.dock_item_ids(), ["gamerig-astra", "com.mitchellh.ghostty", "steam"])
}