tony / oxwm

Branch: fd44600
Switch branches/tags
master
tonybanters Updated systray to support underlines in config.lua fd44600 2026-03-25
369 lines · 12.8 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 return switch (layout_index) {
15 0 => config.layout_tile_symbol,
16 1 => config.layout_monocle_symbol,
17 2 => config.layout_floating_symbol,
18 3 => config.layout_scrolling_symbol,
19 4 => "[#]",
20 else => "[?]",
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 const window = xlib.c.XCreateSimpleWindow(
84 display,
85 root,
86 monitor.mon_x,
87 monitor.mon_y,
88 @intCast(monitor.mon_w),
89 @intCast(bar_height),
90 0,
91 0,
92 0x1a1b26,
93 );
94
95 var attributes: xlib.c.XSetWindowAttributes = undefined;
96 attributes.override_redirect = xlib.True;
97 attributes.event_mask = xlib.c.ExposureMask | xlib.c.ButtonPressMask;
98 _ = xlib.c.XChangeWindowAttributes(display, window, xlib.c.CWOverrideRedirect | xlib.c.CWEventMask, &attributes);
99
100 const pixmap = xlib.XCreatePixmap(
101 display,
102 window,
103 @intCast(monitor.mon_w),
104 @intCast(bar_height),
105 @intCast(depth),
106 );
107
108 const graphics_context = xlib.XCreateGC(display, pixmap, 0, null);
109 const xft_draw = xlib.XftDrawCreate(display, pixmap, visual, colormap);
110
111 _ = xlib.XMapWindow(display, window);
112
113 const systray: ?*Systray = if (with_systray)
114 Systray.init(allocator, display, screen, window, bar_height, config.scheme_normal.background)
115 else
116 null;
117
118 bar.* = Bar{
119 .window = window,
120 .pixmap = pixmap,
121 .graphics_context = graphics_context,
122 .xft_draw = xft_draw,
123 .width = monitor.mon_w,
124 .height = bar_height,
125 .monitor = monitor,
126 .font = font,
127 .font_height = font_height,
128 .scheme_normal = config.scheme_normal,
129 .scheme_selected = config.scheme_selected,
130 .scheme_occupied = config.scheme_occupied,
131 .scheme_urgent = config.scheme_urgent,
132 .hide_vacant_tags = config.hide_vacant_tags,
133 .allocator = allocator,
134 .blocks = .empty,
135 .needs_redraw = true,
136 .next = null,
137 .systray = systray,
138 .systray_underline = false,
139 .systray_color = 0xffffff,
140 };
141
142 monitor.bar_win = window;
143 monitor.win_y = monitor.mon_y + bar_height;
144 monitor.win_h = monitor.mon_h - bar_height;
145
146 return bar;
147 }
148
149 /// Destroys the bar's X resources and frees the allocation.
150 pub fn destroy(self: *Bar, display: *xlib.Display) void {
151 if (self.systray) |tray| tray.deinit();
152
153 if (self.xft_draw) |xft_draw| xlib.XftDrawDestroy(xft_draw);
154 if (self.font) |font| xlib.XftFontClose(display, font);
155
156 _ = xlib.XFreeGC(display, self.graphics_context);
157 _ = xlib.XFreePixmap(display, self.pixmap);
158 _ = xlib.c.XDestroyWindow(display, self.window);
159 self.blocks.deinit(self.allocator);
160 self.allocator.destroy(self);
161 }
162
163 pub fn addBlock(self: *Bar, block: Block) void {
164 self.blocks.append(self.allocator, block) catch {};
165 }
166
167 pub fn setSystrayConfig(self: *Bar, underline: bool, col: u32) void {
168 self.systray_underline = underline;
169 self.systray_color = col;
170 }
171
172 pub fn clearBlocks(self: *Bar) void {
173 self.blocks.clearRetainingCapacity();
174 }
175
176 pub fn invalidate(self: *Bar) void {
177 self.needs_redraw = true;
178 }
179
180 /// Redraws the bar if marked dirty. Tags are taken from `config.tags`
181 pub fn draw(self: *Bar, display: *xlib.Display, config: config_mod.Config) void {
182 if (!self.needs_redraw) return;
183
184 self.fillRect(display, 0, 0, self.width, self.height, self.scheme_normal.background);
185
186 var x_position: i32 = 0;
187 const padding: i32 = 8;
188 const monitor = self.monitor;
189 const current_tags = monitor.tagset[monitor.sel_tags];
190
191 for (config.tags, 0..) |tag, index| {
192 const tag_mask: u32 = @as(u32, 1) << @intCast(index);
193 const is_selected = (current_tags & tag_mask) != 0;
194 const is_occupied = hasClientsOnTag(monitor, tag_mask);
195
196 if (self.hide_vacant_tags and !is_occupied and !is_selected) continue;
197
198 const scheme = if (is_selected)
199 self.scheme_selected
200 else if (is_occupied)
201 self.scheme_occupied
202 else
203 self.scheme_normal;
204
205 const tag_text_width = self.textWidth(display, tag);
206 const tag_width = tag_text_width + padding * 2;
207
208 if (is_selected) {
209 self.fillRect(display, x_position, self.height - 3, tag_width, 3, scheme.border);
210 }
211
212 const text_y = @divTrunc(self.height + self.font_height, 2) - 4;
213 self.drawText(display, x_position + padding, text_y, tag, scheme.foreground);
214 x_position += tag_width;
215 }
216
217 x_position += padding;
218
219 const layout_symbol = getLayoutSymbol(monitor.sel_lt, config);
220 self.drawText(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground);
221 x_position += self.textWidth(display, layout_symbol) + padding;
222
223 const systray_width: i32 = if (self.systray) |tray| tray.width() else 0;
224 var block_x: i32 = self.width - padding - systray_width;
225 if (systray_width > 0) block_x -= padding;
226
227 var block_index: usize = self.blocks.items.len;
228 while (block_index > 0) {
229 block_index -= 1;
230 const block = &self.blocks.items[block_index];
231 const content = block.getContent();
232 const content_width = self.textWidth(display, content);
233 block_x -= content_width;
234 self.drawText(display, block_x, @divTrunc(self.height + self.font_height, 2) - 4, content, block.color());
235 if (block.underline) {
236 self.fillRect(display, block_x, self.height - 2, content_width, 2, block.color());
237 }
238 block_x -= padding;
239 }
240
241 if (self.systray) |tray| {
242 const systray_x = self.width - systray_width - padding;
243 tray.updatePosition(systray_x, 0);
244 if (self.systray_underline and systray_width > 0) {
245 self.fillRect(display, systray_x, self.height - 2, systray_width, 2, self.systray_color);
246 }
247 }
248
249 _ = xlib.XCopyArea(display, self.pixmap, self.window, self.graphics_context, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0);
250 _ = xlib.XSync(display, xlib.False);
251
252 self.needs_redraw = false;
253 }
254
255 /// Returns the index of the tag the user clicked on, or null if the
256 /// click was outside the tag area.
257 pub fn handleClick(self: *Bar, display: *xlib.Display, click_x: i32, config: config_mod.Config) ?usize {
258 var x_position: i32 = 0;
259 const padding: i32 = 8;
260 const monitor = self.monitor;
261 const current_tags = monitor.tagset[monitor.sel_tags];
262
263 for (config.tags, 0..) |tag, index| {
264 const tag_mask = @as(u32, 1) << @intCast(index);
265 const is_selected = (current_tags & tag_mask) != 0;
266 const is_occupied = hasClientsOnTag(monitor, tag_mask);
267
268 if (self.hide_vacant_tags and !is_occupied and !is_selected) continue;
269
270 const tag_text_width = self.textWidth(display, tag);
271 const tag_width = tag_text_width + padding * 2;
272
273 if (click_x >= x_position and click_x < x_position + tag_width) {
274 return index;
275 }
276 x_position += tag_width;
277 }
278 return null;
279 }
280
281 /// Updates all blocks and marks the bar dirty if any block changed.
282 pub fn updateBlocks(self: *Bar) void {
283 var changed = false;
284 for (self.blocks.items) |*block| {
285 if (block.update()) changed = true;
286 }
287 if (changed) self.needs_redraw = true;
288 }
289
290 fn fillRect(self: *Bar, display: *xlib.Display, x: i32, y: i32, width: i32, height: i32, color: c_ulong) void {
291 _ = xlib.XSetForeground(display, self.graphics_context, color);
292 _ = xlib.XFillRectangle(display, self.pixmap, self.graphics_context, x, y, @intCast(width), @intCast(height));
293 }
294
295 fn drawText(self: *Bar, display: *xlib.Display, x: i32, y: i32, text: []const u8, color: c_ulong) void {
296 if (self.xft_draw == null or self.font == null) return;
297
298 var xft_color: xlib.XftColor = undefined;
299 var render_color: xlib.XRenderColor = undefined;
300 render_color.red = @intCast((color >> 16 & 0xff) * 257);
301 render_color.green = @intCast((color >> 8 & 0xff) * 257);
302 render_color.blue = @intCast((color & 0xff) * 257);
303 render_color.alpha = 0xffff;
304
305 const visual = xlib.XDefaultVisual(display, 0);
306 const colormap = xlib.XDefaultColormap(display, 0);
307
308 _ = xlib.XftColorAllocValue(display, visual, colormap, &render_color, &xft_color);
309 xlib.XftDrawStringUtf8(self.xft_draw, &xft_color, self.font, x, y, text.ptr, @intCast(text.len));
310 xlib.XftColorFree(display, visual, colormap, &xft_color);
311 }
312
313 fn textWidth(self: *Bar, display: *xlib.Display, text: []const u8) i32 {
314 if (self.font == null) return 0;
315
316 var extents: xlib.XGlyphInfo = undefined;
317 xlib.XftTextExtentsUtf8(display, self.font, text.ptr, @intCast(text.len), &extents);
318 return extents.xOff;
319 }
320 };
321
322 // Bar list helpers >.<
323
324 /// Marks all bars in the list as needing a redraw.
325 pub fn invalidateBars(bars: ?*Bar) void {
326 var current = bars;
327 while (current) |bar| {
328 bar.invalidate();
329 current = bar.next;
330 }
331 }
332
333 /// Destroys all bars in the list and frees their resources.
334 pub fn destroyBars(bars: ?*Bar, display: *xlib.Display) void {
335 var current = bars;
336 while (current) |bar| {
337 const next = bar.next;
338 bar.destroy(display);
339 current = next;
340 }
341 }
342
343 /// Returns the bar whose window matches `win`, or null.
344 pub fn windowToBar(bars: ?*Bar, win: xlib.Window) ?*Bar {
345 var current = bars;
346 while (current) |bar| {
347 if (bar.window == win) return bar;
348 current = bar.next;
349 }
350 return null;
351 }
352
353 fn hasClientsOnTag(monitor: *Monitor, tag_mask: u32) bool {
354 var current = monitor.clients;
355 while (current) |client| {
356 if ((client.tags & tag_mask) != 0) return true;
357 current = client.next;
358 }
359 return false;
360 }
361
362 pub fn getSystray(bars: ?*Bar) ?*Systray {
363 var current = bars;
364 while (current) |bar| {
365 if (bar.systray) |tray| return tray;
366 current = bar.next;
367 }
368 return null;
369 }