tony / oxwm

refactor(wm): move keybind, numlock and bar helpers into `WindowManager` methods Browse files
emzywastaken committed 2026-02-22 parent f7c2786 commit efae0702386b29c1ef36a2c2b589b65335113528
Showing 2 changed files with 91 additions and 86 deletions. UnifiedSplit
+5 −86 src/main.zig
@@ -12,7 +12,6 @@ const bar_mod = @import("bar/bar.zig");
12 const config_mod = @import("config/config.zig"); 12 const config_mod = @import("config/config.zig");
13 const lua = @import("config/lua.zig"); 13 const lua = @import("config/lua.zig");
14 14
15 -const Display = display_mod.Display;
16 const Client = client_mod.Client; 15 const Client = client_mod.Client;
17 const Monitor = monitor_mod.Monitor; 16 const Monitor = monitor_mod.Monitor;
18 const WindowManager = wm_mod.WindowManager; 17 const WindowManager = wm_mod.WindowManager;
@@ -200,7 +199,7 @@ pub fn main() !void {
200 std.debug.print("successfully became window manager\n", .{}); 199 std.debug.print("successfully became window manager\n", .{});
201 std.debug.print("atoms initialized with EWMH support\n", .{}); 200 std.debug.print("atoms initialized with EWMH support\n", .{});
202 201
203 - grab_keybinds(&wm); 202 + wm.grab_keybinds();
204 scan_existing_windows(&wm); 203 scan_existing_windows(&wm);
205 204
206 try run_autostart_commands(allocator, wm.config.autostart.items); 205 try run_autostart_commands(allocator, wm.config.autostart.items);
@@ -271,53 +270,6 @@ fn initialize_default_config(cfg: *config_mod.Config) void {
271 cfg.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {}; 270 cfg.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {};
272 } 271 }
273 272
274 -fn grab_keybinds(wm: *WindowManager) void {
275 - update_numlock_mask(wm);
276 - const modifiers = [_]c_uint{ 0, xlib.LockMask, wm.numlock_mask, wm.numlock_mask | xlib.LockMask };
277 -
278 - _ = xlib.XUngrabKey(wm.display.handle, xlib.AnyKey, xlib.AnyModifier, wm.display.root);
279 -
280 - for (wm.config.keybinds.items) |keybind| {
281 - if (keybind.key_count == 0) continue;
282 - const first_key = keybind.keys[0];
283 - const keycode = xlib.XKeysymToKeycode(wm.display.handle, @intCast(first_key.keysym));
284 - if (keycode != 0) {
285 - for (modifiers) |modifier| {
286 - _ = xlib.XGrabKey(
287 - wm.display.handle,
288 - keycode,
289 - first_key.mod_mask | modifier,
290 - wm.display.root,
291 - xlib.True,
292 - xlib.GrabModeAsync,
293 - xlib.GrabModeAsync,
294 - );
295 - }
296 - }
297 - }
298 -
299 - for (wm.config.buttons.items) |button| {
300 - if (button.click == .client_win) {
301 - for (modifiers) |modifier| {
302 - _ = xlib.XGrabButton(
303 - wm.display.handle,
304 - @intCast(button.button),
305 - button.mod_mask | modifier,
306 - wm.display.root,
307 - xlib.True,
308 - xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
309 - xlib.GrabModeAsync,
310 - xlib.GrabModeAsync,
311 - xlib.None,
312 - xlib.None,
313 - );
314 - }
315 - }
316 - }
317 -
318 - std.debug.print("grabbed {d} keybinds from config\n", .{wm.config.keybinds.items.len});
319 -}
320 -
321 fn get_state(window: xlib.Window, wm: *WindowManager) c_long { 273 fn get_state(window: xlib.Window, wm: *WindowManager) c_long {
322 var actual_type: xlib.Atom = 0; 274 var actual_type: xlib.Atom = 0;
323 var actual_format: c_int = 0; 275 var actual_format: c_int = 0;
@@ -734,7 +686,7 @@ fn execute_action(action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8,
734 fn reload_config(wm: *WindowManager) void { 686 fn reload_config(wm: *WindowManager) void {
735 std.debug.print("reloading config...\n", .{}); 687 std.debug.print("reloading config...\n", .{});
736 688
737 - ungrab_keybinds(wm); 689 + wm.ungrab_keybinds();
738 690
739 wm.config.keybinds.clearRetainingCapacity(); 691 wm.config.keybinds.clearRetainingCapacity();
740 wm.config.buttons.clearRetainingCapacity(); 692 wm.config.buttons.clearRetainingCapacity();
@@ -763,22 +715,9 @@ fn reload_config(wm: *WindowManager) void {
763 bar_mod.destroy_bars(wm.bars, gpa.allocator(), wm.display.handle); 715 bar_mod.destroy_bars(wm.bars, gpa.allocator(), wm.display.handle);
764 wm.bars = null; 716 wm.bars = null;
765 wm.setup_bars(); 717 wm.setup_bars();
766 - rebuild_bar_blocks(wm); 718 + wm.rebuild_bar_blocks();
767 -
768 - grab_keybinds(wm);
769 -}
770 -
771 -fn rebuild_bar_blocks(wm: *WindowManager) void {
772 - var current_bar = wm.bars;
773 - while (current_bar) |bar| {
774 - bar.clear_blocks();
775 - wm.populate_bar_blocks(bar);
776 - current_bar = bar.next;
777 - }
778 -}
779 719
780 -fn ungrab_keybinds(wm: *WindowManager) void { 720 + wm.grab_keybinds();
781 - _ = xlib.XUngrabKey(wm.display.handle, xlib.AnyKey, xlib.AnyModifier, wm.display.root);
782 } 721 }
783 722
784 fn spawn_child_setup() void { 723 fn spawn_child_setup() void {
@@ -1778,28 +1717,8 @@ fn set_client_state(client: *Client, state: c_long, wm: *WindowManager) void {
1778 _ = xlib.c.XChangeProperty(wm.display.handle, client.window, wm.atoms.wm_state, xlib.XA_ATOM, 32, xlib.PropModeReplace, @ptrCast(&data), 2); 1717 _ = xlib.c.XChangeProperty(wm.display.handle, client.window, wm.atoms.wm_state, xlib.XA_ATOM, 32, xlib.PropModeReplace, @ptrCast(&data), 2);
1779 } 1718 }
1780 1719
1781 -fn update_numlock_mask(wm: *WindowManager) void {
1782 - wm.numlock_mask = 0;
1783 - const modmap = xlib.XGetModifierMapping(wm.display.handle);
1784 - if (modmap == null) return;
1785 - defer _ = xlib.XFreeModifiermap(modmap);
1786 -
1787 - const numlock_keycode = xlib.XKeysymToKeycode(wm.display.handle, xlib.XK_Num_Lock);
1788 -
1789 - var modifier_index: usize = 0;
1790 - while (modifier_index < 8) : (modifier_index += 1) {
1791 - var key_index: usize = 0;
1792 - while (key_index < @as(usize, @intCast(modmap.*.max_keypermod))) : (key_index += 1) {
1793 - const keycode = modmap.*.modifiermap[modifier_index * @as(usize, @intCast(modmap.*.max_keypermod)) + key_index];
1794 - if (keycode == numlock_keycode) {
1795 - wm.numlock_mask = @as(c_uint, 1) << @intCast(modifier_index);
1796 - }
1797 - }
1798 - }
1799 -}
1800 -
1801 fn grabbuttons(client: *Client, focused: bool, wm: *WindowManager) void { 1720 fn grabbuttons(client: *Client, focused: bool, wm: *WindowManager) void {
1802 - update_numlock_mask(wm); 1721 + wm.update_numlock_mask();
1803 const modifiers = [_]c_uint{ 0, xlib.LockMask, wm.numlock_mask, wm.numlock_mask | xlib.LockMask }; 1722 const modifiers = [_]c_uint{ 0, xlib.LockMask, wm.numlock_mask, wm.numlock_mask | xlib.LockMask };
1804 1723
1805 _ = xlib.XUngrabButton(wm.display.handle, xlib.AnyButton, xlib.AnyModifier, client.window); 1724 _ = xlib.XUngrabButton(wm.display.handle, xlib.AnyButton, xlib.AnyModifier, client.window);
+86 −0 src/wm.zig
@@ -53,6 +53,7 @@ pub const WindowManager = struct {
53 53
54 atoms: Atoms, 54 atoms: Atoms,
55 cursors: Cursors, 55 cursors: Cursors,
56 + /// Cached numlock modifier mask, refreshed before any key/button grab.
56 numlock_mask: c_uint, 57 numlock_mask: c_uint,
57 58
58 config: Config, 59 config: Config,
@@ -325,6 +326,91 @@ pub const WindowManager = struct {
325 pub fn window_to_bar(self: *WindowManager, win: xlib.Window) ?*Bar { 326 pub fn window_to_bar(self: *WindowManager, win: xlib.Window) ?*Bar {
326 return bar_mod.window_to_bar(self.bars, win); 327 return bar_mod.window_to_bar(self.bars, win);
327 } 328 }
329 +
330 + /// Refreshes the cached numlock modifier bitmask from the X server's
331 + /// current modifier map. Called before any key or button grab so that
332 + /// grabs cover all numlock combinations.
333 + pub fn update_numlock_mask(self: *WindowManager) void {
334 + self.numlock_mask = 0;
335 + const modmap = xlib.XGetModifierMapping(self.display.handle);
336 + if (modmap == null) return;
337 + defer _ = xlib.XFreeModifiermap(modmap);
338 +
339 + const numlock_keycode = xlib.XKeysymToKeycode(self.display.handle, xlib.XK_Num_Lock);
340 +
341 + var modifier_index: usize = 0;
342 + while (modifier_index < 8) : (modifier_index += 1) {
343 + var key_index: usize = 0;
344 + while (key_index < @as(usize, @intCast(modmap.*.max_keypermod))) : (key_index += 1) {
345 + const keycode = modmap.*.modifiermap[modifier_index * @as(usize, @intCast(modmap.*.max_keypermod)) + key_index];
346 + if (keycode == numlock_keycode) {
347 + self.numlock_mask = @as(c_uint, 1) << @intCast(modifier_index);
348 + }
349 + }
350 + }
351 + }
352 +
353 + /// Grabs all configured keybinds and mouse buttons from the X server.
354 + /// Replaces any existing grabs. Call after config load or reload.
355 + pub fn grab_keybinds(self: *WindowManager) void {
356 + self.update_numlock_mask();
357 + const modifiers = [_]c_uint{ 0, xlib.LockMask, self.numlock_mask, self.numlock_mask | xlib.LockMask };
358 +
359 + _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root);
360 +
361 + for (self.config.keybinds.items) |keybind| {
362 + if (keybind.key_count == 0) continue;
363 + const first_key = keybind.keys[0];
364 + const keycode = xlib.XKeysymToKeycode(self.display.handle, @intCast(first_key.keysym));
365 + if (keycode != 0) {
366 + for (modifiers) |modifier| {
367 + _ = xlib.XGrabKey(
368 + self.display.handle,
369 + keycode,
370 + first_key.mod_mask | modifier,
371 + self.display.root,
372 + xlib.True,
373 + xlib.GrabModeAsync,
374 + xlib.GrabModeAsync,
375 + );
376 + }
377 + }
378 + }
379 +
380 + for (self.config.buttons.items) |button| {
381 + if (button.click == .client_win) {
382 + for (modifiers) |modifier| {
383 + _ = xlib.XGrabButton(
384 + self.display.handle,
385 + @intCast(button.button),
386 + button.mod_mask | modifier,
387 + self.display.root,
388 + xlib.True,
389 + xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
390 + xlib.GrabModeAsync,
391 + xlib.GrabModeAsync,
392 + xlib.None,
393 + xlib.None,
394 + );
395 + }
396 + }
397 + }
398 +
399 + std.debug.print("grabbed {d} keybinds from config\n", .{self.config.keybinds.items.len});
400 + }
401 +
402 + pub fn ungrab_keybinds(self: *WindowManager) void {
403 + _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root);
404 + }
405 +
406 + pub fn rebuild_bar_blocks(self: *WindowManager) void {
407 + var current_bar = self.bars;
408 + while (current_bar) |bar| {
409 + bar.clear_blocks();
410 + self.populate_bar_blocks(bar);
411 + current_bar = bar.next;
412 + }
413 + }
328 }; 414 };
329 415
330 /// Converts a config block description into a live status bar block. 416 /// Converts a config block description into a live status bar block.