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