~andreafeletto/levee

ref: a773bcb6a7f6170279720581150b2eb8eabf762c levee/src/wayland.zig -rw-r--r-- 4.3 KiB
a773bcb6Andrea Feletto add README.md 5 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
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 Wayland = struct {
    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 = null,
    layerSurface: ?*zwlr.LayerSurfaceV1 = null,

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

    pub fn init() !Wayland {
        const display = try wl.Display.connect(null);
        const registry = try display.getRegistry();
        var result = Wayland{
            .display = display,
            .registry = registry,
        };
        registry.setListener(*Wayland, globalListener, &result);
        _ = try display.roundtrip();

        if (result.shm == null) {
            std.log.err("globals: shared memory not available", .{});
            os.exit(1);
        }
        if (result.compositor == null) {
            std.log.err("globals: compositor not available", .{});
            os.exit(1);
        }
        if (result.output == null) {
            std.log.err("globals: output not available", .{});
            os.exit(1);
        }
        if (result.layerShell == null) {
            std.log.err("globals: layer shell not available", .{});
            os.exit(1);
        }

        result.wlSurface = try result.compositor.?.createSurface();
        result.layerSurface = try result.layerShell.?.getLayerSurface(
            result.wlSurface.?,
            result.output.?,
            .overlay,
            "wlroots"
        );

        return result;
    }

    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 frame(self: *Wayland, state: *State) !void {
        const cb = try self.wlSurface.?.frame();
        cb.setListener(*State, frameListener, state);
        _ = try self.display.roundtrip();
    }

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

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

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 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;
        },
    }
}

fn frameListener(callback: *wl.Callback, event: wl.Callback.Event, state: *State) void {
    switch (event) {
        .done => |time| {
            const cb = state.wayland.wlSurface.?.frame() catch return;
            cb.setListener(*State, frameListener, state);
            state.draw(state);
        }
    }
}