tony / oxwm

fixed attachBottom dropping clients after the anchor, removed dead ChangeAttachMethod action, stored attach method as enum and applied it to all attach sites Browse files
tonybanters committed 2026-09-15 parent 57531bf commit 0331bc44972c63c15d45d1926ffcb826c4d2469d
Showing 7 changed files with 48 additions and 103 deletions. UnifiedSplit
+1 −1 resources/test-config.lua
@@ -40,7 +40,7 @@ oxwm.bar.set_hide_vacant_tags(false);
40 40 oxwm.set_layout_symbol("tiling", "[T]")
41 41 oxwm.set_layout_symbol("normie", "[F]")
42 42
43 -oxwm.set_attach_method("above")
43 +oxwm.set_attach_method("top")
44 44
45 45 oxwm.border.set_width(2)
46 46 oxwm.border.set_focused_color(colors.blue)
+38 −46 src/client.zig
@@ -1,6 +1,7 @@
1 1 const std = @import("std");
2 2 const xlib = @import("x11/xlib.zig");
3 3 const Monitor = @import("monitor.zig").Monitor;
4 +const AttachMethods = @import("config/config.zig").AttachMethods;
4 5
5 6 pub const Client = struct {
6 7 name: [256]u8 = std.mem.zeroes([256]u8),
@@ -151,21 +152,23 @@ pub fn nextTagged(client: *Client) ?*Client {
151 152 pub fn lastTagged(client: *Client) ?*Client {
152 153 const monitor = client.monitor orelse return null;
153 154 var walked = monitor.clients;
154 - var last_tiled: u32 = 0;
155 - var i: u32 = 0;
155 + var last: ?*Client = null;
156 156 while (walked) |iter| {
157 - if (!iter.is_floating and isVisibleOnTag(iter, client.tags))
158 - last_tiled = i;
157 + if (!iter.is_floating and isVisibleOnTag(iter, client.tags)) last = iter;
159 158 walked = iter.next;
160 - i += 1;
161 159 }
162 - walked = monitor.clients;
163 - i = 0;
164 - while (i < last_tiled) {
165 - walked = walked.?.next;
166 - i += 1;
160 + return last;
161 +}
162 +
163 +/// Inserts `client` into its monitor's client list using `method`.
164 +pub fn attachWith(client: *Client, method: AttachMethods) void {
165 + switch (method) {
166 + .aside => attachAside(client),
167 + .top => attach(client),
168 + .bottom => attachBottom(client),
169 + .above => attachAbove(client),
170 + .below => attachBelow(client),
167 171 }
168 - return walked;
169 172 }
170 173
171 174 /// Inserts `client` just after the first client that shares its tags,
@@ -180,43 +183,45 @@ pub fn attachAside(client: *Client) void {
180 183 at.?.next = client;
181 184 }
182 185
183 -/// Inserts `client` at the top of the client stack.
184 -pub fn attachTop(client: *Client) void {
185 - client.next = client.monitor.?.clients;
186 - client.monitor.?.clients = client;
187 -}
188 -
189 -/// Inserts `client` at the bottom of the client stack.
186 +/// Inserts `client` just after the last client that shares its tags,
187 +/// falling back to prepend if none exists.
190 188 pub fn attachBottom(client: *Client) void {
191 - const last = lastTagged(client);
192 - if (last == null) {
189 + const last = lastTagged(client) orelse {
193 190 attach(client);
194 191 return;
195 - }
196 - client.next = null;
197 - last.?.next = client;
192 + };
193 + client.next = last.next;
194 + last.next = client;
198 195 }
199 196
200 -/// Inserts `client` above the selected client,
201 -/// falling back to prepend if none exists.
197 +/// Inserts `client` just before the selected client, falling back to
198 +/// prepend if there is no selection or the selection is floating.
202 199 pub fn attachAbove(client: *Client) void {
203 200 const monitor = client.monitor orelse return;
204 - if (monitor.sel == null) {
201 + const sel = monitor.sel orelse {
202 + attach(client);
203 + return;
204 + };
205 + if (sel == client or sel.is_floating) {
205 206 attach(client);
206 207 return;
207 208 }
208 - insertBefore(client, monitor.sel orelse return);
209 + insertBefore(client, sel);
209 210 }
210 211
211 -/// Inserts `client` below the selected client,
212 -/// falling back to prepend if none exists.
212 +/// Inserts `client` just after the selected client, falling back to
213 +/// prepend if there is no selection or the selection is floating.
213 214 pub fn attachBelow(client: *Client) void {
214 215 const monitor = client.monitor orelse return;
215 - if (monitor.sel == null) {
216 + const sel = monitor.sel orelse {
217 + attach(client);
218 + return;
219 + };
220 + if (sel == client or sel.is_floating) {
216 221 attach(client);
217 222 return;
218 223 }
219 - insertAfter(client, monitor.sel orelse return);
224 + insertAfter(client, sel);
220 225 }
221 226
222 227 /// Counts non-floating, visible clients on `monitor`.
@@ -285,21 +290,8 @@ pub fn insertAfter(client: *Client, target: *Client) void {
285 290
286 291 detach(client);
287 292
288 - if (monitor.clients == target) {
289 - client.next = target.next;
290 - monitor.clients.?.next = client;
291 - return;
292 - }
293 -
294 - var current = monitor.clients;
295 - while (current) |iter| {
296 - if (iter.next == target) {
297 - client.next = target.next;
298 - iter.next.?.next = client;
299 - return;
300 - }
301 - current = iter.next;
302 - }
293 + client.next = target.next;
294 + target.next = client;
303 295 }
304 296
305 297 /// Swaps the positions of `client_a` and `client_b` in their shared monitor's
+3 −10 src/config/config.zig
@@ -23,7 +23,6 @@ pub const Action = enum {
23 23 set_layout,
24 24 set_layout_tiling,
25 25 set_layout_floating,
26 - set_attach_method,
27 26 view_tag,
28 27 view_next_tag,
29 28 view_prev_tag,
@@ -103,7 +102,7 @@ pub const Layouts = enum(u32) {
103 102 }
104 103 };
105 104
106 -pub const AttachMethods = enum(u32) {
105 +pub const AttachMethods = enum {
107 106 aside,
108 107 top,
109 108 bottom,
@@ -111,13 +110,7 @@ pub const AttachMethods = enum(u32) {
111 110 below,
112 111
113 112 pub fn fromString(name: []const u8) ?AttachMethods {
114 - if (std.meta.stringToEnum(AttachMethods, name)) |v| return v;
115 - if (std.mem.eql(u8, name, "aside")) return .aside;
116 - if (std.mem.eql(u8, name, "top")) return .top;
117 - if (std.mem.eql(u8, name, "bottom")) return .bottom;
118 - if (std.mem.eql(u8, name, "above")) return .above;
119 - if (std.mem.eql(u8, name, "below")) return .below;
120 - return null;
113 + return std.meta.stringToEnum(AttachMethods, name);
121 114 }
122 115 };
123 116
@@ -206,7 +199,7 @@ pub const Config = struct {
206 199 tags: [12][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" },
207 200 tag_count: u32 = 9,
208 201 layout: []const u8 = "tiling",
209 - attach_method: []const u8 = "aside",
202 + attach_method: AttachMethods = .aside,
210 203 tag_layouts: [12]?[]const u8 = .{null} ** 12,
211 204
212 205 border_width: i32 = 2,
+2 −12 src/config/lua.zig
@@ -639,12 +639,6 @@ fn luaLayoutScrollRight(state: ?*c.lua_State) callconv(.c) c_int {
639 639 return 1;
640 640 }
641 641
642 -fn luaAttachMethodSet(state: ?*c.lua_State) callconv(.c) c_int {
643 - const s = state orelse return 0;
644 - createActionTableWithString(s, "ChangeAttachMethod");
645 - return 1;
646 -}
647 -
648 642 fn luaTagView(state: ?*c.lua_State) callconv(.c) c_int {
649 643 const s = state orelse return 0;
650 644 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
@@ -1108,13 +1102,10 @@ fn luaSetAttachMethod(state: ?*c.lua_State) callconv(.c) c_int {
1108 1102 const cfg = config orelse return 0;
1109 1103 const s = state orelse return 0;
1110 1104 const name = getStringArg(s, 1) orelse return 0;
1111 - if (config_mod.AttachMethods.fromString(name) == null) {
1105 + cfg.attach_method = config_mod.AttachMethods.fromString(name) orelse {
1112 1106 std.debug.print("set_attach_method: unknown attach method '{s}'\n", .{name});
1113 1107 return 0;
1114 - }
1115 - if (dupeLuaString(s, 1)) |attach_method| {
1116 - cfg.attach_method = attach_method;
1117 - }
1108 + };
1118 1109 return 0;
1119 1110 }
1120 1111
@@ -1418,7 +1409,6 @@ fn parseAction(name: []const u8) ?Action {
1418 1409 .{ "ToggleBar", Action.toggle_bar },
1419 1410 .{ "CycleLayout", Action.cycle_layout },
1420 1411 .{ "ChangeLayout", Action.set_layout },
1421 - .{ "ChangeAttachMethod", Action.set_attach_method },
1422 1412 .{ "ViewTag", Action.view_tag },
1423 1413 .{ "ViewNextTag", Action.view_next_tag },
1424 1414 .{ "ViewPreviousTag", Action.view_prev_tag },
+2 −14 src/wm/actions.zig
@@ -440,17 +440,6 @@ pub fn setLayoutIndex(index: u32, wm: *WindowManager) void {
440 440 }
441 441 }
442 442
443 -pub fn setAttachMethod(attach_method_name: ?[]const u8, wm: *WindowManager) void {
444 - const name = attach_method_name orelse return;
445 - const new_att_m: u32 = if (config_mod.AttachMethods.fromString(name)) |value|
446 - @intFromEnum(value)
447 - else {
448 - std.debug.print("set_attach_method: unknown attach method '{s}'\n", .{name});
449 - return;
450 - };
451 - wm.attach_method = new_att_m;
452 -}
453 -
454 443 fn warpCursorToMonitor(monitor: *Monitor, wm: *WindowManager) void {
455 444 const center_x = monitor.win_x + @divTrunc(monitor.win_w, 2);
456 445 const center_y = monitor.win_y + @divTrunc(monitor.win_h, 2);
@@ -485,7 +474,7 @@ pub fn sendmon(direction: i32, wm: *WindowManager) void {
485 474 client_mod.detachStack(client);
486 475 client.monitor = target;
487 476 client.tags = target.tagset[target.sel_tags];
488 - client_mod.attachAside(client);
477 + client_mod.attachWith(client, wm.config.attach_method);
489 478 client_mod.attachStack(client);
490 479
491 480 core.focusTopClient(source_monitor, wm);
@@ -602,7 +591,7 @@ pub fn movemouse(wm: *WindowManager) void {
602 591 client_mod.detachStack(client);
603 592 client.monitor = new_mon;
604 593 client.tags = new_mon.?.tagset[new_mon.?.sel_tags];
605 - client_mod.attachAside(client);
594 + client_mod.attachWith(client, wm.config.attach_method);
606 595 client_mod.attachStack(client);
607 596 wm.selected_monitor = new_mon;
608 597 core.focus(client, wm);
@@ -843,7 +832,6 @@ pub fn executeAction(action: config_mod.Action, int_arg: i32, str_arg: ?[]const
843 832 .set_layout => setLayout(str_arg, wm),
844 833 .set_layout_tiling => setLayoutIndex(0, wm),
845 834 .set_layout_floating => setLayoutIndex(2, wm),
846 - .set_attach_method => setAttachMethod(str_arg, wm),
847 835 .view_tag => {
848 836 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
849 837 core.view(tag_mask, wm);
+1 −14 src/wm/core.zig
@@ -90,7 +90,7 @@ pub fn manage(win: xlib.Window, window_attrs: *xlib.XWindowAttributes, wm: *Wind
90 90 _ = xlib.XRaiseWindow(wm.display.handle, client.window);
91 91 }
92 92
93 - attach(client, wm.attach_method);
93 + client_mod.attachWith(client, wm.config.attach_method);
94 94 client_mod.attachStack(client);
95 95
96 96 _ = xlib.XChangeProperty(wm.display.handle, wm.display.root, wm.atoms.net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
@@ -267,19 +267,6 @@ pub fn arrange(monitor: *Monitor, wm: *WindowManager) void {
267 267 restack(monitor, wm);
268 268 }
269 269
270 -pub fn attach(client: *Client, att_m: u32) void {
271 - switch (att_m) {
272 - @intFromEnum(config_mod.AttachMethods.aside) => client_mod.attachAside(client),
273 - @intFromEnum(config_mod.AttachMethods.top) => client_mod.attachTop(client),
274 - @intFromEnum(config_mod.AttachMethods.bottom) => client_mod.attachBottom(client),
275 - @intFromEnum(config_mod.AttachMethods.above) => client_mod.attachAbove(client),
276 - @intFromEnum(config_mod.AttachMethods.below) => client_mod.attachBelow(client),
277 - else => {
278 - std.debug.print("No attach method found! Value: {}\n", .{att_m});
279 - },
280 - }
281 -}
282 -
283 270 pub fn showhide(monitor: *Monitor, wm: *WindowManager) void {
284 271 showhideClient(monitor.stack, wm);
285 272 }
+1 −6 src/wm/wm.zig
@@ -77,8 +77,6 @@ pub const WindowManager = struct {
77 77 /// The monitor that currently has input focus.
78 78 selected_monitor: ?*Monitor,
79 79
80 - attach_method: u32,
81 -
82 80 /// Head of the linked list of status bars (one per monitor).
83 81 bars: ?*Bar,
84 82
@@ -127,7 +125,6 @@ pub const WindowManager = struct {
127 125 .config_path = config_path,
128 126 .monitors = null,
129 127 .selected_monitor = null,
130 - .attach_method = @intFromEnum(config_mod.AttachMethods.fromString(config.attach_method) orelse config_mod.AttachMethods.aside),
131 128 .bars = null,
132 129 .chord = .{},
133 130 .overlay = null,
@@ -355,7 +352,7 @@ pub const WindowManager = struct {
355 352 client_mod.detachStack(c);
356 353 c.monitor = target;
357 354 c.tags = target.tagset[target.sel_tags];
358 - client_mod.attachAside(c);
355 + client_mod.attachWith(c, self.config.attach_method);
359 356 client_mod.attachStack(c);
360 357 }
361 358 }
@@ -721,8 +718,6 @@ pub const WindowManager = struct {
721 718
722 719 loadFn(self);
723 720
724 - self.attach_method = @intFromEnum(config_mod.AttachMethods.fromString(self.config.attach_method) orelse config_mod.AttachMethods.aside);
725 -
726 721 bar_mod.destroyBars(self.bars, self.display.handle);
727 722 self.bars = null;
728 723 self.setupBars();