2018-10-14 02:28:38 +00:00
|
|
|
#include <limits.h>
|
2018-10-13 06:04:37 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <poll.h>
|
2018-10-14 02:28:38 +00:00
|
|
|
#include <time.h>
|
2018-10-13 06:04:37 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "loop.h"
|
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
struct loop_fd_event {
|
2018-10-13 06:04:37 +00:00
|
|
|
void (*callback)(int fd, short mask, void *data);
|
|
|
|
void *data;
|
2018-10-14 02:28:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct loop_timer {
|
|
|
|
void (*callback)(void *data);
|
|
|
|
void *data;
|
|
|
|
struct timespec expiry;
|
2018-10-13 06:04:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct loop {
|
|
|
|
struct pollfd *fds;
|
|
|
|
int fd_length;
|
|
|
|
int fd_capacity;
|
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
list_t *fd_events; // struct loop_fd_event
|
|
|
|
list_t *timers; // struct loop_timer
|
2018-10-13 06:04:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct loop *loop_create(void) {
|
|
|
|
struct loop *loop = calloc(1, sizeof(struct loop));
|
|
|
|
if (!loop) {
|
2019-01-20 18:51:12 +00:00
|
|
|
sway_log(SWAY_ERROR, "Unable to allocate memory for loop");
|
2018-10-13 06:04:37 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
loop->fd_capacity = 10;
|
|
|
|
loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity);
|
2018-10-14 02:28:38 +00:00
|
|
|
loop->fd_events = create_list();
|
|
|
|
loop->timers = create_list();
|
2018-10-13 06:04:37 +00:00
|
|
|
return loop;
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop_destroy(struct loop *loop) {
|
2018-12-09 01:15:23 +00:00
|
|
|
list_free_items_and_destroy(loop->fd_events);
|
|
|
|
list_free_items_and_destroy(loop->timers);
|
2018-10-14 13:59:51 +00:00
|
|
|
free(loop->fds);
|
2018-10-13 06:04:37 +00:00
|
|
|
free(loop);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop_poll(struct loop *loop) {
|
2018-10-14 02:28:38 +00:00
|
|
|
// Calculate next timer in ms
|
|
|
|
int ms = INT_MAX;
|
|
|
|
if (loop->timers->length) {
|
|
|
|
struct timespec now;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
for (int i = 0; i < loop->timers->length; ++i) {
|
|
|
|
struct loop_timer *timer = loop->timers->items[i];
|
|
|
|
int timer_ms = (timer->expiry.tv_sec - now.tv_sec) * 1000;
|
|
|
|
timer_ms += (timer->expiry.tv_nsec - now.tv_nsec) / 1000000;
|
|
|
|
if (timer_ms < ms) {
|
|
|
|
ms = timer_ms;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-14 13:59:51 +00:00
|
|
|
if (ms < 0) {
|
|
|
|
ms = 0;
|
|
|
|
}
|
2018-10-13 06:04:37 +00:00
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
poll(loop->fds, loop->fd_length, ms);
|
|
|
|
|
|
|
|
// Dispatch fds
|
2018-10-13 06:04:37 +00:00
|
|
|
for (int i = 0; i < loop->fd_length; ++i) {
|
|
|
|
struct pollfd pfd = loop->fds[i];
|
2018-10-14 02:28:38 +00:00
|
|
|
struct loop_fd_event *event = loop->fd_events->items[i];
|
2018-10-13 06:04:37 +00:00
|
|
|
|
|
|
|
// Always send these events
|
|
|
|
unsigned events = pfd.events | POLLHUP | POLLERR;
|
|
|
|
|
|
|
|
if (pfd.revents & events) {
|
|
|
|
event->callback(pfd.fd, pfd.revents, event->data);
|
2018-10-14 02:28:38 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-13 06:52:13 +00:00
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
// Dispatch timers
|
|
|
|
if (loop->timers->length) {
|
|
|
|
struct timespec now;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
|
|
|
for (int i = 0; i < loop->timers->length; ++i) {
|
|
|
|
struct loop_timer *timer = loop->timers->items[i];
|
|
|
|
bool expired = timer->expiry.tv_sec < now.tv_sec ||
|
|
|
|
(timer->expiry.tv_sec == now.tv_sec &&
|
|
|
|
timer->expiry.tv_nsec < now.tv_nsec);
|
|
|
|
if (expired) {
|
|
|
|
timer->callback(timer->data);
|
|
|
|
loop_remove_timer(loop, timer);
|
2018-10-13 06:52:13 +00:00
|
|
|
--i;
|
|
|
|
}
|
2018-10-13 06:04:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
void loop_add_fd(struct loop *loop, int fd, short mask,
|
2018-10-13 06:04:37 +00:00
|
|
|
void (*callback)(int fd, short mask, void *data), void *data) {
|
2018-10-14 02:28:38 +00:00
|
|
|
struct loop_fd_event *event = calloc(1, sizeof(struct loop_fd_event));
|
|
|
|
if (!event) {
|
2019-01-20 18:51:12 +00:00
|
|
|
sway_log(SWAY_ERROR, "Unable to allocate memory for event");
|
2018-10-14 02:28:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
event->callback = callback;
|
|
|
|
event->data = data;
|
|
|
|
list_add(loop->fd_events, event);
|
|
|
|
|
2018-10-13 06:04:37 +00:00
|
|
|
struct pollfd pfd = {fd, mask, 0};
|
|
|
|
|
|
|
|
if (loop->fd_length == loop->fd_capacity) {
|
2020-06-15 13:46:58 +00:00
|
|
|
int capacity = loop->fd_capacity + 10;
|
|
|
|
struct pollfd *tmp = realloc(loop->fds,
|
|
|
|
sizeof(struct pollfd) * capacity);
|
|
|
|
if (!tmp) {
|
|
|
|
sway_log(SWAY_ERROR, "Unable to allocate memory for pollfd");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
loop->fds = tmp;
|
|
|
|
loop->fd_capacity = capacity;
|
2018-10-13 06:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loop->fds[loop->fd_length++] = pfd;
|
2018-10-14 02:28:38 +00:00
|
|
|
}
|
2018-10-13 06:04:37 +00:00
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
struct loop_timer *loop_add_timer(struct loop *loop, int ms,
|
|
|
|
void (*callback)(void *data), void *data) {
|
|
|
|
struct loop_timer *timer = calloc(1, sizeof(struct loop_timer));
|
|
|
|
if (!timer) {
|
2019-01-20 18:51:12 +00:00
|
|
|
sway_log(SWAY_ERROR, "Unable to allocate memory for timer");
|
2018-10-14 02:28:38 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
timer->callback = callback;
|
|
|
|
timer->data = data;
|
2018-10-13 06:04:37 +00:00
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC, &timer->expiry);
|
|
|
|
timer->expiry.tv_sec += ms / 1000;
|
2018-10-13 06:04:37 +00:00
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
long int nsec = (ms % 1000) * 1000000;
|
|
|
|
if (timer->expiry.tv_nsec + nsec >= 1000000000) {
|
|
|
|
timer->expiry.tv_sec++;
|
|
|
|
nsec -= 1000000000;
|
|
|
|
}
|
|
|
|
timer->expiry.tv_nsec += nsec;
|
2018-10-13 06:04:37 +00:00
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
list_add(loop->timers, timer);
|
2018-10-13 06:04:37 +00:00
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
return timer;
|
|
|
|
}
|
2018-10-13 06:04:37 +00:00
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
bool loop_remove_fd(struct loop *loop, int fd) {
|
|
|
|
for (int i = 0; i < loop->fd_length; ++i) {
|
|
|
|
if (loop->fds[i].fd == fd) {
|
|
|
|
free(loop->fd_events->items[i]);
|
|
|
|
list_del(loop->fd_events, i);
|
2018-10-13 06:52:13 +00:00
|
|
|
|
2018-10-13 06:04:37 +00:00
|
|
|
loop->fd_length--;
|
2018-10-14 02:28:38 +00:00
|
|
|
memmove(&loop->fds[i], &loop->fds[i + 1],
|
2018-10-14 14:23:53 +00:00
|
|
|
sizeof(struct pollfd) * (loop->fd_length - i));
|
2018-10-14 02:28:38 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-13 06:04:37 +00:00
|
|
|
|
2018-10-14 02:28:38 +00:00
|
|
|
bool loop_remove_timer(struct loop *loop, struct loop_timer *timer) {
|
|
|
|
for (int i = 0; i < loop->timers->length; ++i) {
|
|
|
|
if (loop->timers->items[i] == timer) {
|
|
|
|
list_del(loop->timers, i);
|
|
|
|
free(timer);
|
2018-10-13 06:04:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|