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