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