aboutsummaryrefslogtreecommitdiff
path: root/src/wl/output/root.zig
blob: 9cffd43e41196a300088d7e962bda8bcfb326963 (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
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,
};


staging: Info = undefined,
info: ?Info = null,

handle: wayland.Object.Ref,
init: wayland.Object.interface.Init(Self) = .{},

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

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

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 format(
    self: *const Self,
    comptime fmt: []const u8,
    options: std.fmt.FormatOptions,
    writer: anytype,
) !void {
    _ = fmt;
    _ = options;
    if (self.info) |info| {
        try writer.print("wl.output({s}) [ {} ]", .{ info.name, info.mode});
    } else {
        try writer.print("wl.output(?)", .{});
    }
}