tony / oxwm

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