tony / oxwm

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