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