tony / oxwm

Merge pull request #49 from tonybanters/dynamic-window-management Browse files
Transient Dialogs, Size Hints, Overlay
Tony, btw. committed 2025-12-11 parents bc6b4cb f392c08 commit 994258d099226c92194dbc5416b32407f3cfedd5
Showing 6 changed files with 381 additions and 553 deletions. UnifiedSplit
+1 −1 Cargo.lock
@@ -291,7 +291,7 @@ checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
291 291
292 292 [[package]]
293 293 name = "oxwm"
294 -version = "0.8.0"
294 +version = "0.8.1"
295 295 dependencies = [
296 296 "chrono",
297 297 "dirs",
+1 −1 Cargo.toml
@@ -1,6 +1,6 @@
1 1 [package]
2 2 name = "oxwm"
3 -version = "0.8.0"
3 +version = "0.8.1"
4 4 edition = "2024"
5 5
6 6 [lib]
+1 −0 src/lib.rs
@@ -6,6 +6,7 @@ pub mod keyboard;
6 6 pub mod layout;
7 7 pub mod monitor;
8 8 pub mod overlay;
9 +pub mod size_hints;
9 10 pub mod tab_bar;
10 11 pub mod window_manager;
11 12
+7 −3 src/overlay/keybind.rs
@@ -61,6 +61,8 @@ impl KeybindOverlay {
61 61 connection: &RustConnection,
62 62 font: &Font,
63 63 keybindings: &[KeyBinding],
64 + monitor_x: i16,
65 + monitor_y: i16,
64 66 screen_width: u16,
65 67 screen_height: u16,
66 68 ) -> Result<(), X11Error> {
@@ -93,8 +95,8 @@ impl KeybindOverlay {
93 95 let height =
94 96 title_height + (self.keybindings.len() as u16 * line_height) + (PADDING as u16 * 2);
95 97
96 - let x = ((screen_width - width) / 2) as i16;
97 - let y = ((screen_height - height) / 2) as i16;
98 + let x = monitor_x + ((screen_width - width) / 2) as i16;
99 + let y = monitor_y + ((screen_height - height) / 2) as i16;
98 100
99 101 self.base.configure(connection, x, y, width, height)?;
100 102
@@ -113,13 +115,15 @@ impl KeybindOverlay {
113 115 connection: &RustConnection,
114 116 font: &Font,
115 117 keybindings: &[KeyBinding],
118 + monitor_x: i16,
119 + monitor_y: i16,
116 120 screen_width: u16,
117 121 screen_height: u16,
118 122 ) -> Result<(), X11Error> {
119 123 if self.base.is_visible {
120 124 self.hide(connection)?;
121 125 } else {
122 - self.show(connection, font, keybindings, screen_width, screen_height)?;
126 + self.show(connection, font, keybindings, monitor_x, monitor_y, screen_width, screen_height)?;
123 127 }
124 128 Ok(())
125 129 }
+25 −0 src/size_hints.rs new file mode 100644
@@ -0,0 +1,25 @@
1 +#![allow(dead_code)]
2 +
3 +pub mod flags {
4 + pub const P_MIN_SIZE: u32 = 1 << 4;
5 + pub const P_MAX_SIZE: u32 = 1 << 5;
6 + pub const P_RESIZE_INC: u32 = 1 << 6;
7 + pub const P_ASPECT: u32 = 1 << 7;
8 + pub const P_BASE_SIZE: u32 = 1 << 8;
9 +}
10 +
11 +pub mod offset {
12 + pub const FLAGS: usize = 0;
13 + pub const MIN_WIDTH: usize = 5;
14 + pub const MIN_HEIGHT: usize = 6;
15 + pub const MAX_WIDTH: usize = 7;
16 + pub const MAX_HEIGHT: usize = 8;
17 + pub const WIDTH_INC: usize = 9;
18 + pub const HEIGHT_INC: usize = 10;
19 + pub const MIN_ASPECT_X: usize = 11;
20 + pub const MIN_ASPECT_Y: usize = 12;
21 + pub const MAX_ASPECT_X: usize = 13;
22 + pub const MAX_ASPECT_Y: usize = 14;
23 + pub const BASE_WIDTH: usize = 15;
24 + pub const BASE_HEIGHT: usize = 16;
25 +}
+346 −548 src/window_manager.rs Large diff collapsed, click to expand
@@ -17,14 +17,6 @@ use x11rb::protocol::Event;
17 17 use x11rb::protocol::xproto::*;
18 18 use x11rb::rust_connection::RustConnection;
19 19
20 -#[derive(Debug, Clone, Copy)]
21 -pub struct CachedGeometry {
22 - pub x_position: i16,
23 - pub y_position: i16,
24 - pub width: u16,
25 - pub height: u16,
26 - pub border_width: u16,
27 -}
28 20
29 21 pub fn tag_mask(tag: usize) -> TagMask {
30 22 1 << tag
@@ -43,9 +35,6 @@ struct AtomCache {
43 35 wm_name: Atom,
44 36 net_wm_name: Atom,
45 37 utf8_string: Atom,
46 - wm_normal_hints: Atom,
47 - wm_hints: Atom,
48 - wm_transient_for: Atom,
49 38 net_active_window: Atom,
50 39 }
51 40
@@ -96,9 +85,6 @@ impl AtomCache {
96 85 let wm_name = AtomEnum::WM_NAME.into();
97 86 let net_wm_name = connection.intern_atom(false, b"_NET_WM_NAME")?.reply()?.atom;
98 87 let utf8_string = connection.intern_atom(false, b"UTF8_STRING")?.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 88 let net_active_window = connection.intern_atom(false, b"_NET_ACTIVE_WINDOW")?.reply()?.atom;
103 89
104 90 Ok(Self {
@@ -114,9 +100,6 @@ impl AtomCache {
114 100 wm_name,
115 101 net_wm_name,
116 102 utf8_string,
117 - wm_normal_hints,
118 - wm_hints,
119 - wm_transient_for,
120 103 net_active_window,
121 104 })
122 105 }
@@ -131,7 +114,6 @@ pub struct WindowManager {
131 114 windows: Vec<Window>,
132 115 clients: HashMap<Window, Client>,
133 116 layout: LayoutBox,
134 - window_geometry_cache: HashMap<Window, CachedGeometry>,
135 117 gaps_enabled: bool,
136 118 floating_windows: HashSet<Window>,
137 119 fullscreen_windows: HashSet<Window>,
@@ -289,7 +271,6 @@ impl WindowManager {
289 271 windows: Vec::new(),
290 272 clients: HashMap::new(),
291 273 layout: Box::new(TilingLayout),
292 - window_geometry_cache: HashMap::new(),
293 274 gaps_enabled,
294 275 floating_windows: HashSet::new(),
295 276 fullscreen_windows: HashSet::new(),
@@ -856,6 +837,8 @@ impl WindowManager {
856 837 &self.connection,
857 838 &self.font,
858 839 &self.config.keybindings,
840 + monitor.screen_x as i16,
841 + monitor.screen_y as i16,
859 842 monitor.screen_width as u16,
860 843 monitor.screen_height as u16,
861 844 )?;
@@ -1040,14 +1023,36 @@ impl WindowManager {
1040 1023 let has_no_layout = self.layout.name() == LayoutType::Normie.as_str();
1041 1024
1042 1025 if (has_no_layout || is_floating) && !is_fullscreen {
1043 - self.connection.configure_window(
1026 + let (x, y, w, h, changed) = self.apply_size_hints(
1044 1027 window,
1045 - &ConfigureWindowAux::new()
1046 - .x(client.x_position as i32)
1047 - .y(client.y_position as i32)
1048 - .width(client.width as u32)
1049 - .height(client.height as u32),
1050 - )?;
1028 + client.x_position as i32,
1029 + client.y_position as i32,
1030 + client.width as i32,
1031 + client.height as i32,
1032 + );
1033 + if changed {
1034 + if let Some(c) = self.clients.get_mut(&window) {
1035 + c.old_x_position = c.x_position;
1036 + c.old_y_position = c.y_position;
1037 + c.old_width = c.width;
1038 + c.old_height = c.height;
1039 + c.x_position = x as i16;
1040 + c.y_position = y as i16;
1041 + c.width = w as u16;
1042 + c.height = h as u16;
1043 + }
1044 + self.connection.configure_window(
1045 + window,
1046 + &ConfigureWindowAux::new()
1047 + .x(x)
1048 + .y(y)
1049 + .width(w as u32)
1050 + .height(h as u32)
1051 + .border_width(self.config.border_width),
1052 + )?;
1053 + self.send_configure_notify(window)?;
1054 + self.connection.flush()?;
1055 + }
1051 1056 }
1052 1057
1053 1058 self.showhide(client.stack_next)?;
@@ -1369,30 +1374,6 @@ impl WindowManager {
1369 1374 Ok(())
1370 1375 }
1371 1376
1372 - fn get_window_state(&self, window: Window) -> WmResult<i32> {
1373 - let reply = self.connection.get_property(
1374 - false,
1375 - window,
1376 - self.atoms.wm_state,
1377 - self.atoms.wm_state,
1378 - 0,
1379 - 2,
1380 - )?.reply();
1381 -
1382 - match reply {
1383 - Ok(prop) if !prop.value.is_empty() && prop.value.len() >= 4 => {
1384 - let state = u32::from_ne_bytes([
1385 - prop.value[0],
1386 - prop.value[1],
1387 - prop.value[2],
1388 - prop.value[3],
1389 - ]);
1390 - Ok(state as i32)
1391 - }
1392 - _ => Ok(-1),
1393 - }
1394 - }
1395 -
1396 1377 fn get_window_atom_property(&self, window: Window, property: Atom) -> WmResult<Option<Atom>> {
1397 1378 let reply = self.connection.get_property(
1398 1379 false,
@@ -1417,25 +1398,6 @@ impl WindowManager {
1417 1398 }
1418 1399 }
1419 1400
1420 - fn get_window_text_property(&self, window: Window, atom: Atom) -> WmResult<Option<String>> {
1421 - let reply = self.connection.get_property(
1422 - false,
1423 - window,
1424 - atom,
1425 - AtomEnum::ANY,
1426 - 0,
1427 - 1024,
1428 - )?.reply();
1429 -
1430 - match reply {
1431 - Ok(prop) if !prop.value.is_empty() => {
1432 - let text = String::from_utf8_lossy(&prop.value).to_string();
1433 - Ok(Some(text.trim_end_matches('\0').to_string()))
1434 - }
1435 - _ => Ok(None),
1436 - }
1437 - }
1438 -
1439 1401 fn fullscreen(&mut self) -> WmResult<()> {
1440 1402 if self.show_bar {
1441 1403 let windows: Vec<Window> = self.windows.iter()
@@ -1511,7 +1473,7 @@ impl WindowManager {
1511 1473 if let Some(&(x, y, width, height, border_width)) = self.floating_geometry_before_fullscreen.get(&window) {
1512 1474 self.connection.configure_window(
1513 1475 window,
1514 - &x11rb::protocol::xproto::ConfigureWindowAux::new()
1476 + &ConfigureWindowAux::new()
1515 1477 .x(x as i32)
1516 1478 .y(y as i32)
1517 1479 .width(width as u32)
@@ -1519,13 +1481,13 @@ impl WindowManager {
1519 1481 .border_width(border_width as u32),
1520 1482 )?;
1521 1483
1522 - self.update_geometry_cache(window, CachedGeometry {
1523 - x_position: x,
1524 - y_position: y,
1525 - width,
1526 - height,
1527 - border_width,
1528 - });
1484 + if let Some(c) = self.clients.get_mut(&window) {
1485 + c.x_position = x;
1486 + c.y_position = y;
1487 + c.width = width;
1488 + c.height = height;
1489 + c.border_width = border_width;
1490 + }
1529 1491
1530 1492 self.floating_geometry_before_fullscreen.remove(&window);
1531 1493 }
@@ -1673,79 +1635,6 @@ impl WindowManager {
1673 1635 })
1674 1636 }
1675 1637
1676 - fn has_user_set_position(&self, window: Window) -> bool {
1677 - const US_POSITION: u32 = 1 << 0;
1678 - const P_POSITION: u32 = 1 << 2;
1679 -
1680 - let size_hints = self.connection
1681 - .get_property(
1682 - false,
1683 - window,
1684 - x11rb::protocol::xproto::AtomEnum::WM_NORMAL_HINTS,
1685 - x11rb::protocol::xproto::AtomEnum::WM_SIZE_HINTS,
1686 - 0,
1687 - 18,
1688 - )
1689 - .ok()
1690 - .and_then(|cookie| cookie.reply().ok());
1691 -
1692 - if let Some(hints) = size_hints {
1693 - if hints.value.len() >= 4 {
1694 - let flags = u32::from_ne_bytes([
1695 - hints.value[0],
1696 - hints.value[1],
1697 - hints.value[2],
1698 - hints.value[3],
1699 - ]);
1700 - return (flags & US_POSITION) != 0 || (flags & P_POSITION) != 0;
1701 - }
1702 - }
1703 - false
1704 - }
1705 -
1706 - fn is_dialog_window(&self, window: Window) -> bool {
1707 - let window_type_property = self.connection
1708 - .get_property(
1709 - false,
1710 - window,
1711 - self.atoms.net_wm_window_type,
1712 - AtomEnum::ATOM,
1713 - 0,
1714 - 32,
1715 - )
1716 - .ok()
1717 - .and_then(|cookie| cookie.reply().ok());
1718 -
1719 - if let Some(reply) = window_type_property {
1720 - let atoms: Vec<Atom> = reply
1721 - .value
1722 - .chunks_exact(4)
1723 - .map(|chunk| u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
1724 - .collect();
1725 -
1726 - let is_dialog = atoms.contains(&self.atoms.net_wm_window_type_dialog);
1727 - is_dialog
1728 - } else {
1729 - false
1730 - }
1731 - }
1732 -
1733 - fn get_window_class(&self, window: Window) -> Option<String> {
1734 - self.connection
1735 - .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)
1736 - .ok()
1737 - .and_then(|cookie| cookie.reply().ok())
1738 - .and_then(|reply| {
1739 - if reply.value.is_empty() {
1740 - None
1741 - } else {
1742 - std::str::from_utf8(&reply.value).ok().map(|s| {
1743 - s.split('\0').nth(1).unwrap_or(s.split('\0').next().unwrap_or("")).to_string()
1744 - })
1745 - }
1746 - })
1747 - }
1748 -
1749 1638 fn get_window_class_instance(&self, window: Window) -> (String, String) {
1750 1639 let reply = self.connection
1751 1640 .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)
@@ -1819,133 +1708,81 @@ impl WindowManager {
1819 1708
1820 1709 fn manage_window(&mut self, window: Window) -> WmResult<()> {
1821 1710 let geometry = self.connection.get_geometry(window)?.reply()?;
1822 - let mut window_x = geometry.x as i32;
1823 - let mut window_y = geometry.y as i32;
1824 - let mut window_width = geometry.width as u32;
1825 - let mut window_height = geometry.height as u32;
1826 1711 let border_width = self.config.border_width;
1827 1712
1828 - self.update_window_title(window)?;
1829 -
1830 1713 let transient_parent = self.get_transient_parent(window);
1831 1714 let is_transient = transient_parent.is_some();
1832 1715
1833 - let parent_geometry = if let Some(parent) = transient_parent {
1834 - self.connection.get_geometry(parent)
1835 - .ok()
1836 - .and_then(|cookie| cookie.reply().ok())
1837 - } else {
1838 - None
1839 - };
1840 -
1841 - let (initial_tags, initial_monitor_index) = if let Some(parent) = transient_parent {
1842 - if let Some(ref parent_geom) = parent_geometry {
1843 - let parent_mon_idx = self.get_monitor_for_rect(
1844 - parent_geom.x as i32,
1845 - parent_geom.y as i32,
1846 - parent_geom.width as i32,
1847 - parent_geom.height as i32
1848 - );
1849 -
1850 - let tags = if let Some(parent_client) = self.clients.get(&parent) {
1851 - parent_client.tags
1852 - } else {
1853 - self.monitors
1854 - .get(parent_mon_idx)
1855 - .map(|m| m.tagset[m.selected_tags_index])
1856 - .unwrap_or(tag_mask(0))
1857 - };
1858 -
1859 - (tags, parent_mon_idx)
1716 + let (monitor_index, tags) = if let Some(parent) = transient_parent {
1717 + if let Some(parent_client) = self.clients.get(&parent) {
1718 + (parent_client.monitor_index, parent_client.tags)
1860 1719 } else {
1861 - let tags = self.monitors
1862 - .get(self.selected_monitor)
1720 + let tags = self.monitors.get(self.selected_monitor)
1863 1721 .map(|m| m.tagset[m.selected_tags_index])
1864 1722 .unwrap_or(tag_mask(0));
1865 - (tags, self.selected_monitor)
1723 + (self.selected_monitor, tags)
1866 1724 }
1867 1725 } else {
1868 - let tags = self.monitors
1869 - .get(self.selected_monitor)
1726 + let tags = self.monitors.get(self.selected_monitor)
1870 1727 .map(|m| m.tagset[m.selected_tags_index])
1871 1728 .unwrap_or(tag_mask(0));
1872 - (tags, self.selected_monitor)
1729 + (self.selected_monitor, tags)
1873 1730 };
1874 1731
1875 - let mut client = Client::new(window, initial_monitor_index, initial_tags);
1876 - client.old_border_width = border_width as u16;
1732 + let mut client = Client::new(window, monitor_index, tags);
1733 + client.x_position = geometry.x;
1734 + client.y_position = geometry.y;
1735 + client.width = geometry.width;
1736 + client.height = geometry.height;
1737 + client.old_x_position = geometry.x;
1738 + client.old_y_position = geometry.y;
1739 + client.old_width = geometry.width;
1740 + client.old_height = geometry.height;
1741 + client.old_border_width = geometry.border_width;
1742 + client.border_width = border_width as u16;
1877 1743
1878 1744 self.clients.insert(window, client);
1745 + self.update_window_title(window)?;
1879 1746
1880 1747 if !is_transient {
1881 1748 self.apply_rules(window)?;
1882 1749 }
1883 1750
1884 - let final_monitor_index = self.clients.get(&window).map(|c| c.monitor_index).unwrap_or(initial_monitor_index);
1885 - let final_monitor = self.monitors.get(final_monitor_index).cloned().unwrap_or_else(|| {
1886 - self.monitors.get(self.selected_monitor).cloned().unwrap_or_else(|| {
1887 - Monitor::new(0, 0, self.screen.width_in_pixels as u32, self.screen.height_in_pixels as u32)
1888 - })
1889 - });
1751 + let client_monitor = self.clients.get(&window).map(|c| c.monitor_index).unwrap_or(monitor_index);
1752 + let monitor = &self.monitors[client_monitor];
1890 1753
1891 - let total_width = window_width as i32 + 2 * border_width as i32;
1892 - let total_height = window_height as i32 + 2 * border_width as i32;
1754 + let mut x = self.clients.get(&window).map(|c| c.x_position as i32).unwrap_or(0);
1755 + let mut y = self.clients.get(&window).map(|c| c.y_position as i32).unwrap_or(0);
1756 + let w = self.clients.get(&window).map(|c| c.width as i32).unwrap_or(1);
1757 + let h = self.clients.get(&window).map(|c| c.height as i32).unwrap_or(1);
1758 + let bw = border_width as i32;
1893 1759
1894 - self.update_window_type(window)?;
1895 -
1896 - let has_explicit_position = self.has_user_set_position(window);
1897 - let is_dialog = self.is_dialog_window(window);
1898 - let position_is_origin = window_x == 0 && window_y == 0;
1899 - let should_center = (is_transient || is_dialog) && (!has_explicit_position || position_is_origin);
1900 -
1901 - if should_center {
1902 - if transient_parent.is_some() {
1903 - if let Some(ref parent_geom) = parent_geometry {
1904 - let parent_center_x = parent_geom.x as i32 + parent_geom.width as i32 / 2;
1905 - let parent_center_y = parent_geom.y as i32 + parent_geom.height as i32 / 2;
1906 - window_x = parent_center_x - window_width as i32 / 2;
1907 - window_y = parent_center_y - window_height as i32 / 2;
1908 - } else {
1909 - window_x = final_monitor.window_area_x + final_monitor.window_area_width / 2 - window_width as i32 / 2;
1910 - window_y = final_monitor.window_area_y + final_monitor.window_area_height / 2 - window_height as i32 / 2;
1911 - }
1912 - } else {
1913 - window_x = final_monitor.window_area_x + final_monitor.window_area_width / 2 - window_width as i32 / 2;
1914 - window_y = final_monitor.window_area_y + final_monitor.window_area_height / 2 - window_height as i32 / 2;
1915 - }
1916 - }
1917 -
1918 - if window_x + total_width > final_monitor.window_area_x + final_monitor.window_area_width {
1919 - window_x = final_monitor.window_area_x + final_monitor.window_area_width - total_width;
1760 + if x + w + 2 * bw > monitor.window_area_x + monitor.window_area_width {
1761 + x = monitor.window_area_x + monitor.window_area_width - w - 2 * bw;
1920 1762 }
1921 - if window_y + total_height > final_monitor.window_area_y + final_monitor.window_area_height {
1922 - window_y = final_monitor.window_area_y + final_monitor.window_area_height - total_height;
1763 + if y + h + 2 * bw > monitor.window_area_y + monitor.window_area_height {
1764 + y = monitor.window_area_y + monitor.window_area_height - h - 2 * bw;
1923 1765 }
1924 - window_x = window_x.max(final_monitor.window_area_x);
1925 - window_y = window_y.max(final_monitor.window_area_y);
1766 + x = x.max(monitor.window_area_x);
1767 + y = y.max(monitor.window_area_y);
1926 1768
1927 - if let Some(client) = self.clients.get_mut(&window) {
1928 - client.x_position = window_x as i16;
1929 - client.y_position = window_y as i16;
1930 - client.width = window_width as u16;
1931 - client.height = window_height as u16;
1932 - client.old_x_position = window_x as i16;
1933 - client.old_y_position = window_y as i16;
1934 - client.old_width = window_width as u16;
1935 - client.old_height = window_height as u16;
1936 - client.border_width = border_width as u16;
1769 + if let Some(c) = self.clients.get_mut(&window) {
1770 + c.x_position = x as i16;
1771 + c.y_position = y as i16;
1937 1772 }
1938 1773
1939 1774 self.connection.configure_window(
1940 1775 window,
1941 - &ConfigureWindowAux::new()
1942 - .x(window_x)
1943 - .y(window_y)
1944 - .border_width(border_width),
1776 + &ConfigureWindowAux::new().border_width(border_width),
1777 + )?;
1778 + self.connection.change_window_attributes(
1779 + window,
1780 + &ChangeWindowAttributesAux::new().border_pixel(self.config.border_unfocused),
1945 1781 )?;
1946 -
1947 1782 self.send_configure_notify(window)?;
1783 + self.update_window_type(window)?;
1948 1784 self.update_size_hints(window)?;
1785 + self.update_window_hints(window)?;
1949 1786
1950 1787 self.connection.change_window_attributes(
1951 1788 window,
@@ -1955,17 +1792,14 @@ impl WindowManager {
1955 1792 )?;
1956 1793
1957 1794 let is_fixed = self.clients.get(&window).map(|c| c.is_fixed).unwrap_or(false);
1958 - let already_floating = self.clients.get(&window).map(|c| c.is_floating).unwrap_or(false);
1959 -
1960 - if !already_floating {
1961 - if let Some(client) = self.clients.get_mut(&window) {
1962 - client.is_floating = is_transient || is_fixed;
1963 - client.old_state = client.is_floating;
1795 + if let Some(c) = self.clients.get_mut(&window) {
1796 + if !c.is_floating {
1797 + c.is_floating = is_transient || is_fixed;
1798 + c.old_state = c.is_floating;
1964 1799 }
1965 1800 }
1966 1801
1967 - let is_floating = self.clients.get(&window).map(|c| c.is_floating).unwrap_or(false);
1968 - if is_floating {
1802 + if self.clients.get(&window).map(|c| c.is_floating).unwrap_or(false) {
1969 1803 self.floating_windows.insert(window);
1970 1804 self.connection.configure_window(
1971 1805 window,
@@ -1973,35 +1807,33 @@ impl WindowManager {
1973 1807 )?;
1974 1808 }
1975 1809
1976 - self.attach_aside(window, final_monitor_index);
1977 - self.attach_stack(window, final_monitor_index);
1810 + self.attach_aside(window, client_monitor);
1811 + self.attach_stack(window, client_monitor);
1978 1812 self.windows.push(window);
1979 1813
1980 - let off_screen_x = window_x + 2 * self.screen.width_in_pixels as i32;
1814 + let off_screen_x = x + 2 * self.screen.width_in_pixels as i32;
1981 1815 self.connection.configure_window(
1982 1816 window,
1983 1817 &ConfigureWindowAux::new()
1984 1818 .x(off_screen_x)
1985 - .y(window_y)
1986 - .width(window_width)
1987 - .height(window_height),
1819 + .y(y)
1820 + .width(w as u32)
1821 + .height(h as u32),
1988 1822 )?;
1989 1823
1990 1824 self.set_wm_state(window, 1)?;
1991 1825
1992 - let final_tags = self.clients.get(&window).map(|c| c.tags).unwrap_or(initial_tags);
1993 - if let Err(error) = self.save_client_tag(window, final_tags) {
1994 - eprintln!("Failed to save client tag for new window: {:?}", error);
1995 - }
1826 + let final_tags = self.clients.get(&window).map(|c| c.tags).unwrap_or(tags);
1827 + let _ = self.save_client_tag(window, final_tags);
1996 1828
1997 - if final_monitor_index == self.selected_monitor {
1998 - if let Some(old_selected) = self.monitors.get(self.selected_monitor).and_then(|m| m.selected_client) {
1999 - self.unfocus(old_selected)?;
1829 + if client_monitor == self.selected_monitor {
1830 + if let Some(old_sel) = self.monitors.get(self.selected_monitor).and_then(|m| m.selected_client) {
1831 + self.unfocus(old_sel)?;
2000 1832 }
2001 1833 }
2002 1834
2003 - if let Some(monitor) = self.monitors.get_mut(final_monitor_index) {
2004 - monitor.selected_client = Some(window);
1835 + if let Some(m) = self.monitors.get_mut(client_monitor) {
1836 + m.selected_client = Some(window);
2005 1837 }
2006 1838
2007 1839 self.apply_layout()?;
@@ -2591,12 +2423,10 @@ impl WindowManager {
2591 2423 return Ok(());
2592 2424 };
2593 2425
2594 - let monitor = self.monitors.get(monitor_idx).cloned();
2595 - let Some(monitor) = monitor else {
2426 + if self.monitors.get(monitor_idx).is_none() {
2596 2427 return Ok(());
2597 - };
2428 + }
2598 2429
2599 - let snap = 32;
2600 2430 let is_normie = self.layout.name() == "normie";
2601 2431
2602 2432 if !was_floating && !is_normie {
@@ -2647,9 +2477,11 @@ impl WindowManager {
2647 2477 .unwrap_or(false);
2648 2478
2649 2479 if should_resize {
2650 - if let Some(client) = self.clients.get(&window) {
2651 - let (hint_width, hint_height) = self.apply_size_hints(
2652 - client,
2480 + if let Some(client) = self.clients.get(&window).cloned() {
2481 + let (_, _, hint_width, hint_height, _) = self.apply_size_hints(
2482 + window,
2483 + client.x_position as i32,
2484 + client.y_position as i32,
2653 2485 new_width as i32,
2654 2486 new_height as i32,
2655 2487 );
@@ -2742,7 +2574,9 @@ impl WindowManager {
2742 2574 use crate::keyboard::keysyms;
2743 2575 if let Some(mapping) = &self.keyboard_mapping {
2744 2576 let keysym = mapping.keycode_to_keysym(e.detail);
2745 - if keysym == keysyms::XK_ESCAPE || keysym == keysyms::XK_Q {
2577 + let is_escape = keysym == keysyms::XK_ESCAPE;
2578 + let is_q = keysym == keysyms::XK_Q || keysym == 0x0051;
2579 + if is_escape || is_q {
2746 2580 if let Err(error) = self.keybind_overlay.hide(&self.connection) {
2747 2581 eprintln!("Failed to hide keybind overlay: {:?}", error);
2748 2582 }
@@ -2752,6 +2586,7 @@ impl WindowManager {
2752 2586 return Ok(None);
2753 2587 }
2754 2588 Event::ButtonPress(ref e) if e.event == self.keybind_overlay.window() => {
2589 + self.connection.allow_events(Allow::REPLAY_POINTER, e.time)?;
2755 2590 return Ok(None);
2756 2591 }
2757 2592 Event::Expose(ref expose_event) if expose_event.window == self.keybind_overlay.window() => {
@@ -2787,7 +2622,7 @@ impl WindowManager {
2787 2622 }
2788 2623 }
2789 2624 Event::PropertyNotify(event) => {
2790 - if event.state == x11rb::protocol::xproto::Property::DELETE {
2625 + if event.state == Property::DELETE {
2791 2626 return Ok(None);
2792 2627 }
2793 2628
@@ -2796,16 +2631,12 @@ impl WindowManager {
2796 2631 }
2797 2632
2798 2633 if event.atom == AtomEnum::WM_TRANSIENT_FOR.into() {
2799 - let is_floating = self.clients
2800 - .get(&event.window)
2801 - .map(|c| c.is_floating)
2802 - .unwrap_or(false);
2803 -
2634 + let is_floating = self.clients.get(&event.window).map(|c| c.is_floating).unwrap_or(false);
2804 2635 if !is_floating {
2805 - if let Some(transient_parent) = self.get_transient_parent(event.window) {
2806 - if self.clients.contains_key(&transient_parent) {
2807 - if let Some(client) = self.clients.get_mut(&event.window) {
2808 - client.is_floating = true;
2636 + if let Some(parent) = self.get_transient_parent(event.window) {
2637 + if self.clients.contains_key(&parent) {
2638 + if let Some(c) = self.clients.get_mut(&event.window) {
2639 + c.is_floating = true;
2809 2640 }
2810 2641 self.floating_windows.insert(event.window);
2811 2642 self.apply_layout()?;
@@ -2813,8 +2644,8 @@ impl WindowManager {
2813 2644 }
2814 2645 }
2815 2646 } else if event.atom == AtomEnum::WM_NORMAL_HINTS.into() {
2816 - if let Some(client) = self.clients.get_mut(&event.window) {
2817 - client.hints_valid = false;
2647 + if let Some(c) = self.clients.get_mut(&event.window) {
2648 + c.hints_valid = false;
2818 2649 }
2819 2650 } else if event.atom == AtomEnum::WM_HINTS.into() {
2820 2651 self.update_window_hints(event.window)?;
@@ -2830,77 +2661,6 @@ impl WindowManager {
2830 2661
2831 2662 if event.atom == self.atoms.net_wm_window_type {
2832 2663 self.update_window_type(event.window)?;
2833 -
2834 - if self.is_dialog_window(event.window) {
2835 -
2836 - let client_info = self.clients.get(&event.window).map(|c| {
2837 - (c.width, c.height, c.border_width, c.monitor_index, c.is_floating)
2838 - });
2839 -
2840 - if let Some((width, height, border_width, current_monitor_index, is_floating)) = client_info {
2841 - if !is_floating {
2842 - if let Some(client) = self.clients.get_mut(&event.window) {
2843 - client.is_floating = true;
2844 - }
2845 - self.floating_windows.insert(event.window);
2846 - }
2847 -
2848 - let transient_parent = self.get_transient_parent(event.window);
2849 - let target_monitor_index = if let Some(parent) = transient_parent {
2850 - if let Ok(parent_geom_reply) = self.connection.get_geometry(parent) {
2851 - if let Ok(parent_geom) = parent_geom_reply.reply() {
2852 - self.get_monitor_for_rect(
2853 - parent_geom.x as i32,
2854 - parent_geom.y as i32,
2855 - parent_geom.width as i32,
2856 - parent_geom.height as i32,
2857 - )
2858 - } else {
2859 - current_monitor_index
2860 - }
2861 - } else {
2862 - current_monitor_index
2863 - }
2864 - } else {
2865 - current_monitor_index
2866 - };
2867 -
2868 - let monitor = &self.monitors[target_monitor_index];
2869 - let window_width = width as i32;
2870 - let window_height = height as i32;
2871 -
2872 - let mut window_x = monitor.window_area_x + monitor.window_area_width / 2 - window_width / 2;
2873 - let mut window_y = monitor.window_area_y + monitor.window_area_height / 2 - window_height / 2;
2874 -
2875 - let total_width = window_width + 2 * border_width as i32;
2876 - let total_height = window_height + 2 * border_width as i32;
2877 -
2878 - if window_x + total_width > monitor.window_area_x + monitor.window_area_width {
2879 - window_x = monitor.window_area_x + monitor.window_area_width - total_width;
2880 - }
2881 - if window_y + total_height > monitor.window_area_y + monitor.window_area_height {
2882 - window_y = monitor.window_area_y + monitor.window_area_height - total_height;
2883 - }
2884 - window_x = window_x.max(monitor.window_area_x);
2885 - window_y = window_y.max(monitor.window_area_y);
2886 -
2887 - if let Some(client) = self.clients.get_mut(&event.window) {
2888 - client.x_position = window_x as i16;
2889 - client.y_position = window_y as i16;
2890 - client.monitor_index = target_monitor_index;
2891 - }
2892 -
2893 - self.connection.configure_window(
2894 - event.window,
2895 - &ConfigureWindowAux::new()
2896 - .x(window_x)
2897 - .y(window_y)
2898 - )?.check()?;
2899 -
2900 - self.connection.flush()?;
2901 - self.apply_layout()?;
2902 - }
2903 - }
2904 2664 }
2905 2665 }
2906 2666 Event::EnterNotify(event) => {
@@ -3013,6 +2773,12 @@ impl WindowManager {
3013 2773 }
3014 2774 }
3015 2775 Event::ButtonPress(event) => {
2776 + if self.keybind_overlay.is_visible() && event.event != self.keybind_overlay.window() {
2777 + if let Err(error) = self.keybind_overlay.hide(&self.connection) {
2778 + eprintln!("Failed to hide keybind overlay: {:?}", error);
2779 + }
2780 + }
2781 +
3016 2782 let is_bar_click = self
3017 2783 .bars
3018 2784 .iter()
@@ -3074,9 +2840,15 @@ impl WindowManager {
3074 2840 let modkey_held = state_clean & u16::from(self.config.modkey) != 0;
3075 2841
3076 2842 if modkey_held && event.detail == ButtonIndex::M1.into() {
3077 - self.drag_window(event.child)?;
2843 + if self.clients.contains_key(&event.child) {
2844 + self.drag_window(event.child)?;
2845 + }
2846 + self.connection.allow_events(Allow::REPLAY_POINTER, event.time)?;
3078 2847 } else if modkey_held && event.detail == ButtonIndex::M3.into() {
3079 - self.resize_window_with_mouse(event.child)?;
2848 + if self.clients.contains_key(&event.child) {
2849 + self.resize_window_with_mouse(event.child)?;
2850 + }
2851 + self.connection.allow_events(Allow::REPLAY_POINTER, event.time)?;
3080 2852 } else {
3081 2853 self.connection.allow_events(Allow::REPLAY_POINTER, event.time)?;
3082 2854 }
@@ -3089,8 +2861,10 @@ impl WindowManager {
3089 2861
3090 2862 if modkey_held && event.detail == ButtonIndex::M1.into() {
3091 2863 self.drag_window(event.event)?;
2864 + self.connection.allow_events(Allow::REPLAY_POINTER, event.time)?;
3092 2865 } else if modkey_held && event.detail == ButtonIndex::M3.into() {
3093 2866 self.resize_window_with_mouse(event.event)?;
2867 + self.connection.allow_events(Allow::REPLAY_POINTER, event.time)?;
3094 2868 } else {
3095 2869 self.connection.allow_events(Allow::REPLAY_POINTER, event.time)?;
3096 2870 }
@@ -3115,117 +2889,109 @@ impl WindowManager {
3115 2889 }
3116 2890 }
3117 2891 Event::ConfigureRequest(event) => {
3118 - if self.windows.contains(&event.window) {
3119 - let monitor_index = self.clients.get(&event.window)
3120 - .map(|c| c.monitor_index)
3121 - .unwrap_or(self.selected_monitor);
3122 - let monitor = &self.monitors[monitor_index];
3123 - let is_floating = self.floating_windows.contains(&event.window);
3124 - let is_tiling_layout = self.layout.name() != "normie";
3125 -
3126 - if is_floating || !is_tiling_layout {
3127 - let cached_geom = self.window_geometry_cache.get(&event.window);
3128 - let border_width = self.config.border_width as u16;
3129 - let value_mask = event.value_mask;
3130 -
3131 - let final_x = if value_mask.contains(ConfigWindow::X) {
3132 - let mut x = event.x as i32;
3133 - x = x.max(monitor.screen_x);
3134 - if x + event.width as i32 + 2 * border_width as i32 > monitor.screen_x + monitor.screen_width as i32 {
3135 - x = monitor.screen_x + monitor.screen_width as i32 - event.width as i32 - 2 * border_width as i32;
2892 + if let Some(client) = self.clients.get(&event.window) {
2893 + let monitor = &self.monitors[client.monitor_index];
2894 + let is_floating = client.is_floating;
2895 + let has_layout = self.layout.name() != "normie";
2896 +
2897 + if event.value_mask.contains(ConfigWindow::BORDER_WIDTH) {
2898 + if let Some(c) = self.clients.get_mut(&event.window) {
2899 + c.border_width = event.border_width;
2900 + }
2901 + } else if is_floating || !has_layout {
2902 + let mut x = client.x_position as i32;
2903 + let mut y = client.y_position as i32;
2904 + let mut w = client.width as i32;
2905 + let mut h = client.height as i32;
2906 +
2907 + if event.value_mask.contains(ConfigWindow::X) {
2908 + if let Some(c) = self.clients.get_mut(&event.window) {
2909 + c.old_x_position = c.x_position;
3136 2910 }
3137 - x as i16
3138 - } else {
3139 - cached_geom.map(|g| g.x_position).unwrap_or(0)
3140 - };
3141 -
3142 - let final_y = if value_mask.contains(ConfigWindow::Y) {
3143 - let mut y = event.y as i32;
3144 - y = y.max(monitor.screen_y);
3145 - if y + event.height as i32 + 2 * border_width as i32 > monitor.screen_y + monitor.screen_height as i32 {
3146 - y = monitor.screen_y + monitor.screen_height as i32 - event.height as i32 - 2 * border_width as i32;
2911 + x = monitor.screen_x + event.x as i32;
2912 + }
2913 + if event.value_mask.contains(ConfigWindow::Y) {
2914 + if let Some(c) = self.clients.get_mut(&event.window) {
2915 + c.old_y_position = c.y_position;
3147 2916 }
3148 - y as i16
3149 - } else {
3150 - cached_geom.map(|g| g.y_position).unwrap_or(0)
3151 - };
3152 -
3153 - let mut final_width = if value_mask.contains(ConfigWindow::WIDTH) { event.width } else { cached_geom.map(|g| g.width).unwrap_or(1) };
3154 - let mut final_height = if value_mask.contains(ConfigWindow::HEIGHT) { event.height } else { cached_geom.map(|g| g.height).unwrap_or(1) };
3155 -
3156 - if let Some(client) = self.clients.get(&event.window) {
3157 - let (w, h) = self.apply_size_hints(client, final_width as i32, final_height as i32);
3158 - final_width = w as u16;
3159 - final_height = h as u16;
2917 + y = monitor.screen_y + event.y as i32;
2918 + }
2919 + if event.value_mask.contains(ConfigWindow::WIDTH) {
2920 + if let Some(c) = self.clients.get_mut(&event.window) {
2921 + c.old_width = c.width;
2922 + }
2923 + w = event.width as i32;
2924 + }
2925 + if event.value_mask.contains(ConfigWindow::HEIGHT) {
2926 + if let Some(c) = self.clients.get_mut(&event.window) {
2927 + c.old_height = c.height;
2928 + }
2929 + h = event.height as i32;
3160 2930 }
3161 2931
3162 - let mut config = ConfigureWindowAux::new()
3163 - .x(final_x as i32)
3164 - .y(final_y as i32)
3165 - .width(final_width as u32)
3166 - .height(final_height as u32);
2932 + let bw = self.config.border_width as i32;
2933 + let width_with_border = w + 2 * bw;
2934 + let height_with_border = h + 2 * bw;
3167 2935
3168 - if value_mask.contains(ConfigWindow::BORDER_WIDTH) {
3169 - config = config.border_width(event.border_width as u32);
2936 + if (x + w) > monitor.screen_x + monitor.screen_width as i32 && is_floating {
2937 + x = monitor.screen_x + (monitor.screen_width as i32 / 2 - width_with_border / 2);
3170 2938 }
3171 -
3172 - if value_mask.contains(ConfigWindow::SIBLING) {
3173 - config = config.sibling(event.sibling);
2939 + if (y + h) > monitor.screen_y + monitor.screen_height as i32 && is_floating {
2940 + y = monitor.screen_y + (monitor.screen_height as i32 / 2 - height_with_border / 2);
3174 2941 }
3175 2942
3176 - if value_mask.contains(ConfigWindow::STACK_MODE) {
3177 - config = config.stack_mode(event.stack_mode);
2943 + if let Some(c) = self.clients.get_mut(&event.window) {
2944 + c.x_position = x as i16;
2945 + c.y_position = y as i16;
2946 + c.width = w as u16;
2947 + c.height = h as u16;
3178 2948 }
3179 2949
3180 - self.connection.configure_window(event.window, &config)?;
3181 -
3182 - if let Some(client) = self.clients.get_mut(&event.window) {
3183 - client.old_x_position = client.x_position;
3184 - client.old_y_position = client.y_position;
3185 - client.old_width = client.width;
3186 - client.old_height = client.height;
3187 - client.x_position = final_x;
3188 - client.y_position = final_y;
3189 - client.width = final_width;
3190 - client.height = final_height;
2950 + let only_position_change = event.value_mask.contains(ConfigWindow::X) || event.value_mask.contains(ConfigWindow::Y);
2951 + let no_size_change = !event.value_mask.contains(ConfigWindow::WIDTH) && !event.value_mask.contains(ConfigWindow::HEIGHT);
2952 + if only_position_change && no_size_change {
2953 + self.send_configure_notify(event.window)?;
3191 2954 }
3192 2955
3193 - self.update_geometry_cache(event.window, CachedGeometry {
3194 - x_position: final_x,
3195 - y_position: final_y,
3196 - width: final_width,
3197 - height: final_height,
3198 - border_width: if value_mask.contains(ConfigWindow::BORDER_WIDTH) { event.border_width } else { border_width },
3199 - });
3200 -
3201 - if is_floating {
3202 - let new_monitor = self.get_monitor_for_rect(final_x as i32, final_y as i32, final_width as i32, final_height as i32);
3203 -
3204 - if new_monitor != monitor_index {
3205 - self.move_window_to_monitor(event.window, new_monitor)?;
3206 - }
2956 + if self.is_visible(event.window) {
2957 + self.connection.configure_window(
2958 + event.window,
2959 + &ConfigureWindowAux::new()
2960 + .x(x)
2961 + .y(y)
2962 + .width(w as u32)
2963 + .height(h as u32),
2964 + )?;
3207 2965 }
3208 2966 } else {
3209 2967 self.send_configure_notify(event.window)?;
3210 2968 }
3211 2969 } else {
3212 - let mut config = ConfigureWindowAux::new()
3213 - .x(event.x as i32)
3214 - .y(event.y as i32)
3215 - .width(event.width as u32)
3216 - .height(event.height as u32)
3217 - .border_width(event.border_width as u32);
3218 -
2970 + let mut aux = ConfigureWindowAux::new();
2971 + if event.value_mask.contains(ConfigWindow::X) {
2972 + aux = aux.x(event.x as i32);
2973 + }
2974 + if event.value_mask.contains(ConfigWindow::Y) {
2975 + aux = aux.y(event.y as i32);
2976 + }
2977 + if event.value_mask.contains(ConfigWindow::WIDTH) {
2978 + aux = aux.width(event.width as u32);
2979 + }
2980 + if event.value_mask.contains(ConfigWindow::HEIGHT) {
2981 + aux = aux.height(event.height as u32);
2982 + }
2983 + if event.value_mask.contains(ConfigWindow::BORDER_WIDTH) {
2984 + aux = aux.border_width(event.border_width as u32);
2985 + }
3219 2986 if event.value_mask.contains(ConfigWindow::SIBLING) {
3220 - config = config.sibling(event.sibling);
2987 + aux = aux.sibling(event.sibling);
3221 2988 }
3222 -
3223 2989 if event.value_mask.contains(ConfigWindow::STACK_MODE) {
3224 - config = config.stack_mode(event.stack_mode);
2990 + aux = aux.stack_mode(event.stack_mode);
3225 2991 }
3226 -
3227 - self.connection.configure_window(event.window, &config)?;
2992 + self.connection.configure_window(event.window, &aux)?;
3228 2993 }
2994 + self.connection.flush()?;
3229 2995 }
3230 2996 Event::ClientMessage(event) => {
3231 2997 if !self.clients.contains_key(&event.window) {
@@ -3293,6 +3059,11 @@ impl WindowManager {
3293 3059 }
3294 3060
3295 3061 fn apply_layout(&mut self) -> WmResult<()> {
3062 + for monitor_index in 0..self.monitors.len() {
3063 + let stack_head = self.monitors.get(monitor_index).and_then(|m| m.stack_head);
3064 + self.showhide(stack_head)?;
3065 + }
3066 +
3296 3067 let is_normie = self.layout.name() == LayoutType::Normie.as_str();
3297 3068
3298 3069 if !is_normie {
@@ -3360,10 +3131,12 @@ impl WindowManager {
3360 3131 let mut adjusted_width = geometry.width.saturating_sub(2 * border_width);
3361 3132 let mut adjusted_height = geometry.height.saturating_sub(2 * border_width);
3362 3133
3363 - if let Some(client) = self.clients.get(window) {
3134 + if let Some(client) = self.clients.get(window).cloned() {
3364 3135 if !client.is_floating {
3365 - let (hint_width, hint_height) = self.apply_size_hints(
3366 - client,
3136 + let (_, _, hint_width, hint_height, _) = self.apply_size_hints(
3137 + *window,
3138 + geometry.x_coordinate as i32,
3139 + geometry.y_coordinate as i32,
3367 3140 adjusted_width as i32,
3368 3141 adjusted_height as i32,
3369 3142 );
@@ -3392,13 +3165,13 @@ impl WindowManager {
3392 3165 .border_width(border_width),
3393 3166 )?;
3394 3167
3395 - self.update_geometry_cache(*window, CachedGeometry {
3396 - x_position: adjusted_x as i16,
3397 - y_position: adjusted_y as i16,
3398 - width: adjusted_width as u16,
3399 - height: adjusted_height as u16,
3400 - border_width: border_width as u16,
3401 - });
3168 + if let Some(c) = self.clients.get_mut(window) {
3169 + c.x_position = adjusted_x as i16;
3170 + c.y_position = adjusted_y as i16;
3171 + c.width = adjusted_width as u16;
3172 + c.height = adjusted_height as u16;
3173 + c.border_width = border_width as u16;
3174 + }
3402 3175 }
3403 3176 }
3404 3177 }
@@ -3494,52 +3267,33 @@ impl WindowManager {
3494 3267 Ok(())
3495 3268 }
3496 3269
3497 - fn update_geometry_cache(&mut self, window: Window, geometry: CachedGeometry) {
3498 - self.window_geometry_cache.insert(window, geometry);
3499 - }
3500 -
3501 - fn get_cached_geometry(&self, window: Window) -> Option<CachedGeometry> {
3502 - self.window_geometry_cache.get(&window).copied()
3503 - }
3504 -
3505 - fn get_or_query_geometry(&mut self, window: Window) -> WmResult<CachedGeometry> {
3506 - if let Some(cached) = self.get_cached_geometry(window) {
3507 - return Ok(cached);
3508 - }
3509 -
3510 - let geometry = self.connection.get_geometry(window)?.reply()?;
3511 - let cached = CachedGeometry {
3512 - x_position: geometry.x,
3513 - y_position: geometry.y,
3514 - width: geometry.width,
3515 - height: geometry.height,
3516 - border_width: geometry.border_width as u16,
3270 + fn send_configure_notify(&self, window: Window) -> WmResult<()> {
3271 + let client = self.clients.get(&window);
3272 + let (x, y, w, h, bw) = if let Some(c) = client {
3273 + (c.x_position, c.y_position, c.width, c.height, c.border_width)
3274 + } else {
3275 + let geom = self.connection.get_geometry(window)?.reply()?;
3276 + (geom.x, geom.y, geom.width, geom.height, geom.border_width)
3517 3277 };
3518 - self.update_geometry_cache(window, cached);
3519 - Ok(cached)
3520 - }
3521 -
3522 - fn send_configure_notify(&mut self, window: Window) -> WmResult<()> {
3523 - let geometry = self.get_or_query_geometry(window)?;
3524 3278
3525 - let event = x11rb::protocol::xproto::ConfigureNotifyEvent {
3526 - response_type: x11rb::protocol::xproto::CONFIGURE_NOTIFY_EVENT,
3279 + let event = ConfigureNotifyEvent {
3280 + response_type: CONFIGURE_NOTIFY_EVENT,
3527 3281 sequence: 0,
3528 3282 event: window,
3529 3283 window,
3530 3284 above_sibling: x11rb::NONE,
3531 - x: geometry.x_position,
3532 - y: geometry.y_position,
3533 - width: geometry.width,
3534 - height: geometry.height,
3535 - border_width: geometry.border_width,
3285 + x,
3286 + y,
3287 + width: w,
3288 + height: h,
3289 + border_width: bw,
3536 3290 override_redirect: false,
3537 3291 };
3538 3292
3539 3293 self.connection.send_event(
3540 3294 false,
3541 3295 window,
3542 - x11rb::protocol::xproto::EventMask::STRUCTURE_NOTIFY,
3296 + EventMask::STRUCTURE_NOTIFY,
3543 3297 event,
3544 3298 )?;
3545 3299
@@ -3572,62 +3326,57 @@ impl WindowManager {
3572 3326 return Ok(());
3573 3327 }
3574 3328
3329 + use crate::size_hints::{flags::*, offset::*};
3330 +
3575 3331 let read_u32 = |offset: usize| -> u32 {
3576 3332 let bytes = &size_hints.value[offset * 4..(offset + 1) * 4];
3577 3333 u32::from_ne_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])
3578 3334 };
3579 3335
3580 - let flags = read_u32(0);
3581 -
3582 - const P_SIZE: u32 = 1 << 3;
3583 - const P_MIN_SIZE: u32 = 1 << 4;
3584 - const P_MAX_SIZE: u32 = 1 << 5;
3585 - const P_RESIZE_INC: u32 = 1 << 6;
3586 - const P_ASPECT: u32 = 1 << 7;
3587 - const P_BASE_SIZE: u32 = 1 << 8;
3336 + let flags = read_u32(FLAGS);
3588 3337
3589 3338 if let Some(client) = self.clients.get_mut(&window) {
3590 3339 if flags & P_BASE_SIZE != 0 {
3591 - client.base_width = read_u32(8) as i32;
3592 - client.base_height = read_u32(9) as i32;
3340 + client.base_width = read_u32(BASE_WIDTH) as i32;
3341 + client.base_height = read_u32(BASE_HEIGHT) as i32;
3593 3342 } else if flags & P_MIN_SIZE != 0 {
3594 - client.base_width = read_u32(5) as i32;
3595 - client.base_height = read_u32(6) as i32;
3343 + client.base_width = read_u32(MIN_WIDTH) as i32;
3344 + client.base_height = read_u32(MIN_HEIGHT) as i32;
3596 3345 } else {
3597 3346 client.base_width = 0;
3598 3347 client.base_height = 0;
3599 3348 }
3600 3349
3601 3350 if flags & P_RESIZE_INC != 0 {
3602 - client.increment_width = read_u32(10) as i32;
3603 - client.increment_height = read_u32(11) as i32;
3351 + client.increment_width = read_u32(WIDTH_INC) as i32;
3352 + client.increment_height = read_u32(HEIGHT_INC) as i32;
3604 3353 } else {
3605 3354 client.increment_width = 0;
3606 3355 client.increment_height = 0;
3607 3356 }
3608 3357
3609 3358 if flags & P_MAX_SIZE != 0 {
3610 - client.max_width = read_u32(7) as i32;
3611 - client.max_height = read_u32(8) as i32;
3359 + client.max_width = read_u32(MAX_WIDTH) as i32;
3360 + client.max_height = read_u32(MAX_HEIGHT) as i32;
3612 3361 } else {
3613 3362 client.max_width = 0;
3614 3363 client.max_height = 0;
3615 3364 }
3616 3365
3617 3366 if flags & P_MIN_SIZE != 0 {
3618 - client.min_width = read_u32(5) as i32;
3619 - client.min_height = read_u32(6) as i32;
3620 - } else if flags & P_SIZE != 0 {
3621 - client.min_width = read_u32(3) as i32;
3622 - client.min_height = read_u32(4) as i32;
3367 + client.min_width = read_u32(MIN_WIDTH) as i32;
3368 + client.min_height = read_u32(MIN_HEIGHT) as i32;
3369 + } else if flags & P_BASE_SIZE != 0 {
3370 + client.min_width = read_u32(BASE_WIDTH) as i32;
3371 + client.min_height = read_u32(BASE_HEIGHT) as i32;
3623 3372 } else {
3624 3373 client.min_width = 0;
3625 3374 client.min_height = 0;
3626 3375 }
3627 3376
3628 3377 if flags & P_ASPECT != 0 {
3629 - client.min_aspect = (read_u32(12) as f32) / (read_u32(13) as f32).max(1.0);
3630 - client.max_aspect = (read_u32(14) as f32) / (read_u32(15) as f32).max(1.0);
3378 + client.min_aspect = (read_u32(MIN_ASPECT_Y) as f32) / (read_u32(MIN_ASPECT_X) as f32).max(1.0);
3379 + client.max_aspect = (read_u32(MAX_ASPECT_X) as f32) / (read_u32(MAX_ASPECT_Y) as f32).max(1.0);
3631 3380 } else {
3632 3381 client.min_aspect = 0.0;
3633 3382 client.max_aspect = 0.0;
@@ -3773,51 +3522,101 @@ impl WindowManager {
3773 3522 Ok(())
3774 3523 }
3775 3524
3776 - fn apply_size_hints(&self, client: &Client, mut width: i32, mut height: i32) -> (i32, i32) {
3777 - let min_size = 20;
3778 - width = width.max(min_size);
3779 - height = height.max(min_size);
3525 + fn apply_size_hints(&mut self, window: Window, mut x: i32, mut y: i32, mut w: i32, mut h: i32) -> (i32, i32, i32, i32, bool) {
3526 + let bh = 20;
3780 3527
3781 - if !client.hints_valid {
3782 - return (width, height);
3783 - }
3528 + let (client_x, client_y, client_w, client_h, bw, monitor_index, is_floating, mut hints_valid) = {
3529 + let client = match self.clients.get(&window) {
3530 + Some(c) => c,
3531 + None => return (x, y, w, h, false),
3532 + };
3533 + (client.x_position as i32, client.y_position as i32, client.width as i32, client.height as i32,
3534 + client.border_width as i32, client.monitor_index, client.is_floating, client.hints_valid)
3535 + };
3536 +
3537 + let monitor = &self.monitors[monitor_index];
3538 + let client_width = client_w + 2 * bw;
3539 + let client_height = client_h + 2 * bw;
3784 3540
3785 - if client.min_width > 0 {
3786 - width = width.max(client.min_width);
3541 + w = w.max(1);
3542 + h = h.max(1);
3543 +
3544 + if x >= monitor.window_area_x + monitor.window_area_width {
3545 + x = monitor.window_area_x + monitor.window_area_width - client_width;
3787 3546 }
3788 - if client.min_height > 0 {
3789 - height = height.max(client.min_height);
3547 + if y >= monitor.window_area_y + monitor.window_area_height {
3548 + y = monitor.window_area_y + monitor.window_area_height - client_height;
3790 3549 }
3791 -
3792 - if client.max_width > 0 {
3793 - width = width.min(client.max_width);
3550 + if x + w + 2 * bw <= monitor.window_area_x {
3551 + x = monitor.window_area_x;
3794 3552 }
3795 - if client.max_height > 0 {
3796 - height = height.min(client.max_height);
3553 + if y + h + 2 * bw <= monitor.window_area_y {
3554 + y = monitor.window_area_y;
3797 3555 }
3798 3556
3799 - if client.increment_width > 0 {
3800 - width -= client.base_width;
3801 - width -= width % client.increment_width;
3802 - width += client.base_width;
3557 + if h < bh {
3558 + h = bh;
3803 3559 }
3804 - if client.increment_height > 0 {
3805 - height -= client.base_height;
3806 - height -= height % client.increment_height;
3807 - height += client.base_height;
3560 + if w < bh {
3561 + w = bh;
3808 3562 }
3809 3563
3810 - if client.min_aspect > 0.0 || client.max_aspect > 0.0 {
3811 - let actual_aspect = width as f32 / height as f32;
3564 + if is_floating || self.layout.name() == "normie" {
3565 + if !hints_valid {
3566 + let _ = self.update_size_hints(window);
3567 + hints_valid = self.clients.get(&window).map(|c| c.hints_valid).unwrap_or(false);
3568 + }
3812 3569
3813 - if client.max_aspect > 0.0 && actual_aspect > client.max_aspect {
3814 - width = (height as f32 * client.max_aspect) as i32;
3815 - } else if client.min_aspect > 0.0 && actual_aspect < client.min_aspect {
3816 - height = (width as f32 / client.min_aspect) as i32;
3570 + if hints_valid {
3571 + let (base_width, base_height, min_width, min_height, max_width, max_height,
3572 + inc_width, inc_height, min_aspect, max_aspect) = {
3573 + let client = self.clients.get(&window).unwrap();
3574 + (client.base_width, client.base_height, client.min_width, client.min_height,
3575 + client.max_width, client.max_height, client.increment_width, client.increment_height,
3576 + client.min_aspect, client.max_aspect)
3577 + };
3578 +
3579 + let base_is_min = base_width == min_width && base_height == min_height;
3580 +
3581 + if !base_is_min {
3582 + w -= base_width;
3583 + h -= base_height;
3584 + }
3585 +
3586 + if min_aspect > 0.0 && max_aspect > 0.0 {
3587 + if max_aspect < (w as f32 / h as f32) {
3588 + w = (h as f32 * max_aspect + 0.5) as i32;
3589 + } else if min_aspect < (h as f32 / w as f32) {
3590 + h = (w as f32 * min_aspect + 0.5) as i32;
3591 + }
3592 + }
3593 +
3594 + if base_is_min {
3595 + w -= base_width;
3596 + h -= base_height;
3597 + }
3598 +
3599 + if inc_width > 0 {
3600 + w -= w % inc_width;
3601 + }
3602 + if inc_height > 0 {
3603 + h -= h % inc_height;
3604 + }
3605 +
3606 + w = (w + base_width).max(min_width);
3607 + h = (h + base_height).max(min_height);
3608 +
3609 + if max_width > 0 {
3610 + w = w.min(max_width);
3611 + }
3612 + if max_height > 0 {
3613 + h = h.min(max_height);
3614 + }
3817 3615 }
3818 3616 }
3819 3617
3820 - (width.max(1), height.max(1))
3618 + let changed = x != client_x || y != client_y || w != client_w || h != client_h;
3619 + (x, y, w, h, changed)
3821 3620 }
3822 3621
3823 3622 fn next_tiled(&self, start: Option<Window>, monitor: &Monitor) -> Option<Window> {
@@ -3991,7 +3790,6 @@ impl WindowManager {
3991 3790 }
3992 3791
3993 3792 self.windows.retain(|&w| w != window);
3994 - self.window_geometry_cache.remove(&window);
3995 3793 self.floating_windows.remove(&window);
3996 3794
3997 3795 if self.windows.len() < initial_count {