sleep until notification times out or we need redraw

This commit is contained in:
Sascha Kruse 2013-03-08 04:15:47 +01:00
parent 15cd7586a2
commit c612822057
3 changed files with 48 additions and 35 deletions

78
dunst.c
View File

@ -179,54 +179,62 @@ void history_pop(void)
} }
} }
void update(void)
{
time_t last_time = time(&last_time);
static time_t last_redraw = 0;
/* move messages from notification_queue to displayed_notifications */
update_lists();
if (displayed->length > 0 && !xctx.visible) {
x_win_show();
}
if (displayed->length == 0 && xctx.visible) {
x_win_hide();
}
if (xctx.visible && (force_redraw || time(NULL) - last_redraw > 0)) {
x_win_draw();
force_redraw = false;
last_redraw = time(NULL);
}
}
void wake_up(void) void wake_up(void)
{ {
force_redraw = true; force_redraw = true;
update(); run(NULL);
if (!timer_active) { }
timer_active = true;
g_timeout_add(1000, run, mainloop); static int get_sleep_time(void)
{
int sleep = 0;
for (GList *iter = g_queue_peek_head_link(displayed); iter;
iter = iter->next) {
notification *n = iter->data;
if (sleep == 0) {
sleep = notification_get_ttl(n);
} else {
sleep = MIN(sleep, notification_get_ttl(n));
}
} }
sleep = MIN(sleep, settings.show_age_threshold);
sleep = sleep * 1000;
/* add 501 milliseconds to make sure we wake are in the second
* after the next notification times out. Otherwise we'll wake
* up, but the notification won't get closed until we get woken
* up again (which might be multiple seconds later */
return sleep + 501;
} }
gboolean run(void *data) gboolean run(void *data)
{ {
update_lists();
update(); if (displayed->length > 0 && !xctx.visible) {
x_win_show();
}
if (displayed->length == 0 && xctx.visible) {
x_win_hide();
}
if (xctx.visible && force_redraw) {
x_win_draw();
force_redraw = false;
}
if (xctx.visible && !timer_active) { if (xctx.visible && !timer_active) {
g_timeout_add(200, run, mainloop); int sleep = get_sleep_time();
timer_active = true; if (sleep > 0)
g_timeout_add(sleep, run, mainloop);
} }
if (!xctx.visible && timer_active) { /* always return false to delete timers */
timer_active = false; return false;
/* returning false disables timeout */
return false;
}
return true;
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])

View File

@ -464,4 +464,8 @@ void notification_update_text_to_render(notification *n)
n->text_to_render = buf; n->text_to_render = buf;
} }
int notification_get_ttl(notification *n) {
return n->timeout - (time(NULL) - n->start);
}
/* vim: set ts=8 sw=8 tw=0: */ /* vim: set ts=8 sw=8 tw=0: */

View File

@ -48,3 +48,4 @@ int notification_close(notification * n, int reason);
void notification_print(notification * n); void notification_print(notification * n);
char *notification_fix_markup(char *str); char *notification_fix_markup(char *str);
void notification_update_text_to_render(notification *n); void notification_update_text_to_render(notification *n);
int notification_get_ttl(notification*n);