|
|
@@ -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 |
} |