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