~novakane/agertu

agertu/src/event_loop.zig -rw-r--r-- 4.2 KiB
9ea83d8fHugo Machet build: Bump to version 2.0.0-dev 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
// This file is part of agertu
//
// Copyright (C) 2021-2022 Hugo Machet
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

const std = @import("std");
const os = std.os;
const time = std.time;

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

const backend = &@import("main.zig").backend;
const util = @import("util.zig");

const log = std.log.scoped(.event_loop);

pub fn run() !void {
    const poll_wayland = 0;

    var pfds: [1]os.pollfd = undefined;

    pfds[poll_wayland] = .{
        .fd = backend.display.getFd(),
        .events = os.POLL.IN,
        .revents = 0,
    };

    os.sigaction(os.SIG.TERM, &os.Sigaction{
        .handler = .{ .handler = handle_sig },
        .mask = os.empty_sigset,
        .flags = 0,
    }, null);
    os.sigaction(os.SIG.INT, &os.Sigaction{
        .handler = .{ .handler = handle_sig },
        .mask = os.empty_sigset,
        .flags = 0,
    }, null);

    var timeout: i64 = -1;
    var when: os.timespec = undefined;

    while (backend.initialized) {
        var it = backend.outputs.first;
        while (it) |node| : (it = node.next) {
            const output = &node.data;
            if (output.surface) |surface| {
                if (!surface.configured) continue;

                var now: os.timespec = undefined;
                os.clock_gettime(os.CLOCK.MONOTONIC, &now) catch {
                    @panic("CLOCK_MONOTONIC not supported");
                };

                timespec_diff(&now, &surface.last_frame, &when);
            } else continue;

            const half_second_ns: i64 = 500_000_000; // 0.5 second in nanosecond

            if (when.tv_sec > 0 or
                when.tv_nsec >= (half_second_ns - time.ns_per_ms))
            {
                output.surface.?.deinit();
                util.gpa.destroy(output.surface.?);
                output.surface = null;
            } else {
                timeout = blk: {
                    // ns to ms
                    const _timeout = @divFloor((half_second_ns - when.tv_nsec), time.ns_per_ms);
                    if (timeout == -1 or timeout > _timeout) break :blk @intCast(i32, _timeout);
                    break :blk timeout;
                };
            }
        }

        backend.flush_wayland_and_prepare_read();

        _ = os.poll(&pfds, @intCast(i32, timeout)) catch |err| {
            util.fatal(.event_loop, "failed to poll {s}", .{@errorName(err)});
        };

        if (pfds[poll_wayland].revents & os.POLL.IN != 0) {
            const errno = backend.display.readEvents();
            if (errno != .SUCCESS) {
                util.fatal(.event_loop, "error reading wayland events: {s}", .{@tagName(errno)});
            }
        } else {
            backend.display.cancelRead();
        }
    }

    backend.flush_wayland_and_prepare_read();
}

fn handle_sig(_: c_int) callconv(.C) void {
    backend.initialized = false;
}

fn timespec_diff(ts1: *const os.timespec, ts2: *const os.timespec, result: *os.timespec) void {
    if ((ts1.tv_nsec - ts2.tv_nsec) < 0) {
        result.* = .{
            .tv_sec = (ts1.tv_sec - ts2.tv_sec) - 1,
            .tv_nsec = (ts1.tv_nsec - ts2.tv_nsec) + time.ns_per_s,
        };
    } else result.* = .{
        .tv_sec = ts1.tv_sec - ts2.tv_sec,
        .tv_nsec = ts1.tv_nsec - ts2.tv_nsec,
    };
}

test "timespec functions" {
    const testing = std.testing;
    const ts_a: os.timespec = .{ .tv_sec = 2, .tv_nsec = 500 };
    const ts_b: os.timespec = .{ .tv_sec = 3, .tv_nsec = 0 };
    var ts_c: os.timespec = undefined;
    timespec_diff(&ts_b, &ts_a, &ts_c);

    // 3s - (2s + 500ns) = 999999500ns
    try testing.expect(ts_c.tv_sec == 0);
    try testing.expect(ts_c.tv_nsec == 999999500);
}