tony / oxwm

Merge pull request #110 from tonybanters/dwm-compliance Browse files
Dwm Compliance - Huge Refactor to fullscreen.
Tony, btw. committed 2026-01-05 parents 755fc86 09af55f commit fdc348b1e4e2d14da52c8bb6bfeb4246e240a72f
Showing 3 changed files with 378 additions and 153 deletions. UnifiedSplit
+1 −1 Cargo.lock
@@ -291,7 +291,7 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
291 291
292 [[package]] 292 [[package]]
293 name = "oxwm" 293 name = "oxwm"
294 -version = "0.9.1" 294 +version = "0.10.0"
295 dependencies = [ 295 dependencies = [
296 "chrono", 296 "chrono",
297 "dirs", 297 "dirs",
+1 −1 Cargo.toml
@@ -1,6 +1,6 @@
1 [package] 1 [package]
2 name = "oxwm" 2 name = "oxwm"
3 -version = "0.9.1" 3 +version = "0.10.0"
4 edition = "2024" 4 edition = "2024"
5 5
6 [lib] 6 [lib]
+376 −151 src/window_manager.rs Large diff collapsed, click to expand
@@ -25,14 +25,13 @@ pub fn tag_mask(tag: usize) -> TagMask {
25 1 << tag 25 1 << tag
26 } 26 }
27 27
28 -/// Get back a tag index from a [`TagMask`]
29 pub fn unmask_tag(mask: TagMask) -> usize { 28 pub fn unmask_tag(mask: TagMask) -> usize {
30 - // mask only has one bit set, so this works.
31 mask.trailing_zeros() as usize 29 mask.trailing_zeros() as usize
32 } 30 }
33 31
34 struct AtomCache { 32 struct AtomCache {
35 net_supported: Atom, 33 net_supported: Atom,
34 + net_supporting_wm_check: Atom,
36 net_current_desktop: Atom, 35 net_current_desktop: Atom,
37 net_client_info: Atom, 36 net_client_info: Atom,
38 wm_state: Atom, 37 wm_state: Atom,
@@ -46,6 +45,8 @@ struct AtomCache {
46 net_wm_name: Atom, 45 net_wm_name: Atom,
47 utf8_string: Atom, 46 utf8_string: Atom,
48 net_active_window: Atom, 47 net_active_window: Atom,
48 + wm_take_focus: Atom,
49 + net_client_list: Atom,
49 } 50 }
50 51
51 impl AtomCache { 52 impl AtomCache {
@@ -55,6 +56,11 @@ impl AtomCache {
55 .reply()? 56 .reply()?
56 .atom; 57 .atom;
57 58
59 + let net_supporting_wm_check = connection
60 + .intern_atom(false, b"_NET_SUPPORTING_WM_CHECK")?
61 + .reply()?
62 + .atom;
63 +
58 let net_current_desktop = connection 64 let net_current_desktop = connection
59 .intern_atom(false, b"_NET_CURRENT_DESKTOP")? 65 .intern_atom(false, b"_NET_CURRENT_DESKTOP")?
60 .reply()? 66 .reply()?
@@ -108,8 +114,19 @@ impl AtomCache {
108 .reply()? 114 .reply()?
109 .atom; 115 .atom;
110 116
117 + let wm_take_focus = connection
118 + .intern_atom(false, b"WM_TAKE_FOCUS")?
119 + .reply()?
120 + .atom;
121 +
122 + let net_client_list = connection
123 + .intern_atom(false, b"_NET_CLIENT_LIST")?
124 + .reply()?
125 + .atom;
126 +
111 Ok(Self { 127 Ok(Self {
112 net_supported, 128 net_supported,
129 + net_supporting_wm_check,
113 net_current_desktop, 130 net_current_desktop,
114 net_client_info, 131 net_client_info,
115 wm_state, 132 wm_state,
@@ -123,6 +140,8 @@ impl AtomCache {
123 net_wm_name, 140 net_wm_name,
124 utf8_string, 141 utf8_string,
125 net_active_window, 142 net_active_window,
143 + wm_take_focus,
144 + net_client_list,
126 }) 145 })
127 } 146 }
128 } 147 }
@@ -132,6 +151,7 @@ pub struct WindowManager {
132 connection: RustConnection, 151 connection: RustConnection,
133 screen_number: usize, 152 screen_number: usize,
134 root: Window, 153 root: Window,
154 + _wm_check_window: Window,
135 screen: Screen, 155 screen: Screen,
136 windows: Vec<Window>, 156 windows: Vec<Window>,
137 clients: HashMap<Window, Client>, 157 clients: HashMap<Window, Client>,
@@ -274,6 +294,7 @@ impl WindowManager {
274 294
275 let supported_atoms: Vec<Atom> = vec![ 295 let supported_atoms: Vec<Atom> = vec![
276 atoms.net_supported, 296 atoms.net_supported,
297 + atoms.net_supporting_wm_check,
277 atoms.net_wm_state, 298 atoms.net_wm_state,
278 atoms.net_wm_state_fullscreen, 299 atoms.net_wm_state_fullscreen,
279 atoms.net_wm_window_type, 300 atoms.net_wm_window_type,
@@ -282,6 +303,7 @@ impl WindowManager {
282 atoms.net_wm_name, 303 atoms.net_wm_name,
283 atoms.net_current_desktop, 304 atoms.net_current_desktop,
284 atoms.net_client_info, 305 atoms.net_client_info,
306 + atoms.net_client_list,
285 ]; 307 ];
286 let supported_bytes: Vec<u8> = supported_atoms 308 let supported_bytes: Vec<u8> = supported_atoms
287 .iter() 309 .iter()
@@ -297,6 +319,51 @@ impl WindowManager {
297 &supported_bytes, 319 &supported_bytes,
298 )?; 320 )?;
299 321
322 + let wm_check_window = connection.generate_id()?;
323 + connection.create_window(
324 + screen.root_depth,
325 + wm_check_window,
326 + root,
327 + 0,
328 + 0,
329 + 1,
330 + 1,
331 + 0,
332 + WindowClass::INPUT_OUTPUT,
333 + 0,
334 + &CreateWindowAux::new(),
335 + )?;
336 +
337 + connection.change_property(
338 + PropMode::REPLACE,
339 + wm_check_window,
340 + atoms.net_supporting_wm_check,
341 + AtomEnum::WINDOW,
342 + 32,
343 + 1,
344 + &wm_check_window.to_ne_bytes(),
345 + )?;
346 +
347 + connection.change_property(
348 + PropMode::REPLACE,
349 + wm_check_window,
350 + atoms.net_wm_name,
351 + atoms.utf8_string,
352 + 8,
353 + 4,
354 + b"oxwm",
355 + )?;
356 +
357 + connection.change_property(
358 + PropMode::REPLACE,
359 + root,
360 + atoms.net_supporting_wm_check,
361 + AtomEnum::WINDOW,
362 + 32,
363 + 1,
364 + &wm_check_window.to_ne_bytes(),
365 + )?;
366 +
300 let overlay = ErrorOverlay::new( 367 let overlay = ErrorOverlay::new(
301 &connection, 368 &connection,
302 &screen, 369 &screen,
@@ -314,6 +381,7 @@ impl WindowManager {
314 connection, 381 connection,
315 screen_number, 382 screen_number,
316 root, 383 root,
384 + _wm_check_window: wm_check_window,
317 screen, 385 screen,
318 windows: Vec::new(), 386 windows: Vec::new(),
319 clients: HashMap::new(), 387 clients: HashMap::new(),
@@ -527,6 +595,26 @@ impl WindowManager {
527 Ok(()) 595 Ok(())
528 } 596 }
529 597
598 + fn update_client_list(&self) -> WmResult<()> {
599 + let window_bytes: Vec<u8> = self
600 + .windows
601 + .iter()
602 + .flat_map(|window| window.to_ne_bytes())
603 + .collect();
604 +
605 + self.connection.change_property(
606 + PropMode::REPLACE,
607 + self.root,
608 + self.atoms.net_client_list,
609 + AtomEnum::WINDOW,
610 + 32,
611 + self.windows.len() as u32,
612 + &window_bytes,
613 + )?;
614 +
615 + Ok(())
616 + }
617 +
530 pub fn run(&mut self) -> WmResult<()> { 618 pub fn run(&mut self) -> WmResult<()> {
531 println!("oxwm started on display {}", self.screen_number); 619 println!("oxwm started on display {}", self.screen_number);
532 620
@@ -827,9 +915,7 @@ impl WindowManager {
827 self.restack()?; 915 self.restack()?;
828 } 916 }
829 } 917 }
830 - KeyAction::Quit | KeyAction::Restart => { 918 + KeyAction::Quit | KeyAction::Restart => {}
831 - // Handled in handle_event
832 - }
833 KeyAction::ViewTag => { 919 KeyAction::ViewTag => {
834 if let Arg::Int(tag_index) = arg { 920 if let Arg::Int(tag_index) = arg {
835 self.view_tag(*tag_index as usize)?; 921 self.view_tag(*tag_index as usize)?;
@@ -1025,7 +1111,7 @@ impl WindowManager {
1025 return Ok(()); 1111 return Ok(());
1026 } 1112 }
1027 1113
1028 - self.unfocus(window)?; 1114 + self.unfocus(window, false)?;
1029 self.detach(window); 1115 self.detach(window);
1030 self.detach_stack(window); 1116 self.detach_stack(window);
1031 1117
@@ -1544,6 +1630,7 @@ impl WindowManager {
1544 client.old_width = client.width; 1630 client.old_width = client.width;
1545 client.old_height = client.height; 1631 client.old_height = client.height;
1546 client.border_width = 0; 1632 client.border_width = 0;
1633 + client.is_floating = true;
1547 } 1634 }
1548 1635
1549 self.fullscreen_windows.insert(window); 1636 self.fullscreen_windows.insert(window);
@@ -1574,11 +1661,20 @@ impl WindowManager {
1574 1661
1575 self.fullscreen_windows.remove(&window); 1662 self.fullscreen_windows.remove(&window);
1576 1663
1577 - let was_floating = self 1664 + let (was_floating, restored_x, restored_y, restored_width, restored_height, restored_border) = self
1578 .clients 1665 .clients
1579 .get(&window) 1666 .get(&window)
1580 - .map(|c| c.old_state) 1667 + .map(|client| {
1581 - .unwrap_or(false); 1668 + (
1669 + client.old_state,
1670 + client.old_x_position,
1671 + client.old_y_position,
1672 + client.old_width,
1673 + client.old_height,
1674 + client.old_border_width,
1675 + )
1676 + })
1677 + .unwrap_or((false, 0, 0, 100, 100, 0));
1582 1678
1583 if !was_floating { 1679 if !was_floating {
1584 self.floating_windows.remove(&window); 1680 self.floating_windows.remove(&window);
@@ -1588,8 +1684,22 @@ impl WindowManager {
1588 client.is_fullscreen = false; 1684 client.is_fullscreen = false;
1589 client.is_floating = client.old_state; 1685 client.is_floating = client.old_state;
1590 client.border_width = client.old_border_width; 1686 client.border_width = client.old_border_width;
1687 + client.x_position = client.old_x_position;
1688 + client.y_position = client.old_y_position;
1689 + client.width = client.old_width;
1690 + client.height = client.old_height;
1591 } 1691 }
1592 1692
1693 + self.connection.configure_window(
1694 + window,
1695 + &ConfigureWindowAux::new()
1696 + .x(restored_x as i32)
1697 + .y(restored_y as i32)
1698 + .width(restored_width as u32)
1699 + .height(restored_height as u32)
1700 + .border_width(restored_border as u32),
1701 + )?;
1702 +
1593 self.apply_layout()?; 1703 self.apply_layout()?;
1594 } 1704 }
1595 1705
@@ -1688,25 +1798,21 @@ impl WindowManager {
1688 client.monitor_index = monitor_index; 1798 client.monitor_index = monitor_index;
1689 } 1799 }
1690 1800
1691 - let monitor_tagset = || { 1801 + if let Some(tags) = rule_tags {
1692 - self.monitors 1802 + client.tags = tags;
1693 - .get(client.monitor_index)
1694 - .map(|m| m.get_selected_tag())
1695 - .unwrap_or(tag_mask(0))
1696 - };
1697 -
1698 - let tags = rule_tags.unwrap_or_else(monitor_tagset);
1699 -
1700 - client.tags = tags;
1701 1803
1702 - // Focus window rule tag if config option is set 1804 + if rule_focus {
1703 - if rule_focus && let Some(tag_mask) = rule_tags { 1805 + let tag_index = unmask_tag(tags);
1704 - let tag_index = unmask_tag(tag_mask); 1806 + let monitor_tagset = self
1705 - let monitor_tagset = monitor_tagset(); 1807 + .monitors
1706 - let is_tag_focused = monitor_tagset & tag_mask == tag_mask; 1808 + .get(client.monitor_index)
1809 + .map(|monitor| monitor.get_selected_tag())
1810 + .unwrap_or(tag_mask(0));
1811 + let is_tag_focused = monitor_tagset & tags == tags;
1707 1812
1708 - if !is_tag_focused { 1813 + if !is_tag_focused {
1709 - self.view_tag(tag_index)?; 1814 + self.view_tag(tag_index)?;
1815 + }
1710 } 1816 }
1711 } 1817 }
1712 } 1818 }
@@ -1728,7 +1834,7 @@ impl WindowManager {
1728 let tags = self 1834 let tags = self
1729 .monitors 1835 .monitors
1730 .get(self.selected_monitor) 1836 .get(self.selected_monitor)
1731 - .map(|m| m.tagset[m.selected_tags_index]) 1837 + .map(|monitor| monitor.tagset[monitor.selected_tags_index])
1732 .unwrap_or(tag_mask(0)); 1838 .unwrap_or(tag_mask(0));
1733 (self.selected_monitor, tags) 1839 (self.selected_monitor, tags)
1734 } 1840 }
@@ -1736,7 +1842,7 @@ impl WindowManager {
1736 let tags = self 1842 let tags = self
1737 .monitors 1843 .monitors
1738 .get(self.selected_monitor) 1844 .get(self.selected_monitor)
1739 - .map(|m| m.tagset[m.selected_tags_index]) 1845 + .map(|monitor| monitor.tagset[monitor.selected_tags_index])
1740 .unwrap_or(tag_mask(0)); 1846 .unwrap_or(tag_mask(0));
1741 (self.selected_monitor, tags) 1847 (self.selected_monitor, tags)
1742 }; 1848 };
@@ -1866,26 +1972,14 @@ impl WindowManager {
1866 )?; 1972 )?;
1867 1973
1868 self.set_wm_state(window, 1)?; 1974 self.set_wm_state(window, 1)?;
1975 + self.update_client_list()?;
1869 1976
1870 let final_tags = self.clients.get(&window).map(|c| c.tags).unwrap_or(tags); 1977 let final_tags = self.clients.get(&window).map(|c| c.tags).unwrap_or(tags);
1871 let _ = self.save_client_tag(window, final_tags); 1978 let _ = self.save_client_tag(window, final_tags);
1872 1979
1873 - if client_monitor == self.selected_monitor
1874 - && let Some(old_sel) = self
1875 - .monitors
1876 - .get(self.selected_monitor)
1877 - .and_then(|m| m.selected_client)
1878 - {
1879 - self.unfocus(old_sel)?;
1880 - }
1881 -
1882 - if let Some(m) = self.monitors.get_mut(client_monitor) {
1883 - m.selected_client = Some(window);
1884 - }
1885 -
1886 self.apply_layout()?; 1980 self.apply_layout()?;
1887 self.connection.map_window(window)?; 1981 self.connection.map_window(window)?;
1888 - self.focus(Some(window))?; 1982 + self.focus(None)?;
1889 self.update_bar()?; 1983 self.update_bar()?;
1890 1984
1891 if self.layout.name() == "tabbed" { 1985 if self.layout.name() == "tabbed" {
@@ -1896,108 +1990,190 @@ impl WindowManager {
1896 } 1990 }
1897 1991
1898 pub fn set_focus(&mut self, window: Window) -> WmResult<()> { 1992 pub fn set_focus(&mut self, window: Window) -> WmResult<()> {
1899 - let old_focused = self.previous_focused; 1993 + let never_focus = self
1994 + .clients
1995 + .get(&window)
1996 + .map(|c| c.never_focus)
1997 + .unwrap_or(false);
1900 1998
1901 - if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { 1999 + if !never_focus {
1902 - monitor.selected_client = Some(window); 2000 + self.connection
2001 + .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?;
2002 +
2003 + self.connection.change_property(
2004 + PropMode::REPLACE,
2005 + self.root,
2006 + self.atoms.net_active_window,
2007 + AtomEnum::WINDOW,
2008 + 32,
2009 + 1,
2010 + &window.to_ne_bytes(),
2011 + )?;
1903 } 2012 }
1904 2013
1905 - self.connection 2014 + let _ = self.send_event(window, self.atoms.wm_take_focus);
1906 - .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?;
1907 self.connection.flush()?; 2015 self.connection.flush()?;
1908 2016
1909 - self.update_focus_visuals(old_focused, window)?; 2017 + Ok(())
1910 - self.previous_focused = Some(window); 2018 + }
1911 2019
1912 - if self.layout.name() == "tabbed" { 2020 + fn grabbuttons(&self, window: Window, focused: bool) -> WmResult<()> {
1913 - self.update_tab_bars()?; 2021 + self.connection.ungrab_button(ButtonIndex::ANY, window, ModMask::ANY)?;
2022 +
2023 + if !focused {
2024 + self.connection.grab_button(
2025 + false,
2026 + window,
2027 + EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE,
2028 + GrabMode::SYNC,
2029 + GrabMode::SYNC,
2030 + x11rb::NONE,
2031 + x11rb::NONE,
2032 + ButtonIndex::ANY,
2033 + ModMask::ANY,
2034 + )?;
2035 + }
2036 +
2037 + let ignore_modifiers = [
2038 + 0u16,
2039 + u16::from(ModMask::LOCK),
2040 + u16::from(ModMask::M2),
2041 + u16::from(ModMask::LOCK | ModMask::M2),
2042 + ];
2043 +
2044 + for &ignore_mask in &ignore_modifiers {
2045 + let grab_mask = u16::from(self.config.modkey) | ignore_mask;
2046 +
2047 + self.connection.grab_button(
2048 + false,
2049 + window,
2050 + EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE,
2051 + GrabMode::ASYNC,
2052 + GrabMode::SYNC,
2053 + x11rb::NONE,
2054 + x11rb::NONE,
2055 + ButtonIndex::M1,
2056 + grab_mask.into(),
2057 + )?;
2058 +
2059 + self.connection.grab_button(
2060 + false,
2061 + window,
2062 + EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE,
2063 + GrabMode::ASYNC,
2064 + GrabMode::SYNC,
2065 + x11rb::NONE,
2066 + x11rb::NONE,
2067 + ButtonIndex::M3,
2068 + grab_mask.into(),
2069 + )?;
1914 } 2070 }
1915 2071
1916 Ok(()) 2072 Ok(())
1917 } 2073 }
1918 2074
1919 - fn unfocus(&self, window: Window) -> WmResult<()> { 2075 + fn unfocus(&self, window: Window, reset_input_focus: bool) -> WmResult<()> {
1920 if !self.windows.contains(&window) { 2076 if !self.windows.contains(&window) {
1921 return Ok(()); 2077 return Ok(());
1922 } 2078 }
1923 2079
2080 + self.grabbuttons(window, false)?;
2081 +
1924 self.connection.change_window_attributes( 2082 self.connection.change_window_attributes(
1925 window, 2083 window,
1926 &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), 2084 &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused),
1927 )?; 2085 )?;
1928 2086
1929 - self.connection.grab_button( 2087 + if reset_input_focus {
1930 - false, 2088 + self.connection.set_input_focus(
1931 - window, 2089 + InputFocus::POINTER_ROOT,
1932 - EventMask::BUTTON_PRESS, 2090 + self.root,
1933 - GrabMode::SYNC, 2091 + x11rb::CURRENT_TIME,
1934 - GrabMode::SYNC, 2092 + )?;
1935 - x11rb::NONE, 2093 + self.connection.delete_property(self.root, self.atoms.net_active_window)?;
1936 - x11rb::NONE, 2094 + }
1937 - ButtonIndex::ANY,
1938 - ModMask::ANY,
1939 - )?;
1940 2095
1941 Ok(()) 2096 Ok(())
1942 } 2097 }
1943 2098
1944 fn focus(&mut self, window: Option<Window>) -> WmResult<()> { 2099 fn focus(&mut self, window: Option<Window>) -> WmResult<()> {
1945 - let monitor = self.monitors.get_mut(self.selected_monitor).unwrap(); 2100 + let old_selected = self
1946 - let old_selected = monitor.selected_client; 2101 + .monitors
2102 + .get(self.selected_monitor)
2103 + .and_then(|m| m.selected_client);
1947 2104
1948 - if let Some(old_win) = old_selected 2105 + let mut focus_client = window;
1949 - && old_selected != window 2106 + if focus_client.is_none()
2107 + || focus_client.is_some_and(|w| !self.is_visible(w))
1950 { 2108 {
1951 - self.unfocus(old_win)?;
1952 - }
1953 -
1954 - let mut win = window;
1955 - if win.is_none() || !self.is_visible(win.unwrap()) {
1956 let mut current = self 2109 let mut current = self
1957 .monitors 2110 .monitors
1958 .get(self.selected_monitor) 2111 .get(self.selected_monitor)
1959 .and_then(|m| m.stack_head); 2112 .and_then(|m| m.stack_head);
1960 2113
2114 + focus_client = None;
1961 while let Some(w) = current { 2115 while let Some(w) = current {
1962 if self.is_visible(w) { 2116 if self.is_visible(w) {
1963 - win = Some(w); 2117 + focus_client = Some(w);
1964 break; 2118 break;
1965 } 2119 }
1966 current = self.clients.get(&w).and_then(|c| c.stack_next); 2120 current = self.clients.get(&w).and_then(|c| c.stack_next);
1967 } 2121 }
1968 } 2122 }
1969 2123
1970 - if let Some(win) = win { 2124 + if old_selected != focus_client {
1971 - if !self.windows.contains(&win) { 2125 + if let Some(old_win) = old_selected {
1972 - return Ok(()); 2126 + self.unfocus(old_win, false)?;
1973 - }
1974 -
1975 - if self.clients.get(&win).is_some_and(|c| c.is_urgent) {
1976 - self.set_urgent(win, false)?;
1977 } 2127 }
2128 + }
1978 2129
2130 + if let Some(win) = focus_client {
1979 let monitor_idx = self 2131 let monitor_idx = self
1980 .clients 2132 .clients
1981 .get(&win) 2133 .get(&win)
1982 .map(|c| c.monitor_index) 2134 .map(|c| c.monitor_index)
1983 .unwrap_or(self.selected_monitor); 2135 .unwrap_or(self.selected_monitor);
2136 +
1984 if monitor_idx != self.selected_monitor { 2137 if monitor_idx != self.selected_monitor {
1985 self.selected_monitor = monitor_idx; 2138 self.selected_monitor = monitor_idx;
1986 } 2139 }
1987 2140
2141 + if self.clients.get(&win).is_some_and(|c| c.is_urgent) {
2142 + self.set_urgent(win, false)?;
2143 + }
2144 +
1988 self.detach_stack(win); 2145 self.detach_stack(win);
1989 self.attach_stack(win, monitor_idx); 2146 self.attach_stack(win, monitor_idx);
1990 2147
2148 + self.grabbuttons(win, true)?;
2149 +
1991 self.connection.change_window_attributes( 2150 self.connection.change_window_attributes(
1992 win, 2151 win,
1993 &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused), 2152 &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused),
1994 )?; 2153 )?;
1995 2154
1996 - self.connection 2155 + let never_focus = self
1997 - .ungrab_button(ButtonIndex::ANY, win, ModMask::ANY)?; 2156 + .clients
2157 + .get(&win)
2158 + .map(|client| client.never_focus)
2159 + .unwrap_or(false);
1998 2160
1999 - self.connection 2161 + if !never_focus {
2000 - .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?; 2162 + self.connection
2163 + .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
2164 +
2165 + self.connection.change_property(
2166 + PropMode::REPLACE,
2167 + self.root,
2168 + self.atoms.net_active_window,
2169 + AtomEnum::WINDOW,
2170 + 32,
2171 + 1,
2172 + &win.to_ne_bytes(),
2173 + )?;
2174 + }
2175 +
2176 + let _ = self.send_event(win, self.atoms.wm_take_focus);
2001 2177
2002 if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { 2178 if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
2003 monitor.selected_client = Some(win); 2179 monitor.selected_client = Some(win);
@@ -2011,6 +2187,9 @@ impl WindowManager {
2011 x11rb::CURRENT_TIME, 2187 x11rb::CURRENT_TIME,
2012 )?; 2188 )?;
2013 2189
2190 + self.connection
2191 + .delete_property(self.root, self.atoms.net_active_window)?;
2192 +
2014 if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { 2193 if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
2015 monitor.selected_client = None; 2194 monitor.selected_client = None;
2016 } 2195 }
@@ -2075,25 +2254,35 @@ impl WindowManager {
2075 2254
2076 fn focusstack(&mut self, direction: i32) -> WmResult<()> { 2255 fn focusstack(&mut self, direction: i32) -> WmResult<()> {
2077 let monitor = match self.monitors.get(self.selected_monitor) { 2256 let monitor = match self.monitors.get(self.selected_monitor) {
2078 - Some(m) => m, 2257 + Some(monitor) => monitor,
2079 None => return Ok(()), 2258 None => return Ok(()),
2080 }; 2259 };
2081 2260
2082 - let selected = match monitor.selected_client { 2261 + let selected_window = match monitor.selected_client {
2083 - Some(win) => win, 2262 + Some(window) => window,
2084 None => return Ok(()), 2263 None => return Ok(()),
2085 }; 2264 };
2086 2265
2266 + let selected_is_fullscreen = self
2267 + .clients
2268 + .get(&selected_window)
2269 + .map(|client| client.is_fullscreen)
2270 + .unwrap_or(false);
2271 +
2272 + if selected_is_fullscreen {
2273 + return Ok(());
2274 + }
2275 +
2087 let selected_tags = monitor.tagset[monitor.selected_tags_index]; 2276 let selected_tags = monitor.tagset[monitor.selected_tags_index];
2088 2277
2089 let mut stack_windows: Vec<Window> = Vec::new(); 2278 let mut stack_windows: Vec<Window> = Vec::new();
2090 - let mut current = monitor.clients_head; 2279 + let mut current_window = monitor.clients_head;
2091 - while let Some(win) = current { 2280 + while let Some(window) = current_window {
2092 - if let Some(client) = self.clients.get(&win) { 2281 + if let Some(client) = self.clients.get(&window) {
2093 if client.tags & selected_tags != 0 && !client.is_floating { 2282 if client.tags & selected_tags != 0 && !client.is_floating {
2094 - stack_windows.push(win); 2283 + stack_windows.push(window);
2095 } 2284 }
2096 - current = client.next; 2285 + current_window = client.next;
2097 } else { 2286 } else {
2098 break; 2287 break;
2099 } 2288 }
@@ -2103,17 +2292,19 @@ impl WindowManager {
2103 return Ok(()); 2292 return Ok(());
2104 } 2293 }
2105 2294
2106 - let current_idx = stack_windows.iter().position(|&w| w == selected); 2295 + let current_index = stack_windows
2296 + .iter()
2297 + .position(|&window| window == selected_window);
2107 2298
2108 - let next_window = if let Some(idx) = current_idx { 2299 + let next_window = if let Some(index) = current_index {
2109 if direction > 0 { 2300 if direction > 0 {
2110 - if idx + 1 < stack_windows.len() { 2301 + if index + 1 < stack_windows.len() {
2111 - stack_windows[idx + 1] 2302 + stack_windows[index + 1]
2112 } else { 2303 } else {
2113 stack_windows[0] 2304 stack_windows[0]
2114 } 2305 }
2115 - } else if idx > 0 { 2306 + } else if index > 0 {
2116 - stack_windows[idx - 1] 2307 + stack_windows[index - 1]
2117 } else { 2308 } else {
2118 stack_windows[stack_windows.len() - 1] 2309 stack_windows[stack_windows.len() - 1]
2119 } 2310 }
@@ -2122,6 +2313,7 @@ impl WindowManager {
2122 }; 2313 };
2123 2314
2124 self.focus(Some(next_window))?; 2315 self.focus(Some(next_window))?;
2316 + self.restack()?;
2125 self.update_tab_bars()?; 2317 self.update_tab_bars()?;
2126 2318
2127 Ok(()) 2319 Ok(())
@@ -2275,7 +2467,7 @@ impl WindowManager {
2275 .and_then(|m| m.selected_client); 2467 .and_then(|m| m.selected_client);
2276 2468
2277 if let Some(win) = old_selected { 2469 if let Some(win) = old_selected {
2278 - self.unfocus(win)?; 2470 + self.unfocus(win, true)?;
2279 } 2471 }
2280 2472
2281 self.selected_monitor = target_monitor; 2473 self.selected_monitor = target_monitor;
@@ -2309,39 +2501,6 @@ impl WindowManager {
2309 Ok(()) 2501 Ok(())
2310 } 2502 }
2311 2503
2312 - fn update_focus_visuals(
2313 - &self,
2314 - old_focused: Option<Window>,
2315 - new_focused: Window,
2316 - ) -> WmResult<()> {
2317 - if let Some(old_win) = old_focused
2318 - && old_win != new_focused
2319 - {
2320 - self.connection.configure_window(
2321 - old_win,
2322 - &ConfigureWindowAux::new().border_width(self.config.border_width),
2323 - )?;
2324 -
2325 - self.connection.change_window_attributes(
2326 - old_win,
2327 - &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused),
2328 - )?;
2329 - }
2330 -
2331 - self.connection.configure_window(
2332 - new_focused,
2333 - &ConfigureWindowAux::new().border_width(self.config.border_width),
2334 - )?;
2335 -
2336 - self.connection.change_window_attributes(
2337 - new_focused,
2338 - &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused),
2339 - )?;
2340 -
2341 - self.connection.flush()?;
2342 - Ok(())
2343 - }
2344 -
2345 fn drag_window(&mut self, window: Window) -> WmResult<()> { 2504 fn drag_window(&mut self, window: Window) -> WmResult<()> {
2346 let is_fullscreen = self 2505 let is_fullscreen = self
2347 .clients 2506 .clients
@@ -2823,12 +2982,12 @@ impl WindowManager {
2823 } 2982 }
2824 Event::UnmapNotify(event) => { 2983 Event::UnmapNotify(event) => {
2825 if self.windows.contains(&event.window) && self.is_window_visible(event.window) { 2984 if self.windows.contains(&event.window) && self.is_window_visible(event.window) {
2826 - self.remove_window(event.window)?; 2985 + self.remove_window(event.window, false)?;
2827 } 2986 }
2828 } 2987 }
2829 Event::DestroyNotify(event) => { 2988 Event::DestroyNotify(event) => {
2830 if self.windows.contains(&event.window) { 2989 if self.windows.contains(&event.window) {
2831 - self.remove_window(event.window)?; 2990 + self.remove_window(event.window, true)?;
2832 } 2991 }
2833 } 2992 }
2834 Event::PropertyNotify(event) => { 2993 Event::PropertyNotify(event) => {
@@ -2877,13 +3036,22 @@ impl WindowManager {
2877 } 3036 }
2878 } 3037 }
2879 Event::EnterNotify(event) => { 3038 Event::EnterNotify(event) => {
2880 - if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL { 3039 + if event.mode != x11rb::protocol::xproto::NotifyMode::NORMAL
3040 + || event.detail == x11rb::protocol::xproto::NotifyDetail::INFERIOR
3041 + {
2881 return Ok(Control::Continue); 3042 return Ok(Control::Continue);
2882 } 3043 }
2883 if self.windows.contains(&event.event) { 3044 if self.windows.contains(&event.event) {
2884 if let Some(client) = self.clients.get(&event.event) 3045 if let Some(client) = self.clients.get(&event.event)
2885 && client.monitor_index != self.selected_monitor 3046 && client.monitor_index != self.selected_monitor
2886 { 3047 {
3048 + if let Some(old_selected) = self
3049 + .monitors
3050 + .get(self.selected_monitor)
3051 + .and_then(|monitor| monitor.selected_client)
3052 + {
3053 + self.unfocus(old_selected, false)?;
3054 + }
2887 self.selected_monitor = client.monitor_index; 3055 self.selected_monitor = client.monitor_index;
2888 self.update_bar()?; 3056 self.update_bar()?;
2889 } 3057 }
@@ -2900,14 +3068,18 @@ impl WindowManager {
2900 self.get_monitor_at_point(event.root_x as i32, event.root_y as i32) 3068 self.get_monitor_at_point(event.root_x as i32, event.root_y as i32)
2901 && monitor_index != self.selected_monitor 3069 && monitor_index != self.selected_monitor
2902 { 3070 {
3071 + if let Some(old_selected) = self
3072 + .monitors
3073 + .get(self.selected_monitor)
3074 + .and_then(|monitor| monitor.selected_client)
3075 + {
3076 + self.unfocus(old_selected, true)?;
3077 + }
3078 +
2903 self.selected_monitor = monitor_index; 3079 self.selected_monitor = monitor_index;
3080 + self.focus(None)?;
2904 self.update_bar()?; 3081 self.update_bar()?;
2905 - 3082 + self.update_tab_bars()?;
2906 - let visible = self.visible_windows_on_monitor(monitor_index);
2907 - if let Some(&win) = visible.first() {
2908 - self.focus(Some(win))?;
2909 - self.update_tab_bars()?;
2910 - }
2911 } 3083 }
2912 } 3084 }
2913 Event::KeyPress(event) => { 3085 Event::KeyPress(event) => {
@@ -3274,15 +3446,25 @@ impl WindowManager {
3274 } 3446 }
3275 } 3447 }
3276 Event::FocusIn(event) => { 3448 Event::FocusIn(event) => {
3277 - let selected_window = self 3449 + if !self.windows.contains(&event.event) {
3278 - .monitors 3450 + return Ok(Control::Continue);
3279 - .get(self.selected_monitor) 3451 + }
3280 - .and_then(|m| m.selected_client);
3281 3452
3282 - if let Some(sel_win) = selected_window 3453 + let event_window_visible = self.is_visible(event.event);
3283 - && event.event != sel_win 3454 +
3284 - { 3455 + if !event_window_visible {
3285 - self.set_focus(sel_win)?; 3456 + self.focus(None)?;
3457 + } else {
3458 + let selected_window = self
3459 + .monitors
3460 + .get(self.selected_monitor)
3461 + .and_then(|monitor| monitor.selected_client);
3462 +
3463 + if let Some(focused_window) = selected_window
3464 + && event.event != focused_window
3465 + {
3466 + self.set_focus(focused_window)?;
3467 + }
3286 } 3468 }
3287 } 3469 }
3288 Event::MappingNotify(event) => { 3470 Event::MappingNotify(event) => {
@@ -3297,6 +3479,38 @@ impl WindowManager {
3297 3479
3298 if event.width != old_width || event.height != old_height { 3480 if event.width != old_width || event.height != old_height {
3299 self.screen = self.connection.setup().roots[self.screen_number].clone(); 3481 self.screen = self.connection.setup().roots[self.screen_number].clone();
3482 +
3483 + for monitor_index in 0..self.monitors.len() {
3484 + let monitor = &self.monitors[monitor_index];
3485 + let monitor_x = monitor.screen_x;
3486 + let monitor_y = monitor.screen_y;
3487 + let monitor_width = monitor.screen_width as u32;
3488 + let monitor_height = monitor.screen_height as u32;
3489 +
3490 + let fullscreen_on_monitor: Vec<Window> = self
3491 + .fullscreen_windows
3492 + .iter()
3493 + .filter(|&&window| {
3494 + self.clients
3495 + .get(&window)
3496 + .map(|client| client.monitor_index == monitor_index)
3497 + .unwrap_or(false)
3498 + })
3499 + .copied()
3500 + .collect();
3501 +
3502 + for window in fullscreen_on_monitor {
3503 + self.connection.configure_window(
3504 + window,
3505 + &ConfigureWindowAux::new()
3506 + .x(monitor_x)
3507 + .y(monitor_y)
3508 + .width(monitor_width)
3509 + .height(monitor_height),
3510 + )?;
3511 + }
3512 + }
3513 +
3300 self.apply_layout()?; 3514 self.apply_layout()?;
3301 } 3515 }
3302 } 3516 }
@@ -4109,7 +4323,7 @@ impl WindowManager {
4109 } 4323 }
4110 } 4324 }
4111 4325
4112 - fn remove_window(&mut self, window: Window) -> WmResult<()> { 4326 + fn remove_window(&mut self, window: Window, destroyed: bool) -> WmResult<()> {
4113 let initial_count = self.windows.len(); 4327 let initial_count = self.windows.len();
4114 4328
4115 let focused = self 4329 let focused = self
@@ -4117,6 +4331,17 @@ impl WindowManager {
4117 .get(self.selected_monitor) 4331 .get(self.selected_monitor)
4118 .and_then(|m| m.selected_client); 4332 .and_then(|m| m.selected_client);
4119 4333
4334 + if !destroyed {
4335 + if let Some(client) = self.clients.get(&window) {
4336 + let old_border_width = client.old_border_width;
4337 + self.connection.configure_window(
4338 + window,
4339 + &ConfigureWindowAux::new().border_width(old_border_width as u32),
4340 + )?;
4341 + }
4342 + self.set_wm_state(window, 0)?;
4343 + }
4344 +
4120 if self.clients.contains_key(&window) { 4345 if self.clients.contains_key(&window) {
4121 self.detach(window); 4346 self.detach(window);
4122 self.detach_stack(window); 4347 self.detach_stack(window);
@@ -4125,6 +4350,7 @@ impl WindowManager {
4125 4350
4126 self.windows.retain(|&w| w != window); 4351 self.windows.retain(|&w| w != window);
4127 self.floating_windows.remove(&window); 4352 self.floating_windows.remove(&window);
4353 + self.update_client_list()?;
4128 4354
4129 if self.windows.len() < initial_count { 4355 if self.windows.len() < initial_count {
4130 if focused == Some(window) { 4356 if focused == Some(window) {
@@ -4153,7 +4379,6 @@ impl WindowManager {
4153 4379
4154 let mut current = monitor.clients_head; 4380 let mut current = monitor.clients_head;
4155 while let Some(window) = current { 4381 while let Some(window) = current {
4156 - // A window should always have a client attatched to it.
4157 let Some(client) = self.clients.get(&window) else { 4382 let Some(client) = self.clients.get(&window) else {
4158 break; 4383 break;
4159 }; 4384 };