Release 0.5
[wine/multimedia.git] / windows / timer.c
blobda649610fd23a11ec0e7ce0b42e321aaee96fc00
1 /*
2 * Timer functions
4 * Copyright 1993 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
9 #include "windows.h"
10 #include "message.h"
13 typedef struct tagTIMER
15 HWND hwnd;
16 WORD msg; /* WM_TIMER or WM_SYSTIMER */
17 WORD id;
18 WORD timeout;
19 struct tagTIMER *next;
20 DWORD expires;
21 FARPROC proc;
22 } TIMER;
24 #define NB_TIMERS 34
25 #define NB_RESERVED_TIMERS 2 /* for SetSystemTimer */
27 static TIMER TimersArray[NB_TIMERS];
29 static TIMER * pNextTimer = NULL; /* Next timer to expire */
32 /***********************************************************************
33 * TIMER_InsertTimer
35 * Insert the timer at its place in the chain.
37 static void TIMER_InsertTimer( TIMER * pTimer )
39 if (!pNextTimer || (pTimer->expires < pNextTimer->expires))
41 pTimer->next = pNextTimer;
42 pNextTimer = pTimer;
44 else
46 TIMER * ptr = pNextTimer;
47 while (ptr->next && (pTimer->expires >= ptr->next->expires))
48 ptr = ptr->next;
49 pTimer->next = ptr;
50 ptr->next = pTimer;
55 /***********************************************************************
56 * TIMER_RemoveTimer
58 * Remove the timer from the chain.
60 static void TIMER_RemoveTimer( TIMER * pTimer )
62 if (pTimer == pNextTimer) pNextTimer = pTimer->next;
63 else
65 TIMER * ptr = pNextTimer;
66 while (ptr && (ptr->next != pTimer)) ptr = ptr->next;
67 if (ptr) ptr->next = pTimer->next;
69 pTimer->next = NULL;
73 /***********************************************************************
74 * TIMER_NextExpire
76 * Return time until next timer expiration (-1 if none).
78 static DWORD TIMER_NextExpire( DWORD curTime )
80 if (!pNextTimer) return -1;
81 if (pNextTimer->expires <= curTime) return 0;
82 return pNextTimer->expires - curTime;
86 /***********************************************************************
87 * TIMER_CheckTimer
89 * Check whether a timer has expired, and post a message if necessary.
90 * Return TRUE if msg posted, and return time until next expiration in 'next'.
92 BOOL TIMER_CheckTimer( DWORD *next )
94 TIMER * pTimer = pNextTimer;
95 DWORD curTime = GetTickCount();
97 if ((*next = TIMER_NextExpire( curTime )) != 0) return FALSE;
99 PostMessage( pTimer->hwnd, pTimer->msg, pTimer->id, (LONG)pTimer->proc );
100 TIMER_RemoveTimer( pTimer );
102 /* If timeout == 0, the timer has been removed by KillTimer */
103 if (pTimer->timeout)
105 /* Restart the timer */
106 pTimer->expires = curTime + pTimer->timeout;
107 TIMER_InsertTimer( pTimer );
109 *next = TIMER_NextExpire( curTime );
110 return TRUE;
114 /***********************************************************************
115 * TIMER_SetTimer
117 static WORD TIMER_SetTimer( HWND hwnd, WORD id, WORD timeout,
118 FARPROC proc, BOOL sys )
120 int i;
121 TIMER * pTimer;
123 if (!timeout) return 0;
124 if (!hwnd && !proc) return 0;
126 /* Find a free timer */
128 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
129 if (!pTimer->timeout) break;
131 if (i >= NB_TIMERS) return 0;
132 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return 0;
133 if (!hwnd) id = i + 1;
135 /* Add the timer */
137 pTimer->hwnd = hwnd;
138 pTimer->msg = sys ? WM_SYSTIMER : WM_TIMER;
139 pTimer->id = id;
140 pTimer->timeout = timeout;
141 pTimer->expires = GetTickCount() + timeout;
142 pTimer->proc = proc;
143 TIMER_InsertTimer( pTimer );
144 MSG_IncTimerCount( GetTaskQueue(0) );
145 return id;
149 /***********************************************************************
150 * TIMER_KillTimer
152 static BOOL TIMER_KillTimer( HWND hwnd, WORD id, BOOL sys )
154 int i;
155 TIMER * pTimer;
157 /* Find the timer */
159 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
160 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
161 (pTimer->timeout != 0)) break;
162 if (i >= NB_TIMERS) return FALSE;
163 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return FALSE;
164 if (!sys && (pTimer->msg != WM_TIMER)) return FALSE;
165 else if (sys && (pTimer->msg != WM_SYSTIMER)) return FALSE;
167 /* Delete the timer */
169 pTimer->hwnd = 0;
170 pTimer->msg = 0;
171 pTimer->id = 0;
172 pTimer->timeout = 0;
173 pTimer->proc = 0;
174 TIMER_RemoveTimer( pTimer );
175 MSG_DecTimerCount( GetTaskQueue(0) );
176 return TRUE;
180 /***********************************************************************
181 * SetTimer (USER.10)
183 WORD SetTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
185 #ifdef DEBUG_TIMER
186 printf( "SetTimer: %d %d %d %p\n", hwnd, id, timeout, proc );
187 #endif
188 return TIMER_SetTimer( hwnd, id, timeout, proc, FALSE );
192 /***********************************************************************
193 * SetSystemTimer (USER.11)
195 WORD SetSystemTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
197 #ifdef DEBUG_TIMER
198 printf( "SetSystemTimer: %d %d %d %p\n", hwnd, id, timeout, proc );
199 #endif
200 return TIMER_SetTimer( hwnd, id, timeout, proc, TRUE );
204 /***********************************************************************
205 * KillTimer (USER.12)
207 BOOL KillTimer( HWND hwnd, WORD id )
209 #ifdef DEBUG_TIMER
210 printf( "KillTimer: %d %d\n", hwnd, id );
211 #endif
212 return TIMER_KillTimer( hwnd, id, FALSE );
216 /***********************************************************************
217 * KillSystemTimer (USER.182)
219 BOOL KillSystemTimer( HWND hwnd, WORD id )
221 #ifdef DEBUG_TIMER
222 printf( "KillSystemTimer: %d %d\n", hwnd, id );
223 #endif
224 return TIMER_KillTimer( hwnd, id, TRUE );