aboutsummaryrefslogtreecommitdiff
path: root/src/wl
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-08-30 15:49:18 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2025-08-30 15:49:18 +0200
commita46436e58beaaa322c082af5e886f96fd31d7a08 (patch)
tree05a05b149c2f18cb0562aef94fe41bd5aaad9fbc /src/wl
parent4feb8c7dab2b0a3492b8248ee12c3f0a475106f1 (diff)
Use mix-in design for interface abstraction.HEADmaster
Diffstat (limited to 'src/wl')
-rw-r--r--src/wl/buffer.zig28
-rw-r--r--src/wl/callback.zig17
-rw-r--r--src/wl/compositor.zig28
-rw-r--r--src/wl/display.zig3
-rw-r--r--src/wl/output/root.zig30
-rw-r--r--src/wl/shm/pixel/argb8888.zig6
-rw-r--r--src/wl/shm/pixel/root.zig0
-rw-r--r--src/wl/shm/pool.zig39
-rw-r--r--src/wl/shm/root.zig44
-rw-r--r--src/wl/surface.zig14
10 files changed, 56 insertions, 153 deletions
diff --git a/src/wl/buffer.zig b/src/wl/buffer.zig
index fe69899..ae52196 100644
--- a/src/wl/buffer.zig
+++ b/src/wl/buffer.zig
@@ -12,29 +12,13 @@ pub fn Buffer(fmt: wl.Shm.Format) type {
pub const Events = wayland.EventSet(Self, .{});
- handle: wayland.Object.Ref,
+ handle: wayland.Object.Ref = .null,
+ init: wayland.Object.interface.Init(Self) = .{},
+
data: []wl.Shm.Format.Pixel(fmt),
width: usize,
height: usize,
- pub fn init(
- self: *Self,
- ctx: wayland.Context,
- width: usize,
- height: usize,
- data: []u8,
- ) !void {
- self.* = .{
- .handle = try ctx.display.registry.add_object(
- ctx.allocator,
- wayland.Object.from_self(self),
- ),
- .data = @alignCast(@ptrCast(data)),
- .width = width,
- .height = height,
- };
- }
-
pub fn format(
self: *const Self,
comptime f: []const u8,
@@ -44,7 +28,11 @@ pub fn Buffer(fmt: wl.Shm.Format) type {
_ = f;
_ = options;
- try writer.print("wl.buffer<{s}> {{ {}x{} }}", .{@tagName(fmt), self.width, self.height});
+ try writer.print("wl.buffer<{s}> {{ {}x{} }}", .{
+ @tagName(fmt),
+ self.width,
+ self.height
+ });
}
};
}
diff --git a/src/wl/callback.zig b/src/wl/callback.zig
index ad2d192..dc9028d 100644
--- a/src/wl/callback.zig
+++ b/src/wl/callback.zig
@@ -9,21 +9,8 @@ const Self = @This();
pub const Events = wayland.EventSet(Self, .{done});
is_done: *bool,
-handle: wayland.Object.Ref,
-
-pub fn init(
- self: *Self,
- ctx: wayland.Context,
- is_done: *bool,
-) !void {
- self.is_done = is_done;
- self.handle = try ctx.display.registry.add_object(
- ctx.allocator,
- wayland.Object.from_self(self),
- );
- try ctx.display.request(ctx.display, .{ .sync = .{self.handle} });
-
-}
+handle: wayland.Object.Ref = .null,
+init: wayland.Object.interface.Init(Self) = .{},
pub fn deinit(self: *Self, ctx: wayland.Context) void {
ctx.display.registry.disable_object(self.handle);
diff --git a/src/wl/compositor.zig b/src/wl/compositor.zig
index 166430a..ed8ac6b 100644
--- a/src/wl/compositor.zig
+++ b/src/wl/compositor.zig
@@ -11,35 +11,15 @@ pub const Request = union(enum) {
};
handle: wayland.Object.Ref,
+init: wayland.Object.interface.Init(Self) = .{},
-pub var instances: std.ArrayListUnmanaged(*Self) = .empty;
-
+pub var globals: wayland.Object.interface.Global(Self) = .init;
pub const handler: wl.Registry.GlobalHandler = .{
.interface = "wl_compositor",
.version = 6,
- .handler = register,
+ .handler = globals.register,
};
-pub fn init(
- self: *Self,
- ctx: wayland.Context
-) !void {
- self.* = .{
- .handle = try ctx.display.registry.add_object(
- ctx.allocator,
- wayland.Object.from_self(self),
- ),
- };
-}
-
-fn register(ctx: wayland.Context) ?wayland.Object.Ref {
- const compositor = ctx.allocator.create(Self) catch return null;
- compositor.init(ctx) catch return null;
-
- instances.append(ctx.allocator, compositor) catch return null;
- return compositor.handle;
-}
-
pub fn format(
self: *const Self,
comptime fmt: []const u8,
@@ -60,7 +40,7 @@ pub fn create_surface(
const surface = try ctx.allocator.create(wl.Surface);
errdefer ctx.allocator.destroy(surface);
- try surface.init(ctx);
+ try surface.init.default(ctx);
try ctx.display.request(self, .{ .create_surface = .{ surface.handle } });
diff --git a/src/wl/display.zig b/src/wl/display.zig
index fed8bbd..5b31646 100644
--- a/src/wl/display.zig
+++ b/src/wl/display.zig
@@ -200,7 +200,8 @@ pub fn roundtrip(self: *Self, allocator: std.mem.Allocator) !void {
var done = false;
var callback: wl.Callback = undefined;
- try callback.init(context, &done);
+ try callback.init.with(context, .{ .is_done = &done });
+ try self.request(self, .{ .sync = .{ callback.handle } });
defer callback.deinit(context);
errdefer self.read_event(context) catch |e| {
diff --git a/src/wl/output/root.zig b/src/wl/output/root.zig
index 61c1798..9cffd43 100644
--- a/src/wl/output/root.zig
+++ b/src/wl/output/root.zig
@@ -30,40 +30,22 @@ pub const Info = struct {
description: []const u8,
};
-pub var instances: std.ArrayListUnmanaged(*Self) = .empty;
staging: Info = undefined,
info: ?Info = null,
-handle: wayland.Object.Ref,
-
-pub fn init(
- self: *Self,
- ctx: wayland.Context,
-) !void {
- self.* = .{
- .handle = try ctx.display.registry.add_object(
- ctx.allocator,
- wayland.Object.from_self(self),
- ),
- };
-}
-pub fn deinit(self: *Self, display: *wl.Display) !void {
- display.request(self, .{ .release = void{} });
-}
+handle: wayland.Object.Ref,
+init: wayland.Object.interface.Init(Self) = .{},
+pub var globals: wayland.Object.interface.Global(Self) = .init;
pub const handler: wl.Registry.GlobalHandler = .{
.interface = "wl_output",
.version = 4,
- .handler = register,
+ .handler = globals.register,
};
-fn register(ctx: wayland.Context) ?wayland.Object.Ref {
- const output = ctx.allocator.create(Self) catch return null;
- output.init(ctx) catch return null;
-
- instances.append(ctx.allocator, output) catch return null;
- return output.handle;
+pub fn deinit(self: *Self, display: *wl.Display) !void {
+ display.request(self, .{ .release = void{} });
}
fn geometry(
diff --git a/src/wl/shm/pixel/argb8888.zig b/src/wl/shm/pixel/argb8888.zig
new file mode 100644
index 0000000..5e5a68e
--- /dev/null
+++ b/src/wl/shm/pixel/argb8888.zig
@@ -0,0 +1,6 @@
+pub const Pixel = packed struct(u32) {
+ alpha: u8,
+ red: u8,
+ green: u8,
+ blue: u8,
+}
diff --git a/src/wl/shm/pixel/root.zig b/src/wl/shm/pixel/root.zig
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/wl/shm/pixel/root.zig
diff --git a/src/wl/shm/pool.zig b/src/wl/shm/pool.zig
index 62b6c18..16ba936 100644
--- a/src/wl/shm/pool.zig
+++ b/src/wl/shm/pool.zig
@@ -15,29 +15,11 @@ pub const Request = union(enum) {
resize: struct { u32 },
};
-handle: wayland.Object.Ref,
+handle: wayland.Object.Ref = .null,
+init: wayland.Object.interface.Init(Self) = .{},
file: AnonymousFile,
buffer: []u8,
-pub fn init(
- self: *Self,
- ctx: wayland.Context,
- size: usize,
-) !void {
- var file = try AnonymousFile.init_random();
- errdefer file.close();
- try file.truncate(size);
-
- self.* = .{
- .handle = try ctx.display.registry.add_object(
- ctx.allocator,
- wayland.Object.from_self(self),
- ),
- .file = file,
- .buffer = try file.mmap(),
- };
-}
-
pub fn deinit(self: *Self, ctx: wayland.Context) void {
self.file.close();
std.posix.munmap(@alignCast(self.buffer));
@@ -51,9 +33,9 @@ pub fn deinit(self: *Self, ctx: wayland.Context) void {
pub fn create_buffer(
self: *Self,
ctx: wayland.Context,
- offset: usize,
- width: usize,
- height: usize,
+ offset: u32,
+ width: u32,
+ height: u32,
comptime format: Format,
) !*wl.Buffer(format) {
const stride = width * format.bytes_per_pixel();
@@ -61,12 +43,11 @@ pub fn create_buffer(
const buffer = try ctx.allocator.create(wl.Buffer(format));
errdefer ctx.allocator.destroy(buffer);
- try buffer.init(
- ctx,
- width,
- height,
- data,
- );
+ try buffer.init.with(ctx, .{
+ .width = width,
+ .height = height,
+ .data = @alignCast(@ptrCast(data)),
+ });
try ctx.display.request(
self,
diff --git a/src/wl/shm/root.zig b/src/wl/shm/root.zig
index 741c7df..46388ad 100644
--- a/src/wl/shm/root.zig
+++ b/src/wl/shm/root.zig
@@ -13,46 +13,34 @@ pub const Request = union(enum) {
create_pool: struct { wayland.Object.Ref, wayland.types.Fd, u32 },
};
-pub var instances: std.ArrayListUnmanaged(*Self) = .empty;
-
-handle: wayland.Object.Ref,
+handle: wayland.Object.Ref = .null,
+init: wayland.Object.interface.Init(Self) = .{},
formats: std.EnumSet(Format) = .initEmpty(),
-pub fn init(
- self: *Self,
- ctx: wayland.Context,
-) !void {
- self.* = .{
- .handle = try ctx.display.registry.add_object(
- ctx.allocator,
- wayland.Object.from_self(self),
- ),
- };
-}
-
-fn announce_format(self: * Self, ctx: wayland.Context, fmt: Format) void {
- _ = ctx;
- self.formats.insert(fmt);
-}
-
+pub var globals: wayland.Object.interface.Global(Self) = .init;
pub const handler: wl.Registry.GlobalHandler = .{
.interface = "wl_shm",
.version = 1,
- .handler = register,
+ .handler = globals.register,
};
-fn register(ctx: wayland.Context) ?wayland.Object.Ref {
- const shm = ctx.allocator.create(Self) catch return null;
- shm.init(ctx) catch return null;
-
- instances.append(ctx.allocator, shm) catch return null;
- return shm.handle;
+fn announce_format(self: * Self, ctx: wayland.Context, fmt: Format) void {
+ _ = ctx;
+ self.formats.insert(fmt);
}
pub fn create_pool(self: *Self, ctx: wayland.Context, size: usize) !*Pool {
const pool = try ctx.allocator.create(Pool);
errdefer ctx.allocator.destroy(pool);
- try pool.init(ctx, size);
+
+ var file = try AnonymousFile.init_random();
+ errdefer file.close();
+ try file.truncate(size);
+
+ try pool.init.with(ctx, .{
+ .file = file,
+ .buffer = try file.mmap(),
+ });
try ctx.display.request(self, .{ .create_pool = .{
pool.handle,
diff --git a/src/wl/surface.zig b/src/wl/surface.zig
index 07cd80b..4405c96 100644
--- a/src/wl/surface.zig
+++ b/src/wl/surface.zig
@@ -26,21 +26,11 @@ pub const Events = wayland.EventSet(Self, .{
});
handle: wayland.Object.Ref,
+init: wayland.Object.interface.Init(Self) = .{},
+
preferred_buffer_scale: u32 = 1,
preferred_buffer_transform: wl.Output.Transform = .normal,
-pub fn init(
- self: *Self,
- ctx: wayland.Context,
-) !void {
- self.* = .{
- .handle = try ctx.display.registry.add_object(
- ctx.allocator,
- wayland.Object.from_self(self),
- ),
- };
-}
-
pub fn deinit(
self: *Self,
ctx: wayland.Context,