tony / oxwm

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