Release 950522
[wine.git] / windows / timer.c
blob96fff6a39d245e3ad2d5121bbde3bac3adf58ee2
1 /*
2 * Timer functions
4 * Copyright 1993 Alexandre Julliard
5 */
7 #include "windows.h"
8 #include "message.h"
9 #include "stddebug.h"
10 /* #define DEBUG_TIMER */
11 #include "debug.h"
14 typedef struct tagTIMER
16 HWND hwnd;
17 WORD msg; /* WM_TIMER or WM_SYSTIMER */
18 WORD id;
19 WORD timeout;
20 struct tagTIMER *next;
21 DWORD expires;
22 FARPROC proc;
23 } TIMER;
25 #define NB_TIMERS 34
26 #define NB_RESERVED_TIMERS 2 /* for SetSystemTimer */
28 static TIMER TimersArray[NB_TIMERS];
30 static TIMER * pNextTimer = NULL; /* Next timer to expire */
32 /* Duration from 'time' until expiration of the timer */
33 #define EXPIRE_TIME(pTimer,time) \
34 (((pTimer)->expires <= (time)) ? 0 : (pTimer)->expires - (time))
37 /***********************************************************************
38 * TIMER_InsertTimer
40 * Insert the timer at its place in the chain.
42 static void TIMER_InsertTimer( TIMER * pTimer )
44 if (!pNextTimer || (pTimer->expires < pNextTimer->expires))
46 pTimer->next = pNextTimer;
47 pNextTimer = pTimer;
49 else
51 TIMER * ptr = pNextTimer;
52 while (ptr->next && (pTimer->expires >= ptr->next->expires))
53 ptr = ptr->next;
54 pTimer->next = ptr->next;
55 ptr->next = pTimer;
60 /***********************************************************************
61 * TIMER_RemoveTimer
63 * Remove the timer from the chain.
65 static void TIMER_RemoveTimer( TIMER * pTimer )
67 if (pTimer == pNextTimer) pNextTimer = pTimer->next;
68 else
70 TIMER * ptr = pNextTimer;
71 while (ptr && (ptr->next != pTimer)) ptr = ptr->next;
72 if (ptr) ptr->next = pTimer->next;
74 pTimer->next = NULL;
78 /***********************************************************************
79 * TIMER_RestartTimers
81 * Restart an expired timer.
83 static void TIMER_RestartTimer( TIMER * pTimer, DWORD curTime )
85 TIMER_RemoveTimer( pTimer );
86 pTimer->expires = curTime + pTimer->timeout;
87 TIMER_InsertTimer( pTimer );
91 /***********************************************************************
92 * TIMER_CheckTimer
94 * Check whether a timer has expired, and create a message if necessary.
95 * Otherwise, return time until next timer expiration in 'next'.
96 * If 'hwnd' is not NULL, only consider timers for this window.
97 * If 'remove' is TRUE, remove all expired timers up to the returned one.
99 BOOL TIMER_CheckTimer( LONG *next, MSG *msg, HWND hwnd, BOOL remove )
101 TIMER * pTimer = pNextTimer;
102 DWORD curTime = GetTickCount();
104 if (hwnd) /* Find first timer for this window */
105 while (pTimer && (pTimer->hwnd != hwnd)) pTimer = pTimer->next;
107 if (!pTimer) *next = -1;
108 else *next = EXPIRE_TIME( pTimer, curTime );
109 if (*next != 0) return FALSE; /* No timer expired */
111 if (remove) /* Restart all timers before pTimer, and then pTimer itself */
113 while (pNextTimer != pTimer) TIMER_RestartTimer( pNextTimer, curTime );
114 TIMER_RestartTimer( pTimer, curTime );
117 /* Build the message */
118 msg->hwnd = pTimer->hwnd;
119 msg->message = pTimer->msg;
120 msg->wParam = pTimer->id;
121 msg->lParam = (LONG)pTimer->proc;
122 msg->time = curTime;
123 return TRUE;
127 /***********************************************************************
128 * TIMER_SetTimer
130 static WORD TIMER_SetTimer( HWND hwnd, WORD id, WORD timeout,
131 FARPROC proc, BOOL sys )
133 int i;
134 TIMER * pTimer;
136 if (!timeout) return 0;
137 /* if (!hwnd && !proc) return 0; */
139 /* Check if there's already a timer with the same hwnd and id */
141 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
142 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
143 (pTimer->timeout != 0))
145 /* Got one: set new values and return */
146 pTimer->timeout = timeout;
147 pTimer->expires = GetTickCount() + timeout;
148 pTimer->proc = proc;
149 TIMER_RemoveTimer( pTimer );
150 TIMER_InsertTimer( pTimer );
151 return id;
154 /* Find a free timer */
156 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
157 if (!pTimer->timeout) break;
159 if (i >= NB_TIMERS) return 0;
160 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return 0;
161 if (!hwnd) id = i + 1;
163 /* Add the timer */
165 pTimer->hwnd = hwnd;
166 pTimer->msg = sys ? WM_SYSTIMER : WM_TIMER;
167 pTimer->id = id;
168 pTimer->timeout = timeout;
169 pTimer->expires = GetTickCount() + timeout;
170 pTimer->proc = proc;
171 TIMER_InsertTimer( pTimer );
172 MSG_IncTimerCount( GetTaskQueue(0) );
173 if (!id)
174 return TRUE;
175 else
176 return id;
180 /***********************************************************************
181 * TIMER_KillTimer
183 static BOOL TIMER_KillTimer( HWND hwnd, WORD id, BOOL sys )
185 int i;
186 TIMER * pTimer;
188 /* Find the timer */
190 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
191 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
192 (pTimer->timeout != 0)) break;
193 if (i >= NB_TIMERS) return FALSE;
194 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return FALSE;
195 if (!sys && (pTimer->msg != WM_TIMER)) return FALSE;
196 else if (sys && (pTimer->msg != WM_SYSTIMER)) return FALSE;
198 /* Delete the timer */
200 pTimer->hwnd = 0;
201 pTimer->msg = 0;
202 pTimer->id = 0;
203 pTimer->timeout = 0;
204 pTimer->proc = 0;
205 TIMER_RemoveTimer( pTimer );
206 MSG_DecTimerCount( GetTaskQueue(0) );
207 return TRUE;
211 /***********************************************************************
212 * SetTimer (USER.10)
214 WORD SetTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
216 dprintf_timer(stddeb, "SetTimer: %d %d %d %08lx\n", hwnd, id, timeout, (LONG)proc );
217 return TIMER_SetTimer( hwnd, id, timeout, proc, FALSE );
221 /***********************************************************************
222 * SetSystemTimer (USER.11)
224 WORD SetSystemTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
226 dprintf_timer(stddeb, "SetSystemTimer: %d %d %d %08lx\n",
227 hwnd, id, timeout, (LONG)proc );
228 return TIMER_SetTimer( hwnd, id, timeout, proc, TRUE );
232 /***********************************************************************
233 * KillTimer (USER.12)
235 BOOL KillTimer( HWND hwnd, WORD id )
237 dprintf_timer(stddeb, "KillTimer: %d %d\n", hwnd, id );
238 return TIMER_KillTimer( hwnd, id, FALSE );
242 /***********************************************************************
243 * KillSystemTimer (USER.182)
245 BOOL KillSystemTimer( HWND hwnd, WORD id )
247 dprintf_timer(stddeb, "KillSystemTimer: %d %d\n", hwnd, id );
248 return TIMER_KillTimer( hwnd, id, TRUE );