Refactor Bar/Output surfaces into anon struct

Both the wl_surface and layer_surface in each are always expected to
exist at the same time. Since they're optional, it makes more sense to
combine them into a single optional struct.
This commit is contained in:
Ben Buhse 2026-02-15 18:02:48 -06:00
commit 43ebdd273c
No known key found for this signature in database
GPG key ID: 7916ACFCD38FD0B4
3 changed files with 52 additions and 43 deletions

View file

@ -21,8 +21,10 @@ output: *Output,
width: u31 = 0, width: u31 = 0,
height: u31 = 0, height: u31 = 0,
wl_surface: ?*wl.Surface = null, surfaces: ?struct {
layer_surface: ?*zwlr.LayerSurfaceV1 = null, wl_surface: *wl.Surface,
layer_surface: *zwlr.LayerSurfaceV1,
} = null,
configured: bool = false, configured: bool = false,
@ -43,34 +45,37 @@ pub fn init(context: *Context, output: *Output) !Bar {
// TODO: Add config options for whether it's top or bottom // TODO: Add config options for whether it's top or bottom
pub fn initSurface(bar: *Bar) !void { pub fn initSurface(bar: *Bar) !void {
if (bar.layer_surface) |_| { if (bar.surfaces) |_| {
// This bar already has a layer surface, we can exit early // This bar already has a surface, we can exit early
return; return;
} }
const context = bar.context; const context = bar.context;
// TODO: Add padding to config
const vertical_padding = 5;
const bar_height: u31 = @intCast(bar.fonts.height + 2 * vertical_padding);
const wl_surface = try context.wl_compositor.createSurface(); const wl_surface = try context.wl_compositor.createSurface();
errdefer wl_surface.destroy(); errdefer wl_surface.destroy();
const layer_surface = try context
.zwlr_layer_shell_v1
.getLayerSurface(wl_surface, bar.output.wl_output, .top, "beansprout-bar");
errdefer layer_surface.destroy();
// TODO: Allow clicking on tags to switch between them? // TODO: Allow clicking on tags to switch between them?
// We don't want our surface to have any input region (default is infinite) // We don't want our surface to have any input region (default is infinite)
const empty_region = try context.wl_compositor.createRegion(); const empty_region = try context.wl_compositor.createRegion();
defer empty_region.destroy(); defer empty_region.destroy();
wl_surface.setInputRegion(empty_region); wl_surface.setInputRegion(empty_region);
const layer_surface = try context // TODO: Add padding to config
.zwlr_layer_shell_v1 const vertical_padding = 5;
.getLayerSurface(wl_surface, bar.output.wl_output, .top, "beansprout-bar"); const bar_height: u31 = @intCast(bar.fonts.height + 2 * vertical_padding);
layer_surface.setSize(0, bar_height); layer_surface.setSize(0, bar_height);
layer_surface.setAnchor(.{ .top = true, .right = true, .left = true }); layer_surface.setAnchor(.{ .top = true, .right = true, .left = true });
bar.wl_surface = wl_surface; bar.surfaces = .{
bar.layer_surface = layer_surface; .wl_surface = wl_surface,
.layer_surface = layer_surface,
};
context.buffer_pool.surface_count += 1; context.buffer_pool.surface_count += 1;
layer_surface.setListener(*Bar, layerSurfaceListener, bar); layer_surface.setListener(*Bar, layerSurfaceListener, bar);
@ -81,11 +86,9 @@ pub fn deinit(bar: *Bar) void {
bar.configured = false; bar.configured = false;
bar.timezone.deinit(); bar.timezone.deinit();
bar.fonts.destroy(); bar.fonts.destroy();
if (bar.wl_surface) |wl_surface| { if (bar.surfaces) |surfaces| {
wl_surface.destroy(); surfaces.wl_surface.destroy();
} surfaces.layer_surface.destroy();
if (bar.layer_surface) |layer_surface| {
layer_surface.destroy();
bar.context.buffer_pool.surface_count -= 1; bar.context.buffer_pool.surface_count -= 1;
} }
} }
@ -106,15 +109,15 @@ pub fn layerSurfaceListener(
bar.height == height and bar.height == height and
bar.output.scale == bar.font_scale) bar.output.scale == bar.font_scale)
{ {
if (bar.wl_surface) |wl_surface| { if (bar.surfaces) |surfaces| {
wl_surface.commit(); surfaces.wl_surface.commit();
} else { } else {
log.warn("Bar is marked as configured but is missing a layer_surface for the wallpaper", .{}); log.warn("Bar is marked as configured but is missing its surfaces.", .{});
} }
return; return;
} }
log.debug("configuring bar surface with width {} and height {}", .{ width, height }); log.debug("Configuring bar surface with width {} and height {}", .{ width, height });
bar.width = width; bar.width = width;
bar.height = height; bar.height = height;
// Excluse zone == the bar's height // Excluse zone == the bar's height
@ -128,7 +131,7 @@ pub fn layerSurfaceListener(
// TODO: Need to change the x/y if we support anchoring to the bottom // TODO: Need to change the x/y if we support anchoring to the bottom
opaque_region.add(0, 0, bar.width, bar.height); opaque_region.add(0, 0, bar.width, bar.height);
defer opaque_region.destroy(); defer opaque_region.destroy();
bar.wl_surface.?.setOpaqueRegion(opaque_region); bar.surfaces.?.wl_surface.setOpaqueRegion(opaque_region);
bar.configured = true; bar.configured = true;
@ -222,7 +225,8 @@ pub fn render(bar: *Bar) !void {
try bar.renderChars(codepoints, buffer, &x, y, color); try bar.renderChars(codepoints, buffer, &x, y, color);
// Finally, attach the buffer to the surface // Finally, attach the buffer to the surface
const wl_surface = bar.wl_surface orelse return; const surfaces = bar.surfaces orelse return error.NoSurfaces;
const wl_surface = surfaces.wl_surface;
wl_surface.setBufferScale(scale); wl_surface.setBufferScale(scale);
wl_surface.attach(buffer.wl_buffer, 0, 0); wl_surface.attach(buffer.wl_buffer, 0, 0);
wl_surface.damageBuffer(0, 0, render_width, render_height); wl_surface.damageBuffer(0, 0, render_width, render_height);

View file

@ -143,7 +143,7 @@ pub fn manage(context: *Context) void {
while (out_it.next()) |output| { while (out_it.next()) |output| {
if (context.wallpaper_image == null) { if (context.wallpaper_image == null) {
output.deinitWallpaperLayerSurface(); output.deinitWallpaperLayerSurface();
} else if (output.wl_surface != null) { } else if (output.surfaces != null) {
output.renderWallpaper() catch |err| { output.renderWallpaper() catch |err| {
log.err("Wallpaper re-render failed: {}", .{err}); log.err("Wallpaper re-render failed: {}", .{err});
}; };

View file

@ -32,8 +32,11 @@ usable_height: u31 = 0,
wallpaper_render_scale: u31 = 0, wallpaper_render_scale: u31 = 0,
wallpaper_render_width: u31 = 0, wallpaper_render_width: u31 = 0,
wallpaper_render_height: u31 = 0, wallpaper_render_height: u31 = 0,
wl_surface: ?*wl.Surface = null,
layer_surface: ?*zwlr.LayerSurfaceV1 = null, surfaces: ?struct {
wl_surface: *wl.Surface,
layer_surface: *zwlr.LayerSurfaceV1,
} = null,
// TODO: Make Bar a user option, can disable if they want // TODO: Make Bar a user option, can disable if they want
// This Output's bar // This Output's bar
@ -319,8 +322,8 @@ pub fn initWallpaperLayerSurface(output: *Output) !void {
return; return;
} }
if (output.layer_surface) |_| { if (output.surfaces) |_| {
// This output already has a layer surface, we can exit early // This output already has a surface, we can exit early
return; return;
} }
@ -329,6 +332,9 @@ pub fn initWallpaperLayerSurface(output: *Output) !void {
const wl_surface = try context.wl_compositor.createSurface(); const wl_surface = try context.wl_compositor.createSurface();
errdefer wl_surface.destroy(); errdefer wl_surface.destroy();
const layer_surface = try context.zwlr_layer_shell_v1.getLayerSurface(wl_surface, output.wl_output, .background, "beansprout-wallpaper");
errdefer layer_surface.destroy();
// We don't want our surface to have any input region (default is infinite) // We don't want our surface to have any input region (default is infinite)
const empty_region = try context.wl_compositor.createRegion(); const empty_region = try context.wl_compositor.createRegion();
defer empty_region.destroy(); defer empty_region.destroy();
@ -340,12 +346,13 @@ pub fn initWallpaperLayerSurface(output: *Output) !void {
defer opaque_region.destroy(); defer opaque_region.destroy();
wl_surface.setOpaqueRegion(opaque_region); wl_surface.setOpaqueRegion(opaque_region);
const layer_surface = try context.zwlr_layer_shell_v1.getLayerSurface(wl_surface, output.wl_output, .background, "beansprout-wallpaper");
layer_surface.setExclusiveZone(-1); layer_surface.setExclusiveZone(-1);
layer_surface.setAnchor(.{ .top = true, .right = true, .bottom = true, .left = true }); layer_surface.setAnchor(.{ .top = true, .right = true, .bottom = true, .left = true });
output.wl_surface = wl_surface; output.surfaces = .{
output.layer_surface = layer_surface; .wl_surface = wl_surface,
.layer_surface = layer_surface,
};
context.buffer_pool.surface_count += 1; context.buffer_pool.surface_count += 1;
layer_surface.setListener(*Output, wallpaperLayerSurfaceListener, output); layer_surface.setListener(*Output, wallpaperLayerSurfaceListener, output);
@ -353,16 +360,13 @@ pub fn initWallpaperLayerSurface(output: *Output) !void {
} }
pub fn deinitWallpaperLayerSurface(output: *Output) void { pub fn deinitWallpaperLayerSurface(output: *Output) void {
if (output.layer_surface) |layer_surface| { if (output.surfaces) |surfaces| {
layer_surface.destroy(); surfaces.wl_surface.destroy();
} surfaces.layer_surface.destroy();
if (output.wl_surface) |wl_surface| {
wl_surface.destroy();
output.context.buffer_pool.surface_count -= 1; output.context.buffer_pool.surface_count -= 1;
} }
output.layer_surface = null; output.surfaces = null;
output.wl_surface = null;
output.configured = false; output.configured = false;
} }
@ -379,10 +383,10 @@ fn wallpaperLayerSurfaceListener(layer_surface: *zwlr.LayerSurfaceV1, event: zwl
output.wallpaper_render_height == height and output.wallpaper_render_height == height and
output.scale == output.wallpaper_render_scale) output.scale == output.wallpaper_render_scale)
{ {
if (output.wl_surface) |wl_surface| { if (output.surfaces) |surfaces| {
wl_surface.commit(); surfaces.wl_surface.commit();
} else { } else {
log.warn("Output is marked as configured but is missing a layer_surface for the wallpaper", .{}); log.warn("Output is marked as configured but is missing its surfaces.", .{});
} }
return; return;
} }
@ -485,7 +489,8 @@ pub fn renderWallpaper(output: *Output) !void {
log.info("render: {}x{} (scaled from {}x{})", .{ width * scale, height * scale, image_width, image_height }); log.info("render: {}x{} (scaled from {}x{})", .{ width * scale, height * scale, image_width, image_height });
// Attach the buffer to the surface // Attach the buffer to the surface
const wl_surface = output.wl_surface.?; const surfaces = output.surfaces orelse return error.NoSurfaces;
const wl_surface = surfaces.wl_surface;
wl_surface.setBufferScale(scale); wl_surface.setBufferScale(scale);
wl_surface.attach(buffer.wl_buffer, 0, 0); wl_surface.attach(buffer.wl_buffer, 0, 0);
wl_surface.damageBuffer(0, 0, width * scale, height * scale); wl_surface.damageBuffer(0, 0, width * scale, height * scale);