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