-
Notifications
You must be signed in to change notification settings - Fork 139
/
event-loop.c
284 lines (235 loc) · 6.11 KB
/
event-loop.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
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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/timerfd.h>
#include <signal.h>
#include <sys/signalfd.h>
#include <time.h>
#include <unistd.h>
#include "event-loop.h"
static int init_signalfd() {
sigset_t mask;
int sfd;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGQUIT);
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
fprintf(stderr, "sigprocmask: %s", strerror(errno));
return -1;
}
if ((sfd = signalfd(-1, &mask, SFD_NONBLOCK)) == -1) {
fprintf(stderr, "signalfd: %s", strerror(errno));
return -1;
}
return sfd;
}
bool init_event_loop(struct mako_event_loop *loop, sd_bus *bus,
struct wl_display *display) {
if ((loop->sfd = init_signalfd()) == -1) {
return false;
}
loop->fds[MAKO_EVENT_SIGNAL] = (struct pollfd){
.fd = loop->sfd,
.events = POLLIN,
};
loop->fds[MAKO_EVENT_DBUS] = (struct pollfd){
.fd = sd_bus_get_fd(bus),
.events = POLLIN,
};
loop->fds[MAKO_EVENT_WAYLAND] = (struct pollfd){
.fd = wl_display_get_fd(display),
.events = POLLIN,
};
loop->fds[MAKO_EVENT_TIMER] = (struct pollfd){
.fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC),
.events = POLLIN,
};
loop->bus = bus;
loop->display = display;
wl_list_init(&loop->timers);
return true;
}
void finish_event_loop(struct mako_event_loop *loop) {
close(loop->fds[MAKO_EVENT_TIMER].fd);
loop->fds[MAKO_EVENT_TIMER].fd = -1;
struct mako_timer *timer, *tmp;
wl_list_for_each_safe(timer, tmp, &loop->timers, link) {
destroy_timer(timer);
}
}
static void timespec_add(struct timespec *t, int delta_ms) {
static const long ms = 1000000, s = 1000000000;
int delta_ms_low = delta_ms % 1000;
int delta_s_high = delta_ms / 1000;
t->tv_sec += delta_s_high;
t->tv_nsec += (long)delta_ms_low * ms;
if (t->tv_nsec >= s) {
t->tv_nsec -= s;
++t->tv_sec;
}
}
static bool timespec_less(struct timespec *t1, struct timespec *t2) {
if (t1->tv_sec != t2->tv_sec) {
return t1->tv_sec < t2->tv_sec;
}
return t1->tv_nsec < t2->tv_nsec;
}
static void update_event_loop_timer(struct mako_event_loop *loop) {
int timer_fd = loop->fds[MAKO_EVENT_TIMER].fd;
if (timer_fd < 0) {
return;
}
bool updated = false;
struct mako_timer *timer;
wl_list_for_each(timer, &loop->timers, link) {
if (loop->next_timer == NULL ||
timespec_less(&timer->at, &loop->next_timer->at)) {
loop->next_timer = timer;
updated = true;
}
}
if (updated) {
struct itimerspec delay = { .it_value = loop->next_timer->at };
errno = 0;
int ret = timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &delay, NULL);
if (ret < 0) {
fprintf(stderr, "failed to timerfd_settime(): %s\n",
strerror(errno));
}
}
}
struct mako_timer *add_event_loop_timer(struct mako_event_loop *loop,
int delay_ms, mako_event_loop_timer_func_t func, void *data) {
struct mako_timer *timer = calloc(1, sizeof(struct mako_timer));
if (timer == NULL) {
fprintf(stderr, "allocation failed\n");
return NULL;
}
timer->event_loop = loop;
timer->func = func;
timer->user_data = data;
wl_list_insert(&loop->timers, &timer->link);
clock_gettime(CLOCK_MONOTONIC, &timer->at);
timespec_add(&timer->at, delay_ms);
update_event_loop_timer(loop);
return timer;
}
void destroy_timer(struct mako_timer *timer) {
if (timer == NULL) {
return;
}
struct mako_event_loop *loop = timer->event_loop;
if (loop->next_timer == timer) {
loop->next_timer = NULL;
}
wl_list_remove(&timer->link);
free(timer);
update_event_loop_timer(loop);
}
static void handle_event_loop_timer(struct mako_event_loop *loop) {
int timer_fd = loop->fds[MAKO_EVENT_TIMER].fd;
uint64_t expirations;
ssize_t n = read(timer_fd, &expirations, sizeof(expirations));
if (n < 0) {
fprintf(stderr, "failed to read from timer FD\n");
return;
}
struct mako_timer *timer = loop->next_timer;
if (timer == NULL) {
return;
}
mako_event_loop_timer_func_t func = timer->func;
void *user_data = timer->user_data;
destroy_timer(timer);
func(user_data);
}
int run_event_loop(struct mako_event_loop *loop) {
loop->running = true;
int ret = 0;
// Unprocessed messages can be queued up by synchronous sd_bus methods. We
// need to process these.
do {
ret = sd_bus_process(loop->bus, NULL);
} while (ret > 0);
if (ret < 0) {
return ret;
}
while (loop->running) {
errno = 0;
// Wayland requests can be generated while handling non-Wayland events.
// We need to flush these.
do {
ret = wl_display_dispatch_pending(loop->display);
wl_display_flush(loop->display);
} while (ret > 0);
if (ret < 0) {
fprintf(stderr, "failed to dispatch pending Wayland events\n");
break;
}
// Same for D-Bus.
sd_bus_flush(loop->bus);
ret = poll(loop->fds, MAKO_EVENT_COUNT, -1);
if (!loop->running) {
ret = 0;
break;
}
if (ret < 0) {
fprintf(stderr, "failed to poll(): %s\n", strerror(errno));
break;
}
for (size_t i = 0; i < MAKO_EVENT_COUNT; ++i) {
if (loop->fds[i].revents & POLLHUP) {
loop->running = false;
break;
}
if (loop->fds[i].revents & POLLERR) {
fprintf(stderr, "failed to poll() socket %zu\n", i);
ret = -1;
break;
}
}
if (!loop->running || ret < 0) {
break;
}
if (loop->fds[MAKO_EVENT_SIGNAL].revents & POLLIN) {
break;
}
if (loop->fds[MAKO_EVENT_DBUS].revents & POLLIN) {
do {
ret = sd_bus_process(loop->bus, NULL);
} while (ret > 0);
if (ret < 0) {
fprintf(stderr, "failed to process D-Bus: %s\n",
strerror(-ret));
break;
}
}
if (loop->fds[MAKO_EVENT_DBUS].revents & POLLOUT) {
ret = sd_bus_flush(loop->bus);
if (ret < 0) {
fprintf(stderr, "failed to flush D-Bus: %s\n",
strerror(-ret));
break;
}
}
if (loop->fds[MAKO_EVENT_WAYLAND].revents & POLLIN) {
ret = wl_display_dispatch(loop->display);
if (ret < 0) {
fprintf(stderr, "failed to read Wayland events\n");
break;
}
}
if (loop->fds[MAKO_EVENT_WAYLAND].revents & POLLOUT) {
ret = wl_display_flush(loop->display);
if (ret < 0) {
fprintf(stderr, "failed to flush Wayland events\n");
break;
}
}
if (loop->fds[MAKO_EVENT_TIMER].revents & POLLIN) {
handle_event_loop_timer(loop);
}
}
return ret;
}