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