Remove list.c
This replaces all occurrences of `list_t` with `wl_list` to eliminate the need for `list.c`
This commit is contained in:
parent
56b672a16c
commit
dff6b63725
@ -1,21 +0,0 @@
|
|||||||
#ifndef _SWAY_LIST_H
|
|
||||||
#define _SWAY_LIST_H
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int capacity;
|
|
||||||
int length;
|
|
||||||
void **items;
|
|
||||||
} list_t;
|
|
||||||
|
|
||||||
list_t *create_list(void);
|
|
||||||
void list_free(list_t *list);
|
|
||||||
void list_add(list_t *list, void *item);
|
|
||||||
void list_insert(list_t *list, int index, void *item);
|
|
||||||
void list_del(list_t *list, int index);
|
|
||||||
|
|
||||||
/* Calls `free` for each item in the list, then frees the list.
|
|
||||||
* Do not use this to free lists of primitives or items that require more
|
|
||||||
* complicated deallocation code.
|
|
||||||
*/
|
|
||||||
void list_free_items_and_destroy(list_t *list);
|
|
||||||
#endif
|
|
59
list.c
59
list.c
@ -1,59 +0,0 @@
|
|||||||
#include "list.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
list_t *create_list(void) {
|
|
||||||
list_t *list = malloc(sizeof(list_t));
|
|
||||||
if (!list) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
list->capacity = 10;
|
|
||||||
list->length = 0;
|
|
||||||
list->items = malloc(sizeof(void*) * list->capacity);
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void list_resize(list_t *list) {
|
|
||||||
if (list->length == list->capacity) {
|
|
||||||
list->capacity *= 2;
|
|
||||||
list->items = realloc(list->items, sizeof(void*) * list->capacity);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void list_free(list_t *list) {
|
|
||||||
if (list == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
free(list->items);
|
|
||||||
free(list);
|
|
||||||
}
|
|
||||||
|
|
||||||
void list_add(list_t *list, void *item) {
|
|
||||||
list_resize(list);
|
|
||||||
list->items[list->length++] = item;
|
|
||||||
}
|
|
||||||
|
|
||||||
void list_insert(list_t *list, int index, void *item) {
|
|
||||||
list_resize(list);
|
|
||||||
memmove(&list->items[index + 1], &list->items[index], sizeof(void*) * (list->length - index));
|
|
||||||
list->length++;
|
|
||||||
list->items[index] = item;
|
|
||||||
}
|
|
||||||
|
|
||||||
void list_del(list_t *list, int index) {
|
|
||||||
list->length--;
|
|
||||||
memmove(&list->items[index], &list->items[index + 1], sizeof(void*) * (list->length - index));
|
|
||||||
}
|
|
||||||
|
|
||||||
void list_free_items_and_destroy(list_t *list) {
|
|
||||||
if (!list) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < list->length; ++i) {
|
|
||||||
free(list->items[i]);
|
|
||||||
}
|
|
||||||
list_free(list);
|
|
||||||
}
|
|
||||||
|
|
75
loop.c
75
loop.c
@ -7,19 +7,21 @@
|
|||||||
#include <poll.h>
|
#include <poll.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "list.h"
|
#include <wayland-client.h>
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "loop.h"
|
#include "loop.h"
|
||||||
|
|
||||||
struct loop_fd_event {
|
struct loop_fd_event {
|
||||||
void (*callback)(int fd, short mask, void *data);
|
void (*callback)(int fd, short mask, void *data);
|
||||||
void *data;
|
void *data;
|
||||||
|
struct wl_list link; // struct loop_fd_event::link
|
||||||
};
|
};
|
||||||
|
|
||||||
struct loop_timer {
|
struct loop_timer {
|
||||||
void (*callback)(void *data);
|
void (*callback)(void *data);
|
||||||
void *data;
|
void *data;
|
||||||
struct timespec expiry;
|
struct timespec expiry;
|
||||||
|
struct wl_list link; // struct loop_timer::link
|
||||||
};
|
};
|
||||||
|
|
||||||
struct loop {
|
struct loop {
|
||||||
@ -27,8 +29,8 @@ struct loop {
|
|||||||
int fd_length;
|
int fd_length;
|
||||||
int fd_capacity;
|
int fd_capacity;
|
||||||
|
|
||||||
list_t *fd_events; // struct loop_fd_event
|
struct wl_list fd_events; // struct loop_fd_event::link
|
||||||
list_t *timers; // struct loop_timer
|
struct wl_list timers; // struct loop_timer::link
|
||||||
};
|
};
|
||||||
|
|
||||||
struct loop *loop_create(void) {
|
struct loop *loop_create(void) {
|
||||||
@ -39,14 +41,22 @@ struct loop *loop_create(void) {
|
|||||||
}
|
}
|
||||||
loop->fd_capacity = 10;
|
loop->fd_capacity = 10;
|
||||||
loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity);
|
loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity);
|
||||||
loop->fd_events = create_list();
|
wl_list_init(&loop->fd_events);
|
||||||
loop->timers = create_list();
|
wl_list_init(&loop->timers);
|
||||||
return loop;
|
return loop;
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop_destroy(struct loop *loop) {
|
void loop_destroy(struct loop *loop) {
|
||||||
list_free_items_and_destroy(loop->fd_events);
|
struct loop_fd_event *event = NULL, *tmp_event = NULL;
|
||||||
list_free_items_and_destroy(loop->timers);
|
wl_list_for_each_safe(event, tmp_event, &loop->fd_events, link) {
|
||||||
|
wl_list_remove(&event->link);
|
||||||
|
free(event);
|
||||||
|
}
|
||||||
|
struct loop_timer *timer = NULL, *tmp_timer = NULL;
|
||||||
|
wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) {
|
||||||
|
wl_list_remove(&timer->link);
|
||||||
|
free(timer);
|
||||||
|
}
|
||||||
free(loop->fds);
|
free(loop->fds);
|
||||||
free(loop);
|
free(loop);
|
||||||
}
|
}
|
||||||
@ -54,11 +64,11 @@ void loop_destroy(struct loop *loop) {
|
|||||||
void loop_poll(struct loop *loop) {
|
void loop_poll(struct loop *loop) {
|
||||||
// Calculate next timer in ms
|
// Calculate next timer in ms
|
||||||
int ms = INT_MAX;
|
int ms = INT_MAX;
|
||||||
if (loop->timers->length) {
|
if (!wl_list_empty(&loop->timers)) {
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||||
for (int i = 0; i < loop->timers->length; ++i) {
|
struct loop_timer *timer = NULL;
|
||||||
struct loop_timer *timer = loop->timers->items[i];
|
wl_list_for_each(timer, &loop->timers, link) {
|
||||||
int timer_ms = (timer->expiry.tv_sec - now.tv_sec) * 1000;
|
int timer_ms = (timer->expiry.tv_sec - now.tv_sec) * 1000;
|
||||||
timer_ms += (timer->expiry.tv_nsec - now.tv_nsec) / 1000000;
|
timer_ms += (timer->expiry.tv_nsec - now.tv_nsec) / 1000000;
|
||||||
if (timer_ms < ms) {
|
if (timer_ms < ms) {
|
||||||
@ -73,9 +83,10 @@ void loop_poll(struct loop *loop) {
|
|||||||
poll(loop->fds, loop->fd_length, ms);
|
poll(loop->fds, loop->fd_length, ms);
|
||||||
|
|
||||||
// Dispatch fds
|
// Dispatch fds
|
||||||
for (int i = 0; i < loop->fd_length; ++i) {
|
size_t fd_index = 0;
|
||||||
struct pollfd pfd = loop->fds[i];
|
struct loop_fd_event *event = NULL;
|
||||||
struct loop_fd_event *event = loop->fd_events->items[i];
|
wl_list_for_each(event, &loop->fd_events, link) {
|
||||||
|
struct pollfd pfd = loop->fds[fd_index];
|
||||||
|
|
||||||
// Always send these events
|
// Always send these events
|
||||||
unsigned events = pfd.events | POLLHUP | POLLERR;
|
unsigned events = pfd.events | POLLHUP | POLLERR;
|
||||||
@ -83,21 +94,22 @@ void loop_poll(struct loop *loop) {
|
|||||||
if (pfd.revents & events) {
|
if (pfd.revents & events) {
|
||||||
event->callback(pfd.fd, pfd.revents, event->data);
|
event->callback(pfd.fd, pfd.revents, event->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
++fd_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Dispatch timers
|
// Dispatch timers
|
||||||
if (loop->timers->length) {
|
if (!wl_list_empty(&loop->timers)) {
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||||
for (int i = 0; i < loop->timers->length; ++i) {
|
struct loop_timer *timer = NULL, *tmp_timer = NULL;
|
||||||
struct loop_timer *timer = loop->timers->items[i];
|
wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) {
|
||||||
bool expired = timer->expiry.tv_sec < now.tv_sec ||
|
bool expired = timer->expiry.tv_sec < now.tv_sec ||
|
||||||
(timer->expiry.tv_sec == now.tv_sec &&
|
(timer->expiry.tv_sec == now.tv_sec &&
|
||||||
timer->expiry.tv_nsec < now.tv_nsec);
|
timer->expiry.tv_nsec < now.tv_nsec);
|
||||||
if (expired) {
|
if (expired) {
|
||||||
timer->callback(timer->data);
|
timer->callback(timer->data);
|
||||||
loop_remove_timer(loop, timer);
|
loop_remove_timer(loop, timer);
|
||||||
--i;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -112,7 +124,7 @@ void loop_add_fd(struct loop *loop, int fd, short mask,
|
|||||||
}
|
}
|
||||||
event->callback = callback;
|
event->callback = callback;
|
||||||
event->data = data;
|
event->data = data;
|
||||||
list_add(loop->fd_events, event);
|
wl_list_insert(&loop->fd_events, &event->link);
|
||||||
|
|
||||||
struct pollfd pfd = {fd, mask, 0};
|
struct pollfd pfd = {fd, mask, 0};
|
||||||
|
|
||||||
@ -145,31 +157,34 @@ struct loop_timer *loop_add_timer(struct loop *loop, int ms,
|
|||||||
}
|
}
|
||||||
timer->expiry.tv_nsec += nsec;
|
timer->expiry.tv_nsec += nsec;
|
||||||
|
|
||||||
list_add(loop->timers, timer);
|
wl_list_insert(&loop->timers, &timer->link);
|
||||||
|
|
||||||
return timer;
|
return timer;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool loop_remove_fd(struct loop *loop, int fd) {
|
bool loop_remove_fd(struct loop *loop, int fd) {
|
||||||
for (int i = 0; i < loop->fd_length; ++i) {
|
size_t fd_index = 0;
|
||||||
if (loop->fds[i].fd == fd) {
|
struct loop_fd_event *event = NULL, *tmp_event = NULL;
|
||||||
free(loop->fd_events->items[i]);
|
wl_list_for_each_safe(event, tmp_event, &loop->fd_events, link) {
|
||||||
list_del(loop->fd_events, i);
|
if (loop->fds[fd_index].fd == fd) {
|
||||||
|
wl_list_remove(&event->link);
|
||||||
|
free(event);
|
||||||
|
|
||||||
loop->fd_length--;
|
loop->fd_length--;
|
||||||
memmove(&loop->fds[i], &loop->fds[i + 1],
|
memmove(&loop->fds[fd_index], &loop->fds[fd_index + 1],
|
||||||
sizeof(struct pollfd) * (loop->fd_length - i));
|
sizeof(struct pollfd) * (loop->fd_length - fd_index));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
++fd_index;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool loop_remove_timer(struct loop *loop, struct loop_timer *timer) {
|
bool loop_remove_timer(struct loop *loop, struct loop_timer *remove) {
|
||||||
for (int i = 0; i < loop->timers->length; ++i) {
|
struct loop_timer *timer = NULL, *tmp_timer = NULL;
|
||||||
if (loop->timers->items[i] == timer) {
|
wl_list_for_each_safe(timer, tmp_timer, &loop->timers, link) {
|
||||||
list_del(loop->timers, i);
|
if (timer == remove) {
|
||||||
|
wl_list_remove(&timer->link);
|
||||||
free(timer);
|
free(timer);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -127,7 +127,6 @@ dependencies = [
|
|||||||
sources = [
|
sources = [
|
||||||
'background-image.c',
|
'background-image.c',
|
||||||
'cairo.c',
|
'cairo.c',
|
||||||
'list.c',
|
|
||||||
'log.c',
|
'log.c',
|
||||||
'loop.c',
|
'loop.c',
|
||||||
'main.c',
|
'main.c',
|
||||||
|
Loading…
Reference in New Issue
Block a user