tony / oxwm

Branch: ea422a3
Switch branches/tags
master
emzywastaken treewide: change function names to camelCase b3ae92e 2026-02-23
160 lines · 4.9 KB Raw Blame History
1 const std = @import("std");
2 const xlib = @import("x11/xlib.zig");
3
4 const Client = @import("client.zig").Client;
5 const WindowManager = @import("wm/wm.zig").WindowManager;
6
7 pub const Layout = struct {
8 symbol: []const u8,
9 arrange_fn: ?*const fn (*Monitor) void,
10 };
11
12 pub const Pertag = struct {
13 curtag: u32 = 1,
14 prevtag: u32 = 1,
15 nmasters: [10]i32 = [_]i32{1} ** 10,
16 mfacts: [10]f32 = [_]f32{0.55} ** 10,
17 sellts: [10]u32 = [_]u32{0} ** 10,
18 ltidxs: [10][5]?*const Layout = [_][5]?*const Layout{.{ null, null, null, null, null }} ** 10,
19 showbars: [10]bool = [_]bool{true} ** 10,
20 };
21
22 pub const Monitor = struct {
23 lt_symbol: [16]u8 = std.mem.zeroes([16]u8),
24 mfact: f32 = 0.55,
25 nmaster: i32 = 1,
26 num: i32 = 0,
27 bar_y: i32 = 0,
28 mon_x: i32 = 0,
29 mon_y: i32 = 0,
30 mon_w: i32 = 0,
31 mon_h: i32 = 0,
32 win_x: i32 = 0,
33 win_y: i32 = 0,
34 win_w: i32 = 0,
35 win_h: i32 = 0,
36 gap_inner_h: i32 = 0,
37 gap_inner_v: i32 = 0,
38 gap_outer_h: i32 = 0,
39 gap_outer_v: i32 = 0,
40 scroll_offset: i32 = 0,
41 sel_tags: u32 = 0,
42 sel_lt: u32 = 0,
43 tagset: [2]u32 = .{ 1, 1 },
44 show_bar: bool = true,
45 top_bar: bool = true,
46 clients: ?*Client = null,
47 sel: ?*Client = null,
48 stack: ?*Client = null,
49 next: ?*Monitor = null,
50 bar_win: xlib.Window = 0,
51 lt: [5]?*const Layout = .{null} ** 5,
52 pertag: Pertag = .{},
53 };
54
55 /// Allocates and zero-initialises a new `Monitor`.
56 pub fn create(allocator: std.mem.Allocator) ?*Monitor {
57 const mon = allocator.create(Monitor) catch return null;
58 mon.* = Monitor{};
59 return mon;
60 }
61
62 /// Frees a monitor previously returned by `create`.
63 pub fn destroy(allocator: std.mem.Allocator, mon: *Monitor) void {
64 allocator.destroy(mon);
65 }
66
67 /// Returns the monitor whose bar window or client matches `win`, falling
68 /// back to a pointer-position query when `win` is the root window.
69 pub fn windowToMonitor(wm: *WindowManager, win: xlib.Window) ?*Monitor {
70 const monitors = wm.monitors;
71 const selected_monitor = wm.selected_monitor;
72
73 if (win == wm.display.root) {
74 var root_x: c_int = undefined;
75 var root_y: c_int = undefined;
76 var dummy_win: xlib.Window = undefined;
77 var dummy_int: c_int = undefined;
78 var dummy_uint: c_uint = undefined;
79 if (xlib.XQueryPointer(wm.display.handle, wm.display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint) != 0) {
80 return rectToMonitor(wm, root_x, root_y, 1, 1);
81 }
82 }
83
84 var current = monitors;
85 while (current) |monitor| {
86 if (monitor.bar_win == win) return monitor;
87 current = monitor.next;
88 }
89
90 const client = @import("client.zig").windowToClient(monitors, win);
91 if (client) |found_client| {
92 return found_client.monitor;
93 }
94
95 return selected_monitor;
96 }
97
98 /// Returns the monitor with the greatest intersection area with the given
99 /// rectangle, or `selected_monitor` if no intersection is found.
100 pub fn rectToMonitor(wm: *WindowManager, x: i32, y: i32, width: i32, height: i32) ?*Monitor {
101 const monitors = wm.monitors;
102 const selected_monitor = wm.selected_monitor;
103 var result = selected_monitor;
104 var max_area: i32 = 0;
105
106 var current = monitors;
107 while (current) |monitor| {
108 const intersect_x = @max(0, @min(x + width, monitor.win_x + monitor.win_w) - @max(x, monitor.win_x));
109 const intersect_y = @max(0, @min(y + height, monitor.win_y + monitor.win_h) - @max(y, monitor.win_y));
110 const area = intersect_x * intersect_y;
111 if (area > max_area) {
112 max_area = area;
113 result = monitor;
114 }
115 current = monitor.next;
116 }
117 return result;
118 }
119
120 /// Returns the next or previous monitor relative to `wm.selected_monitor`.
121 ///
122 /// Positive `direction` moves forward through the linked list (wrapping to
123 /// the head); negative moves backward (wrapping to the tail).
124 ///
125 // TODO:
126 // - Change direction to an enum/enum_literal
127 // - Rename function
128 pub fn dirToMonitor(wm: *WindowManager, direction: i32) ?*Monitor {
129 const monitors = wm.monitors;
130 const selected_monitor = wm.selected_monitor;
131 var target: ?*Monitor = null;
132
133 if (direction > 0) {
134 target = if (selected_monitor) |current| current.next else null;
135 if (target == null) {
136 target = monitors;
137 }
138 } else if (selected_monitor == monitors) {
139 // Already at head, walk to tail.
140 var last = monitors;
141 while (last) |iter| {
142 if (iter.next == null) {
143 target = iter;
144 break;
145 }
146 last = iter.next;
147 }
148 } else {
149 // Walk until we find the node just before selected_monitor.
150 var previous = monitors;
151 while (previous) |iter| {
152 if (iter.next == selected_monitor) {
153 target = iter;
154 break;
155 }
156 previous = iter.next;
157 }
158 }
159 return target;
160 }