tony / oxwm

Branch: b33b7bb
Switch branches/tags
master
tonybanters updated for scrolling with gestures, and size of windows in scrolling layout. b419fcd 2026-09-20
162 lines · 5 KB Raw Blame History
1 const std = @import("std");
2 const xlib = @import("x11/xlib.zig");
3
4 const Client = @import("client.zig").Client;
5 const WindowManager = @import("wm/wm.zig").WindowManager;
6
7 pub const Layout = struct {
8 symbol: []const u8,
9 arrange_fn: ?*const fn (*Monitor) void,
10 };
11
12 pub const Pertag = struct {
13 curtag: u32 = 1,
14 prevtag: u32 = 1,
15 nmasters: [13]i32 = [_]i32{1} ** 13,
16 mfacts: [13]f32 = [_]f32{0.55} ** 13,
17 sellts: [13]u32 = [_]u32{0} ** 13,
18 ltidxs: [13][6]?*const Layout = [_][6]?*const Layout{.{ null, null, null, null, null, null }} ** 13,
19 showbars: [13]bool = [_]bool{true} ** 13,
20 };
21
22 pub const Monitor = struct {
23 lt_symbol: [16]u8 = std.mem.zeroes([16]u8),
24 mfact: f32 = 0.55,
25 nmaster: i32 = 1,
26 num: i32 = 0,
27 bar_y: i32 = 0,
28 mon_x: i32 = 0,
29 mon_y: i32 = 0,
30 mon_w: i32 = 0,
31 mon_h: i32 = 0,
32 win_x: i32 = 0,
33 win_y: i32 = 0,
34 win_w: i32 = 0,
35 win_h: i32 = 0,
36 gap_inner_h: i32 = 0,
37 gap_inner_v: i32 = 0,
38 gap_outer_h: i32 = 0,
39 gap_outer_v: i32 = 0,
40 smartgaps_enabled: bool = false,
41 scroll_offset: i32 = 0,
42 scroll_default_width: f32 = 0.5,
43 sel_tags: u32 = 0,
44 sel_lt: u32 = 0,
45 tagset: [2]u32 = .{ 1, 1 },
46 show_bar: bool = true,
47 top_bar: bool = true,
48 clients: ?*Client = null,
49 sel: ?*Client = null,
50 stack: ?*Client = null,
51 next: ?*Monitor = null,
52 bar_win: xlib.Window = 0,
53 lt: [6]?*const Layout = .{null} ** 6,
54 pertag: Pertag = .{},
55 };
56
57 /// Allocates and zero-initialises a new `Monitor`.
58 pub fn create(allocator: std.mem.Allocator) ?*Monitor {
59 const mon = allocator.create(Monitor) catch return null;
60 mon.* = Monitor{};
61 return mon;
62 }
63
64 /// Frees a monitor previously returned by `create`.
65 pub fn destroy(allocator: std.mem.Allocator, mon: *Monitor) void {
66 allocator.destroy(mon);
67 }
68
69 /// Returns the monitor whose bar window or client matches `win`, falling
70 /// back to a pointer-position query when `win` is the root window.
71 pub fn windowToMonitor(wm: *WindowManager, win: xlib.Window) ?*Monitor {
72 const monitors = wm.monitors;
73 const selected_monitor = wm.selected_monitor;
74
75 if (win == wm.display.root) {
76 var root_x: c_int = undefined;
77 var root_y: c_int = undefined;
78 var dummy_win: xlib.Window = undefined;
79 var dummy_int: c_int = undefined;
80 var dummy_uint: c_uint = undefined;
81 if (xlib.XQueryPointer(wm.display.handle, wm.display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint) != 0) {
82 return rectToMonitor(wm, root_x, root_y, 1, 1);
83 }
84 }
85
86 var current = monitors;
87 while (current) |monitor| {
88 if (monitor.bar_win == win) return monitor;
89 current = monitor.next;
90 }
91
92 const client = @import("client.zig").windowToClient(monitors, win);
93 if (client) |found_client| {
94 return found_client.monitor;
95 }
96
97 return selected_monitor;
98 }
99
100 /// Returns the monitor with the greatest intersection area with the given
101 /// rectangle, or `selected_monitor` if no intersection is found.
102 pub fn rectToMonitor(wm: *WindowManager, x: i32, y: i32, width: i32, height: i32) ?*Monitor {
103 const monitors = wm.monitors;
104 const selected_monitor = wm.selected_monitor;
105 var result = selected_monitor;
106 var max_area: i32 = 0;
107
108 var current = monitors;
109 while (current) |monitor| {
110 const intersect_x = @max(0, @min(x + width, monitor.win_x + monitor.win_w) - @max(x, monitor.win_x));
111 const intersect_y = @max(0, @min(y + height, monitor.win_y + monitor.win_h) - @max(y, monitor.win_y));
112 const area = intersect_x * intersect_y;
113 if (area > max_area) {
114 max_area = area;
115 result = monitor;
116 }
117 current = monitor.next;
118 }
119 return result;
120 }
121
122 /// Returns the next or previous monitor relative to `wm.selected_monitor`.
123 ///
124 /// Positive `direction` moves forward through the linked list (wrapping to
125 /// the head); negative moves backward (wrapping to the tail).
126 ///
127 // TODO:
128 // - Change direction to an enum/enum_literal
129 // - Rename function
130 pub fn dirToMonitor(wm: *WindowManager, direction: i32) ?*Monitor {
131 const monitors = wm.monitors;
132 const selected_monitor = wm.selected_monitor;
133 var target: ?*Monitor = null;
134
135 if (direction > 0) {
136 target = if (selected_monitor) |current| current.next else null;
137 if (target == null) {
138 target = monitors;
139 }
140 } else if (selected_monitor == monitors) {
141 // Already at head, walk to tail.
142 var last = monitors;
143 while (last) |iter| {
144 if (iter.next == null) {
145 target = iter;
146 break;
147 }
148 last = iter.next;
149 }
150 } else {
151 // Walk until we find the node just before selected_monitor.
152 var previous = monitors;
153 while (previous) |iter| {
154 if (iter.next == selected_monitor) {
155 target = iter;
156 break;
157 }
158 previous = iter.next;
159 }
160 }
161 return target;
162 }