tony / oxwm

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