Restored managed mode config file option.
[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 #include <sys/time.h>
28 #include <unistd.h>
29
30 #include "mmsystem.h"
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35
36 #include "wine/mmsystem16.h"
37 #include "winemm.h"
38
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(mmtime);
42
43 /*
44  * FIXME
45  * We're using "1" as the mininum resolution to the timer,
46  * as Windows 95 does, according to the docs. Maybe it should
47  * depend on the computers resources!
48  */
49 #define MMSYSTIME_MININTERVAL (1)
50 #define MMSYSTIME_MAXINTERVAL (65535)
51
52 #define MMSYSTIME_STDINTERVAL (10) /* reasonable value? */
53
54 /* ### start build ### */
55 extern WORD CALLBACK TIME_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
56 /* ### stop build ### */
57
58 static  void    TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer)
59 {
60     TRACE("before CallBack => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
61           lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
62         
63     /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called 
64      *          during interrupt time,  is allowed to execute very limited number of API calls (like
65      *          PostMessage), and must reside in DLL (therefore uses stack of active application). So I 
66      *       guess current implementation via SetTimer has to be improved upon.         
67      */
68     switch (lpTimer->wFlags & 0x30) {
69     case TIME_CALLBACK_FUNCTION:
70         if (lpTimer->wFlags & WINE_TIMER_IS32)
71             ((LPTIMECALLBACK)lpTimer->lpFunc)(lpTimer->wTimerID, 0, lpTimer->dwUser, 0, 0);
72         else
73             TIME_CallTo16_word_wwlll(lpTimer->lpFunc, lpTimer->wTimerID, 0,
74                                      lpTimer->dwUser, 0, 0);
75         break;
76     case TIME_CALLBACK_EVENT_SET:
77         SetEvent((HANDLE)lpTimer->lpFunc);
78         break;
79     case TIME_CALLBACK_EVENT_PULSE:
80         PulseEvent((HANDLE)lpTimer->lpFunc);
81         break;
82     default:
83         FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n", 
84               lpTimer->wFlags, lpTimer->lpFunc);
85         break;
86     }
87     TRACE("after CallBack !\n");
88 }
89
90 /**************************************************************************
91  *           TIME_MMSysTimeCallback
92  */
93 static void CALLBACK TIME_MMSysTimeCallback(LPWINE_MM_IDATA iData)
94 {
95     LPWINE_TIMERENTRY   lpTimer, lpNextTimer;
96     DWORD               delta = GetTickCount() - iData->mmSysTimeMS;
97     int                 idx;
98
99     TRACE("Time delta: %ld\n", delta);
100
101     while (delta >= MMSYSTIME_MININTERVAL) {
102         delta -= MMSYSTIME_MININTERVAL;
103         iData->mmSysTimeMS += MMSYSTIME_MININTERVAL;
104         
105         /* since timeSetEvent() and timeKillEvent() can be called
106          * from 16 bit code, there are cases where win16 lock is
107          * locked upon entering timeSetEvent(), and then the mm timer 
108          * critical section is locked. This function cannot call the
109          * timer callback with the crit sect locked (because callback
110          * may need to acquire Win16 lock, thus providing a deadlock
111          * situation).
112          * To cope with that, we just copy the WINE_TIMERENTRY struct
113          * that need to trigger the callback, and call it without the
114          * mm timer crit sect locked. The bad side of this 
115          * implementation is that, in some cases, the callback may be
116          * invoked *after* a timer has been destroyed...
117          * EPP 99/07/13
118          */
119         idx = 0;
120         
121         EnterCriticalSection(&iData->cs);
122         for (lpTimer = iData->lpTimerList; lpTimer != NULL; ) {
123             lpNextTimer = lpTimer->lpNext;
124             if (lpTimer->uCurTime < MMSYSTIME_MININTERVAL) {
125                 /* since lpTimer->wDelay is >= MININTERVAL, wCurTime value
126                  * shall be correct (>= 0)
127                  */
128                 lpTimer->uCurTime += lpTimer->wDelay - MMSYSTIME_MININTERVAL;
129                 if (lpTimer->lpFunc) {
130                     if (idx == iData->nSizeLpTimers) {
131                         iData->lpTimers = (LPWINE_TIMERENTRY)
132                             HeapReAlloc(GetProcessHeap(), 0, 
133                                         iData->lpTimers, 
134                                         ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
135                     }
136                     iData->lpTimers[idx++] = *lpTimer;
137                 }
138                 /* TIME_ONESHOT is defined as 0 */
139                 if (!(lpTimer->wFlags & TIME_PERIODIC))
140                     timeKillEvent(lpTimer->wTimerID);
141             } else {
142                 lpTimer->uCurTime -= MMSYSTIME_MININTERVAL;
143             }
144             lpTimer = lpNextTimer;
145         }
146         LeaveCriticalSection(&iData->cs);
147         
148         while (idx > 0) {
149             TIME_TriggerCallBack(&iData->lpTimers[--idx]);
150         }
151     }
152 }
153
154 /**************************************************************************
155  *           TIME_MMSysTimeThread
156  */
157 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
158 {
159     LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg;
160     volatile HANDLE *pActive = (volatile HANDLE *)&iData->hMMTimer;
161     DWORD last_time, cur_time;
162
163     usleep(MMSYSTIME_STDINTERVAL * 1000);
164     last_time = GetTickCount();
165     while (*pActive) {
166         TIME_MMSysTimeCallback(iData);
167         cur_time = GetTickCount();
168         while (last_time < cur_time)
169             last_time += MMSYSTIME_STDINTERVAL;
170         usleep((last_time - cur_time) * 1000);
171     }
172     return 0;
173 }
174
175 /**************************************************************************
176  *                              TIME_MMTimeStart
177  */
178 LPWINE_MM_IDATA TIME_MMTimeStart(void)
179 {
180     LPWINE_MM_IDATA     iData = MULTIMEDIA_GetIData();
181
182     if (IsBadWritePtr(iData, sizeof(WINE_MM_IDATA))) {
183         ERR("iData is not correctly set, please report. Expect failure.\n");
184         return 0;
185     }
186     /* one could think it's possible to stop the service thread activity when no more
187      * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
188      * without being incremented within the service thread callback.
189      */
190     if (!iData->hMMTimer) {
191         iData->mmSysTimeMS = GetTickCount();
192         iData->lpTimerList = NULL;
193         iData->hMMTimer = CreateThread(NULL, 0, TIME_MMSysTimeThread, iData, 0, NULL);
194     }
195
196     return iData;
197 }
198
199 /**************************************************************************
200  *                              TIME_MMTimeStop
201  */
202 void    TIME_MMTimeStop(void)
203 {
204     LPWINE_MM_IDATA     iData = MULTIMEDIA_GetIData();
205
206     if (IsBadWritePtr(iData, sizeof(WINE_MM_IDATA))) {
207         ERR("iData is not correctly set, please report. Expect failure.\n");
208         return;
209     }
210     if (iData->hMMTimer) {
211         HANDLE hMMTimer = iData->hMMTimer;
212         iData->hMMTimer = 0;
213         WaitForSingleObject(hMMTimer, INFINITE);
214         CloseHandle(hMMTimer);
215     }
216 }
217
218 /**************************************************************************
219  *                              timeGetSystemTime       [WINMM.@]
220  */
221 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
222 {
223     TRACE("(%p, %u);\n", lpTime, wSize);
224
225     if (wSize >= sizeof(*lpTime)) {
226         lpTime->wType = TIME_MS;
227         lpTime->u.ms = TIME_MMTimeStart()->mmSysTimeMS;
228
229         TRACE("=> %lu\n", lpTime->u.ms);
230     }
231
232     return 0;
233 }
234
235 /**************************************************************************
236  *                              timeGetSystemTime       [MMSYSTEM.601]
237  */
238 MMRESULT16 WINAPI timeGetSystemTime16(LPMMTIME16 lpTime, UINT16 wSize)
239 {
240     TRACE("(%p, %u);\n", lpTime, wSize);
241
242     if (wSize >= sizeof(*lpTime)) {
243         lpTime->wType = TIME_MS;
244         lpTime->u.ms = TIME_MMTimeStart()->mmSysTimeMS;
245
246         TRACE("=> %lu\n", lpTime->u.ms);
247     }
248
249     return 0;
250 }
251
252 /**************************************************************************
253  *                              timeSetEventInternal    [internal]
254  */
255 static  WORD    timeSetEventInternal(UINT wDelay, UINT wResol,
256                                      FARPROC16 lpFunc, DWORD dwUser, UINT wFlags)
257 {
258     WORD                wNewID = 0;
259     LPWINE_TIMERENTRY   lpNewTimer;
260     LPWINE_TIMERENTRY   lpTimer;
261     LPWINE_MM_IDATA     iData;
262
263     TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
264
265     lpNewTimer = (LPWINE_TIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
266     if (lpNewTimer == NULL)
267         return 0;
268
269     if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
270         return 0;
271
272     iData = TIME_MMTimeStart();
273
274     lpNewTimer->uCurTime = wDelay;
275     lpNewTimer->wDelay = wDelay;
276     lpNewTimer->wResol = wResol;
277     lpNewTimer->lpFunc = lpFunc;
278     lpNewTimer->dwUser = dwUser;
279     lpNewTimer->wFlags = wFlags;
280
281     EnterCriticalSection(&iData->cs);
282
283     for (lpTimer = iData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
284         wNewID = max(wNewID, lpTimer->wTimerID);
285     }
286     
287     lpNewTimer->lpNext = iData->lpTimerList;
288     iData->lpTimerList = lpNewTimer;
289     lpNewTimer->wTimerID = wNewID + 1;
290
291     LeaveCriticalSection(&iData->cs);
292
293     TRACE("=> %u\n", wNewID + 1);
294
295     return wNewID + 1;
296 }
297
298 /**************************************************************************
299  *                              timeSetEvent            [WINMM.@]
300  */
301 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
302                              DWORD dwUser, UINT wFlags)
303 {
304     if (wFlags & WINE_TIMER_IS32)
305         WARN("Unknown windows flag... wine internally used.. ooch\n");
306
307     return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc, 
308                                 dwUser, wFlags|WINE_TIMER_IS32);
309 }
310
311 /**************************************************************************
312  *                              timeSetEvent            [MMSYSTEM.602]
313  */
314 MMRESULT16 WINAPI timeSetEvent16(UINT16 wDelay, UINT16 wResol, LPTIMECALLBACK16 lpFunc, 
315                                  DWORD dwUser, UINT16 wFlags)
316 {
317     if (wFlags & WINE_TIMER_IS32)
318         WARN("Unknown windows flag... wine internally used.. ooch\n");
319
320     return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc, 
321                                 dwUser, wFlags & ~WINE_TIMER_IS32);
322 }
323
324 /**************************************************************************
325  *                              timeKillEvent           [WINMM.@]
326  */
327 MMRESULT WINAPI timeKillEvent(UINT wID)
328 {
329     LPWINE_TIMERENTRY*  lpTimer;
330     LPWINE_MM_IDATA     iData = MULTIMEDIA_GetIData();
331     MMRESULT            ret = MMSYSERR_INVALPARAM;
332
333     TRACE("(%u)\n", wID);
334     EnterCriticalSection(&iData->cs);
335     /* remove WINE_TIMERENTRY from list */
336     for (lpTimer = &iData->lpTimerList; *lpTimer; lpTimer = &(*lpTimer)->lpNext) {
337         if (wID == (*lpTimer)->wTimerID) {
338             break;
339         }
340     }
341     LeaveCriticalSection(&iData->cs);
342     
343     if (*lpTimer) {
344         LPWINE_TIMERENTRY       lpTemp = *lpTimer;
345
346         /* unlink timer of id 'wID' */
347         *lpTimer = (*lpTimer)->lpNext;
348         HeapFree(GetProcessHeap(), 0, lpTemp);
349         ret = TIMERR_NOERROR;
350     } else {
351         WARN("wID=%u is not a valid timer ID\n", wID);
352     }
353
354     return ret;
355 }
356
357 /**************************************************************************
358  *                              timeKillEvent           [MMSYSTEM.603]
359  */
360 MMRESULT16 WINAPI timeKillEvent16(UINT16 wID)
361 {
362     return timeKillEvent(wID);
363 }
364
365 /**************************************************************************
366  *                              timeGetDevCaps          [WINMM.@]
367  */
368 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
369 {
370     TRACE("(%p, %u) !\n", lpCaps, wSize);
371
372     lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
373     lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
374     return 0;
375 }
376
377 /**************************************************************************
378  *                              timeGetDevCaps          [MMSYSTEM.604]
379  */
380 MMRESULT16 WINAPI timeGetDevCaps16(LPTIMECAPS16 lpCaps, UINT16 wSize)
381 {
382     TRACE("(%p, %u) !\n", lpCaps, wSize);
383
384     lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
385     lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
386     return 0;
387 }
388
389 /**************************************************************************
390  *                              timeBeginPeriod         [WINMM.@]
391  */
392 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
393 {
394     TRACE("(%u) !\n", wPeriod);
395
396     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
397         return TIMERR_NOCANDO;
398     return 0;
399 }
400
401 /**************************************************************************
402  *                              timeBeginPeriod [MMSYSTEM.605]
403  */
404 MMRESULT16 WINAPI timeBeginPeriod16(UINT16 wPeriod)
405 {
406     TRACE("(%u) !\n", wPeriod);
407
408     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
409         return TIMERR_NOCANDO;
410     return 0;
411 }
412
413 /**************************************************************************
414  *                              timeEndPeriod           [WINMM.@]
415  */
416 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
417 {
418     TRACE("(%u) !\n", wPeriod);
419
420     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
421         return TIMERR_NOCANDO;
422     return 0;
423 }
424
425 /**************************************************************************
426  *                              timeEndPeriod           [MMSYSTEM.606]
427  */
428 MMRESULT16 WINAPI timeEndPeriod16(UINT16 wPeriod)
429 {
430     TRACE("(%u) !\n", wPeriod);
431
432     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
433         return TIMERR_NOCANDO;
434     return 0;
435 }
436
437 /**************************************************************************
438  *                              timeGetTime    [MMSYSTEM.607]
439  *                              timeGetTime    [WINMM.@]
440  */
441 DWORD WINAPI timeGetTime(void)
442 {
443     /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
444      * that lets mciavi.drv run correctly
445      */
446     DWORD count;
447     ReleaseThunkLock(&count);
448     RestoreThunkLock(count);
449     return TIME_MMTimeStart()->mmSysTimeMS;
450 }