tony / oxwm

Branch: dd25e8c
Switch branches/tags
master
tonybanters added comment for context on execvp, with relevant codeberg issue link associated. fixed inconsistency on movestack, and utilized .real vs .awake for clock 4d61920 2026-05-16
845 lines · 27.8 KB Raw Blame History
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 < 9) : (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 var new_tag: i32 = @intCast(current_tag);
254
255 new_tag += direction;
256 if (new_tag < 1) new_tag = 9;
257 if (new_tag > 9) new_tag = 1;
258
259 const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
260 core.view(tag_mask, wm);
261 }
262
263 pub fn viewAdjacentNonemptyTag(direction: i32, wm: *WindowManager) void {
264 const monitor = wm.selected_monitor orelse return;
265 const current_tag = monitor.pertag.curtag;
266 var new_tag: i32 = @intCast(current_tag);
267
268 var attempts: i32 = 0;
269 while (attempts < 9) : (attempts += 1) {
270 new_tag += direction;
271 if (new_tag < 1) new_tag = 9;
272 if (new_tag > 9) new_tag = 1;
273
274 const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
275 if (core.hasClientsOnTag(monitor, tag_mask)) {
276 core.view(tag_mask, wm);
277 return;
278 }
279 }
280 }
281
282 pub fn tagClient(tag_mask: u32, wm: *WindowManager) void {
283 const monitor = wm.selected_monitor orelse return;
284 const client = monitor.sel orelse return;
285 if (tag_mask == 0) {
286 return;
287 }
288 client.tags = tag_mask;
289 core.focusTopClient(monitor, wm);
290 core.arrange(monitor, wm);
291 wm.invalidateBars();
292 std.debug.print("tag_client: window=0x{x} tag_mask={d}\n", .{ client.window, tag_mask });
293 }
294
295 pub fn focusstack(direction: i32, wm: *WindowManager) void {
296 if (direction == 0) return;
297 const monitor = wm.selected_monitor orelse return;
298 const current = monitor.sel orelse return;
299
300 var next_client: ?*Client = null;
301
302 if (direction > 0) {
303 next_client = current.next;
304 while (next_client) |client| {
305 if (client_mod.isVisible(client)) {
306 break;
307 }
308 next_client = client.next;
309 }
310 if (next_client == null) {
311 next_client = monitor.clients;
312 while (next_client) |client| {
313 if (client_mod.isVisible(client)) {
314 break;
315 }
316 next_client = client.next;
317 }
318 }
319 } else {
320 var prev: ?*Client = null;
321 var iter = monitor.clients;
322 while (iter) |client| {
323 if (client == current) {
324 break;
325 }
326 if (client_mod.isVisible(client)) {
327 prev = client;
328 }
329 iter = client.next;
330 }
331 if (prev == null) {
332 iter = current.next;
333 while (iter) |client| {
334 if (client_mod.isVisible(client)) {
335 prev = client;
336 }
337 iter = client.next;
338 }
339 }
340 next_client = prev;
341 }
342
343 if (next_client) |client| {
344 core.focus(client, wm);
345 if (client.monitor) |client_monitor| {
346 core.restack(client_monitor, wm);
347 }
348 }
349 }
350
351 pub fn toggleFloating(wm: *WindowManager) void {
352 const monitor = wm.selected_monitor orelse return;
353 const client = monitor.sel orelse return;
354
355 if (client.is_fullscreen) {
356 return;
357 }
358
359 client.is_floating = !client.is_floating;
360
361 if (client.is_floating) {
362 tiling.resize(client, client.x, client.y, client.width, client.height, false);
363 }
364
365 core.arrange(monitor, wm);
366 std.debug.print("toggle_floating: window=0x{x} floating={}\n", .{ client.window, client.is_floating });
367 }
368
369 pub fn incnmaster(delta: i32, wm: *WindowManager) void {
370 const monitor = wm.selected_monitor orelse return;
371 const new_val = @max(0, monitor.nmaster + delta);
372 monitor.nmaster = new_val;
373 monitor.pertag.nmasters[monitor.pertag.curtag] = new_val;
374 core.arrange(monitor, wm);
375 std.debug.print("incnmaster: nmaster={d}\n", .{monitor.nmaster});
376 }
377
378 pub fn setmfact(delta: f32, wm: *WindowManager) void {
379 const monitor = wm.selected_monitor orelse return;
380 const new_mfact = monitor.mfact + delta;
381 if (new_mfact < 0.05 or new_mfact > 0.95) {
382 return;
383 }
384 monitor.mfact = new_mfact;
385 monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact;
386 core.arrange(monitor, wm);
387 std.debug.print("setmfact: mfact={d:.2}\n", .{monitor.mfact});
388 }
389
390 pub fn cycleLayout(wm: *WindowManager) void {
391 const monitor = wm.selected_monitor orelse return;
392 const new_lt = (monitor.sel_lt + 1) % @as(u32, @intCast(std.meta.fields(config_mod.Layouts).len));
393 monitor.sel_lt = new_lt;
394 monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
395 if (new_lt != @intFromEnum(config_mod.Layouts.scrolling)) {
396 monitor.scroll_offset = 0;
397 }
398 core.arrange(monitor, wm);
399 wm.invalidateBars();
400 if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| {
401 std.debug.print("cycle_layout: {s}\n", .{layout.symbol});
402 }
403 }
404
405 pub fn setLayout(layout_name: ?[]const u8, wm: *WindowManager) void {
406 const monitor = wm.selected_monitor orelse return;
407 const name = layout_name orelse return;
408 const new_lt: u32 = if (config_mod.Layouts.fromString(name)) |value|
409 @intFromEnum(value)
410 else {
411 std.debug.print("set_layout: unknown layout '{s}'\n", .{name});
412 return;
413 };
414 monitor.sel_lt = new_lt;
415 monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
416 if (new_lt != @intFromEnum(config_mod.Layouts.scrolling)) {
417 monitor.scroll_offset = 0;
418 }
419 core.arrange(monitor, wm);
420 wm.invalidateBars();
421 if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| {
422 std.debug.print("set_layout: {s}\n", .{layout.symbol});
423 }
424 }
425
426 pub fn setLayoutIndex(index: u32, wm: *WindowManager) void {
427 const monitor = wm.selected_monitor orelse return;
428 const lt_index = index % @as(u32, @intCast(std.meta.fields(config_mod.Layouts).len));
429 monitor.sel_lt = lt_index;
430 monitor.pertag.sellts[monitor.pertag.curtag] = lt_index;
431 if (lt_index != @intFromEnum(config_mod.Layouts.scrolling)) {
432 monitor.scroll_offset = 0;
433 }
434 core.arrange(monitor, wm);
435 wm.invalidateBars();
436 if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| {
437 std.debug.print("set_layout_index: {s}\n", .{layout.symbol});
438 }
439 }
440
441 pub fn focusmon(direction: i32, wm: *WindowManager) void {
442 const selmon = wm.selected_monitor orelse return;
443 const target = monitor_mod.dirToMonitor(wm, direction) orelse return;
444 if (target == selmon) {
445 return;
446 }
447 core.unfocusClient(selmon.sel, false, wm);
448 wm.selected_monitor = target;
449 core.focus(null, wm);
450 std.debug.print("focusmon: monitor {d}\n", .{target.num});
451 }
452
453 pub fn sendmon(direction: i32, wm: *WindowManager) void {
454 const source_monitor = wm.selected_monitor orelse return;
455 const client = source_monitor.sel orelse return;
456 const target = monitor_mod.dirToMonitor(wm, direction) orelse return;
457
458 if (target == source_monitor) {
459 return;
460 }
461
462 client_mod.detach(client);
463 client_mod.detachStack(client);
464 client.monitor = target;
465 client.tags = target.tagset[target.sel_tags];
466 client_mod.attachAside(client);
467 client_mod.attachStack(client);
468
469 core.focusTopClient(source_monitor, wm);
470 core.arrange(source_monitor, wm);
471 core.arrange(target, wm);
472
473 std.debug.print("sendmon: window=0x{x} to monitor {d}\n", .{ client.window, target.num });
474 }
475
476 pub fn snapX(client: *Client, new_x: i32, monitor: *Monitor) i32 {
477 const client_width = client.width + 2 * client.border_width;
478 if (@abs(monitor.win_x - new_x) < snap_distance) {
479 return monitor.win_x;
480 } else if (@abs((monitor.win_x + monitor.win_w) - (new_x + client_width)) < snap_distance) {
481 return monitor.win_x + monitor.win_w - client_width;
482 }
483 return new_x;
484 }
485
486 pub fn snapY(client: *Client, new_y: i32, monitor: *Monitor) i32 {
487 const client_height = client.height + 2 * client.border_width;
488 if (@abs(monitor.win_y - new_y) < snap_distance) {
489 return monitor.win_y;
490 } else if (@abs((monitor.win_y + monitor.win_h) - (new_y + client_height)) < snap_distance) {
491 return monitor.win_y + monitor.win_h - client_height;
492 }
493 return new_y;
494 }
495
496 pub fn movemouse(wm: *WindowManager) void {
497 const monitor = wm.selected_monitor orelse return;
498 const client = monitor.sel orelse return;
499
500 if (client.is_fullscreen) {
501 return;
502 }
503
504 core.restack(monitor, wm);
505
506 const was_floating = client.is_floating;
507 if (!client.is_floating) {
508 client.is_floating = true;
509 }
510
511 var root_x: c_int = undefined;
512 var root_y: c_int = undefined;
513 var dummy_win: xlib.Window = undefined;
514 var dummy_int: c_int = undefined;
515 var dummy_uint: c_uint = undefined;
516
517 _ = xlib.XQueryPointer(wm.display.handle, wm.display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint);
518
519 const grab_result = xlib.XGrabPointer(
520 wm.display.handle,
521 wm.display.root,
522 xlib.False,
523 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
524 xlib.GrabModeAsync,
525 xlib.GrabModeAsync,
526 xlib.None,
527 wm.cursors.move,
528 xlib.CurrentTime,
529 );
530
531 if (grab_result != xlib.GrabSuccess) {
532 return;
533 }
534
535 const start_x = client.x;
536 const start_y = client.y;
537 const pointer_start_x = root_x;
538 const pointer_start_y = root_y;
539 var last_time: c_ulong = 0;
540
541 var event: xlib.XEvent = undefined;
542 var done = false;
543
544 while (!done) {
545 _ = xlib.XNextEvent(wm.display.handle, &event);
546
547 switch (event.type) {
548 xlib.MotionNotify => {
549 const motion = &event.xmotion;
550 if ((motion.time - last_time) < (1000 / 60)) {
551 continue;
552 }
553 last_time = motion.time;
554 const delta_x = motion.x_root - pointer_start_x;
555 const delta_y = motion.y_root - pointer_start_y;
556 var new_x = start_x + delta_x;
557 var new_y = start_y + delta_y;
558 if (client.monitor) |client_monitor| {
559 new_x = snapX(client, new_x, client_monitor);
560 new_y = snapY(client, new_y, client_monitor);
561 }
562 tiling.resize(client, new_x, new_y, client.width, client.height, true);
563 },
564 xlib.ButtonRelease => {
565 done = true;
566 },
567 else => {},
568 }
569 }
570
571 _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
572
573 const new_mon = monitor_mod.rectToMonitor(wm, client.x, client.y, client.width, client.height);
574 if (new_mon != null and new_mon != monitor) {
575 client_mod.detach(client);
576 client_mod.detachStack(client);
577 client.monitor = new_mon;
578 client.tags = new_mon.?.tagset[new_mon.?.sel_tags];
579 client_mod.attachAside(client);
580 client_mod.attachStack(client);
581 wm.selected_monitor = new_mon;
582 core.focus(client, wm);
583 core.arrange(monitor, wm);
584 core.arrange(new_mon.?, wm);
585 } else {
586 core.arrange(monitor, wm);
587 }
588
589 if (wm.config.auto_tile and !was_floating) {
590 const drop_monitor = client.monitor orelse return;
591 const center_x = client.x + @divTrunc(client.width, 2);
592 const center_y = client.y + @divTrunc(client.height, 2);
593
594 if (client_mod.tiledWindowAt(client, drop_monitor, center_x, center_y)) |target| {
595 client_mod.insertBefore(client, target);
596 }
597
598 client.is_floating = false;
599 core.arrange(drop_monitor, wm);
600 }
601 }
602
603 pub fn resizemouse(wm: *WindowManager) void {
604 const monitor = wm.selected_monitor orelse return;
605 const client = monitor.sel orelse return;
606
607 if (client.is_fullscreen) {
608 return;
609 }
610
611 core.restack(monitor, wm);
612
613 // If tiled_resize_mode is enabled and window is tiled, adjust mfact instead
614 if (wm.config.tiled_resize_mode and !client.is_floating) {
615 resizemouseTiled(wm, monitor);
616 return;
617 }
618
619 if (!client.is_floating) {
620 client.is_floating = true;
621 }
622
623 const grab_result = xlib.XGrabPointer(
624 wm.display.handle,
625 wm.display.root,
626 xlib.False,
627 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
628 xlib.GrabModeAsync,
629 xlib.GrabModeAsync,
630 xlib.None,
631 wm.cursors.resize,
632 xlib.CurrentTime,
633 );
634
635 if (grab_result != xlib.GrabSuccess) {
636 return;
637 }
638
639 _ = 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);
640
641 var event: xlib.XEvent = undefined;
642 var done = false;
643 var last_time: c_ulong = 0;
644
645 while (!done) {
646 _ = xlib.XNextEvent(wm.display.handle, &event);
647
648 switch (event.type) {
649 xlib.MotionNotify => {
650 const motion = &event.xmotion;
651 if ((motion.time - last_time) < (1000 / 60)) {
652 continue;
653 }
654 last_time = motion.time;
655 var new_width = @max(1, motion.x_root - client.x - 2 * client.border_width + 1);
656 var new_height = @max(1, motion.y_root - client.y - 2 * client.border_width + 1);
657 if (client.monitor) |client_monitor| {
658 const client_right = client.x + new_width + 2 * client.border_width;
659 const client_bottom = client.y + new_height + 2 * client.border_width;
660 const mon_right = client_monitor.win_x + client_monitor.win_w;
661 const mon_bottom = client_monitor.win_y + client_monitor.win_h;
662 if (@abs(mon_right - client_right) < snap_distance) {
663 new_width = @max(1, mon_right - client.x - 2 * client.border_width);
664 }
665 if (@abs(mon_bottom - client_bottom) < snap_distance) {
666 new_height = @max(1, mon_bottom - client.y - 2 * client.border_width);
667 }
668 }
669 tiling.resize(client, client.x, client.y, new_width, new_height, true);
670 },
671 xlib.ButtonRelease => {
672 done = true;
673 },
674 else => {},
675 }
676 }
677
678 _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
679 core.arrange(monitor, wm);
680 }
681
682 /// Resize tiled windows by adjusting mfact with mouse drag (like Super+H/L but with mouse)
683 fn resizemouseTiled(wm: *WindowManager, monitor: *Monitor) void {
684 const grab_result = xlib.XGrabPointer(
685 wm.display.handle,
686 wm.display.root,
687 xlib.False,
688 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
689 xlib.GrabModeAsync,
690 xlib.GrabModeAsync,
691 xlib.None,
692 wm.cursors.resize,
693 xlib.CurrentTime,
694 );
695
696 if (grab_result != xlib.GrabSuccess) {
697 return;
698 }
699
700 // Get initial pointer position
701 var root_return: xlib.Window = undefined;
702 var child_return: xlib.Window = undefined;
703 var root_x: c_int = undefined;
704 var root_y: c_int = undefined;
705 var win_x: c_int = undefined;
706 var win_y: c_int = undefined;
707 var mask_return: c_uint = undefined;
708
709 _ = xlib.XQueryPointer(
710 wm.display.handle,
711 wm.display.root,
712 &root_return,
713 &child_return,
714 &root_x,
715 &root_y,
716 &win_x,
717 &win_y,
718 &mask_return,
719 );
720
721 const start_x = root_x;
722 const initial_mfact = monitor.mfact;
723
724 const total_width: f32 = @floatFromInt(monitor.win_w - 2 * monitor.gap_outer_v - monitor.gap_inner_v);
725
726 var event: xlib.XEvent = undefined;
727 var done = false;
728 var last_time: c_ulong = 0;
729
730 while (!done) {
731 _ = xlib.XNextEvent(wm.display.handle, &event);
732
733 switch (event.type) {
734 xlib.MotionNotify => {
735 const motion = &event.xmotion;
736 if ((motion.time - last_time) < (1000 / 60)) {
737 continue;
738 }
739 last_time = motion.time;
740
741 const delta_x: f32 = @floatFromInt(motion.x_root - start_x);
742 const mfact_delta = delta_x / total_width;
743 const new_mfact = initial_mfact + mfact_delta;
744
745 if (new_mfact >= 0.05 and new_mfact <= 0.95) {
746 monitor.mfact = new_mfact;
747 monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact;
748 core.arrange(monitor, wm);
749 }
750 },
751 xlib.ButtonRelease => {
752 done = true;
753 },
754 else => {},
755 }
756 }
757
758 _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
759 }
760
761 /// Config-loading callback for wm.reloadConfig().
762 /// Re-initialises Lua and loads the config file (or falls back to defaults).
763 pub fn reloadLoadConfig(wm: *WindowManager) void {
764 lua.deinit();
765 _ = lua.init(&wm.config);
766
767 const loaded = if (wm.config_path) |path|
768 lua.loadFile(path)
769 else
770 lua.loadConfig(wm.env);
771
772 if (loaded) {
773 if (wm.config_path) |path| {
774 std.debug.print("reloaded config from {s}\n", .{path});
775 } else {
776 std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{});
777 }
778 } else {
779 std.debug.print("reload failed, restoring defaults\n", .{});
780 config_mod.initializeDefaultConfig(&wm.config);
781 }
782 }
783
784 pub fn executeAction(action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8, wm: *WindowManager) void {
785 switch (action) {
786 .spawn_terminal => spawnTerminal(wm),
787 .spawn => {
788 if (str_arg) |cmd| spawnCommand(wm, cmd);
789 },
790 .kill_client => killFocused(wm),
791 .quit => {
792 std.debug.print("quit keybind pressed\n", .{});
793 wm.running = false;
794 },
795 .reload_config => wm.reloadConfig(reloadLoadConfig),
796 .restart => wm.reloadConfig(reloadLoadConfig),
797 .show_keybinds => {
798 if (wm.overlay) |overlay| {
799 const mon = wm.selected_monitor orelse wm.monitors;
800 if (mon) |m| {
801 overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h, &wm.config);
802 }
803 }
804 },
805 .focus_next => focusstack(if (int_arg == 0) 1 else int_arg, wm),
806 .focus_prev => focusstack(-1, wm),
807 .move_next => movestack(if (int_arg == 0) 1 else int_arg, wm),
808 .move_prev => movestack(-1, wm),
809 .resize_master => setmfact(@as(f32, @floatFromInt(int_arg)) / 1000.0, wm),
810 .inc_master => incnmaster(1, wm),
811 .dec_master => incnmaster(-1, wm),
812 .toggle_floating => toggleFloating(wm),
813 .toggle_fullscreen => toggleFullscreen(wm),
814 .toggle_gaps => toggleGaps(wm),
815 .toggle_bar => toggleBar(wm),
816 .cycle_layout => cycleLayout(wm),
817 .set_layout => setLayout(str_arg, wm),
818 .set_layout_tiling => setLayoutIndex(0, wm),
819 .set_layout_floating => setLayoutIndex(2, wm),
820 .view_tag => {
821 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
822 core.view(tag_mask, wm);
823 },
824 .view_next_tag => viewAdjacentTag(1, wm),
825 .view_prev_tag => viewAdjacentTag(-1, wm),
826 .view_next_nonempty_tag => viewAdjacentNonemptyTag(1, wm),
827 .view_prev_nonempty_tag => viewAdjacentNonemptyTag(-1, wm),
828 .move_to_tag => {
829 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
830 tagClient(tag_mask, wm);
831 },
832 .toggle_view_tag => {
833 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
834 toggleView(tag_mask, wm);
835 },
836 .toggle_tag => {
837 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
838 toggleClientTag(tag_mask, wm);
839 },
840 .focus_monitor => focusmon(int_arg, wm),
841 .send_to_monitor => sendmon(int_arg, wm),
842 .scroll_left => core.scrollLayout(-1, wm),
843 .scroll_right => core.scrollLayout(1, wm),
844 }
845 }