Avoid calling Win32 resource functions on 16-bit modules.
[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 static  void    TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer)
56 {
57     TRACE("before CallBack => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
58           lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
59
60     /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called
61      *          during interrupt time,  is allowed to execute very limited number of API calls (like
62      *          PostMessage), and must reside in DLL (therefore uses stack of active application). So I
63      *       guess current implementation via SetTimer has to be improved upon.
64      */
65     switch (lpTimer->wFlags & 0x30) {
66     case TIME_CALLBACK_FUNCTION:
67         if (lpTimer->wFlags & WINE_TIMER_IS32)
68             ((LPTIMECALLBACK)lpTimer->lpFunc)(lpTimer->wTimerID, 0, lpTimer->dwUser, 0, 0);
69         else if (pFnCallMMDrvFunc16)
70             pFnCallMMDrvFunc16(lpTimer->lpFunc, lpTimer->wTimerID, 0,
71                                lpTimer->dwUser, 0, 0);
72         break;
73     case TIME_CALLBACK_EVENT_SET:
74         SetEvent((HANDLE)lpTimer->lpFunc);
75         break;
76     case TIME_CALLBACK_EVENT_PULSE:
77         PulseEvent((HANDLE)lpTimer->lpFunc);
78         break;
79     default:
80         FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n",
81               lpTimer->wFlags, lpTimer->lpFunc);
82         break;
83     }
84     TRACE("after CallBack !\n");
85 }
86
87 /**************************************************************************
88  *           TIME_MMSysTimeCallback
89  */
90 static void CALLBACK TIME_MMSysTimeCallback(LPWINE_MM_IDATA iData)
91 {
92     LPWINE_TIMERENTRY   lpTimer, lpNextTimer;
93     DWORD               delta = GetTickCount() - iData->mmSysTimeMS;
94     int                 idx;
95
96     TRACE("Time delta: %ld\n", delta);
97
98     while (delta >= MMSYSTIME_MININTERVAL) {
99         delta -= MMSYSTIME_MININTERVAL;
100         iData->mmSysTimeMS += MMSYSTIME_MININTERVAL;
101
102         /* since timeSetEvent() and timeKillEvent() can be called
103          * from 16 bit code, there are cases where win16 lock is
104          * locked upon entering timeSetEvent(), and then the mm timer
105          * critical section is locked. This function cannot call the
106          * timer callback with the crit sect locked (because callback
107          * may need to acquire Win16 lock, thus providing a deadlock
108          * situation).
109          * To cope with that, we just copy the WINE_TIMERENTRY struct
110          * that need to trigger the callback, and call it without the
111          * mm timer crit sect locked. The bad side of this
112          * implementation is that, in some cases, the callback may be
113          * invoked *after* a timer has been destroyed...
114          * EPP 99/07/13
115          */
116         idx = 0;
117
118         EnterCriticalSection(&iData->cs);
119         for (lpTimer = iData->lpTimerList; lpTimer != NULL; ) {
120             lpNextTimer = lpTimer->lpNext;
121             if (lpTimer->uCurTime < MMSYSTIME_MININTERVAL) {
122                 /* since lpTimer->wDelay is >= MININTERVAL, wCurTime value
123                  * shall be correct (>= 0)
124                  */
125                 lpTimer->uCurTime += lpTimer->wDelay - MMSYSTIME_MININTERVAL;
126                 if (lpTimer->lpFunc) {
127                     if (idx == iData->nSizeLpTimers) {
128                         iData->lpTimers = (LPWINE_TIMERENTRY)
129                             HeapReAlloc(GetProcessHeap(), 0,
130                                         iData->lpTimers,
131                                         ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
132                     }
133                     iData->lpTimers[idx++] = *lpTimer;
134                 }
135                 /* TIME_ONESHOT is defined as 0 */
136                 if (!(lpTimer->wFlags & TIME_PERIODIC))
137                     timeKillEvent(lpTimer->wTimerID);
138             } else {
139                 lpTimer->uCurTime -= MMSYSTIME_MININTERVAL;
140             }
141             lpTimer = lpNextTimer;
142         }
143         LeaveCriticalSection(&iData->cs);
144
145         while (idx > 0) {
146             TIME_TriggerCallBack(&iData->lpTimers[--idx]);
147         }
148     }
149 }
150
151 /**************************************************************************
152  *           TIME_MMSysTimeThread
153  */
154 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
155 {
156     LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg;
157     volatile HANDLE *pActive = (volatile HANDLE *)&iData->hMMTimer;
158     DWORD last_time, cur_time;
159
160     usleep(MMSYSTIME_STDINTERVAL * 1000);
161     last_time = GetTickCount();
162     while (*pActive) {
163         TIME_MMSysTimeCallback(iData);
164         cur_time = GetTickCount();
165         while (last_time < cur_time)
166             last_time += MMSYSTIME_STDINTERVAL;
167         usleep((last_time - cur_time) * 1000);
168     }
169     return 0;
170 }
171
172 /**************************************************************************
173  *                              TIME_MMTimeStart
174  */
175 void    TIME_MMTimeStart(void)
176 {
177     /* one could think it's possible to stop the service thread activity when no more
178      * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
179      * without being incremented within the service thread callback.
180      */
181     if (!WINMM_IData->hMMTimer) {
182         WINMM_IData->mmSysTimeMS = GetTickCount();
183         WINMM_IData->lpTimerList = NULL;
184         WINMM_IData->hMMTimer = CreateThread(NULL, 0, TIME_MMSysTimeThread, WINMM_IData, 0, NULL);
185     }
186 }
187
188 /**************************************************************************
189  *                              TIME_MMTimeStop
190  */
191 void    TIME_MMTimeStop(void)
192 {
193     if (WINMM_IData->hMMTimer) {
194         HANDLE hMMTimer = WINMM_IData->hMMTimer;
195         WINMM_IData->hMMTimer = 0;
196         WaitForSingleObject(hMMTimer, INFINITE);
197         CloseHandle(hMMTimer);
198     }
199 }
200
201 /**************************************************************************
202  *                              timeGetSystemTime       [WINMM.@]
203  */
204 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
205 {
206     TRACE("(%p, %u);\n", lpTime, wSize);
207
208     if (wSize >= sizeof(*lpTime)) {
209         TIME_MMTimeStart();
210         lpTime->wType = TIME_MS;
211         lpTime->u.ms = WINMM_IData->mmSysTimeMS;
212
213         TRACE("=> %lu\n", lpTime->u.ms);
214     }
215
216     return 0;
217 }
218
219 /**************************************************************************
220  *                              TIME_SetEventInternal   [internal]
221  */
222 WORD    TIME_SetEventInternal(UINT wDelay, UINT wResol,
223                               FARPROC16 lpFunc, DWORD dwUser, UINT wFlags)
224 {
225     WORD                wNewID = 0;
226     LPWINE_TIMERENTRY   lpNewTimer;
227     LPWINE_TIMERENTRY   lpTimer;
228
229     TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
230
231     lpNewTimer = (LPWINE_TIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
232     if (lpNewTimer == NULL)
233         return 0;
234
235     if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
236         return 0;
237
238     TIME_MMTimeStart();
239
240     lpNewTimer->uCurTime = wDelay;
241     lpNewTimer->wDelay = wDelay;
242     lpNewTimer->wResol = wResol;
243     lpNewTimer->lpFunc = lpFunc;
244     lpNewTimer->dwUser = dwUser;
245     lpNewTimer->wFlags = wFlags;
246
247     EnterCriticalSection(&WINMM_IData->cs);
248
249     for (lpTimer = WINMM_IData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
250         wNewID = max(wNewID, lpTimer->wTimerID);
251     }
252
253     lpNewTimer->lpNext = WINMM_IData->lpTimerList;
254     WINMM_IData->lpTimerList = lpNewTimer;
255     lpNewTimer->wTimerID = wNewID + 1;
256
257     LeaveCriticalSection(&WINMM_IData->cs);
258
259     TRACE("=> %u\n", wNewID + 1);
260
261     return wNewID + 1;
262 }
263
264 /**************************************************************************
265  *                              timeSetEvent            [WINMM.@]
266  */
267 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
268                              DWORD dwUser, UINT wFlags)
269 {
270     if (wFlags & WINE_TIMER_IS32)
271         WARN("Unknown windows flag... wine internally used.. ooch\n");
272
273     return TIME_SetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
274                                  dwUser, wFlags|WINE_TIMER_IS32);
275 }
276
277 /**************************************************************************
278  *                              timeKillEvent           [WINMM.@]
279  */
280 MMRESULT WINAPI timeKillEvent(UINT wID)
281 {
282     LPWINE_TIMERENTRY*  lpTimer;
283     MMRESULT            ret = MMSYSERR_INVALPARAM;
284
285     TRACE("(%u)\n", wID);
286     EnterCriticalSection(&WINMM_IData->cs);
287     /* remove WINE_TIMERENTRY from list */
288     for (lpTimer = &WINMM_IData->lpTimerList; *lpTimer; lpTimer = &(*lpTimer)->lpNext) {
289         if (wID == (*lpTimer)->wTimerID) {
290             break;
291         }
292     }
293     LeaveCriticalSection(&WINMM_IData->cs);
294
295     if (*lpTimer) {
296         LPWINE_TIMERENTRY       lpTemp = *lpTimer;
297
298         /* unlink timer of id 'wID' */
299         *lpTimer = (*lpTimer)->lpNext;
300         HeapFree(GetProcessHeap(), 0, lpTemp);
301         ret = TIMERR_NOERROR;
302     } else {
303         WARN("wID=%u is not a valid timer ID\n", wID);
304     }
305
306     return ret;
307 }
308
309 /**************************************************************************
310  *                              timeGetDevCaps          [WINMM.@]
311  */
312 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
313 {
314     TRACE("(%p, %u) !\n", lpCaps, wSize);
315
316     lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
317     lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
318     return 0;
319 }
320
321 /**************************************************************************
322  *                              timeBeginPeriod         [WINMM.@]
323  */
324 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
325 {
326     TRACE("(%u) !\n", wPeriod);
327
328     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
329         return TIMERR_NOCANDO;
330     return 0;
331 }
332
333 /**************************************************************************
334  *                              timeEndPeriod           [WINMM.@]
335  */
336 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
337 {
338     TRACE("(%u) !\n", wPeriod);
339
340     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
341         return TIMERR_NOCANDO;
342     return 0;
343 }
344
345 /**************************************************************************
346  *                              timeGetTime    [MMSYSTEM.607]
347  *                              timeGetTime    [WINMM.@]
348  */
349 DWORD WINAPI timeGetTime(void)
350 {
351     /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
352      * that lets mciavi.drv run correctly
353      */
354     DWORD count;
355     ReleaseThunkLock(&count);
356     RestoreThunkLock(count);
357     TIME_MMTimeStart();
358     return WINMM_IData->mmSysTimeMS;
359 }