]> Dogcows Code - chaz/openbox/blob - openbox/timer.c
changes to the timer api, pass the timer to the callback function.
[chaz/openbox] / 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 (((ObTimer*)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_usec;
19
20 }
21
22 static void insert_timer(ObTimer *self)
23 {
24 GSList *it;
25 for (it = timers; it != NULL; it = it->next) {
26 ObTimer *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 ObTimer *timer_start(long delay, ObTimeoutHandler cb, void *data)
53 {
54 ObTimer *self = g_new(ObTimer, 1);
55 self->delay = delay;
56 self->action = cb;
57 self->data = data;
58 self->del_me = FALSE;
59 g_get_current_time(&now);
60 self->last = self->timeout = now;
61 g_time_val_add(&self->timeout, delay);
62
63 insert_timer(self);
64
65 return self;
66 }
67
68 void timer_stop(ObTimer *self)
69 {
70 self->del_me = TRUE;
71 }
72
73 /* find the time to wait for the nearest timeout */
74 static gboolean nearest_timeout_wait(GTimeVal *tm)
75 {
76 if (timers == NULL)
77 return FALSE;
78
79 tm->tv_sec = NEAREST_TIMEOUT.tv_sec - now.tv_sec;
80 tm->tv_usec = NEAREST_TIMEOUT.tv_usec - now.tv_usec;
81
82 while (tm->tv_usec < 0) {
83 tm->tv_usec += G_USEC_PER_SEC;
84 tm->tv_sec--;
85 }
86 tm->tv_sec += tm->tv_usec / G_USEC_PER_SEC;
87 tm->tv_usec %= G_USEC_PER_SEC;
88 if (tm->tv_sec < 0)
89 tm->tv_sec = 0;
90
91 return TRUE;
92 }
93
94
95 void timer_dispatch(GTimeVal **wait)
96 {
97 g_get_current_time(&now);
98
99 while (timers != NULL) {
100 ObTimer *curr = timers->data; /* get the top element */
101 /* since timer_stop doesn't actually free the timer, we have to do our
102 real freeing in here.
103 */
104 if (curr->del_me) {
105 timers = g_slist_delete_link(timers, timers); /* delete the top */
106 g_free(curr);
107 continue;
108 }
109
110 /* the queue is sorted, so if this timer shouldn't fire, none are
111 ready */
112 if (timecompare(&NEAREST_TIMEOUT, &now) <= 0)
113 break;
114
115 /* we set the last fired time to delay msec after the previous firing,
116 then re-insert. timers maintain their order and may trigger more
117 than once if they've waited more than one delay's worth of time.
118 */
119 timers = g_slist_delete_link(timers, timers);
120 g_time_val_add(&curr->last, curr->delay);
121 curr->action(curr, curr->data);
122 g_time_val_add(&curr->timeout, curr->delay);
123 insert_timer(curr);
124
125 /* if at least one timer fires, then don't wait on X events, as there
126 may already be some in the queue from the timer callbacks.
127 */
128 ret_wait.tv_sec = ret_wait.tv_usec = 0;
129 *wait = &ret_wait;
130 return;
131 }
132
133 if (nearest_timeout_wait(&ret_wait))
134 *wait = &ret_wait;
135 else
136 *wait = NULL;
137 }
This page took 0.039384 seconds and 4 git commands to generate.