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