added systray to bar, updated background to match config bg color
Browse files
+30 −1 src/bar/bar.zig
| @@ -3,10 +3,12 @@ const xlib = @import("../x11/xlib.zig"); | |||
| 3 | const monitor_mod = @import("../monitor.zig"); | 3 | const monitor_mod = @import("../monitor.zig"); |
| 4 | const blocks_mod = @import("blocks/blocks.zig"); | 4 | const blocks_mod = @import("blocks/blocks.zig"); |
| 5 | const config_mod = @import("../config/config.zig"); | 5 | const config_mod = @import("../config/config.zig"); |
| 6 | +const systray_mod = @import("systray.zig"); | ||
| 6 | const ColorScheme = config_mod.ColorScheme; | 7 | const ColorScheme = config_mod.ColorScheme; |
| 7 | 8 | ||
| 8 | const Monitor = monitor_mod.Monitor; | 9 | const Monitor = monitor_mod.Monitor; |
| 9 | const Block = blocks_mod.Block; | 10 | const Block = blocks_mod.Block; |
| 11 | +pub const Systray = systray_mod.Systray; | ||
| 10 | 12 | ||
| 11 | fn getLayoutSymbol(layout_index: u32, config: config_mod.Config) []const u8 { | 13 | fn getLayoutSymbol(layout_index: u32, config: config_mod.Config) []const u8 { |
| 12 | const layout = std.meta.intToEnum(config_mod.Layouts, layout_index) catch return "[?]"; | 14 | const layout = std.meta.intToEnum(config_mod.Layouts, layout_index) catch return "[?]"; |
| @@ -41,15 +43,18 @@ pub const Bar = struct { | |||
| 41 | blocks: std.ArrayList(Block), | 43 | blocks: std.ArrayList(Block), |
| 42 | needs_redraw: bool, | 44 | needs_redraw: bool, |
| 43 | next: ?*Bar, | 45 | next: ?*Bar, |
| 46 | + systray: ?*Systray, | ||
| 44 | 47 | ||
| 45 | /// Creates a bar window for `monitor` using the given config. | 48 | /// Creates a bar window for `monitor` using the given config. |
| 46 | /// Returns null on allocation failure or if the font cannot be loaded. | 49 | /// Returns null on allocation failure or if the font cannot be loaded. |
| 50 | + /// If `with_systray` is true, this bar will host the system tray. | ||
| 47 | pub fn create( | 51 | pub fn create( |
| 48 | allocator: std.mem.Allocator, | 52 | allocator: std.mem.Allocator, |
| 49 | display: *xlib.Display, | 53 | display: *xlib.Display, |
| 50 | screen: c_int, | 54 | screen: c_int, |
| 51 | monitor: *Monitor, | 55 | monitor: *Monitor, |
| 52 | config: config_mod.Config, | 56 | config: config_mod.Config, |
| 57 | + with_systray: bool, | ||
| 53 | ) ?*Bar { | 58 | ) ?*Bar { |
| 54 | const bar = allocator.create(Bar) catch return null; | 59 | const bar = allocator.create(Bar) catch return null; |
| 55 | 60 | ||
| @@ -115,6 +120,11 @@ pub const Bar = struct { | |||
| 115 | 120 | ||
| 116 | _ = xlib.XMapWindow(display, window); | 121 | _ = xlib.XMapWindow(display, window); |
| 117 | 122 | ||
| 123 | + const systray: ?*Systray = if (with_systray) | ||
| 124 | + Systray.init(allocator, display, screen, window, bar_height, config.scheme_normal.background) | ||
| 125 | + else | ||
| 126 | + null; | ||
| 127 | + | ||
| 118 | bar.* = Bar{ | 128 | bar.* = Bar{ |
| 119 | .window = window, | 129 | .window = window, |
| 120 | .pixmap = pixmap, | 130 | .pixmap = pixmap, |
| @@ -134,6 +144,7 @@ pub const Bar = struct { | |||
| 134 | .blocks = .empty, | 144 | .blocks = .empty, |
| 135 | .needs_redraw = true, | 145 | .needs_redraw = true, |
| 136 | .next = null, | 146 | .next = null, |
| 147 | + .systray = systray, | ||
| 137 | }; | 148 | }; |
| 138 | 149 | ||
| 139 | monitor.bar_win = window; | 150 | monitor.bar_win = window; |
| @@ -144,6 +155,8 @@ pub const Bar = struct { | |||
| 144 | 155 | ||
| 145 | /// Destroys the bar's X resources and frees the allocation. | 156 | /// Destroys the bar's X resources and frees the allocation. |
| 146 | pub fn destroy(self: *Bar, display: *xlib.Display) void { | 157 | pub fn destroy(self: *Bar, display: *xlib.Display) void { |
| 158 | + if (self.systray) |tray| tray.deinit(); | ||
| 159 | + | ||
| 147 | if (self.xft_draw) |xft_draw| xlib.XftDrawDestroy(xft_draw); | 160 | if (self.xft_draw) |xft_draw| xlib.XftDrawDestroy(xft_draw); |
| 148 | if (self.font) |font| xlib.XftFontClose(display, font); | 161 | if (self.font) |font| xlib.XftFontClose(display, font); |
| 149 | 162 | ||
| @@ -209,7 +222,10 @@ pub const Bar = struct { | |||
| 209 | self.drawText(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground); | 222 | self.drawText(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground); |
| 210 | x_position += self.textWidth(display, layout_symbol) + padding; | 223 | x_position += self.textWidth(display, layout_symbol) + padding; |
| 211 | 224 | ||
| 212 | - var block_x: i32 = self.width - padding; | 225 | + const systray_width: i32 = if (self.systray) |tray| tray.width() else 0; |
| 226 | + var block_x: i32 = self.width - padding - systray_width; | ||
| 227 | + if (systray_width > 0) block_x -= padding; | ||
| 228 | + | ||
| 213 | var block_index: usize = self.blocks.items.len; | 229 | var block_index: usize = self.blocks.items.len; |
| 214 | while (block_index > 0) { | 230 | while (block_index > 0) { |
| 215 | block_index -= 1; | 231 | block_index -= 1; |
| @@ -226,6 +242,10 @@ pub const Bar = struct { | |||
| 226 | block_x -= padding; | 242 | block_x -= padding; |
| 227 | } | 243 | } |
| 228 | 244 | ||
| 245 | + if (self.systray) |tray| { | ||
| 246 | + tray.updatePosition(self.width - systray_width - padding, 0); | ||
| 247 | + } | ||
| 248 | + | ||
| 229 | _ = xlib.XCopyArea(display, self.pixmap, self.window, self.graphics_context, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0); | 249 | _ = xlib.XCopyArea(display, self.pixmap, self.window, self.graphics_context, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0); |
| 230 | _ = xlib.XSync(display, xlib.False); | 250 | _ = xlib.XSync(display, xlib.False); |
| 231 | 251 | ||
| @@ -348,3 +368,12 @@ fn hasClientsOnTag(monitor: *Monitor, tag_mask: u32) bool { | |||
| 348 | } | 368 | } |
| 349 | return false; | 369 | return false; |
| 350 | } | 370 | } |
| 371 | + | ||
| 372 | +pub fn getSystray(bars: ?*Bar) ?*Systray { | ||
| 373 | + var current = bars; | ||
| 374 | + while (current) |bar| { | ||
| 375 | + if (bar.systray) |tray| return tray; | ||
| 376 | + current = bar.next; | ||
| 377 | + } | ||
| 378 | + return null; | ||
| 379 | +} | ||
+67 −1 src/wm/handlers.zig
| @@ -34,6 +34,9 @@ pub fn handleEvent(event: *xlib.XEvent, wm: *WindowManager) void { | |||
| 34 | .expose => handleExpose(&event.xexpose, wm), | 34 | .expose => handleExpose(&event.xexpose, wm), |
| 35 | .property_notify => handlePropertyNotify(&event.xproperty, wm), | 35 | .property_notify => handlePropertyNotify(&event.xproperty, wm), |
| 36 | .mapping_notify => handleMappingNotify(&event.xmapping, wm), | 36 | .mapping_notify => handleMappingNotify(&event.xmapping, wm), |
| 37 | + .reparent_notify => handleReparentNotify(&event.xreparent, wm), | ||
| 38 | + .map_notify => handleMapNotify(&event.xmap, wm), | ||
| 39 | + .resize_request => handleResizeRequest(&event.xresizerequest, wm), | ||
| 37 | else => {}, | 40 | else => {}, |
| 38 | } | 41 | } |
| 39 | } | 42 | } |
| @@ -41,6 +44,10 @@ pub fn handleEvent(event: *xlib.XEvent, wm: *WindowManager) void { | |||
| 41 | fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void { | 44 | fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void { |
| 42 | std.debug.print("map_request: window=0x{x}\n", .{event.window}); | 45 | std.debug.print("map_request: window=0x{x}\n", .{event.window}); |
| 43 | 46 | ||
| 47 | + if (wm.getSystray()) |tray| { | ||
| 48 | + if (tray.isIconWindow(event.window)) return; | ||
| 49 | + } | ||
| 50 | + | ||
| 44 | var window_attributes: xlib.XWindowAttributes = undefined; | 51 | var window_attributes: xlib.XWindowAttributes = undefined; |
| 45 | if (xlib.XGetWindowAttributes(wm.display.handle, event.window, &window_attributes) == 0) { | 52 | if (xlib.XGetWindowAttributes(wm.display.handle, event.window, &window_attributes) == 0) { |
| 46 | return; | 53 | return; |
| @@ -56,6 +63,13 @@ fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void { | |||
| 56 | } | 63 | } |
| 57 | 64 | ||
| 58 | fn handleConfigureRequest(event: *xlib.XConfigureRequestEvent, wm: *WindowManager) void { | 65 | fn handleConfigureRequest(event: *xlib.XConfigureRequestEvent, wm: *WindowManager) void { |
| 66 | + if (wm.getSystray()) |tray| { | ||
| 67 | + if (tray.handleConfigureRequest(event)) { | ||
| 68 | + wm.invalidateBars(); | ||
| 69 | + return; | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | + | ||
| 59 | const client = client_mod.windowToClient(wm.monitors, event.window); | 73 | const client = client_mod.windowToClient(wm.monitors, event.window); |
| 60 | 74 | ||
| 61 | if (client) |managed_client| { | 75 | if (client) |managed_client| { |
| @@ -272,6 +286,13 @@ fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void { | |||
| 272 | } | 286 | } |
| 273 | 287 | ||
| 274 | fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void { | 288 | fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void { |
| 289 | + if (wm.getSystray()) |tray| { | ||
| 290 | + if (tray.handleClientMessage(event)) { | ||
| 291 | + wm.invalidateBars(); | ||
| 292 | + return; | ||
| 293 | + } | ||
| 294 | + } | ||
| 295 | + | ||
| 275 | const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; | 296 | const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; |
| 276 | 297 | ||
| 277 | if (event.message_type == wm.atoms.net_wm_state) { | 298 | if (event.message_type == wm.atoms.net_wm_state) { |
| @@ -301,12 +322,26 @@ fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) voi | |||
| 301 | } | 322 | } |
| 302 | 323 | ||
| 303 | fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void { | 324 | fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void { |
| 325 | + if (wm.getSystray()) |tray| { | ||
| 326 | + if (tray.handleDestroyNotify(event.window)) { | ||
| 327 | + wm.invalidateBars(); | ||
| 328 | + return; | ||
| 329 | + } | ||
| 330 | + } | ||
| 331 | + | ||
| 304 | const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; | 332 | const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; |
| 305 | std.debug.print("destroy_notify: window=0x{x}\n", .{event.window}); | 333 | std.debug.print("destroy_notify: window=0x{x}\n", .{event.window}); |
| 306 | core.unmanage(client, wm); | 334 | core.unmanage(client, wm); |
| 307 | } | 335 | } |
| 308 | 336 | ||
| 309 | fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void { | 337 | fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void { |
| 338 | + if (wm.getSystray()) |tray| { | ||
| 339 | + if (tray.handleUnmapNotify(event.window)) { | ||
| 340 | + wm.invalidateBars(); | ||
| 341 | + return; | ||
| 342 | + } | ||
| 343 | + } | ||
| 344 | + | ||
| 310 | const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; | 345 | const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; |
| 311 | std.debug.print("unmap_notify: window=0x{x}\n", .{event.window}); | 346 | std.debug.print("unmap_notify: window=0x{x}\n", .{event.window}); |
| 312 | core.unmanage(client, wm); | 347 | core.unmanage(client, wm); |
| @@ -364,6 +399,13 @@ fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void { | |||
| 364 | return; | 399 | return; |
| 365 | } | 400 | } |
| 366 | 401 | ||
| 402 | + if (wm.getSystray()) |tray| { | ||
| 403 | + if (tray.handlePropertyNotify(event)) { | ||
| 404 | + wm.invalidateBars(); | ||
| 405 | + return; | ||
| 406 | + } | ||
| 407 | + } | ||
| 408 | + | ||
| 367 | const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; | 409 | const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; |
| 368 | 410 | ||
| 369 | if (event.atom == xlib.XA_WM_TRANSIENT_FOR) { | 411 | if (event.atom == xlib.XA_WM_TRANSIENT_FOR) { |
| @@ -390,9 +432,33 @@ fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void { | |||
| 390 | 432 | ||
| 391 | fn handleMappingNotify(event: *xlib.XMappingEvent, wm: *WindowManager) void { | 433 | fn handleMappingNotify(event: *xlib.XMappingEvent, wm: *WindowManager) void { |
| 392 | _ = xlib.XRefreshKeyboardMapping(event); | 434 | _ = xlib.XRefreshKeyboardMapping(event); |
| 393 | - | 435 | + |
| 394 | if (event.request == xlib.MappingKeyboard or event.request == xlib.MappingModifier) { | 436 | if (event.request == xlib.MappingKeyboard or event.request == xlib.MappingModifier) { |
| 395 | wm.ungrabKeybinds(); | 437 | wm.ungrabKeybinds(); |
| 396 | wm.grabKeybinds(); | 438 | wm.grabKeybinds(); |
| 397 | } | 439 | } |
| 398 | } | 440 | } |
| 441 | + | ||
| 442 | +fn handleReparentNotify(event: *xlib.XReparentEvent, wm: *WindowManager) void { | ||
| 443 | + if (wm.getSystray()) |tray| { | ||
| 444 | + if (tray.handleReparentNotify(event.window, event.parent)) { | ||
| 445 | + wm.invalidateBars(); | ||
| 446 | + } | ||
| 447 | + } | ||
| 448 | +} | ||
| 449 | + | ||
| 450 | +fn handleMapNotify(event: *xlib.c.XMapEvent, wm: *WindowManager) void { | ||
| 451 | + if (wm.getSystray()) |tray| { | ||
| 452 | + if (tray.handleMapNotify(event.window)) { | ||
| 453 | + wm.invalidateBars(); | ||
| 454 | + } | ||
| 455 | + } | ||
| 456 | +} | ||
| 457 | + | ||
| 458 | +fn handleResizeRequest(event: *xlib.XResizeRequestEvent, wm: *WindowManager) void { | ||
| 459 | + if (wm.getSystray()) |tray| { | ||
| 460 | + if (tray.handleResizeRequest(event.window, event.width, event.height)) { | ||
| 461 | + wm.invalidateBars(); | ||
| 462 | + } | ||
| 463 | + } | ||
| 464 | +} | ||
+7 −0 src/wm/wm.zig
| @@ -419,6 +419,7 @@ pub const WindowManager = struct { | |||
| 419 | pub fn setupBars(self: *WindowManager) void { | 419 | pub fn setupBars(self: *WindowManager) void { |
| 420 | var current_monitor = self.monitors; | 420 | var current_monitor = self.monitors; |
| 421 | var last_bar: ?*Bar = null; | 421 | var last_bar: ?*Bar = null; |
| 422 | + var is_first_bar = true; | ||
| 422 | 423 | ||
| 423 | while (current_monitor) |monitor| { | 424 | while (current_monitor) |monitor| { |
| 424 | const bar = Bar.create( | 425 | const bar = Bar.create( |
| @@ -427,10 +428,12 @@ pub const WindowManager = struct { | |||
| 427 | self.display.screen, | 428 | self.display.screen, |
| 428 | monitor, | 429 | monitor, |
| 429 | self.config, | 430 | self.config, |
| 431 | + is_first_bar, | ||
| 430 | ) orelse { | 432 | ) orelse { |
| 431 | current_monitor = monitor.next; | 433 | current_monitor = monitor.next; |
| 432 | continue; | 434 | continue; |
| 433 | }; | 435 | }; |
| 436 | + is_first_bar = false; | ||
| 434 | 437 | ||
| 435 | if (tiling.bar_height == 0) { | 438 | if (tiling.bar_height == 0) { |
| 436 | tiling.setBarHeight(bar.height); | 439 | tiling.setBarHeight(bar.height); |
| @@ -482,6 +485,10 @@ pub const WindowManager = struct { | |||
| 482 | return bar_mod.windowToBar(self.bars, win); | 485 | return bar_mod.windowToBar(self.bars, win); |
| 483 | } | 486 | } |
| 484 | 487 | ||
| 488 | + pub fn getSystray(self: *WindowManager) ?*bar_mod.Systray { | ||
| 489 | + return bar_mod.getSystray(self.bars); | ||
| 490 | + } | ||
| 491 | + | ||
| 485 | /// Refreshes the cached numlock modifier bitmask from the X server's | 492 | /// Refreshes the cached numlock modifier bitmask from the X server's |
| 486 | /// current modifier map. Called before any key or button grab so that | 493 | /// current modifier map. Called before any key or button grab so that |
| 487 | /// grabs cover all numlock combinations. | 494 | /// grabs cover all numlock combinations. |
+9 −0 src/x11/xlib.zig
| @@ -275,3 +275,12 @@ pub const XMappingEvent = c.XMappingEvent; | |||
| 275 | pub const XRefreshKeyboardMapping = c.XRefreshKeyboardMapping; | 275 | pub const XRefreshKeyboardMapping = c.XRefreshKeyboardMapping; |
| 276 | pub const MappingKeyboard = c.MappingKeyboard; | 276 | pub const MappingKeyboard = c.MappingKeyboard; |
| 277 | pub const MappingModifier = c.MappingModifier; | 277 | pub const MappingModifier = c.MappingModifier; |
| 278 | + | ||
| 279 | +pub const XReparentWindow = c.XReparentWindow; | ||
| 280 | +pub const XResizeWindow = c.XResizeWindow; | ||
| 281 | +pub const XSetSelectionOwner = c.XSetSelectionOwner; | ||
| 282 | +pub const XGetSelectionOwner = c.XGetSelectionOwner; | ||
| 283 | +pub const XA_CARDINAL = c.XA_CARDINAL; | ||
| 284 | +pub const XReparentEvent = c.XReparentEvent; | ||
| 285 | +pub const XConfigureEvent = c.XConfigureEvent; | ||
| 286 | +pub const XResizeRequestEvent = c.XResizeRequestEvent; | ||