| 1 | use crate::Config; |
| 2 | use crate::bar::Bar; |
| 3 | use crate::errors::WmError; |
| 4 | use crate::keyboard::{self, Arg, KeyAction, handlers}; |
| 5 | use crate::layout::GapConfig; |
| 6 | use crate::layout::tiling::TilingLayout; |
| 7 | use crate::layout::{Layout, layout_from_str, next_layout}; |
| 8 | use crate::monitor::{Monitor, detect_monitors}; |
| 9 | use std::collections::HashSet; |
| 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 | 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.intern_atom(false, b"WM_STATE")?.reply()?.atom; |
| 41 | |
| 42 | Ok(Self { |
| 43 | net_current_desktop, |
| 44 | net_client_info, |
| 45 | wm_state, |
| 46 | }) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub struct WindowManager { |
| 51 | config: Config, |
| 52 | connection: RustConnection, |
| 53 | screen_number: usize, |
| 54 | root: Window, |
| 55 | screen: Screen, |
| 56 | windows: Vec<Window>, |
| 57 | layout: Box<dyn Layout>, |
| 58 | window_tags: std::collections::HashMap<Window, TagMask>, |
| 59 | window_monitor: std::collections::HashMap<Window, usize>, |
| 60 | gaps_enabled: bool, |
| 61 | fullscreen_window: Option<Window>, |
| 62 | floating_windows: HashSet<Window>, |
| 63 | bars: Vec<Bar>, |
| 64 | monitors: Vec<Monitor>, |
| 65 | selected_monitor: usize, |
| 66 | atoms: AtomCache, |
| 67 | previous_focused: Option<Window>, |
| 68 | display: *mut x11::xlib::Display, |
| 69 | font: crate::bar::font::Font, |
| 70 | } |
| 71 | |
| 72 | type WmResult<T> = Result<T, WmError>; |
| 73 | |
| 74 | impl WindowManager { |
| 75 | pub fn new(config: Config) -> WmResult<Self> { |
| 76 | let (connection, screen_number) = x11rb::connect(None)?; |
| 77 | let root = connection.setup().roots[screen_number].root; |
| 78 | let screen = connection.setup().roots[screen_number].clone(); |
| 79 | |
| 80 | let normal_cursor = CursorHandle::new( |
| 81 | &connection, |
| 82 | screen_number, |
| 83 | &x11rb::resource_manager::new_from_default(&connection)?, |
| 84 | )? |
| 85 | .reply()? |
| 86 | .load_cursor(&connection, "left_ptr")?; |
| 87 | |
| 88 | connection |
| 89 | .change_window_attributes( |
| 90 | root, |
| 91 | &ChangeWindowAttributesAux::new() |
| 92 | .cursor(normal_cursor) |
| 93 | .event_mask( |
| 94 | EventMask::SUBSTRUCTURE_REDIRECT |
| 95 | | EventMask::SUBSTRUCTURE_NOTIFY |
| 96 | | EventMask::PROPERTY_CHANGE |
| 97 | | EventMask::KEY_PRESS |
| 98 | | EventMask::BUTTON_PRESS |
| 99 | | EventMask::POINTER_MOTION, |
| 100 | ), |
| 101 | )? |
| 102 | .check()?; |
| 103 | |
| 104 | connection.grab_button( |
| 105 | false, |
| 106 | root, |
| 107 | EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, |
| 108 | GrabMode::SYNC, |
| 109 | GrabMode::ASYNC, |
| 110 | x11rb::NONE, |
| 111 | x11rb::NONE, |
| 112 | ButtonIndex::M1, |
| 113 | u16::from(config.modkey).into(), |
| 114 | )?; |
| 115 | |
| 116 | connection.grab_button( |
| 117 | false, |
| 118 | root, |
| 119 | EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, |
| 120 | GrabMode::SYNC, |
| 121 | GrabMode::ASYNC, |
| 122 | x11rb::NONE, |
| 123 | x11rb::NONE, |
| 124 | ButtonIndex::M3, |
| 125 | u16::from(config.modkey).into(), |
| 126 | )?; |
| 127 | |
| 128 | let monitors = detect_monitors(&connection, &screen, root)?; |
| 129 | |
| 130 | let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) }; |
| 131 | if display.is_null() { |
| 132 | return Err(WmError::X11(crate::errors::X11Error::DisplayOpenFailed)); |
| 133 | } |
| 134 | |
| 135 | let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?; |
| 136 | |
| 137 | let mut bars = Vec::new(); |
| 138 | for monitor in monitors.iter() { |
| 139 | let bar = Bar::new( |
| 140 | &connection, |
| 141 | &screen, |
| 142 | screen_number, |
| 143 | &config, |
| 144 | display, |
| 145 | &font, |
| 146 | monitor.x as i16, |
| 147 | monitor.y as i16, |
| 148 | monitor.width as u16, |
| 149 | )?; |
| 150 | bars.push(bar); |
| 151 | } |
| 152 | |
| 153 | let gaps_enabled = config.gaps_enabled; |
| 154 | |
| 155 | let atoms = AtomCache::new(&connection)?; |
| 156 | |
| 157 | let mut window_manager = Self { |
| 158 | config, |
| 159 | connection, |
| 160 | screen_number, |
| 161 | root, |
| 162 | screen, |
| 163 | windows: Vec::new(), |
| 164 | layout: Box::new(TilingLayout), |
| 165 | window_tags: std::collections::HashMap::new(), |
| 166 | window_monitor: std::collections::HashMap::new(), |
| 167 | gaps_enabled, |
| 168 | fullscreen_window: None, |
| 169 | floating_windows: HashSet::new(), |
| 170 | bars, |
| 171 | monitors, |
| 172 | selected_monitor: 0, |
| 173 | atoms, |
| 174 | previous_focused: None, |
| 175 | display, |
| 176 | font, |
| 177 | }; |
| 178 | |
| 179 | window_manager.scan_existing_windows()?; |
| 180 | window_manager.update_bar()?; |
| 181 | |
| 182 | Ok(window_manager) |
| 183 | } |
| 184 | |
| 185 | fn get_saved_selected_tags( |
| 186 | connection: &RustConnection, |
| 187 | root: Window, |
| 188 | tag_count: usize, |
| 189 | ) -> WmResult<TagMask> { |
| 190 | let net_current_desktop = connection |
| 191 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| 192 | .reply()? |
| 193 | .atom; |
| 194 | |
| 195 | match connection |
| 196 | .get_property(false, root, net_current_desktop, AtomEnum::CARDINAL, 0, 1)? |
| 197 | .reply() |
| 198 | { |
| 199 | Ok(prop) if prop.value.len() >= 4 => { |
| 200 | let desktop = u32::from_ne_bytes([ |
| 201 | prop.value[0], |
| 202 | prop.value[1], |
| 203 | prop.value[2], |
| 204 | prop.value[3], |
| 205 | ]); |
| 206 | if desktop < tag_count as u32 { |
| 207 | let mask = tag_mask(desktop as usize); |
| 208 | return Ok(mask); |
| 209 | } |
| 210 | } |
| 211 | Ok(_) => {} |
| 212 | Err(e) => { |
| 213 | eprintln!("No _NET_CURRENT_DESKTOP property ({})", e); |
| 214 | } |
| 215 | } |
| 216 | Ok(tag_mask(0)) |
| 217 | } |
| 218 | |
| 219 | fn scan_existing_windows(&mut self) -> WmResult<()> { |
| 220 | let tree = self.connection.query_tree(self.root)?.reply()?; |
| 221 | let net_client_info = self.atoms.net_client_info; |
| 222 | let wm_state_atom = self.atoms.wm_state; |
| 223 | |
| 224 | for &window in &tree.children { |
| 225 | if self.bars.iter().any(|bar| bar.window() == window) { |
| 226 | continue; |
| 227 | } |
| 228 | |
| 229 | let Ok(attrs) = self.connection.get_window_attributes(window)?.reply() else { |
| 230 | continue; |
| 231 | }; |
| 232 | |
| 233 | if attrs.override_redirect { |
| 234 | continue; |
| 235 | } |
| 236 | |
| 237 | if attrs.map_state == MapState::VIEWABLE { |
| 238 | let tag = self.get_saved_tag(window, net_client_info)?; |
| 239 | self.windows.push(window); |
| 240 | self.window_tags.insert(window, tag); |
| 241 | self.window_monitor.insert(window, self.selected_monitor); |
| 242 | continue; |
| 243 | } |
| 244 | |
| 245 | if attrs.map_state == MapState::UNMAPPED { |
| 246 | let has_wm_state = self |
| 247 | .connection |
| 248 | .get_property(false, window, wm_state_atom, AtomEnum::ANY, 0, 2)? |
| 249 | .reply() |
| 250 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 251 | |
| 252 | if !has_wm_state { |
| 253 | continue; |
| 254 | } |
| 255 | |
| 256 | let has_wm_class = self |
| 257 | .connection |
| 258 | .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)? |
| 259 | .reply() |
| 260 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 261 | |
| 262 | if has_wm_class { |
| 263 | let tag = self.get_saved_tag(window, net_client_info)?; |
| 264 | self.windows.push(window); |
| 265 | self.window_tags.insert(window, tag); |
| 266 | self.window_monitor.insert(window, self.selected_monitor); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | if let Some(&first) = self.windows.first() { |
| 272 | self.set_focus(first)?; |
| 273 | } |
| 274 | |
| 275 | self.apply_layout()?; |
| 276 | Ok(()) |
| 277 | } |
| 278 | |
| 279 | fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> WmResult<TagMask> { |
| 280 | match self |
| 281 | .connection |
| 282 | .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)? |
| 283 | .reply() |
| 284 | { |
| 285 | Ok(prop) if prop.value.len() >= 4 => { |
| 286 | let tags = u32::from_ne_bytes([ |
| 287 | prop.value[0], |
| 288 | prop.value[1], |
| 289 | prop.value[2], |
| 290 | prop.value[3], |
| 291 | ]); |
| 292 | |
| 293 | if tags != 0 && tags < (1 << self.config.tags.len()) { |
| 294 | return Ok(tags); |
| 295 | } |
| 296 | } |
| 297 | Ok(_) => {} |
| 298 | Err(e) => { |
| 299 | eprintln!("No _NET_CLIENT_INFO property ({})", e); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | Ok(self |
| 304 | .monitors |
| 305 | .get(self.selected_monitor) |
| 306 | .map(|m| m.selected_tags) |
| 307 | .unwrap_or(tag_mask(0))) |
| 308 | } |
| 309 | |
| 310 | fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { |
| 311 | let net_client_info = self.atoms.net_client_info; |
| 312 | |
| 313 | let bytes = tag.to_ne_bytes().to_vec(); |
| 314 | |
| 315 | self.connection.change_property( |
| 316 | PropMode::REPLACE, |
| 317 | window, |
| 318 | net_client_info, |
| 319 | AtomEnum::CARDINAL, |
| 320 | 32, |
| 321 | 1, |
| 322 | &bytes, |
| 323 | )?; |
| 324 | |
| 325 | self.connection.flush()?; |
| 326 | Ok(()) |
| 327 | } |
| 328 | |
| 329 | fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> { |
| 330 | let wm_state_atom = self.atoms.wm_state; |
| 331 | |
| 332 | let data = [state, 0u32]; |
| 333 | let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); |
| 334 | |
| 335 | self.connection.change_property( |
| 336 | PropMode::REPLACE, |
| 337 | window, |
| 338 | wm_state_atom, |
| 339 | wm_state_atom, |
| 340 | 32, |
| 341 | 2, |
| 342 | &bytes, |
| 343 | )?; |
| 344 | |
| 345 | self.connection.flush()?; |
| 346 | Ok(()) |
| 347 | } |
| 348 | |
| 349 | pub fn run(&mut self) -> WmResult<bool> { |
| 350 | println!("oxwm started on display {}", self.screen_number); |
| 351 | |
| 352 | keyboard::setup_keybinds(&self.connection, self.root, &self.config.keybindings)?; |
| 353 | self.update_bar()?; |
| 354 | |
| 355 | loop { |
| 356 | while let Some(event) = self.connection.poll_for_event()? { |
| 357 | if let Some(should_restart) = self.handle_event(event)? { |
| 358 | return Ok(should_restart); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | if let Some(bar) = self.bars.get_mut(self.selected_monitor) { |
| 363 | bar.update_blocks(); |
| 364 | } |
| 365 | |
| 366 | if self.bars.iter().any(|bar| bar.needs_redraw()) { |
| 367 | self.update_bar()?; |
| 368 | } |
| 369 | |
| 370 | std::thread::sleep(std::time::Duration::from_millis(100)); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | fn toggle_floating(&mut self) -> WmResult<()> { |
| 375 | if let Some(focused) = self |
| 376 | .monitors |
| 377 | .get(self.selected_monitor) |
| 378 | .and_then(|m| m.focused_window) |
| 379 | { |
| 380 | if self.floating_windows.contains(&focused) { |
| 381 | self.floating_windows.remove(&focused); |
| 382 | self.apply_layout()?; |
| 383 | } else { |
| 384 | let float_width = (self.screen.width_in_pixels / 2) as u32; |
| 385 | let float_height = (self.screen.height_in_pixels / 2) as u32; |
| 386 | |
| 387 | let border_width = self.config.border_width; |
| 388 | |
| 389 | let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32; |
| 390 | let center_height = |
| 391 | ((self.screen.height_in_pixels - float_height as u16) / 2) as i32; |
| 392 | |
| 393 | self.connection.configure_window( |
| 394 | focused, |
| 395 | &ConfigureWindowAux::new() |
| 396 | .x(center_width) |
| 397 | .y(center_height) |
| 398 | .width(float_width) |
| 399 | .height(float_height) |
| 400 | .border_width(border_width) |
| 401 | .stack_mode(StackMode::ABOVE), |
| 402 | )?; |
| 403 | |
| 404 | self.floating_windows.insert(focused); |
| 405 | self.apply_layout()?; |
| 406 | self.connection.flush()?; |
| 407 | } |
| 408 | } |
| 409 | Ok(()) |
| 410 | } |
| 411 | |
| 412 | fn toggle_fullscreen(&mut self) -> WmResult<()> { |
| 413 | if let Some(focused) = self |
| 414 | .monitors |
| 415 | .get(self.selected_monitor) |
| 416 | .and_then(|m| m.focused_window) |
| 417 | { |
| 418 | if self.fullscreen_window == Some(focused) { |
| 419 | self.fullscreen_window = None; |
| 420 | |
| 421 | for bar in &self.bars { |
| 422 | self.connection.map_window(bar.window())?; |
| 423 | } |
| 424 | |
| 425 | self.apply_layout()?; |
| 426 | } else { |
| 427 | self.fullscreen_window = Some(focused); |
| 428 | |
| 429 | if let Some(bar) = self.bars.get(self.selected_monitor) { |
| 430 | self.connection.unmap_window(bar.window())?; |
| 431 | } |
| 432 | |
| 433 | let monitor = &self.monitors[self.selected_monitor]; |
| 434 | let screen_width = monitor.width; |
| 435 | let screen_height = monitor.height; |
| 436 | |
| 437 | self.connection.configure_window( |
| 438 | focused, |
| 439 | &ConfigureWindowAux::new() |
| 440 | .x(monitor.x) |
| 441 | .y(monitor.y) |
| 442 | .width(screen_width) |
| 443 | .height(screen_height) |
| 444 | .border_width(0) |
| 445 | .stack_mode(StackMode::ABOVE), |
| 446 | )?; |
| 447 | |
| 448 | self.connection.flush()?; |
| 449 | } |
| 450 | } |
| 451 | Ok(()) |
| 452 | } |
| 453 | |
| 454 | fn update_bar(&mut self) -> WmResult<()> { |
| 455 | for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 456 | if let Some(bar) = self.bars.get_mut(monitor_index) { |
| 457 | let mut occupied_tags: TagMask = 0; |
| 458 | for (&window, &tags) in &self.window_tags { |
| 459 | if self.window_monitor.get(&window).copied().unwrap_or(0) == monitor_index { |
| 460 | occupied_tags |= tags; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | let draw_blocks = monitor_index == self.selected_monitor; |
| 465 | bar.invalidate(); |
| 466 | bar.draw( |
| 467 | &self.connection, |
| 468 | &self.font, |
| 469 | self.display, |
| 470 | monitor.selected_tags, |
| 471 | occupied_tags, |
| 472 | draw_blocks, |
| 473 | )?; |
| 474 | } |
| 475 | } |
| 476 | Ok(()) |
| 477 | } |
| 478 | |
| 479 | fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> WmResult<()> { |
| 480 | match action { |
| 481 | KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, |
| 482 | KeyAction::KillClient => { |
| 483 | if let Some(focused) = self |
| 484 | .monitors |
| 485 | .get(self.selected_monitor) |
| 486 | .and_then(|m| m.focused_window) |
| 487 | { |
| 488 | match self.connection.kill_client(focused) { |
| 489 | Ok(_) => { |
| 490 | self.connection.flush()?; |
| 491 | } |
| 492 | Err(e) => { |
| 493 | eprintln!("Failed to kill window {}: {}", focused, e); |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | KeyAction::ToggleFullScreen => { |
| 499 | self.toggle_fullscreen()?; |
| 500 | } |
| 501 | KeyAction::ChangeLayout => { |
| 502 | if let Arg::Str(layout_name) = arg { |
| 503 | match layout_from_str(layout_name) { |
| 504 | Ok(layout) => { |
| 505 | self.layout = layout; |
| 506 | self.apply_layout()?; |
| 507 | } |
| 508 | Err(e) => eprintln!("Failed to change layout: {}", e), |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | KeyAction::CycleLayout => { |
| 513 | let current_name = self.layout.name(); |
| 514 | let next_name = next_layout(current_name); |
| 515 | match layout_from_str(next_name) { |
| 516 | Ok(layout) => { |
| 517 | self.layout = layout; |
| 518 | self.apply_layout()?; |
| 519 | } |
| 520 | Err(e) => eprintln!("Failed to cycle layout: {}", e), |
| 521 | } |
| 522 | } |
| 523 | KeyAction::ToggleFloating => { |
| 524 | self.toggle_floating()?; |
| 525 | } |
| 526 | |
| 527 | KeyAction::FocusStack => { |
| 528 | if let Arg::Int(direction) = arg { |
| 529 | self.cycle_focus(*direction)?; |
| 530 | } |
| 531 | } |
| 532 | KeyAction::Quit | KeyAction::Restart => { |
| 533 | // Handled in handle_event |
| 534 | } |
| 535 | KeyAction::Recompile => { |
| 536 | match std::process::Command::new("oxwm") |
| 537 | .arg("--recompile") |
| 538 | .spawn() |
| 539 | { |
| 540 | Ok(_) => eprintln!("Recompiling in background"), |
| 541 | Err(e) => eprintln!("Failed to spawn recompile: {}", e), |
| 542 | } |
| 543 | } |
| 544 | KeyAction::ViewTag => { |
| 545 | if let Arg::Int(tag_index) = arg { |
| 546 | self.view_tag(*tag_index as usize)?; |
| 547 | } |
| 548 | } |
| 549 | KeyAction::MoveToTag => { |
| 550 | if let Arg::Int(tag_index) = arg { |
| 551 | self.move_to_tag(*tag_index as usize)?; |
| 552 | } |
| 553 | } |
| 554 | KeyAction::ToggleGaps => { |
| 555 | self.gaps_enabled = !self.gaps_enabled; |
| 556 | self.apply_layout()?; |
| 557 | } |
| 558 | KeyAction::FocusMonitor => { |
| 559 | if let Arg::Int(direction) = arg { |
| 560 | self.focus_monitor(*direction)?; |
| 561 | } |
| 562 | } |
| 563 | KeyAction::None => {} |
| 564 | } |
| 565 | Ok(()) |
| 566 | } |
| 567 | |
| 568 | fn focus_monitor(&mut self, direction: i32) -> WmResult<()> { |
| 569 | if self.monitors.is_empty() { |
| 570 | return Ok(()); |
| 571 | } |
| 572 | |
| 573 | let new_monitor = if direction > 0 { |
| 574 | (self.selected_monitor + 1) % self.monitors.len() |
| 575 | } else { |
| 576 | (self.selected_monitor + self.monitors.len() - 1) % self.monitors.len() |
| 577 | }; |
| 578 | |
| 579 | if new_monitor == self.selected_monitor { |
| 580 | return Ok(()); |
| 581 | } |
| 582 | |
| 583 | self.selected_monitor = new_monitor; |
| 584 | |
| 585 | self.update_bar()?; |
| 586 | |
| 587 | let visible = self.visible_windows_on_monitor(new_monitor); |
| 588 | if let Some(&win) = visible.first() { |
| 589 | self.set_focus(win)?; |
| 590 | } |
| 591 | |
| 592 | Ok(()) |
| 593 | } |
| 594 | |
| 595 | fn is_window_visible(&self, window: Window) -> bool { |
| 596 | let window_mon = self.window_monitor.get(&window).copied().unwrap_or(0); |
| 597 | |
| 598 | if let Some(&tags) = self.window_tags.get(&window) { |
| 599 | let monitor = self.monitors.get(window_mon); |
| 600 | let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0); |
| 601 | (tags & selected_tags) != 0 |
| 602 | } else { |
| 603 | false |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | fn visible_windows(&self) -> Vec<Window> { |
| 608 | self.windows |
| 609 | .iter() |
| 610 | .filter(|&&w| self.is_window_visible(w)) |
| 611 | .copied() |
| 612 | .collect() |
| 613 | } |
| 614 | |
| 615 | fn visible_windows_on_monitor(&self, monitor_index: usize) -> Vec<Window> { |
| 616 | self.windows |
| 617 | .iter() |
| 618 | .filter(|&&w| { |
| 619 | let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0); |
| 620 | if window_mon != monitor_index { |
| 621 | return false; |
| 622 | } |
| 623 | if let Some(&tags) = self.window_tags.get(&w) { |
| 624 | let monitor = self.monitors.get(monitor_index); |
| 625 | let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0); |
| 626 | (tags & selected_tags) != 0 |
| 627 | } else { |
| 628 | false |
| 629 | } |
| 630 | }) |
| 631 | .copied() |
| 632 | .collect() |
| 633 | } |
| 634 | |
| 635 | fn get_monitor_at_point(&self, x: i32, y: i32) -> Option<usize> { |
| 636 | self.monitors |
| 637 | .iter() |
| 638 | .position(|mon| mon.contains_point(x, y)) |
| 639 | } |
| 640 | |
| 641 | fn update_window_visibility(&self) -> WmResult<()> { |
| 642 | for &window in &self.windows { |
| 643 | if self.is_window_visible(window) { |
| 644 | self.connection.map_window(window)?; |
| 645 | } else { |
| 646 | self.connection.unmap_window(window)?; |
| 647 | } |
| 648 | } |
| 649 | self.connection.flush()?; |
| 650 | Ok(()) |
| 651 | } |
| 652 | |
| 653 | pub fn view_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 654 | if tag_index >= self.config.tags.len() { |
| 655 | return Ok(()); |
| 656 | } |
| 657 | |
| 658 | if self.fullscreen_window.is_some() { |
| 659 | self.fullscreen_window = None; |
| 660 | for bar in &self.bars { |
| 661 | self.connection.map_window(bar.window())?; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 666 | monitor.selected_tags = tag_mask(tag_index); |
| 667 | } |
| 668 | |
| 669 | self.save_selected_tags()?; |
| 670 | |
| 671 | self.update_window_visibility()?; |
| 672 | self.apply_layout()?; |
| 673 | self.update_bar()?; |
| 674 | |
| 675 | let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 676 | if let Some(&win) = visible.first() { |
| 677 | self.set_focus(win)?; |
| 678 | } |
| 679 | |
| 680 | Ok(()) |
| 681 | } |
| 682 | |
| 683 | fn save_selected_tags(&self) -> WmResult<()> { |
| 684 | let net_current_desktop = self.atoms.net_current_desktop; |
| 685 | |
| 686 | let selected_tags = self |
| 687 | .monitors |
| 688 | .get(self.selected_monitor) |
| 689 | .map(|m| m.selected_tags) |
| 690 | .unwrap_or(tag_mask(0)); |
| 691 | let desktop = selected_tags.trailing_zeros(); |
| 692 | |
| 693 | let bytes = (desktop as u32).to_ne_bytes(); |
| 694 | self.connection.change_property( |
| 695 | PropMode::REPLACE, |
| 696 | self.root, |
| 697 | net_current_desktop, |
| 698 | AtomEnum::CARDINAL, |
| 699 | 32, |
| 700 | 1, |
| 701 | &bytes, |
| 702 | )?; |
| 703 | |
| 704 | self.connection.flush()?; |
| 705 | Ok(()) |
| 706 | } |
| 707 | |
| 708 | pub fn move_to_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 709 | if tag_index >= self.config.tags.len() { |
| 710 | return Ok(()); |
| 711 | } |
| 712 | |
| 713 | if let Some(focused) = self |
| 714 | .monitors |
| 715 | .get(self.selected_monitor) |
| 716 | .and_then(|m| m.focused_window) |
| 717 | { |
| 718 | let mask = tag_mask(tag_index); |
| 719 | self.window_tags.insert(focused, mask); |
| 720 | |
| 721 | let _ = self.save_client_tag(focused, mask); |
| 722 | |
| 723 | self.update_window_visibility()?; |
| 724 | self.apply_layout()?; |
| 725 | self.update_bar()?; |
| 726 | } |
| 727 | |
| 728 | Ok(()) |
| 729 | } |
| 730 | |
| 731 | pub fn cycle_focus(&mut self, direction: i32) -> WmResult<()> { |
| 732 | let visible = self.visible_windows(); |
| 733 | |
| 734 | if visible.is_empty() { |
| 735 | return Ok(()); |
| 736 | } |
| 737 | |
| 738 | let current = self |
| 739 | .monitors |
| 740 | .get(self.selected_monitor) |
| 741 | .and_then(|m| m.focused_window); |
| 742 | |
| 743 | let next_window = if let Some(current) = current { |
| 744 | if let Some(current_index) = visible.iter().position(|&w| w == current) { |
| 745 | let next_index = if direction > 0 { |
| 746 | (current_index + 1) % visible.len() |
| 747 | } else { |
| 748 | (current_index + visible.len() - 1) % visible.len() |
| 749 | }; |
| 750 | visible[next_index] |
| 751 | } else { |
| 752 | visible[0] |
| 753 | } |
| 754 | } else { |
| 755 | visible[0] |
| 756 | }; |
| 757 | |
| 758 | self.set_focus(next_window)?; |
| 759 | Ok(()) |
| 760 | } |
| 761 | |
| 762 | pub fn set_focus(&mut self, window: Window) -> WmResult<()> { |
| 763 | let old_focused = self.previous_focused; |
| 764 | |
| 765 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 766 | monitor.focused_window = Some(window); |
| 767 | } |
| 768 | |
| 769 | self.connection |
| 770 | .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?; |
| 771 | self.connection.flush()?; |
| 772 | |
| 773 | self.update_focus_visuals(old_focused, window)?; |
| 774 | self.previous_focused = Some(window); |
| 775 | Ok(()) |
| 776 | } |
| 777 | |
| 778 | fn update_focus_visuals( |
| 779 | &self, |
| 780 | old_focused: Option<Window>, |
| 781 | new_focused: Window, |
| 782 | ) -> WmResult<()> { |
| 783 | if let Some(old_win) = old_focused { |
| 784 | if old_win != new_focused { |
| 785 | self.connection.configure_window( |
| 786 | old_win, |
| 787 | &ConfigureWindowAux::new().border_width(self.config.border_width), |
| 788 | )?; |
| 789 | |
| 790 | self.connection.change_window_attributes( |
| 791 | old_win, |
| 792 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), |
| 793 | )?; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | self.connection.configure_window( |
| 798 | new_focused, |
| 799 | &ConfigureWindowAux::new().border_width(self.config.border_width), |
| 800 | )?; |
| 801 | |
| 802 | self.connection.change_window_attributes( |
| 803 | new_focused, |
| 804 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused), |
| 805 | )?; |
| 806 | |
| 807 | self.connection.flush()?; |
| 808 | Ok(()) |
| 809 | } |
| 810 | |
| 811 | fn move_mouse(&mut self, window: Window) -> WmResult<()> { |
| 812 | self.floating_windows.insert(window); |
| 813 | |
| 814 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 815 | |
| 816 | self.connection |
| 817 | .grab_pointer( |
| 818 | false, |
| 819 | self.root, |
| 820 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), |
| 821 | GrabMode::ASYNC, |
| 822 | GrabMode::ASYNC, |
| 823 | x11rb::NONE, |
| 824 | x11rb::NONE, |
| 825 | x11rb::CURRENT_TIME, |
| 826 | )? |
| 827 | .reply()?; |
| 828 | |
| 829 | let pointer = self.connection.query_pointer(self.root)?.reply()?; |
| 830 | let (start_x, start_y) = (pointer.root_x, pointer.root_y); |
| 831 | |
| 832 | self.connection.configure_window( |
| 833 | window, |
| 834 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 835 | )?; |
| 836 | |
| 837 | loop { |
| 838 | let event = self.connection.wait_for_event()?; |
| 839 | match event { |
| 840 | Event::MotionNotify(e) => { |
| 841 | let new_x = geometry.x + (e.root_x - start_x); |
| 842 | let new_y = geometry.y + (e.root_y - start_y); |
| 843 | self.connection.configure_window( |
| 844 | window, |
| 845 | &ConfigureWindowAux::new().x(new_x as i32).y(new_y as i32), |
| 846 | )?; |
| 847 | self.connection.flush()?; |
| 848 | } |
| 849 | Event::ButtonRelease(_) => break, |
| 850 | _ => {} |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | self.connection |
| 855 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 856 | .check()?; |
| 857 | self.connection |
| 858 | .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? |
| 859 | .check()?; |
| 860 | |
| 861 | Ok(()) |
| 862 | } |
| 863 | |
| 864 | fn resize_mouse(&mut self, window: Window) -> WmResult<()> { |
| 865 | self.floating_windows.insert(window); |
| 866 | |
| 867 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 868 | |
| 869 | self.connection.warp_pointer( |
| 870 | x11rb::NONE, |
| 871 | window, |
| 872 | 0, |
| 873 | 0, |
| 874 | 0, |
| 875 | 0, |
| 876 | geometry.width as i16, |
| 877 | geometry.height as i16, |
| 878 | )?; |
| 879 | |
| 880 | self.connection |
| 881 | .grab_pointer( |
| 882 | false, |
| 883 | self.root, |
| 884 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), |
| 885 | GrabMode::ASYNC, |
| 886 | GrabMode::ASYNC, |
| 887 | x11rb::NONE, |
| 888 | x11rb::NONE, |
| 889 | x11rb::CURRENT_TIME, |
| 890 | )? |
| 891 | .reply()?; |
| 892 | |
| 893 | self.connection.configure_window( |
| 894 | window, |
| 895 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 896 | )?; |
| 897 | |
| 898 | loop { |
| 899 | let event = self.connection.wait_for_event()?; |
| 900 | match event { |
| 901 | Event::MotionNotify(e) => { |
| 902 | let new_width = (e.root_x - geometry.x).max(1) as u32; |
| 903 | let new_height = (e.root_y - geometry.y).max(1) as u32; |
| 904 | |
| 905 | self.connection.configure_window( |
| 906 | window, |
| 907 | &ConfigureWindowAux::new() |
| 908 | .width(new_width) |
| 909 | .height(new_height), |
| 910 | )?; |
| 911 | self.connection.flush()?; |
| 912 | } |
| 913 | Event::ButtonRelease(_) => break, |
| 914 | _ => {} |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | self.connection |
| 919 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 920 | .check()?; |
| 921 | self.connection |
| 922 | .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? |
| 923 | .check()?; |
| 924 | |
| 925 | Ok(()) |
| 926 | } |
| 927 | |
| 928 | fn handle_event(&mut self, event: Event) -> WmResult<Option<bool>> { |
| 929 | match event { |
| 930 | Event::MapRequest(event) => { |
| 931 | let attrs = match self.connection.get_window_attributes(event.window)?.reply() { |
| 932 | Ok(attrs) => attrs, |
| 933 | Err(_) => return Ok(None), |
| 934 | }; |
| 935 | |
| 936 | if attrs.override_redirect { |
| 937 | return Ok(None); |
| 938 | } |
| 939 | |
| 940 | if self.windows.contains(&event.window) { |
| 941 | return Ok(None); |
| 942 | } |
| 943 | |
| 944 | self.connection.map_window(event.window)?; |
| 945 | self.connection.change_window_attributes( |
| 946 | event.window, |
| 947 | &ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW), |
| 948 | )?; |
| 949 | |
| 950 | let selected_tags = self |
| 951 | .monitors |
| 952 | .get(self.selected_monitor) |
| 953 | .map(|m| m.selected_tags) |
| 954 | .unwrap_or(tag_mask(0)); |
| 955 | |
| 956 | self.windows.push(event.window); |
| 957 | self.window_tags.insert(event.window, selected_tags); |
| 958 | self.window_monitor |
| 959 | .insert(event.window, self.selected_monitor); |
| 960 | self.set_wm_state(event.window, 1)?; |
| 961 | let _ = self.save_client_tag(event.window, selected_tags); |
| 962 | |
| 963 | self.apply_layout()?; |
| 964 | self.update_bar()?; |
| 965 | self.set_focus(event.window)?; |
| 966 | } |
| 967 | Event::UnmapNotify(event) => { |
| 968 | if self.windows.contains(&event.window) && self.is_window_visible(event.window) { |
| 969 | self.remove_window(event.window)?; |
| 970 | } |
| 971 | } |
| 972 | Event::DestroyNotify(event) => { |
| 973 | if self.windows.contains(&event.window) { |
| 974 | self.remove_window(event.window)?; |
| 975 | } |
| 976 | } |
| 977 | Event::EnterNotify(event) => { |
| 978 | if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL { |
| 979 | return Ok(None); |
| 980 | } |
| 981 | if self.windows.contains(&event.event) { |
| 982 | self.set_focus(event.event)?; |
| 983 | } |
| 984 | } |
| 985 | Event::MotionNotify(event) => { |
| 986 | if event.event != self.root { |
| 987 | return Ok(None); |
| 988 | } |
| 989 | |
| 990 | if let Some(monitor_index) = |
| 991 | self.get_monitor_at_point(event.root_x as i32, event.root_y as i32) |
| 992 | { |
| 993 | if monitor_index != self.selected_monitor { |
| 994 | self.selected_monitor = monitor_index; |
| 995 | self.update_bar()?; |
| 996 | |
| 997 | let visible = self.visible_windows_on_monitor(monitor_index); |
| 998 | if let Some(&win) = visible.first() { |
| 999 | self.set_focus(win)?; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | Event::KeyPress(event) => { |
| 1005 | let (action, arg) = keyboard::handle_key_press(event, &self.config.keybindings); |
| 1006 | match action { |
| 1007 | KeyAction::Quit => return Ok(Some(false)), |
| 1008 | KeyAction::Restart => return Ok(Some(true)), |
| 1009 | _ => self.handle_key_action(action, &arg)?, |
| 1010 | } |
| 1011 | } |
| 1012 | Event::ButtonPress(event) => { |
| 1013 | let is_bar_click = self |
| 1014 | .bars |
| 1015 | .iter() |
| 1016 | .enumerate() |
| 1017 | .find(|(_, bar)| bar.window() == event.event); |
| 1018 | |
| 1019 | if let Some((monitor_index, bar)) = is_bar_click { |
| 1020 | if let Some(tag_index) = bar.handle_click(event.event_x) { |
| 1021 | if monitor_index != self.selected_monitor { |
| 1022 | self.selected_monitor = monitor_index; |
| 1023 | } |
| 1024 | self.view_tag(tag_index)?; |
| 1025 | } |
| 1026 | } else if event.child != x11rb::NONE { |
| 1027 | self.set_focus(event.child)?; |
| 1028 | |
| 1029 | if event.detail == ButtonIndex::M1.into() { |
| 1030 | self.move_mouse(event.child)?; |
| 1031 | } else if event.detail == ButtonIndex::M3.into() { |
| 1032 | self.resize_mouse(event.child)?; |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | Event::Expose(event) => { |
| 1037 | for bar in &mut self.bars { |
| 1038 | if event.window == bar.window() { |
| 1039 | bar.invalidate(); |
| 1040 | self.update_bar()?; |
| 1041 | break; |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | _ => {} |
| 1046 | } |
| 1047 | Ok(None) |
| 1048 | } |
| 1049 | |
| 1050 | fn apply_layout(&self) -> WmResult<()> { |
| 1051 | if self.fullscreen_window.is_some() { |
| 1052 | return Ok(()); |
| 1053 | } |
| 1054 | |
| 1055 | let border_width = self.config.border_width; |
| 1056 | |
| 1057 | let gaps = if self.gaps_enabled { |
| 1058 | GapConfig { |
| 1059 | inner_horizontal: self.config.gap_inner_horizontal, |
| 1060 | inner_vertical: self.config.gap_inner_vertical, |
| 1061 | outer_horizontal: self.config.gap_outer_horizontal, |
| 1062 | outer_vertical: self.config.gap_outer_vertical, |
| 1063 | } |
| 1064 | } else { |
| 1065 | GapConfig { |
| 1066 | inner_horizontal: 0, |
| 1067 | inner_vertical: 0, |
| 1068 | outer_horizontal: 0, |
| 1069 | outer_vertical: 0, |
| 1070 | } |
| 1071 | }; |
| 1072 | |
| 1073 | for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 1074 | let visible: Vec<Window> = self |
| 1075 | .windows |
| 1076 | .iter() |
| 1077 | .filter(|&&w| { |
| 1078 | let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0); |
| 1079 | if window_mon != monitor_index { |
| 1080 | return false; |
| 1081 | } |
| 1082 | if self.floating_windows.contains(&w) { |
| 1083 | return false; |
| 1084 | } |
| 1085 | if let Some(&tags) = self.window_tags.get(&w) { |
| 1086 | (tags & monitor.selected_tags) != 0 |
| 1087 | } else { |
| 1088 | false |
| 1089 | } |
| 1090 | }) |
| 1091 | .copied() |
| 1092 | .collect(); |
| 1093 | |
| 1094 | let bar_height = self |
| 1095 | .bars |
| 1096 | .get(monitor_index) |
| 1097 | .map(|b| b.height() as u32) |
| 1098 | .unwrap_or(0); |
| 1099 | let usable_height = monitor.height.saturating_sub(bar_height); |
| 1100 | |
| 1101 | let geometries = self |
| 1102 | .layout |
| 1103 | .arrange(&visible, monitor.width, usable_height, &gaps); |
| 1104 | |
| 1105 | for (window, geometry) in visible.iter().zip(geometries.iter()) { |
| 1106 | let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
| 1107 | let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
| 1108 | |
| 1109 | let adjusted_x = geometry.x_coordinate + monitor.x; |
| 1110 | let adjusted_y = geometry.y_coordinate + monitor.y + bar_height as i32; |
| 1111 | |
| 1112 | self.connection.configure_window( |
| 1113 | *window, |
| 1114 | &ConfigureWindowAux::new() |
| 1115 | .x(adjusted_x) |
| 1116 | .y(adjusted_y) |
| 1117 | .width(adjusted_width) |
| 1118 | .height(adjusted_height), |
| 1119 | )?; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | self.connection.flush()?; |
| 1124 | Ok(()) |
| 1125 | } |
| 1126 | |
| 1127 | pub fn change_layout<L: Layout + 'static>(&mut self, new_layout: L) -> WmResult<()> { |
| 1128 | self.layout = Box::new(new_layout); |
| 1129 | self.apply_layout()?; |
| 1130 | Ok(()) |
| 1131 | } |
| 1132 | |
| 1133 | fn remove_window(&mut self, window: Window) -> WmResult<()> { |
| 1134 | let initial_count = self.windows.len(); |
| 1135 | self.windows.retain(|&w| w != window); |
| 1136 | self.window_tags.remove(&window); |
| 1137 | self.window_monitor.remove(&window); |
| 1138 | self.floating_windows.remove(&window); |
| 1139 | |
| 1140 | if self.fullscreen_window == Some(window) { |
| 1141 | self.fullscreen_window = None; |
| 1142 | for bar in &self.bars { |
| 1143 | self.connection.map_window(bar.window())?; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | if self.windows.len() < initial_count { |
| 1148 | let focused = self |
| 1149 | .monitors |
| 1150 | .get(self.selected_monitor) |
| 1151 | .and_then(|m| m.focused_window); |
| 1152 | if focused == Some(window) { |
| 1153 | let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 1154 | if let Some(&new_win) = visible.last() { |
| 1155 | self.set_focus(new_win)?; |
| 1156 | } else if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 1157 | monitor.focused_window = None; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | self.apply_layout()?; |
| 1162 | self.update_bar()?; |
| 1163 | } |
| 1164 | Ok(()) |
| 1165 | } |
| 1166 | } |