tony / oxwm

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