tony / oxwm

Merge pull request #250 from Hamster3332/attachmethods Browse files
Attachmethods
Tony, btw. committed 2026-09-15 parents d0bb35b 4124e49 commit fc4ada9ac4ee8e34ace203290a2b14d10e4671cc
Showing 10 changed files with 121 additions and 5 deletions. UnifiedSplit
+1 −1 build.zig.zon
@@ -1,6 +1,6 @@
1 1 .{
2 2 .name = .oxwm,
3 - .version = "0.12.0",
3 + .version = "0.13.0",
4 4 .fingerprint = 0x4b9ef3753914924d, // Changing this has security and trust implications.
5 5 .minimum_zig_version = "0.16.0",
6 6 .dependencies = .{
+2 −0 resources/test-config.lua
@@ -40,6 +40,8 @@ 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("top")
44 +
43 45 oxwm.border.set_width(2)
44 46 oxwm.border.set_focused_color(colors.blue)
45 47 oxwm.border.set_unfocused_color(colors.grey)
+78 −0 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),
@@ -146,6 +147,30 @@ pub fn nextTagged(client: *Client) ?*Client {
146 147 return null;
147 148 }
148 149
150 +/// Returns the last non-floating client on `client`'s monitor that shares
151 +/// any tag with `client`. Used for `attach_bottom` ordering.
152 +pub fn lastTagged(client: *Client) ?*Client {
153 + const monitor = client.monitor orelse return null;
154 + var walked = monitor.clients;
155 + var last: ?*Client = null;
156 + while (walked) |iter| {
157 + if (!iter.is_floating and isVisibleOnTag(iter, client.tags)) last = iter;
158 + walked = iter.next;
159 + }
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),
171 + }
172 +}
173 +
149 174 /// Inserts `client` just after the first client that shares its tags,
150 175 /// falling back to prepend if none exists.
151 176 pub fn attachAside(client: *Client) void {
@@ -158,6 +183,47 @@ pub fn attachAside(client: *Client) void {
158 183 at.?.next = client;
159 184 }
160 185
186 +/// Inserts `client` just after the last client that shares its tags,
187 +/// falling back to prepend if none exists.
188 +pub fn attachBottom(client: *Client) void {
189 + const last = lastTagged(client) orelse {
190 + attach(client);
191 + return;
192 + };
193 + client.next = last.next;
194 + last.next = client;
195 +}
196 +
197 +/// Inserts `client` just before the selected client, falling back to
198 +/// prepend if there is no selection or the selection is floating.
199 +pub fn attachAbove(client: *Client) void {
200 + const monitor = client.monitor orelse return;
201 + const sel = monitor.sel orelse {
202 + attach(client);
203 + return;
204 + };
205 + if (sel == client or sel.is_floating) {
206 + attach(client);
207 + return;
208 + }
209 + insertBefore(client, sel);
210 +}
211 +
212 +/// Inserts `client` just after the selected client, falling back to
213 +/// prepend if there is no selection or the selection is floating.
214 +pub fn attachBelow(client: *Client) void {
215 + const monitor = client.monitor orelse return;
216 + const sel = monitor.sel orelse {
217 + attach(client);
218 + return;
219 + };
220 + if (sel == client or sel.is_floating) {
221 + attach(client);
222 + return;
223 + }
224 + insertAfter(client, sel);
225 +}
226 +
161 227 /// Counts non-floating, visible clients on `monitor`.
162 228 pub fn countTiled(monitor: *Monitor) u32 {
163 229 var count: u32 = 0;
@@ -216,6 +282,18 @@ pub fn insertBefore(client: *Client, target: *Client) void {
216 282 }
217 283 }
218 284
285 +/// Moves `client` to just after `target` in the monitor's client list.
286 +/// Does nothing if they are on different monitors.
287 +pub fn insertAfter(client: *Client, target: *Client) void {
288 + const monitor = target.monitor orelse return;
289 + if (client.monitor != monitor) return;
290 +
291 + detach(client);
292 +
293 + client.next = target.next;
294 + target.next = client;
295 +}
296 +
219 297 /// Swaps the positions of `client_a` and `client_b` in their shared monitor's
220 298 /// client list. Does nothing if they are on different monitors.
221 299 pub fn swapClients(client_a: *Client, client_b: *Client) void {
+13 −0 src/config/config.zig
@@ -102,6 +102,18 @@ pub const Layouts = enum(u32) {
102 102 }
103 103 };
104 104
105 +pub const AttachMethods = enum {
106 + aside,
107 + top,
108 + bottom,
109 + above,
110 + below,
111 +
112 + pub fn fromString(name: []const u8) ?AttachMethods {
113 + return std.meta.stringToEnum(AttachMethods, name);
114 + }
115 +};
116 +
105 117 pub const MouseButton = struct {
106 118 click: ClickTarget,
107 119 mod_mask: u32,
@@ -187,6 +199,7 @@ pub const Config = struct {
187 199 tags: [12][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" },
188 200 tag_count: u32 = 9,
189 201 layout: []const u8 = "tiling",
202 + attach_method: AttachMethods = .aside,
190 203 tag_layouts: [12]?[]const u8 = .{null} ** 12,
191 204
192 205 border_width: i32 = 2,
+14 −0 src/config/lua.zig
@@ -324,6 +324,9 @@ fn registerMiscFunctions(state: *c.lua_State) void {
324 324 c.lua_pushcfunction(state, luaSetLayoutSymbol);
325 325 c.lua_setfield(state, -2, "set_layout_symbol");
326 326
327 + c.lua_pushcfunction(state, luaSetAttachMethod);
328 + c.lua_setfield(state, -2, "set_attach_method");
329 +
327 330 c.lua_pushcfunction(state, luaAutostart);
328 331 c.lua_setfield(state, -2, "autostart");
329 332
@@ -1095,6 +1098,17 @@ fn luaSetLayout(state: ?*c.lua_State) callconv(.c) c_int {
1095 1098 return 0;
1096 1099 }
1097 1100
1101 +fn luaSetAttachMethod(state: ?*c.lua_State) callconv(.c) c_int {
1102 + const cfg = config orelse return 0;
1103 + const s = state orelse return 0;
1104 + const name = getStringArg(s, 1) orelse return 0;
1105 + cfg.attach_method = config_mod.AttachMethods.fromString(name) orelse {
1106 + std.debug.print("set_attach_method: unknown attach method '{s}'\n", .{name});
1107 + return 0;
1108 + };
1109 + return 0;
1110 +}
1111 +
1098 1112 fn luaSetModkey(state: ?*c.lua_State) callconv(.c) c_int {
1099 1113 const cfg = config orelse return 0;
1100 1114 const s = state orelse return 0;
+2 −2 src/wm/actions.zig
@@ -474,7 +474,7 @@ pub fn sendmon(direction: i32, wm: *WindowManager) void {
474 474 client_mod.detachStack(client);
475 475 client.monitor = target;
476 476 client.tags = target.tagset[target.sel_tags];
477 - client_mod.attachAside(client);
477 + client_mod.attachWith(client, wm.config.attach_method);
478 478 client_mod.attachStack(client);
479 479
480 480 core.focusTopClient(source_monitor, wm);
@@ -591,7 +591,7 @@ pub fn movemouse(wm: *WindowManager) void {
591 591 client_mod.detachStack(client);
592 592 client.monitor = new_mon;
593 593 client.tags = new_mon.?.tagset[new_mon.?.sel_tags];
594 - client_mod.attachAside(client);
594 + client_mod.attachWith(client, wm.config.attach_method);
595 595 client_mod.attachStack(client);
596 596 wm.selected_monitor = new_mon;
597 597 core.focus(client, wm);
+1 −1 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 - client_mod.attachAside(client);
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);
+1 −1 src/wm/wm.zig
@@ -352,7 +352,7 @@ pub const WindowManager = struct {
352 352 client_mod.detachStack(c);
353 353 c.monitor = target;
354 354 c.tags = target.tagset[target.sel_tags];
355 - client_mod.attachAside(c);
355 + client_mod.attachWith(c, self.config.attach_method);
356 356 client_mod.attachStack(c);
357 357 }
358 358 }
+5 −0 templates/config.lua
@@ -129,6 +129,11 @@ oxwm.set_layout_symbol("tabbed", "[=]")
129 129 -- Unset value uses oxwm.set_layout value
130 130 -- oxwm.set_tag_layout(1, "grid")
131 131
132 +-- Set the default attach method, similar to the patches from dwm
133 +-- Available methods: "aside" (default), "top", "bottom", "above", "below"
134 +-- For example "above" makes new clients appear above the currently selected one
135 +oxwm.set_attach_method("aside")
136 +
132 137 -------------------------------------------------------------------------------
133 138 -- Appearance
134 139 -------------------------------------------------------------------------------
+4 −0 templates/oxwm.lua
@@ -59,6 +59,10 @@ function oxwm.set_layout(name) end
59 59 ---@param name string Layout name ("tiling", "floating", "scrolling", "grid", "monocle", "dwindle", or aliases)
60 60 function oxwm.set_tag_layout(tag, name) end
61 61
62 +---Set default attach method
63 +---@param name string Attach Method name ("aside", "top", "bottom", "above", "below")
64 +function oxwm.set_attach_method(name) end
65 +
62 66 ---Set floating window spawn position
63 67 ---@param position string Position ("center", "top-left", "top-right", "bottom-left", "bottom-right", "top-center", "bottom-center", "center-left", "center-right", or underscore variants)
64 68 function oxwm.set_floating_position(position) end