| 1 | const std = @import("std"); |
| 2 | |
| 3 | const client_mod = @import("../client.zig"); |
| 4 | const config_mod = @import("../config/config.zig"); |
| 5 | const lua = @import("../config/lua.zig"); |
| 6 | const core = @import("core.zig"); |
| 7 | const tiling = @import("../layouts/tiling.zig"); |
| 8 | const monitor_mod = @import("../monitor.zig"); |
| 9 | const bar_mod = @import("../bar/bar.zig"); |
| 10 | const wm_mod = @import("wm.zig"); |
| 11 | const xlib = @import("../x11/xlib.zig"); |
| 12 | |
| 13 | const Client = client_mod.Client; |
| 14 | const Monitor = monitor_mod.Monitor; |
| 15 | const WindowManager = wm_mod.WindowManager; |
| 16 | |
| 17 | const snap_distance: i32 = 32; |
| 18 | |
| 19 | // execvpeZ removed in zig 0.16 std lib rework (codeberg.org/ziglang/zig/issues/31694) with no replacement |
| 20 | extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int; |
| 21 | |
| 22 | pub fn spawnChildSetup(wm: *WindowManager) void { |
| 23 | _ = std.c.setsid(); |
| 24 | if (wm.x11_fd >= 0) _ = std.c.close(@intCast(wm.x11_fd)); |
| 25 | const sigchld_handler = std.posix.Sigaction{ |
| 26 | .handler = .{ .handler = std.posix.SIG.DFL }, |
| 27 | .mask = std.mem.zeroes(std.posix.sigset_t), |
| 28 | .flags = 0, |
| 29 | }; |
| 30 | std.posix.sigaction(.CHLD, &sigchld_handler, null); |
| 31 | } |
| 32 | |
| 33 | pub fn spawnCommand(wm: *WindowManager, cmd: []const u8) void { |
| 34 | std.debug.print("running cmd: {s}\n", .{cmd}); |
| 35 | const pid = std.c.fork(); |
| 36 | if (pid < 0) return; |
| 37 | if (pid == 0) { |
| 38 | const grandchild = std.c.fork(); |
| 39 | if (grandchild < 0) std.c._exit(1); |
| 40 | if (grandchild != 0) std.c._exit(0); |
| 41 | spawnChildSetup(wm); |
| 42 | var cmd_buf: [1024]u8 = undefined; |
| 43 | if (cmd.len >= cmd_buf.len) std.c._exit(1); |
| 44 | @memcpy(cmd_buf[0..cmd.len], cmd); |
| 45 | cmd_buf[cmd.len] = 0; |
| 46 | const argv = [_:null]?[*:0]const u8{ "sh", "-c", @ptrCast(&cmd_buf) }; |
| 47 | _ = execvp("sh", &argv); |
| 48 | std.c._exit(1); |
| 49 | } |
| 50 | _ = std.c.waitpid(pid, null, 0); |
| 51 | } |
| 52 | |
| 53 | pub fn spawnTerminal(wm: *WindowManager) void { |
| 54 | const pid = std.c.fork(); |
| 55 | if (pid < 0) return; |
| 56 | if (pid == 0) { |
| 57 | const grandchild = std.c.fork(); |
| 58 | if (grandchild < 0) std.c._exit(1); |
| 59 | if (grandchild != 0) std.c._exit(0); |
| 60 | spawnChildSetup(wm); |
| 61 | var term_buf: [256]u8 = undefined; |
| 62 | const terminal = wm.config.terminal; |
| 63 | if (terminal.len >= term_buf.len) std.c._exit(1); |
| 64 | @memcpy(term_buf[0..terminal.len], terminal); |
| 65 | term_buf[terminal.len] = 0; |
| 66 | const term_ptr: [*:0]const u8 = @ptrCast(&term_buf); |
| 67 | const argv = [_:null]?[*:0]const u8{term_ptr}; |
| 68 | _ = execvp(term_ptr, &argv); |
| 69 | std.c._exit(1); |
| 70 | } |
| 71 | _ = std.c.waitpid(pid, null, 0); |
| 72 | } |
| 73 | |
| 74 | pub fn movestack(direction: i32, wm: *WindowManager) void { |
| 75 | if (direction == 0) return; |
| 76 | const monitor = wm.selected_monitor orelse return; |
| 77 | const current = monitor.sel orelse return; |
| 78 | |
| 79 | if (current.is_floating) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | var target: ?*Client = null; |
| 84 | |
| 85 | if (direction > 0) { |
| 86 | target = current.next; |
| 87 | while (target) |client| { |
| 88 | if (client_mod.isVisible(client) and !client.is_floating) { |
| 89 | break; |
| 90 | } |
| 91 | target = client.next; |
| 92 | } |
| 93 | if (target == null) { |
| 94 | target = monitor.clients; |
| 95 | while (target) |client| { |
| 96 | if (client == current) { |
| 97 | break; |
| 98 | } |
| 99 | if (client_mod.isVisible(client) and !client.is_floating) { |
| 100 | break; |
| 101 | } |
| 102 | target = client.next; |
| 103 | } |
| 104 | } |
| 105 | } else { |
| 106 | var prev: ?*Client = null; |
| 107 | var iter = monitor.clients; |
| 108 | while (iter) |client| { |
| 109 | if (client == current) { |
| 110 | break; |
| 111 | } |
| 112 | if (client_mod.isVisible(client) and !client.is_floating) { |
| 113 | prev = client; |
| 114 | } |
| 115 | iter = client.next; |
| 116 | } |
| 117 | if (prev == null) { |
| 118 | iter = current.next; |
| 119 | while (iter) |client| { |
| 120 | if (client_mod.isVisible(client) and !client.is_floating) { |
| 121 | prev = client; |
| 122 | } |
| 123 | iter = client.next; |
| 124 | } |
| 125 | } |
| 126 | target = prev; |
| 127 | } |
| 128 | |
| 129 | if (target) |swap_client| { |
| 130 | if (swap_client != current) { |
| 131 | client_mod.swapClients(current, swap_client); |
| 132 | core.arrange(monitor, wm); |
| 133 | core.focus(current, wm); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | pub fn toggleView(tag_mask: u32, wm: *WindowManager) void { |
| 139 | const monitor = wm.selected_monitor orelse return; |
| 140 | const new_tags = monitor.tagset[monitor.sel_tags] ^ tag_mask; |
| 141 | if (new_tags != 0) { |
| 142 | monitor.tagset[monitor.sel_tags] = new_tags; |
| 143 | |
| 144 | if (new_tags == ~@as(u32, 0)) { |
| 145 | monitor.pertag.prevtag = monitor.pertag.curtag; |
| 146 | monitor.pertag.curtag = 0; |
| 147 | } |
| 148 | |
| 149 | if ((new_tags & (@as(u32, 1) << @intCast(monitor.pertag.curtag -| 1))) == 0) { |
| 150 | monitor.pertag.prevtag = monitor.pertag.curtag; |
| 151 | var i: u32 = 0; |
| 152 | while (i < wm.config.tag_count) : (i += 1) { |
| 153 | if ((new_tags & (@as(u32, 1) << @intCast(i))) != 0) break; |
| 154 | } |
| 155 | monitor.pertag.curtag = i + 1; |
| 156 | } |
| 157 | |
| 158 | monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag]; |
| 159 | monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag]; |
| 160 | monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag]; |
| 161 | |
| 162 | const new_show_bar = monitor.pertag.showbars[monitor.pertag.curtag]; |
| 163 | if (new_show_bar != monitor.show_bar) { |
| 164 | monitor.show_bar = new_show_bar; |
| 165 | updateBarVisibility(monitor, wm); |
| 166 | } |
| 167 | |
| 168 | core.focusTopClient(monitor, wm); |
| 169 | core.arrange(monitor, wm); |
| 170 | wm.invalidateBars(); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | pub fn toggleClientTag(tag_mask: u32, wm: *WindowManager) void { |
| 175 | const monitor = wm.selected_monitor orelse return; |
| 176 | const client = monitor.sel orelse return; |
| 177 | const new_tags = client.tags ^ tag_mask; |
| 178 | if (new_tags != 0) { |
| 179 | client.tags = new_tags; |
| 180 | core.focusTopClient(monitor, wm); |
| 181 | core.arrange(monitor, wm); |
| 182 | wm.invalidateBars(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | pub fn toggleGaps(wm: *WindowManager) void { |
| 187 | const monitor = wm.selected_monitor orelse return; |
| 188 | if (monitor.gap_inner_h == 0) { |
| 189 | monitor.gap_inner_h = wm.config.gap_inner_h; |
| 190 | monitor.gap_inner_v = wm.config.gap_inner_v; |
| 191 | monitor.gap_outer_h = wm.config.gap_outer_h; |
| 192 | monitor.gap_outer_v = wm.config.gap_outer_v; |
| 193 | } else { |
| 194 | monitor.gap_inner_h = 0; |
| 195 | monitor.gap_inner_v = 0; |
| 196 | monitor.gap_outer_h = 0; |
| 197 | monitor.gap_outer_v = 0; |
| 198 | } |
| 199 | core.arrange(monitor, wm); |
| 200 | } |
| 201 | |
| 202 | pub fn updateBarVisibility(monitor: *Monitor, wm: *WindowManager) void { |
| 203 | const bar = bar_mod.windowToBar(wm.bars, monitor.bar_win) orelse return; |
| 204 | |
| 205 | if (monitor.show_bar) { |
| 206 | _ = xlib.XMapWindow(wm.display.handle, bar.window); |
| 207 | monitor.win_h -= bar.height; |
| 208 | if (std.mem.eql(u8, wm.config.bar_position, "top")) { |
| 209 | monitor.win_y += bar.height; |
| 210 | } |
| 211 | } else { |
| 212 | _ = xlib.XUnmapWindow(wm.display.handle, bar.window); |
| 213 | monitor.win_h += bar.height; |
| 214 | if (std.mem.eql(u8, wm.config.bar_position, "top")) { |
| 215 | monitor.win_y -= bar.height; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | pub fn toggleBar(wm: *WindowManager) void { |
| 221 | const monitor = wm.selected_monitor orelse return; |
| 222 | monitor.show_bar = !monitor.show_bar; |
| 223 | monitor.pertag.showbars[monitor.pertag.curtag] = monitor.show_bar; |
| 224 | |
| 225 | updateBarVisibility(monitor, wm); |
| 226 | |
| 227 | core.arrange(monitor, wm); |
| 228 | wm.invalidateBars(); |
| 229 | } |
| 230 | |
| 231 | pub fn killFocused(wm: *WindowManager) void { |
| 232 | const selected = wm.selected_monitor orelse return; |
| 233 | const client = selected.sel orelse return; |
| 234 | std.debug.print("killing window: 0x{x}\n", .{client.window}); |
| 235 | |
| 236 | if (!core.sendEvent(client, wm.atoms.wm_delete, wm)) { |
| 237 | _ = xlib.XGrabServer(wm.display.handle); |
| 238 | _ = xlib.XKillClient(wm.display.handle, client.window); |
| 239 | _ = xlib.XSync(wm.display.handle, xlib.False); |
| 240 | _ = xlib.XUngrabServer(wm.display.handle); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | pub fn toggleFullscreen(wm: *WindowManager) void { |
| 245 | const selected = wm.selected_monitor orelse return; |
| 246 | const client = selected.sel orelse return; |
| 247 | core.setFullscreen(client, !client.is_fullscreen, wm); |
| 248 | } |
| 249 | |
| 250 | pub fn viewAdjacentTag(direction: i32, wm: *WindowManager) void { |
| 251 | const monitor = wm.selected_monitor orelse return; |
| 252 | const current_tag = monitor.pertag.curtag; |
| 253 | const max_tag: i32 = @intCast(wm.config.tag_count); |
| 254 | var new_tag: i32 = @intCast(current_tag); |
| 255 | |
| 256 | new_tag += direction; |
| 257 | if (new_tag < 1) new_tag = max_tag; |
| 258 | if (new_tag > max_tag) new_tag = 1; |
| 259 | |
| 260 | const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1); |
| 261 | core.view(tag_mask, wm); |
| 262 | } |
| 263 | |
| 264 | pub fn viewAdjacentNonemptyTag(direction: i32, wm: *WindowManager) void { |
| 265 | const monitor = wm.selected_monitor orelse return; |
| 266 | const current_tag = monitor.pertag.curtag; |
| 267 | const max_tag: i32 = @intCast(wm.config.tag_count); |
| 268 | var new_tag: i32 = @intCast(current_tag); |
| 269 | |
| 270 | var attempts: i32 = 0; |
| 271 | while (attempts < max_tag) : (attempts += 1) { |
| 272 | new_tag += direction; |
| 273 | if (new_tag < 1) new_tag = max_tag; |
| 274 | if (new_tag > max_tag) new_tag = 1; |
| 275 | |
| 276 | const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1); |
| 277 | if (core.hasClientsOnTag(monitor, tag_mask)) { |
| 278 | core.view(tag_mask, wm); |
| 279 | return; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | pub fn tagClient(tag_mask: u32, wm: *WindowManager) void { |
| 285 | const monitor = wm.selected_monitor orelse return; |
| 286 | const client = monitor.sel orelse return; |
| 287 | if (tag_mask == 0) { |
| 288 | return; |
| 289 | } |
| 290 | client.tags = tag_mask; |
| 291 | core.focusTopClient(monitor, wm); |
| 292 | core.arrange(monitor, wm); |
| 293 | wm.invalidateBars(); |
| 294 | std.debug.print("tag_client: window=0x{x} tag_mask={d}\n", .{ client.window, tag_mask }); |
| 295 | } |
| 296 | |
| 297 | pub fn focusstack(direction: i32, wm: *WindowManager) void { |
| 298 | if (direction == 0) return; |
| 299 | const monitor = wm.selected_monitor orelse return; |
| 300 | const current = monitor.sel orelse return; |
| 301 | |
| 302 | var next_client: ?*Client = null; |
| 303 | |
| 304 | if (direction > 0) { |
| 305 | next_client = current.next; |
| 306 | while (next_client) |client| { |
| 307 | if (client_mod.isVisible(client)) { |
| 308 | break; |
| 309 | } |
| 310 | next_client = client.next; |
| 311 | } |
| 312 | if (next_client == null) { |
| 313 | next_client = monitor.clients; |
| 314 | while (next_client) |client| { |
| 315 | if (client_mod.isVisible(client)) { |
| 316 | break; |
| 317 | } |
| 318 | next_client = client.next; |
| 319 | } |
| 320 | } |
| 321 | } else { |
| 322 | var prev: ?*Client = null; |
| 323 | var iter = monitor.clients; |
| 324 | while (iter) |client| { |
| 325 | if (client == current) { |
| 326 | break; |
| 327 | } |
| 328 | if (client_mod.isVisible(client)) { |
| 329 | prev = client; |
| 330 | } |
| 331 | iter = client.next; |
| 332 | } |
| 333 | if (prev == null) { |
| 334 | iter = current.next; |
| 335 | while (iter) |client| { |
| 336 | if (client_mod.isVisible(client)) { |
| 337 | prev = client; |
| 338 | } |
| 339 | iter = client.next; |
| 340 | } |
| 341 | } |
| 342 | next_client = prev; |
| 343 | } |
| 344 | |
| 345 | if (next_client) |client| { |
| 346 | core.focus(client, wm); |
| 347 | if (client.monitor) |client_monitor| { |
| 348 | core.restack(client_monitor, wm); |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | pub fn toggleFloating(wm: *WindowManager) void { |
| 354 | const monitor = wm.selected_monitor orelse return; |
| 355 | const client = monitor.sel orelse return; |
| 356 | |
| 357 | if (client.is_fullscreen) { |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | client.is_floating = !client.is_floating; |
| 362 | |
| 363 | if (client.is_floating) { |
| 364 | tiling.resize(client, client.x, client.y, client.width, client.height, false); |
| 365 | } |
| 366 | |
| 367 | core.arrange(monitor, wm); |
| 368 | std.debug.print("toggle_floating: window=0x{x} floating={}\n", .{ client.window, client.is_floating }); |
| 369 | } |
| 370 | |
| 371 | pub fn incnmaster(delta: i32, wm: *WindowManager) void { |
| 372 | const monitor = wm.selected_monitor orelse return; |
| 373 | const new_val = @max(0, monitor.nmaster + delta); |
| 374 | monitor.nmaster = new_val; |
| 375 | monitor.pertag.nmasters[monitor.pertag.curtag] = new_val; |
| 376 | core.arrange(monitor, wm); |
| 377 | std.debug.print("incnmaster: nmaster={d}\n", .{monitor.nmaster}); |
| 378 | } |
| 379 | |
| 380 | pub fn setmfact(delta: f32, wm: *WindowManager) void { |
| 381 | const monitor = wm.selected_monitor orelse return; |
| 382 | const new_mfact = monitor.mfact + delta; |
| 383 | if (new_mfact < 0.05 or new_mfact > 0.95) { |
| 384 | return; |
| 385 | } |
| 386 | monitor.mfact = new_mfact; |
| 387 | monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact; |
| 388 | core.arrange(monitor, wm); |
| 389 | std.debug.print("setmfact: mfact={d:.2}\n", .{monitor.mfact}); |
| 390 | } |
| 391 | |
| 392 | pub fn cycleLayout(wm: *WindowManager) void { |
| 393 | const monitor = wm.selected_monitor orelse return; |
| 394 | const new_lt = (monitor.sel_lt + 1) % @as(u32, @intCast(std.meta.fields(config_mod.Layouts).len)); |
| 395 | monitor.sel_lt = new_lt; |
| 396 | monitor.pertag.sellts[monitor.pertag.curtag] = new_lt; |
| 397 | if (new_lt != @intFromEnum(config_mod.Layouts.scrolling)) { |
| 398 | monitor.scroll_offset = 0; |
| 399 | } |
| 400 | core.arrange(monitor, wm); |
| 401 | wm.invalidateBars(); |
| 402 | if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| { |
| 403 | std.debug.print("cycle_layout: {s}\n", .{layout.symbol}); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | pub fn setLayout(layout_name: ?[]const u8, wm: *WindowManager) void { |
| 408 | const monitor = wm.selected_monitor orelse return; |
| 409 | const name = layout_name orelse return; |
| 410 | const new_lt: u32 = if (config_mod.Layouts.fromString(name)) |value| |
| 411 | @intFromEnum(value) |
| 412 | else { |
| 413 | std.debug.print("set_layout: unknown layout '{s}'\n", .{name}); |
| 414 | return; |
| 415 | }; |
| 416 | monitor.sel_lt = new_lt; |
| 417 | monitor.pertag.sellts[monitor.pertag.curtag] = new_lt; |
| 418 | if (new_lt != @intFromEnum(config_mod.Layouts.scrolling)) { |
| 419 | monitor.scroll_offset = 0; |
| 420 | } |
| 421 | core.arrange(monitor, wm); |
| 422 | wm.invalidateBars(); |
| 423 | if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| { |
| 424 | std.debug.print("set_layout: {s}\n", .{layout.symbol}); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | pub fn setLayoutIndex(index: u32, wm: *WindowManager) void { |
| 429 | const monitor = wm.selected_monitor orelse return; |
| 430 | const lt_index = index % @as(u32, @intCast(std.meta.fields(config_mod.Layouts).len)); |
| 431 | monitor.sel_lt = lt_index; |
| 432 | monitor.pertag.sellts[monitor.pertag.curtag] = lt_index; |
| 433 | if (lt_index != @intFromEnum(config_mod.Layouts.scrolling)) { |
| 434 | monitor.scroll_offset = 0; |
| 435 | } |
| 436 | core.arrange(monitor, wm); |
| 437 | wm.invalidateBars(); |
| 438 | if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| { |
| 439 | std.debug.print("set_layout_index: {s}\n", .{layout.symbol}); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | fn warpCursorToMonitor(monitor: *Monitor, wm: *WindowManager) void { |
| 444 | const center_x = monitor.win_x + @divTrunc(monitor.win_w, 2); |
| 445 | const center_y = monitor.win_y + @divTrunc(monitor.win_h, 2); |
| 446 | _ = xlib.XWarpPointer(wm.display.handle, xlib.None, wm.display.root, 0, 0, 0, 0, center_x, center_y); |
| 447 | } |
| 448 | |
| 449 | pub fn focusmon(direction: i32, wm: *WindowManager) void { |
| 450 | const selmon = wm.selected_monitor orelse return; |
| 451 | const target = monitor_mod.dirToMonitor(wm, direction) orelse return; |
| 452 | if (target == selmon) { |
| 453 | return; |
| 454 | } |
| 455 | core.unfocusClient(selmon.sel, false, wm); |
| 456 | wm.selected_monitor = target; |
| 457 | core.focus(null, wm); |
| 458 | if (wm.config.warp_cursor_to_monitor) { |
| 459 | warpCursorToMonitor(target, wm); |
| 460 | } |
| 461 | std.debug.print("focusmon: monitor {d}\n", .{target.num}); |
| 462 | } |
| 463 | |
| 464 | pub fn sendmon(direction: i32, wm: *WindowManager) void { |
| 465 | const source_monitor = wm.selected_monitor orelse return; |
| 466 | const client = source_monitor.sel orelse return; |
| 467 | const target = monitor_mod.dirToMonitor(wm, direction) orelse return; |
| 468 | |
| 469 | if (target == source_monitor) { |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | client_mod.detach(client); |
| 474 | client_mod.detachStack(client); |
| 475 | client.monitor = target; |
| 476 | client.tags = target.tagset[target.sel_tags]; |
| 477 | client_mod.attachAside(client); |
| 478 | client_mod.attachStack(client); |
| 479 | |
| 480 | core.focusTopClient(source_monitor, wm); |
| 481 | core.arrange(source_monitor, wm); |
| 482 | core.arrange(target, wm); |
| 483 | |
| 484 | if (wm.config.warp_cursor_on_send) { |
| 485 | warpCursorToMonitor(target, wm); |
| 486 | } |
| 487 | |
| 488 | std.debug.print("sendmon: window=0x{x} to monitor {d}\n", .{ client.window, target.num }); |
| 489 | } |
| 490 | |
| 491 | pub fn snapX(client: *Client, new_x: i32, monitor: *Monitor) i32 { |
| 492 | const client_width = client.width + 2 * client.border_width; |
| 493 | if (@abs(monitor.win_x - new_x) < snap_distance) { |
| 494 | return monitor.win_x; |
| 495 | } else if (@abs((monitor.win_x + monitor.win_w) - (new_x + client_width)) < snap_distance) { |
| 496 | return monitor.win_x + monitor.win_w - client_width; |
| 497 | } |
| 498 | return new_x; |
| 499 | } |
| 500 | |
| 501 | pub fn snapY(client: *Client, new_y: i32, monitor: *Monitor) i32 { |
| 502 | const client_height = client.height + 2 * client.border_width; |
| 503 | if (@abs(monitor.win_y - new_y) < snap_distance) { |
| 504 | return monitor.win_y; |
| 505 | } else if (@abs((monitor.win_y + monitor.win_h) - (new_y + client_height)) < snap_distance) { |
| 506 | return monitor.win_y + monitor.win_h - client_height; |
| 507 | } |
| 508 | return new_y; |
| 509 | } |
| 510 | |
| 511 | pub fn movemouse(wm: *WindowManager) void { |
| 512 | const monitor = wm.selected_monitor orelse return; |
| 513 | const client = monitor.sel orelse return; |
| 514 | |
| 515 | if (client.is_fullscreen) { |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | core.restack(monitor, wm); |
| 520 | |
| 521 | const was_floating = client.is_floating; |
| 522 | if (!client.is_floating) { |
| 523 | client.is_floating = true; |
| 524 | } |
| 525 | |
| 526 | var root_x: c_int = undefined; |
| 527 | var root_y: c_int = undefined; |
| 528 | var dummy_win: xlib.Window = undefined; |
| 529 | var dummy_int: c_int = undefined; |
| 530 | var dummy_uint: c_uint = undefined; |
| 531 | |
| 532 | _ = xlib.XQueryPointer(wm.display.handle, wm.display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint); |
| 533 | |
| 534 | const grab_result = xlib.XGrabPointer( |
| 535 | wm.display.handle, |
| 536 | wm.display.root, |
| 537 | xlib.False, |
| 538 | xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask, |
| 539 | xlib.GrabModeAsync, |
| 540 | xlib.GrabModeAsync, |
| 541 | xlib.None, |
| 542 | wm.cursors.move, |
| 543 | xlib.CurrentTime, |
| 544 | ); |
| 545 | |
| 546 | if (grab_result != xlib.GrabSuccess) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | const start_x = client.x; |
| 551 | const start_y = client.y; |
| 552 | const pointer_start_x = root_x; |
| 553 | const pointer_start_y = root_y; |
| 554 | var last_time: c_ulong = 0; |
| 555 | |
| 556 | var event: xlib.XEvent = undefined; |
| 557 | var done = false; |
| 558 | |
| 559 | while (!done) { |
| 560 | _ = xlib.XNextEvent(wm.display.handle, &event); |
| 561 | |
| 562 | switch (event.type) { |
| 563 | xlib.MotionNotify => { |
| 564 | const motion = &event.xmotion; |
| 565 | if ((motion.time - last_time) < (1000 / 60)) { |
| 566 | continue; |
| 567 | } |
| 568 | last_time = motion.time; |
| 569 | const delta_x = motion.x_root - pointer_start_x; |
| 570 | const delta_y = motion.y_root - pointer_start_y; |
| 571 | var new_x = start_x + delta_x; |
| 572 | var new_y = start_y + delta_y; |
| 573 | if (client.monitor) |client_monitor| { |
| 574 | new_x = snapX(client, new_x, client_monitor); |
| 575 | new_y = snapY(client, new_y, client_monitor); |
| 576 | } |
| 577 | tiling.resize(client, new_x, new_y, client.width, client.height, true); |
| 578 | }, |
| 579 | xlib.ButtonRelease => { |
| 580 | done = true; |
| 581 | }, |
| 582 | else => {}, |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime); |
| 587 | |
| 588 | const new_mon = monitor_mod.rectToMonitor(wm, client.x, client.y, client.width, client.height); |
| 589 | if (new_mon != null and new_mon != monitor) { |
| 590 | client_mod.detach(client); |
| 591 | client_mod.detachStack(client); |
| 592 | client.monitor = new_mon; |
| 593 | client.tags = new_mon.?.tagset[new_mon.?.sel_tags]; |
| 594 | client_mod.attachAside(client); |
| 595 | client_mod.attachStack(client); |
| 596 | wm.selected_monitor = new_mon; |
| 597 | core.focus(client, wm); |
| 598 | core.arrange(monitor, wm); |
| 599 | core.arrange(new_mon.?, wm); |
| 600 | } else { |
| 601 | core.arrange(monitor, wm); |
| 602 | } |
| 603 | |
| 604 | if (wm.config.auto_tile and !was_floating) { |
| 605 | const drop_monitor = client.monitor orelse return; |
| 606 | const center_x = client.x + @divTrunc(client.width, 2); |
| 607 | const center_y = client.y + @divTrunc(client.height, 2); |
| 608 | |
| 609 | if (client_mod.tiledWindowAt(client, drop_monitor, center_x, center_y)) |target| { |
| 610 | client_mod.insertBefore(client, target); |
| 611 | } |
| 612 | |
| 613 | client.is_floating = false; |
| 614 | core.arrange(drop_monitor, wm); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | pub fn resizemouse(wm: *WindowManager) void { |
| 619 | const monitor = wm.selected_monitor orelse return; |
| 620 | const client = monitor.sel orelse return; |
| 621 | |
| 622 | if (client.is_fullscreen) { |
| 623 | return; |
| 624 | } |
| 625 | |
| 626 | core.restack(monitor, wm); |
| 627 | |
| 628 | // If tiled_resize_mode is enabled and window is tiled, adjust mfact instead |
| 629 | if (wm.config.tiled_resize_mode and !client.is_floating) { |
| 630 | resizemouseTiled(wm, monitor); |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | if (!client.is_floating) { |
| 635 | client.is_floating = true; |
| 636 | } |
| 637 | |
| 638 | const grab_result = xlib.XGrabPointer( |
| 639 | wm.display.handle, |
| 640 | wm.display.root, |
| 641 | xlib.False, |
| 642 | xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask, |
| 643 | xlib.GrabModeAsync, |
| 644 | xlib.GrabModeAsync, |
| 645 | xlib.None, |
| 646 | wm.cursors.resize, |
| 647 | xlib.CurrentTime, |
| 648 | ); |
| 649 | |
| 650 | if (grab_result != xlib.GrabSuccess) { |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | _ = xlib.XWarpPointer(wm.display.handle, xlib.None, client.window, 0, 0, 0, 0, client.width + client.border_width - 1, client.height + client.border_width - 1); |
| 655 | |
| 656 | var event: xlib.XEvent = undefined; |
| 657 | var done = false; |
| 658 | var last_time: c_ulong = 0; |
| 659 | |
| 660 | while (!done) { |
| 661 | _ = xlib.XNextEvent(wm.display.handle, &event); |
| 662 | |
| 663 | switch (event.type) { |
| 664 | xlib.MotionNotify => { |
| 665 | const motion = &event.xmotion; |
| 666 | if ((motion.time - last_time) < (1000 / 60)) { |
| 667 | continue; |
| 668 | } |
| 669 | last_time = motion.time; |
| 670 | var new_width = @max(1, motion.x_root - client.x - 2 * client.border_width + 1); |
| 671 | var new_height = @max(1, motion.y_root - client.y - 2 * client.border_width + 1); |
| 672 | if (client.monitor) |client_monitor| { |
| 673 | const client_right = client.x + new_width + 2 * client.border_width; |
| 674 | const client_bottom = client.y + new_height + 2 * client.border_width; |
| 675 | const mon_right = client_monitor.win_x + client_monitor.win_w; |
| 676 | const mon_bottom = client_monitor.win_y + client_monitor.win_h; |
| 677 | if (@abs(mon_right - client_right) < snap_distance) { |
| 678 | new_width = @max(1, mon_right - client.x - 2 * client.border_width); |
| 679 | } |
| 680 | if (@abs(mon_bottom - client_bottom) < snap_distance) { |
| 681 | new_height = @max(1, mon_bottom - client.y - 2 * client.border_width); |
| 682 | } |
| 683 | } |
| 684 | tiling.resize(client, client.x, client.y, new_width, new_height, true); |
| 685 | }, |
| 686 | xlib.ButtonRelease => { |
| 687 | done = true; |
| 688 | }, |
| 689 | else => {}, |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime); |
| 694 | core.arrange(monitor, wm); |
| 695 | } |
| 696 | |
| 697 | /// Resize tiled windows by adjusting mfact with mouse drag (like Super+H/L but with mouse) |
| 698 | fn resizemouseTiled(wm: *WindowManager, monitor: *Monitor) void { |
| 699 | const grab_result = xlib.XGrabPointer( |
| 700 | wm.display.handle, |
| 701 | wm.display.root, |
| 702 | xlib.False, |
| 703 | xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask, |
| 704 | xlib.GrabModeAsync, |
| 705 | xlib.GrabModeAsync, |
| 706 | xlib.None, |
| 707 | wm.cursors.resize, |
| 708 | xlib.CurrentTime, |
| 709 | ); |
| 710 | |
| 711 | if (grab_result != xlib.GrabSuccess) { |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | // Get initial pointer position |
| 716 | var root_return: xlib.Window = undefined; |
| 717 | var child_return: xlib.Window = undefined; |
| 718 | var root_x: c_int = undefined; |
| 719 | var root_y: c_int = undefined; |
| 720 | var win_x: c_int = undefined; |
| 721 | var win_y: c_int = undefined; |
| 722 | var mask_return: c_uint = undefined; |
| 723 | |
| 724 | _ = xlib.XQueryPointer( |
| 725 | wm.display.handle, |
| 726 | wm.display.root, |
| 727 | &root_return, |
| 728 | &child_return, |
| 729 | &root_x, |
| 730 | &root_y, |
| 731 | &win_x, |
| 732 | &win_y, |
| 733 | &mask_return, |
| 734 | ); |
| 735 | |
| 736 | const start_x = root_x; |
| 737 | const initial_mfact = monitor.mfact; |
| 738 | |
| 739 | const total_width: f32 = @floatFromInt(monitor.win_w - 2 * monitor.gap_outer_v - monitor.gap_inner_v); |
| 740 | |
| 741 | var event: xlib.XEvent = undefined; |
| 742 | var done = false; |
| 743 | var last_time: c_ulong = 0; |
| 744 | |
| 745 | while (!done) { |
| 746 | _ = xlib.XNextEvent(wm.display.handle, &event); |
| 747 | |
| 748 | switch (event.type) { |
| 749 | xlib.MotionNotify => { |
| 750 | const motion = &event.xmotion; |
| 751 | if ((motion.time - last_time) < (1000 / 60)) { |
| 752 | continue; |
| 753 | } |
| 754 | last_time = motion.time; |
| 755 | |
| 756 | const delta_x: f32 = @floatFromInt(motion.x_root - start_x); |
| 757 | const mfact_delta = delta_x / total_width; |
| 758 | const new_mfact = initial_mfact + mfact_delta; |
| 759 | |
| 760 | if (new_mfact >= 0.05 and new_mfact <= 0.95) { |
| 761 | monitor.mfact = new_mfact; |
| 762 | monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact; |
| 763 | core.arrange(monitor, wm); |
| 764 | } |
| 765 | }, |
| 766 | xlib.ButtonRelease => { |
| 767 | done = true; |
| 768 | }, |
| 769 | else => {}, |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime); |
| 774 | } |
| 775 | |
| 776 | /// Config-loading callback for wm.reloadConfig(). |
| 777 | /// Re-initialises Lua and loads the config file (or falls back to defaults). |
| 778 | pub fn reloadLoadConfig(wm: *WindowManager) void { |
| 779 | lua.deinit(); |
| 780 | _ = lua.init(&wm.config); |
| 781 | |
| 782 | const loaded = if (wm.config_path) |path| |
| 783 | lua.loadFile(path) |
| 784 | else |
| 785 | lua.loadConfig(wm.env); |
| 786 | |
| 787 | if (loaded) { |
| 788 | if (wm.config_path) |path| { |
| 789 | std.debug.print("reloaded config from {s}\n", .{path}); |
| 790 | } else { |
| 791 | std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{}); |
| 792 | } |
| 793 | } else { |
| 794 | std.debug.print("reload failed, restoring defaults\n", .{}); |
| 795 | config_mod.initializeDefaultConfig(&wm.config); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | pub fn executeAction(action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8, wm: *WindowManager) void { |
| 800 | switch (action) { |
| 801 | .spawn_terminal => spawnTerminal(wm), |
| 802 | .spawn => { |
| 803 | if (str_arg) |cmd| spawnCommand(wm, cmd); |
| 804 | }, |
| 805 | .kill_client => killFocused(wm), |
| 806 | .quit => { |
| 807 | std.debug.print("quit keybind pressed\n", .{}); |
| 808 | wm.running = false; |
| 809 | }, |
| 810 | .reload_config => wm.reloadConfig(reloadLoadConfig), |
| 811 | .restart => wm.reloadConfig(reloadLoadConfig), |
| 812 | .show_keybinds => { |
| 813 | if (wm.overlay) |overlay| { |
| 814 | const mon = wm.selected_monitor orelse wm.monitors; |
| 815 | if (mon) |m| { |
| 816 | overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h, &wm.config); |
| 817 | } |
| 818 | } |
| 819 | }, |
| 820 | .focus_next => focusstack(if (int_arg == 0) 1 else int_arg, wm), |
| 821 | .focus_prev => focusstack(-1, wm), |
| 822 | .move_next => movestack(if (int_arg == 0) 1 else int_arg, wm), |
| 823 | .move_prev => movestack(-1, wm), |
| 824 | .resize_master => setmfact(@as(f32, @floatFromInt(int_arg)) / 1000.0, wm), |
| 825 | .inc_master => incnmaster(1, wm), |
| 826 | .dec_master => incnmaster(-1, wm), |
| 827 | .toggle_floating => toggleFloating(wm), |
| 828 | .toggle_fullscreen => toggleFullscreen(wm), |
| 829 | .toggle_gaps => toggleGaps(wm), |
| 830 | .toggle_bar => toggleBar(wm), |
| 831 | .cycle_layout => cycleLayout(wm), |
| 832 | .set_layout => setLayout(str_arg, wm), |
| 833 | .set_layout_tiling => setLayoutIndex(0, wm), |
| 834 | .set_layout_floating => setLayoutIndex(2, wm), |
| 835 | .view_tag => { |
| 836 | const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg); |
| 837 | core.view(tag_mask, wm); |
| 838 | }, |
| 839 | .view_next_tag => viewAdjacentTag(1, wm), |
| 840 | .view_prev_tag => viewAdjacentTag(-1, wm), |
| 841 | .view_next_nonempty_tag => viewAdjacentNonemptyTag(1, wm), |
| 842 | .view_prev_nonempty_tag => viewAdjacentNonemptyTag(-1, wm), |
| 843 | .move_to_tag => { |
| 844 | const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg); |
| 845 | tagClient(tag_mask, wm); |
| 846 | }, |
| 847 | .toggle_view_tag => { |
| 848 | const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg); |
| 849 | toggleView(tag_mask, wm); |
| 850 | }, |
| 851 | .toggle_tag => { |
| 852 | const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg); |
| 853 | toggleClientTag(tag_mask, wm); |
| 854 | }, |
| 855 | .focus_monitor => focusmon(int_arg, wm), |
| 856 | .send_to_monitor => sendmon(int_arg, wm), |
| 857 | .scroll_left => core.scrollLayout(-1, wm), |
| 858 | .scroll_right => core.scrollLayout(1, wm), |
| 859 | } |
| 860 | } |