4 * Copyright 1993 Alexandre Julliard
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
12 /* #define DEBUG_TIMER /* */
13 /* #undef DEBUG_TIMER /* */
17 typedef struct tagTIMER
20 WORD msg; /* WM_TIMER or WM_SYSTIMER */
23 struct tagTIMER *next;
29 #define NB_RESERVED_TIMERS 2 /* for SetSystemTimer */
31 static TIMER TimersArray[NB_TIMERS];
33 static TIMER * pNextTimer = NULL; /* Next timer to expire */
35 /* Duration from 'time' until expiration of the timer */
36 #define EXPIRE_TIME(pTimer,time) \
37 (((pTimer)->expires <= (time)) ? 0 : (pTimer)->expires - (time))
40 /***********************************************************************
43 * Insert the timer at its place in the chain.
45 static void TIMER_InsertTimer( TIMER * pTimer )
47 if (!pNextTimer || (pTimer->expires < pNextTimer->expires))
49 pTimer->next = pNextTimer;
54 TIMER * ptr = pNextTimer;
55 while (ptr->next && (pTimer->expires >= ptr->next->expires))
57 pTimer->next = ptr->next;
63 /***********************************************************************
66 * Remove the timer from the chain.
68 static void TIMER_RemoveTimer( TIMER * pTimer )
70 if (pTimer == pNextTimer) pNextTimer = pTimer->next;
73 TIMER * ptr = pNextTimer;
74 while (ptr && (ptr->next != pTimer)) ptr = ptr->next;
75 if (ptr) ptr->next = pTimer->next;
81 /***********************************************************************
84 * Restart an expired timer.
86 static void TIMER_RestartTimer( TIMER * pTimer, DWORD curTime )
88 TIMER_RemoveTimer( pTimer );
89 pTimer->expires = curTime + pTimer->timeout;
90 TIMER_InsertTimer( pTimer );
94 /***********************************************************************
97 * Check whether a timer has expired, and create a message if necessary.
98 * Otherwise, return time until next timer expiration in 'next'.
99 * If 'hwnd' is not NULL, only consider timers for this window.
100 * If 'remove' is TRUE, remove all expired timers up to the returned one.
102 BOOL TIMER_CheckTimer( LONG *next, MSG *msg, HWND hwnd, BOOL remove )
104 TIMER * pTimer = pNextTimer;
105 DWORD curTime = GetTickCount();
107 if (hwnd) /* Find first timer for this window */
108 while (pTimer && (pTimer->hwnd != hwnd)) pTimer = pTimer->next;
110 if (!pTimer) *next = -1;
111 else *next = EXPIRE_TIME( pTimer, curTime );
112 if (*next != 0) return FALSE; /* No timer expired */
114 if (remove) /* Restart all timers before pTimer, and then pTimer itself */
116 while (pNextTimer != pTimer) TIMER_RestartTimer( pNextTimer, curTime );
117 TIMER_RestartTimer( pTimer, curTime );
120 /* Build the message */
121 msg->hwnd = pTimer->hwnd;
122 msg->message = pTimer->msg;
123 msg->wParam = pTimer->id;
124 msg->lParam = (LONG)pTimer->proc;
130 /***********************************************************************
133 static WORD TIMER_SetTimer( HWND hwnd, WORD id, WORD timeout,
134 FARPROC proc, BOOL sys )
139 if (!timeout) return 0;
140 /* if (!hwnd && !proc) return 0; */
142 /* Check if there's already a timer with the same hwnd and id */
144 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
145 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
146 (pTimer->timeout != 0))
148 /* Got one: set new values and return */
149 pTimer->timeout = timeout;
150 pTimer->expires = GetTickCount() + timeout;
152 TIMER_RemoveTimer( pTimer );
153 TIMER_InsertTimer( pTimer );
157 /* Find a free timer */
159 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
160 if (!pTimer->timeout) break;
162 if (i >= NB_TIMERS) return 0;
163 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return 0;
164 if (!hwnd) id = i + 1;
169 pTimer->msg = sys ? WM_SYSTIMER : WM_TIMER;
171 pTimer->timeout = timeout;
172 pTimer->expires = GetTickCount() + timeout;
174 TIMER_InsertTimer( pTimer );
175 MSG_IncTimerCount( GetTaskQueue(0) );
183 /***********************************************************************
186 static BOOL TIMER_KillTimer( HWND hwnd, WORD id, BOOL sys )
193 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
194 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
195 (pTimer->timeout != 0)) break;
196 if (i >= NB_TIMERS) return FALSE;
197 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return FALSE;
198 if (!sys && (pTimer->msg != WM_TIMER)) return FALSE;
199 else if (sys && (pTimer->msg != WM_SYSTIMER)) return FALSE;
201 /* Delete the timer */
208 TIMER_RemoveTimer( pTimer );
209 MSG_DecTimerCount( GetTaskQueue(0) );
214 /***********************************************************************
217 WORD SetTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
219 dprintf_timer(stddeb, "SetTimer: %d %d %d %p\n", hwnd, id, timeout, proc );
220 return TIMER_SetTimer( hwnd, id, timeout, proc, FALSE );
224 /***********************************************************************
225 * SetSystemTimer (USER.11)
227 WORD SetSystemTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
229 dprintf_timer(stddeb, "SetSystemTimer: %d %d %d %p\n",
230 hwnd, id, timeout, proc );
231 return TIMER_SetTimer( hwnd, id, timeout, proc, TRUE );
235 /***********************************************************************
236 * KillTimer (USER.12)
238 BOOL KillTimer( HWND hwnd, WORD id )
240 dprintf_timer(stddeb, "KillTimer: %d %d\n", hwnd, id );
241 return TIMER_KillTimer( hwnd, id, FALSE );
245 /***********************************************************************
246 * KillSystemTimer (USER.182)
248 BOOL KillSystemTimer( HWND hwnd, WORD id )
250 dprintf_timer(stddeb, "KillSystemTimer: %d %d\n", hwnd, id );
251 return TIMER_KillTimer( hwnd, id, TRUE );