aboutsummaryrefslogtreecommitdiff
path: root/src/wl/compositor.zig
blob: ae8d15e7641549108edb4e6c990f592d6dae0d0e (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
const std = @import("std");
const wayland = @import("../root.zig");
const wl = wayland.wl;

const Self = @This();

pub const Events = wayland.EventSet(Self, .{});
pub const Request = union(enum) {
    create_surface: struct { wayland.Object.Ref },
    create_region: struct { wayland.Object.Ref },
};

handle: wayland.Object.Ref,

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

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

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 register(ctx: *const wayland.Context) ?wayland.Object.Ref {
    const compositor = ctx.allocator.create(Self) catch return null;
    compositor.init(ctx) catch return null;

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

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

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

pub fn create_surface(
    self: *Self,
    ctx: *const wayland.Context
) !*wl.Surface {
    const surface = try ctx.allocator.create(wl.Surface);
    errdefer ctx.allocator.destroy(surface);

    try surface.init(ctx);

    try ctx.display.request(self, .{ .create_surface = .{ surface.handle } });

    return surface;
}