* The loop functions are now prefixed with `loop_`. * It is now easy to add timers to the loop. * Timers are implemented using pollfd and timerfd, rather than manually checking them when any other event happens to arrive.master
parent
7f2e6d812a
commit
4056c09e13
@ -0,0 +1,105 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <poll.h>
|
||||
#include <sys/timerfd.h>
|
||||
#include <unistd.h>
|
||||
#include "list.h"
|
||||
#include "log.h"
|
||||
#include "loop.h"
|
||||
|
||||
struct loop_event {
|
||||
void (*callback)(int fd, short mask, void *data);
|
||||
void *data;
|
||||
};
|
||||
|
||||
struct loop {
|
||||
struct pollfd *fds;
|
||||
int fd_length;
|
||||
int fd_capacity;
|
||||
|
||||
list_t *events; // struct loop_event
|
||||
};
|
||||
|
||||
struct loop *loop_create(void) {
|
||||
struct loop *loop = calloc(1, sizeof(struct loop));
|
||||
if (!loop) {
|
||||
wlr_log(WLR_ERROR, "Unable to allocate memory for loop");
|
||||
return NULL;
|
||||
}
|
||||
loop->fd_capacity = 10;
|
||||
loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity);
|
||||
loop->events = create_list();
|
||||
return loop;
|
||||
}
|
||||
|
||||
void loop_destroy(struct loop *loop) {
|
||||
list_foreach(loop->events, free);
|
||||
list_free(loop->events);
|
||||
free(loop);
|
||||
}
|
||||
|
||||
void loop_poll(struct loop *loop) {
|
||||
poll(loop->fds, loop->fd_length, -1);
|
||||
|
||||
for (int i = 0; i < loop->fd_length; ++i) {
|
||||
struct pollfd pfd = loop->fds[i];
|
||||
struct loop_event *event = loop->events->items[i];
|
||||
|
||||
// Always send these events
|
||||
unsigned events = pfd.events | POLLHUP | POLLERR;
|
||||
|
||||
if (pfd.revents & events) {
|
||||
event->callback(pfd.fd, pfd.revents, event->data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct loop_event *loop_add_fd(struct loop *loop, int fd, short mask,
|
||||
void (*callback)(int fd, short mask, void *data), void *data) {
|
||||
struct pollfd pfd = {fd, mask, 0};
|
||||
|
||||
if (loop->fd_length == loop->fd_capacity) {
|
||||
loop->fd_capacity += 10;
|
||||
loop->fds = realloc(loop->fds, sizeof(struct pollfd) * loop->fd_capacity);
|
||||
}
|
||||
|
||||
loop->fds[loop->fd_length++] = pfd;
|
||||
|
||||
struct loop_event *event = calloc(1, sizeof(struct loop_event));
|
||||
event->callback = callback;
|
||||
event->data = data;
|
||||
|
||||
list_add(loop->events, event);
|
||||
|
||||
return event;
|
||||
}
|
||||
|
||||
struct loop_event *loop_add_timer(struct loop *loop, int ms,
|
||||
void (*callback)(int fd, short mask, void *data), void *data) {
|
||||
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
|
||||
struct itimerspec its;
|
||||
its.it_interval.tv_sec = 0;
|
||||
its.it_interval.tv_nsec = 0;
|
||||
its.it_value.tv_sec = ms / 1000000000;
|
||||
its.it_value.tv_nsec = (ms * 1000000) % 1000000000;
|
||||
timerfd_settime(fd, 0, &its, NULL);
|
||||
|
||||
return loop_add_fd(loop, fd, POLLIN, callback, data);
|
||||
}
|
||||
|
||||
bool loop_remove_event(struct loop *loop, struct loop_event *event) {
|
||||
for (int i = 0; i < loop->events->length; ++i) {
|
||||
if (loop->events->items[i] == event) {
|
||||
list_del(loop->events, i);
|
||||
|
||||
loop->fd_length--;
|
||||
memmove(&loop->fds[i], &loop->fds[i + 1], sizeof(void*) * (loop->fd_length - i));
|
||||
|
||||
free(event);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
#ifndef _SWAY_LOOP_H
|
||||
#define _SWAY_LOOP_H
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
* This is an event loop system designed for sway clients, not sway itself.
|
||||
*
|
||||
* It uses pollfds to block on multiple file descriptors at once, and provides
|
||||
* an easy way to set timers. Typically the Wayland display's fd will be one of
|
||||
* the fds in the loop.
|
||||
*/
|
||||
|
||||
struct loop;
|
||||
|
||||
/**
|
||||
* Create an event loop.
|
||||
*/
|
||||
struct loop *loop_create(void);
|
||||
|
||||
/**
|
||||
* Destroy the event loop (eg. on program termination).
|
||||
*/
|
||||
void loop_destroy(struct loop *loop);
|
||||
|
||||
/**
|
||||
* Poll the event loop. This will block until one of the fds has data.
|
||||
*/
|
||||
void loop_poll(struct loop *loop);
|
||||
|
||||
/**
|
||||
* Add an fd to the loop.
|
||||
*/
|
||||
struct loop_event *loop_add_fd(struct loop *loop, int fd, short mask,
|
||||
void (*func)(int fd, short mask, void *data), void *data);
|
||||
|
||||
/**
|
||||
* Add a timer to the loop.
|
||||
*/
|
||||
struct loop_event *loop_add_timer(struct loop *loop, int ms,
|
||||
void (*callback)(int fd, short mask, void *data), void *data);
|
||||
|
||||
/**
|
||||
* Remove an event from the loop.
|
||||
*/
|
||||
bool loop_remove_event(struct loop *loop, struct loop_event *event);
|
||||
|
||||
#endif
|
@ -1,26 +0,0 @@
|
||||
#ifndef _SWAYBAR_EVENT_LOOP_H
|
||||
#define _SWAYBAR_EVENT_LOOP_H
|
||||
#include <stdbool.h>
|
||||
#include <time.h>
|
||||
|
||||
void add_event(int fd, short mask,
|
||||
void(*cb)(int fd, short mask, void *data),
|
||||
void *data);
|
||||
|
||||
// Not guaranteed to notify cb immediately
|
||||
void add_timer(timer_t timer,
|
||||
void(*cb)(timer_t timer, void *data),
|
||||
void *data);
|
||||
|
||||
// Returns false if nothing exists, true otherwise
|
||||
bool remove_event(int fd);
|
||||
|
||||
// Returns false if nothing exists, true otherwise
|
||||
bool remove_timer(timer_t timer);
|
||||
|
||||
// Blocks and returns after sending callbacks
|
||||
void event_loop_poll(void);
|
||||
|
||||
void init_event_loop(void);
|
||||
|
||||
#endif
|
@ -1,156 +0,0 @@
|
||||
#define _XOPEN_SOURCE 700
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <poll.h>
|
||||
#include <time.h>
|
||||
#include "swaybar/event_loop.h"
|
||||
#include "list.h"
|
||||
|
||||
struct event_item {
|
||||
void (*cb)(int fd, short mask, void *data);
|
||||
void *data;
|
||||
};
|
||||
|
||||
struct timer_item {
|
||||
timer_t timer;
|
||||
void (*cb)(timer_t timer, void *data);
|
||||
void *data;
|
||||
};
|
||||
|
||||
static struct {
|
||||
// The order of each must be kept consistent
|
||||
struct { /* pollfd array */
|
||||
struct pollfd *items;
|
||||
int capacity;
|
||||
int length;
|
||||
} fds;
|
||||
list_t *items; /* event_item list */
|
||||
|
||||
// Timer list
|
||||
list_t *timers;
|
||||
} event_loop;
|
||||
|
||||
void add_timer(timer_t timer,
|
||||
void(*cb)(timer_t timer, void *data),
|
||||
void *data) {
|
||||
|
||||
struct timer_item *item = malloc(sizeof(struct timer_item));
|
||||
item->timer = timer;
|
||||
item->cb = cb;
|
||||
item->data = data;
|
||||
|
||||
list_add(event_loop.timers, item);
|
||||
}
|
||||
|
||||
void add_event(int fd, short mask,
|
||||
void(*cb)(int fd, short mask, void *data), void *data) {
|
||||
|
||||
struct pollfd pollfd = {
|
||||
fd,
|
||||
mask,
|
||||
0,
|
||||
};
|
||||
|
||||
// Resize
|
||||
if (event_loop.fds.length == event_loop.fds.capacity) {
|
||||
event_loop.fds.capacity += 10;
|
||||
event_loop.fds.items = realloc(event_loop.fds.items,
|
||||
sizeof(struct pollfd) * event_loop.fds.capacity);
|
||||
}
|
||||
|
||||
event_loop.fds.items[event_loop.fds.length++] = pollfd;
|
||||
|
||||
struct event_item *item = malloc(sizeof(struct event_item));
|
||||
item->cb = cb;
|
||||
item->data = data;
|
||||
|
||||
list_add(event_loop.items, item);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
bool remove_event(int fd) {
|
||||
/*
|
||||
* Instead of removing events immediately, we mark them for deletion
|
||||
* and clean them up later. This is so we can call remove_event inside
|
||||
* an event callback safely.
|
||||
*/
|
||||
for (int i = 0; i < event_loop.fds.length; ++i) {
|
||||
if (event_loop.fds.items[i].fd == fd) {
|
||||
event_loop.fds.items[i].fd = -1;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int timer_item_timer_cmp(const void *_timer_item, const void *_timer) {
|
||||
const struct timer_item *timer_item = _timer_item;
|
||||
const timer_t *timer = _timer;
|
||||
if (timer_item->timer == *timer) {
|
||||
return 0;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
bool remove_timer(timer_t timer) {
|
||||
int index = list_seq_find(event_loop.timers, timer_item_timer_cmp, &timer);
|
||||
if (index != -1) {
|
||||
free(event_loop.timers->items[index]);
|
||||
list_del(event_loop.timers, index);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void event_loop_poll(void) {
|
||||
poll(event_loop.fds.items, event_loop.fds.length, -1);
|
||||
|
||||
for (int i = 0; i < event_loop.fds.length; ++i) {
|
||||
struct pollfd pfd = event_loop.fds.items[i];
|
||||
struct event_item *item = (struct event_item *)event_loop.items->items[i];
|
||||
|
||||
// Always send these events
|
||||
unsigned events = pfd.events | POLLHUP | POLLERR;
|
||||
|
||||
if (pfd.revents & events) {
|
||||
item->cb(pfd.fd, pfd.revents, item->data);
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup removed events
|
||||
int end = 0;
|
||||
int length = event_loop.fds.length;
|
||||
for (int i = 0; i < length; ++i) {
|
||||
if (event_loop.fds.items[i].fd == -1) {
|
||||
free(event_loop.items->items[i]);
|
||||
list_del(event_loop.items, i);
|
||||
--event_loop.fds.length;
|
||||
} else if (end != i) {
|
||||
event_loop.fds.items[end++] = event_loop.fds.items[i];
|
||||
} else {
|
||||
end = i + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// check timers
|
||||
// not tested, but seems to work
|
||||
for (int i = 0; i < event_loop.timers->length; ++i) {
|
||||
struct timer_item *item = event_loop.timers->items[i];
|
||||
int overrun = timer_getoverrun(item->timer);
|
||||
if (overrun && overrun != -1) {
|
||||
item->cb(item->timer, item->data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void init_event_loop(void) {
|
||||
event_loop.fds.length = 0;
|
||||
event_loop.fds.capacity = 10;
|
||||
event_loop.fds.items = malloc(
|
||||
event_loop.fds.capacity * sizeof(struct pollfd));
|
||||
event_loop.items = create_list();
|
||||
event_loop.timers = create_list();
|
||||
}
|
Loading…
Reference in new issue