~andreafeletto/levee

ref: f2b74950b7431f6024814ff54cbc0949e29e9a5d levee/src/wayland.zig -rw-r--r-- 10.0 KiB
f2b74950Andrea Feletto add args for help and modules 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
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 Buffer = @import("Buffer.zig");
const Event = @import("Loop.zig").Event;
const render = @import("render.zig");
const State = @import("main.zig").State;
const Bar = @import("Bar.zig");
const Tags = @import("Tags.zig");
const utils = @import("utils.zig");

pub const Wayland = struct {
    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);
        _ = try self.display.roundtrip();

        for (self.globalsMask) |is_registered| {
            if (!is_registered) return error.UnsupportedGlobal;
        }
    }

    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 = dispatch,
            .callbackOut = flush,
        };
    }

    fn dispatch(self_opaque: *anyopaque) error{Terminate}!void {
        const self = utils.cast(Wayland)(self_opaque);
        _ = self.display.dispatch() catch return;
    }

    fn flush(self_opaque: *anyopaque) error{Terminate}!void {
        const self = utils.cast(Wayland)(self_opaque);
        _ = self.display.flush() catch return;
    }

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

pub const Monitor = struct {
    state: *State,
    output: *wl.Output,
    globalName: u32,
    scale: i32,

    bar: ?*Bar,
    tags: *Tags,

    pub fn create(state: *State, registry: *wl.Registry, name: u32) !*Monitor {
        const self = try state.gpa.create(Monitor);
        self.state = state;
        self.output = try registry.bind(name, wl.Output, 3);
        self.globalName = name;
        self.scale = 1;

        self.bar = null;
        self.tags = try Tags.create(state, self);

        self.output.setListener(*Monitor, listener, self);
        return self;
    }

    pub fn destroy(self: *Monitor) void {
        if (self.bar) |bar| {
            bar.destroy();
        }
        self.tags.destroy();
        self.state.gpa.destroy(self);
    }

    fn listener(_: *wl.Output, event: wl.Output.Event, monitor: *Monitor) void {
        switch (event) {
            .scale => |scale| {
                monitor.scale = scale.factor;
            },
            .geometry => {},
            .mode => {},
            .name => {},
            .description => {},
            .done => {
                if (monitor.bar) |_| {} else {
                    monitor.bar = Bar.create(monitor) catch return;
                }
            },
        }
    }
};

pub const Input = struct {
    state: *State,
    seat: *wl.Seat,
    globalName: u32,

    pointer: struct {
        wlPointer: ?*wl.Pointer,
        x: i32,
        y: i32,
        bar: ?*Bar,
    },

    pub fn create(state: *State, registry: *wl.Registry, name: u32) !*Input {
        const self = try state.gpa.create(Input);
        self.state = state;
        self.seat = try registry.bind(name, wl.Seat, 3);
        self.globalName = name;

        self.pointer.wlPointer = null;
        self.pointer.bar = null;

        self.seat.setListener(*Input, listener, self);
        return self;
    }

    pub fn destroy(self: *Input) void {
        if (self.pointer.wlPointer) |wlPointer| {
            wlPointer.release();
        }
        self.seat.release();
        self.state.gpa.destroy(self);
    }

    fn listener(seat: *wl.Seat, event: wl.Seat.Event, input: *Input) void {
        switch (event) {
            .capabilities => |data| {
                if (input.pointer.wlPointer) |wlPointer| {
                    wlPointer.release();
                    input.pointer.wlPointer = null;
                }
                if (data.capabilities.pointer) {
                    input.pointer.wlPointer = seat.getPointer() catch return;
                    input.pointer.wlPointer.?.setListener(
                        *Input,
                        pointerListener,
                        input,
                    );
                }
            },
            .name => {},
        }
    }

    fn pointerListener(
        _: *wl.Pointer,
        event: wl.Pointer.Event,
        input: *Input,
    ) void {
        switch (event) {
            .enter => |data| {
                input.pointer.x = data.surface_x.toInt();
                input.pointer.y = data.surface_y.toInt();
                const bar = input.state.wayland.findBar(data.surface);
                input.pointer.bar = bar;
            },
            .leave => |_| {
                input.pointer.bar = null;
            },
            .motion => |data| {
                input.pointer.x = data.surface_x.toInt();
                input.pointer.y = data.surface_y.toInt();
            },
            .button => |data| {
                if (data.state != .pressed) return;
                if (input.pointer.bar) |bar| {
                    if (!bar.configured) return;

                    const x = @intCast(u32, input.pointer.x);
                    if (x < bar.height * 9) {
                        bar.monitor.tags.handleClick(x, input) catch return;
                    }
                }
            },
            else => {},
        }
    }
};