If using the default values, also set dwType to REG_SZ as our default
[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             (lpTimer->lpFunc)(lpTimer->wTimerID, 0, lpTimer->dwUser, 0, 0);
70         else if (pFnCallMMDrvFunc16)
71             pFnCallMMDrvFunc16((DWORD)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                         if (iData->lpTimers) 
130                             iData->lpTimers = (LPWINE_TIMERENTRY)
131                             HeapReAlloc(GetProcessHeap(), 0,
132                                         iData->lpTimers,
133                                         ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
134                         else 
135                             iData->lpTimers = (LPWINE_TIMERENTRY)
136                             HeapAlloc(GetProcessHeap(), 0,
137                                         ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
138                     }
139                     iData->lpTimers[idx++] = *lpTimer;
140                 }
141                 /* TIME_ONESHOT is defined as 0 */
142                 if (!(lpTimer->wFlags & TIME_PERIODIC))
143                     timeKillEvent(lpTimer->wTimerID);
144             } else {
145                 lpTimer->uCurTime -= MMSYSTIME_MININTERVAL;
146             }
147             lpTimer = lpNextTimer;
148         }
149         LeaveCriticalSection(&iData->cs);
150
151         while (idx > 0) {
152             TIME_TriggerCallBack(&iData->lpTimers[--idx]);
153         }
154     }
155 }
156
157 /**************************************************************************
158  *           TIME_MMSysTimeThread
159  */
160 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
161 {
162     LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg;
163     volatile HANDLE *pActive = (volatile HANDLE *)&iData->hMMTimer;
164     DWORD last_time, cur_time;
165
166     usleep(MMSYSTIME_STDINTERVAL * 1000);
167     last_time = GetTickCount();
168     while (*pActive) {
169         TIME_MMSysTimeCallback(iData);
170         cur_time = GetTickCount();
171         while (last_time < cur_time)
172             last_time += MMSYSTIME_STDINTERVAL;
173         usleep((last_time - cur_time) * 1000);
174     }
175     return 0;
176 }
177
178 /**************************************************************************
179  *                              TIME_MMTimeStart
180  */
181 void    TIME_MMTimeStart(void)
182 {
183     /* one could think it's possible to stop the service thread activity when no more
184      * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
185      * without being incremented within the service thread callback.
186      */
187     if (!WINMM_IData->hMMTimer) {
188         WINMM_IData->mmSysTimeMS = GetTickCount();
189         WINMM_IData->lpTimerList = NULL;
190         WINMM_IData->hMMTimer = CreateThread(NULL, 0, TIME_MMSysTimeThread, WINMM_IData, 0, NULL);
191     }
192 }
193
194 /**************************************************************************
195  *                              TIME_MMTimeStop
196  */
197 void    TIME_MMTimeStop(void)
198 {
199     if (WINMM_IData->hMMTimer) {
200         HANDLE hMMTimer = WINMM_IData->hMMTimer;
201         WINMM_IData->hMMTimer = 0;
202         WaitForSingleObject(hMMTimer, INFINITE);
203         CloseHandle(hMMTimer);
204     }
205 }
206
207 /**************************************************************************
208  *                              timeGetSystemTime       [WINMM.@]
209  */
210 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
211 {
212     TRACE("(%p, %u);\n", lpTime, wSize);
213
214     if (wSize >= sizeof(*lpTime)) {
215         TIME_MMTimeStart();
216         lpTime->wType = TIME_MS;
217         lpTime->u.ms = WINMM_IData->mmSysTimeMS;
218
219         TRACE("=> %lu\n", lpTime->u.ms);
220     }
221
222     return 0;
223 }
224
225 /**************************************************************************
226  *                              TIME_SetEventInternal   [internal]
227  */
228 WORD    TIME_SetEventInternal(UINT wDelay, UINT wResol,
229                               LPTIMECALLBACK lpFunc, DWORD dwUser, UINT wFlags)
230 {
231     WORD                wNewID = 0;
232     LPWINE_TIMERENTRY   lpNewTimer;
233     LPWINE_TIMERENTRY   lpTimer;
234
235     TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
236
237     lpNewTimer = (LPWINE_TIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
238     if (lpNewTimer == NULL)
239         return 0;
240
241     if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
242         return 0;
243
244     TIME_MMTimeStart();
245
246     lpNewTimer->uCurTime = wDelay;
247     lpNewTimer->wDelay = wDelay;
248     lpNewTimer->wResol = wResol;
249     lpNewTimer->lpFunc = lpFunc;
250     lpNewTimer->dwUser = dwUser;
251     lpNewTimer->wFlags = wFlags;
252
253     EnterCriticalSection(&WINMM_IData->cs);
254
255     for (lpTimer = WINMM_IData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
256         wNewID = max(wNewID, lpTimer->wTimerID);
257     }
258
259     lpNewTimer->lpNext = WINMM_IData->lpTimerList;
260     WINMM_IData->lpTimerList = lpNewTimer;
261     lpNewTimer->wTimerID = wNewID + 1;
262
263     LeaveCriticalSection(&WINMM_IData->cs);
264
265     TRACE("=> %u\n", wNewID + 1);
266
267     return wNewID + 1;
268 }
269
270 /**************************************************************************
271  *                              timeSetEvent            [WINMM.@]
272  */
273 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
274                              DWORD dwUser, UINT wFlags)
275 {
276     if (wFlags & WINE_TIMER_IS32)
277         WARN("Unknown windows flag... wine internally used.. ooch\n");
278
279     return TIME_SetEventInternal(wDelay, wResol, lpFunc,
280                                  dwUser, wFlags|WINE_TIMER_IS32);
281 }
282
283 /**************************************************************************
284  *                              timeKillEvent           [WINMM.@]
285  */
286 MMRESULT WINAPI timeKillEvent(UINT wID)
287 {
288     LPWINE_TIMERENTRY*  lpTimer;
289     MMRESULT            ret = MMSYSERR_INVALPARAM;
290
291     TRACE("(%u)\n", wID);
292     EnterCriticalSection(&WINMM_IData->cs);
293     /* remove WINE_TIMERENTRY from list */
294     for (lpTimer = &WINMM_IData->lpTimerList; *lpTimer; lpTimer = &(*lpTimer)->lpNext) {
295         if (wID == (*lpTimer)->wTimerID) {
296             break;
297         }
298     }
299     LeaveCriticalSection(&WINMM_IData->cs);
300
301     if (*lpTimer) {
302         LPWINE_TIMERENTRY       lpTemp = *lpTimer;
303
304         /* unlink timer of id 'wID' */
305         *lpTimer = (*lpTimer)->lpNext;
306         HeapFree(GetProcessHeap(), 0, lpTemp);
307         ret = TIMERR_NOERROR;
308     } else {
309         WARN("wID=%u is not a valid timer ID\n", wID);
310     }
311
312     return ret;
313 }
314
315 /**************************************************************************
316  *                              timeGetDevCaps          [WINMM.@]
317  */
318 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
319 {
320     TRACE("(%p, %u) !\n", lpCaps, wSize);
321
322     lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
323     lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
324     return 0;
325 }
326
327 /**************************************************************************
328  *                              timeBeginPeriod         [WINMM.@]
329  */
330 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
331 {
332     TRACE("(%u) !\n", wPeriod);
333
334     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
335         return TIMERR_NOCANDO;
336     return 0;
337 }
338
339 /**************************************************************************
340  *                              timeEndPeriod           [WINMM.@]
341  */
342 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
343 {
344     TRACE("(%u) !\n", wPeriod);
345
346     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
347         return TIMERR_NOCANDO;
348     return 0;
349 }
350
351 /**************************************************************************
352  *                              timeGetTime    [MMSYSTEM.607]
353  *                              timeGetTime    [WINMM.@]
354  */
355 DWORD WINAPI timeGetTime(void)
356 {
357     /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
358      * that lets mciavi.drv run correctly
359      */
360     DWORD count;
361     ReleaseThunkLock(&count);
362     RestoreThunkLock(count);
363     TIME_MMTimeStart();
364     return WINMM_IData->mmSysTimeMS;
365 }