tony / oxwm

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