tony / oxwm

Branch: c95e5bb
Switch branches/tags
master
MaliCopter Attach Methods but no hot reload c95e5bb 2026-09-06
873 lines · 28.9 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 < 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 pub fn setAttachMethod(attach_method_name: ?[]const u8, wm: *WindowManager) void {
444 const monitor = wm.selected_monitor orelse return;
445 const name = attach_method_name orelse return;
446 const new_att_m: u32 = if (config_mod.AttachMethods.fromString(name)) |value|
447 @intFromEnum(value)
448 else {
449 std.debug.print("set_attach_method: unknown attach method '{s}'\n", .{name});
450 return;
451 };
452 monitor.att_m = new_att_m;
453 }
454
455 fn warpCursorToMonitor(monitor: *Monitor, wm: *WindowManager) void {
456 const center_x = monitor.win_x + @divTrunc(monitor.win_w, 2);
457 const center_y = monitor.win_y + @divTrunc(monitor.win_h, 2);
458 _ = xlib.XWarpPointer(wm.display.handle, xlib.None, wm.display.root, 0, 0, 0, 0, center_x, center_y);
459 }
460
461 pub fn focusmon(direction: i32, wm: *WindowManager) void {
462 const selmon = wm.selected_monitor orelse return;
463 const target = monitor_mod.dirToMonitor(wm, direction) orelse return;
464 if (target == selmon) {
465 return;
466 }
467 core.unfocusClient(selmon.sel, false, wm);
468 wm.selected_monitor = target;
469 core.focus(null, wm);
470 if (wm.config.warp_cursor_to_monitor) {
471 warpCursorToMonitor(target, wm);
472 }
473 std.debug.print("focusmon: monitor {d}\n", .{target.num});
474 }
475
476 pub fn sendmon(direction: i32, wm: *WindowManager) void {
477 const source_monitor = wm.selected_monitor orelse return;
478 const client = source_monitor.sel orelse return;
479 const target = monitor_mod.dirToMonitor(wm, direction) orelse return;
480
481 if (target == source_monitor) {
482 return;
483 }
484
485 client_mod.detach(client);
486 client_mod.detachStack(client);
487 client.monitor = target;
488 client.tags = target.tagset[target.sel_tags];
489 client_mod.attachAside(client);
490 client_mod.attachStack(client);
491
492 core.focusTopClient(source_monitor, wm);
493 core.arrange(source_monitor, wm);
494 core.arrange(target, wm);
495
496 if (wm.config.warp_cursor_on_send) {
497 warpCursorToMonitor(target, wm);
498 }
499
500 std.debug.print("sendmon: window=0x{x} to monitor {d}\n", .{ client.window, target.num });
501 }
502
503 pub fn snapX(client: *Client, new_x: i32, monitor: *Monitor) i32 {
504 const client_width = client.width + 2 * client.border_width;
505 if (@abs(monitor.win_x - new_x) < snap_distance) {
506 return monitor.win_x;
507 } else if (@abs((monitor.win_x + monitor.win_w) - (new_x + client_width)) < snap_distance) {
508 return monitor.win_x + monitor.win_w - client_width;
509 }
510 return new_x;
511 }
512
513 pub fn snapY(client: *Client, new_y: i32, monitor: *Monitor) i32 {
514 const client_height = client.height + 2 * client.border_width;
515 if (@abs(monitor.win_y - new_y) < snap_distance) {
516 return monitor.win_y;
517 } else if (@abs((monitor.win_y + monitor.win_h) - (new_y + client_height)) < snap_distance) {
518 return monitor.win_y + monitor.win_h - client_height;
519 }
520 return new_y;
521 }
522
523 pub fn movemouse(wm: *WindowManager) void {
524 const monitor = wm.selected_monitor orelse return;
525 const client = monitor.sel orelse return;
526
527 if (client.is_fullscreen) {
528 return;
529 }
530
531 core.restack(monitor, wm);
532
533 const was_floating = client.is_floating;
534 if (!client.is_floating) {
535 client.is_floating = true;
536 }
537
538 var root_x: c_int = undefined;
539 var root_y: c_int = undefined;
540 var dummy_win: xlib.Window = undefined;
541 var dummy_int: c_int = undefined;
542 var dummy_uint: c_uint = undefined;
543
544 _ = xlib.XQueryPointer(wm.display.handle, wm.display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint);
545
546 const grab_result = xlib.XGrabPointer(
547 wm.display.handle,
548 wm.display.root,
549 xlib.False,
550 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
551 xlib.GrabModeAsync,
552 xlib.GrabModeAsync,
553 xlib.None,
554 wm.cursors.move,
555 xlib.CurrentTime,
556 );
557
558 if (grab_result != xlib.GrabSuccess) {
559 return;
560 }
561
562 const start_x = client.x;
563 const start_y = client.y;
564 const pointer_start_x = root_x;
565 const pointer_start_y = root_y;
566 var last_time: c_ulong = 0;
567
568 var event: xlib.XEvent = undefined;
569 var done = false;
570
571 while (!done) {
572 _ = xlib.XNextEvent(wm.display.handle, &event);
573
574 switch (event.type) {
575 xlib.MotionNotify => {
576 const motion = &event.xmotion;
577 if ((motion.time - last_time) < (1000 / 60)) {
578 continue;
579 }
580 last_time = motion.time;
581 const delta_x = motion.x_root - pointer_start_x;
582 const delta_y = motion.y_root - pointer_start_y;
583 var new_x = start_x + delta_x;
584 var new_y = start_y + delta_y;
585 if (client.monitor) |client_monitor| {
586 new_x = snapX(client, new_x, client_monitor);
587 new_y = snapY(client, new_y, client_monitor);
588 }
589 tiling.resize(client, new_x, new_y, client.width, client.height, true);
590 },
591 xlib.ButtonRelease => {
592 done = true;
593 },
594 else => {},
595 }
596 }
597
598 _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
599
600 const new_mon = monitor_mod.rectToMonitor(wm, client.x, client.y, client.width, client.height);
601 if (new_mon != null and new_mon != monitor) {
602 client_mod.detach(client);
603 client_mod.detachStack(client);
604 client.monitor = new_mon;
605 client.tags = new_mon.?.tagset[new_mon.?.sel_tags];
606 client_mod.attachAside(client);
607 client_mod.attachStack(client);
608 wm.selected_monitor = new_mon;
609 core.focus(client, wm);
610 core.arrange(monitor, wm);
611 core.arrange(new_mon.?, wm);
612 } else {
613 core.arrange(monitor, wm);
614 }
615
616 if (wm.config.auto_tile and !was_floating) {
617 const drop_monitor = client.monitor orelse return;
618 const center_x = client.x + @divTrunc(client.width, 2);
619 const center_y = client.y + @divTrunc(client.height, 2);
620
621 if (client_mod.tiledWindowAt(client, drop_monitor, center_x, center_y)) |target| {
622 client_mod.insertBefore(client, target);
623 }
624
625 client.is_floating = false;
626 core.arrange(drop_monitor, wm);
627 }
628 }
629
630 pub fn resizemouse(wm: *WindowManager) void {
631 const monitor = wm.selected_monitor orelse return;
632 const client = monitor.sel orelse return;
633
634 if (client.is_fullscreen) {
635 return;
636 }
637
638 core.restack(monitor, wm);
639
640 // If tiled_resize_mode is enabled and window is tiled, adjust mfact instead
641 if (wm.config.tiled_resize_mode and !client.is_floating) {
642 resizemouseTiled(wm, monitor);
643 return;
644 }
645
646 if (!client.is_floating) {
647 client.is_floating = true;
648 }
649
650 const grab_result = xlib.XGrabPointer(
651 wm.display.handle,
652 wm.display.root,
653 xlib.False,
654 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
655 xlib.GrabModeAsync,
656 xlib.GrabModeAsync,
657 xlib.None,
658 wm.cursors.resize,
659 xlib.CurrentTime,
660 );
661
662 if (grab_result != xlib.GrabSuccess) {
663 return;
664 }
665
666 _ = 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);
667
668 var event: xlib.XEvent = undefined;
669 var done = false;
670 var last_time: c_ulong = 0;
671
672 while (!done) {
673 _ = xlib.XNextEvent(wm.display.handle, &event);
674
675 switch (event.type) {
676 xlib.MotionNotify => {
677 const motion = &event.xmotion;
678 if ((motion.time - last_time) < (1000 / 60)) {
679 continue;
680 }
681 last_time = motion.time;
682 var new_width = @max(1, motion.x_root - client.x - 2 * client.border_width + 1);
683 var new_height = @max(1, motion.y_root - client.y - 2 * client.border_width + 1);
684 if (client.monitor) |client_monitor| {
685 const client_right = client.x + new_width + 2 * client.border_width;
686 const client_bottom = client.y + new_height + 2 * client.border_width;
687 const mon_right = client_monitor.win_x + client_monitor.win_w;
688 const mon_bottom = client_monitor.win_y + client_monitor.win_h;
689 if (@abs(mon_right - client_right) < snap_distance) {
690 new_width = @max(1, mon_right - client.x - 2 * client.border_width);
691 }
692 if (@abs(mon_bottom - client_bottom) < snap_distance) {
693 new_height = @max(1, mon_bottom - client.y - 2 * client.border_width);
694 }
695 }
696 tiling.resize(client, client.x, client.y, new_width, new_height, true);
697 },
698 xlib.ButtonRelease => {
699 done = true;
700 },
701 else => {},
702 }
703 }
704
705 _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
706 core.arrange(monitor, wm);
707 }
708
709 /// Resize tiled windows by adjusting mfact with mouse drag (like Super+H/L but with mouse)
710 fn resizemouseTiled(wm: *WindowManager, monitor: *Monitor) void {
711 const grab_result = xlib.XGrabPointer(
712 wm.display.handle,
713 wm.display.root,
714 xlib.False,
715 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
716 xlib.GrabModeAsync,
717 xlib.GrabModeAsync,
718 xlib.None,
719 wm.cursors.resize,
720 xlib.CurrentTime,
721 );
722
723 if (grab_result != xlib.GrabSuccess) {
724 return;
725 }
726
727 // Get initial pointer position
728 var root_return: xlib.Window = undefined;
729 var child_return: xlib.Window = undefined;
730 var root_x: c_int = undefined;
731 var root_y: c_int = undefined;
732 var win_x: c_int = undefined;
733 var win_y: c_int = undefined;
734 var mask_return: c_uint = undefined;
735
736 _ = xlib.XQueryPointer(
737 wm.display.handle,
738 wm.display.root,
739 &root_return,
740 &child_return,
741 &root_x,
742 &root_y,
743 &win_x,
744 &win_y,
745 &mask_return,
746 );
747
748 const start_x = root_x;
749 const initial_mfact = monitor.mfact;
750
751 const total_width: f32 = @floatFromInt(monitor.win_w - 2 * monitor.gap_outer_v - monitor.gap_inner_v);
752
753 var event: xlib.XEvent = undefined;
754 var done = false;
755 var last_time: c_ulong = 0;
756
757 while (!done) {
758 _ = xlib.XNextEvent(wm.display.handle, &event);
759
760 switch (event.type) {
761 xlib.MotionNotify => {
762 const motion = &event.xmotion;
763 if ((motion.time - last_time) < (1000 / 60)) {
764 continue;
765 }
766 last_time = motion.time;
767
768 const delta_x: f32 = @floatFromInt(motion.x_root - start_x);
769 const mfact_delta = delta_x / total_width;
770 const new_mfact = initial_mfact + mfact_delta;
771
772 if (new_mfact >= 0.05 and new_mfact <= 0.95) {
773 monitor.mfact = new_mfact;
774 monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact;
775 core.arrange(monitor, wm);
776 }
777 },
778 xlib.ButtonRelease => {
779 done = true;
780 },
781 else => {},
782 }
783 }
784
785 _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
786 }
787
788 /// Config-loading callback for wm.reloadConfig().
789 /// Re-initialises Lua and loads the config file (or falls back to defaults).
790 pub fn reloadLoadConfig(wm: *WindowManager) void {
791 lua.deinit();
792 _ = lua.init(&wm.config);
793
794 const loaded = if (wm.config_path) |path|
795 lua.loadFile(path)
796 else
797 lua.loadConfig(wm.env);
798
799 if (loaded) {
800 if (wm.config_path) |path| {
801 std.debug.print("reloaded config from {s}\n", .{path});
802 } else {
803 std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{});
804 }
805 } else {
806 std.debug.print("reload failed, restoring defaults\n", .{});
807 config_mod.initializeDefaultConfig(&wm.config);
808 }
809 }
810
811 pub fn executeAction(action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8, wm: *WindowManager) void {
812 switch (action) {
813 .spawn_terminal => spawnTerminal(wm),
814 .spawn => {
815 if (str_arg) |cmd| spawnCommand(wm, cmd);
816 },
817 .kill_client => killFocused(wm),
818 .quit => {
819 std.debug.print("quit keybind pressed\n", .{});
820 wm.running = false;
821 },
822 .reload_config => wm.reloadConfig(reloadLoadConfig),
823 .restart => wm.reloadConfig(reloadLoadConfig),
824 .show_keybinds => {
825 if (wm.overlay) |overlay| {
826 const mon = wm.selected_monitor orelse wm.monitors;
827 if (mon) |m| {
828 overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h, &wm.config);
829 }
830 }
831 },
832 .focus_next => focusstack(if (int_arg == 0) 1 else int_arg, wm),
833 .focus_prev => focusstack(-1, wm),
834 .move_next => movestack(if (int_arg == 0) 1 else int_arg, wm),
835 .move_prev => movestack(-1, wm),
836 .resize_master => setmfact(@as(f32, @floatFromInt(int_arg)) / 1000.0, wm),
837 .inc_master => incnmaster(1, wm),
838 .dec_master => incnmaster(-1, wm),
839 .toggle_floating => toggleFloating(wm),
840 .toggle_fullscreen => toggleFullscreen(wm),
841 .toggle_gaps => toggleGaps(wm),
842 .toggle_bar => toggleBar(wm),
843 .cycle_layout => cycleLayout(wm),
844 .set_layout => setLayout(str_arg, wm),
845 .set_layout_tiling => setLayoutIndex(0, wm),
846 .set_layout_floating => setLayoutIndex(2, wm),
847 .set_attach_method => setAttachMethod(str_arg, wm),
848 .view_tag => {
849 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
850 core.view(tag_mask, wm);
851 },
852 .view_next_tag => viewAdjacentTag(1, wm),
853 .view_prev_tag => viewAdjacentTag(-1, wm),
854 .view_next_nonempty_tag => viewAdjacentNonemptyTag(1, wm),
855 .view_prev_nonempty_tag => viewAdjacentNonemptyTag(-1, wm),
856 .move_to_tag => {
857 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
858 tagClient(tag_mask, wm);
859 },
860 .toggle_view_tag => {
861 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
862 toggleView(tag_mask, wm);
863 },
864 .toggle_tag => {
865 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
866 toggleClientTag(tag_mask, wm);
867 },
868 .focus_monitor => focusmon(int_arg, wm),
869 .send_to_monitor => sendmon(int_arg, wm),
870 .scroll_left => core.scrollLayout(-1, wm),
871 .scroll_right => core.scrollLayout(1, wm),
872 }
873 }