tony / oxwm

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