tony / oxwm

Branch: b419fcd
Switch branches/tags
master
tonybanters updated for scrolling with gestures, and size of windows in scrolling layout. b419fcd 2026-09-20
945 lines · 31.4 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 fn focusedScrollClient(wm: *WindowManager) ?*Client {
381 const monitor = wm.selected_monitor orelse return null;
382 if (!core.isScrollingLayout(monitor)) return null;
383 const client = monitor.sel orelse return null;
384 if (client.is_floating) return null;
385 return client;
386 }
387
388 fn effectiveScrollWidth(client: *Client) f32 {
389 if (client.scroll_width > 0) return client.scroll_width;
390 if (client.monitor) |monitor| return monitor.scroll_default_width;
391 return 0.5;
392 }
393
394 pub fn cycleWidth(direction: i32, wm: *WindowManager) void {
395 const client = focusedScrollClient(wm) orelse return;
396 const monitor = client.monitor orelse return;
397 const count: usize = @intCast(wm.config.scroll_width_preset_count);
398 if (count == 0) return;
399 const presets = wm.config.scroll_width_presets[0..count];
400 const current = effectiveScrollWidth(client);
401
402 var matched: ?usize = null;
403 for (presets, 0..) |preset, index| {
404 if (@abs(preset - current) < 0.001) matched = index;
405 }
406
407 var next: usize = 0;
408 if (matched) |index| {
409 const step: i32 = if (direction >= 0) 1 else -1;
410 next = @intCast(@mod(@as(i32, @intCast(index)) + step, @as(i32, @intCast(count))));
411 } else if (direction >= 0) {
412 next = 0;
413 for (presets, 0..) |preset, index| {
414 if (preset > current) {
415 next = index;
416 break;
417 }
418 }
419 } else {
420 next = count - 1;
421 var index: usize = count;
422 while (index > 0) {
423 index -= 1;
424 if (presets[index] < current) {
425 next = index;
426 break;
427 }
428 }
429 }
430
431 client.scroll_width = presets[next];
432 core.arrange(monitor, wm);
433 std.debug.print("cycle_width: width={d:.3}\n", .{client.scroll_width});
434 }
435
436 pub fn setWidth(permille: i32, wm: *WindowManager) void {
437 if (permille <= 0) return;
438 const client = focusedScrollClient(wm) orelse return;
439 const monitor = client.monitor orelse return;
440 client.scroll_width = @as(f32, @floatFromInt(permille)) / 1000.0;
441 core.arrange(monitor, wm);
442 std.debug.print("set_width: width={d:.3}\n", .{client.scroll_width});
443 }
444
445 fn adjustScrollWidth(delta: f32, wm: *WindowManager) void {
446 const client = focusedScrollClient(wm) orelse return;
447 const monitor = client.monitor orelse return;
448 const current = effectiveScrollWidth(client);
449 if (current > 1.0) {
450 const available: f32 = @floatFromInt(monitor.win_w - 2 * monitor.gap_outer_v);
451 client.scroll_width = @max(100.0, @min(current + delta * available, available));
452 } else {
453 client.scroll_width = @max(0.1, @min(current + delta, 1.0));
454 }
455 core.arrange(monitor, wm);
456 std.debug.print("adjust_width: width={d:.3}\n", .{client.scroll_width});
457 }
458
459 pub fn setmfact(delta: f32, wm: *WindowManager) void {
460 const monitor = wm.selected_monitor orelse return;
461 if (core.isScrollingLayout(monitor)) {
462 adjustScrollWidth(delta, wm);
463 return;
464 }
465 const new_mfact = monitor.mfact + delta;
466 if (new_mfact < 0.05 or new_mfact > 0.95) {
467 return;
468 }
469 monitor.mfact = new_mfact;
470 monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact;
471 core.arrange(monitor, wm);
472 std.debug.print("setmfact: mfact={d:.2}\n", .{monitor.mfact});
473 }
474
475 pub fn cycleLayout(wm: *WindowManager) void {
476 const monitor = wm.selected_monitor orelse return;
477 const new_lt = (monitor.sel_lt + 1) % @as(u32, @intCast(std.meta.fields(config_mod.Layouts).len));
478 monitor.sel_lt = new_lt;
479 monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
480 if (new_lt != @intFromEnum(config_mod.Layouts.scrolling)) {
481 monitor.scroll_offset = 0;
482 }
483 core.arrange(monitor, wm);
484 wm.invalidateBars();
485 if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| {
486 std.debug.print("cycle_layout: {s}\n", .{layout.symbol});
487 }
488 }
489
490 pub fn setLayout(layout_name: ?[]const u8, wm: *WindowManager) void {
491 const monitor = wm.selected_monitor orelse return;
492 const name = layout_name orelse return;
493 const new_lt: u32 = if (config_mod.Layouts.fromString(name)) |value|
494 @intFromEnum(value)
495 else {
496 std.debug.print("set_layout: unknown layout '{s}'\n", .{name});
497 return;
498 };
499 monitor.sel_lt = new_lt;
500 monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
501 if (new_lt != @intFromEnum(config_mod.Layouts.scrolling)) {
502 monitor.scroll_offset = 0;
503 }
504 core.arrange(monitor, wm);
505 wm.invalidateBars();
506 if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| {
507 std.debug.print("set_layout: {s}\n", .{layout.symbol});
508 }
509 }
510
511 pub fn setLayoutIndex(index: u32, wm: *WindowManager) void {
512 const monitor = wm.selected_monitor orelse return;
513 const lt_index = index % @as(u32, @intCast(std.meta.fields(config_mod.Layouts).len));
514 monitor.sel_lt = lt_index;
515 monitor.pertag.sellts[monitor.pertag.curtag] = lt_index;
516 if (lt_index != @intFromEnum(config_mod.Layouts.scrolling)) {
517 monitor.scroll_offset = 0;
518 }
519 core.arrange(monitor, wm);
520 wm.invalidateBars();
521 if (monitor.lt[@as(usize, monitor.sel_lt)]) |layout| {
522 std.debug.print("set_layout_index: {s}\n", .{layout.symbol});
523 }
524 }
525
526 fn warpCursorToMonitor(monitor: *Monitor, wm: *WindowManager) void {
527 const center_x = monitor.win_x + @divTrunc(monitor.win_w, 2);
528 const center_y = monitor.win_y + @divTrunc(monitor.win_h, 2);
529 _ = xlib.XWarpPointer(wm.display.handle, xlib.None, wm.display.root, 0, 0, 0, 0, center_x, center_y);
530 }
531
532 pub fn focusmon(direction: i32, wm: *WindowManager) void {
533 const selmon = wm.selected_monitor orelse return;
534 const target = monitor_mod.dirToMonitor(wm, direction) orelse return;
535 if (target == selmon) {
536 return;
537 }
538 core.unfocusClient(selmon.sel, false, wm);
539 wm.selected_monitor = target;
540 core.focus(null, wm);
541 if (wm.config.warp_cursor_to_monitor) {
542 warpCursorToMonitor(target, wm);
543 }
544 std.debug.print("focusmon: monitor {d}\n", .{target.num});
545 }
546
547 pub fn sendmon(direction: i32, wm: *WindowManager) void {
548 const source_monitor = wm.selected_monitor orelse return;
549 const client = source_monitor.sel orelse return;
550 const target = monitor_mod.dirToMonitor(wm, direction) orelse return;
551
552 if (target == source_monitor) {
553 return;
554 }
555
556 client_mod.detach(client);
557 client_mod.detachStack(client);
558 client.monitor = target;
559 client.tags = target.tagset[target.sel_tags];
560 client_mod.attachWith(client, wm.config.attach_method);
561 client_mod.attachStack(client);
562
563 core.focusTopClient(source_monitor, wm);
564 core.arrange(source_monitor, wm);
565 core.arrange(target, wm);
566
567 if (wm.config.warp_cursor_on_send) {
568 warpCursorToMonitor(target, wm);
569 }
570
571 std.debug.print("sendmon: window=0x{x} to monitor {d}\n", .{ client.window, target.num });
572 }
573
574 pub fn snapX(client: *Client, new_x: i32, monitor: *Monitor) i32 {
575 const client_width = client.width + 2 * client.border_width;
576 if (@abs(monitor.win_x - new_x) < snap_distance) {
577 return monitor.win_x;
578 } else if (@abs((monitor.win_x + monitor.win_w) - (new_x + client_width)) < snap_distance) {
579 return monitor.win_x + monitor.win_w - client_width;
580 }
581 return new_x;
582 }
583
584 pub fn snapY(client: *Client, new_y: i32, monitor: *Monitor) i32 {
585 const client_height = client.height + 2 * client.border_width;
586 if (@abs(monitor.win_y - new_y) < snap_distance) {
587 return monitor.win_y;
588 } else if (@abs((monitor.win_y + monitor.win_h) - (new_y + client_height)) < snap_distance) {
589 return monitor.win_y + monitor.win_h - client_height;
590 }
591 return new_y;
592 }
593
594 pub fn movemouse(wm: *WindowManager) void {
595 const monitor = wm.selected_monitor orelse return;
596 const client = monitor.sel orelse return;
597
598 if (client.is_fullscreen) {
599 return;
600 }
601
602 core.restack(monitor, wm);
603
604 const was_floating = client.is_floating;
605 if (!client.is_floating) {
606 client.is_floating = true;
607 }
608
609 var root_x: c_int = undefined;
610 var root_y: c_int = undefined;
611 var dummy_win: xlib.Window = undefined;
612 var dummy_int: c_int = undefined;
613 var dummy_uint: c_uint = undefined;
614
615 _ = xlib.XQueryPointer(wm.display.handle, wm.display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint);
616
617 const grab_result = xlib.XGrabPointer(
618 wm.display.handle,
619 wm.display.root,
620 xlib.False,
621 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
622 xlib.GrabModeAsync,
623 xlib.GrabModeAsync,
624 xlib.None,
625 wm.cursors.move,
626 xlib.CurrentTime,
627 );
628
629 if (grab_result != xlib.GrabSuccess) {
630 return;
631 }
632
633 const start_x = client.x;
634 const start_y = client.y;
635 const pointer_start_x = root_x;
636 const pointer_start_y = root_y;
637 var last_time: c_ulong = 0;
638
639 var event: xlib.XEvent = undefined;
640 var done = false;
641
642 while (!done) {
643 _ = xlib.XNextEvent(wm.display.handle, &event);
644
645 switch (event.type) {
646 xlib.MotionNotify => {
647 const motion = &event.xmotion;
648 if ((motion.time - last_time) < (1000 / 60)) {
649 continue;
650 }
651 last_time = motion.time;
652 const delta_x = motion.x_root - pointer_start_x;
653 const delta_y = motion.y_root - pointer_start_y;
654 var new_x = start_x + delta_x;
655 var new_y = start_y + delta_y;
656 if (client.monitor) |client_monitor| {
657 new_x = snapX(client, new_x, client_monitor);
658 new_y = snapY(client, new_y, client_monitor);
659 }
660 tiling.resize(client, new_x, new_y, client.width, client.height, true);
661 },
662 xlib.ButtonRelease => {
663 done = true;
664 },
665 else => {},
666 }
667 }
668
669 _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
670
671 const new_mon = monitor_mod.rectToMonitor(wm, client.x, client.y, client.width, client.height);
672 if (new_mon != null and new_mon != monitor) {
673 client_mod.detach(client);
674 client_mod.detachStack(client);
675 client.monitor = new_mon;
676 client.tags = new_mon.?.tagset[new_mon.?.sel_tags];
677 client_mod.attachWith(client, wm.config.attach_method);
678 client_mod.attachStack(client);
679 wm.selected_monitor = new_mon;
680 core.focus(client, wm);
681 core.arrange(monitor, wm);
682 core.arrange(new_mon.?, wm);
683 } else {
684 core.arrange(monitor, wm);
685 }
686
687 if (wm.config.auto_tile and !was_floating) {
688 const drop_monitor = client.monitor orelse return;
689 const center_x = client.x + @divTrunc(client.width, 2);
690 const center_y = client.y + @divTrunc(client.height, 2);
691
692 if (client_mod.tiledWindowAt(client, drop_monitor, center_x, center_y)) |target| {
693 client_mod.insertBefore(client, target);
694 }
695
696 client.is_floating = false;
697 core.arrange(drop_monitor, wm);
698 }
699 }
700
701 pub fn resizemouse(wm: *WindowManager) void {
702 const monitor = wm.selected_monitor orelse return;
703 const client = monitor.sel orelse return;
704
705 if (client.is_fullscreen) {
706 return;
707 }
708
709 core.restack(monitor, wm);
710
711 // If tiled_resize_mode is enabled and window is tiled, adjust mfact instead
712 if (wm.config.tiled_resize_mode and !client.is_floating) {
713 resizemouseTiled(wm, monitor);
714 return;
715 }
716
717 if (!client.is_floating) {
718 client.is_floating = true;
719 }
720
721 const grab_result = xlib.XGrabPointer(
722 wm.display.handle,
723 wm.display.root,
724 xlib.False,
725 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
726 xlib.GrabModeAsync,
727 xlib.GrabModeAsync,
728 xlib.None,
729 wm.cursors.resize,
730 xlib.CurrentTime,
731 );
732
733 if (grab_result != xlib.GrabSuccess) {
734 return;
735 }
736
737 _ = 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);
738
739 var event: xlib.XEvent = undefined;
740 var done = false;
741 var last_time: c_ulong = 0;
742
743 while (!done) {
744 _ = xlib.XNextEvent(wm.display.handle, &event);
745
746 switch (event.type) {
747 xlib.MotionNotify => {
748 const motion = &event.xmotion;
749 if ((motion.time - last_time) < (1000 / 60)) {
750 continue;
751 }
752 last_time = motion.time;
753 var new_width = @max(1, motion.x_root - client.x - 2 * client.border_width + 1);
754 var new_height = @max(1, motion.y_root - client.y - 2 * client.border_width + 1);
755 if (client.monitor) |client_monitor| {
756 const client_right = client.x + new_width + 2 * client.border_width;
757 const client_bottom = client.y + new_height + 2 * client.border_width;
758 const mon_right = client_monitor.win_x + client_monitor.win_w;
759 const mon_bottom = client_monitor.win_y + client_monitor.win_h;
760 if (@abs(mon_right - client_right) < snap_distance) {
761 new_width = @max(1, mon_right - client.x - 2 * client.border_width);
762 }
763 if (@abs(mon_bottom - client_bottom) < snap_distance) {
764 new_height = @max(1, mon_bottom - client.y - 2 * client.border_width);
765 }
766 }
767 tiling.resize(client, client.x, client.y, new_width, new_height, true);
768 },
769 xlib.ButtonRelease => {
770 done = true;
771 },
772 else => {},
773 }
774 }
775
776 _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
777 core.arrange(monitor, wm);
778 }
779
780 /// Resize tiled windows by adjusting mfact with mouse drag (like Super+H/L but with mouse)
781 fn resizemouseTiled(wm: *WindowManager, monitor: *Monitor) void {
782 const grab_result = xlib.XGrabPointer(
783 wm.display.handle,
784 wm.display.root,
785 xlib.False,
786 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
787 xlib.GrabModeAsync,
788 xlib.GrabModeAsync,
789 xlib.None,
790 wm.cursors.resize,
791 xlib.CurrentTime,
792 );
793
794 if (grab_result != xlib.GrabSuccess) {
795 return;
796 }
797
798 // Get initial pointer position
799 var root_return: xlib.Window = undefined;
800 var child_return: xlib.Window = undefined;
801 var root_x: c_int = undefined;
802 var root_y: c_int = undefined;
803 var win_x: c_int = undefined;
804 var win_y: c_int = undefined;
805 var mask_return: c_uint = undefined;
806
807 _ = xlib.XQueryPointer(
808 wm.display.handle,
809 wm.display.root,
810 &root_return,
811 &child_return,
812 &root_x,
813 &root_y,
814 &win_x,
815 &win_y,
816 &mask_return,
817 );
818
819 const start_x = root_x;
820 const initial_mfact = monitor.mfact;
821
822 const total_width: f32 = @floatFromInt(monitor.win_w - 2 * monitor.gap_outer_v - monitor.gap_inner_v);
823
824 var event: xlib.XEvent = undefined;
825 var done = false;
826 var last_time: c_ulong = 0;
827
828 while (!done) {
829 _ = xlib.XNextEvent(wm.display.handle, &event);
830
831 switch (event.type) {
832 xlib.MotionNotify => {
833 const motion = &event.xmotion;
834 if ((motion.time - last_time) < (1000 / 60)) {
835 continue;
836 }
837 last_time = motion.time;
838
839 const delta_x: f32 = @floatFromInt(motion.x_root - start_x);
840 const mfact_delta = delta_x / total_width;
841 const new_mfact = initial_mfact + mfact_delta;
842
843 if (new_mfact >= 0.05 and new_mfact <= 0.95) {
844 monitor.mfact = new_mfact;
845 monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact;
846 core.arrange(monitor, wm);
847 }
848 },
849 xlib.ButtonRelease => {
850 done = true;
851 },
852 else => {},
853 }
854 }
855
856 _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
857 }
858
859 /// Config-loading callback for wm.reloadConfig().
860 /// Re-initialises Lua and loads the config file (or falls back to defaults).
861 pub fn reloadLoadConfig(wm: *WindowManager) void {
862 lua.deinit();
863 _ = lua.init(&wm.config);
864
865 const loaded = if (wm.config_path) |path|
866 lua.loadFile(path)
867 else
868 lua.loadConfig(wm.env);
869
870 if (loaded) {
871 if (wm.config_path) |path| {
872 std.debug.print("reloaded config from {s}\n", .{path});
873 } else {
874 std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{});
875 }
876 } else {
877 std.debug.print("reload failed, restoring defaults\n", .{});
878 config_mod.initializeDefaultConfig(&wm.config);
879 }
880 }
881
882 pub fn executeAction(action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8, wm: *WindowManager) void {
883 switch (action) {
884 .spawn_terminal => spawnTerminal(wm),
885 .spawn => {
886 if (str_arg) |cmd| spawnCommand(wm, cmd);
887 },
888 .kill_client => killFocused(wm),
889 .quit => {
890 std.debug.print("quit keybind pressed\n", .{});
891 wm.running = false;
892 },
893 .reload_config => wm.reloadConfig(reloadLoadConfig),
894 .restart => wm.reloadConfig(reloadLoadConfig),
895 .show_keybinds => {
896 if (wm.overlay) |overlay| {
897 const mon = wm.selected_monitor orelse wm.monitors;
898 if (mon) |m| {
899 overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h, &wm.config);
900 }
901 }
902 },
903 .focus_next => focusstack(if (int_arg == 0) 1 else int_arg, wm),
904 .focus_prev => focusstack(-1, wm),
905 .move_next => movestack(if (int_arg == 0) 1 else int_arg, wm),
906 .move_prev => movestack(-1, wm),
907 .resize_master => setmfact(@as(f32, @floatFromInt(int_arg)) / 1000.0, wm),
908 .inc_master => incnmaster(1, wm),
909 .dec_master => incnmaster(-1, wm),
910 .toggle_floating => toggleFloating(wm),
911 .toggle_fullscreen => toggleFullscreen(wm),
912 .toggle_gaps => toggleGaps(wm),
913 .toggle_bar => toggleBar(wm),
914 .cycle_layout => cycleLayout(wm),
915 .set_layout => setLayout(str_arg, wm),
916 .set_layout_tiling => setLayoutIndex(0, wm),
917 .set_layout_floating => setLayoutIndex(2, wm),
918 .view_tag => {
919 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
920 core.view(tag_mask, wm);
921 },
922 .view_next_tag => viewAdjacentTag(1, wm),
923 .view_prev_tag => viewAdjacentTag(-1, wm),
924 .view_next_nonempty_tag => viewAdjacentNonemptyTag(1, wm),
925 .view_prev_nonempty_tag => viewAdjacentNonemptyTag(-1, wm),
926 .move_to_tag => {
927 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
928 tagClient(tag_mask, wm);
929 },
930 .toggle_view_tag => {
931 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
932 toggleView(tag_mask, wm);
933 },
934 .toggle_tag => {
935 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
936 toggleClientTag(tag_mask, wm);
937 },
938 .focus_monitor => focusmon(int_arg, wm),
939 .send_to_monitor => sendmon(int_arg, wm),
940 .scroll_left => core.scrollLayout(-1, wm),
941 .scroll_right => core.scrollLayout(1, wm),
942 .cycle_width => cycleWidth(if (int_arg == 0) 1 else int_arg, wm),
943 .set_width => setWidth(int_arg, wm),
944 }
945 }