tony / oxwm

path to dwm compliance Browse files
tonybanters committed 2026-01-04 parent 755fc86 commit 45e7ffe531b259f034d2842a2793062366636329
Showing 1 changed file with 266 additions and 63 deletions. UnifiedSplit
+266 −63 src/window_manager.rs Large diff collapsed, click to expand
@@ -46,6 +46,8 @@ struct AtomCache {
46 46 net_wm_name: Atom,
47 47 utf8_string: Atom,
48 48 net_active_window: Atom,
49 + wm_take_focus: Atom,
50 + net_client_list: Atom,
49 51 }
50 52
51 53 impl AtomCache {
@@ -108,6 +110,16 @@ impl AtomCache {
108 110 .reply()?
109 111 .atom;
110 112
113 + let wm_take_focus = connection
114 + .intern_atom(false, b"WM_TAKE_FOCUS")?
115 + .reply()?
116 + .atom;
117 +
118 + let net_client_list = connection
119 + .intern_atom(false, b"_NET_CLIENT_LIST")?
120 + .reply()?
121 + .atom;
122 +
111 123 Ok(Self {
112 124 net_supported,
113 125 net_current_desktop,
@@ -123,6 +135,8 @@ impl AtomCache {
123 135 net_wm_name,
124 136 utf8_string,
125 137 net_active_window,
138 + wm_take_focus,
139 + net_client_list,
126 140 })
127 141 }
128 142 }
@@ -282,6 +296,7 @@ impl WindowManager {
282 296 atoms.net_wm_name,
283 297 atoms.net_current_desktop,
284 298 atoms.net_client_info,
299 + atoms.net_client_list,
285 300 ];
286 301 let supported_bytes: Vec<u8> = supported_atoms
287 302 .iter()
@@ -527,6 +542,26 @@ impl WindowManager {
527 542 Ok(())
528 543 }
529 544
545 + fn update_client_list(&self) -> WmResult<()> {
546 + let window_bytes: Vec<u8> = self
547 + .windows
548 + .iter()
549 + .flat_map(|window| window.to_ne_bytes())
550 + .collect();
551 +
552 + self.connection.change_property(
553 + PropMode::REPLACE,
554 + self.root,
555 + self.atoms.net_client_list,
556 + AtomEnum::WINDOW,
557 + 32,
558 + self.windows.len() as u32,
559 + &window_bytes,
560 + )?;
561 +
562 + Ok(())
563 + }
564 +
530 565 pub fn run(&mut self) -> WmResult<()> {
531 566 println!("oxwm started on display {}", self.screen_number);
532 567
@@ -1544,6 +1579,7 @@ impl WindowManager {
1544 1579 client.old_width = client.width;
1545 1580 client.old_height = client.height;
1546 1581 client.border_width = 0;
1582 + client.is_floating = true;
1547 1583 }
1548 1584
1549 1585 self.fullscreen_windows.insert(window);
@@ -1574,11 +1610,20 @@ impl WindowManager {
1574 1610
1575 1611 self.fullscreen_windows.remove(&window);
1576 1612
1577 - let was_floating = self
1613 + let (was_floating, restored_x, restored_y, restored_width, restored_height, restored_border) = self
1578 1614 .clients
1579 1615 .get(&window)
1580 - .map(|c| c.old_state)
1581 - .unwrap_or(false);
1616 + .map(|client| {
1617 + (
1618 + client.old_state,
1619 + client.old_x_position,
1620 + client.old_y_position,
1621 + client.old_width,
1622 + client.old_height,
1623 + client.old_border_width,
1624 + )
1625 + })
1626 + .unwrap_or((false, 0, 0, 100, 100, 0));
1582 1627
1583 1628 if !was_floating {
1584 1629 self.floating_windows.remove(&window);
@@ -1588,8 +1633,22 @@ impl WindowManager {
1588 1633 client.is_fullscreen = false;
1589 1634 client.is_floating = client.old_state;
1590 1635 client.border_width = client.old_border_width;
1636 + client.x_position = client.old_x_position;
1637 + client.y_position = client.old_y_position;
1638 + client.width = client.old_width;
1639 + client.height = client.old_height;
1591 1640 }
1592 1641
1642 + self.connection.configure_window(
1643 + window,
1644 + &ConfigureWindowAux::new()
1645 + .x(restored_x as i32)
1646 + .y(restored_y as i32)
1647 + .width(restored_width as u32)
1648 + .height(restored_height as u32)
1649 + .border_width(restored_border as u32),
1650 + )?;
1651 +
1593 1652 self.apply_layout()?;
1594 1653 }
1595 1654
@@ -1624,6 +1683,39 @@ impl WindowManager {
1624 1683 })
1625 1684 }
1626 1685
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 +
1627 1719 fn get_window_class_instance(&self, window: Window) -> (String, String) {
1628 1720 let reply = self
1629 1721 .connection
@@ -1688,25 +1780,21 @@ impl WindowManager {
1688 1780 client.monitor_index = monitor_index;
1689 1781 }
1690 1782
1691 - let monitor_tagset = || {
1692 - self.monitors
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);
1783 + if let Some(tags) = rule_tags {
1784 + client.tags = tags;
1699 1785
1700 - client.tags = tags;
1701 -
1702 - // Focus window rule tag if config option is set
1703 - if rule_focus && let Some(tag_mask) = rule_tags {
1704 - let tag_index = unmask_tag(tag_mask);
1705 - let monitor_tagset = monitor_tagset();
1706 - let is_tag_focused = monitor_tagset & tag_mask == tag_mask;
1786 + if rule_focus {
1787 + let tag_index = unmask_tag(tags);
1788 + let monitor_tagset = self
1789 + .monitors
1790 + .get(client.monitor_index)
1791 + .map(|monitor| monitor.get_selected_tag())
1792 + .unwrap_or(tag_mask(0));
1793 + let is_tag_focused = monitor_tagset & tags == tags;
1707 1794
1708 - if !is_tag_focused {
1709 - self.view_tag(tag_index)?;
1795 + if !is_tag_focused {
1796 + self.view_tag(tag_index)?;
1797 + }
1710 1798 }
1711 1799 }
1712 1800 }
@@ -1720,6 +1808,20 @@ impl WindowManager {
1720 1808
1721 1809 let transient_parent = self.get_transient_parent(window);
1722 1810 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 + };
1723 1825
1724 1826 let (monitor_index, tags) = if let Some(parent) = transient_parent {
1725 1827 if let Some(parent_client) = self.clients.get(&parent) {
@@ -1728,15 +1830,23 @@ impl WindowManager {
1728 1830 let tags = self
1729 1831 .monitors
1730 1832 .get(self.selected_monitor)
1731 - .map(|m| m.tagset[m.selected_tags_index])
1833 + .map(|monitor| monitor.tagset[monitor.selected_tags_index])
1732 1834 .unwrap_or(tag_mask(0));
1733 1835 (self.selected_monitor, tags)
1734 1836 }
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)
1735 1845 } else {
1736 1846 let tags = self
1737 1847 .monitors
1738 1848 .get(self.selected_monitor)
1739 - .map(|m| m.tagset[m.selected_tags_index])
1849 + .map(|monitor| monitor.tagset[monitor.selected_tags_index])
1740 1850 .unwrap_or(tag_mask(0));
1741 1851 (self.selected_monitor, tags)
1742 1852 };
@@ -1866,26 +1976,14 @@ impl WindowManager {
1866 1976 )?;
1867 1977
1868 1978 self.set_wm_state(window, 1)?;
1979 + self.update_client_list()?;
1869 1980
1870 1981 let final_tags = self.clients.get(&window).map(|c| c.tags).unwrap_or(tags);
1871 1982 let _ = self.save_client_tag(window, final_tags);
1872 1983
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 1984 self.apply_layout()?;
1887 1985 self.connection.map_window(window)?;
1888 - self.focus(Some(window))?;
1986 + self.focus(None)?;
1889 1987 self.update_bar()?;
1890 1988
1891 1989 if self.layout.name() == "tabbed" {
@@ -1996,8 +2094,28 @@ impl WindowManager {
1996 2094 self.connection
1997 2095 .ungrab_button(ButtonIndex::ANY, win, ModMask::ANY)?;
1998 2096
1999 - self.connection
2000 - .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
2097 + let never_focus = self
2098 + .clients
2099 + .get(&win)
2100 + .map(|client| client.never_focus)
2101 + .unwrap_or(false);
2102 +
2103 + if !never_focus {
2104 + self.connection
2105 + .set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?;
2106 +
2107 + self.connection.change_property(
2108 + PropMode::REPLACE,
2109 + self.root,
2110 + self.atoms.net_active_window,
2111 + AtomEnum::WINDOW,
2112 + 32,
2113 + 1,
2114 + &win.to_ne_bytes(),
2115 + )?;
2116 + }
2117 +
2118 + let _ = self.send_event(win, self.atoms.wm_take_focus);
2001 2119
2002 2120 if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
2003 2121 monitor.selected_client = Some(win);
@@ -2011,6 +2129,9 @@ impl WindowManager {
2011 2129 x11rb::CURRENT_TIME,
2012 2130 )?;
2013 2131
2132 + self.connection
2133 + .delete_property(self.root, self.atoms.net_active_window)?;
2134 +
2014 2135 if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
2015 2136 monitor.selected_client = None;
2016 2137 }
@@ -2075,25 +2196,35 @@ impl WindowManager {
2075 2196
2076 2197 fn focusstack(&mut self, direction: i32) -> WmResult<()> {
2077 2198 let monitor = match self.monitors.get(self.selected_monitor) {
2078 - Some(m) => m,
2199 + Some(monitor) => monitor,
2079 2200 None => return Ok(()),
2080 2201 };
2081 2202
2082 - let selected = match monitor.selected_client {
2083 - Some(win) => win,
2203 + let selected_window = match monitor.selected_client {
2204 + Some(window) => window,
2084 2205 None => return Ok(()),
2085 2206 };
2086 2207
2208 + let selected_is_fullscreen = self
2209 + .clients
2210 + .get(&selected_window)
2211 + .map(|client| client.is_fullscreen)
2212 + .unwrap_or(false);
2213 +
2214 + if selected_is_fullscreen {
2215 + return Ok(());
2216 + }
2217 +
2087 2218 let selected_tags = monitor.tagset[monitor.selected_tags_index];
2088 2219
2089 2220 let mut stack_windows: Vec<Window> = Vec::new();
2090 - let mut current = monitor.clients_head;
2091 - while let Some(win) = current {
2092 - if let Some(client) = self.clients.get(&win) {
2221 + let mut current_window = monitor.clients_head;
2222 + while let Some(window) = current_window {
2223 + if let Some(client) = self.clients.get(&window) {
2093 2224 if client.tags & selected_tags != 0 && !client.is_floating {
2094 - stack_windows.push(win);
2225 + stack_windows.push(window);
2095 2226 }
2096 - current = client.next;
2227 + current_window = client.next;
2097 2228 } else {
2098 2229 break;
2099 2230 }
@@ -2103,17 +2234,19 @@ impl WindowManager {
2103 2234 return Ok(());
2104 2235 }
2105 2236
2106 - let current_idx = stack_windows.iter().position(|&w| w == selected);
2237 + let current_index = stack_windows
2238 + .iter()
2239 + .position(|&window| window == selected_window);
2107 2240
2108 - let next_window = if let Some(idx) = current_idx {
2241 + let next_window = if let Some(index) = current_index {
2109 2242 if direction > 0 {
2110 - if idx + 1 < stack_windows.len() {
2111 - stack_windows[idx + 1]
2243 + if index + 1 < stack_windows.len() {
2244 + stack_windows[index + 1]
2112 2245 } else {
2113 2246 stack_windows[0]
2114 2247 }
2115 - } else if idx > 0 {
2116 - stack_windows[idx - 1]
2248 + } else if index > 0 {
2249 + stack_windows[index - 1]
2117 2250 } else {
2118 2251 stack_windows[stack_windows.len() - 1]
2119 2252 }
@@ -2122,6 +2255,7 @@ impl WindowManager {
2122 2255 };
2123 2256
2124 2257 self.focus(Some(next_window))?;
2258 + self.restack()?;
2125 2259 self.update_tab_bars()?;
2126 2260
2127 2261 Ok(())
@@ -2823,12 +2957,12 @@ impl WindowManager {
2823 2957 }
2824 2958 Event::UnmapNotify(event) => {
2825 2959 if self.windows.contains(&event.window) && self.is_window_visible(event.window) {
2826 - self.remove_window(event.window)?;
2960 + self.remove_window(event.window, false)?;
2827 2961 }
2828 2962 }
2829 2963 Event::DestroyNotify(event) => {
2830 2964 if self.windows.contains(&event.window) {
2831 - self.remove_window(event.window)?;
2965 + self.remove_window(event.window, true)?;
2832 2966 }
2833 2967 }
2834 2968 Event::PropertyNotify(event) => {
@@ -2884,6 +3018,13 @@ impl WindowManager {
2884 3018 if let Some(client) = self.clients.get(&event.event)
2885 3019 && client.monitor_index != self.selected_monitor
2886 3020 {
3021 + if let Some(old_selected) = self
3022 + .monitors
3023 + .get(self.selected_monitor)
3024 + .and_then(|monitor| monitor.selected_client)
3025 + {
3026 + self.unfocus(old_selected)?;
3027 + }
2887 3028 self.selected_monitor = client.monitor_index;
2888 3029 self.update_bar()?;
2889 3030 }
@@ -2900,6 +3041,14 @@ impl WindowManager {
2900 3041 self.get_monitor_at_point(event.root_x as i32, event.root_y as i32)
2901 3042 && monitor_index != self.selected_monitor
2902 3043 {
3044 + if let Some(old_selected) = self
3045 + .monitors
3046 + .get(self.selected_monitor)
3047 + .and_then(|monitor| monitor.selected_client)
3048 + {
3049 + self.unfocus(old_selected)?;
3050 + }
3051 +
2903 3052 self.selected_monitor = monitor_index;
2904 3053 self.update_bar()?;
2905 3054
@@ -3274,15 +3423,25 @@ impl WindowManager {
3274 3423 }
3275 3424 }
3276 3425 Event::FocusIn(event) => {
3277 - let selected_window = self
3278 - .monitors
3279 - .get(self.selected_monitor)
3280 - .and_then(|m| m.selected_client);
3426 + if !self.windows.contains(&event.event) {
3427 + return Ok(Control::Continue);
3428 + }
3281 3429
3282 - if let Some(sel_win) = selected_window
3283 - && event.event != sel_win
3284 - {
3285 - self.set_focus(sel_win)?;
3430 + let event_window_visible = self.is_visible(event.event);
3431 +
3432 + if !event_window_visible {
3433 + self.focus(None)?;
3434 + } else {
3435 + let selected_window = self
3436 + .monitors
3437 + .get(self.selected_monitor)
3438 + .and_then(|monitor| monitor.selected_client);
3439 +
3440 + if let Some(focused_window) = selected_window
3441 + && event.event != focused_window
3442 + {
3443 + self.set_focus(focused_window)?;
3444 + }
3286 3445 }
3287 3446 }
3288 3447 Event::MappingNotify(event) => {
@@ -3297,6 +3456,38 @@ impl WindowManager {
3297 3456
3298 3457 if event.width != old_width || event.height != old_height {
3299 3458 self.screen = self.connection.setup().roots[self.screen_number].clone();
3459 +
3460 + for monitor_index in 0..self.monitors.len() {
3461 + let monitor = &self.monitors[monitor_index];
3462 + let monitor_x = monitor.screen_x;
3463 + let monitor_y = monitor.screen_y;
3464 + let monitor_width = monitor.screen_width as u32;
3465 + let monitor_height = monitor.screen_height as u32;
3466 +
3467 + let fullscreen_on_monitor: Vec<Window> = self
3468 + .fullscreen_windows
3469 + .iter()
3470 + .filter(|&&window| {
3471 + self.clients
3472 + .get(&window)
3473 + .map(|client| client.monitor_index == monitor_index)
3474 + .unwrap_or(false)
3475 + })
3476 + .copied()
3477 + .collect();
3478 +
3479 + for window in fullscreen_on_monitor {
3480 + self.connection.configure_window(
3481 + window,
3482 + &ConfigureWindowAux::new()
3483 + .x(monitor_x)
3484 + .y(monitor_y)
3485 + .width(monitor_width)
3486 + .height(monitor_height),
3487 + )?;
3488 + }
3489 + }
3490 +
3300 3491 self.apply_layout()?;
3301 3492 }
3302 3493 }
@@ -4109,7 +4300,7 @@ impl WindowManager {
4109 4300 }
4110 4301 }
4111 4302
4112 - fn remove_window(&mut self, window: Window) -> WmResult<()> {
4303 + fn remove_window(&mut self, window: Window, destroyed: bool) -> WmResult<()> {
4113 4304 let initial_count = self.windows.len();
4114 4305
4115 4306 let focused = self
@@ -4117,6 +4308,17 @@ impl WindowManager {
4117 4308 .get(self.selected_monitor)
4118 4309 .and_then(|m| m.selected_client);
4119 4310
4311 + if !destroyed {
4312 + if let Some(client) = self.clients.get(&window) {
4313 + let old_border_width = client.old_border_width;
4314 + self.connection.configure_window(
4315 + window,
4316 + &ConfigureWindowAux::new().border_width(old_border_width as u32),
4317 + )?;
4318 + }
4319 + self.set_wm_state(window, 0)?;
4320 + }
4321 +
4120 4322 if self.clients.contains_key(&window) {
4121 4323 self.detach(window);
4122 4324 self.detach_stack(window);
@@ -4125,6 +4327,7 @@ impl WindowManager {
4125 4327
4126 4328 self.windows.retain(|&w| w != window);
4127 4329 self.floating_windows.remove(&window);
4330 + self.update_client_list()?;
4128 4331
4129 4332 if self.windows.len() < initial_count {
4130 4333 if focused == Some(window) {