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 ((LPTIMECALLBACK)lpTimer->lpFunc)(lpTimer->wTimerID, 0, lpTimer->dwUser, 0, 0);
70 else if (pFnCallMMDrvFunc16)
71 pFnCallMMDrvFunc16(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) {
129 iData->lpTimers = (LPWINE_TIMERENTRY)
130 HeapReAlloc(GetProcessHeap(), 0,
132 ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
134 iData->lpTimers[idx++] = *lpTimer;
136 /* TIME_ONESHOT is defined as 0 */
137 if (!(lpTimer->wFlags & TIME_PERIODIC))
138 timeKillEvent(lpTimer->wTimerID);
140 lpTimer->uCurTime -= MMSYSTIME_MININTERVAL;
142 lpTimer = lpNextTimer;
144 LeaveCriticalSection(&iData->cs);
147 TIME_TriggerCallBack(&iData->lpTimers[--idx]);
152 /**************************************************************************
153 * TIME_MMSysTimeThread
155 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
157 LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg;
158 volatile HANDLE *pActive = (volatile HANDLE *)&iData->hMMTimer;
159 DWORD last_time, cur_time;
161 usleep(MMSYSTIME_STDINTERVAL * 1000);
162 last_time = GetTickCount();
164 TIME_MMSysTimeCallback(iData);
165 cur_time = GetTickCount();
166 while (last_time < cur_time)
167 last_time += MMSYSTIME_STDINTERVAL;
168 usleep((last_time - cur_time) * 1000);
173 /**************************************************************************
176 void TIME_MMTimeStart(void)
178 /* one could think it's possible to stop the service thread activity when no more
179 * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
180 * without being incremented within the service thread callback.
182 if (!WINMM_IData->hMMTimer) {
183 WINMM_IData->mmSysTimeMS = GetTickCount();
184 WINMM_IData->lpTimerList = NULL;
185 WINMM_IData->hMMTimer = CreateThread(NULL, 0, TIME_MMSysTimeThread, WINMM_IData, 0, NULL);
189 /**************************************************************************
192 void TIME_MMTimeStop(void)
194 if (WINMM_IData->hMMTimer) {
195 HANDLE hMMTimer = WINMM_IData->hMMTimer;
196 WINMM_IData->hMMTimer = 0;
197 WaitForSingleObject(hMMTimer, INFINITE);
198 CloseHandle(hMMTimer);
202 /**************************************************************************
203 * timeGetSystemTime [WINMM.@]
205 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
207 TRACE("(%p, %u);\n", lpTime, wSize);
209 if (wSize >= sizeof(*lpTime)) {
211 lpTime->wType = TIME_MS;
212 lpTime->u.ms = WINMM_IData->mmSysTimeMS;
214 TRACE("=> %lu\n", lpTime->u.ms);
220 /**************************************************************************
221 * TIME_SetEventInternal [internal]
223 WORD TIME_SetEventInternal(UINT wDelay, UINT wResol,
224 FARPROC16 lpFunc, DWORD dwUser, UINT wFlags)
227 LPWINE_TIMERENTRY lpNewTimer;
228 LPWINE_TIMERENTRY lpTimer;
230 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
232 lpNewTimer = (LPWINE_TIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
233 if (lpNewTimer == NULL)
236 if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
241 lpNewTimer->uCurTime = wDelay;
242 lpNewTimer->wDelay = wDelay;
243 lpNewTimer->wResol = wResol;
244 lpNewTimer->lpFunc = lpFunc;
245 lpNewTimer->dwUser = dwUser;
246 lpNewTimer->wFlags = wFlags;
248 EnterCriticalSection(&WINMM_IData->cs);
250 for (lpTimer = WINMM_IData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
251 wNewID = max(wNewID, lpTimer->wTimerID);
254 lpNewTimer->lpNext = WINMM_IData->lpTimerList;
255 WINMM_IData->lpTimerList = lpNewTimer;
256 lpNewTimer->wTimerID = wNewID + 1;
258 LeaveCriticalSection(&WINMM_IData->cs);
260 TRACE("=> %u\n", wNewID + 1);
265 /**************************************************************************
266 * timeSetEvent [WINMM.@]
268 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
269 DWORD dwUser, UINT wFlags)
271 if (wFlags & WINE_TIMER_IS32)
272 WARN("Unknown windows flag... wine internally used.. ooch\n");
274 return TIME_SetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
275 dwUser, wFlags|WINE_TIMER_IS32);
278 /**************************************************************************
279 * timeKillEvent [WINMM.@]
281 MMRESULT WINAPI timeKillEvent(UINT wID)
283 LPWINE_TIMERENTRY* lpTimer;
284 MMRESULT ret = MMSYSERR_INVALPARAM;
286 TRACE("(%u)\n", wID);
287 EnterCriticalSection(&WINMM_IData->cs);
288 /* remove WINE_TIMERENTRY from list */
289 for (lpTimer = &WINMM_IData->lpTimerList; *lpTimer; lpTimer = &(*lpTimer)->lpNext) {
290 if (wID == (*lpTimer)->wTimerID) {
294 LeaveCriticalSection(&WINMM_IData->cs);
297 LPWINE_TIMERENTRY lpTemp = *lpTimer;
299 /* unlink timer of id 'wID' */
300 *lpTimer = (*lpTimer)->lpNext;
301 HeapFree(GetProcessHeap(), 0, lpTemp);
302 ret = TIMERR_NOERROR;
304 WARN("wID=%u is not a valid timer ID\n", wID);
310 /**************************************************************************
311 * timeGetDevCaps [WINMM.@]
313 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
315 TRACE("(%p, %u) !\n", lpCaps, wSize);
317 lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
318 lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
322 /**************************************************************************
323 * timeBeginPeriod [WINMM.@]
325 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
327 TRACE("(%u) !\n", wPeriod);
329 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
330 return TIMERR_NOCANDO;
334 /**************************************************************************
335 * timeEndPeriod [WINMM.@]
337 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
339 TRACE("(%u) !\n", wPeriod);
341 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
342 return TIMERR_NOCANDO;
346 /**************************************************************************
347 * timeGetTime [MMSYSTEM.607]
348 * timeGetTime [WINMM.@]
350 DWORD WINAPI timeGetTime(void)
352 /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
353 * that lets mciavi.drv run correctly
356 ReleaseThunkLock(&count);
357 RestoreThunkLock(count);
359 return WINMM_IData->mmSysTimeMS;