tony / oxwm

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