1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
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
26 * 98/9 added Win32 MCI support
27 * 99/4 added midiStream support
28 * 99/9 added support for loadable low level drivers
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,
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
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;
67 static CRITICAL_SECTION_DEBUG critsect_debug =
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);
85 /**************************************************************************
86 * WINMM_DeleteIData [internal]
88 static void WINMM_DeleteIData(void)
92 WINMM_DeleteWaveform();
93 CloseHandle(psLastEvent);
94 DeleteCriticalSection(&WINMM_cs);
97 /******************************************************************
100 const char* WINMM_ErrorToString(MMRESULT error)
102 #define ERR_TO_STR(dev) case dev: return #dev
104 ERR_TO_STR(MMSYSERR_NOERROR);
105 ERR_TO_STR(MMSYSERR_ERROR);
106 ERR_TO_STR(MMSYSERR_BADDEVICEID);
107 ERR_TO_STR(MMSYSERR_NOTENABLED);
108 ERR_TO_STR(MMSYSERR_ALLOCATED);
109 ERR_TO_STR(MMSYSERR_INVALHANDLE);
110 ERR_TO_STR(MMSYSERR_NODRIVER);
111 ERR_TO_STR(MMSYSERR_NOMEM);
112 ERR_TO_STR(MMSYSERR_NOTSUPPORTED);
113 ERR_TO_STR(MMSYSERR_BADERRNUM);
114 ERR_TO_STR(MMSYSERR_INVALFLAG);
115 ERR_TO_STR(MMSYSERR_INVALPARAM);
116 ERR_TO_STR(MMSYSERR_HANDLEBUSY);
117 ERR_TO_STR(MMSYSERR_INVALIDALIAS);
118 ERR_TO_STR(MMSYSERR_BADDB);
119 ERR_TO_STR(MMSYSERR_KEYNOTFOUND);
120 ERR_TO_STR(MMSYSERR_READERROR);
121 ERR_TO_STR(MMSYSERR_WRITEERROR);
122 ERR_TO_STR(MMSYSERR_DELETEERROR);
123 ERR_TO_STR(MMSYSERR_VALNOTFOUND);
124 ERR_TO_STR(MMSYSERR_NODRIVERCB);
125 ERR_TO_STR(WAVERR_BADFORMAT);
126 ERR_TO_STR(WAVERR_STILLPLAYING);
127 ERR_TO_STR(WAVERR_UNPREPARED);
128 ERR_TO_STR(WAVERR_SYNC);
129 ERR_TO_STR(MIDIERR_INVALIDSETUP);
130 ERR_TO_STR(MIDIERR_NODEVICE);
131 ERR_TO_STR(MIDIERR_STILLPLAYING);
132 ERR_TO_STR(MIDIERR_UNPREPARED);
135 return wine_dbg_sprintf("Unknown(0x%08x)", error);
138 /**************************************************************************
139 * DllMain (WINMM.init)
141 * WINMM DLL entry point
144 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
146 TRACE("%p 0x%x %p\n", hInstDLL, fdwReason, fImpLoad);
149 case DLL_PROCESS_ATTACH:
150 DisableThreadLibraryCalls(hInstDLL);
152 if (!WINMM_CreateIData(hInstDLL))
155 case DLL_PROCESS_DETACH:
156 /* close all opened MCI drivers */
157 MCI_SendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0L);
159 /* There's no guarantee the drivers haven't already been unloaded on
164 /* now unload all remaining drivers... */
174 /**************************************************************************
175 * WINMM_CheckCallback [internal]
177 MMRESULT WINMM_CheckCallback(DWORD_PTR dwCallback, DWORD fdwOpen, BOOL mixer)
179 switch (fdwOpen & CALLBACK_TYPEMASK) {
180 case CALLBACK_NULL: /* dwCallback need not be NULL */
182 case CALLBACK_WINDOW:
183 if (dwCallback && !IsWindow((HWND)dwCallback))
184 return MMSYSERR_INVALPARAM;
187 case CALLBACK_FUNCTION:
188 /* a NULL cb is acceptable since w2k, MMSYSERR_INVALPARAM earlier */
190 return MMSYSERR_INVALFLAG; /* since w2k, MMSYSERR_NOTSUPPORTED earlier */
192 case CALLBACK_THREAD:
194 if (mixer) /* FIXME: mixer supports THREAD+EVENT since w2k */
195 return MMSYSERR_NOTSUPPORTED; /* w9X */
198 WARN("Unknown callback type %d\n", HIWORD(fdwOpen));
200 return MMSYSERR_NOERROR;
203 /**************************************************************************
204 * auxGetNumDevs [WINMM.@]
206 UINT WINAPI auxGetNumDevs(void)
208 return MMDRV_GetNum(MMDRV_AUX);
211 /**************************************************************************
212 * auxGetDevCapsW [WINMM.@]
214 UINT WINAPI auxGetDevCapsW(UINT_PTR uDeviceID, LPAUXCAPSW lpCaps, UINT uSize)
218 TRACE("(%04lX, %p, %d) !\n", uDeviceID, lpCaps, uSize);
220 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
222 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
223 return MMSYSERR_BADDEVICEID;
224 return MMDRV_Message(wmld, AUXDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
227 /**************************************************************************
228 * auxGetDevCapsA [WINMM.@]
230 UINT WINAPI auxGetDevCapsA(UINT_PTR uDeviceID, LPAUXCAPSA lpCaps, UINT uSize)
235 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
237 ret = auxGetDevCapsW(uDeviceID, &acW, sizeof(acW));
239 if (ret == MMSYSERR_NOERROR) {
243 acA.vDriverVersion = acW.vDriverVersion;
244 WideCharToMultiByte( CP_ACP, 0, acW.szPname, -1, acA.szPname,
245 sizeof(acA.szPname), NULL, NULL );
246 acA.wTechnology = acW.wTechnology;
247 acA.dwSupport = acW.dwSupport;
248 memcpy(lpCaps, &acA, min(uSize, sizeof(acA)));
253 /**************************************************************************
254 * auxGetVolume [WINMM.@]
256 UINT WINAPI auxGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
260 TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);
262 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
263 return MMSYSERR_INVALHANDLE;
264 return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
267 /**************************************************************************
268 * auxSetVolume [WINMM.@]
270 UINT WINAPI auxSetVolume(UINT uDeviceID, DWORD dwVolume)
274 TRACE("(%04X, %u) !\n", uDeviceID, dwVolume);
276 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
277 return MMSYSERR_INVALHANDLE;
278 return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L);
281 /**************************************************************************
282 * auxOutMessage [WINMM.@]
284 UINT WINAPI auxOutMessage(UINT uDeviceID, UINT uMessage, DWORD_PTR dw1, DWORD_PTR dw2)
288 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
289 return MMSYSERR_INVALHANDLE;
291 return MMDRV_Message(wmld, uMessage, dw1, dw2);
294 /**************************************************************************
295 * midiOutGetNumDevs [WINMM.@]
297 UINT WINAPI midiOutGetNumDevs(void)
299 return MMDRV_GetNum(MMDRV_MIDIOUT);
302 /**************************************************************************
303 * midiOutGetDevCapsW [WINMM.@]
305 UINT WINAPI midiOutGetDevCapsW(UINT_PTR uDeviceID, LPMIDIOUTCAPSW lpCaps,
310 TRACE("(%lu, %p, %u);\n", uDeviceID, lpCaps, uSize);
312 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
314 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL)
315 return MMSYSERR_BADDEVICEID;
317 return MMDRV_Message(wmld, MODM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
320 /**************************************************************************
321 * midiOutGetDevCapsA [WINMM.@]
323 UINT WINAPI midiOutGetDevCapsA(UINT_PTR uDeviceID, LPMIDIOUTCAPSA lpCaps,
329 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
331 ret = midiOutGetDevCapsW(uDeviceID, &mocW, sizeof(mocW));
333 if (ret == MMSYSERR_NOERROR) {
335 mocA.wMid = mocW.wMid;
336 mocA.wPid = mocW.wPid;
337 mocA.vDriverVersion = mocW.vDriverVersion;
338 WideCharToMultiByte( CP_ACP, 0, mocW.szPname, -1, mocA.szPname,
339 sizeof(mocA.szPname), NULL, NULL );
340 mocA.wTechnology = mocW.wTechnology;
341 mocA.wVoices = mocW.wVoices;
342 mocA.wNotes = mocW.wNotes;
343 mocA.wChannelMask = mocW.wChannelMask;
344 mocA.dwSupport = mocW.dwSupport;
345 memcpy(lpCaps, &mocA, min(uSize, sizeof(mocA)));
350 /**************************************************************************
351 * midiOutGetErrorTextA [WINMM.@]
352 * midiInGetErrorTextA [WINMM.@]
354 UINT WINAPI midiOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
358 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
359 else if (uSize == 0) ret = MMSYSERR_NOERROR;
362 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
363 if (!xstr) ret = MMSYSERR_NOMEM;
366 ret = midiOutGetErrorTextW(uError, xstr, uSize);
367 if (ret == MMSYSERR_NOERROR)
368 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
369 HeapFree(GetProcessHeap(), 0, xstr);
375 /**************************************************************************
376 * midiOutGetErrorTextW [WINMM.@]
377 * midiInGetErrorTextW [WINMM.@]
379 UINT WINAPI midiOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
381 UINT ret = MMSYSERR_BADERRNUM;
383 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
384 else if (uSize == 0) ret = MMSYSERR_NOERROR;
386 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
387 * a warning for the test was always true */
388 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
389 (uError >= MIDIERR_BASE && uError <= MIDIERR_LASTERROR)) {
390 if (LoadStringW(hWinMM32Instance, uError, lpText, uSize) > 0) {
391 ret = MMSYSERR_NOERROR;
397 /**************************************************************************
398 * MIDI_OutAlloc [internal]
400 static LPWINE_MIDI MIDI_OutAlloc(HMIDIOUT* lphMidiOut, DWORD_PTR* lpdwCallback,
401 DWORD_PTR* lpdwInstance, LPDWORD lpdwFlags,
402 DWORD cIDs, MIDIOPENSTRMID* lpIDs)
408 size = sizeof(WINE_MIDI) + (cIDs ? (cIDs-1) : 0) * sizeof(MIDIOPENSTRMID);
410 lpwm = (LPWINE_MIDI)MMDRV_Alloc(size, MMDRV_MIDIOUT, &hMidiOut, lpdwFlags,
411 lpdwCallback, lpdwInstance);
412 *lphMidiOut = hMidiOut;
415 lpwm->mod.hMidi = hMidiOut;
416 lpwm->mod.dwCallback = *lpdwCallback;
417 lpwm->mod.dwInstance = *lpdwInstance;
418 lpwm->mod.dnDevNode = 0;
419 lpwm->mod.cIds = cIDs;
421 memcpy(&(lpwm->mod.rgIds), lpIDs, cIDs * sizeof(MIDIOPENSTRMID));
426 /**************************************************************************
427 * midiOutOpen [WINMM.@]
429 UINT WINAPI midiOutOpen(LPHMIDIOUT lphMidiOut, UINT uDeviceID,
430 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
436 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
437 lphMidiOut, uDeviceID, dwCallback, dwInstance, dwFlags);
439 if (lphMidiOut != NULL) *lphMidiOut = 0;
441 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
442 if (dwRet != MMSYSERR_NOERROR)
445 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &dwFlags, 0, NULL);
448 return MMSYSERR_NOMEM;
450 lpwm->mld.uDeviceID = uDeviceID;
452 dwRet = MMDRV_Open((LPWINE_MLD)lpwm, MODM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
454 if (dwRet != MMSYSERR_NOERROR) {
455 MMDRV_Free(hMidiOut, (LPWINE_MLD)lpwm);
459 if (lphMidiOut) *lphMidiOut = hMidiOut;
460 TRACE("=> %d hMidi=%p\n", dwRet, hMidiOut);
465 /**************************************************************************
466 * midiOutClose [WINMM.@]
468 UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
473 TRACE("(%p)\n", hMidiOut);
475 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
476 return MMSYSERR_INVALHANDLE;
478 dwRet = MMDRV_Close(wmld, MODM_CLOSE);
479 MMDRV_Free(hMidiOut, wmld);
484 /**************************************************************************
485 * midiOutPrepareHeader [WINMM.@]
487 UINT WINAPI midiOutPrepareHeader(HMIDIOUT hMidiOut,
488 MIDIHDR* lpMidiOutHdr, UINT uSize)
492 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
494 if (lpMidiOutHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
495 return MMSYSERR_INVALPARAM;
497 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
498 return MMSYSERR_INVALHANDLE;
499 /* FIXME: detect MIDIStream handles and enforce 64KB buffer limit on those */
501 return MMDRV_Message(wmld, MODM_PREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
504 /**************************************************************************
505 * midiOutUnprepareHeader [WINMM.@]
507 UINT WINAPI midiOutUnprepareHeader(HMIDIOUT hMidiOut,
508 MIDIHDR* lpMidiOutHdr, UINT uSize)
512 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
514 if (lpMidiOutHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
515 return MMSYSERR_INVALPARAM;
517 if (!(lpMidiOutHdr->dwFlags & MHDR_PREPARED)) {
518 return MMSYSERR_NOERROR;
521 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
522 return MMSYSERR_INVALHANDLE;
524 return MMDRV_Message(wmld, MODM_UNPREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
527 /**************************************************************************
528 * midiOutShortMsg [WINMM.@]
530 UINT WINAPI midiOutShortMsg(HMIDIOUT hMidiOut, DWORD dwMsg)
534 TRACE("(%p, %08X)\n", hMidiOut, dwMsg);
536 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
537 return MMSYSERR_INVALHANDLE;
539 return MMDRV_Message(wmld, MODM_DATA, dwMsg, 0L);
542 /**************************************************************************
543 * midiOutLongMsg [WINMM.@]
545 UINT WINAPI midiOutLongMsg(HMIDIOUT hMidiOut,
546 MIDIHDR* lpMidiOutHdr, UINT uSize)
550 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
552 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
553 return MMSYSERR_INVALHANDLE;
555 return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD_PTR)lpMidiOutHdr, uSize);
558 /**************************************************************************
559 * midiOutReset [WINMM.@]
561 UINT WINAPI midiOutReset(HMIDIOUT hMidiOut)
565 TRACE("(%p)\n", hMidiOut);
567 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
568 return MMSYSERR_INVALHANDLE;
570 return MMDRV_Message(wmld, MODM_RESET, 0L, 0L);
573 /**************************************************************************
574 * midiOutGetVolume [WINMM.@]
576 UINT WINAPI midiOutGetVolume(HMIDIOUT hMidiOut, DWORD* lpdwVolume)
580 TRACE("(%p, %p);\n", hMidiOut, lpdwVolume);
582 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
583 return MMSYSERR_INVALHANDLE;
585 return MMDRV_Message(wmld, MODM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
588 /**************************************************************************
589 * midiOutSetVolume [WINMM.@]
591 UINT WINAPI midiOutSetVolume(HMIDIOUT hMidiOut, DWORD dwVolume)
595 TRACE("(%p, %d);\n", hMidiOut, dwVolume);
597 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
598 return MMSYSERR_INVALHANDLE;
600 return MMDRV_Message(wmld, MODM_SETVOLUME, dwVolume, 0L);
603 /**************************************************************************
604 * midiOutCachePatches [WINMM.@]
606 UINT WINAPI midiOutCachePatches(HMIDIOUT hMidiOut, UINT uBank,
607 WORD* lpwPatchArray, UINT uFlags)
609 /* not really necessary to support this */
610 FIXME("not supported yet\n");
611 return MMSYSERR_NOTSUPPORTED;
614 /**************************************************************************
615 * midiOutCacheDrumPatches [WINMM.@]
617 UINT WINAPI midiOutCacheDrumPatches(HMIDIOUT hMidiOut, UINT uPatch,
618 WORD* lpwKeyArray, UINT uFlags)
620 FIXME("not supported yet\n");
621 return MMSYSERR_NOTSUPPORTED;
624 /**************************************************************************
625 * midiOutGetID [WINMM.@]
627 UINT WINAPI midiOutGetID(HMIDIOUT hMidiOut, UINT* lpuDeviceID)
631 TRACE("(%p, %p)\n", hMidiOut, lpuDeviceID);
633 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
634 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
635 return MMSYSERR_INVALHANDLE;
637 *lpuDeviceID = wmld->uDeviceID;
638 return MMSYSERR_NOERROR;
641 /**************************************************************************
642 * midiOutMessage [WINMM.@]
644 UINT WINAPI midiOutMessage(HMIDIOUT hMidiOut, UINT uMessage,
645 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
649 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
651 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) {
653 if (uMessage == 0x0001) {
654 *(LPDWORD)dwParam1 = 1;
657 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) != NULL) {
658 return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
660 return MMSYSERR_INVALHANDLE;
666 FIXME("can't handle OPEN or CLOSE message!\n");
667 return MMSYSERR_NOTSUPPORTED;
669 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
672 /**************************************************************************
673 * midiInGetNumDevs [WINMM.@]
675 UINT WINAPI midiInGetNumDevs(void)
677 return MMDRV_GetNum(MMDRV_MIDIIN);
680 /**************************************************************************
681 * midiInGetDevCapsW [WINMM.@]
683 UINT WINAPI midiInGetDevCapsW(UINT_PTR uDeviceID, LPMIDIINCAPSW lpCaps, UINT uSize)
687 TRACE("(%ld, %p, %d);\n", uDeviceID, lpCaps, uSize);
689 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
691 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIIN, TRUE)) == NULL)
692 return MMSYSERR_BADDEVICEID;
694 return MMDRV_Message(wmld, MIDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
697 /**************************************************************************
698 * midiInGetDevCapsA [WINMM.@]
700 UINT WINAPI midiInGetDevCapsA(UINT_PTR uDeviceID, LPMIDIINCAPSA lpCaps, UINT uSize)
705 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
707 ret = midiInGetDevCapsW(uDeviceID, &micW, sizeof(micW));
709 if (ret == MMSYSERR_NOERROR) {
711 micA.wMid = micW.wMid;
712 micA.wPid = micW.wPid;
713 micA.vDriverVersion = micW.vDriverVersion;
714 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
715 sizeof(micA.szPname), NULL, NULL );
716 micA.dwSupport = micW.dwSupport;
717 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
722 /**************************************************************************
723 * midiInOpen [WINMM.@]
725 UINT WINAPI midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID,
726 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
732 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
733 lphMidiIn, uDeviceID, dwCallback, dwInstance, dwFlags);
735 if (lphMidiIn != NULL) *lphMidiIn = 0;
737 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
738 if (dwRet != MMSYSERR_NOERROR)
741 lpwm = (LPWINE_MIDI)MMDRV_Alloc(sizeof(WINE_MIDI), MMDRV_MIDIIN, &hMidiIn,
742 &dwFlags, &dwCallback, &dwInstance);
745 return MMSYSERR_NOMEM;
747 lpwm->mod.hMidi = hMidiIn;
748 lpwm->mod.dwCallback = dwCallback;
749 lpwm->mod.dwInstance = dwInstance;
751 lpwm->mld.uDeviceID = uDeviceID;
752 dwRet = MMDRV_Open(&lpwm->mld, MIDM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
754 if (dwRet != MMSYSERR_NOERROR) {
755 MMDRV_Free(hMidiIn, &lpwm->mld);
758 if (lphMidiIn != NULL) *lphMidiIn = hMidiIn;
759 TRACE("=> %d hMidi=%p\n", dwRet, hMidiIn);
764 /**************************************************************************
765 * midiInClose [WINMM.@]
767 UINT WINAPI midiInClose(HMIDIIN hMidiIn)
772 TRACE("(%p)\n", hMidiIn);
774 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
775 return MMSYSERR_INVALHANDLE;
777 dwRet = MMDRV_Close(wmld, MIDM_CLOSE);
778 MMDRV_Free(hMidiIn, wmld);
782 /**************************************************************************
783 * midiInPrepareHeader [WINMM.@]
785 UINT WINAPI midiInPrepareHeader(HMIDIIN hMidiIn,
786 MIDIHDR* lpMidiInHdr, UINT uSize)
790 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
792 if (lpMidiInHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
793 return MMSYSERR_INVALPARAM;
795 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
796 return MMSYSERR_INVALHANDLE;
798 return MMDRV_Message(wmld, MIDM_PREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
801 /**************************************************************************
802 * midiInUnprepareHeader [WINMM.@]
804 UINT WINAPI midiInUnprepareHeader(HMIDIIN hMidiIn,
805 MIDIHDR* lpMidiInHdr, UINT uSize)
809 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
811 if (lpMidiInHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
812 return MMSYSERR_INVALPARAM;
814 if (!(lpMidiInHdr->dwFlags & MHDR_PREPARED)) {
815 return MMSYSERR_NOERROR;
818 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
819 return MMSYSERR_INVALHANDLE;
821 return MMDRV_Message(wmld, MIDM_UNPREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
824 /**************************************************************************
825 * midiInAddBuffer [WINMM.@]
827 UINT WINAPI midiInAddBuffer(HMIDIIN hMidiIn,
828 MIDIHDR* lpMidiInHdr, UINT uSize)
832 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
834 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
835 return MMSYSERR_INVALHANDLE;
837 return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD_PTR)lpMidiInHdr, uSize);
840 /**************************************************************************
841 * midiInStart [WINMM.@]
843 UINT WINAPI midiInStart(HMIDIIN hMidiIn)
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_START, 0L, 0L);
855 /**************************************************************************
856 * midiInStop [WINMM.@]
858 UINT WINAPI midiInStop(HMIDIIN hMidiIn)
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_STOP, 0L, 0L);
870 /**************************************************************************
871 * midiInReset [WINMM.@]
873 UINT WINAPI midiInReset(HMIDIIN hMidiIn)
877 TRACE("(%p)\n", hMidiIn);
879 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
880 return MMSYSERR_INVALHANDLE;
882 return MMDRV_Message(wmld, MIDM_RESET, 0L, 0L);
885 /**************************************************************************
886 * midiInGetID [WINMM.@]
888 UINT WINAPI midiInGetID(HMIDIIN hMidiIn, UINT* lpuDeviceID)
892 TRACE("(%p, %p)\n", hMidiIn, lpuDeviceID);
894 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
896 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, TRUE)) == NULL)
897 return MMSYSERR_INVALHANDLE;
899 *lpuDeviceID = wmld->uDeviceID;
901 return MMSYSERR_NOERROR;
904 /**************************************************************************
905 * midiInMessage [WINMM.@]
907 UINT WINAPI midiInMessage(HMIDIIN hMidiIn, UINT uMessage,
908 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
912 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiIn, uMessage, dwParam1, dwParam2);
914 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
915 return MMSYSERR_INVALHANDLE;
920 FIXME("can't handle OPEN or CLOSE message!\n");
921 return MMSYSERR_NOTSUPPORTED;
923 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
926 /**************************************************************************
927 * midiConnect [WINMM.@]
929 MMRESULT WINAPI midiConnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
931 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
932 return MMSYSERR_ERROR;
935 /**************************************************************************
936 * midiDisconnect [WINMM.@]
938 MMRESULT WINAPI midiDisconnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
940 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
941 return MMSYSERR_ERROR;
944 typedef struct WINE_MIDIStream {
958 #define WINE_MSM_HEADER (WM_USER+0)
959 #define WINE_MSM_STOP (WM_USER+1)
960 #define WINE_MSM_PAUSE (WM_USER+2)
961 #define WINE_MSM_RESUME (WM_USER+3)
963 /**************************************************************************
964 * MMSYSTEM_GetMidiStream [internal]
966 static BOOL MMSYSTEM_GetMidiStream(HMIDISTRM hMidiStrm, WINE_MIDIStream** lpMidiStrm, WINE_MIDI** lplpwm)
968 WINE_MIDI* lpwm = (LPWINE_MIDI)MMDRV_Get(hMidiStrm, MMDRV_MIDIOUT, FALSE);
977 *lpMidiStrm = (WINE_MIDIStream*)lpwm->mod.rgIds.dwStreamID;
979 return *lpMidiStrm != NULL;
982 /**************************************************************************
983 * MMSYSTEM_MidiStream_Convert [internal]
985 static DWORD MMSYSTEM_MidiStream_Convert(WINE_MIDIStream* lpMidiStrm, DWORD pulse)
989 if (lpMidiStrm->dwTimeDiv == 0) {
990 FIXME("Shouldn't happen. lpMidiStrm->dwTimeDiv = 0\n");
991 } else if (lpMidiStrm->dwTimeDiv > 0x8000) { /* SMPTE, unchecked FIXME? */
992 int nf = -(char)HIBYTE(lpMidiStrm->dwTimeDiv); /* number of frames */
993 int nsf = LOBYTE(lpMidiStrm->dwTimeDiv); /* number of sub-frames */
994 ret = (pulse * 1000) / (nf * nsf);
996 ret = (DWORD)((double)pulse * ((double)lpMidiStrm->dwTempo / 1000) /
997 (double)lpMidiStrm->dwTimeDiv);
1003 /**************************************************************************
1004 * MMSYSTEM_MidiStream_MessageHandler [internal]
1006 static BOOL MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream* lpMidiStrm, LPWINE_MIDI lpwm, LPMSG msg)
1008 LPMIDIHDR lpMidiHdr;
1011 BOOL paused = FALSE;
1014 switch (msg->message) {
1019 /* this is not quite what MS doc says... */
1020 midiOutReset(lpMidiStrm->hDevice);
1021 /* empty list of already submitted buffers */
1022 lpMidiHdr = lpMidiStrm->lpMidiHdr;
1023 lpMidiStrm->lpMidiHdr = NULL;
1025 LPMIDIHDR lphdr = lpMidiHdr;
1026 lpMidiHdr = lpMidiHdr->lpNext;
1027 lphdr->dwFlags |= MHDR_DONE;
1028 lphdr->dwFlags &= ~MHDR_INQUEUE;
1030 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1031 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1032 lpwm->mod.dwInstance, (DWORD_PTR)lphdr, 0);
1035 case WINE_MSM_RESUME:
1036 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1037 lpMidiStrm->dwStartTicks = GetTickCount() - lpMidiStrm->dwPositionMS;
1039 case WINE_MSM_PAUSE:
1040 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1043 /* FIXME(EPP): "I don't understand the content of the first MIDIHDR sent
1044 * by native mcimidi, it doesn't look like a correct one".
1045 * this trick allows to throw it away... but I don't like it.
1046 * It looks like part of the file I'm trying to play and definitively looks
1047 * like raw midi content
1048 * I'd really like to understand why native mcimidi sends it. Perhaps a bad
1049 * synchronization issue where native mcimidi is still processing raw MIDI
1050 * content before generating MIDIEVENTs ?
1052 * 4c 04 89 3b 00 81 7c 99 3b 43 00 99 23 5e 04 89 L..;..|.;C..#^..
1053 * 3b 00 00 89 23 00 7c 99 3b 45 00 99 28 62 04 89 ;...#.|.;E..(b..
1054 * 3b 00 00 89 28 00 81 7c 99 3b 4e 00 99 23 5e 04 ;...(..|.;N..#^.
1055 * 89 3b 00 00 89 23 00 7c 99 3b 45 00 99 23 78 04 .;...#.|.;E..#x.
1056 * 89 3b 00 00 89 23 00 81 7c 99 3b 48 00 99 23 5e .;...#..|.;H..#^
1057 * 04 89 3b 00 00 89 23 00 7c 99 3b 4e 00 99 28 62 ..;...#.|.;N..(b
1058 * 04 89 3b 00 00 89 28 00 81 7c 99 39 4c 00 99 23 ..;...(..|.9L..#
1059 * 5e 04 89 39 00 00 89 23 00 82 7c 99 3b 4c 00 99 ^..9...#..|.;L..
1060 * 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 00 99 #^..;...#.|.;H..
1061 * 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b 3f 04 (b..;...(..|.;?.
1062 * 89 3b 00 1c 99 23 5e 04 89 23 00 5c 99 3b 45 00 .;...#^..#.\.;E.
1063 * 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 3b 46 .#x..;...#..|.;F
1064 * 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 ..#^..;...#.|.;H
1065 * 00 99 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b ..(b..;...(..|.;
1066 * 46 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b F..#^..;...#.|.;
1067 * 48 00 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 H..#x..;...#..|.
1068 * 3b 4c 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 ;L..#^..;...#.|.
1070 case WINE_MSM_HEADER:
1071 /* sets initial tick count for first MIDIHDR */
1072 if (!lpMidiStrm->dwStartTicks)
1073 lpMidiStrm->dwStartTicks = GetTickCount();
1074 lpMidiHdr = (LPMIDIHDR)msg->lParam;
1075 lpData = (LPBYTE)lpMidiHdr->lpData;
1076 TRACE("Adding %s lpMidiHdr=%p [lpData=0x%p dwBytesRecorded=%u/%u dwFlags=0x%08x size=%lu]\n",
1077 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular", lpMidiHdr,
1078 lpData, lpMidiHdr->dwBytesRecorded, lpMidiHdr->dwBufferLength,
1079 lpMidiHdr->dwFlags, msg->wParam);
1081 /* dumps content of lpMidiHdr->lpData
1082 * FIXME: there should be a debug routine somewhere that already does this
1084 for (dwToGo = 0; dwToGo < lpMidiHdr->dwBufferLength; dwToGo += 16) {
1088 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++)
1089 printf("%02x ", lpData[dwToGo + i]);
1092 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++) {
1093 ch = lpData[dwToGo + i];
1094 printf("%c", (ch >= 0x20 && ch <= 0x7F) ? ch : '.');
1099 if (((LPMIDIEVENT)lpData)->dwStreamID != 0 &&
1100 ((LPMIDIEVENT)lpData)->dwStreamID != 0xFFFFFFFF &&
1101 ((LPMIDIEVENT)lpData)->dwStreamID != (DWORD)lpMidiStrm) {
1102 FIXME("Dropping bad %s lpMidiHdr (streamID=%08x)\n",
1103 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular",
1104 ((LPMIDIEVENT)lpData)->dwStreamID);
1105 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1106 lpMidiHdr->dwFlags |= MHDR_DONE;
1108 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1109 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1110 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1114 lpMidiHdr->lpNext = 0;
1115 for (lpmh = &lpMidiStrm->lpMidiHdr; *lpmh; lpmh = &(*lpmh)->lpNext);
1119 FIXME("Unknown message %d\n", msg->message);
1124 GetMessageA(msg, 0, 0, 0);
1128 /**************************************************************************
1129 * MMSYSTEM_MidiStream_Player [internal]
1131 static DWORD CALLBACK MMSYSTEM_MidiStream_Player(LPVOID pmt)
1133 WINE_MIDIStream* lpMidiStrm = pmt;
1138 LPMIDIHDR lpMidiHdr;
1141 TRACE("(%p)!\n", lpMidiStrm);
1144 (lpwm = (LPWINE_MIDI)MMDRV_Get(lpMidiStrm->hDevice, MMDRV_MIDIOUT, FALSE)) == NULL)
1147 /* force thread's queue creation */
1148 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
1150 lpMidiStrm->dwStartTicks = 0;
1151 lpMidiStrm->dwPulses = 0;
1153 lpMidiStrm->lpMidiHdr = 0;
1155 /* midiStreamOpen is waiting for ack */
1156 SetEvent(lpMidiStrm->hEvent);
1159 lpMidiHdr = lpMidiStrm->lpMidiHdr;
1161 /* for first message, block until one arrives, then process all that are available */
1162 GetMessageA(&msg, 0, 0, 0);
1164 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1166 } while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE));
1171 while (dwOffset + offsetof(MIDIEVENT,dwParms) <= lpMidiHdr->dwBytesRecorded) {
1172 LPMIDIEVENT me = (LPMIDIEVENT)(lpMidiHdr->lpData+dwOffset);
1174 /* do we have to wait ? */
1175 if (me->dwDeltaTime) {
1176 lpMidiStrm->dwPositionMS += MMSYSTEM_MidiStream_Convert(lpMidiStrm, me->dwDeltaTime);
1177 lpMidiStrm->dwPulses += me->dwDeltaTime;
1179 dwToGo = lpMidiStrm->dwStartTicks + lpMidiStrm->dwPositionMS;
1181 TRACE("%d/%d/%d\n", dwToGo, GetTickCount(), me->dwDeltaTime);
1182 while ((dwCurrTC = GetTickCount()) < dwToGo) {
1183 if (MsgWaitForMultipleObjects(0, NULL, FALSE, dwToGo - dwCurrTC, QS_ALLINPUT) == WAIT_OBJECT_0) {
1184 /* got a message, handle it */
1185 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
1186 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1188 /* is lpMidiHdr still current? */
1189 if (lpMidiHdr != lpMidiStrm->lpMidiHdr) {
1194 /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
1199 switch (MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK)) {
1201 FIXME("NIY: MEVT_COMMENT\n");
1202 /* do nothing, skip bytes */
1205 midiOutLongMsg(lpMidiStrm->hDevice, lpMidiStrm->lpMidiHdr, MEVT_EVENTPARM(me->dwEvent));
1210 midiOutShortMsg(lpMidiStrm->hDevice, MEVT_EVENTPARM(me->dwEvent));
1213 lpMidiStrm->dwTempo = MEVT_EVENTPARM(me->dwEvent);
1218 FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK));
1221 if (me->dwEvent & MEVT_F_CALLBACK) {
1222 /* native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
1223 lpMidiHdr->dwOffset = dwOffset;
1224 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1225 (HDRVR)lpMidiStrm->hDevice, MM_MOM_POSITIONCB,
1226 lpwm->mod.dwInstance, (LPARAM)lpMidiHdr, 0L);
1228 dwOffset += offsetof(MIDIEVENT,dwParms);
1229 if (me->dwEvent & MEVT_F_LONG)
1230 dwOffset += (MEVT_EVENTPARM(me->dwEvent) + 3) & ~3;
1232 /* done with this header */
1233 lpMidiStrm->lpMidiHdr = lpMidiHdr->lpNext;
1234 lpMidiHdr->dwFlags |= MHDR_DONE;
1235 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1237 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1238 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1239 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1243 TRACE("End of thread\n");
1247 /**************************************************************************
1248 * midiStreamClose [WINMM.@]
1250 MMRESULT WINAPI midiStreamClose(HMIDISTRM hMidiStrm)
1252 WINE_MIDIStream* lpMidiStrm;
1255 TRACE("(%p)!\n", hMidiStrm);
1257 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1258 return MMSYSERR_INVALHANDLE;
1260 midiStreamStop(hMidiStrm);
1261 PostThreadMessageA(lpMidiStrm->dwThreadID, WM_QUIT, 0, 0);
1262 CloseHandle(lpMidiStrm->hEvent);
1263 if (lpMidiStrm->hThread) {
1264 if (GetCurrentThreadId() != lpMidiStrm->dwThreadID)
1265 WaitForSingleObject(lpMidiStrm->hThread, INFINITE);
1267 FIXME("leak from call within function callback\n");
1268 ret = MMSYSERR_HANDLEBUSY; /* yet don't signal it to app */
1270 CloseHandle(lpMidiStrm->hThread);
1273 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1275 return midiOutClose((HMIDIOUT)hMidiStrm);
1278 /**************************************************************************
1279 * midiStreamOpen [WINMM.@]
1281 MMRESULT WINAPI midiStreamOpen(HMIDISTRM* lphMidiStrm, LPUINT lpuDeviceID,
1282 DWORD cMidi, DWORD_PTR dwCallback,
1283 DWORD_PTR dwInstance, DWORD fdwOpen)
1285 WINE_MIDIStream* lpMidiStrm;
1287 MIDIOPENSTRMID mosm;
1291 TRACE("(%p, %p, %d, 0x%08lx, 0x%08lx, 0x%08x)!\n",
1292 lphMidiStrm, lpuDeviceID, cMidi, dwCallback, dwInstance, fdwOpen);
1294 if (cMidi != 1 || lphMidiStrm == NULL || lpuDeviceID == NULL)
1295 return MMSYSERR_INVALPARAM;
1297 ret = WINMM_CheckCallback(dwCallback, fdwOpen, FALSE);
1298 if (ret != MMSYSERR_NOERROR)
1301 lpMidiStrm = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream));
1303 return MMSYSERR_NOMEM;
1305 lpMidiStrm->dwTempo = 500000;
1306 lpMidiStrm->dwTimeDiv = 480; /* 480 is 120 quarter notes per minute *//* FIXME ??*/
1307 lpMidiStrm->dwPositionMS = 0;
1309 mosm.dwStreamID = (DWORD)lpMidiStrm;
1310 /* FIXME: the correct value is not allocated yet for MAPPER */
1311 mosm.wDeviceID = *lpuDeviceID;
1312 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &fdwOpen, 1, &mosm);
1314 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1315 return MMSYSERR_NOMEM;
1317 lpMidiStrm->hDevice = hMidiOut;
1318 *lphMidiStrm = (HMIDISTRM)hMidiOut;
1320 lpwm->mld.uDeviceID = *lpuDeviceID;
1322 ret = MMDRV_Open(&lpwm->mld, MODM_OPEN, (DWORD_PTR)&lpwm->mod, fdwOpen);
1323 if (ret != MMSYSERR_NOERROR) {
1324 MMDRV_Free(hMidiOut, &lpwm->mld);
1325 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1329 lpMidiStrm->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1330 lpMidiStrm->wFlags = HIWORD(fdwOpen);
1332 lpMidiStrm->hThread = CreateThread(NULL, 0, MMSYSTEM_MidiStream_Player,
1333 lpMidiStrm, 0, &(lpMidiStrm->dwThreadID));
1335 if (!lpMidiStrm->hThread) {
1336 midiStreamClose((HMIDISTRM)hMidiOut);
1337 return MMSYSERR_NOMEM;
1339 SetThreadPriority(lpMidiStrm->hThread, THREAD_PRIORITY_TIME_CRITICAL);
1341 /* wait for thread to have started, and for its queue to be created */
1342 WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
1344 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1346 TRACE("=> (%u/%d) hMidi=%p ret=%d lpMidiStrm=%p\n",
1347 *lpuDeviceID, lpwm->mld.uDeviceID, *lphMidiStrm, ret, lpMidiStrm);
1351 /**************************************************************************
1352 * midiStreamOut [WINMM.@]
1354 MMRESULT WINAPI midiStreamOut(HMIDISTRM hMidiStrm, LPMIDIHDR lpMidiHdr,
1357 WINE_MIDIStream* lpMidiStrm;
1358 DWORD ret = MMSYSERR_NOERROR;
1360 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMidiHdr, cbMidiHdr);
1362 if (cbMidiHdr < offsetof(MIDIHDR,dwOffset) || !lpMidiHdr || !lpMidiHdr->lpData
1363 || lpMidiHdr->dwBufferLength < lpMidiHdr->dwBytesRecorded
1364 || lpMidiHdr->dwBytesRecorded % 4 /* player expects DWORD padding */)
1365 return MMSYSERR_INVALPARAM;
1366 /* FIXME: Native additionally checks if the MIDIEVENTs in lpData
1367 * exactly fit dwBytesRecorded. */
1369 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1370 return MIDIERR_UNPREPARED;
1372 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1373 return MIDIERR_STILLPLAYING;
1375 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1376 ret = MMSYSERR_INVALHANDLE;
1378 lpMidiHdr->dwFlags |= MHDR_ISSTRM | MHDR_INQUEUE;
1379 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1380 if (!PostThreadMessageA(lpMidiStrm->dwThreadID,
1381 WINE_MSM_HEADER, cbMidiHdr,
1382 (LPARAM)lpMidiHdr)) {
1383 ERR("bad PostThreadMessageA\n");
1384 ret = MMSYSERR_ERROR;
1390 /**************************************************************************
1391 * midiStreamPause [WINMM.@]
1393 MMRESULT WINAPI midiStreamPause(HMIDISTRM hMidiStrm)
1395 WINE_MIDIStream* lpMidiStrm;
1396 DWORD ret = MMSYSERR_NOERROR;
1398 TRACE("(%p)!\n", hMidiStrm);
1400 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1401 return MMSYSERR_INVALHANDLE;
1402 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1406 /**************************************************************************
1407 * midiStreamPosition [WINMM.@]
1409 MMRESULT WINAPI midiStreamPosition(HMIDISTRM hMidiStrm, LPMMTIME lpMMT, UINT cbmmt)
1411 WINE_MIDIStream* lpMidiStrm;
1412 DWORD ret = MMSYSERR_NOERROR;
1414 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMMT, cbmmt);
1416 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1417 ret = MMSYSERR_INVALHANDLE;
1418 } else if (lpMMT == NULL || cbmmt != sizeof(MMTIME)) {
1419 ret = MMSYSERR_INVALPARAM;
1421 switch (lpMMT->wType) {
1423 FIXME("Unsupported time type %x\n", lpMMT->wType);
1426 lpMMT->wType = TIME_MS;
1427 /* fall through to alternative format */
1429 lpMMT->u.ms = lpMidiStrm->dwPositionMS;
1430 TRACE("=> %d ms\n", lpMMT->u.ms);
1433 lpMMT->u.ticks = lpMidiStrm->dwPulses;
1434 TRACE("=> %d ticks\n", lpMMT->u.ticks);
1441 /**************************************************************************
1442 * midiStreamProperty [WINMM.@]
1444 MMRESULT WINAPI midiStreamProperty(HMIDISTRM hMidiStrm, LPBYTE lpPropData, DWORD dwProperty)
1446 WINE_MIDIStream* lpMidiStrm;
1447 MMRESULT ret = MMSYSERR_NOERROR;
1449 TRACE("(%p, %p, %x)\n", hMidiStrm, lpPropData, dwProperty);
1451 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1452 ret = MMSYSERR_INVALHANDLE;
1453 } else if ((dwProperty & (MIDIPROP_GET|MIDIPROP_SET)) == 0) {
1454 ret = MMSYSERR_INVALPARAM;
1455 } else if (dwProperty & MIDIPROP_TEMPO) {
1456 MIDIPROPTEMPO* mpt = (MIDIPROPTEMPO*)lpPropData;
1458 if (sizeof(MIDIPROPTEMPO) != mpt->cbStruct) {
1459 ret = MMSYSERR_INVALPARAM;
1460 } else if (dwProperty & MIDIPROP_SET) {
1461 lpMidiStrm->dwTempo = mpt->dwTempo;
1462 TRACE("Setting tempo to %d\n", mpt->dwTempo);
1463 } else if (dwProperty & MIDIPROP_GET) {
1464 mpt->dwTempo = lpMidiStrm->dwTempo;
1465 TRACE("Getting tempo <= %d\n", mpt->dwTempo);
1467 } else if (dwProperty & MIDIPROP_TIMEDIV) {
1468 MIDIPROPTIMEDIV* mptd = (MIDIPROPTIMEDIV*)lpPropData;
1470 if (sizeof(MIDIPROPTIMEDIV) != mptd->cbStruct) {
1471 ret = MMSYSERR_INVALPARAM;
1472 } else if (dwProperty & MIDIPROP_SET) {
1473 lpMidiStrm->dwTimeDiv = mptd->dwTimeDiv;
1474 TRACE("Setting time div to %d\n", mptd->dwTimeDiv);
1475 } else if (dwProperty & MIDIPROP_GET) {
1476 mptd->dwTimeDiv = lpMidiStrm->dwTimeDiv;
1477 TRACE("Getting time div <= %d\n", mptd->dwTimeDiv);
1480 ret = MMSYSERR_INVALPARAM;
1486 /**************************************************************************
1487 * midiStreamRestart [WINMM.@]
1489 MMRESULT WINAPI midiStreamRestart(HMIDISTRM hMidiStrm)
1491 WINE_MIDIStream* lpMidiStrm;
1492 MMRESULT ret = MMSYSERR_NOERROR;
1494 TRACE("(%p)!\n", hMidiStrm);
1496 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1497 return MMSYSERR_INVALHANDLE;
1498 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_RESUME, 0, 0);
1502 /**************************************************************************
1503 * midiStreamStop [WINMM.@]
1505 MMRESULT WINAPI midiStreamStop(HMIDISTRM hMidiStrm)
1507 WINE_MIDIStream* lpMidiStrm;
1508 MMRESULT ret = MMSYSERR_NOERROR;
1510 TRACE("(%p)!\n", hMidiStrm);
1512 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1513 return MMSYSERR_INVALHANDLE;
1514 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_STOP, 0, 0);
1525 static DWORD WINAPI mmTaskRun(void* pmt)
1527 struct mm_starter mms;
1529 memcpy(&mms, pmt, sizeof(struct mm_starter));
1530 HeapFree(GetProcessHeap(), 0, pmt);
1532 if (mms.event) SetEvent(mms.event);
1536 /******************************************************************
1537 * mmTaskCreate (WINMM.@)
1539 UINT WINAPI mmTaskCreate(LPTASKCALLBACK cb, HANDLE* ph, DWORD_PTR client)
1543 struct mm_starter *mms;
1545 mms = HeapAlloc(GetProcessHeap(), 0, sizeof(struct mm_starter));
1546 if (mms == NULL) return TASKERR_OUTOFMEMORY;
1549 mms->client = client;
1550 if (ph) hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1551 mms->event = hEvent;
1553 hThread = CreateThread(0, 0, mmTaskRun, mms, 0, NULL);
1555 HeapFree(GetProcessHeap(), 0, mms);
1556 if (hEvent) CloseHandle(hEvent);
1557 return TASKERR_OUTOFMEMORY;
1559 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
1560 if (ph) *ph = hEvent;
1561 CloseHandle(hThread);
1565 /******************************************************************
1566 * mmTaskBlock (WINMM.@)
1568 VOID WINAPI mmTaskBlock(DWORD tid)
1574 GetMessageA(&msg, 0, 0, 0);
1575 if (msg.hwnd) DispatchMessageA(&msg);
1576 } while (msg.message != WM_USER);
1579 /******************************************************************
1580 * mmTaskSignal (WINMM.@)
1582 BOOL WINAPI mmTaskSignal(DWORD tid)
1584 return PostThreadMessageW(tid, WM_USER, 0, 0);
1587 /******************************************************************
1588 * mmTaskYield (WINMM.@)
1590 VOID WINAPI mmTaskYield(VOID) {}
1592 /******************************************************************
1593 * mmGetCurrentTask (WINMM.@)
1595 DWORD WINAPI mmGetCurrentTask(VOID)
1597 return GetCurrentThreadId();