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 window: xlib.Window = 0, 38 window: xlib.Window = 0,
39 }; 39 };
40 40
41 -var allocator: std.mem.Allocator = undefined; 41 +/// Initialises a new `Client` for the given X window.
42 - 42 +pub fn create(allocator: std.mem.Allocator, window: xlib.Window) ?*Client {
43 -pub fn init(alloc: std.mem.Allocator) void {
44 - allocator = alloc;
45 -}
46 -
47 -pub fn create(window: xlib.Window) ?*Client {
48 const client = allocator.create(Client) catch return null; 43 const client = allocator.create(Client) catch return null;
49 client.* = Client{ .window = window }; 44 client.* = Client{ .window = window };
50 return client; 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 allocator.destroy(client); 50 allocator.destroy(client);
55 } 51 }
56 52
53 +/// Prepends `client` to the front of its monitor's client list.
57 pub fn attach(client: *Client) void { 54 pub fn attach(client: *Client) void {
58 if (client.monitor) |monitor| { 55 if (client.monitor) |monitor| {
59 client.next = monitor.clients; 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 pub fn detach(client: *Client) void { 62 pub fn detach(client: *Client) void {
65 if (client.monitor) |monitor| { 63 if (client.monitor) |monitor| {
66 var current_ptr: *?*Client = &monitor.clients; 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 pub fn attach_stack(client: *Client) void { 76 pub fn attach_stack(client: *Client) void {
78 if (client.monitor) |monitor| { 77 if (client.monitor) |monitor| {
79 client.stack_next = monitor.stack; 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 pub fn detach_stack(client: *Client) void { 84 pub fn detach_stack(client: *Client) void {
85 if (client.monitor) |monitor| { 85 if (client.monitor) |monitor| {
86 var current_ptr: *?*Client = &monitor.stack; 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 { 97 +/// Searches all monitors for a client whose X window matches `window`.
98 - const monitor_mod = @import("monitor.zig"); 98 +///
99 - var current_monitor = monitor_mod.monitors; 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 while (current_monitor) |monitor| { 102 while (current_monitor) |monitor| {
101 var current_client = monitor.clients; 103 var current_client = monitor.clients;
102 while (current_client) |client| { 104 while (current_client) |client| {
103 - if (client.window == window) { 105 + if (client.window == window) return client;
104 - return client;
105 - }
106 current_client = client.next; 106 current_client = client.next;
107 } 107 }
108 current_monitor = monitor.next; 108 current_monitor = monitor.next;
@@ -110,17 +110,8 @@ pub fn window_to_client(window: xlib.Window) ?*Client {
110 return null; 110 return null;
111 } 111 }
112 112
113 -pub fn next_tiled(client: ?*Client) ?*Client { 113 +/// Returns true if `client` is visible on its monitor's currently selected
114 - var current = client; 114 +/// tag set.
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 -
124 pub fn is_visible(client: *Client) bool { 115 pub fn is_visible(client: *Client) bool {
125 if (client.monitor) |monitor| { 116 if (client.monitor) |monitor| {
126 return (client.tags & monitor.tagset[monitor.sel_tags]) != 0; 117 return (client.tags & monitor.tagset[monitor.sel_tags]) != 0;
@@ -128,22 +119,35 @@ pub fn is_visible(client: *Client) bool {
128 return false; 119 return false;
129 } 120 }
130 121
122 +/// Returns true if `client` is visible on the given tag bitmask.
131 pub fn is_visible_on_tag(client: *Client, tags: u32) bool { 123 pub fn is_visible_on_tag(client: *Client, tags: u32) bool {
132 return (client.tags & tags) != 0; 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 pub fn next_tagged(client: *Client) ?*Client { 139 pub fn next_tagged(client: *Client) ?*Client {
136 const monitor = client.monitor orelse return null; 140 const monitor = client.monitor orelse return null;
137 var walked = monitor.clients; 141 var walked = monitor.clients;
138 while (walked) |iter| { 142 while (walked) |iter| {
139 - if (!iter.is_floating and is_visible_on_tag(iter, client.tags)) { 143 + if (!iter.is_floating and is_visible_on_tag(iter, client.tags)) return iter;
140 - return iter;
141 - }
142 walked = iter.next; 144 walked = iter.next;
143 } 145 }
144 return null; 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 pub fn attach_aside(client: *Client) void { 151 pub fn attach_aside(client: *Client) void {
148 const at = next_tagged(client); 152 const at = next_tagged(client);
149 if (at == null) { 153 if (at == null) {
@@ -154,6 +158,7 @@ pub fn attach_aside(client: *Client) void {
154 at.?.next = client; 158 at.?.next = client;
155 } 159 }
156 160
161 +/// Counts non-floating, visible clients on `monitor`.
157 pub fn count_tiled(monitor: *Monitor) u32 { 162 pub fn count_tiled(monitor: *Monitor) u32 {
158 var count: u32 = 0; 163 var count: u32 = 0;
159 var current = next_tiled(monitor.clients); 164 var current = next_tiled(monitor.clients);
@@ -164,19 +169,19 @@ pub fn count_tiled(monitor: *Monitor) u32 {
164 return count; 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 pub fn tiled_window_at(exclude: *Client, monitor: *Monitor, point_x: i32, point_y: i32) ?*Client { 174 pub fn tiled_window_at(exclude: *Client, monitor: *Monitor, point_x: i32, point_y: i32) ?*Client {
168 const tags = monitor.tagset[monitor.sel_tags]; 175 const tags = monitor.tagset[monitor.sel_tags];
169 var current = monitor.clients; 176 var current = monitor.clients;
170 177
171 while (current) |client| { 178 while (current) |client| {
172 if (client != exclude and !client.is_floating and (client.tags & tags) != 0) { 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 const client_w = client.width + client.border_width * 2; 180 const client_w = client.width + client.border_width * 2;
176 const client_h = client.height + client.border_width * 2; 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 183 + 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) 184 + point_y >= client.y and point_y < client.y + client_h)
180 { 185 {
181 return client; 186 return client;
182 } 187 }
@@ -186,6 +191,8 @@ pub fn tiled_window_at(exclude: *Client, monitor: *Monitor, point_x: i32, point_
186 return null; 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 pub fn insert_before(client: *Client, target: *Client) void { 196 pub fn insert_before(client: *Client, target: *Client) void {
190 const monitor = target.monitor orelse return; 197 const monitor = target.monitor orelse return;
191 if (client.monitor != monitor) return; 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 pub fn swap_clients(client_a: *Client, client_b: *Client) void { 221 pub fn swap_clients(client_a: *Client, client_b: *Client) void {
213 const monitor = client_a.monitor orelse return; 222 const monitor = client_a.monitor orelse return;
214 if (client_b.monitor != monitor) return; 223 if (client_b.monitor != monitor) return;
@@ -229,31 +238,15 @@ pub fn swap_clients(client_a: *Client, client_b: *Client) void {
229 if (next_a == client_b) { 238 if (next_a == client_b) {
230 client_a.next = next_b; 239 client_a.next = next_b;
231 client_b.next = client_a; 240 client_b.next = client_a;
232 - if (prev_a) |prev| { 241 + if (prev_a) |prev| prev.next = client_b else monitor.clients = client_b;
233 - prev.next = client_b;
234 - } else {
235 - monitor.clients = client_b;
236 - }
237 } else if (next_b == client_a) { 242 } else if (next_b == client_a) {
238 client_b.next = next_a; 243 client_b.next = next_a;
239 client_a.next = client_b; 244 client_a.next = client_b;
240 - if (prev_b) |prev| { 245 + if (prev_b) |prev| prev.next = client_a else monitor.clients = client_a;
241 - prev.next = client_a;
242 - } else {
243 - monitor.clients = client_a;
244 - }
245 } else { 246 } else {
246 client_a.next = next_b; 247 client_a.next = next_b;
247 client_b.next = next_a; 248 client_b.next = next_a;
248 - if (prev_a) |prev| { 249 + if (prev_a) |prev| prev.next = client_b else monitor.clients = client_b;
249 - prev.next = client_b; 250 + if (prev_b) |prev| prev.next = client_a else monitor.clients = client_a;
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 - }
258 } 251 }
259 } 252 }
+12 −13 src/main.zig
@@ -249,7 +249,6 @@ pub fn main() !void {
249 249
250 cursors = Cursors.init(&display); 250 cursors = Cursors.init(&display);
251 _ = xlib.XDefineCursor(display.handle, display.root, cursors.normal); 251 _ = xlib.XDefineCursor(display.handle, display.root, cursors.normal);
252 - client_mod.init(allocator);
253 monitor_mod.init(allocator); 252 monitor_mod.init(allocator);
254 tiling.set_display(display.handle); 253 tiling.set_display(display.handle);
255 tiling.set_screen_size(display.screen_width(), display.screen_height()); 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 if (window_attributes.override_redirect != 0) { 696 if (window_attributes.override_redirect != 0) {
698 return; 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 return; 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 fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttributes) void { 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 var trans: xlib.Window = 0; 708 var trans: xlib.Window = 0;
710 709
711 client.x = window_attrs.x; 710 client.x = window_attrs.x;
@@ -722,7 +721,7 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib
722 update_title(display, client); 721 update_title(display, client);
723 722
724 if (xlib.XGetTransientForHint(display.handle, win, &trans) != 0) { 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 client.monitor = transient_client.monitor; 725 client.monitor = transient_client.monitor;
727 client.tags = transient_client.tags; 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 fn handle_configure_request(display: *Display, event: *xlib.XConfigureRequestEvent) void { 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 if (client) |managed_client| { 794 if (client) |managed_client| {
796 if ((event.value_mask & xlib.c.CWBorderWidth) != 0) { 795 if ((event.value_mask & xlib.c.CWBorderWidth) != 0) {
@@ -1816,7 +1815,7 @@ fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void {
1816 return; 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 if (click_client) |found_client| { 1819 if (click_client) |found_client| {
1821 focus(display, found_client); 1820 focus(display, found_client);
1822 if (monitor_mod.selected_monitor) |selmon| { 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 fn handle_client_message(display: *Display, event: *xlib.XClientMessageEvent) void { 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 if (event.message_type == atoms.net_wm_state) { 1852 if (event.message_type == atoms.net_wm_state) {
1854 const action = event.data.l[0]; 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 fn handle_destroy_notify(display: *Display, event: *xlib.XDestroyWindowEvent) void { 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 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window}); 1880 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
1882 unmanage(display, client); 1881 unmanage(display, client);
1883 } 1882 }
1884 1883
1885 fn handle_unmap_notify(display: *Display, event: *xlib.XUnmapEvent) void { 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 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window}); 1886 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
1888 unmanage(display, client); 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 update_client_list(display); 1935 update_client_list(display);
1937 bar_mod.invalidate_bars(); 1936 bar_mod.invalidate_bars();
1938 } 1937 }
@@ -1942,7 +1941,7 @@ fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent) void {
1942 return; 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 const target_mon = if (client) |c| c.monitor else monitor_mod.window_to_monitor(display.handle, display.root, event.window); 1945 const target_mon = if (client) |c| c.monitor else monitor_mod.window_to_monitor(display.handle, display.root, event.window);
1947 const selmon = monitor_mod.selected_monitor; 1946 const selmon = monitor_mod.selected_monitor;
1948 1947
@@ -2001,12 +2000,12 @@ fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void {
2001 return; 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 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) { 2005 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
2007 var trans: xlib.Window = 0; 2006 var trans: xlib.Window = 0;
2008 if (!client.is_floating and xlib.XGetTransientForHint(display.handle, client.window, &trans) != 0) { 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 if (client.is_floating) { 2009 if (client.is_floating) {
2011 if (client.monitor) |monitor| { 2010 if (client.monitor) |monitor| {
2012 arrange(monitor); 2011 arrange(monitor);