]> icculus.org git repositories - dana/openbox.git/blob - openbox/timer.c
engine builds
[dana/openbox.git] / openbox / timer.c
1 #include "timer.h"
2
3 #ifdef    HAVE_SYS_TIME_H
4 #  include <sys/time.h>
5 #endif
6
7 static GTimeVal now;
8 static GTimeVal ret_wait;
9 static GSList *timers; /* nearest timer is at the top */
10
11 #define NEAREST_TIMEOUT (((Timer*)timers->data)->timeout)
12
13 static long timecompare(GTimeVal *a, GTimeVal *b)
14 {
15     long r;
16
17     if ((r = b->tv_sec - a->tv_sec)) return r;
18     return b->tv_usec - a->tv_sec;
19     
20 }
21
22 static void insert_timer(Timer *self)
23 {
24     GSList *it;
25     for (it = timers; it != NULL; it = it->next) {
26         Timer *t = it->data;
27         if (timecompare(&self->timeout, &t->timeout) <= 0) {
28             timers = g_slist_insert_before(timers, it, self);
29             break;
30         }
31     }
32     if (it == NULL) /* didnt fit anywhere in the list */
33         timers = g_slist_append(timers, self);
34 }
35
36 void timer_startup()
37 {
38     g_get_current_time(&now);
39     timers = NULL;
40 }
41
42 void timer_shutdown()
43 {
44     GSList *it;
45     for (it = timers; it != NULL; it = it->next) {
46         g_free(it->data);
47     }
48     g_slist_free(timers);
49     timers = NULL;
50 }
51
52 Timer *timer_start(long delay, TimeoutHandler cb, void *data)
53 {
54     Timer *self = g_new(Timer, 1);
55     self->delay = delay;
56     self->action = cb;
57     self->data = data;
58     self->del_me = FALSE;
59     self->last = self->timeout = now;
60     g_time_val_add(&self->timeout, delay);
61
62     insert_timer(self);
63
64     return self;
65 }
66
67 void timer_stop(Timer *self)
68 {
69     self->del_me = TRUE;
70 }
71
72 /* find the time to wait for the nearest timeout */
73 static gboolean nearest_timeout_wait(GTimeVal *tm)
74 {
75   if (timers == NULL)
76     return FALSE;
77
78   tm->tv_sec = NEAREST_TIMEOUT.tv_sec - now.tv_sec;
79   tm->tv_usec = NEAREST_TIMEOUT.tv_usec - now.tv_usec;
80
81   while (tm->tv_usec < 0) {
82     tm->tv_usec += G_USEC_PER_SEC;
83     tm->tv_sec--;
84   }
85   tm->tv_sec += tm->tv_usec / G_USEC_PER_SEC;
86   tm->tv_usec %= G_USEC_PER_SEC;
87   if (tm->tv_sec < 0)
88     tm->tv_sec = 0;
89
90   return TRUE;
91 }
92
93
94 void timer_dispatch(GTimeVal **wait)
95 {
96     g_get_current_time(&now);
97
98     while (timers != NULL) {
99         Timer *curr = timers->data; /* get the top element */
100         /* since timer_stop doesn't actually free the timer, we have to do our
101            real freeing in here.
102         */
103         if (curr->del_me) {
104             timers = g_slist_delete_link(timers, timers); /* delete the top */
105             g_free(curr);
106             continue;
107         }
108         
109         /* the queue is sorted, so if this timer shouldn't fire, none are 
110            ready */
111         if (timecompare(&now, &NEAREST_TIMEOUT) <= 0)
112             break;
113
114         /* we set the last fired time to delay msec after the previous firing,
115            then re-insert.  timers maintain their order and may trigger more
116            than once if they've waited more than one delay's worth of time.
117         */
118         timers = g_slist_delete_link(timers, timers);
119         g_time_val_add(&curr->last, curr->delay);
120         curr->action(curr->data);
121         g_time_val_add(&curr->timeout, curr->delay);
122         insert_timer(curr);
123
124         /* if at least one timer fires, then don't wait on X events, as there
125            may already be some in the queue from the timer callbacks.
126         */
127         ret_wait.tv_sec = ret_wait.tv_usec = 0;
128         *wait = &ret_wait;
129         return;
130     }
131
132     if (nearest_timeout_wait(&ret_wait))
133         *wait = &ret_wait;
134     else
135         *wait = NULL;
136 }