tony / oxwm

Branch: 8a8d303
Switch branches/tags
master
emzywastaken refactor(atoms): create `Atoms` type to store intern atoms and remove globals from main 8a8d303 2026-02-20
2539 lines · 88.6 KB Raw Blame History
1 const std = @import("std");
2 const VERSION = "v0.11.2";
3 const Atoms = @import("x11/atoms.zig").Atoms;
4 const kchord = @import("keyboard/chord.zig");
5 const display_mod = @import("x11/display.zig");
6 const events = @import("x11/events.zig");
7 const xlib = @import("x11/xlib.zig");
8 const client_mod = @import("client.zig");
9 const monitor_mod = @import("monitor.zig");
10 const tiling = @import("layouts/tiling.zig");
11 const monocle = @import("layouts/monocle.zig");
12 const floating = @import("layouts/floating.zig");
13 const scrolling = @import("layouts/scrolling.zig");
14 const grid = @import("layouts/grid.zig");
15 const animations = @import("animations.zig");
16 const bar_mod = @import("bar/bar.zig");
17 const blocks_mod = @import("bar/blocks/blocks.zig");
18 const config_mod = @import("config/config.zig");
19 const lua = @import("config/lua.zig");
20 const overlay_mod = @import("overlay.zig");
21
22 const Display = display_mod.Display;
23 const Client = client_mod.Client;
24 const Monitor = monitor_mod.Monitor;
25
26 var running: bool = true;
27 var gpa = std.heap.GeneralPurposeAllocator(.{}){};
28
29 var atoms: Atoms = undefined;
30 var wm_check_window: xlib.Window = undefined;
31
32 var border_color_focused: c_ulong = 0x6dade3;
33 var border_color_unfocused: c_ulong = 0x444444;
34 var border_width: i32 = 2;
35 var gap_outer_v: i32 = 5;
36 var gap_outer_h: i32 = 5;
37 var gap_inner_h: i32 = 5;
38 var gap_inner_v: i32 = 5;
39
40 var tags: [9][]const u8 = .{ "1", "2", "3", "4", "5", "6", "7", "8", "9" };
41
42 var cursor_normal: xlib.Cursor = 0;
43 var cursor_resize: xlib.Cursor = 0;
44 var cursor_move: xlib.Cursor = 0;
45
46 const NormalState: c_long = 1;
47 const WithdrawnState: c_long = 0;
48 const IconicState: c_long = 3;
49 const IsViewable: c_int = 2;
50 const snap_distance: i32 = 32;
51
52 var numlock_mask: c_uint = 0;
53
54 var config: config_mod.Config = undefined;
55 var display_global: ?*Display = null;
56 var config_path_global: ?[]const u8 = null;
57
58 var scroll_animation: animations.Scroll_Animation = .{};
59 var animation_config: animations.Animation_Config = .{ .duration_ms = 150, .easing = .ease_out };
60
61 var chord = kchord.ChordState{};
62
63 var keybind_overlay: ?*overlay_mod.Keybind_Overlay = null;
64
65 fn print_help() void {
66 std.debug.print(
67 \\oxwm - A window manager
68 \\
69 \\USAGE:
70 \\ oxwm [OPTIONS]
71 \\
72 \\OPTIONS:
73 \\ --init Create default config in ~/.config/oxwm/config.lua
74 \\ --config <PATH> Use custom config file
75 \\ --validate Validate config file without starting window manager
76 \\ --version Print version information
77 \\ --help Print this help message
78 \\
79 \\CONFIG:
80 \\ Location: ~/.config/oxwm/config.lua
81 \\ Edit the config file and use Mod+Shift+R to reload
82 \\ No compilation needed - instant hot-reload!
83 \\
84 \\FIRST RUN:
85 \\ Run 'oxwm --init' to create a config file
86 \\ Or just start oxwm and it will create one automatically
87 \\
88 , .{});
89 }
90
91 fn get_config_path(allocator: std.mem.Allocator) ![]u8 {
92 const config_home = std.posix.getenv("XDG_CONFIG_HOME") orelse blk: {
93 const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir;
94 break :blk try std.fs.path.join(allocator, &.{ home, ".config" });
95 };
96 defer if (std.posix.getenv("XDG_CONFIG_HOME") == null) allocator.free(config_home);
97
98 const config_path = try std.fs.path.join(allocator, &.{ config_home, "oxwm", "config.lua" });
99 return config_path;
100 }
101
102 fn init_config(allocator: std.mem.Allocator) void {
103 const config_path = get_config_path(allocator) catch return;
104 defer allocator.free(config_path);
105
106 const template = @embedFile("templates/config.lua");
107
108 if (std.fs.path.dirname(config_path)) |dir_path| {
109 var root = std.fs.openDirAbsolute("/", .{}) catch |err| {
110 std.debug.print("error: could not open root directory: {}\n", .{err});
111 return;
112 };
113 defer root.close();
114
115 const relative_path = std.mem.trimLeft(u8, dir_path, "/");
116 root.makePath(relative_path) catch |err| {
117 std.debug.print("error: could not create config directory: {}\n", .{err});
118 return;
119 };
120 }
121
122 const file = std.fs.createFileAbsolute(config_path, .{}) catch |err| {
123 std.debug.print("error: could not create config file: {}\n", .{err});
124 return;
125 };
126 defer file.close();
127
128 _ = file.writeAll(template) catch |err| {
129 std.debug.print("error: could not write config file: {}\n", .{err});
130 return;
131 };
132
133 std.debug.print("Config created at {s}\n", .{config_path});
134 std.debug.print("Edit the file and reload with Mod+Shift+R\n", .{});
135 std.debug.print("No compilation needed - changes take effect immediately!\n", .{});
136 }
137
138 fn validate_config(allocator: std.mem.Allocator, config_path: []const u8) !void {
139 config = config_mod.Config.init(allocator);
140 defer config.deinit();
141
142 if (!lua.init(&config)) {
143 std.debug.print("error: failed to initialize lua\n", .{});
144 std.process.exit(1);
145 }
146 defer lua.deinit();
147
148 _ = std.fs.cwd().statFile(config_path) catch |err| {
149 std.debug.print("error: config file not found: {s}\n", .{config_path});
150 std.debug.print(" {}\n", .{err});
151 std.process.exit(1);
152 };
153
154 if (lua.load_file(config_path)) {
155 std.debug.print("✓ config valid: {s}\n", .{config_path});
156 std.process.exit(0);
157 } else {
158 std.debug.print("✗ config validation failed\n", .{});
159 std.process.exit(1);
160 }
161 }
162
163 pub fn main() !void {
164 const allocator = gpa.allocator();
165 defer _ = gpa.deinit();
166
167 const default_config_path = try get_config_path(allocator);
168 defer allocator.free(default_config_path);
169
170 var config_path: []const u8 = default_config_path;
171 var validate_mode: bool = false;
172 var args = std.process.args();
173 _ = args.skip();
174 while (args.next()) |arg| {
175 if (std.mem.eql(u8, arg, "-c") or std.mem.eql(u8, arg, "--config")) {
176 if (args.next()) |path| config_path = path;
177 } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) {
178 print_help();
179 return;
180 } else if (std.mem.eql(u8, arg, "-v") or std.mem.eql(u8, arg, "--version")) {
181 std.debug.print("{s}\n", .{VERSION});
182 return;
183 } else if (std.mem.eql(u8, arg, "--init")) {
184 init_config(allocator);
185 return;
186 } else if (std.mem.eql(u8, arg, "--validate")) {
187 validate_mode = true;
188 }
189 }
190
191 if (validate_mode) {
192 try validate_config(allocator, config_path);
193 return;
194 }
195
196 std.debug.print("oxwm starting\n", .{});
197
198 config = config_mod.Config.init(allocator);
199 defer config.deinit();
200
201 if (lua.init(&config)) {
202 const loaded = if (std.fs.cwd().statFile(config_path)) |_|
203 lua.load_file(config_path)
204 else |_| blk: {
205 init_config(allocator);
206 break :blk lua.load_config();
207 };
208
209 if (loaded) {
210 config_path_global = config_path;
211 std.debug.print("loaded config from {s}\n", .{config_path});
212 apply_config_values();
213 } else {
214 std.debug.print("no config found, using defaults\n", .{});
215 initialize_default_config();
216 }
217 } else {
218 std.debug.print("failed to init lua, using defaults\n", .{});
219 initialize_default_config();
220 }
221
222 var display = Display.open() catch |err| {
223 std.debug.print("failed to open display: {}\n", .{err});
224 return;
225 };
226 defer display.close();
227
228 display_global = &display;
229
230 std.debug.print("display opened: screen={d} root=0x{x}\n", .{ display.screen, display.root });
231 std.debug.print("screen size: {d}x{d}\n", .{ display.screen_width(), display.screen_height() });
232
233 display.become_window_manager() catch |err| {
234 std.debug.print("failed to become window manager: {}\n", .{err});
235 return;
236 };
237
238 std.debug.print("successfully became window manager\n", .{});
239
240 const atoms_result = Atoms.init(display_global.?.handle, display_global.?.root);
241 atoms = atoms_result.atoms;
242 wm_check_window = atoms_result.check_window;
243 std.debug.print("atoms initialized with EWMH support\n", .{});
244
245 setup_cursors(&display);
246 client_mod.init(allocator);
247 monitor_mod.init(allocator);
248 monitor_mod.set_root_window(display.root, display.handle);
249 tiling.set_display(display.handle);
250 tiling.set_screen_size(display.screen_width(), display.screen_height());
251
252 setup_monitors(&display);
253 setup_bars(allocator, &display);
254 setup_overlay(allocator, &display);
255 grab_keybinds(&display);
256 scan_existing_windows(&display);
257
258 try run_autostart_commands(allocator, config.autostart.items);
259 std.debug.print("entering event loop\n", .{});
260 run_event_loop(&display);
261
262 bar_mod.destroy_bars(allocator, display.handle);
263
264 var mon = monitor_mod.monitors;
265 while (mon) |m| {
266 const next = m.next;
267 monitor_mod.destroy(m);
268 mon = next;
269 }
270
271 if (keybind_overlay) |overlay| {
272 overlay.deinit(allocator);
273 }
274
275 lua.deinit();
276 std.debug.print("oxwm exiting\n", .{});
277 }
278
279 fn setup_cursors(display: *Display) void {
280 cursor_normal = xlib.XCreateFontCursor(display.handle, xlib.XC_left_ptr);
281 cursor_resize = xlib.XCreateFontCursor(display.handle, xlib.XC_sizing);
282 cursor_move = xlib.XCreateFontCursor(display.handle, xlib.XC_fleur);
283 _ = xlib.XDefineCursor(display.handle, display.root, cursor_normal);
284 }
285
286 fn setup_bars(allocator: std.mem.Allocator, display: *Display) void {
287 var current_monitor = monitor_mod.monitors;
288 var last_bar: ?*bar_mod.Bar = null;
289
290 while (current_monitor) |monitor| {
291 const bar = bar_mod.Bar.create(allocator, display.handle, display.screen, monitor, config);
292 if (bar) |created_bar| {
293 if (tiling.bar_height == 0) {
294 tiling.set_bar_height(created_bar.height);
295 }
296
297 if (config.blocks.items.len > 0) {
298 for (config.blocks.items) |cfg_block| {
299 const block = config_block_to_bar_block(cfg_block);
300 created_bar.add_block(block);
301 }
302 } else {
303 created_bar.add_block(blocks_mod.Block.init_ram("", 5, 0x7aa2f7, true));
304 created_bar.add_block(blocks_mod.Block.init_static(" | ", 0x666666, false));
305 created_bar.add_block(blocks_mod.Block.init_datetime("", "%H:%M", 1, 0x0db9d7, true));
306 }
307
308 if (last_bar) |prev| {
309 prev.next = created_bar;
310 } else {
311 bar_mod.bars = created_bar;
312 }
313 last_bar = created_bar;
314 std.debug.print("bar created for monitor {d}\n", .{monitor.num});
315 }
316 current_monitor = monitor.next;
317 }
318 }
319
320 fn setup_overlay(allocator: std.mem.Allocator, display: *Display) void {
321 keybind_overlay = overlay_mod.Keybind_Overlay.init(display.handle, display.screen, display.root, config.font, allocator);
322 }
323
324 fn config_block_to_bar_block(cfg: config_mod.Block) blocks_mod.Block {
325 return switch (cfg.block_type) {
326 .static => blocks_mod.Block.init_static(cfg.format, cfg.color, cfg.underline),
327 .datetime => blocks_mod.Block.init_datetime(cfg.format, cfg.datetime_format orelse "%H:%M", cfg.interval, cfg.color, cfg.underline),
328 .ram => blocks_mod.Block.init_ram(cfg.format, cfg.interval, cfg.color, cfg.underline),
329 .shell => blocks_mod.Block.init_shell(cfg.format, cfg.command orelse "", cfg.interval, cfg.color, cfg.underline),
330 .battery => blocks_mod.Block.init_battery(
331 cfg.format_charging orelse "",
332 cfg.format_discharging orelse "",
333 cfg.format_full orelse "",
334 cfg.battery_name orelse "BAT0",
335 cfg.interval,
336 cfg.color,
337 cfg.underline,
338 ),
339 .cpu_temp => blocks_mod.Block.init_cpu_temp(
340 cfg.format,
341 cfg.thermal_zone orelse "thermal_zone0",
342 cfg.interval,
343 cfg.color,
344 cfg.underline,
345 ),
346 };
347 }
348
349 fn setup_monitors(display: *Display) void {
350 std.debug.print("checking xinerama...\n", .{});
351 if (xlib.XineramaIsActive(display.handle) != 0) {
352 std.debug.print("xinerama is active!\n", .{});
353 var screen_count: c_int = 0;
354 const screens = xlib.XineramaQueryScreens(display.handle, &screen_count);
355
356 if (screen_count > 0 and screens != null) {
357 var prev_monitor: ?*Monitor = null;
358 var index: usize = 0;
359
360 while (index < @as(usize, @intCast(screen_count))) : (index += 1) {
361 const screen = screens[index];
362 const mon = monitor_mod.create() orelse continue;
363
364 mon.num = @intCast(index);
365 mon.mon_x = screen.x_org;
366 mon.mon_y = screen.y_org;
367 mon.mon_w = screen.width;
368 mon.mon_h = screen.height;
369 mon.win_x = screen.x_org;
370 mon.win_y = screen.y_org;
371 mon.win_w = screen.width;
372 mon.win_h = screen.height;
373 mon.lt[0] = &tiling.layout;
374 mon.lt[1] = &monocle.layout;
375 mon.lt[2] = &floating.layout;
376 mon.lt[3] = &scrolling.layout;
377 mon.lt[4] = &grid.layout;
378 for (0..10) |i| {
379 mon.pertag.ltidxs[i][0] = mon.lt[0];
380 mon.pertag.ltidxs[i][1] = mon.lt[1];
381 mon.pertag.ltidxs[i][2] = mon.lt[2];
382 mon.pertag.ltidxs[i][3] = mon.lt[3];
383 mon.pertag.ltidxs[i][4] = mon.lt[4];
384 }
385
386 init_monitor_gaps(mon);
387
388 if (prev_monitor) |prev| {
389 prev.next = mon;
390 } else {
391 monitor_mod.monitors = mon;
392 }
393 prev_monitor = mon;
394
395 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 });
396 }
397
398 monitor_mod.selected_monitor = monitor_mod.monitors;
399 _ = xlib.XFree(@ptrCast(screens));
400 return;
401 }
402 } else {
403 std.debug.print("xinerama not active, using single monitor\n", .{});
404 }
405
406 const mon = monitor_mod.create() orelse return;
407 mon.mon_x = 0;
408 mon.mon_y = 0;
409 mon.mon_w = display.screen_width();
410 mon.mon_h = display.screen_height();
411 mon.win_x = 0;
412 mon.win_y = 0;
413 mon.win_w = display.screen_width();
414 mon.win_h = display.screen_height();
415 mon.lt[0] = &tiling.layout;
416 mon.lt[1] = &monocle.layout;
417 mon.lt[2] = &floating.layout;
418 mon.lt[3] = &scrolling.layout;
419 mon.lt[4] = &grid.layout;
420 for (0..10) |i| {
421 mon.pertag.ltidxs[i][0] = mon.lt[0];
422 mon.pertag.ltidxs[i][1] = mon.lt[1];
423 mon.pertag.ltidxs[i][2] = mon.lt[2];
424 mon.pertag.ltidxs[i][3] = mon.lt[3];
425 mon.pertag.ltidxs[i][4] = mon.lt[4];
426 }
427
428 init_monitor_gaps(mon);
429
430 monitor_mod.monitors = mon;
431 monitor_mod.selected_monitor = mon;
432 std.debug.print("monitor created: {d}x{d}\n", .{ mon.mon_w, mon.mon_h });
433 }
434
435 fn apply_config_values() void {
436 border_color_focused = config.border_focused;
437 border_color_unfocused = config.border_unfocused;
438 border_width = config.border_width;
439 gap_inner_h = config.gap_inner_h;
440 gap_inner_v = config.gap_inner_v;
441 gap_outer_h = config.gap_outer_h;
442 gap_outer_v = config.gap_outer_v;
443 tags = config.tags;
444 }
445
446 fn init_monitor_gaps(mon: *Monitor) void {
447 const any_gap_nonzero = config.gap_inner_h != 0 or config.gap_inner_v != 0 or
448 config.gap_outer_h != 0 or config.gap_outer_v != 0;
449
450 if (config.gaps_enabled and any_gap_nonzero) {
451 mon.gap_inner_h = config.gap_inner_h;
452 mon.gap_inner_v = config.gap_inner_v;
453 mon.gap_outer_h = config.gap_outer_h;
454 mon.gap_outer_v = config.gap_outer_v;
455 } else {
456 mon.gap_inner_h = 0;
457 mon.gap_inner_v = 0;
458 mon.gap_outer_h = 0;
459 mon.gap_outer_v = 0;
460 }
461 }
462
463 fn make_keybind(mod: u32, key: u64, action: config_mod.Action) config_mod.Keybind {
464 var kb: config_mod.Keybind = .{ .action = action };
465 kb.keys[0] = .{ .mod_mask = mod, .keysym = key };
466 kb.key_count = 1;
467 return kb;
468 }
469
470 fn make_keybind_int(mod: u32, key: u64, action: config_mod.Action, int_arg: i32) config_mod.Keybind {
471 var kb = make_keybind(mod, key, action);
472 kb.int_arg = int_arg;
473 return kb;
474 }
475
476 fn make_keybind_str(mod: u32, key: u64, action: config_mod.Action, str_arg: []const u8) config_mod.Keybind {
477 var kb = make_keybind(mod, key, action);
478 kb.str_arg = str_arg;
479 return kb;
480 }
481
482 fn initialize_default_config() void {
483 const mod_key: u32 = 1 << 6;
484 const shift_key: u32 = 1 << 0;
485 const control_key: u32 = 1 << 2;
486
487 config.add_keybind(make_keybind(mod_key, 0xff0d, .spawn_terminal)) catch {};
488 config.add_keybind(make_keybind_str(mod_key, 'd', .spawn, "rofi -show drun")) catch {};
489 config.add_keybind(make_keybind_str(mod_key, 's', .spawn, "maim -s | xclip -selection clipboard -t image/png")) catch {};
490 config.add_keybind(make_keybind(mod_key, 'q', .kill_client)) catch {};
491 config.add_keybind(make_keybind(mod_key | shift_key, 'q', .quit)) catch {};
492 config.add_keybind(make_keybind(mod_key | shift_key, 'r', .reload_config)) catch {};
493 config.add_keybind(make_keybind(mod_key, 'j', .focus_next)) catch {};
494 config.add_keybind(make_keybind(mod_key, 'k', .focus_prev)) catch {};
495 config.add_keybind(make_keybind(mod_key | shift_key, 'j', .move_next)) catch {};
496 config.add_keybind(make_keybind(mod_key | shift_key, 'k', .move_prev)) catch {};
497 config.add_keybind(make_keybind_int(mod_key, 'h', .resize_master, -50)) catch {};
498 config.add_keybind(make_keybind_int(mod_key, 'l', .resize_master, 50)) catch {};
499 config.add_keybind(make_keybind(mod_key, 'i', .inc_master)) catch {};
500 config.add_keybind(make_keybind(mod_key, 'p', .dec_master)) catch {};
501 config.add_keybind(make_keybind(mod_key, 'a', .toggle_gaps)) catch {};
502 config.add_keybind(make_keybind(mod_key, 'f', .toggle_fullscreen)) catch {};
503 config.add_keybind(make_keybind(mod_key, 0x0020, .toggle_floating)) catch {};
504 config.add_keybind(make_keybind(mod_key, 'n', .cycle_layout)) catch {};
505 config.add_keybind(make_keybind_int(mod_key, 0x002c, .focus_monitor, -1)) catch {};
506 config.add_keybind(make_keybind_int(mod_key, 0x002e, .focus_monitor, 1)) catch {};
507 config.add_keybind(make_keybind_int(mod_key | shift_key, 0x002c, .send_to_monitor, -1)) catch {};
508 config.add_keybind(make_keybind_int(mod_key | shift_key, 0x002e, .send_to_monitor, 1)) catch {};
509
510 var tag_index: i32 = 0;
511 while (tag_index < 9) : (tag_index += 1) {
512 const keysym: u64 = @as(u64, '1') + @as(u64, @intCast(tag_index));
513 config.add_keybind(make_keybind_int(mod_key, keysym, .view_tag, tag_index)) catch {};
514 config.add_keybind(make_keybind_int(mod_key | shift_key, keysym, .move_to_tag, tag_index)) catch {};
515 config.add_keybind(make_keybind_int(mod_key | control_key, keysym, .toggle_view_tag, tag_index)) catch {};
516 config.add_keybind(make_keybind_int(mod_key | control_key | shift_key, keysym, .toggle_tag, tag_index)) catch {};
517 }
518
519 config.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 1, .action = .move_mouse }) catch {};
520 config.add_button(.{ .click = .client_win, .mod_mask = mod_key, .button = 3, .action = .resize_mouse }) catch {};
521 }
522
523 fn grab_keybinds(display: *Display) void {
524 update_numlock_mask(display);
525 const modifiers = [_]c_uint{ 0, xlib.LockMask, numlock_mask, numlock_mask | xlib.LockMask };
526
527 _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root);
528
529 for (config.keybinds.items) |keybind| {
530 if (keybind.key_count == 0) continue;
531 const first_key = keybind.keys[0];
532 const keycode = xlib.XKeysymToKeycode(display.handle, @intCast(first_key.keysym));
533 if (keycode != 0) {
534 for (modifiers) |modifier| {
535 _ = xlib.XGrabKey(
536 display.handle,
537 keycode,
538 first_key.mod_mask | modifier,
539 display.root,
540 xlib.True,
541 xlib.GrabModeAsync,
542 xlib.GrabModeAsync,
543 );
544 }
545 }
546 }
547
548 for (config.buttons.items) |button| {
549 if (button.click == .client_win) {
550 for (modifiers) |modifier| {
551 _ = xlib.XGrabButton(
552 display.handle,
553 @intCast(button.button),
554 button.mod_mask | modifier,
555 display.root,
556 xlib.True,
557 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
558 xlib.GrabModeAsync,
559 xlib.GrabModeAsync,
560 xlib.None,
561 xlib.None,
562 );
563 }
564 }
565 }
566
567 std.debug.print("grabbed {d} keybinds from config\n", .{config.keybinds.items.len});
568 }
569
570 fn get_state(display: *Display, window: xlib.Window) c_long {
571 var actual_type: xlib.Atom = 0;
572 var actual_format: c_int = 0;
573 var num_items: c_ulong = 0;
574 var bytes_after: c_ulong = 0;
575 var prop: [*c]u8 = null;
576
577 const result = xlib.XGetWindowProperty(
578 display.handle,
579 window,
580 atoms.wm_state,
581 0,
582 2,
583 xlib.False,
584 atoms.wm_state,
585 &actual_type,
586 &actual_format,
587 &num_items,
588 &bytes_after,
589 &prop,
590 );
591
592 if (result != 0 or actual_type != atoms.wm_state or num_items < 1) {
593 if (prop != null) {
594 _ = xlib.XFree(prop);
595 }
596 return WithdrawnState;
597 }
598
599 const state: c_long = @as(*c_long, @ptrCast(@alignCast(prop))).*;
600 _ = xlib.XFree(prop);
601 return state;
602 }
603
604 fn scan_existing_windows(display: *Display) void {
605 var root_return: xlib.Window = undefined;
606 var parent_return: xlib.Window = undefined;
607 var children: [*c]xlib.Window = undefined;
608 var num_children: c_uint = undefined;
609
610 if (xlib.XQueryTree(display.handle, display.root, &root_return, &parent_return, &children, &num_children) == 0) {
611 return;
612 }
613
614 var index: c_uint = 0;
615 while (index < num_children) : (index += 1) {
616 var window_attrs: xlib.XWindowAttributes = undefined;
617 if (xlib.XGetWindowAttributes(display.handle, children[index], &window_attrs) == 0) {
618 continue;
619 }
620 if (window_attrs.override_redirect != 0) {
621 continue;
622 }
623 var trans: xlib.Window = 0;
624 if (xlib.XGetTransientForHint(display.handle, children[index], &trans) != 0) {
625 continue;
626 }
627 if (window_attrs.map_state == IsViewable or get_state(display, children[index]) == IconicState) {
628 manage(display, children[index], &window_attrs);
629 }
630 }
631
632 index = 0;
633 while (index < num_children) : (index += 1) {
634 var window_attrs: xlib.XWindowAttributes = undefined;
635 if (xlib.XGetWindowAttributes(display.handle, children[index], &window_attrs) == 0) {
636 continue;
637 }
638 var trans: xlib.Window = 0;
639 if (xlib.XGetTransientForHint(display.handle, children[index], &trans) != 0) {
640 if (window_attrs.map_state == IsViewable or get_state(display, children[index]) == IconicState) {
641 manage(display, children[index], &window_attrs);
642 }
643 }
644 }
645
646 if (children != null) {
647 _ = xlib.XFree(@ptrCast(children));
648 }
649 }
650
651 fn run_event_loop(display: *Display) void {
652 const x11_fd = xlib.XConnectionNumber(display.handle);
653 var fds = [_]std.posix.pollfd{
654 .{ .fd = x11_fd, .events = std.posix.POLL.IN, .revents = 0 },
655 };
656
657 _ = xlib.XSync(display.handle, xlib.False);
658
659 while (running) {
660 while (xlib.XPending(display.handle) > 0) {
661 var event = display.next_event();
662 handle_event(display, &event);
663 }
664
665 tick_animations();
666
667 var current_bar = bar_mod.bars;
668 while (current_bar) |bar| {
669 bar.update_blocks();
670 bar.draw(display.handle, &tags, config);
671 current_bar = bar.next;
672 }
673
674 const poll_timeout: i32 = if (scroll_animation.is_active()) 16 else 1000;
675 _ = std.posix.poll(&fds, poll_timeout) catch 0;
676 }
677 }
678
679 fn handle_event(display: *Display, event: *xlib.XEvent) void {
680 const event_type = events.get_event_type(event);
681
682 if (event_type == .button_press) {
683 std.debug.print("EVENT: button_press received type={d}\n", .{event.type});
684 }
685
686 switch (event_type) {
687 .map_request => handle_map_request(display, &event.xmaprequest),
688 .configure_request => handle_configure_request(display, &event.xconfigurerequest),
689 .key_press => handle_key_press(display, &event.xkey),
690 .destroy_notify => handle_destroy_notify(display, &event.xdestroywindow),
691 .unmap_notify => handle_unmap_notify(display, &event.xunmap),
692 .enter_notify => handle_enter_notify(display, &event.xcrossing),
693 .focus_in => handle_focus_in(display, &event.xfocus),
694 .motion_notify => handle_motion_notify(display, &event.xmotion),
695 .client_message => handle_client_message(display, &event.xclient),
696 .button_press => handle_button_press(display, &event.xbutton),
697 .expose => handle_expose(display, &event.xexpose),
698 .property_notify => handle_property_notify(display, &event.xproperty),
699 else => {},
700 }
701 }
702
703 fn handle_map_request(display: *Display, event: *xlib.XMapRequestEvent) void {
704 std.debug.print("map_request: window=0x{x}\n", .{event.window});
705
706 var window_attributes: xlib.XWindowAttributes = undefined;
707 if (xlib.XGetWindowAttributes(display.handle, event.window, &window_attributes) == 0) {
708 return;
709 }
710 if (window_attributes.override_redirect != 0) {
711 return;
712 }
713 if (client_mod.window_to_client(event.window) != null) {
714 return;
715 }
716
717 manage(display, event.window, &window_attributes);
718 }
719
720 fn manage(display: *Display, win: xlib.Window, window_attrs: *xlib.XWindowAttributes) void {
721 const client = client_mod.create(win) orelse return;
722 var trans: xlib.Window = 0;
723
724 client.x = window_attrs.x;
725 client.y = window_attrs.y;
726 client.width = window_attrs.width;
727 client.height = window_attrs.height;
728 client.old_x = window_attrs.x;
729 client.old_y = window_attrs.y;
730 client.old_width = window_attrs.width;
731 client.old_height = window_attrs.height;
732 client.old_border_width = window_attrs.border_width;
733 client.border_width = border_width;
734
735 update_title(display, client);
736
737 if (xlib.XGetTransientForHint(display.handle, win, &trans) != 0) {
738 if (client_mod.window_to_client(trans)) |transient_client| {
739 client.monitor = transient_client.monitor;
740 client.tags = transient_client.tags;
741 }
742 }
743
744 if (client.monitor == null) {
745 client.monitor = monitor_mod.selected_monitor;
746 apply_rules(display, client);
747 }
748
749 const monitor = client.monitor orelse return;
750
751 if (client.x + client.width > monitor.win_x + monitor.win_w) {
752 client.x = monitor.win_x + monitor.win_w - client.width - 2 * client.border_width;
753 }
754 if (client.y + client.height > monitor.win_y + monitor.win_h) {
755 client.y = monitor.win_y + monitor.win_h - client.height - 2 * client.border_width;
756 }
757 client.x = @max(client.x, monitor.win_x);
758 client.y = @max(client.y, monitor.win_y);
759
760 _ = xlib.XSetWindowBorderWidth(display.handle, win, @intCast(client.border_width));
761 _ = xlib.XSetWindowBorder(display.handle, win, border_color_unfocused);
762 tiling.send_configure(client);
763
764 update_window_type(display, client);
765 update_size_hints(display, client);
766 update_wm_hints(display, client);
767
768 _ = xlib.XSelectInput(
769 display.handle,
770 win,
771 xlib.EnterWindowMask | xlib.FocusChangeMask | xlib.PropertyChangeMask | xlib.StructureNotifyMask,
772 );
773 grabbuttons(display, client, false);
774
775 if (!client.is_floating) {
776 client.is_floating = trans != 0 or client.is_fixed;
777 client.old_state = client.is_floating;
778 }
779 if (client.is_floating) {
780 _ = xlib.XRaiseWindow(display.handle, client.window);
781 }
782
783 client_mod.attach_aside(client);
784 client_mod.attach_stack(client);
785
786 _ = xlib.XChangeProperty(display.handle, display.root, atoms.net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
787 _ = xlib.XMoveResizeWindow(display.handle, client.window, client.x + 2 * display.screen_width(), client.y, @intCast(client.width), @intCast(client.height));
788 set_client_state(display, client, NormalState);
789
790 if (client.monitor == monitor_mod.selected_monitor) {
791 const selmon = monitor_mod.selected_monitor orelse return;
792 unfocus_client(display, selmon.sel, false);
793 }
794 monitor.sel = client;
795
796 if (is_scrolling_layout(monitor)) {
797 monitor.scroll_offset = 0;
798 }
799
800 arrange(monitor);
801 _ = xlib.XMapWindow(display.handle, win);
802 focus(display, null);
803 }
804
805 fn handle_configure_request(display: *Display, event: *xlib.XConfigureRequestEvent) void {
806 const client = client_mod.window_to_client(event.window);
807
808 if (client) |managed_client| {
809 if ((event.value_mask & xlib.c.CWBorderWidth) != 0) {
810 managed_client.border_width = event.border_width;
811 } else if (managed_client.is_floating or (managed_client.monitor != null and managed_client.monitor.?.lt[managed_client.monitor.?.sel_lt] == null)) {
812 const monitor = managed_client.monitor orelse return;
813 if ((event.value_mask & xlib.c.CWX) != 0) {
814 managed_client.old_x = managed_client.x;
815 managed_client.x = monitor.mon_x + event.x;
816 }
817 if ((event.value_mask & xlib.c.CWY) != 0) {
818 managed_client.old_y = managed_client.y;
819 managed_client.y = monitor.mon_y + event.y;
820 }
821 if ((event.value_mask & xlib.c.CWWidth) != 0) {
822 managed_client.old_width = managed_client.width;
823 managed_client.width = event.width;
824 }
825 if ((event.value_mask & xlib.c.CWHeight) != 0) {
826 managed_client.old_height = managed_client.height;
827 managed_client.height = event.height;
828 }
829 const client_full_width = managed_client.width + managed_client.border_width * 2;
830 const client_full_height = managed_client.height + managed_client.border_width * 2;
831 if ((managed_client.x + managed_client.width) > monitor.mon_x + monitor.mon_w and managed_client.is_floating) {
832 managed_client.x = monitor.mon_x + @divTrunc(monitor.mon_w, 2) - @divTrunc(client_full_width, 2);
833 }
834 if ((managed_client.y + managed_client.height) > monitor.mon_y + monitor.mon_h and managed_client.is_floating) {
835 managed_client.y = monitor.mon_y + @divTrunc(monitor.mon_h, 2) - @divTrunc(client_full_height, 2);
836 }
837 if (((event.value_mask & (xlib.c.CWX | xlib.c.CWY)) != 0) and ((event.value_mask & (xlib.c.CWWidth | xlib.c.CWHeight)) == 0)) {
838 tiling.send_configure(managed_client);
839 }
840 if (client_mod.is_visible(managed_client)) {
841 _ = xlib.XMoveResizeWindow(display.handle, managed_client.window, managed_client.x, managed_client.y, @intCast(managed_client.width), @intCast(managed_client.height));
842 }
843 } else {
844 tiling.send_configure(managed_client);
845 }
846 } else {
847 var changes: xlib.XWindowChanges = undefined;
848 changes.x = event.x;
849 changes.y = event.y;
850 changes.width = event.width;
851 changes.height = event.height;
852 changes.border_width = event.border_width;
853 changes.sibling = event.above;
854 changes.stack_mode = event.detail;
855 _ = xlib.XConfigureWindow(display.handle, event.window, @intCast(event.value_mask), &changes);
856 }
857 _ = xlib.XSync(display.handle, xlib.False);
858 }
859
860 fn reset_chord_state(display_handle: *xlib.Display) void {
861 chord.reset(display_handle);
862 }
863
864 fn handle_key_press(display: *Display, event: *xlib.XKeyEvent) void {
865 const keysym = xlib.XKeycodeToKeysym(display.handle, @intCast(event.keycode), 0);
866
867 if (keybind_overlay) |overlay| {
868 if (overlay.handle_key(keysym)) {
869 return;
870 }
871 }
872
873 const clean_state = event.state & ~@as(c_uint, xlib.LockMask | xlib.Mod2Mask);
874 const current_time = std.time.milliTimestamp();
875
876 if (chord.index > 0 and (current_time - chord.last_timestamp) > kchord.timeout_ms) {
877 reset_chord_state(display.handle);
878 }
879
880 _ = chord.push(.{ .mod_mask = clean_state, .keysym = keysym });
881
882 for (config.keybinds.items) |keybind| {
883 if (keybind.key_count == 0) continue;
884
885 if (keybind.key_count == chord.index) {
886 var matches = true;
887 for (0..keybind.key_count) |i| {
888 if (chord.keys[i].keysym != keybind.keys[i].keysym or chord.keys[i].mod_mask != keybind.keys[i].mod_mask) {
889 matches = false;
890 break;
891 }
892 }
893 if (matches) {
894 execute_action(display, keybind.action, keybind.int_arg, keybind.str_arg);
895 reset_chord_state(display.handle);
896 return;
897 }
898 }
899 }
900
901 var has_partial_match = false;
902 for (config.keybinds.items) |keybind| {
903 if (keybind.key_count > chord.index) {
904 var matches = true;
905 for (0..chord.index) |i| {
906 if (chord.keys[i].keysym != keybind.keys[i].keysym or chord.keys[i].mod_mask != keybind.keys[i].mod_mask) {
907 matches = false;
908 break;
909 }
910 }
911 if (matches) {
912 has_partial_match = true;
913 break;
914 }
915 }
916 }
917
918 if (has_partial_match) {
919 chord.grab_keyboard(display.handle, display.root);
920 } else if (!has_partial_match) {
921 reset_chord_state(display.handle);
922 }
923 }
924
925 fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, str_arg: ?[]const u8) void {
926 switch (action) {
927 .spawn_terminal => spawn_terminal(),
928 .spawn => {
929 if (str_arg) |cmd| {
930 spawn_command(cmd);
931 }
932 },
933 .kill_client => kill_focused(display),
934 .quit => {
935 std.debug.print("quit keybind pressed\n", .{});
936 running = false;
937 },
938 .reload_config => reload_config(display),
939 .restart => reload_config(display),
940 .show_keybinds => {
941 if (keybind_overlay) |overlay| {
942 const mon = monitor_mod.selected_monitor orelse monitor_mod.monitors;
943 if (mon) |m| {
944 overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h, &config);
945 }
946 }
947 },
948 .focus_next => focusstack(display, 1),
949 .focus_prev => focusstack(display, -1),
950 .move_next => movestack(display, 1),
951 .move_prev => movestack(display, -1),
952 .resize_master => setmfact(@as(f32, @floatFromInt(int_arg)) / 1000.0),
953 .inc_master => incnmaster(1),
954 .dec_master => incnmaster(-1),
955 .toggle_floating => toggle_floating(display),
956 .toggle_fullscreen => toggle_fullscreen(display),
957 .toggle_gaps => toggle_gaps(),
958 .cycle_layout => cycle_layout(),
959 .set_layout => set_layout(str_arg),
960 .set_layout_tiling => set_layout_index(0),
961 .set_layout_floating => set_layout_index(2),
962 .view_tag => {
963 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
964 view(display, tag_mask);
965 },
966 .view_next_tag => view_adjacent_tag(display, 1),
967 .view_prev_tag => view_adjacent_tag(display, -1),
968 .view_next_nonempty_tag => view_adjacent_nonempty_tag(display, 1),
969 .view_prev_nonempty_tag => view_adjacent_nonempty_tag(display, -1),
970 .move_to_tag => {
971 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
972 tag_client(display, tag_mask);
973 },
974 .toggle_view_tag => {
975 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
976 toggle_view(display, tag_mask);
977 },
978 .toggle_tag => {
979 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
980 toggle_client_tag(display, tag_mask);
981 },
982 .focus_monitor => focusmon(display, int_arg),
983 .send_to_monitor => sendmon(display, int_arg),
984 .scroll_left => {
985 scroll_layout(-1);
986 },
987 .scroll_right => {
988 scroll_layout(1);
989 },
990 }
991 }
992
993 fn reload_config(display: *Display) void {
994 std.debug.print("reloading config...\n", .{});
995
996 ungrab_keybinds(display);
997
998 config.keybinds.clearRetainingCapacity();
999 config.buttons.clearRetainingCapacity();
1000 config.rules.clearRetainingCapacity();
1001 config.blocks.clearRetainingCapacity();
1002
1003 lua.deinit();
1004 _ = lua.init(&config);
1005
1006 const loaded = if (config_path_global) |path|
1007 lua.load_file(path)
1008 else
1009 lua.load_config();
1010
1011 if (loaded) {
1012 if (config_path_global) |path| {
1013 std.debug.print("reloaded config from {s}\n", .{path});
1014 } else {
1015 std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{});
1016 }
1017 apply_config_values();
1018 } else {
1019 std.debug.print("reload failed, restoring defaults\n", .{});
1020 initialize_default_config();
1021 }
1022
1023 bar_mod.destroy_bars(gpa.allocator(), display.handle);
1024 setup_bars(gpa.allocator(), display);
1025 rebuild_bar_blocks();
1026
1027 grab_keybinds(display);
1028 }
1029
1030 fn rebuild_bar_blocks() void {
1031 var current_bar = bar_mod.bars;
1032 while (current_bar) |bar| {
1033 bar.clear_blocks();
1034 if (config.blocks.items.len > 0) {
1035 for (config.blocks.items) |cfg_block| {
1036 const block = config_block_to_bar_block(cfg_block);
1037 bar.add_block(block);
1038 }
1039 } else {
1040 bar.add_block(blocks_mod.Block.init_ram("", 5, 0x7aa2f7, true));
1041 bar.add_block(blocks_mod.Block.init_static(" | ", 0x666666, false));
1042 bar.add_block(blocks_mod.Block.init_datetime("", "%H:%M", 1, 0x0db9d7, true));
1043 }
1044 current_bar = bar.next;
1045 }
1046 }
1047
1048 fn ungrab_keybinds(display: *Display) void {
1049 _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root);
1050 }
1051
1052 fn spawn_child_setup() void {
1053 _ = std.c.setsid();
1054 if (display_global) |display| {
1055 const display_fd = xlib.XConnectionNumber(display.handle);
1056 std.posix.close(@intCast(display_fd));
1057 }
1058 const sigchld_handler = std.posix.Sigaction{
1059 .handler = .{ .handler = std.posix.SIG.DFL },
1060 .mask = std.mem.zeroes(std.posix.sigset_t),
1061 .flags = 0,
1062 };
1063 std.posix.sigaction(std.posix.SIG.CHLD, &sigchld_handler, null);
1064 }
1065
1066 fn spawn_command(cmd: []const u8) void {
1067 std.debug.print("running cmd: {s}\n", .{cmd});
1068 const pid = std.posix.fork() catch return;
1069 if (pid == 0) {
1070 spawn_child_setup();
1071 var cmd_buf: [1024]u8 = undefined;
1072 if (cmd.len >= cmd_buf.len) {
1073 std.posix.exit(1);
1074 }
1075 @memcpy(cmd_buf[0..cmd.len], cmd);
1076 cmd_buf[cmd.len] = 0;
1077 const argv = [_:null]?[*:0]const u8{ "sh", "-c", @ptrCast(&cmd_buf) };
1078 _ = std.posix.execvpeZ("sh", &argv, std.c.environ) catch {};
1079 std.posix.exit(1);
1080 }
1081 }
1082
1083 fn spawn_terminal() void {
1084 const pid = std.posix.fork() catch return;
1085 if (pid == 0) {
1086 spawn_child_setup();
1087 var term_buf: [256]u8 = undefined;
1088 const terminal = config.terminal;
1089 if (terminal.len >= term_buf.len) {
1090 std.posix.exit(1);
1091 }
1092 @memcpy(term_buf[0..terminal.len], terminal);
1093 term_buf[terminal.len] = 0;
1094 const term_ptr: [*:0]const u8 = @ptrCast(&term_buf);
1095 const argv = [_:null]?[*:0]const u8{term_ptr};
1096 _ = std.posix.execvpeZ(term_ptr, &argv, std.c.environ) catch {};
1097 std.posix.exit(1);
1098 }
1099 }
1100
1101 fn movestack(display: *Display, direction: i32) void {
1102 const monitor = monitor_mod.selected_monitor orelse return;
1103 const current = monitor.sel orelse return;
1104
1105 if (current.is_floating) {
1106 return;
1107 }
1108
1109 var target: ?*Client = null;
1110
1111 if (direction > 0) {
1112 target = current.next;
1113 while (target) |client| {
1114 if (client_mod.is_visible(client) and !client.is_floating) {
1115 break;
1116 }
1117 target = client.next;
1118 }
1119 if (target == null) {
1120 target = monitor.clients;
1121 while (target) |client| {
1122 if (client == current) {
1123 break;
1124 }
1125 if (client_mod.is_visible(client) and !client.is_floating) {
1126 break;
1127 }
1128 target = client.next;
1129 }
1130 }
1131 } else {
1132 var prev: ?*Client = null;
1133 var iter = monitor.clients;
1134 while (iter) |client| {
1135 if (client == current) {
1136 break;
1137 }
1138 if (client_mod.is_visible(client) and !client.is_floating) {
1139 prev = client;
1140 }
1141 iter = client.next;
1142 }
1143 if (prev == null) {
1144 iter = current.next;
1145 while (iter) |client| {
1146 if (client_mod.is_visible(client) and !client.is_floating) {
1147 prev = client;
1148 }
1149 iter = client.next;
1150 }
1151 }
1152 target = prev;
1153 }
1154
1155 if (target) |swap_client| {
1156 if (swap_client != current) {
1157 client_mod.swap_clients(current, swap_client);
1158 arrange(monitor);
1159 focus(display, current);
1160 }
1161 }
1162 }
1163
1164 fn toggle_view(display: *Display, tag_mask: u32) void {
1165 const monitor = monitor_mod.selected_monitor orelse return;
1166 const new_tags = monitor.tagset[monitor.sel_tags] ^ tag_mask;
1167 if (new_tags != 0) {
1168 monitor.tagset[monitor.sel_tags] = new_tags;
1169
1170 if (new_tags == ~@as(u32, 0)) {
1171 monitor.pertag.prevtag = monitor.pertag.curtag;
1172 monitor.pertag.curtag = 0;
1173 }
1174
1175 if ((new_tags & (@as(u32, 1) << @intCast(monitor.pertag.curtag -| 1))) == 0) {
1176 monitor.pertag.prevtag = monitor.pertag.curtag;
1177 var i: u32 = 0;
1178 while (i < 9) : (i += 1) {
1179 if ((new_tags & (@as(u32, 1) << @intCast(i))) != 0) break;
1180 }
1181 monitor.pertag.curtag = i + 1;
1182 }
1183
1184 monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag];
1185 monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag];
1186 monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag];
1187
1188 focus_top_client(display, monitor);
1189 arrange(monitor);
1190 bar_mod.invalidate_bars();
1191 }
1192 }
1193
1194 fn toggle_client_tag(display: *Display, tag_mask: u32) void {
1195 const monitor = monitor_mod.selected_monitor orelse return;
1196 const client = monitor.sel orelse return;
1197 const new_tags = client.tags ^ tag_mask;
1198 if (new_tags != 0) {
1199 client.tags = new_tags;
1200 focus_top_client(display, monitor);
1201 arrange(monitor);
1202 bar_mod.invalidate_bars();
1203 }
1204 }
1205
1206 fn toggle_gaps() void {
1207 const monitor = monitor_mod.selected_monitor orelse return;
1208 if (monitor.gap_inner_h == 0) {
1209 monitor.gap_inner_h = gap_inner_v;
1210 monitor.gap_inner_v = gap_inner_v;
1211 monitor.gap_outer_h = gap_outer_h;
1212 monitor.gap_outer_v = gap_outer_v;
1213 } else {
1214 monitor.gap_inner_h = 0;
1215 monitor.gap_inner_v = 0;
1216 monitor.gap_outer_h = 0;
1217 monitor.gap_outer_v = 0;
1218 }
1219 arrange(monitor);
1220 }
1221
1222 fn kill_focused(display: *Display) void {
1223 const selected = monitor_mod.selected_monitor orelse return;
1224 const client = selected.sel orelse return;
1225 std.debug.print("killing window: 0x{x}\n", .{client.window});
1226
1227 if (!send_event(display, client, atoms.wm_delete)) {
1228 _ = xlib.XGrabServer(display.handle);
1229 _ = xlib.XKillClient(display.handle, client.window);
1230 _ = xlib.XSync(display.handle, xlib.False);
1231 _ = xlib.XUngrabServer(display.handle);
1232 }
1233 }
1234
1235 fn toggle_fullscreen(display: *Display) void {
1236 const selected = monitor_mod.selected_monitor orelse return;
1237 const client = selected.sel orelse return;
1238 set_fullscreen(display, client, !client.is_fullscreen);
1239 }
1240
1241 fn set_fullscreen(display: *Display, client: *Client, fullscreen: bool) void {
1242 const monitor = client.monitor orelse return;
1243
1244 if (fullscreen and !client.is_fullscreen) {
1245 var fullscreen_atom = atoms.net_wm_state_fullscreen;
1246 _ = xlib.XChangeProperty(
1247 display.handle,
1248 client.window,
1249 atoms.net_wm_state,
1250 xlib.XA_ATOM,
1251 32,
1252 xlib.PropModeReplace,
1253 @ptrCast(&fullscreen_atom),
1254 1,
1255 );
1256 client.is_fullscreen = true;
1257 client.old_state = client.is_floating;
1258 client.old_border_width = client.border_width;
1259 client.border_width = 0;
1260 client.is_floating = true;
1261
1262 _ = xlib.XSetWindowBorderWidth(display.handle, client.window, 0);
1263 tiling.resize_client(client, monitor.mon_x, monitor.mon_y, monitor.mon_w, monitor.mon_h);
1264 _ = xlib.XRaiseWindow(display.handle, client.window);
1265
1266 std.debug.print("fullscreen enabled: window=0x{x}\n", .{client.window});
1267 } else if (!fullscreen and client.is_fullscreen) {
1268 var no_atom: xlib.Atom = 0;
1269 _ = xlib.XChangeProperty(
1270 display.handle,
1271 client.window,
1272 atoms.net_wm_state,
1273 xlib.XA_ATOM,
1274 32,
1275 xlib.PropModeReplace,
1276 @ptrCast(&no_atom),
1277 0,
1278 );
1279 client.is_fullscreen = false;
1280 client.is_floating = client.old_state;
1281 client.border_width = client.old_border_width;
1282
1283 client.x = client.old_x;
1284 client.y = client.old_y;
1285 client.width = client.old_width;
1286 client.height = client.old_height;
1287
1288 tiling.resize_client(client, client.x, client.y, client.width, client.height);
1289 arrange(monitor);
1290
1291 std.debug.print("fullscreen disabled: window=0x{x}\n", .{client.window});
1292 }
1293 }
1294
1295 fn view(display: *Display, tag_mask: u32) void {
1296 const monitor = monitor_mod.selected_monitor orelse return;
1297 if (tag_mask == monitor.tagset[monitor.sel_tags]) {
1298 return;
1299 }
1300 monitor.sel_tags ^= 1;
1301 if (tag_mask != 0) {
1302 monitor.tagset[monitor.sel_tags] = tag_mask;
1303 monitor.pertag.prevtag = monitor.pertag.curtag;
1304
1305 if (tag_mask == ~@as(u32, 0)) {
1306 monitor.pertag.curtag = 0;
1307 } else {
1308 var i: u32 = 0;
1309 while (i < 9) : (i += 1) {
1310 if ((tag_mask & (@as(u32, 1) << @intCast(i))) != 0) break;
1311 }
1312 monitor.pertag.curtag = i + 1;
1313 }
1314 } else {
1315 const tmp = monitor.pertag.prevtag;
1316 monitor.pertag.prevtag = monitor.pertag.curtag;
1317 monitor.pertag.curtag = tmp;
1318 }
1319
1320 monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag];
1321 monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag];
1322 monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag];
1323
1324 focus_top_client(display, monitor);
1325 arrange(monitor);
1326 bar_mod.invalidate_bars();
1327 std.debug.print("view: tag_mask={d}\n", .{monitor.tagset[monitor.sel_tags]});
1328 }
1329
1330 fn view_adjacent_tag(display: *Display, direction: i32) void {
1331 const monitor = monitor_mod.selected_monitor orelse return;
1332 const current_tag = monitor.pertag.curtag;
1333 var new_tag: i32 = @intCast(current_tag);
1334
1335 new_tag += direction;
1336 if (new_tag < 1) new_tag = 9;
1337 if (new_tag > 9) new_tag = 1;
1338
1339 const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
1340 view(display, tag_mask);
1341 }
1342
1343 fn view_adjacent_nonempty_tag(display: *Display, direction: i32) void {
1344 const monitor = monitor_mod.selected_monitor orelse return;
1345 const current_tag = monitor.pertag.curtag;
1346 var new_tag: i32 = @intCast(current_tag);
1347
1348 var attempts: i32 = 0;
1349 while (attempts < 9) : (attempts += 1) {
1350 new_tag += direction;
1351 if (new_tag < 1) new_tag = 9;
1352 if (new_tag > 9) new_tag = 1;
1353
1354 const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
1355 if (has_clients_on_tag(monitor, tag_mask)) {
1356 view(display, tag_mask);
1357 return;
1358 }
1359 }
1360 }
1361
1362 fn has_clients_on_tag(monitor: *monitor_mod.Monitor, tag_mask: u32) bool {
1363 var client = monitor.clients;
1364 while (client) |c| {
1365 if ((c.tags & tag_mask) != 0) {
1366 return true;
1367 }
1368 client = c.next;
1369 }
1370 return false;
1371 }
1372
1373 fn tag_client(display: *Display, tag_mask: u32) void {
1374 const monitor = monitor_mod.selected_monitor orelse return;
1375 const client = monitor.sel orelse return;
1376 if (tag_mask == 0) {
1377 return;
1378 }
1379 client.tags = tag_mask;
1380 focus_top_client(display, monitor);
1381 arrange(monitor);
1382 bar_mod.invalidate_bars();
1383 std.debug.print("tag_client: window=0x{x} tag_mask={d}\n", .{ client.window, tag_mask });
1384 }
1385
1386 fn focus_top_client(display: *Display, monitor: *Monitor) void {
1387 var visible_client = monitor.stack;
1388 while (visible_client) |client| {
1389 if (client_mod.is_visible(client)) {
1390 focus(display, client);
1391 return;
1392 }
1393 visible_client = client.stack_next;
1394 }
1395 monitor.sel = null;
1396 _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
1397 }
1398
1399 fn focusstack(display: *Display, direction: i32) void {
1400 const monitor = monitor_mod.selected_monitor orelse return;
1401 const current = monitor.sel orelse return;
1402
1403 var next_client: ?*Client = null;
1404
1405 if (direction > 0) {
1406 next_client = current.next;
1407 while (next_client) |client| {
1408 if (client_mod.is_visible(client)) {
1409 break;
1410 }
1411 next_client = client.next;
1412 }
1413 if (next_client == null) {
1414 next_client = monitor.clients;
1415 while (next_client) |client| {
1416 if (client_mod.is_visible(client)) {
1417 break;
1418 }
1419 next_client = client.next;
1420 }
1421 }
1422 } else {
1423 var prev: ?*Client = null;
1424 var iter = monitor.clients;
1425 while (iter) |client| {
1426 if (client == current) {
1427 break;
1428 }
1429 if (client_mod.is_visible(client)) {
1430 prev = client;
1431 }
1432 iter = client.next;
1433 }
1434 if (prev == null) {
1435 iter = current.next;
1436 while (iter) |client| {
1437 if (client_mod.is_visible(client)) {
1438 prev = client;
1439 }
1440 iter = client.next;
1441 }
1442 }
1443 next_client = prev;
1444 }
1445
1446 if (next_client) |client| {
1447 focus(display, client);
1448 if (client.monitor) |client_monitor| {
1449 restack(display, client_monitor);
1450 }
1451 }
1452 }
1453
1454 fn toggle_floating(_: *Display) void {
1455 const monitor = monitor_mod.selected_monitor orelse return;
1456 const client = monitor.sel orelse return;
1457
1458 if (client.is_fullscreen) {
1459 return;
1460 }
1461
1462 client.is_floating = !client.is_floating;
1463
1464 if (client.is_floating) {
1465 tiling.resize(client, client.x, client.y, client.width, client.height, false);
1466 }
1467
1468 arrange(monitor);
1469 std.debug.print("toggle_floating: window=0x{x} floating={}\n", .{ client.window, client.is_floating });
1470 }
1471
1472 fn incnmaster(delta: i32) void {
1473 const monitor = monitor_mod.selected_monitor orelse return;
1474 const new_val = @max(0, monitor.nmaster + delta);
1475 monitor.nmaster = new_val;
1476 monitor.pertag.nmasters[monitor.pertag.curtag] = new_val;
1477 arrange(monitor);
1478 std.debug.print("incnmaster: nmaster={d}\n", .{monitor.nmaster});
1479 }
1480
1481 fn setmfact(delta: f32) void {
1482 const monitor = monitor_mod.selected_monitor orelse return;
1483 const new_mfact = monitor.mfact + delta;
1484 if (new_mfact < 0.05 or new_mfact > 0.95) {
1485 return;
1486 }
1487 monitor.mfact = new_mfact;
1488 monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact;
1489 arrange(monitor);
1490 std.debug.print("setmfact: mfact={d:.2}\n", .{monitor.mfact});
1491 }
1492
1493 fn cycle_layout() void {
1494 const monitor = monitor_mod.selected_monitor orelse return;
1495 const new_lt = (monitor.sel_lt + 1) % 5;
1496 monitor.sel_lt = new_lt;
1497 monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
1498 if (new_lt != 3) {
1499 monitor.scroll_offset = 0;
1500 }
1501 arrange(monitor);
1502 bar_mod.invalidate_bars();
1503 if (monitor.lt[monitor.sel_lt]) |layout| {
1504 std.debug.print("cycle_layout: {s}\n", .{layout.symbol});
1505 }
1506 }
1507
1508 fn set_layout(layout_name: ?[]const u8) void {
1509 const monitor = monitor_mod.selected_monitor orelse return;
1510 const name = layout_name orelse return;
1511
1512 const new_lt: u32 = if (std.mem.eql(u8, name, "tiling") or std.mem.eql(u8, name, "[]="))
1513 0
1514 else if (std.mem.eql(u8, name, "monocle") or std.mem.eql(u8, name, "[M]"))
1515 1
1516 else if (std.mem.eql(u8, name, "floating") or std.mem.eql(u8, name, "><>"))
1517 2
1518 else if (std.mem.eql(u8, name, "scrolling") or std.mem.eql(u8, name, "[S]"))
1519 3
1520 else if (std.mem.eql(u8, name, "grid") or std.mem.eql(u8, name, "[#]"))
1521 4
1522 else {
1523 std.debug.print("set_layout: unknown layout '{s}'\n", .{name});
1524 return;
1525 };
1526
1527 monitor.sel_lt = new_lt;
1528 monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
1529 if (new_lt != 3) {
1530 monitor.scroll_offset = 0;
1531 }
1532 arrange(monitor);
1533 bar_mod.invalidate_bars();
1534 if (monitor.lt[monitor.sel_lt]) |layout| {
1535 std.debug.print("set_layout: {s}\n", .{layout.symbol});
1536 }
1537 }
1538
1539 fn set_layout_index(index: u32) void {
1540 const monitor = monitor_mod.selected_monitor orelse return;
1541 monitor.sel_lt = index;
1542 monitor.pertag.sellts[monitor.pertag.curtag] = index;
1543 if (index != 3) {
1544 monitor.scroll_offset = 0;
1545 }
1546 arrange(monitor);
1547 bar_mod.invalidate_bars();
1548 if (monitor.lt[monitor.sel_lt]) |layout| {
1549 std.debug.print("set_layout_index: {s}\n", .{layout.symbol});
1550 }
1551 }
1552
1553 fn focusmon(display: *Display, direction: i32) void {
1554 const selmon = monitor_mod.selected_monitor orelse return;
1555 const target = monitor_mod.dir_to_monitor(direction) orelse return;
1556 if (target == selmon) {
1557 return;
1558 }
1559 unfocus_client(display, selmon.sel, false);
1560 monitor_mod.selected_monitor = target;
1561 focus(display, null);
1562 std.debug.print("focusmon: monitor {d}\n", .{target.num});
1563 }
1564
1565 fn sendmon(display: *Display, direction: i32) void {
1566 const source_monitor = monitor_mod.selected_monitor orelse return;
1567 const client = source_monitor.sel orelse return;
1568 const target = monitor_mod.dir_to_monitor(direction) orelse return;
1569
1570 if (target == source_monitor) {
1571 return;
1572 }
1573
1574 client_mod.detach(client);
1575 client_mod.detach_stack(client);
1576 client.monitor = target;
1577 client.tags = target.tagset[target.sel_tags];
1578 client_mod.attach_aside(client);
1579 client_mod.attach_stack(client);
1580
1581 focus_top_client(display, source_monitor);
1582 arrange(source_monitor);
1583 arrange(target);
1584
1585 std.debug.print("sendmon: window=0x{x} to monitor {d}\n", .{ client.window, target.num });
1586 }
1587
1588 fn snap_x(client: *Client, new_x: i32, monitor: *Monitor) i32 {
1589 const client_width = client.width + 2 * client.border_width;
1590 if (@abs(monitor.win_x - new_x) < snap_distance) {
1591 return monitor.win_x;
1592 } else if (@abs((monitor.win_x + monitor.win_w) - (new_x + client_width)) < snap_distance) {
1593 return monitor.win_x + monitor.win_w - client_width;
1594 }
1595 return new_x;
1596 }
1597
1598 fn snap_y(client: *Client, new_y: i32, monitor: *Monitor) i32 {
1599 const client_height = client.height + 2 * client.border_width;
1600 if (@abs(monitor.win_y - new_y) < snap_distance) {
1601 return monitor.win_y;
1602 } else if (@abs((monitor.win_y + monitor.win_h) - (new_y + client_height)) < snap_distance) {
1603 return monitor.win_y + monitor.win_h - client_height;
1604 }
1605 return new_y;
1606 }
1607
1608 fn movemouse(display: *Display) void {
1609 const monitor = monitor_mod.selected_monitor orelse return;
1610 const client = monitor.sel orelse return;
1611
1612 if (client.is_fullscreen) {
1613 return;
1614 }
1615
1616 restack(display, monitor);
1617
1618 const was_floating = client.is_floating;
1619 if (!client.is_floating) {
1620 client.is_floating = true;
1621 }
1622
1623 var root_x: c_int = undefined;
1624 var root_y: c_int = undefined;
1625 var dummy_win: xlib.Window = undefined;
1626 var dummy_int: c_int = undefined;
1627 var dummy_uint: c_uint = undefined;
1628
1629 _ = xlib.XQueryPointer(display.handle, display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint);
1630
1631 const grab_result = xlib.XGrabPointer(
1632 display.handle,
1633 display.root,
1634 xlib.False,
1635 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
1636 xlib.GrabModeAsync,
1637 xlib.GrabModeAsync,
1638 xlib.None,
1639 cursor_move,
1640 xlib.CurrentTime,
1641 );
1642
1643 if (grab_result != xlib.GrabSuccess) {
1644 return;
1645 }
1646
1647 const start_x = client.x;
1648 const start_y = client.y;
1649 const pointer_start_x = root_x;
1650 const pointer_start_y = root_y;
1651 var last_time: c_ulong = 0;
1652
1653 var event: xlib.XEvent = undefined;
1654 var done = false;
1655
1656 while (!done) {
1657 _ = xlib.XNextEvent(display.handle, &event);
1658
1659 switch (event.type) {
1660 xlib.MotionNotify => {
1661 const motion = &event.xmotion;
1662 if ((motion.time - last_time) < (1000 / 60)) {
1663 continue;
1664 }
1665 last_time = motion.time;
1666 const delta_x = motion.x_root - pointer_start_x;
1667 const delta_y = motion.y_root - pointer_start_y;
1668 var new_x = start_x + delta_x;
1669 var new_y = start_y + delta_y;
1670 if (client.monitor) |client_monitor| {
1671 new_x = snap_x(client, new_x, client_monitor);
1672 new_y = snap_y(client, new_y, client_monitor);
1673 }
1674 tiling.resize(client, new_x, new_y, client.width, client.height, true);
1675 },
1676 xlib.ButtonRelease => {
1677 done = true;
1678 },
1679 else => {},
1680 }
1681 }
1682
1683 _ = xlib.XUngrabPointer(display.handle, xlib.CurrentTime);
1684
1685 const new_mon = monitor_mod.rect_to_monitor(client.x, client.y, client.width, client.height);
1686 if (new_mon != null and new_mon != monitor) {
1687 client_mod.detach(client);
1688 client_mod.detach_stack(client);
1689 client.monitor = new_mon;
1690 client.tags = new_mon.?.tagset[new_mon.?.sel_tags];
1691 client_mod.attach_aside(client);
1692 client_mod.attach_stack(client);
1693 monitor_mod.selected_monitor = new_mon;
1694 focus(display, client);
1695 arrange(monitor);
1696 arrange(new_mon.?);
1697 } else {
1698 arrange(monitor);
1699 }
1700
1701 if (config.auto_tile and !was_floating) {
1702 const drop_monitor = client.monitor orelse return;
1703 const center_x = client.x + @divTrunc(client.width, 2);
1704 const center_y = client.y + @divTrunc(client.height, 2);
1705
1706 if (client_mod.tiled_window_at(client, drop_monitor, center_x, center_y)) |target| {
1707 client_mod.insert_before(client, target);
1708 }
1709
1710 client.is_floating = false;
1711 arrange(drop_monitor);
1712 }
1713 }
1714
1715 fn resizemouse(display: *Display) void {
1716 const monitor = monitor_mod.selected_monitor orelse return;
1717 const client = monitor.sel orelse return;
1718
1719 if (client.is_fullscreen) {
1720 return;
1721 }
1722
1723 restack(display, monitor);
1724
1725 if (!client.is_floating) {
1726 client.is_floating = true;
1727 }
1728
1729 const grab_result = xlib.XGrabPointer(
1730 display.handle,
1731 display.root,
1732 xlib.False,
1733 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
1734 xlib.GrabModeAsync,
1735 xlib.GrabModeAsync,
1736 xlib.None,
1737 cursor_resize,
1738 xlib.CurrentTime,
1739 );
1740
1741 if (grab_result != xlib.GrabSuccess) {
1742 return;
1743 }
1744
1745 _ = xlib.XWarpPointer(display.handle, xlib.None, client.window, 0, 0, 0, 0, client.width + client.border_width - 1, client.height + client.border_width - 1);
1746
1747 var event: xlib.XEvent = undefined;
1748 var done = false;
1749 var last_time: c_ulong = 0;
1750
1751 while (!done) {
1752 _ = xlib.XNextEvent(display.handle, &event);
1753
1754 switch (event.type) {
1755 xlib.MotionNotify => {
1756 const motion = &event.xmotion;
1757 if ((motion.time - last_time) < (1000 / 60)) {
1758 continue;
1759 }
1760 last_time = motion.time;
1761 var new_width = @max(1, motion.x_root - client.x - 2 * client.border_width + 1);
1762 var new_height = @max(1, motion.y_root - client.y - 2 * client.border_width + 1);
1763 if (client.monitor) |client_monitor| {
1764 const client_right = client.x + new_width + 2 * client.border_width;
1765 const client_bottom = client.y + new_height + 2 * client.border_width;
1766 const mon_right = client_monitor.win_x + client_monitor.win_w;
1767 const mon_bottom = client_monitor.win_y + client_monitor.win_h;
1768 if (@abs(mon_right - client_right) < snap_distance) {
1769 new_width = @max(1, mon_right - client.x - 2 * client.border_width);
1770 }
1771 if (@abs(mon_bottom - client_bottom) < snap_distance) {
1772 new_height = @max(1, mon_bottom - client.y - 2 * client.border_width);
1773 }
1774 }
1775 tiling.resize(client, client.x, client.y, new_width, new_height, true);
1776 },
1777 xlib.ButtonRelease => {
1778 done = true;
1779 },
1780 else => {},
1781 }
1782 }
1783
1784 _ = xlib.XUngrabPointer(display.handle, xlib.CurrentTime);
1785 arrange(monitor);
1786 }
1787
1788 fn handle_expose(display: *Display, event: *xlib.XExposeEvent) void {
1789 if (event.count != 0) return;
1790
1791 if (bar_mod.window_to_bar(event.window)) |bar| {
1792 bar.invalidate();
1793 bar.draw(display.handle, &tags, config);
1794 }
1795 }
1796
1797 fn clean_mask(mask: c_uint) c_uint {
1798 const lock: c_uint = @intCast(xlib.LockMask);
1799 const shift: c_uint = @intCast(xlib.ShiftMask);
1800 const ctrl: c_uint = @intCast(xlib.ControlMask);
1801 const mod1: c_uint = @intCast(xlib.Mod1Mask);
1802 const mod2: c_uint = @intCast(xlib.Mod2Mask);
1803 const mod3: c_uint = @intCast(xlib.Mod3Mask);
1804 const mod4: c_uint = @intCast(xlib.Mod4Mask);
1805 const mod5: c_uint = @intCast(xlib.Mod5Mask);
1806 return mask & ~(lock | numlock_mask) & (shift | ctrl | mod1 | mod2 | mod3 | mod4 | mod5);
1807 }
1808
1809 fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void {
1810 std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow });
1811
1812 const clicked_monitor = monitor_mod.window_to_monitor(event.window);
1813 if (clicked_monitor) |monitor| {
1814 if (monitor != monitor_mod.selected_monitor) {
1815 if (monitor_mod.selected_monitor) |selmon| {
1816 unfocus_client(display, selmon.sel, true);
1817 }
1818 monitor_mod.selected_monitor = monitor;
1819 focus(display, null);
1820 }
1821 }
1822
1823 if (bar_mod.window_to_bar(event.window)) |bar| {
1824 const clicked_tag = bar.handle_click(event.x, &tags);
1825 if (clicked_tag) |tag_index| {
1826 const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index);
1827 view(display, tag_mask);
1828 }
1829 return;
1830 }
1831
1832 const click_client = client_mod.window_to_client(event.window);
1833 if (click_client) |found_client| {
1834 focus(display, found_client);
1835 if (monitor_mod.selected_monitor) |selmon| {
1836 restack(display, selmon);
1837 }
1838 _ = xlib.XAllowEvents(display.handle, xlib.ReplayPointer, xlib.CurrentTime);
1839 }
1840
1841 const clean_state = clean_mask(event.state);
1842 for (config.buttons.items) |button| {
1843 if (button.click != .client_win) continue;
1844 const button_clean_mask = clean_mask(button.mod_mask);
1845 if (clean_state == button_clean_mask and event.button == button.button) {
1846 switch (button.action) {
1847 .move_mouse => movemouse(display),
1848 .resize_mouse => resizemouse(display),
1849 .toggle_floating => {
1850 if (click_client) |found_client| {
1851 found_client.is_floating = !found_client.is_floating;
1852 if (monitor_mod.selected_monitor) |monitor| {
1853 arrange(monitor);
1854 }
1855 }
1856 },
1857 }
1858 return;
1859 }
1860 }
1861 }
1862
1863 fn handle_client_message(display: *Display, event: *xlib.XClientMessageEvent) void {
1864 const client = client_mod.window_to_client(event.window) orelse return;
1865
1866 if (event.message_type == atoms.net_wm_state) {
1867 const action = event.data.l[0];
1868 const first_property = @as(xlib.Atom, @intCast(event.data.l[1]));
1869 const second_property = @as(xlib.Atom, @intCast(event.data.l[2]));
1870
1871 if (first_property == atoms.net_wm_state_fullscreen or second_property == atoms.net_wm_state_fullscreen) {
1872 const net_wm_state_remove = 0;
1873 const net_wm_state_add = 1;
1874 const net_wm_state_toggle = 2;
1875
1876 if (action == net_wm_state_add) {
1877 set_fullscreen(display, client, true);
1878 } else if (action == net_wm_state_remove) {
1879 set_fullscreen(display, client, false);
1880 } else if (action == net_wm_state_toggle) {
1881 set_fullscreen(display, client, !client.is_fullscreen);
1882 }
1883 }
1884 } else if (event.message_type == atoms.net_active_window) {
1885 const selected = monitor_mod.selected_monitor orelse return;
1886 if (client != selected.sel and !client.is_urgent) {
1887 set_urgent(display, client, true);
1888 }
1889 }
1890 }
1891
1892 fn handle_destroy_notify(display: *Display, event: *xlib.XDestroyWindowEvent) void {
1893 const client = client_mod.window_to_client(event.window) orelse return;
1894 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
1895 unmanage(display, client);
1896 }
1897
1898 fn handle_unmap_notify(display: *Display, event: *xlib.XUnmapEvent) void {
1899 const client = client_mod.window_to_client(event.window) orelse return;
1900 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
1901 unmanage(display, client);
1902 }
1903
1904 fn unmanage(display: *Display, client: *Client) void {
1905 const client_monitor = client.monitor;
1906
1907 var next_focus: ?*Client = null;
1908 if (client_monitor) |monitor| {
1909 if (monitor.sel == client and is_scrolling_layout(monitor)) {
1910 next_focus = client.next;
1911 if (next_focus == null) {
1912 var prev: ?*Client = null;
1913 var iter = monitor.clients;
1914 while (iter) |c| {
1915 if (c == client) break;
1916 prev = c;
1917 iter = c.next;
1918 }
1919 next_focus = prev;
1920 }
1921 }
1922 }
1923
1924 client_mod.detach(client);
1925 client_mod.detach_stack(client);
1926
1927 if (client_monitor) |monitor| {
1928 if (monitor.sel == client) {
1929 monitor.sel = if (next_focus) |nf| nf else monitor.stack;
1930 }
1931 if (is_scrolling_layout(monitor)) {
1932 const target = if (monitor.sel) |sel| scrolling.get_target_scroll_for_window(monitor, sel) else 0;
1933 if (target == 0) {
1934 monitor.scroll_offset = scrolling.get_scroll_step(monitor);
1935 } else {
1936 monitor.scroll_offset = 0;
1937 }
1938 }
1939 arrange(monitor);
1940 }
1941
1942 if (client_monitor) |monitor| {
1943 if (monitor.sel) |selected| {
1944 focus(display, selected);
1945 }
1946 }
1947
1948 client_mod.destroy(client);
1949 update_client_list(display);
1950 bar_mod.invalidate_bars();
1951 }
1952
1953 fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent) void {
1954 if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != display.root) {
1955 return;
1956 }
1957
1958 const client = client_mod.window_to_client(event.window);
1959 const target_mon = if (client) |c| c.monitor else monitor_mod.window_to_monitor(event.window);
1960 const selmon = monitor_mod.selected_monitor;
1961
1962 if (target_mon != selmon) {
1963 if (selmon) |sel| {
1964 unfocus_client(display, sel.sel, true);
1965 }
1966 monitor_mod.selected_monitor = target_mon;
1967 } else if (client == null) {
1968 return;
1969 } else if (selmon) |sel| {
1970 if (client.? == sel.sel) {
1971 return;
1972 }
1973 }
1974
1975 focus(display, client);
1976 }
1977
1978 fn handle_focus_in(display: *Display, event: *xlib.XFocusChangeEvent) void {
1979 const selmon = monitor_mod.selected_monitor orelse return;
1980 const selected = selmon.sel orelse return;
1981 if (event.window != selected.window) {
1982 set_focus(display, selected);
1983 }
1984 }
1985
1986 fn set_focus(display: *Display, client: *Client) void {
1987 if (!client.never_focus) {
1988 _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime);
1989 _ = xlib.XChangeProperty(display.handle, display.root, atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
1990 }
1991 _ = send_event(display, client, atoms.wm_take_focus);
1992 }
1993
1994 var last_motion_monitor: ?*Monitor = null;
1995
1996 fn handle_motion_notify(display: *Display, event: *xlib.XMotionEvent) void {
1997 if (event.window != display.root) {
1998 return;
1999 }
2000
2001 const target_mon = monitor_mod.rect_to_monitor(event.x_root, event.y_root, 1, 1);
2002 if (target_mon != last_motion_monitor and last_motion_monitor != null) {
2003 if (monitor_mod.selected_monitor) |selmon| {
2004 unfocus_client(display, selmon.sel, true);
2005 }
2006 monitor_mod.selected_monitor = target_mon;
2007 focus(display, null);
2008 }
2009 last_motion_monitor = target_mon;
2010 }
2011
2012 fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void {
2013 if (event.state == xlib.PropertyDelete) {
2014 return;
2015 }
2016
2017 const client = client_mod.window_to_client(event.window) orelse return;
2018
2019 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
2020 var trans: xlib.Window = 0;
2021 if (!client.is_floating and xlib.XGetTransientForHint(display.handle, client.window, &trans) != 0) {
2022 client.is_floating = client_mod.window_to_client(trans) != null;
2023 if (client.is_floating) {
2024 if (client.monitor) |monitor| {
2025 arrange(monitor);
2026 }
2027 }
2028 }
2029 } else if (event.atom == xlib.XA_WM_NORMAL_HINTS) {
2030 client.hints_valid = false;
2031 } else if (event.atom == xlib.XA_WM_HINTS) {
2032 update_wm_hints(display, client);
2033 bar_mod.invalidate_bars();
2034 } else if (event.atom == xlib.XA_WM_NAME or event.atom == atoms.net_wm_name) {
2035 update_title(display, client);
2036 } else if (event.atom == atoms.net_wm_window_type) {
2037 update_window_type(display, client);
2038 }
2039 }
2040
2041 fn unfocus_client(display: *Display, client: ?*Client, reset_input_focus: bool) void {
2042 const unfocus_target = client orelse return;
2043 grabbuttons(display, unfocus_target, false);
2044 _ = xlib.XSetWindowBorder(display.handle, unfocus_target.window, border_color_unfocused);
2045 if (reset_input_focus) {
2046 _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
2047 _ = xlib.XDeleteProperty(display.handle, display.root, atoms.net_active_window);
2048 }
2049 }
2050
2051 fn set_client_state(display: *Display, client: *Client, state: c_long) void {
2052 var data: [2]c_long = .{ state, xlib.None };
2053 _ = xlib.c.XChangeProperty(display.handle, client.window, atoms.wm_state, xlib.XA_ATOM, 32, xlib.PropModeReplace, @ptrCast(&data), 2);
2054 }
2055
2056 fn update_numlock_mask(display: *Display) void {
2057 numlock_mask = 0;
2058 const modmap = xlib.XGetModifierMapping(display.handle);
2059 if (modmap == null) return;
2060 defer _ = xlib.XFreeModifiermap(modmap);
2061
2062 const numlock_keycode = xlib.XKeysymToKeycode(display.handle, xlib.XK_Num_Lock);
2063
2064 var modifier_index: usize = 0;
2065 while (modifier_index < 8) : (modifier_index += 1) {
2066 var key_index: usize = 0;
2067 while (key_index < @as(usize, @intCast(modmap.*.max_keypermod))) : (key_index += 1) {
2068 const keycode = modmap.*.modifiermap[modifier_index * @as(usize, @intCast(modmap.*.max_keypermod)) + key_index];
2069 if (keycode == numlock_keycode) {
2070 numlock_mask = @as(c_uint, 1) << @intCast(modifier_index);
2071 }
2072 }
2073 }
2074 }
2075
2076 fn grabbuttons(display: *Display, client: *Client, focused: bool) void {
2077 update_numlock_mask(display);
2078 const modifiers = [_]c_uint{ 0, xlib.LockMask, numlock_mask, numlock_mask | xlib.LockMask };
2079
2080 _ = xlib.XUngrabButton(display.handle, xlib.AnyButton, xlib.AnyModifier, client.window);
2081 if (!focused) {
2082 _ = xlib.XGrabButton(
2083 display.handle,
2084 xlib.AnyButton,
2085 xlib.AnyModifier,
2086 client.window,
2087 xlib.False,
2088 xlib.ButtonPressMask | xlib.ButtonReleaseMask,
2089 xlib.GrabModeSync,
2090 xlib.GrabModeSync,
2091 xlib.None,
2092 xlib.None,
2093 );
2094 }
2095 for (config.buttons.items) |button| {
2096 if (button.click == .client_win) {
2097 for (modifiers) |modifier| {
2098 _ = xlib.XGrabButton(
2099 display.handle,
2100 @intCast(button.button),
2101 button.mod_mask | modifier,
2102 client.window,
2103 xlib.False,
2104 xlib.ButtonPressMask | xlib.ButtonReleaseMask,
2105 xlib.GrabModeAsync,
2106 xlib.GrabModeSync,
2107 xlib.None,
2108 xlib.None,
2109 );
2110 }
2111 }
2112 }
2113 }
2114
2115 fn focus(display: *Display, target_client: ?*Client) void {
2116 const selmon = monitor_mod.selected_monitor orelse return;
2117
2118 var focus_client = target_client;
2119 if (focus_client == null or !client_mod.is_visible(focus_client.?)) {
2120 focus_client = selmon.stack;
2121 while (focus_client) |iter| {
2122 if (client_mod.is_visible(iter)) break;
2123 focus_client = iter.stack_next;
2124 }
2125 }
2126
2127 if (selmon.sel != null and selmon.sel != focus_client) {
2128 unfocus_client(display, selmon.sel, false);
2129 }
2130
2131 if (focus_client) |client| {
2132 if (client.monitor != selmon) {
2133 monitor_mod.selected_monitor = client.monitor;
2134 }
2135 if (client.is_urgent) {
2136 set_urgent(display, client, false);
2137 }
2138 client_mod.detach_stack(client);
2139 client_mod.attach_stack(client);
2140 grabbuttons(display, client, true);
2141 _ = xlib.XSetWindowBorder(display.handle, client.window, border_color_focused);
2142 if (!client.never_focus) {
2143 _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime);
2144 _ = xlib.XChangeProperty(display.handle, display.root, atoms.net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
2145 }
2146 _ = send_event(display, client, atoms.wm_take_focus);
2147 } else {
2148 _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
2149 _ = xlib.XDeleteProperty(display.handle, display.root, atoms.net_active_window);
2150 }
2151
2152 const current_selmon = monitor_mod.selected_monitor orelse return;
2153 current_selmon.sel = focus_client;
2154
2155 if (focus_client) |client| {
2156 if (is_scrolling_layout(current_selmon)) {
2157 scroll_to_window(client, true);
2158 }
2159 }
2160
2161 bar_mod.invalidate_bars();
2162 }
2163
2164 fn restack(display: *Display, monitor: *Monitor) void {
2165 bar_mod.invalidate_bars();
2166 const selected_client = monitor.sel orelse return;
2167
2168 if (selected_client.is_floating or monitor.lt[monitor.sel_lt] == null) {
2169 _ = xlib.XRaiseWindow(display.handle, selected_client.window);
2170 }
2171
2172 if (monitor.lt[monitor.sel_lt] != null) {
2173 var window_changes: xlib.c.XWindowChanges = undefined;
2174 window_changes.stack_mode = xlib.c.Below;
2175 window_changes.sibling = monitor.bar_win;
2176
2177 var current = monitor.stack;
2178 while (current) |client| {
2179 if (!client.is_floating and client_mod.is_visible(client)) {
2180 _ = xlib.c.XConfigureWindow(display.handle, client.window, xlib.c.CWSibling | xlib.c.CWStackMode, &window_changes);
2181 window_changes.sibling = client.window;
2182 }
2183 current = client.stack_next;
2184 }
2185 }
2186
2187 _ = xlib.XSync(display.handle, xlib.False);
2188
2189 var discard_event: xlib.XEvent = undefined;
2190 while (xlib.c.XCheckMaskEvent(display.handle, xlib.EnterWindowMask, &discard_event) != 0) {}
2191 }
2192
2193 fn arrange(monitor: *Monitor) void {
2194 if (display_global) |display| {
2195 showhide(display, monitor);
2196 }
2197 if (monitor.lt[monitor.sel_lt]) |layout| {
2198 if (layout.arrange_fn) |arrange_fn| {
2199 arrange_fn(monitor);
2200 }
2201 }
2202 if (display_global) |display| {
2203 restack(display, monitor);
2204 }
2205 }
2206
2207 fn tick_animations() void {
2208 if (!scroll_animation.is_active()) return;
2209
2210 const monitor = monitor_mod.selected_monitor orelse return;
2211 if (scroll_animation.update()) |new_offset| {
2212 monitor.scroll_offset = new_offset;
2213 arrange(monitor);
2214 }
2215 }
2216
2217 fn is_scrolling_layout(monitor: *Monitor) bool {
2218 if (monitor.lt[monitor.sel_lt]) |layout| {
2219 return layout.arrange_fn == scrolling.layout.arrange_fn;
2220 }
2221 return false;
2222 }
2223
2224 fn scroll_layout(direction: i32) void {
2225 const monitor = monitor_mod.selected_monitor orelse return;
2226 if (!is_scrolling_layout(monitor)) return;
2227
2228 const scroll_step = scrolling.get_scroll_step(monitor);
2229 const max_scroll = scrolling.get_max_scroll(monitor);
2230
2231 const current = if (scroll_animation.is_active())
2232 scroll_animation.target()
2233 else
2234 monitor.scroll_offset;
2235
2236 var target = current + direction * scroll_step;
2237 target = @max(0, @min(target, max_scroll));
2238
2239 scroll_animation.start(monitor.scroll_offset, target, animation_config);
2240 }
2241
2242 fn scroll_to_window(client: *Client, animate: bool) void {
2243 const monitor = client.monitor orelse return;
2244 if (!is_scrolling_layout(monitor)) return;
2245
2246 const target = scrolling.get_target_scroll_for_window(monitor, client);
2247
2248 if (animate) {
2249 scroll_animation.start(monitor.scroll_offset, target, animation_config);
2250 } else {
2251 monitor.scroll_offset = target;
2252 arrange(monitor);
2253 }
2254 }
2255
2256 fn showhide_client(display: *Display, client: ?*Client) void {
2257 const target = client orelse return;
2258 if (client_mod.is_visible(target)) {
2259 _ = xlib.XMoveWindow(display.handle, target.window, target.x, target.y);
2260 const monitor = target.monitor orelse return;
2261 if ((monitor.lt[monitor.sel_lt] == null or target.is_floating) and !target.is_fullscreen) {
2262 tiling.resize(target, target.x, target.y, target.width, target.height, false);
2263 }
2264 showhide_client(display, target.stack_next);
2265 } else {
2266 showhide_client(display, target.stack_next);
2267 const client_width = target.width + 2 * target.border_width;
2268 _ = xlib.XMoveWindow(display.handle, target.window, -2 * client_width, target.y);
2269 }
2270 }
2271
2272 fn showhide(display: *Display, monitor: *Monitor) void {
2273 showhide_client(display, monitor.stack);
2274 }
2275
2276 fn update_size_hints(display: *Display, client: *Client) void {
2277 var size_hints: xlib.XSizeHints = undefined;
2278 var msize: c_long = 0;
2279
2280 if (xlib.XGetWMNormalHints(display.handle, client.window, &size_hints, &msize) == 0) {
2281 size_hints.flags = xlib.PSize;
2282 }
2283
2284 if ((size_hints.flags & xlib.PBaseSize) != 0) {
2285 client.base_width = size_hints.base_width;
2286 client.base_height = size_hints.base_height;
2287 } else if ((size_hints.flags & xlib.PMinSize) != 0) {
2288 client.base_width = size_hints.min_width;
2289 client.base_height = size_hints.min_height;
2290 } else {
2291 client.base_width = 0;
2292 client.base_height = 0;
2293 }
2294
2295 if ((size_hints.flags & xlib.PResizeInc) != 0) {
2296 client.increment_width = size_hints.width_inc;
2297 client.increment_height = size_hints.height_inc;
2298 } else {
2299 client.increment_width = 0;
2300 client.increment_height = 0;
2301 }
2302
2303 if ((size_hints.flags & xlib.PMaxSize) != 0) {
2304 client.max_width = size_hints.max_width;
2305 client.max_height = size_hints.max_height;
2306 } else {
2307 client.max_width = 0;
2308 client.max_height = 0;
2309 }
2310
2311 if ((size_hints.flags & xlib.PMinSize) != 0) {
2312 client.min_width = size_hints.min_width;
2313 client.min_height = size_hints.min_height;
2314 } else if ((size_hints.flags & xlib.PBaseSize) != 0) {
2315 client.min_width = size_hints.base_width;
2316 client.min_height = size_hints.base_height;
2317 } else {
2318 client.min_width = 0;
2319 client.min_height = 0;
2320 }
2321
2322 if ((size_hints.flags & xlib.PAspect) != 0) {
2323 client.min_aspect = @as(f32, @floatFromInt(size_hints.min_aspect.y)) / @as(f32, @floatFromInt(size_hints.min_aspect.x));
2324 client.max_aspect = @as(f32, @floatFromInt(size_hints.max_aspect.x)) / @as(f32, @floatFromInt(size_hints.max_aspect.y));
2325 } else {
2326 client.min_aspect = 0.0;
2327 client.max_aspect = 0.0;
2328 }
2329
2330 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);
2331 client.hints_valid = true;
2332 }
2333
2334 fn update_wm_hints(display: *Display, client: *Client) void {
2335 const wmh = xlib.XGetWMHints(display.handle, client.window);
2336 if (wmh) |hints| {
2337 defer _ = xlib.XFree(@ptrCast(hints));
2338
2339 if (client == (monitor_mod.selected_monitor orelse return).sel and (hints.*.flags & xlib.XUrgencyHint) != 0) {
2340 hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint);
2341 _ = xlib.XSetWMHints(display.handle, client.window, hints);
2342 } else {
2343 client.is_urgent = (hints.*.flags & xlib.XUrgencyHint) != 0;
2344 }
2345
2346 if ((hints.*.flags & xlib.InputHint) != 0) {
2347 client.never_focus = hints.*.input == 0;
2348 } else {
2349 client.never_focus = false;
2350 }
2351 }
2352 }
2353
2354 fn update_window_type(display: *Display, client: *Client) void {
2355 const state = get_atom_prop(display, client, atoms.net_wm_state);
2356 const window_type = get_atom_prop(display, client, atoms.net_wm_window_type);
2357
2358 if (state == atoms.net_wm_state_fullscreen) {
2359 set_fullscreen(display, client, true);
2360 }
2361 if (window_type == atoms.net_wm_window_type_dialog) {
2362 client.is_floating = true;
2363 }
2364 }
2365
2366 fn update_title(display: *Display, client: *Client) void {
2367 if (!get_text_prop(display, client.window, atoms.net_wm_name, &client.name)) {
2368 _ = get_text_prop(display, client.window, xlib.XA_WM_NAME, &client.name);
2369 }
2370 if (client.name[0] == 0) {
2371 @memcpy(client.name[0..6], "broken");
2372 }
2373 }
2374
2375 fn get_atom_prop(display: *Display, client: *Client, prop: xlib.Atom) xlib.Atom {
2376 var actual_type: xlib.Atom = undefined;
2377 var actual_format: c_int = undefined;
2378 var num_items: c_ulong = undefined;
2379 var bytes_after: c_ulong = undefined;
2380 var prop_data: [*c]u8 = undefined;
2381
2382 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) {
2383 const atom: xlib.Atom = @as(*xlib.Atom, @ptrCast(@alignCast(prop_data))).*;
2384 _ = xlib.XFree(@ptrCast(prop_data));
2385 return atom;
2386 }
2387 return 0;
2388 }
2389
2390 fn get_text_prop(display: *Display, window: xlib.Window, atom: xlib.Atom, text: *[256]u8) bool {
2391 var name: xlib.XTextProperty = undefined;
2392 text[0] = 0;
2393
2394 if (xlib.XGetTextProperty(display.handle, window, &name, atom) == 0 or name.nitems == 0) {
2395 return false;
2396 }
2397
2398 if (name.encoding == xlib.XA_STRING) {
2399 const len = @min(name.nitems, 255);
2400 @memcpy(text[0..len], name.value[0..len]);
2401 text[len] = 0;
2402 } else {
2403 var list: [*c][*c]u8 = undefined;
2404 var count: c_int = undefined;
2405 if (xlib.XmbTextPropertyToTextList(display.handle, &name, &list, &count) >= xlib.Success and count > 0 and list[0] != null) {
2406 const str = std.mem.sliceTo(list[0], 0);
2407 const copy_len = @min(str.len, 255);
2408 @memcpy(text[0..copy_len], str[0..copy_len]);
2409 text[copy_len] = 0;
2410 xlib.XFreeStringList(list);
2411 }
2412 }
2413 text[255] = 0;
2414 _ = xlib.XFree(@ptrCast(name.value));
2415 return true;
2416 }
2417
2418 fn apply_rules(display: *Display, client: *Client) void {
2419 var class_hint: xlib.XClassHint = .{ .res_name = null, .res_class = null };
2420 _ = xlib.XGetClassHint(display.handle, client.window, &class_hint);
2421
2422 const class_str: []const u8 = if (class_hint.res_class) |ptr| std.mem.sliceTo(ptr, 0) else "";
2423 const instance_str: []const u8 = if (class_hint.res_name) |ptr| std.mem.sliceTo(ptr, 0) else "";
2424
2425 client.is_floating = false;
2426 client.tags = 0;
2427 var rule_focus = false;
2428
2429 for (config.rules.items) |rule| {
2430 const class_matches = if (rule.class) |rc| std.mem.indexOf(u8, class_str, rc) != null else true;
2431 const instance_matches = if (rule.instance) |ri| std.mem.indexOf(u8, instance_str, ri) != null else true;
2432 const title_matches = if (rule.title) |rt| std.mem.indexOf(u8, std.mem.sliceTo(&client.name, 0), rt) != null else true;
2433
2434 if (class_matches and instance_matches and title_matches) {
2435 client.is_floating = rule.is_floating;
2436 client.tags |= rule.tags;
2437 if (rule.monitor >= 0) {
2438 var target = monitor_mod.monitors;
2439 var index: i32 = 0;
2440 while (target) |mon| {
2441 if (index == rule.monitor) {
2442 client.monitor = mon;
2443 break;
2444 }
2445 index += 1;
2446 target = mon.next;
2447 }
2448 }
2449 if (rule.focus) {
2450 rule_focus = true;
2451 }
2452 }
2453 }
2454
2455 if (class_hint.res_class) |ptr| {
2456 _ = xlib.XFree(@ptrCast(ptr));
2457 }
2458 if (class_hint.res_name) |ptr| {
2459 _ = xlib.XFree(@ptrCast(ptr));
2460 }
2461
2462 const monitor = client.monitor orelse return;
2463 if (client.tags == 0) {
2464 client.tags = monitor.tagset[monitor.sel_tags];
2465 }
2466
2467 if (rule_focus and client.tags != 0) {
2468 const monitor_tagset = monitor.tagset[monitor.sel_tags];
2469 const is_tag_focused = (monitor_tagset & client.tags) == client.tags;
2470 if (!is_tag_focused) {
2471 view(display, client.tags);
2472 }
2473 }
2474 }
2475
2476 fn update_client_list(display: *Display) void {
2477 _ = xlib.XDeleteProperty(display.handle, display.root, atoms.net_client_list);
2478
2479 var current_monitor = monitor_mod.monitors;
2480 while (current_monitor) |monitor| {
2481 var current_client = monitor.clients;
2482 while (current_client) |client| {
2483 _ = xlib.XChangeProperty(display.handle, display.root, atoms.net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
2484 current_client = client.next;
2485 }
2486 current_monitor = monitor.next;
2487 }
2488 }
2489
2490 fn send_event(display: *Display, client: *Client, protocol: xlib.Atom) bool {
2491 var protocols: [*c]xlib.Atom = undefined;
2492 var num_protocols: c_int = 0;
2493 var exists = false;
2494
2495 if (xlib.XGetWMProtocols(display.handle, client.window, &protocols, &num_protocols) != 0) {
2496 var index: usize = 0;
2497 while (index < @as(usize, @intCast(num_protocols))) : (index += 1) {
2498 if (protocols[index] == protocol) {
2499 exists = true;
2500 break;
2501 }
2502 }
2503 _ = xlib.XFree(@ptrCast(protocols));
2504 }
2505
2506 if (exists) {
2507 var event: xlib.XEvent = undefined;
2508 event.type = xlib.ClientMessage;
2509 event.xclient.window = client.window;
2510 event.xclient.message_type = atoms.wm_protocols;
2511 event.xclient.format = 32;
2512 event.xclient.data.l[0] = @intCast(protocol);
2513 event.xclient.data.l[1] = xlib.CurrentTime;
2514 _ = xlib.XSendEvent(display.handle, client.window, xlib.False, xlib.NoEventMask, &event);
2515 }
2516 return exists;
2517 }
2518
2519 fn set_urgent(display: *Display, client: *Client, urgent: bool) void {
2520 client.is_urgent = urgent;
2521 const wmh = xlib.XGetWMHints(display.handle, client.window);
2522 if (wmh) |hints| {
2523 if (urgent) {
2524 hints.*.flags = hints.*.flags | xlib.XUrgencyHint;
2525 } else {
2526 hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint);
2527 }
2528 _ = xlib.XSetWMHints(display.handle, client.window, hints);
2529 _ = xlib.XFree(@ptrCast(hints));
2530 }
2531 }
2532
2533 fn run_autostart_commands(_: std.mem.Allocator, commands: []const []const u8) !void {
2534 for (commands) |cmd| spawn_command(cmd);
2535 }
2536
2537 test {
2538 _ = @import("x11/events.zig");
2539 }