~andreafeletto/levee

ref: ae55230ec4895a224104a266195278ff23a8d44b levee/src/wayland.zig -rw-r--r-- 4.5 KiB
ae55230eAndrea Feletto correct rendering of clock 3 months ago
                                                                                
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
const std = @import("std");
const mem = std.mem;
const os = std.os;

const wayland = @import("wayland").client;
const wl = wayland.wl;
const zwlr = wayland.zwlr;

const State = @import("main.zig").State;

pub const WaylandError = error{
    GlobalsUnavailable,
};

pub const Wayland = struct {
    allocator: *mem.Allocator,

    display: *wl.Display,
    registry: *wl.Registry,

    shm: ?*wl.Shm = null,
    compositor: ?*wl.Compositor = null,
    output: ?*wl.Output = null,
    layerShell: ?*zwlr.LayerShellV1 = null,

    wlSurface: *wl.Surface,
    layerSurface: *zwlr.LayerSurfaceV1,

    running: bool = false,
    width: u32 = 0,
    height: u32 = 0,

    pub fn create(allocator: *mem.Allocator) !*Wayland {
        const result = try allocator.create(Wayland);
        errdefer allocator.destroy(result);
        result.allocator = allocator;

        result.display = try wl.Display.connect(null);
        errdefer result.display.disconnect();

        result.registry = try result.display.getRegistry();
        result.registry.setListener(*Wayland, globalListener, result);

        result.running = false;
        const sync_callback = try result.display.sync();
        sync_callback.setListener(*Wayland, syncListener, result);

        _ = try result.display.roundtrip();
        return if (result.running) result else error.GlobalsUnavailable;
    }

    pub fn setup(self: *Wayland, height: u32) !void {
        const surface = self.layerSurface;

        surface.setSize(0, height);
        surface.setAnchor(.{ .top = true, .left = true, .right = true, .bottom = false });
        surface.setExclusiveZone(@intCast(i32, height));
        surface.setMargin(0, 0, 0, 0);
        surface.setListener(*Wayland, layerSurfaceListener, self);

        self.wlSurface.commit();
        _ = try self.display.roundtrip();
    }

    pub fn loop(self: *Wayland) void {
        while (self.running) {
            _ = self.display.dispatch() catch return;
        }
    }

    pub fn destroy(self: *Wayland) void {
        self.display.disconnect();
        self.allocator.destroy(self);
    }
};

fn globalListener(registry: *wl.Registry, event: wl.Registry.Event, state: *Wayland) void {
    switch (event) {
        .global => |global| {
            const cmp = std.cstr.cmp;
            const interface = global.interface;

            if (cmp(interface, wl.Compositor.getInterface().name) == 0) {
                state.compositor = registry.bind(global.name, wl.Compositor, 4) catch return;
            } else if (cmp(interface, wl.Shm.getInterface().name) == 0) {
                state.shm = registry.bind(global.name, wl.Shm, 1) catch return;
            } else if (cmp(interface, wl.Output.getInterface().name) == 0) {
                state.output = registry.bind(global.name, wl.Output, 1) catch return;
            } else if (cmp(interface, zwlr.LayerShellV1.getInterface().name) == 0) {
                state.layerShell = registry.bind(global.name, zwlr.LayerShellV1, 1) catch return;
            }
        },
        .global_remove => {},
    }
}

fn syncListener(callback: *wl.Callback, event: wl.Callback.Event, state: *Wayland) void {
    switch (event) {
        .done => {
            if (state.shm == null) {
                std.log.err("globals: shared memory not available", .{});
                return;
            }
            if (state.compositor == null) {
                std.log.err("globals: compositor not available", .{});
                return;
            }
            if (state.output == null) {
                std.log.err("globals: output not available", .{});
                return;
            }
            if (state.layerShell == null) {
                std.log.err("globals: layer shell not available", .{});
                return;
            }

            state.wlSurface = state.compositor.?.createSurface() catch return;

            const shell = state.layerShell.?;
            const output = state.output.?;
            const surface = state.wlSurface;
            state.layerSurface = shell.getLayerSurface(surface, output, .overlay, "wlroots") catch return;

            state.running = true;
        },
    }
}

fn layerSurfaceListener(surface: *zwlr.LayerSurfaceV1, event: zwlr.LayerSurfaceV1.Event, state: *Wayland) void {
    switch (event) {
        .configure => |data| {
            state.width = data.width;
            state.height = data.height;
            surface.ackConfigure(data.serial);
        },
        .closed => {
            surface.destroy();
            state.wlSurface.destroy();
            state.running = false;
        },
    }
}