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