4 * Copyright 1993 Alexandre Julliard
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
13 typedef struct tagTIMER
16 WORD msg; /* WM_TIMER or WM_SYSTIMER */
19 struct tagTIMER *next;
25 #define NB_RESERVED_TIMERS 2 /* for SetSystemTimer */
27 static TIMER TimersArray[NB_TIMERS];
29 static TIMER * pNextTimer = NULL; /* Next timer to expire */
31 /* Duration from 'time' until expiration of the timer */
32 #define EXPIRE_TIME(pTimer,time) \
33 (((pTimer)->expires <= (time)) ? 0 : (pTimer)->expires - (time))
36 /***********************************************************************
39 * Insert the timer at its place in the chain.
41 static void TIMER_InsertTimer( TIMER * pTimer )
43 if (!pNextTimer || (pTimer->expires < pNextTimer->expires))
45 pTimer->next = pNextTimer;
50 TIMER * ptr = pNextTimer;
51 while (ptr->next && (pTimer->expires >= ptr->next->expires))
53 pTimer->next = ptr->next;
59 /***********************************************************************
62 * Remove the timer from the chain.
64 static void TIMER_RemoveTimer( TIMER * pTimer )
66 if (pTimer == pNextTimer) pNextTimer = pTimer->next;
69 TIMER * ptr = pNextTimer;
70 while (ptr && (ptr->next != pTimer)) ptr = ptr->next;
71 if (ptr) ptr->next = pTimer->next;
77 /***********************************************************************
80 * Restart an expired timer.
82 static void TIMER_RestartTimer( TIMER * pTimer, DWORD curTime )
84 TIMER_RemoveTimer( pTimer );
85 pTimer->expires = curTime + pTimer->timeout;
86 TIMER_InsertTimer( pTimer );
90 /***********************************************************************
93 * Check whether a timer has expired, and create a message if necessary.
94 * Otherwise, return time until next timer expiration in 'next'.
95 * If 'hwnd' is not NULL, only consider timers for this window.
96 * If 'remove' is TRUE, remove all expired timers up to the returned one.
98 BOOL TIMER_CheckTimer( LONG *next, MSG *msg, HWND hwnd, BOOL remove )
100 TIMER * pTimer = pNextTimer;
101 DWORD curTime = GetTickCount();
103 if (hwnd) /* Find first timer for this window */
104 while (pTimer && (pTimer->hwnd != hwnd)) pTimer = pTimer->next;
106 if (!pTimer) *next = -1;
107 else *next = EXPIRE_TIME( pTimer, curTime );
108 if (*next != 0) return FALSE; /* No timer expired */
110 if (remove) /* Restart all timers before pTimer, and then pTimer itself */
112 while (pNextTimer != pTimer) TIMER_RestartTimer( pNextTimer, curTime );
113 TIMER_RestartTimer( pTimer, curTime );
116 /* Build the message */
117 msg->hwnd = pTimer->hwnd;
118 msg->message = pTimer->msg;
119 msg->wParam = pTimer->id;
120 msg->lParam = (LONG)pTimer->proc;
126 /***********************************************************************
129 static WORD TIMER_SetTimer( HWND hwnd, WORD id, WORD timeout,
130 FARPROC proc, BOOL sys )
135 if (!timeout) return 0;
136 if (!hwnd && !proc) return 0;
138 /* Check if there's already a timer with the same hwnd and id */
142 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
143 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
144 (pTimer->timeout != 0))
146 /* Got one: set new values and return */
147 pTimer->timeout = timeout;
148 pTimer->expires = GetTickCount() + timeout;
150 TIMER_RemoveTimer( pTimer );
151 TIMER_InsertTimer( pTimer );
156 /* Find a free timer */
158 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
159 if (!pTimer->timeout) break;
161 if (i >= NB_TIMERS) return 0;
162 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return 0;
163 if (!hwnd) id = i + 1;
168 pTimer->msg = sys ? WM_SYSTIMER : WM_TIMER;
170 pTimer->timeout = timeout;
171 pTimer->expires = GetTickCount() + timeout;
173 TIMER_InsertTimer( pTimer );
174 MSG_IncTimerCount( GetTaskQueue(0) );
182 /***********************************************************************
185 static BOOL TIMER_KillTimer( HWND hwnd, WORD id, BOOL sys )
192 for (i = 0, pTimer = TimersArray; i < NB_TIMERS; i++, pTimer++)
193 if ((pTimer->hwnd == hwnd) && (pTimer->id == id) &&
194 (pTimer->timeout != 0)) break;
195 if (i >= NB_TIMERS) return FALSE;
196 if (!sys && (i >= NB_TIMERS-NB_RESERVED_TIMERS)) return FALSE;
197 if (!sys && (pTimer->msg != WM_TIMER)) return FALSE;
198 else if (sys && (pTimer->msg != WM_SYSTIMER)) return FALSE;
200 /* Delete the timer */
207 TIMER_RemoveTimer( pTimer );
208 MSG_DecTimerCount( GetTaskQueue(0) );
213 /***********************************************************************
216 WORD SetTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
219 printf( "SetTimer: %d %d %d %p\n", hwnd, id, timeout, proc );
221 return TIMER_SetTimer( hwnd, id, timeout, proc, FALSE );
225 /***********************************************************************
226 * SetSystemTimer (USER.11)
228 WORD SetSystemTimer( HWND hwnd, WORD id, WORD timeout, FARPROC proc )
231 printf( "SetSystemTimer: %d %d %d %p\n", hwnd, id, timeout, proc );
233 return TIMER_SetTimer( hwnd, id, timeout, proc, TRUE );
237 /***********************************************************************
238 * KillTimer (USER.12)
240 BOOL KillTimer( HWND hwnd, WORD id )
243 printf( "KillTimer: %d %d\n", hwnd, id );
245 return TIMER_KillTimer( hwnd, id, FALSE );
249 /***********************************************************************
250 * KillSystemTimer (USER.182)
252 BOOL KillSystemTimer( HWND hwnd, WORD id )
255 printf( "KillSystemTimer: %d %d\n", hwnd, id );
257 return TIMER_KillTimer( hwnd, id, TRUE );