tony / oxwm

Merge pull request #201 from tonybanters/systray Browse files
Systray (oxwm is becoming a DE at this point)
Tony, btw. committed 2026-05-09 parents f6a28b2 0149088 commit f699f6d1ff9e07cdd3831591bda84400e784b2c1
Showing 8 changed files with 689 additions and 3 deletions. UnifiedSplit
+43 −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,20 @@ 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,
47 + systray_underline: bool,
48 + systray_color: u32,
44 49
45 /// Creates a bar window for `monitor` using the given config. 50 /// Creates a bar window for `monitor` using the given config.
46 /// Returns null on allocation failure or if the font cannot be loaded. 51 /// Returns null on allocation failure or if the font cannot be loaded.
52 + /// If `with_systray` is true, this bar will host the system tray.
47 pub fn create( 53 pub fn create(
48 allocator: std.mem.Allocator, 54 allocator: std.mem.Allocator,
49 display: *xlib.Display, 55 display: *xlib.Display,
50 screen: c_int, 56 screen: c_int,
51 monitor: *Monitor, 57 monitor: *Monitor,
52 config: config_mod.Config, 58 config: config_mod.Config,
59 + with_systray: bool,
53 ) ?*Bar { 60 ) ?*Bar {
54 const bar = allocator.create(Bar) catch return null; 61 const bar = allocator.create(Bar) catch return null;
55 62
@@ -115,6 +122,11 @@ pub const Bar = struct {
115 122
116 _ = xlib.XMapWindow(display, window); 123 _ = xlib.XMapWindow(display, window);
117 124
125 + const systray: ?*Systray = if (with_systray)
126 + Systray.init(allocator, display, screen, window, bar_height, config.scheme_normal.background)
127 + else
128 + null;
129 +
118 bar.* = Bar{ 130 bar.* = Bar{
119 .window = window, 131 .window = window,
120 .pixmap = pixmap, 132 .pixmap = pixmap,
@@ -134,6 +146,9 @@ pub const Bar = struct {
134 .blocks = .empty, 146 .blocks = .empty,
135 .needs_redraw = true, 147 .needs_redraw = true,
136 .next = null, 148 .next = null,
149 + .systray = systray,
150 + .systray_underline = false,
151 + .systray_color = 0xffffff,
137 }; 152 };
138 153
139 monitor.bar_win = window; 154 monitor.bar_win = window;
@@ -144,6 +159,8 @@ pub const Bar = struct {
144 159
145 /// Destroys the bar's X resources and frees the allocation. 160 /// Destroys the bar's X resources and frees the allocation.
146 pub fn destroy(self: *Bar, display: *xlib.Display) void { 161 pub fn destroy(self: *Bar, display: *xlib.Display) void {
162 + if (self.systray) |tray| tray.deinit();
163 +
147 if (self.xft_draw) |xft_draw| xlib.XftDrawDestroy(xft_draw); 164 if (self.xft_draw) |xft_draw| xlib.XftDrawDestroy(xft_draw);
148 if (self.font) |font| xlib.XftFontClose(display, font); 165 if (self.font) |font| xlib.XftFontClose(display, font);
149 166
@@ -158,6 +175,11 @@ pub const Bar = struct {
158 self.blocks.append(self.allocator, block) catch {}; 175 self.blocks.append(self.allocator, block) catch {};
159 } 176 }
160 177
178 + pub fn setSystrayConfig(self: *Bar, underline: bool, col: u32) void {
179 + self.systray_underline = underline;
180 + self.systray_color = col;
181 + }
182 +
161 pub fn clearBlocks(self: *Bar) void { 183 pub fn clearBlocks(self: *Bar) void {
162 self.blocks.clearRetainingCapacity(); 184 self.blocks.clearRetainingCapacity();
163 } 185 }
@@ -209,7 +231,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); 231 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; 232 x_position += self.textWidth(display, layout_symbol) + padding;
211 233
212 - var block_x: i32 = self.width - padding; 234 + const systray_width: i32 = if (self.systray) |tray| tray.width() else 0;
235 + var block_x: i32 = self.width - padding - systray_width;
236 + if (systray_width > 0) block_x -= padding;
237 +
213 var block_index: usize = self.blocks.items.len; 238 var block_index: usize = self.blocks.items.len;
214 while (block_index > 0) { 239 while (block_index > 0) {
215 block_index -= 1; 240 block_index -= 1;
@@ -226,6 +251,14 @@ pub const Bar = struct {
226 block_x -= padding; 251 block_x -= padding;
227 } 252 }
228 253
254 + if (self.systray) |tray| {
255 + const systray_x = self.width - systray_width - padding;
256 + tray.updatePosition(systray_x, 0);
257 + if (self.systray_underline and systray_width > 0) {
258 + self.fillRect(display, systray_x, self.height - 2, systray_width, 2, self.systray_color);
259 + }
260 + }
261 +
229 _ = xlib.XCopyArea(display, self.pixmap, self.window, self.graphics_context, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0); 262 _ = 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); 263 _ = xlib.XSync(display, xlib.False);
231 264
@@ -348,3 +381,12 @@ fn hasClientsOnTag(monitor: *Monitor, tag_mask: u32) bool {
348 } 381 }
349 return false; 382 return false;
350 } 383 }
384 +
385 +pub fn getSystray(bars: ?*Bar) ?*Systray {
386 + var current = bars;
387 + while (current) |bar| {
388 + if (bar.systray) |tray| return tray;
389 + current = bar.next;
390 + }
391 + return null;
392 +}
+522 −0 src/bar/systray.zig new file mode 100644 Large diff collapsed, click to expand
@@ -0,0 +1,522 @@
1 +const std = @import("std");
2 +const xlib = @import("../x11/xlib.zig");
3 +
4 +pub const SYSTEM_TRAY_REQUEST_DOCK: c_long = 0;
5 +pub const SYSTEM_TRAY_BEGIN_MESSAGE: c_long = 1;
6 +pub const SYSTEM_TRAY_CANCEL_MESSAGE: c_long = 2;
7 +
8 +pub const XEMBED_EMBEDDED_NOTIFY: c_long = 0;
9 +pub const XEMBED_WINDOW_ACTIVATE: c_long = 1;
10 +pub const XEMBED_WINDOW_DEACTIVATE: c_long = 2;
11 +pub const XEMBED_REQUEST_FOCUS: c_long = 3;
12 +pub const XEMBED_FOCUS_IN: c_long = 4;
13 +pub const XEMBED_FOCUS_OUT: c_long = 5;
14 +pub const XEMBED_FOCUS_NEXT: c_long = 6;
15 +pub const XEMBED_FOCUS_PREV: c_long = 7;
16 +pub const XEMBED_MAPPED: c_ulong = 1 << 0;
17 +
18 +pub const XEMBED_VERSION: c_long = 0;
19 +
20 +pub const Icon = struct {
21 + window: xlib.Window,
22 + width: i32,
23 + height: i32,
24 + mapped: bool,
25 +};
26 +
27 +pub const Systray = struct {
28 + window: xlib.Window,
29 + icons: std.ArrayList(Icon),
30 + allocator: std.mem.Allocator,
31 + display: *xlib.Display,
32 + screen: c_int,
33 + icon_size: i32,
34 + bar_height: i32,
35 +
36 + net_system_tray_opcode: xlib.Atom,
37 + net_system_tray_orientation: xlib.Atom,
38 + net_system_tray_visual: xlib.Atom,
39 + manager_atom: xlib.Atom,
40 + xembed: xlib.Atom,
41 + xembed_info: xlib.Atom,
42 +
43 + pub fn init(
44 + allocator: std.mem.Allocator,
45 + display: *xlib.Display,
46 + screen: c_int,
47 + bar_win: xlib.Window,
48 + bar_height: i32,
49 + background: u32,
50 + ) ?*Systray {
51 + const self = allocator.create(Systray) catch return null;
52 +
53 + const root = xlib.XRootWindow(display, screen);
54 +
55 + self.allocator = allocator;
56 + self.display = display;
57 + self.screen = screen;
58 + self.bar_height = bar_height;
59 + self.icon_size = bar_height - 8;
60 + self.icons = .empty;
61 +
62 + self.net_system_tray_opcode = xlib.XInternAtom(display, "_NET_SYSTEM_TRAY_OPCODE", xlib.False);
63 + self.net_system_tray_orientation = xlib.XInternAtom(display, "_NET_SYSTEM_TRAY_ORIENTATION", xlib.False);
64 + self.net_system_tray_visual = xlib.XInternAtom(display, "_NET_SYSTEM_TRAY_VISUAL", xlib.False);
65 + self.xembed = xlib.XInternAtom(display, "_XEMBED", xlib.False);
66 + self.xembed_info = xlib.XInternAtom(display, "_XEMBED_INFO", xlib.False);
67 +
68 + var selection_name: [32]u8 = undefined;
69 + const name_len = std.fmt.bufPrint(&selection_name, "_NET_SYSTEM_TRAY_S{d}", .{screen}) catch return null;
70 + selection_name[name_len.len] = 0;
71 + self.manager_atom = xlib.XInternAtom(display, @ptrCast(&selection_name), xlib.False);
72 +
73 + const current_owner = xlib.c.XGetSelectionOwner(display, self.manager_atom);
74 + if (current_owner != xlib.None) {
75 + allocator.destroy(self);
76 + return null;
77 + }
78 +
79 + self.window = xlib.c.XCreateSimpleWindow(
80 + display,
81 + bar_win,
82 + 0,
83 + 0,
84 + 1,
85 + @intCast(bar_height - 2),
86 + 0,
87 + 0,
88 + 0,
89 + );
90 +
91 + var attrs: xlib.c.XSetWindowAttributes = undefined;
92 + attrs.event_mask = xlib.c.SubstructureNotifyMask | xlib.c.SubstructureRedirectMask | xlib.c.StructureNotifyMask | xlib.c.ExposureMask;
93 + attrs.override_redirect = xlib.True;
94 + attrs.background_pixel = background;
95 + _ = xlib.c.XChangeWindowAttributes(
96 + display,
97 + self.window,
98 + xlib.c.CWEventMask | xlib.c.CWOverrideRedirect | xlib.c.CWBackPixel,
99 + &attrs,
100 + );
101 +
102 + _ = xlib.c.XSetSelectionOwner(display, self.manager_atom, self.window, xlib.CurrentTime);
103 + if (xlib.c.XGetSelectionOwner(display, self.manager_atom) != self.window) {
104 + _ = xlib.c.XDestroyWindow(display, self.window);
105 + allocator.destroy(self);
106 + return null;
107 + }
108 +
109 + const orientation: c_long = 0;
110 + _ = xlib.XChangeProperty(
111 + display,
112 + self.window,
113 + self.net_system_tray_orientation,
114 + xlib.XA_CARDINAL,
115 + 32,
116 + xlib.PropModeReplace,
117 + @ptrCast(&orientation),
118 + 1,
119 + );
120 +
121 + self.sendManagerMessage(root);
122 +
123 + _ = xlib.XMapWindow(display, self.window);
124 + _ = xlib.XSync(display, xlib.False);
125 +
126 + return self;
127 + }
128 +
129 + pub fn deinit(self: *Systray) void {
130 + for (self.icons.items) |icon| {
131 + _ = xlib.c.XUnmapWindow(self.display, icon.window);
132 + _ = xlib.c.XReparentWindow(self.display, icon.window, xlib.XRootWindow(self.display, self.screen), 0, 0);
133 + }
134 + self.icons.deinit(self.allocator);
135 + _ = xlib.c.XDestroyWindow(self.display, self.window);
136 + self.allocator.destroy(self);
137 + }
138 +
139 + fn sendManagerMessage(self: *Systray, root: xlib.Window) void {
140 + var ev: xlib.c.XClientMessageEvent = undefined;
141 + ev.type = xlib.ClientMessage;
142 + ev.window = root;
143 + ev.message_type = xlib.XInternAtom(self.display, "MANAGER", xlib.False);
144 + ev.format = 32;
145 + ev.data.l[0] = xlib.CurrentTime;
146 + ev.data.l[1] = @intCast(self.manager_atom);
147 + ev.data.l[2] = @intCast(self.window);
148 + ev.data.l[3] = 0;
149 + ev.data.l[4] = 0;
150 +
151 + _ = xlib.XSendEvent(
152 + self.display,
153 + root,
154 + xlib.False,
155 + xlib.c.StructureNotifyMask,
156 + @ptrCast(&ev),
157 + );
158 + }
159 +
160 + fn sendXembedMessage(self: *Systray, win: xlib.Window, msg: c_long, detail: c_long, data1: c_long, data2: c_long) void {
161 + var ev: xlib.c.XClientMessageEvent = undefined;
162 + ev.type = xlib.ClientMessage;
163 + ev.window = win;
164 + ev.message_type = self.xembed;
165 + ev.format = 32;
166 + ev.data.l[0] = xlib.CurrentTime;
167 + ev.data.l[1] = msg;
168 + ev.data.l[2] = detail;
169 + ev.data.l[3] = data1;
170 + ev.data.l[4] = data2;
171 +
172 + _ = xlib.XSendEvent(
173 + self.display,
174 + win,
175 + xlib.False,
176 + xlib.c.NoEventMask,
177 + @ptrCast(&ev),
178 + );
179 + }
180 +
181 + fn sendConfigureNotify(self: *Systray, win: xlib.Window, x: i32, y: i32, w: i32, h: i32) void {
182 + var ev: xlib.c.XConfigureEvent = undefined;
183 + ev.type = xlib.ConfigureNotify;
184 + ev.event = win;
185 + ev.window = win;
186 + ev.x = x;
187 + ev.y = y;
188 + ev.width = w;
189 + ev.height = h;
190 + ev.border_width = 0;
191 + ev.above = xlib.None;
192 + ev.override_redirect = xlib.False;
193 +
194 + _ = xlib.XSendEvent(
195 + self.display,
196 + win,
197 + xlib.False,
198 + xlib.c.StructureNotifyMask,
199 + @ptrCast(&ev),
200 + );
201 + }
202 +
203 + fn getXembedInfo(self: *Systray, win: xlib.Window) ?struct { version: c_ulong, flags: c_ulong } {
204 + var actual_type: xlib.Atom = 0;
205 + var actual_format: c_int = 0;
206 + var nitems: c_ulong = 0;
207 + var bytes_after: c_ulong = 0;
208 + var data: [*c]u8 = null;
209 +
210 + const result = xlib.XGetWindowProperty(
211 + self.display,
212 + win,
213 + self.xembed_info,
214 + 0,
215 + 2,
216 + xlib.False,
217 + self.xembed_info,
218 + &actual_type,
219 + &actual_format,
220 + &nitems,
221 + &bytes_after,
222 + &data,
223 + );
224 +
225 + if (result != 0 or actual_type != self.xembed_info or actual_format != 32 or nitems < 2) {
226 + if (data != null) _ = xlib.XFree(data);
227 + return null;
228 + }
229 +
230 + const info_ptr: [*]c_ulong = @ptrCast(@alignCast(data));
231 + const version = info_ptr[0];
232 + const flags = info_ptr[1];
233 + _ = xlib.XFree(data);
234 +
235 + return .{ .version = version, .flags = flags };
236 + }
237 +
238 + pub fn handleClientMessage(self: *Systray, ev: *xlib.XClientMessageEvent) bool {
239 + if (ev.message_type != self.net_system_tray_opcode) return false;
240 +
241 + const opcode = ev.data.l[1];
242 + if (opcode == SYSTEM_TRAY_REQUEST_DOCK) {
243 + const icon_win: xlib.Window = @intCast(ev.data.l[2]);
244 + self.dockIcon(icon_win);
245 + return true;
246 + }
247 +
248 + return false;
249 + }
250 +
251 + fn updateIconGeometry(self: *Systray, icon: *Icon, w: i32, h: i32) void {
252 + _ = w;
253 + _ = h;
254 + icon.width = self.icon_size;
255 + icon.height = self.icon_size;
256 +
257 + _ = xlib.c.XResizeWindow(
258 + self.display,
259 + icon.window,
260 + @intCast(icon.width),
261 + @intCast(icon.height),
262 + );
263 + }
264 +
265 + fn dockIcon(self: *Systray, icon_win: xlib.Window) void {
266 + for (self.icons.items) |icon| {
267 + if (icon.window == icon_win) return;
268 + }
269 +
270 + var wa: xlib.XWindowAttributes = undefined;
271 + if (xlib.XGetWindowAttributes(self.display, icon_win, &wa) == 0) {
272 + return;
273 + }
274 +
275 + _ = xlib.c.XSetWindowBorderWidth(self.display, icon_win, 0);
276 +
277 + _ = xlib.c.XSelectInput(
278 + self.display,
279 + icon_win,
280 + xlib.c.StructureNotifyMask | xlib.c.PropertyChangeMask | xlib.c.ResizeRedirectMask,
281 + );
282 +
283 + _ = xlib.c.XReparentWindow(
284 + self.display,
285 + icon_win,
286 + self.window,
287 + 0,
288 + 0,
289 + );
290 +
291 + const info = self.getXembedInfo(icon_win);
292 + const mapped = if (info) |i| (i.flags & XEMBED_MAPPED) != 0 else true;
293 +
294 + var icon = Icon{
295 + .window = icon_win,
296 + .width = self.icon_size,
297 + .height = self.icon_size,
298 + .mapped = mapped,
299 + };
300 +
301 + self.updateIconGeometry(&icon, wa.width, wa.height);
302 +
303 + self.icons.append(self.allocator, icon) catch return;
304 +
305 + self.sendXembedMessage(icon_win, XEMBED_EMBEDDED_NOTIFY, 0, @intCast(self.window), XEMBED_VERSION);
306 + self.sendXembedMessage(icon_win, XEMBED_WINDOW_ACTIVATE, 0, 0, 0);
307 + self.sendXembedMessage(icon_win, XEMBED_FOCUS_IN, 0, 0, 0);
308 +
309 + if (mapped) {
310 + _ = xlib.XMapWindow(self.display, icon_win);
311 + }
312 +
313 + self.arrangeIcons();
314 + }
315 +
316 + pub fn handleConfigureRequest(self: *Systray, ev: *xlib.XConfigureRequestEvent) bool {
317 + for (self.icons.items) |*icon| {
318 + if (icon.window == ev.window) {
319 + if ((ev.value_mask & xlib.c.CWWidth) != 0 or (ev.value_mask & xlib.c.CWHeight) != 0) {
320 + self.updateIconGeometry(icon, ev.width, ev.height);
321 + self.arrangeIcons();
322 + }
323 + return true;
324 + }
325 + }
326 + return false;
327 + }
328 +
329 + pub fn handleResizeRequest(self: *Systray, win: xlib.Window, w: c_int, h: c_int) bool {
330 + for (self.icons.items) |*icon| {
331 + if (icon.window == win) {
332 + self.updateIconGeometry(icon, w, h);
333 + self.arrangeIcons();
334 + return true;
335 + }
336 + }
337 + return false;
338 + }
339 +
340 + pub fn handleDestroyNotify(self: *Systray, win: xlib.Window) bool {
341 + return self.removeIcon(win);
342 + }
343 +
344 + pub fn handleUnmapNotify(self: *Systray, win: xlib.Window) bool {
345 + for (self.icons.items) |*icon| {
346 + if (icon.window == win) {
347 + icon.mapped = false;
348 + self.arrangeIcons();
349 + return true;
350 + }
351 + }
352 + return false;
353 + }
354 +
355 + pub fn handleMapNotify(self: *Systray, win: xlib.Window) bool {
356 + for (self.icons.items) |*icon| {
357 + if (icon.window == win) {
358 + icon.mapped = true;
359 + self.arrangeIcons();
360 + return true;
361 + }
362 + }
363 + return false;
364 + }
365 +
366 + pub fn handlePropertyNotify(self: *Systray, ev: *xlib.XPropertyEvent) bool {
367 + if (ev.atom != self.xembed_info) return false;
368 +
369 + for (self.icons.items) |*icon| {
370 + if (icon.window == ev.window) {
371 + const info = self.getXembedInfo(icon.window) orelse return true;
372 + const should_map = (info.flags & XEMBED_MAPPED) != 0;
373 +
374 + if (should_map and !icon.mapped) {
375 + icon.mapped = true;
376 + _ = xlib.XMapWindow(self.display, icon.window);
377 + self.arrangeIcons();
378 + } else if (!should_map and icon.mapped) {
379 + icon.mapped = false;
380 + _ = xlib.c.XUnmapWindow(self.display, icon.window);
381 + self.arrangeIcons();
382 + }
383 + return true;
384 + }
385 + }
386 + return false;
387 + }
388 +
389 + pub fn handleReparentNotify(self: *Systray, win: xlib.Window, parent: xlib.Window) bool {
390 + if (parent != self.window) {
391 + return self.removeIcon(win);
392 + }
393 + return false;
394 + }
395 +
396 + fn removeIcon(self: *Systray, win: xlib.Window) bool {
397 + for (self.icons.items, 0..) |icon, i| {
398 + if (icon.window == win) {
399 + _ = self.icons.orderedRemove(i);
400 + self.arrangeIcons();
401 + return true;
402 + }
403 + }
404 + return false;
405 + }
406 +
407 + fn arrangeIcons(self: *Systray) void {
408 + const spacing: i32 = 4;
409 + var x: i32 = 0;
410 + const y: i32 = @divTrunc(self.bar_height - self.icon_size, 2);
411 +
412 + for (self.icons.items) |icon| {
413 + if (icon.mapped) {
414 + _ = xlib.XMoveWindow(self.display, icon.window, x, y);
415 + self.sendConfigureNotify(icon.window, x, y, icon.width, icon.height);
416 + x += icon.width + spacing;
417 + }
418 + }
419 +
420 + const total_width: u32 = if (x > 0) @intCast(x) else 1;
421 + _ = xlib.c.XResizeWindow(self.display, self.window, total_width, @intCast(self.bar_height - 2));
422 + _ = xlib.XSync(self.display, xlib.False);
423 + }
424 +
425 + pub fn width(self: *const Systray) i32 {
426 + const spacing: i32 = 4;
427 + var w: i32 = 0;
428 + for (self.icons.items) |icon| {
429 + if (icon.mapped) {
430 + w += icon.width + spacing;
431 + }
432 + }
433 + return w;
434 + }
435 +
436 + pub fn updatePosition(self: *Systray, x: i32, y: i32) void {
437 + _ = xlib.XMoveWindow(self.display, self.window, x, y);
438 + }
439 +
440 + pub fn isIconWindow(self: *const Systray, win: xlib.Window) bool {
441 + if (win == self.window) return true;
442 + for (self.icons.items) |icon| {
443 + if (icon.window == win) return true;
444 + }
445 + return false;
446 + }
447 +
448 + pub fn handleButtonPress(self: *Systray, ev: *xlib.XButtonEvent) bool {
449 + if (ev.window != self.window) {
450 + for (self.icons.items) |icon| {
451 + if (icon.window == ev.window) {
452 + self.sendButtonEvent(icon.window, ev, true);
453 + return true;
454 + }
455 + }
456 + return false;
457 + }
458 +
459 + const spacing: i32 = 4;
460 + var x: i32 = 0;
461 + for (self.icons.items) |icon| {
462 + if (icon.mapped) {
463 + if (ev.x >= x and ev.x < x + icon.width) {
464 + self.sendButtonEvent(icon.window, ev, true);
465 + return true;
466 + }
467 + x += icon.width + spacing;
468 + }
469 + }
470 + return false;
471 + }
472 +
473 + pub fn handleButtonRelease(self: *Systray, ev: *xlib.XButtonEvent) bool {
474 + if (ev.window != self.window) {
475 + for (self.icons.items) |icon| {
476 + if (icon.window == ev.window) {
477 + self.sendButtonEvent(icon.window, ev, false);
478 + return true;
479 + }
480 + }
481 + return false;
482 + }
483 +
484 + const spacing: i32 = 4;
485 + var x: i32 = 0;
486 + for (self.icons.items) |icon| {
487 + if (icon.mapped) {
488 + if (ev.x >= x and ev.x < x + icon.width) {
489 + self.sendButtonEvent(icon.window, ev, false);
490 + return true;
491 + }
492 + x += icon.width + spacing;
493 + }
494 + }
495 + return false;
496 + }
497 +
498 + fn sendButtonEvent(self: *Systray, win: xlib.Window, ev: *xlib.XButtonEvent, is_press: bool) void {
499 + var event: xlib.c.XButtonEvent = undefined;
500 + event.type = if (is_press) xlib.ButtonPress else xlib.ButtonRelease;
501 + event.window = win;
502 + event.root = xlib.XRootWindow(self.display, self.screen);
503 + event.subwindow = xlib.None;
504 + event.time = ev.time;
505 + event.x = ev.x;
506 + event.y = ev.y;
507 + event.x_root = ev.x_root;
508 + event.y_root = ev.y_root;
509 + event.state = ev.state;
510 + event.button = ev.button;
511 + event.same_screen = xlib.True;
512 +
513 + _ = xlib.XSendEvent(
514 + self.display,
515 + win,
516 + xlib.False,
517 + xlib.c.ButtonPressMask | xlib.c.ButtonReleaseMask,
518 + @ptrCast(&event),
519 + );
520 + _ = xlib.XSync(self.display, xlib.False);
521 + }
522 +};
+1 −0 src/config/config.zig
@@ -67,6 +67,7 @@ pub const BlockType = enum {
67 shell, 67 shell,
68 battery, 68 battery,
69 cpu_temp, 69 cpu_temp,
70 + systray,
70 }; 71 };
71 72
72 pub const ClickTarget = enum { 73 pub const ClickTarget = enum {
+11 −0 src/config/lua.zig
@@ -294,6 +294,9 @@ fn registerBarModule(state: *c.lua_State) void {
294 c.lua_pushcfunction(state, luaBarBlockBattery); 294 c.lua_pushcfunction(state, luaBarBlockBattery);
295 c.lua_setfield(state, -2, "battery"); 295 c.lua_setfield(state, -2, "battery");
296 296
297 + c.lua_pushcfunction(state, luaBarBlockSystray);
298 + c.lua_setfield(state, -2, "systray");
299 +
297 c.lua_setfield(state, -2, "block"); 300 c.lua_setfield(state, -2, "block");
298 301
299 c.lua_setfield(state, -2, "bar"); 302 c.lua_setfield(state, -2, "bar");
@@ -882,6 +885,8 @@ fn parseBlockConfig(state: *c.lua_State, idx: c_int) ?Block {
882 c.lua_settop(state, -2); 885 c.lua_settop(state, -2);
883 } 886 }
884 c.lua_settop(state, -2); 887 c.lua_settop(state, -2);
888 + } else if (std.mem.eql(u8, block_type_str, "Systray")) {
889 + block.block_type = .systray;
885 } else { 890 } else {
886 return null; 891 return null;
887 } 892 }
@@ -957,6 +962,12 @@ fn luaBarBlockStatic(state: ?*c.lua_State) callconv(.c) c_int {
957 return 1; 962 return 1;
958 } 963 }
959 964
965 +fn luaBarBlockSystray(state: ?*c.lua_State) callconv(.c) c_int {
966 + const s = state orelse return 0;
967 + createBlockTable(s, "Systray", null);
968 + return 1;
969 +}
970 +
960 fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int { 971 fn luaBarBlockBattery(state: ?*c.lua_State) callconv(.c) c_int {
961 const s = state orelse return 0; 972 const s = state orelse return 0;
962 973
+78 −1 src/wm/handlers.zig
@@ -31,9 +31,13 @@ pub fn handleEvent(event: *xlib.XEvent, wm: *WindowManager) void {
31 .motion_notify => handleMotionNotify(&event.xmotion, wm), 31 .motion_notify => handleMotionNotify(&event.xmotion, wm),
32 .client_message => handleClientMessage(&event.xclient, wm), 32 .client_message => handleClientMessage(&event.xclient, wm),
33 .button_press => handleButtonPress(&event.xbutton, wm), 33 .button_press => handleButtonPress(&event.xbutton, wm),
34 + .button_release => handleButtonRelease(&event.xbutton, wm),
34 .expose => handleExpose(&event.xexpose, wm), 35 .expose => handleExpose(&event.xexpose, wm),
35 .property_notify => handlePropertyNotify(&event.xproperty, wm), 36 .property_notify => handlePropertyNotify(&event.xproperty, wm),
36 .mapping_notify => handleMappingNotify(&event.xmapping, wm), 37 .mapping_notify => handleMappingNotify(&event.xmapping, wm),
38 + .reparent_notify => handleReparentNotify(&event.xreparent, wm),
39 + .map_notify => handleMapNotify(&event.xmap, wm),
40 + .resize_request => handleResizeRequest(&event.xresizerequest, wm),
37 else => {}, 41 else => {},
38 } 42 }
39 } 43 }
@@ -41,6 +45,10 @@ pub fn handleEvent(event: *xlib.XEvent, wm: *WindowManager) void {
41 fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void { 45 fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void {
42 std.debug.print("map_request: window=0x{x}\n", .{event.window}); 46 std.debug.print("map_request: window=0x{x}\n", .{event.window});
43 47
48 + if (wm.getSystray()) |tray| {
49 + if (tray.isIconWindow(event.window)) return;
50 + }
51 +
44 var window_attributes: xlib.XWindowAttributes = undefined; 52 var window_attributes: xlib.XWindowAttributes = undefined;
45 if (xlib.XGetWindowAttributes(wm.display.handle, event.window, &window_attributes) == 0) { 53 if (xlib.XGetWindowAttributes(wm.display.handle, event.window, &window_attributes) == 0) {
46 return; 54 return;
@@ -56,6 +64,13 @@ fn handleMapRequest(event: *xlib.XMapRequestEvent, wm: *WindowManager) void {
56 } 64 }
57 65
58 fn handleConfigureRequest(event: *xlib.XConfigureRequestEvent, wm: *WindowManager) void { 66 fn handleConfigureRequest(event: *xlib.XConfigureRequestEvent, wm: *WindowManager) void {
67 + if (wm.getSystray()) |tray| {
68 + if (tray.handleConfigureRequest(event)) {
69 + wm.invalidateBars();
70 + return;
71 + }
72 + }
73 +
59 const client = client_mod.windowToClient(wm.monitors, event.window); 74 const client = client_mod.windowToClient(wm.monitors, event.window);
60 75
61 if (client) |managed_client| { 76 if (client) |managed_client| {
@@ -216,6 +231,10 @@ fn handleConfigureNotify(event: *xlib.XConfigureEvent, wm: *WindowManager) void
216 fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void { 231 fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void {
217 std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow }); 232 std.debug.print("button_press: window=0x{x} subwindow=0x{x}\n", .{ event.window, event.subwindow });
218 233
234 + if (wm.getSystray()) |tray| {
235 + if (tray.handleButtonPress(event)) return;
236 + }
237 +
219 const clicked_monitor = monitor_mod.windowToMonitor(wm, event.window); 238 const clicked_monitor = monitor_mod.windowToMonitor(wm, event.window);
220 if (clicked_monitor) |monitor| { 239 if (clicked_monitor) |monitor| {
221 if (monitor != wm.selected_monitor) { 240 if (monitor != wm.selected_monitor) {
@@ -272,6 +291,13 @@ fn handleButtonPress(event: *xlib.XButtonEvent, wm: *WindowManager) void {
272 } 291 }
273 292
274 fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void { 293 fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) void {
294 + if (wm.getSystray()) |tray| {
295 + if (tray.handleClientMessage(event)) {
296 + wm.invalidateBars();
297 + return;
298 + }
299 + }
300 +
275 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; 301 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
276 302
277 if (event.message_type == wm.atoms.net_wm_state) { 303 if (event.message_type == wm.atoms.net_wm_state) {
@@ -301,12 +327,26 @@ fn handleClientMessage(event: *xlib.XClientMessageEvent, wm: *WindowManager) voi
301 } 327 }
302 328
303 fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void { 329 fn handleDestroyNotify(event: *xlib.XDestroyWindowEvent, wm: *WindowManager) void {
330 + if (wm.getSystray()) |tray| {
331 + if (tray.handleDestroyNotify(event.window)) {
332 + wm.invalidateBars();
333 + return;
334 + }
335 + }
336 +
304 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; 337 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
305 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window}); 338 std.debug.print("destroy_notify: window=0x{x}\n", .{event.window});
306 core.unmanage(client, wm); 339 core.unmanage(client, wm);
307 } 340 }
308 341
309 fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void { 342 fn handleUnmapNotify(event: *xlib.XUnmapEvent, wm: *WindowManager) void {
343 + if (wm.getSystray()) |tray| {
344 + if (tray.handleUnmapNotify(event.window)) {
345 + wm.invalidateBars();
346 + return;
347 + }
348 + }
349 +
310 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; 350 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
311 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window}); 351 std.debug.print("unmap_notify: window=0x{x}\n", .{event.window});
312 core.unmanage(client, wm); 352 core.unmanage(client, wm);
@@ -364,6 +404,13 @@ fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void {
364 return; 404 return;
365 } 405 }
366 406
407 + if (wm.getSystray()) |tray| {
408 + if (tray.handlePropertyNotify(event)) {
409 + wm.invalidateBars();
410 + return;
411 + }
412 + }
413 +
367 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return; 414 const client = client_mod.windowToClient(wm.monitors, event.window) orelse return;
368 415
369 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) { 416 if (event.atom == xlib.XA_WM_TRANSIENT_FOR) {
@@ -390,9 +437,39 @@ fn handlePropertyNotify(event: *xlib.XPropertyEvent, wm: *WindowManager) void {
390 437
391 fn handleMappingNotify(event: *xlib.XMappingEvent, wm: *WindowManager) void { 438 fn handleMappingNotify(event: *xlib.XMappingEvent, wm: *WindowManager) void {
392 _ = xlib.XRefreshKeyboardMapping(event); 439 _ = xlib.XRefreshKeyboardMapping(event);
393 - 440 +
394 if (event.request == xlib.MappingKeyboard or event.request == xlib.MappingModifier) { 441 if (event.request == xlib.MappingKeyboard or event.request == xlib.MappingModifier) {
395 wm.ungrabKeybinds(); 442 wm.ungrabKeybinds();
396 wm.grabKeybinds(); 443 wm.grabKeybinds();
397 } 444 }
398 } 445 }
446 +
447 +fn handleReparentNotify(event: *xlib.XReparentEvent, wm: *WindowManager) void {
448 + if (wm.getSystray()) |tray| {
449 + if (tray.handleReparentNotify(event.window, event.parent)) {
450 + wm.invalidateBars();
451 + }
452 + }
453 +}
454 +
455 +fn handleMapNotify(event: *xlib.c.XMapEvent, wm: *WindowManager) void {
456 + if (wm.getSystray()) |tray| {
457 + if (tray.handleMapNotify(event.window)) {
458 + wm.invalidateBars();
459 + }
460 + }
461 +}
462 +
463 +fn handleResizeRequest(event: *xlib.XResizeRequestEvent, wm: *WindowManager) void {
464 + if (wm.getSystray()) |tray| {
465 + if (tray.handleResizeRequest(event.window, event.width, event.height)) {
466 + wm.invalidateBars();
467 + }
468 + }
469 +}
470 +
471 +fn handleButtonRelease(event: *xlib.XButtonEvent, wm: *WindowManager) void {
472 + if (wm.getSystray()) |tray| {
473 + _ = tray.handleButtonRelease(event);
474 + }
475 +}
+21 −1 src/wm/wm.zig
@@ -419,6 +419,8 @@ 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;
423 + const want_systray = configHasSystray(self.config);
422 424
423 while (current_monitor) |monitor| { 425 while (current_monitor) |monitor| {
424 const bar = Bar.create( 426 const bar = Bar.create(
@@ -427,10 +429,12 @@ pub const WindowManager = struct {
427 self.display.screen, 429 self.display.screen,
428 monitor, 430 monitor,
429 self.config, 431 self.config,
432 + is_first_bar and want_systray,
430 ) orelse { 433 ) orelse {
431 current_monitor = monitor.next; 434 current_monitor = monitor.next;
432 continue; 435 continue;
433 }; 436 };
437 + is_first_bar = false;
434 438
435 if (tiling.bar_height == 0) { 439 if (tiling.bar_height == 0) {
436 tiling.setBarHeight(bar.height); 440 tiling.setBarHeight(bar.height);
@@ -453,7 +457,11 @@ pub const WindowManager = struct {
453 pub fn populateBarBlocks(self: *WindowManager, bar: *Bar) void { 457 pub fn populateBarBlocks(self: *WindowManager, bar: *Bar) void {
454 if (self.config.blocks.items.len > 0) { 458 if (self.config.blocks.items.len > 0) {
455 for (self.config.blocks.items) |cfg_block| { 459 for (self.config.blocks.items) |cfg_block| {
456 - bar.addBlock(configBlockToBarBlock(cfg_block)); 460 + if (cfg_block.block_type == .systray) {
461 + bar.setSystrayConfig(cfg_block.underline, cfg_block.color);
462 + } else {
463 + bar.addBlock(configBlockToBarBlock(cfg_block));
464 + }
457 } 465 }
458 } else { 466 } else {
459 bar.addBlock(blocks_mod.Block.initRam("", 5, 0x7aa2f7, true)); 467 bar.addBlock(blocks_mod.Block.initRam("", 5, 0x7aa2f7, true));
@@ -482,6 +490,10 @@ pub const WindowManager = struct {
482 return bar_mod.windowToBar(self.bars, win); 490 return bar_mod.windowToBar(self.bars, win);
483 } 491 }
484 492
493 + pub fn getSystray(self: *WindowManager) ?*bar_mod.Systray {
494 + return bar_mod.getSystray(self.bars);
495 + }
496 +
485 /// Refreshes the cached numlock modifier bitmask from the X server's 497 /// Refreshes the cached numlock modifier bitmask from the X server's
486 /// current modifier map. Called before any key or button grab so that 498 /// current modifier map. Called before any key or button grab so that
487 /// grabs cover all numlock combinations. 499 /// grabs cover all numlock combinations.
@@ -710,6 +722,13 @@ pub const WindowManager = struct {
710 } 722 }
711 }; 723 };
712 724
725 +fn configHasSystray(config: Config) bool {
726 + for (config.blocks.items) |block| {
727 + if (block.block_type == .systray) return true;
728 + }
729 + return false;
730 +}
731 +
713 /// Converts a config block description into a live status bar block. 732 /// Converts a config block description into a live status bar block.
714 pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block { 733 pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block {
715 var block = switch (cfg.block_type) { 734 var block = switch (cfg.block_type) {
@@ -745,6 +764,7 @@ pub fn configBlockToBarBlock(cfg: config_mod.Block) blocks_mod.Block {
745 cfg.color, 764 cfg.color,
746 cfg.underline, 765 cfg.underline,
747 ), 766 ),
767 + .systray => blocks_mod.Block.initStatic("", 0, false),
748 }; 768 };
749 block.click = cfg.click; 769 block.click = cfg.click;
750 return block; 770 return block;
+8 −0 src/x11/xlib.zig
@@ -275,3 +275,11 @@ 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 XResizeRequestEvent = c.XResizeRequestEvent;
+5 −0 templates/oxwm.lua
@@ -324,6 +324,11 @@ function oxwm.bar.block.static(config) end
324 ---@return table Block configuration 324 ---@return table Block configuration
325 function oxwm.bar.block.battery(config) end 325 function oxwm.bar.block.battery(config) end
326 326
327 +---Create a system tray block. Including this block enables the tray on the first bar; omitting it disables the tray entirely.
328 +---@param config {color: string|integer, underline: boolean}? Optional appearance config (only color and underline are used)
329 +---@return table Block configuration
330 +function oxwm.bar.block.systray(config) end
331 +
327 ---Set normal tag color scheme (unselected, no windows) 332 ---Set normal tag color scheme (unselected, no windows)
328 ---@param foreground string|integer Foreground color 333 ---@param foreground string|integer Foreground color
329 ---@param background string|integer Background color 334 ---@param background string|integer Background color