crypt32: Add tests for encoding/decoding cert policy constraints.
[wine] / dlls / winmm / winmm.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  * WINMM functions
5  *
6  * Copyright 1993      Martin Ayotte
7  *           1998-2002 Eric Pouech
8  *
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.
13  *
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.
18  *
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
22  */
23
24 /*
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
29  */
30
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
40  */
41
42 #include <stdio.h>
43 #include <stdarg.h>
44 #include <string.h>
45
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"
55
56 #include "wine/debug.h"
57
58 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
59
60 /* ========================================================================
61  *                   G L O B A L   S E T T I N G S
62  * ========================================================================*/
63
64 HINSTANCE hWinMM32Instance;
65 HANDLE psLastEvent;
66 HANDLE psStopEvent;
67
68 static CRITICAL_SECTION_DEBUG critsect_debug =
69 {
70     0, 0, &WINMM_cs,
71     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
72       0, 0, { (DWORD_PTR)(__FILE__ ": WINMM_cs") }
73 };
74 CRITICAL_SECTION WINMM_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
75
76 /**************************************************************************
77  *                      WINMM_CreateIData                       [internal]
78  */
79 static  BOOL    WINMM_CreateIData(HINSTANCE hInstDLL)
80 {
81     hWinMM32Instance = hInstDLL;
82     psStopEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
83     psLastEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
84     return TRUE;
85 }
86
87 /**************************************************************************
88  *                      WINMM_DeleteIData                       [internal]
89  */
90 static  void WINMM_DeleteIData(void)
91 {
92     TIME_MMTimeStop();
93
94     /* FIXME: should also free content and resources allocated
95      * inside WINMM_IData */
96     CloseHandle(psStopEvent);
97     CloseHandle(psLastEvent);
98     DeleteCriticalSection(&WINMM_cs);
99 }
100
101 /******************************************************************
102  *             WINMM_ErrorToString
103  */
104 const char* WINMM_ErrorToString(MMRESULT error)
105 {
106 #define ERR_TO_STR(dev) case dev: return #dev
107     static char unknown[32];
108     switch (error) {
109     ERR_TO_STR(MMSYSERR_NOERROR);
110     ERR_TO_STR(MMSYSERR_ERROR);
111     ERR_TO_STR(MMSYSERR_BADDEVICEID);
112     ERR_TO_STR(MMSYSERR_NOTENABLED);
113     ERR_TO_STR(MMSYSERR_ALLOCATED);
114     ERR_TO_STR(MMSYSERR_INVALHANDLE);
115     ERR_TO_STR(MMSYSERR_NODRIVER);
116     ERR_TO_STR(MMSYSERR_NOMEM);
117     ERR_TO_STR(MMSYSERR_NOTSUPPORTED);
118     ERR_TO_STR(MMSYSERR_BADERRNUM);
119     ERR_TO_STR(MMSYSERR_INVALFLAG);
120     ERR_TO_STR(MMSYSERR_INVALPARAM);
121     ERR_TO_STR(MMSYSERR_HANDLEBUSY);
122     ERR_TO_STR(MMSYSERR_INVALIDALIAS);
123     ERR_TO_STR(MMSYSERR_BADDB);
124     ERR_TO_STR(MMSYSERR_KEYNOTFOUND);
125     ERR_TO_STR(MMSYSERR_READERROR);
126     ERR_TO_STR(MMSYSERR_WRITEERROR);
127     ERR_TO_STR(MMSYSERR_DELETEERROR);
128     ERR_TO_STR(MMSYSERR_VALNOTFOUND);
129     ERR_TO_STR(MMSYSERR_NODRIVERCB);
130     ERR_TO_STR(WAVERR_BADFORMAT);
131     ERR_TO_STR(WAVERR_STILLPLAYING);
132     ERR_TO_STR(WAVERR_UNPREPARED);
133     ERR_TO_STR(WAVERR_SYNC);
134     }
135     sprintf(unknown, "Unknown(0x%08x)", error);
136     return unknown;
137 #undef ERR_TO_STR
138 }
139
140 /**************************************************************************
141  *              DllMain (WINMM.init)
142  *
143  * WINMM DLL entry point
144  *
145  */
146 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
147 {
148     TRACE("%p 0x%x %p\n", hInstDLL, fdwReason, fImpLoad);
149
150     switch (fdwReason) {
151     case DLL_PROCESS_ATTACH:
152         DisableThreadLibraryCalls(hInstDLL);
153
154         if (!WINMM_CreateIData(hInstDLL))
155             return FALSE;
156         if (!MMDRV_Init()) {
157             WINMM_DeleteIData();
158             return FALSE;
159         }
160         break;
161     case DLL_PROCESS_DETACH:
162         /* close all opened MCI drivers */
163         MCI_SendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0L);
164         MMDRV_Exit();
165         /* There's no guarantee the drivers haven't already been unloaded on
166          * process shutdown.
167          */
168         if (!fImpLoad)
169         {
170             /* now unload all remaining drivers... */
171             DRIVER_UnloadAll();
172         }
173
174         WINMM_DeleteIData();
175         break;
176     }
177     return TRUE;
178 }
179
180 /**************************************************************************
181  *      Mixer devices. New to Win95
182  */
183
184 /**************************************************************************
185  * find out the real mixer ID depending on hmix (depends on dwFlags)
186  */
187 static UINT MIXER_GetDev(HMIXEROBJ hmix, DWORD dwFlags, LPWINE_MIXER * lplpwm)
188 {
189     LPWINE_MIXER        lpwm = NULL;
190     UINT                uRet = MMSYSERR_NOERROR;
191
192     switch (dwFlags & 0xF0000000ul) {
193     case MIXER_OBJECTF_MIXER:
194         lpwm = (LPWINE_MIXER)MMDRV_Get(hmix, MMDRV_MIXER, TRUE);
195         break;
196     case MIXER_OBJECTF_HMIXER:
197         lpwm = (LPWINE_MIXER)MMDRV_Get(hmix, MMDRV_MIXER, FALSE);
198         break;
199     case MIXER_OBJECTF_WAVEOUT:
200         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEOUT, TRUE,  MMDRV_MIXER);
201         break;
202     case MIXER_OBJECTF_HWAVEOUT:
203         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEOUT, FALSE, MMDRV_MIXER);
204         break;
205     case MIXER_OBJECTF_WAVEIN:
206         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEIN,  TRUE,  MMDRV_MIXER);
207         break;
208     case MIXER_OBJECTF_HWAVEIN:
209         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEIN,  FALSE, MMDRV_MIXER);
210         break;
211     case MIXER_OBJECTF_MIDIOUT:
212         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIOUT, TRUE,  MMDRV_MIXER);
213         break;
214     case MIXER_OBJECTF_HMIDIOUT:
215         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIOUT, FALSE, MMDRV_MIXER);
216         break;
217     case MIXER_OBJECTF_MIDIIN:
218         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIIN,  TRUE,  MMDRV_MIXER);
219         break;
220     case MIXER_OBJECTF_HMIDIIN:
221         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIIN,  FALSE, MMDRV_MIXER);
222         break;
223     case MIXER_OBJECTF_AUX:
224         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_AUX,     TRUE,  MMDRV_MIXER);
225         break;
226     default:
227         WARN("Unsupported flag (%08lx)\n", dwFlags & 0xF0000000ul);
228         lpwm = 0;
229         uRet = MMSYSERR_INVALFLAG;
230         break;
231     }
232     *lplpwm = lpwm;
233     if (lpwm == 0 && uRet == MMSYSERR_NOERROR)
234         uRet = MMSYSERR_INVALPARAM;
235     return uRet;
236 }
237
238 /**************************************************************************
239  *                              mixerGetNumDevs                 [WINMM.@]
240  */
241 UINT WINAPI mixerGetNumDevs(void)
242 {
243     return MMDRV_GetNum(MMDRV_MIXER);
244 }
245
246 /**************************************************************************
247  *                              mixerGetDevCapsA                [WINMM.@]
248  */
249 UINT WINAPI mixerGetDevCapsA(UINT_PTR uDeviceID, LPMIXERCAPSA lpCaps, UINT uSize)
250 {
251     MIXERCAPSW micW;
252     UINT       ret;
253
254     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
255
256     ret = mixerGetDevCapsW(uDeviceID, &micW, sizeof(micW));
257
258     if (ret == MMSYSERR_NOERROR) {
259         MIXERCAPSA micA;
260         micA.wMid           = micW.wMid;
261         micA.wPid           = micW.wPid;
262         micA.vDriverVersion = micW.vDriverVersion;
263         WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
264                              sizeof(micA.szPname), NULL, NULL );
265         micA.fdwSupport     = micW.fdwSupport;
266         micA.cDestinations  = micW.cDestinations;
267         memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
268     }
269     return ret;
270 }
271
272 /**************************************************************************
273  *                              mixerGetDevCapsW                [WINMM.@]
274  */
275 UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
276 {
277     LPWINE_MLD wmld;
278
279     if (lpCaps == NULL)        return MMSYSERR_INVALPARAM;
280
281     if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIXER, TRUE)) == NULL)
282         return MMSYSERR_BADDEVICEID;
283
284     return MMDRV_Message(wmld, MXDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
285 }
286
287 static void CALLBACK MIXER_WCallback(HMIXEROBJ hmx, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam, DWORD_PTR param2)
288 {
289     HWND hWnd = (HWND)dwInstance;
290
291     if (!dwInstance)
292         return;
293
294     PostMessageW(hWnd, uMsg, (WPARAM)hmx, (LPARAM)dwParam);
295 }
296
297 /**************************************************************************
298  *                              mixerOpen                       [WINMM.@]
299  */
300 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD_PTR dwCallback,
301                       DWORD_PTR dwInstance, DWORD fdwOpen)
302 {
303     HANDLE              hMix;
304     LPWINE_MLD          wmld;
305     DWORD               dwRet = 0;
306     MIXEROPENDESC       mod;
307
308     TRACE("(%p, %d, %08lx, %08lx, %08x)\n",
309           lphMix, uDeviceID, dwCallback, dwInstance, fdwOpen);
310
311     mod.dwCallback = (DWORD_PTR)MIXER_WCallback;
312     mod.dwInstance = 0;
313
314 /* If callback is a function,
315  * dwCallback contains function pointer
316  * dwInstance private data
317  *
318  * if callback is a window
319  * dwCallback contains a window handle
320  */
321     switch (fdwOpen & CALLBACK_TYPEMASK) {
322     default:
323         return MMSYSERR_INVALFLAG;
324
325     case CALLBACK_NULL:
326         break;
327
328     case CALLBACK_WINDOW:
329         mod.dwInstance = dwCallback;
330         if (dwCallback && !IsWindow((HWND)dwCallback))
331             return MMSYSERR_INVALPARAM;
332         break;
333     }
334
335     wmld = MMDRV_Alloc(sizeof(WINE_MIXER), MMDRV_MIXER, &hMix, &fdwOpen,
336                        &dwCallback, &dwInstance);
337     wmld->uDeviceID = uDeviceID;
338     mod.hmx = hMix;
339
340     dwRet = MMDRV_Open(wmld, MXDM_OPEN, (DWORD_PTR)&mod, CALLBACK_FUNCTION);
341
342     if (dwRet != MMSYSERR_NOERROR) {
343         MMDRV_Free(hMix, wmld);
344         hMix = 0;
345     }
346     if (lphMix) *lphMix = hMix;
347     TRACE("=> %d hMixer=%p\n", dwRet, hMix);
348
349     return dwRet;
350 }
351
352 /**************************************************************************
353  *                              mixerClose                      [WINMM.@]
354  */
355 UINT WINAPI mixerClose(HMIXER hMix)
356 {
357     LPWINE_MLD          wmld;
358     DWORD               dwRet;
359
360     TRACE("(%p)\n", hMix);
361
362     if ((wmld = MMDRV_Get(hMix, MMDRV_MIXER, FALSE)) == NULL) return MMSYSERR_INVALHANDLE;
363
364     dwRet = MMDRV_Close(wmld, MXDM_CLOSE);
365     MMDRV_Free(hMix, wmld);
366
367     return dwRet;
368 }
369
370 /**************************************************************************
371  *                              mixerGetID                      [WINMM.@]
372  */
373 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
374 {
375     LPWINE_MIXER        lpwm;
376     UINT                uRet = MMSYSERR_NOERROR;
377
378     TRACE("(%p %p %08x)\n", hmix, lpid, fdwID);
379
380     if ((uRet = MIXER_GetDev(hmix, fdwID, &lpwm)) != MMSYSERR_NOERROR)
381         return uRet;
382
383     if (lpid)
384       *lpid = lpwm->mld.uDeviceID;
385
386     return uRet;
387 }
388
389 /**************************************************************************
390  *                              mixerGetControlDetailsW         [WINMM.@]
391  */
392 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW,
393                                     DWORD fdwDetails)
394 {
395     LPWINE_MIXER        lpwm;
396     UINT                uRet = MMSYSERR_NOERROR;
397
398     TRACE("(%p, %p, %08x)\n", hmix, lpmcdW, fdwDetails);
399
400     if ((uRet = MIXER_GetDev(hmix, fdwDetails, &lpwm)) != MMSYSERR_NOERROR)
401         return uRet;
402
403     if (lpmcdW == NULL || lpmcdW->cbStruct != sizeof(*lpmcdW))
404         return MMSYSERR_INVALPARAM;
405
406     return MMDRV_Message(&lpwm->mld, MXDM_GETCONTROLDETAILS, (DWORD_PTR)lpmcdW,
407                          fdwDetails);
408 }
409
410 /**************************************************************************
411  *                              mixerGetControlDetailsA [WINMM.@]
412  */
413 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
414                                     DWORD fdwDetails)
415 {
416     DWORD                       ret = MMSYSERR_NOTENABLED;
417
418     TRACE("(%p, %p, %08x)\n", hmix, lpmcdA, fdwDetails);
419
420     if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
421         return MMSYSERR_INVALPARAM;
422
423     switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
424     case MIXER_GETCONTROLDETAILSF_VALUE:
425         /* can safely use A structure as it is, no string inside */
426         ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
427         break;
428     case MIXER_GETCONTROLDETAILSF_LISTTEXT:
429         {
430             MIXERCONTROLDETAILS_LISTTEXTA *pDetailsA = lpmcdA->paDetails;
431             MIXERCONTROLDETAILS_LISTTEXTW *pDetailsW;
432             int size = max(1, lpmcdA->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
433             unsigned int i;
434
435             if (lpmcdA->u.cMultipleItems != 0) {
436                 size *= lpmcdA->u.cMultipleItems;
437             }
438             pDetailsW = HeapAlloc(GetProcessHeap(), 0, size);
439             lpmcdA->paDetails = pDetailsW;
440             lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTW);
441             /* set up lpmcd->paDetails */
442             ret = mixerGetControlDetailsW(hmix, lpmcdA, fdwDetails);
443             /* copy from lpmcd->paDetails back to paDetailsW; */
444             if (ret == MMSYSERR_NOERROR) {
445                 for (i = 0; i < lpmcdA->u.cMultipleItems * lpmcdA->cChannels; i++) {
446                     pDetailsA->dwParam1 = pDetailsW->dwParam1;
447                     pDetailsA->dwParam2 = pDetailsW->dwParam2;
448                     WideCharToMultiByte( CP_ACP, 0, pDetailsW->szName, -1,
449                                          pDetailsA->szName,
450                                          sizeof(pDetailsA->szName), NULL, NULL );
451                     pDetailsA++;
452                     pDetailsW++;
453                 }
454                 pDetailsA -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
455                 pDetailsW -= lpmcdA->u.cMultipleItems * lpmcdA->cChannels;
456             }
457             HeapFree(GetProcessHeap(), 0, pDetailsW);
458             lpmcdA->paDetails = pDetailsA;
459             lpmcdA->cbDetails = sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
460         }
461         break;
462     default:
463         ERR("Unsupported fdwDetails=0x%08x\n", fdwDetails);
464     }
465
466     return ret;
467 }
468
469 /**************************************************************************
470  *                              mixerGetLineControlsA   [WINMM.@]
471  */
472 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA,
473                                   DWORD fdwControls)
474 {
475     MIXERLINECONTROLSW  mlcW;
476     DWORD               ret;
477     unsigned int        i;
478
479     TRACE("(%p, %p, %08x)\n", hmix, lpmlcA, fdwControls);
480
481     if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA) ||
482         lpmlcA->cbmxctrl != sizeof(MIXERCONTROLA))
483         return MMSYSERR_INVALPARAM;
484
485     mlcW.cbStruct = sizeof(mlcW);
486     mlcW.dwLineID = lpmlcA->dwLineID;
487     mlcW.u.dwControlID = lpmlcA->u.dwControlID;
488     mlcW.u.dwControlType = lpmlcA->u.dwControlType;
489
490     /* Debugging on Windows shows for MIXER_GETLINECONTROLSF_ONEBYTYPE only,
491        the control count is assumed to be 1 - This is relied upon by a game,
492        "Dynomite Deluze"                                                    */
493     if (MIXER_GETLINECONTROLSF_ONEBYTYPE == (fdwControls & MIXER_GETLINECONTROLSF_QUERYMASK)) {
494         mlcW.cControls = 1;
495     } else {
496         mlcW.cControls = lpmlcA->cControls;
497     }
498     mlcW.cbmxctrl = sizeof(MIXERCONTROLW);
499     mlcW.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
500                               mlcW.cControls * mlcW.cbmxctrl);
501
502     ret = mixerGetLineControlsW(hmix, &mlcW, fdwControls);
503
504     if (ret == MMSYSERR_NOERROR) {
505         lpmlcA->dwLineID = mlcW.dwLineID;
506         lpmlcA->u.dwControlID = mlcW.u.dwControlID;
507         lpmlcA->u.dwControlType = mlcW.u.dwControlType;
508
509         for (i = 0; i < mlcW.cControls; i++) {
510             lpmlcA->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLA);
511             lpmlcA->pamxctrl[i].dwControlID = mlcW.pamxctrl[i].dwControlID;
512             lpmlcA->pamxctrl[i].dwControlType = mlcW.pamxctrl[i].dwControlType;
513             lpmlcA->pamxctrl[i].fdwControl = mlcW.pamxctrl[i].fdwControl;
514             lpmlcA->pamxctrl[i].cMultipleItems = mlcW.pamxctrl[i].cMultipleItems;
515             WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szShortName, -1,
516                                  lpmlcA->pamxctrl[i].szShortName,
517                                  sizeof(lpmlcA->pamxctrl[i].szShortName), NULL, NULL );
518             WideCharToMultiByte( CP_ACP, 0, mlcW.pamxctrl[i].szName, -1,
519                                  lpmlcA->pamxctrl[i].szName,
520                                  sizeof(lpmlcA->pamxctrl[i].szName), NULL, NULL );
521             /* sizeof(lpmlcA->pamxctrl[i].Bounds) ==
522              * sizeof(mlcW.pamxctrl[i].Bounds) */
523             memcpy(&lpmlcA->pamxctrl[i].Bounds, &mlcW.pamxctrl[i].Bounds,
524                    sizeof(mlcW.pamxctrl[i].Bounds));
525             /* sizeof(lpmlcA->pamxctrl[i].Metrics) ==
526              * sizeof(mlcW.pamxctrl[i].Metrics) */
527             memcpy(&lpmlcA->pamxctrl[i].Metrics, &mlcW.pamxctrl[i].Metrics,
528                    sizeof(mlcW.pamxctrl[i].Metrics));
529         }
530     }
531
532     HeapFree(GetProcessHeap(), 0, mlcW.pamxctrl);
533
534     return ret;
535 }
536
537 /**************************************************************************
538  *                              mixerGetLineControlsW           [WINMM.@]
539  */
540 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW,
541                                   DWORD fdwControls)
542 {
543     LPWINE_MIXER        lpwm;
544     UINT                uRet = MMSYSERR_NOERROR;
545
546     TRACE("(%p, %p, %08x)\n", hmix, lpmlcW, fdwControls);
547
548     if ((uRet = MIXER_GetDev(hmix, fdwControls, &lpwm)) != MMSYSERR_NOERROR)
549         return uRet;
550
551     if (lpmlcW == NULL || lpmlcW->cbStruct != sizeof(*lpmlcW))
552         return MMSYSERR_INVALPARAM;
553
554     return MMDRV_Message(&lpwm->mld, MXDM_GETLINECONTROLS, (DWORD_PTR)lpmlcW,
555                          fdwControls);
556 }
557
558 /**************************************************************************
559  *                              mixerGetLineInfoW               [WINMM.@]
560  */
561 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
562 {
563     LPWINE_MIXER        lpwm;
564     UINT                uRet = MMSYSERR_NOERROR;
565
566     TRACE("(%p, %p, %08x)\n", hmix, lpmliW, fdwInfo);
567
568     if ((uRet = MIXER_GetDev(hmix, fdwInfo, &lpwm)) != MMSYSERR_NOERROR)
569         return uRet;
570
571     return MMDRV_Message(&lpwm->mld, MXDM_GETLINEINFO, (DWORD_PTR)lpmliW,
572                          fdwInfo);
573 }
574
575 /**************************************************************************
576  *                              mixerGetLineInfoA               [WINMM.@]
577  */
578 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliA,
579                               DWORD fdwInfo)
580 {
581     MIXERLINEW          mliW;
582     UINT                ret;
583
584     TRACE("(%p, %p, %08x)\n", hmix, lpmliA, fdwInfo);
585
586     if (lpmliA == NULL || lpmliA->cbStruct != sizeof(*lpmliA))
587         return MMSYSERR_INVALPARAM;
588
589     mliW.cbStruct = sizeof(mliW);
590     switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
591     case MIXER_GETLINEINFOF_COMPONENTTYPE:
592         mliW.dwComponentType = lpmliA->dwComponentType;
593         break;
594     case MIXER_GETLINEINFOF_DESTINATION:
595         mliW.dwDestination = lpmliA->dwDestination;
596         break;
597     case MIXER_GETLINEINFOF_LINEID:
598         mliW.dwLineID = lpmliA->dwLineID;
599         break;
600     case MIXER_GETLINEINFOF_SOURCE:
601         mliW.dwDestination = lpmliA->dwDestination;
602         mliW.dwSource = lpmliA->dwSource;
603         break;
604     case MIXER_GETLINEINFOF_TARGETTYPE:
605         mliW.Target.dwType = lpmliA->Target.dwType;
606         mliW.Target.wMid = lpmliA->Target.wMid;
607         mliW.Target.wPid = lpmliA->Target.wPid;
608         mliW.Target.vDriverVersion = lpmliA->Target.vDriverVersion;
609         MultiByteToWideChar( CP_ACP, 0, lpmliA->Target.szPname, -1, mliW.Target.szPname, sizeof(mliW.Target.szPname)/sizeof(WCHAR));
610         break;
611     default:
612         WARN("Unsupported fdwControls=0x%08x\n", fdwInfo);
613         return MMSYSERR_INVALFLAG;
614     }
615
616     ret = mixerGetLineInfoW(hmix, &mliW, fdwInfo);
617
618     if(ret == MMSYSERR_NOERROR)
619     {
620         lpmliA->dwDestination = mliW.dwDestination;
621         lpmliA->dwSource = mliW.dwSource;
622         lpmliA->dwLineID = mliW.dwLineID;
623         lpmliA->fdwLine = mliW.fdwLine;
624         lpmliA->dwUser = mliW.dwUser;
625         lpmliA->dwComponentType = mliW.dwComponentType;
626         lpmliA->cChannels = mliW.cChannels;
627         lpmliA->cConnections = mliW.cConnections;
628         lpmliA->cControls = mliW.cControls;
629         WideCharToMultiByte( CP_ACP, 0, mliW.szShortName, -1, lpmliA->szShortName,
630                              sizeof(lpmliA->szShortName), NULL, NULL);
631         WideCharToMultiByte( CP_ACP, 0, mliW.szName, -1, lpmliA->szName,
632                              sizeof(lpmliA->szName), NULL, NULL );
633         lpmliA->Target.dwType = mliW.Target.dwType;
634         lpmliA->Target.dwDeviceID = mliW.Target.dwDeviceID;
635         lpmliA->Target.wMid = mliW.Target.wMid;
636         lpmliA->Target.wPid = mliW.Target.wPid;
637         lpmliA->Target.vDriverVersion = mliW.Target.vDriverVersion;
638         WideCharToMultiByte( CP_ACP, 0, mliW.Target.szPname, -1, lpmliA->Target.szPname,
639                              sizeof(lpmliA->Target.szPname), NULL, NULL );
640     }
641     return ret;
642 }
643
644 /**************************************************************************
645  *                              mixerSetControlDetails  [WINMM.@]
646  */
647 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd,
648                                    DWORD fdwDetails)
649 {
650     LPWINE_MIXER        lpwm;
651     UINT                uRet = MMSYSERR_NOERROR;
652
653     TRACE("(%p, %p, %08x)\n", hmix, lpmcd, fdwDetails);
654
655     if ((uRet = MIXER_GetDev(hmix, fdwDetails, &lpwm)) != MMSYSERR_NOERROR)
656         return uRet;
657
658     return MMDRV_Message(&lpwm->mld, MXDM_SETCONTROLDETAILS, (DWORD_PTR)lpmcd,
659                          fdwDetails);
660 }
661
662 /**************************************************************************
663  *                              mixerMessage            [WINMM.@]
664  */
665 DWORD WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
666 {
667     LPWINE_MLD          wmld;
668
669     TRACE("(%p, %d, %08lx, %08lx): semi-stub?\n",
670           hmix, uMsg, dwParam1, dwParam2);
671
672     if ((wmld = MMDRV_Get(hmix, MMDRV_MIXER, FALSE)) == NULL)
673         return MMSYSERR_INVALHANDLE;
674
675     return MMDRV_Message(wmld, uMsg, dwParam1, dwParam2);
676 }
677
678 /**************************************************************************
679  *                              auxGetNumDevs           [WINMM.@]
680  */
681 UINT WINAPI auxGetNumDevs(void)
682 {
683     return MMDRV_GetNum(MMDRV_AUX);
684 }
685
686 /**************************************************************************
687  *                              auxGetDevCapsW          [WINMM.@]
688  */
689 UINT WINAPI auxGetDevCapsW(UINT_PTR uDeviceID, LPAUXCAPSW lpCaps, UINT uSize)
690 {
691     LPWINE_MLD          wmld;
692
693     TRACE("(%04lX, %p, %d) !\n", uDeviceID, lpCaps, uSize);
694
695     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
696
697     if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
698         return MMSYSERR_INVALHANDLE;
699     return MMDRV_Message(wmld, AUXDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
700 }
701
702 /**************************************************************************
703  *                              auxGetDevCapsA          [WINMM.@]
704  */
705 UINT WINAPI auxGetDevCapsA(UINT_PTR uDeviceID, LPAUXCAPSA lpCaps, UINT uSize)
706 {
707     AUXCAPSW    acW;
708     UINT        ret;
709
710     if (lpCaps == NULL)        return MMSYSERR_INVALPARAM;
711
712     ret = auxGetDevCapsW(uDeviceID, &acW, sizeof(acW));
713
714     if (ret == MMSYSERR_NOERROR) {
715         AUXCAPSA acA;
716         acA.wMid           = acW.wMid;
717         acA.wPid           = acW.wPid;
718         acA.vDriverVersion = acW.vDriverVersion;
719         WideCharToMultiByte( CP_ACP, 0, acW.szPname, -1, acA.szPname,
720                              sizeof(acA.szPname), NULL, NULL );
721         acA.wTechnology    = acW.wTechnology;
722         acA.dwSupport      = acW.dwSupport;
723         memcpy(lpCaps, &acA, min(uSize, sizeof(acA)));
724     }
725     return ret;
726 }
727
728 /**************************************************************************
729  *                              auxGetVolume            [WINMM.@]
730  */
731 UINT WINAPI auxGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
732 {
733     LPWINE_MLD          wmld;
734
735     TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);
736
737     if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
738         return MMSYSERR_INVALHANDLE;
739     return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
740 }
741
742 /**************************************************************************
743  *                              auxSetVolume            [WINMM.@]
744  */
745 UINT WINAPI auxSetVolume(UINT uDeviceID, DWORD dwVolume)
746 {
747     LPWINE_MLD          wmld;
748
749     TRACE("(%04X, %u) !\n", uDeviceID, dwVolume);
750
751     if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
752         return MMSYSERR_INVALHANDLE;
753     return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L);
754 }
755
756 /**************************************************************************
757  *                              auxOutMessage           [WINMM.@]
758  */
759 UINT WINAPI auxOutMessage(UINT uDeviceID, UINT uMessage, DWORD_PTR dw1, DWORD_PTR dw2)
760 {
761     LPWINE_MLD          wmld;
762
763     if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
764         return MMSYSERR_INVALHANDLE;
765
766     return MMDRV_Message(wmld, uMessage, dw1, dw2);
767 }
768
769 /**************************************************************************
770  *                              midiOutGetNumDevs       [WINMM.@]
771  */
772 UINT WINAPI midiOutGetNumDevs(void)
773 {
774     return MMDRV_GetNum(MMDRV_MIDIOUT);
775 }
776
777 /**************************************************************************
778  *                              midiOutGetDevCapsW      [WINMM.@]
779  */
780 UINT WINAPI midiOutGetDevCapsW(UINT_PTR uDeviceID, LPMIDIOUTCAPSW lpCaps,
781                                UINT uSize)
782 {
783     LPWINE_MLD  wmld;
784
785     TRACE("(%lu, %p, %u);\n", uDeviceID, lpCaps, uSize);
786
787     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
788
789     if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL)
790         return MMSYSERR_INVALHANDLE;
791
792     return MMDRV_Message(wmld, MODM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
793 }
794
795 /**************************************************************************
796  *                              midiOutGetDevCapsA      [WINMM.@]
797  */
798 UINT WINAPI midiOutGetDevCapsA(UINT_PTR uDeviceID, LPMIDIOUTCAPSA lpCaps,
799                                UINT uSize)
800 {
801     MIDIOUTCAPSW        mocW;
802     UINT                ret;
803
804     if (lpCaps == NULL)        return MMSYSERR_INVALPARAM;
805
806     ret = midiOutGetDevCapsW(uDeviceID, &mocW, sizeof(mocW));
807
808     if (ret == MMSYSERR_NOERROR) {
809         MIDIOUTCAPSA mocA;
810         mocA.wMid               = mocW.wMid;
811         mocA.wPid               = mocW.wPid;
812         mocA.vDriverVersion     = mocW.vDriverVersion;
813         WideCharToMultiByte( CP_ACP, 0, mocW.szPname, -1, mocA.szPname,
814                              sizeof(mocA.szPname), NULL, NULL );
815         mocA.wTechnology        = mocW.wTechnology;
816         mocA.wVoices            = mocW.wVoices;
817         mocA.wNotes             = mocW.wNotes;
818         mocA.wChannelMask       = mocW.wChannelMask;
819         mocA.dwSupport          = mocW.dwSupport;
820         memcpy(lpCaps, &mocA, min(uSize, sizeof(mocA)));
821     }
822     return ret;
823 }
824
825 /**************************************************************************
826  *                              midiOutGetErrorTextA    [WINMM.@]
827  *                              midiInGetErrorTextA     [WINMM.@]
828  */
829 UINT WINAPI midiOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
830 {
831     UINT        ret;
832
833     if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
834     else if (uSize == 0) ret = MMSYSERR_NOERROR;
835     else
836     {
837         LPWSTR  xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
838         if (!xstr) ret = MMSYSERR_NOMEM;
839         else
840         {
841             ret = midiOutGetErrorTextW(uError, xstr, uSize);
842             if (ret == MMSYSERR_NOERROR)
843                 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
844             HeapFree(GetProcessHeap(), 0, xstr);
845         }
846     }
847     return ret;
848 }
849
850 /**************************************************************************
851  *                              midiOutGetErrorTextW    [WINMM.@]
852  *                              midiInGetErrorTextW     [WINMM.@]
853  */
854 UINT WINAPI midiOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
855 {
856     UINT        ret = MMSYSERR_BADERRNUM;
857
858     if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
859     else if (uSize == 0) ret = MMSYSERR_NOERROR;
860     else if (
861                /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
862                 * a warning for the test was always true */
863                (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
864                (uError >= MIDIERR_BASE  && uError <= MIDIERR_LASTERROR)) {
865         if (LoadStringW(hWinMM32Instance, uError, lpText, uSize) > 0) {
866             ret = MMSYSERR_NOERROR;
867         }
868     }
869     return ret;
870 }
871
872 /**************************************************************************
873  *                              MIDI_OutAlloc                   [internal]
874  */
875 static  LPWINE_MIDI     MIDI_OutAlloc(HMIDIOUT* lphMidiOut, DWORD_PTR* lpdwCallback,
876                                       DWORD_PTR* lpdwInstance, LPDWORD lpdwFlags,
877                                       DWORD cIDs, MIDIOPENSTRMID* lpIDs)
878 {
879     HANDLE              hMidiOut;
880     LPWINE_MIDI         lpwm;
881     UINT                size;
882
883     size = sizeof(WINE_MIDI) + (cIDs ? (cIDs-1) : 0) * sizeof(MIDIOPENSTRMID);
884
885     lpwm = (LPWINE_MIDI)MMDRV_Alloc(size, MMDRV_MIDIOUT, &hMidiOut, lpdwFlags,
886                                     lpdwCallback, lpdwInstance);
887
888     if (lphMidiOut != NULL)
889         *lphMidiOut = hMidiOut;
890
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));
899     }
900     return lpwm;
901 }
902
903 /**************************************************************************
904  *                              midiOutOpen             [WINMM.@]
905  */
906 UINT WINAPI midiOutOpen(LPHMIDIOUT lphMidiOut, UINT uDeviceID,
907                        DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
908 {
909     HMIDIOUT            hMidiOut;
910     LPWINE_MIDI         lpwm;
911     UINT                dwRet = 0;
912
913     TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
914           lphMidiOut, uDeviceID, dwCallback, dwInstance, dwFlags);
915
916     if (lphMidiOut != NULL) *lphMidiOut = 0;
917
918     lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &dwFlags, 0, NULL);
919
920     if (lpwm == NULL)
921         return MMSYSERR_NOMEM;
922
923     lpwm->mld.uDeviceID = uDeviceID;
924
925     dwRet = MMDRV_Open((LPWINE_MLD)lpwm, MODM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
926
927     if (dwRet != MMSYSERR_NOERROR) {
928         MMDRV_Free(hMidiOut, (LPWINE_MLD)lpwm);
929         hMidiOut = 0;
930     }
931
932     if (lphMidiOut) *lphMidiOut = hMidiOut;
933     TRACE("=> %d hMidi=%p\n", dwRet, hMidiOut);
934
935     return dwRet;
936 }
937
938 /**************************************************************************
939  *                              midiOutClose            [WINMM.@]
940  */
941 UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
942 {
943     LPWINE_MLD          wmld;
944     DWORD               dwRet;
945
946     TRACE("(%p)\n", hMidiOut);
947
948     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
949         return MMSYSERR_INVALHANDLE;
950
951     dwRet = MMDRV_Close(wmld, MODM_CLOSE);
952     MMDRV_Free(hMidiOut, wmld);
953
954     return dwRet;
955 }
956
957 /**************************************************************************
958  *                              midiOutPrepareHeader    [WINMM.@]
959  */
960 UINT WINAPI midiOutPrepareHeader(HMIDIOUT hMidiOut,
961                                  MIDIHDR* lpMidiOutHdr, UINT uSize)
962 {
963     LPWINE_MLD          wmld;
964
965     TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
966
967     if (lpMidiOutHdr == NULL || uSize < sizeof (MIDIHDR))
968         return MMSYSERR_INVALPARAM;
969
970     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
971         return MMSYSERR_INVALHANDLE;
972
973     return MMDRV_Message(wmld, MODM_PREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
974 }
975
976 /**************************************************************************
977  *                              midiOutUnprepareHeader  [WINMM.@]
978  */
979 UINT WINAPI midiOutUnprepareHeader(HMIDIOUT hMidiOut,
980                                    MIDIHDR* lpMidiOutHdr, UINT uSize)
981 {
982     LPWINE_MLD          wmld;
983
984     TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
985
986     if (lpMidiOutHdr == NULL || uSize < sizeof (MIDIHDR))
987         return MMSYSERR_INVALPARAM;
988
989     if (!(lpMidiOutHdr->dwFlags & MHDR_PREPARED)) {
990         return MMSYSERR_NOERROR;
991     }
992
993     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
994         return MMSYSERR_INVALHANDLE;
995
996     return MMDRV_Message(wmld, MODM_UNPREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
997 }
998
999 /**************************************************************************
1000  *                              midiOutShortMsg         [WINMM.@]
1001  */
1002 UINT WINAPI midiOutShortMsg(HMIDIOUT hMidiOut, DWORD dwMsg)
1003 {
1004     LPWINE_MLD          wmld;
1005
1006     TRACE("(%p, %08X)\n", hMidiOut, dwMsg);
1007
1008     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
1009         return MMSYSERR_INVALHANDLE;
1010
1011     return MMDRV_Message(wmld, MODM_DATA, dwMsg, 0L);
1012 }
1013
1014 /**************************************************************************
1015  *                              midiOutLongMsg          [WINMM.@]
1016  */
1017 UINT WINAPI midiOutLongMsg(HMIDIOUT hMidiOut,
1018                            MIDIHDR* lpMidiOutHdr, UINT uSize)
1019 {
1020     LPWINE_MLD          wmld;
1021
1022     TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
1023
1024     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
1025         return MMSYSERR_INVALHANDLE;
1026
1027     return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD_PTR)lpMidiOutHdr, uSize);
1028 }
1029
1030 /**************************************************************************
1031  *                              midiOutReset            [WINMM.@]
1032  */
1033 UINT WINAPI midiOutReset(HMIDIOUT hMidiOut)
1034 {
1035     LPWINE_MLD          wmld;
1036
1037     TRACE("(%p)\n", hMidiOut);
1038
1039     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
1040         return MMSYSERR_INVALHANDLE;
1041
1042     return MMDRV_Message(wmld, MODM_RESET, 0L, 0L);
1043 }
1044
1045 /**************************************************************************
1046  *                              midiOutGetVolume        [WINMM.@]
1047  */
1048 UINT WINAPI midiOutGetVolume(HMIDIOUT hMidiOut, DWORD* lpdwVolume)
1049 {
1050     LPWINE_MLD          wmld;
1051
1052     TRACE("(%p, %p);\n", hMidiOut, lpdwVolume);
1053
1054     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
1055         return MMSYSERR_INVALHANDLE;
1056
1057     return MMDRV_Message(wmld, MODM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
1058 }
1059
1060 /**************************************************************************
1061  *                              midiOutSetVolume        [WINMM.@]
1062  */
1063 UINT WINAPI midiOutSetVolume(HMIDIOUT hMidiOut, DWORD dwVolume)
1064 {
1065     LPWINE_MLD          wmld;
1066
1067     TRACE("(%p, %d);\n", hMidiOut, dwVolume);
1068
1069     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
1070         return MMSYSERR_INVALHANDLE;
1071
1072     return MMDRV_Message(wmld, MODM_SETVOLUME, dwVolume, 0L);
1073 }
1074
1075 /**************************************************************************
1076  *                              midiOutCachePatches             [WINMM.@]
1077  */
1078 UINT WINAPI midiOutCachePatches(HMIDIOUT hMidiOut, UINT uBank,
1079                                 WORD* lpwPatchArray, UINT uFlags)
1080 {
1081     /* not really necessary to support this */
1082     FIXME("not supported yet\n");
1083     return MMSYSERR_NOTSUPPORTED;
1084 }
1085
1086 /**************************************************************************
1087  *                              midiOutCacheDrumPatches [WINMM.@]
1088  */
1089 UINT WINAPI midiOutCacheDrumPatches(HMIDIOUT hMidiOut, UINT uPatch,
1090                                     WORD* lpwKeyArray, UINT uFlags)
1091 {
1092     FIXME("not supported yet\n");
1093     return MMSYSERR_NOTSUPPORTED;
1094 }
1095
1096 /**************************************************************************
1097  *                              midiOutGetID            [WINMM.@]
1098  */
1099 UINT WINAPI midiOutGetID(HMIDIOUT hMidiOut, UINT* lpuDeviceID)
1100 {
1101     LPWINE_MLD          wmld;
1102
1103     TRACE("(%p, %p)\n", hMidiOut, lpuDeviceID);
1104
1105     if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
1106     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
1107         return MMSYSERR_INVALHANDLE;
1108
1109     *lpuDeviceID = wmld->uDeviceID;
1110     return MMSYSERR_NOERROR;
1111 }
1112
1113 /**************************************************************************
1114  *                              midiOutMessage          [WINMM.@]
1115  */
1116 UINT WINAPI midiOutMessage(HMIDIOUT hMidiOut, UINT uMessage,
1117                            DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1118 {
1119     LPWINE_MLD          wmld;
1120
1121     TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
1122
1123     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) {
1124         /* HACK... */
1125         if (uMessage == 0x0001) {
1126             *(LPDWORD)dwParam1 = 1;
1127             return 0;
1128         }
1129         if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) != NULL) {
1130             return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
1131         }
1132         return MMSYSERR_INVALHANDLE;
1133     }
1134
1135     switch (uMessage) {
1136     case MODM_OPEN:
1137     case MODM_CLOSE:
1138         FIXME("can't handle OPEN or CLOSE message!\n");
1139         return MMSYSERR_NOTSUPPORTED;
1140     }
1141     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
1142 }
1143
1144 /**************************************************************************
1145  *                              midiInGetNumDevs        [WINMM.@]
1146  */
1147 UINT WINAPI midiInGetNumDevs(void)
1148 {
1149     return MMDRV_GetNum(MMDRV_MIDIIN);
1150 }
1151
1152 /**************************************************************************
1153  *                              midiInGetDevCapsW       [WINMM.@]
1154  */
1155 UINT WINAPI midiInGetDevCapsW(UINT_PTR uDeviceID, LPMIDIINCAPSW lpCaps, UINT uSize)
1156 {
1157     LPWINE_MLD  wmld;
1158
1159     TRACE("(%ld, %p, %d);\n", uDeviceID, lpCaps, uSize);
1160
1161     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
1162
1163     if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIIN, TRUE)) == NULL)
1164         return MMSYSERR_INVALHANDLE;
1165
1166    return MMDRV_Message(wmld, MIDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
1167 }
1168
1169 /**************************************************************************
1170  *                              midiInGetDevCapsA       [WINMM.@]
1171  */
1172 UINT WINAPI midiInGetDevCapsA(UINT_PTR uDeviceID, LPMIDIINCAPSA lpCaps, UINT uSize)
1173 {
1174     MIDIINCAPSW         micW;
1175     UINT                ret;
1176
1177     if (lpCaps == NULL)        return MMSYSERR_INVALPARAM;
1178
1179     ret = midiInGetDevCapsW(uDeviceID, &micW, sizeof(micW));
1180
1181     if (ret == MMSYSERR_NOERROR) {
1182         MIDIINCAPSA micA;
1183         micA.wMid           = micW.wMid;
1184         micA.wPid           = micW.wPid;
1185         micA.vDriverVersion = micW.vDriverVersion;
1186         WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
1187                              sizeof(micA.szPname), NULL, NULL );
1188         micA.dwSupport      = micW.dwSupport;
1189         memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
1190     }
1191     return ret;
1192 }
1193
1194 /**************************************************************************
1195  *                              midiInOpen              [WINMM.@]
1196  */
1197 UINT WINAPI midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID,
1198                        DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
1199 {
1200     HANDLE              hMidiIn;
1201     LPWINE_MIDI         lpwm;
1202     DWORD               dwRet = 0;
1203
1204     TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
1205           lphMidiIn, uDeviceID, dwCallback, dwInstance, dwFlags);
1206
1207     if (lphMidiIn != NULL) *lphMidiIn = 0;
1208
1209     lpwm = (LPWINE_MIDI)MMDRV_Alloc(sizeof(WINE_MIDI), MMDRV_MIDIIN, &hMidiIn,
1210                                     &dwFlags, &dwCallback, &dwInstance);
1211
1212     if (lpwm == NULL)
1213         return MMSYSERR_NOMEM;
1214
1215     lpwm->mod.hMidi = hMidiIn;
1216     lpwm->mod.dwCallback = dwCallback;
1217     lpwm->mod.dwInstance = dwInstance;
1218
1219     lpwm->mld.uDeviceID = uDeviceID;
1220     dwRet = MMDRV_Open(&lpwm->mld, MIDM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
1221
1222     if (dwRet != MMSYSERR_NOERROR) {
1223         MMDRV_Free(hMidiIn, &lpwm->mld);
1224         hMidiIn = 0;
1225     }
1226     if (lphMidiIn != NULL) *lphMidiIn = hMidiIn;
1227     TRACE("=> %d hMidi=%p\n", dwRet, hMidiIn);
1228
1229     return dwRet;
1230 }
1231
1232 /**************************************************************************
1233  *                              midiInClose             [WINMM.@]
1234  */
1235 UINT WINAPI midiInClose(HMIDIIN hMidiIn)
1236 {
1237     LPWINE_MLD          wmld;
1238     DWORD               dwRet;
1239
1240     TRACE("(%p)\n", hMidiIn);
1241
1242     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1243         return MMSYSERR_INVALHANDLE;
1244
1245     dwRet = MMDRV_Close(wmld, MIDM_CLOSE);
1246     MMDRV_Free(hMidiIn, wmld);
1247     return dwRet;
1248 }
1249
1250 /**************************************************************************
1251  *                              midiInPrepareHeader     [WINMM.@]
1252  */
1253 UINT WINAPI midiInPrepareHeader(HMIDIIN hMidiIn,
1254                                 MIDIHDR* lpMidiInHdr, UINT uSize)
1255 {
1256     LPWINE_MLD          wmld;
1257
1258     TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
1259
1260     if (lpMidiInHdr == NULL || uSize < sizeof (MIDIHDR))
1261         return MMSYSERR_INVALPARAM;
1262
1263     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1264         return MMSYSERR_INVALHANDLE;
1265
1266     return MMDRV_Message(wmld, MIDM_PREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
1267 }
1268
1269 /**************************************************************************
1270  *                              midiInUnprepareHeader   [WINMM.@]
1271  */
1272 UINT WINAPI midiInUnprepareHeader(HMIDIIN hMidiIn,
1273                                   MIDIHDR* lpMidiInHdr, UINT uSize)
1274 {
1275     LPWINE_MLD          wmld;
1276
1277     TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
1278
1279     if (lpMidiInHdr == NULL || uSize < sizeof (MIDIHDR))
1280         return MMSYSERR_INVALPARAM;
1281
1282     if (!(lpMidiInHdr->dwFlags & MHDR_PREPARED)) {
1283         return MMSYSERR_NOERROR;
1284     }
1285
1286     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1287         return MMSYSERR_INVALHANDLE;
1288
1289     return MMDRV_Message(wmld, MIDM_UNPREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
1290 }
1291
1292 /**************************************************************************
1293  *                              midiInAddBuffer         [WINMM.@]
1294  */
1295 UINT WINAPI midiInAddBuffer(HMIDIIN hMidiIn,
1296                             MIDIHDR* lpMidiInHdr, UINT uSize)
1297 {
1298     LPWINE_MLD          wmld;
1299
1300     TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
1301
1302     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1303         return MMSYSERR_INVALHANDLE;
1304
1305     return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD_PTR)lpMidiInHdr, uSize);
1306 }
1307
1308 /**************************************************************************
1309  *                              midiInStart                     [WINMM.@]
1310  */
1311 UINT WINAPI midiInStart(HMIDIIN hMidiIn)
1312 {
1313     LPWINE_MLD          wmld;
1314
1315     TRACE("(%p)\n", hMidiIn);
1316
1317     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1318         return MMSYSERR_INVALHANDLE;
1319
1320     return MMDRV_Message(wmld, MIDM_START, 0L, 0L);
1321 }
1322
1323 /**************************************************************************
1324  *                              midiInStop                      [WINMM.@]
1325  */
1326 UINT WINAPI midiInStop(HMIDIIN hMidiIn)
1327 {
1328     LPWINE_MLD          wmld;
1329
1330     TRACE("(%p)\n", hMidiIn);
1331
1332     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1333         return MMSYSERR_INVALHANDLE;
1334
1335     return MMDRV_Message(wmld, MIDM_STOP, 0L, 0L);
1336 }
1337
1338 /**************************************************************************
1339  *                              midiInReset                     [WINMM.@]
1340  */
1341 UINT WINAPI midiInReset(HMIDIIN hMidiIn)
1342 {
1343     LPWINE_MLD          wmld;
1344
1345     TRACE("(%p)\n", hMidiIn);
1346
1347     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1348         return MMSYSERR_INVALHANDLE;
1349
1350     return MMDRV_Message(wmld, MIDM_RESET, 0L, 0L);
1351 }
1352
1353 /**************************************************************************
1354  *                              midiInGetID                     [WINMM.@]
1355  */
1356 UINT WINAPI midiInGetID(HMIDIIN hMidiIn, UINT* lpuDeviceID)
1357 {
1358     LPWINE_MLD          wmld;
1359
1360     TRACE("(%p, %p)\n", hMidiIn, lpuDeviceID);
1361
1362     if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
1363
1364     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, TRUE)) == NULL)
1365         return MMSYSERR_INVALHANDLE;
1366
1367     *lpuDeviceID = wmld->uDeviceID;
1368
1369     return MMSYSERR_NOERROR;
1370 }
1371
1372 /**************************************************************************
1373  *                              midiInMessage           [WINMM.@]
1374  */
1375 UINT WINAPI midiInMessage(HMIDIIN hMidiIn, UINT uMessage,
1376                           DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1377 {
1378     LPWINE_MLD          wmld;
1379
1380     TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiIn, uMessage, dwParam1, dwParam2);
1381
1382     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
1383         return MMSYSERR_INVALHANDLE;
1384
1385     switch (uMessage) {
1386     case MIDM_OPEN:
1387     case MIDM_CLOSE:
1388         FIXME("can't handle OPEN or CLOSE message!\n");
1389         return MMSYSERR_NOTSUPPORTED;
1390     }
1391     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
1392 }
1393
1394 /**************************************************************************
1395  *                              midiConnect                     [WINMM.@]
1396  */
1397 MMRESULT WINAPI midiConnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
1398 {
1399     FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
1400     return MMSYSERR_ERROR;
1401 }
1402
1403 /**************************************************************************
1404  *                              midiDisconnect                  [WINMM.@]
1405  */
1406 MMRESULT WINAPI midiDisconnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
1407 {
1408     FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
1409     return MMSYSERR_ERROR;
1410 }
1411
1412 typedef struct WINE_MIDIStream {
1413     HMIDIOUT                    hDevice;
1414     HANDLE                      hThread;
1415     DWORD                       dwThreadID;
1416     DWORD                       dwTempo;
1417     DWORD                       dwTimeDiv;
1418     DWORD                       dwPositionMS;
1419     DWORD                       dwPulses;
1420     DWORD                       dwStartTicks;
1421     WORD                        wFlags;
1422     HANDLE                      hEvent;
1423     LPMIDIHDR                   lpMidiHdr;
1424 } WINE_MIDIStream;
1425
1426 #define WINE_MSM_HEADER         (WM_USER+0)
1427 #define WINE_MSM_STOP           (WM_USER+1)
1428
1429 /**************************************************************************
1430  *                              MMSYSTEM_GetMidiStream          [internal]
1431  */
1432 static  BOOL    MMSYSTEM_GetMidiStream(HMIDISTRM hMidiStrm, WINE_MIDIStream** lpMidiStrm, WINE_MIDI** lplpwm)
1433 {
1434     WINE_MIDI* lpwm = (LPWINE_MIDI)MMDRV_Get(hMidiStrm, MMDRV_MIDIOUT, FALSE);
1435
1436     if (lplpwm)
1437         *lplpwm = lpwm;
1438
1439     if (lpwm == NULL) {
1440         return FALSE;
1441     }
1442
1443     *lpMidiStrm = (WINE_MIDIStream*)lpwm->mod.rgIds.dwStreamID;
1444
1445     return *lpMidiStrm != NULL;
1446 }
1447
1448 /**************************************************************************
1449  *                              MMSYSTEM_MidiStream_Convert     [internal]
1450  */
1451 static  DWORD   MMSYSTEM_MidiStream_Convert(WINE_MIDIStream* lpMidiStrm, DWORD pulse)
1452 {
1453     DWORD       ret = 0;
1454
1455     if (lpMidiStrm->dwTimeDiv == 0) {
1456         FIXME("Shouldn't happen. lpMidiStrm->dwTimeDiv = 0\n");
1457     } else if (lpMidiStrm->dwTimeDiv > 0x8000) { /* SMPTE, unchecked FIXME? */
1458         int     nf = -(char)HIBYTE(lpMidiStrm->dwTimeDiv);      /* number of frames     */
1459         int     nsf = LOBYTE(lpMidiStrm->dwTimeDiv);            /* number of sub-frames */
1460         ret = (pulse * 1000) / (nf * nsf);
1461     } else {
1462         ret = (DWORD)((double)pulse * ((double)lpMidiStrm->dwTempo / 1000) /
1463                       (double)lpMidiStrm->dwTimeDiv);
1464     }
1465
1466     return ret;
1467 }
1468
1469 /**************************************************************************
1470  *                      MMSYSTEM_MidiStream_MessageHandler      [internal]
1471  */
1472 static  BOOL    MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream* lpMidiStrm, LPWINE_MIDI lpwm, LPMSG msg)
1473 {
1474     LPMIDIHDR   lpMidiHdr;
1475     LPMIDIHDR*  lpmh;
1476     LPBYTE      lpData;
1477
1478     switch (msg->message) {
1479     case WM_QUIT:
1480         SetEvent(lpMidiStrm->hEvent);
1481         return FALSE;
1482     case WINE_MSM_STOP:
1483         TRACE("STOP\n");
1484         /* this is not quite what MS doc says... */
1485         midiOutReset(lpMidiStrm->hDevice);
1486         /* empty list of already submitted buffers */
1487         for (lpMidiHdr = lpMidiStrm->lpMidiHdr; lpMidiHdr; lpMidiHdr = lpMidiHdr->lpNext) {
1488             lpMidiHdr->dwFlags |= MHDR_DONE;
1489             lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1490
1491             DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1492                            (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1493                            lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1494         }
1495         lpMidiStrm->lpMidiHdr = 0;
1496         SetEvent(lpMidiStrm->hEvent);
1497         break;
1498     case WINE_MSM_HEADER:
1499         /* sets initial tick count for first MIDIHDR */
1500         if (!lpMidiStrm->dwStartTicks)
1501             lpMidiStrm->dwStartTicks = GetTickCount();
1502
1503         /* FIXME(EPP): "I don't understand the content of the first MIDIHDR sent
1504          * by native mcimidi, it doesn't look like a correct one".
1505          * this trick allows to throw it away... but I don't like it.
1506          * It looks like part of the file I'm trying to play and definitively looks
1507          * like raw midi content
1508          * I'd really like to understand why native mcimidi sends it. Perhaps a bad
1509          * synchronization issue where native mcimidi is still processing raw MIDI
1510          * content before generating MIDIEVENTs ?
1511          *
1512          * 4c 04 89 3b 00 81 7c 99 3b 43 00 99 23 5e 04 89 L..;..|.;C..#^..
1513          * 3b 00 00 89 23 00 7c 99 3b 45 00 99 28 62 04 89 ;...#.|.;E..(b..
1514          * 3b 00 00 89 28 00 81 7c 99 3b 4e 00 99 23 5e 04 ;...(..|.;N..#^.
1515          * 89 3b 00 00 89 23 00 7c 99 3b 45 00 99 23 78 04 .;...#.|.;E..#x.
1516          * 89 3b 00 00 89 23 00 81 7c 99 3b 48 00 99 23 5e .;...#..|.;H..#^
1517          * 04 89 3b 00 00 89 23 00 7c 99 3b 4e 00 99 28 62 ..;...#.|.;N..(b
1518          * 04 89 3b 00 00 89 28 00 81 7c 99 39 4c 00 99 23 ..;...(..|.9L..#
1519          * 5e 04 89 39 00 00 89 23 00 82 7c 99 3b 4c 00 99 ^..9...#..|.;L..
1520          * 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 00 99 #^..;...#.|.;H..
1521          * 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b 3f 04 (b..;...(..|.;?.
1522          * 89 3b 00 1c 99 23 5e 04 89 23 00 5c 99 3b 45 00 .;...#^..#.\.;E.
1523          * 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 3b 46 .#x..;...#..|.;F
1524          * 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 ..#^..;...#.|.;H
1525          * 00 99 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b ..(b..;...(..|.;
1526          * 46 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b F..#^..;...#.|.;
1527          * 48 00 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 H..#x..;...#..|.
1528          * 3b 4c 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 ;L..#^..;...#.|.
1529          */
1530         lpMidiHdr = (LPMIDIHDR)msg->lParam;
1531         lpData = (LPBYTE)lpMidiHdr->lpData;
1532         TRACE("Adding %s lpMidiHdr=%p [lpData=0x%p dwBufferLength=%u/%u dwFlags=0x%08x size=%lu]\n",
1533               (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular", lpMidiHdr,
1534               lpMidiHdr, lpMidiHdr->dwBufferLength, lpMidiHdr->dwBytesRecorded,
1535               lpMidiHdr->dwFlags, msg->wParam);
1536 #if 0
1537         /* dumps content of lpMidiHdr->lpData
1538          * FIXME: there should be a debug routine somewhere that already does this
1539          * I hate spreading this type of shit all around the code
1540          */
1541         for (dwToGo = 0; dwToGo < lpMidiHdr->dwBufferLength; dwToGo += 16) {
1542             DWORD       i;
1543             BYTE        ch;
1544
1545             for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++)
1546                 printf("%02x ", lpData[dwToGo + i]);
1547             for (; i < 16; i++)
1548                 printf("   ");
1549             for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++) {
1550                 ch = lpData[dwToGo + i];
1551                 printf("%c", (ch >= 0x20 && ch <= 0x7F) ? ch : '.');
1552             }
1553             printf("\n");
1554         }
1555 #endif
1556         if (((LPMIDIEVENT)lpData)->dwStreamID != 0 &&
1557             ((LPMIDIEVENT)lpData)->dwStreamID != 0xFFFFFFFF &&
1558             ((LPMIDIEVENT)lpData)->dwStreamID != (DWORD)lpMidiStrm) {
1559             FIXME("Dropping bad %s lpMidiHdr (streamID=%08x)\n",
1560                   (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular",
1561                   ((LPMIDIEVENT)lpData)->dwStreamID);
1562             lpMidiHdr->dwFlags |= MHDR_DONE;
1563             lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1564
1565             DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1566                            (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1567                            lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1568             break;
1569         }
1570
1571         for (lpmh = &lpMidiStrm->lpMidiHdr; *lpmh; lpmh = &(*lpmh)->lpNext);
1572         *lpmh = lpMidiHdr;
1573         lpMidiHdr = (LPMIDIHDR)msg->lParam;
1574         lpMidiHdr->lpNext = 0;
1575         lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1576         lpMidiHdr->dwFlags &= ~MHDR_DONE;
1577         lpMidiHdr->dwOffset = 0;
1578
1579         break;
1580     default:
1581         FIXME("Unknown message %d\n", msg->message);
1582         break;
1583     }
1584     return TRUE;
1585 }
1586
1587 /**************************************************************************
1588  *                              MMSYSTEM_MidiStream_Player      [internal]
1589  */
1590 static  DWORD   CALLBACK        MMSYSTEM_MidiStream_Player(LPVOID pmt)
1591 {
1592     WINE_MIDIStream*    lpMidiStrm = pmt;
1593     WINE_MIDI*          lpwm;
1594     MSG                 msg;
1595     DWORD               dwToGo;
1596     DWORD               dwCurrTC;
1597     LPMIDIHDR           lpMidiHdr;
1598     LPMIDIEVENT         me;
1599     LPBYTE              lpData = 0;
1600
1601     TRACE("(%p)!\n", lpMidiStrm);
1602
1603     if (!lpMidiStrm ||
1604         (lpwm = (LPWINE_MIDI)MMDRV_Get(lpMidiStrm->hDevice, MMDRV_MIDIOUT, FALSE)) == NULL)
1605         goto the_end;
1606
1607     /* force thread's queue creation */
1608     /* Used to be InitThreadInput16(0, 5); */
1609     /* but following works also with hack in midiStreamOpen */
1610     PeekMessageA(&msg, 0, 0, 0, 0);
1611
1612     /* FIXME: this next line must be called before midiStreamOut or midiStreamRestart are called */
1613     SetEvent(lpMidiStrm->hEvent);
1614     TRACE("Ready to go 1\n");
1615     /* thread is started in paused mode */
1616     SuspendThread(lpMidiStrm->hThread);
1617     TRACE("Ready to go 2\n");
1618
1619     lpMidiStrm->dwStartTicks = 0;
1620     lpMidiStrm->dwPulses = 0;
1621
1622     lpMidiStrm->lpMidiHdr = 0;
1623
1624     for (;;) {
1625         lpMidiHdr = lpMidiStrm->lpMidiHdr;
1626         if (!lpMidiHdr) {
1627             /* for first message, block until one arrives, then process all that are available */
1628             GetMessageA(&msg, 0, 0, 0);
1629             do {
1630                 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1631                     goto the_end;
1632             } while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE));
1633             lpData = 0;
1634             continue;
1635         }
1636
1637         if (!lpData)
1638             lpData = (LPBYTE)lpMidiHdr->lpData;
1639
1640         me = (LPMIDIEVENT)(lpData + lpMidiHdr->dwOffset);
1641
1642         /* do we have to wait ? */
1643         if (me->dwDeltaTime) {
1644             lpMidiStrm->dwPositionMS += MMSYSTEM_MidiStream_Convert(lpMidiStrm, me->dwDeltaTime);
1645             lpMidiStrm->dwPulses += me->dwDeltaTime;
1646
1647             dwToGo = lpMidiStrm->dwStartTicks + lpMidiStrm->dwPositionMS;
1648
1649             TRACE("%d/%d/%d\n", dwToGo, GetTickCount(), me->dwDeltaTime);
1650             while ((dwCurrTC = GetTickCount()) < dwToGo) {
1651                 if (MsgWaitForMultipleObjects(0, NULL, FALSE, dwToGo - dwCurrTC, QS_ALLINPUT) == WAIT_OBJECT_0) {
1652                     /* got a message, handle it */
1653                     while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
1654                         if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
1655                             goto the_end;
1656                     }
1657                     lpData = 0;
1658                 } else {
1659                     /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
1660                     break;
1661                 }
1662             }
1663         }
1664         switch (MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK)) {
1665         case MEVT_COMMENT:
1666             FIXME("NIY: MEVT_COMMENT\n");
1667             /* do nothing, skip bytes */
1668             break;
1669         case MEVT_LONGMSG:
1670             FIXME("NIY: MEVT_LONGMSG, aka sending Sysex event\n");
1671             break;
1672         case MEVT_NOP:
1673             break;
1674         case MEVT_SHORTMSG:
1675             midiOutShortMsg(lpMidiStrm->hDevice, MEVT_EVENTPARM(me->dwEvent));
1676             break;
1677         case MEVT_TEMPO:
1678             lpMidiStrm->dwTempo = MEVT_EVENTPARM(me->dwEvent);
1679             break;
1680         case MEVT_VERSION:
1681             break;
1682         default:
1683             FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK));
1684             break;
1685         }
1686         if (me->dwEvent & MEVT_F_CALLBACK) {
1687             DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1688                            (HDRVR)lpMidiStrm->hDevice, MM_MOM_POSITIONCB,
1689                            lpwm->mod.dwInstance, (LPARAM)lpMidiHdr, 0L);
1690         }
1691         lpMidiHdr->dwOffset += sizeof(MIDIEVENT) - sizeof(me->dwParms);
1692         if (me->dwEvent & MEVT_F_LONG)
1693             lpMidiHdr->dwOffset += (MEVT_EVENTPARM(me->dwEvent) + 3) & ~3;
1694         if (lpMidiHdr->dwOffset >= lpMidiHdr->dwBufferLength) {
1695             /* done with this header */
1696             lpMidiHdr->dwFlags |= MHDR_DONE;
1697             lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1698
1699             lpMidiStrm->lpMidiHdr = lpMidiHdr->lpNext;
1700             DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
1701                            (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
1702                            lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
1703             lpData = 0;
1704         }
1705     }
1706 the_end:
1707     TRACE("End of thread\n");
1708     ExitThread(0);
1709     return 0;   /* for removing the warning, never executed */
1710 }
1711
1712 /**************************************************************************
1713  *                              MMSYSTEM_MidiStream_PostMessage [internal]
1714  */
1715 static  BOOL MMSYSTEM_MidiStream_PostMessage(WINE_MIDIStream* lpMidiStrm, WORD msg, DWORD pmt1, DWORD pmt2)
1716 {
1717     if (PostThreadMessageA(lpMidiStrm->dwThreadID, msg, pmt1, pmt2)) {
1718         DWORD   count;
1719
1720         ReleaseThunkLock(&count);
1721         WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
1722         RestoreThunkLock(count);
1723     } else {
1724         WARN("bad PostThreadMessageA\n");
1725         return FALSE;
1726     }
1727     return TRUE;
1728 }
1729
1730 /**************************************************************************
1731  *                              midiStreamClose                 [WINMM.@]
1732  */
1733 MMRESULT WINAPI midiStreamClose(HMIDISTRM hMidiStrm)
1734 {
1735     WINE_MIDIStream*    lpMidiStrm;
1736
1737     TRACE("(%p)!\n", hMidiStrm);
1738
1739     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
1740         return MMSYSERR_INVALHANDLE;
1741
1742     midiStreamStop(hMidiStrm);
1743     MMSYSTEM_MidiStream_PostMessage(lpMidiStrm, WM_QUIT, 0, 0);
1744     HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1745     CloseHandle(lpMidiStrm->hEvent);
1746
1747     return midiOutClose((HMIDIOUT)hMidiStrm);
1748 }
1749
1750 /**************************************************************************
1751  *                              midiStreamOpen                  [WINMM.@]
1752  */
1753 MMRESULT WINAPI midiStreamOpen(HMIDISTRM* lphMidiStrm, LPUINT lpuDeviceID,
1754                                DWORD cMidi, DWORD_PTR dwCallback,
1755                                DWORD_PTR dwInstance, DWORD fdwOpen)
1756 {
1757     WINE_MIDIStream*    lpMidiStrm;
1758     MMRESULT            ret;
1759     MIDIOPENSTRMID      mosm;
1760     LPWINE_MIDI         lpwm;
1761     HMIDIOUT            hMidiOut;
1762
1763     TRACE("(%p, %p, %d, 0x%08lx, 0x%08lx, 0x%08x)!\n",
1764           lphMidiStrm, lpuDeviceID, cMidi, dwCallback, dwInstance, fdwOpen);
1765
1766     if (cMidi != 1 || lphMidiStrm == NULL || lpuDeviceID == NULL)
1767         return MMSYSERR_INVALPARAM;
1768
1769     lpMidiStrm = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream));
1770     if (!lpMidiStrm)
1771         return MMSYSERR_NOMEM;
1772
1773     lpMidiStrm->dwTempo = 500000;
1774     lpMidiStrm->dwTimeDiv = 480;        /* 480 is 120 quarter notes per minute *//* FIXME ??*/
1775     lpMidiStrm->dwPositionMS = 0;
1776
1777     mosm.dwStreamID = (DWORD)lpMidiStrm;
1778     /* FIXME: the correct value is not allocated yet for MAPPER */
1779     mosm.wDeviceID  = *lpuDeviceID;
1780     lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &fdwOpen, 1, &mosm);
1781     lpMidiStrm->hDevice = hMidiOut;
1782     if (lphMidiStrm)
1783         *lphMidiStrm = (HMIDISTRM)hMidiOut;
1784
1785     lpwm->mld.uDeviceID = *lpuDeviceID;
1786
1787     ret = MMDRV_Open(&lpwm->mld, MODM_OPEN, (DWORD_PTR)&lpwm->mod, fdwOpen);
1788     lpMidiStrm->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1789     lpMidiStrm->wFlags = HIWORD(fdwOpen);
1790
1791     lpMidiStrm->hThread = CreateThread(NULL, 0, MMSYSTEM_MidiStream_Player,
1792                                        lpMidiStrm, 0, &(lpMidiStrm->dwThreadID));
1793
1794     if (!lpMidiStrm->hThread) {
1795         midiStreamClose((HMIDISTRM)hMidiOut);
1796         return MMSYSERR_NOMEM;
1797     }
1798     SetThreadPriority(lpMidiStrm->hThread, THREAD_PRIORITY_TIME_CRITICAL);
1799
1800     /* wait for thread to have started, and for its queue to be created */
1801     {
1802         DWORD   count;
1803
1804         /* (Release|Restore)ThunkLock() is needed when this method is called from 16 bit code,
1805          * (meaning the Win16Lock is set), so that it's released and the 32 bit thread running
1806          * MMSYSTEM_MidiStreamPlayer can acquire Win16Lock to create its queue.
1807          */
1808         ReleaseThunkLock(&count);
1809         WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
1810         RestoreThunkLock(count);
1811     }
1812
1813     TRACE("=> (%u/%d) hMidi=%p ret=%d lpMidiStrm=%p\n",
1814           *lpuDeviceID, lpwm->mld.uDeviceID, *lphMidiStrm, ret, lpMidiStrm);
1815     return ret;
1816 }
1817
1818 /**************************************************************************
1819  *                              midiStreamOut                   [WINMM.@]
1820  */
1821 MMRESULT WINAPI midiStreamOut(HMIDISTRM hMidiStrm, LPMIDIHDR lpMidiHdr,
1822                               UINT cbMidiHdr)
1823 {
1824     WINE_MIDIStream*    lpMidiStrm;
1825     DWORD               ret = MMSYSERR_NOERROR;
1826
1827     TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMidiHdr, cbMidiHdr);
1828
1829     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1830         ret = MMSYSERR_INVALHANDLE;
1831     } else if (!lpMidiHdr) {
1832         ret = MMSYSERR_INVALPARAM;
1833     } else {
1834         if (!PostThreadMessageA(lpMidiStrm->dwThreadID,
1835                                 WINE_MSM_HEADER, cbMidiHdr,
1836                                 (LPARAM)lpMidiHdr)) {
1837             WARN("bad PostThreadMessageA\n");
1838             ret = MMSYSERR_ERROR;
1839         }
1840     }
1841     return ret;
1842 }
1843
1844 /**************************************************************************
1845  *                              midiStreamPause                 [WINMM.@]
1846  */
1847 MMRESULT WINAPI midiStreamPause(HMIDISTRM hMidiStrm)
1848 {
1849     WINE_MIDIStream*    lpMidiStrm;
1850     DWORD               ret = MMSYSERR_NOERROR;
1851
1852     TRACE("(%p)!\n", hMidiStrm);
1853
1854     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1855         ret = MMSYSERR_INVALHANDLE;
1856     } else {
1857         if (SuspendThread(lpMidiStrm->hThread) == 0xFFFFFFFF) {
1858             WARN("bad Suspend (%d)\n", GetLastError());
1859             ret = MMSYSERR_ERROR;
1860         }
1861     }
1862     return ret;
1863 }
1864
1865 /**************************************************************************
1866  *                              midiStreamPosition              [WINMM.@]
1867  */
1868 MMRESULT WINAPI midiStreamPosition(HMIDISTRM hMidiStrm, LPMMTIME lpMMT, UINT cbmmt)
1869 {
1870     WINE_MIDIStream*    lpMidiStrm;
1871     DWORD               ret = MMSYSERR_NOERROR;
1872
1873     TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMMT, cbmmt);
1874
1875     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1876         ret = MMSYSERR_INVALHANDLE;
1877     } else if (lpMMT == NULL || cbmmt != sizeof(MMTIME)) {
1878         ret = MMSYSERR_INVALPARAM;
1879     } else {
1880         switch (lpMMT->wType) {
1881         case TIME_MS:
1882             lpMMT->u.ms = lpMidiStrm->dwPositionMS;
1883             TRACE("=> %d ms\n", lpMMT->u.ms);
1884             break;
1885         case TIME_TICKS:
1886             lpMMT->u.ticks = lpMidiStrm->dwPulses;
1887             TRACE("=> %d ticks\n", lpMMT->u.ticks);
1888             break;
1889         default:
1890             WARN("Unsupported time type %d\n", lpMMT->wType);
1891             lpMMT->wType = TIME_MS;
1892             ret = MMSYSERR_INVALPARAM;
1893             break;
1894         }
1895     }
1896     return ret;
1897 }
1898
1899 /**************************************************************************
1900  *                              midiStreamProperty              [WINMM.@]
1901  */
1902 MMRESULT WINAPI midiStreamProperty(HMIDISTRM hMidiStrm, LPBYTE lpPropData, DWORD dwProperty)
1903 {
1904     WINE_MIDIStream*    lpMidiStrm;
1905     MMRESULT            ret = MMSYSERR_NOERROR;
1906
1907     TRACE("(%p, %p, %x)\n", hMidiStrm, lpPropData, dwProperty);
1908
1909     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1910         ret = MMSYSERR_INVALHANDLE;
1911     } else if ((dwProperty & (MIDIPROP_GET|MIDIPROP_SET)) == 0) {
1912         ret = MMSYSERR_INVALPARAM;
1913     } else if (dwProperty & MIDIPROP_TEMPO) {
1914         MIDIPROPTEMPO*  mpt = (MIDIPROPTEMPO*)lpPropData;
1915
1916         if (sizeof(MIDIPROPTEMPO) != mpt->cbStruct) {
1917             ret = MMSYSERR_INVALPARAM;
1918         } else if (dwProperty & MIDIPROP_SET) {
1919             lpMidiStrm->dwTempo = mpt->dwTempo;
1920             TRACE("Setting tempo to %d\n", mpt->dwTempo);
1921         } else if (dwProperty & MIDIPROP_GET) {
1922             mpt->dwTempo = lpMidiStrm->dwTempo;
1923             TRACE("Getting tempo <= %d\n", mpt->dwTempo);
1924         }
1925     } else if (dwProperty & MIDIPROP_TIMEDIV) {
1926         MIDIPROPTIMEDIV*        mptd = (MIDIPROPTIMEDIV*)lpPropData;
1927
1928         if (sizeof(MIDIPROPTIMEDIV) != mptd->cbStruct) {
1929             ret = MMSYSERR_INVALPARAM;
1930         } else if (dwProperty & MIDIPROP_SET) {
1931             lpMidiStrm->dwTimeDiv = mptd->dwTimeDiv;
1932             TRACE("Setting time div to %d\n", mptd->dwTimeDiv);
1933         } else if (dwProperty & MIDIPROP_GET) {
1934             mptd->dwTimeDiv = lpMidiStrm->dwTimeDiv;
1935             TRACE("Getting time div <= %d\n", mptd->dwTimeDiv);
1936         }
1937     } else {
1938         ret = MMSYSERR_INVALPARAM;
1939     }
1940
1941     return ret;
1942 }
1943
1944 /**************************************************************************
1945  *                              midiStreamRestart               [WINMM.@]
1946  */
1947 MMRESULT WINAPI midiStreamRestart(HMIDISTRM hMidiStrm)
1948 {
1949     WINE_MIDIStream*    lpMidiStrm;
1950     MMRESULT            ret = MMSYSERR_NOERROR;
1951
1952     TRACE("(%p)!\n", hMidiStrm);
1953
1954     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1955         ret = MMSYSERR_INVALHANDLE;
1956     } else {
1957         DWORD   ret;
1958
1959         /* since we increase the thread suspend count on each midiStreamPause
1960          * there may be a need for several midiStreamResume
1961          */
1962         do {
1963             ret = ResumeThread(lpMidiStrm->hThread);
1964         } while (ret != 0xFFFFFFFF && ret != 0);
1965         if (ret == 0xFFFFFFFF) {
1966             WARN("bad Resume (%d)\n", GetLastError());
1967             ret = MMSYSERR_ERROR;
1968         } else {
1969             lpMidiStrm->dwStartTicks = GetTickCount() - lpMidiStrm->dwPositionMS;
1970         }
1971     }
1972     return ret;
1973 }
1974
1975 /**************************************************************************
1976  *                              midiStreamStop                  [WINMM.@]
1977  */
1978 MMRESULT WINAPI midiStreamStop(HMIDISTRM hMidiStrm)
1979 {
1980     WINE_MIDIStream*    lpMidiStrm;
1981     MMRESULT            ret = MMSYSERR_NOERROR;
1982
1983     TRACE("(%p)!\n", hMidiStrm);
1984
1985     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
1986         ret = MMSYSERR_INVALHANDLE;
1987     } else {
1988         /* in case stream has been paused... FIXME is the current state correct ? */
1989         midiStreamRestart(hMidiStrm);
1990         MMSYSTEM_MidiStream_PostMessage(lpMidiStrm, WINE_MSM_STOP, 0, 0);
1991     }
1992     return ret;
1993 }
1994
1995 static UINT WAVE_Open(HANDLE* lphndl, UINT uDeviceID, UINT uType,
1996                       LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
1997                       DWORD_PTR dwInstance, DWORD dwFlags)
1998 {
1999     HANDLE              handle;
2000     LPWINE_MLD          wmld;
2001     DWORD               dwRet = MMSYSERR_NOERROR;
2002     WAVEOPENDESC        wod;
2003
2004     TRACE("(%p, %d, %s, %p, %08lX, %08lX, %08X);\n",
2005           lphndl, (int)uDeviceID, (uType==MMDRV_WAVEOUT)?"Out":"In", lpFormat, dwCallback,
2006           dwInstance, dwFlags);
2007
2008     if (dwFlags & WAVE_FORMAT_QUERY)
2009         TRACE("WAVE_FORMAT_QUERY requested !\n");
2010
2011     if (lpFormat == NULL) {
2012         WARN("bad format\n");
2013         return WAVERR_BADFORMAT;
2014     }
2015
2016     if ((dwFlags & WAVE_MAPPED) && (uDeviceID == (UINT)-1)) {
2017         WARN("invalid parameter\n");
2018         return MMSYSERR_INVALPARAM;
2019     }
2020
2021     /* may have a PCMWAVEFORMAT rather than a WAVEFORMATEX so don't read cbSize */
2022     TRACE("wFormatTag=%u, nChannels=%u, nSamplesPerSec=%u, nAvgBytesPerSec=%u, nBlockAlign=%u, wBitsPerSample=%u\n",
2023           lpFormat->wFormatTag, lpFormat->nChannels, lpFormat->nSamplesPerSec,
2024           lpFormat->nAvgBytesPerSec, lpFormat->nBlockAlign, lpFormat->wBitsPerSample);
2025
2026     if ((wmld = MMDRV_Alloc(sizeof(WINE_WAVE), uType, &handle,
2027                             &dwFlags, &dwCallback, &dwInstance)) == NULL) {
2028         WARN("no memory\n");
2029         return MMSYSERR_NOMEM;
2030     }
2031
2032     wod.hWave = handle;
2033     wod.lpFormat = (LPWAVEFORMATEX)lpFormat;  /* should the struct be copied iso pointer? */
2034     wod.dwCallback = dwCallback;
2035     wod.dwInstance = dwInstance;
2036     wod.dnDevNode = 0L;
2037
2038     TRACE("cb=%08lx\n", wod.dwCallback);
2039
2040     for (;;) {
2041         if (dwFlags & WAVE_MAPPED) {
2042             wod.uMappedDeviceID = uDeviceID;
2043             uDeviceID = WAVE_MAPPER;
2044         } else {
2045             wod.uMappedDeviceID = -1;
2046         }
2047         wmld->uDeviceID = uDeviceID;
2048     
2049         dwRet = MMDRV_Open(wmld, (uType == MMDRV_WAVEOUT) ? WODM_OPEN : WIDM_OPEN,
2050                            (DWORD_PTR)&wod, dwFlags);
2051
2052         TRACE("dwRet = %s\n", WINMM_ErrorToString(dwRet));
2053         if (dwRet != WAVERR_BADFORMAT ||
2054             ((dwFlags & (WAVE_MAPPED|WAVE_FORMAT_DIRECT)) != 0) || (uDeviceID == WAVE_MAPPER)) break;
2055         /* if we ask for a format which isn't supported by the physical driver, 
2056          * let's try to map it through the wave mapper (except, if we already tried
2057          * or user didn't allow us to use acm codecs or the device is already the mapper)
2058          */
2059         dwFlags |= WAVE_MAPPED;
2060         /* we shall loop only one */
2061     }
2062
2063     if ((dwFlags & WAVE_FORMAT_QUERY) || dwRet != MMSYSERR_NOERROR) {
2064         MMDRV_Free(handle, wmld);
2065         handle = 0;
2066     }
2067
2068     if (lphndl != NULL) *lphndl = handle;
2069     TRACE("=> %s hWave=%p\n", WINMM_ErrorToString(dwRet), handle);
2070
2071     return dwRet;
2072 }
2073
2074 /**************************************************************************
2075  *                              waveOutGetNumDevs               [WINMM.@]
2076  */
2077 UINT WINAPI waveOutGetNumDevs(void)
2078 {
2079     return MMDRV_GetNum(MMDRV_WAVEOUT);
2080 }
2081
2082 /**************************************************************************
2083  *                              waveOutGetDevCapsA              [WINMM.@]
2084  */
2085 UINT WINAPI waveOutGetDevCapsA(UINT_PTR uDeviceID, LPWAVEOUTCAPSA lpCaps,
2086                                UINT uSize)
2087 {
2088     WAVEOUTCAPSW        wocW;
2089     UINT                ret;
2090
2091     if (lpCaps == NULL)        return MMSYSERR_INVALPARAM;
2092
2093     ret = waveOutGetDevCapsW(uDeviceID, &wocW, sizeof(wocW));
2094
2095     if (ret == MMSYSERR_NOERROR) {
2096         WAVEOUTCAPSA wocA;
2097         wocA.wMid           = wocW.wMid;
2098         wocA.wPid           = wocW.wPid;
2099         wocA.vDriverVersion = wocW.vDriverVersion;
2100         WideCharToMultiByte( CP_ACP, 0, wocW.szPname, -1, wocA.szPname,
2101                              sizeof(wocA.szPname), NULL, NULL );
2102         wocA.dwFormats      = wocW.dwFormats;
2103         wocA.wChannels      = wocW.wChannels;
2104         wocA.dwSupport      = wocW.dwSupport;
2105         memcpy(lpCaps, &wocA, min(uSize, sizeof(wocA)));
2106     }
2107     return ret;
2108 }
2109
2110 /**************************************************************************
2111  *                              waveOutGetDevCapsW              [WINMM.@]
2112  */
2113 UINT WINAPI waveOutGetDevCapsW(UINT_PTR uDeviceID, LPWAVEOUTCAPSW lpCaps,
2114                                UINT uSize)
2115 {
2116     LPWINE_MLD          wmld;
2117
2118     TRACE("(%lu %p %u)!\n", uDeviceID, lpCaps, uSize);
2119
2120     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2121
2122     if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_WAVEOUT, TRUE)) == NULL)
2123         return MMSYSERR_BADDEVICEID;
2124
2125     return MMDRV_Message(wmld, WODM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
2126 }
2127
2128 /**************************************************************************
2129  *                              waveOutGetErrorTextA    [WINMM.@]
2130  *                              waveInGetErrorTextA     [WINMM.@]
2131  */
2132 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2133 {
2134     UINT        ret;
2135
2136     if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2137     else if (uSize == 0) ret = MMSYSERR_NOERROR;
2138     else
2139     {
2140         LPWSTR  xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
2141         if (!xstr) ret = MMSYSERR_NOMEM;
2142         else
2143         {
2144             ret = waveOutGetErrorTextW(uError, xstr, uSize);
2145             if (ret == MMSYSERR_NOERROR)
2146                 WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
2147             HeapFree(GetProcessHeap(), 0, xstr);
2148         }
2149     }
2150     return ret;
2151 }
2152
2153 /**************************************************************************
2154  *                              waveOutGetErrorTextW    [WINMM.@]
2155  *                              waveInGetErrorTextW     [WINMM.@]
2156  */
2157 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2158 {
2159     UINT        ret = MMSYSERR_BADERRNUM;
2160
2161     if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
2162     else if (uSize == 0) ret = MMSYSERR_NOERROR;
2163     else if (
2164                /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
2165                 * a warning for the test was always true */
2166                (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
2167                (uError >= WAVERR_BASE  && uError <= WAVERR_LASTERROR)) {
2168         if (LoadStringW(hWinMM32Instance,
2169                         uError, lpText, uSize) > 0) {
2170             ret = MMSYSERR_NOERROR;
2171         }
2172     }
2173     return ret;
2174 }
2175
2176 /**************************************************************************
2177  *                      waveOutOpen                     [WINMM.@]
2178  * All the args/structs have the same layout as the win16 equivalents
2179  */
2180 MMRESULT WINAPI waveOutOpen(LPHWAVEOUT lphWaveOut, UINT uDeviceID,
2181                        LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2182                        DWORD_PTR dwInstance, DWORD dwFlags)
2183 {
2184     return WAVE_Open((HANDLE*)lphWaveOut, uDeviceID, MMDRV_WAVEOUT, lpFormat,
2185                      dwCallback, dwInstance, dwFlags);
2186 }
2187
2188 /**************************************************************************
2189  *                              waveOutClose            [WINMM.@]
2190  */
2191 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
2192 {
2193     LPWINE_MLD          wmld;
2194     DWORD               dwRet;
2195
2196     TRACE("(%p)\n", hWaveOut);
2197
2198     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2199         return MMSYSERR_INVALHANDLE;
2200
2201     dwRet = MMDRV_Close(wmld, WODM_CLOSE);
2202     if (dwRet != WAVERR_STILLPLAYING)
2203         MMDRV_Free(hWaveOut, wmld);
2204
2205     return dwRet;
2206 }
2207
2208 /**************************************************************************
2209  *                              waveOutPrepareHeader    [WINMM.@]
2210  */
2211 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
2212                                  WAVEHDR* lpWaveOutHdr, UINT uSize)
2213 {
2214     LPWINE_MLD          wmld;
2215     UINT                result;
2216
2217     TRACE("(%p, %p, %u);\n", hWaveOut, lpWaveOutHdr, uSize);
2218
2219     if (lpWaveOutHdr == NULL || uSize < sizeof (WAVEHDR))
2220         return MMSYSERR_INVALPARAM;
2221
2222     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2223         return MMSYSERR_INVALHANDLE;
2224
2225     if ((result = MMDRV_Message(wmld, WODM_PREPARE, (DWORD_PTR)lpWaveOutHdr,
2226                                 uSize)) != MMSYSERR_NOTSUPPORTED)
2227         return result;
2228
2229     if (lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2230         return WAVERR_STILLPLAYING;
2231
2232     lpWaveOutHdr->dwFlags |= WHDR_PREPARED;
2233     lpWaveOutHdr->dwFlags &= ~WHDR_DONE;
2234
2235     return MMSYSERR_NOERROR;
2236 }
2237
2238 /**************************************************************************
2239  *                              waveOutUnprepareHeader  [WINMM.@]
2240  */
2241 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
2242                                    LPWAVEHDR lpWaveOutHdr, UINT uSize)
2243 {
2244     LPWINE_MLD          wmld;
2245     UINT                result;
2246
2247     TRACE("(%p, %p, %u);\n", hWaveOut, lpWaveOutHdr, uSize);
2248
2249     if (lpWaveOutHdr == NULL || uSize < sizeof (WAVEHDR))
2250         return MMSYSERR_INVALPARAM;
2251     
2252     if (!(lpWaveOutHdr->dwFlags & WHDR_PREPARED)) {
2253         return MMSYSERR_NOERROR;
2254     }
2255
2256     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2257         return MMSYSERR_INVALHANDLE;
2258
2259     if ((result = MMDRV_Message(wmld, WODM_UNPREPARE, (DWORD_PTR)lpWaveOutHdr,
2260                                 uSize)) != MMSYSERR_NOTSUPPORTED)
2261         return result;
2262
2263     if (lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
2264         return WAVERR_STILLPLAYING;
2265
2266     lpWaveOutHdr->dwFlags &= ~WHDR_PREPARED;
2267     lpWaveOutHdr->dwFlags |= WHDR_DONE;
2268
2269     return MMSYSERR_NOERROR;
2270 }
2271
2272 /**************************************************************************
2273  *                              waveOutWrite            [WINMM.@]
2274  */
2275 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, LPWAVEHDR lpWaveOutHdr,
2276                          UINT uSize)
2277 {
2278     LPWINE_MLD          wmld;
2279
2280     TRACE("(%p, %p, %u);\n", hWaveOut, lpWaveOutHdr, uSize);
2281
2282     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2283         return MMSYSERR_INVALHANDLE;
2284
2285     return MMDRV_Message(wmld, WODM_WRITE, (DWORD_PTR)lpWaveOutHdr, uSize);
2286 }
2287
2288 /**************************************************************************
2289  *                              waveOutBreakLoop        [WINMM.@]
2290  */
2291 UINT WINAPI waveOutBreakLoop(HWAVEOUT hWaveOut)
2292 {
2293     LPWINE_MLD          wmld;
2294
2295     TRACE("(%p);\n", hWaveOut);
2296
2297     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2298         return MMSYSERR_INVALHANDLE;
2299     return MMDRV_Message(wmld, WODM_BREAKLOOP, 0L, 0L);
2300 }
2301
2302 /**************************************************************************
2303  *                              waveOutPause            [WINMM.@]
2304  */
2305 UINT WINAPI waveOutPause(HWAVEOUT hWaveOut)
2306 {
2307     LPWINE_MLD          wmld;
2308
2309     TRACE("(%p);\n", hWaveOut);
2310
2311     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2312         return MMSYSERR_INVALHANDLE;
2313     return MMDRV_Message(wmld, WODM_PAUSE, 0L, 0L);
2314 }
2315
2316 /**************************************************************************
2317  *                              waveOutReset            [WINMM.@]
2318  */
2319 UINT WINAPI waveOutReset(HWAVEOUT hWaveOut)
2320 {
2321     LPWINE_MLD          wmld;
2322
2323     TRACE("(%p);\n", hWaveOut);
2324
2325     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2326         return MMSYSERR_INVALHANDLE;
2327     return MMDRV_Message(wmld, WODM_RESET, 0L, 0L);
2328 }
2329
2330 /**************************************************************************
2331  *                              waveOutRestart          [WINMM.@]
2332  */
2333 UINT WINAPI waveOutRestart(HWAVEOUT hWaveOut)
2334 {
2335     LPWINE_MLD          wmld;
2336
2337     TRACE("(%p);\n", hWaveOut);
2338
2339     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2340         return MMSYSERR_INVALHANDLE;
2341     return MMDRV_Message(wmld, WODM_RESTART, 0L, 0L);
2342 }
2343
2344 /**************************************************************************
2345  *                              waveOutGetPosition      [WINMM.@]
2346  */
2347 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
2348                                UINT uSize)
2349 {
2350     LPWINE_MLD          wmld;
2351
2352     TRACE("(%p, %p, %u);\n", hWaveOut, lpTime, uSize);
2353
2354     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2355         return MMSYSERR_INVALHANDLE;
2356
2357     return MMDRV_Message(wmld, WODM_GETPOS, (DWORD_PTR)lpTime, uSize);
2358 }
2359
2360 /**************************************************************************
2361  *                              waveOutGetPitch         [WINMM.@]
2362  */
2363 UINT WINAPI waveOutGetPitch(HWAVEOUT hWaveOut, LPDWORD lpdw)
2364 {
2365     LPWINE_MLD          wmld;
2366
2367     TRACE("(%p, %p);\n", hWaveOut, lpdw);
2368
2369     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2370         return MMSYSERR_INVALHANDLE;
2371     return MMDRV_Message(wmld, WODM_GETPITCH, (DWORD_PTR)lpdw, 0L);
2372 }
2373
2374 /**************************************************************************
2375  *                              waveOutSetPitch         [WINMM.@]
2376  */
2377 UINT WINAPI waveOutSetPitch(HWAVEOUT hWaveOut, DWORD dw)
2378 {
2379     LPWINE_MLD          wmld;
2380
2381     TRACE("(%p, %08x);\n", hWaveOut, dw);
2382
2383     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2384         return MMSYSERR_INVALHANDLE;
2385     return MMDRV_Message(wmld, WODM_SETPITCH, dw, 0L);
2386 }
2387
2388 /**************************************************************************
2389  *                              waveOutGetPlaybackRate  [WINMM.@]
2390  */
2391 UINT WINAPI waveOutGetPlaybackRate(HWAVEOUT hWaveOut, LPDWORD lpdw)
2392 {
2393     LPWINE_MLD          wmld;
2394
2395     TRACE("(%p, %p);\n", hWaveOut, lpdw);
2396
2397     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2398         return MMSYSERR_INVALHANDLE;
2399     return MMDRV_Message(wmld, WODM_GETPLAYBACKRATE, (DWORD_PTR)lpdw, 0L);
2400 }
2401
2402 /**************************************************************************
2403  *                              waveOutSetPlaybackRate  [WINMM.@]
2404  */
2405 UINT WINAPI waveOutSetPlaybackRate(HWAVEOUT hWaveOut, DWORD dw)
2406 {
2407     LPWINE_MLD          wmld;
2408
2409     TRACE("(%p, %08x);\n", hWaveOut, dw);
2410
2411     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2412         return MMSYSERR_INVALHANDLE;
2413     return MMDRV_Message(wmld, WODM_SETPLAYBACKRATE, dw, 0L);
2414 }
2415
2416 /**************************************************************************
2417  *                              waveOutGetVolume        [WINMM.@]
2418  */
2419 UINT WINAPI waveOutGetVolume(HWAVEOUT hWaveOut, LPDWORD lpdw)
2420 {
2421     LPWINE_MLD          wmld;
2422
2423     TRACE("(%p, %p);\n", hWaveOut, lpdw);
2424
2425     if (lpdw == NULL) {
2426         WARN("invalid parameter\n");
2427         return MMSYSERR_INVALPARAM;
2428     }
2429
2430     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, TRUE)) == NULL)
2431         return MMSYSERR_INVALHANDLE;
2432
2433     return MMDRV_Message(wmld, WODM_GETVOLUME, (DWORD_PTR)lpdw, 0L);
2434 }
2435
2436 /**************************************************************************
2437  *                              waveOutSetVolume        [WINMM.@]
2438  */
2439 UINT WINAPI waveOutSetVolume(HWAVEOUT hWaveOut, DWORD dw)
2440 {
2441     LPWINE_MLD          wmld;
2442
2443     TRACE("(%p, %08x);\n", hWaveOut, dw);
2444
2445      if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, TRUE)) == NULL)
2446         return MMSYSERR_INVALHANDLE;
2447
2448     return MMDRV_Message(wmld, WODM_SETVOLUME, dw, 0L);
2449 }
2450
2451 /**************************************************************************
2452  *                              waveOutGetID            [WINMM.@]
2453  */
2454 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
2455 {
2456     LPWINE_MLD          wmld;
2457
2458     TRACE("(%p, %p);\n", hWaveOut, lpuDeviceID);
2459
2460     if (lpuDeviceID == NULL) return MMSYSERR_INVALHANDLE;
2461
2462     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL)
2463         return MMSYSERR_INVALHANDLE;
2464
2465     *lpuDeviceID = wmld->uDeviceID;
2466     return 0;
2467 }
2468
2469 /**************************************************************************
2470  *                              waveOutMessage          [WINMM.@]
2471  */
2472 UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage,
2473                            DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2474 {
2475     LPWINE_MLD          wmld;
2476
2477     TRACE("(%p, %u, %ld, %ld)\n", hWaveOut, uMessage, dwParam1, dwParam2);
2478
2479     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) {
2480         if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, TRUE)) != NULL) {
2481             return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
2482         }
2483         WARN("invalid handle\n");
2484         return MMSYSERR_INVALHANDLE;
2485     }
2486
2487     /* from M$ KB */
2488     if (uMessage < DRVM_IOCTL || (uMessage >= DRVM_IOCTL_LAST && uMessage < DRVM_MAPPER)) {
2489         WARN("invalid parameter\n");
2490         return MMSYSERR_INVALPARAM;
2491     }
2492
2493     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
2494 }
2495
2496 /**************************************************************************
2497  *                              waveInGetNumDevs                [WINMM.@]
2498  */
2499 UINT WINAPI waveInGetNumDevs(void)
2500 {
2501     return MMDRV_GetNum(MMDRV_WAVEIN);
2502 }
2503
2504 /**************************************************************************
2505  *                              waveInGetDevCapsW               [WINMM.@]
2506  */
2507 UINT WINAPI waveInGetDevCapsW(UINT_PTR uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
2508 {
2509     LPWINE_MLD          wmld;
2510
2511     TRACE("(%lu %p %u)!\n", uDeviceID, lpCaps, uSize);
2512
2513     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2514
2515     if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_WAVEIN, TRUE)) == NULL)
2516         return MMSYSERR_BADDEVICEID;
2517
2518     return MMDRV_Message(wmld, WIDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
2519 }
2520
2521 /**************************************************************************
2522  *                              waveInGetDevCapsA               [WINMM.@]
2523  */
2524 UINT WINAPI waveInGetDevCapsA(UINT_PTR uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
2525 {
2526     WAVEINCAPSW         wicW;
2527     UINT                ret;
2528
2529     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2530
2531     ret = waveInGetDevCapsW(uDeviceID, &wicW, sizeof(wicW));
2532
2533     if (ret == MMSYSERR_NOERROR) {
2534         WAVEINCAPSA wicA;
2535         wicA.wMid           = wicW.wMid;
2536         wicA.wPid           = wicW.wPid;
2537         wicA.vDriverVersion = wicW.vDriverVersion;
2538         WideCharToMultiByte( CP_ACP, 0, wicW.szPname, -1, wicA.szPname,
2539                              sizeof(wicA.szPname), NULL, NULL );
2540         wicA.dwFormats      = wicW.dwFormats;
2541         wicA.wChannels      = wicW.wChannels;
2542         memcpy(lpCaps, &wicA, min(uSize, sizeof(wicA)));
2543     }
2544     return ret;
2545 }
2546
2547 /**************************************************************************
2548  *                              waveInOpen                      [WINMM.@]
2549  */
2550 MMRESULT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
2551                            LPCWAVEFORMATEX lpFormat, DWORD_PTR dwCallback,
2552                            DWORD_PTR dwInstance, DWORD dwFlags)
2553 {
2554     return WAVE_Open((HANDLE*)lphWaveIn, uDeviceID, MMDRV_WAVEIN, lpFormat,
2555                      dwCallback, dwInstance, dwFlags);
2556 }
2557
2558 /**************************************************************************
2559  *                              waveInClose                     [WINMM.@]
2560  */
2561 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
2562 {
2563     LPWINE_MLD          wmld;
2564     DWORD               dwRet;
2565
2566     TRACE("(%p)\n", hWaveIn);
2567
2568     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2569         return MMSYSERR_INVALHANDLE;
2570
2571     dwRet = MMDRV_Message(wmld, WIDM_CLOSE, 0L, 0L);
2572     if (dwRet != WAVERR_STILLPLAYING)
2573         MMDRV_Free(hWaveIn, wmld);
2574     return dwRet;
2575 }
2576
2577 /**************************************************************************
2578  *                              waveInPrepareHeader             [WINMM.@]
2579  */
2580 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
2581                                 UINT uSize)
2582 {
2583     LPWINE_MLD          wmld;
2584     UINT                result;
2585
2586     TRACE("(%p, %p, %u);\n", hWaveIn, lpWaveInHdr, uSize);
2587
2588     if (lpWaveInHdr == NULL || uSize < sizeof (WAVEHDR))
2589         return MMSYSERR_INVALPARAM;
2590
2591     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2592         return MMSYSERR_INVALHANDLE;
2593
2594     if ((result = MMDRV_Message(wmld, WIDM_PREPARE, (DWORD_PTR)lpWaveInHdr,
2595                                 uSize)) != MMSYSERR_NOTSUPPORTED)
2596         return result;
2597
2598     if (lpWaveInHdr->dwFlags & WHDR_INQUEUE)
2599         return WAVERR_STILLPLAYING;
2600
2601     lpWaveInHdr->dwFlags |= WHDR_PREPARED;
2602     lpWaveInHdr->dwFlags &= ~WHDR_DONE;
2603     lpWaveInHdr->dwBytesRecorded = 0;
2604
2605     return MMSYSERR_NOERROR;
2606 }
2607
2608 /**************************************************************************
2609  *                              waveInUnprepareHeader   [WINMM.@]
2610  */
2611 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr,
2612                                   UINT uSize)
2613 {
2614     LPWINE_MLD          wmld;
2615     UINT                result;
2616
2617     TRACE("(%p, %p, %u);\n", hWaveIn, lpWaveInHdr, uSize);
2618
2619     if (lpWaveInHdr == NULL || uSize < sizeof (WAVEHDR))
2620         return MMSYSERR_INVALPARAM;
2621
2622     if (!(lpWaveInHdr->dwFlags & WHDR_PREPARED))
2623         return MMSYSERR_NOERROR;
2624
2625     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2626         return MMSYSERR_INVALHANDLE;
2627
2628     if ((result = MMDRV_Message(wmld, WIDM_UNPREPARE, (DWORD_PTR)lpWaveInHdr,
2629                                 uSize)) != MMSYSERR_NOTSUPPORTED)
2630         return result;
2631
2632     if (lpWaveInHdr->dwFlags & WHDR_INQUEUE)
2633         return WAVERR_STILLPLAYING;
2634
2635     lpWaveInHdr->dwFlags &= ~WHDR_PREPARED;
2636     lpWaveInHdr->dwFlags |= WHDR_DONE;
2637
2638     return MMSYSERR_NOERROR;
2639 }
2640
2641 /**************************************************************************
2642  *                              waveInAddBuffer         [WINMM.@]
2643  */
2644 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn,
2645                             WAVEHDR* lpWaveInHdr, UINT uSize)
2646 {
2647     LPWINE_MLD          wmld;
2648
2649     TRACE("(%p, %p, %u);\n", hWaveIn, lpWaveInHdr, uSize);
2650
2651     if (lpWaveInHdr == NULL) return MMSYSERR_INVALPARAM;
2652     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2653         return MMSYSERR_INVALHANDLE;
2654
2655     return MMDRV_Message(wmld, WIDM_ADDBUFFER, (DWORD_PTR)lpWaveInHdr, uSize);
2656 }
2657
2658 /**************************************************************************
2659  *                              waveInReset             [WINMM.@]
2660  */
2661 UINT WINAPI waveInReset(HWAVEIN hWaveIn)
2662 {
2663     LPWINE_MLD          wmld;
2664
2665     TRACE("(%p);\n", hWaveIn);
2666
2667     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2668         return MMSYSERR_INVALHANDLE;
2669
2670     return MMDRV_Message(wmld, WIDM_RESET, 0L, 0L);
2671 }
2672
2673 /**************************************************************************
2674  *                              waveInStart             [WINMM.@]
2675  */
2676 UINT WINAPI waveInStart(HWAVEIN hWaveIn)
2677 {
2678     LPWINE_MLD          wmld;
2679
2680     TRACE("(%p);\n", hWaveIn);
2681
2682     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2683         return MMSYSERR_INVALHANDLE;
2684
2685     return MMDRV_Message(wmld, WIDM_START, 0L, 0L);
2686 }
2687
2688 /**************************************************************************
2689  *                              waveInStop              [WINMM.@]
2690  */
2691 UINT WINAPI waveInStop(HWAVEIN hWaveIn)
2692 {
2693     LPWINE_MLD          wmld;
2694
2695     TRACE("(%p);\n", hWaveIn);
2696
2697     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2698         return MMSYSERR_INVALHANDLE;
2699
2700     return MMDRV_Message(wmld,WIDM_STOP, 0L, 0L);
2701 }
2702
2703 /**************************************************************************
2704  *                              waveInGetPosition       [WINMM.@]
2705  */
2706 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
2707                               UINT uSize)
2708 {
2709     LPWINE_MLD          wmld;
2710
2711     TRACE("(%p, %p, %u);\n", hWaveIn, lpTime, uSize);
2712
2713     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2714         return MMSYSERR_INVALHANDLE;
2715
2716     return MMDRV_Message(wmld, WIDM_GETPOS, (DWORD_PTR)lpTime, uSize);
2717 }
2718
2719 /**************************************************************************
2720  *                              waveInGetID                     [WINMM.@]
2721  */
2722 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
2723 {
2724     LPWINE_MLD          wmld;
2725
2726     TRACE("(%p, %p);\n", hWaveIn, lpuDeviceID);
2727
2728     if (lpuDeviceID == NULL) return MMSYSERR_INVALHANDLE;
2729
2730     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL)
2731         return MMSYSERR_INVALHANDLE;
2732
2733     *lpuDeviceID = wmld->uDeviceID;
2734     return MMSYSERR_NOERROR;
2735 }
2736
2737 /**************************************************************************
2738  *                              waveInMessage           [WINMM.@]
2739  */
2740 UINT WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
2741                           DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2742 {
2743     LPWINE_MLD          wmld;
2744
2745     TRACE("(%p, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
2746
2747     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) {
2748         if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, TRUE)) != NULL) {
2749             return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
2750         }
2751         return MMSYSERR_INVALHANDLE;
2752     }
2753
2754     /* from M$ KB */
2755     if (uMessage < DRVM_IOCTL || (uMessage >= DRVM_IOCTL_LAST && uMessage < DRVM_MAPPER))
2756         return MMSYSERR_INVALPARAM;
2757
2758
2759     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
2760 }
2761
2762 struct mm_starter
2763 {
2764     LPTASKCALLBACK      cb;
2765     DWORD               client;
2766     HANDLE              event;
2767 };
2768
2769 static DWORD WINAPI mmTaskRun(void* pmt)
2770 {
2771     struct mm_starter mms;
2772
2773     memcpy(&mms, pmt, sizeof(struct mm_starter));
2774     HeapFree(GetProcessHeap(), 0, pmt);
2775     mms.cb(mms.client);
2776     if (mms.event) SetEvent(mms.event);
2777     return 0;
2778 }
2779
2780 /******************************************************************
2781  *              mmTaskCreate (WINMM.@)
2782  */
2783 UINT     WINAPI mmTaskCreate(LPTASKCALLBACK cb, HANDLE* ph, DWORD_PTR client)
2784 {
2785     HANDLE               hThread;
2786     HANDLE               hEvent = 0;
2787     struct mm_starter   *mms;
2788
2789     mms = HeapAlloc(GetProcessHeap(), 0, sizeof(struct mm_starter));
2790     if (mms == NULL) return TASKERR_OUTOFMEMORY;
2791
2792     mms->cb = cb;
2793     mms->client = client;
2794     if (ph) hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
2795     mms->event = hEvent;
2796
2797     hThread = CreateThread(0, 0, mmTaskRun, mms, 0, NULL);
2798     if (!hThread) {
2799         HeapFree(GetProcessHeap(), 0, mms);
2800         if (hEvent) CloseHandle(hEvent);
2801         return TASKERR_OUTOFMEMORY;
2802     }
2803     SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
2804     if (ph) *ph = hEvent;
2805     CloseHandle(hThread);
2806     return 0;
2807 }
2808
2809 /******************************************************************
2810  *              mmTaskBlock (WINMM.@)
2811  */
2812 VOID     WINAPI mmTaskBlock(DWORD tid)
2813 {
2814     MSG         msg;
2815
2816     do
2817     {
2818         GetMessageA(&msg, 0, 0, 0);
2819         if (msg.hwnd) DispatchMessageA(&msg);
2820     } while (msg.message != WM_USER);
2821 }
2822
2823 /******************************************************************
2824  *              mmTaskSignal (WINMM.@)
2825  */
2826 BOOL     WINAPI mmTaskSignal(DWORD tid)
2827 {
2828     return PostThreadMessageW(tid, WM_USER, 0, 0);
2829 }
2830
2831 /******************************************************************
2832  *              mmTaskYield (WINMM.@)
2833  */
2834 VOID     WINAPI mmTaskYield(VOID) {}
2835
2836 /******************************************************************
2837  *              mmGetCurrentTask (WINMM.@)
2838  */
2839 DWORD    WINAPI mmGetCurrentTask(VOID)
2840 {
2841     return GetCurrentThreadId();
2842 }