| 1 | const std = @import("std"); |
| 2 | const xlib = @import("../x11/xlib.zig"); |
| 3 | const client_mod = @import("../client.zig"); |
| 4 | const monitor_mod = @import("../monitor.zig"); |
| 5 | const tiling = @import("../layouts/tiling.zig"); |
| 6 | const scrolling = @import("../layouts/scrolling.zig"); |
| 7 | const window_manager = @import("wm.zig"); |
| 8 | |
| 9 | const config_mod = @import("../config/config.zig"); |
| 10 | |
| 11 | const Client = client_mod.Client; |
| 12 | const Monitor = monitor_mod.Monitor; |
| 13 | const WindowManager = window_manager.WindowManager; |
| 14 | |
| 15 | pub const NormalState = window_manager.NormalState; |
| 16 | |
| 17 | pub fn manage(win: xlib.Window, window_attrs: *xlib.XWindowAttributes, wm: *WindowManager) void { |
| 18 | const client = client_mod.create(wm.allocator, win) orelse return; |
| 19 | var trans: xlib.Window = 0; |
| 20 | |
| 21 | client.x = window_attrs.x; |
| 22 | client.y = window_attrs.y; |
| 23 | client.width = window_attrs.width; |
| 24 | client.height = window_attrs.height; |
| 25 | client.old_x = window_attrs.x; |
| 26 | client.old_y = window_attrs.y; |
| 27 | client.old_width = window_attrs.width; |
| 28 | client.old_height = window_attrs.height; |
| 29 | client.old_border_width = window_attrs.border_width; |
| 30 | client.border_width = wm.config.border_width; |
| 31 | |
| 32 | updateTitle(client, wm); |
| 33 | |
| 34 | if (xlib.XGetTransientForHint(wm.display.handle, win, &trans) != 0) { |
| 35 | if (client_mod.windowToClient(wm.monitors, trans)) |transient_client| { |
| 36 | client.monitor = transient_client.monitor; |
| 37 | client.tags = transient_client.tags; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | if (client.monitor == null) { |
| 42 | client.monitor = wm.selected_monitor; |
| 43 | if (wm.next_spawn_bypass_rules) { |
| 44 | if (client.monitor) |monitor| { |
| 45 | client.tags = monitor.tagset[monitor.sel_tags]; |
| 46 | } |
| 47 | wm.next_spawn_bypass_rules = false; |
| 48 | } else { |
| 49 | applyRules(client, wm); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (wm.next_spawn_floating) { |
| 54 | client.is_floating = true; |
| 55 | wm.next_spawn_floating = false; |
| 56 | } |
| 57 | |
| 58 | const monitor = client.monitor orelse return; |
| 59 | |
| 60 | if (client.x + client.width > monitor.win_x + monitor.win_w) { |
| 61 | client.x = monitor.win_x + monitor.win_w - client.width - 2 * client.border_width; |
| 62 | } |
| 63 | if (client.y + client.height > monitor.win_y + monitor.win_h) { |
| 64 | client.y = monitor.win_y + monitor.win_h - client.height - 2 * client.border_width; |
| 65 | } |
| 66 | client.x = @max(client.x, monitor.win_x); |
| 67 | client.y = @max(client.y, monitor.win_y); |
| 68 | |
| 69 | _ = xlib.XSetWindowBorderWidth(wm.display.handle, win, @intCast(client.border_width)); |
| 70 | _ = xlib.XSetWindowBorder(wm.display.handle, win, wm.config.border_unfocused); |
| 71 | tiling.sendConfigure(client); |
| 72 | |
| 73 | updateWindowType(client, wm); |
| 74 | updateSizeHints(client, wm); |
| 75 | updateWmHints(client, wm); |
| 76 | |
| 77 | _ = xlib.XSelectInput( |
| 78 | wm.display.handle, |
| 79 | win, |
| 80 | xlib.EnterWindowMask | xlib.FocusChangeMask | xlib.PropertyChangeMask | xlib.StructureNotifyMask, |
| 81 | ); |
| 82 | grabbuttons(client, false, wm); |
| 83 | |
| 84 | if (!client.is_floating) { |
| 85 | client.is_floating = trans != 0 or client.is_fixed; |
| 86 | client.old_state = client.is_floating; |
| 87 | } |
| 88 | if (client.is_floating) { |
| 89 | positionFloating(client, monitor, wm.config.floating_position); |
| 90 | _ = xlib.XRaiseWindow(wm.display.handle, client.window); |
| 91 | } |
| 92 | |
| 93 | client_mod.attachWith(client, wm.config.attach_method); |
| 94 | client_mod.attachStack(client); |
| 95 | |
| 96 | _ = xlib.XChangeProperty(wm.display.handle, wm.display.root, wm.atoms.net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1); |
| 97 | _ = xlib.XMoveResizeWindow(wm.display.handle, client.window, client.x + 2 * wm.display.screenWidth(), client.y, @intCast(client.width), @intCast(client.height)); |
| 98 | setClientState(client, NormalState, wm); |
| 99 | |
| 100 | if (client.monitor == wm.selected_monitor) { |
| 101 | const selmon = wm.selected_monitor orelse return; |
| 102 | unfocusClient(selmon.sel, false, wm); |
| 103 | } |
| 104 | monitor.sel = client; |
| 105 | |
| 106 | if (isScrollingLayout(monitor)) { |
| 107 | monitor.scroll_offset = 0; |
| 108 | } |
| 109 | |
| 110 | arrange(monitor, wm); |
| 111 | _ = xlib.XMapWindow(wm.display.handle, win); |
| 112 | focus(null, wm); |
| 113 | } |
| 114 | |
| 115 | pub fn unmanage(client: *Client, wm: *WindowManager) void { |
| 116 | const client_monitor = client.monitor; |
| 117 | |
| 118 | var next_focus: ?*Client = null; |
| 119 | if (client_monitor) |monitor| { |
| 120 | if (monitor.sel == client and isScrollingLayout(monitor)) { |
| 121 | next_focus = client.next; |
| 122 | if (next_focus == null) { |
| 123 | var prev: ?*Client = null; |
| 124 | var iter = monitor.clients; |
| 125 | while (iter) |c| { |
| 126 | if (c == client) break; |
| 127 | prev = c; |
| 128 | iter = c.next; |
| 129 | } |
| 130 | next_focus = prev; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | client_mod.detach(client); |
| 136 | client_mod.detachStack(client); |
| 137 | |
| 138 | if (client_monitor) |monitor| { |
| 139 | if (monitor.sel == client) { |
| 140 | monitor.sel = if (next_focus) |nf| nf else monitor.stack; |
| 141 | } |
| 142 | if (isScrollingLayout(monitor)) { |
| 143 | const target = if (monitor.sel) |sel| scrolling.getTargetScrollForWindow(monitor, sel) else 0; |
| 144 | if (target == 0) { |
| 145 | monitor.scroll_offset = scrolling.getScrollStep(monitor); |
| 146 | } else { |
| 147 | monitor.scroll_offset = 0; |
| 148 | } |
| 149 | } |
| 150 | arrange(monitor, wm); |
| 151 | } |
| 152 | |
| 153 | if (client_monitor) |monitor| { |
| 154 | if (monitor.sel) |selected| { |
| 155 | focus(selected, wm); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | client_mod.destroy(wm.allocator, client); |
| 160 | updateClientList(wm); |
| 161 | wm.invalidateBars(); |
| 162 | } |
| 163 | |
| 164 | pub fn focus(target_client: ?*Client, wm: *WindowManager) void { |
| 165 | const selmon = wm.selected_monitor orelse return; |
| 166 | |
| 167 | var focus_client = target_client; |
| 168 | if (focus_client == null or !client_mod.isVisible(focus_client.?)) { |
| 169 | focus_client = selmon.stack; |
| 170 | while (focus_client) |iter| { |
| 171 | if (client_mod.isVisible(iter)) break; |
| 172 | focus_client = iter.stack_next; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (selmon.sel != null and selmon.sel != focus_client) { |
| 177 | unfocusClient(selmon.sel, false, wm); |
| 178 | } |
| 179 | |
| 180 | if (focus_client) |client| { |
| 181 | if (client.monitor != selmon) { |
| 182 | wm.selected_monitor = client.monitor; |
| 183 | } |
| 184 | if (client.is_urgent) { |
| 185 | setUrgent(client, false, wm); |
| 186 | } |
| 187 | client_mod.detachStack(client); |
| 188 | client_mod.attachStack(client); |
| 189 | grabbuttons(client, true, wm); |
| 190 | _ = xlib.XSetWindowBorder(wm.display.handle, client.window, wm.config.border_focused); |
| 191 | if (!client.never_focus) { |
| 192 | _ = xlib.XSetInputFocus(wm.display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime); |
| 193 | _ = xlib.XChangeProperty(wm.display.handle, wm.display.root, wm.atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1); |
| 194 | } |
| 195 | _ = sendEvent(client, wm.atoms.wm_take_focus, wm); |
| 196 | } else { |
| 197 | _ = xlib.XSetInputFocus(wm.display.handle, wm.display.root, xlib.RevertToPointerRoot, xlib.CurrentTime); |
| 198 | _ = xlib.XDeleteProperty(wm.display.handle, wm.display.root, wm.atoms.net_active_window); |
| 199 | } |
| 200 | |
| 201 | const current_selmon = wm.selected_monitor orelse return; |
| 202 | current_selmon.sel = focus_client; |
| 203 | |
| 204 | if (focus_client) |client| { |
| 205 | if (isScrollingLayout(current_selmon)) { |
| 206 | scrollToWindow(client, true, wm); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | wm.invalidateBars(); |
| 211 | } |
| 212 | |
| 213 | pub fn unfocusClient(client: ?*Client, reset_input_focus: bool, wm: *WindowManager) void { |
| 214 | const unfocus_target = client orelse return; |
| 215 | grabbuttons(unfocus_target, false, wm); |
| 216 | _ = xlib.XSetWindowBorder(wm.display.handle, unfocus_target.window, wm.config.border_unfocused); |
| 217 | if (reset_input_focus) { |
| 218 | _ = xlib.XSetInputFocus(wm.display.handle, wm.display.root, xlib.RevertToPointerRoot, xlib.CurrentTime); |
| 219 | _ = xlib.XDeleteProperty(wm.display.handle, wm.display.root, wm.atoms.net_active_window); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | pub fn setFocus(client: *Client, wm: *WindowManager) void { |
| 224 | if (!client.never_focus) { |
| 225 | _ = xlib.XSetInputFocus(wm.display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime); |
| 226 | _ = xlib.XChangeProperty(wm.display.handle, wm.display.root, wm.atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1); |
| 227 | } |
| 228 | _ = sendEvent(client, wm.atoms.wm_take_focus, wm); |
| 229 | } |
| 230 | |
| 231 | pub fn restack(monitor: *Monitor, wm: *WindowManager) void { |
| 232 | wm.invalidateBars(); |
| 233 | const selected_client = monitor.sel orelse return; |
| 234 | |
| 235 | if (selected_client.is_floating or monitor.lt[monitor.sel_lt] == null) { |
| 236 | _ = xlib.XRaiseWindow(wm.display.handle, selected_client.window); |
| 237 | } |
| 238 | |
| 239 | if (monitor.lt[monitor.sel_lt] != null) { |
| 240 | var window_changes: xlib.c.XWindowChanges = undefined; |
| 241 | window_changes.stack_mode = xlib.c.Below; |
| 242 | window_changes.sibling = monitor.bar_win; |
| 243 | |
| 244 | var current = monitor.stack; |
| 245 | while (current) |client| { |
| 246 | if (!client.is_floating and client_mod.isVisible(client)) { |
| 247 | _ = xlib.c.XConfigureWindow(wm.display.handle, client.window, xlib.c.CWSibling | xlib.c.CWStackMode, &window_changes); |
| 248 | window_changes.sibling = client.window; |
| 249 | } |
| 250 | current = client.stack_next; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | _ = xlib.XSync(wm.display.handle, xlib.False); |
| 255 | |
| 256 | var discard_event: xlib.XEvent = undefined; |
| 257 | while (xlib.c.XCheckMaskEvent(wm.display.handle, xlib.EnterWindowMask, &discard_event) != 0) {} |
| 258 | } |
| 259 | |
| 260 | pub fn arrange(monitor: *Monitor, wm: *WindowManager) void { |
| 261 | showhide(monitor, wm); |
| 262 | if (monitor.lt[monitor.sel_lt]) |layout| { |
| 263 | if (layout.arrange_fn) |arrange_fn| { |
| 264 | arrange_fn(monitor); |
| 265 | } |
| 266 | } |
| 267 | restack(monitor, wm); |
| 268 | } |
| 269 | |
| 270 | pub fn showhide(monitor: *Monitor, wm: *WindowManager) void { |
| 271 | showhideClient(monitor.stack, wm); |
| 272 | } |
| 273 | |
| 274 | pub fn showhideClient(client: ?*Client, wm: *WindowManager) void { |
| 275 | const target = client orelse return; |
| 276 | if (client_mod.isVisible(target)) { |
| 277 | _ = xlib.XMoveWindow(wm.display.handle, target.window, target.x, target.y); |
| 278 | const monitor = target.monitor orelse return; |
| 279 | if ((monitor.lt[monitor.sel_lt] == null or target.is_floating) and !target.is_fullscreen) { |
| 280 | tiling.resize(target, target.x, target.y, target.width, target.height, false); |
| 281 | } |
| 282 | showhideClient(target.stack_next, wm); |
| 283 | } else { |
| 284 | showhideClient(target.stack_next, wm); |
| 285 | const client_width = target.width + 2 * target.border_width; |
| 286 | _ = xlib.XMoveWindow(wm.display.handle, target.window, -2 * client_width, target.y); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | pub fn grabbuttons(client: *Client, focused: bool, wm: *WindowManager) void { |
| 291 | wm.updateNumlockMask(); |
| 292 | const modifiers = [_]c_uint{ 0, xlib.LockMask, wm.numlock_mask, wm.numlock_mask | xlib.LockMask }; |
| 293 | |
| 294 | _ = xlib.XUngrabButton(wm.display.handle, xlib.AnyButton, xlib.AnyModifier, client.window); |
| 295 | if (!focused) { |
| 296 | _ = xlib.XGrabButton( |
| 297 | wm.display.handle, |
| 298 | xlib.AnyButton, |
| 299 | xlib.AnyModifier, |
| 300 | client.window, |
| 301 | xlib.False, |
| 302 | xlib.ButtonPressMask | xlib.ButtonReleaseMask, |
| 303 | xlib.GrabModeSync, |
| 304 | xlib.GrabModeSync, |
| 305 | xlib.None, |
| 306 | xlib.None, |
| 307 | ); |
| 308 | } |
| 309 | for (wm.config.buttons.items) |button| { |
| 310 | if (button.click == .client_win) { |
| 311 | for (modifiers) |modifier| { |
| 312 | _ = xlib.XGrabButton( |
| 313 | wm.display.handle, |
| 314 | @intCast(button.button), |
| 315 | button.mod_mask | modifier, |
| 316 | client.window, |
| 317 | xlib.False, |
| 318 | xlib.ButtonPressMask | xlib.ButtonReleaseMask, |
| 319 | xlib.GrabModeAsync, |
| 320 | xlib.GrabModeSync, |
| 321 | xlib.None, |
| 322 | xlib.None, |
| 323 | ); |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | pub fn setClientState(client: *Client, state: c_long, wm: *WindowManager) void { |
| 330 | var data: [2]c_long = .{ state, xlib.None }; |
| 331 | _ = xlib.c.XChangeProperty(wm.display.handle, client.window, wm.atoms.wm_state, xlib.XA_ATOM, 32, xlib.PropModeReplace, @ptrCast(&data), 2); |
| 332 | } |
| 333 | |
| 334 | pub fn setFullscreen(client: *Client, fullscreen: bool, wm: *WindowManager) void { |
| 335 | const monitor = client.monitor orelse return; |
| 336 | |
| 337 | if (fullscreen and !client.is_fullscreen) { |
| 338 | var fullscreen_atom = wm.atoms.net_wm_state_fullscreen; |
| 339 | _ = xlib.XChangeProperty( |
| 340 | wm.display.handle, |
| 341 | client.window, |
| 342 | wm.atoms.net_wm_state, |
| 343 | xlib.XA_ATOM, |
| 344 | 32, |
| 345 | xlib.PropModeReplace, |
| 346 | @ptrCast(&fullscreen_atom), |
| 347 | 1, |
| 348 | ); |
| 349 | client.is_fullscreen = true; |
| 350 | client.old_state = client.is_floating; |
| 351 | client.old_border_width = client.border_width; |
| 352 | client.border_width = 0; |
| 353 | client.is_floating = true; |
| 354 | |
| 355 | _ = xlib.XSetWindowBorderWidth(wm.display.handle, client.window, 0); |
| 356 | tiling.resizeClient(client, monitor.mon_x, monitor.mon_y, monitor.mon_w, monitor.mon_h); |
| 357 | _ = xlib.XRaiseWindow(wm.display.handle, client.window); |
| 358 | |
| 359 | std.debug.print("fullscreen enabled: window=0x{x}\n", .{client.window}); |
| 360 | } else if (!fullscreen and client.is_fullscreen) { |
| 361 | var no_atom: xlib.Atom = 0; |
| 362 | _ = xlib.XChangeProperty( |
| 363 | wm.display.handle, |
| 364 | client.window, |
| 365 | wm.atoms.net_wm_state, |
| 366 | xlib.XA_ATOM, |
| 367 | 32, |
| 368 | xlib.PropModeReplace, |
| 369 | @ptrCast(&no_atom), |
| 370 | 0, |
| 371 | ); |
| 372 | client.is_fullscreen = false; |
| 373 | client.is_floating = client.old_state; |
| 374 | client.border_width = client.old_border_width; |
| 375 | |
| 376 | client.x = client.old_x; |
| 377 | client.y = client.old_y; |
| 378 | client.width = client.old_width; |
| 379 | client.height = client.old_height; |
| 380 | |
| 381 | tiling.resizeClient(client, client.x, client.y, client.width, client.height); |
| 382 | arrange(monitor, wm); |
| 383 | |
| 384 | std.debug.print("fullscreen disabled: window=0x{x}\n", .{client.window}); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | pub fn focusTopClient(monitor: *Monitor, wm: *WindowManager) void { |
| 389 | var visible_client = monitor.stack; |
| 390 | while (visible_client) |client| { |
| 391 | if (client_mod.isVisible(client)) { |
| 392 | focus(client, wm); |
| 393 | return; |
| 394 | } |
| 395 | visible_client = client.stack_next; |
| 396 | } |
| 397 | monitor.sel = null; |
| 398 | _ = xlib.XSetInputFocus(wm.display.handle, wm.display.root, xlib.RevertToPointerRoot, xlib.CurrentTime); |
| 399 | } |
| 400 | |
| 401 | pub fn tickAnimations(wm: *WindowManager) void { |
| 402 | if (!wm.scroll_animation.isActive()) return; |
| 403 | |
| 404 | const monitor = wm.selected_monitor orelse return; |
| 405 | if (wm.scroll_animation.update(wm.io)) |new_offset| { |
| 406 | monitor.scroll_offset = new_offset; |
| 407 | arrange(monitor, wm); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | pub fn isScrollingLayout(monitor: *Monitor) bool { |
| 412 | if (monitor.lt[monitor.sel_lt]) |layout| { |
| 413 | return layout.arrange_fn == scrolling.layout.arrange_fn; |
| 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | |
| 418 | pub fn scrollLayout(direction: i32, wm: *WindowManager) void { |
| 419 | const monitor = wm.selected_monitor orelse return; |
| 420 | if (!isScrollingLayout(monitor)) return; |
| 421 | |
| 422 | const scroll_step = scrolling.getScrollStep(monitor); |
| 423 | const max_scroll = scrolling.getMaxScroll(monitor); |
| 424 | |
| 425 | const current = if (wm.scroll_animation.isActive()) |
| 426 | wm.scroll_animation.target() |
| 427 | else |
| 428 | monitor.scroll_offset; |
| 429 | |
| 430 | var target = current + direction * scroll_step; |
| 431 | target = @max(0, @min(target, max_scroll)); |
| 432 | |
| 433 | wm.scroll_animation.start(wm.io, monitor.scroll_offset, target, wm.animation_config); |
| 434 | } |
| 435 | |
| 436 | pub fn scrollToWindow(client: *Client, animate: bool, wm: *WindowManager) void { |
| 437 | const monitor = client.monitor orelse return; |
| 438 | if (!isScrollingLayout(monitor)) return; |
| 439 | |
| 440 | const target = scrolling.getTargetScrollForWindow(monitor, client); |
| 441 | |
| 442 | if (animate) { |
| 443 | wm.scroll_animation.start(wm.io, monitor.scroll_offset, target, wm.animation_config); |
| 444 | } else { |
| 445 | monitor.scroll_offset = target; |
| 446 | arrange(monitor, wm); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | fn positionFloating(client: *Client, monitor: *Monitor, pos: config_mod.FloatingPosition) void { |
| 451 | const bw = 2 * client.border_width; |
| 452 | const x: i32 = switch (pos) { |
| 453 | .top_left, .center_left, .bottom_left => monitor.win_x, |
| 454 | .top_center, .center, .bottom_center => monitor.win_x + @divTrunc(monitor.win_w - client.width - bw, 2), |
| 455 | .top_right, .center_right, .bottom_right => monitor.win_x + monitor.win_w - client.width - bw, |
| 456 | }; |
| 457 | const y: i32 = switch (pos) { |
| 458 | .top_left, .top_center, .top_right => monitor.win_y, |
| 459 | .center_left, .center, .center_right => monitor.win_y + @divTrunc(monitor.win_h - client.height - bw, 2), |
| 460 | .bottom_left, .bottom_center, .bottom_right => monitor.win_y + monitor.win_h - client.height - bw, |
| 461 | }; |
| 462 | client.x = x; |
| 463 | client.y = y; |
| 464 | } |
| 465 | |
| 466 | pub fn applyRules(client: *Client, wm: *WindowManager) void { |
| 467 | var class_hint: xlib.XClassHint = .{ .res_name = null, .res_class = null }; |
| 468 | _ = xlib.XGetClassHint(wm.display.handle, client.window, &class_hint); |
| 469 | |
| 470 | const class_str: []const u8 = if (class_hint.res_class) |ptr| std.mem.sliceTo(ptr, 0) else ""; |
| 471 | const instance_str: []const u8 = if (class_hint.res_name) |ptr| std.mem.sliceTo(ptr, 0) else ""; |
| 472 | |
| 473 | client.is_floating = false; |
| 474 | client.tags = 0; |
| 475 | var rule_focus = false; |
| 476 | |
| 477 | for (wm.config.rules.items) |rule| { |
| 478 | const class_matches = if (rule.class) |rc| std.mem.indexOf(u8, class_str, rc) != null else true; |
| 479 | const instance_matches = if (rule.instance) |ri| std.mem.indexOf(u8, instance_str, ri) != null else true; |
| 480 | const title_matches = if (rule.title) |rt| std.mem.indexOf(u8, std.mem.sliceTo(&client.name, 0), rt) != null else true; |
| 481 | |
| 482 | if (class_matches and instance_matches and title_matches) { |
| 483 | client.is_floating = rule.is_floating; |
| 484 | client.tags |= rule.tags; |
| 485 | if (rule.monitor >= 0) { |
| 486 | var target = wm.monitors; |
| 487 | var index: i32 = 0; |
| 488 | while (target) |mon| { |
| 489 | if (index == rule.monitor) { |
| 490 | client.monitor = mon; |
| 491 | break; |
| 492 | } |
| 493 | index += 1; |
| 494 | target = mon.next; |
| 495 | } |
| 496 | } |
| 497 | if (rule.focus) { |
| 498 | rule_focus = true; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if (class_hint.res_class) |ptr| { |
| 504 | _ = xlib.XFree(@ptrCast(ptr)); |
| 505 | } |
| 506 | if (class_hint.res_name) |ptr| { |
| 507 | _ = xlib.XFree(@ptrCast(ptr)); |
| 508 | } |
| 509 | |
| 510 | const monitor = client.monitor orelse return; |
| 511 | if (client.tags == 0) { |
| 512 | client.tags = monitor.tagset[monitor.sel_tags]; |
| 513 | } |
| 514 | |
| 515 | if (rule_focus and client.tags != 0) { |
| 516 | const monitor_tagset = monitor.tagset[monitor.sel_tags]; |
| 517 | const is_tag_focused = (monitor_tagset & client.tags) == client.tags; |
| 518 | if (!is_tag_focused) { |
| 519 | view(client.tags, wm); |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | pub fn updateSizeHints(client: *Client, wm: *WindowManager) void { |
| 525 | var size_hints: xlib.XSizeHints = undefined; |
| 526 | var msize: c_long = 0; |
| 527 | |
| 528 | if (xlib.XGetWMNormalHints(wm.display.handle, client.window, &size_hints, &msize) == 0) { |
| 529 | size_hints.flags = xlib.PSize; |
| 530 | } |
| 531 | |
| 532 | if ((size_hints.flags & xlib.PBaseSize) != 0) { |
| 533 | client.base_width = size_hints.base_width; |
| 534 | client.base_height = size_hints.base_height; |
| 535 | } else if ((size_hints.flags & xlib.PMinSize) != 0) { |
| 536 | client.base_width = size_hints.min_width; |
| 537 | client.base_height = size_hints.min_height; |
| 538 | } else { |
| 539 | client.base_width = 0; |
| 540 | client.base_height = 0; |
| 541 | } |
| 542 | |
| 543 | if ((size_hints.flags & xlib.PResizeInc) != 0) { |
| 544 | client.increment_width = size_hints.width_inc; |
| 545 | client.increment_height = size_hints.height_inc; |
| 546 | } else { |
| 547 | client.increment_width = 0; |
| 548 | client.increment_height = 0; |
| 549 | } |
| 550 | |
| 551 | if ((size_hints.flags & xlib.PMaxSize) != 0) { |
| 552 | client.max_width = size_hints.max_width; |
| 553 | client.max_height = size_hints.max_height; |
| 554 | } else { |
| 555 | client.max_width = 0; |
| 556 | client.max_height = 0; |
| 557 | } |
| 558 | |
| 559 | if ((size_hints.flags & xlib.PMinSize) != 0) { |
| 560 | client.min_width = size_hints.min_width; |
| 561 | client.min_height = size_hints.min_height; |
| 562 | } else if ((size_hints.flags & xlib.PBaseSize) != 0) { |
| 563 | client.min_width = size_hints.base_width; |
| 564 | client.min_height = size_hints.base_height; |
| 565 | } else { |
| 566 | client.min_width = 0; |
| 567 | client.min_height = 0; |
| 568 | } |
| 569 | |
| 570 | if ((size_hints.flags & xlib.PAspect) != 0) { |
| 571 | client.min_aspect = @as(f32, @floatFromInt(size_hints.min_aspect.y)) / @as(f32, @floatFromInt(size_hints.min_aspect.x)); |
| 572 | client.max_aspect = @as(f32, @floatFromInt(size_hints.max_aspect.x)) / @as(f32, @floatFromInt(size_hints.max_aspect.y)); |
| 573 | } else { |
| 574 | client.min_aspect = 0.0; |
| 575 | client.max_aspect = 0.0; |
| 576 | } |
| 577 | |
| 578 | client.is_fixed = (client.max_width != 0 and client.max_height != 0 and client.max_width == client.min_width and client.max_height == client.min_height); |
| 579 | client.hints_valid = true; |
| 580 | } |
| 581 | |
| 582 | pub fn updateWmHints(client: *Client, wm: *WindowManager) void { |
| 583 | const wmh = xlib.XGetWMHints(wm.display.handle, client.window); |
| 584 | if (wmh) |hints| { |
| 585 | defer _ = xlib.XFree(@ptrCast(hints)); |
| 586 | |
| 587 | if (client == (wm.selected_monitor orelse return).sel and (hints.*.flags & xlib.XUrgencyHint) != 0) { |
| 588 | hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint); |
| 589 | _ = xlib.XSetWMHints(wm.display.handle, client.window, hints); |
| 590 | } else { |
| 591 | client.is_urgent = (hints.*.flags & xlib.XUrgencyHint) != 0; |
| 592 | } |
| 593 | |
| 594 | if ((hints.*.flags & xlib.InputHint) != 0) { |
| 595 | client.never_focus = hints.*.input == 0; |
| 596 | } else { |
| 597 | client.never_focus = false; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | pub fn updateWindowType(client: *Client, wm: *WindowManager) void { |
| 603 | const state = getAtomProp(client, wm.atoms.net_wm_state, wm); |
| 604 | const window_type = getAtomProp(client, wm.atoms.net_wm_window_type, wm); |
| 605 | |
| 606 | if (state == wm.atoms.net_wm_state_fullscreen) { |
| 607 | setFullscreen(client, true, wm); |
| 608 | } |
| 609 | if (window_type == wm.atoms.net_wm_window_type_dialog) { |
| 610 | client.is_floating = true; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | pub fn updateTitle(client: *Client, wm: *WindowManager) void { |
| 615 | if (!getTextProp(client.window, wm.atoms.net_wm_name, &client.name, wm)) { |
| 616 | _ = getTextProp(client.window, xlib.XA_WM_NAME, &client.name, wm); |
| 617 | } |
| 618 | if (client.name[0] == 0) { |
| 619 | @memcpy(client.name[0..6], "broken"); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | pub fn getAtomProp(client: *Client, prop: xlib.Atom, wm: *WindowManager) xlib.Atom { |
| 624 | var actual_type: xlib.Atom = undefined; |
| 625 | var actual_format: c_int = undefined; |
| 626 | var num_items: c_ulong = undefined; |
| 627 | var bytes_after: c_ulong = undefined; |
| 628 | var prop_data: [*c]u8 = undefined; |
| 629 | |
| 630 | if (xlib.XGetWindowProperty(wm.display.handle, client.window, prop, 0, @sizeOf(xlib.Atom), xlib.False, xlib.XA_ATOM, &actual_type, &actual_format, &num_items, &bytes_after, &prop_data) == 0 and prop_data != null) { |
| 631 | const atom: xlib.Atom = @as(*xlib.Atom, @ptrCast(@alignCast(prop_data))).*; |
| 632 | _ = xlib.XFree(@ptrCast(prop_data)); |
| 633 | return atom; |
| 634 | } |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | pub fn getTextProp(window: xlib.Window, atom: xlib.Atom, text: *[256]u8, wm: *WindowManager) bool { |
| 639 | var name: xlib.XTextProperty = undefined; |
| 640 | text[0] = 0; |
| 641 | |
| 642 | if (xlib.XGetTextProperty(wm.display.handle, window, &name, atom) == 0 or name.nitems == 0) { |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | if (name.encoding == xlib.XA_STRING) { |
| 647 | const len = @min(name.nitems, 255); |
| 648 | @memcpy(text[0..len], name.value[0..len]); |
| 649 | text[len] = 0; |
| 650 | } else { |
| 651 | var list: [*c][*c]u8 = undefined; |
| 652 | var count: c_int = undefined; |
| 653 | if (xlib.XmbTextPropertyToTextList(wm.display.handle, &name, &list, &count) >= xlib.Success and count > 0 and list[0] != null) { |
| 654 | const str = std.mem.sliceTo(list[0], 0); |
| 655 | const copy_len = @min(str.len, 255); |
| 656 | @memcpy(text[0..copy_len], str[0..copy_len]); |
| 657 | text[copy_len] = 0; |
| 658 | xlib.XFreeStringList(list); |
| 659 | } |
| 660 | } |
| 661 | text[255] = 0; |
| 662 | _ = xlib.XFree(@ptrCast(name.value)); |
| 663 | return true; |
| 664 | } |
| 665 | |
| 666 | pub fn updateClientList(wm: *WindowManager) void { |
| 667 | _ = xlib.XDeleteProperty(wm.display.handle, wm.display.root, wm.atoms.net_client_list); |
| 668 | |
| 669 | var current_monitor = wm.monitors; |
| 670 | while (current_monitor) |monitor| { |
| 671 | var current_client = monitor.clients; |
| 672 | while (current_client) |client| { |
| 673 | _ = xlib.XChangeProperty(wm.display.handle, wm.display.root, wm.atoms.net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1); |
| 674 | current_client = client.next; |
| 675 | } |
| 676 | current_monitor = monitor.next; |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | pub fn sendEvent(client: *Client, protocol: xlib.Atom, wm: *WindowManager) bool { |
| 681 | var protocols: [*c]xlib.Atom = undefined; |
| 682 | var num_protocols: c_int = 0; |
| 683 | var exists = false; |
| 684 | |
| 685 | if (xlib.XGetWMProtocols(wm.display.handle, client.window, &protocols, &num_protocols) != 0) { |
| 686 | var index: usize = 0; |
| 687 | while (index < @as(usize, @intCast(num_protocols))) : (index += 1) { |
| 688 | if (protocols[index] == protocol) { |
| 689 | exists = true; |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | _ = xlib.XFree(@ptrCast(protocols)); |
| 694 | } |
| 695 | |
| 696 | if (exists) { |
| 697 | var event: xlib.XEvent = undefined; |
| 698 | event.type = xlib.ClientMessage; |
| 699 | event.xclient.window = client.window; |
| 700 | event.xclient.message_type = wm.atoms.wm_protocols; |
| 701 | event.xclient.format = 32; |
| 702 | event.xclient.data.l[0] = @intCast(protocol); |
| 703 | event.xclient.data.l[1] = xlib.CurrentTime; |
| 704 | _ = xlib.XSendEvent(wm.display.handle, client.window, xlib.False, xlib.NoEventMask, &event); |
| 705 | } |
| 706 | return exists; |
| 707 | } |
| 708 | |
| 709 | pub fn setUrgent(client: *Client, urgent: bool, wm: *WindowManager) void { |
| 710 | client.is_urgent = urgent; |
| 711 | const wmh = xlib.XGetWMHints(wm.display.handle, client.window); |
| 712 | if (wmh) |hints| { |
| 713 | if (urgent) { |
| 714 | hints.*.flags = hints.*.flags | xlib.XUrgencyHint; |
| 715 | } else { |
| 716 | hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint); |
| 717 | } |
| 718 | _ = xlib.XSetWMHints(wm.display.handle, client.window, hints); |
| 719 | _ = xlib.XFree(@ptrCast(hints)); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | pub fn hasClientsOnTag(monitor: *monitor_mod.Monitor, tag_mask: u32) bool { |
| 724 | var client = monitor.clients; |
| 725 | while (client) |c| { |
| 726 | if ((c.tags & tag_mask) != 0) { |
| 727 | return true; |
| 728 | } |
| 729 | client = c.next; |
| 730 | } |
| 731 | return false; |
| 732 | } |
| 733 | |
| 734 | pub fn view(tag_mask: u32, wm: *WindowManager) void { |
| 735 | const monitor = wm.selected_monitor orelse return; |
| 736 | if (tag_mask == monitor.tagset[monitor.sel_tags]) { |
| 737 | return; |
| 738 | } |
| 739 | monitor.sel_tags ^= 1; |
| 740 | if (tag_mask != 0) { |
| 741 | monitor.tagset[monitor.sel_tags] = tag_mask; |
| 742 | monitor.pertag.prevtag = monitor.pertag.curtag; |
| 743 | |
| 744 | if (tag_mask == ~@as(u32, 0)) { |
| 745 | monitor.pertag.curtag = 0; |
| 746 | } else { |
| 747 | var i: u32 = 0; |
| 748 | while (i < wm.config.tag_count) : (i += 1) { |
| 749 | if ((tag_mask & (@as(u32, 1) << @intCast(i))) != 0) break; |
| 750 | } |
| 751 | monitor.pertag.curtag = i + 1; |
| 752 | } |
| 753 | } else { |
| 754 | const tmp = monitor.pertag.prevtag; |
| 755 | monitor.pertag.prevtag = monitor.pertag.curtag; |
| 756 | monitor.pertag.curtag = tmp; |
| 757 | } |
| 758 | |
| 759 | monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag]; |
| 760 | monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag]; |
| 761 | monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag]; |
| 762 | |
| 763 | const new_show_bar = monitor.pertag.showbars[monitor.pertag.curtag]; |
| 764 | if (new_show_bar != monitor.show_bar) { |
| 765 | monitor.show_bar = new_show_bar; |
| 766 | window_manager.actions.updateBarVisibility(monitor, wm); |
| 767 | } |
| 768 | |
| 769 | focusTopClient(monitor, wm); |
| 770 | arrange(monitor, wm); |
| 771 | wm.invalidateBars(); |
| 772 | std.debug.print("view: tag_mask={d}\n", .{monitor.tagset[monitor.sel_tags]}); |
| 773 | } |