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
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
);
82 return psLastEvent
!= NULL
;
85 /******************************************************************
88 const char* WINMM_ErrorToString(MMRESULT error
)
90 #define ERR_TO_STR(dev) case dev: return #dev
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
);
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
);
137 case DLL_PROCESS_ATTACH
:
138 DisableThreadLibraryCalls(hInstDLL
);
140 if (!WINMM_CreateIData(hInstDLL
))
144 case DLL_PROCESS_DETACH
:
148 MCI_SendCommand(MCI_ALL_DEVICE_ID
, MCI_CLOSE
, MCI_WAIT
, 0L);
151 WINMM_DeleteWaveform();
153 CloseHandle(psLastEvent
);
154 DeleteCriticalSection(&WINMM_cs
);
160 /**************************************************************************
161 * WINMM_CheckCallback [internal]
163 MMRESULT
WINMM_CheckCallback(DWORD_PTR dwCallback
, DWORD fdwOpen
, BOOL mixer
)
165 switch (fdwOpen
& CALLBACK_TYPEMASK
) {
166 case CALLBACK_NULL
: /* dwCallback need not be NULL */
168 case CALLBACK_WINDOW
:
169 if (dwCallback
&& !IsWindow((HWND
)dwCallback
))
170 return MMSYSERR_INVALPARAM
;
173 case CALLBACK_FUNCTION
:
174 /* a NULL cb is acceptable since w2k, MMSYSERR_INVALPARAM earlier */
176 return MMSYSERR_INVALFLAG
; /* since w2k, MMSYSERR_NOTSUPPORTED earlier */
178 case CALLBACK_THREAD
:
180 if (mixer
) /* FIXME: mixer supports THREAD+EVENT since w2k */
181 return MMSYSERR_NOTSUPPORTED
; /* w9X */
184 WARN("Unknown callback type %d\n", HIWORD(fdwOpen
));
186 return MMSYSERR_NOERROR
;
189 /**************************************************************************
190 * auxGetNumDevs [WINMM.@]
192 UINT WINAPI
auxGetNumDevs(void)
194 return MMDRV_GetNum(MMDRV_AUX
);
197 /**************************************************************************
198 * auxGetDevCapsW [WINMM.@]
200 UINT WINAPI
auxGetDevCapsW(UINT_PTR uDeviceID
, LPAUXCAPSW lpCaps
, UINT uSize
)
204 TRACE("(%04lX, %p, %d) !\n", uDeviceID
, lpCaps
, uSize
);
206 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
208 if ((wmld
= MMDRV_Get((HANDLE
)uDeviceID
, MMDRV_AUX
, TRUE
)) == NULL
)
209 return MMSYSERR_BADDEVICEID
;
210 return MMDRV_Message(wmld
, AUXDM_GETDEVCAPS
, (DWORD_PTR
)lpCaps
, uSize
);
213 /**************************************************************************
214 * auxGetDevCapsA [WINMM.@]
216 UINT WINAPI
auxGetDevCapsA(UINT_PTR uDeviceID
, LPAUXCAPSA lpCaps
, UINT uSize
)
221 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
223 ret
= auxGetDevCapsW(uDeviceID
, &acW
, sizeof(acW
));
225 if (ret
== MMSYSERR_NOERROR
) {
229 acA
.vDriverVersion
= acW
.vDriverVersion
;
230 WideCharToMultiByte( CP_ACP
, 0, acW
.szPname
, -1, acA
.szPname
,
231 sizeof(acA
.szPname
), NULL
, NULL
);
232 acA
.wTechnology
= acW
.wTechnology
;
233 acA
.wReserved1
= acW
.wReserved1
;
234 acA
.dwSupport
= acW
.dwSupport
;
235 memcpy(lpCaps
, &acA
, min(uSize
, sizeof(acA
)));
240 /**************************************************************************
241 * auxGetVolume [WINMM.@]
243 UINT WINAPI
auxGetVolume(UINT uDeviceID
, DWORD
* lpdwVolume
)
247 TRACE("(%04X, %p) !\n", uDeviceID
, lpdwVolume
);
249 if ((wmld
= MMDRV_Get((HANDLE
)(DWORD_PTR
)uDeviceID
, MMDRV_AUX
, TRUE
)) == NULL
)
250 return MMSYSERR_INVALHANDLE
;
251 return MMDRV_Message(wmld
, AUXDM_GETVOLUME
, (DWORD_PTR
)lpdwVolume
, 0L);
254 /**************************************************************************
255 * auxSetVolume [WINMM.@]
257 UINT WINAPI
auxSetVolume(UINT uDeviceID
, DWORD dwVolume
)
261 TRACE("(%04X, %u) !\n", uDeviceID
, dwVolume
);
263 if ((wmld
= MMDRV_Get((HANDLE
)(DWORD_PTR
)uDeviceID
, MMDRV_AUX
, TRUE
)) == NULL
)
264 return MMSYSERR_INVALHANDLE
;
265 return MMDRV_Message(wmld
, AUXDM_SETVOLUME
, dwVolume
, 0L);
268 /**************************************************************************
269 * auxOutMessage [WINMM.@]
271 UINT WINAPI
auxOutMessage(UINT uDeviceID
, UINT uMessage
, DWORD_PTR dw1
, DWORD_PTR dw2
)
275 if ((wmld
= MMDRV_Get((HANDLE
)(DWORD_PTR
)uDeviceID
, MMDRV_AUX
, TRUE
)) == NULL
)
276 return MMSYSERR_INVALHANDLE
;
278 return MMDRV_Message(wmld
, uMessage
, dw1
, dw2
);
281 /**************************************************************************
282 * midiOutGetNumDevs [WINMM.@]
284 UINT WINAPI
midiOutGetNumDevs(void)
286 return MMDRV_GetNum(MMDRV_MIDIOUT
);
289 /**************************************************************************
290 * midiOutGetDevCapsW [WINMM.@]
292 UINT WINAPI
midiOutGetDevCapsW(UINT_PTR uDeviceID
, LPMIDIOUTCAPSW lpCaps
,
297 TRACE("(%lu, %p, %u);\n", uDeviceID
, lpCaps
, uSize
);
299 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
301 if ((wmld
= MMDRV_Get((HANDLE
)uDeviceID
, MMDRV_MIDIOUT
, TRUE
)) == NULL
)
302 return MMSYSERR_BADDEVICEID
;
304 return MMDRV_Message(wmld
, MODM_GETDEVCAPS
, (DWORD_PTR
)lpCaps
, uSize
);
307 /**************************************************************************
308 * midiOutGetDevCapsA [WINMM.@]
310 UINT WINAPI
midiOutGetDevCapsA(UINT_PTR uDeviceID
, LPMIDIOUTCAPSA lpCaps
,
316 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
318 ret
= midiOutGetDevCapsW(uDeviceID
, &mocW
, sizeof(mocW
));
320 if (ret
== MMSYSERR_NOERROR
) {
322 mocA
.wMid
= mocW
.wMid
;
323 mocA
.wPid
= mocW
.wPid
;
324 mocA
.vDriverVersion
= mocW
.vDriverVersion
;
325 WideCharToMultiByte( CP_ACP
, 0, mocW
.szPname
, -1, mocA
.szPname
,
326 sizeof(mocA
.szPname
), NULL
, NULL
);
327 mocA
.wTechnology
= mocW
.wTechnology
;
328 mocA
.wVoices
= mocW
.wVoices
;
329 mocA
.wNotes
= mocW
.wNotes
;
330 mocA
.wChannelMask
= mocW
.wChannelMask
;
331 mocA
.dwSupport
= mocW
.dwSupport
;
332 memcpy(lpCaps
, &mocA
, min(uSize
, sizeof(mocA
)));
337 /**************************************************************************
338 * midiOutGetErrorTextA [WINMM.@]
339 * midiInGetErrorTextA [WINMM.@]
341 UINT WINAPI
midiOutGetErrorTextA(UINT uError
, LPSTR lpText
, UINT uSize
)
345 if (lpText
== NULL
) ret
= MMSYSERR_INVALPARAM
;
346 else if (uSize
== 0) ret
= MMSYSERR_NOERROR
;
349 LPWSTR xstr
= HeapAlloc(GetProcessHeap(), 0, uSize
* sizeof(WCHAR
));
350 if (!xstr
) ret
= MMSYSERR_NOMEM
;
353 ret
= midiOutGetErrorTextW(uError
, xstr
, uSize
);
354 if (ret
== MMSYSERR_NOERROR
)
355 WideCharToMultiByte(CP_ACP
, 0, xstr
, -1, lpText
, uSize
, NULL
, NULL
);
356 HeapFree(GetProcessHeap(), 0, xstr
);
362 /**************************************************************************
363 * midiOutGetErrorTextW [WINMM.@]
364 * midiInGetErrorTextW [WINMM.@]
366 UINT WINAPI
midiOutGetErrorTextW(UINT uError
, LPWSTR lpText
, UINT uSize
)
368 UINT ret
= MMSYSERR_BADERRNUM
;
370 if (lpText
== NULL
) ret
= MMSYSERR_INVALPARAM
;
371 else if (uSize
== 0) ret
= MMSYSERR_NOERROR
;
373 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
374 * a warning for the test was always true */
375 (/*uError >= MMSYSERR_BASE && */ uError
<= MMSYSERR_LASTERROR
) ||
376 (uError
>= MIDIERR_BASE
&& uError
<= MIDIERR_LASTERROR
)) {
377 if (LoadStringW(hWinMM32Instance
, uError
, lpText
, uSize
) > 0) {
378 ret
= MMSYSERR_NOERROR
;
384 /**************************************************************************
385 * MIDI_OutAlloc [internal]
387 static LPWINE_MIDI
MIDI_OutAlloc(HMIDIOUT
* lphMidiOut
, DWORD_PTR
* lpdwCallback
,
388 DWORD_PTR
* lpdwInstance
, LPDWORD lpdwFlags
,
389 DWORD cIDs
, MIDIOPENSTRMID
* lpIDs
)
395 size
= sizeof(WINE_MIDI
) + (cIDs
? (cIDs
-1) : 0) * sizeof(MIDIOPENSTRMID
);
397 lpwm
= (LPWINE_MIDI
)MMDRV_Alloc(size
, MMDRV_MIDIOUT
, &hMidiOut
, lpdwFlags
,
398 lpdwCallback
, lpdwInstance
);
399 *lphMidiOut
= hMidiOut
;
402 lpwm
->mod
.hMidi
= hMidiOut
;
403 lpwm
->mod
.dwCallback
= *lpdwCallback
;
404 lpwm
->mod
.dwInstance
= *lpdwInstance
;
405 lpwm
->mod
.dnDevNode
= 0;
406 lpwm
->mod
.cIds
= cIDs
;
408 memcpy(&(lpwm
->mod
.rgIds
), lpIDs
, cIDs
* sizeof(MIDIOPENSTRMID
));
413 /**************************************************************************
414 * midiOutOpen [WINMM.@]
416 MMRESULT WINAPI
midiOutOpen(LPHMIDIOUT lphMidiOut
, UINT uDeviceID
,
417 DWORD_PTR dwCallback
, DWORD_PTR dwInstance
, DWORD dwFlags
)
423 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
424 lphMidiOut
, uDeviceID
, dwCallback
, dwInstance
, dwFlags
);
426 if (lphMidiOut
!= NULL
) *lphMidiOut
= 0;
428 dwRet
= WINMM_CheckCallback(dwCallback
, dwFlags
, FALSE
);
429 if (dwRet
!= MMSYSERR_NOERROR
)
432 lpwm
= MIDI_OutAlloc(&hMidiOut
, &dwCallback
, &dwInstance
, &dwFlags
, 0, NULL
);
435 return MMSYSERR_NOMEM
;
437 lpwm
->mld
.uDeviceID
= uDeviceID
;
439 dwRet
= MMDRV_Open((LPWINE_MLD
)lpwm
, MODM_OPEN
, (DWORD_PTR
)&lpwm
->mod
, dwFlags
);
441 if (dwRet
!= MMSYSERR_NOERROR
) {
442 MMDRV_Free(hMidiOut
, (LPWINE_MLD
)lpwm
);
446 if (lphMidiOut
) *lphMidiOut
= hMidiOut
;
447 TRACE("=> %d hMidi=%p\n", dwRet
, hMidiOut
);
452 /**************************************************************************
453 * midiOutClose [WINMM.@]
455 UINT WINAPI
midiOutClose(HMIDIOUT hMidiOut
)
460 TRACE("(%p)\n", hMidiOut
);
462 if ((wmld
= MMDRV_Get(hMidiOut
, MMDRV_MIDIOUT
, FALSE
)) == NULL
)
463 return MMSYSERR_INVALHANDLE
;
465 dwRet
= MMDRV_Close(wmld
, MODM_CLOSE
);
466 MMDRV_Free(hMidiOut
, wmld
);
471 /**************************************************************************
472 * midiOutPrepareHeader [WINMM.@]
474 UINT WINAPI
midiOutPrepareHeader(HMIDIOUT hMidiOut
,
475 MIDIHDR
* lpMidiOutHdr
, UINT uSize
)
479 TRACE("(%p, %p, %d)\n", hMidiOut
, lpMidiOutHdr
, uSize
);
481 if ((wmld
= MMDRV_Get(hMidiOut
, MMDRV_MIDIOUT
, FALSE
)) == NULL
)
482 return MMSYSERR_INVALHANDLE
;
483 /* FIXME: detect MIDIStream handles and enforce 64KB buffer limit on those */
485 return MMDRV_Message(wmld
, MODM_PREPARE
, (DWORD_PTR
)lpMidiOutHdr
, uSize
);
488 /**************************************************************************
489 * midiOutUnprepareHeader [WINMM.@]
491 UINT WINAPI
midiOutUnprepareHeader(HMIDIOUT hMidiOut
,
492 MIDIHDR
* lpMidiOutHdr
, UINT uSize
)
496 TRACE("(%p, %p, %d)\n", hMidiOut
, lpMidiOutHdr
, uSize
);
498 if ((wmld
= MMDRV_Get(hMidiOut
, MMDRV_MIDIOUT
, FALSE
)) == NULL
)
499 return MMSYSERR_INVALHANDLE
;
501 return MMDRV_Message(wmld
, MODM_UNPREPARE
, (DWORD_PTR
)lpMidiOutHdr
, uSize
);
504 /**************************************************************************
505 * midiOutShortMsg [WINMM.@]
507 UINT WINAPI
midiOutShortMsg(HMIDIOUT hMidiOut
, DWORD dwMsg
)
511 TRACE("(%p, %08X)\n", hMidiOut
, dwMsg
);
513 if ((wmld
= MMDRV_Get(hMidiOut
, MMDRV_MIDIOUT
, FALSE
)) == NULL
)
514 return MMSYSERR_INVALHANDLE
;
516 return MMDRV_Message(wmld
, MODM_DATA
, dwMsg
, 0L);
519 /**************************************************************************
520 * midiOutLongMsg [WINMM.@]
522 UINT WINAPI
midiOutLongMsg(HMIDIOUT hMidiOut
,
523 MIDIHDR
* lpMidiOutHdr
, UINT uSize
)
527 TRACE("(%p, %p, %d)\n", hMidiOut
, lpMidiOutHdr
, uSize
);
529 if ((wmld
= MMDRV_Get(hMidiOut
, MMDRV_MIDIOUT
, FALSE
)) == NULL
)
530 return MMSYSERR_INVALHANDLE
;
532 return MMDRV_Message(wmld
, MODM_LONGDATA
, (DWORD_PTR
)lpMidiOutHdr
, uSize
);
535 /**************************************************************************
536 * midiOutReset [WINMM.@]
538 UINT WINAPI
midiOutReset(HMIDIOUT hMidiOut
)
542 TRACE("(%p)\n", hMidiOut
);
544 if ((wmld
= MMDRV_Get(hMidiOut
, MMDRV_MIDIOUT
, FALSE
)) == NULL
)
545 return MMSYSERR_INVALHANDLE
;
547 return MMDRV_Message(wmld
, MODM_RESET
, 0L, 0L);
550 /**************************************************************************
551 * midiOutGetVolume [WINMM.@]
553 UINT WINAPI
midiOutGetVolume(HMIDIOUT hMidiOut
, DWORD
* lpdwVolume
)
557 TRACE("(%p, %p);\n", hMidiOut
, lpdwVolume
);
559 if ((wmld
= MMDRV_Get(hMidiOut
, MMDRV_MIDIOUT
, TRUE
)) == NULL
)
560 return MMSYSERR_INVALHANDLE
;
562 return MMDRV_Message(wmld
, MODM_GETVOLUME
, (DWORD_PTR
)lpdwVolume
, 0L);
565 /**************************************************************************
566 * midiOutSetVolume [WINMM.@]
568 UINT WINAPI
midiOutSetVolume(HMIDIOUT hMidiOut
, DWORD dwVolume
)
572 TRACE("(%p, %d);\n", hMidiOut
, dwVolume
);
574 if ((wmld
= MMDRV_Get(hMidiOut
, MMDRV_MIDIOUT
, TRUE
)) == NULL
)
575 return MMSYSERR_INVALHANDLE
;
577 return MMDRV_Message(wmld
, MODM_SETVOLUME
, dwVolume
, 0L);
580 /**************************************************************************
581 * midiOutCachePatches [WINMM.@]
583 UINT WINAPI
midiOutCachePatches(HMIDIOUT hMidiOut
, UINT uBank
,
584 WORD
* lpwPatchArray
, UINT uFlags
)
586 /* not really necessary to support this */
587 FIXME("(%p, %u, %p, %x): Stub\n", hMidiOut
, uBank
, lpwPatchArray
, uFlags
);
588 return MMSYSERR_NOTSUPPORTED
;
591 /**************************************************************************
592 * midiOutCacheDrumPatches [WINMM.@]
594 UINT WINAPI
midiOutCacheDrumPatches(HMIDIOUT hMidiOut
, UINT uPatch
,
595 WORD
* lpwKeyArray
, UINT uFlags
)
597 FIXME("(%p, %u, %p, %x): Stub\n", hMidiOut
, uPatch
, lpwKeyArray
, uFlags
);
598 return MMSYSERR_NOTSUPPORTED
;
601 /**************************************************************************
602 * midiOutGetID [WINMM.@]
604 UINT WINAPI
midiOutGetID(HMIDIOUT hMidiOut
, UINT
* lpuDeviceID
)
608 TRACE("(%p, %p)\n", hMidiOut
, lpuDeviceID
);
610 if (lpuDeviceID
== NULL
) return MMSYSERR_INVALPARAM
;
611 if ((wmld
= MMDRV_Get(hMidiOut
, MMDRV_MIDIOUT
, FALSE
)) == NULL
)
612 return MMSYSERR_INVALHANDLE
;
614 *lpuDeviceID
= wmld
->uDeviceID
;
615 return MMSYSERR_NOERROR
;
618 /**************************************************************************
619 * midiOutMessage [WINMM.@]
621 UINT WINAPI
midiOutMessage(HMIDIOUT hMidiOut
, UINT uMessage
,
622 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
626 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiOut
, uMessage
, dwParam1
, dwParam2
);
628 if ((wmld
= MMDRV_Get(hMidiOut
, MMDRV_MIDIOUT
, FALSE
)) == NULL
) {
630 if (uMessage
== 0x0001) {
631 *(LPDWORD
)dwParam1
= 1;
634 if ((wmld
= MMDRV_Get(hMidiOut
, MMDRV_MIDIOUT
, TRUE
)) != NULL
) {
635 return MMDRV_PhysicalFeatures(wmld
, uMessage
, dwParam1
, dwParam2
);
637 return MMSYSERR_INVALHANDLE
;
643 FIXME("can't handle OPEN or CLOSE message!\n");
644 return MMSYSERR_NOTSUPPORTED
;
646 return MMDRV_Message(wmld
, uMessage
, dwParam1
, dwParam2
);
649 /**************************************************************************
650 * midiInGetNumDevs [WINMM.@]
652 UINT WINAPI
midiInGetNumDevs(void)
654 return MMDRV_GetNum(MMDRV_MIDIIN
);
657 /**************************************************************************
658 * midiInGetDevCapsW [WINMM.@]
660 UINT WINAPI
midiInGetDevCapsW(UINT_PTR uDeviceID
, LPMIDIINCAPSW lpCaps
, UINT uSize
)
664 TRACE("(%ld, %p, %d);\n", uDeviceID
, lpCaps
, uSize
);
666 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
668 if ((wmld
= MMDRV_Get((HANDLE
)uDeviceID
, MMDRV_MIDIIN
, TRUE
)) == NULL
)
669 return MMSYSERR_BADDEVICEID
;
671 return MMDRV_Message(wmld
, MIDM_GETDEVCAPS
, (DWORD_PTR
)lpCaps
, uSize
);
674 /**************************************************************************
675 * midiInGetDevCapsA [WINMM.@]
677 UINT WINAPI
midiInGetDevCapsA(UINT_PTR uDeviceID
, LPMIDIINCAPSA lpCaps
, UINT uSize
)
682 if (lpCaps
== NULL
) return MMSYSERR_INVALPARAM
;
684 ret
= midiInGetDevCapsW(uDeviceID
, &micW
, sizeof(micW
));
686 if (ret
== MMSYSERR_NOERROR
) {
688 micA
.wMid
= micW
.wMid
;
689 micA
.wPid
= micW
.wPid
;
690 micA
.vDriverVersion
= micW
.vDriverVersion
;
691 WideCharToMultiByte( CP_ACP
, 0, micW
.szPname
, -1, micA
.szPname
,
692 sizeof(micA
.szPname
), NULL
, NULL
);
693 micA
.dwSupport
= micW
.dwSupport
;
694 memcpy(lpCaps
, &micA
, min(uSize
, sizeof(micA
)));
699 /**************************************************************************
700 * midiInOpen [WINMM.@]
702 MMRESULT WINAPI
midiInOpen(HMIDIIN
* lphMidiIn
, UINT uDeviceID
,
703 DWORD_PTR dwCallback
, DWORD_PTR dwInstance
, DWORD dwFlags
)
709 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
710 lphMidiIn
, uDeviceID
, dwCallback
, dwInstance
, dwFlags
);
712 if (lphMidiIn
!= NULL
) *lphMidiIn
= 0;
714 dwRet
= WINMM_CheckCallback(dwCallback
, dwFlags
, FALSE
);
715 if (dwRet
!= MMSYSERR_NOERROR
)
718 lpwm
= (LPWINE_MIDI
)MMDRV_Alloc(sizeof(WINE_MIDI
), MMDRV_MIDIIN
, &hMidiIn
,
719 &dwFlags
, &dwCallback
, &dwInstance
);
722 return MMSYSERR_NOMEM
;
724 lpwm
->mod
.hMidi
= hMidiIn
;
725 lpwm
->mod
.dwCallback
= dwCallback
;
726 lpwm
->mod
.dwInstance
= dwInstance
;
728 lpwm
->mld
.uDeviceID
= uDeviceID
;
729 dwRet
= MMDRV_Open(&lpwm
->mld
, MIDM_OPEN
, (DWORD_PTR
)&lpwm
->mod
, dwFlags
);
731 if (dwRet
!= MMSYSERR_NOERROR
) {
732 MMDRV_Free(hMidiIn
, &lpwm
->mld
);
735 if (lphMidiIn
!= NULL
) *lphMidiIn
= hMidiIn
;
736 TRACE("=> %d hMidi=%p\n", dwRet
, hMidiIn
);
741 /**************************************************************************
742 * midiInClose [WINMM.@]
744 UINT WINAPI
midiInClose(HMIDIIN hMidiIn
)
749 TRACE("(%p)\n", hMidiIn
);
751 if ((wmld
= MMDRV_Get(hMidiIn
, MMDRV_MIDIIN
, FALSE
)) == NULL
)
752 return MMSYSERR_INVALHANDLE
;
754 dwRet
= MMDRV_Close(wmld
, MIDM_CLOSE
);
755 MMDRV_Free(hMidiIn
, wmld
);
759 /**************************************************************************
760 * midiInPrepareHeader [WINMM.@]
762 UINT WINAPI
midiInPrepareHeader(HMIDIIN hMidiIn
,
763 MIDIHDR
* lpMidiInHdr
, UINT uSize
)
767 TRACE("(%p, %p, %d)\n", hMidiIn
, lpMidiInHdr
, uSize
);
769 if ((wmld
= MMDRV_Get(hMidiIn
, MMDRV_MIDIIN
, FALSE
)) == NULL
)
770 return MMSYSERR_INVALHANDLE
;
772 return MMDRV_Message(wmld
, MIDM_PREPARE
, (DWORD_PTR
)lpMidiInHdr
, uSize
);
775 /**************************************************************************
776 * midiInUnprepareHeader [WINMM.@]
778 UINT WINAPI
midiInUnprepareHeader(HMIDIIN hMidiIn
,
779 MIDIHDR
* lpMidiInHdr
, UINT uSize
)
783 TRACE("(%p, %p, %d)\n", hMidiIn
, lpMidiInHdr
, uSize
);
785 if ((wmld
= MMDRV_Get(hMidiIn
, MMDRV_MIDIIN
, FALSE
)) == NULL
)
786 return MMSYSERR_INVALHANDLE
;
788 return MMDRV_Message(wmld
, MIDM_UNPREPARE
, (DWORD_PTR
)lpMidiInHdr
, uSize
);
791 /**************************************************************************
792 * midiInAddBuffer [WINMM.@]
794 UINT WINAPI
midiInAddBuffer(HMIDIIN hMidiIn
,
795 MIDIHDR
* lpMidiInHdr
, UINT uSize
)
799 TRACE("(%p, %p, %d)\n", hMidiIn
, lpMidiInHdr
, uSize
);
801 if ((wmld
= MMDRV_Get(hMidiIn
, MMDRV_MIDIIN
, FALSE
)) == NULL
)
802 return MMSYSERR_INVALHANDLE
;
804 return MMDRV_Message(wmld
, MIDM_ADDBUFFER
, (DWORD_PTR
)lpMidiInHdr
, uSize
);
807 /**************************************************************************
808 * midiInStart [WINMM.@]
810 UINT WINAPI
midiInStart(HMIDIIN hMidiIn
)
814 TRACE("(%p)\n", hMidiIn
);
816 if ((wmld
= MMDRV_Get(hMidiIn
, MMDRV_MIDIIN
, FALSE
)) == NULL
)
817 return MMSYSERR_INVALHANDLE
;
819 return MMDRV_Message(wmld
, MIDM_START
, 0L, 0L);
822 /**************************************************************************
823 * midiInStop [WINMM.@]
825 UINT WINAPI
midiInStop(HMIDIIN hMidiIn
)
829 TRACE("(%p)\n", hMidiIn
);
831 if ((wmld
= MMDRV_Get(hMidiIn
, MMDRV_MIDIIN
, FALSE
)) == NULL
)
832 return MMSYSERR_INVALHANDLE
;
834 return MMDRV_Message(wmld
, MIDM_STOP
, 0L, 0L);
837 /**************************************************************************
838 * midiInReset [WINMM.@]
840 UINT WINAPI
midiInReset(HMIDIIN hMidiIn
)
844 TRACE("(%p)\n", hMidiIn
);
846 if ((wmld
= MMDRV_Get(hMidiIn
, MMDRV_MIDIIN
, FALSE
)) == NULL
)
847 return MMSYSERR_INVALHANDLE
;
849 return MMDRV_Message(wmld
, MIDM_RESET
, 0L, 0L);
852 /**************************************************************************
853 * midiInGetID [WINMM.@]
855 UINT WINAPI
midiInGetID(HMIDIIN hMidiIn
, UINT
* lpuDeviceID
)
859 TRACE("(%p, %p)\n", hMidiIn
, lpuDeviceID
);
861 if (lpuDeviceID
== NULL
) return MMSYSERR_INVALPARAM
;
863 if ((wmld
= MMDRV_Get(hMidiIn
, MMDRV_MIDIIN
, TRUE
)) == NULL
)
864 return MMSYSERR_INVALHANDLE
;
866 *lpuDeviceID
= wmld
->uDeviceID
;
868 return MMSYSERR_NOERROR
;
871 /**************************************************************************
872 * midiInMessage [WINMM.@]
874 UINT WINAPI
midiInMessage(HMIDIIN hMidiIn
, UINT uMessage
,
875 DWORD_PTR dwParam1
, DWORD_PTR dwParam2
)
879 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiIn
, uMessage
, dwParam1
, dwParam2
);
881 if ((wmld
= MMDRV_Get(hMidiIn
, MMDRV_MIDIIN
, FALSE
)) == NULL
)
882 return MMSYSERR_INVALHANDLE
;
887 FIXME("can't handle OPEN or CLOSE message!\n");
888 return MMSYSERR_NOTSUPPORTED
;
890 return MMDRV_Message(wmld
, uMessage
, dwParam1
, dwParam2
);
893 /**************************************************************************
894 * midiConnect [WINMM.@]
896 MMRESULT WINAPI
midiConnect(HMIDI hMidi
, HMIDIOUT hmo
, LPVOID pReserved
)
898 FIXME("(%p, %p, %p): Stub\n", hMidi
, hmo
, pReserved
);
899 return MMSYSERR_ERROR
;
902 /**************************************************************************
903 * midiDisconnect [WINMM.@]
905 MMRESULT WINAPI
midiDisconnect(HMIDI hMidi
, HMIDIOUT hmo
, LPVOID pReserved
)
907 FIXME("(%p, %p, %p): Stub\n", hMidi
, hmo
, pReserved
);
908 return MMSYSERR_ERROR
;
911 typedef struct WINE_MIDIStream
{
915 CRITICAL_SECTION lock
;
918 ULONGLONG position_usec
;
922 DWORD dwLastPositionMS
;
929 #define WINE_MSM_HEADER (WM_USER+0)
930 #define WINE_MSM_STOP (WM_USER+1)
931 #define WINE_MSM_PAUSE (WM_USER+2)
932 #define WINE_MSM_RESUME (WM_USER+3)
934 #define MSM_STATUS_STOPPED WINE_MSM_STOP
935 #define MSM_STATUS_PAUSED WINE_MSM_PAUSE
936 #define MSM_STATUS_PLAYING WINE_MSM_RESUME
938 /**************************************************************************
939 * MMSYSTEM_GetMidiStream [internal]
941 static BOOL
MMSYSTEM_GetMidiStream(HMIDISTRM hMidiStrm
, WINE_MIDIStream
** lpMidiStrm
, WINE_MIDI
** lplpwm
)
943 WINE_MIDI
* lpwm
= (LPWINE_MIDI
)MMDRV_Get(hMidiStrm
, MMDRV_MIDIOUT
, FALSE
);
952 *lpMidiStrm
= (WINE_MIDIStream
*)lpwm
->mod
.rgIds
.dwStreamID
;
954 return *lpMidiStrm
!= NULL
;
957 /**************************************************************************
958 * MMSYSTEM_MidiStream_Convert [internal]
960 static DWORD
MMSYSTEM_MidiStream_Convert(WINE_MIDIStream
* lpMidiStrm
, DWORD pulse
)
964 if (lpMidiStrm
->dwTimeDiv
== 0) {
965 FIXME("Shouldn't happen. lpMidiStrm->dwTimeDiv = 0\n");
966 } else if (lpMidiStrm
->dwTimeDiv
> 0x8000) { /* SMPTE, unchecked FIXME? */
967 int nf
= 256 - HIBYTE(lpMidiStrm
->dwTimeDiv
); /* number of frames */
968 int nsf
= LOBYTE(lpMidiStrm
->dwTimeDiv
); /* number of sub-frames */
969 ret
= (pulse
* 1000000) / (nf
* nsf
);
971 ret
= (DWORD
)((double)pulse
* (double)lpMidiStrm
->dwTempo
/
972 (double)lpMidiStrm
->dwTimeDiv
);
975 return ret
; /* in microseconds */
978 static DWORD
midistream_get_playing_position(WINE_MIDIStream
* lpMidiStrm
)
980 switch (lpMidiStrm
->status
) {
981 case MSM_STATUS_STOPPED
:
982 case MSM_STATUS_PAUSED
:
983 return lpMidiStrm
->dwElapsedMS
;
984 case MSM_STATUS_PLAYING
:
985 return GetTickCount() - lpMidiStrm
->dwStartTicks
;
987 FIXME("Unknown playing status %hu\n", lpMidiStrm
->status
);
992 /**************************************************************************
993 * MMSYSTEM_MidiStream_MessageHandler [internal]
995 static BOOL
MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream
* lpMidiStrm
, LPWINE_MIDI lpwm
, LPMSG msg
)
1002 switch (msg
->message
) {
1007 EnterCriticalSection(&lpMidiStrm
->lock
);
1008 lpMidiStrm
->status
= MSM_STATUS_STOPPED
;
1009 lpMidiStrm
->dwPulses
= 0;
1010 lpMidiStrm
->dwElapsedMS
= 0;
1011 lpMidiStrm
->position_usec
= 0;
1012 lpMidiStrm
->dwLastPositionMS
= 0;
1013 LeaveCriticalSection(&lpMidiStrm
->lock
);
1014 /* this is not quite what MS doc says... */
1015 midiOutReset(lpMidiStrm
->hDevice
);
1016 /* empty list of already submitted buffers */
1017 lpMidiHdr
= lpMidiStrm
->lpMidiHdr
;
1018 lpMidiStrm
->lpMidiHdr
= NULL
;
1020 LPMIDIHDR lphdr
= lpMidiHdr
;
1021 lpMidiHdr
= lpMidiHdr
->lpNext
;
1022 lphdr
->dwFlags
|= MHDR_DONE
;
1023 lphdr
->dwFlags
&= ~MHDR_INQUEUE
;
1025 DriverCallback(lpwm
->mod
.dwCallback
, lpMidiStrm
->wFlags
,
1026 (HDRVR
)lpMidiStrm
->hDevice
, MM_MOM_DONE
,
1027 lpwm
->mod
.dwInstance
, (DWORD_PTR
)lphdr
, 0);
1029 SetEvent((HANDLE
)msg
->wParam
);
1031 case WINE_MSM_RESUME
:
1032 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1033 if (lpMidiStrm
->status
!= MSM_STATUS_PLAYING
) {
1034 EnterCriticalSection(&lpMidiStrm
->lock
);
1035 lpMidiStrm
->dwStartTicks
= GetTickCount() - lpMidiStrm
->dwElapsedMS
;
1036 lpMidiStrm
->status
= MSM_STATUS_PLAYING
;
1037 LeaveCriticalSection(&lpMidiStrm
->lock
);
1039 SetEvent((HANDLE
)msg
->wParam
);
1041 case WINE_MSM_PAUSE
:
1042 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1043 if (lpMidiStrm
->status
!= MSM_STATUS_PAUSED
) {
1044 EnterCriticalSection(&lpMidiStrm
->lock
);
1045 lpMidiStrm
->dwElapsedMS
= GetTickCount() - lpMidiStrm
->dwStartTicks
;
1046 lpMidiStrm
->status
= MSM_STATUS_PAUSED
;
1047 LeaveCriticalSection(&lpMidiStrm
->lock
);
1049 SetEvent((HANDLE
)msg
->wParam
);
1051 /* FIXME(EPP): "I don't understand the content of the first MIDIHDR sent
1052 * by native mcimidi, it doesn't look like a correct one".
1053 * this trick allows us to throw it away... but I don't like it.
1054 * It looks like part of the file I'm trying to play and definitively looks
1055 * like raw midi content.
1056 * I'd really like to understand why native mcimidi sends it. Perhaps a bad
1057 * synchronization issue where native mcimidi is still processing raw MIDI
1058 * content before generating MIDIEVENTs ?
1060 * 4c 04 89 3b 00 81 7c 99 3b 43 00 99 23 5e 04 89 L..;..|.;C..#^..
1061 * 3b 00 00 89 23 00 7c 99 3b 45 00 99 28 62 04 89 ;...#.|.;E..(b..
1062 * 3b 00 00 89 28 00 81 7c 99 3b 4e 00 99 23 5e 04 ;...(..|.;N..#^.
1063 * 89 3b 00 00 89 23 00 7c 99 3b 45 00 99 23 78 04 .;...#.|.;E..#x.
1064 * 89 3b 00 00 89 23 00 81 7c 99 3b 48 00 99 23 5e .;...#..|.;H..#^
1065 * 04 89 3b 00 00 89 23 00 7c 99 3b 4e 00 99 28 62 ..;...#.|.;N..(b
1066 * 04 89 3b 00 00 89 28 00 81 7c 99 39 4c 00 99 23 ..;...(..|.9L..#
1067 * 5e 04 89 39 00 00 89 23 00 82 7c 99 3b 4c 00 99 ^..9...#..|.;L..
1068 * 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 00 99 #^..;...#.|.;H..
1069 * 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b 3f 04 (b..;...(..|.;?.
1070 * 89 3b 00 1c 99 23 5e 04 89 23 00 5c 99 3b 45 00 .;...#^..#.\.;E.
1071 * 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 3b 46 .#x..;...#..|.;F
1072 * 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 ..#^..;...#.|.;H
1073 * 00 99 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b ..(b..;...(..|.;
1074 * 46 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b F..#^..;...#.|.;
1075 * 48 00 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 H..#x..;...#..|.
1076 * 3b 4c 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 ;L..#^..;...#.|.
1078 case WINE_MSM_HEADER
:
1079 /* sets initial tick count for first MIDIHDR */
1080 if (!lpMidiStrm
->dwStartTicks
)
1081 lpMidiStrm
->dwStartTicks
= GetTickCount();
1082 lpMidiHdr
= (LPMIDIHDR
)msg
->lParam
;
1083 lpData
= (LPBYTE
)lpMidiHdr
->lpData
;
1084 TRACE("Adding %s lpMidiHdr=%p [lpData=0x%p dwBytesRecorded=%u/%u dwFlags=0x%08x size=%lu]\n",
1085 (lpMidiHdr
->dwFlags
& MHDR_ISSTRM
) ? "stream" : "regular", lpMidiHdr
,
1086 lpData
, lpMidiHdr
->dwBytesRecorded
, lpMidiHdr
->dwBufferLength
,
1087 lpMidiHdr
->dwFlags
, msg
->wParam
);
1089 /* dumps content of lpMidiHdr->lpData
1090 * FIXME: there should be a debug routine somewhere that already does this
1092 for (dwToGo
= 0; dwToGo
< lpMidiHdr
->dwBufferLength
; dwToGo
+= 16) {
1096 for (i
= 0; i
< min(16, lpMidiHdr
->dwBufferLength
- dwToGo
); i
++)
1097 printf("%02x ", lpData
[dwToGo
+ i
]);
1100 for (i
= 0; i
< min(16, lpMidiHdr
->dwBufferLength
- dwToGo
); i
++) {
1101 ch
= lpData
[dwToGo
+ i
];
1102 printf("%c", (ch
>= 0x20 && ch
<= 0x7F) ? ch
: '.');
1107 if (((LPMIDIEVENT
)lpData
)->dwStreamID
!= 0 &&
1108 ((LPMIDIEVENT
)lpData
)->dwStreamID
!= 0xFFFFFFFF &&
1109 ((LPMIDIEVENT
)lpData
)->dwStreamID
!= (DWORD
)lpMidiStrm
) {
1110 FIXME("Dropping bad %s lpMidiHdr (streamID=%08x)\n",
1111 (lpMidiHdr
->dwFlags
& MHDR_ISSTRM
) ? "stream" : "regular",
1112 ((LPMIDIEVENT
)lpData
)->dwStreamID
);
1113 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
1114 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
1116 DriverCallback(lpwm
->mod
.dwCallback
, lpMidiStrm
->wFlags
,
1117 (HDRVR
)lpMidiStrm
->hDevice
, MM_MOM_DONE
,
1118 lpwm
->mod
.dwInstance
, (DWORD_PTR
)lpMidiHdr
, 0);
1122 lpMidiHdr
->lpNext
= 0;
1123 for (lpmh
= &lpMidiStrm
->lpMidiHdr
; *lpmh
; lpmh
= &(*lpmh
)->lpNext
);
1127 FIXME("Unknown message %d\n", msg
->message
);
1130 if (lpMidiStrm
->status
!= MSM_STATUS_PAUSED
)
1132 GetMessageA(msg
, 0, 0, 0);
1136 /**************************************************************************
1137 * MMSYSTEM_MidiStream_Player [internal]
1139 static DWORD CALLBACK
MMSYSTEM_MidiStream_Player(LPVOID pmt
)
1141 WINE_MIDIStream
* lpMidiStrm
= pmt
;
1146 LPMIDIHDR lpMidiHdr
;
1149 TRACE("(%p)!\n", lpMidiStrm
);
1152 (lpwm
= (LPWINE_MIDI
)MMDRV_Get(lpMidiStrm
->hDevice
, MMDRV_MIDIOUT
, FALSE
)) == NULL
)
1155 /* force thread's queue creation */
1156 PeekMessageA(&msg
, 0, 0, 0, PM_NOREMOVE
);
1158 lpMidiStrm
->dwStartTicks
= 0;
1159 lpMidiStrm
->dwPulses
= 0;
1161 lpMidiStrm
->lpMidiHdr
= 0;
1163 /* midiStreamOpen is waiting for ack */
1164 SetEvent(lpMidiStrm
->hEvent
);
1167 lpMidiHdr
= lpMidiStrm
->lpMidiHdr
;
1169 /* for first message, block until one arrives, then process all that are available */
1170 GetMessageA(&msg
, 0, 0, 0);
1172 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm
, lpwm
, &msg
))
1174 } while (PeekMessageA(&msg
, 0, 0, 0, PM_REMOVE
));
1179 while (dwOffset
+ offsetof(MIDIEVENT
,dwParms
) <= lpMidiHdr
->dwBytesRecorded
) {
1180 LPMIDIEVENT me
= (LPMIDIEVENT
)(lpMidiHdr
->lpData
+dwOffset
);
1182 /* do we have to wait ? */
1183 if (me
->dwDeltaTime
) {
1184 EnterCriticalSection(&lpMidiStrm
->lock
);
1185 lpMidiStrm
->position_usec
+= MMSYSTEM_MidiStream_Convert(lpMidiStrm
, me
->dwDeltaTime
);
1186 LeaveCriticalSection(&lpMidiStrm
->lock
);
1188 dwToGo
= lpMidiStrm
->dwStartTicks
+ lpMidiStrm
->position_usec
/ 1000;
1190 TRACE("%u/%u/%u\n", dwToGo
, GetTickCount(), me
->dwDeltaTime
);
1191 while (dwToGo
- (dwCurrTC
= GetTickCount()) <= MAXLONG
) {
1192 if (MsgWaitForMultipleObjects(0, NULL
, FALSE
, dwToGo
- dwCurrTC
, QS_ALLINPUT
) == WAIT_OBJECT_0
) {
1193 /* got a message, handle it */
1194 while (PeekMessageA(&msg
, 0, 0, 0, PM_REMOVE
)) {
1195 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm
, lpwm
, &msg
))
1197 /* is lpMidiHdr still current? */
1198 if (lpMidiHdr
!= lpMidiStrm
->lpMidiHdr
) {
1202 /* reset dwToGo because dwStartTicks might be updated */
1203 dwToGo
= lpMidiStrm
->dwStartTicks
+ lpMidiStrm
->position_usec
/ 1000;
1205 /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
1209 EnterCriticalSection(&lpMidiStrm
->lock
);
1210 lpMidiStrm
->dwPulses
+= me
->dwDeltaTime
;
1211 lpMidiStrm
->dwLastPositionMS
= midistream_get_playing_position(lpMidiStrm
);
1212 LeaveCriticalSection(&lpMidiStrm
->lock
);
1214 switch (MEVT_EVENTTYPE(me
->dwEvent
& ~MEVT_F_CALLBACK
)) {
1216 FIXME("NIY: MEVT_COMMENT\n");
1217 /* do nothing, skip bytes */
1222 memset(&mh
, 0, sizeof(mh
));
1223 mh
.lpData
= (LPSTR
)me
->dwParms
;
1224 mh
.dwBufferLength
= MEVT_EVENTPARM(me
->dwEvent
);
1225 midiOutPrepareHeader(lpMidiStrm
->hDevice
, &mh
, sizeof(mh
));
1226 midiOutLongMsg(lpMidiStrm
->hDevice
, &mh
, sizeof(mh
));
1227 midiOutUnprepareHeader(lpMidiStrm
->hDevice
, &mh
, sizeof(mh
));
1233 midiOutShortMsg(lpMidiStrm
->hDevice
, MEVT_EVENTPARM(me
->dwEvent
));
1236 EnterCriticalSection(&lpMidiStrm
->lock
);
1237 lpMidiStrm
->dwTempo
= MEVT_EVENTPARM(me
->dwEvent
);
1238 LeaveCriticalSection(&lpMidiStrm
->lock
);
1243 FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me
->dwEvent
& ~MEVT_F_CALLBACK
));
1246 if (me
->dwEvent
& MEVT_F_CALLBACK
) {
1247 /* native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
1248 lpMidiHdr
->dwOffset
= dwOffset
;
1249 DriverCallback(lpwm
->mod
.dwCallback
, lpMidiStrm
->wFlags
,
1250 (HDRVR
)lpMidiStrm
->hDevice
, MM_MOM_POSITIONCB
,
1251 lpwm
->mod
.dwInstance
, (LPARAM
)lpMidiHdr
, 0L);
1253 dwOffset
+= offsetof(MIDIEVENT
,dwParms
);
1254 if (me
->dwEvent
& MEVT_F_LONG
)
1255 dwOffset
+= (MEVT_EVENTPARM(me
->dwEvent
) + 3) & ~3;
1257 /* done with this header */
1258 lpMidiStrm
->lpMidiHdr
= lpMidiHdr
->lpNext
;
1259 lpMidiHdr
->dwFlags
|= MHDR_DONE
;
1260 lpMidiHdr
->dwFlags
&= ~MHDR_INQUEUE
;
1262 DriverCallback(lpwm
->mod
.dwCallback
, lpMidiStrm
->wFlags
,
1263 (HDRVR
)lpMidiStrm
->hDevice
, MM_MOM_DONE
,
1264 lpwm
->mod
.dwInstance
, (DWORD_PTR
)lpMidiHdr
, 0);
1268 TRACE("End of thread\n");
1272 /**************************************************************************
1273 * midiStreamClose [WINMM.@]
1275 MMRESULT WINAPI
midiStreamClose(HMIDISTRM hMidiStrm
)
1278 WINE_MIDIStream
* lpMidiStrm
;
1281 TRACE("(%p)!\n", hMidiStrm
);
1283 if (!MMSYSTEM_GetMidiStream(hMidiStrm
, &lpMidiStrm
, &lpwm
))
1284 return MMSYSERR_INVALHANDLE
;
1286 midiStreamStop(hMidiStrm
);
1287 PostThreadMessageA(lpMidiStrm
->dwThreadID
, WM_QUIT
, 0, 0);
1288 CloseHandle(lpMidiStrm
->hEvent
);
1289 if (lpMidiStrm
->hThread
) {
1290 if (GetCurrentThreadId() != lpMidiStrm
->dwThreadID
)
1291 WaitForSingleObject(lpMidiStrm
->hThread
, INFINITE
);
1293 FIXME("leak from call within function callback\n");
1294 ret
= MMSYSERR_HANDLEBUSY
; /* yet don't signal it to app */
1296 CloseHandle(lpMidiStrm
->hThread
);
1298 DriverCallback(lpwm
->mod
.dwCallback
, lpMidiStrm
->wFlags
,
1299 (HDRVR
)lpMidiStrm
->hDevice
, MM_MOM_CLOSE
,
1300 lpwm
->mod
.dwInstance
, 0, 0);
1302 lpMidiStrm
->lock
.DebugInfo
->Spare
[0] = 0;
1303 DeleteCriticalSection(&lpMidiStrm
->lock
);
1304 HeapFree(GetProcessHeap(), 0, lpMidiStrm
);
1307 return midiOutClose((HMIDIOUT
)hMidiStrm
);
1310 /**************************************************************************
1311 * midiStreamOpen [WINMM.@]
1313 MMRESULT WINAPI
midiStreamOpen(HMIDISTRM
* lphMidiStrm
, LPUINT lpuDeviceID
,
1314 DWORD cMidi
, DWORD_PTR dwCallback
,
1315 DWORD_PTR dwInstance
, DWORD fdwOpen
)
1317 WINE_MIDIStream
* lpMidiStrm
;
1319 MIDIOPENSTRMID mosm
;
1323 TRACE("(%p, %p, %d, 0x%08lx, 0x%08lx, 0x%08x)!\n",
1324 lphMidiStrm
, lpuDeviceID
, cMidi
, dwCallback
, dwInstance
, fdwOpen
);
1326 if (cMidi
!= 1 || lphMidiStrm
== NULL
|| lpuDeviceID
== NULL
)
1327 return MMSYSERR_INVALPARAM
;
1329 ret
= WINMM_CheckCallback(dwCallback
, fdwOpen
, FALSE
);
1330 if (ret
!= MMSYSERR_NOERROR
)
1333 lpMidiStrm
= HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream
));
1335 return MMSYSERR_NOMEM
;
1337 lpMidiStrm
->dwTempo
= 500000; /* microseconds per quarter note, i.e. 120 BPM */
1338 lpMidiStrm
->dwTimeDiv
= 24; /* ticks per quarter note */
1339 lpMidiStrm
->position_usec
= 0;
1340 lpMidiStrm
->dwLastPositionMS
= 0;
1341 lpMidiStrm
->status
= MSM_STATUS_PAUSED
;
1342 lpMidiStrm
->dwElapsedMS
= 0;
1344 mosm
.dwStreamID
= (DWORD
)lpMidiStrm
;
1345 /* FIXME: the correct value is not allocated yet for MAPPER */
1346 mosm
.wDeviceID
= *lpuDeviceID
;
1347 lpwm
= MIDI_OutAlloc(&hMidiOut
, &dwCallback
, &dwInstance
, &fdwOpen
, 1, &mosm
);
1349 HeapFree(GetProcessHeap(), 0, lpMidiStrm
);
1350 return MMSYSERR_NOMEM
;
1352 lpMidiStrm
->hDevice
= hMidiOut
;
1353 *lphMidiStrm
= (HMIDISTRM
)hMidiOut
;
1355 lpwm
->mld
.uDeviceID
= *lpuDeviceID
;
1357 /* don't rely on midiOut callbacks */
1358 ret
= MMDRV_Open(&lpwm
->mld
, MODM_OPEN
, (DWORD_PTR
)&lpwm
->mod
, CALLBACK_NULL
);
1359 if (ret
!= MMSYSERR_NOERROR
) {
1360 MMDRV_Free(hMidiOut
, &lpwm
->mld
);
1361 HeapFree(GetProcessHeap(), 0, lpMidiStrm
);
1365 InitializeCriticalSection(&lpMidiStrm
->lock
);
1366 lpMidiStrm
->lock
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": WINMM_MidiStream.lock");
1368 lpMidiStrm
->hEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1369 lpMidiStrm
->wFlags
= HIWORD(fdwOpen
);
1371 lpMidiStrm
->hThread
= CreateThread(NULL
, 0, MMSYSTEM_MidiStream_Player
,
1372 lpMidiStrm
, 0, &(lpMidiStrm
->dwThreadID
));
1374 if (!lpMidiStrm
->hThread
) {
1375 midiStreamClose((HMIDISTRM
)hMidiOut
);
1376 return MMSYSERR_NOMEM
;
1378 SetThreadPriority(lpMidiStrm
->hThread
, THREAD_PRIORITY_TIME_CRITICAL
);
1380 /* wait for thread to have started, and for its queue to be created */
1381 WaitForSingleObject(lpMidiStrm
->hEvent
, INFINITE
);
1383 TRACE("=> (%u/%d) hMidi=%p ret=%d lpMidiStrm=%p\n",
1384 *lpuDeviceID
, lpwm
->mld
.uDeviceID
, *lphMidiStrm
, ret
, lpMidiStrm
);
1386 DriverCallback(lpwm
->mod
.dwCallback
, lpMidiStrm
->wFlags
,
1387 (HDRVR
)lpMidiStrm
->hDevice
, MM_MOM_OPEN
,
1388 lpwm
->mod
.dwInstance
, 0, 0);
1392 /**************************************************************************
1393 * midiStreamOut [WINMM.@]
1395 MMRESULT WINAPI
midiStreamOut(HMIDISTRM hMidiStrm
, LPMIDIHDR lpMidiHdr
,
1398 WINE_MIDIStream
* lpMidiStrm
;
1399 DWORD ret
= MMSYSERR_NOERROR
;
1401 TRACE("(%p, %p, %u)!\n", hMidiStrm
, lpMidiHdr
, cbMidiHdr
);
1403 if (cbMidiHdr
< offsetof(MIDIHDR
,dwOffset
) || !lpMidiHdr
|| !lpMidiHdr
->lpData
1404 || lpMidiHdr
->dwBufferLength
< lpMidiHdr
->dwBytesRecorded
1405 || lpMidiHdr
->dwBytesRecorded
% 4 /* player expects DWORD padding */)
1406 return MMSYSERR_INVALPARAM
;
1407 /* FIXME: Native additionally checks if the MIDIEVENTs in lpData
1408 * exactly fit dwBytesRecorded. */
1410 if (!(lpMidiHdr
->dwFlags
& MHDR_PREPARED
))
1411 return MIDIERR_UNPREPARED
;
1413 if (lpMidiHdr
->dwFlags
& MHDR_INQUEUE
)
1414 return MIDIERR_STILLPLAYING
;
1416 if (!MMSYSTEM_GetMidiStream(hMidiStrm
, &lpMidiStrm
, NULL
)) {
1417 ret
= MMSYSERR_INVALHANDLE
;
1419 lpMidiHdr
->dwFlags
|= MHDR_ISSTRM
| MHDR_INQUEUE
;
1420 lpMidiHdr
->dwFlags
&= ~MHDR_DONE
;
1421 if (!PostThreadMessageA(lpMidiStrm
->dwThreadID
,
1422 WINE_MSM_HEADER
, cbMidiHdr
,
1423 (LPARAM
)lpMidiHdr
)) {
1424 ERR("bad PostThreadMessageA\n");
1425 ret
= MMSYSERR_ERROR
;
1431 static MMRESULT
midistream_post_message_and_wait(WINE_MIDIStream
* lpMidiStrm
, UINT msg
, LPARAM lParam
)
1435 hObjects
[0] = CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1437 return MMSYSERR_ERROR
;
1439 if (!PostThreadMessageA(lpMidiStrm
->dwThreadID
, msg
, (WPARAM
)hObjects
[0], lParam
)) {
1440 WARN("bad PostThreadMessage\n");
1441 CloseHandle(hObjects
[0]);
1442 return MMSYSERR_ERROR
;
1445 if (GetCurrentThreadId() != lpMidiStrm
->dwThreadID
) {
1447 hObjects
[1] = lpMidiStrm
->hThread
;
1448 ret
= WaitForMultipleObjects(ARRAY_SIZE(hObjects
), hObjects
, FALSE
, INFINITE
);
1449 if (ret
!= WAIT_OBJECT_0
) {
1450 CloseHandle(hObjects
[0]);
1451 WARN("bad WaitForSingleObject (%u)\n", ret
);
1452 return MMSYSERR_ERROR
;
1456 CloseHandle(hObjects
[0]);
1458 return MMSYSERR_NOERROR
;
1461 /**************************************************************************
1462 * midiStreamPause [WINMM.@]
1464 MMRESULT WINAPI
midiStreamPause(HMIDISTRM hMidiStrm
)
1466 WINE_MIDIStream
* lpMidiStrm
;
1468 TRACE("(%p)!\n", hMidiStrm
);
1470 if (!MMSYSTEM_GetMidiStream(hMidiStrm
, &lpMidiStrm
, NULL
))
1471 return MMSYSERR_INVALHANDLE
;
1472 return midistream_post_message_and_wait(lpMidiStrm
, WINE_MSM_PAUSE
, 0);
1475 static DWORD
midistream_get_current_pulse(WINE_MIDIStream
* lpMidiStrm
)
1478 DWORD now
= midistream_get_playing_position(lpMidiStrm
);
1479 DWORD delta
= now
- lpMidiStrm
->dwLastPositionMS
;
1480 if (lpMidiStrm
->dwTimeDiv
> 0x8000) {
1481 /* SMPTE, unchecked FIXME */
1482 BYTE nf
= 256 - HIBYTE(lpMidiStrm
->dwTimeDiv
);
1483 BYTE nsf
= LOBYTE(lpMidiStrm
->dwTimeDiv
);
1484 pulses
= (delta
* nf
* nsf
) / 1000;
1486 else if (lpMidiStrm
->dwTimeDiv
) {
1487 pulses
= (DWORD
)((double)(delta
* lpMidiStrm
->dwTimeDiv
) *
1488 1000.0 / (double)lpMidiStrm
->dwTempo
);
1490 return lpMidiStrm
->dwPulses
+ pulses
;
1493 /**************************************************************************
1494 * midiStreamPosition [WINMM.@]
1496 MMRESULT WINAPI
midiStreamPosition(HMIDISTRM hMidiStrm
, LPMMTIME lpMMT
, UINT cbmmt
)
1498 WINE_MIDIStream
* lpMidiStrm
;
1499 DWORD ret
= MMSYSERR_NOERROR
;
1501 TRACE("(%p, %p, %u)!\n", hMidiStrm
, lpMMT
, cbmmt
);
1503 if (!MMSYSTEM_GetMidiStream(hMidiStrm
, &lpMidiStrm
, NULL
)) {
1504 ret
= MMSYSERR_INVALHANDLE
;
1505 } else if (lpMMT
== NULL
|| cbmmt
!= sizeof(MMTIME
)) {
1506 ret
= MMSYSERR_INVALPARAM
;
1508 EnterCriticalSection(&lpMidiStrm
->lock
);
1509 switch (lpMMT
->wType
) {
1511 if (lpMidiStrm
->dwTimeDiv
< 0x8000) {
1513 tdiv
= (lpMidiStrm
->dwTimeDiv
> 24) ? lpMidiStrm
->dwTimeDiv
: 24;
1514 pulses
= midistream_get_current_pulse(lpMidiStrm
);
1515 lpMMT
->u
.midi
.songptrpos
= (pulses
+ tdiv
/8) / (tdiv
/4);
1516 if (!lpMMT
->u
.midi
.songptrpos
&& pulses
) lpMMT
->u
.midi
.songptrpos
++;
1517 TRACE("=> song position %d (pulses %u, tdiv %u)\n", lpMMT
->u
.midi
.songptrpos
, pulses
, tdiv
);
1523 lpMMT
->wType
= TIME_MS
;
1524 /* fall through to alternative format */
1526 lpMMT
->u
.ms
= midistream_get_playing_position(lpMidiStrm
);
1527 TRACE("=> %d ms\n", lpMMT
->u
.ms
);
1530 lpMMT
->u
.ticks
= midistream_get_current_pulse(lpMidiStrm
);
1531 TRACE("=> %d ticks\n", lpMMT
->u
.ticks
);
1534 FIXME("Unsupported time type %x\n", lpMMT
->wType
);
1535 /* use TIME_MS instead */
1536 lpMMT
->wType
= TIME_MS
;
1537 lpMMT
->u
.ms
= midistream_get_playing_position(lpMidiStrm
);
1538 TRACE("=> %d ms\n", lpMMT
->u
.ms
);
1541 LeaveCriticalSection(&lpMidiStrm
->lock
);
1546 /**************************************************************************
1547 * midiStreamProperty [WINMM.@]
1549 MMRESULT WINAPI
midiStreamProperty(HMIDISTRM hMidiStrm
, LPBYTE lpPropData
, DWORD dwProperty
)
1551 WINE_MIDIStream
* lpMidiStrm
;
1552 MMRESULT ret
= MMSYSERR_NOERROR
;
1554 TRACE("(%p, %p, %x)\n", hMidiStrm
, lpPropData
, dwProperty
);
1556 if (!MMSYSTEM_GetMidiStream(hMidiStrm
, &lpMidiStrm
, NULL
)) {
1557 ret
= MMSYSERR_INVALHANDLE
;
1558 } else if ((dwProperty
& (MIDIPROP_GET
|MIDIPROP_SET
)) == 0) {
1559 ret
= MMSYSERR_INVALPARAM
;
1560 } else if (dwProperty
& MIDIPROP_TEMPO
) {
1561 MIDIPROPTEMPO
* mpt
= (MIDIPROPTEMPO
*)lpPropData
;
1563 EnterCriticalSection(&lpMidiStrm
->lock
);
1564 if (sizeof(MIDIPROPTEMPO
) != mpt
->cbStruct
) {
1565 ret
= MMSYSERR_INVALPARAM
;
1566 } else if (dwProperty
& MIDIPROP_SET
) {
1567 lpMidiStrm
->dwTempo
= mpt
->dwTempo
;
1568 TRACE("Setting tempo to %d\n", mpt
->dwTempo
);
1569 } else if (dwProperty
& MIDIPROP_GET
) {
1570 mpt
->dwTempo
= lpMidiStrm
->dwTempo
;
1571 TRACE("Getting tempo <= %d\n", mpt
->dwTempo
);
1573 LeaveCriticalSection(&lpMidiStrm
->lock
);
1574 } else if (dwProperty
& MIDIPROP_TIMEDIV
) {
1575 MIDIPROPTIMEDIV
* mptd
= (MIDIPROPTIMEDIV
*)lpPropData
;
1577 if (sizeof(MIDIPROPTIMEDIV
) != mptd
->cbStruct
) {
1578 ret
= MMSYSERR_INVALPARAM
;
1579 } else if (dwProperty
& MIDIPROP_SET
) {
1580 EnterCriticalSection(&lpMidiStrm
->lock
);
1581 if (lpMidiStrm
->status
!= MSM_STATUS_PLAYING
) {
1582 lpMidiStrm
->dwTimeDiv
= mptd
->dwTimeDiv
;
1583 TRACE("Setting time div to %d\n", mptd
->dwTimeDiv
);
1586 ret
= MMSYSERR_INVALPARAM
;
1587 LeaveCriticalSection(&lpMidiStrm
->lock
);
1588 } else if (dwProperty
& MIDIPROP_GET
) {
1589 mptd
->dwTimeDiv
= lpMidiStrm
->dwTimeDiv
;
1590 TRACE("Getting time div <= %d\n", mptd
->dwTimeDiv
);
1593 ret
= MMSYSERR_INVALPARAM
;
1599 /**************************************************************************
1600 * midiStreamRestart [WINMM.@]
1602 MMRESULT WINAPI
midiStreamRestart(HMIDISTRM hMidiStrm
)
1604 WINE_MIDIStream
* lpMidiStrm
;
1606 TRACE("(%p)!\n", hMidiStrm
);
1608 if (!MMSYSTEM_GetMidiStream(hMidiStrm
, &lpMidiStrm
, NULL
))
1609 return MMSYSERR_INVALHANDLE
;
1610 return midistream_post_message_and_wait(lpMidiStrm
, WINE_MSM_RESUME
, 0);
1613 /**************************************************************************
1614 * midiStreamStop [WINMM.@]
1616 MMRESULT WINAPI
midiStreamStop(HMIDISTRM hMidiStrm
)
1618 WINE_MIDIStream
* lpMidiStrm
;
1620 TRACE("(%p)!\n", hMidiStrm
);
1622 if (!MMSYSTEM_GetMidiStream(hMidiStrm
, &lpMidiStrm
, NULL
))
1623 return MMSYSERR_INVALHANDLE
;
1624 return midistream_post_message_and_wait(lpMidiStrm
, WINE_MSM_STOP
, 0);
1634 static DWORD WINAPI
mmTaskRun(void* pmt
)
1636 struct mm_starter mms
;
1638 memcpy(&mms
, pmt
, sizeof(struct mm_starter
));
1639 HeapFree(GetProcessHeap(), 0, pmt
);
1641 if (mms
.event
) SetEvent(mms
.event
);
1645 /******************************************************************
1646 * mmTaskCreate (WINMM.@)
1648 UINT WINAPI
mmTaskCreate(LPTASKCALLBACK cb
, HANDLE
* ph
, DWORD_PTR client
)
1652 struct mm_starter
*mms
;
1654 mms
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct mm_starter
));
1655 if (mms
== NULL
) return TASKERR_OUTOFMEMORY
;
1658 mms
->client
= client
;
1659 if (ph
) hEvent
= CreateEventW(NULL
, FALSE
, FALSE
, NULL
);
1660 mms
->event
= hEvent
;
1662 hThread
= CreateThread(0, 0, mmTaskRun
, mms
, 0, NULL
);
1664 HeapFree(GetProcessHeap(), 0, mms
);
1665 if (hEvent
) CloseHandle(hEvent
);
1666 return TASKERR_OUTOFMEMORY
;
1668 SetThreadPriority(hThread
, THREAD_PRIORITY_TIME_CRITICAL
);
1669 if (ph
) *ph
= hEvent
;
1670 CloseHandle(hThread
);
1674 /******************************************************************
1675 * mmTaskBlock (WINMM.@)
1677 VOID WINAPI
mmTaskBlock(DWORD tid
)
1683 GetMessageA(&msg
, 0, 0, 0);
1684 if (msg
.hwnd
) DispatchMessageA(&msg
);
1685 } while (msg
.message
!= WM_USER
);
1688 /******************************************************************
1689 * mmTaskSignal (WINMM.@)
1691 BOOL WINAPI
mmTaskSignal(DWORD tid
)
1693 return PostThreadMessageW(tid
, WM_USER
, 0, 0);
1696 /******************************************************************
1697 * mmTaskYield (WINMM.@)
1699 VOID WINAPI
mmTaskYield(VOID
) {}
1701 /******************************************************************
1702 * mmGetCurrentTask (WINMM.@)
1704 DWORD WINAPI
mmGetCurrentTask(VOID
)
1706 return GetCurrentThreadId();