wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / winmm / winmm.c
blobeea29ea61e5be019e804ff1d9750023980b46134
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"
57 #include "wine/rbtree.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
61 /* ========================================================================
62 * G L O B A L S E T T I N G S
63 * ========================================================================*/
65 HINSTANCE hWinMM32Instance;
66 HANDLE psLastEvent;
68 static CRITICAL_SECTION_DEBUG critsect_debug =
70 0, 0, &WINMM_cs,
71 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
72 0, 0, { (DWORD_PTR)(__FILE__ ": WINMM_cs") }
74 CRITICAL_SECTION WINMM_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
76 static struct wine_rb_tree wine_midi_streams;
77 static int wine_midi_stream_compare(const void *key, const struct wine_rb_entry *entry);
79 /**************************************************************************
80 * WINMM_CreateIData [internal]
82 static BOOL WINMM_CreateIData(HINSTANCE hInstDLL)
84 hWinMM32Instance = hInstDLL;
85 wine_rb_init(&wine_midi_streams, wine_midi_stream_compare);
86 psLastEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
87 return psLastEvent != NULL;
90 /******************************************************************
91 * WINMM_ErrorToString
93 const char* WINMM_ErrorToString(MMRESULT error)
95 #define ERR_TO_STR(dev) case dev: return #dev
96 switch (error) {
97 ERR_TO_STR(MMSYSERR_NOERROR);
98 ERR_TO_STR(MMSYSERR_ERROR);
99 ERR_TO_STR(MMSYSERR_BADDEVICEID);
100 ERR_TO_STR(MMSYSERR_NOTENABLED);
101 ERR_TO_STR(MMSYSERR_ALLOCATED);
102 ERR_TO_STR(MMSYSERR_INVALHANDLE);
103 ERR_TO_STR(MMSYSERR_NODRIVER);
104 ERR_TO_STR(MMSYSERR_NOMEM);
105 ERR_TO_STR(MMSYSERR_NOTSUPPORTED);
106 ERR_TO_STR(MMSYSERR_BADERRNUM);
107 ERR_TO_STR(MMSYSERR_INVALFLAG);
108 ERR_TO_STR(MMSYSERR_INVALPARAM);
109 ERR_TO_STR(MMSYSERR_HANDLEBUSY);
110 ERR_TO_STR(MMSYSERR_INVALIDALIAS);
111 ERR_TO_STR(MMSYSERR_BADDB);
112 ERR_TO_STR(MMSYSERR_KEYNOTFOUND);
113 ERR_TO_STR(MMSYSERR_READERROR);
114 ERR_TO_STR(MMSYSERR_WRITEERROR);
115 ERR_TO_STR(MMSYSERR_DELETEERROR);
116 ERR_TO_STR(MMSYSERR_VALNOTFOUND);
117 ERR_TO_STR(MMSYSERR_NODRIVERCB);
118 ERR_TO_STR(WAVERR_BADFORMAT);
119 ERR_TO_STR(WAVERR_STILLPLAYING);
120 ERR_TO_STR(WAVERR_UNPREPARED);
121 ERR_TO_STR(WAVERR_SYNC);
122 ERR_TO_STR(MIDIERR_INVALIDSETUP);
123 ERR_TO_STR(MIDIERR_NODEVICE);
124 ERR_TO_STR(MIDIERR_STILLPLAYING);
125 ERR_TO_STR(MIDIERR_UNPREPARED);
127 #undef ERR_TO_STR
128 return wine_dbg_sprintf("Unknown(0x%08x)", error);
131 /**************************************************************************
132 * DllMain (WINMM.init)
134 * WINMM DLL entry point
137 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
139 TRACE("%p 0x%x %p\n", hInstDLL, fdwReason, fImpLoad);
141 switch (fdwReason) {
142 case DLL_PROCESS_ATTACH:
143 DisableThreadLibraryCalls(hInstDLL);
145 if (!WINMM_CreateIData(hInstDLL))
146 return FALSE;
148 break;
149 case DLL_PROCESS_DETACH:
150 if(fImpLoad)
151 break;
153 MCI_SendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0L);
154 MMDRV_Exit();
155 DRIVER_UnloadAll();
156 WINMM_DeleteWaveform();
157 TIME_MMTimeStop();
158 CloseHandle(psLastEvent);
159 DeleteCriticalSection(&WINMM_cs);
160 break;
162 return TRUE;
165 /**************************************************************************
166 * WINMM_CheckCallback [internal]
168 MMRESULT WINMM_CheckCallback(DWORD_PTR dwCallback, DWORD fdwOpen, BOOL mixer)
170 switch (fdwOpen & CALLBACK_TYPEMASK) {
171 case CALLBACK_NULL: /* dwCallback need not be NULL */
172 break;
173 case CALLBACK_WINDOW:
174 if (dwCallback && !IsWindow((HWND)dwCallback))
175 return MMSYSERR_INVALPARAM;
176 break;
178 case CALLBACK_FUNCTION:
179 /* a NULL cb is acceptable since w2k, MMSYSERR_INVALPARAM earlier */
180 if (mixer)
181 return MMSYSERR_INVALFLAG; /* since w2k, MMSYSERR_NOTSUPPORTED earlier */
182 break;
183 case CALLBACK_THREAD:
184 case CALLBACK_EVENT:
185 if (mixer) /* FIXME: mixer supports THREAD+EVENT since w2k */
186 return MMSYSERR_NOTSUPPORTED; /* w9X */
187 break;
188 default:
189 WARN("Unknown callback type %d\n", HIWORD(fdwOpen));
191 return MMSYSERR_NOERROR;
194 /**************************************************************************
195 * auxGetNumDevs [WINMM.@]
197 UINT WINAPI auxGetNumDevs(void)
199 return MMDRV_GetNum(MMDRV_AUX);
202 /**************************************************************************
203 * auxGetDevCapsW [WINMM.@]
205 UINT WINAPI auxGetDevCapsW(UINT_PTR uDeviceID, LPAUXCAPSW lpCaps, UINT uSize)
207 LPWINE_MLD wmld;
209 TRACE("(%04lX, %p, %d) !\n", uDeviceID, lpCaps, uSize);
211 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
213 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
214 return MMSYSERR_BADDEVICEID;
215 return MMDRV_Message(wmld, AUXDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
218 /**************************************************************************
219 * auxGetDevCapsA [WINMM.@]
221 UINT WINAPI auxGetDevCapsA(UINT_PTR uDeviceID, LPAUXCAPSA lpCaps, UINT uSize)
223 AUXCAPSW acW;
224 UINT ret;
226 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
228 ret = auxGetDevCapsW(uDeviceID, &acW, sizeof(acW));
230 if (ret == MMSYSERR_NOERROR) {
231 AUXCAPSA acA;
232 acA.wMid = acW.wMid;
233 acA.wPid = acW.wPid;
234 acA.vDriverVersion = acW.vDriverVersion;
235 WideCharToMultiByte( CP_ACP, 0, acW.szPname, -1, acA.szPname,
236 sizeof(acA.szPname), NULL, NULL );
237 acA.wTechnology = acW.wTechnology;
238 acA.wReserved1 = acW.wReserved1;
239 acA.dwSupport = acW.dwSupport;
240 memcpy(lpCaps, &acA, min(uSize, sizeof(acA)));
242 return ret;
245 /**************************************************************************
246 * auxGetVolume [WINMM.@]
248 UINT WINAPI auxGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
250 LPWINE_MLD wmld;
252 TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);
254 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
255 return MMSYSERR_INVALHANDLE;
256 return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
259 /**************************************************************************
260 * auxSetVolume [WINMM.@]
262 UINT WINAPI auxSetVolume(UINT uDeviceID, DWORD dwVolume)
264 LPWINE_MLD wmld;
266 TRACE("(%04X, %u) !\n", uDeviceID, dwVolume);
268 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
269 return MMSYSERR_INVALHANDLE;
270 return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L);
273 /**************************************************************************
274 * auxOutMessage [WINMM.@]
276 UINT WINAPI auxOutMessage(UINT uDeviceID, UINT uMessage, DWORD_PTR dw1, DWORD_PTR dw2)
278 LPWINE_MLD wmld;
280 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
281 return MMSYSERR_INVALHANDLE;
283 return MMDRV_Message(wmld, uMessage, dw1, dw2);
286 /**************************************************************************
287 * midiOutGetNumDevs [WINMM.@]
289 UINT WINAPI midiOutGetNumDevs(void)
291 return MMDRV_GetNum(MMDRV_MIDIOUT);
294 /**************************************************************************
295 * midiOutGetDevCapsW [WINMM.@]
297 UINT WINAPI midiOutGetDevCapsW(UINT_PTR uDeviceID, LPMIDIOUTCAPSW lpCaps,
298 UINT uSize)
300 LPWINE_MLD wmld;
302 TRACE("(%lu, %p, %u);\n", uDeviceID, lpCaps, uSize);
304 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
306 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL)
307 return MMSYSERR_BADDEVICEID;
309 return MMDRV_Message(wmld, MODM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
312 /**************************************************************************
313 * midiOutGetDevCapsA [WINMM.@]
315 UINT WINAPI midiOutGetDevCapsA(UINT_PTR uDeviceID, LPMIDIOUTCAPSA lpCaps,
316 UINT uSize)
318 MIDIOUTCAPSW mocW;
319 UINT ret;
321 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
323 ret = midiOutGetDevCapsW(uDeviceID, &mocW, sizeof(mocW));
325 if (ret == MMSYSERR_NOERROR) {
326 MIDIOUTCAPSA mocA;
327 mocA.wMid = mocW.wMid;
328 mocA.wPid = mocW.wPid;
329 mocA.vDriverVersion = mocW.vDriverVersion;
330 WideCharToMultiByte( CP_ACP, 0, mocW.szPname, -1, mocA.szPname,
331 sizeof(mocA.szPname), NULL, NULL );
332 mocA.wTechnology = mocW.wTechnology;
333 mocA.wVoices = mocW.wVoices;
334 mocA.wNotes = mocW.wNotes;
335 mocA.wChannelMask = mocW.wChannelMask;
336 mocA.dwSupport = mocW.dwSupport;
337 memcpy(lpCaps, &mocA, min(uSize, sizeof(mocA)));
339 return ret;
342 /**************************************************************************
343 * midiOutGetErrorTextA [WINMM.@]
344 * midiInGetErrorTextA [WINMM.@]
346 UINT WINAPI midiOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
348 UINT ret;
350 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
351 else if (uSize == 0) ret = MMSYSERR_NOERROR;
352 else
354 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
355 if (!xstr) ret = MMSYSERR_NOMEM;
356 else
358 ret = midiOutGetErrorTextW(uError, xstr, uSize);
359 if (ret == MMSYSERR_NOERROR)
360 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
361 HeapFree(GetProcessHeap(), 0, xstr);
364 return ret;
367 /**************************************************************************
368 * midiOutGetErrorTextW [WINMM.@]
369 * midiInGetErrorTextW [WINMM.@]
371 UINT WINAPI midiOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
373 UINT ret = MMSYSERR_BADERRNUM;
375 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
376 else if (uSize == 0) ret = MMSYSERR_NOERROR;
377 else if (
378 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
379 * a warning for the test was always true */
380 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
381 (uError >= MIDIERR_BASE && uError <= MIDIERR_LASTERROR)) {
382 if (LoadStringW(hWinMM32Instance, uError, lpText, uSize) > 0) {
383 ret = MMSYSERR_NOERROR;
386 return ret;
389 /**************************************************************************
390 * MIDI_OutAlloc [internal]
392 static LPWINE_MIDI MIDI_OutAlloc(HMIDIOUT* lphMidiOut, DWORD_PTR* lpdwCallback,
393 DWORD_PTR* lpdwInstance, LPDWORD lpdwFlags,
394 DWORD cIDs, MIDIOPENSTRMID* lpIDs)
396 HANDLE hMidiOut;
397 LPWINE_MIDI lpwm;
398 UINT size;
400 size = sizeof(WINE_MIDI) + (cIDs ? (cIDs-1) : 0) * sizeof(MIDIOPENSTRMID);
402 lpwm = (LPWINE_MIDI)MMDRV_Alloc(size, MMDRV_MIDIOUT, &hMidiOut, lpdwFlags,
403 lpdwCallback, lpdwInstance);
404 *lphMidiOut = hMidiOut;
406 if (lpwm) {
407 lpwm->mod.hMidi = hMidiOut;
408 lpwm->mod.dwCallback = *lpdwCallback;
409 lpwm->mod.dwInstance = *lpdwInstance;
410 lpwm->mod.dnDevNode = 0;
411 lpwm->mod.cIds = cIDs;
412 if (cIDs)
413 memcpy(&(lpwm->mod.rgIds), lpIDs, cIDs * sizeof(MIDIOPENSTRMID));
415 return lpwm;
418 /**************************************************************************
419 * midiOutOpen [WINMM.@]
421 MMRESULT WINAPI midiOutOpen(LPHMIDIOUT lphMidiOut, UINT uDeviceID,
422 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
424 HMIDIOUT hMidiOut;
425 LPWINE_MIDI lpwm;
426 MMRESULT dwRet;
428 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
429 lphMidiOut, uDeviceID, dwCallback, dwInstance, dwFlags);
431 if (lphMidiOut != NULL) *lphMidiOut = 0;
433 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
434 if (dwRet != MMSYSERR_NOERROR)
435 return dwRet;
437 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &dwFlags, 0, NULL);
439 if (lpwm == NULL)
440 return MMSYSERR_NOMEM;
442 lpwm->mld.uDeviceID = uDeviceID;
444 dwRet = MMDRV_Open((LPWINE_MLD)lpwm, MODM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
446 if (dwRet != MMSYSERR_NOERROR) {
447 MMDRV_Free(hMidiOut, (LPWINE_MLD)lpwm);
448 hMidiOut = 0;
451 if (lphMidiOut) *lphMidiOut = hMidiOut;
452 TRACE("=> %d hMidi=%p\n", dwRet, hMidiOut);
454 return dwRet;
457 /**************************************************************************
458 * midiOutClose [WINMM.@]
460 UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
462 LPWINE_MLD wmld;
463 DWORD dwRet;
465 TRACE("(%p)\n", hMidiOut);
467 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
468 return MMSYSERR_INVALHANDLE;
470 dwRet = MMDRV_Close(wmld, MODM_CLOSE);
471 MMDRV_Free(hMidiOut, wmld);
473 return dwRet;
476 /**************************************************************************
477 * midiOutPrepareHeader [WINMM.@]
479 UINT WINAPI midiOutPrepareHeader(HMIDIOUT hMidiOut,
480 MIDIHDR* lpMidiOutHdr, UINT uSize)
482 LPWINE_MLD wmld;
484 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
486 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
487 return MMSYSERR_INVALHANDLE;
488 /* FIXME: detect MIDIStream handles and enforce 64KB buffer limit on those */
490 return MMDRV_Message(wmld, MODM_PREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
493 /**************************************************************************
494 * midiOutUnprepareHeader [WINMM.@]
496 UINT WINAPI midiOutUnprepareHeader(HMIDIOUT hMidiOut,
497 MIDIHDR* lpMidiOutHdr, UINT uSize)
499 LPWINE_MLD wmld;
501 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
503 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
504 return MMSYSERR_INVALHANDLE;
506 return MMDRV_Message(wmld, MODM_UNPREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
509 /**************************************************************************
510 * midiOutShortMsg [WINMM.@]
512 UINT WINAPI midiOutShortMsg(HMIDIOUT hMidiOut, DWORD dwMsg)
514 LPWINE_MLD wmld;
516 TRACE("(%p, %08X)\n", hMidiOut, dwMsg);
518 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
519 return MMSYSERR_INVALHANDLE;
521 return MMDRV_Message(wmld, MODM_DATA, dwMsg, 0L);
524 /**************************************************************************
525 * midiOutLongMsg [WINMM.@]
527 UINT WINAPI midiOutLongMsg(HMIDIOUT hMidiOut,
528 MIDIHDR* lpMidiOutHdr, UINT uSize)
530 LPWINE_MLD wmld;
532 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
534 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
535 return MMSYSERR_INVALHANDLE;
537 return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD_PTR)lpMidiOutHdr, uSize);
540 /**************************************************************************
541 * midiOutReset [WINMM.@]
543 UINT WINAPI midiOutReset(HMIDIOUT hMidiOut)
545 LPWINE_MLD wmld;
547 TRACE("(%p)\n", hMidiOut);
549 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
550 return MMSYSERR_INVALHANDLE;
552 return MMDRV_Message(wmld, MODM_RESET, 0L, 0L);
555 /**************************************************************************
556 * midiOutGetVolume [WINMM.@]
558 UINT WINAPI midiOutGetVolume(HMIDIOUT hMidiOut, DWORD* lpdwVolume)
560 LPWINE_MLD wmld;
562 TRACE("(%p, %p);\n", hMidiOut, lpdwVolume);
564 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
565 return MMSYSERR_INVALHANDLE;
567 return MMDRV_Message(wmld, MODM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
570 /**************************************************************************
571 * midiOutSetVolume [WINMM.@]
573 UINT WINAPI midiOutSetVolume(HMIDIOUT hMidiOut, DWORD dwVolume)
575 LPWINE_MLD wmld;
577 TRACE("(%p, %d);\n", hMidiOut, dwVolume);
579 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
580 return MMSYSERR_INVALHANDLE;
582 return MMDRV_Message(wmld, MODM_SETVOLUME, dwVolume, 0L);
585 /**************************************************************************
586 * midiOutCachePatches [WINMM.@]
588 UINT WINAPI midiOutCachePatches(HMIDIOUT hMidiOut, UINT uBank,
589 WORD* lpwPatchArray, UINT uFlags)
591 /* not really necessary to support this */
592 FIXME("(%p, %u, %p, %x): Stub\n", hMidiOut, uBank, lpwPatchArray, uFlags);
593 return MMSYSERR_NOTSUPPORTED;
596 /**************************************************************************
597 * midiOutCacheDrumPatches [WINMM.@]
599 UINT WINAPI midiOutCacheDrumPatches(HMIDIOUT hMidiOut, UINT uPatch,
600 WORD* lpwKeyArray, UINT uFlags)
602 FIXME("(%p, %u, %p, %x): Stub\n", hMidiOut, uPatch, lpwKeyArray, uFlags);
603 return MMSYSERR_NOTSUPPORTED;
606 /**************************************************************************
607 * midiOutGetID [WINMM.@]
609 UINT WINAPI midiOutGetID(HMIDIOUT hMidiOut, UINT* lpuDeviceID)
611 LPWINE_MLD wmld;
613 TRACE("(%p, %p)\n", hMidiOut, lpuDeviceID);
615 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
616 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
617 return MMSYSERR_INVALHANDLE;
619 *lpuDeviceID = wmld->uDeviceID;
620 return MMSYSERR_NOERROR;
623 /**************************************************************************
624 * midiOutMessage [WINMM.@]
626 UINT WINAPI midiOutMessage(HMIDIOUT hMidiOut, UINT uMessage,
627 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
629 LPWINE_MLD wmld;
631 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
633 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) {
634 /* HACK... */
635 if (uMessage == 0x0001) {
636 *(LPDWORD)dwParam1 = 1;
637 return 0;
639 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) != NULL) {
640 return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
642 return MMSYSERR_INVALHANDLE;
645 switch (uMessage) {
646 case MODM_OPEN:
647 case MODM_CLOSE:
648 FIXME("can't handle OPEN or CLOSE message!\n");
649 return MMSYSERR_NOTSUPPORTED;
651 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
654 /**************************************************************************
655 * midiInGetNumDevs [WINMM.@]
657 UINT WINAPI midiInGetNumDevs(void)
659 return MMDRV_GetNum(MMDRV_MIDIIN);
662 /**************************************************************************
663 * midiInGetDevCapsW [WINMM.@]
665 UINT WINAPI midiInGetDevCapsW(UINT_PTR uDeviceID, LPMIDIINCAPSW lpCaps, UINT uSize)
667 LPWINE_MLD wmld;
669 TRACE("(%ld, %p, %d);\n", uDeviceID, lpCaps, uSize);
671 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
673 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIIN, TRUE)) == NULL)
674 return MMSYSERR_BADDEVICEID;
676 return MMDRV_Message(wmld, MIDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
679 /**************************************************************************
680 * midiInGetDevCapsA [WINMM.@]
682 UINT WINAPI midiInGetDevCapsA(UINT_PTR uDeviceID, LPMIDIINCAPSA lpCaps, UINT uSize)
684 MIDIINCAPSW micW;
685 UINT ret;
687 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
689 ret = midiInGetDevCapsW(uDeviceID, &micW, sizeof(micW));
691 if (ret == MMSYSERR_NOERROR) {
692 MIDIINCAPSA micA;
693 micA.wMid = micW.wMid;
694 micA.wPid = micW.wPid;
695 micA.vDriverVersion = micW.vDriverVersion;
696 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
697 sizeof(micA.szPname), NULL, NULL );
698 micA.dwSupport = micW.dwSupport;
699 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
701 return ret;
704 /**************************************************************************
705 * midiInOpen [WINMM.@]
707 MMRESULT WINAPI midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID,
708 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
710 HANDLE hMidiIn;
711 LPWINE_MIDI lpwm;
712 MMRESULT dwRet;
714 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
715 lphMidiIn, uDeviceID, dwCallback, dwInstance, dwFlags);
717 if (lphMidiIn != NULL) *lphMidiIn = 0;
719 dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
720 if (dwRet != MMSYSERR_NOERROR)
721 return dwRet;
723 lpwm = (LPWINE_MIDI)MMDRV_Alloc(sizeof(WINE_MIDI), MMDRV_MIDIIN, &hMidiIn,
724 &dwFlags, &dwCallback, &dwInstance);
726 if (lpwm == NULL)
727 return MMSYSERR_NOMEM;
729 lpwm->mod.hMidi = hMidiIn;
730 lpwm->mod.dwCallback = dwCallback;
731 lpwm->mod.dwInstance = dwInstance;
733 lpwm->mld.uDeviceID = uDeviceID;
734 dwRet = MMDRV_Open(&lpwm->mld, MIDM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
736 if (dwRet != MMSYSERR_NOERROR) {
737 MMDRV_Free(hMidiIn, &lpwm->mld);
738 hMidiIn = 0;
740 if (lphMidiIn != NULL) *lphMidiIn = hMidiIn;
741 TRACE("=> %d hMidi=%p\n", dwRet, hMidiIn);
743 return dwRet;
746 /**************************************************************************
747 * midiInClose [WINMM.@]
749 UINT WINAPI midiInClose(HMIDIIN hMidiIn)
751 LPWINE_MLD wmld;
752 DWORD dwRet;
754 TRACE("(%p)\n", hMidiIn);
756 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
757 return MMSYSERR_INVALHANDLE;
759 dwRet = MMDRV_Close(wmld, MIDM_CLOSE);
760 MMDRV_Free(hMidiIn, wmld);
761 return dwRet;
764 /**************************************************************************
765 * midiInPrepareHeader [WINMM.@]
767 UINT WINAPI midiInPrepareHeader(HMIDIIN hMidiIn,
768 MIDIHDR* lpMidiInHdr, UINT uSize)
770 LPWINE_MLD wmld;
772 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
774 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
775 return MMSYSERR_INVALHANDLE;
777 return MMDRV_Message(wmld, MIDM_PREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
780 /**************************************************************************
781 * midiInUnprepareHeader [WINMM.@]
783 UINT WINAPI midiInUnprepareHeader(HMIDIIN hMidiIn,
784 MIDIHDR* lpMidiInHdr, UINT uSize)
786 LPWINE_MLD wmld;
788 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
790 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
791 return MMSYSERR_INVALHANDLE;
793 return MMDRV_Message(wmld, MIDM_UNPREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
796 /**************************************************************************
797 * midiInAddBuffer [WINMM.@]
799 UINT WINAPI midiInAddBuffer(HMIDIIN hMidiIn,
800 MIDIHDR* lpMidiInHdr, UINT uSize)
802 LPWINE_MLD wmld;
804 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
806 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
807 return MMSYSERR_INVALHANDLE;
809 return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD_PTR)lpMidiInHdr, uSize);
812 /**************************************************************************
813 * midiInStart [WINMM.@]
815 UINT WINAPI midiInStart(HMIDIIN hMidiIn)
817 LPWINE_MLD wmld;
819 TRACE("(%p)\n", hMidiIn);
821 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
822 return MMSYSERR_INVALHANDLE;
824 return MMDRV_Message(wmld, MIDM_START, 0L, 0L);
827 /**************************************************************************
828 * midiInStop [WINMM.@]
830 UINT WINAPI midiInStop(HMIDIIN hMidiIn)
832 LPWINE_MLD wmld;
834 TRACE("(%p)\n", hMidiIn);
836 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
837 return MMSYSERR_INVALHANDLE;
839 return MMDRV_Message(wmld, MIDM_STOP, 0L, 0L);
842 /**************************************************************************
843 * midiInReset [WINMM.@]
845 UINT WINAPI midiInReset(HMIDIIN hMidiIn)
847 LPWINE_MLD wmld;
849 TRACE("(%p)\n", hMidiIn);
851 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
852 return MMSYSERR_INVALHANDLE;
854 return MMDRV_Message(wmld, MIDM_RESET, 0L, 0L);
857 /**************************************************************************
858 * midiInGetID [WINMM.@]
860 UINT WINAPI midiInGetID(HMIDIIN hMidiIn, UINT* lpuDeviceID)
862 LPWINE_MLD wmld;
864 TRACE("(%p, %p)\n", hMidiIn, lpuDeviceID);
866 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
868 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, TRUE)) == NULL)
869 return MMSYSERR_INVALHANDLE;
871 *lpuDeviceID = wmld->uDeviceID;
873 return MMSYSERR_NOERROR;
876 /**************************************************************************
877 * midiInMessage [WINMM.@]
879 UINT WINAPI midiInMessage(HMIDIIN hMidiIn, UINT uMessage,
880 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
882 LPWINE_MLD wmld;
884 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiIn, uMessage, dwParam1, dwParam2);
886 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
887 return MMSYSERR_INVALHANDLE;
889 switch (uMessage) {
890 case MIDM_OPEN:
891 case MIDM_CLOSE:
892 FIXME("can't handle OPEN or CLOSE message!\n");
893 return MMSYSERR_NOTSUPPORTED;
895 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
898 /**************************************************************************
899 * midiConnect [WINMM.@]
901 MMRESULT WINAPI midiConnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
903 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
904 return MMSYSERR_ERROR;
907 /**************************************************************************
908 * midiDisconnect [WINMM.@]
910 MMRESULT WINAPI midiDisconnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
912 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
913 return MMSYSERR_ERROR;
916 typedef struct WINE_MIDIStream {
917 HMIDIOUT hDevice;
918 HANDLE hThread;
919 DWORD dwThreadID;
920 CRITICAL_SECTION lock;
921 DWORD dwTempo;
922 DWORD dwTimeDiv;
923 ULONGLONG position_usec;
924 DWORD dwPulses;
925 DWORD dwStartTicks;
926 DWORD dwElapsedMS;
927 DWORD dwLastPositionMS;
928 WORD wFlags;
929 WORD status;
930 HANDLE hEvent;
931 LPMIDIHDR lpMidiHdr;
932 DWORD dwStreamID;
933 struct wine_rb_entry entry;
934 } WINE_MIDIStream;
936 #define WINE_MSM_HEADER (WM_USER+0)
937 #define WINE_MSM_STOP (WM_USER+1)
938 #define WINE_MSM_PAUSE (WM_USER+2)
939 #define WINE_MSM_RESUME (WM_USER+3)
941 #define MSM_STATUS_STOPPED WINE_MSM_STOP
942 #define MSM_STATUS_PAUSED WINE_MSM_PAUSE
943 #define MSM_STATUS_PLAYING WINE_MSM_RESUME
945 static int wine_midi_stream_compare(const void *key, const struct wine_rb_entry *entry)
947 WINE_MIDIStream *stream = WINE_RB_ENTRY_VALUE(entry, struct WINE_MIDIStream, entry);
948 return memcmp(key, &stream->dwStreamID, sizeof(stream->dwStreamID));
951 static WINE_MIDIStream *wine_midi_stream_allocate(void)
953 DWORD stream_id = 1;
954 WINE_MIDIStream *stream = NULL;
956 EnterCriticalSection(&WINMM_cs);
958 while (stream_id < 0xFFFFFFFF && wine_rb_get(&wine_midi_streams, &stream_id))
959 stream_id++;
961 if (stream_id < 0xFFFFFFFF &&
962 (stream = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream))))
964 stream->dwStreamID = stream_id;
965 wine_rb_put(&wine_midi_streams, &stream_id, &stream->entry);
968 LeaveCriticalSection(&WINMM_cs);
969 return stream;
972 static void wine_midi_stream_free(WINE_MIDIStream *stream)
974 EnterCriticalSection(&WINMM_cs);
976 wine_rb_remove(&wine_midi_streams, &stream->entry);
977 HeapFree(GetProcessHeap(), 0, stream);
979 LeaveCriticalSection(&WINMM_cs);
982 /**************************************************************************
983 * MMSYSTEM_GetMidiStream [internal]
985 static BOOL MMSYSTEM_GetMidiStream(HMIDISTRM hMidiStrm, WINE_MIDIStream** lpMidiStrm, WINE_MIDI** lplpwm)
987 WINE_MIDI* lpwm = (LPWINE_MIDI)MMDRV_Get(hMidiStrm, MMDRV_MIDIOUT, FALSE);
988 struct wine_rb_entry *entry;
990 if (lplpwm)
991 *lplpwm = lpwm;
993 if (lpwm == NULL) {
994 return FALSE;
997 EnterCriticalSection(&WINMM_cs);
998 if ((entry = wine_rb_get(&wine_midi_streams, &lpwm->mod.rgIds.dwStreamID)))
999 *lpMidiStrm = WINE_RB_ENTRY_VALUE(entry, struct WINE_MIDIStream, entry);
1000 LeaveCriticalSection(&WINMM_cs);
1002 return *lpMidiStrm != NULL;
1005 /**************************************************************************
1006 * MMSYSTEM_MidiStream_Convert [internal]
1008 static DWORD MMSYSTEM_MidiStream_Convert(WINE_MIDIStream* lpMidiStrm, DWORD pulse)
1010 DWORD ret = 0;
1012 if (lpMidiStrm->dwTimeDiv == 0) {
1013 FIXME("Shouldn't happen. lpMidiStrm->dwTimeDiv = 0\n");
1014 } else if (lpMidiStrm->dwTimeDiv > 0x8000) { /* SMPTE, unchecked FIXME? */
1015 int nf = 256 - HIBYTE(lpMidiStrm->dwTimeDiv); /* number of frames */
1016 int nsf = LOBYTE(lpMidiStrm->dwTimeDiv); /* number of sub-frames */
1017 ret = (pulse * 1000000) / (nf * nsf);
1018 } else {
1019 ret = (DWORD)((double)pulse * (double)lpMidiStrm->dwTempo /
1020 (double)lpMidiStrm->dwTimeDiv);
1023 return ret; /* in microseconds */
1026 static DWORD midistream_get_playing_position(WINE_MIDIStream* lpMidiStrm)
1028 switch (lpMidiStrm->status) {
1029 case MSM_STATUS_STOPPED:
1030 case MSM_STATUS_PAUSED:
1031 return lpMidiStrm->dwElapsedMS;
1032 case MSM_STATUS_PLAYING:
1033 return timeGetTime() - lpMidiStrm->dwStartTicks;
1034 default:
1035 FIXME("Unknown playing status %hu\n", lpMidiStrm->status);
1036 return 0;
1040 /**************************************************************************
1041 * MMSYSTEM_MidiStream_MessageHandler [internal]
1043 static BOOL MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream* lpMidiStrm, LPWINE_MIDI lpwm, LPMSG msg)
1045 LPMIDIHDR lpMidiHdr;
1046 LPMIDIHDR* lpmh;
1047 LPBYTE lpData;
1049 for (;;) {
1050 switch (msg->message) {
1051 case WM_QUIT:
1052 return FALSE;
1053 case WINE_MSM_STOP:
1054 TRACE("STOP\n");
1055 EnterCriticalSection(&lpMidiStrm->lock);
1056 lpMidiStrm->status = MSM_STATUS_STOPPED;
1057 lpMidiStrm->dwPulses = 0;
1058 lpMidiStrm->dwElapsedMS = 0;
1059 lpMidiStrm->position_usec = 0;
1060 lpMidiStrm->dwLastPositionMS = 0;
1061 LeaveCriticalSection(&lpMidiStrm->lock);
1062 /* this is not quite what MS doc says... */
1063 midiOutReset(lpMidiStrm->hDevice);
1064 /* empty list of already submitted buffers */
1065 lpMidiHdr = lpMidiStrm->lpMidiHdr;
1066 lpMidiStrm->lpMidiHdr = NULL;
1067 while (lpMidiHdr) {
1068 LPMIDIHDR lphdr = lpMidiHdr;
1069 lpMidiHdr = lpMidiHdr->lpNext;
1070 lphdr->dwFlags |= MHDR_DONE;
1071 lphdr->dwFlags &= ~MHDR_INQUEUE;
1073 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1074 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1075 lpwm->mod.dwInstance, (DWORD_PTR)lphdr, 0);
1077 SetEvent((HANDLE)msg->wParam);
1078 return TRUE;
1079 case WINE_MSM_RESUME:
1080 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1081 if (lpMidiStrm->status != MSM_STATUS_PLAYING) {
1082 EnterCriticalSection(&lpMidiStrm->lock);
1083 lpMidiStrm->dwStartTicks = timeGetTime() - lpMidiStrm->dwElapsedMS;
1084 lpMidiStrm->status = MSM_STATUS_PLAYING;
1085 LeaveCriticalSection(&lpMidiStrm->lock);
1087 SetEvent((HANDLE)msg->wParam);
1088 return TRUE;
1089 case WINE_MSM_PAUSE:
1090 /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
1091 if (lpMidiStrm->status != MSM_STATUS_PAUSED) {
1092 EnterCriticalSection(&lpMidiStrm->lock);
1093 lpMidiStrm->dwElapsedMS = timeGetTime() - lpMidiStrm->dwStartTicks;
1094 lpMidiStrm->status = MSM_STATUS_PAUSED;
1095 LeaveCriticalSection(&lpMidiStrm->lock);
1097 SetEvent((HANDLE)msg->wParam);
1098 break;
1099 /* FIXME(EPP): "I don't understand the content of the first MIDIHDR sent
1100 * by native mcimidi, it doesn't look like a correct one".
1101 * this trick allows us to throw it away... but I don't like it.
1102 * It looks like part of the file I'm trying to play and definitively looks
1103 * like raw midi content.
1104 * I'd really like to understand why native mcimidi sends it. Perhaps a bad
1105 * synchronization issue where native mcimidi is still processing raw MIDI
1106 * content before generating MIDIEVENTs ?
1108 * 4c 04 89 3b 00 81 7c 99 3b 43 00 99 23 5e 04 89 L..;..|.;C..#^..
1109 * 3b 00 00 89 23 00 7c 99 3b 45 00 99 28 62 04 89 ;...#.|.;E..(b..
1110 * 3b 00 00 89 28 00 81 7c 99 3b 4e 00 99 23 5e 04 ;...(..|.;N..#^.
1111 * 89 3b 00 00 89 23 00 7c 99 3b 45 00 99 23 78 04 .;...#.|.;E..#x.
1112 * 89 3b 00 00 89 23 00 81 7c 99 3b 48 00 99 23 5e .;...#..|.;H..#^
1113 * 04 89 3b 00 00 89 23 00 7c 99 3b 4e 00 99 28 62 ..;...#.|.;N..(b
1114 * 04 89 3b 00 00 89 28 00 81 7c 99 39 4c 00 99 23 ..;...(..|.9L..#
1115 * 5e 04 89 39 00 00 89 23 00 82 7c 99 3b 4c 00 99 ^..9...#..|.;L..
1116 * 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 00 99 #^..;...#.|.;H..
1117 * 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b 3f 04 (b..;...(..|.;?.
1118 * 89 3b 00 1c 99 23 5e 04 89 23 00 5c 99 3b 45 00 .;...#^..#.\.;E.
1119 * 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 3b 46 .#x..;...#..|.;F
1120 * 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 ..#^..;...#.|.;H
1121 * 00 99 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b ..(b..;...(..|.;
1122 * 46 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b F..#^..;...#.|.;
1123 * 48 00 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 H..#x..;...#..|.
1124 * 3b 4c 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 ;L..#^..;...#.|.
1126 case WINE_MSM_HEADER:
1127 /* sets initial tick count for first MIDIHDR */
1128 if (!lpMidiStrm->dwStartTicks)
1129 lpMidiStrm->dwStartTicks = timeGetTime();
1130 lpMidiHdr = (LPMIDIHDR)msg->lParam;
1131 lpData = (LPBYTE)lpMidiHdr->lpData;
1132 TRACE("Adding %s lpMidiHdr=%p [lpData=0x%p dwBytesRecorded=%u/%u dwFlags=0x%08x size=%lu]\n",
1133 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular", lpMidiHdr,
1134 lpData, lpMidiHdr->dwBytesRecorded, lpMidiHdr->dwBufferLength,
1135 lpMidiHdr->dwFlags, msg->wParam);
1136 #if 0
1137 /* dumps content of lpMidiHdr->lpData
1138 * FIXME: there should be a debug routine somewhere that already does this
1140 for (dwToGo = 0; dwToGo < lpMidiHdr->dwBufferLength; dwToGo += 16) {
1141 DWORD i;
1142 BYTE ch;
1144 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++)
1145 printf("%02x ", lpData[dwToGo + i]);
1146 for (; i < 16; i++)
1147 printf(" ");
1148 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++) {
1149 ch = lpData[dwToGo + i];
1150 printf("%c", (ch >= 0x20 && ch <= 0x7F) ? ch : '.');
1152 printf("\n");
1154 #endif
1155 if (((LPMIDIEVENT)lpData)->dwStreamID != 0 &&
1156 ((LPMIDIEVENT)lpData)->dwStreamID != 0xFFFFFFFF &&
1157 ((LPMIDIEVENT)lpData)->dwStreamID != lpMidiStrm->dwStreamID) {
1158 FIXME("Dropping bad %s lpMidiHdr (streamID=%08x)\n",
1159 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular",
1160 ((LPMIDIEVENT)lpData)->dwStreamID);
1161 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1162 lpMidiHdr->dwFlags |= MHDR_DONE;
1164 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1165 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1166 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1167 break;
1170 lpMidiHdr->lpNext = 0;
1171 for (lpmh = &lpMidiStrm->lpMidiHdr; *lpmh; lpmh = &(*lpmh)->lpNext);
1172 *lpmh = lpMidiHdr;
1173 break;
1174 default:
1175 FIXME("Unknown message %d\n", msg->message);
1176 break;
1178 if (lpMidiStrm->status != MSM_STATUS_PAUSED)
1179 return TRUE;
1180 GetMessageA(msg, 0, 0, 0);
1184 /**************************************************************************
1185 * MMSYSTEM_MidiStream_Player [internal]
1187 static DWORD CALLBACK MMSYSTEM_MidiStream_Player(LPVOID pmt)
1189 WINE_MIDIStream* lpMidiStrm = pmt;
1190 WINE_MIDI* lpwm;
1191 MSG msg;
1192 DWORD dwToGo;
1193 DWORD dwCurrTC;
1194 LPMIDIHDR lpMidiHdr;
1195 DWORD dwOffset;
1197 TRACE("(%p)!\n", lpMidiStrm);
1199 if (!lpMidiStrm ||
1200 (lpwm = (LPWINE_MIDI)MMDRV_Get(lpMidiStrm->hDevice, MMDRV_MIDIOUT, FALSE)) == NULL)
1201 goto the_end;
1203 /* force thread's queue creation */
1204 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
1206 lpMidiStrm->dwStartTicks = 0;
1207 lpMidiStrm->dwPulses = 0;
1209 lpMidiStrm->lpMidiHdr = 0;
1211 /* midiStreamOpen is waiting for ack */
1212 SetEvent(lpMidiStrm->hEvent);
1214 start_header:
1215 lpMidiHdr = lpMidiStrm->lpMidiHdr;
1216 if (!lpMidiHdr) {
1217 /* for first message, block until one arrives, then process all that are available */
1218 GetMessageA(&msg, 0, 0, 0);
1219 do {
1220 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1221 goto the_end;
1222 } while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE));
1223 goto start_header;
1226 dwOffset = 0;
1227 while (dwOffset + offsetof(MIDIEVENT,dwParms) <= lpMidiHdr->dwBytesRecorded) {
1228 LPMIDIEVENT me = (LPMIDIEVENT)(lpMidiHdr->lpData+dwOffset);
1230 /* do we have to wait ? */
1231 if (me->dwDeltaTime) {
1232 EnterCriticalSection(&lpMidiStrm->lock);
1233 lpMidiStrm->position_usec += MMSYSTEM_MidiStream_Convert(lpMidiStrm, me->dwDeltaTime);
1234 LeaveCriticalSection(&lpMidiStrm->lock);
1236 dwToGo = lpMidiStrm->dwStartTicks + lpMidiStrm->position_usec / 1000;
1238 TRACE("%u/%u/%u\n", dwToGo, timeGetTime(), me->dwDeltaTime);
1239 while (dwToGo - (dwCurrTC = timeGetTime()) <= MAXLONG) {
1240 if (MsgWaitForMultipleObjects(0, NULL, FALSE, dwToGo - dwCurrTC, QS_ALLINPUT) == WAIT_OBJECT_0) {
1241 /* got a message, handle it */
1242 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
1243 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1244 goto the_end;
1245 /* is lpMidiHdr still current? */
1246 if (lpMidiHdr != lpMidiStrm->lpMidiHdr) {
1247 goto start_header;
1250 /* reset dwToGo because dwStartTicks might be updated */
1251 dwToGo = lpMidiStrm->dwStartTicks + lpMidiStrm->position_usec / 1000;
1252 } else {
1253 /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
1254 break;
1257 EnterCriticalSection(&lpMidiStrm->lock);
1258 lpMidiStrm->dwPulses += me->dwDeltaTime;
1259 lpMidiStrm->dwLastPositionMS = midistream_get_playing_position(lpMidiStrm);
1260 LeaveCriticalSection(&lpMidiStrm->lock);
1262 switch (MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK)) {
1263 case MEVT_COMMENT:
1264 FIXME("NIY: MEVT_COMMENT\n");
1265 /* do nothing, skip bytes */
1266 break;
1267 case MEVT_LONGMSG:
1269 MIDIHDR mh;
1270 memset(&mh, 0, sizeof(mh));
1271 mh.lpData = (LPSTR)me->dwParms;
1272 mh.dwBufferLength = MEVT_EVENTPARM(me->dwEvent);
1273 midiOutPrepareHeader(lpMidiStrm->hDevice, &mh, sizeof(mh));
1274 midiOutLongMsg(lpMidiStrm->hDevice, &mh, sizeof(mh));
1275 midiOutUnprepareHeader(lpMidiStrm->hDevice, &mh, sizeof(mh));
1276 break;
1278 case MEVT_NOP:
1279 break;
1280 case MEVT_SHORTMSG:
1281 midiOutShortMsg(lpMidiStrm->hDevice, MEVT_EVENTPARM(me->dwEvent));
1282 break;
1283 case MEVT_TEMPO:
1284 EnterCriticalSection(&lpMidiStrm->lock);
1285 lpMidiStrm->dwTempo = MEVT_EVENTPARM(me->dwEvent);
1286 LeaveCriticalSection(&lpMidiStrm->lock);
1287 break;
1288 case MEVT_VERSION:
1289 break;
1290 default:
1291 FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK));
1292 break;
1294 if (me->dwEvent & MEVT_F_CALLBACK) {
1295 /* native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
1296 lpMidiHdr->dwOffset = dwOffset;
1297 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1298 (HDRVR)lpMidiStrm->hDevice, MM_MOM_POSITIONCB,
1299 lpwm->mod.dwInstance, (LPARAM)lpMidiHdr, 0L);
1301 dwOffset += offsetof(MIDIEVENT,dwParms);
1302 if (me->dwEvent & MEVT_F_LONG)
1303 dwOffset += (MEVT_EVENTPARM(me->dwEvent) + 3) & ~3;
1305 /* done with this header */
1306 lpMidiStrm->lpMidiHdr = lpMidiHdr->lpNext;
1307 lpMidiHdr->dwFlags |= MHDR_DONE;
1308 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1310 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1311 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1312 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1313 goto start_header;
1315 the_end:
1316 TRACE("End of thread\n");
1317 return 0;
1320 /**************************************************************************
1321 * midiStreamClose [WINMM.@]
1323 MMRESULT WINAPI midiStreamClose(HMIDISTRM hMidiStrm)
1325 WINE_MIDI* lpwm;
1326 WINE_MIDIStream* lpMidiStrm;
1327 MMRESULT ret = 0;
1329 TRACE("(%p)!\n", hMidiStrm);
1331 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, &lpwm))
1332 return MMSYSERR_INVALHANDLE;
1334 midiStreamStop(hMidiStrm);
1335 PostThreadMessageA(lpMidiStrm->dwThreadID, WM_QUIT, 0, 0);
1336 CloseHandle(lpMidiStrm->hEvent);
1337 if (lpMidiStrm->hThread) {
1338 if (GetCurrentThreadId() != lpMidiStrm->dwThreadID)
1339 WaitForSingleObject(lpMidiStrm->hThread, INFINITE);
1340 else {
1341 FIXME("leak from call within function callback\n");
1342 ret = MMSYSERR_HANDLEBUSY; /* yet don't signal it to app */
1344 CloseHandle(lpMidiStrm->hThread);
1346 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1347 (HDRVR)lpMidiStrm->hDevice, MM_MOM_CLOSE,
1348 lpwm->mod.dwInstance, 0, 0);
1349 if(!ret) {
1350 lpMidiStrm->lock.DebugInfo->Spare[0] = 0;
1351 DeleteCriticalSection(&lpMidiStrm->lock);
1352 wine_midi_stream_free(lpMidiStrm);
1355 return midiOutClose((HMIDIOUT)hMidiStrm);
1358 /**************************************************************************
1359 * midiStreamOpen [WINMM.@]
1361 MMRESULT WINAPI midiStreamOpen(HMIDISTRM* lphMidiStrm, LPUINT lpuDeviceID,
1362 DWORD cMidi, DWORD_PTR dwCallback,
1363 DWORD_PTR dwInstance, DWORD fdwOpen)
1365 WINE_MIDIStream* lpMidiStrm;
1366 MMRESULT ret;
1367 MIDIOPENSTRMID mosm;
1368 LPWINE_MIDI lpwm;
1369 HMIDIOUT hMidiOut;
1371 TRACE("(%p, %p, %d, 0x%08lx, 0x%08lx, 0x%08x)!\n",
1372 lphMidiStrm, lpuDeviceID, cMidi, dwCallback, dwInstance, fdwOpen);
1374 if (cMidi != 1 || lphMidiStrm == NULL || lpuDeviceID == NULL)
1375 return MMSYSERR_INVALPARAM;
1377 ret = WINMM_CheckCallback(dwCallback, fdwOpen, FALSE);
1378 if (ret != MMSYSERR_NOERROR)
1379 return ret;
1381 lpMidiStrm = wine_midi_stream_allocate();
1382 if (!lpMidiStrm)
1383 return MMSYSERR_NOMEM;
1385 lpMidiStrm->dwTempo = 500000; /* microseconds per quarter note, i.e. 120 BPM */
1386 lpMidiStrm->dwTimeDiv = 24; /* ticks per quarter note */
1387 lpMidiStrm->position_usec = 0;
1388 lpMidiStrm->dwLastPositionMS = 0;
1389 lpMidiStrm->status = MSM_STATUS_PAUSED;
1390 lpMidiStrm->dwElapsedMS = 0;
1392 mosm.dwStreamID = lpMidiStrm->dwStreamID;
1393 /* FIXME: the correct value is not allocated yet for MAPPER */
1394 mosm.wDeviceID = *lpuDeviceID;
1395 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &fdwOpen, 1, &mosm);
1396 if (!lpwm) {
1397 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1398 return MMSYSERR_NOMEM;
1400 lpMidiStrm->hDevice = hMidiOut;
1401 *lphMidiStrm = (HMIDISTRM)hMidiOut;
1403 lpwm->mld.uDeviceID = *lpuDeviceID;
1405 /* don't rely on midiOut callbacks */
1406 ret = MMDRV_Open(&lpwm->mld, MODM_OPEN, (DWORD_PTR)&lpwm->mod, CALLBACK_NULL);
1407 if (ret != MMSYSERR_NOERROR) {
1408 MMDRV_Free(hMidiOut, &lpwm->mld);
1409 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1410 return ret;
1413 InitializeCriticalSection(&lpMidiStrm->lock);
1414 lpMidiStrm->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINMM_MidiStream.lock");
1416 lpMidiStrm->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1417 lpMidiStrm->wFlags = HIWORD(fdwOpen);
1419 lpMidiStrm->hThread = CreateThread(NULL, 0, MMSYSTEM_MidiStream_Player,
1420 lpMidiStrm, 0, &(lpMidiStrm->dwThreadID));
1422 if (!lpMidiStrm->hThread) {
1423 midiStreamClose((HMIDISTRM)hMidiOut);
1424 return MMSYSERR_NOMEM;
1426 SetThreadPriority(lpMidiStrm->hThread, THREAD_PRIORITY_TIME_CRITICAL);
1428 /* wait for thread to have started, and for its queue to be created */
1429 WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
1431 TRACE("=> (%u/%d) hMidi=%p ret=%d lpMidiStrm=%p\n",
1432 *lpuDeviceID, lpwm->mld.uDeviceID, *lphMidiStrm, ret, lpMidiStrm);
1434 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1435 (HDRVR)lpMidiStrm->hDevice, MM_MOM_OPEN,
1436 lpwm->mod.dwInstance, 0, 0);
1437 return ret;
1440 /**************************************************************************
1441 * midiStreamOut [WINMM.@]
1443 MMRESULT WINAPI midiStreamOut(HMIDISTRM hMidiStrm, LPMIDIHDR lpMidiHdr,
1444 UINT cbMidiHdr)
1446 WINE_MIDIStream* lpMidiStrm;
1447 DWORD ret = MMSYSERR_NOERROR;
1449 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMidiHdr, cbMidiHdr);
1451 if (cbMidiHdr < offsetof(MIDIHDR,dwOffset) || !lpMidiHdr || !lpMidiHdr->lpData
1452 || lpMidiHdr->dwBufferLength < lpMidiHdr->dwBytesRecorded
1453 || lpMidiHdr->dwBytesRecorded % 4 /* player expects DWORD padding */)
1454 return MMSYSERR_INVALPARAM;
1455 /* FIXME: Native additionally checks if the MIDIEVENTs in lpData
1456 * exactly fit dwBytesRecorded. */
1458 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1459 return MIDIERR_UNPREPARED;
1461 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1462 return MIDIERR_STILLPLAYING;
1464 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1465 ret = MMSYSERR_INVALHANDLE;
1466 } else {
1467 lpMidiHdr->dwFlags |= MHDR_ISSTRM | MHDR_INQUEUE;
1468 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1469 if (!PostThreadMessageA(lpMidiStrm->dwThreadID,
1470 WINE_MSM_HEADER, cbMidiHdr,
1471 (LPARAM)lpMidiHdr)) {
1472 ERR("bad PostThreadMessageA\n");
1473 ret = MMSYSERR_ERROR;
1476 return ret;
1479 static MMRESULT midistream_post_message_and_wait(WINE_MIDIStream* lpMidiStrm, UINT msg, LPARAM lParam)
1481 HANDLE hObjects[2];
1483 hObjects[0] = CreateEventW(NULL, FALSE, FALSE, NULL);
1484 if (!hObjects[0])
1485 return MMSYSERR_ERROR;
1487 if (!PostThreadMessageA(lpMidiStrm->dwThreadID, msg, (WPARAM)hObjects[0], lParam)) {
1488 WARN("bad PostThreadMessage\n");
1489 CloseHandle(hObjects[0]);
1490 return MMSYSERR_ERROR;
1493 if (GetCurrentThreadId() != lpMidiStrm->dwThreadID) {
1494 DWORD ret;
1495 hObjects[1] = lpMidiStrm->hThread;
1496 ret = WaitForMultipleObjects(ARRAY_SIZE(hObjects), hObjects, FALSE, INFINITE);
1497 if (ret != WAIT_OBJECT_0) {
1498 CloseHandle(hObjects[0]);
1499 WARN("bad WaitForSingleObject (%u)\n", ret);
1500 return MMSYSERR_ERROR;
1504 CloseHandle(hObjects[0]);
1506 return MMSYSERR_NOERROR;
1509 /**************************************************************************
1510 * midiStreamPause [WINMM.@]
1512 MMRESULT WINAPI midiStreamPause(HMIDISTRM hMidiStrm)
1514 WINE_MIDIStream* lpMidiStrm;
1516 TRACE("(%p)!\n", hMidiStrm);
1518 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1519 return MMSYSERR_INVALHANDLE;
1520 return midistream_post_message_and_wait(lpMidiStrm, WINE_MSM_PAUSE, 0);
1523 static DWORD midistream_get_current_pulse(WINE_MIDIStream* lpMidiStrm)
1525 DWORD pulses = 0;
1526 DWORD now = midistream_get_playing_position(lpMidiStrm);
1527 DWORD delta = now - lpMidiStrm->dwLastPositionMS;
1528 if (lpMidiStrm->dwTimeDiv > 0x8000) {
1529 /* SMPTE, unchecked FIXME */
1530 BYTE nf = 256 - HIBYTE(lpMidiStrm->dwTimeDiv);
1531 BYTE nsf = LOBYTE(lpMidiStrm->dwTimeDiv);
1532 pulses = (delta * nf * nsf) / 1000;
1534 else if (lpMidiStrm->dwTimeDiv) {
1535 pulses = (DWORD)((double)(delta * lpMidiStrm->dwTimeDiv) *
1536 1000.0 / (double)lpMidiStrm->dwTempo);
1538 return lpMidiStrm->dwPulses + pulses;
1541 /**************************************************************************
1542 * midiStreamPosition [WINMM.@]
1544 MMRESULT WINAPI midiStreamPosition(HMIDISTRM hMidiStrm, LPMMTIME lpMMT, UINT cbmmt)
1546 WINE_MIDIStream* lpMidiStrm;
1547 DWORD ret = MMSYSERR_NOERROR;
1549 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMMT, cbmmt);
1551 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1552 ret = MMSYSERR_INVALHANDLE;
1553 } else if (lpMMT == NULL || cbmmt != sizeof(MMTIME)) {
1554 ret = MMSYSERR_INVALPARAM;
1555 } else {
1556 EnterCriticalSection(&lpMidiStrm->lock);
1557 switch (lpMMT->wType) {
1558 case TIME_MIDI:
1559 if (lpMidiStrm->dwTimeDiv < 0x8000) {
1560 DWORD tdiv, pulses;
1561 tdiv = (lpMidiStrm->dwTimeDiv > 24) ? lpMidiStrm->dwTimeDiv : 24;
1562 pulses = midistream_get_current_pulse(lpMidiStrm);
1563 lpMMT->u.midi.songptrpos = (pulses + tdiv/8) / (tdiv/4);
1564 if (!lpMMT->u.midi.songptrpos && pulses) lpMMT->u.midi.songptrpos++;
1565 TRACE("=> song position %d (pulses %u, tdiv %u)\n", lpMMT->u.midi.songptrpos, pulses, tdiv);
1566 break;
1568 /* fall through */
1569 case TIME_BYTES:
1570 case TIME_SAMPLES:
1571 lpMMT->wType = TIME_MS;
1572 /* fall through to alternative format */
1573 case TIME_MS:
1574 lpMMT->u.ms = midistream_get_playing_position(lpMidiStrm);
1575 TRACE("=> %d ms\n", lpMMT->u.ms);
1576 break;
1577 case TIME_TICKS:
1578 lpMMT->u.ticks = midistream_get_current_pulse(lpMidiStrm);
1579 TRACE("=> %d ticks\n", lpMMT->u.ticks);
1580 break;
1581 default:
1582 FIXME("Unsupported time type %x\n", lpMMT->wType);
1583 /* use TIME_MS instead */
1584 lpMMT->wType = TIME_MS;
1585 lpMMT->u.ms = midistream_get_playing_position(lpMidiStrm);
1586 TRACE("=> %d ms\n", lpMMT->u.ms);
1587 break;
1589 LeaveCriticalSection(&lpMidiStrm->lock);
1591 return ret;
1594 /**************************************************************************
1595 * midiStreamProperty [WINMM.@]
1597 MMRESULT WINAPI midiStreamProperty(HMIDISTRM hMidiStrm, LPBYTE lpPropData, DWORD dwProperty)
1599 WINE_MIDIStream* lpMidiStrm;
1600 MMRESULT ret = MMSYSERR_NOERROR;
1602 TRACE("(%p, %p, %x)\n", hMidiStrm, lpPropData, dwProperty);
1604 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1605 ret = MMSYSERR_INVALHANDLE;
1606 } else if ((dwProperty & (MIDIPROP_GET|MIDIPROP_SET)) == 0) {
1607 ret = MMSYSERR_INVALPARAM;
1608 } else if (dwProperty & MIDIPROP_TEMPO) {
1609 MIDIPROPTEMPO* mpt = (MIDIPROPTEMPO*)lpPropData;
1611 EnterCriticalSection(&lpMidiStrm->lock);
1612 if (sizeof(MIDIPROPTEMPO) != mpt->cbStruct) {
1613 ret = MMSYSERR_INVALPARAM;
1614 } else if (dwProperty & MIDIPROP_SET) {
1615 lpMidiStrm->dwTempo = mpt->dwTempo;
1616 TRACE("Setting tempo to %d\n", mpt->dwTempo);
1617 } else if (dwProperty & MIDIPROP_GET) {
1618 mpt->dwTempo = lpMidiStrm->dwTempo;
1619 TRACE("Getting tempo <= %d\n", mpt->dwTempo);
1621 LeaveCriticalSection(&lpMidiStrm->lock);
1622 } else if (dwProperty & MIDIPROP_TIMEDIV) {
1623 MIDIPROPTIMEDIV* mptd = (MIDIPROPTIMEDIV*)lpPropData;
1625 if (sizeof(MIDIPROPTIMEDIV) != mptd->cbStruct) {
1626 ret = MMSYSERR_INVALPARAM;
1627 } else if (dwProperty & MIDIPROP_SET) {
1628 EnterCriticalSection(&lpMidiStrm->lock);
1629 if (lpMidiStrm->status != MSM_STATUS_PLAYING) {
1630 lpMidiStrm->dwTimeDiv = mptd->dwTimeDiv;
1631 TRACE("Setting time div to %d\n", mptd->dwTimeDiv);
1633 else
1634 ret = MMSYSERR_INVALPARAM;
1635 LeaveCriticalSection(&lpMidiStrm->lock);
1636 } else if (dwProperty & MIDIPROP_GET) {
1637 mptd->dwTimeDiv = lpMidiStrm->dwTimeDiv;
1638 TRACE("Getting time div <= %d\n", mptd->dwTimeDiv);
1640 } else {
1641 ret = MMSYSERR_INVALPARAM;
1644 return ret;
1647 /**************************************************************************
1648 * midiStreamRestart [WINMM.@]
1650 MMRESULT WINAPI midiStreamRestart(HMIDISTRM hMidiStrm)
1652 WINE_MIDIStream* lpMidiStrm;
1654 TRACE("(%p)!\n", hMidiStrm);
1656 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1657 return MMSYSERR_INVALHANDLE;
1658 return midistream_post_message_and_wait(lpMidiStrm, WINE_MSM_RESUME, 0);
1661 /**************************************************************************
1662 * midiStreamStop [WINMM.@]
1664 MMRESULT WINAPI midiStreamStop(HMIDISTRM hMidiStrm)
1666 WINE_MIDIStream* lpMidiStrm;
1668 TRACE("(%p)!\n", hMidiStrm);
1670 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1671 return MMSYSERR_INVALHANDLE;
1672 return midistream_post_message_and_wait(lpMidiStrm, WINE_MSM_STOP, 0);
1675 struct mm_starter
1677 LPTASKCALLBACK cb;
1678 DWORD client;
1679 HANDLE event;
1682 static DWORD WINAPI mmTaskRun(void* pmt)
1684 struct mm_starter mms;
1686 memcpy(&mms, pmt, sizeof(struct mm_starter));
1687 HeapFree(GetProcessHeap(), 0, pmt);
1688 mms.cb(mms.client);
1689 if (mms.event) SetEvent(mms.event);
1690 return 0;
1693 /******************************************************************
1694 * mmTaskCreate (WINMM.@)
1696 UINT WINAPI mmTaskCreate(LPTASKCALLBACK cb, HANDLE* ph, DWORD_PTR client)
1698 HANDLE hThread;
1699 HANDLE hEvent = 0;
1700 struct mm_starter *mms;
1702 mms = HeapAlloc(GetProcessHeap(), 0, sizeof(struct mm_starter));
1703 if (mms == NULL) return TASKERR_OUTOFMEMORY;
1705 mms->cb = cb;
1706 mms->client = client;
1707 if (ph) hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1708 mms->event = hEvent;
1710 hThread = CreateThread(0, 0, mmTaskRun, mms, 0, NULL);
1711 if (!hThread) {
1712 HeapFree(GetProcessHeap(), 0, mms);
1713 if (hEvent) CloseHandle(hEvent);
1714 return TASKERR_OUTOFMEMORY;
1716 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
1717 if (ph) *ph = hEvent;
1718 CloseHandle(hThread);
1719 return 0;
1722 /******************************************************************
1723 * mmTaskBlock (WINMM.@)
1725 VOID WINAPI mmTaskBlock(DWORD tid)
1727 MSG msg;
1731 GetMessageA(&msg, 0, 0, 0);
1732 if (msg.hwnd) DispatchMessageA(&msg);
1733 } while (msg.message != WM_USER);
1736 /******************************************************************
1737 * mmTaskSignal (WINMM.@)
1739 BOOL WINAPI mmTaskSignal(DWORD tid)
1741 return PostThreadMessageW(tid, WM_USER, 0, 0);
1744 /******************************************************************
1745 * mmTaskYield (WINMM.@)
1747 VOID WINAPI mmTaskYield(VOID) {}
1749 /******************************************************************
1750 * mmGetCurrentTask (WINMM.@)
1752 DWORD WINAPI mmGetCurrentTask(VOID)
1754 return GetCurrentThreadId();