tony / oxwm

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