| @@ -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| { |