aboutsummaryrefslogtreecommitdiff
path: root/src/wl/shm/root.zig
blob: 2d24aca5337a725a917e3bd70fe0bf518a821667 (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
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;

const Self = @This();

pub const Events = wayland.EventSet(Self, .{ announce_format });
pub const Requests = union(enum) {
    create_pool: struct { u32, 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: *wayland.Context,
) !void {
    self.* = .{
        .handle = try ctx.display.registry.add_object(
            ctx.allocator,
            wayland.Object.from_self(self),
        ),
    };
}

fn announce_format(self: * Self, ctx: *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: *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 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("}}", .{});
}