tony / oxwm

Merge pull request #160 from tonybanters/cleanup Browse files
General Cleanup
Tony, btw. committed 2026-02-25 parents 0ac25c0 413ee10 commit 5bb9e13edce622017fea5c49b3f8a4e03afc6a79
Showing 32 changed files with 3357 additions and 3180 deletions. UnifiedSplit
+2 −0 .gitignore
@@ -10,3 +10,5 @@ target/
10 10 # Notes
11 11 notes/
12 12 *.md
13 +
14 +oxwm-rs/
+11 −6 build.zig
@@ -1,4 +1,5 @@
1 1 const std = @import("std");
2 +const zon = @import("build.zig.zon");
2 3
3 4 pub fn build(b: *std.Build) void {
4 5 const target = b.standardTargetOptions(.{});
@@ -13,6 +14,10 @@ pub fn build(b: *std.Build) void {
13 14 }),
14 15 });
15 16
17 + const exe_options = b.addOptions();
18 + exe_options.addOption([]const u8, "version", zon.version);
19 + exe.root_module.addOptions("build_options", exe_options);
20 +
16 21 exe.root_module.addAnonymousImport("templates/config.lua", .{
17 22 .root_source_file = b.path("templates/config.lua"),
18 23 });
@@ -81,16 +86,16 @@ pub fn build(b: *std.Build) void {
81 86 test_step.dependOn(&b.addRunArtifact(lua_config_tests).step);
82 87
83 88 const xephyr_step = b.step("xephyr", "Run in Xephyr (1280x800 on :2)");
84 - xephyr_step.dependOn(&add_xephyr_run(b, exe, false).step);
89 + xephyr_step.dependOn(&addXephyrRun(b, exe, false).step);
85 90
86 91 const xephyr_multi_step = b.step("xephyr-multi", "Run in Xephyr multi-monitor on :2");
87 - xephyr_multi_step.dependOn(&add_xephyr_run(b, exe, true).step);
92 + xephyr_multi_step.dependOn(&addXephyrRun(b, exe, true).step);
88 93
89 94 const multimon_step = b.step("multimon", "Alias for xephyr-multi");
90 - multimon_step.dependOn(&add_xephyr_run(b, exe, true).step);
95 + multimon_step.dependOn(&addXephyrRun(b, exe, true).step);
91 96
92 97 const xwayland_step = b.step("xwayland", "Run in Xwayland on :2");
93 - xwayland_step.dependOn(&add_xwayland_run(b, exe).step);
98 + xwayland_step.dependOn(&addXwaylandRun(b, exe).step);
94 99
95 100 const kill_step = b.step("kill", "Kill Xephyr and oxwm");
96 101 kill_step.dependOn(&b.addSystemCommand(&.{ "sh", "-c", "pkill -9 Xephyr || true; pkill -9 oxwm || true" }).step);
@@ -125,7 +130,7 @@ pub fn build(b: *std.Build) void {
125 130 }).step);
126 131 }
127 132
128 -fn add_xephyr_run(b: *std.Build, exe: *std.Build.Step.Compile, multimon: bool) *std.Build.Step.Run {
133 +fn addXephyrRun(b: *std.Build, exe: *std.Build.Step.Compile, multimon: bool) *std.Build.Step.Run {
129 134 const kill_cmd = if (multimon)
130 135 "pkill -9 Xephyr || true; Xephyr +xinerama -glamor -screen 640x480 -screen 640x480 :2 & sleep 1"
131 136 else
@@ -141,7 +146,7 @@ fn add_xephyr_run(b: *std.Build, exe: *std.Build.Step.Compile, multimon: bool) *
141 146 return run_wm;
142 147 }
143 148
144 -fn add_xwayland_run(b: *std.Build, exe: *std.Build.Step.Compile) *std.Build.Step.Run {
149 +fn addXwaylandRun(b: *std.Build, exe: *std.Build.Step.Compile) *std.Build.Step.Run {
145 150 const cmd = "Xwayland -retro -noreset :2 & sleep 1";
146 151
147 152 const setup = b.addSystemCommand(&.{ "sh", "-c", cmd });
+13 −0 build.zig.zon new file mode 100644
@@ -0,0 +1,13 @@
1 +.{
2 + .name = .oxwm,
3 + .version = "0.11.3",
4 + .fingerprint = 0x4b9ef3753914924d, // Changing this has security and trust implications.
5 + .minimum_zig_version = "0.15.2",
6 + .paths = .{
7 + "build.zig",
8 + "build.zig.zon",
9 + "src",
10 + "LICENSE",
11 + "README.md",
12 + },
13 +}
+7 −7 src/animations.zig
@@ -17,12 +17,12 @@ pub const Easing = enum {
17 17 }
18 18 };
19 19
20 -pub const Animation_Config = struct {
20 +pub const AnimationConfig = struct {
21 21 duration_ms: u64 = 150,
22 22 easing: Easing = .ease_out,
23 23 };
24 24
25 -pub const Scroll_Animation = struct {
25 +pub const ScrollAnimation = struct {
26 26 start_value: i32 = 0,
27 27 end_value: i32 = 0,
28 28 start_time: i64 = 0,
@@ -30,7 +30,7 @@ pub const Scroll_Animation = struct {
30 30 easing: Easing = .ease_out,
31 31 active: bool = false,
32 32
33 - pub fn start(self: *Scroll_Animation, from: i32, to: i32, config: Animation_Config) void {
33 + pub fn start(self: *ScrollAnimation, from: i32, to: i32, config: AnimationConfig) void {
34 34 if (from == to) {
35 35 self.active = false;
36 36 return;
@@ -43,7 +43,7 @@ pub const Scroll_Animation = struct {
43 43 self.active = true;
44 44 }
45 45
46 - pub fn update(self: *Scroll_Animation) ?i32 {
46 + pub fn update(self: *ScrollAnimation) ?i32 {
47 47 if (!self.active) return null;
48 48
49 49 const now = std.time.milliTimestamp();
@@ -62,15 +62,15 @@ pub const Scroll_Animation = struct {
62 62 return @intFromFloat(current);
63 63 }
64 64
65 - pub fn is_active(self: *const Scroll_Animation) bool {
65 + pub fn isActive(self: *const ScrollAnimation) bool {
66 66 return self.active;
67 67 }
68 68
69 - pub fn target(self: *const Scroll_Animation) i32 {
69 + pub fn target(self: *const ScrollAnimation) i32 {
70 70 return self.end_value;
71 71 }
72 72
73 - pub fn stop(self: *Scroll_Animation) void {
73 + pub fn stop(self: *ScrollAnimation) void {
74 74 self.active = false;
75 75 }
76 76 };
+98 −133 src/bar/bar.zig
@@ -1,7 +1,6 @@
1 1 const std = @import("std");
2 2 const xlib = @import("../x11/xlib.zig");
3 3 const monitor_mod = @import("../monitor.zig");
4 -const client_mod = @import("../client.zig");
5 4 const blocks_mod = @import("blocks/blocks.zig");
6 5 const config_mod = @import("../config/config.zig");
7 6 const ColorScheme = config_mod.ColorScheme;
@@ -9,23 +8,12 @@ const ColorScheme = config_mod.ColorScheme;
9 8 const Monitor = monitor_mod.Monitor;
10 9 const Block = blocks_mod.Block;
11 10
12 -fn get_layout_symbol(layout_index: u32) []const u8 {
13 - const cfg = config_mod.get_config();
14 - if (cfg) |conf| {
15 - return switch (layout_index) {
16 - 0 => conf.layout_tile_symbol,
17 - 1 => conf.layout_monocle_symbol,
18 - 2 => conf.layout_floating_symbol,
19 - 3 => "[S]",
20 - 4 => "[#]",
21 - else => "[?]",
22 - };
23 - }
11 +fn getLayoutSymbol(layout_index: u32, config: config_mod.Config) []const u8 {
24 12 return switch (layout_index) {
25 - 0 => "[]=",
26 - 1 => "[M]",
27 - 2 => "><>",
28 - 3 => "[S]",
13 + 0 => config.layout_tile_symbol,
14 + 1 => config.layout_monocle_symbol,
15 + 2 => config.layout_floating_symbol,
16 + 3 => config.layout_scrolling_symbol,
29 17 4 => "[#]",
30 18 else => "[?]",
31 19 };
@@ -54,6 +42,8 @@ pub const Bar = struct {
54 42 needs_redraw: bool,
55 43 next: ?*Bar,
56 44
45 + /// Creates a bar window for `monitor` using the given config.
46 + /// Returns null on allocation failure or if the font cannot be loaded.
57 47 pub fn create(
58 48 allocator: std.mem.Allocator,
59 49 display: *xlib.Display,
@@ -68,7 +58,10 @@ pub const Bar = struct {
68 58 const depth = xlib.XDefaultDepth(display, screen);
69 59 const root = xlib.XRootWindow(display, screen);
70 60
71 - const font_name_z = allocator.dupeZ(u8, config.font) catch return null;
61 + const font_name_z = allocator.dupeZ(u8, config.font) catch {
62 + allocator.destroy(bar);
63 + return null;
64 + };
72 65 defer allocator.free(font_name_z);
73 66
74 67 const font = xlib.XftFontOpenName(display, screen, font_name_z);
@@ -92,7 +85,6 @@ pub const Bar = struct {
92 85 0x1a1b26,
93 86 );
94 87
95 - _ = xlib.c.XSetWindowAttributes{};
96 88 var attributes: xlib.c.XSetWindowAttributes = undefined;
97 89 attributes.override_redirect = xlib.True;
98 90 attributes.event_mask = xlib.c.ExposureMask | xlib.c.ButtonPressMask;
@@ -107,7 +99,6 @@ pub const Bar = struct {
107 99 );
108 100
109 101 const graphics_context = xlib.XCreateGC(display, pixmap, 0, null);
110 -
111 102 const xft_draw = xlib.XftDrawCreate(display, pixmap, visual, colormap);
112 103
113 104 _ = xlib.XMapWindow(display, window);
@@ -128,7 +119,7 @@ pub const Bar = struct {
128 119 .scheme_urgent = config.scheme_urgent,
129 120 .hide_vacant_tags = config.hide_vacant_tags,
130 121 .allocator = allocator,
131 - .blocks = .{},
122 + .blocks = .empty,
132 123 .needs_redraw = true,
133 124 .next = null,
134 125 };
@@ -140,77 +131,84 @@ pub const Bar = struct {
140 131 return bar;
141 132 }
142 133
143 - pub fn destroy(self: *Bar, allocator: std.mem.Allocator, display: *xlib.Display) void {
144 - if (self.xft_draw) |xft_draw| {
145 - xlib.XftDrawDestroy(xft_draw);
146 - }
147 - if (self.font) |font| {
148 - xlib.XftFontClose(display, font);
149 - }
134 + /// Destroys the bar's X resources and frees the allocation.
135 + pub fn destroy(self: *Bar, display: *xlib.Display) void {
136 + if (self.xft_draw) |xft_draw| xlib.XftDrawDestroy(xft_draw);
137 + if (self.font) |font| xlib.XftFontClose(display, font);
138 +
150 139 _ = xlib.XFreeGC(display, self.graphics_context);
151 140 _ = xlib.XFreePixmap(display, self.pixmap);
152 141 _ = xlib.c.XDestroyWindow(display, self.window);
153 142 self.blocks.deinit(self.allocator);
154 - allocator.destroy(self);
143 + self.allocator.destroy(self);
155 144 }
156 145
157 - pub fn add_block(self: *Bar, block: Block) void {
146 + pub fn addBlock(self: *Bar, block: Block) void {
158 147 self.blocks.append(self.allocator, block) catch {};
159 148 }
160 149
150 + pub fn clearBlocks(self: *Bar) void {
151 + self.blocks.clearRetainingCapacity();
152 + }
153 +
161 154 pub fn invalidate(self: *Bar) void {
162 155 self.needs_redraw = true;
163 156 }
164 157
165 - pub fn draw(self: *Bar, display: *xlib.Display, tags: []const []const u8) void {
158 + /// Redraws the bar if marked dirty. Tags are taken from `config.tags`
159 + pub fn draw(self: *Bar, display: *xlib.Display, config: config_mod.Config) void {
166 160 if (!self.needs_redraw) return;
167 161
168 - self.fill_rect(display, 0, 0, self.width, self.height, self.scheme_normal.background);
162 + self.fillRect(display, 0, 0, self.width, self.height, self.scheme_normal.background);
169 163
170 164 var x_position: i32 = 0;
171 165 const padding: i32 = 8;
172 166 const monitor = self.monitor;
173 167 const current_tags = monitor.tagset[monitor.sel_tags];
174 168
175 - for (tags, 0..) |tag, index| {
169 + for (config.tags, 0..) |tag, index| {
176 170 const tag_mask: u32 = @as(u32, 1) << @intCast(index);
177 171 const is_selected = (current_tags & tag_mask) != 0;
178 - const is_occupied = has_clients_on_tag(monitor, tag_mask);
172 + const is_occupied = hasClientsOnTag(monitor, tag_mask);
179 173
180 174 if (self.hide_vacant_tags and !is_occupied and !is_selected) continue;
181 175
182 - const scheme = if (is_selected) self.scheme_selected else if (is_occupied) self.scheme_occupied else self.scheme_normal;
176 + const scheme = if (is_selected)
177 + self.scheme_selected
178 + else if (is_occupied)
179 + self.scheme_occupied
180 + else
181 + self.scheme_normal;
183 182
184 - const tag_text_width = self.text_width(display, tag);
183 + const tag_text_width = self.textWidth(display, tag);
185 184 const tag_width = tag_text_width + padding * 2;
186 185
187 186 if (is_selected) {
188 - self.fill_rect(display, x_position, self.height - 3, tag_width, 3, scheme.border);
187 + self.fillRect(display, x_position, self.height - 3, tag_width, 3, scheme.border);
189 188 }
190 189
191 190 const text_y = @divTrunc(self.height + self.font_height, 2) - 4;
192 - self.draw_text(display, x_position + padding, text_y, tag, scheme.foreground);
193 -
191 + self.drawText(display, x_position + padding, text_y, tag, scheme.foreground);
194 192 x_position += tag_width;
195 193 }
196 194
197 195 x_position += padding;
198 196
199 - const layout_symbol = get_layout_symbol(monitor.sel_lt);
200 - self.draw_text(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground);
201 - x_position += self.text_width(display, layout_symbol) + padding;
197 + const layout_symbol = getLayoutSymbol(monitor.sel_lt, config);
198 + self.drawText(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground);
199 + x_position += self.textWidth(display, layout_symbol) + padding;
202 200
203 201 var block_x: i32 = self.width - padding;
204 202 var block_index: usize = self.blocks.items.len;
205 203 while (block_index > 0) {
206 204 block_index -= 1;
207 205 const block = &self.blocks.items[block_index];
208 - const content = block.get_content();
209 - const content_width = self.text_width(display, content);
206 + const content = block.getContent();
207 + const content_width = self.textWidth(display, content);
210 208 block_x -= content_width;
211 - self.draw_text(display, block_x, @divTrunc(self.height + self.font_height, 2) - 4, content, block.color());
209 + self.drawText(display, block_x, @divTrunc(self.height + self.font_height, 2) - 4, content, block.color());
212 210 if (block.underline) {
213 - self.fill_rect(display, block_x, self.height - 2, content_width, 2, block.color());
211 + self.fillRect(display, block_x, self.height - 2, content_width, 2, block.color());
214 212 }
215 213 block_x -= padding;
216 214 }
@@ -221,55 +219,22 @@ pub const Bar = struct {
221 219 self.needs_redraw = false;
222 220 }
223 221
224 - fn fill_rect(self: *Bar, display: *xlib.Display, x: i32, y: i32, width: i32, height: i32, color: c_ulong) void {
225 - _ = xlib.XSetForeground(display, self.graphics_context, color);
226 - _ = xlib.XFillRectangle(display, self.pixmap, self.graphics_context, x, y, @intCast(width), @intCast(height));
227 - }
228 -
229 - fn draw_text(self: *Bar, display: *xlib.Display, x: i32, y: i32, text: []const u8, color: c_ulong) void {
230 - if (self.xft_draw == null or self.font == null) return;
231 -
232 - var xft_color: xlib.XftColor = undefined;
233 - var render_color: xlib.XRenderColor = undefined;
234 - render_color.red = @intCast((color >> 16 & 0xff) * 257);
235 - render_color.green = @intCast((color >> 8 & 0xff) * 257);
236 - render_color.blue = @intCast((color & 0xff) * 257);
237 - render_color.alpha = 0xffff;
238 -
239 - const visual = xlib.XDefaultVisual(display, 0);
240 - const colormap = xlib.XDefaultColormap(display, 0);
241 -
242 - _ = xlib.XftColorAllocValue(display, visual, colormap, &render_color, &xft_color);
243 -
244 - xlib.XftDrawStringUtf8(self.xft_draw, &xft_color, self.font, x, y, text.ptr, @intCast(text.len));
245 -
246 - xlib.XftColorFree(display, visual, colormap, &xft_color);
247 - }
248 -
249 - fn text_width(self: *Bar, display: *xlib.Display, text: []const u8) i32 {
250 - if (self.font == null) return 0;
251 -
252 - var extents: xlib.XGlyphInfo = undefined;
253 - xlib.XftTextExtentsUtf8(display, self.font, text.ptr, @intCast(text.len), &extents);
254 - return extents.xOff;
255 - }
256 -
257 - pub fn handle_click(self: *Bar, click_x: i32, tags: []const []const u8) ?usize {
222 + /// Returns the index of the tag the user clicked on, or null if the
223 + /// click was outside the tag area.
224 + pub fn handleClick(self: *Bar, display: *xlib.Display, click_x: i32, config: config_mod.Config) ?usize {
258 225 var x_position: i32 = 0;
259 226 const padding: i32 = 8;
260 - const display = xlib.c.XOpenDisplay(null) orelse return null;
261 - defer _ = xlib.XCloseDisplay(display);
262 -
263 227 const monitor = self.monitor;
264 228 const current_tags = monitor.tagset[monitor.sel_tags];
265 - for (tags, 0..) |tag, index| {
229 +
230 + for (config.tags, 0..) |tag, index| {
266 231 const tag_mask = @as(u32, 1) << @intCast(index);
267 232 const is_selected = (current_tags & tag_mask) != 0;
268 - const is_occupied = has_clients_on_tag(monitor, tag_mask);
233 + const is_occupied = hasClientsOnTag(monitor, tag_mask);
269 234
270 235 if (self.hide_vacant_tags and !is_occupied and !is_selected) continue;
271 236
272 - const tag_text_width = self.text_width(display, tag);
237 + const tag_text_width = self.textWidth(display, tag);
273 238 const tag_width = tag_text_width + padding * 2;
274 239
275 240 if (click_x >= x_position and click_x < x_position + tag_width) {
@@ -280,59 +245,51 @@ pub const Bar = struct {
280 245 return null;
281 246 }
282 247
283 - pub fn update_blocks(self: *Bar) void {
248 + /// Updates all blocks and marks the bar dirty if any block changed.
249 + pub fn updateBlocks(self: *Bar) void {
284 250 var changed = false;
285 251 for (self.blocks.items) |*block| {
286 - if (block.update()) {
287 - changed = true;
288 - }
289 - }
290 - if (changed) {
291 - self.needs_redraw = true;
252 + if (block.update()) changed = true;
292 253 }
254 + if (changed) self.needs_redraw = true;
293 255 }
294 256
295 - pub fn clear_blocks(self: *Bar) void {
296 - self.blocks.clearRetainingCapacity();
257 + fn fillRect(self: *Bar, display: *xlib.Display, x: i32, y: i32, width: i32, height: i32, color: c_ulong) void {
258 + _ = xlib.XSetForeground(display, self.graphics_context, color);
259 + _ = xlib.XFillRectangle(display, self.pixmap, self.graphics_context, x, y, @intCast(width), @intCast(height));
297 260 }
298 -};
299 261
300 -fn has_clients_on_tag(monitor: *Monitor, tag_mask: u32) bool {
301 - var current = monitor.clients;
302 - while (current) |client| {
303 - if ((client.tags & tag_mask) != 0) {
304 - return true;
305 - }
306 - current = client.next;
307 - }
308 - return false;
309 -}
262 + fn drawText(self: *Bar, display: *xlib.Display, x: i32, y: i32, text: []const u8, color: c_ulong) void {
263 + if (self.xft_draw == null or self.font == null) return;
310 264
311 -pub var bars: ?*Bar = null;
265 + var xft_color: xlib.XftColor = undefined;
266 + var render_color: xlib.XRenderColor = undefined;
267 + render_color.red = @intCast((color >> 16 & 0xff) * 257);
268 + render_color.green = @intCast((color >> 8 & 0xff) * 257);
269 + render_color.blue = @intCast((color & 0xff) * 257);
270 + render_color.alpha = 0xffff;
312 271
313 -pub fn create_bars(allocator: std.mem.Allocator, display: *xlib.Display, screen: c_int) void {
314 - var current_monitor = monitor_mod.monitors;
315 - while (current_monitor) |monitor| {
316 - const bar = Bar.create(allocator, display, screen, monitor, "monospace:size=10");
317 - if (bar) |created_bar| {
318 - bars = created_bar;
319 - }
320 - current_monitor = monitor.next;
272 + const visual = xlib.XDefaultVisual(display, 0);
273 + const colormap = xlib.XDefaultColormap(display, 0);
274 +
275 + _ = xlib.XftColorAllocValue(display, visual, colormap, &render_color, &xft_color);
276 + xlib.XftDrawStringUtf8(self.xft_draw, &xft_color, self.font, x, y, text.ptr, @intCast(text.len));
277 + xlib.XftColorFree(display, visual, colormap, &xft_color);
321 278 }
322 -}
323 279
324 -pub fn draw_bars(display: *xlib.Display, tags: []const []const u8) void {
325 - var current_monitor = monitor_mod.monitors;
326 - while (current_monitor) |monitor| {
327 - _ = monitor;
328 - if (bars) |bar| {
329 - bar.draw(display, tags);
330 - }
331 - current_monitor = if (current_monitor) |m| m.next else null;
280 + fn textWidth(self: *Bar, display: *xlib.Display, text: []const u8) i32 {
281 + if (self.font == null) return 0;
282 +
283 + var extents: xlib.XGlyphInfo = undefined;
284 + xlib.XftTextExtentsUtf8(display, self.font, text.ptr, @intCast(text.len), &extents);
285 + return extents.xOff;
332 286 }
333 -}
287 +};
288 +
289 +// Bar list helpers >.<
334 290
335 -pub fn invalidate_bars() void {
291 +/// Marks all bars in the list as needing a redraw.
292 +pub fn invalidateBars(bars: ?*Bar) void {
336 293 var current = bars;
337 294 while (current) |bar| {
338 295 bar.invalidate();
@@ -340,23 +297,31 @@ pub fn invalidate_bars() void {
340 297 }
341 298 }
342 299
343 -pub fn destroy_bars(allocator: std.mem.Allocator, display: *xlib.Display) void {
300 +/// Destroys all bars in the list and frees their resources.
301 +pub fn destroyBars(bars: ?*Bar, display: *xlib.Display) void {
344 302 var current = bars;
345 303 while (current) |bar| {
346 304 const next = bar.next;
347 - bar.destroy(allocator, display);
305 + bar.destroy(display);
348 306 current = next;
349 307 }
350 - bars = null;
351 308 }
352 309
353 -pub fn window_to_bar(win: xlib.Window) ?*Bar {
310 +/// Returns the bar whose window matches `win`, or null.
311 +pub fn windowToBar(bars: ?*Bar, win: xlib.Window) ?*Bar {
354 312 var current = bars;
355 313 while (current) |bar| {
356 - if (bar.window == win) {
357 - return bar;
358 - }
314 + if (bar.window == win) return bar;
359 315 current = bar.next;
360 316 }
361 317 return null;
362 318 }
319 +
320 +fn hasClientsOnTag(monitor: *Monitor, tag_mask: u32) bool {
321 + var current = monitor.clients;
322 + while (current) |client| {
323 + if ((client.tags & tag_mask) != 0) return true;
324 + current = client.next;
325 + }
326 + return false;
327 +}
+5 −5 src/bar/blocks/battery.zig
@@ -30,8 +30,8 @@ pub const Battery = struct {
30 30 pub fn content(self: *Battery, buffer: []u8) []const u8 {
31 31 var path_buf: [128]u8 = undefined;
32 32
33 - const capacity = self.read_battery_file(&path_buf, "capacity") orelse return buffer[0..0];
34 - const status = self.read_battery_status(&path_buf) orelse return buffer[0..0];
33 + const capacity = self.readBatteryFile(&path_buf, "capacity") orelse return buffer[0..0];
34 + const status = self.readBatteryStatus(&path_buf) orelse return buffer[0..0];
35 35
36 36 const format = switch (status) {
37 37 .charging => self.format_charging,
@@ -47,7 +47,7 @@ pub const Battery = struct {
47 47
48 48 const Status = enum { charging, discharging, full };
49 49
50 - fn read_battery_status(self: *Battery, path_buf: *[128]u8) ?Status {
50 + fn readBatteryStatus(self: *Battery, path_buf: *[128]u8) ?Status {
51 51 const path = std.fmt.bufPrint(path_buf, "/sys/class/power_supply/{s}/status", .{self.battery_name}) catch return null;
52 52
53 53 const file = std.fs.openFileAbsolute(path, .{}) catch return null;
@@ -64,7 +64,7 @@ pub const Battery = struct {
64 64 return .discharging;
65 65 }
66 66
67 - fn read_battery_file(self: *Battery, path_buf: *[128]u8, file_name: []const u8) ?u8 {
67 + fn readBatteryFile(self: *Battery, path_buf: *[128]u8, file_name: []const u8) ?u8 {
68 68 const path = std.fmt.bufPrint(path_buf, "/sys/class/power_supply/{s}/{s}", .{ self.battery_name, file_name }) catch return null;
69 69
70 70 const file = std.fs.openFileAbsolute(path, .{}) catch return null;
@@ -81,7 +81,7 @@ pub const Battery = struct {
81 81 return self.interval_secs;
82 82 }
83 83
84 - pub fn get_color(self: *Battery) c_ulong {
84 + pub fn getColor(self: *Battery) c_ulong {
85 85 return self.color;
86 86 }
87 87 };
+13 −13 src/bar/blocks/blocks.zig
@@ -1,11 +1,11 @@
1 1 const std = @import("std");
2 2
3 3 pub const Static = @import("static.zig").Static;
4 -pub const Date_Time = @import("datetime.zig").Date_Time;
4 +pub const DateTime = @import("datetime.zig").DateTime;
5 5 pub const Ram = @import("ram.zig").Ram;
6 6 pub const Shell = @import("shell.zig").Shell;
7 7 pub const Battery = @import("battery.zig").Battery;
8 -pub const Cpu_Temp = @import("cpu_temp.zig").Cpu_Temp;
8 +pub const CpuTemp = @import("cpu_temp.zig").CpuTemp;
9 9
10 10 pub const Block = struct {
11 11 data: Data,
@@ -16,14 +16,14 @@ pub const Block = struct {
16 16
17 17 pub const Data = union(enum) {
18 18 static: Static,
19 - datetime: Date_Time,
19 + datetime: DateTime,
20 20 ram: Ram,
21 21 shell: Shell,
22 22 battery: Battery,
23 - cpu_temp: Cpu_Temp,
23 + cpu_temp: CpuTemp,
24 24 };
25 25
26 - pub fn init_static(text: []const u8, col: c_ulong, ul: bool) Block {
26 + pub fn initStatic(text: []const u8, col: c_ulong, ul: bool) Block {
27 27 var block = Block{
28 28 .data = .{ .static = Static.init(text, col) },
29 29 .last_update = 0,
@@ -36,9 +36,9 @@ pub const Block = struct {
36 36 return block;
37 37 }
38 38
39 - pub fn init_datetime(format: []const u8, datetime_format: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block {
39 + pub fn initDatetime(format: []const u8, datetime_format: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block {
40 40 return .{
41 - .data = .{ .datetime = Date_Time.init(format, datetime_format, interval_secs, col) },
41 + .data = .{ .datetime = DateTime.init(format, datetime_format, interval_secs, col) },
42 42 .last_update = 0,
43 43 .cached_content = undefined,
44 44 .cached_len = 0,
@@ -46,7 +46,7 @@ pub const Block = struct {
46 46 };
47 47 }
48 48
49 - pub fn init_ram(format: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block {
49 + pub fn initRam(format: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block {
50 50 return .{
51 51 .data = .{ .ram = Ram.init(format, interval_secs, col) },
52 52 .last_update = 0,
@@ -56,7 +56,7 @@ pub const Block = struct {
56 56 };
57 57 }
58 58
59 - pub fn init_shell(format: []const u8, command: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block {
59 + pub fn initShell(format: []const u8, command: []const u8, interval_secs: u64, col: c_ulong, ul: bool) Block {
60 60 return .{
61 61 .data = .{ .shell = Shell.init(format, command, interval_secs, col) },
62 62 .last_update = 0,
@@ -66,7 +66,7 @@ pub const Block = struct {
66 66 };
67 67 }
68 68
69 - pub fn init_battery(
69 + pub fn initBattery(
70 70 format_charging: []const u8,
71 71 format_discharging: []const u8,
72 72 format_full: []const u8,
@@ -84,7 +84,7 @@ pub const Block = struct {
84 84 };
85 85 }
86 86
87 - pub fn init_cpu_temp(
87 + pub fn initCpuTemp(
88 88 format: []const u8,
89 89 thermal_zone: []const u8,
90 90 interval_secs: u64,
@@ -92,7 +92,7 @@ pub const Block = struct {
92 92 ul: bool,
93 93 ) Block {
94 94 return .{
95 - .data = .{ .cpu_temp = Cpu_Temp.init(format, thermal_zone, interval_secs, col) },
95 + .data = .{ .cpu_temp = CpuTemp.init(format, thermal_zone, interval_secs, col) },
96 96 .last_update = 0,
97 97 .cached_content = undefined,
98 98 .cached_len = 0,
@@ -146,7 +146,7 @@ pub const Block = struct {
146 146 };
147 147 }
148 148
149 - pub fn get_content(self: *const Block) []const u8 {
149 + pub fn getContent(self: *const Block) []const u8 {
150 150 return self.cached_content[0..self.cached_len];
151 151 }
152 152 };
+14 −14 src/bar/blocks/cpu_temp.zig
@@ -1,7 +1,7 @@
1 1 const std = @import("std");
2 2 const format_util = @import("format.zig");
3 3
4 -pub const Cpu_Temp = struct {
4 +pub const CpuTemp = struct {
5 5 format: []const u8,
6 6 device: []const u8,
7 7 interval_secs: u64,
@@ -14,8 +14,8 @@ pub const Cpu_Temp = struct {
14 14 device: []const u8,
15 15 interval_secs: u64,
16 16 color: c_ulong,
17 - ) Cpu_Temp {
18 - var self = Cpu_Temp{
17 + ) CpuTemp {
18 + var self = CpuTemp{
19 19 .format = format,
20 20 .device = device,
21 21 .interval_secs = interval_secs,
@@ -23,11 +23,11 @@ pub const Cpu_Temp = struct {
23 23 .cached_path = undefined,
24 24 .cached_path_len = 0,
25 25 };
26 - self.detect_path();
26 + self.detectPath();
27 27 return self;
28 28 }
29 29
30 - fn detect_path(self: *Cpu_Temp) void {
30 + fn detectPath(self: *CpuTemp) void {
31 31 if (self.device.len > 0 and self.device[0] == '/') {
32 32 if (self.device.len <= self.cached_path.len) {
33 33 @memcpy(self.cached_path[0..self.device.len], self.device);
@@ -37,16 +37,16 @@ pub const Cpu_Temp = struct {
37 37 }
38 38
39 39 if (self.device.len > 0) {
40 - if (self.try_path("/sys/class/thermal/{s}/temp", self.device)) return;
41 - if (self.try_path("/sys/class/hwmon/{s}/temp1_input", self.device)) return;
40 + if (self.tryPath("/sys/class/thermal/{s}/temp", self.device)) return;
41 + if (self.tryPath("/sys/class/hwmon/{s}/temp1_input", self.device)) return;
42 42 }
43 43
44 - if (self.find_hwmon_cpu()) return;
44 + if (self.findHwmonCpu()) return;
45 45
46 - if (self.try_path("/sys/class/thermal/{s}/temp", "thermal_zone0")) return;
46 + if (self.tryPath("/sys/class/thermal/{s}/temp", "thermal_zone0")) return;
47 47 }
48 48
49 - fn try_path(self: *Cpu_Temp, comptime fmt: []const u8, device: []const u8) bool {
49 + fn tryPath(self: *CpuTemp, comptime fmt: []const u8, device: []const u8) bool {
50 50 const path = std.fmt.bufPrint(&self.cached_path, fmt, .{device}) catch return false;
51 51 const file = std.fs.openFileAbsolute(path, .{}) catch return false;
52 52 file.close();
@@ -54,7 +54,7 @@ pub const Cpu_Temp = struct {
54 54 return true;
55 55 }
56 56
57 - fn find_hwmon_cpu(self: *Cpu_Temp) bool {
57 + fn findHwmonCpu(self: *CpuTemp) bool {
58 58 var dir = std.fs.openDirAbsolute("/sys/class/hwmon", .{ .iterate = true }) catch return false;
59 59 defer dir.close();
60 60
@@ -83,7 +83,7 @@ pub const Cpu_Temp = struct {
83 83 return false;
84 84 }
85 85
86 - pub fn content(self: *Cpu_Temp, buffer: []u8) []const u8 {
86 + pub fn content(self: *CpuTemp, buffer: []u8) []const u8 {
87 87 if (self.cached_path_len == 0) return buffer[0..0];
88 88
89 89 const path = self.cached_path[0..self.cached_path_len];
@@ -103,11 +103,11 @@ pub const Cpu_Temp = struct {
103 103 return format_util.substitute(self.format, deg_str, buffer);
104 104 }
105 105
106 - pub fn interval(self: *Cpu_Temp) u64 {
106 + pub fn interval(self: *CpuTemp) u64 {
107 107 return self.interval_secs;
108 108 }
109 109
110 - pub fn get_color(self: *Cpu_Temp) c_ulong {
110 + pub fn getColor(self: *CpuTemp) c_ulong {
111 111 return self.color;
112 112 }
113 113 };
+8 −8 src/bar/blocks/datetime.zig
@@ -4,13 +4,13 @@ const c = @cImport({
4 4 @cInclude("time.h");
5 5 });
6 6
7 -pub const Date_Time = struct {
7 +pub const DateTime = struct {
8 8 format: []const u8,
9 9 datetime_format: []const u8,
10 10 interval_secs: u64,
11 11 color: c_ulong,
12 12
13 - pub fn init(format: []const u8, datetime_format: []const u8, interval_secs: u64, color: c_ulong) Date_Time {
13 + pub fn init(format: []const u8, datetime_format: []const u8, interval_secs: u64, color: c_ulong) DateTime {
14 14 return .{
15 15 .format = format,
16 16 .datetime_format = datetime_format,
@@ -19,7 +19,7 @@ pub const Date_Time = struct {
19 19 };
20 20 }
21 21
22 - pub fn content(self: *Date_Time, buffer: []u8) []const u8 {
22 + pub fn content(self: *DateTime, buffer: []u8) []const u8 {
23 23 var now: c.time_t = c.time(null);
24 24 const tm_ptr = c.localtime(&now);
25 25 if (tm_ptr == null) return buffer[0..0];
@@ -42,10 +42,10 @@ pub const Date_Time = struct {
42 42 const next = self.datetime_format[fmt_idx + 1];
43 43 if (next == '-' and fmt_idx + 2 < self.datetime_format.len) {
44 44 const spec = self.datetime_format[fmt_idx + 2];
45 - dt_len += format_spec(spec, false, hours, minutes, seconds, day, month, year, dow, datetime_buf[dt_len..]);
45 + dt_len += formatSpec(spec, false, hours, minutes, seconds, day, month, year, dow, datetime_buf[dt_len..]);
46 46 fmt_idx += 3;
47 47 } else {
48 - dt_len += format_spec(next, true, hours, minutes, seconds, day, month, year, dow, datetime_buf[dt_len..]);
48 + dt_len += formatSpec(next, true, hours, minutes, seconds, day, month, year, dow, datetime_buf[dt_len..]);
49 49 fmt_idx += 2;
50 50 }
51 51 } else {
@@ -58,7 +58,7 @@ pub const Date_Time = struct {
58 58 return format_util.substitute(self.format, datetime_buf[0..dt_len], buffer);
59 59 }
60 60
61 - fn format_spec(spec: u8, pad: bool, hours: u32, minutes: u32, seconds: u32, day: u8, month: u8, year: i32, dow: i32, buf: []u8) usize {
61 + fn formatSpec(spec: u8, pad: bool, hours: u32, minutes: u32, seconds: u32, day: u8, month: u8, year: i32, dow: i32, buf: []u8) usize {
62 62 const day_names = [_][]const u8{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
63 63 const month_names = [_][]const u8{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
64 64
@@ -97,11 +97,11 @@ pub const Date_Time = struct {
97 97 };
98 98 }
99 99
100 - pub fn interval(self: *Date_Time) u64 {
100 + pub fn interval(self: *DateTime) u64 {
101 101 return self.interval_secs;
102 102 }
103 103
104 - pub fn get_color(self: *Date_Time) c_ulong {
104 + pub fn getColor(self: *DateTime) c_ulong {
105 105 return self.color;
106 106 }
107 107 };
+1 −1 src/bar/blocks/format.zig
@@ -21,7 +21,7 @@ pub fn substitute(format: []const u8, value: []const u8, buffer: []u8) []const u
21 21 return buffer[0..out_idx];
22 22 }
23 23
24 -pub fn substitute_multi(format: []const u8, values: []const []const u8, buffer: []u8) []const u8 {
24 +pub fn substituteMulti(format: []const u8, values: []const []const u8, buffer: []u8) []const u8 {
25 25 if (format.len == 0 and values.len > 0) {
26 26 const len = @min(values[0].len, buffer.len);
27 27 @memcpy(buffer[0..len], values[0][0..len]);
+4 −4 src/bar/blocks/ram.zig
@@ -27,9 +27,9 @@ pub const Ram = struct {
27 27 var lines = std.mem.splitScalar(u8, file_content, '\n');
28 28 while (lines.next()) |line| {
29 29 if (std.mem.startsWith(u8, line, "MemTotal:")) {
30 - total = parse_mem_value(line);
30 + total = parseMemValue(line);
31 31 } else if (std.mem.startsWith(u8, line, "MemAvailable:")) {
32 - available = parse_mem_value(line);
32 + available = parseMemValue(line);
33 33 }
34 34 }
35 35
@@ -56,12 +56,12 @@ pub const Ram = struct {
56 56 return self.interval_secs;
57 57 }
58 58
59 - pub fn get_color(self: *Ram) c_ulong {
59 + pub fn getColor(self: *Ram) c_ulong {
60 60 return self.color;
61 61 }
62 62 };
63 63
64 -fn parse_mem_value(line: []const u8) u64 {
64 +fn parseMemValue(line: []const u8) u64 {
65 65 var iter = std.mem.tokenizeAny(u8, line, ": \tkB");
66 66 _ = iter.next();
67 67 if (iter.next()) |value_str| {
+1 −1 src/bar/blocks/shell.zig
@@ -39,7 +39,7 @@ pub const Shell = struct {
39 39 return self.interval_secs;
40 40 }
41 41
42 - pub fn get_color(self: *Shell) c_ulong {
42 + pub fn getColor(self: *Shell) c_ulong {
43 43 return self.color;
44 44 }
45 45 };
+1 −1 src/bar/blocks/static.zig
@@ -18,7 +18,7 @@ pub const Static = struct {
18 18 return 0;
19 19 }
20 20
21 - pub fn get_color(self: *Static) c_ulong {
21 + pub fn getColor(self: *Static) c_ulong {
22 22 return self.color;
23 23 }
24 24 };
+58 −65 src/client.zig
@@ -38,22 +38,19 @@ pub const Client = struct {
38 38 window: xlib.Window = 0,
39 39 };
40 40
41 -var allocator: std.mem.Allocator = undefined;
42 -
43 -pub fn init(alloc: std.mem.Allocator) void {
44 - allocator = alloc;
45 -}
46 -
47 -pub fn create(window: xlib.Window) ?*Client {
41 +/// Initialises a new `Client` for the given X window.
42 +pub fn create(allocator: std.mem.Allocator, window: xlib.Window) ?*Client {
48 43 const client = allocator.create(Client) catch return null;
49 44 client.* = Client{ .window = window };
50 45 return client;
51 46 }
52 47
53 -pub fn destroy(client: *Client) void {
48 +/// Frees a client previously returned by `create`.
49 +pub fn destroy(allocator: std.mem.Allocator, client: *Client) void {
54 50 allocator.destroy(client);
55 51 }
56 52
53 +/// Prepends `client` to the front of its monitor's client list.
57 54 pub fn attach(client: *Client) void {
58 55 if (client.monitor) |monitor| {
59 56 client.next = monitor.clients;
@@ -61,6 +58,7 @@ pub fn attach(client: *Client) void {
61 58 }
62 59 }
63 60
61 +/// Removes `client` from its monitor's client list.
64 62 pub fn detach(client: *Client) void {
65 63 if (client.monitor) |monitor| {
66 64 var current_ptr: *?*Client = &monitor.clients;
@@ -74,14 +72,16 @@ pub fn detach(client: *Client) void {
74 72 }
75 73 }
76 74
77 -pub fn attach_stack(client: *Client) void {
75 +/// Prepends `client` to the front of its monitor's focus stack.
76 +pub fn attachStack(client: *Client) void {
78 77 if (client.monitor) |monitor| {
79 78 client.stack_next = monitor.stack;
80 79 monitor.stack = client;
81 80 }
82 81 }
83 82
84 -pub fn detach_stack(client: *Client) void {
83 +/// Removes `client` from its monitor's focus stack.
84 +pub fn detachStack(client: *Client) void {
85 85 if (client.monitor) |monitor| {
86 86 var current_ptr: *?*Client = &monitor.stack;
87 87 while (current_ptr.*) |current| {
@@ -94,15 +94,15 @@ pub fn detach_stack(client: *Client) void {
94 94 }
95 95 }
96 96
97 -pub fn window_to_client(window: xlib.Window) ?*Client {
98 - const monitor_mod = @import("monitor.zig");
99 - var current_monitor = monitor_mod.monitors;
97 +/// Searches all monitors for a client whose X window matches `window`.
98 +///
99 +/// `monitors` is the head of the monitor linked list.
100 +pub fn windowToClient(monitors: ?*Monitor, window: xlib.Window) ?*Client {
101 + var current_monitor = monitors;
100 102 while (current_monitor) |monitor| {
101 103 var current_client = monitor.clients;
102 104 while (current_client) |client| {
103 - if (client.window == window) {
104 - return client;
105 - }
105 + if (client.window == window) return client;
106 106 current_client = client.next;
107 107 }
108 108 current_monitor = monitor.next;
@@ -110,42 +110,46 @@ pub fn window_to_client(window: xlib.Window) ?*Client {
110 110 return null;
111 111 }
112 112
113 -pub fn next_tiled(client: ?*Client) ?*Client {
114 - var current = client;
115 - while (current) |iter| {
116 - if (!iter.is_floating and is_visible(iter)) {
117 - return iter;
118 - }
119 - current = iter.next;
120 - }
121 - return null;
122 -}
123 -
124 -pub fn is_visible(client: *Client) bool {
113 +/// Returns true if `client` is visible on its monitor's currently selected
114 +/// tag set.
115 +pub fn isVisible(client: *Client) bool {
125 116 if (client.monitor) |monitor| {
126 117 return (client.tags & monitor.tagset[monitor.sel_tags]) != 0;
127 118 }
128 119 return false;
129 120 }
130 121
131 -pub fn is_visible_on_tag(client: *Client, tags: u32) bool {
122 +/// Returns true if `client` is visible on the given tag bitmask.
123 +pub fn isVisibleOnTag(client: *Client, tags: u32) bool {
132 124 return (client.tags & tags) != 0;
133 125 }
134 126
135 -pub fn next_tagged(client: *Client) ?*Client {
127 +/// Returns the first non-floating, visible client at or after `client`.
128 +pub fn nextTiled(client: ?*Client) ?*Client {
129 + var current = client;
130 + while (current) |iter| {
131 + if (!iter.is_floating and isVisible(iter)) return iter;
132 + current = iter.next;
133 + }
134 + return null;
135 +}
136 +
137 +/// Returns the first non-floating client on `client`'s monitor that shares
138 +/// any tag with `client`. Used for `attach_aside` ordering.
139 +pub fn nextTagged(client: *Client) ?*Client {
136 140 const monitor = client.monitor orelse return null;
137 141 var walked = monitor.clients;
138 142 while (walked) |iter| {
139 - if (!iter.is_floating and is_visible_on_tag(iter, client.tags)) {
140 - return iter;
141 - }
143 + if (!iter.is_floating and isVisibleOnTag(iter, client.tags)) return iter;
142 144 walked = iter.next;
143 145 }
144 146 return null;
145 147 }
146 148
147 -pub fn attach_aside(client: *Client) void {
148 - const at = next_tagged(client);
149 +/// Inserts `client` just after the first client that shares its tags,
150 +/// falling back to prepend if none exists.
151 +pub fn attachAside(client: *Client) void {
152 + const at = nextTagged(client);
149 153 if (at == null) {
150 154 attach(client);
151 155 return;
@@ -154,29 +158,30 @@ pub fn attach_aside(client: *Client) void {
154 158 at.?.next = client;
155 159 }
156 160
157 -pub fn count_tiled(monitor: *Monitor) u32 {
161 +/// Counts non-floating, visible clients on `monitor`.
162 +pub fn countTiled(monitor: *Monitor) u32 {
158 163 var count: u32 = 0;
159 - var current = next_tiled(monitor.clients);
164 + var current = nextTiled(monitor.clients);
160 165 while (current) |client| {
161 166 count += 1;
162 - current = next_tiled(client.next);
167 + current = nextTiled(client.next);
163 168 }
164 169 return count;
165 170 }
166 171
167 -pub fn tiled_window_at(exclude: *Client, monitor: *Monitor, point_x: i32, point_y: i32) ?*Client {
172 +/// Returns the tiled client on `monitor` whose bounds contain (`point_x`,
173 +/// `point_y`), excluding `exclude`. Returns null if none found.
174 +pub fn tiledWindowAt(exclude: *Client, monitor: *Monitor, point_x: i32, point_y: i32) ?*Client {
168 175 const tags = monitor.tagset[monitor.sel_tags];
169 176 var current = monitor.clients;
170 177
171 178 while (current) |client| {
172 179 if (client != exclude and !client.is_floating and (client.tags & tags) != 0) {
173 - const client_x = client.x;
174 - const client_y = client.y;
175 180 const client_w = client.width + client.border_width * 2;
176 181 const client_h = client.height + client.border_width * 2;
177 182
178 - if (point_x >= client_x and point_x < client_x + client_w and
179 - point_y >= client_y and point_y < client_y + client_h)
183 + if (point_x >= client.x and point_x < client.x + client_w and
184 + point_y >= client.y and point_y < client.y + client_h)
180 185 {
181 186 return client;
182 187 }
@@ -186,7 +191,9 @@ pub fn tiled_window_at(exclude: *Client, monitor: *Monitor, point_x: i32, point_
186 191 return null;
187 192 }
188 193
189 -pub fn insert_before(client: *Client, target: *Client) void {
194 +/// Moves `client` to just before `target` in the monitor's client list.
195 +/// Does nothing if they are on different monitors.
196 +pub fn insertBefore(client: *Client, target: *Client) void {
190 197 const monitor = target.monitor orelse return;
191 198 if (client.monitor != monitor) return;
192 199
@@ -209,7 +216,9 @@ pub fn insert_before(client: *Client, target: *Client) void {
209 216 }
210 217 }
211 218
212 -pub fn swap_clients(client_a: *Client, client_b: *Client) void {
219 +/// Swaps the positions of `client_a` and `client_b` in their shared monitor's
220 +/// client list. Does nothing if they are on different monitors.
221 +pub fn swapClients(client_a: *Client, client_b: *Client) void {
213 222 const monitor = client_a.monitor orelse return;
214 223 if (client_b.monitor != monitor) return;
215 224
@@ -229,31 +238,15 @@ pub fn swap_clients(client_a: *Client, client_b: *Client) void {
229 238 if (next_a == client_b) {
230 239 client_a.next = next_b;
231 240 client_b.next = client_a;
232 - if (prev_a) |prev| {
233 - prev.next = client_b;
234 - } else {
235 - monitor.clients = client_b;
236 - }
241 + if (prev_a) |prev| prev.next = client_b else monitor.clients = client_b;
237 242 } else if (next_b == client_a) {
238 243 client_b.next = next_a;
239 244 client_a.next = client_b;
240 - if (prev_b) |prev| {
241 - prev.next = client_a;
242 - } else {
243 - monitor.clients = client_a;
244 - }
245 + if (prev_b) |prev| prev.next = client_a else monitor.clients = client_a;
245 246 } else {
246 247 client_a.next = next_b;
247 248 client_b.next = next_a;
248 - if (prev_a) |prev| {
249 - prev.next = client_b;
250 - } else {
251 - monitor.clients = client_b;
252 - }
253 - if (prev_b) |prev| {
254 - prev.next = client_a;
255 - } else {
256 - monitor.clients = client_a;
257 - }
249 + if (prev_a) |prev| prev.next = client_b else monitor.clients = client_b;
250 + if (prev_b) |prev| prev.next = client_a else monitor.clients = client_a;
258 251 }
259 252 }
+70 −20 src/config/config.zig
@@ -36,13 +36,13 @@ pub const Action = enum {
36 36 scroll_right,
37 37 };
38 38
39 -pub const Key_Press = struct {
39 +pub const KeyPress = struct {
40 40 mod_mask: u32 = 0,
41 41 keysym: u64 = 0,
42 42 };
43 43
44 44 pub const Keybind = struct {
45 - keys: [4]Key_Press = [_]Key_Press{.{}} ** 4,
45 + keys: [4]KeyPress = [_]KeyPress{.{}} ** 4,
46 46 key_count: u8 = 1,
47 47 action: Action,
48 48 int_arg: i32 = 0,
@@ -59,7 +59,7 @@ pub const Rule = struct {
59 59 focus: bool,
60 60 };
61 61
62 -pub const Block_Type = enum {
62 +pub const BlockType = enum {
63 63 static,
64 64 datetime,
65 65 ram,
@@ -68,27 +68,27 @@ pub const Block_Type = enum {
68 68 cpu_temp,
69 69 };
70 70
71 -pub const Click_Target = enum {
71 +pub const ClickTarget = enum {
72 72 client_win,
73 73 root_win,
74 74 tag_bar,
75 75 };
76 76
77 -pub const Mouse_Action = enum {
77 +pub const MouseAction = enum {
78 78 move_mouse,
79 79 resize_mouse,
80 80 toggle_floating,
81 81 };
82 82
83 -pub const Mouse_Button = struct {
84 - click: Click_Target,
83 +pub const MouseButton = struct {
84 + click: ClickTarget,
85 85 mod_mask: u32,
86 86 button: u32,
87 - action: Mouse_Action,
87 + action: MouseAction,
88 88 };
89 89
90 90 pub const Block = struct {
91 - block_type: Block_Type,
91 + block_type: BlockType,
92 92 format: []const u8,
93 93 command: ?[]const u8 = null,
94 94 interval: u32,
@@ -145,7 +145,7 @@ pub const Config = struct {
145 145 keybinds: std.ArrayListUnmanaged(Keybind) = .{},
146 146 rules: std.ArrayListUnmanaged(Rule) = .{},
147 147 blocks: std.ArrayListUnmanaged(Block) = .{},
148 - buttons: std.ArrayListUnmanaged(Mouse_Button) = .{},
148 + buttons: std.ArrayListUnmanaged(MouseButton) = .{},
149 149 autostart: std.ArrayListUnmanaged([]const u8) = .{},
150 150
151 151 pub fn init(allocator: std.mem.Allocator) Config {
@@ -164,33 +164,83 @@ pub const Config = struct {
164 164 self.autostart.deinit(self.allocator);
165 165 }
166 166
167 - pub fn add_keybind(self: *Config, keybind: Keybind) !void {
167 + pub fn addKeybind(self: *Config, keybind: Keybind) !void {
168 168 try self.keybinds.append(self.allocator, keybind);
169 169 }
170 170
171 - pub fn add_rule(self: *Config, rule: Rule) !void {
171 + pub fn addRule(self: *Config, rule: Rule) !void {
172 172 try self.rules.append(self.allocator, rule);
173 173 }
174 174
175 - pub fn add_block(self: *Config, block: Block) !void {
175 + pub fn addBlock(self: *Config, block: Block) !void {
176 176 try self.blocks.append(self.allocator, block);
177 177 }
178 178
179 - pub fn add_button(self: *Config, button: Mouse_Button) !void {
179 + pub fn addButton(self: *Config, button: MouseButton) !void {
180 180 try self.buttons.append(self.allocator, button);
181 181 }
182 182
183 - pub fn add_autostart(self: *Config, cmd: []const u8) !void {
183 + pub fn addAutostart(self: *Config, cmd: []const u8) !void {
184 184 try self.autostart.append(self.allocator, cmd);
185 185 }
186 186 };
187 187
188 -pub var global_config: ?*Config = null;
188 +pub fn initializeDefaultConfig(cfg: *Config) void {
189 + const mod_key: u32 = 1 << 6;
190 + const shift_key: u32 = 1 << 0;
191 + const control_key: u32 = 1 << 2;
192 +
193 + cfg.addKeybind(makeKeybind(mod_key, 0xff0d, .spawn_terminal)) catch {};
194 + cfg.addKeybind(makeKeybindStr(mod_key, 'd', .spawn, "rofi -show drun")) catch {};
195 + cfg.addKeybind(makeKeybindStr(mod_key, 's', .spawn, "maim -s | xclip -selection clipboard -t image/png")) catch {};
196 + cfg.addKeybind(makeKeybind(mod_key, 'q', .kill_client)) catch {};
197 + cfg.addKeybind(makeKeybind(mod_key | shift_key, 'q', .quit)) catch {};
198 + cfg.addKeybind(makeKeybind(mod_key | shift_key, 'r', .reload_config)) catch {};
199 + cfg.addKeybind(makeKeybind(mod_key, 'j', .focus_next)) catch {};
200 + cfg.addKeybind(makeKeybind(mod_key, 'k', .focus_prev)) catch {};
201 + cfg.addKeybind(makeKeybind(mod_key | shift_key, 'j', .move_next)) catch {};
202 + cfg.addKeybind(makeKeybind(mod_key | shift_key, 'k', .move_prev)) catch {};
203 + cfg.addKeybind(makeKeybindInt(mod_key, 'h', .resize_master, -50)) catch {};
204 + cfg.addKeybind(makeKeybindInt(mod_key, 'l', .resize_master, 50)) catch {};
205 + cfg.addKeybind(makeKeybind(mod_key, 'i', .inc_master)) catch {};
206 + cfg.addKeybind(makeKeybind(mod_key, 'p', .dec_master)) catch {};
207 + cfg.addKeybind(makeKeybind(mod_key, 'a', .toggle_gaps)) catch {};
208 + cfg.addKeybind(makeKeybind(mod_key, 'f', .toggle_fullscreen)) catch {};
209 + cfg.addKeybind(makeKeybind(mod_key, 0x0020, .toggle_floating)) catch {};
210 + cfg.addKeybind(makeKeybind(mod_key, 'n', .cycle_layout)) catch {};
211 + cfg.addKeybind(makeKeybindInt(mod_key, 0x002c, .focus_monitor, -1)) catch {};
212 + cfg.addKeybind(makeKeybindInt(mod_key, 0x002e, .focus_monitor, 1)) catch {};
213 + cfg.addKeybind(makeKeybindInt(mod_key | shift_key, 0x002c, .send_to_monitor, -1)) catch {};
214 + cfg.addKeybind(makeKeybindInt(mod_key | shift_key, 0x002e, .send_to_monitor, 1)) catch {};
215 +
216 + var tag_index: i32 = 0;
217 + while (tag_index < 9) : (tag_index += 1) {
218 + const keysym: u64 = @as(u64, '1') + @as(u64, @intCast(tag_index));
219 + cfg.addKeybind(makeKeybindInt(mod_key, keysym, .view_tag, tag_index)) catch {};
220 + cfg.addKeybind(makeKeybindInt(mod_key | shift_key, keysym, .move_to_tag, tag_index)) catch {};
221 + cfg.addKeybind(makeKeybindInt(mod_key | control_key, keysym, .toggle_view_tag, tag_index)) catch {};
222 + cfg.addKeybind(makeKeybindInt(mod_key | control_key | shift_key, keysym, .toggle_tag, tag_index)) catch {};
223 + }
224 +
225 + cfg.addButton(.{ .click = .client_win, .mod_mask = mod_key, .button = 1, .action = .move_mouse }) catch {};
226 + cfg.addButton(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {};
227 +}
228 +
229 +fn makeKeybind(mod: u32, key: u64, action: Action) Keybind {
230 + var kb: Keybind = .{ .action = action };
231 + kb.keys[0] = .{ .mod_mask = mod, .keysym = key };
232 + kb.key_count = 1;
233 + return kb;
234 +}
189 235
190 -pub fn get_config() ?*Config {
191 - return global_config;
236 +fn makeKeybindInt(mod: u32, key: u64, action: Action, int_arg: i32) Keybind {
237 + var kb = makeKeybind(mod, key, action);
238 + kb.int_arg = int_arg;
239 + return kb;
192 240 }
193 241
194 -pub fn set_config(cfg: *Config) void {
195 - global_config = cfg;
242 +fn makeKeybindStr(mod: u32, key: u64, action: Action, str_arg: []const u8) Keybind {
243 + var kb = makeKeybind(mod, key, action);
244 + kb.str_arg = str_arg;
245 + return kb;
196 246 }
+256 −260 src/config/lua.zig Large diff collapsed, click to expand
@@ -5,10 +5,6 @@ const Keybind = config_mod.Keybind;
5 5 const Action = config_mod.Action;
6 6 const Rule = config_mod.Rule;
7 7 const Block = config_mod.Block;
8 -const Block_Type = config_mod.Block_Type;
9 -const Mouse_Button = config_mod.Mouse_Button;
10 -const Click_Target = config_mod.Click_Target;
11 -const Mouse_Action = config_mod.Mouse_Action;
12 8 const ColorScheme = config_mod.ColorScheme;
13 9
14 10 const c = @cImport({
@@ -25,7 +21,7 @@ pub fn init(cfg: *Config) bool {
25 21 L = c.luaL_newstate();
26 22 if (L == null) return false;
27 23 c.luaL_openlibs(L);
28 - register_api();
24 + registerApi();
29 25 return true;
30 26 }
31 27
@@ -37,7 +33,7 @@ pub fn deinit() void {
37 33 config = null;
38 34 }
39 35
40 -pub fn load_file(path: []const u8) bool {
36 +pub fn loadFile(path: []const u8) bool {
41 37 const state = L orelse return false;
42 38 var path_buf: [512]u8 = undefined;
43 39 if (path.len >= path_buf.len) return false;
@@ -75,224 +71,224 @@ pub fn load_file(path: []const u8) bool {
75 71 return true;
76 72 }
77 73
78 -pub fn load_config() bool {
74 +pub fn loadConfig() bool {
79 75 const home = std.posix.getenv("HOME") orelse return false;
80 76 var path_buf: [512]u8 = undefined;
81 77 const path = std.fmt.bufPrint(&path_buf, "{s}/.config/oxwm/config.lua", .{home}) catch return false;
82 - return load_file(path);
78 + return loadFile(path);
83 79 }
84 80
85 -fn register_api() void {
81 +fn registerApi() void {
86 82 const state = L orelse return;
87 83
88 84 c.lua_createtable(state, 0, 16);
89 85
90 - register_spawn_functions(state);
91 - register_key_module(state);
92 - register_gaps_module(state);
93 - register_border_module(state);
94 - register_client_module(state);
95 - register_layout_module(state);
96 - register_tag_module(state);
97 - register_monitor_module(state);
98 - register_rule_module(state);
99 - register_bar_module(state);
100 - register_misc_functions(state);
86 + registerSpawnFunctions(state);
87 + registerKeyModule(state);
88 + registerGapsModule(state);
89 + registerBorderModule(state);
90 + registerClientModule(state);
91 + registerLayoutModule(state);
92 + registerTagModule(state);
93 + registerMonitorModule(state);
94 + registerRuleModule(state);
95 + registerBarModule(state);
96 + registerMiscFunctions(state);
101 97
102 98 c.lua_setglobal(state, "oxwm");
103 99 }
104 100
105 -fn register_spawn_functions(state: *c.lua_State) void {
106 - c.lua_pushcfunction(state, lua_spawn);
101 +fn registerSpawnFunctions(state: *c.lua_State) void {
102 + c.lua_pushcfunction(state, luaSpawn);
107 103 c.lua_setfield(state, -2, "spawn");
108 104
109 - c.lua_pushcfunction(state, lua_spawn_terminal);
105 + c.lua_pushcfunction(state, luaSpawnTerminal);
110 106 c.lua_setfield(state, -2, "spawn_terminal");
111 107 }
112 108
113 -fn register_key_module(state: *c.lua_State) void {
109 +fn registerKeyModule(state: *c.lua_State) void {
114 110 c.lua_createtable(state, 0, 2);
115 111
116 - c.lua_pushcfunction(state, lua_key_bind);
112 + c.lua_pushcfunction(state, luaKeyBind);
117 113 c.lua_setfield(state, -2, "bind");
118 114
119 - c.lua_pushcfunction(state, lua_key_chord);
115 + c.lua_pushcfunction(state, luaKeyChord);
120 116 c.lua_setfield(state, -2, "chord");
121 117
122 118 c.lua_setfield(state, -2, "key");
123 119 }
124 120
125 -fn register_gaps_module(state: *c.lua_State) void {
121 +fn registerGapsModule(state: *c.lua_State) void {
126 122 c.lua_createtable(state, 0, 6);
127 123
128 - c.lua_pushcfunction(state, lua_gaps_set_enabled);
124 + c.lua_pushcfunction(state, luaGapsSetEnabled);
129 125 c.lua_setfield(state, -2, "set_enabled");
130 126
131 - c.lua_pushcfunction(state, lua_gaps_enable);
127 + c.lua_pushcfunction(state, luaGapsEnable);
132 128 c.lua_setfield(state, -2, "enable");
133 129
134 - c.lua_pushcfunction(state, lua_gaps_disable);
130 + c.lua_pushcfunction(state, luaGapsDisable);
135 131 c.lua_setfield(state, -2, "disable");
136 132
137 - c.lua_pushcfunction(state, lua_gaps_set_inner);
133 + c.lua_pushcfunction(state, luaGapsSetInner);
138 134 c.lua_setfield(state, -2, "set_inner");
139 135
140 - c.lua_pushcfunction(state, lua_gaps_set_outer);
136 + c.lua_pushcfunction(state, luaGapsSetOuter);
141 137 c.lua_setfield(state, -2, "set_outer");
142 138
143 - c.lua_pushcfunction(state, lua_gaps_set_smart);
139 + c.lua_pushcfunction(state, luaGapsSetSmart);
144 140 c.lua_setfield(state, -2, "set_smart");
145 141
146 142 c.lua_setfield(state, -2, "gaps");
147 143 }
148 144
149 -fn register_border_module(state: *c.lua_State) void {
145 +fn registerBorderModule(state: *c.lua_State) void {
150 146 c.lua_createtable(state, 0, 3);
151 147
152 - c.lua_pushcfunction(state, lua_border_set_width);
148 + c.lua_pushcfunction(state, luaBorderSetWidth);
153 149 c.lua_setfield(state, -2, "set_width");
154 150
155 - c.lua_pushcfunction(state, lua_border_set_focused_color);
151 + c.lua_pushcfunction(state, luaBorderSetFocusedColor);
156 152 c.lua_setfield(state, -2, "set_focused_color");
157 153
158 - c.lua_pushcfunction(state, lua_border_set_unfocused_color);
154 + c.lua_pushcfunction(state, luaBorderSetUnfocusedColor);
159 155 c.lua_setfield(state, -2, "set_unfocused_color");
160 156
161 157 c.lua_setfield(state, -2, "border");
162 158 }
163 159
164 -fn register_client_module(state: *c.lua_State) void {
160 +fn registerClientModule(state: *c.lua_State) void {
165 161 c.lua_createtable(state, 0, 5);
166 162
167 - c.lua_pushcfunction(state, lua_client_kill);
163 + c.lua_pushcfunction(state, luaClientKill);
168 164 c.lua_setfield(state, -2, "kill");
169 165
170 - c.lua_pushcfunction(state, lua_client_toggle_fullscreen);
166 + c.lua_pushcfunction(state, luaClientToggleFullscreen);
171 167 c.lua_setfield(state, -2, "toggle_fullscreen");
172 168
173 - c.lua_pushcfunction(state, lua_client_toggle_floating);
169 + c.lua_pushcfunction(state, luaClientToggleFloating);
174 170 c.lua_setfield(state, -2, "toggle_floating");
175 171
176 - c.lua_pushcfunction(state, lua_client_focus_stack);
172 + c.lua_pushcfunction(state, luaClientFocusStack);
177 173 c.lua_setfield(state, -2, "focus_stack");
178 174
179 - c.lua_pushcfunction(state, lua_client_move_stack);
175 + c.lua_pushcfunction(state, luaClientMoveStack);
180 176 c.lua_setfield(state, -2, "move_stack");
181 177
182 178 c.lua_setfield(state, -2, "client");
183 179 }
184 180
185 -fn register_layout_module(state: *c.lua_State) void {
181 +fn registerLayoutModule(state: *c.lua_State) void {
186 182 c.lua_createtable(state, 0, 4);
187 183
188 - c.lua_pushcfunction(state, lua_layout_cycle);
184 + c.lua_pushcfunction(state, luaLayoutCycle);
189 185 c.lua_setfield(state, -2, "cycle");
190 186
191 - c.lua_pushcfunction(state, lua_layout_set);
187 + c.lua_pushcfunction(state, luaLayoutSet);
192 188 c.lua_setfield(state, -2, "set");
193 189
194 - c.lua_pushcfunction(state, lua_layout_scroll_left);
190 + c.lua_pushcfunction(state, luaLayoutScrollLeft);
195 191 c.lua_setfield(state, -2, "scroll_left");
196 192
197 - c.lua_pushcfunction(state, lua_layout_scroll_right);
193 + c.lua_pushcfunction(state, luaLayoutScrollRight);
198 194 c.lua_setfield(state, -2, "scroll_right");
199 195
200 196 c.lua_setfield(state, -2, "layout");
201 197 }
202 198
203 -fn register_tag_module(state: *c.lua_State) void {
199 +fn registerTagModule(state: *c.lua_State) void {
204 200 c.lua_createtable(state, 0, 10);
205 201
206 - c.lua_pushcfunction(state, lua_tag_view);
202 + c.lua_pushcfunction(state, luaTagView);
207 203 c.lua_setfield(state, -2, "view");
208 204
209 - c.lua_pushcfunction(state, lua_tag_view_next);
205 + c.lua_pushcfunction(state, luaTagViewNext);
210 206 c.lua_setfield(state, -2, "view_next");
211 207
212 - c.lua_pushcfunction(state, lua_tag_view_previous);
208 + c.lua_pushcfunction(state, luaTagViewPrevious);
213 209 c.lua_setfield(state, -2, "view_previous");
214 210
215 - c.lua_pushcfunction(state, lua_tag_view_next_nonempty);
211 + c.lua_pushcfunction(state, luaTagViewNextNonempty);
216 212 c.lua_setfield(state, -2, "view_next_nonempty");
217 213
218 - c.lua_pushcfunction(state, lua_tag_view_previous_nonempty);
214 + c.lua_pushcfunction(state, luaTagViewPreviousNonempty);
219 215 c.lua_setfield(state, -2, "view_previous_nonempty");
220 216
221 - c.lua_pushcfunction(state, lua_tag_toggleview);
217 + c.lua_pushcfunction(state, luaTagToggleview);
222 218 c.lua_setfield(state, -2, "toggleview");
223 219
224 - c.lua_pushcfunction(state, lua_tag_move_to);
220 + c.lua_pushcfunction(state, luaTagMoveTo);
225 221 c.lua_setfield(state, -2, "move_to");
226 222
227 - c.lua_pushcfunction(state, lua_tag_toggletag);
223 + c.lua_pushcfunction(state, luaTagToggletag);
228 224 c.lua_setfield(state, -2, "toggletag");
229 225
230 - c.lua_pushcfunction(state, lua_tag_set_back_and_forth);
226 + c.lua_pushcfunction(state, luaTagSetBackAndForth);
231 227 c.lua_setfield(state, -2, "set_back_and_forth");
232 228
233 229 c.lua_setfield(state, -2, "tag");
234 230 }
235 231
236 -fn register_monitor_module(state: *c.lua_State) void {
232 +fn registerMonitorModule(state: *c.lua_State) void {
237 233 c.lua_createtable(state, 0, 2);
238 234
239 - c.lua_pushcfunction(state, lua_monitor_focus);
235 + c.lua_pushcfunction(state, luaMonitorFocus);
240 236 c.lua_setfield(state, -2, "focus");
241 237
242 - c.lua_pushcfunction(state, lua_monitor_tag);
238 + c.lua_pushcfunction(state, luaMonitorTag);
243 239 c.lua_setfield(state, -2, "tag");
244 240
245 241 c.lua_setfield(state, -2, "monitor");
246 242 }
247 243
248 -fn register_rule_module(state: *c.lua_State) void {
244 +fn registerRuleModule(state: *c.lua_State) void {
249 245 c.lua_createtable(state, 0, 1);
250 246
251 - c.lua_pushcfunction(state, lua_rule_add);
247 + c.lua_pushcfunction(state, luaRuleAdd);
252 248 c.lua_setfield(state, -2, "add");
253 249
254 250 c.lua_setfield(state, -2, "rule");
255 251 }
256 252
257 -fn register_bar_module(state: *c.lua_State) void {
253 +fn registerBarModule(state: *c.lua_State) void {
258 254 c.lua_createtable(state, 0, 10);
259 255
260 - c.lua_pushcfunction(state, lua_bar_set_font);
256 + c.lua_pushcfunction(state, luaBarSetFont);
261 257 c.lua_setfield(state, -2, "set_font");
262 258
263 - c.lua_pushcfunction(state, lua_bar_set_blocks);
259 + c.lua_pushcfunction(state, luaBarSetBlocks);
264 260 c.lua_setfield(state, -2, "set_blocks");
265 261
266 - c.lua_pushcfunction(state, lua_bar_set_scheme_normal);
262 + c.lua_pushcfunction(state, luaBarSetSchemeNormal);
267 263 c.lua_setfield(state, -2, "set_scheme_normal");
268 264
269 - c.lua_pushcfunction(state, lua_bar_set_scheme_selected);
265 + c.lua_pushcfunction(state, luaBarSetSchemeSelected);
270 266 c.lua_setfield(state, -2, "set_scheme_selected");
271 267
272 - c.lua_pushcfunction(state, lua_bar_set_scheme_occupied);
268 + c.lua_pushcfunction(state, luaBarSetSchemeOccupied);
273 269 c.lua_setfield(state, -2, "set_scheme_occupied");
274 270
275 - c.lua_pushcfunction(state, lua_bar_set_scheme_urgent);
271 + c.lua_pushcfunction(state, luaBarSetSchemeUrgent);
276 272 c.lua_setfield(state, -2, "set_scheme_urgent");
277 273
278 - c.lua_pushcfunction(state, lua_bar_set_hide_vacant_tags);
274 + c.lua_pushcfunction(state, luaBarSetHideVacantTags);
279 275 c.lua_setfield(state, -2, "set_hide_vacant_tags");
280 276
281 277 c.lua_createtable(state, 0, 6);
282 278
283 - c.lua_pushcfunction(state, lua_bar_block_ram);
279 + c.lua_pushcfunction(state, luaBarBlockRam);
284 280 c.lua_setfield(state, -2, "ram");
285 281
286 - c.lua_pushcfunction(state, lua_bar_block_datetime);
282 + c.lua_pushcfunction(state, luaBarBlockDatetime);
287 283 c.lua_setfield(state, -2, "datetime");
288 284
289 - c.lua_pushcfunction(state, lua_bar_block_shell);
285 + c.lua_pushcfunction(state, luaBarBlockShell);
290 286 c.lua_setfield(state, -2, "shell");
291 287
292 - c.lua_pushcfunction(state, lua_bar_block_static);
288 + c.lua_pushcfunction(state, luaBarBlockStatic);
293 289 c.lua_setfield(state, -2, "static");
294 290
295 - c.lua_pushcfunction(state, lua_bar_block_battery);
291 + c.lua_pushcfunction(state, luaBarBlockBattery);
296 292 c.lua_setfield(state, -2, "battery");
297 293
298 294 c.lua_setfield(state, -2, "block");
@@ -300,51 +296,51 @@ fn register_bar_module(state: *c.lua_State) void {
300 296 c.lua_setfield(state, -2, "bar");
301 297 }
302 298
303 -fn register_misc_functions(state: *c.lua_State) void {
304 - c.lua_pushcfunction(state, lua_set_terminal);
299 +fn registerMiscFunctions(state: *c.lua_State) void {
300 + c.lua_pushcfunction(state, luaSetTerminal);
305 301 c.lua_setfield(state, -2, "set_terminal");
306 302
307 - c.lua_pushcfunction(state, lua_set_modkey);
303 + c.lua_pushcfunction(state, luaSetModkey);
308 304 c.lua_setfield(state, -2, "set_modkey");
309 305
310 - c.lua_pushcfunction(state, lua_set_tags);
306 + c.lua_pushcfunction(state, luaSetTags);
311 307 c.lua_setfield(state, -2, "set_tags");
312 308
313 - c.lua_pushcfunction(state, lua_set_layout_symbol);
309 + c.lua_pushcfunction(state, luaSetLayoutSymbol);
314 310 c.lua_setfield(state, -2, "set_layout_symbol");
315 311
316 - c.lua_pushcfunction(state, lua_autostart);
312 + c.lua_pushcfunction(state, luaAutostart);
317 313 c.lua_setfield(state, -2, "autostart");
318 314
319 - c.lua_pushcfunction(state, lua_auto_tile);
315 + c.lua_pushcfunction(state, luaAutoTile);
320 316 c.lua_setfield(state, -2, "auto_tile");
321 317
322 - c.lua_pushcfunction(state, lua_quit);
318 + c.lua_pushcfunction(state, luaQuit);
323 319 c.lua_setfield(state, -2, "quit");
324 320
325 - c.lua_pushcfunction(state, lua_restart);
321 + c.lua_pushcfunction(state, luaRestart);
326 322 c.lua_setfield(state, -2, "restart");
327 323
328 - c.lua_pushcfunction(state, lua_toggle_gaps);
324 + c.lua_pushcfunction(state, luaToggleGaps);
329 325 c.lua_setfield(state, -2, "toggle_gaps");
330 326
331 - c.lua_pushcfunction(state, lua_show_keybinds);
327 + c.lua_pushcfunction(state, luaShowKeybinds);
332 328 c.lua_setfield(state, -2, "show_keybinds");
333 329
334 - c.lua_pushcfunction(state, lua_set_master_factor);
330 + c.lua_pushcfunction(state, luaSetMasterFactor);
335 331 c.lua_setfield(state, -2, "set_master_factor");
336 332
337 - c.lua_pushcfunction(state, lua_inc_num_master);
333 + c.lua_pushcfunction(state, luaIncNumMaster);
338 334 c.lua_setfield(state, -2, "inc_num_master");
339 335 }
340 336
341 -fn create_action_table(state: *c.lua_State, action_name: [*:0]const u8) void {
337 +fn createActionTable(state: *c.lua_State, action_name: [*:0]const u8) void {
342 338 c.lua_createtable(state, 0, 2);
343 339 _ = c.lua_pushstring(state, action_name);
344 340 c.lua_setfield(state, -2, "__action");
345 341 }
346 342
347 -fn create_action_table_with_int(state: *c.lua_State, action_name: [*:0]const u8, arg: i32) void {
343 +fn createActionTableWithInt(state: *c.lua_State, action_name: [*:0]const u8, arg: i32) void {
348 344 c.lua_createtable(state, 0, 2);
349 345 _ = c.lua_pushstring(state, action_name);
350 346 c.lua_setfield(state, -2, "__action");
@@ -352,7 +348,7 @@ fn create_action_table_with_int(state: *c.lua_State, action_name: [*:0]const u8,
352 348 c.lua_setfield(state, -2, "__arg");
353 349 }
354 350
355 -fn create_action_table_with_string(state: *c.lua_State, action_name: [*:0]const u8) void {
351 +fn createActionTableWithString(state: *c.lua_State, action_name: [*:0]const u8) void {
356 352 c.lua_createtable(state, 0, 2);
357 353 _ = c.lua_pushstring(state, action_name);
358 354 c.lua_setfield(state, -2, "__action");
@@ -360,36 +356,36 @@ fn create_action_table_with_string(state: *c.lua_State, action_name: [*:0]const
360 356 c.lua_setfield(state, -2, "__arg");
361 357 }
362 358
363 -fn lua_spawn(state: ?*c.lua_State) callconv(.c) c_int {
359 +fn luaSpawn(state: ?*c.lua_State) callconv(.c) c_int {
364 360 const s = state orelse return 0;
365 - create_action_table_with_string(s, "Spawn");
361 + createActionTableWithString(s, "Spawn");
366 362 return 1;
367 363 }
368 364
369 -fn lua_spawn_terminal(state: ?*c.lua_State) callconv(.c) c_int {
365 +fn luaSpawnTerminal(state: ?*c.lua_State) callconv(.c) c_int {
370 366 const s = state orelse return 0;
371 - create_action_table(s, "SpawnTerminal");
367 + createActionTable(s, "SpawnTerminal");
372 368 return 1;
373 369 }
374 370
375 -fn lua_key_bind(state: ?*c.lua_State) callconv(.c) c_int {
371 +fn luaKeyBind(state: ?*c.lua_State) callconv(.c) c_int {
376 372 const s = state orelse return 0;
377 373 const cfg = config orelse return 0;
378 374
379 - const mod_mask = parse_modifiers(s, 1);
380 - const key_str = get_string_arg(s, 2) orelse return 0;
381 - const keysym = key_name_to_keysym(key_str) orelse return 0;
375 + const mod_mask = parseModifiers(s, 1);
376 + const key_str = getStringArg(s, 2) orelse return 0;
377 + const keysym = keynameToKeysym(key_str) orelse return 0;
382 378
383 379 if (c.lua_type(s, 3) != c.LUA_TTABLE) return 0;
384 380
385 381 _ = c.lua_getfield(s, 3, "__action");
386 - const action_str = get_lua_string(s, -1) orelse {
382 + const action_str = getLuaString(s, -1) orelse {
387 383 c.lua_settop(s, -2);
388 384 return 0;
389 385 };
390 386 c.lua_settop(s, -2);
391 387
392 - const action = parse_action(action_str) orelse return 0;
388 + const action = parseAction(action_str) orelse return 0;
393 389
394 390 var int_arg: i32 = 0;
395 391 var str_arg: ?[]const u8 = null;
@@ -398,9 +394,9 @@ fn lua_key_bind(state: ?*c.lua_State) callconv(.c) c_int {
398 394 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
399 395 int_arg = @intCast(c.lua_tointegerx(s, -1, null));
400 396 } else if (c.lua_type(s, -1) == c.LUA_TSTRING) {
401 - str_arg = get_lua_string(s, -1);
397 + str_arg = getLuaString(s, -1);
402 398 } else if (c.lua_type(s, -1) == c.LUA_TTABLE) {
403 - str_arg = extract_spawn_command(s, -1);
399 + str_arg = extractSpawnCommand(s, -1);
404 400 }
405 401 c.lua_settop(s, -2);
406 402
@@ -412,12 +408,12 @@ fn lua_key_bind(state: ?*c.lua_State) callconv(.c) c_int {
412 408 keybind.keys[0] = .{ .mod_mask = mod_mask, .keysym = keysym };
413 409 keybind.key_count = 1;
414 410
415 - cfg.add_keybind(keybind) catch return 0;
411 + cfg.addKeybind(keybind) catch return 0;
416 412
417 413 return 0;
418 414 }
419 415
420 -fn lua_key_chord(state: ?*c.lua_State) callconv(.c) c_int {
416 +fn luaKeyChord(state: ?*c.lua_State) callconv(.c) c_int {
421 417 const s = state orelse return 0;
422 418 const cfg = config orelse return 0;
423 419
@@ -443,17 +439,17 @@ fn lua_key_chord(state: ?*c.lua_State) callconv(.c) c_int {
443 439 }
444 440
445 441 _ = c.lua_rawgeti(s, -1, 1);
446 - const mod_mask = parse_modifiers_at_top(s);
442 + const mod_mask = parseModifiersAtTop(s);
447 443 c.lua_settop(s, -2);
448 444
449 445 _ = c.lua_rawgeti(s, -1, 2);
450 - const key_str = get_lua_string(s, -1) orelse {
446 + const key_str = getLuaString(s, -1) orelse {
451 447 c.lua_settop(s, -3);
452 448 return 0;
453 449 };
454 450 c.lua_settop(s, -2);
455 451
456 - const keysym = key_name_to_keysym(key_str) orelse {
452 + const keysym = keynameToKeysym(key_str) orelse {
457 453 c.lua_settop(s, -2);
458 454 return 0;
459 455 };
@@ -465,51 +461,51 @@ fn lua_key_chord(state: ?*c.lua_State) callconv(.c) c_int {
465 461 }
466 462
467 463 _ = c.lua_getfield(s, 2, "__action");
468 - const action_str = get_lua_string(s, -1) orelse {
464 + const action_str = getLuaString(s, -1) orelse {
469 465 c.lua_settop(s, -2);
470 466 return 0;
471 467 };
472 468 c.lua_settop(s, -2);
473 469
474 - keybind.action = parse_action(action_str) orelse return 0;
470 + keybind.action = parseAction(action_str) orelse return 0;
475 471
476 472 _ = c.lua_getfield(s, 2, "__arg");
477 473 if (c.lua_type(s, -1) == c.LUA_TNUMBER) {
478 474 keybind.int_arg = @intCast(c.lua_tointegerx(s, -1, null));
479 475 } else if (c.lua_type(s, -1) == c.LUA_TSTRING) {
480 - keybind.str_arg = get_lua_string(s, -1);
476 + keybind.str_arg = getLuaString(s, -1);
481 477 } else if (c.lua_type(s, -1) == c.LUA_TTABLE) {
482 - keybind.str_arg = extract_spawn_command(s, -1);
478 + keybind.str_arg = extractSpawnCommand(s, -1);
483 479 }
484 480
485 481 c.lua_settop(s, -2);
486 - cfg.add_keybind(keybind) catch return 0;
482 + cfg.addKeybind(keybind) catch return 0;
487 483
488 484 return 0;
489 485 }
490 486
491 -fn lua_gaps_set_enabled(state: ?*c.lua_State) callconv(.c) c_int {
487 +fn luaGapsSetEnabled(state: ?*c.lua_State) callconv(.c) c_int {
492 488 const cfg = config orelse return 0;
493 489 const s = state orelse return 0;
494 490 cfg.gaps_enabled = c.lua_toboolean(s, 1) != 0;
495 491 return 0;
496 492 }
497 493
498 -fn lua_gaps_enable(state: ?*c.lua_State) callconv(.c) c_int {
494 +fn luaGapsEnable(state: ?*c.lua_State) callconv(.c) c_int {
499 495 _ = state;
500 496 const cfg = config orelse return 0;
501 497 cfg.gaps_enabled = true;
502 498 return 0;
503 499 }
504 500
505 -fn lua_gaps_disable(state: ?*c.lua_State) callconv(.c) c_int {
501 +fn luaGapsDisable(state: ?*c.lua_State) callconv(.c) c_int {
506 502 _ = state;
507 503 const cfg = config orelse return 0;
508 504 cfg.gaps_enabled = false;
509 505 return 0;
510 506 }
511 507
512 -fn lua_gaps_set_inner(state: ?*c.lua_State) callconv(.c) c_int {
508 +fn luaGapsSetInner(state: ?*c.lua_State) callconv(.c) c_int {
513 509 const cfg = config orelse return 0;
514 510 const s = state orelse return 0;
515 511 cfg.gap_inner_h = @intCast(c.lua_tointegerx(s, 1, null));
@@ -517,7 +513,7 @@ fn lua_gaps_set_inner(state: ?*c.lua_State) callconv(.c) c_int {
517 513 return 0;
518 514 }
519 515
520 -fn lua_gaps_set_outer(state: ?*c.lua_State) callconv(.c) c_int {
516 +fn luaGapsSetOuter(state: ?*c.lua_State) callconv(.c) c_int {
521 517 const cfg = config orelse return 0;
522 518 const s = state orelse return 0;
523 519 cfg.gap_outer_h = @intCast(c.lua_tointegerx(s, 1, null));
@@ -525,164 +521,164 @@ fn lua_gaps_set_outer(state: ?*c.lua_State) callconv(.c) c_int {
525 521 return 0;
526 522 }
527 523
528 -fn lua_gaps_set_smart(state: ?*c.lua_State) callconv(.c) c_int {
524 +fn luaGapsSetSmart(state: ?*c.lua_State) callconv(.c) c_int {
529 525 const cfg = config orelse return 0;
530 526 const s = state orelse return 0;
531 527 cfg.smartgaps_enabled = c.lua_toboolean(s, 1) != 0;
532 528 return 0;
533 529 }
534 530
535 -fn lua_border_set_width(state: ?*c.lua_State) callconv(.c) c_int {
531 +fn luaBorderSetWidth(state: ?*c.lua_State) callconv(.c) c_int {
536 532 const cfg = config orelse return 0;
537 533 const s = state orelse return 0;
538 534 cfg.border_width = @intCast(c.lua_tointegerx(s, 1, null));
539 535 return 0;
540 536 }
541 537
542 -fn lua_border_set_focused_color(state: ?*c.lua_State) callconv(.c) c_int {
538 +fn luaBorderSetFocusedColor(state: ?*c.lua_State) callconv(.c) c_int {
543 539 const cfg = config orelse return 0;
544 540 const s = state orelse return 0;
545 - cfg.border_focused = parse_color(s, 1);
541 + cfg.border_focused = parseColor(s, 1);
546 542 return 0;
547 543 }
548 544
549 -fn lua_border_set_unfocused_color(state: ?*c.lua_State) callconv(.c) c_int {
545 +fn luaBorderSetUnfocusedColor(state: ?*c.lua_State) callconv(.c) c_int {
550 546 const cfg = config orelse return 0;
551 547 const s = state orelse return 0;
552 - cfg.border_unfocused = parse_color(s, 1);
548 + cfg.border_unfocused = parseColor(s, 1);
553 549 return 0;
554 550 }
555 551
556 -fn lua_client_kill(state: ?*c.lua_State) callconv(.c) c_int {
552 +fn luaClientKill(state: ?*c.lua_State) callconv(.c) c_int {
557 553 const s = state orelse return 0;
558 - create_action_table(s, "KillClient");
554 + createActionTable(s, "KillClient");
559 555 return 1;
560 556 }
561 557
562 -fn lua_client_toggle_fullscreen(state: ?*c.lua_State) callconv(.c) c_int {
558 +fn luaClientToggleFullscreen(state: ?*c.lua_State) callconv(.c) c_int {
563 559 const s = state orelse return 0;
564 - create_action_table(s, "ToggleFullScreen");
560 + createActionTable(s, "ToggleFullScreen");
565 561 return 1;
566 562 }
567 563
568 -fn lua_client_toggle_floating(state: ?*c.lua_State) callconv(.c) c_int {
564 +fn luaClientToggleFloating(state: ?*c.lua_State) callconv(.c) c_int {
569 565 const s = state orelse return 0;
570 - create_action_table(s, "ToggleFloating");
566 + createActionTable(s, "ToggleFloating");
571 567 return 1;
572 568 }
573 569
574 -fn lua_client_focus_stack(state: ?*c.lua_State) callconv(.c) c_int {
570 +fn luaClientFocusStack(state: ?*c.lua_State) callconv(.c) c_int {
575 571 const s = state orelse return 0;
576 572 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
577 - create_action_table_with_int(s, "FocusStack", dir);
573 + createActionTableWithInt(s, "FocusStack", dir);
578 574 return 1;
579 575 }
580 576
581 -fn lua_client_move_stack(state: ?*c.lua_State) callconv(.c) c_int {
577 +fn luaClientMoveStack(state: ?*c.lua_State) callconv(.c) c_int {
582 578 const s = state orelse return 0;
583 579 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
584 - create_action_table_with_int(s, "MoveStack", dir);
580 + createActionTableWithInt(s, "MoveStack", dir);
585 581 return 1;
586 582 }
587 583
588 -fn lua_layout_cycle(state: ?*c.lua_State) callconv(.c) c_int {
584 +fn luaLayoutCycle(state: ?*c.lua_State) callconv(.c) c_int {
589 585 const s = state orelse return 0;
590 - create_action_table(s, "CycleLayout");
586 + createActionTable(s, "CycleLayout");
591 587 return 1;
592 588 }
593 589
594 -fn lua_layout_set(state: ?*c.lua_State) callconv(.c) c_int {
590 +fn luaLayoutSet(state: ?*c.lua_State) callconv(.c) c_int {
595 591 const s = state orelse return 0;
596 - create_action_table_with_string(s, "ChangeLayout");
592 + createActionTableWithString(s, "ChangeLayout");
597 593 return 1;
598 594 }
599 595
600 -fn lua_layout_scroll_left(state: ?*c.lua_State) callconv(.c) c_int {
596 +fn luaLayoutScrollLeft(state: ?*c.lua_State) callconv(.c) c_int {
601 597 const s = state orelse return 0;
602 - create_action_table(s, "ScrollLeft");
598 + createActionTable(s, "ScrollLeft");
603 599 return 1;
604 600 }
605 601
606 -fn lua_layout_scroll_right(state: ?*c.lua_State) callconv(.c) c_int {
602 +fn luaLayoutScrollRight(state: ?*c.lua_State) callconv(.c) c_int {
607 603 const s = state orelse return 0;
608 - create_action_table(s, "ScrollRight");
604 + createActionTable(s, "ScrollRight");
609 605 return 1;
610 606 }
611 607
612 -fn lua_tag_view(state: ?*c.lua_State) callconv(.c) c_int {
608 +fn luaTagView(state: ?*c.lua_State) callconv(.c) c_int {
613 609 const s = state orelse return 0;
614 610 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
615 - create_action_table_with_int(s, "ViewTag", idx);
611 + createActionTableWithInt(s, "ViewTag", idx);
616 612 return 1;
617 613 }
618 614
619 -fn lua_tag_view_next(state: ?*c.lua_State) callconv(.c) c_int {
615 +fn luaTagViewNext(state: ?*c.lua_State) callconv(.c) c_int {
620 616 const s = state orelse return 0;
621 - create_action_table(s, "ViewNextTag");
617 + createActionTable(s, "ViewNextTag");
622 618 return 1;
623 619 }
624 620
625 -fn lua_tag_view_previous(state: ?*c.lua_State) callconv(.c) c_int {
621 +fn luaTagViewPrevious(state: ?*c.lua_State) callconv(.c) c_int {
626 622 const s = state orelse return 0;
627 - create_action_table(s, "ViewPreviousTag");
623 + createActionTable(s, "ViewPreviousTag");
628 624 return 1;
629 625 }
630 626
631 -fn lua_tag_view_next_nonempty(state: ?*c.lua_State) callconv(.c) c_int {
627 +fn luaTagViewNextNonempty(state: ?*c.lua_State) callconv(.c) c_int {
632 628 const s = state orelse return 0;
633 - create_action_table(s, "ViewNextNonEmptyTag");
629 + createActionTable(s, "ViewNextNonEmptyTag");
634 630 return 1;
635 631 }
636 632
637 -fn lua_tag_view_previous_nonempty(state: ?*c.lua_State) callconv(.c) c_int {
633 +fn luaTagViewPreviousNonempty(state: ?*c.lua_State) callconv(.c) c_int {
638 634 const s = state orelse return 0;
639 - create_action_table(s, "ViewPreviousNonEmptyTag");
635 + createActionTable(s, "ViewPreviousNonEmptyTag");
640 636 return 1;
641 637 }
642 638
643 -fn lua_tag_toggleview(state: ?*c.lua_State) callconv(.c) c_int {
639 +fn luaTagToggleview(state: ?*c.lua_State) callconv(.c) c_int {
644 640 const s = state orelse return 0;
645 641 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
646 - create_action_table_with_int(s, "ToggleView", idx);
642 + createActionTableWithInt(s, "ToggleView", idx);
647 643 return 1;
648 644 }
649 645
650 -fn lua_tag_move_to(state: ?*c.lua_State) callconv(.c) c_int {
646 +fn luaTagMoveTo(state: ?*c.lua_State) callconv(.c) c_int {
651 647 const s = state orelse return 0;
652 648 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
653 - create_action_table_with_int(s, "MoveToTag", idx);
649 + createActionTableWithInt(s, "MoveToTag", idx);
654 650 return 1;
655 651 }
656 652
657 -fn lua_tag_toggletag(state: ?*c.lua_State) callconv(.c) c_int {
653 +fn luaTagToggletag(state: ?*c.lua_State) callconv(.c) c_int {
658 654 const s = state orelse return 0;
659 655 const idx: i32 = @intCast(c.lua_tointegerx(s, 1, null));
660 - create_action_table_with_int(s, "ToggleTag", idx);
656 + createActionTableWithInt(s, "ToggleTag", idx);
661 657 return 1;
662 658 }
663 659
664 -fn lua_tag_set_back_and_forth(state: ?*c.lua_State) callconv(.c) c_int {
660 +fn luaTagSetBackAndForth(state: ?*c.lua_State) callconv(.c) c_int {
665 661 const cfg = config orelse return 0;
666 662 const s = state orelse return 0;
667 663 cfg.tag_back_and_forth = c.lua_toboolean(s, 1) != 0;
668 664 return 0;
669 665 }
670 666
671 -fn lua_monitor_focus(state: ?*c.lua_State) callconv(.c) c_int {
667 +fn luaMonitorFocus(state: ?*c.lua_State) callconv(.c) c_int {
672 668 const s = state orelse return 0;
673 669 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
674 - create_action_table_with_int(s, "FocusMonitor", dir);
670 + createActionTableWithInt(s, "FocusMonitor", dir);
675 671 return 1;
676 672 }
677 673
678 -fn lua_monitor_tag(state: ?*c.lua_State) callconv(.c) c_int {
674 +fn luaMonitorTag(state: ?*c.lua_State) callconv(.c) c_int {
679 675 const s = state orelse return 0;
680 676 const dir: i32 = @intCast(c.lua_tointegerx(s, 1, null));
681 - create_action_table_with_int(s, "TagMonitor", dir);
677 + createActionTableWithInt(s, "TagMonitor", dir);
682 678 return 1;
683 679 }
684 680
685 -fn lua_rule_add(state: ?*c.lua_State) callconv(.c) c_int {
681 +fn luaRuleAdd(state: ?*c.lua_State) callconv(.c) c_int {
686 682 const cfg = config orelse return 0;
687 683 const s = state orelse return 0;
688 684
@@ -700,19 +696,19 @@ fn lua_rule_add(state: ?*c.lua_State) callconv(.c) c_int {
700 696
701 697 _ = c.lua_getfield(s, 1, "class");
702 698 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
703 - rule.class = get_lua_string(s, -1);
699 + rule.class = getLuaString(s, -1);
704 700 }
705 701 c.lua_settop(s, -2);
706 702
707 703 _ = c.lua_getfield(s, 1, "instance");
708 704 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
709 - rule.instance = get_lua_string(s, -1);
705 + rule.instance = getLuaString(s, -1);
710 706 }
711 707 c.lua_settop(s, -2);
712 708
713 709 _ = c.lua_getfield(s, 1, "title");
714 710 if (c.lua_type(s, -1) == c.LUA_TSTRING) {
715 - rule.title = get_lua_string(s, -1);
711 + rule.title = getLuaString(s, -1);
716 712 }
717 713 c.lua_settop(s, -2);
718 714
@@ -743,20 +739,20 @@ fn lua_rule_add(state: ?*c.lua_State) callconv(.c) c_int {
743 739 }
744 740 c.lua_settop(s, -2);
745 741
746 - cfg.add_rule(rule) catch return 0;
742 + cfg.addRule(rule) catch return 0;
747 743 return 0;
748 744 }
749 745
750 -fn lua_bar_set_font(state: ?*c.lua_State) callconv(.c) c_int {
746 +fn luaBarSetFont(state: ?*c.lua_State) callconv(.c) c_int {
751 747 const cfg = config orelse return 0;
752 748 const s = state orelse return 0;
753 - if (dupe_lua_string(s, 1)) |font| {
749 + if (dupeLuaString(s, 1)) |font| {
754 750 cfg.font = font;
755 751 }
756 752 return 0;
757 753 }
758 754
759 -fn lua_bar_set_blocks(state: ?*c.lua_State) callconv(.c) c_int {
755 +fn luaBarSetBlocks(state: ?*c.lua_State) callconv(.c) c_int {
760 756 const cfg = config orelse return 0;
761 757 const s = state orelse return 0;
762 758
@@ -772,8 +768,8 @@ fn lua_bar_set_blocks(state: ?*c.lua_State) callconv(.c) c_int {
772 768 continue;
773 769 }
774 770
775 - if (parse_block_config(s, -1)) |block| {
776 - cfg.add_block(block) catch {};
771 + if (parseBlockConfig(s, -1)) |block| {
772 + cfg.addBlock(block) catch {};
777 773 }
778 774
779 775 c.lua_settop(s, -2);
@@ -782,16 +778,16 @@ fn lua_bar_set_blocks(state: ?*c.lua_State) callconv(.c) c_int {
782 778 return 0;
783 779 }
784 780
785 -fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block {
781 +fn parseBlockConfig(state: *c.lua_State, idx: c_int) ?Block {
786 782 _ = c.lua_getfield(state, idx, "__block_type");
787 - const block_type_str = get_lua_string(state, -1) orelse {
783 + const block_type_str = getLuaString(state, -1) orelse {
788 784 c.lua_settop(state, -2);
789 785 return null;
790 786 };
791 787 c.lua_settop(state, -2);
792 788
793 789 _ = c.lua_getfield(state, idx, "format");
794 - const format = dupe_lua_string(state, -1) orelse "";
790 + const format = dupeLuaString(state, -1) orelse "";
795 791 c.lua_settop(state, -2);
796 792
797 793 _ = c.lua_getfield(state, idx, "interval");
@@ -799,7 +795,7 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block {
799 795 c.lua_settop(state, -2);
800 796
801 797 _ = c.lua_getfield(state, idx, "color");
802 - const color = parse_color(state, -1);
798 + const color = parseColor(state, -1);
803 799 c.lua_settop(state, -2);
804 800
805 801 _ = c.lua_getfield(state, idx, "underline");
@@ -819,17 +815,17 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block {
819 815 } else if (std.mem.eql(u8, block_type_str, "DateTime")) {
820 816 block.block_type = .datetime;
821 817 _ = c.lua_getfield(state, idx, "__arg");
822 - block.datetime_format = dupe_lua_string(state, -1);
818 + block.datetime_format = dupeLuaString(state, -1);
823 819 c.lua_settop(state, -2);
824 820 } else if (std.mem.eql(u8, block_type_str, "Shell")) {
825 821 block.block_type = .shell;
826 822 _ = c.lua_getfield(state, idx, "__arg");
827 - block.command = dupe_lua_string(state, -1);
823 + block.command = dupeLuaString(state, -1);
828 824 c.lua_settop(state, -2);
829 825 } else if (std.mem.eql(u8, block_type_str, "Static")) {
830 826 block.block_type = .static;
831 827 _ = c.lua_getfield(state, idx, "__arg");
832 - if (dupe_lua_string(state, -1)) |text| {
828 + if (dupeLuaString(state, -1)) |text| {
833 829 block.format = text;
834 830 }
835 831 c.lua_settop(state, -2);
@@ -838,19 +834,19 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block {
838 834 _ = c.lua_getfield(state, idx, "__arg");
839 835 if (c.lua_type(state, -1) == c.LUA_TTABLE) {
840 836 _ = c.lua_getfield(state, -1, "charging");
841 - block.format_charging = dupe_lua_string(state, -1);
837 + block.format_charging = dupeLuaString(state, -1);
842 838 c.lua_settop(state, -2);
843 839
844 840 _ = c.lua_getfield(state, -1, "discharging");
845 - block.format_discharging = dupe_lua_string(state, -1);
841 + block.format_discharging = dupeLuaString(state, -1);
846 842 c.lua_settop(state, -2);
847 843
848 844 _ = c.lua_getfield(state, -1, "full");
849 - block.format_full = dupe_lua_string(state, -1);
845 + block.format_full = dupeLuaString(state, -1);
850 846 c.lua_settop(state, -2);
851 847
852 848 _ = c.lua_getfield(state, -1, "battery_name");
853 - block.battery_name = dupe_lua_string(state, -1);
849 + block.battery_name = dupeLuaString(state, -1);
854 850 c.lua_settop(state, -2);
855 851 }
856 852 c.lua_settop(state, -2);
@@ -861,75 +857,75 @@ fn parse_block_config(state: *c.lua_State, idx: c_int) ?Block {
861 857 return block;
862 858 }
863 859
864 -fn lua_bar_set_scheme_normal(state: ?*c.lua_State) callconv(.c) c_int {
860 +fn luaBarSetSchemeNormal(state: ?*c.lua_State) callconv(.c) c_int {
865 861 const cfg = config orelse return 0;
866 862 const s = state orelse return 0;
867 - cfg.scheme_normal = parse_scheme(s);
863 + cfg.scheme_normal = parseScheme(s);
868 864 return 0;
869 865 }
870 866
871 -fn lua_bar_set_scheme_selected(state: ?*c.lua_State) callconv(.c) c_int {
867 +fn luaBarSetSchemeSelected(state: ?*c.lua_State) callconv(.c) c_int {
872 868 const cfg = config orelse return 0;
873 869 const s = state orelse return 0;
874 - cfg.scheme_selected = parse_scheme(s);
870 + cfg.scheme_selected = parseScheme(s);
875 871 return 0;
876 872 }
877 873
878 -fn lua_bar_set_scheme_occupied(state: ?*c.lua_State) callconv(.c) c_int {
874 +fn luaBarSetSchemeOccupied(state: ?*c.lua_State) callconv(.c) c_int {
879 875 const cfg = config orelse return 0;
880 876 const s = state orelse return 0;
881 - cfg.scheme_occupied = parse_scheme(s);
877 + cfg.scheme_occupied = parseScheme(s);
882 878 return 0;
883 879 }
884 880
885 -fn lua_bar_set_scheme_urgent(state: ?*c.lua_State) callconv(.c) c_int {
881 +fn luaBarSetSchemeUrgent(state: ?*c.lua_State) callconv(.c) c_int {
886 882 const cfg = config orelse return 0;
887 883 const s = state orelse return 0;
888 - cfg.scheme_urgent = parse_scheme(s);
884 + cfg.scheme_urgent = parseScheme(s);
889 885 return 0;
890 886 }
891 887
892 -fn lua_bar_set_hide_vacant_tags(state: ?*c.lua_State) callconv(.c) c_int {
888 +fn luaBarSetHideVacantTags(state: ?*c.lua_State) callconv(.c) c_int {
893 889 const cfg = config orelse return 0;
894 890 const s = state orelse return 0;
895 891 cfg.hide_vacant_tags = c.lua_toboolean(s, 1) != 0;
896 892 return 0;
897 893 }
898 894
899 -fn lua_bar_block_ram(state: ?*c.lua_State) callconv(.c) c_int {
895 +fn luaBarBlockRam(state: ?*c.lua_State) callconv(.c) c_int {
900 896 const s = state orelse return 0;
901 - create_block_table(s, "Ram", null);
897 + createBlockTable(s, "Ram", null);
902 898 return 1;
903 899 }
904 900
905 -fn lua_bar_block_datetime(state: ?*c.lua_State) callconv(.c) c_int {
901 +fn luaBarBlockDatetime(state: ?*c.lua_State) callconv(.c) c_int {
906 902 const s = state orelse return 0;
907 903 _ = c.lua_getfield(s, 1, "date_format");
908 - const date_format = get_lua_string(s, -1);
904 + const date_format = getLuaString(s, -1);
909 905 c.lua_settop(s, -2);
910 - create_block_table(s, "DateTime", date_format);
906 + createBlockTable(s, "DateTime", date_format);
911 907 return 1;
912 908 }
913 909
914 -fn lua_bar_block_shell(state: ?*c.lua_State) callconv(.c) c_int {
910 +fn luaBarBlockShell(state: ?*c.lua_State) callconv(.c) c_int {
915 911 const s = state orelse return 0;
916 912 _ = c.lua_getfield(s, 1, "command");
917 - const command = get_lua_string(s, -1);
913 + const command = getLuaString(s, -1);
918 914 c.lua_settop(s, -2);
919 - create_block_table(s, "Shell", command);
915 + createBlockTable(s, "Shell", command);
920 916 return 1;
921 917 }
922 918
923 -fn lua_bar_block_static(state: ?*c.lua_State) callconv(.c) c_int {
919 +fn luaBarBlockStatic(state: ?*c.lua_State) callconv(.c) c_int {
924 920 const s = state orelse return 0;
925 921 _ = c.lua_getfield(s, 1, "text");
926 - const text = get_lua_string(s, -1);
922 + const text = getLuaString(s, -1);
927 923 c.lua_settop(s, -2);
928 - create_block_table(s, "Static", text);
924 + createBlockTable(s, "Static", text);
929 925 return 1;
930 926 }
931 927
932 -fn lua_bar_block_battery(state: ?*c.lua_State) callconv(.c) c_int {
928 +fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int {
933 929 const s = state orelse return 0;
934 930
935 931 c.lua_createtable(s, 0, 6);
@@ -963,7 +959,7 @@ fn lua_bar_block_battery(state: ?*c.lua_State) callconv(.c) c_int {
963 959 return 1;
964 960 }
965 961
966 -fn create_block_table(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void {
962 +fn createBlockTable(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]const u8) void {
967 963 c.lua_createtable(state, 0, 6);
968 964
969 965 _ = c.lua_pushstring(state, block_type);
@@ -992,27 +988,27 @@ fn create_block_table(state: *c.lua_State, block_type: [*:0]const u8, arg: ?[]co
992 988 }
993 989 }
994 990
995 -fn lua_set_terminal(state: ?*c.lua_State) callconv(.c) c_int {
991 +fn luaSetTerminal(state: ?*c.lua_State) callconv(.c) c_int {
996 992 const cfg = config orelse return 0;
997 993 const s = state orelse return 0;
998 - if (dupe_lua_string(s, 1)) |term| {
994 + if (dupeLuaString(s, 1)) |term| {
999 995 cfg.terminal = term;
1000 996 }
1001 997 return 0;
1002 998 }
1003 999
1004 -fn lua_set_modkey(state: ?*c.lua_State) callconv(.c) c_int {
1000 +fn luaSetModkey(state: ?*c.lua_State) callconv(.c) c_int {
1005 1001 const cfg = config orelse return 0;
1006 1002 const s = state orelse return 0;
1007 - if (get_string_arg(s, 1)) |modkey_str| {
1008 - cfg.modkey = parse_single_modifier(modkey_str);
1009 - cfg.add_button(.{
1003 + if (getStringArg(s, 1)) |modkey_str| {
1004 + cfg.modkey = parseSingleModifier(modkey_str);
1005 + cfg.addButton(.{
1010 1006 .click = .client_win,
1011 1007 .mod_mask = cfg.modkey,
1012 1008 .button = 1,
1013 1009 .action = .move_mouse,
1014 1010 }) catch {};
1015 - cfg.add_button(.{
1011 + cfg.addButton(.{
1016 1012 .click = .client_win,
1017 1013 .mod_mask = cfg.modkey,
1018 1014 .button = 3,
@@ -1022,7 +1018,7 @@ fn lua_set_modkey(state: ?*c.lua_State) callconv(.c) c_int {
1022 1018 return 0;
1023 1019 }
1024 1020
1025 -fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int {
1021 +fn luaSetTags(state: ?*c.lua_State) callconv(.c) c_int {
1026 1022 const cfg = config orelse return 0;
1027 1023 const s = state orelse return 0;
1028 1024
@@ -1032,7 +1028,7 @@ fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int {
1032 1028 var i: usize = 0;
1033 1029 while (i < len and i < 9) : (i += 1) {
1034 1030 _ = c.lua_rawgeti(s, 1, @intCast(i + 1));
1035 - if (dupe_lua_string(s, -1)) |tag_str| {
1031 + if (dupeLuaString(s, -1)) |tag_str| {
1036 1032 cfg.tags[i] = tag_str;
1037 1033 }
1038 1034 c.lua_settop(s, -2);
@@ -1041,27 +1037,27 @@ fn lua_set_tags(state: ?*c.lua_State) callconv(.c) c_int {
1041 1037 return 0;
1042 1038 }
1043 1039
1044 -fn lua_autostart(state: ?*c.lua_State) callconv(.c) c_int {
1040 +fn luaAutostart(state: ?*c.lua_State) callconv(.c) c_int {
1045 1041 const cfg = config orelse return 0;
1046 1042 const s = state orelse return 0;
1047 - if (dupe_lua_string(s, 1)) |cmd| {
1048 - cfg.add_autostart(cmd) catch return 0;
1043 + if (dupeLuaString(s, 1)) |cmd| {
1044 + cfg.addAutostart(cmd) catch return 0;
1049 1045 }
1050 1046 return 0;
1051 1047 }
1052 1048
1053 -fn lua_auto_tile(state: ?*c.lua_State) callconv(.c) c_int {
1049 +fn luaAutoTile(state: ?*c.lua_State) callconv(.c) c_int {
1054 1050 const cfg = config orelse return 0;
1055 1051 const s = state orelse return 0;
1056 1052 cfg.auto_tile = c.lua_toboolean(s, 1) != 0;
1057 1053 return 0;
1058 1054 }
1059 1055
1060 -fn lua_set_layout_symbol(state: ?*c.lua_State) callconv(.c) c_int {
1056 +fn luaSetLayoutSymbol(state: ?*c.lua_State) callconv(.c) c_int {
1061 1057 const cfg = config orelse return 0;
1062 1058 const s = state orelse return 0;
1063 - const name = get_string_arg(s, 1) orelse return 0;
1064 - const symbol = dupe_lua_string(s, 2) orelse return 0;
1059 + const name = getStringArg(s, 1) orelse return 0;
1060 + const symbol = dupeLuaString(s, 2) orelse return 0;
1065 1061
1066 1062 const layout_map = .{
1067 1063 .{ "tiling", &cfg.layout_tile_symbol },
@@ -1083,103 +1079,103 @@ fn lua_set_layout_symbol(state: ?*c.lua_State) callconv(.c) c_int {
1083 1079 return 0;
1084 1080 }
1085 1081
1086 -fn lua_quit(state: ?*c.lua_State) callconv(.c) c_int {
1082 +fn luaQuit(state: ?*c.lua_State) callconv(.c) c_int {
1087 1083 const s = state orelse return 0;
1088 - create_action_table(s, "Quit");
1084 + createActionTable(s, "Quit");
1089 1085 return 1;
1090 1086 }
1091 1087
1092 -fn lua_restart(state: ?*c.lua_State) callconv(.c) c_int {
1088 +fn luaRestart(state: ?*c.lua_State) callconv(.c) c_int {
1093 1089 const s = state orelse return 0;
1094 - create_action_table(s, "Restart");
1090 + createActionTable(s, "Restart");
1095 1091 return 1;
1096 1092 }
1097 1093
1098 -fn lua_toggle_gaps(state: ?*c.lua_State) callconv(.c) c_int {
1094 +fn luaToggleGaps(state: ?*c.lua_State) callconv(.c) c_int {
1099 1095 const s = state orelse return 0;
1100 - create_action_table(s, "ToggleGaps");
1096 + createActionTable(s, "ToggleGaps");
1101 1097 return 1;
1102 1098 }
1103 1099
1104 -fn lua_show_keybinds(state: ?*c.lua_State) callconv(.c) c_int {
1100 +fn luaShowKeybinds(state: ?*c.lua_State) callconv(.c) c_int {
1105 1101 const s = state orelse return 0;
1106 - create_action_table(s, "ShowKeybinds");
1102 + createActionTable(s, "ShowKeybinds");
1107 1103 return 1;
1108 1104 }
1109 1105
1110 -fn lua_set_master_factor(state: ?*c.lua_State) callconv(.c) c_int {
1106 +fn luaSetMasterFactor(state: ?*c.lua_State) callconv(.c) c_int {
1111 1107 const s = state orelse return 0;
1112 1108 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1113 - create_action_table_with_int(s, "ResizeMaster", delta);
1109 + createActionTableWithInt(s, "ResizeMaster", delta);
1114 1110 return 1;
1115 1111 }
1116 1112
1117 -fn lua_inc_num_master(state: ?*c.lua_State) callconv(.c) c_int {
1113 +fn luaIncNumMaster(state: ?*c.lua_State) callconv(.c) c_int {
1118 1114 const s = state orelse return 0;
1119 1115 const delta: i32 = @intCast(c.lua_tointegerx(s, 1, null));
1120 1116 if (delta > 0) {
1121 - create_action_table(s, "IncMaster");
1117 + createActionTable(s, "IncMaster");
1122 1118 } else {
1123 - create_action_table(s, "DecMaster");
1119 + createActionTable(s, "DecMaster");
1124 1120 }
1125 1121 return 1;
1126 1122 }
1127 1123
1128 -fn get_string_arg(state: *c.lua_State, idx: c_int) ?[]const u8 {
1124 +fn getStringArg(state: *c.lua_State, idx: c_int) ?[]const u8 {
1129 1125 if (c.lua_type(state, idx) != c.LUA_TSTRING) return null;
1130 - return get_lua_string(state, idx);
1126 + return getLuaString(state, idx);
1131 1127 }
1132 1128
1133 -fn extract_spawn_command(state: *c.lua_State, idx: c_int) ?[]const u8 {
1129 +fn extractSpawnCommand(state: *c.lua_State, idx: c_int) ?[]const u8 {
1134 1130 const len = c.lua_rawlen(state, idx);
1135 1131 if (len == 0) return null;
1136 1132
1137 1133 if (len >= 3) {
1138 1134 _ = c.lua_rawgeti(state, idx, 1);
1139 - const first = get_lua_string(state, -1);
1135 + const first = getLuaString(state, -1);
1140 1136 c.lua_settop(state, -2);
1141 1137
1142 1138 _ = c.lua_rawgeti(state, idx, 2);
1143 - const second = get_lua_string(state, -1);
1139 + const second = getLuaString(state, -1);
1144 1140 c.lua_settop(state, -2);
1145 1141
1146 1142 if (first != null and second != null and
1147 1143 std.mem.eql(u8, first.?, "sh") and std.mem.eql(u8, second.?, "-c"))
1148 1144 {
1149 1145 _ = c.lua_rawgeti(state, idx, 3);
1150 - const cmd = get_lua_string(state, -1);
1146 + const cmd = getLuaString(state, -1);
1151 1147 c.lua_settop(state, -2);
1152 1148 return cmd;
1153 1149 }
1154 1150 }
1155 1151
1156 1152 _ = c.lua_rawgeti(state, idx, 1);
1157 - const first_elem = get_lua_string(state, -1);
1153 + const first_elem = getLuaString(state, -1);
1158 1154 c.lua_settop(state, -2);
1159 1155 return first_elem;
1160 1156 }
1161 1157
1162 -fn get_lua_string(state: *c.lua_State, idx: c_int) ?[]const u8 {
1158 +fn getLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1163 1159 const cstr = c.lua_tolstring(state, idx, null);
1164 1160 if (cstr == null) return null;
1165 1161 return std.mem.span(cstr);
1166 1162 }
1167 1163
1168 -fn dupe_lua_string(state: *c.lua_State, idx: c_int) ?[]const u8 {
1164 +fn dupeLuaString(state: *c.lua_State, idx: c_int) ?[]const u8 {
1169 1165 const cfg = config orelse return null;
1170 - const lua_str = get_lua_string(state, idx) orelse return null;
1166 + const lua_str = getLuaString(state, idx) orelse return null;
1171 1167 const arena_allocator = cfg.string_arena.allocator();
1172 1168 const duped = arena_allocator.dupe(u8, lua_str) catch return null;
1173 1169 return duped;
1174 1170 }
1175 1171
1176 -fn parse_color(state: *c.lua_State, idx: c_int) u32 {
1172 +fn parseColor(state: *c.lua_State, idx: c_int) u32 {
1177 1173 const lua_type = c.lua_type(state, idx);
1178 1174 if (lua_type == c.LUA_TNUMBER) {
1179 1175 return @intCast(c.lua_tointegerx(state, idx, null));
1180 1176 }
1181 1177 if (lua_type == c.LUA_TSTRING) {
1182 - const str = get_lua_string(state, idx) orelse return 0;
1178 + const str = getLuaString(state, idx) orelse return 0;
1183 1179 if (str.len > 0 and str[0] == '#') {
1184 1180 return std.fmt.parseInt(u32, str[1..], 16) catch return 0;
1185 1181 }
@@ -1191,15 +1187,15 @@ fn parse_color(state: *c.lua_State, idx: c_int) u32 {
1191 1187 return 0;
1192 1188 }
1193 1189
1194 -fn parse_scheme(state: *c.lua_State) ColorScheme {
1190 +fn parseScheme(state: *c.lua_State) ColorScheme {
1195 1191 return ColorScheme{
1196 - .foreground = parse_color(state, 1),
1197 - .background = parse_color(state, 2),
1198 - .border = parse_color(state, 3),
1192 + .foreground = parseColor(state, 1),
1193 + .background = parseColor(state, 2),
1194 + .border = parseColor(state, 3),
1199 1195 };
1200 1196 }
1201 1197
1202 -fn parse_modifiers(state: *c.lua_State, idx: c_int) u32 {
1198 +fn parseModifiers(state: *c.lua_State, idx: c_int) u32 {
1203 1199 var mod_mask: u32 = 0;
1204 1200
1205 1201 if (c.lua_type(state, idx) != c.LUA_TTABLE) return mod_mask;
@@ -1208,8 +1204,8 @@ fn parse_modifiers(state: *c.lua_State, idx: c_int) u32 {
1208 1204 var i: usize = 1;
1209 1205 while (i <= len) : (i += 1) {
1210 1206 _ = c.lua_rawgeti(state, idx, @intCast(i));
1211 - if (get_lua_string(state, -1)) |mod_str| {
1212 - const parsed = parse_single_modifier(mod_str);
1207 + if (getLuaString(state, -1)) |mod_str| {
1208 + const parsed = parseSingleModifier(mod_str);
1213 1209 mod_mask |= parsed;
1214 1210 }
1215 1211 c.lua_settop(state, -2);
@@ -1218,11 +1214,11 @@ fn parse_modifiers(state: *c.lua_State, idx: c_int) u32 {
1218 1214 return mod_mask;
1219 1215 }
1220 1216
1221 -fn parse_modifiers_at_top(state: *c.lua_State) u32 {
1222 - return parse_modifiers(state, -1);
1217 +fn parseModifiersAtTop(state: *c.lua_State) u32 {
1218 + return parseModifiers(state, -1);
1223 1219 }
1224 1220
1225 -fn parse_single_modifier(name: []const u8) u32 {
1221 +fn parseSingleModifier(name: []const u8) u32 {
1226 1222 if (std.mem.eql(u8, name, "Mod4") or std.mem.eql(u8, name, "mod4") or std.mem.eql(u8, name, "super")) {
1227 1223 return (1 << 6);
1228 1224 } else if (std.mem.eql(u8, name, "Mod1") or std.mem.eql(u8, name, "mod1") or std.mem.eql(u8, name, "alt")) {
@@ -1235,7 +1231,7 @@ fn parse_single_modifier(name: []const u8) u32 {
1235 1231 return 0;
1236 1232 }
1237 1233
1238 -fn parse_action(name: []const u8) ?Action {
1234 +fn parseAction(name: []const u8) ?Action {
1239 1235 const action_map = .{
1240 1236 .{ "Spawn", Action.spawn },
1241 1237 .{ "SpawnTerminal", Action.spawn_terminal },
@@ -1275,7 +1271,7 @@ fn parse_action(name: []const u8) ?Action {
1275 1271 return null;
1276 1272 }
1277 1273
1278 -fn key_name_to_keysym(name: []const u8) ?u64 {
1274 +fn keynameToKeysym(name: []const u8) ?u64 {
1279 1275 const key_map = .{
1280 1276 .{ "Return", 0xff0d },
1281 1277 .{ "Enter", 0xff0d },
+69 −0 src/keyboard/chord.zig new file mode 100644
@@ -0,0 +1,69 @@
1 +const std = @import("std");
2 +const xlib = @import("../x11/xlib.zig");
3 +const config = @import("../config/config.zig");
4 +
5 +pub const max_chord_len: u8 = 4;
6 +/// How long (in milliseconds) the user has between key presses within
7 +/// a chord before the sequence is abandoned and state is reset.
8 +pub const timeout_ms: i64 = 1000;
9 +
10 +/// Tracks the in-progress key-chord sequence.
11 +///
12 +/// Owned by `WindowManager`. Call `update` on every key press; it returns
13 +/// whether the sequence is still live. Call `reset` to abandon the
14 +/// current sequence and release the keyboard grab if one is held.
15 +pub const ChordState = struct {
16 + keys: [max_chord_len]config.KeyPress = .{config.KeyPress{}} ** max_chord_len,
17 + index: u8 = 0,
18 + last_timestamp: i64 = 0,
19 + keyboard_grabbed: bool = false,
20 +
21 + /// Push a new key press onto the sequence and update the timestamp.
22 + ///
23 + /// Returns `false` if the sequence is already at maximum length, in which
24 + /// case the caller should call `reset` before retrying.
25 + pub fn push(self: *ChordState, key: config.KeyPress) bool {
26 + if (self.index >= max_chord_len) return false;
27 + self.keys[self.index] = key;
28 + self.index += 1;
29 + self.last_timestamp = std.time.milliTimestamp();
30 +
31 + return true;
32 + }
33 +
34 + /// Returns true if the sequence has timed out and should be reset.
35 + pub fn isTimedOut(self: *const ChordState) bool {
36 + if (self.index == 0) return false;
37 + return (std.time.milliTimestamp() - self.last_timestamp) >= timeout_ms;
38 + }
39 +
40 + /// Clears the sequence and releases the keyboard grab if one is held.
41 + ///
42 + /// `display` may be null only during early startup before the connection
43 + /// is open, in normal operation it should always be provided.
44 + pub fn reset(self: *ChordState, display: ?*xlib.Display) void {
45 + self.keys = .{config.KeyPress{}} ** max_chord_len;
46 + self.index = 0;
47 + self.last_timestamp = 0;
48 +
49 + if (self.keyboard_grabbed) {
50 + if (display) |dpy| {
51 + _ = xlib.XUngrabKeyboard(dpy, xlib.CurrentTime);
52 + }
53 + self.keyboard_grabbed = false;
54 + }
55 + }
56 +
57 + /// Try to grab the keyboard for exclusive input during a partial match.
58 + ///
59 + /// Sets `keyboard_grabbed` on success. Safe to call repeatedly,
60 + /// does nothing if already grabbed.
61 + pub fn grabKeyboard(self: *ChordState, display: *xlib.Display, root: xlib.Window) void {
62 + if (self.keyboard_grabbed) return;
63 +
64 + const result = xlib.XGrabKeyboard(display, root, xlib.True, xlib.GrabModeAsync, xlib.GrabModeAsync, xlib.CurrentTime);
65 + if (result == xlib.GrabSuccess) {
66 + self.keyboard_grabbed = true;
67 + }
68 + }
69 +};
+5 −5 src/layouts/grid.zig
@@ -13,8 +13,8 @@ pub const layout = monitor_mod.Layout{
13 13
14 14 pub fn grid(monitor: *Monitor) void {
15 15 var client_count: u32 = 0;
16 - var current = client_mod.next_tiled(monitor.clients);
17 - while (current) |_| : (current = client_mod.next_tiled(current.?.next)) {
16 + var current = client_mod.nextTiled(monitor.clients);
17 + while (current) |_| : (current = client_mod.nextTiled(current.?.next)) {
18 18 client_count += 1;
19 19 }
20 20
@@ -26,7 +26,7 @@ pub fn grid(monitor: *Monitor) void {
26 26 const gap_inner_v = monitor.gap_inner_v;
27 27
28 28 if (client_count == 1) {
29 - const client = client_mod.next_tiled(monitor.clients).?;
29 + const client = client_mod.nextTiled(monitor.clients).?;
30 30 tiling.resize(
31 31 client,
32 32 monitor.win_x + gap_outer_v,
@@ -50,8 +50,8 @@ pub fn grid(monitor: *Monitor) void {
50 50 const cell_height: i32 = @divTrunc(available_height, @as(i32, @intCast(rows)));
51 51
52 52 var index: u32 = 0;
53 - current = client_mod.next_tiled(monitor.clients);
54 - while (current) |client| : (current = client_mod.next_tiled(client.next)) {
53 + current = client_mod.nextTiled(monitor.clients);
54 + while (current) |client| : (current = client_mod.nextTiled(client.next)) {
55 55 const row = index / cols;
56 56 const col = index % cols;
57 57
+2 −2 src/layouts/monocle.zig
@@ -16,7 +16,7 @@ pub fn monocle(monitor: *Monitor) void {
16 16 const gap_h = monitor.gap_outer_h;
17 17 const gap_v = monitor.gap_outer_v;
18 18
19 - var current = client_mod.next_tiled(monitor.clients);
19 + var current = client_mod.nextTiled(monitor.clients);
20 20 while (current) |client| {
21 21 tiling.resize(
22 22 client,
@@ -26,6 +26,6 @@ pub fn monocle(monitor: *Monitor) void {
26 26 monitor.win_h - 2 * gap_h - 2 * client.border_width,
27 27 false,
28 28 );
29 - current = client_mod.next_tiled(client.next);
29 + current = client_mod.nextTiled(client.next);
30 30 }
31 31 }
+17 −17 src/layouts/scrolling.zig
@@ -14,8 +14,8 @@ pub const layout = monitor_mod.Layout{
14 14
15 15 pub fn scroll(monitor: *Monitor) void {
16 16 var client_count: u32 = 0;
17 - var current = client_mod.next_tiled(monitor.clients);
18 - while (current) |_| : (current = client_mod.next_tiled(current.?.next)) {
17 + var current = client_mod.nextTiled(monitor.clients);
18 + while (current) |_| : (current = client_mod.nextTiled(current.?.next)) {
19 19 client_count += 1;
20 20 }
21 21
@@ -37,8 +37,8 @@ pub fn scroll(monitor: *Monitor) void {
37 37 const height = available_height;
38 38
39 39 var index: u32 = 0;
40 - current = client_mod.next_tiled(monitor.clients);
41 - while (current) |client| : (current = client_mod.next_tiled(client.next)) {
40 + current = client_mod.nextTiled(monitor.clients);
41 + while (current) |client| : (current = client_mod.nextTiled(client.next)) {
42 42 const window_right = x_pos + window_width;
43 43 const screen_left = monitor.win_x;
44 44 const screen_right = monitor.win_x + monitor.win_w;
@@ -54,7 +54,7 @@ pub fn scroll(monitor: *Monitor) void {
54 54 false,
55 55 );
56 56 } else {
57 - tiling.resize_client(
57 + tiling.resizeClient(
58 58 client,
59 59 -2 * window_width,
60 60 y_pos,
@@ -67,7 +67,7 @@ pub fn scroll(monitor: *Monitor) void {
67 67 }
68 68 }
69 69
70 -pub fn get_scroll_step(monitor: *Monitor) i32 {
70 +pub fn getScrollStep(monitor: *Monitor) i32 {
71 71 const gap_outer_v = monitor.gap_outer_v;
72 72 const gap_inner_v = monitor.gap_inner_v;
73 73 const visible_count: u32 = @intCast(@max(1, monitor.nmaster));
@@ -77,37 +77,37 @@ pub fn get_scroll_step(monitor: *Monitor) i32 {
77 77 return window_width + gap_inner_v;
78 78 }
79 79
80 -pub fn get_max_scroll(monitor: *Monitor) i32 {
80 +pub fn getMaxScroll(monitor: *Monitor) i32 {
81 81 var client_count: u32 = 0;
82 - var current = client_mod.next_tiled(monitor.clients);
83 - while (current) |_| : (current = client_mod.next_tiled(current.?.next)) {
82 + var current = client_mod.nextTiled(monitor.clients);
83 + while (current) |_| : (current = client_mod.nextTiled(current.?.next)) {
84 84 client_count += 1;
85 85 }
86 86
87 87 const visible_count: u32 = @intCast(@max(1, monitor.nmaster));
88 88 if (client_count <= visible_count) return 0;
89 89
90 - const scroll_step = get_scroll_step(monitor);
90 + const scroll_step = getScrollStep(monitor);
91 91 const scrollable = client_count - visible_count;
92 92 return scroll_step * @as(i32, @intCast(scrollable));
93 93 }
94 94
95 -pub fn get_window_index(monitor: *Monitor, target: *Client) ?u32 {
95 +pub fn getWindowIndex(monitor: *Monitor, target: *Client) ?u32 {
96 96 var index: u32 = 0;
97 - var current = client_mod.next_tiled(monitor.clients);
98 - while (current) |client| : (current = client_mod.next_tiled(client.next)) {
97 + var current = client_mod.nextTiled(monitor.clients);
98 + while (current) |client| : (current = client_mod.nextTiled(client.next)) {
99 99 if (client == target) return index;
100 100 index += 1;
101 101 }
102 102 return null;
103 103 }
104 104
105 -pub fn get_target_scroll_for_window(monitor: *Monitor, target: *Client) i32 {
106 - const index = get_window_index(monitor, target) orelse return 0;
105 +pub fn getTargetScrollForWindow(monitor: *Monitor, target: *Client) i32 {
106 + const index = getWindowIndex(monitor, target) orelse return 0;
107 107 if (index == 0) return 0;
108 108
109 - const scroll_step = get_scroll_step(monitor);
110 - const max_scroll = get_max_scroll(monitor);
109 + const scroll_step = getScrollStep(monitor);
110 + const max_scroll = getMaxScroll(monitor);
111 111
112 112 const target_scroll = scroll_step * @as(i32, @intCast(index));
113 113 return @min(target_scroll, max_scroll);
+27 −27 src/layouts/tiling.zig
@@ -16,16 +16,16 @@ pub var screen_width: i32 = 0;
16 16 pub var screen_height: i32 = 0;
17 17 pub var bar_height: i32 = 0;
18 18
19 -pub fn set_display(display: *xlib.Display) void {
19 +pub fn setDisplay(display: *xlib.Display) void {
20 20 display_handle = display;
21 21 }
22 22
23 -pub fn set_screen_size(width: i32, height: i32) void {
23 +pub fn setScreenSize(width: i32, height: i32) void {
24 24 screen_width = width;
25 25 screen_height = height;
26 26 }
27 27
28 -pub fn set_bar_height(height: i32) void {
28 +pub fn setBarHeight(height: i32) void {
29 29 bar_height = height;
30 30 }
31 31
@@ -36,7 +36,7 @@ pub fn tile(monitor: *Monitor) void {
36 36 var gap_inner_v: i32 = 0;
37 37 var client_count: u32 = 0;
38 38
39 - get_gaps(monitor, &gap_outer_h, &gap_outer_v, &gap_inner_h, &gap_inner_v, &client_count);
39 + getGaps(monitor, &gap_outer_h, &gap_outer_v, &gap_inner_h, &gap_inner_v, &client_count);
40 40 if (client_count == 0) return;
41 41
42 42 const nmaster: i32 = monitor.nmaster;
@@ -62,29 +62,29 @@ pub fn tile(monitor: *Monitor) void {
62 62 var stack_facts: f32 = 0;
63 63 var master_rest: i32 = 0;
64 64 var stack_rest: i32 = 0;
65 - get_facts(monitor, master_height, stack_height, &master_facts, &stack_facts, &master_rest, &stack_rest);
65 + getFacts(monitor, master_height, stack_height, &master_facts, &stack_facts, &master_rest, &stack_rest);
66 66
67 67 var index: u32 = 0;
68 - var current = client_mod.next_tiled(monitor.clients);
69 - while (current) |client| : (current = client_mod.next_tiled(client.next)) {
68 + var current = client_mod.nextTiled(monitor.clients);
69 + while (current) |client| : (current = client_mod.nextTiled(client.next)) {
70 70 if (index < nmaster_count) {
71 71 const height = @as(i32, @intFromFloat(@as(f32, @floatFromInt(master_height)) / master_facts)) + (if (index < @as(u32, @intCast(master_rest))) @as(i32, 1) else @as(i32, 0)) - 2 * client.border_width;
72 72 resize(client, master_x, master_y, master_width - 2 * client.border_width, height, false);
73 - master_y += get_client_height(client) + gap_inner_h;
73 + master_y += getClientHeight(client) + gap_inner_h;
74 74 } else {
75 75 const stack_index = index - nmaster_count;
76 76 const height = @as(i32, @intFromFloat(@as(f32, @floatFromInt(stack_height)) / stack_facts)) + (if (stack_index < @as(u32, @intCast(stack_rest))) @as(i32, 1) else @as(i32, 0)) - 2 * client.border_width;
77 77 resize(client, stack_x, stack_y, stack_width - 2 * client.border_width, height, false);
78 - stack_y += get_client_height(client) + gap_inner_h;
78 + stack_y += getClientHeight(client) + gap_inner_h;
79 79 }
80 80 index += 1;
81 81 }
82 82 }
83 83
84 -fn get_gaps(monitor: *Monitor, gap_outer_h: *i32, gap_outer_v: *i32, gap_inner_h: *i32, gap_inner_v: *i32, client_count: *u32) void {
84 +fn getGaps(monitor: *Monitor, gap_outer_h: *i32, gap_outer_v: *i32, gap_inner_h: *i32, gap_inner_v: *i32, client_count: *u32) void {
85 85 var count: u32 = 0;
86 - var current = client_mod.next_tiled(monitor.clients);
87 - while (current) |client| : (current = client_mod.next_tiled(client.next)) {
86 + var current = client_mod.nextTiled(monitor.clients);
87 + while (current) |client| : (current = client_mod.nextTiled(client.next)) {
88 88 count += 1;
89 89 }
90 90
@@ -95,10 +95,10 @@ fn get_gaps(monitor: *Monitor, gap_outer_h: *i32, gap_outer_v: *i32, gap_inner_h
95 95 client_count.* = count;
96 96 }
97 97
98 -fn get_facts(monitor: *Monitor, master_size: i32, stack_size: i32, master_factor: *f32, stack_factor: *f32, master_rest: *i32, stack_rest: *i32) void {
98 +fn getFacts(monitor: *Monitor, master_size: i32, stack_size: i32, master_factor: *f32, stack_factor: *f32, master_rest: *i32, stack_rest: *i32) void {
99 99 var count: u32 = 0;
100 - var current = client_mod.next_tiled(monitor.clients);
101 - while (current) |client| : (current = client_mod.next_tiled(client.next)) {
100 + var current = client_mod.nextTiled(monitor.clients);
101 + while (current) |client| : (current = client_mod.nextTiled(client.next)) {
102 102 count += 1;
103 103 }
104 104
@@ -122,15 +122,15 @@ fn get_facts(monitor: *Monitor, master_size: i32, stack_size: i32, master_factor
122 122 stack_rest.* = stack_size - stack_total;
123 123 }
124 124
125 -fn get_client_width(client: *Client) i32 {
125 +fn getClientWidth(client: *Client) i32 {
126 126 return client.width + 2 * client.border_width;
127 127 }
128 128
129 -fn get_client_height(client: *Client) i32 {
129 +fn getClientHeight(client: *Client) i32 {
130 130 return client.height + 2 * client.border_width;
131 131 }
132 132
133 -pub fn apply_size_hints(client: *Client, target_x: *i32, target_y: *i32, target_width: *i32, target_height: *i32, interact: bool) bool {
133 +pub fn applySizeHints(client: *Client, target_x: *i32, target_y: *i32, target_width: *i32, target_height: *i32, interact: bool) bool {
134 134 const monitor = client.monitor orelse return false;
135 135
136 136 target_width.* = @max(1, target_width.*);
@@ -138,10 +138,10 @@ pub fn apply_size_hints(client: *Client, target_x: *i32, target_y: *i32, target_
138 138
139 139 if (interact) {
140 140 if (target_x.* > screen_width) {
141 - target_x.* = screen_width - get_client_width(client);
141 + target_x.* = screen_width - getClientWidth(client);
142 142 }
143 143 if (target_y.* > screen_height) {
144 - target_y.* = screen_height - get_client_height(client);
144 + target_y.* = screen_height - getClientHeight(client);
145 145 }
146 146 if (target_x.* + target_width.* + 2 * client.border_width < 0) {
147 147 target_x.* = 0;
@@ -151,10 +151,10 @@ pub fn apply_size_hints(client: *Client, target_x: *i32, target_y: *i32, target_
151 151 }
152 152 } else {
153 153 if (target_x.* >= monitor.win_x + monitor.win_w) {
154 - target_x.* = monitor.win_x + monitor.win_w - get_client_width(client);
154 + target_x.* = monitor.win_x + monitor.win_w - getClientWidth(client);
155 155 }
156 156 if (target_y.* >= monitor.win_y + monitor.win_h) {
157 - target_y.* = monitor.win_y + monitor.win_h - get_client_height(client);
157 + target_y.* = monitor.win_y + monitor.win_h - getClientHeight(client);
158 158 }
159 159 if (target_x.* + target_width.* + 2 * client.border_width <= monitor.win_x) {
160 160 target_x.* = monitor.win_x;
@@ -224,12 +224,12 @@ pub fn resize(client: *Client, target_x: i32, target_y: i32, target_width: i32,
224 224 var final_width = target_width;
225 225 var final_height = target_height;
226 226
227 - if (apply_size_hints(client, &final_x, &final_y, &final_width, &final_height, interact)) {
228 - resize_client(client, final_x, final_y, final_width, final_height);
227 + if (applySizeHints(client, &final_x, &final_y, &final_width, &final_height, interact)) {
228 + resizeClient(client, final_x, final_y, final_width, final_height);
229 229 }
230 230 }
231 231
232 -pub fn resize_client(client: *Client, target_x: i32, target_y: i32, target_width: i32, target_height: i32) void {
232 +pub fn resizeClient(client: *Client, target_x: i32, target_y: i32, target_width: i32, target_height: i32) void {
233 233 client.old_x = client.x;
234 234 client.old_y = client.y;
235 235 client.old_width = client.width;
@@ -255,11 +255,11 @@ pub fn resize_client(client: *Client, target_x: i32, target_y: i32, target_width
255 255 &window_changes,
256 256 );
257 257
258 - send_configure(client);
258 + sendConfigure(client);
259 259 _ = xlib.XSync(display, xlib.False);
260 260 }
261 261
262 -pub fn send_configure(client: *Client) void {
262 +pub fn sendConfigure(client: *Client) void {
263 263 const display = display_handle orelse return;
264 264
265 265 var configure_event: xlib.c.XConfigureEvent = undefined;
+92 −2505 src/main.zig Large diff collapsed, click to expand
@@ -1,84 +1,93 @@
1 1 const std = @import("std");
2 -const VERSION = "v0.11.2";
3 -const display_mod = @import("x11/display.zig");
4 -const events = @import("x11/events.zig");
5 -const xlib = @import("x11/xlib.zig");
6 -const client_mod = @import("client.zig");
7 -const monitor_mod = @import("monitor.zig");
8 -const tiling = @import("layouts/tiling.zig");
9 -const monocle = @import("layouts/monocle.zig");
10 -const floating = @import("layouts/floating.zig");
11 -const scrolling = @import("layouts/scrolling.zig");
12 -const grid = @import("layouts/grid.zig");
13 -const animations = @import("animations.zig");
14 -const bar_mod = @import("bar/bar.zig");
15 -const blocks_mod = @import("bar/blocks/blocks.zig");
2 +const build_options = @import("build_options");
3 +
4 +const actions = @import("wm/actions.zig");
5 +const handlers = @import("wm/handlers.zig");
6 +const window_manager = @import("wm/wm.zig");
16 7 const config_mod = @import("config/config.zig");
17 8 const lua = @import("config/lua.zig");
18 -const overlay_mod = @import("overlay.zig");
19 9
20 -const Display = display_mod.Display;
21 -const Client = client_mod.Client;
22 -const Monitor = monitor_mod.Monitor;
10 +const WindowManager = window_manager.WindowManager;
23 11
24 -var running: bool = true;
25 -var gpa = std.heap.GeneralPurposeAllocator(.{}){};
12 +pub fn main() !void {
13 + var gpa = std.heap.GeneralPurposeAllocator(.{}){};
14 + const allocator = gpa.allocator();
15 + defer _ = gpa.deinit();
26 16
27 -var wm_protocols: xlib.Atom = 0;
28 -var wm_delete: xlib.Atom = 0;
29 -var wm_state: xlib.Atom = 0;
30 -var wm_take_focus: xlib.Atom = 0;
17 + const default_config_path = try getConfigPath(allocator);
18 + defer allocator.free(default_config_path);
31 19
32 -var net_supported: xlib.Atom = 0;
33 -var net_wm_name: xlib.Atom = 0;
34 -var net_wm_state: xlib.Atom = 0;
35 -var net_wm_check: xlib.Atom = 0;
36 -var net_wm_state_fullscreen: xlib.Atom = 0;
37 -var net_active_window: xlib.Atom = 0;
38 -var net_wm_window_type: xlib.Atom = 0;
39 -var net_wm_window_type_dialog: xlib.Atom = 0;
40 -var net_client_list: xlib.Atom = 0;
20 + var config_path: []const u8 = default_config_path;
21 + var validate_mode: bool = false;
22 + var args = std.process.args();
23 + _ = args.skip();
24 + while (args.next()) |arg| {
25 + if (std.mem.eql(u8, arg, "-c") or std.mem.eql(u8, arg, "--config")) {
26 + if (args.next()) |path| config_path = path;
27 + } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) {
28 + printHelp();
29 + return;
30 + } else if (std.mem.eql(u8, arg, "-v") or std.mem.eql(u8, arg, "--version")) {
31 + std.debug.print("v{s}\n", .{build_options.version});
32 + return;
33 + } else if (std.mem.eql(u8, arg, "--init")) {
34 + initConfig(allocator);
35 + return;
36 + } else if (std.mem.eql(u8, arg, "--validate")) {
37 + validate_mode = true;
38 + }
39 + }
41 40
42 -var wm_check_window: xlib.Window = 0;
41 + if (validate_mode) {
42 + try validateConfig(allocator, config_path);
43 + return;
44 + }
43 45
44 -var border_color_focused: c_ulong = 0x6dade3;
45 -var border_color_unfocused: c_ulong = 0x444444;
46 -var border_width: i32 = 2;
47 -var gap_outer_v: i32 = 5;
48 -var gap_outer_h: i32 = 5;
49 -var gap_inner_h: i32 = 5;
50 -var gap_inner_v: i32 = 5;
46 + std.debug.print("oxwm starting\n", .{});
51 47
52 -var tags: [9][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9" };
48 + var config = config_mod.Config.init(allocator);
53 49
54 -var cursor_normal: xlib.Cursor = 0;
55 -var cursor_resize: xlib.Cursor = 0;
56 -var cursor_move: xlib.Cursor = 0;
50 + if (lua.init(&config)) {
51 + const loaded = if (std.fs.cwd().statFile(config_path)) |_| blk: {
52 + break :blk lua.loadFile(config_path);
53 + } else |_| blk: {
54 + initConfig(allocator);
55 + break :blk lua.loadConfig();
56 + };
57 57
58 -const NormalState: c_long = 1;
59 -const WithdrawnState: c_long = 0;
60 -const IconicState: c_long = 3;
61 -const IsViewable: c_int = 2;
62 -const snap_distance: i32 = 32;
58 + if (loaded) {
59 + std.debug.print("loaded config from {s}\n", .{config_path});
60 + } else {
61 + std.debug.print("no config found, using defaults\n", .{});
62 + config_mod.initializeDefaultConfig(&config);
63 + }
64 + } else {
65 + std.debug.print("failed to init lua, using defaults\n", .{});
66 + config_mod.initializeDefaultConfig(&config);
67 + }
63 68
64 -var numlock_mask: c_uint = 0;
69 + var wm = WindowManager.init(allocator, config, config_path) catch |err| {
70 + std.debug.print("failed to start window manager: {}\n", .{err});
71 + return;
72 + };
73 + defer wm.deinit();
65 74
66 -var config: config_mod.Config = undefined;
67 -var display_global: ?*Display = null;
68 -var config_path_global: ?[]const u8 = null;
75 + std.debug.print("display opened: screen={d} root=0x{x}\n", .{ wm.display.screen, wm.display.root });
76 + std.debug.print("successfully became window manager\n", .{});
77 + std.debug.print("atoms initialized with EWMH support\n", .{});
69 78
70 -var scroll_animation: animations.Scroll_Animation = .{};
71 -var animation_config: animations.Animation_Config = .{ .duration_ms = 150, .easing = .ease_out };
79 + wm.grabKeybinds();
80 + wm.scanExistingWindows(window_manager.core.manage);
72 81
73 -var chord_keys: [4]config_mod.Key_Press = [_]config_mod.Key_Press{.{}} ** 4;
74 -var chord_index: u8 = 0;
75 -var chord_timestamp: i64 = 0;
76 -const chord_timeout_ms: i64 = 1000;
77 -var keyboard_grabbed: bool = false;
82 + try runAutostartCommands(&wm);
83 + std.debug.print("entering event loop\n", .{});
84 + wm.run(handlers.handleEvent, window_manager.core.tickAnimations);
78 85
79 -var keybind_overlay: ?*overlay_mod.Keybind_Overlay = null;
86 + lua.deinit();
87 + std.debug.print("oxwm exiting\n", .{});
88 +}
80 89
81 -fn print_help() void {
90 +fn printHelp() void {
82 91 std.debug.print(
83 92 \\oxwm - A window manager
84 93 \\
@@ -104,19 +113,8 @@ fn print_help() void {
104 113 , .{});
105 114 }
106 115
107 -fn get_config_path(allocator: std.mem.Allocator) ![]u8 {
108 - const config_home = std.posix.getenv("XDG_CONFIG_HOME") orelse blk: {
109 - const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir;
110 - break :blk try std.fs.path.join(allocator, &.{ home, ".config" });
111 - };
112 - defer if (std.posix.getenv("XDG_CONFIG_HOME") == null) allocator.free(config_home);
113 -
114 - const config_path = try std.fs.path.join(allocator, &.{ config_home, "oxwm", "config.lua" });
115 - return config_path;
116 -}
117 -
118 -fn init_config(allocator: std.mem.Allocator) void {
119 - const config_path = get_config_path(allocator) catch return;
116 +fn initConfig(allocator: std.mem.Allocator) void {
117 + const config_path = getConfigPath(allocator) catch return;
120 118 defer allocator.free(config_path);
121 119
122 120 const template = @embedFile("templates/config.lua");
@@ -151,8 +149,20 @@ fn init_config(allocator: std.mem.Allocator) void {
151 149 std.debug.print("No compilation needed - changes take effect immediately!\n", .{});
152 150 }
153 151
154 -fn validate_config(allocator: std.mem.Allocator, config_path: []const u8) !void {
155 - config = config_mod.Config.init(allocator);
152 +fn getConfigPath(allocator: std.mem.Allocator) ![]u8 {
153 + const config_home = std.posix.getenv("XDG_CONFIG_HOME") orelse blk: {
154 + const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir;
155 + break :blk try std.fs.path.join(allocator, &.{ home, ".config" });
156 + };
157 + // TODO: wtf is this shit
158 + defer if (std.posix.getenv("XDG_CONFIG_HOME") == null) allocator.free(config_home);
159 +
160 + const config_path = try std.fs.path.join(allocator, &.{ config_home, "oxwm", "config.lua" });
161 + return config_path;
162 +}
163 +
164 +fn validateConfig(allocator: std.mem.Allocator, config_path: []const u8) !void {
165 + var config = config_mod.Config.init(allocator);
156 166 defer config.deinit();
157 167
158 168 if (!lua.init(&config)) {
@@ -167,7 +177,7 @@ fn validate_config(allocator: std.mem.Allocator, config_path: []const u8) !void
167 177 std.process.exit(1);
168 178 };
169 179
170 - if (lua.load_file(config_path)) {
180 + if (lua.loadFile(config_path)) {
171 181 std.debug.print("✓ config valid: {s}\n", .{config_path});
172 182 std.process.exit(0);
173 183 } else {
@@ -176,2432 +186,9 @@ fn validate_config(allocator: std.mem.Allocator, config_path: []const u8) !void
176 186 }
177 187 }
178 188
179 -pub fn main() !void {
180 - const allocator = gpa.allocator();
181 - defer _ = gpa.deinit();
182 -
183 - const default_config_path = try get_config_path(allocator);
184 - defer allocator.free(default_config_path);
185 -
186 - var config_path: []const u8 = default_config_path;
187 - var validate_mode: bool = false;
188 - var args = std.process.args();
189 - _ = args.skip();
190 - while (args.next()) |arg| {
191 - if (std.mem.eql(u8, arg, "-c") or std.mem.eql(u8, arg, "--config")) {
192 - if (args.next()) |path| config_path = path;
193 - } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) {
194 - print_help();
195 - return;
196 - } else if (std.mem.eql(u8, arg, "-v") or std.mem.eql(u8, arg, "--version")) {
197 - std.debug.print("{s}\n", .{VERSION});
198 - return;
199 - } else if (std.mem.eql(u8, arg, "--init")) {
200 - init_config(allocator);
201 - return;
202 - } else if (std.mem.eql(u8, arg, "--validate")) {
203 - validate_mode = true;
204 - }
205 - }
206 -
207 - if (validate_mode) {
208 - try validate_config(allocator, config_path);
209 - return;
210 - }
211 -
212 - std.debug.print("oxwm starting\n", .{});
213 -
214 - config = config_mod.Config.init(allocator);
215 - defer config.deinit();
216 - config_mod.set_config(&config);
217 -
218 - if (lua.init(&config)) {
219 - const loaded = if (std.fs.cwd().statFile(config_path)) |_|
220 - lua.load_file(config_path)
221 - else |_| blk: {
222 - init_config(allocator);
223 - break :blk lua.load_config();
224 - };
225 -
226 - if (loaded) {
227 - config_path_global = config_path;
228 - std.debug.print("loaded config from {s}\n", .{config_path});
229 - apply_config_values();
230 - } else {
231 - std.debug.print("no config found, using defaults\n", .{});
232 - initialize_default_config();
233 - }
234 - } else {
235 - std.debug.print("failed to init lua, using defaults\n", .{});
236 - initialize_default_config();
237 - }
238 -
239 - var display = Display.open() catch |err| {
240 - std.debug.print("failed to open display: {}\n", .{err});
241 - return;
242 - };
243 - defer display.close();
244 -
245 - display_global = &display;
246 -
247 - std.debug.print("display opened: screen={d} root=0x{x}\n", .{ display.screen, display.root });
248 - std.debug.print("screen size: {d}x{d}\n", .{ display.screen_width(), display.screen_height() });
249 -
250 - display.become_window_manager() catch |err| {
251 - std.debug.print("failed to become window manager: {}\n", .{err});
252 - return;
253 - };
254 -
255 - std.debug.print("successfully became window manager\n", .{});
256 -
257 - setup_atoms(&display);
258 - setup_cursors(&display);
259 - client_mod.init(allocator);
260 - monitor_mod.init(allocator);
261 - monitor_mod.set_root_window(display.root, display.handle);
262 - tiling.set_display(display.handle);
263 - tiling.set_screen_size(display.screen_width(), display.screen_height());
264 -
265 - setup_monitors(&display);
266 - setup_bars(allocator, &display);
267 - setup_overlay(allocator, &display);
268 - grab_keybinds(&display);
269 - scan_existing_windows(&display);
270 -
271 - try run_autostart_commands(allocator, config.autostart.items);
272 - std.debug.print("entering event loop\n", .{});
273 - run_event_loop(&display);
274 -
275 - bar_mod.destroy_bars(allocator, display.handle);
276 -
277 - var mon = monitor_mod.monitors;
278 - while (mon) |m| {
279 - const next = m.next;
280 - monitor_mod.destroy(m);
281 - mon = next;
282 - }
283 -
284 - if (keybind_overlay) |overlay| {
285 - overlay.deinit(allocator);
286 - }
287 -
288 - lua.deinit();
289 - std.debug.print("oxwm exiting\n", .{});
290 -}
291 -
292 -fn setup_atoms(display: *Display) void {
293 - wm_protocols = xlib.XInternAtom(display.handle, "WM_PROTOCOLS", xlib.False);
294 - wm_delete = xlib.XInternAtom(display.handle, "WM_DELETE_WINDOW", xlib.False);
295 - wm_state = xlib.XInternAtom(display.handle, "WM_STATE", xlib.False);
296 - wm_take_focus = xlib.XInternAtom(display.handle, "WM_TAKE_FOCUS", xlib.False);
297 -
298 - net_active_window = xlib.XInternAtom(display.handle, "_NET_ACTIVE_WINDOW", xlib.False);
299 - net_supported = xlib.XInternAtom(display.handle, "_NET_SUPPORTED", xlib.False);
300 - net_wm_name = xlib.XInternAtom(display.handle, "_NET_WM_NAME", xlib.False);
301 - net_wm_state = xlib.XInternAtom(display.handle, "_NET_WM_STATE", xlib.False);
302 - net_wm_check = xlib.XInternAtom(display.handle, "_NET_SUPPORTING_WM_CHECK", xlib.False);
303 - net_wm_state_fullscreen = xlib.XInternAtom(display.handle, "_NET_WM_STATE_FULLSCREEN", xlib.False);
304 - net_wm_window_type = xlib.XInternAtom(display.handle, "_NET_WM_WINDOW_TYPE", xlib.False);
305 - net_wm_window_type_dialog = xlib.XInternAtom(display.handle, "_NET_WM_WINDOW_TYPE_DIALOG", xlib.False);
306 - net_client_list = xlib.XInternAtom(display.handle, "_NET_CLIENT_LIST", xlib.False);
307 -
308 - const utf8_string = xlib.XInternAtom(display.handle, "UTF8_STRING", xlib.False);
309 -
310 - wm_check_window = xlib.XCreateSimpleWindow(display.handle, display.root, 0, 0, 1, 1, 0, 0, 0);
311 - _ = xlib.XChangeProperty(display.handle, wm_check_window, net_wm_check, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&wm_check_window), 1);
312 - _ = xlib.XChangeProperty(display.handle, wm_check_window, net_wm_name, utf8_string, 8, xlib.PropModeReplace, "oxwm", 6);
313 - _ = xlib.XChangeProperty(display.handle, display.root, net_wm_check, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&wm_check_window), 1);
314 -
315 - var net_atoms = [_]xlib.Atom{ net_supported, net_wm_name, net_wm_state, net_wm_check, net_wm_state_fullscreen, net_active_window, net_wm_window_type, net_wm_window_type_dialog, net_client_list };
316 - _ = xlib.XChangeProperty(display.handle, display.root, net_supported, xlib.XA_ATOM, 32, xlib.PropModeReplace, @ptrCast(&net_atoms), net_atoms.len);
317 -
318 - _ = xlib.XDeleteProperty(display.handle, display.root, net_client_list);
319 -
320 - std.debug.print("atoms initialized with EWMH support\n", .{});
321 -}
322 -
323 -fn setup_cursors(display: *Display) void {
324 - cursor_normal = xlib.XCreateFontCursor(display.handle, xlib.XC_left_ptr);
325 - cursor_resize = xlib.XCreateFontCursor(display.handle, xlib.XC_sizing);
326 - cursor_move = xlib.XCreateFontCursor(display.handle, xlib.XC_fleur);
327 - _ = xlib.XDefineCursor(display.handle, display.root, cursor_normal);
328 -}
329 -
330 -fn setup_bars(allocator: std.mem.Allocator, display: *Display) void {
331 - var current_monitor = monitor_mod.monitors;
332 - var last_bar: ?*bar_mod.Bar = null;
333 -
334 - while (current_monitor) |monitor| {
335 - const bar = bar_mod.Bar.create(allocator, display.handle, display.screen, monitor, config);
336 - if (bar) |created_bar| {
337 - if (tiling.bar_height == 0) {
338 - tiling.set_bar_height(created_bar.height);
339 - }
340 -
341 - if (config.blocks.items.len > 0) {
342 - for (config.blocks.items) |cfg_block| {
343 - const block = config_block_to_bar_block(cfg_block);
344 - created_bar.add_block(block);
345 - }
346 - } else {
347 - created_bar.add_block(blocks_mod.Block.init_ram("", 5, 0x7aa2f7, true));
348 - created_bar.add_block(blocks_mod.Block.init_static(" | ", 0x666666, false));
349 - created_bar.add_block(blocks_mod.Block.init_datetime("", "%H:%M", 1, 0x0db9d7, true));
350 - }
351 -
352 - if (last_bar) |prev| {
353 - prev.next = created_bar;
354 - } else {
355 - bar_mod.bars = created_bar;
356 - }
357 - last_bar = created_bar;
358 - std.debug.print("bar created for monitor {d}\n", .{monitor.num});
359 - }
360 - current_monitor = monitor.next;
361 - }
362 -}
363 -
364 -fn setup_overlay(allocator: std.mem.Allocator, display: *Display) void {
365 - keybind_overlay = overlay_mod.Keybind_Overlay.init(display.handle, display.screen, display.root, config.font, allocator);
366 -}
367 -
368 -fn config_block_to_bar_block(cfg: config_mod.Block) blocks_mod.Block {
369 - return switch (cfg.block_type) {
370 - .static => blocks_mod.Block.init_static(cfg.format, cfg.color, cfg.underline),
371 - .datetime => blocks_mod.Block.init_datetime(cfg.format, cfg.datetime_format orelse "%H:%M", cfg.interval, cfg.color, cfg.underline),
372 - .ram => blocks_mod.Block.init_ram(cfg.format, cfg.interval, cfg.color, cfg.underline),
373 - .shell => blocks_mod.Block.init_shell(cfg.format, cfg.command orelse "", cfg.interval, cfg.color, cfg.underline),
374 - .battery => blocks_mod.Block.init_battery(
375 - cfg.format_charging orelse "",
376 - cfg.format_discharging orelse "",
377 - cfg.format_full orelse "",
378 - cfg.battery_name orelse "BAT0",
379 - cfg.interval,
380 - cfg.color,
381 - cfg.underline,
382 - ),
383 - .cpu_temp => blocks_mod.Block.init_cpu_temp(
384 - cfg.format,
385 - cfg.thermal_zone orelse "thermal_zone0",
386 - cfg.interval,
387 - cfg.color,
388 - cfg.underline,
389 - ),
390 - };
391 -}
392 -
393 -fn setup_monitors(display: *Display) void {
394 - std.debug.print("checking xinerama...\n", .{});
395 - if (xlib.XineramaIsActive(display.handle) != 0) {
396 - std.debug.print("xinerama is active!\n", .{});
397 - var screen_count: c_int = 0;
398 - const screens = xlib.XineramaQueryScreens(display.handle, &screen_count);
399 -
400 - if (screen_count > 0 and screens != null) {
401 - var prev_monitor: ?*Monitor = null;
402 - var index: usize = 0;
403 -
404 - while (index < @as(usize, @intCast(screen_count))) : (index += 1) {
405 - const screen = screens[index];
406 - const mon = monitor_mod.create() orelse continue;
407 -
408 - mon.num = @intCast(index);
409 - mon.mon_x = screen.x_org;
410 - mon.mon_y = screen.y_org;
411 - mon.mon_w = screen.width;
412 - mon.mon_h = screen.height;
413 - mon.win_x = screen.x_org;
414 - mon.win_y = screen.y_org;
415 - mon.win_w = screen.width;
416 - mon.win_h = screen.height;
417 - mon.lt[0] = &tiling.layout;
418 - mon.lt[1] = &monocle.layout;
419 - mon.lt[2] = &floating.layout;
420 - mon.lt[3] = &scrolling.layout;
421 - mon.lt[4] = &grid.layout;
422 - for (0..10) |i| {
423 - mon.pertag.ltidxs[i][0] = mon.lt[0];
424 - mon.pertag.ltidxs[i][1] = mon.lt[1];
425 - mon.pertag.ltidxs[i][2] = mon.lt[2];
426 - mon.pertag.ltidxs[i][3] = mon.lt[3];
427 - mon.pertag.ltidxs[i][4] = mon.lt[4];
428 - }
429 -
430 - init_monitor_gaps(mon);
431 -
432 - if (prev_monitor) |prev| {
433 - prev.next = mon;
434 - } else {
435 - monitor_mod.monitors = mon;
436 - }
437 - prev_monitor = mon;
438 -
439 - std.debug.print("monitor {d}: {d}x{d} at ({d},{d})\n", .{ index, mon.mon_w, mon.mon_h, mon.mon_x, mon.mon_y });
440 - }
441 -
442 - monitor_mod.selected_monitor = monitor_mod.monitors;
443 - _ = xlib.XFree(@ptrCast(screens));
444 - return;
445 - }
446 - } else {
447 - std.debug.print("xinerama not active, using single monitor\n", .{});
448 - }
449 -
450 - const mon = monitor_mod.create() orelse return;
451 - mon.mon_x = 0;
452 - mon.mon_y = 0;
453 - mon.mon_w = display.screen_width();
454 - mon.mon_h = display.screen_height();
455 - mon.win_x = 0;
456 - mon.win_y = 0;
457 - mon.win_w = display.screen_width();
458 - mon.win_h = display.screen_height();
459 - mon.lt[0] = &tiling.layout;
460 - mon.lt[1] = &monocle.layout;
461 - mon.lt[2] = &floating.layout;
462 - mon.lt[3] = &scrolling.layout;
463 - mon.lt[4] = &grid.layout;
464 - for (0..10) |i| {
465 - mon.pertag.ltidxs[i][0] = mon.lt[0];
466 - mon.pertag.ltidxs[i][1] = mon.lt[1];
467 - mon.pertag.ltidxs[i][2] = mon.lt[2];
468 - mon.pertag.ltidxs[i][3] = mon.lt[3];
469 - mon.pertag.ltidxs[i][4] = mon.lt[4];
470 - }
471 -
472 - init_monitor_gaps(mon);
473 -
474 - monitor_mod.monitors = mon;
475 - monitor_mod.selected_monitor = mon;
476 - std.debug.print("monitor created: {d}x{d}\n", .{ mon.mon_w, mon.mon_h });
477 -}
478 -
479 -fn apply_config_values() void {
480 - border_color_focused = config.border_focused;
481 - border_color_unfocused = config.border_unfocused;
482 - border_width = config.border_width;
483 - gap_inner_h = config.gap_inner_h;
484 - gap_inner_v = config.gap_inner_v;
485 - gap_outer_h = config.gap_outer_h;
486 - gap_outer_v = config.gap_outer_v;
487 - tags = config.tags;
488 -}
489 -
490 -fn init_monitor_gaps(mon: *Monitor) void {
491 - const any_gap_nonzero = config.gap_inner_h != 0 or config.gap_inner_v != 0 or
492 - config.gap_outer_h != 0 or config.gap_outer_v != 0;
493 -
494 - if (config.gaps_enabled and any_gap_nonzero) {
495 - mon.gap_inner_h = config.gap_inner_h;
496 - mon.gap_inner_v = config.gap_inner_v;
497 - mon.gap_outer_h = config.gap_outer_h;
498 - mon.gap_outer_v = config.gap_outer_v;
499 - } else {
500 - mon.gap_inner_h = 0;
501 - mon.gap_inner_v = 0;
502 - mon.gap_outer_h = 0;
503 - mon.gap_outer_v = 0;
504 - }
505 -}
506 -
507 -fn make_keybind(mod: u32, key: u64, action: config_mod.Action) config_mod.Keybind {
508 - var kb: config_mod.Keybind = .{ .action = action };
509 - kb.keys[0] = .{ .mod_mask = mod, .keysym = key };
510 - kb.key_count = 1;
511 - return kb;
512 -}
513 -
514 -fn make_keybind_int(mod: u32, key: u64, action: config_mod.Action, int_arg: i32) config_mod.Keybind {
515 - var kb = make_keybind(mod, key, action);
516 - kb.int_arg = int_arg;
517 - return kb;
518 -}
519 -
520 -fn make_keybind_str(mod: u32, key: u64, action: config_mod.Action, str_arg: []const u8) config_mod.Keybind {
521 - var kb = make_keybind(mod, key, action);
522 - kb.str_arg = str_arg;
523 - return kb;
524 -}
525 -
526 -fn initialize_default_config() void {
527 - const mod_key: u32 = 1 << 6;
528 - const shift_key: u32 = 1 << 0;
529 - const control_key: u32 = 1 << 2;
530 -
531 - config.add_keybind(make_keybind(mod_key, 0xff0d, .spawn_terminal)) catch {};
532 - config.add_keybind(make_keybind_str(mod_key, 'd', .spawn, "rofi -show drun")) catch {};
533 - config.add_keybind(make_keybind_str(mod_key, 's', .spawn, "maim -s | xclip -selection clipboard -t image/png")) catch {};
534 - config.add_keybind(make_keybind(mod_key, 'q', .kill_client)) catch {};
535 - config.add_keybind(make_keybind(mod_key | shift_key, 'q', .quit)) catch {};
536 - config.add_keybind(make_keybind(mod_key | shift_key, 'r', .reload_config)) catch {};
537 - config.add_keybind(make_keybind(mod_key, 'j', .focus_next)) catch {};
538 - config.add_keybind(make_keybind(mod_key, 'k', .focus_prev)) catch {};
539 - config.add_keybind(make_keybind(mod_key | shift_key, 'j', .move_next)) catch {};
540 - config.add_keybind(make_keybind(mod_key | shift_key, 'k', .move_prev)) catch {};
541 - config.add_keybind(make_keybind_int(mod_key, 'h', .resize_master, -50)) catch {};
542 - config.add_keybind(make_keybind_int(mod_key, 'l', .resize_master, 50)) catch {};
543 - config.add_keybind(make_keybind(mod_key, 'i', .inc_master)) catch {};
544 - config.add_keybind(make_keybind(mod_key, 'p', .dec_master)) catch {};
545 - config.add_keybind(make_keybind(mod_key, 'a', .toggle_gaps)) catch {};
546 - config.add_keybind(make_keybind(mod_key, 'f', .toggle_fullscreen)) catch {};
547 - config.add_keybind(make_keybind(mod_key, 0x0020, .toggle_floating)) catch {};
548 - config.add_keybind(make_keybind(mod_key, 'n', .cycle_layout)) catch {};
549 - config.add_keybind(make_keybind_int(mod_key, 0x002c, .focus_monitor, -1)) catch {};
550 - config.add_keybind(make_keybind_int(mod_key, 0x002e, .focus_monitor, 1)) catch {};
551 - config.add_keybind(make_keybind_int(mod_key | shift_key, 0x002c, .send_to_monitor, -1)) catch {};
552 - config.add_keybind(make_keybind_int(mod_key | shift_key, 0x002e, .send_to_monitor, 1)) catch {};
553 -
554 - var tag_index: i32 = 0;
555 - while (tag_index < 9) : (tag_index += 1) {
556 - const keysym: u64 = @as(u64, '1') + @as(u64, @intCast(tag_index));
557 - config.add_keybind(make_keybind_int(mod_key, keysym, .view_tag, tag_index)) catch {};
558 - config.add_keybind(make_keybind_int(mod_key | shift_key, keysym, .move_to_tag, tag_index)) catch {};
559 - config.add_keybind(make_keybind_int(mod_key | control_key, keysym, .toggle_view_tag, tag_index)) catch {};
560 - config.add_keybind(make_keybind_int(mod_key | control_key | shift_key, keysym, .toggle_tag, tag_index)) catch {};
561 - }
562 -
563 - config.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 1, .action = .move_mouse }) catch {};
564 - config.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {};
565 -}
566 -
567 -fn grab_keybinds(display: *Display) void {
568 - update_numlock_mask(display);
569 - const modifiers = [_]c_uint{ 0, xlib.LockMask, numlock_mask, numlock_mask | xlib.LockMask };
570 -
571 - _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root);
572 -
573 - for (config.keybinds.items) |keybind| {
574 - if (keybind.key_count == 0) continue;
575 - const first_key = keybind.keys[0];
576 - const keycode = xlib.XKeysymToKeycode(display.handle, @intCast(first_key.keysym));
577 - if (keycode != 0) {
578 - for (modifiers) |modifier| {
579 - _ = xlib.XGrabKey(
580 - display.handle,
581 - keycode,
582 - first_key.mod_mask | modifier,
583 - display.root,
584 - xlib.True,
585 - xlib.GrabModeAsync,
586 - xlib.GrabModeAsync,
587 - );
588 - }
589 - }
590 - }
591 -
592 - for (config.buttons.items) |button| {
593 - if (button.click == .client_win) {
594 - for (modifiers) |modifier| {
595 - _ = xlib.XGrabButton(
596 - display.handle,
597 - @intCast(button.button),
598 - button.mod_mask | modifier,
599 - display.root,
600 - xlib.True,
601 - xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
602 - xlib.GrabModeAsync,
603 - xlib.GrabModeAsync,
604 - xlib.None,
605 - xlib.None,
606 - );
607 - }
608 - }
609 - }
610 -
611 - std.debug.print("grabbed {d} keybinds from config\n", .{config.keybinds.items.len});
612 -}
613 -
614 -fn get_state(display: *Display, window: xlib.Window) c_long {
615 - var actual_type: xlib.Atom = 0;
616 - var actual_format: c_int = 0;
617 - var num_items: c_ulong = 0;
618 - var bytes_after: c_ulong = 0;
619 - var prop: [*c]u8 = null;
620 -
621 - const result = xlib.XGetWindowProperty(
622 - display.handle,
623 - window,
624 - wm_state,
625 - 0,
626 - 2,
627 - xlib.False,
628 - wm_state,
629 - &actual_type,
630 - &actual_format,
631 - &num_items,
632 - &bytes_after,
633 - &prop,
634 - );
635 -
636 - if (result != 0 or actual_type != wm_state or num_items < 1) {
637 - if (prop != null) {
638 - _ = xlib.XFree(prop);
639 - }
640 - return WithdrawnState;
641 - }
642 -
643 - const state: c_long = @as(*c_long, @ptrCast(@alignCast(prop))).*;
644 - _ = xlib.XFree(prop);
645 - return state;
646 -}
647 -
648 -fn scan_existing_windows(display: *Display) void {
649 - var root_return: xlib.Window = undefined;
650 - var parent_return: xlib.Window = undefined;
651 - var children: [*c]xlib.Window = undefined;
652 - var num_children: c_uint = undefined;
653 -
654 - if (xlib.XQueryTree(display.handle, display.root, &root_return, &parent_return, &children, &num_children) == 0) {
655 - return;
656 - }
657 -
658 - var index: c_uint = 0;
659 - while (index < num_children) : (index += 1) {
660 - var window_attrs: xlib.XWindowAttributes = undefined;
661 - if (xlib.XGetWindowAttributes(display.handle, children[index], &window_attrs) == 0) {
662 - continue;
663 - }
664 - if (window_attrs.override_redirect != 0) {
665 - continue;
666 - }
667 - var trans: xlib.Window = 0;
668 - if (xlib.XGetTransientForHint(display.handle, children[index], &trans) != 0) {
669 - continue;
670 - }
671 - if (window_attrs.map_state == IsViewable or get_state(display, children[index]) == IconicState) {
672 - manage(display, children[index], &window_attrs);
673 - }
674 - }
675 -
676 - index = 0;
677 - while (index < num_children) : (index += 1) {
678 - var window_attrs: xlib.XWindowAttributes = undefined;
679 - if (xlib.XGetWindowAttributes(display.handle, children[index], &window_attrs) == 0) {
680 - continue;
681 - }
682 - var trans: xlib.Window = 0;
683 - if (xlib.XGetTransientForHint(display.handle, children[index], &trans) != 0) {
684 - if (window_attrs.map_state == IsViewable or get_state(display, children[index]) == IconicState) {
685 - manage(display, children[index], &window_attrs);
686 - }
687 - }
688 - }
689 -
690 - if (children != null) {
691 - _ = xlib.XFree(@ptrCast(children));
692 - }
693 -}
694 -
695 -fn run_event_loop(display: *Display) void {
696 - const x11_fd = xlib.XConnectionNumber(display.handle);
697 - var fds = [_]std.posix.pollfd{
698 - .{ .fd = x11_fd, .events = std.posix.POLL.IN, .revents = 0 },
699 - };
700 -
701 - _ = xlib.XSync(display.handle, xlib.False);
702 -
703 - while (running) {
704 - while (xlib.XPending(display.handle) > 0) {
705 - var event = display.next_event();
706 - handle_event(display, &event);
707 - }
708 -
709 - tick_animations();
710 -
711 - var current_bar = bar_mod.bars;
712 - while (current_bar) |bar| {
713 - bar.update_blocks();
714 - bar.draw(display.handle, &tags);
715 - current_bar = bar.next;
716 - }
717 -
718 - const poll_timeout: i32 = if (scroll_animation.is_active()) 16 else 1000;
719 - _ = std.posix.poll(&fds, poll_timeout) catch 0;
720 - }
721 -}
722 -
723 -fn handle_event(display: *Display, event: *xlib.XEvent) void {
724 - const event_type = events.get_event_type(event);
725 -
726 - if (event_type == .button_press) {
727 - std.debug.print("EVENT: button_press received type={d}\n", .{event.type});
728 - }
729 -
730 - switch (event_type) {
731 - .map_request => handle_map_request(display, &event.xmaprequest),
732 - .configure_request => handle_configure_request(display, &event.xconfigurerequest),
733 - .key_press => handle_key_press(display, &event.xkey),
734 - .destroy_notify => handle_destroy_notify(display, &event.xdestroywindow),
735 - .unmap_notify => handle_unmap_notify(display, &event.xunmap),
736 - .enter_notify => handle_enter_notify(display, &event.xcrossing),
737 - .focus_in => handle_focus_in(display, &event.xfocus),
738 - .motion_notify => handle_motion_notify(display, &event.xmotion),
739 - .client_message => handle_client_message(display, &event.xclient),
740 - .button_press => handle_button_press(display, &event.xbutton),
741 - .expose => handle_expose(display, &event.xexpose),
742 - .property_notify => handle_property_notify(display, &event.xproperty),
743 - else => {},
744 - }
745 -}
746 -
747 -fn handle_map_request(display: *Display, event: *xlib.XMapRequestEvent) void {
748 - std.debug.print("map_request: window=0x{x}\n", .{event.window});
749 -
750 - var window_attributes: xlib.XWindowAttributes = undefined;
751 - if (xlib.XGetWindowAttributes(display.handle, event.window, &window_attributes) == 0) {
752 - return;
753 - }
754 - if (window_attributes.override_redirect != 0) {
755 - return;
756 - }
757 - if (client_mod.window_to_client(event.window) != null) {
758 - return;
759 - }
760 -
761 - manage(display, event.window, &window_attributes);
762 -}
763 -
764 -fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttributes) void {
765 - const client = client_mod.create(win) orelse return;
766 - var trans: xlib.Window = 0;
767 -
768 - client.x = window_attrs.x;
769 - client.y = window_attrs.y;
770 - client.width = window_attrs.width;
771 - client.height = window_attrs.height;
772 - client.old_x = window_attrs.x;
773 - client.old_y = window_attrs.y;
774 - client.old_width = window_attrs.width;
775 - client.old_height = window_attrs.height;
776 - client.old_border_width = window_attrs.border_width;
777 - client.border_width = border_width;
778 -
779 - update_title(display, client);
780 -
781 - if (xlib.XGetTransientForHint(display.handle, win, &trans) != 0) {
782 - if (client_mod.window_to_client(trans)) |transient_client| {
783 - client.monitor = transient_client.monitor;
784 - client.tags = transient_client.tags;
785 - }
786 - }
787 -
788 - if (client.monitor == null) {
789 - client.monitor = monitor_mod.selected_monitor;
790 - apply_rules(display, client);
791 - }
792 -
793 - const monitor = client.monitor orelse return;
794 -
795 - if (client.x + client.width > monitor.win_x + monitor.win_w) {
796 - client.x = monitor.win_x + monitor.win_w - client.width - 2 * client.border_width;
797 - }
798 - if (client.y + client.height > monitor.win_y + monitor.win_h) {
799 - client.y = monitor.win_y + monitor.win_h - client.height - 2 * client.border_width;
800 - }
801 - client.x = @max(client.x, monitor.win_x);
802 - client.y = @max(client.y, monitor.win_y);
803 -
804 - _ = xlib.XSetWindowBorderWidth(display.handle, win, @intCast(client.border_width));
805 - _ = xlib.XSetWindowBorder(display.handle, win, border_color_unfocused);
806 - tiling.send_configure(client);
807 -
808 - update_window_type(display, client);
809 - update_size_hints(display, client);
810 - update_wm_hints(display, client);
811 -
812 - _ = xlib.XSelectInput(
813 - display.handle,
814 - win,
815 - xlib.EnterWindowMask | xlib.FocusChangeMask | xlib.PropertyChangeMask | xlib.StructureNotifyMask,
816 - );
817 - grabbuttons(display, client, false);
818 -
819 - if (!client.is_floating) {
820 - client.is_floating = trans != 0 or client.is_fixed;
821 - client.old_state = client.is_floating;
822 - }
823 - if (client.is_floating) {
824 - _ = xlib.XRaiseWindow(display.handle, client.window);
825 - }
826 -
827 - client_mod.attach_aside(client);
828 - client_mod.attach_stack(client);
829 -
830 - _ = xlib.XChangeProperty(display.handle, display.root, net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
831 - _ = xlib.XMoveResizeWindow(display.handle, client.window, client.x + 2 * display.screen_width(), client.y, @intCast(client.width), @intCast(client.height));
832 - set_client_state(display, client, NormalState);
833 -
834 - if (client.monitor == monitor_mod.selected_monitor) {
835 - const selmon = monitor_mod.selected_monitor orelse return;
836 - unfocus_client(display, selmon.sel, false);
837 - }
838 - monitor.sel = client;
839 -
840 - if (is_scrolling_layout(monitor)) {
841 - monitor.scroll_offset = 0;
842 - }
843 -
844 - arrange(monitor);
845 - _ = xlib.XMapWindow(display.handle, win);
846 - focus(display, null);
847 -}
848 -
849 -fn handle_configure_request(display: *Display, event: *xlib.XConfigureRequestEvent) void {
850 - const client = client_mod.window_to_client(event.window);
851 -
852 - if (client) |managed_client| {
853 - if ((event.value_mask & xlib.c.CWBorderWidth) != 0) {
854 - managed_client.border_width = event.border_width;
855 - } else if (managed_client.is_floating or (managed_client.monitor != null and managed_client.monitor.?.lt[managed_client.monitor.?.sel_lt] == null)) {
856 - const monitor = managed_client.monitor orelse return;
857 - if ((event.value_mask & xlib.c.CWX) != 0) {
858 - managed_client.old_x = managed_client.x;
859 - managed_client.x = monitor.mon_x + event.x;
860 - }
861 - if ((event.value_mask & xlib.c.CWY) != 0) {
862 - managed_client.old_y = managed_client.y;
863 - managed_client.y = monitor.mon_y + event.y;
864 - }
865 - if ((event.value_mask & xlib.c.CWWidth) != 0) {
866 - managed_client.old_width = managed_client.width;
867 - managed_client.width = event.width;
868 - }
869 - if ((event.value_mask & xlib.c.CWHeight) != 0) {
870 - managed_client.old_height = managed_client.height;
871 - managed_client.height = event.height;
872 - }
873 - const client_full_width = managed_client.width + managed_client.border_width * 2;
874 - const client_full_height = managed_client.height + managed_client.border_width * 2;
875 - if ((managed_client.x + managed_client.width) > monitor.mon_x + monitor.mon_w and managed_client.is_floating) {
876 - managed_client.x = monitor.mon_x + @divTrunc(monitor.mon_w, 2) - @divTrunc(client_full_width, 2);
877 - }
878 - if ((managed_client.y + managed_client.height) > monitor.mon_y + monitor.mon_h and managed_client.is_floating) {
879 - managed_client.y = monitor.mon_y + @divTrunc(monitor.mon_h, 2) - @divTrunc(client_full_height, 2);
880 - }
881 - if (((event.value_mask & (xlib.c.CWX | xlib.c.CWY)) != 0) and ((event.value_mask & (xlib.c.CWWidth | xlib.c.CWHeight)) == 0)) {
882 - tiling.send_configure(managed_client);
883 - }
884 - if (client_mod.is_visible(managed_client)) {
885 - _ = xlib.XMoveResizeWindow(display.handle, managed_client.window, managed_client.x, managed_client.y, @intCast(managed_client.width), @intCast(managed_client.height));
886 - }
887 - } else {
888 - tiling.send_configure(managed_client);
889 - }
890 - } else {
891 - var changes: xlib.XWindowChanges = undefined;
892 - changes.x = event.x;
893 - changes.y = event.y;
894 - changes.width = event.width;
895 - changes.height = event.height;
896 - changes.border_width = event.border_width;
897 - changes.sibling = event.above;
898 - changes.stack_mode = event.detail;
899 - _ = xlib.XConfigureWindow(display.handle, event.window, @intCast(event.value_mask), &changes);
900 - }
901 - _ = xlib.XSync(display.handle, xlib.False);
902 -}
903 -
904 -fn reset_chord_state() void {
905 - chord_index = 0;
906 - chord_keys = [_]config_mod.Key_Press{.{}} ** 4;
907 - chord_timestamp = 0;
908 - if (keyboard_grabbed) {
909 - if (display_global) |dsp| {
910 - _ = xlib.XUngrabKeyboard(dsp.handle, xlib.CurrentTime);
911 - }
912 - keyboard_grabbed = false;
913 - }
914 -}
915 -
916 -fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void {
917 - const keysym = xlib.XKeycodeToKeysym(display.handle, @intCast(event.keycode), 0);
918 -
919 - if (keybind_overlay) |overlay| {
920 - if (overlay.handle_key(keysym)) {
921 - return;
922 - }
923 - }
924 -
925 - const clean_state = event.state & ~@as(c_uint, xlib.LockMask | xlib.Mod2Mask);
926 - const current_time = std.time.milliTimestamp();
927 -
928 - if (chord_index > 0 and (current_time - chord_timestamp) > chord_timeout_ms) {
929 - reset_chord_state();
930 - }
931 -
932 - chord_keys[chord_index] = .{ .mod_mask = clean_state, .keysym = keysym };
933 - chord_index += 1;
934 - chord_timestamp = current_time;
935 -
936 - for (config.keybinds.items) |keybind| {
937 - if (keybind.key_count == 0) continue;
938 -
939 - if (keybind.key_count == chord_index) {
940 - var matches = true;
941 - var i: u8 = 0;
942 - while (i < keybind.key_count) : (i += 1) {
943 - if (chord_keys[i].keysym != keybind.keys[i].keysym or
944 - chord_keys[i].mod_mask != keybind.keys[i].mod_mask)
945 - {
946 - matches = false;
947 - break;
948 - }
949 - }
950 - if (matches) {
951 - execute_action(display, keybind.action, keybind.int_arg, keybind.str_arg);
952 - reset_chord_state();
953 - return;
954 - }
955 - }
956 - }
957 -
958 - var has_partial_match = false;
959 - for (config.keybinds.items) |keybind| {
960 - if (keybind.key_count > chord_index) {
961 - var matches = true;
962 - var i: u8 = 0;
963 - while (i < chord_index) : (i += 1) {
964 - if (chord_keys[i].keysym != keybind.keys[i].keysym or
965 - chord_keys[i].mod_mask != keybind.keys[i].mod_mask)
966 - {
967 - matches = false;
968 - break;
969 - }
970 - }
971 - if (matches) {
972 - has_partial_match = true;
973 - break;
974 - }
975 - }
976 - }
977 -
978 - if (has_partial_match and !keyboard_grabbed) {
979 - const grab_result = xlib.XGrabKeyboard(
980 - display.handle,
981 - display.root,
982 - xlib.True,
983 - xlib.GrabModeAsync,
984 - xlib.GrabModeAsync,
985 - xlib.CurrentTime,
986 - );
987 - if (grab_result == xlib.GrabSuccess) {
988 - keyboard_grabbed = true;
989 - }
990 - } else if (!has_partial_match) {
991 - reset_chord_state();
992 - }
993 -}
994 -
995 -fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8) void {
996 - switch (action) {
997 - .spawn_terminal => spawn_terminal(),
998 - .spawn => {
999 - if (str_arg) |cmd| {
1000 - spawn_command(cmd);
1001 - }
1002 - },
1003 - .kill_client => kill_focused(display),
1004 - .quit => {
1005 - std.debug.print("quit keybind pressed\n", .{});
1006 - running = false;
1007 - },
1008 - .reload_config => reload_config(display),
1009 - .restart => reload_config(display),
1010 - .show_keybinds => {
1011 - if (keybind_overlay) |overlay| {
1012 - const mon = monitor_mod.selected_monitor orelse monitor_mod.monitors;
1013 - if (mon) |m| {
1014 - overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h);
1015 - }
1016 - }
1017 - },
1018 - .focus_next => focusstack(display, 1),
1019 - .focus_prev => focusstack(display, -1),
1020 - .move_next => movestack(display, 1),
1021 - .move_prev => movestack(display, -1),
1022 - .resize_master => setmfact(@as(f32, @floatFromInt(int_arg)) / 1000.0),
1023 - .inc_master => incnmaster(1),
1024 - .dec_master => incnmaster(-1),
1025 - .toggle_floating => toggle_floating(display),
1026 - .toggle_fullscreen => toggle_fullscreen(display),
1027 - .toggle_gaps => toggle_gaps(),
1028 - .cycle_layout => cycle_layout(),
1029 - .set_layout => set_layout(str_arg),
1030 - .set_layout_tiling => set_layout_index(0),
1031 - .set_layout_floating => set_layout_index(2),
1032 - .view_tag => {
1033 - const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
1034 - view(display, tag_mask);
1035 - },
1036 - .view_next_tag => view_adjacent_tag(display, 1),
1037 - .view_prev_tag => view_adjacent_tag(display, -1),
1038 - .view_next_nonempty_tag => view_adjacent_nonempty_tag(display, 1),
1039 - .view_prev_nonempty_tag => view_adjacent_nonempty_tag(display, -1),
1040 - .move_to_tag => {
1041 - const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
1042 - tag_client(display, tag_mask);
1043 - },
1044 - .toggle_view_tag => {
1045 - const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
1046 - toggle_view(display, tag_mask);
1047 - },
1048 - .toggle_tag => {
1049 - const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
1050 - toggle_client_tag(display, tag_mask);
1051 - },
1052 - .focus_monitor => focusmon(display, int_arg),
1053 - .send_to_monitor => sendmon(display, int_arg),
1054 - .scroll_left => {
1055 - scroll_layout(-1);
1056 - },
1057 - .scroll_right => {
1058 - scroll_layout(1);
1059 - },
1060 - }
1061 -}
1062 -
1063 -fn reload_config(display: *Display) void {
1064 - std.debug.print("reloading config...\n", .{});
1065 -
1066 - ungrab_keybinds(display);
1067 -
1068 - config.keybinds.clearRetainingCapacity();
1069 - config.buttons.clearRetainingCapacity();
1070 - config.rules.clearRetainingCapacity();
1071 - config.blocks.clearRetainingCapacity();
1072 -
1073 - lua.deinit();
1074 - _ = lua.init(&config);
1075 -
1076 - const loaded = if (config_path_global) |path|
1077 - lua.load_file(path)
1078 - else
1079 - lua.load_config();
1080 -
1081 - if (loaded) {
1082 - if (config_path_global) |path| {
1083 - std.debug.print("reloaded config from {s}\n", .{path});
1084 - } else {
1085 - std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{});
1086 - }
1087 - apply_config_values();
1088 - } else {
1089 - std.debug.print("reload failed, restoring defaults\n", .{});
1090 - initialize_default_config();
1091 - }
1092 -
1093 - bar_mod.destroy_bars(gpa.allocator(), display.handle);
1094 - setup_bars(gpa.allocator(), display);
1095 - rebuild_bar_blocks();
1096 -
1097 - grab_keybinds(display);
1098 -}
1099 -
1100 -fn rebuild_bar_blocks() void {
1101 - var current_bar = bar_mod.bars;
1102 - while (current_bar) |bar| {
1103 - bar.clear_blocks();
1104 - if (config.blocks.items.len > 0) {
1105 - for (config.blocks.items) |cfg_block| {
1106 - const block = config_block_to_bar_block(cfg_block);
1107 - bar.add_block(block);
1108 - }
1109 - } else {
1110 - bar.add_block(blocks_mod.Block.init_ram("", 5, 0x7aa2f7, true));
1111 - bar.add_block(blocks_mod.Block.init_static(" | ", 0x666666, false));
1112 - bar.add_block(blocks_mod.Block.init_datetime("", "%H:%M", 1, 0x0db9d7, true));
1113 - }
1114 - current_bar = bar.next;
1115 - }
1116 -}
1117 -
1118 -fn ungrab_keybinds(display: *Display) void {
1119 - _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root);
1120 -}
1121 -
1122 -fn spawn_child_setup() void {
1123 - _ = std.c.setsid();
1124 - if (display_global) |display| {
1125 - const display_fd = xlib.XConnectionNumber(display.handle);
1126 - std.posix.close(@intCast(display_fd));
1127 - }
1128 - const sigchld_handler = std.posix.Sigaction{
1129 - .handler = .{ .handler = std.posix.SIG.DFL },
1130 - .mask = std.mem.zeroes(std.posix.sigset_t),
1131 - .flags = 0,
1132 - };
1133 - std.posix.sigaction(std.posix.SIG.CHLD, &sigchld_handler, null);
1134 -}
1135 -
1136 -fn spawn_command(cmd: []const u8) void {
1137 - std.debug.print("running cmd: {s}\n", .{cmd});
1138 - const pid = std.posix.fork() catch return;
1139 - if (pid == 0) {
1140 - spawn_child_setup();
1141 - var cmd_buf: [1024]u8 = undefined;
1142 - if (cmd.len >= cmd_buf.len) {
1143 - std.posix.exit(1);
1144 - }
1145 - @memcpy(cmd_buf[0..cmd.len], cmd);
1146 - cmd_buf[cmd.len] = 0;
1147 - const argv = [_:null]?[*:0]const u8{ "sh", "-c", @ptrCast(&cmd_buf) };
1148 - _ = std.posix.execvpeZ("sh", &argv, std.c.environ) catch {};
1149 - std.posix.exit(1);
1150 - }
1151 -}
1152 -
1153 -fn spawn_terminal() void {
1154 - const pid = std.posix.fork() catch return;
1155 - if (pid == 0) {
1156 - spawn_child_setup();
1157 - var term_buf: [256]u8 = undefined;
1158 - const terminal = config.terminal;
1159 - if (terminal.len >= term_buf.len) {
1160 - std.posix.exit(1);
1161 - }
1162 - @memcpy(term_buf[0..terminal.len], terminal);
1163 - term_buf[terminal.len] = 0;
1164 - const term_ptr: [*:0]const u8 = @ptrCast(&term_buf);
1165 - const argv = [_:null]?[*:0]const u8{term_ptr};
1166 - _ = std.posix.execvpeZ(term_ptr, &argv, std.c.environ) catch {};
1167 - std.posix.exit(1);
1168 - }
1169 -}
1170 -
1171 -fn movestack(display: *Display, direction: i32) void {
1172 - const monitor = monitor_mod.selected_monitor orelse return;
1173 - const current = monitor.sel orelse return;
1174 -
1175 - if (current.is_floating) {
1176 - return;
1177 - }
1178 -
1179 - var target: ?*Client = null;
1180 -
1181 - if (direction > 0) {
1182 - target = current.next;
1183 - while (target) |client| {
1184 - if (client_mod.is_visible(client) and !client.is_floating) {
1185 - break;
1186 - }
1187 - target = client.next;
1188 - }
1189 - if (target == null) {
1190 - target = monitor.clients;
1191 - while (target) |client| {
1192 - if (client == current) {
1193 - break;
1194 - }
1195 - if (client_mod.is_visible(client) and !client.is_floating) {
1196 - break;
1197 - }
1198 - target = client.next;
1199 - }
1200 - }
1201 - } else {
1202 - var prev: ?*Client = null;
1203 - var iter = monitor.clients;
1204 - while (iter) |client| {
1205 - if (client == current) {
1206 - break;
1207 - }
1208 - if (client_mod.is_visible(client) and !client.is_floating) {
1209 - prev = client;
1210 - }
1211 - iter = client.next;
1212 - }
1213 - if (prev == null) {
1214 - iter = current.next;
1215 - while (iter) |client| {
1216 - if (client_mod.is_visible(client) and !client.is_floating) {
1217 - prev = client;
1218 - }
1219 - iter = client.next;
1220 - }
1221 - }
1222 - target = prev;
1223 - }
1224 -
1225 - if (target) |swap_client| {
1226 - if (swap_client != current) {
1227 - client_mod.swap_clients(current, swap_client);
1228 - arrange(monitor);
1229 - focus(display, current);
1230 - }
1231 - }
1232 -}
1233 -
1234 -fn toggle_view(display: *Display, tag_mask: u32) void {
1235 - const monitor = monitor_mod.selected_monitor orelse return;
1236 - const new_tags = monitor.tagset[monitor.sel_tags] ^ tag_mask;
1237 - if (new_tags != 0) {
1238 - monitor.tagset[monitor.sel_tags] = new_tags;
1239 -
1240 - if (new_tags == ~@as(u32, 0)) {
1241 - monitor.pertag.prevtag = monitor.pertag.curtag;
1242 - monitor.pertag.curtag = 0;
1243 - }
1244 -
1245 - if ((new_tags & (@as(u32, 1) << @intCast(monitor.pertag.curtag -| 1))) == 0) {
1246 - monitor.pertag.prevtag = monitor.pertag.curtag;
1247 - var i: u32 = 0;
1248 - while (i < 9) : (i += 1) {
1249 - if ((new_tags & (@as(u32, 1) << @intCast(i))) != 0) break;
1250 - }
1251 - monitor.pertag.curtag = i + 1;
1252 - }
1253 -
1254 - monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag];
1255 - monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag];
1256 - monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag];
1257 -
1258 - focus_top_client(display, monitor);
1259 - arrange(monitor);
1260 - bar_mod.invalidate_bars();
1261 - }
1262 -}
1263 -
1264 -fn toggle_client_tag(display: *Display, tag_mask: u32) void {
1265 - const monitor = monitor_mod.selected_monitor orelse return;
1266 - const client = monitor.sel orelse return;
1267 - const new_tags = client.tags ^ tag_mask;
1268 - if (new_tags != 0) {
1269 - client.tags = new_tags;
1270 - focus_top_client(display, monitor);
1271 - arrange(monitor);
1272 - bar_mod.invalidate_bars();
1273 - }
1274 -}
1275 -
1276 -fn toggle_gaps() void {
1277 - const monitor = monitor_mod.selected_monitor orelse return;
1278 - if (monitor.gap_inner_h == 0) {
1279 - monitor.gap_inner_h = gap_inner_v;
1280 - monitor.gap_inner_v = gap_inner_v;
1281 - monitor.gap_outer_h = gap_outer_h;
1282 - monitor.gap_outer_v = gap_outer_v;
1283 - } else {
1284 - monitor.gap_inner_h = 0;
1285 - monitor.gap_inner_v = 0;
1286 - monitor.gap_outer_h = 0;
1287 - monitor.gap_outer_v = 0;
1288 - }
1289 - arrange(monitor);
1290 -}
1291 -
1292 -fn kill_focused(display: *Display) void {
1293 - const selected = monitor_mod.selected_monitor orelse return;
1294 - const client = selected.sel orelse return;
1295 - std.debug.print("killing window: 0x{x}\n", .{client.window});
1296 -
1297 - if (!send_event(display, client, wm_delete)) {
1298 - _ = xlib.XGrabServer(display.handle);
1299 - _ = xlib.XKillClient(display.handle, client.window);
1300 - _ = xlib.XSync(display.handle, xlib.False);
1301 - _ = xlib.XUngrabServer(display.handle);
1302 - }
1303 -}
1304 -
1305 -fn toggle_fullscreen(display: *Display) void {
1306 - const selected = monitor_mod.selected_monitor orelse return;
1307 - const client = selected.sel orelse return;
1308 - set_fullscreen(display, client, !client.is_fullscreen);
1309 -}
1310 -
1311 -fn set_fullscreen(display: *Display, client: *Client, fullscreen: bool) void {
1312 - const monitor = client.monitor orelse return;
1313 -
1314 - if (fullscreen and !client.is_fullscreen) {
1315 - var fullscreen_atom = net_wm_state_fullscreen;
1316 - _ = xlib.XChangeProperty(
1317 - display.handle,
1318 - client.window,
1319 - net_wm_state,
1320 - xlib.XA_ATOM,
1321 - 32,
1322 - xlib.PropModeReplace,
1323 - @ptrCast(&fullscreen_atom),
1324 - 1,
1325 - );
1326 - client.is_fullscreen = true;
1327 - client.old_state = client.is_floating;
1328 - client.old_border_width = client.border_width;
1329 - client.border_width = 0;
1330 - client.is_floating = true;
1331 -
1332 - _ = xlib.XSetWindowBorderWidth(display.handle, client.window, 0);
1333 - tiling.resize_client(client, monitor.mon_x, monitor.mon_y, monitor.mon_w, monitor.mon_h);
1334 - _ = xlib.XRaiseWindow(display.handle, client.window);
1335 -
1336 - std.debug.print("fullscreen enabled: window=0x{x}\n", .{client.window});
1337 - } else if (!fullscreen and client.is_fullscreen) {
1338 - var no_atom: xlib.Atom = 0;
1339 - _ = xlib.XChangeProperty(
1340 - display.handle,
1341 - client.window,
1342 - net_wm_state,
1343 - xlib.XA_ATOM,
1344 - 32,
1345 - xlib.PropModeReplace,
1346 - @ptrCast(&no_atom),
1347 - 0,
1348 - );
1349 - client.is_fullscreen = false;
1350 - client.is_floating = client.old_state;
1351 - client.border_width = client.old_border_width;
1352 -
1353 - client.x = client.old_x;
1354 - client.y = client.old_y;
1355 - client.width = client.old_width;
1356 - client.height = client.old_height;
1357 -
1358 - tiling.resize_client(client, client.x, client.y, client.width, client.height);
1359 - arrange(monitor);
1360 -
1361 - std.debug.print("fullscreen disabled: window=0x{x}\n", .{client.window});
1362 - }
1363 -}
1364 -
1365 -fn view(display: *Display, tag_mask: u32) void {
1366 - const monitor = monitor_mod.selected_monitor orelse return;
1367 - if (tag_mask == monitor.tagset[monitor.sel_tags]) {
1368 - return;
1369 - }
1370 - monitor.sel_tags ^= 1;
1371 - if (tag_mask != 0) {
1372 - monitor.tagset[monitor.sel_tags] = tag_mask;
1373 - monitor.pertag.prevtag = monitor.pertag.curtag;
1374 -
1375 - if (tag_mask == ~@as(u32, 0)) {
1376 - monitor.pertag.curtag = 0;
1377 - } else {
1378 - var i: u32 = 0;
1379 - while (i < 9) : (i += 1) {
1380 - if ((tag_mask & (@as(u32, 1) << @intCast(i))) != 0) break;
1381 - }
1382 - monitor.pertag.curtag = i + 1;
1383 - }
1384 - } else {
1385 - const tmp = monitor.pertag.prevtag;
1386 - monitor.pertag.prevtag = monitor.pertag.curtag;
1387 - monitor.pertag.curtag = tmp;
1388 - }
1389 -
1390 - monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag];
1391 - monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag];
1392 - monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag];
1393 -
1394 - focus_top_client(display, monitor);
1395 - arrange(monitor);
1396 - bar_mod.invalidate_bars();
1397 - std.debug.print("view: tag_mask={d}\n", .{monitor.tagset[monitor.sel_tags]});
1398 -}
1399 -
1400 -fn view_adjacent_tag(display: *Display, direction: i32) void {
1401 - const monitor = monitor_mod.selected_monitor orelse return;
1402 - const current_tag = monitor.pertag.curtag;
1403 - var new_tag: i32 = @intCast(current_tag);
1404 -
1405 - new_tag += direction;
1406 - if (new_tag < 1) new_tag = 9;
1407 - if (new_tag > 9) new_tag = 1;
1408 -
1409 - const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
1410 - view(display, tag_mask);
1411 -}
1412 -
1413 -fn view_adjacent_nonempty_tag(display: *Display, direction: i32) void {
1414 - const monitor = monitor_mod.selected_monitor orelse return;
1415 - const current_tag = monitor.pertag.curtag;
1416 - var new_tag: i32 = @intCast(current_tag);
1417 -
1418 - var attempts: i32 = 0;
1419 - while (attempts < 9) : (attempts += 1) {
1420 - new_tag += direction;
1421 - if (new_tag < 1) new_tag = 9;
1422 - if (new_tag > 9) new_tag = 1;
1423 -
1424 - const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
1425 - if (has_clients_on_tag(monitor, tag_mask)) {
1426 - view(display, tag_mask);
1427 - return;
1428 - }
1429 - }
1430 -}
1431 -
1432 -fn has_clients_on_tag(monitor: *monitor_mod.Monitor, tag_mask: u32) bool {
1433 - var client = monitor.clients;
1434 - while (client) |c| {
1435 - if ((c.tags & tag_mask) != 0) {
1436 - return true;
1437 - }
1438 - client = c.next;
1439 - }
1440 - return false;
1441 -}
1442 -
1443 -fn tag_client(display: *Display, tag_mask: u32) void {
1444 - const monitor = monitor_mod.selected_monitor orelse return;
1445 - const client = monitor.sel orelse return;
1446 - if (tag_mask == 0) {
1447 - return;
1448 - }
1449 - client.tags = tag_mask;
1450 - focus_top_client(display, monitor);
1451 - arrange(monitor);
1452 - bar_mod.invalidate_bars();
1453 - std.debug.print("tag_client: window=0x{x} tag_mask={d}\n", .{ client.window, tag_mask });
1454 -}
1455 -
1456 -fn focus_top_client(display: *Display, monitor: *Monitor) void {
1457 - var visible_client = monitor.stack;
1458 - while (visible_client) |client| {
1459 - if (client_mod.is_visible(client)) {
1460 - focus(display, client);
1461 - return;
1462 - }
1463 - visible_client = client.stack_next;
1464 - }
1465 - monitor.sel = null;
1466 - _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
1467 -}
1468 -
1469 -fn focusstack(display: *Display, direction: i32) void {
1470 - const monitor = monitor_mod.selected_monitor orelse return;
1471 - const current = monitor.sel orelse return;
1472 -
1473 - var next_client: ?*Client = null;
1474 -
1475 - if (direction > 0) {
1476 - next_client = current.next;
1477 - while (next_client) |client| {
1478 - if (client_mod.is_visible(client)) {
1479 - break;
1480 - }
1481 - next_client = client.next;
1482 - }
1483 - if (next_client == null) {
1484 - next_client = monitor.clients;
1485 - while (next_client) |client| {
1486 - if (client_mod.is_visible(client)) {
1487 - break;
1488 - }
1489 - next_client = client.next;
1490 - }
1491 - }
1492 - } else {
1493 - var prev: ?*Client = null;
1494 - var iter = monitor.clients;
1495 - while (iter) |client| {
1496 - if (client == current) {
1497 - break;
1498 - }
1499 - if (client_mod.is_visible(client)) {
1500 - prev = client;
1501 - }
1502 - iter = client.next;
1503 - }
1504 - if (prev == null) {
1505 - iter = current.next;
1506 - while (iter) |client| {
1507 - if (client_mod.is_visible(client)) {
1508 - prev = client;
1509 - }
1510 - iter = client.next;
1511 - }
1512 - }
1513 - next_client = prev;
1514 - }
1515 -
1516 - if (next_client) |client| {
1517 - focus(display, client);
1518 - if (client.monitor) |client_monitor| {
1519 - restack(display, client_monitor);
1520 - }
1521 - }
1522 -}
1523 -
1524 -fn toggle_floating(_: *Display) void {
1525 - const monitor = monitor_mod.selected_monitor orelse return;
1526 - const client = monitor.sel orelse return;
1527 -
1528 - if (client.is_fullscreen) {
1529 - return;
1530 - }
1531 -
1532 - client.is_floating = !client.is_floating;
1533 -
1534 - if (client.is_floating) {
1535 - tiling.resize(client, client.x, client.y, client.width, client.height, false);
1536 - }
1537 -
1538 - arrange(monitor);
1539 - std.debug.print("toggle_floating: window=0x{x} floating={}\n", .{ client.window, client.is_floating });
1540 -}
1541 -
1542 -fn incnmaster(delta: i32) void {
1543 - const monitor = monitor_mod.selected_monitor orelse return;
1544 - const new_val = @max(0, monitor.nmaster + delta);
1545 - monitor.nmaster = new_val;
1546 - monitor.pertag.nmasters[monitor.pertag.curtag] = new_val;
1547 - arrange(monitor);
1548 - std.debug.print("incnmaster: nmaster={d}\n", .{monitor.nmaster});
1549 -}
1550 -
1551 -fn setmfact(delta: f32) void {
1552 - const monitor = monitor_mod.selected_monitor orelse return;
1553 - const new_mfact = monitor.mfact + delta;
1554 - if (new_mfact < 0.05 or new_mfact > 0.95) {
1555 - return;
1556 - }
1557 - monitor.mfact = new_mfact;
1558 - monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact;
1559 - arrange(monitor);
1560 - std.debug.print("setmfact: mfact={d:.2}\n", .{monitor.mfact});
1561 -}
1562 -
1563 -fn cycle_layout() void {
1564 - const monitor = monitor_mod.selected_monitor orelse return;
1565 - const new_lt = (monitor.sel_lt + 1) % 5;
1566 - monitor.sel_lt = new_lt;
1567 - monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
1568 - if (new_lt != 3) {
1569 - monitor.scroll_offset = 0;
1570 - }
1571 - arrange(monitor);
1572 - bar_mod.invalidate_bars();
1573 - if (monitor.lt[monitor.sel_lt]) |layout| {
1574 - std.debug.print("cycle_layout: {s}\n", .{layout.symbol});
1575 - }
1576 -}
1577 -
1578 -fn set_layout(layout_name: ?[]const u8) void {
1579 - const monitor = monitor_mod.selected_monitor orelse return;
1580 - const name = layout_name orelse return;
1581 -
1582 - const new_lt: u32 = if (std.mem.eql(u8, name, "tiling") or std.mem.eql(u8, name, "[]="))
1583 - 0
1584 - else if (std.mem.eql(u8, name, "monocle") or std.mem.eql(u8, name, "[M]"))
1585 - 1
1586 - else if (std.mem.eql(u8, name, "floating") or std.mem.eql(u8, name, "><>"))
1587 - 2
1588 - else if (std.mem.eql(u8, name, "scrolling") or std.mem.eql(u8, name, "[S]"))
1589 - 3
1590 - else if (std.mem.eql(u8, name, "grid") or std.mem.eql(u8, name, "[#]"))
1591 - 4
1592 - else {
1593 - std.debug.print("set_layout: unknown layout '{s}'\n", .{name});
1594 - return;
1595 - };
1596 -
1597 - monitor.sel_lt = new_lt;
1598 - monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
1599 - if (new_lt != 3) {
1600 - monitor.scroll_offset = 0;
1601 - }
1602 - arrange(monitor);
1603 - bar_mod.invalidate_bars();
1604 - if (monitor.lt[monitor.sel_lt]) |layout| {
1605 - std.debug.print("set_layout: {s}\n", .{layout.symbol});
1606 - }
1607 -}
1608 -
1609 -fn set_layout_index(index: u32) void {
1610 - const monitor = monitor_mod.selected_monitor orelse return;
1611 - monitor.sel_lt = index;
1612 - monitor.pertag.sellts[monitor.pertag.curtag] = index;
1613 - if (index != 3) {
1614 - monitor.scroll_offset = 0;
1615 - }
1616 - arrange(monitor);
1617 - bar_mod.invalidate_bars();
1618 - if (monitor.lt[monitor.sel_lt]) |layout| {
1619 - std.debug.print("set_layout_index: {s}\n", .{layout.symbol});
1620 - }
1621 -}
1622 -
1623 -fn focusmon(display: *Display, direction: i32) void {
1624 - const selmon = monitor_mod.selected_monitor orelse return;
1625 - const target = monitor_mod.dir_to_monitor(direction) orelse return;
1626 - if (target == selmon) {
1627 - return;
1628 - }
1629 - unfocus_client(display, selmon.sel, false);
1630 - monitor_mod.selected_monitor = target;
1631 - focus(display, null);
1632 - std.debug.print("focusmon: monitor {d}\n", .{target.num});
1633 -}
1634 -
1635 -fn sendmon(display: *Display, direction: i32) void {
1636 - const source_monitor = monitor_mod.selected_monitor orelse return;
1637 - const client = source_monitor.sel orelse return;
1638 - const target = monitor_mod.dir_to_monitor(direction) orelse return;
1639 -
1640 - if (target == source_monitor) {
1641 - return;
1642 - }
1643 -
1644 - client_mod.detach(client);
1645 - client_mod.detach_stack(client);
1646 - client.monitor = target;
1647 - client.tags = target.tagset[target.sel_tags];
1648 - client_mod.attach_aside(client);
1649 - client_mod.attach_stack(client);
1650 -
1651 - focus_top_client(display, source_monitor);
1652 - arrange(source_monitor);
1653 - arrange(target);
1654 -
1655 - std.debug.print("sendmon: window=0x{x} to monitor {d}\n", .{ client.window, target.num });
1656 -}
1657 -
1658 -fn snap_x(client: *Client, new_x: i32, monitor: *Monitor) i32 {
1659 - const client_width = client.width + 2 * client.border_width;
1660 - if (@abs(monitor.win_x - new_x) < snap_distance) {
1661 - return monitor.win_x;
1662 - } else if (@abs((monitor.win_x + monitor.win_w) - (new_x + client_width)) < snap_distance) {
1663 - return monitor.win_x + monitor.win_w - client_width;
1664 - }
1665 - return new_x;
1666 -}
1667 -
1668 -fn snap_y(client: *Client, new_y: i32, monitor: *Monitor) i32 {
1669 - const client_height = client.height + 2 * client.border_width;
1670 - if (@abs(monitor.win_y - new_y) < snap_distance) {
1671 - return monitor.win_y;
1672 - } else if (@abs((monitor.win_y + monitor.win_h) - (new_y + client_height)) < snap_distance) {
1673 - return monitor.win_y + monitor.win_h - client_height;
1674 - }
1675 - return new_y;
1676 -}
1677 -
1678 -fn movemouse(display: *Display) void {
1679 - const monitor = monitor_mod.selected_monitor orelse return;
1680 - const client = monitor.sel orelse return;
1681 -
1682 - if (client.is_fullscreen) {
1683 - return;
1684 - }
1685 -
1686 - restack(display, monitor);
1687 -
1688 - const was_floating = client.is_floating;
1689 - if (!client.is_floating) {
1690 - client.is_floating = true;
1691 - }
1692 -
1693 - var root_x: c_int = undefined;
1694 - var root_y: c_int = undefined;
1695 - var dummy_win: xlib.Window = undefined;
1696 - var dummy_int: c_int = undefined;
1697 - var dummy_uint: c_uint = undefined;
1698 -
1699 - _ = xlib.XQueryPointer(display.handle, display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint);
1700 -
1701 - const grab_result = xlib.XGrabPointer(
1702 - display.handle,
1703 - display.root,
1704 - xlib.False,
1705 - xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
1706 - xlib.GrabModeAsync,
1707 - xlib.GrabModeAsync,
1708 - xlib.None,
1709 - cursor_move,
1710 - xlib.CurrentTime,
1711 - );
1712 -
1713 - if (grab_result != xlib.GrabSuccess) {
1714 - return;
1715 - }
1716 -
1717 - const start_x = client.x;
1718 - const start_y = client.y;
1719 - const pointer_start_x = root_x;
1720 - const pointer_start_y = root_y;
1721 - var last_time: c_ulong = 0;
1722 -
1723 - var event: xlib.XEvent = undefined;
1724 - var done = false;
1725 -
1726 - while (!done) {
1727 - _ = xlib.XNextEvent(display.handle, &event);
1728 -
1729 - switch (event.type) {
1730 - xlib.MotionNotify => {
1731 - const motion = &event.xmotion;
1732 - if ((motion.time - last_time) < (1000 / 60)) {
1733 - continue;
1734 - }
1735 - last_time = motion.time;
1736 - const delta_x = motion.x_root - pointer_start_x;
1737 - const delta_y = motion.y_root - pointer_start_y;
1738 - var new_x = start_x + delta_x;
1739 - var new_y = start_y + delta_y;
1740 - if (client.monitor) |client_monitor| {
1741 - new_x = snap_x(client, new_x, client_monitor);
1742 - new_y = snap_y(client, new_y, client_monitor);
1743 - }
1744 - tiling.resize(client, new_x, new_y, client.width, client.height, true);
1745 - },
1746 - xlib.ButtonRelease => {
1747 - done = true;
1748 - },
1749 - else => {},
1750 - }
1751 - }
1752 -
1753 - _ = xlib.XUngrabPointer(display.handle, xlib.CurrentTime);
1754 -
1755 - const new_mon = monitor_mod.rect_to_monitor(client.x, client.y, client.width, client.height);
1756 - if (new_mon != null and new_mon != monitor) {
1757 - client_mod.detach(client);
1758 - client_mod.detach_stack(client);
1759 - client.monitor = new_mon;
1760 - client.tags = new_mon.?.tagset[new_mon.?.sel_tags];
1761 - client_mod.attach_aside(client);
1762 - client_mod.attach_stack(client);
1763 - monitor_mod.selected_monitor = new_mon;
1764 - focus(display, client);
1765 - arrange(monitor);
1766 - arrange(new_mon.?);
1767 - } else {
1768 - arrange(monitor);
1769 - }
1770 -
1771 - if (config.auto_tile and !was_floating) {
1772 - const drop_monitor = client.monitor orelse return;
1773 - const center_x = client.x + @divTrunc(client.width, 2);
1774 - const center_y = client.y + @divTrunc(client.height, 2);
1775 -
1776 - if (client_mod.tiled_window_at(client, drop_monitor, center_x, center_y)) |target| {
1777 - client_mod.insert_before(client, target);
1778 - }
1779 -
1780 - client.is_floating = false;
1781 - arrange(drop_monitor);
1782 - }
1783 -}
1784 -
1785 -fn resizemouse(display: *Display) void {
1786 - const monitor = monitor_mod.selected_monitor orelse return;
1787 - const client = monitor.sel orelse return;
1788 -
1789 - if (client.is_fullscreen) {
1790 - return;
1791 - }
1792 -
1793 - restack(display, monitor);
1794 -
1795 - if (!client.is_floating) {
1796 - client.is_floating = true;
1797 - }
1798 -
1799 - const grab_result = xlib.XGrabPointer(
1800 - display.handle,
1801 - display.root,
1802 - xlib.False,
1803 - xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
1804 - xlib.GrabModeAsync,
1805 - xlib.GrabModeAsync,
1806 - xlib.None,
1807 - cursor_resize,
1808 - xlib.CurrentTime,
1809 - );
1810 -
1811 - if (grab_result != xlib.GrabSuccess) {
1812 - return;
1813 - }
1814 -
1815 - _ = xlib.XWarpPointer(display.handle, xlib.None, client.window, 0, 0, 0, 0, client.width + client.border_width - 1, client.height + client.border_width - 1);
1816 -
1817 - var event: xlib.XEvent = undefined;
1818 - var done = false;
1819 - var last_time: c_ulong = 0;
1820 -
1821 - while (!done) {
1822 - _ = xlib.XNextEvent(display.handle, &event);
1823 -
1824 - switch (event.type) {
1825 - xlib.MotionNotify => {
1826 - const motion = &event.xmotion;
1827 - if ((motion.time - last_time) < (1000 / 60)) {
1828 - continue;
1829 - }
1830 - last_time = motion.time;
1831 - var new_width = @max(1, motion.x_root - client.x - 2 * client.border_width + 1);
1832 - var new_height = @max(1, motion.y_root - client.y - 2 * client.border_width + 1);
1833 - if (client.monitor) |client_monitor| {
1834 - const client_right = client.x + new_width + 2 * client.border_width;
1835 - const client_bottom = client.y + new_height + 2 * client.border_width;
1836 - const mon_right = client_monitor.win_x + client_monitor.win_w;
1837 - const mon_bottom = client_monitor.win_y + client_monitor.win_h;
1838 - if (@abs(mon_right - client_right) < snap_distance) {
1839 - new_width = @max(1, mon_right - client.x - 2 * client.border_width);
1840 - }
1841 - if (@abs(mon_bottom - client_bottom) < snap_distance) {
1842 - new_height = @max(1, mon_bottom - client.y - 2 * client.border_width);
1843 - }
1844 - }
1845 - tiling.resize(client, client.x, client.y, new_width, new_height, true);
1846 - },
1847 - xlib.ButtonRelease => {
1848 - done = true;
1849 - },
1850 - else => {},
1851 - }
1852 - }
1853 -
1854 - _ = xlib.XUngrabPointer(display.handle, xlib.CurrentTime);
1855 - arrange(monitor);
1856 -}
1857 -
1858 -fn handle_expose(display: *Display, event: *xlib.XExposeEvent) void {
1859 - if (event.count != 0) return;
1860 -
1861 - if (bar_mod.window_to_bar(event.window)) |bar| {
1862 - bar.invalidate();
1863 - bar.draw(display.handle, &tags);
1864 - }
1865 -}
1866 -
1867 -fn clean_mask(mask: c_uint) c_uint {
1868 - const lock: c_uint = @intCast(xlib.LockMask);
1869 - const shift: c_uint = @intCast(xlib.ShiftMask);
1870 - const ctrl: c_uint = @intCast(xlib.ControlMask);
1871 - const mod1: c_uint = @intCast(xlib.Mod1Mask);
1872 - const mod2: c_uint = @intCast(xlib.Mod2Mask);
1873 - const mod3: c_uint = @intCast(xlib.Mod3Mask);
1874 - const mod4: c_uint = @intCast(xlib.Mod4Mask);
1875 - const mod5: c_uint = @intCast(xlib.Mod5Mask);
1876 - return mask & ~(lock | numlock_mask) & (shift | ctrl | mod1 | mod2 | mod3 | mod4 | mod5);
1877 -}
1878 -
1879 -fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void {
1880 - std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow });
1881 -
1882 - const clicked_monitor = monitor_mod.window_to_monitor(event.window);
1883 - if (clicked_monitor) |monitor| {
1884 - if (monitor != monitor_mod.selected_monitor) {
1885 - if (monitor_mod.selected_monitor) |selmon| {
1886 - unfocus_client(display, selmon.sel, true);
1887 - }
1888 - monitor_mod.selected_monitor = monitor;
1889 - focus(display, null);
1890 - }
1891 - }
1892 -
1893 - if (bar_mod.window_to_bar(event.window)) |bar| {
1894 - const clicked_tag = bar.handle_click(event.x, &tags);
1895 - if (clicked_tag) |tag_index| {
1896 - const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index);
1897 - view(display, tag_mask);
1898 - }
1899 - return;
1900 - }
1901 -
1902 - const click_client = client_mod.window_to_client(event.window);
1903 - if (click_client) |found_client| {
1904 - focus(display, found_client);
1905 - if (monitor_mod.selected_monitor) |selmon| {
1906 - restack(display, selmon);
1907 - }
1908 - _ = xlib.XAllowEvents(display.handle, xlib.ReplayPointer, xlib.CurrentTime);
1909 - }
1910 -
1911 - const clean_state = clean_mask(event.state);
1912 - for (config.buttons.items) |button| {
1913 - if (button.click != .client_win) continue;
1914 - const button_clean_mask = clean_mask(button.mod_mask);
1915 - if (clean_state == button_clean_mask and event.button == button.button) {
1916 - switch (button.action) {
1917 - .move_mouse => movemouse(display),
1918 - .resize_mouse => resizemouse(display),
1919 - .toggle_floating => {
1920 - if (click_client) |found_client| {
1921 - found_client.is_floating = !found_client.is_floating;
1922 - if (monitor_mod.selected_monitor) |monitor| {
1923 - arrange(monitor);
1924 - }
1925 - }
1926 - },
1927 - }
1928 - return;
1929 - }
1930 - }
1931 -}
1932 -
1933 -fn handle_client_message(display: *Display, event: *xlib.XClientMessageEvent) void {
1934 - const client = client_mod.window_to_client(event.window) orelse return;
1935 -
1936 - if (event.message_type == net_wm_state) {
1937 - const action = event.data.l[0];
1938 - const first_property = @as(xlib.Atom, @intCast(event.data.l[1]));
1939 - const second_property = @as(xlib.Atom, @intCast(event.data.l[2]));
1940 -
1941 - if (first_property == net_wm_state_fullscreen or second_property == net_wm_state_fullscreen) {
1942 - const net_wm_state_remove = 0;
1943 - const net_wm_state_add = 1;
1944 - const net_wm_state_toggle = 2;
1945 -
1946 - if (action == net_wm_state_add) {
1947 - set_fullscreen(display, client, true);
1948 - } else if (action == net_wm_state_remove) {
1949 - set_fullscreen(display, client, false);
1950 - } else if (action == net_wm_state_toggle) {
1951 - set_fullscreen(display, client, !client.is_fullscreen);
1952 - }
1953 - }
1954 - } else if (event.message_type == net_active_window) {
1955 - const selected = monitor_mod.selected_monitor orelse return;
1956 - if (client != selected.sel and !client.is_urgent) {
1957 - set_urgent(display, client, true);
1958 - }
1959 - }
1960 -}
1961 -
1962 -fn handle_destroy_notify(display: *Display, event: *xlib.XDestroyWindowEvent) void {
1963 - const client = client_mod.window_to_client(event.window) orelse return;
1964 - std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
1965 - unmanage(display, client);
1966 -}
1967 -
1968 -fn handle_unmap_notify(display: *Display, event: *xlib.XUnmapEvent) void {
1969 - const client = client_mod.window_to_client(event.window) orelse return;
1970 - std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
1971 - unmanage(display, client);
1972 -}
1973 -
1974 -fn unmanage(display: *Display, client: *Client) void {
1975 - const client_monitor = client.monitor;
1976 -
1977 - var next_focus: ?*Client = null;
1978 - if (client_monitor) |monitor| {
1979 - if (monitor.sel == client and is_scrolling_layout(monitor)) {
1980 - next_focus = client.next;
1981 - if (next_focus == null) {
1982 - var prev: ?*Client = null;
1983 - var iter = monitor.clients;
1984 - while (iter) |c| {
1985 - if (c == client) break;
1986 - prev = c;
1987 - iter = c.next;
1988 - }
1989 - next_focus = prev;
1990 - }
1991 - }
1992 - }
1993 -
1994 - client_mod.detach(client);
1995 - client_mod.detach_stack(client);
1996 -
1997 - if (client_monitor) |monitor| {
1998 - if (monitor.sel == client) {
1999 - monitor.sel = if (next_focus) |nf| nf else monitor.stack;
2000 - }
2001 - if (is_scrolling_layout(monitor)) {
2002 - const target = if (monitor.sel) |sel| scrolling.get_target_scroll_for_window(monitor, sel) else 0;
2003 - if (target == 0) {
2004 - monitor.scroll_offset = scrolling.get_scroll_step(monitor);
2005 - } else {
2006 - monitor.scroll_offset = 0;
2007 - }
2008 - }
2009 - arrange(monitor);
2010 - }
2011 -
2012 - if (client_monitor) |monitor| {
2013 - if (monitor.sel) |selected| {
2014 - focus(display, selected);
2015 - }
2016 - }
2017 -
2018 - client_mod.destroy(client);
2019 - update_client_list(display);
2020 - bar_mod.invalidate_bars();
2021 -}
2022 -
2023 -fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent) void {
2024 - if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != display.root) {
2025 - return;
2026 - }
2027 -
2028 - const client = client_mod.window_to_client(event.window);
2029 - const target_mon = if (client) |c| c.monitor else monitor_mod.window_to_monitor(event.window);
2030 - const selmon = monitor_mod.selected_monitor;
2031 -
2032 - if (target_mon != selmon) {
2033 - if (selmon) |sel| {
2034 - unfocus_client(display, sel.sel, true);
2035 - }
2036 - monitor_mod.selected_monitor = target_mon;
2037 - } else if (client == null) {
2038 - return;
2039 - } else if (selmon) |sel| {
2040 - if (client.? == sel.sel) {
2041 - return;
2042 - }
2043 - }
2044 -
2045 - focus(display, client);
2046 -}
2047 -
2048 -fn handle_focus_in(display: *Display, event: *xlib.XFocusChangeEvent) void {
2049 - const selmon = monitor_mod.selected_monitor orelse return;
2050 - const selected = selmon.sel orelse return;
2051 - if (event.window != selected.window) {
2052 - set_focus(display, selected);
2053 - }
2054 -}
2055 -
2056 -fn set_focus(display: *Display, client: *Client) void {
2057 - if (!client.never_focus) {
2058 - _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime);
2059 - _ = xlib.XChangeProperty(display.handle, display.root, net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
2060 - }
2061 - _ = send_event(display, client, wm_take_focus);
2062 -}
2063 -
2064 -var last_motion_monitor: ?*Monitor = null;
2065 -
2066 -fn handle_motion_notify(display: *Display, event: *xlib.XMotionEvent) void {
2067 - if (event.window != display.root) {
2068 - return;
2069 - }
2070 -
2071 - const target_mon = monitor_mod.rect_to_monitor(event.x_root, event.y_root, 1, 1);
2072 - if (target_mon != last_motion_monitor and last_motion_monitor != null) {
2073 - if (monitor_mod.selected_monitor) |selmon| {
2074 - unfocus_client(display, selmon.sel, true);
2075 - }
2076 - monitor_mod.selected_monitor = target_mon;
2077 - focus(display, null);
2078 - }
2079 - last_motion_monitor = target_mon;
2080 -}
2081 -
2082 -fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void {
2083 - if (event.state == xlib.PropertyDelete) {
2084 - return;
2085 - }
2086 -
2087 - const client = client_mod.window_to_client(event.window) orelse return;
2088 -
2089 - if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
2090 - var trans: xlib.Window = 0;
2091 - if (!client.is_floating and xlib.XGetTransientForHint(display.handle, client.window, &trans) != 0) {
2092 - client.is_floating = client_mod.window_to_client(trans) != null;
2093 - if (client.is_floating) {
2094 - if (client.monitor) |monitor| {
2095 - arrange(monitor);
2096 - }
2097 - }
2098 - }
2099 - } else if (event.atom == xlib.XA_WM_NORMAL_HINTS) {
2100 - client.hints_valid = false;
2101 - } else if (event.atom == xlib.XA_WM_HINTS) {
2102 - update_wm_hints(display, client);
2103 - bar_mod.invalidate_bars();
2104 - } else if (event.atom == xlib.XA_WM_NAME or event.atom == net_wm_name) {
2105 - update_title(display, client);
2106 - } else if (event.atom == net_wm_window_type) {
2107 - update_window_type(display, client);
2108 - }
2109 -}
2110 -
2111 -fn unfocus_client(display: *Display, client: ?*Client, reset_input_focus: bool) void {
2112 - const unfocus_target = client orelse return;
2113 - grabbuttons(display, unfocus_target, false);
2114 - _ = xlib.XSetWindowBorder(display.handle, unfocus_target.window, border_color_unfocused);
2115 - if (reset_input_focus) {
2116 - _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
2117 - _ = xlib.XDeleteProperty(display.handle, display.root, net_active_window);
2118 - }
2119 -}
2120 -
2121 -fn set_client_state(display: *Display, client: *Client, state: c_long) void {
2122 - var data: [2]c_long = .{ state, xlib.None };
2123 - _ = xlib.c.XChangeProperty(display.handle, client.window, wm_state, wm_state, 32, xlib.PropModeReplace, @ptrCast(&data), 2);
2124 -}
2125 -
2126 -fn update_numlock_mask(display: *Display) void {
2127 - numlock_mask = 0;
2128 - const modmap = xlib.XGetModifierMapping(display.handle);
2129 - if (modmap == null) return;
2130 - defer _ = xlib.XFreeModifiermap(modmap);
2131 -
2132 - const numlock_keycode = xlib.XKeysymToKeycode(display.handle, xlib.XK_Num_Lock);
2133 -
2134 - var modifier_index: usize = 0;
2135 - while (modifier_index < 8) : (modifier_index += 1) {
2136 - var key_index: usize = 0;
2137 - while (key_index < @as(usize, @intCast(modmap.*.max_keypermod))) : (key_index += 1) {
2138 - const keycode = modmap.*.modifiermap[modifier_index * @as(usize, @intCast(modmap.*.max_keypermod)) + key_index];
2139 - if (keycode == numlock_keycode) {
2140 - numlock_mask = @as(c_uint, 1) << @intCast(modifier_index);
2141 - }
2142 - }
2143 - }
2144 -}
2145 -
2146 -fn grabbuttons(display: *Display, client: *Client, focused: bool) void {
2147 - update_numlock_mask(display);
2148 - const modifiers = [_]c_uint{ 0, xlib.LockMask, numlock_mask, numlock_mask | xlib.LockMask };
2149 -
2150 - _ = xlib.XUngrabButton(display.handle, xlib.AnyButton, xlib.AnyModifier, client.window);
2151 - if (!focused) {
2152 - _ = xlib.XGrabButton(
2153 - display.handle,
2154 - xlib.AnyButton,
2155 - xlib.AnyModifier,
2156 - client.window,
2157 - xlib.False,
2158 - xlib.ButtonPressMask | xlib.ButtonReleaseMask,
2159 - xlib.GrabModeSync,
2160 - xlib.GrabModeSync,
2161 - xlib.None,
2162 - xlib.None,
2163 - );
2164 - }
2165 - for (config.buttons.items) |button| {
2166 - if (button.click == .client_win) {
2167 - for (modifiers) |modifier| {
2168 - _ = xlib.XGrabButton(
2169 - display.handle,
2170 - @intCast(button.button),
2171 - button.mod_mask | modifier,
2172 - client.window,
2173 - xlib.False,
2174 - xlib.ButtonPressMask | xlib.ButtonReleaseMask,
2175 - xlib.GrabModeAsync,
2176 - xlib.GrabModeSync,
2177 - xlib.None,
2178 - xlib.None,
2179 - );
2180 - }
2181 - }
2182 - }
2183 -}
2184 -
2185 -fn focus(display: *Display, target_client: ?*Client) void {
2186 - const selmon = monitor_mod.selected_monitor orelse return;
2187 -
2188 - var focus_client = target_client;
2189 - if (focus_client == null or !client_mod.is_visible(focus_client.?)) {
2190 - focus_client = selmon.stack;
2191 - while (focus_client) |iter| {
2192 - if (client_mod.is_visible(iter)) break;
2193 - focus_client = iter.stack_next;
2194 - }
2195 - }
2196 -
2197 - if (selmon.sel != null and selmon.sel != focus_client) {
2198 - unfocus_client(display, selmon.sel, false);
2199 - }
2200 -
2201 - if (focus_client) |client| {
2202 - if (client.monitor != selmon) {
2203 - monitor_mod.selected_monitor = client.monitor;
2204 - }
2205 - if (client.is_urgent) {
2206 - set_urgent(display, client, false);
2207 - }
2208 - client_mod.detach_stack(client);
2209 - client_mod.attach_stack(client);
2210 - grabbuttons(display, client, true);
2211 - _ = xlib.XSetWindowBorder(display.handle, client.window, border_color_focused);
2212 - if (!client.never_focus) {
2213 - _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime);
2214 - _ = xlib.XChangeProperty(display.handle, display.root, net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
2215 - }
2216 - _ = send_event(display, client, wm_take_focus);
2217 - } else {
2218 - _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
2219 - _ = xlib.XDeleteProperty(display.handle, display.root, net_active_window);
2220 - }
2221 -
2222 - const current_selmon = monitor_mod.selected_monitor orelse return;
2223 - current_selmon.sel = focus_client;
2224 -
2225 - if (focus_client) |client| {
2226 - if (is_scrolling_layout(current_selmon)) {
2227 - scroll_to_window(client, true);
2228 - }
2229 - }
2230 -
2231 - bar_mod.invalidate_bars();
2232 -}
2233 -
2234 -fn restack(display: *Display, monitor: *Monitor) void {
2235 - bar_mod.invalidate_bars();
2236 - const selected_client = monitor.sel orelse return;
2237 -
2238 - if (selected_client.is_floating or monitor.lt[monitor.sel_lt] == null) {
2239 - _ = xlib.XRaiseWindow(display.handle, selected_client.window);
2240 - }
2241 -
2242 - if (monitor.lt[monitor.sel_lt] != null) {
2243 - var window_changes: xlib.c.XWindowChanges = undefined;
2244 - window_changes.stack_mode = xlib.c.Below;
2245 - window_changes.sibling = monitor.bar_win;
2246 -
2247 - var current = monitor.stack;
2248 - while (current) |client| {
2249 - if (!client.is_floating and client_mod.is_visible(client)) {
2250 - _ = xlib.c.XConfigureWindow(display.handle, client.window, xlib.c.CWSibling | xlib.c.CWStackMode, &window_changes);
2251 - window_changes.sibling = client.window;
2252 - }
2253 - current = client.stack_next;
2254 - }
2255 - }
2256 -
2257 - _ = xlib.XSync(display.handle, xlib.False);
2258 -
2259 - var discard_event: xlib.XEvent = undefined;
2260 - while (xlib.c.XCheckMaskEvent(display.handle, xlib.EnterWindowMask, &discard_event) != 0) {}
2261 -}
2262 -
2263 -fn arrange(monitor: *Monitor) void {
2264 - if (display_global) |display| {
2265 - showhide(display, monitor);
2266 - }
2267 - if (monitor.lt[monitor.sel_lt]) |layout| {
2268 - if (layout.arrange_fn) |arrange_fn| {
2269 - arrange_fn(monitor);
2270 - }
2271 - }
2272 - if (display_global) |display| {
2273 - restack(display, monitor);
2274 - }
2275 -}
2276 -
2277 -fn tick_animations() void {
2278 - if (!scroll_animation.is_active()) return;
2279 -
2280 - const monitor = monitor_mod.selected_monitor orelse return;
2281 - if (scroll_animation.update()) |new_offset| {
2282 - monitor.scroll_offset = new_offset;
2283 - arrange(monitor);
2284 - }
2285 -}
2286 -
2287 -fn is_scrolling_layout(monitor: *Monitor) bool {
2288 - if (monitor.lt[monitor.sel_lt]) |layout| {
2289 - return layout.arrange_fn == scrolling.layout.arrange_fn;
2290 - }
2291 - return false;
2292 -}
2293 -
2294 -fn scroll_layout(direction: i32) void {
2295 - const monitor = monitor_mod.selected_monitor orelse return;
2296 - if (!is_scrolling_layout(monitor)) return;
2297 -
2298 - const scroll_step = scrolling.get_scroll_step(monitor);
2299 - const max_scroll = scrolling.get_max_scroll(monitor);
2300 -
2301 - const current = if (scroll_animation.is_active())
2302 - scroll_animation.target()
2303 - else
2304 - monitor.scroll_offset;
2305 -
2306 - var target = current + direction * scroll_step;
2307 - target = @max(0, @min(target, max_scroll));
2308 -
2309 - scroll_animation.start(monitor.scroll_offset, target, animation_config);
2310 -}
2311 -
2312 -fn scroll_to_window(client: *Client, animate: bool) void {
2313 - const monitor = client.monitor orelse return;
2314 - if (!is_scrolling_layout(monitor)) return;
2315 -
2316 - const target = scrolling.get_target_scroll_for_window(monitor, client);
2317 -
2318 - if (animate) {
2319 - scroll_animation.start(monitor.scroll_offset, target, animation_config);
2320 - } else {
2321 - monitor.scroll_offset = target;
2322 - arrange(monitor);
2323 - }
2324 -}
2325 -
2326 -fn showhide_client(display: *Display, client: ?*Client) void {
2327 - const target = client orelse return;
2328 - if (client_mod.is_visible(target)) {
2329 - _ = xlib.XMoveWindow(display.handle, target.window, target.x, target.y);
2330 - const monitor = target.monitor orelse return;
2331 - if ((monitor.lt[monitor.sel_lt] == null or target.is_floating) and !target.is_fullscreen) {
2332 - tiling.resize(target, target.x, target.y, target.width, target.height, false);
2333 - }
2334 - showhide_client(display, target.stack_next);
2335 - } else {
2336 - showhide_client(display, target.stack_next);
2337 - const client_width = target.width + 2 * target.border_width;
2338 - _ = xlib.XMoveWindow(display.handle, target.window, -2 * client_width, target.y);
2339 - }
2340 -}
2341 -
2342 -fn showhide(display: *Display, monitor: *Monitor) void {
2343 - showhide_client(display, monitor.stack);
2344 -}
2345 -
2346 -fn update_size_hints(display: *Display, client: *Client) void {
2347 - var size_hints: xlib.XSizeHints = undefined;
2348 - var msize: c_long = 0;
2349 -
2350 - if (xlib.XGetWMNormalHints(display.handle, client.window, &size_hints, &msize) == 0) {
2351 - size_hints.flags = xlib.PSize;
2352 - }
2353 -
2354 - if ((size_hints.flags & xlib.PBaseSize) != 0) {
2355 - client.base_width = size_hints.base_width;
2356 - client.base_height = size_hints.base_height;
2357 - } else if ((size_hints.flags & xlib.PMinSize) != 0) {
2358 - client.base_width = size_hints.min_width;
2359 - client.base_height = size_hints.min_height;
2360 - } else {
2361 - client.base_width = 0;
2362 - client.base_height = 0;
2363 - }
2364 -
2365 - if ((size_hints.flags & xlib.PResizeInc) != 0) {
2366 - client.increment_width = size_hints.width_inc;
2367 - client.increment_height = size_hints.height_inc;
2368 - } else {
2369 - client.increment_width = 0;
2370 - client.increment_height = 0;
2371 - }
2372 -
2373 - if ((size_hints.flags & xlib.PMaxSize) != 0) {
2374 - client.max_width = size_hints.max_width;
2375 - client.max_height = size_hints.max_height;
2376 - } else {
2377 - client.max_width = 0;
2378 - client.max_height = 0;
2379 - }
2380 -
2381 - if ((size_hints.flags & xlib.PMinSize) != 0) {
2382 - client.min_width = size_hints.min_width;
2383 - client.min_height = size_hints.min_height;
2384 - } else if ((size_hints.flags & xlib.PBaseSize) != 0) {
2385 - client.min_width = size_hints.base_width;
2386 - client.min_height = size_hints.base_height;
2387 - } else {
2388 - client.min_width = 0;
2389 - client.min_height = 0;
2390 - }
2391 -
2392 - if ((size_hints.flags & xlib.PAspect) != 0) {
2393 - client.min_aspect = @as(f32, @floatFromInt(size_hints.min_aspect.y)) / @as(f32, @floatFromInt(size_hints.min_aspect.x));
2394 - client.max_aspect = @as(f32, @floatFromInt(size_hints.max_aspect.x)) / @as(f32, @floatFromInt(size_hints.max_aspect.y));
2395 - } else {
2396 - client.min_aspect = 0.0;
2397 - client.max_aspect = 0.0;
2398 - }
2399 -
2400 - 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);
2401 - client.hints_valid = true;
2402 -}
2403 -
2404 -fn update_wm_hints(display: *Display, client: *Client) void {
2405 - const wmh = xlib.XGetWMHints(display.handle, client.window);
2406 - if (wmh) |hints| {
2407 - defer _ = xlib.XFree(@ptrCast(hints));
2408 -
2409 - if (client == (monitor_mod.selected_monitor orelse return).sel and (hints.*.flags & xlib.XUrgencyHint) != 0) {
2410 - hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint);
2411 - _ = xlib.XSetWMHints(display.handle, client.window, hints);
2412 - } else {
2413 - client.is_urgent = (hints.*.flags & xlib.XUrgencyHint) != 0;
2414 - }
2415 -
2416 - if ((hints.*.flags & xlib.InputHint) != 0) {
2417 - client.never_focus = hints.*.input == 0;
2418 - } else {
2419 - client.never_focus = false;
2420 - }
2421 - }
2422 -}
2423 -
2424 -fn update_window_type(display: *Display, client: *Client) void {
2425 - const state = get_atom_prop(display, client, net_wm_state);
2426 - const window_type = get_atom_prop(display, client, net_wm_window_type);
2427 -
2428 - if (state == net_wm_state_fullscreen) {
2429 - set_fullscreen(display, client, true);
2430 - }
2431 - if (window_type == net_wm_window_type_dialog) {
2432 - client.is_floating = true;
2433 - }
2434 -}
2435 -
2436 -fn update_title(display: *Display, client: *Client) void {
2437 - if (!get_text_prop(display, client.window, net_wm_name, &client.name)) {
2438 - _ = get_text_prop(display, client.window, xlib.XA_WM_NAME, &client.name);
2439 - }
2440 - if (client.name[0] == 0) {
2441 - @memcpy(client.name[0..6], "broken");
2442 - }
2443 -}
2444 -
2445 -fn get_atom_prop(display: *Display, client: *Client, prop: xlib.Atom) xlib.Atom {
2446 - var actual_type: xlib.Atom = undefined;
2447 - var actual_format: c_int = undefined;
2448 - var num_items: c_ulong = undefined;
2449 - var bytes_after: c_ulong = undefined;
2450 - var prop_data: [*c]u8 = undefined;
2451 -
2452 - if (xlib.XGetWindowProperty(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) {
2453 - const atom: xlib.Atom = @as(*xlib.Atom, @ptrCast(@alignCast(prop_data))).*;
2454 - _ = xlib.XFree(@ptrCast(prop_data));
2455 - return atom;
2456 - }
2457 - return 0;
2458 -}
2459 -
2460 -fn get_text_prop(display: *Display, window: xlib.Window, atom: xlib.Atom, text: *[256]u8) bool {
2461 - var name: xlib.XTextProperty = undefined;
2462 - text[0] = 0;
2463 -
2464 - if (xlib.XGetTextProperty(display.handle, window, &name, atom) == 0 or name.nitems == 0) {
2465 - return false;
2466 - }
2467 -
2468 - if (name.encoding == xlib.XA_STRING) {
2469 - const len = @min(name.nitems, 255);
2470 - @memcpy(text[0..len], name.value[0..len]);
2471 - text[len] = 0;
2472 - } else {
2473 - var list: [*c][*c]u8 = undefined;
2474 - var count: c_int = undefined;
2475 - if (xlib.XmbTextPropertyToTextList(display.handle, &name, &list, &count) >= xlib.Success and count > 0 and list[0] != null) {
2476 - const str = std.mem.sliceTo(list[0], 0);
2477 - const copy_len = @min(str.len, 255);
2478 - @memcpy(text[0..copy_len], str[0..copy_len]);
2479 - text[copy_len] = 0;
2480 - xlib.XFreeStringList(list);
2481 - }
2482 - }
2483 - text[255] = 0;
2484 - _ = xlib.XFree(@ptrCast(name.value));
2485 - return true;
2486 -}
2487 -
2488 -fn apply_rules(display: *Display, client: *Client) void {
2489 - var class_hint: xlib.XClassHint = .{ .res_name = null, .res_class = null };
2490 - _ = xlib.XGetClassHint(display.handle, client.window, &class_hint);
2491 -
2492 - const class_str: []const u8 = if (class_hint.res_class) |ptr| std.mem.sliceTo(ptr, 0) else "";
2493 - const instance_str: []const u8 = if (class_hint.res_name) |ptr| std.mem.sliceTo(ptr, 0) else "";
2494 -
2495 - client.is_floating = false;
2496 - client.tags = 0;
2497 - var rule_focus = false;
2498 -
2499 - for (config.rules.items) |rule| {
2500 - const class_matches = if (rule.class) |rc| std.mem.indexOf(u8, class_str, rc) != null else true;
2501 - const instance_matches = if (rule.instance) |ri| std.mem.indexOf(u8, instance_str, ri) != null else true;
2502 - const title_matches = if (rule.title) |rt| std.mem.indexOf(u8, std.mem.sliceTo(&client.name, 0), rt) != null else true;
2503 -
2504 - if (class_matches and instance_matches and title_matches) {
2505 - client.is_floating = rule.is_floating;
2506 - client.tags |= rule.tags;
2507 - if (rule.monitor >= 0) {
2508 - var target = monitor_mod.monitors;
2509 - var index: i32 = 0;
2510 - while (target) |mon| {
2511 - if (index == rule.monitor) {
2512 - client.monitor = mon;
2513 - break;
2514 - }
2515 - index += 1;
2516 - target = mon.next;
2517 - }
2518 - }
2519 - if (rule.focus) {
2520 - rule_focus = true;
2521 - }
2522 - }
2523 - }
2524 -
2525 - if (class_hint.res_class) |ptr| {
2526 - _ = xlib.XFree(@ptrCast(ptr));
2527 - }
2528 - if (class_hint.res_name) |ptr| {
2529 - _ = xlib.XFree(@ptrCast(ptr));
2530 - }
2531 -
2532 - const monitor = client.monitor orelse return;
2533 - if (client.tags == 0) {
2534 - client.tags = monitor.tagset[monitor.sel_tags];
2535 - }
2536 -
2537 - if (rule_focus and client.tags != 0) {
2538 - const monitor_tagset = monitor.tagset[monitor.sel_tags];
2539 - const is_tag_focused = (monitor_tagset & client.tags) == client.tags;
2540 - if (!is_tag_focused) {
2541 - view(display, client.tags);
2542 - }
2543 - }
2544 -}
2545 -
2546 -fn update_client_list(display: *Display) void {
2547 - _ = xlib.XDeleteProperty(display.handle, display.root, net_client_list);
2548 -
2549 - var current_monitor = monitor_mod.monitors;
2550 - while (current_monitor) |monitor| {
2551 - var current_client = monitor.clients;
2552 - while (current_client) |client| {
2553 - _ = xlib.XChangeProperty(display.handle, display.root, net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
2554 - current_client = client.next;
2555 - }
2556 - current_monitor = monitor.next;
2557 - }
2558 -}
2559 -
2560 -fn send_event(display: *Display, client: *Client, protocol: xlib.Atom) bool {
2561 - var protocols: [*c]xlib.Atom = undefined;
2562 - var num_protocols: c_int = 0;
2563 - var exists = false;
2564 -
2565 - if (xlib.XGetWMProtocols(display.handle, client.window, &protocols, &num_protocols) != 0) {
2566 - var index: usize = 0;
2567 - while (index < @as(usize, @intCast(num_protocols))) : (index += 1) {
2568 - if (protocols[index] == protocol) {
2569 - exists = true;
2570 - break;
2571 - }
2572 - }
2573 - _ = xlib.XFree(@ptrCast(protocols));
2574 - }
2575 -
2576 - if (exists) {
2577 - var event: xlib.XEvent = undefined;
2578 - event.type = xlib.ClientMessage;
2579 - event.xclient.window = client.window;
2580 - event.xclient.message_type = wm_protocols;
2581 - event.xclient.format = 32;
2582 - event.xclient.data.l[0] = @intCast(protocol);
2583 - event.xclient.data.l[1] = xlib.CurrentTime;
2584 - _ = xlib.XSendEvent(display.handle, client.window, xlib.False, xlib.NoEventMask, &event);
2585 - }
2586 - return exists;
2587 -}
2588 -
2589 -fn set_urgent(display: *Display, client: *Client, urgent: bool) void {
2590 - client.is_urgent = urgent;
2591 - const wmh = xlib.XGetWMHints(display.handle, client.window);
2592 - if (wmh) |hints| {
2593 - if (urgent) {
2594 - hints.*.flags = hints.*.flags | xlib.XUrgencyHint;
2595 - } else {
2596 - hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint);
2597 - }
2598 - _ = xlib.XSetWMHints(display.handle, client.window, hints);
2599 - _ = xlib.XFree(@ptrCast(hints));
2600 - }
2601 -}
2602 -
2603 -fn run_autostart_commands(_: std.mem.Allocator, commands: []const []const u8) !void {
2604 - for (commands) |cmd| spawn_command(cmd);
189 +fn runAutostartCommands(wm: *WindowManager) !void {
190 + const commands = wm.config.autostart.items;
191 + for (commands) |cmd| actions.spawnCommand(wm, cmd);
2605 192 }
2606 193
2607 194 test {
+36 −30 src/monitor.zig
@@ -1,6 +1,8 @@
1 1 const std = @import("std");
2 2 const xlib = @import("x11/xlib.zig");
3 +
3 4 const Client = @import("client.zig").Client;
5 +const WindowManager = @import("wm/wm.zig").WindowManager;
4 6
5 7 pub const Layout = struct {
6 8 symbol: []const u8,
@@ -46,58 +48,46 @@ pub const Monitor = struct {
46 48 stack: ?*Client = null,
47 49 next: ?*Monitor = null,
48 50 bar_win: xlib.Window = 0,
49 - lt: [5]?*const Layout = .{ null, null, null, null, null },
50 - pertag: Pertag = Pertag{},
51 + lt: [5]?*const Layout = .{null} ** 5,
52 + pertag: Pertag = .{},
51 53 };
52 54
53 -pub var monitors: ?*Monitor = null;
54 -pub var selected_monitor: ?*Monitor = null;
55 -
56 -var allocator: std.mem.Allocator = undefined;
57 -
58 -pub fn init(alloc: std.mem.Allocator) void {
59 - allocator = alloc;
60 -}
61 -
62 -pub fn create() ?*Monitor {
55 +/// Allocates and zero-initialises a new `Monitor`.
56 +pub fn create(allocator: std.mem.Allocator) ?*Monitor {
63 57 const mon = allocator.create(Monitor) catch return null;
64 58 mon.* = Monitor{};
65 59 return mon;
66 60 }
67 61
68 -pub fn destroy(mon: *Monitor) void {
62 +/// Frees a monitor previously returned by `create`.
63 +pub fn destroy(allocator: std.mem.Allocator, mon: *Monitor) void {
69 64 allocator.destroy(mon);
70 65 }
71 66
72 -var root_window: xlib.Window = 0;
73 -var display_handle: ?*xlib.Display = null;
74 -
75 -pub fn set_root_window(root: xlib.Window, display: *xlib.Display) void {
76 - root_window = root;
77 - display_handle = display;
78 -}
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;
79 72
80 -pub fn window_to_monitor(win: xlib.Window) ?*Monitor {
81 - if (win == root_window and display_handle != null) {
73 + if (win == wm.display.root) {
82 74 var root_x: c_int = undefined;
83 75 var root_y: c_int = undefined;
84 76 var dummy_win: xlib.Window = undefined;
85 77 var dummy_int: c_int = undefined;
86 78 var dummy_uint: c_uint = undefined;
87 - if (xlib.XQueryPointer(display_handle.?, root_window, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint) != 0) {
88 - return rect_to_monitor(root_x, root_y, 1, 1);
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);
89 81 }
90 82 }
91 83
92 84 var current = monitors;
93 85 while (current) |monitor| {
94 - if (monitor.bar_win == win) {
95 - return monitor;
96 - }
86 + if (monitor.bar_win == win) return monitor;
97 87 current = monitor.next;
98 88 }
99 89
100 - const client = @import("client.zig").window_to_client(win);
90 + const client = @import("client.zig").windowToClient(monitors, win);
101 91 if (client) |found_client| {
102 92 return found_client.monitor;
103 93 }
@@ -105,7 +95,11 @@ pub fn window_to_monitor(win: xlib.Window) ?*Monitor {
105 95 return selected_monitor;
106 96 }
107 97
108 -pub fn rect_to_monitor(x: i32, y: i32, width: i32, height: i32) ?*Monitor {
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;
109 103 var result = selected_monitor;
110 104 var max_area: i32 = 0;
111 105
@@ -123,7 +117,17 @@ pub fn rect_to_monitor(x: i32, y: i32, width: i32, height: i32) ?*Monitor {
123 117 return result;
124 118 }
125 119
126 -pub fn dir_to_monitor(direction: i32) ?*Monitor {
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;
127 131 var target: ?*Monitor = null;
128 132
129 133 if (direction > 0) {
@@ -132,6 +136,7 @@ pub fn dir_to_monitor(direction: i32) ?*Monitor {
132 136 target = monitors;
133 137 }
134 138 } else if (selected_monitor == monitors) {
139 + // Already at head, walk to tail.
135 140 var last = monitors;
136 141 while (last) |iter| {
137 142 if (iter.next == null) {
@@ -141,6 +146,7 @@ pub fn dir_to_monitor(direction: i32) ?*Monitor {
141 146 last = iter.next;
142 147 }
143 148 } else {
149 + // Walk until we find the node just before selected_monitor.
144 150 var previous = monitors;
145 151 while (previous) |iter| {
146 152 if (iter.next == selected_monitor) {
+35 −36 src/overlay.zig
@@ -13,7 +13,7 @@ const key_bg_color: c_ulong = 0x2a2a2a;
13 13
14 14 const max_lines: usize = 12;
15 15
16 -pub const Keybind_Overlay = struct {
16 +pub const KeybindOverlay = struct {
17 17 window: xlib.Window = 0,
18 18 pixmap: xlib.Pixmap = 0,
19 19 gc: xlib.GC = null,
@@ -32,8 +32,8 @@ pub const Keybind_Overlay = struct {
32 32 descs: [max_lines][]const u8 = undefined,
33 33 line_count: usize = 0,
34 34
35 - pub fn init(display: *xlib.Display, screen: c_int, root: xlib.Window, font_name: []const u8, allocator: std.mem.Allocator) ?*Keybind_Overlay {
36 - const overlay = allocator.create(Keybind_Overlay) catch return null;
35 + pub fn init(display: *xlib.Display, screen: c_int, root: xlib.Window, font_name: []const u8, allocator: std.mem.Allocator) ?*KeybindOverlay {
36 + const overlay = allocator.create(KeybindOverlay) catch return null;
37 37
38 38 const font_name_z = allocator.dupeZ(u8, font_name) catch {
39 39 allocator.destroy(overlay);
@@ -60,9 +60,9 @@ pub const Keybind_Overlay = struct {
60 60 return overlay;
61 61 }
62 62
63 - pub fn deinit(self: *Keybind_Overlay, allocator: std.mem.Allocator) void {
63 + pub fn deinit(self: *KeybindOverlay, allocator: std.mem.Allocator) void {
64 64 if (self.display) |display| {
65 - self.destroy_window(display);
65 + self.destroyWindow(display);
66 66 if (self.font) |font| {
67 67 xlib.XftFontClose(display, font);
68 68 }
@@ -70,7 +70,7 @@ pub const Keybind_Overlay = struct {
70 70 allocator.destroy(self);
71 71 }
72 72
73 - fn destroy_window(self: *Keybind_Overlay, display: *xlib.Display) void {
73 + fn destroyWindow(self: *KeybindOverlay, display: *xlib.Display) void {
74 74 if (self.xft_draw) |xft_draw| {
75 75 xlib.XftDrawDestroy(xft_draw);
76 76 self.xft_draw = null;
@@ -89,19 +89,18 @@ pub const Keybind_Overlay = struct {
89 89 }
90 90 }
91 91
92 - pub fn toggle(self: *Keybind_Overlay, mon_x: i32, mon_y: i32, mon_w: i32, mon_h: i32) void {
92 + pub fn toggle(self: *KeybindOverlay, mon_x: i32, mon_y: i32, mon_w: i32, mon_h: i32, config: *config_mod.Config) void {
93 93 if (self.visible) {
94 94 self.hide();
95 95 } else {
96 - self.show(mon_x, mon_y, mon_w, mon_h);
96 + self.show(mon_x, mon_y, mon_w, mon_h, config);
97 97 }
98 98 }
99 99
100 - pub fn show(self: *Keybind_Overlay, mon_x: i32, mon_y: i32, mon_w: i32, mon_h: i32) void {
100 + pub fn show(self: *KeybindOverlay, mon_x: i32, mon_y: i32, mon_w: i32, mon_h: i32, config: *config_mod.Config) void {
101 101 const display = self.display orelse return;
102 - const cfg = config_mod.get_config() orelse return;
103 102
104 - self.collect_keybinds(cfg);
103 + self.collectKeybinds(config);
105 104 if (self.line_count == 0) return;
106 105
107 106 var max_key_width: i32 = 0;
@@ -109,14 +108,14 @@ pub const Keybind_Overlay = struct {
109 108
110 109 for (0..self.line_count) |i| {
111 110 const key_slice = self.key_bufs[i][0..self.key_lens[i]];
112 - const key_w = self.text_width(display, key_slice);
113 - const desc_w = self.text_width(display, self.descs[i]);
111 + const key_w = self.textWidth(display, key_slice);
112 + const desc_w = self.textWidth(display, self.descs[i]);
114 113 if (key_w > max_key_width) max_key_width = key_w;
115 114 if (desc_w > max_desc_width) max_desc_width = desc_w;
116 115 }
117 116
118 117 const title = "Keybindings";
119 - const title_width = self.text_width(display, title);
118 + const title_width = self.textWidth(display, title);
120 119
121 120 const content_width = max_key_width + key_action_spacing + max_desc_width;
122 121 const min_width = @max(title_width, content_width);
@@ -126,7 +125,7 @@ pub const Keybind_Overlay = struct {
126 125 const title_height = self.font_height + 20;
127 126 self.height = title_height + @as(i32, @intCast(self.line_count)) * line_height + padding * 2;
128 127
129 - self.destroy_window(display);
128 + self.destroyWindow(display);
130 129
131 130 const x: i32 = mon_x + @divTrunc(mon_w - self.width, 2);
132 131 const y: i32 = mon_y + @divTrunc(mon_h - self.height, 2);
@@ -168,7 +167,7 @@ pub const Keybind_Overlay = struct {
168 167 self.visible = true;
169 168 }
170 169
171 - pub fn hide(self: *Keybind_Overlay) void {
170 + pub fn hide(self: *KeybindOverlay) void {
172 171 if (!self.visible) return;
173 172 if (self.display) |display| {
174 173 _ = xlib.XUngrabKeyboard(display, xlib.CurrentTime);
@@ -179,7 +178,7 @@ pub const Keybind_Overlay = struct {
179 178 self.visible = false;
180 179 }
181 180
182 - pub fn handle_key(self: *Keybind_Overlay, keysym: u64) bool {
181 + pub fn handleKey(self: *KeybindOverlay, keysym: u64) bool {
183 182 if (!self.visible) return false;
184 183 if (keysym == 0xff1b or keysym == 'q' or keysym == 'Q') {
185 184 self.hide();
@@ -188,28 +187,28 @@ pub const Keybind_Overlay = struct {
188 187 return false;
189 188 }
190 189
191 - pub fn is_overlay_window(self: *Keybind_Overlay, win: xlib.Window) bool {
190 + pub fn isOverlayWindow(self: *KeybindOverlay, win: xlib.Window) bool {
192 191 return self.visible and self.window != 0 and self.window == win;
193 192 }
194 193
195 - fn draw(self: *Keybind_Overlay, display: *xlib.Display, max_key_width: i32, title: []const u8) void {
196 - self.fill_rect(display, 0, 0, self.width, self.height, bg_color);
194 + fn draw(self: *KeybindOverlay, display: *xlib.Display, max_key_width: i32, title: []const u8) void {
195 + self.fillRect(display, 0, 0, self.width, self.height, bg_color);
197 196
198 - const title_x = @divTrunc(self.width - self.text_width(display, title), 2);
197 + const title_x = @divTrunc(self.width - self.textWidth(display, title), 2);
199 198 const title_y = padding + self.font.?.*.ascent;
200 - self.draw_text(display, title_x, title_y, title, fg_color);
199 + self.drawText(display, title_x, title_y, title, fg_color);
201 200
202 201 const line_height = self.font_height + line_spacing;
203 202 var y = padding + self.font_height + 20 + self.font.?.*.ascent;
204 203
205 204 for (0..self.line_count) |i| {
206 205 const key_slice = self.key_bufs[i][0..self.key_lens[i]];
207 - const key_w = self.text_width(display, key_slice);
208 - self.fill_rect(display, padding - 4, y - self.font.?.*.ascent - 2, key_w + 8, self.font_height + 4, key_bg_color);
209 - self.draw_text(display, padding, y, key_slice, fg_color);
206 + const key_w = self.textWidth(display, key_slice);
207 + self.fillRect(display, padding - 4, y - self.font.?.*.ascent - 2, key_w + 8, self.font_height + 4, key_bg_color);
208 + self.drawText(display, padding, y, key_slice, fg_color);
210 209
211 210 const desc_x = padding + max_key_width + key_action_spacing;
212 - self.draw_text(display, desc_x, y, self.descs[i], fg_color);
211 + self.drawText(display, desc_x, y, self.descs[i], fg_color);
213 212
214 213 y += line_height;
215 214 }
@@ -218,12 +217,12 @@ pub const Keybind_Overlay = struct {
218 217 _ = xlib.c.XFlush(display);
219 218 }
220 219
221 - fn fill_rect(self: *Keybind_Overlay, display: *xlib.Display, x: i32, y: i32, w: i32, h: i32, color: c_ulong) void {
220 + fn fillRect(self: *KeybindOverlay, display: *xlib.Display, x: i32, y: i32, w: i32, h: i32, color: c_ulong) void {
222 221 _ = xlib.XSetForeground(display, self.gc, color);
223 222 _ = xlib.XFillRectangle(display, self.pixmap, self.gc, x, y, @intCast(w), @intCast(h));
224 223 }
225 224
226 - fn draw_text(self: *Keybind_Overlay, display: *xlib.Display, x: i32, y: i32, text: []const u8, color: c_ulong) void {
225 + fn drawText(self: *KeybindOverlay, display: *xlib.Display, x: i32, y: i32, text: []const u8, color: c_ulong) void {
227 226 if (self.xft_draw == null or self.font == null) return;
228 227 if (text.len == 0) return;
229 228
@@ -242,14 +241,14 @@ pub const Keybind_Overlay = struct {
242 241 xlib.XftColorFree(display, visual, colormap, &xft_color);
243 242 }
244 243
245 - fn text_width(self: *Keybind_Overlay, display: *xlib.Display, text: []const u8) i32 {
244 + fn textWidth(self: *KeybindOverlay, display: *xlib.Display, text: []const u8) i32 {
246 245 if (self.font == null or text.len == 0) return 0;
247 246 var extents: xlib.XGlyphInfo = undefined;
248 247 xlib.XftTextExtentsUtf8(display, self.font, text.ptr, @intCast(text.len), &extents);
249 248 return extents.xOff;
250 249 }
251 250
252 - fn collect_keybinds(self: *Keybind_Overlay, cfg: *config_mod.Config) void {
251 + fn collectKeybinds(self: *KeybindOverlay, cfg: *config_mod.Config) void {
253 252 const priority_actions = [_]config_mod.Action{
254 253 .show_keybinds,
255 254 .quit,
@@ -272,8 +271,8 @@ pub const Keybind_Overlay = struct {
272 271
273 272 for (cfg.keybinds.items) |kb| {
274 273 if (kb.action == action and kb.key_count > 0) {
275 - self.format_key_to_buf(self.line_count, &kb.keys[0]);
276 - self.descs[self.line_count] = action_desc(action);
274 + self.formatKeyToBuf(self.line_count, &kb.keys[0]);
275 + self.descs[self.line_count] = actionDesc(action);
277 276 self.line_count += 1;
278 277 break;
279 278 }
@@ -281,7 +280,7 @@ pub const Keybind_Overlay = struct {
281 280 }
282 281 }
283 282
284 - fn format_key_to_buf(self: *Keybind_Overlay, idx: usize, key: *const config_mod.Key_Press) void {
283 + fn formatKeyToBuf(self: *KeybindOverlay, idx: usize, key: *const config_mod.KeyPress) void {
285 284 var len: usize = 0;
286 285 var buf = &self.key_bufs[idx];
287 286
@@ -301,7 +300,7 @@ pub const Keybind_Overlay = struct {
301 300 len += s.len;
302 301 }
303 302
304 - const key_name = keysym_to_name(key.keysym);
303 + const key_name = keysymToName(key.keysym);
305 304 if (len + key_name.len < buf.len) {
306 305 @memcpy(buf[len .. len + key_name.len], key_name);
307 306 len += key_name.len;
@@ -310,7 +309,7 @@ pub const Keybind_Overlay = struct {
310 309 self.key_lens[idx] = len;
311 310 }
312 311
313 - fn keysym_to_name(keysym: u64) []const u8 {
312 + fn keysymToName(keysym: u64) []const u8 {
314 313 const upper_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
315 314 const digits = "0123456789";
316 315
@@ -346,7 +345,7 @@ pub const Keybind_Overlay = struct {
346 345 };
347 346 }
348 347
349 - fn action_desc(action: config_mod.Action) []const u8 {
348 + fn actionDesc(action: config_mod.Action) []const u8 {
350 349 return switch (action) {
351 350 .show_keybinds => "Show Keybinds",
352 351 .quit => "Quit WM",
+721 −0 src/wm/actions.zig new file mode 100644 Large diff collapsed, click to expand
@@ -0,0 +1,721 @@
1 +const std = @import("std");
2 +
3 +const client_mod = @import("../client.zig");
4 +const config_mod = @import("../config/config.zig");
5 +const lua = @import("../config/lua.zig");
6 +const core = @import("core.zig");
7 +const tiling = @import("../layouts/tiling.zig");
8 +const monitor_mod = @import("../monitor.zig");
9 +const wm_mod = @import("wm.zig");
10 +const xlib = @import("../x11/xlib.zig");
11 +
12 +const Client = client_mod.Client;
13 +const Monitor = monitor_mod.Monitor;
14 +const WindowManager = wm_mod.WindowManager;
15 +
16 +const snap_distance: i32 = 32;
17 +
18 +pub fn spawnChildSetup(wm: *WindowManager) void {
19 + _ = std.c.setsid();
20 + if (wm.x11_fd >= 0) std.posix.close(@intCast(wm.x11_fd));
21 + const sigchld_handler = std.posix.Sigaction{
22 + .handler = .{ .handler = std.posix.SIG.DFL },
23 + .mask = std.mem.zeroes(std.posix.sigset_t),
24 + .flags = 0,
25 + };
26 + std.posix.sigaction(std.posix.SIG.CHLD, &sigchld_handler, null);
27 +}
28 +
29 +pub fn spawnCommand(wm: *WindowManager, cmd: []const u8) void {
30 + std.debug.print("running cmd: {s}\n", .{cmd});
31 + const pid = std.posix.fork() catch return;
32 + if (pid == 0) {
33 + spawnChildSetup(wm);
34 + var cmd_buf: [1024]u8 = undefined;
35 + if (cmd.len >= cmd_buf.len) {
36 + std.posix.exit(1);
37 + }
38 + @memcpy(cmd_buf[0..cmd.len], cmd);
39 + cmd_buf[cmd.len] = 0;
40 + const argv = [_:null]?[*:0]const u8{ "sh", "-c", @ptrCast(&cmd_buf) };
41 + _ = std.posix.execvpeZ("sh", &argv, std.c.environ) catch {};
42 + std.posix.exit(1);
43 + }
44 +}
45 +
46 +pub fn spawnTerminal(wm: *WindowManager) void {
47 + const pid = std.posix.fork() catch return;
48 + if (pid == 0) {
49 + spawnChildSetup(wm);
50 + var term_buf: [256]u8 = undefined;
51 + const terminal = wm.config.terminal;
52 + if (terminal.len >= term_buf.len) {
53 + std.posix.exit(1);
54 + }
55 + @memcpy(term_buf[0..terminal.len], terminal);
56 + term_buf[terminal.len] = 0;
57 + const term_ptr: [*:0]const u8 = @ptrCast(&term_buf);
58 + const argv = [_:null]?[*:0]const u8{term_ptr};
59 + _ = std.posix.execvpeZ(term_ptr, &argv, std.c.environ) catch {};
60 + std.posix.exit(1);
61 + }
62 +}
63 +
64 +pub fn movestack(direction: i32, wm: *WindowManager) void {
65 + const monitor = wm.selected_monitor orelse return;
66 + const current = monitor.sel orelse return;
67 +
68 + if (current.is_floating) {
69 + return;
70 + }
71 +
72 + var target: ?*Client = null;
73 +
74 + if (direction > 0) {
75 + target = current.next;
76 + while (target) |client| {
77 + if (client_mod.isVisible(client) and !client.is_floating) {
78 + break;
79 + }
80 + target = client.next;
81 + }
82 + if (target == null) {
83 + target = monitor.clients;
84 + while (target) |client| {
85 + if (client == current) {
86 + break;
87 + }
88 + if (client_mod.isVisible(client) and !client.is_floating) {
89 + break;
90 + }
91 + target = client.next;
92 + }
93 + }
94 + } else {
95 + var prev: ?*Client = null;
96 + var iter = monitor.clients;
97 + while (iter) |client| {
98 + if (client == current) {
99 + break;
100 + }
101 + if (client_mod.isVisible(client) and !client.is_floating) {
102 + prev = client;
103 + }
104 + iter = client.next;
105 + }
106 + if (prev == null) {
107 + iter = current.next;
108 + while (iter) |client| {
109 + if (client_mod.isVisible(client) and !client.is_floating) {
110 + prev = client;
111 + }
112 + iter = client.next;
113 + }
114 + }
115 + target = prev;
116 + }
117 +
118 + if (target) |swap_client| {
119 + if (swap_client != current) {
120 + client_mod.swapClients(current, swap_client);
121 + core.arrange(monitor, wm);
122 + core.focus(current, wm);
123 + }
124 + }
125 +}
126 +
127 +pub fn toggleView(tag_mask: u32, wm: *WindowManager) void {
128 + const monitor = wm.selected_monitor orelse return;
129 + const new_tags = monitor.tagset[monitor.sel_tags] ^ tag_mask;
130 + if (new_tags != 0) {
131 + monitor.tagset[monitor.sel_tags] = new_tags;
132 +
133 + if (new_tags == ~@as(u32, 0)) {
134 + monitor.pertag.prevtag = monitor.pertag.curtag;
135 + monitor.pertag.curtag = 0;
136 + }
137 +
138 + if ((new_tags & (@as(u32, 1) << @intCast(monitor.pertag.curtag -| 1))) == 0) {
139 + monitor.pertag.prevtag = monitor.pertag.curtag;
140 + var i: u32 = 0;
141 + while (i < 9) : (i += 1) {
142 + if ((new_tags & (@as(u32, 1) << @intCast(i))) != 0) break;
143 + }
144 + monitor.pertag.curtag = i + 1;
145 + }
146 +
147 + monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag];
148 + monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag];
149 + monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag];
150 +
151 + core.focusTopClient(monitor, wm);
152 + core.arrange(monitor, wm);
153 + wm.invalidateBars();
154 + }
155 +}
156 +
157 +pub fn toggleClientTag(tag_mask: u32, wm: *WindowManager) void {
158 + const monitor = wm.selected_monitor orelse return;
159 + const client = monitor.sel orelse return;
160 + const new_tags = client.tags ^ tag_mask;
161 + if (new_tags != 0) {
162 + client.tags = new_tags;
163 + core.focusTopClient(monitor, wm);
164 + core.arrange(monitor, wm);
165 + wm.invalidateBars();
166 + }
167 +}
168 +
169 +pub fn toggleGaps(wm: *WindowManager) void {
170 + const monitor = wm.selected_monitor orelse return;
171 + if (monitor.gap_inner_h == 0) {
172 + monitor.gap_inner_h = wm.config.gap_inner_h;
173 + monitor.gap_inner_v = wm.config.gap_inner_v;
174 + monitor.gap_outer_h = wm.config.gap_outer_h;
175 + monitor.gap_outer_v = wm.config.gap_outer_v;
176 + } else {
177 + monitor.gap_inner_h = 0;
178 + monitor.gap_inner_v = 0;
179 + monitor.gap_outer_h = 0;
180 + monitor.gap_outer_v = 0;
181 + }
182 + core.arrange(monitor, wm);
183 +}
184 +
185 +pub fn killFocused(wm: *WindowManager) void {
186 + const selected = wm.selected_monitor orelse return;
187 + const client = selected.sel orelse return;
188 + std.debug.print("killing window: 0x{x}\n", .{client.window});
189 +
190 + if (!core.sendEvent(client, wm.atoms.wm_delete, wm)) {
191 + _ = xlib.XGrabServer(wm.display.handle);
192 + _ = xlib.XKillClient(wm.display.handle, client.window);
193 + _ = xlib.XSync(wm.display.handle, xlib.False);
194 + _ = xlib.XUngrabServer(wm.display.handle);
195 + }
196 +}
197 +
198 +pub fn toggleFullscreen(wm: *WindowManager) void {
199 + const selected = wm.selected_monitor orelse return;
200 + const client = selected.sel orelse return;
201 + core.setFullscreen(client, !client.is_fullscreen, wm);
202 +}
203 +
204 +pub fn viewAdjacentTag(direction: i32, wm: *WindowManager) void {
205 + const monitor = wm.selected_monitor orelse return;
206 + const current_tag = monitor.pertag.curtag;
207 + var new_tag: i32 = @intCast(current_tag);
208 +
209 + new_tag += direction;
210 + if (new_tag < 1) new_tag = 9;
211 + if (new_tag > 9) new_tag = 1;
212 +
213 + const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
214 + core.view(tag_mask, wm);
215 +}
216 +
217 +pub fn viewAdjacentNonemptyTag(direction: i32, wm: *WindowManager) void {
218 + const monitor = wm.selected_monitor orelse return;
219 + const current_tag = monitor.pertag.curtag;
220 + var new_tag: i32 = @intCast(current_tag);
221 +
222 + var attempts: i32 = 0;
223 + while (attempts < 9) : (attempts += 1) {
224 + new_tag += direction;
225 + if (new_tag < 1) new_tag = 9;
226 + if (new_tag > 9) new_tag = 1;
227 +
228 + const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
229 + if (core.hasClientsOnTag(monitor, tag_mask)) {
230 + core.view(tag_mask, wm);
231 + return;
232 + }
233 + }
234 +}
235 +
236 +pub fn tagClient(tag_mask: u32, wm: *WindowManager) void {
237 + const monitor = wm.selected_monitor orelse return;
238 + const client = monitor.sel orelse return;
239 + if (tag_mask == 0) {
240 + return;
241 + }
242 + client.tags = tag_mask;
243 + core.focusTopClient(monitor, wm);
244 + core.arrange(monitor, wm);
245 + wm.invalidateBars();
246 + std.debug.print("tag_client: window=0x{x} tag_mask={d}\n", .{ client.window, tag_mask });
247 +}
248 +
249 +pub fn focusstack(direction: i32, wm: *WindowManager) void {
250 + const monitor = wm.selected_monitor orelse return;
251 + const current = monitor.sel orelse return;
252 +
253 + var next_client: ?*Client = null;
254 +
255 + if (direction > 0) {
256 + next_client = current.next;
257 + while (next_client) |client| {
258 + if (client_mod.isVisible(client)) {
259 + break;
260 + }
261 + next_client = client.next;
262 + }
263 + if (next_client == null) {
264 + next_client = monitor.clients;
265 + while (next_client) |client| {
266 + if (client_mod.isVisible(client)) {
267 + break;
268 + }
269 + next_client = client.next;
270 + }
271 + }
272 + } else {
273 + var prev: ?*Client = null;
274 + var iter = monitor.clients;
275 + while (iter) |client| {
276 + if (client == current) {
277 + break;
278 + }
279 + if (client_mod.isVisible(client)) {
280 + prev = client;
281 + }
282 + iter = client.next;
283 + }
284 + if (prev == null) {
285 + iter = current.next;
286 + while (iter) |client| {
287 + if (client_mod.isVisible(client)) {
288 + prev = client;
289 + }
290 + iter = client.next;
291 + }
292 + }
293 + next_client = prev;
294 + }
295 +
296 + if (next_client) |client| {
297 + core.focus(client, wm);
298 + if (client.monitor) |client_monitor| {
299 + core.restack(client_monitor, wm);
300 + }
301 + }
302 +}
303 +
304 +pub fn toggleFloating(wm: *WindowManager) void {
305 + const monitor = wm.selected_monitor orelse return;
306 + const client = monitor.sel orelse return;
307 +
308 + if (client.is_fullscreen) {
309 + return;
310 + }
311 +
312 + client.is_floating = !client.is_floating;
313 +
314 + if (client.is_floating) {
315 + tiling.resize(client, client.x, client.y, client.width, client.height, false);
316 + }
317 +
318 + core.arrange(monitor, wm);
319 + std.debug.print("toggle_floating: window=0x{x} floating={}\n", .{ client.window, client.is_floating });
320 +}
321 +
322 +pub fn incnmaster(delta: i32, wm: *WindowManager) void {
323 + const monitor = wm.selected_monitor orelse return;
324 + const new_val = @max(0, monitor.nmaster + delta);
325 + monitor.nmaster = new_val;
326 + monitor.pertag.nmasters[monitor.pertag.curtag] = new_val;
327 + core.arrange(monitor, wm);
328 + std.debug.print("incnmaster: nmaster={d}\n", .{monitor.nmaster});
329 +}
330 +
331 +pub fn setmfact(delta: f32, wm: *WindowManager) void {
332 + const monitor = wm.selected_monitor orelse return;
333 + const new_mfact = monitor.mfact + delta;
334 + if (new_mfact < 0.05 or new_mfact > 0.95) {
335 + return;
336 + }
337 + monitor.mfact = new_mfact;
338 + monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact;
339 + core.arrange(monitor, wm);
340 + std.debug.print("setmfact: mfact={d:.2}\n", .{monitor.mfact});
341 +}
342 +
343 +pub fn cycleLayout(wm: *WindowManager) void {
344 + const monitor = wm.selected_monitor orelse return;
345 + const new_lt = (monitor.sel_lt + 1) % 5;
346 + monitor.sel_lt = new_lt;
347 + monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
348 + if (new_lt != 3) {
349 + monitor.scroll_offset = 0;
350 + }
351 + core.arrange(monitor, wm);
352 + wm.invalidateBars();
353 + if (monitor.lt[monitor.sel_lt]) |layout| {
354 + std.debug.print("cycle_layout: {s}\n", .{layout.symbol});
355 + }
356 +}
357 +
358 +pub fn setLayout(layout_name: ?[]const u8, wm: *WindowManager) void {
359 + const monitor = wm.selected_monitor orelse return;
360 + const name = layout_name orelse return;
361 +
362 + const new_lt: u32 = if (std.mem.eql(u8, name, "tiling") or std.mem.eql(u8, name, "[]="))
363 + 0
364 + else if (std.mem.eql(u8, name, "monocle") or std.mem.eql(u8, name, "[M]"))
365 + 1
366 + else if (std.mem.eql(u8, name, "floating") or std.mem.eql(u8, name, "><>"))
367 + 2
368 + else if (std.mem.eql(u8, name, "scrolling") or std.mem.eql(u8, name, "[S]"))
369 + 3
370 + else if (std.mem.eql(u8, name, "grid") or std.mem.eql(u8, name, "[#]"))
371 + 4
372 + else {
373 + std.debug.print("set_layout: unknown layout '{s}'\n", .{name});
374 + return;
375 + };
376 +
377 + monitor.sel_lt = new_lt;
378 + monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
379 + if (new_lt != 3) {
380 + monitor.scroll_offset = 0;
381 + }
382 + core.arrange(monitor, wm);
383 + wm.invalidateBars();
384 + if (monitor.lt[monitor.sel_lt]) |layout| {
385 + std.debug.print("set_layout: {s}\n", .{layout.symbol});
386 + }
387 +}
388 +
389 +pub fn setLayoutIndex(index: u32, wm: *WindowManager) void {
390 + const monitor = wm.selected_monitor orelse return;
391 + monitor.sel_lt = index;
392 + monitor.pertag.sellts[monitor.pertag.curtag] = index;
393 + if (index != 3) {
394 + monitor.scroll_offset = 0;
395 + }
396 + core.arrange(monitor, wm);
397 + wm.invalidateBars();
398 + if (monitor.lt[monitor.sel_lt]) |layout| {
399 + std.debug.print("set_layout_index: {s}\n", .{layout.symbol});
400 + }
401 +}
402 +
403 +pub fn focusmon(direction: i32, wm: *WindowManager) void {
404 + const selmon = wm.selected_monitor orelse return;
405 + const target = monitor_mod.dirToMonitor(wm, direction) orelse return;
406 + if (target == selmon) {
407 + return;
408 + }
409 + core.unfocusClient(selmon.sel, false, wm);
410 + wm.selected_monitor = target;
411 + core.focus(null, wm);
412 + std.debug.print("focusmon: monitor {d}\n", .{target.num});
413 +}
414 +
415 +pub fn sendmon(direction: i32, wm: *WindowManager) void {
416 + const source_monitor = wm.selected_monitor orelse return;
417 + const client = source_monitor.sel orelse return;
418 + const target = monitor_mod.dirToMonitor(wm, direction) orelse return;
419 +
420 + if (target == source_monitor) {
421 + return;
422 + }
423 +
424 + client_mod.detach(client);
425 + client_mod.detachStack(client);
426 + client.monitor = target;
427 + client.tags = target.tagset[target.sel_tags];
428 + client_mod.attachAside(client);
429 + client_mod.attachStack(client);
430 +
431 + core.focusTopClient(source_monitor, wm);
432 + core.arrange(source_monitor, wm);
433 + core.arrange(target, wm);
434 +
435 + std.debug.print("sendmon: window=0x{x} to monitor {d}\n", .{ client.window, target.num });
436 +}
437 +
438 +pub fn snapX(client: *Client, new_x: i32, monitor: *Monitor) i32 {
439 + const client_width = client.width + 2 * client.border_width;
440 + if (@abs(monitor.win_x - new_x) < snap_distance) {
441 + return monitor.win_x;
442 + } else if (@abs((monitor.win_x + monitor.win_w) - (new_x + client_width)) < snap_distance) {
443 + return monitor.win_x + monitor.win_w - client_width;
444 + }
445 + return new_x;
446 +}
447 +
448 +pub fn snapY(client: *Client, new_y: i32, monitor: *Monitor) i32 {
449 + const client_height = client.height + 2 * client.border_width;
450 + if (@abs(monitor.win_y - new_y) < snap_distance) {
451 + return monitor.win_y;
452 + } else if (@abs((monitor.win_y + monitor.win_h) - (new_y + client_height)) < snap_distance) {
453 + return monitor.win_y + monitor.win_h - client_height;
454 + }
455 + return new_y;
456 +}
457 +
458 +pub fn movemouse(wm: *WindowManager) void {
459 + const monitor = wm.selected_monitor orelse return;
460 + const client = monitor.sel orelse return;
461 +
462 + if (client.is_fullscreen) {
463 + return;
464 + }
465 +
466 + core.restack(monitor, wm);
467 +
468 + const was_floating = client.is_floating;
469 + if (!client.is_floating) {
470 + client.is_floating = true;
471 + }
472 +
473 + var root_x: c_int = undefined;
474 + var root_y: c_int = undefined;
475 + var dummy_win: xlib.Window = undefined;
476 + var dummy_int: c_int = undefined;
477 + var dummy_uint: c_uint = undefined;
478 +
479 + _ = xlib.XQueryPointer(wm.display.handle, wm.display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint);
480 +
481 + const grab_result = xlib.XGrabPointer(
482 + wm.display.handle,
483 + wm.display.root,
484 + xlib.False,
485 + xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
486 + xlib.GrabModeAsync,
487 + xlib.GrabModeAsync,
488 + xlib.None,
489 + wm.cursors.move,
490 + xlib.CurrentTime,
491 + );
492 +
493 + if (grab_result != xlib.GrabSuccess) {
494 + return;
495 + }
496 +
497 + const start_x = client.x;
498 + const start_y = client.y;
499 + const pointer_start_x = root_x;
500 + const pointer_start_y = root_y;
501 + var last_time: c_ulong = 0;
502 +
503 + var event: xlib.XEvent = undefined;
504 + var done = false;
505 +
506 + while (!done) {
507 + _ = xlib.XNextEvent(wm.display.handle, &event);
508 +
509 + switch (event.type) {
510 + xlib.MotionNotify => {
511 + const motion = &event.xmotion;
512 + if ((motion.time - last_time) < (1000 / 60)) {
513 + continue;
514 + }
515 + last_time = motion.time;
516 + const delta_x = motion.x_root - pointer_start_x;
517 + const delta_y = motion.y_root - pointer_start_y;
518 + var new_x = start_x + delta_x;
519 + var new_y = start_y + delta_y;
520 + if (client.monitor) |client_monitor| {
521 + new_x = snapX(client, new_x, client_monitor);
522 + new_y = snapY(client, new_y, client_monitor);
523 + }
524 + tiling.resize(client, new_x, new_y, client.width, client.height, true);
525 + },
526 + xlib.ButtonRelease => {
527 + done = true;
528 + },
529 + else => {},
530 + }
531 + }
532 +
533 + _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
534 +
535 + const new_mon = monitor_mod.rectToMonitor(wm, client.x, client.y, client.width, client.height);
536 + if (new_mon != null and new_mon != monitor) {
537 + client_mod.detach(client);
538 + client_mod.detachStack(client);
539 + client.monitor = new_mon;
540 + client.tags = new_mon.?.tagset[new_mon.?.sel_tags];
541 + client_mod.attachAside(client);
542 + client_mod.attachStack(client);
543 + wm.selected_monitor = new_mon;
544 + core.focus(client, wm);
545 + core.arrange(monitor, wm);
546 + core.arrange(new_mon.?, wm);
547 + } else {
548 + core.arrange(monitor, wm);
549 + }
550 +
551 + if (wm.config.auto_tile and !was_floating) {
552 + const drop_monitor = client.monitor orelse return;
553 + const center_x = client.x + @divTrunc(client.width, 2);
554 + const center_y = client.y + @divTrunc(client.height, 2);
555 +
556 + if (client_mod.tiledWindowAt(client, drop_monitor, center_x, center_y)) |target| {
557 + client_mod.insertBefore(client, target);
558 + }
559 +
560 + client.is_floating = false;
561 + core.arrange(drop_monitor, wm);
562 + }
563 +}
564 +
565 +pub fn resizemouse(wm: *WindowManager) void {
566 + const monitor = wm.selected_monitor orelse return;
567 + const client = monitor.sel orelse return;
568 +
569 + if (client.is_fullscreen) {
570 + return;
571 + }
572 +
573 + core.restack(monitor, wm);
574 +
575 + if (!client.is_floating) {
576 + client.is_floating = true;
577 + }
578 +
579 + const grab_result = xlib.XGrabPointer(
580 + wm.display.handle,
581 + wm.display.root,
582 + xlib.False,
583 + xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
584 + xlib.GrabModeAsync,
585 + xlib.GrabModeAsync,
586 + xlib.None,
587 + wm.cursors.resize,
588 + xlib.CurrentTime,
589 + );
590 +
591 + if (grab_result != xlib.GrabSuccess) {
592 + return;
593 + }
594 +
595 + _ = xlib.XWarpPointer(wm.display.handle, xlib.None, client.window, 0, 0, 0, 0, client.width + client.border_width - 1, client.height + client.border_width - 1);
596 +
597 + var event: xlib.XEvent = undefined;
598 + var done = false;
599 + var last_time: c_ulong = 0;
600 +
601 + while (!done) {
602 + _ = xlib.XNextEvent(wm.display.handle, &event);
603 +
604 + switch (event.type) {
605 + xlib.MotionNotify => {
606 + const motion = &event.xmotion;
607 + if ((motion.time - last_time) < (1000 / 60)) {
608 + continue;
609 + }
610 + last_time = motion.time;
611 + var new_width = @max(1, motion.x_root - client.x - 2 * client.border_width + 1);
612 + var new_height = @max(1, motion.y_root - client.y - 2 * client.border_width + 1);
613 + if (client.monitor) |client_monitor| {
614 + const client_right = client.x + new_width + 2 * client.border_width;
615 + const client_bottom = client.y + new_height + 2 * client.border_width;
616 + const mon_right = client_monitor.win_x + client_monitor.win_w;
617 + const mon_bottom = client_monitor.win_y + client_monitor.win_h;
618 + if (@abs(mon_right - client_right) < snap_distance) {
619 + new_width = @max(1, mon_right - client.x - 2 * client.border_width);
620 + }
621 + if (@abs(mon_bottom - client_bottom) < snap_distance) {
622 + new_height = @max(1, mon_bottom - client.y - 2 * client.border_width);
623 + }
624 + }
625 + tiling.resize(client, client.x, client.y, new_width, new_height, true);
626 + },
627 + xlib.ButtonRelease => {
628 + done = true;
629 + },
630 + else => {},
631 + }
632 + }
633 +
634 + _ = xlib.XUngrabPointer(wm.display.handle, xlib.CurrentTime);
635 + core.arrange(monitor, wm);
636 +}
637 +
638 +/// Config-loading callback for wm.reloadConfig().
639 +/// Re-initialises Lua and loads the config file (or falls back to defaults).
640 +pub fn reloadLoadConfig(wm: *WindowManager) void {
641 + lua.deinit();
642 + _ = lua.init(&wm.config);
643 +
644 + const loaded = if (wm.config_path) |path|
645 + lua.loadFile(path)
646 + else
647 + lua.loadConfig();
648 +
649 + if (loaded) {
650 + if (wm.config_path) |path| {
651 + std.debug.print("reloaded config from {s}\n", .{path});
652 + } else {
653 + std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{});
654 + }
655 + } else {
656 + std.debug.print("reload failed, restoring defaults\n", .{});
657 + config_mod.initializeDefaultConfig(&wm.config);
658 + }
659 +}
660 +
661 +pub fn executeAction(action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8, wm: *WindowManager) void {
662 + switch (action) {
663 + .spawn_terminal => spawnTerminal(wm),
664 + .spawn => {
665 + if (str_arg) |cmd| spawnCommand(wm, cmd);
666 + },
667 + .kill_client => killFocused(wm),
668 + .quit => {
669 + std.debug.print("quit keybind pressed\n", .{});
670 + wm.running = false;
671 + },
672 + .reload_config => wm.reloadConfig(reloadLoadConfig),
673 + .restart => wm.reloadConfig(reloadLoadConfig),
674 + .show_keybinds => {
675 + if (wm.overlay) |overlay| {
676 + const mon = wm.selected_monitor orelse wm.monitors;
677 + if (mon) |m| {
678 + overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h, &wm.config);
679 + }
680 + }
681 + },
682 + .focus_next => focusstack(1, wm),
683 + .focus_prev => focusstack(-1, wm),
684 + .move_next => movestack(1, wm),
685 + .move_prev => movestack(-1, wm),
686 + .resize_master => setmfact(@as(f32, @floatFromInt(int_arg)) / 1000.0, wm),
687 + .inc_master => incnmaster(1, wm),
688 + .dec_master => incnmaster(-1, wm),
689 + .toggle_floating => toggleFloating(wm),
690 + .toggle_fullscreen => toggleFullscreen(wm),
691 + .toggle_gaps => toggleGaps(wm),
692 + .cycle_layout => cycleLayout(wm),
693 + .set_layout => setLayout(str_arg, wm),
694 + .set_layout_tiling => setLayoutIndex(0, wm),
695 + .set_layout_floating => setLayoutIndex(2, wm),
696 + .view_tag => {
697 + const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
698 + core.view(tag_mask, wm);
699 + },
700 + .view_next_tag => viewAdjacentTag(1, wm),
701 + .view_prev_tag => viewAdjacentTag(-1, wm),
702 + .view_next_nonempty_tag => viewAdjacentNonemptyTag(1, wm),
703 + .view_prev_nonempty_tag => viewAdjacentNonemptyTag(-1, wm),
704 + .move_to_tag => {
705 + const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
706 + tagClient(tag_mask, wm);
707 + },
708 + .toggle_view_tag => {
709 + const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
710 + toggleView(tag_mask, wm);
711 + },
712 + .toggle_tag => {
713 + const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
714 + toggleClientTag(tag_mask, wm);
715 + },
716 + .focus_monitor => focusmon(int_arg, wm),
717 + .send_to_monitor => sendmon(int_arg, wm),
718 + .scroll_left => core.scrollLayout(-1, wm),
719 + .scroll_right => core.scrollLayout(1, wm),
720 + }
721 +}
+736 −0 src/wm/core.zig new file mode 100644 Large diff collapsed, click to expand
@@ -0,0 +1,736 @@
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 + focusTopClient(monitor, wm);
733 + arrange(monitor, wm);
734 + wm.invalidateBars();
735 + std.debug.print("view: tag_mask={d}\n", .{monitor.tagset[monitor.sel_tags]});
736 +}
+362 −0 src/wm/handlers.zig new file mode 100644
@@ -0,0 +1,362 @@
1 +const std = @import("std");
2 +
3 +const actions = @import("actions.zig");
4 +const bar_mod = @import("../bar/bar.zig");
5 +const client_mod = @import("../client.zig");
6 +const core = @import("core.zig");
7 +const tiling = @import("../layouts/tiling.zig");
8 +const monitor_mod = @import("../monitor.zig");
9 +const wm_mod = @import("wm.zig");
10 +const events = @import("../x11/events.zig");
11 +const xlib = @import("../x11/xlib.zig");
12 +
13 +const WindowManager = wm_mod.WindowManager;
14 +
15 +pub fn handleEvent(event: *xlib.XEvent, wm: *WindowManager) void {
16 + const event_type = events.getEventType(event);
17 +
18 + if (event_type == .button_press) {
19 + std.debug.print("EVENT: button_press received type={d}\n", .{event.type});
20 + }
21 +
22 + switch (event_type) {
23 + .map_request => handleMapRequest(&event.xmaprequest, wm),
24 + .configure_request => handleConfigureRequest(&event.xconfigurerequest, wm),
25 + .key_press => handleKeyPress(&event.xkey, wm),
26 + .destroy_notify => handleDestroyNotify(&event.xdestroywindow, wm),
27 + .unmap_notify => handleUnmapNotify(&event.xunmap, wm),
28 + .enter_notify => handleEnterNotify(&event.xcrossing, wm),
29 + .focus_in => handleFocusIn(&event.xfocus, wm),
30 + .motion_notify => handleMotionNotify(&event.xmotion, wm),
31 + .client_message => handleClientMessage(&event.xclient, wm),
32 + .button_press => handleButtonPress(&event.xbutton, wm),
33 + .expose => handleExpose(&event.xexpose, wm),
34 + .property_notify => handlePropertyNotify(&event.xproperty, wm),
35 + else => {},
36 + }
37 +}
38 +
39 +fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void {
40 + std.debug.print("map_request: window=0x{x}\n", .{event.window});
41 +
42 + var window_attributes: xlib.XWindowAttributes = undefined;
43 + if (xlib.XGetWindowAttributes(wm.display.handle, event.window, &window_attributes) == 0) {
44 + return;
45 + }
46 + if (window_attributes.override_redirect != 0) {
47 + return;
48 + }
49 + if (client_mod.windowToClient(wm.monitors, event.window) != null) {
50 + return;
51 + }
52 +
53 + core.manage(event.window, &window_attributes, wm);
54 +}
55 +
56 +fn handleConfigureRequest(event: *xlib.XConfigureRequestEvent, wm: *WindowManager) void {
57 + const client = client_mod.windowToClient(wm.monitors, event.window);
58 +
59 + if (client) |managed_client| {
60 + if ((event.value_mask & xlib.c.CWBorderWidth) != 0) {
61 + managed_client.border_width = event.border_width;
62 + } else if (managed_client.is_floating or (managed_client.monitor != null and managed_client.monitor.?.lt[managed_client.monitor.?.sel_lt] == null)) {
63 + const monitor = managed_client.monitor orelse return;
64 + if ((event.value_mask & xlib.c.CWX) != 0) {
65 + managed_client.old_x = managed_client.x;
66 + managed_client.x = monitor.mon_x + event.x;
67 + }
68 + if ((event.value_mask & xlib.c.CWY) != 0) {
69 + managed_client.old_y = managed_client.y;
70 + managed_client.y = monitor.mon_y + event.y;
71 + }
72 + if ((event.value_mask & xlib.c.CWWidth) != 0) {
73 + managed_client.old_width = managed_client.width;
74 + managed_client.width = event.width;
75 + }
76 + if ((event.value_mask & xlib.c.CWHeight) != 0) {
77 + managed_client.old_height = managed_client.height;
78 + managed_client.height = event.height;
79 + }
80 + const client_full_width = managed_client.width + managed_client.border_width * 2;
81 + const client_full_height = managed_client.height + managed_client.border_width * 2;
82 + if ((managed_client.x + managed_client.width) > monitor.mon_x + monitor.mon_w and managed_client.is_floating) {
83 + managed_client.x = monitor.mon_x + @divTrunc(monitor.mon_w, 2) - @divTrunc(client_full_width, 2);
84 + }
85 + if ((managed_client.y + managed_client.height) > monitor.mon_y + monitor.mon_h and managed_client.is_floating) {
86 + managed_client.y = monitor.mon_y + @divTrunc(monitor.mon_h, 2) - @divTrunc(client_full_height, 2);
87 + }
88 + if (((event.value_mask & (xlib.c.CWX | xlib.c.CWY)) != 0) and ((event.value_mask & (xlib.c.CWWidth | xlib.c.CWHeight)) == 0)) {
89 + tiling.sendConfigure(managed_client);
90 + }
91 + if (client_mod.isVisible(managed_client)) {
92 + _ = xlib.XMoveResizeWindow(wm.display.handle, managed_client.window, managed_client.x, managed_client.y, @intCast(managed_client.width), @intCast(managed_client.height));
93 + }
94 + } else {
95 + tiling.sendConfigure(managed_client);
96 + }
97 + } else {
98 + var changes: xlib.XWindowChanges = undefined;
99 + changes.x = event.x;
100 + changes.y = event.y;
101 + changes.width = event.width;
102 + changes.height = event.height;
103 + changes.border_width = event.border_width;
104 + changes.sibling = event.above;
105 + changes.stack_mode = event.detail;
106 + _ = xlib.XConfigureWindow(wm.display.handle, event.window, @intCast(event.value_mask), &changes);
107 + }
108 + _ = xlib.XSync(wm.display.handle, xlib.False);
109 +}
110 +
111 +fn handleKeyPress(event: *xlib.XKeyEvent, wm: *WindowManager) void {
112 + const keysym = xlib.XKeycodeToKeysym(wm.display.handle, @intCast(event.keycode), 0);
113 +
114 + if (wm.overlay) |overlay| {
115 + if (overlay.handleKey(keysym)) return;
116 + }
117 +
118 + const clean_state = event.state & ~@as(c_uint, xlib.LockMask | xlib.Mod2Mask);
119 +
120 + if (wm.chord.isTimedOut()) {
121 + wm.chord.reset(wm.display.handle);
122 + }
123 +
124 + _ = wm.chord.push(.{ .mod_mask = clean_state, .keysym = keysym });
125 +
126 + for (wm.config.keybinds.items) |keybind| {
127 + if (keybind.key_count == 0) continue;
128 +
129 + if (keybind.key_count == wm.chord.index) {
130 + var matches = true;
131 + for (0..keybind.key_count) |i| {
132 + if (wm.chord.keys[i].keysym != keybind.keys[i].keysym or
133 + wm.chord.keys[i].mod_mask != keybind.keys[i].mod_mask)
134 + {
135 + matches = false;
136 + break;
137 + }
138 + }
139 + if (matches) {
140 + actions.executeAction(keybind.action, keybind.int_arg, keybind.str_arg, wm);
141 + wm.chord.reset(wm.display.handle);
142 + return;
143 + }
144 + }
145 + }
146 +
147 + var has_partial_match = false;
148 + for (wm.config.keybinds.items) |keybind| {
149 + if (keybind.key_count > wm.chord.index) {
150 + var matches = true;
151 + for (0..wm.chord.index) |i| {
152 + if (wm.chord.keys[i].keysym != keybind.keys[i].keysym or
153 + wm.chord.keys[i].mod_mask != keybind.keys[i].mod_mask)
154 + {
155 + matches = false;
156 + break;
157 + }
158 + }
159 + if (matches) {
160 + has_partial_match = true;
161 + break;
162 + }
163 + }
164 + }
165 +
166 + if (has_partial_match) {
167 + wm.chord.grabKeyboard(wm.display.handle, wm.display.root);
168 + } else {
169 + wm.chord.reset(wm.display.handle);
170 + }
171 +}
172 +
173 +fn cleanMask(mask: c_uint, wm: *WindowManager) c_uint {
174 + const lock: c_uint = @intCast(xlib.LockMask);
175 + const shift: c_uint = @intCast(xlib.ShiftMask);
176 + const ctrl: c_uint = @intCast(xlib.ControlMask);
177 + const mod1: c_uint = @intCast(xlib.Mod1Mask);
178 + const mod2: c_uint = @intCast(xlib.Mod2Mask);
179 + const mod3: c_uint = @intCast(xlib.Mod3Mask);
180 + const mod4: c_uint = @intCast(xlib.Mod4Mask);
181 + const mod5: c_uint = @intCast(xlib.Mod5Mask);
182 + return mask & ~(lock | wm.numlock_mask) & (shift | ctrl | mod1 | mod2 | mod3 | mod4 | mod5);
183 +}
184 +
185 +fn handleExpose(event: *xlib.XExposeEvent, wm: *WindowManager) void {
186 + if (event.count != 0) return;
187 + if (bar_mod.windowToBar(wm.bars, event.window)) |bar| {
188 + bar.invalidate();
189 + bar.draw(wm.display.handle, wm.config);
190 + }
191 +}
192 +
193 +fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void {
194 + std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow });
195 +
196 + const clicked_monitor = monitor_mod.windowToMonitor(wm, event.window);
197 + if (clicked_monitor) |monitor| {
198 + if (monitor != wm.selected_monitor) {
199 + if (wm.selected_monitor) |selmon| {
200 + core.unfocusClient(selmon.sel, true, wm);
201 + }
202 + wm.selected_monitor = monitor;
203 + core.focus(null, wm);
204 + }
205 + }
206 +
207 + if (bar_mod.windowToBar(wm.bars, event.window)) |bar| {
208 + const clicked_tag = bar.handleClick(wm.display.handle, event.x, wm.config);
209 + if (clicked_tag) |tag_index| {
210 + const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index);
211 + core.view(tag_mask, wm);
212 + }
213 + return;
214 + }
215 +
216 + const click_client = client_mod.windowToClient(wm.monitors, event.window);
217 + if (click_client) |found_client| {
218 + core.focus(found_client, wm);
219 + if (wm.selected_monitor) |selmon| {
220 + core.restack(selmon, wm);
221 + }
222 + _ = xlib.XAllowEvents(wm.display.handle, xlib.ReplayPointer, xlib.CurrentTime);
223 + }
224 +
225 + const clean_state = cleanMask(event.state, wm);
226 + for (wm.config.buttons.items) |button| {
227 + if (button.click != .client_win) continue;
228 + const button_clean_mask = cleanMask(button.mod_mask, wm);
229 + if (clean_state == button_clean_mask and event.button == button.button) {
230 + switch (button.action) {
231 + .move_mouse => actions.movemouse(wm),
232 + .resize_mouse => actions.resizemouse(wm),
233 + .toggle_floating => {
234 + if (click_client) |found_client| {
235 + found_client.is_floating = !found_client.is_floating;
236 + if (wm.selected_monitor) |monitor| {
237 + core.arrange(monitor, wm);
238 + }
239 + }
240 + },
241 + }
242 + return;
243 + }
244 + }
245 +}
246 +
247 +fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void {
248 + const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
249 +
250 + if (event.message_type == wm.atoms.net_wm_state) {
251 + const action = event.data.l[0];
252 + const first_property: xlib.Atom = @intCast(event.data.l[1]);
253 + const second_property: xlib.Atom = @intCast(event.data.l[2]);
254 +
255 + if (first_property == wm.atoms.net_wm_state_fullscreen or second_property == wm.atoms.net_wm_state_fullscreen) {
256 + const net_wm_state_remove = 0;
257 + const net_wm_state_add = 1;
258 + const net_wm_state_toggle = 2;
259 +
260 + if (action == net_wm_state_add) {
261 + core.setFullscreen(client, true, wm);
262 + } else if (action == net_wm_state_remove) {
263 + core.setFullscreen(client, false, wm);
264 + } else if (action == net_wm_state_toggle) {
265 + core.setFullscreen(client, !client.is_fullscreen, wm);
266 + }
267 + }
268 + } else if (event.message_type == wm.atoms.net_active_window) {
269 + const selected = wm.selected_monitor orelse return;
270 + if (client != selected.sel and !client.is_urgent) {
271 + core.setUrgent(client, true, wm);
272 + }
273 + }
274 +}
275 +
276 +fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void {
277 + const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
278 + std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
279 + core.unmanage(client, wm);
280 +}
281 +
282 +fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void {
283 + const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
284 + std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
285 + core.unmanage(client, wm);
286 +}
287 +
288 +fn handleEnterNotify(event: *xlib.XCrossingEvent, wm: *WindowManager) void {
289 + if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != wm.display.root) {
290 + return;
291 + }
292 +
293 + const client = client_mod.windowToClient(wm.monitors, event.window);
294 + const target_mon = if (client) |c| c.monitor else monitor_mod.windowToMonitor(wm, event.window);
295 + const selmon = wm.selected_monitor;
296 +
297 + if (target_mon != selmon) {
298 + if (selmon) |sel| {
299 + core.unfocusClient(sel.sel, true, wm);
300 + }
301 + wm.selected_monitor = target_mon;
302 + } else if (client == null) {
303 + return;
304 + } else if (selmon) |sel| {
305 + if (client.? == sel.sel) {
306 + return;
307 + }
308 + }
309 +
310 + core.focus(client, wm);
311 +}
312 +
313 +fn handleFocusIn(event: *xlib.XFocusChangeEvent, wm: *WindowManager) void {
314 + const selmon = wm.selected_monitor orelse return;
315 + const selected = selmon.sel orelse return;
316 + if (event.window != selected.window) {
317 + core.setFocus(selected, wm);
318 + }
319 +}
320 +
321 +fn handleMotionNotify(event: *xlib.XMotionEvent, wm: *WindowManager) void {
322 + if (event.window != wm.display.root) return;
323 +
324 + const target_mon = monitor_mod.rectToMonitor(wm, event.x_root, event.y_root, 1, 1);
325 + if (target_mon != wm.last_motion_monitor and wm.last_motion_monitor != null) {
326 + if (wm.selected_monitor) |selmon| {
327 + core.unfocusClient(selmon.sel, true, wm);
328 + }
329 + wm.selected_monitor = target_mon;
330 + core.focus(null, wm);
331 + }
332 + wm.last_motion_monitor = target_mon;
333 +}
334 +
335 +fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void {
336 + if (event.state == xlib.PropertyDelete) {
337 + return;
338 + }
339 +
340 + const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
341 +
342 + if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
343 + var trans: xlib.Window = 0;
344 + if (!client.is_floating and xlib.XGetTransientForHint(wm.display.handle, client.window, &trans) != 0) {
345 + client.is_floating = client_mod.windowToClient(wm.monitors, trans) != null;
346 + if (client.is_floating) {
347 + if (client.monitor) |monitor| {
348 + core.arrange(monitor, wm);
349 + }
350 + }
351 + }
352 + } else if (event.atom == xlib.XA_WM_NORMAL_HINTS) {
353 + client.hints_valid = false;
354 + } else if (event.atom == xlib.XA_WM_HINTS) {
355 + core.updateWmHints(client, wm);
356 + wm.invalidateBars();
357 + } else if (event.atom == xlib.XA_WM_NAME or event.atom == wm.atoms.net_wm_name) {
358 + core.updateTitle(client, wm);
359 + } else if (event.atom == wm.atoms.net_wm_window_type) {
360 + core.updateWindowType(client, wm);
361 + }
362 +}
+592 −0 src/wm/wm.zig new file mode 100644 Large diff collapsed, click to expand
@@ -0,0 +1,592 @@
1 +const std = @import("std");
2 +const mem = std.mem;
3 +
4 +const display_mod = @import("../x11/display.zig");
5 +const xlib = @import("../x11/xlib.zig");
6 +const atoms_mod = @import("../x11/atoms.zig");
7 +const chord_mod = @import("../keyboard/chord.zig");
8 +const monitor_mod = @import("../monitor.zig");
9 +const bar_mod = @import("../bar/bar.zig");
10 +const blocks_mod = @import("../bar/blocks/blocks.zig");
11 +const config_mod = @import("../config/config.zig");
12 +const overlay_mod = @import("../overlay.zig");
13 +const animations = @import("../animations.zig");
14 +const tiling = @import("../layouts/tiling.zig");
15 +const monocle = @import("../layouts/monocle.zig");
16 +const floating = @import("../layouts/floating.zig");
17 +const scrolling = @import("../layouts/scrolling.zig");
18 +const grid = @import("../layouts/grid.zig");
19 +
20 +pub const core = @import("core.zig");
21 +pub const actions = @import("actions.zig");
22 +pub const handlers = @import("handlers.zig");
23 +
24 +const Display = display_mod.Display;
25 +const Atoms = atoms_mod.Atoms;
26 +const ChordState = chord_mod.ChordState;
27 +const Monitor = monitor_mod.Monitor;
28 +const Bar = bar_mod.Bar;
29 +const Config = config_mod.Config;
30 +
31 +// Standard ICCCM WM_STATE values used by get_state and set_client_state.
32 +pub const NormalState: c_long = 1;
33 +pub const WithdrawnState: c_long = 0;
34 +pub const IconicState: c_long = 3;
35 +pub const IsViewable: c_int = 2;
36 +
37 +pub const Cursors = struct {
38 + normal: xlib.Cursor,
39 + resize: xlib.Cursor,
40 + move: xlib.Cursor,
41 +
42 + pub fn init(display: *Display) Cursors {
43 + return .{
44 + .normal = xlib.XCreateFontCursor(display.handle, xlib.XC_left_ptr),
45 + .resize = xlib.XCreateFontCursor(display.handle, xlib.XC_sizing),
46 + .move = xlib.XCreateFontCursor(display.handle, xlib.XC_fleur),
47 + };
48 + }
49 +};
50 +
51 +pub const WindowManager = struct {
52 + allocator: mem.Allocator,
53 +
54 + /// The connection to the X server.
55 + display: Display,
56 + x11_fd: c_int,
57 + /// Invisible 1×1 window used to satisfy the _NET_SUPPORTING_WM_CHECK
58 + /// EWMH convention.
59 + wm_check_window: xlib.Window,
60 +
61 + atoms: Atoms,
62 + cursors: Cursors,
63 + /// Cached numlock modifier mask, refreshed before any key/button grab.
64 + numlock_mask: c_uint,
65 +
66 + config: Config,
67 + /// Path to the config file that was loaded.
68 + /// Null if using default config.
69 + config_path: ?[]const u8,
70 +
71 + /// Head of the linked list of all managed monitors.
72 + monitors: ?*Monitor,
73 + /// The monitor that currently has input focus.
74 + selected_monitor: ?*Monitor,
75 +
76 + /// Head of the linked list of status bars (one per monitor).
77 + bars: ?*Bar,
78 +
79 + chord: ChordState,
80 +
81 + overlay: ?*overlay_mod.KeybindOverlay,
82 +
83 + scroll_animation: animations.ScrollAnimation,
84 + animation_config: animations.AnimationConfig,
85 +
86 + running: bool,
87 + last_motion_monitor: ?*Monitor,
88 +
89 + /// Initialises the window manager
90 + ///
91 + /// Returns an error if the display cannot be opened or another WM is
92 + /// already running.
93 + pub fn init(allocator: mem.Allocator, config: Config, config_path: ?[]const u8) !WindowManager {
94 + var display = try Display.open();
95 + errdefer display.close();
96 +
97 + try display.becomeWindowManager();
98 +
99 + const x11_fd = xlib.XConnectionNumber(display.handle);
100 +
101 + const atoms_result = Atoms.init(display.handle, display.root);
102 + const cursors = Cursors.init(&display);
103 + _ = xlib.XDefineCursor(display.handle, display.root, cursors.normal);
104 +
105 + tiling.setDisplay(display.handle);
106 + tiling.setScreenSize(display.screenWidth(), display.screenHeight());
107 +
108 + var wm = WindowManager{
109 + .allocator = allocator,
110 + .display = display,
111 + .x11_fd = x11_fd,
112 + .wm_check_window = atoms_result.check_window,
113 + .atoms = atoms_result.atoms,
114 + .cursors = cursors,
115 + .numlock_mask = 0,
116 + .config = config,
117 + .config_path = config_path,
118 + .monitors = null,
119 + .selected_monitor = null,
120 + .bars = null,
121 + .chord = .{},
122 + .overlay = null,
123 + .scroll_animation = .{},
124 + .animation_config = .{ .duration_ms = 150, .easing = .ease_out },
125 + .running = true,
126 + .last_motion_monitor = null,
127 + };
128 +
129 + wm.setupMonitors();
130 + wm.setupBars();
131 + wm.setupOverlay();
132 +
133 + return wm;
134 + }
135 +
136 + /// Release all allocated memory owned by the WM.
137 + pub fn deinit(self: *WindowManager) void {
138 + bar_mod.destroyBars(self.bars, self.display.handle);
139 + self.bars = null;
140 +
141 + if (self.overlay) |o| {
142 + o.deinit(self.allocator);
143 + self.overlay = null;
144 + }
145 +
146 + var mon = self.monitors;
147 + while (mon) |m| {
148 + const next = m.next;
149 + monitor_mod.destroy(self.allocator, m);
150 + mon = next;
151 + }
152 + self.monitors = null;
153 + self.selected_monitor = null;
154 +
155 + _ = xlib.XDestroyWindow(self.display.handle, self.wm_check_window);
156 +
157 + self.display.close();
158 + self.config.deinit();
159 + }
160 +
161 + fn setupMonitors(self: *WindowManager) void {
162 + if (xlib.XineramaIsActive(self.display.handle) != 0) {
163 + var screen_count: c_int = 0;
164 + const screens = xlib.XineramaQueryScreens(self.display.handle, &screen_count);
165 +
166 + if (screen_count > 0 and screens != null) {
167 + var prev_monitor: ?*Monitor = null;
168 + var index: usize = 0;
169 +
170 + while (index < @as(usize, @intCast(screen_count))) : (index += 1) {
171 + const screen = screens[index];
172 + const mon = monitor_mod.create(self.allocator) orelse continue;
173 +
174 + mon.num = @intCast(index);
175 + mon.mon_x = screen.x_org;
176 + mon.mon_y = screen.y_org;
177 + mon.mon_w = screen.width;
178 + mon.mon_h = screen.height;
179 + mon.win_x = screen.x_org;
180 + mon.win_y = screen.y_org;
181 + mon.win_w = screen.width;
182 + mon.win_h = screen.height;
183 + mon.lt[0] = &tiling.layout;
184 + mon.lt[1] = &monocle.layout;
185 + mon.lt[2] = &floating.layout;
186 + mon.lt[3] = &scrolling.layout;
187 + mon.lt[4] = &grid.layout;
188 +
189 + for (0..10) |i| {
190 + mon.pertag.ltidxs[i][0] = mon.lt[0];
191 + mon.pertag.ltidxs[i][1] = mon.lt[1];
192 + mon.pertag.ltidxs[i][2] = mon.lt[2];
193 + mon.pertag.ltidxs[i][3] = mon.lt[3];
194 + mon.pertag.ltidxs[i][4] = mon.lt[4];
195 + }
196 +
197 + self.initMonitorGaps(mon);
198 +
199 + if (prev_monitor) |prev| {
200 + prev.next = mon;
201 + } else {
202 + self.monitors = mon;
203 + self.selected_monitor = mon;
204 + }
205 + prev_monitor = mon;
206 + }
207 +
208 + _ = xlib.XFree(@ptrCast(screens));
209 + }
210 + }
211 +
212 + // Fallback: single monitor covering the full screen.
213 + if (self.monitors == null) {
214 + const mon = monitor_mod.create(self.allocator) orelse return;
215 + mon.num = 0;
216 + mon.mon_x = 0;
217 + mon.mon_y = 0;
218 + mon.mon_w = self.display.screenWidth();
219 + mon.mon_h = self.display.screenHeight();
220 + mon.win_x = 0;
221 + mon.win_y = 0;
222 + mon.win_w = mon.mon_w;
223 + mon.win_h = mon.mon_h;
224 + mon.lt[0] = &tiling.layout;
225 + mon.lt[1] = &monocle.layout;
226 + mon.lt[2] = &floating.layout;
227 + mon.lt[3] = &scrolling.layout;
228 + mon.lt[4] = &grid.layout;
229 +
230 + for (0..10) |i| {
231 + mon.pertag.ltidxs[i][0] = mon.lt[0];
232 + mon.pertag.ltidxs[i][1] = mon.lt[1];
233 + mon.pertag.ltidxs[i][2] = mon.lt[2];
234 + mon.pertag.ltidxs[i][3] = mon.lt[3];
235 + mon.pertag.ltidxs[i][4] = mon.lt[4];
236 + }
237 +
238 + self.initMonitorGaps(mon);
239 + self.monitors = mon;
240 + self.selected_monitor = mon;
241 + }
242 + }
243 +
244 + fn initMonitorGaps(self: *WindowManager, mon: *Monitor) void {
245 + const cfg = &self.config;
246 + const any_gap_nonzero = cfg.gap_inner_h != 0 or cfg.gap_inner_v != 0 or
247 + cfg.gap_outer_h != 0 or cfg.gap_outer_v != 0;
248 +
249 + if (cfg.gaps_enabled and any_gap_nonzero) {
250 + mon.gap_inner_h = cfg.gap_inner_h;
251 + mon.gap_inner_v = cfg.gap_inner_v;
252 + mon.gap_outer_h = cfg.gap_outer_h;
253 + mon.gap_outer_v = cfg.gap_outer_v;
254 + } else {
255 + mon.gap_inner_h = 0;
256 + mon.gap_inner_v = 0;
257 + mon.gap_outer_h = 0;
258 + mon.gap_outer_v = 0;
259 + }
260 + }
261 +
262 + pub fn setupBars(self: *WindowManager) void {
263 + var current_monitor = self.monitors;
264 + var last_bar: ?*Bar = null;
265 +
266 + while (current_monitor) |monitor| {
267 + const bar = Bar.create(
268 + self.allocator,
269 + self.display.handle,
270 + self.display.screen,
271 + monitor,
272 + self.config,
273 + ) orelse {
274 + current_monitor = monitor.next;
275 + continue;
276 + };
277 +
278 + if (tiling.bar_height == 0) {
279 + tiling.setBarHeight(bar.height);
280 + }
281 +
282 + self.populateBarBlocks(bar);
283 +
284 + if (last_bar) |prev| {
285 + prev.next = bar;
286 + } else {
287 + self.bars = bar;
288 + }
289 + last_bar = bar;
290 +
291 + std.debug.print("bar created for monitor {d}\n", .{monitor.num});
292 + current_monitor = monitor.next;
293 + }
294 + }
295 +
296 + pub fn populateBarBlocks(self: *WindowManager, bar: *Bar) void {
297 + if (self.config.blocks.items.len > 0) {
298 + for (self.config.blocks.items) |cfg_block| {
299 + bar.addBlock(configBlockToBarBlock(cfg_block));
300 + }
301 + } else {
302 + bar.addBlock(blocks_mod.Block.initRam("", 5, 0x7aa2f7, true));
303 + bar.addBlock(blocks_mod.Block.initStatic(" | ", 0x666666, false));
304 + bar.addBlock(blocks_mod.Block.initDatetime("", "%H:%M", 1, 0x0db9d7, true));
305 + }
306 + }
307 +
308 + fn setupOverlay(self: *WindowManager) void {
309 + self.overlay = overlay_mod.KeybindOverlay.init(
310 + self.display.handle,
311 + self.display.screen,
312 + self.display.root,
313 + self.config.font,
314 + self.allocator,
315 + );
316 + }
317 +
318 + // Wrap free functions in `bar_mod` with `self.bars` passed in.
319 +
320 + pub fn invalidateBars(self: *WindowManager) void {
321 + bar_mod.invalidateBars(self.bars);
322 + }
323 +
324 + pub fn windowToBar(self: *WindowManager, win: xlib.Window) ?*Bar {
325 + return bar_mod.windowToBar(self.bars, win);
326 + }
327 +
328 + /// Refreshes the cached numlock modifier bitmask from the X server's
329 + /// current modifier map. Called before any key or button grab so that
330 + /// grabs cover all numlock combinations.
331 + pub fn updateNumlockMask(self: *WindowManager) void {
332 + self.numlock_mask = 0;
333 + const modmap = xlib.XGetModifierMapping(self.display.handle);
334 + if (modmap == null) return;
335 + defer _ = xlib.XFreeModifiermap(modmap);
336 +
337 + const numlock_keycode = xlib.XKeysymToKeycode(self.display.handle, xlib.XK_Num_Lock);
338 +
339 + var modifier_index: usize = 0;
340 + while (modifier_index < 8) : (modifier_index += 1) {
341 + var key_index: usize = 0;
342 + while (key_index < @as(usize, @intCast(modmap.*.max_keypermod))) : (key_index += 1) {
343 + const keycode = modmap.*.modifiermap[modifier_index * @as(usize, @intCast(modmap.*.max_keypermod)) + key_index];
344 + if (keycode == numlock_keycode) {
345 + self.numlock_mask = @as(c_uint, 1) << @intCast(modifier_index);
346 + }
347 + }
348 + }
349 + }
350 +
351 + /// Grabs all configured keybinds and mouse buttons from the X server.
352 + /// Replaces any existing grabs. Call after config load or reload.
353 + pub fn grabKeybinds(self: *WindowManager) void {
354 + self.updateNumlockMask();
355 + const modifiers = [_]c_uint{ 0, xlib.LockMask, self.numlock_mask, self.numlock_mask | xlib.LockMask };
356 +
357 + _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root);
358 +
359 + for (self.config.keybinds.items) |keybind| {
360 + if (keybind.key_count == 0) continue;
361 + const first_key = keybind.keys[0];
362 + const keycode = xlib.XKeysymToKeycode(self.display.handle, @intCast(first_key.keysym));
363 + if (keycode != 0) {
364 + for (modifiers) |modifier| {
365 + _ = xlib.XGrabKey(
366 + self.display.handle,
367 + keycode,
368 + first_key.mod_mask | modifier,
369 + self.display.root,
370 + xlib.True,
371 + xlib.GrabModeAsync,
372 + xlib.GrabModeAsync,
373 + );
374 + }
375 + }
376 + }
377 +
378 + for (self.config.buttons.items) |button| {
379 + if (button.click == .client_win) {
380 + for (modifiers) |modifier| {
381 + _ = xlib.XGrabButton(
382 + self.display.handle,
383 + @intCast(button.button),
384 + button.mod_mask | modifier,
385 + self.display.root,
386 + xlib.True,
387 + xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
388 + xlib.GrabModeAsync,
389 + xlib.GrabModeAsync,
390 + xlib.None,
391 + xlib.None,
392 + );
393 + }
394 + }
395 + }
396 +
397 + std.debug.print("grabbed {d} keybinds from config\n", .{self.config.keybinds.items.len});
398 + }
399 +
400 + pub fn ungrabKeybinds(self: *WindowManager) void {
401 + _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root);
402 + }
403 +
404 + /// Walk the existing window tree and call `manageFn` for each window
405 + /// that should be managed. Non-transient windows are processed first
406 + /// so transient (dialog) windows can be stacked on top.
407 + pub fn scanExistingWindows(self: *WindowManager, manageFn: fn (xlib.Window, *xlib.XWindowAttributes, *WindowManager) void) void {
408 + var root_return: xlib.Window = undefined;
409 + var parent_return: xlib.Window = undefined;
410 + var children: [*c]xlib.Window = undefined;
411 + var num_children: c_uint = undefined;
412 +
413 + if (xlib.XQueryTree(self.display.handle, self.display.root, &root_return, &parent_return, &children, &num_children) == 0) {
414 + return;
415 + }
416 +
417 + var index: c_uint = 0;
418 + while (index < num_children) : (index += 1) {
419 + var window_attrs: xlib.XWindowAttributes = undefined;
420 + if (xlib.XGetWindowAttributes(self.display.handle, children[index], &window_attrs) == 0) {
421 + continue;
422 + }
423 + if (window_attrs.override_redirect != 0) continue;
424 +
425 + var trans: xlib.Window = 0;
426 + if (xlib.XGetTransientForHint(self.display.handle, children[index], &trans) != 0) continue;
427 +
428 + if (window_attrs.map_state == IsViewable or self.getState(children[index]) == IconicState) {
429 + manageFn(children[index], &window_attrs, self);
430 + }
431 + }
432 +
433 + index = 0;
434 + while (index < num_children) : (index += 1) {
435 + var window_attrs: xlib.XWindowAttributes = undefined;
436 + if (xlib.XGetWindowAttributes(self.display.handle, children[index], &window_attrs) == 0) {
437 + continue;
438 + }
439 + var trans: xlib.Window = 0;
440 + if (xlib.XGetTransientForHint(self.display.handle, children[index], &trans) != 0) {
441 + if (window_attrs.map_state == IsViewable or self.getState(children[index]) == IconicState) {
442 + manageFn(children[index], &window_attrs, self);
443 + }
444 + }
445 + }
446 +
447 + if (children != null) _ = xlib.XFree(@ptrCast(children));
448 + }
449 +
450 + /// Reads the ICCCM WM_STATE property for a window.
451 + /// Returns WithdrawnState if the property is absent or malformed.
452 + pub fn getState(self: *WindowManager, window: xlib.Window) c_long {
453 + var actual_type: xlib.Atom = 0;
454 + var actual_format: c_int = 0;
455 + var num_items: c_ulong = 0;
456 + var bytes_after: c_ulong = 0;
457 + var prop: [*c]u8 = null;
458 +
459 + const result = xlib.XGetWindowProperty(
460 + self.display.handle,
461 + window,
462 + self.atoms.wm_state,
463 + 0,
464 + 2,
465 + xlib.False,
466 + self.atoms.wm_state,
467 + &actual_type,
468 + &actual_format,
469 + &num_items,
470 + &bytes_after,
471 + &prop,
472 + );
473 +
474 + if (result != 0 or actual_type != self.atoms.wm_state or num_items < 1) {
475 + if (prop != null) _ = xlib.XFree(prop);
476 + return WithdrawnState;
477 + }
478 +
479 + const state: c_long = @as(*c_long, @ptrCast(@alignCast(prop))).*;
480 + _ = xlib.XFree(prop);
481 + return state;
482 + }
483 +
484 + /// Run the event loop until `self.running` is set to false.
485 + ///
486 + /// `eventFn` dispatches a single XEvent
487 + /// `tickFn` is called once per loop iteration to advance animations
488 + pub fn run(
489 + self: *WindowManager,
490 + eventFn: fn (*xlib.XEvent, *WindowManager) void,
491 + tickFn: fn (*WindowManager) void,
492 + ) void {
493 + var fds = [_]std.posix.pollfd{
494 + .{ .fd = self.x11_fd, .events = std.posix.POLL.IN, .revents = 0 },
495 + };
496 +
497 + _ = xlib.XSync(self.display.handle, xlib.False);
498 +
499 + while (self.running) {
500 + while (xlib.XPending(self.display.handle) > 0) {
501 + var event = self.display.nextEvent();
502 + eventFn(&event, self);
503 + }
504 +
505 + tickFn(self);
506 +
507 + var current_bar = self.bars;
508 + while (current_bar) |bar| {
509 + bar.updateBlocks();
510 + bar.draw(self.display.handle, self.config);
511 + current_bar = bar.next;
512 + }
513 +
514 + const poll_timeout: i32 = if (self.scroll_animation.isActive()) 16 else 1000;
515 + _ = std.posix.poll(&fds, poll_timeout) catch 0;
516 + }
517 + }
518 +
519 + /// Reloads the configuration
520 + ///
521 + /// `loadFn` repopulates `self.config`
522 + /// keys/buttons are unbound before reload and rebound after
523 + pub fn reloadConfig(
524 + self: *WindowManager,
525 + loadFn: fn (*WindowManager) void,
526 + ) void {
527 + std.debug.print("reloading config...\n", .{});
528 +
529 + self.ungrabKeybinds();
530 +
531 + self.config.keybinds.clearRetainingCapacity();
532 + self.config.buttons.clearRetainingCapacity();
533 + self.config.rules.clearRetainingCapacity();
534 + self.config.blocks.clearRetainingCapacity();
535 +
536 + loadFn(self);
537 +
538 + bar_mod.destroyBars(self.bars, self.display.handle);
539 + self.bars = null;
540 + self.setupBars();
541 + self.rebuildBarBlocks();
542 +
543 + self.grabKeybinds();
544 + }
545 +
546 + fn rebuildBarBlocks(self: *WindowManager) void {
547 + var current_bar = self.bars;
548 + while (current_bar) |bar| {
549 + bar.clearBlocks();
550 + self.populateBarBlocks(bar);
551 + current_bar = bar.next;
552 + }
553 + }
554 +};
555 +
556 +/// Converts a config block description into a live status bar block.
557 +pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block {
558 + return switch (cfg.block_type) {
559 + .static => blocks_mod.Block.initStatic(cfg.format, cfg.color, cfg.underline),
560 + .datetime => blocks_mod.Block.initDatetime(
561 + cfg.format,
562 + cfg.datetime_format orelse "%H:%M",
563 + cfg.interval,
564 + cfg.color,
565 + cfg.underline,
566 + ),
567 + .ram => blocks_mod.Block.initRam(cfg.format, cfg.interval, cfg.color, cfg.underline),
568 + .shell => blocks_mod.Block.initShell(
569 + cfg.format,
570 + cfg.command orelse "",
571 + cfg.interval,
572 + cfg.color,
573 + cfg.underline,
574 + ),
575 + .battery => blocks_mod.Block.initBattery(
576 + cfg.format_charging orelse "",
577 + cfg.format_discharging orelse "",
578 + cfg.format_full orelse "",
579 + cfg.battery_name orelse "BAT0",
580 + cfg.interval,
581 + cfg.color,
582 + cfg.underline,
583 + ),
584 + .cpu_temp => blocks_mod.Block.initCpuTemp(
585 + cfg.format,
586 + cfg.thermal_zone orelse "thermal_zone0",
587 + cfg.interval,
588 + cfg.color,
589 + cfg.underline,
590 + ),
591 + };
592 +}
+81 −0 src/x11/atoms.zig new file mode 100644
@@ -0,0 +1,81 @@
1 +const std = @import("std");
2 +const xlib = @import("xlib.zig");
3 +
4 +pub const Atoms = struct {
5 + // ICCCM atoms
6 + wm_protocols: xlib.Atom,
7 + wm_delete: xlib.Atom,
8 + wm_state: xlib.Atom,
9 + wm_take_focus: xlib.Atom,
10 +
11 + // EWHM atoms
12 + net_supported: xlib.Atom,
13 + net_wm_name: xlib.Atom,
14 + net_wm_state: xlib.Atom,
15 + net_wm_check: xlib.Atom,
16 + net_wm_state_fullscreen: xlib.Atom,
17 + net_active_window: xlib.Atom,
18 + net_wm_window_type: xlib.Atom,
19 + net_wm_window_type_dialog: xlib.Atom,
20 + net_client_list: xlib.Atom,
21 +
22 + /// Intern all atoms and set up the EWMH check window on `root`.
23 + ///
24 + /// `check_window` is a 1×1 invisible window created solely to satisfy
25 + /// the _NET_SUPPORTING_WM_CHECK convention. The caller owns it and
26 + /// should destroy it on shutdown via `XDestroyWindow`.
27 + pub fn init(display: *xlib.Display, root: xlib.Window) struct {
28 + atoms: Atoms,
29 + check_window: xlib.Window,
30 + } {
31 + const intern = struct {
32 + fn call(dpy: *xlib.Display, name: [*:0]const u8) xlib.Atom {
33 + return xlib.XInternAtom(dpy, name, xlib.False);
34 + }
35 + }.call;
36 +
37 + const atoms = Atoms{
38 + .wm_protocols = intern(display, "WM_PROTOCOLS"),
39 + .wm_delete = intern(display, "WM_DELETE_WINDOW"),
40 + .wm_state = intern(display, "WM_STATE"),
41 + .wm_take_focus = intern(display, "WM_TAKE_FOCUS"),
42 +
43 + .net_supported = intern(display, "_NET_SUPPORTED"),
44 + .net_wm_name = intern(display, "_NET_WM_NAME"),
45 + .net_wm_state = intern(display, "_NET_WM_STATE"),
46 + .net_wm_check = intern(display, "_NET_SUPPORTING_WM_CHECK"),
47 + .net_wm_state_fullscreen = intern(display, "_NET_WM_STATE_FULLSCREEN"),
48 + .net_active_window = intern(display, "_NET_ACTIVE_WINDOW"),
49 + .net_wm_window_type = intern(display, "_NET_WM_WINDOW_TYPE"),
50 + .net_wm_window_type_dialog = intern(display, "_NET_WM_WINDOW_TYPE_DIALOG"),
51 + .net_client_list = intern(display, "_NET_CLIENT_LIST"),
52 + };
53 +
54 + const utf8_string = intern(display, "UTF8_STRING");
55 +
56 + // Create the EWMH check window.
57 + var check_win = xlib.XCreateSimpleWindow(display, root, 0, 0, 1, 1, 0, 0, 0);
58 +
59 + _ = xlib.XChangeProperty(display, check_win, atoms.net_wm_check, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&check_win), 1);
60 + _ = xlib.XChangeProperty(display, check_win, atoms.net_wm_name, utf8_string, 8, xlib.PropModeReplace, "oxwm", 4);
61 + _ = xlib.XChangeProperty(display, root, atoms.net_wm_check, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&check_win), 1);
62 +
63 + // Advertise supported EWMH hints on the root window.
64 + var net_atoms = [_]xlib.Atom{
65 + atoms.net_supported,
66 + atoms.net_wm_name,
67 + atoms.net_wm_state,
68 + atoms.net_wm_check,
69 + atoms.net_wm_state_fullscreen,
70 + atoms.net_active_window,
71 + atoms.net_wm_window_type,
72 + atoms.net_wm_window_type_dialog,
73 + atoms.net_client_list,
74 + };
75 + _ = xlib.XChangeProperty(display, root, atoms.net_supported, xlib.XA_ATOM, 32, xlib.PropModeReplace, @ptrCast(&net_atoms), net_atoms.len);
76 +
77 + _ = xlib.XDeleteProperty(display, root, atoms.net_client_list);
78 +
79 + return .{ .atoms = atoms, .check_window = check_win };
80 + }
81 +};
+14 −14 src/x11/display.zig
@@ -2,8 +2,8 @@ const std = @import("std");
2 2 const xlib = @import("xlib.zig");
3 3
4 4 pub const DisplayError = error{
5 - cannot_open_display,
6 - another_wm_running,
5 + CannotOpenDisplay,
6 + AnotherWmRunning,
7 7 };
8 8
9 9 var wm_detected: bool = false;
@@ -14,7 +14,7 @@ pub const Display = struct {
14 14 root: xlib.Window,
15 15
16 16 pub fn open() DisplayError!Display {
17 - const handle = xlib.XOpenDisplay(null) orelse return DisplayError.cannot_open_display;
17 + const handle = xlib.XOpenDisplay(null) orelse return DisplayError.CannotOpenDisplay;
18 18 const screen = xlib.XDefaultScreen(handle);
19 19 const root = xlib.XRootWindow(handle, screen);
20 20
@@ -29,9 +29,9 @@ pub const Display = struct {
29 29 _ = xlib.XCloseDisplay(self.handle);
30 30 }
31 31
32 - pub fn become_window_manager(self: *Display) DisplayError!void {
32 + pub fn becomeWindowManager(self: *Display) DisplayError!void {
33 33 wm_detected = false;
34 - _ = xlib.XSetErrorHandler(on_wm_detected);
34 + _ = xlib.XSetErrorHandler(onWmDetected);
35 35 _ = xlib.XSelectInput(
36 36 self.handle,
37 37 self.root,
@@ -40,21 +40,21 @@ pub const Display = struct {
40 40 _ = xlib.XSync(self.handle, xlib.False);
41 41
42 42 if (wm_detected) {
43 - return DisplayError.another_wm_running;
43 + return DisplayError.AnotherWmRunning;
44 44 }
45 45
46 - _ = xlib.XSetErrorHandler(on_x_error);
46 + _ = xlib.XSetErrorHandler(onXError);
47 47 }
48 48
49 - pub fn screen_width(self: *Display) c_int {
49 + pub fn screenWidth(self: *Display) c_int {
50 50 return xlib.XDisplayWidth(self.handle, self.screen);
51 51 }
52 52
53 - pub fn screen_height(self: *Display) c_int {
53 + pub fn screenHeight(self: *Display) c_int {
54 54 return xlib.XDisplayHeight(self.handle, self.screen);
55 55 }
56 56
57 - pub fn next_event(self: *Display) xlib.XEvent {
57 + pub fn nextEvent(self: *Display) xlib.XEvent {
58 58 var event: xlib.XEvent = undefined;
59 59 _ = xlib.XNextEvent(self.handle, &event);
60 60 return event;
@@ -68,7 +68,7 @@ pub const Display = struct {
68 68 _ = xlib.XSync(self.handle, if (discard) xlib.True else xlib.False);
69 69 }
70 70
71 - pub fn grab_key(
71 + pub fn grabKey(
72 72 self: *Display,
73 73 keycode: c_int,
74 74 modifiers: c_uint,
@@ -84,17 +84,17 @@ pub const Display = struct {
84 84 );
85 85 }
86 86
87 - pub fn keysym_to_keycode(self: *Display, keysym: xlib.KeySym) c_int {
87 + pub fn keysymToKeycode(self: *Display, keysym: xlib.KeySym) c_int {
88 88 return @intCast(xlib.XKeysymToKeycode(self.handle, keysym));
89 89 }
90 90 };
91 91
92 -fn on_wm_detected(_: ?*xlib.Display, _: [*c]xlib.XErrorEvent) callconv(.c) c_int {
92 +fn onWmDetected(_: ?*xlib.Display, _: [*c]xlib.XErrorEvent) callconv(.c) c_int {
93 93 wm_detected = true;
94 94 return 0;
95 95 }
96 96
97 -fn on_x_error(_: ?*xlib.Display, event: [*c]xlib.XErrorEvent) callconv(.c) c_int {
97 +fn onXError(_: ?*xlib.Display, event: [*c]xlib.XErrorEvent) callconv(.c) c_int {
98 98 std.debug.print("x11 error: request={d} error={d}\n", .{ event.*.request_code, event.*.error_code });
99 99 return 0;
100 100 }
+5 −5 src/x11/events.zig Large diff collapsed, click to expand
@@ -39,21 +39,21 @@ pub const EventType = enum(c_int) {
39 39 _,
40 40 };
41 41
42 -pub fn get_event_type(event: *const xlib.XEvent) EventType {
42 +pub fn getEventType(event: *const xlib.XEvent) EventType {
43 43 return @enumFromInt(event.type);
44 44 }
45 45
46 -pub fn event_name(event_type: EventType) []const u8 {
46 +pub fn eventName(event_type: EventType) []const u8 {
47 47 if (@intFromEnum(event_type) > @intFromEnum(EventType.generic_event)) return "unknown";
48 48
49 49 return @tagName(event_type);
50 50 }
51 51
52 -test event_name {
52 +test eventName {
53 53 const testing = std.testing;
54 54
55 - const name = event_name(.key_press);
55 + const name = eventName(.key_press);
56 56 try testing.expectEqualStrings("key_press", name);
57 57
58 - try testing.expectEqualStrings("unknown", event_name(@enumFromInt(100)));
58 + try testing.expectEqualStrings("unknown", eventName(@enumFromInt(100)));
59 59 }
+1 −1 tests/lua_config_tests.zig Large diff collapsed, click to expand
@@ -13,6 +13,6 @@ test "test-config.lua loads without errors" {
13 13 }
14 14 defer lua.deinit();
15 15
16 - const loaded = lua.load_file("resources/test-config.lua");
16 + const loaded = lua.loadFile("resources/test-config.lua");
17 17 try testing.expect(loaded);
18 18 }