tony / oxwm

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