Move VerLanguageName[AW] back to base (exported from KERNEL32 ...).
[wine/hacks.git] / multimedia / time.c
blob4502cf16329435957d9dcb394ffb7801959a14bd
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * MMSYTEM time functions
6 * Copyright 1993 Martin Ayotte
7 */
9 #include <time.h>
10 #include <sys/time.h>
11 #include "winbase.h"
12 #include "callback.h"
13 #include "winemm.h"
14 #include "services.h"
15 #include "syslevel.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(mmtime)
21 * FIXME
22 * We're using "1" as the mininum resolution to the timer,
23 * as Windows 95 does, according to the docs. Maybe it should
24 * depend on the computers resources!
26 #define MMSYSTIME_MININTERVAL /* (1) */ (10)
27 #define MMSYSTIME_MAXINTERVAL (65535)
29 static void TIME_TriggerCallBack(LPWINE_TIMERENTRY lpTimer, DWORD dwCurrent)
31 TRACE("before CallBack (%lu) => lpFunc=%p wTimerID=%04X dwUser=%08lX !\n",
32 dwCurrent, lpTimer->lpFunc, lpTimer->wTimerID, lpTimer->dwUser);
34 /* - TimeProc callback that is called here is something strange, under Windows 3.1x it is called
35 * during interrupt time, is allowed to execute very limited number of API calls (like
36 * PostMessage), and must reside in DLL (therefore uses stack of active application). So I
37 * guess current implementation via SetTimer has to be improved upon.
39 switch (lpTimer->wFlags & 0x30) {
40 case TIME_CALLBACK_FUNCTION:
41 if (lpTimer->wFlags & WINE_TIMER_IS32)
42 ((LPTIMECALLBACK)lpTimer->lpFunc)(lpTimer->wTimerID, 0, lpTimer->dwUser, 0, 0);
43 else
44 Callbacks->CallTimeFuncProc(lpTimer->lpFunc,
45 lpTimer->wTimerID, 0,
46 lpTimer->dwUser, 0, 0);
47 break;
48 case TIME_CALLBACK_EVENT_SET:
49 SetEvent((HANDLE)lpTimer->lpFunc);
50 break;
51 case TIME_CALLBACK_EVENT_PULSE:
52 PulseEvent((HANDLE)lpTimer->lpFunc);
53 break;
54 default:
55 FIXME("Unknown callback type 0x%04x for mmtime callback (%p), ignored.\n", lpTimer->wFlags, lpTimer->lpFunc);
56 break;
58 TRACE("after CallBack !\n");
61 /**************************************************************************
62 * TIME_MMSysTimeCallback
64 static void CALLBACK TIME_MMSysTimeCallback(ULONG_PTR ptr_)
66 LPWINE_TIMERENTRY lpTimer, lpNextTimer;
67 LPWINE_MM_IDATA iData = (LPWINE_MM_IDATA)ptr_;
68 int idx;
70 iData->mmSysTimeMS += MMSYSTIME_MININTERVAL;
72 /* since timeSetEvent() and timeKillEvent() can be called
73 * from 16 bit code, there are cases where win16 lock is
74 * locked upon entering timeSetEvent(), and then the mm timer
75 * critical section is locked. This function cannot call the
76 * timer callback with the crit sect locked (because callback
77 * may need to acquire Win16 lock, thus providing a deadlock
78 * situation).
79 * To cope with that, we just copy the WINE_TIMERENTRY struct
80 * that need to trigger the callback, and call it without the
81 * mm timer crit sect locked. The bad side of this
82 * implementation is that, in some cases, the callback may be
83 * invoked *after* a timer has been destroyed...
84 * EPP 99/07/13
86 idx = 0;
88 EnterCriticalSection(&iData->cs);
89 for (lpTimer = iData->lpTimerList; lpTimer != NULL; ) {
90 lpNextTimer = lpTimer->lpNext;
91 if (lpTimer->uCurTime < MMSYSTIME_MININTERVAL) {
92 /* since lpTimer->wDelay is >= MININTERVAL, wCurTime value
93 * shall be correct (>= 0)
95 lpTimer->uCurTime += lpTimer->wDelay - MMSYSTIME_MININTERVAL;
96 if (lpTimer->lpFunc) {
97 if (idx == iData->nSizeLpTimers) {
98 iData->lpTimers = (LPWINE_TIMERENTRY)
99 HeapReAlloc(GetProcessHeap(), 0,
100 iData->lpTimers,
101 ++iData->nSizeLpTimers * sizeof(WINE_TIMERENTRY));
103 iData->lpTimers[idx++] = *lpTimer;
105 /* TIME_ONESHOT is defined as 0 */
106 if (!(lpTimer->wFlags & TIME_PERIODIC))
107 timeKillEvent(lpTimer->wTimerID);
108 } else {
109 lpTimer->uCurTime -= MMSYSTIME_MININTERVAL;
111 lpTimer = lpNextTimer;
113 LeaveCriticalSection(&iData->cs);
115 while (idx > 0) {
116 TIME_TriggerCallBack(&iData->lpTimers[--idx], iData->mmSysTimeMS);
120 /**************************************************************************
121 * MULTIMEDIA_MMTimeStart [internal]
123 static LPWINE_MM_IDATA MULTIMEDIA_MMTimeStart(void)
125 LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
127 if (!iData || IsBadWritePtr(iData, sizeof(WINE_MM_IDATA))) {
128 ERR("iData is not correctly set, please report. Expect failure.\n");
129 return 0;
131 /* FIXME: the service timer is never killed, even when process is
132 * detached from this DLL
134 /* one could think it's possible to stop the service thread activity when no more
135 * mm timers are active, but this would require to keep mmSysTimeMS up-to-date
136 * without being incremented within the service thread callback.
138 if (!iData->hMMTimer) {
139 iData->mmSysTimeMS = GetTickCount();
140 iData->lpTimerList = NULL;
141 iData->hMMTimer = SERVICE_AddTimer(MMSYSTIME_MININTERVAL*1000L, TIME_MMSysTimeCallback, (DWORD)iData);
144 return iData;
147 /**************************************************************************
148 * timeGetSystemTime [WINMM.140]
150 MMRESULT WINAPI timeGetSystemTime(LPMMTIME lpTime, UINT wSize)
152 LPWINE_MM_IDATA iData;
154 TRACE("(%p, %u);\n", lpTime, wSize);
156 if (wSize >= sizeof(*lpTime)) {
157 iData = MULTIMEDIA_MMTimeStart();
158 if (!iData)
159 return MMSYSERR_NOMEM;
160 lpTime->wType = TIME_MS;
161 lpTime->u.ms = iData->mmSysTimeMS;
163 TRACE("=> %lu\n", iData->mmSysTimeMS);
166 return 0;
169 /**************************************************************************
170 * timeGetSystemTime [MMSYSTEM.601]
172 MMRESULT16 WINAPI timeGetSystemTime16(LPMMTIME16 lpTime, UINT16 wSize)
174 LPWINE_MM_IDATA iData;
176 TRACE("(%p, %u);\n", lpTime, wSize);
178 if (wSize >= sizeof(*lpTime)) {
179 iData = MULTIMEDIA_MMTimeStart();
180 if (!iData)
181 return MMSYSERR_NOMEM;
182 lpTime->wType = TIME_MS;
183 lpTime->u.ms = iData->mmSysTimeMS;
185 TRACE("=> %lu\n", iData->mmSysTimeMS);
188 return 0;
191 /**************************************************************************
192 * timeSetEventInternal [internal]
194 static WORD timeSetEventInternal(UINT wDelay, UINT wResol,
195 FARPROC16 lpFunc, DWORD dwUser, UINT wFlags)
197 WORD wNewID = 0;
198 LPWINE_TIMERENTRY lpNewTimer;
199 LPWINE_TIMERENTRY lpTimer;
200 LPWINE_MM_IDATA iData;
202 TRACE("(%u, %u, %p, %08lX, %04X);\n", wDelay, wResol, lpFunc, dwUser, wFlags);
204 lpNewTimer = (LPWINE_TIMERENTRY)HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_TIMERENTRY));
205 if (lpNewTimer == NULL)
206 return 0;
208 if (wDelay < MMSYSTIME_MININTERVAL || wDelay > MMSYSTIME_MAXINTERVAL)
209 return 0;
211 iData = MULTIMEDIA_MMTimeStart();
213 if (!iData)
214 return 0;
216 lpNewTimer->uCurTime = wDelay;
217 lpNewTimer->wDelay = wDelay;
218 lpNewTimer->wResol = wResol;
219 lpNewTimer->lpFunc = lpFunc;
220 lpNewTimer->dwUser = dwUser;
221 lpNewTimer->wFlags = wFlags;
223 EnterCriticalSection(&iData->cs);
225 for (lpTimer = iData->lpTimerList; lpTimer != NULL; lpTimer = lpTimer->lpNext) {
226 wNewID = MAX(wNewID, lpTimer->wTimerID);
229 lpNewTimer->lpNext = iData->lpTimerList;
230 iData->lpTimerList = lpNewTimer;
231 lpNewTimer->wTimerID = wNewID + 1;
233 LeaveCriticalSection(&iData->cs);
235 TRACE("=> %u\n", wNewID + 1);
237 return wNewID + 1;
240 /**************************************************************************
241 * timeSetEvent [MMSYSTEM.602]
243 MMRESULT WINAPI timeSetEvent(UINT wDelay, UINT wResol, LPTIMECALLBACK lpFunc,
244 DWORD dwUser, UINT wFlags)
246 if (wFlags & WINE_TIMER_IS32)
247 WARN("Unknown windows flag... wine internally used.. ooch\n");
249 return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
250 dwUser, wFlags|WINE_TIMER_IS32);
253 /**************************************************************************
254 * timeSetEvent [MMSYSTEM.602]
256 MMRESULT16 WINAPI timeSetEvent16(UINT16 wDelay, UINT16 wResol, LPTIMECALLBACK16 lpFunc,
257 DWORD dwUser, UINT16 wFlags)
259 if (wFlags & WINE_TIMER_IS32)
260 WARN("Unknown windows flag... wine internally used.. ooch\n");
262 return timeSetEventInternal(wDelay, wResol, (FARPROC16)lpFunc,
263 dwUser, wFlags & ~WINE_TIMER_IS32);
266 /**************************************************************************
267 * timeKillEvent [WINMM.142]
269 MMRESULT WINAPI timeKillEvent(UINT wID)
271 LPWINE_TIMERENTRY* lpTimer;
272 LPWINE_MM_IDATA iData = MULTIMEDIA_GetIData();
273 MMRESULT ret = MMSYSERR_INVALPARAM;
275 TRACE("(%u)\n", wID);
276 EnterCriticalSection(&iData->cs);
277 /* remove WINE_TIMERENTRY from list */
278 for (lpTimer = &iData->lpTimerList; *lpTimer; lpTimer = &(*lpTimer)->lpNext) {
279 if (wID == (*lpTimer)->wTimerID) {
280 break;
283 LeaveCriticalSection(&iData->cs);
285 if (*lpTimer) {
286 LPWINE_TIMERENTRY lpTemp = *lpTimer;
288 /* unlink timer of id 'wID' */
289 *lpTimer = (*lpTimer)->lpNext;
290 HeapFree(GetProcessHeap(), 0, lpTemp);
291 ret = TIMERR_NOERROR;
292 } else {
293 WARN("wID=%u is not a valid timer ID\n", wID);
296 return ret;
299 /**************************************************************************
300 * timeKillEvent [MMSYSTEM.603]
302 MMRESULT16 WINAPI timeKillEvent16(UINT16 wID)
304 return timeKillEvent(wID);
307 /**************************************************************************
308 * timeGetDevCaps [WINMM.139]
310 MMRESULT WINAPI timeGetDevCaps(LPTIMECAPS lpCaps, UINT wSize)
312 TRACE("(%p, %u) !\n", lpCaps, wSize);
314 lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
315 lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
316 return 0;
319 /**************************************************************************
320 * timeGetDevCaps [MMSYSTEM.604]
322 MMRESULT16 WINAPI timeGetDevCaps16(LPTIMECAPS16 lpCaps, UINT16 wSize)
324 TRACE("(%p, %u) !\n", lpCaps, wSize);
326 lpCaps->wPeriodMin = MMSYSTIME_MININTERVAL;
327 lpCaps->wPeriodMax = MMSYSTIME_MAXINTERVAL;
328 return 0;
331 /**************************************************************************
332 * timeBeginPeriod [WINMM.137]
334 MMRESULT WINAPI timeBeginPeriod(UINT wPeriod)
336 TRACE("(%u) !\n", wPeriod);
338 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
339 return TIMERR_NOCANDO;
340 return 0;
343 /**************************************************************************
344 * timeBeginPeriod16 [MMSYSTEM.605]
346 MMRESULT16 WINAPI timeBeginPeriod16(UINT16 wPeriod)
348 TRACE("(%u) !\n", wPeriod);
350 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
351 return TIMERR_NOCANDO;
352 return 0;
355 /**************************************************************************
356 * timeEndPeriod [WINMM.138]
358 MMRESULT WINAPI timeEndPeriod(UINT wPeriod)
360 TRACE("(%u) !\n", wPeriod);
362 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
363 return TIMERR_NOCANDO;
364 return 0;
367 /**************************************************************************
368 * timeEndPeriod16 [MMSYSTEM.606]
370 MMRESULT16 WINAPI timeEndPeriod16(UINT16 wPeriod)
372 TRACE("(%u) !\n", wPeriod);
374 if (wPeriod < MMSYSTIME_MININTERVAL || wPeriod > MMSYSTIME_MAXINTERVAL)
375 return TIMERR_NOCANDO;
376 return 0;
379 /**************************************************************************
380 * timeGetTime [MMSYSTEM.607][WINMM.141]
382 DWORD WINAPI timeGetTime(void)
384 return MULTIMEDIA_MMTimeStart()->mmSysTimeMS;