Attach Methods but no hot reload
Browse files
+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("below") | |
| 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) |
+86 −0 src/client.zig
| @@ -146,6 +146,28 @@ pub fn nextTagged(client: *Client) ?*Client { | ||
| 146 | 146 | return null; |
| 147 | 147 | } |
| 148 | 148 | |
| 149 | +/// Returns the last non-floating client on `client`'s monitor that shares | |
| 150 | +/// any tag with `client`. Used for `attach_bottom` ordering. | |
| 151 | +pub fn lastTagged(client: *Client) ?*Client { | |
| 152 | + const monitor = client.monitor orelse return null; | |
| 153 | + var walked = monitor.clients; | |
| 154 | + var last_tiled: u32 = 0; | |
| 155 | + var i: u32 = 0; | |
| 156 | + while (walked) |iter| { | |
| 157 | + if (!iter.is_floating and isVisibleOnTag(iter, client.tags)) | |
| 158 | + last_tiled = i; | |
| 159 | + walked = iter.next; | |
| 160 | + i += 1; | |
| 161 | + } | |
| 162 | + walked = monitor.clients; | |
| 163 | + i = 0; | |
| 164 | + while (i < last_tiled) { | |
| 165 | + walked = walked.?.next; | |
| 166 | + i += 1; | |
| 167 | + } | |
| 168 | + return walked; | |
| 169 | +} | |
| 170 | + | |
| 149 | 171 | /// Inserts `client` just after the first client that shares its tags, |
| 150 | 172 | /// falling back to prepend if none exists. |
| 151 | 173 | pub fn attachAside(client: *Client) void { |
| @@ -158,6 +180,45 @@ pub fn attachAside(client: *Client) void { | ||
| 158 | 180 | at.?.next = client; |
| 159 | 181 | } |
| 160 | 182 | |
| 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. | |
| 190 | +pub fn attachBottom(client: *Client) void { | |
| 191 | + const last = lastTagged(client); | |
| 192 | + if (last == null) { | |
| 193 | + attach(client); | |
| 194 | + return; | |
| 195 | + } | |
| 196 | + client.next = null; | |
| 197 | + last.?.next = client; | |
| 198 | +} | |
| 199 | + | |
| 200 | +/// Inserts `client` above the selected client, | |
| 201 | +/// falling back to prepend if none exists. | |
| 202 | +pub fn attachAbove(client: *Client) void { | |
| 203 | + const monitor = client.monitor orelse return; | |
| 204 | + if (monitor.sel == null) { | |
| 205 | + attach(client); | |
| 206 | + return; | |
| 207 | + } | |
| 208 | + insertBefore(client, monitor.sel orelse return); | |
| 209 | +} | |
| 210 | + | |
| 211 | +/// Inserts `client` below the selected client, | |
| 212 | +/// falling back to prepend if none exists. | |
| 213 | +pub fn attachBelow(client: *Client) void { | |
| 214 | + const monitor = client.monitor orelse return; | |
| 215 | + if (monitor.sel == null) { | |
| 216 | + attach(client); | |
| 217 | + return; | |
| 218 | + } | |
| 219 | + insertAfter(client, monitor.sel orelse return); | |
| 220 | +} | |
| 221 | + | |
| 161 | 222 | /// Counts non-floating, visible clients on `monitor`. |
| 162 | 223 | pub fn countTiled(monitor: *Monitor) u32 { |
| 163 | 224 | var count: u32 = 0; |
| @@ -216,6 +277,31 @@ pub fn insertBefore(client: *Client, target: *Client) void { | ||
| 216 | 277 | } |
| 217 | 278 | } |
| 218 | 279 | |
| 280 | +/// Moves `client` to just after `target` in the monitor's client list. | |
| 281 | +/// Does nothing if they are on different monitors. | |
| 282 | +pub fn insertAfter(client: *Client, target: *Client) void { | |
| 283 | + const monitor = target.monitor orelse return; | |
| 284 | + if (client.monitor != monitor) return; | |
| 285 | + | |
| 286 | + detach(client); | |
| 287 | + | |
| 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 | + } | |
| 303 | +} | |
| 304 | + | |
| 219 | 305 | /// Swaps the positions of `client_a` and `client_b` in their shared monitor's |
| 220 | 306 | /// client list. Does nothing if they are on different monitors. |
| 221 | 307 | pub fn swapClients(client_a: *Client, client_b: *Client) void { |
+20 −0 src/config/config.zig
| @@ -23,6 +23,7 @@ pub const Action = enum { | ||
| 23 | 23 | set_layout, |
| 24 | 24 | set_layout_tiling, |
| 25 | 25 | set_layout_floating, |
| 26 | + set_attach_method, | |
| 26 | 27 | view_tag, |
| 27 | 28 | view_next_tag, |
| 28 | 29 | view_prev_tag, |
| @@ -102,6 +103,24 @@ pub const Layouts = enum(u32) { | ||
| 102 | 103 | } |
| 103 | 104 | }; |
| 104 | 105 | |
| 106 | +pub const AttachMethods = enum(u32) { | |
| 107 | + aside, | |
| 108 | + top, | |
| 109 | + bottom, | |
| 110 | + above, | |
| 111 | + below, | |
| 112 | + | |
| 113 | + 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; | |
| 121 | + } | |
| 122 | +}; | |
| 123 | + | |
| 105 | 124 | pub const MouseButton = struct { |
| 106 | 125 | click: ClickTarget, |
| 107 | 126 | mod_mask: u32, |
| @@ -187,6 +206,7 @@ pub const Config = struct { | ||
| 187 | 206 | tags: [12][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" }, |
| 188 | 207 | tag_count: u32 = 9, |
| 189 | 208 | layout: []const u8 = "tiling", |
| 209 | + attach_method: []const u8 = "aside", | |
| 190 | 210 | tag_layouts: [12]?[]const u8 = .{null} ** 12, |
| 191 | 211 | |
| 192 | 212 | border_width: i32 = 2, |
+24 −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 | |
| @@ -636,6 +639,12 @@ fn luaLayoutScrollRight(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 636 | 639 | return 1; |
| 637 | 640 | } |
| 638 | 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 | + | |
| 639 | 648 | fn luaTagView(state: ?*c.lua_State) callconv(.c) c_int { |
| 640 | 649 | const s = state orelse return 0; |
| 641 | 650 | const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null)); |
| @@ -1095,6 +1104,20 @@ fn luaSetLayout(state: ?*c.lua_State) callconv(.c) c_int { | ||
| 1095 | 1104 | return 0; |
| 1096 | 1105 | } |
| 1097 | 1106 | |
| 1107 | +fn luaSetAttachMethod(state: ?*c.lua_State) callconv(.c) c_int { | |
| 1108 | + const cfg = config orelse return 0; | |
| 1109 | + const s = state orelse return 0; | |
| 1110 | + const name = getStringArg(s, 1) orelse return 0; | |
| 1111 | + if (config_mod.AttachMethods.fromString(name) == null) { | |
| 1112 | + std.debug.print("set_attach_method: unknown attach method '{s}'\n", .{name}); | |
| 1113 | + return 0; | |
| 1114 | + } | |
| 1115 | + if (dupeLuaString(s, 1)) |attach_method| { | |
| 1116 | + cfg.attach_method = attach_method; | |
| 1117 | + } | |
| 1118 | + return 0; | |
| 1119 | +} | |
| 1120 | + | |
| 1098 | 1121 | fn luaSetModkey(state: ?*c.lua_State) callconv(.c) c_int { |
| 1099 | 1122 | const cfg = config orelse return 0; |
| 1100 | 1123 | const s = state orelse return 0; |
| @@ -1395,6 +1418,7 @@ fn parseAction(name: []const u8) ?Action { | ||
| 1395 | 1418 | .{ "ToggleBar", Action.toggle_bar }, |
| 1396 | 1419 | .{ "CycleLayout", Action.cycle_layout }, |
| 1397 | 1420 | .{ "ChangeLayout", Action.set_layout }, |
| 1421 | + .{ "ChangeAttachMethod", Action.set_attach_method }, | |
| 1398 | 1422 | .{ "ViewTag", Action.view_tag }, |
| 1399 | 1423 | .{ "ViewNextTag", Action.view_next_tag }, |
| 1400 | 1424 | .{ "ViewPreviousTag", Action.view_prev_tag }, |
+1 −0 src/monitor.zig
| @@ -41,6 +41,7 @@ pub const Monitor = struct { | ||
| 41 | 41 | scroll_offset: i32 = 0, |
| 42 | 42 | sel_tags: u32 = 0, |
| 43 | 43 | sel_lt: u32 = 0, |
| 44 | + att_m: u32 = 0, | |
| 44 | 45 | tagset: [2]u32 = .{ 1, 1 }, |
| 45 | 46 | show_bar: bool = true, |
| 46 | 47 | top_bar: bool = true, |
+13 −0 src/wm/actions.zig
| @@ -440,6 +440,18 @@ 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 monitor = wm.selected_monitor orelse return; | |
| 445 | + const name = attach_method_name orelse return; | |
| 446 | + const new_att_m: u32 = if (config_mod.AttachMethods.fromString(name)) |value| | |
| 447 | + @intFromEnum(value) | |
| 448 | + else { | |
| 449 | + std.debug.print("set_attach_method: unknown attach method '{s}'\n", .{name}); | |
| 450 | + return; | |
| 451 | + }; | |
| 452 | + monitor.att_m = new_att_m; | |
| 453 | +} | |
| 454 | + | |
| 443 | 455 | fn warpCursorToMonitor(monitor: *Monitor, wm: *WindowManager) void { |
| 444 | 456 | const center_x = monitor.win_x + @divTrunc(monitor.win_w, 2); |
| 445 | 457 | const center_y = monitor.win_y + @divTrunc(monitor.win_h, 2); |
| @@ -832,6 +844,7 @@ pub fn executeAction(action: config_mod.Action, int_arg: i32, str_arg: ?[]const | ||
| 832 | 844 | .set_layout => setLayout(str_arg, wm), |
| 833 | 845 | .set_layout_tiling => setLayoutIndex(0, wm), |
| 834 | 846 | .set_layout_floating => setLayoutIndex(2, wm), |
| 847 | + .set_attach_method => setAttachMethod(str_arg, wm), | |
| 835 | 848 | .view_tag => { |
| 836 | 849 | const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg); |
| 837 | 850 | core.view(tag_mask, wm); |
+14 −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 | + attach(client, monitor.att_m); | |
| 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,6 +267,19 @@ 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 | + | |
| 270 | 283 | pub fn showhide(monitor: *Monitor, wm: *WindowManager) void { |
| 271 | 284 | showhideClient(monitor.stack, wm); |
| 272 | 285 | } |
+4 −0 src/wm/wm.zig
| @@ -194,6 +194,10 @@ pub const WindowManager = struct { | ||
| 194 | 194 | mon.sel_lt = @intFromEnum(value); |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | + if (config_mod.AttachMethods.fromString(self.config.attach_method)) |value| { | |
| 198 | + mon.att_m = @intFromEnum(value); | |
| 199 | + } | |
| 200 | + | |
| 197 | 201 | for (0..10) |i| { |
| 198 | 202 | for (0..layouts.len) |j| { |
| 199 | 203 | mon.pertag.ltidxs[i][j] = mon.lt[j]; |
+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("top") | |
| 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 |