tony / oxwm

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