Skip to main content

agentmux_srv/server/streamed_browser/screen/
ewmh.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The browser's window fills the virtual screen, and keeps filling it when the screen changes size. Chrome's own `--start-maximized` does not
5//! make the window manager's window maximised here, so a window that is not maximised stays the size it was; the maximise is the window
6//! manager's own request (EWMH `_NET_WM_STATE`), the same one a person's maximise button makes. Nothing here reaches into the browser.
7
8use std::time::{Duration, Instant};
9
10use x11rb::connection::Connection;
11use x11rb::protocol::xproto::{Atom, AtomEnum, ClientMessageEvent, ConnectionExt as _, EventMask};
12
13use super::x::Screen;
14
15/// `_NET_WM_STATE` actions.
16const REMOVE: u32 = 0;
17const ADD: u32 = 1;
18/// The request comes from an ordinary application (a pager would say 2).
19const FROM_APPLICATION: u32 = 1;
20/// Windows smaller than this (a tooltip's frame, a hidden helper) are not the browser's window.
21const MIN_WINDOW: u16 = 100;
22
23impl Screen {
24    fn atom(&self, name: &str) -> Result<Atom, String> {
25        Ok(self.conn.intern_atom(false, name.as_bytes()).map_err(|e| e.to_string())?.reply().map_err(|e| e.to_string())?.atom)
26    }
27
28    /// The windows the window manager manages (its `_NET_CLIENT_LIST`); none when there is no window manager or it has none yet.
29    fn clients(&self) -> Result<Vec<u32>, String> {
30        let list = self.atom("_NET_CLIENT_LIST")?;
31        let reply = self.conn.get_property(false, self.root, list, AtomEnum::WINDOW, 0, u32::MAX).map_err(|e| e.to_string())?.reply().map_err(|e| e.to_string())?;
32        Ok(reply.value32().map(|v| v.collect()).unwrap_or_default())
33    }
34
35    fn is_maximized(&self, window: u32) -> Result<bool, String> {
36        let state = self.atom("_NET_WM_STATE")?;
37        let (vert, horz) = (self.atom("_NET_WM_STATE_MAXIMIZED_VERT")?, self.atom("_NET_WM_STATE_MAXIMIZED_HORZ")?);
38        let reply = self.conn.get_property(false, window, state, AtomEnum::ATOM, 0, 64).map_err(|e| e.to_string())?.reply().map_err(|e| e.to_string())?;
39        let set: Vec<u32> = reply.value32().map(|v| v.collect()).unwrap_or_default();
40        Ok(set.contains(&vert) && set.contains(&horz))
41    }
42
43    /// Ask the window manager to make `window` maximised (or not): the message a pager or a maximise button sends to the root window.
44    fn maximize(&self, window: u32, on: bool) -> Result<(), String> {
45        let state = self.atom("_NET_WM_STATE")?;
46        let (vert, horz) = (self.atom("_NET_WM_STATE_MAXIMIZED_VERT")?, self.atom("_NET_WM_STATE_MAXIMIZED_HORZ")?);
47        let event = ClientMessageEvent::new(32, window, state, [if on { ADD } else { REMOVE }, vert, horz, FROM_APPLICATION, 0]);
48        self.conn
49            .send_event(false, self.root, EventMask::SUBSTRUCTURE_REDIRECT | EventMask::SUBSTRUCTURE_NOTIFY, event)
50            .map_err(|e| e.to_string())?
51            .check()
52            .map_err(|e| format!("the window manager could not be asked to maximise the window: {e}"))
53    }
54
55    /// Make the browser's window fill the screen: the biggest window the window manager manages, once there is one (waits up to `wait`). It
56    /// stays maximised, and follows the screen's size (`refit`).
57    pub fn maximize_first_within(&self, wait: Duration) -> Result<(), String> {
58        let until = Instant::now() + wait;
59        loop {
60            let mut biggest: Option<(u32, u32)> = None;
61            for window in self.clients()? {
62                let Ok(g) = self.conn.get_geometry(window).map_err(|e| e.to_string()).and_then(|c| c.reply().map_err(|e| e.to_string())) else { continue };
63                let area = u32::from(g.width) * u32::from(g.height);
64                if g.width >= MIN_WINDOW && g.height >= MIN_WINDOW && biggest.map_or(true, |(_, a)| area > a) {
65                    biggest = Some((window, area));
66                }
67            }
68            if let Some((window, _)) = biggest {
69                return self.maximize(window, true);
70            }
71            if Instant::now() >= until {
72                return Err("the browser's window did not appear".to_string());
73            }
74            std::thread::sleep(Duration::from_millis(100));
75        }
76    }
77
78    /// After the screen changed size: every window that is maximised is made so again, so the window manager fits it to the new screen. A window
79    /// that is not maximised (a pop-up) keeps its size.
80    pub fn refit(&self) -> Result<(), String> {
81        for window in self.clients()? {
82            if self.is_maximized(window)? {
83                self.maximize(window, false)?;
84                self.maximize(window, true)?;
85            }
86        }
87        Ok(())
88    }
89}