tony / oxwm

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