Add support for reading arrays from messages

dev
LeRoyce Pearson 2023-08-10 18:51:32 -06:00
parent 8b50eeb7eb
commit 846f81b838
3 changed files with 22 additions and 11 deletions

View File

@ -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);

View File

@ -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)),
},

View File

@ -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,
},
};