aboutsummaryrefslogtreecommitdiff
path: root/src/wl/shm/root.zig
blob: fd967271285619b1d72b7121b0812d2aa80a2d6c (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
73
74
75
76
77
78
79
80
81
82
83
84
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 },
};

pub var instances: std.ArrayListUnmanaged(*Self) = .empty;

handle: wayland.Object.Ref,
formats: std.EnumSet(Format) = .initEmpty(),

pub fn init(
    self: *Self,
    ctx: *const wayland.Context,
) !void {
    self.* = .{
        .handle = try ctx.display.registry.add_object(
            ctx.allocator,
            wayland.Object.from_self(self),
        ),
    };
}

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

pub const handler: wl.Registry.GlobalHandler = .{
    .interface = "wl_shm",
    .version = 1,
    .handler = register,
};

fn register(ctx: *const wayland.Context) ?wayland.Object.Ref {
    const shm = ctx.allocator.create(Self) catch return null;
    shm.init(ctx) catch return null;

    instances.append(ctx.allocator, shm) catch return null;
    return shm.handle;
}

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

    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("}}", .{});
}