tony / oxwm

Fix EWMH compliance and focus management to match dwm behavior Browse files
tonybanters committed 2026-01-05 parent 45e7ffe commit c8ec311e89096b34f4cda90eb7658216ed958467
Showing 1 changed file with 168 additions and 146 deletions. UnifiedSplit
+168 −146 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,
@@ -57,6 +56,11 @@ impl AtomCache {
57 .reply()? 56 .reply()?
58 .atom; 57 .atom;
59 58
59 + let net_supporting_wm_check = connection
60 + .intern_atom(false, b"_NET_SUPPORTING_WM_CHECK")?
61 + .reply()?
62 + .atom;
63 +
60 let net_current_desktop = connection 64 let net_current_desktop = connection
61 .intern_atom(false, b"_NET_CURRENT_DESKTOP")? 65 .intern_atom(false, b"_NET_CURRENT_DESKTOP")?
62 .reply()? 66 .reply()?
@@ -122,6 +126,7 @@ impl AtomCache {
122 126
123 Ok(Self { 127 Ok(Self {
124 net_supported, 128 net_supported,
129 + net_supporting_wm_check,
125 net_current_desktop, 130 net_current_desktop,
126 net_client_info, 131 net_client_info,
127 wm_state, 132 wm_state,
@@ -146,6 +151,7 @@ pub struct WindowManager {
146 connection: RustConnection, 151 connection: RustConnection,
147 screen_number: usize, 152 screen_number: usize,
148 root: Window, 153 root: Window,
154 + _wm_check_window: Window,
149 screen: Screen, 155 screen: Screen,
150 windows: Vec<Window>, 156 windows: Vec<Window>,
151 clients: HashMap<Window, Client>, 157 clients: HashMap<Window, Client>,
@@ -288,6 +294,7 @@ impl WindowManager {
288 294
289 let supported_atoms: Vec<Atom> = vec![ 295 let supported_atoms: Vec<Atom> = vec![
290 atoms.net_supported, 296 atoms.net_supported,
297 + atoms.net_supporting_wm_check,
291 atoms.net_wm_state, 298 atoms.net_wm_state,
292 atoms.net_wm_state_fullscreen, 299 atoms.net_wm_state_fullscreen,
293 atoms.net_wm_window_type, 300 atoms.net_wm_window_type,
@@ -312,6 +319,51 @@ impl WindowManager {
312 &supported_bytes, 319 &supported_bytes,
313 )?; 320 )?;
314 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 +
315 let overlay = ErrorOverlay::new( 367 let overlay = ErrorOverlay::new(
316 &connection, 368 &connection,
317 &screen, 369 &screen,
@@ -329,6 +381,7 @@ impl WindowManager {
329 connection, 381 connection,
330 screen_number, 382 screen_number,
331 root, 383 root,
384 + _wm_check_window: wm_check_window,
332 screen, 385 screen,
333 windows: Vec::new(), 386 windows: Vec::new(),
334 clients: HashMap::new(), 387 clients: HashMap::new(),
@@ -862,9 +915,7 @@ impl WindowManager {
862 self.restack()?; 915 self.restack()?;
863 } 916 }
864 } 917 }
865 - KeyAction::Quit | KeyAction::Restart => { 918 + KeyAction::Quit | KeyAction::Restart => {}
866 - // Handled in handle_event
867 - }
868 KeyAction::ViewTag => { 919 KeyAction::ViewTag => {
869 if let Arg::Int(tag_index) = arg { 920 if let Arg::Int(tag_index) = arg {
870 self.view_tag(*tag_index as usize)?; 921 self.view_tag(*tag_index as usize)?;
@@ -1060,7 +1111,7 @@ impl WindowManager {
1060 return Ok(()); 1111 return Ok(());
1061 } 1112 }
1062 1113
1063 - self.unfocus(window)?; 1114 + self.unfocus(window, false)?;
1064 self.detach(window); 1115 self.detach(window);
1065 self.detach_stack(window); 1116 self.detach_stack(window);
1066 1117
@@ -1683,39 +1734,6 @@ impl WindowManager {
1683 }) 1734 })
1684 } 1735 }
1685 1736
1686 - fn get_window_group(&self, window: Window) -> Option<Window> {
1687 - let hints_reply = self
1688 - .connection
1689 - .get_property(false, window, AtomEnum::WM_HINTS, AtomEnum::WM_HINTS, 0, 9)
1690 - .ok()
1691 - .and_then(|cookie| cookie.reply().ok());
1692 -
1693 - if let Some(hints) = hints_reply
1694 - && hints.value.len() >= 36
1695 - {
1696 - let flags = u32::from_ne_bytes([
1697 - hints.value[0],
1698 - hints.value[1],
1699 - hints.value[2],
1700 - hints.value[3],
1701 - ]);
1702 -
1703 - let window_group_hint_flag = 1 << 6;
1704 - if flags & window_group_hint_flag != 0 {
1705 - let group_leader = u32::from_ne_bytes([
1706 - hints.value[32],
1707 - hints.value[33],
1708 - hints.value[34],
1709 - hints.value[35],
1710 - ]);
1711 - if group_leader != 0 && group_leader != window {
1712 - return Some(group_leader);
1713 - }
1714 - }
1715 - }
1716 - None
1717 - }
1718 -
1719 fn get_window_class_instance(&self, window: Window) -> (String, String) { 1737 fn get_window_class_instance(&self, window: Window) -> (String, String) {
1720 let reply = self 1738 let reply = self
1721 .connection 1739 .connection
@@ -1808,20 +1826,6 @@ impl WindowManager {
1808 1826
1809 let transient_parent = self.get_transient_parent(window); 1827 let transient_parent = self.get_transient_parent(window);
1810 let is_transient = transient_parent.is_some(); 1828 let is_transient = transient_parent.is_some();
1811 - let group_leader = self.get_window_group(window);
1812 - let (_, window_class) = self.get_window_class_instance(window);
1813 -
1814 - let existing_same_class = if !window_class.is_empty() {
1815 - self.windows.iter().find(|&&existing_window| {
1816 - if existing_window == window {
1817 - return false;
1818 - }
1819 - let (_, existing_class) = self.get_window_class_instance(existing_window);
1820 - existing_class == window_class
1821 - }).copied()
1822 - } else {
1823 - None
1824 - };
1825 1829
1826 let (monitor_index, tags) = if let Some(parent) = transient_parent { 1830 let (monitor_index, tags) = if let Some(parent) = transient_parent {
1827 if let Some(parent_client) = self.clients.get(&parent) { 1831 if let Some(parent_client) = self.clients.get(&parent) {
@@ -1834,14 +1838,6 @@ impl WindowManager {
1834 .unwrap_or(tag_mask(0)); 1838 .unwrap_or(tag_mask(0));
1835 (self.selected_monitor, tags) 1839 (self.selected_monitor, tags)
1836 } 1840 }
1837 - } else if let Some(leader) = group_leader
1838 - && let Some(leader_client) = self.clients.get(&leader)
1839 - {
1840 - (leader_client.monitor_index, leader_client.tags)
1841 - } else if let Some(existing) = existing_same_class
1842 - && let Some(existing_client) = self.clients.get(&existing)
1843 - {
1844 - (existing_client.monitor_index, existing_client.tags)
1845 } else { 1841 } else {
1846 let tags = self 1842 let tags = self
1847 .monitors 1843 .monitors
@@ -1994,106 +1990,168 @@ impl WindowManager {
1994 } 1990 }
1995 1991
1996 pub fn set_focus(&mut self, window: Window) -> WmResult<()> { 1992 pub fn set_focus(&mut self, window: Window) -> WmResult<()> {
1997 - 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);
1998 1998
1999 - if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) { 1999 + if !never_focus {
2000 - 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 + )?;
2001 } 2012 }
2002 2013
2003 - self.connection 2014 + let _ = self.send_event(window, self.atoms.wm_take_focus);
2004 - .set_input_focus(InputFocus::POINTER_ROOT, window, x11rb::CURRENT_TIME)?;
2005 self.connection.flush()?; 2015 self.connection.flush()?;
2006 2016
2007 - self.update_focus_visuals(old_focused, window)?; 2017 + Ok(())
2008 - self.previous_focused = Some(window); 2018 + }
2009 2019
2010 - if self.layout.name() == "tabbed" { 2020 + fn grabbuttons(&self, window: Window, focused: bool) -> WmResult<()> {
2011 - 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 + )?;
2012 } 2070 }
2013 2071
2014 Ok(()) 2072 Ok(())
2015 } 2073 }
2016 2074
2017 - fn unfocus(&self, window: Window) -> WmResult<()> { 2075 + fn unfocus(&self, window: Window, reset_input_focus: bool) -> WmResult<()> {
2018 if !self.windows.contains(&window) { 2076 if !self.windows.contains(&window) {
2019 return Ok(()); 2077 return Ok(());
2020 } 2078 }
2021 2079
2080 + self.grabbuttons(window, false)?;
2081 +
2022 self.connection.change_window_attributes( 2082 self.connection.change_window_attributes(
2023 window, 2083 window,
2024 &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused), 2084 &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused),
2025 )?; 2085 )?;
2026 2086
2027 - self.connection.grab_button( 2087 + if reset_input_focus {
2028 - false, 2088 + self.connection.set_input_focus(
2029 - window, 2089 + InputFocus::POINTER_ROOT,
2030 - EventMask::BUTTON_PRESS, 2090 + self.root,
2031 - GrabMode::SYNC, 2091 + x11rb::CURRENT_TIME,
2032 - GrabMode::SYNC, 2092 + )?;
2033 - x11rb::NONE, 2093 + self.connection.delete_property(self.root, self.atoms.net_active_window)?;
2034 - x11rb::NONE, 2094 + }
2035 - ButtonIndex::ANY,
2036 - ModMask::ANY,
2037 - )?;
2038 2095
2039 Ok(()) 2096 Ok(())
2040 } 2097 }
2041 2098
2042 fn focus(&mut self, window: Option<Window>) -> WmResult<()> { 2099 fn focus(&mut self, window: Option<Window>) -> WmResult<()> {
2043 - let monitor = self.monitors.get_mut(self.selected_monitor).unwrap(); 2100 + let old_selected = self
2044 - let old_selected = monitor.selected_client; 2101 + .monitors
2102 + .get(self.selected_monitor)
2103 + .and_then(|m| m.selected_client);
2045 2104
2046 - if let Some(old_win) = old_selected 2105 + let mut focus_client = window;
2047 - && old_selected != window 2106 + if focus_client.is_none()
2107 + || focus_client.is_some_and(|w| !self.is_visible(w))
2048 { 2108 {
2049 - self.unfocus(old_win)?;
2050 - }
2051 -
2052 - let mut win = window;
2053 - if win.is_none() || !self.is_visible(win.unwrap()) {
2054 let mut current = self 2109 let mut current = self
2055 .monitors 2110 .monitors
2056 .get(self.selected_monitor) 2111 .get(self.selected_monitor)
2057 .and_then(|m| m.stack_head); 2112 .and_then(|m| m.stack_head);
2058 2113
2114 + focus_client = None;
2059 while let Some(w) = current { 2115 while let Some(w) = current {
2060 if self.is_visible(w) { 2116 if self.is_visible(w) {
2061 - win = Some(w); 2117 + focus_client = Some(w);
2062 break; 2118 break;
2063 } 2119 }
2064 current = self.clients.get(&w).and_then(|c| c.stack_next); 2120 current = self.clients.get(&w).and_then(|c| c.stack_next);
2065 } 2121 }
2066 } 2122 }
2067 2123
2068 - if let Some(win) = win { 2124 + if old_selected != focus_client {
2069 - if !self.windows.contains(&win) { 2125 + if let Some(old_win) = old_selected {
2070 - return Ok(()); 2126 + self.unfocus(old_win, false)?;
2071 - }
2072 -
2073 - if self.clients.get(&win).is_some_and(|c| c.is_urgent) {
2074 - self.set_urgent(win, false)?;
2075 } 2127 }
2128 + }
2076 2129
2130 + if let Some(win) = focus_client {
2077 let monitor_idx = self 2131 let monitor_idx = self
2078 .clients 2132 .clients
2079 .get(&win) 2133 .get(&win)
2080 .map(|c| c.monitor_index) 2134 .map(|c| c.monitor_index)
2081 .unwrap_or(self.selected_monitor); 2135 .unwrap_or(self.selected_monitor);
2136 +
2082 if monitor_idx != self.selected_monitor { 2137 if monitor_idx != self.selected_monitor {
2083 self.selected_monitor = monitor_idx; 2138 self.selected_monitor = monitor_idx;
2084 } 2139 }
2085 2140
2141 + if self.clients.get(&win).is_some_and(|c| c.is_urgent) {
2142 + self.set_urgent(win, false)?;
2143 + }
2144 +
2086 self.detach_stack(win); 2145 self.detach_stack(win);
2087 self.attach_stack(win, monitor_idx); 2146 self.attach_stack(win, monitor_idx);
2088 2147
2148 + self.grabbuttons(win, true)?;
2149 +
2089 self.connection.change_window_attributes( 2150 self.connection.change_window_attributes(
2090 win, 2151 win,
2091 &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused), 2152 &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused),
2092 )?; 2153 )?;
2093 2154
2094 - self.connection
2095 - .ungrab_button(ButtonIndex::ANY, win, ModMask::ANY)?;
2096 -
2097 let never_focus = self 2155 let never_focus = self
2098 .clients 2156 .clients
2099 .get(&win) 2157 .get(&win)
@@ -2409,7 +2467,7 @@ impl WindowManager {
2409 .and_then(|m| m.selected_client); 2467 .and_then(|m| m.selected_client);
2410 2468
2411 if let Some(win) = old_selected { 2469 if let Some(win) = old_selected {
2412 - self.unfocus(win)?; 2470 + self.unfocus(win, true)?;
2413 } 2471 }
2414 2472
2415 self.selected_monitor = target_monitor; 2473 self.selected_monitor = target_monitor;
@@ -2443,39 +2501,6 @@ impl WindowManager {
2443 Ok(()) 2501 Ok(())
2444 } 2502 }
2445 2503
2446 - fn update_focus_visuals(
2447 - &self,
2448 - old_focused: Option<Window>,
2449 - new_focused: Window,
2450 - ) -> WmResult<()> {
2451 - if let Some(old_win) = old_focused
2452 - && old_win != new_focused
2453 - {
2454 - self.connection.configure_window(
2455 - old_win,
2456 - &ConfigureWindowAux::new().border_width(self.config.border_width),
2457 - )?;
2458 -
2459 - self.connection.change_window_attributes(
2460 - old_win,
2461 - &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused),
2462 - )?;
2463 - }
2464 -
2465 - self.connection.configure_window(
2466 - new_focused,
2467 - &ConfigureWindowAux::new().border_width(self.config.border_width),
2468 - )?;
2469 -
2470 - self.connection.change_window_attributes(
2471 - new_focused,
2472 - &ChangeWindowAttributesAux::new().border_pixel(self.config.border_focused),
2473 - )?;
2474 -
2475 - self.connection.flush()?;
2476 - Ok(())
2477 - }
2478 -
2479 fn drag_window(&mut self, window: Window) -> WmResult<()> { 2504 fn drag_window(&mut self, window: Window) -> WmResult<()> {
2480 let is_fullscreen = self 2505 let is_fullscreen = self
2481 .clients 2506 .clients
@@ -3011,7 +3036,9 @@ impl WindowManager {
3011 } 3036 }
3012 } 3037 }
3013 Event::EnterNotify(event) => { 3038 Event::EnterNotify(event) => {
3014 - 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 + {
3015 return Ok(Control::Continue); 3042 return Ok(Control::Continue);
3016 } 3043 }
3017 if self.windows.contains(&event.event) { 3044 if self.windows.contains(&event.event) {
@@ -3023,7 +3050,7 @@ impl WindowManager {
3023 .get(self.selected_monitor) 3050 .get(self.selected_monitor)
3024 .and_then(|monitor| monitor.selected_client) 3051 .and_then(|monitor| monitor.selected_client)
3025 { 3052 {
3026 - self.unfocus(old_selected)?; 3053 + self.unfocus(old_selected, false)?;
3027 } 3054 }
3028 self.selected_monitor = client.monitor_index; 3055 self.selected_monitor = client.monitor_index;
3029 self.update_bar()?; 3056 self.update_bar()?;
@@ -3046,17 +3073,13 @@ impl WindowManager {
3046 .get(self.selected_monitor) 3073 .get(self.selected_monitor)
3047 .and_then(|monitor| monitor.selected_client) 3074 .and_then(|monitor| monitor.selected_client)
3048 { 3075 {
3049 - self.unfocus(old_selected)?; 3076 + self.unfocus(old_selected, true)?;
3050 } 3077 }
3051 3078
3052 self.selected_monitor = monitor_index; 3079 self.selected_monitor = monitor_index;
3080 + self.focus(None)?;
3053 self.update_bar()?; 3081 self.update_bar()?;
3054 - 3082 + self.update_tab_bars()?;
3055 - let visible = self.visible_windows_on_monitor(monitor_index);
3056 - if let Some(&win) = visible.first() {
3057 - self.focus(Some(win))?;
3058 - self.update_tab_bars()?;
3059 - }
3060 } 3083 }
3061 } 3084 }
3062 Event::KeyPress(event) => { 3085 Event::KeyPress(event) => {
@@ -4356,7 +4379,6 @@ impl WindowManager {
4356 4379
4357 let mut current = monitor.clients_head; 4380 let mut current = monitor.clients_head;
4358 while let Some(window) = current { 4381 while let Some(window) = current {
4359 - // A window should always have a client attatched to it.
4360 let Some(client) = self.clients.get(&window) else { 4382 let Some(client) = self.clients.get(&window) else {
4361 break; 4383 break;
4362 }; 4384 };