tony / oxwm

Branch: 5660dcb
Switch branches/tags
master
emzywastaken refactor(config): remove globals 2141216 2026-02-20
361 lines · 11.9 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 client_mod = @import("../client.zig");
5 const blocks_mod = @import("blocks/blocks.zig");
6 const config_mod = @import("../config/config.zig");
7 const ColorScheme = config_mod.ColorScheme;
8
9 const Monitor = monitor_mod.Monitor;
10 const Block = blocks_mod.Block;
11
12 fn get_layout_symbol(layout_index: u32, config: ?config_mod.Config) []const u8 {
13 if (config) |conf| {
14 return switch (layout_index) {
15 0 => conf.layout_tile_symbol,
16 1 => conf.layout_monocle_symbol,
17 2 => conf.layout_floating_symbol,
18 3 => "[S]",
19 4 => "[#]",
20 else => "[?]",
21 };
22 }
23 return switch (layout_index) {
24 0 => "[]=",
25 1 => "[M]",
26 2 => "><>",
27 3 => "[S]",
28 4 => "[#]",
29 else => "[?]",
30 };
31 }
32
33 pub const Bar = struct {
34 window: xlib.Window,
35 pixmap: xlib.Pixmap,
36 graphics_context: xlib.GC,
37 xft_draw: ?*xlib.XftDraw,
38 width: i32,
39 height: i32,
40 monitor: *Monitor,
41
42 font: ?*xlib.XftFont,
43 font_height: i32,
44
45 scheme_normal: ColorScheme,
46 scheme_selected: ColorScheme,
47 scheme_occupied: ColorScheme,
48 scheme_urgent: ColorScheme,
49 hide_vacant_tags: bool,
50
51 allocator: std.mem.Allocator,
52 blocks: std.ArrayList(Block),
53 needs_redraw: bool,
54 next: ?*Bar,
55
56 pub fn create(
57 allocator: std.mem.Allocator,
58 display: *xlib.Display,
59 screen: c_int,
60 monitor: *Monitor,
61 config: config_mod.Config,
62 ) ?*Bar {
63 const bar = allocator.create(Bar) catch return null;
64
65 const visual = xlib.XDefaultVisual(display, screen);
66 const colormap = xlib.XDefaultColormap(display, screen);
67 const depth = xlib.XDefaultDepth(display, screen);
68 const root = xlib.XRootWindow(display, screen);
69
70 const font_name_z = allocator.dupeZ(u8, config.font) catch return null;
71 defer allocator.free(font_name_z);
72
73 const font = xlib.XftFontOpenName(display, screen, font_name_z);
74 if (font == null) {
75 allocator.destroy(bar);
76 return null;
77 }
78
79 const font_height = font.*.ascent + font.*.descent;
80 const bar_height: i32 = @intCast(@as(i32, font_height) + 8);
81
82 const window = xlib.c.XCreateSimpleWindow(
83 display,
84 root,
85 monitor.mon_x,
86 monitor.mon_y,
87 @intCast(monitor.mon_w),
88 @intCast(bar_height),
89 0,
90 0,
91 0x1a1b26,
92 );
93
94 _ = xlib.c.XSetWindowAttributes{};
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
110 const xft_draw = xlib.XftDrawCreate(display, pixmap, visual, colormap);
111
112 _ = xlib.XMapWindow(display, window);
113
114 bar.* = Bar{
115 .window = window,
116 .pixmap = pixmap,
117 .graphics_context = graphics_context,
118 .xft_draw = xft_draw,
119 .width = monitor.mon_w,
120 .height = bar_height,
121 .monitor = monitor,
122 .font = font,
123 .font_height = font_height,
124 .scheme_normal = config.scheme_normal,
125 .scheme_selected = config.scheme_selected,
126 .scheme_occupied = config.scheme_occupied,
127 .scheme_urgent = config.scheme_urgent,
128 .hide_vacant_tags = config.hide_vacant_tags,
129 .allocator = allocator,
130 .blocks = .{},
131 .needs_redraw = true,
132 .next = null,
133 };
134
135 monitor.bar_win = window;
136 monitor.win_y = monitor.mon_y + bar_height;
137 monitor.win_h = monitor.mon_h - bar_height;
138
139 return bar;
140 }
141
142 pub fn destroy(self: *Bar, allocator: std.mem.Allocator, display: *xlib.Display) void {
143 if (self.xft_draw) |xft_draw| {
144 xlib.XftDrawDestroy(xft_draw);
145 }
146 if (self.font) |font| {
147 xlib.XftFontClose(display, font);
148 }
149 _ = xlib.XFreeGC(display, self.graphics_context);
150 _ = xlib.XFreePixmap(display, self.pixmap);
151 _ = xlib.c.XDestroyWindow(display, self.window);
152 self.blocks.deinit(self.allocator);
153 allocator.destroy(self);
154 }
155
156 pub fn add_block(self: *Bar, block: Block) void {
157 self.blocks.append(self.allocator, block) catch {};
158 }
159
160 pub fn invalidate(self: *Bar) void {
161 self.needs_redraw = true;
162 }
163
164 pub fn draw(self: *Bar, display: *xlib.Display, tags: []const []const u8, config: ?config_mod.Config) void {
165 if (!self.needs_redraw) return;
166
167 self.fill_rect(display, 0, 0, self.width, self.height, self.scheme_normal.background);
168
169 var x_position: i32 = 0;
170 const padding: i32 = 8;
171 const monitor = self.monitor;
172 const current_tags = monitor.tagset[monitor.sel_tags];
173
174 for (tags, 0..) |tag, index| {
175 const tag_mask: u32 = @as(u32, 1) << @intCast(index);
176 const is_selected = (current_tags & tag_mask) != 0;
177 const is_occupied = has_clients_on_tag(monitor, tag_mask);
178
179 if (self.hide_vacant_tags and !is_occupied and !is_selected) continue;
180
181 const scheme = if (is_selected) self.scheme_selected else if (is_occupied) self.scheme_occupied else self.scheme_normal;
182
183 const tag_text_width = self.text_width(display, tag);
184 const tag_width = tag_text_width + padding * 2;
185
186 if (is_selected) {
187 self.fill_rect(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.draw_text(display, x_position + padding, text_y, tag, scheme.foreground);
192
193 x_position += tag_width;
194 }
195
196 x_position += padding;
197
198 const layout_symbol = get_layout_symbol(monitor.sel_lt, config);
199 self.draw_text(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground);
200 x_position += self.text_width(display, layout_symbol) + padding;
201
202 var block_x: i32 = self.width - padding;
203 var block_index: usize = self.blocks.items.len;
204 while (block_index > 0) {
205 block_index -= 1;
206 const block = &self.blocks.items[block_index];
207 const content = block.get_content();
208 const content_width = self.text_width(display, content);
209 block_x -= content_width;
210 self.draw_text(display, block_x, @divTrunc(self.height + self.font_height, 2) - 4, content, block.color());
211 if (block.underline) {
212 self.fill_rect(display, block_x, self.height - 2, content_width, 2, block.color());
213 }
214 block_x -= padding;
215 }
216
217 _ = xlib.XCopyArea(display, self.pixmap, self.window, self.graphics_context, 0, 0, @intCast(self.width), @intCast(self.height), 0, 0);
218 _ = xlib.XSync(display, xlib.False);
219
220 self.needs_redraw = false;
221 }
222
223 fn fill_rect(self: *Bar, display: *xlib.Display, x: i32, y: i32, width: i32, height: i32, color: c_ulong) void {
224 _ = xlib.XSetForeground(display, self.graphics_context, color);
225 _ = xlib.XFillRectangle(display, self.pixmap, self.graphics_context, x, y, @intCast(width), @intCast(height));
226 }
227
228 fn draw_text(self: *Bar, display: *xlib.Display, x: i32, y: i32, text: []const u8, color: c_ulong) void {
229 if (self.xft_draw == null or self.font == null) return;
230
231 var xft_color: xlib.XftColor = undefined;
232 var render_color: xlib.XRenderColor = undefined;
233 render_color.red = @intCast((color >> 16 & 0xff) * 257);
234 render_color.green = @intCast((color >> 8 & 0xff) * 257);
235 render_color.blue = @intCast((color & 0xff) * 257);
236 render_color.alpha = 0xffff;
237
238 const visual = xlib.XDefaultVisual(display, 0);
239 const colormap = xlib.XDefaultColormap(display, 0);
240
241 _ = xlib.XftColorAllocValue(display, visual, colormap, &render_color, &xft_color);
242
243 xlib.XftDrawStringUtf8(self.xft_draw, &xft_color, self.font, x, y, text.ptr, @intCast(text.len));
244
245 xlib.XftColorFree(display, visual, colormap, &xft_color);
246 }
247
248 fn text_width(self: *Bar, display: *xlib.Display, text: []const u8) i32 {
249 if (self.font == null) return 0;
250
251 var extents: xlib.XGlyphInfo = undefined;
252 xlib.XftTextExtentsUtf8(display, self.font, text.ptr, @intCast(text.len), &extents);
253 return extents.xOff;
254 }
255
256 pub fn handle_click(self: *Bar, click_x: i32, tags: []const []const u8) ?usize {
257 var x_position: i32 = 0;
258 const padding: i32 = 8;
259 const display = xlib.c.XOpenDisplay(null) orelse return null;
260 defer _ = xlib.XCloseDisplay(display);
261
262 const monitor = self.monitor;
263 const current_tags = monitor.tagset[monitor.sel_tags];
264 for (tags, 0..) |tag, index| {
265 const tag_mask = @as(u32, 1) << @intCast(index);
266 const is_selected = (current_tags & tag_mask) != 0;
267 const is_occupied = has_clients_on_tag(monitor, tag_mask);
268
269 if (self.hide_vacant_tags and !is_occupied and !is_selected) continue;
270
271 const tag_text_width = self.text_width(display, tag);
272 const tag_width = tag_text_width + padding * 2;
273
274 if (click_x >= x_position and click_x < x_position + tag_width) {
275 return index;
276 }
277 x_position += tag_width;
278 }
279 return null;
280 }
281
282 pub fn update_blocks(self: *Bar) void {
283 var changed = false;
284 for (self.blocks.items) |*block| {
285 if (block.update()) {
286 changed = true;
287 }
288 }
289 if (changed) {
290 self.needs_redraw = true;
291 }
292 }
293
294 pub fn clear_blocks(self: *Bar) void {
295 self.blocks.clearRetainingCapacity();
296 }
297 };
298
299 fn has_clients_on_tag(monitor: *Monitor, tag_mask: u32) bool {
300 var current = monitor.clients;
301 while (current) |client| {
302 if ((client.tags & tag_mask) != 0) {
303 return true;
304 }
305 current = client.next;
306 }
307 return false;
308 }
309
310 pub var bars: ?*Bar = null;
311
312 pub fn create_bars(allocator: std.mem.Allocator, display: *xlib.Display, screen: c_int) void {
313 var current_monitor = monitor_mod.monitors;
314 while (current_monitor) |monitor| {
315 const bar = Bar.create(allocator, display, screen, monitor, "monospace:size=10");
316 if (bar) |created_bar| {
317 bars = created_bar;
318 }
319 current_monitor = monitor.next;
320 }
321 }
322
323 pub fn draw_bars(display: *xlib.Display, tags: []const []const u8) void {
324 var current_monitor = monitor_mod.monitors;
325 while (current_monitor) |monitor| {
326 _ = monitor;
327 if (bars) |bar| {
328 bar.draw(display, tags, null);
329 }
330 current_monitor = if (current_monitor) |m| m.next else null;
331 }
332 }
333
334 pub fn invalidate_bars() void {
335 var current = bars;
336 while (current) |bar| {
337 bar.invalidate();
338 current = bar.next;
339 }
340 }
341
342 pub fn destroy_bars(allocator: std.mem.Allocator, display: *xlib.Display) void {
343 var current = bars;
344 while (current) |bar| {
345 const next = bar.next;
346 bar.destroy(allocator, display);
347 current = next;
348 }
349 bars = null;
350 }
351
352 pub fn window_to_bar(win: xlib.Window) ?*Bar {
353 var current = bars;
354 while (current) |bar| {
355 if (bar.window == win) {
356 return bar;
357 }
358 current = bar.next;
359 }
360 return null;
361 }