aboutsummaryrefslogtreecommitdiff
path: root/src/wl/output/root.zig
blob: fca2ecc3f112babbd98231d8b30b6f0d1758d410 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const std = @import("std");
const wayland = @import("../../root.zig");
const wl = wayland.wl;

pub const Subpixel = @import("subpixel.zig").Subpixel;
pub const Transform = @import("transform.zig").Transform;
pub const Geometry = @import("geometry.zig");
pub const Mode = @import("mode.zig");

const Self = @This();

pub const Requests = union(enum) {
    release: struct {},
};

pub const Events = wayland.EventSet(Self, .{
    geometry,
    mode,
    done,
    scale,
    name,
    description,
});

pub const Info = struct {
    geometry: Geometry,
    mode: Mode,
    scale: u32,
    name: []const u8,
    description: []const u8,
};

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

staging: Info = undefined,
info: ?Info = null,
handle: wayland.Object.Ref,

pub fn init(
    self: *Self,
    allocator: std.mem.Allocator,
    display: *wl.Display,
) !void {
    self.* = .{
        .handle = try display.registry.add_object(
            allocator,
            wayland.Object.from_self(self),
        ),
    };
}

pub fn deinit(self: *Self, display: *wl.Display) !void {
    display.request(self, .{ .release = void{} });
}

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

pub fn register(ctx: *wayland.Context) ?wayland.Object.Ref {
    const output = ctx.allocator.create(Self) catch return null;
    output.init(ctx.allocator, ctx.display) catch return null;
    outputs.append(ctx.allocator, output) catch return null;
    return output.handle;
}

fn geometry(
    self: *Self,
    ctx: *wayland.Context,
    x: u32,
    y: u32,
    physical_width: u32,
    physical_height: u32,
    subpixel: Subpixel,
    make: []const u8,
    model: []const u8,
    transform: Transform,
) void {
    _ = ctx;
    self.staging.geometry = .{
        .x = x,
        .y = y,
        .physical_width = physical_width,
        .physical_height = physical_height,
        .subpixel = subpixel,
        .make = make,
        .model = model,
        .transform = transform,
    };
}

fn mode(
    self: *Self,
    ctx: *wayland.Context,
    flags: Mode.Kind,
    width: u32,
    height: u32,
    refresh: u32,
) void {
    _ = ctx;
    self.staging.mode = .{
        .kind = flags,
        .width = width,
        .height = height,
        .refresh = refresh,
    };
}

fn done(self: *Self, ctx: *wayland.Context) void {
    if (self.info) |info| {
        ctx.allocator.free(info.name);
        ctx.allocator.free(info.description);
    }

    self.info = self.staging;
}

fn scale(self: *Self, ctx: *wayland.Context, factor: u32) void {
    _ = ctx;
    self.staging.scale = factor;
}

fn name(self: *Self, ctx: *wayland.Context, n: []const u8) !void {
    self.staging.name = try ctx.allocator.alloc(u8, n.len);
    @memcpy(@constCast(self.staging.name), n);
}

fn description(self: *Self, ctx: *wayland.Context, d: []const u8) !void {
    self.staging.description = try ctx.allocator.alloc(u8, d.len);
    @memcpy(@constCast(self.staging.description), d);
}

pub fn get(index: usize) *Self {
    return outputs.items[index];
}

pub fn all() []*Self {
    return outputs.items;
}

pub fn format(
    self: *const Self,
    comptime fmt: []const u8,
    options: std.fmt.FormatOptions,
    writer: anytype,
) !void {
    _ = fmt;
    _ = options;
    if (self.info) |info| {
        try writer.print("output({s}) [ {} ]", .{ info.name, info.mode});
    } else {
        try writer.print("output(?)", .{});
    }
}