Use service thread for "true" multimedia timers.
[wine] / multimedia / 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 <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include "winbase.h"
13 #include "wine/winbase16.h" /* GetTaskDS */
14 #include "callback.h"
15 #include "mmsystem.h"
16 #include "xmalloc.h"
17 #include "services.h"
18 #include "options.h"
19 #include "debugtools.h"
20
21 DECLARE_DEBUG_CHANNEL(mmsys)
22 DECLARE_DEBUG_CHANNEL(mmtime)
23
24 static MMTIME16 mmSysTimeMS;
25 static MMTIME16 mmSysTimeSMPTE;
26
27 typedef struct tagTIMERENTRY {
28     UINT                        wDelay;
29     UINT                        wResol;
30     FARPROC16                   lpFunc;
31     HINSTANCE                   hInstance;
32     DWORD                       dwUser;
33     UINT                        wFlags;
34     UINT                        wTimerID;
35     UINT                        wCurTime;
36     UINT                        isWin32;
37     struct tagTIMERENTRY*       Next;
38 } TIMERENTRY, *LPTIMERENTRY;
39
40 static LPTIMERENTRY lpTimerList = NULL;
41 static HANDLE hMMTimer;
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) */ (10)
50 #define MMSYSTIME_MAXINTERVAL (65535)
51
52 static  void    TIME_TriggerCallBack(LPTIMERENTRY lpTimer, DWORD dwCurrent)
53 {
54     lpTimer->wCurTime = lpTimer->wDelay;
55     
56     if (lpTimer->lpFunc != (FARPROC16) NULL) {
57         TRACE_(mmtime)("before CallBack16 (%lu)!\n", dwCurrent);
58         TRACE_(mmtime)("lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
59               lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
60         TRACE_(mmtime)("hInstance=%04X !\n", lpTimer->hInstance);
61         
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->isWin32)
71                     lpTimer->lpFunc(lpTimer->wTimerID,0,lpTimer->dwUser,0,0);
72                 else
73                     Callbacks->CallTimeFuncProc(lpTimer->lpFunc,
74                                                 lpTimer->wTimerID,0,
75                                                 lpTimer->dwUser,0,0);
76                 break;
77         case TIME_CALLBACK_EVENT_SET:
78                 SetEvent((HANDLE)lpTimer->lpFunc);
79                 break;
80         case TIME_CALLBACK_EVENT_PULSE:
81                 PulseEvent((HANDLE)lpTimer->lpFunc);
82                 break;
83         default:
84                 FIXME_(mmtime)("Unknown callback type 0x%04x for mmtime callback (%p),ignored.\n",lpTimer->wFlags,lpTimer->lpFunc);
85                 break;
86         }
87         TRACE_(mmtime)("after CallBack16 !\n");
88     }
89     if (lpTimer->wFlags & TIME_ONESHOT)
90         timeKillEvent(lpTimer->wTimerID);
91 }
92
93 /**************************************************************************
94  *           TIME_MMSysTimeCallback
95  */
96 static VOID CALLBACK TIME_MMSysTimeCallback( ULONG_PTR dummy )
97 {
98     LPTIMERENTRY lpTimer;
99     
100     mmSysTimeMS.u.ms += MMSYSTIME_MININTERVAL;
101     mmSysTimeSMPTE.u.smpte.frame++;
102     
103     for (lpTimer = lpTimerList; lpTimer != NULL; lpTimer = lpTimer->Next) {
104         if (lpTimer->wCurTime < MMSYSTIME_MININTERVAL) {
105             TIME_TriggerCallBack(lpTimer, mmSysTimeMS.u.ms);
106         } else {
107             lpTimer->wCurTime -= MMSYSTIME_MININTERVAL;
108         }
109     }
110 }
111
112 /**************************************************************************
113  *                              StartMMTime                     [internal]
114  */
115 static void StartMMTime()
116 {
117     static BOOL         mmTimeStarted = FALSE;
118     
119     if (!mmTimeStarted) {
120         mmTimeStarted = TRUE;
121         mmSysTimeMS.wType = TIME_MS;
122         mmSysTimeMS.u.ms = GetTickCount();
123         mmSysTimeSMPTE.wType = TIME_SMPTE;
124         mmSysTimeSMPTE.u.smpte.hour = 0;
125         mmSysTimeSMPTE.u.smpte.min = 0;
126         mmSysTimeSMPTE.u.smpte.sec = 0;
127         mmSysTimeSMPTE.u.smpte.frame = 0;
128         mmSysTimeSMPTE.u.smpte.fps = 0;
129         mmSysTimeSMPTE.u.smpte.dummy = 0;
130         hMMTimer = SERVICE_AddTimer( MMSYSTIME_MININTERVAL*1000L, TIME_MMSysTimeCallback, 0 );
131     }
132 }
133
134 /**************************************************************************
135  *                              timeGetSystemTime       [WINMM.140]
136  */
137 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
138 {
139     TRACE_(mmsys)("(%p, %u);\n", lpTime, wSize);
140     StartMMTime();
141     lpTime->wType = TIME_MS;
142     lpTime->u.ms = mmSysTimeMS.u.ms;
143     return 0;
144 }
145
146 /**************************************************************************
147  *                              timeGetSystemTime       [MMSYSTEM.601]
148  */
149 MMRESULT16 WINAPI timeGetSystemTime16(LPMMTIME16 lpTime, UINT16 wSize)
150 {
151     TRACE_(mmsys)("(%p, %u);\n", lpTime, wSize);
152     StartMMTime();
153     lpTime->wType = TIME_MS;
154     lpTime->u.ms = mmSysTimeMS.u.ms;
155     return 0;
156 }
157
158 static  WORD    timeSetEventInternal(UINT wDelay,UINT wResol,
159                                      FARPROC16 lpFunc,DWORD dwUser,
160                                      UINT wFlags, UINT16 isWin32)
161 {
162     WORD                wNewID = 0;
163     LPTIMERENTRY        lpNewTimer;
164     LPTIMERENTRY        lpTimer = lpTimerList;
165     
166     TRACE_(mmtime)("(%u, %u, %p, %08lX, %04X);\n",
167           wDelay, wResol, lpFunc, dwUser, wFlags);
168     StartMMTime();
169     lpNewTimer = (LPTIMERENTRY)xmalloc(sizeof(TIMERENTRY));
170     if (lpNewTimer == NULL)
171         return 0;
172     while (lpTimer != NULL) {
173         wNewID = MAX(wNewID, lpTimer->wTimerID);
174         lpTimer = lpTimer->Next;
175     }
176     
177     lpNewTimer->Next = lpTimerList;
178     lpTimerList = lpNewTimer;
179     lpNewTimer->wTimerID = wNewID + 1;
180     lpNewTimer->wCurTime = wDelay;
181     lpNewTimer->wDelay = wDelay;
182     lpNewTimer->wResol = wResol;
183     lpNewTimer->lpFunc = lpFunc;
184     lpNewTimer->isWin32 = isWin32;
185     lpNewTimer->hInstance = GetTaskDS16();
186     TRACE_(mmtime)("hInstance=%04X !\n", lpNewTimer->hInstance);
187     TRACE_(mmtime)("lpFunc=0x%08lx !\n", (DWORD)lpFunc );
188     lpNewTimer->dwUser = dwUser;
189     lpNewTimer->wFlags = wFlags;
190     return lpNewTimer->wTimerID;
191 }
192
193 /**************************************************************************
194  *                              timeSetEvent            [MMSYSTEM.602]
195  */
196 MMRESULT WINAPI timeSetEvent(UINT wDelay,UINT wResol,
197                                  LPTIMECALLBACK lpFunc,DWORD dwUser,
198                                  UINT wFlags)
199 {
200     return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc, 
201                                 dwUser, wFlags, 1);
202 }
203
204 /**************************************************************************
205  *                              timeSetEvent            [MMSYSTEM.602]
206  */
207 MMRESULT16 WINAPI timeSetEvent16(UINT16 wDelay, UINT16 wResol,
208                                  LPTIMECALLBACK16 lpFunc,DWORD dwUser,
209                                  UINT16 wFlags)
210 {
211     return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc, 
212                                 dwUser, wFlags, 0);
213 }
214
215 /**************************************************************************
216  *                              timeKillEvent           [WINMM.142]
217  */
218 MMRESULT WINAPI timeKillEvent(UINT wID)
219 {
220     LPTIMERENTRY*       lpTimer;
221     
222     for (lpTimer = &lpTimerList; *lpTimer; lpTimer = &((*lpTimer)->Next)) {
223         if (wID == (*lpTimer)->wTimerID) {
224             LPTIMERENTRY xlptimer = (*lpTimer)->Next;
225             
226             free(*lpTimer);
227             *lpTimer = xlptimer;
228             return TRUE;
229         }
230     }
231     return 0;
232 }
233
234 /**************************************************************************
235  *                              timeKillEvent           [MMSYSTEM.603]
236  */
237 MMRESULT16 WINAPI timeKillEvent16(UINT16 wID)
238 {
239     return timeKillEvent(wID);
240 }
241
242 /**************************************************************************
243  *                              timeGetDevCaps          [WINMM.139]
244  */
245 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps,UINT wSize)
246 {
247     TRACE_(mmtime)("(%p, %u) !\n", lpCaps, wSize);
248     StartMMTime();
249     lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
250     lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
251     return 0;
252 }
253
254 /**************************************************************************
255  *                              timeGetDevCaps          [MMSYSTEM.604]
256  */
257 MMRESULT16 WINAPI timeGetDevCaps16(LPTIMECAPS16 lpCaps, UINT16 wSize)
258 {
259     TRACE_(mmtime)("(%p, %u) !\n", lpCaps, wSize);
260     StartMMTime();
261     lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
262     lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
263     return 0;
264 }
265
266 /**************************************************************************
267  *                              timeBeginPeriod         [WINMM.137]
268  */
269 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
270 {
271     TRACE_(mmtime)("(%u) !\n", wPeriod);
272     StartMMTime();
273     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
274         return TIMERR_NOCANDO;
275     return 0;
276 }
277 /**************************************************************************
278  *                              timeBeginPeriod         [MMSYSTEM.605]
279  */
280 MMRESULT16 WINAPI timeBeginPeriod16(UINT16 wPeriod)
281 {
282     TRACE_(mmtime)("(%u) !\n", wPeriod);
283     StartMMTime();
284     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
285         return TIMERR_NOCANDO;
286     return 0;
287 }
288
289 /**************************************************************************
290  *                              timeEndPeriod           [WINMM.138]
291  */
292 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
293 {
294     TRACE_(mmtime)("(%u) !\n", wPeriod);
295     StartMMTime();
296     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
297         return TIMERR_NOCANDO;
298     return 0;
299 }
300
301 /**************************************************************************
302  *                              timeEndPeriod           [MMSYSTEM.606]
303  */
304 MMRESULT16 WINAPI timeEndPeriod16(UINT16 wPeriod)
305 {
306     TRACE_(mmtime)("(%u) !\n", wPeriod);
307     if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL) 
308         return TIMERR_NOCANDO;
309     return 0;
310 }
311
312 /**************************************************************************
313  *                              timeGetTime    [MMSYSTEM.607][WINMM.141]
314  */
315 DWORD WINAPI timeGetTime()
316 {
317     StartMMTime();
318     return mmSysTimeMS.u.ms;
319 }