wevtapi: Add EvtOpenSession stub.
[wine.git] / dlls / winmm / winmm.c
blob1412d5ec41408b1119bd31c500b095e68fac54e2
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 psLastEvent != NULL;
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;
143 break;
144 case DLL_PROCESS_DETACH:
145 if(fImpLoad)
146 break;
148 MCI_SendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0L);
149 MMDRV_Exit();
150 DRIVER_UnloadAll();
151 WINMM_DeleteWaveform();
152 TIME_MMTimeStop();
153 CloseHandle(psLastEvent);
154 DeleteCriticalSection(&WINMM_cs);
155 break;
157 return TRUE;
160 /**************************************************************************
161 * WINMM_CheckCallback [internal]
163 MMRESULT WINMM_CheckCallback(DWORD_PTR dwCallback, DWORD fdwOpen, BOOL mixer)
165 switch (fdwOpen & CALLBACK_TYPEMASK) {
166 case CALLBACK_NULL: /* dwCallback need not be NULL */
167 break;
168 case CALLBACK_WINDOW:
169 if (dwCallback && !IsWindow((HWND)dwCallback))
170 return MMSYSERR_INVALPARAM;
171 break;
173 case CALLBACK_FUNCTION:
174 /* a NULL cb is acceptable since w2k, MMSYSERR_INVALPARAM earlier */
175 if (mixer)
176 return MMSYSERR_INVALFLAG; /* since w2k, MMSYSERR_NOTSUPPORTED earlier */
177 break;
178 case CALLBACK_THREAD:
179 case CALLBACK_EVENT:
180 if (mixer) /* FIXME: mixer supports THREAD+EVENT since w2k */
181 return MMSYSERR_NOTSUPPORTED; /* w9X */
182 break;
183 default:
184 WARN("Unknown callback type %d\n", HIWORD(fdwOpen));
186 return MMSYSERR_NOERROR;
189 /**************************************************************************
190 * auxGetNumDevs [WINMM.@]
192 UINT WINAPI auxGetNumDevs(void)
194 return MMDRV_GetNum(MMDRV_AUX);
197 /**************************************************************************
198 * auxGetDevCapsW [WINMM.@]
200 UINT WINAPI auxGetDevCapsW(UINT_PTR uDeviceID, LPAUXCAPSW lpCaps, UINT uSize)
202 LPWINE_MLD wmld;
204 TRACE("(%04lX, %p, %d) !\n", uDeviceID, lpCaps, uSize);
206 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
208 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
209 return MMSYSERR_BADDEVICEID;
210 return MMDRV_Message(wmld, AUXDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
213 /**************************************************************************
214 * auxGetDevCapsA [WINMM.@]
216 UINT WINAPI auxGetDevCapsA(UINT_PTR uDeviceID, LPAUXCAPSA lpCaps, UINT uSize)
218 AUXCAPSW acW;
219 UINT ret;
221 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
223 ret = auxGetDevCapsW(uDeviceID, &acW, sizeof(acW));
225 if (ret == MMSYSERR_NOERROR) {
226 AUXCAPSA acA;
227 acA.wMid = acW.wMid;
228 acA.wPid = acW.wPid;
229 acA.vDriverVersion = acW.vDriverVersion;
230 WideCharToMultiByte( CP_ACP, 0, acW.szPname, -1, acA.szPname,
231 sizeof(acA.szPname), NULL, NULL );
232 acA.wTechnology = acW.wTechnology;
233 acA.wReserved1 = acW.wReserved1;
234 acA.dwSupport = acW.dwSupport;
235 memcpy(lpCaps, &acA, min(uSize, sizeof(acA)));
237 return ret;
240 /**************************************************************************
241 * auxGetVolume [WINMM.@]
243 UINT WINAPI auxGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
245 LPWINE_MLD wmld;
247 TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);
249 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
250 return MMSYSERR_INVALHANDLE;
251 return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
254 /**************************************************************************
255 * auxSetVolume [WINMM.@]
257 UINT WINAPI auxSetVolume(UINT uDeviceID, DWORD dwVolume)
259 LPWINE_MLD wmld;
261 TRACE("(%04X, %u) !\n", uDeviceID, dwVolume);
263 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
264 return MMSYSERR_INVALHANDLE;
265 return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L);
268 /**************************************************************************
269 * auxOutMessage [WINMM.@]
271 UINT WINAPI auxOutMessage(UINT uDeviceID, UINT uMessage, DWORD_PTR dw1, DWORD_PTR dw2)
273 LPWINE_MLD wmld;
275 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
276 return MMSYSERR_INVALHANDLE;
278 return MMDRV_Message(wmld, uMessage, dw1, dw2);
281 /**************************************************************************
282 * midiOutGetNumDevs [WINMM.@]
284 UINT WINAPI midiOutGetNumDevs(void)
286 return MMDRV_GetNum(MMDRV_MIDIOUT);
289 /**************************************************************************
290 * midiOutGetDevCapsW [WINMM.@]
292 UINT WINAPI midiOutGetDevCapsW(UINT_PTR uDeviceID, LPMIDIOUTCAPSW lpCaps,
293 UINT uSize)
295 LPWINE_MLD wmld;
297 TRACE("(%lu, %p, %u);\n", uDeviceID, lpCaps, uSize);
299 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
301 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL)
302 return MMSYSERR_BADDEVICEID;
304 return MMDRV_Message(wmld, MODM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
307 /**************************************************************************
308 * midiOutGetDevCapsA [WINMM.@]
310 UINT WINAPI midiOutGetDevCapsA(UINT_PTR uDeviceID, LPMIDIOUTCAPSA lpCaps,
311 UINT uSize)
313 MIDIOUTCAPSW mocW;
314 UINT ret;
316 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
318 ret = midiOutGetDevCapsW(uDeviceID, &mocW, sizeof(mocW));
320 if (ret == MMSYSERR_NOERROR) {
321 MIDIOUTCAPSA mocA;
322 mocA.wMid = mocW.wMid;
323 mocA.wPid = mocW.wPid;
324 mocA.vDriverVersion = mocW.vDriverVersion;
325 WideCharToMultiByte( CP_ACP, 0, mocW.szPname, -1, mocA.szPname,
326 sizeof(mocA.szPname), NULL, NULL );
327 mocA.wTechnology = mocW.wTechnology;
328 mocA.wVoices = mocW.wVoices;
329 mocA.wNotes = mocW.wNotes;
330 mocA.wChannelMask = mocW.wChannelMask;
331 mocA.dwSupport = mocW.dwSupport;
332 memcpy(lpCaps, &mocA, min(uSize, sizeof(mocA)));
334 return ret;
337 /**************************************************************************
338 * midiOutGetErrorTextA [WINMM.@]
339 * midiInGetErrorTextA [WINMM.@]
341 UINT WINAPI midiOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
343 UINT ret;
345 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
346 else if (uSize == 0) ret = MMSYSERR_NOERROR;
347 else
349 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
350 if (!xstr) ret = MMSYSERR_NOMEM;
351 else
353 ret = midiOutGetErrorTextW(uError, xstr, uSize);
354 if (ret == MMSYSERR_NOERROR)
355 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
356 HeapFree(GetProcessHeap(), 0, xstr);
359 return ret;
362 /**************************************************************************
363 * midiOutGetErrorTextW [WINMM.@]
364 * midiInGetErrorTextW [WINMM.@]
366 UINT WINAPI midiOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
368 UINT ret = MMSYSERR_BADERRNUM;
370 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
371 else if (uSize == 0) ret = MMSYSERR_NOERROR;
372 else if (
373 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
374 * a warning for the test was always true */
375 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
376 (uError >= MIDIERR_BASE && uError <= MIDIERR_LASTERROR)) {
377 if (LoadStringW(hWinMM32Instance, uError, lpText, uSize) > 0) {
378 ret = MMSYSERR_NOERROR;
381 return ret;
384 /**************************************************************************
385 * MIDI_OutAlloc [internal]
387 static LPWINE_MIDI MIDI_OutAlloc(HMIDIOUT* lphMidiOut, DWORD_PTR* lpdwCallback,
388 DWORD_PTR* lpdwInstance, LPDWORD lpdwFlags,
389 DWORD cIDs, MIDIOPENSTRMID* lpIDs)
391 HANDLE hMidiOut;
392 LPWINE_MIDI lpwm;
393 UINT size;
395 size = sizeof(WINE_MIDI) + (cIDs ? (cIDs-1) : 0) * sizeof(MIDIOPENSTRMID);
397 lpwm = (LPWINE_MIDI)MMDRV_Alloc(size, MMDRV_MIDIOUT, &hMidiOut, lpdwFlags,
398 lpdwCallback, lpdwInstance);
399 *lphMidiOut = hMidiOut;
401 if (lpwm) {
402 lpwm->mod.hMidi = hMidiOut;
403 lpwm->mod.dwCallback = *lpdwCallback;
404 lpwm->mod.dwInstance = *lpdwInstance;
405 lpwm->mod.dnDevNode = 0;
406 lpwm->mod.cIds = cIDs;
407 if (cIDs)
408 memcpy(&(lpwm->mod.rgIds), lpIDs, cIDs * sizeof(MIDIOPENSTRMID));
410 return lpwm;
413 /**************************************************************************
414 * midiOutOpen [WINMM.@]
416 MMRESULT WINAPI midiOutOpen(LPHMIDIOUT lphMidiOut, UINT uDeviceID,
417 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
419 HMIDIOUT hMidiOut;
420 LPWINE_MIDI lpwm;
421 MMRESULT dwRet;
423 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
424 lphMidiOut, uDeviceID, dwCallback, dwInstance, dwFlags);
426 if (lphMidiOut != NULL) *lphMidiOut = 0;
428 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
429 if (dwRet != MMSYSERR_NOERROR)
430 return dwRet;
432 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &dwFlags, 0, NULL);
434 if (lpwm == NULL)
435 return MMSYSERR_NOMEM;
437 lpwm->mld.uDeviceID = uDeviceID;
439 dwRet = MMDRV_Open((LPWINE_MLD)lpwm, MODM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
441 if (dwRet != MMSYSERR_NOERROR) {
442 MMDRV_Free(hMidiOut, (LPWINE_MLD)lpwm);
443 hMidiOut = 0;
446 if (lphMidiOut) *lphMidiOut = hMidiOut;
447 TRACE("=> %d hMidi=%p\n", dwRet, hMidiOut);
449 return dwRet;
452 /**************************************************************************
453 * midiOutClose [WINMM.@]
455 UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
457 LPWINE_MLD wmld;
458 DWORD dwRet;
460 TRACE("(%p)\n", hMidiOut);
462 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
463 return MMSYSERR_INVALHANDLE;
465 dwRet = MMDRV_Close(wmld, MODM_CLOSE);
466 MMDRV_Free(hMidiOut, wmld);
468 return dwRet;
471 /**************************************************************************
472 * midiOutPrepareHeader [WINMM.@]
474 UINT WINAPI midiOutPrepareHeader(HMIDIOUT hMidiOut,
475 MIDIHDR* lpMidiOutHdr, UINT uSize)
477 LPWINE_MLD wmld;
479 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
481 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
482 return MMSYSERR_INVALHANDLE;
483 /* FIXME: detect MIDIStream handles and enforce 64KB buffer limit on those */
485 return MMDRV_Message(wmld, MODM_PREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
488 /**************************************************************************
489 * midiOutUnprepareHeader [WINMM.@]
491 UINT WINAPI midiOutUnprepareHeader(HMIDIOUT hMidiOut,
492 MIDIHDR* lpMidiOutHdr, UINT uSize)
494 LPWINE_MLD wmld;
496 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
498 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
499 return MMSYSERR_INVALHANDLE;
501 return MMDRV_Message(wmld, MODM_UNPREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
504 /**************************************************************************
505 * midiOutShortMsg [WINMM.@]
507 UINT WINAPI midiOutShortMsg(HMIDIOUT hMidiOut, DWORD dwMsg)
509 LPWINE_MLD wmld;
511 TRACE("(%p, %08X)\n", hMidiOut, dwMsg);
513 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
514 return MMSYSERR_INVALHANDLE;
516 return MMDRV_Message(wmld, MODM_DATA, dwMsg, 0L);
519 /**************************************************************************
520 * midiOutLongMsg [WINMM.@]
522 UINT WINAPI midiOutLongMsg(HMIDIOUT hMidiOut,
523 MIDIHDR* lpMidiOutHdr, UINT uSize)
525 LPWINE_MLD wmld;
527 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
529 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
530 return MMSYSERR_INVALHANDLE;
532 return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD_PTR)lpMidiOutHdr, uSize);
535 /**************************************************************************
536 * midiOutReset [WINMM.@]
538 UINT WINAPI midiOutReset(HMIDIOUT hMidiOut)
540 LPWINE_MLD wmld;
542 TRACE("(%p)\n", hMidiOut);
544 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
545 return MMSYSERR_INVALHANDLE;
547 return MMDRV_Message(wmld, MODM_RESET, 0L, 0L);
550 /**************************************************************************
551 * midiOutGetVolume [WINMM.@]
553 UINT WINAPI midiOutGetVolume(HMIDIOUT hMidiOut, DWORD* lpdwVolume)
555 LPWINE_MLD wmld;
557 TRACE("(%p, %p);\n", hMidiOut, lpdwVolume);
559 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
560 return MMSYSERR_INVALHANDLE;
562 return MMDRV_Message(wmld, MODM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
565 /**************************************************************************
566 * midiOutSetVolume [WINMM.@]
568 UINT WINAPI midiOutSetVolume(HMIDIOUT hMidiOut, DWORD dwVolume)
570 LPWINE_MLD wmld;
572 TRACE("(%p, %d);\n", hMidiOut, dwVolume);
574 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
575 return MMSYSERR_INVALHANDLE;
577 return MMDRV_Message(wmld, MODM_SETVOLUME, dwVolume, 0L);
580 /**************************************************************************
581 * midiOutCachePatches [WINMM.@]
583 UINT WINAPI midiOutCachePatches(HMIDIOUT hMidiOut, UINT uBank,
584 WORD* lpwPatchArray, UINT uFlags)
586 /* not really necessary to support this */
587 FIXME("(%p, %u, %p, %x): Stub\n", hMidiOut, uBank, lpwPatchArray, uFlags);
588 return MMSYSERR_NOTSUPPORTED;
591 /**************************************************************************
592 * midiOutCacheDrumPatches [WINMM.@]
594 UINT WINAPI midiOutCacheDrumPatches(HMIDIOUT hMidiOut, UINT uPatch,
595 WORD* lpwKeyArray, UINT uFlags)
597 FIXME("(%p, %u, %p, %x): Stub\n", hMidiOut, uPatch, lpwKeyArray, uFlags);
598 return MMSYSERR_NOTSUPPORTED;
601 /**************************************************************************
602 * midiOutGetID [WINMM.@]
604 UINT WINAPI midiOutGetID(HMIDIOUT hMidiOut, UINT* lpuDeviceID)
606 LPWINE_MLD wmld;
608 TRACE("(%p, %p)\n", hMidiOut, lpuDeviceID);
610 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
611 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
612 return MMSYSERR_INVALHANDLE;
614 *lpuDeviceID = wmld->uDeviceID;
615 return MMSYSERR_NOERROR;
618 /**************************************************************************
619 * midiOutMessage [WINMM.@]
621 UINT WINAPI midiOutMessage(HMIDIOUT hMidiOut, UINT uMessage,
622 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
624 LPWINE_MLD wmld;
626 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
628 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) {
629 /* HACK... */
630 if (uMessage == 0x0001) {
631 *(LPDWORD)dwParam1 = 1;
632 return 0;
634 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) != NULL) {
635 return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
637 return MMSYSERR_INVALHANDLE;
640 switch (uMessage) {
641 case MODM_OPEN:
642 case MODM_CLOSE:
643 FIXME("can't handle OPEN or CLOSE message!\n");
644 return MMSYSERR_NOTSUPPORTED;
646 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
649 /**************************************************************************
650 * midiInGetNumDevs [WINMM.@]
652 UINT WINAPI midiInGetNumDevs(void)
654 return MMDRV_GetNum(MMDRV_MIDIIN);
657 /**************************************************************************
658 * midiInGetDevCapsW [WINMM.@]
660 UINT WINAPI midiInGetDevCapsW(UINT_PTR uDeviceID, LPMIDIINCAPSW lpCaps, UINT uSize)
662 LPWINE_MLD wmld;
664 TRACE("(%ld, %p, %d);\n", uDeviceID, lpCaps, uSize);
666 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
668 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIIN, TRUE)) == NULL)
669 return MMSYSERR_BADDEVICEID;
671 return MMDRV_Message(wmld, MIDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
674 /**************************************************************************
675 * midiInGetDevCapsA [WINMM.@]
677 UINT WINAPI midiInGetDevCapsA(UINT_PTR uDeviceID, LPMIDIINCAPSA lpCaps, UINT uSize)
679 MIDIINCAPSW micW;
680 UINT ret;
682 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
684 ret = midiInGetDevCapsW(uDeviceID, &micW, sizeof(micW));
686 if (ret == MMSYSERR_NOERROR) {
687 MIDIINCAPSA micA;
688 micA.wMid = micW.wMid;
689 micA.wPid = micW.wPid;
690 micA.vDriverVersion = micW.vDriverVersion;
691 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
692 sizeof(micA.szPname), NULL, NULL );
693 micA.dwSupport = micW.dwSupport;
694 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
696 return ret;
699 /**************************************************************************
700 * midiInOpen [WINMM.@]
702 MMRESULT WINAPI midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID,
703 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
705 HANDLE hMidiIn;
706 LPWINE_MIDI lpwm;
707 MMRESULT dwRet;
709 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
710 lphMidiIn, uDeviceID, dwCallback, dwInstance, dwFlags);
712 if (lphMidiIn != NULL) *lphMidiIn = 0;
714 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
715 if (dwRet != MMSYSERR_NOERROR)
716 return dwRet;
718 lpwm = (LPWINE_MIDI)MMDRV_Alloc(sizeof(WINE_MIDI), MMDRV_MIDIIN, &hMidiIn,
719 &dwFlags, &dwCallback, &dwInstance);
721 if (lpwm == NULL)
722 return MMSYSERR_NOMEM;
724 lpwm->mod.hMidi = hMidiIn;
725 lpwm->mod.dwCallback = dwCallback;
726 lpwm->mod.dwInstance = dwInstance;
728 lpwm->mld.uDeviceID = uDeviceID;
729 dwRet = MMDRV_Open(&lpwm->mld, MIDM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
731 if (dwRet != MMSYSERR_NOERROR) {
732 MMDRV_Free(hMidiIn, &lpwm->mld);
733 hMidiIn = 0;
735 if (lphMidiIn != NULL) *lphMidiIn = hMidiIn;
736 TRACE("=> %d hMidi=%p\n", dwRet, hMidiIn);
738 return dwRet;
741 /**************************************************************************
742 * midiInClose [WINMM.@]
744 UINT WINAPI midiInClose(HMIDIIN hMidiIn)
746 LPWINE_MLD wmld;
747 DWORD dwRet;
749 TRACE("(%p)\n", hMidiIn);
751 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
752 return MMSYSERR_INVALHANDLE;
754 dwRet = MMDRV_Close(wmld, MIDM_CLOSE);
755 MMDRV_Free(hMidiIn, wmld);
756 return dwRet;
759 /**************************************************************************
760 * midiInPrepareHeader [WINMM.@]
762 UINT WINAPI midiInPrepareHeader(HMIDIIN hMidiIn,
763 MIDIHDR* lpMidiInHdr, UINT uSize)
765 LPWINE_MLD wmld;
767 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
769 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
770 return MMSYSERR_INVALHANDLE;
772 return MMDRV_Message(wmld, MIDM_PREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
775 /**************************************************************************
776 * midiInUnprepareHeader [WINMM.@]
778 UINT WINAPI midiInUnprepareHeader(HMIDIIN hMidiIn,
779 MIDIHDR* lpMidiInHdr, UINT uSize)
781 LPWINE_MLD wmld;
783 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
785 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
786 return MMSYSERR_INVALHANDLE;
788 return MMDRV_Message(wmld, MIDM_UNPREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
791 /**************************************************************************
792 * midiInAddBuffer [WINMM.@]
794 UINT WINAPI midiInAddBuffer(HMIDIIN hMidiIn,
795 MIDIHDR* lpMidiInHdr, UINT uSize)
797 LPWINE_MLD wmld;
799 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
801 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
802 return MMSYSERR_INVALHANDLE;
804 return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD_PTR)lpMidiInHdr, uSize);
807 /**************************************************************************
808 * midiInStart [WINMM.@]
810 UINT WINAPI midiInStart(HMIDIIN hMidiIn)
812 LPWINE_MLD wmld;
814 TRACE("(%p)\n", hMidiIn);
816 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
817 return MMSYSERR_INVALHANDLE;
819 return MMDRV_Message(wmld, MIDM_START, 0L, 0L);
822 /**************************************************************************
823 * midiInStop [WINMM.@]
825 UINT WINAPI midiInStop(HMIDIIN hMidiIn)
827 LPWINE_MLD wmld;
829 TRACE("(%p)\n", hMidiIn);
831 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
832 return MMSYSERR_INVALHANDLE;
834 return MMDRV_Message(wmld, MIDM_STOP, 0L, 0L);
837 /**************************************************************************
838 * midiInReset [WINMM.@]
840 UINT WINAPI midiInReset(HMIDIIN hMidiIn)
842 LPWINE_MLD wmld;
844 TRACE("(%p)\n", hMidiIn);
846 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
847 return MMSYSERR_INVALHANDLE;
849 return MMDRV_Message(wmld, MIDM_RESET, 0L, 0L);
852 /**************************************************************************
853 * midiInGetID [WINMM.@]
855 UINT WINAPI midiInGetID(HMIDIIN hMidiIn, UINT* lpuDeviceID)
857 LPWINE_MLD wmld;
859 TRACE("(%p, %p)\n", hMidiIn, lpuDeviceID);
861 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
863 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, TRUE)) == NULL)
864 return MMSYSERR_INVALHANDLE;
866 *lpuDeviceID = wmld->uDeviceID;
868 return MMSYSERR_NOERROR;
871 /**************************************************************************
872 * midiInMessage [WINMM.@]
874 UINT WINAPI midiInMessage(HMIDIIN hMidiIn, UINT uMessage,
875 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
877 LPWINE_MLD wmld;
879 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiIn, uMessage, dwParam1, dwParam2);
881 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
882 return MMSYSERR_INVALHANDLE;
884 switch (uMessage) {
885 case MIDM_OPEN:
886 case MIDM_CLOSE:
887 FIXME("can't handle OPEN or CLOSE message!\n");
888 return MMSYSERR_NOTSUPPORTED;
890 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
893 /**************************************************************************
894 * midiConnect [WINMM.@]
896 MMRESULT WINAPI midiConnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
898 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
899 return MMSYSERR_ERROR;
902 /**************************************************************************
903 * midiDisconnect [WINMM.@]
905 MMRESULT WINAPI midiDisconnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
907 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
908 return MMSYSERR_ERROR;
911 typedef struct WINE_MIDIStream {
912 HMIDIOUT hDevice;
913 HANDLE hThread;
914 DWORD dwThreadID;
915 DWORD dwTempo;
916 DWORD dwTimeDiv;
917 DWORD dwPositionMS;
918 DWORD dwPulses;
919 DWORD dwStartTicks;
920 WORD wFlags;
921 HANDLE hEvent;
922 LPMIDIHDR lpMidiHdr;
923 } WINE_MIDIStream;
925 #define WINE_MSM_HEADER (WM_USER+0)
926 #define WINE_MSM_STOP (WM_USER+1)
927 #define WINE_MSM_PAUSE (WM_USER+2)
928 #define WINE_MSM_RESUME (WM_USER+3)
930 /**************************************************************************
931 * MMSYSTEM_GetMidiStream [internal]
933 static BOOL MMSYSTEM_GetMidiStream(HMIDISTRM hMidiStrm, WINE_MIDIStream** lpMidiStrm, WINE_MIDI** lplpwm)
935 WINE_MIDI* lpwm = (LPWINE_MIDI)MMDRV_Get(hMidiStrm, MMDRV_MIDIOUT, FALSE);
937 if (lplpwm)
938 *lplpwm = lpwm;
940 if (lpwm == NULL) {
941 return FALSE;
944 *lpMidiStrm = (WINE_MIDIStream*)lpwm->mod.rgIds.dwStreamID;
946 return *lpMidiStrm != NULL;
949 /**************************************************************************
950 * MMSYSTEM_MidiStream_Convert [internal]
952 static DWORD MMSYSTEM_MidiStream_Convert(WINE_MIDIStream* lpMidiStrm, DWORD pulse)
954 DWORD ret = 0;
956 if (lpMidiStrm->dwTimeDiv == 0) {
957 FIXME("Shouldn't happen. lpMidiStrm->dwTimeDiv = 0\n");
958 } else if (lpMidiStrm->dwTimeDiv > 0x8000) { /* SMPTE, unchecked FIXME? */
959 int nf = -(char)HIBYTE(lpMidiStrm->dwTimeDiv); /* number of frames */
960 int nsf = LOBYTE(lpMidiStrm->dwTimeDiv); /* number of sub-frames */
961 ret = (pulse * 1000) / (nf * nsf);
962 } else {
963 ret = (DWORD)((double)pulse * ((double)lpMidiStrm->dwTempo / 1000) /
964 (double)lpMidiStrm->dwTimeDiv);
967 return ret;
970 /**************************************************************************
971 * MMSYSTEM_MidiStream_MessageHandler [internal]
973 static BOOL MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream* lpMidiStrm, LPWINE_MIDI lpwm, LPMSG msg)
975 LPMIDIHDR lpMidiHdr;
976 LPMIDIHDR* lpmh;
977 LPBYTE lpData;
978 BOOL paused = FALSE;
980 for (;;) {
981 switch (msg->message) {
982 case WM_QUIT:
983 return FALSE;
984 case WINE_MSM_STOP:
985 TRACE("STOP\n");
986 /* this is not quite what MS doc says... */
987 midiOutReset(lpMidiStrm->hDevice);
988 /* empty list of already submitted buffers */
989 lpMidiHdr = lpMidiStrm->lpMidiHdr;
990 lpMidiStrm->lpMidiHdr = NULL;
991 while (lpMidiHdr) {
992 LPMIDIHDR lphdr = lpMidiHdr;
993 lpMidiHdr = lpMidiHdr->lpNext;
994 lphdr->dwFlags |= MHDR_DONE;
995 lphdr->dwFlags &= ~MHDR_INQUEUE;
997 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
998 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
999 lpwm->mod.dwInstance, (DWORD_PTR)lphdr, 0);
1001 return TRUE;
1002 case WINE_MSM_RESUME:
1003 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1004 lpMidiStrm->dwStartTicks = GetTickCount() - lpMidiStrm->dwPositionMS;
1005 return TRUE;
1006 case WINE_MSM_PAUSE:
1007 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1008 paused = TRUE;
1009 break;
1010 /* FIXME(EPP): "I don't understand the content of the first MIDIHDR sent
1011 * by native mcimidi, it doesn't look like a correct one".
1012 * this trick allows us to throw it away... but I don't like it.
1013 * It looks like part of the file I'm trying to play and definitively looks
1014 * like raw midi content.
1015 * I'd really like to understand why native mcimidi sends it. Perhaps a bad
1016 * synchronization issue where native mcimidi is still processing raw MIDI
1017 * content before generating MIDIEVENTs ?
1019 * 4c 04 89 3b 00 81 7c 99 3b 43 00 99 23 5e 04 89 L..;..|.;C..#^..
1020 * 3b 00 00 89 23 00 7c 99 3b 45 00 99 28 62 04 89 ;...#.|.;E..(b..
1021 * 3b 00 00 89 28 00 81 7c 99 3b 4e 00 99 23 5e 04 ;...(..|.;N..#^.
1022 * 89 3b 00 00 89 23 00 7c 99 3b 45 00 99 23 78 04 .;...#.|.;E..#x.
1023 * 89 3b 00 00 89 23 00 81 7c 99 3b 48 00 99 23 5e .;...#..|.;H..#^
1024 * 04 89 3b 00 00 89 23 00 7c 99 3b 4e 00 99 28 62 ..;...#.|.;N..(b
1025 * 04 89 3b 00 00 89 28 00 81 7c 99 39 4c 00 99 23 ..;...(..|.9L..#
1026 * 5e 04 89 39 00 00 89 23 00 82 7c 99 3b 4c 00 99 ^..9...#..|.;L..
1027 * 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 00 99 #^..;...#.|.;H..
1028 * 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b 3f 04 (b..;...(..|.;?.
1029 * 89 3b 00 1c 99 23 5e 04 89 23 00 5c 99 3b 45 00 .;...#^..#.\.;E.
1030 * 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 3b 46 .#x..;...#..|.;F
1031 * 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 ..#^..;...#.|.;H
1032 * 00 99 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b ..(b..;...(..|.;
1033 * 46 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b F..#^..;...#.|.;
1034 * 48 00 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 H..#x..;...#..|.
1035 * 3b 4c 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 ;L..#^..;...#.|.
1037 case WINE_MSM_HEADER:
1038 /* sets initial tick count for first MIDIHDR */
1039 if (!lpMidiStrm->dwStartTicks)
1040 lpMidiStrm->dwStartTicks = GetTickCount();
1041 lpMidiHdr = (LPMIDIHDR)msg->lParam;
1042 lpData = (LPBYTE)lpMidiHdr->lpData;
1043 TRACE("Adding %s lpMidiHdr=%p [lpData=0x%p dwBytesRecorded=%u/%u dwFlags=0x%08x size=%lu]\n",
1044 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular", lpMidiHdr,
1045 lpData, lpMidiHdr->dwBytesRecorded, lpMidiHdr->dwBufferLength,
1046 lpMidiHdr->dwFlags, msg->wParam);
1047 #if 0
1048 /* dumps content of lpMidiHdr->lpData
1049 * FIXME: there should be a debug routine somewhere that already does this
1051 for (dwToGo = 0; dwToGo < lpMidiHdr->dwBufferLength; dwToGo += 16) {
1052 DWORD i;
1053 BYTE ch;
1055 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++)
1056 printf("%02x ", lpData[dwToGo + i]);
1057 for (; i < 16; i++)
1058 printf(" ");
1059 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++) {
1060 ch = lpData[dwToGo + i];
1061 printf("%c", (ch >= 0x20 && ch <= 0x7F) ? ch : '.');
1063 printf("\n");
1065 #endif
1066 if (((LPMIDIEVENT)lpData)->dwStreamID != 0 &&
1067 ((LPMIDIEVENT)lpData)->dwStreamID != 0xFFFFFFFF &&
1068 ((LPMIDIEVENT)lpData)->dwStreamID != (DWORD)lpMidiStrm) {
1069 FIXME("Dropping bad %s lpMidiHdr (streamID=%08x)\n",
1070 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular",
1071 ((LPMIDIEVENT)lpData)->dwStreamID);
1072 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1073 lpMidiHdr->dwFlags |= MHDR_DONE;
1075 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1076 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1077 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1078 break;
1081 lpMidiHdr->lpNext = 0;
1082 for (lpmh = &lpMidiStrm->lpMidiHdr; *lpmh; lpmh = &(*lpmh)->lpNext);
1083 *lpmh = lpMidiHdr;
1084 break;
1085 default:
1086 FIXME("Unknown message %d\n", msg->message);
1087 break;
1089 if (!paused)
1090 return TRUE;
1091 GetMessageA(msg, 0, 0, 0);
1095 /**************************************************************************
1096 * MMSYSTEM_MidiStream_Player [internal]
1098 static DWORD CALLBACK MMSYSTEM_MidiStream_Player(LPVOID pmt)
1100 WINE_MIDIStream* lpMidiStrm = pmt;
1101 WINE_MIDI* lpwm;
1102 MSG msg;
1103 DWORD dwToGo;
1104 DWORD dwCurrTC;
1105 LPMIDIHDR lpMidiHdr;
1106 DWORD dwOffset;
1108 TRACE("(%p)!\n", lpMidiStrm);
1110 if (!lpMidiStrm ||
1111 (lpwm = (LPWINE_MIDI)MMDRV_Get(lpMidiStrm->hDevice, MMDRV_MIDIOUT, FALSE)) == NULL)
1112 goto the_end;
1114 /* force thread's queue creation */
1115 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
1117 lpMidiStrm->dwStartTicks = 0;
1118 lpMidiStrm->dwPulses = 0;
1120 lpMidiStrm->lpMidiHdr = 0;
1122 /* midiStreamOpen is waiting for ack */
1123 SetEvent(lpMidiStrm->hEvent);
1125 start_header:
1126 lpMidiHdr = lpMidiStrm->lpMidiHdr;
1127 if (!lpMidiHdr) {
1128 /* for first message, block until one arrives, then process all that are available */
1129 GetMessageA(&msg, 0, 0, 0);
1130 do {
1131 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1132 goto the_end;
1133 } while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE));
1134 goto start_header;
1137 dwOffset = 0;
1138 while (dwOffset + offsetof(MIDIEVENT,dwParms) <= lpMidiHdr->dwBytesRecorded) {
1139 LPMIDIEVENT me = (LPMIDIEVENT)(lpMidiHdr->lpData+dwOffset);
1141 /* do we have to wait ? */
1142 if (me->dwDeltaTime) {
1143 lpMidiStrm->dwPositionMS += MMSYSTEM_MidiStream_Convert(lpMidiStrm, me->dwDeltaTime);
1144 lpMidiStrm->dwPulses += me->dwDeltaTime;
1146 dwToGo = lpMidiStrm->dwStartTicks + lpMidiStrm->dwPositionMS;
1148 TRACE("%u/%u/%u\n", dwToGo, GetTickCount(), me->dwDeltaTime);
1149 while (dwToGo - (dwCurrTC = GetTickCount()) <= MAXLONG) {
1150 if (MsgWaitForMultipleObjects(0, NULL, FALSE, dwToGo - dwCurrTC, QS_ALLINPUT) == WAIT_OBJECT_0) {
1151 /* got a message, handle it */
1152 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
1153 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1154 goto the_end;
1155 /* is lpMidiHdr still current? */
1156 if (lpMidiHdr != lpMidiStrm->lpMidiHdr) {
1157 goto start_header;
1160 } else {
1161 /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
1162 break;
1166 switch (MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK)) {
1167 case MEVT_COMMENT:
1168 FIXME("NIY: MEVT_COMMENT\n");
1169 /* do nothing, skip bytes */
1170 break;
1171 case MEVT_LONGMSG:
1172 midiOutLongMsg(lpMidiStrm->hDevice, lpMidiStrm->lpMidiHdr, MEVT_EVENTPARM(me->dwEvent));
1173 break;
1174 case MEVT_NOP:
1175 break;
1176 case MEVT_SHORTMSG:
1177 midiOutShortMsg(lpMidiStrm->hDevice, MEVT_EVENTPARM(me->dwEvent));
1178 break;
1179 case MEVT_TEMPO:
1180 lpMidiStrm->dwTempo = MEVT_EVENTPARM(me->dwEvent);
1181 break;
1182 case MEVT_VERSION:
1183 break;
1184 default:
1185 FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK));
1186 break;
1188 if (me->dwEvent & MEVT_F_CALLBACK) {
1189 /* native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
1190 lpMidiHdr->dwOffset = dwOffset;
1191 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1192 (HDRVR)lpMidiStrm->hDevice, MM_MOM_POSITIONCB,
1193 lpwm->mod.dwInstance, (LPARAM)lpMidiHdr, 0L);
1195 dwOffset += offsetof(MIDIEVENT,dwParms);
1196 if (me->dwEvent & MEVT_F_LONG)
1197 dwOffset += (MEVT_EVENTPARM(me->dwEvent) + 3) & ~3;
1199 /* done with this header */
1200 lpMidiStrm->lpMidiHdr = lpMidiHdr->lpNext;
1201 lpMidiHdr->dwFlags |= MHDR_DONE;
1202 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1204 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1205 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1206 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1207 goto start_header;
1209 the_end:
1210 TRACE("End of thread\n");
1211 return 0;
1214 /**************************************************************************
1215 * midiStreamClose [WINMM.@]
1217 MMRESULT WINAPI midiStreamClose(HMIDISTRM hMidiStrm)
1219 WINE_MIDIStream* lpMidiStrm;
1220 MMRESULT ret = 0;
1222 TRACE("(%p)!\n", hMidiStrm);
1224 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1225 return MMSYSERR_INVALHANDLE;
1227 midiStreamStop(hMidiStrm);
1228 PostThreadMessageA(lpMidiStrm->dwThreadID, WM_QUIT, 0, 0);
1229 CloseHandle(lpMidiStrm->hEvent);
1230 if (lpMidiStrm->hThread) {
1231 if (GetCurrentThreadId() != lpMidiStrm->dwThreadID)
1232 WaitForSingleObject(lpMidiStrm->hThread, INFINITE);
1233 else {
1234 FIXME("leak from call within function callback\n");
1235 ret = MMSYSERR_HANDLEBUSY; /* yet don't signal it to app */
1237 CloseHandle(lpMidiStrm->hThread);
1239 if(!ret)
1240 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1242 return midiOutClose((HMIDIOUT)hMidiStrm);
1245 /**************************************************************************
1246 * midiStreamOpen [WINMM.@]
1248 MMRESULT WINAPI midiStreamOpen(HMIDISTRM* lphMidiStrm, LPUINT lpuDeviceID,
1249 DWORD cMidi, DWORD_PTR dwCallback,
1250 DWORD_PTR dwInstance, DWORD fdwOpen)
1252 WINE_MIDIStream* lpMidiStrm;
1253 MMRESULT ret;
1254 MIDIOPENSTRMID mosm;
1255 LPWINE_MIDI lpwm;
1256 HMIDIOUT hMidiOut;
1258 TRACE("(%p, %p, %d, 0x%08lx, 0x%08lx, 0x%08x)!\n",
1259 lphMidiStrm, lpuDeviceID, cMidi, dwCallback, dwInstance, fdwOpen);
1261 if (cMidi != 1 || lphMidiStrm == NULL || lpuDeviceID == NULL)
1262 return MMSYSERR_INVALPARAM;
1264 ret = WINMM_CheckCallback(dwCallback, fdwOpen, FALSE);
1265 if (ret != MMSYSERR_NOERROR)
1266 return ret;
1268 lpMidiStrm = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream));
1269 if (!lpMidiStrm)
1270 return MMSYSERR_NOMEM;
1272 lpMidiStrm->dwTempo = 500000;
1273 lpMidiStrm->dwTimeDiv = 480; /* 480 is 120 quarter notes per minute *//* FIXME ??*/
1274 lpMidiStrm->dwPositionMS = 0;
1276 mosm.dwStreamID = (DWORD)lpMidiStrm;
1277 /* FIXME: the correct value is not allocated yet for MAPPER */
1278 mosm.wDeviceID = *lpuDeviceID;
1279 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &fdwOpen, 1, &mosm);
1280 if (!lpwm) {
1281 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1282 return MMSYSERR_NOMEM;
1284 lpMidiStrm->hDevice = hMidiOut;
1285 *lphMidiStrm = (HMIDISTRM)hMidiOut;
1287 lpwm->mld.uDeviceID = *lpuDeviceID;
1289 ret = MMDRV_Open(&lpwm->mld, MODM_OPEN, (DWORD_PTR)&lpwm->mod, fdwOpen);
1290 if (ret != MMSYSERR_NOERROR) {
1291 MMDRV_Free(hMidiOut, &lpwm->mld);
1292 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1293 return ret;
1296 lpMidiStrm->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1297 lpMidiStrm->wFlags = HIWORD(fdwOpen);
1299 lpMidiStrm->hThread = CreateThread(NULL, 0, MMSYSTEM_MidiStream_Player,
1300 lpMidiStrm, 0, &(lpMidiStrm->dwThreadID));
1302 if (!lpMidiStrm->hThread) {
1303 midiStreamClose((HMIDISTRM)hMidiOut);
1304 return MMSYSERR_NOMEM;
1306 SetThreadPriority(lpMidiStrm->hThread, THREAD_PRIORITY_TIME_CRITICAL);
1308 /* wait for thread to have started, and for its queue to be created */
1309 WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
1311 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1313 TRACE("=> (%u/%d) hMidi=%p ret=%d lpMidiStrm=%p\n",
1314 *lpuDeviceID, lpwm->mld.uDeviceID, *lphMidiStrm, ret, lpMidiStrm);
1315 return ret;
1318 /**************************************************************************
1319 * midiStreamOut [WINMM.@]
1321 MMRESULT WINAPI midiStreamOut(HMIDISTRM hMidiStrm, LPMIDIHDR lpMidiHdr,
1322 UINT cbMidiHdr)
1324 WINE_MIDIStream* lpMidiStrm;
1325 DWORD ret = MMSYSERR_NOERROR;
1327 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMidiHdr, cbMidiHdr);
1329 if (cbMidiHdr < offsetof(MIDIHDR,dwOffset) || !lpMidiHdr || !lpMidiHdr->lpData
1330 || lpMidiHdr->dwBufferLength < lpMidiHdr->dwBytesRecorded
1331 || lpMidiHdr->dwBytesRecorded % 4 /* player expects DWORD padding */)
1332 return MMSYSERR_INVALPARAM;
1333 /* FIXME: Native additionally checks if the MIDIEVENTs in lpData
1334 * exactly fit dwBytesRecorded. */
1336 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1337 return MIDIERR_UNPREPARED;
1339 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1340 return MIDIERR_STILLPLAYING;
1342 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1343 ret = MMSYSERR_INVALHANDLE;
1344 } else {
1345 lpMidiHdr->dwFlags |= MHDR_ISSTRM | MHDR_INQUEUE;
1346 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1347 if (!PostThreadMessageA(lpMidiStrm->dwThreadID,
1348 WINE_MSM_HEADER, cbMidiHdr,
1349 (LPARAM)lpMidiHdr)) {
1350 ERR("bad PostThreadMessageA\n");
1351 ret = MMSYSERR_ERROR;
1354 return ret;
1357 /**************************************************************************
1358 * midiStreamPause [WINMM.@]
1360 MMRESULT WINAPI midiStreamPause(HMIDISTRM hMidiStrm)
1362 WINE_MIDIStream* lpMidiStrm;
1363 DWORD ret = MMSYSERR_NOERROR;
1365 TRACE("(%p)!\n", hMidiStrm);
1367 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1368 return MMSYSERR_INVALHANDLE;
1369 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1370 return ret;
1373 /**************************************************************************
1374 * midiStreamPosition [WINMM.@]
1376 MMRESULT WINAPI midiStreamPosition(HMIDISTRM hMidiStrm, LPMMTIME lpMMT, UINT cbmmt)
1378 WINE_MIDIStream* lpMidiStrm;
1379 DWORD ret = MMSYSERR_NOERROR;
1381 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMMT, cbmmt);
1383 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1384 ret = MMSYSERR_INVALHANDLE;
1385 } else if (lpMMT == NULL || cbmmt != sizeof(MMTIME)) {
1386 ret = MMSYSERR_INVALPARAM;
1387 } else {
1388 switch (lpMMT->wType) {
1389 default:
1390 FIXME("Unsupported time type %x\n", lpMMT->wType);
1391 /* fall through */
1392 case TIME_BYTES:
1393 case TIME_SAMPLES:
1394 lpMMT->wType = TIME_MS;
1395 /* fall through to alternative format */
1396 case TIME_MS:
1397 lpMMT->u.ms = lpMidiStrm->dwPositionMS;
1398 TRACE("=> %d ms\n", lpMMT->u.ms);
1399 break;
1400 case TIME_TICKS:
1401 lpMMT->u.ticks = lpMidiStrm->dwPulses;
1402 TRACE("=> %d ticks\n", lpMMT->u.ticks);
1403 break;
1406 return ret;
1409 /**************************************************************************
1410 * midiStreamProperty [WINMM.@]
1412 MMRESULT WINAPI midiStreamProperty(HMIDISTRM hMidiStrm, LPBYTE lpPropData, DWORD dwProperty)
1414 WINE_MIDIStream* lpMidiStrm;
1415 MMRESULT ret = MMSYSERR_NOERROR;
1417 TRACE("(%p, %p, %x)\n", hMidiStrm, lpPropData, dwProperty);
1419 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1420 ret = MMSYSERR_INVALHANDLE;
1421 } else if ((dwProperty & (MIDIPROP_GET|MIDIPROP_SET)) == 0) {
1422 ret = MMSYSERR_INVALPARAM;
1423 } else if (dwProperty & MIDIPROP_TEMPO) {
1424 MIDIPROPTEMPO* mpt = (MIDIPROPTEMPO*)lpPropData;
1426 if (sizeof(MIDIPROPTEMPO) != mpt->cbStruct) {
1427 ret = MMSYSERR_INVALPARAM;
1428 } else if (dwProperty & MIDIPROP_SET) {
1429 lpMidiStrm->dwTempo = mpt->dwTempo;
1430 TRACE("Setting tempo to %d\n", mpt->dwTempo);
1431 } else if (dwProperty & MIDIPROP_GET) {
1432 mpt->dwTempo = lpMidiStrm->dwTempo;
1433 TRACE("Getting tempo <= %d\n", mpt->dwTempo);
1435 } else if (dwProperty & MIDIPROP_TIMEDIV) {
1436 MIDIPROPTIMEDIV* mptd = (MIDIPROPTIMEDIV*)lpPropData;
1438 if (sizeof(MIDIPROPTIMEDIV) != mptd->cbStruct) {
1439 ret = MMSYSERR_INVALPARAM;
1440 } else if (dwProperty & MIDIPROP_SET) {
1441 lpMidiStrm->dwTimeDiv = mptd->dwTimeDiv;
1442 TRACE("Setting time div to %d\n", mptd->dwTimeDiv);
1443 } else if (dwProperty & MIDIPROP_GET) {
1444 mptd->dwTimeDiv = lpMidiStrm->dwTimeDiv;
1445 TRACE("Getting time div <= %d\n", mptd->dwTimeDiv);
1447 } else {
1448 ret = MMSYSERR_INVALPARAM;
1451 return ret;
1454 /**************************************************************************
1455 * midiStreamRestart [WINMM.@]
1457 MMRESULT WINAPI midiStreamRestart(HMIDISTRM hMidiStrm)
1459 WINE_MIDIStream* lpMidiStrm;
1460 MMRESULT ret = MMSYSERR_NOERROR;
1462 TRACE("(%p)!\n", hMidiStrm);
1464 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1465 return MMSYSERR_INVALHANDLE;
1466 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_RESUME, 0, 0);
1467 return ret;
1470 /**************************************************************************
1471 * midiStreamStop [WINMM.@]
1473 MMRESULT WINAPI midiStreamStop(HMIDISTRM hMidiStrm)
1475 WINE_MIDIStream* lpMidiStrm;
1476 MMRESULT ret = MMSYSERR_NOERROR;
1478 TRACE("(%p)!\n", hMidiStrm);
1480 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1481 return MMSYSERR_INVALHANDLE;
1482 PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_STOP, 0, 0);
1483 return ret;
1486 struct mm_starter
1488 LPTASKCALLBACK cb;
1489 DWORD client;
1490 HANDLE event;
1493 static DWORD WINAPI mmTaskRun(void* pmt)
1495 struct mm_starter mms;
1497 memcpy(&mms, pmt, sizeof(struct mm_starter));
1498 HeapFree(GetProcessHeap(), 0, pmt);
1499 mms.cb(mms.client);
1500 if (mms.event) SetEvent(mms.event);
1501 return 0;
1504 /******************************************************************
1505 * mmTaskCreate (WINMM.@)
1507 UINT WINAPI mmTaskCreate(LPTASKCALLBACK cb, HANDLE* ph, DWORD_PTR client)
1509 HANDLE hThread;
1510 HANDLE hEvent = 0;
1511 struct mm_starter *mms;
1513 mms = HeapAlloc(GetProcessHeap(), 0, sizeof(struct mm_starter));
1514 if (mms == NULL) return TASKERR_OUTOFMEMORY;
1516 mms->cb = cb;
1517 mms->client = client;
1518 if (ph) hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1519 mms->event = hEvent;
1521 hThread = CreateThread(0, 0, mmTaskRun, mms, 0, NULL);
1522 if (!hThread) {
1523 HeapFree(GetProcessHeap(), 0, mms);
1524 if (hEvent) CloseHandle(hEvent);
1525 return TASKERR_OUTOFMEMORY;
1527 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
1528 if (ph) *ph = hEvent;
1529 CloseHandle(hThread);
1530 return 0;
1533 /******************************************************************
1534 * mmTaskBlock (WINMM.@)
1536 VOID WINAPI mmTaskBlock(DWORD tid)
1538 MSG msg;
1542 GetMessageA(&msg, 0, 0, 0);
1543 if (msg.hwnd) DispatchMessageA(&msg);
1544 } while (msg.message != WM_USER);
1547 /******************************************************************
1548 * mmTaskSignal (WINMM.@)
1550 BOOL WINAPI mmTaskSignal(DWORD tid)
1552 return PostThreadMessageW(tid, WM_USER, 0, 0);
1555 /******************************************************************
1556 * mmTaskYield (WINMM.@)
1558 VOID WINAPI mmTaskYield(VOID) {}
1560 /******************************************************************
1561 * mmGetCurrentTask (WINMM.@)
1563 DWORD WINAPI mmGetCurrentTask(VOID)
1565 return GetCurrentThreadId();