aboutsummaryrefslogtreecommitdiff
path: root/src/wl/shm/anonymous-file.zig
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2025-08-27 09:02:43 +0200
committerNathan Reiner <nathan@nathanreiner.xyz>2025-08-27 09:03:21 +0200
commit8a7392dea729d3ed49a8bf8eee25906c4fd616ac (patch)
tree09e0b2fa96c60a2e0c8b0cae9e631bd2dc66f52f /src/wl/shm/anonymous-file.zig
parentc217e7ec5cddfc002c4582fb5d52727aee843a7d (diff)
Add ancillary data mechanism to send fds to compositor.
Currently we are just attaching the fds to the object id. In theory this is not a valid implementation, since if we have more than MAX_FD file descriptors this will not work. But since this wont be the case in basically all cases of the wayland protocol, we can just ignore that for now.
Diffstat (limited to 'src/wl/shm/anonymous-file.zig')
-rw-r--r--src/wl/shm/anonymous-file.zig53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/wl/shm/anonymous-file.zig b/src/wl/shm/anonymous-file.zig
new file mode 100644
index 0000000..f73fbc9
--- /dev/null
+++ b/src/wl/shm/anonymous-file.zig
@@ -0,0 +1,53 @@
+const std = @import("std");
+
+const Self = @This();
+
+size: usize = 0,
+name: [24]u8 = undefined,
+fd: std.posix.fd_t = undefined,
+
+pub fn init_random() !Self {
+ var buf: [8]u8 = undefined;
+ try std.posix.getrandom(&buf);
+
+ var self: Self = .{};
+
+ const name = std.fmt.bufPrint(&self.name, "/wl.shm.{x}{x}{x}{x}{x}{x}{x}{x}", .{
+ buf[0],
+ buf[1],
+ buf[2],
+ buf[3],
+ buf[4],
+ buf[5],
+ buf[6],
+ buf[7],
+ }) catch unreachable;
+
+ self.fd = try std.posix.memfd_create(name, 0);
+
+ return self;
+}
+
+pub fn truncate(self: *Self, size: usize) !void {
+ try std.posix.ftruncate(self.fd, size);
+ self.size = size;
+}
+
+pub fn mmap(self: *Self) ![]u8 {
+ return std.posix.mmap(
+ null,
+ self.size,
+ std.os.linux.PROT.READ | std.os.linux.PROT.WRITE,
+ .{
+ .TYPE = .SHARED,
+ .ANONYMOUS = true,
+ },
+ self.fd,
+ 0,
+ );
+}
+
+pub fn close(self: *Self) void {
+ std.posix.close(self.fd);
+ self.* = undefined;
+}