1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * MMSYSTEM time functions
6 * Copyright 1993 Martin Ayotte
10 #include "wine/port.h"
22 #include "wine/mmsystem16.h"
25 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(mmtime
);
31 * We're using "1" as the mininum resolution to the timer,
32 * as Windows 95 does, according to the docs. Maybe it should
33 * depend on the computers resources!
35 #define MMSYSTIME_MININTERVAL (1)
36 #define MMSYSTIME_MAXINTERVAL (65535)
38 #define MMSYSTIME_STDINTERVAL (10) /* reasonable value? */
40 /* ### start build ### */
41 extern WORD CALLBACK
TIME_CallTo16_word_wwlll(FARPROC16
,WORD
,WORD
,LONG
,LONG
,LONG
);
42 /* ### stop build ### */
44 static void TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer
)
46 TRACE("before CallBack => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
47 lpTimer
->lpFunc
, lpTimer
->wTimerID
, lpTimer
->dwUser
);
49 /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called
50 * during interrupt time, is allowed to execute very limited number of API calls (like
51 * PostMessage), and must reside in DLL (therefore uses stack of active application). So I
52 * guess current implementation via SetTimer has to be improved upon.
54 switch (lpTimer
->wFlags
& 0x30) {
55 case TIME_CALLBACK_FUNCTION
:
56 if (lpTimer
->wFlags
& WINE_TIMER_IS32
)
57 ((LPTIMECALLBACK
)lpTimer
->lpFunc
)(lpTimer
->wTimerID
, 0, lpTimer
->dwUser
, 0, 0);
59 TIME_CallTo16_word_wwlll(lpTimer
->lpFunc
, lpTimer
->wTimerID
, 0,
60 lpTimer
->dwUser
, 0, 0);
62 case TIME_CALLBACK_EVENT_SET
:
63 SetEvent((HANDLE
)lpTimer
->lpFunc
);
65 case TIME_CALLBACK_EVENT_PULSE
:
66 PulseEvent((HANDLE
)lpTimer
->lpFunc
);
69 FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n",
70 lpTimer
->wFlags
, lpTimer
->lpFunc
);
73 TRACE("after CallBack !\n");
76 /**************************************************************************
77 * TIME_MMSysTimeCallback
79 static void CALLBACK
TIME_MMSysTimeCallback(LPWINE_MM_IDATA iData
)
81 LPWINE_TIMERENTRY lpTimer
, lpNextTimer
;
82 DWORD delta
= GetTickCount() - iData
->mmSysTimeMS
;
85 TRACE("Time delta: %ld\n", delta
);
87 while (delta
>= MMSYSTIME_MININTERVAL
) {
88 delta
-= MMSYSTIME_MININTERVAL
;
89 iData
->mmSysTimeMS
+= MMSYSTIME_MININTERVAL
;
91 /* since timeSetEvent() and timeKillEvent() can be called
92 * from 16 bit code, there are cases where win16 lock is
93 * locked upon entering timeSetEvent(), and then the mm timer
94 * critical section is locked. This function cannot call the
95 * timer callback with the crit sect locked (because callback
96 * may need to acquire Win16 lock, thus providing a deadlock
98 * To cope with that, we just copy the WINE_TIMERENTRY struct
99 * that need to trigger the callback, and call it without the
100 * mm timer crit sect locked. The bad side of this
101 * implementation is that, in some cases, the callback may be
102 * invoked *after* a timer has been destroyed...
107 EnterCriticalSection(&iData
->cs
);
108 for (lpTimer
= iData
->lpTimerList
; lpTimer
!= NULL
; ) {
109 lpNextTimer
= lpTimer
->lpNext
;
110 if (lpTimer
->uCurTime
< MMSYSTIME_MININTERVAL
) {
111 /* since lpTimer->wDelay is >= MININTERVAL, wCurTime value
112 * shall be correct (>= 0)
114 lpTimer
->uCurTime
+= lpTimer
->wDelay
- MMSYSTIME_MININTERVAL
;
115 if (lpTimer
->lpFunc
) {
116 if (idx
== iData
->nSizeLpTimers
) {
117 iData
->lpTimers
= (LPWINE_TIMERENTRY
)
118 HeapReAlloc(GetProcessHeap(), 0,
120 ++iData
->nSizeLpTimers
* sizeof(WINE_TIMERENTRY
));
122 iData
->lpTimers
[idx
++] = *lpTimer
;
124 /* TIME_ONESHOT is defined as 0 */
125 if (!(lpTimer
->wFlags
& TIME_PERIODIC
))
126 timeKillEvent(lpTimer
->wTimerID
);
128 lpTimer
->uCurTime
-= MMSYSTIME_MININTERVAL
;
130 lpTimer
= lpNextTimer
;
132 LeaveCriticalSection(&iData
->cs
);
135 TIME_TriggerCallBack(&iData
->lpTimers
[--idx
]);
140 /**************************************************************************
141 * TIME_MMSysTimeThread
143 static DWORD CALLBACK
TIME_MMSysTimeThread(LPVOID arg
)
145 LPWINE_MM_IDATA iData
= (LPWINE_MM_IDATA
)arg
;
146 volatile HANDLE
*pActive
= (volatile HANDLE
*)&iData
->hMMTimer
;
147 DWORD last_time
, cur_time
;
149 usleep(MMSYSTIME_STDINTERVAL
* 1000);
150 last_time
= GetTickCount();
152 TIME_MMSysTimeCallback(iData
);
153 cur_time
= GetTickCount();
154 while (last_time
< cur_time
)
155 last_time
+= MMSYSTIME_STDINTERVAL
;
156 usleep((last_time
- cur_time
) * 1000);
161 /**************************************************************************
164 LPWINE_MM_IDATA
TIME_MMTimeStart(void)
166 LPWINE_MM_IDATA iData
= MULTIMEDIA_GetIData();
168 if (IsBadWritePtr(iData
, sizeof(WINE_MM_IDATA
))) {
169 ERR("iData is not correctly set, please report. Expect failure.\n");
172 /* one could think it's possible to stop the service thread activity when no more
173 * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
174 * without being incremented within the service thread callback.
176 if (!iData
->hMMTimer
) {
177 iData
->mmSysTimeMS
= GetTickCount();
178 iData
->lpTimerList
= NULL
;
179 iData
->hMMTimer
= CreateThread(NULL
, 0, TIME_MMSysTimeThread
, iData
, 0, NULL
);
185 /**************************************************************************
188 void TIME_MMTimeStop(void)
190 LPWINE_MM_IDATA iData
= MULTIMEDIA_GetIData();
192 if (IsBadWritePtr(iData
, sizeof(WINE_MM_IDATA
))) {
193 ERR("iData is not correctly set, please report. Expect failure.\n");
196 if (iData
->hMMTimer
) {
197 HANDLE hMMTimer
= iData
->hMMTimer
;
199 WaitForSingleObject(hMMTimer
, INFINITE
);
200 CloseHandle(hMMTimer
);
204 /**************************************************************************
205 * timeGetSystemTime [WINMM.@]
207 MMRESULT WINAPI
timeGetSystemTime(LPMMTIME lpTime
, UINT wSize
)
209 TRACE("(%p, %u);\n", lpTime
, wSize
);
211 if (wSize
>= sizeof(*lpTime
)) {
212 lpTime
->wType
= TIME_MS
;
213 lpTime
->u
.ms
= TIME_MMTimeStart()->mmSysTimeMS
;
215 TRACE("=> %lu\n", lpTime
->u
.ms
);
221 /**************************************************************************
222 * timeGetSystemTime [MMSYSTEM.601]
224 MMRESULT16 WINAPI
timeGetSystemTime16(LPMMTIME16 lpTime
, UINT16 wSize
)
226 TRACE("(%p, %u);\n", lpTime
, wSize
);
228 if (wSize
>= sizeof(*lpTime
)) {
229 lpTime
->wType
= TIME_MS
;
230 lpTime
->u
.ms
= TIME_MMTimeStart()->mmSysTimeMS
;
232 TRACE("=> %lu\n", lpTime
->u
.ms
);
238 /**************************************************************************
239 * timeSetEventInternal [internal]
241 static WORD
timeSetEventInternal(UINT wDelay
, UINT wResol
,
242 FARPROC16 lpFunc
, DWORD dwUser
, UINT wFlags
)
245 LPWINE_TIMERENTRY lpNewTimer
;
246 LPWINE_TIMERENTRY lpTimer
;
247 LPWINE_MM_IDATA iData
;
249 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay
, wResol
, lpFunc
, dwUser
, wFlags
);
251 lpNewTimer
= (LPWINE_TIMERENTRY
)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY
));
252 if (lpNewTimer
== NULL
)
255 if (wDelay
< MMSYSTIME_MININTERVAL
|| wDelay
> MMSYSTIME_MAXINTERVAL
)
258 iData
= TIME_MMTimeStart();
260 lpNewTimer
->uCurTime
= wDelay
;
261 lpNewTimer
->wDelay
= wDelay
;
262 lpNewTimer
->wResol
= wResol
;
263 lpNewTimer
->lpFunc
= lpFunc
;
264 lpNewTimer
->dwUser
= dwUser
;
265 lpNewTimer
->wFlags
= wFlags
;
267 EnterCriticalSection(&iData
->cs
);
269 for (lpTimer
= iData
->lpTimerList
; lpTimer
!= NULL
; lpTimer
= lpTimer
->lpNext
) {
270 wNewID
= max(wNewID
, lpTimer
->wTimerID
);
273 lpNewTimer
->lpNext
= iData
->lpTimerList
;
274 iData
->lpTimerList
= lpNewTimer
;
275 lpNewTimer
->wTimerID
= wNewID
+ 1;
277 LeaveCriticalSection(&iData
->cs
);
279 TRACE("=> %u\n", wNewID
+ 1);
284 /**************************************************************************
285 * timeSetEvent [WINMM.@]
287 MMRESULT WINAPI
timeSetEvent(UINT wDelay
, UINT wResol
, LPTIMECALLBACK lpFunc
,
288 DWORD dwUser
, UINT wFlags
)
290 if (wFlags
& WINE_TIMER_IS32
)
291 WARN("Unknown windows flag... wine internally used.. ooch\n");
293 return timeSetEventInternal(wDelay
, wResol
, (FARPROC16
)lpFunc
,
294 dwUser
, wFlags
|WINE_TIMER_IS32
);
297 /**************************************************************************
298 * timeSetEvent [MMSYSTEM.602]
300 MMRESULT16 WINAPI
timeSetEvent16(UINT16 wDelay
, UINT16 wResol
, LPTIMECALLBACK16 lpFunc
,
301 DWORD dwUser
, UINT16 wFlags
)
303 if (wFlags
& WINE_TIMER_IS32
)
304 WARN("Unknown windows flag... wine internally used.. ooch\n");
306 return timeSetEventInternal(wDelay
, wResol
, (FARPROC16
)lpFunc
,
307 dwUser
, wFlags
& ~WINE_TIMER_IS32
);
310 /**************************************************************************
311 * timeKillEvent [WINMM.@]
313 MMRESULT WINAPI
timeKillEvent(UINT wID
)
315 LPWINE_TIMERENTRY
* lpTimer
;
316 LPWINE_MM_IDATA iData
= MULTIMEDIA_GetIData();
317 MMRESULT ret
= MMSYSERR_INVALPARAM
;
319 TRACE("(%u)\n", wID
);
320 EnterCriticalSection(&iData
->cs
);
321 /* remove WINE_TIMERENTRY from list */
322 for (lpTimer
= &iData
->lpTimerList
; *lpTimer
; lpTimer
= &(*lpTimer
)->lpNext
) {
323 if (wID
== (*lpTimer
)->wTimerID
) {
327 LeaveCriticalSection(&iData
->cs
);
330 LPWINE_TIMERENTRY lpTemp
= *lpTimer
;
332 /* unlink timer of id 'wID' */
333 *lpTimer
= (*lpTimer
)->lpNext
;
334 HeapFree(GetProcessHeap(), 0, lpTemp
);
335 ret
= TIMERR_NOERROR
;
337 WARN("wID=%u is not a valid timer ID\n", wID
);
343 /**************************************************************************
344 * timeKillEvent [MMSYSTEM.603]
346 MMRESULT16 WINAPI
timeKillEvent16(UINT16 wID
)
348 return timeKillEvent(wID
);
351 /**************************************************************************
352 * timeGetDevCaps [WINMM.@]
354 MMRESULT WINAPI
timeGetDevCaps(LPTIMECAPS lpCaps
, UINT wSize
)
356 TRACE("(%p, %u) !\n", lpCaps
, wSize
);
358 lpCaps
->wPeriodMin
= MMSYSTIME_MININTERVAL
;
359 lpCaps
->wPeriodMax
= MMSYSTIME_MAXINTERVAL
;
363 /**************************************************************************
364 * timeGetDevCaps [MMSYSTEM.604]
366 MMRESULT16 WINAPI
timeGetDevCaps16(LPTIMECAPS16 lpCaps
, UINT16 wSize
)
368 TRACE("(%p, %u) !\n", lpCaps
, wSize
);
370 lpCaps
->wPeriodMin
= MMSYSTIME_MININTERVAL
;
371 lpCaps
->wPeriodMax
= MMSYSTIME_MAXINTERVAL
;
375 /**************************************************************************
376 * timeBeginPeriod [WINMM.@]
378 MMRESULT WINAPI
timeBeginPeriod(UINT wPeriod
)
380 TRACE("(%u) !\n", wPeriod
);
382 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
383 return TIMERR_NOCANDO
;
387 /**************************************************************************
388 * timeBeginPeriod [MMSYSTEM.605]
390 MMRESULT16 WINAPI
timeBeginPeriod16(UINT16 wPeriod
)
392 TRACE("(%u) !\n", wPeriod
);
394 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
395 return TIMERR_NOCANDO
;
399 /**************************************************************************
400 * timeEndPeriod [WINMM.@]
402 MMRESULT WINAPI
timeEndPeriod(UINT wPeriod
)
404 TRACE("(%u) !\n", wPeriod
);
406 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
407 return TIMERR_NOCANDO
;
411 /**************************************************************************
412 * timeEndPeriod [MMSYSTEM.606]
414 MMRESULT16 WINAPI
timeEndPeriod16(UINT16 wPeriod
)
416 TRACE("(%u) !\n", wPeriod
);
418 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
419 return TIMERR_NOCANDO
;
423 /**************************************************************************
424 * timeGetTime [MMSYSTEM.607]
425 * timeGetTime [WINMM.@]
427 DWORD WINAPI
timeGetTime(void)
429 /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
430 * that lets mciavi.drv run correctly
433 ReleaseThunkLock(&count
);
434 RestoreThunkLock(count
);
435 return TIME_MMTimeStart()->mmSysTimeMS
;