Removed last dependancies between MCI drivers and WINMM/MMSYSTEM
[wine] / multimedia / time.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  * MMSYTEM time functions
5  *
6  * Copyright 1993 Martin Ayotte
7  */
8
9 #include <time.h>
10 #include <sys/time.h>
11 #include "winbase.h"
12 #include "callback.h"
13 #include "winemm.h"
14 #include "services.h"
15 #include "syslevel.h"
16 #include "debugtools.h"
17
18 DEFAULT_DEBUG_CHANNEL(mmtime)
19
20 /*
21  * FIXME
22  * We're using "1" as the mininum resolution to the timer,
23  * as Windows 95 does, according to the docs. Maybe it should
24  * depend on the computers resources!
25  */
26 #define MMSYSTIME_MININTERVAL /* (1) */ (10)
27 #define MMSYSTIME_MAXINTERVAL (65535)
28
29 static  void    TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer, DWORD dwCurrent)
30 {
31     TRACE("before CallBack (%lu) => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
32           dwCurrent, lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
33         
34     /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called 
35      *          during interrupt time,  is allowed to execute very limited number of API calls (like
36      *          PostMessage), and must reside in DLL (therefore uses stack of active application). So I 
37      *       guess current implementation via SetTimer has to be improved upon.         
38      */
39     switch (lpTimer->wFlags & 0x30) {
40     case TIME_CALLBACK_FUNCTION:
41         if (lpTimer->wFlags & WINE_TIMER_IS32)
42             ((LPTIMECALLBACK)lpTimer->lpFunc)(lpTimer->wTimerID, 0, lpTimer->dwUser, 0, 0);
43         else
44             Callbacks->CallTimeFuncProc(lpTimer->lpFunc,
45                                         lpTimer->wTimerID, 0,
46                                         lpTimer->dwUser, 0, 0);
47         break;
48     case TIME_CALLBACK_EVENT_SET:
49         SetEvent((HANDLE)lpTimer->lpFunc);
50         break;
51     case TIME_CALLBACK_EVENT_PULSE:
52         PulseEvent((HANDLE)lpTimer->lpFunc);
53         break;
54     default:
55         FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n", lpTimer->wFlags, lpTimer->lpFunc);
56         break;
57     }
58     TRACE("after CallBack !\n");
59 }
60
61 /**************************************************************************
62  *           TIME_MMSysTimeCallback
63  */
64 static void CALLBACK TIME_MMSysTimeCallback(ULONG_PTR ptr_)
65 {
66     LPWINE_TIMERENTRY   lpTimer, lpNextTimer;
67     LPWINE_MM_IDATA     iData = (LPWINE_MM_IDATA)ptr_;
68     int                 idx;
69
70     iData->mmSysTimeMS += MMSYSTIME_MININTERVAL;
71
72     /* since timeSetEvent() and timeKillEvent() can be called
73      * from 16 bit code, there are cases where win16 lock is
74      * locked upon entering timeSetEvent(), and then the mm timer 
75      * critical section is locked. This function cannot call the
76      * timer callback with the crit sect locked (because callback
77      * may need to acquire Win16 lock, thus providing a deadlock
78      * situation).
79      * To cope with that, we just copy the WINE_TIMERENTRY struct
80      * that need to trigger the callback, and call it without the
81      * mm timer crit sect locked. The bad side of this 
82      * implementation is that, in some cases, the callback may be
83      * invoked *after* a timer has been destroyed...
84      * EPP 99/07/13
85      */
86     idx = 0;
87
88     EnterCriticalSection(&iData->cs);
89     for (lpTimer = iData->lpTimerList; lpTimer != NULL; ) {
90         lpNextTimer = lpTimer->lpNext;
91         if (lpTimer->uCurTime < MMSYSTIME_MININTERVAL) {
92             /* since lpTimer->wDelay is >= MININTERVAL, wCurTime value
93              * shall be correct (>= 0)
94              */
95             lpTimer->uCurTime += lpTimer->wDelay - MMSYSTIME_MININTERVAL;
96             if (lpTimer->lpFunc) {
97                 if (idx == iData->nSizeLpTimers) {
98                     iData->lpTimers = (LPWINE_TIMERENTRY)
99                         HeapReAlloc(GetProcessHeap(), 0, 
100                                     iData->lpTimers, 
101                                     ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
102                 }
103                 iData->lpTimers[idx++] = *lpTimer;
104             }
105             /* TIME_ONESHOT is defined as 0 */
106             if (!(lpTimer->wFlags & TIME_PERIODIC))
107                 timeKillEvent(lpTimer->wTimerID);
108         } else {
109             lpTimer->uCurTime -= MMSYSTIME_MININTERVAL;
110         }
111         lpTimer = lpNextTimer;
112     }
113     LeaveCriticalSection(&iData->cs);
114
115     while (idx > 0) {
116         TIME_TriggerCallBack(&iData->lpTimers[--idx], iData->mmSysTimeMS);
117     }
118 }
119
120 /**************************************************************************
121  *                              MULTIMEDIA_MMTimeStart          [internal]
122  */
123 static  LPWINE_MM_IDATA MULTIMEDIA_MMTimeStart(void)
124 {
125     LPWINE_MM_IDATA     iData = MULTIMEDIA_GetIData();
126
127     if (IsBadWritePtr(iData, sizeof(WINE_MM_IDATA))) {
128         ERR("iData is not correctly set, please report. Expect failure.\n");
129         return 0;
130     }
131     /* FIXME: the service timer is never killed, even when process is
132      * detached from this DLL
133      */
134     /* one could think it's possible to stop the service thread activity when no more
135      * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
136      * without being incremented within the service thread callback.
137      */
138     if (!iData->hMMTimer) {
139         iData->mmSysTimeMS = GetTickCount();
140         iData->lpTimerList = NULL;
141         iData->hMMTimer = SERVICE_AddTimer(MMSYSTIME_MININTERVAL*1000L, TIME_MMSysTimeCallback, (DWORD)iData);
142     }
143
144     return iData;
145 }
146
147 /**************************************************************************
148  *                              timeGetSystemTime       [WINMM.140]
149  */
150 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
151 {
152     TRACE("(%p, %u);\n", lpTime, wSize);
153
154     if (wSize >= sizeof(*lpTime)) {
155         lpTime->wType = TIME_MS;
156         lpTime->u.ms = MULTIMEDIA_MMTimeStart()->mmSysTimeMS;
157
158         TRACE("=> %lu\n", lpTime->u.ms);
159     }
160
161     return 0;
162 }
163
164 /**************************************************************************
165  *                              timeGetSystemTime       [MMSYSTEM.601]
166  */
167 MMRESULT16 WINAPI timeGetSystemTime16(LPMMTIME16 lpTime, UINT16 wSize)
168 {
169     TRACE("(%p, %u);\n", lpTime, wSize);
170
171     if (wSize >= sizeof(*lpTime)) {
172         lpTime->wType = TIME_MS;
173         lpTime->u.ms = MULTIMEDIA_MMTimeStart()->mmSysTimeMS;
174
175         TRACE("=> %lu\n", lpTime->u.ms);
176     }
177
178     return 0;
179 }
180
181 /**************************************************************************
182  *                              timeSetEventInternal    [internal]
183  */
184 static  WORD    timeSetEventInternal(UINT wDelay, UINT wResol,
185                                      FARPROC16 lpFunc, DWORD dwUser, UINT wFlags)
186 {
187     WORD                wNewID = 0;
188     LPWINE_TIMERENTRY   lpNewTimer;
189     LPWINE_TIMERENTRY   lpTimer;
190     LPWINE_MM_IDATA     iData;
191
192     TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
193
194     lpNewTimer = (LPWINE_TIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
195     if (lpNewTimer == NULL)
196         return 0;
197
198     if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
199         return 0;
200
201     iData = MULTIMEDIA_MMTimeStart();
202
203     lpNewTimer->uCurTime = wDelay;
204     lpNewTimer->wDelay = wDelay;
205     lpNewTimer->wResol = wResol;
206     lpNewTimer->lpFunc = lpFunc;
207     lpNewTimer->dwUser = dwUser;
208     lpNewTimer->wFlags = wFlags;
209
210     EnterCriticalSection(&iData->cs);
211
212     for (lpTimer = iData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
213         wNewID = MAX(wNewID, lpTimer->wTimerID);
214     }
215     
216     lpNewTimer->lpNext = iData->lpTimerList;
217     iData->lpTimerList = lpNewTimer;
218     lpNewTimer->wTimerID = wNewID + 1;
219
220     LeaveCriticalSection(&iData->cs);
221
222     TRACE("=> %u\n", wNewID + 1);
223
224     return wNewID + 1;
225 }
226
227 /**************************************************************************
228  *                              timeSetEvent            [MMSYSTEM.602]
229  */
230 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
231                              DWORD dwUser, UINT wFlags)
232 {
233     if (wFlags & WINE_TIMER_IS32)
234         WARN("Unknown windows flag... wine internally used.. ooch\n");
235
236     return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc, 
237                                 dwUser, wFlags|WINE_TIMER_IS32);
238 }
239
240 /**************************************************************************
241  *                              timeSetEvent            [MMSYSTEM.602]
242  */
243 MMRESULT16 WINAPI timeSetEvent16(UINT16 wDelay, UINT16 wResol, LPTIMECALLBACK16 lpFunc, 
244                                  DWORD dwUser, UINT16 wFlags)
245 {
246     if (wFlags & WINE_TIMER_IS32)
247         WARN("Unknown windows flag... wine internally used.. ooch\n");
248
249     return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc, 
250                                 dwUser, wFlags & ~WINE_TIMER_IS32);
251 }
252
253 /**************************************************************************
254  *                              timeKillEvent           [WINMM.142]
255  */
256 MMRESULT WINAPI timeKillEvent(UINT wID)
257 {
258     LPWINE_TIMERENTRY*  lpTimer;
259     LPWINE_MM_IDATA     iData = MULTIMEDIA_GetIData();
260     MMRESULT            ret = MMSYSERR_INVALPARAM;
261
262     TRACE("(%u)\n", wID);
263     EnterCriticalSection(&iData->cs);
264     /* remove WINE_TIMERENTRY from list */
265     for (lpTimer = &iData->lpTimerList; *lpTimer; lpTimer = &(*lpTimer)->lpNext) {
266         if (wID == (*lpTimer)->wTimerID) {
267             break;
268         }
269     }
270     LeaveCriticalSection(&iData->cs);
271     
272     if (*lpTimer) {
273         LPWINE_TIMERENTRY       lpTemp = *lpTimer;
274
275         /* unlink timer of id 'wID' */
276         *lpTimer = (*lpTimer)->lpNext;
277         HeapFree(GetProcessHeap(), 0, lpTemp);
278         ret = TIMERR_NOERROR;
279     } else {
280         WARN("wID=%u is not a valid timer ID\n", wID);
281     }
282
283     return ret;
284 }
285
286 /**************************************************************************
287  *                              timeKillEvent           [MMSYSTEM.603]
288  */
289 MMRESULT16 WINAPI timeKillEvent16(UINT16 wID)
290 {
291     return timeKillEvent(wID);
292 }
293
294 /**************************************************************************
295  *                              timeGetDevCaps          [WINMM.139]
296  */
297 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
298 {
299     TRACE("(%p, %u) !\n", lpCaps, wSize);
300
301     lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
302     lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
303     return 0;
304 }
305
306 /**************************************************************************
307  *                              timeGetDevCaps          [MMSYSTEM.604]
308  */
309 MMRESULT16 WINAPI timeGetDevCaps16(LPTIMECAPS16 lpCaps, UINT16 wSize)
310 {
311     TRACE("(%p, %u) !\n", lpCaps, wSize);
312
313     lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
314     lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
315     return 0;
316 }
317
318 /**************************************************************************
319  *                              timeBeginPeriod         [WINMM.137]
320  */
321 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
322 {
323     TRACE("(%u) !\n", wPeriod);
324
325     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
326         return TIMERR_NOCANDO;
327     return 0;
328 }
329
330 /**************************************************************************
331  *                              timeBeginPeriod16       [MMSYSTEM.605]
332  */
333 MMRESULT16 WINAPI timeBeginPeriod16(UINT16 wPeriod)
334 {
335     TRACE("(%u) !\n", wPeriod);
336
337     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
338         return TIMERR_NOCANDO;
339     return 0;
340 }
341
342 /**************************************************************************
343  *                              timeEndPeriod           [WINMM.138]
344  */
345 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
346 {
347     TRACE("(%u) !\n", wPeriod);
348
349     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
350         return TIMERR_NOCANDO;
351     return 0;
352 }
353
354 /**************************************************************************
355  *                              timeEndPeriod16         [MMSYSTEM.606]
356  */
357 MMRESULT16 WINAPI timeEndPeriod16(UINT16 wPeriod)
358 {
359     TRACE("(%u) !\n", wPeriod);
360
361     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
362         return TIMERR_NOCANDO;
363     return 0;
364 }
365
366 /**************************************************************************
367  *                              timeGetTime    [MMSYSTEM.607][WINMM.141]
368  */
369 DWORD WINAPI timeGetTime(void)
370 {
371     /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
372      * that lets mciavi.drv run correctly
373      */
374     if ( _ConfirmWin16Lock() ) {
375         SYSLEVEL_ReleaseWin16Lock();
376         SYSLEVEL_RestoreWin16Lock();
377     }
378
379     return MULTIMEDIA_MMTimeStart()->mmSysTimeMS;
380 }