forked from emersion/mako
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent-loop.c
224 lines (186 loc) · 5.06 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
#define _POSIX_C_SOURCE 199309L
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
#include "event-loop.h"
void init_event_loop(struct mako_event_loop *loop, sd_bus *bus,
struct wl_display *display) {
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);
}
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 int poll_event_loop(struct mako_event_loop *loop) {
return poll(loop->fds, MAKO_EVENT_COUNT, -1);
}
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;
while (loop->running) {
while (wl_display_prepare_read(loop->display) != 0) {
wl_display_dispatch_pending(loop->display);
}
wl_display_flush(loop->display);
ret = poll_event_loop(loop);
if (!loop->running) {
wl_display_cancel_read(loop->display);
ret = 0;
break;
}
if (ret < 0) {
wl_display_cancel_read(loop->display);
fprintf(stderr, "failed to poll(): %s\n", strerror(-ret));
break;
}
if (!(loop->fds[MAKO_EVENT_WAYLAND].revents & POLLIN)) {
wl_display_cancel_read(loop->display);
}
if (loop->fds[MAKO_EVENT_DBUS].revents & POLLIN) {
while (1) {
ret = sd_bus_process(loop->bus, NULL);
if (ret < 0) {
fprintf(stderr, "failed to process bus: %s\n",
strerror(-ret));
break;
}
if (ret == 0) {
break;
}
// We processed a request, try to process another one, right-away
}
if (ret < 0) {
break;
}
}
if (loop->fds[MAKO_EVENT_WAYLAND].revents & POLLIN) {
ret = wl_display_read_events(loop->display);
if (ret < 0) {
fprintf(stderr, "failed to read Wayland events: %s\n",
strerror(errno));
break;
}
wl_display_dispatch_pending(loop->display);
}
if (loop->fds[MAKO_EVENT_TIMER].revents & POLLIN) {
handle_event_loop_timer(loop);
}
}
return ret;
}
static void handle_stop_event_loop_timer(void *data) {
// No-op
}
void stop_event_loop(struct mako_event_loop *loop) {
loop->running = false;
// Wake up the event loop
add_event_loop_timer(loop, 0, handle_stop_event_loop_timer, NULL);
}