winmm: dwBytesRecorded controls how much is played, not dwBufferLength.
[wine/hramrach.git] / dlls / winmm / winmm.c
blob46a3c068007433338fa555bd19baa911286d5df2
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * WINMM functions
6 * Copyright 1993 Martin Ayotte
7 * 1998-2002 Eric Pouech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * Eric POUECH :
26 * 98/9 added Win32 MCI support
27 * 99/4 added midiStream support
28 * 99/9 added support for loadable low level drivers
31 /* TODO
32 * + it seems that some programs check what's installed in
33 * registry against the value returned by drivers. Wine is
34 * currently broken regarding this point.
35 * + check thread-safeness for MMSYSTEM and WINMM entry points
36 * + unicode entry points are badly supported (would require
37 * moving 32 bit drivers as Unicode as they are supposed to be)
38 * + allow joystick and timer external calls as we do for wave,
39 * midi, mixer and aux
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <string.h>
46 #define NONAMELESSUNION
47 #define NONAMELESSSTRUCT
48 #include "windef.h"
49 #include "winbase.h"
50 #include "mmsystem.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "winternl.h"
54 #include "winemm.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
60 /* ========================================================================
61 * G L O B A L S E T T I N G S
62 * ========================================================================*/
64 HINSTANCE hWinMM32Instance;
65 HANDLE psLastEvent;
66 HANDLE psStopEvent;
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 /**************************************************************************
77 * WINMM_CreateIData [internal]
79 static BOOL WINMM_CreateIData(HINSTANCE hInstDLL)
81 hWinMM32Instance = hInstDLL;
82 psStopEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
83 psLastEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
84 return TRUE;
87 /**************************************************************************
88 * WINMM_DeleteIData [internal]
90 static void WINMM_DeleteIData(void)
92 TIME_MMTimeStop();
94 /* FIXME: should also free content and resources allocated
95 * inside WINMM_IData */
96 CloseHandle(psStopEvent);
97 CloseHandle(psLastEvent);
98 DeleteCriticalSection(&WINMM_cs);
101 /******************************************************************
102 * WINMM_ErrorToString
104 const char* WINMM_ErrorToString(MMRESULT error)
106 #define ERR_TO_STR(dev) case dev: return #dev
107 switch (error) {
108 ERR_TO_STR(MMSYSERR_NOERROR);
109 ERR_TO_STR(MMSYSERR_ERROR);
110 ERR_TO_STR(MMSYSERR_BADDEVICEID);
111 ERR_TO_STR(MMSYSERR_NOTENABLED);
112 ERR_TO_STR(MMSYSERR_ALLOCATED);
113 ERR_TO_STR(MMSYSERR_INVALHANDLE);
114 ERR_TO_STR(MMSYSERR_NODRIVER);
115 ERR_TO_STR(MMSYSERR_NOMEM);
116 ERR_TO_STR(MMSYSERR_NOTSUPPORTED);
117 ERR_TO_STR(MMSYSERR_BADERRNUM);
118 ERR_TO_STR(MMSYSERR_INVALFLAG);
119 ERR_TO_STR(MMSYSERR_INVALPARAM);
120 ERR_TO_STR(MMSYSERR_HANDLEBUSY);
121 ERR_TO_STR(MMSYSERR_INVALIDALIAS);
122 ERR_TO_STR(MMSYSERR_BADDB);
123 ERR_TO_STR(MMSYSERR_KEYNOTFOUND);
124 ERR_TO_STR(MMSYSERR_READERROR);
125 ERR_TO_STR(MMSYSERR_WRITEERROR);
126 ERR_TO_STR(MMSYSERR_DELETEERROR);
127 ERR_TO_STR(MMSYSERR_VALNOTFOUND);
128 ERR_TO_STR(MMSYSERR_NODRIVERCB);
129 ERR_TO_STR(WAVERR_BADFORMAT);
130 ERR_TO_STR(WAVERR_STILLPLAYING);
131 ERR_TO_STR(WAVERR_UNPREPARED);
132 ERR_TO_STR(WAVERR_SYNC);
133 ERR_TO_STR(MIDIERR_INVALIDSETUP);
134 ERR_TO_STR(MIDIERR_NODEVICE);
135 ERR_TO_STR(MIDIERR_STILLPLAYING);
136 ERR_TO_STR(MIDIERR_UNPREPARED);
138 #undef ERR_TO_STR
139 return wine_dbg_sprintf("Unknown(0x%08x)", error);
142 /**************************************************************************
143 * DllMain (WINMM.init)
145 * WINMM DLL entry point
148 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
150 TRACE("%p 0x%x %p\n", hInstDLL, fdwReason, fImpLoad);
152 switch (fdwReason) {
153 case DLL_PROCESS_ATTACH:
154 DisableThreadLibraryCalls(hInstDLL);
156 if (!WINMM_CreateIData(hInstDLL))
157 return FALSE;
158 if (!MMDRV_Init()) {
159 WINMM_DeleteIData();
160 return FALSE;
162 break;
163 case DLL_PROCESS_DETACH:
164 /* close all opened MCI drivers */
165 MCI_SendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0L);
166 MMDRV_Exit();
167 /* There's no guarantee the drivers haven't already been unloaded on
168 * process shutdown.
170 if (!fImpLoad)
172 /* now unload all remaining drivers... */
173 DRIVER_UnloadAll();
176 WINMM_DeleteIData();
177 break;
179 return TRUE;
182 /**************************************************************************
183 * Mixer devices. New to Win95
186 /**************************************************************************
187 * find out the real mixer ID depending on hmix (depends on dwFlags)
189 static UINT MIXER_GetDev(HMIXEROBJ hmix, DWORD dwFlags, LPWINE_MIXER * lplpwm)
191 LPWINE_MIXER lpwm = NULL;
192 UINT uRet = MMSYSERR_NOERROR;
194 switch (dwFlags & 0xF0000000ul) {
195 case MIXER_OBJECTF_MIXER:
196 lpwm = (LPWINE_MIXER)MMDRV_Get(hmix, MMDRV_MIXER, TRUE);
197 break;
198 case MIXER_OBJECTF_HMIXER:
199 lpwm = (LPWINE_MIXER)MMDRV_Get(hmix, MMDRV_MIXER, FALSE);
200 break;
201 case MIXER_OBJECTF_WAVEOUT:
202 lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEOUT, TRUE, MMDRV_MIXER);
203 break;
204 case MIXER_OBJECTF_HWAVEOUT:
205 lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEOUT, FALSE, MMDRV_MIXER);
206 break;
207 case MIXER_OBJECTF_WAVEIN:
208 lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEIN, TRUE, MMDRV_MIXER);
209 break;
210 case MIXER_OBJECTF_HWAVEIN:
211 lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEIN, FALSE, MMDRV_MIXER);
212 break;
213 case MIXER_OBJECTF_MIDIOUT:
214 lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIOUT, TRUE, MMDRV_MIXER);
215 break;
216 case MIXER_OBJECTF_HMIDIOUT:
217 lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIOUT, FALSE, MMDRV_MIXER);
218 break;
219 case MIXER_OBJECTF_MIDIIN:
220 lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIIN, TRUE, MMDRV_MIXER);
221 break;
222 case MIXER_OBJECTF_HMIDIIN:
223 lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIIN, FALSE, MMDRV_MIXER);
224 break;
225 case MIXER_OBJECTF_AUX:
226 lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_AUX, TRUE, MMDRV_MIXER);
227 break;
228 default:
229 WARN("Unsupported flag (%08lx)\n", dwFlags & 0xF0000000ul);
230 lpwm = 0;
231 uRet = MMSYSERR_INVALFLAG;
232 break;
234 *lplpwm = lpwm;
235 if (lpwm == 0 && uRet == MMSYSERR_NOERROR)
236 uRet = MMSYSERR_INVALPARAM;
237 return uRet;
240 /**************************************************************************
241 * mixerGetNumDevs [WINMM.@]
243 UINT WINAPI mixerGetNumDevs(void)
245 return MMDRV_GetNum(MMDRV_MIXER);
248 /**************************************************************************
249 * mixerGetDevCapsA [WINMM.@]
251 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
253 MIXERCAPSW micW;
254 UINT ret;
256 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
258 ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
260 if (ret == MMSYSERR_NOERROR) {
261 MIXERCAPSA micA;
262 micA.wMid = micW.wMid;
263 micA.wPid = micW.wPid;
264 micA.vDriverVersion = micW.vDriverVersion;
265 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
266 sizeof(micA.szPname), NULL, NULL );
267 micA.fdwSupport = micW.fdwSupport;
268 micA.cDestinations = micW.cDestinations;
269 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
271 return ret;
274 /**************************************************************************
275 * mixerGetDevCapsW [WINMM.@]
277 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
279 LPWINE_MLD wmld;
281 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
283 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIXER, TRUE)) == NULL)
284 return MMSYSERR_BADDEVICEID;
286 return MMDRV_Message(wmld, MXDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
289 static void CALLBACK MIXER_WCallback(HMIXEROBJ hmx, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam, DWORD_PTR param2)
291 HWND hWnd = (HWND)dwInstance;
293 if (!dwInstance)
294 return;
296 PostMessageW(hWnd, uMsg, (WPARAM)hmx, (LPARAM)dwParam);
299 /**************************************************************************
300 * mixerOpen [WINMM.@]
302 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
303 DWORD_PTR dwInstance, DWORD fdwOpen)
305 HANDLE hMix;
306 LPWINE_MLD wmld;
307 DWORD dwRet = 0;
308 MIXEROPENDESC mod;
310 TRACE("(%p, %d, %08lx, %08lx, %08x)\n",
311 lphMix, uDeviceID, dwCallback, dwInstance, fdwOpen);
313 mod.dwCallback = (DWORD_PTR)MIXER_WCallback;
314 mod.dwInstance = 0;
316 /* If callback is a function,
317 * dwCallback contains function pointer
318 * dwInstance private data
320 * if callback is a window
321 * dwCallback contains a window handle
323 switch (fdwOpen & CALLBACK_TYPEMASK) {
324 default:
325 return MMSYSERR_INVALFLAG;
327 case CALLBACK_NULL:
328 break;
330 case CALLBACK_WINDOW:
331 mod.dwInstance = dwCallback;
332 if (dwCallback && !IsWindow((HWND)dwCallback))
333 return MMSYSERR_INVALPARAM;
334 break;
337 wmld = MMDRV_Alloc(sizeof(WINE_MIXER), MMDRV_MIXER, &hMix, &fdwOpen,
338 &dwCallback, &dwInstance);
339 wmld->uDeviceID = uDeviceID;
340 mod.hmx = hMix;
342 dwRet = MMDRV_Open(wmld, MXDM_OPEN, (DWORD_PTR)&mod, CALLBACK_FUNCTION);
344 if (dwRet != MMSYSERR_NOERROR) {
345 MMDRV_Free(hMix, wmld);
346 hMix = 0;
348 if (lphMix) *lphMix = hMix;
349 TRACE("=> %d hMixer=%p\n", dwRet, hMix);
351 return dwRet;
354 /**************************************************************************
355 * mixerClose [WINMM.@]
357 UINT WINAPI mixerClose(HMIXER hMix)
359 LPWINE_MLD wmld;
360 DWORD dwRet;
362 TRACE("(%p)\n", hMix);
364 if ((wmld = MMDRV_Get(hMix, MMDRV_MIXER, FALSE)) == NULL) return MMSYSERR_INVALHANDLE;
366 dwRet = MMDRV_Close(wmld, MXDM_CLOSE);
367 MMDRV_Free(hMix, wmld);
369 return dwRet;
372 /**************************************************************************
373 * mixerGetID [WINMM.@]
375 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
377 LPWINE_MIXER lpwm;
378 UINT uRet = MMSYSERR_NOERROR;
380 TRACE("(%p %p %08x)\n", hmix, lpid, fdwID);
382 if ((uRet = MIXER_GetDev(hmix, fdwID, &lpwm)) != MMSYSERR_NOERROR)
383 return uRet;
385 if (lpid)
386 *lpid = lpwm->mld.uDeviceID;
388 return uRet;
391 /**************************************************************************
392 * mixerGetControlDetailsW [WINMM.@]
394 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
395 DWORD fdwDetails)
397 LPWINE_MIXER lpwm;
398 UINT uRet = MMSYSERR_NOERROR;
400 TRACE("(%p, %p, %08x)\n", hmix, lpmcdW, fdwDetails);
402 if ((uRet = MIXER_GetDev(hmix, fdwDetails, &lpwm)) != MMSYSERR_NOERROR)
403 return uRet;
405 if (lpmcdW == NULL || lpmcdW->cbStruct != sizeof(*lpmcdW))
406 return MMSYSERR_INVALPARAM;
408 return MMDRV_Message(&lpwm->mld, MXDM_GETCONTROLDETAILS, (DWORD_PTR)lpmcdW,
409 fdwDetails);
412 /**************************************************************************
413 * mixerGetControlDetailsA [WINMM.@]
415 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
416 DWORD fdwDetails)
418 DWORD ret = MMSYSERR_NOTENABLED;
420 TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
422 if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
423 return MMSYSERR_INVALPARAM;
425 switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
426 case MIXER_GETCONTROLDETAILSF_VALUE:
427 /* can safely use A structure as it is, no string inside */
428 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
429 break;
430 case MIXER_GETCONTROLDETAILSF_LISTTEXT:
432 MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
433 MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
434 int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
435 unsigned int i;
437 if (lpmcdA->u.cMultipleItems != 0) {
438 size *= lpmcdA->u.cMultipleItems;
440 pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
441 lpmcdA->paDetails = pDetailsW;
442 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
443 /* set up lpmcd->paDetails */
444 ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
445 /* copy from lpmcd->paDetails back to paDetailsW; */
446 if (ret == MMSYSERR_NOERROR) {
447 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
448 pDetailsA->dwParam1 = pDetailsW->dwParam1;
449 pDetailsA->dwParam2 = pDetailsW->dwParam2;
450 WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
451 pDetailsA->szName,
452 sizeof(pDetailsA->szName), NULL, NULL );
453 pDetailsA++;
454 pDetailsW++;
456 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
457 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
459 HeapFree(GetProcessHeap(), 0, pDetailsW);
460 lpmcdA->paDetails = pDetailsA;
461 lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
463 break;
464 default:
465 ERR("Unsupported fdwDetails=0x%08x\n", fdwDetails);
468 return ret;
471 /**************************************************************************
472 * mixerGetLineControlsA [WINMM.@]
474 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
475 DWORD fdwControls)
477 MIXERLINECONTROLSW mlcW;
478 DWORD ret;
479 unsigned int i;
481 TRACE("(%p, %p, %08x)\n", hmix, lpmlcA, fdwControls);
483 if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
484 lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
485 return MMSYSERR_INVALPARAM;
487 mlcW.cbStruct = sizeof(mlcW);
488 mlcW.dwLineID = lpmlcA->dwLineID;
489 mlcW.u.dwControlID = lpmlcA->u.dwControlID;
490 mlcW.u.dwControlType = lpmlcA->u.dwControlType;
492 /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
493 the control count is assumed to be 1 - This is relied upon by a game,
494 "Dynomite Deluze" */
495 if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
496 mlcW.cControls = 1;
497 } else {
498 mlcW.cControls = lpmlcA->cControls;
500 mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
501 mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
502 mlcW.cControls * mlcW.cbmxctrl);
504 ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
506 if (ret == MMSYSERR_NOERROR) {
507 lpmlcA->dwLineID = mlcW.dwLineID;
508 lpmlcA->u.dwControlID = mlcW.u.dwControlID;
509 lpmlcA->u.dwControlType = mlcW.u.dwControlType;
511 for (i = 0; i < mlcW.cControls; i++) {
512 lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
513 lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
514 lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
515 lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
516 lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
517 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
518 lpmlcA->pamxctrl[i].szShortName,
519 sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
520 WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
521 lpmlcA->pamxctrl[i].szName,
522 sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
523 /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
524 * sizeof(mlcW.pamxctrl[i].Bounds) */
525 memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
526 sizeof(mlcW.pamxctrl[i].Bounds));
527 /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
528 * sizeof(mlcW.pamxctrl[i].Metrics) */
529 memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
530 sizeof(mlcW.pamxctrl[i].Metrics));
534 HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
536 return ret;
539 /**************************************************************************
540 * mixerGetLineControlsW [WINMM.@]
542 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
543 DWORD fdwControls)
545 LPWINE_MIXER lpwm;
546 UINT uRet = MMSYSERR_NOERROR;
548 TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
550 if ((uRet = MIXER_GetDev(hmix, fdwControls, &lpwm)) != MMSYSERR_NOERROR)
551 return uRet;
553 if (lpmlcW == NULL || lpmlcW->cbStruct != sizeof(*lpmlcW))
554 return MMSYSERR_INVALPARAM;
556 return MMDRV_Message(&lpwm->mld, MXDM_GETLINECONTROLS, (DWORD_PTR)lpmlcW,
557 fdwControls);
560 /**************************************************************************
561 * mixerGetLineInfoW [WINMM.@]
563 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
565 LPWINE_MIXER lpwm;
566 UINT uRet = MMSYSERR_NOERROR;
568 TRACE("(%p, %p, %08x)\n", hmix, lpmliW, fdwInfo);
570 if ((uRet = MIXER_GetDev(hmix, fdwInfo, &lpwm)) != MMSYSERR_NOERROR)
571 return uRet;
573 return MMDRV_Message(&lpwm->mld, MXDM_GETLINEINFO, (DWORD_PTR)lpmliW,
574 fdwInfo);
577 /**************************************************************************
578 * mixerGetLineInfoA [WINMM.@]
580 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
581 DWORD fdwInfo)
583 MIXERLINEW mliW;
584 UINT ret;
586 TRACE("(%p, %p, %08x)\n", hmix, lpmliA, fdwInfo);
588 if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
589 return MMSYSERR_INVALPARAM;
591 mliW.cbStruct = sizeof(mliW);
592 switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
593 case MIXER_GETLINEINFOF_COMPONENTTYPE:
594 mliW.dwComponentType = lpmliA->dwComponentType;
595 break;
596 case MIXER_GETLINEINFOF_DESTINATION:
597 mliW.dwDestination = lpmliA->dwDestination;
598 break;
599 case MIXER_GETLINEINFOF_LINEID:
600 mliW.dwLineID = lpmliA->dwLineID;
601 break;
602 case MIXER_GETLINEINFOF_SOURCE:
603 mliW.dwDestination = lpmliA->dwDestination;
604 mliW.dwSource = lpmliA->dwSource;
605 break;
606 case MIXER_GETLINEINFOF_TARGETTYPE:
607 mliW.Target.dwType = lpmliA->Target.dwType;
608 mliW.Target.wMid = lpmliA->Target.wMid;
609 mliW.Target.wPid = lpmliA->Target.wPid;
610 mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
611 MultiByteToWideChar( CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, sizeof(mliW.Target.szPname)/sizeof(WCHAR));
612 break;
613 default:
614 WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
615 return MMSYSERR_INVALFLAG;
618 ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
620 if(ret == MMSYSERR_NOERROR)
622 lpmliA->dwDestination = mliW.dwDestination;
623 lpmliA->dwSource = mliW.dwSource;
624 lpmliA->dwLineID = mliW.dwLineID;
625 lpmliA->fdwLine = mliW.fdwLine;
626 lpmliA->dwUser = mliW.dwUser;
627 lpmliA->dwComponentType = mliW.dwComponentType;
628 lpmliA->cChannels = mliW.cChannels;
629 lpmliA->cConnections = mliW.cConnections;
630 lpmliA->cControls = mliW.cControls;
631 WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
632 sizeof(lpmliA->szShortName), NULL, NULL);
633 WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
634 sizeof(lpmliA->szName), NULL, NULL );
635 lpmliA->Target.dwType = mliW.Target.dwType;
636 lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
637 lpmliA->Target.wMid = mliW.Target.wMid;
638 lpmliA->Target.wPid = mliW.Target.wPid;
639 lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
640 WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
641 sizeof(lpmliA->Target.szPname), NULL, NULL );
643 return ret;
646 /**************************************************************************
647 * mixerSetControlDetails [WINMM.@]
649 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
650 DWORD fdwDetails)
652 LPWINE_MIXER lpwm;
653 UINT uRet = MMSYSERR_NOERROR;
655 TRACE("(%p, %p, %08x)\n", hmix, lpmcd, fdwDetails);
657 if ((uRet = MIXER_GetDev(hmix, fdwDetails, &lpwm)) != MMSYSERR_NOERROR)
658 return uRet;
660 return MMDRV_Message(&lpwm->mld, MXDM_SETCONTROLDETAILS, (DWORD_PTR)lpmcd,
661 fdwDetails);
664 /**************************************************************************
665 * mixerMessage [WINMM.@]
667 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
669 LPWINE_MLD wmld;
671 TRACE("(%p, %d, %08lx, %08lx): semi-stub?\n",
672 hmix, uMsg, dwParam1, dwParam2);
674 if ((wmld = MMDRV_Get(hmix, MMDRV_MIXER, FALSE)) == NULL)
675 return MMSYSERR_INVALHANDLE;
677 return MMDRV_Message(wmld, uMsg, dwParam1, dwParam2);
680 /**************************************************************************
681 * auxGetNumDevs [WINMM.@]
683 UINT WINAPI auxGetNumDevs(void)
685 return MMDRV_GetNum(MMDRV_AUX);
688 /**************************************************************************
689 * auxGetDevCapsW [WINMM.@]
691 UINT WINAPI auxGetDevCapsW(UINT_PTR uDeviceID, LPAUXCAPSW lpCaps, UINT uSize)
693 LPWINE_MLD wmld;
695 TRACE("(%04lX, %p, %d) !\n", uDeviceID, lpCaps, uSize);
697 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
699 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
700 return MMSYSERR_BADDEVICEID;
701 return MMDRV_Message(wmld, AUXDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
704 /**************************************************************************
705 * auxGetDevCapsA [WINMM.@]
707 UINT WINAPI auxGetDevCapsA(UINT_PTR uDeviceID, LPAUXCAPSA lpCaps, UINT uSize)
709 AUXCAPSW acW;
710 UINT ret;
712 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
714 ret = auxGetDevCapsW(uDeviceID, &acW, sizeof(acW));
716 if (ret == MMSYSERR_NOERROR) {
717 AUXCAPSA acA;
718 acA.wMid = acW.wMid;
719 acA.wPid = acW.wPid;
720 acA.vDriverVersion = acW.vDriverVersion;
721 WideCharToMultiByte( CP_ACP, 0, acW.szPname, -1, acA.szPname,
722 sizeof(acA.szPname), NULL, NULL );
723 acA.wTechnology = acW.wTechnology;
724 acA.dwSupport = acW.dwSupport;
725 memcpy(lpCaps, &acA, min(uSize, sizeof(acA)));
727 return ret;
730 /**************************************************************************
731 * auxGetVolume [WINMM.@]
733 UINT WINAPI auxGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
735 LPWINE_MLD wmld;
737 TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);
739 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
740 return MMSYSERR_INVALHANDLE;
741 return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
744 /**************************************************************************
745 * auxSetVolume [WINMM.@]
747 UINT WINAPI auxSetVolume(UINT uDeviceID, DWORD dwVolume)
749 LPWINE_MLD wmld;
751 TRACE("(%04X, %u) !\n", uDeviceID, dwVolume);
753 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
754 return MMSYSERR_INVALHANDLE;
755 return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L);
758 /**************************************************************************
759 * auxOutMessage [WINMM.@]
761 UINT WINAPI auxOutMessage(UINT uDeviceID, UINT uMessage, DWORD_PTR dw1, DWORD_PTR dw2)
763 LPWINE_MLD wmld;
765 if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
766 return MMSYSERR_INVALHANDLE;
768 return MMDRV_Message(wmld, uMessage, dw1, dw2);
771 /**************************************************************************
772 * midiOutGetNumDevs [WINMM.@]
774 UINT WINAPI midiOutGetNumDevs(void)
776 return MMDRV_GetNum(MMDRV_MIDIOUT);
779 /**************************************************************************
780 * midiOutGetDevCapsW [WINMM.@]
782 UINT WINAPI midiOutGetDevCapsW(UINT_PTR uDeviceID, LPMIDIOUTCAPSW lpCaps,
783 UINT uSize)
785 LPWINE_MLD wmld;
787 TRACE("(%lu, %p, %u);\n", uDeviceID, lpCaps, uSize);
789 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
791 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL)
792 return MMSYSERR_BADDEVICEID;
794 return MMDRV_Message(wmld, MODM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
797 /**************************************************************************
798 * midiOutGetDevCapsA [WINMM.@]
800 UINT WINAPI midiOutGetDevCapsA(UINT_PTR uDeviceID, LPMIDIOUTCAPSA lpCaps,
801 UINT uSize)
803 MIDIOUTCAPSW mocW;
804 UINT ret;
806 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
808 ret = midiOutGetDevCapsW(uDeviceID, &mocW, sizeof(mocW));
810 if (ret == MMSYSERR_NOERROR) {
811 MIDIOUTCAPSA mocA;
812 mocA.wMid = mocW.wMid;
813 mocA.wPid = mocW.wPid;
814 mocA.vDriverVersion = mocW.vDriverVersion;
815 WideCharToMultiByte( CP_ACP, 0, mocW.szPname, -1, mocA.szPname,
816 sizeof(mocA.szPname), NULL, NULL );
817 mocA.wTechnology = mocW.wTechnology;
818 mocA.wVoices = mocW.wVoices;
819 mocA.wNotes = mocW.wNotes;
820 mocA.wChannelMask = mocW.wChannelMask;
821 mocA.dwSupport = mocW.dwSupport;
822 memcpy(lpCaps, &mocA, min(uSize, sizeof(mocA)));
824 return ret;
827 /**************************************************************************
828 * midiOutGetErrorTextA [WINMM.@]
829 * midiInGetErrorTextA [WINMM.@]
831 UINT WINAPI midiOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
833 UINT ret;
835 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
836 else if (uSize == 0) ret = MMSYSERR_NOERROR;
837 else
839 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
840 if (!xstr) ret = MMSYSERR_NOMEM;
841 else
843 ret = midiOutGetErrorTextW(uError, xstr, uSize);
844 if (ret == MMSYSERR_NOERROR)
845 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
846 HeapFree(GetProcessHeap(), 0, xstr);
849 return ret;
852 /**************************************************************************
853 * midiOutGetErrorTextW [WINMM.@]
854 * midiInGetErrorTextW [WINMM.@]
856 UINT WINAPI midiOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
858 UINT ret = MMSYSERR_BADERRNUM;
860 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
861 else if (uSize == 0) ret = MMSYSERR_NOERROR;
862 else if (
863 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
864 * a warning for the test was always true */
865 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
866 (uError >= MIDIERR_BASE && uError <= MIDIERR_LASTERROR)) {
867 if (LoadStringW(hWinMM32Instance, uError, lpText, uSize) > 0) {
868 ret = MMSYSERR_NOERROR;
871 return ret;
874 /**************************************************************************
875 * MIDI_OutAlloc [internal]
877 static LPWINE_MIDI MIDI_OutAlloc(HMIDIOUT* lphMidiOut, DWORD_PTR* lpdwCallback,
878 DWORD_PTR* lpdwInstance, LPDWORD lpdwFlags,
879 DWORD cIDs, MIDIOPENSTRMID* lpIDs)
881 HANDLE hMidiOut;
882 LPWINE_MIDI lpwm;
883 UINT size;
885 size = sizeof(WINE_MIDI) + (cIDs ? (cIDs-1) : 0) * sizeof(MIDIOPENSTRMID);
887 lpwm = (LPWINE_MIDI)MMDRV_Alloc(size, MMDRV_MIDIOUT, &hMidiOut, lpdwFlags,
888 lpdwCallback, lpdwInstance);
889 *lphMidiOut = hMidiOut;
891 if (lpwm) {
892 lpwm->mod.hMidi = hMidiOut;
893 lpwm->mod.dwCallback = *lpdwCallback;
894 lpwm->mod.dwInstance = *lpdwInstance;
895 lpwm->mod.dnDevNode = 0;
896 lpwm->mod.cIds = cIDs;
897 if (cIDs)
898 memcpy(&(lpwm->mod.rgIds), lpIDs, cIDs * sizeof(MIDIOPENSTRMID));
900 return lpwm;
903 /**************************************************************************
904 * midiOutOpen [WINMM.@]
906 UINT WINAPI midiOutOpen(LPHMIDIOUT lphMidiOut, UINT uDeviceID,
907 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
909 HMIDIOUT hMidiOut;
910 LPWINE_MIDI lpwm;
911 UINT dwRet = 0;
913 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
914 lphMidiOut, uDeviceID, dwCallback, dwInstance, dwFlags);
916 if (lphMidiOut != NULL) *lphMidiOut = 0;
918 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &dwFlags, 0, NULL);
920 if (lpwm == NULL)
921 return MMSYSERR_NOMEM;
923 lpwm->mld.uDeviceID = uDeviceID;
925 dwRet = MMDRV_Open((LPWINE_MLD)lpwm, MODM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
927 if (dwRet != MMSYSERR_NOERROR) {
928 MMDRV_Free(hMidiOut, (LPWINE_MLD)lpwm);
929 hMidiOut = 0;
932 if (lphMidiOut) *lphMidiOut = hMidiOut;
933 TRACE("=> %d hMidi=%p\n", dwRet, hMidiOut);
935 return dwRet;
938 /**************************************************************************
939 * midiOutClose [WINMM.@]
941 UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
943 LPWINE_MLD wmld;
944 DWORD dwRet;
946 TRACE("(%p)\n", hMidiOut);
948 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
949 return MMSYSERR_INVALHANDLE;
951 dwRet = MMDRV_Close(wmld, MODM_CLOSE);
952 MMDRV_Free(hMidiOut, wmld);
954 return dwRet;
957 /**************************************************************************
958 * midiOutPrepareHeader [WINMM.@]
960 UINT WINAPI midiOutPrepareHeader(HMIDIOUT hMidiOut,
961 MIDIHDR* lpMidiOutHdr, UINT uSize)
963 LPWINE_MLD wmld;
965 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
967 if (lpMidiOutHdr == NULL || uSize < sizeof (MIDIHDR))
968 return MMSYSERR_INVALPARAM;
970 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
971 return MMSYSERR_INVALHANDLE;
972 /* FIXME: detect MIDIStream handles and enforce 64KB buffer limit on those */
974 return MMDRV_Message(wmld, MODM_PREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
977 /**************************************************************************
978 * midiOutUnprepareHeader [WINMM.@]
980 UINT WINAPI midiOutUnprepareHeader(HMIDIOUT hMidiOut,
981 MIDIHDR* lpMidiOutHdr, UINT uSize)
983 LPWINE_MLD wmld;
985 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
987 if (lpMidiOutHdr == NULL || uSize < sizeof (MIDIHDR))
988 return MMSYSERR_INVALPARAM;
990 if (!(lpMidiOutHdr->dwFlags & MHDR_PREPARED)) {
991 return MMSYSERR_NOERROR;
994 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
995 return MMSYSERR_INVALHANDLE;
997 return MMDRV_Message(wmld, MODM_UNPREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
1000 /**************************************************************************
1001 * midiOutShortMsg [WINMM.@]
1003 UINT WINAPI midiOutShortMsg(HMIDIOUT hMidiOut, DWORD dwMsg)
1005 LPWINE_MLD wmld;
1007 TRACE("(%p, %08X)\n", hMidiOut, dwMsg);
1009 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
1010 return MMSYSERR_INVALHANDLE;
1012 return MMDRV_Message(wmld, MODM_DATA, dwMsg, 0L);
1015 /**************************************************************************
1016 * midiOutLongMsg [WINMM.@]
1018 UINT WINAPI midiOutLongMsg(HMIDIOUT hMidiOut,
1019 MIDIHDR* lpMidiOutHdr, UINT uSize)
1021 LPWINE_MLD wmld;
1023 TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
1025 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
1026 return MMSYSERR_INVALHANDLE;
1028 return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD_PTR)lpMidiOutHdr, uSize);
1031 /**************************************************************************
1032 * midiOutReset [WINMM.@]
1034 UINT WINAPI midiOutReset(HMIDIOUT hMidiOut)
1036 LPWINE_MLD wmld;
1038 TRACE("(%p)\n", hMidiOut);
1040 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
1041 return MMSYSERR_INVALHANDLE;
1043 return MMDRV_Message(wmld, MODM_RESET, 0L, 0L);
1046 /**************************************************************************
1047 * midiOutGetVolume [WINMM.@]
1049 UINT WINAPI midiOutGetVolume(HMIDIOUT hMidiOut, DWORD* lpdwVolume)
1051 LPWINE_MLD wmld;
1053 TRACE("(%p, %p);\n", hMidiOut, lpdwVolume);
1055 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
1056 return MMSYSERR_INVALHANDLE;
1058 return MMDRV_Message(wmld, MODM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
1061 /**************************************************************************
1062 * midiOutSetVolume [WINMM.@]
1064 UINT WINAPI midiOutSetVolume(HMIDIOUT hMidiOut, DWORD dwVolume)
1066 LPWINE_MLD wmld;
1068 TRACE("(%p, %d);\n", hMidiOut, dwVolume);
1070 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
1071 return MMSYSERR_INVALHANDLE;
1073 return MMDRV_Message(wmld, MODM_SETVOLUME, dwVolume, 0L);
1076 /**************************************************************************
1077 * midiOutCachePatches [WINMM.@]
1079 UINT WINAPI midiOutCachePatches(HMIDIOUT hMidiOut, UINT uBank,
1080 WORD* lpwPatchArray, UINT uFlags)
1082 /* not really necessary to support this */
1083 FIXME("not supported yet\n");
1084 return MMSYSERR_NOTSUPPORTED;
1087 /**************************************************************************
1088 * midiOutCacheDrumPatches [WINMM.@]
1090 UINT WINAPI midiOutCacheDrumPatches(HMIDIOUT hMidiOut, UINT uPatch,
1091 WORD* lpwKeyArray, UINT uFlags)
1093 FIXME("not supported yet\n");
1094 return MMSYSERR_NOTSUPPORTED;
1097 /**************************************************************************
1098 * midiOutGetID [WINMM.@]
1100 UINT WINAPI midiOutGetID(HMIDIOUT hMidiOut, UINT* lpuDeviceID)
1102 LPWINE_MLD wmld;
1104 TRACE("(%p, %p)\n", hMidiOut, lpuDeviceID);
1106 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
1107 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
1108 return MMSYSERR_INVALHANDLE;
1110 *lpuDeviceID = wmld->uDeviceID;
1111 return MMSYSERR_NOERROR;
1114 /**************************************************************************
1115 * midiOutMessage [WINMM.@]
1117 UINT WINAPI midiOutMessage(HMIDIOUT hMidiOut, UINT uMessage,
1118 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1120 LPWINE_MLD wmld;
1122 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
1124 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) {
1125 /* HACK... */
1126 if (uMessage == 0x0001) {
1127 *(LPDWORD)dwParam1 = 1;
1128 return 0;
1130 if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) != NULL) {
1131 return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
1133 return MMSYSERR_INVALHANDLE;
1136 switch (uMessage) {
1137 case MODM_OPEN:
1138 case MODM_CLOSE:
1139 FIXME("can't handle OPEN or CLOSE message!\n");
1140 return MMSYSERR_NOTSUPPORTED;
1142 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
1145 /**************************************************************************
1146 * midiInGetNumDevs [WINMM.@]
1148 UINT WINAPI midiInGetNumDevs(void)
1150 return MMDRV_GetNum(MMDRV_MIDIIN);
1153 /**************************************************************************
1154 * midiInGetDevCapsW [WINMM.@]
1156 UINT WINAPI midiInGetDevCapsW(UINT_PTR uDeviceID, LPMIDIINCAPSW lpCaps, UINT uSize)
1158 LPWINE_MLD wmld;
1160 TRACE("(%ld, %p, %d);\n", uDeviceID, lpCaps, uSize);
1162 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
1164 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIIN, TRUE)) == NULL)
1165 return MMSYSERR_BADDEVICEID;
1167 return MMDRV_Message(wmld, MIDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
1170 /**************************************************************************
1171 * midiInGetDevCapsA [WINMM.@]
1173 UINT WINAPI midiInGetDevCapsA(UINT_PTR uDeviceID, LPMIDIINCAPSA lpCaps, UINT uSize)
1175 MIDIINCAPSW micW;
1176 UINT ret;
1178 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
1180 ret = midiInGetDevCapsW(uDeviceID, &micW, sizeof(micW));
1182 if (ret == MMSYSERR_NOERROR) {
1183 MIDIINCAPSA micA;
1184 micA.wMid = micW.wMid;
1185 micA.wPid = micW.wPid;
1186 micA.vDriverVersion = micW.vDriverVersion;
1187 WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
1188 sizeof(micA.szPname), NULL, NULL );
1189 micA.dwSupport = micW.dwSupport;
1190 memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
1192 return ret;
1195 /**************************************************************************
1196 * midiInOpen [WINMM.@]
1198 UINT WINAPI midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID,
1199 DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
1201 HANDLE hMidiIn;
1202 LPWINE_MIDI lpwm;
1203 DWORD dwRet = 0;
1205 TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
1206 lphMidiIn, uDeviceID, dwCallback, dwInstance, dwFlags);
1208 if (lphMidiIn != NULL) *lphMidiIn = 0;
1210 lpwm = (LPWINE_MIDI)MMDRV_Alloc(sizeof(WINE_MIDI), MMDRV_MIDIIN, &hMidiIn,
1211 &dwFlags, &dwCallback, &dwInstance);
1213 if (lpwm == NULL)
1214 return MMSYSERR_NOMEM;
1216 lpwm->mod.hMidi = hMidiIn;
1217 lpwm->mod.dwCallback = dwCallback;
1218 lpwm->mod.dwInstance = dwInstance;
1220 lpwm->mld.uDeviceID = uDeviceID;
1221 dwRet = MMDRV_Open(&lpwm->mld, MIDM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
1223 if (dwRet != MMSYSERR_NOERROR) {
1224 MMDRV_Free(hMidiIn, &lpwm->mld);
1225 hMidiIn = 0;
1227 if (lphMidiIn != NULL) *lphMidiIn = hMidiIn;
1228 TRACE("=> %d hMidi=%p\n", dwRet, hMidiIn);
1230 return dwRet;
1233 /**************************************************************************
1234 * midiInClose [WINMM.@]
1236 UINT WINAPI midiInClose(HMIDIIN hMidiIn)
1238 LPWINE_MLD wmld;
1239 DWORD dwRet;
1241 TRACE("(%p)\n", hMidiIn);
1243 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1244 return MMSYSERR_INVALHANDLE;
1246 dwRet = MMDRV_Close(wmld, MIDM_CLOSE);
1247 MMDRV_Free(hMidiIn, wmld);
1248 return dwRet;
1251 /**************************************************************************
1252 * midiInPrepareHeader [WINMM.@]
1254 UINT WINAPI midiInPrepareHeader(HMIDIIN hMidiIn,
1255 MIDIHDR* lpMidiInHdr, UINT uSize)
1257 LPWINE_MLD wmld;
1259 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
1261 if (lpMidiInHdr == NULL || uSize < sizeof (MIDIHDR))
1262 return MMSYSERR_INVALPARAM;
1264 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1265 return MMSYSERR_INVALHANDLE;
1267 return MMDRV_Message(wmld, MIDM_PREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
1270 /**************************************************************************
1271 * midiInUnprepareHeader [WINMM.@]
1273 UINT WINAPI midiInUnprepareHeader(HMIDIIN hMidiIn,
1274 MIDIHDR* lpMidiInHdr, UINT uSize)
1276 LPWINE_MLD wmld;
1278 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
1280 if (lpMidiInHdr == NULL || uSize < sizeof (MIDIHDR))
1281 return MMSYSERR_INVALPARAM;
1283 if (!(lpMidiInHdr->dwFlags & MHDR_PREPARED)) {
1284 return MMSYSERR_NOERROR;
1287 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1288 return MMSYSERR_INVALHANDLE;
1290 return MMDRV_Message(wmld, MIDM_UNPREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
1293 /**************************************************************************
1294 * midiInAddBuffer [WINMM.@]
1296 UINT WINAPI midiInAddBuffer(HMIDIIN hMidiIn,
1297 MIDIHDR* lpMidiInHdr, UINT uSize)
1299 LPWINE_MLD wmld;
1301 TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
1303 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1304 return MMSYSERR_INVALHANDLE;
1306 return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD_PTR)lpMidiInHdr, uSize);
1309 /**************************************************************************
1310 * midiInStart [WINMM.@]
1312 UINT WINAPI midiInStart(HMIDIIN hMidiIn)
1314 LPWINE_MLD wmld;
1316 TRACE("(%p)\n", hMidiIn);
1318 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1319 return MMSYSERR_INVALHANDLE;
1321 return MMDRV_Message(wmld, MIDM_START, 0L, 0L);
1324 /**************************************************************************
1325 * midiInStop [WINMM.@]
1327 UINT WINAPI midiInStop(HMIDIIN hMidiIn)
1329 LPWINE_MLD wmld;
1331 TRACE("(%p)\n", hMidiIn);
1333 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1334 return MMSYSERR_INVALHANDLE;
1336 return MMDRV_Message(wmld, MIDM_STOP, 0L, 0L);
1339 /**************************************************************************
1340 * midiInReset [WINMM.@]
1342 UINT WINAPI midiInReset(HMIDIIN hMidiIn)
1344 LPWINE_MLD wmld;
1346 TRACE("(%p)\n", hMidiIn);
1348 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1349 return MMSYSERR_INVALHANDLE;
1351 return MMDRV_Message(wmld, MIDM_RESET, 0L, 0L);
1354 /**************************************************************************
1355 * midiInGetID [WINMM.@]
1357 UINT WINAPI midiInGetID(HMIDIIN hMidiIn, UINT* lpuDeviceID)
1359 LPWINE_MLD wmld;
1361 TRACE("(%p, %p)\n", hMidiIn, lpuDeviceID);
1363 if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
1365 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, TRUE)) == NULL)
1366 return MMSYSERR_INVALHANDLE;
1368 *lpuDeviceID = wmld->uDeviceID;
1370 return MMSYSERR_NOERROR;
1373 /**************************************************************************
1374 * midiInMessage [WINMM.@]
1376 UINT WINAPI midiInMessage(HMIDIIN hMidiIn, UINT uMessage,
1377 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1379 LPWINE_MLD wmld;
1381 TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiIn, uMessage, dwParam1, dwParam2);
1383 if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1384 return MMSYSERR_INVALHANDLE;
1386 switch (uMessage) {
1387 case MIDM_OPEN:
1388 case MIDM_CLOSE:
1389 FIXME("can't handle OPEN or CLOSE message!\n");
1390 return MMSYSERR_NOTSUPPORTED;
1392 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
1395 /**************************************************************************
1396 * midiConnect [WINMM.@]
1398 MMRESULT WINAPI midiConnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
1400 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
1401 return MMSYSERR_ERROR;
1404 /**************************************************************************
1405 * midiDisconnect [WINMM.@]
1407 MMRESULT WINAPI midiDisconnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
1409 FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
1410 return MMSYSERR_ERROR;
1413 typedef struct WINE_MIDIStream {
1414 HMIDIOUT hDevice;
1415 HANDLE hThread;
1416 DWORD dwThreadID;
1417 DWORD dwTempo;
1418 DWORD dwTimeDiv;
1419 DWORD dwPositionMS;
1420 DWORD dwPulses;
1421 DWORD dwStartTicks;
1422 WORD wFlags;
1423 HANDLE hEvent;
1424 LPMIDIHDR lpMidiHdr;
1425 } WINE_MIDIStream;
1427 #define WINE_MSM_HEADER (WM_USER+0)
1428 #define WINE_MSM_STOP (WM_USER+1)
1430 /**************************************************************************
1431 * MMSYSTEM_GetMidiStream [internal]
1433 static BOOL MMSYSTEM_GetMidiStream(HMIDISTRM hMidiStrm, WINE_MIDIStream** lpMidiStrm, WINE_MIDI** lplpwm)
1435 WINE_MIDI* lpwm = (LPWINE_MIDI)MMDRV_Get(hMidiStrm, MMDRV_MIDIOUT, FALSE);
1437 if (lplpwm)
1438 *lplpwm = lpwm;
1440 if (lpwm == NULL) {
1441 return FALSE;
1444 *lpMidiStrm = (WINE_MIDIStream*)lpwm->mod.rgIds.dwStreamID;
1446 return *lpMidiStrm != NULL;
1449 /**************************************************************************
1450 * MMSYSTEM_MidiStream_Convert [internal]
1452 static DWORD MMSYSTEM_MidiStream_Convert(WINE_MIDIStream* lpMidiStrm, DWORD pulse)
1454 DWORD ret = 0;
1456 if (lpMidiStrm->dwTimeDiv == 0) {
1457 FIXME("Shouldn't happen. lpMidiStrm->dwTimeDiv = 0\n");
1458 } else if (lpMidiStrm->dwTimeDiv > 0x8000) { /* SMPTE, unchecked FIXME? */
1459 int nf = -(char)HIBYTE(lpMidiStrm->dwTimeDiv); /* number of frames */
1460 int nsf = LOBYTE(lpMidiStrm->dwTimeDiv); /* number of sub-frames */
1461 ret = (pulse * 1000) / (nf * nsf);
1462 } else {
1463 ret = (DWORD)((double)pulse * ((double)lpMidiStrm->dwTempo / 1000) /
1464 (double)lpMidiStrm->dwTimeDiv);
1467 return ret;
1470 /**************************************************************************
1471 * MMSYSTEM_MidiStream_MessageHandler [internal]
1473 static BOOL MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream* lpMidiStrm, LPWINE_MIDI lpwm, LPMSG msg)
1475 LPMIDIHDR lpMidiHdr;
1476 LPMIDIHDR* lpmh;
1477 LPBYTE lpData;
1479 switch (msg->message) {
1480 case WM_QUIT:
1481 SetEvent(lpMidiStrm->hEvent);
1482 return FALSE;
1483 case WINE_MSM_STOP:
1484 TRACE("STOP\n");
1485 /* this is not quite what MS doc says... */
1486 midiOutReset(lpMidiStrm->hDevice);
1487 /* empty list of already submitted buffers */
1488 for (lpMidiHdr = lpMidiStrm->lpMidiHdr; lpMidiHdr; lpMidiHdr = lpMidiHdr->lpNext) {
1489 lpMidiHdr->dwFlags |= MHDR_DONE;
1490 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1492 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1493 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1494 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1496 lpMidiStrm->lpMidiHdr = 0;
1497 SetEvent(lpMidiStrm->hEvent);
1498 break;
1499 case WINE_MSM_HEADER:
1500 /* sets initial tick count for first MIDIHDR */
1501 if (!lpMidiStrm->dwStartTicks)
1502 lpMidiStrm->dwStartTicks = GetTickCount();
1504 /* FIXME(EPP): "I don't understand the content of the first MIDIHDR sent
1505 * by native mcimidi, it doesn't look like a correct one".
1506 * this trick allows to throw it away... but I don't like it.
1507 * It looks like part of the file I'm trying to play and definitively looks
1508 * like raw midi content
1509 * I'd really like to understand why native mcimidi sends it. Perhaps a bad
1510 * synchronization issue where native mcimidi is still processing raw MIDI
1511 * content before generating MIDIEVENTs ?
1513 * 4c 04 89 3b 00 81 7c 99 3b 43 00 99 23 5e 04 89 L..;..|.;C..#^..
1514 * 3b 00 00 89 23 00 7c 99 3b 45 00 99 28 62 04 89 ;...#.|.;E..(b..
1515 * 3b 00 00 89 28 00 81 7c 99 3b 4e 00 99 23 5e 04 ;...(..|.;N..#^.
1516 * 89 3b 00 00 89 23 00 7c 99 3b 45 00 99 23 78 04 .;...#.|.;E..#x.
1517 * 89 3b 00 00 89 23 00 81 7c 99 3b 48 00 99 23 5e .;...#..|.;H..#^
1518 * 04 89 3b 00 00 89 23 00 7c 99 3b 4e 00 99 28 62 ..;...#.|.;N..(b
1519 * 04 89 3b 00 00 89 28 00 81 7c 99 39 4c 00 99 23 ..;...(..|.9L..#
1520 * 5e 04 89 39 00 00 89 23 00 82 7c 99 3b 4c 00 99 ^..9...#..|.;L..
1521 * 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 00 99 #^..;...#.|.;H..
1522 * 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b 3f 04 (b..;...(..|.;?.
1523 * 89 3b 00 1c 99 23 5e 04 89 23 00 5c 99 3b 45 00 .;...#^..#.\.;E.
1524 * 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 3b 46 .#x..;...#..|.;F
1525 * 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 ..#^..;...#.|.;H
1526 * 00 99 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b ..(b..;...(..|.;
1527 * 46 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b F..#^..;...#.|.;
1528 * 48 00 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 H..#x..;...#..|.
1529 * 3b 4c 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 ;L..#^..;...#.|.
1531 lpMidiHdr = (LPMIDIHDR)msg->lParam;
1532 lpData = (LPBYTE)lpMidiHdr->lpData;
1533 TRACE("Adding %s lpMidiHdr=%p [lpData=0x%p dwBytesRecorded=%u/%u dwFlags=0x%08x size=%lu]\n",
1534 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular", lpMidiHdr,
1535 lpMidiHdr, lpMidiHdr->dwBytesRecorded, lpMidiHdr->dwBufferLength,
1536 lpMidiHdr->dwFlags, msg->wParam);
1537 #if 0
1538 /* dumps content of lpMidiHdr->lpData
1539 * FIXME: there should be a debug routine somewhere that already does this
1540 * I hate spreading this type of shit all around the code
1542 for (dwToGo = 0; dwToGo < lpMidiHdr->dwBufferLength; dwToGo += 16) {
1543 DWORD i;
1544 BYTE ch;
1546 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++)
1547 printf("%02x ", lpData[dwToGo + i]);
1548 for (; i < 16; i++)
1549 printf(" ");
1550 for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++) {
1551 ch = lpData[dwToGo + i];
1552 printf("%c", (ch >= 0x20 && ch <= 0x7F) ? ch : '.');
1554 printf("\n");
1556 #endif
1557 if (((LPMIDIEVENT)lpData)->dwStreamID != 0 &&
1558 ((LPMIDIEVENT)lpData)->dwStreamID != 0xFFFFFFFF &&
1559 ((LPMIDIEVENT)lpData)->dwStreamID != (DWORD)lpMidiStrm) {
1560 FIXME("Dropping bad %s lpMidiHdr (streamID=%08x)\n",
1561 (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular",
1562 ((LPMIDIEVENT)lpData)->dwStreamID);
1563 lpMidiHdr->dwFlags |= MHDR_DONE;
1564 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1566 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1567 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1568 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1569 break;
1572 for (lpmh = &lpMidiStrm->lpMidiHdr; *lpmh; lpmh = &(*lpmh)->lpNext);
1573 *lpmh = lpMidiHdr;
1574 lpMidiHdr = (LPMIDIHDR)msg->lParam;
1575 lpMidiHdr->lpNext = 0;
1576 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1577 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1578 lpMidiHdr->dwOffset = 0;
1580 break;
1581 default:
1582 FIXME("Unknown message %d\n", msg->message);
1583 break;
1585 return TRUE;
1588 /**************************************************************************
1589 * MMSYSTEM_MidiStream_Player [internal]
1591 static DWORD CALLBACK MMSYSTEM_MidiStream_Player(LPVOID pmt)
1593 WINE_MIDIStream* lpMidiStrm = pmt;
1594 WINE_MIDI* lpwm;
1595 MSG msg;
1596 DWORD dwToGo;
1597 DWORD dwCurrTC;
1598 LPMIDIHDR lpMidiHdr;
1599 LPMIDIEVENT me;
1600 LPBYTE lpData = 0;
1602 TRACE("(%p)!\n", lpMidiStrm);
1604 if (!lpMidiStrm ||
1605 (lpwm = (LPWINE_MIDI)MMDRV_Get(lpMidiStrm->hDevice, MMDRV_MIDIOUT, FALSE)) == NULL)
1606 goto the_end;
1608 /* force thread's queue creation */
1609 PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
1611 lpMidiStrm->dwStartTicks = 0;
1612 lpMidiStrm->dwPulses = 0;
1614 lpMidiStrm->lpMidiHdr = 0;
1616 /* midiStreamOpen is waiting for ack */
1617 SetEvent(lpMidiStrm->hEvent);
1619 for (;;) {
1620 lpMidiHdr = lpMidiStrm->lpMidiHdr;
1621 if (!lpMidiHdr) {
1622 /* for first message, block until one arrives, then process all that are available */
1623 GetMessageA(&msg, 0, 0, 0);
1624 do {
1625 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1626 goto the_end;
1627 } while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE));
1628 lpData = 0;
1629 continue;
1632 if (!lpData)
1633 lpData = (LPBYTE)lpMidiHdr->lpData;
1635 me = (LPMIDIEVENT)(lpData + lpMidiHdr->dwOffset);
1637 /* do we have to wait ? */
1638 if (me->dwDeltaTime) {
1639 lpMidiStrm->dwPositionMS += MMSYSTEM_MidiStream_Convert(lpMidiStrm, me->dwDeltaTime);
1640 lpMidiStrm->dwPulses += me->dwDeltaTime;
1642 dwToGo = lpMidiStrm->dwStartTicks + lpMidiStrm->dwPositionMS;
1644 TRACE("%d/%d/%d\n", dwToGo, GetTickCount(), me->dwDeltaTime);
1645 while ((dwCurrTC = GetTickCount()) < dwToGo) {
1646 if (MsgWaitForMultipleObjects(0, NULL, FALSE, dwToGo - dwCurrTC, QS_ALLINPUT) == WAIT_OBJECT_0) {
1647 /* got a message, handle it */
1648 while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
1649 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1650 goto the_end;
1652 lpData = 0;
1653 } else {
1654 /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
1655 break;
1659 switch (MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK)) {
1660 case MEVT_COMMENT:
1661 FIXME("NIY: MEVT_COMMENT\n");
1662 /* do nothing, skip bytes */
1663 break;
1664 case MEVT_LONGMSG:
1665 FIXME("NIY: MEVT_LONGMSG, aka sending Sysex event\n");
1666 break;
1667 case MEVT_NOP:
1668 break;
1669 case MEVT_SHORTMSG:
1670 midiOutShortMsg(lpMidiStrm->hDevice, MEVT_EVENTPARM(me->dwEvent));
1671 break;
1672 case MEVT_TEMPO:
1673 lpMidiStrm->dwTempo = MEVT_EVENTPARM(me->dwEvent);
1674 break;
1675 case MEVT_VERSION:
1676 break;
1677 default:
1678 FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK));
1679 break;
1681 if (me->dwEvent & MEVT_F_CALLBACK) {
1682 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1683 (HDRVR)lpMidiStrm->hDevice, MM_MOM_POSITIONCB,
1684 lpwm->mod.dwInstance, (LPARAM)lpMidiHdr, 0L);
1686 lpMidiHdr->dwOffset += sizeof(MIDIEVENT) - sizeof(me->dwParms);
1687 if (me->dwEvent & MEVT_F_LONG)
1688 lpMidiHdr->dwOffset += (MEVT_EVENTPARM(me->dwEvent) + 3) & ~3;
1689 if (lpMidiHdr->dwOffset >= lpMidiHdr->dwBytesRecorded) {
1690 /* done with this header */
1691 lpMidiHdr->dwFlags |= MHDR_DONE;
1692 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1694 lpMidiStrm->lpMidiHdr = lpMidiHdr->lpNext;
1695 DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1696 (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1697 lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1698 lpData = 0;
1701 the_end:
1702 TRACE("End of thread\n");
1703 return 0;
1706 /**************************************************************************
1707 * MMSYSTEM_MidiStream_PostMessage [internal]
1709 static BOOL MMSYSTEM_MidiStream_PostMessage(WINE_MIDIStream* lpMidiStrm, WORD msg, DWORD pmt1, DWORD pmt2)
1711 if (PostThreadMessageA(lpMidiStrm->dwThreadID, msg, pmt1, pmt2)) {
1712 MsgWaitForMultipleObjects( 1, &lpMidiStrm->hEvent, FALSE, INFINITE, 0 );
1713 } else {
1714 ERR("bad PostThreadMessageA\n");
1715 return FALSE;
1717 return TRUE;
1720 /**************************************************************************
1721 * midiStreamClose [WINMM.@]
1723 MMRESULT WINAPI midiStreamClose(HMIDISTRM hMidiStrm)
1725 WINE_MIDIStream* lpMidiStrm;
1727 TRACE("(%p)!\n", hMidiStrm);
1729 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1730 return MMSYSERR_INVALHANDLE;
1732 midiStreamStop(hMidiStrm);
1733 MMSYSTEM_MidiStream_PostMessage(lpMidiStrm, WM_QUIT, 0, 0);
1734 CloseHandle(lpMidiStrm->hEvent);
1735 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1737 return midiOutClose((HMIDIOUT)hMidiStrm);
1740 /**************************************************************************
1741 * midiStreamOpen [WINMM.@]
1743 MMRESULT WINAPI midiStreamOpen(HMIDISTRM* lphMidiStrm, LPUINT lpuDeviceID,
1744 DWORD cMidi, DWORD_PTR dwCallback,
1745 DWORD_PTR dwInstance, DWORD fdwOpen)
1747 WINE_MIDIStream* lpMidiStrm;
1748 MMRESULT ret;
1749 MIDIOPENSTRMID mosm;
1750 LPWINE_MIDI lpwm;
1751 HMIDIOUT hMidiOut;
1753 TRACE("(%p, %p, %d, 0x%08lx, 0x%08lx, 0x%08x)!\n",
1754 lphMidiStrm, lpuDeviceID, cMidi, dwCallback, dwInstance, fdwOpen);
1756 if (cMidi != 1 || lphMidiStrm == NULL || lpuDeviceID == NULL)
1757 return MMSYSERR_INVALPARAM;
1759 lpMidiStrm = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream));
1760 if (!lpMidiStrm)
1761 return MMSYSERR_NOMEM;
1763 lpMidiStrm->dwTempo = 500000;
1764 lpMidiStrm->dwTimeDiv = 480; /* 480 is 120 quarter notes per minute *//* FIXME ??*/
1765 lpMidiStrm->dwPositionMS = 0;
1767 mosm.dwStreamID = (DWORD)lpMidiStrm;
1768 /* FIXME: the correct value is not allocated yet for MAPPER */
1769 mosm.wDeviceID = *lpuDeviceID;
1770 lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &fdwOpen, 1, &mosm);
1771 if (!lpwm) {
1772 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1773 return MMSYSERR_NOMEM;
1775 lpMidiStrm->hDevice = hMidiOut;
1776 *lphMidiStrm = (HMIDISTRM)hMidiOut;
1778 lpwm->mld.uDeviceID = *lpuDeviceID;
1780 ret = MMDRV_Open(&lpwm->mld, MODM_OPEN, (DWORD_PTR)&lpwm->mod, fdwOpen);
1781 if (ret != MMSYSERR_NOERROR) {
1782 MMDRV_Free(hMidiOut, &lpwm->mld);
1783 HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1784 return ret;
1787 lpMidiStrm->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1788 lpMidiStrm->wFlags = HIWORD(fdwOpen);
1790 lpMidiStrm->hThread = CreateThread(NULL, 0, MMSYSTEM_MidiStream_Player,
1791 lpMidiStrm, 0, &(lpMidiStrm->dwThreadID));
1793 if (!lpMidiStrm->hThread) {
1794 midiStreamClose((HMIDISTRM)hMidiOut);
1795 return MMSYSERR_NOMEM;
1797 SetThreadPriority(lpMidiStrm->hThread, THREAD_PRIORITY_TIME_CRITICAL);
1799 /* wait for thread to have started, and for its queue to be created */
1800 WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
1801 /* start in paused mode */
1802 SuspendThread(lpMidiStrm->hThread);
1804 TRACE("=> (%u/%d) hMidi=%p ret=%d lpMidiStrm=%p\n",
1805 *lpuDeviceID, lpwm->mld.uDeviceID, *lphMidiStrm, ret, lpMidiStrm);
1806 return ret;
1809 /**************************************************************************
1810 * midiStreamOut [WINMM.@]
1812 MMRESULT WINAPI midiStreamOut(HMIDISTRM hMidiStrm, LPMIDIHDR lpMidiHdr,
1813 UINT cbMidiHdr)
1815 WINE_MIDIStream* lpMidiStrm;
1816 DWORD ret = MMSYSERR_NOERROR;
1818 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMidiHdr, cbMidiHdr);
1820 if (cbMidiHdr < sizeof(MIDIHDR) || !lpMidiHdr || !lpMidiHdr->lpData
1821 || lpMidiHdr->dwBufferLength < lpMidiHdr->dwBytesRecorded)
1822 return MMSYSERR_INVALPARAM;
1823 /* FIXME: Native additionaly checks if the MIDIEVENTs in lpData
1824 * exactly fit dwBytesRecorded. */
1826 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1827 return MIDIERR_UNPREPARED;
1829 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1830 return MIDIERR_STILLPLAYING;
1832 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1833 ret = MMSYSERR_INVALHANDLE;
1834 } else {
1835 lpMidiHdr->dwFlags |= MHDR_ISSTRM | MHDR_INQUEUE;
1836 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1837 if (!PostThreadMessageA(lpMidiStrm->dwThreadID,
1838 WINE_MSM_HEADER, cbMidiHdr,
1839 (LPARAM)lpMidiHdr)) {
1840 ERR("bad PostThreadMessageA\n");
1841 ret = MMSYSERR_ERROR;
1844 return ret;
1847 /**************************************************************************
1848 * midiStreamPause [WINMM.@]
1850 MMRESULT WINAPI midiStreamPause(HMIDISTRM hMidiStrm)
1852 WINE_MIDIStream* lpMidiStrm;
1853 DWORD ret = MMSYSERR_NOERROR;
1855 TRACE("(%p)!\n", hMidiStrm);
1857 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1858 ret = MMSYSERR_INVALHANDLE;
1859 } else {
1860 if (SuspendThread(lpMidiStrm->hThread) == 0xFFFFFFFF) {
1861 ERR("bad Suspend (%d)\n", GetLastError());
1862 ret = MMSYSERR_ERROR;
1865 return ret;
1868 /**************************************************************************
1869 * midiStreamPosition [WINMM.@]
1871 MMRESULT WINAPI midiStreamPosition(HMIDISTRM hMidiStrm, LPMMTIME lpMMT, UINT cbmmt)
1873 WINE_MIDIStream* lpMidiStrm;
1874 DWORD ret = MMSYSERR_NOERROR;
1876 TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMMT, cbmmt);
1878 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1879 ret = MMSYSERR_INVALHANDLE;
1880 } else if (lpMMT == NULL || cbmmt != sizeof(MMTIME)) {
1881 ret = MMSYSERR_INVALPARAM;
1882 } else {
1883 switch (lpMMT->wType) {
1884 default:
1885 FIXME("Unsupported time type %x\n", lpMMT->wType);
1886 case TIME_BYTES:
1887 case TIME_SAMPLES:
1888 lpMMT->wType = TIME_MS;
1889 /* fall through to alternative format */
1890 case TIME_MS:
1891 lpMMT->u.ms = lpMidiStrm->dwPositionMS;
1892 TRACE("=> %d ms\n", lpMMT->u.ms);
1893 break;
1894 case TIME_TICKS:
1895 lpMMT->u.ticks = lpMidiStrm->dwPulses;
1896 TRACE("=> %d ticks\n", lpMMT->u.ticks);
1897 break;
1900 return ret;
1903 /**************************************************************************
1904 * midiStreamProperty [WINMM.@]
1906 MMRESULT WINAPI midiStreamProperty(HMIDISTRM hMidiStrm, LPBYTE lpPropData, DWORD dwProperty)
1908 WINE_MIDIStream* lpMidiStrm;
1909 MMRESULT ret = MMSYSERR_NOERROR;
1911 TRACE("(%p, %p, %x)\n", hMidiStrm, lpPropData, dwProperty);
1913 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1914 ret = MMSYSERR_INVALHANDLE;
1915 } else if ((dwProperty & (MIDIPROP_GET|MIDIPROP_SET)) == 0) {
1916 ret = MMSYSERR_INVALPARAM;
1917 } else if (dwProperty & MIDIPROP_TEMPO) {
1918 MIDIPROPTEMPO* mpt = (MIDIPROPTEMPO*)lpPropData;
1920 if (sizeof(MIDIPROPTEMPO) != mpt->cbStruct) {
1921 ret = MMSYSERR_INVALPARAM;
1922 } else if (dwProperty & MIDIPROP_SET) {
1923 lpMidiStrm->dwTempo = mpt->dwTempo;
1924 TRACE("Setting tempo to %d\n", mpt->dwTempo);
1925 } else if (dwProperty & MIDIPROP_GET) {
1926 mpt->dwTempo = lpMidiStrm->dwTempo;
1927 TRACE("Getting tempo <= %d\n", mpt->dwTempo);
1929 } else if (dwProperty & MIDIPROP_TIMEDIV) {
1930 MIDIPROPTIMEDIV* mptd = (MIDIPROPTIMEDIV*)lpPropData;
1932 if (sizeof(MIDIPROPTIMEDIV) != mptd->cbStruct) {
1933 ret = MMSYSERR_INVALPARAM;
1934 } else if (dwProperty & MIDIPROP_SET) {
1935 lpMidiStrm->dwTimeDiv = mptd->dwTimeDiv;
1936 TRACE("Setting time div to %d\n", mptd->dwTimeDiv);
1937 } else if (dwProperty & MIDIPROP_GET) {
1938 mptd->dwTimeDiv = lpMidiStrm->dwTimeDiv;
1939 TRACE("Getting time div <= %d\n", mptd->dwTimeDiv);
1941 } else {
1942 ret = MMSYSERR_INVALPARAM;
1945 return ret;
1948 /**************************************************************************
1949 * midiStreamRestart [WINMM.@]
1951 MMRESULT WINAPI midiStreamRestart(HMIDISTRM hMidiStrm)
1953 WINE_MIDIStream* lpMidiStrm;
1954 MMRESULT ret = MMSYSERR_NOERROR;
1956 TRACE("(%p)!\n", hMidiStrm);
1958 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1959 ret = MMSYSERR_INVALHANDLE;
1960 } else {
1961 DWORD ret;
1963 /* since we increase the thread suspend count on each midiStreamPause
1964 * there may be a need for several ResumeThread
1966 do {
1967 ret = ResumeThread(lpMidiStrm->hThread);
1968 } while (ret != 0xFFFFFFFF && ret > 1);
1969 if (ret == 0xFFFFFFFF) {
1970 ERR("bad Resume (%d)\n", GetLastError());
1971 ret = MMSYSERR_ERROR;
1972 } else {
1973 lpMidiStrm->dwStartTicks = GetTickCount() - lpMidiStrm->dwPositionMS;
1976 return ret;
1979 /**************************************************************************
1980 * midiStreamStop [WINMM.@]
1982 MMRESULT WINAPI midiStreamStop(HMIDISTRM hMidiStrm)
1984 WINE_MIDIStream* lpMidiStrm;
1985 MMRESULT ret = MMSYSERR_NOERROR;
1987 TRACE("(%p)!\n", hMidiStrm);
1989 if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1990 ret = MMSYSERR_INVALHANDLE;
1991 } else {
1992 /* in case stream has been paused... FIXME is the current state correct ? */
1993 midiStreamRestart(hMidiStrm);
1994 MMSYSTEM_MidiStream_PostMessage(lpMidiStrm, WINE_MSM_STOP, 0, 0);
1996 return ret;
1999 static UINT WAVE_Open(HANDLE* lphndl, UINT uDeviceID, UINT uType,
2000 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2001 DWORD_PTR dwInstance, DWORD dwFlags)
2003 HANDLE handle;
2004 LPWINE_MLD wmld;
2005 DWORD dwRet = MMSYSERR_NOERROR;
2006 WAVEOPENDESC wod;
2008 TRACE("(%p, %d, %s, %p, %08lX, %08lX, %08X);\n",
2009 lphndl, (int)uDeviceID, (uType==MMDRV_WAVEOUT)?"Out":"In", lpFormat, dwCallback,
2010 dwInstance, dwFlags);
2012 if (dwFlags & WAVE_FORMAT_QUERY)
2013 TRACE("WAVE_FORMAT_QUERY requested !\n");
2015 if (lpFormat == NULL) {
2016 WARN("bad format\n");
2017 return WAVERR_BADFORMAT;
2020 if ((dwFlags & WAVE_MAPPED) && (uDeviceID == (UINT)-1)) {
2021 WARN("invalid parameter\n");
2022 return MMSYSERR_INVALPARAM;
2025 /* may have a PCMWAVEFORMAT rather than a WAVEFORMATEX so don't read cbSize */
2026 TRACE("wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u\n",
2027 lpFormat->wFormatTag, lpFormat->nChannels, lpFormat->nSamplesPerSec,
2028 lpFormat->nAvgBytesPerSec, lpFormat->nBlockAlign, lpFormat->wBitsPerSample);
2030 if ((wmld = MMDRV_Alloc(sizeof(WINE_WAVE), uType, &handle,
2031 &dwFlags, &dwCallback, &dwInstance)) == NULL) {
2032 WARN("no memory\n");
2033 return MMSYSERR_NOMEM;
2036 wod.hWave = handle;
2037 wod.lpFormat = (LPWAVEFORMATEX)lpFormat; /* should the struct be copied iso pointer? */
2038 wod.dwCallback = dwCallback;
2039 wod.dwInstance = dwInstance;
2040 wod.dnDevNode = 0L;
2042 TRACE("cb=%08lx\n", wod.dwCallback);
2044 for (;;) {
2045 if (dwFlags & WAVE_MAPPED) {
2046 wod.uMappedDeviceID = uDeviceID;
2047 uDeviceID = WAVE_MAPPER;
2048 } else {
2049 wod.uMappedDeviceID = -1;
2051 wmld->uDeviceID = uDeviceID;
2053 dwRet = MMDRV_Open(wmld, (uType == MMDRV_WAVEOUT) ? WODM_OPEN : WIDM_OPEN,
2054 (DWORD_PTR)&wod, dwFlags);
2056 TRACE("dwRet = %s\n", WINMM_ErrorToString(dwRet));
2057 if (dwRet != WAVERR_BADFORMAT ||
2058 ((dwFlags & (WAVE_MAPPED|WAVE_FORMAT_DIRECT)) != 0) || (uDeviceID == WAVE_MAPPER)) break;
2059 /* if we ask for a format which isn't supported by the physical driver,
2060 * let's try to map it through the wave mapper (except, if we already tried
2061 * or user didn't allow us to use acm codecs or the device is already the mapper)
2063 dwFlags |= WAVE_MAPPED;
2064 /* we shall loop only one */
2067 if ((dwFlags & WAVE_FORMAT_QUERY) || dwRet != MMSYSERR_NOERROR) {
2068 MMDRV_Free(handle, wmld);
2069 handle = 0;
2072 if (lphndl != NULL) *lphndl = handle;
2073 TRACE("=> %s hWave=%p\n", WINMM_ErrorToString(dwRet), handle);
2075 return dwRet;
2078 /**************************************************************************
2079 * waveOutGetNumDevs [WINMM.@]
2081 UINT WINAPI waveOutGetNumDevs(void)
2083 return MMDRV_GetNum(MMDRV_WAVEOUT);
2086 /**************************************************************************
2087 * waveOutGetDevCapsA [WINMM.@]
2089 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2090 UINT uSize)
2092 WAVEOUTCAPSW wocW;
2093 UINT ret;
2095 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2097 ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2099 if (ret == MMSYSERR_NOERROR) {
2100 WAVEOUTCAPSA wocA;
2101 wocA.wMid = wocW.wMid;
2102 wocA.wPid = wocW.wPid;
2103 wocA.vDriverVersion = wocW.vDriverVersion;
2104 WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2105 sizeof(wocA.szPname), NULL, NULL );
2106 wocA.dwFormats = wocW.dwFormats;
2107 wocA.wChannels = wocW.wChannels;
2108 wocA.dwSupport = wocW.dwSupport;
2109 memcpy(lpCaps, &wocA, min(uSize, sizeof(wocA)));
2111 return ret;
2114 /**************************************************************************
2115 * waveOutGetDevCapsW [WINMM.@]
2117 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2118 UINT uSize)
2120 LPWINE_MLD wmld;
2122 TRACE("(%lu %p %u)!\n", uDeviceID, lpCaps, uSize);
2124 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2126 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_WAVEOUT, TRUE)) == NULL)
2127 return MMSYSERR_BADDEVICEID;
2129 return MMDRV_Message(wmld, WODM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
2132 /**************************************************************************
2133 * waveOutGetErrorTextA [WINMM.@]
2134 * waveInGetErrorTextA [WINMM.@]
2136 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2138 UINT ret;
2140 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2141 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2142 else
2144 LPWSTR xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2145 if (!xstr) ret = MMSYSERR_NOMEM;
2146 else
2148 ret = waveOutGetErrorTextW(uError, xstr, uSize);
2149 if (ret == MMSYSERR_NOERROR)
2150 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2151 HeapFree(GetProcessHeap(), 0, xstr);
2154 return ret;
2157 /**************************************************************************
2158 * waveOutGetErrorTextW [WINMM.@]
2159 * waveInGetErrorTextW [WINMM.@]
2161 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2163 UINT ret = MMSYSERR_BADERRNUM;
2165 if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2166 else if (uSize == 0) ret = MMSYSERR_NOERROR;
2167 else if (
2168 /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2169 * a warning for the test was always true */
2170 (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2171 (uError >= WAVERR_BASE && uError <= WAVERR_LASTERROR)) {
2172 if (LoadStringW(hWinMM32Instance,
2173 uError, lpText, uSize) > 0) {
2174 ret = MMSYSERR_NOERROR;
2177 return ret;
2180 /**************************************************************************
2181 * waveOutOpen [WINMM.@]
2182 * All the args/structs have the same layout as the win16 equivalents
2184 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2185 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2186 DWORD_PTR dwInstance, DWORD dwFlags)
2188 return WAVE_Open((HANDLE*)lphWaveOut, uDeviceID, MMDRV_WAVEOUT, lpFormat,
2189 dwCallback, dwInstance, dwFlags);
2192 /**************************************************************************
2193 * waveOutClose [WINMM.@]
2195 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2197 LPWINE_MLD wmld;
2198 DWORD dwRet;
2200 TRACE("(%p)\n", hWaveOut);
2202 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2203 return MMSYSERR_INVALHANDLE;
2205 dwRet = MMDRV_Close(wmld, WODM_CLOSE);
2206 if (dwRet != WAVERR_STILLPLAYING)
2207 MMDRV_Free(hWaveOut, wmld);
2209 return dwRet;
2212 /**************************************************************************
2213 * waveOutPrepareHeader [WINMM.@]
2215 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2216 WAVEHDR* lpWaveOutHdr, UINT uSize)
2218 LPWINE_MLD wmld;
2219 UINT result;
2221 TRACE("(%p, %p, %u);\n", hWaveOut, lpWaveOutHdr, uSize);
2223 if (lpWaveOutHdr == NULL || uSize < sizeof (WAVEHDR))
2224 return MMSYSERR_INVALPARAM;
2226 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2227 return MMSYSERR_INVALHANDLE;
2229 if ((result = MMDRV_Message(wmld, WODM_PREPARE, (DWORD_PTR)lpWaveOutHdr,
2230 uSize)) != MMSYSERR_NOTSUPPORTED)
2231 return result;
2233 if (lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2234 return WAVERR_STILLPLAYING;
2236 lpWaveOutHdr->dwFlags |= WHDR_PREPARED;
2237 lpWaveOutHdr->dwFlags &= ~WHDR_DONE;
2239 return MMSYSERR_NOERROR;
2242 /**************************************************************************
2243 * waveOutUnprepareHeader [WINMM.@]
2245 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2246 LPWAVEHDR lpWaveOutHdr, UINT uSize)
2248 LPWINE_MLD wmld;
2249 UINT result;
2251 TRACE("(%p, %p, %u);\n", hWaveOut, lpWaveOutHdr, uSize);
2253 if (lpWaveOutHdr == NULL || uSize < sizeof (WAVEHDR))
2254 return MMSYSERR_INVALPARAM;
2256 if (!(lpWaveOutHdr->dwFlags & WHDR_PREPARED)) {
2257 return MMSYSERR_NOERROR;
2260 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2261 return MMSYSERR_INVALHANDLE;
2263 if ((result = MMDRV_Message(wmld, WODM_UNPREPARE, (DWORD_PTR)lpWaveOutHdr,
2264 uSize)) != MMSYSERR_NOTSUPPORTED)
2265 return result;
2267 if (lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2268 return WAVERR_STILLPLAYING;
2270 lpWaveOutHdr->dwFlags &= ~WHDR_PREPARED;
2271 lpWaveOutHdr->dwFlags |= WHDR_DONE;
2273 return MMSYSERR_NOERROR;
2276 /**************************************************************************
2277 * waveOutWrite [WINMM.@]
2279 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, LPWAVEHDR lpWaveOutHdr,
2280 UINT uSize)
2282 LPWINE_MLD wmld;
2284 TRACE("(%p, %p, %u);\n", hWaveOut, lpWaveOutHdr, uSize);
2286 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2287 return MMSYSERR_INVALHANDLE;
2289 return MMDRV_Message(wmld, WODM_WRITE, (DWORD_PTR)lpWaveOutHdr, uSize);
2292 /**************************************************************************
2293 * waveOutBreakLoop [WINMM.@]
2295 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2297 LPWINE_MLD wmld;
2299 TRACE("(%p);\n", hWaveOut);
2301 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2302 return MMSYSERR_INVALHANDLE;
2303 return MMDRV_Message(wmld, WODM_BREAKLOOP, 0L, 0L);
2306 /**************************************************************************
2307 * waveOutPause [WINMM.@]
2309 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2311 LPWINE_MLD wmld;
2313 TRACE("(%p);\n", hWaveOut);
2315 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2316 return MMSYSERR_INVALHANDLE;
2317 return MMDRV_Message(wmld, WODM_PAUSE, 0L, 0L);
2320 /**************************************************************************
2321 * waveOutReset [WINMM.@]
2323 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2325 LPWINE_MLD wmld;
2327 TRACE("(%p);\n", hWaveOut);
2329 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2330 return MMSYSERR_INVALHANDLE;
2331 return MMDRV_Message(wmld, WODM_RESET, 0L, 0L);
2334 /**************************************************************************
2335 * waveOutRestart [WINMM.@]
2337 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2339 LPWINE_MLD wmld;
2341 TRACE("(%p);\n", hWaveOut);
2343 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2344 return MMSYSERR_INVALHANDLE;
2345 return MMDRV_Message(wmld, WODM_RESTART, 0L, 0L);
2348 /**************************************************************************
2349 * waveOutGetPosition [WINMM.@]
2351 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2352 UINT uSize)
2354 LPWINE_MLD wmld;
2356 TRACE("(%p, %p, %u);\n", hWaveOut, lpTime, uSize);
2358 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2359 return MMSYSERR_INVALHANDLE;
2361 return MMDRV_Message(wmld, WODM_GETPOS, (DWORD_PTR)lpTime, uSize);
2364 /**************************************************************************
2365 * waveOutGetPitch [WINMM.@]
2367 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
2369 LPWINE_MLD wmld;
2371 TRACE("(%p, %p);\n", hWaveOut, lpdw);
2373 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2374 return MMSYSERR_INVALHANDLE;
2375 return MMDRV_Message(wmld, WODM_GETPITCH, (DWORD_PTR)lpdw, 0L);
2378 /**************************************************************************
2379 * waveOutSetPitch [WINMM.@]
2381 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
2383 LPWINE_MLD wmld;
2385 TRACE("(%p, %08x);\n", hWaveOut, dw);
2387 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2388 return MMSYSERR_INVALHANDLE;
2389 return MMDRV_Message(wmld, WODM_SETPITCH, dw, 0L);
2392 /**************************************************************************
2393 * waveOutGetPlaybackRate [WINMM.@]
2395 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
2397 LPWINE_MLD wmld;
2399 TRACE("(%p, %p);\n", hWaveOut, lpdw);
2401 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2402 return MMSYSERR_INVALHANDLE;
2403 return MMDRV_Message(wmld, WODM_GETPLAYBACKRATE, (DWORD_PTR)lpdw, 0L);
2406 /**************************************************************************
2407 * waveOutSetPlaybackRate [WINMM.@]
2409 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
2411 LPWINE_MLD wmld;
2413 TRACE("(%p, %08x);\n", hWaveOut, dw);
2415 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2416 return MMSYSERR_INVALHANDLE;
2417 return MMDRV_Message(wmld, WODM_SETPLAYBACKRATE, dw, 0L);
2420 /**************************************************************************
2421 * waveOutGetVolume [WINMM.@]
2423 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, LPDWORD lpdw)
2425 LPWINE_MLD wmld;
2427 TRACE("(%p, %p);\n", hWaveOut, lpdw);
2429 if (lpdw == NULL) {
2430 WARN("invalid parameter\n");
2431 return MMSYSERR_INVALPARAM;
2434 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, TRUE)) == NULL)
2435 return MMSYSERR_INVALHANDLE;
2437 return MMDRV_Message(wmld, WODM_GETVOLUME, (DWORD_PTR)lpdw, 0L);
2440 /**************************************************************************
2441 * waveOutSetVolume [WINMM.@]
2443 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD dw)
2445 LPWINE_MLD wmld;
2447 TRACE("(%p, %08x);\n", hWaveOut, dw);
2449 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, TRUE)) == NULL)
2450 return MMSYSERR_INVALHANDLE;
2452 return MMDRV_Message(wmld, WODM_SETVOLUME, dw, 0L);
2455 /**************************************************************************
2456 * waveOutGetID [WINMM.@]
2458 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
2460 LPWINE_MLD wmld;
2462 TRACE("(%p, %p);\n", hWaveOut, lpuDeviceID);
2464 if (lpuDeviceID == NULL) return MMSYSERR_INVALHANDLE;
2466 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2467 return MMSYSERR_INVALHANDLE;
2469 *lpuDeviceID = wmld->uDeviceID;
2470 return 0;
2473 /**************************************************************************
2474 * waveOutMessage [WINMM.@]
2476 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
2477 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2479 LPWINE_MLD wmld;
2481 TRACE("(%p, %u, %ld, %ld)\n", hWaveOut, uMessage, dwParam1, dwParam2);
2483 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) {
2484 if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, TRUE)) != NULL) {
2485 return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
2487 WARN("invalid handle\n");
2488 return MMSYSERR_INVALHANDLE;
2491 /* from M$ KB */
2492 if (uMessage < DRVM_IOCTL || (uMessage >= DRVM_IOCTL_LAST && uMessage < DRVM_MAPPER)) {
2493 WARN("invalid parameter\n");
2494 return MMSYSERR_INVALPARAM;
2497 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
2500 /**************************************************************************
2501 * waveInGetNumDevs [WINMM.@]
2503 UINT WINAPI waveInGetNumDevs(void)
2505 return MMDRV_GetNum(MMDRV_WAVEIN);
2508 /**************************************************************************
2509 * waveInGetDevCapsW [WINMM.@]
2511 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
2513 LPWINE_MLD wmld;
2515 TRACE("(%lu %p %u)!\n", uDeviceID, lpCaps, uSize);
2517 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2519 if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_WAVEIN, TRUE)) == NULL)
2520 return MMSYSERR_BADDEVICEID;
2522 return MMDRV_Message(wmld, WIDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
2525 /**************************************************************************
2526 * waveInGetDevCapsA [WINMM.@]
2528 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
2530 WAVEINCAPSW wicW;
2531 UINT ret;
2533 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2535 ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
2537 if (ret == MMSYSERR_NOERROR) {
2538 WAVEINCAPSA wicA;
2539 wicA.wMid = wicW.wMid;
2540 wicA.wPid = wicW.wPid;
2541 wicA.vDriverVersion = wicW.vDriverVersion;
2542 WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
2543 sizeof(wicA.szPname), NULL, NULL );
2544 wicA.dwFormats = wicW.dwFormats;
2545 wicA.wChannels = wicW.wChannels;
2546 memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
2548 return ret;
2551 /**************************************************************************
2552 * waveInOpen [WINMM.@]
2554 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
2555 LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2556 DWORD_PTR dwInstance, DWORD dwFlags)
2558 return WAVE_Open((HANDLE*)lphWaveIn, uDeviceID, MMDRV_WAVEIN, lpFormat,
2559 dwCallback, dwInstance, dwFlags);
2562 /**************************************************************************
2563 * waveInClose [WINMM.@]
2565 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
2567 LPWINE_MLD wmld;
2568 DWORD dwRet;
2570 TRACE("(%p)\n", hWaveIn);
2572 if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2573 return MMSYSERR_INVALHANDLE;
2575 dwRet = MMDRV_Message(wmld, WIDM_CLOSE, 0L, 0L);
2576 if (dwRet != WAVERR_STILLPLAYING)
2577 MMDRV_Free(hWaveIn, wmld);
2578 return dwRet;
2581 /**************************************************************************
2582 * waveInPrepareHeader [WINMM.@]
2584 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
2585 UINT uSize)
2587 LPWINE_MLD wmld;
2588 UINT result;
2590 TRACE("(%p, %p, %u);\n", hWaveIn, lpWaveInHdr, uSize);
2592 if (lpWaveInHdr == NULL || uSize < sizeof (WAVEHDR))
2593 return MMSYSERR_INVALPARAM;
2595 if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2596 return MMSYSERR_INVALHANDLE;
2598 if ((result = MMDRV_Message(wmld, WIDM_PREPARE, (DWORD_PTR)lpWaveInHdr,
2599 uSize)) != MMSYSERR_NOTSUPPORTED)
2600 return result;
2602 if (lpWaveInHdr->dwFlags & WHDR_INQUEUE)
2603 return WAVERR_STILLPLAYING;
2605 lpWaveInHdr->dwFlags |= WHDR_PREPARED;
2606 lpWaveInHdr->dwFlags &= ~WHDR_DONE;
2607 lpWaveInHdr->dwBytesRecorded = 0;
2609 return MMSYSERR_NOERROR;
2612 /**************************************************************************
2613 * waveInUnprepareHeader [WINMM.@]
2615 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
2616 UINT uSize)
2618 LPWINE_MLD wmld;
2619 UINT result;
2621 TRACE("(%p, %p, %u);\n", hWaveIn, lpWaveInHdr, uSize);
2623 if (lpWaveInHdr == NULL || uSize < sizeof (WAVEHDR))
2624 return MMSYSERR_INVALPARAM;
2626 if (!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
2627 return MMSYSERR_NOERROR;
2629 if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2630 return MMSYSERR_INVALHANDLE;
2632 if ((result = MMDRV_Message(wmld, WIDM_UNPREPARE, (DWORD_PTR)lpWaveInHdr,
2633 uSize)) != MMSYSERR_NOTSUPPORTED)
2634 return result;
2636 if (lpWaveInHdr->dwFlags & WHDR_INQUEUE)
2637 return WAVERR_STILLPLAYING;
2639 lpWaveInHdr->dwFlags &= ~WHDR_PREPARED;
2640 lpWaveInHdr->dwFlags |= WHDR_DONE;
2642 return MMSYSERR_NOERROR;
2645 /**************************************************************************
2646 * waveInAddBuffer [WINMM.@]
2648 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn,
2649 WAVEHDR* lpWaveInHdr, UINT uSize)
2651 LPWINE_MLD wmld;
2653 TRACE("(%p, %p, %u);\n", hWaveIn, lpWaveInHdr, uSize);
2655 if (lpWaveInHdr == NULL) return MMSYSERR_INVALPARAM;
2656 if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2657 return MMSYSERR_INVALHANDLE;
2659 return MMDRV_Message(wmld, WIDM_ADDBUFFER, (DWORD_PTR)lpWaveInHdr, uSize);
2662 /**************************************************************************
2663 * waveInReset [WINMM.@]
2665 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
2667 LPWINE_MLD wmld;
2669 TRACE("(%p);\n", hWaveIn);
2671 if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2672 return MMSYSERR_INVALHANDLE;
2674 return MMDRV_Message(wmld, WIDM_RESET, 0L, 0L);
2677 /**************************************************************************
2678 * waveInStart [WINMM.@]
2680 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
2682 LPWINE_MLD wmld;
2684 TRACE("(%p);\n", hWaveIn);
2686 if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2687 return MMSYSERR_INVALHANDLE;
2689 return MMDRV_Message(wmld, WIDM_START, 0L, 0L);
2692 /**************************************************************************
2693 * waveInStop [WINMM.@]
2695 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
2697 LPWINE_MLD wmld;
2699 TRACE("(%p);\n", hWaveIn);
2701 if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2702 return MMSYSERR_INVALHANDLE;
2704 return MMDRV_Message(wmld,WIDM_STOP, 0L, 0L);
2707 /**************************************************************************
2708 * waveInGetPosition [WINMM.@]
2710 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
2711 UINT uSize)
2713 LPWINE_MLD wmld;
2715 TRACE("(%p, %p, %u);\n", hWaveIn, lpTime, uSize);
2717 if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2718 return MMSYSERR_INVALHANDLE;
2720 return MMDRV_Message(wmld, WIDM_GETPOS, (DWORD_PTR)lpTime, uSize);
2723 /**************************************************************************
2724 * waveInGetID [WINMM.@]
2726 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
2728 LPWINE_MLD wmld;
2730 TRACE("(%p, %p);\n", hWaveIn, lpuDeviceID);
2732 if (lpuDeviceID == NULL) return MMSYSERR_INVALHANDLE;
2734 if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2735 return MMSYSERR_INVALHANDLE;
2737 *lpuDeviceID = wmld->uDeviceID;
2738 return MMSYSERR_NOERROR;
2741 /**************************************************************************
2742 * waveInMessage [WINMM.@]
2744 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
2745 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2747 LPWINE_MLD wmld;
2749 TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
2751 if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) {
2752 if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, TRUE)) != NULL) {
2753 return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
2755 return MMSYSERR_INVALHANDLE;
2758 /* from M$ KB */
2759 if (uMessage < DRVM_IOCTL || (uMessage >= DRVM_IOCTL_LAST && uMessage < DRVM_MAPPER))
2760 return MMSYSERR_INVALPARAM;
2763 return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
2766 struct mm_starter
2768 LPTASKCALLBACK cb;
2769 DWORD client;
2770 HANDLE event;
2773 static DWORD WINAPI mmTaskRun(void* pmt)
2775 struct mm_starter mms;
2777 memcpy(&mms, pmt, sizeof(struct mm_starter));
2778 HeapFree(GetProcessHeap(), 0, pmt);
2779 mms.cb(mms.client);
2780 if (mms.event) SetEvent(mms.event);
2781 return 0;
2784 /******************************************************************
2785 * mmTaskCreate (WINMM.@)
2787 UINT WINAPI mmTaskCreate(LPTASKCALLBACK cb, HANDLE* ph, DWORD_PTR client)
2789 HANDLE hThread;
2790 HANDLE hEvent = 0;
2791 struct mm_starter *mms;
2793 mms = HeapAlloc(GetProcessHeap(), 0, sizeof(struct mm_starter));
2794 if (mms == NULL) return TASKERR_OUTOFMEMORY;
2796 mms->cb = cb;
2797 mms->client = client;
2798 if (ph) hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2799 mms->event = hEvent;
2801 hThread = CreateThread(0, 0, mmTaskRun, mms, 0, NULL);
2802 if (!hThread) {
2803 HeapFree(GetProcessHeap(), 0, mms);
2804 if (hEvent) CloseHandle(hEvent);
2805 return TASKERR_OUTOFMEMORY;
2807 SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
2808 if (ph) *ph = hEvent;
2809 CloseHandle(hThread);
2810 return 0;
2813 /******************************************************************
2814 * mmTaskBlock (WINMM.@)
2816 VOID WINAPI mmTaskBlock(DWORD tid)
2818 MSG msg;
2822 GetMessageA(&msg, 0, 0, 0);
2823 if (msg.hwnd) DispatchMessageA(&msg);
2824 } while (msg.message != WM_USER);
2827 /******************************************************************
2828 * mmTaskSignal (WINMM.@)
2830 BOOL WINAPI mmTaskSignal(DWORD tid)
2832 return PostThreadMessageW(tid, WM_USER, 0, 0);
2835 /******************************************************************
2836 * mmTaskYield (WINMM.@)
2838 VOID WINAPI mmTaskYield(VOID) {}
2840 /******************************************************************
2841 * mmGetCurrentTask (WINMM.@)
2843 DWORD WINAPI mmGetCurrentTask(VOID)
2845 return GetCurrentThreadId();