Implemented sound flushing.
[wine] / dlls / winmm / time.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  * MMSYSTEM time functions
5  *
6  * Copyright 1993 Martin Ayotte
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include <time.h>
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
30 #endif
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34
35 #include "windef.h"
36 #include "winbase.h"
37 #include "mmsystem.h"
38
39 #include "winemm.h"
40
41 #include "wine/debug.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(mmtime);
44
45 /*
46  * FIXME
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!
50  */
51 #define MMSYSTIME_MININTERVAL (1)
52 #define MMSYSTIME_MAXINTERVAL (65535)
53
54 #define MMSYSTIME_STDINTERVAL (10) /* reasonable value? */
55
56 static  void    TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer)
57 {
58     TRACE("before CallBack => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
59           lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
60
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.
65      */
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);
73         break;
74     case TIME_CALLBACK_EVENT_SET:
75         SetEvent((HANDLE)lpTimer->lpFunc);
76         break;
77     case TIME_CALLBACK_EVENT_PULSE:
78         PulseEvent((HANDLE)lpTimer->lpFunc);
79         break;
80     default:
81         FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n",
82               lpTimer->wFlags, lpTimer->lpFunc);
83         break;
84     }
85     TRACE("after CallBack !\n");
86 }
87
88 /**************************************************************************
89  *           TIME_MMSysTimeCallback
90  */
91 static void CALLBACK TIME_MMSysTimeCallback(LPWINE_MM_IDATA iData)
92 {
93     LPWINE_TIMERENTRY   lpTimer, lpNextTimer;
94     DWORD               delta = GetTickCount() - iData->mmSysTimeMS;
95     int                 idx;
96
97     TRACE("Time delta: %ld\n", delta);
98
99     while (delta >= MMSYSTIME_MININTERVAL) {
100         delta -= MMSYSTIME_MININTERVAL;
101         iData->mmSysTimeMS += MMSYSTIME_MININTERVAL;
102
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
109          * situation).
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...
115          * EPP 99/07/13
116          */
117         idx = 0;
118
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)
125                  */
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,
131                                         iData->lpTimers,
132                                         ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
133                     }
134                     iData->lpTimers[idx++] = *lpTimer;
135                 }
136                 /* TIME_ONESHOT is defined as 0 */
137                 if (!(lpTimer->wFlags & TIME_PERIODIC))
138                     timeKillEvent(lpTimer->wTimerID);
139             } else {
140                 lpTimer->uCurTime -= MMSYSTIME_MININTERVAL;
141             }
142             lpTimer = lpNextTimer;
143         }
144         LeaveCriticalSection(&iData->cs);
145
146         while (idx > 0) {
147             TIME_TriggerCallBack(&iData->lpTimers[--idx]);
148         }
149     }
150 }
151
152 /**************************************************************************
153  *           TIME_MMSysTimeThread
154  */
155 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
156 {
157     LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg;
158     volatile HANDLE *pActive = (volatile HANDLE *)&iData->hMMTimer;
159     DWORD last_time, cur_time;
160
161     usleep(MMSYSTIME_STDINTERVAL * 1000);
162     last_time = GetTickCount();
163     while (*pActive) {
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);
169     }
170     return 0;
171 }
172
173 /**************************************************************************
174  *                              TIME_MMTimeStart
175  */
176 void    TIME_MMTimeStart(void)
177 {
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.
181      */
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);
186     }
187 }
188
189 /**************************************************************************
190  *                              TIME_MMTimeStop
191  */
192 void    TIME_MMTimeStop(void)
193 {
194     if (WINMM_IData->hMMTimer) {
195         HANDLE hMMTimer = WINMM_IData->hMMTimer;
196         WINMM_IData->hMMTimer = 0;
197         WaitForSingleObject(hMMTimer, INFINITE);
198         CloseHandle(hMMTimer);
199     }
200 }
201
202 /**************************************************************************
203  *                              timeGetSystemTime       [WINMM.@]
204  */
205 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
206 {
207     TRACE("(%p, %u);\n", lpTime, wSize);
208
209     if (wSize >= sizeof(*lpTime)) {
210         TIME_MMTimeStart();
211         lpTime->wType = TIME_MS;
212         lpTime->u.ms = WINMM_IData->mmSysTimeMS;
213
214         TRACE("=> %lu\n", lpTime->u.ms);
215     }
216
217     return 0;
218 }
219
220 /**************************************************************************
221  *                              TIME_SetEventInternal   [internal]
222  */
223 WORD    TIME_SetEventInternal(UINT wDelay, UINT wResol,
224                               FARPROC16 lpFunc, DWORD dwUser, UINT wFlags)
225 {
226     WORD                wNewID = 0;
227     LPWINE_TIMERENTRY   lpNewTimer;
228     LPWINE_TIMERENTRY   lpTimer;
229
230     TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
231
232     lpNewTimer = (LPWINE_TIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
233     if (lpNewTimer == NULL)
234         return 0;
235
236     if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
237         return 0;
238
239     TIME_MMTimeStart();
240
241     lpNewTimer->uCurTime = wDelay;
242     lpNewTimer->wDelay = wDelay;
243     lpNewTimer->wResol = wResol;
244     lpNewTimer->lpFunc = lpFunc;
245     lpNewTimer->dwUser = dwUser;
246     lpNewTimer->wFlags = wFlags;
247
248     EnterCriticalSection(&WINMM_IData->cs);
249
250     for (lpTimer = WINMM_IData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
251         wNewID = max(wNewID, lpTimer->wTimerID);
252     }
253
254     lpNewTimer->lpNext = WINMM_IData->lpTimerList;
255     WINMM_IData->lpTimerList = lpNewTimer;
256     lpNewTimer->wTimerID = wNewID + 1;
257
258     LeaveCriticalSection(&WINMM_IData->cs);
259
260     TRACE("=> %u\n", wNewID + 1);
261
262     return wNewID + 1;
263 }
264
265 /**************************************************************************
266  *                              timeSetEvent            [WINMM.@]
267  */
268 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
269                              DWORD dwUser, UINT wFlags)
270 {
271     if (wFlags & WINE_TIMER_IS32)
272         WARN("Unknown windows flag... wine internally used.. ooch\n");
273
274     return TIME_SetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
275                                  dwUser, wFlags|WINE_TIMER_IS32);
276 }
277
278 /**************************************************************************
279  *                              timeKillEvent           [WINMM.@]
280  */
281 MMRESULT WINAPI timeKillEvent(UINT wID)
282 {
283     LPWINE_TIMERENTRY*  lpTimer;
284     MMRESULT            ret = MMSYSERR_INVALPARAM;
285
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) {
291             break;
292         }
293     }
294     LeaveCriticalSection(&WINMM_IData->cs);
295
296     if (*lpTimer) {
297         LPWINE_TIMERENTRY       lpTemp = *lpTimer;
298
299         /* unlink timer of id 'wID' */
300         *lpTimer = (*lpTimer)->lpNext;
301         HeapFree(GetProcessHeap(), 0, lpTemp);
302         ret = TIMERR_NOERROR;
303     } else {
304         WARN("wID=%u is not a valid timer ID\n", wID);
305     }
306
307     return ret;
308 }
309
310 /**************************************************************************
311  *                              timeGetDevCaps          [WINMM.@]
312  */
313 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
314 {
315     TRACE("(%p, %u) !\n", lpCaps, wSize);
316
317     lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
318     lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
319     return 0;
320 }
321
322 /**************************************************************************
323  *                              timeBeginPeriod         [WINMM.@]
324  */
325 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
326 {
327     TRACE("(%u) !\n", wPeriod);
328
329     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
330         return TIMERR_NOCANDO;
331     return 0;
332 }
333
334 /**************************************************************************
335  *                              timeEndPeriod           [WINMM.@]
336  */
337 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
338 {
339     TRACE("(%u) !\n", wPeriod);
340
341     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
342         return TIMERR_NOCANDO;
343     return 0;
344 }
345
346 /**************************************************************************
347  *                              timeGetTime    [MMSYSTEM.607]
348  *                              timeGetTime    [WINMM.@]
349  */
350 DWORD WINAPI timeGetTime(void)
351 {
352     /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
353      * that lets mciavi.drv run correctly
354      */
355     DWORD count;
356     ReleaseThunkLock(&count);
357     RestoreThunkLock(count);
358     TIME_MMTimeStart();
359     return WINMM_IData->mmSysTimeMS;
360 }