quartz: Add tests for IBasicVideo.
[wine.git] / dlls / winmm / winmm.c
blobf2d0858a55b79f04871e266193d258709e4107d6
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
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.wReserved1 = acW.wReserved1;
233 acA.dwSupport = acW.dwSupport;
234 memcpy(lpCaps, &acA, min(uSize, sizeof(acA)));
236 return ret;
239 /**************************************************************************
240 * auxGetVolume [WINMM.@]
242 UINT WINAPI auxGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
244 LPWINE_MLD wmld;
246 TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);
248 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
249 return MMSYSERR_INVALHANDLE;
250 return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
253 /**************************************************************************
254 * auxSetVolume [WINMM.@]
256 UINT WINAPI auxSetVolume(UINT uDeviceID, DWORD dwVolume)
258 LPWINE_MLD wmld;
260 TRACE("(%04X, %u) !\n", uDeviceID, dwVolume);
262 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
263 return MMSYSERR_INVALHANDLE;
264 return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L);
267 /**************************************************************************
268 * auxOutMessage [WINMM.@]
270 UINT WINAPI auxOutMessage(UINT uDeviceID, UINT uMessage, DWORD_PTR dw1, DWORD_PTR dw2)
272 LPWINE_MLD wmld;
274 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
275 return MMSYSERR_INVALHANDLE;
277 return MMDRV_Message(wmld, uMessage, dw1, dw2);
280 /**************************************************************************
281 * midiOutGetNumDevs [WINMM.@]
283 UINT WINAPI midiOutGetNumDevs(void)
285 return MMDRV_GetNum(MMDRV_MIDIOUT);
288 /**************************************************************************
289 * midiOutGetDevCapsW [WINMM.@]
291 UINT WINAPI midiOutGetDevCapsW(UINT_PTR uDeviceID, LPMIDIOUTCAPSW lpCaps,
292 UINT uSize)
294 LPWINE_MLD wmld;
296 TRACE("(%lu, %p, %u);\n", uDeviceID, lpCaps, uSize);
298 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
300 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL)
301 return MMSYSERR_BADDEVICEID;
303 return MMDRV_Message(wmld, MODM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
306 /**************************************************************************
307 * midiOutGetDevCapsA [WINMM.@]
309 UINT WINAPI midiOutGetDevCapsA(UINT_PTR uDeviceID, LPMIDIOUTCAPSA lpCaps,
310 UINT uSize)
312 MIDIOUTCAPSW mocW;
313 UINT ret;
315 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
317 ret = midiOutGetDevCapsW(uDeviceID, &mocW, sizeof(mocW));
319 if (ret == MMSYSERR_NOERROR) {
320 MIDIOUTCAPSA mocA;
321 mocA.wMid = mocW.wMid;
322 mocA.wPid = mocW.wPid;
323 mocA.vDriverVersion = mocW.vDriverVersion;
324 WideCharToMultiByte( CP_ACP, 0, mocW.szPname, -1, mocA.szPname,
325 sizeof(mocA.szPname), NULL, NULL );
326 mocA.wTechnology = mocW.wTechnology;
327 mocA.wVoices = mocW.wVoices;
328 mocA.wNotes = mocW.wNotes;
329 mocA.wChannelMask = mocW.wChannelMask;
330 mocA.dwSupport = mocW.dwSupport;
331 memcpy(lpCaps, &mocA, min(uSize, sizeof(mocA)));
333 return ret;
336 /**************************************************************************
337 * midiOutGetErrorTextA [WINMM.@]
338 * midiInGetErrorTextA [WINMM.@]
340 UINT WINAPI midiOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
342 UINT ret;
344 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
345 else if (uSize == 0) ret = MMSYSERR_NOERROR;
346 else
348 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
349 if (!xstr) ret = MMSYSERR_NOMEM;
350 else
352 ret = midiOutGetErrorTextW(uError, xstr, uSize);
353 if (ret == MMSYSERR_NOERROR)
354 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
355 HeapFree(GetProcessHeap(), 0, xstr);
358 return ret;
361 /**************************************************************************
362 * midiOutGetErrorTextW [WINMM.@]
363 * midiInGetErrorTextW [WINMM.@]
365 UINT WINAPI midiOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
367 UINT ret = MMSYSERR_BADERRNUM;
369 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
370 else if (uSize == 0) ret = MMSYSERR_NOERROR;
371 else if (
372 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
373 * a warning for the test was always true */
374 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
375 (uError >= MIDIERR_BASE && uError <= MIDIERR_LASTERROR)) {
376 if (LoadStringW(hWinMM32Instance, uError, lpText, uSize) > 0) {
377 ret = MMSYSERR_NOERROR;
380 return ret;
383 /**************************************************************************
384 * MIDI_OutAlloc [internal]
386 static LPWINE_MIDI MIDI_OutAlloc(HMIDIOUT* lphMidiOut, DWORD_PTR* lpdwCallback,
387 DWORD_PTR* lpdwInstance, LPDWORD lpdwFlags,
388 DWORD cIDs, MIDIOPENSTRMID* lpIDs)
390 HANDLE hMidiOut;
391 LPWINE_MIDI lpwm;
392 UINT size;
394 size = sizeof(WINE_MIDI) + (cIDs ? (cIDs-1) : 0) * sizeof(MIDIOPENSTRMID);
396 lpwm = (LPWINE_MIDI)MMDRV_Alloc(size, MMDRV_MIDIOUT, &hMidiOut, lpdwFlags,
397 lpdwCallback, lpdwInstance);
398 *lphMidiOut = hMidiOut;
400 if (lpwm) {
401 lpwm->mod.hMidi = hMidiOut;
402 lpwm->mod.dwCallback = *lpdwCallback;
403 lpwm->mod.dwInstance = *lpdwInstance;
404 lpwm->mod.dnDevNode = 0;
405 lpwm->mod.cIds = cIDs;
406 if (cIDs)
407 memcpy(&(lpwm->mod.rgIds), lpIDs, cIDs * sizeof(MIDIOPENSTRMID));
409 return lpwm;
412 /**************************************************************************
413 * midiOutOpen [WINMM.@]
415 UINT WINAPI midiOutOpen(LPHMIDIOUT lphMidiOut, UINT uDeviceID,
416 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
418 HMIDIOUT hMidiOut;
419 LPWINE_MIDI lpwm;
420 UINT dwRet;
422 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
423 lphMidiOut, uDeviceID, dwCallback, dwInstance, dwFlags);
425 if (lphMidiOut != NULL) *lphMidiOut = 0;
427 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
428 if (dwRet != MMSYSERR_NOERROR)
429 return dwRet;
431 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &dwFlags, 0, NULL);
433 if (lpwm == NULL)
434 return MMSYSERR_NOMEM;
436 lpwm->mld.uDeviceID = uDeviceID;
438 dwRet = MMDRV_Open((LPWINE_MLD)lpwm, MODM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
440 if (dwRet != MMSYSERR_NOERROR) {
441 MMDRV_Free(hMidiOut, (LPWINE_MLD)lpwm);
442 hMidiOut = 0;
445 if (lphMidiOut) *lphMidiOut = hMidiOut;
446 TRACE("=> %d hMidi=%p\n", dwRet, hMidiOut);
448 return dwRet;
451 /**************************************************************************
452 * midiOutClose [WINMM.@]
454 UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
456 LPWINE_MLD wmld;
457 DWORD dwRet;
459 TRACE("(%p)\n", hMidiOut);
461 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
462 return MMSYSERR_INVALHANDLE;
464 dwRet = MMDRV_Close(wmld, MODM_CLOSE);
465 MMDRV_Free(hMidiOut, wmld);
467 return dwRet;
470 /**************************************************************************
471 * midiOutPrepareHeader [WINMM.@]
473 UINT WINAPI midiOutPrepareHeader(HMIDIOUT hMidiOut,
474 MIDIHDR* lpMidiOutHdr, UINT uSize)
476 LPWINE_MLD wmld;
478 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
480 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
481 return MMSYSERR_INVALHANDLE;
482 /* FIXME: detect MIDIStream handles and enforce 64KB buffer limit on those */
484 return MMDRV_Message(wmld, MODM_PREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
487 /**************************************************************************
488 * midiOutUnprepareHeader [WINMM.@]
490 UINT WINAPI midiOutUnprepareHeader(HMIDIOUT hMidiOut,
491 MIDIHDR* lpMidiOutHdr, UINT uSize)
493 LPWINE_MLD wmld;
495 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
497 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
498 return MMSYSERR_INVALHANDLE;
500 return MMDRV_Message(wmld, MODM_UNPREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
503 /**************************************************************************
504 * midiOutShortMsg [WINMM.@]
506 UINT WINAPI midiOutShortMsg(HMIDIOUT hMidiOut, DWORD dwMsg)
508 LPWINE_MLD wmld;
510 TRACE("(%p, %08X)\n", hMidiOut, dwMsg);
512 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
513 return MMSYSERR_INVALHANDLE;
515 return MMDRV_Message(wmld, MODM_DATA, dwMsg, 0L);
518 /**************************************************************************
519 * midiOutLongMsg [WINMM.@]
521 UINT WINAPI midiOutLongMsg(HMIDIOUT hMidiOut,
522 MIDIHDR* lpMidiOutHdr, UINT uSize)
524 LPWINE_MLD wmld;
526 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
528 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
529 return MMSYSERR_INVALHANDLE;
531 return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD_PTR)lpMidiOutHdr, uSize);
534 /**************************************************************************
535 * midiOutReset [WINMM.@]
537 UINT WINAPI midiOutReset(HMIDIOUT hMidiOut)
539 LPWINE_MLD wmld;
541 TRACE("(%p)\n", hMidiOut);
543 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
544 return MMSYSERR_INVALHANDLE;
546 return MMDRV_Message(wmld, MODM_RESET, 0L, 0L);
549 /**************************************************************************
550 * midiOutGetVolume [WINMM.@]
552 UINT WINAPI midiOutGetVolume(HMIDIOUT hMidiOut, DWORD* lpdwVolume)
554 LPWINE_MLD wmld;
556 TRACE("(%p, %p);\n", hMidiOut, lpdwVolume);
558 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
559 return MMSYSERR_INVALHANDLE;
561 return MMDRV_Message(wmld, MODM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
564 /**************************************************************************
565 * midiOutSetVolume [WINMM.@]
567 UINT WINAPI midiOutSetVolume(HMIDIOUT hMidiOut, DWORD dwVolume)
569 LPWINE_MLD wmld;
571 TRACE("(%p, %d);\n", hMidiOut, dwVolume);
573 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
574 return MMSYSERR_INVALHANDLE;
576 return MMDRV_Message(wmld, MODM_SETVOLUME, dwVolume, 0L);
579 /**************************************************************************
580 * midiOutCachePatches [WINMM.@]
582 UINT WINAPI midiOutCachePatches(HMIDIOUT hMidiOut, UINT uBank,
583 WORD* lpwPatchArray, UINT uFlags)
585 /* not really necessary to support this */
586 FIXME("(%p, %u, %p, %x): Stub\n", hMidiOut, uBank, lpwPatchArray, uFlags);
587 return MMSYSERR_NOTSUPPORTED;
590 /**************************************************************************
591 * midiOutCacheDrumPatches [WINMM.@]
593 UINT WINAPI midiOutCacheDrumPatches(HMIDIOUT hMidiOut, UINT uPatch,
594 WORD* lpwKeyArray, UINT uFlags)
596 FIXME("(%p, %u, %p, %x): Stub\n", hMidiOut, uPatch, lpwKeyArray, uFlags);
597 return MMSYSERR_NOTSUPPORTED;
600 /**************************************************************************
601 * midiOutGetID [WINMM.@]
603 UINT WINAPI midiOutGetID(HMIDIOUT hMidiOut, UINT* lpuDeviceID)
605 LPWINE_MLD wmld;
607 TRACE("(%p, %p)\n", hMidiOut, lpuDeviceID);
609 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
610 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
611 return MMSYSERR_INVALHANDLE;
613 *lpuDeviceID = wmld->uDeviceID;
614 return MMSYSERR_NOERROR;
617 /**************************************************************************
618 * midiOutMessage [WINMM.@]
620 UINT WINAPI midiOutMessage(HMIDIOUT hMidiOut, UINT uMessage,
621 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
623 LPWINE_MLD wmld;
625 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
627 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) {
628 /* HACK... */
629 if (uMessage == 0x0001) {
630 *(LPDWORD)dwParam1 = 1;
631 return 0;
633 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) != NULL) {
634 return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
636 return MMSYSERR_INVALHANDLE;
639 switch (uMessage) {
640 case MODM_OPEN:
641 case MODM_CLOSE:
642 FIXME("can't handle OPEN or CLOSE message!\n");
643 return MMSYSERR_NOTSUPPORTED;
645 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
648 /**************************************************************************
649 * midiInGetNumDevs [WINMM.@]
651 UINT WINAPI midiInGetNumDevs(void)
653 return MMDRV_GetNum(MMDRV_MIDIIN);
656 /**************************************************************************
657 * midiInGetDevCapsW [WINMM.@]
659 UINT WINAPI midiInGetDevCapsW(UINT_PTR uDeviceID, LPMIDIINCAPSW lpCaps, UINT uSize)
661 LPWINE_MLD wmld;
663 TRACE("(%ld, %p, %d);\n", uDeviceID, lpCaps, uSize);
665 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
667 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIIN, TRUE)) == NULL)
668 return MMSYSERR_BADDEVICEID;
670 return MMDRV_Message(wmld, MIDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
673 /**************************************************************************
674 * midiInGetDevCapsA [WINMM.@]
676 UINT WINAPI midiInGetDevCapsA(UINT_PTR uDeviceID, LPMIDIINCAPSA lpCaps, UINT uSize)
678 MIDIINCAPSW micW;
679 UINT ret;
681 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
683 ret = midiInGetDevCapsW(uDeviceID, &micW, sizeof(micW));
685 if (ret == MMSYSERR_NOERROR) {
686 MIDIINCAPSA micA;
687 micA.wMid = micW.wMid;
688 micA.wPid = micW.wPid;
689 micA.vDriverVersion = micW.vDriverVersion;
690 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
691 sizeof(micA.szPname), NULL, NULL );
692 micA.dwSupport = micW.dwSupport;
693 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
695 return ret;
698 /**************************************************************************
699 * midiInOpen [WINMM.@]
701 UINT WINAPI midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID,
702 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
704 HANDLE hMidiIn;
705 LPWINE_MIDI lpwm;
706 DWORD dwRet;
708 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
709 lphMidiIn, uDeviceID, dwCallback, dwInstance, dwFlags);
711 if (lphMidiIn != NULL) *lphMidiIn = 0;
713 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
714 if (dwRet != MMSYSERR_NOERROR)
715 return dwRet;
717 lpwm = (LPWINE_MIDI)MMDRV_Alloc(sizeof(WINE_MIDI), MMDRV_MIDIIN, &hMidiIn,
718 &dwFlags, &dwCallback, &dwInstance);
720 if (lpwm == NULL)
721 return MMSYSERR_NOMEM;
723 lpwm->mod.hMidi = hMidiIn;
724 lpwm->mod.dwCallback = dwCallback;
725 lpwm->mod.dwInstance = dwInstance;
727 lpwm->mld.uDeviceID = uDeviceID;
728 dwRet = MMDRV_Open(&lpwm->mld, MIDM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
730 if (dwRet != MMSYSERR_NOERROR) {
731 MMDRV_Free(hMidiIn, &lpwm->mld);
732 hMidiIn = 0;
734 if (lphMidiIn != NULL) *lphMidiIn = hMidiIn;
735 TRACE("=> %d hMidi=%p\n", dwRet, hMidiIn);
737 return dwRet;
740 /**************************************************************************
741 * midiInClose [WINMM.@]
743 UINT WINAPI midiInClose(HMIDIIN hMidiIn)
745 LPWINE_MLD wmld;
746 DWORD dwRet;
748 TRACE("(%p)\n", hMidiIn);
750 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
751 return MMSYSERR_INVALHANDLE;
753 dwRet = MMDRV_Close(wmld, MIDM_CLOSE);
754 MMDRV_Free(hMidiIn, wmld);
755 return dwRet;
758 /**************************************************************************
759 * midiInPrepareHeader [WINMM.@]
761 UINT WINAPI midiInPrepareHeader(HMIDIIN hMidiIn,
762 MIDIHDR* lpMidiInHdr, UINT uSize)
764 LPWINE_MLD wmld;
766 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
768 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
769 return MMSYSERR_INVALHANDLE;
771 return MMDRV_Message(wmld, MIDM_PREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
774 /**************************************************************************
775 * midiInUnprepareHeader [WINMM.@]
777 UINT WINAPI midiInUnprepareHeader(HMIDIIN hMidiIn,
778 MIDIHDR* lpMidiInHdr, UINT uSize)
780 LPWINE_MLD wmld;
782 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
784 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
785 return MMSYSERR_INVALHANDLE;
787 return MMDRV_Message(wmld, MIDM_UNPREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
790 /**************************************************************************
791 * midiInAddBuffer [WINMM.@]
793 UINT WINAPI midiInAddBuffer(HMIDIIN hMidiIn,
794 MIDIHDR* lpMidiInHdr, UINT uSize)
796 LPWINE_MLD wmld;
798 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
800 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
801 return MMSYSERR_INVALHANDLE;
803 return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD_PTR)lpMidiInHdr, uSize);
806 /**************************************************************************
807 * midiInStart [WINMM.@]
809 UINT WINAPI midiInStart(HMIDIIN hMidiIn)
811 LPWINE_MLD wmld;
813 TRACE("(%p)\n", hMidiIn);
815 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
816 return MMSYSERR_INVALHANDLE;
818 return MMDRV_Message(wmld, MIDM_START, 0L, 0L);
821 /**************************************************************************
822 * midiInStop [WINMM.@]
824 UINT WINAPI midiInStop(HMIDIIN hMidiIn)
826 LPWINE_MLD wmld;
828 TRACE("(%p)\n", hMidiIn);
830 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
831 return MMSYSERR_INVALHANDLE;
833 return MMDRV_Message(wmld, MIDM_STOP, 0L, 0L);
836 /**************************************************************************
837 * midiInReset [WINMM.@]
839 UINT WINAPI midiInReset(HMIDIIN hMidiIn)
841 LPWINE_MLD wmld;
843 TRACE("(%p)\n", hMidiIn);
845 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
846 return MMSYSERR_INVALHANDLE;
848 return MMDRV_Message(wmld, MIDM_RESET, 0L, 0L);
851 /**************************************************************************
852 * midiInGetID [WINMM.@]
854 UINT WINAPI midiInGetID(HMIDIIN hMidiIn, UINT* lpuDeviceID)
856 LPWINE_MLD wmld;
858 TRACE("(%p, %p)\n", hMidiIn, lpuDeviceID);
860 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
862 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, TRUE)) == NULL)
863 return MMSYSERR_INVALHANDLE;
865 *lpuDeviceID = wmld->uDeviceID;
867 return MMSYSERR_NOERROR;
870 /**************************************************************************
871 * midiInMessage [WINMM.@]
873 UINT WINAPI midiInMessage(HMIDIIN hMidiIn, UINT uMessage,
874 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
876 LPWINE_MLD wmld;
878 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiIn, uMessage, dwParam1, dwParam2);
880 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
881 return MMSYSERR_INVALHANDLE;
883 switch (uMessage) {
884 case MIDM_OPEN:
885 case MIDM_CLOSE:
886 FIXME("can't handle OPEN or CLOSE message!\n");
887 return MMSYSERR_NOTSUPPORTED;
889 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
892 /**************************************************************************
893 * midiConnect [WINMM.@]
895 MMRESULT WINAPI midiConnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
897 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
898 return MMSYSERR_ERROR;
901 /**************************************************************************
902 * midiDisconnect [WINMM.@]
904 MMRESULT WINAPI midiDisconnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
906 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
907 return MMSYSERR_ERROR;
910 typedef struct WINE_MIDIStream {
911 HMIDIOUT hDevice;
912 HANDLE hThread;
913 DWORD dwThreadID;
914 DWORD dwTempo;
915 DWORD dwTimeDiv;
916 DWORD dwPositionMS;
917 DWORD dwPulses;
918 DWORD dwStartTicks;
919 WORD wFlags;
920 HANDLE hEvent;
921 LPMIDIHDR lpMidiHdr;
922 } WINE_MIDIStream;
924 #define WINE_MSM_HEADER (WM_USER+0)
925 #define WINE_MSM_STOP (WM_USER+1)
926 #define WINE_MSM_PAUSE (WM_USER+2)
927 #define WINE_MSM_RESUME (WM_USER+3)
929 /**************************************************************************
930 * MMSYSTEM_GetMidiStream [internal]
932 static BOOL MMSYSTEM_GetMidiStream(HMIDISTRM hMidiStrm, WINE_MIDIStream** lpMidiStrm, WINE_MIDI** lplpwm)
934 WINE_MIDI* lpwm = (LPWINE_MIDI)MMDRV_Get(hMidiStrm, MMDRV_MIDIOUT, FALSE);
936 if (lplpwm)
937 *lplpwm = lpwm;
939 if (lpwm == NULL) {
940 return FALSE;
943 *lpMidiStrm = (WINE_MIDIStream*)lpwm->mod.rgIds.dwStreamID;
945 return *lpMidiStrm != NULL;
948 /**************************************************************************
949 * MMSYSTEM_MidiStream_Convert [internal]
951 static DWORD MMSYSTEM_MidiStream_Convert(WINE_MIDIStream* lpMidiStrm, DWORD pulse)
953 DWORD ret = 0;
955 if (lpMidiStrm->dwTimeDiv == 0) {
956 FIXME("Shouldn't happen. lpMidiStrm->dwTimeDiv = 0\n");
957 } else if (lpMidiStrm->dwTimeDiv > 0x8000) { /* SMPTE, unchecked FIXME? */
958 int nf = -(char)HIBYTE(lpMidiStrm->dwTimeDiv); /* number of frames */
959 int nsf = LOBYTE(lpMidiStrm->dwTimeDiv); /* number of sub-frames */
960 ret = (pulse * 1000) / (nf * nsf);
961 } else {
962 ret = (DWORD)((double)pulse * ((double)lpMidiStrm->dwTempo / 1000) /
963 (double)lpMidiStrm->dwTimeDiv);
966 return ret;
969 /**************************************************************************
970 * MMSYSTEM_MidiStream_MessageHandler [internal]
972 static BOOL MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream* lpMidiStrm, LPWINE_MIDI lpwm, LPMSG msg)
974 LPMIDIHDR lpMidiHdr;
975 LPMIDIHDR* lpmh;
976 LPBYTE lpData;
977 BOOL paused = FALSE;
979 for (;;) {
980 switch (msg->message) {
981 case WM_QUIT:
982 return FALSE;
983 case WINE_MSM_STOP:
984 TRACE("STOP\n");
985 /* this is not quite what MS doc says... */
986 midiOutReset(lpMidiStrm->hDevice);
987 /* empty list of already submitted buffers */
988 lpMidiHdr = lpMidiStrm->lpMidiHdr;
989 lpMidiStrm->lpMidiHdr = NULL;
990 while (lpMidiHdr) {
991 LPMIDIHDR lphdr = lpMidiHdr;
992 lpMidiHdr = lpMidiHdr->lpNext;
993 lphdr->dwFlags |= MHDR_DONE;
994 lphdr->dwFlags &= ~MHDR_INQUEUE;
996 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
997 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
998 lpwm->mod.dwInstance, (DWORD_PTR)lphdr, 0);
1000 return TRUE;
1001 case WINE_MSM_RESUME:
1002 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1003 lpMidiStrm->dwStartTicks = GetTickCount() - lpMidiStrm->dwPositionMS;
1004 return TRUE;
1005 case WINE_MSM_PAUSE:
1006 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1007 paused = TRUE;
1008 break;
1009 /* FIXME(EPP): "I don't understand the content of the first MIDIHDR sent
1010 * by native mcimidi, it doesn't look like a correct one".
1011 * this trick allows us to throw it away... but I don't like it.
1012 * It looks like part of the file I'm trying to play and definitively looks
1013 * like raw midi content.
1014 * I'd really like to understand why native mcimidi sends it. Perhaps a bad
1015 * synchronization issue where native mcimidi is still processing raw MIDI
1016 * content before generating MIDIEVENTs ?
1018 * 4c 04 89 3b 00 81 7c 99 3b 43 00 99 23 5e 04 89 L..;..|.;C..#^..
1019 * 3b 00 00 89 23 00 7c 99 3b 45 00 99 28 62 04 89 ;...#.|.;E..(b..
1020 * 3b 00 00 89 28 00 81 7c 99 3b 4e 00 99 23 5e 04 ;...(..|.;N..#^.
1021 * 89 3b 00 00 89 23 00 7c 99 3b 45 00 99 23 78 04 .;...#.|.;E..#x.
1022 * 89 3b 00 00 89 23 00 81 7c 99 3b 48 00 99 23 5e .;...#..|.;H..#^
1023 * 04 89 3b 00 00 89 23 00 7c 99 3b 4e 00 99 28 62 ..;...#.|.;N..(b
1024 * 04 89 3b 00 00 89 28 00 81 7c 99 39 4c 00 99 23 ..;...(..|.9L..#
1025 * 5e 04 89 39 00 00 89 23 00 82 7c 99 3b 4c 00 99 ^..9...#..|.;L..
1026 * 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 00 99 #^..;...#.|.;H..
1027 * 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b 3f 04 (b..;...(..|.;?.
1028 * 89 3b 00 1c 99 23 5e 04 89 23 00 5c 99 3b 45 00 .;...#^..#.\.;E.
1029 * 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 3b 46 .#x..;...#..|.;F
1030 * 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 ..#^..;...#.|.;H
1031 * 00 99 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b ..(b..;...(..|.;
1032 * 46 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b F..#^..;...#.|.;
1033 * 48 00 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 H..#x..;...#..|.
1034 * 3b 4c 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 ;L..#^..;...#.|.
1036 case WINE_MSM_HEADER:
1037 /* sets initial tick count for first MIDIHDR */
1038 if (!lpMidiStrm->dwStartTicks)
1039 lpMidiStrm->dwStartTicks = GetTickCount();
1040 lpMidiHdr = (LPMIDIHDR)msg->lParam;
1041 lpData = (LPBYTE)lpMidiHdr->lpData;
1042 TRACE("Adding %s lpMidiHdr=%p [lpData=0x%p dwBytesRecorded=%u/%u dwFlags=0x%08x size=%lu]\n",
1043 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular", lpMidiHdr,
1044 lpData, lpMidiHdr->dwBytesRecorded, lpMidiHdr->dwBufferLength,
1045 lpMidiHdr->dwFlags, msg->wParam);
1046 #if 0
1047 /* dumps content of lpMidiHdr->lpData
1048 * FIXME: there should be a debug routine somewhere that already does this
1050 for (dwToGo = 0; dwToGo < lpMidiHdr->dwBufferLength; dwToGo += 16) {
1051 DWORD i;
1052 BYTE ch;
1054 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++)
1055 printf("%02x ", lpData[dwToGo + i]);
1056 for (; i < 16; i++)
1057 printf(" ");
1058 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++) {
1059 ch = lpData[dwToGo + i];
1060 printf("%c", (ch >= 0x20 && ch <= 0x7F) ? ch : '.');
1062 printf("\n");
1064 #endif
1065 if (((LPMIDIEVENT)lpData)->dwStreamID != 0 &&
1066 ((LPMIDIEVENT)lpData)->dwStreamID != 0xFFFFFFFF &&
1067 ((LPMIDIEVENT)lpData)->dwStreamID != (DWORD)lpMidiStrm) {
1068 FIXME("Dropping bad %s lpMidiHdr (streamID=%08x)\n",
1069 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular",
1070 ((LPMIDIEVENT)lpData)->dwStreamID);
1071 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1072 lpMidiHdr->dwFlags |= MHDR_DONE;
1074 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1075 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1076 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1077 break;
1080 lpMidiHdr->lpNext = 0;
1081 for (lpmh = &lpMidiStrm->lpMidiHdr; *lpmh; lpmh = &(*lpmh)->lpNext);
1082 *lpmh = lpMidiHdr;
1083 break;
1084 default:
1085 FIXME("Unknown message %d\n", msg->message);
1086 break;
1088 if (!paused)
1089 return TRUE;
1090 GetMessageA(msg, 0, 0, 0);
1094 /**************************************************************************
1095 * MMSYSTEM_MidiStream_Player [internal]
1097 static DWORD CALLBACK MMSYSTEM_MidiStream_Player(LPVOID pmt)
1099 WINE_MIDIStream* lpMidiStrm = pmt;
1100 WINE_MIDI* lpwm;
1101 MSG msg;
1102 DWORD dwToGo;
1103 DWORD dwCurrTC;
1104 LPMIDIHDR lpMidiHdr;
1105 DWORD dwOffset;
1107 TRACE("(%p)!\n", lpMidiStrm);
1109 if (!lpMidiStrm ||
1110 (lpwm = (LPWINE_MIDI)MMDRV_Get(lpMidiStrm->hDevice, MMDRV_MIDIOUT, FALSE)) == NULL)
1111 goto the_end;
1113 /* force thread's queue creation */
1114 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
1116 lpMidiStrm->dwStartTicks = 0;
1117 lpMidiStrm->dwPulses = 0;
1119 lpMidiStrm->lpMidiHdr = 0;
1121 /* midiStreamOpen is waiting for ack */
1122 SetEvent(lpMidiStrm->hEvent);
1124 start_header:
1125 lpMidiHdr = lpMidiStrm->lpMidiHdr;
1126 if (!lpMidiHdr) {
1127 /* for first message, block until one arrives, then process all that are available */
1128 GetMessageA(&msg, 0, 0, 0);
1129 do {
1130 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1131 goto the_end;
1132 } while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE));
1133 goto start_header;
1136 dwOffset = 0;
1137 while (dwOffset + offsetof(MIDIEVENT,dwParms) <= lpMidiHdr->dwBytesRecorded) {
1138 LPMIDIEVENT me = (LPMIDIEVENT)(lpMidiHdr->lpData+dwOffset);
1140 /* do we have to wait ? */
1141 if (me->dwDeltaTime) {
1142 lpMidiStrm->dwPositionMS += MMSYSTEM_MidiStream_Convert(lpMidiStrm, me->dwDeltaTime);
1143 lpMidiStrm->dwPulses += me->dwDeltaTime;
1145 dwToGo = lpMidiStrm->dwStartTicks + lpMidiStrm->dwPositionMS;
1147 TRACE("%u/%u/%u\n", dwToGo, GetTickCount(), me->dwDeltaTime);
1148 while (dwToGo - (dwCurrTC = GetTickCount()) <= MAXLONG) {
1149 if (MsgWaitForMultipleObjects(0, NULL, FALSE, dwToGo - dwCurrTC, QS_ALLINPUT) == WAIT_OBJECT_0) {
1150 /* got a message, handle it */
1151 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
1152 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1153 goto the_end;
1154 /* is lpMidiHdr still current? */
1155 if (lpMidiHdr != lpMidiStrm->lpMidiHdr) {
1156 goto start_header;
1159 } else {
1160 /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
1161 break;
1165 switch (MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK)) {
1166 case MEVT_COMMENT:
1167 FIXME("NIY: MEVT_COMMENT\n");
1168 /* do nothing, skip bytes */
1169 break;
1170 case MEVT_LONGMSG:
1171 midiOutLongMsg(lpMidiStrm->hDevice, lpMidiStrm->lpMidiHdr, MEVT_EVENTPARM(me->dwEvent));
1172 break;
1173 case MEVT_NOP:
1174 break;
1175 case MEVT_SHORTMSG:
1176 midiOutShortMsg(lpMidiStrm->hDevice, MEVT_EVENTPARM(me->dwEvent));
1177 break;
1178 case MEVT_TEMPO:
1179 lpMidiStrm->dwTempo = MEVT_EVENTPARM(me->dwEvent);
1180 break;
1181 case MEVT_VERSION:
1182 break;
1183 default:
1184 FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK));
1185 break;
1187 if (me->dwEvent & MEVT_F_CALLBACK) {
1188 /* native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
1189 lpMidiHdr->dwOffset = dwOffset;
1190 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1191 (HDRVR)lpMidiStrm->hDevice, MM_MOM_POSITIONCB,
1192 lpwm->mod.dwInstance, (LPARAM)lpMidiHdr, 0L);
1194 dwOffset += offsetof(MIDIEVENT,dwParms);
1195 if (me->dwEvent & MEVT_F_LONG)
1196 dwOffset += (MEVT_EVENTPARM(me->dwEvent) + 3) & ~3;
1198 /* done with this header */
1199 lpMidiStrm->lpMidiHdr = lpMidiHdr->lpNext;
1200 lpMidiHdr->dwFlags |= MHDR_DONE;
1201 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1203 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1204 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1205 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1206 goto start_header;
1208 the_end:
1209 TRACE("End of thread\n");
1210 return 0;
1213 /**************************************************************************
1214 * midiStreamClose [WINMM.@]
1216 MMRESULT WINAPI midiStreamClose(HMIDISTRM hMidiStrm)
1218 WINE_MIDIStream* lpMidiStrm;
1219 MMRESULT ret = 0;
1221 TRACE("(%p)!\n", hMidiStrm);
1223 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1224 return MMSYSERR_INVALHANDLE;
1226 midiStreamStop(hMidiStrm);
1227 PostThreadMessageA(lpMidiStrm->dwThreadID, WM_QUIT, 0, 0);
1228 CloseHandle(lpMidiStrm->hEvent);
1229 if (lpMidiStrm->hThread) {
1230 if (GetCurrentThreadId() != lpMidiStrm->dwThreadID)
1231 WaitForSingleObject(lpMidiStrm->hThread, INFINITE);
1232 else {
1233 FIXME("leak from call within function callback\n");
1234 ret = MMSYSERR_HANDLEBUSY; /* yet don't signal it to app */
1236 CloseHandle(lpMidiStrm->hThread);
1238 if(!ret)
1239 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1241 return midiOutClose((HMIDIOUT)hMidiStrm);
1244 /**************************************************************************
1245 * midiStreamOpen [WINMM.@]
1247 MMRESULT WINAPI midiStreamOpen(HMIDISTRM* lphMidiStrm, LPUINT lpuDeviceID,
1248 DWORD cMidi, DWORD_PTR dwCallback,
1249 DWORD_PTR dwInstance, DWORD fdwOpen)
1251 WINE_MIDIStream* lpMidiStrm;
1252 MMRESULT ret;
1253 MIDIOPENSTRMID mosm;
1254 LPWINE_MIDI lpwm;
1255 HMIDIOUT hMidiOut;
1257 TRACE("(%p, %p, %d, 0x%08lx, 0x%08lx, 0x%08x)!\n",
1258 lphMidiStrm, lpuDeviceID, cMidi, dwCallback, dwInstance, fdwOpen);
1260 if (cMidi != 1 || lphMidiStrm == NULL || lpuDeviceID == NULL)
1261 return MMSYSERR_INVALPARAM;
1263 ret = WINMM_CheckCallback(dwCallback, fdwOpen, FALSE);
1264 if (ret != MMSYSERR_NOERROR)
1265 return ret;
1267 lpMidiStrm = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream));
1268 if (!lpMidiStrm)
1269 return MMSYSERR_NOMEM;
1271 lpMidiStrm->dwTempo = 500000;
1272 lpMidiStrm->dwTimeDiv = 480; /* 480 is 120 quarter notes per minute *//* FIXME ??*/
1273 lpMidiStrm->dwPositionMS = 0;
1275 mosm.dwStreamID = (DWORD)lpMidiStrm;
1276 /* FIXME: the correct value is not allocated yet for MAPPER */
1277 mosm.wDeviceID = *lpuDeviceID;
1278 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &fdwOpen, 1, &mosm);
1279 if (!lpwm) {
1280 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1281 return MMSYSERR_NOMEM;
1283 lpMidiStrm->hDevice = hMidiOut;
1284 *lphMidiStrm = (HMIDISTRM)hMidiOut;
1286 lpwm->mld.uDeviceID = *lpuDeviceID;
1288 ret = MMDRV_Open(&lpwm->mld, MODM_OPEN, (DWORD_PTR)&lpwm->mod, fdwOpen);
1289 if (ret != MMSYSERR_NOERROR) {
1290 MMDRV_Free(hMidiOut, &lpwm->mld);
1291 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1292 return ret;
1295 lpMidiStrm->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1296 lpMidiStrm->wFlags = HIWORD(fdwOpen);
1298 lpMidiStrm->hThread = CreateThread(NULL, 0, MMSYSTEM_MidiStream_Player,
1299 lpMidiStrm, 0, &(lpMidiStrm->dwThreadID));
1301 if (!lpMidiStrm->hThread) {
1302 midiStreamClose((HMIDISTRM)hMidiOut);
1303 return MMSYSERR_NOMEM;
1305 SetThreadPriority(lpMidiStrm->hThread, THREAD_PRIORITY_TIME_CRITICAL);
1307 /* wait for thread to have started, and for its queue to be created */
1308 WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
1310 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1312 TRACE("=> (%u/%d) hMidi=%p ret=%d lpMidiStrm=%p\n",
1313 *lpuDeviceID, lpwm->mld.uDeviceID, *lphMidiStrm, ret, lpMidiStrm);
1314 return ret;
1317 /**************************************************************************
1318 * midiStreamOut [WINMM.@]
1320 MMRESULT WINAPI midiStreamOut(HMIDISTRM hMidiStrm, LPMIDIHDR lpMidiHdr,
1321 UINT cbMidiHdr)
1323 WINE_MIDIStream* lpMidiStrm;
1324 DWORD ret = MMSYSERR_NOERROR;
1326 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMidiHdr, cbMidiHdr);
1328 if (cbMidiHdr < offsetof(MIDIHDR,dwOffset) || !lpMidiHdr || !lpMidiHdr->lpData
1329 || lpMidiHdr->dwBufferLength < lpMidiHdr->dwBytesRecorded
1330 || lpMidiHdr->dwBytesRecorded % 4 /* player expects DWORD padding */)
1331 return MMSYSERR_INVALPARAM;
1332 /* FIXME: Native additionally checks if the MIDIEVENTs in lpData
1333 * exactly fit dwBytesRecorded. */
1335 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1336 return MIDIERR_UNPREPARED;
1338 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1339 return MIDIERR_STILLPLAYING;
1341 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1342 ret = MMSYSERR_INVALHANDLE;
1343 } else {
1344 lpMidiHdr->dwFlags |= MHDR_ISSTRM | MHDR_INQUEUE;
1345 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1346 if (!PostThreadMessageA(lpMidiStrm->dwThreadID,
1347 WINE_MSM_HEADER, cbMidiHdr,
1348 (LPARAM)lpMidiHdr)) {
1349 ERR("bad PostThreadMessageA\n");
1350 ret = MMSYSERR_ERROR;
1353 return ret;
1356 /**************************************************************************
1357 * midiStreamPause [WINMM.@]
1359 MMRESULT WINAPI midiStreamPause(HMIDISTRM hMidiStrm)
1361 WINE_MIDIStream* lpMidiStrm;
1362 DWORD ret = MMSYSERR_NOERROR;
1364 TRACE("(%p)!\n", hMidiStrm);
1366 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1367 return MMSYSERR_INVALHANDLE;
1368 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1369 return ret;
1372 /**************************************************************************
1373 * midiStreamPosition [WINMM.@]
1375 MMRESULT WINAPI midiStreamPosition(HMIDISTRM hMidiStrm, LPMMTIME lpMMT, UINT cbmmt)
1377 WINE_MIDIStream* lpMidiStrm;
1378 DWORD ret = MMSYSERR_NOERROR;
1380 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMMT, cbmmt);
1382 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1383 ret = MMSYSERR_INVALHANDLE;
1384 } else if (lpMMT == NULL || cbmmt != sizeof(MMTIME)) {
1385 ret = MMSYSERR_INVALPARAM;
1386 } else {
1387 switch (lpMMT->wType) {
1388 default:
1389 FIXME("Unsupported time type %x\n", lpMMT->wType);
1390 /* fall through */
1391 case TIME_BYTES:
1392 case TIME_SAMPLES:
1393 lpMMT->wType = TIME_MS;
1394 /* fall through to alternative format */
1395 case TIME_MS:
1396 lpMMT->u.ms = lpMidiStrm->dwPositionMS;
1397 TRACE("=> %d ms\n", lpMMT->u.ms);
1398 break;
1399 case TIME_TICKS:
1400 lpMMT->u.ticks = lpMidiStrm->dwPulses;
1401 TRACE("=> %d ticks\n", lpMMT->u.ticks);
1402 break;
1405 return ret;
1408 /**************************************************************************
1409 * midiStreamProperty [WINMM.@]
1411 MMRESULT WINAPI midiStreamProperty(HMIDISTRM hMidiStrm, LPBYTE lpPropData, DWORD dwProperty)
1413 WINE_MIDIStream* lpMidiStrm;
1414 MMRESULT ret = MMSYSERR_NOERROR;
1416 TRACE("(%p, %p, %x)\n", hMidiStrm, lpPropData, dwProperty);
1418 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1419 ret = MMSYSERR_INVALHANDLE;
1420 } else if ((dwProperty & (MIDIPROP_GET|MIDIPROP_SET)) == 0) {
1421 ret = MMSYSERR_INVALPARAM;
1422 } else if (dwProperty & MIDIPROP_TEMPO) {
1423 MIDIPROPTEMPO* mpt = (MIDIPROPTEMPO*)lpPropData;
1425 if (sizeof(MIDIPROPTEMPO) != mpt->cbStruct) {
1426 ret = MMSYSERR_INVALPARAM;
1427 } else if (dwProperty & MIDIPROP_SET) {
1428 lpMidiStrm->dwTempo = mpt->dwTempo;
1429 TRACE("Setting tempo to %d\n", mpt->dwTempo);
1430 } else if (dwProperty & MIDIPROP_GET) {
1431 mpt->dwTempo = lpMidiStrm->dwTempo;
1432 TRACE("Getting tempo <= %d\n", mpt->dwTempo);
1434 } else if (dwProperty & MIDIPROP_TIMEDIV) {
1435 MIDIPROPTIMEDIV* mptd = (MIDIPROPTIMEDIV*)lpPropData;
1437 if (sizeof(MIDIPROPTIMEDIV) != mptd->cbStruct) {
1438 ret = MMSYSERR_INVALPARAM;
1439 } else if (dwProperty & MIDIPROP_SET) {
1440 lpMidiStrm->dwTimeDiv = mptd->dwTimeDiv;
1441 TRACE("Setting time div to %d\n", mptd->dwTimeDiv);
1442 } else if (dwProperty & MIDIPROP_GET) {
1443 mptd->dwTimeDiv = lpMidiStrm->dwTimeDiv;
1444 TRACE("Getting time div <= %d\n", mptd->dwTimeDiv);
1446 } else {
1447 ret = MMSYSERR_INVALPARAM;
1450 return ret;
1453 /**************************************************************************
1454 * midiStreamRestart [WINMM.@]
1456 MMRESULT WINAPI midiStreamRestart(HMIDISTRM hMidiStrm)
1458 WINE_MIDIStream* lpMidiStrm;
1459 MMRESULT ret = MMSYSERR_NOERROR;
1461 TRACE("(%p)!\n", hMidiStrm);
1463 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1464 return MMSYSERR_INVALHANDLE;
1465 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_RESUME, 0, 0);
1466 return ret;
1469 /**************************************************************************
1470 * midiStreamStop [WINMM.@]
1472 MMRESULT WINAPI midiStreamStop(HMIDISTRM hMidiStrm)
1474 WINE_MIDIStream* lpMidiStrm;
1475 MMRESULT ret = MMSYSERR_NOERROR;
1477 TRACE("(%p)!\n", hMidiStrm);
1479 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1480 return MMSYSERR_INVALHANDLE;
1481 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_STOP, 0, 0);
1482 return ret;
1485 struct mm_starter
1487 LPTASKCALLBACK cb;
1488 DWORD client;
1489 HANDLE event;
1492 static DWORD WINAPI mmTaskRun(void* pmt)
1494 struct mm_starter mms;
1496 memcpy(&mms, pmt, sizeof(struct mm_starter));
1497 HeapFree(GetProcessHeap(), 0, pmt);
1498 mms.cb(mms.client);
1499 if (mms.event) SetEvent(mms.event);
1500 return 0;
1503 /******************************************************************
1504 * mmTaskCreate (WINMM.@)
1506 UINT WINAPI mmTaskCreate(LPTASKCALLBACK cb, HANDLE* ph, DWORD_PTR client)
1508 HANDLE hThread;
1509 HANDLE hEvent = 0;
1510 struct mm_starter *mms;
1512 mms = HeapAlloc(GetProcessHeap(), 0, sizeof(struct mm_starter));
1513 if (mms == NULL) return TASKERR_OUTOFMEMORY;
1515 mms->cb = cb;
1516 mms->client = client;
1517 if (ph) hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1518 mms->event = hEvent;
1520 hThread = CreateThread(0, 0, mmTaskRun, mms, 0, NULL);
1521 if (!hThread) {
1522 HeapFree(GetProcessHeap(), 0, mms);
1523 if (hEvent) CloseHandle(hEvent);
1524 return TASKERR_OUTOFMEMORY;
1526 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
1527 if (ph) *ph = hEvent;
1528 CloseHandle(hThread);
1529 return 0;
1532 /******************************************************************
1533 * mmTaskBlock (WINMM.@)
1535 VOID WINAPI mmTaskBlock(DWORD tid)
1537 MSG msg;
1541 GetMessageA(&msg, 0, 0, 0);
1542 if (msg.hwnd) DispatchMessageA(&msg);
1543 } while (msg.message != WM_USER);
1546 /******************************************************************
1547 * mmTaskSignal (WINMM.@)
1549 BOOL WINAPI mmTaskSignal(DWORD tid)
1551 return PostThreadMessageW(tid, WM_USER, 0, 0);
1554 /******************************************************************
1555 * mmTaskYield (WINMM.@)
1557 VOID WINAPI mmTaskYield(VOID) {}
1559 /******************************************************************
1560 * mmGetCurrentTask (WINMM.@)
1562 DWORD WINAPI mmGetCurrentTask(VOID)
1564 return GetCurrentThreadId();