dmusic: Support creating default ports with GUID_NULL.
[wine.git] / dlls / winmm / winmm.c
blob714a84e1c27e7abd77b5f3effcf26fecbe48bad5
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_DeleteIData [internal]
88 static void WINMM_DeleteIData(void)
90 TIME_MMTimeStop();
92 WINMM_DeleteWaveform();
93 CloseHandle(psLastEvent);
94 DeleteCriticalSection(&WINMM_cs);
97 /******************************************************************
98 * WINMM_ErrorToString
100 const char* WINMM_ErrorToString(MMRESULT error)
102 #define ERR_TO_STR(dev) case dev: return #dev
103 switch (error) {
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);
134 #undef ERR_TO_STR
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);
148 switch (fdwReason) {
149 case DLL_PROCESS_ATTACH:
150 DisableThreadLibraryCalls(hInstDLL);
152 if (!WINMM_CreateIData(hInstDLL))
153 return FALSE;
154 break;
155 case DLL_PROCESS_DETACH:
156 /* close all opened MCI drivers */
157 MCI_SendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0L);
158 MMDRV_Exit();
159 /* There's no guarantee the drivers haven't already been unloaded on
160 * process shutdown.
162 if (!fImpLoad)
164 /* now unload all remaining drivers... */
165 DRIVER_UnloadAll();
168 WINMM_DeleteIData();
169 break;
171 return TRUE;
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 */
181 break;
182 case CALLBACK_WINDOW:
183 if (dwCallback && !IsWindow((HWND)dwCallback))
184 return MMSYSERR_INVALPARAM;
185 break;
187 case CALLBACK_FUNCTION:
188 /* a NULL cb is acceptable since w2k, MMSYSERR_INVALPARAM earlier */
189 if (mixer)
190 return MMSYSERR_INVALFLAG; /* since w2k, MMSYSERR_NOTSUPPORTED earlier */
191 break;
192 case CALLBACK_THREAD:
193 case CALLBACK_EVENT:
194 if (mixer) /* FIXME: mixer supports THREAD+EVENT since w2k */
195 return MMSYSERR_NOTSUPPORTED; /* w9X */
196 break;
197 default:
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)
216 LPWINE_MLD wmld;
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)
232 AUXCAPSW acW;
233 UINT ret;
235 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
237 ret = auxGetDevCapsW(uDeviceID, &acW, sizeof(acW));
239 if (ret == MMSYSERR_NOERROR) {
240 AUXCAPSA acA;
241 acA.wMid = acW.wMid;
242 acA.wPid = acW.wPid;
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)));
250 return ret;
253 /**************************************************************************
254 * auxGetVolume [WINMM.@]
256 UINT WINAPI auxGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
258 LPWINE_MLD wmld;
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)
272 LPWINE_MLD wmld;
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)
286 LPWINE_MLD wmld;
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,
306 UINT uSize)
308 LPWINE_MLD wmld;
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,
324 UINT uSize)
326 MIDIOUTCAPSW mocW;
327 UINT ret;
329 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
331 ret = midiOutGetDevCapsW(uDeviceID, &mocW, sizeof(mocW));
333 if (ret == MMSYSERR_NOERROR) {
334 MIDIOUTCAPSA mocA;
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)));
347 return ret;
350 /**************************************************************************
351 * midiOutGetErrorTextA [WINMM.@]
352 * midiInGetErrorTextA [WINMM.@]
354 UINT WINAPI midiOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
356 UINT ret;
358 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
359 else if (uSize == 0) ret = MMSYSERR_NOERROR;
360 else
362 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
363 if (!xstr) ret = MMSYSERR_NOMEM;
364 else
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);
372 return ret;
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;
385 else if (
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;
394 return ret;
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)
404 HANDLE hMidiOut;
405 LPWINE_MIDI lpwm;
406 UINT size;
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;
414 if (lpwm) {
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;
420 if (cIDs)
421 memcpy(&(lpwm->mod.rgIds), lpIDs, cIDs * sizeof(MIDIOPENSTRMID));
423 return lpwm;
426 /**************************************************************************
427 * midiOutOpen [WINMM.@]
429 UINT WINAPI midiOutOpen(LPHMIDIOUT lphMidiOut, UINT uDeviceID,
430 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
432 HMIDIOUT hMidiOut;
433 LPWINE_MIDI lpwm;
434 UINT dwRet;
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)
443 return dwRet;
445 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &dwFlags, 0, NULL);
447 if (lpwm == 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);
456 hMidiOut = 0;
459 if (lphMidiOut) *lphMidiOut = hMidiOut;
460 TRACE("=> %d hMidi=%p\n", dwRet, hMidiOut);
462 return dwRet;
465 /**************************************************************************
466 * midiOutClose [WINMM.@]
468 UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
470 LPWINE_MLD wmld;
471 DWORD dwRet;
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);
481 return dwRet;
484 /**************************************************************************
485 * midiOutPrepareHeader [WINMM.@]
487 UINT WINAPI midiOutPrepareHeader(HMIDIOUT hMidiOut,
488 MIDIHDR* lpMidiOutHdr, UINT uSize)
490 LPWINE_MLD wmld;
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)
510 LPWINE_MLD wmld;
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)
532 LPWINE_MLD wmld;
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)
548 LPWINE_MLD wmld;
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)
563 LPWINE_MLD wmld;
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)
578 LPWINE_MLD wmld;
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)
593 LPWINE_MLD wmld;
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)
629 LPWINE_MLD wmld;
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)
647 LPWINE_MLD wmld;
649 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
651 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) {
652 /* HACK... */
653 if (uMessage == 0x0001) {
654 *(LPDWORD)dwParam1 = 1;
655 return 0;
657 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) != NULL) {
658 return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
660 return MMSYSERR_INVALHANDLE;
663 switch (uMessage) {
664 case MODM_OPEN:
665 case MODM_CLOSE:
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)
685 LPWINE_MLD wmld;
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)
702 MIDIINCAPSW micW;
703 UINT ret;
705 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
707 ret = midiInGetDevCapsW(uDeviceID, &micW, sizeof(micW));
709 if (ret == MMSYSERR_NOERROR) {
710 MIDIINCAPSA micA;
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)));
719 return ret;
722 /**************************************************************************
723 * midiInOpen [WINMM.@]
725 UINT WINAPI midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID,
726 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
728 HANDLE hMidiIn;
729 LPWINE_MIDI lpwm;
730 DWORD dwRet;
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)
739 return dwRet;
741 lpwm = (LPWINE_MIDI)MMDRV_Alloc(sizeof(WINE_MIDI), MMDRV_MIDIIN, &hMidiIn,
742 &dwFlags, &dwCallback, &dwInstance);
744 if (lpwm == NULL)
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);
756 hMidiIn = 0;
758 if (lphMidiIn != NULL) *lphMidiIn = hMidiIn;
759 TRACE("=> %d hMidi=%p\n", dwRet, hMidiIn);
761 return dwRet;
764 /**************************************************************************
765 * midiInClose [WINMM.@]
767 UINT WINAPI midiInClose(HMIDIIN hMidiIn)
769 LPWINE_MLD wmld;
770 DWORD dwRet;
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);
779 return dwRet;
782 /**************************************************************************
783 * midiInPrepareHeader [WINMM.@]
785 UINT WINAPI midiInPrepareHeader(HMIDIIN hMidiIn,
786 MIDIHDR* lpMidiInHdr, UINT uSize)
788 LPWINE_MLD wmld;
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)
807 LPWINE_MLD wmld;
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)
830 LPWINE_MLD wmld;
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)
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_START, 0L, 0L);
855 /**************************************************************************
856 * midiInStop [WINMM.@]
858 UINT WINAPI midiInStop(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_STOP, 0L, 0L);
870 /**************************************************************************
871 * midiInReset [WINMM.@]
873 UINT WINAPI midiInReset(HMIDIIN hMidiIn)
875 LPWINE_MLD wmld;
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)
890 LPWINE_MLD wmld;
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)
910 LPWINE_MLD wmld;
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;
917 switch (uMessage) {
918 case MIDM_OPEN:
919 case MIDM_CLOSE:
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 {
945 HMIDIOUT hDevice;
946 HANDLE hThread;
947 DWORD dwThreadID;
948 DWORD dwTempo;
949 DWORD dwTimeDiv;
950 DWORD dwPositionMS;
951 DWORD dwPulses;
952 DWORD dwStartTicks;
953 WORD wFlags;
954 HANDLE hEvent;
955 LPMIDIHDR lpMidiHdr;
956 } 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);
970 if (lplpwm)
971 *lplpwm = lpwm;
973 if (lpwm == NULL) {
974 return 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)
987 DWORD ret = 0;
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);
995 } else {
996 ret = (DWORD)((double)pulse * ((double)lpMidiStrm->dwTempo / 1000) /
997 (double)lpMidiStrm->dwTimeDiv);
1000 return ret;
1003 /**************************************************************************
1004 * MMSYSTEM_MidiStream_MessageHandler [internal]
1006 static BOOL MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream* lpMidiStrm, LPWINE_MIDI lpwm, LPMSG msg)
1008 LPMIDIHDR lpMidiHdr;
1009 LPMIDIHDR* lpmh;
1010 LPBYTE lpData;
1011 BOOL paused = FALSE;
1013 for (;;) {
1014 switch (msg->message) {
1015 case WM_QUIT:
1016 return FALSE;
1017 case WINE_MSM_STOP:
1018 TRACE("STOP\n");
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;
1024 while (lpMidiHdr) {
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);
1034 return TRUE;
1035 case WINE_MSM_RESUME:
1036 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1037 lpMidiStrm->dwStartTicks = GetTickCount() - lpMidiStrm->dwPositionMS;
1038 return TRUE;
1039 case WINE_MSM_PAUSE:
1040 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1041 paused = TRUE;
1042 break;
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);
1080 #if 0
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) {
1085 DWORD i;
1086 BYTE ch;
1088 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++)
1089 printf("%02x ", lpData[dwToGo + i]);
1090 for (; i < 16; i++)
1091 printf(" ");
1092 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++) {
1093 ch = lpData[dwToGo + i];
1094 printf("%c", (ch >= 0x20 && ch <= 0x7F) ? ch : '.');
1096 printf("\n");
1098 #endif
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);
1111 break;
1114 lpMidiHdr->lpNext = 0;
1115 for (lpmh = &lpMidiStrm->lpMidiHdr; *lpmh; lpmh = &(*lpmh)->lpNext);
1116 *lpmh = lpMidiHdr;
1117 break;
1118 default:
1119 FIXME("Unknown message %d\n", msg->message);
1120 break;
1122 if (!paused)
1123 return TRUE;
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;
1134 WINE_MIDI* lpwm;
1135 MSG msg;
1136 DWORD dwToGo;
1137 DWORD dwCurrTC;
1138 LPMIDIHDR lpMidiHdr;
1139 DWORD dwOffset;
1141 TRACE("(%p)!\n", lpMidiStrm);
1143 if (!lpMidiStrm ||
1144 (lpwm = (LPWINE_MIDI)MMDRV_Get(lpMidiStrm->hDevice, MMDRV_MIDIOUT, FALSE)) == NULL)
1145 goto the_end;
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);
1158 start_header:
1159 lpMidiHdr = lpMidiStrm->lpMidiHdr;
1160 if (!lpMidiHdr) {
1161 /* for first message, block until one arrives, then process all that are available */
1162 GetMessageA(&msg, 0, 0, 0);
1163 do {
1164 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1165 goto the_end;
1166 } while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE));
1167 goto start_header;
1170 dwOffset = 0;
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))
1187 goto the_end;
1188 /* is lpMidiHdr still current? */
1189 if (lpMidiHdr != lpMidiStrm->lpMidiHdr) {
1190 goto start_header;
1193 } else {
1194 /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
1195 break;
1199 switch (MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK)) {
1200 case MEVT_COMMENT:
1201 FIXME("NIY: MEVT_COMMENT\n");
1202 /* do nothing, skip bytes */
1203 break;
1204 case MEVT_LONGMSG:
1205 midiOutLongMsg(lpMidiStrm->hDevice, lpMidiStrm->lpMidiHdr, MEVT_EVENTPARM(me->dwEvent));
1206 break;
1207 case MEVT_NOP:
1208 break;
1209 case MEVT_SHORTMSG:
1210 midiOutShortMsg(lpMidiStrm->hDevice, MEVT_EVENTPARM(me->dwEvent));
1211 break;
1212 case MEVT_TEMPO:
1213 lpMidiStrm->dwTempo = MEVT_EVENTPARM(me->dwEvent);
1214 break;
1215 case MEVT_VERSION:
1216 break;
1217 default:
1218 FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK));
1219 break;
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);
1240 goto start_header;
1242 the_end:
1243 TRACE("End of thread\n");
1244 return 0;
1247 /**************************************************************************
1248 * midiStreamClose [WINMM.@]
1250 MMRESULT WINAPI midiStreamClose(HMIDISTRM hMidiStrm)
1252 WINE_MIDIStream* lpMidiStrm;
1253 MMRESULT ret = 0;
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);
1266 else {
1267 FIXME("leak from call within function callback\n");
1268 ret = MMSYSERR_HANDLEBUSY; /* yet don't signal it to app */
1270 CloseHandle(lpMidiStrm->hThread);
1272 if(!ret)
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;
1286 MMRESULT ret;
1287 MIDIOPENSTRMID mosm;
1288 LPWINE_MIDI lpwm;
1289 HMIDIOUT hMidiOut;
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)
1299 return ret;
1301 lpMidiStrm = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream));
1302 if (!lpMidiStrm)
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);
1313 if (!lpwm) {
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);
1326 return ret;
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);
1348 return ret;
1351 /**************************************************************************
1352 * midiStreamOut [WINMM.@]
1354 MMRESULT WINAPI midiStreamOut(HMIDISTRM hMidiStrm, LPMIDIHDR lpMidiHdr,
1355 UINT cbMidiHdr)
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;
1377 } else {
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;
1387 return ret;
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);
1403 return ret;
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;
1420 } else {
1421 switch (lpMMT->wType) {
1422 default:
1423 FIXME("Unsupported time type %x\n", lpMMT->wType);
1424 case TIME_BYTES:
1425 case TIME_SAMPLES:
1426 lpMMT->wType = TIME_MS;
1427 /* fall through to alternative format */
1428 case TIME_MS:
1429 lpMMT->u.ms = lpMidiStrm->dwPositionMS;
1430 TRACE("=> %d ms\n", lpMMT->u.ms);
1431 break;
1432 case TIME_TICKS:
1433 lpMMT->u.ticks = lpMidiStrm->dwPulses;
1434 TRACE("=> %d ticks\n", lpMMT->u.ticks);
1435 break;
1438 return ret;
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);
1479 } else {
1480 ret = MMSYSERR_INVALPARAM;
1483 return ret;
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);
1499 return ret;
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);
1515 return ret;
1518 struct mm_starter
1520 LPTASKCALLBACK cb;
1521 DWORD client;
1522 HANDLE event;
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);
1531 mms.cb(mms.client);
1532 if (mms.event) SetEvent(mms.event);
1533 return 0;
1536 /******************************************************************
1537 * mmTaskCreate (WINMM.@)
1539 UINT WINAPI mmTaskCreate(LPTASKCALLBACK cb, HANDLE* ph, DWORD_PTR client)
1541 HANDLE hThread;
1542 HANDLE hEvent = 0;
1543 struct mm_starter *mms;
1545 mms = HeapAlloc(GetProcessHeap(), 0, sizeof(struct mm_starter));
1546 if (mms == NULL) return TASKERR_OUTOFMEMORY;
1548 mms->cb = cb;
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);
1554 if (!hThread) {
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);
1562 return 0;
1565 /******************************************************************
1566 * mmTaskBlock (WINMM.@)
1568 VOID WINAPI mmTaskBlock(DWORD tid)
1570 MSG msg;
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();