Files
chandra/compositor/host/external_windows.mbt
nandi 1871d50a3e
CI / test (push) Failing after 2m37s
X11 dock bridge: show and control host EWMH windows under --x11
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.
2026-11-27 14:33:26 -08:00

126 lines
4.1 KiB
MoonBit

///| 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
}