tony / oxwm

refactor(client): remove allocator global, document functions and `window_to_client` now takes a monitors list Browse files
- Removed the allocator global and `init()` function. create and destroy
now take allocator as a parameter.
- window_to_client no longer does a hidden `@import("monitor.zig")` to
reach the global. It now takes `monitors: ?*Monitor` as its first
parameter.
emzywastaken committed 2026-02-21 parent 5660dcb commit c7d9989d6ce32ef3e510168640c089d2c872ba36
Showing 2 changed files with 57 additions and 65 deletions. UnifiedSplit
+45 −52 src/client.zig
@@ -38,22 +38,19 @@ pub const Client = struct {
38 38 window: xlib.Window = 0,
39 39 };
40 40
41 -var allocator: std.mem.Allocator = undefined;
42 -
43 -pub fn init(alloc: std.mem.Allocator) void {
44 - allocator = alloc;
45 -}
46 -
47 -pub fn create(window: xlib.Window) ?*Client {
41 +/// Initialises a new `Client` for the given X window.
42 +pub fn create(allocator: std.mem.Allocator, window: xlib.Window) ?*Client {
48 43 const client = allocator.create(Client) catch return null;
49 44 client.* = Client{ .window = window };
50 45 return client;
51 46 }
52 47
53 -pub fn destroy(client: *Client) void {
48 +/// Frees a client previously returned by `create`.
49 +pub fn destroy(allocator: std.mem.Allocator, client: *Client) void {
54 50 allocator.destroy(client);
55 51 }
56 52
53 +/// Prepends `client` to the front of its monitor's client list.
57 54 pub fn attach(client: *Client) void {
58 55 if (client.monitor) |monitor| {
59 56 client.next = monitor.clients;
@@ -61,6 +58,7 @@ pub fn attach(client: *Client) void {
61 58 }
62 59 }
63 60
61 +/// Removes `client` from its monitor's client list.
64 62 pub fn detach(client: *Client) void {
65 63 if (client.monitor) |monitor| {
66 64 var current_ptr: *?*Client = &monitor.clients;
@@ -74,6 +72,7 @@ pub fn detach(client: *Client) void {
74 72 }
75 73 }
76 74
75 +/// Prepends `client` to the front of its monitor's focus stack.
77 76 pub fn attach_stack(client: *Client) void {
78 77 if (client.monitor) |monitor| {
79 78 client.stack_next = monitor.stack;
@@ -81,6 +80,7 @@ pub fn attach_stack(client: *Client) void {
81 80 }
82 81 }
83 82
83 +/// Removes `client` from its monitor's focus stack.
84 84 pub fn detach_stack(client: *Client) void {
85 85 if (client.monitor) |monitor| {
86 86 var current_ptr: *?*Client = &monitor.stack;
@@ -94,15 +94,15 @@ pub fn detach_stack(client: *Client) void {
94 94 }
95 95 }
96 96
97 -pub fn window_to_client(window: xlib.Window) ?*Client {
98 - const monitor_mod = @import("monitor.zig");
99 - var current_monitor = monitor_mod.monitors;
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 window_to_client(monitors: ?*Monitor, window: xlib.Window) ?*Client {
101 + var current_monitor = monitors;
100 102 while (current_monitor) |monitor| {
101 103 var current_client = monitor.clients;
102 104 while (current_client) |client| {
103 - if (client.window == window) {
104 - return client;
105 - }
105 + if (client.window == window) return client;
106 106 current_client = client.next;
107 107 }
108 108 current_monitor = monitor.next;
@@ -110,17 +110,8 @@ pub fn window_to_client(window: xlib.Window) ?*Client {
110 110 return null;
111 111 }
112 112
113 -pub fn next_tiled(client: ?*Client) ?*Client {
114 - var current = client;
115 - while (current) |iter| {
116 - if (!iter.is_floating and is_visible(iter)) {
117 - return iter;
118 - }
119 - current = iter.next;
120 - }
121 - return null;
122 -}
123 -
113 +/// Returns true if `client` is visible on its monitor's currently selected
114 +/// tag set.
124 115 pub fn is_visible(client: *Client) bool {
125 116 if (client.monitor) |monitor| {
126 117 return (client.tags & monitor.tagset[monitor.sel_tags]) != 0;
@@ -128,22 +119,35 @@ pub fn is_visible(client: *Client) bool {
128 119 return false;
129 120 }
130 121
122 +/// Returns true if `client` is visible on the given tag bitmask.
131 123 pub fn is_visible_on_tag(client: *Client, tags: u32) bool {
132 124 return (client.tags & tags) != 0;
133 125 }
134 126
127 +/// Returns the first non-floating, visible client at or after `client`.
128 +pub fn next_tiled(client: ?*Client) ?*Client {
129 + var current = client;
130 + while (current) |iter| {
131 + if (!iter.is_floating and is_visible(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.
135 139 pub fn next_tagged(client: *Client) ?*Client {
136 140 const monitor = client.monitor orelse return null;
137 141 var walked = monitor.clients;
138 142 while (walked) |iter| {
139 - if (!iter.is_floating and is_visible_on_tag(iter, client.tags)) {
140 - return iter;
141 - }
143 + if (!iter.is_floating and is_visible_on_tag(iter, client.tags)) return iter;
142 144 walked = iter.next;
143 145 }
144 146 return null;
145 147 }
146 148
149 +/// Inserts `client` just after the first client that shares its tags,
150 +/// falling back to prepend if none exists.
147 151 pub fn attach_aside(client: *Client) void {
148 152 const at = next_tagged(client);
149 153 if (at == null) {
@@ -154,6 +158,7 @@ pub fn attach_aside(client: *Client) void {
154 158 at.?.next = client;
155 159 }
156 160
161 +/// Counts non-floating, visible clients on `monitor`.
157 162 pub fn count_tiled(monitor: *Monitor) u32 {
158 163 var count: u32 = 0;
159 164 var current = next_tiled(monitor.clients);
@@ -164,19 +169,19 @@ pub fn count_tiled(monitor: *Monitor) u32 {
164 169 return count;
165 170 }
166 171
172 +/// Returns the tiled client on `monitor` whose bounds contain (`point_x`,
173 +/// `point_y`), excluding `exclude`. Returns null if none found.
167 174 pub fn tiled_window_at(exclude: *Client, monitor: *Monitor, point_x: i32, point_y: i32) ?*Client {
168 175 const tags = monitor.tagset[monitor.sel_tags];
169 176 var current = monitor.clients;
170 177
171 178 while (current) |client| {
172 179 if (client != exclude and !client.is_floating and (client.tags & tags) != 0) {
173 - const client_x = client.x;
174 - const client_y = client.y;
175 180 const client_w = client.width + client.border_width * 2;
176 181 const client_h = client.height + client.border_width * 2;
177 182
178 - if (point_x >= client_x and point_x < client_x + client_w and
179 - point_y >= client_y and point_y < client_y + client_h)
183 + if (point_x >= client.x and point_x < client.x + client_w and
184 + point_y >= client.y and point_y < client.y + client_h)
180 185 {
181 186 return client;
182 187 }
@@ -186,6 +191,8 @@ pub fn tiled_window_at(exclude: *Client, monitor: *Monitor, point_x: i32, point_
186 191 return null;
187 192 }
188 193
194 +/// Moves `client` to just before `target` in the monitor's client list.
195 +/// Does nothing if they are on different monitors.
189 196 pub fn insert_before(client: *Client, target: *Client) void {
190 197 const monitor = target.monitor orelse return;
191 198 if (client.monitor != monitor) return;
@@ -209,6 +216,8 @@ pub fn insert_before(client: *Client, target: *Client) void {
209 216 }
210 217 }
211 218
219 +/// Swaps the positions of `client_a` and `client_b` in their shared monitor's
220 +/// client list. Does nothing if they are on different monitors.
212 221 pub fn swap_clients(client_a: *Client, client_b: *Client) void {
213 222 const monitor = client_a.monitor orelse return;
214 223 if (client_b.monitor != monitor) return;
@@ -229,31 +238,15 @@ pub fn swap_clients(client_a: *Client, client_b: *Client) void {
229 238 if (next_a == client_b) {
230 239 client_a.next = next_b;
231 240 client_b.next = client_a;
232 - if (prev_a) |prev| {
233 - prev.next = client_b;
234 - } else {
235 - monitor.clients = client_b;
236 - }
241 + if (prev_a) |prev| prev.next = client_b else monitor.clients = client_b;
237 242 } else if (next_b == client_a) {
238 243 client_b.next = next_a;
239 244 client_a.next = client_b;
240 - if (prev_b) |prev| {
241 - prev.next = client_a;
242 - } else {
243 - monitor.clients = client_a;
244 - }
245 + if (prev_b) |prev| prev.next = client_a else monitor.clients = client_a;
245 246 } else {
246 247 client_a.next = next_b;
247 248 client_b.next = next_a;
248 - if (prev_a) |prev| {
249 - prev.next = client_b;
250 - } else {
251 - monitor.clients = client_b;
252 - }
253 - if (prev_b) |prev| {
254 - prev.next = client_a;
255 - } else {
256 - monitor.clients = client_a;
257 - }
249 + if (prev_a) |prev| prev.next = client_b else monitor.clients = client_b;
250 + if (prev_b) |prev| prev.next = client_a else monitor.clients = client_a;
258 251 }
259 252 }
+12 −13 src/main.zig
@@ -249,7 +249,6 @@ pub fn main() !void {
249 249
250 250 cursors = Cursors.init(&display);
251 251 _ = xlib.XDefineCursor(display.handle, display.root, cursors.normal);
252 - client_mod.init(allocator);
253 252 monitor_mod.init(allocator);
254 253 tiling.set_display(display.handle);
255 254 tiling.set_screen_size(display.screen_width(), display.screen_height());
@@ -697,7 +696,7 @@ fn handle_map_request(display: *Display, event: *xlib.XMapRequestEvent) void {
697 696 if (window_attributes.override_redirect != 0) {
698 697 return;
699 698 }
700 - if (client_mod.window_to_client(event.window) != null) {
699 + if (client_mod.window_to_client(monitor_mod.monitors, event.window) != null) {
701 700 return;
702 701 }
703 702
@@ -705,7 +704,7 @@ fn handle_map_request(display: *Display, event: *xlib.XMapRequestEvent) void {
705 704 }
706 705
707 706 fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttributes) void {
708 - const client = client_mod.create(win) orelse return;
707 + const client = client_mod.create(gpa.allocator(), win) orelse return;
709 708 var trans: xlib.Window = 0;
710 709
711 710 client.x = window_attrs.x;
@@ -722,7 +721,7 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib
722 721 update_title(display, client);
723 722
724 723 if (xlib.XGetTransientForHint(display.handle, win, &trans) != 0) {
725 - if (client_mod.window_to_client(trans)) |transient_client| {
724 + if (client_mod.window_to_client(monitor_mod.monitors, trans)) |transient_client| {
726 725 client.monitor = transient_client.monitor;
727 726 client.tags = transient_client.tags;
728 727 }
@@ -790,7 +789,7 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib
790 789 }
791 790
792 791 fn handle_configure_request(display: *Display, event: *xlib.XConfigureRequestEvent) void {
793 - const client = client_mod.window_to_client(event.window);
792 + const client = client_mod.window_to_client(monitor_mod.monitors, event.window);
794 793
795 794 if (client) |managed_client| {
796 795 if ((event.value_mask & xlib.c.CWBorderWidth) != 0) {
@@ -1816,7 +1815,7 @@ fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void {
1816 1815 return;
1817 1816 }
1818 1817
1819 - const click_client = client_mod.window_to_client(event.window);
1818 + const click_client = client_mod.window_to_client(monitor_mod.monitors, event.window);
1820 1819 if (click_client) |found_client| {
1821 1820 focus(display, found_client);
1822 1821 if (monitor_mod.selected_monitor) |selmon| {
@@ -1848,7 +1847,7 @@ fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void {
1848 1847 }
1849 1848
1850 1849 fn handle_client_message(display: *Display, event: *xlib.XClientMessageEvent) void {
1851 - const client = client_mod.window_to_client(event.window) orelse return;
1850 + const client = client_mod.window_to_client(monitor_mod.monitors, event.window) orelse return;
1852 1851
1853 1852 if (event.message_type == atoms.net_wm_state) {
1854 1853 const action = event.data.l[0];
@@ -1877,13 +1876,13 @@ fn handle_client_message(display: *Display, event: *xlib.XClientMessageEvent) vo
1877 1876 }
1878 1877
1879 1878 fn handle_destroy_notify(display: *Display, event: *xlib.XDestroyWindowEvent) void {
1880 - const client = client_mod.window_to_client(event.window) orelse return;
1879 + const client = client_mod.window_to_client(monitor_mod.monitors, event.window) orelse return;
1881 1880 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
1882 1881 unmanage(display, client);
1883 1882 }
1884 1883
1885 1884 fn handle_unmap_notify(display: *Display, event: *xlib.XUnmapEvent) void {
1886 - const client = client_mod.window_to_client(event.window) orelse return;
1885 + const client = client_mod.window_to_client(monitor_mod.monitors, event.window) orelse return;
1887 1886 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
1888 1887 unmanage(display, client);
1889 1888 }
@@ -1932,7 +1931,7 @@ fn unmanage(display: *Display, client: *Client) void {
1932 1931 }
1933 1932 }
1934 1933
1935 - client_mod.destroy(client);
1934 + client_mod.destroy(gpa.allocator(), client);
1936 1935 update_client_list(display);
1937 1936 bar_mod.invalidate_bars();
1938 1937 }
@@ -1942,7 +1941,7 @@ fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent) void {
1942 1941 return;
1943 1942 }
1944 1943
1945 - const client = client_mod.window_to_client(event.window);
1944 + const client = client_mod.window_to_client(monitor_mod.monitors, event.window);
1946 1945 const target_mon = if (client) |c| c.monitor else monitor_mod.window_to_monitor(display.handle, display.root, event.window);
1947 1946 const selmon = monitor_mod.selected_monitor;
1948 1947
@@ -2001,12 +2000,12 @@ fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void {
2001 2000 return;
2002 2001 }
2003 2002
2004 - const client = client_mod.window_to_client(event.window) orelse return;
2003 + const client = client_mod.window_to_client(monitor_mod.monitors, event.window) orelse return;
2005 2004
2006 2005 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
2007 2006 var trans: xlib.Window = 0;
2008 2007 if (!client.is_floating and xlib.XGetTransientForHint(display.handle, client.window, &trans) != 0) {
2009 - client.is_floating = client_mod.window_to_client(trans) != null;
2008 + client.is_floating = client_mod.window_to_client(monitor_mod.monitors, trans) != null;
2010 2009 if (client.is_floating) {
2011 2010 if (client.monitor) |monitor| {
2012 2011 arrange(monitor);