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
+74 −51 src/window_manager.rs
| @@ -19,6 +19,37 @@ pub fn tag_mask(tag: usize) -> TagMask { | ||
| 19 | 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 | 53 | pub struct WindowManager { |
| 23 | 54 | config: Config, |
| 24 | 55 | connection: RustConnection, |
| @@ -35,6 +66,8 @@ pub struct WindowManager { | ||
| 35 | 66 | bars: Vec<Bar>, |
| 36 | 67 | monitors: Vec<Monitor>, |
| 37 | 68 | selected_monitor: usize, |
| 69 | + atoms: AtomCache, | |
| 70 | + previous_focused: Option<Window>, | |
| 38 | 71 | } |
| 39 | 72 | |
| 40 | 73 | type WmResult<T> = Result<T, WmError>; |
| @@ -116,6 +149,8 @@ impl WindowManager { | ||
| 116 | 149 | |
| 117 | 150 | let gaps_enabled = config.gaps_enabled; |
| 118 | 151 | |
| 152 | + let atoms = AtomCache::new(&connection)?; | |
| 153 | + | |
| 119 | 154 | let mut window_manager = Self { |
| 120 | 155 | config, |
| 121 | 156 | connection, |
| @@ -132,6 +167,8 @@ impl WindowManager { | ||
| 132 | 167 | bars, |
| 133 | 168 | monitors, |
| 134 | 169 | selected_monitor: 0, |
| 170 | + atoms, | |
| 171 | + previous_focused: None, | |
| 135 | 172 | }; |
| 136 | 173 | |
| 137 | 174 | window_manager.scan_existing_windows()?; |
| @@ -176,17 +213,8 @@ impl WindowManager { | ||
| 176 | 213 | |
| 177 | 214 | fn scan_existing_windows(&mut self) -> WmResult<()> { |
| 178 | 215 | let tree = self.connection.query_tree(self.root)?.reply()?; |
| 179 | - let net_client_info = self | |
| 180 | - .connection | |
| 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; | |
| 216 | + let net_client_info = self.atoms.net_client_info; | |
| 217 | + let wm_state_atom = self.atoms.wm_state; | |
| 190 | 218 | |
| 191 | 219 | for &window in &tree.children { |
| 192 | 220 | if self.bars.iter().any(|bar| bar.window() == window) { |
| @@ -271,11 +299,7 @@ impl WindowManager { | ||
| 271 | 299 | } |
| 272 | 300 | |
| 273 | 301 | fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { |
| 274 | - let net_client_info = self | |
| 275 | - .connection | |
| 276 | - .intern_atom(false, b"_NET_CLIENT_INFO")? | |
| 277 | - .reply()? | |
| 278 | - .atom; | |
| 302 | + let net_client_info = self.atoms.net_client_info; | |
| 279 | 303 | |
| 280 | 304 | let bytes = tag.to_ne_bytes().to_vec(); |
| 281 | 305 | |
| @@ -294,11 +318,7 @@ impl WindowManager { | ||
| 294 | 318 | } |
| 295 | 319 | |
| 296 | 320 | fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> { |
| 297 | - let wm_state_atom = self | |
| 298 | - .connection | |
| 299 | - .intern_atom(false, b"WM_STATE")? | |
| 300 | - .reply()? | |
| 301 | - .atom; | |
| 321 | + let wm_state_atom = self.atoms.wm_state; | |
| 302 | 322 | |
| 303 | 323 | let data = [state, 0u32]; |
| 304 | 324 | let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); |
| @@ -324,21 +344,21 @@ impl WindowManager { | ||
| 324 | 344 | self.update_bar()?; |
| 325 | 345 | |
| 326 | 346 | loop { |
| 327 | - if let Some(bar) = self.bars.get_mut(self.selected_monitor) { | |
| 328 | - bar.update_blocks(); | |
| 329 | - } | |
| 330 | - | |
| 331 | - if let Ok(Some(event)) = self.connection.poll_for_event() { | |
| 347 | + while let Some(event) = self.connection.poll_for_event()? { | |
| 332 | 348 | if let Some(should_restart) = self.handle_event(event)? { |
| 333 | 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 | 357 | if self.bars.iter().any(|bar| bar.needs_redraw()) { |
| 338 | 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 | 392 | self.apply_layout()?; |
| 373 | - self.update_focus_visuals()?; | |
| 374 | 393 | } else { |
| 375 | 394 | self.fullscreen_window = Some(focused); |
| 376 | 395 | |
| @@ -596,11 +615,7 @@ impl WindowManager { | ||
| 596 | 615 | } |
| 597 | 616 | |
| 598 | 617 | fn save_selected_tags(&self) -> WmResult<()> { |
| 599 | - let net_current_desktop = self | |
| 600 | - .connection | |
| 601 | - .intern_atom(false, b"_NET_CURRENT_DESKTOP")? | |
| 602 | - .reply()? | |
| 603 | - .atom; | |
| 618 | + let net_current_desktop = self.atoms.net_current_desktop; | |
| 604 | 619 | |
| 605 | 620 | let selected_tags = self.monitors.get(self.selected_monitor).map(|m| m.selected_tags).unwrap_or(tag_mask(0)); |
| 606 | 621 | let desktop = selected_tags.trailing_zeros(); |
| @@ -668,6 +683,8 @@ impl WindowManager { | ||
| 668 | 683 | } |
| 669 | 684 | |
| 670 | 685 | pub fn set_focus(&mut self, window: Window) -> WmResult<()> { |
| 686 | + let old_focused = self.previous_focused; | |
| 687 | + | |
| 671 | 688 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 672 | 689 | monitor.focused_window = Some(window); |
| 673 | 690 | } |
| @@ -676,30 +693,36 @@ impl WindowManager { | ||
| 676 | 693 | .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?; |
| 677 | 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 | 698 | Ok(()) |
| 681 | 699 | } |
| 682 | 700 | |
| 683 | - fn update_focus_visuals(&self) -> WmResult<()> { | |
| 684 | - let focused = self.monitors.get(self.selected_monitor).and_then(|m| m.focused_window); | |
| 701 | + fn update_focus_visuals(&self, old_focused: Option<Window>, new_focused: Window) -> WmResult<()> { | |
| 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 { | |
| 687 | - let (border_color, border_width) = if focused == Some(window) { | |
| 688 | - (self.config.border_focused, self.config.border_width) | |
| 689 | - } else { | |
| 690 | - (self.config.border_unfocused, self.config.border_width) | |
| 691 | - }; | |
| 709 | + self.connection.change_window_attributes( | |
| 710 | + old_win, | |
| 711 | + &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), | |
| 712 | + )?; | |
| 713 | + } | |
| 714 | + } | |
| 692 | 715 | |
| 693 | - self.connection.configure_window( | |
| 694 | - window, | |
| 695 | - &ConfigureWindowAux::new().border_width(border_width), | |
| 696 | - )?; | |
| 716 | + self.connection.configure_window( | |
| 717 | + new_focused, | |
| 718 | + &ConfigureWindowAux::new().border_width(self.config.border_width), | |
| 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 | 726 | self.connection.flush()?; |
| 704 | 727 | Ok(()) |
| 705 | 728 | } |