From 10475dd0368631a9276e62d49a3074775b34a732 Mon Sep 17 00:00:00 2001 From: Louis Pearson Date: Mon, 15 Jan 2024 00:36:55 -0700 Subject: [PATCH] feat: add zxdg, decoration manager --- examples/01_client_connect.zig | 63 +++++++++++++++++++++++++++++++--- src/core.zig | 2 +- src/main.zig | 4 +++ src/zxdg.zig | 49 ++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 5 deletions(-) create mode 100644 src/zxdg.zig diff --git a/examples/01_client_connect.zig b/examples/01_client_connect.zig index da2e973..87fa780 100644 --- a/examples/01_client_connect.zig +++ b/examples/01_client_connect.zig @@ -25,6 +25,7 @@ pub fn main() !void { // create a sync callback so we know when the registry is done listing extensions const registry_done_id = id_pool.create(); + std.debug.print("registry done id: {}\n", .{registry_done_id}); { var buffer: [5]u32 = undefined; const message = try wayland.serialize(wayland.core.Display.Request, &buffer, 1, .{ .sync = .{ .callback = registry_done_id } }); @@ -34,6 +35,7 @@ pub fn main() !void { var shm_id_opt: ?u32 = null; var compositor_id_opt: ?u32 = null; var xdg_wm_base_id_opt: ?u32 = null; + var zxdg_decoration_manager_id_opt: ?u32 = null; var message_buffer = std.ArrayList(u32).init(gpa); defer message_buffer.deinit(); @@ -95,6 +97,20 @@ pub fn main() !void { } }, ); try socket.writeAll(std.mem.sliceAsBytes(message)); + } else if (std.mem.eql(u8, global.interface, "zxdg_decoration_manager_v1")) { + zxdg_decoration_manager_id_opt = id_pool.create(); + const message = try wayland.serialize( + wayland.core.Registry.Request, + &buffer, + registry_id, + .{ .bind = .{ + .name = global.name, + .interface = global.interface, + .version = 1, + .new_id = zxdg_decoration_manager_id_opt.?, + } }, + ); + try socket.writeAll(std.mem.sliceAsBytes(message)); } }, .global_remove => {}, @@ -153,6 +169,24 @@ pub fn main() !void { try socket.writeAll(std.mem.sliceAsBytes(message)); } + var zxdg_toplevel_decoration_id_opt: ?u32 = null; + if (zxdg_decoration_manager_id_opt) |zxdg_decoration_manager_id| { + zxdg_toplevel_decoration_id_opt = id_pool.create(); + { + var buffer: [10]u32 = undefined; + const message = try wayland.serialize( + wayland.zxdg.DecorationManagerV1.Request, + &buffer, + zxdg_decoration_manager_id, + .{ .get_toplevel_decoration = .{ + .new_id = zxdg_toplevel_decoration_id_opt.?, + .toplevel = xdg_toplevel_id, + } }, + ); + try socket.writeAll(std.mem.sliceAsBytes(message)); + } + } + { var buffer: [10]u32 = undefined; const message = try wayland.serialize( @@ -200,6 +234,23 @@ pub fn main() !void { surface_configured = true; }, } + } else if (zxdg_toplevel_decoration_id_opt != null and header.object_id == zxdg_toplevel_decoration_id_opt.?) { + const event = try wayland.deserialize(wayland.zxdg.ToplevelDecorationV1.Event, header, message_buffer.items); + std.debug.print("<- zxdg_toplevel_decoration@{}\n", .{event}); + // switch (event) { + // .configure => |_| { + // var buffer: [10]u32 = undefined; + // const message = try wayland.serialize( + // wayland.zxdg.ToplevelDecorationV1.Request, + // &buffer, + // zxdg_toplevel_decoration_id_opt.?, + // .{ .set_mode = .{ + // .mode = .server_side, + // } }, + // ); + // try socket.writeAll(std.mem.sliceAsBytes(message)); + // }, + // } } 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}); @@ -214,7 +265,10 @@ pub fn main() !void { const event = try wayland.deserialize(wayland.core.Display.Event, header, message_buffer.items); switch (event) { .@"error" => |err| std.debug.print("<- error({}): {} {s}\n", .{ err.object_id, err.code, err.message }), - .delete_id => |id| id_pool.destroy(id.name), + .delete_id => |id| { + std.debug.print("id {} deleted\n", .{id}); + id_pool.destroy(id.id); + }, } } else { std.debug.print("{} {x} \"{}\"\n", .{ header.object_id, header.size_and_opcode.opcode, std.zig.fmtEscapes(std.mem.sliceAsBytes(message_buffer.items)) }); @@ -244,7 +298,7 @@ pub fn main() !void { } } - const wl_shm_pool_id = 10; + const wl_shm_pool_id = id_pool.create(); { var buffer: [10]u32 = undefined; const message = try wayland.serialize( @@ -283,8 +337,9 @@ pub fn main() !void { _ = try std.os.sendmsg(socket.handle, &socket_message, 0); } - const wl_buffer_id = 11; + const wl_buffer_id = id_pool.create(); { + std.debug.print("buffer id: {}\n", .{wl_buffer_id}); var buffer: [10]u32 = undefined; const message = try wayland.serialize( wayland.core.ShmPool.Request, @@ -414,7 +469,7 @@ pub fn main() !void { const event = try wayland.deserialize(wayland.core.Display.Event, header, message_buffer.items); switch (event) { .@"error" => |err| std.debug.print("<- error({}): {} {s}\n", .{ err.object_id, err.code, err.message }), - .delete_id => |id| id_pool.destroy(id.name), + .delete_id => |id| id_pool.destroy(id.id), } } else { std.debug.print("{} {x} \"{}\"\n", .{ header.object_id, header.size_and_opcode.opcode, std.zig.fmtEscapes(std.mem.sliceAsBytes(message_buffer.items)) }); diff --git a/src/core.zig b/src/core.zig index d67bad4..946bc32 100644 --- a/src/core.zig +++ b/src/core.zig @@ -17,7 +17,7 @@ pub const Display = struct { message: []const u8, }, delete_id: struct { - name: u32, + id: u32, }, }; diff --git a/src/main.zig b/src/main.zig index b6e6189..73fed2c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,6 +2,7 @@ const std = @import("std"); const testing = std.testing; pub const core = @import("./core.zig"); pub const xdg = @import("./xdg.zig"); +pub const zxdg = @import("./zxdg.zig"); pub fn getDisplayPath(gpa: std.mem.Allocator) ![]u8 { const xdg_runtime_dir_path = try std.process.getEnvVarOwned(gpa, "XDG_RUNTIME_DIR"); @@ -340,6 +341,9 @@ pub const IdPool = struct { } pub fn destroy(this: *@This(), id: u32) void { + for (this.free_ids.slice()) |existing_id| { + if (existing_id == id) return; + } this.free_ids.append(id) catch {}; } }; diff --git a/src/zxdg.zig b/src/zxdg.zig new file mode 100644 index 0000000..769dce5 --- /dev/null +++ b/src/zxdg.zig @@ -0,0 +1,49 @@ +pub const DecorationManagerV1 = struct { + pub const Request = union(Request.Tag) { + destroy: void, + get_toplevel_decoration: struct { + new_id: u32, + toplevel: u32, + }, + + pub const Tag = enum(u16) { + destroy, + get_toplevel_decoration, + }; + }; +}; + +pub const ToplevelDecorationV1 = struct { + pub const Request = union(Request.Tag) { + destroy: void, + unset_mode: void, + set_mode: struct { + mode: Mode, + }, + + pub const Tag = enum(u16) { + destroy, + unset_mode, + set_mode, + }; + }; + + pub const Event = union(Event.Tag) { + configure: struct { mode: Mode }, + + pub const Tag = enum(u16) { + configure, + }; + }; + + pub const Error = enum(u32) { + unconfigured_buffer, + already_constructed, + orphaned, + }; + + pub const Mode = enum(u32) { + client_side = 1, + server_side = 2, + }; +};