1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * MMSYSTEM time functions
6 * Copyright 1993 Martin Ayotte
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(mmtime
);
47 * We're using "1" as the mininum resolution to the timer,
48 * as Windows 95 does, according to the docs. Maybe it should
49 * depend on the computers resources!
51 #define MMSYSTIME_MININTERVAL (1)
52 #define MMSYSTIME_MAXINTERVAL (65535)
54 #define MMSYSTIME_STDINTERVAL (10) /* reasonable value? */
56 static void TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer
)
58 TRACE("before CallBack => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
59 lpTimer
->lpFunc
, lpTimer
->wTimerID
, lpTimer
->dwUser
);
61 /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called
62 * during interrupt time, is allowed to execute very limited number of API calls (like
63 * PostMessage), and must reside in DLL (therefore uses stack of active application). So I
64 * guess current implementation via SetTimer has to be improved upon.
66 switch (lpTimer
->wFlags
& 0x30) {
67 case TIME_CALLBACK_FUNCTION
:
68 if (lpTimer
->wFlags
& WINE_TIMER_IS32
)
69 (lpTimer
->lpFunc
)(lpTimer
->wTimerID
, 0, lpTimer
->dwUser
, 0, 0);
70 else if (pFnCallMMDrvFunc16
)
71 pFnCallMMDrvFunc16((DWORD
)lpTimer
->lpFunc
, lpTimer
->wTimerID
, 0,
72 lpTimer
->dwUser
, 0, 0);
74 case TIME_CALLBACK_EVENT_SET
:
75 SetEvent((HANDLE
)lpTimer
->lpFunc
);
77 case TIME_CALLBACK_EVENT_PULSE
:
78 PulseEvent((HANDLE
)lpTimer
->lpFunc
);
81 FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n",
82 lpTimer
->wFlags
, lpTimer
->lpFunc
);
85 TRACE("after CallBack !\n");
88 /**************************************************************************
89 * TIME_MMSysTimeCallback
91 static void CALLBACK
TIME_MMSysTimeCallback(LPWINE_MM_IDATA iData
)
93 LPWINE_TIMERENTRY lpTimer
, lpNextTimer
;
94 DWORD delta
= GetTickCount() - iData
->mmSysTimeMS
;
97 TRACE("Time delta: %ld\n", delta
);
99 while (delta
>= MMSYSTIME_MININTERVAL
) {
100 delta
-= MMSYSTIME_MININTERVAL
;
101 iData
->mmSysTimeMS
+= MMSYSTIME_MININTERVAL
;
103 /* since timeSetEvent() and timeKillEvent() can be called
104 * from 16 bit code, there are cases where win16 lock is
105 * locked upon entering timeSetEvent(), and then the mm timer
106 * critical section is locked. This function cannot call the
107 * timer callback with the crit sect locked (because callback
108 * may need to acquire Win16 lock, thus providing a deadlock
110 * To cope with that, we just copy the WINE_TIMERENTRY struct
111 * that need to trigger the callback, and call it without the
112 * mm timer crit sect locked. The bad side of this
113 * implementation is that, in some cases, the callback may be
114 * invoked *after* a timer has been destroyed...
119 EnterCriticalSection(&iData
->cs
);
120 for (lpTimer
= iData
->lpTimerList
; lpTimer
!= NULL
; ) {
121 lpNextTimer
= lpTimer
->lpNext
;
122 if (lpTimer
->uCurTime
< MMSYSTIME_MININTERVAL
) {
123 /* since lpTimer->wDelay is >= MININTERVAL, wCurTime value
124 * shall be correct (>= 0)
126 lpTimer
->uCurTime
+= lpTimer
->wDelay
- MMSYSTIME_MININTERVAL
;
127 if (lpTimer
->lpFunc
) {
128 if (idx
== iData
->nSizeLpTimers
) {
130 iData
->lpTimers
= (LPWINE_TIMERENTRY
)
131 HeapReAlloc(GetProcessHeap(), 0,
133 ++iData
->nSizeLpTimers
* sizeof(WINE_TIMERENTRY
));
135 iData
->lpTimers
= (LPWINE_TIMERENTRY
)
136 HeapAlloc(GetProcessHeap(), 0,
137 ++iData
->nSizeLpTimers
* sizeof(WINE_TIMERENTRY
));
139 iData
->lpTimers
[idx
++] = *lpTimer
;
141 /* TIME_ONESHOT is defined as 0 */
142 if (!(lpTimer
->wFlags
& TIME_PERIODIC
))
143 timeKillEvent(lpTimer
->wTimerID
);
145 lpTimer
->uCurTime
-= MMSYSTIME_MININTERVAL
;
147 lpTimer
= lpNextTimer
;
149 LeaveCriticalSection(&iData
->cs
);
152 TIME_TriggerCallBack(&iData
->lpTimers
[--idx
]);
157 /**************************************************************************
158 * TIME_MMSysTimeThread
160 static DWORD CALLBACK
TIME_MMSysTimeThread(LPVOID arg
)
162 LPWINE_MM_IDATA iData
= (LPWINE_MM_IDATA
)arg
;
163 volatile HANDLE
*pActive
= (volatile HANDLE
*)&iData
->hMMTimer
;
164 DWORD last_time
, cur_time
;
166 usleep(MMSYSTIME_STDINTERVAL
* 1000);
167 last_time
= GetTickCount();
169 TIME_MMSysTimeCallback(iData
);
170 cur_time
= GetTickCount();
171 while (last_time
< cur_time
)
172 last_time
+= MMSYSTIME_STDINTERVAL
;
173 usleep((last_time
- cur_time
) * 1000);
178 /**************************************************************************
181 void TIME_MMTimeStart(void)
183 /* one could think it's possible to stop the service thread activity when no more
184 * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
185 * without being incremented within the service thread callback.
187 if (!WINMM_IData
->hMMTimer
) {
188 WINMM_IData
->mmSysTimeMS
= GetTickCount();
189 WINMM_IData
->lpTimerList
= NULL
;
190 WINMM_IData
->hMMTimer
= CreateThread(NULL
, 0, TIME_MMSysTimeThread
, WINMM_IData
, 0, NULL
);
194 /**************************************************************************
197 void TIME_MMTimeStop(void)
199 if (WINMM_IData
->hMMTimer
) {
200 HANDLE hMMTimer
= WINMM_IData
->hMMTimer
;
201 WINMM_IData
->hMMTimer
= 0;
202 WaitForSingleObject(hMMTimer
, INFINITE
);
203 CloseHandle(hMMTimer
);
207 /**************************************************************************
208 * timeGetSystemTime [WINMM.@]
210 MMRESULT WINAPI
timeGetSystemTime(LPMMTIME lpTime
, UINT wSize
)
212 TRACE("(%p, %u);\n", lpTime
, wSize
);
214 if (wSize
>= sizeof(*lpTime
)) {
216 lpTime
->wType
= TIME_MS
;
217 lpTime
->u
.ms
= WINMM_IData
->mmSysTimeMS
;
219 TRACE("=> %lu\n", lpTime
->u
.ms
);
225 /**************************************************************************
226 * TIME_SetEventInternal [internal]
228 WORD
TIME_SetEventInternal(UINT wDelay
, UINT wResol
,
229 LPTIMECALLBACK lpFunc
, DWORD dwUser
, UINT wFlags
)
232 LPWINE_TIMERENTRY lpNewTimer
;
233 LPWINE_TIMERENTRY lpTimer
;
235 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay
, wResol
, lpFunc
, dwUser
, wFlags
);
237 lpNewTimer
= (LPWINE_TIMERENTRY
)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY
));
238 if (lpNewTimer
== NULL
)
241 if (wDelay
< MMSYSTIME_MININTERVAL
|| wDelay
> MMSYSTIME_MAXINTERVAL
)
246 lpNewTimer
->uCurTime
= wDelay
;
247 lpNewTimer
->wDelay
= wDelay
;
248 lpNewTimer
->wResol
= wResol
;
249 lpNewTimer
->lpFunc
= lpFunc
;
250 lpNewTimer
->dwUser
= dwUser
;
251 lpNewTimer
->wFlags
= wFlags
;
253 EnterCriticalSection(&WINMM_IData
->cs
);
255 for (lpTimer
= WINMM_IData
->lpTimerList
; lpTimer
!= NULL
; lpTimer
= lpTimer
->lpNext
) {
256 wNewID
= max(wNewID
, lpTimer
->wTimerID
);
259 lpNewTimer
->lpNext
= WINMM_IData
->lpTimerList
;
260 WINMM_IData
->lpTimerList
= lpNewTimer
;
261 lpNewTimer
->wTimerID
= wNewID
+ 1;
263 LeaveCriticalSection(&WINMM_IData
->cs
);
265 TRACE("=> %u\n", wNewID
+ 1);
270 /**************************************************************************
271 * timeSetEvent [WINMM.@]
273 MMRESULT WINAPI
timeSetEvent(UINT wDelay
, UINT wResol
, LPTIMECALLBACK lpFunc
,
274 DWORD_PTR dwUser
, UINT wFlags
)
276 if (wFlags
& WINE_TIMER_IS32
)
277 WARN("Unknown windows flag... wine internally used.. ooch\n");
279 return TIME_SetEventInternal(wDelay
, wResol
, lpFunc
,
280 dwUser
, wFlags
|WINE_TIMER_IS32
);
283 /**************************************************************************
284 * timeKillEvent [WINMM.@]
286 MMRESULT WINAPI
timeKillEvent(UINT wID
)
288 LPWINE_TIMERENTRY
* lpTimer
;
289 MMRESULT ret
= MMSYSERR_INVALPARAM
;
291 TRACE("(%u)\n", wID
);
292 EnterCriticalSection(&WINMM_IData
->cs
);
293 /* remove WINE_TIMERENTRY from list */
294 for (lpTimer
= &WINMM_IData
->lpTimerList
; *lpTimer
; lpTimer
= &(*lpTimer
)->lpNext
) {
295 if (wID
== (*lpTimer
)->wTimerID
) {
299 LeaveCriticalSection(&WINMM_IData
->cs
);
302 LPWINE_TIMERENTRY lpTemp
= *lpTimer
;
304 /* unlink timer of id 'wID' */
305 *lpTimer
= (*lpTimer
)->lpNext
;
306 HeapFree(GetProcessHeap(), 0, lpTemp
);
307 ret
= TIMERR_NOERROR
;
309 WARN("wID=%u is not a valid timer ID\n", wID
);
315 /**************************************************************************
316 * timeGetDevCaps [WINMM.@]
318 MMRESULT WINAPI
timeGetDevCaps(LPTIMECAPS lpCaps
, UINT wSize
)
320 TRACE("(%p, %u) !\n", lpCaps
, wSize
);
322 lpCaps
->wPeriodMin
= MMSYSTIME_MININTERVAL
;
323 lpCaps
->wPeriodMax
= MMSYSTIME_MAXINTERVAL
;
327 /**************************************************************************
328 * timeBeginPeriod [WINMM.@]
330 MMRESULT WINAPI
timeBeginPeriod(UINT wPeriod
)
332 TRACE("(%u) !\n", wPeriod
);
334 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
335 return TIMERR_NOCANDO
;
339 /**************************************************************************
340 * timeEndPeriod [WINMM.@]
342 MMRESULT WINAPI
timeEndPeriod(UINT wPeriod
)
344 TRACE("(%u) !\n", wPeriod
);
346 if (wPeriod
< MMSYSTIME_MININTERVAL
|| wPeriod
> MMSYSTIME_MAXINTERVAL
)
347 return TIMERR_NOCANDO
;
351 /**************************************************************************
352 * timeGetTime [MMSYSTEM.607]
353 * timeGetTime [WINMM.@]
355 DWORD WINAPI
timeGetTime(void)
358 /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
359 * that lets mciavi.drv run correctly
361 if (pFnReleaseThunkLock
) pFnReleaseThunkLock(&count
);
363 if (pFnRestoreThunkLock
) pFnRestoreThunkLock(count
);
364 return WINMM_IData
->mmSysTimeMS
;