| 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 client_mod = @import("../client.zig"); |
| 10 | const bar_mod = @import("../bar/bar.zig"); |
| 11 | const blocks_mod = @import("../bar/blocks/blocks.zig"); |
| 12 | const config_mod = @import("../config/config.zig"); |
| 13 | const overlay_mod = @import("../overlay.zig"); |
| 14 | const animations = @import("../animations.zig"); |
| 15 | const tiling = @import("../layouts/tiling.zig"); |
| 16 | const monocle = @import("../layouts/monocle.zig"); |
| 17 | const floating = @import("../layouts/floating.zig"); |
| 18 | const scrolling = @import("../layouts/scrolling.zig"); |
| 19 | const grid = @import("../layouts/grid.zig"); |
| 20 | |
| 21 | pub const core = @import("core.zig"); |
| 22 | pub const actions = @import("actions.zig"); |
| 23 | pub const handlers = @import("handlers.zig"); |
| 24 | |
| 25 | const Display = display_mod.Display; |
| 26 | const Atoms = atoms_mod.Atoms; |
| 27 | const ChordState = chord_mod.ChordState; |
| 28 | const Monitor = monitor_mod.Monitor; |
| 29 | const Bar = bar_mod.Bar; |
| 30 | const Config = config_mod.Config; |
| 31 | |
| 32 | // Standard ICCCM WM_STATE values used by get_state and set_client_state. |
| 33 | pub const NormalState: c_long = 1; |
| 34 | pub const WithdrawnState: c_long = 0; |
| 35 | pub const IconicState: c_long = 3; |
| 36 | pub const IsViewable: c_int = 2; |
| 37 | |
| 38 | pub const Cursors = struct { |
| 39 | normal: xlib.Cursor, |
| 40 | resize: xlib.Cursor, |
| 41 | move: xlib.Cursor, |
| 42 | |
| 43 | pub fn init(display: *Display) Cursors { |
| 44 | return .{ |
| 45 | .normal = xlib.XCreateFontCursor(display.handle, xlib.XC_left_ptr), |
| 46 | .resize = xlib.XCreateFontCursor(display.handle, xlib.XC_sizing), |
| 47 | .move = xlib.XCreateFontCursor(display.handle, xlib.XC_fleur), |
| 48 | }; |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | pub const WindowManager = struct { |
| 53 | allocator: mem.Allocator, |
| 54 | |
| 55 | /// The connection to the X server. |
| 56 | display: Display, |
| 57 | x11_fd: c_int, |
| 58 | /// Invisible 1×1 window used to satisfy the _NET_SUPPORTING_WM_CHECK |
| 59 | /// EWMH convention. |
| 60 | wm_check_window: xlib.Window, |
| 61 | |
| 62 | atoms: Atoms, |
| 63 | cursors: Cursors, |
| 64 | /// Cached numlock modifier mask, refreshed before any key/button grab. |
| 65 | numlock_mask: c_uint, |
| 66 | |
| 67 | config: Config, |
| 68 | /// Path to the config file that was loaded. |
| 69 | /// Null if using default config. |
| 70 | config_path: ?[]const u8, |
| 71 | |
| 72 | /// Head of the linked list of all managed monitors. |
| 73 | monitors: ?*Monitor, |
| 74 | /// The monitor that currently has input focus. |
| 75 | selected_monitor: ?*Monitor, |
| 76 | |
| 77 | /// Head of the linked list of status bars (one per monitor). |
| 78 | bars: ?*Bar, |
| 79 | |
| 80 | chord: ChordState, |
| 81 | |
| 82 | overlay: ?*overlay_mod.KeybindOverlay, |
| 83 | |
| 84 | scroll_animation: animations.ScrollAnimation, |
| 85 | animation_config: animations.AnimationConfig, |
| 86 | |
| 87 | running: bool, |
| 88 | next_spawn_floating: bool = false, |
| 89 | next_spawn_bypass_rules: bool = false, |
| 90 | last_motion_monitor: ?*Monitor, |
| 91 | |
| 92 | /// Initialises the window manager |
| 93 | /// |
| 94 | /// Returns an error if the display cannot be opened or another WM is |
| 95 | /// already running. |
| 96 | pub fn init(allocator: mem.Allocator, config: Config, config_path: ?[]const u8) !WindowManager { |
| 97 | var display = try Display.open(); |
| 98 | errdefer display.close(); |
| 99 | |
| 100 | try display.becomeWindowManager(); |
| 101 | |
| 102 | const x11_fd = xlib.XConnectionNumber(display.handle); |
| 103 | |
| 104 | const atoms_result = Atoms.init(display.handle, display.root); |
| 105 | const cursors = Cursors.init(&display); |
| 106 | _ = xlib.XDefineCursor(display.handle, display.root, cursors.normal); |
| 107 | |
| 108 | tiling.setDisplay(display.handle); |
| 109 | tiling.setScreenSize(display.screenWidth(), display.screenHeight()); |
| 110 | |
| 111 | var wm = WindowManager{ |
| 112 | .allocator = allocator, |
| 113 | .display = display, |
| 114 | .x11_fd = x11_fd, |
| 115 | .wm_check_window = atoms_result.check_window, |
| 116 | .atoms = atoms_result.atoms, |
| 117 | .cursors = cursors, |
| 118 | .numlock_mask = 0, |
| 119 | .config = config, |
| 120 | .config_path = config_path, |
| 121 | .monitors = null, |
| 122 | .selected_monitor = null, |
| 123 | .bars = null, |
| 124 | .chord = .{}, |
| 125 | .overlay = null, |
| 126 | .scroll_animation = .{}, |
| 127 | .animation_config = .{ .duration_ms = 150, .easing = .ease_out }, |
| 128 | .running = true, |
| 129 | .last_motion_monitor = null, |
| 130 | }; |
| 131 | |
| 132 | wm.setupMonitors(); |
| 133 | wm.setupBars(); |
| 134 | wm.setupOverlay(); |
| 135 | |
| 136 | return wm; |
| 137 | } |
| 138 | |
| 139 | /// Release all allocated memory owned by the WM. |
| 140 | pub fn deinit(self: *WindowManager) void { |
| 141 | bar_mod.destroyBars(self.bars, self.display.handle); |
| 142 | self.bars = null; |
| 143 | |
| 144 | if (self.overlay) |o| { |
| 145 | o.deinit(self.allocator); |
| 146 | self.overlay = null; |
| 147 | } |
| 148 | |
| 149 | var mon = self.monitors; |
| 150 | while (mon) |m| { |
| 151 | const next = m.next; |
| 152 | monitor_mod.destroy(self.allocator, m); |
| 153 | mon = next; |
| 154 | } |
| 155 | self.monitors = null; |
| 156 | self.selected_monitor = null; |
| 157 | |
| 158 | _ = xlib.XDestroyWindow(self.display.handle, self.wm_check_window); |
| 159 | |
| 160 | self.display.close(); |
| 161 | self.config.deinit(); |
| 162 | } |
| 163 | |
| 164 | const layouts = [_]*const monitor_mod.Layout{ |
| 165 | &tiling.layout, |
| 166 | &monocle.layout, |
| 167 | &floating.layout, |
| 168 | &scrolling.layout, |
| 169 | &grid.layout, |
| 170 | }; |
| 171 | |
| 172 | fn initMonitor(self: *WindowManager, mon: *Monitor, num: usize, x: i16, y: i16, w: c_int, h: c_int) void { |
| 173 | mon.num = @intCast(num); |
| 174 | mon.mon_x = x; |
| 175 | mon.mon_y = y; |
| 176 | mon.mon_w = w; |
| 177 | mon.mon_h = h; |
| 178 | mon.win_x = x; |
| 179 | mon.win_y = y; |
| 180 | mon.win_w = w; |
| 181 | mon.win_h = h; |
| 182 | |
| 183 | for (&mon.lt, layouts) |*slot, layout| { |
| 184 | slot.* = layout; |
| 185 | } |
| 186 | |
| 187 | if (config_mod.Layouts.fromString(self.config.layout)) |value| { |
| 188 | mon.sel_lt = @intFromEnum(value); |
| 189 | } |
| 190 | |
| 191 | for (0..10) |i| { |
| 192 | for (0..layouts.len) |j| { |
| 193 | mon.pertag.ltidxs[i][j] = mon.lt[j]; |
| 194 | } |
| 195 | mon.pertag.sellts[i] = mon.sel_lt; |
| 196 | } |
| 197 | |
| 198 | for (self.config.tag_layouts, 0..) |maybe_layout, i| { |
| 199 | if (maybe_layout) |layout_name| { |
| 200 | if (config_mod.Layouts.fromString(layout_name)) |value| { |
| 201 | mon.pertag.sellts[i + 1] = @intFromEnum(value); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | self.initMonitorGaps(mon); |
| 207 | } |
| 208 | |
| 209 | fn setupMonitors(self: *WindowManager) void { |
| 210 | if (xlib.XineramaIsActive(self.display.handle) != 0) { |
| 211 | var screen_count: c_int = 0; |
| 212 | const screens = xlib.XineramaQueryScreens(self.display.handle, &screen_count); |
| 213 | defer if (screens != null) { |
| 214 | _ = xlib.XFree(@ptrCast(screens)); |
| 215 | }; |
| 216 | |
| 217 | if (screen_count > 0 and screens != null) { |
| 218 | var prev_monitor: ?*Monitor = null; |
| 219 | for (0..@as(usize, @intCast(screen_count))) |index| { |
| 220 | const screen = screens[index]; |
| 221 | const mon = monitor_mod.create(self.allocator) orelse continue; |
| 222 | self.initMonitor(mon, index, screen.x_org, screen.y_org, screen.width, screen.height); |
| 223 | |
| 224 | if (prev_monitor) |prev| { |
| 225 | prev.next = mon; |
| 226 | } else { |
| 227 | self.monitors = mon; |
| 228 | self.selected_monitor = mon; |
| 229 | } |
| 230 | prev_monitor = mon; |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (self.monitors == null) { |
| 236 | const mon = monitor_mod.create(self.allocator) orelse return; |
| 237 | self.initMonitor(mon, 0, 0, 0, self.display.screenWidth(), self.display.screenHeight()); |
| 238 | self.monitors = mon; |
| 239 | self.selected_monitor = mon; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | pub fn updateGeom(self: *WindowManager) bool { |
| 244 | var dirty = false; |
| 245 | |
| 246 | if (xlib.XineramaIsActive(self.display.handle) != 0) { |
| 247 | var n: usize = 0; |
| 248 | var m = self.monitors; |
| 249 | while (m) |mon| : (m = mon.next) n += 1; |
| 250 | |
| 251 | var nn: c_int = 0; |
| 252 | const info = xlib.XineramaQueryScreens(self.display.handle, &nn); |
| 253 | if (info == null) return false; |
| 254 | defer _ = xlib.XFree(@ptrCast(info)); |
| 255 | |
| 256 | const new_count: usize = @intCast(nn); |
| 257 | |
| 258 | for (n..new_count) |_| { |
| 259 | var last = self.monitors; |
| 260 | while (last) |mon| { |
| 261 | if (mon.next == null) break; |
| 262 | last = mon.next; |
| 263 | } |
| 264 | const new_mon = monitor_mod.create(self.allocator) orelse continue; |
| 265 | new_mon.lt[0] = &tiling.layout; |
| 266 | new_mon.lt[1] = &monocle.layout; |
| 267 | new_mon.lt[2] = &floating.layout; |
| 268 | new_mon.lt[3] = &scrolling.layout; |
| 269 | new_mon.lt[4] = &grid.layout; |
| 270 | for (0..10) |i| { |
| 271 | new_mon.pertag.ltidxs[i][0] = new_mon.lt[0]; |
| 272 | new_mon.pertag.ltidxs[i][1] = new_mon.lt[1]; |
| 273 | new_mon.pertag.ltidxs[i][2] = new_mon.lt[2]; |
| 274 | new_mon.pertag.ltidxs[i][3] = new_mon.lt[3]; |
| 275 | new_mon.pertag.ltidxs[i][4] = new_mon.lt[4]; |
| 276 | } |
| 277 | if (last) |l| { |
| 278 | l.next = new_mon; |
| 279 | } else { |
| 280 | self.monitors = new_mon; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | var i: usize = 0; |
| 285 | m = self.monitors; |
| 286 | while (m != null and i < new_count) : (i += 1) { |
| 287 | const mon = m.?; |
| 288 | const screen = info[i]; |
| 289 | if (i >= n or |
| 290 | screen.x_org != mon.mon_x or screen.y_org != mon.mon_y or |
| 291 | screen.width != mon.mon_w or screen.height != mon.mon_h) |
| 292 | { |
| 293 | dirty = true; |
| 294 | mon.num = @intCast(i); |
| 295 | mon.mon_x = screen.x_org; |
| 296 | mon.mon_y = screen.y_org; |
| 297 | mon.mon_w = screen.width; |
| 298 | mon.mon_h = screen.height; |
| 299 | mon.win_x = screen.x_org; |
| 300 | mon.win_y = screen.y_org; |
| 301 | mon.win_w = screen.width; |
| 302 | mon.win_h = screen.height; |
| 303 | self.initMonitorGaps(mon); |
| 304 | } |
| 305 | m = mon.next; |
| 306 | } |
| 307 | |
| 308 | for (new_count..n) |_| { |
| 309 | var last: ?*Monitor = null; |
| 310 | var iter = self.monitors; |
| 311 | while (iter) |mon| { |
| 312 | if (mon.next == null) { |
| 313 | last = mon; |
| 314 | break; |
| 315 | } |
| 316 | iter = mon.next; |
| 317 | } |
| 318 | const removed = last orelse continue; |
| 319 | |
| 320 | while (removed.clients) |c| { |
| 321 | dirty = true; |
| 322 | removed.clients = c.next; |
| 323 | client_mod.detachStack(c); |
| 324 | c.monitor = self.monitors; |
| 325 | client_mod.attachAside(c); |
| 326 | client_mod.attachStack(c); |
| 327 | } |
| 328 | |
| 329 | if (self.selected_monitor == removed) { |
| 330 | self.selected_monitor = self.monitors; |
| 331 | } |
| 332 | |
| 333 | var prev: ?*Monitor = null; |
| 334 | iter = self.monitors; |
| 335 | while (iter) |mon| { |
| 336 | if (mon == removed) { |
| 337 | if (prev) |p| { |
| 338 | p.next = removed.next; |
| 339 | } else { |
| 340 | self.monitors = removed.next; |
| 341 | } |
| 342 | break; |
| 343 | } |
| 344 | prev = mon; |
| 345 | iter = mon.next; |
| 346 | } |
| 347 | monitor_mod.destroy(self.allocator, removed); |
| 348 | } |
| 349 | |
| 350 | tiling.setScreenSize(info[0].width, info[0].height); |
| 351 | } else { |
| 352 | if (self.monitors == null) { |
| 353 | const mon = monitor_mod.create(self.allocator) orelse return false; |
| 354 | mon.lt[0] = &tiling.layout; |
| 355 | mon.lt[1] = &monocle.layout; |
| 356 | mon.lt[2] = &floating.layout; |
| 357 | mon.lt[3] = &scrolling.layout; |
| 358 | mon.lt[4] = &grid.layout; |
| 359 | self.monitors = mon; |
| 360 | self.selected_monitor = mon; |
| 361 | } |
| 362 | |
| 363 | const sw = self.display.screenWidth(); |
| 364 | const sh = self.display.screenHeight(); |
| 365 | |
| 366 | if (self.monitors) |mon| { |
| 367 | if (mon.mon_w != sw or mon.mon_h != sh) { |
| 368 | dirty = true; |
| 369 | mon.mon_x = 0; |
| 370 | mon.mon_y = 0; |
| 371 | mon.mon_w = sw; |
| 372 | mon.mon_h = sh; |
| 373 | mon.win_x = 0; |
| 374 | mon.win_y = 0; |
| 375 | mon.win_w = sw; |
| 376 | mon.win_h = sh; |
| 377 | self.initMonitorGaps(mon); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | tiling.setScreenSize(sw, sh); |
| 382 | } |
| 383 | |
| 384 | if (dirty) { |
| 385 | self.selected_monitor = self.monitors; |
| 386 | self.selected_monitor = monitor_mod.windowToMonitor(self, self.display.root); |
| 387 | } |
| 388 | |
| 389 | return dirty; |
| 390 | } |
| 391 | |
| 392 | pub fn rebuildBars(self: *WindowManager) void { |
| 393 | bar_mod.destroyBars(self.bars, self.display.handle); |
| 394 | self.bars = null; |
| 395 | self.setupBars(); |
| 396 | self.rebuildBarBlocks(); |
| 397 | } |
| 398 | |
| 399 | fn initMonitorGaps(self: *WindowManager, mon: *Monitor) void { |
| 400 | const cfg = &self.config; |
| 401 | const any_gap_nonzero = cfg.gap_inner_h != 0 or cfg.gap_inner_v != 0 or |
| 402 | cfg.gap_outer_h != 0 or cfg.gap_outer_v != 0; |
| 403 | |
| 404 | mon.smartgaps_enabled = cfg.smartgaps_enabled; |
| 405 | |
| 406 | if (cfg.gaps_enabled and any_gap_nonzero) { |
| 407 | mon.gap_inner_h = cfg.gap_inner_h; |
| 408 | mon.gap_inner_v = cfg.gap_inner_v; |
| 409 | mon.gap_outer_h = cfg.gap_outer_h; |
| 410 | mon.gap_outer_v = cfg.gap_outer_v; |
| 411 | } else { |
| 412 | mon.gap_inner_h = 0; |
| 413 | mon.gap_inner_v = 0; |
| 414 | mon.gap_outer_h = 0; |
| 415 | mon.gap_outer_v = 0; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | pub fn setupBars(self: *WindowManager) void { |
| 420 | var current_monitor = self.monitors; |
| 421 | var last_bar: ?*Bar = null; |
| 422 | var is_first_bar = true; |
| 423 | |
| 424 | while (current_monitor) |monitor| { |
| 425 | const bar = Bar.create( |
| 426 | self.allocator, |
| 427 | self.display.handle, |
| 428 | self.display.screen, |
| 429 | monitor, |
| 430 | self.config, |
| 431 | is_first_bar, |
| 432 | ) orelse { |
| 433 | current_monitor = monitor.next; |
| 434 | continue; |
| 435 | }; |
| 436 | is_first_bar = false; |
| 437 | |
| 438 | if (tiling.bar_height == 0) { |
| 439 | tiling.setBarHeight(bar.height); |
| 440 | } |
| 441 | |
| 442 | self.populateBarBlocks(bar); |
| 443 | |
| 444 | if (last_bar) |prev| { |
| 445 | prev.next = bar; |
| 446 | } else { |
| 447 | self.bars = bar; |
| 448 | } |
| 449 | last_bar = bar; |
| 450 | |
| 451 | std.debug.print("bar created for monitor {d}\n", .{monitor.num}); |
| 452 | current_monitor = monitor.next; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | pub fn populateBarBlocks(self: *WindowManager, bar: *Bar) void { |
| 457 | if (self.config.blocks.items.len > 0) { |
| 458 | for (self.config.blocks.items) |cfg_block| { |
| 459 | if (cfg_block.block_type == .systray) { |
| 460 | bar.setSystrayConfig(cfg_block.underline, cfg_block.color); |
| 461 | } else { |
| 462 | bar.addBlock(configBlockToBarBlock(cfg_block)); |
| 463 | } |
| 464 | } |
| 465 | } else { |
| 466 | bar.addBlock(blocks_mod.Block.initRam("", 5, 0x7aa2f7, true)); |
| 467 | bar.addBlock(blocks_mod.Block.initStatic(" | ", 0x666666, false)); |
| 468 | bar.addBlock(blocks_mod.Block.initDatetime("", "%H:%M", 1, 0x0db9d7, true)); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | fn setupOverlay(self: *WindowManager) void { |
| 473 | self.overlay = overlay_mod.KeybindOverlay.init( |
| 474 | self.display.handle, |
| 475 | self.display.screen, |
| 476 | self.display.root, |
| 477 | self.config.font, |
| 478 | self.allocator, |
| 479 | ); |
| 480 | } |
| 481 | |
| 482 | // Wrap free functions in `bar_mod` with `self.bars` passed in. |
| 483 | |
| 484 | pub fn invalidateBars(self: *WindowManager) void { |
| 485 | bar_mod.invalidateBars(self.bars); |
| 486 | } |
| 487 | |
| 488 | pub fn windowToBar(self: *WindowManager, win: xlib.Window) ?*Bar { |
| 489 | return bar_mod.windowToBar(self.bars, win); |
| 490 | } |
| 491 | |
| 492 | pub fn getSystray(self: *WindowManager) ?*bar_mod.Systray { |
| 493 | return bar_mod.getSystray(self.bars); |
| 494 | } |
| 495 | |
| 496 | /// Refreshes the cached numlock modifier bitmask from the X server's |
| 497 | /// current modifier map. Called before any key or button grab so that |
| 498 | /// grabs cover all numlock combinations. |
| 499 | pub fn updateNumlockMask(self: *WindowManager) void { |
| 500 | self.numlock_mask = 0; |
| 501 | const modmap = xlib.XGetModifierMapping(self.display.handle); |
| 502 | if (modmap == null) return; |
| 503 | defer _ = xlib.XFreeModifiermap(modmap); |
| 504 | |
| 505 | const numlock_keycode = xlib.XKeysymToKeycode(self.display.handle, xlib.XK_Num_Lock); |
| 506 | |
| 507 | var modifier_index: usize = 0; |
| 508 | while (modifier_index < 8) : (modifier_index += 1) { |
| 509 | var key_index: usize = 0; |
| 510 | while (key_index < @as(usize, @intCast(modmap.*.max_keypermod))) : (key_index += 1) { |
| 511 | const keycode = modmap.*.modifiermap[modifier_index * @as(usize, @intCast(modmap.*.max_keypermod)) + key_index]; |
| 512 | if (keycode == numlock_keycode) { |
| 513 | self.numlock_mask = @as(c_uint, 1) << @intCast(modifier_index); |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | /// Grabs all configured keybinds and mouse buttons from the X server. |
| 520 | /// Replaces any existing grabs. Call after config load or reload. |
| 521 | pub fn grabKeybinds(self: *WindowManager) void { |
| 522 | self.updateNumlockMask(); |
| 523 | const modifiers = [_]c_uint{ 0, xlib.LockMask, self.numlock_mask, self.numlock_mask | xlib.LockMask }; |
| 524 | |
| 525 | _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root); |
| 526 | |
| 527 | for (self.config.keybinds.items) |keybind| { |
| 528 | if (keybind.key_count == 0) continue; |
| 529 | const first_key = keybind.keys[0]; |
| 530 | const keycode = xlib.XKeysymToKeycode(self.display.handle, @intCast(first_key.keysym)); |
| 531 | if (keycode != 0) { |
| 532 | for (modifiers) |modifier| { |
| 533 | _ = xlib.XGrabKey( |
| 534 | self.display.handle, |
| 535 | keycode, |
| 536 | first_key.mod_mask | modifier, |
| 537 | self.display.root, |
| 538 | xlib.True, |
| 539 | xlib.GrabModeAsync, |
| 540 | xlib.GrabModeAsync, |
| 541 | ); |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | for (self.config.buttons.items) |button| { |
| 547 | if (button.click == .client_win) { |
| 548 | for (modifiers) |modifier| { |
| 549 | _ = xlib.XGrabButton( |
| 550 | self.display.handle, |
| 551 | @intCast(button.button), |
| 552 | button.mod_mask | modifier, |
| 553 | self.display.root, |
| 554 | xlib.True, |
| 555 | xlib.ButtonPressMask | xlib.ButtonReleaseMask | xlib.PointerMotionMask, |
| 556 | xlib.GrabModeAsync, |
| 557 | xlib.GrabModeAsync, |
| 558 | xlib.None, |
| 559 | xlib.None, |
| 560 | ); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | std.debug.print("grabbed {d} keybinds from config\n", .{self.config.keybinds.items.len}); |
| 566 | } |
| 567 | |
| 568 | pub fn ungrabKeybinds(self: *WindowManager) void { |
| 569 | _ = xlib.XUngrabKey(self.display.handle, xlib.AnyKey, xlib.AnyModifier, self.display.root); |
| 570 | } |
| 571 | |
| 572 | /// Walk the existing window tree and call `manageFn` for each window |
| 573 | /// that should be managed. Non-transient windows are processed first |
| 574 | /// so transient (dialog) windows can be stacked on top. |
| 575 | pub fn scanExistingWindows(self: *WindowManager, manageFn: fn (xlib.Window, *xlib.XWindowAttributes, *WindowManager) void) void { |
| 576 | var root_return: xlib.Window = undefined; |
| 577 | var parent_return: xlib.Window = undefined; |
| 578 | var children: [*c]xlib.Window = undefined; |
| 579 | var num_children: c_uint = undefined; |
| 580 | |
| 581 | if (xlib.XQueryTree(self.display.handle, self.display.root, &root_return, &parent_return, &children, &num_children) == 0) { |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | var index: c_uint = 0; |
| 586 | while (index < num_children) : (index += 1) { |
| 587 | var window_attrs: xlib.XWindowAttributes = undefined; |
| 588 | if (xlib.XGetWindowAttributes(self.display.handle, children[index], &window_attrs) == 0) { |
| 589 | continue; |
| 590 | } |
| 591 | if (window_attrs.override_redirect != 0) continue; |
| 592 | |
| 593 | var trans: xlib.Window = 0; |
| 594 | if (xlib.XGetTransientForHint(self.display.handle, children[index], &trans) != 0) continue; |
| 595 | |
| 596 | if (window_attrs.map_state == IsViewable or self.getState(children[index]) == IconicState) { |
| 597 | manageFn(children[index], &window_attrs, self); |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | index = 0; |
| 602 | while (index < num_children) : (index += 1) { |
| 603 | var window_attrs: xlib.XWindowAttributes = undefined; |
| 604 | if (xlib.XGetWindowAttributes(self.display.handle, children[index], &window_attrs) == 0) { |
| 605 | continue; |
| 606 | } |
| 607 | var trans: xlib.Window = 0; |
| 608 | if (xlib.XGetTransientForHint(self.display.handle, children[index], &trans) != 0) { |
| 609 | if (window_attrs.map_state == IsViewable or self.getState(children[index]) == IconicState) { |
| 610 | manageFn(children[index], &window_attrs, self); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | if (children != null) _ = xlib.XFree(@ptrCast(children)); |
| 616 | } |
| 617 | |
| 618 | /// Reads the ICCCM WM_STATE property for a window. |
| 619 | /// Returns WithdrawnState if the property is absent or malformed. |
| 620 | pub fn getState(self: *WindowManager, window: xlib.Window) c_long { |
| 621 | var actual_type: xlib.Atom = 0; |
| 622 | var actual_format: c_int = 0; |
| 623 | var num_items: c_ulong = 0; |
| 624 | var bytes_after: c_ulong = 0; |
| 625 | var prop: [*c]u8 = null; |
| 626 | |
| 627 | const result = xlib.XGetWindowProperty( |
| 628 | self.display.handle, |
| 629 | window, |
| 630 | self.atoms.wm_state, |
| 631 | 0, |
| 632 | 2, |
| 633 | xlib.False, |
| 634 | self.atoms.wm_state, |
| 635 | &actual_type, |
| 636 | &actual_format, |
| 637 | &num_items, |
| 638 | &bytes_after, |
| 639 | &prop, |
| 640 | ); |
| 641 | |
| 642 | if (result != 0 or actual_type != self.atoms.wm_state or num_items < 1) { |
| 643 | if (prop != null) _ = xlib.XFree(prop); |
| 644 | return WithdrawnState; |
| 645 | } |
| 646 | |
| 647 | const state: c_long = @as(*c_long, @ptrCast(@alignCast(prop))).*; |
| 648 | _ = xlib.XFree(prop); |
| 649 | return state; |
| 650 | } |
| 651 | |
| 652 | /// Run the event loop until `self.running` is set to false. |
| 653 | /// |
| 654 | /// `eventFn` dispatches a single XEvent |
| 655 | /// `tickFn` is called once per loop iteration to advance animations |
| 656 | pub fn run( |
| 657 | self: *WindowManager, |
| 658 | eventFn: fn (*xlib.XEvent, *WindowManager) void, |
| 659 | tickFn: fn (*WindowManager) void, |
| 660 | ) void { |
| 661 | var fds = [_]std.posix.pollfd{ |
| 662 | .{ .fd = self.x11_fd, .events = std.posix.POLL.IN, .revents = 0 }, |
| 663 | }; |
| 664 | |
| 665 | _ = xlib.XSync(self.display.handle, xlib.False); |
| 666 | |
| 667 | while (self.running) { |
| 668 | while (xlib.XPending(self.display.handle) > 0) { |
| 669 | var event = self.display.nextEvent(); |
| 670 | eventFn(&event, self); |
| 671 | } |
| 672 | |
| 673 | tickFn(self); |
| 674 | |
| 675 | var current_bar = self.bars; |
| 676 | while (current_bar) |bar| { |
| 677 | bar.updateBlocks(); |
| 678 | bar.draw(self.display.handle, self.config); |
| 679 | current_bar = bar.next; |
| 680 | } |
| 681 | |
| 682 | const poll_timeout: i32 = if (self.scroll_animation.isActive()) 16 else 1000; |
| 683 | _ = std.posix.poll(&fds, poll_timeout) catch 0; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | /// Reloads the configuration |
| 688 | /// |
| 689 | /// `loadFn` repopulates `self.config` |
| 690 | /// keys/buttons are unbound before reload and rebound after |
| 691 | pub fn reloadConfig( |
| 692 | self: *WindowManager, |
| 693 | loadFn: fn (*WindowManager) void, |
| 694 | ) void { |
| 695 | std.debug.print("reloading config...\n", .{}); |
| 696 | |
| 697 | self.ungrabKeybinds(); |
| 698 | |
| 699 | self.config.keybinds.clearRetainingCapacity(); |
| 700 | self.config.buttons.clearRetainingCapacity(); |
| 701 | self.config.rules.clearRetainingCapacity(); |
| 702 | self.config.blocks.clearRetainingCapacity(); |
| 703 | |
| 704 | loadFn(self); |
| 705 | |
| 706 | bar_mod.destroyBars(self.bars, self.display.handle); |
| 707 | self.bars = null; |
| 708 | self.setupBars(); |
| 709 | self.rebuildBarBlocks(); |
| 710 | |
| 711 | self.grabKeybinds(); |
| 712 | } |
| 713 | |
| 714 | fn rebuildBarBlocks(self: *WindowManager) void { |
| 715 | var current_bar = self.bars; |
| 716 | while (current_bar) |bar| { |
| 717 | bar.clearBlocks(); |
| 718 | self.populateBarBlocks(bar); |
| 719 | current_bar = bar.next; |
| 720 | } |
| 721 | } |
| 722 | }; |
| 723 | |
| 724 | /// Converts a config block description into a live status bar block. |
| 725 | pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block { |
| 726 | var block = switch (cfg.block_type) { |
| 727 | .static => blocks_mod.Block.initStatic(cfg.format, cfg.color, cfg.underline), |
| 728 | .datetime => blocks_mod.Block.initDatetime( |
| 729 | cfg.format, |
| 730 | cfg.datetime_format orelse "%H:%M", |
| 731 | cfg.interval, |
| 732 | cfg.color, |
| 733 | cfg.underline, |
| 734 | ), |
| 735 | .ram => blocks_mod.Block.initRam(cfg.format, cfg.interval, cfg.color, cfg.underline), |
| 736 | .shell => blocks_mod.Block.initShell( |
| 737 | cfg.format, |
| 738 | cfg.command orelse "", |
| 739 | cfg.interval, |
| 740 | cfg.color, |
| 741 | cfg.underline, |
| 742 | ), |
| 743 | .battery => blocks_mod.Block.initBattery( |
| 744 | cfg.format_charging orelse "", |
| 745 | cfg.format_discharging orelse "", |
| 746 | cfg.format_full orelse "", |
| 747 | cfg.battery_name orelse "BAT0", |
| 748 | cfg.interval, |
| 749 | cfg.color, |
| 750 | cfg.underline, |
| 751 | ), |
| 752 | .cpu_temp => blocks_mod.Block.initCpuTemp( |
| 753 | cfg.format, |
| 754 | cfg.thermal_zone orelse "thermal_zone0", |
| 755 | cfg.interval, |
| 756 | cfg.color, |
| 757 | cfg.underline, |
| 758 | ), |
| 759 | .systray => blocks_mod.Block.initStatic("", 0, false), |
| 760 | }; |
| 761 | block.click = cfg.click; |
| 762 | return block; |
| 763 | } |