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 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 83 oxwm.key.bind({ modkey, "Shift" }, "Q", oxwm.quit())
76 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 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 599 let show_keybinds = lua.create_function(|lua, ()| {
592 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 628 parent.set("restart", restart)?;
621 629 parent.set("recompile", recompile)?;
622 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 633 parent.set("show_keybinds", show_keybinds)?;
624 634 parent.set("focus_monitor", focus_monitor)?;
625 635 Ok(())
@@ -708,6 +718,8 @@ fn string_to_action(s: &str) -> mlua::Result<KeyAction> {
708 718 "Recompile" => Ok(KeyAction::Recompile),
709 719 "ViewTag" => Ok(KeyAction::ViewTag),
710 720 "ToggleGaps" => Ok(KeyAction::ToggleGaps),
721 + "SetMasterFactor" => Ok(KeyAction::SetMasterFactor),
722 + "IncNumMaster" => Ok(KeyAction::IncNumMaster),
711 723 "ToggleFullScreen" => Ok(KeyAction::ToggleFullScreen),
712 724 "ToggleFloating" => Ok(KeyAction::ToggleFloating),
713 725 "ChangeLayout" => Ok(KeyAction::ChangeLayout),
+2 −0 src/keyboard/handlers.rs
@@ -30,6 +30,8 @@ pub enum KeyAction {
30 30 FocusMonitor,
31 31 ExchangeClient,
32 32 ShowKeybindOverlay,
33 + SetMasterFactor,
34 + IncNumMaster,
33 35 None,
34 36 }
35 37
+2 −0 src/layout/grid.rs
@@ -18,6 +18,8 @@ impl Layout for GridLayout {
18 18 screen_width: u32,
19 19 screen_height: u32,
20 20 gaps: &GapConfig,
21 + _master_factor: f32,
22 + _num_master: i32,
21 23 ) -> Vec<WindowGeometry> {
22 24 let window_count = windows.len();
23 25 if window_count == 0 {
+2 −0 src/layout/mod.rs
@@ -86,6 +86,8 @@ pub trait Layout {
86 86 screen_width: u32,
87 87 screen_height: u32,
88 88 gaps: &GapConfig,
89 + master_factor: f32,
90 + num_master: i32,
89 91 ) -> Vec<WindowGeometry>;
90 92 fn name(&self) -> &'static str;
91 93 fn symbol(&self) -> &'static str;
+2 −0 src/layout/monocle.rs
@@ -18,6 +18,8 @@ impl Layout for MonocleLayout {
18 18 screen_width: u32,
19 19 screen_height: u32,
20 20 gaps: &GapConfig,
21 + _master_factor: f32,
22 + _num_master: i32,
21 23 ) -> Vec<WindowGeometry> {
22 24 let window_count = windows.len();
23 25 if window_count == 0 {
+2 −0 src/layout/normie.rs
@@ -19,6 +19,8 @@ impl Layout for NormieLayout {
19 19 screen_width: u32,
20 20 screen_height: u32,
21 21 _gaps: &GapConfig,
22 + _master_factor: f32,
23 + _num_master: i32,
22 24 ) -> Vec<WindowGeometry> {
23 25 const DEFAULT_WIDTH_RATIO: f32 = 0.6;
24 26 const DEFAULT_HEIGHT_RATIO: f32 = 0.6;
+2 −0 src/layout/tabbed.rs
@@ -20,6 +20,8 @@ impl Layout for TabbedLayout {
20 20 screen_width: u32,
21 21 screen_height: u32,
22 22 gaps: &GapConfig,
23 + _master_factor: f32,
24 + _num_master: i32,
23 25 ) -> Vec<WindowGeometry> {
24 26 let window_count = windows.len();
25 27 if window_count == 0 {
+136 −43 src/layout/tiling.rs
@@ -3,6 +3,73 @@ use x11rb::protocol::xproto::Window;
3 3
4 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 73 impl Layout for TilingLayout {
7 74 fn name(&self) -> &'static str {
8 75 super::LayoutType::Tiling.as_str()
@@ -18,68 +85,94 @@ impl Layout for TilingLayout {
18 85 screen_width: u32,
19 86 screen_height: u32,
20 87 gaps: &GapConfig,
88 + master_factor: f32,
89 + num_master: i32,
21 90 ) -> Vec<WindowGeometry> {
22 91 let window_count = windows.len();
23 92 if window_count == 0 {
24 93 return Vec::new();
25 94 }
26 95
27 - if window_count == 1 {
28 - let x = gaps.outer_horizontal as i32;
29 - let y = gaps.outer_vertical as i32;
30 - let width = screen_width.saturating_sub(2 * gaps.outer_horizontal);
31 - let height = screen_height.saturating_sub(2 * gaps.outer_vertical);
32 -
33 - vec![WindowGeometry {
34 - x_coordinate: x,
35 - y_coordinate: y,
36 - width,
37 - height,
38 - }]
96 + let smartgaps_enabled = true;
97 + let gap_values = Self::getgaps(gaps, window_count, smartgaps_enabled);
98 +
99 + let outer_gap_horizontal = gap_values.outer_horizontal;
100 + let outer_gap_vertical = gap_values.outer_vertical;
101 + let inner_gap_horizontal = gap_values.inner_horizontal;
102 + let inner_gap_vertical = gap_values.inner_vertical;
103 +
104 + let mut stack_x = outer_gap_vertical as i32;
105 + let mut stack_y = outer_gap_horizontal as i32;
106 + let master_x = outer_gap_vertical as i32;
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 113 } else {
40 - let mut geometries = Vec::new();
114 + 0
115 + };
41 116
42 - let master_width = (screen_width / 2)
43 - .saturating_sub(gaps.outer_horizontal)
44 - .saturating_sub(gaps.inner_horizontal / 2);
117 + let master_height = (screen_height as i32)
118 + - (2 * outer_gap_horizontal) as i32
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;
47 - let master_y = gaps.outer_vertical as i32;
48 - let master_height = screen_height.saturating_sub(2 * gaps.outer_vertical);
126 + if num_master > 0 && window_count > num_master_usize {
127 + stack_width = ((master_width as f32 - inner_gap_vertical as f32) * (1.0 - master_factor)) as i32;
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 {
51 - x_coordinate: master_x,
52 - y_coordinate: master_y,
53 - width: master_width,
54 - height: master_height,
55 - });
132 + let facts = Self::getfacts(window_count, num_master, master_height, stack_height);
56 133
57 - let stack_count = window_count - 1;
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);
134 + let mut geometries = Vec::new();
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);
66 - let stack_height =
67 - total_stack_height.saturating_sub(total_inner_gaps) / stack_count as u32;
145 + geometries.push(WindowGeometry {
146 + x_coordinate: master_x,
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 {
70 - let stack_index = i - 1;
71 - let y_offset = gaps.outer_vertical
72 - + (stack_index as u32) * (stack_height + gaps.inner_vertical);
152 + master_y += window_height + inner_gap_horizontal as i32;
153 + } else {
154 + let window_height = if facts.stack_facts > 0.0 {
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 165 geometries.push(WindowGeometry {
75 166 x_coordinate: stack_x,
76 - y_coordinate: y_offset as i32,
77 - width: stack_width,
78 - height: stack_height,
167 + y_coordinate: stack_y,
168 + width: stack_width as u32,
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 217 KeyAction::CycleLayout => "Cycle Through Layouts".to_string(),
218 218 KeyAction::FocusMonitor => "Focus Next Monitor".to_string(),
219 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 222 KeyAction::None => "No Action".to_string(),
221 223 }
222 224 }
+83 −5 src/window_manager.rs
@@ -594,6 +594,24 @@ impl WindowManager {
594 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 615 fn exchange_client(&mut self, direction: i32) -> WmResult<()> {
598 616 let focused = match self
599 617 .monitors
@@ -907,6 +925,16 @@ impl WindowManager {
907 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 938 KeyAction::None => {}
911 939 }
912 940 Ok(())
@@ -1732,7 +1760,7 @@ impl WindowManager {
1732 1760 self.clients.insert(window, client);
1733 1761 self.update_size_hints(window)?;
1734 1762 self.update_window_title(window)?;
1735 - self.attach(window, monitor_index);
1763 + self.attach_aside(window, monitor_index);
1736 1764 self.attach_stack(window, monitor_index);
1737 1765
1738 1766 self.windows.push(window);
@@ -2668,10 +2696,17 @@ impl WindowManager {
2668 2696 0
2669 2697 };
2670 2698 let usable_height = monitor_height.saturating_sub(bar_height as i32);
2671 -
2672 - let geometries = self
2673 - .layout
2674 - .arrange(&visible, monitor_width as u32, usable_height as u32, &gaps);
2699 + let master_factor = monitor.master_factor;
2700 + let num_master = monitor.num_master;
2701 +
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 2711 for (window, geometry) in visible.iter().zip(geometries.iter()) {
2677 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 3125 fn detach(&mut self, window: Window) {
3048 3126 let monitor_index = self.clients.get(&window).map(|c| c.monitor_index);
3049 3127 if let Some(monitor_index) = monitor_index {