summaryrefslogtreecommitdiffstats
path: root/common/loop.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/loop.c')
-rw-r--r--common/loop.c179
1 files changed, 179 insertions, 0 deletions
diff --git a/common/loop.c b/common/loop.c
new file mode 100644
index 00000000..1b174967
--- /dev/null
+++ b/common/loop.c
@@ -0,0 +1,179 @@
1#include <limits.h>
2#include <string.h>
3#include <stdbool.h>
4#include <stdlib.h>
5#include <stdio.h>
6#include <poll.h>
7#include <time.h>
8#include <unistd.h>
9#include "list.h"
10#include "log.h"
11#include "loop.h"
12
13struct loop_fd_event {
14 void (*callback)(int fd, short mask, void *data);
15 void *data;
16};
17
18struct loop_timer {
19 void (*callback)(void *data);
20 void *data;
21 struct timespec expiry;
22};
23
24struct loop {
25 struct pollfd *fds;
26 int fd_length;
27 int fd_capacity;
28
29 list_t *fd_events; // struct loop_fd_event
30 list_t *timers; // struct loop_timer
31};
32
33struct loop *loop_create(void) {
34 struct loop *loop = calloc(1, sizeof(struct loop));
35 if (!loop) {
36 wlr_log(WLR_ERROR, "Unable to allocate memory for loop");
37 return NULL;
38 }
39 loop->fd_capacity = 10;
40 loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity);
41 loop->fd_events = create_list();
42 loop->timers = create_list();
43 return loop;
44}
45
46void loop_destroy(struct loop *loop) {
47 list_foreach(loop->fd_events, free);
48 list_foreach(loop->timers, free);
49 list_free(loop->fd_events);
50 list_free(loop->timers);
51 free(loop->fds);
52 free(loop);
53}
54
55void loop_poll(struct loop *loop) {
56 // Calculate next timer in ms
57 int ms = INT_MAX;
58 if (loop->timers->length) {
59 struct timespec now;
60 clock_gettime(CLOCK_MONOTONIC, &now);
61 for (int i = 0; i < loop->timers->length; ++i) {
62 struct loop_timer *timer = loop->timers->items[i];
63 int timer_ms = (timer->expiry.tv_sec - now.tv_sec) * 1000;
64 timer_ms += (timer->expiry.tv_nsec - now.tv_nsec) / 1000000;
65 if (timer_ms < ms) {
66 ms = timer_ms;
67 }
68 }
69 }
70 if (ms < 0) {
71 ms = 0;
72 }
73
74 poll(loop->fds, loop->fd_length, ms);
75
76 // Dispatch fds
77 for (int i = 0; i < loop->fd_length; ++i) {
78 struct pollfd pfd = loop->fds[i];
79 struct loop_fd_event *event = loop->fd_events->items[i];
80
81 // Always send these events
82 unsigned events = pfd.events | POLLHUP | POLLERR;
83
84 if (pfd.revents & events) {
85 event->callback(pfd.fd, pfd.revents, event->data);
86 }
87 }
88
89 // Dispatch timers
90 if (loop->timers->length) {
91 struct timespec now;
92 clock_gettime(CLOCK_MONOTONIC, &now);
93 for (int i = 0; i < loop->timers->length; ++i) {
94 struct loop_timer *timer = loop->timers->items[i];
95 bool expired = timer->expiry.tv_sec < now.tv_sec ||
96 (timer->expiry.tv_sec == now.tv_sec &&
97 timer->expiry.tv_nsec < now.tv_nsec);
98 if (expired) {
99 timer->callback(timer->data);
100 loop_remove_timer(loop, timer);
101 --i;
102 }
103 }
104 }
105}
106
107void loop_add_fd(struct loop *loop, int fd, short mask,
108 void (*callback)(int fd, short mask, void *data), void *data) {
109 struct loop_fd_event *event = calloc(1, sizeof(struct loop_fd_event));
110 if (!event) {
111 wlr_log(WLR_ERROR, "Unable to allocate memory for event");
112 return;
113 }
114 event->callback = callback;
115 event->data = data;
116 list_add(loop->fd_events, event);
117
118 struct pollfd pfd = {fd, mask, 0};
119
120 if (loop->fd_length == loop->fd_capacity) {
121 loop->fd_capacity += 10;
122 loop->fds = realloc(loop->fds,
123 sizeof(struct pollfd) * loop->fd_capacity);
124 }
125
126 loop->fds[loop->fd_length++] = pfd;
127}
128
129struct loop_timer *loop_add_timer(struct loop *loop, int ms,
130 void (*callback)(void *data), void *data) {
131 struct loop_timer *timer = calloc(1, sizeof(struct loop_timer));
132 if (!timer) {
133 wlr_log(WLR_ERROR, "Unable to allocate memory for timer");
134 return NULL;
135 }
136 timer->callback = callback;
137 timer->data = data;
138
139 clock_gettime(CLOCK_MONOTONIC, &timer->expiry);
140 timer->expiry.tv_sec += ms / 1000;
141
142 long int nsec = (ms % 1000) * 1000000;
143 if (timer->expiry.tv_nsec + nsec >= 1000000000) {
144 timer->expiry.tv_sec++;
145 nsec -= 1000000000;
146 }
147 timer->expiry.tv_nsec += nsec;
148
149 list_add(loop->timers, timer);
150
151 return timer;
152}
153
154bool loop_remove_fd(struct loop *loop, int fd) {
155 for (int i = 0; i < loop->fd_length; ++i) {
156 if (loop->fds[i].fd == fd) {
157 free(loop->fd_events->items[i]);
158 list_del(loop->fd_events, i);
159
160 loop->fd_length--;
161 memmove(&loop->fds[i], &loop->fds[i + 1],
162 sizeof(struct pollfd) * (loop->fd_length - i));
163
164 return true;
165 }
166 }
167 return false;
168}
169
170bool loop_remove_timer(struct loop *loop, struct loop_timer *timer) {
171 for (int i = 0; i < loop->timers->length; ++i) {
172 if (loop->timers->items[i] == timer) {
173 list_del(loop->timers, i);
174 free(timer);
175 return true;
176 }
177 }
178 return false;
179}