Skip to main content

agentmux_srv/server/streamed_browser/screen/
wm.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The window manager's settings. A pane's client sends keys and mouse buttons at screen level, so whatever the window manager binds to a
5//! key or a button, the client can use: a stock Openbox has a root menu that starts programs on the server and an Alt-drag that moves a
6//! window. This configuration binds nothing: no keyboard shortcut, no mouse action, an empty menu, and no frame on any window (so there is
7//! no title bar to drag and no button to press). The window manager is there to give the browser the focus and to place its windows; the
8//! browser is the only program a client can reach.
9
10use std::path::Path;
11
12/// The window manager's own file names, inside the folder it is given.
13pub const RC_FILE: &str = "rc.xml";
14pub const MENU_FILE: &str = "menu.xml";
15
16/// The settings: nothing bound, the menu empty, no window decorated.
17pub fn rc(menu: &Path) -> String {
18    format!(
19        r#"<?xml version="1.0" encoding="UTF-8"?>
20<openbox_config xmlns="http://openbox.org/3.4/rc">
21  <focus><focusNew>yes</focusNew><followMouse>no</followMouse><focusLast>yes</focusLast></focus>
22  <placement><policy>Smart</policy></placement>
23  <desktops><number>1</number></desktops>
24  <keyboard></keyboard>
25  <mouse><dragThreshold>8</dragThreshold><doubleClickTime>500</doubleClickTime></mouse>
26  <menu><file>{}</file></menu>
27  <applications><application class="*"><decor>no</decor></application></applications>
28</openbox_config>
29"#,
30        menu.display()
31    )
32}
33
34/// A menu with nothing in it.
35pub const MENU: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
36<openbox_menu xmlns="http://openbox.org/3.4/menu">
37  <menu id="root-menu" label="AgentMux"></menu>
38</openbox_menu>
39"#;
40
41/// Write both files into `dir` and give the arguments that make the window manager read them.
42pub fn write(dir: &Path) -> Result<Vec<String>, String> {
43    std::fs::create_dir_all(dir).map_err(|e| format!("cannot make the window manager's folder: {e}"))?;
44    let (rc_path, menu_path) = (dir.join(RC_FILE), dir.join(MENU_FILE));
45    std::fs::write(&menu_path, MENU).map_err(|e| format!("cannot write the window manager's menu: {e}"))?;
46    std::fs::write(&rc_path, rc(&menu_path)).map_err(|e| format!("cannot write the window manager's settings: {e}"))?;
47    Ok(vec!["--config-file".to_string(), rc_path.display().to_string()])
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn the_window_manager_binds_nothing_a_client_could_use_to_run_a_program_or_move_a_window() {
56        let text = rc(Path::new("/tmp/wm/menu.xml"));
57        for bound in ["<keybind", "<mousebind", "<action", "Execute", "ShowMenu", "Move"] {
58            assert!(!text.contains(bound), "{bound} must not be in the settings");
59        }
60        assert!(text.contains("<keyboard></keyboard>"), "the keyboard section is there and empty (an absent one may fall back to defaults)");
61        assert!(text.contains("<decor>no</decor>"), "no window has a frame to drag");
62        assert!(text.contains("/tmp/wm/menu.xml"));
63        assert!(!MENU.contains("<item") && !MENU.contains("Execute"), "the menu is empty");
64    }
65
66    #[test]
67    fn the_files_are_written_and_the_arguments_name_the_settings() {
68        let dir = tempfile::tempdir().unwrap();
69        let args = write(&dir.path().join("wm")).unwrap();
70        assert_eq!(args[0], "--config-file");
71        assert!(Path::new(&args[1]).is_file() && dir.path().join("wm").join(MENU_FILE).is_file());
72    }
73}