From ed0ec55697b11fa54ff661bc6c250398a096f85a Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 27 Aug 2025 20:34:57 +0200 Subject: update Object.Ref to be a struct --- src/event-set.zig | 2 +- src/object.zig | 12 +++++++++++- src/wl/display.zig | 10 +++++----- src/wl/registry.zig | 24 ++++++++++++------------ 4 files changed, 29 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/event-set.zig b/src/event-set.zig index da17734..0ff16f1 100644 --- a/src/event-set.zig +++ b/src/event-set.zig @@ -30,7 +30,7 @@ pub fn EventSet(T: type, events: anytype) type { }, *wayland.Object => { const result = std.mem.readPackedIntNative(u32, args, offset * 8); - fn_args[arg_index] = ctx.display.registry.get(result) orelse unreachable; + fn_args[arg_index] = ctx.display.registry.get_by_id(result) orelse unreachable; offset += @sizeOf(u32); }, else => |t| switch (@typeInfo(t)) { diff --git a/src/object.zig b/src/object.zig index 5502d3d..5b7726e 100644 --- a/src/object.zig +++ b/src/object.zig @@ -3,7 +3,17 @@ const wayland = @import("root.zig"); const Self = @This(); -pub const Ref = usize; +pub const Ref = struct { + id: u32, + + pub const display: @This() = .{ + .id = 1, + }; + + pub const registry: @This() = .{ + .id = 2, + }; +}; const VTable = struct { on_event: *const fn ( diff --git a/src/wl/display.zig b/src/wl/display.zig index 2a7b94c..d8b9e93 100644 --- a/src/wl/display.zig +++ b/src/wl/display.zig @@ -83,7 +83,7 @@ pub fn request(self: *Self, object: anytype, args: @TypeOf(object.*).Request) !v inline for (@typeInfo(tag_type).@"enum".fields) |f| { if (f.value == opcode) { const data = if (@FieldType(@TypeOf(args), f.name) == void) .{} else @field(args, f.name); - const id: u32 = @truncate(object.handle); + const id: u32 = object.handle.id; var size: u32 = 8; comptime var count_fds = 0; @@ -131,8 +131,8 @@ pub fn request(self: *Self, object: anytype, args: @TypeOf(object.*).Request) !v inline for (data) |value| { switch (@TypeOf(value)) { u32, i32 => _ = try self.stream.writeAll(std.mem.asBytes(&value)), - wayland.Object.Ref => try self.stream.writeAll(std.mem.asBytes(&(@as(u32, @truncate(value))))), - ?wayland.Object.Ref => try self.stream.writeAll(std.mem.asBytes(&(@as(u32, @truncate(if (value) |v| v else 0))))), + wayland.Object.Ref => try self.stream.writeAll(std.mem.asBytes(&value.id)), + ?wayland.Object.Ref => try self.stream.writeAll(std.mem.asBytes(&(if (value) |v| v else 0))), []const u8, []u8 => { const padding_size: usize = padding_len(u8, value.len + 1); _ = try self.stream.writeAll(std.mem.asBytes(&@as(u32, @truncate(value.len + 1)))); @@ -184,7 +184,7 @@ fn read_event(self: *Self, ctx: *const wayland.Context) !void { return error.UnregisteredObject; } - if (self.registry.get(object_id)) |object| { + if (self.registry.get_by_id(object_id)) |object| { try object.on_event(ctx, opcode, body); } else { return error.UnregisteredObject; @@ -230,7 +230,7 @@ fn delete_id( id: u32, ) void { _ = ctx; - self.registry.release_object(id); + self.registry.release_object(.{ .id = id }); } test "request" { diff --git a/src/wl/registry.zig b/src/wl/registry.zig index 0939d17..2c15c67 100644 --- a/src/wl/registry.zig +++ b/src/wl/registry.zig @@ -12,7 +12,7 @@ pub const Events = wayland.EventSet(Self, .{ }); pub const Request = union(enum) { - bind: struct { u32, []const u8, u32, u32 }, + bind: struct { u32, []const u8, u32, wayland.Object.Ref }, }; pub const GlobalHandler = struct { @@ -33,13 +33,13 @@ pub fn init( ) !void { self.* = .{ .handlers = handlers, - .handle = 2, + .handle = .registry, }; try self.objects.append(allocator, null); try self.objects.append(allocator, wayland.Object.from_self(display)); try self.objects.append(allocator, wayland.Object.from_self(self)); - display.handle = 1; + display.handle = .display; try display.request(display, .{ .get_registry = .{ self.handle } }); } @@ -56,26 +56,26 @@ pub fn add_object( for (self.objects.items[1..], 1..) |obj, index| { if (obj == null) { self.objects.items[index] = object; - return index; + return .{ .id = @truncate(index) }; } } try self.objects.append(allocator, object); - return self.objects.items.len - 1; + return .{ .id = @truncate(self.objects.items.len - 1) }; } -pub fn disable_object(self: *Self, id: usize) void { - if (self.objects.items[id]) |*object| { +pub fn disable_object(self: *Self, handle: wayland.Object.Ref) void { + if (self.objects.items[handle.id]) |*object| { object.disable(); } } -pub fn release_object(self: *Self, id: u32) void { - self.objects.items[id] = null; +pub fn release_object(self: *Self, handle: wayland.Object.Ref) void { + self.objects.items[handle.id] = null; } -pub fn get(self: *Self, handle: usize) ?*wayland.Object { - if (self.objects.items[handle]) |*objects| { +pub fn get_by_id(self: *Self, id: u32) ?*wayland.Object { + if (self.objects.items[id]) |*objects| { return objects; } return null; @@ -96,7 +96,7 @@ fn global( name, interface, version, - @truncate(object_ref), + object_ref, } }); } } -- cgit v1.2.3-70-g09d2