tony / oxwm

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