tony / oxwm

Added correct logic to focusstack() and unmap() windows (straight from DWMs logic). Browse files
tonybtw committed 2025-12-01 parent a4a6486 commit b045fc8feb3f02525fd66fc2c3642aca0062cc7c
Showing 2 changed files with 331 additions and 97 deletions. UnifiedSplit
+2 −2 resources/test-config.lua
@@ -75,9 +75,9 @@ oxwm.key.bind({ modkey }, "A", oxwm.toggle_gaps())
75 oxwm.key.bind({ modkey, "Shift" }, "Q", oxwm.quit()) 75 oxwm.key.bind({ modkey, "Shift" }, "Q", oxwm.quit())
76 oxwm.key.bind({ modkey, "Shift" }, "R", oxwm.restart()) 76 oxwm.key.bind({ modkey, "Shift" }, "R", oxwm.restart())
77 77
78 +oxwm.key.bind({ modkey }, "J", oxwm.client.focus_stack(1))
79 +oxwm.key.bind({ modkey }, "K", oxwm.client.focus_stack(-1))
78 oxwm.key.bind({ modkey }, "H", oxwm.client.focus_direction("left")) 80 oxwm.key.bind({ modkey }, "H", oxwm.client.focus_direction("left"))
79 -oxwm.key.bind({ modkey }, "J", oxwm.client.focus_direction("down"))
80 -oxwm.key.bind({ modkey }, "K", oxwm.client.focus_direction("up"))
81 oxwm.key.bind({ modkey }, "L", oxwm.client.focus_direction("right")) 81 oxwm.key.bind({ modkey }, "L", oxwm.client.focus_direction("right"))
82 82
83 oxwm.key.bind({ modkey, "Shift" }, "H", oxwm.client.swap_direction("left")) 83 oxwm.key.bind({ modkey, "Shift" }, "H", oxwm.client.swap_direction("left"))
+329 −95 src/window_manager.rs Large diff collapsed, click to expand
@@ -43,6 +43,11 @@ struct AtomCache {
43 net_wm_state_fullscreen: Atom, 43 net_wm_state_fullscreen: Atom,
44 net_wm_window_type: Atom, 44 net_wm_window_type: Atom,
45 net_wm_window_type_dialog: Atom, 45 net_wm_window_type_dialog: Atom,
46 + wm_name: Atom,
47 + net_wm_name: Atom,
48 + wm_normal_hints: Atom,
49 + wm_hints: Atom,
50 + wm_transient_for: Atom,
46 } 51 }
47 52
48 impl AtomCache { 53 impl AtomCache {
@@ -89,6 +94,12 @@ impl AtomCache {
89 .reply()? 94 .reply()?
90 .atom; 95 .atom;
91 96
97 + let wm_name = AtomEnum::WM_NAME.into();
98 + let net_wm_name = connection.intern_atom(false, b"_NET_WM_NAME")?.reply()?.atom;
99 + let wm_normal_hints = AtomEnum::WM_NORMAL_HINTS.into();
100 + let wm_hints = AtomEnum::WM_HINTS.into();
101 + let wm_transient_for = AtomEnum::WM_TRANSIENT_FOR.into();
102 +
92 Ok(Self { 103 Ok(Self {
93 net_current_desktop, 104 net_current_desktop,
94 net_client_info, 105 net_client_info,
@@ -99,6 +110,11 @@ impl AtomCache {
99 net_wm_state_fullscreen, 110 net_wm_state_fullscreen,
100 net_wm_window_type, 111 net_wm_window_type,
101 net_wm_window_type_dialog, 112 net_wm_window_type_dialog,
113 + wm_name,
114 + net_wm_name,
115 + wm_normal_hints,
116 + wm_hints,
117 + wm_transient_for,
102 }) 118 })
103 } 119 }
104 } 120 }
@@ -409,7 +425,7 @@ impl WindowManager {
409 } 425 }
410 426
411 if let Some(&first) = self.windows.first() { 427 if let Some(&first) = self.windows.first() {
412 - self.set_focus(first)?; 428 + self.focus(Some(first))?;
413 } 429 }
414 430
415 self.apply_layout()?; 431 self.apply_layout()?;
@@ -546,32 +562,33 @@ impl WindowManager {
546 562
547 self.apply_layout()?; 563 self.apply_layout()?;
548 } else { 564 } else {
549 - let float_width = (self.screen.width_in_pixels / 2) as u32; 565 + if let Some(monitor) = self.monitors.get(self.selected_monitor) {
550 - let float_height = (self.screen.height_in_pixels / 2) as u32; 566 + let float_width = (monitor.screen_width as f32 * 0.5) as u32;
567 + let float_height = (monitor.screen_height as f32 * 0.5) as u32;
551 568
552 - let border_width = self.config.border_width; 569 + let border_width = self.config.border_width;
553 570
554 - let center_width = ((self.screen.width_in_pixels - float_width as u16) / 2) as i32; 571 + let center_x = monitor.screen_x + (monitor.screen_width as i32 - float_width as i32) / 2;
555 - let center_height = 572 + let center_y = monitor.screen_y + (monitor.screen_height as i32 - float_height as i32) / 2;
556 - ((self.screen.height_in_pixels - float_height as u16) / 2) as i32;
557 573
558 - self.connection.configure_window( 574 + self.connection.configure_window(
559 - focused, 575 + focused,
560 - &ConfigureWindowAux::new() 576 + &ConfigureWindowAux::new()
561 - .x(center_width) 577 + .x(center_x)
562 - .y(center_height) 578 + .y(center_y)
563 - .width(float_width) 579 + .width(float_width)
564 - .height(float_height) 580 + .height(float_height)
565 - .border_width(border_width) 581 + .border_width(border_width)
566 - .stack_mode(StackMode::ABOVE), 582 + .stack_mode(StackMode::ABOVE),
567 - )?; 583 + )?;
568 584
569 - self.floating_windows.insert(focused); 585 + self.floating_windows.insert(focused);
570 - if let Some(client) = self.clients.get_mut(&focused) { 586 + if let Some(client) = self.clients.get_mut(&focused) {
571 - client.is_floating = true; 587 + client.is_floating = true;
588 + }
589 + self.apply_layout()?;
590 + self.connection.flush()?;
572 } 591 }
573 - self.apply_layout()?;
574 - self.connection.flush()?;
575 } 592 }
576 } 593 }
577 Ok(()) 594 Ok(())
@@ -627,7 +644,7 @@ impl WindowManager {
627 644
628 self.apply_layout()?; 645 self.apply_layout()?;
629 646
630 - self.set_focus(focused)?; 647 + self.focus(Some(focused))?;
631 648
632 if let Ok(geometry) = self.connection.get_geometry(focused)?.reply() { 649 if let Ok(geometry) = self.connection.get_geometry(focused)?.reply() {
633 self.connection.warp_pointer( 650 self.connection.warp_pointer(
@@ -788,6 +805,7 @@ impl WindowManager {
788 } 805 }
789 KeyAction::ToggleFullScreen => { 806 KeyAction::ToggleFullScreen => {
790 self.fullscreen()?; 807 self.fullscreen()?;
808 + self.restack()?;
791 } 809 }
792 KeyAction::ChangeLayout => { 810 KeyAction::ChangeLayout => {
793 if let Arg::Str(layout_name) = arg { 811 if let Arg::Str(layout_name) = arg {
@@ -799,6 +817,7 @@ impl WindowManager {
799 } 817 }
800 self.apply_layout()?; 818 self.apply_layout()?;
801 self.update_bar()?; 819 self.update_bar()?;
820 + self.restack()?;
802 } 821 }
803 Err(e) => eprintln!("Failed to change layout: {}", e), 822 Err(e) => eprintln!("Failed to change layout: {}", e),
804 } 823 }
@@ -815,12 +834,14 @@ impl WindowManager {
815 } 834 }
816 self.apply_layout()?; 835 self.apply_layout()?;
817 self.update_bar()?; 836 self.update_bar()?;
837 + self.restack()?;
818 } 838 }
819 Err(e) => eprintln!("Failed to cycle layout: {}", e), 839 Err(e) => eprintln!("Failed to cycle layout: {}", e),
820 } 840 }
821 } 841 }
822 KeyAction::ToggleFloating => { 842 KeyAction::ToggleFloating => {
823 self.toggle_floating()?; 843 self.toggle_floating()?;
844 + self.restack()?;
824 } 845 }
825 846
826 KeyAction::ExchangeClient => { 847 KeyAction::ExchangeClient => {
@@ -831,7 +852,7 @@ impl WindowManager {
831 852
832 KeyAction::FocusStack => { 853 KeyAction::FocusStack => {
833 if let Arg::Int(direction) = arg { 854 if let Arg::Int(direction) = arg {
834 - self.cycle_focus(*direction)?; 855 + self.focusstack(*direction)?;
835 } 856 }
836 } 857 }
837 KeyAction::FocusDirection => { 858 KeyAction::FocusDirection => {
@@ -869,6 +890,7 @@ impl WindowManager {
869 KeyAction::ToggleGaps => { 890 KeyAction::ToggleGaps => {
870 self.gaps_enabled = !self.gaps_enabled; 891 self.gaps_enabled = !self.gaps_enabled;
871 self.apply_layout()?; 892 self.apply_layout()?;
893 + self.restack()?;
872 } 894 }
873 KeyAction::FocusMonitor => { 895 KeyAction::FocusMonitor => {
874 if let Arg::Int(direction) = arg { 896 if let Arg::Int(direction) = arg {
@@ -911,7 +933,7 @@ impl WindowManager {
911 933
912 let visible = self.visible_windows_on_monitor(new_monitor); 934 let visible = self.visible_windows_on_monitor(new_monitor);
913 if let Some(&win) = visible.first() { 935 if let Some(&win) = visible.first() {
914 - self.set_focus(win)?; 936 + self.focus(Some(win))?;
915 } 937 }
916 938
917 Ok(()) 939 Ok(())
@@ -1007,7 +1029,7 @@ impl WindowManager {
1007 1029
1008 let visible = self.visible_windows_on_monitor(self.selected_monitor); 1030 let visible = self.visible_windows_on_monitor(self.selected_monitor);
1009 if let Some(&win) = visible.first() { 1031 if let Some(&win) = visible.first() {
1010 - self.set_focus(win)?; 1032 + self.focus(Some(win))?;
1011 } 1033 }
1012 1034
1013 Ok(()) 1035 Ok(())
@@ -1098,7 +1120,7 @@ impl WindowManager {
1098 )?; 1120 )?;
1099 } 1121 }
1100 1122
1101 - self.set_focus(next_window)?; 1123 + self.focus(Some(next_window))?;
1102 1124
1103 if is_tabbed { 1125 if is_tabbed {
1104 self.update_tab_bars()?; 1126 self.update_tab_bars()?;
@@ -1162,7 +1184,7 @@ impl WindowManager {
1162 }; 1184 };
1163 1185
1164 if let Some(target_window) = self.find_directional_window_candidate(focused_window, direction) { 1186 if let Some(target_window) = self.find_directional_window_candidate(focused_window, direction) {
1165 - self.set_focus(target_window)?; 1187 + self.focus(Some(target_window))?;
1166 } 1188 }
1167 1189
1168 Ok(()) 1190 Ok(())
@@ -1185,7 +1207,7 @@ impl WindowManager {
1185 if let (Some(focused_index), Some(target_index)) = (focused_position, target_position) { 1207 if let (Some(focused_index), Some(target_index)) = (focused_position, target_position) {
1186 self.windows.swap(focused_index, target_index); 1208 self.windows.swap(focused_index, target_index);
1187 self.apply_layout()?; 1209 self.apply_layout()?;
1188 - self.set_focus(focused_window)?; 1210 + self.focus(Some(focused_window))?;
1189 1211
1190 if let Ok(geometry) = self.get_or_query_geometry(focused_window) { 1212 if let Ok(geometry) = self.get_or_query_geometry(focused_window) {
1191 self.connection.warp_pointer( 1213 self.connection.warp_pointer(
@@ -1670,9 +1692,6 @@ impl WindowManager {
1670 client.border_width = border_width as u16; 1692 client.border_width = border_width as u16;
1671 client.old_border_width = border_width as u16; 1693 client.old_border_width = border_width as u16;
1672 1694
1673 - self.update_size_hints(&mut client)?;
1674 - self.update_window_title(&mut client)?;
1675 -
1676 if window_x + (window_width as i32) + (2 * border_width as i32) > monitor.screen_x + monitor.screen_width as i32 { 1695 if window_x + (window_width as i32) + (2 * border_width as i32) > monitor.screen_x + monitor.screen_width as i32 {
1677 window_x = monitor.screen_x + monitor.screen_width as i32 - (window_width as i32) - (2 * border_width as i32); 1696 window_x = monitor.screen_x + monitor.screen_width as i32 - (window_width as i32) - (2 * border_width as i32);
1678 } 1697 }
@@ -1711,6 +1730,8 @@ impl WindowManager {
1711 client.is_floating = is_transient || is_dialog; 1730 client.is_floating = is_transient || is_dialog;
1712 1731
1713 self.clients.insert(window, client); 1732 self.clients.insert(window, client);
1733 + self.update_size_hints(window)?;
1734 + self.update_window_title(window)?;
1714 self.attach(window, monitor_index); 1735 self.attach(window, monitor_index);
1715 self.attach_stack(window, monitor_index); 1736 self.attach_stack(window, monitor_index);
1716 1737
@@ -1812,7 +1833,7 @@ impl WindowManager {
1812 self.apply_layout()?; 1833 self.apply_layout()?;
1813 self.connection.map_window(window)?; 1834 self.connection.map_window(window)?;
1814 self.update_bar()?; 1835 self.update_bar()?;
1815 - self.set_focus(window)?; 1836 + self.focus(Some(window))?;
1816 1837
1817 if self.layout.name() == "tabbed" { 1838 if self.layout.name() == "tabbed" {
1818 self.update_tab_bars()?; 1839 self.update_tab_bars()?;
@@ -1842,6 +1863,196 @@ impl WindowManager {
1842 Ok(()) 1863 Ok(())
1843 } 1864 }
1844 1865
1866 + fn unfocus(&self, window: Window) -> WmResult<()> {
1867 + if !self.windows.contains(&window) {
1868 + return Ok(());
1869 + }
1870 +
1871 + self.connection.change_window_attributes(
1872 + window,
1873 + &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused),
1874 + )?;
1875 +
1876 + self.connection.grab_button(
1877 + false,
1878 + window,
1879 + EventMask::BUTTON_PRESS.into(),
1880 + GrabMode::SYNC,
1881 + GrabMode::SYNC,
1882 + x11rb::NONE,
1883 + x11rb::NONE,
1884 + ButtonIndex::ANY,
1885 + ModMask::ANY,
1886 + )?;
1887 +
1888 + Ok(())
1889 + }
1890 +
1891 + fn focus(&mut self, window: Option<Window>) -> WmResult<()> {
1892 + let monitor = self.monitors.get_mut(self.selected_monitor).unwrap();
1893 + let old_selected = monitor.selected_client;
1894 +
1895 + if let Some(old_win) = old_selected {
1896 + if old_selected != window {
1897 + self.unfocus(old_win)?;
1898 + }
1899 + }
1900 +
1901 + if let Some(win) = window {
1902 + if !self.windows.contains(&win) {
1903 + return Ok(());
1904 + }
1905 +
1906 + let monitor_idx = *self.window_monitor.get(&win).unwrap_or(&self.selected_monitor);
1907 + if monitor_idx != self.selected_monitor {
1908 + self.selected_monitor = monitor_idx;
1909 + }
1910 +
1911 + self.detach_stack(win);
1912 + self.attach_stack(win, monitor_idx);
1913 +
1914 + self.connection.change_window_attributes(
1915 + win,
1916 + &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused),
1917 + )?;
1918 +
1919 + self.connection.ungrab_button(ButtonIndex::ANY, win, ModMask::ANY)?;
1920 +
1921 + self.connection.set_input_focus(
1922 + InputFocus::POINTER_ROOT,
1923 + win,
1924 + x11rb::CURRENT_TIME,
1925 + )?;
1926 +
1927 + if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
1928 + monitor.selected_client = Some(win);
1929 + }
1930 +
1931 + self.previous_focused = Some(win);
1932 + } else {
1933 + self.connection.set_input_focus(
1934 + InputFocus::POINTER_ROOT,
1935 + self.root,
1936 + x11rb::CURRENT_TIME,
1937 + )?;
1938 +
1939 + if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
1940 + monitor.selected_client = None;
1941 + }
1942 + }
1943 +
1944 + self.restack()?;
1945 + self.connection.flush()?;
1946 +
1947 + Ok(())
1948 + }
1949 +
1950 + fn restack(&mut self) -> WmResult<()> {
1951 + let monitor = match self.monitors.get(self.selected_monitor) {
1952 + Some(m) => m,
1953 + None => return Ok(()),
1954 + };
1955 +
1956 + let mut windows_to_restack: Vec<Window> = Vec::new();
1957 +
1958 + if let Some(selected) = monitor.selected_client {
1959 + if self.floating_windows.contains(&selected) {
1960 + windows_to_restack.push(selected);
1961 + }
1962 + }
1963 +
1964 + let mut current = monitor.stack_head;
1965 + while let Some(win) = current {
1966 + if self.windows.contains(&win) && self.floating_windows.contains(&win) {
1967 + if Some(win) != monitor.selected_client {
1968 + windows_to_restack.push(win);
1969 + }
1970 + }
1971 + current = self.clients.get(&win).and_then(|c| c.stack_next);
1972 + }
1973 +
1974 + current = monitor.stack_head;
1975 + while let Some(win) = current {
1976 + if self.windows.contains(&win) && !self.floating_windows.contains(&win) {
1977 + windows_to_restack.push(win);
1978 + }
1979 + current = self.clients.get(&win).and_then(|c| c.stack_next);
1980 + }
1981 +
1982 + for (i, &win) in windows_to_restack.iter().enumerate() {
1983 + if i == 0 {
1984 + self.connection.configure_window(
1985 + win,
1986 + &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE),
1987 + )?;
1988 + } else {
1989 + self.connection.configure_window(
1990 + win,
1991 + &ConfigureWindowAux::new()
1992 + .sibling(windows_to_restack[i - 1])
1993 + .stack_mode(StackMode::BELOW),
1994 + )?;
1995 + }
1996 + }
1997 +
1998 + Ok(())
1999 + }
2000 +
2001 + fn focusstack(&mut self, direction: i32) -> WmResult<()> {
2002 + let monitor = match self.monitors.get(self.selected_monitor) {
2003 + Some(m) => m,
2004 + None => return Ok(()),
2005 + };
2006 +
2007 + let selected = match monitor.selected_client {
2008 + Some(win) => win,
2009 + None => return Ok(()),
2010 + };
2011 +
2012 + let selected_tags = monitor.tagset[monitor.selected_tags_index];
2013 +
2014 + let mut stack_windows: Vec<Window> = Vec::new();
2015 + let mut current = monitor.clients_head;
2016 + while let Some(win) = current {
2017 + if let Some(client) = self.clients.get(&win) {
2018 + if client.tags & selected_tags != 0 && !client.is_floating {
2019 + stack_windows.push(win);
2020 + }
2021 + current = client.next;
2022 + } else {
2023 + break;
2024 + }
2025 + }
2026 +
2027 + if stack_windows.is_empty() {
2028 + return Ok(());
2029 + }
2030 +
2031 + let current_idx = stack_windows.iter().position(|&w| w == selected);
2032 +
2033 + let next_window = if let Some(idx) = current_idx {
2034 + if direction > 0 {
2035 + if idx + 1 < stack_windows.len() {
2036 + stack_windows[idx + 1]
2037 + } else {
2038 + stack_windows[0]
2039 + }
2040 + } else {
2041 + if idx > 0 {
2042 + stack_windows[idx - 1]
2043 + } else {
2044 + stack_windows[stack_windows.len() - 1]
2045 + }
2046 + }
2047 + } else {
2048 + return Ok(());
2049 + };
2050 +
2051 + self.focus(Some(next_window))?;
2052 +
2053 + Ok(())
2054 + }
2055 +
1845 fn update_focus_visuals( 2056 fn update_focus_visuals(
1846 &self, 2057 &self,
1847 old_focused: Option<Window>, 2058 old_focused: Option<Window>,
@@ -2082,6 +2293,20 @@ impl WindowManager {
2082 self.remove_window(event.window)?; 2293 self.remove_window(event.window)?;
2083 } 2294 }
2084 } 2295 }
2296 + Event::PropertyNotify(event) => {
2297 + if !self.windows.contains(&event.window) {
2298 + return Ok(None);
2299 + }
2300 +
2301 + if event.atom == self.atoms.wm_name || event.atom == self.atoms.net_wm_name {
2302 + let _ = self.update_window_title(event.window);
2303 + if self.layout.name() == "tabbed" {
2304 + self.update_tab_bars()?;
2305 + }
2306 + } else if event.atom == self.atoms.wm_normal_hints {
2307 + let _ = self.update_size_hints(event.window);
2308 + }
2309 + }
2085 Event::EnterNotify(event) => { 2310 Event::EnterNotify(event) => {
2086 if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL { 2311 if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL {
2087 return Ok(None); 2312 return Ok(None);
@@ -2093,7 +2318,7 @@ impl WindowManager {
2093 self.update_bar()?; 2318 self.update_bar()?;
2094 } 2319 }
2095 } 2320 }
2096 - self.set_focus(event.event)?; 2321 + self.focus(Some(event.event))?;
2097 } 2322 }
2098 } 2323 }
2099 Event::MotionNotify(event) => { 2324 Event::MotionNotify(event) => {
@@ -2110,7 +2335,7 @@ impl WindowManager {
2110 2335
2111 let visible = self.visible_windows_on_monitor(monitor_index); 2336 let visible = self.visible_windows_on_monitor(monitor_index);
2112 if let Some(&win) = visible.first() { 2337 if let Some(&win) = visible.first() {
2113 - self.set_focus(win)?; 2338 + self.focus(Some(win))?;
2114 } 2339 }
2115 } 2340 }
2116 } 2341 }
@@ -2237,11 +2462,11 @@ impl WindowManager {
2237 clicked_window, 2462 clicked_window,
2238 &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE), 2463 &ConfigureWindowAux::new().stack_mode(StackMode::ABOVE),
2239 )?; 2464 )?;
2240 - self.set_focus(clicked_window)?; 2465 + self.focus(Some(clicked_window))?;
2241 self.update_tab_bars()?; 2466 self.update_tab_bars()?;
2242 } 2467 }
2243 } else if event.child != x11rb::NONE { 2468 } else if event.child != x11rb::NONE {
2244 - self.set_focus(event.child)?; 2469 + self.focus(Some(event.child))?;
2245 2470
2246 if event.detail == ButtonIndex::M1.into() { 2471 if event.detail == ButtonIndex::M1.into() {
2247 self.move_mouse(event.child)?; 2472 self.move_mouse(event.child)?;
@@ -2614,11 +2839,11 @@ impl WindowManager {
2614 Ok(()) 2839 Ok(())
2615 } 2840 }
2616 2841
2617 - fn update_size_hints(&mut self, client: &mut Client) -> WmResult<()> { 2842 + fn update_size_hints(&mut self, window: Window) -> WmResult<()> {
2618 let size_hints = self.connection 2843 let size_hints = self.connection
2619 .get_property( 2844 .get_property(
2620 false, 2845 false,
2621 - client.window, 2846 + window,
2622 x11rb::protocol::xproto::AtomEnum::WM_NORMAL_HINTS, 2847 x11rb::protocol::xproto::AtomEnum::WM_NORMAL_HINTS,
2623 x11rb::protocol::xproto::AtomEnum::WM_SIZE_HINTS, 2848 x11rb::protocol::xproto::AtomEnum::WM_SIZE_HINTS,
2624 0, 2849 0,
@@ -2627,12 +2852,16 @@ impl WindowManager {
2627 .reply()?; 2852 .reply()?;
2628 2853
2629 if size_hints.value.is_empty() { 2854 if size_hints.value.is_empty() {
2630 - client.hints_valid = false; 2855 + if let Some(client) = self.clients.get_mut(&window) {
2856 + client.hints_valid = false;
2857 + }
2631 return Ok(()); 2858 return Ok(());
2632 } 2859 }
2633 2860
2634 if size_hints.value.len() < 18 * 4 { 2861 if size_hints.value.len() < 18 * 4 {
2635 - client.hints_valid = false; 2862 + if let Some(client) = self.clients.get_mut(&window) {
2863 + client.hints_valid = false;
2864 + }
2636 return Ok(()); 2865 return Ok(());
2637 } 2866 }
2638 2867
@@ -2650,66 +2879,68 @@ impl WindowManager {
2650 const P_ASPECT: u32 = 1 << 7; 2879 const P_ASPECT: u32 = 1 << 7;
2651 const P_BASE_SIZE: u32 = 1 << 8; 2880 const P_BASE_SIZE: u32 = 1 << 8;
2652 2881
2653 - if flags & P_BASE_SIZE != 0 { 2882 + if let Some(client) = self.clients.get_mut(&window) {
2654 - client.base_width = read_u32(8) as i32; 2883 + if flags & P_BASE_SIZE != 0 {
2655 - client.base_height = read_u32(9) as i32; 2884 + client.base_width = read_u32(8) as i32;
2656 - } else if flags & P_MIN_SIZE != 0 { 2885 + client.base_height = read_u32(9) as i32;
2657 - client.base_width = read_u32(5) as i32; 2886 + } else if flags & P_MIN_SIZE != 0 {
2658 - client.base_height = read_u32(6) as i32; 2887 + client.base_width = read_u32(5) as i32;
2659 - } else { 2888 + client.base_height = read_u32(6) as i32;
2660 - client.base_width = 0; 2889 + } else {
2661 - client.base_height = 0; 2890 + client.base_width = 0;
2662 - } 2891 + client.base_height = 0;
2892 + }
2663 2893
2664 - if flags & P_RESIZE_INC != 0 { 2894 + if flags & P_RESIZE_INC != 0 {
2665 - client.increment_width = read_u32(10) as i32; 2895 + client.increment_width = read_u32(10) as i32;
2666 - client.increment_height = read_u32(11) as i32; 2896 + client.increment_height = read_u32(11) as i32;
2667 - } else { 2897 + } else {
2668 - client.increment_width = 0; 2898 + client.increment_width = 0;
2669 - client.increment_height = 0; 2899 + client.increment_height = 0;
2670 - } 2900 + }
2671 2901
2672 - if flags & P_MAX_SIZE != 0 { 2902 + if flags & P_MAX_SIZE != 0 {
2673 - client.max_width = read_u32(7) as i32; 2903 + client.max_width = read_u32(7) as i32;
2674 - client.max_height = read_u32(8) as i32; 2904 + client.max_height = read_u32(8) as i32;
2675 - } else { 2905 + } else {
2676 - client.max_width = 0; 2906 + client.max_width = 0;
2677 - client.max_height = 0; 2907 + client.max_height = 0;
2678 - } 2908 + }
2679 2909
2680 - if flags & P_MIN_SIZE != 0 { 2910 + if flags & P_MIN_SIZE != 0 {
2681 - client.min_width = read_u32(5) as i32; 2911 + client.min_width = read_u32(5) as i32;
2682 - client.min_height = read_u32(6) as i32; 2912 + client.min_height = read_u32(6) as i32;
2683 - } else if flags & P_SIZE != 0 { 2913 + } else if flags & P_SIZE != 0 {
2684 - client.min_width = read_u32(3) as i32; 2914 + client.min_width = read_u32(3) as i32;
2685 - client.min_height = read_u32(4) as i32; 2915 + client.min_height = read_u32(4) as i32;
2686 - } else { 2916 + } else {
2687 - client.min_width = 0; 2917 + client.min_width = 0;
2688 - client.min_height = 0; 2918 + client.min_height = 0;
2689 - } 2919 + }
2690 2920
2691 - if flags & P_ASPECT != 0 { 2921 + if flags & P_ASPECT != 0 {
2692 - client.min_aspect = (read_u32(12) as f32) / (read_u32(13) as f32).max(1.0); 2922 + client.min_aspect = (read_u32(12) as f32) / (read_u32(13) as f32).max(1.0);
2693 - client.max_aspect = (read_u32(14) as f32) / (read_u32(15) as f32).max(1.0); 2923 + client.max_aspect = (read_u32(14) as f32) / (read_u32(15) as f32).max(1.0);
2694 - } else { 2924 + } else {
2695 - client.min_aspect = 0.0; 2925 + client.min_aspect = 0.0;
2696 - client.max_aspect = 0.0; 2926 + client.max_aspect = 0.0;
2697 - } 2927 + }
2698 2928
2699 - client.is_fixed = client.max_width > 0 2929 + client.is_fixed = client.max_width > 0
2700 - && client.max_height > 0 2930 + && client.max_height > 0
2701 - && client.max_width == client.min_width 2931 + && client.max_width == client.min_width
2702 - && client.max_height == client.min_height; 2932 + && client.max_height == client.min_height;
2703 2933
2704 - client.hints_valid = true; 2934 + client.hints_valid = true;
2935 + }
2705 Ok(()) 2936 Ok(())
2706 } 2937 }
2707 2938
2708 - fn update_window_title(&mut self, client: &mut Client) -> WmResult<()> { 2939 + fn update_window_title(&mut self, window: Window) -> WmResult<()> {
2709 let name = self.connection 2940 let name = self.connection
2710 .get_property( 2941 .get_property(
2711 false, 2942 false,
2712 - client.window, 2943 + window,
2713 x11rb::protocol::xproto::AtomEnum::WM_NAME, 2944 x11rb::protocol::xproto::AtomEnum::WM_NAME,
2714 x11rb::protocol::xproto::AtomEnum::STRING, 2945 x11rb::protocol::xproto::AtomEnum::STRING,
2715 0, 2946 0,
@@ -2719,7 +2950,9 @@ impl WindowManager {
2719 2950
2720 if !name.value.is_empty() { 2951 if !name.value.is_empty() {
2721 if let Ok(title) = String::from_utf8(name.value.clone()) { 2952 if let Ok(title) = String::from_utf8(name.value.clone()) {
2722 - client.name = title; 2953 + if let Some(client) = self.clients.get_mut(&window) {
2954 + client.name = title;
2955 + }
2723 } 2956 }
2724 } 2957 }
2725 2958
@@ -2902,6 +3135,11 @@ impl WindowManager {
2902 fn remove_window(&mut self, window: Window) -> WmResult<()> { 3135 fn remove_window(&mut self, window: Window) -> WmResult<()> {
2903 let initial_count = self.windows.len(); 3136 let initial_count = self.windows.len();
2904 3137
3138 + let focused = self
3139 + .monitors
3140 + .get(self.selected_monitor)
3141 + .and_then(|m| m.selected_client);
3142 +
2905 if self.clients.contains_key(&window) { 3143 if self.clients.contains_key(&window) {
2906 self.detach(window); 3144 self.detach(window);
2907 self.detach_stack(window); 3145 self.detach_stack(window);
@@ -2915,14 +3153,10 @@ impl WindowManager {
2915 self.floating_windows.remove(&window); 3153 self.floating_windows.remove(&window);
2916 3154
2917 if self.windows.len() < initial_count { 3155 if self.windows.len() < initial_count {
2918 - let focused = self
2919 - .monitors
2920 - .get(self.selected_monitor)
2921 - .and_then(|m| m.selected_client);
2922 if focused == Some(window) { 3156 if focused == Some(window) {
2923 let visible = self.visible_windows_on_monitor(self.selected_monitor); 3157 let visible = self.visible_windows_on_monitor(self.selected_monitor);
2924 if let Some(&new_win) = visible.last() { 3158 if let Some(&new_win) = visible.last() {
2925 - self.set_focus(new_win)?; 3159 + self.focus(Some(new_win))?;
2926 } else if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { 3160 } else if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
2927 monitor.selected_client = None; 3161 monitor.selected_client = None;
2928 } 3162 }