Move get_sleep_time to queues.c

This commit is contained in:
Benedikt Heine 2017-10-08 20:01:24 +02:00
parent 3fab4c470b
commit 59ac6d0f88
3 changed files with 44 additions and 34 deletions

View File

@ -120,39 +120,6 @@ void wake_up(void)
run(NULL); 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) gboolean run(void *data)
{ {
update_lists(); update_lists();
@ -177,7 +144,7 @@ gboolean run(void *data)
if (xctx.visible) { if (xctx.visible) {
gint64 now = g_get_monotonic_time(); gint64 now = g_get_monotonic_time();
gint64 sleep = get_sleep_time(); gint64 sleep = queues_get_next_datachange(now);
gint64 timeout_at = now + sleep; gint64 timeout_at = now + sleep;
if (sleep >= 0) { if (sleep >= 0) {

View File

@ -213,6 +213,37 @@ void history_push(notification *n)
g_queue_push_tail(history, 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) static void teardown_notification(gpointer data)
{ {
notification *n = data; notification *n = data;

View File

@ -62,6 +62,18 @@ void history_pop(void);
void history_push(notification *n); void history_push(notification *n);
void move_all_to_history(void); 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 * Remove all notifications from all lists
* and free the notifications * and free the notifications