4 * Copyright 1993 Alexandre Julliard
14 DEFAULT_DEBUG_CHANNEL(timer
)
17 typedef struct tagTIMER
21 UINT16 msg
; /* WM_TIMER or WM_SYSTIMER */
24 struct tagTIMER
*next
;
25 DWORD expires
; /* Next expiration, or 0 if already expired */
30 #define NB_RESERVED_TIMERS 2 /* for SetSystemTimer */
32 static TIMER TimersArray
[NB_TIMERS
];
34 static TIMER
* pNextTimer
= NULL
; /* Next timer to expire */
36 static CRITICAL_SECTION csTimer
;
38 /* Duration from 'time' until expiration of the timer */
39 #define EXPIRE_TIME(pTimer,time) \
40 (((pTimer)->expires <= (time)) ? 0 : (pTimer)->expires - (time))
43 /***********************************************************************
46 * Initialize critical section for the timer.
48 BOOL
TIMER_Init( void )
50 InitializeCriticalSection( &csTimer
);
51 MakeCriticalSectionGlobal( &csTimer
);
57 /***********************************************************************
60 * Insert the timer at its place in the chain.
62 static void TIMER_InsertTimer( TIMER
* pTimer
)
64 EnterCriticalSection( &csTimer
);
66 if (!pNextTimer
|| (pTimer
->expires
< pNextTimer
->expires
))
68 pTimer
->next
= pNextTimer
;
73 TIMER
* ptr
= pNextTimer
;
74 while (ptr
->next
&& (pTimer
->expires
>= ptr
->next
->expires
))
76 pTimer
->next
= ptr
->next
;
80 LeaveCriticalSection( &csTimer
);
84 /***********************************************************************
87 * Remove the timer from the chain.
89 static void TIMER_RemoveTimer( TIMER
* pTimer
)
91 TIMER
**ppTimer
= &pNextTimer
;
93 EnterCriticalSection( &csTimer
);
95 while (*ppTimer
&& (*ppTimer
!= pTimer
)) ppTimer
= &(*ppTimer
)->next
;
96 if (*ppTimer
) *ppTimer
= pTimer
->next
;
99 LeaveCriticalSection( &csTimer
);
101 if (!pTimer
->expires
) QUEUE_DecTimerCount( pTimer
->hq
);
105 /***********************************************************************
108 * Clear and remove a timer.
110 static void TIMER_ClearTimer( TIMER
* pTimer
)
112 TIMER_RemoveTimer( pTimer
);
117 WINPROC_FreeProc( pTimer
->proc
, WIN_PROC_TIMER
);
121 /***********************************************************************
122 * TIMER_RemoveWindowTimers
124 * Remove all timers for a given window.
126 void TIMER_RemoveWindowTimers( HWND hwnd
)
131 EnterCriticalSection( &csTimer
);
133 for (i
= NB_TIMERS
, pTimer
= TimersArray
; i
> 0; i
--, pTimer
++)
134 if ((pTimer
->hwnd
== hwnd
) && pTimer
->timeout
)
135 TIMER_ClearTimer( pTimer
);
137 LeaveCriticalSection( &csTimer
);
141 /***********************************************************************
142 * TIMER_RemoveQueueTimers
144 * Remove all timers for a given queue.
146 void TIMER_RemoveQueueTimers( HQUEUE16 hqueue
)
151 EnterCriticalSection( &csTimer
);
153 for (i
= NB_TIMERS
, pTimer
= TimersArray
; i
> 0; i
--, pTimer
++)
154 if ((pTimer
->hq
== hqueue
) && pTimer
->timeout
)
155 TIMER_ClearTimer( pTimer
);
157 LeaveCriticalSection( &csTimer
);
161 /***********************************************************************
162 * TIMER_RestartTimers
164 * Restart an expired timer.
166 static void TIMER_RestartTimer( TIMER
* pTimer
, DWORD curTime
)
168 TIMER_RemoveTimer( pTimer
);
169 pTimer
->expires
= curTime
+ pTimer
->timeout
;
170 TIMER_InsertTimer( pTimer
);
173 /***********************************************************************
176 * Mark expired timers and wake the appropriate queues.
178 static void CALLBACK
TIMER_CheckTimers( ULONG_PTR forceTimer
)
180 static HANDLE ServiceHandle
= INVALID_HANDLE_VALUE
;
181 static LONG ServiceTimeout
= 0;
184 DWORD curTime
= GetTickCount();
186 EnterCriticalSection( &csTimer
);
188 TRACE(timer
, "Called at %ld (%s)\n", curTime
, forceTimer
? "manual" : "auto" );
192 while (pTimer
&& !pTimer
->expires
) /* Skip already expired timers */
193 pTimer
= pTimer
->next
;
194 while (pTimer
&& (pTimer
->expires
<= curTime
))
197 QUEUE_IncTimerCount( pTimer
->hq
);
198 pTimer
= pTimer
->next
;
201 /* Install service callback with appropriate timeout, so that
202 we get called again once the next timer has expired */
206 LONG timeout
= pTimer
->expires
- curTime
;
208 if ( forceTimer
|| timeout
!= ServiceTimeout
)
210 if ( ServiceHandle
!= INVALID_HANDLE_VALUE
)
211 SERVICE_Delete( ServiceHandle
);
213 ServiceHandle
= SERVICE_AddTimer( timeout
* 1000L,
214 TIMER_CheckTimers
, FALSE
);
215 ServiceTimeout
= timeout
;
217 TRACE(timer
, "Installed service callback with timeout %ld\n", timeout
);
222 if ( ServiceHandle
!= INVALID_HANDLE_VALUE
)
224 SERVICE_Delete( ServiceHandle
);
225 ServiceHandle
= INVALID_HANDLE_VALUE
;
228 TRACE(timer
, "Deleted service callback\n" );
232 LeaveCriticalSection( &csTimer
);
236 /***********************************************************************
239 * Build a message for an expired timer.
241 BOOL
TIMER_GetTimerMsg( MSG
*msg
, HWND hwnd
,
242 HQUEUE16 hQueue
, BOOL remove
)
245 DWORD curTime
= GetTickCount();
247 EnterCriticalSection( &csTimer
);
251 if (hwnd
) /* Find first timer for this window */
252 while (pTimer
&& (pTimer
->hwnd
!= hwnd
)) pTimer
= pTimer
->next
;
253 else /* Find first timer for this queue */
254 while (pTimer
&& (pTimer
->hq
!= hQueue
)) pTimer
= pTimer
->next
;
256 if (!pTimer
|| (pTimer
->expires
> curTime
))
258 LeaveCriticalSection( &csTimer
);
259 return FALSE
; /* No timer */
262 TRACE(timer
, "Timer expired: %04x, %04x, %04x, %08lx\n",
263 pTimer
->hwnd
, pTimer
->msg
, pTimer
->id
, (DWORD
)pTimer
->proc
);
267 TIMER_RestartTimer( pTimer
, curTime
); /* Restart it */
268 TIMER_CheckTimers( TRUE
);
271 /* Build the message */
272 msg
->hwnd
= pTimer
->hwnd
;
273 msg
->message
= pTimer
->msg
;
274 msg
->wParam
= pTimer
->id
;
275 msg
->lParam
= (LONG
)pTimer
->proc
;
278 LeaveCriticalSection( &csTimer
);
284 /***********************************************************************
287 static UINT
TIMER_SetTimer( HWND hwnd
, UINT id
, UINT timeout
,
288 WNDPROC16 proc
, WINDOWPROCTYPE type
, BOOL sys
)
293 if (!timeout
) return 0;
295 EnterCriticalSection( &csTimer
);
297 /* Check if there's already a timer with the same hwnd and id */
299 for (i
= 0, pTimer
= TimersArray
; i
< NB_TIMERS
; i
++, pTimer
++)
300 if ((pTimer
->hwnd
== hwnd
) && (pTimer
->id
== id
) &&
301 (pTimer
->timeout
!= 0))
303 /* Got one: set new values and return */
304 TIMER_RemoveTimer( pTimer
);
305 pTimer
->timeout
= timeout
;
306 WINPROC_FreeProc( pTimer
->proc
, WIN_PROC_TIMER
);
307 pTimer
->proc
= (HWINDOWPROC
)0;
308 if (proc
) WINPROC_SetProc( &pTimer
->proc
, proc
,
309 type
, WIN_PROC_TIMER
);
310 pTimer
->expires
= GetTickCount() + timeout
;
311 TIMER_InsertTimer( pTimer
);
312 TIMER_CheckTimers( TRUE
);
313 LeaveCriticalSection( &csTimer
);
317 /* Find a free timer */
319 for (i
= 0, pTimer
= TimersArray
; i
< NB_TIMERS
; i
++, pTimer
++)
320 if (!pTimer
->timeout
) break;
322 if ( (i
>= NB_TIMERS
) ||
323 (!sys
&& (i
>= NB_TIMERS
-NB_RESERVED_TIMERS
)) )
325 LeaveCriticalSection( &csTimer
);
329 if (!hwnd
) id
= i
+ 1;
334 pTimer
->hq
= (hwnd
) ? GetThreadQueue16( GetWindowThreadProcessId( hwnd
, NULL
) )
336 pTimer
->msg
= sys
? WM_SYSTIMER
: WM_TIMER
;
338 pTimer
->timeout
= timeout
;
339 pTimer
->expires
= GetTickCount() + timeout
;
340 pTimer
->proc
= (HWINDOWPROC
)0;
341 if (proc
) WINPROC_SetProc( &pTimer
->proc
, proc
, type
, WIN_PROC_TIMER
);
342 TRACE(timer
, "Timer added: %p, %04x, %04x, %04x, %08lx\n",
343 pTimer
, pTimer
->hwnd
, pTimer
->msg
, pTimer
->id
,
344 (DWORD
)pTimer
->proc
);
345 TIMER_InsertTimer( pTimer
);
346 TIMER_CheckTimers( TRUE
);
348 LeaveCriticalSection( &csTimer
);
350 if (!id
) return TRUE
;
355 /***********************************************************************
358 static BOOL
TIMER_KillTimer( HWND hwnd
, UINT id
, BOOL sys
)
363 EnterCriticalSection( &csTimer
);
367 for (i
= 0, pTimer
= TimersArray
; i
< NB_TIMERS
; i
++, pTimer
++)
368 if ((pTimer
->hwnd
== hwnd
) && (pTimer
->id
== id
) &&
369 (pTimer
->timeout
!= 0)) break;
371 if ( (i
>= NB_TIMERS
) ||
372 (!sys
&& (i
>= NB_TIMERS
-NB_RESERVED_TIMERS
)) ||
373 (!sys
&& (pTimer
->msg
!= WM_TIMER
)) ||
374 (sys
&& (pTimer
->msg
!= WM_SYSTIMER
)) )
376 LeaveCriticalSection( &csTimer
);
380 /* Delete the timer */
382 TIMER_ClearTimer( pTimer
);
384 LeaveCriticalSection( &csTimer
);
390 /***********************************************************************
391 * SetTimer16 (USER.10)
393 UINT16 WINAPI
SetTimer16( HWND16 hwnd
, UINT16 id
, UINT16 timeout
,
396 TRACE(timer
, "%04x %d %d %08lx\n",
397 hwnd
, id
, timeout
, (LONG
)proc
);
398 return TIMER_SetTimer( hwnd
, id
, timeout
, (WNDPROC16
)proc
,
399 WIN_PROC_16
, FALSE
);
403 /***********************************************************************
404 * SetTimer32 (USER32.511)
406 UINT WINAPI
SetTimer( HWND hwnd
, UINT id
, UINT timeout
,
409 TRACE(timer
, "%04x %d %d %08lx\n",
410 hwnd
, id
, timeout
, (LONG
)proc
);
411 return TIMER_SetTimer( hwnd
, id
, timeout
, (WNDPROC16
)proc
,
412 WIN_PROC_32A
, FALSE
);
416 /***********************************************************************
417 * SetSystemTimer16 (USER.11)
419 UINT16 WINAPI
SetSystemTimer16( HWND16 hwnd
, UINT16 id
, UINT16 timeout
,
422 TRACE(timer
, "%04x %d %d %08lx\n",
423 hwnd
, id
, timeout
, (LONG
)proc
);
424 return TIMER_SetTimer( hwnd
, id
, timeout
, (WNDPROC16
)proc
,
429 /***********************************************************************
430 * SetSystemTimer32 (USER32.509)
432 UINT WINAPI
SetSystemTimer( HWND hwnd
, UINT id
, UINT timeout
,
435 TRACE(timer
, "%04x %d %d %08lx\n",
436 hwnd
, id
, timeout
, (LONG
)proc
);
437 return TIMER_SetTimer( hwnd
, id
, timeout
, (WNDPROC16
)proc
,
438 WIN_PROC_32A
, TRUE
);
442 /***********************************************************************
443 * KillTimer16 (USER.12)
445 BOOL16 WINAPI
KillTimer16( HWND16 hwnd
, UINT16 id
)
447 TRACE(timer
, "%04x %d\n", hwnd
, id
);
448 return TIMER_KillTimer( hwnd
, id
, FALSE
);
452 /***********************************************************************
453 * KillTimer32 (USER32.354)
455 BOOL WINAPI
KillTimer( HWND hwnd
, UINT id
)
457 TRACE(timer
, "%04x %d\n", hwnd
, id
);
458 return TIMER_KillTimer( hwnd
, id
, FALSE
);
462 /***********************************************************************
463 * KillSystemTimer16 (USER.182)
465 BOOL16 WINAPI
KillSystemTimer16( HWND16 hwnd
, UINT16 id
)
467 TRACE(timer
, "%04x %d\n", hwnd
, id
);
468 return TIMER_KillTimer( hwnd
, id
, TRUE
);
472 /***********************************************************************
473 * KillSystemTimer32 (USER32.353)
475 BOOL WINAPI
KillSystemTimer( HWND hwnd
, UINT id
)
477 TRACE(timer
, "%04x %d\n", hwnd
, id
);
478 return TIMER_KillTimer( hwnd
, id
, TRUE
);