tony / oxwm

Branch: c95e5bb
Switch branches/tags
master
MaliCopter Attach Methods but no hot reload c95e5bb 2026-09-06
338 lines · 9.9 KB Raw Blame History
1 const std = @import("std");
2 const xlib = @import("x11/xlib.zig");
3 const Monitor = @import("monitor.zig").Monitor;
4
5 pub const Client = struct {
6 name: [256]u8 = std.mem.zeroes([256]u8),
7 min_aspect: f32 = 0,
8 max_aspect: f32 = 0,
9 x: i32 = 0,
10 y: i32 = 0,
11 width: i32 = 0,
12 height: i32 = 0,
13 old_x: i32 = 0,
14 old_y: i32 = 0,
15 old_width: i32 = 0,
16 old_height: i32 = 0,
17 base_width: i32 = 0,
18 base_height: i32 = 0,
19 increment_width: i32 = 0,
20 increment_height: i32 = 0,
21 max_width: i32 = 0,
22 max_height: i32 = 0,
23 min_width: i32 = 0,
24 min_height: i32 = 0,
25 hints_valid: bool = false,
26 border_width: i32 = 0,
27 old_border_width: i32 = 0,
28 tags: u32 = 0,
29 is_fixed: bool = false,
30 is_floating: bool = false,
31 is_urgent: bool = false,
32 never_focus: bool = false,
33 old_state: bool = false,
34 is_fullscreen: bool = false,
35 next: ?*Client = null,
36 stack_next: ?*Client = null,
37 monitor: ?*Monitor = null,
38 window: xlib.Window = 0,
39 };
40
41 /// Initialises a new `Client` for the given X window.
42 pub fn create(allocator: std.mem.Allocator, window: xlib.Window) ?*Client {
43 const client = allocator.create(Client) catch return null;
44 client.* = Client{ .window = window };
45 return client;
46 }
47
48 /// Frees a client previously returned by `create`.
49 pub fn destroy(allocator: std.mem.Allocator, client: *Client) void {
50 allocator.destroy(client);
51 }
52
53 /// Prepends `client` to the front of its monitor's client list.
54 pub fn attach(client: *Client) void {
55 if (client.monitor) |monitor| {
56 client.next = monitor.clients;
57 monitor.clients = client;
58 }
59 }
60
61 /// Removes `client` from its monitor's client list.
62 pub fn detach(client: *Client) void {
63 if (client.monitor) |monitor| {
64 var current_ptr: *?*Client = &monitor.clients;
65 while (current_ptr.*) |current| {
66 if (current == client) {
67 current_ptr.* = client.next;
68 return;
69 }
70 current_ptr = &current.next;
71 }
72 }
73 }
74
75 /// Prepends `client` to the front of its monitor's focus stack.
76 pub fn attachStack(client: *Client) void {
77 if (client.monitor) |monitor| {
78 client.stack_next = monitor.stack;
79 monitor.stack = client;
80 }
81 }
82
83 /// Removes `client` from its monitor's focus stack.
84 pub fn detachStack(client: *Client) void {
85 if (client.monitor) |monitor| {
86 var current_ptr: *?*Client = &monitor.stack;
87 while (current_ptr.*) |current| {
88 if (current == client) {
89 current_ptr.* = client.stack_next;
90 return;
91 }
92 current_ptr = &current.stack_next;
93 }
94 }
95 }
96
97 /// Searches all monitors for a client whose X window matches `window`.
98 ///
99 /// `monitors` is the head of the monitor linked list.
100 pub fn windowToClient(monitors: ?*Monitor, window: xlib.Window) ?*Client {
101 var current_monitor = monitors;
102 while (current_monitor) |monitor| {
103 var current_client = monitor.clients;
104 while (current_client) |client| {
105 if (client.window == window) return client;
106 current_client = client.next;
107 }
108 current_monitor = monitor.next;
109 }
110 return null;
111 }
112
113 /// Returns true if `client` is visible on its monitor's currently selected
114 /// tag set.
115 pub fn isVisible(client: *Client) bool {
116 if (client.monitor) |monitor| {
117 return (client.tags & monitor.tagset[monitor.sel_tags]) != 0;
118 }
119 return false;
120 }
121
122 /// Returns true if `client` is visible on the given tag bitmask.
123 pub fn isVisibleOnTag(client: *Client, tags: u32) bool {
124 return (client.tags & tags) != 0;
125 }
126
127 /// Returns the first non-floating, visible client at or after `client`.
128 pub fn nextTiled(client: ?*Client) ?*Client {
129 var current = client;
130 while (current) |iter| {
131 if (!iter.is_floating and isVisible(iter)) return iter;
132 current = iter.next;
133 }
134 return null;
135 }
136
137 /// Returns the first non-floating client on `client`'s monitor that shares
138 /// any tag with `client`. Used for `attach_aside` ordering.
139 pub fn nextTagged(client: *Client) ?*Client {
140 const monitor = client.monitor orelse return null;
141 var walked = monitor.clients;
142 while (walked) |iter| {
143 if (!iter.is_floating and isVisibleOnTag(iter, client.tags)) return iter;
144 walked = iter.next;
145 }
146 return null;
147 }
148
149 /// Returns the last non-floating client on `client`'s monitor that shares
150 /// any tag with `client`. Used for `attach_bottom` ordering.
151 pub fn lastTagged(client: *Client) ?*Client {
152 const monitor = client.monitor orelse return null;
153 var walked = monitor.clients;
154 var last_tiled: u32 = 0;
155 var i: u32 = 0;
156 while (walked) |iter| {
157 if (!iter.is_floating and isVisibleOnTag(iter, client.tags))
158 last_tiled = i;
159 walked = iter.next;
160 i += 1;
161 }
162 walked = monitor.clients;
163 i = 0;
164 while (i < last_tiled) {
165 walked = walked.?.next;
166 i += 1;
167 }
168 return walked;
169 }
170
171 /// Inserts `client` just after the first client that shares its tags,
172 /// falling back to prepend if none exists.
173 pub fn attachAside(client: *Client) void {
174 const at = nextTagged(client);
175 if (at == null) {
176 attach(client);
177 return;
178 }
179 client.next = at.?.next;
180 at.?.next = client;
181 }
182
183 /// Inserts `client` at the top of the client stack.
184 pub fn attachTop(client: *Client) void {
185 client.next = client.monitor.?.clients;
186 client.monitor.?.clients = client;
187 }
188
189 /// Inserts `client` at the bottom of the client stack.
190 pub fn attachBottom(client: *Client) void {
191 const last = lastTagged(client);
192 if (last == null) {
193 attach(client);
194 return;
195 }
196 client.next = null;
197 last.?.next = client;
198 }
199
200 /// Inserts `client` above the selected client,
201 /// falling back to prepend if none exists.
202 pub fn attachAbove(client: *Client) void {
203 const monitor = client.monitor orelse return;
204 if (monitor.sel == null) {
205 attach(client);
206 return;
207 }
208 insertBefore(client, monitor.sel orelse return);
209 }
210
211 /// Inserts `client` below the selected client,
212 /// falling back to prepend if none exists.
213 pub fn attachBelow(client: *Client) void {
214 const monitor = client.monitor orelse return;
215 if (monitor.sel == null) {
216 attach(client);
217 return;
218 }
219 insertAfter(client, monitor.sel orelse return);
220 }
221
222 /// Counts non-floating, visible clients on `monitor`.
223 pub fn countTiled(monitor: *Monitor) u32 {
224 var count: u32 = 0;
225 var current = nextTiled(monitor.clients);
226 while (current) |client| {
227 count += 1;
228 current = nextTiled(client.next);
229 }
230 return count;
231 }
232
233 /// Returns the tiled client on `monitor` whose bounds contain (`point_x`,
234 /// `point_y`), excluding `exclude`. Returns null if none found.
235 pub fn tiledWindowAt(exclude: *Client, monitor: *Monitor, point_x: i32, point_y: i32) ?*Client {
236 const tags = monitor.tagset[monitor.sel_tags];
237 var current = monitor.clients;
238
239 while (current) |client| {
240 if (client != exclude and !client.is_floating and (client.tags & tags) != 0) {
241 const client_w = client.width + client.border_width * 2;
242 const client_h = client.height + client.border_width * 2;
243
244 if (point_x >= client.x and point_x < client.x + client_w and
245 point_y >= client.y and point_y < client.y + client_h)
246 {
247 return client;
248 }
249 }
250 current = client.next;
251 }
252 return null;
253 }
254
255 /// Moves `client` to just before `target` in the monitor's client list.
256 /// Does nothing if they are on different monitors.
257 pub fn insertBefore(client: *Client, target: *Client) void {
258 const monitor = target.monitor orelse return;
259 if (client.monitor != monitor) return;
260
261 detach(client);
262
263 if (monitor.clients == target) {
264 client.next = target;
265 monitor.clients = client;
266 return;
267 }
268
269 var current = monitor.clients;
270 while (current) |iter| {
271 if (iter.next == target) {
272 client.next = target;
273 iter.next = client;
274 return;
275 }
276 current = iter.next;
277 }
278 }
279
280 /// Moves `client` to just after `target` in the monitor's client list.
281 /// Does nothing if they are on different monitors.
282 pub fn insertAfter(client: *Client, target: *Client) void {
283 const monitor = target.monitor orelse return;
284 if (client.monitor != monitor) return;
285
286 detach(client);
287
288 if (monitor.clients == target) {
289 client.next = target.next;
290 monitor.clients.?.next = client;
291 return;
292 }
293
294 var current = monitor.clients;
295 while (current) |iter| {
296 if (iter.next == target) {
297 client.next = target.next;
298 iter.next.?.next = client;
299 return;
300 }
301 current = iter.next;
302 }
303 }
304
305 /// Swaps the positions of `client_a` and `client_b` in their shared monitor's
306 /// client list. Does nothing if they are on different monitors.
307 pub fn swapClients(client_a: *Client, client_b: *Client) void {
308 const monitor = client_a.monitor orelse return;
309 if (client_b.monitor != monitor) return;
310
311 var prev_a: ?*Client = null;
312 var prev_b: ?*Client = null;
313 var iter = monitor.clients;
314
315 while (iter) |client| {
316 if (client.next == client_a) prev_a = client;
317 if (client.next == client_b) prev_b = client;
318 iter = client.next;
319 }
320
321 const next_a = client_a.next;
322 const next_b = client_b.next;
323
324 if (next_a == client_b) {
325 client_a.next = next_b;
326 client_b.next = client_a;
327 if (prev_a) |prev| prev.next = client_b else monitor.clients = client_b;
328 } else if (next_b == client_a) {
329 client_b.next = next_a;
330 client_a.next = client_b;
331 if (prev_b) |prev| prev.next = client_a else monitor.clients = client_a;
332 } else {
333 client_a.next = next_b;
334 client_b.next = next_a;
335 if (prev_a) |prev| prev.next = client_b else monitor.clients = client_b;
336 if (prev_b) |prev| prev.next = client_a else monitor.clients = client_a;
337 }
338 }