Added floating and resizing.
Browse files
+172 −12 src/window_manager.rs
| @@ -1,13 +1,14 @@ | ||
| 1 | 1 | use crate::bar::Bar; |
| 2 | 2 | use crate::config::{ |
| 3 | 3 | BORDER_FOCUSED, BORDER_UNFOCUSED, BORDER_WIDTH, GAP_INNER_HORIZONTAL, GAP_INNER_VERTICAL, |
| 4 | - GAP_OUTER_HORIZONTAL, GAP_OUTER_VERTICAL, GAPS_ENABLED, TAG_COUNT, | |
| 4 | + GAP_OUTER_HORIZONTAL, GAP_OUTER_VERTICAL, GAPS_ENABLED, MODKEY, TAG_COUNT, | |
| 5 | 5 | }; |
| 6 | 6 | use crate::keyboard::{self, Arg, KeyAction}; |
| 7 | 7 | use crate::layout::GapConfig; |
| 8 | 8 | use crate::layout::Layout; |
| 9 | 9 | use crate::layout::tiling::TilingLayout; |
| 10 | 10 | use anyhow::Result; |
| 11 | +use std::collections::HashSet; | |
| 11 | 12 | |
| 12 | 13 | use x11rb::connection::Connection; |
| 13 | 14 | use x11rb::protocol::Event; |
| @@ -31,6 +32,7 @@ pub struct WindowManager { | ||
| 31 | 32 | selected_tags: TagMask, |
| 32 | 33 | gaps_enabled: bool, |
| 33 | 34 | fullscreen_window: Option<Window>, |
| 35 | + floating_windows: HashSet<Window>, | |
| 34 | 36 | bar: Bar, |
| 35 | 37 | } |
| 36 | 38 | |
| @@ -47,11 +49,36 @@ impl WindowManager { | ||
| 47 | 49 | EventMask::SUBSTRUCTURE_REDIRECT |
| 48 | 50 | | EventMask::SUBSTRUCTURE_NOTIFY |
| 49 | 51 | | EventMask::PROPERTY_CHANGE |
| 50 | - | EventMask::KEY_PRESS, | |
| 52 | + | EventMask::KEY_PRESS | |
| 53 | + | EventMask::BUTTON_PRESS, | |
| 51 | 54 | ), |
| 52 | 55 | )? |
| 53 | 56 | .check()?; |
| 54 | 57 | |
| 58 | + connection.grab_button( | |
| 59 | + false, | |
| 60 | + root, | |
| 61 | + EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, | |
| 62 | + GrabMode::SYNC, | |
| 63 | + GrabMode::ASYNC, | |
| 64 | + x11rb::NONE, | |
| 65 | + x11rb::NONE, | |
| 66 | + ButtonIndex::M1, | |
| 67 | + u16::from(MODKEY).into(), | |
| 68 | + )?; | |
| 69 | + | |
| 70 | + connection.grab_button( | |
| 71 | + false, | |
| 72 | + root, | |
| 73 | + EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, | |
| 74 | + GrabMode::SYNC, | |
| 75 | + GrabMode::ASYNC, | |
| 76 | + x11rb::NONE, | |
| 77 | + x11rb::NONE, | |
| 78 | + ButtonIndex::M3, | |
| 79 | + u16::from(MODKEY).into(), | |
| 80 | + )?; | |
| 81 | + | |
| 55 | 82 | let bar = Bar::new(&connection, &screen, screen_number)?; |
| 56 | 83 | |
| 57 | 84 | let selected_tags = Self::get_saved_selected_tags(&connection, root)?; |
| @@ -69,6 +96,7 @@ impl WindowManager { | ||
| 69 | 96 | selected_tags, |
| 70 | 97 | gaps_enabled, |
| 71 | 98 | fullscreen_window: None, |
| 99 | + floating_windows: HashSet::new(), | |
| 72 | 100 | bar, |
| 73 | 101 | }; |
| 74 | 102 | |
| @@ -88,7 +116,6 @@ impl WindowManager { | ||
| 88 | 116 | .get_property(false, root, net_current_desktop, AtomEnum::CARDINAL, 0, 1)? |
| 89 | 117 | .reply() |
| 90 | 118 | { |
| 91 | - // I don't undestand this but I got it from dwm->persist_tags patch and it worked. | |
| 92 | 119 | Ok(prop) if prop.value.len() >= 4 => { |
| 93 | 120 | let desktop = u32::from_ne_bytes([ |
| 94 | 121 | prop.value[0], |
| @@ -240,7 +267,7 @@ impl WindowManager { | ||
| 240 | 267 | .reply()? |
| 241 | 268 | .atom; |
| 242 | 269 | |
| 243 | - let data = [state, 0u32]; // No icon window | |
| 270 | + let data = [state, 0u32]; | |
| 244 | 271 | let bytes: Vec<u8> = data.iter().flat_map(|&v| v.to_ne_bytes()).collect(); |
| 245 | 272 | |
| 246 | 273 | self.connection.change_property( |
| @@ -284,19 +311,19 @@ impl WindowManager { | ||
| 284 | 311 | if let Some(focused) = self.focused_window { |
| 285 | 312 | if self.fullscreen_window == Some(focused) { |
| 286 | 313 | self.fullscreen_window = None; |
| 287 | - | |
| 314 | + | |
| 288 | 315 | self.connection.map_window(self.bar.window())?; |
| 289 | - | |
| 316 | + | |
| 290 | 317 | self.apply_layout()?; |
| 291 | 318 | self.update_focus_visuals()?; |
| 292 | 319 | } else { |
| 293 | 320 | self.fullscreen_window = Some(focused); |
| 294 | - | |
| 321 | + | |
| 295 | 322 | self.connection.unmap_window(self.bar.window())?; |
| 296 | - | |
| 323 | + | |
| 297 | 324 | let screen_width = self.screen.width_in_pixels as u32; |
| 298 | 325 | let screen_height = self.screen.height_in_pixels as u32; |
| 299 | - | |
| 326 | + | |
| 300 | 327 | self.connection.configure_window( |
| 301 | 328 | focused, |
| 302 | 329 | &ConfigureWindowAux::new() |
| @@ -307,13 +334,13 @@ impl WindowManager { | ||
| 307 | 334 | .border_width(0) |
| 308 | 335 | .stack_mode(StackMode::ABOVE), |
| 309 | 336 | )?; |
| 310 | - | |
| 337 | + | |
| 311 | 338 | self.connection.flush()?; |
| 312 | 339 | } |
| 313 | 340 | } |
| 314 | 341 | Ok(()) |
| 315 | 342 | } |
| 316 | - | |
| 343 | + | |
| 317 | 344 | fn update_bar(&mut self) -> Result<()> { |
| 318 | 345 | let mut occupied_tags: TagMask = 0; |
| 319 | 346 | for &tags in self.window_tags.values() { |
| @@ -539,6 +566,125 @@ impl WindowManager { | ||
| 539 | 566 | Ok(()) |
| 540 | 567 | } |
| 541 | 568 | |
| 569 | + fn move_mouse(&mut self, window: Window) -> Result<()> { | |
| 570 | + self.floating_windows.insert(window); | |
| 571 | + | |
| 572 | + let geometry = self.connection.get_geometry(window)?.reply()?; | |
| 573 | + | |
| 574 | + self.connection | |
| 575 | + .grab_pointer( | |
| 576 | + false, | |
| 577 | + self.root, | |
| 578 | + (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), | |
| 579 | + GrabMode::ASYNC, | |
| 580 | + GrabMode::ASYNC, | |
| 581 | + x11rb::NONE, | |
| 582 | + x11rb::NONE, | |
| 583 | + x11rb::CURRENT_TIME, | |
| 584 | + )? | |
| 585 | + .reply()?; | |
| 586 | + | |
| 587 | + let pointer = self.connection.query_pointer(self.root)?.reply()?; | |
| 588 | + let (original_x_value, original_y_value) = (pointer.root_x, pointer.root_y); | |
| 589 | + | |
| 590 | + self.connection.configure_window( | |
| 591 | + window, | |
| 592 | + &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), | |
| 593 | + )?; | |
| 594 | + | |
| 595 | + loop { | |
| 596 | + let event = self.connection.wait_for_event()?; | |
| 597 | + match event { | |
| 598 | + Event::MotionNotify(e) => { | |
| 599 | + let new_x_value = geometry.x + (e.root_x - original_x_value); | |
| 600 | + let new_y_value = geometry.y + (e.root_y - original_y_value); | |
| 601 | + self.connection.configure_window( | |
| 602 | + window, | |
| 603 | + &ConfigureWindowAux::new() | |
| 604 | + .x(new_x_value as i32) | |
| 605 | + .y(new_y_value as i32), | |
| 606 | + )?; | |
| 607 | + self.connection.flush()?; | |
| 608 | + } | |
| 609 | + Event::ButtonRelease(_) => break, | |
| 610 | + _ => {} | |
| 611 | + } | |
| 612 | + } | |
| 613 | + | |
| 614 | + self.connection | |
| 615 | + .ungrab_pointer(x11rb::CURRENT_TIME)? | |
| 616 | + .check()?; | |
| 617 | + self.connection | |
| 618 | + .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? | |
| 619 | + .check()?; | |
| 620 | + | |
| 621 | + Ok(()) | |
| 622 | + } | |
| 623 | + | |
| 624 | + fn resize_mouse(&mut self, window: Window) -> Result<()> { | |
| 625 | + self.floating_windows.insert(window); | |
| 626 | + | |
| 627 | + let geometry = self.connection.get_geometry(window)?.reply()?; | |
| 628 | + | |
| 629 | + self.connection.warp_pointer( | |
| 630 | + x11rb::NONE, | |
| 631 | + window, | |
| 632 | + 0, | |
| 633 | + 0, | |
| 634 | + 0, | |
| 635 | + 0, | |
| 636 | + geometry.width as i16, | |
| 637 | + geometry.height as i16, | |
| 638 | + )?; | |
| 639 | + | |
| 640 | + self.connection | |
| 641 | + .grab_pointer( | |
| 642 | + false, | |
| 643 | + self.root, | |
| 644 | + (EventMask::POINTER_MOTION | EventMask::BUTTON_RELEASE).into(), | |
| 645 | + GrabMode::ASYNC, | |
| 646 | + GrabMode::ASYNC, | |
| 647 | + x11rb::NONE, | |
| 648 | + x11rb::NONE, | |
| 649 | + x11rb::CURRENT_TIME, | |
| 650 | + )? | |
| 651 | + .reply()?; | |
| 652 | + | |
| 653 | + self.connection.configure_window( | |
| 654 | + window, | |
| 655 | + &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), | |
| 656 | + )?; | |
| 657 | + | |
| 658 | + loop { | |
| 659 | + let event = self.connection.wait_for_event()?; | |
| 660 | + match event { | |
| 661 | + Event::MotionNotify(e) => { | |
| 662 | + let new_width = (e.root_x - geometry.x).max(1) as u32; | |
| 663 | + let new_height = (e.root_y - geometry.y).max(1) as u32; | |
| 664 | + | |
| 665 | + self.connection.configure_window( | |
| 666 | + window, | |
| 667 | + &ConfigureWindowAux::new() | |
| 668 | + .width(new_width) | |
| 669 | + .height(new_height), | |
| 670 | + )?; | |
| 671 | + self.connection.flush()?; | |
| 672 | + } | |
| 673 | + Event::ButtonRelease(_) => break, | |
| 674 | + _ => {} | |
| 675 | + } | |
| 676 | + } | |
| 677 | + | |
| 678 | + self.connection | |
| 679 | + .ungrab_pointer(x11rb::CURRENT_TIME)? | |
| 680 | + .check()?; | |
| 681 | + self.connection | |
| 682 | + .allow_events(Allow::REPLAY_POINTER, x11rb::CURRENT_TIME)? | |
| 683 | + .check()?; | |
| 684 | + | |
| 685 | + Ok(()) | |
| 686 | + } | |
| 687 | + | |
| 542 | 688 | fn handle_event(&mut self, event: Event) -> Result<Option<bool>> { |
| 543 | 689 | match event { |
| 544 | 690 | Event::MapRequest(event) => { |
| @@ -601,6 +747,14 @@ impl WindowManager { | ||
| 601 | 747 | if let Some(tag_index) = self.bar.handle_click(event.event_x) { |
| 602 | 748 | self.view_tag(tag_index)?; |
| 603 | 749 | } |
| 750 | + } else if event.child != x11rb::NONE { | |
| 751 | + self.set_focus(Some(event.child))?; | |
| 752 | + | |
| 753 | + if event.detail == ButtonIndex::M1.into() { | |
| 754 | + self.move_mouse(event.child)?; | |
| 755 | + } else if event.detail == ButtonIndex::M3.into() { | |
| 756 | + self.resize_mouse(event.child)?; | |
| 757 | + } | |
| 604 | 758 | } |
| 605 | 759 | } |
| 606 | 760 | Event::Expose(event) => { |
| @@ -641,7 +795,12 @@ impl WindowManager { | ||
| 641 | 795 | } |
| 642 | 796 | }; |
| 643 | 797 | |
| 644 | - let visible = self.visible_windows(); | |
| 798 | + let visible: Vec<Window> = self | |
| 799 | + .visible_windows() | |
| 800 | + .into_iter() | |
| 801 | + .filter(|w| !self.floating_windows.contains(w)) | |
| 802 | + .collect(); | |
| 803 | + | |
| 645 | 804 | let geometries = self |
| 646 | 805 | .layout |
| 647 | 806 | .arrange(&visible, screen_width, usable_height, &gaps); |
| @@ -669,6 +828,7 @@ impl WindowManager { | ||
| 669 | 828 | let initial_count = self.windows.len(); |
| 670 | 829 | self.windows.retain(|&w| w != window); |
| 671 | 830 | self.window_tags.remove(&window); |
| 831 | + self.floating_windows.remove(&window); | |
| 672 | 832 | |
| 673 | 833 | if self.fullscreen_window == Some(window) { |
| 674 | 834 | self.fullscreen_window = None; |