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