aboutsummaryrefslogtreecommitdiff
path: root/src/object
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/object
parent4feb8c7dab2b0a3492b8248ee12c3f0a475106f1 (diff)
Use mix-in design for interface abstraction.HEADmaster
Diffstat (limited to 'src/object')
-rw-r--r--src/object/interface.zig68
-rw-r--r--src/object/root.zig76
2 files changed, 144 insertions, 0 deletions
diff --git a/src/object/interface.zig b/src/object/interface.zig
new file mode 100644
index 0000000..3098c46
--- /dev/null
+++ b/src/object/interface.zig
@@ -0,0 +1,68 @@
+const std = @import("std");
+const wayland = @import("../root.zig");
+const wl = wayland.wl;
+
+pub fn Init(T: type) type {
+ return struct {
+ const Self = @This();
+
+ pub fn default(
+ self: *Self,
+ ctx: wayland.Context,
+ ) !void {
+ const parent: *T = @alignCast(@fieldParentPtr("init", self));
+
+ parent.* = .{
+ .handle = try ctx.display.registry.add_object(
+ ctx.allocator,
+ wayland.Object.from_self(parent),
+ ),
+ };
+ }
+
+ pub fn with(
+ self: *Self,
+ ctx: wayland.Context,
+ values: T,
+ ) !void {
+ const parent: *T = @alignCast(@fieldParentPtr("init", self));
+ parent.* = values;
+ parent.handle = try ctx.display.registry.add_object(
+ ctx.allocator,
+ wayland.Object.from_self(parent),
+ );
+ }
+ };
+}
+
+pub fn Global(T: type) type {
+ return struct {
+ const Self = @This();
+
+ pub const init = Self { };
+ var instances: std.ArrayListUnmanaged(*T) = .empty;
+
+ comptime register: fn (ctx: wayland.Context) ?wayland.Object.Ref = reg,
+
+ pub fn reg(ctx: wayland.Context) ?wayland.Object.Ref {
+ const interface = ctx.allocator.create(T) catch return null;
+ interface.init.default(ctx) catch return null;
+
+ instances.append(ctx.allocator, interface) catch return null;
+ return interface.handle;
+ }
+
+ pub fn as_slice(self: *Self) []*T {
+ _ = self;
+ return instances.items;
+ }
+
+ pub fn get(self: *Self, index: usize) ?*T {
+ _ = self;
+ if (index >= instances.items.len) {
+ return null;
+ }
+ return instances.items[index];
+ }
+ };
+}
diff --git a/src/object/root.zig b/src/object/root.zig
new file mode 100644
index 0000000..1895d3f
--- /dev/null
+++ b/src/object/root.zig
@@ -0,0 +1,76 @@
+const std = @import("std");
+const wayland = @import("../root.zig");
+
+const Self = @This();
+
+pub const interface = @import("interface.zig");
+
+pub const Ref = struct {
+ id: u32,
+
+ pub const @"null": @This() = .{
+ .id = 0,
+ };
+
+ pub const display: @This() = .{
+ .id = 1,
+ };
+
+ pub const registry: @This() = .{
+ .id = 2,
+ };
+};
+
+const VTable = struct {
+ on_event: *const fn (
+ ptr: *anyopaque,
+ ctx: wayland.Context,
+ opcode: u16,
+ args: []const u8,
+ ) void,
+};
+
+ptr: ?*anyopaque,
+vtable: VTable,
+name: []const u8,
+
+pub inline fn on_event(
+ self: *Self,
+ ctx: wayland.Context,
+ opcode: u16,
+ args: []const u8,
+) !void {
+ if (self.ptr) |ptr| {
+ self.vtable.on_event(ptr, ctx, opcode, args);
+ } else {
+ return error.UseAfterRelease;
+ }
+}
+
+pub inline fn from_self(ptr: anytype) Self {
+ const T = @TypeOf(ptr.*);
+
+ return .{
+ .ptr = ptr,
+ .vtable = VTable{
+ .on_event = T.Events.on_event,
+ },
+ .name = @typeName(T),
+ };
+}
+
+pub fn disable(self: *Self) void {
+ self.ptr = null;
+}
+
+pub fn format(
+ self: *const Self,
+ comptime fmt: []const u8,
+ options: std.fmt.FormatOptions,
+ writer: anytype,
+) !void {
+ _ = fmt;
+ _ = options;
+
+ try writer.print("Object<{s}> {{ {?} }}", .{self.name, self.ptr});
+}