agentmux_srv/server/streamed_browser/screen/
ewmh.rs1use 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
15const REMOVE: u32 = 0;
17const ADD: u32 = 1;
18const FROM_APPLICATION: u32 = 1;
20const 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 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 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 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 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}