Implement VerSetConditionMask by forwarding to ntdll.
[wine/hacks.git] / dlls / winmm / time.c
blobd9e1a89433a1337cd932d185e575cbbc97edc7b7
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <time.h>
27 #ifdef HAVE_SYS_TIME_H
28 # include <sys/time.h>
29 #endif
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
34 #include "mmsystem.h"
35 #include "windef.h"
36 #include "winbase.h"
38 #include "winemm.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mmtime);
45 * FIXME
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 static void TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer)
57 TRACE("before CallBack => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
58 lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
60 /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called
61 * during interrupt time, is allowed to execute very limited number of API calls (like
62 * PostMessage), and must reside in DLL (therefore uses stack of active application). So I
63 * guess current implementation via SetTimer has to be improved upon.
65 switch (lpTimer->wFlags & 0x30) {
66 case TIME_CALLBACK_FUNCTION:
67 if (lpTimer->wFlags & WINE_TIMER_IS32)
68 ((LPTIMECALLBACK)lpTimer->lpFunc)(lpTimer->wTimerID, 0, lpTimer->dwUser, 0, 0);
69 else if (pFnCallMMDrvFunc16)
70 pFnCallMMDrvFunc16(lpTimer->lpFunc, lpTimer->wTimerID, 0,
71 lpTimer->dwUser, 0, 0);
72 break;
73 case TIME_CALLBACK_EVENT_SET:
74 SetEvent((HANDLE)lpTimer->lpFunc);
75 break;
76 case TIME_CALLBACK_EVENT_PULSE:
77 PulseEvent((HANDLE)lpTimer->lpFunc);
78 break;
79 default:
80 FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n",
81 lpTimer->wFlags, lpTimer->lpFunc);
82 break;
84 TRACE("after CallBack !\n");
87 /**************************************************************************
88 * TIME_MMSysTimeCallback
90 static void CALLBACK TIME_MMSysTimeCallback(LPWINE_MM_IDATA iData)
92 LPWINE_TIMERENTRY lpTimer, lpNextTimer;
93 DWORD delta = GetTickCount() - iData->mmSysTimeMS;
94 int idx;
96 TRACE("Time delta: %ld\n", delta);
98 while (delta >= MMSYSTIME_MININTERVAL) {
99 delta -= MMSYSTIME_MININTERVAL;
100 iData->mmSysTimeMS += MMSYSTIME_MININTERVAL;
102 /* since timeSetEvent() and timeKillEvent() can be called
103 * from 16 bit code, there are cases where win16 lock is
104 * locked upon entering timeSetEvent(), and then the mm timer
105 * critical section is locked. This function cannot call the
106 * timer callback with the crit sect locked (because callback
107 * may need to acquire Win16 lock, thus providing a deadlock
108 * situation).
109 * To cope with that, we just copy the WINE_TIMERENTRY struct
110 * that need to trigger the callback, and call it without the
111 * mm timer crit sect locked. The bad side of this
112 * implementation is that, in some cases, the callback may be
113 * invoked *after* a timer has been destroyed...
114 * EPP 99/07/13
116 idx = 0;
118 EnterCriticalSection(&iData->cs);
119 for (lpTimer = iData->lpTimerList; lpTimer != NULL; ) {
120 lpNextTimer = lpTimer->lpNext;
121 if (lpTimer->uCurTime < MMSYSTIME_MININTERVAL) {
122 /* since lpTimer->wDelay is >= MININTERVAL, wCurTime value
123 * shall be correct (>= 0)
125 lpTimer->uCurTime += lpTimer->wDelay - MMSYSTIME_MININTERVAL;
126 if (lpTimer->lpFunc) {
127 if (idx == iData->nSizeLpTimers) {
128 iData->lpTimers = (LPWINE_TIMERENTRY)
129 HeapReAlloc(GetProcessHeap(), 0,
130 iData->lpTimers,
131 ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
133 iData->lpTimers[idx++] = *lpTimer;
135 /* TIME_ONESHOT is defined as 0 */
136 if (!(lpTimer->wFlags & TIME_PERIODIC))
137 timeKillEvent(lpTimer->wTimerID);
138 } else {
139 lpTimer->uCurTime -= MMSYSTIME_MININTERVAL;
141 lpTimer = lpNextTimer;
143 LeaveCriticalSection(&iData->cs);
145 while (idx > 0) {
146 TIME_TriggerCallBack(&iData->lpTimers[--idx]);
151 /**************************************************************************
152 * TIME_MMSysTimeThread
154 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
156 LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg;
157 volatile HANDLE *pActive = (volatile HANDLE *)&iData->hMMTimer;
158 DWORD last_time, cur_time;
160 usleep(MMSYSTIME_STDINTERVAL * 1000);
161 last_time = GetTickCount();
162 while (*pActive) {
163 TIME_MMSysTimeCallback(iData);
164 cur_time = GetTickCount();
165 while (last_time < cur_time)
166 last_time += MMSYSTIME_STDINTERVAL;
167 usleep((last_time - cur_time) * 1000);
169 return 0;
172 /**************************************************************************
173 * TIME_MMTimeStart
175 void TIME_MMTimeStart(void)
177 /* one could think it's possible to stop the service thread activity when no more
178 * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
179 * without being incremented within the service thread callback.
181 if (!WINMM_IData->hMMTimer) {
182 WINMM_IData->mmSysTimeMS = GetTickCount();
183 WINMM_IData->lpTimerList = NULL;
184 WINMM_IData->hMMTimer = CreateThread(NULL, 0, TIME_MMSysTimeThread, WINMM_IData, 0, NULL);
188 /**************************************************************************
189 * TIME_MMTimeStop
191 void TIME_MMTimeStop(void)
193 if (WINMM_IData->hMMTimer) {
194 HANDLE hMMTimer = WINMM_IData->hMMTimer;
195 WINMM_IData->hMMTimer = 0;
196 WaitForSingleObject(hMMTimer, INFINITE);
197 CloseHandle(hMMTimer);
201 /**************************************************************************
202 * timeGetSystemTime [WINMM.@]
204 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
206 TRACE("(%p, %u);\n", lpTime, wSize);
208 if (wSize >= sizeof(*lpTime)) {
209 TIME_MMTimeStart();
210 lpTime->wType = TIME_MS;
211 lpTime->u.ms = WINMM_IData->mmSysTimeMS;
213 TRACE("=> %lu\n", lpTime->u.ms);
216 return 0;
219 /**************************************************************************
220 * TIME_SetEventInternal [internal]
222 WORD TIME_SetEventInternal(UINT wDelay, UINT wResol,
223 FARPROC16 lpFunc, DWORD dwUser, UINT wFlags)
225 WORD wNewID = 0;
226 LPWINE_TIMERENTRY lpNewTimer;
227 LPWINE_TIMERENTRY lpTimer;
229 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
231 lpNewTimer = (LPWINE_TIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
232 if (lpNewTimer == NULL)
233 return 0;
235 if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
236 return 0;
238 TIME_MMTimeStart();
240 lpNewTimer->uCurTime = wDelay;
241 lpNewTimer->wDelay = wDelay;
242 lpNewTimer->wResol = wResol;
243 lpNewTimer->lpFunc = lpFunc;
244 lpNewTimer->dwUser = dwUser;
245 lpNewTimer->wFlags = wFlags;
247 EnterCriticalSection(&WINMM_IData->cs);
249 for (lpTimer = WINMM_IData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
250 wNewID = max(wNewID, lpTimer->wTimerID);
253 lpNewTimer->lpNext = WINMM_IData->lpTimerList;
254 WINMM_IData->lpTimerList = lpNewTimer;
255 lpNewTimer->wTimerID = wNewID + 1;
257 LeaveCriticalSection(&WINMM_IData->cs);
259 TRACE("=> %u\n", wNewID + 1);
261 return wNewID + 1;
264 /**************************************************************************
265 * timeSetEvent [WINMM.@]
267 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
268 DWORD dwUser, UINT wFlags)
270 if (wFlags & WINE_TIMER_IS32)
271 WARN("Unknown windows flag... wine internally used.. ooch\n");
273 return TIME_SetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
274 dwUser, wFlags|WINE_TIMER_IS32);
277 /**************************************************************************
278 * timeKillEvent [WINMM.@]
280 MMRESULT WINAPI timeKillEvent(UINT wID)
282 LPWINE_TIMERENTRY* lpTimer;
283 MMRESULT ret = MMSYSERR_INVALPARAM;
285 TRACE("(%u)\n", wID);
286 EnterCriticalSection(&WINMM_IData->cs);
287 /* remove WINE_TIMERENTRY from list */
288 for (lpTimer = &WINMM_IData->lpTimerList; *lpTimer; lpTimer = &(*lpTimer)->lpNext) {
289 if (wID == (*lpTimer)->wTimerID) {
290 break;
293 LeaveCriticalSection(&WINMM_IData->cs);
295 if (*lpTimer) {
296 LPWINE_TIMERENTRY lpTemp = *lpTimer;
298 /* unlink timer of id 'wID' */
299 *lpTimer = (*lpTimer)->lpNext;
300 HeapFree(GetProcessHeap(), 0, lpTemp);
301 ret = TIMERR_NOERROR;
302 } else {
303 WARN("wID=%u is not a valid timer ID\n", wID);
306 return ret;
309 /**************************************************************************
310 * timeGetDevCaps [WINMM.@]
312 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
314 TRACE("(%p, %u) !\n", lpCaps, wSize);
316 lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
317 lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
318 return 0;
321 /**************************************************************************
322 * timeBeginPeriod [WINMM.@]
324 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
326 TRACE("(%u) !\n", wPeriod);
328 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
329 return TIMERR_NOCANDO;
330 return 0;
333 /**************************************************************************
334 * timeEndPeriod [WINMM.@]
336 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
338 TRACE("(%u) !\n", wPeriod);
340 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
341 return TIMERR_NOCANDO;
342 return 0;
345 /**************************************************************************
346 * timeGetTime [MMSYSTEM.607]
347 * timeGetTime [WINMM.@]
349 DWORD WINAPI timeGetTime(void)
351 /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
352 * that lets mciavi.drv run correctly
354 DWORD count;
355 ReleaseThunkLock(&count);
356 RestoreThunkLock(count);
357 TIME_MMTimeStart();
358 return WINMM_IData->mmSysTimeMS;