tony / oxwm

updated atom caching to eliminate 6 round trips to X server per operation, lowered cpu usage by updating polling frequency to 100ms for updating status blocks, changed focus updates to only update necessary windows/monitors by implementing dwms approach to go from O(n) to O(1) runtime. Browse files
tonybtw committed 2025-10-19 parent e4b5586 commit 3f51577d231357ca8029b1342091432df0c578cf
Showing 1 changed file with 74 additions and 51 deletions. UnifiedSplit
+74 −51 src/window_manager.rs
@@ -19,6 +19,37 @@ pub fn tag_mask(tag: usize) -> TagMask {
19 1 << tag 19 1 << tag
20 } 20 }
21 21
22 +struct AtomCache {
23 + net_current_desktop: Atom,
24 + net_client_info: Atom,
25 + wm_state: Atom,
26 +}
27 +
28 +impl AtomCache {
29 + fn new(connection: &RustConnection) -> WmResult<Self> {
30 + let net_current_desktop = connection
31 + .intern_atom(false, b"_NET_CURRENT_DESKTOP")?
32 + .reply()?
33 + .atom;
34 +
35 + let net_client_info = connection
36 + .intern_atom(false, b"_NET_CLIENT_INFO")?
37 + .reply()?
38 + .atom;
39 +
40 + let wm_state = connection
41 + .intern_atom(false, b"WM_STATE")?
42 + .reply()?
43 + .atom;
44 +
45 + Ok(Self {
46 + net_current_desktop,
47 + net_client_info,
48 + wm_state,
49 + })
50 + }
51 +}
52 +
22 pub struct WindowManager { 53 pub struct WindowManager {
23 config: Config, 54 config: Config,
24 connection: RustConnection, 55 connection: RustConnection,
@@ -35,6 +66,8 @@ pub struct WindowManager {
35 bars: Vec<Bar>, 66 bars: Vec<Bar>,
36 monitors: Vec<Monitor>, 67 monitors: Vec<Monitor>,
37 selected_monitor: usize, 68 selected_monitor: usize,
69 + atoms: AtomCache,
70 + previous_focused: Option<Window>,
38 } 71 }
39 72
40 type WmResult<T> = Result<T, WmError>; 73 type WmResult<T> = Result<T, WmError>;
@@ -116,6 +149,8 @@ impl WindowManager {
116 149
117 let gaps_enabled = config.gaps_enabled; 150 let gaps_enabled = config.gaps_enabled;
118 151
152 + let atoms = AtomCache::new(&connection)?;
153 +
119 let mut window_manager = Self { 154 let mut window_manager = Self {
120 config, 155 config,
121 connection, 156 connection,
@@ -132,6 +167,8 @@ impl WindowManager {
132 bars, 167 bars,
133 monitors, 168 monitors,
134 selected_monitor: 0, 169 selected_monitor: 0,
170 + atoms,
171 + previous_focused: None,
135 }; 172 };
136 173
137 window_manager.scan_existing_windows()?; 174 window_manager.scan_existing_windows()?;
@@ -176,17 +213,8 @@ impl WindowManager {
176 213
177 fn scan_existing_windows(&mut self) -> WmResult<()> { 214 fn scan_existing_windows(&mut self) -> WmResult<()> {
178 let tree = self.connection.query_tree(self.root)?.reply()?; 215 let tree = self.connection.query_tree(self.root)?.reply()?;
179 - let net_client_info = self 216 + let net_client_info = self.atoms.net_client_info;
180 - .connection 217 + let wm_state_atom = self.atoms.wm_state;
181 - .intern_atom(false, b"_NET_CLIENT_INFO")?
182 - .reply()?
183 - .atom;
184 -
185 - let wm_state_atom = self
186 - .connection
187 - .intern_atom(false, b"WM_STATE")?
188 - .reply()?
189 - .atom;
190 218
191 for &window in &tree.children { 219 for &window in &tree.children {
192 if self.bars.iter().any(|bar| bar.window() == window) { 220 if self.bars.iter().any(|bar| bar.window() == window) {
@@ -271,11 +299,7 @@ impl WindowManager {
271 } 299 }
272 300
273 fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { 301 fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> {
274 - let net_client_info = self 302 + let net_client_info = self.atoms.net_client_info;
275 - .connection
276 - .intern_atom(false, b"_NET_CLIENT_INFO")?
277 - .reply()?
278 - .atom;
279 303
280 let bytes = tag.to_ne_bytes().to_vec(); 304 let bytes = tag.to_ne_bytes().to_vec();
281 305
@@ -294,11 +318,7 @@ impl WindowManager {
294 } 318 }
295 319
296 fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> { 320 fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> {
297 - let wm_state_atom = self 321 + let wm_state_atom = self.atoms.wm_state;
298 - .connection
299 - .intern_atom(false, b"WM_STATE")?
300 - .reply()?
301 - .atom;
302 322
303 let data = [state, 0u32]; 323 let data = [state, 0u32];
304 let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); 324 let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect();
@@ -324,21 +344,21 @@ impl WindowManager {
324 self.update_bar()?; 344 self.update_bar()?;
325 345
326 loop { 346 loop {
327 - if let Some(bar) = self.bars.get_mut(self.selected_monitor) { 347 + while let Some(event) = self.connection.poll_for_event()? {
328 - bar.update_blocks();
329 - }
330 -
331 - if let Ok(Some(event)) = self.connection.poll_for_event() {
332 if let Some(should_restart) = self.handle_event(event)? { 348 if let Some(should_restart) = self.handle_event(event)? {
333 return Ok(should_restart); 349 return Ok(should_restart);
334 } 350 }
335 } 351 }
336 352
353 + if let Some(bar) = self.bars.get_mut(self.selected_monitor) {
354 + bar.update_blocks();
355 + }
356 +
337 if self.bars.iter().any(|bar| bar.needs_redraw()) { 357 if self.bars.iter().any(|bar| bar.needs_redraw()) {
338 self.update_bar()?; 358 self.update_bar()?;
339 } 359 }
340 360
341 - std::thread::sleep(std::time::Duration::from_millis(10)); 361 + std::thread::sleep(std::time::Duration::from_millis(100));
342 } 362 }
343 } 363 }
344 364
@@ -370,7 +390,6 @@ impl WindowManager {
370 } 390 }
371 391
372 self.apply_layout()?; 392 self.apply_layout()?;
373 - self.update_focus_visuals()?;
374 } else { 393 } else {
375 self.fullscreen_window = Some(focused); 394 self.fullscreen_window = Some(focused);
376 395
@@ -596,11 +615,7 @@ impl WindowManager {
596 } 615 }
597 616
598 fn save_selected_tags(&self) -> WmResult<()> { 617 fn save_selected_tags(&self) -> WmResult<()> {
599 - let net_current_desktop = self 618 + let net_current_desktop = self.atoms.net_current_desktop;
600 - .connection
601 - .intern_atom(false, b"_NET_CURRENT_DESKTOP")?
602 - .reply()?
603 - .atom;
604 619
605 let selected_tags = self.monitors.get(self.selected_monitor).map(|m| m.selected_tags).unwrap_or(tag_mask(0)); 620 let selected_tags = self.monitors.get(self.selected_monitor).map(|m| m.selected_tags).unwrap_or(tag_mask(0));
606 let desktop = selected_tags.trailing_zeros(); 621 let desktop = selected_tags.trailing_zeros();
@@ -668,6 +683,8 @@ impl WindowManager {
668 } 683 }
669 684
670 pub fn set_focus(&mut self, window: Window) -> WmResult<()> { 685 pub fn set_focus(&mut self, window: Window) -> WmResult<()> {
686 + let old_focused = self.previous_focused;
687 +
671 if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { 688 if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
672 monitor.focused_window = Some(window); 689 monitor.focused_window = Some(window);
673 } 690 }
@@ -676,30 +693,36 @@ impl WindowManager {
676 .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?; 693 .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?;
677 self.connection.flush()?; 694 self.connection.flush()?;
678 695
679 - self.update_focus_visuals()?; 696 + self.update_focus_visuals(old_focused, window)?;
697 + self.previous_focused = Some(window);
680 Ok(()) 698 Ok(())
681 } 699 }
682 700
683 - fn update_focus_visuals(&self) -> WmResult<()> { 701 + fn update_focus_visuals(&self, old_focused: Option<Window>, new_focused: Window) -> WmResult<()> {
684 - let focused = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window); 702 + if let Some(old_win) = old_focused {
703 + if old_win != new_focused {
704 + self.connection.configure_window(
705 + old_win,
706 + &ConfigureWindowAux::new().border_width(self.config.border_width),
707 + )?;
685 708
686 - for &window in &self.windows { 709 + self.connection.change_window_attributes(
687 - let (border_color, border_width) = if focused == Some(window) { 710 + old_win,
688 - (self.config.border_focused, self.config.border_width) 711 + &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused),
689 - } else { 712 + )?;
690 - (self.config.border_unfocused, self.config.border_width) 713 + }
691 - }; 714 + }
692 715
693 - self.connection.configure_window( 716 + self.connection.configure_window(
694 - window, 717 + new_focused,
695 - &ConfigureWindowAux::new().border_width(border_width), 718 + &ConfigureWindowAux::new().border_width(self.config.border_width),
696 - )?; 719 + )?;
720 +
721 + self.connection.change_window_attributes(
722 + new_focused,
723 + &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused),
724 + )?;
697 725
698 - self.connection.change_window_attributes(
699 - window,
700 - &ChangeWindowAttributesAux::new().border_pixel(border_color),
701 - )?;
702 - }
703 self.connection.flush()?; 726 self.connection.flush()?;
704 Ok(()) 727 Ok(())
705 } 728 }