| 1 | use crate::bar::Bar; |
| 2 | use crate::config::{BORDER_FOCUSED, BORDER_UNFOCUSED, BORDER_WIDTH, TAG_COUNT}; |
| 3 | use crate::keyboard::{self, Arg, KeyAction}; |
| 4 | use crate::layout::Layout; |
| 5 | use crate::layout::tiling::TilingLayout; |
| 6 | use anyhow::Result; |
| 7 | |
| 8 | use x11rb::connection::Connection; |
| 9 | use x11rb::protocol::Event; |
| 10 | use x11rb::protocol::xproto::*; |
| 11 | use x11rb::rust_connection::RustConnection; |
| 12 | |
| 13 | pub type TagMask = u32; |
| 14 | pub fn tag_mask(tag: usize) -> TagMask { |
| 15 | 1 << tag |
| 16 | } |
| 17 | |
| 18 | pub struct WindowManager { |
| 19 | connection: RustConnection, |
| 20 | screen_number: usize, |
| 21 | root: Window, |
| 22 | screen: Screen, |
| 23 | windows: Vec<Window>, |
| 24 | focused_window: Option<Window>, |
| 25 | layout: Box<dyn Layout>, |
| 26 | window_tags: std::collections::HashMap<Window, TagMask>, |
| 27 | selected_tags: TagMask, |
| 28 | bar: Bar, |
| 29 | } |
| 30 | |
| 31 | impl WindowManager { |
| 32 | pub fn new() -> Result<Self> { |
| 33 | let (connection, screen_number) = x11rb::connect(None)?; |
| 34 | let root = connection.setup().roots[screen_number].root; |
| 35 | let screen = connection.setup().roots[screen_number].clone(); |
| 36 | |
| 37 | connection |
| 38 | .change_window_attributes( |
| 39 | root, |
| 40 | &ChangeWindowAttributesAux::new().event_mask( |
| 41 | EventMask::SUBSTRUCTURE_REDIRECT |
| 42 | | EventMask::SUBSTRUCTURE_NOTIFY |
| 43 | | EventMask::PROPERTY_CHANGE |
| 44 | | EventMask::KEY_PRESS, |
| 45 | ), |
| 46 | )? |
| 47 | .check()?; |
| 48 | |
| 49 | let bar = Bar::new(&connection, &screen, screen_number)?; |
| 50 | |
| 51 | let mut window_manger = Self { |
| 52 | connection, |
| 53 | screen_number, |
| 54 | root, |
| 55 | screen, |
| 56 | windows: Vec::new(), |
| 57 | focused_window: None, |
| 58 | layout: Box::new(TilingLayout), |
| 59 | window_tags: std::collections::HashMap::new(), |
| 60 | selected_tags: tag_mask(0), |
| 61 | bar, |
| 62 | }; |
| 63 | |
| 64 | window_manger.scan_existing_windows()?; |
| 65 | |
| 66 | Ok(window_manger) |
| 67 | } |
| 68 | |
| 69 | fn scan_existing_windows(&mut self) -> Result<()> { |
| 70 | let tree = self.connection.query_tree(self.root)?.reply()?; |
| 71 | |
| 72 | println!("=== Scanning existing windows ==="); |
| 73 | println!("Total children: {}", tree.children.len()); |
| 74 | println!("Current selected_tags: {:b}", self.selected_tags); |
| 75 | |
| 76 | for &window in &tree.children { |
| 77 | if window == self.bar.window() { |
| 78 | println!("Skipping bar window: {}", window); |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | let attrs = match self.connection.get_window_attributes(window)?.reply() { |
| 83 | Ok(attrs) => attrs, |
| 84 | Err(_) => { |
| 85 | println!("Failed to get attributes for window: {}", window); |
| 86 | continue; |
| 87 | } |
| 88 | }; |
| 89 | |
| 90 | println!( |
| 91 | "Window {}: override_redirect={}, map_state={:?}", |
| 92 | window, attrs.override_redirect, attrs.map_state |
| 93 | ); |
| 94 | |
| 95 | if attrs.override_redirect { |
| 96 | println!(" -> Skipped (override_redirect)"); |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | if attrs.map_state == MapState::VIEWABLE { |
| 101 | println!( |
| 102 | " -> Managing VIEWABLE window, tagging with {:b}", |
| 103 | self.selected_tags |
| 104 | ); |
| 105 | self.windows.push(window); |
| 106 | self.window_tags.insert(window, self.selected_tags); |
| 107 | continue; |
| 108 | } |
| 109 | |
| 110 | if attrs.map_state == MapState::UNMAPPED { |
| 111 | let has_wm_class = match self |
| 112 | .connection |
| 113 | .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)? |
| 114 | .reply() |
| 115 | { |
| 116 | Ok(prop) => !prop.value.is_empty(), |
| 117 | Err(_) => false, |
| 118 | }; |
| 119 | |
| 120 | if has_wm_class { |
| 121 | println!( |
| 122 | " -> Managing UNMAPPED window (has WM_CLASS), tagging with {:b}", |
| 123 | self.selected_tags |
| 124 | ); |
| 125 | self.windows.push(window); |
| 126 | self.window_tags.insert(window, self.selected_tags); |
| 127 | } else { |
| 128 | println!(" -> Skipped UNMAPPED (no WM_CLASS)"); |
| 129 | } |
| 130 | } else { |
| 131 | println!(" -> Skipped (map_state={:?})", attrs.map_state); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | println!("Total managed windows: {}", self.windows.len()); |
| 136 | |
| 137 | if let Some(&first) = self.windows.first() { |
| 138 | self.set_focus(Some(first))?; |
| 139 | } |
| 140 | |
| 141 | self.apply_layout()?; |
| 142 | Ok(()) |
| 143 | } |
| 144 | |
| 145 | pub fn run(&mut self) -> Result<bool> { |
| 146 | println!("oxwm started on display {}", self.screen_number); |
| 147 | |
| 148 | keyboard::setup_keybinds(&self.connection, self.root)?; |
| 149 | self.update_bar()?; |
| 150 | |
| 151 | loop { |
| 152 | self.bar.update_blocks()?; |
| 153 | |
| 154 | if let Ok(Some(event)) = self.connection.poll_for_event() { |
| 155 | if let Some(should_restart) = self.handle_event(event)? { |
| 156 | return Ok(should_restart); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if self.bar.needs_redraw() { |
| 161 | self.update_bar()?; |
| 162 | } |
| 163 | |
| 164 | std::thread::sleep(std::time::Duration::from_millis(10)); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | fn update_bar(&mut self) -> Result<()> { |
| 169 | let mut occupied_tags: TagMask = 0; |
| 170 | for &tags in self.window_tags.values() { |
| 171 | occupied_tags |= tags; |
| 172 | } |
| 173 | |
| 174 | self.bar.invalidate(); |
| 175 | self.bar |
| 176 | .draw(&self.connection, self.selected_tags, occupied_tags)?; |
| 177 | Ok(()) |
| 178 | } |
| 179 | |
| 180 | fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> { |
| 181 | match action { |
| 182 | KeyAction::Spawn => match arg { |
| 183 | Arg::Str(command) => { |
| 184 | std::process::Command::new(command).spawn()?; |
| 185 | } |
| 186 | Arg::Array(cmd) => { |
| 187 | if let Some((program, args)) = cmd.split_first() { |
| 188 | std::process::Command::new(program).args(args).spawn()?; |
| 189 | } |
| 190 | } |
| 191 | _ => {} |
| 192 | }, |
| 193 | KeyAction::KillClient => { |
| 194 | if let Some(focused) = self.focused_window { |
| 195 | match self.connection.kill_client(focused) { |
| 196 | Ok(_) => { |
| 197 | self.connection.flush()?; |
| 198 | } |
| 199 | Err(e) => { |
| 200 | eprintln!("Failed to kill window {}: {}", focused, e); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | KeyAction::FocusStack => { |
| 206 | if let Arg::Int(direction) = arg { |
| 207 | self.cycle_focus(*direction)?; |
| 208 | } |
| 209 | } |
| 210 | KeyAction::Quit | KeyAction::Restart => { |
| 211 | //no-op |
| 212 | } |
| 213 | KeyAction::ViewTag => { |
| 214 | if let Arg::Int(tag_index) = arg { |
| 215 | self.view_tag(*tag_index as usize)?; |
| 216 | } |
| 217 | } |
| 218 | KeyAction::MoveToTag => { |
| 219 | if let Arg::Int(tag_index) = arg { |
| 220 | self.move_to_tag(*tag_index as usize)?; |
| 221 | } |
| 222 | } |
| 223 | KeyAction::None => { |
| 224 | //no-op |
| 225 | } |
| 226 | } |
| 227 | Ok(()) |
| 228 | } |
| 229 | |
| 230 | fn is_window_visible(&self, window: Window) -> bool { |
| 231 | if let Some(&tags) = self.window_tags.get(&window) { |
| 232 | (tags & self.selected_tags) != 0 |
| 233 | } else { |
| 234 | false |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | fn visible_windows(&self) -> Vec<Window> { |
| 239 | self.windows |
| 240 | .iter() |
| 241 | .filter(|&&w| self.is_window_visible(w)) |
| 242 | .copied() |
| 243 | .collect() |
| 244 | } |
| 245 | |
| 246 | fn update_window_visibility(&self) -> Result<()> { |
| 247 | for &window in &self.windows { |
| 248 | if self.is_window_visible(window) { |
| 249 | self.connection.map_window(window)?; |
| 250 | } else { |
| 251 | self.connection.unmap_window(window)?; |
| 252 | } |
| 253 | } |
| 254 | self.connection.flush()?; |
| 255 | Ok(()) |
| 256 | } |
| 257 | |
| 258 | pub fn view_tag(&mut self, tag_index: usize) -> Result<()> { |
| 259 | if tag_index >= TAG_COUNT { |
| 260 | return Ok(()); |
| 261 | } |
| 262 | |
| 263 | self.selected_tags = tag_mask(tag_index); |
| 264 | self.update_window_visibility()?; |
| 265 | self.apply_layout()?; |
| 266 | self.update_bar()?; |
| 267 | |
| 268 | let visible = self.visible_windows(); |
| 269 | self.set_focus(visible.first().copied())?; |
| 270 | |
| 271 | Ok(()) |
| 272 | } |
| 273 | |
| 274 | pub fn move_to_tag(&mut self, tag_index: usize) -> Result<()> { |
| 275 | if tag_index >= TAG_COUNT { |
| 276 | return Ok(()); |
| 277 | } |
| 278 | |
| 279 | if let Some(focused) = self.focused_window { |
| 280 | let mask = tag_mask(tag_index); |
| 281 | self.window_tags.insert(focused, mask); |
| 282 | self.update_window_visibility()?; |
| 283 | self.apply_layout()?; |
| 284 | self.update_bar()?; |
| 285 | } |
| 286 | |
| 287 | Ok(()) |
| 288 | } |
| 289 | |
| 290 | pub fn cycle_focus(&mut self, direction: i32) -> Result<()> { |
| 291 | let visible = self.visible_windows(); |
| 292 | |
| 293 | if visible.is_empty() { |
| 294 | return Ok(()); |
| 295 | } |
| 296 | |
| 297 | let next_window = if let Some(current) = self.focused_window { |
| 298 | if let Some(current_index) = visible.iter().position(|&w| w == current) { |
| 299 | let next_index = if direction > 0 { |
| 300 | (current_index + 1) % visible.len() |
| 301 | } else { |
| 302 | (current_index + visible.len() - 1) % visible.len() |
| 303 | }; |
| 304 | visible[next_index] |
| 305 | } else { |
| 306 | visible[0] |
| 307 | } |
| 308 | } else { |
| 309 | visible[0] |
| 310 | }; |
| 311 | |
| 312 | self.set_focus(Some(next_window))?; |
| 313 | Ok(()) |
| 314 | } |
| 315 | |
| 316 | pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> { |
| 317 | self.focused_window = window; |
| 318 | |
| 319 | if let Some(win) = window { |
| 320 | self.connection |
| 321 | .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?; |
| 322 | self.connection.flush()?; |
| 323 | } |
| 324 | |
| 325 | self.update_focus_visuals()?; |
| 326 | Ok(()) |
| 327 | } |
| 328 | |
| 329 | fn update_focus_visuals(&self) -> Result<()> { |
| 330 | for &window in &self.windows { |
| 331 | let (border_color, border_width) = if self.focused_window == Some(window) { |
| 332 | (BORDER_FOCUSED, BORDER_WIDTH) |
| 333 | } else { |
| 334 | (BORDER_UNFOCUSED, BORDER_WIDTH) |
| 335 | }; |
| 336 | |
| 337 | self.connection.configure_window( |
| 338 | window, |
| 339 | &ConfigureWindowAux::new().border_width(border_width), |
| 340 | )?; |
| 341 | |
| 342 | self.connection.change_window_attributes( |
| 343 | window, |
| 344 | &ChangeWindowAttributesAux::new().border_pixel(border_color), |
| 345 | )?; |
| 346 | } |
| 347 | self.connection.flush()?; |
| 348 | Ok(()) |
| 349 | } |
| 350 | |
| 351 | fn handle_event(&mut self, event: Event) -> Result<Option<bool>> { |
| 352 | match event { |
| 353 | Event::MapRequest(event) => { |
| 354 | self.connection.map_window(event.window)?; |
| 355 | self.windows.push(event.window); |
| 356 | self.window_tags.insert(event.window, self.selected_tags); |
| 357 | self.apply_layout()?; |
| 358 | self.update_bar()?; |
| 359 | self.set_focus(Some(event.window))?; |
| 360 | } |
| 361 | Event::UnmapNotify(event) => { |
| 362 | if self.windows.contains(&event.window) && self.is_window_visible(event.window) { |
| 363 | self.remove_window(event.window)?; |
| 364 | } |
| 365 | } |
| 366 | Event::DestroyNotify(event) => { |
| 367 | if self.windows.contains(&event.window) { |
| 368 | self.remove_window(event.window)?; |
| 369 | } |
| 370 | } |
| 371 | Event::KeyPress(event) => { |
| 372 | let (action, arg) = keyboard::handle_key_press(event)?; |
| 373 | |
| 374 | match action { |
| 375 | KeyAction::Quit => return Ok(Some(false)), |
| 376 | KeyAction::Restart => return Ok(Some(true)), |
| 377 | _ => self.handle_key_action(action, arg)?, |
| 378 | } |
| 379 | } |
| 380 | Event::ButtonPress(event) => { |
| 381 | if event.event == self.bar.window() { |
| 382 | if let Some(tag_index) = self.bar.handle_click(event.event_x) { |
| 383 | self.view_tag(tag_index)?; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | Event::Expose(event) => { |
| 388 | if event.window == self.bar.window() { |
| 389 | self.bar.invalidate(); |
| 390 | self.update_bar()?; |
| 391 | } |
| 392 | } |
| 393 | _ => {} |
| 394 | } |
| 395 | Ok(None) |
| 396 | } |
| 397 | |
| 398 | fn apply_layout(&self) -> Result<()> { |
| 399 | let screen_width = self.screen.width_in_pixels as u32; |
| 400 | let screen_height = self.screen.height_in_pixels as u32; |
| 401 | let border_width = BORDER_WIDTH; |
| 402 | |
| 403 | let bar_height = self.bar.height() as u32; |
| 404 | let usable_height = screen_height.saturating_sub(bar_height); |
| 405 | |
| 406 | let visible = self.visible_windows(); |
| 407 | let geometries = self.layout.arrange(&visible, screen_width, usable_height); |
| 408 | |
| 409 | for (window, geometry) in visible.iter().zip(geometries.iter()) { |
| 410 | let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
| 411 | let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
| 412 | |
| 413 | let adjusted_y = geometry.y_coordinate + bar_height as i32; |
| 414 | |
| 415 | self.connection.configure_window( |
| 416 | *window, |
| 417 | &ConfigureWindowAux::new() |
| 418 | .x(geometry.x_coordinate) |
| 419 | .y(adjusted_y) |
| 420 | .width(adjusted_width) |
| 421 | .height(adjusted_height), |
| 422 | )?; |
| 423 | } |
| 424 | self.connection.flush()?; |
| 425 | Ok(()) |
| 426 | } |
| 427 | |
| 428 | fn remove_window(&mut self, window: Window) -> Result<()> { |
| 429 | let initial_count = self.windows.len(); |
| 430 | self.windows.retain(|&w| w != window); |
| 431 | self.window_tags.remove(&window); |
| 432 | |
| 433 | if self.windows.len() < initial_count { |
| 434 | if self.focused_window == Some(window) { |
| 435 | let new_focus = if self.windows.is_empty() { |
| 436 | None |
| 437 | } else { |
| 438 | Some(self.windows[self.windows.len() - 1]) |
| 439 | }; |
| 440 | self.set_focus(new_focus)?; |
| 441 | } |
| 442 | |
| 443 | self.apply_layout()?; |
| 444 | self.update_bar()?; |
| 445 | } |
| 446 | Ok(()) |
| 447 | } |
| 448 | } |