tony / oxwm

Branch: 86288f6
Switch branches/tags
master
tonybanters updated for scrolling with gestures, and size of windows in scrolling layout. b419fcd 2026-09-20
864 lines · 31 KB Raw Blame History
1 const std = @import("std");
2 const xlib = @import("../x11/xlib.zig");
3 const client_mod = @import("../client.zig");
4 const monitor_mod = @import("../monitor.zig");
5 const tiling = @import("../layouts/tiling.zig");
6 const scrolling = @import("../layouts/scrolling.zig");
7 const gesture_mod = @import("../input/gesture.zig");
8 const window_manager = @import("wm.zig");
9
10 const config_mod = @import("../config/config.zig");
11
12 const Client = client_mod.Client;
13 const Monitor = monitor_mod.Monitor;
14 const WindowManager = window_manager.WindowManager;
15
16 pub const NormalState = window_manager.NormalState;
17
18 pub fn manage(win: xlib.Window, window_attrs: *xlib.XWindowAttributes, wm: *WindowManager) void {
19 const client = client_mod.create(wm.allocator, win) orelse return;
20 var trans: xlib.Window = 0;
21
22 client.x = window_attrs.x;
23 client.y = window_attrs.y;
24 client.width = window_attrs.width;
25 client.height = window_attrs.height;
26 client.old_x = window_attrs.x;
27 client.old_y = window_attrs.y;
28 client.old_width = window_attrs.width;
29 client.old_height = window_attrs.height;
30 client.old_border_width = window_attrs.border_width;
31 client.border_width = wm.config.border_width;
32
33 updateTitle(client, wm);
34
35 if (xlib.XGetTransientForHint(wm.display.handle, win, &trans) != 0) {
36 if (client_mod.windowToClient(wm.monitors, trans)) |transient_client| {
37 client.monitor = transient_client.monitor;
38 client.tags = transient_client.tags;
39 }
40 }
41
42 if (client.monitor == null) {
43 client.monitor = wm.selected_monitor;
44 if (wm.next_spawn_bypass_rules) {
45 if (client.monitor) |monitor| {
46 client.tags = monitor.tagset[monitor.sel_tags];
47 }
48 wm.next_spawn_bypass_rules = false;
49 } else {
50 applyRules(client, wm);
51 }
52 }
53
54 if (wm.next_spawn_floating) {
55 client.is_floating = true;
56 wm.next_spawn_floating = false;
57 }
58
59 const monitor = client.monitor orelse return;
60
61 if (client.x + client.width > monitor.win_x + monitor.win_w) {
62 client.x = monitor.win_x + monitor.win_w - client.width - 2 * client.border_width;
63 }
64 if (client.y + client.height > monitor.win_y + monitor.win_h) {
65 client.y = monitor.win_y + monitor.win_h - client.height - 2 * client.border_width;
66 }
67 client.x = @max(client.x, monitor.win_x);
68 client.y = @max(client.y, monitor.win_y);
69
70 _ = xlib.XSetWindowBorderWidth(wm.display.handle, win, @intCast(client.border_width));
71 _ = xlib.XSetWindowBorder(wm.display.handle, win, wm.config.border_unfocused);
72 tiling.sendConfigure(client);
73
74 updateWindowType(client, wm);
75 updateSizeHints(client, wm);
76 updateWmHints(client, wm);
77
78 _ = xlib.XSelectInput(
79 wm.display.handle,
80 win,
81 xlib.EnterWindowMask | xlib.FocusChangeMask | xlib.PropertyChangeMask | xlib.StructureNotifyMask,
82 );
83 grabbuttons(client, false, wm);
84
85 if (!client.is_floating) {
86 client.is_floating = trans != 0 or client.is_fixed;
87 client.old_state = client.is_floating;
88 }
89 if (client.is_floating) {
90 positionFloating(client, monitor, wm.config.floating_position);
91 _ = xlib.XRaiseWindow(wm.display.handle, client.window);
92 }
93
94 client_mod.attachWith(client, wm.config.attach_method);
95 client_mod.attachStack(client);
96
97 _ = xlib.XChangeProperty(wm.display.handle, wm.display.root, wm.atoms.net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
98 _ = xlib.XMoveResizeWindow(wm.display.handle, client.window, client.x + 2 * wm.display.screenWidth(), client.y, @intCast(client.width), @intCast(client.height));
99 setClientState(client, NormalState, wm);
100
101 if (client.monitor == wm.selected_monitor) {
102 const selmon = wm.selected_monitor orelse return;
103 unfocusClient(selmon.sel, false, wm);
104 }
105 monitor.sel = client;
106
107 if (isScrollingLayout(monitor)) {
108 scrollToWindow(client, true, wm);
109 }
110
111 arrange(monitor, wm);
112 _ = xlib.XMapWindow(wm.display.handle, win);
113 focus(null, wm);
114 }
115
116 pub fn unmanage(client: *Client, wm: *WindowManager) void {
117 const client_monitor = client.monitor;
118
119 var next_focus: ?*Client = null;
120 if (client_monitor) |monitor| {
121 if (monitor.sel == client and isScrollingLayout(monitor)) {
122 next_focus = client.next;
123 if (next_focus == null) {
124 var prev: ?*Client = null;
125 var iter = monitor.clients;
126 while (iter) |c| {
127 if (c == client) break;
128 prev = c;
129 iter = c.next;
130 }
131 next_focus = prev;
132 }
133 }
134 }
135
136 client_mod.detach(client);
137 client_mod.detachStack(client);
138
139 if (client_monitor) |monitor| {
140 if (monitor.sel == client) {
141 monitor.sel = if (next_focus) |nf| nf else monitor.stack;
142 }
143 if (isScrollingLayout(monitor)) {
144 if (wm.animation_monitor == monitor) wm.scroll_animation.stop();
145 monitor.scroll_offset = @max(0, @min(monitor.scroll_offset, scrolling.maxScroll(monitor)));
146 if (monitor.sel) |sel| scrollToWindow(sel, true, wm);
147 }
148 arrange(monitor, wm);
149 }
150
151 if (client_monitor) |monitor| {
152 if (monitor.sel) |selected| {
153 focus(selected, wm);
154 }
155 }
156
157 client_mod.destroy(wm.allocator, client);
158 updateClientList(wm);
159 wm.invalidateBars();
160 }
161
162 pub fn focus(target_client: ?*Client, wm: *WindowManager) void {
163 const selmon = wm.selected_monitor orelse return;
164
165 var focus_client = target_client;
166 if (focus_client == null or !client_mod.isVisible(focus_client.?)) {
167 focus_client = selmon.stack;
168 while (focus_client) |iter| {
169 if (client_mod.isVisible(iter)) break;
170 focus_client = iter.stack_next;
171 }
172 }
173
174 if (selmon.sel != null and selmon.sel != focus_client) {
175 unfocusClient(selmon.sel, false, wm);
176 }
177
178 if (focus_client) |client| {
179 if (client.monitor != selmon) {
180 wm.selected_monitor = client.monitor;
181 }
182 if (client.is_urgent) {
183 setUrgent(client, false, wm);
184 }
185 client_mod.detachStack(client);
186 client_mod.attachStack(client);
187 grabbuttons(client, true, wm);
188 _ = xlib.XSetWindowBorder(wm.display.handle, client.window, wm.config.border_focused);
189 if (!client.never_focus) {
190 _ = xlib.XSetInputFocus(wm.display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime);
191 _ = xlib.XChangeProperty(wm.display.handle, wm.display.root, wm.atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
192 }
193 _ = sendEvent(client, wm.atoms.wm_take_focus, wm);
194 } else {
195 _ = xlib.XSetInputFocus(wm.display.handle, wm.display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
196 _ = xlib.XDeleteProperty(wm.display.handle, wm.display.root, wm.atoms.net_active_window);
197 }
198
199 const current_selmon = wm.selected_monitor orelse return;
200 current_selmon.sel = focus_client;
201
202 if (focus_client) |client| {
203 if (isScrollingLayout(current_selmon)) {
204 scrollToWindow(client, true, wm);
205 }
206 }
207
208 wm.invalidateBars();
209 }
210
211 pub fn unfocusClient(client: ?*Client, reset_input_focus: bool, wm: *WindowManager) void {
212 const unfocus_target = client orelse return;
213 grabbuttons(unfocus_target, false, wm);
214 _ = xlib.XSetWindowBorder(wm.display.handle, unfocus_target.window, wm.config.border_unfocused);
215 if (reset_input_focus) {
216 _ = xlib.XSetInputFocus(wm.display.handle, wm.display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
217 _ = xlib.XDeleteProperty(wm.display.handle, wm.display.root, wm.atoms.net_active_window);
218 }
219 }
220
221 pub fn setFocus(client: *Client, wm: *WindowManager) void {
222 if (!client.never_focus) {
223 _ = xlib.XSetInputFocus(wm.display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime);
224 _ = xlib.XChangeProperty(wm.display.handle, wm.display.root, wm.atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
225 }
226 _ = sendEvent(client, wm.atoms.wm_take_focus, wm);
227 }
228
229 pub fn restack(monitor: *Monitor, wm: *WindowManager) void {
230 wm.invalidateBars();
231 const selected_client = monitor.sel orelse return;
232
233 if (selected_client.is_floating or monitor.lt[monitor.sel_lt] == null) {
234 _ = xlib.XRaiseWindow(wm.display.handle, selected_client.window);
235 }
236
237 if (monitor.lt[monitor.sel_lt] != null) {
238 var window_changes: xlib.c.XWindowChanges = undefined;
239 window_changes.stack_mode = xlib.c.Below;
240 window_changes.sibling = monitor.bar_win;
241
242 var current = monitor.stack;
243 while (current) |client| {
244 if (!client.is_floating and client_mod.isVisible(client)) {
245 _ = xlib.c.XConfigureWindow(wm.display.handle, client.window, xlib.c.CWSibling | xlib.c.CWStackMode, &window_changes);
246 window_changes.sibling = client.window;
247 }
248 current = client.stack_next;
249 }
250 }
251
252 _ = xlib.XSync(wm.display.handle, xlib.False);
253
254 var discard_event: xlib.XEvent = undefined;
255 while (xlib.c.XCheckMaskEvent(wm.display.handle, xlib.EnterWindowMask, &discard_event) != 0) {}
256 }
257
258 pub fn arrange(monitor: *Monitor, wm: *WindowManager) void {
259 showhide(monitor, wm);
260 if (isScrollingLayout(monitor) and !scrollInMotion(monitor, wm)) {
261 var offset: i32 = @max(0, @min(monitor.scroll_offset, scrolling.maxScroll(monitor)));
262 if (monitor.sel) |sel| {
263 if (!sel.is_floating and client_mod.isVisible(sel)) {
264 offset = scrolling.offsetToReveal(monitor, offset, sel);
265 }
266 }
267 monitor.scroll_offset = offset;
268 }
269 if (monitor.lt[monitor.sel_lt]) |layout| {
270 if (layout.arrange_fn) |arrange_fn| {
271 arrange_fn(monitor);
272 }
273 }
274 restack(monitor, wm);
275 }
276
277 pub fn showhide(monitor: *Monitor, wm: *WindowManager) void {
278 showhideClient(monitor.stack, wm);
279 }
280
281 pub fn showhideClient(client: ?*Client, wm: *WindowManager) void {
282 const target = client orelse return;
283 if (client_mod.isVisible(target)) {
284 _ = xlib.XMoveWindow(wm.display.handle, target.window, target.x, target.y);
285 const monitor = target.monitor orelse return;
286 if ((monitor.lt[monitor.sel_lt] == null or target.is_floating) and !target.is_fullscreen) {
287 tiling.resize(target, target.x, target.y, target.width, target.height, false);
288 }
289 showhideClient(target.stack_next, wm);
290 } else {
291 showhideClient(target.stack_next, wm);
292 const client_width = target.width + 2 * target.border_width;
293 _ = xlib.XMoveWindow(wm.display.handle, target.window, -2 * client_width, target.y);
294 }
295 }
296
297 pub fn grabbuttons(client: *Client, focused: bool, wm: *WindowManager) void {
298 wm.updateNumlockMask();
299 const modifiers = [_]c_uint{ 0, xlib.LockMask, wm.numlock_mask, wm.numlock_mask | xlib.LockMask };
300
301 _ = xlib.XUngrabButton(wm.display.handle, xlib.AnyButton, xlib.AnyModifier, client.window);
302 if (!focused) {
303 _ = xlib.XGrabButton(
304 wm.display.handle,
305 xlib.AnyButton,
306 xlib.AnyModifier,
307 client.window,
308 xlib.False,
309 xlib.ButtonPressMask | xlib.ButtonReleaseMask,
310 xlib.GrabModeSync,
311 xlib.GrabModeSync,
312 xlib.None,
313 xlib.None,
314 );
315 }
316 for (wm.config.buttons.items) |button| {
317 if (button.click == .client_win) {
318 for (modifiers) |modifier| {
319 _ = xlib.XGrabButton(
320 wm.display.handle,
321 @intCast(button.button),
322 button.mod_mask | modifier,
323 client.window,
324 xlib.False,
325 xlib.ButtonPressMask | xlib.ButtonReleaseMask,
326 xlib.GrabModeAsync,
327 xlib.GrabModeSync,
328 xlib.None,
329 xlib.None,
330 );
331 }
332 }
333 }
334 }
335
336 pub fn setClientState(client: *Client, state: c_long, wm: *WindowManager) void {
337 var data: [2]c_long = .{ state, xlib.None };
338 _ = xlib.c.XChangeProperty(wm.display.handle, client.window, wm.atoms.wm_state, xlib.XA_ATOM, 32, xlib.PropModeReplace, @ptrCast(&data), 2);
339 }
340
341 pub fn setFullscreen(client: *Client, fullscreen: bool, wm: *WindowManager) void {
342 const monitor = client.monitor orelse return;
343
344 if (fullscreen and !client.is_fullscreen) {
345 var fullscreen_atom = wm.atoms.net_wm_state_fullscreen;
346 _ = xlib.XChangeProperty(
347 wm.display.handle,
348 client.window,
349 wm.atoms.net_wm_state,
350 xlib.XA_ATOM,
351 32,
352 xlib.PropModeReplace,
353 @ptrCast(&fullscreen_atom),
354 1,
355 );
356 client.is_fullscreen = true;
357 client.old_state = client.is_floating;
358 client.old_border_width = client.border_width;
359 client.border_width = 0;
360 client.is_floating = true;
361
362 _ = xlib.XSetWindowBorderWidth(wm.display.handle, client.window, 0);
363 tiling.resizeClient(client, monitor.mon_x, monitor.mon_y, monitor.mon_w, monitor.mon_h);
364 _ = xlib.XRaiseWindow(wm.display.handle, client.window);
365
366 std.debug.print("fullscreen enabled: window=0x{x}\n", .{client.window});
367 } else if (!fullscreen and client.is_fullscreen) {
368 var no_atom: xlib.Atom = 0;
369 _ = xlib.XChangeProperty(
370 wm.display.handle,
371 client.window,
372 wm.atoms.net_wm_state,
373 xlib.XA_ATOM,
374 32,
375 xlib.PropModeReplace,
376 @ptrCast(&no_atom),
377 0,
378 );
379 client.is_fullscreen = false;
380 client.is_floating = client.old_state;
381 client.border_width = client.old_border_width;
382
383 client.x = client.old_x;
384 client.y = client.old_y;
385 client.width = client.old_width;
386 client.height = client.old_height;
387
388 tiling.resizeClient(client, client.x, client.y, client.width, client.height);
389 arrange(monitor, wm);
390
391 std.debug.print("fullscreen disabled: window=0x{x}\n", .{client.window});
392 }
393 }
394
395 pub fn focusTopClient(monitor: *Monitor, wm: *WindowManager) void {
396 var visible_client = monitor.stack;
397 while (visible_client) |client| {
398 if (client_mod.isVisible(client)) {
399 focus(client, wm);
400 return;
401 }
402 visible_client = client.stack_next;
403 }
404 monitor.sel = null;
405 _ = xlib.XSetInputFocus(wm.display.handle, wm.display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
406 }
407
408 pub fn tickAnimations(wm: *WindowManager) void {
409 if (!wm.scroll_animation.isActive()) return;
410
411 const monitor = wm.animation_monitor orelse return;
412 if (wm.scroll_animation.update(wm.io)) |new_offset| {
413 monitor.scroll_offset = new_offset;
414 arrange(monitor, wm);
415 }
416 }
417
418 pub fn isScrollingLayout(monitor: *Monitor) bool {
419 if (monitor.lt[monitor.sel_lt]) |layout| {
420 return layout.arrange_fn == scrolling.layout.arrange_fn;
421 }
422 return false;
423 }
424
425 fn animationActiveOn(monitor: *Monitor, wm: *WindowManager) bool {
426 return wm.scroll_animation.isActive() and wm.animation_monitor == monitor;
427 }
428
429 fn scrollInMotion(monitor: *Monitor, wm: *WindowManager) bool {
430 return animationActiveOn(monitor, wm) or (wm.gesture.active and wm.gesture.monitor == monitor);
431 }
432
433 fn scrollTarget(monitor: *Monitor, wm: *WindowManager) i32 {
434 return if (animationActiveOn(monitor, wm)) wm.scroll_animation.target() else monitor.scroll_offset;
435 }
436
437 fn startScrollAnimation(monitor: *Monitor, target: i32, wm: *WindowManager) void {
438 wm.animation_monitor = monitor;
439 wm.scroll_animation.start(wm.io, monitor.scroll_offset, target, wm.animation_config);
440 }
441
442 pub fn scrollLayout(direction: i32, wm: *WindowManager) void {
443 const monitor = wm.selected_monitor orelse return;
444 if (!isScrollingLayout(monitor) or wm.gesture.active) return;
445
446 const current = scrollTarget(monitor, wm);
447 const snap = scrolling.adjacentSnap(monitor, current, direction) orelse return;
448
449 focus(snap.client, wm);
450 startScrollAnimation(monitor, snap.offset, wm);
451 }
452
453 pub fn scrollToWindow(client: *Client, animate: bool, wm: *WindowManager) void {
454 const monitor = client.monitor orelse return;
455 if (!isScrollingLayout(monitor)) return;
456 if (wm.gesture.active and wm.gesture.monitor == monitor) return;
457
458 const base = scrollTarget(monitor, wm);
459 const target = scrolling.offsetToReveal(monitor, base, client);
460
461 if (animate) {
462 if (target == base and animationActiveOn(monitor, wm)) return;
463 startScrollAnimation(monitor, target, wm);
464 } else {
465 monitor.scroll_offset = target;
466 arrange(monitor, wm);
467 }
468 }
469
470 fn nowMs(wm: *WindowManager) i64 {
471 return std.Io.Timestamp.now(wm.io, .awake).toMilliseconds();
472 }
473
474 pub fn handleGesture(wm: *WindowManager, event: gesture_mod.Event) void {
475 switch (event.kind) {
476 .begin => gestureBegin(event, wm),
477 .update => gestureUpdate(event, wm),
478 .end => gestureEnd(event, wm),
479 }
480 }
481
482 fn gestureBegin(event: gesture_mod.Event, wm: *WindowManager) void {
483 if (event.fingers != wm.config.gesture_fingers) return;
484 const monitor = wm.selected_monitor orelse return;
485 if (!isScrollingLayout(monitor)) return;
486
487 if (animationActiveOn(monitor, wm)) wm.scroll_animation.stop();
488
489 wm.gesture = .{
490 .active = true,
491 .monitor = monitor,
492 .position = @floatFromInt(monitor.scroll_offset),
493 };
494 }
495
496 fn gestureUpdate(event: gesture_mod.Event, wm: *WindowManager) void {
497 if (!wm.gesture.active) return;
498 const monitor = wm.gesture.monitor orelse return;
499 if (!isScrollingLayout(monitor)) {
500 wm.gesture.active = false;
501 return;
502 }
503
504 const sign: f64 = if (wm.config.gesture_natural) -1.0 else 1.0;
505 const delta = event.dx * @as(f64, wm.config.gesture_speed) * sign;
506
507 const geo = scrolling.geometry(monitor);
508 const min_position: f64 = @floatFromInt(-geo.available_w);
509 const max_position: f64 = @floatFromInt(scrolling.contentWidth(monitor));
510
511 wm.gesture.position = @max(min_position, @min(wm.gesture.position + delta, max_position));
512 wm.gesture.push(delta, nowMs(wm));
513
514 monitor.scroll_offset = @intFromFloat(@round(wm.gesture.position));
515 arrange(monitor, wm);
516 }
517
518 fn gestureEnd(event: gesture_mod.Event, wm: *WindowManager) void {
519 _ = event;
520 if (!wm.gesture.active) return;
521 wm.gesture.active = false;
522 const monitor = wm.gesture.monitor orelse return;
523 if (!isScrollingLayout(monitor)) return;
524
525 const velocity = wm.gesture.velocity(nowMs(wm));
526 const deceleration = 1000.0 * @log(window_manager.gesture_deceleration);
527 const projected = wm.gesture.position - velocity / deceleration;
528
529 const snap = scrolling.nearestSnap(monitor, projected) orelse {
530 arrange(monitor, wm);
531 return;
532 };
533
534 focus(snap.client, wm);
535 wm.animation_monitor = monitor;
536 wm.scroll_animation.startSpring(wm.io, monitor.scroll_offset, snap.offset, velocity, .{});
537 if (!wm.scroll_animation.isActive()) arrange(monitor, wm);
538 }
539
540 fn positionFloating(client: *Client, monitor: *Monitor, pos: config_mod.FloatingPosition) void {
541 const bw = 2 * client.border_width;
542 const x: i32 = switch (pos) {
543 .top_left, .center_left, .bottom_left => monitor.win_x,
544 .top_center, .center, .bottom_center => monitor.win_x + @divTrunc(monitor.win_w - client.width - bw, 2),
545 .top_right, .center_right, .bottom_right => monitor.win_x + monitor.win_w - client.width - bw,
546 };
547 const y: i32 = switch (pos) {
548 .top_left, .top_center, .top_right => monitor.win_y,
549 .center_left, .center, .center_right => monitor.win_y + @divTrunc(monitor.win_h - client.height - bw, 2),
550 .bottom_left, .bottom_center, .bottom_right => monitor.win_y + monitor.win_h - client.height - bw,
551 };
552 client.x = x;
553 client.y = y;
554 }
555
556 pub fn applyRules(client: *Client, wm: *WindowManager) void {
557 var class_hint: xlib.XClassHint = .{ .res_name = null, .res_class = null };
558 _ = xlib.XGetClassHint(wm.display.handle, client.window, &class_hint);
559
560 const class_str: []const u8 = if (class_hint.res_class) |ptr| std.mem.sliceTo(ptr, 0) else "";
561 const instance_str: []const u8 = if (class_hint.res_name) |ptr| std.mem.sliceTo(ptr, 0) else "";
562
563 client.is_floating = false;
564 client.tags = 0;
565 var rule_focus = false;
566
567 for (wm.config.rules.items) |rule| {
568 const class_matches = if (rule.class) |rc| std.mem.indexOf(u8, class_str, rc) != null else true;
569 const instance_matches = if (rule.instance) |ri| std.mem.indexOf(u8, instance_str, ri) != null else true;
570 const title_matches = if (rule.title) |rt| std.mem.indexOf(u8, std.mem.sliceTo(&client.name, 0), rt) != null else true;
571
572 if (class_matches and instance_matches and title_matches) {
573 client.is_floating = rule.is_floating;
574 client.tags |= rule.tags;
575 if (rule.width > 0) client.scroll_width = rule.width;
576 if (rule.monitor >= 0) {
577 var target = wm.monitors;
578 var index: i32 = 0;
579 while (target) |mon| {
580 if (index == rule.monitor) {
581 client.monitor = mon;
582 break;
583 }
584 index += 1;
585 target = mon.next;
586 }
587 }
588 if (rule.focus) {
589 rule_focus = true;
590 }
591 }
592 }
593
594 if (class_hint.res_class) |ptr| {
595 _ = xlib.XFree(@ptrCast(ptr));
596 }
597 if (class_hint.res_name) |ptr| {
598 _ = xlib.XFree(@ptrCast(ptr));
599 }
600
601 const monitor = client.monitor orelse return;
602 if (client.tags == 0) {
603 client.tags = monitor.tagset[monitor.sel_tags];
604 }
605
606 if (rule_focus and client.tags != 0) {
607 const monitor_tagset = monitor.tagset[monitor.sel_tags];
608 const is_tag_focused = (monitor_tagset & client.tags) == client.tags;
609 if (!is_tag_focused) {
610 view(client.tags, wm);
611 }
612 }
613 }
614
615 pub fn updateSizeHints(client: *Client, wm: *WindowManager) void {
616 var size_hints: xlib.XSizeHints = undefined;
617 var msize: c_long = 0;
618
619 if (xlib.XGetWMNormalHints(wm.display.handle, client.window, &size_hints, &msize) == 0) {
620 size_hints.flags = xlib.PSize;
621 }
622
623 if ((size_hints.flags & xlib.PBaseSize) != 0) {
624 client.base_width = size_hints.base_width;
625 client.base_height = size_hints.base_height;
626 } else if ((size_hints.flags & xlib.PMinSize) != 0) {
627 client.base_width = size_hints.min_width;
628 client.base_height = size_hints.min_height;
629 } else {
630 client.base_width = 0;
631 client.base_height = 0;
632 }
633
634 if ((size_hints.flags & xlib.PResizeInc) != 0) {
635 client.increment_width = size_hints.width_inc;
636 client.increment_height = size_hints.height_inc;
637 } else {
638 client.increment_width = 0;
639 client.increment_height = 0;
640 }
641
642 if ((size_hints.flags & xlib.PMaxSize) != 0) {
643 client.max_width = size_hints.max_width;
644 client.max_height = size_hints.max_height;
645 } else {
646 client.max_width = 0;
647 client.max_height = 0;
648 }
649
650 if ((size_hints.flags & xlib.PMinSize) != 0) {
651 client.min_width = size_hints.min_width;
652 client.min_height = size_hints.min_height;
653 } else if ((size_hints.flags & xlib.PBaseSize) != 0) {
654 client.min_width = size_hints.base_width;
655 client.min_height = size_hints.base_height;
656 } else {
657 client.min_width = 0;
658 client.min_height = 0;
659 }
660
661 if ((size_hints.flags & xlib.PAspect) != 0) {
662 client.min_aspect = @as(f32, @floatFromInt(size_hints.min_aspect.y)) / @as(f32, @floatFromInt(size_hints.min_aspect.x));
663 client.max_aspect = @as(f32, @floatFromInt(size_hints.max_aspect.x)) / @as(f32, @floatFromInt(size_hints.max_aspect.y));
664 } else {
665 client.min_aspect = 0.0;
666 client.max_aspect = 0.0;
667 }
668
669 client.is_fixed = (client.max_width != 0 and client.max_height != 0 and client.max_width == client.min_width and client.max_height == client.min_height);
670 client.hints_valid = true;
671 }
672
673 pub fn updateWmHints(client: *Client, wm: *WindowManager) void {
674 const wmh = xlib.XGetWMHints(wm.display.handle, client.window);
675 if (wmh) |hints| {
676 defer _ = xlib.XFree(@ptrCast(hints));
677
678 if (client == (wm.selected_monitor orelse return).sel and (hints.*.flags & xlib.XUrgencyHint) != 0) {
679 hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint);
680 _ = xlib.XSetWMHints(wm.display.handle, client.window, hints);
681 } else {
682 client.is_urgent = (hints.*.flags & xlib.XUrgencyHint) != 0;
683 }
684
685 if ((hints.*.flags & xlib.InputHint) != 0) {
686 client.never_focus = hints.*.input == 0;
687 } else {
688 client.never_focus = false;
689 }
690 }
691 }
692
693 pub fn updateWindowType(client: *Client, wm: *WindowManager) void {
694 const state = getAtomProp(client, wm.atoms.net_wm_state, wm);
695 const window_type = getAtomProp(client, wm.atoms.net_wm_window_type, wm);
696
697 if (state == wm.atoms.net_wm_state_fullscreen) {
698 setFullscreen(client, true, wm);
699 }
700 if (window_type == wm.atoms.net_wm_window_type_dialog) {
701 client.is_floating = true;
702 }
703 }
704
705 pub fn updateTitle(client: *Client, wm: *WindowManager) void {
706 if (!getTextProp(client.window, wm.atoms.net_wm_name, &client.name, wm)) {
707 _ = getTextProp(client.window, xlib.XA_WM_NAME, &client.name, wm);
708 }
709 if (client.name[0] == 0) {
710 @memcpy(client.name[0..6], "broken");
711 }
712 }
713
714 pub fn getAtomProp(client: *Client, prop: xlib.Atom, wm: *WindowManager) xlib.Atom {
715 var actual_type: xlib.Atom = undefined;
716 var actual_format: c_int = undefined;
717 var num_items: c_ulong = undefined;
718 var bytes_after: c_ulong = undefined;
719 var prop_data: [*c]u8 = undefined;
720
721 if (xlib.XGetWindowProperty(wm.display.handle, client.window, prop, 0, @sizeOf(xlib.Atom), xlib.False, xlib.XA_ATOM, &actual_type, &actual_format, &num_items, &bytes_after, &prop_data) == 0 and prop_data != null) {
722 const atom: xlib.Atom = @as(*xlib.Atom, @ptrCast(@alignCast(prop_data))).*;
723 _ = xlib.XFree(@ptrCast(prop_data));
724 return atom;
725 }
726 return 0;
727 }
728
729 pub fn getTextProp(window: xlib.Window, atom: xlib.Atom, text: *[256]u8, wm: *WindowManager) bool {
730 var name: xlib.XTextProperty = undefined;
731 text[0] = 0;
732
733 if (xlib.XGetTextProperty(wm.display.handle, window, &name, atom) == 0 or name.nitems == 0) {
734 return false;
735 }
736
737 if (name.encoding == xlib.XA_STRING) {
738 const len = @min(name.nitems, 255);
739 @memcpy(text[0..len], name.value[0..len]);
740 text[len] = 0;
741 } else {
742 var list: [*c][*c]u8 = undefined;
743 var count: c_int = undefined;
744 if (xlib.XmbTextPropertyToTextList(wm.display.handle, &name, &list, &count) >= xlib.Success and count > 0 and list[0] != null) {
745 const str = std.mem.sliceTo(list[0], 0);
746 const copy_len = @min(str.len, 255);
747 @memcpy(text[0..copy_len], str[0..copy_len]);
748 text[copy_len] = 0;
749 xlib.XFreeStringList(list);
750 }
751 }
752 text[255] = 0;
753 _ = xlib.XFree(@ptrCast(name.value));
754 return true;
755 }
756
757 pub fn updateClientList(wm: *WindowManager) void {
758 _ = xlib.XDeleteProperty(wm.display.handle, wm.display.root, wm.atoms.net_client_list);
759
760 var current_monitor = wm.monitors;
761 while (current_monitor) |monitor| {
762 var current_client = monitor.clients;
763 while (current_client) |client| {
764 _ = xlib.XChangeProperty(wm.display.handle, wm.display.root, wm.atoms.net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
765 current_client = client.next;
766 }
767 current_monitor = monitor.next;
768 }
769 }
770
771 pub fn sendEvent(client: *Client, protocol: xlib.Atom, wm: *WindowManager) bool {
772 var protocols: [*c]xlib.Atom = undefined;
773 var num_protocols: c_int = 0;
774 var exists = false;
775
776 if (xlib.XGetWMProtocols(wm.display.handle, client.window, &protocols, &num_protocols) != 0) {
777 var index: usize = 0;
778 while (index < @as(usize, @intCast(num_protocols))) : (index += 1) {
779 if (protocols[index] == protocol) {
780 exists = true;
781 break;
782 }
783 }
784 _ = xlib.XFree(@ptrCast(protocols));
785 }
786
787 if (exists) {
788 var event: xlib.XEvent = undefined;
789 event.type = xlib.ClientMessage;
790 event.xclient.window = client.window;
791 event.xclient.message_type = wm.atoms.wm_protocols;
792 event.xclient.format = 32;
793 event.xclient.data.l[0] = @intCast(protocol);
794 event.xclient.data.l[1] = xlib.CurrentTime;
795 _ = xlib.XSendEvent(wm.display.handle, client.window, xlib.False, xlib.NoEventMask, &event);
796 }
797 return exists;
798 }
799
800 pub fn setUrgent(client: *Client, urgent: bool, wm: *WindowManager) void {
801 client.is_urgent = urgent;
802 const wmh = xlib.XGetWMHints(wm.display.handle, client.window);
803 if (wmh) |hints| {
804 if (urgent) {
805 hints.*.flags = hints.*.flags | xlib.XUrgencyHint;
806 } else {
807 hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint);
808 }
809 _ = xlib.XSetWMHints(wm.display.handle, client.window, hints);
810 _ = xlib.XFree(@ptrCast(hints));
811 }
812 }
813
814 pub fn hasClientsOnTag(monitor: *monitor_mod.Monitor, tag_mask: u32) bool {
815 var client = monitor.clients;
816 while (client) |c| {
817 if ((c.tags & tag_mask) != 0) {
818 return true;
819 }
820 client = c.next;
821 }
822 return false;
823 }
824
825 pub fn view(tag_mask: u32, wm: *WindowManager) void {
826 const monitor = wm.selected_monitor orelse return;
827 if (tag_mask == monitor.tagset[monitor.sel_tags]) {
828 return;
829 }
830 monitor.sel_tags ^= 1;
831 if (tag_mask != 0) {
832 monitor.tagset[monitor.sel_tags] = tag_mask;
833 monitor.pertag.prevtag = monitor.pertag.curtag;
834
835 if (tag_mask == ~@as(u32, 0)) {
836 monitor.pertag.curtag = 0;
837 } else {
838 var i: u32 = 0;
839 while (i < wm.config.tag_count) : (i += 1) {
840 if ((tag_mask & (@as(u32, 1) << @intCast(i))) != 0) break;
841 }
842 monitor.pertag.curtag = i + 1;
843 }
844 } else {
845 const tmp = monitor.pertag.prevtag;
846 monitor.pertag.prevtag = monitor.pertag.curtag;
847 monitor.pertag.curtag = tmp;
848 }
849
850 monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag];
851 monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag];
852 monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag];
853
854 const new_show_bar = monitor.pertag.showbars[monitor.pertag.curtag];
855 if (new_show_bar != monitor.show_bar) {
856 monitor.show_bar = new_show_bar;
857 window_manager.actions.updateBarVisibility(monitor, wm);
858 }
859
860 focusTopClient(monitor, wm);
861 arrange(monitor, wm);
862 wm.invalidateBars();
863 std.debug.print("view: tag_mask={d}\n", .{monitor.tagset[monitor.sel_tags]});
864 }