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 */
32 /***********************************************************************
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
;
46 TIMER
* ptr
= pNextTimer
;
47 while (ptr
->next
&& (pTimer
->expires
>= ptr
->next
->expires
))
55 /***********************************************************************
58 * Remove the timer from the chain.
60 static void TIMER_RemoveTimer( TIMER
* pTimer
)
62 if (pTimer
== pNextTimer
) pNextTimer
= pTimer
->next
;
65 TIMER
* ptr
= pNextTimer
;
66 while (ptr
&& (ptr
->next
!= pTimer
)) ptr
= ptr
->next
;
67 if (ptr
) ptr
->next
= pTimer
->next
;
73 /***********************************************************************
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 /***********************************************************************
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 */
105 /* Restart the timer */
106 pTimer
->expires
= curTime
+ pTimer
->timeout
;
107 TIMER_InsertTimer( pTimer
);
109 *next
= TIMER_NextExpire( curTime
);
114 /***********************************************************************
117 static WORD
TIMER_SetTimer( HWND hwnd
, WORD id
, WORD timeout
,
118 FARPROC proc
, BOOL sys
)
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;
138 pTimer
->msg
= sys
? WM_SYSTIMER
: WM_TIMER
;
140 pTimer
->timeout
= timeout
;
141 pTimer
->expires
= GetTickCount() + timeout
;
143 TIMER_InsertTimer( pTimer
);
144 MSG_IncTimerCount( GetTaskQueue(0) );
149 /***********************************************************************
152 static BOOL
TIMER_KillTimer( HWND hwnd
, WORD id
, BOOL sys
)
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 */
174 TIMER_RemoveTimer( pTimer
);
175 MSG_DecTimerCount( GetTaskQueue(0) );
180 /***********************************************************************
183 WORD
SetTimer( HWND hwnd
, WORD id
, WORD timeout
, FARPROC proc
)
186 printf( "SetTimer: %d %d %d %p\n", hwnd
, id
, timeout
, proc
);
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
)
198 printf( "SetSystemTimer: %d %d %d %p\n", hwnd
, id
, timeout
, proc
);
200 return TIMER_SetTimer( hwnd
, id
, timeout
, proc
, TRUE
);
204 /***********************************************************************
205 * KillTimer (USER.12)
207 BOOL
KillTimer( HWND hwnd
, WORD id
)
210 printf( "KillTimer: %d %d\n", hwnd
, id
);
212 return TIMER_KillTimer( hwnd
, id
, FALSE
);
216 /***********************************************************************
217 * KillSystemTimer (USER.182)
219 BOOL
KillSystemTimer( HWND hwnd
, WORD id
)
222 printf( "KillSystemTimer: %d %d\n", hwnd
, id
);
224 return TIMER_KillTimer( hwnd
, id
, TRUE
);