tony / oxwm

refactor: remove more globals and replace with `WindowManager`3 instance in main Browse files
emzywastaken committed 2026-02-21 parent 82fd9ea commit 2cbdc6778accb1329883ac3106f0df0579b441ed
Showing 1 changed file with 221 additions and 254 deletions. UnifiedSplit
+221 −254 src/main.zig Large diff collapsed, click to expand
@@ -11,7 +11,6 @@ const scrolling = @import("layouts/scrolling.zig");
11 11 const bar_mod = @import("bar/bar.zig");
12 12 const config_mod = @import("config/config.zig");
13 13 const lua = @import("config/lua.zig");
14 -const overlay_mod = @import("overlay.zig");
15 14
16 15 const Display = display_mod.Display;
17 16 const Client = client_mod.Client;
@@ -26,36 +25,13 @@ const IconicState: c_long = 3;
26 25 const IsViewable: c_int = 2;
27 26 const snap_distance: i32 = 32;
28 27
29 -// Remaining module-level state
30 -
31 -/// Config built from Lua, passed into WindowManager.init.
28 +/// Config built from Lua, passed into `WindowManager.init`.
32 29 var config: config_mod.Config = undefined;
33 -/// Path to the loaded config file, kept for hot-reload.
30 +/// Path to the loaded config file.
34 31 var config_path_global: ?[]const u8 = null;
35 32
36 -/// TODO(wm-refactor): move into WindowManager, waiting on handle_key_press
37 -/// and execute_action being converted to methods.
38 -var chord = @import("keyboard/chord.zig").ChordState{};
39 -
40 -/// TODO(wm-refactor): remove once all callers are WindowManager methods.
41 -/// Aliases wm.atoms so existing functions that take display but not wm
42 -/// can still reach atom values without requiring a full signature change.
43 -var atoms: @import("x11/atoms.zig").Atoms = undefined;
44 -
45 -/// TODO(wm-refactor): move into WindowManager, waiting on arrange() becoming
46 -/// a method so it can access self.display directly.
47 -var wm_display: ?*Display = null;
48 -
49 -/// TODO(wm-refactor): remove once all callers are WindowManager methods.
50 -/// Used only by functions that need to invalidate bars but don't yet receive
51 -/// wm as a parameter.
52 -var wm_ptr: ?*WindowManager = null;
53 -
54 -/// TODO(wm-refactor): move into WindowManager.
55 -var numlock_mask: c_uint = 0;
56 -
57 33 /// File descriptor of the X11 connection, used in spawn_child_setup.
58 -/// Set from wm.x11_fd after init.
34 +/// Set from `wm.x11_fd` after init.
59 35 var x11_fd: c_int = -1;
60 36
61 37 fn print_help() void {
@@ -223,16 +199,13 @@ pub fn main() !void {
223 199 // Propagate values that transitional code in this file still reads
224 200 // from module-level vars. These will be removed soon.
225 201 x11_fd = wm.x11_fd;
226 - wm_display = &wm.display;
227 - wm_ptr = &wm;
228 - atoms = wm.atoms;
229 202
230 203 std.debug.print("display opened: screen={d} root=0x{x}\n", .{ wm.display.screen, wm.display.root });
231 204 std.debug.print("successfully became window manager\n", .{});
232 205 std.debug.print("atoms initialized with EWMH support\n", .{});
233 206
234 - grab_keybinds(&wm.display);
235 - scan_existing_windows(&wm.display);
207 + grab_keybinds(&wm.display, &wm);
208 + scan_existing_windows(&wm.display, &wm);
236 209
237 210 try run_autostart_commands(allocator, config.autostart.items);
238 211 std.debug.print("entering event loop\n", .{});
@@ -302,9 +275,9 @@ fn initialize_default_config() void {
302 275 config.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {};
303 276 }
304 277
305 -fn grab_keybinds(display: *Display) void {
306 - update_numlock_mask(display);
307 - const modifiers = [_]c_uint{ 0, xlib.LockMask, numlock_mask, numlock_mask | xlib.LockMask };
278 +fn grab_keybinds(display: *Display, wm: *WindowManager) void {
279 + update_numlock_mask(display, wm);
280 + const modifiers = [_]c_uint{ 0, xlib.LockMask, wm.numlock_mask, wm.numlock_mask | xlib.LockMask };
308 281
309 282 _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root);
310 283
@@ -349,7 +322,7 @@ fn grab_keybinds(display: *Display) void {
349 322 std.debug.print("grabbed {d} keybinds from config\n", .{config.keybinds.items.len});
350 323 }
351 324
352 -fn get_state(display: *Display, window: xlib.Window) c_long {
325 +fn get_state(display: *Display, window: xlib.Window, wm: *WindowManager) c_long {
353 326 var actual_type: xlib.Atom = 0;
354 327 var actual_format: c_int = 0;
355 328 var num_items: c_ulong = 0;
@@ -359,11 +332,11 @@ fn get_state(display: *Display, window: xlib.Window) c_long {
359 332 const result = xlib.XGetWindowProperty(
360 333 display.handle,
361 334 window,
362 - atoms.wm_state,
335 + wm.atoms.wm_state,
363 336 0,
364 337 2,
365 338 xlib.False,
366 - atoms.wm_state,
339 + wm.atoms.wm_state,
367 340 &actual_type,
368 341 &actual_format,
369 342 &num_items,
@@ -371,7 +344,7 @@ fn get_state(display: *Display, window: xlib.Window) c_long {
371 344 &prop,
372 345 );
373 346
374 - if (result != 0 or actual_type != atoms.wm_state or num_items < 1) {
347 + if (result != 0 or actual_type != wm.atoms.wm_state or num_items < 1) {
375 348 if (prop != null) {
376 349 _ = xlib.XFree(prop);
377 350 }
@@ -383,7 +356,7 @@ fn get_state(display: *Display, window: xlib.Window) c_long {
383 356 return state;
384 357 }
385 358
386 -fn scan_existing_windows(display: *Display) void {
359 +fn scan_existing_windows(display: *Display, wm: *WindowManager) void {
387 360 var root_return: xlib.Window = undefined;
388 361 var parent_return: xlib.Window = undefined;
389 362 var children: [*c]xlib.Window = undefined;
@@ -406,8 +379,8 @@ fn scan_existing_windows(display: *Display) void {
406 379 if (xlib.XGetTransientForHint(display.handle, children[index], &trans) != 0) {
407 380 continue;
408 381 }
409 - if (window_attrs.map_state == IsViewable or get_state(display, children[index]) == IconicState) {
410 - manage(display, children[index], &window_attrs);
382 + if (window_attrs.map_state == IsViewable or get_state(display, children[index], wm) == IconicState) {
383 + manage(display, children[index], &window_attrs, wm);
411 384 }
412 385 }
413 386
@@ -419,8 +392,8 @@ fn scan_existing_windows(display: *Display) void {
419 392 }
420 393 var trans: xlib.Window = 0;
421 394 if (xlib.XGetTransientForHint(display.handle, children[index], &trans) != 0) {
422 - if (window_attrs.map_state == IsViewable or get_state(display, children[index]) == IconicState) {
423 - manage(display, children[index], &window_attrs);
395 + if (window_attrs.map_state == IsViewable or get_state(display, children[index], wm) == IconicState) {
396 + manage(display, children[index], &window_attrs, wm);
424 397 }
425 398 }
426 399 }
@@ -465,23 +438,23 @@ fn handle_event(display: *Display, event: *xlib.XEvent, wm: *WindowManager) void
465 438 }
466 439
467 440 switch (event_type) {
468 - .map_request => handle_map_request(display, &event.xmaprequest),
469 - .configure_request => handle_configure_request(display, &event.xconfigurerequest),
441 + .map_request => handle_map_request(display, &event.xmaprequest, wm),
442 + .configure_request => handle_configure_request(display, &event.xconfigurerequest, wm),
470 443 .key_press => handle_key_press(display, &event.xkey, wm),
471 - .destroy_notify => handle_destroy_notify(display, &event.xdestroywindow),
472 - .unmap_notify => handle_unmap_notify(display, &event.xunmap),
444 + .destroy_notify => handle_destroy_notify(display, &event.xdestroywindow, wm),
445 + .unmap_notify => handle_unmap_notify(display, &event.xunmap, wm),
473 446 .enter_notify => handle_enter_notify(display, &event.xcrossing, wm),
474 - .focus_in => handle_focus_in(display, &event.xfocus),
447 + .focus_in => handle_focus_in(display, &event.xfocus, wm),
475 448 .motion_notify => handle_motion_notify(display, &event.xmotion, wm),
476 - .client_message => handle_client_message(display, &event.xclient),
449 + .client_message => handle_client_message(display, &event.xclient, wm),
477 450 .button_press => handle_button_press(display, &event.xbutton, wm),
478 451 .expose => handle_expose(display, &event.xexpose, wm),
479 - .property_notify => handle_property_notify(display, &event.xproperty),
452 + .property_notify => handle_property_notify(display, &event.xproperty, wm),
480 453 else => {},
481 454 }
482 455 }
483 456
484 -fn handle_map_request(display: *Display, event: *xlib.XMapRequestEvent) void {
457 +fn handle_map_request(display: *Display, event: *xlib.XMapRequestEvent, wm: *WindowManager) void {
485 458 std.debug.print("map_request: window=0x{x}\n", .{event.window});
486 459
487 460 var window_attributes: xlib.XWindowAttributes = undefined;
@@ -495,10 +468,10 @@ fn handle_map_request(display: *Display, event: *xlib.XMapRequestEvent) void {
495 468 return;
496 469 }
497 470
498 - manage(display, event.window, &window_attributes);
471 + manage(display, event.window, &window_attributes, wm);
499 472 }
500 473
501 -fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttributes) void {
474 +fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttributes, wm: *WindowManager) void {
502 475 const client = client_mod.create(gpa.allocator(), win) orelse return;
503 476 var trans: xlib.Window = 0;
504 477
@@ -513,7 +486,7 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib
513 486 client.old_border_width = window_attrs.border_width;
514 487 client.border_width = config.border_width;
515 488
516 - update_title(display, client);
489 + update_title(display, client, wm);
517 490
518 491 if (xlib.XGetTransientForHint(display.handle, win, &trans) != 0) {
519 492 if (client_mod.window_to_client(monitor_mod.monitors, trans)) |transient_client| {
@@ -524,7 +497,7 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib
524 497
525 498 if (client.monitor == null) {
526 499 client.monitor = monitor_mod.selected_monitor;
527 - apply_rules(display, client);
500 + apply_rules(display, client, wm);
528 501 }
529 502
530 503 const monitor = client.monitor orelse return;
@@ -542,7 +515,7 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib
542 515 _ = xlib.XSetWindowBorder(display.handle, win, config.border_unfocused);
543 516 tiling.send_configure(client);
544 517
545 - update_window_type(display, client);
518 + update_window_type(display, client, wm);
546 519 update_size_hints(display, client);
547 520 update_wm_hints(display, client);
548 521
@@ -551,7 +524,7 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib
551 524 win,
552 525 xlib.EnterWindowMask | xlib.FocusChangeMask | xlib.PropertyChangeMask | xlib.StructureNotifyMask,
553 526 );
554 - grabbuttons(display, client, false);
527 + grabbuttons(display, client, false, wm);
555 528
556 529 if (!client.is_floating) {
557 530 client.is_floating = trans != 0 or client.is_fixed;
@@ -564,13 +537,13 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib
564 537 client_mod.attach_aside(client);
565 538 client_mod.attach_stack(client);
566 539
567 - _ = xlib.XChangeProperty(display.handle, display.root, atoms.net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
540 + _ = xlib.XChangeProperty(display.handle, display.root, wm.atoms.net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
568 541 _ = xlib.XMoveResizeWindow(display.handle, client.window, client.x + 2 * display.screen_width(), client.y, @intCast(client.width), @intCast(client.height));
569 - set_client_state(display, client, NormalState);
542 + set_client_state(display, client, NormalState, wm);
570 543
571 544 if (client.monitor == monitor_mod.selected_monitor) {
572 545 const selmon = monitor_mod.selected_monitor orelse return;
573 - unfocus_client(display, selmon.sel, false);
546 + unfocus_client(display, selmon.sel, false, wm);
574 547 }
575 548 monitor.sel = client;
576 549
@@ -578,12 +551,13 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib
578 551 monitor.scroll_offset = 0;
579 552 }
580 553
581 - arrange(monitor);
554 + arrange(monitor, wm);
582 555 _ = xlib.XMapWindow(display.handle, win);
583 - focus(display, null);
556 + focus(display, null, wm);
584 557 }
585 558
586 -fn handle_configure_request(display: *Display, event: *xlib.XConfigureRequestEvent) void {
559 +fn handle_configure_request(display: *Display, event: *xlib.XConfigureRequestEvent, wm: *WindowManager) void {
560 + _ = wm;
587 561 const client = client_mod.window_to_client(monitor_mod.monitors, event.window);
588 562
589 563 if (client) |managed_client| {
@@ -706,7 +680,7 @@ fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, st
706 680 .spawn => {
707 681 if (str_arg) |cmd| spawn_command(cmd);
708 682 },
709 - .kill_client => kill_focused(display),
683 + .kill_client => kill_focused(display, wm),
710 684 .quit => {
711 685 std.debug.print("quit keybind pressed\n", .{});
712 686 wm.running = false;
@@ -721,42 +695,42 @@ fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, st
721 695 }
722 696 }
723 697 },
724 - .focus_next => focusstack(display, 1),
725 - .focus_prev => focusstack(display, -1),
726 - .move_next => movestack(display, 1),
727 - .move_prev => movestack(display, -1),
728 - .resize_master => setmfact(@as(f32, @floatFromInt(int_arg)) / 1000.0),
729 - .inc_master => incnmaster(1),
730 - .dec_master => incnmaster(-1),
731 - .toggle_floating => toggle_floating(display),
732 - .toggle_fullscreen => toggle_fullscreen(display),
733 - .toggle_gaps => toggle_gaps(),
734 - .cycle_layout => cycle_layout(),
735 - .set_layout => set_layout(str_arg),
736 - .set_layout_tiling => set_layout_index(0),
737 - .set_layout_floating => set_layout_index(2),
698 + .focus_next => focusstack(display, 1, wm),
699 + .focus_prev => focusstack(display, -1, wm),
700 + .move_next => movestack(display, 1, wm),
701 + .move_prev => movestack(display, -1, wm),
702 + .resize_master => setmfact(@as(f32, @floatFromInt(int_arg)) / 1000.0, wm),
703 + .inc_master => incnmaster(1, wm),
704 + .dec_master => incnmaster(-1, wm),
705 + .toggle_floating => toggle_floating(display, wm),
706 + .toggle_fullscreen => toggle_fullscreen(display, wm),
707 + .toggle_gaps => toggle_gaps(wm),
708 + .cycle_layout => cycle_layout(wm),
709 + .set_layout => set_layout(str_arg, wm),
710 + .set_layout_tiling => set_layout_index(0, wm),
711 + .set_layout_floating => set_layout_index(2, wm),
738 712 .view_tag => {
739 713 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
740 - view(display, tag_mask);
714 + view(display, tag_mask, wm);
741 715 },
742 - .view_next_tag => view_adjacent_tag(display, 1),
743 - .view_prev_tag => view_adjacent_tag(display, -1),
744 - .view_next_nonempty_tag => view_adjacent_nonempty_tag(display, 1),
745 - .view_prev_nonempty_tag => view_adjacent_nonempty_tag(display, -1),
716 + .view_next_tag => view_adjacent_tag(display, 1, wm),
717 + .view_prev_tag => view_adjacent_tag(display, -1, wm),
718 + .view_next_nonempty_tag => view_adjacent_nonempty_tag(display, 1, wm),
719 + .view_prev_nonempty_tag => view_adjacent_nonempty_tag(display, -1, wm),
746 720 .move_to_tag => {
747 721 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
748 - tag_client(display, tag_mask);
722 + tag_client(display, tag_mask, wm);
749 723 },
750 724 .toggle_view_tag => {
751 725 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
752 - toggle_view(display, tag_mask);
726 + toggle_view(display, tag_mask, wm);
753 727 },
754 728 .toggle_tag => {
755 729 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
756 - toggle_client_tag(display, tag_mask);
730 + toggle_client_tag(display, tag_mask, wm);
757 731 },
758 - .focus_monitor => focusmon(display, int_arg),
759 - .send_to_monitor => sendmon(display, int_arg),
732 + .focus_monitor => focusmon(display, int_arg, wm),
733 + .send_to_monitor => sendmon(display, int_arg, wm),
760 734 .scroll_left => scroll_layout(-1, wm),
761 735 .scroll_right => scroll_layout(1, wm),
762 736 }
@@ -796,7 +770,7 @@ fn reload_config(display: *Display, wm: *WindowManager) void {
796 770 wm.setup_bars();
797 771 rebuild_bar_blocks(wm);
798 772
799 - grab_keybinds(display);
773 + grab_keybinds(display, wm);
800 774 }
801 775
802 776 fn rebuild_bar_blocks(wm: *WindowManager) void {
@@ -858,7 +832,7 @@ fn spawn_terminal() void {
858 832 }
859 833 }
860 834
861 -fn movestack(display: *Display, direction: i32) void {
835 +fn movestack(display: *Display, direction: i32, wm: *WindowManager) void {
862 836 const monitor = monitor_mod.selected_monitor orelse return;
863 837 const current = monitor.sel orelse return;
864 838
@@ -915,13 +889,13 @@ fn movestack(display: *Display, direction: i32) void {
915 889 if (target) |swap_client| {
916 890 if (swap_client != current) {
917 891 client_mod.swap_clients(current, swap_client);
918 - arrange(monitor);
919 - focus(display, current);
892 + arrange(monitor, wm);
893 + focus(display, current, wm);
920 894 }
921 895 }
922 896 }
923 897
924 -fn toggle_view(display: *Display, tag_mask: u32) void {
898 +fn toggle_view(display: *Display, tag_mask: u32, wm: *WindowManager) void {
925 899 const monitor = monitor_mod.selected_monitor orelse return;
926 900 const new_tags = monitor.tagset[monitor.sel_tags] ^ tag_mask;
927 901 if (new_tags != 0) {
@@ -945,25 +919,25 @@ fn toggle_view(display: *Display, tag_mask: u32) void {
945 919 monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag];
946 920 monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag];
947 921
948 - focus_top_client(display, monitor);
949 - arrange(monitor);
950 - if (wm_ptr) |wm| wm.invalidate_bars();
922 + focus_top_client(display, monitor, wm);
923 + arrange(monitor, wm);
924 + wm.invalidate_bars();
951 925 }
952 926 }
953 927
954 -fn toggle_client_tag(display: *Display, tag_mask: u32) void {
928 +fn toggle_client_tag(display: *Display, tag_mask: u32, wm: *WindowManager) void {
955 929 const monitor = monitor_mod.selected_monitor orelse return;
956 930 const client = monitor.sel orelse return;
957 931 const new_tags = client.tags ^ tag_mask;
958 932 if (new_tags != 0) {
959 933 client.tags = new_tags;
960 - focus_top_client(display, monitor);
961 - arrange(monitor);
962 - if (wm_ptr) |wm| wm.invalidate_bars();
934 + focus_top_client(display, monitor, wm);
935 + arrange(monitor, wm);
936 + wm.invalidate_bars();
963 937 }
964 938 }
965 939
966 -fn toggle_gaps() void {
940 +fn toggle_gaps(wm: *WindowManager) void {
967 941 const monitor = monitor_mod.selected_monitor orelse return;
968 942 if (monitor.gap_inner_h == 0) {
969 943 monitor.gap_inner_h = config.gap_inner_h;
@@ -976,15 +950,15 @@ fn toggle_gaps() void {
976 950 monitor.gap_outer_h = 0;
977 951 monitor.gap_outer_v = 0;
978 952 }
979 - arrange(monitor);
953 + arrange(monitor, wm);
980 954 }
981 955
982 -fn kill_focused(display: *Display) void {
956 +fn kill_focused(display: *Display, wm: *WindowManager) void {
983 957 const selected = monitor_mod.selected_monitor orelse return;
984 958 const client = selected.sel orelse return;
985 959 std.debug.print("killing window: 0x{x}\n", .{client.window});
986 960
987 - if (!send_event(display, client, atoms.wm_delete)) {
961 + if (!send_event(display, client, wm.atoms.wm_delete, wm)) {
988 962 _ = xlib.XGrabServer(display.handle);
989 963 _ = xlib.XKillClient(display.handle, client.window);
990 964 _ = xlib.XSync(display.handle, xlib.False);
@@ -992,21 +966,21 @@ fn kill_focused(display: *Display) void {
992 966 }
993 967 }
994 968
995 -fn toggle_fullscreen(display: *Display) void {
969 +fn toggle_fullscreen(display: *Display, wm: *WindowManager) void {
996 970 const selected = monitor_mod.selected_monitor orelse return;
997 971 const client = selected.sel orelse return;
998 - set_fullscreen(display, client, !client.is_fullscreen);
972 + set_fullscreen(display, client, !client.is_fullscreen, wm);
999 973 }
1000 974
1001 -fn set_fullscreen(display: *Display, client: *Client, fullscreen: bool) void {
975 +fn set_fullscreen(display: *Display, client: *Client, fullscreen: bool, wm: *WindowManager) void {
1002 976 const monitor = client.monitor orelse return;
1003 977
1004 978 if (fullscreen and !client.is_fullscreen) {
1005 - var fullscreen_atom = atoms.net_wm_state_fullscreen;
979 + var fullscreen_atom = wm.atoms.net_wm_state_fullscreen;
1006 980 _ = xlib.XChangeProperty(
1007 981 display.handle,
1008 982 client.window,
1009 - atoms.net_wm_state,
983 + wm.atoms.net_wm_state,
1010 984 xlib.XA_ATOM,
1011 985 32,
1012 986 xlib.PropModeReplace,
@@ -1029,7 +1003,7 @@ fn set_fullscreen(display: *Display, client: *Client, fullscreen: bool) void {
1029 1003 _ = xlib.XChangeProperty(
1030 1004 display.handle,
1031 1005 client.window,
1032 - atoms.net_wm_state,
1006 + wm.atoms.net_wm_state,
1033 1007 xlib.XA_ATOM,
1034 1008 32,
1035 1009 xlib.PropModeReplace,
@@ -1046,13 +1020,13 @@ fn set_fullscreen(display: *Display, client: *Client, fullscreen: bool) void {
1046 1020 client.height = client.old_height;
1047 1021
1048 1022 tiling.resize_client(client, client.x, client.y, client.width, client.height);
1049 - arrange(monitor);
1023 + arrange(monitor, wm);
1050 1024
1051 1025 std.debug.print("fullscreen disabled: window=0x{x}\n", .{client.window});
1052 1026 }
1053 1027 }
1054 1028
1055 -fn view(display: *Display, tag_mask: u32) void {
1029 +fn view(display: *Display, tag_mask: u32, wm: *WindowManager) void {
1056 1030 const monitor = monitor_mod.selected_monitor orelse return;
1057 1031 if (tag_mask == monitor.tagset[monitor.sel_tags]) {
1058 1032 return;
@@ -1081,13 +1055,13 @@ fn view(display: *Display, tag_mask: u32) void {
1081 1055 monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag];
1082 1056 monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag];
1083 1057
1084 - focus_top_client(display, monitor);
1085 - arrange(monitor);
1086 - if (wm_ptr) |wm| wm.invalidate_bars();
1058 + focus_top_client(display, monitor, wm);
1059 + arrange(monitor, wm);
1060 + wm.invalidate_bars();
1087 1061 std.debug.print("view: tag_mask={d}\n", .{monitor.tagset[monitor.sel_tags]});
1088 1062 }
1089 1063
1090 -fn view_adjacent_tag(display: *Display, direction: i32) void {
1064 +fn view_adjacent_tag(display: *Display, direction: i32, wm: *WindowManager) void {
1091 1065 const monitor = monitor_mod.selected_monitor orelse return;
1092 1066 const current_tag = monitor.pertag.curtag;
1093 1067 var new_tag: i32 = @intCast(current_tag);
@@ -1097,10 +1071,10 @@ fn view_adjacent_tag(display: *Display, direction: i32) void {
1097 1071 if (new_tag > 9) new_tag = 1;
1098 1072
1099 1073 const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
1100 - view(display, tag_mask);
1074 + view(display, tag_mask, wm);
1101 1075 }
1102 1076
1103 -fn view_adjacent_nonempty_tag(display: *Display, direction: i32) void {
1077 +fn view_adjacent_nonempty_tag(display: *Display, direction: i32, wm: *WindowManager) void {
1104 1078 const monitor = monitor_mod.selected_monitor orelse return;
1105 1079 const current_tag = monitor.pertag.curtag;
1106 1080 var new_tag: i32 = @intCast(current_tag);
@@ -1113,7 +1087,7 @@ fn view_adjacent_nonempty_tag(display: *Display, direction: i32) void {
1113 1087
1114 1088 const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
1115 1089 if (has_clients_on_tag(monitor, tag_mask)) {
1116 - view(display, tag_mask);
1090 + view(display, tag_mask, wm);
1117 1091 return;
1118 1092 }
1119 1093 }
@@ -1130,24 +1104,24 @@ fn has_clients_on_tag(monitor: *monitor_mod.Monitor, tag_mask: u32) bool {
1130 1104 return false;
1131 1105 }
1132 1106
1133 -fn tag_client(display: *Display, tag_mask: u32) void {
1107 +fn tag_client(display: *Display, tag_mask: u32, wm: *WindowManager) void {
1134 1108 const monitor = monitor_mod.selected_monitor orelse return;
1135 1109 const client = monitor.sel orelse return;
1136 1110 if (tag_mask == 0) {
1137 1111 return;
1138 1112 }
1139 1113 client.tags = tag_mask;
1140 - focus_top_client(display, monitor);
1141 - arrange(monitor);
1142 - if (wm_ptr) |wm| wm.invalidate_bars();
1114 + focus_top_client(display, monitor, wm);
1115 + arrange(monitor, wm);
1116 + wm.invalidate_bars();
1143 1117 std.debug.print("tag_client: window=0x{x} tag_mask={d}\n", .{ client.window, tag_mask });
1144 1118 }
1145 1119
1146 -fn focus_top_client(display: *Display, monitor: *Monitor) void {
1120 +fn focus_top_client(display: *Display, monitor: *Monitor, wm: *WindowManager) void {
1147 1121 var visible_client = monitor.stack;
1148 1122 while (visible_client) |client| {
1149 1123 if (client_mod.is_visible(client)) {
1150 - focus(display, client);
1124 + focus(display, client, wm);
1151 1125 return;
1152 1126 }
1153 1127 visible_client = client.stack_next;
@@ -1156,7 +1130,7 @@ fn focus_top_client(display: *Display, monitor: *Monitor) void {
1156 1130 _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
1157 1131 }
1158 1132
1159 -fn focusstack(display: *Display, direction: i32) void {
1133 +fn focusstack(display: *Display, direction: i32, wm: *WindowManager) void {
1160 1134 const monitor = monitor_mod.selected_monitor orelse return;
1161 1135 const current = monitor.sel orelse return;
1162 1136
@@ -1204,14 +1178,14 @@ fn focusstack(display: *Display, direction: i32) void {
1204 1178 }
1205 1179
1206 1180 if (next_client) |client| {
1207 - focus(display, client);
1181 + focus(display, client, wm);
1208 1182 if (client.monitor) |client_monitor| {
1209 - restack(display, client_monitor);
1183 + restack(display, client_monitor, wm);
1210 1184 }
1211 1185 }
1212 1186 }
1213 1187
1214 -fn toggle_floating(_: *Display) void {
1188 +fn toggle_floating(_: *Display, wm: *WindowManager) void {
1215 1189 const monitor = monitor_mod.selected_monitor orelse return;
1216 1190 const client = monitor.sel orelse return;
1217 1191
@@ -1225,20 +1199,20 @@ fn toggle_floating(_: *Display) void {
1225 1199 tiling.resize(client, client.x, client.y, client.width, client.height, false);
1226 1200 }
1227 1201
1228 - arrange(monitor);
1202 + arrange(monitor, wm);
1229 1203 std.debug.print("toggle_floating: window=0x{x} floating={}\n", .{ client.window, client.is_floating });
1230 1204 }
1231 1205
1232 -fn incnmaster(delta: i32) void {
1206 +fn incnmaster(delta: i32, wm: *WindowManager) void {
1233 1207 const monitor = monitor_mod.selected_monitor orelse return;
1234 1208 const new_val = @max(0, monitor.nmaster + delta);
1235 1209 monitor.nmaster = new_val;
1236 1210 monitor.pertag.nmasters[monitor.pertag.curtag] = new_val;
1237 - arrange(monitor);
1211 + arrange(monitor, wm);
1238 1212 std.debug.print("incnmaster: nmaster={d}\n", .{monitor.nmaster});
1239 1213 }
1240 1214
1241 -fn setmfact(delta: f32) void {
1215 +fn setmfact(delta: f32, wm: *WindowManager) void {
1242 1216 const monitor = monitor_mod.selected_monitor orelse return;
1243 1217 const new_mfact = monitor.mfact + delta;
1244 1218 if (new_mfact < 0.05 or new_mfact > 0.95) {
@@ -1246,11 +1220,11 @@ fn setmfact(delta: f32) void {
1246 1220 }
1247 1221 monitor.mfact = new_mfact;
1248 1222 monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact;
1249 - arrange(monitor);
1223 + arrange(monitor, wm);
1250 1224 std.debug.print("setmfact: mfact={d:.2}\n", .{monitor.mfact});
1251 1225 }
1252 1226
1253 -fn cycle_layout() void {
1227 +fn cycle_layout(wm: *WindowManager) void {
1254 1228 const monitor = monitor_mod.selected_monitor orelse return;
1255 1229 const new_lt = (monitor.sel_lt + 1) % 5;
1256 1230 monitor.sel_lt = new_lt;
@@ -1258,14 +1232,14 @@ fn cycle_layout() void {
1258 1232 if (new_lt != 3) {
1259 1233 monitor.scroll_offset = 0;
1260 1234 }
1261 - arrange(monitor);
1262 - if (wm_ptr) |wm| wm.invalidate_bars();
1235 + arrange(monitor, wm);
1236 + wm.invalidate_bars();
1263 1237 if (monitor.lt[monitor.sel_lt]) |layout| {
1264 1238 std.debug.print("cycle_layout: {s}\n", .{layout.symbol});
1265 1239 }
1266 1240 }
1267 1241
1268 -fn set_layout(layout_name: ?[]const u8) void {
1242 +fn set_layout(layout_name: ?[]const u8, wm: *WindowManager) void {
1269 1243 const monitor = monitor_mod.selected_monitor orelse return;
1270 1244 const name = layout_name orelse return;
1271 1245
@@ -1289,40 +1263,40 @@ fn set_layout(layout_name: ?[]const u8) void {
1289 1263 if (new_lt != 3) {
1290 1264 monitor.scroll_offset = 0;
1291 1265 }
1292 - arrange(monitor);
1293 - if (wm_ptr) |wm| wm.invalidate_bars();
1266 + arrange(monitor, wm);
1267 + wm.invalidate_bars();
1294 1268 if (monitor.lt[monitor.sel_lt]) |layout| {
1295 1269 std.debug.print("set_layout: {s}\n", .{layout.symbol});
1296 1270 }
1297 1271 }
1298 1272
1299 -fn set_layout_index(index: u32) void {
1273 +fn set_layout_index(index: u32, wm: *WindowManager) void {
1300 1274 const monitor = monitor_mod.selected_monitor orelse return;
1301 1275 monitor.sel_lt = index;
1302 1276 monitor.pertag.sellts[monitor.pertag.curtag] = index;
1303 1277 if (index != 3) {
1304 1278 monitor.scroll_offset = 0;
1305 1279 }
1306 - arrange(monitor);
1307 - if (wm_ptr) |wm| wm.invalidate_bars();
1280 + arrange(monitor, wm);
1281 + wm.invalidate_bars();
1308 1282 if (monitor.lt[monitor.sel_lt]) |layout| {
1309 1283 std.debug.print("set_layout_index: {s}\n", .{layout.symbol});
1310 1284 }
1311 1285 }
1312 1286
1313 -fn focusmon(display: *Display, direction: i32) void {
1287 +fn focusmon(display: *Display, direction: i32, wm: *WindowManager) void {
1314 1288 const selmon = monitor_mod.selected_monitor orelse return;
1315 1289 const target = monitor_mod.dir_to_monitor(direction) orelse return;
1316 1290 if (target == selmon) {
1317 1291 return;
1318 1292 }
1319 - unfocus_client(display, selmon.sel, false);
1293 + unfocus_client(display, selmon.sel, false, wm);
1320 1294 monitor_mod.selected_monitor = target;
1321 - focus(display, null);
1295 + focus(display, null, wm);
1322 1296 std.debug.print("focusmon: monitor {d}\n", .{target.num});
1323 1297 }
1324 1298
1325 -fn sendmon(display: *Display, direction: i32) void {
1299 +fn sendmon(display: *Display, direction: i32, wm: *WindowManager) void {
1326 1300 const source_monitor = monitor_mod.selected_monitor orelse return;
1327 1301 const client = source_monitor.sel orelse return;
1328 1302 const target = monitor_mod.dir_to_monitor(direction) orelse return;
@@ -1338,9 +1312,9 @@ fn sendmon(display: *Display, direction: i32) void {
1338 1312 client_mod.attach_aside(client);
1339 1313 client_mod.attach_stack(client);
1340 1314
1341 - focus_top_client(display, source_monitor);
1342 - arrange(source_monitor);
1343 - arrange(target);
1315 + focus_top_client(display, source_monitor, wm);
1316 + arrange(source_monitor, wm);
1317 + arrange(target, wm);
1344 1318
1345 1319 std.debug.print("sendmon: window=0x{x} to monitor {d}\n", .{ client.window, target.num });
1346 1320 }
@@ -1373,7 +1347,7 @@ fn movemouse(display: *Display, wm: *WindowManager) void {
1373 1347 return;
1374 1348 }
1375 1349
1376 - restack(display, monitor);
1350 + restack(display, monitor, wm);
1377 1351
1378 1352 const was_floating = client.is_floating;
1379 1353 if (!client.is_floating) {
@@ -1451,11 +1425,11 @@ fn movemouse(display: *Display, wm: *WindowManager) void {
1451 1425 client_mod.attach_aside(client);
1452 1426 client_mod.attach_stack(client);
1453 1427 monitor_mod.selected_monitor = new_mon;
1454 - focus(display, client);
1455 - arrange(monitor);
1456 - arrange(new_mon.?);
1428 + focus(display, client, wm);
1429 + arrange(monitor, wm);
1430 + arrange(new_mon.?, wm);
1457 1431 } else {
1458 - arrange(monitor);
1432 + arrange(monitor, wm);
1459 1433 }
1460 1434
1461 1435 if (config.auto_tile and !was_floating) {
@@ -1468,7 +1442,7 @@ fn movemouse(display: *Display, wm: *WindowManager) void {
1468 1442 }
1469 1443
1470 1444 client.is_floating = false;
1471 - arrange(drop_monitor);
1445 + arrange(drop_monitor, wm);
1472 1446 }
1473 1447 }
1474 1448
@@ -1480,7 +1454,7 @@ fn resizemouse(display: *Display, wm: *WindowManager) void {
1480 1454 return;
1481 1455 }
1482 1456
1483 - restack(display, monitor);
1457 + restack(display, monitor, wm);
1484 1458
1485 1459 if (!client.is_floating) {
1486 1460 client.is_floating = true;
@@ -1542,7 +1516,7 @@ fn resizemouse(display: *Display, wm: *WindowManager) void {
1542 1516 }
1543 1517
1544 1518 _ = xlib.XUngrabPointer(display.handle, xlib.CurrentTime);
1545 - arrange(monitor);
1519 + arrange(monitor, wm);
1546 1520 }
1547 1521
1548 1522 fn handle_expose(display: *Display, event: *xlib.XExposeEvent, wm: *WindowManager) void {
@@ -1553,7 +1527,7 @@ fn handle_expose(display: *Display, event: *xlib.XExposeEvent, wm: *WindowManage
1553 1527 }
1554 1528 }
1555 1529
1556 -fn clean_mask(mask: c_uint) c_uint {
1530 +fn clean_mask(mask: c_uint, wm: *WindowManager) c_uint {
1557 1531 const lock: c_uint = @intCast(xlib.LockMask);
1558 1532 const shift: c_uint = @intCast(xlib.ShiftMask);
1559 1533 const ctrl: c_uint = @intCast(xlib.ControlMask);
@@ -1562,7 +1536,7 @@ fn clean_mask(mask: c_uint) c_uint {
1562 1536 const mod3: c_uint = @intCast(xlib.Mod3Mask);
1563 1537 const mod4: c_uint = @intCast(xlib.Mod4Mask);
1564 1538 const mod5: c_uint = @intCast(xlib.Mod5Mask);
1565 - return mask & ~(lock | numlock_mask) & (shift | ctrl | mod1 | mod2 | mod3 | mod4 | mod5);
1539 + return mask & ~(lock | wm.numlock_mask) & (shift | ctrl | mod1 | mod2 | mod3 | mod4 | mod5);
1566 1540 }
1567 1541
1568 1542 fn handle_button_press(display: *Display, event: *xlib.XButtonEvent, wm: *WindowManager) void {
@@ -1572,10 +1546,10 @@ fn handle_button_press(display: *Display, event: *xlib.XButtonEvent, wm: *Window
1572 1546 if (clicked_monitor) |monitor| {
1573 1547 if (monitor != monitor_mod.selected_monitor) {
1574 1548 if (monitor_mod.selected_monitor) |selmon| {
1575 - unfocus_client(display, selmon.sel, true);
1549 + unfocus_client(display, selmon.sel, true, wm);
1576 1550 }
1577 1551 monitor_mod.selected_monitor = monitor;
1578 - focus(display, null);
1552 + focus(display, null, wm);
1579 1553 }
1580 1554 }
1581 1555
@@ -1583,24 +1557,24 @@ fn handle_button_press(display: *Display, event: *xlib.XButtonEvent, wm: *Window
1583 1557 const clicked_tag = bar.handle_click(display.handle, event.x, wm.config);
1584 1558 if (clicked_tag) |tag_index| {
1585 1559 const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index);
1586 - view(display, tag_mask);
1560 + view(display, tag_mask, wm);
1587 1561 }
1588 1562 return;
1589 1563 }
1590 1564
1591 1565 const click_client = client_mod.window_to_client(monitor_mod.monitors, event.window);
1592 1566 if (click_client) |found_client| {
1593 - focus(display, found_client);
1567 + focus(display, found_client, wm);
1594 1568 if (monitor_mod.selected_monitor) |selmon| {
1595 - restack(display, selmon);
1569 + restack(display, selmon, wm);
1596 1570 }
1597 1571 _ = xlib.XAllowEvents(display.handle, xlib.ReplayPointer, xlib.CurrentTime);
1598 1572 }
1599 1573
1600 - const clean_state = clean_mask(event.state);
1574 + const clean_state = clean_mask(event.state, wm);
1601 1575 for (config.buttons.items) |button| {
1602 1576 if (button.click != .client_win) continue;
1603 - const button_clean_mask = clean_mask(button.mod_mask);
1577 + const button_clean_mask = clean_mask(button.mod_mask, wm);
1604 1578 if (clean_state == button_clean_mask and event.button == button.button) {
1605 1579 switch (button.action) {
1606 1580 .move_mouse => movemouse(display, wm),
@@ -1609,7 +1583,7 @@ fn handle_button_press(display: *Display, event: *xlib.XButtonEvent, wm: *Window
1609 1583 if (click_client) |found_client| {
1610 1584 found_client.is_floating = !found_client.is_floating;
1611 1585 if (monitor_mod.selected_monitor) |monitor| {
1612 - arrange(monitor);
1586 + arrange(monitor, wm);
1613 1587 }
1614 1588 }
1615 1589 },
@@ -1619,28 +1593,28 @@ fn handle_button_press(display: *Display, event: *xlib.XButtonEvent, wm: *Window
1619 1593 }
1620 1594 }
1621 1595
1622 -fn handle_client_message(display: *Display, event: *xlib.XClientMessageEvent) void {
1596 +fn handle_client_message(display: *Display, event: *xlib.XClientMessageEvent, wm: *WindowManager) void {
1623 1597 const client = client_mod.window_to_client(monitor_mod.monitors, event.window) orelse return;
1624 1598
1625 - if (event.message_type == atoms.net_wm_state) {
1599 + if (event.message_type == wm.atoms.net_wm_state) {
1626 1600 const action = event.data.l[0];
1627 1601 const first_property = @as(xlib.Atom, @intCast(event.data.l[1]));
1628 1602 const second_property = @as(xlib.Atom, @intCast(event.data.l[2]));
1629 1603
1630 - if (first_property == atoms.net_wm_state_fullscreen or second_property == atoms.net_wm_state_fullscreen) {
1604 + if (first_property == wm.atoms.net_wm_state_fullscreen or second_property == wm.atoms.net_wm_state_fullscreen) {
1631 1605 const net_wm_state_remove = 0;
1632 1606 const net_wm_state_add = 1;
1633 1607 const net_wm_state_toggle = 2;
1634 1608
1635 1609 if (action == net_wm_state_add) {
1636 - set_fullscreen(display, client, true);
1610 + set_fullscreen(display, client, true, wm);
1637 1611 } else if (action == net_wm_state_remove) {
1638 - set_fullscreen(display, client, false);
1612 + set_fullscreen(display, client, false, wm);
1639 1613 } else if (action == net_wm_state_toggle) {
1640 - set_fullscreen(display, client, !client.is_fullscreen);
1614 + set_fullscreen(display, client, !client.is_fullscreen, wm);
1641 1615 }
1642 1616 }
1643 - } else if (event.message_type == atoms.net_active_window) {
1617 + } else if (event.message_type == wm.atoms.net_active_window) {
1644 1618 const selected = monitor_mod.selected_monitor orelse return;
1645 1619 if (client != selected.sel and !client.is_urgent) {
1646 1620 set_urgent(display, client, true);
@@ -1648,19 +1622,19 @@ fn handle_client_message(display: *Display, event: *xlib.XClientMessageEvent) vo
1648 1622 }
1649 1623 }
1650 1624
1651 -fn handle_destroy_notify(display: *Display, event: *xlib.XDestroyWindowEvent) void {
1625 +fn handle_destroy_notify(display: *Display, event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void {
1652 1626 const client = client_mod.window_to_client(monitor_mod.monitors, event.window) orelse return;
1653 1627 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
1654 - unmanage(display, client);
1628 + unmanage(display, client, wm);
1655 1629 }
1656 1630
1657 -fn handle_unmap_notify(display: *Display, event: *xlib.XUnmapEvent) void {
1631 +fn handle_unmap_notify(display: *Display, event: *xlib.XUnmapEvent, wm: *WindowManager) void {
1658 1632 const client = client_mod.window_to_client(monitor_mod.monitors, event.window) orelse return;
1659 1633 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
1660 - unmanage(display, client);
1634 + unmanage(display, client, wm);
1661 1635 }
1662 1636
1663 -fn unmanage(display: *Display, client: *Client) void {
1637 +fn unmanage(display: *Display, client: *Client, wm: *WindowManager) void {
1664 1638 const client_monitor = client.monitor;
1665 1639
1666 1640 var next_focus: ?*Client = null;
@@ -1695,22 +1669,21 @@ fn unmanage(display: *Display, client: *Client) void {
1695 1669 monitor.scroll_offset = 0;
1696 1670 }
1697 1671 }
1698 - arrange(monitor);
1672 + arrange(monitor, wm);
1699 1673 }
1700 1674
1701 1675 if (client_monitor) |monitor| {
1702 1676 if (monitor.sel) |selected| {
1703 - focus(display, selected);
1677 + focus(display, selected, wm);
1704 1678 }
1705 1679 }
1706 1680
1707 1681 client_mod.destroy(gpa.allocator(), client);
1708 - update_client_list(display);
1709 - if (wm_ptr) |wm| wm.invalidate_bars();
1682 + update_client_list(display, wm);
1683 + wm.invalidate_bars();
1710 1684 }
1711 1685
1712 1686 fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent, wm: *WindowManager) void {
1713 - _ = wm;
1714 1687 if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != display.root) {
1715 1688 return;
1716 1689 }
@@ -1721,7 +1694,7 @@ fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent, wm: *Wind
1721 1694
1722 1695 if (target_mon != selmon) {
1723 1696 if (selmon) |sel| {
1724 - unfocus_client(display, sel.sel, true);
1697 + unfocus_client(display, sel.sel, true, wm);
1725 1698 }
1726 1699 monitor_mod.selected_monitor = target_mon;
1727 1700 } else if (client == null) {
@@ -1732,23 +1705,23 @@ fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent, wm: *Wind
1732 1705 }
1733 1706 }
1734 1707
1735 - focus(display, client);
1708 + focus(display, client, wm);
1736 1709 }
1737 1710
1738 -fn handle_focus_in(display: *Display, event: *xlib.XFocusChangeEvent) void {
1711 +fn handle_focus_in(display: *Display, event: *xlib.XFocusChangeEvent, wm: *WindowManager) void {
1739 1712 const selmon = monitor_mod.selected_monitor orelse return;
1740 1713 const selected = selmon.sel orelse return;
1741 1714 if (event.window != selected.window) {
1742 - set_focus(display, selected);
1715 + set_focus(display, selected, wm);
1743 1716 }
1744 1717 }
1745 1718
1746 -fn set_focus(display: *Display, client: *Client) void {
1719 +fn set_focus(display: *Display, client: *Client, wm: *WindowManager) void {
1747 1720 if (!client.never_focus) {
1748 1721 _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime);
1749 - _ = xlib.XChangeProperty(display.handle, display.root, atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
1722 + _ = xlib.XChangeProperty(display.handle, display.root, wm.atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
1750 1723 }
1751 - _ = send_event(display, client, atoms.wm_take_focus);
1724 + _ = send_event(display, client, wm.atoms.wm_take_focus, wm);
1752 1725 }
1753 1726
1754 1727 fn handle_motion_notify(display: *Display, event: *xlib.XMotionEvent, wm: *WindowManager) void {
@@ -1757,15 +1730,15 @@ fn handle_motion_notify(display: *Display, event: *xlib.XMotionEvent, wm: *Windo
1757 1730 const target_mon = monitor_mod.rect_to_monitor(event.x_root, event.y_root, 1, 1);
1758 1731 if (target_mon != wm.last_motion_monitor and wm.last_motion_monitor != null) {
1759 1732 if (monitor_mod.selected_monitor) |selmon| {
1760 - unfocus_client(display, selmon.sel, true);
1733 + unfocus_client(display, selmon.sel, true, wm);
1761 1734 }
1762 1735 monitor_mod.selected_monitor = target_mon;
1763 - focus(display, null);
1736 + focus(display, null, wm);
1764 1737 }
1765 1738 wm.last_motion_monitor = target_mon;
1766 1739 }
1767 1740
1768 -fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void {
1741 +fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent, wm: *WindowManager) void {
1769 1742 if (event.state == xlib.PropertyDelete) {
1770 1743 return;
1771 1744 }
@@ -1778,7 +1751,7 @@ fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void {
1778 1751 client.is_floating = client_mod.window_to_client(monitor_mod.monitors, trans) != null;
1779 1752 if (client.is_floating) {
1780 1753 if (client.monitor) |monitor| {
1781 - arrange(monitor);
1754 + arrange(monitor, wm);
1782 1755 }
1783 1756 }
1784 1757 }
@@ -1786,31 +1759,31 @@ fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void {
1786 1759 client.hints_valid = false;
1787 1760 } else if (event.atom == xlib.XA_WM_HINTS) {
1788 1761 update_wm_hints(display, client);
1789 - if (wm_ptr) |wm| wm.invalidate_bars();
1790 - } else if (event.atom == xlib.XA_WM_NAME or event.atom == atoms.net_wm_name) {
1791 - update_title(display, client);
1792 - } else if (event.atom == atoms.net_wm_window_type) {
1793 - update_window_type(display, client);
1762 + wm.invalidate_bars();
1763 + } else if (event.atom == xlib.XA_WM_NAME or event.atom == wm.atoms.net_wm_name) {
1764 + update_title(display, client, wm);
1765 + } else if (event.atom == wm.atoms.net_wm_window_type) {
1766 + update_window_type(display, client, wm);
1794 1767 }
1795 1768 }
1796 1769
1797 -fn unfocus_client(display: *Display, client: ?*Client, reset_input_focus: bool) void {
1770 +fn unfocus_client(display: *Display, client: ?*Client, reset_input_focus: bool, wm: *WindowManager) void {
1798 1771 const unfocus_target = client orelse return;
1799 - grabbuttons(display, unfocus_target, false);
1772 + grabbuttons(display, unfocus_target, false, wm);
1800 1773 _ = xlib.XSetWindowBorder(display.handle, unfocus_target.window, config.border_unfocused);
1801 1774 if (reset_input_focus) {
1802 1775 _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
1803 - _ = xlib.XDeleteProperty(display.handle, display.root, atoms.net_active_window);
1776 + _ = xlib.XDeleteProperty(display.handle, display.root, wm.atoms.net_active_window);
1804 1777 }
1805 1778 }
1806 1779
1807 -fn set_client_state(display: *Display, client: *Client, state: c_long) void {
1780 +fn set_client_state(display: *Display, client: *Client, state: c_long, wm: *WindowManager) void {
1808 1781 var data: [2]c_long = .{ state, xlib.None };
1809 - _ = xlib.c.XChangeProperty(display.handle, client.window, atoms.wm_state, xlib.XA_ATOM, 32, xlib.PropModeReplace, @ptrCast(&data), 2);
1782 + _ = xlib.c.XChangeProperty(display.handle, client.window, wm.atoms.wm_state, xlib.XA_ATOM, 32, xlib.PropModeReplace, @ptrCast(&data), 2);
1810 1783 }
1811 1784
1812 -fn update_numlock_mask(display: *Display) void {
1813 - numlock_mask = 0;
1785 +fn update_numlock_mask(display: *Display, wm: *WindowManager) void {
1786 + wm.numlock_mask = 0;
1814 1787 const modmap = xlib.XGetModifierMapping(display.handle);
1815 1788 if (modmap == null) return;
1816 1789 defer _ = xlib.XFreeModifiermap(modmap);
@@ -1823,15 +1796,15 @@ fn update_numlock_mask(display: *Display) void {
1823 1796 while (key_index < @as(usize, @intCast(modmap.*.max_keypermod))) : (key_index += 1) {
1824 1797 const keycode = modmap.*.modifiermap[modifier_index * @as(usize, @intCast(modmap.*.max_keypermod)) + key_index];
1825 1798 if (keycode == numlock_keycode) {
1826 - numlock_mask = @as(c_uint, 1) << @intCast(modifier_index);
1799 + wm.numlock_mask = @as(c_uint, 1) << @intCast(modifier_index);
1827 1800 }
1828 1801 }
1829 1802 }
1830 1803 }
1831 1804
1832 -fn grabbuttons(display: *Display, client: *Client, focused: bool) void {
1833 - update_numlock_mask(display);
1834 - const modifiers = [_]c_uint{ 0, xlib.LockMask, numlock_mask, numlock_mask | xlib.LockMask };
1805 +fn grabbuttons(display: *Display, client: *Client, focused: bool, wm: *WindowManager) void {
1806 + update_numlock_mask(display, wm);
1807 + const modifiers = [_]c_uint{ 0, xlib.LockMask, wm.numlock_mask, wm.numlock_mask | xlib.LockMask };
1835 1808
1836 1809 _ = xlib.XUngrabButton(display.handle, xlib.AnyButton, xlib.AnyModifier, client.window);
1837 1810 if (!focused) {
@@ -1868,7 +1841,7 @@ fn grabbuttons(display: *Display, client: *Client, focused: bool) void {
1868 1841 }
1869 1842 }
1870 1843
1871 -fn focus(display: *Display, target_client: ?*Client) void {
1844 +fn focus(display: *Display, target_client: ?*Client, wm: *WindowManager) void {
1872 1845 const selmon = monitor_mod.selected_monitor orelse return;
1873 1846
1874 1847 var focus_client = target_client;
@@ -1881,7 +1854,7 @@ fn focus(display: *Display, target_client: ?*Client) void {
1881 1854 }
1882 1855
1883 1856 if (selmon.sel != null and selmon.sel != focus_client) {
1884 - unfocus_client(display, selmon.sel, false);
1857 + unfocus_client(display, selmon.sel, false, wm);
1885 1858 }
1886 1859
1887 1860 if (focus_client) |client| {
@@ -1893,16 +1866,16 @@ fn focus(display: *Display, target_client: ?*Client) void {
1893 1866 }
1894 1867 client_mod.detach_stack(client);
1895 1868 client_mod.attach_stack(client);
1896 - grabbuttons(display, client, true);
1869 + grabbuttons(display, client, true, wm);
1897 1870 _ = xlib.XSetWindowBorder(display.handle, client.window, config.border_focused);
1898 1871 if (!client.never_focus) {
1899 1872 _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime);
1900 - _ = xlib.XChangeProperty(display.handle, display.root, atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
1873 + _ = xlib.XChangeProperty(display.handle, display.root, wm.atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
1901 1874 }
1902 - _ = send_event(display, client, atoms.wm_take_focus);
1875 + _ = send_event(display, client, wm.atoms.wm_take_focus, wm);
1903 1876 } else {
1904 1877 _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
1905 - _ = xlib.XDeleteProperty(display.handle, display.root, atoms.net_active_window);
1878 + _ = xlib.XDeleteProperty(display.handle, display.root, wm.atoms.net_active_window);
1906 1879 }
1907 1880
1908 1881 const current_selmon = monitor_mod.selected_monitor orelse return;
@@ -1910,15 +1883,15 @@ fn focus(display: *Display, target_client: ?*Client) void {
1910 1883
1911 1884 if (focus_client) |client| {
1912 1885 if (is_scrolling_layout(current_selmon)) {
1913 - scroll_to_window(client, true, wm_ptr orelse return);
1886 + scroll_to_window(client, true, wm);
1914 1887 }
1915 1888 }
1916 1889
1917 - if (wm_ptr) |wm| wm.invalidate_bars();
1890 + wm.invalidate_bars();
1918 1891 }
1919 1892
1920 -fn restack(display: *Display, monitor: *Monitor) void {
1921 - if (wm_ptr) |wm| wm.invalidate_bars();
1893 +fn restack(display: *Display, monitor: *Monitor, wm: *WindowManager) void {
1894 + wm.invalidate_bars();
1922 1895 const selected_client = monitor.sel orelse return;
1923 1896
1924 1897 if (selected_client.is_floating or monitor.lt[monitor.sel_lt] == null) {
@@ -1946,20 +1919,14 @@ fn restack(display: *Display, monitor: *Monitor) void {
1946 1919 while (xlib.c.XCheckMaskEvent(display.handle, xlib.EnterWindowMask, &discard_event) != 0) {}
1947 1920 }
1948 1921
1949 -fn arrange(monitor: *Monitor) void {
1950 - // TODO(wm-refactor): display will become a WindowManager field; for now
1951 - // we use a module-level pointer set once after the display is opened.
1952 - if (wm_display) |display| {
1953 - showhide(display, monitor);
1954 - }
1922 +fn arrange(monitor: *Monitor, wm: *WindowManager) void {
1923 + showhide(&wm.display, monitor, wm);
1955 1924 if (monitor.lt[monitor.sel_lt]) |layout| {
1956 1925 if (layout.arrange_fn) |arrange_fn| {
1957 1926 arrange_fn(monitor);
1958 1927 }
1959 1928 }
1960 - if (wm_display) |display| {
1961 - restack(display, monitor);
1962 - }
1929 + restack(&wm.display, monitor, wm);
1963 1930 }
1964 1931
1965 1932 fn tick_animations(wm: *WindowManager) void {
@@ -1968,7 +1935,7 @@ fn tick_animations(wm: *WindowManager) void {
1968 1935 const monitor = wm.selected_monitor orelse return;
1969 1936 if (wm.scroll_animation.update()) |new_offset| {
1970 1937 monitor.scroll_offset = new_offset;
1971 - arrange(monitor);
1938 + arrange(monitor, wm);
1972 1939 }
1973 1940 }
1974 1941
@@ -2007,11 +1974,11 @@ fn scroll_to_window(client: *Client, animate: bool, wm: *WindowManager) void {
2007 1974 wm.scroll_animation.start(monitor.scroll_offset, target, wm.animation_config);
2008 1975 } else {
2009 1976 monitor.scroll_offset = target;
2010 - arrange(monitor);
1977 + arrange(monitor, wm);
2011 1978 }
2012 1979 }
2013 1980
2014 -fn showhide_client(display: *Display, client: ?*Client) void {
1981 +fn showhide_client(display: *Display, client: ?*Client, wm: *WindowManager) void {
2015 1982 const target = client orelse return;
2016 1983 if (client_mod.is_visible(target)) {
2017 1984 _ = xlib.XMoveWindow(display.handle, target.window, target.x, target.y);
@@ -2019,16 +1986,16 @@ fn showhide_client(display: *Display, client: ?*Client) void {
2019 1986 if ((monitor.lt[monitor.sel_lt] == null or target.is_floating) and !target.is_fullscreen) {
2020 1987 tiling.resize(target, target.x, target.y, target.width, target.height, false);
2021 1988 }
2022 - showhide_client(display, target.stack_next);
1989 + showhide_client(display, target.stack_next, wm);
2023 1990 } else {
2024 - showhide_client(display, target.stack_next);
1991 + showhide_client(display, target.stack_next, wm);
2025 1992 const client_width = target.width + 2 * target.border_width;
2026 1993 _ = xlib.XMoveWindow(display.handle, target.window, -2 * client_width, target.y);
2027 1994 }
2028 1995 }
2029 1996
2030 -fn showhide(display: *Display, monitor: *Monitor) void {
2031 - showhide_client(display, monitor.stack);
1997 +fn showhide(display: *Display, monitor: *Monitor, wm: *WindowManager) void {
1998 + showhide_client(display, monitor.stack, wm);
2032 1999 }
2033 2000
2034 2001 fn update_size_hints(display: *Display, client: *Client) void {
@@ -2109,20 +2076,20 @@ fn update_wm_hints(display: *Display, client: *Client) void {
2109 2076 }
2110 2077 }
2111 2078
2112 -fn update_window_type(display: *Display, client: *Client) void {
2113 - const state = get_atom_prop(display, client, atoms.net_wm_state);
2114 - const window_type = get_atom_prop(display, client, atoms.net_wm_window_type);
2079 +fn update_window_type(display: *Display, client: *Client, wm: *WindowManager) void {
2080 + const state = get_atom_prop(display, client, wm.atoms.net_wm_state);
2081 + const window_type = get_atom_prop(display, client, wm.atoms.net_wm_window_type);
2115 2082
2116 - if (state == atoms.net_wm_state_fullscreen) {
2117 - set_fullscreen(display, client, true);
2083 + if (state == wm.atoms.net_wm_state_fullscreen) {
2084 + set_fullscreen(display, client, true, wm);
2118 2085 }
2119 - if (window_type == atoms.net_wm_window_type_dialog) {
2086 + if (window_type == wm.atoms.net_wm_window_type_dialog) {
2120 2087 client.is_floating = true;
2121 2088 }
2122 2089 }
2123 2090
2124 -fn update_title(display: *Display, client: *Client) void {
2125 - if (!get_text_prop(display, client.window, atoms.net_wm_name, &client.name)) {
2091 +fn update_title(display: *Display, client: *Client, wm: *WindowManager) void {
2092 + if (!get_text_prop(display, client.window, wm.atoms.net_wm_name, &client.name)) {
2126 2093 _ = get_text_prop(display, client.window, xlib.XA_WM_NAME, &client.name);
2127 2094 }
2128 2095 if (client.name[0] == 0) {
@@ -2173,7 +2140,7 @@ fn get_text_prop(display: *Display, window: xlib.Window, atom: xlib.Atom, text:
2173 2140 return true;
2174 2141 }
2175 2142
2176 -fn apply_rules(display: *Display, client: *Client) void {
2143 +fn apply_rules(display: *Display, client: *Client, wm: *WindowManager) void {
2177 2144 var class_hint: xlib.XClassHint = .{ .res_name = null, .res_class = null };
2178 2145 _ = xlib.XGetClassHint(display.handle, client.window, &class_hint);
2179 2146
@@ -2226,26 +2193,26 @@ fn apply_rules(display: *Display, client: *Client) void {
2226 2193 const monitor_tagset = monitor.tagset[monitor.sel_tags];
2227 2194 const is_tag_focused = (monitor_tagset & client.tags) == client.tags;
2228 2195 if (!is_tag_focused) {
2229 - view(display, client.tags);
2196 + view(display, client.tags, wm);
2230 2197 }
2231 2198 }
2232 2199 }
2233 2200
2234 -fn update_client_list(display: *Display) void {
2235 - _ = xlib.XDeleteProperty(display.handle, display.root, atoms.net_client_list);
2201 +fn update_client_list(display: *Display, wm: *WindowManager) void {
2202 + _ = xlib.XDeleteProperty(display.handle, display.root, wm.atoms.net_client_list);
2236 2203
2237 2204 var current_monitor = monitor_mod.monitors;
2238 2205 while (current_monitor) |monitor| {
2239 2206 var current_client = monitor.clients;
2240 2207 while (current_client) |client| {
2241 - _ = xlib.XChangeProperty(display.handle, display.root, atoms.net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
2208 + _ = xlib.XChangeProperty(display.handle, display.root, wm.atoms.net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
2242 2209 current_client = client.next;
2243 2210 }
2244 2211 current_monitor = monitor.next;
2245 2212 }
2246 2213 }
2247 2214
2248 -fn send_event(display: *Display, client: *Client, protocol: xlib.Atom) bool {
2215 +fn send_event(display: *Display, client: *Client, protocol: xlib.Atom, wm: *WindowManager) bool {
2249 2216 var protocols: [*c]xlib.Atom = undefined;
2250 2217 var num_protocols: c_int = 0;
2251 2218 var exists = false;
@@ -2265,7 +2232,7 @@ fn send_event(display: *Display, client: *Client, protocol: xlib.Atom) bool {
2265 2232 var event: xlib.XEvent = undefined;
2266 2233 event.type = xlib.ClientMessage;
2267 2234 event.xclient.window = client.window;
2268 - event.xclient.message_type = atoms.wm_protocols;
2235 + event.xclient.message_type = wm.atoms.wm_protocols;
2269 2236 event.xclient.format = 32;
2270 2237 event.xclient.data.l[0] = @intCast(protocol);
2271 2238 event.xclient.data.l[1] = xlib.CurrentTime;