From 846f81b838da782c0340c5e4babda73e22337aac Mon Sep 17 00:00:00 2001 From: LeRoyce Pearson Date: Thu, 10 Aug 2023 18:51:32 -0600 Subject: [PATCH] Add support for reading arrays from messages --- examples/01_client_connect.zig | 13 +++++-------- src/main.zig | 16 +++++++++++++++- src/xdg.zig | 4 ++-- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/examples/01_client_connect.zig b/examples/01_client_connect.zig index 8e51778..d4a1167 100644 --- a/examples/01_client_connect.zig +++ b/examples/01_client_connect.zig @@ -370,16 +370,13 @@ pub fn main() !void { }, } } else if (header.object_id == xdg_toplevel_id) { - // const event = try wayland.deserialize(wayland.xdg.Toplevel.Event, header, message_buffer.items); - // std.debug.print("<- {}\n", .{event}); - switch (@as(std.meta.Tag(wayland.xdg.Toplevel.Event), @enumFromInt(header.size_and_opcode.opcode))) { - .configure => { - const width: i32 = @intCast(message_buffer.items[0]); - const height: i32 = @intCast(message_buffer.items[1]); - std.debug.print("<- xdg_toplevel@{} configure <{}, {}>\n", .{ header.object_id, width, height }); + const event = try wayland.deserialize(wayland.xdg.Toplevel.Event, header, message_buffer.items); + switch (event) { + .configure => |conf| { + std.debug.print("<- xdg_toplevel@{} configure <{}, {}> {any}\n", .{ header.object_id, conf.width, conf.height, conf.states }); }, .close => running = false, - else => |tag| std.debug.print("<- xdg_toplevel@{} {s} {}\n", .{ header.object_id, @tagName(tag), std.zig.fmtEscapes(std.mem.sliceAsBytes(message_buffer.items)) }), + else => |tag| std.debug.print("<- xdg_toplevel@{} {s} {}\n", .{ header.object_id, @tagName(tag), event }), } } else if (header.object_id == wl_buffer_id) { const event = try wayland.deserialize(wayland.core.Buffer.Event, header, message_buffer.items); diff --git a/src/main.zig b/src/main.zig index f73b544..2972cd9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -91,6 +91,18 @@ pub fn readString(buffer: []const u32, parent_pos: *usize) ![:0]const u8 { return string; } +pub fn readArray(comptime T: type, buffer: []const u32, parent_pos: *usize) ![]const T { + var pos = parent_pos.*; + + const byte_size = try readUInt(buffer, &pos); + + const array = @as([*]const T, @ptrCast(buffer[pos..].ptr))[0 .. byte_size / @sizeOf(T)]; + pos += byte_size / @sizeOf(u32); + + parent_pos.* = pos; + return array; +} + pub fn deserializeArguments(comptime Signature: type, buffer: []const u32) !Signature { if (Signature == void) return {}; var result: Signature = undefined; @@ -102,8 +114,10 @@ pub fn deserializeArguments(comptime Signature: type, buffer: []const u32) !Sign .unsigned => @field(result, field.name) = try readUInt(buffer, &pos), }, .Pointer => |ptr| switch (ptr.size) { - .Slice => { + .Slice => if (ptr.child == u8) { @field(result, field.name) = try readString(buffer, &pos); + } else { + @field(result, field.name) = try readArray(ptr.child, buffer, &pos); }, else => @compileError("Unsupported type " ++ @typeName(field.type)), }, diff --git a/src/xdg.zig b/src/xdg.zig index 2de6f93..0e40c4f 100644 --- a/src/xdg.zig +++ b/src/xdg.zig @@ -110,7 +110,7 @@ pub const Toplevel = struct { configure: struct { width: i32, height: i32, - states: []Toplevel.State, + states: []const Toplevel.State, }, close: void, configure_bounds: struct { @@ -118,7 +118,7 @@ pub const Toplevel = struct { height: i32, }, wm_capabilities: struct { - capabilities: []Toplevel.WmCapabilities, + capabilities: []const Toplevel.WmCapabilities, }, };