| 1 | use crate::Config; |
| 2 | use crate::bar::Bar; |
| 3 | use crate::errors::WmError; |
| 4 | use crate::keyboard::{self, Arg, KeyAction, handlers}; |
| 5 | use crate::layout::GapConfig; |
| 6 | use crate::layout::tiling::TilingLayout; |
| 7 | use crate::layout::{Layout, layout_from_str, next_layout}; |
| 8 | use crate::monitor::{Monitor, detect_monitors}; |
| 9 | use std::collections::HashSet; |
| 10 | use x11rb::cursor::Handle as CursorHandle; |
| 11 | |
| 12 | use x11rb::connection::Connection; |
| 13 | use x11rb::protocol::Event; |
| 14 | use x11rb::protocol::xproto::*; |
| 15 | use x11rb::rust_connection::RustConnection; |
| 16 | |
| 17 | pub type TagMask = u32; |
| 18 | pub fn tag_mask(tag: usize) -> TagMask { |
| 19 | 1 << tag |
| 20 | } |
| 21 | |
| 22 | struct AtomCache { |
| 23 | net_current_desktop: Atom, |
| 24 | net_client_info: Atom, |
| 25 | wm_state: Atom, |
| 26 | } |
| 27 | |
| 28 | impl AtomCache { |
| 29 | fn new(connection: &RustConnection) -> WmResult<Self> { |
| 30 | let net_current_desktop = connection |
| 31 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| 32 | .reply()? |
| 33 | .atom; |
| 34 | |
| 35 | let net_client_info = connection |
| 36 | .intern_atom(false, b"_NET_CLIENT_INFO")? |
| 37 | .reply()? |
| 38 | .atom; |
| 39 | |
| 40 | let wm_state = connection.intern_atom(false, b"WM_STATE")?.reply()?.atom; |
| 41 | |
| 42 | Ok(Self { |
| 43 | net_current_desktop, |
| 44 | net_client_info, |
| 45 | wm_state, |
| 46 | }) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | pub struct WindowManager { |
| 51 | config: Config, |
| 52 | connection: RustConnection, |
| 53 | screen_number: usize, |
| 54 | root: Window, |
| 55 | screen: Screen, |
| 56 | windows: Vec<Window>, |
| 57 | layout: Box<dyn Layout>, |
| 58 | window_tags: std::collections::HashMap<Window, TagMask>, |
| 59 | window_monitor: std::collections::HashMap<Window, usize>, |
| 60 | window_geometries: std::collections::HashMap<Window, (i16, i16, u16, u16)>, |
| 61 | gaps_enabled: bool, |
| 62 | fullscreen_window: Option<Window>, |
| 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 | } |
| 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 | fullscreen_window: None, |
| 171 | floating_windows: HashSet::new(), |
| 172 | bars, |
| 173 | monitors, |
| 174 | selected_monitor: 0, |
| 175 | atoms, |
| 176 | previous_focused: None, |
| 177 | display, |
| 178 | font, |
| 179 | }; |
| 180 | |
| 181 | window_manager.scan_existing_windows()?; |
| 182 | window_manager.update_bar()?; |
| 183 | |
| 184 | Ok(window_manager) |
| 185 | } |
| 186 | |
| 187 | fn get_saved_selected_tags( |
| 188 | connection: &RustConnection, |
| 189 | root: Window, |
| 190 | tag_count: usize, |
| 191 | ) -> WmResult<TagMask> { |
| 192 | let net_current_desktop = connection |
| 193 | .intern_atom(false, b"_NET_CURRENT_DESKTOP")? |
| 194 | .reply()? |
| 195 | .atom; |
| 196 | |
| 197 | match connection |
| 198 | .get_property(false, root, net_current_desktop, AtomEnum::CARDINAL, 0, 1)? |
| 199 | .reply() |
| 200 | { |
| 201 | Ok(prop) if prop.value.len() >= 4 => { |
| 202 | let desktop = u32::from_ne_bytes([ |
| 203 | prop.value[0], |
| 204 | prop.value[1], |
| 205 | prop.value[2], |
| 206 | prop.value[3], |
| 207 | ]); |
| 208 | if desktop < tag_count as u32 { |
| 209 | let mask = tag_mask(desktop as usize); |
| 210 | return Ok(mask); |
| 211 | } |
| 212 | } |
| 213 | Ok(_) => {} |
| 214 | Err(e) => { |
| 215 | eprintln!("No _NET_CURRENT_DESKTOP property ({})", e); |
| 216 | } |
| 217 | } |
| 218 | Ok(tag_mask(0)) |
| 219 | } |
| 220 | |
| 221 | fn scan_existing_windows(&mut self) -> WmResult<()> { |
| 222 | let tree = self.connection.query_tree(self.root)?.reply()?; |
| 223 | let net_client_info = self.atoms.net_client_info; |
| 224 | let wm_state_atom = self.atoms.wm_state; |
| 225 | |
| 226 | for &window in &tree.children { |
| 227 | if self.bars.iter().any(|bar| bar.window() == window) { |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | let Ok(attrs) = self.connection.get_window_attributes(window)?.reply() else { |
| 232 | continue; |
| 233 | }; |
| 234 | |
| 235 | if attrs.override_redirect { |
| 236 | continue; |
| 237 | } |
| 238 | |
| 239 | if attrs.map_state == MapState::VIEWABLE { |
| 240 | let tag = self.get_saved_tag(window, net_client_info)?; |
| 241 | self.windows.push(window); |
| 242 | self.window_tags.insert(window, tag); |
| 243 | self.window_monitor.insert(window, self.selected_monitor); |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | if attrs.map_state == MapState::UNMAPPED { |
| 248 | let has_wm_state = self |
| 249 | .connection |
| 250 | .get_property(false, window, wm_state_atom, AtomEnum::ANY, 0, 2)? |
| 251 | .reply() |
| 252 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 253 | |
| 254 | if !has_wm_state { |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | let has_wm_class = self |
| 259 | .connection |
| 260 | .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)? |
| 261 | .reply() |
| 262 | .is_ok_and(|prop| !prop.value.is_empty()); |
| 263 | |
| 264 | if has_wm_class { |
| 265 | let tag = self.get_saved_tag(window, net_client_info)?; |
| 266 | self.windows.push(window); |
| 267 | self.window_tags.insert(window, tag); |
| 268 | self.window_monitor.insert(window, self.selected_monitor); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | if let Some(&first) = self.windows.first() { |
| 274 | self.set_focus(first)?; |
| 275 | } |
| 276 | |
| 277 | self.apply_layout()?; |
| 278 | Ok(()) |
| 279 | } |
| 280 | |
| 281 | fn get_saved_tag(&self, window: Window, net_client_info: Atom) -> WmResult<TagMask> { |
| 282 | match self |
| 283 | .connection |
| 284 | .get_property(false, window, net_client_info, AtomEnum::CARDINAL, 0, 2)? |
| 285 | .reply() |
| 286 | { |
| 287 | Ok(prop) if prop.value.len() >= 4 => { |
| 288 | let tags = u32::from_ne_bytes([ |
| 289 | prop.value[0], |
| 290 | prop.value[1], |
| 291 | prop.value[2], |
| 292 | prop.value[3], |
| 293 | ]); |
| 294 | |
| 295 | if tags != 0 && tags < (1 << self.config.tags.len()) { |
| 296 | return Ok(tags); |
| 297 | } |
| 298 | } |
| 299 | Ok(_) => {} |
| 300 | Err(e) => { |
| 301 | eprintln!("No _NET_CLIENT_INFO property ({})", e); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | Ok(self |
| 306 | .monitors |
| 307 | .get(self.selected_monitor) |
| 308 | .map(|m| m.selected_tags) |
| 309 | .unwrap_or(tag_mask(0))) |
| 310 | } |
| 311 | |
| 312 | fn save_client_tag(&self, window: Window, tag: TagMask) -> WmResult<()> { |
| 313 | let net_client_info = self.atoms.net_client_info; |
| 314 | |
| 315 | let bytes = tag.to_ne_bytes().to_vec(); |
| 316 | |
| 317 | self.connection.change_property( |
| 318 | PropMode::REPLACE, |
| 319 | window, |
| 320 | net_client_info, |
| 321 | AtomEnum::CARDINAL, |
| 322 | 32, |
| 323 | 1, |
| 324 | &bytes, |
| 325 | )?; |
| 326 | |
| 327 | self.connection.flush()?; |
| 328 | Ok(()) |
| 329 | } |
| 330 | |
| 331 | fn set_wm_state(&self, window: Window, state: u32) -> WmResult<()> { |
| 332 | let wm_state_atom = self.atoms.wm_state; |
| 333 | |
| 334 | let data = [state, 0u32]; |
| 335 | let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); |
| 336 | |
| 337 | self.connection.change_property( |
| 338 | PropMode::REPLACE, |
| 339 | window, |
| 340 | wm_state_atom, |
| 341 | wm_state_atom, |
| 342 | 32, |
| 343 | 2, |
| 344 | &bytes, |
| 345 | )?; |
| 346 | |
| 347 | self.connection.flush()?; |
| 348 | Ok(()) |
| 349 | } |
| 350 | |
| 351 | pub fn run(&mut self) -> WmResult<bool> { |
| 352 | println!("oxwm started on display {}", self.screen_number); |
| 353 | |
| 354 | keyboard::setup_keybinds(&self.connection, self.root, &self.config.keybindings)?; |
| 355 | self.update_bar()?; |
| 356 | |
| 357 | loop { |
| 358 | while let Some(event) = self.connection.poll_for_event()? { |
| 359 | if let Some(should_restart) = self.handle_event(event)? { |
| 360 | return Ok(should_restart); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | if let Some(bar) = self.bars.get_mut(self.selected_monitor) { |
| 365 | bar.update_blocks(); |
| 366 | } |
| 367 | |
| 368 | if self.bars.iter().any(|bar| bar.needs_redraw()) { |
| 369 | self.update_bar()?; |
| 370 | } |
| 371 | |
| 372 | std::thread::sleep(std::time::Duration::from_millis(100)); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | fn toggle_floating(&mut self) -> WmResult<()> { |
| 377 | if let Some(focused) = self |
| 378 | .monitors |
| 379 | .get(self.selected_monitor) |
| 380 | .and_then(|m| m.focused_window) |
| 381 | { |
| 382 | if self.floating_windows.contains(&focused) { |
| 383 | self.floating_windows.remove(&focused); |
| 384 | self.apply_layout()?; |
| 385 | } else { |
| 386 | let float_width = (self.screen.width_in_pixels / 2) as u32; |
| 387 | let float_height = (self.screen.height_in_pixels / 2) as u32; |
| 388 | |
| 389 | let border_width = self.config.border_width; |
| 390 | |
| 391 | let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32; |
| 392 | let center_height = |
| 393 | ((self.screen.height_in_pixels - float_height as u16) / 2) as i32; |
| 394 | |
| 395 | self.connection.configure_window( |
| 396 | focused, |
| 397 | &ConfigureWindowAux::new() |
| 398 | .x(center_width) |
| 399 | .y(center_height) |
| 400 | .width(float_width) |
| 401 | .height(float_height) |
| 402 | .border_width(border_width) |
| 403 | .stack_mode(StackMode::ABOVE), |
| 404 | )?; |
| 405 | |
| 406 | self.floating_windows.insert(focused); |
| 407 | self.apply_layout()?; |
| 408 | self.connection.flush()?; |
| 409 | } |
| 410 | } |
| 411 | Ok(()) |
| 412 | } |
| 413 | |
| 414 | fn smart_move_window(&mut self, direction: i32) -> WmResult<()> { |
| 415 | let focused = match self |
| 416 | .monitors |
| 417 | .get(self.selected_monitor) |
| 418 | .and_then(|m| m.focused_window) |
| 419 | { |
| 420 | Some(win) => win, |
| 421 | None => return Ok(()), |
| 422 | }; |
| 423 | |
| 424 | if self.fullscreen_window == Some(focused) { |
| 425 | return Ok(()); |
| 426 | } |
| 427 | |
| 428 | if !self.floating_windows.contains(&focused) { |
| 429 | let float_width = (self.screen.width_in_pixels / 2) as u32; |
| 430 | let float_height = (self.screen.height_in_pixels / 2) as u32; |
| 431 | let border_width = self.config.border_width; |
| 432 | let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32; |
| 433 | let center_height = ((self.screen.height_in_pixels - float_height as u16) / 2) as i32; |
| 434 | |
| 435 | self.connection.configure_window( |
| 436 | focused, |
| 437 | &ConfigureWindowAux::new() |
| 438 | .x(center_width) |
| 439 | .y(center_height) |
| 440 | .width(float_width) |
| 441 | .height(float_height) |
| 442 | .border_width(border_width) |
| 443 | .stack_mode(StackMode::ABOVE), |
| 444 | )?; |
| 445 | self.floating_windows.insert(focused); |
| 446 | } |
| 447 | |
| 448 | let current_geom = match self.connection.get_geometry(focused)?.reply() { |
| 449 | Ok(geom) => geom, |
| 450 | Err(_) => return Ok(()), |
| 451 | }; |
| 452 | |
| 453 | let c_x = current_geom.x as i32; |
| 454 | let c_y = current_geom.y as i32; |
| 455 | let c_width = current_geom.width as i32; |
| 456 | let c_height = current_geom.height as i32; |
| 457 | |
| 458 | let monitor = match self.monitors.get(self.selected_monitor) { |
| 459 | Some(m) => m, |
| 460 | None => return Ok(()), |
| 461 | }; |
| 462 | |
| 463 | let (gap_ih, gap_iv, gap_oh, gap_ov) = if self.gaps_enabled { |
| 464 | ( |
| 465 | self.config.gap_inner_horizontal as i32, |
| 466 | self.config.gap_inner_vertical as i32, |
| 467 | self.config.gap_outer_horizontal as i32, |
| 468 | self.config.gap_outer_vertical as i32, |
| 469 | ) |
| 470 | } else { |
| 471 | (0, 0, 0, 0) |
| 472 | }; |
| 473 | |
| 474 | let (new_x, new_y) = match direction { |
| 475 | 0 => { |
| 476 | // UP |
| 477 | let mut target = i32::MIN; |
| 478 | let top = c_y; |
| 479 | let mut ny = c_y - (monitor.height as i32 / 4); |
| 480 | |
| 481 | for &other_window in &self.windows { |
| 482 | if other_window == focused { |
| 483 | continue; |
| 484 | } |
| 485 | if !self.floating_windows.contains(&other_window) { |
| 486 | continue; |
| 487 | } |
| 488 | if !self.is_window_visible(other_window) { |
| 489 | continue; |
| 490 | } |
| 491 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 492 | if other_mon != self.selected_monitor { |
| 493 | continue; |
| 494 | } |
| 495 | |
| 496 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 497 | Ok(geom) => geom, |
| 498 | Err(_) => continue, |
| 499 | }; |
| 500 | |
| 501 | let o_x = other_geom.x as i32; |
| 502 | let o_y = other_geom.y as i32; |
| 503 | let o_width = other_geom.width as i32; |
| 504 | let o_height = other_geom.height as i32; |
| 505 | |
| 506 | if c_x + c_width < o_x || c_x > o_x + o_width { |
| 507 | continue; |
| 508 | } |
| 509 | |
| 510 | let bottom = o_y + o_height + gap_iv; |
| 511 | if top > bottom && ny < bottom { |
| 512 | target = target.max(bottom); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if target != i32::MIN { |
| 517 | ny = target; |
| 518 | } |
| 519 | ny = ny.max(monitor.y as i32 + gap_ov); |
| 520 | (c_x, ny) |
| 521 | } |
| 522 | 1 => { |
| 523 | // DOWN |
| 524 | let mut target = i32::MAX; |
| 525 | let bottom = c_y + c_height; |
| 526 | let mut ny = c_y + (monitor.height as i32 / 4); |
| 527 | |
| 528 | for &other_window in &self.windows { |
| 529 | if other_window == focused { |
| 530 | continue; |
| 531 | } |
| 532 | if !self.floating_windows.contains(&other_window) { |
| 533 | continue; |
| 534 | } |
| 535 | if !self.is_window_visible(other_window) { |
| 536 | continue; |
| 537 | } |
| 538 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 539 | if other_mon != self.selected_monitor { |
| 540 | continue; |
| 541 | } |
| 542 | |
| 543 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 544 | Ok(geom) => geom, |
| 545 | Err(_) => continue, |
| 546 | }; |
| 547 | |
| 548 | let o_x = other_geom.x as i32; |
| 549 | let o_y = other_geom.y as i32; |
| 550 | let o_width = other_geom.width as i32; |
| 551 | |
| 552 | if c_x + c_width < o_x || c_x > o_x + o_width { |
| 553 | continue; |
| 554 | } |
| 555 | |
| 556 | let top = o_y - gap_iv; |
| 557 | if bottom < top && (ny + c_height) > top { |
| 558 | target = target.min(top - c_height); |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | if target != i32::MAX { |
| 563 | ny = target; |
| 564 | } |
| 565 | ny = ny.min(monitor.y as i32 + monitor.height as i32 - c_height - gap_ov); |
| 566 | (c_x, ny) |
| 567 | } |
| 568 | 2 => { |
| 569 | // LEFT |
| 570 | let mut target = i32::MIN; |
| 571 | let left = c_x; |
| 572 | let mut nx = c_x - (monitor.width as i32 / 6); |
| 573 | |
| 574 | for &other_window in &self.windows { |
| 575 | if other_window == focused { |
| 576 | continue; |
| 577 | } |
| 578 | if !self.floating_windows.contains(&other_window) { |
| 579 | continue; |
| 580 | } |
| 581 | if !self.is_window_visible(other_window) { |
| 582 | continue; |
| 583 | } |
| 584 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 585 | if other_mon != self.selected_monitor { |
| 586 | continue; |
| 587 | } |
| 588 | |
| 589 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 590 | Ok(geom) => geom, |
| 591 | Err(_) => continue, |
| 592 | }; |
| 593 | |
| 594 | let o_x = other_geom.x as i32; |
| 595 | let o_y = other_geom.y as i32; |
| 596 | let o_width = other_geom.width as i32; |
| 597 | let o_height = other_geom.height as i32; |
| 598 | |
| 599 | if c_y + c_height < o_y || c_y > o_y + o_height { |
| 600 | continue; |
| 601 | } |
| 602 | |
| 603 | let right = o_x + o_width + gap_ih; |
| 604 | if left > right && nx < right { |
| 605 | target = target.max(right); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | if target != i32::MIN { |
| 610 | nx = target; |
| 611 | } |
| 612 | nx = nx.max(monitor.x as i32 + gap_oh); |
| 613 | (nx, c_y) |
| 614 | } |
| 615 | 3 => { |
| 616 | // RIGHT |
| 617 | let mut target = i32::MAX; |
| 618 | let right = c_x + c_width; |
| 619 | let mut nx = c_x + (monitor.width as i32 / 6); |
| 620 | |
| 621 | for &other_window in &self.windows { |
| 622 | if other_window == focused { |
| 623 | continue; |
| 624 | } |
| 625 | if !self.floating_windows.contains(&other_window) { |
| 626 | continue; |
| 627 | } |
| 628 | if !self.is_window_visible(other_window) { |
| 629 | continue; |
| 630 | } |
| 631 | let other_mon = self.window_monitor.get(&other_window).copied().unwrap_or(0); |
| 632 | if other_mon != self.selected_monitor { |
| 633 | continue; |
| 634 | } |
| 635 | |
| 636 | let other_geom = match self.connection.get_geometry(other_window)?.reply() { |
| 637 | Ok(geom) => geom, |
| 638 | Err(_) => continue, |
| 639 | }; |
| 640 | |
| 641 | let o_x = other_geom.x as i32; |
| 642 | let o_y = other_geom.y as i32; |
| 643 | let o_height = other_geom.height as i32; |
| 644 | |
| 645 | if c_y + c_height < o_y || c_y > o_y + o_height { |
| 646 | continue; |
| 647 | } |
| 648 | |
| 649 | let left = o_x - gap_ih; |
| 650 | if right < left && (nx + c_width) > left { |
| 651 | target = target.min(left - c_width); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | if target != i32::MAX { |
| 656 | nx = target; |
| 657 | } |
| 658 | nx = nx.min(monitor.x as i32 + monitor.width as i32 - c_width - gap_oh); |
| 659 | (nx, c_y) |
| 660 | } |
| 661 | _ => return Ok(()), |
| 662 | }; |
| 663 | |
| 664 | self.connection.configure_window( |
| 665 | focused, |
| 666 | &ConfigureWindowAux::new() |
| 667 | .x(new_x) |
| 668 | .y(new_y) |
| 669 | .stack_mode(StackMode::ABOVE), |
| 670 | )?; |
| 671 | |
| 672 | self.connection.flush()?; |
| 673 | Ok(()) |
| 674 | } |
| 675 | |
| 676 | fn exchange_client(&mut self, direction: i32) -> WmResult<()> { |
| 677 | let focused = match self |
| 678 | .monitors |
| 679 | .get(self.selected_monitor) |
| 680 | .and_then(|m| m.focused_window) |
| 681 | { |
| 682 | Some(win) => win, |
| 683 | None => return Ok(()), |
| 684 | }; |
| 685 | |
| 686 | if self.fullscreen_window == Some(focused) || self.floating_windows.contains(&focused) { |
| 687 | return Ok(()); |
| 688 | } |
| 689 | |
| 690 | let visible = self.visible_windows(); |
| 691 | if visible.len() < 2 { |
| 692 | return Ok(()); |
| 693 | } |
| 694 | |
| 695 | let current_idx = match visible.iter().position(|&w| w == focused) { |
| 696 | Some(idx) => idx, |
| 697 | None => return Ok(()), |
| 698 | }; |
| 699 | |
| 700 | let target_idx = match direction { |
| 701 | 0 | 2 => { |
| 702 | // UP or LEFT - previous in stack |
| 703 | if current_idx == 0 { |
| 704 | visible.len() - 1 |
| 705 | } else { |
| 706 | current_idx - 1 |
| 707 | } |
| 708 | } |
| 709 | 1 | 3 => { |
| 710 | // DOWN or RIGHT - next in stack |
| 711 | (current_idx + 1) % visible.len() |
| 712 | } |
| 713 | _ => return Ok(()), |
| 714 | }; |
| 715 | |
| 716 | let target = visible[target_idx]; |
| 717 | |
| 718 | let focused_pos = self.windows.iter().position(|&w| w == focused); |
| 719 | let target_pos = self.windows.iter().position(|&w| w == target); |
| 720 | |
| 721 | if let (Some(f_pos), Some(t_pos)) = (focused_pos, target_pos) { |
| 722 | self.windows.swap(f_pos, t_pos); |
| 723 | |
| 724 | self.apply_layout()?; |
| 725 | |
| 726 | self.set_focus(focused)?; |
| 727 | |
| 728 | if let Ok(geometry) = self.connection.get_geometry(focused)?.reply() { |
| 729 | self.connection.warp_pointer( |
| 730 | x11rb::NONE, |
| 731 | focused, |
| 732 | 0, |
| 733 | 0, |
| 734 | 0, |
| 735 | 0, |
| 736 | geometry.width as i16 / 2, |
| 737 | geometry.height as i16 / 2, |
| 738 | )?; |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | Ok(()) |
| 743 | } |
| 744 | |
| 745 | fn toggle_fullscreen(&mut self) -> WmResult<()> { |
| 746 | if let Some(focused) = self |
| 747 | .monitors |
| 748 | .get(self.selected_monitor) |
| 749 | .and_then(|m| m.focused_window) |
| 750 | { |
| 751 | if self.fullscreen_window == Some(focused) { |
| 752 | self.fullscreen_window = None; |
| 753 | |
| 754 | for bar in &self.bars { |
| 755 | self.connection.map_window(bar.window())?; |
| 756 | } |
| 757 | |
| 758 | self.apply_layout()?; |
| 759 | } else { |
| 760 | self.fullscreen_window = Some(focused); |
| 761 | |
| 762 | if let Some(bar) = self.bars.get(self.selected_monitor) { |
| 763 | self.connection.unmap_window(bar.window())?; |
| 764 | } |
| 765 | |
| 766 | let monitor = &self.monitors[self.selected_monitor]; |
| 767 | let screen_width = monitor.width; |
| 768 | let screen_height = monitor.height; |
| 769 | |
| 770 | self.connection.configure_window( |
| 771 | focused, |
| 772 | &ConfigureWindowAux::new() |
| 773 | .x(monitor.x) |
| 774 | .y(monitor.y) |
| 775 | .width(screen_width) |
| 776 | .height(screen_height) |
| 777 | .border_width(0) |
| 778 | .stack_mode(StackMode::ABOVE), |
| 779 | )?; |
| 780 | |
| 781 | self.connection.flush()?; |
| 782 | } |
| 783 | } |
| 784 | Ok(()) |
| 785 | } |
| 786 | |
| 787 | fn update_bar(&mut self) -> WmResult<()> { |
| 788 | for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 789 | if let Some(bar) = self.bars.get_mut(monitor_index) { |
| 790 | let mut occupied_tags: TagMask = 0; |
| 791 | for (&window, &tags) in &self.window_tags { |
| 792 | if self.window_monitor.get(&window).copied().unwrap_or(0) == monitor_index { |
| 793 | occupied_tags |= tags; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | let draw_blocks = monitor_index == self.selected_monitor; |
| 798 | bar.invalidate(); |
| 799 | bar.draw( |
| 800 | &self.connection, |
| 801 | &self.font, |
| 802 | self.display, |
| 803 | monitor.selected_tags, |
| 804 | occupied_tags, |
| 805 | draw_blocks, |
| 806 | )?; |
| 807 | } |
| 808 | } |
| 809 | Ok(()) |
| 810 | } |
| 811 | |
| 812 | fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> WmResult<()> { |
| 813 | match action { |
| 814 | KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, |
| 815 | KeyAction::KillClient => { |
| 816 | if let Some(focused) = self |
| 817 | .monitors |
| 818 | .get(self.selected_monitor) |
| 819 | .and_then(|m| m.focused_window) |
| 820 | { |
| 821 | match self.connection.kill_client(focused) { |
| 822 | Ok(_) => { |
| 823 | self.connection.flush()?; |
| 824 | } |
| 825 | Err(e) => { |
| 826 | eprintln!("Failed to kill window {}: {}", focused, e); |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | KeyAction::ToggleFullScreen => { |
| 832 | self.toggle_fullscreen()?; |
| 833 | } |
| 834 | KeyAction::ChangeLayout => { |
| 835 | if let Arg::Str(layout_name) = arg { |
| 836 | match layout_from_str(layout_name) { |
| 837 | Ok(layout) => { |
| 838 | self.layout = layout; |
| 839 | self.apply_layout()?; |
| 840 | } |
| 841 | Err(e) => eprintln!("Failed to change layout: {}", e), |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | KeyAction::CycleLayout => { |
| 846 | let current_name = self.layout.name(); |
| 847 | let next_name = next_layout(current_name); |
| 848 | match layout_from_str(next_name) { |
| 849 | Ok(layout) => { |
| 850 | self.layout = layout; |
| 851 | self.apply_layout()?; |
| 852 | } |
| 853 | Err(e) => eprintln!("Failed to cycle layout: {}", e), |
| 854 | } |
| 855 | } |
| 856 | KeyAction::ToggleFloating => { |
| 857 | self.toggle_floating()?; |
| 858 | } |
| 859 | |
| 860 | KeyAction::SmartMoveWin => { |
| 861 | if let Arg::Int(direction) = arg { |
| 862 | self.smart_move_window(*direction)?; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | KeyAction::ExchangeClient => { |
| 867 | if let Arg::Int(direction) = arg { |
| 868 | self.exchange_client(*direction)?; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | KeyAction::FocusStack => { |
| 873 | if let Arg::Int(direction) = arg { |
| 874 | self.cycle_focus(*direction)?; |
| 875 | } |
| 876 | } |
| 877 | KeyAction::Quit | KeyAction::Restart => { |
| 878 | // Handled in handle_event |
| 879 | } |
| 880 | KeyAction::Recompile => { |
| 881 | match std::process::Command::new("oxwm") |
| 882 | .arg("--recompile") |
| 883 | .spawn() |
| 884 | { |
| 885 | Ok(_) => eprintln!("Recompiling in background"), |
| 886 | Err(e) => eprintln!("Failed to spawn recompile: {}", e), |
| 887 | } |
| 888 | } |
| 889 | KeyAction::ViewTag => { |
| 890 | if let Arg::Int(tag_index) = arg { |
| 891 | self.view_tag(*tag_index as usize)?; |
| 892 | } |
| 893 | } |
| 894 | KeyAction::MoveToTag => { |
| 895 | if let Arg::Int(tag_index) = arg { |
| 896 | self.move_to_tag(*tag_index as usize)?; |
| 897 | } |
| 898 | } |
| 899 | KeyAction::ToggleGaps => { |
| 900 | self.gaps_enabled = !self.gaps_enabled; |
| 901 | self.apply_layout()?; |
| 902 | } |
| 903 | KeyAction::FocusMonitor => { |
| 904 | if let Arg::Int(direction) = arg { |
| 905 | self.focus_monitor(*direction)?; |
| 906 | } |
| 907 | } |
| 908 | KeyAction::None => {} |
| 909 | } |
| 910 | Ok(()) |
| 911 | } |
| 912 | |
| 913 | fn focus_monitor(&mut self, direction: i32) -> WmResult<()> { |
| 914 | if self.monitors.is_empty() { |
| 915 | return Ok(()); |
| 916 | } |
| 917 | |
| 918 | let new_monitor = if direction > 0 { |
| 919 | (self.selected_monitor + 1) % self.monitors.len() |
| 920 | } else { |
| 921 | (self.selected_monitor + self.monitors.len() - 1) % self.monitors.len() |
| 922 | }; |
| 923 | |
| 924 | if new_monitor == self.selected_monitor { |
| 925 | return Ok(()); |
| 926 | } |
| 927 | |
| 928 | self.selected_monitor = new_monitor; |
| 929 | |
| 930 | self.update_bar()?; |
| 931 | |
| 932 | let visible = self.visible_windows_on_monitor(new_monitor); |
| 933 | if let Some(&win) = visible.first() { |
| 934 | self.set_focus(win)?; |
| 935 | } |
| 936 | |
| 937 | Ok(()) |
| 938 | } |
| 939 | |
| 940 | fn is_window_visible(&self, window: Window) -> bool { |
| 941 | let window_mon = self.window_monitor.get(&window).copied().unwrap_or(0); |
| 942 | |
| 943 | if let Some(&tags) = self.window_tags.get(&window) { |
| 944 | let monitor = self.monitors.get(window_mon); |
| 945 | let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0); |
| 946 | (tags & selected_tags) != 0 |
| 947 | } else { |
| 948 | false |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | fn visible_windows(&self) -> Vec<Window> { |
| 953 | self.windows |
| 954 | .iter() |
| 955 | .filter(|&&w| self.is_window_visible(w)) |
| 956 | .copied() |
| 957 | .collect() |
| 958 | } |
| 959 | |
| 960 | fn visible_windows_on_monitor(&self, monitor_index: usize) -> Vec<Window> { |
| 961 | self.windows |
| 962 | .iter() |
| 963 | .filter(|&&w| { |
| 964 | let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0); |
| 965 | if window_mon != monitor_index { |
| 966 | return false; |
| 967 | } |
| 968 | if let Some(&tags) = self.window_tags.get(&w) { |
| 969 | let monitor = self.monitors.get(monitor_index); |
| 970 | let selected_tags = monitor.map(|m| m.selected_tags).unwrap_or(0); |
| 971 | (tags & selected_tags) != 0 |
| 972 | } else { |
| 973 | false |
| 974 | } |
| 975 | }) |
| 976 | .copied() |
| 977 | .collect() |
| 978 | } |
| 979 | |
| 980 | fn get_monitor_at_point(&self, x: i32, y: i32) -> Option<usize> { |
| 981 | self.monitors |
| 982 | .iter() |
| 983 | .position(|mon| mon.contains_point(x, y)) |
| 984 | } |
| 985 | |
| 986 | fn update_window_visibility(&self) -> WmResult<()> { |
| 987 | for &window in &self.windows { |
| 988 | if self.is_window_visible(window) { |
| 989 | self.connection.map_window(window)?; |
| 990 | } else { |
| 991 | self.connection.unmap_window(window)?; |
| 992 | } |
| 993 | } |
| 994 | self.connection.flush()?; |
| 995 | Ok(()) |
| 996 | } |
| 997 | |
| 998 | pub fn view_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 999 | if tag_index >= self.config.tags.len() { |
| 1000 | return Ok(()); |
| 1001 | } |
| 1002 | |
| 1003 | if self.fullscreen_window.is_some() { |
| 1004 | self.fullscreen_window = None; |
| 1005 | for bar in &self.bars { |
| 1006 | self.connection.map_window(bar.window())?; |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 1011 | monitor.selected_tags = tag_mask(tag_index); |
| 1012 | } |
| 1013 | |
| 1014 | self.save_selected_tags()?; |
| 1015 | |
| 1016 | self.update_window_visibility()?; |
| 1017 | self.apply_layout()?; |
| 1018 | self.update_bar()?; |
| 1019 | |
| 1020 | let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 1021 | if let Some(&win) = visible.first() { |
| 1022 | self.set_focus(win)?; |
| 1023 | } |
| 1024 | |
| 1025 | Ok(()) |
| 1026 | } |
| 1027 | |
| 1028 | fn save_selected_tags(&self) -> WmResult<()> { |
| 1029 | let net_current_desktop = self.atoms.net_current_desktop; |
| 1030 | |
| 1031 | let selected_tags = self |
| 1032 | .monitors |
| 1033 | .get(self.selected_monitor) |
| 1034 | .map(|m| m.selected_tags) |
| 1035 | .unwrap_or(tag_mask(0)); |
| 1036 | let desktop = selected_tags.trailing_zeros(); |
| 1037 | |
| 1038 | let bytes = (desktop as u32).to_ne_bytes(); |
| 1039 | self.connection.change_property( |
| 1040 | PropMode::REPLACE, |
| 1041 | self.root, |
| 1042 | net_current_desktop, |
| 1043 | AtomEnum::CARDINAL, |
| 1044 | 32, |
| 1045 | 1, |
| 1046 | &bytes, |
| 1047 | )?; |
| 1048 | |
| 1049 | self.connection.flush()?; |
| 1050 | Ok(()) |
| 1051 | } |
| 1052 | |
| 1053 | pub fn move_to_tag(&mut self, tag_index: usize) -> WmResult<()> { |
| 1054 | if tag_index >= self.config.tags.len() { |
| 1055 | return Ok(()); |
| 1056 | } |
| 1057 | |
| 1058 | if let Some(focused) = self |
| 1059 | .monitors |
| 1060 | .get(self.selected_monitor) |
| 1061 | .and_then(|m| m.focused_window) |
| 1062 | { |
| 1063 | let mask = tag_mask(tag_index); |
| 1064 | self.window_tags.insert(focused, mask); |
| 1065 | |
| 1066 | let _ = self.save_client_tag(focused, mask); |
| 1067 | |
| 1068 | self.update_window_visibility()?; |
| 1069 | self.apply_layout()?; |
| 1070 | self.update_bar()?; |
| 1071 | } |
| 1072 | |
| 1073 | Ok(()) |
| 1074 | } |
| 1075 | |
| 1076 | pub fn cycle_focus(&mut self, direction: i32) -> WmResult<()> { |
| 1077 | let visible = self.visible_windows(); |
| 1078 | |
| 1079 | if visible.is_empty() { |
| 1080 | return Ok(()); |
| 1081 | } |
| 1082 | |
| 1083 | let current = self |
| 1084 | .monitors |
| 1085 | .get(self.selected_monitor) |
| 1086 | .and_then(|m| m.focused_window); |
| 1087 | |
| 1088 | let next_window = if let Some(current) = current { |
| 1089 | if let Some(current_index) = visible.iter().position(|&w| w == current) { |
| 1090 | let next_index = if direction > 0 { |
| 1091 | (current_index + 1) % visible.len() |
| 1092 | } else { |
| 1093 | (current_index + visible.len() - 1) % visible.len() |
| 1094 | }; |
| 1095 | visible[next_index] |
| 1096 | } else { |
| 1097 | visible[0] |
| 1098 | } |
| 1099 | } else { |
| 1100 | visible[0] |
| 1101 | }; |
| 1102 | |
| 1103 | self.set_focus(next_window)?; |
| 1104 | Ok(()) |
| 1105 | } |
| 1106 | |
| 1107 | pub fn set_focus(&mut self, window: Window) -> WmResult<()> { |
| 1108 | let old_focused = self.previous_focused; |
| 1109 | |
| 1110 | if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 1111 | monitor.focused_window = Some(window); |
| 1112 | } |
| 1113 | |
| 1114 | self.connection |
| 1115 | .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?; |
| 1116 | self.connection.flush()?; |
| 1117 | |
| 1118 | self.update_focus_visuals(old_focused, window)?; |
| 1119 | self.previous_focused = Some(window); |
| 1120 | Ok(()) |
| 1121 | } |
| 1122 | |
| 1123 | fn update_focus_visuals( |
| 1124 | &self, |
| 1125 | old_focused: Option<Window>, |
| 1126 | new_focused: Window, |
| 1127 | ) -> WmResult<()> { |
| 1128 | if let Some(old_win) = old_focused { |
| 1129 | if old_win != new_focused { |
| 1130 | self.connection.configure_window( |
| 1131 | old_win, |
| 1132 | &ConfigureWindowAux::new().border_width(self.config.border_width), |
| 1133 | )?; |
| 1134 | |
| 1135 | self.connection.change_window_attributes( |
| 1136 | old_win, |
| 1137 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), |
| 1138 | )?; |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | self.connection.configure_window( |
| 1143 | new_focused, |
| 1144 | &ConfigureWindowAux::new().border_width(self.config.border_width), |
| 1145 | )?; |
| 1146 | |
| 1147 | self.connection.change_window_attributes( |
| 1148 | new_focused, |
| 1149 | &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused), |
| 1150 | )?; |
| 1151 | |
| 1152 | self.connection.flush()?; |
| 1153 | Ok(()) |
| 1154 | } |
| 1155 | |
| 1156 | fn move_mouse(&mut self, window: Window) -> WmResult<()> { |
| 1157 | self.floating_windows.insert(window); |
| 1158 | |
| 1159 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 1160 | |
| 1161 | self.connection |
| 1162 | .grab_pointer( |
| 1163 | false, |
| 1164 | self.root, |
| 1165 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), |
| 1166 | GrabMode::ASYNC, |
| 1167 | GrabMode::ASYNC, |
| 1168 | x11rb::NONE, |
| 1169 | x11rb::NONE, |
| 1170 | x11rb::CURRENT_TIME, |
| 1171 | )? |
| 1172 | .reply()?; |
| 1173 | |
| 1174 | let pointer = self.connection.query_pointer(self.root)?.reply()?; |
| 1175 | let (start_x, start_y) = (pointer.root_x, pointer.root_y); |
| 1176 | |
| 1177 | self.connection.configure_window( |
| 1178 | window, |
| 1179 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 1180 | )?; |
| 1181 | |
| 1182 | loop { |
| 1183 | let event = self.connection.wait_for_event()?; |
| 1184 | match event { |
| 1185 | Event::MotionNotify(e) => { |
| 1186 | let new_x = geometry.x + (e.root_x - start_x); |
| 1187 | let new_y = geometry.y + (e.root_y - start_y); |
| 1188 | self.connection.configure_window( |
| 1189 | window, |
| 1190 | &ConfigureWindowAux::new().x(new_x as i32).y(new_y as i32), |
| 1191 | )?; |
| 1192 | self.connection.flush()?; |
| 1193 | } |
| 1194 | Event::ButtonRelease(_) => break, |
| 1195 | _ => {} |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | self.connection |
| 1200 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 1201 | .check()?; |
| 1202 | self.connection |
| 1203 | .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? |
| 1204 | .check()?; |
| 1205 | |
| 1206 | Ok(()) |
| 1207 | } |
| 1208 | |
| 1209 | fn resize_mouse(&mut self, window: Window) -> WmResult<()> { |
| 1210 | self.floating_windows.insert(window); |
| 1211 | |
| 1212 | let geometry = self.connection.get_geometry(window)?.reply()?; |
| 1213 | |
| 1214 | self.connection.warp_pointer( |
| 1215 | x11rb::NONE, |
| 1216 | window, |
| 1217 | 0, |
| 1218 | 0, |
| 1219 | 0, |
| 1220 | 0, |
| 1221 | geometry.width as i16, |
| 1222 | geometry.height as i16, |
| 1223 | )?; |
| 1224 | |
| 1225 | self.connection |
| 1226 | .grab_pointer( |
| 1227 | false, |
| 1228 | self.root, |
| 1229 | (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), |
| 1230 | GrabMode::ASYNC, |
| 1231 | GrabMode::ASYNC, |
| 1232 | x11rb::NONE, |
| 1233 | x11rb::NONE, |
| 1234 | x11rb::CURRENT_TIME, |
| 1235 | )? |
| 1236 | .reply()?; |
| 1237 | |
| 1238 | self.connection.configure_window( |
| 1239 | window, |
| 1240 | &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), |
| 1241 | )?; |
| 1242 | |
| 1243 | loop { |
| 1244 | let event = self.connection.wait_for_event()?; |
| 1245 | match event { |
| 1246 | Event::MotionNotify(e) => { |
| 1247 | let new_width = (e.root_x - geometry.x).max(1) as u32; |
| 1248 | let new_height = (e.root_y - geometry.y).max(1) as u32; |
| 1249 | |
| 1250 | self.connection.configure_window( |
| 1251 | window, |
| 1252 | &ConfigureWindowAux::new() |
| 1253 | .width(new_width) |
| 1254 | .height(new_height), |
| 1255 | )?; |
| 1256 | self.connection.flush()?; |
| 1257 | } |
| 1258 | Event::ButtonRelease(_) => break, |
| 1259 | _ => {} |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | self.connection |
| 1264 | .ungrab_pointer(x11rb::CURRENT_TIME)? |
| 1265 | .check()?; |
| 1266 | self.connection |
| 1267 | .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? |
| 1268 | .check()?; |
| 1269 | |
| 1270 | Ok(()) |
| 1271 | } |
| 1272 | |
| 1273 | fn handle_event(&mut self, event: Event) -> WmResult<Option<bool>> { |
| 1274 | match event { |
| 1275 | Event::MapRequest(event) => { |
| 1276 | let attrs = match self.connection.get_window_attributes(event.window)?.reply() { |
| 1277 | Ok(attrs) => attrs, |
| 1278 | Err(_) => return Ok(None), |
| 1279 | }; |
| 1280 | |
| 1281 | if attrs.override_redirect { |
| 1282 | return Ok(None); |
| 1283 | } |
| 1284 | |
| 1285 | if self.windows.contains(&event.window) { |
| 1286 | return Ok(None); |
| 1287 | } |
| 1288 | |
| 1289 | if let Ok(geom) = self.connection.get_geometry(event.window)?.reply() { |
| 1290 | self.window_geometries |
| 1291 | .insert(event.window, (geom.x, geom.y, geom.width, geom.height)); |
| 1292 | } |
| 1293 | |
| 1294 | self.connection.map_window(event.window)?; |
| 1295 | self.connection.change_window_attributes( |
| 1296 | event.window, |
| 1297 | &ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW), |
| 1298 | )?; |
| 1299 | |
| 1300 | let selected_tags = self |
| 1301 | .monitors |
| 1302 | .get(self.selected_monitor) |
| 1303 | .map(|m| m.selected_tags) |
| 1304 | .unwrap_or(tag_mask(0)); |
| 1305 | |
| 1306 | self.windows.push(event.window); |
| 1307 | self.window_tags.insert(event.window, selected_tags); |
| 1308 | self.window_monitor |
| 1309 | .insert(event.window, self.selected_monitor); |
| 1310 | self.set_wm_state(event.window, 1)?; |
| 1311 | let _ = self.save_client_tag(event.window, selected_tags); |
| 1312 | |
| 1313 | self.apply_layout()?; |
| 1314 | self.update_bar()?; |
| 1315 | self.set_focus(event.window)?; |
| 1316 | } |
| 1317 | Event::UnmapNotify(event) => { |
| 1318 | if self.windows.contains(&event.window) && self.is_window_visible(event.window) { |
| 1319 | self.remove_window(event.window)?; |
| 1320 | } |
| 1321 | } |
| 1322 | Event::DestroyNotify(event) => { |
| 1323 | if self.windows.contains(&event.window) { |
| 1324 | self.remove_window(event.window)?; |
| 1325 | } |
| 1326 | } |
| 1327 | Event::EnterNotify(event) => { |
| 1328 | if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL { |
| 1329 | return Ok(None); |
| 1330 | } |
| 1331 | if self.windows.contains(&event.event) { |
| 1332 | self.set_focus(event.event)?; |
| 1333 | } |
| 1334 | } |
| 1335 | Event::MotionNotify(event) => { |
| 1336 | if event.event != self.root { |
| 1337 | return Ok(None); |
| 1338 | } |
| 1339 | |
| 1340 | if let Some(monitor_index) = |
| 1341 | self.get_monitor_at_point(event.root_x as i32, event.root_y as i32) |
| 1342 | { |
| 1343 | if monitor_index != self.selected_monitor { |
| 1344 | self.selected_monitor = monitor_index; |
| 1345 | self.update_bar()?; |
| 1346 | |
| 1347 | let visible = self.visible_windows_on_monitor(monitor_index); |
| 1348 | if let Some(&win) = visible.first() { |
| 1349 | self.set_focus(win)?; |
| 1350 | } |
| 1351 | } |
| 1352 | } |
| 1353 | } |
| 1354 | Event::KeyPress(event) => { |
| 1355 | let (action, arg) = keyboard::handle_key_press(event, &self.config.keybindings); |
| 1356 | match action { |
| 1357 | KeyAction::Quit => return Ok(Some(false)), |
| 1358 | KeyAction::Restart => return Ok(Some(true)), |
| 1359 | _ => self.handle_key_action(action, &arg)?, |
| 1360 | } |
| 1361 | } |
| 1362 | Event::ButtonPress(event) => { |
| 1363 | let is_bar_click = self |
| 1364 | .bars |
| 1365 | .iter() |
| 1366 | .enumerate() |
| 1367 | .find(|(_, bar)| bar.window() == event.event); |
| 1368 | |
| 1369 | if let Some((monitor_index, bar)) = is_bar_click { |
| 1370 | if let Some(tag_index) = bar.handle_click(event.event_x) { |
| 1371 | if monitor_index != self.selected_monitor { |
| 1372 | self.selected_monitor = monitor_index; |
| 1373 | } |
| 1374 | self.view_tag(tag_index)?; |
| 1375 | } |
| 1376 | } else if event.child != x11rb::NONE { |
| 1377 | self.set_focus(event.child)?; |
| 1378 | |
| 1379 | if event.detail == ButtonIndex::M1.into() { |
| 1380 | self.move_mouse(event.child)?; |
| 1381 | } else if event.detail == ButtonIndex::M3.into() { |
| 1382 | self.resize_mouse(event.child)?; |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | Event::Expose(event) => { |
| 1387 | for bar in &mut self.bars { |
| 1388 | if event.window == bar.window() { |
| 1389 | bar.invalidate(); |
| 1390 | self.update_bar()?; |
| 1391 | break; |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | _ => {} |
| 1396 | } |
| 1397 | Ok(None) |
| 1398 | } |
| 1399 | |
| 1400 | fn apply_layout(&self) -> WmResult<()> { |
| 1401 | if self.fullscreen_window.is_some() { |
| 1402 | return Ok(()); |
| 1403 | } |
| 1404 | |
| 1405 | if self.layout.name() == crate::layout::NORMIE { |
| 1406 | return Ok(()); |
| 1407 | } |
| 1408 | |
| 1409 | let border_width = self.config.border_width; |
| 1410 | |
| 1411 | let gaps = if self.gaps_enabled { |
| 1412 | GapConfig { |
| 1413 | inner_horizontal: self.config.gap_inner_horizontal, |
| 1414 | inner_vertical: self.config.gap_inner_vertical, |
| 1415 | outer_horizontal: self.config.gap_outer_horizontal, |
| 1416 | outer_vertical: self.config.gap_outer_vertical, |
| 1417 | } |
| 1418 | } else { |
| 1419 | GapConfig { |
| 1420 | inner_horizontal: 0, |
| 1421 | inner_vertical: 0, |
| 1422 | outer_horizontal: 0, |
| 1423 | outer_vertical: 0, |
| 1424 | } |
| 1425 | }; |
| 1426 | |
| 1427 | for (monitor_index, monitor) in self.monitors.iter().enumerate() { |
| 1428 | let visible: Vec<Window> = self |
| 1429 | .windows |
| 1430 | .iter() |
| 1431 | .filter(|&&w| { |
| 1432 | let window_mon = self.window_monitor.get(&w).copied().unwrap_or(0); |
| 1433 | if window_mon != monitor_index { |
| 1434 | return false; |
| 1435 | } |
| 1436 | if self.floating_windows.contains(&w) { |
| 1437 | return false; |
| 1438 | } |
| 1439 | if let Some(&tags) = self.window_tags.get(&w) { |
| 1440 | (tags & monitor.selected_tags) != 0 |
| 1441 | } else { |
| 1442 | false |
| 1443 | } |
| 1444 | }) |
| 1445 | .copied() |
| 1446 | .collect(); |
| 1447 | |
| 1448 | let bar_height = self |
| 1449 | .bars |
| 1450 | .get(monitor_index) |
| 1451 | .map(|b| b.height() as u32) |
| 1452 | .unwrap_or(0); |
| 1453 | let usable_height = monitor.height.saturating_sub(bar_height); |
| 1454 | |
| 1455 | let geometries = self |
| 1456 | .layout |
| 1457 | .arrange(&visible, monitor.width, usable_height, &gaps); |
| 1458 | |
| 1459 | for (window, geometry) in visible.iter().zip(geometries.iter()) { |
| 1460 | let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
| 1461 | let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
| 1462 | |
| 1463 | let adjusted_x = geometry.x_coordinate + monitor.x; |
| 1464 | let adjusted_y = geometry.y_coordinate + monitor.y + bar_height as i32; |
| 1465 | |
| 1466 | self.connection.configure_window( |
| 1467 | *window, |
| 1468 | &ConfigureWindowAux::new() |
| 1469 | .x(adjusted_x) |
| 1470 | .y(adjusted_y) |
| 1471 | .width(adjusted_width) |
| 1472 | .height(adjusted_height), |
| 1473 | )?; |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | self.connection.flush()?; |
| 1478 | Ok(()) |
| 1479 | } |
| 1480 | |
| 1481 | pub fn change_layout<L: Layout + 'static>(&mut self, new_layout: L) -> WmResult<()> { |
| 1482 | self.layout = Box::new(new_layout); |
| 1483 | self.apply_layout()?; |
| 1484 | Ok(()) |
| 1485 | } |
| 1486 | |
| 1487 | fn remove_window(&mut self, window: Window) -> WmResult<()> { |
| 1488 | let initial_count = self.windows.len(); |
| 1489 | self.windows.retain(|&w| w != window); |
| 1490 | self.window_tags.remove(&window); |
| 1491 | self.window_monitor.remove(&window); |
| 1492 | self.window_geometries.remove(&window); |
| 1493 | self.floating_windows.remove(&window); |
| 1494 | |
| 1495 | if self.fullscreen_window == Some(window) { |
| 1496 | self.fullscreen_window = None; |
| 1497 | for bar in &self.bars { |
| 1498 | self.connection.map_window(bar.window())?; |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | if self.windows.len() < initial_count { |
| 1503 | let focused = self |
| 1504 | .monitors |
| 1505 | .get(self.selected_monitor) |
| 1506 | .and_then(|m| m.focused_window); |
| 1507 | if focused == Some(window) { |
| 1508 | let visible = self.visible_windows_on_monitor(self.selected_monitor); |
| 1509 | if let Some(&new_win) = visible.last() { |
| 1510 | self.set_focus(new_win)?; |
| 1511 | } else if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { |
| 1512 | monitor.focused_window = None; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | self.apply_layout()?; |
| 1517 | self.update_bar()?; |
| 1518 | } |
| 1519 | Ok(()) |
| 1520 | } |
| 1521 | } |