tony / oxwm

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