tony / oxwm

Added smartgaps, mfact, and nmaster (inc master, dec master) functionality from dwm. Default to attach-aside patch. Browse files
tonybtw committed 2025-12-01 parent b045fc8 commit 16b90b9ce0f36ef5bfe41dc4f77630c325b681d3
Showing 11 changed files with 253 additions and 48 deletions. UnifiedSplit
+8 −0 resources/test-config.lua
@@ -72,6 +72,14 @@ oxwm.key.bind({ modkey }, "N", oxwm.layout.cycle())
72 72
73 oxwm.key.bind({ modkey }, "A", oxwm.toggle_gaps()) 73 oxwm.key.bind({ modkey }, "A", oxwm.toggle_gaps())
74 74
75 +-- Master area controls (dwm-style)
76 +-- NOTE: Mod+H/L are currently used for directional focus
77 +-- Using Comma/Period instead. To match dwm exactly, replace H/L bindings above
78 +oxwm.key.bind({ modkey }, "Comma", oxwm.set_master_factor(-5)) -- Decrease master area
79 +oxwm.key.bind({ modkey }, "Period", oxwm.set_master_factor(5)) -- Increase master area
80 +oxwm.key.bind({ modkey }, "I", oxwm.inc_num_master(1)) -- More master windows
81 +oxwm.key.bind({ modkey }, "P", oxwm.inc_num_master(-1)) -- Fewer master windows
82 +
75 oxwm.key.bind({ modkey, "Shift" }, "Q", oxwm.quit()) 83 oxwm.key.bind({ modkey, "Shift" }, "Q", oxwm.quit())
76 oxwm.key.bind({ modkey, "Shift" }, "R", oxwm.restart()) 84 oxwm.key.bind({ modkey, "Shift" }, "R", oxwm.restart())
77 85
+12 −0 src/config/lua_api.rs
@@ -588,6 +588,14 @@ fn register_misc(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<()
588 create_action_table(lua, "ToggleGaps", Value::Nil) 588 create_action_table(lua, "ToggleGaps", Value::Nil)
589 })?; 589 })?;
590 590
591 + let set_master_factor = lua.create_function(|lua, delta: i32| {
592 + create_action_table(lua, "SetMasterFactor", Value::Integer(delta as i64))
593 + })?;
594 +
595 + let inc_num_master = lua.create_function(|lua, delta: i32| {
596 + create_action_table(lua, "IncNumMaster", Value::Integer(delta as i64))
597 + })?;
598 +
591 let show_keybinds = lua.create_function(|lua, ()| { 599 let show_keybinds = lua.create_function(|lua, ()| {
592 create_action_table(lua, "ShowKeybindOverlay", Value::Nil) 600 create_action_table(lua, "ShowKeybindOverlay", Value::Nil)
593 })?; 601 })?;
@@ -620,6 +628,8 @@ fn register_misc(lua: &Lua, parent: &Table, builder: SharedBuilder) -> Result<()
620 parent.set("restart", restart)?; 628 parent.set("restart", restart)?;
621 parent.set("recompile", recompile)?; 629 parent.set("recompile", recompile)?;
622 parent.set("toggle_gaps", toggle_gaps)?; 630 parent.set("toggle_gaps", toggle_gaps)?;
631 + parent.set("set_master_factor", set_master_factor)?;
632 + parent.set("inc_num_master", inc_num_master)?;
623 parent.set("show_keybinds", show_keybinds)?; 633 parent.set("show_keybinds", show_keybinds)?;
624 parent.set("focus_monitor", focus_monitor)?; 634 parent.set("focus_monitor", focus_monitor)?;
625 Ok(()) 635 Ok(())
@@ -708,6 +718,8 @@ fn string_to_action(s: &str) -> mlua::Result<KeyAction> {
708 "Recompile" => Ok(KeyAction::Recompile), 718 "Recompile" => Ok(KeyAction::Recompile),
709 "ViewTag" => Ok(KeyAction::ViewTag), 719 "ViewTag" => Ok(KeyAction::ViewTag),
710 "ToggleGaps" => Ok(KeyAction::ToggleGaps), 720 "ToggleGaps" => Ok(KeyAction::ToggleGaps),
721 + "SetMasterFactor" => Ok(KeyAction::SetMasterFactor),
722 + "IncNumMaster" => Ok(KeyAction::IncNumMaster),
711 "ToggleFullScreen" => Ok(KeyAction::ToggleFullScreen), 723 "ToggleFullScreen" => Ok(KeyAction::ToggleFullScreen),
712 "ToggleFloating" => Ok(KeyAction::ToggleFloating), 724 "ToggleFloating" => Ok(KeyAction::ToggleFloating),
713 "ChangeLayout" => Ok(KeyAction::ChangeLayout), 725 "ChangeLayout" => Ok(KeyAction::ChangeLayout),
+2 −0 src/keyboard/handlers.rs
@@ -30,6 +30,8 @@ pub enum KeyAction {
30 FocusMonitor, 30 FocusMonitor,
31 ExchangeClient, 31 ExchangeClient,
32 ShowKeybindOverlay, 32 ShowKeybindOverlay,
33 + SetMasterFactor,
34 + IncNumMaster,
33 None, 35 None,
34 } 36 }
35 37
+2 −0 src/layout/grid.rs
@@ -18,6 +18,8 @@ impl Layout for GridLayout {
18 screen_width: u32, 18 screen_width: u32,
19 screen_height: u32, 19 screen_height: u32,
20 gaps: &GapConfig, 20 gaps: &GapConfig,
21 + _master_factor: f32,
22 + _num_master: i32,
21 ) -> Vec<WindowGeometry> { 23 ) -> Vec<WindowGeometry> {
22 let window_count = windows.len(); 24 let window_count = windows.len();
23 if window_count == 0 { 25 if window_count == 0 {
+2 −0 src/layout/mod.rs
@@ -86,6 +86,8 @@ pub trait Layout {
86 screen_width: u32, 86 screen_width: u32,
87 screen_height: u32, 87 screen_height: u32,
88 gaps: &GapConfig, 88 gaps: &GapConfig,
89 + master_factor: f32,
90 + num_master: i32,
89 ) -> Vec<WindowGeometry>; 91 ) -> Vec<WindowGeometry>;
90 fn name(&self) -> &'static str; 92 fn name(&self) -> &'static str;
91 fn symbol(&self) -> &'static str; 93 fn symbol(&self) -> &'static str;
+2 −0 src/layout/monocle.rs
@@ -18,6 +18,8 @@ impl Layout for MonocleLayout {
18 screen_width: u32, 18 screen_width: u32,
19 screen_height: u32, 19 screen_height: u32,
20 gaps: &GapConfig, 20 gaps: &GapConfig,
21 + _master_factor: f32,
22 + _num_master: i32,
21 ) -> Vec<WindowGeometry> { 23 ) -> Vec<WindowGeometry> {
22 let window_count = windows.len(); 24 let window_count = windows.len();
23 if window_count == 0 { 25 if window_count == 0 {
+2 −0 src/layout/normie.rs
@@ -19,6 +19,8 @@ impl Layout for NormieLayout {
19 screen_width: u32, 19 screen_width: u32,
20 screen_height: u32, 20 screen_height: u32,
21 _gaps: &GapConfig, 21 _gaps: &GapConfig,
22 + _master_factor: f32,
23 + _num_master: i32,
22 ) -> Vec<WindowGeometry> { 24 ) -> Vec<WindowGeometry> {
23 const DEFAULT_WIDTH_RATIO: f32 = 0.6; 25 const DEFAULT_WIDTH_RATIO: f32 = 0.6;
24 const DEFAULT_HEIGHT_RATIO: f32 = 0.6; 26 const DEFAULT_HEIGHT_RATIO: f32 = 0.6;
+2 −0 src/layout/tabbed.rs
@@ -20,6 +20,8 @@ impl Layout for TabbedLayout {
20 screen_width: u32, 20 screen_width: u32,
21 screen_height: u32, 21 screen_height: u32,
22 gaps: &GapConfig, 22 gaps: &GapConfig,
23 + _master_factor: f32,
24 + _num_master: i32,
23 ) -> Vec<WindowGeometry> { 25 ) -> Vec<WindowGeometry> {
24 let window_count = windows.len(); 26 let window_count = windows.len();
25 if window_count == 0 { 27 if window_count == 0 {
+136 −43 src/layout/tiling.rs
@@ -3,6 +3,73 @@ use x11rb::protocol::xproto::Window;
3 3
4 pub struct TilingLayout; 4 pub struct TilingLayout;
5 5
6 +struct GapValues {
7 + outer_horizontal: u32,
8 + outer_vertical: u32,
9 + inner_horizontal: u32,
10 + inner_vertical: u32,
11 +}
12 +
13 +struct FactValues {
14 + master_facts: f32,
15 + stack_facts: f32,
16 + master_remainder: i32,
17 + stack_remainder: i32,
18 +}
19 +
20 +impl TilingLayout {
21 + fn getgaps(gaps: &GapConfig, window_count: usize, smartgaps_enabled: bool) -> GapValues {
22 + let outer_enabled = if smartgaps_enabled && window_count == 1 {
23 + 0
24 + } else {
25 + 1
26 + };
27 + let inner_enabled = 1;
28 +
29 + GapValues {
30 + outer_horizontal: gaps.outer_horizontal * outer_enabled,
31 + outer_vertical: gaps.outer_vertical * outer_enabled,
32 + inner_horizontal: gaps.inner_horizontal * inner_enabled,
33 + inner_vertical: gaps.inner_vertical * inner_enabled,
34 + }
35 + }
36 +
37 + fn getfacts(
38 + window_count: usize,
39 + num_master: i32,
40 + master_size: i32,
41 + stack_size: i32,
42 + ) -> FactValues {
43 + let num_master = num_master.max(0) as usize;
44 + let master_facts = window_count.min(num_master) as f32;
45 + let stack_facts = if window_count > num_master {
46 + (window_count - num_master) as f32
47 + } else {
48 + 0.0
49 + };
50 +
51 + let mut master_total = 0;
52 + let mut stack_total = 0;
53 +
54 + for i in 0..window_count {
55 + if i < num_master {
56 + master_total += (master_size as f32 / master_facts) as i32;
57 + } else {
58 + if stack_facts > 0.0 {
59 + stack_total += (stack_size as f32 / stack_facts) as i32;
60 + }
61 + }
62 + }
63 +
64 + FactValues {
65 + master_facts,
66 + stack_facts,
67 + master_remainder: master_size - master_total,
68 + stack_remainder: stack_size - stack_total,
69 + }
70 + }
71 +}
72 +
6 impl Layout for TilingLayout { 73 impl Layout for TilingLayout {
7 fn name(&self) -> &'static str { 74 fn name(&self) -> &'static str {
8 super::LayoutType::Tiling.as_str() 75 super::LayoutType::Tiling.as_str()
@@ -18,68 +85,94 @@ impl Layout for TilingLayout {
18 screen_width: u32, 85 screen_width: u32,
19 screen_height: u32, 86 screen_height: u32,
20 gaps: &GapConfig, 87 gaps: &GapConfig,
88 + master_factor: f32,
89 + num_master: i32,
21 ) -> Vec<WindowGeometry> { 90 ) -> Vec<WindowGeometry> {
22 let window_count = windows.len(); 91 let window_count = windows.len();
23 if window_count == 0 { 92 if window_count == 0 {
24 return Vec::new(); 93 return Vec::new();
25 } 94 }
26 95
27 - if window_count == 1 { 96 + let smartgaps_enabled = true;
28 - let x = gaps.outer_horizontal as i32; 97 + let gap_values = Self::getgaps(gaps, window_count, smartgaps_enabled);
29 - let y = gaps.outer_vertical as i32; 98 +
30 - let width = screen_width.saturating_sub(2 * gaps.outer_horizontal); 99 + let outer_gap_horizontal = gap_values.outer_horizontal;
31 - let height = screen_height.saturating_sub(2 * gaps.outer_vertical); 100 + let outer_gap_vertical = gap_values.outer_vertical;
32 - 101 + let inner_gap_horizontal = gap_values.inner_horizontal;
33 - vec![WindowGeometry { 102 + let inner_gap_vertical = gap_values.inner_vertical;
34 - x_coordinate: x, 103 +
35 - y_coordinate: y, 104 + let mut stack_x = outer_gap_vertical as i32;
36 - width, 105 + let mut stack_y = outer_gap_horizontal as i32;
37 - height, 106 + let master_x = outer_gap_vertical as i32;
38 - }] 107 + let mut master_y = outer_gap_horizontal as i32;
108 +
109 + let num_master_usize = num_master.max(0) as usize;
110 + let master_count = window_count.min(num_master_usize);
111 + let stack_count = if window_count > num_master_usize {
112 + window_count - num_master_usize
39 } else { 113 } else {
40 - let mut geometries = Vec::new(); 114 + 0
115 + };
41 116
42 - let master_width = (screen_width / 2) 117 + let master_height = (screen_height as i32)
43 - .saturating_sub(gaps.outer_horizontal) 118 + - (2 * outer_gap_horizontal) as i32
44 - .saturating_sub(gaps.inner_horizontal / 2); 119 + - (inner_gap_horizontal as i32 * (master_count.saturating_sub(1)) as i32);
120 + let stack_height = (screen_height as i32)
121 + - (2 * outer_gap_horizontal) as i32
122 + - (inner_gap_horizontal as i32 * stack_count.saturating_sub(1) as i32);
123 + let mut stack_width = (screen_width as i32) - (2 * outer_gap_vertical) as i32;
124 + let mut master_width = stack_width;
45 125
46 - let master_x = gaps.outer_horizontal as i32; 126 + if num_master > 0 && window_count > num_master_usize {
47 - let master_y = gaps.outer_vertical as i32; 127 + stack_width = ((master_width as f32 - inner_gap_vertical as f32) * (1.0 - master_factor)) as i32;
48 - let master_height = screen_height.saturating_sub(2 * gaps.outer_vertical); 128 + master_width = master_width - inner_gap_vertical as i32 - stack_width;
129 + stack_x = master_x + master_width + inner_gap_vertical as i32;
130 + }
49 131
50 - geometries.push(WindowGeometry { 132 + let facts = Self::getfacts(window_count, num_master, master_height, stack_height);
51 - x_coordinate: master_x,
52 - y_coordinate: master_y,
53 - width: master_width,
54 - height: master_height,
55 - });
56 133
57 - let stack_count = window_count - 1; 134 + let mut geometries = Vec::new();
58 - let stack_x = (screen_width / 2 + gaps.inner_horizontal / 2) as i32;
59 - let stack_width = (screen_width / 2)
60 - .saturating_sub(gaps.outer_horizontal)
61 - .saturating_sub(gaps.inner_horizontal / 2);
62 135
63 - let total_stack_height = screen_height.saturating_sub(2 * gaps.outer_vertical); 136 + for (i, _window) in windows.iter().enumerate() {
137 + if i < num_master_usize {
138 + let window_height = (master_height as f32 / facts.master_facts) as i32
139 + + if (i as i32) < facts.master_remainder {
140 + 1
141 + } else {
142 + 0
143 + };
64 144
65 - let total_inner_gaps = gaps.inner_vertical * (stack_count as u32 - 1); 145 + geometries.push(WindowGeometry {
66 - let stack_height = 146 + x_coordinate: master_x,
67 - total_stack_height.saturating_sub(total_inner_gaps) / stack_count as u32; 147 + y_coordinate: master_y,
148 + width: master_width as u32,
149 + height: window_height as u32,
150 + });
68 151
69 - for i in 1..window_count { 152 + master_y += window_height + inner_gap_horizontal as i32;
70 - let stack_index = i - 1; 153 + } else {
71 - let y_offset = gaps.outer_vertical 154 + let window_height = if facts.stack_facts > 0.0 {
72 - + (stack_index as u32) * (stack_height + gaps.inner_vertical); 155 + (stack_height as f32 / facts.stack_facts) as i32
156 + + if ((i - num_master_usize) as i32) < facts.stack_remainder {
157 + 1
158 + } else {
159 + 0
160 + }
161 + } else {
162 + stack_height
163 + };
73 164
74 geometries.push(WindowGeometry { 165 geometries.push(WindowGeometry {
75 x_coordinate: stack_x, 166 x_coordinate: stack_x,
76 - y_coordinate: y_offset as i32, 167 + y_coordinate: stack_y,
77 - width: stack_width, 168 + width: stack_width as u32,
78 - height: stack_height, 169 + height: window_height as u32,
79 }); 170 });
80 - }
81 171
82 - return geometries; 172 + stack_y += window_height + inner_gap_horizontal as i32;
173 + }
83 } 174 }
175 +
176 + geometries
84 } 177 }
85 } 178 }
+2 −0 src/overlay/keybind.rs
@@ -217,6 +217,8 @@ impl KeybindOverlay {
217 KeyAction::CycleLayout => "Cycle Through Layouts".to_string(), 217 KeyAction::CycleLayout => "Cycle Through Layouts".to_string(),
218 KeyAction::FocusMonitor => "Focus Next Monitor".to_string(), 218 KeyAction::FocusMonitor => "Focus Next Monitor".to_string(),
219 KeyAction::ExchangeClient => "Exchange Client Windows".to_string(), 219 KeyAction::ExchangeClient => "Exchange Client Windows".to_string(),
220 + KeyAction::SetMasterFactor => "Adjust Master Area Size".to_string(),
221 + KeyAction::IncNumMaster => "Adjust Number of Master Windows".to_string(),
220 KeyAction::None => "No Action".to_string(), 222 KeyAction::None => "No Action".to_string(),
221 } 223 }
222 } 224 }
+83 −5 src/window_manager.rs
@@ -594,6 +594,24 @@ impl WindowManager {
594 Ok(()) 594 Ok(())
595 } 595 }
596 596
597 + fn set_master_factor(&mut self, delta: f32) -> WmResult<()> {
598 + if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
599 + let new_mfact = (monitor.master_factor + delta).max(0.05).min(0.95);
600 + monitor.master_factor = new_mfact;
601 + self.apply_layout()?;
602 + }
603 + Ok(())
604 + }
605 +
606 + fn inc_num_master(&mut self, delta: i32) -> WmResult<()> {
607 + if let Some(monitor) = self.monitors.get_mut(self.selected_monitor) {
608 + let new_nmaster = (monitor.num_master + delta).max(0);
609 + monitor.num_master = new_nmaster;
610 + self.apply_layout()?;
611 + }
612 + Ok(())
613 + }
614 +
597 fn exchange_client(&mut self, direction: i32) -> WmResult<()> { 615 fn exchange_client(&mut self, direction: i32) -> WmResult<()> {
598 let focused = match self 616 let focused = match self
599 .monitors 617 .monitors
@@ -907,6 +925,16 @@ impl WindowManager {
907 monitor.screen_height as u16, 925 monitor.screen_height as u16,
908 )?; 926 )?;
909 } 927 }
928 + KeyAction::SetMasterFactor => {
929 + if let Arg::Int(delta) = arg {
930 + self.set_master_factor(*delta as f32 / 100.0)?;
931 + }
932 + }
933 + KeyAction::IncNumMaster => {
934 + if let Arg::Int(delta) = arg {
935 + self.inc_num_master(*delta)?;
936 + }
937 + }
910 KeyAction::None => {} 938 KeyAction::None => {}
911 } 939 }
912 Ok(()) 940 Ok(())
@@ -1732,7 +1760,7 @@ impl WindowManager {
1732 self.clients.insert(window, client); 1760 self.clients.insert(window, client);
1733 self.update_size_hints(window)?; 1761 self.update_size_hints(window)?;
1734 self.update_window_title(window)?; 1762 self.update_window_title(window)?;
1735 - self.attach(window, monitor_index); 1763 + self.attach_aside(window, monitor_index);
1736 self.attach_stack(window, monitor_index); 1764 self.attach_stack(window, monitor_index);
1737 1765
1738 self.windows.push(window); 1766 self.windows.push(window);
@@ -2668,10 +2696,17 @@ impl WindowManager {
2668 0 2696 0
2669 }; 2697 };
2670 let usable_height = monitor_height.saturating_sub(bar_height as i32); 2698 let usable_height = monitor_height.saturating_sub(bar_height as i32);
2671 - 2699 + let master_factor = monitor.master_factor;
2672 - let geometries = self 2700 + let num_master = monitor.num_master;
2673 - .layout 2701 +
2674 - .arrange(&visible, monitor_width as u32, usable_height as u32, &gaps); 2702 + let geometries = self.layout.arrange(
2703 + &visible,
2704 + monitor_width as u32,
2705 + usable_height as u32,
2706 + &gaps,
2707 + master_factor,
2708 + num_master,
2709 + );
2675 2710
2676 for (window, geometry) in visible.iter().zip(geometries.iter()) { 2711 for (window, geometry) in visible.iter().zip(geometries.iter()) {
2677 let adjusted_width = geometry.width.saturating_sub(2 * border_width); 2712 let adjusted_width = geometry.width.saturating_sub(2 * border_width);
@@ -3044,6 +3079,49 @@ impl WindowManager {
3044 } 3079 }
3045 } 3080 }
3046 3081
3082 + fn attach_aside(&mut self, window: Window, monitor_index: usize) {
3083 + let monitor = match self.monitors.get(monitor_index) {
3084 + Some(m) => m,
3085 + None => return,
3086 + };
3087 +
3088 + if monitor.clients_head.is_none() {
3089 + self.attach(window, monitor_index);
3090 + return;
3091 + }
3092 +
3093 + let num_master = monitor.num_master.max(1) as usize;
3094 + let mut current = monitor.clients_head;
3095 + let mut position = 0;
3096 +
3097 + while position < num_master - 1 {
3098 + if let Some(current_window) = current {
3099 + if let Some(current_client) = self.clients.get(&current_window) {
3100 + current = current_client.next;
3101 + position += 1;
3102 + } else {
3103 + break;
3104 + }
3105 + } else {
3106 + break;
3107 + }
3108 + }
3109 +
3110 + if let Some(insert_after) = current {
3111 + if let Some(after_client) = self.clients.get(&insert_after) {
3112 + let old_next = after_client.next;
3113 + if let Some(new_client) = self.clients.get_mut(&window) {
3114 + new_client.next = old_next;
3115 + }
3116 + if let Some(after_client_mut) = self.clients.get_mut(&insert_after) {
3117 + after_client_mut.next = Some(window);
3118 + }
3119 + }
3120 + } else {
3121 + self.attach(window, monitor_index);
3122 + }
3123 + }
3124 +
3047 fn detach(&mut self, window: Window) { 3125 fn detach(&mut self, window: Window) {
3048 let monitor_index = self.clients.get(&window).map(|c| c.monitor_index); 3126 let monitor_index = self.clients.get(&window).map(|c| c.monitor_index);
3049 if let Some(monitor_index) = monitor_index { 3127 if let Some(monitor_index) = monitor_index {