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

+418 -12
View File
@@ -17,6 +17,17 @@
/// The keymap given to Wayland clients is the X server's own (`xkbcomp -xkb $DISPLAY -`), so
/// keycodes, layout and options match what Xorg was configured with.
///| One top-level X11 client window reported by the host EWMH window manager (`_NET_CLIENT_LIST`).
pub(all) struct ExternalWindow {
xid : Int
wm_class : String
wm_instance : String
title : String
focused : Bool
minimized : Bool
maximized : Bool
} derive(Eq, Debug)
///|
pub(all) struct X11Window {
fd : Int
@@ -51,6 +62,25 @@ pub(all) struct X11Window {
// Atoms.
mut a_wm_protocols : Int
mut a_wm_delete : Int
mut a_net_wm_name : Int
mut a_net_client_list : Int
mut a_net_active_window : Int
mut a_net_close_window : Int
mut a_net_wm_state : Int
mut a_net_wm_state_hidden : Int
mut a_net_wm_state_skip_taskbar : Int
mut a_net_wm_state_max_vert : Int
mut a_net_wm_state_max_horz : Int
mut a_net_wm_window_type : Int
mut a_net_wm_window_type_normal : Int
mut a_net_wm_window_type_dialog : Int
mut a_wm_change_state : Int
mut a_wm_state : Int
// External EWMH windows (when running under an X11 window manager).
mut ext_windows : Array[ExternalWindow]
mut ext_dirty : Bool
mut ext_changed : Bool
mut ext_watched : Map[Int, Bool]
// MIT-SHM.
mut shm_major : Int
mut shm_event : Int
@@ -175,6 +205,8 @@ fn X11Window::request(
self.send_all(Bytes::from_array(out))
}
self.seq = (self.seq + 1) & 0xFFFF
self.replies.remove(self.seq)
self.errors.remove(self.seq)
self.seq
}
@@ -323,15 +355,20 @@ fn X11Window::set_prop(
ignore(self.request(18, 0, b))
}
///| Value of `prop` on window `w` as raw bytes (first 64 KiB); None if unset.
fn X11Window::get_prop(self : X11Window, w : Int, prop : Int) -> Array[Byte]? {
///| Send a GetProperty request for `prop` on window `w` and return its sequence number.
fn X11Window::request_prop(self : X11Window, w : Int, prop : Int) -> Int {
let b : Array[Byte] = []
put32(b, w)
put32(b, prop)
put32(b, 0) // AnyPropertyType
put32(b, 0)
put32(b, 16384)
match self.wait_reply(self.request(20, 0, b)) {
self.request(20, 0, b)
}
///| Read a GetProperty reply for sequence `seq` as raw bytes; None if unset or errored.
fn X11Window::read_prop(self : X11Window, seq : Int) -> Array[Byte]? {
match self.wait_reply(seq) {
None => None
Some(r) => {
let format = get8(r, 1)
@@ -349,6 +386,117 @@ fn X11Window::get_prop(self : X11Window, w : Int, prop : Int) -> Array[Byte]? {
}
}
///| Value of `prop` on window `w` as raw bytes (first 64 KiB); None if unset.
fn X11Window::get_prop(self : X11Window, w : Int, prop : Int) -> Array[Byte]? {
self.read_prop(self.request_prop(w, prop))
}
///| Parse ICCCM `WM_CLASS` bytes (`instance\0class\0`) into `(instance, class)`.
pub fn parse_wm_class(raw : Array[Byte]) -> (String, String) {
let parts : Array[Array[Byte]] = []
let mut cur : Array[Byte] = []
for b in raw {
if b == b'\x00' {
parts.push(cur)
cur = []
} else {
cur.push(b)
}
}
if cur.length() > 0 {
parts.push(cur)
}
let inst = if parts.length() >= 1 {
@utf8.decode_lossy(Bytes::from_array(parts[0]))
} else {
""
}
let cls = if parts.length() >= 2 {
@utf8.decode_lossy(Bytes::from_array(parts[1]))
} else {
""
}
(inst, cls)
}
///| Parse a 32-bit property (CARDINAL/ATOM/WINDOW list) from little-endian bytes.
pub fn parse_u32_list(raw : Array[Byte]) -> Array[Int] {
let out : Array[Int] = []
let n = raw.length() / 4
for i = 0; i < n; i = i + 1 {
out.push(get32(raw, i * 4))
}
out
}
///| Whether an X11 window from `_NET_CLIENT_LIST` should appear in the dock/taskbar.
pub fn is_taskbar_window(
w : Int,
self_win : Int,
states : Array[Int],
skip_taskbar : Int,
types : Array[Int],
normal_type : Int,
dialog_type : Int,
transient_for : Int,
client_set : Map[Int, Bool],
) -> Bool {
if w == 0 || w == self_win {
return false
}
if skip_taskbar != 0 && states.contains(skip_taskbar) {
return false
}
if types.length() > 0 {
let ok = (normal_type != 0 && types.contains(normal_type)) ||
(dialog_type != 0 && types.contains(dialog_type))
if !ok {
return false
}
}
if transient_for != 0 && client_set.contains(transient_for) {
return false
}
true
}
///| Build the 40-byte `SendEvent` (opcode 25) body for a 32-bit EWMH/ICCCM ClientMessage on `root`.
pub fn build_client_msg_body(
root : Int,
target : Int,
msg_type : Int,
d0 : Int,
d1 : Int,
d2 : Int,
d3 : Int,
d4 : Int,
) -> Array[Byte] {
let b : Array[Byte] = []
put32(b, root)
put32(b, 0x180000) // SubstructureRedirectMask | SubstructureNotifyMask
put8(b, 33) // ClientMessage
put8(b, 32) // format 32
put16(b, 0) // sequence
put32(b, target)
put32(b, msg_type)
put32(b, d0)
put32(b, d1)
put32(b, d2)
put32(b, d3)
put32(b, d4)
b
}
///| Build the 12-byte `ConfigureWindow` (opcode 12) body to raise `target` to the top (`StackMode=Above`).
pub fn build_raise_body(target : Int) -> Array[Byte] {
let b : Array[Byte] = []
put32(b, target)
put16(b, 0x40) // CWStackMode
put16(b, 0)
put32(b, 0) // Above
b
}
// ---- connection ---------------------------------------------------------------------------
///| Display number and screen from a DISPLAY value (":0", ":1.0", "unix:0"). None for TCP
@@ -720,6 +868,24 @@ pub fn X11Window::open(
mapped: false,
a_wm_protocols: 0,
a_wm_delete: 0,
a_net_wm_name: 0,
a_net_client_list: 0,
a_net_active_window: 0,
a_net_close_window: 0,
a_net_wm_state: 0,
a_net_wm_state_hidden: 0,
a_net_wm_state_skip_taskbar: 0,
a_net_wm_state_max_vert: 0,
a_net_wm_state_max_horz: 0,
a_net_wm_window_type: 0,
a_net_wm_window_type_normal: 0,
a_net_wm_window_type_dialog: 0,
a_wm_change_state: 0,
a_wm_state: 0,
ext_windows: [],
ext_dirty: false,
ext_changed: false,
ext_watched: {},
shm_major: -1,
shm_event: -1,
shm_seg: 0,
@@ -741,12 +907,24 @@ pub fn X11Window::open(
}
self.a_wm_protocols = self.intern("WM_PROTOCOLS")
self.a_wm_delete = self.intern("WM_DELETE_WINDOW")
let a_net_wm_name = self.intern("_NET_WM_NAME")
self.a_net_wm_name = self.intern("_NET_WM_NAME")
let a_utf8 = self.intern("UTF8_STRING")
let a_state = self.intern("_NET_WM_STATE")
self.a_net_wm_state = self.intern("_NET_WM_STATE")
let a_fullscreen = self.intern("_NET_WM_STATE_FULLSCREEN")
let a_wm_check = self.intern("_NET_SUPPORTING_WM_CHECK")
let a_pid = self.intern("_NET_WM_PID")
self.a_net_client_list = self.intern("_NET_CLIENT_LIST")
self.a_net_active_window = self.intern("_NET_ACTIVE_WINDOW")
self.a_net_close_window = self.intern("_NET_CLOSE_WINDOW")
self.a_net_wm_state_hidden = self.intern("_NET_WM_STATE_HIDDEN")
self.a_net_wm_state_skip_taskbar = self.intern("_NET_WM_STATE_SKIP_TASKBAR")
self.a_net_wm_state_max_vert = self.intern("_NET_WM_STATE_MAXIMIZED_VERT")
self.a_net_wm_state_max_horz = self.intern("_NET_WM_STATE_MAXIMIZED_HORZ")
self.a_net_wm_window_type = self.intern("_NET_WM_WINDOW_TYPE")
self.a_net_wm_window_type_normal = self.intern("_NET_WM_WINDOW_TYPE_NORMAL")
self.a_net_wm_window_type_dialog = self.intern("_NET_WM_WINDOW_TYPE_DIALOG")
self.a_wm_change_state = self.intern("WM_CHANGE_STATE")
self.a_wm_state = self.intern("WM_STATE")
// A window manager announces itself on the root window (EWMH); without one, take the screen.
// The property outlives a window manager that was killed, so also check that the window it
// names still exists and points at itself (EWMH).
@@ -816,8 +994,8 @@ pub fn X11Window::open(
// Titles, class, close button, minimum size, pid.
let t = ascii(title)
self.set_prop(39, 31, 8, t.to_array()) // WM_NAME STRING
if a_net_wm_name != 0 && a_utf8 != 0 {
self.set_prop(a_net_wm_name, a_utf8, 8, @sys.str_to_utf8_bytes(title).to_array())
if self.a_net_wm_name != 0 && a_utf8 != 0 {
self.set_prop(self.a_net_wm_name, a_utf8, 8, @sys.str_to_utf8_bytes(title).to_array())
}
let class : Array[Byte] = []
put_str(class, ascii(app_id))
@@ -846,18 +1024,25 @@ pub fn X11Window::open(
put32(hints, 0)
}
self.set_prop(40, 41, 32, hints) // WM_NORMAL_HINTS WM_SIZE_HINTS
if fullscreen && !self.session && a_state != 0 && a_fullscreen != 0 {
if fullscreen && !self.session && self.a_net_wm_state != 0 && a_fullscreen != 0 {
let p : Array[Byte] = []
put32(p, a_fullscreen)
self.set_prop(a_state, 4, 32, p)
self.set_prop(self.a_net_wm_state, 4, 32, p)
}
// Follow the screen size (xrandr) when the window is the screen.
// Follow the screen size (xrandr) when the window is the screen; under a WM, watch EWMH root props.
if self.session {
let cw : Array[Byte] = []
put32(cw, root)
put32(cw, 0x800)
put32(cw, 0x20000) // StructureNotify
ignore(self.request(2, 0, cw))
} else {
let cw : Array[Byte] = []
put32(cw, root)
put32(cw, 0x800)
put32(cw, 0x400000) // PropertyChangeMask
ignore(self.request(2, 0, cw))
self.ext_dirty = true
}
// A GC for PutImage, without GraphicsExpose events.
self.gc = self.alloc_xid()
@@ -1035,8 +1220,9 @@ fn X11Window::dispatch(self : X11Window, off : Int) -> Int {
self.seg_attached = false
self.frame_pending = false
self.redraw = true
} else if self.errors_logged < 20 && !(major == 20 && err == 3) {
// (BadWindow from GetProperty is the window-manager check finding a stale window.)
} else if self.errors_logged < 20 &&
!(err == 3 && (major == 2 || major == 12 || major == 20 || major == 25)) {
// (BadWindow on ChangeWindowAttributes / ConfigureWindow / GetProperty / SendEvent is a client that closed.)
self.errors_logged = self.errors_logged + 1
@sys.log(
"x11: X error " +
@@ -1176,6 +1362,28 @@ fn X11Window::dispatch(self : X11Window, off : Int) -> Int {
ignore(self.request(12, 0, b)) // ConfigureWindow
}
}
28 => {
// PropertyNotify: EWMH window list, active window, or watched client properties changed.
if !self.session {
let w = get32(rx, off + 4)
let atom = get32(rx, off + 8)
if w == self.root {
if atom == self.a_net_client_list || atom == self.a_net_active_window {
self.ext_dirty = true
}
} else if self.ext_watched.contains(w) {
if atom == self.a_net_wm_name ||
atom == 39 ||
atom == 67 ||
atom == self.a_net_wm_state ||
atom == self.a_wm_state ||
atom == self.a_net_wm_window_type ||
atom == 68 {
self.ext_dirty = true
}
}
}
}
33 => {
// ClientMessage: WM_DELETE_WINDOW from the window manager's close button.
if get32(rx, off + 8) == self.a_wm_protocols &&
@@ -1197,6 +1405,135 @@ fn X11Window::dispatch(self : X11Window, off : Int) -> Int {
// ---- the backend API (the same shape as @nested.NestedWindow) -----------------------------
///| Re-read `_NET_CLIENT_LIST`, `_NET_ACTIVE_WINDOW`, and each taskbar window's metadata.
fn X11Window::refresh_external_windows(self : X11Window) -> Unit {
if self.session || self.closed || self.a_net_client_list == 0 {
return
}
let client_ids = match self.get_prop(self.root, self.a_net_client_list) {
Some(b) => parse_u32_list(b)
None => []
}
let active_win = if self.a_net_active_window != 0 {
match self.get_prop(self.root, self.a_net_active_window) {
Some(b) => {
let ids = parse_u32_list(b)
if ids.length() > 0 { ids[0] } else { 0 }
}
None => 0
}
} else {
0
}
let client_set : Map[Int, Bool] = {}
for w in client_ids {
if w != 0 && w != self.win {
client_set.set(w, true)
if !self.ext_watched.contains(w) {
let cw : Array[Byte] = []
put32(cw, w)
put32(cw, 0x800) // CWEventMask
put32(cw, 0x400000) // PropertyChangeMask
ignore(self.request(2, 0, cw))
}
}
}
self.ext_watched = client_set
let next : Array[ExternalWindow] = []
for w in client_ids {
if w == 0 || w == self.win {
continue
}
let s_state = if self.a_net_wm_state != 0 { Some(self.request_prop(w, self.a_net_wm_state)) } else { None }
let s_type = if self.a_net_wm_window_type != 0 {
Some(self.request_prop(w, self.a_net_wm_window_type))
} else {
None
}
let s_trans = self.request_prop(w, 68) // WM_TRANSIENT_FOR
let s_class = self.request_prop(w, 67) // WM_CLASS
let s_net_name = if self.a_net_wm_name != 0 { Some(self.request_prop(w, self.a_net_wm_name)) } else { None }
let s_name = self.request_prop(w, 39) // WM_NAME
let s_wm_state = if self.a_wm_state != 0 { Some(self.request_prop(w, self.a_wm_state)) } else { None }
let read_opt = fn(seq : Int?) -> Array[Byte]? {
match seq {
Some(s) => self.read_prop(s)
None => None
}
}
let states = match read_opt(s_state) {
Some(b) => parse_u32_list(b)
None => []
}
let types = match read_opt(s_type) {
Some(b) => parse_u32_list(b)
None => []
}
let transient_for = match self.read_prop(s_trans) {
Some(b) => {
let ids = parse_u32_list(b)
if ids.length() > 0 { ids[0] } else { 0 }
}
None => 0
}
let (wm_instance, wm_class) = match self.read_prop(s_class) {
Some(b) => parse_wm_class(b)
None => ("", "")
}
let net_name = read_opt(s_net_name)
let legacy_name = self.read_prop(s_name)
let title = match net_name {
Some(b) if b.length() > 0 => @utf8.decode_lossy(Bytes::from_array(b))
_ =>
match legacy_name {
Some(b) => @utf8.decode_lossy(Bytes::from_array(b))
None => ""
}
}
let wm_state_iconic = match read_opt(s_wm_state) {
Some(b) => {
let vals = parse_u32_list(b)
vals.length() > 0 && vals[0] == 3
}
None => false
}
if !is_taskbar_window(
w,
self.win,
states,
self.a_net_wm_state_skip_taskbar,
types,
self.a_net_wm_window_type_normal,
self.a_net_wm_window_type_dialog,
transient_for,
client_set,
) {
continue
}
if wm_class == "" && wm_instance == "" && title == "" {
continue
}
let minimized = (self.a_net_wm_state_hidden != 0 && states.contains(self.a_net_wm_state_hidden)) ||
wm_state_iconic
let maximized = (self.a_net_wm_state_max_vert != 0 && states.contains(self.a_net_wm_state_max_vert)) ||
(self.a_net_wm_state_max_horz != 0 && states.contains(self.a_net_wm_state_max_horz))
next.push({
xid: w,
wm_class,
wm_instance,
title,
focused: w == active_win,
minimized,
maximized,
})
}
if next != self.ext_windows {
self.ext_windows = next
self.ext_changed = true
}
}
///| Pump X events (input is queued; see take_input). False once the window was closed or the
/// X server went away. `display` is used to re-read the keymap after setxkbmap.
pub fn X11Window::poll(self : X11Window) -> Bool {
@@ -1209,9 +1546,78 @@ pub fn X11Window::poll(self : X11Window) -> Bool {
self.keymap = km
}
}
if self.ext_dirty && !self.session && !self.closed {
self.ext_dirty = false
self.refresh_external_windows()
}
!self.closed
}
///| Updated external X11 window list if `_NET_CLIENT_LIST`, focus, titles, or states changed.
pub fn X11Window::take_external_windows(self : X11Window) -> Array[ExternalWindow]? {
if !self.ext_changed {
return None
}
self.ext_changed = false
Some(self.ext_windows.copy())
}
///| Send a 32-bit EWMH/ICCCM ClientMessage on the root window targeting `target`.
fn X11Window::send_client_msg(
self : X11Window,
target : Int,
msg_type : Int,
d0 : Int,
d1 : Int,
d2 : Int,
d3 : Int,
d4 : Int,
) -> Unit {
if self.closed || target == 0 || msg_type == 0 {
return
}
ignore(self.request(25, 0, build_client_msg_body(self.root, target, msg_type, d0, d1, d2, d3, d4)))
}
///| Ask the host EWMH window manager to restore, focus, and raise external window `xid`.
pub fn X11Window::focus_external(self : X11Window, xid : Int) -> Unit {
if self.closed || xid == 0 {
return
}
self.send_client_msg(xid, self.a_net_active_window, 2, 0, 0, 0, 0)
ignore(self.request(12, 0, build_raise_body(xid)))
}
///| Ask the host EWMH window manager to close external window `xid`.
pub fn X11Window::close_external(self : X11Window, xid : Int) -> Unit {
self.send_client_msg(xid, self.a_net_close_window, 0, 2, 0, 0, 0)
}
///| Ask the host EWMH window manager to minimize (iconify) external window `xid`.
pub fn X11Window::minimize_external(self : X11Window, xid : Int) -> Unit {
self.send_client_msg(xid, self.a_wm_change_state, 3, 0, 0, 0, 0)
}
///| Ask the host EWMH window manager to focus and toggle maximized state on external window `xid`.
pub fn X11Window::toggle_maximize_external(self : X11Window, xid : Int) -> Unit {
if self.closed || xid == 0 {
return
}
self.focus_external(xid)
if self.a_net_wm_state != 0 &&
(self.a_net_wm_state_max_vert != 0 || self.a_net_wm_state_max_horz != 0) {
self.send_client_msg(
xid,
self.a_net_wm_state,
2,
self.a_net_wm_state_max_vert,
self.a_net_wm_state_max_horz,
2,
0,
)
}
}
///| The new window size if it changed since the last call.
pub fn X11Window::take_resize(self : X11Window) -> (Int, Int)? {
if !self.resized {