tony / oxwm

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