From 59ac6d0f8898e01e5dc897c7f8c8a6022825b846 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Sun, 8 Oct 2017 20:01:24 +0200 Subject: [PATCH] Move get_sleep_time to queues.c --- src/dunst.c | 35 +---------------------------------- src/queues.c | 31 +++++++++++++++++++++++++++++++ src/queues.h | 12 ++++++++++++ 3 files changed, 44 insertions(+), 34 deletions(-) diff --git a/src/dunst.c b/src/dunst.c index 6ed9eff..d32601a 100644 --- a/src/dunst.c +++ b/src/dunst.c @@ -120,39 +120,6 @@ void wake_up(void) run(NULL); } -/* TODO: move parts to queue.c */ -static gint64 get_sleep_time(void) -{ - gint64 time = g_get_monotonic_time(); - gint64 sleep = G_MAXINT64; - - for (GList *iter = g_queue_peek_head_link(displayed); iter; - iter = iter->next) { - notification *n = iter->data; - gint64 ttl = n->timeout - (time - n->start); - - if (n->timeout > 0) { - if (ttl > 0) - sleep = MIN(sleep, ttl); - else - // while we're processing, the notification already timed out - return 0; - } - - if (settings.show_age_threshold >= 0) { - gint64 age = time - n->timestamp; - - if (age > settings.show_age_threshold) - // sleep exactly until the next shift of the second happens - sleep = MIN(sleep, ((G_USEC_PER_SEC) - (age % (G_USEC_PER_SEC)))); - else if (ttl > settings.show_age_threshold) - sleep = MIN(sleep, settings.show_age_threshold); - } - } - - return sleep != G_MAXINT64 ? sleep : -1; -} - gboolean run(void *data) { update_lists(); @@ -177,7 +144,7 @@ gboolean run(void *data) if (xctx.visible) { gint64 now = g_get_monotonic_time(); - gint64 sleep = get_sleep_time(); + gint64 sleep = queues_get_next_datachange(now); gint64 timeout_at = now + sleep; if (sleep >= 0) { diff --git a/src/queues.c b/src/queues.c index 2070bbb..0782835 100644 --- a/src/queues.c +++ b/src/queues.c @@ -213,6 +213,37 @@ void history_push(notification *n) g_queue_push_tail(history, n); } +gint64 queues_get_next_datachange(gint64 time) +{ + gint64 sleep = G_MAXINT64; + + for (GList *iter = g_queue_peek_head_link(displayed); iter; + iter = iter->next) { + notification *n = iter->data; + gint64 ttl = n->timeout - (time - n->start); + + if (n->timeout > 0) { + if (ttl > 0) + sleep = MIN(sleep, ttl); + else + // while we're processing, the notification already timed out + return 0; + } + + if (settings.show_age_threshold >= 0) { + gint64 age = time - n->timestamp; + + if (age > settings.show_age_threshold) + // sleep exactly until the next shift of the second happens + sleep = MIN(sleep, ((G_USEC_PER_SEC) - (age % (G_USEC_PER_SEC)))); + else if (ttl > settings.show_age_threshold) + sleep = MIN(sleep, settings.show_age_threshold); + } + } + + return sleep != G_MAXINT64 ? sleep : -1; +} + static void teardown_notification(gpointer data) { notification *n = data; diff --git a/src/queues.h b/src/queues.h index f6517e8..942d0a0 100644 --- a/src/queues.h +++ b/src/queues.h @@ -62,6 +62,18 @@ void history_pop(void); void history_push(notification *n); void move_all_to_history(void); +/* + * Return the distance to the next event in the queue, + * which forces an update visible to the user + * + * This may be: + * + * - notification hits timeout + * - notification's age second changes + * - notification's age threshold is hit + */ +gint64 queues_get_next_datachange(gint64 time); + /* * Remove all notifications from all lists * and free the notifications