4 * Copyright 1993 Alexandre Julliard
10 /* #define DEBUG_TIMER */
14 typedef struct tagTIMER
18 WORD msg; /* WM_TIMER or WM_SYSTIMER */
21 struct tagTIMER *next;
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 /***********************************************************************
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;
52 TIMER * ptr = pNextTimer;
53 while (ptr->next && (pTimer->expires >= ptr->next->expires))
55 pTimer->next = ptr->next;
61 /***********************************************************************
64 * Remove the timer from the chain.
66 static void TIMER_RemoveTimer( TIMER * pTimer )
68 if (pTimer == pNextTimer) pNextTimer = pTimer->next;
71 TIMER * ptr = pNextTimer;
72 while (ptr && (ptr->next != pTimer)) ptr = ptr->next;
73 if (ptr) ptr->next = pTimer->next;
78 /***********************************************************************
81 void TIMER_SwitchQueue(HQUEUE old, HQUEUE new)
83 TIMER* pT = pNextTimer;
87 if( pT->hq == old ) pT->hq = new;
93 /***********************************************************************
96 * Trash all timers that are bound to the hwnd or hq
98 void TIMER_NukeTimers(HWND hwnd, HQUEUE hq)
100 HQUEUE hQToUpdate = ( hwnd ) ? GetTaskQueue( GetWindowTask( hwnd ) )
102 TIMER* pT = pNextTimer;
107 while( (hwnd && pT->hwnd == hwnd) ||
108 (hq && pT->hq == hq) )
110 QUEUE_DecTimerCount( hQToUpdate );
111 if( !(pT = pNextTimer = pNextTimer->next) )
115 /* pT points to the "good" timer */
117 while( (pTnext = pT->next) )
119 while( (hwnd && pTnext->hwnd == hwnd) ||
120 (hq && pTnext->hq == hq) )
122 QUEUE_DecTimerCount( hQToUpdate );
123 if( !(pT->next = pTnext->next) )
131 /***********************************************************************
132 * TIMER_RestartTimers
134 * Restart an expired timer.
136 static void TIMER_RestartTimer( TIMER * pTimer, DWORD curTime )
138 TIMER_RemoveTimer( pTimer );
139 pTimer->expires = curTime + pTimer->timeout;
140 TIMER_InsertTimer( pTimer );
144 /***********************************************************************
147 * Check whether a timer has expired, and create a message if necessary.
148 * Otherwise, return time until next timer expiration in 'next'.
149 * If 'hwnd' is not NULL, only consider timers for this window.
150 * If 'remove' is TRUE, remove all expired timers up to the returned one.
152 BOOL TIMER_CheckTimer( LONG *next, MSG *msg, HWND hwnd, BOOL remove )
154 TIMER * pTimer = pNextTimer;
155 DWORD curTime = GetTickCount();
157 if (hwnd) /* Find first timer for this window */
158 while (pTimer && (pTimer->hwnd != hwnd)) pTimer = pTimer->next;
160 if (!pTimer) *next = -1;
161 else *next = EXPIRE_TIME( pTimer, curTime );
162 if (*next != 0) return FALSE; /* No timer expired */
164 if (remove) /* Restart all timers before pTimer, and then pTimer itself */
166 while (pNextTimer != pTimer) TIMER_RestartTimer( pNextTimer, curTime );
167 TIMER_RestartTimer( pTimer, curTime );
170 dprintf_timer(stddeb, "Timer expired: %p, %04x, %04x, %04x, %08lx\n",
171 pTimer, pTimer->hwnd, pTimer->msg, pTimer->id, (DWORD)pTimer->proc);
172 /* Build the message */
173 msg->hwnd = pTimer->hwnd;
174 msg->message = pTimer->msg;
175 msg->wParam = pTimer->id;
176 msg->lParam = (LONG)pTimer->proc;
182 /***********************************************************************
185 static WORD TIMER_SetTimer( HWND hwnd, WORD id, WORD timeout,
186 FARPROC proc, BOOL sys )
191 if (!timeout) return 0;
192 /* if (!hwnd && !proc) return 0; */
194 /* Check if there's already a timer with the same hwnd and id */
196 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
197 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
198 (pTimer->timeout != 0))
200 /* Got one: set new values and return */
201 pTimer->timeout = timeout;
202 pTimer->expires = GetTickCount() + timeout;
204 TIMER_RemoveTimer( pTimer );
205 TIMER_InsertTimer( pTimer );
209 /* Find a free timer */
211 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
212 if (!pTimer->timeout) break;
214 if (i >= NB_TIMERS) return 0;
215 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return 0;
216 if (!hwnd) id = i + 1;
221 pTimer->hq = (hwnd) ? GetTaskQueue( GetWindowTask( hwnd ) )
223 pTimer->msg = sys ? WM_SYSTIMER : WM_TIMER;
225 pTimer->timeout = timeout;
226 pTimer->expires = GetTickCount() + timeout;
228 dprintf_timer(stddeb, "Timer added: %p, %04x, %04x, %04x, %08lx\n",
229 pTimer, pTimer->hwnd, pTimer->msg, pTimer->id, (DWORD)pTimer->proc);
230 TIMER_InsertTimer( pTimer );
231 QUEUE_IncTimerCount( pTimer->hq );
239 /***********************************************************************
242 static BOOL TIMER_KillTimer( HWND hwnd, WORD id, BOOL sys )
250 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
251 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
252 (pTimer->timeout != 0)) break;
253 if (i >= NB_TIMERS) return FALSE;
254 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return FALSE;
255 if (!sys && (pTimer->msg != WM_TIMER)) return FALSE;
256 else if (sys && (pTimer->msg != WM_SYSTIMER)) return FALSE;
258 /* Delete the timer */
267 TIMER_RemoveTimer( pTimer );
268 QUEUE_DecTimerCount( hq );
273 /***********************************************************************
276 WORD SetTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
278 dprintf_timer(stddeb, "SetTimer: %04x %d %d %08lx\n", hwnd, id, timeout, (LONG)proc );
279 return TIMER_SetTimer( hwnd, id, timeout, proc, FALSE );
283 /***********************************************************************
284 * SetSystemTimer (USER.11)
286 WORD SetSystemTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
288 dprintf_timer(stddeb, "SetSystemTimer: %04x %d %d %08lx\n",
289 hwnd, id, timeout, (LONG)proc );
290 return TIMER_SetTimer( hwnd, id, timeout, proc, TRUE );
294 /***********************************************************************
295 * KillTimer (USER.12)
297 BOOL KillTimer( HWND hwnd, WORD id )
299 dprintf_timer(stddeb, "KillTimer: %04x %d\n", hwnd, id );
300 return TIMER_KillTimer( hwnd, id, FALSE );
304 /***********************************************************************
305 * KillSystemTimer (USER.182)
307 BOOL KillSystemTimer( HWND hwnd, WORD id )
309 dprintf_timer(stddeb, "KillSystemTimer: %04x %d\n", hwnd, id );
310 return TIMER_KillTimer( hwnd, id, TRUE );