| 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 std::collections::HashSet; |
| 10 | use x11rb::cursor::Handle as CursorHandle; |
| 11 | |
| 12 | use x11rb::connection::Connection; |
| 13 | use x11rb::protocol::Event; |
| 14 | use x11rb::protocol::xproto::*; |
| 15 | use x11rb::rust_connection::RustConnection; |
| 16 | |
| 17 | pub type TagMask = u32; |
| 18 | pub fn tag_mask(tag: usize) -> TagMask { |
| 19 | 1 << tag |
| 20 | } |
| 21 | |
| 22 | struct AtomCache { |
| 23 | net_current_desktop: Atom, |
| 24 | net_client_info: Atom, |
| 25 | wm_state: Atom, |
| 26 | } |
| 27 | |
| 28 | impl AtomCache { |
| 29 | fn new(connection: &RustConnection) -> WmResult<Self> { |
| 30 | let net_current_desktop = connection |
| 31 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| 32 | .reply()? |
| 33 | .atom; |
| 34 | |
| 35 | let net_client_info = connection |
| 36 | .intern_atom(false, b"_NET_CLIENT_INFO")? |
| 37 | .reply()? |
| 38 | .atom; |
| 39 | |
| 40 | let wm_state = connection.intern_atom(false, b"WM_STATE")?.reply()?.atom; |
| 41 | |
| 42 | Ok(Self { |
| 43 | net_current_desktop, |
| 44 | net_client_info, |
| 45 | wm_state, |
| 46 | }) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub struct WindowManager { |
| 51 | config: Config, |
| 52 | connection: RustConnection, |
| 53 | screen_number: usize, |
| 54 | root: Window, |
| 55 | screen: Screen, |
| 56 | windows: Vec<Window>, |
| 57 | layout: LayoutBox, |
| 58 | window_tags: std::collections::HashMap<Window, TagMask>, |
| 59 | window_monitor: std::collections::HashMap<Window, usize>, |
| 60 | window_geometries: std::collections::HashMap<Window, (i16, i16, u16, u16)>, |
| 61 | gaps_enabled: bool, |
| 62 | fullscreen_enabled: bool, |
| 63 | floating_windows: HashSet<Window>, |
| 64 | bars: Vec<Bar>, |
| 65 | monitors: Vec<Monitor>, |
| 66 | selected_monitor: usize, |
| 67 | atoms: AtomCache, |
| 68 | previous_focused: Option<Window>, |
| 69 | display: *mut x11::xlib::Display, |
| 70 | font: crate::bar::font::Font, |
| 71 | keychord_state: keyboard::handlers::KeychordState, |
| 72 | } |
| 73 | |
| 74 | type WmResult<T> = Result<T, WmError>; |
| 75 | |
| 76 | impl WindowManager { |
| 77 | pub fn new(config: Config) -> WmResult<Self> { |
| 78 | let (connection, screen_number) = x11rb::connect(None)?; |
| 79 | let root = connection.setup().roots[screen_number].root; |
| 80 | let screen = connection.setup().roots[screen_number].clone(); |
| 81 | |
| 82 | let normal_cursor = CursorHandle::new( |
| 83 | &connection, |
| 84 | screen_number, |
| 85 | &x11rb::resource_manager::new_from_default(&connection)?, |
| 86 | )? |
| 87 | .reply()? |
| 88 | .load_cursor(&connection, "left_ptr")?; |
| 89 | |
| 90 | connection |
| 91 | .change_window_attributes( |
| 92 | root, |
| 93 | &ChangeWindowAttributesAux::new() |
| 94 | .cursor(normal_cursor) |
| 95 | .event_mask( |
| 96 | EventMask::SUBSTRUCTURE_REDIRECT |
| 97 | | EventMask::SUBSTRUCTURE_NOTIFY |
| 98 | | EventMask::PROPERTY_CHANGE |
| 99 | | EventMask::KEY_PRESS |
| 100 | | EventMask::BUTTON_PRESS |
| 101 | | EventMask::POINTER_MOTION, |
| 102 | ), |
| 103 | )? |
| 104 | .check()?; |
| 105 | |
| 106 | connection.grab_button( |
| 107 | false, |
| 108 | root, |
| 109 | EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, |
| 110 | GrabMode::SYNC, |
| 111 | GrabMode::ASYNC, |
| 112 | x11rb::NONE, |
| 113 | x11rb::NONE, |
| 114 | ButtonIndex::M1, |
| 115 | u16::from(config.modkey).into(), |
| 116 | )?; |
| 117 | |
| 118 | connection.grab_button( |
| 119 | false, |
| 120 | root, |
| 121 | EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, |
| 122 | GrabMode::SYNC, |
| 123 | GrabMode::ASYNC, |
| 124 | x11rb::NONE, |
| 125 | x11rb::NONE, |
| 126 | ButtonIndex::M3, |
| 127 | u16::from(config.modkey).into(), |
| 128 | )?; |
| 129 | |
| 130 | let monitors = detect_monitors(&connection, &screen, root)?; |
| 131 | |
| 132 | let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) }; |
| 133 | if display.is_null() { |
| 134 | return Err(WmError::X11(crate::errors::X11Error::DisplayOpenFailed)); |
| 135 | } |
| 136 | |
| 137 | let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?; |
| 138 | |
| 139 | let mut bars = Vec::new(); |
| 140 | for monitor in monitors.iter() { |
| 141 | let bar = Bar::new( |
| 142 | &connection, |
| 143 | &screen, |
| 144 | screen_number, |
| 145 | &config, |
| 146 | display, |
| 147 | &font, |
| 148 | monitor.x as i16, |
| 149 | monitor.y as i16, |
| 150 | monitor.width as u16, |
| 151 | )?; |
| 152 | bars.push(bar); |
| 153 | } |
| 154 | |
| 155 | let gaps_enabled = config.gaps_enabled; |
| 156 | |
| 157 | let atoms = AtomCache::new(&connection)?; |
| 158 | |
| 159 | let mut window_manager = Self { |
| 160 | config, |
| 161 | connection, |
| 162 | screen_number, |
| 163 | root, |
| 164 | screen, |
| 165 | windows: Vec::new(), |
| 166 | layout: Box::new(TilingLayout), |
| 167 | window_tags: std::collections::HashMap::new(), |
| 168 | window_monitor: std::collections::HashMap::new(), |
| 169 | window_geometries: std::collections::HashMap::new(), |
| 170 | gaps_enabled, |
| 171 | fullscreen_enabled: false, |
| 172 | floating_windows: HashSet::new(), |
| 173 | bars, |
| 174 | monitors, |
| 175 | selected_monitor: 0, |
| 176 | atoms, |
| 177 | previous_focused: None, |
| 178 | display, |
| 179 | font, |
| 180 | keychord_state: keyboard::handlers::KeychordState::Idle, |
| 181 | }; |
| 182 | |
| 183 | window_manager.scan_existing_windows()?; |
| 184 | window_manager.update_bar()?; |
| 185 | |
| 186 | Ok(window_manager) |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * |
| 191 | * This function is deprecated for now, but will potentially be used in the future. |
| 192 | * |
| 193 | */ |
| 194 | fn _get_saved_selected_tags( |
| 195 | connection: &RustConnection, |
| 196 | root: Window, |
| 197 | tag_count: usize, |
| 198 | ) -> WmResult<TagMask> { |
| 199 | let net_current_desktop = connection |
| 200 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| 201 | .reply()? |
| 202 | .atom; |
| 203 | |
| 204 | match connection |
| 205 | .get_property(false, root, net_current_desktop, AtomEnum::CARDINAL, 0, 1)? |
| 206 | .reply() |
| 207 | { |
| 208 | Ok(prop) if prop.value.len() >= 4 => { |
| 209 | let desktop = u32::from_ne_bytes([ |
| 210 | prop.value[0], |
| 211 | prop.value[1], |
| 212 | prop.value[2], |
| 213 | prop.value[3], |
| 214 | ]); |
| 215 | if desktop < tag_count as u32 { |
| 216 | let mask = tag_mask(desktop as usize); |
| 217 | return Ok(mask); |
| 218 | } |
| 219 | } |
| 220 | Ok(_) => {} |
| 221 | Err(e) => { |
| 222 | eprintln!("No _NET_CURRENT_DESKTOP property ({})", e); |
| 223 | } |
| 224 | } |
| 225 | Ok(tag_mask(0)) |
| 226 | } |
| 227 | |
| 228 | fn scan_existing_windows(&mut self) -> WmResult<()> { |
| 229 | let tree = self.connection.query_tree(self.root)?.reply()?; |
| 230 | let net_client_info = self.atoms.net_client_info; |
| 231 | let wm_state_atom = self.atoms.wm_state; |
| 232 | |
| 233 | for &window in &tree.children { |
| 234 | if self.bars.iter().any(|bar| bar.window() == window) { |
| 235 | continue; |
| 236 | } |
| 237 | |
| 238 | let Ok(attrs) = self.connection.get_window_attributes(window)?.reply() else { |
| 239 | continue; |
| 240 | }; |
| 241 | |
| 242 | if attrs.override_redirect { |
| 243 | continue; |
| 244 | } |
| 245 | |
| 246 | if attrs.map_state == MapState::VIEWABLE { |
| 247 | let tag = self.get_saved_tag(window, net_client_info)?; |
| 248 | self.windows.push(window); |
| 249 | self.window_tags.insert(window, tag); |
| 250 | self.window_monitor.insert(window, self.selected_monitor); |
| 251 | continue; |
| 252 | } |
| 253 | |
| 254 | if attrs.map_state == MapState::UNMAPPED { |
| 255 | let has_wm_state = self |
| 256 | .connection |
| 257 | .get_property(false, window, wm_state_atom, AtomEnum::ANY, 0, 2)? |
| 258 | .reply() |
| 259 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 260 | |
| 261 | if !has_wm_state { |
| 262 | continue; |
| 263 | } |
| 264 | |
| 265 | let has_wm_class = self |
| 266 | .connection |
| 267 | .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)? |
| 268 | .reply() |
| 269 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 270 | |
| 271 | if has_wm_class { |
| 272 | let tag = self.get_saved_tag(window, net_client_info)?; |
| 273 | self.windows.push(window); |
| 274 | self.window_tags.insert(window, tag); |
| 275 | self.window_monitor.insert(window, self.selected_monitor); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | if let Some(&first) = self.windows.first() { |
| 281 | self.set_focus(first)?; |
| 282 | } |
| 283 | |
| 284 | self.apply_layout()?; |
| 285 | Ok(()) |
| 286 | } |
| 287 | |
| 288 | fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> WmResult<TagMask> { |
| 289 | match self |
| 290 | .connection |
| 291 | .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)? |
| 292 | .reply() |
| 293 | { |
| 294 | Ok(prop) if prop.value.len() >= 4 => { |
| 295 | let tags = u32::from_ne_bytes([ |
| 296 | prop.value[0], |
| 297 | prop.value[1], |
| 298 | prop.value[2], |
| 299 | prop.value[3], |
| 300 | ]); |
| 301 | |
| 302 | if tags != 0 && tags < (1 << self.config.tags.len()) { |
| 303 | return Ok(tags); |
| 304 | } |
| 305 | } |
| 306 | Ok(_) => {} |
| 307 | Err(e) => { |
| 308 | eprintln!("No _NET_CLIENT_INFO property ({})", e); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | Ok(self |
| 313 | .monitors |
| 314 | .get(self.selected_monitor) |
| 315 | .map(|m| m.selected_tags) |
| 316 | .unwrap_or(tag_mask(0))) |
| 317 | } |
| 318 | |
| 319 | fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { |
| 320 | let net_client_info = self.atoms.net_client_info; |
| 321 | |
| 322 | let bytes = tag.to_ne_bytes().to_vec(); |
| 323 | |
| 324 | self.connection.change_property( |
| 325 | PropMode::REPLACE, |
| 326 | window, |
| 327 | net_client_info, |
| 328 | AtomEnum::CARDINAL, |
| 329 | 32, |
| 330 | 1, |
| 331 | &bytes, |
| 332 | )?; |
| 333 | |
| 334 | self.connection.flush()?; |
| 335 | Ok(()) |
| 336 | } |
| 337 | |
| 338 | fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> { |
| 339 | let wm_state_atom = self.atoms.wm_state; |
| 340 | |
| 341 | let data = [state, 0u32]; |
| 342 | let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); |
| 343 | |
| 344 | self.connection.change_property( |
| 345 | PropMode::REPLACE, |
| 346 | window, |
| 347 | wm_state_atom, |
| 348 | wm_state_atom, |
| 349 | 32, |
| 350 | 2, |
| 351 | &bytes, |
| 352 | )?; |
| 353 | |
| 354 | self.connection.flush()?; |
| 355 | Ok(()) |
| 356 | } |
| 357 | |
| 358 | pub fn run(&mut self) -> WmResult<bool> { |
| 359 | println!("oxwm started on display {}", self.screen_number); |
| 360 | |
| 361 | keyboard::setup_keybinds(&self.connection, self.root, &self.config.keybindings)?; |
| 362 | self.update_bar()?; |
| 363 | |
| 364 | loop { |
| 365 | while let Some(event) = self.connection.poll_for_event()? { |
| 366 | if let Some(should_restart) = self.handle_event(event)? { |
| 367 | return Ok(should_restart); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | if let Some(bar) = self.bars.get_mut(self.selected_monitor) { |
| 372 | bar.update_blocks(); |
| 373 | } |
| 374 | |
| 375 | if self.bars.iter().any(|bar| bar.needs_redraw()) { |
| 376 | self.update_bar()?; |
| 377 | } |
| 378 | |
| 379 | std::thread::sleep(std::time::Duration::from_millis(100)); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | fn toggle_floating(&mut self) -> WmResult<()> { |
| 384 | if let Some(focused) = self |
| 385 | .monitors |
| 386 | .get(self.selected_monitor) |
| 387 | .and_then(|m| m.focused_window) |
| 388 | { |
| 389 | if self.floating_windows.contains(&focused) { |
| 390 | self.floating_windows.remove(&focused); |
| 391 | |
| 392 | let selected_tags = self |
| 393 | .monitors |
| 394 | .get(self.selected_monitor) |
| 395 | .map(|m| m.selected_tags) |
| 396 | .unwrap_or(tag_mask(0)); |
| 397 | |
| 398 | self.window_tags.insert(focused, selected_tags); |
| 399 | self.window_monitor.insert(focused, self.selected_monitor); |
| 400 | let _ = self.save_client_tag(focused, selected_tags); |
| 401 | |
| 402 | self.apply_layout()?; |
| 403 | } else { |
| 404 | let float_width = (self.screen.width_in_pixels / 2) as u32; |
| 405 | let float_height = (self.screen.height_in_pixels / 2) as u32; |
| 406 | |
| 407 | let border_width = self.config.border_width; |
| 408 | |
| 409 | let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32; |
| 410 | let center_height = |
| 411 | ((self.screen.height_in_pixels - float_height as u16) / 2) as i32; |
| 412 | |
| 413 | self.connection.configure_window( |
| 414 | focused, |
| 415 | &ConfigureWindowAux::new() |
| 416 | .x(center_width) |
| 417 | .y(center_height) |
| 418 | .width(float_width) |
| 419 | .height(float_height) |
| 420 | .border_width(border_width) |
| 421 | .stack_mode(StackMode::ABOVE), |
| 422 | )?; |
| 423 | |
| 424 | self.floating_windows.insert(focused); |
| 425 | self.apply_layout()?; |
| 426 | self.connection.flush()?; |
| 427 | } |
| 428 | } |
| 429 | Ok(()) |
| 430 | } |
| 431 | |
| 432 | fn smart_move_window(&mut self, direction: i32) -> WmResult<()> { |
| 433 | let focused = match self |
| 434 | .monitors |
| 435 | .get(self.selected_monitor) |
| 436 | .and_then(|m| m.focused_window) |
| 437 | { |
| 438 | Some(win) => win, |
| 439 | None => return Ok(()), |
| 440 | }; |
| 441 | |
| 442 | if !self.floating_windows.contains(&focused) { |
| 443 | let float_width = (self.screen.width_in_pixels / 2) as u32; |
| 444 | let float_height = (self.screen.height_in_pixels / 2) as u32; |
| 445 | let border_width = self.config.border_width; |
| 446 | let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32; |
| 447 | let center_height = ((self.screen.height_in_pixels - float_height as u16) / 2) as i32; |
| 448 | |
| 449 | self.connection.configure_window( |
| 450 | focused, |
| 451 | &ConfigureWindowAux::new() |
| 452 | .x(center_width) |
| 453 | .y(center_height) |
| 454 | .width(float_width) |
| 455 | .height(float_height) |
| 456 | .border_width(border_width) |
| 457 | .stack_mode(StackMode::ABOVE), |
| 458 | )?; |
| 459 | self.floating_windows.insert(focused); |
| 460 | } |
| 461 | |
| 462 | let current_geom = match self.connection.get_geometry(focused)?.reply() { |
| 463 | Ok(geom) => geom, |
| 464 | Err(_) => return Ok(()), |
| 465 | }; |
| 466 | |
| 467 | let c_x = current_geom.x as i32; |
| 468 | let c_y = current_geom.y as i32; |
| 469 | let c_width = current_geom.width as i32; |
| 470 | let c_height = current_geom.height as i32; |
| 471 | |
| 472 | let monitor = match self.monitors.get(self.selected_monitor) { |
| 473 | Some(m) => m, |
| 474 | None => return Ok(()), |
| 475 | }; |
| 476 | |
| 477 | let (gap_ih, gap_iv, gap_oh, gap_ov) = if self.gaps_enabled { |
| 478 | ( |
| 479 | self.config.gap_inner_horizontal as i32, |
| 480 | self.config.gap_inner_vertical as i32, |
| 481 | self.config.gap_outer_horizontal as i32, |
| 482 | self.config.gap_outer_vertical as i32, |
| 483 | ) |
| 484 | } else { |
| 485 | (0, 0, 0, 0) |
| 486 | }; |
| 487 | |
| 488 | let (new_x, new_y) = match direction { |
| 489 | 0 => { |
| 490 | // UP |
| 491 | let mut target = i32::MIN; |
| 492 | let top = c_y; |
| 493 | let mut ny = c_y - (monitor.height as i32 / 4); |
| 494 | |
| 495 | for &other_window in &self.windows { |
| 496 | if other_window == focused { |
| 497 | continue; |
| 498 | } |
| 499 | if !self.floating_windows.contains(&other_window) { |
| 500 | continue; |
| 501 | } |
| 502 | if !self.is_window_visible(other_window) { |
| 503 | continue; |
| 504 | } |
| 505 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 506 | if other_mon != self.selected_monitor { |
| 507 | continue; |
| 508 | } |
| 509 | |
| 510 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 511 | Ok(geom) => geom, |
| 512 | Err(_) => continue, |
| 513 | }; |
| 514 | |
| 515 | let o_x = other_geom.x as i32; |
| 516 | let o_y = other_geom.y as i32; |
| 517 | let o_width = other_geom.width as i32; |
| 518 | let o_height = other_geom.height as i32; |
| 519 | |
| 520 | if c_x + c_width < o_x || c_x > o_x + o_width { |
| 521 | continue; |
| 522 | } |
| 523 | |
| 524 | let bottom = o_y + o_height + gap_iv; |
| 525 | if top > bottom && ny < bottom { |
| 526 | target = target.max(bottom); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | if target != i32::MIN { |
| 531 | ny = target; |
| 532 | } |
| 533 | ny = ny.max(monitor.y as i32 + gap_ov); |
| 534 | (c_x, ny) |
| 535 | } |
| 536 | 1 => { |
| 537 | // DOWN |
| 538 | let mut target = i32::MAX; |
| 539 | let bottom = c_y + c_height; |
| 540 | let mut ny = c_y + (monitor.height as i32 / 4); |
| 541 | |
| 542 | for &other_window in &self.windows { |
| 543 | if other_window == focused { |
| 544 | continue; |
| 545 | } |
| 546 | if !self.floating_windows.contains(&other_window) { |
| 547 | continue; |
| 548 | } |
| 549 | if !self.is_window_visible(other_window) { |
| 550 | continue; |
| 551 | } |
| 552 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 553 | if other_mon != self.selected_monitor { |
| 554 | continue; |
| 555 | } |
| 556 | |
| 557 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 558 | Ok(geom) => geom, |
| 559 | Err(_) => continue, |
| 560 | }; |
| 561 | |
| 562 | let o_x = other_geom.x as i32; |
| 563 | let o_y = other_geom.y as i32; |
| 564 | let o_width = other_geom.width as i32; |
| 565 | |
| 566 | if c_x + c_width < o_x || c_x > o_x + o_width { |
| 567 | continue; |
| 568 | } |
| 569 | |
| 570 | let top = o_y - gap_iv; |
| 571 | if bottom < top && (ny + c_height) > top { |
| 572 | target = target.min(top - c_height); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | if target != i32::MAX { |
| 577 | ny = target; |
| 578 | } |
| 579 | ny = ny.min(monitor.y as i32 + monitor.height as i32 - c_height - gap_ov); |
| 580 | (c_x, ny) |
| 581 | } |
| 582 | 2 => { |
| 583 | // LEFT |
| 584 | let mut target = i32::MIN; |
| 585 | let left = c_x; |
| 586 | let mut nx = c_x - (monitor.width as i32 / 6); |
| 587 | |
| 588 | for &other_window in &self.windows { |
| 589 | if other_window == focused { |
| 590 | continue; |
| 591 | } |
| 592 | if !self.floating_windows.contains(&other_window) { |
| 593 | continue; |
| 594 | } |
| 595 | if !self.is_window_visible(other_window) { |
| 596 | continue; |
| 597 | } |
| 598 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 599 | if other_mon != self.selected_monitor { |
| 600 | continue; |
| 601 | } |
| 602 | |
| 603 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 604 | Ok(geom) => geom, |
| 605 | Err(_) => continue, |
| 606 | }; |
| 607 | |
| 608 | let o_x = other_geom.x as i32; |
| 609 | let o_y = other_geom.y as i32; |
| 610 | let o_width = other_geom.width as i32; |
| 611 | let o_height = other_geom.height as i32; |
| 612 | |
| 613 | if c_y + c_height < o_y || c_y > o_y + o_height { |
| 614 | continue; |
| 615 | } |
| 616 | |
| 617 | let right = o_x + o_width + gap_ih; |
| 618 | if left > right && nx < right { |
| 619 | target = target.max(right); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | if target != i32::MIN { |
| 624 | nx = target; |
| 625 | } |
| 626 | nx = nx.max(monitor.x as i32 + gap_oh); |
| 627 | (nx, c_y) |
| 628 | } |
| 629 | 3 => { |
| 630 | // RIGHT |
| 631 | let mut target = i32::MAX; |
| 632 | let right = c_x + c_width; |
| 633 | let mut nx = c_x + (monitor.width as i32 / 6); |
| 634 | |
| 635 | for &other_window in &self.windows { |
| 636 | if other_window == focused { |
| 637 | continue; |
| 638 | } |
| 639 | if !self.floating_windows.contains(&other_window) { |
| 640 | continue; |
| 641 | } |
| 642 | if !self.is_window_visible(other_window) { |
| 643 | continue; |
| 644 | } |
| 645 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 646 | if other_mon != self.selected_monitor { |
| 647 | continue; |
| 648 | } |
| 649 | |
| 650 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 651 | Ok(geom) => geom, |
| 652 | Err(_) => continue, |
| 653 | }; |
| 654 | |
| 655 | let o_x = other_geom.x as i32; |
| 656 | let o_y = other_geom.y as i32; |
| 657 | let o_height = other_geom.height as i32; |
| 658 | |
| 659 | if c_y + c_height < o_y || c_y > o_y + o_height { |
| 660 | continue; |
| 661 | } |
| 662 | |
| 663 | let left = o_x - gap_ih; |
| 664 | if right < left && (nx + c_width) > left { |
| 665 | target = target.min(left - c_width); |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | if target != i32::MAX { |
| 670 | nx = target; |
| 671 | } |
| 672 | nx = nx.min(monitor.x as i32 + monitor.width as i32 - c_width - gap_oh); |
| 673 | (nx, c_y) |
| 674 | } |
| 675 | _ => return Ok(()), |
| 676 | }; |
| 677 | |
| 678 | self.connection.configure_window( |
| 679 | focused, |
| 680 | &ConfigureWindowAux::new() |
| 681 | .x(new_x) |
| 682 | .y(new_y) |
| 683 | .stack_mode(StackMode::ABOVE), |
| 684 | )?; |
| 685 | |
| 686 | self.connection.flush()?; |
| 687 | Ok(()) |
| 688 | } |
| 689 | |
| 690 | fn exchange_client(&mut self, direction: i32) -> WmResult<()> { |
| 691 | let focused = match self |
| 692 | .monitors |
| 693 | .get(self.selected_monitor) |
| 694 | .and_then(|m| m.focused_window) |
| 695 | { |
| 696 | Some(win) => win, |
| 697 | None => return Ok(()), |
| 698 | }; |
| 699 | |
| 700 | if self.floating_windows.contains(&focused) { |
| 701 | return Ok(()); |
| 702 | } |
| 703 | |
| 704 | let visible = self.visible_windows(); |
| 705 | if visible.len() < 2 { |
| 706 | return Ok(()); |
| 707 | } |
| 708 | |
| 709 | let current_idx = match visible.iter().position(|&w| w == focused) { |
| 710 | Some(idx) => idx, |
| 711 | None => return Ok(()), |
| 712 | }; |
| 713 | |
| 714 | let target_idx = match direction { |
| 715 | 0 | 2 => { |
| 716 | // UP or LEFT - previous in stack |
| 717 | if current_idx == 0 { |
| 718 | visible.len() - 1 |
| 719 | } else { |
| 720 | current_idx - 1 |
| 721 | } |
| 722 | } |
| 723 | 1 | 3 => { |
| 724 | // DOWN or RIGHT - next in stack |
| 725 | (current_idx + 1) % visible.len() |
| 726 | } |
| 727 | _ => return Ok(()), |
| 728 | }; |
| 729 | |
| 730 | let target = visible[target_idx]; |
| 731 | |
| 732 | let focused_pos = self.windows.iter().position(|&w| w == focused); |
| 733 | let target_pos = self.windows.iter().position(|&w| w == target); |
| 734 | |
| 735 | if let (Some(f_pos), Some(t_pos)) = (focused_pos, target_pos) { |
| 736 | self.windows.swap(f_pos, t_pos); |
| 737 | |
| 738 | self.apply_layout()?; |
| 739 | |
| 740 | self.set_focus(focused)?; |
| 741 | |
| 742 | if let Ok(geometry) = self.connection.get_geometry(focused)?.reply() { |
| 743 | self.connection.warp_pointer( |
| 744 | x11rb::NONE, |
| 745 | focused, |
| 746 | 0, |
| 747 | 0, |
| 748 | 0, |
| 749 | 0, |
| 750 | geometry.width as i16 / 2, |
| 751 | geometry.height as i16 / 2, |
| 752 | )?; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | Ok(()) |
| 757 | } |
| 758 | |
| 759 | fn toggle_fullscreen(&mut self) -> WmResult<()> { |
| 760 | self.fullscreen_enabled = !self.fullscreen_enabled; |
| 761 | |
| 762 | if self.fullscreen_enabled { |
| 763 | for bar in &self.bars { |
| 764 | self.connection.unmap_window(bar.window())?; |
| 765 | } |
| 766 | } else { |
| 767 | for bar in &self.bars { |
| 768 | self.connection.map_window(bar.window())?; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | self.apply_layout()?; |
| 773 | Ok(()) |
| 774 | } |
| 775 | |
| 776 | fn get_layout_symbol(&self) -> String { |
| 777 | let layout_name = self.layout.name(); |
| 778 | self.config |
| 779 | .layout_symbols |
| 780 | .iter() |
| 781 | .find(|l| l.name == layout_name) |
| 782 | .map(|l| l.symbol.clone()) |
| 783 | .unwrap_or_else(|| self.layout.symbol().to_string()) |
| 784 | } |
| 785 | |
| 786 | fn get_keychord_indicator(&self) -> Option<String> { |
| 787 | match &self.keychord_state { |
| 788 | keyboard::handlers::KeychordState::Idle => None, |
| 789 | keyboard::handlers::KeychordState::InProgress { candidates, keys_pressed } => { |
| 790 | if candidates.is_empty() { |
| 791 | return None; |
| 792 | } |
| 793 | |
| 794 | let binding = &self.config.keybindings[candidates[0]]; |
| 795 | let mut indicator = String::new(); |
| 796 | |
| 797 | for (i, key_press) in binding.keys.iter().take(*keys_pressed).enumerate() { |
| 798 | if i > 0 { |
| 799 | indicator.push(' '); |
| 800 | } |
| 801 | |
| 802 | for modifier in &key_press.modifiers { |
| 803 | indicator.push_str(Self::format_modifier(*modifier)); |
| 804 | indicator.push('+'); |
| 805 | } |
| 806 | |
| 807 | indicator.push_str(&self.format_keysym(key_press.keysym)); |
| 808 | } |
| 809 | |
| 810 | indicator.push('-'); |
| 811 | Some(indicator) |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | fn format_modifier(modifier: KeyButMask) -> &'static str { |
| 817 | match modifier { |
| 818 | KeyButMask::MOD1 => "Alt", |
| 819 | KeyButMask::MOD4 => "Super", |
| 820 | KeyButMask::SHIFT => "Shift", |
| 821 | KeyButMask::CONTROL => "Ctrl", |
| 822 | _ => "Mod", |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | fn format_keysym(&self, keysym: keyboard::keysyms::Keysym) -> String { |
| 827 | use keyboard::keysyms::*; |
| 828 | |
| 829 | match keysym { |
| 830 | XK_RETURN => "Return", |
| 831 | XK_ESCAPE => "Esc", |
| 832 | XK_SPACE => "Space", |
| 833 | XK_TAB => "Tab", |
| 834 | XK_BACKSPACE => "Backspace", |
| 835 | XK_DELETE => "Del", |
| 836 | XK_F1 => "F1", |
| 837 | XK_F2 => "F2", |
| 838 | XK_F3 => "F3", |
| 839 | XK_F4 => "F4", |
| 840 | XK_F5 => "F5", |
| 841 | XK_F6 => "F6", |
| 842 | XK_F7 => "F7", |
| 843 | XK_F8 => "F8", |
| 844 | XK_F9 => "F9", |
| 845 | XK_F10 => "F10", |
| 846 | XK_F11 => "F11", |
| 847 | XK_F12 => "F12", |
| 848 | XK_A..=XK_Z | XK_0..=XK_9 => { |
| 849 | return char::from_u32(keysym).unwrap_or('?').to_string(); |
| 850 | } |
| 851 | XK_LEFT => "Left", |
| 852 | XK_RIGHT => "Right", |
| 853 | XK_UP => "Up", |
| 854 | XK_DOWN => "Down", |
| 855 | XK_HOME => "Home", |
| 856 | XK_END => "End", |
| 857 | XK_PAGE_UP => "PgUp", |
| 858 | XK_PAGE_DOWN => "PgDn", |
| 859 | XK_INSERT => "Ins", |
| 860 | XK_MINUS => "-", |
| 861 | XK_EQUAL => "=", |
| 862 | XK_LEFT_BRACKET => "[", |
| 863 | XK_RIGHT_BRACKET => "]", |
| 864 | XK_SEMICOLON => ";", |
| 865 | XK_APOSTROPHE => "'", |
| 866 | XK_GRAVE => "`", |
| 867 | XK_BACKSLASH => "\\", |
| 868 | XK_COMMA => ",", |
| 869 | XK_PERIOD => ".", |
| 870 | XK_SLASH => "/", |
| 871 | XF86_AUDIO_RAISE_VOLUME => "Vol+", |
| 872 | XF86_AUDIO_LOWER_VOLUME => "Vol-", |
| 873 | XF86_AUDIO_MUTE => "Mute", |
| 874 | XF86_MON_BRIGHTNESS_UP => "Bri+", |
| 875 | XF86_MON_BRIGHTNESS_DOWN => "Bri-", |
| 876 | _ => "?", |
| 877 | }.to_string() |
| 878 | } |
| 879 | |
| 880 | fn update_bar(&mut self) -> WmResult<()> { |
| 881 | let layout_symbol = self.get_layout_symbol(); |
| 882 | let keychord_indicator = self.get_keychord_indicator(); |
| 883 | |
| 884 | for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 885 | if let Some(bar) = self.bars.get_mut(monitor_index) { |
| 886 | let mut occupied_tags: TagMask = 0; |
| 887 | for (&window, &tags) in &self.window_tags { |
| 888 | if self.window_monitor.get(&window).copied().unwrap_or(0) == monitor_index { |
| 889 | occupied_tags |= tags; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | let draw_blocks = monitor_index == self.selected_monitor; |
| 894 | bar.invalidate(); |
| 895 | bar.draw( |
| 896 | &self.connection, |
| 897 | &self.font, |
| 898 | self.display, |
| 899 | monitor.selected_tags, |
| 900 | occupied_tags, |
| 901 | draw_blocks, |
| 902 | &layout_symbol, |
| 903 | keychord_indicator.as_deref(), |
| 904 | )?; |
| 905 | } |
| 906 | } |
| 907 | Ok(()) |
| 908 | } |
| 909 | |
| 910 | fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> WmResult<()> { |
| 911 | match action { |
| 912 | KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, |
| 913 | KeyAction::KillClient => { |
| 914 | if let Some(focused) = self |
| 915 | .monitors |
| 916 | .get(self.selected_monitor) |
| 917 | .and_then(|m| m.focused_window) |
| 918 | { |
| 919 | match self.connection.kill_client(focused) { |
| 920 | Ok(_) => { |
| 921 | self.connection.flush()?; |
| 922 | } |
| 923 | Err(e) => { |
| 924 | eprintln!("Failed to kill window {}: {}", focused, e); |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | KeyAction::ToggleFullScreen => { |
| 930 | self.toggle_fullscreen()?; |
| 931 | } |
| 932 | KeyAction::ChangeLayout => { |
| 933 | if let Arg::Str(layout_name) = arg { |
| 934 | match layout_from_str(layout_name) { |
| 935 | Ok(layout) => { |
| 936 | self.layout = layout; |
| 937 | self.apply_layout()?; |
| 938 | self.update_bar()?; |
| 939 | } |
| 940 | Err(e) => eprintln!("Failed to change layout: {}", e), |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | KeyAction::CycleLayout => { |
| 945 | let current_name = self.layout.name(); |
| 946 | let next_name = next_layout(current_name); |
| 947 | match layout_from_str(next_name) { |
| 948 | Ok(layout) => { |
| 949 | self.layout = layout; |
| 950 | self.apply_layout()?; |
| 951 | self.update_bar()?; |
| 952 | } |
| 953 | Err(e) => eprintln!("Failed to cycle layout: {}", e), |
| 954 | } |
| 955 | } |
| 956 | KeyAction::ToggleFloating => { |
| 957 | self.toggle_floating()?; |
| 958 | } |
| 959 | |
| 960 | KeyAction::SmartMoveWin => { |
| 961 | if let Arg::Int(direction) = arg { |
| 962 | self.smart_move_window(*direction)?; |
| 963 | } |
| 964 | } |
| 965 | |
| 966 | KeyAction::ExchangeClient => { |
| 967 | if let Arg::Int(direction) = arg { |
| 968 | self.exchange_client(*direction)?; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | KeyAction::FocusStack => { |
| 973 | if let Arg::Int(direction) = arg { |
| 974 | self.cycle_focus(*direction)?; |
| 975 | } |
| 976 | } |
| 977 | KeyAction::Quit | KeyAction::Restart => { |
| 978 | // Handled in handle_event |
| 979 | } |
| 980 | KeyAction::Recompile => { |
| 981 | match std::process::Command::new("oxwm") |
| 982 | .arg("--recompile") |
| 983 | .spawn() |
| 984 | { |
| 985 | Ok(_) => eprintln!("Recompiling in background"), |
| 986 | Err(e) => eprintln!("Failed to spawn recompile: {}", e), |
| 987 | } |
| 988 | } |
| 989 | KeyAction::ViewTag => { |
| 990 | if let Arg::Int(tag_index) = arg { |
| 991 | self.view_tag(*tag_index as usize)?; |
| 992 | } |
| 993 | } |
| 994 | KeyAction::MoveToTag => { |
| 995 | if let Arg::Int(tag_index) = arg { |
| 996 | self.move_to_tag(*tag_index as usize)?; |
| 997 | } |
| 998 | } |
| 999 | KeyAction::ToggleGaps => { |
| 1000 | self.gaps_enabled = !self.gaps_enabled; |
| 1001 | self.apply_layout()?; |
| 1002 | } |
| 1003 | KeyAction::FocusMonitor => { |
| 1004 | if let Arg::Int(direction) = arg { |
| 1005 | self.focus_monitor(*direction)?; |
| 1006 | } |
| 1007 | } |
| 1008 | KeyAction::None => {} |
| 1009 | } |
| 1010 | Ok(()) |
| 1011 | } |
| 1012 | |
| 1013 | fn focus_monitor(&mut self, direction: i32) -> WmResult<()> { |
| 1014 | if self.monitors.is_empty() { |
| 1015 | return Ok(()); |
| 1016 | } |
| 1017 | |
| 1018 | let new_monitor = if direction > 0 { |
| 1019 | (self.selected_monitor + 1) % self.monitors.len() |
| 1020 | } else { |
| 1021 | (self.selected_monitor + self.monitors.len() - 1) % self.monitors.len() |
| 1022 | }; |
| 1023 | |
| 1024 | if new_monitor == self.selected_monitor { |
| 1025 | return Ok(()); |
| 1026 | } |
| 1027 | |
| 1028 | self.selected_monitor = new_monitor; |
| 1029 | |
| 1030 | self.update_bar()?; |
| 1031 | |
| 1032 | let visible = self.visible_windows_on_monitor(new_monitor); |
| 1033 | if let Some(&win) = visible.first() { |
| 1034 | self.set_focus(win)?; |
| 1035 | } |
| 1036 | |
| 1037 | Ok(()) |
| 1038 | } |
| 1039 | |
| 1040 | fn is_window_visible(&self, window: Window) -> bool { |
| 1041 | let window_mon = self.window_monitor.get(&window).copied().unwrap_or(0); |
| 1042 | |
| 1043 | if let Some(&tags) = self.window_tags.get(&window) { |
| 1044 | let monitor = self.monitors.get(window_mon); |
| 1045 | let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0); |
| 1046 | (tags & selected_tags) != 0 |
| 1047 | } else { |
| 1048 | false |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | fn visible_windows(&self) -> Vec<Window> { |
| 1053 | self.windows |
| 1054 | .iter() |
| 1055 | .filter(|&&w| self.is_window_visible(w)) |
| 1056 | .copied() |
| 1057 | .collect() |
| 1058 | } |
| 1059 | |
| 1060 | fn visible_windows_on_monitor(&self, monitor_index: usize) -> Vec<Window> { |
| 1061 | self.windows |
| 1062 | .iter() |
| 1063 | .filter(|&&w| { |
| 1064 | let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0); |
| 1065 | if window_mon != monitor_index { |
| 1066 | return false; |
| 1067 | } |
| 1068 | if let Some(&tags) = self.window_tags.get(&w) { |
| 1069 | let monitor = self.monitors.get(monitor_index); |
| 1070 | let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0); |
| 1071 | (tags & selected_tags) != 0 |
| 1072 | } else { |
| 1073 | false |
| 1074 | } |
| 1075 | }) |
| 1076 | .copied() |
| 1077 | .collect() |
| 1078 | } |
| 1079 | |
| 1080 | fn get_monitor_at_point(&self, x: i32, y: i32) -> Option<usize> { |
| 1081 | self.monitors |
| 1082 | .iter() |
| 1083 | .position(|mon| mon.contains_point(x, y)) |
| 1084 | } |
| 1085 | |
| 1086 | fn update_window_visibility(&self) -> WmResult<()> { |
| 1087 | for &window in &self.windows { |
| 1088 | if self.is_window_visible(window) { |
| 1089 | self.connection.map_window(window)?; |
| 1090 | } else { |
| 1091 | self.connection.unmap_window(window)?; |
| 1092 | } |
| 1093 | } |
| 1094 | self.connection.flush()?; |
| 1095 | Ok(()) |
| 1096 | } |
| 1097 | |
| 1098 | pub fn view_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 1099 | if tag_index >= self.config.tags.len() { |
| 1100 | return Ok(()); |
| 1101 | } |
| 1102 | |
| 1103 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 1104 | monitor.selected_tags = tag_mask(tag_index); |
| 1105 | } |
| 1106 | |
| 1107 | self.save_selected_tags()?; |
| 1108 | |
| 1109 | self.update_window_visibility()?; |
| 1110 | self.apply_layout()?; |
| 1111 | self.update_bar()?; |
| 1112 | |
| 1113 | let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 1114 | if let Some(&win) = visible.first() { |
| 1115 | self.set_focus(win)?; |
| 1116 | } |
| 1117 | |
| 1118 | Ok(()) |
| 1119 | } |
| 1120 | |
| 1121 | fn save_selected_tags(&self) -> WmResult<()> { |
| 1122 | let net_current_desktop = self.atoms.net_current_desktop; |
| 1123 | |
| 1124 | let selected_tags = self |
| 1125 | .monitors |
| 1126 | .get(self.selected_monitor) |
| 1127 | .map(|m| m.selected_tags) |
| 1128 | .unwrap_or(tag_mask(0)); |
| 1129 | let desktop = selected_tags.trailing_zeros(); |
| 1130 | |
| 1131 | let bytes = (desktop as u32).to_ne_bytes(); |
| 1132 | self.connection.change_property( |
| 1133 | PropMode::REPLACE, |
| 1134 | self.root, |
| 1135 | net_current_desktop, |
| 1136 | AtomEnum::CARDINAL, |
| 1137 | 32, |
| 1138 | 1, |
| 1139 | &bytes, |
| 1140 | )?; |
| 1141 | |
| 1142 | self.connection.flush()?; |
| 1143 | Ok(()) |
| 1144 | } |
| 1145 | |
| 1146 | pub fn move_to_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 1147 | if tag_index >= self.config.tags.len() { |
| 1148 | return Ok(()); |
| 1149 | } |
| 1150 | |
| 1151 | if let Some(focused) = self |
| 1152 | .monitors |
| 1153 | .get(self.selected_monitor) |
| 1154 | .and_then(|m| m.focused_window) |
| 1155 | { |
| 1156 | let mask = tag_mask(tag_index); |
| 1157 | self.window_tags.insert(focused, mask); |
| 1158 | |
| 1159 | let _ = self.save_client_tag(focused, mask); |
| 1160 | |
| 1161 | self.update_window_visibility()?; |
| 1162 | self.apply_layout()?; |
| 1163 | self.update_bar()?; |
| 1164 | } |
| 1165 | |
| 1166 | Ok(()) |
| 1167 | } |
| 1168 | |
| 1169 | pub fn cycle_focus(&mut self, direction: i32) -> WmResult<()> { |
| 1170 | let visible = self.visible_windows(); |
| 1171 | |
| 1172 | if visible.is_empty() { |
| 1173 | return Ok(()); |
| 1174 | } |
| 1175 | |
| 1176 | let current = self |
| 1177 | .monitors |
| 1178 | .get(self.selected_monitor) |
| 1179 | .and_then(|m| m.focused_window); |
| 1180 | |
| 1181 | let next_window = if let Some(current) = current { |
| 1182 | if let Some(current_index) = visible.iter().position(|&w| w == current) { |
| 1183 | let next_index = if direction > 0 { |
| 1184 | (current_index + 1) % visible.len() |
| 1185 | } else { |
| 1186 | (current_index + visible.len() - 1) % visible.len() |
| 1187 | }; |
| 1188 | visible[next_index] |
| 1189 | } else { |
| 1190 | visible[0] |
| 1191 | } |
| 1192 | } else { |
| 1193 | visible[0] |
| 1194 | }; |
| 1195 | |
| 1196 | self.set_focus(next_window)?; |
| 1197 | Ok(()) |
| 1198 | } |
| 1199 | |
| 1200 | fn grab_next_keys(&self, candidates: &[usize], keys_pressed: usize) -> WmResult<()> { |
| 1201 | use std::collections::HashMap; |
| 1202 | use x11rb::protocol::xproto::Keycode; |
| 1203 | |
| 1204 | let setup = self.connection.setup(); |
| 1205 | let min_keycode = setup.min_keycode; |
| 1206 | let max_keycode = setup.max_keycode; |
| 1207 | |
| 1208 | let keyboard_mapping = self.connection.get_keyboard_mapping( |
| 1209 | min_keycode, |
| 1210 | max_keycode - min_keycode + 1, |
| 1211 | )?.reply()?; |
| 1212 | |
| 1213 | let mut keysym_to_keycode: HashMap<keyboard::keysyms::Keysym, Vec<Keycode>> = HashMap::new(); |
| 1214 | let keysyms_per_keycode = keyboard_mapping.keysyms_per_keycode; |
| 1215 | |
| 1216 | for keycode in min_keycode..=max_keycode { |
| 1217 | let index = (keycode - min_keycode) as usize * keysyms_per_keycode as usize; |
| 1218 | for i in 0..keysyms_per_keycode as usize { |
| 1219 | if let Some(&keysym) = keyboard_mapping.keysyms.get(index + i) { |
| 1220 | if keysym != 0 { |
| 1221 | keysym_to_keycode.entry(keysym).or_insert_with(Vec::new).push(keycode); |
| 1222 | } |
| 1223 | } |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | let mut grabbed_keys: HashSet<(u16, Keycode)> = HashSet::new(); |
| 1228 | |
| 1229 | for &candidate_index in candidates { |
| 1230 | let binding = &self.config.keybindings[candidate_index]; |
| 1231 | if keys_pressed < binding.keys.len() { |
| 1232 | let next_key = &binding.keys[keys_pressed]; |
| 1233 | let modifier_mask = keyboard::handlers::modifiers_to_mask(&next_key.modifiers); |
| 1234 | |
| 1235 | if let Some(keycodes) = keysym_to_keycode.get(&next_key.keysym) { |
| 1236 | if let Some(&keycode) = keycodes.first() { |
| 1237 | let key_tuple = (modifier_mask, keycode); |
| 1238 | |
| 1239 | if grabbed_keys.insert(key_tuple) { |
| 1240 | self.connection.grab_key( |
| 1241 | false, |
| 1242 | self.root, |
| 1243 | modifier_mask.into(), |
| 1244 | keycode, |
| 1245 | GrabMode::ASYNC, |
| 1246 | GrabMode::ASYNC, |
| 1247 | )?; |
| 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | if let Some(keycodes) = keysym_to_keycode.get(&keyboard::keysyms::XK_ESCAPE) { |
| 1255 | if let Some(&keycode) = keycodes.first() { |
| 1256 | self.connection.grab_key( |
| 1257 | false, |
| 1258 | self.root, |
| 1259 | ModMask::from(0u16), |
| 1260 | keycode, |
| 1261 | GrabMode::ASYNC, |
| 1262 | GrabMode::ASYNC, |
| 1263 | )?; |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | self.connection.flush()?; |
| 1268 | Ok(()) |
| 1269 | } |
| 1270 | |
| 1271 | fn ungrab_chord_keys(&self) -> WmResult<()> { |
| 1272 | self.connection.ungrab_key(x11rb::protocol::xproto::Grab::ANY, self.root, ModMask::ANY)?; |
| 1273 | keyboard::setup_keybinds(&self.connection, self.root, &self.config.keybindings)?; |
| 1274 | self.connection.flush()?; |
| 1275 | Ok(()) |
| 1276 | } |
| 1277 | |
| 1278 | pub fn set_focus(&mut self, window: Window) -> WmResult<()> { |
| 1279 | let old_focused = self.previous_focused; |
| 1280 | |
| 1281 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 1282 | monitor.focused_window = Some(window); |
| 1283 | } |
| 1284 | |
| 1285 | self.connection |
| 1286 | .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?; |
| 1287 | self.connection.flush()?; |
| 1288 | |
| 1289 | self.update_focus_visuals(old_focused, window)?; |
| 1290 | self.previous_focused = Some(window); |
| 1291 | Ok(()) |
| 1292 | } |
| 1293 | |
| 1294 | fn update_focus_visuals( |
| 1295 | &self, |
| 1296 | old_focused: Option<Window>, |
| 1297 | new_focused: Window, |
| 1298 | ) -> WmResult<()> { |
| 1299 | if let Some(old_win) = old_focused { |
| 1300 | if old_win != new_focused { |
| 1301 | self.connection.configure_window( |
| 1302 | old_win, |
| 1303 | &ConfigureWindowAux::new().border_width(self.config.border_width), |
| 1304 | )?; |
| 1305 | |
| 1306 | self.connection.change_window_attributes( |
| 1307 | old_win, |
| 1308 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), |
| 1309 | )?; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | self.connection.configure_window( |
| 1314 | new_focused, |
| 1315 | &ConfigureWindowAux::new().border_width(self.config.border_width), |
| 1316 | )?; |
| 1317 | |
| 1318 | self.connection.change_window_attributes( |
| 1319 | new_focused, |
| 1320 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused), |
| 1321 | )?; |
| 1322 | |
| 1323 | self.connection.flush()?; |
| 1324 | Ok(()) |
| 1325 | } |
| 1326 | |
| 1327 | fn move_mouse(&mut self, window: Window) -> WmResult<()> { |
| 1328 | self.floating_windows.insert(window); |
| 1329 | |
| 1330 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 1331 | |
| 1332 | self.connection |
| 1333 | .grab_pointer( |
| 1334 | false, |
| 1335 | self.root, |
| 1336 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), |
| 1337 | GrabMode::ASYNC, |
| 1338 | GrabMode::ASYNC, |
| 1339 | x11rb::NONE, |
| 1340 | x11rb::NONE, |
| 1341 | x11rb::CURRENT_TIME, |
| 1342 | )? |
| 1343 | .reply()?; |
| 1344 | |
| 1345 | let pointer = self.connection.query_pointer(self.root)?.reply()?; |
| 1346 | let (start_x, start_y) = (pointer.root_x, pointer.root_y); |
| 1347 | |
| 1348 | self.connection.configure_window( |
| 1349 | window, |
| 1350 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 1351 | )?; |
| 1352 | |
| 1353 | loop { |
| 1354 | let event = self.connection.wait_for_event()?; |
| 1355 | match event { |
| 1356 | Event::MotionNotify(e) => { |
| 1357 | let new_x = geometry.x + (e.root_x - start_x); |
| 1358 | let new_y = geometry.y + (e.root_y - start_y); |
| 1359 | self.connection.configure_window( |
| 1360 | window, |
| 1361 | &ConfigureWindowAux::new().x(new_x as i32).y(new_y as i32), |
| 1362 | )?; |
| 1363 | self.connection.flush()?; |
| 1364 | } |
| 1365 | Event::ButtonRelease(_) => break, |
| 1366 | _ => {} |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | self.connection |
| 1371 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 1372 | .check()?; |
| 1373 | self.connection |
| 1374 | .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? |
| 1375 | .check()?; |
| 1376 | |
| 1377 | Ok(()) |
| 1378 | } |
| 1379 | |
| 1380 | fn resize_mouse(&mut self, window: Window) -> WmResult<()> { |
| 1381 | self.floating_windows.insert(window); |
| 1382 | |
| 1383 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 1384 | |
| 1385 | self.connection.warp_pointer( |
| 1386 | x11rb::NONE, |
| 1387 | window, |
| 1388 | 0, |
| 1389 | 0, |
| 1390 | 0, |
| 1391 | 0, |
| 1392 | geometry.width as i16, |
| 1393 | geometry.height as i16, |
| 1394 | )?; |
| 1395 | |
| 1396 | self.connection |
| 1397 | .grab_pointer( |
| 1398 | false, |
| 1399 | self.root, |
| 1400 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), |
| 1401 | GrabMode::ASYNC, |
| 1402 | GrabMode::ASYNC, |
| 1403 | x11rb::NONE, |
| 1404 | x11rb::NONE, |
| 1405 | x11rb::CURRENT_TIME, |
| 1406 | )? |
| 1407 | .reply()?; |
| 1408 | |
| 1409 | self.connection.configure_window( |
| 1410 | window, |
| 1411 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 1412 | )?; |
| 1413 | |
| 1414 | loop { |
| 1415 | let event = self.connection.wait_for_event()?; |
| 1416 | match event { |
| 1417 | Event::MotionNotify(e) => { |
| 1418 | let new_width = (e.root_x - geometry.x).max(1) as u32; |
| 1419 | let new_height = (e.root_y - geometry.y).max(1) as u32; |
| 1420 | |
| 1421 | self.connection.configure_window( |
| 1422 | window, |
| 1423 | &ConfigureWindowAux::new() |
| 1424 | .width(new_width) |
| 1425 | .height(new_height), |
| 1426 | )?; |
| 1427 | self.connection.flush()?; |
| 1428 | } |
| 1429 | Event::ButtonRelease(_) => break, |
| 1430 | _ => {} |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | self.connection |
| 1435 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 1436 | .check()?; |
| 1437 | self.connection |
| 1438 | .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? |
| 1439 | .check()?; |
| 1440 | |
| 1441 | Ok(()) |
| 1442 | } |
| 1443 | |
| 1444 | fn handle_event(&mut self, event: Event) -> WmResult<Option<bool>> { |
| 1445 | match event { |
| 1446 | Event::MapRequest(event) => { |
| 1447 | let attrs = match self.connection.get_window_attributes(event.window)?.reply() { |
| 1448 | Ok(attrs) => attrs, |
| 1449 | Err(_) => return Ok(None), |
| 1450 | }; |
| 1451 | |
| 1452 | if attrs.override_redirect { |
| 1453 | return Ok(None); |
| 1454 | } |
| 1455 | |
| 1456 | if self.windows.contains(&event.window) { |
| 1457 | return Ok(None); |
| 1458 | } |
| 1459 | |
| 1460 | if let Ok(geom) = self.connection.get_geometry(event.window)?.reply() { |
| 1461 | self.window_geometries |
| 1462 | .insert(event.window, (geom.x, geom.y, geom.width, geom.height)); |
| 1463 | } |
| 1464 | |
| 1465 | self.connection.map_window(event.window)?; |
| 1466 | self.connection.change_window_attributes( |
| 1467 | event.window, |
| 1468 | &ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW), |
| 1469 | )?; |
| 1470 | |
| 1471 | let selected_tags = self |
| 1472 | .monitors |
| 1473 | .get(self.selected_monitor) |
| 1474 | .map(|m| m.selected_tags) |
| 1475 | .unwrap_or(tag_mask(0)); |
| 1476 | |
| 1477 | self.windows.push(event.window); |
| 1478 | self.window_tags.insert(event.window, selected_tags); |
| 1479 | self.window_monitor |
| 1480 | .insert(event.window, self.selected_monitor); |
| 1481 | self.set_wm_state(event.window, 1)?; |
| 1482 | let _ = self.save_client_tag(event.window, selected_tags); |
| 1483 | |
| 1484 | self.apply_layout()?; |
| 1485 | self.update_bar()?; |
| 1486 | self.set_focus(event.window)?; |
| 1487 | } |
| 1488 | Event::UnmapNotify(event) => { |
| 1489 | if self.windows.contains(&event.window) && self.is_window_visible(event.window) { |
| 1490 | self.remove_window(event.window)?; |
| 1491 | } |
| 1492 | } |
| 1493 | Event::DestroyNotify(event) => { |
| 1494 | if self.windows.contains(&event.window) { |
| 1495 | self.remove_window(event.window)?; |
| 1496 | } |
| 1497 | } |
| 1498 | Event::EnterNotify(event) => { |
| 1499 | if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL { |
| 1500 | return Ok(None); |
| 1501 | } |
| 1502 | if self.windows.contains(&event.event) { |
| 1503 | self.set_focus(event.event)?; |
| 1504 | } |
| 1505 | } |
| 1506 | Event::MotionNotify(event) => { |
| 1507 | if event.event != self.root { |
| 1508 | return Ok(None); |
| 1509 | } |
| 1510 | |
| 1511 | if let Some(monitor_index) = |
| 1512 | self.get_monitor_at_point(event.root_x as i32, event.root_y as i32) |
| 1513 | { |
| 1514 | if monitor_index != self.selected_monitor { |
| 1515 | self.selected_monitor = monitor_index; |
| 1516 | self.update_bar()?; |
| 1517 | |
| 1518 | let visible = self.visible_windows_on_monitor(monitor_index); |
| 1519 | if let Some(&win) = visible.first() { |
| 1520 | self.set_focus(win)?; |
| 1521 | } |
| 1522 | } |
| 1523 | } |
| 1524 | } |
| 1525 | Event::KeyPress(event) => { |
| 1526 | let result = keyboard::handle_key_press( |
| 1527 | event, |
| 1528 | &self.config.keybindings, |
| 1529 | &self.keychord_state, |
| 1530 | &self.connection, |
| 1531 | )?; |
| 1532 | |
| 1533 | match result { |
| 1534 | keyboard::handlers::KeychordResult::Completed(action, arg) => { |
| 1535 | self.keychord_state = keyboard::handlers::KeychordState::Idle; |
| 1536 | self.ungrab_chord_keys()?; |
| 1537 | self.update_bar()?; |
| 1538 | |
| 1539 | match action { |
| 1540 | KeyAction::Quit => return Ok(Some(false)), |
| 1541 | KeyAction::Restart => return Ok(Some(true)), |
| 1542 | _ => self.handle_key_action(action, &arg)?, |
| 1543 | } |
| 1544 | } |
| 1545 | keyboard::handlers::KeychordResult::InProgress(candidates) => { |
| 1546 | let keys_pressed = match &self.keychord_state { |
| 1547 | keyboard::handlers::KeychordState::Idle => 1, |
| 1548 | keyboard::handlers::KeychordState::InProgress { keys_pressed, .. } => keys_pressed + 1, |
| 1549 | }; |
| 1550 | |
| 1551 | self.keychord_state = keyboard::handlers::KeychordState::InProgress { |
| 1552 | candidates: candidates.clone(), |
| 1553 | keys_pressed, |
| 1554 | }; |
| 1555 | |
| 1556 | self.grab_next_keys(&candidates, keys_pressed)?; |
| 1557 | self.update_bar()?; |
| 1558 | } |
| 1559 | keyboard::handlers::KeychordResult::Cancelled | keyboard::handlers::KeychordResult::None => { |
| 1560 | self.keychord_state = keyboard::handlers::KeychordState::Idle; |
| 1561 | self.ungrab_chord_keys()?; |
| 1562 | self.update_bar()?; |
| 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 | Event::ButtonPress(event) => { |
| 1567 | let is_bar_click = self |
| 1568 | .bars |
| 1569 | .iter() |
| 1570 | .enumerate() |
| 1571 | .find(|(_, bar)| bar.window() == event.event); |
| 1572 | |
| 1573 | if let Some((monitor_index, bar)) = is_bar_click { |
| 1574 | if let Some(tag_index) = bar.handle_click(event.event_x) { |
| 1575 | if monitor_index != self.selected_monitor { |
| 1576 | self.selected_monitor = monitor_index; |
| 1577 | } |
| 1578 | self.view_tag(tag_index)?; |
| 1579 | } |
| 1580 | } else if event.child != x11rb::NONE { |
| 1581 | self.set_focus(event.child)?; |
| 1582 | |
| 1583 | if event.detail == ButtonIndex::M1.into() { |
| 1584 | self.move_mouse(event.child)?; |
| 1585 | } else if event.detail == ButtonIndex::M3.into() { |
| 1586 | self.resize_mouse(event.child)?; |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 | Event::Expose(event) => { |
| 1591 | for bar in &mut self.bars { |
| 1592 | if event.window == bar.window() { |
| 1593 | bar.invalidate(); |
| 1594 | self.update_bar()?; |
| 1595 | break; |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | _ => {} |
| 1600 | } |
| 1601 | Ok(None) |
| 1602 | } |
| 1603 | |
| 1604 | fn apply_layout(&self) -> WmResult<()> { |
| 1605 | if self.layout.name() == LayoutType::Normie.as_str() { |
| 1606 | return Ok(()); |
| 1607 | } |
| 1608 | |
| 1609 | let border_width = if self.fullscreen_enabled { 0 } else { self.config.border_width }; |
| 1610 | |
| 1611 | let gaps = if self.gaps_enabled && !self.fullscreen_enabled { |
| 1612 | GapConfig { |
| 1613 | inner_horizontal: self.config.gap_inner_horizontal, |
| 1614 | inner_vertical: self.config.gap_inner_vertical, |
| 1615 | outer_horizontal: self.config.gap_outer_horizontal, |
| 1616 | outer_vertical: self.config.gap_outer_vertical, |
| 1617 | } |
| 1618 | } else { |
| 1619 | GapConfig { |
| 1620 | inner_horizontal: 0, |
| 1621 | inner_vertical: 0, |
| 1622 | outer_horizontal: 0, |
| 1623 | outer_vertical: 0, |
| 1624 | } |
| 1625 | }; |
| 1626 | |
| 1627 | for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 1628 | let visible: Vec<Window> = self |
| 1629 | .windows |
| 1630 | .iter() |
| 1631 | .filter(|&&w| { |
| 1632 | let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0); |
| 1633 | if window_mon != monitor_index { |
| 1634 | return false; |
| 1635 | } |
| 1636 | if self.floating_windows.contains(&w) { |
| 1637 | return false; |
| 1638 | } |
| 1639 | if let Some(&tags) = self.window_tags.get(&w) { |
| 1640 | (tags & monitor.selected_tags) != 0 |
| 1641 | } else { |
| 1642 | false |
| 1643 | } |
| 1644 | }) |
| 1645 | .copied() |
| 1646 | .collect(); |
| 1647 | |
| 1648 | let bar_height = if self.fullscreen_enabled { |
| 1649 | 0 |
| 1650 | } else { |
| 1651 | self.bars |
| 1652 | .get(monitor_index) |
| 1653 | .map(|b| b.height() as u32) |
| 1654 | .unwrap_or(0) |
| 1655 | }; |
| 1656 | let usable_height = monitor.height.saturating_sub(bar_height); |
| 1657 | |
| 1658 | let geometries = self |
| 1659 | .layout |
| 1660 | .arrange(&visible, monitor.width, usable_height, &gaps); |
| 1661 | |
| 1662 | for (window, geometry) in visible.iter().zip(geometries.iter()) { |
| 1663 | let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
| 1664 | let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
| 1665 | |
| 1666 | let adjusted_x = geometry.x_coordinate + monitor.x; |
| 1667 | let adjusted_y = geometry.y_coordinate + monitor.y + bar_height as i32; |
| 1668 | |
| 1669 | self.connection.configure_window( |
| 1670 | *window, |
| 1671 | &ConfigureWindowAux::new() |
| 1672 | .x(adjusted_x) |
| 1673 | .y(adjusted_y) |
| 1674 | .width(adjusted_width) |
| 1675 | .height(adjusted_height), |
| 1676 | )?; |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | self.connection.flush()?; |
| 1681 | Ok(()) |
| 1682 | } |
| 1683 | |
| 1684 | pub fn change_layout<L: Layout + 'static>(&mut self, new_layout: L) -> WmResult<()> { |
| 1685 | self.layout = Box::new(new_layout); |
| 1686 | self.apply_layout()?; |
| 1687 | Ok(()) |
| 1688 | } |
| 1689 | |
| 1690 | fn remove_window(&mut self, window: Window) -> WmResult<()> { |
| 1691 | let initial_count = self.windows.len(); |
| 1692 | self.windows.retain(|&w| w != window); |
| 1693 | self.window_tags.remove(&window); |
| 1694 | self.window_monitor.remove(&window); |
| 1695 | self.window_geometries.remove(&window); |
| 1696 | self.floating_windows.remove(&window); |
| 1697 | |
| 1698 | if self.windows.len() < initial_count { |
| 1699 | let focused = self |
| 1700 | .monitors |
| 1701 | .get(self.selected_monitor) |
| 1702 | .and_then(|m| m.focused_window); |
| 1703 | if focused == Some(window) { |
| 1704 | let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 1705 | if let Some(&new_win) = visible.last() { |
| 1706 | self.set_focus(new_win)?; |
| 1707 | } else if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 1708 | monitor.focused_window = None; |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | self.apply_layout()?; |
| 1713 | self.update_bar()?; |
| 1714 | } |
| 1715 | Ok(()) |
| 1716 | } |
| 1717 | } |