1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * MMSYTEM time functions
6 * Copyright 1993 Martin Ayotte
24 #define USE_FAKE_MM_TIMERS
26 static MMTIME16 mmSysTimeMS;
27 static MMTIME16 mmSysTimeSMPTE;
29 typedef struct tagTIMERENTRY {
33 HINSTANCE32 hInstance;
39 struct tagTIMERENTRY* Next;
40 } TIMERENTRY, *LPTIMERENTRY;
42 static LPTIMERENTRY lpTimerList = NULL;
44 #ifdef USE_FAKE_MM_TIMERS
45 static DWORD dwLastCBTick = 0;
46 static BOOL32 bUseFakeTimers = FALSE;
47 static WORD wInCallBackLoop = 0;
52 * We're using "1" as the mininum resolution to the timer,
53 * as Windows 95 does, according to the docs. Maybe it should
54 * depend on the computers resources!
56 #define MMSYSTIME_MININTERVAL (1)
57 #define MMSYSTIME_MAXINTERVAL (65535)
59 static void TIME_TriggerCallBack(LPTIMERENTRY lpTimer, DWORD dwCurrent)
61 lpTimer->wCurTime = lpTimer->wDelay;
63 if (lpTimer->lpFunc != (FARPROC16) NULL) {
64 TRACE(mmtime, "before CallBack16 (%lu)!\n", dwCurrent);
65 TRACE(mmtime, "lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
66 lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
67 TRACE(mmtime, "hInstance=%04X !\n", lpTimer->hInstance);
70 /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called
71 * during interrupt time, is allowed to execute very limited number of API calls (like
72 * PostMessage), and must reside in DLL (therefore uses stack of active application). So I
73 * guess current implementation via SetTimer has to be improved upon.
75 switch (lpTimer->wFlags & 0x30) {
76 case TIME_CALLBACK_FUNCTION:
78 lpTimer->lpFunc(lpTimer->wTimerID,0,lpTimer->dwUser,0,0);
80 Callbacks->CallTimeFuncProc(lpTimer->lpFunc,
84 case TIME_CALLBACK_EVENT_SET:
85 SetEvent((HANDLE32)lpTimer->lpFunc);
87 case TIME_CALLBACK_EVENT_PULSE:
88 PulseEvent((HANDLE32)lpTimer->lpFunc);
91 FIXME(mmtime,"Unknown callback type 0x%04x for mmtime callback (%p),ignored.\n",lpTimer->wFlags,lpTimer->lpFunc);
94 TRACE(mmtime, "after CallBack16 !\n");
97 if (lpTimer->wFlags & TIME_ONESHOT)
98 timeKillEvent32(lpTimer->wTimerID);
101 /**************************************************************************
102 * TIME_MMSysTimeCallback
104 static VOID WINAPI TIME_MMSysTimeCallback( HWND32 hwnd, UINT32 msg,
105 UINT32 id, DWORD dwTime )
107 LPTIMERENTRY lpTimer;
109 mmSysTimeMS.u.ms += MMSYSTIME_MININTERVAL;
110 mmSysTimeSMPTE.u.smpte.frame++;
112 #ifdef USE_FAKE_MM_TIMERS
114 dwLastCBTick = GetTickCount();
116 if (!wInCallBackLoop++)
118 for (lpTimer = lpTimerList; lpTimer != NULL; lpTimer = lpTimer->Next) {
119 if (lpTimer->wCurTime < MMSYSTIME_MININTERVAL) {
120 TIME_TriggerCallBack(lpTimer, dwTime);
122 lpTimer->wCurTime -= MMSYSTIME_MININTERVAL;
125 #ifdef USE_FAKE_MM_TIMERS
130 /**************************************************************************
131 * StartMMTime [internal]
133 static void StartMMTime()
135 static BOOL32 mmTimeStarted = FALSE;
137 if (!mmTimeStarted) {
138 mmTimeStarted = TRUE;
139 mmSysTimeMS.wType = TIME_MS;
140 mmSysTimeMS.u.ms = 0;
141 mmSysTimeSMPTE.wType = TIME_SMPTE;
142 mmSysTimeSMPTE.u.smpte.hour = 0;
143 mmSysTimeSMPTE.u.smpte.min = 0;
144 mmSysTimeSMPTE.u.smpte.sec = 0;
145 mmSysTimeSMPTE.u.smpte.frame = 0;
146 mmSysTimeSMPTE.u.smpte.fps = 0;
147 mmSysTimeSMPTE.u.smpte.dummy = 0;
148 SetTimer32( 0, 0, MMSYSTIME_MININTERVAL, TIME_MMSysTimeCallback );
149 #ifdef USE_FAKE_MM_TIMERS
150 bUseFakeTimers = PROFILE_GetWineIniBool("options", "MMFakeTimers", TRUE);
151 TRACE(mmtime, "FakeTimer=%c\n", bUseFakeTimers ? 'Y' : 'N');
153 dwLastCBTick = GetTickCount();
158 /**************************************************************************
159 * timeGetSystemTime [WINMM.140]
161 MMRESULT32 WINAPI timeGetSystemTime32(LPMMTIME32 lpTime, UINT32 wSize)
163 TRACE(mmsys, "(%p, %u);\n", lpTime, wSize);
165 lpTime->wType = TIME_MS;
166 lpTime->u.ms = mmSysTimeMS.u.ms;
170 /**************************************************************************
171 * timeGetSystemTime [MMSYSTEM.601]
173 MMRESULT16 WINAPI timeGetSystemTime16(LPMMTIME16 lpTime, UINT16 wSize)
175 TRACE(mmsys, "(%p, %u);\n", lpTime, wSize);
177 lpTime->wType = TIME_MS;
178 lpTime->u.ms = mmSysTimeMS.u.ms;
182 static WORD timeSetEventInternal(UINT32 wDelay,UINT32 wResol,
183 FARPROC16 lpFunc,DWORD dwUser,
184 UINT32 wFlags, UINT16 isWin32)
187 LPTIMERENTRY lpNewTimer;
188 LPTIMERENTRY lpTimer = lpTimerList;
190 TRACE(mmtime, "(%u, %u, %p, %08lX, %04X);\n",
191 wDelay, wResol, lpFunc, dwUser, wFlags);
193 lpNewTimer = (LPTIMERENTRY)xmalloc(sizeof(TIMERENTRY));
194 if (lpNewTimer == NULL)
196 while (lpTimer != NULL) {
197 wNewID = MAX(wNewID, lpTimer->wTimerID);
198 lpTimer = lpTimer->Next;
201 lpNewTimer->Next = lpTimerList;
202 lpTimerList = lpNewTimer;
203 lpNewTimer->wTimerID = wNewID + 1;
204 lpNewTimer->wCurTime = wDelay;
205 lpNewTimer->wDelay = wDelay;
206 lpNewTimer->wResol = wResol;
207 lpNewTimer->lpFunc = lpFunc;
208 lpNewTimer->isWin32 = isWin32;
209 lpNewTimer->hInstance = GetTaskDS();
210 TRACE(mmtime, "hInstance=%04X !\n", lpNewTimer->hInstance);
211 TRACE(mmtime, "lpFunc=%p !\n", isWin32 ? lpFunc : PTR_SEG_TO_LIN(lpFunc));
212 lpNewTimer->dwUser = dwUser;
213 lpNewTimer->wFlags = wFlags;
214 return lpNewTimer->wTimerID;
217 /**************************************************************************
218 * timeSetEvent [MMSYSTEM.602]
220 MMRESULT32 WINAPI timeSetEvent32(UINT32 wDelay,UINT32 wResol,
221 LPTIMECALLBACK32 lpFunc,DWORD dwUser,
224 return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
228 /**************************************************************************
229 * timeSetEvent [MMSYSTEM.602]
231 MMRESULT16 WINAPI timeSetEvent16(UINT16 wDelay, UINT16 wResol,
232 LPTIMECALLBACK16 lpFunc,DWORD dwUser,
235 return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
239 /**************************************************************************
240 * timeKillEvent [WINMM.142]
242 MMRESULT32 WINAPI timeKillEvent32(UINT32 wID)
244 LPTIMERENTRY* lpTimer;
246 for (lpTimer = &lpTimerList; *lpTimer; lpTimer = &((*lpTimer)->Next)) {
247 if (wID == (*lpTimer)->wTimerID) {
248 LPTIMERENTRY xlptimer = (*lpTimer)->Next;
258 /**************************************************************************
259 * timeKillEvent [MMSYSTEM.603]
261 MMRESULT16 WINAPI timeKillEvent16(UINT16 wID)
263 return timeKillEvent32(wID);
266 /**************************************************************************
267 * timeGetDevCaps [WINMM.139]
269 MMRESULT32 WINAPI timeGetDevCaps32(LPTIMECAPS32 lpCaps,UINT32 wSize)
271 TRACE(mmtime, "(%p, %u) !\n", lpCaps, wSize);
273 lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
274 lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
278 /**************************************************************************
279 * timeGetDevCaps [MMSYSTEM.604]
281 MMRESULT16 WINAPI timeGetDevCaps16(LPTIMECAPS16 lpCaps, UINT16 wSize)
283 TRACE(mmtime, "(%p, %u) !\n", lpCaps, wSize);
285 lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
286 lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
290 /**************************************************************************
291 * timeBeginPeriod [WINMM.137]
293 MMRESULT32 WINAPI timeBeginPeriod32(UINT32 wPeriod)
295 TRACE(mmtime, "(%u) !\n", wPeriod);
297 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
298 return TIMERR_NOCANDO;
301 /**************************************************************************
302 * timeBeginPeriod [MMSYSTEM.605]
304 MMRESULT16 WINAPI timeBeginPeriod16(UINT16 wPeriod)
306 TRACE(mmtime, "(%u) !\n", wPeriod);
308 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
309 return TIMERR_NOCANDO;
313 /**************************************************************************
314 * timeEndPeriod [WINMM.138]
316 MMRESULT32 WINAPI timeEndPeriod32(UINT32 wPeriod)
318 TRACE(mmtime, "(%u) !\n", wPeriod);
320 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
321 return TIMERR_NOCANDO;
325 /**************************************************************************
326 * timeEndPeriod [MMSYSTEM.606]
328 MMRESULT16 WINAPI timeEndPeriod16(UINT16 wPeriod)
330 TRACE(mmtime, "(%u) !\n", wPeriod);
331 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
332 return TIMERR_NOCANDO;
336 /**************************************************************************
337 * timeGetTime [MMSYSTEM.607][WINMM.141]
339 DWORD WINAPI timeGetTime()
341 DWORD dwNewTick = GetTickCount();
344 #ifdef USE_FAKE_MM_TIMERS
345 if (bUseFakeTimers) {
346 if (wInCallBackLoop++) {
350 if (dwNewTick < dwLastCBTick) {
351 ERR(mmtime, "dwNewTick(%lu) < dwLastCBTick(%lu)\n", dwNewTick, dwLastCBTick);
353 dwDelta = dwNewTick - dwLastCBTick;
354 if (dwDelta > MMSYSTIME_MININTERVAL) {
355 LPTIMERENTRY lpTimer;
357 mmSysTimeMS.u.ms += dwDelta; /* FIXME: faked timer */
358 dwLastCBTick = dwNewTick;
359 for (lpTimer = lpTimerList; lpTimer != NULL; lpTimer = lpTimer->Next) {
360 if (lpTimer->wCurTime < dwDelta) {
361 TIME_TriggerCallBack(lpTimer, dwNewTick);
363 lpTimer->wCurTime -= dwDelta;
366 dwNewTick = mmSysTimeMS.u.ms;