diff options
Diffstat (limited to 'src/object/interface.zig')
| -rw-r--r-- | src/object/interface.zig | 68 |
1 files changed, 68 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]; + } + }; +} |