refactor: create new `Cursors` struct and removed unnecessary globals for config values
Browse files
- reduce usage of `display_global` by splitting into two narrower vars in preparation for `WindowManager` struct refactor. - remove usage of `monitor_mod.set_root_window` since `window_to_monitor` now takes `display`/`root` as parameters. In plans to remove monitor mod globals
+49 −60 src/main.zig
| @@ -29,19 +29,21 @@ var gpa = std.heap.GeneralPurposeAllocator(.{}){}; | ||
| 29 | 29 | var atoms: Atoms = undefined; |
| 30 | 30 | var wm_check_window: xlib.Window = undefined; |
| 31 | 31 | |
| 32 | -var border_color_focused: c_ulong = 0x6dade3; | |
| 33 | -var border_color_unfocused: c_ulong = 0x444444; | |
| 34 | -var border_width: i32 = 2; | |
| 35 | -var gap_outer_v: i32 = 5; | |
| 36 | -var gap_outer_h: i32 = 5; | |
| 37 | -var gap_inner_h: i32 = 5; | |
| 38 | -var gap_inner_v: i32 = 5; | |
| 39 | - | |
| 40 | -var tags: [9][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9" }; | |
| 32 | +const Cursors = struct { | |
| 33 | + normal: xlib.Cursor, | |
| 34 | + resize: xlib.Cursor, | |
| 35 | + move: xlib.Cursor, | |
| 36 | + | |
| 37 | + fn init(display: *Display) Cursors { | |
| 38 | + return .{ | |
| 39 | + .normal = xlib.XCreateFontCursor(display.handle, xlib.XC_left_ptr), | |
| 40 | + .resize = xlib.XCreateFontCursor(display.handle, xlib.XC_sizing), | |
| 41 | + .move = xlib.XCreateFontCursor(display.handle, xlib.XC_fleur), | |
| 42 | + }; | |
| 43 | + } | |
| 44 | +}; | |
| 41 | 45 | |
| 42 | -var cursor_normal: xlib.Cursor = 0; | |
| 43 | -var cursor_resize: xlib.Cursor = 0; | |
| 44 | -var cursor_move: xlib.Cursor = 0; | |
| 46 | +var cursors: Cursors = undefined; | |
| 45 | 47 | |
| 46 | 48 | const NormalState: c_long = 1; |
| 47 | 49 | const WithdrawnState: c_long = 0; |
| @@ -52,9 +54,12 @@ const snap_distance: i32 = 32; | ||
| 52 | 54 | var numlock_mask: c_uint = 0; |
| 53 | 55 | |
| 54 | 56 | var config: config_mod.Config = undefined; |
| 55 | -var display_global: ?*Display = null; | |
| 56 | 57 | var config_path_global: ?[]const u8 = null; |
| 57 | 58 | |
| 59 | +/// Interim pointer to the X11 display, used only by `arrange` until the | |
| 60 | +/// WindowManager struct refactor moves display ownership there properly. | |
| 61 | +var wm_display: ?*Display = null; | |
| 62 | + | |
| 58 | 63 | var scroll_animation: animations.Scroll_Animation = .{}; |
| 59 | 64 | var animation_config: animations.Animation_Config = .{ .duration_ms = 150, .easing = .ease_out }; |
| 60 | 65 | |
| @@ -209,7 +214,6 @@ pub fn main() !void { | ||
| 209 | 214 | if (loaded) { |
| 210 | 215 | config_path_global = config_path; |
| 211 | 216 | std.debug.print("loaded config from {s}\n", .{config_path}); |
| 212 | - apply_config_values(); | |
| 213 | 217 | } else { |
| 214 | 218 | std.debug.print("no config found, using defaults\n", .{}); |
| 215 | 219 | initialize_default_config(); |
| @@ -225,7 +229,8 @@ pub fn main() !void { | ||
| 225 | 229 | }; |
| 226 | 230 | defer display.close(); |
| 227 | 231 | |
| 228 | - display_global = &display; | |
| 232 | + x11_fd = xlib.XConnectionNumber(display.handle); | |
| 233 | + wm_display = &display; | |
| 229 | 234 | |
| 230 | 235 | std.debug.print("display opened: screen={d} root=0x{x}\n", .{ display.screen, display.root }); |
| 231 | 236 | std.debug.print("screen size: {d}x{d}\n", .{ display.screen_width(), display.screen_height() }); |
| @@ -237,15 +242,15 @@ pub fn main() !void { | ||
| 237 | 242 | |
| 238 | 243 | std.debug.print("successfully became window manager\n", .{}); |
| 239 | 244 | |
| 240 | - const atoms_result = Atoms.init(display_global.?.handle, display_global.?.root); | |
| 245 | + const atoms_result = Atoms.init(display.handle, display.root); | |
| 241 | 246 | atoms = atoms_result.atoms; |
| 242 | 247 | wm_check_window = atoms_result.check_window; |
| 243 | 248 | std.debug.print("atoms initialized with EWMH support\n", .{}); |
| 244 | 249 | |
| 245 | - setup_cursors(&display); | |
| 250 | + cursors = Cursors.init(&display); | |
| 251 | + _ = xlib.XDefineCursor(display.handle, display.root, cursors.normal); | |
| 246 | 252 | client_mod.init(allocator); |
| 247 | 253 | monitor_mod.init(allocator); |
| 248 | - monitor_mod.set_root_window(display.root, display.handle); | |
| 249 | 254 | tiling.set_display(display.handle); |
| 250 | 255 | tiling.set_screen_size(display.screen_width(), display.screen_height()); |
| 251 | 256 | |
| @@ -276,13 +281,6 @@ pub fn main() !void { | ||
| 276 | 281 | std.debug.print("oxwm exiting\n", .{}); |
| 277 | 282 | } |
| 278 | 283 | |
| 279 | -fn setup_cursors(display: *Display) void { | |
| 280 | - cursor_normal = xlib.XCreateFontCursor(display.handle, xlib.XC_left_ptr); | |
| 281 | - cursor_resize = xlib.XCreateFontCursor(display.handle, xlib.XC_sizing); | |
| 282 | - cursor_move = xlib.XCreateFontCursor(display.handle, xlib.XC_fleur); | |
| 283 | - _ = xlib.XDefineCursor(display.handle, display.root, cursor_normal); | |
| 284 | -} | |
| 285 | - | |
| 286 | 284 | fn setup_bars(allocator: std.mem.Allocator, display: *Display) void { |
| 287 | 285 | var current_monitor = monitor_mod.monitors; |
| 288 | 286 | var last_bar: ?*bar_mod.Bar = null; |
| @@ -432,17 +430,6 @@ fn setup_monitors(display: *Display) void { | ||
| 432 | 430 | std.debug.print("monitor created: {d}x{d}\n", .{ mon.mon_w, mon.mon_h }); |
| 433 | 431 | } |
| 434 | 432 | |
| 435 | -fn apply_config_values() void { | |
| 436 | - border_color_focused = config.border_focused; | |
| 437 | - border_color_unfocused = config.border_unfocused; | |
| 438 | - border_width = config.border_width; | |
| 439 | - gap_inner_h = config.gap_inner_h; | |
| 440 | - gap_inner_v = config.gap_inner_v; | |
| 441 | - gap_outer_h = config.gap_outer_h; | |
| 442 | - gap_outer_v = config.gap_outer_v; | |
| 443 | - tags = config.tags; | |
| 444 | -} | |
| 445 | - | |
| 446 | 433 | fn init_monitor_gaps(mon: *Monitor) void { |
| 447 | 434 | const any_gap_nonzero = config.gap_inner_h != 0 or config.gap_inner_v != 0 or |
| 448 | 435 | config.gap_outer_h != 0 or config.gap_outer_v != 0; |
| @@ -649,9 +636,9 @@ fn scan_existing_windows(display: *Display) void { | ||
| 649 | 636 | } |
| 650 | 637 | |
| 651 | 638 | fn run_event_loop(display: *Display) void { |
| 652 | - const x11_fd = xlib.XConnectionNumber(display.handle); | |
| 639 | + const fd = xlib.XConnectionNumber(display.handle); | |
| 653 | 640 | var fds = [_]std.posix.pollfd{ |
| 654 | - .{ .fd = x11_fd, .events = std.posix.POLL.IN, .revents = 0 }, | |
| 641 | + .{ .fd = fd, .events = std.posix.POLL.IN, .revents = 0 }, | |
| 655 | 642 | }; |
| 656 | 643 | |
| 657 | 644 | _ = xlib.XSync(display.handle, xlib.False); |
| @@ -667,7 +654,7 @@ fn run_event_loop(display: *Display) void { | ||
| 667 | 654 | var current_bar = bar_mod.bars; |
| 668 | 655 | while (current_bar) |bar| { |
| 669 | 656 | bar.update_blocks(); |
| 670 | - bar.draw(display.handle, &tags, config); | |
| 657 | + bar.draw(display.handle, &config.tags, config); | |
| 671 | 658 | current_bar = bar.next; |
| 672 | 659 | } |
| 673 | 660 | |
| @@ -730,7 +717,7 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib | ||
| 730 | 717 | client.old_width = window_attrs.width; |
| 731 | 718 | client.old_height = window_attrs.height; |
| 732 | 719 | client.old_border_width = window_attrs.border_width; |
| 733 | - client.border_width = border_width; | |
| 720 | + client.border_width = config.border_width; | |
| 734 | 721 | |
| 735 | 722 | update_title(display, client); |
| 736 | 723 | |
| @@ -758,7 +745,7 @@ fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttrib | ||
| 758 | 745 | client.y = @max(client.y, monitor.win_y); |
| 759 | 746 | |
| 760 | 747 | _ = xlib.XSetWindowBorderWidth(display.handle, win, @intCast(client.border_width)); |
| 761 | - _ = xlib.XSetWindowBorder(display.handle, win, border_color_unfocused); | |
| 748 | + _ = xlib.XSetWindowBorder(display.handle, win, config.border_unfocused); | |
| 762 | 749 | tiling.send_configure(client); |
| 763 | 750 | |
| 764 | 751 | update_window_type(display, client); |
| @@ -1014,7 +1001,6 @@ fn reload_config(display: *Display) void { | ||
| 1014 | 1001 | } else { |
| 1015 | 1002 | std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{}); |
| 1016 | 1003 | } |
| 1017 | - apply_config_values(); | |
| 1018 | 1004 | } else { |
| 1019 | 1005 | std.debug.print("reload failed, restoring defaults\n", .{}); |
| 1020 | 1006 | initialize_default_config(); |
| @@ -1049,12 +1035,13 @@ fn ungrab_keybinds(display: *Display) void { | ||
| 1049 | 1035 | _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root); |
| 1050 | 1036 | } |
| 1051 | 1037 | |
| 1038 | +/// File descriptor for the X11 connection. Set once after the display is | |
| 1039 | +/// opened and used only in post-fork child setup to close the inherited fd. | |
| 1040 | +var x11_fd: c_int = -1; | |
| 1041 | + | |
| 1052 | 1042 | fn spawn_child_setup() void { |
| 1053 | 1043 | _ = std.c.setsid(); |
| 1054 | - if (display_global) |display| { | |
| 1055 | - const display_fd = xlib.XConnectionNumber(display.handle); | |
| 1056 | - std.posix.close(@intCast(display_fd)); | |
| 1057 | - } | |
| 1044 | + if (x11_fd >= 0) std.posix.close(@intCast(x11_fd)); | |
| 1058 | 1045 | const sigchld_handler = std.posix.Sigaction{ |
| 1059 | 1046 | .handler = .{ .handler = std.posix.SIG.DFL }, |
| 1060 | 1047 | .mask = std.mem.zeroes(std.posix.sigset_t), |
| @@ -1206,10 +1193,10 @@ fn toggle_client_tag(display: *Display, tag_mask: u32) void { | ||
| 1206 | 1193 | fn toggle_gaps() void { |
| 1207 | 1194 | const monitor = monitor_mod.selected_monitor orelse return; |
| 1208 | 1195 | if (monitor.gap_inner_h == 0) { |
| 1209 | - monitor.gap_inner_h = gap_inner_v; | |
| 1210 | - monitor.gap_inner_v = gap_inner_v; | |
| 1211 | - monitor.gap_outer_h = gap_outer_h; | |
| 1212 | - monitor.gap_outer_v = gap_outer_v; | |
| 1196 | + monitor.gap_inner_h = config.gap_inner_h; | |
| 1197 | + monitor.gap_inner_v = config.gap_inner_v; | |
| 1198 | + monitor.gap_outer_h = config.gap_outer_h; | |
| 1199 | + monitor.gap_outer_v = config.gap_outer_v; | |
| 1213 | 1200 | } else { |
| 1214 | 1201 | monitor.gap_inner_h = 0; |
| 1215 | 1202 | monitor.gap_inner_v = 0; |
| @@ -1636,7 +1623,7 @@ fn movemouse(display: *Display) void { | ||
| 1636 | 1623 | xlib.GrabModeAsync, |
| 1637 | 1624 | xlib.GrabModeAsync, |
| 1638 | 1625 | xlib.None, |
| 1639 | - cursor_move, | |
| 1626 | + cursors.move, | |
| 1640 | 1627 | xlib.CurrentTime, |
| 1641 | 1628 | ); |
| 1642 | 1629 | |
| @@ -1734,7 +1721,7 @@ fn resizemouse(display: *Display) void { | ||
| 1734 | 1721 | xlib.GrabModeAsync, |
| 1735 | 1722 | xlib.GrabModeAsync, |
| 1736 | 1723 | xlib.None, |
| 1737 | - cursor_resize, | |
| 1724 | + cursors.resize, | |
| 1738 | 1725 | xlib.CurrentTime, |
| 1739 | 1726 | ); |
| 1740 | 1727 | |
| @@ -1790,7 +1777,7 @@ fn handle_expose(display: *Display, event: *xlib.XExposeEvent) void { | ||
| 1790 | 1777 | |
| 1791 | 1778 | if (bar_mod.window_to_bar(event.window)) |bar| { |
| 1792 | 1779 | bar.invalidate(); |
| 1793 | - bar.draw(display.handle, &tags, config); | |
| 1780 | + bar.draw(display.handle, &config.tags, config); | |
| 1794 | 1781 | } |
| 1795 | 1782 | } |
| 1796 | 1783 | |
| @@ -1809,7 +1796,7 @@ fn clean_mask(mask: c_uint) c_uint { | ||
| 1809 | 1796 | fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void { |
| 1810 | 1797 | std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow }); |
| 1811 | 1798 | |
| 1812 | - const clicked_monitor = monitor_mod.window_to_monitor(event.window); | |
| 1799 | + const clicked_monitor = monitor_mod.window_to_monitor(display.handle, display.root, event.window); | |
| 1813 | 1800 | if (clicked_monitor) |monitor| { |
| 1814 | 1801 | if (monitor != monitor_mod.selected_monitor) { |
| 1815 | 1802 | if (monitor_mod.selected_monitor) |selmon| { |
| @@ -1821,7 +1808,7 @@ fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void { | ||
| 1821 | 1808 | } |
| 1822 | 1809 | |
| 1823 | 1810 | if (bar_mod.window_to_bar(event.window)) |bar| { |
| 1824 | - const clicked_tag = bar.handle_click(event.x, &tags); | |
| 1811 | + const clicked_tag = bar.handle_click(event.x, &config.tags); | |
| 1825 | 1812 | if (clicked_tag) |tag_index| { |
| 1826 | 1813 | const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index); |
| 1827 | 1814 | view(display, tag_mask); |
| @@ -1956,7 +1943,7 @@ fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent) void { | ||
| 1956 | 1943 | } |
| 1957 | 1944 | |
| 1958 | 1945 | const client = client_mod.window_to_client(event.window); |
| 1959 | - const target_mon = if (client) |c| c.monitor else monitor_mod.window_to_monitor(event.window); | |
| 1946 | + const target_mon = if (client) |c| c.monitor else monitor_mod.window_to_monitor(display.handle, display.root, event.window); | |
| 1960 | 1947 | const selmon = monitor_mod.selected_monitor; |
| 1961 | 1948 | |
| 1962 | 1949 | if (target_mon != selmon) { |
| @@ -2041,7 +2028,7 @@ fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void { | ||
| 2041 | 2028 | fn unfocus_client(display: *Display, client: ?*Client, reset_input_focus: bool) void { |
| 2042 | 2029 | const unfocus_target = client orelse return; |
| 2043 | 2030 | grabbuttons(display, unfocus_target, false); |
| 2044 | - _ = xlib.XSetWindowBorder(display.handle, unfocus_target.window, border_color_unfocused); | |
| 2031 | + _ = xlib.XSetWindowBorder(display.handle, unfocus_target.window, config.border_unfocused); | |
| 2045 | 2032 | if (reset_input_focus) { |
| 2046 | 2033 | _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime); |
| 2047 | 2034 | _ = xlib.XDeleteProperty(display.handle, display.root, atoms.net_active_window); |
| @@ -2138,7 +2125,7 @@ fn focus(display: *Display, target_client: ?*Client) void { | ||
| 2138 | 2125 | client_mod.detach_stack(client); |
| 2139 | 2126 | client_mod.attach_stack(client); |
| 2140 | 2127 | grabbuttons(display, client, true); |
| 2141 | - _ = xlib.XSetWindowBorder(display.handle, client.window, border_color_focused); | |
| 2128 | + _ = xlib.XSetWindowBorder(display.handle, client.window, config.border_focused); | |
| 2142 | 2129 | if (!client.never_focus) { |
| 2143 | 2130 | _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime); |
| 2144 | 2131 | _ = xlib.XChangeProperty(display.handle, display.root, atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1); |
| @@ -2191,7 +2178,9 @@ fn restack(display: *Display, monitor: *Monitor) void { | ||
| 2191 | 2178 | } |
| 2192 | 2179 | |
| 2193 | 2180 | fn arrange(monitor: *Monitor) void { |
| 2194 | - if (display_global) |display| { | |
| 2181 | + // TODO: display will become a WindowManager field; for now | |
| 2182 | + // we use a module-level pointer set once after the display is opened. | |
| 2183 | + if (wm_display) |display| { | |
| 2195 | 2184 | showhide(display, monitor); |
| 2196 | 2185 | } |
| 2197 | 2186 | if (monitor.lt[monitor.sel_lt]) |layout| { |
| @@ -2199,7 +2188,7 @@ fn arrange(monitor: *Monitor) void { | ||
| 2199 | 2188 | arrange_fn(monitor); |
| 2200 | 2189 | } |
| 2201 | 2190 | } |
| 2202 | - if (display_global) |display| { | |
| 2191 | + if (wm_display) |display| { | |
| 2203 | 2192 | restack(display, monitor); |
| 2204 | 2193 | } |
| 2205 | 2194 | } |