msvcp90: Make a couple more numpunct_*_use_facet() functions static.
[wine/multimedia.git] / dlls / winmm / winmm.c
blob890a3f02d8ce897371b51f9f86601695c7fd3073
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * WINMM functions
6 * Copyright 1993 Martin Ayotte
7 * 1998-2002 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * Eric POUECH :
26 * 98/9 added Win32 MCI support
27 * 99/4 added midiStream support
28 * 99/9 added support for loadable low level drivers
31 /* TODO
32 * + it seems that some programs check what's installed in
33 * registry against the value returned by drivers. Wine is
34 * currently broken regarding this point.
35 * + check thread-safeness for MMSYSTEM and WINMM entry points
36 * + unicode entry points are badly supported (would require
37 * moving 32 bit drivers as Unicode as they are supposed to be)
38 * + allow joystick and timer external calls as we do for wave,
39 * midi, mixer and aux
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <string.h>
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
48 #include "windef.h"
49 #include "winbase.h"
50 #include "mmsystem.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "winternl.h"
54 #include "winemm.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
60 /* ========================================================================
61 * G L O B A L S E T T I N G S
62 * ========================================================================*/
64 HINSTANCE hWinMM32Instance;
65 HANDLE psLastEvent;
67 static CRITICAL_SECTION_DEBUG critsect_debug =
69 0, 0, &WINMM_cs,
70 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
71 0, 0, { (DWORD_PTR)(__FILE__ ": WINMM_cs") }
73 CRITICAL_SECTION WINMM_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
75 /**************************************************************************
76 * WINMM_CreateIData [internal]
78 static BOOL WINMM_CreateIData(HINSTANCE hInstDLL)
80 hWinMM32Instance = hInstDLL;
81 psLastEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
82 return TRUE;
85 /******************************************************************
86 * WINMM_ErrorToString
88 const char* WINMM_ErrorToString(MMRESULT error)
90 #define ERR_TO_STR(dev) case dev: return #dev
91 switch (error) {
92 ERR_TO_STR(MMSYSERR_NOERROR);
93 ERR_TO_STR(MMSYSERR_ERROR);
94 ERR_TO_STR(MMSYSERR_BADDEVICEID);
95 ERR_TO_STR(MMSYSERR_NOTENABLED);
96 ERR_TO_STR(MMSYSERR_ALLOCATED);
97 ERR_TO_STR(MMSYSERR_INVALHANDLE);
98 ERR_TO_STR(MMSYSERR_NODRIVER);
99 ERR_TO_STR(MMSYSERR_NOMEM);
100 ERR_TO_STR(MMSYSERR_NOTSUPPORTED);
101 ERR_TO_STR(MMSYSERR_BADERRNUM);
102 ERR_TO_STR(MMSYSERR_INVALFLAG);
103 ERR_TO_STR(MMSYSERR_INVALPARAM);
104 ERR_TO_STR(MMSYSERR_HANDLEBUSY);
105 ERR_TO_STR(MMSYSERR_INVALIDALIAS);
106 ERR_TO_STR(MMSYSERR_BADDB);
107 ERR_TO_STR(MMSYSERR_KEYNOTFOUND);
108 ERR_TO_STR(MMSYSERR_READERROR);
109 ERR_TO_STR(MMSYSERR_WRITEERROR);
110 ERR_TO_STR(MMSYSERR_DELETEERROR);
111 ERR_TO_STR(MMSYSERR_VALNOTFOUND);
112 ERR_TO_STR(MMSYSERR_NODRIVERCB);
113 ERR_TO_STR(WAVERR_BADFORMAT);
114 ERR_TO_STR(WAVERR_STILLPLAYING);
115 ERR_TO_STR(WAVERR_UNPREPARED);
116 ERR_TO_STR(WAVERR_SYNC);
117 ERR_TO_STR(MIDIERR_INVALIDSETUP);
118 ERR_TO_STR(MIDIERR_NODEVICE);
119 ERR_TO_STR(MIDIERR_STILLPLAYING);
120 ERR_TO_STR(MIDIERR_UNPREPARED);
122 #undef ERR_TO_STR
123 return wine_dbg_sprintf("Unknown(0x%08x)", error);
126 /**************************************************************************
127 * DllMain (WINMM.init)
129 * WINMM DLL entry point
132 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
134 TRACE("%p 0x%x %p\n", hInstDLL, fdwReason, fImpLoad);
136 switch (fdwReason) {
137 case DLL_PROCESS_ATTACH:
138 DisableThreadLibraryCalls(hInstDLL);
140 if (!WINMM_CreateIData(hInstDLL))
141 return FALSE;
142 break;
143 case DLL_PROCESS_DETACH:
144 if(fImpLoad)
145 break;
147 MCI_SendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0L);
148 MMDRV_Exit();
149 DRIVER_UnloadAll();
150 WINMM_DeleteWaveform();
151 TIME_MMTimeStop();
152 CloseHandle(psLastEvent);
153 DeleteCriticalSection(&WINMM_cs);
154 break;
156 return TRUE;
159 /**************************************************************************
160 * WINMM_CheckCallback [internal]
162 MMRESULT WINMM_CheckCallback(DWORD_PTR dwCallback, DWORD fdwOpen, BOOL mixer)
164 switch (fdwOpen & CALLBACK_TYPEMASK) {
165 case CALLBACK_NULL: /* dwCallback need not be NULL */
166 break;
167 case CALLBACK_WINDOW:
168 if (dwCallback && !IsWindow((HWND)dwCallback))
169 return MMSYSERR_INVALPARAM;
170 break;
172 case CALLBACK_FUNCTION:
173 /* a NULL cb is acceptable since w2k, MMSYSERR_INVALPARAM earlier */
174 if (mixer)
175 return MMSYSERR_INVALFLAG; /* since w2k, MMSYSERR_NOTSUPPORTED earlier */
176 break;
177 case CALLBACK_THREAD:
178 case CALLBACK_EVENT:
179 if (mixer) /* FIXME: mixer supports THREAD+EVENT since w2k */
180 return MMSYSERR_NOTSUPPORTED; /* w9X */
181 break;
182 default:
183 WARN("Unknown callback type %d\n", HIWORD(fdwOpen));
185 return MMSYSERR_NOERROR;
188 /**************************************************************************
189 * auxGetNumDevs [WINMM.@]
191 UINT WINAPI auxGetNumDevs(void)
193 return MMDRV_GetNum(MMDRV_AUX);
196 /**************************************************************************
197 * auxGetDevCapsW [WINMM.@]
199 UINT WINAPI auxGetDevCapsW(UINT_PTR uDeviceID, LPAUXCAPSW lpCaps, UINT uSize)
201 LPWINE_MLD wmld;
203 TRACE("(%04lX, %p, %d) !\n", uDeviceID, lpCaps, uSize);
205 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
207 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
208 return MMSYSERR_BADDEVICEID;
209 return MMDRV_Message(wmld, AUXDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
212 /**************************************************************************
213 * auxGetDevCapsA [WINMM.@]
215 UINT WINAPI auxGetDevCapsA(UINT_PTR uDeviceID, LPAUXCAPSA lpCaps, UINT uSize)
217 AUXCAPSW acW;
218 UINT ret;
220 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
222 ret = auxGetDevCapsW(uDeviceID, &acW, sizeof(acW));
224 if (ret == MMSYSERR_NOERROR) {
225 AUXCAPSA acA;
226 acA.wMid = acW.wMid;
227 acA.wPid = acW.wPid;
228 acA.vDriverVersion = acW.vDriverVersion;
229 WideCharToMultiByte( CP_ACP, 0, acW.szPname, -1, acA.szPname,
230 sizeof(acA.szPname), NULL, NULL );
231 acA.wTechnology = acW.wTechnology;
232 acA.dwSupport = acW.dwSupport;
233 memcpy(lpCaps, &acA, min(uSize, sizeof(acA)));
235 return ret;
238 /**************************************************************************
239 * auxGetVolume [WINMM.@]
241 UINT WINAPI auxGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
243 LPWINE_MLD wmld;
245 TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);
247 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
248 return MMSYSERR_INVALHANDLE;
249 return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
252 /**************************************************************************
253 * auxSetVolume [WINMM.@]
255 UINT WINAPI auxSetVolume(UINT uDeviceID, DWORD dwVolume)
257 LPWINE_MLD wmld;
259 TRACE("(%04X, %u) !\n", uDeviceID, dwVolume);
261 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
262 return MMSYSERR_INVALHANDLE;
263 return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L);
266 /**************************************************************************
267 * auxOutMessage [WINMM.@]
269 UINT WINAPI auxOutMessage(UINT uDeviceID, UINT uMessage, DWORD_PTR dw1, DWORD_PTR dw2)
271 LPWINE_MLD wmld;
273 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
274 return MMSYSERR_INVALHANDLE;
276 return MMDRV_Message(wmld, uMessage, dw1, dw2);
279 /**************************************************************************
280 * midiOutGetNumDevs [WINMM.@]
282 UINT WINAPI midiOutGetNumDevs(void)
284 return MMDRV_GetNum(MMDRV_MIDIOUT);
287 /**************************************************************************
288 * midiOutGetDevCapsW [WINMM.@]
290 UINT WINAPI midiOutGetDevCapsW(UINT_PTR uDeviceID, LPMIDIOUTCAPSW lpCaps,
291 UINT uSize)
293 LPWINE_MLD wmld;
295 TRACE("(%lu, %p, %u);\n", uDeviceID, lpCaps, uSize);
297 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
299 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL)
300 return MMSYSERR_BADDEVICEID;
302 return MMDRV_Message(wmld, MODM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
305 /**************************************************************************
306 * midiOutGetDevCapsA [WINMM.@]
308 UINT WINAPI midiOutGetDevCapsA(UINT_PTR uDeviceID, LPMIDIOUTCAPSA lpCaps,
309 UINT uSize)
311 MIDIOUTCAPSW mocW;
312 UINT ret;
314 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
316 ret = midiOutGetDevCapsW(uDeviceID, &mocW, sizeof(mocW));
318 if (ret == MMSYSERR_NOERROR) {
319 MIDIOUTCAPSA mocA;
320 mocA.wMid = mocW.wMid;
321 mocA.wPid = mocW.wPid;
322 mocA.vDriverVersion = mocW.vDriverVersion;
323 WideCharToMultiByte( CP_ACP, 0, mocW.szPname, -1, mocA.szPname,
324 sizeof(mocA.szPname), NULL, NULL );
325 mocA.wTechnology = mocW.wTechnology;
326 mocA.wVoices = mocW.wVoices;
327 mocA.wNotes = mocW.wNotes;
328 mocA.wChannelMask = mocW.wChannelMask;
329 mocA.dwSupport = mocW.dwSupport;
330 memcpy(lpCaps, &mocA, min(uSize, sizeof(mocA)));
332 return ret;
335 /**************************************************************************
336 * midiOutGetErrorTextA [WINMM.@]
337 * midiInGetErrorTextA [WINMM.@]
339 UINT WINAPI midiOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
341 UINT ret;
343 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
344 else if (uSize == 0) ret = MMSYSERR_NOERROR;
345 else
347 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
348 if (!xstr) ret = MMSYSERR_NOMEM;
349 else
351 ret = midiOutGetErrorTextW(uError, xstr, uSize);
352 if (ret == MMSYSERR_NOERROR)
353 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
354 HeapFree(GetProcessHeap(), 0, xstr);
357 return ret;
360 /**************************************************************************
361 * midiOutGetErrorTextW [WINMM.@]
362 * midiInGetErrorTextW [WINMM.@]
364 UINT WINAPI midiOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
366 UINT ret = MMSYSERR_BADERRNUM;
368 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
369 else if (uSize == 0) ret = MMSYSERR_NOERROR;
370 else if (
371 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
372 * a warning for the test was always true */
373 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
374 (uError >= MIDIERR_BASE && uError <= MIDIERR_LASTERROR)) {
375 if (LoadStringW(hWinMM32Instance, uError, lpText, uSize) > 0) {
376 ret = MMSYSERR_NOERROR;
379 return ret;
382 /**************************************************************************
383 * MIDI_OutAlloc [internal]
385 static LPWINE_MIDI MIDI_OutAlloc(HMIDIOUT* lphMidiOut, DWORD_PTR* lpdwCallback,
386 DWORD_PTR* lpdwInstance, LPDWORD lpdwFlags,
387 DWORD cIDs, MIDIOPENSTRMID* lpIDs)
389 HANDLE hMidiOut;
390 LPWINE_MIDI lpwm;
391 UINT size;
393 size = sizeof(WINE_MIDI) + (cIDs ? (cIDs-1) : 0) * sizeof(MIDIOPENSTRMID);
395 lpwm = (LPWINE_MIDI)MMDRV_Alloc(size, MMDRV_MIDIOUT, &hMidiOut, lpdwFlags,
396 lpdwCallback, lpdwInstance);
397 *lphMidiOut = hMidiOut;
399 if (lpwm) {
400 lpwm->mod.hMidi = hMidiOut;
401 lpwm->mod.dwCallback = *lpdwCallback;
402 lpwm->mod.dwInstance = *lpdwInstance;
403 lpwm->mod.dnDevNode = 0;
404 lpwm->mod.cIds = cIDs;
405 if (cIDs)
406 memcpy(&(lpwm->mod.rgIds), lpIDs, cIDs * sizeof(MIDIOPENSTRMID));
408 return lpwm;
411 /**************************************************************************
412 * midiOutOpen [WINMM.@]
414 UINT WINAPI midiOutOpen(LPHMIDIOUT lphMidiOut, UINT uDeviceID,
415 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
417 HMIDIOUT hMidiOut;
418 LPWINE_MIDI lpwm;
419 UINT dwRet;
421 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
422 lphMidiOut, uDeviceID, dwCallback, dwInstance, dwFlags);
424 if (lphMidiOut != NULL) *lphMidiOut = 0;
426 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
427 if (dwRet != MMSYSERR_NOERROR)
428 return dwRet;
430 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &dwFlags, 0, NULL);
432 if (lpwm == NULL)
433 return MMSYSERR_NOMEM;
435 lpwm->mld.uDeviceID = uDeviceID;
437 dwRet = MMDRV_Open((LPWINE_MLD)lpwm, MODM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
439 if (dwRet != MMSYSERR_NOERROR) {
440 MMDRV_Free(hMidiOut, (LPWINE_MLD)lpwm);
441 hMidiOut = 0;
444 if (lphMidiOut) *lphMidiOut = hMidiOut;
445 TRACE("=> %d hMidi=%p\n", dwRet, hMidiOut);
447 return dwRet;
450 /**************************************************************************
451 * midiOutClose [WINMM.@]
453 UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
455 LPWINE_MLD wmld;
456 DWORD dwRet;
458 TRACE("(%p)\n", hMidiOut);
460 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
461 return MMSYSERR_INVALHANDLE;
463 dwRet = MMDRV_Close(wmld, MODM_CLOSE);
464 MMDRV_Free(hMidiOut, wmld);
466 return dwRet;
469 /**************************************************************************
470 * midiOutPrepareHeader [WINMM.@]
472 UINT WINAPI midiOutPrepareHeader(HMIDIOUT hMidiOut,
473 MIDIHDR* lpMidiOutHdr, UINT uSize)
475 LPWINE_MLD wmld;
477 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
479 if (lpMidiOutHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
480 return MMSYSERR_INVALPARAM;
482 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
483 return MMSYSERR_INVALHANDLE;
484 /* FIXME: detect MIDIStream handles and enforce 64KB buffer limit on those */
486 return MMDRV_Message(wmld, MODM_PREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
489 /**************************************************************************
490 * midiOutUnprepareHeader [WINMM.@]
492 UINT WINAPI midiOutUnprepareHeader(HMIDIOUT hMidiOut,
493 MIDIHDR* lpMidiOutHdr, UINT uSize)
495 LPWINE_MLD wmld;
497 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
499 if (lpMidiOutHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
500 return MMSYSERR_INVALPARAM;
502 if (!(lpMidiOutHdr->dwFlags & MHDR_PREPARED)) {
503 return MMSYSERR_NOERROR;
506 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
507 return MMSYSERR_INVALHANDLE;
509 return MMDRV_Message(wmld, MODM_UNPREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
512 /**************************************************************************
513 * midiOutShortMsg [WINMM.@]
515 UINT WINAPI midiOutShortMsg(HMIDIOUT hMidiOut, DWORD dwMsg)
517 LPWINE_MLD wmld;
519 TRACE("(%p, %08X)\n", hMidiOut, dwMsg);
521 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
522 return MMSYSERR_INVALHANDLE;
524 return MMDRV_Message(wmld, MODM_DATA, dwMsg, 0L);
527 /**************************************************************************
528 * midiOutLongMsg [WINMM.@]
530 UINT WINAPI midiOutLongMsg(HMIDIOUT hMidiOut,
531 MIDIHDR* lpMidiOutHdr, UINT uSize)
533 LPWINE_MLD wmld;
535 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
537 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
538 return MMSYSERR_INVALHANDLE;
540 return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD_PTR)lpMidiOutHdr, uSize);
543 /**************************************************************************
544 * midiOutReset [WINMM.@]
546 UINT WINAPI midiOutReset(HMIDIOUT hMidiOut)
548 LPWINE_MLD wmld;
550 TRACE("(%p)\n", hMidiOut);
552 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
553 return MMSYSERR_INVALHANDLE;
555 return MMDRV_Message(wmld, MODM_RESET, 0L, 0L);
558 /**************************************************************************
559 * midiOutGetVolume [WINMM.@]
561 UINT WINAPI midiOutGetVolume(HMIDIOUT hMidiOut, DWORD* lpdwVolume)
563 LPWINE_MLD wmld;
565 TRACE("(%p, %p);\n", hMidiOut, lpdwVolume);
567 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
568 return MMSYSERR_INVALHANDLE;
570 return MMDRV_Message(wmld, MODM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
573 /**************************************************************************
574 * midiOutSetVolume [WINMM.@]
576 UINT WINAPI midiOutSetVolume(HMIDIOUT hMidiOut, DWORD dwVolume)
578 LPWINE_MLD wmld;
580 TRACE("(%p, %d);\n", hMidiOut, dwVolume);
582 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
583 return MMSYSERR_INVALHANDLE;
585 return MMDRV_Message(wmld, MODM_SETVOLUME, dwVolume, 0L);
588 /**************************************************************************
589 * midiOutCachePatches [WINMM.@]
591 UINT WINAPI midiOutCachePatches(HMIDIOUT hMidiOut, UINT uBank,
592 WORD* lpwPatchArray, UINT uFlags)
594 /* not really necessary to support this */
595 FIXME("not supported yet\n");
596 return MMSYSERR_NOTSUPPORTED;
599 /**************************************************************************
600 * midiOutCacheDrumPatches [WINMM.@]
602 UINT WINAPI midiOutCacheDrumPatches(HMIDIOUT hMidiOut, UINT uPatch,
603 WORD* lpwKeyArray, UINT uFlags)
605 FIXME("not supported yet\n");
606 return MMSYSERR_NOTSUPPORTED;
609 /**************************************************************************
610 * midiOutGetID [WINMM.@]
612 UINT WINAPI midiOutGetID(HMIDIOUT hMidiOut, UINT* lpuDeviceID)
614 LPWINE_MLD wmld;
616 TRACE("(%p, %p)\n", hMidiOut, lpuDeviceID);
618 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
619 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
620 return MMSYSERR_INVALHANDLE;
622 *lpuDeviceID = wmld->uDeviceID;
623 return MMSYSERR_NOERROR;
626 /**************************************************************************
627 * midiOutMessage [WINMM.@]
629 UINT WINAPI midiOutMessage(HMIDIOUT hMidiOut, UINT uMessage,
630 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
632 LPWINE_MLD wmld;
634 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
636 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) {
637 /* HACK... */
638 if (uMessage == 0x0001) {
639 *(LPDWORD)dwParam1 = 1;
640 return 0;
642 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) != NULL) {
643 return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
645 return MMSYSERR_INVALHANDLE;
648 switch (uMessage) {
649 case MODM_OPEN:
650 case MODM_CLOSE:
651 FIXME("can't handle OPEN or CLOSE message!\n");
652 return MMSYSERR_NOTSUPPORTED;
654 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
657 /**************************************************************************
658 * midiInGetNumDevs [WINMM.@]
660 UINT WINAPI midiInGetNumDevs(void)
662 return MMDRV_GetNum(MMDRV_MIDIIN);
665 /**************************************************************************
666 * midiInGetDevCapsW [WINMM.@]
668 UINT WINAPI midiInGetDevCapsW(UINT_PTR uDeviceID, LPMIDIINCAPSW lpCaps, UINT uSize)
670 LPWINE_MLD wmld;
672 TRACE("(%ld, %p, %d);\n", uDeviceID, lpCaps, uSize);
674 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
676 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIIN, TRUE)) == NULL)
677 return MMSYSERR_BADDEVICEID;
679 return MMDRV_Message(wmld, MIDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
682 /**************************************************************************
683 * midiInGetDevCapsA [WINMM.@]
685 UINT WINAPI midiInGetDevCapsA(UINT_PTR uDeviceID, LPMIDIINCAPSA lpCaps, UINT uSize)
687 MIDIINCAPSW micW;
688 UINT ret;
690 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
692 ret = midiInGetDevCapsW(uDeviceID, &micW, sizeof(micW));
694 if (ret == MMSYSERR_NOERROR) {
695 MIDIINCAPSA micA;
696 micA.wMid = micW.wMid;
697 micA.wPid = micW.wPid;
698 micA.vDriverVersion = micW.vDriverVersion;
699 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
700 sizeof(micA.szPname), NULL, NULL );
701 micA.dwSupport = micW.dwSupport;
702 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
704 return ret;
707 /**************************************************************************
708 * midiInOpen [WINMM.@]
710 UINT WINAPI midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID,
711 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
713 HANDLE hMidiIn;
714 LPWINE_MIDI lpwm;
715 DWORD dwRet;
717 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
718 lphMidiIn, uDeviceID, dwCallback, dwInstance, dwFlags);
720 if (lphMidiIn != NULL) *lphMidiIn = 0;
722 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
723 if (dwRet != MMSYSERR_NOERROR)
724 return dwRet;
726 lpwm = (LPWINE_MIDI)MMDRV_Alloc(sizeof(WINE_MIDI), MMDRV_MIDIIN, &hMidiIn,
727 &dwFlags, &dwCallback, &dwInstance);
729 if (lpwm == NULL)
730 return MMSYSERR_NOMEM;
732 lpwm->mod.hMidi = hMidiIn;
733 lpwm->mod.dwCallback = dwCallback;
734 lpwm->mod.dwInstance = dwInstance;
736 lpwm->mld.uDeviceID = uDeviceID;
737 dwRet = MMDRV_Open(&lpwm->mld, MIDM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
739 if (dwRet != MMSYSERR_NOERROR) {
740 MMDRV_Free(hMidiIn, &lpwm->mld);
741 hMidiIn = 0;
743 if (lphMidiIn != NULL) *lphMidiIn = hMidiIn;
744 TRACE("=> %d hMidi=%p\n", dwRet, hMidiIn);
746 return dwRet;
749 /**************************************************************************
750 * midiInClose [WINMM.@]
752 UINT WINAPI midiInClose(HMIDIIN hMidiIn)
754 LPWINE_MLD wmld;
755 DWORD dwRet;
757 TRACE("(%p)\n", hMidiIn);
759 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
760 return MMSYSERR_INVALHANDLE;
762 dwRet = MMDRV_Close(wmld, MIDM_CLOSE);
763 MMDRV_Free(hMidiIn, wmld);
764 return dwRet;
767 /**************************************************************************
768 * midiInPrepareHeader [WINMM.@]
770 UINT WINAPI midiInPrepareHeader(HMIDIIN hMidiIn,
771 MIDIHDR* lpMidiInHdr, UINT uSize)
773 LPWINE_MLD wmld;
775 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
777 if (lpMidiInHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
778 return MMSYSERR_INVALPARAM;
780 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
781 return MMSYSERR_INVALHANDLE;
783 return MMDRV_Message(wmld, MIDM_PREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
786 /**************************************************************************
787 * midiInUnprepareHeader [WINMM.@]
789 UINT WINAPI midiInUnprepareHeader(HMIDIIN hMidiIn,
790 MIDIHDR* lpMidiInHdr, UINT uSize)
792 LPWINE_MLD wmld;
794 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
796 if (lpMidiInHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
797 return MMSYSERR_INVALPARAM;
799 if (!(lpMidiInHdr->dwFlags & MHDR_PREPARED)) {
800 return MMSYSERR_NOERROR;
803 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
804 return MMSYSERR_INVALHANDLE;
806 return MMDRV_Message(wmld, MIDM_UNPREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
809 /**************************************************************************
810 * midiInAddBuffer [WINMM.@]
812 UINT WINAPI midiInAddBuffer(HMIDIIN hMidiIn,
813 MIDIHDR* lpMidiInHdr, UINT uSize)
815 LPWINE_MLD wmld;
817 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
819 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
820 return MMSYSERR_INVALHANDLE;
822 return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD_PTR)lpMidiInHdr, uSize);
825 /**************************************************************************
826 * midiInStart [WINMM.@]
828 UINT WINAPI midiInStart(HMIDIIN hMidiIn)
830 LPWINE_MLD wmld;
832 TRACE("(%p)\n", hMidiIn);
834 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
835 return MMSYSERR_INVALHANDLE;
837 return MMDRV_Message(wmld, MIDM_START, 0L, 0L);
840 /**************************************************************************
841 * midiInStop [WINMM.@]
843 UINT WINAPI midiInStop(HMIDIIN hMidiIn)
845 LPWINE_MLD wmld;
847 TRACE("(%p)\n", hMidiIn);
849 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
850 return MMSYSERR_INVALHANDLE;
852 return MMDRV_Message(wmld, MIDM_STOP, 0L, 0L);
855 /**************************************************************************
856 * midiInReset [WINMM.@]
858 UINT WINAPI midiInReset(HMIDIIN hMidiIn)
860 LPWINE_MLD wmld;
862 TRACE("(%p)\n", hMidiIn);
864 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
865 return MMSYSERR_INVALHANDLE;
867 return MMDRV_Message(wmld, MIDM_RESET, 0L, 0L);
870 /**************************************************************************
871 * midiInGetID [WINMM.@]
873 UINT WINAPI midiInGetID(HMIDIIN hMidiIn, UINT* lpuDeviceID)
875 LPWINE_MLD wmld;
877 TRACE("(%p, %p)\n", hMidiIn, lpuDeviceID);
879 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
881 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, TRUE)) == NULL)
882 return MMSYSERR_INVALHANDLE;
884 *lpuDeviceID = wmld->uDeviceID;
886 return MMSYSERR_NOERROR;
889 /**************************************************************************
890 * midiInMessage [WINMM.@]
892 UINT WINAPI midiInMessage(HMIDIIN hMidiIn, UINT uMessage,
893 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
895 LPWINE_MLD wmld;
897 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiIn, uMessage, dwParam1, dwParam2);
899 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
900 return MMSYSERR_INVALHANDLE;
902 switch (uMessage) {
903 case MIDM_OPEN:
904 case MIDM_CLOSE:
905 FIXME("can't handle OPEN or CLOSE message!\n");
906 return MMSYSERR_NOTSUPPORTED;
908 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
911 /**************************************************************************
912 * midiConnect [WINMM.@]
914 MMRESULT WINAPI midiConnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
916 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
917 return MMSYSERR_ERROR;
920 /**************************************************************************
921 * midiDisconnect [WINMM.@]
923 MMRESULT WINAPI midiDisconnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
925 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
926 return MMSYSERR_ERROR;
929 typedef struct WINE_MIDIStream {
930 HMIDIOUT hDevice;
931 HANDLE hThread;
932 DWORD dwThreadID;
933 DWORD dwTempo;
934 DWORD dwTimeDiv;
935 DWORD dwPositionMS;
936 DWORD dwPulses;
937 DWORD dwStartTicks;
938 WORD wFlags;
939 HANDLE hEvent;
940 LPMIDIHDR lpMidiHdr;
941 } WINE_MIDIStream;
943 #define WINE_MSM_HEADER (WM_USER+0)
944 #define WINE_MSM_STOP (WM_USER+1)
945 #define WINE_MSM_PAUSE (WM_USER+2)
946 #define WINE_MSM_RESUME (WM_USER+3)
948 /**************************************************************************
949 * MMSYSTEM_GetMidiStream [internal]
951 static BOOL MMSYSTEM_GetMidiStream(HMIDISTRM hMidiStrm, WINE_MIDIStream** lpMidiStrm, WINE_MIDI** lplpwm)
953 WINE_MIDI* lpwm = (LPWINE_MIDI)MMDRV_Get(hMidiStrm, MMDRV_MIDIOUT, FALSE);
955 if (lplpwm)
956 *lplpwm = lpwm;
958 if (lpwm == NULL) {
959 return FALSE;
962 *lpMidiStrm = (WINE_MIDIStream*)lpwm->mod.rgIds.dwStreamID;
964 return *lpMidiStrm != NULL;
967 /**************************************************************************
968 * MMSYSTEM_MidiStream_Convert [internal]
970 static DWORD MMSYSTEM_MidiStream_Convert(WINE_MIDIStream* lpMidiStrm, DWORD pulse)
972 DWORD ret = 0;
974 if (lpMidiStrm->dwTimeDiv == 0) {
975 FIXME("Shouldn't happen. lpMidiStrm->dwTimeDiv = 0\n");
976 } else if (lpMidiStrm->dwTimeDiv > 0x8000) { /* SMPTE, unchecked FIXME? */
977 int nf = -(char)HIBYTE(lpMidiStrm->dwTimeDiv); /* number of frames */
978 int nsf = LOBYTE(lpMidiStrm->dwTimeDiv); /* number of sub-frames */
979 ret = (pulse * 1000) / (nf * nsf);
980 } else {
981 ret = (DWORD)((double)pulse * ((double)lpMidiStrm->dwTempo / 1000) /
982 (double)lpMidiStrm->dwTimeDiv);
985 return ret;
988 /**************************************************************************
989 * MMSYSTEM_MidiStream_MessageHandler [internal]
991 static BOOL MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream* lpMidiStrm, LPWINE_MIDI lpwm, LPMSG msg)
993 LPMIDIHDR lpMidiHdr;
994 LPMIDIHDR* lpmh;
995 LPBYTE lpData;
996 BOOL paused = FALSE;
998 for (;;) {
999 switch (msg->message) {
1000 case WM_QUIT:
1001 return FALSE;
1002 case WINE_MSM_STOP:
1003 TRACE("STOP\n");
1004 /* this is not quite what MS doc says... */
1005 midiOutReset(lpMidiStrm->hDevice);
1006 /* empty list of already submitted buffers */
1007 lpMidiHdr = lpMidiStrm->lpMidiHdr;
1008 lpMidiStrm->lpMidiHdr = NULL;
1009 while (lpMidiHdr) {
1010 LPMIDIHDR lphdr = lpMidiHdr;
1011 lpMidiHdr = lpMidiHdr->lpNext;
1012 lphdr->dwFlags |= MHDR_DONE;
1013 lphdr->dwFlags &= ~MHDR_INQUEUE;
1015 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1016 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1017 lpwm->mod.dwInstance, (DWORD_PTR)lphdr, 0);
1019 return TRUE;
1020 case WINE_MSM_RESUME:
1021 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1022 lpMidiStrm->dwStartTicks = GetTickCount() - lpMidiStrm->dwPositionMS;
1023 return TRUE;
1024 case WINE_MSM_PAUSE:
1025 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1026 paused = TRUE;
1027 break;
1028 /* FIXME(EPP): "I don't understand the content of the first MIDIHDR sent
1029 * by native mcimidi, it doesn't look like a correct one".
1030 * this trick allows us to throw it away... but I don't like it.
1031 * It looks like part of the file I'm trying to play and definitively looks
1032 * like raw midi content.
1033 * I'd really like to understand why native mcimidi sends it. Perhaps a bad
1034 * synchronization issue where native mcimidi is still processing raw MIDI
1035 * content before generating MIDIEVENTs ?
1037 * 4c 04 89 3b 00 81 7c 99 3b 43 00 99 23 5e 04 89 L..;..|.;C..#^..
1038 * 3b 00 00 89 23 00 7c 99 3b 45 00 99 28 62 04 89 ;...#.|.;E..(b..
1039 * 3b 00 00 89 28 00 81 7c 99 3b 4e 00 99 23 5e 04 ;...(..|.;N..#^.
1040 * 89 3b 00 00 89 23 00 7c 99 3b 45 00 99 23 78 04 .;...#.|.;E..#x.
1041 * 89 3b 00 00 89 23 00 81 7c 99 3b 48 00 99 23 5e .;...#..|.;H..#^
1042 * 04 89 3b 00 00 89 23 00 7c 99 3b 4e 00 99 28 62 ..;...#.|.;N..(b
1043 * 04 89 3b 00 00 89 28 00 81 7c 99 39 4c 00 99 23 ..;...(..|.9L..#
1044 * 5e 04 89 39 00 00 89 23 00 82 7c 99 3b 4c 00 99 ^..9...#..|.;L..
1045 * 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 00 99 #^..;...#.|.;H..
1046 * 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b 3f 04 (b..;...(..|.;?.
1047 * 89 3b 00 1c 99 23 5e 04 89 23 00 5c 99 3b 45 00 .;...#^..#.\.;E.
1048 * 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 3b 46 .#x..;...#..|.;F
1049 * 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 ..#^..;...#.|.;H
1050 * 00 99 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b ..(b..;...(..|.;
1051 * 46 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b F..#^..;...#.|.;
1052 * 48 00 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 H..#x..;...#..|.
1053 * 3b 4c 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 ;L..#^..;...#.|.
1055 case WINE_MSM_HEADER:
1056 /* sets initial tick count for first MIDIHDR */
1057 if (!lpMidiStrm->dwStartTicks)
1058 lpMidiStrm->dwStartTicks = GetTickCount();
1059 lpMidiHdr = (LPMIDIHDR)msg->lParam;
1060 lpData = (LPBYTE)lpMidiHdr->lpData;
1061 TRACE("Adding %s lpMidiHdr=%p [lpData=0x%p dwBytesRecorded=%u/%u dwFlags=0x%08x size=%lu]\n",
1062 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular", lpMidiHdr,
1063 lpData, lpMidiHdr->dwBytesRecorded, lpMidiHdr->dwBufferLength,
1064 lpMidiHdr->dwFlags, msg->wParam);
1065 #if 0
1066 /* dumps content of lpMidiHdr->lpData
1067 * FIXME: there should be a debug routine somewhere that already does this
1069 for (dwToGo = 0; dwToGo < lpMidiHdr->dwBufferLength; dwToGo += 16) {
1070 DWORD i;
1071 BYTE ch;
1073 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++)
1074 printf("%02x ", lpData[dwToGo + i]);
1075 for (; i < 16; i++)
1076 printf(" ");
1077 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++) {
1078 ch = lpData[dwToGo + i];
1079 printf("%c", (ch >= 0x20 && ch <= 0x7F) ? ch : '.');
1081 printf("\n");
1083 #endif
1084 if (((LPMIDIEVENT)lpData)->dwStreamID != 0 &&
1085 ((LPMIDIEVENT)lpData)->dwStreamID != 0xFFFFFFFF &&
1086 ((LPMIDIEVENT)lpData)->dwStreamID != (DWORD)lpMidiStrm) {
1087 FIXME("Dropping bad %s lpMidiHdr (streamID=%08x)\n",
1088 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular",
1089 ((LPMIDIEVENT)lpData)->dwStreamID);
1090 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1091 lpMidiHdr->dwFlags |= MHDR_DONE;
1093 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1094 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1095 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1096 break;
1099 lpMidiHdr->lpNext = 0;
1100 for (lpmh = &lpMidiStrm->lpMidiHdr; *lpmh; lpmh = &(*lpmh)->lpNext);
1101 *lpmh = lpMidiHdr;
1102 break;
1103 default:
1104 FIXME("Unknown message %d\n", msg->message);
1105 break;
1107 if (!paused)
1108 return TRUE;
1109 GetMessageA(msg, 0, 0, 0);
1113 /**************************************************************************
1114 * MMSYSTEM_MidiStream_Player [internal]
1116 static DWORD CALLBACK MMSYSTEM_MidiStream_Player(LPVOID pmt)
1118 WINE_MIDIStream* lpMidiStrm = pmt;
1119 WINE_MIDI* lpwm;
1120 MSG msg;
1121 DWORD dwToGo;
1122 DWORD dwCurrTC;
1123 LPMIDIHDR lpMidiHdr;
1124 DWORD dwOffset;
1126 TRACE("(%p)!\n", lpMidiStrm);
1128 if (!lpMidiStrm ||
1129 (lpwm = (LPWINE_MIDI)MMDRV_Get(lpMidiStrm->hDevice, MMDRV_MIDIOUT, FALSE)) == NULL)
1130 goto the_end;
1132 /* force thread's queue creation */
1133 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
1135 lpMidiStrm->dwStartTicks = 0;
1136 lpMidiStrm->dwPulses = 0;
1138 lpMidiStrm->lpMidiHdr = 0;
1140 /* midiStreamOpen is waiting for ack */
1141 SetEvent(lpMidiStrm->hEvent);
1143 start_header:
1144 lpMidiHdr = lpMidiStrm->lpMidiHdr;
1145 if (!lpMidiHdr) {
1146 /* for first message, block until one arrives, then process all that are available */
1147 GetMessageA(&msg, 0, 0, 0);
1148 do {
1149 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1150 goto the_end;
1151 } while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE));
1152 goto start_header;
1155 dwOffset = 0;
1156 while (dwOffset + offsetof(MIDIEVENT,dwParms) <= lpMidiHdr->dwBytesRecorded) {
1157 LPMIDIEVENT me = (LPMIDIEVENT)(lpMidiHdr->lpData+dwOffset);
1159 /* do we have to wait ? */
1160 if (me->dwDeltaTime) {
1161 lpMidiStrm->dwPositionMS += MMSYSTEM_MidiStream_Convert(lpMidiStrm, me->dwDeltaTime);
1162 lpMidiStrm->dwPulses += me->dwDeltaTime;
1164 dwToGo = lpMidiStrm->dwStartTicks + lpMidiStrm->dwPositionMS;
1166 TRACE("%d/%d/%d\n", dwToGo, GetTickCount(), me->dwDeltaTime);
1167 while ((dwCurrTC = GetTickCount()) < dwToGo) {
1168 if (MsgWaitForMultipleObjects(0, NULL, FALSE, dwToGo - dwCurrTC, QS_ALLINPUT) == WAIT_OBJECT_0) {
1169 /* got a message, handle it */
1170 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
1171 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1172 goto the_end;
1173 /* is lpMidiHdr still current? */
1174 if (lpMidiHdr != lpMidiStrm->lpMidiHdr) {
1175 goto start_header;
1178 } else {
1179 /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
1180 break;
1184 switch (MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK)) {
1185 case MEVT_COMMENT:
1186 FIXME("NIY: MEVT_COMMENT\n");
1187 /* do nothing, skip bytes */
1188 break;
1189 case MEVT_LONGMSG:
1190 midiOutLongMsg(lpMidiStrm->hDevice, lpMidiStrm->lpMidiHdr, MEVT_EVENTPARM(me->dwEvent));
1191 break;
1192 case MEVT_NOP:
1193 break;
1194 case MEVT_SHORTMSG:
1195 midiOutShortMsg(lpMidiStrm->hDevice, MEVT_EVENTPARM(me->dwEvent));
1196 break;
1197 case MEVT_TEMPO:
1198 lpMidiStrm->dwTempo = MEVT_EVENTPARM(me->dwEvent);
1199 break;
1200 case MEVT_VERSION:
1201 break;
1202 default:
1203 FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK));
1204 break;
1206 if (me->dwEvent & MEVT_F_CALLBACK) {
1207 /* native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
1208 lpMidiHdr->dwOffset = dwOffset;
1209 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1210 (HDRVR)lpMidiStrm->hDevice, MM_MOM_POSITIONCB,
1211 lpwm->mod.dwInstance, (LPARAM)lpMidiHdr, 0L);
1213 dwOffset += offsetof(MIDIEVENT,dwParms);
1214 if (me->dwEvent & MEVT_F_LONG)
1215 dwOffset += (MEVT_EVENTPARM(me->dwEvent) + 3) & ~3;
1217 /* done with this header */
1218 lpMidiStrm->lpMidiHdr = lpMidiHdr->lpNext;
1219 lpMidiHdr->dwFlags |= MHDR_DONE;
1220 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1222 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1223 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1224 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1225 goto start_header;
1227 the_end:
1228 TRACE("End of thread\n");
1229 return 0;
1232 /**************************************************************************
1233 * midiStreamClose [WINMM.@]
1235 MMRESULT WINAPI midiStreamClose(HMIDISTRM hMidiStrm)
1237 WINE_MIDIStream* lpMidiStrm;
1238 MMRESULT ret = 0;
1240 TRACE("(%p)!\n", hMidiStrm);
1242 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1243 return MMSYSERR_INVALHANDLE;
1245 midiStreamStop(hMidiStrm);
1246 PostThreadMessageA(lpMidiStrm->dwThreadID, WM_QUIT, 0, 0);
1247 CloseHandle(lpMidiStrm->hEvent);
1248 if (lpMidiStrm->hThread) {
1249 if (GetCurrentThreadId() != lpMidiStrm->dwThreadID)
1250 WaitForSingleObject(lpMidiStrm->hThread, INFINITE);
1251 else {
1252 FIXME("leak from call within function callback\n");
1253 ret = MMSYSERR_HANDLEBUSY; /* yet don't signal it to app */
1255 CloseHandle(lpMidiStrm->hThread);
1257 if(!ret)
1258 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1260 return midiOutClose((HMIDIOUT)hMidiStrm);
1263 /**************************************************************************
1264 * midiStreamOpen [WINMM.@]
1266 MMRESULT WINAPI midiStreamOpen(HMIDISTRM* lphMidiStrm, LPUINT lpuDeviceID,
1267 DWORD cMidi, DWORD_PTR dwCallback,
1268 DWORD_PTR dwInstance, DWORD fdwOpen)
1270 WINE_MIDIStream* lpMidiStrm;
1271 MMRESULT ret;
1272 MIDIOPENSTRMID mosm;
1273 LPWINE_MIDI lpwm;
1274 HMIDIOUT hMidiOut;
1276 TRACE("(%p, %p, %d, 0x%08lx, 0x%08lx, 0x%08x)!\n",
1277 lphMidiStrm, lpuDeviceID, cMidi, dwCallback, dwInstance, fdwOpen);
1279 if (cMidi != 1 || lphMidiStrm == NULL || lpuDeviceID == NULL)
1280 return MMSYSERR_INVALPARAM;
1282 ret = WINMM_CheckCallback(dwCallback, fdwOpen, FALSE);
1283 if (ret != MMSYSERR_NOERROR)
1284 return ret;
1286 lpMidiStrm = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream));
1287 if (!lpMidiStrm)
1288 return MMSYSERR_NOMEM;
1290 lpMidiStrm->dwTempo = 500000;
1291 lpMidiStrm->dwTimeDiv = 480; /* 480 is 120 quarter notes per minute *//* FIXME ??*/
1292 lpMidiStrm->dwPositionMS = 0;
1294 mosm.dwStreamID = (DWORD)lpMidiStrm;
1295 /* FIXME: the correct value is not allocated yet for MAPPER */
1296 mosm.wDeviceID = *lpuDeviceID;
1297 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &fdwOpen, 1, &mosm);
1298 if (!lpwm) {
1299 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1300 return MMSYSERR_NOMEM;
1302 lpMidiStrm->hDevice = hMidiOut;
1303 *lphMidiStrm = (HMIDISTRM)hMidiOut;
1305 lpwm->mld.uDeviceID = *lpuDeviceID;
1307 ret = MMDRV_Open(&lpwm->mld, MODM_OPEN, (DWORD_PTR)&lpwm->mod, fdwOpen);
1308 if (ret != MMSYSERR_NOERROR) {
1309 MMDRV_Free(hMidiOut, &lpwm->mld);
1310 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1311 return ret;
1314 lpMidiStrm->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1315 lpMidiStrm->wFlags = HIWORD(fdwOpen);
1317 lpMidiStrm->hThread = CreateThread(NULL, 0, MMSYSTEM_MidiStream_Player,
1318 lpMidiStrm, 0, &(lpMidiStrm->dwThreadID));
1320 if (!lpMidiStrm->hThread) {
1321 midiStreamClose((HMIDISTRM)hMidiOut);
1322 return MMSYSERR_NOMEM;
1324 SetThreadPriority(lpMidiStrm->hThread, THREAD_PRIORITY_TIME_CRITICAL);
1326 /* wait for thread to have started, and for its queue to be created */
1327 WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
1329 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1331 TRACE("=> (%u/%d) hMidi=%p ret=%d lpMidiStrm=%p\n",
1332 *lpuDeviceID, lpwm->mld.uDeviceID, *lphMidiStrm, ret, lpMidiStrm);
1333 return ret;
1336 /**************************************************************************
1337 * midiStreamOut [WINMM.@]
1339 MMRESULT WINAPI midiStreamOut(HMIDISTRM hMidiStrm, LPMIDIHDR lpMidiHdr,
1340 UINT cbMidiHdr)
1342 WINE_MIDIStream* lpMidiStrm;
1343 DWORD ret = MMSYSERR_NOERROR;
1345 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMidiHdr, cbMidiHdr);
1347 if (cbMidiHdr < offsetof(MIDIHDR,dwOffset) || !lpMidiHdr || !lpMidiHdr->lpData
1348 || lpMidiHdr->dwBufferLength < lpMidiHdr->dwBytesRecorded
1349 || lpMidiHdr->dwBytesRecorded % 4 /* player expects DWORD padding */)
1350 return MMSYSERR_INVALPARAM;
1351 /* FIXME: Native additionally checks if the MIDIEVENTs in lpData
1352 * exactly fit dwBytesRecorded. */
1354 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1355 return MIDIERR_UNPREPARED;
1357 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1358 return MIDIERR_STILLPLAYING;
1360 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1361 ret = MMSYSERR_INVALHANDLE;
1362 } else {
1363 lpMidiHdr->dwFlags |= MHDR_ISSTRM | MHDR_INQUEUE;
1364 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1365 if (!PostThreadMessageA(lpMidiStrm->dwThreadID,
1366 WINE_MSM_HEADER, cbMidiHdr,
1367 (LPARAM)lpMidiHdr)) {
1368 ERR("bad PostThreadMessageA\n");
1369 ret = MMSYSERR_ERROR;
1372 return ret;
1375 /**************************************************************************
1376 * midiStreamPause [WINMM.@]
1378 MMRESULT WINAPI midiStreamPause(HMIDISTRM hMidiStrm)
1380 WINE_MIDIStream* lpMidiStrm;
1381 DWORD ret = MMSYSERR_NOERROR;
1383 TRACE("(%p)!\n", hMidiStrm);
1385 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1386 return MMSYSERR_INVALHANDLE;
1387 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1388 return ret;
1391 /**************************************************************************
1392 * midiStreamPosition [WINMM.@]
1394 MMRESULT WINAPI midiStreamPosition(HMIDISTRM hMidiStrm, LPMMTIME lpMMT, UINT cbmmt)
1396 WINE_MIDIStream* lpMidiStrm;
1397 DWORD ret = MMSYSERR_NOERROR;
1399 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMMT, cbmmt);
1401 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1402 ret = MMSYSERR_INVALHANDLE;
1403 } else if (lpMMT == NULL || cbmmt != sizeof(MMTIME)) {
1404 ret = MMSYSERR_INVALPARAM;
1405 } else {
1406 switch (lpMMT->wType) {
1407 default:
1408 FIXME("Unsupported time type %x\n", lpMMT->wType);
1409 case TIME_BYTES:
1410 case TIME_SAMPLES:
1411 lpMMT->wType = TIME_MS;
1412 /* fall through to alternative format */
1413 case TIME_MS:
1414 lpMMT->u.ms = lpMidiStrm->dwPositionMS;
1415 TRACE("=> %d ms\n", lpMMT->u.ms);
1416 break;
1417 case TIME_TICKS:
1418 lpMMT->u.ticks = lpMidiStrm->dwPulses;
1419 TRACE("=> %d ticks\n", lpMMT->u.ticks);
1420 break;
1423 return ret;
1426 /**************************************************************************
1427 * midiStreamProperty [WINMM.@]
1429 MMRESULT WINAPI midiStreamProperty(HMIDISTRM hMidiStrm, LPBYTE lpPropData, DWORD dwProperty)
1431 WINE_MIDIStream* lpMidiStrm;
1432 MMRESULT ret = MMSYSERR_NOERROR;
1434 TRACE("(%p, %p, %x)\n", hMidiStrm, lpPropData, dwProperty);
1436 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1437 ret = MMSYSERR_INVALHANDLE;
1438 } else if ((dwProperty & (MIDIPROP_GET|MIDIPROP_SET)) == 0) {
1439 ret = MMSYSERR_INVALPARAM;
1440 } else if (dwProperty & MIDIPROP_TEMPO) {
1441 MIDIPROPTEMPO* mpt = (MIDIPROPTEMPO*)lpPropData;
1443 if (sizeof(MIDIPROPTEMPO) != mpt->cbStruct) {
1444 ret = MMSYSERR_INVALPARAM;
1445 } else if (dwProperty & MIDIPROP_SET) {
1446 lpMidiStrm->dwTempo = mpt->dwTempo;
1447 TRACE("Setting tempo to %d\n", mpt->dwTempo);
1448 } else if (dwProperty & MIDIPROP_GET) {
1449 mpt->dwTempo = lpMidiStrm->dwTempo;
1450 TRACE("Getting tempo <= %d\n", mpt->dwTempo);
1452 } else if (dwProperty & MIDIPROP_TIMEDIV) {
1453 MIDIPROPTIMEDIV* mptd = (MIDIPROPTIMEDIV*)lpPropData;
1455 if (sizeof(MIDIPROPTIMEDIV) != mptd->cbStruct) {
1456 ret = MMSYSERR_INVALPARAM;
1457 } else if (dwProperty & MIDIPROP_SET) {
1458 lpMidiStrm->dwTimeDiv = mptd->dwTimeDiv;
1459 TRACE("Setting time div to %d\n", mptd->dwTimeDiv);
1460 } else if (dwProperty & MIDIPROP_GET) {
1461 mptd->dwTimeDiv = lpMidiStrm->dwTimeDiv;
1462 TRACE("Getting time div <= %d\n", mptd->dwTimeDiv);
1464 } else {
1465 ret = MMSYSERR_INVALPARAM;
1468 return ret;
1471 /**************************************************************************
1472 * midiStreamRestart [WINMM.@]
1474 MMRESULT WINAPI midiStreamRestart(HMIDISTRM hMidiStrm)
1476 WINE_MIDIStream* lpMidiStrm;
1477 MMRESULT ret = MMSYSERR_NOERROR;
1479 TRACE("(%p)!\n", hMidiStrm);
1481 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1482 return MMSYSERR_INVALHANDLE;
1483 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_RESUME, 0, 0);
1484 return ret;
1487 /**************************************************************************
1488 * midiStreamStop [WINMM.@]
1490 MMRESULT WINAPI midiStreamStop(HMIDISTRM hMidiStrm)
1492 WINE_MIDIStream* lpMidiStrm;
1493 MMRESULT ret = MMSYSERR_NOERROR;
1495 TRACE("(%p)!\n", hMidiStrm);
1497 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1498 return MMSYSERR_INVALHANDLE;
1499 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_STOP, 0, 0);
1500 return ret;
1503 struct mm_starter
1505 LPTASKCALLBACK cb;
1506 DWORD client;
1507 HANDLE event;
1510 static DWORD WINAPI mmTaskRun(void* pmt)
1512 struct mm_starter mms;
1514 memcpy(&mms, pmt, sizeof(struct mm_starter));
1515 HeapFree(GetProcessHeap(), 0, pmt);
1516 mms.cb(mms.client);
1517 if (mms.event) SetEvent(mms.event);
1518 return 0;
1521 /******************************************************************
1522 * mmTaskCreate (WINMM.@)
1524 UINT WINAPI mmTaskCreate(LPTASKCALLBACK cb, HANDLE* ph, DWORD_PTR client)
1526 HANDLE hThread;
1527 HANDLE hEvent = 0;
1528 struct mm_starter *mms;
1530 mms = HeapAlloc(GetProcessHeap(), 0, sizeof(struct mm_starter));
1531 if (mms == NULL) return TASKERR_OUTOFMEMORY;
1533 mms->cb = cb;
1534 mms->client = client;
1535 if (ph) hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1536 mms->event = hEvent;
1538 hThread = CreateThread(0, 0, mmTaskRun, mms, 0, NULL);
1539 if (!hThread) {
1540 HeapFree(GetProcessHeap(), 0, mms);
1541 if (hEvent) CloseHandle(hEvent);
1542 return TASKERR_OUTOFMEMORY;
1544 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
1545 if (ph) *ph = hEvent;
1546 CloseHandle(hThread);
1547 return 0;
1550 /******************************************************************
1551 * mmTaskBlock (WINMM.@)
1553 VOID WINAPI mmTaskBlock(DWORD tid)
1555 MSG msg;
1559 GetMessageA(&msg, 0, 0, 0);
1560 if (msg.hwnd) DispatchMessageA(&msg);
1561 } while (msg.message != WM_USER);
1564 /******************************************************************
1565 * mmTaskSignal (WINMM.@)
1567 BOOL WINAPI mmTaskSignal(DWORD tid)
1569 return PostThreadMessageW(tid, WM_USER, 0, 0);
1572 /******************************************************************
1573 * mmTaskYield (WINMM.@)
1575 VOID WINAPI mmTaskYield(VOID) {}
1577 /******************************************************************
1578 * mmGetCurrentTask (WINMM.@)
1580 DWORD WINAPI mmGetCurrentTask(VOID)
1582 return GetCurrentThreadId();