Release 990523.
[wine/multimedia.git] / windows / timer.c
blob5e68ba49c2369989946eeb8d646fab6fc150d4f3
1 /*
2 * Timer functions
4 * Copyright 1993 Alexandre Julliard
5 */
7 #include "winuser.h"
8 #include "queue.h"
9 #include "task.h"
10 #include "winproc.h"
11 #include "services.h"
12 #include "debug.h"
14 DEFAULT_DEBUG_CHANNEL(timer)
17 typedef struct tagTIMER
19 HWND hwnd;
20 HQUEUE16 hq;
21 UINT16 msg; /* WM_TIMER or WM_SYSTIMER */
22 UINT id;
23 UINT timeout;
24 struct tagTIMER *next;
25 DWORD expires; /* Next expiration, or 0 if already expired */
26 HWINDOWPROC proc;
27 } TIMER;
29 #define NB_TIMERS 34
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 /***********************************************************************
44 * TIMER_Init
46 * Initialize critical section for the timer.
48 BOOL TIMER_Init( void )
50 InitializeCriticalSection( &csTimer );
51 MakeCriticalSectionGlobal( &csTimer );
53 return TRUE;
57 /***********************************************************************
58 * TIMER_InsertTimer
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;
69 pNextTimer = pTimer;
71 else
73 TIMER * ptr = pNextTimer;
74 while (ptr->next && (pTimer->expires >= ptr->next->expires))
75 ptr = ptr->next;
76 pTimer->next = ptr->next;
77 ptr->next = pTimer;
80 LeaveCriticalSection( &csTimer );
84 /***********************************************************************
85 * TIMER_RemoveTimer
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;
97 pTimer->next = NULL;
99 LeaveCriticalSection( &csTimer );
101 if (!pTimer->expires) QUEUE_DecTimerCount( pTimer->hq );
105 /***********************************************************************
106 * TIMER_ClearTimer
108 * Clear and remove a timer.
110 static void TIMER_ClearTimer( TIMER * pTimer )
112 TIMER_RemoveTimer( pTimer );
113 pTimer->hwnd = 0;
114 pTimer->msg = 0;
115 pTimer->id = 0;
116 pTimer->timeout = 0;
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 )
128 int i;
129 TIMER *pTimer;
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 )
148 int i;
149 TIMER *pTimer;
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 /***********************************************************************
174 * TIMER_CheckTimers
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;
183 TIMER *pTimer;
184 DWORD curTime = GetTickCount();
186 EnterCriticalSection( &csTimer );
188 TRACE(timer, "Called at %ld (%s)\n", curTime, forceTimer? "manual" : "auto" );
190 pTimer = pNextTimer;
192 while (pTimer && !pTimer->expires) /* Skip already expired timers */
193 pTimer = pTimer->next;
194 while (pTimer && (pTimer->expires <= curTime))
196 pTimer->expires = 0;
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 */
204 if (pTimer)
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 );
220 else
222 if ( ServiceHandle != INVALID_HANDLE_VALUE )
224 SERVICE_Delete( ServiceHandle );
225 ServiceHandle = INVALID_HANDLE_VALUE;
226 ServiceTimeout = 0;
228 TRACE(timer, "Deleted service callback\n" );
232 LeaveCriticalSection( &csTimer );
236 /***********************************************************************
237 * TIMER_GetTimerMsg
239 * Build a message for an expired timer.
241 BOOL TIMER_GetTimerMsg( MSG *msg, HWND hwnd,
242 HQUEUE16 hQueue, BOOL remove )
244 TIMER *pTimer;
245 DWORD curTime = GetTickCount();
247 EnterCriticalSection( &csTimer );
249 pTimer = pNextTimer;
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);
265 if (remove)
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;
276 msg->time = curTime;
278 LeaveCriticalSection( &csTimer );
280 return TRUE;
284 /***********************************************************************
285 * TIMER_SetTimer
287 static UINT TIMER_SetTimer( HWND hwnd, UINT id, UINT timeout,
288 WNDPROC16 proc, WINDOWPROCTYPE type, BOOL sys )
290 int i;
291 TIMER * pTimer;
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 );
314 return id;
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 );
326 return 0;
329 if (!hwnd) id = i + 1;
331 /* Add the timer */
333 pTimer->hwnd = hwnd;
334 pTimer->hq = (hwnd) ? GetThreadQueue16( GetWindowThreadProcessId( hwnd, NULL ) )
335 : GetFastQueue16( );
336 pTimer->msg = sys ? WM_SYSTIMER : WM_TIMER;
337 pTimer->id = id;
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;
351 else return id;
355 /***********************************************************************
356 * TIMER_KillTimer
358 static BOOL TIMER_KillTimer( HWND hwnd, UINT id, BOOL sys )
360 int i;
361 TIMER * pTimer;
363 EnterCriticalSection( &csTimer );
365 /* Find the timer */
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 );
377 return FALSE;
380 /* Delete the timer */
382 TIMER_ClearTimer( pTimer );
384 LeaveCriticalSection( &csTimer );
386 return TRUE;
390 /***********************************************************************
391 * SetTimer16 (USER.10)
393 UINT16 WINAPI SetTimer16( HWND16 hwnd, UINT16 id, UINT16 timeout,
394 TIMERPROC16 proc )
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,
407 TIMERPROC proc )
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,
420 TIMERPROC16 proc )
422 TRACE(timer, "%04x %d %d %08lx\n",
423 hwnd, id, timeout, (LONG)proc );
424 return TIMER_SetTimer( hwnd, id, timeout, (WNDPROC16)proc,
425 WIN_PROC_16, TRUE );
429 /***********************************************************************
430 * SetSystemTimer32 (USER32.509)
432 UINT WINAPI SetSystemTimer( HWND hwnd, UINT id, UINT timeout,
433 TIMERPROC proc )
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 );