tony / oxwm

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