| 1 | const std = @import("std"); |
| 2 | const client_mod = @import("../client.zig"); |
| 3 | const monitor_mod = @import("../monitor.zig"); |
| 4 | const xlib = @import("../x11/xlib.zig"); |
| 5 | |
| 6 | const Client = client_mod.Client; |
| 7 | const Monitor = monitor_mod.Monitor; |
| 8 | |
| 9 | pub const layout = monitor_mod.Layout{ |
| 10 | .symbol = "[]=", |
| 11 | .arrange_fn = tile, |
| 12 | }; |
| 13 | |
| 14 | pub var display_handle: ?*xlib.Display = null; |
| 15 | pub var screen_width: i32 = 0; |
| 16 | pub var screen_height: i32 = 0; |
| 17 | pub var bar_height: i32 = 0; |
| 18 | |
| 19 | pub fn setDisplay(display: *xlib.Display) void { |
| 20 | display_handle = display; |
| 21 | } |
| 22 | |
| 23 | pub fn setScreenSize(width: i32, height: i32) void { |
| 24 | screen_width = width; |
| 25 | screen_height = height; |
| 26 | } |
| 27 | |
| 28 | pub fn setBarHeight(height: i32) void { |
| 29 | bar_height = height; |
| 30 | } |
| 31 | |
| 32 | pub fn tile(monitor: *Monitor) void { |
| 33 | var gap_outer_h: i32 = 0; |
| 34 | var gap_outer_v: i32 = 0; |
| 35 | var gap_inner_h: i32 = 0; |
| 36 | var gap_inner_v: i32 = 0; |
| 37 | var client_count: u32 = 0; |
| 38 | |
| 39 | getGaps(monitor, &gap_outer_h, &gap_outer_v, &gap_inner_h, &gap_inner_v, &client_count); |
| 40 | if (client_count == 0) return; |
| 41 | |
| 42 | const nmaster: i32 = monitor.nmaster; |
| 43 | const nmaster_count: u32 = @intCast(@max(0, nmaster)); |
| 44 | |
| 45 | const master_x: i32 = monitor.win_x + gap_outer_v; |
| 46 | var master_y: i32 = monitor.win_y + gap_outer_h; |
| 47 | const master_height: i32 = monitor.win_h - 2 * gap_outer_h - gap_inner_h * (@as(i32, @intCast(@min(client_count, nmaster_count))) - 1); |
| 48 | var master_width: i32 = monitor.win_w - 2 * gap_outer_v; |
| 49 | |
| 50 | var stack_x: i32 = master_x; |
| 51 | var stack_y: i32 = monitor.win_y + gap_outer_h; |
| 52 | const stack_height: i32 = monitor.win_h - 2 * gap_outer_h - gap_inner_h * (@as(i32, @intCast(client_count)) - nmaster - 1); |
| 53 | var stack_width: i32 = master_width; |
| 54 | |
| 55 | if (nmaster > 0 and client_count > nmaster_count) { |
| 56 | stack_width = @intFromFloat(@as(f32, @floatFromInt(master_width - gap_inner_v)) * (1.0 - monitor.mfact)); |
| 57 | master_width = master_width - gap_inner_v - stack_width; |
| 58 | stack_x = master_x + master_width + gap_inner_v; |
| 59 | } |
| 60 | |
| 61 | var master_facts: f32 = 0; |
| 62 | var stack_facts: f32 = 0; |
| 63 | var master_rest: i32 = 0; |
| 64 | var stack_rest: i32 = 0; |
| 65 | getFacts(monitor, master_height, stack_height, &master_facts, &stack_facts, &master_rest, &stack_rest); |
| 66 | |
| 67 | var index: u32 = 0; |
| 68 | var current = client_mod.nextTiled(monitor.clients); |
| 69 | while (current) |client| : (current = client_mod.nextTiled(client.next)) { |
| 70 | if (index < nmaster_count) { |
| 71 | const height = @as(i32, @intFromFloat(@as(f32, @floatFromInt(master_height)) / master_facts)) + (if (index < @as(u32, @intCast(master_rest))) @as(i32, 1) else @as(i32, 0)) - 2 * client.border_width; |
| 72 | resize(client, master_x, master_y, master_width - 2 * client.border_width, height, false); |
| 73 | master_y += getClientHeight(client) + gap_inner_h; |
| 74 | } else { |
| 75 | const stack_index = index - nmaster_count; |
| 76 | const height = @as(i32, @intFromFloat(@as(f32, @floatFromInt(stack_height)) / stack_facts)) + (if (stack_index < @as(u32, @intCast(stack_rest))) @as(i32, 1) else @as(i32, 0)) - 2 * client.border_width; |
| 77 | resize(client, stack_x, stack_y, stack_width - 2 * client.border_width, height, false); |
| 78 | stack_y += getClientHeight(client) + gap_inner_h; |
| 79 | } |
| 80 | index += 1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | fn getGaps(monitor: *Monitor, gap_outer_h: *i32, gap_outer_v: *i32, gap_inner_h: *i32, gap_inner_v: *i32, client_count: *u32) void { |
| 85 | var count: u32 = 0; |
| 86 | var current = client_mod.nextTiled(monitor.clients); |
| 87 | while (current) |client| : (current = client_mod.nextTiled(client.next)) { |
| 88 | count += 1; |
| 89 | } |
| 90 | |
| 91 | if (monitor.smartgaps_enabled and count == 1) { |
| 92 | gap_outer_h.* = 0; |
| 93 | gap_outer_v.* = 0; |
| 94 | } else { |
| 95 | gap_outer_h.* = monitor.gap_outer_h; |
| 96 | gap_outer_v.* = monitor.gap_outer_v; |
| 97 | } |
| 98 | |
| 99 | gap_inner_h.* = monitor.gap_inner_h; |
| 100 | gap_inner_v.* = monitor.gap_inner_v; |
| 101 | client_count.* = count; |
| 102 | } |
| 103 | |
| 104 | fn getFacts(monitor: *Monitor, master_size: i32, stack_size: i32, master_factor: *f32, stack_factor: *f32, master_rest: *i32, stack_rest: *i32) void { |
| 105 | var count: u32 = 0; |
| 106 | var current = client_mod.nextTiled(monitor.clients); |
| 107 | while (current) |client| : (current = client_mod.nextTiled(client.next)) { |
| 108 | count += 1; |
| 109 | } |
| 110 | |
| 111 | const nmaster_count: u32 = @intCast(@max(0, monitor.nmaster)); |
| 112 | const master_facts: f32 = @floatFromInt(@min(count, nmaster_count)); |
| 113 | const stack_facts: f32 = @floatFromInt(if (count > nmaster_count) count - nmaster_count else 0); |
| 114 | |
| 115 | var master_total: i32 = 0; |
| 116 | var stack_total: i32 = 0; |
| 117 | |
| 118 | if (master_facts > 0) { |
| 119 | master_total = @as(i32, @intFromFloat(@as(f32, @floatFromInt(master_size)) / master_facts)) * @as(i32, @intFromFloat(master_facts)); |
| 120 | } |
| 121 | if (stack_facts > 0) { |
| 122 | stack_total = @as(i32, @intFromFloat(@as(f32, @floatFromInt(stack_size)) / stack_facts)) * @as(i32, @intFromFloat(stack_facts)); |
| 123 | } |
| 124 | |
| 125 | master_factor.* = master_facts; |
| 126 | stack_factor.* = stack_facts; |
| 127 | master_rest.* = master_size - master_total; |
| 128 | stack_rest.* = stack_size - stack_total; |
| 129 | } |
| 130 | |
| 131 | fn getClientWidth(client: *Client) i32 { |
| 132 | return client.width + 2 * client.border_width; |
| 133 | } |
| 134 | |
| 135 | fn getClientHeight(client: *Client) i32 { |
| 136 | return client.height + 2 * client.border_width; |
| 137 | } |
| 138 | |
| 139 | pub fn applySizeHints(client: *Client, target_x: *i32, target_y: *i32, target_width: *i32, target_height: *i32, interact: bool) bool { |
| 140 | const monitor = client.monitor orelse return false; |
| 141 | |
| 142 | target_width.* = @max(1, target_width.*); |
| 143 | target_height.* = @max(1, target_height.*); |
| 144 | |
| 145 | if (interact) { |
| 146 | if (target_x.* > screen_width) { |
| 147 | target_x.* = screen_width - getClientWidth(client); |
| 148 | } |
| 149 | if (target_y.* > screen_height) { |
| 150 | target_y.* = screen_height - getClientHeight(client); |
| 151 | } |
| 152 | if (target_x.* + target_width.* + 2 * client.border_width < 0) { |
| 153 | target_x.* = 0; |
| 154 | } |
| 155 | if (target_y.* + target_height.* + 2 * client.border_width < 0) { |
| 156 | target_y.* = 0; |
| 157 | } |
| 158 | } else { |
| 159 | if (target_x.* >= monitor.win_x + monitor.win_w) { |
| 160 | target_x.* = monitor.win_x + monitor.win_w - getClientWidth(client); |
| 161 | } |
| 162 | if (target_y.* >= monitor.win_y + monitor.win_h) { |
| 163 | target_y.* = monitor.win_y + monitor.win_h - getClientHeight(client); |
| 164 | } |
| 165 | if (target_x.* + target_width.* + 2 * client.border_width <= monitor.win_x) { |
| 166 | target_x.* = monitor.win_x; |
| 167 | } |
| 168 | if (target_y.* + target_height.* + 2 * client.border_width <= monitor.win_y) { |
| 169 | target_y.* = monitor.win_y; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | if (target_height.* < bar_height) { |
| 174 | target_height.* = bar_height; |
| 175 | } |
| 176 | if (target_width.* < bar_height) { |
| 177 | target_width.* = bar_height; |
| 178 | } |
| 179 | |
| 180 | if (client.is_floating or monitor.lt[monitor.sel_lt] == null) { |
| 181 | const base_is_min = client.base_width == client.min_width and client.base_height == client.min_height; |
| 182 | |
| 183 | var adjusted_width = target_width.*; |
| 184 | var adjusted_height = target_height.*; |
| 185 | |
| 186 | if (!base_is_min) { |
| 187 | adjusted_width -= client.base_width; |
| 188 | adjusted_height -= client.base_height; |
| 189 | } |
| 190 | |
| 191 | if (client.min_aspect > 0 and client.max_aspect > 0) { |
| 192 | const width_float: f32 = @floatFromInt(adjusted_width); |
| 193 | const height_float: f32 = @floatFromInt(adjusted_height); |
| 194 | if (client.max_aspect < width_float / height_float) { |
| 195 | adjusted_width = @intFromFloat(height_float * client.max_aspect + 0.5); |
| 196 | } else if (client.min_aspect < height_float / width_float) { |
| 197 | adjusted_height = @intFromFloat(width_float * client.min_aspect + 0.5); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (base_is_min) { |
| 202 | adjusted_width -= client.base_width; |
| 203 | adjusted_height -= client.base_height; |
| 204 | } |
| 205 | |
| 206 | if (client.increment_width > 0) { |
| 207 | adjusted_width -= @mod(adjusted_width, client.increment_width); |
| 208 | } |
| 209 | if (client.increment_height > 0) { |
| 210 | adjusted_height -= @mod(adjusted_height, client.increment_height); |
| 211 | } |
| 212 | |
| 213 | target_width.* = @max(adjusted_width + client.base_width, client.min_width); |
| 214 | target_height.* = @max(adjusted_height + client.base_height, client.min_height); |
| 215 | |
| 216 | if (client.max_width > 0) { |
| 217 | target_width.* = @min(target_width.*, client.max_width); |
| 218 | } |
| 219 | if (client.max_height > 0) { |
| 220 | target_height.* = @min(target_height.*, client.max_height); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return target_x.* != client.x or target_y.* != client.y or target_width.* != client.width or target_height.* != client.height; |
| 225 | } |
| 226 | |
| 227 | pub fn resize(client: *Client, target_x: i32, target_y: i32, target_width: i32, target_height: i32, interact: bool) void { |
| 228 | var final_x = target_x; |
| 229 | var final_y = target_y; |
| 230 | var final_width = target_width; |
| 231 | var final_height = target_height; |
| 232 | |
| 233 | if (applySizeHints(client, &final_x, &final_y, &final_width, &final_height, interact)) { |
| 234 | resizeClient(client, final_x, final_y, final_width, final_height); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | pub fn resizeClient(client: *Client, target_x: i32, target_y: i32, target_width: i32, target_height: i32) void { |
| 239 | client.old_x = client.x; |
| 240 | client.old_y = client.y; |
| 241 | client.old_width = client.width; |
| 242 | client.old_height = client.height; |
| 243 | client.x = target_x; |
| 244 | client.y = target_y; |
| 245 | client.width = target_width; |
| 246 | client.height = target_height; |
| 247 | |
| 248 | const display = display_handle orelse return; |
| 249 | |
| 250 | var window_changes: xlib.c.XWindowChanges = undefined; |
| 251 | window_changes.x = target_x; |
| 252 | window_changes.y = target_y; |
| 253 | window_changes.width = @intCast(@max(1, target_width)); |
| 254 | window_changes.height = @intCast(@max(1, target_height)); |
| 255 | window_changes.border_width = client.border_width; |
| 256 | |
| 257 | _ = xlib.c.XConfigureWindow( |
| 258 | display, |
| 259 | client.window, |
| 260 | xlib.c.CWX | xlib.c.CWY | xlib.c.CWWidth | xlib.c.CWHeight | xlib.c.CWBorderWidth, |
| 261 | &window_changes, |
| 262 | ); |
| 263 | |
| 264 | sendConfigure(client); |
| 265 | _ = xlib.XSync(display, xlib.False); |
| 266 | } |
| 267 | |
| 268 | pub fn sendConfigure(client: *Client) void { |
| 269 | const display = display_handle orelse return; |
| 270 | |
| 271 | var configure_event: xlib.c.XConfigureEvent = undefined; |
| 272 | configure_event.type = xlib.c.ConfigureNotify; |
| 273 | configure_event.display = display; |
| 274 | configure_event.event = client.window; |
| 275 | configure_event.window = client.window; |
| 276 | configure_event.x = client.x; |
| 277 | configure_event.y = client.y; |
| 278 | configure_event.width = client.width; |
| 279 | configure_event.height = client.height; |
| 280 | configure_event.border_width = client.border_width; |
| 281 | configure_event.above = xlib.None; |
| 282 | configure_event.override_redirect = xlib.False; |
| 283 | |
| 284 | _ = xlib.c.XSendEvent(display, client.window, xlib.False, xlib.StructureNotifyMask, @ptrCast(&configure_event)); |
| 285 | } |