Files
chandra/compositor/shell/apps.mbt
T
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

265 lines
8.7 KiB
MoonBit

///| Installed desktop applications, discovered from freedesktop `.desktop` files
/// (XDG data dirs + flatpak exports). Feeds the launcher's search.
///|
priv struct DesktopApp {
id : String
name : String
comment : String
category : String
command : String
/// StartupWMClass: the app id its windows carry when that is not the desktop file's name.
wm_class : String
/// Icon=: an icon theme name ("firefox") or an absolute path.
icon : String
/// Terminal=true: runs inside a terminal.
terminal : Bool
/// Categories= has TerminalEmulator.
terminal_emulator : Bool
}
///| Directories scanned, lowest priority first (later ones override by file name).
fn app_dirs() -> Array[String] {
let home = @sys.getenv("HOME")
let data_home = {
let x = @sys.getenv("XDG_DATA_HOME")
if x.length() > 0 { x } else { home + "/.local/share" }
}
let dirs : Array[String] = []
let xdg = @sys.getenv("XDG_DATA_DIRS")
let list = if xdg.length() > 0 { xdg } else { "/usr/local/share:/usr/share" }
let parts : Array[String] = list.split(":").map(x => x.to_string()).collect()
// XDG_DATA_DIRS is highest priority first; we want lowest first.
for i = parts.length() - 1; i >= 0; i = i - 1 {
if parts[i].length() > 0 {
dirs.push(parts[i] + "/applications")
}
}
dirs.push("/var/lib/flatpak/exports/share/applications")
dirs.push(home + "/.local/share/flatpak/exports/share/applications")
dirs.push(data_home + "/applications")
dirs
}
///| Drop Exec field codes (%f %U ...), keep a literal %%.
fn clean_exec(exec : String) -> String {
let sb = StringBuilder::new()
let chars = exec.to_array()
let mut i = 0
while i < chars.length() {
if chars[i] == '%' && i + 1 < chars.length() {
if chars[i + 1] == '%' {
sb.write_char('%')
}
i = i + 2
} else {
sb.write_char(chars[i])
i = i + 1
}
}
sb.to_string().trim(chars=" ").to_string()
}
///| Parse one .desktop file; None when it should not be listed. A Terminal=true app keeps its
/// own command (`terminal`): it is started inside the chosen terminal (`DesktopShell::command_for`).
fn parse_desktop(id : String, text : String, have_terminal : Bool) -> DesktopApp? {
let k = desktop_entry_keys(text)
let get = fn(key : String) { k.get(key).unwrap_or("") }
let name = get("Name")
let exec = get("Exec")
let hidden = get("NoDisplay") == "true" || get("Hidden") == "true"
let terminal = get("Terminal") == "true"
let kind = k.get("Type").unwrap_or("Application")
if hidden || kind != "Application" || name.length() == 0 || exec.length() == 0 {
return None
}
if terminal && !have_terminal {
return None
}
let cats = get("Categories")
let category = match cats.split(";").next() {
Some(c) if c.length() > 0 => c.to_string()
_ => "Other"
}
let comment = get("Comment")
let comment = if comment.length() > 0 { comment } else { get("GenericName") }
Some({
id,
name,
comment,
category,
command: clean_exec(exec),
wm_class: get("StartupWMClass"),
icon: get("Icon"),
terminal,
terminal_emulator: semi_list(cats).contains("TerminalEmulator"),
})
}
///| Scan every applications dir; later dirs override earlier ones, result sorted by name.
fn scan_desktop_apps() -> Array[DesktopApp] {
let by_id : Map[String, DesktopApp] = {}
let removed : Map[String, Bool] = {}
for dir in app_dirs() {
for file in @sys.list_desktop_files(dir) {
let bytes = @sys.read_file_bytes(dir + "/" + file)
let id = file[:file.length() - 8].to_string()
match parse_desktop(id, @utf8.decode_lossy(bytes), true) {
Some(app) => {
by_id.set(id, app)
removed.remove(id)
}
None => {
by_id.remove(id)
}
}
}
}
// Terminal apps are only listed when there is a terminal to run them in.
let mut have_terminal = @sys.has_exe("foot")
for _, a in by_id {
have_terminal = have_terminal || a.terminal_emulator
}
let apps : Array[DesktopApp] = []
for _, a in by_id {
if have_terminal || !a.terminal {
apps.push(a)
}
}
apps.sort_by(fn(a, b) {
let x = a.name.to_lower()
let y = b.name.to_lower()
if x < y { -1 } else if x > y { 1 } else { 0 }
})
apps
}
///|
test "parse_desktop reads a normal entry and strips field codes" {
let t = "[Desktop Entry]\nType=Application\nName=Firefox\nName[de]=Feuerfuchs\nComment=Browse\nExec=firefox %u\nCategories=Network;WebBrowser;\n[Desktop Action x]\nName=Nope\n"
match parse_desktop("firefox", t, true) {
Some(a) => {
assert_eq(a.name, "Firefox")
assert_eq(a.command, "firefox")
assert_eq(a.category, "Network")
assert_eq(a.comment, "Browse")
}
None => fail("expected entry")
}
}
///|
test "parse_desktop keeps StartupWMClass" {
let t = "[Desktop Entry]\nType=Application\nName=Code\nExec=code %F\nStartupWMClass=Code\n"
match parse_desktop("visual-studio-code", t, true) {
Some(a) => assert_eq(a.wm_class, "Code")
None => fail("expected entry")
}
let t2 = "[Desktop Entry]\nType=Application\nName=Files\nExec=nautilus\nIcon=org.gnome.Nautilus\n[Desktop Action w]\nIcon=other\n"
match parse_desktop("org.gnome.Nautilus", t2, true) {
Some(a) => assert_eq(a.icon, "org.gnome.Nautilus")
None => fail("expected entry")
}
}
///|
test "parse_desktop hides NoDisplay entries" {
let t = "[Desktop Entry]\nType=Application\nName=X\nExec=x\nNoDisplay=true\n"
assert_true(parse_desktop("x", t, true) is None)
}
///| The desktop file id that launches the app whose windows carry `app_id`: the file of that
/// name, else one whose StartupWMClass is it, else a case-insensitive match on either, else one
/// whose last dotted part matches (a window "nautilus" for org.gnome.Nautilus.desktop).
fn resolve_app_id(
app_id : String,
exec : Map[String, String],
wm_classes : Map[String, String],
) -> String? {
if app_id.length() == 0 {
return None
}
if exec.contains(app_id) {
return Some(app_id)
}
let low = app_id.to_lower()
match wm_classes.get(low) {
Some(id) => return Some(id)
None => ()
}
for id, _ in exec {
if id.to_lower() == low {
return Some(id)
}
}
for id, _ in exec {
let parts : Array[String] = id.split(".").map(x => x.to_string()).collect()
if parts.length() > 1 && parts[parts.length() - 1].to_lower() == low {
return Some(id)
}
}
None
}
///|
test "resolve_app_id finds the desktop file for a window's app id" {
let exec : Map[String, String] = {
"foot": "foot",
"org.gnome.Nautilus": "nautilus --new-window",
"visual-studio-code": "code",
}
let wm : Map[String, String] = { "code": "visual-studio-code" }
assert_eq(resolve_app_id("foot", exec, wm), Some("foot"))
assert_eq(resolve_app_id("Code", exec, wm), Some("visual-studio-code"))
assert_eq(resolve_app_id("org.gnome.nautilus", exec, wm), Some("org.gnome.Nautilus"))
assert_eq(resolve_app_id("nautilus", exec, wm), Some("org.gnome.Nautilus"))
assert_eq(resolve_app_id("unknown", exec, wm), None)
assert_eq(resolve_app_id("", exec, wm), None)
}
///| Canonical app id for an external X11 window: resolve WM_CLASS class then instance against
/// installed .desktop files (including StartupWMClass, e.g. `astra` -> `gamerig-astra`),
/// falling back to the raw class, instance, or title.
pub fn DesktopShell::resolve_external_app_id(
self : DesktopShell,
wm_class : String,
wm_instance : String,
title : String,
) -> String {
match resolve_app_id(wm_class, self.exec, self.wm_classes) {
Some(id) => return id
None => ()
}
match resolve_app_id(wm_instance, self.exec, self.wm_classes) {
Some(id) => return id
None => ()
}
if wm_class.length() > 0 {
wm_class
} else if wm_instance.length() > 0 {
wm_instance
} else {
title
}
}
///|
test "resolve_external_app_id resolves StartupWMClass and falls back cleanly" {
let sh = DesktopShell::new(":0")
sh.exec.clear()
sh.wm_classes.clear()
sh.exec.set("com.mitchellh.ghostty", "gamerig-open terminal")
sh.exec.set("steam", "gamerig-open steam")
sh.exec.set("gamerig-astra", "gamerig-open astra")
sh.wm_classes.set("com.mitchellh.ghostty", "com.mitchellh.ghostty")
sh.wm_classes.set("steam", "steam")
sh.wm_classes.set("astra", "gamerig-astra")
assert_eq(sh.resolve_external_app_id("com.mitchellh.ghostty", "ghostty", "Terminal"), "com.mitchellh.ghostty")
assert_eq(sh.resolve_external_app_id("steam", "steamwebhelper", "Steam"), "steam")
assert_eq(sh.resolve_external_app_id("astra", "astra", "Space Rage: Astra Incognita"), "gamerig-astra")
assert_eq(sh.resolve_external_app_id("steam_app_1145360", "steam_app_1145360", "Hades"), "steam_app_1145360")
assert_eq(sh.resolve_external_app_id("", "my-inst", "Fallback"), "my-inst")
assert_eq(sh.resolve_external_app_id("", "", "Window Title"), "Window Title")
}