Release 960616
[wine/gsoc-2012-control.git] / windows / timer.c
blob31e3940062fc41e255e956726cf2f90d05058241
1 /*
2 * Timer functions
4 * Copyright 1993 Alexandre Julliard
5 */
7 #include "windows.h"
8 #include "queue.h"
9 #include "stddebug.h"
10 /* #define DEBUG_TIMER */
11 #include "debug.h"
14 typedef struct tagTIMER
16 HWND hwnd;
17 HQUEUE hq;
18 WORD msg; /* WM_TIMER or WM_SYSTIMER */
19 WORD id;
20 WORD timeout;
21 struct tagTIMER *next;
22 DWORD expires; /* Next expiration, or 0 if already expired */
23 FARPROC proc;
24 } TIMER;
26 #define NB_TIMERS 34
27 #define NB_RESERVED_TIMERS 2 /* for SetSystemTimer */
29 static TIMER TimersArray[NB_TIMERS];
31 static TIMER * pNextTimer = NULL; /* Next timer to expire */
33 /* Duration from 'time' until expiration of the timer */
34 #define EXPIRE_TIME(pTimer,time) \
35 (((pTimer)->expires <= (time)) ? 0 : (pTimer)->expires - (time))
38 /***********************************************************************
39 * TIMER_InsertTimer
41 * Insert the timer at its place in the chain.
43 static void TIMER_InsertTimer( TIMER * pTimer )
45 if (!pNextTimer || (pTimer->expires < pNextTimer->expires))
47 pTimer->next = pNextTimer;
48 pNextTimer = pTimer;
50 else
52 TIMER * ptr = pNextTimer;
53 while (ptr->next && (pTimer->expires >= ptr->next->expires))
54 ptr = ptr->next;
55 pTimer->next = ptr->next;
56 ptr->next = pTimer;
61 /***********************************************************************
62 * TIMER_RemoveTimer
64 * Remove the timer from the chain.
66 static void TIMER_RemoveTimer( TIMER * pTimer )
68 TIMER **ppTimer = &pNextTimer;
70 while (*ppTimer && (*ppTimer != pTimer)) ppTimer = &(*ppTimer)->next;
71 if (*ppTimer) *ppTimer = pTimer->next;
72 pTimer->next = NULL;
73 if (!pTimer->expires) QUEUE_DecTimerCount( pTimer->hq );
77 /***********************************************************************
78 * TIMER_ClearTimer
80 * Clear and remove a timer.
82 static void TIMER_ClearTimer( TIMER * pTimer )
84 TIMER_RemoveTimer( pTimer );
85 pTimer->hwnd = 0;
86 pTimer->msg = 0;
87 pTimer->id = 0;
88 pTimer->timeout = 0;
89 pTimer->proc = 0;
93 /***********************************************************************
94 * TIMER_SwitchQueue
96 void TIMER_SwitchQueue(HQUEUE old, HQUEUE new)
98 TIMER * pT = pNextTimer;
100 while (pT)
102 if (pT->hq == old) pT->hq = new;
103 pT = pT->next;
108 /***********************************************************************
109 * TIMER_RemoveWindowTimers
111 * Remove all timers for a given window.
113 void TIMER_RemoveWindowTimers( HWND hwnd )
115 int i;
116 TIMER *pTimer;
118 for (i = NB_TIMERS, pTimer = TimersArray; i > 0; i--, pTimer++)
119 if ((pTimer->hwnd == hwnd) && pTimer->timeout)
120 TIMER_ClearTimer( pTimer );
124 /***********************************************************************
125 * TIMER_RemoveQueueTimers
127 * Remove all timers for a given queue.
129 void TIMER_RemoveQueueTimers( HQUEUE hqueue )
131 int i;
132 TIMER *pTimer;
134 for (i = NB_TIMERS, pTimer = TimersArray; i > 0; i--, pTimer++)
135 if ((pTimer->hq == hqueue) && pTimer->timeout)
136 TIMER_ClearTimer( pTimer );
140 /***********************************************************************
141 * TIMER_RestartTimers
143 * Restart an expired timer.
145 static void TIMER_RestartTimer( TIMER * pTimer, DWORD curTime )
147 TIMER_RemoveTimer( pTimer );
148 pTimer->expires = curTime + pTimer->timeout;
149 TIMER_InsertTimer( pTimer );
153 /***********************************************************************
154 * TIMER_GetNextExpiration
156 * Return next timer expiration time, or -1 if none.
158 LONG TIMER_GetNextExpiration(void)
160 return pNextTimer ? EXPIRE_TIME( pNextTimer, GetTickCount() ) : -1;
164 /***********************************************************************
165 * TIMER_ExpireTimers
167 * Mark expired timers and wake the appropriate queues.
169 void TIMER_ExpireTimers(void)
171 TIMER *pTimer = pNextTimer;
172 DWORD curTime = GetTickCount();
174 while (pTimer && !pTimer->expires) /* Skip already expired timers */
175 pTimer = pTimer->next;
176 while (pTimer && (pTimer->expires <= curTime))
178 pTimer->expires = 0;
179 QUEUE_IncTimerCount( pTimer->hq );
180 pTimer = pTimer->next;
185 /***********************************************************************
186 * TIMER_GetTimerMsg
188 * Build a message for an expired timer.
190 BOOL TIMER_GetTimerMsg( MSG16 *msg, HWND hwnd, HQUEUE hQueue, BOOL remove )
192 TIMER *pTimer = pNextTimer;
193 DWORD curTime = GetTickCount();
195 if (hwnd) /* Find first timer for this window */
196 while (pTimer && (pTimer->hwnd != hwnd)) pTimer = pTimer->next;
197 else /* Find first timer for this queue */
198 while (pTimer && (pTimer->hq != hQueue)) pTimer = pTimer->next;
200 if (!pTimer || (pTimer->expires > curTime)) return FALSE; /* No timer */
201 if (remove) TIMER_RestartTimer( pTimer, curTime ); /* Restart it */
203 dprintf_timer( stddeb, "Timer expired: %04x, %04x, %04x, %08lx\n",
204 pTimer->hwnd, pTimer->msg, pTimer->id, (DWORD)pTimer->proc);
206 /* Build the message */
207 msg->hwnd = pTimer->hwnd;
208 msg->message = pTimer->msg;
209 msg->wParam = pTimer->id;
210 msg->lParam = (LONG)pTimer->proc;
211 msg->time = curTime;
212 return TRUE;
216 /***********************************************************************
217 * TIMER_SetTimer
219 static WORD TIMER_SetTimer( HWND hwnd, WORD id, WORD timeout,
220 FARPROC proc, BOOL sys )
222 int i;
223 TIMER * pTimer;
225 if (!timeout) return 0;
226 /* if (!hwnd && !proc) return 0; */
228 /* Check if there's already a timer with the same hwnd and id */
230 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
231 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
232 (pTimer->timeout != 0))
234 /* Got one: set new values and return */
235 TIMER_RemoveTimer( pTimer );
236 pTimer->timeout = timeout;
237 pTimer->proc = proc;
238 pTimer->expires = GetTickCount() + timeout;
239 TIMER_InsertTimer( pTimer );
240 return id;
243 /* Find a free timer */
245 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
246 if (!pTimer->timeout) break;
248 if (i >= NB_TIMERS) return 0;
249 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return 0;
250 if (!hwnd) id = i + 1;
252 /* Add the timer */
254 pTimer->hwnd = hwnd;
255 pTimer->hq = (hwnd) ? GetTaskQueue( GetWindowTask( hwnd ) )
256 : GetTaskQueue( 0 );
257 pTimer->msg = sys ? WM_SYSTIMER : WM_TIMER;
258 pTimer->id = id;
259 pTimer->timeout = timeout;
260 pTimer->expires = GetTickCount() + timeout;
261 pTimer->proc = proc;
262 dprintf_timer(stddeb, "Timer added: %p, %04x, %04x, %04x, %08lx\n",
263 pTimer, pTimer->hwnd, pTimer->msg, pTimer->id, (DWORD)pTimer->proc);
264 TIMER_InsertTimer( pTimer );
265 if (!id)
266 return TRUE;
267 else
268 return id;
272 /***********************************************************************
273 * TIMER_KillTimer
275 static BOOL TIMER_KillTimer( HWND hwnd, WORD id, BOOL sys )
277 int i;
278 TIMER * pTimer;
280 /* Find the timer */
282 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
283 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
284 (pTimer->timeout != 0)) break;
285 if (i >= NB_TIMERS) return FALSE;
286 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return FALSE;
287 if (!sys && (pTimer->msg != WM_TIMER)) return FALSE;
288 else if (sys && (pTimer->msg != WM_SYSTIMER)) return FALSE;
290 /* Delete the timer */
292 TIMER_ClearTimer( pTimer );
293 return TRUE;
297 /***********************************************************************
298 * SetTimer (USER.10)
300 WORD SetTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
302 dprintf_timer(stddeb, "SetTimer: %04x %d %d %08lx\n", hwnd, id, timeout, (LONG)proc );
303 return TIMER_SetTimer( hwnd, id, timeout, proc, FALSE );
307 /***********************************************************************
308 * SetSystemTimer (USER.11)
310 WORD SetSystemTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
312 dprintf_timer(stddeb, "SetSystemTimer: %04x %d %d %08lx\n",
313 hwnd, id, timeout, (LONG)proc );
314 return TIMER_SetTimer( hwnd, id, timeout, proc, TRUE );
318 /***********************************************************************
319 * KillTimer (USER.12)
321 BOOL KillTimer( HWND hwnd, WORD id )
323 dprintf_timer(stddeb, "KillTimer: %04x %d\n", hwnd, id );
324 return TIMER_KillTimer( hwnd, id, FALSE );
328 /***********************************************************************
329 * KillSystemTimer (USER.182)
331 BOOL KillSystemTimer( HWND hwnd, WORD id )
333 dprintf_timer(stddeb, "KillSystemTimer: %04x %d\n", hwnd, id );
334 return TIMER_KillTimer( hwnd, id, TRUE );