1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * MMSYTEM time functions
6 * Copyright 1993 Martin Ayotte
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(mmtime
)
23 * We're using "1" as the mininum resolution to the timer,
24 * as Windows 95 does, according to the docs. Maybe it should
25 * depend on the computers resources!
27 #define MMSYSTIME_MININTERVAL (1)
28 #define MMSYSTIME_MAXINTERVAL (65535)
30 /* ### start build ### */
31 extern WORD CALLBACK
TIME_CallTo16_word_wwlll(FARPROC16
,WORD
,WORD
,LONG
,LONG
,LONG
);
32 /* ### stop build ### */
34 static void TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer
)
36 TRACE("before CallBack => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
37 lpTimer
->lpFunc
, lpTimer
->wTimerID
, lpTimer
->dwUser
);
39 /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called
40 * during interrupt time, is allowed to execute very limited number of API calls (like
41 * PostMessage), and must reside in DLL (therefore uses stack of active application). So I
42 * guess current implementation via SetTimer has to be improved upon.
44 switch (lpTimer
->wFlags
& 0x30) {
45 case TIME_CALLBACK_FUNCTION
:
46 if (lpTimer
->wFlags
& WINE_TIMER_IS32
)
47 ((LPTIMECALLBACK
)lpTimer
->lpFunc
)(lpTimer
->wTimerID
, 0, lpTimer
->dwUser
, 0, 0);
49 TIME_CallTo16_word_wwlll(lpTimer
->lpFunc
, lpTimer
->wTimerID
, 0,
50 lpTimer
->dwUser
, 0, 0);
52 case TIME_CALLBACK_EVENT_SET
:
53 SetEvent((HANDLE
)lpTimer
->lpFunc
);
55 case TIME_CALLBACK_EVENT_PULSE
:
56 PulseEvent((HANDLE
)lpTimer
->lpFunc
);
59 FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n",
60 lpTimer
->wFlags
, lpTimer
->lpFunc
);
63 TRACE("after CallBack !\n");
66 /**************************************************************************
67 * TIME_MMSysTimeCallback
69 static void CALLBACK
TIME_MMSysTimeCallback(ULONG_PTR ptr_
)
71 LPWINE_TIMERENTRY lpTimer
, lpNextTimer
;
72 LPWINE_MM_IDATA iData
= (LPWINE_MM_IDATA
)ptr_
;
73 DWORD delta
= GetTickCount() - iData
->mmSysTimeMS
;
76 TRACE("Time delta: %ld\n", delta
);
78 while (delta
>= MMSYSTIME_MININTERVAL
) {
79 delta
-= MMSYSTIME_MININTERVAL
;
80 iData
->mmSysTimeMS
+= MMSYSTIME_MININTERVAL
;
82 /* since timeSetEvent() and timeKillEvent() can be called
83 * from 16 bit code, there are cases where win16 lock is
84 * locked upon entering timeSetEvent(), and then the mm timer
85 * critical section is locked. This function cannot call the
86 * timer callback with the crit sect locked (because callback
87 * may need to acquire Win16 lock, thus providing a deadlock
89 * To cope with that, we just copy the WINE_TIMERENTRY struct
90 * that need to trigger the callback, and call it without the
91 * mm timer crit sect locked. The bad side of this
92 * implementation is that, in some cases, the callback may be
93 * invoked *after* a timer has been destroyed...
98 EnterCriticalSection(&iData
->cs
);
99 for (lpTimer
= iData
->lpTimerList
; lpTimer
!= NULL
; ) {
100 lpNextTimer
= lpTimer
->lpNext
;
101 if (lpTimer
->uCurTime
< MMSYSTIME_MININTERVAL
) {
102 /* since lpTimer->wDelay is >= MININTERVAL, wCurTime value
103 * shall be correct (>= 0)
105 lpTimer
->uCurTime
+= lpTimer
->wDelay
- MMSYSTIME_MININTERVAL
;
106 if (lpTimer
->lpFunc
) {
107 if (idx
== iData
->nSizeLpTimers
) {
108 iData
->lpTimers
= (LPWINE_TIMERENTRY
)
109 HeapReAlloc(GetProcessHeap(), 0,
111 ++iData
->nSizeLpTimers
* sizeof(WINE_TIMERENTRY
));
113 iData
->lpTimers
[idx
++] = *lpTimer
;
115 /* TIME_ONESHOT is defined as 0 */
116 if (!(lpTimer
->wFlags
& TIME_PERIODIC
))
117 timeKillEvent(lpTimer
->wTimerID
);
119 lpTimer
->uCurTime
-= MMSYSTIME_MININTERVAL
;
121 lpTimer
= lpNextTimer
;
123 LeaveCriticalSection(&iData
->cs
);
126 TIME_TriggerCallBack(&iData
->lpTimers
[--idx
]);
131 /**************************************************************************
132 * MULTIMEDIA_MMTimeStart [internal]
134 static LPWINE_MM_IDATA
MULTIMEDIA_MMTimeStart(void)
136 LPWINE_MM_IDATA iData
= MULTIMEDIA_GetIData();
138 if (IsBadWritePtr(iData
, sizeof(WINE_MM_IDATA
))) {
139 ERR("iData is not correctly set, please report. Expect failure.\n");
142 /* FIXME: the service timer is never killed, even when process is
143 * detached from this DLL
145 /* one could think it's possible to stop the service thread activity when no more
146 * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
147 * without being incremented within the service thread callback.
149 if (!iData
->hMMTimer
) {
150 iData
->mmSysTimeMS
= GetTickCount();
151 iData
->lpTimerList
= NULL
;
152 /* 10ms seems a reasonable value ?? */
153 iData
->hMMTimer
= SERVICE_AddTimer(10*1000L, TIME_MMSysTimeCallback
, (DWORD
)iData
);
159 /**************************************************************************
160 * timeGetSystemTime [WINMM.140]
162 MMRESULT WINAPI
timeGetSystemTime(LPMMTIME lpTime
, UINT wSize
)
164 TRACE("(%p, %u);\n", lpTime
, wSize
);
166 if (wSize
>= sizeof(*lpTime
)) {
167 lpTime
->wType
= TIME_MS
;
168 lpTime
->u
.ms
= MULTIMEDIA_MMTimeStart()->mmSysTimeMS
;
170 TRACE("=> %lu\n", lpTime
->u
.ms
);
176 /**************************************************************************
177 * timeGetSystemTime [MMSYSTEM.601]
179 MMRESULT16 WINAPI
timeGetSystemTime16(LPMMTIME16 lpTime
, UINT16 wSize
)
181 TRACE("(%p, %u);\n", lpTime
, wSize
);
183 if (wSize
>= sizeof(*lpTime
)) {
184 lpTime
->wType
= TIME_MS
;
185 lpTime
->u
.ms
= MULTIMEDIA_MMTimeStart()->mmSysTimeMS
;
187 TRACE("=> %lu\n", lpTime
->u
.ms
);
193 /**************************************************************************
194 * timeSetEventInternal [internal]
196 static WORD
timeSetEventInternal(UINT wDelay
, UINT wResol
,
197 FARPROC16 lpFunc
, DWORD dwUser
, UINT wFlags
)
200 LPWINE_TIMERENTRY lpNewTimer
;
201 LPWINE_TIMERENTRY lpTimer
;
202 LPWINE_MM_IDATA iData
;
204 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay
, wResol
, lpFunc
, dwUser
, wFlags
);
206 lpNewTimer
= (LPWINE_TIMERENTRY
)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY
));
207 if (lpNewTimer
== NULL
)
210 if (wDelay
< MMSYSTIME_MININTERVAL
|| wDelay
> MMSYSTIME_MAXINTERVAL
)
213 iData
= MULTIMEDIA_MMTimeStart();
215 lpNewTimer
->uCurTime
= wDelay
;
216 lpNewTimer
->wDelay
= wDelay
;
217 lpNewTimer
->wResol
= wResol
;
218 lpNewTimer
->lpFunc
= lpFunc
;
219 lpNewTimer
->dwUser
= dwUser
;
220 lpNewTimer
->wFlags
= wFlags
;
222 EnterCriticalSection(&iData
->cs
);
224 for (lpTimer
= iData
->lpTimerList
; lpTimer
!= NULL
; lpTimer
= lpTimer
->lpNext
) {
225 wNewID
= MAX(wNewID
, lpTimer
->wTimerID
);
228 lpNewTimer
->lpNext
= iData
->lpTimerList
;
229 iData
->lpTimerList
= lpNewTimer
;
230 lpNewTimer
->wTimerID
= wNewID
+ 1;
232 LeaveCriticalSection(&iData
->cs
);
234 TRACE("=> %u\n", wNewID
+ 1);
239 /**************************************************************************
240 * timeSetEvent [MMSYSTEM.602]
242 MMRESULT WINAPI
timeSetEvent(UINT wDelay
, UINT wResol
, LPTIMECALLBACK lpFunc
,
243 DWORD dwUser
, UINT wFlags
)
245 if (wFlags
& WINE_TIMER_IS32
)
246 WARN("Unknown windows flag... wine internally used.. ooch\n");
248 return timeSetEventInternal(wDelay
, wResol
, (FARPROC16
)lpFunc
,
249 dwUser
, wFlags
|WINE_TIMER_IS32
);
252 /**************************************************************************
253 * timeSetEvent [MMSYSTEM.602]
255 MMRESULT16 WINAPI
timeSetEvent16(UINT16 wDelay
, UINT16 wResol
, LPTIMECALLBACK16 lpFunc
,
256 DWORD dwUser
, UINT16 wFlags
)
258 if (wFlags
& WINE_TIMER_IS32
)
259 WARN("Unknown windows flag... wine internally used.. ooch\n");
261 return timeSetEventInternal(wDelay
, wResol
, (FARPROC16
)lpFunc
,
262 dwUser
, wFlags
& ~WINE_TIMER_IS32
);
265 /**************************************************************************
266 * timeKillEvent [WINMM.142]
268 MMRESULT WINAPI
timeKillEvent(UINT wID
)
270 LPWINE_TIMERENTRY
* lpTimer
;
271 LPWINE_MM_IDATA iData
= MULTIMEDIA_GetIData();
272 MMRESULT ret
= MMSYSERR_INVALPARAM
;
274 TRACE("(%u)\n", wID
);
275 EnterCriticalSection(&iData
->cs
);
276 /* remove WINE_TIMERENTRY from list */
277 for (lpTimer
= &iData
->lpTimerList
; *lpTimer
; lpTimer
= &(*lpTimer
)->lpNext
) {
278 if (wID
== (*lpTimer
)->wTimerID
) {
282 LeaveCriticalSection(&iData
->cs
);
285 LPWINE_TIMERENTRY lpTemp
= *lpTimer
;
287 /* unlink timer of id 'wID' */
288 *lpTimer
= (*lpTimer
)->lpNext
;
289 HeapFree(GetProcessHeap(), 0, lpTemp
);
290 ret
= TIMERR_NOERROR
;
292 WARN("wID=%u is not a valid timer ID\n", wID
);
298 /**************************************************************************
299 * timeKillEvent [MMSYSTEM.603]
301 MMRESULT16 WINAPI
timeKillEvent16(UINT16 wID
)
303 return timeKillEvent(wID
);
306 /**************************************************************************
307 * timeGetDevCaps [WINMM.139]
309 MMRESULT WINAPI
timeGetDevCaps(LPTIMECAPS lpCaps
, UINT wSize
)
311 TRACE("(%p, %u) !\n", lpCaps
, wSize
);
313 lpCaps
->wPeriodMin
= MMSYSTIME_MININTERVAL
;
314 lpCaps
->wPeriodMax
= MMSYSTIME_MAXINTERVAL
;
318 /**************************************************************************
319 * timeGetDevCaps [MMSYSTEM.604]
321 MMRESULT16 WINAPI
timeGetDevCaps16(LPTIMECAPS16 lpCaps
, UINT16 wSize
)
323 TRACE("(%p, %u) !\n", lpCaps
, wSize
);
325 lpCaps
->wPeriodMin
= MMSYSTIME_MININTERVAL
;
326 lpCaps
->wPeriodMax
= MMSYSTIME_MAXINTERVAL
;
330 /**************************************************************************
331 * timeBeginPeriod [WINMM.137]
333 MMRESULT WINAPI
timeBeginPeriod(UINT wPeriod
)
335 TRACE("(%u) !\n", wPeriod
);
337 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
338 return TIMERR_NOCANDO
;
342 /**************************************************************************
343 * timeBeginPeriod16 [MMSYSTEM.605]
345 MMRESULT16 WINAPI
timeBeginPeriod16(UINT16 wPeriod
)
347 TRACE("(%u) !\n", wPeriod
);
349 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
350 return TIMERR_NOCANDO
;
354 /**************************************************************************
355 * timeEndPeriod [WINMM.138]
357 MMRESULT WINAPI
timeEndPeriod(UINT wPeriod
)
359 TRACE("(%u) !\n", wPeriod
);
361 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
362 return TIMERR_NOCANDO
;
366 /**************************************************************************
367 * timeEndPeriod16 [MMSYSTEM.606]
369 MMRESULT16 WINAPI
timeEndPeriod16(UINT16 wPeriod
)
371 TRACE("(%u) !\n", wPeriod
);
373 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
374 return TIMERR_NOCANDO
;
378 /**************************************************************************
379 * timeGetTime [MMSYSTEM.607][WINMM.141]
381 DWORD WINAPI
timeGetTime(void)
383 /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
384 * that lets mciavi.drv run correctly
386 if ( _ConfirmWin16Lock() ) {
387 SYSLEVEL_ReleaseWin16Lock();
388 SYSLEVEL_RestoreWin16Lock();
391 return MULTIMEDIA_MMTimeStart()->mmSysTimeMS
;