aboutsummaryrefslogtreecommitdiff
path: root/src/wl/shm/pool.zig
blob: 1f42a179a33fbb5253191fd60d039948ae392342 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const std = @import("std");
const wayland = @import("../../root.zig");
const wl = wayland.wl;
const Format = wl.Shm.Format;
const AnonymousFile = wl.Shm.AnonymousFile;

const Self = @This();

pub const Events = wayland.EventSet(@This(), .{});
pub const Requests = union(enum) {
    create_buffer: struct { u32, u32, u32, u32, u32, Format },
    destroy: struct {},
    resize: struct { u32 },
};

handle: wayland.Object.Ref,
file: AnonymousFile,
buffer: []u8,

pub fn init(
    self: *Self,
    ctx: *const wayland.Context,
    size: usize,
) !void {
    var file = try AnonymousFile.init_random();
    errdefer file.close();
    try file.truncate(size);

    self.* = .{
        .handle = try ctx.display.registry.add_object(
            ctx.allocator,
            wayland.Object.from_self(self),
        ),
        .file = file,
        .buffer = try file.mmap(),
    };
}

pub fn deinit(self: *Self, ctx: *const wayland.Context) void {
    self.file.close();
    std.posix.munmap(@alignCast(self.buffer));
    ctx.display.registry.disable_object(self.handle);
    self.* = undefined;
}