tony / oxwm

added systray to bar, updated background to match config bg color Browse files
tonybanters committed 2026-03-23 parent f72f06d commit 3328e335451d02d973173cd3382c3a2e3981a9b3
Showing 4 changed files with 113 additions and 2 deletions. UnifiedSplit
+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 return switch (layout_index) { 14 return switch (layout_index) {
@@ -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
@@ -103,6 +108,11 @@ pub const Bar = struct {
103 108
104 _ = xlib.XMapWindow(display, window); 109 _ = xlib.XMapWindow(display, window);
105 110
111 + const systray: ?*Systray = if (with_systray)
112 + Systray.init(allocator, display, screen, window, bar_height, config.scheme_normal.background)
113 + else
114 + null;
115 +
106 bar.* = Bar{ 116 bar.* = Bar{
107 .window = window, 117 .window = window,
108 .pixmap = pixmap, 118 .pixmap = pixmap,
@@ -122,6 +132,7 @@ pub const Bar = struct {
122 .blocks = .empty, 132 .blocks = .empty,
123 .needs_redraw = true, 133 .needs_redraw = true,
124 .next = null, 134 .next = null,
135 + .systray = systray,
125 }; 136 };
126 137
127 monitor.bar_win = window; 138 monitor.bar_win = window;
@@ -133,6 +144,8 @@ pub const Bar = struct {
133 144
134 /// Destroys the bar's X resources and frees the allocation. 145 /// Destroys the bar's X resources and frees the allocation.
135 pub fn destroy(self: *Bar, display: *xlib.Display) void { 146 pub fn destroy(self: *Bar, display: *xlib.Display) void {
147 + if (self.systray) |tray| tray.deinit();
148 +
136 if (self.xft_draw) |xft_draw| xlib.XftDrawDestroy(xft_draw); 149 if (self.xft_draw) |xft_draw| xlib.XftDrawDestroy(xft_draw);
137 if (self.font) |font| xlib.XftFontClose(display, font); 150 if (self.font) |font| xlib.XftFontClose(display, font);
138 151
@@ -198,7 +211,10 @@ pub const Bar = struct {
198 self.drawText(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground); 211 self.drawText(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground);
199 x_position += self.textWidth(display, layout_symbol) + padding; 212 x_position += self.textWidth(display, layout_symbol) + padding;
200 213
201 - var block_x: i32 = self.width - padding; 214 + const systray_width: i32 = if (self.systray) |tray| tray.width() else 0;
215 + var block_x: i32 = self.width - padding - systray_width;
216 + if (systray_width > 0) block_x -= padding;
217 +
202 var block_index: usize = self.blocks.items.len; 218 var block_index: usize = self.blocks.items.len;
203 while (block_index > 0) { 219 while (block_index > 0) {
204 block_index -= 1; 220 block_index -= 1;
@@ -213,6 +229,10 @@ pub const Bar = struct {
213 block_x -= padding; 229 block_x -= padding;
214 } 230 }
215 231
232 + if (self.systray) |tray| {
233 + tray.updatePosition(self.width - systray_width - padding, 0);
234 + }
235 +
216 _ = xlib.XCopyArea(display, self.pixmap, self.window, self.graphics_context, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0); 236 _ = xlib.XCopyArea(display, self.pixmap, self.window, self.graphics_context, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0);
217 _ = xlib.XSync(display, xlib.False); 237 _ = xlib.XSync(display, xlib.False);
218 238
@@ -325,3 +345,12 @@ fn hasClientsOnTag(monitor: *Monitor, tag_mask: u32) bool {
325 } 345 }
326 return false; 346 return false;
327 } 347 }
348 +
349 +pub fn getSystray(bars: ?*Bar) ?*Systray {
350 + var current = bars;
351 + while (current) |bar| {
352 + if (bar.systray) |tray| return tray;
353 + current = bar.next;
354 + }
355 + return null;
356 +}
+67 −1 src/wm/handlers.zig
@@ -33,6 +33,9 @@ pub fn handleEvent(event: *xlib.XEvent, wm: *WindowManager) void {
33 .expose => handleExpose(&event.xexpose, wm), 33 .expose => handleExpose(&event.xexpose, wm),
34 .property_notify => handlePropertyNotify(&event.xproperty, wm), 34 .property_notify => handlePropertyNotify(&event.xproperty, wm),
35 .mapping_notify => handleMappingNotify(&event.xmapping, wm), 35 .mapping_notify => handleMappingNotify(&event.xmapping, wm),
36 + .reparent_notify => handleReparentNotify(&event.xreparent, wm),
37 + .map_notify => handleMapNotify(&event.xmap, wm),
38 + .resize_request => handleResizeRequest(&event.xresizerequest, wm),
36 else => {}, 39 else => {},
37 } 40 }
38 } 41 }
@@ -40,6 +43,10 @@ pub fn handleEvent(event: *xlib.XEvent, wm: *WindowManager) void {
40 fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void { 43 fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void {
41 std.debug.print("map_request: window=0x{x}\n", .{event.window}); 44 std.debug.print("map_request: window=0x{x}\n", .{event.window});
42 45
46 + if (wm.getSystray()) |tray| {
47 + if (tray.isIconWindow(event.window)) return;
48 + }
49 +
43 var window_attributes: xlib.XWindowAttributes = undefined; 50 var window_attributes: xlib.XWindowAttributes = undefined;
44 if (xlib.XGetWindowAttributes(wm.display.handle, event.window, &window_attributes) == 0) { 51 if (xlib.XGetWindowAttributes(wm.display.handle, event.window, &window_attributes) == 0) {
45 return; 52 return;
@@ -55,6 +62,13 @@ fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void {
55 } 62 }
56 63
57 fn handleConfigureRequest(event: *xlib.XConfigureRequestEvent, wm: *WindowManager) void { 64 fn handleConfigureRequest(event: *xlib.XConfigureRequestEvent, wm: *WindowManager) void {
65 + if (wm.getSystray()) |tray| {
66 + if (tray.handleConfigureRequest(event)) {
67 + wm.invalidateBars();
68 + return;
69 + }
70 + }
71 +
58 const client = client_mod.windowToClient(wm.monitors, event.window); 72 const client = client_mod.windowToClient(wm.monitors, event.window);
59 73
60 if (client) |managed_client| { 74 if (client) |managed_client| {
@@ -246,6 +260,13 @@ fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void {
246 } 260 }
247 261
248 fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void { 262 fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void {
263 + if (wm.getSystray()) |tray| {
264 + if (tray.handleClientMessage(event)) {
265 + wm.invalidateBars();
266 + return;
267 + }
268 + }
269 +
249 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; 270 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
250 271
251 if (event.message_type == wm.atoms.net_wm_state) { 272 if (event.message_type == wm.atoms.net_wm_state) {
@@ -275,12 +296,26 @@ fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) voi
275 } 296 }
276 297
277 fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void { 298 fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void {
299 + if (wm.getSystray()) |tray| {
300 + if (tray.handleDestroyNotify(event.window)) {
301 + wm.invalidateBars();
302 + return;
303 + }
304 + }
305 +
278 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; 306 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
279 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window}); 307 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
280 core.unmanage(client, wm); 308 core.unmanage(client, wm);
281 } 309 }
282 310
283 fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void { 311 fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void {
312 + if (wm.getSystray()) |tray| {
313 + if (tray.handleUnmapNotify(event.window)) {
314 + wm.invalidateBars();
315 + return;
316 + }
317 + }
318 +
284 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; 319 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
285 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window}); 320 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
286 core.unmanage(client, wm); 321 core.unmanage(client, wm);
@@ -338,6 +373,13 @@ fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void {
338 return; 373 return;
339 } 374 }
340 375
376 + if (wm.getSystray()) |tray| {
377 + if (tray.handlePropertyNotify(event)) {
378 + wm.invalidateBars();
379 + return;
380 + }
381 + }
382 +
341 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; 383 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
342 384
343 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) { 385 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
@@ -364,9 +406,33 @@ fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void {
364 406
365 fn handleMappingNotify(event: *xlib.XMappingEvent, wm: *WindowManager) void { 407 fn handleMappingNotify(event: *xlib.XMappingEvent, wm: *WindowManager) void {
366 _ = xlib.XRefreshKeyboardMapping(event); 408 _ = xlib.XRefreshKeyboardMapping(event);
367 - 409 +
368 if (event.request == xlib.MappingKeyboard or event.request == xlib.MappingModifier) { 410 if (event.request == xlib.MappingKeyboard or event.request == xlib.MappingModifier) {
369 wm.ungrabKeybinds(); 411 wm.ungrabKeybinds();
370 wm.grabKeybinds(); 412 wm.grabKeybinds();
371 } 413 }
372 } 414 }
415 +
416 +fn handleReparentNotify(event: *xlib.XReparentEvent, wm: *WindowManager) void {
417 + if (wm.getSystray()) |tray| {
418 + if (tray.handleReparentNotify(event.window, event.parent)) {
419 + wm.invalidateBars();
420 + }
421 + }
422 +}
423 +
424 +fn handleMapNotify(event: *xlib.c.XMapEvent, wm: *WindowManager) void {
425 + if (wm.getSystray()) |tray| {
426 + if (tray.handleMapNotify(event.window)) {
427 + wm.invalidateBars();
428 + }
429 + }
430 +}
431 +
432 +fn handleResizeRequest(event: *xlib.XResizeRequestEvent, wm: *WindowManager) void {
433 + if (wm.getSystray()) |tray| {
434 + if (tray.handleResizeRequest(event.window, event.width, event.height)) {
435 + wm.invalidateBars();
436 + }
437 + }
438 +}
+7 −0 src/wm/wm.zig
@@ -264,6 +264,7 @@ pub const WindowManager = struct {
264 pub fn setupBars(self: *WindowManager) void { 264 pub fn setupBars(self: *WindowManager) void {
265 var current_monitor = self.monitors; 265 var current_monitor = self.monitors;
266 var last_bar: ?*Bar = null; 266 var last_bar: ?*Bar = null;
267 + var is_first_bar = true;
267 268
268 while (current_monitor) |monitor| { 269 while (current_monitor) |monitor| {
269 const bar = Bar.create( 270 const bar = Bar.create(
@@ -272,10 +273,12 @@ pub const WindowManager = struct {
272 self.display.screen, 273 self.display.screen,
273 monitor, 274 monitor,
274 self.config, 275 self.config,
276 + is_first_bar,
275 ) orelse { 277 ) orelse {
276 current_monitor = monitor.next; 278 current_monitor = monitor.next;
277 continue; 279 continue;
278 }; 280 };
281 + is_first_bar = false;
279 282
280 if (tiling.bar_height == 0) { 283 if (tiling.bar_height == 0) {
281 tiling.setBarHeight(bar.height); 284 tiling.setBarHeight(bar.height);
@@ -327,6 +330,10 @@ pub const WindowManager = struct {
327 return bar_mod.windowToBar(self.bars, win); 330 return bar_mod.windowToBar(self.bars, win);
328 } 331 }
329 332
333 + pub fn getSystray(self: *WindowManager) ?*bar_mod.Systray {
334 + return bar_mod.getSystray(self.bars);
335 + }
336 +
330 /// Refreshes the cached numlock modifier bitmask from the X server's 337 /// Refreshes the cached numlock modifier bitmask from the X server's
331 /// current modifier map. Called before any key or button grab so that 338 /// current modifier map. Called before any key or button grab so that
332 /// grabs cover all numlock combinations. 339 /// grabs cover all numlock combinations.
+9 −0 src/x11/xlib.zig
@@ -273,3 +273,12 @@ pub const XMappingEvent = c.XMappingEvent;
273 pub const XRefreshKeyboardMapping = c.XRefreshKeyboardMapping; 273 pub const XRefreshKeyboardMapping = c.XRefreshKeyboardMapping;
274 pub const MappingKeyboard = c.MappingKeyboard; 274 pub const MappingKeyboard = c.MappingKeyboard;
275 pub const MappingModifier = c.MappingModifier; 275 pub const MappingModifier = c.MappingModifier;
276 +
277 +pub const XReparentWindow = c.XReparentWindow;
278 +pub const XResizeWindow = c.XResizeWindow;
279 +pub const XSetSelectionOwner = c.XSetSelectionOwner;
280 +pub const XGetSelectionOwner = c.XGetSelectionOwner;
281 +pub const XA_CARDINAL = c.XA_CARDINAL;
282 +pub const XReparentEvent = c.XReparentEvent;
283 +pub const XConfigureEvent = c.XConfigureEvent;
284 +pub const XResizeRequestEvent = c.XResizeRequestEvent;