summaryrefslogtreecommitdiff
path: root/src/poll_common.c
blob: 868fa46523ce9d4c7c2985f97a7b043e1ef04b14 (plain)
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
#define AL_LOG_SECTION "poll"
#include <al/log.h>
#include <nnwt/error.h>

#include "poll_common.h"

#ifdef STELA_POLL_INLINE
bool stl_poll_common(struct stl_window *window, struct nn_pollfd *fds, bool block)
{
    fds[0].revents = 0;
#ifdef STELA_PAUSE
    fds[1].revents = 0;
#endif

#ifdef STELA_PAUSE
    if (nn_poll_fds(fds, 2, (block) ? -1 : 0) < 0 && errno != EINTR) {
#else
    if (nn_poll_fds(fds, 1, (block) ? -1 : 0) < 0 && errno != EINTR) {
#endif
        log_error("poll() failed %s (%d).", nn_strerror(errno), errno);
        return false;
    }

    // On Wayland this will happen consistantly if the program is lagging too hard.
    // https://github.com/swaywm/sway/commit/e3f0ba4cd9ca709cac115ade54958885614d889c
    if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) { // Fatal error.
        log_error("poll() returned error in revents (%d).", fds[0].revents);
        al_assert(false);
        exit(0);
        return false;
    }

    return !window->closed;
}
#ifdef STELA_PAUSE
// wake() must be thread-safe.
void stl_wake_common(struct nn_pollfd *fds)
{
    s64 val = 1;
    ssize_t ret = nn_write(fds[1].fd, &val, sizeof(s64));
    al_assert(ret == sizeof(s64));
}
#endif
#endif

bool stl_process_events_common(struct stl_window *window, struct nn_pollfd *fds)
{
#ifdef STELA_POLL_INLINE
#ifdef STELA_PAUSE
    if (fds[1].revents & POLLIN) {
        s64 val;
        ssize_t ret = nn_read(fds[1].fd, &val, sizeof(s64));
        al_assert(ret == sizeof(s64));
        window->pending_refresh = true;
    }
#endif
    return (fds[0].revents & POLLIN);
#else
    (void)fds;
#endif
    return true;
}