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