aboutsummaryrefslogtreecommitdiff
path: root/src/wl/shm/root.zig
blob: 46388ad14ed6bb279999bdb10a43193a76ef3009 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const std = @import("std");
const wayland = @import("../../root.zig");
const wl = wayland.wl;

pub const Pool = @import("pool.zig");
pub const Format = @import("format.zig").Format;
pub const AnonymousFile = @import("anonymous-file.zig");

const Self = @This();

pub const Events = wayland.EventSet(Self, .{ announce_format });
pub const Request = union(enum) {
    create_pool: struct { wayland.Object.Ref, wayland.types.Fd, u32 },
};

handle: wayland.Object.Ref = .null,
init: wayland.Object.interface.Init(Self) = .{},
formats: std.EnumSet(Format) = .initEmpty(),

pub var globals: wayland.Object.interface.Global(Self) = .init;
pub const handler: wl.Registry.GlobalHandler = .{
    .interface = "wl_shm",
    .version = 1,
    .handler = globals.register,
};

fn announce_format(self: * Self, ctx: wayland.Context, fmt: Format) void {
    _ = ctx;
    self.formats.insert(fmt);
}

pub fn create_pool(self: *Self, ctx: wayland.Context, size: usize) !*Pool {
    const pool = try ctx.allocator.create(Pool);
    errdefer ctx.allocator.destroy(pool);

    var file = try AnonymousFile.init_random();
    errdefer file.close();
    try file.truncate(size);

    try pool.init.with(ctx, .{
        .file = file,
        .buffer = try file.mmap(),
    });

    try ctx.display.request(self, .{ .create_pool = .{
        pool.handle,
        .{ .fd = pool.file.fd },
        @truncate(size)
    } });

    return pool;
}

pub fn format(
    self: *const Self,
    comptime fmt: []const u8,
    options: std.fmt.FormatOptions,
    writer: anytype,
) !void {
    _ = fmt;
    _ = options;
    try writer.print("wl.shm {{ ", .{});

    {
        var iterator = self.formats.iterator();
        while (iterator.next()) |f| {
            try writer.print("{s} ", .{@tagName(f)});
        }
    }

    try writer.print("}}", .{});
}