4 * Copyright 1993 Alexandre Julliard
11 /* #define DEBUG_TIMER */
15 typedef struct tagTIMER
19 UINT16 msg; /* WM_TIMER or WM_SYSTIMER */
22 struct tagTIMER *next;
23 DWORD expires; /* Next expiration, or 0 if already expired */
28 #define NB_RESERVED_TIMERS 2 /* for SetSystemTimer */
30 static TIMER TimersArray[NB_TIMERS];
32 static TIMER * pNextTimer = NULL; /* Next timer to expire */
34 /* Duration from 'time' until expiration of the timer */
35 #define EXPIRE_TIME(pTimer,time) \
36 (((pTimer)->expires <= (time)) ? 0 : (pTimer)->expires - (time))
39 /***********************************************************************
42 * Insert the timer at its place in the chain.
44 static void TIMER_InsertTimer( TIMER * pTimer )
46 if (!pNextTimer || (pTimer->expires < pNextTimer->expires))
48 pTimer->next = pNextTimer;
53 TIMER * ptr = pNextTimer;
54 while (ptr->next && (pTimer->expires >= ptr->next->expires))
56 pTimer->next = ptr->next;
62 /***********************************************************************
65 * Remove the timer from the chain.
67 static void TIMER_RemoveTimer( TIMER * pTimer )
69 TIMER **ppTimer = &pNextTimer;
71 while (*ppTimer && (*ppTimer != pTimer)) ppTimer = &(*ppTimer)->next;
72 if (*ppTimer) *ppTimer = pTimer->next;
74 if (!pTimer->expires) QUEUE_DecTimerCount( pTimer->hq );
78 /***********************************************************************
81 * Clear and remove a timer.
83 static void TIMER_ClearTimer( TIMER * pTimer )
85 TIMER_RemoveTimer( pTimer );
90 WINPROC_FreeProc( pTimer->proc, WIN_PROC_TIMER );
94 /***********************************************************************
97 void TIMER_SwitchQueue( HQUEUE16 old, HQUEUE16 new )
99 TIMER * pT = pNextTimer;
103 if (pT->hq == old) pT->hq = new;
109 /***********************************************************************
110 * TIMER_RemoveWindowTimers
112 * Remove all timers for a given window.
114 void TIMER_RemoveWindowTimers( HWND32 hwnd )
119 for (i = NB_TIMERS, pTimer = TimersArray; i > 0; i--, pTimer++)
120 if ((pTimer->hwnd == hwnd) && pTimer->timeout)
121 TIMER_ClearTimer( pTimer );
125 /***********************************************************************
126 * TIMER_RemoveQueueTimers
128 * Remove all timers for a given queue.
130 void TIMER_RemoveQueueTimers( HQUEUE16 hqueue )
135 for (i = NB_TIMERS, pTimer = TimersArray; i > 0; i--, pTimer++)
136 if ((pTimer->hq == hqueue) && pTimer->timeout)
137 TIMER_ClearTimer( pTimer );
141 /***********************************************************************
142 * TIMER_RestartTimers
144 * Restart an expired timer.
146 static void TIMER_RestartTimer( TIMER * pTimer, DWORD curTime )
148 TIMER_RemoveTimer( pTimer );
149 pTimer->expires = curTime + pTimer->timeout;
150 TIMER_InsertTimer( pTimer );
154 /***********************************************************************
155 * TIMER_GetNextExpiration
157 * Return next timer expiration time, or -1 if none.
159 LONG TIMER_GetNextExpiration(void)
161 return pNextTimer ? EXPIRE_TIME( pNextTimer, GetTickCount() ) : -1;
165 /***********************************************************************
168 * Mark expired timers and wake the appropriate queues.
170 void TIMER_ExpireTimers(void)
172 TIMER *pTimer = pNextTimer;
173 DWORD curTime = GetTickCount();
175 while (pTimer && !pTimer->expires) /* Skip already expired timers */
176 pTimer = pTimer->next;
177 while (pTimer && (pTimer->expires <= curTime))
180 QUEUE_IncTimerCount( pTimer->hq );
181 pTimer = pTimer->next;
186 /***********************************************************************
189 * Build a message for an expired timer.
191 BOOL32 TIMER_GetTimerMsg( MSG16 *msg, HWND32 hwnd,
192 HQUEUE16 hQueue, BOOL32 remove )
194 TIMER *pTimer = pNextTimer;
195 DWORD curTime = GetTickCount();
197 if (hwnd) /* Find first timer for this window */
198 while (pTimer && (pTimer->hwnd != hwnd)) pTimer = pTimer->next;
199 else /* Find first timer for this queue */
200 while (pTimer && (pTimer->hq != hQueue)) pTimer = pTimer->next;
202 if (!pTimer || (pTimer->expires > curTime)) return FALSE; /* No timer */
203 if (remove) TIMER_RestartTimer( pTimer, curTime ); /* Restart it */
205 dprintf_timer( stddeb, "Timer expired: %04x, %04x, %04x, %08lx\n",
206 pTimer->hwnd, pTimer->msg, pTimer->id, (DWORD)pTimer->proc);
208 /* Build the message */
209 msg->hwnd = (HWND16)pTimer->hwnd;
210 msg->message = pTimer->msg;
211 msg->wParam = (UINT16)pTimer->id;
212 msg->lParam = (LONG)pTimer->proc;
218 /***********************************************************************
221 static UINT32 TIMER_SetTimer( HWND32 hwnd, UINT32 id, UINT32 timeout,
222 WNDPROC16 proc, WINDOWPROCTYPE type, BOOL32 sys )
227 if (!timeout) return 0;
229 /* Check if there's already a timer with the same hwnd and id */
231 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
232 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
233 (pTimer->timeout != 0))
235 /* Got one: set new values and return */
236 TIMER_RemoveTimer( pTimer );
237 pTimer->timeout = timeout;
238 WINPROC_FreeProc( pTimer->proc, WIN_PROC_TIMER );
239 pTimer->proc = (HWINDOWPROC)0;
240 if (proc) WINPROC_SetProc( &pTimer->proc, proc,
241 type, WIN_PROC_TIMER );
242 pTimer->expires = GetTickCount() + timeout;
243 TIMER_InsertTimer( pTimer );
247 /* Find a free timer */
249 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
250 if (!pTimer->timeout) break;
252 if (i >= NB_TIMERS) return 0;
253 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return 0;
254 if (!hwnd) id = i + 1;
259 pTimer->hq = (hwnd) ? GetTaskQueue( GetWindowTask16( hwnd ) )
261 pTimer->msg = sys ? WM_SYSTIMER : WM_TIMER;
263 pTimer->timeout = timeout;
264 pTimer->expires = GetTickCount() + timeout;
265 pTimer->proc = (HWINDOWPROC)0;
266 if (proc) WINPROC_SetProc( &pTimer->proc, proc, type, WIN_PROC_TIMER );
267 dprintf_timer( stddeb, "Timer added: %p, %04x, %04x, %04x, %08lx\n",
268 pTimer, pTimer->hwnd, pTimer->msg, pTimer->id,
269 (DWORD)pTimer->proc );
270 TIMER_InsertTimer( pTimer );
271 if (!id) return TRUE;
276 /***********************************************************************
279 static BOOL32 TIMER_KillTimer( HWND32 hwnd, UINT32 id, BOOL32 sys )
286 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
287 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
288 (pTimer->timeout != 0)) break;
289 if (i >= NB_TIMERS) return FALSE;
290 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return FALSE;
291 if (!sys && (pTimer->msg != WM_TIMER)) return FALSE;
292 else if (sys && (pTimer->msg != WM_SYSTIMER)) return FALSE;
294 /* Delete the timer */
296 TIMER_ClearTimer( pTimer );
301 /***********************************************************************
302 * SetTimer16 (USER.10)
304 UINT16 WINAPI SetTimer16( HWND16 hwnd, UINT16 id, UINT16 timeout,
307 dprintf_timer( stddeb, "SetTimer16: %04x %d %d %08lx\n",
308 hwnd, id, timeout, (LONG)proc );
309 return TIMER_SetTimer( hwnd, id, timeout, (WNDPROC16)proc,
310 WIN_PROC_16, FALSE );
314 /***********************************************************************
315 * SetTimer32 (USER32.510)
317 UINT32 WINAPI SetTimer32( HWND32 hwnd, UINT32 id, UINT32 timeout,
320 dprintf_timer( stddeb, "SetTimer32: %04x %d %d %08lx\n",
321 hwnd, id, timeout, (LONG)proc );
322 return TIMER_SetTimer( hwnd, id, timeout, (WNDPROC16)proc,
323 WIN_PROC_32A, FALSE );
327 /***********************************************************************
328 * SetSystemTimer16 (USER.11)
330 UINT16 WINAPI SetSystemTimer16( HWND16 hwnd, UINT16 id, UINT16 timeout,
333 dprintf_timer( stddeb, "SetSystemTimer16: %04x %d %d %08lx\n",
334 hwnd, id, timeout, (LONG)proc );
335 return TIMER_SetTimer( hwnd, id, timeout, (WNDPROC16)proc,
340 /***********************************************************************
341 * SetSystemTimer32 (USER32.508)
343 UINT32 WINAPI SetSystemTimer32( HWND32 hwnd, UINT32 id, UINT32 timeout,
346 dprintf_timer( stddeb, "SetSystemTimer32: %04x %d %d %08lx\n",
347 hwnd, id, timeout, (LONG)proc );
348 return TIMER_SetTimer( hwnd, id, timeout, (WNDPROC16)proc,
349 WIN_PROC_32A, TRUE );
353 /***********************************************************************
354 * KillTimer16 (USER.12)
356 BOOL16 WINAPI KillTimer16( HWND16 hwnd, UINT16 id )
358 dprintf_timer(stddeb, "KillTimer16: %04x %d\n", hwnd, id );
359 return TIMER_KillTimer( hwnd, id, FALSE );
363 /***********************************************************************
364 * KillTimer32 (USER32.353)
366 BOOL32 WINAPI KillTimer32( HWND32 hwnd, UINT32 id )
368 dprintf_timer(stddeb, "KillTimer32: %04x %d\n", hwnd, id );
369 return TIMER_KillTimer( hwnd, id, FALSE );
373 /***********************************************************************
374 * KillSystemTimer16 (USER.182)
376 BOOL16 WINAPI KillSystemTimer16( HWND16 hwnd, UINT16 id )
378 dprintf_timer( stddeb, "KillSystemTimer16: %04x %d\n", hwnd, id );
379 return TIMER_KillTimer( hwnd, id, TRUE );
383 /***********************************************************************
384 * KillSystemTimer32 (USER32.352)
386 BOOL32 WINAPI KillSystemTimer32( HWND32 hwnd, UINT32 id )
388 dprintf_timer( stddeb, "KillSystemTimer32: %04x %d\n", hwnd, id );
389 return TIMER_KillTimer( hwnd, id, TRUE );