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