tony / oxwm

- Extracted manage_window() function following dwm's manage() pattern (MapRequest: 153 lines → 13 lines) Browse files
- Implemented off-screen window placement before mapping to prevent visual flashing
  - Fixed transient/dialog window positioning (center over parent or on monitor)
  - Fixed normie mode multi-monitor support (use per-monitor dimensions, not combined width)
  - Fixed floating window geometry calculations to properly respect monitor bounds
tonybtw committed 2025-11-29 parent 8d0e55b commit 6faa550b2d25b985797d53536fa40e3d61535e1c
Showing 4 changed files with 488 additions and 151 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.7.3" 294 +version = "0.7.4"
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.7.3" 3 +version = "0.7.4"
4 edition = "2024" 4 edition = "2024"
5 5
6 [lib] 6 [lib]
+39 −16 src/keyboard/handlers.rs
@@ -148,6 +148,13 @@ pub fn setup_keybinds(
148 let (keysym_to_keycode, _) = build_keysym_maps(connection)?; 148 let (keysym_to_keycode, _) = build_keysym_maps(connection)?;
149 let mut grabbed_keys: HashSet<(u16, Keycode)> = HashSet::new(); 149 let mut grabbed_keys: HashSet<(u16, Keycode)> = HashSet::new();
150 150
151 + let ignore_modifiers = [
152 + 0,
153 + u16::from(ModMask::LOCK),
154 + u16::from(ModMask::M2),
155 + u16::from(ModMask::LOCK | ModMask::M2),
156 + ];
157 +
151 for keybinding in keybindings { 158 for keybinding in keybindings {
152 if keybinding.keys.is_empty() { 159 if keybinding.keys.is_empty() {
153 continue; 160 continue;
@@ -158,17 +165,20 @@ pub fn setup_keybinds(
158 165
159 if let Some(keycodes) = keysym_to_keycode.get(&first_key.keysym) { 166 if let Some(keycodes) = keysym_to_keycode.get(&first_key.keysym) {
160 if let Some(&keycode) = keycodes.first() { 167 if let Some(&keycode) = keycodes.first() {
161 - let key_tuple = (modifier_mask, keycode); 168 + for &ignore_mask in &ignore_modifiers {
162 - 169 + let grab_mask = modifier_mask | ignore_mask;
163 - if grabbed_keys.insert(key_tuple) { 170 + let key_tuple = (grab_mask, keycode);
164 - connection.grab_key( 171 +
165 - false, 172 + if grabbed_keys.insert(key_tuple) {
166 - root, 173 + connection.grab_key(
167 - modifier_mask.into(), 174 + false,
168 - keycode, 175 + root,
169 - GrabMode::ASYNC, 176 + grab_mask.into(),
170 - GrabMode::ASYNC, 177 + keycode,
171 - )?; 178 + GrabMode::ASYNC,
179 + GrabMode::ASYNC,
180 + )?;
181 + }
172 } 182 }
173 } 183 }
174 } 184 }
@@ -209,6 +219,8 @@ fn handle_first_key(
209 ) -> KeychordResult { 219 ) -> KeychordResult {
210 let mut candidates = Vec::new(); 220 let mut candidates = Vec::new();
211 221
222 + let clean_state = event.state & !(u16::from(ModMask::LOCK) | u16::from(ModMask::M2));
223 +
212 for (keybinding_index, keybinding) in keybindings.iter().enumerate() { 224 for (keybinding_index, keybinding) in keybindings.iter().enumerate() {
213 if keybinding.keys.is_empty() { 225 if keybinding.keys.is_empty() {
214 continue; 226 continue;
@@ -217,7 +229,7 @@ fn handle_first_key(
217 let first_key = &keybinding.keys[0]; 229 let first_key = &keybinding.keys[0];
218 let modifier_mask = modifiers_to_mask(&first_key.modifiers); 230 let modifier_mask = modifiers_to_mask(&first_key.modifiers);
219 231
220 - if event_keysym == first_key.keysym && event.state == modifier_mask.into() { 232 + if event_keysym == first_key.keysym && clean_state == modifier_mask.into() {
221 if keybinding.keys.len() == 1 { 233 if keybinding.keys.len() == 1 {
222 return KeychordResult::Completed(keybinding.func, keybinding.arg.clone()); 234 return KeychordResult::Completed(keybinding.func, keybinding.arg.clone());
223 } else { 235 } else {
@@ -242,6 +254,8 @@ fn handle_next_key(
242 ) -> KeychordResult { 254 ) -> KeychordResult {
243 let mut new_candidates = Vec::new(); 255 let mut new_candidates = Vec::new();
244 256
257 + let clean_state = event.state & !(u16::from(ModMask::LOCK) | u16::from(ModMask::M2));
258 +
245 for &candidate_index in candidates { 259 for &candidate_index in candidates {
246 let keybinding = &keybindings[candidate_index]; 260 let keybinding = &keybindings[candidate_index];
247 261
@@ -251,12 +265,11 @@ fn handle_next_key(
251 265
252 let next_key = &keybinding.keys[keys_pressed]; 266 let next_key = &keybinding.keys[keys_pressed];
253 let required_mask = modifiers_to_mask(&next_key.modifiers); 267 let required_mask = modifiers_to_mask(&next_key.modifiers);
254 - let event_state: u16 = event.state.into();
255 268
256 let modifiers_match = if next_key.modifiers.is_empty() { 269 let modifiers_match = if next_key.modifiers.is_empty() {
257 true 270 true
258 } else { 271 } else {
259 - (event_state & required_mask) == required_mask 272 + (clean_state & required_mask) == required_mask.into()
260 }; 273 };
261 274
262 if event_keysym == next_key.keysym && modifiers_match { 275 if event_keysym == next_key.keysym && modifiers_match {
@@ -275,7 +288,7 @@ fn handle_next_key(
275 } 288 }
276 } 289 }
277 290
278 -pub fn handle_spawn_action(action: KeyAction, arg: &Arg) -> Result<()> { 291 +pub fn handle_spawn_action(action: KeyAction, arg: &Arg, selected_monitor: usize) -> Result<()> {
279 if let KeyAction::Spawn = action { 292 if let KeyAction::Spawn = action {
280 match arg { 293 match arg {
281 Arg::Str(command) => match Command::new(command.as_str()).spawn() { 294 Arg::Str(command) => match Command::new(command.as_str()).spawn() {
@@ -293,7 +306,17 @@ pub fn handle_spawn_action(action: KeyAction, arg: &Arg) -> Result<()> {
293 return Ok(()); 306 return Ok(());
294 }; 307 };
295 308
296 - let args_str: Vec<&str> = args.iter().map(|s| s.as_str()).collect(); 309 + let mut args_vec: Vec<String> = args.to_vec();
310 +
311 + let is_dmenu = cmd.contains("dmenu");
312 + let has_monitor_flag = args.iter().any(|arg| arg == "-m");
313 +
314 + if is_dmenu && !has_monitor_flag {
315 + args_vec.insert(0, selected_monitor.to_string());
316 + args_vec.insert(0, "-m".to_string());
317 + }
318 +
319 + let args_str: Vec<&str> = args_vec.iter().map(|s| s.as_str()).collect();
297 match Command::new(cmd.as_str()).args(&args_str).spawn() { 320 match Command::new(cmd.as_str()).args(&args_str).spawn() {
298 Err(error) if error.kind() == ErrorKind::NotFound => { 321 Err(error) if error.kind() == ErrorKind::NotFound => {
299 eprintln!( 322 eprintln!(
+447 −133 src/window_manager.rs Large diff collapsed, click to expand
@@ -43,6 +43,8 @@ struct AtomCache {
43 wm_delete_window: Atom, 43 wm_delete_window: Atom,
44 net_wm_state: Atom, 44 net_wm_state: Atom,
45 net_wm_state_fullscreen: Atom, 45 net_wm_state_fullscreen: Atom,
46 + net_wm_window_type: Atom,
47 + net_wm_window_type_dialog: Atom,
46 } 48 }
47 49
48 impl AtomCache { 50 impl AtomCache {
@@ -79,6 +81,16 @@ impl AtomCache {
79 .reply()? 81 .reply()?
80 .atom; 82 .atom;
81 83
84 + let net_wm_window_type = connection
85 + .intern_atom(false, b"_NET_WM_WINDOW_TYPE")?
86 + .reply()?
87 + .atom;
88 +
89 + let net_wm_window_type_dialog = connection
90 + .intern_atom(false, b"_NET_WM_WINDOW_TYPE_DIALOG")?
91 + .reply()?
92 + .atom;
93 +
82 Ok(Self { 94 Ok(Self {
83 net_current_desktop, 95 net_current_desktop,
84 net_client_info, 96 net_client_info,
@@ -87,6 +99,8 @@ impl AtomCache {
87 wm_delete_window, 99 wm_delete_window,
88 net_wm_state, 100 net_wm_state,
89 net_wm_state_fullscreen, 101 net_wm_state_fullscreen,
102 + net_wm_window_type,
103 + net_wm_window_type_dialog,
90 }) 104 })
91 } 105 }
92 } 106 }
@@ -154,29 +168,40 @@ impl WindowManager {
154 )? 168 )?
155 .check()?; 169 .check()?;
156 170
157 - connection.grab_button( 171 + let ignore_modifiers = [
158 - false, 172 + 0,
159 - root, 173 + u16::from(ModMask::LOCK),
160 - EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, 174 + u16::from(ModMask::M2),
161 - GrabMode::SYNC, 175 + u16::from(ModMask::LOCK | ModMask::M2),
162 - GrabMode::ASYNC, 176 + ];
163 - x11rb::NONE,
164 - x11rb::NONE,
165 - ButtonIndex::M1,
166 - u16::from(config.modkey).into(),
167 - )?;
168 177
169 - connection.grab_button( 178 + for &ignore_mask in &ignore_modifiers {
170 - false, 179 + let grab_mask = u16::from(config.modkey) | ignore_mask;
171 - root, 180 +
172 - EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE, 181 + connection.grab_button(
173 - GrabMode::SYNC, 182 + false,
174 - GrabMode::ASYNC, 183 + root,
175 - x11rb::NONE, 184 + EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE,
176 - x11rb::NONE, 185 + GrabMode::SYNC,
177 - ButtonIndex::M3, 186 + GrabMode::ASYNC,
178 - u16::from(config.modkey).into(), 187 + x11rb::NONE,
179 - )?; 188 + x11rb::NONE,
189 + ButtonIndex::M1,
190 + grab_mask.into(),
191 + )?;
192 +
193 + connection.grab_button(
194 + false,
195 + root,
196 + EventMask::BUTTON_PRESS | EventMask::BUTTON_RELEASE,
197 + GrabMode::SYNC,
198 + GrabMode::ASYNC,
199 + x11rb::NONE,
200 + x11rb::NONE,
201 + ButtonIndex::M3,
202 + grab_mask.into(),
203 + )?;
204 + }
180 205
181 let monitors = detect_monitors(&connection, &screen, root)?; 206 let monitors = detect_monitors(&connection, &screen, root)?;
182 207
@@ -966,7 +991,7 @@ impl WindowManager {
966 991
967 fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> WmResult<()> { 992 fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> WmResult<()> {
968 match action { 993 match action {
969 - KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?, 994 + KeyAction::Spawn => handlers::handle_spawn_action(action, arg, self.selected_monitor)?,
970 KeyAction::SpawnTerminal => { 995 KeyAction::SpawnTerminal => {
971 use std::process::Command; 996 use std::process::Command;
972 let terminal = &self.config.terminal; 997 let terminal = &self.config.terminal;
@@ -1431,6 +1456,13 @@ impl WindowManager {
1431 1456
1432 let mut grabbed_keys: HashSet<(u16, Keycode)> = HashSet::new(); 1457 let mut grabbed_keys: HashSet<(u16, Keycode)> = HashSet::new();
1433 1458
1459 + let ignore_modifiers = [
1460 + 0,
1461 + u16::from(ModMask::LOCK),
1462 + u16::from(ModMask::M2),
1463 + u16::from(ModMask::LOCK | ModMask::M2),
1464 + ];
1465 +
1434 for &candidate_index in candidates { 1466 for &candidate_index in candidates {
1435 let binding = &self.config.keybindings[candidate_index]; 1467 let binding = &self.config.keybindings[candidate_index];
1436 if keys_pressed < binding.keys.len() { 1468 if keys_pressed < binding.keys.len() {
@@ -1439,17 +1471,20 @@ impl WindowManager {
1439 1471
1440 if let Some(keycodes) = keysym_to_keycode.get(&next_key.keysym) { 1472 if let Some(keycodes) = keysym_to_keycode.get(&next_key.keysym) {
1441 if let Some(&keycode) = keycodes.first() { 1473 if let Some(&keycode) = keycodes.first() {
1442 - let key_tuple = (modifier_mask, keycode); 1474 + for &ignore_mask in &ignore_modifiers {
1443 - 1475 + let grab_mask = modifier_mask | ignore_mask;
1444 - if grabbed_keys.insert(key_tuple) { 1476 + let key_tuple = (grab_mask, keycode);
1445 - self.connection.grab_key( 1477 +
1446 - false, 1478 + if grabbed_keys.insert(key_tuple) {
1447 - self.root, 1479 + self.connection.grab_key(
1448 - modifier_mask.into(), 1480 + false,
1449 - keycode, 1481 + self.root,
1450 - GrabMode::ASYNC, 1482 + grab_mask.into(),
1451 - GrabMode::ASYNC, 1483 + keycode,
1452 - )?; 1484 + GrabMode::ASYNC,
1485 + GrabMode::ASYNC,
1486 + )?;
1487 + }
1453 } 1488 }
1454 } 1489 }
1455 } 1490 }
@@ -1458,14 +1493,16 @@ impl WindowManager {
1458 1493
1459 if let Some(keycodes) = keysym_to_keycode.get(&keyboard::keysyms::XK_ESCAPE) { 1494 if let Some(keycodes) = keysym_to_keycode.get(&keyboard::keysyms::XK_ESCAPE) {
1460 if let Some(&keycode) = keycodes.first() { 1495 if let Some(&keycode) = keycodes.first() {
1461 - self.connection.grab_key( 1496 + for &ignore_mask in &ignore_modifiers {
1462 - false, 1497 + self.connection.grab_key(
1463 - self.root, 1498 + false,
1464 - ModMask::from(0u16), 1499 + self.root,
1465 - keycode, 1500 + ModMask::from(ignore_mask),
1466 - GrabMode::ASYNC, 1501 + keycode,
1467 - GrabMode::ASYNC, 1502 + GrabMode::ASYNC,
1468 - )?; 1503 + GrabMode::ASYNC,
1504 + )?;
1505 + }
1469 } 1506 }
1470 } 1507 }
1471 1508
@@ -1482,12 +1519,9 @@ impl WindowManager {
1482 } 1519 }
1483 1520
1484 fn kill_client(&self, window: Window) -> WmResult<()> { 1521 fn kill_client(&self, window: Window) -> WmResult<()> {
1485 - // Try to send WM_DELETE_WINDOW message first (graceful close)
1486 if self.send_event(window, self.atoms.wm_delete_window)? { 1522 if self.send_event(window, self.atoms.wm_delete_window)? {
1487 - // Window supports WM_DELETE_WINDOW, it will close gracefully
1488 self.connection.flush()?; 1523 self.connection.flush()?;
1489 } else { 1524 } else {
1490 - // Window doesn't support WM_DELETE_WINDOW, forcefully kill it
1491 eprintln!("Window {} doesn't support WM_DELETE_WINDOW, killing forcefully", window); 1525 eprintln!("Window {} doesn't support WM_DELETE_WINDOW, killing forcefully", window);
1492 self.connection.kill_client(window)?; 1526 self.connection.kill_client(window)?;
1493 self.connection.flush()?; 1527 self.connection.flush()?;
@@ -1496,7 +1530,6 @@ impl WindowManager {
1496 } 1530 }
1497 1531
1498 fn send_event(&self, window: Window, protocol: Atom) -> WmResult<bool> { 1532 fn send_event(&self, window: Window, protocol: Atom) -> WmResult<bool> {
1499 - // Check if the window supports this protocol
1500 let protocols_reply = self.connection.get_property( 1533 let protocols_reply = self.connection.get_property(
1501 false, 1534 false,
1502 window, 1535 window,
@@ -1521,7 +1554,6 @@ impl WindowManager {
1521 return Ok(false); 1554 return Ok(false);
1522 } 1555 }
1523 1556
1524 - // Send a ClientMessage event
1525 let event = x11rb::protocol::xproto::ClientMessageEvent { 1557 let event = x11rb::protocol::xproto::ClientMessageEvent {
1526 response_type: x11rb::protocol::xproto::CLIENT_MESSAGE_EVENT, 1558 response_type: x11rb::protocol::xproto::CLIENT_MESSAGE_EVENT,
1527 format: 32, 1559 format: 32,
@@ -1736,8 +1768,8 @@ impl WindowManager {
1736 Ok(()) 1768 Ok(())
1737 } 1769 }
1738 1770
1739 - fn is_transient_window(&self, window: Window) -> bool { 1771 + fn get_transient_parent(&self, window: Window) -> Option<Window> {
1740 - let transient_for = self.connection 1772 + self.connection
1741 .get_property( 1773 .get_property(
1742 false, 1774 false,
1743 window, 1775 window,
@@ -1748,9 +1780,233 @@ impl WindowManager {
1748 ) 1780 )
1749 .ok() 1781 .ok()
1750 .and_then(|cookie| cookie.reply().ok()) 1782 .and_then(|cookie| cookie.reply().ok())
1751 - .filter(|reply| !reply.value.is_empty()); 1783 + .filter(|reply| !reply.value.is_empty())
1784 + .and_then(|reply| {
1785 + if reply.value.len() >= 4 {
1786 + let parent_window = u32::from_ne_bytes([
1787 + reply.value[0],
1788 + reply.value[1],
1789 + reply.value[2],
1790 + reply.value[3],
1791 + ]);
1792 + Some(parent_window)
1793 + } else {
1794 + None
1795 + }
1796 + })
1797 + }
1798 +
1799 + fn is_transient_window(&self, window: Window) -> bool {
1800 + self.get_transient_parent(window).is_some()
1801 + }
1802 +
1803 + fn is_dialog_window(&self, window: Window) -> bool {
1804 + let window_type_property = self.connection
1805 + .get_property(
1806 + false,
1807 + window,
1808 + self.atoms.net_wm_window_type,
1809 + AtomEnum::ATOM,
1810 + 0,
1811 + 32,
1812 + )
1813 + .ok()
1814 + .and_then(|cookie| cookie.reply().ok());
1815 +
1816 + if let Some(reply) = window_type_property {
1817 + let atoms: Vec<Atom> = reply
1818 + .value
1819 + .chunks_exact(4)
1820 + .map(|chunk| u32::from_ne_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]))
1821 + .collect();
1822 +
1823 + atoms.contains(&self.atoms.net_wm_window_type_dialog)
1824 + } else {
1825 + false
1826 + }
1827 + }
1828 +
1829 + fn get_window_class(&self, window: Window) -> Option<String> {
1830 + self.connection
1831 + .get_property(false, window, AtomEnum::WM_CLASS, AtomEnum::STRING, 0, 1024)
1832 + .ok()
1833 + .and_then(|cookie| cookie.reply().ok())
1834 + .and_then(|reply| {
1835 + if reply.value.is_empty() {
1836 + None
1837 + } else {
1838 + std::str::from_utf8(&reply.value).ok().map(|s| {
1839 + s.split('\0').nth(1).unwrap_or(s.split('\0').next().unwrap_or("")).to_string()
1840 + })
1841 + }
1842 + })
1843 + }
1844 +
1845 + fn manage_window(&mut self, window: Window) -> WmResult<()> {
1846 + let geometry = self.connection.get_geometry(window)?.reply()?;
1847 + let mut window_x = geometry.x as i32;
1848 + let mut window_y = geometry.y as i32;
1849 + let window_width = geometry.width as u32;
1850 + let window_height = geometry.height as u32;
1851 +
1852 + let transient_parent = self.get_transient_parent(window);
1853 + let (window_tags, monitor_index) = if let Some(parent) = transient_parent {
1854 + let parent_tags = self.window_tags.get(&parent).copied().unwrap_or_else(|| {
1855 + self.monitors
1856 + .get(self.selected_monitor)
1857 + .map(|m| m.selected_tags)
1858 + .unwrap_or(tag_mask(0))
1859 + });
1860 + let parent_monitor = self.window_monitor.get(&parent).copied().unwrap_or(self.selected_monitor);
1861 + (parent_tags, parent_monitor)
1862 + } else {
1863 + let selected_tags = self.monitors
1864 + .get(self.selected_monitor)
1865 + .map(|m| m.selected_tags)
1866 + .unwrap_or(tag_mask(0));
1867 + (selected_tags, self.selected_monitor)
1868 + };
1869 +
1870 + let monitor = self.monitors[monitor_index].clone();
1871 + let border_width = self.config.border_width;
1872 +
1873 + if window_x + (window_width as i32) + (2 * border_width as i32) > monitor.x + monitor.width as i32 {
1874 + window_x = monitor.x + monitor.width as i32 - (window_width as i32) - (2 * border_width as i32);
1875 + }
1876 + if window_y + (window_height as i32) + (2 * border_width as i32) > monitor.y + monitor.height as i32 {
1877 + window_y = monitor.y + monitor.height as i32 - (window_height as i32) - (2 * border_width as i32);
1878 + }
1879 + window_x = window_x.max(monitor.x);
1880 + window_y = window_y.max(monitor.y);
1881 +
1882 + let is_transient = transient_parent.is_some();
1883 + let is_dialog = self.is_dialog_window(window);
1884 +
1885 + let class_name = self.get_window_class(window).unwrap_or_default();
1886 + eprintln!("MapRequest 0x{:x}: class='{}' size={}x{} pos=({},{}) transient={} dialog={}",
1887 + window, class_name, window_width, window_height, window_x, window_y, is_transient, is_dialog);
1888 +
1889 + let off_screen_x = window_x + (2 * self.screen.width_in_pixels as i32);
1890 +
1891 + self.connection.configure_window(
1892 + window,
1893 + &ConfigureWindowAux::new()
1894 + .x(off_screen_x)
1895 + .y(window_y)
1896 + .width(window_width)
1897 + .height(window_height)
1898 + .border_width(border_width)
1899 + )?;
1900 +
1901 + self.connection.change_window_attributes(
1902 + window,
1903 + &ChangeWindowAttributesAux::new().event_mask(
1904 + EventMask::ENTER_WINDOW | EventMask::STRUCTURE_NOTIFY | EventMask::PROPERTY_CHANGE
1905 + ),
1906 + )?;
1907 +
1908 + self.windows.push(window);
1909 + self.window_tags.insert(window, window_tags);
1910 + self.window_monitor.insert(window, monitor_index);
1911 +
1912 + if is_transient || is_dialog {
1913 + self.floating_windows.insert(window);
1914 +
1915 + let (center_x, center_y) = if let Some(parent) = transient_parent {
1916 + if let Ok(parent_geom) = self.connection.get_geometry(parent)?.reply() {
1917 + let parent_center_x = parent_geom.x as i32 + (parent_geom.width as i32 / 2);
1918 + let parent_center_y = parent_geom.y as i32 + (parent_geom.height as i32 / 2);
1919 + (parent_center_x, parent_center_y)
1920 + } else {
1921 + let monitor_center_x = monitor.x + (monitor.width as i32 / 2);
1922 + let monitor_center_y = monitor.y + (monitor.height as i32 / 2);
1923 + (monitor_center_x, monitor_center_y)
1924 + }
1925 + } else {
1926 + let monitor_center_x = monitor.x + (monitor.width as i32 / 2);
1927 + let monitor_center_y = monitor.y + (monitor.height as i32 / 2);
1928 + (monitor_center_x, monitor_center_y)
1929 + };
1930 +
1931 + let positioned_x = center_x - (window_width as i32 / 2);
1932 + let positioned_y = center_y - (window_height as i32 / 2);
1933 +
1934 + let clamped_x = positioned_x
1935 + .max(monitor.x)
1936 + .min(monitor.x + monitor.width as i32 - window_width as i32);
1937 + let clamped_y = positioned_y
1938 + .max(monitor.y)
1939 + .min(monitor.y + monitor.height as i32 - window_height as i32);
1940 +
1941 + self.update_geometry_cache(window, CachedGeometry {
1942 + x_position: clamped_x as i16,
1943 + y_position: clamped_y as i16,
1944 + width: window_width as u16,
1945 + height: window_height as u16,
1946 + border_width: border_width as u16,
1947 + });
1948 +
1949 + self.connection.configure_window(
1950 + window,
1951 + &ConfigureWindowAux::new()
1952 + .x(clamped_x)
1953 + .y(clamped_y)
1954 + .width(window_width)
1955 + .height(window_height)
1956 + .border_width(border_width)
1957 + .stack_mode(StackMode::ABOVE),
1958 + )?;
1959 + }
1960 +
1961 + let is_normie_layout = self.layout.name() == "normie";
1962 + if is_normie_layout && !is_transient && !is_dialog {
1963 + if let Ok(pointer) = self.connection.query_pointer(self.root)?.reply() {
1964 + let cursor_monitor = self.get_monitor_at_point(pointer.root_x as i32, pointer.root_y as i32)
1965 + .and_then(|idx| self.monitors.get(idx))
1966 + .unwrap_or(&monitor);
1967 +
1968 + let float_width = (cursor_monitor.width as f32 * 0.6) as u32;
1969 + let float_height = (cursor_monitor.height as f32 * 0.6) as u32;
1970 + let spawn_x = pointer.root_x as i32 - (float_width as i32 / 2);
1971 + let spawn_y = pointer.root_y as i32 - (float_height as i32 / 2);
1972 +
1973 + let clamped_x = spawn_x
1974 + .max(cursor_monitor.x)
1975 + .min(cursor_monitor.x + cursor_monitor.width as i32 - float_width as i32);
1976 + let clamped_y = spawn_y
1977 + .max(cursor_monitor.y)
1978 + .min(cursor_monitor.y + cursor_monitor.height as i32 - float_height as i32);
1979 +
1980 + self.connection.configure_window(
1981 + window,
1982 + &ConfigureWindowAux::new()
1983 + .x(clamped_x)
1984 + .y(clamped_y)
1985 + .width(float_width)
1986 + .height(float_height)
1987 + .border_width(border_width)
1988 + .stack_mode(StackMode::ABOVE),
1989 + )?;
1990 +
1991 + self.floating_windows.insert(window);
1992 + }
1993 + }
1994 +
1995 + self.set_wm_state(window, 1)?;
1996 + if let Err(error) = self.save_client_tag(window, window_tags) {
1997 + eprintln!("Failed to save client tag for new window: {:?}", error);
1998 + }
1999 +
2000 + self.apply_layout()?;
2001 + self.connection.map_window(window)?;
2002 + self.update_bar()?;
2003 + self.set_focus(window)?;
2004 +
2005 + if self.layout.name() == "tabbed" {
2006 + self.update_tab_bars()?;
2007 + }
1752 2008
1753 - transient_for.is_some() 2009 + Ok(())
1754 } 2010 }
1755 2011
1756 pub fn set_focus(&mut self, window: Window) -> WmResult<()> { 2012 pub fn set_focus(&mut self, window: Window) -> WmResult<()> {
@@ -2000,88 +2256,8 @@ impl WindowManager {
2000 return Ok(None); 2256 return Ok(None);
2001 } 2257 }
2002 2258
2003 - if self.windows.contains(&event.window) { 2259 + if !self.windows.contains(&event.window) {
2004 - return Ok(None); 2260 + self.manage_window(event.window)?;
2005 - }
2006 -
2007 - self.connection.map_window(event.window)?;
2008 - self.connection.change_window_attributes(
2009 - event.window,
2010 - &ChangeWindowAttributesAux::new().event_mask(EventMask::ENTER_WINDOW | EventMask::STRUCTURE_NOTIFY | EventMask::PROPERTY_CHANGE),
2011 - )?;
2012 -
2013 - let selected_tags = self
2014 - .monitors
2015 - .get(self.selected_monitor)
2016 - .map(|m| m.selected_tags)
2017 - .unwrap_or(tag_mask(0));
2018 -
2019 - self.windows.push(event.window);
2020 - self.window_tags.insert(event.window, selected_tags);
2021 - self.window_monitor
2022 - .insert(event.window, self.selected_monitor);
2023 - self.set_wm_state(event.window, 1)?;
2024 - if let Err(error) = self.save_client_tag(event.window, selected_tags) {
2025 - eprintln!("Failed to save client tag for new window: {:?}", error);
2026 - }
2027 -
2028 - let is_transient = self.is_transient_window(event.window);
2029 - if is_transient {
2030 - self.floating_windows.insert(event.window);
2031 - }
2032 -
2033 - let is_normie_layout = self.layout.name() == "normie";
2034 - if is_normie_layout && !is_transient {
2035 - let mut pointer_x: i32 = 0;
2036 - let mut pointer_y: i32 = 0;
2037 - let mut root_return: x11::xlib::Window = 0;
2038 - let mut child_return: x11::xlib::Window = 0;
2039 - let mut win_x: i32 = 0;
2040 - let mut win_y: i32 = 0;
2041 - let mut mask_return: u32 = 0;
2042 -
2043 - let pointer_queried = unsafe {
2044 - x11::xlib::XQueryPointer(
2045 - self.display,
2046 - self.root as x11::xlib::Window,
2047 - &mut root_return,
2048 - &mut child_return,
2049 - &mut pointer_x,
2050 - &mut pointer_y,
2051 - &mut win_x,
2052 - &mut win_y,
2053 - &mut mask_return,
2054 - ) != 0
2055 - };
2056 -
2057 - if pointer_queried {
2058 - let window_width = (self.screen.width_in_pixels as f32 * 0.6) as u32;
2059 - let window_height = (self.screen.height_in_pixels as f32 * 0.6) as u32;
2060 -
2061 - let spawn_x = pointer_x - (window_width as i32 / 2);
2062 - let spawn_y = pointer_y - (window_height as i32 / 2);
2063 -
2064 - self.connection.configure_window(
2065 - event.window,
2066 - &ConfigureWindowAux::new()
2067 - .x(spawn_x)
2068 - .y(spawn_y)
2069 - .width(window_width)
2070 - .height(window_height)
2071 - .border_width(self.config.border_width)
2072 - .stack_mode(StackMode::ABOVE),
2073 - )?;
2074 -
2075 - self.floating_windows.insert(event.window);
2076 - }
2077 - }
2078 -
2079 - self.apply_layout()?;
2080 - self.update_bar()?;
2081 - self.set_focus(event.window)?;
2082 -
2083 - if self.layout.name() == "tabbed" {
2084 - self.update_tab_bars()?;
2085 } 2261 }
2086 } 2262 }
2087 Event::UnmapNotify(event) => { 2263 Event::UnmapNotify(event) => {
@@ -2099,6 +2275,12 @@ impl WindowManager {
2099 return Ok(None); 2275 return Ok(None);
2100 } 2276 }
2101 if self.windows.contains(&event.event) { 2277 if self.windows.contains(&event.event) {
2278 + if let Some(&window_monitor_index) = self.window_monitor.get(&event.event) {
2279 + if window_monitor_index != self.selected_monitor {
2280 + self.selected_monitor = window_monitor_index;
2281 + self.update_bar()?;
2282 + }
2283 + }
2102 self.set_focus(event.event)?; 2284 self.set_focus(event.event)?;
2103 } 2285 }
2104 } 2286 }
@@ -2272,6 +2454,111 @@ impl WindowManager {
2272 } 2454 }
2273 } 2455 }
2274 } 2456 }
2457 + Event::ConfigureRequest(event) => {
2458 + if self.windows.contains(&event.window) {
2459 + let monitor_index = *self.window_monitor.get(&event.window).unwrap_or(&self.selected_monitor);
2460 + let monitor = &self.monitors[monitor_index];
2461 + let is_floating = self.floating_windows.contains(&event.window);
2462 + let is_tiling_layout = self.layout.name() != "normie";
2463 +
2464 + if is_floating || !is_tiling_layout {
2465 + let cached_geom = self.window_geometry_cache.get(&event.window);
2466 + let border_width = self.config.border_width as u16;
2467 +
2468 + let mut config = ConfigureWindowAux::new();
2469 + let value_mask = event.value_mask;
2470 +
2471 + if value_mask.contains(ConfigWindow::BORDER_WIDTH) {
2472 + config = config.border_width(event.border_width as u32);
2473 + }
2474 +
2475 + if value_mask.contains(ConfigWindow::X) {
2476 + let mut x = event.x as i32;
2477 + x = x.max(monitor.x);
2478 + if x + event.width as i32 + 2 * border_width as i32 > monitor.x + monitor.width as i32 {
2479 + x = monitor.x + monitor.width as i32 - event.width as i32 - 2 * border_width as i32;
2480 + }
2481 + config = config.x(x);
2482 + }
2483 +
2484 + if value_mask.contains(ConfigWindow::Y) {
2485 + let mut y = event.y as i32;
2486 + y = y.max(monitor.y);
2487 + if y + event.height as i32 + 2 * border_width as i32 > monitor.y + monitor.height as i32 {
2488 + y = monitor.y + monitor.height as i32 - event.height as i32 - 2 * border_width as i32;
2489 + }
2490 + config = config.y(y);
2491 + }
2492 +
2493 + if value_mask.contains(ConfigWindow::WIDTH) {
2494 + config = config.width(event.width as u32);
2495 + }
2496 +
2497 + if value_mask.contains(ConfigWindow::HEIGHT) {
2498 + config = config.height(event.height as u32);
2499 + }
2500 +
2501 + if value_mask.contains(ConfigWindow::SIBLING) {
2502 + config = config.sibling(event.sibling);
2503 + }
2504 +
2505 + if value_mask.contains(ConfigWindow::STACK_MODE) {
2506 + config = config.stack_mode(event.stack_mode);
2507 + }
2508 +
2509 + self.connection.configure_window(event.window, &config)?;
2510 +
2511 + let final_x = if value_mask.contains(ConfigWindow::X) {
2512 + let mut x = event.x as i32;
2513 + x = x.max(monitor.x);
2514 + if x + event.width as i32 + 2 * border_width as i32 > monitor.x + monitor.width as i32 {
2515 + x = monitor.x + monitor.width as i32 - event.width as i32 - 2 * border_width as i32;
2516 + }
2517 + x as i16
2518 + } else {
2519 + cached_geom.map(|g| g.x_position).unwrap_or(0)
2520 + };
2521 +
2522 + let final_y = if value_mask.contains(ConfigWindow::Y) {
2523 + let mut y = event.y as i32;
2524 + y = y.max(monitor.y);
2525 + if y + event.height as i32 + 2 * border_width as i32 > monitor.y + monitor.height as i32 {
2526 + y = monitor.y + monitor.height as i32 - event.height as i32 - 2 * border_width as i32;
2527 + }
2528 + y as i16
2529 + } else {
2530 + cached_geom.map(|g| g.y_position).unwrap_or(0)
2531 + };
2532 +
2533 + self.update_geometry_cache(event.window, CachedGeometry {
2534 + x_position: final_x,
2535 + y_position: final_y,
2536 + width: if value_mask.contains(ConfigWindow::WIDTH) { event.width } else { cached_geom.map(|g| g.width).unwrap_or(1) },
2537 + height: if value_mask.contains(ConfigWindow::HEIGHT) { event.height } else { cached_geom.map(|g| g.height).unwrap_or(1) },
2538 + border_width: if value_mask.contains(ConfigWindow::BORDER_WIDTH) { event.border_width } else { border_width },
2539 + });
2540 + } else {
2541 + self.send_configure_notify(event.window)?;
2542 + }
2543 + } else {
2544 + let mut config = ConfigureWindowAux::new()
2545 + .x(event.x as i32)
2546 + .y(event.y as i32)
2547 + .width(event.width as u32)
2548 + .height(event.height as u32)
2549 + .border_width(event.border_width as u32);
2550 +
2551 + if event.value_mask.contains(ConfigWindow::SIBLING) {
2552 + config = config.sibling(event.sibling);
2553 + }
2554 +
2555 + if event.value_mask.contains(ConfigWindow::STACK_MODE) {
2556 + config = config.stack_mode(event.stack_mode);
2557 + }
2558 +
2559 + self.connection.configure_window(event.window, &config)?;
2560 + }
2561 + }
2275 Event::ClientMessage(event) => { 2562 Event::ClientMessage(event) => {
2276 if event.type_ == self.atoms.net_wm_state { 2563 if event.type_ == self.atoms.net_wm_state {
2277 if let Some(data) = event.data.as_data32().get(1) { 2564 if let Some(data) = event.data.as_data32().get(1) {
@@ -2498,6 +2785,33 @@ impl WindowManager {
2498 Ok(cached) 2785 Ok(cached)
2499 } 2786 }
2500 2787
2788 + fn send_configure_notify(&mut self, window: Window) -> WmResult<()> {
2789 + let geometry = self.get_or_query_geometry(window)?;
2790 +
2791 + let event = x11rb::protocol::xproto::ConfigureNotifyEvent {
2792 + response_type: x11rb::protocol::xproto::CONFIGURE_NOTIFY_EVENT,
2793 + sequence: 0,
2794 + event: window,
2795 + window,
2796 + above_sibling: x11rb::NONE,
2797 + x: geometry.x_position,
2798 + y: geometry.y_position,
2799 + width: geometry.width,
2800 + height: geometry.height,
2801 + border_width: geometry.border_width,
2802 + override_redirect: false,
2803 + };
2804 +
2805 + self.connection.send_event(
2806 + false,
2807 + window,
2808 + x11rb::protocol::xproto::EventMask::STRUCTURE_NOTIFY,
2809 + event,
2810 + )?;
2811 +
2812 + Ok(())
2813 + }
2814 +
2501 fn remove_window(&mut self, window: Window) -> WmResult<()> { 2815 fn remove_window(&mut self, window: Window) -> WmResult<()> {
2502 let initial_count = self.windows.len(); 2816 let initial_count = self.windows.len();
2503 2817