ui/common: Free message on return
[petitboot] / ui / common / timer.c
index 954a18ad62b8a764d2c38c113457c70cfc402ee0..3d808987e3cc76d6f170406f50153089840f76a2 100644 (file)
@@ -20,7 +20,6 @@
 #include "config.h"
 #endif
 
-#define _GNU_SOURCE
 #include <assert.h>
 #include <limits.h>
 #include <unistd.h>
  * @seconds: The final timeout value in seconds.
  */
 
-void ui_timer_init(struct ui_timer *timer, unsigned int seconds)
+void ui_timer_init(struct waitset *waitset, struct ui_timer *timer,
+               unsigned int seconds)
 {
        pb_log("%s: %u\n", __func__, seconds);
-       assert(!timer->disabled);
        timer->timeout = seconds;
+       timer->waitset = waitset;
 }
 
 /**
- * ui_timer_next - Calculate the next timer interval.
+ * ui_timer_kick - Kickstart the next timer interval.
  */
 
-static unsigned int ui_timer_next(unsigned int seconds)
+static int timer_cb(void *arg)
 {
-       unsigned int next;
-
-       if (seconds == 0) {
-               next = 0;
-               goto done;
-       }
-
-       if (seconds <= 10) {
-               next = 1;
-               goto done;
-       }
-
-       if (seconds <= 60) {
-               next = seconds % 5;
-               next = next ? next : 5;
-               goto done;
-       }
+       struct ui_timer *timer = arg;
 
-       next = seconds % 10;
-       next = next ? next : 10;
-
-done:
-       pb_log("%s: %u = %u\n", __func__, seconds, next);
-       return next;
+       timer->handle_timeout(timer);
+       timer->waiter = 0;
+       return 0;
 }
 
-/**
- * ui_timer_kick - Kickstart the next timer interval.
- */
-
 void ui_timer_kick(struct ui_timer *timer)
 {
-       unsigned int next;
-
-       if(timer->disabled)
-               return;
-
        if (timer->update_display)
                timer->update_display(timer, timer->timeout);
 
-       next = ui_timer_next(timer->timeout);
-       timer->timeout -= next;
-
-       if (next) {
-               alarm(next);
-               return;
-       }
-
-       pb_log("%s: timed out\n", __func__);
+       if (timer->waiter)
+               waiter_remove(timer->waiter);
 
-       ui_timer_disable(timer);
-       timer->handle_timeout(timer);
+       timer->waiter = waiter_register_timeout(timer->waitset,
+                       timer->timeout * 1000, timer_cb, timer);
 }
 
 /**
@@ -106,34 +71,10 @@ void ui_timer_kick(struct ui_timer *timer)
 
 void ui_timer_disable(struct ui_timer *timer)
 {
-       if (timer->disabled)
+       if (!timer->waiter)
                return;
 
        pb_log("%s\n", __func__);
-       timer->disabled = 1;
-       timer->timeout = UINT_MAX;
-       alarm(0);
-}
-
-/**
- * ui_timer_sigalrm
- *
- * Called at SIGALRM.
- */
-
-void ui_timer_sigalrm(struct ui_timer *timer)
-{
-       timer->signaled = 1;
-}
-
-/**
- * ui_timer_process_sig - Process a timer signal
- */
-
-void ui_timer_process_sig(struct ui_timer *timer)
-{
-       while (timer->signaled) {
-               timer->signaled = 0;
-               ui_timer_kick(timer);
-       }
+       waiter_remove(timer->waiter);
+       timer->waiter = NULL;
 }