tony / oxwm

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