tony / oxwm

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