tony / oxwm

Added logic to move windows from monitors with a keybind, and toggle monitors with a keybind. removed all references to old hasmap window list Browse files
tonybtw committed 2025-12-01 parent 5767bfd commit 6c1e59a8a7eff1c0cdfb68232815a46b2047e59d
Showing 5 changed files with 229 additions and 86 deletions. UnifiedSplit
+11 −7 resources/test-config.lua
@@ -77,13 +77,17 @@ oxwm.key.bind({ modkey }, "N", oxwm.layout.cycle())
77 77
78 78 oxwm.key.bind({ modkey }, "A", oxwm.toggle_gaps())
79 79
80 --- Master area controls (dwm-style)
81 --- NOTE: Mod+H/L are currently used for directional focus
82 --- Using Comma/Period instead. To match dwm exactly, replace H/L bindings above
83 -oxwm.key.bind({ modkey }, "Comma", oxwm.set_master_factor(-5)) -- Decrease master area
84 -oxwm.key.bind({ modkey }, "Period", oxwm.set_master_factor(5)) -- Increase master area
85 -oxwm.key.bind({ modkey }, "I", oxwm.inc_num_master(1)) -- More master windows
86 -oxwm.key.bind({ modkey }, "P", oxwm.inc_num_master(-1)) -- Fewer master windows
80 +-- Master area controls
81 +oxwm.key.bind({ modkey }, "BracketLeft", oxwm.set_master_factor(-5)) -- Decrease master area
82 +oxwm.key.bind({ modkey }, "BracketRight", oxwm.set_master_factor(5)) -- Increase master area
83 +oxwm.key.bind({ modkey }, "I", oxwm.inc_num_master(1)) -- More master windows
84 +oxwm.key.bind({ modkey }, "P", oxwm.inc_num_master(-1)) -- Fewer master windows
85 +
86 +-- Multi-monitor controls (dwm-style)
87 +oxwm.key.bind({ modkey }, "Comma", oxwm.monitor.focus(-1)) -- Focus previous monitor
88 +oxwm.key.bind({ modkey }, "Period", oxwm.monitor.focus(1)) -- Focus next monitor
89 +oxwm.key.bind({ modkey, "Shift" }, "Comma", oxwm.monitor.tag(-1)) -- Send window to previous monitor
90 +oxwm.key.bind({ modkey, "Shift" }, "Period", oxwm.monitor.tag(1)) -- Send window to next monitor
87 91
88 92 oxwm.key.bind({ modkey, "Shift" }, "Q", oxwm.quit())
89 93 oxwm.key.bind({ modkey, "Shift" }, "R", oxwm.restart())
+19 −0 src/config/lua_api.rs
@@ -88,6 +88,7 @@ pub fn register_api(lua: &Lua) -> Result<SharedBuilder, ConfigError> {
88 88 register_client_module(&lua, &oxwm_table)?;
89 89 register_layout_module(&lua, &oxwm_table)?;
90 90 register_tag_module(&lua, &oxwm_table)?;
91 + register_monitor_module(&lua, &oxwm_table)?;
91 92 register_rule_module(&lua, &oxwm_table, builder.clone())?;
92 93 register_bar_module(&lua, &oxwm_table, builder.clone())?;
93 94 register_misc(&lua, &oxwm_table, builder.clone())?;
@@ -324,6 +325,23 @@ fn register_tag_module(lua: &Lua, parent: &Table) -> Result<(), ConfigError> {
324 325 Ok(())
325 326 }
326 327
328 +fn register_monitor_module(lua: &Lua, parent: &Table) -> Result<(), ConfigError> {
329 + let monitor_table = lua.create_table()?;
330 +
331 + let focus = lua.create_function(|lua, direction: i64| {
332 + create_action_table(lua, "FocusMonitor", Value::Integer(direction))
333 + })?;
334 +
335 + let tag = lua.create_function(|lua, direction: i64| {
336 + create_action_table(lua, "TagMonitor", Value::Integer(direction))
337 + })?;
338 +
339 + monitor_table.set("focus", focus)?;
340 + monitor_table.set("tag", tag)?;
341 + parent.set("monitor", monitor_table)?;
342 + Ok(())
343 +}
344 +
327 345 fn register_rule_module(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<(), ConfigError> {
328 346 let rule_table = lua.create_table()?;
329 347
@@ -789,6 +807,7 @@ fn string_to_action(s: &str) -> mlua::Result<KeyAction> {
789 807 "ChangeLayout" => Ok(KeyAction::ChangeLayout),
790 808 "CycleLayout" => Ok(KeyAction::CycleLayout),
791 809 "FocusMonitor" => Ok(KeyAction::FocusMonitor),
810 + "TagMonitor" => Ok(KeyAction::TagMonitor),
792 811 "ExchangeClient" => Ok(KeyAction::ExchangeClient),
793 812 "ShowKeybindOverlay" => Ok(KeyAction::ShowKeybindOverlay),
794 813 _ => Err(mlua::Error::RuntimeError(format!("unknown action '{}'. this is an internal error, please report it", s))),
+1 −0 src/keyboard/handlers.rs
@@ -30,6 +30,7 @@ pub enum KeyAction {
30 30 ChangeLayout,
31 31 CycleLayout,
32 32 FocusMonitor,
33 + TagMonitor,
33 34 ExchangeClient,
34 35 ShowKeybindOverlay,
35 36 SetMasterFactor,
+1 −0 src/overlay/keybind.rs
@@ -221,6 +221,7 @@ impl KeybindOverlay {
221 221 KeyAction::ChangeLayout => "Change Layout".to_string(),
222 222 KeyAction::CycleLayout => "Cycle Through Layouts".to_string(),
223 223 KeyAction::FocusMonitor => "Focus Next Monitor".to_string(),
224 + KeyAction::TagMonitor => "Send Window to Monitor".to_string(),
224 225 KeyAction::ExchangeClient => "Exchange Client Windows".to_string(),
225 226 KeyAction::SetMasterFactor => "Adjust Master Area Size".to_string(),
226 227 KeyAction::IncNumMaster => "Adjust Number of Master Windows".to_string(),
+197 −79 src/window_manager.rs
@@ -743,9 +743,9 @@ impl WindowManager {
743 743 for (monitor_index, monitor) in self.monitors.iter().enumerate() {
744 744 if let Some(bar) = self.bars.get_mut(monitor_index) {
745 745 let mut occupied_tags: TagMask = 0;
746 - for (&window, &tags) in &self.window_tags {
747 - if self.window_monitor.get(&window).copied().unwrap_or(0) == monitor_index {
748 - occupied_tags |= tags;
746 + for client in self.clients.values() {
747 + if client.monitor_index == monitor_index {
748 + occupied_tags |= client.tags;
749 749 }
750 750 }
751 751
@@ -773,15 +773,14 @@ impl WindowManager {
773 773 .windows
774 774 .iter()
775 775 .filter(|&&window| {
776 - let window_monitor_index = self.window_monitor.get(&window).copied().unwrap_or(0);
777 - if window_monitor_index != monitor_index
778 - || self.floating_windows.contains(&window)
779 - || self.fullscreen_windows.contains(&window)
780 - {
781 - return false;
782 - }
783 - if let Some(&tags) = self.window_tags.get(&window) {
784 - (tags & monitor.tagset[monitor.selected_tags_index]) != 0
776 + if let Some(client) = self.clients.get(&window) {
777 + if client.monitor_index != monitor_index
778 + || self.floating_windows.contains(&window)
779 + || self.fullscreen_windows.contains(&window)
780 + {
781 + return false;
782 + }
783 + (client.tags & monitor.tagset[monitor.selected_tags_index]) != 0
785 784 } else {
786 785 false
787 786 }
@@ -925,6 +924,11 @@ impl WindowManager {
925 924 self.focus_monitor(*direction)?;
926 925 }
927 926 }
927 + KeyAction::TagMonitor => {
928 + if let Arg::Int(direction) = arg {
929 + self.tag_monitor(*direction)?;
930 + }
931 + }
928 932 KeyAction::ShowKeybindOverlay => {
929 933 let monitor = &self.monitors[self.selected_monitor];
930 934 self.keybind_overlay.toggle(
@@ -950,40 +954,12 @@ impl WindowManager {
950 954 Ok(())
951 955 }
952 956
953 - fn focus_monitor(&mut self, direction: i32) -> WmResult<()> {
954 - if self.monitors.is_empty() {
955 - return Ok(());
956 - }
957 -
958 - let new_monitor = if direction > 0 {
959 - (self.selected_monitor + 1) % self.monitors.len()
960 - } else {
961 - (self.selected_monitor + self.monitors.len() - 1) % self.monitors.len()
962 - };
963 -
964 - if new_monitor == self.selected_monitor {
965 - return Ok(());
966 - }
967 -
968 - self.selected_monitor = new_monitor;
969 -
970 - self.update_bar()?;
971 -
972 - let visible = self.visible_windows_on_monitor(new_monitor);
973 - if let Some(&win) = visible.first() {
974 - self.focus(Some(win))?;
975 - }
976 -
977 - Ok(())
978 - }
979 957
980 958 fn is_window_visible(&self, window: Window) -> bool {
981 - let window_mon = self.window_monitor.get(&window).copied().unwrap_or(0);
982 -
983 - if let Some(&tags) = self.window_tags.get(&window) {
984 - let monitor = self.monitors.get(window_mon);
959 + if let Some(client) = self.clients.get(&window) {
960 + let monitor = self.monitors.get(client.monitor_index);
985 961 let selected_tags = monitor.map(|m| m.tagset[m.selected_tags_index]).unwrap_or(0);
986 - (tags & selected_tags) != 0
962 + (client.tags & selected_tags) != 0
987 963 } else {
988 964 false
989 965 }
@@ -1033,6 +1009,44 @@ impl WindowManager {
1033 1009 .position(|mon| mon.contains_point(x, y))
1034 1010 }
1035 1011
1012 + fn rect_to_monitor(&self, x: i32, y: i32, w: i32, h: i32) -> usize {
1013 + let mut best_monitor = self.selected_monitor;
1014 + let mut max_area = 0;
1015 +
1016 + for (idx, monitor) in self.monitors.iter().enumerate() {
1017 + let intersect_width = 0.max((x + w).min(monitor.window_area_x + monitor.window_area_width) - x.max(monitor.window_area_x));
1018 + let intersect_height = 0.max((y + h).min(monitor.window_area_y + monitor.window_area_height) - y.max(monitor.window_area_y));
1019 + let area = intersect_width * intersect_height;
1020 +
1021 + if area > max_area {
1022 + max_area = area;
1023 + best_monitor = idx;
1024 + }
1025 + }
1026 +
1027 + best_monitor
1028 + }
1029 +
1030 + fn dir_to_monitor(&self, direction: i32) -> Option<usize> {
1031 + if self.monitors.len() <= 1 {
1032 + return None;
1033 + }
1034 +
1035 + if direction > 0 {
1036 + if self.selected_monitor + 1 < self.monitors.len() {
1037 + Some(self.selected_monitor + 1)
1038 + } else {
1039 + Some(0)
1040 + }
1041 + } else {
1042 + if self.selected_monitor == 0 {
1043 + Some(self.monitors.len() - 1)
1044 + } else {
1045 + Some(self.selected_monitor - 1)
1046 + }
1047 + }
1048 + }
1049 +
1036 1050 // Dwm's g-loaded approach to handling the spam alternating crash.
1037 1051 fn update_window_visibility(&self) -> WmResult<()> {
1038 1052 for &window in &self.windows {
@@ -1579,7 +1593,9 @@ impl WindowManager {
1579 1593 .collect();
1580 1594
1581 1595 for window in floating_windows {
1582 - let monitor_idx = *self.window_monitor.get(&window).unwrap_or(&self.selected_monitor);
1596 + let monitor_idx = self.clients.get(&window)
1597 + .map(|c| c.monitor_index)
1598 + .unwrap_or(self.selected_monitor);
1583 1599 let monitor = &self.monitors[monitor_idx];
1584 1600
1585 1601 let (outer_gap_h, outer_gap_v) = if self.gaps_enabled {
@@ -1679,7 +1695,9 @@ impl WindowManager {
1679 1695 client.is_fullscreen = true;
1680 1696 }
1681 1697
1682 - let monitor_idx = *self.window_monitor.get(&window).unwrap_or(&self.selected_monitor);
1698 + let monitor_idx = self.clients.get(&window)
1699 + .map(|c| c.monitor_index)
1700 + .unwrap_or(self.selected_monitor);
1683 1701 let monitor = &self.monitors[monitor_idx];
1684 1702
1685 1703 if self.show_bar {
@@ -1723,7 +1741,9 @@ impl WindowManager {
1723 1741 .border_width(self.config.border_width),
1724 1742 )?;
1725 1743
1726 - let monitor_idx = *self.window_monitor.get(&window).unwrap_or(&self.selected_monitor);
1744 + let monitor_idx = self.clients.get(&window)
1745 + .map(|c| c.monitor_index)
1746 + .unwrap_or(self.selected_monitor);
1727 1747 if self.show_bar {
1728 1748 if let Some(bar) = self.bars.get(monitor_idx) {
1729 1749 self.connection.map_window(bar.window())?;
@@ -1906,14 +1926,15 @@ impl WindowManager {
1906 1926
1907 1927 let transient_parent = self.get_transient_parent(window);
1908 1928 let (window_tags, monitor_index) = if let Some(parent) = transient_parent {
1909 - let parent_tags = self.window_tags.get(&parent).copied().unwrap_or_else(|| {
1910 - self.monitors
1929 + if let Some(parent_client) = self.clients.get(&parent) {
1930 + (parent_client.tags, parent_client.monitor_index)
1931 + } else {
1932 + let tags = self.monitors
1911 1933 .get(self.selected_monitor)
1912 1934 .map(|m| m.tagset[m.selected_tags_index])
1913 - .unwrap_or(tag_mask(0))
1914 - });
1915 - let parent_monitor = self.window_monitor.get(&parent).copied().unwrap_or(self.selected_monitor);
1916 - (parent_tags, parent_monitor)
1935 + .unwrap_or(tag_mask(0));
1936 + (tags, self.selected_monitor)
1937 + }
1917 1938 } else {
1918 1939 let selected_tags = self.monitors
1919 1940 .get(self.selected_monitor)
@@ -2190,7 +2211,9 @@ impl WindowManager {
2190 2211 return Ok(());
2191 2212 }
2192 2213
2193 - let monitor_idx = *self.window_monitor.get(&win).unwrap_or(&self.selected_monitor);
2214 + let monitor_idx = self.clients.get(&win)
2215 + .map(|c| c.monitor_index)
2216 + .unwrap_or(self.selected_monitor);
2194 2217 if monitor_idx != self.selected_monitor {
2195 2218 self.selected_monitor = monitor_idx;
2196 2219 }
@@ -2340,6 +2363,54 @@ impl WindowManager {
2340 2363 Ok(())
2341 2364 }
2342 2365
2366 + pub fn focus_monitor(&mut self, direction: i32) -> WmResult<()> {
2367 + if self.monitors.len() <= 1 {
2368 + return Ok(());
2369 + }
2370 +
2371 + let target_monitor = match self.dir_to_monitor(direction) {
2372 + Some(idx) if idx != self.selected_monitor => idx,
2373 + _ => return Ok(()),
2374 + };
2375 +
2376 + let old_selected = self.monitors
2377 + .get(self.selected_monitor)
2378 + .and_then(|m| m.selected_client);
2379 +
2380 + if let Some(win) = old_selected {
2381 + self.unfocus(win)?;
2382 + }
2383 +
2384 + self.selected_monitor = target_monitor;
2385 + self.focus(None)?;
2386 +
2387 + Ok(())
2388 + }
2389 +
2390 + pub fn tag_monitor(&mut self, direction: i32) -> WmResult<()> {
2391 + if self.monitors.len() <= 1 {
2392 + return Ok(());
2393 + }
2394 +
2395 + let selected_window = self.monitors
2396 + .get(self.selected_monitor)
2397 + .and_then(|m| m.selected_client);
2398 +
2399 + let window = match selected_window {
2400 + Some(win) => win,
2401 + None => return Ok(()),
2402 + };
2403 +
2404 + let target_monitor = match self.dir_to_monitor(direction) {
2405 + Some(idx) => idx,
2406 + None => return Ok(()),
2407 + };
2408 +
2409 + self.send_to_monitor(window, target_monitor)?;
2410 +
2411 + Ok(())
2412 + }
2413 +
2343 2414 fn update_focus_visuals(
2344 2415 &self,
2345 2416 old_focused: Option<Window>,
@@ -2599,9 +2670,9 @@ impl WindowManager {
2599 2670 return Ok(None);
2600 2671 }
2601 2672 if self.windows.contains(&event.event) {
2602 - if let Some(&window_monitor_index) = self.window_monitor.get(&event.event) {
2603 - if window_monitor_index != self.selected_monitor {
2604 - self.selected_monitor = window_monitor_index;
2673 + if let Some(client) = self.clients.get(&event.event) {
2674 + if client.monitor_index != self.selected_monitor {
2675 + self.selected_monitor = client.monitor_index;
2605 2676 self.update_bar()?;
2606 2677 }
2607 2678 }
@@ -2727,16 +2798,15 @@ impl WindowManager {
2727 2798 .windows
2728 2799 .iter()
2729 2800 .filter(|&&window| {
2730 - let window_monitor_index = self.window_monitor.get(&window).copied().unwrap_or(0);
2731 - if window_monitor_index != monitor_index
2732 - || self.floating_windows.contains(&window)
2733 - || self.fullscreen_windows.contains(&window)
2734 - {
2735 - return false;
2736 - }
2737 - let monitor_tags = self.monitors.get(monitor_index).map(|m| m.tagset[m.selected_tags_index]).unwrap_or(0);
2738 - if let Some(&tags) = self.window_tags.get(&window) {
2739 - (tags & monitor_tags) != 0
2801 + if let Some(client) = self.clients.get(&window) {
2802 + if client.monitor_index != monitor_index
2803 + || self.floating_windows.contains(&window)
2804 + || self.fullscreen_windows.contains(&window)
2805 + {
2806 + return false;
2807 + }
2808 + let monitor_tags = self.monitors.get(monitor_index).map(|m| m.tagset[m.selected_tags_index]).unwrap_or(0);
2809 + (client.tags & monitor_tags) != 0
2740 2810 } else {
2741 2811 false
2742 2812 }
@@ -2780,7 +2850,9 @@ impl WindowManager {
2780 2850 }
2781 2851 Event::ConfigureRequest(event) => {
2782 2852 if self.windows.contains(&event.window) {
2783 - let monitor_index = *self.window_monitor.get(&event.window).unwrap_or(&self.selected_monitor);
2853 + let monitor_index = self.clients.get(&event.window)
2854 + .map(|c| c.monitor_index)
2855 + .unwrap_or(self.selected_monitor);
2784 2856 let monitor = &self.monitors[monitor_index];
2785 2857 let is_floating = self.floating_windows.contains(&event.window);
2786 2858 let is_tiling_layout = self.layout.name() != "normie";
@@ -2854,13 +2926,24 @@ impl WindowManager {
2854 2926 cached_geom.map(|g| g.y_position).unwrap_or(0)
2855 2927 };
2856 2928
2929 + let final_width = if value_mask.contains(ConfigWindow::WIDTH) { event.width } else { cached_geom.map(|g| g.width).unwrap_or(1) };
2930 + let final_height = if value_mask.contains(ConfigWindow::HEIGHT) { event.height } else { cached_geom.map(|g| g.height).unwrap_or(1) };
2931 +
2857 2932 self.update_geometry_cache(event.window, CachedGeometry {
2858 2933 x_position: final_x,
2859 2934 y_position: final_y,
2860 - width: if value_mask.contains(ConfigWindow::WIDTH) { event.width } else { cached_geom.map(|g| g.width).unwrap_or(1) },
2861 - height: if value_mask.contains(ConfigWindow::HEIGHT) { event.height } else { cached_geom.map(|g| g.height).unwrap_or(1) },
2935 + width: final_width,
2936 + height: final_height,
2862 2937 border_width: if value_mask.contains(ConfigWindow::BORDER_WIDTH) { event.border_width } else { border_width },
2863 2938 });
2939 +
2940 + if is_floating {
2941 + let new_monitor = self.rect_to_monitor(final_x as i32, final_y as i32, final_width as i32, final_height as i32);
2942 +
2943 + if new_monitor != monitor_index {
2944 + self.send_to_monitor(event.window, new_monitor)?;
2945 + }
2946 + }
2864 2947 } else {
2865 2948 self.send_configure_notify(event.window)?;
2866 2949 }
@@ -3068,16 +3151,15 @@ impl WindowManager {
3068 3151 .windows
3069 3152 .iter()
3070 3153 .any(|&window| {
3071 - let window_monitor_index = self.window_monitor.get(&window).copied().unwrap_or(0);
3072 - if window_monitor_index != monitor_index
3073 - || self.floating_windows.contains(&window)
3074 - || self.fullscreen_windows.contains(&window)
3075 - {
3076 - return false;
3077 - }
3078 - if let Some(monitor) = self.monitors.get(monitor_index) {
3079 - if let Some(&tags) = self.window_tags.get(&window) {
3080 - return (tags & monitor.tagset[monitor.selected_tags_index]) != 0;
3154 + if let Some(client) = self.clients.get(&window) {
3155 + if client.monitor_index != monitor_index
3156 + || self.floating_windows.contains(&window)
3157 + || self.fullscreen_windows.contains(&window)
3158 + {
3159 + return false;
3160 + }
3161 + if let Some(monitor) = self.monitors.get(monitor_index) {
3162 + return (client.tags & monitor.tagset[monitor.selected_tags_index]) != 0;
3081 3163 }
3082 3164 }
3083 3165 false
@@ -3495,6 +3577,42 @@ impl WindowManager {
3495 3577 }
3496 3578 }
3497 3579
3580 + fn send_to_monitor(&mut self, window: Window, target_monitor: usize) -> WmResult<()> {
3581 + if target_monitor >= self.monitors.len() {
3582 + return Ok(());
3583 + }
3584 +
3585 + let current_monitor = self.clients.get(&window).map(|c| c.monitor_index);
3586 + if current_monitor == Some(target_monitor) {
3587 + return Ok(());
3588 + }
3589 +
3590 + self.unfocus(window)?;
3591 +
3592 + self.detach(window);
3593 + self.detach_stack(window);
3594 +
3595 + if let Some(client) = self.clients.get_mut(&window) {
3596 + client.monitor_index = target_monitor;
3597 + let new_tags = self.monitors
3598 + .get(target_monitor)
3599 + .map(|m| m.tagset[m.selected_tags_index])
3600 + .unwrap_or(1);
3601 + client.tags = new_tags;
3602 + self.window_tags.insert(window, new_tags);
3603 + }
3604 +
3605 + self.window_monitor.insert(window, target_monitor);
3606 +
3607 + self.attach_aside(window, target_monitor);
3608 + self.attach_stack(window, target_monitor);
3609 +
3610 + self.focus(None)?;
3611 + self.apply_layout()?;
3612 +
3613 + Ok(())
3614 + }
3615 +
3498 3616 fn remove_window(&mut self, window: Window) -> WmResult<()> {
3499 3617 let initial_count = self.windows.len();
3500 3618