Release 0.5
[wine] / windows / timer.c
1 /*
2  * Timer functions
3  *
4  * Copyright 1993 Alexandre Julliard
5  */
6
7 static char Copyright[] = "Copyright  Alexandre Julliard, 1993";
8
9 #include "windows.h"
10 #include "message.h"
11
12
13 typedef struct tagTIMER
14 {
15     HWND             hwnd;
16     WORD             msg;  /* WM_TIMER or WM_SYSTIMER */
17     WORD             id;
18     WORD             timeout;
19     struct tagTIMER *next;
20     DWORD            expires;
21     FARPROC          proc;
22 } TIMER;
23
24 #define NB_TIMERS            34
25 #define NB_RESERVED_TIMERS    2  /* for SetSystemTimer */
26
27 static TIMER TimersArray[NB_TIMERS];
28
29 static TIMER * pNextTimer = NULL;  /* Next timer to expire */
30
31
32 /***********************************************************************
33  *           TIMER_InsertTimer
34  *
35  * Insert the timer at its place in the chain.
36  */
37 static void TIMER_InsertTimer( TIMER * pTimer )
38 {
39     if (!pNextTimer || (pTimer->expires < pNextTimer->expires))
40     {
41         pTimer->next = pNextTimer;
42         pNextTimer = pTimer;
43     }
44     else
45     {
46         TIMER * ptr = pNextTimer;       
47         while (ptr->next && (pTimer->expires >= ptr->next->expires))
48             ptr = ptr->next;
49         pTimer->next = ptr;
50         ptr->next = pTimer;
51     }
52 }
53
54
55 /***********************************************************************
56  *           TIMER_RemoveTimer
57  *
58  * Remove the timer from the chain.
59  */
60 static void TIMER_RemoveTimer( TIMER * pTimer )
61 {
62     if (pTimer == pNextTimer) pNextTimer = pTimer->next;
63     else
64     {
65         TIMER * ptr = pNextTimer;
66         while (ptr && (ptr->next != pTimer)) ptr = ptr->next;
67         if (ptr) ptr->next = pTimer->next;
68     }
69     pTimer->next = NULL;
70 }
71
72
73 /***********************************************************************
74  *           TIMER_NextExpire
75  *
76  * Return time until next timer expiration (-1 if none).
77  */
78 static DWORD TIMER_NextExpire( DWORD curTime )
79 {
80     if (!pNextTimer) return -1;
81     if (pNextTimer->expires <= curTime) return 0;
82     return pNextTimer->expires - curTime;
83 }
84
85
86 /***********************************************************************
87  *           TIMER_CheckTimer
88  *
89  * Check whether a timer has expired, and post a message if necessary.
90  * Return TRUE if msg posted, and return time until next expiration in 'next'.
91  */
92 BOOL TIMER_CheckTimer( DWORD *next )
93 {
94     TIMER * pTimer = pNextTimer;
95     DWORD curTime = GetTickCount();
96     
97     if ((*next = TIMER_NextExpire( curTime )) != 0) return FALSE;
98
99     PostMessage( pTimer->hwnd, pTimer->msg, pTimer->id, (LONG)pTimer->proc );
100     TIMER_RemoveTimer( pTimer );
101
102       /* If timeout == 0, the timer has been removed by KillTimer */
103     if (pTimer->timeout)
104     {
105           /* Restart the timer */
106         pTimer->expires = curTime + pTimer->timeout;
107         TIMER_InsertTimer( pTimer );
108     }
109     *next = TIMER_NextExpire( curTime );
110     return TRUE;
111 }
112
113
114 /***********************************************************************
115  *           TIMER_SetTimer
116  */
117 static WORD TIMER_SetTimer( HWND hwnd, WORD id, WORD timeout,
118                             FARPROC proc, BOOL sys )
119 {
120     int i;
121     TIMER * pTimer;
122
123     if (!timeout) return 0;
124     if (!hwnd && !proc) return 0;
125     
126       /* Find a free timer */
127     
128     for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
129         if (!pTimer->timeout) break;
130
131     if (i >= NB_TIMERS) return 0;
132     if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return 0;
133     if (!hwnd) id = i + 1;
134     
135       /* Add the timer */
136
137     pTimer->hwnd    = hwnd;
138     pTimer->msg     = sys ? WM_SYSTIMER : WM_TIMER;
139     pTimer->id      = id;
140     pTimer->timeout = timeout;
141     pTimer->expires = GetTickCount() + timeout;
142     pTimer->proc    = proc;
143     TIMER_InsertTimer( pTimer );
144     MSG_IncTimerCount( GetTaskQueue(0) );
145     return id;
146 }
147
148
149 /***********************************************************************
150  *           TIMER_KillTimer
151  */
152 static BOOL TIMER_KillTimer( HWND hwnd, WORD id, BOOL sys )
153 {
154     int i;
155     TIMER * pTimer;
156     
157       /* Find the timer */
158     
159     for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
160         if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
161             (pTimer->timeout != 0)) break;
162     if (i >= NB_TIMERS) return FALSE;
163     if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return FALSE;
164     if (!sys && (pTimer->msg != WM_TIMER)) return FALSE;
165     else if (sys && (pTimer->msg != WM_SYSTIMER)) return FALSE;    
166
167       /* Delete the timer */
168
169     pTimer->hwnd    = 0;
170     pTimer->msg     = 0;
171     pTimer->id      = 0;
172     pTimer->timeout = 0;
173     pTimer->proc    = 0;
174     TIMER_RemoveTimer( pTimer );
175     MSG_DecTimerCount( GetTaskQueue(0) );
176     return TRUE;
177 }
178
179
180 /***********************************************************************
181  *           SetTimer   (USER.10)
182  */
183 WORD SetTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
184 {
185 #ifdef DEBUG_TIMER    
186     printf( "SetTimer: %d %d %d %p\n", hwnd, id, timeout, proc );
187 #endif
188     return TIMER_SetTimer( hwnd, id, timeout, proc, FALSE );
189 }
190
191
192 /***********************************************************************
193  *           SetSystemTimer   (USER.11)
194  */
195 WORD SetSystemTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
196 {
197 #ifdef DEBUG_TIMER    
198     printf( "SetSystemTimer: %d %d %d %p\n", hwnd, id, timeout, proc );
199 #endif
200     return TIMER_SetTimer( hwnd, id, timeout, proc, TRUE );
201 }
202
203
204 /***********************************************************************
205  *           KillTimer   (USER.12)
206  */
207 BOOL KillTimer( HWND hwnd, WORD id )
208 {
209 #ifdef DEBUG_TIMER
210     printf( "KillTimer: %d %d\n", hwnd, id );
211 #endif
212     return TIMER_KillTimer( hwnd, id, FALSE );
213 }
214
215
216 /***********************************************************************
217  *           KillSystemTimer   (USER.182)
218  */
219 BOOL KillSystemTimer( HWND hwnd, WORD id )
220 {
221 #ifdef DEBUG_TIMER
222     printf( "KillSystemTimer: %d %d\n", hwnd, id );
223 #endif
224     return TIMER_KillTimer( hwnd, id, TRUE );
225 }