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
14 struct 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
20 struct 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
28 struct 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
loop_create(void)37 struct 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
loop_destroy(struct loop * loop)50 void 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
loop_poll(struct loop * loop)65 void 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 poll(loop->fds, loop->fd_length, ms);
85
86 // Dispatch fds
87 size_t fd_index = 0;
88 struct loop_fd_event *event = NULL;
89 wl_list_for_each(event, &loop->fd_events, link) {
90 struct pollfd pfd = loop->fds[fd_index];
91
92 // Always send these events
93 unsigned events = pfd.events | POLLHUP | POLLERR;
94
95 if (pfd.revents & events) {
96 event->callback(pfd.fd, pfd.revents, event->data);
97 }
98
99 ++fd_index;
100 }
101
102 // Dispatch timers
103 if (!wl_list_empty(&loop->timers)) {
104 struct timespec now;
105 clock_gettime(CLOCK_MONOTONIC, &now);
106 struct loop_timer *timer = NULL, *tmp_timer = NULL;
107 wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) {
108 if (timer->removed) {
109 wl_list_remove(&timer->link);
110 free(timer);
111 continue;
112 }
113
114 bool expired = timer->expiry.tv_sec < now.tv_sec ||
115 (timer->expiry.tv_sec == now.tv_sec &&
116 timer->expiry.tv_nsec < now.tv_nsec);
117 if (expired) {
118 timer->callback(timer->data);
119 wl_list_remove(&timer->link);
120 free(timer);
121 }
122 }
123 }
124 }
125
loop_add_fd(struct loop * loop,int fd,short mask,void (* callback)(int fd,short mask,void * data),void * data)126 void loop_add_fd(struct loop *loop, int fd, short mask,
127 void (*callback)(int fd, short mask, void *data), void *data) {
128 struct loop_fd_event *event = calloc(1, sizeof(struct loop_fd_event));
129 if (!event) {
130 swaylock_log(LOG_ERROR, "Unable to allocate memory for event");
131 return;
132 }
133 event->callback = callback;
134 event->data = data;
135 wl_list_insert(loop->fd_events.prev, &event->link);
136
137 struct pollfd pfd = {fd, mask, 0};
138
139 if (loop->fd_length == loop->fd_capacity) {
140 loop->fd_capacity += 10;
141 loop->fds = realloc(loop->fds,
142 sizeof(struct pollfd) * loop->fd_capacity);
143 }
144
145 loop->fds[loop->fd_length++] = pfd;
146 }
147
loop_add_timer(struct loop * loop,int ms,void (* callback)(void * data),void * data)148 struct loop_timer *loop_add_timer(struct loop *loop, int ms,
149 void (*callback)(void *data), void *data) {
150 struct loop_timer *timer = calloc(1, sizeof(struct loop_timer));
151 if (!timer) {
152 swaylock_log(LOG_ERROR, "Unable to allocate memory for timer");
153 return NULL;
154 }
155 timer->callback = callback;
156 timer->data = data;
157
158 clock_gettime(CLOCK_MONOTONIC, &timer->expiry);
159 timer->expiry.tv_sec += ms / 1000;
160
161 long int nsec = (ms % 1000) * 1000000;
162 if (timer->expiry.tv_nsec + nsec >= 1000000000) {
163 timer->expiry.tv_sec++;
164 nsec -= 1000000000;
165 }
166 timer->expiry.tv_nsec += nsec;
167
168 wl_list_insert(&loop->timers, &timer->link);
169
170 return timer;
171 }
172
loop_remove_fd(struct loop * loop,int fd)173 bool loop_remove_fd(struct loop *loop, int fd) {
174 size_t fd_index = 0;
175 struct loop_fd_event *event = NULL, *tmp_event = NULL;
176 wl_list_for_each_safe(event, tmp_event, &loop->fd_events, link) {
177 if (loop->fds[fd_index].fd == fd) {
178 wl_list_remove(&event->link);
179 free(event);
180
181 loop->fd_length--;
182 memmove(&loop->fds[fd_index], &loop->fds[fd_index + 1],
183 sizeof(struct pollfd) * (loop->fd_length - fd_index));
184 return true;
185 }
186 ++fd_index;
187 }
188 return false;
189 }
190
loop_remove_timer(struct loop * loop,struct loop_timer * remove)191 bool loop_remove_timer(struct loop *loop, struct loop_timer *remove) {
192 struct loop_timer *timer = NULL, *tmp_timer = NULL;
193 wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) {
194 if (timer == remove) {
195 timer->removed = true;
196 return true;
197 }
198 }
199 return false;
200 }
201