tony / oxwm

Branch: efae070
Switch branches/tags
master
emzywastaken refactor(wm): move keybind, numlock and bar helpers into `WindowManager` methods efae070 2026-02-22
452 lines · 15.5 KB Raw Blame History
1 const std = @import("std");
2 const mem = std.mem;
3
4 const display_mod = @import("x11/display.zig");
5 const xlib = @import("x11/xlib.zig");
6 const events = @import("x11/events.zig");
7 const atoms_mod = @import("x11/atoms.zig");
8 const chord_mod = @import("keyboard/chord.zig");
9 const client_mod = @import("client.zig");
10 const monitor_mod = @import("monitor.zig");
11 const bar_mod = @import("bar/bar.zig");
12 const blocks_mod = @import("bar/blocks/blocks.zig");
13 const config_mod = @import("config/config.zig");
14 const overlay_mod = @import("overlay.zig");
15 const animations = @import("animations.zig");
16 const tiling = @import("layouts/tiling.zig");
17 const monocle = @import("layouts/monocle.zig");
18 const floating = @import("layouts/floating.zig");
19 const scrolling = @import("layouts/scrolling.zig");
20 const grid = @import("layouts/grid.zig");
21
22 const Display = display_mod.Display;
23 const Atoms = atoms_mod.Atoms;
24 const ChordState = chord_mod.ChordState;
25 const Client = client_mod.Client;
26 const Monitor = monitor_mod.Monitor;
27 const Bar = bar_mod.Bar;
28 const Config = config_mod.Config;
29
30 pub const Cursors = struct {
31 normal: xlib.Cursor,
32 resize: xlib.Cursor,
33 move: xlib.Cursor,
34
35 pub fn init(display: *Display) Cursors {
36 return .{
37 .normal = xlib.XCreateFontCursor(display.handle, xlib.XC_left_ptr),
38 .resize = xlib.XCreateFontCursor(display.handle, xlib.XC_sizing),
39 .move = xlib.XCreateFontCursor(display.handle, xlib.XC_fleur),
40 };
41 }
42 };
43
44 pub const WindowManager = struct {
45 allocator: mem.Allocator,
46
47 /// The connection to the X server.
48 display: Display,
49 x11_fd: c_int,
50 /// Invisible 1×1 window used to satisfy the _NET_SUPPORTING_WM_CHECK
51 /// EWMH convention.
52 wm_check_window: xlib.Window,
53
54 atoms: Atoms,
55 cursors: Cursors,
56 /// Cached numlock modifier mask, refreshed before any key/button grab.
57 numlock_mask: c_uint,
58
59 config: Config,
60 /// Path to the config file that was loaded.
61 /// Null if using default config.
62 config_path: ?[]const u8,
63
64 /// Head of the linked list of all managed monitors.
65 monitors: ?*Monitor,
66 /// The monitor that currently has input focus.
67 selected_monitor: ?*Monitor,
68
69 /// Head of the linked list of status bars (one per monitor).
70 bars: ?*Bar,
71
72 chord: ChordState,
73
74 overlay: ?*overlay_mod.Keybind_Overlay,
75
76 scroll_animation: animations.Scroll_Animation,
77 animation_config: animations.Animation_Config,
78
79 running: bool,
80 last_motion_monitor: ?*Monitor,
81
82 /// Initialises the window manager
83 ///
84 /// Returns an error if the display cannot be opened or another WM is
85 /// already running.
86 pub fn init(allocator: mem.Allocator, config: Config, config_path: ?[]const u8) !WindowManager {
87 var display = try Display.open();
88 errdefer display.close();
89
90 try display.become_window_manager();
91
92 const x11_fd = xlib.XConnectionNumber(display.handle);
93
94 const atoms_result = Atoms.init(display.handle, display.root);
95 const cursors = Cursors.init(&display);
96 _ = xlib.XDefineCursor(display.handle, display.root, cursors.normal);
97
98 tiling.set_display(display.handle);
99 tiling.set_screen_size(display.screen_width(), display.screen_height());
100
101 monitor_mod.init(allocator);
102 // client_mod.init(allocator);
103
104 var wm = WindowManager{
105 .allocator = allocator,
106 .display = display,
107 .x11_fd = x11_fd,
108 .wm_check_window = atoms_result.check_window,
109 .atoms = atoms_result.atoms,
110 .cursors = cursors,
111 .numlock_mask = 0,
112 .config = config,
113 .config_path = config_path,
114 .monitors = null,
115 .selected_monitor = null,
116 .bars = null,
117 .chord = .{},
118 .overlay = null,
119 .scroll_animation = .{},
120 .animation_config = .{ .duration_ms = 150, .easing = .ease_out },
121 .running = true,
122 .last_motion_monitor = null,
123 };
124
125 wm.setup_monitors();
126 wm.setup_bars();
127 wm.setup_overlay();
128
129 return wm;
130 }
131
132 /// Release all allocated memory owned by the WM.
133 pub fn deinit(self: *WindowManager) void {
134 bar_mod.destroy_bars(self.bars, self.allocator, self.display.handle);
135 self.bars = null;
136
137 if (self.overlay) |o| {
138 o.deinit(self.allocator);
139 self.overlay = null;
140 }
141
142 var mon = self.monitors;
143 while (mon) |m| {
144 const next = m.next;
145 monitor_mod.destroy(m);
146 mon = next;
147 }
148 self.monitors = null;
149 self.selected_monitor = null;
150
151 _ = xlib.XDestroyWindow(self.display.handle, self.wm_check_window);
152
153 self.display.close();
154 self.config.deinit();
155 }
156
157 fn setup_monitors(self: *WindowManager) void {
158 if (xlib.XineramaIsActive(self.display.handle) != 0) {
159 var screen_count: c_int = 0;
160 const screens = xlib.XineramaQueryScreens(self.display.handle, &screen_count);
161
162 if (screen_count > 0 and screens != null) {
163 var prev_monitor: ?*Monitor = null;
164 var index: usize = 0;
165
166 while (index < @as(usize, @intCast(screen_count))) : (index += 1) {
167 const screen = screens[index];
168 const mon = monitor_mod.create() orelse continue;
169
170 mon.num = @intCast(index);
171 mon.mon_x = screen.x_org;
172 mon.mon_y = screen.y_org;
173 mon.mon_w = screen.width;
174 mon.mon_h = screen.height;
175 mon.win_x = screen.x_org;
176 mon.win_y = screen.y_org;
177 mon.win_w = screen.width;
178 mon.win_h = screen.height;
179 mon.lt[0] = &tiling.layout;
180 mon.lt[1] = &monocle.layout;
181 mon.lt[2] = &floating.layout;
182 mon.lt[3] = &scrolling.layout;
183 mon.lt[4] = &grid.layout;
184
185 for (0..10) |i| {
186 mon.pertag.ltidxs[i][0] = mon.lt[0];
187 mon.pertag.ltidxs[i][1] = mon.lt[1];
188 mon.pertag.ltidxs[i][2] = mon.lt[2];
189 mon.pertag.ltidxs[i][3] = mon.lt[3];
190 mon.pertag.ltidxs[i][4] = mon.lt[4];
191 }
192
193 self.init_monitor_gaps(mon);
194
195 if (prev_monitor) |prev| {
196 prev.next = mon;
197 } else {
198 self.monitors = mon;
199 self.selected_monitor = mon;
200 }
201 prev_monitor = mon;
202 }
203
204 _ = xlib.XFree(@ptrCast(screens));
205 }
206 }
207
208 // Fallback: single monitor covering the full screen.
209 if (self.monitors == null) {
210 const mon = monitor_mod.create() orelse return;
211 mon.num = 0;
212 mon.mon_x = 0;
213 mon.mon_y = 0;
214 mon.mon_w = self.display.screen_width();
215 mon.mon_h = self.display.screen_height();
216 mon.win_x = 0;
217 mon.win_y = 0;
218 mon.win_w = mon.mon_w;
219 mon.win_h = mon.mon_h;
220 mon.lt[0] = &tiling.layout;
221 mon.lt[1] = &monocle.layout;
222 mon.lt[2] = &floating.layout;
223 mon.lt[3] = &scrolling.layout;
224 mon.lt[4] = &grid.layout;
225
226 for (0..10) |i| {
227 mon.pertag.ltidxs[i][0] = mon.lt[0];
228 mon.pertag.ltidxs[i][1] = mon.lt[1];
229 mon.pertag.ltidxs[i][2] = mon.lt[2];
230 mon.pertag.ltidxs[i][3] = mon.lt[3];
231 mon.pertag.ltidxs[i][4] = mon.lt[4];
232 }
233
234 self.init_monitor_gaps(mon);
235 self.monitors = mon;
236 self.selected_monitor = mon;
237 }
238
239 // Mirror into monitor_mod so legacy code that still reads the
240 // module-level vars continues to work during the transition.
241 // TODO(wm-refactor): remove once all callers use self.monitors.
242 monitor_mod.monitors = self.monitors;
243 monitor_mod.selected_monitor = self.selected_monitor;
244 }
245
246 fn init_monitor_gaps(self: *WindowManager, mon: *Monitor) void {
247 const cfg = &self.config;
248 const any_gap_nonzero = cfg.gap_inner_h != 0 or cfg.gap_inner_v != 0 or
249 cfg.gap_outer_h != 0 or cfg.gap_outer_v != 0;
250
251 if (cfg.gaps_enabled and any_gap_nonzero) {
252 mon.gap_inner_h = cfg.gap_inner_h;
253 mon.gap_inner_v = cfg.gap_inner_v;
254 mon.gap_outer_h = cfg.gap_outer_h;
255 mon.gap_outer_v = cfg.gap_outer_v;
256 } else {
257 mon.gap_inner_h = 0;
258 mon.gap_inner_v = 0;
259 mon.gap_outer_h = 0;
260 mon.gap_outer_v = 0;
261 }
262 }
263
264 pub fn setup_bars(self: *WindowManager) void {
265 var current_monitor = self.monitors;
266 var last_bar: ?*Bar = null;
267
268 while (current_monitor) |monitor| {
269 const bar = Bar.create(
270 self.allocator,
271 self.display.handle,
272 self.display.screen,
273 monitor,
274 self.config,
275 ) orelse {
276 current_monitor = monitor.next;
277 continue;
278 };
279
280 if (tiling.bar_height == 0) {
281 tiling.set_bar_height(bar.height);
282 }
283
284 self.populate_bar_blocks(bar);
285
286 if (last_bar) |prev| {
287 prev.next = bar;
288 } else {
289 self.bars = bar;
290 }
291 last_bar = bar;
292
293 std.debug.print("bar created for monitor {d}\n", .{monitor.num});
294 current_monitor = monitor.next;
295 }
296 }
297
298 pub fn populate_bar_blocks(self: *WindowManager, bar: *Bar) void {
299 if (self.config.blocks.items.len > 0) {
300 for (self.config.blocks.items) |cfg_block| {
301 bar.add_block(config_block_to_bar_block(cfg_block));
302 }
303 } else {
304 bar.add_block(blocks_mod.Block.init_ram("", 5, 0x7aa2f7, true));
305 bar.add_block(blocks_mod.Block.init_static(" | ", 0x666666, false));
306 bar.add_block(blocks_mod.Block.init_datetime("", "%H:%M", 1, 0x0db9d7, true));
307 }
308 }
309
310 fn setup_overlay(self: *WindowManager) void {
311 self.overlay = overlay_mod.Keybind_Overlay.init(
312 self.display.handle,
313 self.display.screen,
314 self.display.root,
315 self.config.font,
316 self.allocator,
317 );
318 }
319
320 // Wrap free functions in `bar_mod` with `self.bars` passed in.
321
322 pub fn invalidate_bars(self: *WindowManager) void {
323 bar_mod.invalidate_bars(self.bars);
324 }
325
326 pub fn window_to_bar(self: *WindowManager, win: xlib.Window) ?*Bar {
327 return bar_mod.window_to_bar(self.bars, win);
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 }
414 };
415
416 /// Converts a config block description into a live status bar block.
417 pub fn config_block_to_bar_block(cfg: config_mod.Block) blocks_mod.Block {
418 return switch (cfg.block_type) {
419 .static => blocks_mod.Block.init_static(cfg.format, cfg.color, cfg.underline),
420 .datetime => blocks_mod.Block.init_datetime(
421 cfg.format,
422 cfg.datetime_format orelse "%H:%M",
423 cfg.interval,
424 cfg.color,
425 cfg.underline,
426 ),
427 .ram => blocks_mod.Block.init_ram(cfg.format, cfg.interval, cfg.color, cfg.underline),
428 .shell => blocks_mod.Block.init_shell(
429 cfg.format,
430 cfg.command orelse "",
431 cfg.interval,
432 cfg.color,
433 cfg.underline,
434 ),
435 .battery => blocks_mod.Block.init_battery(
436 cfg.format_charging orelse "",
437 cfg.format_discharging orelse "",
438 cfg.format_full orelse "",
439 cfg.battery_name orelse "BAT0",
440 cfg.interval,
441 cfg.color,
442 cfg.underline,
443 ),
444 .cpu_temp => blocks_mod.Block.init_cpu_temp(
445 cfg.format,
446 cfg.thermal_zone orelse "thermal_zone0",
447 cfg.interval,
448 cfg.color,
449 cfg.underline,
450 ),
451 };
452 }