tony / oxwm

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