All low level driver functions (internals for wave, midi, mixer and
[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 <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
33
34 #include "mmsystem.h"
35 #include "windef.h"
36 #include "winbase.h"
37
38 #include "winemm.h"
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(mmtime);
43
44 /*
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!
49  */
50 #define MMSYSTIME_MININTERVAL (1)
51 #define MMSYSTIME_MAXINTERVAL (65535)
52
53 #define MMSYSTIME_STDINTERVAL (10) /* reasonable value? */
54
55 /* ### start build ### */
56 extern WORD CALLBACK TIME_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
57 /* ### stop build ### */
58
59 static  void    TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer)
60 {
61     TRACE("before CallBack => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
62           lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
63
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.
68      */
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);
73         else
74             TIME_CallTo16_word_wwlll(lpTimer->lpFunc, lpTimer->wTimerID, 0,
75                                      lpTimer->dwUser, 0, 0);
76         break;
77     case TIME_CALLBACK_EVENT_SET:
78         SetEvent((HANDLE)lpTimer->lpFunc);
79         break;
80     case TIME_CALLBACK_EVENT_PULSE:
81         PulseEvent((HANDLE)lpTimer->lpFunc);
82         break;
83     default:
84         FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n",
85               lpTimer->wFlags, lpTimer->lpFunc);
86         break;
87     }
88     TRACE("after CallBack !\n");
89 }
90
91 /**************************************************************************
92  *           TIME_MMSysTimeCallback
93  */
94 static void CALLBACK TIME_MMSysTimeCallback(LPWINE_MM_IDATA iData)
95 {
96     LPWINE_TIMERENTRY   lpTimer, lpNextTimer;
97     DWORD               delta = GetTickCount() - iData->mmSysTimeMS;
98     int                 idx;
99
100     TRACE("Time delta: %ld\n", delta);
101
102     while (delta >= MMSYSTIME_MININTERVAL) {
103         delta -= MMSYSTIME_MININTERVAL;
104         iData->mmSysTimeMS += MMSYSTIME_MININTERVAL;
105
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
112          * situation).
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...
118          * EPP 99/07/13
119          */
120         idx = 0;
121
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)
128                  */
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,
134                                         iData->lpTimers,
135                                         ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
136                     }
137                     iData->lpTimers[idx++] = *lpTimer;
138                 }
139                 /* TIME_ONESHOT is defined as 0 */
140                 if (!(lpTimer->wFlags & TIME_PERIODIC))
141                     timeKillEvent(lpTimer->wTimerID);
142             } else {
143                 lpTimer->uCurTime -= MMSYSTIME_MININTERVAL;
144             }
145             lpTimer = lpNextTimer;
146         }
147         LeaveCriticalSection(&iData->cs);
148
149         while (idx > 0) {
150             TIME_TriggerCallBack(&iData->lpTimers[--idx]);
151         }
152     }
153 }
154
155 /**************************************************************************
156  *           TIME_MMSysTimeThread
157  */
158 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
159 {
160     LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg;
161     volatile HANDLE *pActive = (volatile HANDLE *)&iData->hMMTimer;
162     DWORD last_time, cur_time;
163
164     usleep(MMSYSTIME_STDINTERVAL * 1000);
165     last_time = GetTickCount();
166     while (*pActive) {
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);
172     }
173     return 0;
174 }
175
176 /**************************************************************************
177  *                              TIME_MMTimeStart
178  */
179 void    TIME_MMTimeStart(void)
180 {
181     if (IsBadWritePtr(WINMM_IData, sizeof(WINE_MM_IDATA))) {
182         ERR("iData is not correctly set, please report. Expect failure.\n");
183         return;
184     }
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.
188      */
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);
193     }
194
195 }
196
197 /**************************************************************************
198  *                              TIME_MMTimeStop
199  */
200 void    TIME_MMTimeStop(void)
201 {
202     if (IsBadWritePtr(WINMM_IData, sizeof(WINE_MM_IDATA))) {
203         ERR("WINMM_IData is not correctly set, please report. Expect failure.\n");
204         return;
205     }
206     if (WINMM_IData->hMMTimer) {
207         HANDLE hMMTimer = WINMM_IData->hMMTimer;
208         WINMM_IData->hMMTimer = 0;
209         WaitForSingleObject(hMMTimer, INFINITE);
210         CloseHandle(hMMTimer);
211     }
212 }
213
214 /**************************************************************************
215  *                              timeGetSystemTime       [WINMM.@]
216  */
217 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
218 {
219     TRACE("(%p, %u);\n", lpTime, wSize);
220
221     if (wSize >= sizeof(*lpTime)) {
222         TIME_MMTimeStart();
223         lpTime->wType = TIME_MS;
224         lpTime->u.ms = WINMM_IData->mmSysTimeMS;
225
226         TRACE("=> %lu\n", lpTime->u.ms);
227     }
228
229     return 0;
230 }
231
232 /**************************************************************************
233  *                              timeSetEventInternal    [internal]
234  */
235 WORD    timeSetEventInternal(UINT wDelay, UINT wResol,
236                              FARPROC16 lpFunc, DWORD dwUser, UINT wFlags)
237 {
238     WORD                wNewID = 0;
239     LPWINE_TIMERENTRY   lpNewTimer;
240     LPWINE_TIMERENTRY   lpTimer;
241
242     TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
243
244     lpNewTimer = (LPWINE_TIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
245     if (lpNewTimer == NULL)
246         return 0;
247
248     if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
249         return 0;
250
251     TIME_MMTimeStart();
252
253     lpNewTimer->uCurTime = wDelay;
254     lpNewTimer->wDelay = wDelay;
255     lpNewTimer->wResol = wResol;
256     lpNewTimer->lpFunc = lpFunc;
257     lpNewTimer->dwUser = dwUser;
258     lpNewTimer->wFlags = wFlags;
259
260     EnterCriticalSection(&WINMM_IData->cs);
261
262     for (lpTimer = WINMM_IData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
263         wNewID = max(wNewID, lpTimer->wTimerID);
264     }
265
266     lpNewTimer->lpNext = WINMM_IData->lpTimerList;
267     WINMM_IData->lpTimerList = lpNewTimer;
268     lpNewTimer->wTimerID = wNewID + 1;
269
270     LeaveCriticalSection(&WINMM_IData->cs);
271
272     TRACE("=> %u\n", wNewID + 1);
273
274     return wNewID + 1;
275 }
276
277 /**************************************************************************
278  *                              timeSetEvent            [WINMM.@]
279  */
280 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
281                              DWORD dwUser, UINT wFlags)
282 {
283     if (wFlags & WINE_TIMER_IS32)
284         WARN("Unknown windows flag... wine internally used.. ooch\n");
285
286     return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
287                                 dwUser, wFlags|WINE_TIMER_IS32);
288 }
289
290 /**************************************************************************
291  *                              timeKillEvent           [WINMM.@]
292  */
293 MMRESULT WINAPI timeKillEvent(UINT wID)
294 {
295     LPWINE_TIMERENTRY*  lpTimer;
296     MMRESULT            ret = MMSYSERR_INVALPARAM;
297
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) {
303             break;
304         }
305     }
306     LeaveCriticalSection(&WINMM_IData->cs);
307
308     if (*lpTimer) {
309         LPWINE_TIMERENTRY       lpTemp = *lpTimer;
310
311         /* unlink timer of id 'wID' */
312         *lpTimer = (*lpTimer)->lpNext;
313         HeapFree(GetProcessHeap(), 0, lpTemp);
314         ret = TIMERR_NOERROR;
315     } else {
316         WARN("wID=%u is not a valid timer ID\n", wID);
317     }
318
319     return ret;
320 }
321
322 /**************************************************************************
323  *                              timeGetDevCaps          [WINMM.@]
324  */
325 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
326 {
327     TRACE("(%p, %u) !\n", lpCaps, wSize);
328
329     lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
330     lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
331     return 0;
332 }
333
334 /**************************************************************************
335  *                              timeBeginPeriod         [WINMM.@]
336  */
337 MMRESULT WINAPI timeBeginPeriod(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  *                              timeEndPeriod           [WINMM.@]
348  */
349 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
350 {
351     TRACE("(%u) !\n", wPeriod);
352
353     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
354         return TIMERR_NOCANDO;
355     return 0;
356 }
357
358 /**************************************************************************
359  *                              timeGetTime    [MMSYSTEM.607]
360  *                              timeGetTime    [WINMM.@]
361  */
362 DWORD WINAPI timeGetTime(void)
363 {
364     /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
365      * that lets mciavi.drv run correctly
366      */
367     DWORD count;
368     ReleaseThunkLock(&count);
369     RestoreThunkLock(count);
370     TIME_MMTimeStart();
371     return WINMM_IData->mmSysTimeMS;
372 }