aboutsummaryrefslogtreecommitdiff
path: root/src/wl/registry.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-08-22 20:12:56 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2025-08-22 20:12:56 +0200
commit956ecb061b1862a4b632c8d5d6b6fa4318e1f640 (patch)
treef00adc9bfced275f280239b25a2b2ae15f57765b /src/wl/registry.zig
parent33e1de2710fe44512440e0e6055578d67dab330c (diff)
object refactor and add wl output
Now the Object is held by the registry instead of the struct its referencing and the struct only has a `handle` which is a usize.
Diffstat (limited to 'src/wl/registry.zig')
-rw-r--r--src/wl/registry.zig52
1 files changed, 35 insertions, 17 deletions
diff --git a/src/wl/registry.zig b/src/wl/registry.zig
index e4eb3a8..9d91952 100644
--- a/src/wl/registry.zig
+++ b/src/wl/registry.zig
@@ -12,18 +12,18 @@ pub const Events = wayland.EventSet(Self, .{
});
pub const Request = union(enum) {
- bind: struct { u32, u32 },
+ bind: struct { u32, []const u8, u32, u32 },
};
pub const GlobalHandler = struct {
interface: []const u8,
version: u32,
- handler: *const fn (ctx: *wayland.Context) ?wayland.Object,
+ handler: *const fn (ctx: *wayland.Context) ?wayland.Object.Ref,
};
-objects: std.ArrayListUnmanaged(?*wayland.Object) = .empty,
-interface: wayland.Object,
+objects: std.ArrayListUnmanaged(?wayland.Object) = .empty,
handlers: []const GlobalHandler,
+handle: wayland.Object.Ref,
pub fn init(
self: *Self,
@@ -32,40 +32,55 @@ pub fn init(
handlers: []const GlobalHandler,
) !void {
self.* = .{
- .interface = wayland.Object.from_self(self),
.handlers = handlers,
+ .handle = 2,
};
try self.objects.append(allocator, null);
- try self.objects.append(allocator, &display.interface);
- try self.objects.append(allocator, &self.interface);
+ try self.objects.append(allocator, wayland.Object.from_self(display));
+ try self.objects.append(allocator, wayland.Object.from_self(self));
- display.interface.id = 1;
- self.interface.id = 2;
+ display.handle = 1;
- try display.request(display, .{ .get_registry = .{&self.interface} });
+ try display.request(display, .{ .get_registry = .{ self.handle } });
+}
+
+pub fn deinit(self: *Self, allocator: std.mem.Allocator) void {
+ self.objects.deinit(allocator);
}
pub fn add_object(
self: *Self,
allocator: std.mem.Allocator,
- object: *wayland.Object,
-) !void {
+ object: wayland.Object,
+) !usize {
for (self.objects.items[1..], 1..) |obj, index| {
if (obj == null) {
- object.id = @truncate(index);
self.objects.items[index] = object;
- return;
+ return index;
}
}
- object.id = @truncate(self.objects.items.len);
try self.objects.append(allocator, object);
+ return self.objects.items.len - 1;
+}
+
+pub fn disable_object(self: *Self, id: usize) void {
+ if (self.objects.items[id]) |*object| {
+ object.disable();
+ }
}
pub fn release_object(self: *Self, id: u32) void {
self.objects.items[id] = null;
}
+pub fn get(self: *Self, handle: usize) ?*wayland.Object {
+ if (self.objects.items[handle]) |*objects| {
+ return objects;
+ }
+ return null;
+}
+
fn global(
self: *Self,
ctx: *wayland.Context,
@@ -73,12 +88,15 @@ fn global(
interface: []const u8,
version: u32,
) !void {
+ log.debug("global {s}", .{interface});
for (self.handlers) |handler| {
if (version == handler.version and std.mem.eql(u8, interface, handler.interface)) {
- if (handler.handler(ctx)) |object| {
+ if (handler.handler(ctx)) |object_ref| {
try ctx.display.request(self, .{ .bind = .{
name,
- object.id orelse return error.UnregisteredObject,
+ interface,
+ version,
+ @truncate(object_ref),
} });
}
}