tony / oxwm

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