| 1 | use crate::Config; |
| 2 | use crate::bar::Bar; |
| 3 | use crate::keyboard::{self, Arg, KeyAction, handlers}; |
| 4 | use crate::layout::GapConfig; |
| 5 | use crate::layout::Layout; |
| 6 | use crate::layout::tiling::TilingLayout; |
| 7 | use anyhow::Result; |
| 8 | use std::collections::HashSet; |
| 9 | use std::path::PathBuf; |
| 10 | use x11rb::cursor::Handle as CursorHandle; |
| 11 | |
| 12 | use x11rb::connection::Connection; |
| 13 | use x11rb::protocol::Event; |
| 14 | use x11rb::protocol::xproto::*; |
| 15 | use x11rb::rust_connection::RustConnection; |
| 16 | |
| 17 | pub type TagMask = u32; |
| 18 | pub fn tag_mask(tag: usize) -> TagMask { |
| 19 | 1 << tag |
| 20 | } |
| 21 | |
| 22 | pub struct WindowManager { |
| 23 | config: Config, |
| 24 | connection: RustConnection, |
| 25 | screen_number: usize, |
| 26 | root: Window, |
| 27 | screen: Screen, |
| 28 | windows: Vec<Window>, |
| 29 | focused_window: Option<Window>, |
| 30 | layout: Box<dyn Layout>, |
| 31 | window_tags: std::collections::HashMap<Window, TagMask>, |
| 32 | selected_tags: TagMask, |
| 33 | gaps_enabled: bool, |
| 34 | fullscreen_window: Option<Window>, |
| 35 | floating_windows: HashSet<Window>, |
| 36 | bar: Bar, |
| 37 | } |
| 38 | |
| 39 | impl WindowManager { |
| 40 | pub fn new(config: Config) -> Result<Self> { |
| 41 | let (connection, screen_number) = x11rb::connect(None)?; |
| 42 | let root = connection.setup().roots[screen_number].root; |
| 43 | let screen = connection.setup().roots[screen_number].clone(); |
| 44 | |
| 45 | let normal_cursor = CursorHandle::new( |
| 46 | &connection, |
| 47 | screen_number, |
| 48 | &x11rb::resource_manager::new_from_default(&connection)?, |
| 49 | )? |
| 50 | .reply()? |
| 51 | .load_cursor(&connection, "left_ptr")?; |
| 52 | |
| 53 | connection |
| 54 | .change_window_attributes( |
| 55 | root, |
| 56 | &ChangeWindowAttributesAux::new() |
| 57 | .cursor(normal_cursor) |
| 58 | .event_mask( |
| 59 | EventMask::SUBSTRUCTURE_REDIRECT |
| 60 | | EventMask::SUBSTRUCTURE_NOTIFY |
| 61 | | EventMask::PROPERTY_CHANGE |
| 62 | | EventMask::KEY_PRESS |
| 63 | | EventMask::BUTTON_PRESS, |
| 64 | ), |
| 65 | )? |
| 66 | .check()?; |
| 67 | |
| 68 | connection.grab_button( |
| 69 | false, |
| 70 | root, |
| 71 | EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, |
| 72 | GrabMode::SYNC, |
| 73 | GrabMode::ASYNC, |
| 74 | x11rb::NONE, |
| 75 | x11rb::NONE, |
| 76 | ButtonIndex::M1, |
| 77 | u16::from(config.modkey).into(), |
| 78 | )?; |
| 79 | |
| 80 | connection.grab_button( |
| 81 | false, |
| 82 | root, |
| 83 | EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, |
| 84 | GrabMode::SYNC, |
| 85 | GrabMode::ASYNC, |
| 86 | x11rb::NONE, |
| 87 | x11rb::NONE, |
| 88 | ButtonIndex::M3, |
| 89 | u16::from(config.modkey).into(), |
| 90 | )?; |
| 91 | |
| 92 | let bar = Bar::new(&connection, &screen, screen_number, &config)?; |
| 93 | |
| 94 | let selected_tags = Self::get_saved_selected_tags(&connection, root, config.tags.len())?; |
| 95 | let gaps_enabled = config.gaps_enabled; |
| 96 | |
| 97 | let mut window_manager = Self { |
| 98 | config, |
| 99 | connection, |
| 100 | screen_number, |
| 101 | root, |
| 102 | screen, |
| 103 | windows: Vec::new(), |
| 104 | focused_window: None, |
| 105 | layout: Box::new(TilingLayout), |
| 106 | window_tags: std::collections::HashMap::new(), |
| 107 | selected_tags, |
| 108 | gaps_enabled, |
| 109 | fullscreen_window: None, |
| 110 | floating_windows: HashSet::new(), |
| 111 | bar, |
| 112 | }; |
| 113 | |
| 114 | window_manager.scan_existing_windows()?; |
| 115 | window_manager.update_bar()?; |
| 116 | |
| 117 | Ok(window_manager) |
| 118 | } |
| 119 | |
| 120 | fn get_saved_selected_tags( |
| 121 | connection: &RustConnection, |
| 122 | root: Window, |
| 123 | tag_count: usize, |
| 124 | ) -> Result<TagMask> { |
| 125 | let net_current_desktop = connection |
| 126 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| 127 | .reply()? |
| 128 | .atom; |
| 129 | |
| 130 | match connection |
| 131 | .get_property(false, root, net_current_desktop, AtomEnum::CARDINAL, 0, 1)? |
| 132 | .reply() |
| 133 | { |
| 134 | Ok(prop) if prop.value.len() >= 4 => { |
| 135 | let desktop = u32::from_ne_bytes([ |
| 136 | prop.value[0], |
| 137 | prop.value[1], |
| 138 | prop.value[2], |
| 139 | prop.value[3], |
| 140 | ]); |
| 141 | if desktop < tag_count as u32 { |
| 142 | let mask = tag_mask(desktop as usize); |
| 143 | return Ok(mask); |
| 144 | } |
| 145 | } |
| 146 | Ok(_) => {} |
| 147 | Err(e) => { |
| 148 | eprintln!("No _NET_CURRENT_DESKTOP property ({})", e); |
| 149 | } |
| 150 | } |
| 151 | Ok(tag_mask(0)) |
| 152 | } |
| 153 | |
| 154 | fn scan_existing_windows(&mut self) -> Result<()> { |
| 155 | let tree = self.connection.query_tree(self.root)?.reply()?; |
| 156 | let net_client_info = self |
| 157 | .connection |
| 158 | .intern_atom(false, b"_NET_CLIENT_INFO")? |
| 159 | .reply()? |
| 160 | .atom; |
| 161 | |
| 162 | let wm_state_atom = self |
| 163 | .connection |
| 164 | .intern_atom(false, b"WM_STATE")? |
| 165 | .reply()? |
| 166 | .atom; |
| 167 | |
| 168 | for &window in &tree.children { |
| 169 | if window == self.bar.window() { |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | let Ok(attrs) = self.connection.get_window_attributes(window)?.reply() else { |
| 174 | continue; |
| 175 | }; |
| 176 | |
| 177 | if attrs.override_redirect { |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | if attrs.map_state == MapState::VIEWABLE { |
| 182 | let tag = self.get_saved_tag(window, net_client_info)?; |
| 183 | self.windows.push(window); |
| 184 | self.window_tags.insert(window, tag); |
| 185 | continue; |
| 186 | } |
| 187 | |
| 188 | if attrs.map_state == MapState::UNMAPPED { |
| 189 | let has_wm_state = self |
| 190 | .connection |
| 191 | .get_property(false, window, wm_state_atom, AtomEnum::ANY, 0, 2)? |
| 192 | .reply() |
| 193 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 194 | |
| 195 | if !has_wm_state { |
| 196 | continue; |
| 197 | } |
| 198 | |
| 199 | let has_wm_class = self |
| 200 | .connection |
| 201 | .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)? |
| 202 | .reply() |
| 203 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 204 | |
| 205 | if has_wm_class { |
| 206 | let tag = self.get_saved_tag(window, net_client_info)?; |
| 207 | self.windows.push(window); |
| 208 | self.window_tags.insert(window, tag); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if let Some(&first) = self.windows.first() { |
| 214 | self.set_focus(Some(first))?; |
| 215 | } |
| 216 | |
| 217 | self.apply_layout()?; |
| 218 | Ok(()) |
| 219 | } |
| 220 | |
| 221 | fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> Result<TagMask> { |
| 222 | match self |
| 223 | .connection |
| 224 | .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)? |
| 225 | .reply() |
| 226 | { |
| 227 | Ok(prop) if prop.value.len() >= 4 => { |
| 228 | let tags = u32::from_ne_bytes([ |
| 229 | prop.value[0], |
| 230 | prop.value[1], |
| 231 | prop.value[2], |
| 232 | prop.value[3], |
| 233 | ]); |
| 234 | |
| 235 | if tags != 0 && tags < (1 << self.config.tags.len()) { |
| 236 | return Ok(tags); |
| 237 | } |
| 238 | } |
| 239 | Ok(_) => {} |
| 240 | Err(e) => { |
| 241 | eprintln!("No _NET_CLIENT_INFO property ({})", e); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | Ok(self.selected_tags) |
| 246 | } |
| 247 | |
| 248 | fn save_client_tag(&self, window: Window, tag: TagMask) -> Result<()> { |
| 249 | let net_client_info = self |
| 250 | .connection |
| 251 | .intern_atom(false, b"_NET_CLIENT_INFO")? |
| 252 | .reply()? |
| 253 | .atom; |
| 254 | |
| 255 | let bytes = tag.to_ne_bytes().to_vec(); |
| 256 | |
| 257 | self.connection.change_property( |
| 258 | PropMode::REPLACE, |
| 259 | window, |
| 260 | net_client_info, |
| 261 | AtomEnum::CARDINAL, |
| 262 | 32, |
| 263 | 1, |
| 264 | &bytes, |
| 265 | )?; |
| 266 | |
| 267 | self.connection.flush()?; |
| 268 | Ok(()) |
| 269 | } |
| 270 | |
| 271 | fn set_wm_state(&self, window: Window, state: u32) -> Result<()> { |
| 272 | let wm_state_atom = self |
| 273 | .connection |
| 274 | .intern_atom(false, b"WM_STATE")? |
| 275 | .reply()? |
| 276 | .atom; |
| 277 | |
| 278 | let data = [state, 0u32]; |
| 279 | let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); |
| 280 | |
| 281 | self.connection.change_property( |
| 282 | PropMode::REPLACE, |
| 283 | window, |
| 284 | wm_state_atom, |
| 285 | wm_state_atom, |
| 286 | 32, |
| 287 | 2, |
| 288 | &bytes, |
| 289 | )?; |
| 290 | |
| 291 | self.connection.flush()?; |
| 292 | Ok(()) |
| 293 | } |
| 294 | |
| 295 | pub fn run(&mut self) -> Result<bool> { |
| 296 | println!("oxwm started on display {}", self.screen_number); |
| 297 | |
| 298 | keyboard::setup_keybinds(&self.connection, self.root, &self.config.keybindings)?; |
| 299 | self.update_bar()?; |
| 300 | |
| 301 | loop { |
| 302 | self.bar.update_blocks()?; |
| 303 | |
| 304 | if let Ok(Some(event)) = self.connection.poll_for_event() { |
| 305 | if let Some(should_restart) = self.handle_event(event)? { |
| 306 | return Ok(should_restart); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | if self.bar.needs_redraw() { |
| 311 | self.update_bar()?; |
| 312 | } |
| 313 | |
| 314 | std::thread::sleep(std::time::Duration::from_millis(10)); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | fn toggle_fullscreen(&mut self) -> Result<()> { |
| 319 | if let Some(focused) = self.focused_window { |
| 320 | if self.fullscreen_window == Some(focused) { |
| 321 | self.fullscreen_window = None; |
| 322 | |
| 323 | self.connection.map_window(self.bar.window())?; |
| 324 | |
| 325 | self.apply_layout()?; |
| 326 | self.update_focus_visuals()?; |
| 327 | } else { |
| 328 | self.fullscreen_window = Some(focused); |
| 329 | |
| 330 | self.connection.unmap_window(self.bar.window())?; |
| 331 | |
| 332 | let screen_width = self.screen.width_in_pixels as u32; |
| 333 | let screen_height = self.screen.height_in_pixels as u32; |
| 334 | |
| 335 | self.connection.configure_window( |
| 336 | focused, |
| 337 | &ConfigureWindowAux::new() |
| 338 | .x(0) |
| 339 | .y(0) |
| 340 | .width(screen_width) |
| 341 | .height(screen_height) |
| 342 | .border_width(0) |
| 343 | .stack_mode(StackMode::ABOVE), |
| 344 | )?; |
| 345 | |
| 346 | self.connection.flush()?; |
| 347 | } |
| 348 | } |
| 349 | Ok(()) |
| 350 | } |
| 351 | |
| 352 | fn update_bar(&mut self) -> Result<()> { |
| 353 | let mut occupied_tags: TagMask = 0; |
| 354 | for &tags in self.window_tags.values() { |
| 355 | occupied_tags |= tags; |
| 356 | } |
| 357 | |
| 358 | self.bar.invalidate(); |
| 359 | self.bar |
| 360 | .draw(&self.connection, self.selected_tags, occupied_tags)?; |
| 361 | Ok(()) |
| 362 | } |
| 363 | |
| 364 | fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> { |
| 365 | match action { |
| 366 | KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, |
| 367 | KeyAction::KillClient => { |
| 368 | if let Some(focused) = self.focused_window { |
| 369 | match self.connection.kill_client(focused) { |
| 370 | Ok(_) => { |
| 371 | self.connection.flush()?; |
| 372 | } |
| 373 | Err(e) => { |
| 374 | eprintln!("Failed to kill window {}: {}", focused, e); |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | KeyAction::ToggleFullScreen => { |
| 380 | self.toggle_fullscreen()?; |
| 381 | } |
| 382 | KeyAction::FocusStack => { |
| 383 | if let Arg::Int(direction) = arg { |
| 384 | self.cycle_focus(*direction)?; |
| 385 | } |
| 386 | } |
| 387 | KeyAction::Quit | KeyAction::Restart => { |
| 388 | // Handled in handle_event |
| 389 | } |
| 390 | KeyAction::Recompile => { |
| 391 | match std::process::Command::new("oxwm") |
| 392 | .arg("--recompile") |
| 393 | .spawn() |
| 394 | { |
| 395 | Ok(_) => eprintln!("Recompiling in background"), |
| 396 | Err(e) => eprintln!("Failed to spawn recompile: {}", e), |
| 397 | } |
| 398 | } |
| 399 | KeyAction::ViewTag => { |
| 400 | if let Arg::Int(tag_index) = arg { |
| 401 | self.view_tag(*tag_index as usize)?; |
| 402 | } |
| 403 | } |
| 404 | KeyAction::MoveToTag => { |
| 405 | if let Arg::Int(tag_index) = arg { |
| 406 | self.move_to_tag(*tag_index as usize)?; |
| 407 | } |
| 408 | } |
| 409 | KeyAction::ToggleGaps => { |
| 410 | self.gaps_enabled = !self.gaps_enabled; |
| 411 | self.apply_layout()?; |
| 412 | } |
| 413 | KeyAction::None => {} |
| 414 | } |
| 415 | Ok(()) |
| 416 | } |
| 417 | |
| 418 | fn is_window_visible(&self, window: Window) -> bool { |
| 419 | if let Some(&tags) = self.window_tags.get(&window) { |
| 420 | (tags & self.selected_tags) != 0 |
| 421 | } else { |
| 422 | false |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | fn visible_windows(&self) -> Vec<Window> { |
| 427 | self.windows |
| 428 | .iter() |
| 429 | .filter(|&&w| self.is_window_visible(w)) |
| 430 | .copied() |
| 431 | .collect() |
| 432 | } |
| 433 | |
| 434 | fn update_window_visibility(&self) -> Result<()> { |
| 435 | for &window in &self.windows { |
| 436 | if self.is_window_visible(window) { |
| 437 | self.connection.map_window(window)?; |
| 438 | } else { |
| 439 | self.connection.unmap_window(window)?; |
| 440 | } |
| 441 | } |
| 442 | self.connection.flush()?; |
| 443 | Ok(()) |
| 444 | } |
| 445 | |
| 446 | pub fn view_tag(&mut self, tag_index: usize) -> Result<()> { |
| 447 | if tag_index >= self.config.tags.len() { |
| 448 | return Ok(()); |
| 449 | } |
| 450 | |
| 451 | if self.fullscreen_window.is_some() { |
| 452 | self.fullscreen_window = None; |
| 453 | self.connection.map_window(self.bar.window())?; |
| 454 | } |
| 455 | |
| 456 | self.selected_tags = tag_mask(tag_index); |
| 457 | |
| 458 | self.save_selected_tags()?; |
| 459 | |
| 460 | self.update_window_visibility()?; |
| 461 | self.apply_layout()?; |
| 462 | self.update_bar()?; |
| 463 | |
| 464 | let visible = self.visible_windows(); |
| 465 | self.set_focus(visible.first().copied())?; |
| 466 | |
| 467 | Ok(()) |
| 468 | } |
| 469 | |
| 470 | fn save_selected_tags(&self) -> Result<()> { |
| 471 | let net_current_desktop = self |
| 472 | .connection |
| 473 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| 474 | .reply()? |
| 475 | .atom; |
| 476 | |
| 477 | let desktop = self.selected_tags.trailing_zeros(); |
| 478 | |
| 479 | let bytes = (desktop as u32).to_ne_bytes(); |
| 480 | self.connection.change_property( |
| 481 | PropMode::REPLACE, |
| 482 | self.root, |
| 483 | net_current_desktop, |
| 484 | AtomEnum::CARDINAL, |
| 485 | 32, |
| 486 | 1, |
| 487 | &bytes, |
| 488 | )?; |
| 489 | |
| 490 | self.connection.flush()?; |
| 491 | Ok(()) |
| 492 | } |
| 493 | |
| 494 | pub fn move_to_tag(&mut self, tag_index: usize) -> Result<()> { |
| 495 | if tag_index >= self.config.tags.len() { |
| 496 | return Ok(()); |
| 497 | } |
| 498 | |
| 499 | if let Some(focused) = self.focused_window { |
| 500 | let mask = tag_mask(tag_index); |
| 501 | self.window_tags.insert(focused, mask); |
| 502 | |
| 503 | let _ = self.save_client_tag(focused, mask); |
| 504 | |
| 505 | self.update_window_visibility()?; |
| 506 | self.apply_layout()?; |
| 507 | self.update_bar()?; |
| 508 | } |
| 509 | |
| 510 | Ok(()) |
| 511 | } |
| 512 | |
| 513 | pub fn cycle_focus(&mut self, direction: i32) -> Result<()> { |
| 514 | let visible = self.visible_windows(); |
| 515 | |
| 516 | if visible.is_empty() { |
| 517 | return Ok(()); |
| 518 | } |
| 519 | |
| 520 | let next_window = if let Some(current) = self.focused_window { |
| 521 | if let Some(current_index) = visible.iter().position(|&w| w == current) { |
| 522 | let next_index = if direction > 0 { |
| 523 | (current_index + 1) % visible.len() |
| 524 | } else { |
| 525 | (current_index + visible.len() - 1) % visible.len() |
| 526 | }; |
| 527 | visible[next_index] |
| 528 | } else { |
| 529 | visible[0] |
| 530 | } |
| 531 | } else { |
| 532 | visible[0] |
| 533 | }; |
| 534 | |
| 535 | self.set_focus(Some(next_window))?; |
| 536 | Ok(()) |
| 537 | } |
| 538 | |
| 539 | pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> { |
| 540 | self.focused_window = window; |
| 541 | |
| 542 | if let Some(win) = window { |
| 543 | self.connection |
| 544 | .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?; |
| 545 | self.connection.flush()?; |
| 546 | } |
| 547 | |
| 548 | self.update_focus_visuals()?; |
| 549 | Ok(()) |
| 550 | } |
| 551 | |
| 552 | fn update_focus_visuals(&self) -> Result<()> { |
| 553 | for &window in &self.windows { |
| 554 | let (border_color, border_width) = if self.focused_window == Some(window) { |
| 555 | (self.config.border_focused, self.config.border_width) |
| 556 | } else { |
| 557 | (self.config.border_unfocused, self.config.border_width) |
| 558 | }; |
| 559 | |
| 560 | self.connection.configure_window( |
| 561 | window, |
| 562 | &ConfigureWindowAux::new().border_width(border_width), |
| 563 | )?; |
| 564 | |
| 565 | self.connection.change_window_attributes( |
| 566 | window, |
| 567 | &ChangeWindowAttributesAux::new().border_pixel(border_color), |
| 568 | )?; |
| 569 | } |
| 570 | self.connection.flush()?; |
| 571 | Ok(()) |
| 572 | } |
| 573 | |
| 574 | fn move_mouse(&mut self, window: Window) -> Result<()> { |
| 575 | self.floating_windows.insert(window); |
| 576 | |
| 577 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 578 | |
| 579 | self.connection |
| 580 | .grab_pointer( |
| 581 | false, |
| 582 | self.root, |
| 583 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), |
| 584 | GrabMode::ASYNC, |
| 585 | GrabMode::ASYNC, |
| 586 | x11rb::NONE, |
| 587 | x11rb::NONE, |
| 588 | x11rb::CURRENT_TIME, |
| 589 | )? |
| 590 | .reply()?; |
| 591 | |
| 592 | let pointer = self.connection.query_pointer(self.root)?.reply()?; |
| 593 | let (start_x, start_y) = (pointer.root_x, pointer.root_y); |
| 594 | |
| 595 | self.connection.configure_window( |
| 596 | window, |
| 597 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 598 | )?; |
| 599 | |
| 600 | loop { |
| 601 | let event = self.connection.wait_for_event()?; |
| 602 | match event { |
| 603 | Event::MotionNotify(e) => { |
| 604 | let new_x = geometry.x + (e.root_x - start_x); |
| 605 | let new_y = geometry.y + (e.root_y - start_y); |
| 606 | self.connection.configure_window( |
| 607 | window, |
| 608 | &ConfigureWindowAux::new().x(new_x as i32).y(new_y as i32), |
| 609 | )?; |
| 610 | self.connection.flush()?; |
| 611 | } |
| 612 | Event::ButtonRelease(_) => break, |
| 613 | _ => {} |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | self.connection |
| 618 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 619 | .check()?; |
| 620 | self.connection |
| 621 | .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? |
| 622 | .check()?; |
| 623 | |
| 624 | Ok(()) |
| 625 | } |
| 626 | |
| 627 | fn resize_mouse(&mut self, window: Window) -> Result<()> { |
| 628 | self.floating_windows.insert(window); |
| 629 | |
| 630 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 631 | |
| 632 | self.connection.warp_pointer( |
| 633 | x11rb::NONE, |
| 634 | window, |
| 635 | 0, |
| 636 | 0, |
| 637 | 0, |
| 638 | 0, |
| 639 | geometry.width as i16, |
| 640 | geometry.height as i16, |
| 641 | )?; |
| 642 | |
| 643 | self.connection |
| 644 | .grab_pointer( |
| 645 | false, |
| 646 | self.root, |
| 647 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), |
| 648 | GrabMode::ASYNC, |
| 649 | GrabMode::ASYNC, |
| 650 | x11rb::NONE, |
| 651 | x11rb::NONE, |
| 652 | x11rb::CURRENT_TIME, |
| 653 | )? |
| 654 | .reply()?; |
| 655 | |
| 656 | self.connection.configure_window( |
| 657 | window, |
| 658 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 659 | )?; |
| 660 | |
| 661 | loop { |
| 662 | let event = self.connection.wait_for_event()?; |
| 663 | match event { |
| 664 | Event::MotionNotify(e) => { |
| 665 | let new_width = (e.root_x - geometry.x).max(1) as u32; |
| 666 | let new_height = (e.root_y - geometry.y).max(1) as u32; |
| 667 | |
| 668 | self.connection.configure_window( |
| 669 | window, |
| 670 | &ConfigureWindowAux::new() |
| 671 | .width(new_width) |
| 672 | .height(new_height), |
| 673 | )?; |
| 674 | self.connection.flush()?; |
| 675 | } |
| 676 | Event::ButtonRelease(_) => break, |
| 677 | _ => {} |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | self.connection |
| 682 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 683 | .check()?; |
| 684 | self.connection |
| 685 | .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? |
| 686 | .check()?; |
| 687 | |
| 688 | Ok(()) |
| 689 | } |
| 690 | |
| 691 | fn handle_event(&mut self, event: Event) -> Result<Option<bool>> { |
| 692 | match event { |
| 693 | Event::MapRequest(event) => { |
| 694 | let attrs = match self.connection.get_window_attributes(event.window)?.reply() { |
| 695 | Ok(attrs) => attrs, |
| 696 | Err(_) => return Ok(None), |
| 697 | }; |
| 698 | |
| 699 | if attrs.override_redirect { |
| 700 | return Ok(None); |
| 701 | } |
| 702 | |
| 703 | if self.windows.contains(&event.window) { |
| 704 | return Ok(None); |
| 705 | } |
| 706 | |
| 707 | self.connection.map_window(event.window)?; |
| 708 | self.connection.change_window_attributes( |
| 709 | event.window, |
| 710 | &ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW), |
| 711 | )?; |
| 712 | |
| 713 | self.windows.push(event.window); |
| 714 | self.window_tags.insert(event.window, self.selected_tags); |
| 715 | self.set_wm_state(event.window, 1)?; |
| 716 | let _ = self.save_client_tag(event.window, self.selected_tags); |
| 717 | |
| 718 | self.apply_layout()?; |
| 719 | self.update_bar()?; |
| 720 | self.set_focus(Some(event.window))?; |
| 721 | } |
| 722 | Event::UnmapNotify(event) => { |
| 723 | if self.windows.contains(&event.window) && self.is_window_visible(event.window) { |
| 724 | self.remove_window(event.window)?; |
| 725 | } |
| 726 | } |
| 727 | Event::DestroyNotify(event) => { |
| 728 | if self.windows.contains(&event.window) { |
| 729 | self.remove_window(event.window)?; |
| 730 | } |
| 731 | } |
| 732 | Event::EnterNotify(event) => { |
| 733 | if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL { |
| 734 | return Ok(None); |
| 735 | } |
| 736 | if self.windows.contains(&event.event) { |
| 737 | self.set_focus(Some(event.event))?; |
| 738 | } |
| 739 | } |
| 740 | Event::KeyPress(event) => { |
| 741 | let (action, arg) = keyboard::handle_key_press(event, &self.config.keybindings)?; |
| 742 | match action { |
| 743 | KeyAction::Quit => return Ok(Some(false)), |
| 744 | KeyAction::Restart => return Ok(Some(true)), |
| 745 | _ => self.handle_key_action(action, &arg)?, |
| 746 | } |
| 747 | } |
| 748 | Event::ButtonPress(event) => { |
| 749 | if event.event == self.bar.window() { |
| 750 | if let Some(tag_index) = self.bar.handle_click(event.event_x) { |
| 751 | self.view_tag(tag_index)?; |
| 752 | } |
| 753 | } else if event.child != x11rb::NONE { |
| 754 | self.set_focus(Some(event.child))?; |
| 755 | |
| 756 | if event.detail == ButtonIndex::M1.into() { |
| 757 | self.move_mouse(event.child)?; |
| 758 | } else if event.detail == ButtonIndex::M3.into() { |
| 759 | self.resize_mouse(event.child)?; |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | Event::Expose(event) => { |
| 764 | if event.window == self.bar.window() { |
| 765 | self.bar.invalidate(); |
| 766 | self.update_bar()?; |
| 767 | } |
| 768 | } |
| 769 | _ => {} |
| 770 | } |
| 771 | Ok(None) |
| 772 | } |
| 773 | |
| 774 | fn apply_layout(&self) -> Result<()> { |
| 775 | if self.fullscreen_window.is_some() { |
| 776 | return Ok(()); |
| 777 | } |
| 778 | let screen_width = self.screen.width_in_pixels as u32; |
| 779 | let screen_height = self.screen.height_in_pixels as u32; |
| 780 | let border_width = self.config.border_width; |
| 781 | |
| 782 | let bar_height = self.bar.height() as u32; |
| 783 | let usable_height = screen_height.saturating_sub(bar_height); |
| 784 | |
| 785 | let gaps = if self.gaps_enabled { |
| 786 | GapConfig { |
| 787 | inner_horizontal: self.config.gap_inner_horizontal, |
| 788 | inner_vertical: self.config.gap_inner_vertical, |
| 789 | outer_horizontal: self.config.gap_outer_horizontal, |
| 790 | outer_vertical: self.config.gap_outer_vertical, |
| 791 | } |
| 792 | } else { |
| 793 | GapConfig { |
| 794 | inner_horizontal: 0, |
| 795 | inner_vertical: 0, |
| 796 | outer_horizontal: 0, |
| 797 | outer_vertical: 0, |
| 798 | } |
| 799 | }; |
| 800 | |
| 801 | let visible: Vec<Window> = self |
| 802 | .visible_windows() |
| 803 | .into_iter() |
| 804 | .filter(|w| !self.floating_windows.contains(w)) |
| 805 | .collect(); |
| 806 | |
| 807 | let geometries = self |
| 808 | .layout |
| 809 | .arrange(&visible, screen_width, usable_height, &gaps); |
| 810 | |
| 811 | for (window, geometry) in visible.iter().zip(geometries.iter()) { |
| 812 | let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
| 813 | let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
| 814 | |
| 815 | let adjusted_y = geometry.y_coordinate + bar_height as i32; |
| 816 | |
| 817 | self.connection.configure_window( |
| 818 | *window, |
| 819 | &ConfigureWindowAux::new() |
| 820 | .x(geometry.x_coordinate) |
| 821 | .y(adjusted_y) |
| 822 | .width(adjusted_width) |
| 823 | .height(adjusted_height), |
| 824 | )?; |
| 825 | } |
| 826 | self.connection.flush()?; |
| 827 | Ok(()) |
| 828 | } |
| 829 | |
| 830 | fn remove_window(&mut self, window: Window) -> Result<()> { |
| 831 | let initial_count = self.windows.len(); |
| 832 | self.windows.retain(|&w| w != window); |
| 833 | self.window_tags.remove(&window); |
| 834 | self.floating_windows.remove(&window); |
| 835 | |
| 836 | if self.fullscreen_window == Some(window) { |
| 837 | self.fullscreen_window = None; |
| 838 | self.connection.map_window(self.bar.window())?; |
| 839 | } |
| 840 | |
| 841 | if self.windows.len() < initial_count { |
| 842 | if self.focused_window == Some(window) { |
| 843 | let new_focus = if self.windows.is_empty() { |
| 844 | None |
| 845 | } else { |
| 846 | Some(self.windows[self.windows.len() - 1]) |
| 847 | }; |
| 848 | self.set_focus(new_focus)?; |
| 849 | } |
| 850 | |
| 851 | self.apply_layout()?; |
| 852 | self.update_bar()?; |
| 853 | } |
| 854 | Ok(()) |
| 855 | } |
| 856 | } |