tony / oxwm

refactor(wm): move window scanning and event loop into `WindowManager` Browse files
emzywastaken committed 2026-02-22 parent efae070 commit a04b4ceeb11f4178496c683d85bef95c79e76c6e
Showing 2 changed files with 123 additions and 114 deletions. UnifiedSplit
+2 −113 src/main.zig
@@ -19,9 +19,6 @@ const WindowManager = wm_mod.WindowManager;
19 var gpa = std.heap.GeneralPurposeAllocator(.{}){}; 19 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
20 20
21 const NormalState: c_long = 1; 21 const NormalState: c_long = 1;
22 -const WithdrawnState: c_long = 0;
23 -const IconicState: c_long = 3;
24 -const IsViewable: c_int = 2;
25 const snap_distance: i32 = 32; 22 const snap_distance: i32 = 32;
26 23
27 /// File descriptor of the X11 connection, used in spawn_child_setup. 24 /// File descriptor of the X11 connection, used in spawn_child_setup.
@@ -200,11 +197,11 @@ pub fn main() !void {
200 std.debug.print("atoms initialized with EWMH support\n", .{}); 197 std.debug.print("atoms initialized with EWMH support\n", .{});
201 198
202 wm.grab_keybinds(); 199 wm.grab_keybinds();
203 - scan_existing_windows(&wm); 200 + wm.scan_existing_windows(manage);
204 201
205 try run_autostart_commands(allocator, wm.config.autostart.items); 202 try run_autostart_commands(allocator, wm.config.autostart.items);
206 std.debug.print("entering event loop\n", .{}); 203 std.debug.print("entering event loop\n", .{});
207 - run_event_loop(&wm); 204 + wm.run(handle_event, tick_animations);
208 205
209 lua.deinit(); 206 lua.deinit();
210 std.debug.print("oxwm exiting\n", .{}); 207 std.debug.print("oxwm exiting\n", .{});
@@ -270,114 +267,6 @@ fn initialize_default_config(cfg: *config_mod.Config) void {
270 cfg.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {}; 267 cfg.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {};
271 } 268 }
272 269
273 -fn get_state(window: xlib.Window, wm: *WindowManager) c_long {
274 - var actual_type: xlib.Atom = 0;
275 - var actual_format: c_int = 0;
276 - var num_items: c_ulong = 0;
277 - var bytes_after: c_ulong = 0;
278 - var prop: [*c]u8 = null;
279 -
280 - const result = xlib.XGetWindowProperty(
281 - wm.display.handle,
282 - window,
283 - wm.atoms.wm_state,
284 - 0,
285 - 2,
286 - xlib.False,
287 - wm.atoms.wm_state,
288 - &actual_type,
289 - &actual_format,
290 - &num_items,
291 - &bytes_after,
292 - &prop,
293 - );
294 -
295 - if (result != 0 or actual_type != wm.atoms.wm_state or num_items < 1) {
296 - if (prop != null) {
297 - _ = xlib.XFree(prop);
298 - }
299 - return WithdrawnState;
300 - }
301 -
302 - const state: c_long = @as(*c_long, @ptrCast(@alignCast(prop))).*;
303 - _ = xlib.XFree(prop);
304 - return state;
305 -}
306 -
307 -fn scan_existing_windows(wm: *WindowManager) void {
308 - var root_return: xlib.Window = undefined;
309 - var parent_return: xlib.Window = undefined;
310 - var children: [*c]xlib.Window = undefined;
311 - var num_children: c_uint = undefined;
312 -
313 - if (xlib.XQueryTree(wm.display.handle, wm.display.root, &root_return, &parent_return, &children, &num_children) == 0) {
314 - return;
315 - }
316 -
317 - var index: c_uint = 0;
318 - while (index < num_children) : (index += 1) {
319 - var window_attrs: xlib.XWindowAttributes = undefined;
320 - if (xlib.XGetWindowAttributes(wm.display.handle, children[index], &window_attrs) == 0) {
321 - continue;
322 - }
323 - if (window_attrs.override_redirect != 0) {
324 - continue;
325 - }
326 - var trans: xlib.Window = 0;
327 - if (xlib.XGetTransientForHint(wm.display.handle, children[index], &trans) != 0) {
328 - continue;
329 - }
330 - if (window_attrs.map_state == IsViewable or get_state(children[index], wm) == IconicState) {
331 - manage(children[index], &window_attrs, wm);
332 - }
333 - }
334 -
335 - index = 0;
336 - while (index < num_children) : (index += 1) {
337 - var window_attrs: xlib.XWindowAttributes = undefined;
338 - if (xlib.XGetWindowAttributes(wm.display.handle, children[index], &window_attrs) == 0) {
339 - continue;
340 - }
341 - var trans: xlib.Window = 0;
342 - if (xlib.XGetTransientForHint(wm.display.handle, children[index], &trans) != 0) {
343 - if (window_attrs.map_state == IsViewable or get_state(children[index], wm) == IconicState) {
344 - manage(children[index], &window_attrs, wm);
345 - }
346 - }
347 - }
348 -
349 - if (children != null) {
350 - _ = xlib.XFree(@ptrCast(children));
351 - }
352 -}
353 -
354 -fn run_event_loop(wm: *WindowManager) void {
355 - var fds = [_]std.posix.pollfd{
356 - .{ .fd = wm.x11_fd, .events = std.posix.POLL.IN, .revents = 0 },
357 - };
358 -
359 - _ = xlib.XSync(wm.display.handle, xlib.False);
360 -
361 - while (wm.running) {
362 - while (xlib.XPending(wm.display.handle) > 0) {
363 - var event = wm.display.next_event();
364 - handle_event(&event, wm);
365 - }
366 -
367 - tick_animations(wm);
368 -
369 - var current_bar = wm.bars;
370 - while (current_bar) |bar| {
371 - bar.update_blocks();
372 - bar.draw(wm.display.handle, wm.config);
373 - current_bar = bar.next;
374 - }
375 -
376 - const poll_timeout: i32 = if (wm.scroll_animation.is_active()) 16 else 1000;
377 - _ = std.posix.poll(&fds, poll_timeout) catch 0;
378 - }
379 -}
380 -
381 fn handle_event(event: *xlib.XEvent, wm: *WindowManager) void { 270 fn handle_event(event: *xlib.XEvent, wm: *WindowManager) void {
382 const event_type = events.get_event_type(event); 271 const event_type = events.get_event_type(event);
383 272
+121 −1 src/wm.zig
@@ -27,6 +27,12 @@ const Monitor = monitor_mod.Monitor;
27 const Bar = bar_mod.Bar; 27 const Bar = bar_mod.Bar;
28 const Config = config_mod.Config; 28 const Config = config_mod.Config;
29 29
30 +// Standard ICCCM WM_STATE values used by get_state and set_client_state.
31 +pub const NormalState: c_long = 1;
32 +pub const WithdrawnState: c_long = 0;
33 +pub const IconicState: c_long = 3;
34 +pub const IsViewable: c_int = 2;
35 +
30 pub const Cursors = struct { 36 pub const Cursors = struct {
31 normal: xlib.Cursor, 37 normal: xlib.Cursor,
32 resize: xlib.Cursor, 38 resize: xlib.Cursor,
@@ -99,7 +105,6 @@ pub const WindowManager = struct {
99 tiling.set_screen_size(display.screen_width(), display.screen_height()); 105 tiling.set_screen_size(display.screen_width(), display.screen_height());
100 106
101 monitor_mod.init(allocator); 107 monitor_mod.init(allocator);
102 - // client_mod.init(allocator);
103 108
104 var wm = WindowManager{ 109 var wm = WindowManager{
105 .allocator = allocator, 110 .allocator = allocator,
@@ -403,6 +408,121 @@ pub const WindowManager = struct {
403 _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root); 408 _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root);
404 } 409 }
405 410
411 + /// Walk the existing window tree and call `manage_fn` for each window
412 + /// that should be managed. Non-transient windows are processed first
413 + /// so transient (dialog) windows can be stacked on top.
414 + pub fn scan_existing_windows(self: *WindowManager, manage_fn: fn (xlib.Window, *xlib.XWindowAttributes, *WindowManager) void) void {
415 + var root_return: xlib.Window = undefined;
416 + var parent_return: xlib.Window = undefined;
417 + var children: [*c]xlib.Window = undefined;
418 + var num_children: c_uint = undefined;
419 +
420 + if (xlib.XQueryTree(self.display.handle, self.display.root, &root_return, &parent_return, &children, &num_children) == 0) {
421 + return;
422 + }
423 +
424 + var index: c_uint = 0;
425 + while (index < num_children) : (index += 1) {
426 + var window_attrs: xlib.XWindowAttributes = undefined;
427 + if (xlib.XGetWindowAttributes(self.display.handle, children[index], &window_attrs) == 0) {
428 + continue;
429 + }
430 + if (window_attrs.override_redirect != 0) continue;
431 +
432 + var trans: xlib.Window = 0;
433 + if (xlib.XGetTransientForHint(self.display.handle, children[index], &trans) != 0) continue;
434 +
435 + if (window_attrs.map_state == IsViewable or self.get_state(children[index]) == IconicState) {
436 + manage_fn(children[index], &window_attrs, self);
437 + }
438 + }
439 +
440 + index = 0;
441 + while (index < num_children) : (index += 1) {
442 + var window_attrs: xlib.XWindowAttributes = undefined;
443 + if (xlib.XGetWindowAttributes(self.display.handle, children[index], &window_attrs) == 0) {
444 + continue;
445 + }
446 + var trans: xlib.Window = 0;
447 + if (xlib.XGetTransientForHint(self.display.handle, children[index], &trans) != 0) {
448 + if (window_attrs.map_state == IsViewable or self.get_state(children[index]) == IconicState) {
449 + manage_fn(children[index], &window_attrs, self);
450 + }
451 + }
452 + }
453 +
454 + if (children != null) _ = xlib.XFree(@ptrCast(children));
455 + }
456 +
457 + /// Reads the ICCCM WM_STATE property for a window.
458 + /// Returns WithdrawnState if the property is absent or malformed.
459 + pub fn get_state(self: *WindowManager, window: xlib.Window) c_long {
460 + var actual_type: xlib.Atom = 0;
461 + var actual_format: c_int = 0;
462 + var num_items: c_ulong = 0;
463 + var bytes_after: c_ulong = 0;
464 + var prop: [*c]u8 = null;
465 +
466 + const result = xlib.XGetWindowProperty(
467 + self.display.handle,
468 + window,
469 + self.atoms.wm_state,
470 + 0,
471 + 2,
472 + xlib.False,
473 + self.atoms.wm_state,
474 + &actual_type,
475 + &actual_format,
476 + &num_items,
477 + &bytes_after,
478 + &prop,
479 + );
480 +
481 + if (result != 0 or actual_type != self.atoms.wm_state or num_items < 1) {
482 + if (prop != null) _ = xlib.XFree(prop);
483 + return WithdrawnState;
484 + }
485 +
486 + const state: c_long = @as(*c_long, @ptrCast(@alignCast(prop))).*;
487 + _ = xlib.XFree(prop);
488 + return state;
489 + }
490 +
491 + /// Run the event loop until `self.running` is set to false.
492 + ///
493 + /// `event_fn` dispatches a single XEvent
494 + /// `tick_fn` is called once per loop iteration to advance animations
495 + pub fn run(
496 + self: *WindowManager,
497 + event_fn: fn (*xlib.XEvent, *WindowManager) void,
498 + tick_fn: fn (*WindowManager) void,
499 + ) void {
500 + var fds = [_]std.posix.pollfd{
501 + .{ .fd = self.x11_fd, .events = std.posix.POLL.IN, .revents = 0 },
502 + };
503 +
504 + _ = xlib.XSync(self.display.handle, xlib.False);
505 +
506 + while (self.running) {
507 + while (xlib.XPending(self.display.handle) > 0) {
508 + var event = self.display.next_event();
509 + event_fn(&event, self);
510 + }
511 +
512 + tick_fn(self);
513 +
514 + var current_bar = self.bars;
515 + while (current_bar) |bar| {
516 + bar.update_blocks();
517 + bar.draw(self.display.handle, self.config);
518 + current_bar = bar.next;
519 + }
520 +
521 + const poll_timeout: i32 = if (self.scroll_animation.is_active()) 16 else 1000;
522 + _ = std.posix.poll(&fds, poll_timeout) catch 0;
523 + }
524 + }
525 +
406 pub fn rebuild_bar_blocks(self: *WindowManager) void { 526 pub fn rebuild_bar_blocks(self: *WindowManager) void {
407 var current_bar = self.bars; 527 var current_bar = self.bars;
408 while (current_bar) |bar| { 528 while (current_bar) |bar| {