tony / oxwm

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