-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.c
163 lines (131 loc) · 3.38 KB
/
event.c
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
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <sys/param.h>
#include <sys/select.h>
#include <stdio.h>
#define TIMEDIFF(t1, t2) ((t1.tv_sec-t2.tv_sec)*1000 + \
(t1.tv_nsec-t2.tv_nsec)/1E6)
typedef struct event_fd {
struct event_fd *prev, *next;
void (*fn)(int fd, void *arg);
void *arg;
int fd;
} event_fd_t;
static event_fd_t event_fd_list;
static double minlatency = 8;
static double maxlatency = 33;
static double (*event_update_timeout_cb)(double timeout, void *arg);
static bool (*event_pre_handler_cb)(void *arg);
static void event_fd_list_insert(event_fd_t *list, event_fd_t *evt)
{
evt->next = list->next;
evt->prev = list;
if (list->next)
list->next->prev = evt;
list->next = evt;
}
static void event_fd_list_delete(event_fd_t *evt)
{
evt->prev->next = evt->next;
if (evt->next)
evt->next->prev = evt->prev;
}
void event_update_timeout_cb_set(double (*cb)(double timeout, void *arg))
{
event_update_timeout_cb = cb;
}
void event_pre_handler_cb_set(bool (*cb)(void *arg))
{
event_pre_handler_cb = cb;
}
int event_fd_handler_register(int fd, void (*fn)(int fd, void *arg), void *arg)
{
event_fd_t *evt;
evt = malloc(sizeof(*evt));
if (!evt)
return -1;
evt->arg = arg;
evt->fn = fn;
evt->fd = fd;
event_fd_list_insert(&event_fd_list, evt);
return 0;
}
void event_fd_handler_unregister(int fd)
{
event_fd_t *evt = event_fd_list.next;;
int i;
while (evt) {
if (evt->fd == fd) {
event_fd_list_delete(evt);
free(evt);
return;
}
evt = evt->next;
}
}
int event_process(void *arg)
{
struct timespec seltv, *tv, now, lastblink, trigger;
sigset_t emptyset;
double timeout = -1;
event_fd_t *evt;
int r, nfds = 0;
fd_set rd;
int idle;
for (idle = 0;;) {
bool ev = false;
FD_ZERO(&rd);
sigemptyset(&emptyset);
for (evt = event_fd_list.next; evt; evt = evt->next) {
FD_SET(evt->fd, &rd);
nfds = MAX(nfds, evt->fd);
}
if (event_update_timeout_cb)
timeout = event_update_timeout_cb(timeout, arg);
seltv.tv_sec = timeout / 1E3;
seltv.tv_nsec = 1E6 * (timeout - 1E3 * seltv.tv_sec);
tv = timeout >= 0 ? &seltv : NULL;
r = pselect(nfds + 1, &rd, NULL, NULL, tv, &emptyset);
if (r < 0) {
if (errno == EINTR)
continue;
}
clock_gettime(CLOCK_MONOTONIC, &now);
if (event_pre_handler_cb)
ev = event_pre_handler_cb(arg);
evt = event_fd_list.next;
while (evt) {
event_fd_t *next = evt->next;
if (FD_ISSET(evt->fd, &rd) && evt->fn) {
evt->fn(evt->fd, evt->arg);
}
evt = next;
}
/*
* To reduce flicker and tearing, when new content or event
* triggers drawing, we first wait a bit to ensure we got
* everything, and if nothing new arrives - we draw.
* We start with trying to wait minlatency ms. If more content
* arrives sooner, we retry with shorter and shorter periods,
* and eventually draw even without idle after maxlatency ms.
* Typically this results in low latency while interacting,
* maximum latency intervals during `cat huge.txt`, and perfect
* sync with periodic updates from animations/key-repeats/etc.
*/
if (r || ev) {
if (!idle) {
trigger = now;
idle = 1;
}
timeout = (maxlatency - TIMEDIFF(now, trigger)) \
/ maxlatency * minlatency;
if (timeout > 0)
continue; /* we have time, try to find idle */
}
/* idle detected or maxlatency exhausted -> draw */
return r;
}
}