tony / oxwm

Branch: e95e4af
Switch branches/tags
master
tonybanters Zig port. e95e4af 2026-02-02
2291 lines · 81.9 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 .focus_next => focusstack(display, 1),
739 .focus_prev => focusstack(display, -1),
740 .move_next => movestack(display, 1),
741 .move_prev => movestack(display, -1),
742 .resize_master => setmfact(@as(f32, @floatFromInt(int_arg)) / 1000.0),
743 .inc_master => incnmaster(1),
744 .dec_master => incnmaster(-1),
745 .toggle_floating => toggle_floating(display),
746 .toggle_fullscreen => toggle_fullscreen(display),
747 .toggle_gaps => toggle_gaps(),
748 .cycle_layout => cycle_layout(),
749 .set_layout => set_layout(str_arg),
750 .set_layout_tiling => {},
751 .set_layout_floating => {},
752 .view_tag => {
753 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
754 view(display, tag_mask);
755 },
756 .view_next_tag => view_adjacent_tag(display, 1),
757 .view_prev_tag => view_adjacent_tag(display, -1),
758 .view_next_nonempty_tag => view_adjacent_nonempty_tag(display, 1),
759 .view_prev_nonempty_tag => view_adjacent_nonempty_tag(display, -1),
760 .move_to_tag => {
761 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
762 tag_client(display, tag_mask);
763 },
764 .toggle_view_tag => {
765 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
766 toggle_view(display, tag_mask);
767 },
768 .toggle_tag => {
769 const tag_mask: u32 = @as(u32, 1) << @intCast(int_arg);
770 toggle_client_tag(display, tag_mask);
771 },
772 .focus_monitor => focusmon(display, int_arg),
773 .send_to_monitor => sendmon(display, int_arg),
774 .scroll_left => {
775 scroll_layout(-1);
776 },
777 .scroll_right => {
778 scroll_layout(1);
779 },
780 }
781 }
782
783 fn reload_config(display: *Display) void {
784 std.debug.print("reloading config...\n", .{});
785
786 ungrab_keybinds(display);
787
788 config.keybinds.clearRetainingCapacity();
789 config.rules.clearRetainingCapacity();
790 config.blocks.clearRetainingCapacity();
791
792 lua.deinit();
793 _ = lua.init(&config);
794
795 const loaded = if (config_path_global) |path|
796 lua.load_file(path)
797 else
798 lua.load_config();
799
800 if (loaded) {
801 if (config_path_global) |path| {
802 std.debug.print("reloaded config from {s}\n", .{path});
803 } else {
804 std.debug.print("reloaded config from ~/.config/oxwm/config.lua\n", .{});
805 }
806 apply_config_values();
807 } else {
808 std.debug.print("reload failed, restoring defaults\n", .{});
809 setup_default_keybinds();
810 }
811
812 bar_mod.destroy_bars(gpa.allocator(), display.handle);
813 setup_bars(gpa.allocator(), display);
814 rebuild_bar_blocks();
815
816 grab_keybinds(display);
817 }
818
819 fn rebuild_bar_blocks() void {
820 var current_bar = bar_mod.bars;
821 while (current_bar) |bar| {
822 bar.clear_blocks();
823 if (config.blocks.items.len > 0) {
824 for (config.blocks.items) |cfg_block| {
825 const block = config_block_to_bar_block(cfg_block);
826 bar.add_block(block);
827 }
828 } else {
829 bar.add_block(blocks_mod.Block.init_ram("", 5, 0x7aa2f7, true));
830 bar.add_block(blocks_mod.Block.init_static(" | ", 0x666666, false));
831 bar.add_block(blocks_mod.Block.init_datetime("", "%H:%M", 1, 0x0db9d7, true));
832 }
833 current_bar = bar.next;
834 }
835 }
836
837 fn ungrab_keybinds(display: *Display) void {
838 _ = xlib.XUngrabKey(display.handle, xlib.AnyKey, xlib.AnyModifier, display.root);
839 }
840
841 fn spawn_child_setup() void {
842 _ = std.c.setsid();
843 if (display_global) |display| {
844 const display_fd = xlib.XConnectionNumber(display.handle);
845 std.posix.close(@intCast(display_fd));
846 }
847 const sigchld_handler = std.posix.Sigaction{
848 .handler = .{ .handler = std.posix.SIG.DFL },
849 .mask = std.mem.zeroes(std.posix.sigset_t),
850 .flags = 0,
851 };
852 std.posix.sigaction(std.posix.SIG.CHLD, &sigchld_handler, null);
853 }
854
855 fn spawn_command(cmd: []const u8) void {
856 const pid = std.posix.fork() catch return;
857 if (pid == 0) {
858 spawn_child_setup();
859 var cmd_buf: [1024]u8 = undefined;
860 if (cmd.len >= cmd_buf.len) {
861 std.posix.exit(1);
862 }
863 @memcpy(cmd_buf[0..cmd.len], cmd);
864 cmd_buf[cmd.len] = 0;
865 const argv = [_:null]?[*:0]const u8{ "sh", "-c", @ptrCast(&cmd_buf) };
866 _ = std.posix.execvpeZ("sh", &argv, std.c.environ) catch {};
867 std.posix.exit(1);
868 }
869 }
870
871 fn spawn_terminal() void {
872 const pid = std.posix.fork() catch return;
873 if (pid == 0) {
874 spawn_child_setup();
875 var term_buf: [256]u8 = undefined;
876 const terminal = config.terminal;
877 if (terminal.len >= term_buf.len) {
878 std.posix.exit(1);
879 }
880 @memcpy(term_buf[0..terminal.len], terminal);
881 term_buf[terminal.len] = 0;
882 const term_ptr: [*:0]const u8 = @ptrCast(&term_buf);
883 const argv = [_:null]?[*:0]const u8{term_ptr};
884 _ = std.posix.execvpeZ(term_ptr, &argv, std.c.environ) catch {};
885 std.posix.exit(1);
886 }
887 }
888
889 fn movestack(display: *Display, direction: i32) void {
890 const monitor = monitor_mod.selected_monitor orelse return;
891 const current = monitor.sel orelse return;
892
893 if (current.is_floating) {
894 return;
895 }
896
897 var target: ?*Client = null;
898
899 if (direction > 0) {
900 target = current.next;
901 while (target) |client| {
902 if (client_mod.is_visible(client) and !client.is_floating) {
903 break;
904 }
905 target = client.next;
906 }
907 if (target == null) {
908 target = monitor.clients;
909 while (target) |client| {
910 if (client == current) {
911 break;
912 }
913 if (client_mod.is_visible(client) and !client.is_floating) {
914 break;
915 }
916 target = client.next;
917 }
918 }
919 } else {
920 var prev: ?*Client = null;
921 var iter = monitor.clients;
922 while (iter) |client| {
923 if (client == current) {
924 break;
925 }
926 if (client_mod.is_visible(client) and !client.is_floating) {
927 prev = client;
928 }
929 iter = client.next;
930 }
931 if (prev == null) {
932 iter = current.next;
933 while (iter) |client| {
934 if (client_mod.is_visible(client) and !client.is_floating) {
935 prev = client;
936 }
937 iter = client.next;
938 }
939 }
940 target = prev;
941 }
942
943 if (target) |swap_client| {
944 if (swap_client != current) {
945 client_mod.swap_clients(current, swap_client);
946 arrange(monitor);
947 focus(display, current);
948 }
949 }
950 }
951
952 fn toggle_view(display: *Display, tag_mask: u32) void {
953 const monitor = monitor_mod.selected_monitor orelse return;
954 const new_tags = monitor.tagset[monitor.sel_tags] ^ tag_mask;
955 if (new_tags != 0) {
956 monitor.tagset[monitor.sel_tags] = new_tags;
957
958 if (new_tags == ~@as(u32, 0)) {
959 monitor.pertag.prevtag = monitor.pertag.curtag;
960 monitor.pertag.curtag = 0;
961 }
962
963 if ((new_tags & (@as(u32, 1) << @intCast(monitor.pertag.curtag -| 1))) == 0) {
964 monitor.pertag.prevtag = monitor.pertag.curtag;
965 var i: u32 = 0;
966 while (i < 9) : (i += 1) {
967 if ((new_tags & (@as(u32, 1) << @intCast(i))) != 0) break;
968 }
969 monitor.pertag.curtag = i + 1;
970 }
971
972 monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag];
973 monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag];
974 monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag];
975
976 focus_top_client(display, monitor);
977 arrange(monitor);
978 bar_mod.invalidate_bars();
979 }
980 }
981
982 fn toggle_client_tag(display: *Display, tag_mask: u32) void {
983 const monitor = monitor_mod.selected_monitor orelse return;
984 const client = monitor.sel orelse return;
985 const new_tags = client.tags ^ tag_mask;
986 if (new_tags != 0) {
987 client.tags = new_tags;
988 focus_top_client(display, monitor);
989 arrange(monitor);
990 bar_mod.invalidate_bars();
991 }
992 }
993
994 fn toggle_gaps() void {
995 const monitor = monitor_mod.selected_monitor orelse return;
996 if (monitor.gap_inner_h == 0) {
997 monitor.gap_inner_h = gap_inner_v;
998 monitor.gap_inner_v = gap_inner_v;
999 monitor.gap_outer_h = gap_outer_h;
1000 monitor.gap_outer_v = gap_outer_v;
1001 } else {
1002 monitor.gap_inner_h = 0;
1003 monitor.gap_inner_v = 0;
1004 monitor.gap_outer_h = 0;
1005 monitor.gap_outer_v = 0;
1006 }
1007 arrange(monitor);
1008 }
1009
1010 fn kill_focused(display: *Display) void {
1011 const selected = monitor_mod.selected_monitor orelse return;
1012 const client = selected.sel orelse return;
1013 std.debug.print("killing window: 0x{x}\n", .{client.window});
1014
1015 if (!send_event(display, client, wm_delete)) {
1016 _ = xlib.XGrabServer(display.handle);
1017 _ = xlib.XKillClient(display.handle, client.window);
1018 _ = xlib.XSync(display.handle, xlib.False);
1019 _ = xlib.XUngrabServer(display.handle);
1020 }
1021 }
1022
1023 fn toggle_fullscreen(display: *Display) void {
1024 const selected = monitor_mod.selected_monitor orelse return;
1025 const client = selected.sel orelse return;
1026 set_fullscreen(display, client, !client.is_fullscreen);
1027 }
1028
1029 fn set_fullscreen(display: *Display, client: *Client, fullscreen: bool) void {
1030 const monitor = client.monitor orelse return;
1031
1032 if (fullscreen and !client.is_fullscreen) {
1033 var fullscreen_atom = net_wm_state_fullscreen;
1034 _ = xlib.XChangeProperty(
1035 display.handle,
1036 client.window,
1037 net_wm_state,
1038 xlib.XA_ATOM,
1039 32,
1040 xlib.PropModeReplace,
1041 @ptrCast(&fullscreen_atom),
1042 1,
1043 );
1044 client.is_fullscreen = true;
1045 client.old_state = client.is_floating;
1046 client.old_border_width = client.border_width;
1047 client.border_width = 0;
1048 client.is_floating = true;
1049
1050 _ = xlib.XSetWindowBorderWidth(display.handle, client.window, 0);
1051 tiling.resize_client(client, monitor.mon_x, monitor.mon_y, monitor.mon_w, monitor.mon_h);
1052 _ = xlib.XRaiseWindow(display.handle, client.window);
1053
1054 std.debug.print("fullscreen enabled: window=0x{x}\n", .{client.window});
1055 } else if (!fullscreen and client.is_fullscreen) {
1056 var no_atom: xlib.Atom = 0;
1057 _ = xlib.XChangeProperty(
1058 display.handle,
1059 client.window,
1060 net_wm_state,
1061 xlib.XA_ATOM,
1062 32,
1063 xlib.PropModeReplace,
1064 @ptrCast(&no_atom),
1065 0,
1066 );
1067 client.is_fullscreen = false;
1068 client.is_floating = client.old_state;
1069 client.border_width = client.old_border_width;
1070
1071 client.x = client.old_x;
1072 client.y = client.old_y;
1073 client.width = client.old_width;
1074 client.height = client.old_height;
1075
1076 tiling.resize_client(client, client.x, client.y, client.width, client.height);
1077 arrange(monitor);
1078
1079 std.debug.print("fullscreen disabled: window=0x{x}\n", .{client.window});
1080 }
1081 }
1082
1083 fn view(display: *Display, tag_mask: u32) void {
1084 const monitor = monitor_mod.selected_monitor orelse return;
1085 if (tag_mask == monitor.tagset[monitor.sel_tags]) {
1086 return;
1087 }
1088 monitor.sel_tags ^= 1;
1089 if (tag_mask != 0) {
1090 monitor.tagset[monitor.sel_tags] = tag_mask;
1091 monitor.pertag.prevtag = monitor.pertag.curtag;
1092
1093 if (tag_mask == ~@as(u32, 0)) {
1094 monitor.pertag.curtag = 0;
1095 } else {
1096 var i: u32 = 0;
1097 while (i < 9) : (i += 1) {
1098 if ((tag_mask & (@as(u32, 1) << @intCast(i))) != 0) break;
1099 }
1100 monitor.pertag.curtag = i + 1;
1101 }
1102 } else {
1103 const tmp = monitor.pertag.prevtag;
1104 monitor.pertag.prevtag = monitor.pertag.curtag;
1105 monitor.pertag.curtag = tmp;
1106 }
1107
1108 monitor.nmaster = monitor.pertag.nmasters[monitor.pertag.curtag];
1109 monitor.mfact = monitor.pertag.mfacts[monitor.pertag.curtag];
1110 monitor.sel_lt = monitor.pertag.sellts[monitor.pertag.curtag];
1111
1112 focus_top_client(display, monitor);
1113 arrange(monitor);
1114 bar_mod.invalidate_bars();
1115 std.debug.print("view: tag_mask={d}\n", .{monitor.tagset[monitor.sel_tags]});
1116 }
1117
1118 fn view_adjacent_tag(display: *Display, direction: i32) void {
1119 const monitor = monitor_mod.selected_monitor orelse return;
1120 const current_tag = monitor.pertag.curtag;
1121 var new_tag: i32 = @intCast(current_tag);
1122
1123 new_tag += direction;
1124 if (new_tag < 1) new_tag = 9;
1125 if (new_tag > 9) new_tag = 1;
1126
1127 const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
1128 view(display, tag_mask);
1129 }
1130
1131 fn view_adjacent_nonempty_tag(display: *Display, direction: i32) void {
1132 const monitor = monitor_mod.selected_monitor orelse return;
1133 const current_tag = monitor.pertag.curtag;
1134 var new_tag: i32 = @intCast(current_tag);
1135
1136 var attempts: i32 = 0;
1137 while (attempts < 9) : (attempts += 1) {
1138 new_tag += direction;
1139 if (new_tag < 1) new_tag = 9;
1140 if (new_tag > 9) new_tag = 1;
1141
1142 const tag_mask: u32 = @as(u32, 1) << @intCast(new_tag - 1);
1143 if (has_clients_on_tag(monitor, tag_mask)) {
1144 view(display, tag_mask);
1145 return;
1146 }
1147 }
1148 }
1149
1150 fn has_clients_on_tag(monitor: *monitor_mod.Monitor, tag_mask: u32) bool {
1151 var client = monitor.clients;
1152 while (client) |c| {
1153 if ((c.tags & tag_mask) != 0) {
1154 return true;
1155 }
1156 client = c.next;
1157 }
1158 return false;
1159 }
1160
1161 fn tag_client(display: *Display, tag_mask: u32) void {
1162 const monitor = monitor_mod.selected_monitor orelse return;
1163 const client = monitor.sel orelse return;
1164 if (tag_mask == 0) {
1165 return;
1166 }
1167 client.tags = tag_mask;
1168 focus_top_client(display, monitor);
1169 arrange(monitor);
1170 bar_mod.invalidate_bars();
1171 std.debug.print("tag_client: window=0x{x} tag_mask={d}\n", .{ client.window, tag_mask });
1172 }
1173
1174 fn focus_top_client(display: *Display, monitor: *Monitor) void {
1175 var visible_client = monitor.stack;
1176 while (visible_client) |client| {
1177 if (client_mod.is_visible(client)) {
1178 focus(display, client);
1179 return;
1180 }
1181 visible_client = client.stack_next;
1182 }
1183 monitor.sel = null;
1184 _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
1185 }
1186
1187 fn focusstack(display: *Display, direction: i32) void {
1188 const monitor = monitor_mod.selected_monitor orelse return;
1189 const current = monitor.sel orelse return;
1190
1191 var next_client: ?*Client = null;
1192
1193 if (direction > 0) {
1194 next_client = current.next;
1195 while (next_client) |client| {
1196 if (client_mod.is_visible(client)) {
1197 break;
1198 }
1199 next_client = client.next;
1200 }
1201 if (next_client == null) {
1202 next_client = monitor.clients;
1203 while (next_client) |client| {
1204 if (client_mod.is_visible(client)) {
1205 break;
1206 }
1207 next_client = client.next;
1208 }
1209 }
1210 } else {
1211 var prev: ?*Client = null;
1212 var iter = monitor.clients;
1213 while (iter) |client| {
1214 if (client == current) {
1215 break;
1216 }
1217 if (client_mod.is_visible(client)) {
1218 prev = client;
1219 }
1220 iter = client.next;
1221 }
1222 if (prev == null) {
1223 iter = current.next;
1224 while (iter) |client| {
1225 if (client_mod.is_visible(client)) {
1226 prev = client;
1227 }
1228 iter = client.next;
1229 }
1230 }
1231 next_client = prev;
1232 }
1233
1234 if (next_client) |client| {
1235 focus(display, client);
1236 if (client.monitor) |client_monitor| {
1237 restack(display, client_monitor);
1238 }
1239 }
1240 }
1241
1242 fn toggle_floating(_: *Display) void {
1243 const monitor = monitor_mod.selected_monitor orelse return;
1244 const client = monitor.sel orelse return;
1245
1246 if (client.is_fullscreen) {
1247 return;
1248 }
1249
1250 client.is_floating = !client.is_floating;
1251
1252 if (client.is_floating) {
1253 tiling.resize(client, client.x, client.y, client.width, client.height, false);
1254 }
1255
1256 arrange(monitor);
1257 std.debug.print("toggle_floating: window=0x{x} floating={}\n", .{ client.window, client.is_floating });
1258 }
1259
1260 fn incnmaster(delta: i32) void {
1261 const monitor = monitor_mod.selected_monitor orelse return;
1262 const new_val = @max(0, monitor.nmaster + delta);
1263 monitor.nmaster = new_val;
1264 monitor.pertag.nmasters[monitor.pertag.curtag] = new_val;
1265 arrange(monitor);
1266 std.debug.print("incnmaster: nmaster={d}\n", .{monitor.nmaster});
1267 }
1268
1269 fn setmfact(delta: f32) void {
1270 const monitor = monitor_mod.selected_monitor orelse return;
1271 const new_mfact = monitor.mfact + delta;
1272 if (new_mfact < 0.05 or new_mfact > 0.95) {
1273 return;
1274 }
1275 monitor.mfact = new_mfact;
1276 monitor.pertag.mfacts[monitor.pertag.curtag] = new_mfact;
1277 arrange(monitor);
1278 std.debug.print("setmfact: mfact={d:.2}\n", .{monitor.mfact});
1279 }
1280
1281 fn cycle_layout() void {
1282 const monitor = monitor_mod.selected_monitor orelse return;
1283 const new_lt = (monitor.sel_lt + 1) % 4;
1284 monitor.sel_lt = new_lt;
1285 monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
1286 if (new_lt != 3) {
1287 monitor.scroll_offset = 0;
1288 }
1289 arrange(monitor);
1290 bar_mod.invalidate_bars();
1291 if (monitor.lt[monitor.sel_lt]) |layout| {
1292 std.debug.print("cycle_layout: {s}\n", .{layout.symbol});
1293 }
1294 }
1295
1296 fn set_layout(layout_name: ?[]const u8) void {
1297 const monitor = monitor_mod.selected_monitor orelse return;
1298 const name = layout_name orelse return;
1299
1300 const new_lt: u32 = if (std.mem.eql(u8, name, "tiling") or std.mem.eql(u8, name, "[]="))
1301 0
1302 else if (std.mem.eql(u8, name, "monocle") or std.mem.eql(u8, name, "[M]"))
1303 1
1304 else if (std.mem.eql(u8, name, "floating") or std.mem.eql(u8, name, "><>"))
1305 2
1306 else if (std.mem.eql(u8, name, "scrolling") or std.mem.eql(u8, name, "[S]"))
1307 3
1308 else {
1309 std.debug.print("set_layout: unknown layout '{s}'\n", .{name});
1310 return;
1311 };
1312
1313 monitor.sel_lt = new_lt;
1314 monitor.pertag.sellts[monitor.pertag.curtag] = new_lt;
1315 if (new_lt != 3) {
1316 monitor.scroll_offset = 0;
1317 }
1318 arrange(monitor);
1319 bar_mod.invalidate_bars();
1320 if (monitor.lt[monitor.sel_lt]) |layout| {
1321 std.debug.print("set_layout: {s}\n", .{layout.symbol});
1322 }
1323 }
1324
1325 fn focusmon(display: *Display, direction: i32) void {
1326 const selmon = monitor_mod.selected_monitor orelse return;
1327 const target = monitor_mod.dir_to_monitor(direction) orelse return;
1328 if (target == selmon) {
1329 return;
1330 }
1331 unfocus_client(display, selmon.sel, false);
1332 monitor_mod.selected_monitor = target;
1333 focus(display, null);
1334 std.debug.print("focusmon: monitor {d}\n", .{target.num});
1335 }
1336
1337 fn sendmon(display: *Display, direction: i32) void {
1338 const source_monitor = monitor_mod.selected_monitor orelse return;
1339 const client = source_monitor.sel orelse return;
1340 const target = monitor_mod.dir_to_monitor(direction) orelse return;
1341
1342 if (target == source_monitor) {
1343 return;
1344 }
1345
1346 client_mod.detach(client);
1347 client_mod.detach_stack(client);
1348 client.monitor = target;
1349 client.tags = target.tagset[target.sel_tags];
1350 client_mod.attach_aside(client);
1351 client_mod.attach_stack(client);
1352
1353 focus_top_client(display, source_monitor);
1354 arrange(source_monitor);
1355 arrange(target);
1356
1357 std.debug.print("sendmon: window=0x{x} to monitor {d}\n", .{ client.window, target.num });
1358 }
1359
1360 fn snap_x(client: *Client, new_x: i32, monitor: *Monitor) i32 {
1361 const client_width = client.width + 2 * client.border_width;
1362 if (@abs(monitor.win_x - new_x) < snap_distance) {
1363 return monitor.win_x;
1364 } else if (@abs((monitor.win_x + monitor.win_w) - (new_x + client_width)) < snap_distance) {
1365 return monitor.win_x + monitor.win_w - client_width;
1366 }
1367 return new_x;
1368 }
1369
1370 fn snap_y(client: *Client, new_y: i32, monitor: *Monitor) i32 {
1371 const client_height = client.height + 2 * client.border_width;
1372 if (@abs(monitor.win_y - new_y) < snap_distance) {
1373 return monitor.win_y;
1374 } else if (@abs((monitor.win_y + monitor.win_h) - (new_y + client_height)) < snap_distance) {
1375 return monitor.win_y + monitor.win_h - client_height;
1376 }
1377 return new_y;
1378 }
1379
1380 fn movemouse(display: *Display) void {
1381 const monitor = monitor_mod.selected_monitor orelse return;
1382 const client = monitor.sel orelse return;
1383
1384 if (client.is_fullscreen) {
1385 return;
1386 }
1387
1388 restack(display, monitor);
1389
1390 const was_floating = client.is_floating;
1391 if (!client.is_floating) {
1392 client.is_floating = true;
1393 }
1394
1395 var root_x: c_int = undefined;
1396 var root_y: c_int = undefined;
1397 var dummy_win: xlib.Window = undefined;
1398 var dummy_int: c_int = undefined;
1399 var dummy_uint: c_uint = undefined;
1400
1401 _ = xlib.XQueryPointer(display.handle, display.root, &dummy_win, &dummy_win, &root_x, &root_y, &dummy_int, &dummy_int, &dummy_uint);
1402
1403 const grab_result = xlib.XGrabPointer(
1404 display.handle,
1405 display.root,
1406 xlib.False,
1407 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
1408 xlib.GrabModeAsync,
1409 xlib.GrabModeAsync,
1410 xlib.None,
1411 cursor_move,
1412 xlib.CurrentTime,
1413 );
1414
1415 if (grab_result != xlib.GrabSuccess) {
1416 return;
1417 }
1418
1419 const start_x = client.x;
1420 const start_y = client.y;
1421 const pointer_start_x = root_x;
1422 const pointer_start_y = root_y;
1423 var last_time: c_ulong = 0;
1424
1425 var event: xlib.XEvent = undefined;
1426 var done = false;
1427
1428 while (!done) {
1429 _ = xlib.XNextEvent(display.handle, &event);
1430
1431 switch (event.type) {
1432 xlib.MotionNotify => {
1433 const motion = &event.xmotion;
1434 if ((motion.time - last_time) < (1000 / 60)) {
1435 continue;
1436 }
1437 last_time = motion.time;
1438 const delta_x = motion.x_root - pointer_start_x;
1439 const delta_y = motion.y_root - pointer_start_y;
1440 var new_x = start_x + delta_x;
1441 var new_y = start_y + delta_y;
1442 if (client.monitor) |client_monitor| {
1443 new_x = snap_x(client, new_x, client_monitor);
1444 new_y = snap_y(client, new_y, client_monitor);
1445 }
1446 tiling.resize(client, new_x, new_y, client.width, client.height, true);
1447 },
1448 xlib.ButtonRelease => {
1449 done = true;
1450 },
1451 else => {},
1452 }
1453 }
1454
1455 _ = xlib.XUngrabPointer(display.handle, xlib.CurrentTime);
1456
1457 const new_mon = monitor_mod.rect_to_monitor(client.x, client.y, client.width, client.height);
1458 if (new_mon != null and new_mon != monitor) {
1459 client_mod.detach(client);
1460 client_mod.detach_stack(client);
1461 client.monitor = new_mon;
1462 client.tags = new_mon.?.tagset[new_mon.?.sel_tags];
1463 client_mod.attach_aside(client);
1464 client_mod.attach_stack(client);
1465 monitor_mod.selected_monitor = new_mon;
1466 focus(display, client);
1467 arrange(monitor);
1468 arrange(new_mon.?);
1469 } else {
1470 arrange(monitor);
1471 }
1472
1473 if (config.auto_tile and !was_floating) {
1474 const drop_monitor = client.monitor orelse return;
1475 const center_x = client.x + @divTrunc(client.width, 2);
1476 const center_y = client.y + @divTrunc(client.height, 2);
1477
1478 if (client_mod.tiled_window_at(client, drop_monitor, center_x, center_y)) |target| {
1479 client_mod.insert_before(client, target);
1480 }
1481
1482 client.is_floating = false;
1483 arrange(drop_monitor);
1484 }
1485 }
1486
1487 fn resizemouse(display: *Display) void {
1488 const monitor = monitor_mod.selected_monitor orelse return;
1489 const client = monitor.sel orelse return;
1490
1491 if (client.is_fullscreen) {
1492 return;
1493 }
1494
1495 restack(display, monitor);
1496
1497 if (!client.is_floating) {
1498 client.is_floating = true;
1499 }
1500
1501 const grab_result = xlib.XGrabPointer(
1502 display.handle,
1503 display.root,
1504 xlib.False,
1505 xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask,
1506 xlib.GrabModeAsync,
1507 xlib.GrabModeAsync,
1508 xlib.None,
1509 cursor_resize,
1510 xlib.CurrentTime,
1511 );
1512
1513 if (grab_result != xlib.GrabSuccess) {
1514 return;
1515 }
1516
1517 _ = xlib.XWarpPointer(display.handle, xlib.None, client.window, 0, 0, 0, 0, client.width + client.border_width - 1, client.height + client.border_width - 1);
1518
1519 var event: xlib.XEvent = undefined;
1520 var done = false;
1521 var last_time: c_ulong = 0;
1522
1523 while (!done) {
1524 _ = xlib.XNextEvent(display.handle, &event);
1525
1526 switch (event.type) {
1527 xlib.MotionNotify => {
1528 const motion = &event.xmotion;
1529 if ((motion.time - last_time) < (1000 / 60)) {
1530 continue;
1531 }
1532 last_time = motion.time;
1533 var new_width = @max(1, motion.x_root - client.x - 2 * client.border_width + 1);
1534 var new_height = @max(1, motion.y_root - client.y - 2 * client.border_width + 1);
1535 if (client.monitor) |client_monitor| {
1536 const client_right = client.x + new_width + 2 * client.border_width;
1537 const client_bottom = client.y + new_height + 2 * client.border_width;
1538 const mon_right = client_monitor.win_x + client_monitor.win_w;
1539 const mon_bottom = client_monitor.win_y + client_monitor.win_h;
1540 if (@abs(mon_right - client_right) < snap_distance) {
1541 new_width = @max(1, mon_right - client.x - 2 * client.border_width);
1542 }
1543 if (@abs(mon_bottom - client_bottom) < snap_distance) {
1544 new_height = @max(1, mon_bottom - client.y - 2 * client.border_width);
1545 }
1546 }
1547 tiling.resize(client, client.x, client.y, new_width, new_height, true);
1548 },
1549 xlib.ButtonRelease => {
1550 done = true;
1551 },
1552 else => {},
1553 }
1554 }
1555
1556 _ = xlib.XUngrabPointer(display.handle, xlib.CurrentTime);
1557 arrange(monitor);
1558 }
1559
1560 fn handle_expose(display: *Display, event: *xlib.XExposeEvent) void {
1561 if (event.count != 0) return;
1562
1563 if (bar_mod.window_to_bar(event.window)) |bar| {
1564 bar.invalidate();
1565 bar.draw(display.handle, &tags);
1566 }
1567 }
1568
1569 fn clean_mask(mask: c_uint) c_uint {
1570 const lock: c_uint = @intCast(xlib.LockMask);
1571 const shift: c_uint = @intCast(xlib.ShiftMask);
1572 const ctrl: c_uint = @intCast(xlib.ControlMask);
1573 const mod1: c_uint = @intCast(xlib.Mod1Mask);
1574 const mod2: c_uint = @intCast(xlib.Mod2Mask);
1575 const mod3: c_uint = @intCast(xlib.Mod3Mask);
1576 const mod4: c_uint = @intCast(xlib.Mod4Mask);
1577 const mod5: c_uint = @intCast(xlib.Mod5Mask);
1578 return mask & ~(lock | numlock_mask) & (shift | ctrl | mod1 | mod2 | mod3 | mod4 | mod5);
1579 }
1580
1581 fn handle_button_press(display: *Display, event: *xlib.XButtonEvent) void {
1582 std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow });
1583
1584 const clicked_monitor = monitor_mod.window_to_monitor(event.window);
1585 if (clicked_monitor) |monitor| {
1586 if (monitor != monitor_mod.selected_monitor) {
1587 if (monitor_mod.selected_monitor) |selmon| {
1588 unfocus_client(display, selmon.sel, true);
1589 }
1590 monitor_mod.selected_monitor = monitor;
1591 focus(display, null);
1592 }
1593 }
1594
1595 if (bar_mod.window_to_bar(event.window)) |bar| {
1596 const clicked_tag = bar.handle_click(event.x, &tags);
1597 if (clicked_tag) |tag_index| {
1598 const tag_mask: u32 = @as(u32, 1) << @intCast(tag_index);
1599 view(display, tag_mask);
1600 }
1601 return;
1602 }
1603
1604 const click_client = client_mod.window_to_client(event.window);
1605 if (click_client) |found_client| {
1606 focus(display, found_client);
1607 if (monitor_mod.selected_monitor) |selmon| {
1608 restack(display, selmon);
1609 }
1610 _ = xlib.XAllowEvents(display.handle, xlib.ReplayPointer, xlib.CurrentTime);
1611 }
1612
1613 const clean_state = clean_mask(event.state);
1614 for (config.buttons.items) |button| {
1615 if (button.click != .client_win) continue;
1616 const button_clean_mask = clean_mask(button.mod_mask);
1617 if (clean_state == button_clean_mask and event.button == button.button) {
1618 switch (button.action) {
1619 .move_mouse => movemouse(display),
1620 .resize_mouse => resizemouse(display),
1621 .toggle_floating => {
1622 if (click_client) |found_client| {
1623 found_client.is_floating = !found_client.is_floating;
1624 if (monitor_mod.selected_monitor) |monitor| {
1625 arrange(monitor);
1626 }
1627 }
1628 },
1629 }
1630 return;
1631 }
1632 }
1633 }
1634
1635 fn handle_client_message(display: *Display, event: *xlib.XClientMessageEvent) void {
1636 const client = client_mod.window_to_client(event.window) orelse return;
1637
1638 if (event.message_type == net_wm_state) {
1639 const action = event.data.l[0];
1640 const first_property = @as(xlib.Atom, @intCast(event.data.l[1]));
1641 const second_property = @as(xlib.Atom, @intCast(event.data.l[2]));
1642
1643 if (first_property == net_wm_state_fullscreen or second_property == net_wm_state_fullscreen) {
1644 const net_wm_state_remove = 0;
1645 const net_wm_state_add = 1;
1646 const net_wm_state_toggle = 2;
1647
1648 if (action == net_wm_state_add) {
1649 set_fullscreen(display, client, true);
1650 } else if (action == net_wm_state_remove) {
1651 set_fullscreen(display, client, false);
1652 } else if (action == net_wm_state_toggle) {
1653 set_fullscreen(display, client, !client.is_fullscreen);
1654 }
1655 }
1656 } else if (event.message_type == net_active_window) {
1657 const selected = monitor_mod.selected_monitor orelse return;
1658 if (client != selected.sel and !client.is_urgent) {
1659 set_urgent(display, client, true);
1660 }
1661 }
1662 }
1663
1664 fn handle_destroy_notify(display: *Display, event: *xlib.XDestroyWindowEvent) void {
1665 const client = client_mod.window_to_client(event.window) orelse return;
1666 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
1667 unmanage(display, client);
1668 }
1669
1670 fn handle_unmap_notify(display: *Display, event: *xlib.XUnmapEvent) void {
1671 const client = client_mod.window_to_client(event.window) orelse return;
1672 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
1673 unmanage(display, client);
1674 }
1675
1676 fn unmanage(display: *Display, client: *Client) void {
1677 const client_monitor = client.monitor;
1678
1679 var next_focus: ?*Client = null;
1680 if (client_monitor) |monitor| {
1681 if (monitor.sel == client and is_scrolling_layout(monitor)) {
1682 next_focus = client.next;
1683 if (next_focus == null) {
1684 var prev: ?*Client = null;
1685 var iter = monitor.clients;
1686 while (iter) |c| {
1687 if (c == client) break;
1688 prev = c;
1689 iter = c.next;
1690 }
1691 next_focus = prev;
1692 }
1693 }
1694 }
1695
1696 client_mod.detach(client);
1697 client_mod.detach_stack(client);
1698
1699 if (client_monitor) |monitor| {
1700 if (monitor.sel == client) {
1701 monitor.sel = if (next_focus) |nf| nf else monitor.stack;
1702 }
1703 if (is_scrolling_layout(monitor)) {
1704 const target = if (monitor.sel) |sel| scrolling.get_target_scroll_for_window(monitor, sel) else 0;
1705 if (target == 0) {
1706 monitor.scroll_offset = scrolling.get_scroll_step(monitor);
1707 } else {
1708 monitor.scroll_offset = 0;
1709 }
1710 }
1711 arrange(monitor);
1712 }
1713
1714 if (client_monitor) |monitor| {
1715 if (monitor.sel) |selected| {
1716 focus(display, selected);
1717 }
1718 }
1719
1720 client_mod.destroy(client);
1721 update_client_list(display);
1722 bar_mod.invalidate_bars();
1723 }
1724
1725 fn handle_enter_notify(display: *Display, event: *xlib.XCrossingEvent) void {
1726 if ((event.mode != xlib.NotifyNormal or event.detail == xlib.NotifyInferior) and event.window != display.root) {
1727 return;
1728 }
1729
1730 const client = client_mod.window_to_client(event.window);
1731 const target_mon = if (client) |c| c.monitor else monitor_mod.window_to_monitor(event.window);
1732 const selmon = monitor_mod.selected_monitor;
1733
1734 if (target_mon != selmon) {
1735 if (selmon) |sel| {
1736 unfocus_client(display, sel.sel, true);
1737 }
1738 monitor_mod.selected_monitor = target_mon;
1739 } else if (client == null) {
1740 return;
1741 } else if (selmon) |sel| {
1742 if (client.? == sel.sel) {
1743 return;
1744 }
1745 }
1746
1747 focus(display, client);
1748 }
1749
1750 fn handle_focus_in(display: *Display, event: *xlib.XFocusChangeEvent) void {
1751 const selmon = monitor_mod.selected_monitor orelse return;
1752 const selected = selmon.sel orelse return;
1753 if (event.window != selected.window) {
1754 set_focus(display, selected);
1755 }
1756 }
1757
1758 fn set_focus(display: *Display, client: *Client) void {
1759 if (!client.never_focus) {
1760 _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime);
1761 _ = xlib.XChangeProperty(display.handle, display.root, net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
1762 }
1763 _ = send_event(display, client, wm_take_focus);
1764 }
1765
1766 var last_motion_monitor: ?*Monitor = null;
1767
1768 fn handle_motion_notify(display: *Display, event: *xlib.XMotionEvent) void {
1769 if (event.window != display.root) {
1770 return;
1771 }
1772
1773 const target_mon = monitor_mod.rect_to_monitor(event.x_root, event.y_root, 1, 1);
1774 if (target_mon != last_motion_monitor and last_motion_monitor != null) {
1775 if (monitor_mod.selected_monitor) |selmon| {
1776 unfocus_client(display, selmon.sel, true);
1777 }
1778 monitor_mod.selected_monitor = target_mon;
1779 focus(display, null);
1780 }
1781 last_motion_monitor = target_mon;
1782 }
1783
1784 fn handle_property_notify(display: *Display, event: *xlib.XPropertyEvent) void {
1785 if (event.state == xlib.PropertyDelete) {
1786 return;
1787 }
1788
1789 const client = client_mod.window_to_client(event.window) orelse return;
1790
1791 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
1792 var trans: xlib.Window = 0;
1793 if (!client.is_floating and xlib.XGetTransientForHint(display.handle, client.window, &trans) != 0) {
1794 client.is_floating = client_mod.window_to_client(trans) != null;
1795 if (client.is_floating) {
1796 if (client.monitor) |monitor| {
1797 arrange(monitor);
1798 }
1799 }
1800 }
1801 } else if (event.atom == xlib.XA_WM_NORMAL_HINTS) {
1802 client.hints_valid = false;
1803 } else if (event.atom == xlib.XA_WM_HINTS) {
1804 update_wm_hints(display, client);
1805 bar_mod.invalidate_bars();
1806 } else if (event.atom == xlib.XA_WM_NAME or event.atom == net_wm_name) {
1807 update_title(display, client);
1808 } else if (event.atom == net_wm_window_type) {
1809 update_window_type(display, client);
1810 }
1811 }
1812
1813 fn unfocus_client(display: *Display, client: ?*Client, reset_input_focus: bool) void {
1814 const unfocus_target = client orelse return;
1815 grabbuttons(display, unfocus_target, false);
1816 _ = xlib.XSetWindowBorder(display.handle, unfocus_target.window, border_color_unfocused);
1817 if (reset_input_focus) {
1818 _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
1819 _ = xlib.XDeleteProperty(display.handle, display.root, net_active_window);
1820 }
1821 }
1822
1823 fn set_client_state(display: *Display, client: *Client, state: c_long) void {
1824 var data: [2]c_long = .{ state, xlib.None };
1825 _ = xlib.c.XChangeProperty(display.handle, client.window, wm_state, wm_state, 32, xlib.PropModeReplace, @ptrCast(&data), 2);
1826 }
1827
1828 fn update_numlock_mask(display: *Display) void {
1829 numlock_mask = 0;
1830 const modmap = xlib.XGetModifierMapping(display.handle);
1831 if (modmap == null) return;
1832 defer _ = xlib.XFreeModifiermap(modmap);
1833
1834 const numlock_keycode = xlib.XKeysymToKeycode(display.handle, xlib.XK_Num_Lock);
1835
1836 var modifier_index: usize = 0;
1837 while (modifier_index < 8) : (modifier_index += 1) {
1838 var key_index: usize = 0;
1839 while (key_index < @as(usize, @intCast(modmap.*.max_keypermod))) : (key_index += 1) {
1840 const keycode = modmap.*.modifiermap[modifier_index * @as(usize, @intCast(modmap.*.max_keypermod)) + key_index];
1841 if (keycode == numlock_keycode) {
1842 numlock_mask = @as(c_uint, 1) << @intCast(modifier_index);
1843 }
1844 }
1845 }
1846 }
1847
1848 fn grabbuttons(display: *Display, client: *Client, focused: bool) void {
1849 update_numlock_mask(display);
1850 const modifiers = [_]c_uint{ 0, xlib.LockMask, numlock_mask, numlock_mask | xlib.LockMask };
1851
1852 _ = xlib.XUngrabButton(display.handle, xlib.AnyButton, xlib.AnyModifier, client.window);
1853 if (!focused) {
1854 _ = xlib.XGrabButton(
1855 display.handle,
1856 xlib.AnyButton,
1857 xlib.AnyModifier,
1858 client.window,
1859 xlib.False,
1860 xlib.ButtonPressMask | xlib.ButtonReleaseMask,
1861 xlib.GrabModeSync,
1862 xlib.GrabModeSync,
1863 xlib.None,
1864 xlib.None,
1865 );
1866 }
1867 for (config.buttons.items) |button| {
1868 if (button.click == .client_win) {
1869 for (modifiers) |modifier| {
1870 _ = xlib.XGrabButton(
1871 display.handle,
1872 @intCast(button.button),
1873 button.mod_mask | modifier,
1874 client.window,
1875 xlib.False,
1876 xlib.ButtonPressMask | xlib.ButtonReleaseMask,
1877 xlib.GrabModeAsync,
1878 xlib.GrabModeSync,
1879 xlib.None,
1880 xlib.None,
1881 );
1882 }
1883 }
1884 }
1885 }
1886
1887 fn focus(display: *Display, target_client: ?*Client) void {
1888 const selmon = monitor_mod.selected_monitor orelse return;
1889
1890 var focus_client = target_client;
1891 if (focus_client == null or !client_mod.is_visible(focus_client.?)) {
1892 focus_client = selmon.stack;
1893 while (focus_client) |iter| {
1894 if (client_mod.is_visible(iter)) break;
1895 focus_client = iter.stack_next;
1896 }
1897 }
1898
1899 if (selmon.sel != null and selmon.sel != focus_client) {
1900 unfocus_client(display, selmon.sel, false);
1901 }
1902
1903 if (focus_client) |client| {
1904 if (client.monitor != selmon) {
1905 monitor_mod.selected_monitor = client.monitor;
1906 }
1907 if (client.is_urgent) {
1908 set_urgent(display, client, false);
1909 }
1910 client_mod.detach_stack(client);
1911 client_mod.attach_stack(client);
1912 grabbuttons(display, client, true);
1913 _ = xlib.XSetWindowBorder(display.handle, client.window, border_color_focused);
1914 if (!client.never_focus) {
1915 _ = xlib.XSetInputFocus(display.handle, client.window, xlib.RevertToPointerRoot, xlib.CurrentTime);
1916 _ = xlib.XChangeProperty(display.handle, display.root, net_active_window, xlib.XA_WINDOW, 32, xlib.PropModeReplace, @ptrCast(&client.window), 1);
1917 }
1918 _ = send_event(display, client, wm_take_focus);
1919 } else {
1920 _ = xlib.XSetInputFocus(display.handle, display.root, xlib.RevertToPointerRoot, xlib.CurrentTime);
1921 _ = xlib.XDeleteProperty(display.handle, display.root, net_active_window);
1922 }
1923
1924 const current_selmon = monitor_mod.selected_monitor orelse return;
1925 current_selmon.sel = focus_client;
1926
1927 if (focus_client) |client| {
1928 if (is_scrolling_layout(current_selmon)) {
1929 scroll_to_window(client, true);
1930 }
1931 }
1932
1933 bar_mod.invalidate_bars();
1934 }
1935
1936 fn restack(display: *Display, monitor: *Monitor) void {
1937 bar_mod.invalidate_bars();
1938 const selected_client = monitor.sel orelse return;
1939
1940 if (selected_client.is_floating or monitor.lt[monitor.sel_lt] == null) {
1941 _ = xlib.XRaiseWindow(display.handle, selected_client.window);
1942 }
1943
1944 if (monitor.lt[monitor.sel_lt] != null) {
1945 var window_changes: xlib.c.XWindowChanges = undefined;
1946 window_changes.stack_mode = xlib.c.Below;
1947 window_changes.sibling = monitor.bar_win;
1948
1949 var current = monitor.stack;
1950 while (current) |client| {
1951 if (!client.is_floating and client_mod.is_visible(client)) {
1952 _ = xlib.c.XConfigureWindow(display.handle, client.window, xlib.c.CWSibling | xlib.c.CWStackMode, &window_changes);
1953 window_changes.sibling = client.window;
1954 }
1955 current = client.stack_next;
1956 }
1957 }
1958
1959 _ = xlib.XSync(display.handle, xlib.False);
1960
1961 var discard_event: xlib.XEvent = undefined;
1962 while (xlib.c.XCheckMaskEvent(display.handle, xlib.EnterWindowMask, &discard_event) != 0) {}
1963 }
1964
1965 fn arrange(monitor: *Monitor) void {
1966 if (display_global) |display| {
1967 showhide(display, monitor);
1968 }
1969 if (monitor.lt[monitor.sel_lt]) |layout| {
1970 if (layout.arrange_fn) |arrange_fn| {
1971 arrange_fn(monitor);
1972 }
1973 }
1974 if (display_global) |display| {
1975 restack(display, monitor);
1976 }
1977 }
1978
1979 fn tick_animations() void {
1980 if (!scroll_animation.is_active()) return;
1981
1982 const monitor = monitor_mod.selected_monitor orelse return;
1983 if (scroll_animation.update()) |new_offset| {
1984 monitor.scroll_offset = new_offset;
1985 arrange(monitor);
1986 }
1987 }
1988
1989 fn is_scrolling_layout(monitor: *Monitor) bool {
1990 if (monitor.lt[monitor.sel_lt]) |layout| {
1991 return layout.arrange_fn == scrolling.layout.arrange_fn;
1992 }
1993 return false;
1994 }
1995
1996 fn scroll_layout(direction: i32) void {
1997 const monitor = monitor_mod.selected_monitor orelse return;
1998 if (!is_scrolling_layout(monitor)) return;
1999
2000 const scroll_step = scrolling.get_scroll_step(monitor);
2001 const max_scroll = scrolling.get_max_scroll(monitor);
2002
2003 const current = if (scroll_animation.is_active())
2004 scroll_animation.target()
2005 else
2006 monitor.scroll_offset;
2007
2008 var target = current + direction * scroll_step;
2009 target = @max(0, @min(target, max_scroll));
2010
2011 scroll_animation.start(monitor.scroll_offset, target, animation_config);
2012 }
2013
2014 fn scroll_to_window(client: *Client, animate: bool) void {
2015 const monitor = client.monitor orelse return;
2016 if (!is_scrolling_layout(monitor)) return;
2017
2018 const target = scrolling.get_target_scroll_for_window(monitor, client);
2019
2020 if (animate) {
2021 scroll_animation.start(monitor.scroll_offset, target, animation_config);
2022 } else {
2023 monitor.scroll_offset = target;
2024 arrange(monitor);
2025 }
2026 }
2027
2028 fn showhide_client(display: *Display, client: ?*Client) void {
2029 const target = client orelse return;
2030 if (client_mod.is_visible(target)) {
2031 _ = xlib.XMoveWindow(display.handle, target.window, target.x, target.y);
2032 const monitor = target.monitor orelse return;
2033 if ((monitor.lt[monitor.sel_lt] == null or target.is_floating) and !target.is_fullscreen) {
2034 tiling.resize(target, target.x, target.y, target.width, target.height, false);
2035 }
2036 showhide_client(display, target.stack_next);
2037 } else {
2038 showhide_client(display, target.stack_next);
2039 const client_width = target.width + 2 * target.border_width;
2040 _ = xlib.XMoveWindow(display.handle, target.window, -2 * client_width, target.y);
2041 }
2042 }
2043
2044 fn showhide(display: *Display, monitor: *Monitor) void {
2045 showhide_client(display, monitor.stack);
2046 }
2047
2048 fn update_size_hints(display: *Display, client: *Client) void {
2049 var size_hints: xlib.XSizeHints = undefined;
2050 var msize: c_long = 0;
2051
2052 if (xlib.XGetWMNormalHints(display.handle, client.window, &size_hints, &msize) == 0) {
2053 size_hints.flags = xlib.PSize;
2054 }
2055
2056 if ((size_hints.flags & xlib.PBaseSize) != 0) {
2057 client.base_width = size_hints.base_width;
2058 client.base_height = size_hints.base_height;
2059 } else if ((size_hints.flags & xlib.PMinSize) != 0) {
2060 client.base_width = size_hints.min_width;
2061 client.base_height = size_hints.min_height;
2062 } else {
2063 client.base_width = 0;
2064 client.base_height = 0;
2065 }
2066
2067 if ((size_hints.flags & xlib.PResizeInc) != 0) {
2068 client.increment_width = size_hints.width_inc;
2069 client.increment_height = size_hints.height_inc;
2070 } else {
2071 client.increment_width = 0;
2072 client.increment_height = 0;
2073 }
2074
2075 if ((size_hints.flags & xlib.PMaxSize) != 0) {
2076 client.max_width = size_hints.max_width;
2077 client.max_height = size_hints.max_height;
2078 } else {
2079 client.max_width = 0;
2080 client.max_height = 0;
2081 }
2082
2083 if ((size_hints.flags & xlib.PMinSize) != 0) {
2084 client.min_width = size_hints.min_width;
2085 client.min_height = size_hints.min_height;
2086 } else if ((size_hints.flags & xlib.PBaseSize) != 0) {
2087 client.min_width = size_hints.base_width;
2088 client.min_height = size_hints.base_height;
2089 } else {
2090 client.min_width = 0;
2091 client.min_height = 0;
2092 }
2093
2094 if ((size_hints.flags & xlib.PAspect) != 0) {
2095 client.min_aspect = @as(f32, @floatFromInt(size_hints.min_aspect.y)) / @as(f32, @floatFromInt(size_hints.min_aspect.x));
2096 client.max_aspect = @as(f32, @floatFromInt(size_hints.max_aspect.x)) / @as(f32, @floatFromInt(size_hints.max_aspect.y));
2097 } else {
2098 client.min_aspect = 0.0;
2099 client.max_aspect = 0.0;
2100 }
2101
2102 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);
2103 client.hints_valid = true;
2104 }
2105
2106 fn update_wm_hints(display: *Display, client: *Client) void {
2107 const wmh = xlib.XGetWMHints(display.handle, client.window);
2108 if (wmh) |hints| {
2109 defer _ = xlib.XFree(@ptrCast(hints));
2110
2111 if (client == (monitor_mod.selected_monitor orelse return).sel and (hints.*.flags & xlib.XUrgencyHint) != 0) {
2112 hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint);
2113 _ = xlib.XSetWMHints(display.handle, client.window, hints);
2114 } else {
2115 client.is_urgent = (hints.*.flags & xlib.XUrgencyHint) != 0;
2116 }
2117
2118 if ((hints.*.flags & xlib.InputHint) != 0) {
2119 client.never_focus = hints.*.input == 0;
2120 } else {
2121 client.never_focus = false;
2122 }
2123 }
2124 }
2125
2126 fn update_window_type(display: *Display, client: *Client) void {
2127 const state = get_atom_prop(display, client, net_wm_state);
2128 const window_type = get_atom_prop(display, client, net_wm_window_type);
2129
2130 if (state == net_wm_state_fullscreen) {
2131 set_fullscreen(display, client, true);
2132 }
2133 if (window_type == net_wm_window_type_dialog) {
2134 client.is_floating = true;
2135 }
2136 }
2137
2138 fn update_title(display: *Display, client: *Client) void {
2139 if (!get_text_prop(display, client.window, net_wm_name, &client.name)) {
2140 _ = get_text_prop(display, client.window, xlib.XA_WM_NAME, &client.name);
2141 }
2142 if (client.name[0] == 0) {
2143 @memcpy(client.name[0..6], "broken");
2144 }
2145 }
2146
2147 fn get_atom_prop(display: *Display, client: *Client, prop: xlib.Atom) xlib.Atom {
2148 var actual_type: xlib.Atom = undefined;
2149 var actual_format: c_int = undefined;
2150 var num_items: c_ulong = undefined;
2151 var bytes_after: c_ulong = undefined;
2152 var prop_data: [*c]u8 = undefined;
2153
2154 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) {
2155 const atom: xlib.Atom = @as(*xlib.Atom, @ptrCast(@alignCast(prop_data))).*;
2156 _ = xlib.XFree(@ptrCast(prop_data));
2157 return atom;
2158 }
2159 return 0;
2160 }
2161
2162 fn get_text_prop(display: *Display, window: xlib.Window, atom: xlib.Atom, text: *[256]u8) bool {
2163 var name: xlib.XTextProperty = undefined;
2164 text[0] = 0;
2165
2166 if (xlib.XGetTextProperty(display.handle, window, &name, atom) == 0 or name.nitems == 0) {
2167 return false;
2168 }
2169
2170 if (name.encoding == xlib.XA_STRING) {
2171 const len = @min(name.nitems, 255);
2172 @memcpy(text[0..len], name.value[0..len]);
2173 text[len] = 0;
2174 } else {
2175 var list: [*c][*c]u8 = undefined;
2176 var count: c_int = undefined;
2177 if (xlib.XmbTextPropertyToTextList(display.handle, &name, &list, &count) >= xlib.Success and count > 0 and list[0] != null) {
2178 const str = std.mem.sliceTo(list[0], 0);
2179 const copy_len = @min(str.len, 255);
2180 @memcpy(text[0..copy_len], str[0..copy_len]);
2181 text[copy_len] = 0;
2182 xlib.XFreeStringList(list);
2183 }
2184 }
2185 text[255] = 0;
2186 _ = xlib.XFree(@ptrCast(name.value));
2187 return true;
2188 }
2189
2190 fn apply_rules(display: *Display, client: *Client) void {
2191 var class_hint: xlib.XClassHint = .{ .res_name = null, .res_class = null };
2192 _ = xlib.XGetClassHint(display.handle, client.window, &class_hint);
2193
2194 const class_str: []const u8 = if (class_hint.res_class) |ptr| std.mem.sliceTo(ptr, 0) else "";
2195 const instance_str: []const u8 = if (class_hint.res_name) |ptr| std.mem.sliceTo(ptr, 0) else "";
2196
2197 client.is_floating = false;
2198 client.tags = 0;
2199
2200 for (config.rules.items) |rule| {
2201 const class_matches = if (rule.class) |rc| std.mem.indexOf(u8, class_str, rc) != null else true;
2202 const instance_matches = if (rule.instance) |ri| std.mem.indexOf(u8, instance_str, ri) != null else true;
2203 const title_matches = if (rule.title) |rt| std.mem.indexOf(u8, std.mem.sliceTo(&client.name, 0), rt) != null else true;
2204
2205 if (class_matches and instance_matches and title_matches) {
2206 client.is_floating = rule.is_floating;
2207 client.tags |= rule.tags;
2208 if (rule.monitor >= 0) {
2209 var target = monitor_mod.monitors;
2210 var index: i32 = 0;
2211 while (target) |mon| {
2212 if (index == rule.monitor) {
2213 client.monitor = mon;
2214 break;
2215 }
2216 index += 1;
2217 target = mon.next;
2218 }
2219 }
2220 }
2221 }
2222
2223 if (class_hint.res_class) |ptr| {
2224 _ = xlib.XFree(@ptrCast(ptr));
2225 }
2226 if (class_hint.res_name) |ptr| {
2227 _ = xlib.XFree(@ptrCast(ptr));
2228 }
2229
2230 const monitor = client.monitor orelse return;
2231 if (client.tags == 0) {
2232 client.tags = monitor.tagset[monitor.sel_tags];
2233 }
2234 }
2235
2236 fn update_client_list(display: *Display) void {
2237 _ = xlib.XDeleteProperty(display.handle, display.root, net_client_list);
2238
2239 var current_monitor = monitor_mod.monitors;
2240 while (current_monitor) |monitor| {
2241 var current_client = monitor.clients;
2242 while (current_client) |client| {
2243 _ = xlib.XChangeProperty(display.handle, display.root, net_client_list, xlib.XA_WINDOW, 32, xlib.PropModeAppend, @ptrCast(&client.window), 1);
2244 current_client = client.next;
2245 }
2246 current_monitor = monitor.next;
2247 }
2248 }
2249
2250 fn send_event(display: *Display, client: *Client, protocol: xlib.Atom) bool {
2251 var protocols: [*c]xlib.Atom = undefined;
2252 var num_protocols: c_int = 0;
2253 var exists = false;
2254
2255 if (xlib.XGetWMProtocols(display.handle, client.window, &protocols, &num_protocols) != 0) {
2256 var index: usize = 0;
2257 while (index < @as(usize, @intCast(num_protocols))) : (index += 1) {
2258 if (protocols[index] == protocol) {
2259 exists = true;
2260 break;
2261 }
2262 }
2263 _ = xlib.XFree(@ptrCast(protocols));
2264 }
2265
2266 if (exists) {
2267 var event: xlib.XEvent = undefined;
2268 event.type = xlib.ClientMessage;
2269 event.xclient.window = client.window;
2270 event.xclient.message_type = wm_protocols;
2271 event.xclient.format = 32;
2272 event.xclient.data.l[0] = @intCast(protocol);
2273 event.xclient.data.l[1] = xlib.CurrentTime;
2274 _ = xlib.XSendEvent(display.handle, client.window, xlib.False, xlib.NoEventMask, &event);
2275 }
2276 return exists;
2277 }
2278
2279 fn set_urgent(display: *Display, client: *Client, urgent: bool) void {
2280 client.is_urgent = urgent;
2281 const wmh = xlib.XGetWMHints(display.handle, client.window);
2282 if (wmh) |hints| {
2283 if (urgent) {
2284 hints.*.flags = hints.*.flags | xlib.XUrgencyHint;
2285 } else {
2286 hints.*.flags = hints.*.flags & ~@as(c_long, xlib.XUrgencyHint);
2287 }
2288 _ = xlib.XSetWMHints(display.handle, client.window, hints);
2289 _ = xlib.XFree(@ptrCast(hints));
2290 }
2291 }