~andreafeletto/levee

ref: ee46a7673c63c9890c1bf6b0acb0c9e1c8b1f5b2 levee/src/Wayland.zig -rw-r--r-- 6.8 KiB
ee46a767Andrea Feletto require at least one module a month 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
const std = @import("std");
const mem = std.mem;
const meta = std.meta;
const os = std.os;
const strcmp = std.cstr.cmp;
const ArrayList = std.ArrayList;

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

const Bar = @import("Bar.zig");
const Event = @import("Loop.zig").Event;
const Input = @import("Input.zig");
const Monitor = @import("Monitor.zig");
const State = @import("main.zig").State;
const utils = @import("utils.zig");
const Wayland = @This();

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

monitors: ArrayList(*Monitor),
inputs: ArrayList(*Input),
globals: Globals,
globalsMask: GlobalsMask,

const Globals = struct {
    compositor: *wl.Compositor,
    subcompositor: *wl.Subcompositor,
    shm: *wl.Shm,
    layerShell: *zwlr.LayerShellV1,
    statusManager: *zriver.StatusManagerV1,
    control: *zriver.ControlV1,
};
const GlobalsMask = utils.Mask(Globals);

pub fn init(state: *State) !Wayland {
    const display = try wl.Display.connect(null);

    return Wayland{
        .state = state,
        .display = display,
        .registry = try display.getRegistry(),
        .monitors = ArrayList(*Monitor).init(state.gpa),
        .inputs = ArrayList(*Input).init(state.gpa),
        .globals = undefined,
        .globalsMask = mem.zeroes(GlobalsMask),
    };
}

pub fn deinit(self: *Wayland) void {
    for (self.monitors.items) |monitor| monitor.destroy();
    for (self.inputs.items) |input| input.destroy();

    self.monitors.deinit();
    self.inputs.deinit();
}

pub fn registerGlobals(self: *Wayland) !void {
    self.registry.setListener(*State, registryListener, self.state);
    if (self.display.roundtrip() != .SUCCESS) {
        std.log.err("failed roundtrip for initialization of globals", .{});
        os.exit(1);
    }

    for (self.globalsMask) |is_registered| if (!is_registered) {
        std.log.err("missing global", .{});
        os.exit(1);
    };
}

pub fn getEvent(self: *Wayland) !Event {
    const fd = self.display.getFd();

    return Event{
        .fd = .{
            .fd = @intCast(os.fd_t, fd),
            .events = os.POLL.IN,
            .revents = undefined,
        },
        .data = @ptrCast(*anyopaque, self),
        .callbackIn = read,
        .callbackOut = Event.noop,
    };
}

pub fn flushAndPrepareRead(self: *Wayland) void {
    while (!self.display.prepareRead()) {
        const errno = self.display.dispatchPending();
        if (errno != .SUCCESS) {
            std.log.err("failed to dispatch pending wayland events", .{});
            os.exit(1);
        }
    }

    while (true) {
        const errno = self.display.flush();
        switch (errno) {
            .SUCCESS => return,
            .PIPE => {
                _ = self.display.readEvents();
                std.log.err("connection to wayland server unexpectedly terminated", .{});
                os.exit(1);
            },
            .AGAIN => {
                var wayland_out = [_]os.pollfd{.{
                    .fd = self.display.getFd(),
                    .events = os.POLL.OUT,
                    .revents = undefined,
                }};
                _ = os.poll(&wayland_out, -1) catch {
                    std.log.err("polling for wayland socket being writable failed", .{});
                    os.exit(1);
                };
            },
            else => {
                std.log.err("failed to flush wayland requests", .{});
                os.exit(1);
            },
        }
    }
}

fn read(self_opaque: *anyopaque) void {
    const self = utils.cast(Wayland)(self_opaque);
    const errno = self.display.readEvents();
    if (errno != .SUCCESS) {
        std.log.err("failed to read wayland events", .{});
        os.exit(1);
    }
}

fn registryListener(
    registry: *wl.Registry,
    event: wl.Registry.Event,
    state: *State,
) void {
    const self = &state.wayland;

    switch (event) {
        .global => |g| {
            self.bindGlobal(registry, g.interface, g.name) catch return;
        },
        .global_remove => |data| {
            for (self.monitors.items) |monitor, i| {
                if (monitor.globalName == data.name) {
                    monitor.destroy();
                    _ = self.monitors.swapRemove(i);
                    break;
                }
            }
            for (self.inputs.items) |input, i| {
                if (input.globalName == data.name) {
                    input.destroy();
                    _ = self.inputs.swapRemove(i);
                    break;
                }
            }
        },
    }
}

fn bindGlobal(
    self: *Wayland,
    registry: *wl.Registry,
    iface: [*:0]const u8,
    name: u32,
) !void {
    if (strcmp(iface, wl.Compositor.getInterface().name) == 0) {
        const global = try registry.bind(name, wl.Compositor, 4);
        self.setGlobal(global);
    } else if (strcmp(iface, wl.Subcompositor.getInterface().name) == 0) {
        const global = try registry.bind(name, wl.Subcompositor, 1);
        self.setGlobal(global);
    } else if (strcmp(iface, wl.Shm.getInterface().name) == 0) {
        const global = try registry.bind(name, wl.Shm, 1);
        self.setGlobal(global);
    } else if (strcmp(iface, zwlr.LayerShellV1.getInterface().name) == 0) {
        const global = try registry.bind(name, zwlr.LayerShellV1, 1);
        self.setGlobal(global);
    } else if (
        strcmp(iface, zriver.StatusManagerV1.getInterface().name) == 0
    ) {
        const global = try registry.bind(name, zriver.StatusManagerV1, 1);
        self.setGlobal(global);
    } else if (strcmp(iface, zriver.ControlV1.getInterface().name) == 0) {
        const global = try registry.bind(name, zriver.ControlV1, 1);
        self.setGlobal(global);
    } else if (strcmp(iface, wl.Output.getInterface().name) == 0) {
        const monitor = try Monitor.create(self.state, registry, name);
        try self.monitors.append(monitor);
    } else if (strcmp(iface, wl.Seat.getInterface().name) == 0) {
        const input = try Input.create(self.state, registry, name);
        try self.inputs.append(input);
    }
}

pub fn setGlobal(self: *Wayland, global: anytype) void {
    inline for (meta.fields(Globals)) |field, i| {
        if (field.field_type == @TypeOf(global)) {
            @field(self.globals, field.name) = global;
            self.globalsMask[i] = true;
            break;
        }
    }
}

pub fn findBar(self: *Wayland, wlSurface: ?*wl.Surface) ?*Bar {
    if (wlSurface == null) {
        return null;
    }
    for (self.monitors.items) |monitor| {
        if (monitor.bar) |bar| {
            if (bar.background.surface == wlSurface or
                bar.tags.surface == wlSurface or
                bar.clock.surface == wlSurface or
                bar.modules.surface == wlSurface)
            {
                return bar;
            }
        }
    }
    return null;
}