From 4056c09e13c1aeead6dd4085fc7e263a17a0b195 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 13 Oct 2018 16:04:37 +1000 Subject: Move swaybar's event loop to common directory and refactor * 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. --- include/loop.h | 47 +++++++++++++++++++++++++++++++++++++++++++ include/swaybar/bar.h | 5 +++++ include/swaybar/event_loop.h | 26 ------------------------ include/swaybar/status_line.h | 2 ++ 4 files changed, 54 insertions(+), 26 deletions(-) create mode 100644 include/loop.h delete mode 100644 include/swaybar/event_loop.h (limited to 'include') diff --git a/include/loop.h b/include/loop.h new file mode 100644 index 00000000..7c151785 --- /dev/null +++ b/include/loop.h @@ -0,0 +1,47 @@ +#ifndef _SWAY_LOOP_H +#define _SWAY_LOOP_H +#include + +/** + * 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 diff --git a/include/swaybar/bar.h b/include/swaybar/bar.h index 9ff3fe7b..d61da5dc 100644 --- a/include/swaybar/bar.h +++ b/include/swaybar/bar.h @@ -8,6 +8,8 @@ struct swaybar_config; struct swaybar_output; struct swaybar_workspace; +struct loop; +struct loop_event; struct swaybar_pointer { struct wl_pointer *pointer; @@ -66,6 +68,9 @@ struct swaybar { struct swaybar_pointer pointer; struct status_line *status; + struct loop *eventloop; + struct loop_event *status_event; + int ipc_event_socketfd; int ipc_socketfd; diff --git a/include/swaybar/event_loop.h b/include/swaybar/event_loop.h deleted file mode 100644 index 47be5b79..00000000 --- a/include/swaybar/event_loop.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef _SWAYBAR_EVENT_LOOP_H -#define _SWAYBAR_EVENT_LOOP_H -#include -#include - -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 diff --git a/include/swaybar/status_line.h b/include/swaybar/status_line.h index 5e7e8771..957a808e 100644 --- a/include/swaybar/status_line.h +++ b/include/swaybar/status_line.h @@ -14,6 +14,8 @@ enum status_protocol { }; struct status_line { + struct swaybar *bar; + pid_t pid; int read_fd, write_fd; FILE *read, *write; -- cgit v1.2.3-54-g00ecf From 9c833c661ac9e061bf4d666065e3090639f481ff Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 13 Oct 2018 16:14:35 +1000 Subject: swaylock: Use common event loop --- include/swaylock/swaylock.h | 1 + swaylock/main.c | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/swaylock/swaylock.h b/include/swaylock/swaylock.h index fbdd42a8..c8732cb7 100644 --- a/include/swaylock/swaylock.h +++ b/include/swaylock/swaylock.h @@ -54,6 +54,7 @@ struct swaylock_password { }; struct swaylock_state { + struct loop *eventloop; struct wl_display *display; struct wl_compositor *compositor; struct zwlr_layer_shell_v1 *layer_shell; diff --git a/swaylock/main.c b/swaylock/main.c index d1384c6f..27bcfe32 100644 --- a/swaylock/main.c +++ b/swaylock/main.c @@ -21,6 +21,7 @@ #include "pool-buffer.h" #include "cairo.h" #include "log.h" +#include "loop.h" #include "readline.h" #include "stringop.h" #include "util.h" @@ -844,6 +845,10 @@ static int load_config(char *path, struct swaylock_state *state, static struct swaylock_state state; +static void display_in(int fd, short mask, void *data) { + wl_display_dispatch(state.display); +} + int main(int argc, char **argv) { wlr_log_init(WLR_DEBUG, NULL); initialize_pw_backend(); @@ -946,9 +951,14 @@ int main(int argc, char **argv) { daemonize(); } + state.eventloop = loop_create(); + loop_add_fd(state.eventloop, wl_display_get_fd(state.display), POLL_IN, + display_in, NULL); + state.run_display = true; - while (wl_display_dispatch(state.display) != -1 && state.run_display) { - // This space intentionally left blank + while (state.run_display) { + wl_display_flush(state.display); + loop_poll(state.eventloop); } free(state.args.font); -- cgit v1.2.3-54-g00ecf From c242712262c5cb751fc0f89050a7f7a24433b105 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 13 Oct 2018 16:52:13 +1000 Subject: swaylock: Remove indicator after 3 seconds --- common/loop.c | 19 ++++++++++++++++--- include/swaylock/swaylock.h | 1 + swaylock/password.c | 22 ++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/common/loop.c b/common/loop.c index bfbfd5a6..da3c2142 100644 --- a/common/loop.c +++ b/common/loop.c @@ -12,6 +12,7 @@ struct loop_event { void (*callback)(int fd, short mask, void *data); void *data; + bool is_timer; }; struct loop { @@ -52,6 +53,11 @@ void loop_poll(struct loop *loop) { if (pfd.revents & events) { event->callback(pfd.fd, pfd.revents, event->data); + + if (event->is_timer) { + loop_remove_event(loop, event); + --i; + } } } } @@ -82,11 +88,14 @@ struct loop_event *loop_add_timer(struct loop *loop, int ms, 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; + its.it_value.tv_sec = ms / 1000; + its.it_value.tv_nsec = (ms % 1000) * 1000000; timerfd_settime(fd, 0, &its, NULL); - return loop_add_fd(loop, fd, POLLIN, callback, data); + struct loop_event *event = loop_add_fd(loop, fd, POLLIN, callback, data); + event->is_timer = true; + + return event; } bool loop_remove_event(struct loop *loop, struct loop_event *event) { @@ -94,6 +103,10 @@ bool loop_remove_event(struct loop *loop, struct loop_event *event) { if (loop->events->items[i] == event) { list_del(loop->events, i); + if (event->is_timer) { + close(loop->fds[i].fd); + } + loop->fd_length--; memmove(&loop->fds[i], &loop->fds[i + 1], sizeof(void*) * (loop->fd_length - i)); diff --git a/include/swaylock/swaylock.h b/include/swaylock/swaylock.h index c8732cb7..792d34cf 100644 --- a/include/swaylock/swaylock.h +++ b/include/swaylock/swaylock.h @@ -55,6 +55,7 @@ struct swaylock_password { struct swaylock_state { struct loop *eventloop; + struct loop_event *clear_indicator_timer; // clears the indicator struct wl_display *display; struct wl_compositor *compositor; struct zwlr_layer_shell_v1 *layer_shell; diff --git a/swaylock/password.c b/swaylock/password.c index 50b81f6b..fb610bf2 100644 --- a/swaylock/password.c +++ b/swaylock/password.c @@ -8,6 +8,7 @@ #include #include "swaylock/swaylock.h" #include "swaylock/seat.h" +#include "loop.h" #include "unicode.h" void clear_password_buffer(struct swaylock_password *pw) { @@ -39,6 +40,21 @@ static void append_ch(struct swaylock_password *pw, uint32_t codepoint) { pw->len += utf8_size; } +static void clear_indicator(int fd, short mask, void *data) { + struct swaylock_state *state = data; + state->clear_indicator_timer = NULL; + state->auth_state = AUTH_STATE_IDLE; + damage_state(state); +} + +static void schedule_indicator_clear(struct swaylock_state *state) { + if (state->clear_indicator_timer) { + loop_remove_event(state->eventloop, state->clear_indicator_timer); + } + state->clear_indicator_timer = loop_add_timer( + state->eventloop, 3000, clear_indicator, state); +} + void swaylock_handle_key(struct swaylock_state *state, xkb_keysym_t keysym, uint32_t codepoint) { switch (keysym) { @@ -79,11 +95,13 @@ void swaylock_handle_key(struct swaylock_state *state, state->auth_state = AUTH_STATE_CLEAR; } damage_state(state); + schedule_indicator_clear(state); break; case XKB_KEY_Escape: clear_password_buffer(&state->password); state->auth_state = AUTH_STATE_CLEAR; damage_state(state); + schedule_indicator_clear(state); break; case XKB_KEY_Caps_Lock: /* The state is getting active after this @@ -91,6 +109,7 @@ void swaylock_handle_key(struct swaylock_state *state, state->xkb.caps_lock = !state->xkb.caps_lock; state->auth_state = AUTH_STATE_INPUT_NOP; damage_state(state); + schedule_indicator_clear(state); break; case XKB_KEY_Shift_L: case XKB_KEY_Shift_R: @@ -104,12 +123,14 @@ void swaylock_handle_key(struct swaylock_state *state, case XKB_KEY_Super_R: state->auth_state = AUTH_STATE_INPUT_NOP; damage_state(state); + schedule_indicator_clear(state); break; case XKB_KEY_u: if (state->xkb.control) { clear_password_buffer(&state->password); state->auth_state = AUTH_STATE_CLEAR; damage_state(state); + schedule_indicator_clear(state); break; } // fallthrough @@ -118,6 +139,7 @@ void swaylock_handle_key(struct swaylock_state *state, append_ch(&state->password, codepoint); state->auth_state = AUTH_STATE_INPUT; damage_state(state); + schedule_indicator_clear(state); } break; } -- cgit v1.2.3-54-g00ecf From fa11b7f7012d153ba9728fe718ffc1a2650d6e98 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 13 Oct 2018 16:56:35 +1000 Subject: swaylock: clear password after 10 seconds --- include/swaylock/swaylock.h | 1 + swaylock/password.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) (limited to 'include') diff --git a/include/swaylock/swaylock.h b/include/swaylock/swaylock.h index 792d34cf..ae042c4f 100644 --- a/include/swaylock/swaylock.h +++ b/include/swaylock/swaylock.h @@ -56,6 +56,7 @@ struct swaylock_password { struct swaylock_state { struct loop *eventloop; struct loop_event *clear_indicator_timer; // clears the indicator + struct loop_event *clear_password_timer; // clears the password buffer struct wl_display *display; struct wl_compositor *compositor; struct zwlr_layer_shell_v1 *layer_shell; diff --git a/swaylock/password.c b/swaylock/password.c index fb610bf2..5ab50042 100644 --- a/swaylock/password.c +++ b/swaylock/password.c @@ -55,6 +55,23 @@ static void schedule_indicator_clear(struct swaylock_state *state) { state->eventloop, 3000, clear_indicator, state); } +static void clear_password(int fd, short mask, void *data) { + struct swaylock_state *state = data; + state->clear_password_timer = NULL; + state->auth_state = AUTH_STATE_CLEAR; + clear_password_buffer(&state->password); + damage_state(state); + schedule_indicator_clear(state); +} + +static void schedule_password_clear(struct swaylock_state *state) { + if (state->clear_password_timer) { + loop_remove_event(state->eventloop, state->clear_password_timer); + } + state->clear_password_timer = loop_add_timer( + state->eventloop, 10000, clear_password, state); +} + void swaylock_handle_key(struct swaylock_state *state, xkb_keysym_t keysym, uint32_t codepoint) { switch (keysym) { @@ -96,6 +113,7 @@ void swaylock_handle_key(struct swaylock_state *state, } damage_state(state); schedule_indicator_clear(state); + schedule_password_clear(state); break; case XKB_KEY_Escape: clear_password_buffer(&state->password); @@ -110,6 +128,7 @@ void swaylock_handle_key(struct swaylock_state *state, state->auth_state = AUTH_STATE_INPUT_NOP; damage_state(state); schedule_indicator_clear(state); + schedule_password_clear(state); break; case XKB_KEY_Shift_L: case XKB_KEY_Shift_R: @@ -124,6 +143,7 @@ void swaylock_handle_key(struct swaylock_state *state, state->auth_state = AUTH_STATE_INPUT_NOP; damage_state(state); schedule_indicator_clear(state); + schedule_password_clear(state); break; case XKB_KEY_u: if (state->xkb.control) { @@ -140,6 +160,7 @@ void swaylock_handle_key(struct swaylock_state *state, state->auth_state = AUTH_STATE_INPUT; damage_state(state); schedule_indicator_clear(state); + schedule_password_clear(state); } break; } -- cgit v1.2.3-54-g00ecf From f98f351a5275967c46482e1c9c754fee927d72ee Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sat, 13 Oct 2018 17:06:33 +1000 Subject: swaylock: Don't wait too long for surface damage before verifying --- include/swaylock/swaylock.h | 1 + swaylock/password.c | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/swaylock/swaylock.h b/include/swaylock/swaylock.h index ae042c4f..e7165b3b 100644 --- a/include/swaylock/swaylock.h +++ b/include/swaylock/swaylock.h @@ -57,6 +57,7 @@ struct swaylock_state { struct loop *eventloop; struct loop_event *clear_indicator_timer; // clears the indicator struct loop_event *clear_password_timer; // clears the password buffer + struct loop_event *verify_password_timer; struct wl_display *display; struct wl_compositor *compositor; struct zwlr_layer_shell_v1 *layer_shell; diff --git a/swaylock/password.c b/swaylock/password.c index 5ab50042..3f9949b2 100644 --- a/swaylock/password.c +++ b/swaylock/password.c @@ -72,6 +72,11 @@ static void schedule_password_clear(struct swaylock_state *state) { state->eventloop, 10000, clear_password, state); } +static void handle_preverify_timeout(int fd, short mask, void *data) { + struct swaylock_state *state = data; + state->verify_password_timer = NULL; +} + void swaylock_handle_key(struct swaylock_state *state, xkb_keysym_t keysym, uint32_t codepoint) { switch (keysym) { @@ -83,7 +88,18 @@ void swaylock_handle_key(struct swaylock_state *state, state->auth_state = AUTH_STATE_VALIDATING; damage_state(state); - while (wl_display_dispatch(state->display) != -1 && state->run_display) { + + // We generally want to wait until all surfaces are showing the + // "verifying" state before we go and verify the password, because + // verifying it is a blocking operation. However, if the surface is on + // an output with DPMS off then it won't update, so we set a timer. + state->verify_password_timer = loop_add_timer( + state->eventloop, 50, handle_preverify_timeout, state); + + while (state->run_display && state->verify_password_timer) { + wl_display_flush(state->display); + loop_poll(state->eventloop); + bool ok = 1; struct swaylock_surface *surface; wl_list_for_each(surface, &state->surfaces, link) { @@ -103,6 +119,7 @@ void swaylock_handle_key(struct swaylock_state *state, } state->auth_state = AUTH_STATE_INVALID; damage_state(state); + schedule_indicator_clear(state); break; case XKB_KEY_Delete: case XKB_KEY_BackSpace: -- cgit v1.2.3-54-g00ecf From 6921fdc6d6134fd7aaf38ffc1686623eca9bbd18 Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Sun, 14 Oct 2018 12:28:38 +1000 Subject: Remove timerfd from loop implementation timerfd doesn't work on the BSDs, so this replaces it with a timespec for the expiry and uses a poll timeout to check the timers when needed. --- common/loop.c | 143 +++++++++++++++++++++++++++++++------------- include/loop.h | 25 +++++--- include/swaybar/bar.h | 1 - include/swaylock/swaylock.h | 6 +- swaybar/bar.c | 6 +- swaybar/status_line.c | 2 +- swaylock/password.c | 10 ++-- 7 files changed, 128 insertions(+), 65 deletions(-) (limited to 'include') diff --git a/common/loop.c b/common/loop.c index da3c2142..c358e212 100644 --- a/common/loop.c +++ b/common/loop.c @@ -1,18 +1,24 @@ +#include #include #include #include #include #include -#include +#include #include #include "list.h" #include "log.h" #include "loop.h" -struct loop_event { +struct loop_fd_event { void (*callback)(int fd, short mask, void *data); void *data; - bool is_timer; +}; + +struct loop_timer { + void (*callback)(void *data); + void *data; + struct timespec expiry; }; struct loop { @@ -20,7 +26,8 @@ struct loop { int fd_length; int fd_capacity; - list_t *events; // struct loop_event + list_t *fd_events; // struct loop_fd_event + list_t *timers; // struct loop_timer }; struct loop *loop_create(void) { @@ -31,86 +38,136 @@ struct loop *loop_create(void) { } loop->fd_capacity = 10; loop->fds = malloc(sizeof(struct pollfd) * loop->fd_capacity); - loop->events = create_list(); + loop->fd_events = create_list(); + loop->timers = create_list(); return loop; } void loop_destroy(struct loop *loop) { - list_foreach(loop->events, free); - list_free(loop->events); + list_foreach(loop->fd_events, free); + list_foreach(loop->timers, free); + list_free(loop->fd_events); + list_free(loop->timers); free(loop); } void loop_poll(struct loop *loop) { - poll(loop->fds, loop->fd_length, -1); + // 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; + } + } + } + poll(loop->fds, loop->fd_length, ms); + + // Dispatch fds for (int i = 0; i < loop->fd_length; ++i) { struct pollfd pfd = loop->fds[i]; - struct loop_event *event = loop->events->items[i]; + struct loop_fd_event *event = loop->fd_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); + } + } - if (event->is_timer) { - loop_remove_event(loop, event); + // 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); --i; } } } } -struct loop_event *loop_add_fd(struct loop *loop, int fd, short mask, +void loop_add_fd(struct loop *loop, int fd, short mask, void (*callback)(int fd, short mask, void *data), void *data) { + struct loop_fd_event *event = calloc(1, sizeof(struct loop_fd_event)); + if (!event) { + wlr_log(WLR_ERROR, "Unable to allocate memory for event"); + return; + } + event->callback = callback; + event->data = data; + list_add(loop->fd_events, event); + 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 = 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; +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) { + wlr_log(WLR_ERROR, "Unable to allocate memory for timer"); + return NULL; + } + timer->callback = callback; + timer->data = data; - list_add(loop->events, event); + clock_gettime(CLOCK_MONOTONIC, &timer->expiry); + timer->expiry.tv_sec += ms / 1000; - return event; -} + long int nsec = (ms % 1000) * 1000000; + if (timer->expiry.tv_nsec + nsec >= 1000000000) { + timer->expiry.tv_sec++; + nsec -= 1000000000; + } + timer->expiry.tv_nsec += nsec; -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 / 1000; - its.it_value.tv_nsec = (ms % 1000) * 1000000; - timerfd_settime(fd, 0, &its, NULL); - - struct loop_event *event = loop_add_fd(loop, fd, POLLIN, callback, data); - event->is_timer = true; - - return event; -} + list_add(loop->timers, timer); -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); + return timer; +} - if (event->is_timer) { - close(loop->fds[i].fd); - } +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); loop->fd_length--; - memmove(&loop->fds[i], &loop->fds[i + 1], sizeof(void*) * (loop->fd_length - i)); + memmove(&loop->fds[i], &loop->fds[i + 1], + sizeof(void*) * (loop->fd_length - i)); + + return true; + } + } + return false; +} - free(event); +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); return true; } } diff --git a/include/loop.h b/include/loop.h index 7c151785..2f608eda 100644 --- a/include/loop.h +++ b/include/loop.h @@ -5,12 +5,12 @@ /** * 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. + * The loop consists of file descriptors and timers. Typically the Wayland + * display's file descriptor will be one of the fds in the loop. */ struct loop; +struct loop_timer; /** * Create an event loop. @@ -28,20 +28,27 @@ void loop_destroy(struct loop *loop); void loop_poll(struct loop *loop); /** - * Add an fd to the loop. + * Add a file descriptor to the loop. */ -struct loop_event *loop_add_fd(struct loop *loop, int fd, short mask, +void 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. + * + * When the timer expires, the timer will be removed from the loop and freed. + */ +struct loop_timer *loop_add_timer(struct loop *loop, int ms, + void (*callback)(void *data), void *data); + +/** + * Remove a file descriptor from the loop. */ -struct loop_event *loop_add_timer(struct loop *loop, int ms, - void (*callback)(int fd, short mask, void *data), void *data); +bool loop_remove_fd(struct loop *loop, int fd); /** - * Remove an event from the loop. + * Remove a timer from the loop. */ -bool loop_remove_event(struct loop *loop, struct loop_event *event); +bool loop_remove_timer(struct loop *loop, struct loop_timer *timer); #endif diff --git a/include/swaybar/bar.h b/include/swaybar/bar.h index d61da5dc..db2d3d62 100644 --- a/include/swaybar/bar.h +++ b/include/swaybar/bar.h @@ -69,7 +69,6 @@ struct swaybar { struct status_line *status; struct loop *eventloop; - struct loop_event *status_event; int ipc_event_socketfd; int ipc_socketfd; diff --git a/include/swaylock/swaylock.h b/include/swaylock/swaylock.h index e7165b3b..25b41a71 100644 --- a/include/swaylock/swaylock.h +++ b/include/swaylock/swaylock.h @@ -55,9 +55,9 @@ struct swaylock_password { struct swaylock_state { struct loop *eventloop; - struct loop_event *clear_indicator_timer; // clears the indicator - struct loop_event *clear_password_timer; // clears the password buffer - struct loop_event *verify_password_timer; + struct loop_timer *clear_indicator_timer; // clears the indicator + struct loop_timer *clear_password_timer; // clears the password buffer + struct loop_timer *verify_password_timer; struct wl_display *display; struct wl_compositor *compositor; struct zwlr_layer_shell_v1 *layer_shell; diff --git a/swaybar/bar.c b/swaybar/bar.c index 8e89c9a8..be290c18 100644 --- a/swaybar/bar.c +++ b/swaybar/bar.c @@ -647,7 +647,7 @@ static void status_in(int fd, short mask, void *data) { if (mask & (POLLHUP | POLLERR)) { status_error(bar->status, "[error reading from status command]"); set_bar_dirty(bar); - loop_remove_event(bar->eventloop, bar->status_event); + loop_remove_fd(bar->eventloop, fd); } else if (status_handle_readable(bar->status)) { set_bar_dirty(bar); } @@ -658,8 +658,8 @@ void bar_run(struct swaybar *bar) { display_in, bar); loop_add_fd(bar->eventloop, bar->ipc_event_socketfd, POLLIN, ipc_in, bar); if (bar->status) { - bar->status_event = loop_add_fd( - bar->eventloop, bar->status->read_fd, POLLIN, status_in, bar); + loop_add_fd(bar->eventloop, bar->status->read_fd, POLLIN, + status_in, bar); } while (1) { wl_display_flush(bar->display); diff --git a/swaybar/status_line.c b/swaybar/status_line.c index 3f7a386f..65d6c052 100644 --- a/swaybar/status_line.c +++ b/swaybar/status_line.c @@ -16,7 +16,7 @@ static void status_line_close_fds(struct status_line *status) { if (status->read_fd != -1) { - loop_remove_event(status->bar->eventloop, status->bar->status_event); + loop_remove_fd(status->bar->eventloop, status->read_fd); close(status->read_fd); status->read_fd = -1; } diff --git a/swaylock/password.c b/swaylock/password.c index 3f9949b2..fecaecbf 100644 --- a/swaylock/password.c +++ b/swaylock/password.c @@ -40,7 +40,7 @@ static void append_ch(struct swaylock_password *pw, uint32_t codepoint) { pw->len += utf8_size; } -static void clear_indicator(int fd, short mask, void *data) { +static void clear_indicator(void *data) { struct swaylock_state *state = data; state->clear_indicator_timer = NULL; state->auth_state = AUTH_STATE_IDLE; @@ -49,13 +49,13 @@ static void clear_indicator(int fd, short mask, void *data) { static void schedule_indicator_clear(struct swaylock_state *state) { if (state->clear_indicator_timer) { - loop_remove_event(state->eventloop, state->clear_indicator_timer); + loop_remove_timer(state->eventloop, state->clear_indicator_timer); } state->clear_indicator_timer = loop_add_timer( state->eventloop, 3000, clear_indicator, state); } -static void clear_password(int fd, short mask, void *data) { +static void clear_password(void *data) { struct swaylock_state *state = data; state->clear_password_timer = NULL; state->auth_state = AUTH_STATE_CLEAR; @@ -66,13 +66,13 @@ static void clear_password(int fd, short mask, void *data) { static void schedule_password_clear(struct swaylock_state *state) { if (state->clear_password_timer) { - loop_remove_event(state->eventloop, state->clear_password_timer); + loop_remove_timer(state->eventloop, state->clear_password_timer); } state->clear_password_timer = loop_add_timer( state->eventloop, 10000, clear_password, state); } -static void handle_preverify_timeout(int fd, short mask, void *data) { +static void handle_preverify_timeout(void *data) { struct swaylock_state *state = data; state->verify_password_timer = NULL; } -- cgit v1.2.3-54-g00ecf From c6f153d8f9ad6c961c8dd8e620dc2e8fcb20e7bc Mon Sep 17 00:00:00 2001 From: Ryan Dwyer Date: Mon, 15 Oct 2018 00:23:53 +1000 Subject: Event loop: Fix memmove and remove extraneous declaration --- common/loop.c | 2 +- include/swaybar/bar.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'include') diff --git a/common/loop.c b/common/loop.c index 54b034d9..1b174967 100644 --- a/common/loop.c +++ b/common/loop.c @@ -159,7 +159,7 @@ bool loop_remove_fd(struct loop *loop, int fd) { loop->fd_length--; memmove(&loop->fds[i], &loop->fds[i + 1], - sizeof(void*) * (loop->fd_length - i)); + sizeof(struct pollfd) * (loop->fd_length - i)); return true; } diff --git a/include/swaybar/bar.h b/include/swaybar/bar.h index db2d3d62..58e2dee6 100644 --- a/include/swaybar/bar.h +++ b/include/swaybar/bar.h @@ -9,7 +9,6 @@ struct swaybar_config; struct swaybar_output; struct swaybar_workspace; struct loop; -struct loop_event; struct swaybar_pointer { struct wl_pointer *pointer; -- cgit v1.2.3-54-g00ecf