| 1 | use crate::Config; |
| 2 | use crate::bar::Bar; |
| 3 | use crate::errors::WmError; |
| 4 | use crate::keyboard::{self, Arg, KeyAction, handlers}; |
| 5 | use crate::layout::GapConfig; |
| 6 | use crate::layout::tiling::TilingLayout; |
| 7 | use crate::layout::{Layout, LayoutBox, LayoutType, layout_from_str, next_layout}; |
| 8 | use crate::monitor::{Monitor, detect_monitors}; |
| 9 | use crate::overlay::{ErrorOverlay, KeybindOverlay, Overlay}; |
| 10 | use std::collections::HashSet; |
| 11 | use std::process::Command; |
| 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 type TagMask = u32; |
| 20 | pub fn tag_mask(tag: usize) -> TagMask { |
| 21 | 1 << tag |
| 22 | } |
| 23 | |
| 24 | struct AtomCache { |
| 25 | net_current_desktop: Atom, |
| 26 | net_client_info: Atom, |
| 27 | wm_state: Atom, |
| 28 | wm_protocols: Atom, |
| 29 | wm_delete_window: Atom, |
| 30 | net_wm_state: Atom, |
| 31 | net_wm_state_fullscreen: Atom, |
| 32 | } |
| 33 | |
| 34 | impl AtomCache { |
| 35 | fn new(connection: &RustConnection) -> WmResult<Self> { |
| 36 | let net_current_desktop = connection |
| 37 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| 38 | .reply()? |
| 39 | .atom; |
| 40 | |
| 41 | let net_client_info = connection |
| 42 | .intern_atom(false, b"_NET_CLIENT_INFO")? |
| 43 | .reply()? |
| 44 | .atom; |
| 45 | |
| 46 | let wm_state = connection.intern_atom(false, b"WM_STATE")?.reply()?.atom; |
| 47 | |
| 48 | let wm_protocols = connection |
| 49 | .intern_atom(false, b"WM_PROTOCOLS")? |
| 50 | .reply()? |
| 51 | .atom; |
| 52 | |
| 53 | let wm_delete_window = connection |
| 54 | .intern_atom(false, b"WM_DELETE_WINDOW")? |
| 55 | .reply()? |
| 56 | .atom; |
| 57 | |
| 58 | let net_wm_state = connection |
| 59 | .intern_atom(false, b"_NET_WM_STATE")? |
| 60 | .reply()? |
| 61 | .atom; |
| 62 | |
| 63 | let net_wm_state_fullscreen = connection |
| 64 | .intern_atom(false, b"_NET_WM_STATE_FULLSCREEN")? |
| 65 | .reply()? |
| 66 | .atom; |
| 67 | |
| 68 | Ok(Self { |
| 69 | net_current_desktop, |
| 70 | net_client_info, |
| 71 | wm_state, |
| 72 | wm_protocols, |
| 73 | wm_delete_window, |
| 74 | net_wm_state, |
| 75 | net_wm_state_fullscreen, |
| 76 | }) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | pub struct WindowManager { |
| 81 | config: Config, |
| 82 | connection: RustConnection, |
| 83 | screen_number: usize, |
| 84 | root: Window, |
| 85 | screen: Screen, |
| 86 | windows: Vec<Window>, |
| 87 | layout: LayoutBox, |
| 88 | window_tags: std::collections::HashMap<Window, TagMask>, |
| 89 | window_monitor: std::collections::HashMap<Window, usize>, |
| 90 | window_geometries: std::collections::HashMap<Window, (i16, i16, u16, u16)>, |
| 91 | gaps_enabled: bool, |
| 92 | floating_windows: HashSet<Window>, |
| 93 | fullscreen_windows: HashSet<Window>, |
| 94 | bars: Vec<Bar>, |
| 95 | monitors: Vec<Monitor>, |
| 96 | selected_monitor: usize, |
| 97 | atoms: AtomCache, |
| 98 | previous_focused: Option<Window>, |
| 99 | display: *mut x11::xlib::Display, |
| 100 | font: crate::bar::font::Font, |
| 101 | keychord_state: keyboard::handlers::KeychordState, |
| 102 | error_message: Option<String>, |
| 103 | overlay: ErrorOverlay, |
| 104 | keybind_overlay: KeybindOverlay, |
| 105 | } |
| 106 | |
| 107 | type WmResult<T> = Result<T, WmError>; |
| 108 | |
| 109 | impl WindowManager { |
| 110 | pub fn new(config: Config) -> WmResult<Self> { |
| 111 | let (connection, screen_number) = x11rb::connect(None)?; |
| 112 | let root = connection.setup().roots[screen_number].root; |
| 113 | let screen = connection.setup().roots[screen_number].clone(); |
| 114 | |
| 115 | let normal_cursor = CursorHandle::new( |
| 116 | &connection, |
| 117 | screen_number, |
| 118 | &x11rb::resource_manager::new_from_default(&connection)?, |
| 119 | )? |
| 120 | .reply()? |
| 121 | .load_cursor(&connection, "left_ptr")?; |
| 122 | |
| 123 | connection |
| 124 | .change_window_attributes( |
| 125 | root, |
| 126 | &ChangeWindowAttributesAux::new() |
| 127 | .cursor(normal_cursor) |
| 128 | .event_mask( |
| 129 | EventMask::SUBSTRUCTURE_REDIRECT |
| 130 | | EventMask::SUBSTRUCTURE_NOTIFY |
| 131 | | EventMask::PROPERTY_CHANGE |
| 132 | | EventMask::KEY_PRESS |
| 133 | | EventMask::BUTTON_PRESS |
| 134 | | EventMask::POINTER_MOTION, |
| 135 | ), |
| 136 | )? |
| 137 | .check()?; |
| 138 | |
| 139 | connection.grab_button( |
| 140 | false, |
| 141 | root, |
| 142 | EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, |
| 143 | GrabMode::SYNC, |
| 144 | GrabMode::ASYNC, |
| 145 | x11rb::NONE, |
| 146 | x11rb::NONE, |
| 147 | ButtonIndex::M1, |
| 148 | u16::from(config.modkey).into(), |
| 149 | )?; |
| 150 | |
| 151 | connection.grab_button( |
| 152 | false, |
| 153 | root, |
| 154 | EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, |
| 155 | GrabMode::SYNC, |
| 156 | GrabMode::ASYNC, |
| 157 | x11rb::NONE, |
| 158 | x11rb::NONE, |
| 159 | ButtonIndex::M3, |
| 160 | u16::from(config.modkey).into(), |
| 161 | )?; |
| 162 | |
| 163 | let monitors = detect_monitors(&connection, &screen, root)?; |
| 164 | |
| 165 | let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) }; |
| 166 | if display.is_null() { |
| 167 | return Err(WmError::X11(crate::errors::X11Error::DisplayOpenFailed)); |
| 168 | } |
| 169 | |
| 170 | let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?; |
| 171 | |
| 172 | let mut bars = Vec::new(); |
| 173 | for monitor in monitors.iter() { |
| 174 | let bar = Bar::new( |
| 175 | &connection, |
| 176 | &screen, |
| 177 | screen_number, |
| 178 | &config, |
| 179 | display, |
| 180 | &font, |
| 181 | monitor.x as i16, |
| 182 | monitor.y as i16, |
| 183 | monitor.width as u16, |
| 184 | )?; |
| 185 | bars.push(bar); |
| 186 | } |
| 187 | |
| 188 | let gaps_enabled = config.gaps_enabled; |
| 189 | |
| 190 | let atoms = AtomCache::new(&connection)?; |
| 191 | |
| 192 | let overlay = ErrorOverlay::new( |
| 193 | &connection, |
| 194 | &screen, |
| 195 | screen_number, |
| 196 | display, |
| 197 | &font, |
| 198 | screen.width_in_pixels, |
| 199 | )?; |
| 200 | |
| 201 | let keybind_overlay = |
| 202 | KeybindOverlay::new(&connection, &screen, screen_number, display, config.modkey)?; |
| 203 | |
| 204 | let mut window_manager = Self { |
| 205 | config, |
| 206 | connection, |
| 207 | screen_number, |
| 208 | root, |
| 209 | screen, |
| 210 | windows: Vec::new(), |
| 211 | layout: Box::new(TilingLayout), |
| 212 | window_tags: std::collections::HashMap::new(), |
| 213 | window_monitor: std::collections::HashMap::new(), |
| 214 | window_geometries: std::collections::HashMap::new(), |
| 215 | gaps_enabled, |
| 216 | floating_windows: HashSet::new(), |
| 217 | fullscreen_windows: HashSet::new(), |
| 218 | bars, |
| 219 | monitors, |
| 220 | selected_monitor: 0, |
| 221 | atoms, |
| 222 | previous_focused: None, |
| 223 | display, |
| 224 | font, |
| 225 | keychord_state: keyboard::handlers::KeychordState::Idle, |
| 226 | error_message: None, |
| 227 | overlay, |
| 228 | keybind_overlay, |
| 229 | }; |
| 230 | |
| 231 | window_manager.scan_existing_windows()?; |
| 232 | window_manager.update_bar()?; |
| 233 | window_manager.run_autostart_commands()?; |
| 234 | |
| 235 | Ok(window_manager) |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * |
| 240 | * This function is deprecated for now, but will potentially be used in the future. |
| 241 | * |
| 242 | */ |
| 243 | fn _get_saved_selected_tags( |
| 244 | connection: &RustConnection, |
| 245 | root: Window, |
| 246 | tag_count: usize, |
| 247 | ) -> WmResult<TagMask> { |
| 248 | let net_current_desktop = connection |
| 249 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| 250 | .reply()? |
| 251 | .atom; |
| 252 | |
| 253 | match connection |
| 254 | .get_property(false, root, net_current_desktop, AtomEnum::CARDINAL, 0, 1)? |
| 255 | .reply() |
| 256 | { |
| 257 | Ok(prop) if prop.value.len() >= 4 => { |
| 258 | let desktop = u32::from_ne_bytes([ |
| 259 | prop.value[0], |
| 260 | prop.value[1], |
| 261 | prop.value[2], |
| 262 | prop.value[3], |
| 263 | ]); |
| 264 | if desktop < tag_count as u32 { |
| 265 | let mask = tag_mask(desktop as usize); |
| 266 | return Ok(mask); |
| 267 | } |
| 268 | } |
| 269 | Ok(_) => {} |
| 270 | Err(e) => { |
| 271 | eprintln!("No _NET_CURRENT_DESKTOP property ({})", e); |
| 272 | } |
| 273 | } |
| 274 | Ok(tag_mask(0)) |
| 275 | } |
| 276 | |
| 277 | fn try_reload_config(&mut self) -> Result<(), String> { |
| 278 | let config_dir = if let Some(xdg_config) = std::env::var_os("XDG_CONFIG_HOME") { |
| 279 | std::path::PathBuf::from(xdg_config).join("oxwm") |
| 280 | } else if let Some(home) = std::env::var_os("HOME") { |
| 281 | std::path::PathBuf::from(home).join(".config").join("oxwm") |
| 282 | } else { |
| 283 | return Err("Could not find config directory".to_string()); |
| 284 | }; |
| 285 | |
| 286 | let lua_path = config_dir.join("config.lua"); |
| 287 | let ron_path = config_dir.join("config.ron"); |
| 288 | |
| 289 | let config_path = if lua_path.exists() { |
| 290 | lua_path |
| 291 | } else if ron_path.exists() { |
| 292 | ron_path |
| 293 | } else { |
| 294 | return Err("No config file found".to_string()); |
| 295 | }; |
| 296 | |
| 297 | let config_str = std::fs::read_to_string(&config_path) |
| 298 | .map_err(|e| format!("Failed to read config: {}", e))?; |
| 299 | |
| 300 | let is_lua = config_path |
| 301 | .extension() |
| 302 | .and_then(|s| s.to_str()) |
| 303 | .map(|s| s == "lua") |
| 304 | .unwrap_or(false); |
| 305 | |
| 306 | let new_config = if is_lua { |
| 307 | let config_dir = config_path.parent(); |
| 308 | crate::config::parse_lua_config(&config_str, config_dir) |
| 309 | .map_err(|e| format!("Config error: {}", e))? |
| 310 | } else { |
| 311 | crate::config::parse_config(&config_str).map_err(|e| format!("Config error: {}", e))? |
| 312 | }; |
| 313 | |
| 314 | self.config = new_config; |
| 315 | self.error_message = None; |
| 316 | Ok(()) |
| 317 | } |
| 318 | |
| 319 | fn scan_existing_windows(&mut self) -> WmResult<()> { |
| 320 | let tree = self.connection.query_tree(self.root)?.reply()?; |
| 321 | let net_client_info = self.atoms.net_client_info; |
| 322 | let wm_state_atom = self.atoms.wm_state; |
| 323 | |
| 324 | for &window in &tree.children { |
| 325 | if self.bars.iter().any(|bar| bar.window() == window) { |
| 326 | continue; |
| 327 | } |
| 328 | |
| 329 | let Ok(attrs) = self.connection.get_window_attributes(window)?.reply() else { |
| 330 | continue; |
| 331 | }; |
| 332 | |
| 333 | if attrs.override_redirect { |
| 334 | continue; |
| 335 | } |
| 336 | |
| 337 | if attrs.map_state == MapState::VIEWABLE { |
| 338 | let tag = self.get_saved_tag(window, net_client_info)?; |
| 339 | self.windows.push(window); |
| 340 | self.window_tags.insert(window, tag); |
| 341 | self.window_monitor.insert(window, self.selected_monitor); |
| 342 | continue; |
| 343 | } |
| 344 | |
| 345 | if attrs.map_state == MapState::UNMAPPED { |
| 346 | let has_wm_state = self |
| 347 | .connection |
| 348 | .get_property(false, window, wm_state_atom, AtomEnum::ANY, 0, 2)? |
| 349 | .reply() |
| 350 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 351 | |
| 352 | if !has_wm_state { |
| 353 | continue; |
| 354 | } |
| 355 | |
| 356 | let has_wm_class = self |
| 357 | .connection |
| 358 | .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)? |
| 359 | .reply() |
| 360 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 361 | |
| 362 | if has_wm_class { |
| 363 | let tag = self.get_saved_tag(window, net_client_info)?; |
| 364 | self.connection.map_window(window)?; |
| 365 | self.windows.push(window); |
| 366 | self.window_tags.insert(window, tag); |
| 367 | self.window_monitor.insert(window, self.selected_monitor); |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | if let Some(&first) = self.windows.first() { |
| 373 | self.set_focus(first)?; |
| 374 | } |
| 375 | |
| 376 | self.apply_layout()?; |
| 377 | Ok(()) |
| 378 | } |
| 379 | |
| 380 | fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> WmResult<TagMask> { |
| 381 | match self |
| 382 | .connection |
| 383 | .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)? |
| 384 | .reply() |
| 385 | { |
| 386 | Ok(prop) if prop.value.len() >= 4 => { |
| 387 | let tags = u32::from_ne_bytes([ |
| 388 | prop.value[0], |
| 389 | prop.value[1], |
| 390 | prop.value[2], |
| 391 | prop.value[3], |
| 392 | ]); |
| 393 | |
| 394 | if tags != 0 && tags < (1 << self.config.tags.len()) { |
| 395 | return Ok(tags); |
| 396 | } |
| 397 | } |
| 398 | Ok(_) => {} |
| 399 | Err(e) => { |
| 400 | eprintln!("No _NET_CLIENT_INFO property ({})", e); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | Ok(self |
| 405 | .monitors |
| 406 | .get(self.selected_monitor) |
| 407 | .map(|m| m.selected_tags) |
| 408 | .unwrap_or(tag_mask(0))) |
| 409 | } |
| 410 | |
| 411 | fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { |
| 412 | let net_client_info = self.atoms.net_client_info; |
| 413 | |
| 414 | let bytes = tag.to_ne_bytes().to_vec(); |
| 415 | |
| 416 | self.connection.change_property( |
| 417 | PropMode::REPLACE, |
| 418 | window, |
| 419 | net_client_info, |
| 420 | AtomEnum::CARDINAL, |
| 421 | 32, |
| 422 | 1, |
| 423 | &bytes, |
| 424 | )?; |
| 425 | |
| 426 | self.connection.flush()?; |
| 427 | Ok(()) |
| 428 | } |
| 429 | |
| 430 | fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> { |
| 431 | let wm_state_atom = self.atoms.wm_state; |
| 432 | |
| 433 | let data = [state, 0u32]; |
| 434 | let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); |
| 435 | |
| 436 | self.connection.change_property( |
| 437 | PropMode::REPLACE, |
| 438 | window, |
| 439 | wm_state_atom, |
| 440 | wm_state_atom, |
| 441 | 32, |
| 442 | 2, |
| 443 | &bytes, |
| 444 | )?; |
| 445 | |
| 446 | self.connection.flush()?; |
| 447 | Ok(()) |
| 448 | } |
| 449 | |
| 450 | pub fn run(&mut self) -> WmResult<bool> { |
| 451 | println!("oxwm started on display {}", self.screen_number); |
| 452 | |
| 453 | keyboard::setup_keybinds(&self.connection, self.root, &self.config.keybindings)?; |
| 454 | self.update_bar()?; |
| 455 | |
| 456 | loop { |
| 457 | while let Some(event) = self.connection.poll_for_event()? { |
| 458 | if let Some(should_restart) = self.handle_event(event)? { |
| 459 | return Ok(should_restart); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | if let Some(bar) = self.bars.get_mut(self.selected_monitor) { |
| 464 | bar.update_blocks(); |
| 465 | } |
| 466 | |
| 467 | if self.bars.iter().any(|bar| bar.needs_redraw()) { |
| 468 | self.update_bar()?; |
| 469 | } |
| 470 | |
| 471 | if self.overlay.is_visible() && self.overlay.should_auto_dismiss() { |
| 472 | let _ = self.overlay.hide(&self.connection); |
| 473 | } |
| 474 | |
| 475 | std::thread::sleep(std::time::Duration::from_millis(100)); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | fn toggle_floating(&mut self) -> WmResult<()> { |
| 480 | if let Some(focused) = self |
| 481 | .monitors |
| 482 | .get(self.selected_monitor) |
| 483 | .and_then(|m| m.focused_window) |
| 484 | { |
| 485 | if self.floating_windows.contains(&focused) { |
| 486 | self.floating_windows.remove(&focused); |
| 487 | |
| 488 | let selected_tags = self |
| 489 | .monitors |
| 490 | .get(self.selected_monitor) |
| 491 | .map(|m| m.selected_tags) |
| 492 | .unwrap_or(tag_mask(0)); |
| 493 | |
| 494 | self.window_tags.insert(focused, selected_tags); |
| 495 | self.window_monitor.insert(focused, self.selected_monitor); |
| 496 | let _ = self.save_client_tag(focused, selected_tags); |
| 497 | |
| 498 | self.apply_layout()?; |
| 499 | } else { |
| 500 | let float_width = (self.screen.width_in_pixels / 2) as u32; |
| 501 | let float_height = (self.screen.height_in_pixels / 2) as u32; |
| 502 | |
| 503 | let border_width = self.config.border_width; |
| 504 | |
| 505 | let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32; |
| 506 | let center_height = |
| 507 | ((self.screen.height_in_pixels - float_height as u16) / 2) as i32; |
| 508 | |
| 509 | self.connection.configure_window( |
| 510 | focused, |
| 511 | &ConfigureWindowAux::new() |
| 512 | .x(center_width) |
| 513 | .y(center_height) |
| 514 | .width(float_width) |
| 515 | .height(float_height) |
| 516 | .border_width(border_width) |
| 517 | .stack_mode(StackMode::ABOVE), |
| 518 | )?; |
| 519 | |
| 520 | self.floating_windows.insert(focused); |
| 521 | self.apply_layout()?; |
| 522 | self.connection.flush()?; |
| 523 | } |
| 524 | } |
| 525 | Ok(()) |
| 526 | } |
| 527 | |
| 528 | fn smart_move_window(&mut self, direction: i32) -> WmResult<()> { |
| 529 | let focused = match self |
| 530 | .monitors |
| 531 | .get(self.selected_monitor) |
| 532 | .and_then(|m| m.focused_window) |
| 533 | { |
| 534 | Some(win) => win, |
| 535 | None => return Ok(()), |
| 536 | }; |
| 537 | |
| 538 | if !self.floating_windows.contains(&focused) { |
| 539 | let float_width = (self.screen.width_in_pixels / 2) as u32; |
| 540 | let float_height = (self.screen.height_in_pixels / 2) as u32; |
| 541 | let border_width = self.config.border_width; |
| 542 | let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32; |
| 543 | let center_height = ((self.screen.height_in_pixels - float_height as u16) / 2) as i32; |
| 544 | |
| 545 | self.connection.configure_window( |
| 546 | focused, |
| 547 | &ConfigureWindowAux::new() |
| 548 | .x(center_width) |
| 549 | .y(center_height) |
| 550 | .width(float_width) |
| 551 | .height(float_height) |
| 552 | .border_width(border_width) |
| 553 | .stack_mode(StackMode::ABOVE), |
| 554 | )?; |
| 555 | self.floating_windows.insert(focused); |
| 556 | } |
| 557 | |
| 558 | let current_geom = match self.connection.get_geometry(focused)?.reply() { |
| 559 | Ok(geom) => geom, |
| 560 | Err(_) => return Ok(()), |
| 561 | }; |
| 562 | |
| 563 | let c_x = current_geom.x as i32; |
| 564 | let c_y = current_geom.y as i32; |
| 565 | let c_width = current_geom.width as i32; |
| 566 | let c_height = current_geom.height as i32; |
| 567 | |
| 568 | let monitor = match self.monitors.get(self.selected_monitor) { |
| 569 | Some(m) => m, |
| 570 | None => return Ok(()), |
| 571 | }; |
| 572 | |
| 573 | let (gap_ih, gap_iv, gap_oh, gap_ov) = if self.gaps_enabled { |
| 574 | ( |
| 575 | self.config.gap_inner_horizontal as i32, |
| 576 | self.config.gap_inner_vertical as i32, |
| 577 | self.config.gap_outer_horizontal as i32, |
| 578 | self.config.gap_outer_vertical as i32, |
| 579 | ) |
| 580 | } else { |
| 581 | (0, 0, 0, 0) |
| 582 | }; |
| 583 | |
| 584 | let (new_x, new_y) = match direction { |
| 585 | 0 => { |
| 586 | // UP |
| 587 | let mut target = i32::MIN; |
| 588 | let top = c_y; |
| 589 | let mut ny = c_y - (monitor.height as i32 / 4); |
| 590 | |
| 591 | for &other_window in &self.windows { |
| 592 | if other_window == focused { |
| 593 | continue; |
| 594 | } |
| 595 | if !self.floating_windows.contains(&other_window) { |
| 596 | continue; |
| 597 | } |
| 598 | if !self.is_window_visible(other_window) { |
| 599 | continue; |
| 600 | } |
| 601 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 602 | if other_mon != self.selected_monitor { |
| 603 | continue; |
| 604 | } |
| 605 | |
| 606 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 607 | Ok(geom) => geom, |
| 608 | Err(_) => continue, |
| 609 | }; |
| 610 | |
| 611 | let o_x = other_geom.x as i32; |
| 612 | let o_y = other_geom.y as i32; |
| 613 | let o_width = other_geom.width as i32; |
| 614 | let o_height = other_geom.height as i32; |
| 615 | |
| 616 | if c_x + c_width < o_x || c_x > o_x + o_width { |
| 617 | continue; |
| 618 | } |
| 619 | |
| 620 | let bottom = o_y + o_height + gap_iv; |
| 621 | if top > bottom && ny < bottom { |
| 622 | target = target.max(bottom); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | if target != i32::MIN { |
| 627 | ny = target; |
| 628 | } |
| 629 | ny = ny.max(monitor.y as i32 + gap_ov); |
| 630 | (c_x, ny) |
| 631 | } |
| 632 | 1 => { |
| 633 | // DOWN |
| 634 | let mut target = i32::MAX; |
| 635 | let bottom = c_y + c_height; |
| 636 | let mut ny = c_y + (monitor.height as i32 / 4); |
| 637 | |
| 638 | for &other_window in &self.windows { |
| 639 | if other_window == focused { |
| 640 | continue; |
| 641 | } |
| 642 | if !self.floating_windows.contains(&other_window) { |
| 643 | continue; |
| 644 | } |
| 645 | if !self.is_window_visible(other_window) { |
| 646 | continue; |
| 647 | } |
| 648 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 649 | if other_mon != self.selected_monitor { |
| 650 | continue; |
| 651 | } |
| 652 | |
| 653 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 654 | Ok(geom) => geom, |
| 655 | Err(_) => continue, |
| 656 | }; |
| 657 | |
| 658 | let o_x = other_geom.x as i32; |
| 659 | let o_y = other_geom.y as i32; |
| 660 | let o_width = other_geom.width as i32; |
| 661 | |
| 662 | if c_x + c_width < o_x || c_x > o_x + o_width { |
| 663 | continue; |
| 664 | } |
| 665 | |
| 666 | let top = o_y - gap_iv; |
| 667 | if bottom < top && (ny + c_height) > top { |
| 668 | target = target.min(top - c_height); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | if target != i32::MAX { |
| 673 | ny = target; |
| 674 | } |
| 675 | ny = ny.min(monitor.y as i32 + monitor.height as i32 - c_height - gap_ov); |
| 676 | (c_x, ny) |
| 677 | } |
| 678 | 2 => { |
| 679 | // LEFT |
| 680 | let mut target = i32::MIN; |
| 681 | let left = c_x; |
| 682 | let mut nx = c_x - (monitor.width as i32 / 6); |
| 683 | |
| 684 | for &other_window in &self.windows { |
| 685 | if other_window == focused { |
| 686 | continue; |
| 687 | } |
| 688 | if !self.floating_windows.contains(&other_window) { |
| 689 | continue; |
| 690 | } |
| 691 | if !self.is_window_visible(other_window) { |
| 692 | continue; |
| 693 | } |
| 694 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 695 | if other_mon != self.selected_monitor { |
| 696 | continue; |
| 697 | } |
| 698 | |
| 699 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 700 | Ok(geom) => geom, |
| 701 | Err(_) => continue, |
| 702 | }; |
| 703 | |
| 704 | let o_x = other_geom.x as i32; |
| 705 | let o_y = other_geom.y as i32; |
| 706 | let o_width = other_geom.width as i32; |
| 707 | let o_height = other_geom.height as i32; |
| 708 | |
| 709 | if c_y + c_height < o_y || c_y > o_y + o_height { |
| 710 | continue; |
| 711 | } |
| 712 | |
| 713 | let right = o_x + o_width + gap_ih; |
| 714 | if left > right && nx < right { |
| 715 | target = target.max(right); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | if target != i32::MIN { |
| 720 | nx = target; |
| 721 | } |
| 722 | nx = nx.max(monitor.x as i32 + gap_oh); |
| 723 | (nx, c_y) |
| 724 | } |
| 725 | 3 => { |
| 726 | // RIGHT |
| 727 | let mut target = i32::MAX; |
| 728 | let right = c_x + c_width; |
| 729 | let mut nx = c_x + (monitor.width as i32 / 6); |
| 730 | |
| 731 | for &other_window in &self.windows { |
| 732 | if other_window == focused { |
| 733 | continue; |
| 734 | } |
| 735 | if !self.floating_windows.contains(&other_window) { |
| 736 | continue; |
| 737 | } |
| 738 | if !self.is_window_visible(other_window) { |
| 739 | continue; |
| 740 | } |
| 741 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 742 | if other_mon != self.selected_monitor { |
| 743 | continue; |
| 744 | } |
| 745 | |
| 746 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 747 | Ok(geom) => geom, |
| 748 | Err(_) => continue, |
| 749 | }; |
| 750 | |
| 751 | let o_x = other_geom.x as i32; |
| 752 | let o_y = other_geom.y as i32; |
| 753 | let o_height = other_geom.height as i32; |
| 754 | |
| 755 | if c_y + c_height < o_y || c_y > o_y + o_height { |
| 756 | continue; |
| 757 | } |
| 758 | |
| 759 | let left = o_x - gap_ih; |
| 760 | if right < left && (nx + c_width) > left { |
| 761 | target = target.min(left - c_width); |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | if target != i32::MAX { |
| 766 | nx = target; |
| 767 | } |
| 768 | nx = nx.min(monitor.x as i32 + monitor.width as i32 - c_width - gap_oh); |
| 769 | (nx, c_y) |
| 770 | } |
| 771 | _ => return Ok(()), |
| 772 | }; |
| 773 | |
| 774 | self.connection.configure_window( |
| 775 | focused, |
| 776 | &ConfigureWindowAux::new() |
| 777 | .x(new_x) |
| 778 | .y(new_y) |
| 779 | .stack_mode(StackMode::ABOVE), |
| 780 | )?; |
| 781 | |
| 782 | self.connection.flush()?; |
| 783 | Ok(()) |
| 784 | } |
| 785 | |
| 786 | fn exchange_client(&mut self, direction: i32) -> WmResult<()> { |
| 787 | let focused = match self |
| 788 | .monitors |
| 789 | .get(self.selected_monitor) |
| 790 | .and_then(|m| m.focused_window) |
| 791 | { |
| 792 | Some(win) => win, |
| 793 | None => return Ok(()), |
| 794 | }; |
| 795 | |
| 796 | if self.floating_windows.contains(&focused) { |
| 797 | return Ok(()); |
| 798 | } |
| 799 | |
| 800 | let visible = self.visible_windows(); |
| 801 | if visible.len() < 2 { |
| 802 | return Ok(()); |
| 803 | } |
| 804 | |
| 805 | let current_idx = match visible.iter().position(|&w| w == focused) { |
| 806 | Some(idx) => idx, |
| 807 | None => return Ok(()), |
| 808 | }; |
| 809 | |
| 810 | let target_idx = match direction { |
| 811 | 0 | 2 => { |
| 812 | // UP or LEFT - previous in stack |
| 813 | if current_idx == 0 { |
| 814 | visible.len() - 1 |
| 815 | } else { |
| 816 | current_idx - 1 |
| 817 | } |
| 818 | } |
| 819 | 1 | 3 => { |
| 820 | // DOWN or RIGHT - next in stack |
| 821 | (current_idx + 1) % visible.len() |
| 822 | } |
| 823 | _ => return Ok(()), |
| 824 | }; |
| 825 | |
| 826 | let target = visible[target_idx]; |
| 827 | |
| 828 | let focused_pos = self.windows.iter().position(|&w| w == focused); |
| 829 | let target_pos = self.windows.iter().position(|&w| w == target); |
| 830 | |
| 831 | if let (Some(f_pos), Some(t_pos)) = (focused_pos, target_pos) { |
| 832 | self.windows.swap(f_pos, t_pos); |
| 833 | |
| 834 | self.apply_layout()?; |
| 835 | |
| 836 | self.set_focus(focused)?; |
| 837 | |
| 838 | if let Ok(geometry) = self.connection.get_geometry(focused)?.reply() { |
| 839 | self.connection.warp_pointer( |
| 840 | x11rb::NONE, |
| 841 | focused, |
| 842 | 0, |
| 843 | 0, |
| 844 | 0, |
| 845 | 0, |
| 846 | geometry.width as i16 / 2, |
| 847 | geometry.height as i16 / 2, |
| 848 | )?; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | Ok(()) |
| 853 | } |
| 854 | |
| 855 | fn toggle_fullscreen(&mut self) -> WmResult<()> { |
| 856 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 857 | monitor.fullscreen_enabled = !monitor.fullscreen_enabled; |
| 858 | |
| 859 | if let Some(bar) = self.bars.get(self.selected_monitor) { |
| 860 | if monitor.fullscreen_enabled { |
| 861 | self.connection.unmap_window(bar.window())?; |
| 862 | } else { |
| 863 | self.connection.map_window(bar.window())?; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | self.apply_layout()?; |
| 868 | } |
| 869 | Ok(()) |
| 870 | } |
| 871 | |
| 872 | fn get_layout_symbol(&self) -> String { |
| 873 | let layout_name = self.layout.name(); |
| 874 | self.config |
| 875 | .layout_symbols |
| 876 | .iter() |
| 877 | .find(|l| l.name == layout_name) |
| 878 | .map(|l| l.symbol.clone()) |
| 879 | .unwrap_or_else(|| self.layout.symbol().to_string()) |
| 880 | } |
| 881 | |
| 882 | fn get_keychord_indicator(&self) -> Option<String> { |
| 883 | match &self.keychord_state { |
| 884 | keyboard::handlers::KeychordState::Idle => None, |
| 885 | keyboard::handlers::KeychordState::InProgress { |
| 886 | candidates, |
| 887 | keys_pressed, |
| 888 | } => { |
| 889 | if candidates.is_empty() { |
| 890 | return None; |
| 891 | } |
| 892 | |
| 893 | let binding = &self.config.keybindings[candidates[0]]; |
| 894 | let mut indicator = String::new(); |
| 895 | |
| 896 | for (i, key_press) in binding.keys.iter().take(*keys_pressed).enumerate() { |
| 897 | if i > 0 { |
| 898 | indicator.push(' '); |
| 899 | } |
| 900 | |
| 901 | for modifier in &key_press.modifiers { |
| 902 | indicator.push_str(Self::format_modifier(*modifier)); |
| 903 | indicator.push('+'); |
| 904 | } |
| 905 | |
| 906 | indicator.push_str(&keyboard::keysyms::format_keysym(key_press.keysym)); |
| 907 | } |
| 908 | |
| 909 | indicator.push('-'); |
| 910 | Some(indicator) |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | fn format_modifier(modifier: KeyButMask) -> &'static str { |
| 916 | match modifier { |
| 917 | KeyButMask::MOD1 => "Alt", |
| 918 | KeyButMask::MOD4 => "Super", |
| 919 | KeyButMask::SHIFT => "Shift", |
| 920 | KeyButMask::CONTROL => "Ctrl", |
| 921 | _ => "Mod", |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | |
| 926 | fn update_bar(&mut self) -> WmResult<()> { |
| 927 | let layout_symbol = self.get_layout_symbol(); |
| 928 | let keychord_indicator = self.get_keychord_indicator(); |
| 929 | |
| 930 | for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 931 | if let Some(bar) = self.bars.get_mut(monitor_index) { |
| 932 | let mut occupied_tags: TagMask = 0; |
| 933 | for (&window, &tags) in &self.window_tags { |
| 934 | if self.window_monitor.get(&window).copied().unwrap_or(0) == monitor_index { |
| 935 | occupied_tags |= tags; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | let draw_blocks = monitor_index == self.selected_monitor; |
| 940 | bar.invalidate(); |
| 941 | bar.draw( |
| 942 | &self.connection, |
| 943 | &self.font, |
| 944 | self.display, |
| 945 | monitor.selected_tags, |
| 946 | occupied_tags, |
| 947 | draw_blocks, |
| 948 | &layout_symbol, |
| 949 | keychord_indicator.as_deref(), |
| 950 | )?; |
| 951 | } |
| 952 | } |
| 953 | Ok(()) |
| 954 | } |
| 955 | |
| 956 | fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> WmResult<()> { |
| 957 | match action { |
| 958 | KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, |
| 959 | KeyAction::KillClient => { |
| 960 | if let Some(focused) = self |
| 961 | .monitors |
| 962 | .get(self.selected_monitor) |
| 963 | .and_then(|m| m.focused_window) |
| 964 | { |
| 965 | self.kill_client(focused)?; |
| 966 | } |
| 967 | } |
| 968 | KeyAction::ToggleFullScreen => { |
| 969 | // Smart fullscreen: |
| 970 | // - In normie (floating) layout: always fullscreen the focused window |
| 971 | // - With explicitly floating window: fullscreen that window |
| 972 | // - Otherwise: do monitor-level fullscreen (hide bar) |
| 973 | let is_normie_layout = self.layout.name() == "normie"; |
| 974 | |
| 975 | if let Some(focused) = self |
| 976 | .monitors |
| 977 | .get(self.selected_monitor) |
| 978 | .and_then(|m| m.focused_window) |
| 979 | { |
| 980 | if is_normie_layout || self.floating_windows.contains(&focused) { |
| 981 | // In floating layout or window is floating - do per-window fullscreen |
| 982 | let is_fullscreen = self.fullscreen_windows.contains(&focused); |
| 983 | self.set_window_fullscreen(focused, !is_fullscreen)?; |
| 984 | } else { |
| 985 | // Window is tiled - do monitor fullscreen |
| 986 | self.toggle_fullscreen()?; |
| 987 | } |
| 988 | } else { |
| 989 | // No focused window - do monitor fullscreen |
| 990 | self.toggle_fullscreen()?; |
| 991 | } |
| 992 | } |
| 993 | KeyAction::ToggleWindowFullscreen => { |
| 994 | if let Some(focused) = self |
| 995 | .monitors |
| 996 | .get(self.selected_monitor) |
| 997 | .and_then(|m| m.focused_window) |
| 998 | { |
| 999 | let is_fullscreen = self.fullscreen_windows.contains(&focused); |
| 1000 | self.set_window_fullscreen(focused, !is_fullscreen)?; |
| 1001 | } |
| 1002 | } |
| 1003 | KeyAction::ChangeLayout => { |
| 1004 | if let Arg::Str(layout_name) = arg { |
| 1005 | match layout_from_str(layout_name) { |
| 1006 | Ok(layout) => { |
| 1007 | self.layout = layout; |
| 1008 | self.apply_layout()?; |
| 1009 | self.update_bar()?; |
| 1010 | } |
| 1011 | Err(e) => eprintln!("Failed to change layout: {}", e), |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | KeyAction::CycleLayout => { |
| 1016 | let current_name = self.layout.name(); |
| 1017 | let next_name = next_layout(current_name); |
| 1018 | match layout_from_str(next_name) { |
| 1019 | Ok(layout) => { |
| 1020 | self.layout = layout; |
| 1021 | self.apply_layout()?; |
| 1022 | self.update_bar()?; |
| 1023 | } |
| 1024 | Err(e) => eprintln!("Failed to cycle layout: {}", e), |
| 1025 | } |
| 1026 | } |
| 1027 | KeyAction::ToggleFloating => { |
| 1028 | self.toggle_floating()?; |
| 1029 | } |
| 1030 | |
| 1031 | KeyAction::SmartMoveWin => { |
| 1032 | if let Arg::Int(direction) = arg { |
| 1033 | self.smart_move_window(*direction)?; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | KeyAction::ExchangeClient => { |
| 1038 | if let Arg::Int(direction) = arg { |
| 1039 | self.exchange_client(*direction)?; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | KeyAction::FocusStack => { |
| 1044 | if let Arg::Int(direction) = arg { |
| 1045 | self.cycle_focus(*direction)?; |
| 1046 | } |
| 1047 | } |
| 1048 | KeyAction::FocusDirection => { |
| 1049 | if let Arg::Int(direction) = arg { |
| 1050 | self.focus_direction(*direction)?; |
| 1051 | } |
| 1052 | } |
| 1053 | KeyAction::SwapDirection => { |
| 1054 | if let Arg::Int(direction) = arg { |
| 1055 | self.swap_direction(*direction)?; |
| 1056 | } |
| 1057 | } |
| 1058 | KeyAction::Quit | KeyAction::Restart => { |
| 1059 | // Handled in handle_event |
| 1060 | } |
| 1061 | KeyAction::Recompile => { |
| 1062 | match std::process::Command::new("oxwm") |
| 1063 | .arg("--recompile") |
| 1064 | .spawn() |
| 1065 | { |
| 1066 | Ok(_) => eprintln!("Recompiling in background"), |
| 1067 | Err(e) => eprintln!("Failed to spawn recompile: {}", e), |
| 1068 | } |
| 1069 | } |
| 1070 | KeyAction::ViewTag => { |
| 1071 | if let Arg::Int(tag_index) = arg { |
| 1072 | self.view_tag(*tag_index as usize)?; |
| 1073 | } |
| 1074 | } |
| 1075 | KeyAction::MoveToTag => { |
| 1076 | if let Arg::Int(tag_index) = arg { |
| 1077 | self.move_to_tag(*tag_index as usize)?; |
| 1078 | } |
| 1079 | } |
| 1080 | KeyAction::ToggleGaps => { |
| 1081 | self.gaps_enabled = !self.gaps_enabled; |
| 1082 | self.apply_layout()?; |
| 1083 | } |
| 1084 | KeyAction::FocusMonitor => { |
| 1085 | if let Arg::Int(direction) = arg { |
| 1086 | self.focus_monitor(*direction)?; |
| 1087 | } |
| 1088 | } |
| 1089 | KeyAction::ShowKeybindOverlay => { |
| 1090 | let monitor = &self.monitors[self.selected_monitor]; |
| 1091 | self.keybind_overlay.toggle( |
| 1092 | &self.connection, |
| 1093 | &self.font, |
| 1094 | &self.config.keybindings, |
| 1095 | monitor.width as u16, |
| 1096 | monitor.height as u16, |
| 1097 | )?; |
| 1098 | } |
| 1099 | KeyAction::None => {} |
| 1100 | } |
| 1101 | Ok(()) |
| 1102 | } |
| 1103 | |
| 1104 | fn focus_monitor(&mut self, direction: i32) -> WmResult<()> { |
| 1105 | if self.monitors.is_empty() { |
| 1106 | return Ok(()); |
| 1107 | } |
| 1108 | |
| 1109 | let new_monitor = if direction > 0 { |
| 1110 | (self.selected_monitor + 1) % self.monitors.len() |
| 1111 | } else { |
| 1112 | (self.selected_monitor + self.monitors.len() - 1) % self.monitors.len() |
| 1113 | }; |
| 1114 | |
| 1115 | if new_monitor == self.selected_monitor { |
| 1116 | return Ok(()); |
| 1117 | } |
| 1118 | |
| 1119 | self.selected_monitor = new_monitor; |
| 1120 | |
| 1121 | self.update_bar()?; |
| 1122 | |
| 1123 | let visible = self.visible_windows_on_monitor(new_monitor); |
| 1124 | if let Some(&win) = visible.first() { |
| 1125 | self.set_focus(win)?; |
| 1126 | } |
| 1127 | |
| 1128 | Ok(()) |
| 1129 | } |
| 1130 | |
| 1131 | fn is_window_visible(&self, window: Window) -> bool { |
| 1132 | let window_mon = self.window_monitor.get(&window).copied().unwrap_or(0); |
| 1133 | |
| 1134 | if let Some(&tags) = self.window_tags.get(&window) { |
| 1135 | let monitor = self.monitors.get(window_mon); |
| 1136 | let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0); |
| 1137 | (tags & selected_tags) != 0 |
| 1138 | } else { |
| 1139 | false |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | fn visible_windows(&self) -> Vec<Window> { |
| 1144 | self.windows |
| 1145 | .iter() |
| 1146 | .filter(|&&w| self.is_window_visible(w)) |
| 1147 | .copied() |
| 1148 | .collect() |
| 1149 | } |
| 1150 | |
| 1151 | fn visible_windows_on_monitor(&self, monitor_index: usize) -> Vec<Window> { |
| 1152 | self.windows |
| 1153 | .iter() |
| 1154 | .filter(|&&w| { |
| 1155 | let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0); |
| 1156 | if window_mon != monitor_index { |
| 1157 | return false; |
| 1158 | } |
| 1159 | if let Some(&tags) = self.window_tags.get(&w) { |
| 1160 | let monitor = self.monitors.get(monitor_index); |
| 1161 | let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0); |
| 1162 | (tags & selected_tags) != 0 |
| 1163 | } else { |
| 1164 | false |
| 1165 | } |
| 1166 | }) |
| 1167 | .copied() |
| 1168 | .collect() |
| 1169 | } |
| 1170 | |
| 1171 | fn get_monitor_at_point(&self, x: i32, y: i32) -> Option<usize> { |
| 1172 | self.monitors |
| 1173 | .iter() |
| 1174 | .position(|mon| mon.contains_point(x, y)) |
| 1175 | } |
| 1176 | |
| 1177 | // Dwm's g-loaded approach to handling the spam alternating crash. |
| 1178 | fn update_window_visibility(&self) -> WmResult<()> { |
| 1179 | for &window in &self.windows { |
| 1180 | if !self.is_window_visible(window) { |
| 1181 | if let Ok(geom) = self.connection.get_geometry(window)?.reply() { |
| 1182 | self.connection.configure_window( |
| 1183 | window, |
| 1184 | &ConfigureWindowAux::new() |
| 1185 | .x(-(geom.width as i32 * 2)) |
| 1186 | .y(geom.y as i32), |
| 1187 | )?; |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | self.connection.flush()?; |
| 1192 | Ok(()) |
| 1193 | } |
| 1194 | |
| 1195 | pub fn view_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 1196 | if tag_index >= self.config.tags.len() { |
| 1197 | return Ok(()); |
| 1198 | } |
| 1199 | |
| 1200 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 1201 | monitor.selected_tags = tag_mask(tag_index); |
| 1202 | } |
| 1203 | |
| 1204 | self.save_selected_tags()?; |
| 1205 | self.update_window_visibility()?; |
| 1206 | self.apply_layout()?; |
| 1207 | self.update_bar()?; |
| 1208 | |
| 1209 | let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 1210 | if let Some(&win) = visible.first() { |
| 1211 | self.set_focus(win)?; |
| 1212 | } |
| 1213 | |
| 1214 | Ok(()) |
| 1215 | } |
| 1216 | |
| 1217 | fn save_selected_tags(&self) -> WmResult<()> { |
| 1218 | let net_current_desktop = self.atoms.net_current_desktop; |
| 1219 | |
| 1220 | let selected_tags = self |
| 1221 | .monitors |
| 1222 | .get(self.selected_monitor) |
| 1223 | .map(|m| m.selected_tags) |
| 1224 | .unwrap_or(tag_mask(0)); |
| 1225 | let desktop = selected_tags.trailing_zeros(); |
| 1226 | |
| 1227 | let bytes = (desktop as u32).to_ne_bytes(); |
| 1228 | self.connection.change_property( |
| 1229 | PropMode::REPLACE, |
| 1230 | self.root, |
| 1231 | net_current_desktop, |
| 1232 | AtomEnum::CARDINAL, |
| 1233 | 32, |
| 1234 | 1, |
| 1235 | &bytes, |
| 1236 | )?; |
| 1237 | |
| 1238 | self.connection.flush()?; |
| 1239 | Ok(()) |
| 1240 | } |
| 1241 | |
| 1242 | pub fn move_to_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 1243 | if tag_index >= self.config.tags.len() { |
| 1244 | return Ok(()); |
| 1245 | } |
| 1246 | |
| 1247 | if let Some(focused) = self |
| 1248 | .monitors |
| 1249 | .get(self.selected_monitor) |
| 1250 | .and_then(|m| m.focused_window) |
| 1251 | { |
| 1252 | let mask = tag_mask(tag_index); |
| 1253 | self.window_tags.insert(focused, mask); |
| 1254 | |
| 1255 | let _ = self.save_client_tag(focused, mask); |
| 1256 | |
| 1257 | self.update_window_visibility()?; |
| 1258 | self.apply_layout()?; |
| 1259 | self.update_bar()?; |
| 1260 | } |
| 1261 | |
| 1262 | Ok(()) |
| 1263 | } |
| 1264 | |
| 1265 | pub fn cycle_focus(&mut self, direction: i32) -> WmResult<()> { |
| 1266 | let visible = self.visible_windows(); |
| 1267 | |
| 1268 | if visible.is_empty() { |
| 1269 | return Ok(()); |
| 1270 | } |
| 1271 | |
| 1272 | let current = self |
| 1273 | .monitors |
| 1274 | .get(self.selected_monitor) |
| 1275 | .and_then(|m| m.focused_window); |
| 1276 | |
| 1277 | let next_window = if let Some(current) = current { |
| 1278 | if let Some(current_index) = visible.iter().position(|&w| w == current) { |
| 1279 | let next_index = if direction > 0 { |
| 1280 | (current_index + 1) % visible.len() |
| 1281 | } else { |
| 1282 | (current_index + visible.len() - 1) % visible.len() |
| 1283 | }; |
| 1284 | visible[next_index] |
| 1285 | } else { |
| 1286 | visible[0] |
| 1287 | } |
| 1288 | } else { |
| 1289 | visible[0] |
| 1290 | }; |
| 1291 | |
| 1292 | self.set_focus(next_window)?; |
| 1293 | Ok(()) |
| 1294 | } |
| 1295 | |
| 1296 | pub fn focus_direction(&mut self, direction: i32) -> WmResult<()> { |
| 1297 | let focused = match self |
| 1298 | .monitors |
| 1299 | .get(self.selected_monitor) |
| 1300 | .and_then(|m| m.focused_window) |
| 1301 | { |
| 1302 | Some(win) => win, |
| 1303 | None => return Ok(()), |
| 1304 | }; |
| 1305 | |
| 1306 | let visible = self.visible_windows(); |
| 1307 | if visible.len() < 2 { |
| 1308 | return Ok(()); |
| 1309 | } |
| 1310 | |
| 1311 | let focused_geom = match self.connection.get_geometry(focused)?.reply() { |
| 1312 | Ok(geom) => geom, |
| 1313 | Err(_) => return Ok(()), |
| 1314 | }; |
| 1315 | |
| 1316 | let focused_center_x = focused_geom.x + (focused_geom.width as i16 / 2); |
| 1317 | let focused_center_y = focused_geom.y + (focused_geom.height as i16 / 2); |
| 1318 | |
| 1319 | let mut candidates = Vec::new(); |
| 1320 | |
| 1321 | for &window in &visible { |
| 1322 | if window == focused { |
| 1323 | continue; |
| 1324 | } |
| 1325 | |
| 1326 | let geom = match self.connection.get_geometry(window)?.reply() { |
| 1327 | Ok(g) => g, |
| 1328 | Err(_) => continue, |
| 1329 | }; |
| 1330 | |
| 1331 | let center_x = geom.x + (geom.width as i16 / 2); |
| 1332 | let center_y = geom.y + (geom.height as i16 / 2); |
| 1333 | |
| 1334 | let is_valid_direction = match direction { |
| 1335 | 0 => center_y < focused_center_y, |
| 1336 | 1 => center_y > focused_center_y, |
| 1337 | 2 => center_x < focused_center_x, |
| 1338 | 3 => center_x > focused_center_x, |
| 1339 | _ => false, |
| 1340 | }; |
| 1341 | |
| 1342 | if is_valid_direction { |
| 1343 | let dx = (center_x - focused_center_x) as i32; |
| 1344 | let dy = (center_y - focused_center_y) as i32; |
| 1345 | let distance = dx * dx + dy * dy; |
| 1346 | candidates.push((window, distance)); |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | if let Some(&(closest_window, _)) = candidates.iter().min_by_key(|&(_, dist)| dist) { |
| 1351 | self.set_focus(closest_window)?; |
| 1352 | } |
| 1353 | |
| 1354 | Ok(()) |
| 1355 | } |
| 1356 | |
| 1357 | pub fn swap_direction(&mut self, direction: i32) -> WmResult<()> { |
| 1358 | let focused = match self |
| 1359 | .monitors |
| 1360 | .get(self.selected_monitor) |
| 1361 | .and_then(|m| m.focused_window) |
| 1362 | { |
| 1363 | Some(win) => win, |
| 1364 | None => return Ok(()), |
| 1365 | }; |
| 1366 | |
| 1367 | let visible = self.visible_windows(); |
| 1368 | if visible.len() < 2 { |
| 1369 | return Ok(()); |
| 1370 | } |
| 1371 | |
| 1372 | let focused_geom = match self.connection.get_geometry(focused)?.reply() { |
| 1373 | Ok(geom) => geom, |
| 1374 | Err(_) => return Ok(()), |
| 1375 | }; |
| 1376 | |
| 1377 | let focused_center_x = focused_geom.x + (focused_geom.width as i16 / 2); |
| 1378 | let focused_center_y = focused_geom.y + (focused_geom.height as i16 / 2); |
| 1379 | |
| 1380 | let mut candidates = Vec::new(); |
| 1381 | |
| 1382 | for &window in &visible { |
| 1383 | if window == focused { |
| 1384 | continue; |
| 1385 | } |
| 1386 | |
| 1387 | let geom = match self.connection.get_geometry(window)?.reply() { |
| 1388 | Ok(g) => g, |
| 1389 | Err(_) => continue, |
| 1390 | }; |
| 1391 | |
| 1392 | let center_x = geom.x + (geom.width as i16 / 2); |
| 1393 | let center_y = geom.y + (geom.height as i16 / 2); |
| 1394 | |
| 1395 | let is_valid_direction = match direction { |
| 1396 | 0 => center_y < focused_center_y, |
| 1397 | 1 => center_y > focused_center_y, |
| 1398 | 2 => center_x < focused_center_x, |
| 1399 | 3 => center_x > focused_center_x, |
| 1400 | _ => false, |
| 1401 | }; |
| 1402 | |
| 1403 | if is_valid_direction { |
| 1404 | let dx = (center_x - focused_center_x) as i32; |
| 1405 | let dy = (center_y - focused_center_y) as i32; |
| 1406 | let distance = dx * dx + dy * dy; |
| 1407 | candidates.push((window, distance)); |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | if let Some(&(target_window, _)) = candidates.iter().min_by_key(|&(_, dist)| dist) { |
| 1412 | let focused_pos = self.windows.iter().position(|&w| w == focused); |
| 1413 | let target_pos = self.windows.iter().position(|&w| w == target_window); |
| 1414 | |
| 1415 | if let (Some(f_pos), Some(t_pos)) = (focused_pos, target_pos) { |
| 1416 | self.windows.swap(f_pos, t_pos); |
| 1417 | self.apply_layout()?; |
| 1418 | self.set_focus(focused)?; |
| 1419 | |
| 1420 | if let Ok(geometry) = self.connection.get_geometry(focused)?.reply() { |
| 1421 | self.connection.warp_pointer( |
| 1422 | x11rb::NONE, |
| 1423 | focused, |
| 1424 | 0, |
| 1425 | 0, |
| 1426 | 0, |
| 1427 | 0, |
| 1428 | geometry.width as i16 / 2, |
| 1429 | geometry.height as i16 / 2, |
| 1430 | )?; |
| 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | Ok(()) |
| 1436 | } |
| 1437 | |
| 1438 | fn grab_next_keys(&self, candidates: &[usize], keys_pressed: usize) -> WmResult<()> { |
| 1439 | use std::collections::HashMap; |
| 1440 | use x11rb::protocol::xproto::Keycode; |
| 1441 | |
| 1442 | let setup = self.connection.setup(); |
| 1443 | let min_keycode = setup.min_keycode; |
| 1444 | let max_keycode = setup.max_keycode; |
| 1445 | |
| 1446 | let keyboard_mapping = self |
| 1447 | .connection |
| 1448 | .get_keyboard_mapping(min_keycode, max_keycode - min_keycode + 1)? |
| 1449 | .reply()?; |
| 1450 | |
| 1451 | let mut keysym_to_keycode: HashMap<keyboard::keysyms::Keysym, Vec<Keycode>> = |
| 1452 | HashMap::new(); |
| 1453 | let keysyms_per_keycode = keyboard_mapping.keysyms_per_keycode; |
| 1454 | |
| 1455 | for keycode in min_keycode..=max_keycode { |
| 1456 | let index = (keycode - min_keycode) as usize * keysyms_per_keycode as usize; |
| 1457 | for i in 0..keysyms_per_keycode as usize { |
| 1458 | if let Some(&keysym) = keyboard_mapping.keysyms.get(index + i) { |
| 1459 | if keysym != 0 { |
| 1460 | keysym_to_keycode |
| 1461 | .entry(keysym) |
| 1462 | .or_insert_with(Vec::new) |
| 1463 | .push(keycode); |
| 1464 | } |
| 1465 | } |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | let mut grabbed_keys: HashSet<(u16, Keycode)> = HashSet::new(); |
| 1470 | |
| 1471 | for &candidate_index in candidates { |
| 1472 | let binding = &self.config.keybindings[candidate_index]; |
| 1473 | if keys_pressed < binding.keys.len() { |
| 1474 | let next_key = &binding.keys[keys_pressed]; |
| 1475 | let modifier_mask = keyboard::handlers::modifiers_to_mask(&next_key.modifiers); |
| 1476 | |
| 1477 | if let Some(keycodes) = keysym_to_keycode.get(&next_key.keysym) { |
| 1478 | if let Some(&keycode) = keycodes.first() { |
| 1479 | let key_tuple = (modifier_mask, keycode); |
| 1480 | |
| 1481 | if grabbed_keys.insert(key_tuple) { |
| 1482 | self.connection.grab_key( |
| 1483 | false, |
| 1484 | self.root, |
| 1485 | modifier_mask.into(), |
| 1486 | keycode, |
| 1487 | GrabMode::ASYNC, |
| 1488 | GrabMode::ASYNC, |
| 1489 | )?; |
| 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | } |
| 1494 | } |
| 1495 | |
| 1496 | if let Some(keycodes) = keysym_to_keycode.get(&keyboard::keysyms::XK_ESCAPE) { |
| 1497 | if let Some(&keycode) = keycodes.first() { |
| 1498 | self.connection.grab_key( |
| 1499 | false, |
| 1500 | self.root, |
| 1501 | ModMask::from(0u16), |
| 1502 | keycode, |
| 1503 | GrabMode::ASYNC, |
| 1504 | GrabMode::ASYNC, |
| 1505 | )?; |
| 1506 | } |
| 1507 | } |
| 1508 | |
| 1509 | self.connection.flush()?; |
| 1510 | Ok(()) |
| 1511 | } |
| 1512 | |
| 1513 | fn ungrab_chord_keys(&self) -> WmResult<()> { |
| 1514 | self.connection |
| 1515 | .ungrab_key(x11rb::protocol::xproto::Grab::ANY, self.root, ModMask::ANY)?; |
| 1516 | keyboard::setup_keybinds(&self.connection, self.root, &self.config.keybindings)?; |
| 1517 | self.connection.flush()?; |
| 1518 | Ok(()) |
| 1519 | } |
| 1520 | |
| 1521 | fn kill_client(&self, window: Window) -> WmResult<()> { |
| 1522 | // Try to send WM_DELETE_WINDOW message first (graceful close) |
| 1523 | if self.send_event(window, self.atoms.wm_delete_window)? { |
| 1524 | // Window supports WM_DELETE_WINDOW, it will close gracefully |
| 1525 | self.connection.flush()?; |
| 1526 | } else { |
| 1527 | // Window doesn't support WM_DELETE_WINDOW, forcefully kill it |
| 1528 | eprintln!("Window {} doesn't support WM_DELETE_WINDOW, killing forcefully", window); |
| 1529 | self.connection.kill_client(window)?; |
| 1530 | self.connection.flush()?; |
| 1531 | } |
| 1532 | Ok(()) |
| 1533 | } |
| 1534 | |
| 1535 | fn send_event(&self, window: Window, protocol: Atom) -> WmResult<bool> { |
| 1536 | // Check if the window supports this protocol |
| 1537 | let protocols_reply = self.connection.get_property( |
| 1538 | false, |
| 1539 | window, |
| 1540 | self.atoms.wm_protocols, |
| 1541 | AtomEnum::ATOM, |
| 1542 | 0, |
| 1543 | 100, |
| 1544 | )?.reply(); |
| 1545 | |
| 1546 | let protocols_reply = match protocols_reply { |
| 1547 | Ok(reply) => reply, |
| 1548 | Err(_) => return Ok(false), |
| 1549 | }; |
| 1550 | |
| 1551 | let protocols: Vec<Atom> = protocols_reply |
| 1552 | .value |
| 1553 | .chunks_exact(4) |
| 1554 | .map(|chunk| u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]])) |
| 1555 | .collect(); |
| 1556 | |
| 1557 | if !protocols.contains(&protocol) { |
| 1558 | return Ok(false); |
| 1559 | } |
| 1560 | |
| 1561 | // Send a ClientMessage event |
| 1562 | let event = x11rb::protocol::xproto::ClientMessageEvent { |
| 1563 | response_type: x11rb::protocol::xproto::CLIENT_MESSAGE_EVENT, |
| 1564 | format: 32, |
| 1565 | sequence: 0, |
| 1566 | window, |
| 1567 | type_: self.atoms.wm_protocols, |
| 1568 | data: x11rb::protocol::xproto::ClientMessageData::from([protocol, x11rb::CURRENT_TIME, 0, 0, 0]), |
| 1569 | }; |
| 1570 | |
| 1571 | self.connection.send_event( |
| 1572 | false, |
| 1573 | window, |
| 1574 | EventMask::NO_EVENT, |
| 1575 | event, |
| 1576 | )?; |
| 1577 | self.connection.flush()?; |
| 1578 | Ok(true) |
| 1579 | } |
| 1580 | |
| 1581 | fn set_window_fullscreen(&mut self, window: Window, fullscreen: bool) -> WmResult<()> { |
| 1582 | if fullscreen && !self.fullscreen_windows.contains(&window) { |
| 1583 | // Save current geometry before fullscreen |
| 1584 | if let Ok(geom) = self.connection.get_geometry(window)?.reply() { |
| 1585 | self.window_geometries.insert(window, (geom.x, geom.y, geom.width, geom.height)); |
| 1586 | } |
| 1587 | |
| 1588 | // Set the _NET_WM_STATE property to fullscreen |
| 1589 | self.connection.change_property( |
| 1590 | PropMode::REPLACE, |
| 1591 | window, |
| 1592 | self.atoms.net_wm_state, |
| 1593 | AtomEnum::ATOM, |
| 1594 | 32, |
| 1595 | 1, |
| 1596 | &self.atoms.net_wm_state_fullscreen.to_ne_bytes(), |
| 1597 | )?; |
| 1598 | |
| 1599 | self.fullscreen_windows.insert(window); |
| 1600 | self.floating_windows.insert(window); |
| 1601 | |
| 1602 | // Get monitor dimensions |
| 1603 | let window_mon = self.window_monitor.get(&window).copied().unwrap_or(self.selected_monitor); |
| 1604 | let monitor = &self.monitors[window_mon]; |
| 1605 | |
| 1606 | // Make window fullscreen across the entire monitor |
| 1607 | self.connection.configure_window( |
| 1608 | window, |
| 1609 | &ConfigureWindowAux::new() |
| 1610 | .x(monitor.x) |
| 1611 | .y(monitor.y) |
| 1612 | .width(monitor.width) |
| 1613 | .height(monitor.height) |
| 1614 | .border_width(0) |
| 1615 | .stack_mode(StackMode::ABOVE), |
| 1616 | )?; |
| 1617 | |
| 1618 | self.connection.flush()?; |
| 1619 | } else if !fullscreen && self.fullscreen_windows.contains(&window) { |
| 1620 | // Clear the _NET_WM_STATE property |
| 1621 | self.connection.delete_property(window, self.atoms.net_wm_state)?; |
| 1622 | |
| 1623 | self.fullscreen_windows.remove(&window); |
| 1624 | |
| 1625 | // Restore saved geometry |
| 1626 | if let Some((x, y, width, height)) = self.window_geometries.get(&window) { |
| 1627 | self.connection.configure_window( |
| 1628 | window, |
| 1629 | &ConfigureWindowAux::new() |
| 1630 | .x(*x as i32) |
| 1631 | .y(*y as i32) |
| 1632 | .width(*width as u32) |
| 1633 | .height(*height as u32) |
| 1634 | .border_width(self.config.border_width), |
| 1635 | )?; |
| 1636 | } |
| 1637 | |
| 1638 | self.apply_layout()?; |
| 1639 | self.connection.flush()?; |
| 1640 | } |
| 1641 | Ok(()) |
| 1642 | } |
| 1643 | |
| 1644 | fn is_transient_window(&self, window: Window) -> bool { |
| 1645 | // Check if window is transient (e.g., dialog, modal) |
| 1646 | let transient_for = self.connection |
| 1647 | .get_property( |
| 1648 | false, |
| 1649 | window, |
| 1650 | AtomEnum::WM_TRANSIENT_FOR, |
| 1651 | AtomEnum::WINDOW, |
| 1652 | 0, |
| 1653 | 1, |
| 1654 | ) |
| 1655 | .ok() |
| 1656 | .and_then(|cookie| cookie.reply().ok()) |
| 1657 | .filter(|reply| !reply.value.is_empty()); |
| 1658 | |
| 1659 | transient_for.is_some() |
| 1660 | } |
| 1661 | |
| 1662 | pub fn set_focus(&mut self, window: Window) -> WmResult<()> { |
| 1663 | let old_focused = self.previous_focused; |
| 1664 | |
| 1665 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 1666 | monitor.focused_window = Some(window); |
| 1667 | } |
| 1668 | |
| 1669 | self.connection |
| 1670 | .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?; |
| 1671 | self.connection.flush()?; |
| 1672 | |
| 1673 | self.update_focus_visuals(old_focused, window)?; |
| 1674 | self.previous_focused = Some(window); |
| 1675 | Ok(()) |
| 1676 | } |
| 1677 | |
| 1678 | fn update_focus_visuals( |
| 1679 | &self, |
| 1680 | old_focused: Option<Window>, |
| 1681 | new_focused: Window, |
| 1682 | ) -> WmResult<()> { |
| 1683 | if let Some(old_win) = old_focused { |
| 1684 | if old_win != new_focused { |
| 1685 | self.connection.configure_window( |
| 1686 | old_win, |
| 1687 | &ConfigureWindowAux::new().border_width(self.config.border_width), |
| 1688 | )?; |
| 1689 | |
| 1690 | self.connection.change_window_attributes( |
| 1691 | old_win, |
| 1692 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), |
| 1693 | )?; |
| 1694 | } |
| 1695 | } |
| 1696 | |
| 1697 | self.connection.configure_window( |
| 1698 | new_focused, |
| 1699 | &ConfigureWindowAux::new().border_width(self.config.border_width), |
| 1700 | )?; |
| 1701 | |
| 1702 | self.connection.change_window_attributes( |
| 1703 | new_focused, |
| 1704 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused), |
| 1705 | )?; |
| 1706 | |
| 1707 | self.connection.flush()?; |
| 1708 | Ok(()) |
| 1709 | } |
| 1710 | |
| 1711 | fn move_mouse(&mut self, window: Window) -> WmResult<()> { |
| 1712 | self.floating_windows.insert(window); |
| 1713 | |
| 1714 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 1715 | |
| 1716 | self.connection |
| 1717 | .grab_pointer( |
| 1718 | false, |
| 1719 | self.root, |
| 1720 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), |
| 1721 | GrabMode::ASYNC, |
| 1722 | GrabMode::ASYNC, |
| 1723 | x11rb::NONE, |
| 1724 | x11rb::NONE, |
| 1725 | x11rb::CURRENT_TIME, |
| 1726 | )? |
| 1727 | .reply()?; |
| 1728 | |
| 1729 | let pointer = self.connection.query_pointer(self.root)?.reply()?; |
| 1730 | let (start_x, start_y) = (pointer.root_x, pointer.root_y); |
| 1731 | |
| 1732 | self.connection.configure_window( |
| 1733 | window, |
| 1734 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 1735 | )?; |
| 1736 | |
| 1737 | loop { |
| 1738 | let event = self.connection.wait_for_event()?; |
| 1739 | match event { |
| 1740 | Event::MotionNotify(e) => { |
| 1741 | let new_x = geometry.x + (e.root_x - start_x); |
| 1742 | let new_y = geometry.y + (e.root_y - start_y); |
| 1743 | self.connection.configure_window( |
| 1744 | window, |
| 1745 | &ConfigureWindowAux::new().x(new_x as i32).y(new_y as i32), |
| 1746 | )?; |
| 1747 | self.connection.flush()?; |
| 1748 | } |
| 1749 | Event::ButtonRelease(_) => break, |
| 1750 | _ => {} |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | self.connection |
| 1755 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 1756 | .check()?; |
| 1757 | self.connection |
| 1758 | .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? |
| 1759 | .check()?; |
| 1760 | |
| 1761 | Ok(()) |
| 1762 | } |
| 1763 | |
| 1764 | fn resize_mouse(&mut self, window: Window) -> WmResult<()> { |
| 1765 | self.floating_windows.insert(window); |
| 1766 | |
| 1767 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 1768 | |
| 1769 | self.connection.warp_pointer( |
| 1770 | x11rb::NONE, |
| 1771 | window, |
| 1772 | 0, |
| 1773 | 0, |
| 1774 | 0, |
| 1775 | 0, |
| 1776 | geometry.width as i16, |
| 1777 | geometry.height as i16, |
| 1778 | )?; |
| 1779 | |
| 1780 | self.connection |
| 1781 | .grab_pointer( |
| 1782 | false, |
| 1783 | self.root, |
| 1784 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), |
| 1785 | GrabMode::ASYNC, |
| 1786 | GrabMode::ASYNC, |
| 1787 | x11rb::NONE, |
| 1788 | x11rb::NONE, |
| 1789 | x11rb::CURRENT_TIME, |
| 1790 | )? |
| 1791 | .reply()?; |
| 1792 | |
| 1793 | self.connection.configure_window( |
| 1794 | window, |
| 1795 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 1796 | )?; |
| 1797 | |
| 1798 | loop { |
| 1799 | let event = self.connection.wait_for_event()?; |
| 1800 | match event { |
| 1801 | Event::MotionNotify(e) => { |
| 1802 | let new_width = (e.root_x - geometry.x).max(1) as u32; |
| 1803 | let new_height = (e.root_y - geometry.y).max(1) as u32; |
| 1804 | |
| 1805 | self.connection.configure_window( |
| 1806 | window, |
| 1807 | &ConfigureWindowAux::new() |
| 1808 | .width(new_width) |
| 1809 | .height(new_height), |
| 1810 | )?; |
| 1811 | self.connection.flush()?; |
| 1812 | } |
| 1813 | Event::ButtonRelease(_) => break, |
| 1814 | _ => {} |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | self.connection |
| 1819 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 1820 | .check()?; |
| 1821 | self.connection |
| 1822 | .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? |
| 1823 | .check()?; |
| 1824 | |
| 1825 | Ok(()) |
| 1826 | } |
| 1827 | |
| 1828 | fn handle_event(&mut self, event: Event) -> WmResult<Option<bool>> { |
| 1829 | match event { |
| 1830 | Event::KeyPress(ref e) if e.event == self.overlay.window() => { |
| 1831 | if self.overlay.is_visible() { |
| 1832 | let _ = self.overlay.hide(&self.connection); |
| 1833 | } |
| 1834 | return Ok(None); |
| 1835 | } |
| 1836 | Event::ButtonPress(ref e) if e.event == self.overlay.window() => { |
| 1837 | if self.overlay.is_visible() { |
| 1838 | let _ = self.overlay.hide(&self.connection); |
| 1839 | } |
| 1840 | return Ok(None); |
| 1841 | } |
| 1842 | Event::Expose(ref e) if e.window == self.overlay.window() => { |
| 1843 | if self.overlay.is_visible() { |
| 1844 | let _ = self.overlay.draw(&self.connection, &self.font); |
| 1845 | } |
| 1846 | return Ok(None); |
| 1847 | } |
| 1848 | Event::KeyPress(ref e) if e.event == self.keybind_overlay.window() => { |
| 1849 | if self.keybind_overlay.is_visible() |
| 1850 | && !self.keybind_overlay.should_suppress_input() |
| 1851 | { |
| 1852 | use crate::keyboard::keysyms; |
| 1853 | let keyboard_mapping = self |
| 1854 | .connection |
| 1855 | .get_keyboard_mapping( |
| 1856 | self.connection.setup().min_keycode, |
| 1857 | self.connection.setup().max_keycode |
| 1858 | - self.connection.setup().min_keycode |
| 1859 | + 1, |
| 1860 | )? |
| 1861 | .reply()?; |
| 1862 | |
| 1863 | let min_keycode = self.connection.setup().min_keycode; |
| 1864 | let keysyms_per_keycode = keyboard_mapping.keysyms_per_keycode; |
| 1865 | let index = (e.detail - min_keycode) as usize * keysyms_per_keycode as usize; |
| 1866 | |
| 1867 | if let Some(&keysym) = keyboard_mapping.keysyms.get(index) { |
| 1868 | if keysym == keysyms::XK_ESCAPE || keysym == keysyms::XK_Q { |
| 1869 | let _ = self.keybind_overlay.hide(&self.connection); |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | return Ok(None); |
| 1874 | } |
| 1875 | Event::ButtonPress(ref e) if e.event == self.keybind_overlay.window() => { |
| 1876 | return Ok(None); |
| 1877 | } |
| 1878 | Event::Expose(ref e) if e.window == self.keybind_overlay.window() => { |
| 1879 | if self.keybind_overlay.is_visible() { |
| 1880 | let _ = self.keybind_overlay.draw(&self.connection, &self.font); |
| 1881 | } |
| 1882 | return Ok(None); |
| 1883 | } |
| 1884 | Event::MapRequest(event) => { |
| 1885 | let attrs = match self.connection.get_window_attributes(event.window)?.reply() { |
| 1886 | Ok(attrs) => attrs, |
| 1887 | Err(_) => return Ok(None), |
| 1888 | }; |
| 1889 | |
| 1890 | if attrs.override_redirect { |
| 1891 | return Ok(None); |
| 1892 | } |
| 1893 | |
| 1894 | if self.windows.contains(&event.window) { |
| 1895 | return Ok(None); |
| 1896 | } |
| 1897 | |
| 1898 | if let Ok(geom) = self.connection.get_geometry(event.window)?.reply() { |
| 1899 | self.window_geometries |
| 1900 | .insert(event.window, (geom.x, geom.y, geom.width, geom.height)); |
| 1901 | } |
| 1902 | |
| 1903 | self.connection.map_window(event.window)?; |
| 1904 | self.connection.change_window_attributes( |
| 1905 | event.window, |
| 1906 | &ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW | EventMask::STRUCTURE_NOTIFY | EventMask::PROPERTY_CHANGE), |
| 1907 | )?; |
| 1908 | |
| 1909 | let selected_tags = self |
| 1910 | .monitors |
| 1911 | .get(self.selected_monitor) |
| 1912 | .map(|m| m.selected_tags) |
| 1913 | .unwrap_or(tag_mask(0)); |
| 1914 | |
| 1915 | self.windows.push(event.window); |
| 1916 | self.window_tags.insert(event.window, selected_tags); |
| 1917 | self.window_monitor |
| 1918 | .insert(event.window, self.selected_monitor); |
| 1919 | self.set_wm_state(event.window, 1)?; |
| 1920 | let _ = self.save_client_tag(event.window, selected_tags); |
| 1921 | |
| 1922 | // Auto-float transient windows (e.g., dialogs, modals) |
| 1923 | if self.is_transient_window(event.window) { |
| 1924 | self.floating_windows.insert(event.window); |
| 1925 | } |
| 1926 | |
| 1927 | self.apply_layout()?; |
| 1928 | self.update_bar()?; |
| 1929 | self.set_focus(event.window)?; |
| 1930 | } |
| 1931 | Event::ClientMessage(event) => { |
| 1932 | // Handle _NET_WM_STATE fullscreen requests from clients |
| 1933 | if event.type_ == self.atoms.net_wm_state { |
| 1934 | let data = event.data.as_data32(); |
| 1935 | let action = data[0]; // 0 = remove, 1 = add, 2 = toggle |
| 1936 | let property1 = data[1]; |
| 1937 | let property2 = data[2]; |
| 1938 | |
| 1939 | if property1 == self.atoms.net_wm_state_fullscreen || property2 == self.atoms.net_wm_state_fullscreen { |
| 1940 | if self.windows.contains(&event.window) { |
| 1941 | let is_fullscreen = self.fullscreen_windows.contains(&event.window); |
| 1942 | let should_fullscreen = match action { |
| 1943 | 0 => false, // Remove |
| 1944 | 1 => true, // Add |
| 1945 | 2 => !is_fullscreen, // Toggle |
| 1946 | _ => is_fullscreen, |
| 1947 | }; |
| 1948 | self.set_window_fullscreen(event.window, should_fullscreen)?; |
| 1949 | } |
| 1950 | } |
| 1951 | } |
| 1952 | } |
| 1953 | Event::UnmapNotify(event) => { |
| 1954 | if self.windows.contains(&event.window) && self.is_window_visible(event.window) { |
| 1955 | self.remove_window(event.window)?; |
| 1956 | } |
| 1957 | } |
| 1958 | Event::DestroyNotify(event) => { |
| 1959 | if self.windows.contains(&event.window) { |
| 1960 | self.remove_window(event.window)?; |
| 1961 | } |
| 1962 | } |
| 1963 | Event::EnterNotify(event) => { |
| 1964 | if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL { |
| 1965 | return Ok(None); |
| 1966 | } |
| 1967 | if self.windows.contains(&event.event) { |
| 1968 | self.set_focus(event.event)?; |
| 1969 | } |
| 1970 | } |
| 1971 | Event::MotionNotify(event) => { |
| 1972 | if event.event != self.root { |
| 1973 | return Ok(None); |
| 1974 | } |
| 1975 | |
| 1976 | if let Some(monitor_index) = |
| 1977 | self.get_monitor_at_point(event.root_x as i32, event.root_y as i32) |
| 1978 | { |
| 1979 | if monitor_index != self.selected_monitor { |
| 1980 | self.selected_monitor = monitor_index; |
| 1981 | self.update_bar()?; |
| 1982 | |
| 1983 | let visible = self.visible_windows_on_monitor(monitor_index); |
| 1984 | if let Some(&win) = visible.first() { |
| 1985 | self.set_focus(win)?; |
| 1986 | } |
| 1987 | } |
| 1988 | } |
| 1989 | } |
| 1990 | Event::KeyPress(event) => { |
| 1991 | let result = keyboard::handle_key_press( |
| 1992 | event, |
| 1993 | &self.config.keybindings, |
| 1994 | &self.keychord_state, |
| 1995 | &self.connection, |
| 1996 | )?; |
| 1997 | |
| 1998 | match result { |
| 1999 | keyboard::handlers::KeychordResult::Completed(action, arg) => { |
| 2000 | self.keychord_state = keyboard::handlers::KeychordState::Idle; |
| 2001 | self.ungrab_chord_keys()?; |
| 2002 | self.update_bar()?; |
| 2003 | |
| 2004 | match action { |
| 2005 | KeyAction::Quit => return Ok(Some(false)), |
| 2006 | KeyAction::Restart => match self.try_reload_config() { |
| 2007 | Ok(()) => { |
| 2008 | self.gaps_enabled = self.config.gaps_enabled; |
| 2009 | self.error_message = None; |
| 2010 | let _ = self.overlay.hide(&self.connection); |
| 2011 | self.apply_layout()?; |
| 2012 | self.update_bar()?; |
| 2013 | } |
| 2014 | Err(err) => { |
| 2015 | self.error_message = Some(err.clone()); |
| 2016 | let screen_width = self.screen.width_in_pixels; |
| 2017 | let screen_height = self.screen.height_in_pixels; |
| 2018 | let _ = self.overlay.show_error( |
| 2019 | &self.connection, |
| 2020 | &self.font, |
| 2021 | &err, |
| 2022 | screen_width, |
| 2023 | screen_height, |
| 2024 | ); |
| 2025 | } |
| 2026 | }, |
| 2027 | _ => self.handle_key_action(action, &arg)?, |
| 2028 | } |
| 2029 | } |
| 2030 | keyboard::handlers::KeychordResult::InProgress(candidates) => { |
| 2031 | let keys_pressed = match &self.keychord_state { |
| 2032 | keyboard::handlers::KeychordState::Idle => 1, |
| 2033 | keyboard::handlers::KeychordState::InProgress { |
| 2034 | keys_pressed, .. |
| 2035 | } => keys_pressed + 1, |
| 2036 | }; |
| 2037 | |
| 2038 | self.keychord_state = keyboard::handlers::KeychordState::InProgress { |
| 2039 | candidates: candidates.clone(), |
| 2040 | keys_pressed, |
| 2041 | }; |
| 2042 | |
| 2043 | self.grab_next_keys(&candidates, keys_pressed)?; |
| 2044 | self.update_bar()?; |
| 2045 | } |
| 2046 | keyboard::handlers::KeychordResult::Cancelled |
| 2047 | | keyboard::handlers::KeychordResult::None => { |
| 2048 | self.keychord_state = keyboard::handlers::KeychordState::Idle; |
| 2049 | self.ungrab_chord_keys()?; |
| 2050 | self.update_bar()?; |
| 2051 | } |
| 2052 | } |
| 2053 | } |
| 2054 | Event::ButtonPress(event) => { |
| 2055 | let is_bar_click = self |
| 2056 | .bars |
| 2057 | .iter() |
| 2058 | .enumerate() |
| 2059 | .find(|(_, bar)| bar.window() == event.event); |
| 2060 | |
| 2061 | if let Some((monitor_index, bar)) = is_bar_click { |
| 2062 | if let Some(tag_index) = bar.handle_click(event.event_x) { |
| 2063 | if monitor_index != self.selected_monitor { |
| 2064 | self.selected_monitor = monitor_index; |
| 2065 | } |
| 2066 | self.view_tag(tag_index)?; |
| 2067 | } |
| 2068 | } else if event.child != x11rb::NONE { |
| 2069 | self.set_focus(event.child)?; |
| 2070 | |
| 2071 | if event.detail == ButtonIndex::M1.into() { |
| 2072 | self.move_mouse(event.child)?; |
| 2073 | } else if event.detail == ButtonIndex::M3.into() { |
| 2074 | self.resize_mouse(event.child)?; |
| 2075 | } |
| 2076 | } |
| 2077 | } |
| 2078 | Event::Expose(event) => { |
| 2079 | for bar in &mut self.bars { |
| 2080 | if event.window == bar.window() { |
| 2081 | bar.invalidate(); |
| 2082 | self.update_bar()?; |
| 2083 | break; |
| 2084 | } |
| 2085 | } |
| 2086 | } |
| 2087 | _ => {} |
| 2088 | } |
| 2089 | Ok(None) |
| 2090 | } |
| 2091 | |
| 2092 | fn apply_layout(&self) -> WmResult<()> { |
| 2093 | if self.layout.name() == LayoutType::Normie.as_str() { |
| 2094 | return Ok(()); |
| 2095 | } |
| 2096 | |
| 2097 | for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 2098 | let border_width = if monitor.fullscreen_enabled { |
| 2099 | 0 |
| 2100 | } else { |
| 2101 | self.config.border_width |
| 2102 | }; |
| 2103 | |
| 2104 | let gaps = if self.gaps_enabled && !monitor.fullscreen_enabled { |
| 2105 | GapConfig { |
| 2106 | inner_horizontal: self.config.gap_inner_horizontal, |
| 2107 | inner_vertical: self.config.gap_inner_vertical, |
| 2108 | outer_horizontal: self.config.gap_outer_horizontal, |
| 2109 | outer_vertical: self.config.gap_outer_vertical, |
| 2110 | } |
| 2111 | } else { |
| 2112 | GapConfig { |
| 2113 | inner_horizontal: 0, |
| 2114 | inner_vertical: 0, |
| 2115 | outer_horizontal: 0, |
| 2116 | outer_vertical: 0, |
| 2117 | } |
| 2118 | }; |
| 2119 | |
| 2120 | let visible: Vec<Window> = self |
| 2121 | .windows |
| 2122 | .iter() |
| 2123 | .filter(|&&w| { |
| 2124 | let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0); |
| 2125 | if window_mon != monitor_index { |
| 2126 | return false; |
| 2127 | } |
| 2128 | if self.floating_windows.contains(&w) { |
| 2129 | return false; |
| 2130 | } |
| 2131 | if let Some(&tags) = self.window_tags.get(&w) { |
| 2132 | (tags & monitor.selected_tags) != 0 |
| 2133 | } else { |
| 2134 | false |
| 2135 | } |
| 2136 | }) |
| 2137 | .copied() |
| 2138 | .collect(); |
| 2139 | |
| 2140 | let bar_height = if monitor.fullscreen_enabled { |
| 2141 | 0 |
| 2142 | } else { |
| 2143 | self.bars |
| 2144 | .get(monitor_index) |
| 2145 | .map(|b| b.height() as u32) |
| 2146 | .unwrap_or(0) |
| 2147 | }; |
| 2148 | let usable_height = monitor.height.saturating_sub(bar_height); |
| 2149 | |
| 2150 | let geometries = self |
| 2151 | .layout |
| 2152 | .arrange(&visible, monitor.width, usable_height, &gaps); |
| 2153 | |
| 2154 | for (window, geometry) in visible.iter().zip(geometries.iter()) { |
| 2155 | let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
| 2156 | let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
| 2157 | |
| 2158 | let adjusted_x = geometry.x_coordinate + monitor.x; |
| 2159 | let adjusted_y = geometry.y_coordinate + monitor.y + bar_height as i32; |
| 2160 | |
| 2161 | self.connection.configure_window( |
| 2162 | *window, |
| 2163 | &ConfigureWindowAux::new() |
| 2164 | .x(adjusted_x) |
| 2165 | .y(adjusted_y) |
| 2166 | .width(adjusted_width) |
| 2167 | .height(adjusted_height) |
| 2168 | .border_width(border_width), |
| 2169 | )?; |
| 2170 | } |
| 2171 | } |
| 2172 | |
| 2173 | self.connection.flush()?; |
| 2174 | Ok(()) |
| 2175 | } |
| 2176 | |
| 2177 | pub fn change_layout<L: Layout + 'static>(&mut self, new_layout: L) -> WmResult<()> { |
| 2178 | self.layout = Box::new(new_layout); |
| 2179 | self.apply_layout()?; |
| 2180 | Ok(()) |
| 2181 | } |
| 2182 | |
| 2183 | fn remove_window(&mut self, window: Window) -> WmResult<()> { |
| 2184 | let initial_count = self.windows.len(); |
| 2185 | self.windows.retain(|&w| w != window); |
| 2186 | self.window_tags.remove(&window); |
| 2187 | self.window_monitor.remove(&window); |
| 2188 | self.window_geometries.remove(&window); |
| 2189 | self.floating_windows.remove(&window); |
| 2190 | self.fullscreen_windows.remove(&window); |
| 2191 | |
| 2192 | if self.windows.len() < initial_count { |
| 2193 | let focused = self |
| 2194 | .monitors |
| 2195 | .get(self.selected_monitor) |
| 2196 | .and_then(|m| m.focused_window); |
| 2197 | if focused == Some(window) { |
| 2198 | let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 2199 | if let Some(&new_win) = visible.last() { |
| 2200 | self.set_focus(new_win)?; |
| 2201 | } else if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 2202 | monitor.focused_window = None; |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | self.apply_layout()?; |
| 2207 | self.update_bar()?; |
| 2208 | } |
| 2209 | Ok(()) |
| 2210 | } |
| 2211 | |
| 2212 | fn run_autostart_commands(&self) -> Result<(), WmError> { |
| 2213 | for command in &self.config.autostart { |
| 2214 | Command::new("sh") |
| 2215 | .arg("-c") |
| 2216 | .arg(command) |
| 2217 | .spawn() |
| 2218 | .map_err(|e| WmError::Autostart(command.clone(), e))?; |
| 2219 | eprintln!("[autostart] Spawned: {}", command); |
| 2220 | } |
| 2221 | Ok(()) |
| 2222 | } |
| 2223 | } |