| 1 | use crate::Config; |
| 2 | use crate::bar::Bar; |
| 3 | use crate::client::{Client, TagMask}; |
| 4 | use crate::errors::WmError; |
| 5 | use crate::keyboard::{self, Arg, KeyAction, handlers}; |
| 6 | use crate::layout::GapConfig; |
| 7 | use crate::layout::tiling::TilingLayout; |
| 8 | use crate::layout::{Layout, LayoutBox, LayoutType, layout_from_str, next_layout}; |
| 9 | use crate::monitor::{Monitor, detect_monitors}; |
| 10 | use crate::overlay::{ErrorOverlay, KeybindOverlay, Overlay}; |
| 11 | use std::collections::{HashMap, HashSet}; |
| 12 | use x11rb::cursor::Handle as CursorHandle; |
| 13 | |
| 14 | use x11rb::connection::Connection; |
| 15 | use x11rb::protocol::Event; |
| 16 | use x11rb::protocol::xproto::*; |
| 17 | use x11rb::rust_connection::RustConnection; |
| 18 | |
| 19 | pub fn tag_mask(tag: usize) -> TagMask { |
| 20 | 1 << tag |
| 21 | } |
| 22 | |
| 23 | /// Get back a tag index from a [`TagMask`] |
| 24 | pub fn unmask_tag(mask: TagMask) -> usize { |
| 25 | // mask only has one bit set, so this works. |
| 26 | mask.trailing_zeros() as usize |
| 27 | } |
| 28 | |
| 29 | struct AtomCache { |
| 30 | net_current_desktop: Atom, |
| 31 | net_client_info: Atom, |
| 32 | wm_state: Atom, |
| 33 | wm_protocols: Atom, |
| 34 | wm_delete_window: Atom, |
| 35 | net_wm_state: Atom, |
| 36 | net_wm_state_fullscreen: Atom, |
| 37 | net_wm_window_type: Atom, |
| 38 | net_wm_window_type_dialog: Atom, |
| 39 | wm_name: Atom, |
| 40 | net_wm_name: Atom, |
| 41 | utf8_string: Atom, |
| 42 | net_active_window: Atom, |
| 43 | } |
| 44 | |
| 45 | impl AtomCache { |
| 46 | fn new(connection: &RustConnection) -> WmResult<Self> { |
| 47 | let net_current_desktop = connection |
| 48 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| 49 | .reply()? |
| 50 | .atom; |
| 51 | |
| 52 | let net_client_info = connection |
| 53 | .intern_atom(false, b"_NET_CLIENT_INFO")? |
| 54 | .reply()? |
| 55 | .atom; |
| 56 | |
| 57 | let wm_state = connection.intern_atom(false, b"WM_STATE")?.reply()?.atom; |
| 58 | |
| 59 | let wm_protocols = connection |
| 60 | .intern_atom(false, b"WM_PROTOCOLS")? |
| 61 | .reply()? |
| 62 | .atom; |
| 63 | |
| 64 | let wm_delete_window = connection |
| 65 | .intern_atom(false, b"WM_DELETE_WINDOW")? |
| 66 | .reply()? |
| 67 | .atom; |
| 68 | |
| 69 | let net_wm_state = connection |
| 70 | .intern_atom(false, b"_NET_WM_STATE")? |
| 71 | .reply()? |
| 72 | .atom; |
| 73 | |
| 74 | let net_wm_state_fullscreen = connection |
| 75 | .intern_atom(false, b"_NET_WM_STATE_FULLSCREEN")? |
| 76 | .reply()? |
| 77 | .atom; |
| 78 | |
| 79 | let net_wm_window_type = connection |
| 80 | .intern_atom(false, b"_NET_WM_WINDOW_TYPE")? |
| 81 | .reply()? |
| 82 | .atom; |
| 83 | |
| 84 | let net_wm_window_type_dialog = connection |
| 85 | .intern_atom(false, b"_NET_WM_WINDOW_TYPE_DIALOG")? |
| 86 | .reply()? |
| 87 | .atom; |
| 88 | |
| 89 | let wm_name = AtomEnum::WM_NAME.into(); |
| 90 | let net_wm_name = connection |
| 91 | .intern_atom(false, b"_NET_WM_NAME")? |
| 92 | .reply()? |
| 93 | .atom; |
| 94 | let utf8_string = connection.intern_atom(false, b"UTF8_STRING")?.reply()?.atom; |
| 95 | let net_active_window = connection |
| 96 | .intern_atom(false, b"_NET_ACTIVE_WINDOW")? |
| 97 | .reply()? |
| 98 | .atom; |
| 99 | |
| 100 | Ok(Self { |
| 101 | net_current_desktop, |
| 102 | net_client_info, |
| 103 | wm_state, |
| 104 | wm_protocols, |
| 105 | wm_delete_window, |
| 106 | net_wm_state, |
| 107 | net_wm_state_fullscreen, |
| 108 | net_wm_window_type, |
| 109 | net_wm_window_type_dialog, |
| 110 | wm_name, |
| 111 | net_wm_name, |
| 112 | utf8_string, |
| 113 | net_active_window, |
| 114 | }) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | pub struct WindowManager { |
| 119 | config: Config, |
| 120 | connection: RustConnection, |
| 121 | screen_number: usize, |
| 122 | root: Window, |
| 123 | screen: Screen, |
| 124 | windows: Vec<Window>, |
| 125 | clients: HashMap<Window, Client>, |
| 126 | layout: LayoutBox, |
| 127 | gaps_enabled: bool, |
| 128 | floating_windows: HashSet<Window>, |
| 129 | fullscreen_windows: HashSet<Window>, |
| 130 | floating_geometry_before_fullscreen: HashMap<Window, (i16, i16, u16, u16, u16)>, |
| 131 | bars: Vec<Bar>, |
| 132 | tab_bars: Vec<crate::tab_bar::TabBar>, |
| 133 | show_bar: bool, |
| 134 | last_layout: Option<&'static str>, |
| 135 | monitors: Vec<Monitor>, |
| 136 | selected_monitor: usize, |
| 137 | atoms: AtomCache, |
| 138 | previous_focused: Option<Window>, |
| 139 | display: *mut x11::xlib::Display, |
| 140 | font: crate::bar::font::Font, |
| 141 | keychord_state: keyboard::handlers::KeychordState, |
| 142 | current_key: usize, |
| 143 | keyboard_mapping: Option<keyboard::KeyboardMapping>, |
| 144 | error_message: Option<String>, |
| 145 | overlay: ErrorOverlay, |
| 146 | keybind_overlay: KeybindOverlay, |
| 147 | } |
| 148 | |
| 149 | type WmResult<T> = Result<T, WmError>; |
| 150 | |
| 151 | impl WindowManager { |
| 152 | pub fn new(config: Config) -> WmResult<Self> { |
| 153 | let (connection, screen_number) = x11rb::connect(None)?; |
| 154 | let root = connection.setup().roots[screen_number].root; |
| 155 | let screen = connection.setup().roots[screen_number].clone(); |
| 156 | |
| 157 | let normal_cursor = CursorHandle::new( |
| 158 | &connection, |
| 159 | screen_number, |
| 160 | &x11rb::resource_manager::new_from_default(&connection)?, |
| 161 | )? |
| 162 | .reply()? |
| 163 | .load_cursor(&connection, "left_ptr")?; |
| 164 | |
| 165 | connection |
| 166 | .change_window_attributes( |
| 167 | root, |
| 168 | &ChangeWindowAttributesAux::new() |
| 169 | .cursor(normal_cursor) |
| 170 | .event_mask( |
| 171 | EventMask::SUBSTRUCTURE_REDIRECT |
| 172 | | EventMask::SUBSTRUCTURE_NOTIFY |
| 173 | | EventMask::PROPERTY_CHANGE |
| 174 | | EventMask::KEY_PRESS |
| 175 | | EventMask::BUTTON_PRESS |
| 176 | | EventMask::POINTER_MOTION, |
| 177 | ), |
| 178 | )? |
| 179 | .check()?; |
| 180 | |
| 181 | let ignore_modifiers = [ |
| 182 | 0, |
| 183 | u16::from(ModMask::LOCK), |
| 184 | u16::from(ModMask::M2), |
| 185 | u16::from(ModMask::LOCK | ModMask::M2), |
| 186 | ]; |
| 187 | |
| 188 | for &ignore_mask in &ignore_modifiers { |
| 189 | let grab_mask = u16::from(config.modkey) | ignore_mask; |
| 190 | |
| 191 | connection.grab_button( |
| 192 | false, |
| 193 | root, |
| 194 | EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, |
| 195 | GrabMode::SYNC, |
| 196 | GrabMode::ASYNC, |
| 197 | x11rb::NONE, |
| 198 | x11rb::NONE, |
| 199 | ButtonIndex::M1, |
| 200 | grab_mask.into(), |
| 201 | )?; |
| 202 | |
| 203 | connection.grab_button( |
| 204 | false, |
| 205 | root, |
| 206 | EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, |
| 207 | GrabMode::SYNC, |
| 208 | GrabMode::ASYNC, |
| 209 | x11rb::NONE, |
| 210 | x11rb::NONE, |
| 211 | ButtonIndex::M3, |
| 212 | grab_mask.into(), |
| 213 | )?; |
| 214 | } |
| 215 | |
| 216 | let monitors = detect_monitors(&connection, &screen, root)?; |
| 217 | |
| 218 | let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) }; |
| 219 | if display.is_null() { |
| 220 | return Err(WmError::X11(crate::errors::X11Error::DisplayOpenFailed)); |
| 221 | } |
| 222 | |
| 223 | let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?; |
| 224 | |
| 225 | let mut bars = Vec::new(); |
| 226 | for monitor in monitors.iter() { |
| 227 | let bar = Bar::new( |
| 228 | &connection, |
| 229 | &screen, |
| 230 | screen_number, |
| 231 | &config, |
| 232 | display, |
| 233 | &font, |
| 234 | monitor.screen_x as i16, |
| 235 | monitor.screen_y as i16, |
| 236 | monitor.screen_width as u16, |
| 237 | )?; |
| 238 | bars.push(bar); |
| 239 | } |
| 240 | |
| 241 | let bar_height = font.height() as f32 * 1.4; |
| 242 | let mut tab_bars = Vec::new(); |
| 243 | for monitor in monitors.iter() { |
| 244 | let tab_bar = crate::tab_bar::TabBar::new( |
| 245 | &connection, |
| 246 | &screen, |
| 247 | screen_number, |
| 248 | display, |
| 249 | &font, |
| 250 | (monitor.screen_x + config.gap_outer_horizontal as i32) as i16, |
| 251 | (monitor.screen_y as f32 + bar_height + config.gap_outer_vertical as f32) as i16, |
| 252 | monitor |
| 253 | .screen_width |
| 254 | .saturating_sub(2 * config.gap_outer_horizontal as i32) as u16, |
| 255 | config.scheme_occupied, |
| 256 | config.scheme_selected, |
| 257 | )?; |
| 258 | tab_bars.push(tab_bar); |
| 259 | } |
| 260 | |
| 261 | let gaps_enabled = config.gaps_enabled; |
| 262 | |
| 263 | let atoms = AtomCache::new(&connection)?; |
| 264 | |
| 265 | let overlay = ErrorOverlay::new( |
| 266 | &connection, |
| 267 | &screen, |
| 268 | screen_number, |
| 269 | display, |
| 270 | &font, |
| 271 | screen.width_in_pixels, |
| 272 | )?; |
| 273 | |
| 274 | let keybind_overlay = |
| 275 | KeybindOverlay::new(&connection, &screen, screen_number, display, config.modkey)?; |
| 276 | |
| 277 | let mut window_manager = Self { |
| 278 | config, |
| 279 | connection, |
| 280 | screen_number, |
| 281 | root, |
| 282 | screen, |
| 283 | windows: Vec::new(), |
| 284 | clients: HashMap::new(), |
| 285 | layout: Box::new(TilingLayout), |
| 286 | gaps_enabled, |
| 287 | floating_windows: HashSet::new(), |
| 288 | fullscreen_windows: HashSet::new(), |
| 289 | floating_geometry_before_fullscreen: HashMap::new(), |
| 290 | bars, |
| 291 | tab_bars, |
| 292 | show_bar: true, |
| 293 | last_layout: None, |
| 294 | monitors, |
| 295 | selected_monitor: 0, |
| 296 | atoms, |
| 297 | previous_focused: None, |
| 298 | display, |
| 299 | font, |
| 300 | keychord_state: keyboard::handlers::KeychordState::Idle, |
| 301 | current_key: 0, |
| 302 | keyboard_mapping: None, |
| 303 | error_message: None, |
| 304 | overlay, |
| 305 | keybind_overlay, |
| 306 | }; |
| 307 | |
| 308 | for tab_bar in &window_manager.tab_bars { |
| 309 | tab_bar.hide(&window_manager.connection)?; |
| 310 | } |
| 311 | |
| 312 | window_manager.scan_existing_windows()?; |
| 313 | window_manager.update_bar()?; |
| 314 | window_manager.run_autostart_commands(); |
| 315 | |
| 316 | Ok(window_manager) |
| 317 | } |
| 318 | |
| 319 | pub fn show_migration_overlay(&mut self) { |
| 320 | let message = "We are on version 0.8.0 now.\n\n\ |
| 321 | Your config file has been deprecated once again.\n\ |
| 322 | Backup your current config, and run oxwm --init to generate a new one with correct values.\n\n\ |
| 323 | Please reach out to Tony, or check out the\n\ |
| 324 | documentation for help with migration.\n\n\ |
| 325 | We appreciate you testing oxwm!\n\n\ |
| 326 | Press Mod+Shift+/ to see keybinds\n\ |
| 327 | Press Mod+Shift+R to reload after fixing your config"; |
| 328 | |
| 329 | let monitor = &self.monitors[self.selected_monitor]; |
| 330 | let monitor_x = monitor.screen_x as i16; |
| 331 | let monitor_y = monitor.screen_y as i16; |
| 332 | let screen_width = monitor.screen_width as u16; |
| 333 | let screen_height = monitor.screen_height as u16; |
| 334 | |
| 335 | if let Err(e) = self.overlay.show_error( |
| 336 | &self.connection, |
| 337 | &self.font, |
| 338 | message, |
| 339 | monitor_x, |
| 340 | monitor_y, |
| 341 | screen_width, |
| 342 | screen_height, |
| 343 | ) { |
| 344 | eprintln!("Failed to show migration overlay: {:?}", e); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | fn try_reload_config(&mut self) -> Result<(), String> { |
| 349 | let lua_path = self |
| 350 | .config |
| 351 | .path |
| 352 | .take() |
| 353 | .expect("Could not find config file. Config path should've been set while loading"); |
| 354 | |
| 355 | if !lua_path.exists() { |
| 356 | return Err("Could not find config file, has it been moved?".to_string()); |
| 357 | } |
| 358 | |
| 359 | let config_str = std::fs::read_to_string(&lua_path) |
| 360 | .map_err(|e| format!("Failed to read config: {}", e))?; |
| 361 | |
| 362 | let config_dir = lua_path.parent(); |
| 363 | |
| 364 | let new_config = crate::config::parse_lua_config(&config_str, config_dir) |
| 365 | .map_err(|e| format!("{}", e))?; |
| 366 | |
| 367 | self.config = new_config; |
| 368 | self.config.path = Some(lua_path); |
| 369 | self.error_message = None; |
| 370 | |
| 371 | for bar in &mut self.bars { |
| 372 | bar.update_from_config(&self.config); |
| 373 | } |
| 374 | |
| 375 | Ok(()) |
| 376 | } |
| 377 | |
| 378 | fn scan_existing_windows(&mut self) -> WmResult<()> { |
| 379 | let tree = self.connection.query_tree(self.root)?.reply()?; |
| 380 | let net_client_info = self.atoms.net_client_info; |
| 381 | let wm_state_atom = self.atoms.wm_state; |
| 382 | |
| 383 | for &window in &tree.children { |
| 384 | if self.bars.iter().any(|bar| bar.window() == window) { |
| 385 | continue; |
| 386 | } |
| 387 | |
| 388 | let Ok(attrs) = self.connection.get_window_attributes(window)?.reply() else { |
| 389 | continue; |
| 390 | }; |
| 391 | |
| 392 | if attrs.override_redirect { |
| 393 | continue; |
| 394 | } |
| 395 | |
| 396 | if attrs.map_state == MapState::VIEWABLE { |
| 397 | let _tag = self.get_saved_tag(window, net_client_info)?; |
| 398 | self.windows.push(window); |
| 399 | continue; |
| 400 | } |
| 401 | |
| 402 | if attrs.map_state == MapState::UNMAPPED { |
| 403 | let has_wm_state = self |
| 404 | .connection |
| 405 | .get_property(false, window, wm_state_atom, AtomEnum::ANY, 0, 2)? |
| 406 | .reply() |
| 407 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 408 | |
| 409 | if !has_wm_state { |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | let has_wm_class = self |
| 414 | .connection |
| 415 | .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)? |
| 416 | .reply() |
| 417 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 418 | |
| 419 | if has_wm_class { |
| 420 | let _tag = self.get_saved_tag(window, net_client_info)?; |
| 421 | self.connection.map_window(window)?; |
| 422 | self.windows.push(window); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | if let Some(&first) = self.windows.first() { |
| 428 | self.focus(Some(first))?; |
| 429 | } |
| 430 | |
| 431 | self.apply_layout()?; |
| 432 | Ok(()) |
| 433 | } |
| 434 | |
| 435 | fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> WmResult<TagMask> { |
| 436 | match self |
| 437 | .connection |
| 438 | .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)? |
| 439 | .reply() |
| 440 | { |
| 441 | Ok(prop) if prop.value.len() >= 4 => { |
| 442 | let tags = u32::from_ne_bytes([ |
| 443 | prop.value[0], |
| 444 | prop.value[1], |
| 445 | prop.value[2], |
| 446 | prop.value[3], |
| 447 | ]); |
| 448 | |
| 449 | if tags != 0 && tags < (1 << self.config.tags.len()) { |
| 450 | return Ok(tags); |
| 451 | } |
| 452 | } |
| 453 | Ok(_) => {} |
| 454 | Err(e) => { |
| 455 | eprintln!("No _NET_CLIENT_INFO property ({})", e); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | Ok(self |
| 460 | .monitors |
| 461 | .get(self.selected_monitor) |
| 462 | .map(|m| m.tagset[m.selected_tags_index]) |
| 463 | .unwrap_or(tag_mask(0))) |
| 464 | } |
| 465 | |
| 466 | fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { |
| 467 | let net_client_info = self.atoms.net_client_info; |
| 468 | |
| 469 | let bytes = tag.to_ne_bytes().to_vec(); |
| 470 | |
| 471 | self.connection.change_property( |
| 472 | PropMode::REPLACE, |
| 473 | window, |
| 474 | net_client_info, |
| 475 | AtomEnum::CARDINAL, |
| 476 | 32, |
| 477 | 1, |
| 478 | &bytes, |
| 479 | )?; |
| 480 | |
| 481 | self.connection.flush()?; |
| 482 | Ok(()) |
| 483 | } |
| 484 | |
| 485 | fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> { |
| 486 | let wm_state_atom = self.atoms.wm_state; |
| 487 | |
| 488 | let data = [state, 0u32]; |
| 489 | let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); |
| 490 | |
| 491 | self.connection.change_property( |
| 492 | PropMode::REPLACE, |
| 493 | window, |
| 494 | wm_state_atom, |
| 495 | wm_state_atom, |
| 496 | 32, |
| 497 | 2, |
| 498 | &bytes, |
| 499 | )?; |
| 500 | |
| 501 | self.connection.flush()?; |
| 502 | Ok(()) |
| 503 | } |
| 504 | |
| 505 | pub fn run(&mut self) -> WmResult<bool> { |
| 506 | println!("oxwm started on display {}", self.screen_number); |
| 507 | |
| 508 | self.grab_keys()?; |
| 509 | self.update_bar()?; |
| 510 | |
| 511 | let mut last_bar_update = std::time::Instant::now(); |
| 512 | const BAR_UPDATE_INTERVAL_MS: u64 = 100; |
| 513 | |
| 514 | loop { |
| 515 | match self.connection.poll_for_event_with_sequence()? { |
| 516 | Some((event, _sequence)) => { |
| 517 | if let Some(should_restart) = self.handle_event(event)? { |
| 518 | return Ok(should_restart); |
| 519 | } |
| 520 | } |
| 521 | None => { |
| 522 | if last_bar_update.elapsed().as_millis() >= BAR_UPDATE_INTERVAL_MS as u128 { |
| 523 | if let Some(bar) = self.bars.get_mut(self.selected_monitor) { |
| 524 | bar.update_blocks(); |
| 525 | } |
| 526 | if self.bars.iter().any(|bar| bar.needs_redraw()) { |
| 527 | self.update_bar()?; |
| 528 | } |
| 529 | last_bar_update = std::time::Instant::now(); |
| 530 | } |
| 531 | |
| 532 | self.connection.flush()?; |
| 533 | std::thread::sleep(std::time::Duration::from_millis(16)); |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | fn toggle_floating(&mut self) -> WmResult<()> { |
| 540 | let focused = self |
| 541 | .monitors |
| 542 | .get(self.selected_monitor) |
| 543 | .and_then(|m| m.selected_client); |
| 544 | |
| 545 | if focused.is_none() { |
| 546 | return Ok(()); |
| 547 | } |
| 548 | let focused = focused.unwrap(); |
| 549 | |
| 550 | if let Some(client) = self.clients.get(&focused) { |
| 551 | if client.is_fullscreen { |
| 552 | return Ok(()); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | let (is_fixed, x, y, w, h) = if let Some(client) = self.clients.get(&focused) { |
| 557 | ( |
| 558 | client.is_fixed, |
| 559 | client.x_position as i32, |
| 560 | client.y_position as i32, |
| 561 | client.width as u32, |
| 562 | client.height as u32, |
| 563 | ) |
| 564 | } else { |
| 565 | return Ok(()); |
| 566 | }; |
| 567 | |
| 568 | let was_floating = self.floating_windows.contains(&focused); |
| 569 | |
| 570 | if was_floating { |
| 571 | self.floating_windows.remove(&focused); |
| 572 | if let Some(client) = self.clients.get_mut(&focused) { |
| 573 | client.is_floating = false; |
| 574 | } |
| 575 | } else { |
| 576 | self.floating_windows.insert(focused); |
| 577 | if let Some(client) = self.clients.get_mut(&focused) { |
| 578 | client.is_floating = is_fixed || !client.is_floating; |
| 579 | } |
| 580 | |
| 581 | self.connection.configure_window( |
| 582 | focused, |
| 583 | &ConfigureWindowAux::new() |
| 584 | .x(x) |
| 585 | .y(y) |
| 586 | .width(w) |
| 587 | .height(h) |
| 588 | .stack_mode(StackMode::ABOVE), |
| 589 | )?; |
| 590 | } |
| 591 | |
| 592 | self.apply_layout()?; |
| 593 | Ok(()) |
| 594 | } |
| 595 | |
| 596 | fn set_master_factor(&mut self, delta: f32) -> WmResult<()> { |
| 597 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 598 | let new_mfact = (monitor.master_factor + delta).max(0.05).min(0.95); |
| 599 | monitor.master_factor = new_mfact; |
| 600 | self.apply_layout()?; |
| 601 | } |
| 602 | Ok(()) |
| 603 | } |
| 604 | |
| 605 | fn inc_num_master(&mut self, delta: i32) -> WmResult<()> { |
| 606 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 607 | let new_nmaster = (monitor.num_master + delta).max(0); |
| 608 | monitor.num_master = new_nmaster; |
| 609 | self.apply_layout()?; |
| 610 | } |
| 611 | Ok(()) |
| 612 | } |
| 613 | |
| 614 | fn get_layout_symbol(&self) -> String { |
| 615 | let layout_name = self.layout.name(); |
| 616 | self.config |
| 617 | .layout_symbols |
| 618 | .iter() |
| 619 | .find(|l| l.name == layout_name) |
| 620 | .map(|l| l.symbol.clone()) |
| 621 | .unwrap_or_else(|| self.layout.symbol().to_string()) |
| 622 | } |
| 623 | |
| 624 | fn get_keychord_indicator(&self) -> Option<String> { |
| 625 | match &self.keychord_state { |
| 626 | keyboard::handlers::KeychordState::Idle => None, |
| 627 | keyboard::handlers::KeychordState::InProgress { |
| 628 | candidates, |
| 629 | keys_pressed, |
| 630 | } => { |
| 631 | if candidates.is_empty() { |
| 632 | return None; |
| 633 | } |
| 634 | |
| 635 | let binding = &self.config.keybindings[candidates[0]]; |
| 636 | let mut indicator = String::new(); |
| 637 | |
| 638 | for (i, key_press) in binding.keys.iter().take(*keys_pressed).enumerate() { |
| 639 | if i > 0 { |
| 640 | indicator.push(' '); |
| 641 | } |
| 642 | |
| 643 | for modifier in &key_press.modifiers { |
| 644 | indicator.push_str(Self::format_modifier(*modifier)); |
| 645 | indicator.push('+'); |
| 646 | } |
| 647 | |
| 648 | indicator.push_str(&keyboard::keysyms::format_keysym(key_press.keysym)); |
| 649 | } |
| 650 | |
| 651 | indicator.push('-'); |
| 652 | Some(indicator) |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | fn format_modifier(modifier: KeyButMask) -> &'static str { |
| 658 | match modifier { |
| 659 | KeyButMask::MOD1 => "Alt", |
| 660 | KeyButMask::MOD4 => "Super", |
| 661 | KeyButMask::SHIFT => "Shift", |
| 662 | KeyButMask::CONTROL => "Ctrl", |
| 663 | _ => "Mod", |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | fn update_bar(&mut self) -> WmResult<()> { |
| 668 | let layout_symbol = self.get_layout_symbol(); |
| 669 | let keychord_indicator = self.get_keychord_indicator(); |
| 670 | |
| 671 | for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 672 | if let Some(bar) = self.bars.get_mut(monitor_index) { |
| 673 | let mut occupied_tags: TagMask = 0; |
| 674 | for client in self.clients.values() { |
| 675 | if client.monitor_index == monitor_index { |
| 676 | occupied_tags |= client.tags; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | let draw_blocks = monitor_index == self.selected_monitor; |
| 681 | bar.invalidate(); |
| 682 | bar.draw( |
| 683 | &self.connection, |
| 684 | &self.font, |
| 685 | self.display, |
| 686 | monitor.tagset[monitor.selected_tags_index], |
| 687 | occupied_tags, |
| 688 | draw_blocks, |
| 689 | &layout_symbol, |
| 690 | keychord_indicator.as_deref(), |
| 691 | )?; |
| 692 | } |
| 693 | } |
| 694 | Ok(()) |
| 695 | } |
| 696 | |
| 697 | fn update_tab_bars(&mut self) -> WmResult<()> { |
| 698 | for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 699 | if let Some(tab_bar) = self.tab_bars.get_mut(monitor_index) { |
| 700 | let visible_windows: Vec<(Window, String)> = self |
| 701 | .windows |
| 702 | .iter() |
| 703 | .filter_map(|&window| { |
| 704 | if let Some(client) = self.clients.get(&window) { |
| 705 | if client.monitor_index != monitor_index |
| 706 | || self.floating_windows.contains(&window) |
| 707 | || self.fullscreen_windows.contains(&window) |
| 708 | { |
| 709 | return None; |
| 710 | } |
| 711 | if (client.tags & monitor.tagset[monitor.selected_tags_index]) != 0 { |
| 712 | return Some((window, client.name.clone())); |
| 713 | } |
| 714 | } |
| 715 | None |
| 716 | }) |
| 717 | .collect(); |
| 718 | |
| 719 | let focused_window = monitor.selected_client; |
| 720 | |
| 721 | tab_bar.draw( |
| 722 | &self.connection, |
| 723 | &self.font, |
| 724 | &visible_windows, |
| 725 | focused_window, |
| 726 | )?; |
| 727 | } |
| 728 | } |
| 729 | Ok(()) |
| 730 | } |
| 731 | |
| 732 | fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> WmResult<()> { |
| 733 | match action { |
| 734 | KeyAction::Spawn => handlers::handle_spawn_action(action, arg, self.selected_monitor)?, |
| 735 | KeyAction::SpawnTerminal => { |
| 736 | crate::signal::spawn_detached(&self.config.terminal); |
| 737 | } |
| 738 | KeyAction::KillClient => { |
| 739 | if let Some(focused) = self |
| 740 | .monitors |
| 741 | .get(self.selected_monitor) |
| 742 | .and_then(|m| m.selected_client) |
| 743 | { |
| 744 | self.kill_client(focused)?; |
| 745 | } |
| 746 | } |
| 747 | KeyAction::ToggleFullScreen => { |
| 748 | // Disable gaps and save previous state in config if not already in fullscreen |
| 749 | if self.fullscreen_windows.is_empty() { |
| 750 | self.config.gaps_enabled = std::mem::take(&mut self.gaps_enabled); |
| 751 | } else { |
| 752 | self.gaps_enabled = self.config.gaps_enabled; |
| 753 | } |
| 754 | |
| 755 | self.fullscreen()?; |
| 756 | self.restack()?; |
| 757 | } |
| 758 | KeyAction::ChangeLayout => { |
| 759 | if let Arg::Str(layout_name) = arg { |
| 760 | match layout_from_str(layout_name) { |
| 761 | Ok(layout) => { |
| 762 | self.layout = layout; |
| 763 | if layout_name != "normie" && layout_name != "floating" { |
| 764 | self.floating_windows.clear(); |
| 765 | } |
| 766 | self.apply_layout()?; |
| 767 | self.update_bar()?; |
| 768 | self.restack()?; |
| 769 | } |
| 770 | Err(e) => eprintln!("Failed to change layout: {}", e), |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | KeyAction::CycleLayout => { |
| 775 | let current_name = self.layout.name(); |
| 776 | let next_name = next_layout(current_name); |
| 777 | match layout_from_str(next_name) { |
| 778 | Ok(layout) => { |
| 779 | self.layout = layout; |
| 780 | if next_name != "normie" && next_name != "floating" { |
| 781 | self.floating_windows.clear(); |
| 782 | } |
| 783 | self.apply_layout()?; |
| 784 | self.update_bar()?; |
| 785 | self.restack()?; |
| 786 | } |
| 787 | Err(e) => eprintln!("Failed to cycle layout: {}", e), |
| 788 | } |
| 789 | } |
| 790 | KeyAction::ToggleFloating => { |
| 791 | self.toggle_floating()?; |
| 792 | self.restack()?; |
| 793 | } |
| 794 | |
| 795 | KeyAction::FocusStack => { |
| 796 | if let Arg::Int(direction) = arg { |
| 797 | self.focusstack(*direction)?; |
| 798 | } |
| 799 | } |
| 800 | KeyAction::MoveStack => { |
| 801 | if let Arg::Int(direction) = arg { |
| 802 | self.move_stack(*direction)?; |
| 803 | } |
| 804 | } |
| 805 | KeyAction::Quit | KeyAction::Restart => { |
| 806 | // Handled in handle_event |
| 807 | } |
| 808 | KeyAction::ViewTag => { |
| 809 | if let Arg::Int(tag_index) = arg { |
| 810 | self.view_tag(*tag_index as usize)?; |
| 811 | } |
| 812 | } |
| 813 | KeyAction::ViewNextTag => { |
| 814 | let monitor = self.get_selected_monitor(); |
| 815 | let current_tag_index = unmask_tag(monitor.get_selected_tag()) as i32; |
| 816 | let len = self.config.tags.len() as i32; |
| 817 | self.view_tag((current_tag_index + 1).rem_euclid(len) as usize)?; |
| 818 | } |
| 819 | KeyAction::ViewPreviousTag => { |
| 820 | let monitor = self.get_selected_monitor(); |
| 821 | let current_tag_index = unmask_tag(monitor.get_selected_tag()) as i32; |
| 822 | let len = self.config.tags.len() as i32; |
| 823 | self.view_tag((current_tag_index - 1).rem_euclid(len) as usize)?; |
| 824 | } |
| 825 | KeyAction::ViewNextNonEmptyTag => { |
| 826 | let monitor = self.get_selected_monitor(); |
| 827 | let current = unmask_tag(monitor.get_selected_tag()) as i32; |
| 828 | let len = self.config.tags.len() as i32; |
| 829 | let mon_num = monitor.monitor_number; |
| 830 | |
| 831 | for offset in 1..len { |
| 832 | let next = (current + offset).rem_euclid(len) as usize; |
| 833 | if self.has_windows_on_tag(mon_num, next) { |
| 834 | self.view_tag(next)?; |
| 835 | break; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | KeyAction::ViewPreviousNonEmptyTag => { |
| 840 | let monitor = self.get_selected_monitor(); |
| 841 | let current = unmask_tag(monitor.get_selected_tag()) as i32; |
| 842 | let len = self.config.tags.len() as i32; |
| 843 | let mon_num = monitor.monitor_number; |
| 844 | |
| 845 | for offset in 1..len { |
| 846 | let prev = (current - offset).rem_euclid(len) as usize; |
| 847 | if self.has_windows_on_tag(mon_num, prev) { |
| 848 | self.view_tag(prev)?; |
| 849 | break; |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | KeyAction::ToggleView => { |
| 854 | if let Arg::Int(tag_index) = arg { |
| 855 | self.toggleview(*tag_index as usize)?; |
| 856 | } |
| 857 | } |
| 858 | KeyAction::MoveToTag => { |
| 859 | if let Arg::Int(tag_index) = arg { |
| 860 | self.move_to_tag(*tag_index as usize)?; |
| 861 | } |
| 862 | } |
| 863 | KeyAction::ToggleTag => { |
| 864 | if let Arg::Int(tag_index) = arg { |
| 865 | self.toggletag(*tag_index as usize)?; |
| 866 | } |
| 867 | } |
| 868 | KeyAction::ToggleGaps => { |
| 869 | self.gaps_enabled = !self.gaps_enabled; |
| 870 | self.apply_layout()?; |
| 871 | self.restack()?; |
| 872 | } |
| 873 | KeyAction::FocusMonitor => { |
| 874 | if let Arg::Int(direction) = arg { |
| 875 | self.focus_monitor(*direction)?; |
| 876 | } |
| 877 | } |
| 878 | KeyAction::TagMonitor => { |
| 879 | if let Arg::Int(direction) = arg { |
| 880 | self.send_window_to_adjacent_monitor(*direction)?; |
| 881 | } |
| 882 | } |
| 883 | KeyAction::ShowKeybindOverlay => { |
| 884 | let monitor = &self.monitors[self.selected_monitor]; |
| 885 | self.keybind_overlay.toggle( |
| 886 | &self.connection, |
| 887 | &self.font, |
| 888 | &self.config.keybindings, |
| 889 | monitor.screen_x as i16, |
| 890 | monitor.screen_y as i16, |
| 891 | monitor.screen_width as u16, |
| 892 | monitor.screen_height as u16, |
| 893 | )?; |
| 894 | } |
| 895 | KeyAction::SetMasterFactor => { |
| 896 | if let Arg::Int(delta) = arg { |
| 897 | self.set_master_factor(*delta as f32 / 100.0)?; |
| 898 | } |
| 899 | } |
| 900 | KeyAction::IncNumMaster => { |
| 901 | if let Arg::Int(delta) = arg { |
| 902 | self.inc_num_master(*delta)?; |
| 903 | } |
| 904 | } |
| 905 | KeyAction::None => {} |
| 906 | } |
| 907 | Ok(()) |
| 908 | } |
| 909 | |
| 910 | fn is_window_visible(&self, window: Window) -> bool { |
| 911 | if let Some(client) = self.clients.get(&window) { |
| 912 | let monitor = self.monitors.get(client.monitor_index); |
| 913 | let selected_tags = monitor |
| 914 | .map(|m| m.tagset[m.selected_tags_index]) |
| 915 | .unwrap_or(0); |
| 916 | (client.tags & selected_tags) != 0 |
| 917 | } else { |
| 918 | false |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | fn visible_windows(&self) -> Vec<Window> { |
| 923 | let mut result = Vec::new(); |
| 924 | for monitor in &self.monitors { |
| 925 | let mut current = monitor.clients_head; |
| 926 | while let Some(window) = current { |
| 927 | if let Some(client) = self.clients.get(&window) { |
| 928 | let visible_tags = client.tags & monitor.tagset[monitor.selected_tags_index]; |
| 929 | if visible_tags != 0 { |
| 930 | result.push(window); |
| 931 | } |
| 932 | current = client.next; |
| 933 | } else { |
| 934 | break; |
| 935 | } |
| 936 | } |
| 937 | } |
| 938 | result |
| 939 | } |
| 940 | |
| 941 | fn visible_windows_on_monitor(&self, monitor_index: usize) -> Vec<Window> { |
| 942 | let mut result = Vec::new(); |
| 943 | if let Some(monitor) = self.monitors.get(monitor_index) { |
| 944 | let mut current = monitor.clients_head; |
| 945 | while let Some(window) = current { |
| 946 | if let Some(client) = self.clients.get(&window) { |
| 947 | let visible_tags = client.tags & monitor.tagset[monitor.selected_tags_index]; |
| 948 | if visible_tags != 0 { |
| 949 | result.push(window); |
| 950 | } |
| 951 | current = client.next; |
| 952 | } else { |
| 953 | break; |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | result |
| 958 | } |
| 959 | |
| 960 | fn get_monitor_at_point(&self, x: i32, y: i32) -> Option<usize> { |
| 961 | self.monitors |
| 962 | .iter() |
| 963 | .position(|mon| mon.contains_point(x, y)) |
| 964 | } |
| 965 | |
| 966 | fn get_monitor_for_rect(&self, x: i32, y: i32, w: i32, h: i32) -> usize { |
| 967 | let mut best_monitor = self.selected_monitor; |
| 968 | let mut max_area = 0; |
| 969 | |
| 970 | for (idx, monitor) in self.monitors.iter().enumerate() { |
| 971 | let intersect_width = 0.max( |
| 972 | (x + w).min(monitor.window_area_x + monitor.window_area_width) |
| 973 | - x.max(monitor.window_area_x), |
| 974 | ); |
| 975 | let intersect_height = 0.max( |
| 976 | (y + h).min(monitor.window_area_y + monitor.window_area_height) |
| 977 | - y.max(monitor.window_area_y), |
| 978 | ); |
| 979 | let area = intersect_width * intersect_height; |
| 980 | |
| 981 | if area > max_area { |
| 982 | max_area = area; |
| 983 | best_monitor = idx; |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | best_monitor |
| 988 | } |
| 989 | |
| 990 | fn move_window_to_monitor( |
| 991 | &mut self, |
| 992 | window: Window, |
| 993 | target_monitor_index: usize, |
| 994 | ) -> WmResult<()> { |
| 995 | let current_monitor_index = self.clients.get(&window).map(|c| c.monitor_index); |
| 996 | |
| 997 | if let Some(current_idx) = current_monitor_index { |
| 998 | if current_idx == target_monitor_index { |
| 999 | return Ok(()); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | self.unfocus(window)?; |
| 1004 | self.detach(window); |
| 1005 | self.detach_stack(window); |
| 1006 | |
| 1007 | if let Some(client) = self.clients.get_mut(&window) { |
| 1008 | client.monitor_index = target_monitor_index; |
| 1009 | if let Some(target_monitor) = self.monitors.get(target_monitor_index) { |
| 1010 | client.tags = target_monitor.tagset[target_monitor.selected_tags_index]; |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | self.attach_aside(window, target_monitor_index); |
| 1015 | self.attach_stack(window, target_monitor_index); |
| 1016 | |
| 1017 | self.focus(None)?; |
| 1018 | self.apply_layout()?; |
| 1019 | |
| 1020 | Ok(()) |
| 1021 | } |
| 1022 | |
| 1023 | fn get_adjacent_monitor(&self, direction: i32) -> Option<usize> { |
| 1024 | if self.monitors.len() <= 1 { |
| 1025 | return None; |
| 1026 | } |
| 1027 | |
| 1028 | if direction > 0 { |
| 1029 | if self.selected_monitor + 1 < self.monitors.len() { |
| 1030 | Some(self.selected_monitor + 1) |
| 1031 | } else { |
| 1032 | Some(0) |
| 1033 | } |
| 1034 | } else { |
| 1035 | if self.selected_monitor == 0 { |
| 1036 | Some(self.monitors.len() - 1) |
| 1037 | } else { |
| 1038 | Some(self.selected_monitor - 1) |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | fn is_visible(&self, window: Window) -> bool { |
| 1044 | let Some(client) = self.clients.get(&window) else { |
| 1045 | return false; |
| 1046 | }; |
| 1047 | |
| 1048 | let Some(monitor) = self.monitors.get(client.monitor_index) else { |
| 1049 | return false; |
| 1050 | }; |
| 1051 | |
| 1052 | (client.tags & monitor.tagset[monitor.selected_tags_index]) != 0 |
| 1053 | } |
| 1054 | |
| 1055 | fn showhide(&mut self, window: Option<Window>) -> WmResult<()> { |
| 1056 | let Some(window) = window else { |
| 1057 | return Ok(()); |
| 1058 | }; |
| 1059 | |
| 1060 | let Some(client) = self.clients.get(&window).cloned() else { |
| 1061 | return Ok(()); |
| 1062 | }; |
| 1063 | |
| 1064 | let monitor = match self.monitors.get(client.monitor_index) { |
| 1065 | Some(m) => m, |
| 1066 | None => return Ok(()), |
| 1067 | }; |
| 1068 | |
| 1069 | let is_visible = (client.tags & monitor.tagset[monitor.selected_tags_index]) != 0; |
| 1070 | |
| 1071 | if is_visible { |
| 1072 | self.connection.configure_window( |
| 1073 | window, |
| 1074 | &ConfigureWindowAux::new() |
| 1075 | .x(client.x_position as i32) |
| 1076 | .y(client.y_position as i32), |
| 1077 | )?; |
| 1078 | |
| 1079 | let is_floating = client.is_floating; |
| 1080 | let is_fullscreen = client.is_fullscreen; |
| 1081 | let has_no_layout = self.layout.name() == LayoutType::Normie.as_str(); |
| 1082 | |
| 1083 | if (has_no_layout || is_floating) && !is_fullscreen { |
| 1084 | let (x, y, w, h, changed) = self.apply_size_hints( |
| 1085 | window, |
| 1086 | client.x_position as i32, |
| 1087 | client.y_position as i32, |
| 1088 | client.width as i32, |
| 1089 | client.height as i32, |
| 1090 | ); |
| 1091 | if changed { |
| 1092 | if let Some(c) = self.clients.get_mut(&window) { |
| 1093 | c.old_x_position = c.x_position; |
| 1094 | c.old_y_position = c.y_position; |
| 1095 | c.old_width = c.width; |
| 1096 | c.old_height = c.height; |
| 1097 | c.x_position = x as i16; |
| 1098 | c.y_position = y as i16; |
| 1099 | c.width = w as u16; |
| 1100 | c.height = h as u16; |
| 1101 | } |
| 1102 | self.connection.configure_window( |
| 1103 | window, |
| 1104 | &ConfigureWindowAux::new() |
| 1105 | .x(x) |
| 1106 | .y(y) |
| 1107 | .width(w as u32) |
| 1108 | .height(h as u32) |
| 1109 | .border_width(self.config.border_width), |
| 1110 | )?; |
| 1111 | self.send_configure_notify(window)?; |
| 1112 | self.connection.flush()?; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | self.showhide(client.stack_next)?; |
| 1117 | } else { |
| 1118 | self.showhide(client.stack_next)?; |
| 1119 | |
| 1120 | let width = client.width_with_border() as i32; |
| 1121 | self.connection.configure_window( |
| 1122 | window, |
| 1123 | &ConfigureWindowAux::new() |
| 1124 | .x(width * -2) |
| 1125 | .y(client.y_position as i32), |
| 1126 | )?; |
| 1127 | } |
| 1128 | |
| 1129 | Ok(()) |
| 1130 | } |
| 1131 | |
| 1132 | pub fn view_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 1133 | if tag_index >= self.config.tags.len() { |
| 1134 | return Ok(()); |
| 1135 | } |
| 1136 | |
| 1137 | let monitor = match self.monitors.get_mut(self.selected_monitor) { |
| 1138 | Some(m) => m, |
| 1139 | None => return Ok(()), |
| 1140 | }; |
| 1141 | |
| 1142 | let new_tagset = tag_mask(tag_index); |
| 1143 | |
| 1144 | if new_tagset == monitor.tagset[monitor.selected_tags_index] { |
| 1145 | return Ok(()); |
| 1146 | } |
| 1147 | |
| 1148 | monitor.selected_tags_index ^= 1; |
| 1149 | monitor.tagset[monitor.selected_tags_index] = new_tagset; |
| 1150 | |
| 1151 | self.save_selected_tags()?; |
| 1152 | self.focus(None)?; |
| 1153 | self.apply_layout()?; |
| 1154 | self.update_bar()?; |
| 1155 | |
| 1156 | Ok(()) |
| 1157 | } |
| 1158 | |
| 1159 | pub fn toggleview(&mut self, tag_index: usize) -> WmResult<()> { |
| 1160 | if tag_index >= self.config.tags.len() { |
| 1161 | return Ok(()); |
| 1162 | } |
| 1163 | |
| 1164 | let monitor = match self.monitors.get_mut(self.selected_monitor) { |
| 1165 | Some(m) => m, |
| 1166 | None => return Ok(()), |
| 1167 | }; |
| 1168 | |
| 1169 | let mask = tag_mask(tag_index); |
| 1170 | let new_tagset = monitor.tagset[monitor.selected_tags_index] ^ mask; |
| 1171 | |
| 1172 | if new_tagset == 0 { |
| 1173 | return Ok(()); |
| 1174 | } |
| 1175 | |
| 1176 | monitor.tagset[monitor.selected_tags_index] = new_tagset; |
| 1177 | |
| 1178 | self.save_selected_tags()?; |
| 1179 | self.focus(None)?; |
| 1180 | self.apply_layout()?; |
| 1181 | self.update_bar()?; |
| 1182 | |
| 1183 | Ok(()) |
| 1184 | } |
| 1185 | |
| 1186 | fn save_selected_tags(&self) -> WmResult<()> { |
| 1187 | let net_current_desktop = self.atoms.net_current_desktop; |
| 1188 | |
| 1189 | let selected_tags = self |
| 1190 | .monitors |
| 1191 | .get(self.selected_monitor) |
| 1192 | .map(|m| m.tagset[m.selected_tags_index]) |
| 1193 | .unwrap_or(tag_mask(0)); |
| 1194 | let desktop = selected_tags.trailing_zeros(); |
| 1195 | |
| 1196 | let bytes = (desktop as u32).to_ne_bytes(); |
| 1197 | self.connection.change_property( |
| 1198 | PropMode::REPLACE, |
| 1199 | self.root, |
| 1200 | net_current_desktop, |
| 1201 | AtomEnum::CARDINAL, |
| 1202 | 32, |
| 1203 | 1, |
| 1204 | &bytes, |
| 1205 | )?; |
| 1206 | |
| 1207 | self.connection.flush()?; |
| 1208 | Ok(()) |
| 1209 | } |
| 1210 | |
| 1211 | pub fn move_to_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 1212 | if tag_index >= self.config.tags.len() { |
| 1213 | return Ok(()); |
| 1214 | } |
| 1215 | |
| 1216 | let focused = match self |
| 1217 | .monitors |
| 1218 | .get(self.selected_monitor) |
| 1219 | .and_then(|m| m.selected_client) |
| 1220 | { |
| 1221 | Some(win) => win, |
| 1222 | None => return Ok(()), |
| 1223 | }; |
| 1224 | |
| 1225 | let mask = tag_mask(tag_index); |
| 1226 | |
| 1227 | if let Some(client) = self.clients.get_mut(&focused) { |
| 1228 | client.tags = mask; |
| 1229 | } |
| 1230 | |
| 1231 | if let Err(error) = self.save_client_tag(focused, mask) { |
| 1232 | eprintln!("Failed to save client tag: {:?}", error); |
| 1233 | } |
| 1234 | |
| 1235 | self.focus(None)?; |
| 1236 | self.apply_layout()?; |
| 1237 | self.update_bar()?; |
| 1238 | |
| 1239 | Ok(()) |
| 1240 | } |
| 1241 | |
| 1242 | pub fn toggletag(&mut self, tag_index: usize) -> WmResult<()> { |
| 1243 | if tag_index >= self.config.tags.len() { |
| 1244 | return Ok(()); |
| 1245 | } |
| 1246 | |
| 1247 | let focused = match self |
| 1248 | .monitors |
| 1249 | .get(self.selected_monitor) |
| 1250 | .and_then(|m| m.selected_client) |
| 1251 | { |
| 1252 | Some(win) => win, |
| 1253 | None => return Ok(()), |
| 1254 | }; |
| 1255 | |
| 1256 | let mask = tag_mask(tag_index); |
| 1257 | let current_tags = self.clients.get(&focused).map(|c| c.tags).unwrap_or(0); |
| 1258 | let new_tags = current_tags ^ mask; |
| 1259 | |
| 1260 | if new_tags == 0 { |
| 1261 | return Ok(()); |
| 1262 | } |
| 1263 | |
| 1264 | if let Some(client) = self.clients.get_mut(&focused) { |
| 1265 | client.tags = new_tags; |
| 1266 | } |
| 1267 | |
| 1268 | if let Err(error) = self.save_client_tag(focused, new_tags) { |
| 1269 | eprintln!("Failed to save client tag: {:?}", error); |
| 1270 | } |
| 1271 | |
| 1272 | self.focus(None)?; |
| 1273 | self.apply_layout()?; |
| 1274 | self.update_bar()?; |
| 1275 | |
| 1276 | Ok(()) |
| 1277 | } |
| 1278 | |
| 1279 | pub fn cycle_focus(&mut self, direction: i32) -> WmResult<()> { |
| 1280 | let visible = self.visible_windows(); |
| 1281 | |
| 1282 | if visible.is_empty() { |
| 1283 | return Ok(()); |
| 1284 | } |
| 1285 | |
| 1286 | let current = self |
| 1287 | .monitors |
| 1288 | .get(self.selected_monitor) |
| 1289 | .and_then(|m| m.selected_client); |
| 1290 | |
| 1291 | let next_window = if let Some(current) = current { |
| 1292 | if let Some(current_index) = visible.iter().position(|&w| w == current) { |
| 1293 | let next_index = if direction > 0 { |
| 1294 | (current_index + 1) % visible.len() |
| 1295 | } else { |
| 1296 | (current_index + visible.len() - 1) % visible.len() |
| 1297 | }; |
| 1298 | visible[next_index] |
| 1299 | } else { |
| 1300 | visible[0] |
| 1301 | } |
| 1302 | } else { |
| 1303 | visible[0] |
| 1304 | }; |
| 1305 | |
| 1306 | let is_tabbed = self.layout.name() == "tabbed"; |
| 1307 | if is_tabbed { |
| 1308 | self.connection.configure_window( |
| 1309 | next_window, |
| 1310 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 1311 | )?; |
| 1312 | } |
| 1313 | |
| 1314 | self.focus(Some(next_window))?; |
| 1315 | |
| 1316 | if is_tabbed { |
| 1317 | self.update_tab_bars()?; |
| 1318 | } |
| 1319 | |
| 1320 | Ok(()) |
| 1321 | } |
| 1322 | |
| 1323 | fn grab_keys(&mut self) -> WmResult<()> { |
| 1324 | self.keyboard_mapping = Some(keyboard::grab_keys( |
| 1325 | &self.connection, |
| 1326 | self.root, |
| 1327 | &self.config.keybindings, |
| 1328 | self.current_key, |
| 1329 | )?); |
| 1330 | Ok(()) |
| 1331 | } |
| 1332 | |
| 1333 | fn kill_client(&self, window: Window) -> WmResult<()> { |
| 1334 | if self.send_event(window, self.atoms.wm_delete_window)? { |
| 1335 | self.connection.flush()?; |
| 1336 | } else { |
| 1337 | eprintln!( |
| 1338 | "Window {} doesn't support WM_DELETE_WINDOW, killing forcefully", |
| 1339 | window |
| 1340 | ); |
| 1341 | self.connection.kill_client(window)?; |
| 1342 | self.connection.flush()?; |
| 1343 | } |
| 1344 | Ok(()) |
| 1345 | } |
| 1346 | |
| 1347 | fn send_event(&self, window: Window, protocol: Atom) -> WmResult<bool> { |
| 1348 | let protocols_reply = self |
| 1349 | .connection |
| 1350 | .get_property( |
| 1351 | false, |
| 1352 | window, |
| 1353 | self.atoms.wm_protocols, |
| 1354 | AtomEnum::ATOM, |
| 1355 | 0, |
| 1356 | 100, |
| 1357 | )? |
| 1358 | .reply(); |
| 1359 | |
| 1360 | let protocols_reply = match protocols_reply { |
| 1361 | Ok(reply) => reply, |
| 1362 | Err(_) => return Ok(false), |
| 1363 | }; |
| 1364 | |
| 1365 | let protocols: Vec<Atom> = protocols_reply |
| 1366 | .value |
| 1367 | .chunks_exact(4) |
| 1368 | .map(|chunk| u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]])) |
| 1369 | .collect(); |
| 1370 | |
| 1371 | if !protocols.contains(&protocol) { |
| 1372 | return Ok(false); |
| 1373 | } |
| 1374 | |
| 1375 | let event = x11rb::protocol::xproto::ClientMessageEvent { |
| 1376 | response_type: x11rb::protocol::xproto::CLIENT_MESSAGE_EVENT, |
| 1377 | format: 32, |
| 1378 | sequence: 0, |
| 1379 | window, |
| 1380 | type_: self.atoms.wm_protocols, |
| 1381 | data: x11rb::protocol::xproto::ClientMessageData::from([ |
| 1382 | protocol, |
| 1383 | x11rb::CURRENT_TIME, |
| 1384 | 0, |
| 1385 | 0, |
| 1386 | 0, |
| 1387 | ]), |
| 1388 | }; |
| 1389 | |
| 1390 | self.connection |
| 1391 | .send_event(false, window, EventMask::NO_EVENT, event)?; |
| 1392 | self.connection.flush()?; |
| 1393 | Ok(true) |
| 1394 | } |
| 1395 | |
| 1396 | fn set_urgent(&mut self, window: Window, urgent: bool) -> WmResult<()> { |
| 1397 | if let Some(client) = self.clients.get_mut(&window) { |
| 1398 | client.is_urgent = urgent; |
| 1399 | } |
| 1400 | |
| 1401 | let hints_reply = self |
| 1402 | .connection |
| 1403 | .get_property(false, window, AtomEnum::WM_HINTS, AtomEnum::WM_HINTS, 0, 9)? |
| 1404 | .reply(); |
| 1405 | |
| 1406 | if let Ok(hints) = hints_reply { |
| 1407 | if hints.value.len() >= 4 { |
| 1408 | let mut flags = u32::from_ne_bytes([ |
| 1409 | hints.value[0], |
| 1410 | hints.value[1], |
| 1411 | hints.value[2], |
| 1412 | hints.value[3], |
| 1413 | ]); |
| 1414 | |
| 1415 | if urgent { |
| 1416 | flags |= 256; |
| 1417 | } else { |
| 1418 | flags &= !256; |
| 1419 | } |
| 1420 | |
| 1421 | let mut new_hints = hints.value.clone(); |
| 1422 | new_hints[0..4].copy_from_slice(&flags.to_ne_bytes()); |
| 1423 | |
| 1424 | self.connection.change_property( |
| 1425 | PropMode::REPLACE, |
| 1426 | window, |
| 1427 | AtomEnum::WM_HINTS, |
| 1428 | AtomEnum::WM_HINTS, |
| 1429 | 32, |
| 1430 | new_hints.len() as u32 / 4, |
| 1431 | &new_hints, |
| 1432 | )?; |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | Ok(()) |
| 1437 | } |
| 1438 | |
| 1439 | fn get_window_atom_property(&self, window: Window, property: Atom) -> WmResult<Option<Atom>> { |
| 1440 | let reply = self |
| 1441 | .connection |
| 1442 | .get_property(false, window, property, AtomEnum::ATOM, 0, 1)? |
| 1443 | .reply(); |
| 1444 | |
| 1445 | match reply { |
| 1446 | Ok(prop) if !prop.value.is_empty() && prop.value.len() >= 4 => { |
| 1447 | let atom = u32::from_ne_bytes([ |
| 1448 | prop.value[0], |
| 1449 | prop.value[1], |
| 1450 | prop.value[2], |
| 1451 | prop.value[3], |
| 1452 | ]); |
| 1453 | Ok(Some(atom)) |
| 1454 | } |
| 1455 | _ => Ok(None), |
| 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | fn fullscreen(&mut self) -> WmResult<()> { |
| 1460 | if self.show_bar { |
| 1461 | let Some(focused_window) = self |
| 1462 | .monitors |
| 1463 | .get(self.selected_monitor) |
| 1464 | .and_then(|m| m.selected_client) |
| 1465 | else { |
| 1466 | return Ok(()); |
| 1467 | }; |
| 1468 | |
| 1469 | self.fullscreen_windows.insert(focused_window); |
| 1470 | |
| 1471 | let windows: Vec<Window> = self |
| 1472 | .windows |
| 1473 | .iter() |
| 1474 | .filter(|&&w| self.is_window_visible(w)) |
| 1475 | .copied() |
| 1476 | .collect(); |
| 1477 | |
| 1478 | for window in &windows { |
| 1479 | if let Ok(geom) = self.connection.get_geometry(*window)?.reply() { |
| 1480 | self.floating_geometry_before_fullscreen.insert( |
| 1481 | *window, |
| 1482 | ( |
| 1483 | geom.x, |
| 1484 | geom.y, |
| 1485 | geom.width, |
| 1486 | geom.height, |
| 1487 | geom.border_width as u16, |
| 1488 | ), |
| 1489 | ); |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | self.last_layout = Some(self.layout.name()); |
| 1494 | if let Ok(layout) = layout_from_str("monocle") { |
| 1495 | self.layout = layout; |
| 1496 | } |
| 1497 | self.toggle_bar()?; |
| 1498 | self.apply_layout()?; |
| 1499 | |
| 1500 | for window in &windows { |
| 1501 | self.connection.configure_window( |
| 1502 | *window, |
| 1503 | &x11rb::protocol::xproto::ConfigureWindowAux::new().border_width(0), |
| 1504 | )?; |
| 1505 | } |
| 1506 | |
| 1507 | let border_width = self.config.border_width; |
| 1508 | let floating_windows: Vec<Window> = windows |
| 1509 | .iter() |
| 1510 | .filter(|&&w| self.floating_windows.contains(&w)) |
| 1511 | .copied() |
| 1512 | .collect(); |
| 1513 | |
| 1514 | for window in floating_windows { |
| 1515 | let monitor_idx = self |
| 1516 | .clients |
| 1517 | .get(&window) |
| 1518 | .map(|c| c.monitor_index) |
| 1519 | .unwrap_or(self.selected_monitor); |
| 1520 | let monitor = &self.monitors[monitor_idx]; |
| 1521 | |
| 1522 | let (outer_gap_h, outer_gap_v) = if self.gaps_enabled { |
| 1523 | ( |
| 1524 | self.config.gap_outer_horizontal, |
| 1525 | self.config.gap_outer_vertical, |
| 1526 | ) |
| 1527 | } else { |
| 1528 | (0, 0) |
| 1529 | }; |
| 1530 | |
| 1531 | let window_x = monitor.screen_x + outer_gap_h as i32; |
| 1532 | let window_y = monitor.screen_y + outer_gap_v as i32; |
| 1533 | let window_width = monitor |
| 1534 | .screen_width |
| 1535 | .saturating_sub(2 * outer_gap_h as i32) |
| 1536 | .saturating_sub(2 * border_width as i32); |
| 1537 | let window_height = monitor |
| 1538 | .screen_height |
| 1539 | .saturating_sub(2 * outer_gap_v as i32) |
| 1540 | .saturating_sub(2 * border_width as i32); |
| 1541 | |
| 1542 | self.connection.configure_window( |
| 1543 | window, |
| 1544 | &x11rb::protocol::xproto::ConfigureWindowAux::new() |
| 1545 | .x(window_x) |
| 1546 | .y(window_y) |
| 1547 | .width(window_width as u32) |
| 1548 | .height(window_height as u32), |
| 1549 | )?; |
| 1550 | } |
| 1551 | self.connection.flush()?; |
| 1552 | } else { |
| 1553 | let Some(focused_window) = self |
| 1554 | .monitors |
| 1555 | .get(self.selected_monitor) |
| 1556 | .and_then(|m| m.selected_client) |
| 1557 | else { |
| 1558 | return Ok(()); |
| 1559 | }; |
| 1560 | |
| 1561 | self.fullscreen_windows.remove(&focused_window); |
| 1562 | |
| 1563 | if let Some(last) = self.last_layout { |
| 1564 | if let Ok(layout) = layout_from_str(last) { |
| 1565 | self.layout = layout; |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | let windows_to_restore: Vec<Window> = self |
| 1570 | .floating_geometry_before_fullscreen |
| 1571 | .keys() |
| 1572 | .copied() |
| 1573 | .collect(); |
| 1574 | |
| 1575 | for window in windows_to_restore { |
| 1576 | if let Some(&(x, y, width, height, border_width)) = |
| 1577 | self.floating_geometry_before_fullscreen.get(&window) |
| 1578 | { |
| 1579 | self.connection.configure_window( |
| 1580 | window, |
| 1581 | &ConfigureWindowAux::new() |
| 1582 | .x(x as i32) |
| 1583 | .y(y as i32) |
| 1584 | .width(width as u32) |
| 1585 | .height(height as u32) |
| 1586 | .border_width(border_width as u32), |
| 1587 | )?; |
| 1588 | |
| 1589 | if let Some(c) = self.clients.get_mut(&window) { |
| 1590 | c.x_position = x; |
| 1591 | c.y_position = y; |
| 1592 | c.width = width; |
| 1593 | c.height = height; |
| 1594 | c.border_width = border_width; |
| 1595 | } |
| 1596 | |
| 1597 | self.floating_geometry_before_fullscreen.remove(&window); |
| 1598 | } |
| 1599 | } |
| 1600 | self.connection.flush()?; |
| 1601 | |
| 1602 | self.toggle_bar()?; |
| 1603 | |
| 1604 | if self.layout.name() != "normie" { |
| 1605 | self.apply_layout()?; |
| 1606 | } else { |
| 1607 | if let Some(bar) = self.bars.get(self.selected_monitor) { |
| 1608 | self.connection.configure_window( |
| 1609 | bar.window(), |
| 1610 | &x11rb::protocol::xproto::ConfigureWindowAux::new() |
| 1611 | .stack_mode(x11rb::protocol::xproto::StackMode::ABOVE), |
| 1612 | )?; |
| 1613 | self.connection.flush()?; |
| 1614 | } |
| 1615 | } |
| 1616 | } |
| 1617 | Ok(()) |
| 1618 | } |
| 1619 | |
| 1620 | fn set_window_fullscreen(&mut self, window: Window, fullscreen: bool) -> WmResult<()> { |
| 1621 | let monitor_idx = self |
| 1622 | .clients |
| 1623 | .get(&window) |
| 1624 | .map(|c| c.monitor_index) |
| 1625 | .unwrap_or(self.selected_monitor); |
| 1626 | let monitor = &self.monitors[monitor_idx]; |
| 1627 | |
| 1628 | if fullscreen && !self.fullscreen_windows.contains(&window) { |
| 1629 | let bytes = self.atoms.net_wm_state_fullscreen.to_ne_bytes().to_vec(); |
| 1630 | self.connection.change_property( |
| 1631 | PropMode::REPLACE, |
| 1632 | window, |
| 1633 | self.atoms.net_wm_state, |
| 1634 | AtomEnum::ATOM, |
| 1635 | 32, |
| 1636 | 1, |
| 1637 | &bytes, |
| 1638 | )?; |
| 1639 | |
| 1640 | if let Some(client) = self.clients.get_mut(&window) { |
| 1641 | client.is_fullscreen = true; |
| 1642 | client.old_state = client.is_floating; |
| 1643 | client.old_border_width = client.border_width; |
| 1644 | client.border_width = 0; |
| 1645 | client.is_floating = true; |
| 1646 | } |
| 1647 | |
| 1648 | self.fullscreen_windows.insert(window); |
| 1649 | |
| 1650 | self.connection.configure_window( |
| 1651 | window, |
| 1652 | &x11rb::protocol::xproto::ConfigureWindowAux::new() |
| 1653 | .border_width(0) |
| 1654 | .x(monitor.screen_x) |
| 1655 | .y(monitor.screen_y) |
| 1656 | .width(monitor.screen_width as u32) |
| 1657 | .height(monitor.screen_height as u32) |
| 1658 | .stack_mode(x11rb::protocol::xproto::StackMode::ABOVE), |
| 1659 | )?; |
| 1660 | |
| 1661 | self.connection.flush()?; |
| 1662 | } else if !fullscreen && self.fullscreen_windows.contains(&window) { |
| 1663 | self.connection.change_property( |
| 1664 | PropMode::REPLACE, |
| 1665 | window, |
| 1666 | self.atoms.net_wm_state, |
| 1667 | AtomEnum::ATOM, |
| 1668 | 32, |
| 1669 | 0, |
| 1670 | &[], |
| 1671 | )?; |
| 1672 | |
| 1673 | self.fullscreen_windows.remove(&window); |
| 1674 | |
| 1675 | if let Some(client) = self.clients.get_mut(&window) { |
| 1676 | client.is_fullscreen = false; |
| 1677 | client.is_floating = client.old_state; |
| 1678 | client.border_width = client.old_border_width; |
| 1679 | |
| 1680 | let x = client.old_x_position; |
| 1681 | let y = client.old_y_position; |
| 1682 | let w = client.old_width; |
| 1683 | let h = client.old_height; |
| 1684 | let bw = client.border_width; |
| 1685 | |
| 1686 | self.connection.configure_window( |
| 1687 | window, |
| 1688 | &x11rb::protocol::xproto::ConfigureWindowAux::new() |
| 1689 | .x(x as i32) |
| 1690 | .y(y as i32) |
| 1691 | .width(w as u32) |
| 1692 | .height(h as u32) |
| 1693 | .border_width(bw as u32), |
| 1694 | )?; |
| 1695 | } |
| 1696 | |
| 1697 | self.apply_layout()?; |
| 1698 | } |
| 1699 | |
| 1700 | Ok(()) |
| 1701 | } |
| 1702 | |
| 1703 | fn toggle_bar(&mut self) -> WmResult<()> { |
| 1704 | self.show_bar = !self.show_bar; |
| 1705 | if let Some(bar) = self.bars.get(self.selected_monitor) { |
| 1706 | if self.show_bar { |
| 1707 | self.connection.map_window(bar.window())?; |
| 1708 | } else { |
| 1709 | self.connection.unmap_window(bar.window())?; |
| 1710 | } |
| 1711 | self.connection.flush()?; |
| 1712 | } |
| 1713 | self.apply_layout()?; |
| 1714 | Ok(()) |
| 1715 | } |
| 1716 | |
| 1717 | fn get_transient_parent(&self, window: Window) -> Option<Window> { |
| 1718 | self.connection |
| 1719 | .get_property( |
| 1720 | false, |
| 1721 | window, |
| 1722 | AtomEnum::WM_TRANSIENT_FOR, |
| 1723 | AtomEnum::WINDOW, |
| 1724 | 0, |
| 1725 | 1, |
| 1726 | ) |
| 1727 | .ok() |
| 1728 | .and_then(|cookie| cookie.reply().ok()) |
| 1729 | .filter(|reply| !reply.value.is_empty()) |
| 1730 | .and_then(|reply| { |
| 1731 | if reply.value.len() >= 4 { |
| 1732 | let parent_window = u32::from_ne_bytes([ |
| 1733 | reply.value[0], |
| 1734 | reply.value[1], |
| 1735 | reply.value[2], |
| 1736 | reply.value[3], |
| 1737 | ]); |
| 1738 | Some(parent_window) |
| 1739 | } else { |
| 1740 | None |
| 1741 | } |
| 1742 | }) |
| 1743 | } |
| 1744 | |
| 1745 | fn get_window_class_instance(&self, window: Window) -> (String, String) { |
| 1746 | let reply = self |
| 1747 | .connection |
| 1748 | .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024) |
| 1749 | .ok() |
| 1750 | .and_then(|cookie| cookie.reply().ok()); |
| 1751 | |
| 1752 | if let Some(reply) = reply { |
| 1753 | if !reply.value.is_empty() { |
| 1754 | if let Ok(text) = std::str::from_utf8(&reply.value) { |
| 1755 | let parts: Vec<&str> = text.split('\0').collect(); |
| 1756 | let instance = parts.get(0).unwrap_or(&"").to_string(); |
| 1757 | let class = parts.get(1).unwrap_or(&"").to_string(); |
| 1758 | return (instance, class); |
| 1759 | } |
| 1760 | } |
| 1761 | } |
| 1762 | |
| 1763 | (String::new(), String::new()) |
| 1764 | } |
| 1765 | |
| 1766 | fn apply_rules(&mut self, window: Window) -> WmResult<()> { |
| 1767 | let (instance, class) = self.get_window_class_instance(window); |
| 1768 | let title = self |
| 1769 | .clients |
| 1770 | .get(&window) |
| 1771 | .map(|c| c.name.clone()) |
| 1772 | .unwrap_or_default(); |
| 1773 | |
| 1774 | let mut rule_tags: Option<u32> = None; |
| 1775 | let mut rule_floating: Option<bool> = None; |
| 1776 | let mut rule_monitor: Option<usize> = None; |
| 1777 | |
| 1778 | for rule in &self.config.window_rules { |
| 1779 | if rule.matches(&class, &instance, &title) { |
| 1780 | if rule.tags.is_some() { |
| 1781 | rule_tags = rule.tags; |
| 1782 | } |
| 1783 | if rule.is_floating.is_some() { |
| 1784 | rule_floating = rule.is_floating; |
| 1785 | } |
| 1786 | if rule.monitor.is_some() { |
| 1787 | rule_monitor = rule.monitor; |
| 1788 | } |
| 1789 | } |
| 1790 | } |
| 1791 | |
| 1792 | if let Some(client) = self.clients.get_mut(&window) { |
| 1793 | if let Some(is_floating) = rule_floating { |
| 1794 | client.is_floating = is_floating; |
| 1795 | if is_floating { |
| 1796 | self.floating_windows.insert(window); |
| 1797 | } else { |
| 1798 | self.floating_windows.remove(&window); |
| 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | if let Some(monitor_index) = rule_monitor { |
| 1803 | if monitor_index < self.monitors.len() { |
| 1804 | client.monitor_index = monitor_index; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | let tags = rule_tags.unwrap_or_else(|| { |
| 1809 | self.monitors |
| 1810 | .get(client.monitor_index) |
| 1811 | .map(|m| m.tagset[m.selected_tags_index]) |
| 1812 | .unwrap_or(tag_mask(0)) |
| 1813 | }); |
| 1814 | |
| 1815 | client.tags = tags; |
| 1816 | } |
| 1817 | |
| 1818 | Ok(()) |
| 1819 | } |
| 1820 | |
| 1821 | fn manage_window(&mut self, window: Window) -> WmResult<()> { |
| 1822 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 1823 | let border_width = self.config.border_width; |
| 1824 | |
| 1825 | let transient_parent = self.get_transient_parent(window); |
| 1826 | let is_transient = transient_parent.is_some(); |
| 1827 | |
| 1828 | let (monitor_index, tags) = if let Some(parent) = transient_parent { |
| 1829 | if let Some(parent_client) = self.clients.get(&parent) { |
| 1830 | (parent_client.monitor_index, parent_client.tags) |
| 1831 | } else { |
| 1832 | let tags = self |
| 1833 | .monitors |
| 1834 | .get(self.selected_monitor) |
| 1835 | .map(|m| m.tagset[m.selected_tags_index]) |
| 1836 | .unwrap_or(tag_mask(0)); |
| 1837 | (self.selected_monitor, tags) |
| 1838 | } |
| 1839 | } else { |
| 1840 | let tags = self |
| 1841 | .monitors |
| 1842 | .get(self.selected_monitor) |
| 1843 | .map(|m| m.tagset[m.selected_tags_index]) |
| 1844 | .unwrap_or(tag_mask(0)); |
| 1845 | (self.selected_monitor, tags) |
| 1846 | }; |
| 1847 | |
| 1848 | let mut client = Client::new(window, monitor_index, tags); |
| 1849 | client.x_position = geometry.x; |
| 1850 | client.y_position = geometry.y; |
| 1851 | client.width = geometry.width; |
| 1852 | client.height = geometry.height; |
| 1853 | client.old_x_position = geometry.x; |
| 1854 | client.old_y_position = geometry.y; |
| 1855 | client.old_width = geometry.width; |
| 1856 | client.old_height = geometry.height; |
| 1857 | client.old_border_width = geometry.border_width; |
| 1858 | client.border_width = border_width as u16; |
| 1859 | |
| 1860 | self.clients.insert(window, client); |
| 1861 | self.update_window_title(window)?; |
| 1862 | |
| 1863 | if !is_transient { |
| 1864 | self.apply_rules(window)?; |
| 1865 | } |
| 1866 | |
| 1867 | let client_monitor = self |
| 1868 | .clients |
| 1869 | .get(&window) |
| 1870 | .map(|c| c.monitor_index) |
| 1871 | .unwrap_or(monitor_index); |
| 1872 | let monitor = &self.monitors[client_monitor]; |
| 1873 | |
| 1874 | let mut x = self |
| 1875 | .clients |
| 1876 | .get(&window) |
| 1877 | .map(|c| c.x_position as i32) |
| 1878 | .unwrap_or(0); |
| 1879 | let mut y = self |
| 1880 | .clients |
| 1881 | .get(&window) |
| 1882 | .map(|c| c.y_position as i32) |
| 1883 | .unwrap_or(0); |
| 1884 | let w = self |
| 1885 | .clients |
| 1886 | .get(&window) |
| 1887 | .map(|c| c.width as i32) |
| 1888 | .unwrap_or(1); |
| 1889 | let h = self |
| 1890 | .clients |
| 1891 | .get(&window) |
| 1892 | .map(|c| c.height as i32) |
| 1893 | .unwrap_or(1); |
| 1894 | let bw = border_width as i32; |
| 1895 | |
| 1896 | if x + w + 2 * bw > monitor.window_area_x + monitor.window_area_width { |
| 1897 | x = monitor.window_area_x + monitor.window_area_width - w - 2 * bw; |
| 1898 | } |
| 1899 | if y + h + 2 * bw > monitor.window_area_y + monitor.window_area_height { |
| 1900 | y = monitor.window_area_y + monitor.window_area_height - h - 2 * bw; |
| 1901 | } |
| 1902 | x = x.max(monitor.window_area_x); |
| 1903 | y = y.max(monitor.window_area_y); |
| 1904 | |
| 1905 | if let Some(c) = self.clients.get_mut(&window) { |
| 1906 | c.x_position = x as i16; |
| 1907 | c.y_position = y as i16; |
| 1908 | } |
| 1909 | |
| 1910 | self.connection.configure_window( |
| 1911 | window, |
| 1912 | &ConfigureWindowAux::new().border_width(border_width), |
| 1913 | )?; |
| 1914 | self.connection.change_window_attributes( |
| 1915 | window, |
| 1916 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), |
| 1917 | )?; |
| 1918 | self.send_configure_notify(window)?; |
| 1919 | self.update_window_type(window)?; |
| 1920 | self.update_size_hints(window)?; |
| 1921 | self.update_window_hints(window)?; |
| 1922 | |
| 1923 | self.connection.change_window_attributes( |
| 1924 | window, |
| 1925 | &ChangeWindowAttributesAux::new().event_mask( |
| 1926 | EventMask::ENTER_WINDOW |
| 1927 | | EventMask::FOCUS_CHANGE |
| 1928 | | EventMask::PROPERTY_CHANGE |
| 1929 | | EventMask::STRUCTURE_NOTIFY, |
| 1930 | ), |
| 1931 | )?; |
| 1932 | |
| 1933 | let is_fixed = self |
| 1934 | .clients |
| 1935 | .get(&window) |
| 1936 | .map(|c| c.is_fixed) |
| 1937 | .unwrap_or(false); |
| 1938 | if let Some(c) = self.clients.get_mut(&window) { |
| 1939 | if !c.is_floating { |
| 1940 | c.is_floating = is_transient || is_fixed; |
| 1941 | c.old_state = c.is_floating; |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | if self |
| 1946 | .clients |
| 1947 | .get(&window) |
| 1948 | .map(|c| c.is_floating) |
| 1949 | .unwrap_or(false) |
| 1950 | { |
| 1951 | self.floating_windows.insert(window); |
| 1952 | self.connection.configure_window( |
| 1953 | window, |
| 1954 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 1955 | )?; |
| 1956 | } |
| 1957 | |
| 1958 | self.attach_aside(window, client_monitor); |
| 1959 | self.attach_stack(window, client_monitor); |
| 1960 | self.windows.push(window); |
| 1961 | |
| 1962 | let off_screen_x = x + 2 * self.screen.width_in_pixels as i32; |
| 1963 | self.connection.configure_window( |
| 1964 | window, |
| 1965 | &ConfigureWindowAux::new() |
| 1966 | .x(off_screen_x) |
| 1967 | .y(y) |
| 1968 | .width(w as u32) |
| 1969 | .height(h as u32), |
| 1970 | )?; |
| 1971 | |
| 1972 | self.set_wm_state(window, 1)?; |
| 1973 | |
| 1974 | let final_tags = self.clients.get(&window).map(|c| c.tags).unwrap_or(tags); |
| 1975 | let _ = self.save_client_tag(window, final_tags); |
| 1976 | |
| 1977 | if client_monitor == self.selected_monitor { |
| 1978 | if let Some(old_sel) = self |
| 1979 | .monitors |
| 1980 | .get(self.selected_monitor) |
| 1981 | .and_then(|m| m.selected_client) |
| 1982 | { |
| 1983 | self.unfocus(old_sel)?; |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | if let Some(m) = self.monitors.get_mut(client_monitor) { |
| 1988 | m.selected_client = Some(window); |
| 1989 | } |
| 1990 | |
| 1991 | self.apply_layout()?; |
| 1992 | self.connection.map_window(window)?; |
| 1993 | self.focus(Some(window))?; |
| 1994 | self.update_bar()?; |
| 1995 | |
| 1996 | if self.layout.name() == "tabbed" { |
| 1997 | self.update_tab_bars()?; |
| 1998 | } |
| 1999 | |
| 2000 | Ok(()) |
| 2001 | } |
| 2002 | |
| 2003 | pub fn set_focus(&mut self, window: Window) -> WmResult<()> { |
| 2004 | let old_focused = self.previous_focused; |
| 2005 | |
| 2006 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 2007 | monitor.selected_client = Some(window); |
| 2008 | } |
| 2009 | |
| 2010 | self.connection |
| 2011 | .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?; |
| 2012 | self.connection.flush()?; |
| 2013 | |
| 2014 | self.update_focus_visuals(old_focused, window)?; |
| 2015 | self.previous_focused = Some(window); |
| 2016 | |
| 2017 | if self.layout.name() == "tabbed" { |
| 2018 | self.update_tab_bars()?; |
| 2019 | } |
| 2020 | |
| 2021 | Ok(()) |
| 2022 | } |
| 2023 | |
| 2024 | fn unfocus(&self, window: Window) -> WmResult<()> { |
| 2025 | if !self.windows.contains(&window) { |
| 2026 | return Ok(()); |
| 2027 | } |
| 2028 | |
| 2029 | self.connection.change_window_attributes( |
| 2030 | window, |
| 2031 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), |
| 2032 | )?; |
| 2033 | |
| 2034 | self.connection.grab_button( |
| 2035 | false, |
| 2036 | window, |
| 2037 | EventMask::BUTTON_PRESS.into(), |
| 2038 | GrabMode::SYNC, |
| 2039 | GrabMode::SYNC, |
| 2040 | x11rb::NONE, |
| 2041 | x11rb::NONE, |
| 2042 | ButtonIndex::ANY, |
| 2043 | ModMask::ANY, |
| 2044 | )?; |
| 2045 | |
| 2046 | Ok(()) |
| 2047 | } |
| 2048 | |
| 2049 | fn focus(&mut self, window: Option<Window>) -> WmResult<()> { |
| 2050 | let monitor = self.monitors.get_mut(self.selected_monitor).unwrap(); |
| 2051 | let old_selected = monitor.selected_client; |
| 2052 | |
| 2053 | if let Some(old_win) = old_selected { |
| 2054 | if old_selected != window { |
| 2055 | self.unfocus(old_win)?; |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | let mut win = window; |
| 2060 | if win.is_none() || !self.is_visible(win.unwrap()) { |
| 2061 | let mut current = self |
| 2062 | .monitors |
| 2063 | .get(self.selected_monitor) |
| 2064 | .and_then(|m| m.stack_head); |
| 2065 | |
| 2066 | while let Some(w) = current { |
| 2067 | if self.is_visible(w) { |
| 2068 | win = Some(w); |
| 2069 | break; |
| 2070 | } |
| 2071 | current = self.clients.get(&w).and_then(|c| c.stack_next); |
| 2072 | } |
| 2073 | } |
| 2074 | |
| 2075 | if let Some(win) = win { |
| 2076 | if !self.windows.contains(&win) { |
| 2077 | return Ok(()); |
| 2078 | } |
| 2079 | |
| 2080 | let monitor_idx = self |
| 2081 | .clients |
| 2082 | .get(&win) |
| 2083 | .map(|c| c.monitor_index) |
| 2084 | .unwrap_or(self.selected_monitor); |
| 2085 | if monitor_idx != self.selected_monitor { |
| 2086 | self.selected_monitor = monitor_idx; |
| 2087 | } |
| 2088 | |
| 2089 | self.detach_stack(win); |
| 2090 | self.attach_stack(win, monitor_idx); |
| 2091 | |
| 2092 | self.connection.change_window_attributes( |
| 2093 | win, |
| 2094 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused), |
| 2095 | )?; |
| 2096 | |
| 2097 | self.connection |
| 2098 | .ungrab_button(ButtonIndex::ANY, win, ModMask::ANY)?; |
| 2099 | |
| 2100 | self.connection |
| 2101 | .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?; |
| 2102 | |
| 2103 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 2104 | monitor.selected_client = Some(win); |
| 2105 | } |
| 2106 | |
| 2107 | self.previous_focused = Some(win); |
| 2108 | } else { |
| 2109 | self.connection.set_input_focus( |
| 2110 | InputFocus::POINTER_ROOT, |
| 2111 | self.root, |
| 2112 | x11rb::CURRENT_TIME, |
| 2113 | )?; |
| 2114 | |
| 2115 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 2116 | monitor.selected_client = None; |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | self.connection.flush()?; |
| 2121 | |
| 2122 | Ok(()) |
| 2123 | } |
| 2124 | |
| 2125 | fn restack(&mut self) -> WmResult<()> { |
| 2126 | let monitor = match self.monitors.get(self.selected_monitor) { |
| 2127 | Some(m) => m, |
| 2128 | None => return Ok(()), |
| 2129 | }; |
| 2130 | |
| 2131 | let mut windows_to_restack: Vec<Window> = Vec::new(); |
| 2132 | |
| 2133 | if let Some(selected) = monitor.selected_client { |
| 2134 | if self.floating_windows.contains(&selected) { |
| 2135 | windows_to_restack.push(selected); |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | let mut current = monitor.stack_head; |
| 2140 | while let Some(win) = current { |
| 2141 | if self.windows.contains(&win) && self.floating_windows.contains(&win) { |
| 2142 | if Some(win) != monitor.selected_client { |
| 2143 | windows_to_restack.push(win); |
| 2144 | } |
| 2145 | } |
| 2146 | current = self.clients.get(&win).and_then(|c| c.stack_next); |
| 2147 | } |
| 2148 | |
| 2149 | current = monitor.stack_head; |
| 2150 | while let Some(win) = current { |
| 2151 | if self.windows.contains(&win) && !self.floating_windows.contains(&win) { |
| 2152 | windows_to_restack.push(win); |
| 2153 | } |
| 2154 | current = self.clients.get(&win).and_then(|c| c.stack_next); |
| 2155 | } |
| 2156 | |
| 2157 | for (i, &win) in windows_to_restack.iter().enumerate() { |
| 2158 | if i == 0 { |
| 2159 | self.connection.configure_window( |
| 2160 | win, |
| 2161 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 2162 | )?; |
| 2163 | } else { |
| 2164 | self.connection.configure_window( |
| 2165 | win, |
| 2166 | &ConfigureWindowAux::new() |
| 2167 | .sibling(windows_to_restack[i - 1]) |
| 2168 | .stack_mode(StackMode::BELOW), |
| 2169 | )?; |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | Ok(()) |
| 2174 | } |
| 2175 | |
| 2176 | fn focusstack(&mut self, direction: i32) -> WmResult<()> { |
| 2177 | let monitor = match self.monitors.get(self.selected_monitor) { |
| 2178 | Some(m) => m, |
| 2179 | None => return Ok(()), |
| 2180 | }; |
| 2181 | |
| 2182 | let selected = match monitor.selected_client { |
| 2183 | Some(win) => win, |
| 2184 | None => return Ok(()), |
| 2185 | }; |
| 2186 | |
| 2187 | let selected_tags = monitor.tagset[monitor.selected_tags_index]; |
| 2188 | |
| 2189 | let mut stack_windows: Vec<Window> = Vec::new(); |
| 2190 | let mut current = monitor.clients_head; |
| 2191 | while let Some(win) = current { |
| 2192 | if let Some(client) = self.clients.get(&win) { |
| 2193 | if client.tags & selected_tags != 0 && !client.is_floating { |
| 2194 | stack_windows.push(win); |
| 2195 | } |
| 2196 | current = client.next; |
| 2197 | } else { |
| 2198 | break; |
| 2199 | } |
| 2200 | } |
| 2201 | |
| 2202 | if stack_windows.is_empty() { |
| 2203 | return Ok(()); |
| 2204 | } |
| 2205 | |
| 2206 | let current_idx = stack_windows.iter().position(|&w| w == selected); |
| 2207 | |
| 2208 | let next_window = if let Some(idx) = current_idx { |
| 2209 | if direction > 0 { |
| 2210 | if idx + 1 < stack_windows.len() { |
| 2211 | stack_windows[idx + 1] |
| 2212 | } else { |
| 2213 | stack_windows[0] |
| 2214 | } |
| 2215 | } else { |
| 2216 | if idx > 0 { |
| 2217 | stack_windows[idx - 1] |
| 2218 | } else { |
| 2219 | stack_windows[stack_windows.len() - 1] |
| 2220 | } |
| 2221 | } |
| 2222 | } else { |
| 2223 | return Ok(()); |
| 2224 | }; |
| 2225 | |
| 2226 | self.focus(Some(next_window))?; |
| 2227 | self.update_tab_bars()?; |
| 2228 | |
| 2229 | Ok(()) |
| 2230 | } |
| 2231 | |
| 2232 | pub fn move_stack(&mut self, direction: i32) -> WmResult<()> { |
| 2233 | let monitor_index = self.selected_monitor; |
| 2234 | let monitor = match self.monitors.get(monitor_index) { |
| 2235 | Some(m) => m.clone(), |
| 2236 | None => return Ok(()), |
| 2237 | }; |
| 2238 | |
| 2239 | let selected = match monitor.selected_client { |
| 2240 | Some(win) => win, |
| 2241 | None => return Ok(()), |
| 2242 | }; |
| 2243 | |
| 2244 | let selected_client = match self.clients.get(&selected) { |
| 2245 | Some(c) => c, |
| 2246 | None => return Ok(()), |
| 2247 | }; |
| 2248 | |
| 2249 | let target = if direction > 0 { |
| 2250 | let next = self.next_tiled(selected_client.next, &monitor); |
| 2251 | if next.is_some() { |
| 2252 | next |
| 2253 | } else { |
| 2254 | self.next_tiled(monitor.clients_head, &monitor) |
| 2255 | } |
| 2256 | } else { |
| 2257 | let mut previous = None; |
| 2258 | let mut current = monitor.clients_head; |
| 2259 | while let Some(window) = current { |
| 2260 | if window == selected { |
| 2261 | break; |
| 2262 | } |
| 2263 | if let Some(client) = self.clients.get(&window) { |
| 2264 | let visible_tags = client.tags & monitor.tagset[monitor.selected_tags_index]; |
| 2265 | if visible_tags != 0 && !client.is_floating { |
| 2266 | previous = Some(window); |
| 2267 | } |
| 2268 | current = client.next; |
| 2269 | } else { |
| 2270 | break; |
| 2271 | } |
| 2272 | } |
| 2273 | if previous.is_none() { |
| 2274 | let mut last = None; |
| 2275 | let mut current = monitor.clients_head; |
| 2276 | while let Some(window) = current { |
| 2277 | if let Some(client) = self.clients.get(&window) { |
| 2278 | let visible_tags = |
| 2279 | client.tags & monitor.tagset[monitor.selected_tags_index]; |
| 2280 | if visible_tags != 0 && !client.is_floating { |
| 2281 | last = Some(window); |
| 2282 | } |
| 2283 | current = client.next; |
| 2284 | } else { |
| 2285 | break; |
| 2286 | } |
| 2287 | } |
| 2288 | last |
| 2289 | } else { |
| 2290 | previous |
| 2291 | } |
| 2292 | }; |
| 2293 | |
| 2294 | let target = match target { |
| 2295 | Some(t) if t != selected => t, |
| 2296 | _ => return Ok(()), |
| 2297 | }; |
| 2298 | |
| 2299 | let mut prev_selected = None; |
| 2300 | let mut prev_target = None; |
| 2301 | let mut current = monitor.clients_head; |
| 2302 | |
| 2303 | while let Some(window) = current { |
| 2304 | if let Some(client) = self.clients.get(&window) { |
| 2305 | if client.next == Some(selected) { |
| 2306 | prev_selected = Some(window); |
| 2307 | } |
| 2308 | if client.next == Some(target) { |
| 2309 | prev_target = Some(window); |
| 2310 | } |
| 2311 | current = client.next; |
| 2312 | } else { |
| 2313 | break; |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | let selected_next = self.clients.get(&selected).and_then(|c| c.next); |
| 2318 | let target_next = self.clients.get(&target).and_then(|c| c.next); |
| 2319 | |
| 2320 | let temp = if selected_next == Some(target) { |
| 2321 | Some(selected) |
| 2322 | } else { |
| 2323 | selected_next |
| 2324 | }; |
| 2325 | |
| 2326 | if let Some(client) = self.clients.get_mut(&selected) { |
| 2327 | client.next = if target_next == Some(selected) { |
| 2328 | Some(target) |
| 2329 | } else { |
| 2330 | target_next |
| 2331 | }; |
| 2332 | } |
| 2333 | |
| 2334 | if let Some(client) = self.clients.get_mut(&target) { |
| 2335 | client.next = temp; |
| 2336 | } |
| 2337 | |
| 2338 | if let Some(prev) = prev_selected { |
| 2339 | if prev != target { |
| 2340 | if let Some(client) = self.clients.get_mut(&prev) { |
| 2341 | client.next = Some(target); |
| 2342 | } |
| 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | if let Some(prev) = prev_target { |
| 2347 | if prev != selected { |
| 2348 | if let Some(client) = self.clients.get_mut(&prev) { |
| 2349 | client.next = Some(selected); |
| 2350 | } |
| 2351 | } |
| 2352 | } |
| 2353 | |
| 2354 | if let Some(monitor) = self.monitors.get_mut(monitor_index) { |
| 2355 | if monitor.clients_head == Some(selected) { |
| 2356 | monitor.clients_head = Some(target); |
| 2357 | } else if monitor.clients_head == Some(target) { |
| 2358 | monitor.clients_head = Some(selected); |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | self.apply_layout()?; |
| 2363 | Ok(()) |
| 2364 | } |
| 2365 | |
| 2366 | pub fn focus_monitor(&mut self, direction: i32) -> WmResult<()> { |
| 2367 | if self.monitors.len() <= 1 { |
| 2368 | return Ok(()); |
| 2369 | } |
| 2370 | |
| 2371 | let target_monitor = match self.get_adjacent_monitor(direction) { |
| 2372 | Some(idx) if idx != self.selected_monitor => idx, |
| 2373 | _ => return Ok(()), |
| 2374 | }; |
| 2375 | |
| 2376 | let old_selected = self |
| 2377 | .monitors |
| 2378 | .get(self.selected_monitor) |
| 2379 | .and_then(|m| m.selected_client); |
| 2380 | |
| 2381 | if let Some(win) = old_selected { |
| 2382 | self.unfocus(win)?; |
| 2383 | } |
| 2384 | |
| 2385 | self.selected_monitor = target_monitor; |
| 2386 | self.focus(None)?; |
| 2387 | |
| 2388 | Ok(()) |
| 2389 | } |
| 2390 | |
| 2391 | pub fn send_window_to_adjacent_monitor(&mut self, direction: i32) -> WmResult<()> { |
| 2392 | if self.monitors.len() <= 1 { |
| 2393 | return Ok(()); |
| 2394 | } |
| 2395 | |
| 2396 | let selected_window = self |
| 2397 | .monitors |
| 2398 | .get(self.selected_monitor) |
| 2399 | .and_then(|m| m.selected_client); |
| 2400 | |
| 2401 | let window = match selected_window { |
| 2402 | Some(win) => win, |
| 2403 | None => return Ok(()), |
| 2404 | }; |
| 2405 | |
| 2406 | let target_monitor = match self.get_adjacent_monitor(direction) { |
| 2407 | Some(idx) => idx, |
| 2408 | None => return Ok(()), |
| 2409 | }; |
| 2410 | |
| 2411 | self.move_window_to_monitor(window, target_monitor)?; |
| 2412 | |
| 2413 | Ok(()) |
| 2414 | } |
| 2415 | |
| 2416 | fn update_focus_visuals( |
| 2417 | &self, |
| 2418 | old_focused: Option<Window>, |
| 2419 | new_focused: Window, |
| 2420 | ) -> WmResult<()> { |
| 2421 | if let Some(old_win) = old_focused { |
| 2422 | if old_win != new_focused { |
| 2423 | self.connection.configure_window( |
| 2424 | old_win, |
| 2425 | &ConfigureWindowAux::new().border_width(self.config.border_width), |
| 2426 | )?; |
| 2427 | |
| 2428 | self.connection.change_window_attributes( |
| 2429 | old_win, |
| 2430 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), |
| 2431 | )?; |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | self.connection.configure_window( |
| 2436 | new_focused, |
| 2437 | &ConfigureWindowAux::new().border_width(self.config.border_width), |
| 2438 | )?; |
| 2439 | |
| 2440 | self.connection.change_window_attributes( |
| 2441 | new_focused, |
| 2442 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused), |
| 2443 | )?; |
| 2444 | |
| 2445 | self.connection.flush()?; |
| 2446 | Ok(()) |
| 2447 | } |
| 2448 | |
| 2449 | fn drag_window(&mut self, window: Window) -> WmResult<()> { |
| 2450 | let is_fullscreen = self |
| 2451 | .clients |
| 2452 | .get(&window) |
| 2453 | .map(|c| c.is_fullscreen) |
| 2454 | .unwrap_or(false); |
| 2455 | |
| 2456 | if is_fullscreen { |
| 2457 | return Ok(()); |
| 2458 | } |
| 2459 | |
| 2460 | let client_info = self.clients.get(&window).map(|c| { |
| 2461 | ( |
| 2462 | c.x_position, |
| 2463 | c.y_position, |
| 2464 | c.width, |
| 2465 | c.height, |
| 2466 | c.is_floating, |
| 2467 | c.monitor_index, |
| 2468 | ) |
| 2469 | }); |
| 2470 | |
| 2471 | let Some((orig_x, orig_y, width, height, was_floating, monitor_idx)) = client_info else { |
| 2472 | return Ok(()); |
| 2473 | }; |
| 2474 | |
| 2475 | let monitor = self.monitors.get(monitor_idx).cloned(); |
| 2476 | let Some(monitor) = monitor else { |
| 2477 | return Ok(()); |
| 2478 | }; |
| 2479 | |
| 2480 | let snap = 32; |
| 2481 | let is_normie = self.layout.name() == "normie"; |
| 2482 | |
| 2483 | if !was_floating && !is_normie { |
| 2484 | self.toggle_floating()?; |
| 2485 | } |
| 2486 | |
| 2487 | self.connection |
| 2488 | .grab_pointer( |
| 2489 | false, |
| 2490 | self.root, |
| 2491 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE | EventMask::BUTTON_PRESS) |
| 2492 | .into(), |
| 2493 | GrabMode::ASYNC, |
| 2494 | GrabMode::ASYNC, |
| 2495 | x11rb::NONE, |
| 2496 | x11rb::NONE, |
| 2497 | x11rb::CURRENT_TIME, |
| 2498 | )? |
| 2499 | .reply()?; |
| 2500 | |
| 2501 | let pointer = self.connection.query_pointer(self.root)?.reply()?; |
| 2502 | let (start_x, start_y) = (pointer.root_x as i32, pointer.root_y as i32); |
| 2503 | |
| 2504 | let mut last_time = 0u32; |
| 2505 | |
| 2506 | loop { |
| 2507 | let event = self.connection.wait_for_event()?; |
| 2508 | match event { |
| 2509 | Event::ConfigureRequest(_) | Event::MapRequest(_) | Event::Expose(_) => {} |
| 2510 | Event::MotionNotify(e) => { |
| 2511 | if e.time.wrapping_sub(last_time) <= 16 { |
| 2512 | continue; |
| 2513 | } |
| 2514 | last_time = e.time; |
| 2515 | |
| 2516 | let mut new_x = orig_x as i32 + (e.root_x as i32 - start_x); |
| 2517 | let mut new_y = orig_y as i32 + (e.root_y as i32 - start_y); |
| 2518 | |
| 2519 | if (monitor.window_area_x - new_x).abs() < snap { |
| 2520 | new_x = monitor.window_area_x; |
| 2521 | } else if ((monitor.window_area_x + monitor.window_area_width) |
| 2522 | - (new_x + width as i32)) |
| 2523 | .abs() |
| 2524 | < snap |
| 2525 | { |
| 2526 | new_x = monitor.window_area_x + monitor.window_area_width - width as i32; |
| 2527 | } |
| 2528 | |
| 2529 | if (monitor.window_area_y - new_y).abs() < snap { |
| 2530 | new_y = monitor.window_area_y; |
| 2531 | } else if ((monitor.window_area_y + monitor.window_area_height) |
| 2532 | - (new_y + height as i32)) |
| 2533 | .abs() |
| 2534 | < snap |
| 2535 | { |
| 2536 | new_y = monitor.window_area_y + monitor.window_area_height - height as i32; |
| 2537 | } |
| 2538 | |
| 2539 | let should_resize = is_normie |
| 2540 | || self |
| 2541 | .clients |
| 2542 | .get(&window) |
| 2543 | .map(|c| c.is_floating) |
| 2544 | .unwrap_or(false); |
| 2545 | |
| 2546 | if should_resize { |
| 2547 | if let Some(client) = self.clients.get_mut(&window) { |
| 2548 | client.x_position = new_x as i16; |
| 2549 | client.y_position = new_y as i16; |
| 2550 | } |
| 2551 | |
| 2552 | self.connection.configure_window( |
| 2553 | window, |
| 2554 | &ConfigureWindowAux::new().x(new_x).y(new_y), |
| 2555 | )?; |
| 2556 | self.connection.flush()?; |
| 2557 | } |
| 2558 | } |
| 2559 | Event::ButtonRelease(_) => break, |
| 2560 | _ => {} |
| 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | self.connection |
| 2565 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 2566 | .check()?; |
| 2567 | |
| 2568 | let final_client = self |
| 2569 | .clients |
| 2570 | .get(&window) |
| 2571 | .map(|c| (c.x_position, c.y_position, c.width, c.height)); |
| 2572 | |
| 2573 | if let Some((x, y, w, h)) = final_client { |
| 2574 | let new_monitor = self.get_monitor_for_rect(x as i32, y as i32, w as i32, h as i32); |
| 2575 | if new_monitor != monitor_idx { |
| 2576 | self.move_window_to_monitor(window, new_monitor)?; |
| 2577 | self.selected_monitor = new_monitor; |
| 2578 | self.focus(None)?; |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | if self.config.auto_tile && !was_floating && !is_normie { |
| 2583 | let drop_monitor_idx = self |
| 2584 | .clients |
| 2585 | .get(&window) |
| 2586 | .map(|c| c.monitor_index) |
| 2587 | .unwrap_or(monitor_idx); |
| 2588 | |
| 2589 | if let Some((x, y, w, h)) = final_client { |
| 2590 | let center = (x as i32 + w as i32 / 2, y as i32 + h as i32 / 2); |
| 2591 | if let Some(target) = self.tiled_window_at(window, drop_monitor_idx, center) { |
| 2592 | self.detach(window); |
| 2593 | self.insert_before(window, target, drop_monitor_idx); |
| 2594 | } |
| 2595 | } |
| 2596 | |
| 2597 | self.floating_windows.remove(&window); |
| 2598 | if let Some(client) = self.clients.get_mut(&window) { |
| 2599 | client.is_floating = false; |
| 2600 | } |
| 2601 | self.apply_layout()?; |
| 2602 | } |
| 2603 | |
| 2604 | Ok(()) |
| 2605 | } |
| 2606 | |
| 2607 | fn tiled_window_at( |
| 2608 | &self, |
| 2609 | exclude: Window, |
| 2610 | monitor_idx: usize, |
| 2611 | (px, py): (i32, i32), |
| 2612 | ) -> Option<Window> { |
| 2613 | let monitor = self.monitors.get(monitor_idx)?; |
| 2614 | let tags = monitor.tagset[monitor.selected_tags_index]; |
| 2615 | let mut current = monitor.clients_head; |
| 2616 | |
| 2617 | while let Some(win) = current { |
| 2618 | let c = self.clients.get(&win)?; |
| 2619 | current = c.next; |
| 2620 | |
| 2621 | if win == exclude || c.is_floating || (c.tags & tags) == 0 { |
| 2622 | continue; |
| 2623 | } |
| 2624 | |
| 2625 | let (x, y) = (c.x_position as i32, c.y_position as i32); |
| 2626 | let (w, h) = ( |
| 2627 | c.width as i32 + c.border_width as i32 * 2, |
| 2628 | c.height as i32 + c.border_width as i32 * 2, |
| 2629 | ); |
| 2630 | |
| 2631 | if px >= x && px < x + w && py >= y && py < y + h { |
| 2632 | return Some(win); |
| 2633 | } |
| 2634 | } |
| 2635 | None |
| 2636 | } |
| 2637 | |
| 2638 | fn insert_before(&mut self, window: Window, target: Window, monitor_idx: usize) { |
| 2639 | let Some(monitor) = self.monitors.get_mut(monitor_idx) else { |
| 2640 | return; |
| 2641 | }; |
| 2642 | |
| 2643 | if monitor.clients_head == Some(target) { |
| 2644 | if let Some(c) = self.clients.get_mut(&window) { |
| 2645 | c.next = Some(target); |
| 2646 | } |
| 2647 | monitor.clients_head = Some(window); |
| 2648 | return; |
| 2649 | } |
| 2650 | |
| 2651 | let mut current = monitor.clients_head; |
| 2652 | while let Some(w) = current { |
| 2653 | let Some(c) = self.clients.get(&w) else { break }; |
| 2654 | if c.next != Some(target) { |
| 2655 | current = c.next; |
| 2656 | continue; |
| 2657 | } |
| 2658 | if let Some(prev) = self.clients.get_mut(&w) { |
| 2659 | prev.next = Some(window); |
| 2660 | } |
| 2661 | if let Some(inserted) = self.clients.get_mut(&window) { |
| 2662 | inserted.next = Some(target); |
| 2663 | } |
| 2664 | break; |
| 2665 | } |
| 2666 | } |
| 2667 | |
| 2668 | fn resize_window_with_mouse(&mut self, window: Window) -> WmResult<()> { |
| 2669 | let is_fullscreen = self |
| 2670 | .clients |
| 2671 | .get(&window) |
| 2672 | .map(|c| c.is_fullscreen) |
| 2673 | .unwrap_or(false); |
| 2674 | |
| 2675 | if is_fullscreen { |
| 2676 | return Ok(()); |
| 2677 | } |
| 2678 | |
| 2679 | let client_info = self.clients.get(&window).map(|c| { |
| 2680 | ( |
| 2681 | c.x_position, |
| 2682 | c.y_position, |
| 2683 | c.width, |
| 2684 | c.height, |
| 2685 | c.border_width, |
| 2686 | c.is_floating, |
| 2687 | c.monitor_index, |
| 2688 | ) |
| 2689 | }); |
| 2690 | |
| 2691 | let Some(( |
| 2692 | orig_x, |
| 2693 | orig_y, |
| 2694 | orig_width, |
| 2695 | orig_height, |
| 2696 | border_width, |
| 2697 | was_floating, |
| 2698 | monitor_idx, |
| 2699 | )) = client_info |
| 2700 | else { |
| 2701 | return Ok(()); |
| 2702 | }; |
| 2703 | |
| 2704 | let monitor = match self.monitors.get(monitor_idx) { |
| 2705 | Some(m) => m, |
| 2706 | None => return Ok(()), |
| 2707 | }; |
| 2708 | |
| 2709 | let is_normie = self.layout.name() == "normie"; |
| 2710 | |
| 2711 | if self.config.auto_tile && !was_floating && !is_normie { |
| 2712 | let mut tiled_count = 0; |
| 2713 | let mut current = monitor.clients_head; |
| 2714 | while let Some(w) = current { |
| 2715 | if let Some(c) = self.clients.get(&w) { |
| 2716 | let visible = (c.tags & monitor.tagset[monitor.selected_tags_index]) != 0; |
| 2717 | if visible && !c.is_floating { |
| 2718 | tiled_count += 1; |
| 2719 | } |
| 2720 | current = c.next; |
| 2721 | } else { |
| 2722 | break; |
| 2723 | } |
| 2724 | } |
| 2725 | if tiled_count <= 1 { |
| 2726 | return Ok(()); |
| 2727 | } |
| 2728 | } |
| 2729 | |
| 2730 | if !was_floating && !is_normie { |
| 2731 | self.toggle_floating()?; |
| 2732 | } |
| 2733 | |
| 2734 | self.connection.warp_pointer( |
| 2735 | x11rb::NONE, |
| 2736 | window, |
| 2737 | 0, |
| 2738 | 0, |
| 2739 | 0, |
| 2740 | 0, |
| 2741 | (orig_width + border_width - 1) as i16, |
| 2742 | (orig_height + border_width - 1) as i16, |
| 2743 | )?; |
| 2744 | |
| 2745 | self.connection |
| 2746 | .grab_pointer( |
| 2747 | false, |
| 2748 | self.root, |
| 2749 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE | EventMask::BUTTON_PRESS) |
| 2750 | .into(), |
| 2751 | GrabMode::ASYNC, |
| 2752 | GrabMode::ASYNC, |
| 2753 | x11rb::NONE, |
| 2754 | x11rb::NONE, |
| 2755 | x11rb::CURRENT_TIME, |
| 2756 | )? |
| 2757 | .reply()?; |
| 2758 | |
| 2759 | let mut last_time = 0u32; |
| 2760 | |
| 2761 | loop { |
| 2762 | let event = self.connection.wait_for_event()?; |
| 2763 | match event { |
| 2764 | Event::ConfigureRequest(_) | Event::MapRequest(_) | Event::Expose(_) => {} |
| 2765 | Event::MotionNotify(e) => { |
| 2766 | if e.time.wrapping_sub(last_time) <= 16 { |
| 2767 | continue; |
| 2768 | } |
| 2769 | last_time = e.time; |
| 2770 | |
| 2771 | let new_width = ((e.root_x as i32 - orig_x as i32 - 2 * border_width as i32 |
| 2772 | + 1) |
| 2773 | .max(1)) as u32; |
| 2774 | let new_height = ((e.root_y as i32 - orig_y as i32 - 2 * border_width as i32 |
| 2775 | + 1) |
| 2776 | .max(1)) as u32; |
| 2777 | |
| 2778 | let should_resize = is_normie |
| 2779 | || self |
| 2780 | .clients |
| 2781 | .get(&window) |
| 2782 | .map(|c| c.is_floating) |
| 2783 | .unwrap_or(false); |
| 2784 | |
| 2785 | if should_resize { |
| 2786 | if let Some(client) = self.clients.get(&window).cloned() { |
| 2787 | let (_, _, hint_width, hint_height, _) = self.apply_size_hints( |
| 2788 | window, |
| 2789 | client.x_position as i32, |
| 2790 | client.y_position as i32, |
| 2791 | new_width as i32, |
| 2792 | new_height as i32, |
| 2793 | ); |
| 2794 | |
| 2795 | if let Some(client_mut) = self.clients.get_mut(&window) { |
| 2796 | client_mut.width = hint_width as u16; |
| 2797 | client_mut.height = hint_height as u16; |
| 2798 | } |
| 2799 | |
| 2800 | self.connection.configure_window( |
| 2801 | window, |
| 2802 | &ConfigureWindowAux::new() |
| 2803 | .width(hint_width as u32) |
| 2804 | .height(hint_height as u32), |
| 2805 | )?; |
| 2806 | self.connection.flush()?; |
| 2807 | } |
| 2808 | } |
| 2809 | } |
| 2810 | Event::ButtonRelease(_) => break, |
| 2811 | _ => {} |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | let final_client = self.clients.get(&window).map(|c| (c.width, c.border_width)); |
| 2816 | |
| 2817 | if let Some((w, bw)) = final_client { |
| 2818 | self.connection.warp_pointer( |
| 2819 | x11rb::NONE, |
| 2820 | window, |
| 2821 | 0, |
| 2822 | 0, |
| 2823 | 0, |
| 2824 | 0, |
| 2825 | (w + bw - 1) as i16, |
| 2826 | (w + bw - 1) as i16, |
| 2827 | )?; |
| 2828 | } |
| 2829 | |
| 2830 | self.connection |
| 2831 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 2832 | .check()?; |
| 2833 | |
| 2834 | let final_client_pos = self |
| 2835 | .clients |
| 2836 | .get(&window) |
| 2837 | .map(|c| (c.x_position, c.y_position, c.width, c.height)); |
| 2838 | |
| 2839 | if let Some((x, y, w, h)) = final_client_pos { |
| 2840 | let new_monitor = self.get_monitor_for_rect(x as i32, y as i32, w as i32, h as i32); |
| 2841 | if new_monitor != monitor_idx { |
| 2842 | self.move_window_to_monitor(window, new_monitor)?; |
| 2843 | self.selected_monitor = new_monitor; |
| 2844 | self.focus(None)?; |
| 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | if self.config.auto_tile && !was_floating && !is_normie { |
| 2849 | self.floating_windows.remove(&window); |
| 2850 | if let Some(client) = self.clients.get_mut(&window) { |
| 2851 | client.is_floating = false; |
| 2852 | } |
| 2853 | self.apply_layout()?; |
| 2854 | } |
| 2855 | |
| 2856 | Ok(()) |
| 2857 | } |
| 2858 | |
| 2859 | fn handle_event(&mut self, event: Event) -> WmResult<Option<bool>> { |
| 2860 | match event { |
| 2861 | Event::KeyPress(ref key_event) if key_event.event == self.overlay.window() => { |
| 2862 | if self.overlay.is_visible() { |
| 2863 | if let Err(error) = self.overlay.hide(&self.connection) { |
| 2864 | eprintln!("Failed to hide overlay: {:?}", error); |
| 2865 | } |
| 2866 | } |
| 2867 | return Ok(None); |
| 2868 | } |
| 2869 | Event::ButtonPress(ref button_event) if button_event.event == self.overlay.window() => { |
| 2870 | if self.overlay.is_visible() { |
| 2871 | if let Err(error) = self.overlay.hide(&self.connection) { |
| 2872 | eprintln!("Failed to hide overlay: {:?}", error); |
| 2873 | } |
| 2874 | } |
| 2875 | return Ok(None); |
| 2876 | } |
| 2877 | Event::Expose(ref expose_event) if expose_event.window == self.overlay.window() => { |
| 2878 | if self.overlay.is_visible() { |
| 2879 | if let Err(error) = self.overlay.draw(&self.connection, &self.font) { |
| 2880 | eprintln!("Failed to draw overlay: {:?}", error); |
| 2881 | } |
| 2882 | } |
| 2883 | return Ok(None); |
| 2884 | } |
| 2885 | Event::KeyPress(ref e) if e.event == self.keybind_overlay.window() => { |
| 2886 | if self.keybind_overlay.is_visible() |
| 2887 | && !self.keybind_overlay.should_suppress_input() |
| 2888 | { |
| 2889 | use crate::keyboard::keysyms; |
| 2890 | if let Some(mapping) = &self.keyboard_mapping { |
| 2891 | let keysym = mapping.keycode_to_keysym(e.detail); |
| 2892 | let is_escape = keysym == keysyms::XK_ESCAPE; |
| 2893 | let is_q = keysym == keysyms::XK_Q || keysym == 0x0051; |
| 2894 | if is_escape || is_q { |
| 2895 | if let Err(error) = self.keybind_overlay.hide(&self.connection) { |
| 2896 | eprintln!("Failed to hide keybind overlay: {:?}", error); |
| 2897 | } |
| 2898 | } |
| 2899 | } |
| 2900 | } |
| 2901 | return Ok(None); |
| 2902 | } |
| 2903 | Event::ButtonPress(ref e) if e.event == self.keybind_overlay.window() => { |
| 2904 | self.connection |
| 2905 | .allow_events(Allow::REPLAY_POINTER, e.time)?; |
| 2906 | return Ok(None); |
| 2907 | } |
| 2908 | Event::Expose(ref expose_event) |
| 2909 | if expose_event.window == self.keybind_overlay.window() => |
| 2910 | { |
| 2911 | if self.keybind_overlay.is_visible() { |
| 2912 | if let Err(error) = self.keybind_overlay.draw(&self.connection, &self.font) { |
| 2913 | eprintln!("Failed to draw keybind overlay: {:?}", error); |
| 2914 | } |
| 2915 | } |
| 2916 | return Ok(None); |
| 2917 | } |
| 2918 | Event::MapRequest(event) => { |
| 2919 | let attrs = match self.connection.get_window_attributes(event.window)?.reply() { |
| 2920 | Ok(attrs) => attrs, |
| 2921 | Err(_) => return Ok(None), |
| 2922 | }; |
| 2923 | |
| 2924 | if attrs.override_redirect { |
| 2925 | return Ok(None); |
| 2926 | } |
| 2927 | |
| 2928 | if !self.windows.contains(&event.window) { |
| 2929 | self.manage_window(event.window)?; |
| 2930 | } |
| 2931 | } |
| 2932 | Event::UnmapNotify(event) => { |
| 2933 | if self.windows.contains(&event.window) && self.is_window_visible(event.window) { |
| 2934 | self.remove_window(event.window)?; |
| 2935 | } |
| 2936 | } |
| 2937 | Event::DestroyNotify(event) => { |
| 2938 | if self.windows.contains(&event.window) { |
| 2939 | self.remove_window(event.window)?; |
| 2940 | } |
| 2941 | } |
| 2942 | Event::PropertyNotify(event) => { |
| 2943 | if event.state == Property::DELETE { |
| 2944 | return Ok(None); |
| 2945 | } |
| 2946 | |
| 2947 | if !self.clients.contains_key(&event.window) { |
| 2948 | return Ok(None); |
| 2949 | } |
| 2950 | |
| 2951 | if event.atom == AtomEnum::WM_TRANSIENT_FOR.into() { |
| 2952 | let is_floating = self |
| 2953 | .clients |
| 2954 | .get(&event.window) |
| 2955 | .map(|c| c.is_floating) |
| 2956 | .unwrap_or(false); |
| 2957 | if !is_floating { |
| 2958 | if let Some(parent) = self.get_transient_parent(event.window) { |
| 2959 | if self.clients.contains_key(&parent) { |
| 2960 | if let Some(c) = self.clients.get_mut(&event.window) { |
| 2961 | c.is_floating = true; |
| 2962 | } |
| 2963 | self.floating_windows.insert(event.window); |
| 2964 | self.apply_layout()?; |
| 2965 | } |
| 2966 | } |
| 2967 | } |
| 2968 | } else if event.atom == AtomEnum::WM_NORMAL_HINTS.into() { |
| 2969 | if let Some(c) = self.clients.get_mut(&event.window) { |
| 2970 | c.hints_valid = false; |
| 2971 | } |
| 2972 | } else if event.atom == AtomEnum::WM_HINTS.into() { |
| 2973 | self.update_window_hints(event.window)?; |
| 2974 | self.update_bar()?; |
| 2975 | } |
| 2976 | |
| 2977 | if event.atom == self.atoms.wm_name || event.atom == self.atoms.net_wm_name { |
| 2978 | let _ = self.update_window_title(event.window); |
| 2979 | if self.layout.name() == "tabbed" { |
| 2980 | self.update_tab_bars()?; |
| 2981 | } |
| 2982 | } |
| 2983 | |
| 2984 | if event.atom == self.atoms.net_wm_window_type { |
| 2985 | self.update_window_type(event.window)?; |
| 2986 | } |
| 2987 | } |
| 2988 | Event::EnterNotify(event) => { |
| 2989 | if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL { |
| 2990 | return Ok(None); |
| 2991 | } |
| 2992 | if self.windows.contains(&event.event) { |
| 2993 | if let Some(client) = self.clients.get(&event.event) { |
| 2994 | if client.monitor_index != self.selected_monitor { |
| 2995 | self.selected_monitor = client.monitor_index; |
| 2996 | self.update_bar()?; |
| 2997 | } |
| 2998 | } |
| 2999 | self.focus(Some(event.event))?; |
| 3000 | self.update_tab_bars()?; |
| 3001 | } |
| 3002 | } |
| 3003 | Event::MotionNotify(event) => { |
| 3004 | if event.event != self.root { |
| 3005 | return Ok(None); |
| 3006 | } |
| 3007 | |
| 3008 | if let Some(monitor_index) = |
| 3009 | self.get_monitor_at_point(event.root_x as i32, event.root_y as i32) |
| 3010 | { |
| 3011 | if monitor_index != self.selected_monitor { |
| 3012 | self.selected_monitor = monitor_index; |
| 3013 | self.update_bar()?; |
| 3014 | |
| 3015 | let visible = self.visible_windows_on_monitor(monitor_index); |
| 3016 | if let Some(&win) = visible.first() { |
| 3017 | self.focus(Some(win))?; |
| 3018 | self.update_tab_bars()?; |
| 3019 | } |
| 3020 | } |
| 3021 | } |
| 3022 | } |
| 3023 | Event::KeyPress(event) => { |
| 3024 | let Some(mapping) = &self.keyboard_mapping else { |
| 3025 | return Ok(None); |
| 3026 | }; |
| 3027 | |
| 3028 | let result = keyboard::handle_key_press( |
| 3029 | event, |
| 3030 | &self.config.keybindings, |
| 3031 | &self.keychord_state, |
| 3032 | mapping, |
| 3033 | ); |
| 3034 | |
| 3035 | match result { |
| 3036 | keyboard::handlers::KeychordResult::Completed(action, arg) => { |
| 3037 | self.keychord_state = keyboard::handlers::KeychordState::Idle; |
| 3038 | self.current_key = 0; |
| 3039 | self.grab_keys()?; |
| 3040 | self.update_bar()?; |
| 3041 | |
| 3042 | match action { |
| 3043 | KeyAction::Quit => return Ok(Some(false)), |
| 3044 | KeyAction::Restart => match self.try_reload_config() { |
| 3045 | Ok(()) => { |
| 3046 | self.gaps_enabled = self.config.gaps_enabled; |
| 3047 | self.error_message = None; |
| 3048 | if let Err(error) = self.overlay.hide(&self.connection) { |
| 3049 | eprintln!( |
| 3050 | "Failed to hide overlay after config reload: {:?}", |
| 3051 | error |
| 3052 | ); |
| 3053 | } |
| 3054 | self.apply_layout()?; |
| 3055 | self.update_bar()?; |
| 3056 | } |
| 3057 | Err(err) => { |
| 3058 | eprintln!("Config reload error: {}", err); |
| 3059 | self.error_message = Some(err.clone()); |
| 3060 | let monitor = &self.monitors[self.selected_monitor]; |
| 3061 | let monitor_x = monitor.screen_x as i16; |
| 3062 | let monitor_y = monitor.screen_y as i16; |
| 3063 | let screen_width = monitor.screen_width as u16; |
| 3064 | let screen_height = monitor.screen_height as u16; |
| 3065 | match self.overlay.show_error( |
| 3066 | &self.connection, |
| 3067 | &self.font, |
| 3068 | &err, |
| 3069 | monitor_x, |
| 3070 | monitor_y, |
| 3071 | screen_width, |
| 3072 | screen_height, |
| 3073 | ) { |
| 3074 | Ok(()) => eprintln!("Error modal displayed"), |
| 3075 | Err(e) => eprintln!("Failed to show error modal: {:?}", e), |
| 3076 | } |
| 3077 | } |
| 3078 | }, |
| 3079 | _ => self.handle_key_action(action, &arg)?, |
| 3080 | } |
| 3081 | } |
| 3082 | keyboard::handlers::KeychordResult::InProgress(candidates) => { |
| 3083 | self.current_key += 1; |
| 3084 | self.keychord_state = keyboard::handlers::KeychordState::InProgress { |
| 3085 | candidates: candidates.clone(), |
| 3086 | keys_pressed: self.current_key, |
| 3087 | }; |
| 3088 | self.grab_keys()?; |
| 3089 | self.update_bar()?; |
| 3090 | } |
| 3091 | keyboard::handlers::KeychordResult::Cancelled |
| 3092 | | keyboard::handlers::KeychordResult::None => { |
| 3093 | self.keychord_state = keyboard::handlers::KeychordState::Idle; |
| 3094 | self.current_key = 0; |
| 3095 | self.grab_keys()?; |
| 3096 | self.update_bar()?; |
| 3097 | } |
| 3098 | } |
| 3099 | } |
| 3100 | Event::ButtonPress(event) => { |
| 3101 | if self.keybind_overlay.is_visible() && event.event != self.keybind_overlay.window() |
| 3102 | { |
| 3103 | if let Err(error) = self.keybind_overlay.hide(&self.connection) { |
| 3104 | eprintln!("Failed to hide keybind overlay: {:?}", error); |
| 3105 | } |
| 3106 | } |
| 3107 | |
| 3108 | let is_bar_click = self |
| 3109 | .bars |
| 3110 | .iter() |
| 3111 | .enumerate() |
| 3112 | .find(|(_, bar)| bar.window() == event.event); |
| 3113 | |
| 3114 | if let Some((monitor_index, bar)) = is_bar_click { |
| 3115 | if let Some(tag_index) = bar.handle_click(event.event_x) { |
| 3116 | if monitor_index != self.selected_monitor { |
| 3117 | self.selected_monitor = monitor_index; |
| 3118 | } |
| 3119 | self.view_tag(tag_index)?; |
| 3120 | } |
| 3121 | } else { |
| 3122 | let is_tab_bar_click = self |
| 3123 | .tab_bars |
| 3124 | .iter() |
| 3125 | .enumerate() |
| 3126 | .find(|(_, tab_bar)| tab_bar.window() == event.event); |
| 3127 | |
| 3128 | if let Some((monitor_index, tab_bar)) = is_tab_bar_click { |
| 3129 | if monitor_index != self.selected_monitor { |
| 3130 | self.selected_monitor = monitor_index; |
| 3131 | } |
| 3132 | |
| 3133 | let visible_windows: Vec<(Window, String)> = self |
| 3134 | .windows |
| 3135 | .iter() |
| 3136 | .filter_map(|&window| { |
| 3137 | if let Some(client) = self.clients.get(&window) { |
| 3138 | if client.monitor_index != monitor_index |
| 3139 | || self.floating_windows.contains(&window) |
| 3140 | || self.fullscreen_windows.contains(&window) |
| 3141 | { |
| 3142 | return None; |
| 3143 | } |
| 3144 | let monitor_tags = self |
| 3145 | .monitors |
| 3146 | .get(monitor_index) |
| 3147 | .map(|m| m.tagset[m.selected_tags_index]) |
| 3148 | .unwrap_or(0); |
| 3149 | if (client.tags & monitor_tags) != 0 { |
| 3150 | return Some((window, client.name.clone())); |
| 3151 | } |
| 3152 | } |
| 3153 | None |
| 3154 | }) |
| 3155 | .collect(); |
| 3156 | |
| 3157 | if let Some(clicked_window) = |
| 3158 | tab_bar.get_clicked_window(&visible_windows, event.event_x) |
| 3159 | { |
| 3160 | self.connection.configure_window( |
| 3161 | clicked_window, |
| 3162 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 3163 | )?; |
| 3164 | self.focus(Some(clicked_window))?; |
| 3165 | self.update_tab_bars()?; |
| 3166 | } |
| 3167 | } else if event.child != x11rb::NONE { |
| 3168 | self.focus(Some(event.child))?; |
| 3169 | self.restack()?; |
| 3170 | self.update_tab_bars()?; |
| 3171 | |
| 3172 | let state_clean = u16::from(event.state) |
| 3173 | & !(u16::from(ModMask::LOCK) | u16::from(ModMask::M2)); |
| 3174 | let modkey_held = state_clean & u16::from(self.config.modkey) != 0; |
| 3175 | |
| 3176 | if modkey_held && event.detail == ButtonIndex::M1.into() { |
| 3177 | if self.clients.contains_key(&event.child) { |
| 3178 | self.drag_window(event.child)?; |
| 3179 | } |
| 3180 | self.connection |
| 3181 | .allow_events(Allow::REPLAY_POINTER, event.time)?; |
| 3182 | } else if modkey_held && event.detail == ButtonIndex::M3.into() { |
| 3183 | if self.clients.contains_key(&event.child) { |
| 3184 | self.resize_window_with_mouse(event.child)?; |
| 3185 | } |
| 3186 | self.connection |
| 3187 | .allow_events(Allow::REPLAY_POINTER, event.time)?; |
| 3188 | } else { |
| 3189 | self.connection |
| 3190 | .allow_events(Allow::REPLAY_POINTER, event.time)?; |
| 3191 | } |
| 3192 | } else if self.windows.contains(&event.event) { |
| 3193 | self.focus(Some(event.event))?; |
| 3194 | self.restack()?; |
| 3195 | self.update_tab_bars()?; |
| 3196 | |
| 3197 | let state_clean = u16::from(event.state) |
| 3198 | & !(u16::from(ModMask::LOCK) | u16::from(ModMask::M2)); |
| 3199 | let modkey_held = state_clean & u16::from(self.config.modkey) != 0; |
| 3200 | |
| 3201 | if modkey_held && event.detail == ButtonIndex::M1.into() { |
| 3202 | self.drag_window(event.event)?; |
| 3203 | self.connection |
| 3204 | .allow_events(Allow::REPLAY_POINTER, event.time)?; |
| 3205 | } else if modkey_held && event.detail == ButtonIndex::M3.into() { |
| 3206 | self.resize_window_with_mouse(event.event)?; |
| 3207 | self.connection |
| 3208 | .allow_events(Allow::REPLAY_POINTER, event.time)?; |
| 3209 | } else { |
| 3210 | self.connection |
| 3211 | .allow_events(Allow::REPLAY_POINTER, event.time)?; |
| 3212 | } |
| 3213 | } else { |
| 3214 | self.connection |
| 3215 | .allow_events(Allow::REPLAY_POINTER, event.time)?; |
| 3216 | } |
| 3217 | } |
| 3218 | } |
| 3219 | Event::Expose(event) => { |
| 3220 | for bar in &mut self.bars { |
| 3221 | if event.window == bar.window() { |
| 3222 | bar.invalidate(); |
| 3223 | self.update_bar()?; |
| 3224 | break; |
| 3225 | } |
| 3226 | } |
| 3227 | for _tab_bar in &self.tab_bars { |
| 3228 | if event.window == _tab_bar.window() { |
| 3229 | self.update_tab_bars()?; |
| 3230 | break; |
| 3231 | } |
| 3232 | } |
| 3233 | } |
| 3234 | Event::ConfigureRequest(event) => { |
| 3235 | if let Some(client) = self.clients.get(&event.window) { |
| 3236 | let monitor = &self.monitors[client.monitor_index]; |
| 3237 | let is_floating = client.is_floating; |
| 3238 | let has_layout = self.layout.name() != "normie"; |
| 3239 | |
| 3240 | if event.value_mask.contains(ConfigWindow::BORDER_WIDTH) { |
| 3241 | if let Some(c) = self.clients.get_mut(&event.window) { |
| 3242 | c.border_width = event.border_width; |
| 3243 | } |
| 3244 | } else if is_floating || !has_layout { |
| 3245 | let mut x = client.x_position as i32; |
| 3246 | let mut y = client.y_position as i32; |
| 3247 | let mut w = client.width as i32; |
| 3248 | let mut h = client.height as i32; |
| 3249 | |
| 3250 | if event.value_mask.contains(ConfigWindow::X) { |
| 3251 | if let Some(c) = self.clients.get_mut(&event.window) { |
| 3252 | c.old_x_position = c.x_position; |
| 3253 | } |
| 3254 | x = monitor.screen_x + event.x as i32; |
| 3255 | } |
| 3256 | if event.value_mask.contains(ConfigWindow::Y) { |
| 3257 | if let Some(c) = self.clients.get_mut(&event.window) { |
| 3258 | c.old_y_position = c.y_position; |
| 3259 | } |
| 3260 | y = monitor.screen_y + event.y as i32; |
| 3261 | } |
| 3262 | if event.value_mask.contains(ConfigWindow::WIDTH) { |
| 3263 | if let Some(c) = self.clients.get_mut(&event.window) { |
| 3264 | c.old_width = c.width; |
| 3265 | } |
| 3266 | w = event.width as i32; |
| 3267 | } |
| 3268 | if event.value_mask.contains(ConfigWindow::HEIGHT) { |
| 3269 | if let Some(c) = self.clients.get_mut(&event.window) { |
| 3270 | c.old_height = c.height; |
| 3271 | } |
| 3272 | h = event.height as i32; |
| 3273 | } |
| 3274 | |
| 3275 | let bw = self.config.border_width as i32; |
| 3276 | let width_with_border = w + 2 * bw; |
| 3277 | let height_with_border = h + 2 * bw; |
| 3278 | |
| 3279 | if (x + w) > monitor.screen_x + monitor.screen_width as i32 && is_floating { |
| 3280 | x = monitor.screen_x |
| 3281 | + (monitor.screen_width as i32 / 2 - width_with_border / 2); |
| 3282 | } |
| 3283 | if (y + h) > monitor.screen_y + monitor.screen_height as i32 && is_floating |
| 3284 | { |
| 3285 | y = monitor.screen_y |
| 3286 | + (monitor.screen_height as i32 / 2 - height_with_border / 2); |
| 3287 | } |
| 3288 | |
| 3289 | if let Some(c) = self.clients.get_mut(&event.window) { |
| 3290 | c.x_position = x as i16; |
| 3291 | c.y_position = y as i16; |
| 3292 | c.width = w as u16; |
| 3293 | c.height = h as u16; |
| 3294 | } |
| 3295 | |
| 3296 | let only_position_change = event.value_mask.contains(ConfigWindow::X) |
| 3297 | || event.value_mask.contains(ConfigWindow::Y); |
| 3298 | let no_size_change = !event.value_mask.contains(ConfigWindow::WIDTH) |
| 3299 | && !event.value_mask.contains(ConfigWindow::HEIGHT); |
| 3300 | if only_position_change && no_size_change { |
| 3301 | self.send_configure_notify(event.window)?; |
| 3302 | } |
| 3303 | |
| 3304 | if self.is_visible(event.window) { |
| 3305 | self.connection.configure_window( |
| 3306 | event.window, |
| 3307 | &ConfigureWindowAux::new() |
| 3308 | .x(x) |
| 3309 | .y(y) |
| 3310 | .width(w as u32) |
| 3311 | .height(h as u32), |
| 3312 | )?; |
| 3313 | } |
| 3314 | } else { |
| 3315 | self.send_configure_notify(event.window)?; |
| 3316 | } |
| 3317 | } else { |
| 3318 | let mut aux = ConfigureWindowAux::new(); |
| 3319 | if event.value_mask.contains(ConfigWindow::X) { |
| 3320 | aux = aux.x(event.x as i32); |
| 3321 | } |
| 3322 | if event.value_mask.contains(ConfigWindow::Y) { |
| 3323 | aux = aux.y(event.y as i32); |
| 3324 | } |
| 3325 | if event.value_mask.contains(ConfigWindow::WIDTH) { |
| 3326 | aux = aux.width(event.width as u32); |
| 3327 | } |
| 3328 | if event.value_mask.contains(ConfigWindow::HEIGHT) { |
| 3329 | aux = aux.height(event.height as u32); |
| 3330 | } |
| 3331 | if event.value_mask.contains(ConfigWindow::BORDER_WIDTH) { |
| 3332 | aux = aux.border_width(event.border_width as u32); |
| 3333 | } |
| 3334 | if event.value_mask.contains(ConfigWindow::SIBLING) { |
| 3335 | aux = aux.sibling(event.sibling); |
| 3336 | } |
| 3337 | if event.value_mask.contains(ConfigWindow::STACK_MODE) { |
| 3338 | aux = aux.stack_mode(event.stack_mode); |
| 3339 | } |
| 3340 | self.connection.configure_window(event.window, &aux)?; |
| 3341 | } |
| 3342 | self.connection.flush()?; |
| 3343 | } |
| 3344 | Event::ClientMessage(event) => { |
| 3345 | if !self.clients.contains_key(&event.window) { |
| 3346 | return Ok(None); |
| 3347 | } |
| 3348 | |
| 3349 | if event.type_ == self.atoms.net_wm_state { |
| 3350 | if let Some(data) = event.data.as_data32().get(1) { |
| 3351 | if *data == self.atoms.net_wm_state_fullscreen { |
| 3352 | let action = event.data.as_data32()[0]; |
| 3353 | let fullscreen = match action { |
| 3354 | 1 => true, |
| 3355 | 0 => false, |
| 3356 | 2 => !self.fullscreen_windows.contains(&event.window), |
| 3357 | _ => return Ok(None), |
| 3358 | }; |
| 3359 | self.set_window_fullscreen(event.window, fullscreen)?; |
| 3360 | } |
| 3361 | } |
| 3362 | } else if event.type_ == self.atoms.net_active_window { |
| 3363 | let selected_window = self |
| 3364 | .monitors |
| 3365 | .get(self.selected_monitor) |
| 3366 | .and_then(|m| m.selected_client); |
| 3367 | |
| 3368 | let is_urgent = self |
| 3369 | .clients |
| 3370 | .get(&event.window) |
| 3371 | .map(|c| c.is_urgent) |
| 3372 | .unwrap_or(false); |
| 3373 | |
| 3374 | if Some(event.window) != selected_window && !is_urgent { |
| 3375 | self.set_urgent(event.window, true)?; |
| 3376 | } |
| 3377 | } |
| 3378 | } |
| 3379 | Event::FocusIn(event) => { |
| 3380 | let selected_window = self |
| 3381 | .monitors |
| 3382 | .get(self.selected_monitor) |
| 3383 | .and_then(|m| m.selected_client); |
| 3384 | |
| 3385 | if let Some(sel_win) = selected_window { |
| 3386 | if event.event != sel_win { |
| 3387 | self.set_focus(sel_win)?; |
| 3388 | } |
| 3389 | } |
| 3390 | } |
| 3391 | Event::MappingNotify(event) => { |
| 3392 | if event.request == x11rb::protocol::xproto::Mapping::KEYBOARD { |
| 3393 | self.grab_keys()?; |
| 3394 | } |
| 3395 | } |
| 3396 | Event::ConfigureNotify(event) => { |
| 3397 | if event.window == self.root { |
| 3398 | let old_width = self.screen.width_in_pixels; |
| 3399 | let old_height = self.screen.height_in_pixels; |
| 3400 | |
| 3401 | if event.width != old_width || event.height != old_height { |
| 3402 | self.screen = self.connection.setup().roots[self.screen_number].clone(); |
| 3403 | self.apply_layout()?; |
| 3404 | } |
| 3405 | } |
| 3406 | } |
| 3407 | _ => {} |
| 3408 | } |
| 3409 | Ok(None) |
| 3410 | } |
| 3411 | |
| 3412 | fn apply_layout(&mut self) -> WmResult<()> { |
| 3413 | for monitor_index in 0..self.monitors.len() { |
| 3414 | let stack_head = self.monitors.get(monitor_index).and_then(|m| m.stack_head); |
| 3415 | self.showhide(stack_head)?; |
| 3416 | } |
| 3417 | |
| 3418 | let is_normie = self.layout.name() == LayoutType::Normie.as_str(); |
| 3419 | |
| 3420 | if !is_normie { |
| 3421 | let monitor_count = self.monitors.len(); |
| 3422 | for monitor_index in 0..monitor_count { |
| 3423 | let monitor = &self.monitors[monitor_index]; |
| 3424 | let border_width = self.config.border_width; |
| 3425 | |
| 3426 | let gaps = if self.gaps_enabled { |
| 3427 | GapConfig { |
| 3428 | inner_horizontal: self.config.gap_inner_horizontal, |
| 3429 | inner_vertical: self.config.gap_inner_vertical, |
| 3430 | outer_horizontal: self.config.gap_outer_horizontal, |
| 3431 | outer_vertical: self.config.gap_outer_vertical, |
| 3432 | } |
| 3433 | } else { |
| 3434 | GapConfig { |
| 3435 | inner_horizontal: 0, |
| 3436 | inner_vertical: 0, |
| 3437 | outer_horizontal: 0, |
| 3438 | outer_vertical: 0, |
| 3439 | } |
| 3440 | }; |
| 3441 | |
| 3442 | let monitor_x = monitor.screen_x; |
| 3443 | let monitor_y = monitor.screen_y; |
| 3444 | let monitor_width = monitor.screen_width; |
| 3445 | let monitor_height = monitor.screen_height; |
| 3446 | |
| 3447 | let mut visible: Vec<Window> = Vec::new(); |
| 3448 | let mut current = self.next_tiled(monitor.clients_head, monitor); |
| 3449 | while let Some(window) = current { |
| 3450 | visible.push(window); |
| 3451 | if let Some(client) = self.clients.get(&window) { |
| 3452 | current = self.next_tiled(client.next, monitor); |
| 3453 | } else { |
| 3454 | break; |
| 3455 | } |
| 3456 | } |
| 3457 | |
| 3458 | let bar_height = if self.show_bar { |
| 3459 | self.bars |
| 3460 | .get(monitor_index) |
| 3461 | .map(|bar| bar.height() as u32) |
| 3462 | .unwrap_or(0) |
| 3463 | } else { |
| 3464 | 0 |
| 3465 | }; |
| 3466 | let usable_height = monitor_height.saturating_sub(bar_height as i32); |
| 3467 | let master_factor = monitor.master_factor; |
| 3468 | let num_master = monitor.num_master; |
| 3469 | let smartgaps_enabled = self.config.smartgaps_enabled; |
| 3470 | |
| 3471 | let geometries = self.layout.arrange( |
| 3472 | &visible, |
| 3473 | monitor_width as u32, |
| 3474 | usable_height as u32, |
| 3475 | &gaps, |
| 3476 | master_factor, |
| 3477 | num_master, |
| 3478 | smartgaps_enabled, |
| 3479 | ); |
| 3480 | |
| 3481 | for (window, geometry) in visible.iter().zip(geometries.iter()) { |
| 3482 | let mut adjusted_width = geometry.width.saturating_sub(2 * border_width); |
| 3483 | let mut adjusted_height = geometry.height.saturating_sub(2 * border_width); |
| 3484 | |
| 3485 | if let Some(client) = self.clients.get(window).cloned() { |
| 3486 | if !client.is_floating { |
| 3487 | let (_, _, hint_width, hint_height, _) = self.apply_size_hints( |
| 3488 | *window, |
| 3489 | geometry.x_coordinate as i32, |
| 3490 | geometry.y_coordinate as i32, |
| 3491 | adjusted_width as i32, |
| 3492 | adjusted_height as i32, |
| 3493 | ); |
| 3494 | adjusted_width = hint_width as u32; |
| 3495 | adjusted_height = hint_height as u32; |
| 3496 | } |
| 3497 | } |
| 3498 | |
| 3499 | let adjusted_x = geometry.x_coordinate + monitor_x; |
| 3500 | let adjusted_y = geometry.y_coordinate + monitor_y + bar_height as i32; |
| 3501 | |
| 3502 | if let Some(client) = self.clients.get_mut(window) { |
| 3503 | client.x_position = adjusted_x as i16; |
| 3504 | client.y_position = adjusted_y as i16; |
| 3505 | client.width = adjusted_width as u16; |
| 3506 | client.height = adjusted_height as u16; |
| 3507 | } |
| 3508 | |
| 3509 | self.connection.configure_window( |
| 3510 | *window, |
| 3511 | &ConfigureWindowAux::new() |
| 3512 | .x(adjusted_x) |
| 3513 | .y(adjusted_y) |
| 3514 | .width(adjusted_width) |
| 3515 | .height(adjusted_height) |
| 3516 | .border_width(border_width), |
| 3517 | )?; |
| 3518 | |
| 3519 | if let Some(c) = self.clients.get_mut(window) { |
| 3520 | c.x_position = adjusted_x as i16; |
| 3521 | c.y_position = adjusted_y as i16; |
| 3522 | c.width = adjusted_width as u16; |
| 3523 | c.height = adjusted_height as u16; |
| 3524 | c.border_width = border_width as u16; |
| 3525 | } |
| 3526 | } |
| 3527 | } |
| 3528 | } |
| 3529 | |
| 3530 | for monitor_index in 0..self.monitors.len() { |
| 3531 | let stack_head = self.monitors[monitor_index].stack_head; |
| 3532 | self.showhide(stack_head)?; |
| 3533 | } |
| 3534 | |
| 3535 | self.connection.flush()?; |
| 3536 | |
| 3537 | let is_tabbed = self.layout.name() == LayoutType::Tabbed.as_str(); |
| 3538 | |
| 3539 | if is_tabbed { |
| 3540 | let outer_horizontal = if self.gaps_enabled { |
| 3541 | self.config.gap_outer_horizontal |
| 3542 | } else { |
| 3543 | 0 |
| 3544 | }; |
| 3545 | let outer_vertical = if self.gaps_enabled { |
| 3546 | self.config.gap_outer_vertical |
| 3547 | } else { |
| 3548 | 0 |
| 3549 | }; |
| 3550 | |
| 3551 | for monitor_index in 0..self.tab_bars.len() { |
| 3552 | if let Some(monitor) = self.monitors.get(monitor_index) { |
| 3553 | let bar_height = if self.show_bar { |
| 3554 | self.bars |
| 3555 | .get(monitor_index) |
| 3556 | .map(|bar| bar.height() as f32) |
| 3557 | .unwrap_or(0.0) |
| 3558 | } else { |
| 3559 | 0.0 |
| 3560 | }; |
| 3561 | |
| 3562 | let tab_bar_x = (monitor.screen_x + outer_horizontal as i32) as i16; |
| 3563 | let tab_bar_y = |
| 3564 | (monitor.screen_y as f32 + bar_height + outer_vertical as f32) as i16; |
| 3565 | let tab_bar_width = monitor |
| 3566 | .screen_width |
| 3567 | .saturating_sub(2 * outer_horizontal as i32) |
| 3568 | as u16; |
| 3569 | |
| 3570 | if let Err(e) = self.tab_bars[monitor_index].reposition( |
| 3571 | &self.connection, |
| 3572 | tab_bar_x, |
| 3573 | tab_bar_y, |
| 3574 | tab_bar_width, |
| 3575 | ) { |
| 3576 | eprintln!("Failed to reposition tab bar: {:?}", e); |
| 3577 | } |
| 3578 | } |
| 3579 | } |
| 3580 | } |
| 3581 | |
| 3582 | for monitor_index in 0..self.tab_bars.len() { |
| 3583 | let has_visible_windows = self.windows.iter().any(|&window| { |
| 3584 | if let Some(client) = self.clients.get(&window) { |
| 3585 | if client.monitor_index != monitor_index |
| 3586 | || self.floating_windows.contains(&window) |
| 3587 | || self.fullscreen_windows.contains(&window) |
| 3588 | { |
| 3589 | return false; |
| 3590 | } |
| 3591 | if let Some(monitor) = self.monitors.get(monitor_index) { |
| 3592 | return (client.tags & monitor.tagset[monitor.selected_tags_index]) != 0; |
| 3593 | } |
| 3594 | } |
| 3595 | false |
| 3596 | }); |
| 3597 | |
| 3598 | if is_tabbed && has_visible_windows { |
| 3599 | if let Err(e) = self.tab_bars[monitor_index].show(&self.connection) { |
| 3600 | eprintln!("Failed to show tab bar: {:?}", e); |
| 3601 | } |
| 3602 | } else { |
| 3603 | if let Err(e) = self.tab_bars[monitor_index].hide(&self.connection) { |
| 3604 | eprintln!("Failed to hide tab bar: {:?}", e); |
| 3605 | } |
| 3606 | } |
| 3607 | } |
| 3608 | |
| 3609 | if is_tabbed { |
| 3610 | self.update_tab_bars()?; |
| 3611 | } |
| 3612 | |
| 3613 | Ok(()) |
| 3614 | } |
| 3615 | |
| 3616 | pub fn change_layout<L: Layout + 'static>(&mut self, new_layout: L) -> WmResult<()> { |
| 3617 | self.layout = Box::new(new_layout); |
| 3618 | self.apply_layout()?; |
| 3619 | Ok(()) |
| 3620 | } |
| 3621 | |
| 3622 | fn send_configure_notify(&self, window: Window) -> WmResult<()> { |
| 3623 | let client = self.clients.get(&window); |
| 3624 | let (x, y, w, h, bw) = if let Some(c) = client { |
| 3625 | ( |
| 3626 | c.x_position, |
| 3627 | c.y_position, |
| 3628 | c.width, |
| 3629 | c.height, |
| 3630 | c.border_width, |
| 3631 | ) |
| 3632 | } else { |
| 3633 | let geom = self.connection.get_geometry(window)?.reply()?; |
| 3634 | (geom.x, geom.y, geom.width, geom.height, geom.border_width) |
| 3635 | }; |
| 3636 | |
| 3637 | let event = ConfigureNotifyEvent { |
| 3638 | response_type: CONFIGURE_NOTIFY_EVENT, |
| 3639 | sequence: 0, |
| 3640 | event: window, |
| 3641 | window, |
| 3642 | above_sibling: x11rb::NONE, |
| 3643 | x, |
| 3644 | y, |
| 3645 | width: w, |
| 3646 | height: h, |
| 3647 | border_width: bw, |
| 3648 | override_redirect: false, |
| 3649 | }; |
| 3650 | |
| 3651 | self.connection |
| 3652 | .send_event(false, window, EventMask::STRUCTURE_NOTIFY, event)?; |
| 3653 | |
| 3654 | Ok(()) |
| 3655 | } |
| 3656 | |
| 3657 | fn update_size_hints(&mut self, window: Window) -> WmResult<()> { |
| 3658 | let size_hints = self |
| 3659 | .connection |
| 3660 | .get_property( |
| 3661 | false, |
| 3662 | window, |
| 3663 | x11rb::protocol::xproto::AtomEnum::WM_NORMAL_HINTS, |
| 3664 | x11rb::protocol::xproto::AtomEnum::WM_SIZE_HINTS, |
| 3665 | 0, |
| 3666 | 18, |
| 3667 | )? |
| 3668 | .reply()?; |
| 3669 | |
| 3670 | if size_hints.value.is_empty() { |
| 3671 | if let Some(client) = self.clients.get_mut(&window) { |
| 3672 | client.hints_valid = false; |
| 3673 | } |
| 3674 | return Ok(()); |
| 3675 | } |
| 3676 | |
| 3677 | if size_hints.value.len() < 18 * 4 { |
| 3678 | if let Some(client) = self.clients.get_mut(&window) { |
| 3679 | client.hints_valid = false; |
| 3680 | } |
| 3681 | return Ok(()); |
| 3682 | } |
| 3683 | |
| 3684 | use crate::size_hints::{flags::*, offset::*}; |
| 3685 | |
| 3686 | let read_u32 = |offset: usize| -> u32 { |
| 3687 | let bytes = &size_hints.value[offset * 4..(offset + 1) * 4]; |
| 3688 | u32::from_ne_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]) |
| 3689 | }; |
| 3690 | |
| 3691 | let flags = read_u32(FLAGS); |
| 3692 | |
| 3693 | if let Some(client) = self.clients.get_mut(&window) { |
| 3694 | if flags & P_BASE_SIZE != 0 { |
| 3695 | client.base_width = read_u32(BASE_WIDTH) as i32; |
| 3696 | client.base_height = read_u32(BASE_HEIGHT) as i32; |
| 3697 | } else if flags & P_MIN_SIZE != 0 { |
| 3698 | client.base_width = read_u32(MIN_WIDTH) as i32; |
| 3699 | client.base_height = read_u32(MIN_HEIGHT) as i32; |
| 3700 | } else { |
| 3701 | client.base_width = 0; |
| 3702 | client.base_height = 0; |
| 3703 | } |
| 3704 | |
| 3705 | if flags & P_RESIZE_INC != 0 { |
| 3706 | client.increment_width = read_u32(WIDTH_INC) as i32; |
| 3707 | client.increment_height = read_u32(HEIGHT_INC) as i32; |
| 3708 | } else { |
| 3709 | client.increment_width = 0; |
| 3710 | client.increment_height = 0; |
| 3711 | } |
| 3712 | |
| 3713 | if flags & P_MAX_SIZE != 0 { |
| 3714 | client.max_width = read_u32(MAX_WIDTH) as i32; |
| 3715 | client.max_height = read_u32(MAX_HEIGHT) as i32; |
| 3716 | } else { |
| 3717 | client.max_width = 0; |
| 3718 | client.max_height = 0; |
| 3719 | } |
| 3720 | |
| 3721 | if flags & P_MIN_SIZE != 0 { |
| 3722 | client.min_width = read_u32(MIN_WIDTH) as i32; |
| 3723 | client.min_height = read_u32(MIN_HEIGHT) as i32; |
| 3724 | } else if flags & P_BASE_SIZE != 0 { |
| 3725 | client.min_width = read_u32(BASE_WIDTH) as i32; |
| 3726 | client.min_height = read_u32(BASE_HEIGHT) as i32; |
| 3727 | } else { |
| 3728 | client.min_width = 0; |
| 3729 | client.min_height = 0; |
| 3730 | } |
| 3731 | |
| 3732 | if flags & P_ASPECT != 0 { |
| 3733 | client.min_aspect = |
| 3734 | (read_u32(MIN_ASPECT_Y) as f32) / (read_u32(MIN_ASPECT_X) as f32).max(1.0); |
| 3735 | client.max_aspect = |
| 3736 | (read_u32(MAX_ASPECT_X) as f32) / (read_u32(MAX_ASPECT_Y) as f32).max(1.0); |
| 3737 | } else { |
| 3738 | client.min_aspect = 0.0; |
| 3739 | client.max_aspect = 0.0; |
| 3740 | } |
| 3741 | |
| 3742 | client.is_fixed = client.max_width > 0 |
| 3743 | && client.max_height > 0 |
| 3744 | && client.max_width == client.min_width |
| 3745 | && client.max_height == client.min_height; |
| 3746 | |
| 3747 | client.hints_valid = true; |
| 3748 | } |
| 3749 | Ok(()) |
| 3750 | } |
| 3751 | |
| 3752 | fn update_window_title(&mut self, window: Window) -> WmResult<()> { |
| 3753 | let net_name = self |
| 3754 | .connection |
| 3755 | .get_property( |
| 3756 | false, |
| 3757 | window, |
| 3758 | self.atoms.net_wm_name, |
| 3759 | self.atoms.utf8_string, |
| 3760 | 0, |
| 3761 | 256, |
| 3762 | ) |
| 3763 | .ok() |
| 3764 | .and_then(|cookie| cookie.reply().ok()); |
| 3765 | |
| 3766 | if let Some(name) = net_name { |
| 3767 | if !name.value.is_empty() { |
| 3768 | if let Ok(title) = String::from_utf8(name.value.clone()) { |
| 3769 | if let Some(client) = self.clients.get_mut(&window) { |
| 3770 | client.name = title; |
| 3771 | return Ok(()); |
| 3772 | } |
| 3773 | } |
| 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | let wm_name = self |
| 3778 | .connection |
| 3779 | .get_property( |
| 3780 | false, |
| 3781 | window, |
| 3782 | self.atoms.wm_name, |
| 3783 | x11rb::protocol::xproto::AtomEnum::STRING, |
| 3784 | 0, |
| 3785 | 256, |
| 3786 | )? |
| 3787 | .reply()?; |
| 3788 | |
| 3789 | if !wm_name.value.is_empty() { |
| 3790 | if let Ok(title) = String::from_utf8(wm_name.value.clone()) { |
| 3791 | if let Some(client) = self.clients.get_mut(&window) { |
| 3792 | client.name = title; |
| 3793 | } |
| 3794 | } |
| 3795 | } |
| 3796 | |
| 3797 | Ok(()) |
| 3798 | } |
| 3799 | |
| 3800 | fn update_window_hints(&mut self, window: Window) -> WmResult<()> { |
| 3801 | let hints_reply = self |
| 3802 | .connection |
| 3803 | .get_property(false, window, AtomEnum::WM_HINTS, AtomEnum::WM_HINTS, 0, 9)? |
| 3804 | .reply(); |
| 3805 | |
| 3806 | if let Ok(hints) = hints_reply { |
| 3807 | if hints.value.len() >= 4 { |
| 3808 | let flags = u32::from_ne_bytes([ |
| 3809 | hints.value[0], |
| 3810 | hints.value[1], |
| 3811 | hints.value[2], |
| 3812 | hints.value[3], |
| 3813 | ]); |
| 3814 | |
| 3815 | let selected_window = self |
| 3816 | .monitors |
| 3817 | .get(self.selected_monitor) |
| 3818 | .and_then(|m| m.selected_client); |
| 3819 | |
| 3820 | if Some(window) == selected_window && (flags & 256) != 0 { |
| 3821 | let new_flags = flags & !256; |
| 3822 | let mut new_hints = hints.value.clone(); |
| 3823 | new_hints[0..4].copy_from_slice(&new_flags.to_ne_bytes()); |
| 3824 | |
| 3825 | self.connection.change_property( |
| 3826 | x11rb::protocol::xproto::PropMode::REPLACE, |
| 3827 | window, |
| 3828 | AtomEnum::WM_HINTS, |
| 3829 | AtomEnum::WM_HINTS, |
| 3830 | 32, |
| 3831 | 9, |
| 3832 | &new_hints, |
| 3833 | )?; |
| 3834 | } else { |
| 3835 | if let Some(client) = self.clients.get_mut(&window) { |
| 3836 | client.is_urgent = (flags & 256) != 0; |
| 3837 | } |
| 3838 | } |
| 3839 | |
| 3840 | if hints.value.len() >= 8 && (flags & 1) != 0 { |
| 3841 | let input = i32::from_ne_bytes([ |
| 3842 | hints.value[4], |
| 3843 | hints.value[5], |
| 3844 | hints.value[6], |
| 3845 | hints.value[7], |
| 3846 | ]); |
| 3847 | |
| 3848 | if let Some(client) = self.clients.get_mut(&window) { |
| 3849 | client.never_focus = input == 0; |
| 3850 | } |
| 3851 | } else { |
| 3852 | if let Some(client) = self.clients.get_mut(&window) { |
| 3853 | client.never_focus = false; |
| 3854 | } |
| 3855 | } |
| 3856 | } |
| 3857 | } |
| 3858 | |
| 3859 | Ok(()) |
| 3860 | } |
| 3861 | |
| 3862 | fn update_window_type(&mut self, window: Window) -> WmResult<()> { |
| 3863 | if let Ok(Some(state_atom)) = self.get_window_atom_property(window, self.atoms.net_wm_state) |
| 3864 | { |
| 3865 | if state_atom == self.atoms.net_wm_state_fullscreen { |
| 3866 | self.set_window_fullscreen(window, true)?; |
| 3867 | } |
| 3868 | } |
| 3869 | |
| 3870 | if let Ok(Some(type_atom)) = |
| 3871 | self.get_window_atom_property(window, self.atoms.net_wm_window_type) |
| 3872 | { |
| 3873 | if type_atom == self.atoms.net_wm_window_type_dialog { |
| 3874 | if let Some(client) = self.clients.get_mut(&window) { |
| 3875 | client.is_floating = true; |
| 3876 | } |
| 3877 | self.floating_windows.insert(window); |
| 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | Ok(()) |
| 3882 | } |
| 3883 | |
| 3884 | fn apply_size_hints( |
| 3885 | &mut self, |
| 3886 | window: Window, |
| 3887 | mut x: i32, |
| 3888 | mut y: i32, |
| 3889 | mut w: i32, |
| 3890 | mut h: i32, |
| 3891 | ) -> (i32, i32, i32, i32, bool) { |
| 3892 | let bh = 20; |
| 3893 | |
| 3894 | let ( |
| 3895 | client_x, |
| 3896 | client_y, |
| 3897 | client_w, |
| 3898 | client_h, |
| 3899 | bw, |
| 3900 | monitor_index, |
| 3901 | is_floating, |
| 3902 | mut hints_valid, |
| 3903 | ) = { |
| 3904 | let client = match self.clients.get(&window) { |
| 3905 | Some(c) => c, |
| 3906 | None => return (x, y, w, h, false), |
| 3907 | }; |
| 3908 | ( |
| 3909 | client.x_position as i32, |
| 3910 | client.y_position as i32, |
| 3911 | client.width as i32, |
| 3912 | client.height as i32, |
| 3913 | client.border_width as i32, |
| 3914 | client.monitor_index, |
| 3915 | client.is_floating, |
| 3916 | client.hints_valid, |
| 3917 | ) |
| 3918 | }; |
| 3919 | |
| 3920 | let monitor = &self.monitors[monitor_index]; |
| 3921 | let client_width = client_w + 2 * bw; |
| 3922 | let client_height = client_h + 2 * bw; |
| 3923 | |
| 3924 | w = w.max(1); |
| 3925 | h = h.max(1); |
| 3926 | |
| 3927 | if x >= monitor.window_area_x + monitor.window_area_width { |
| 3928 | x = monitor.window_area_x + monitor.window_area_width - client_width; |
| 3929 | } |
| 3930 | if y >= monitor.window_area_y + monitor.window_area_height { |
| 3931 | y = monitor.window_area_y + monitor.window_area_height - client_height; |
| 3932 | } |
| 3933 | if x + w + 2 * bw <= monitor.window_area_x { |
| 3934 | x = monitor.window_area_x; |
| 3935 | } |
| 3936 | if y + h + 2 * bw <= monitor.window_area_y { |
| 3937 | y = monitor.window_area_y; |
| 3938 | } |
| 3939 | |
| 3940 | if h < bh { |
| 3941 | h = bh; |
| 3942 | } |
| 3943 | if w < bh { |
| 3944 | w = bh; |
| 3945 | } |
| 3946 | |
| 3947 | if is_floating || self.layout.name() == "normie" { |
| 3948 | if !hints_valid { |
| 3949 | let _ = self.update_size_hints(window); |
| 3950 | hints_valid = self |
| 3951 | .clients |
| 3952 | .get(&window) |
| 3953 | .map(|c| c.hints_valid) |
| 3954 | .unwrap_or(false); |
| 3955 | } |
| 3956 | |
| 3957 | if hints_valid { |
| 3958 | let ( |
| 3959 | base_width, |
| 3960 | base_height, |
| 3961 | min_width, |
| 3962 | min_height, |
| 3963 | max_width, |
| 3964 | max_height, |
| 3965 | inc_width, |
| 3966 | inc_height, |
| 3967 | min_aspect, |
| 3968 | max_aspect, |
| 3969 | ) = { |
| 3970 | let client = self.clients.get(&window).unwrap(); |
| 3971 | ( |
| 3972 | client.base_width, |
| 3973 | client.base_height, |
| 3974 | client.min_width, |
| 3975 | client.min_height, |
| 3976 | client.max_width, |
| 3977 | client.max_height, |
| 3978 | client.increment_width, |
| 3979 | client.increment_height, |
| 3980 | client.min_aspect, |
| 3981 | client.max_aspect, |
| 3982 | ) |
| 3983 | }; |
| 3984 | |
| 3985 | let base_is_min = base_width == min_width && base_height == min_height; |
| 3986 | |
| 3987 | if !base_is_min { |
| 3988 | w -= base_width; |
| 3989 | h -= base_height; |
| 3990 | } |
| 3991 | |
| 3992 | if min_aspect > 0.0 && max_aspect > 0.0 { |
| 3993 | if max_aspect < (w as f32 / h as f32) { |
| 3994 | w = (h as f32 * max_aspect + 0.5) as i32; |
| 3995 | } else if min_aspect < (h as f32 / w as f32) { |
| 3996 | h = (w as f32 * min_aspect + 0.5) as i32; |
| 3997 | } |
| 3998 | } |
| 3999 | |
| 4000 | if base_is_min { |
| 4001 | w -= base_width; |
| 4002 | h -= base_height; |
| 4003 | } |
| 4004 | |
| 4005 | if inc_width > 0 { |
| 4006 | w -= w % inc_width; |
| 4007 | } |
| 4008 | if inc_height > 0 { |
| 4009 | h -= h % inc_height; |
| 4010 | } |
| 4011 | |
| 4012 | w = (w + base_width).max(min_width); |
| 4013 | h = (h + base_height).max(min_height); |
| 4014 | |
| 4015 | if max_width > 0 { |
| 4016 | w = w.min(max_width); |
| 4017 | } |
| 4018 | if max_height > 0 { |
| 4019 | h = h.min(max_height); |
| 4020 | } |
| 4021 | } |
| 4022 | } |
| 4023 | |
| 4024 | let changed = x != client_x || y != client_y || w != client_w || h != client_h; |
| 4025 | (x, y, w, h, changed) |
| 4026 | } |
| 4027 | |
| 4028 | fn next_tiled(&self, start: Option<Window>, monitor: &Monitor) -> Option<Window> { |
| 4029 | let mut current = start; |
| 4030 | while let Some(window) = current { |
| 4031 | if let Some(client) = self.clients.get(&window) { |
| 4032 | let visible_tags = client.tags & monitor.tagset[monitor.selected_tags_index]; |
| 4033 | if visible_tags != 0 && !client.is_floating { |
| 4034 | return Some(window); |
| 4035 | } |
| 4036 | current = client.next; |
| 4037 | } else { |
| 4038 | break; |
| 4039 | } |
| 4040 | } |
| 4041 | None |
| 4042 | } |
| 4043 | |
| 4044 | fn next_tagged(&self, start: Option<Window>, tags: u32) -> Option<Window> { |
| 4045 | let mut current = start; |
| 4046 | while let Some(window) = current { |
| 4047 | if let Some(client) = self.clients.get(&window) { |
| 4048 | let visible_on_tags = (client.tags & tags) != 0; |
| 4049 | if !client.is_floating && visible_on_tags { |
| 4050 | return Some(window); |
| 4051 | } |
| 4052 | current = client.next; |
| 4053 | } else { |
| 4054 | break; |
| 4055 | } |
| 4056 | } |
| 4057 | None |
| 4058 | } |
| 4059 | |
| 4060 | fn attach(&mut self, window: Window, monitor_index: usize) { |
| 4061 | if let Some(monitor) = self.monitors.get_mut(monitor_index) { |
| 4062 | if let Some(client) = self.clients.get_mut(&window) { |
| 4063 | client.next = monitor.clients_head; |
| 4064 | monitor.clients_head = Some(window); |
| 4065 | } |
| 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | fn attach_aside(&mut self, window: Window, monitor_index: usize) { |
| 4070 | let monitor = match self.monitors.get(monitor_index) { |
| 4071 | Some(m) => m, |
| 4072 | None => return, |
| 4073 | }; |
| 4074 | |
| 4075 | let new_window_tags = self.clients.get(&window).map(|c| c.tags).unwrap_or(0); |
| 4076 | let first_tagged = self.next_tagged(monitor.clients_head, new_window_tags); |
| 4077 | |
| 4078 | if first_tagged.is_none() { |
| 4079 | self.attach(window, monitor_index); |
| 4080 | return; |
| 4081 | } |
| 4082 | |
| 4083 | if let Some(insert_after_window) = first_tagged { |
| 4084 | if let Some(after_client) = self.clients.get(&insert_after_window) { |
| 4085 | let old_next = after_client.next; |
| 4086 | if let Some(new_client) = self.clients.get_mut(&window) { |
| 4087 | new_client.next = old_next; |
| 4088 | } |
| 4089 | if let Some(after_client_mut) = self.clients.get_mut(&insert_after_window) { |
| 4090 | after_client_mut.next = Some(window); |
| 4091 | } |
| 4092 | } |
| 4093 | } |
| 4094 | } |
| 4095 | |
| 4096 | fn detach(&mut self, window: Window) { |
| 4097 | let monitor_index = self.clients.get(&window).map(|c| c.monitor_index); |
| 4098 | if let Some(monitor_index) = monitor_index { |
| 4099 | if let Some(monitor) = self.monitors.get_mut(monitor_index) { |
| 4100 | if monitor.clients_head == Some(window) { |
| 4101 | if let Some(client) = self.clients.get(&window) { |
| 4102 | monitor.clients_head = client.next; |
| 4103 | } |
| 4104 | } else { |
| 4105 | let mut current = monitor.clients_head; |
| 4106 | while let Some(current_window) = current { |
| 4107 | if let Some(current_client) = self.clients.get(¤t_window) { |
| 4108 | if current_client.next == Some(window) { |
| 4109 | let new_next = self.clients.get(&window).and_then(|c| c.next); |
| 4110 | if let Some(current_client_mut) = |
| 4111 | self.clients.get_mut(¤t_window) |
| 4112 | { |
| 4113 | current_client_mut.next = new_next; |
| 4114 | } |
| 4115 | break; |
| 4116 | } |
| 4117 | current = current_client.next; |
| 4118 | } else { |
| 4119 | break; |
| 4120 | } |
| 4121 | } |
| 4122 | } |
| 4123 | } |
| 4124 | } |
| 4125 | } |
| 4126 | |
| 4127 | fn attach_stack(&mut self, window: Window, monitor_index: usize) { |
| 4128 | if let Some(monitor) = self.monitors.get_mut(monitor_index) { |
| 4129 | if let Some(client) = self.clients.get_mut(&window) { |
| 4130 | client.stack_next = monitor.stack_head; |
| 4131 | monitor.stack_head = Some(window); |
| 4132 | } |
| 4133 | } |
| 4134 | } |
| 4135 | |
| 4136 | fn detach_stack(&mut self, window: Window) { |
| 4137 | let monitor_index = self.clients.get(&window).map(|c| c.monitor_index); |
| 4138 | if let Some(monitor_index) = monitor_index { |
| 4139 | if let Some(monitor) = self.monitors.get_mut(monitor_index) { |
| 4140 | if monitor.stack_head == Some(window) { |
| 4141 | if let Some(client) = self.clients.get(&window) { |
| 4142 | monitor.stack_head = client.stack_next; |
| 4143 | } |
| 4144 | let should_update_selected = monitor.selected_client == Some(window); |
| 4145 | let mut new_selected: Option<Window> = None; |
| 4146 | if should_update_selected { |
| 4147 | let mut stack_current = monitor.stack_head; |
| 4148 | while let Some(stack_window) = stack_current { |
| 4149 | if let Some(stack_client) = self.clients.get(&stack_window) { |
| 4150 | if self.is_window_visible(stack_window) { |
| 4151 | new_selected = Some(stack_window); |
| 4152 | break; |
| 4153 | } |
| 4154 | stack_current = stack_client.stack_next; |
| 4155 | } else { |
| 4156 | break; |
| 4157 | } |
| 4158 | } |
| 4159 | } |
| 4160 | if should_update_selected { |
| 4161 | if let Some(monitor) = self.monitors.get_mut(monitor_index) { |
| 4162 | monitor.selected_client = new_selected; |
| 4163 | } |
| 4164 | } |
| 4165 | } else { |
| 4166 | let mut current = monitor.stack_head; |
| 4167 | while let Some(current_window) = current { |
| 4168 | if let Some(current_client) = self.clients.get(¤t_window) { |
| 4169 | if current_client.stack_next == Some(window) { |
| 4170 | let new_stack_next = |
| 4171 | self.clients.get(&window).and_then(|c| c.stack_next); |
| 4172 | if let Some(current_client_mut) = |
| 4173 | self.clients.get_mut(¤t_window) |
| 4174 | { |
| 4175 | current_client_mut.stack_next = new_stack_next; |
| 4176 | } |
| 4177 | break; |
| 4178 | } |
| 4179 | current = current_client.stack_next; |
| 4180 | } else { |
| 4181 | break; |
| 4182 | } |
| 4183 | } |
| 4184 | } |
| 4185 | } |
| 4186 | } |
| 4187 | } |
| 4188 | |
| 4189 | fn remove_window(&mut self, window: Window) -> WmResult<()> { |
| 4190 | let initial_count = self.windows.len(); |
| 4191 | |
| 4192 | let focused = self |
| 4193 | .monitors |
| 4194 | .get(self.selected_monitor) |
| 4195 | .and_then(|m| m.selected_client); |
| 4196 | |
| 4197 | if self.clients.contains_key(&window) { |
| 4198 | self.detach(window); |
| 4199 | self.detach_stack(window); |
| 4200 | self.clients.remove(&window); |
| 4201 | } |
| 4202 | |
| 4203 | self.windows.retain(|&w| w != window); |
| 4204 | self.floating_windows.remove(&window); |
| 4205 | |
| 4206 | if self.windows.len() < initial_count { |
| 4207 | if focused == Some(window) { |
| 4208 | let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 4209 | if let Some(&new_win) = visible.last() { |
| 4210 | self.focus(Some(new_win))?; |
| 4211 | } else if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 4212 | monitor.selected_client = None; |
| 4213 | } |
| 4214 | } |
| 4215 | |
| 4216 | self.apply_layout()?; |
| 4217 | self.update_bar()?; |
| 4218 | } |
| 4219 | Ok(()) |
| 4220 | } |
| 4221 | |
| 4222 | fn get_selected_monitor(&self) -> &Monitor { |
| 4223 | &self.monitors[self.selected_monitor] |
| 4224 | } |
| 4225 | |
| 4226 | fn has_windows_on_tag(&self, monitor_number: usize, tag_index: usize) -> bool { |
| 4227 | let Some(monitor) = self.monitors.get(monitor_number) else { |
| 4228 | return false; |
| 4229 | }; |
| 4230 | |
| 4231 | let mut current = monitor.clients_head; |
| 4232 | while let Some(window) = current { |
| 4233 | // A window should always have a client attatched to it. |
| 4234 | let Some(client) = self.clients.get(&window) else { |
| 4235 | break; |
| 4236 | }; |
| 4237 | |
| 4238 | if unmask_tag(client.tags) == tag_index { |
| 4239 | return true; |
| 4240 | } |
| 4241 | current = client.next; |
| 4242 | } |
| 4243 | |
| 4244 | false |
| 4245 | } |
| 4246 | |
| 4247 | fn run_autostart_commands(&self) { |
| 4248 | for command in &self.config.autostart { |
| 4249 | crate::signal::spawn_detached(command); |
| 4250 | eprintln!("[autostart] Spawned: {}", command); |
| 4251 | } |
| 4252 | } |
| 4253 | } |