1#define _POSIX_C_SOURCE 200809L
2#include <limits.h>
3#include <string.h>
4#include <stdbool.h>
5#include <stdlib.h>
6#include <stdio.h>
7#include <poll.h>
8#include <time.h>
9#include <unistd.h>
10#include <wayland-client.h>
11#include "log.h"
12#include "loop.h"
13
14struct loop_fd_event {
15 void (*callback)(int fd, short mask, void *data);
16 void *data;
17 struct wl_list link; // struct loop_fd_event::link
18};
19
20struct loop_timer {
21 void (*callback)(void *data);
22 void *data;
23 struct timespec expiry;
24 bool removed;
25 struct wl_list link; // struct loop_timer::link
26};
27
28struct loop {
29 struct pollfd *fds;
30 int fd_length;
31 int fd_capacity;
32
33 struct wl_list fd_events; // struct loop_fd_event::link
34 struct wl_list timers; // struct loop_timer::link
35};
36
37struct loop *loop_create(void) {
38 struct loop *loop = calloc(1, sizeof(struct loop));
39 if (!loop) {
40 swaylock_log(LOG_ERROR, "Unable to allocate memory for loop");
41 return NULL;
42 }
43 loop->fd_capacity = 10;
44 loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity);
45 wl_list_init(&loop->fd_events);
46 wl_list_init(&loop->timers);
47 return loop;
48}
49
50void loop_destroy(struct loop *loop) {
51 struct loop_fd_event *event = NULL, *tmp_event = NULL;
52 wl_list_for_each_safe(event, tmp_event, &loop->fd_events, link) {
53 wl_list_remove(&event->link);
54 free(event);
55 }
56 struct loop_timer *timer = NULL, *tmp_timer = NULL;
57 wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) {
58 wl_list_remove(&timer->link);
59 free(timer);
60 }
61 free(loop->fds);
62 free(loop);
63}
64
65void loop_poll(struct loop *loop) {
66 // Calculate next timer in ms
67 int ms = INT_MAX;
68 if (!wl_list_empty(&loop->timers)) {
69 struct timespec now;
70 clock_gettime(CLOCK_MONOTONIC, &now);
71 struct loop_timer *timer = NULL;
72 wl_list_for_each(timer, &loop->timers, link) {
73 int timer_ms = (timer->expiry.tv_sec - now.tv_sec) * 1000;
74 timer_ms += (timer->expiry.tv_nsec - now.tv_nsec) / 1000000;
75 if (timer_ms < ms) {
76 ms = timer_ms;
77 }
78 }
79 }
80 if (ms < 0) {
81 ms = 0;
82 }
83
84 int ret = poll(loop->fds, loop->fd_length, ms);
85 if (ret < 0 && errno != EINTR) {
86 swaylock_log_errno(LOG_ERROR, "poll failed");
87 exit(1);
88 }
89
90 // Dispatch fds
91 size_t fd_index = 0;
92 struct loop_fd_event *event = NULL;
93 wl_list_for_each(event, &loop->fd_events, link) {
94 struct pollfd pfd = loop->fds[fd_index];
95
96 // Always send these events
97 unsigned events = pfd.events | POLLHUP | POLLERR;
98
99 if (pfd.revents & events) {
100 event->callback(pfd.fd, pfd.revents, event->data);
101 }
102
103 ++fd_index;
104 }
105
106 // Dispatch timers
107 if (!wl_list_empty(&loop->timers)) {
108 struct timespec now;
109 clock_gettime(CLOCK_MONOTONIC, &now);
110 struct loop_timer *timer = NULL, *tmp_timer = NULL;
111 wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) {
112 if (timer->removed) {
113 wl_list_remove(&timer->link);
114 free(timer);
115 continue;
116 }
117
118 bool expired = timer->expiry.tv_sec < now.tv_sec ||
119 (timer->expiry.tv_sec == now.tv_sec &&
120 timer->expiry.tv_nsec < now.tv_nsec);
121 if (expired) {
122 timer->callback(timer->data);
123 wl_list_remove(&timer->link);
124 free(timer);
125 }
126 }
127 }
128}
129
130void loop_add_fd(struct loop *loop, int fd, short mask,
131 void (*callback)(int fd, short mask, void *data), void *data) {
132 struct loop_fd_event *event = calloc(1, sizeof(struct loop_fd_event));
133 if (!event) {
134 swaylock_log(LOG_ERROR, "Unable to allocate memory for event");
135 return;
136 }
137 event->callback = callback;
138 event->data = data;
139 wl_list_insert(loop->fd_events.prev, &event->link);
140
141 struct pollfd pfd = {fd, mask, 0};
142
143 if (loop->fd_length == loop->fd_capacity) {
144 loop->fd_capacity += 10;
145 loop->fds = realloc(loop->fds,
146 sizeof(struct pollfd) * loop->fd_capacity);
147 }
148
149 loop->fds[loop->fd_length++] = pfd;
150}
151
152struct loop_timer *loop_add_timer(struct loop *loop, int ms,
153 void (*callback)(void *data), void *data) {
154 struct loop_timer *timer = calloc(1, sizeof(struct loop_timer));
155 if (!timer) {
156 swaylock_log(LOG_ERROR, "Unable to allocate memory for timer");
157 return NULL;
158 }
159 timer->callback = callback;
160 timer->data = data;
161
162 clock_gettime(CLOCK_MONOTONIC, &timer->expiry);
163 timer->expiry.tv_sec += ms / 1000;
164
165 long int nsec = (ms % 1000) * 1000000;
166 if (timer->expiry.tv_nsec + nsec >= 1000000000) {
167 timer->expiry.tv_sec++;
168 nsec -= 1000000000;
169 }
170 timer->expiry.tv_nsec += nsec;
171
172 wl_list_insert(&loop->timers, &timer->link);
173
174 return timer;
175}
176
177bool loop_remove_fd(struct loop *loop, int fd) {
178 size_t fd_index = 0;
179 struct loop_fd_event *event = NULL, *tmp_event = NULL;
180 wl_list_for_each_safe(event, tmp_event, &loop->fd_events, link) {
181 if (loop->fds[fd_index].fd == fd) {
182 wl_list_remove(&event->link);
183 free(event);
184
185 loop->fd_length--;
186 memmove(&loop->fds[fd_index], &loop->fds[fd_index + 1],
187 sizeof(struct pollfd) * (loop->fd_length - fd_index));
188 return true;
189 }
190 ++fd_index;
191 }
192 return false;
193}
194
195bool loop_remove_timer(struct loop *loop, struct loop_timer *remove) {
196 struct loop_timer *timer = NULL, *tmp_timer = NULL;
197 wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) {
198 if (timer == remove) {
199 timer->removed = true;
200 return true;
201 }
202 }
203 return false;
204}