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