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