tony / oxwm

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