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"
27 #ifdef HAVE_SYS_TIME_H
28 # include <sys/time.h>
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mmtime);
46 * We're using "1" as the mininum resolution to the timer,
47 * as Windows 95 does, according to the docs. Maybe it should
48 * depend on the computers resources!
50 #define MMSYSTIME_MININTERVAL (1)
51 #define MMSYSTIME_MAXINTERVAL (65535)
53 #define MMSYSTIME_STDINTERVAL (10) /* reasonable value? */
55 /* ### start build ### */
56 extern WORD CALLBACK TIME_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
57 /* ### stop build ### */
59 static void TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer)
61 TRACE("before CallBack => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
62 lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
64 /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called
65 * during interrupt time, is allowed to execute very limited number of API calls (like
66 * PostMessage), and must reside in DLL (therefore uses stack of active application). So I
67 * guess current implementation via SetTimer has to be improved upon.
69 switch (lpTimer->wFlags & 0x30) {
70 case TIME_CALLBACK_FUNCTION:
71 if (lpTimer->wFlags & WINE_TIMER_IS32)
72 ((LPTIMECALLBACK)lpTimer->lpFunc)(lpTimer->wTimerID, 0, lpTimer->dwUser, 0, 0);
74 TIME_CallTo16_word_wwlll(lpTimer->lpFunc, lpTimer->wTimerID, 0,
75 lpTimer->dwUser, 0, 0);
77 case TIME_CALLBACK_EVENT_SET:
78 SetEvent((HANDLE)lpTimer->lpFunc);
80 case TIME_CALLBACK_EVENT_PULSE:
81 PulseEvent((HANDLE)lpTimer->lpFunc);
84 FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n",
85 lpTimer->wFlags, lpTimer->lpFunc);
88 TRACE("after CallBack !\n");
91 /**************************************************************************
92 * TIME_MMSysTimeCallback
94 static void CALLBACK TIME_MMSysTimeCallback(LPWINE_MM_IDATA iData)
96 LPWINE_TIMERENTRY lpTimer, lpNextTimer;
97 DWORD delta = GetTickCount() - iData->mmSysTimeMS;
100 TRACE("Time delta: %ld\n", delta);
102 while (delta >= MMSYSTIME_MININTERVAL) {
103 delta -= MMSYSTIME_MININTERVAL;
104 iData->mmSysTimeMS += MMSYSTIME_MININTERVAL;
106 /* since timeSetEvent() and timeKillEvent() can be called
107 * from 16 bit code, there are cases where win16 lock is
108 * locked upon entering timeSetEvent(), and then the mm timer
109 * critical section is locked. This function cannot call the
110 * timer callback with the crit sect locked (because callback
111 * may need to acquire Win16 lock, thus providing a deadlock
113 * To cope with that, we just copy the WINE_TIMERENTRY struct
114 * that need to trigger the callback, and call it without the
115 * mm timer crit sect locked. The bad side of this
116 * implementation is that, in some cases, the callback may be
117 * invoked *after* a timer has been destroyed...
122 EnterCriticalSection(&iData->cs);
123 for (lpTimer = iData->lpTimerList; lpTimer != NULL; ) {
124 lpNextTimer = lpTimer->lpNext;
125 if (lpTimer->uCurTime < MMSYSTIME_MININTERVAL) {
126 /* since lpTimer->wDelay is >= MININTERVAL, wCurTime value
127 * shall be correct (>= 0)
129 lpTimer->uCurTime += lpTimer->wDelay - MMSYSTIME_MININTERVAL;
130 if (lpTimer->lpFunc) {
131 if (idx == iData->nSizeLpTimers) {
132 iData->lpTimers = (LPWINE_TIMERENTRY)
133 HeapReAlloc(GetProcessHeap(), 0,
135 ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
137 iData->lpTimers[idx++] = *lpTimer;
139 /* TIME_ONESHOT is defined as 0 */
140 if (!(lpTimer->wFlags & TIME_PERIODIC))
141 timeKillEvent(lpTimer->wTimerID);
143 lpTimer->uCurTime -= MMSYSTIME_MININTERVAL;
145 lpTimer = lpNextTimer;
147 LeaveCriticalSection(&iData->cs);
150 TIME_TriggerCallBack(&iData->lpTimers[--idx]);
155 /**************************************************************************
156 * TIME_MMSysTimeThread
158 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
160 LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg;
161 volatile HANDLE *pActive = (volatile HANDLE *)&iData->hMMTimer;
162 DWORD last_time, cur_time;
164 usleep(MMSYSTIME_STDINTERVAL * 1000);
165 last_time = GetTickCount();
167 TIME_MMSysTimeCallback(iData);
168 cur_time = GetTickCount();
169 while (last_time < cur_time)
170 last_time += MMSYSTIME_STDINTERVAL;
171 usleep((last_time - cur_time) * 1000);
176 /**************************************************************************
179 void TIME_MMTimeStart(void)
181 if (IsBadWritePtr(WINMM_IData, sizeof(WINE_MM_IDATA))) {
182 ERR("iData is not correctly set, please report. Expect failure.\n");
185 /* one could think it's possible to stop the service thread activity when no more
186 * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
187 * without being incremented within the service thread callback.
189 if (!WINMM_IData->hMMTimer) {
190 WINMM_IData->mmSysTimeMS = GetTickCount();
191 WINMM_IData->lpTimerList = NULL;
192 WINMM_IData->hMMTimer = CreateThread(NULL, 0, TIME_MMSysTimeThread, WINMM_IData, 0, NULL);
197 /**************************************************************************
200 void TIME_MMTimeStop(void)
202 if (IsBadWritePtr(WINMM_IData, sizeof(WINE_MM_IDATA))) {
203 ERR("WINMM_IData is not correctly set, please report. Expect failure.\n");
206 if (WINMM_IData->hMMTimer) {
207 HANDLE hMMTimer = WINMM_IData->hMMTimer;
208 WINMM_IData->hMMTimer = 0;
209 WaitForSingleObject(hMMTimer, INFINITE);
210 CloseHandle(hMMTimer);
214 /**************************************************************************
215 * timeGetSystemTime [WINMM.@]
217 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
219 TRACE("(%p, %u);\n", lpTime, wSize);
221 if (wSize >= sizeof(*lpTime)) {
223 lpTime->wType = TIME_MS;
224 lpTime->u.ms = WINMM_IData->mmSysTimeMS;
226 TRACE("=> %lu\n", lpTime->u.ms);
232 /**************************************************************************
233 * timeSetEventInternal [internal]
235 WORD timeSetEventInternal(UINT wDelay, UINT wResol,
236 FARPROC16 lpFunc, DWORD dwUser, UINT wFlags)
239 LPWINE_TIMERENTRY lpNewTimer;
240 LPWINE_TIMERENTRY lpTimer;
242 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
244 lpNewTimer = (LPWINE_TIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
245 if (lpNewTimer == NULL)
248 if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
253 lpNewTimer->uCurTime = wDelay;
254 lpNewTimer->wDelay = wDelay;
255 lpNewTimer->wResol = wResol;
256 lpNewTimer->lpFunc = lpFunc;
257 lpNewTimer->dwUser = dwUser;
258 lpNewTimer->wFlags = wFlags;
260 EnterCriticalSection(&WINMM_IData->cs);
262 for (lpTimer = WINMM_IData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
263 wNewID = max(wNewID, lpTimer->wTimerID);
266 lpNewTimer->lpNext = WINMM_IData->lpTimerList;
267 WINMM_IData->lpTimerList = lpNewTimer;
268 lpNewTimer->wTimerID = wNewID + 1;
270 LeaveCriticalSection(&WINMM_IData->cs);
272 TRACE("=> %u\n", wNewID + 1);
277 /**************************************************************************
278 * timeSetEvent [WINMM.@]
280 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
281 DWORD dwUser, UINT wFlags)
283 if (wFlags & WINE_TIMER_IS32)
284 WARN("Unknown windows flag... wine internally used.. ooch\n");
286 return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
287 dwUser, wFlags|WINE_TIMER_IS32);
290 /**************************************************************************
291 * timeKillEvent [WINMM.@]
293 MMRESULT WINAPI timeKillEvent(UINT wID)
295 LPWINE_TIMERENTRY* lpTimer;
296 MMRESULT ret = MMSYSERR_INVALPARAM;
298 TRACE("(%u)\n", wID);
299 EnterCriticalSection(&WINMM_IData->cs);
300 /* remove WINE_TIMERENTRY from list */
301 for (lpTimer = &WINMM_IData->lpTimerList; *lpTimer; lpTimer = &(*lpTimer)->lpNext) {
302 if (wID == (*lpTimer)->wTimerID) {
306 LeaveCriticalSection(&WINMM_IData->cs);
309 LPWINE_TIMERENTRY lpTemp = *lpTimer;
311 /* unlink timer of id 'wID' */
312 *lpTimer = (*lpTimer)->lpNext;
313 HeapFree(GetProcessHeap(), 0, lpTemp);
314 ret = TIMERR_NOERROR;
316 WARN("wID=%u is not a valid timer ID\n", wID);
322 /**************************************************************************
323 * timeGetDevCaps [WINMM.@]
325 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
327 TRACE("(%p, %u) !\n", lpCaps, wSize);
329 lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
330 lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
334 /**************************************************************************
335 * timeBeginPeriod [WINMM.@]
337 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
339 TRACE("(%u) !\n", wPeriod);
341 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
342 return TIMERR_NOCANDO;
346 /**************************************************************************
347 * timeEndPeriod [WINMM.@]
349 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
351 TRACE("(%u) !\n", wPeriod);
353 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
354 return TIMERR_NOCANDO;
358 /**************************************************************************
359 * timeGetTime [MMSYSTEM.607]
360 * timeGetTime [WINMM.@]
362 DWORD WINAPI timeGetTime(void)
364 /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
365 * that lets mciavi.drv run correctly
368 ReleaseThunkLock(&count);
369 RestoreThunkLock(count);
371 return WINMM_IData->mmSysTimeMS;