comctl32/tests: Use BOOL type where appropriate.
[wine.git] / dlls / winmm / winmm.c
blob13499ada0853e769f331dc8878ab266a06f25f49
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * WINMM functions
6 * Copyright 1993 Martin Ayotte
7 * 1998-2002 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * Eric POUECH :
26 * 98/9 added Win32 MCI support
27 * 99/4 added midiStream support
28 * 99/9 added support for loadable low level drivers
31 /* TODO
32 * + it seems that some programs check what's installed in
33 * registry against the value returned by drivers. Wine is
34 * currently broken regarding this point.
35 * + check thread-safeness for MMSYSTEM and WINMM entry points
36 * + unicode entry points are badly supported (would require
37 * moving 32 bit drivers as Unicode as they are supposed to be)
38 * + allow joystick and timer external calls as we do for wave,
39 * midi, mixer and aux
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <string.h>
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
48 #include "windef.h"
49 #include "winbase.h"
50 #include "mmsystem.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "winternl.h"
54 #include "winemm.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
60 /* ========================================================================
61 * G L O B A L S E T T I N G S
62 * ========================================================================*/
64 HINSTANCE hWinMM32Instance;
65 HANDLE psLastEvent;
67 static CRITICAL_SECTION_DEBUG critsect_debug =
69 0, 0, &WINMM_cs,
70 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
71 0, 0, { (DWORD_PTR)(__FILE__ ": WINMM_cs") }
73 CRITICAL_SECTION WINMM_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
75 /**************************************************************************
76 * WINMM_CreateIData [internal]
78 static BOOL WINMM_CreateIData(HINSTANCE hInstDLL)
80 hWinMM32Instance = hInstDLL;
81 psLastEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
82 return TRUE;
85 /******************************************************************
86 * WINMM_ErrorToString
88 const char* WINMM_ErrorToString(MMRESULT error)
90 #define ERR_TO_STR(dev) case dev: return #dev
91 switch (error) {
92 ERR_TO_STR(MMSYSERR_NOERROR);
93 ERR_TO_STR(MMSYSERR_ERROR);
94 ERR_TO_STR(MMSYSERR_BADDEVICEID);
95 ERR_TO_STR(MMSYSERR_NOTENABLED);
96 ERR_TO_STR(MMSYSERR_ALLOCATED);
97 ERR_TO_STR(MMSYSERR_INVALHANDLE);
98 ERR_TO_STR(MMSYSERR_NODRIVER);
99 ERR_TO_STR(MMSYSERR_NOMEM);
100 ERR_TO_STR(MMSYSERR_NOTSUPPORTED);
101 ERR_TO_STR(MMSYSERR_BADERRNUM);
102 ERR_TO_STR(MMSYSERR_INVALFLAG);
103 ERR_TO_STR(MMSYSERR_INVALPARAM);
104 ERR_TO_STR(MMSYSERR_HANDLEBUSY);
105 ERR_TO_STR(MMSYSERR_INVALIDALIAS);
106 ERR_TO_STR(MMSYSERR_BADDB);
107 ERR_TO_STR(MMSYSERR_KEYNOTFOUND);
108 ERR_TO_STR(MMSYSERR_READERROR);
109 ERR_TO_STR(MMSYSERR_WRITEERROR);
110 ERR_TO_STR(MMSYSERR_DELETEERROR);
111 ERR_TO_STR(MMSYSERR_VALNOTFOUND);
112 ERR_TO_STR(MMSYSERR_NODRIVERCB);
113 ERR_TO_STR(WAVERR_BADFORMAT);
114 ERR_TO_STR(WAVERR_STILLPLAYING);
115 ERR_TO_STR(WAVERR_UNPREPARED);
116 ERR_TO_STR(WAVERR_SYNC);
117 ERR_TO_STR(MIDIERR_INVALIDSETUP);
118 ERR_TO_STR(MIDIERR_NODEVICE);
119 ERR_TO_STR(MIDIERR_STILLPLAYING);
120 ERR_TO_STR(MIDIERR_UNPREPARED);
122 #undef ERR_TO_STR
123 return wine_dbg_sprintf("Unknown(0x%08x)", error);
126 /**************************************************************************
127 * DllMain (WINMM.init)
129 * WINMM DLL entry point
132 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
134 TRACE("%p 0x%x %p\n", hInstDLL, fdwReason, fImpLoad);
136 switch (fdwReason) {
137 case DLL_PROCESS_ATTACH:
138 DisableThreadLibraryCalls(hInstDLL);
140 if (!WINMM_CreateIData(hInstDLL))
141 return FALSE;
142 break;
143 case DLL_PROCESS_DETACH:
144 if(fImpLoad)
145 break;
147 MCI_SendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0L);
148 MMDRV_Exit();
149 DRIVER_UnloadAll();
150 WINMM_DeleteWaveform();
151 TIME_MMTimeStop();
152 CloseHandle(psLastEvent);
153 DeleteCriticalSection(&WINMM_cs);
154 break;
156 return TRUE;
159 /**************************************************************************
160 * WINMM_CheckCallback [internal]
162 MMRESULT WINMM_CheckCallback(DWORD_PTR dwCallback, DWORD fdwOpen, BOOL mixer)
164 switch (fdwOpen & CALLBACK_TYPEMASK) {
165 case CALLBACK_NULL: /* dwCallback need not be NULL */
166 break;
167 case CALLBACK_WINDOW:
168 if (dwCallback && !IsWindow((HWND)dwCallback))
169 return MMSYSERR_INVALPARAM;
170 break;
172 case CALLBACK_FUNCTION:
173 /* a NULL cb is acceptable since w2k, MMSYSERR_INVALPARAM earlier */
174 if (mixer)
175 return MMSYSERR_INVALFLAG; /* since w2k, MMSYSERR_NOTSUPPORTED earlier */
176 break;
177 case CALLBACK_THREAD:
178 case CALLBACK_EVENT:
179 if (mixer) /* FIXME: mixer supports THREAD+EVENT since w2k */
180 return MMSYSERR_NOTSUPPORTED; /* w9X */
181 break;
182 default:
183 WARN("Unknown callback type %d\n", HIWORD(fdwOpen));
185 return MMSYSERR_NOERROR;
188 /**************************************************************************
189 * auxGetNumDevs [WINMM.@]
191 UINT WINAPI auxGetNumDevs(void)
193 return MMDRV_GetNum(MMDRV_AUX);
196 /**************************************************************************
197 * auxGetDevCapsW [WINMM.@]
199 UINT WINAPI auxGetDevCapsW(UINT_PTR uDeviceID, LPAUXCAPSW lpCaps, UINT uSize)
201 LPWINE_MLD wmld;
203 TRACE("(%04lX, %p, %d) !\n", uDeviceID, lpCaps, uSize);
205 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
207 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
208 return MMSYSERR_BADDEVICEID;
209 return MMDRV_Message(wmld, AUXDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
212 /**************************************************************************
213 * auxGetDevCapsA [WINMM.@]
215 UINT WINAPI auxGetDevCapsA(UINT_PTR uDeviceID, LPAUXCAPSA lpCaps, UINT uSize)
217 AUXCAPSW acW;
218 UINT ret;
220 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
222 ret = auxGetDevCapsW(uDeviceID, &acW, sizeof(acW));
224 if (ret == MMSYSERR_NOERROR) {
225 AUXCAPSA acA;
226 acA.wMid = acW.wMid;
227 acA.wPid = acW.wPid;
228 acA.vDriverVersion = acW.vDriverVersion;
229 WideCharToMultiByte( CP_ACP, 0, acW.szPname, -1, acA.szPname,
230 sizeof(acA.szPname), NULL, NULL );
231 acA.wTechnology = acW.wTechnology;
232 acA.dwSupport = acW.dwSupport;
233 memcpy(lpCaps, &acA, min(uSize, sizeof(acA)));
235 return ret;
238 /**************************************************************************
239 * auxGetVolume [WINMM.@]
241 UINT WINAPI auxGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
243 LPWINE_MLD wmld;
245 TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);
247 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
248 return MMSYSERR_INVALHANDLE;
249 return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
252 /**************************************************************************
253 * auxSetVolume [WINMM.@]
255 UINT WINAPI auxSetVolume(UINT uDeviceID, DWORD dwVolume)
257 LPWINE_MLD wmld;
259 TRACE("(%04X, %u) !\n", uDeviceID, dwVolume);
261 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
262 return MMSYSERR_INVALHANDLE;
263 return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L);
266 /**************************************************************************
267 * auxOutMessage [WINMM.@]
269 UINT WINAPI auxOutMessage(UINT uDeviceID, UINT uMessage, DWORD_PTR dw1, DWORD_PTR dw2)
271 LPWINE_MLD wmld;
273 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
274 return MMSYSERR_INVALHANDLE;
276 return MMDRV_Message(wmld, uMessage, dw1, dw2);
279 /**************************************************************************
280 * midiOutGetNumDevs [WINMM.@]
282 UINT WINAPI midiOutGetNumDevs(void)
284 return MMDRV_GetNum(MMDRV_MIDIOUT);
287 /**************************************************************************
288 * midiOutGetDevCapsW [WINMM.@]
290 UINT WINAPI midiOutGetDevCapsW(UINT_PTR uDeviceID, LPMIDIOUTCAPSW lpCaps,
291 UINT uSize)
293 LPWINE_MLD wmld;
295 TRACE("(%lu, %p, %u);\n", uDeviceID, lpCaps, uSize);
297 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
299 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL)
300 return MMSYSERR_BADDEVICEID;
302 return MMDRV_Message(wmld, MODM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
305 /**************************************************************************
306 * midiOutGetDevCapsA [WINMM.@]
308 UINT WINAPI midiOutGetDevCapsA(UINT_PTR uDeviceID, LPMIDIOUTCAPSA lpCaps,
309 UINT uSize)
311 MIDIOUTCAPSW mocW;
312 UINT ret;
314 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
316 ret = midiOutGetDevCapsW(uDeviceID, &mocW, sizeof(mocW));
318 if (ret == MMSYSERR_NOERROR) {
319 MIDIOUTCAPSA mocA;
320 mocA.wMid = mocW.wMid;
321 mocA.wPid = mocW.wPid;
322 mocA.vDriverVersion = mocW.vDriverVersion;
323 WideCharToMultiByte( CP_ACP, 0, mocW.szPname, -1, mocA.szPname,
324 sizeof(mocA.szPname), NULL, NULL );
325 mocA.wTechnology = mocW.wTechnology;
326 mocA.wVoices = mocW.wVoices;
327 mocA.wNotes = mocW.wNotes;
328 mocA.wChannelMask = mocW.wChannelMask;
329 mocA.dwSupport = mocW.dwSupport;
330 memcpy(lpCaps, &mocA, min(uSize, sizeof(mocA)));
332 return ret;
335 /**************************************************************************
336 * midiOutGetErrorTextA [WINMM.@]
337 * midiInGetErrorTextA [WINMM.@]
339 UINT WINAPI midiOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
341 UINT ret;
343 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
344 else if (uSize == 0) ret = MMSYSERR_NOERROR;
345 else
347 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
348 if (!xstr) ret = MMSYSERR_NOMEM;
349 else
351 ret = midiOutGetErrorTextW(uError, xstr, uSize);
352 if (ret == MMSYSERR_NOERROR)
353 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
354 HeapFree(GetProcessHeap(), 0, xstr);
357 return ret;
360 /**************************************************************************
361 * midiOutGetErrorTextW [WINMM.@]
362 * midiInGetErrorTextW [WINMM.@]
364 UINT WINAPI midiOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
366 UINT ret = MMSYSERR_BADERRNUM;
368 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
369 else if (uSize == 0) ret = MMSYSERR_NOERROR;
370 else if (
371 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
372 * a warning for the test was always true */
373 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
374 (uError >= MIDIERR_BASE && uError <= MIDIERR_LASTERROR)) {
375 if (LoadStringW(hWinMM32Instance, uError, lpText, uSize) > 0) {
376 ret = MMSYSERR_NOERROR;
379 return ret;
382 /**************************************************************************
383 * MIDI_OutAlloc [internal]
385 static LPWINE_MIDI MIDI_OutAlloc(HMIDIOUT* lphMidiOut, DWORD_PTR* lpdwCallback,
386 DWORD_PTR* lpdwInstance, LPDWORD lpdwFlags,
387 DWORD cIDs, MIDIOPENSTRMID* lpIDs)
389 HANDLE hMidiOut;
390 LPWINE_MIDI lpwm;
391 UINT size;
393 size = sizeof(WINE_MIDI) + (cIDs ? (cIDs-1) : 0) * sizeof(MIDIOPENSTRMID);
395 lpwm = (LPWINE_MIDI)MMDRV_Alloc(size, MMDRV_MIDIOUT, &hMidiOut, lpdwFlags,
396 lpdwCallback, lpdwInstance);
397 *lphMidiOut = hMidiOut;
399 if (lpwm) {
400 lpwm->mod.hMidi = hMidiOut;
401 lpwm->mod.dwCallback = *lpdwCallback;
402 lpwm->mod.dwInstance = *lpdwInstance;
403 lpwm->mod.dnDevNode = 0;
404 lpwm->mod.cIds = cIDs;
405 if (cIDs)
406 memcpy(&(lpwm->mod.rgIds), lpIDs, cIDs * sizeof(MIDIOPENSTRMID));
408 return lpwm;
411 /**************************************************************************
412 * midiOutOpen [WINMM.@]
414 UINT WINAPI midiOutOpen(LPHMIDIOUT lphMidiOut, UINT uDeviceID,
415 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
417 HMIDIOUT hMidiOut;
418 LPWINE_MIDI lpwm;
419 UINT dwRet;
421 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
422 lphMidiOut, uDeviceID, dwCallback, dwInstance, dwFlags);
424 if (lphMidiOut != NULL) *lphMidiOut = 0;
426 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
427 if (dwRet != MMSYSERR_NOERROR)
428 return dwRet;
430 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &dwFlags, 0, NULL);
432 if (lpwm == NULL)
433 return MMSYSERR_NOMEM;
435 lpwm->mld.uDeviceID = uDeviceID;
437 dwRet = MMDRV_Open((LPWINE_MLD)lpwm, MODM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
439 if (dwRet != MMSYSERR_NOERROR) {
440 MMDRV_Free(hMidiOut, (LPWINE_MLD)lpwm);
441 hMidiOut = 0;
444 if (lphMidiOut) *lphMidiOut = hMidiOut;
445 TRACE("=> %d hMidi=%p\n", dwRet, hMidiOut);
447 return dwRet;
450 /**************************************************************************
451 * midiOutClose [WINMM.@]
453 UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
455 LPWINE_MLD wmld;
456 DWORD dwRet;
458 TRACE("(%p)\n", hMidiOut);
460 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
461 return MMSYSERR_INVALHANDLE;
463 dwRet = MMDRV_Close(wmld, MODM_CLOSE);
464 MMDRV_Free(hMidiOut, wmld);
466 return dwRet;
469 /**************************************************************************
470 * midiOutPrepareHeader [WINMM.@]
472 UINT WINAPI midiOutPrepareHeader(HMIDIOUT hMidiOut,
473 MIDIHDR* lpMidiOutHdr, UINT uSize)
475 LPWINE_MLD wmld;
477 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
479 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
480 return MMSYSERR_INVALHANDLE;
481 /* FIXME: detect MIDIStream handles and enforce 64KB buffer limit on those */
483 return MMDRV_Message(wmld, MODM_PREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
486 /**************************************************************************
487 * midiOutUnprepareHeader [WINMM.@]
489 UINT WINAPI midiOutUnprepareHeader(HMIDIOUT hMidiOut,
490 MIDIHDR* lpMidiOutHdr, UINT uSize)
492 LPWINE_MLD wmld;
494 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
496 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
497 return MMSYSERR_INVALHANDLE;
499 return MMDRV_Message(wmld, MODM_UNPREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
502 /**************************************************************************
503 * midiOutShortMsg [WINMM.@]
505 UINT WINAPI midiOutShortMsg(HMIDIOUT hMidiOut, DWORD dwMsg)
507 LPWINE_MLD wmld;
509 TRACE("(%p, %08X)\n", hMidiOut, dwMsg);
511 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
512 return MMSYSERR_INVALHANDLE;
514 return MMDRV_Message(wmld, MODM_DATA, dwMsg, 0L);
517 /**************************************************************************
518 * midiOutLongMsg [WINMM.@]
520 UINT WINAPI midiOutLongMsg(HMIDIOUT hMidiOut,
521 MIDIHDR* lpMidiOutHdr, UINT uSize)
523 LPWINE_MLD wmld;
525 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
527 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
528 return MMSYSERR_INVALHANDLE;
530 return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD_PTR)lpMidiOutHdr, uSize);
533 /**************************************************************************
534 * midiOutReset [WINMM.@]
536 UINT WINAPI midiOutReset(HMIDIOUT hMidiOut)
538 LPWINE_MLD wmld;
540 TRACE("(%p)\n", hMidiOut);
542 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
543 return MMSYSERR_INVALHANDLE;
545 return MMDRV_Message(wmld, MODM_RESET, 0L, 0L);
548 /**************************************************************************
549 * midiOutGetVolume [WINMM.@]
551 UINT WINAPI midiOutGetVolume(HMIDIOUT hMidiOut, DWORD* lpdwVolume)
553 LPWINE_MLD wmld;
555 TRACE("(%p, %p);\n", hMidiOut, lpdwVolume);
557 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
558 return MMSYSERR_INVALHANDLE;
560 return MMDRV_Message(wmld, MODM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
563 /**************************************************************************
564 * midiOutSetVolume [WINMM.@]
566 UINT WINAPI midiOutSetVolume(HMIDIOUT hMidiOut, DWORD dwVolume)
568 LPWINE_MLD wmld;
570 TRACE("(%p, %d);\n", hMidiOut, dwVolume);
572 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
573 return MMSYSERR_INVALHANDLE;
575 return MMDRV_Message(wmld, MODM_SETVOLUME, dwVolume, 0L);
578 /**************************************************************************
579 * midiOutCachePatches [WINMM.@]
581 UINT WINAPI midiOutCachePatches(HMIDIOUT hMidiOut, UINT uBank,
582 WORD* lpwPatchArray, UINT uFlags)
584 /* not really necessary to support this */
585 FIXME("(%p, %u, %p, %x): Stub\n", hMidiOut, uBank, lpwPatchArray, uFlags);
586 return MMSYSERR_NOTSUPPORTED;
589 /**************************************************************************
590 * midiOutCacheDrumPatches [WINMM.@]
592 UINT WINAPI midiOutCacheDrumPatches(HMIDIOUT hMidiOut, UINT uPatch,
593 WORD* lpwKeyArray, UINT uFlags)
595 FIXME("(%p, %u, %p, %x): Stub\n", hMidiOut, uPatch, lpwKeyArray, uFlags);
596 return MMSYSERR_NOTSUPPORTED;
599 /**************************************************************************
600 * midiOutGetID [WINMM.@]
602 UINT WINAPI midiOutGetID(HMIDIOUT hMidiOut, UINT* lpuDeviceID)
604 LPWINE_MLD wmld;
606 TRACE("(%p, %p)\n", hMidiOut, lpuDeviceID);
608 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
609 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
610 return MMSYSERR_INVALHANDLE;
612 *lpuDeviceID = wmld->uDeviceID;
613 return MMSYSERR_NOERROR;
616 /**************************************************************************
617 * midiOutMessage [WINMM.@]
619 UINT WINAPI midiOutMessage(HMIDIOUT hMidiOut, UINT uMessage,
620 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
622 LPWINE_MLD wmld;
624 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
626 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) {
627 /* HACK... */
628 if (uMessage == 0x0001) {
629 *(LPDWORD)dwParam1 = 1;
630 return 0;
632 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) != NULL) {
633 return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
635 return MMSYSERR_INVALHANDLE;
638 switch (uMessage) {
639 case MODM_OPEN:
640 case MODM_CLOSE:
641 FIXME("can't handle OPEN or CLOSE message!\n");
642 return MMSYSERR_NOTSUPPORTED;
644 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
647 /**************************************************************************
648 * midiInGetNumDevs [WINMM.@]
650 UINT WINAPI midiInGetNumDevs(void)
652 return MMDRV_GetNum(MMDRV_MIDIIN);
655 /**************************************************************************
656 * midiInGetDevCapsW [WINMM.@]
658 UINT WINAPI midiInGetDevCapsW(UINT_PTR uDeviceID, LPMIDIINCAPSW lpCaps, UINT uSize)
660 LPWINE_MLD wmld;
662 TRACE("(%ld, %p, %d);\n", uDeviceID, lpCaps, uSize);
664 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
666 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIIN, TRUE)) == NULL)
667 return MMSYSERR_BADDEVICEID;
669 return MMDRV_Message(wmld, MIDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
672 /**************************************************************************
673 * midiInGetDevCapsA [WINMM.@]
675 UINT WINAPI midiInGetDevCapsA(UINT_PTR uDeviceID, LPMIDIINCAPSA lpCaps, UINT uSize)
677 MIDIINCAPSW micW;
678 UINT ret;
680 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
682 ret = midiInGetDevCapsW(uDeviceID, &micW, sizeof(micW));
684 if (ret == MMSYSERR_NOERROR) {
685 MIDIINCAPSA micA;
686 micA.wMid = micW.wMid;
687 micA.wPid = micW.wPid;
688 micA.vDriverVersion = micW.vDriverVersion;
689 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
690 sizeof(micA.szPname), NULL, NULL );
691 micA.dwSupport = micW.dwSupport;
692 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
694 return ret;
697 /**************************************************************************
698 * midiInOpen [WINMM.@]
700 UINT WINAPI midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID,
701 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
703 HANDLE hMidiIn;
704 LPWINE_MIDI lpwm;
705 DWORD dwRet;
707 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
708 lphMidiIn, uDeviceID, dwCallback, dwInstance, dwFlags);
710 if (lphMidiIn != NULL) *lphMidiIn = 0;
712 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
713 if (dwRet != MMSYSERR_NOERROR)
714 return dwRet;
716 lpwm = (LPWINE_MIDI)MMDRV_Alloc(sizeof(WINE_MIDI), MMDRV_MIDIIN, &hMidiIn,
717 &dwFlags, &dwCallback, &dwInstance);
719 if (lpwm == NULL)
720 return MMSYSERR_NOMEM;
722 lpwm->mod.hMidi = hMidiIn;
723 lpwm->mod.dwCallback = dwCallback;
724 lpwm->mod.dwInstance = dwInstance;
726 lpwm->mld.uDeviceID = uDeviceID;
727 dwRet = MMDRV_Open(&lpwm->mld, MIDM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
729 if (dwRet != MMSYSERR_NOERROR) {
730 MMDRV_Free(hMidiIn, &lpwm->mld);
731 hMidiIn = 0;
733 if (lphMidiIn != NULL) *lphMidiIn = hMidiIn;
734 TRACE("=> %d hMidi=%p\n", dwRet, hMidiIn);
736 return dwRet;
739 /**************************************************************************
740 * midiInClose [WINMM.@]
742 UINT WINAPI midiInClose(HMIDIIN hMidiIn)
744 LPWINE_MLD wmld;
745 DWORD dwRet;
747 TRACE("(%p)\n", hMidiIn);
749 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
750 return MMSYSERR_INVALHANDLE;
752 dwRet = MMDRV_Close(wmld, MIDM_CLOSE);
753 MMDRV_Free(hMidiIn, wmld);
754 return dwRet;
757 /**************************************************************************
758 * midiInPrepareHeader [WINMM.@]
760 UINT WINAPI midiInPrepareHeader(HMIDIIN hMidiIn,
761 MIDIHDR* lpMidiInHdr, UINT uSize)
763 LPWINE_MLD wmld;
765 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
767 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
768 return MMSYSERR_INVALHANDLE;
770 return MMDRV_Message(wmld, MIDM_PREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
773 /**************************************************************************
774 * midiInUnprepareHeader [WINMM.@]
776 UINT WINAPI midiInUnprepareHeader(HMIDIIN hMidiIn,
777 MIDIHDR* lpMidiInHdr, UINT uSize)
779 LPWINE_MLD wmld;
781 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
783 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
784 return MMSYSERR_INVALHANDLE;
786 return MMDRV_Message(wmld, MIDM_UNPREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
789 /**************************************************************************
790 * midiInAddBuffer [WINMM.@]
792 UINT WINAPI midiInAddBuffer(HMIDIIN hMidiIn,
793 MIDIHDR* lpMidiInHdr, UINT uSize)
795 LPWINE_MLD wmld;
797 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
799 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
800 return MMSYSERR_INVALHANDLE;
802 return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD_PTR)lpMidiInHdr, uSize);
805 /**************************************************************************
806 * midiInStart [WINMM.@]
808 UINT WINAPI midiInStart(HMIDIIN hMidiIn)
810 LPWINE_MLD wmld;
812 TRACE("(%p)\n", hMidiIn);
814 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
815 return MMSYSERR_INVALHANDLE;
817 return MMDRV_Message(wmld, MIDM_START, 0L, 0L);
820 /**************************************************************************
821 * midiInStop [WINMM.@]
823 UINT WINAPI midiInStop(HMIDIIN hMidiIn)
825 LPWINE_MLD wmld;
827 TRACE("(%p)\n", hMidiIn);
829 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
830 return MMSYSERR_INVALHANDLE;
832 return MMDRV_Message(wmld, MIDM_STOP, 0L, 0L);
835 /**************************************************************************
836 * midiInReset [WINMM.@]
838 UINT WINAPI midiInReset(HMIDIIN hMidiIn)
840 LPWINE_MLD wmld;
842 TRACE("(%p)\n", hMidiIn);
844 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
845 return MMSYSERR_INVALHANDLE;
847 return MMDRV_Message(wmld, MIDM_RESET, 0L, 0L);
850 /**************************************************************************
851 * midiInGetID [WINMM.@]
853 UINT WINAPI midiInGetID(HMIDIIN hMidiIn, UINT* lpuDeviceID)
855 LPWINE_MLD wmld;
857 TRACE("(%p, %p)\n", hMidiIn, lpuDeviceID);
859 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
861 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, TRUE)) == NULL)
862 return MMSYSERR_INVALHANDLE;
864 *lpuDeviceID = wmld->uDeviceID;
866 return MMSYSERR_NOERROR;
869 /**************************************************************************
870 * midiInMessage [WINMM.@]
872 UINT WINAPI midiInMessage(HMIDIIN hMidiIn, UINT uMessage,
873 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
875 LPWINE_MLD wmld;
877 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiIn, uMessage, dwParam1, dwParam2);
879 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
880 return MMSYSERR_INVALHANDLE;
882 switch (uMessage) {
883 case MIDM_OPEN:
884 case MIDM_CLOSE:
885 FIXME("can't handle OPEN or CLOSE message!\n");
886 return MMSYSERR_NOTSUPPORTED;
888 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
891 /**************************************************************************
892 * midiConnect [WINMM.@]
894 MMRESULT WINAPI midiConnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
896 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
897 return MMSYSERR_ERROR;
900 /**************************************************************************
901 * midiDisconnect [WINMM.@]
903 MMRESULT WINAPI midiDisconnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
905 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
906 return MMSYSERR_ERROR;
909 typedef struct WINE_MIDIStream {
910 HMIDIOUT hDevice;
911 HANDLE hThread;
912 DWORD dwThreadID;
913 DWORD dwTempo;
914 DWORD dwTimeDiv;
915 DWORD dwPositionMS;
916 DWORD dwPulses;
917 DWORD dwStartTicks;
918 WORD wFlags;
919 HANDLE hEvent;
920 LPMIDIHDR lpMidiHdr;
921 } WINE_MIDIStream;
923 #define WINE_MSM_HEADER (WM_USER+0)
924 #define WINE_MSM_STOP (WM_USER+1)
925 #define WINE_MSM_PAUSE (WM_USER+2)
926 #define WINE_MSM_RESUME (WM_USER+3)
928 /**************************************************************************
929 * MMSYSTEM_GetMidiStream [internal]
931 static BOOL MMSYSTEM_GetMidiStream(HMIDISTRM hMidiStrm, WINE_MIDIStream** lpMidiStrm, WINE_MIDI** lplpwm)
933 WINE_MIDI* lpwm = (LPWINE_MIDI)MMDRV_Get(hMidiStrm, MMDRV_MIDIOUT, FALSE);
935 if (lplpwm)
936 *lplpwm = lpwm;
938 if (lpwm == NULL) {
939 return FALSE;
942 *lpMidiStrm = (WINE_MIDIStream*)lpwm->mod.rgIds.dwStreamID;
944 return *lpMidiStrm != NULL;
947 /**************************************************************************
948 * MMSYSTEM_MidiStream_Convert [internal]
950 static DWORD MMSYSTEM_MidiStream_Convert(WINE_MIDIStream* lpMidiStrm, DWORD pulse)
952 DWORD ret = 0;
954 if (lpMidiStrm->dwTimeDiv == 0) {
955 FIXME("Shouldn't happen. lpMidiStrm->dwTimeDiv = 0\n");
956 } else if (lpMidiStrm->dwTimeDiv > 0x8000) { /* SMPTE, unchecked FIXME? */
957 int nf = -(char)HIBYTE(lpMidiStrm->dwTimeDiv); /* number of frames */
958 int nsf = LOBYTE(lpMidiStrm->dwTimeDiv); /* number of sub-frames */
959 ret = (pulse * 1000) / (nf * nsf);
960 } else {
961 ret = (DWORD)((double)pulse * ((double)lpMidiStrm->dwTempo / 1000) /
962 (double)lpMidiStrm->dwTimeDiv);
965 return ret;
968 /**************************************************************************
969 * MMSYSTEM_MidiStream_MessageHandler [internal]
971 static BOOL MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream* lpMidiStrm, LPWINE_MIDI lpwm, LPMSG msg)
973 LPMIDIHDR lpMidiHdr;
974 LPMIDIHDR* lpmh;
975 LPBYTE lpData;
976 BOOL paused = FALSE;
978 for (;;) {
979 switch (msg->message) {
980 case WM_QUIT:
981 return FALSE;
982 case WINE_MSM_STOP:
983 TRACE("STOP\n");
984 /* this is not quite what MS doc says... */
985 midiOutReset(lpMidiStrm->hDevice);
986 /* empty list of already submitted buffers */
987 lpMidiHdr = lpMidiStrm->lpMidiHdr;
988 lpMidiStrm->lpMidiHdr = NULL;
989 while (lpMidiHdr) {
990 LPMIDIHDR lphdr = lpMidiHdr;
991 lpMidiHdr = lpMidiHdr->lpNext;
992 lphdr->dwFlags |= MHDR_DONE;
993 lphdr->dwFlags &= ~MHDR_INQUEUE;
995 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
996 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
997 lpwm->mod.dwInstance, (DWORD_PTR)lphdr, 0);
999 return TRUE;
1000 case WINE_MSM_RESUME:
1001 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1002 lpMidiStrm->dwStartTicks = GetTickCount() - lpMidiStrm->dwPositionMS;
1003 return TRUE;
1004 case WINE_MSM_PAUSE:
1005 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1006 paused = TRUE;
1007 break;
1008 /* FIXME(EPP): "I don't understand the content of the first MIDIHDR sent
1009 * by native mcimidi, it doesn't look like a correct one".
1010 * this trick allows us to throw it away... but I don't like it.
1011 * It looks like part of the file I'm trying to play and definitively looks
1012 * like raw midi content.
1013 * I'd really like to understand why native mcimidi sends it. Perhaps a bad
1014 * synchronization issue where native mcimidi is still processing raw MIDI
1015 * content before generating MIDIEVENTs ?
1017 * 4c 04 89 3b 00 81 7c 99 3b 43 00 99 23 5e 04 89 L..;..|.;C..#^..
1018 * 3b 00 00 89 23 00 7c 99 3b 45 00 99 28 62 04 89 ;...#.|.;E..(b..
1019 * 3b 00 00 89 28 00 81 7c 99 3b 4e 00 99 23 5e 04 ;...(..|.;N..#^.
1020 * 89 3b 00 00 89 23 00 7c 99 3b 45 00 99 23 78 04 .;...#.|.;E..#x.
1021 * 89 3b 00 00 89 23 00 81 7c 99 3b 48 00 99 23 5e .;...#..|.;H..#^
1022 * 04 89 3b 00 00 89 23 00 7c 99 3b 4e 00 99 28 62 ..;...#.|.;N..(b
1023 * 04 89 3b 00 00 89 28 00 81 7c 99 39 4c 00 99 23 ..;...(..|.9L..#
1024 * 5e 04 89 39 00 00 89 23 00 82 7c 99 3b 4c 00 99 ^..9...#..|.;L..
1025 * 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 00 99 #^..;...#.|.;H..
1026 * 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b 3f 04 (b..;...(..|.;?.
1027 * 89 3b 00 1c 99 23 5e 04 89 23 00 5c 99 3b 45 00 .;...#^..#.\.;E.
1028 * 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 3b 46 .#x..;...#..|.;F
1029 * 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 ..#^..;...#.|.;H
1030 * 00 99 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b ..(b..;...(..|.;
1031 * 46 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b F..#^..;...#.|.;
1032 * 48 00 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 H..#x..;...#..|.
1033 * 3b 4c 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 ;L..#^..;...#.|.
1035 case WINE_MSM_HEADER:
1036 /* sets initial tick count for first MIDIHDR */
1037 if (!lpMidiStrm->dwStartTicks)
1038 lpMidiStrm->dwStartTicks = GetTickCount();
1039 lpMidiHdr = (LPMIDIHDR)msg->lParam;
1040 lpData = (LPBYTE)lpMidiHdr->lpData;
1041 TRACE("Adding %s lpMidiHdr=%p [lpData=0x%p dwBytesRecorded=%u/%u dwFlags=0x%08x size=%lu]\n",
1042 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular", lpMidiHdr,
1043 lpData, lpMidiHdr->dwBytesRecorded, lpMidiHdr->dwBufferLength,
1044 lpMidiHdr->dwFlags, msg->wParam);
1045 #if 0
1046 /* dumps content of lpMidiHdr->lpData
1047 * FIXME: there should be a debug routine somewhere that already does this
1049 for (dwToGo = 0; dwToGo < lpMidiHdr->dwBufferLength; dwToGo += 16) {
1050 DWORD i;
1051 BYTE ch;
1053 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++)
1054 printf("%02x ", lpData[dwToGo + i]);
1055 for (; i < 16; i++)
1056 printf(" ");
1057 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++) {
1058 ch = lpData[dwToGo + i];
1059 printf("%c", (ch >= 0x20 && ch <= 0x7F) ? ch : '.');
1061 printf("\n");
1063 #endif
1064 if (((LPMIDIEVENT)lpData)->dwStreamID != 0 &&
1065 ((LPMIDIEVENT)lpData)->dwStreamID != 0xFFFFFFFF &&
1066 ((LPMIDIEVENT)lpData)->dwStreamID != (DWORD)lpMidiStrm) {
1067 FIXME("Dropping bad %s lpMidiHdr (streamID=%08x)\n",
1068 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular",
1069 ((LPMIDIEVENT)lpData)->dwStreamID);
1070 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1071 lpMidiHdr->dwFlags |= MHDR_DONE;
1073 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1074 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1075 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1076 break;
1079 lpMidiHdr->lpNext = 0;
1080 for (lpmh = &lpMidiStrm->lpMidiHdr; *lpmh; lpmh = &(*lpmh)->lpNext);
1081 *lpmh = lpMidiHdr;
1082 break;
1083 default:
1084 FIXME("Unknown message %d\n", msg->message);
1085 break;
1087 if (!paused)
1088 return TRUE;
1089 GetMessageA(msg, 0, 0, 0);
1093 /**************************************************************************
1094 * MMSYSTEM_MidiStream_Player [internal]
1096 static DWORD CALLBACK MMSYSTEM_MidiStream_Player(LPVOID pmt)
1098 WINE_MIDIStream* lpMidiStrm = pmt;
1099 WINE_MIDI* lpwm;
1100 MSG msg;
1101 DWORD dwToGo;
1102 DWORD dwCurrTC;
1103 LPMIDIHDR lpMidiHdr;
1104 DWORD dwOffset;
1106 TRACE("(%p)!\n", lpMidiStrm);
1108 if (!lpMidiStrm ||
1109 (lpwm = (LPWINE_MIDI)MMDRV_Get(lpMidiStrm->hDevice, MMDRV_MIDIOUT, FALSE)) == NULL)
1110 goto the_end;
1112 /* force thread's queue creation */
1113 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
1115 lpMidiStrm->dwStartTicks = 0;
1116 lpMidiStrm->dwPulses = 0;
1118 lpMidiStrm->lpMidiHdr = 0;
1120 /* midiStreamOpen is waiting for ack */
1121 SetEvent(lpMidiStrm->hEvent);
1123 start_header:
1124 lpMidiHdr = lpMidiStrm->lpMidiHdr;
1125 if (!lpMidiHdr) {
1126 /* for first message, block until one arrives, then process all that are available */
1127 GetMessageA(&msg, 0, 0, 0);
1128 do {
1129 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1130 goto the_end;
1131 } while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE));
1132 goto start_header;
1135 dwOffset = 0;
1136 while (dwOffset + offsetof(MIDIEVENT,dwParms) <= lpMidiHdr->dwBytesRecorded) {
1137 LPMIDIEVENT me = (LPMIDIEVENT)(lpMidiHdr->lpData+dwOffset);
1139 /* do we have to wait ? */
1140 if (me->dwDeltaTime) {
1141 lpMidiStrm->dwPositionMS += MMSYSTEM_MidiStream_Convert(lpMidiStrm, me->dwDeltaTime);
1142 lpMidiStrm->dwPulses += me->dwDeltaTime;
1144 dwToGo = lpMidiStrm->dwStartTicks + lpMidiStrm->dwPositionMS;
1146 TRACE("%u/%u/%u\n", dwToGo, GetTickCount(), me->dwDeltaTime);
1147 while (dwToGo - (dwCurrTC = GetTickCount()) <= MAXLONG) {
1148 if (MsgWaitForMultipleObjects(0, NULL, FALSE, dwToGo - dwCurrTC, QS_ALLINPUT) == WAIT_OBJECT_0) {
1149 /* got a message, handle it */
1150 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
1151 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1152 goto the_end;
1153 /* is lpMidiHdr still current? */
1154 if (lpMidiHdr != lpMidiStrm->lpMidiHdr) {
1155 goto start_header;
1158 } else {
1159 /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
1160 break;
1164 switch (MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK)) {
1165 case MEVT_COMMENT:
1166 FIXME("NIY: MEVT_COMMENT\n");
1167 /* do nothing, skip bytes */
1168 break;
1169 case MEVT_LONGMSG:
1170 midiOutLongMsg(lpMidiStrm->hDevice, lpMidiStrm->lpMidiHdr, MEVT_EVENTPARM(me->dwEvent));
1171 break;
1172 case MEVT_NOP:
1173 break;
1174 case MEVT_SHORTMSG:
1175 midiOutShortMsg(lpMidiStrm->hDevice, MEVT_EVENTPARM(me->dwEvent));
1176 break;
1177 case MEVT_TEMPO:
1178 lpMidiStrm->dwTempo = MEVT_EVENTPARM(me->dwEvent);
1179 break;
1180 case MEVT_VERSION:
1181 break;
1182 default:
1183 FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK));
1184 break;
1186 if (me->dwEvent & MEVT_F_CALLBACK) {
1187 /* native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
1188 lpMidiHdr->dwOffset = dwOffset;
1189 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1190 (HDRVR)lpMidiStrm->hDevice, MM_MOM_POSITIONCB,
1191 lpwm->mod.dwInstance, (LPARAM)lpMidiHdr, 0L);
1193 dwOffset += offsetof(MIDIEVENT,dwParms);
1194 if (me->dwEvent & MEVT_F_LONG)
1195 dwOffset += (MEVT_EVENTPARM(me->dwEvent) + 3) & ~3;
1197 /* done with this header */
1198 lpMidiStrm->lpMidiHdr = lpMidiHdr->lpNext;
1199 lpMidiHdr->dwFlags |= MHDR_DONE;
1200 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1202 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1203 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1204 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1205 goto start_header;
1207 the_end:
1208 TRACE("End of thread\n");
1209 return 0;
1212 /**************************************************************************
1213 * midiStreamClose [WINMM.@]
1215 MMRESULT WINAPI midiStreamClose(HMIDISTRM hMidiStrm)
1217 WINE_MIDIStream* lpMidiStrm;
1218 MMRESULT ret = 0;
1220 TRACE("(%p)!\n", hMidiStrm);
1222 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1223 return MMSYSERR_INVALHANDLE;
1225 midiStreamStop(hMidiStrm);
1226 PostThreadMessageA(lpMidiStrm->dwThreadID, WM_QUIT, 0, 0);
1227 CloseHandle(lpMidiStrm->hEvent);
1228 if (lpMidiStrm->hThread) {
1229 if (GetCurrentThreadId() != lpMidiStrm->dwThreadID)
1230 WaitForSingleObject(lpMidiStrm->hThread, INFINITE);
1231 else {
1232 FIXME("leak from call within function callback\n");
1233 ret = MMSYSERR_HANDLEBUSY; /* yet don't signal it to app */
1235 CloseHandle(lpMidiStrm->hThread);
1237 if(!ret)
1238 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1240 return midiOutClose((HMIDIOUT)hMidiStrm);
1243 /**************************************************************************
1244 * midiStreamOpen [WINMM.@]
1246 MMRESULT WINAPI midiStreamOpen(HMIDISTRM* lphMidiStrm, LPUINT lpuDeviceID,
1247 DWORD cMidi, DWORD_PTR dwCallback,
1248 DWORD_PTR dwInstance, DWORD fdwOpen)
1250 WINE_MIDIStream* lpMidiStrm;
1251 MMRESULT ret;
1252 MIDIOPENSTRMID mosm;
1253 LPWINE_MIDI lpwm;
1254 HMIDIOUT hMidiOut;
1256 TRACE("(%p, %p, %d, 0x%08lx, 0x%08lx, 0x%08x)!\n",
1257 lphMidiStrm, lpuDeviceID, cMidi, dwCallback, dwInstance, fdwOpen);
1259 if (cMidi != 1 || lphMidiStrm == NULL || lpuDeviceID == NULL)
1260 return MMSYSERR_INVALPARAM;
1262 ret = WINMM_CheckCallback(dwCallback, fdwOpen, FALSE);
1263 if (ret != MMSYSERR_NOERROR)
1264 return ret;
1266 lpMidiStrm = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream));
1267 if (!lpMidiStrm)
1268 return MMSYSERR_NOMEM;
1270 lpMidiStrm->dwTempo = 500000;
1271 lpMidiStrm->dwTimeDiv = 480; /* 480 is 120 quarter notes per minute *//* FIXME ??*/
1272 lpMidiStrm->dwPositionMS = 0;
1274 mosm.dwStreamID = (DWORD)lpMidiStrm;
1275 /* FIXME: the correct value is not allocated yet for MAPPER */
1276 mosm.wDeviceID = *lpuDeviceID;
1277 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &fdwOpen, 1, &mosm);
1278 if (!lpwm) {
1279 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1280 return MMSYSERR_NOMEM;
1282 lpMidiStrm->hDevice = hMidiOut;
1283 *lphMidiStrm = (HMIDISTRM)hMidiOut;
1285 lpwm->mld.uDeviceID = *lpuDeviceID;
1287 ret = MMDRV_Open(&lpwm->mld, MODM_OPEN, (DWORD_PTR)&lpwm->mod, fdwOpen);
1288 if (ret != MMSYSERR_NOERROR) {
1289 MMDRV_Free(hMidiOut, &lpwm->mld);
1290 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1291 return ret;
1294 lpMidiStrm->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1295 lpMidiStrm->wFlags = HIWORD(fdwOpen);
1297 lpMidiStrm->hThread = CreateThread(NULL, 0, MMSYSTEM_MidiStream_Player,
1298 lpMidiStrm, 0, &(lpMidiStrm->dwThreadID));
1300 if (!lpMidiStrm->hThread) {
1301 midiStreamClose((HMIDISTRM)hMidiOut);
1302 return MMSYSERR_NOMEM;
1304 SetThreadPriority(lpMidiStrm->hThread, THREAD_PRIORITY_TIME_CRITICAL);
1306 /* wait for thread to have started, and for its queue to be created */
1307 WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
1309 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1311 TRACE("=> (%u/%d) hMidi=%p ret=%d lpMidiStrm=%p\n",
1312 *lpuDeviceID, lpwm->mld.uDeviceID, *lphMidiStrm, ret, lpMidiStrm);
1313 return ret;
1316 /**************************************************************************
1317 * midiStreamOut [WINMM.@]
1319 MMRESULT WINAPI midiStreamOut(HMIDISTRM hMidiStrm, LPMIDIHDR lpMidiHdr,
1320 UINT cbMidiHdr)
1322 WINE_MIDIStream* lpMidiStrm;
1323 DWORD ret = MMSYSERR_NOERROR;
1325 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMidiHdr, cbMidiHdr);
1327 if (cbMidiHdr < offsetof(MIDIHDR,dwOffset) || !lpMidiHdr || !lpMidiHdr->lpData
1328 || lpMidiHdr->dwBufferLength < lpMidiHdr->dwBytesRecorded
1329 || lpMidiHdr->dwBytesRecorded % 4 /* player expects DWORD padding */)
1330 return MMSYSERR_INVALPARAM;
1331 /* FIXME: Native additionally checks if the MIDIEVENTs in lpData
1332 * exactly fit dwBytesRecorded. */
1334 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1335 return MIDIERR_UNPREPARED;
1337 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1338 return MIDIERR_STILLPLAYING;
1340 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1341 ret = MMSYSERR_INVALHANDLE;
1342 } else {
1343 lpMidiHdr->dwFlags |= MHDR_ISSTRM | MHDR_INQUEUE;
1344 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1345 if (!PostThreadMessageA(lpMidiStrm->dwThreadID,
1346 WINE_MSM_HEADER, cbMidiHdr,
1347 (LPARAM)lpMidiHdr)) {
1348 ERR("bad PostThreadMessageA\n");
1349 ret = MMSYSERR_ERROR;
1352 return ret;
1355 /**************************************************************************
1356 * midiStreamPause [WINMM.@]
1358 MMRESULT WINAPI midiStreamPause(HMIDISTRM hMidiStrm)
1360 WINE_MIDIStream* lpMidiStrm;
1361 DWORD ret = MMSYSERR_NOERROR;
1363 TRACE("(%p)!\n", hMidiStrm);
1365 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1366 return MMSYSERR_INVALHANDLE;
1367 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1368 return ret;
1371 /**************************************************************************
1372 * midiStreamPosition [WINMM.@]
1374 MMRESULT WINAPI midiStreamPosition(HMIDISTRM hMidiStrm, LPMMTIME lpMMT, UINT cbmmt)
1376 WINE_MIDIStream* lpMidiStrm;
1377 DWORD ret = MMSYSERR_NOERROR;
1379 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMMT, cbmmt);
1381 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1382 ret = MMSYSERR_INVALHANDLE;
1383 } else if (lpMMT == NULL || cbmmt != sizeof(MMTIME)) {
1384 ret = MMSYSERR_INVALPARAM;
1385 } else {
1386 switch (lpMMT->wType) {
1387 default:
1388 FIXME("Unsupported time type %x\n", lpMMT->wType);
1389 /* fall through */
1390 case TIME_BYTES:
1391 case TIME_SAMPLES:
1392 lpMMT->wType = TIME_MS;
1393 /* fall through to alternative format */
1394 case TIME_MS:
1395 lpMMT->u.ms = lpMidiStrm->dwPositionMS;
1396 TRACE("=> %d ms\n", lpMMT->u.ms);
1397 break;
1398 case TIME_TICKS:
1399 lpMMT->u.ticks = lpMidiStrm->dwPulses;
1400 TRACE("=> %d ticks\n", lpMMT->u.ticks);
1401 break;
1404 return ret;
1407 /**************************************************************************
1408 * midiStreamProperty [WINMM.@]
1410 MMRESULT WINAPI midiStreamProperty(HMIDISTRM hMidiStrm, LPBYTE lpPropData, DWORD dwProperty)
1412 WINE_MIDIStream* lpMidiStrm;
1413 MMRESULT ret = MMSYSERR_NOERROR;
1415 TRACE("(%p, %p, %x)\n", hMidiStrm, lpPropData, dwProperty);
1417 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1418 ret = MMSYSERR_INVALHANDLE;
1419 } else if ((dwProperty & (MIDIPROP_GET|MIDIPROP_SET)) == 0) {
1420 ret = MMSYSERR_INVALPARAM;
1421 } else if (dwProperty & MIDIPROP_TEMPO) {
1422 MIDIPROPTEMPO* mpt = (MIDIPROPTEMPO*)lpPropData;
1424 if (sizeof(MIDIPROPTEMPO) != mpt->cbStruct) {
1425 ret = MMSYSERR_INVALPARAM;
1426 } else if (dwProperty & MIDIPROP_SET) {
1427 lpMidiStrm->dwTempo = mpt->dwTempo;
1428 TRACE("Setting tempo to %d\n", mpt->dwTempo);
1429 } else if (dwProperty & MIDIPROP_GET) {
1430 mpt->dwTempo = lpMidiStrm->dwTempo;
1431 TRACE("Getting tempo <= %d\n", mpt->dwTempo);
1433 } else if (dwProperty & MIDIPROP_TIMEDIV) {
1434 MIDIPROPTIMEDIV* mptd = (MIDIPROPTIMEDIV*)lpPropData;
1436 if (sizeof(MIDIPROPTIMEDIV) != mptd->cbStruct) {
1437 ret = MMSYSERR_INVALPARAM;
1438 } else if (dwProperty & MIDIPROP_SET) {
1439 lpMidiStrm->dwTimeDiv = mptd->dwTimeDiv;
1440 TRACE("Setting time div to %d\n", mptd->dwTimeDiv);
1441 } else if (dwProperty & MIDIPROP_GET) {
1442 mptd->dwTimeDiv = lpMidiStrm->dwTimeDiv;
1443 TRACE("Getting time div <= %d\n", mptd->dwTimeDiv);
1445 } else {
1446 ret = MMSYSERR_INVALPARAM;
1449 return ret;
1452 /**************************************************************************
1453 * midiStreamRestart [WINMM.@]
1455 MMRESULT WINAPI midiStreamRestart(HMIDISTRM hMidiStrm)
1457 WINE_MIDIStream* lpMidiStrm;
1458 MMRESULT ret = MMSYSERR_NOERROR;
1460 TRACE("(%p)!\n", hMidiStrm);
1462 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1463 return MMSYSERR_INVALHANDLE;
1464 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_RESUME, 0, 0);
1465 return ret;
1468 /**************************************************************************
1469 * midiStreamStop [WINMM.@]
1471 MMRESULT WINAPI midiStreamStop(HMIDISTRM hMidiStrm)
1473 WINE_MIDIStream* lpMidiStrm;
1474 MMRESULT ret = MMSYSERR_NOERROR;
1476 TRACE("(%p)!\n", hMidiStrm);
1478 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1479 return MMSYSERR_INVALHANDLE;
1480 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_STOP, 0, 0);
1481 return ret;
1484 struct mm_starter
1486 LPTASKCALLBACK cb;
1487 DWORD client;
1488 HANDLE event;
1491 static DWORD WINAPI mmTaskRun(void* pmt)
1493 struct mm_starter mms;
1495 memcpy(&mms, pmt, sizeof(struct mm_starter));
1496 HeapFree(GetProcessHeap(), 0, pmt);
1497 mms.cb(mms.client);
1498 if (mms.event) SetEvent(mms.event);
1499 return 0;
1502 /******************************************************************
1503 * mmTaskCreate (WINMM.@)
1505 UINT WINAPI mmTaskCreate(LPTASKCALLBACK cb, HANDLE* ph, DWORD_PTR client)
1507 HANDLE hThread;
1508 HANDLE hEvent = 0;
1509 struct mm_starter *mms;
1511 mms = HeapAlloc(GetProcessHeap(), 0, sizeof(struct mm_starter));
1512 if (mms == NULL) return TASKERR_OUTOFMEMORY;
1514 mms->cb = cb;
1515 mms->client = client;
1516 if (ph) hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1517 mms->event = hEvent;
1519 hThread = CreateThread(0, 0, mmTaskRun, mms, 0, NULL);
1520 if (!hThread) {
1521 HeapFree(GetProcessHeap(), 0, mms);
1522 if (hEvent) CloseHandle(hEvent);
1523 return TASKERR_OUTOFMEMORY;
1525 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
1526 if (ph) *ph = hEvent;
1527 CloseHandle(hThread);
1528 return 0;
1531 /******************************************************************
1532 * mmTaskBlock (WINMM.@)
1534 VOID WINAPI mmTaskBlock(DWORD tid)
1536 MSG msg;
1540 GetMessageA(&msg, 0, 0, 0);
1541 if (msg.hwnd) DispatchMessageA(&msg);
1542 } while (msg.message != WM_USER);
1545 /******************************************************************
1546 * mmTaskSignal (WINMM.@)
1548 BOOL WINAPI mmTaskSignal(DWORD tid)
1550 return PostThreadMessageW(tid, WM_USER, 0, 0);
1553 /******************************************************************
1554 * mmTaskYield (WINMM.@)
1556 VOID WINAPI mmTaskYield(VOID) {}
1558 /******************************************************************
1559 * mmGetCurrentTask (WINMM.@)
1561 DWORD WINAPI mmGetCurrentTask(VOID)
1563 return GetCurrentThreadId();