aboutsummaryrefslogtreecommitdiff
path: root/src/wl
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-08-18 17:45:13 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2025-08-18 17:45:13 +0200
commit33e1de2710fe44512440e0e6055578d67dab330c (patch)
treedf8198ac5147f466bba600ca06dc1a319099d185 /src/wl
First sketch of wayland interface
**WARNING** this implementation is not working properly yet.
Diffstat (limited to 'src/wl')
-rw-r--r--src/wl/callback.zig33
-rw-r--r--src/wl/display.zig265
-rw-r--r--src/wl/output.zig171
-rw-r--r--src/wl/registry.zig96
-rw-r--r--src/wl/root.zig4
5 files changed, 569 insertions, 0 deletions
diff --git a/src/wl/callback.zig b/src/wl/callback.zig
new file mode 100644
index 0000000..0e7ea5b
--- /dev/null
+++ b/src/wl/callback.zig
@@ -0,0 +1,33 @@
+const std = @import("std");
+const wayland = @import("../root.zig");
+const wl = wayland.wl;
+
+const log = std.log.scoped(.callback);
+
+const Self = @This();
+
+pub const Events = wayland.EventSet(Self, .{done});
+
+is_done: *bool,
+interface: wayland.Object,
+
+pub fn init(
+ self: *Self,
+ allocator: std.mem.Allocator,
+ display: *wl.Display,
+ is_done: *bool,
+) !void {
+ self.is_done = is_done;
+ self.interface = wayland.Object.from_self(self);
+ try display.registry.add_object(allocator, &self.interface);
+ std.debug.print("ids {any}\n", .{display.registry.objects.items});
+ std.debug.print("callback #{?}\n", .{self.interface.id});
+ try display.request(display, .{ .sync = .{&self.interface} });
+
+}
+
+fn done(self: *Self, ctx: *wayland.Context, value: u32) void {
+ _ = value;
+ _ = ctx;
+ self.is_done.* = true;
+}
diff --git a/src/wl/display.zig b/src/wl/display.zig
new file mode 100644
index 0000000..4f04746
--- /dev/null
+++ b/src/wl/display.zig
@@ -0,0 +1,265 @@
+const std = @import("std");
+const wayland = @import("../root.zig");
+const wl = wayland.wl;
+
+const log = std.log.scoped(.display);
+
+const Self = @This();
+
+pub const Error = enum(u32) {
+ invalid_object = 0,
+ invalid_method = 1,
+ no_memory = 2,
+ implementation = 3,
+};
+
+pub const Request = union(enum) {
+ sync: struct { *wayland.Object },
+ get_registry: struct { *wayland.Object },
+};
+
+pub const Events = wayland.EventSet(Self, .{
+ err,
+ delete_id,
+});
+
+stream: std.net.Stream,
+interface: wayland.Object,
+registry: wl.Registry,
+
+pub fn init(
+ self: *Self,
+ allocator: std.mem.Allocator,
+ handlers: []const wl.Registry.GlobalHandler,
+) !void {
+ self.interface = wayland.Object.from_self(self);
+ try self.registry.init(allocator, self, handlers);
+}
+
+pub fn from_fd(fd: std.posix.fd_t) void {
+ var self: Self = undefined;
+ self.stream = std.net.Stream{ .handle = fd };
+ return self;
+}
+
+pub fn open(path: []const u8) !Self {
+ var self: Self = undefined;
+ self.stream = try std.net.connectUnixSocket(path);
+ return self;
+}
+
+pub fn close(self: *Self) void {
+ self.stream.close();
+}
+
+pub fn open_default(allocator: std.mem.Allocator) !Self {
+ const path = try default_path(allocator);
+ defer allocator.free(path);
+ return open(path);
+}
+
+pub fn default_path(allocator: std.mem.Allocator) ![]const u8 {
+ const env = try std.process.getEnvMap(allocator);
+ const xdg_runtime_dir = env.get("XDG_RUNTIME_DIR") orelse return error.MissingRuntimeDir;
+ const wayland_display = env.get("WAYLAND_DISPLAY") orelse "wayland-0";
+ return std.fmt.allocPrint(allocator, "{s}/{s}", .{ xdg_runtime_dir, wayland_display });
+}
+
+pub fn request(self: *Self, object: anytype, args: @TypeOf(object.*).Request) !void {
+ comptime std.debug.assert(@FieldType(@TypeOf(object.*), "interface") == wayland.Object);
+ comptime std.debug.assert(@typeInfo(@TypeOf(object.*).Request) == .@"union");
+ const tag_type = @typeInfo(@TypeOf(args)).@"union".tag_type.?;
+ const opcode: u32 = @intFromEnum(args);
+
+ inline for (@typeInfo(tag_type).@"enum".fields) |f| {
+ if (f.value == opcode) {
+ const data = @field(args, f.name);
+ const id = object.interface.id orelse return error.UnregisteredObject;
+
+ var size: u32 = 8;
+
+ inline for (data) |value| {
+ switch (@TypeOf(value)) {
+ u32, i32 => |t| size += @sizeOf(t),
+ *wayland.Object => size += @sizeOf(u32),
+ []const u8, []u8 => {
+ const padding_size: usize = @mod(@as(usize, @bitCast(-@as(isize, @intCast(value.len + 1)))), 4);
+ size += @as(u32, @intCast(@sizeOf(u32) + (value.len + 1) + padding_size));
+ },
+ else => |t| switch (@typeInfo(t)) {
+ .pointer => |ptr| if (ptr.size == .slice) {
+ const padding_size: usize = @mod(@as(usize, @bitCast(-@as(isize, @intCast(value.len * @sizeOf(ptr.child))))), 4);
+ size += @intCast(@sizeOf(u32) + (value.len * @sizeOf(ptr.child)) + padding_size);
+ } else @compileError(std.fmt.comptimePrint("size: unsupported type {}", .{t})),
+ else => @compileError(std.fmt.comptimePrint("size: unsupported type {}", .{t})),
+ },
+ }
+ }
+
+ _ = try self.stream.writeAll(&std.mem.toBytes(id));
+ _ = try self.stream.writeAll(&std.mem.toBytes((size << 16) | opcode));
+
+ inline for (data) |value| {
+ switch (@TypeOf(value)) {
+ u32, i32 => _ = try self.stream.writeAll(std.mem.asBytes(&value)),
+ *wayland.Object => try self.stream.writeAll(std.mem.asBytes(&(value.id orelse return error.UnregisteredObject))),
+ []const u8, []u8 => {
+ const padding_size: usize = @mod(@as(usize, @bitCast(-@as(isize, @intCast(value.len + 1)))), 4);
+ _ = try self.stream.writeAll(std.mem.asBytes(&@as(u32, @truncate(value.len + 1))));
+ _ = try self.stream.writeAll(value);
+ for (0..padding_size + 1) |_| {
+ _ = try self.stream.writeAll(std.mem.asBytes(&@as(u8, 0)));
+ }
+ },
+ else => |t| switch (@typeInfo(t)) {
+ .pointer => |ptr| if (ptr.size == .slice) {
+ const padding_size: usize = @mod(@as(usize, @bitCast(-@as(isize, @intCast(value.len * @sizeOf(ptr.child))))), 4);
+ _ = try self.stream.writeAll(std.mem.asBytes(&@as(u32, @truncate(value.len))));
+ _ = try self.stream.writeAll(std.mem.sliceAsBytes(value));
+ for (0..padding_size) |_| {
+ _ = try self.stream.writeAll(std.mem.asBytes(&@as(u8, 0)));
+ }
+ } else @compileError(std.fmt.comptimePrint("size: unsupported type {}", .{t})),
+ else => @compileError(std.fmt.comptimePrint("size: unsupported type {}", .{t})),
+ },
+ }
+ }
+ return;
+ }
+ }
+
+ return error.InvalidOpcode;
+}
+
+pub fn roundtrip(self: *Self, allocator: std.mem.Allocator) !void {
+ var done = false;
+ var callback: wl.Callback = undefined;
+ try callback.init(allocator, self, &done);
+
+ var context: wayland.Context = .{
+ .display = self,
+ .allocator = allocator,
+ };
+
+ while (!done) {
+ const endian = @import("builtin").target.cpu.arch.endian();
+ const reader = self.stream.reader();
+
+ const object_id = try reader.readInt(u32, endian);
+
+ const message_info = try reader.readInt(u32, endian);
+
+ const opcode: u16 = @truncate(message_info & 0xffff);
+ const size: u16 = @truncate(((message_info >> 16) & 0xffff));
+ const body_size = size - 2 * @sizeOf(u32);
+
+ const body = try allocator.alloc(u8, body_size);
+ defer allocator.free(body);
+ _ = try reader.readAll(body);
+
+ if (object_id >= self.registry.objects.items.len) {
+ return error.UnregisteredObject;
+ }
+
+ if (self.registry.objects.items[object_id]) |object| {
+ try object.on_event(&context, opcode, body);
+ } else {
+ return error.UnregisteredObject;
+ }
+ }
+}
+
+fn err(
+ self: *Self,
+ ctx: *wayland.Context,
+ object: *wayland.Object,
+ code: u32,
+ message: []const u8,
+) void {
+ _ = self;
+ _ = ctx;
+ log.err("{} ({}): {s}", .{ object, code, message });
+}
+
+fn delete_id(
+ self: *Self,
+ ctx: *wayland.Context,
+ id: u32,
+) void {
+ _ = ctx;
+ self.registry.release_object(id);
+}
+
+test "request" {
+ const Sample = struct {
+ pub const Request = union(enum) {
+ req1: struct { u32 },
+ req2: struct { *wayland.Object },
+ req3: struct { []const u8 },
+ req4: struct { []const u16 },
+ };
+
+ pub const Events = wayland.EventSet(@This(), .{});
+
+ interface: wayland.Object,
+
+ pub fn init(self: *@This()) void {
+ self.interface = wayland.Object.from_self(self);
+ }
+ };
+
+ const in, const out = try std.posix.pipe();
+ defer std.posix.close(in);
+
+ {
+ var sample: Sample = undefined;
+ sample.init();
+ sample.interface.id = 42;
+
+ var displ: Self = .from_fd(out);
+ defer displ.close();
+ try displ.init();
+ try displ.request(sample, .{ .req1 = .{432} });
+ try displ.request(sample, .{ .req2 = .{&sample.interface} });
+ try displ.request(sample, .{ .req3 = .{"abcd"} });
+ try displ.request(sample, .{ .req4 = .{&[_]u16{ 1, 32, 4, 5, 5 }} });
+ }
+
+ const file = std.fs.File{ .handle = in };
+ var test_buffer: [64]u8 = undefined;
+ _ = try file.readAll(test_buffer[0..12]);
+ try std.testing.expectEqualSlices(
+ u8,
+ &(std.mem.toBytes(@as(u32, 42)) ++
+ std.mem.toBytes(@as(u32, (12 << 16) | 0)) ++
+ std.mem.toBytes(@as(u32, 432))),
+ test_buffer[0..12],
+ );
+
+ _ = try file.readAll(test_buffer[0..12]);
+ try std.testing.expectEqualSlices(
+ u8,
+ &(std.mem.toBytes(@as(u32, 42)) ++
+ std.mem.toBytes(@as(u32, (12 << 16) | 1)) ++
+ std.mem.toBytes(@as(u32, 42))),
+ test_buffer[0..12],
+ );
+
+ _ = try file.readAll(test_buffer[0..20]);
+ try std.testing.expectEqualSlices(
+ u8,
+ &(std.mem.toBytes(@as(u32, 42)) ++
+ std.mem.toBytes(@as(u32, (20 << 16) | 2)) ++
+ std.mem.toBytes(@as(u32, 5))) ++ "abcd\x00\x00\x00\x00",
+ test_buffer[0..20],
+ );
+
+ _ = try file.readAll(test_buffer[0..24]);
+ try std.testing.expectEqualSlices(
+ u8,
+ &(std.mem.toBytes(@as(u32, 42)) ++
+ std.mem.toBytes(@as(u32, (24 << 16) | 3)) ++
+ std.mem.toBytes(@as(u32, 5))) ++ std.mem.toBytes([_]u16{ 1, 32, 4, 5, 5, 0 }),
+ test_buffer[0..24],
+ );
+}
diff --git a/src/wl/output.zig b/src/wl/output.zig
new file mode 100644
index 0000000..99e8ca1
--- /dev/null
+++ b/src/wl/output.zig
@@ -0,0 +1,171 @@
+const std = @import("std");
+const wayland = @import("../root.zig");
+const wl = wayland.wl;
+
+const Self = @This();
+
+pub const Requests = union(enum) {
+ release: struct {},
+};
+
+pub const Events = wayland.EventSet(Self, .{
+ geometry,
+ mode,
+ done,
+ scale,
+ name,
+ description,
+});
+
+pub const Subpixel = enum(u32) {
+ unkown = 0,
+ none = 1,
+ orizontal_rgb = 2,
+ orizontal_bgr = 3,
+ vertical_rgb = 4,
+ vertical_bgr = 5,
+};
+
+pub const Transform = enum(u32) {
+ normal = 0,
+ normal_90 = 1,
+ normal_180 = 2,
+ normal_270 = 3,
+ flipped = 4,
+ flipped_90 = 5,
+ flipped_180 = 6,
+ flipped_270 = 7,
+};
+
+pub const Geometry = struct {
+ x: u32,
+ y: u32,
+ physical_width: u32,
+ physical_height: u32,
+ subpixel: Subpixel,
+ make: []const u8,
+ model: []const u8,
+ transform: Transform,
+};
+
+pub const Mode = struct {
+ pub const Kind = enum(u32) {
+ current = 1,
+ preferred = 2,
+ };
+
+ kind: Kind,
+ width: u32,
+ height: u32,
+ refresh: u32,
+};
+
+pub const Info = struct {
+ geometry: Geometry,
+ mode: Mode,
+ scale: u32,
+ name: []const u8,
+ description: []const u8,
+};
+
+var outputs: std.ArrayListUnmanaged(*Self) = .empty;
+
+staging: Info = undefined,
+info: ?Info = null,
+interface: wayland.Object,
+
+pub fn init(
+ self: *Self,
+ allocator: std.mem.Allocator,
+ display: *wl.Display,
+) !void {
+ self.* = .{ .interface = wayland.Object.from_self(self) };
+ try display.registry.add_object(allocator, &self.interface);
+}
+
+pub fn deinit(self: *Self, display: *wl.Display) !void {
+ display.request(self, .{ .release = void{} });
+}
+
+pub const handler: wl.Registry.GlobalHandler = .{
+ .interface = "wl_output",
+ .version = 4,
+ .handler = register,
+};
+
+pub fn register(ctx: *wayland.Context) ?wayland.Object {
+ const output = ctx.allocator.create(Self) catch return null;
+ output.init(ctx.allocator, ctx.display) catch return null;
+ outputs.append(ctx.allocator, output) catch return null;
+ return output.interface;
+}
+
+fn geometry(
+ self: *Self,
+ ctx: *wayland.Context,
+ x: u32,
+ y: u32,
+ physical_width: u32,
+ physical_height: u32,
+ subpixel: Subpixel,
+ make: []const u8,
+ model: []const u8,
+ transform: Transform,
+) void {
+ _ = ctx;
+ self.staging.geometry = .{
+ .x = x,
+ .y = y,
+ .physical_width = physical_width,
+ .physical_height = physical_height,
+ .subpixel = subpixel,
+ .make = make,
+ .model = model,
+ .transform = transform,
+ };
+}
+
+fn mode(
+ self: *Self,
+ ctx: *wayland.Context,
+ flags: Mode.Kind,
+ width: u32,
+ height: u32,
+ refresh: u32,
+) void {
+ _ = ctx;
+ self.staging.mode = .{
+ .kind = flags,
+ .width = width,
+ .height = height,
+ .refresh = refresh,
+ };
+}
+
+fn done(self: *Self, ctx: *wayland.Context) void {
+ if (self.info) |info| {
+ ctx.allocator.free(info.name);
+ ctx.allocator.free(info.description);
+ }
+
+ self.info = self.staging;
+}
+
+fn scale(self: *Self, ctx: *wayland.Context, factor: u32) void {
+ _ = ctx;
+ self.staging.scale = factor;
+}
+
+fn name(self: *Self, ctx: *wayland.Context, n: []const u8) !void {
+ self.staging.name = try ctx.allocator.alloc(u8, n.len);
+ @memcpy(@constCast(self.staging.name), n);
+}
+
+fn description(self: *Self, ctx: *wayland.Context, d: []const u8) !void {
+ self.staging.description = try ctx.allocator.alloc(u8, d.len);
+ @memcpy(@constCast(self.staging.description), d);
+}
+
+pub fn get(index: usize) *Self {
+ return &outputs.items[index];
+}
diff --git a/src/wl/registry.zig b/src/wl/registry.zig
new file mode 100644
index 0000000..e4eb3a8
--- /dev/null
+++ b/src/wl/registry.zig
@@ -0,0 +1,96 @@
+const std = @import("std");
+const wayland = @import("../root.zig");
+const wl = wayland.wl;
+
+const log = std.log.scoped(.registry);
+
+const Self = @This();
+
+pub const Events = wayland.EventSet(Self, .{
+ global,
+ global_remove,
+});
+
+pub const Request = union(enum) {
+ bind: struct { u32, u32 },
+};
+
+pub const GlobalHandler = struct {
+ interface: []const u8,
+ version: u32,
+ handler: *const fn (ctx: *wayland.Context) ?wayland.Object,
+};
+
+objects: std.ArrayListUnmanaged(?*wayland.Object) = .empty,
+interface: wayland.Object,
+handlers: []const GlobalHandler,
+
+pub fn init(
+ self: *Self,
+ allocator: std.mem.Allocator,
+ display: *wl.Display,
+ handlers: []const GlobalHandler,
+) !void {
+ self.* = .{
+ .interface = wayland.Object.from_self(self),
+ .handlers = handlers,
+ };
+ try self.objects.append(allocator, null);
+ try self.objects.append(allocator, &display.interface);
+ try self.objects.append(allocator, &self.interface);
+
+ display.interface.id = 1;
+ self.interface.id = 2;
+
+ try display.request(display, .{ .get_registry = .{&self.interface} });
+}
+
+pub fn add_object(
+ self: *Self,
+ allocator: std.mem.Allocator,
+ object: *wayland.Object,
+) !void {
+ for (self.objects.items[1..], 1..) |obj, index| {
+ if (obj == null) {
+ object.id = @truncate(index);
+ self.objects.items[index] = object;
+ return;
+ }
+ }
+
+ object.id = @truncate(self.objects.items.len);
+ try self.objects.append(allocator, object);
+}
+
+pub fn release_object(self: *Self, id: u32) void {
+ self.objects.items[id] = null;
+}
+
+fn global(
+ self: *Self,
+ ctx: *wayland.Context,
+ name: u32,
+ interface: []const u8,
+ version: u32,
+) !void {
+ for (self.handlers) |handler| {
+ if (version == handler.version and std.mem.eql(u8, interface, handler.interface)) {
+ if (handler.handler(ctx)) |object| {
+ try ctx.display.request(self, .{ .bind = .{
+ name,
+ object.id orelse return error.UnregisteredObject,
+ } });
+ }
+ }
+ }
+}
+
+fn global_remove(
+ self: *Self,
+ ctx: *wayland.Context,
+ name: u32,
+) void {
+ _ = self;
+ _ = ctx;
+ log.debug("remove_global: {}", .{name});
+}
diff --git a/src/wl/root.zig b/src/wl/root.zig
new file mode 100644
index 0000000..b9e149f
--- /dev/null
+++ b/src/wl/root.zig
@@ -0,0 +1,4 @@
+pub const Display = @import("display.zig");
+pub const Registry = @import("registry.zig");
+pub const Callback = @import("callback.zig");
+pub const Output = @import("output.zig");