tony / oxwm

Branch: e4b7180
Switch branches/tags
master
tonybanters Updated systray to support underlines in config.lua 12a7a17 2026-03-25
392 lines · 13.7 KB Raw Blame History
1 const std = @import("std");
2 const xlib = @import("../x11/xlib.zig");
3 const monitor_mod = @import("../monitor.zig");
4 const blocks_mod = @import("blocks/blocks.zig");
5 const config_mod = @import("../config/config.zig");
6 const systray_mod = @import("systray.zig");
7 const ColorScheme = config_mod.ColorScheme;
8
9 const Monitor = monitor_mod.Monitor;
10 const Block = blocks_mod.Block;
11 pub const Systray = systray_mod.Systray;
12
13 fn getLayoutSymbol(layout_index: u32, config: config_mod.Config) []const u8 {
14 const layout = std.meta.intToEnum(config_mod.Layouts, layout_index) catch return "[?]";
15 return switch (layout) {
16 .tiling => config.layout_tile_symbol,
17 .monocle => config.layout_monocle_symbol,
18 .floating => config.layout_floating_symbol,
19 .scrolling => config.layout_scrolling_symbol,
20 .grid => config.layout_grid_symbol,
21 };
22 }
23
24 pub const Bar = struct {
25 window: xlib.Window,
26 pixmap: xlib.Pixmap,
27 graphics_context: xlib.GC,
28 xft_draw: ?*xlib.XftDraw,
29 width: i32,
30 height: i32,
31 monitor: *Monitor,
32
33 font: ?*xlib.XftFont,
34 font_height: i32,
35
36 scheme_normal: ColorScheme,
37 scheme_selected: ColorScheme,
38 scheme_occupied: ColorScheme,
39 scheme_urgent: ColorScheme,
40 hide_vacant_tags: bool,
41
42 allocator: std.mem.Allocator,
43 blocks: std.ArrayList(Block),
44 needs_redraw: bool,
45 next: ?*Bar,
46 systray: ?*Systray,
47 systray_underline: bool,
48 systray_color: u32,
49
50 /// Creates a bar window for `monitor` using the given config.
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.
53 pub fn create(
54 allocator: std.mem.Allocator,
55 display: *xlib.Display,
56 screen: c_int,
57 monitor: *Monitor,
58 config: config_mod.Config,
59 with_systray: bool,
60 ) ?*Bar {
61 const bar = allocator.create(Bar) catch return null;
62
63 const visual = xlib.XDefaultVisual(display, screen);
64 const colormap = xlib.XDefaultColormap(display, screen);
65 const depth = xlib.XDefaultDepth(display, screen);
66 const root = xlib.XRootWindow(display, screen);
67
68 const font_name_z = allocator.dupeZ(u8, config.font) catch {
69 allocator.destroy(bar);
70 return null;
71 };
72 defer allocator.free(font_name_z);
73
74 const font = xlib.XftFontOpenName(display, screen, font_name_z);
75 if (font == null) {
76 allocator.destroy(bar);
77 return null;
78 }
79
80 const font_height = font.*.ascent + font.*.descent;
81 const bar_height: i32 = @intCast(@as(i32, font_height) + 8);
82
83 var bar_y: i32 = 0;
84
85 if (std.mem.eql(u8, config.bar_position, "top")) {
86 bar_y = monitor.mon_y;
87 monitor.win_y = monitor.mon_y + bar_height;
88 } else if (std.mem.eql(u8, config.bar_position, "bottom")) {
89 bar_y = monitor.mon_y + monitor.mon_h - bar_height;
90 monitor.win_y = monitor.mon_y;
91 } else {
92 return null;
93 }
94
95 const window = xlib.c.XCreateSimpleWindow(
96 display,
97 root,
98 monitor.mon_x,
99 bar_y,
100 @intCast(monitor.mon_w),
101 @intCast(bar_height),
102 0,
103 0,
104 0x1a1b26,
105 );
106
107 var attributes: xlib.c.XSetWindowAttributes = undefined;
108 attributes.override_redirect = xlib.True;
109 attributes.event_mask = xlib.c.ExposureMask | xlib.c.ButtonPressMask;
110 _ = xlib.c.XChangeWindowAttributes(display, window, xlib.c.CWOverrideRedirect | xlib.c.CWEventMask, &attributes);
111
112 const pixmap = xlib.XCreatePixmap(
113 display,
114 window,
115 @intCast(monitor.mon_w),
116 @intCast(bar_height),
117 @intCast(depth),
118 );
119
120 const graphics_context = xlib.XCreateGC(display, pixmap, 0, null);
121 const xft_draw = xlib.XftDrawCreate(display, pixmap, visual, colormap);
122
123 _ = xlib.XMapWindow(display, window);
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
130 bar.* = Bar{
131 .window = window,
132 .pixmap = pixmap,
133 .graphics_context = graphics_context,
134 .xft_draw = xft_draw,
135 .width = monitor.mon_w,
136 .height = bar_height,
137 .monitor = monitor,
138 .font = font,
139 .font_height = font_height,
140 .scheme_normal = config.scheme_normal,
141 .scheme_selected = config.scheme_selected,
142 .scheme_occupied = config.scheme_occupied,
143 .scheme_urgent = config.scheme_urgent,
144 .hide_vacant_tags = config.hide_vacant_tags,
145 .allocator = allocator,
146 .blocks = .empty,
147 .needs_redraw = true,
148 .next = null,
149 .systray = systray,
150 .systray_underline = false,
151 .systray_color = 0xffffff,
152 };
153
154 monitor.bar_win = window;
155 monitor.win_h = monitor.mon_h - bar_height;
156
157 return bar;
158 }
159
160 /// Destroys the bar's X resources and frees the allocation.
161 pub fn destroy(self: *Bar, display: *xlib.Display) void {
162 if (self.systray) |tray| tray.deinit();
163
164 if (self.xft_draw) |xft_draw| xlib.XftDrawDestroy(xft_draw);
165 if (self.font) |font| xlib.XftFontClose(display, font);
166
167 _ = xlib.XFreeGC(display, self.graphics_context);
168 _ = xlib.XFreePixmap(display, self.pixmap);
169 _ = xlib.c.XDestroyWindow(display, self.window);
170 self.blocks.deinit(self.allocator);
171 self.allocator.destroy(self);
172 }
173
174 pub fn addBlock(self: *Bar, block: Block) void {
175 self.blocks.append(self.allocator, block) catch {};
176 }
177
178 pub fn setSystrayConfig(self: *Bar, underline: bool, col: u32) void {
179 self.systray_underline = underline;
180 self.systray_color = col;
181 }
182
183 pub fn clearBlocks(self: *Bar) void {
184 self.blocks.clearRetainingCapacity();
185 }
186
187 pub fn invalidate(self: *Bar) void {
188 self.needs_redraw = true;
189 }
190
191 /// Redraws the bar if marked dirty. Tags are taken from `config.tags`
192 pub fn draw(self: *Bar, display: *xlib.Display, config: config_mod.Config) void {
193 if (!self.needs_redraw) return;
194
195 self.fillRect(display, 0, 0, self.width, self.height, self.scheme_normal.background);
196
197 var x_position: i32 = 0;
198 const padding: i32 = 8;
199 const monitor = self.monitor;
200 const current_tags = monitor.tagset[monitor.sel_tags];
201
202 for (config.tags, 0..) |tag, index| {
203 const tag_mask: u32 = @as(u32, 1) << @intCast(index);
204 const is_selected = (current_tags & tag_mask) != 0;
205 const is_occupied = hasClientsOnTag(monitor, tag_mask);
206
207 if (self.hide_vacant_tags and !is_occupied and !is_selected) continue;
208
209 const scheme = if (is_selected)
210 self.scheme_selected
211 else if (is_occupied)
212 self.scheme_occupied
213 else
214 self.scheme_normal;
215
216 const tag_text_width = self.textWidth(display, tag);
217 const tag_width = tag_text_width + padding * 2;
218
219 if (is_selected) {
220 self.fillRect(display, x_position, self.height - 3, tag_width, 3, scheme.border);
221 }
222
223 const text_y = @divTrunc(self.height + self.font_height, 2) - 4;
224 self.drawText(display, x_position + padding, text_y, tag, scheme.foreground);
225 x_position += tag_width;
226 }
227
228 x_position += padding;
229
230 const layout_symbol = getLayoutSymbol(monitor.sel_lt, config);
231 self.drawText(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground);
232 x_position += self.textWidth(display, layout_symbol) + padding;
233
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
238 var block_index: usize = self.blocks.items.len;
239 while (block_index > 0) {
240 block_index -= 1;
241 const block = &self.blocks.items[block_index];
242 const content = block.getContent();
243 const content_width = self.textWidth(display, content);
244 block_x -= content_width;
245 block.x_start = block_x;
246 block.x_end = block_x + content_width;
247 self.drawText(display, block_x, @divTrunc(self.height + self.font_height, 2) - 4, content, block.color());
248 if (block.underline) {
249 self.fillRect(display, block_x, self.height - 2, content_width, 2, block.color());
250 }
251 block_x -= padding;
252 }
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
262 _ = xlib.XCopyArea(display, self.pixmap, self.window, self.graphics_context, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0);
263 _ = xlib.XSync(display, xlib.False);
264
265 self.needs_redraw = false;
266 }
267
268 /// Returns the index of the tag the user clicked on, or null if the
269 /// click was outside the tag area.
270 pub fn handleClick(self: *Bar, display: *xlib.Display, click_x: i32, config: config_mod.Config) ?usize {
271 var x_position: i32 = 0;
272 const padding: i32 = 8;
273 const monitor = self.monitor;
274 const current_tags = monitor.tagset[monitor.sel_tags];
275
276 for (config.tags, 0..) |tag, index| {
277 const tag_mask = @as(u32, 1) << @intCast(index);
278 const is_selected = (current_tags & tag_mask) != 0;
279 const is_occupied = hasClientsOnTag(monitor, tag_mask);
280
281 if (self.hide_vacant_tags and !is_occupied and !is_selected) continue;
282
283 const tag_text_width = self.textWidth(display, tag);
284 const tag_width = tag_text_width + padding * 2;
285
286 if (click_x >= x_position and click_x < x_position + tag_width) {
287 return index;
288 }
289 x_position += tag_width;
290 }
291 return null;
292 }
293
294 /// Returns the click action of the block the user clicked on, or null.
295 pub fn handleBlockClick(self: *Bar, click_x: i32) ?config_mod.ClickAction {
296 for (self.blocks.items) |*block| {
297 if (block.click != null and click_x >= block.x_start and click_x < block.x_end) {
298 return block.click;
299 }
300 }
301 return null;
302 }
303
304 /// Updates all blocks and marks the bar dirty if any block changed.
305 pub fn updateBlocks(self: *Bar) void {
306 var changed = false;
307 for (self.blocks.items) |*block| {
308 if (block.update()) changed = true;
309 }
310 if (changed) self.needs_redraw = true;
311 }
312
313 fn fillRect(self: *Bar, display: *xlib.Display, x: i32, y: i32, width: i32, height: i32, color: c_ulong) void {
314 _ = xlib.XSetForeground(display, self.graphics_context, color);
315 _ = xlib.XFillRectangle(display, self.pixmap, self.graphics_context, x, y, @intCast(width), @intCast(height));
316 }
317
318 fn drawText(self: *Bar, display: *xlib.Display, x: i32, y: i32, text: []const u8, color: c_ulong) void {
319 if (self.xft_draw == null or self.font == null) return;
320
321 var xft_color: xlib.XftColor = undefined;
322 var render_color: xlib.XRenderColor = undefined;
323 render_color.red = @intCast((color >> 16 & 0xff) * 257);
324 render_color.green = @intCast((color >> 8 & 0xff) * 257);
325 render_color.blue = @intCast((color & 0xff) * 257);
326 render_color.alpha = 0xffff;
327
328 const visual = xlib.XDefaultVisual(display, 0);
329 const colormap = xlib.XDefaultColormap(display, 0);
330
331 _ = xlib.XftColorAllocValue(display, visual, colormap, &render_color, &xft_color);
332 xlib.XftDrawStringUtf8(self.xft_draw, &xft_color, self.font, x, y, text.ptr, @intCast(text.len));
333 xlib.XftColorFree(display, visual, colormap, &xft_color);
334 }
335
336 fn textWidth(self: *Bar, display: *xlib.Display, text: []const u8) i32 {
337 if (self.font == null) return 0;
338
339 var extents: xlib.XGlyphInfo = undefined;
340 xlib.XftTextExtentsUtf8(display, self.font, text.ptr, @intCast(text.len), &extents);
341 return extents.xOff;
342 }
343 };
344
345 // Bar list helpers >.<
346
347 /// Marks all bars in the list as needing a redraw.
348 pub fn invalidateBars(bars: ?*Bar) void {
349 var current = bars;
350 while (current) |bar| {
351 bar.invalidate();
352 current = bar.next;
353 }
354 }
355
356 /// Destroys all bars in the list and frees their resources.
357 pub fn destroyBars(bars: ?*Bar, display: *xlib.Display) void {
358 var current = bars;
359 while (current) |bar| {
360 const next = bar.next;
361 bar.destroy(display);
362 current = next;
363 }
364 }
365
366 /// Returns the bar whose window matches `win`, or null.
367 pub fn windowToBar(bars: ?*Bar, win: xlib.Window) ?*Bar {
368 var current = bars;
369 while (current) |bar| {
370 if (bar.window == win) return bar;
371 current = bar.next;
372 }
373 return null;
374 }
375
376 fn hasClientsOnTag(monitor: *Monitor, tag_mask: u32) bool {
377 var current = monitor.clients;
378 while (current) |client| {
379 if ((client.tags & tag_mask) != 0) return true;
380 current = client.next;
381 }
382 return false;
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 }