~andreafeletto/levee

ref: 687c1d16212671656bb79436df39feb6275e00a3 levee/src/event.zig -rw-r--r-- 4.3 KiB
687c1d16Andrea Feletto add case for battery capacity computation 8 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
const std = @import("std");
const mem = std.mem;
const os = std.os;
const ArrayList = std.ArrayList;

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

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

pub const Loop = struct {
    state: *State,
    fds: [4]os.pollfd,
    monitor: *udev.Monitor,

    pub fn init(state: *State) !Loop {
        // signals
        var mask = mem.zeroes(os.linux.sigset_t);
        os.linux.sigaddset(&mask, os.linux.SIG.INT);
        os.linux.sigaddset(&mask, os.linux.SIG.TERM);
        os.linux.sigaddset(&mask, os.linux.SIG.QUIT);
        _ = os.linux.sigprocmask(os.linux.SIG.BLOCK, &mask, null);
        const sfd = os.linux.signalfd(-1, &mask, os.linux.SFD.NONBLOCK);

        // wayland
        const wfd = state.wayland.display.getFd();

        // timer
        const tfd = os.linux.timerfd_create(
            os.CLOCK.MONOTONIC,
            os.linux.TFD.CLOEXEC,
        );
        const interval: os.linux.itimerspec = .{
            .it_interval = .{ .tv_sec = 10, .tv_nsec = 0 },
            .it_value = .{ .tv_sec = 10, .tv_nsec = 0 },
        };
        _ = os.linux.timerfd_settime(@intCast(i32, tfd), 0, &interval, null);

        // udev
        const context = try udev.Udev.new();
        const monitor = try udev.Monitor.newFromNetlink(context, "udev");
        try monitor.filterAddMatchSubsystemDevType("backlight", null);
        try monitor.filterAddMatchSubsystemDevType("power_supply", null);
        try monitor.enableReceiving();
        const ufd = try monitor.getFd();

        // poll fds
        const fds: [4]os.fd_t = .{
            @intCast(os.fd_t, sfd),
            @intCast(os.fd_t, wfd),
            @intCast(os.fd_t, tfd),
            @intCast(os.fd_t, ufd),
        };
        var pfds: [4]os.pollfd = undefined;
        for (fds) |fd, i| {
            pfds[i] = .{ .fd = fd, .events = os.POLL.IN, .revents = 0 };
        }

        return Loop{
            .state = state,
            .fds = pfds,
            .monitor = monitor,
        };
    }

    pub fn run(self: *Loop) !void {
        const display = self.state.wayland.display;

        while (true) {
            while (true) {
                const ret = try display.dispatchPending();
                _ = try display.flush();
                if (ret <= 0) break;
            }
            _ = try os.poll(&self.fds, -1);

            for (self.fds) |fd| {
                if (fd.revents & os.POLL.HUP != 0) {
                    return;
                }
                if (fd.revents & os.POLL.ERR != 0) {
                    return;
                }
            }

            // signals
            if (self.fds[0].revents & os.POLL.IN != 0) {
                return;
            }

            // wayland
            if (self.fds[1].revents & os.POLL.IN != 0) {
                _ = try display.dispatch();
            }
            if (self.fds[1].revents & os.POLL.OUT != 0) {
                _ = try display.flush();
            }

            // timer
            if (self.fds[2].revents & os.POLL.IN != 0) {
                const tfd = self.fds[2].fd;
                var expirations = mem.zeroes([8]u8);
                _ = try os.read(tfd, &expirations);

                for (self.state.wayland.outputs.items) |output| {
                    if (output.surface) |surface| {
                        if (surface.configured) {
                            render.renderClock(surface) catch continue;
                            render.renderModules(surface) catch continue;
                            surface.clockSurface.commit();
                            surface.modulesSurface.commit();
                            surface.backgroundSurface.commit();
                        }
                    }
                }
            }

            // udev
            if (self.fds[3].revents & os.POLL.IN != 0) {
                _ = try self.monitor.receiveDevice();

                for (self.state.wayland.outputs.items) |output| {
                    if (output.surface) |surface| {
                        if (surface.configured) {
                            render.renderModules(surface) catch continue;
                            surface.modulesSurface.commit();
                            surface.backgroundSurface.commit();
                        }
                    }
                }
            }
        }
    }
};