Added support for ResetDC.
[wine.git] / dlls / winmm / time.c
blobca76c7f74ed851445a44b26cf8bff240bccdacab
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * MMSYSTEM time functions
6 * Copyright 1993 Martin Ayotte
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <time.h>
27 #include <sys/time.h>
28 #include <unistd.h>
30 #include "mmsystem.h"
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
36 #include "wine/mmsystem16.h"
37 #include "winemm.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(mmtime);
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!
49 #define MMSYSTIME_MININTERVAL (1)
50 #define MMSYSTIME_MAXINTERVAL (65535)
52 #define MMSYSTIME_STDINTERVAL (10) /* reasonable value? */
54 /* ### start build ### */
55 extern WORD CALLBACK TIME_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
56 /* ### stop build ### */
58 static void TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer)
60 TRACE("before CallBack => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
61 lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
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.
68 switch (lpTimer->wFlags & 0x30) {
69 case TIME_CALLBACK_FUNCTION:
70 if (lpTimer->wFlags & WINE_TIMER_IS32)
71 ((LPTIMECALLBACK)lpTimer->lpFunc)(lpTimer->wTimerID, 0, lpTimer->dwUser, 0, 0);
72 else
73 TIME_CallTo16_word_wwlll(lpTimer->lpFunc, lpTimer->wTimerID, 0,
74 lpTimer->dwUser, 0, 0);
75 break;
76 case TIME_CALLBACK_EVENT_SET:
77 SetEvent((HANDLE)lpTimer->lpFunc);
78 break;
79 case TIME_CALLBACK_EVENT_PULSE:
80 PulseEvent((HANDLE)lpTimer->lpFunc);
81 break;
82 default:
83 FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n",
84 lpTimer->wFlags, lpTimer->lpFunc);
85 break;
87 TRACE("after CallBack !\n");
90 /**************************************************************************
91 * TIME_MMSysTimeCallback
93 static void CALLBACK TIME_MMSysTimeCallback(LPWINE_MM_IDATA iData)
95 LPWINE_TIMERENTRY lpTimer, lpNextTimer;
96 DWORD delta = GetTickCount() - iData->mmSysTimeMS;
97 int idx;
99 TRACE("Time delta: %ld\n", delta);
101 while (delta >= MMSYSTIME_MININTERVAL) {
102 delta -= MMSYSTIME_MININTERVAL;
103 iData->mmSysTimeMS += MMSYSTIME_MININTERVAL;
105 /* since timeSetEvent() and timeKillEvent() can be called
106 * from 16 bit code, there are cases where win16 lock is
107 * locked upon entering timeSetEvent(), and then the mm timer
108 * critical section is locked. This function cannot call the
109 * timer callback with the crit sect locked (because callback
110 * may need to acquire Win16 lock, thus providing a deadlock
111 * situation).
112 * To cope with that, we just copy the WINE_TIMERENTRY struct
113 * that need to trigger the callback, and call it without the
114 * mm timer crit sect locked. The bad side of this
115 * implementation is that, in some cases, the callback may be
116 * invoked *after* a timer has been destroyed...
117 * EPP 99/07/13
119 idx = 0;
121 EnterCriticalSection(&iData->cs);
122 for (lpTimer = iData->lpTimerList; lpTimer != NULL; ) {
123 lpNextTimer = lpTimer->lpNext;
124 if (lpTimer->uCurTime < MMSYSTIME_MININTERVAL) {
125 /* since lpTimer->wDelay is >= MININTERVAL, wCurTime value
126 * shall be correct (>= 0)
128 lpTimer->uCurTime += lpTimer->wDelay - MMSYSTIME_MININTERVAL;
129 if (lpTimer->lpFunc) {
130 if (idx == iData->nSizeLpTimers) {
131 iData->lpTimers = (LPWINE_TIMERENTRY)
132 HeapReAlloc(GetProcessHeap(), 0,
133 iData->lpTimers,
134 ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
136 iData->lpTimers[idx++] = *lpTimer;
138 /* TIME_ONESHOT is defined as 0 */
139 if (!(lpTimer->wFlags & TIME_PERIODIC))
140 timeKillEvent(lpTimer->wTimerID);
141 } else {
142 lpTimer->uCurTime -= MMSYSTIME_MININTERVAL;
144 lpTimer = lpNextTimer;
146 LeaveCriticalSection(&iData->cs);
148 while (idx > 0) {
149 TIME_TriggerCallBack(&iData->lpTimers[--idx]);
154 /**************************************************************************
155 * TIME_MMSysTimeThread
157 static DWORD CALLBACK TIME_MMSysTimeThread(LPVOID arg)
159 LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)arg;
160 volatile HANDLE *pActive = (volatile HANDLE *)&iData->hMMTimer;
161 DWORD last_time, cur_time;
163 usleep(MMSYSTIME_STDINTERVAL * 1000);
164 last_time = GetTickCount();
165 while (*pActive) {
166 TIME_MMSysTimeCallback(iData);
167 cur_time = GetTickCount();
168 while (last_time < cur_time)
169 last_time += MMSYSTIME_STDINTERVAL;
170 usleep((last_time - cur_time) * 1000);
172 return 0;
175 /**************************************************************************
176 * TIME_MMTimeStart
178 LPWINE_MM_IDATA TIME_MMTimeStart(void)
180 LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
182 if (IsBadWritePtr(iData, sizeof(WINE_MM_IDATA))) {
183 ERR("iData is not correctly set, please report. Expect failure.\n");
184 return 0;
186 /* one could think it's possible to stop the service thread activity when no more
187 * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
188 * without being incremented within the service thread callback.
190 if (!iData->hMMTimer) {
191 iData->mmSysTimeMS = GetTickCount();
192 iData->lpTimerList = NULL;
193 iData->hMMTimer = CreateThread(NULL, 0, TIME_MMSysTimeThread, iData, 0, NULL);
196 return iData;
199 /**************************************************************************
200 * TIME_MMTimeStop
202 void TIME_MMTimeStop(void)
204 LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
206 if (IsBadWritePtr(iData, sizeof(WINE_MM_IDATA))) {
207 ERR("iData is not correctly set, please report. Expect failure.\n");
208 return;
210 if (iData->hMMTimer) {
211 HANDLE hMMTimer = iData->hMMTimer;
212 iData->hMMTimer = 0;
213 WaitForSingleObject(hMMTimer, INFINITE);
214 CloseHandle(hMMTimer);
218 /**************************************************************************
219 * timeGetSystemTime [WINMM.@]
221 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
223 TRACE("(%p, %u);\n", lpTime, wSize);
225 if (wSize >= sizeof(*lpTime)) {
226 lpTime->wType = TIME_MS;
227 lpTime->u.ms = TIME_MMTimeStart()->mmSysTimeMS;
229 TRACE("=> %lu\n", lpTime->u.ms);
232 return 0;
235 /**************************************************************************
236 * timeGetSystemTime [MMSYSTEM.601]
238 MMRESULT16 WINAPI timeGetSystemTime16(LPMMTIME16 lpTime, UINT16 wSize)
240 TRACE("(%p, %u);\n", lpTime, wSize);
242 if (wSize >= sizeof(*lpTime)) {
243 lpTime->wType = TIME_MS;
244 lpTime->u.ms = TIME_MMTimeStart()->mmSysTimeMS;
246 TRACE("=> %lu\n", lpTime->u.ms);
249 return 0;
252 /**************************************************************************
253 * timeSetEventInternal [internal]
255 static WORD timeSetEventInternal(UINT wDelay, UINT wResol,
256 FARPROC16 lpFunc, DWORD dwUser, UINT wFlags)
258 WORD wNewID = 0;
259 LPWINE_TIMERENTRY lpNewTimer;
260 LPWINE_TIMERENTRY lpTimer;
261 LPWINE_MM_IDATA iData;
263 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
265 lpNewTimer = (LPWINE_TIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
266 if (lpNewTimer == NULL)
267 return 0;
269 if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
270 return 0;
272 iData = TIME_MMTimeStart();
274 lpNewTimer->uCurTime = wDelay;
275 lpNewTimer->wDelay = wDelay;
276 lpNewTimer->wResol = wResol;
277 lpNewTimer->lpFunc = lpFunc;
278 lpNewTimer->dwUser = dwUser;
279 lpNewTimer->wFlags = wFlags;
281 EnterCriticalSection(&iData->cs);
283 for (lpTimer = iData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
284 wNewID = max(wNewID, lpTimer->wTimerID);
287 lpNewTimer->lpNext = iData->lpTimerList;
288 iData->lpTimerList = lpNewTimer;
289 lpNewTimer->wTimerID = wNewID + 1;
291 LeaveCriticalSection(&iData->cs);
293 TRACE("=> %u\n", wNewID + 1);
295 return wNewID + 1;
298 /**************************************************************************
299 * timeSetEvent [WINMM.@]
301 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
302 DWORD dwUser, UINT wFlags)
304 if (wFlags & WINE_TIMER_IS32)
305 WARN("Unknown windows flag... wine internally used.. ooch\n");
307 return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
308 dwUser, wFlags|WINE_TIMER_IS32);
311 /**************************************************************************
312 * timeSetEvent [MMSYSTEM.602]
314 MMRESULT16 WINAPI timeSetEvent16(UINT16 wDelay, UINT16 wResol, LPTIMECALLBACK16 lpFunc,
315 DWORD dwUser, UINT16 wFlags)
317 if (wFlags & WINE_TIMER_IS32)
318 WARN("Unknown windows flag... wine internally used.. ooch\n");
320 return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
321 dwUser, wFlags & ~WINE_TIMER_IS32);
324 /**************************************************************************
325 * timeKillEvent [WINMM.@]
327 MMRESULT WINAPI timeKillEvent(UINT wID)
329 LPWINE_TIMERENTRY* lpTimer;
330 LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
331 MMRESULT ret = MMSYSERR_INVALPARAM;
333 TRACE("(%u)\n", wID);
334 EnterCriticalSection(&iData->cs);
335 /* remove WINE_TIMERENTRY from list */
336 for (lpTimer = &iData->lpTimerList; *lpTimer; lpTimer = &(*lpTimer)->lpNext) {
337 if (wID == (*lpTimer)->wTimerID) {
338 break;
341 LeaveCriticalSection(&iData->cs);
343 if (*lpTimer) {
344 LPWINE_TIMERENTRY lpTemp = *lpTimer;
346 /* unlink timer of id 'wID' */
347 *lpTimer = (*lpTimer)->lpNext;
348 HeapFree(GetProcessHeap(), 0, lpTemp);
349 ret = TIMERR_NOERROR;
350 } else {
351 WARN("wID=%u is not a valid timer ID\n", wID);
354 return ret;
357 /**************************************************************************
358 * timeKillEvent [MMSYSTEM.603]
360 MMRESULT16 WINAPI timeKillEvent16(UINT16 wID)
362 return timeKillEvent(wID);
365 /**************************************************************************
366 * timeGetDevCaps [WINMM.@]
368 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
370 TRACE("(%p, %u) !\n", lpCaps, wSize);
372 lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
373 lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
374 return 0;
377 /**************************************************************************
378 * timeGetDevCaps [MMSYSTEM.604]
380 MMRESULT16 WINAPI timeGetDevCaps16(LPTIMECAPS16 lpCaps, UINT16 wSize)
382 TRACE("(%p, %u) !\n", lpCaps, wSize);
384 lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
385 lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
386 return 0;
389 /**************************************************************************
390 * timeBeginPeriod [WINMM.@]
392 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
394 TRACE("(%u) !\n", wPeriod);
396 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
397 return TIMERR_NOCANDO;
398 return 0;
401 /**************************************************************************
402 * timeBeginPeriod [MMSYSTEM.605]
404 MMRESULT16 WINAPI timeBeginPeriod16(UINT16 wPeriod)
406 TRACE("(%u) !\n", wPeriod);
408 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
409 return TIMERR_NOCANDO;
410 return 0;
413 /**************************************************************************
414 * timeEndPeriod [WINMM.@]
416 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
418 TRACE("(%u) !\n", wPeriod);
420 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
421 return TIMERR_NOCANDO;
422 return 0;
425 /**************************************************************************
426 * timeEndPeriod [MMSYSTEM.606]
428 MMRESULT16 WINAPI timeEndPeriod16(UINT16 wPeriod)
430 TRACE("(%u) !\n", wPeriod);
432 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
433 return TIMERR_NOCANDO;
434 return 0;
437 /**************************************************************************
438 * timeGetTime [MMSYSTEM.607]
439 * timeGetTime [WINMM.@]
441 DWORD WINAPI timeGetTime(void)
443 /* FIXME: releasing the win16 lock here is a temporary hack (I hope)
444 * that lets mciavi.drv run correctly
446 DWORD count;
447 ReleaseThunkLock(&count);
448 RestoreThunkLock(count);
449 return TIME_MMTimeStart()->mmSysTimeMS;