tony / oxwm

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