Map PrintDlg16 to PrintDlgA.
[wine] / multimedia / mmsystem.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  * MMSYTEM functions
5  *
6  * Copyright 1993 Martin Ayotte
7  */
8
9 /* 
10  * Eric POUECH : 
11  *      98/9    added Win32 MCI support
12  *      99/4    added mmTask and mmThread functions support
13  *              added midiStream support
14  */
15
16 /* FIXME: I think there are some segmented vs. linear pointer weirdnesses 
17  *        and long term pointers to 16 bit space in here
18  */
19
20 #include <string.h>
21
22 #include "winbase.h"
23 #include "wine/winbase16.h"
24 #include "wownt32.h"
25 #include "heap.h"
26 #include "driver.h"
27 #include "winemm.h"
28 #include "syslevel.h"
29 #include "callback.h"
30 #include "selectors.h"
31 #include "module.h"
32 #include "debugtools.h"
33
34 DEFAULT_DEBUG_CHANNEL(mmsys)
35
36 LONG   WINAPI DrvDefDriverProc(DWORD dwDevID, HDRVR16 hDrv, WORD wMsg, 
37                                DWORD dwParam1, DWORD dwParam2);
38
39 static LPWINE_MM_IDATA          lpFirstIData = NULL;
40
41 static  LPWINE_MM_IDATA MULTIMEDIA_GetIDataNoCheck(void)
42 {
43     DWORD               pid = GetCurrentProcessId();
44     LPWINE_MM_IDATA     iData;
45
46     for (iData = lpFirstIData; iData; iData = iData->lpNextIData) {
47         if (iData->dwThisProcess == pid)
48             break;
49     }
50     return iData;
51 }
52
53 /**************************************************************************
54  *                      MULTIMEDIA_GetIData                     [internal]
55  */
56 LPWINE_MM_IDATA MULTIMEDIA_GetIData(void)
57 {
58     LPWINE_MM_IDATA     iData = MULTIMEDIA_GetIDataNoCheck();
59
60     if (!iData) {
61         ERR("IData not found. Suicide !!!\n");
62         ExitProcess(0);
63     }
64     return iData;
65 }
66
67 /**************************************************************************
68  *                      MULTIMEDIA_CreateIData                  [internal]
69  */
70 static  BOOL    MULTIMEDIA_CreateIData(HINSTANCE hInstDLL)
71 {
72     LPWINE_MM_IDATA     iData;
73         
74     iData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MM_IDATA));
75
76     if (!iData)
77         return FALSE;
78     iData->hWinMM32Instance = hInstDLL;
79     iData->dwThisProcess = GetCurrentProcessId();
80     iData->lpNextIData = lpFirstIData;
81     lpFirstIData = iData;
82     InitializeCriticalSection(&iData->cs);
83     return TRUE;
84 }
85
86 /**************************************************************************
87  *                      MULTIMEDIA_DeleteIData                  [internal]
88  */
89 static  void MULTIMEDIA_DeleteIData(void)
90 {
91     LPWINE_MM_IDATA     iData = MULTIMEDIA_GetIDataNoCheck();
92     LPWINE_MM_IDATA*    ppid;
93             
94     if (iData) {
95         for (ppid = &lpFirstIData; *ppid; ppid = &(*ppid)->lpNextIData) {
96             if (*ppid == iData) {
97                 *ppid = iData->lpNextIData;
98                 break;
99             }
100         }
101         /* FIXME: should also free content and resources allocated 
102          * inside iData */
103         HeapFree(GetProcessHeap(), 0, iData);
104     }
105 }
106
107 /**************************************************************************
108  *                      WINMM_LibMain                           [EntryPoint]
109  *
110  * WINMM DLL entry point
111  *
112  */
113 BOOL WINAPI WINMM_LibMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
114 {
115     static BOOL                 bInitDone = FALSE;
116
117     TRACE("0x%x 0x%lx %p\n", hInstDLL, fdwReason, fImpLoad);
118
119     switch (fdwReason) {
120     case DLL_PROCESS_ATTACH:
121         if (!MULTIMEDIA_CreateIData(hInstDLL))
122             return FALSE;
123         if (!bInitDone) { /* to be done only once */
124             if (!MULTIMEDIA_MciInit() || !MMDRV_Init()) {
125                 MULTIMEDIA_DeleteIData();
126                 return FALSE;
127             }
128             bInitDone = TRUE;   
129         }
130         break;
131     case DLL_PROCESS_DETACH:
132         MULTIMEDIA_DeleteIData();
133         break;
134     case DLL_THREAD_ATTACH:
135     case DLL_THREAD_DETACH:
136         break;
137     }
138     return TRUE;
139 }
140
141 /**************************************************************************
142  *                      MMSYSTEM_LibMain                        [EntryPoint]
143  *
144  * MMSYSTEM DLL entry point
145  *
146  */
147 BOOL WINAPI MMSYSTEM_LibMain(DWORD fdwReason, HINSTANCE hinstDLL, WORD ds, 
148                              WORD wHeapSize, DWORD dwReserved1, WORD wReserved2)
149 {
150     HANDLE                      hndl;
151     LPWINE_MM_IDATA             iData;
152
153     TRACE("0x%x 0x%lx\n", hinstDLL, fdwReason);
154
155     switch (fdwReason) {
156     case DLL_PROCESS_ATTACH:
157         /* need to load WinMM in order to:
158          * - correctly initiates shared variables (MULTIMEDIA_Init())
159          * - correctly creates the per process WINE_MM_IDATA chunk
160          */
161         hndl = LoadLibraryA("WINMM.DLL");
162         
163         if (!hndl) {
164             ERR("Could not load sibling WinMM.dll\n");
165             return FALSE;
166         }
167         iData = MULTIMEDIA_GetIData();
168         iData->hWinMM16Instance = hinstDLL;
169         iData->h16Module32 = hndl;
170         break;
171     case DLL_PROCESS_DETACH:
172         iData = MULTIMEDIA_GetIData();
173         FreeLibrary(iData->h16Module32);
174         break;
175     case DLL_THREAD_ATTACH:
176     case DLL_THREAD_DETACH:
177         break;
178     }
179     return TRUE;
180 }
181
182 /**************************************************************************
183  *                              MMSYSTEM_WEP                    [MMSYSTEM.1]
184  */
185 int WINAPI MMSYSTEM_WEP(HINSTANCE16 hInstance, WORD wDataSeg,
186                         WORD cbHeapSize, LPSTR lpCmdLine)
187 {
188     FIXME("STUB: Unloading MMSystem DLL ... hInst=%04X \n", hInstance);
189     return TRUE;
190 }
191
192 void MMSYSTEM_MMTIME32to16(LPMMTIME16 mmt16, const MMTIME* mmt32) 
193 {
194     mmt16->wType = mmt32->wType;
195     /* layout of rest is the same for 32/16,
196      * Note: mmt16->u is 2 bytes smaller than mmt32->u, which has padding
197      */
198     memcpy(&(mmt16->u), &(mmt32->u), sizeof(mmt16->u));
199 }
200
201 void MMSYSTEM_MMTIME16to32(LPMMTIME mmt32, const MMTIME16* mmt16) 
202 {
203     mmt32->wType = mmt16->wType;
204     /* layout of rest is the same for 32/16,
205      * Note: mmt16->u is 2 bytes smaller than mmt32->u, which has padding
206      */
207     memcpy(&(mmt32->u), &(mmt16->u), sizeof(mmt16->u));
208 }
209
210 static HANDLE           PlaySound_hThread = 0;
211 static HANDLE           PlaySound_hPlayEvent = 0;
212 static HANDLE           PlaySound_hReadyEvent = 0;
213 static HANDLE           PlaySound_hMiddleEvent = 0;
214 static BOOL             PlaySound_Result = FALSE;
215 static int              PlaySound_Stop = FALSE;
216 static int              PlaySound_Playing = FALSE;
217
218 static LPCSTR           PlaySound_pszSound = NULL;
219 static HMODULE          PlaySound_hmod = 0;
220 static DWORD            PlaySound_fdwSound = 0;
221 static int              PlaySound_Loop = FALSE;
222 static int              PlaySound_SearchMode = 0; /* 1 - sndPlaySound search order
223                                                      2 - PlaySound order */
224
225 static HMMIO    get_mmioFromFile(LPCSTR lpszName)
226 {
227     return mmioOpenA((LPSTR)lpszName, NULL,
228                      MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
229 }
230
231 static HMMIO    get_mmioFromProfile(UINT uFlags, LPCSTR lpszName) 
232 {
233     char        str[128];
234     LPSTR       ptr;
235     HMMIO       hmmio;
236     
237     TRACE("searching in SystemSound List !\n");
238     GetProfileStringA("Sounds", (LPSTR)lpszName, "", str, sizeof(str));
239     if (strlen(str) == 0) {
240         if (uFlags & SND_NODEFAULT) return 0;
241         GetProfileStringA("Sounds", "Default", "", str, sizeof(str));
242         if (strlen(str) == 0) return 0;
243     }
244     if ((ptr = (LPSTR)strchr(str, ',')) != NULL) *ptr = '\0';
245     hmmio = get_mmioFromFile(str);
246     if (hmmio == 0) {
247         WARN("can't find SystemSound='%s' !\n", str);
248         return 0;
249     }
250     return hmmio;
251 }
252
253 static BOOL WINAPI proc_PlaySound(LPCSTR lpszSoundName, UINT uFlags)
254 {
255     BOOL                bRet = FALSE;
256     HMMIO               hmmio;
257     MMCKINFO            ckMainRIFF;
258     
259     TRACE("SoundName='%s' uFlags=%04X !\n", lpszSoundName, uFlags);
260     if (lpszSoundName == NULL) {
261         TRACE("Stop !\n");
262         return FALSE;
263     }
264     if (uFlags & SND_MEMORY) {
265         MMIOINFO        mminfo;
266         memset(&mminfo, 0, sizeof(mminfo));
267         mminfo.fccIOProc = FOURCC_MEM;
268         mminfo.pchBuffer = (LPSTR)lpszSoundName;
269         mminfo.cchBuffer = -1;
270         TRACE("Memory sound %p\n", lpszSoundName);
271         hmmio = mmioOpenA(NULL, &mminfo, MMIO_READ);
272     } else {
273         hmmio = 0;
274         if (uFlags & SND_ALIAS)
275             if ((hmmio=get_mmioFromProfile(uFlags, lpszSoundName)) == 0) 
276                 return FALSE;
277         
278         if (uFlags & SND_FILENAME)
279             if ((hmmio=get_mmioFromFile(lpszSoundName)) == 0) return FALSE;
280         
281         if (PlaySound_SearchMode == 1) {
282             PlaySound_SearchMode = 0;
283             if ((hmmio=get_mmioFromFile(lpszSoundName)) == 0) 
284                 if ((hmmio=get_mmioFromProfile(uFlags, lpszSoundName)) == 0) 
285                     return FALSE;
286         }
287         
288         if (PlaySound_SearchMode == 2) {
289             PlaySound_SearchMode = 0;
290             if ((hmmio=get_mmioFromProfile(uFlags | SND_NODEFAULT, lpszSoundName)) == 0) 
291                 if ((hmmio=get_mmioFromFile(lpszSoundName)) == 0)       
292                     if ((hmmio=get_mmioFromProfile(uFlags, lpszSoundName)) == 0) return FALSE;
293         }
294     }
295     
296     if (mmioDescend(hmmio, &ckMainRIFF, NULL, 0) == 0) {
297         DWORD   dwPos = mmioSeek(hmmio, 0, SEEK_CUR);
298         do {
299             TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08lX \n",
300                   (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, 
301                   ckMainRIFF.cksize);
302
303             mmioSeek(hmmio, dwPos, SEEK_SET);
304
305             if ((ckMainRIFF.ckid == FOURCC_RIFF) &&
306                 (ckMainRIFF.fccType == mmioFOURCC('W', 'A', 'V', 'E'))) {
307                 MMCKINFO        mmckInfo;
308                 
309                 mmckInfo.ckid = mmioFOURCC('f', 'm', 't', ' ');
310                 
311                 if (mmioDescend(hmmio, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK) == 0) {
312                     PCMWAVEFORMAT           pcmWaveFormat;
313                     
314                     TRACE("Chunk Found ckid=%.4s fccType=%.4s cksize=%08lX \n",
315                           (LPSTR)&mmckInfo.ckid, (LPSTR)&mmckInfo.fccType, mmckInfo.cksize);
316                     
317                     if (mmioRead(hmmio, (HPSTR)&pcmWaveFormat,
318                                  (long) sizeof(PCMWAVEFORMAT)) == (long) sizeof(PCMWAVEFORMAT)) {
319                         TRACE("wFormatTag=%04X !\n", pcmWaveFormat.wf.wFormatTag);
320                         TRACE("nChannels=%d \n", pcmWaveFormat.wf.nChannels);
321                         TRACE("nSamplesPerSec=%ld\n", pcmWaveFormat.wf.nSamplesPerSec);
322                         TRACE("nAvgBytesPerSec=%ld\n", pcmWaveFormat.wf.nAvgBytesPerSec);
323                         TRACE("nBlockAlign=%d \n", pcmWaveFormat.wf.nBlockAlign);
324                         TRACE("wBitsPerSample=%u !\n", pcmWaveFormat.wBitsPerSample);
325                         
326                         mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
327                         /* move to end of 'fmt ' chunk */
328                         mmioSeek(hmmio, mmckInfo.dwDataOffset + ((mmckInfo.cksize + 1) & ~1), SEEK_SET);
329
330                         if (mmioDescend(hmmio, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK) == 0) {
331                             DWORD               dwRet;
332                             HWAVE               hWave;
333
334                             TRACE("Chunk Found ckid=%.4s fccType=%.4s cksize=%08lX\n", 
335                                   (LPSTR)&mmckInfo.ckid, (LPSTR)&mmckInfo.fccType, mmckInfo.cksize);
336                             
337                             pcmWaveFormat.wf.nAvgBytesPerSec = pcmWaveFormat.wf.nSamplesPerSec * 
338                                 pcmWaveFormat.wf.nBlockAlign;
339                             
340                             dwRet = waveOutOpen(&hWave, WAVE_MAPPER, (LPWAVEFORMATEX)&pcmWaveFormat, 0L, 0L, CALLBACK_NULL);
341                             if (dwRet == MMSYSERR_NOERROR) {
342                                 WAVEHDR         waveHdr;
343                                 HGLOBAL16       hData;
344                                 INT             count, bufsize, left = mmckInfo.cksize;
345                                 
346                                 bufsize = 64000;
347                                 hData = GlobalAlloc16(GMEM_MOVEABLE, bufsize);
348                                 waveHdr.lpData = (LPSTR)GlobalLock16(hData);
349                                 waveHdr.dwBufferLength = bufsize;
350                                 waveHdr.dwUser = 0L;
351                                 waveHdr.dwFlags = 0L;
352                                 waveHdr.dwLoops = 0L;
353                                 
354                                 dwRet = waveOutPrepareHeader(hWave, &waveHdr, sizeof(WAVEHDR));
355                                 if (dwRet == MMSYSERR_NOERROR) {
356                                     while (left) {
357                                         if (PlaySound_Stop) {
358                                             PlaySound_Stop = FALSE;
359                                             PlaySound_Loop = FALSE;
360                                             break;
361                                         }
362                                         if (bufsize > left) bufsize = left;
363                                         count = mmioRead(hmmio, waveHdr.lpData, bufsize);
364                                         if (count < 1) break;
365                                         left -= count;
366                                         waveHdr.dwBufferLength = count;
367                                         /* waveHdr.dwBytesRecorded = count; */
368                                         /* FIXME: doesn't expect async ops */ 
369                                         waveOutWrite(hWave, &waveHdr, sizeof(WAVEHDR));
370                                         while (!(waveHdr.dwFlags & WHDR_DONE))
371                                             Sleep(10);
372                                     }
373                                     waveOutUnprepareHeader(hWave, &waveHdr, sizeof(WAVEHDR));
374                                     while (waveOutClose(hWave) == WAVERR_STILLPLAYING)
375                                         Sleep(100);
376                                     
377                                     bRet = TRUE;
378                                 } else 
379                                     WARN("can't prepare WaveOut device !\n");
380                                 
381                                 GlobalUnlock16(hData);
382                                 GlobalFree16(hData);
383                             }
384                         }
385                     }
386                 }
387             }
388         } while (PlaySound_Loop);
389     }
390
391     if (hmmio != 0) mmioClose(hmmio, 0);
392     return bRet;
393 }
394
395 static DWORD WINAPI PlaySound_Thread(LPVOID arg) 
396 {
397     DWORD     res;
398     
399     for (;;) {
400         PlaySound_Playing = FALSE;
401         SetEvent(PlaySound_hReadyEvent);
402         res = WaitForSingleObject(PlaySound_hPlayEvent, INFINITE);
403         ResetEvent(PlaySound_hReadyEvent);
404         SetEvent(PlaySound_hMiddleEvent);
405         if (res == WAIT_FAILED) ExitThread(2);
406         if (res != WAIT_OBJECT_0) continue;
407         PlaySound_Playing = TRUE;
408         
409         if ((PlaySound_fdwSound & SND_RESOURCE) == SND_RESOURCE) {
410             HRSRC       hRES;
411             HGLOBAL     hGLOB;
412             void*       ptr;
413
414             if ((hRES = FindResourceA(PlaySound_hmod, PlaySound_pszSound, "WAVE")) == 0) {
415                 PlaySound_Result = FALSE;
416                 continue;
417             }
418             if ((hGLOB = LoadResource(PlaySound_hmod, hRES)) == 0) {
419                 PlaySound_Result = FALSE;
420                 continue;
421             }
422             if ((ptr = LockResource(hGLOB)) == NULL) {
423                 FreeResource(hGLOB);
424                 PlaySound_Result = FALSE;
425                 continue;
426             }
427             PlaySound_Result = proc_PlaySound(ptr, 
428                                               ((UINT16)PlaySound_fdwSound ^ SND_RESOURCE) | SND_MEMORY);
429             FreeResource(hGLOB);
430             continue;
431         }
432         PlaySound_Result = proc_PlaySound(PlaySound_pszSound, (UINT16)PlaySound_fdwSound);
433     }
434 }
435
436 /**************************************************************************
437  *                              PlaySoundA              [WINMM.1]
438  */
439 BOOL WINAPI PlaySoundA(LPCSTR pszSound, HMODULE hmod, DWORD fdwSound)
440 {
441     static LPSTR StrDup = NULL;
442     
443     TRACE("pszSound='%p' hmod=%04X fdwSound=%08lX\n",
444           pszSound, hmod, fdwSound);
445     
446     if (PlaySound_hThread == 0) { /* This is the first time they called us */
447         DWORD   id;
448         if ((PlaySound_hReadyEvent = CreateEventA(NULL, TRUE, FALSE, NULL)) == 0)
449             return FALSE;
450         if ((PlaySound_hMiddleEvent = CreateEventA(NULL, FALSE, FALSE, NULL)) == 0)
451             return FALSE;
452         if ((PlaySound_hPlayEvent = CreateEventA(NULL, FALSE, FALSE, NULL)) == 0)
453             return FALSE;
454         if ((PlaySound_hThread = CreateThread(NULL, 0, PlaySound_Thread, 0, 0, &id)) == 0) 
455             return FALSE;
456     }
457     
458     /* FIXME? I see no difference between SND_WAIT and SND_NOSTOP ! */ 
459     if ((fdwSound & (SND_NOWAIT | SND_NOSTOP)) && PlaySound_Playing) 
460         return FALSE;
461     
462     /* Trying to stop if playing */
463     if (PlaySound_Playing) PlaySound_Stop = TRUE;
464     
465     /* Waiting playing thread to get ready. I think 10 secs is ok & if not then leave*/
466     if (WaitForSingleObject(PlaySound_hReadyEvent, 1000*10) != WAIT_OBJECT_0)
467         return FALSE;
468     
469     if (!pszSound || (fdwSound & SND_PURGE)) 
470         return FALSE; /* We stoped playing so leaving */
471     
472     if (PlaySound_SearchMode != 1) PlaySound_SearchMode = 2;
473     if (!(fdwSound & SND_ASYNC)) {
474         if (fdwSound & SND_LOOP) 
475             return FALSE;
476         PlaySound_pszSound = pszSound;
477         PlaySound_hmod = hmod;
478         PlaySound_fdwSound = fdwSound;
479         PlaySound_Result = FALSE;
480         SetEvent(PlaySound_hPlayEvent);
481         if (WaitForSingleObject(PlaySound_hMiddleEvent, INFINITE) != WAIT_OBJECT_0) 
482             return FALSE;
483         if (WaitForSingleObject(PlaySound_hReadyEvent, INFINITE) != WAIT_OBJECT_0) 
484             return FALSE;
485         return PlaySound_Result;
486     } else {
487         PlaySound_hmod = hmod;
488         PlaySound_fdwSound = fdwSound;
489         PlaySound_Result = FALSE;
490         if (StrDup) {
491             HeapFree(GetProcessHeap(), 0, StrDup);
492             StrDup = NULL;
493         }
494         if (!((fdwSound & SND_MEMORY) || ((fdwSound & SND_RESOURCE) && 
495                                           !((DWORD)pszSound >> 16)) || !pszSound)) {
496             StrDup = HEAP_strdupA(GetProcessHeap(), 0,pszSound);
497             PlaySound_pszSound = StrDup;
498         } else PlaySound_pszSound = pszSound;
499         PlaySound_Loop = fdwSound & SND_LOOP;
500         SetEvent(PlaySound_hPlayEvent);
501         ResetEvent(PlaySound_hMiddleEvent);
502         return TRUE;
503     }
504     return FALSE;
505 }
506
507 /**************************************************************************
508  *                              PlaySoundW              [WINMM.18]
509  */
510 BOOL WINAPI PlaySoundW(LPCWSTR pszSound, HMODULE hmod, DWORD fdwSound)
511 {
512     LPSTR       pszSoundA;
513     BOOL        bSound;
514     
515     if (!((fdwSound & SND_MEMORY) || ((fdwSound & SND_RESOURCE) && 
516                                       !((DWORD)pszSound >> 16)) || !pszSound)) {
517         pszSoundA = HEAP_strdupWtoA(GetProcessHeap(), 0,pszSound);
518         bSound = PlaySoundA(pszSoundA, hmod, fdwSound);
519         HeapFree(GetProcessHeap(), 0,pszSoundA);
520     } else  
521         bSound = PlaySoundA((LPCSTR)pszSound, hmod, fdwSound);
522     
523     return bSound;
524 }
525
526 /**************************************************************************
527  *                              PlaySound16             [MMSYSTEM.3]
528  */
529 BOOL16 WINAPI PlaySound16(LPCSTR pszSound, HMODULE16 hmod, DWORD fdwSound)
530 {
531     BOOL16 retv;
532
533     SYSLEVEL_ReleaseWin16Lock();
534     retv = PlaySoundA( pszSound, hmod, fdwSound );
535     SYSLEVEL_RestoreWin16Lock();
536
537     return retv;
538 }
539
540 /**************************************************************************
541  *                              sndPlaySoundA           [WINMM135]
542  */
543 BOOL WINAPI sndPlaySoundA(LPCSTR lpszSoundName, UINT uFlags)
544 {
545     PlaySound_SearchMode = 1;
546     return PlaySoundA(lpszSoundName, 0, uFlags);
547 }
548
549 /**************************************************************************
550  *                              sndPlaySoundW           [WINMM.136]
551  */
552 BOOL WINAPI sndPlaySoundW(LPCWSTR lpszSoundName, UINT uFlags)
553 {
554     PlaySound_SearchMode = 1;
555     return PlaySoundW(lpszSoundName, 0, uFlags);
556 }
557
558 /**************************************************************************
559  *                              sndPlaySound16          [MMSYSTEM.2]
560  */
561 BOOL16 WINAPI sndPlaySound16(LPCSTR lpszSoundName, UINT16 uFlags)
562 {
563     BOOL16 retv;
564
565     SYSLEVEL_ReleaseWin16Lock();
566     retv = sndPlaySoundA( lpszSoundName, uFlags );
567     SYSLEVEL_RestoreWin16Lock();
568
569     return retv;
570 }
571
572
573 /**************************************************************************
574  *                              mmsystemGetVersion      [WINMM.134]
575  */
576 UINT WINAPI mmsystemGetVersion(void)
577 {
578     return mmsystemGetVersion16();
579 }
580
581 /**************************************************************************
582  *                              mmsystemGetVersion      [MMSYSTEM.5]
583  * return value borrowed from Win95 winmm.dll ;)
584  */
585 UINT16 WINAPI mmsystemGetVersion16(void)
586 {
587     TRACE("3.10 (Win95?)\n");
588     return 0x030a;
589 }
590
591 /**************************************************************************
592  *                              DriverProc                      [MMSYSTEM.6]
593  */
594 LRESULT WINAPI DriverProc16(DWORD dwDevID, HDRVR16 hDrv, WORD wMsg, 
595                             DWORD dwParam1, DWORD dwParam2)
596 {
597     TRACE("dwDevID=%08lx hDrv=%04x wMsg=%04x dwParam1=%08lx dwParam2=%08lx\n",
598           dwDevID, hDrv, wMsg, dwParam1, dwParam2);
599
600     return DrvDefDriverProc(dwDevID, hDrv, wMsg, dwParam1, dwParam2);
601 }
602
603 /**************************************************************************
604  *                              DriverCallback                  [MMSYSTEM.31]
605  */
606 BOOL WINAPI DriverCallback(DWORD dwCallBack, UINT uFlags, HDRVR hDev, 
607                            UINT wMsg, DWORD dwUser, DWORD dwParam1, 
608                            DWORD dwParam2)
609 {
610     TRACE("(%08lX, %04X, %04X, %04X, %08lX, %08lX, %08lX); !\n",
611           dwCallBack, uFlags, hDev, wMsg, dwUser, dwParam1, dwParam2);
612
613     switch (uFlags & DCB_TYPEMASK) {
614     case DCB_NULL:
615         TRACE("Null !\n");
616         break;
617     case DCB_WINDOW:
618         TRACE("Window(%04lX) handle=%04X!\n", dwCallBack, hDev);
619         if (!IsWindow(dwCallBack))
620             return FALSE;
621         Callout.PostMessageA((HWND16)dwCallBack, wMsg, hDev, dwParam1);
622         break;
623     case DCB_TASK: /* aka DCB_THREAD */
624         TRACE("Task(%04lx) !\n", dwCallBack);
625         Callout.PostThreadMessageA(dwCallBack, wMsg, hDev, dwParam1);
626         break;
627     case DCB_FUNCTION:
628         TRACE("Function (32 bit) !\n");
629         ((LPDRVCALLBACK)dwCallBack)(hDev, wMsg, dwUser, dwParam1, dwParam2);
630         break;
631     case DCB_EVENT:
632         TRACE("Event(%08lx) !\n", dwCallBack);
633         SetEvent((HANDLE)dwCallBack);
634         break;
635     case 6: /* I would dub it DCB_MMTHREADSIGNAL */
636         /* this is an undocumented DCB_ value used for mmThreads
637          * loword of dwCallBack contains the handle of the lpMMThd block
638          * which dwSignalCount has to be incremented
639          */
640         {
641             WINE_MMTHREAD*      lpMMThd = (WINE_MMTHREAD*)PTR_SEG_OFF_TO_LIN(LOWORD(dwCallBack), 0);
642
643             TRACE("mmThread (%04x, %p) !\n", LOWORD(dwCallBack), lpMMThd);
644             /* same as mmThreadSignal16 */
645             InterlockedIncrement(&lpMMThd->dwSignalCount);
646             SetEvent(lpMMThd->hEvent);
647             /* some other stuff on lpMMThd->hVxD */
648         }
649         break;  
650 #if 0
651     case 4:
652         /* this is an undocumented DCB_ value for... I don't know */
653         break;
654 #endif
655     default:
656         WARN("Unknown callback type %d\n", uFlags & DCB_TYPEMASK);
657         return FALSE;
658     }
659     TRACE("Done\n");
660     return TRUE;
661 }
662
663 /**************************************************************************
664  *                              DriverCallback                  [MMSYSTEM.31]
665  */
666 BOOL16 WINAPI DriverCallback16(DWORD dwCallBack, UINT16 uFlags, HDRVR16 hDev, 
667                                WORD wMsg, DWORD dwUser, DWORD dwParam1, 
668                                DWORD dwParam2)
669 {
670     return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwUser, dwParam1, dwParam2);
671 }
672
673 /**************************************************************************
674  *      Mixer devices. New to Win95
675  */
676
677 /**************************************************************************
678  * find out the real mixer ID depending on hmix (depends on dwFlags)
679  */
680 static LPWINE_MIXER MIXER_GetDev(HMIXEROBJ hmix, DWORD dwFlags) 
681 {
682     LPWINE_MIXER        lpwm = NULL;
683
684     switch (dwFlags & 0xF0000000ul) {
685     case MIXER_OBJECTF_MIXER:
686         lpwm = (LPWINE_MIXER)MMDRV_Get(hmix, MMDRV_MIXER, TRUE);
687         break;
688     case MIXER_OBJECTF_HMIXER:
689         lpwm = (LPWINE_MIXER)MMDRV_Get(hmix, MMDRV_MIXER, FALSE);
690         break;
691     case MIXER_OBJECTF_WAVEOUT:
692         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEOUT, TRUE,  MMDRV_MIXER);
693         break;
694     case MIXER_OBJECTF_HWAVEOUT:
695         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEOUT, FALSE, MMDRV_MIXER);
696         break;
697     case MIXER_OBJECTF_WAVEIN:
698         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEIN,  TRUE,  MMDRV_MIXER);
699         break;
700     case MIXER_OBJECTF_HWAVEIN:
701         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_WAVEIN,  FALSE, MMDRV_MIXER);
702         break;
703     case MIXER_OBJECTF_MIDIOUT:
704         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIOUT, TRUE,  MMDRV_MIXER);
705         break;
706     case MIXER_OBJECTF_HMIDIOUT:
707         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIOUT, FALSE, MMDRV_MIXER);
708         break;
709     case MIXER_OBJECTF_MIDIIN:
710         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIIN,  TRUE,  MMDRV_MIXER);
711         break;
712     case MIXER_OBJECTF_HMIDIIN:
713         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_MIDIIN,  FALSE, MMDRV_MIXER);
714         break;
715     case MIXER_OBJECTF_AUX:
716         lpwm = (LPWINE_MIXER)MMDRV_GetRelated(hmix, MMDRV_AUX,     TRUE,  MMDRV_MIXER);
717         break;
718     default:
719         FIXME("Unsupported flag (%08lx)\n", dwFlags & 0xF0000000ul);
720         break;
721     }
722     return lpwm;
723 }
724
725 /**************************************************************************
726  *                              mixerGetNumDevs                 [WINMM.108]
727  */
728 UINT WINAPI mixerGetNumDevs(void) 
729 {
730     return MMDRV_GetNum(MMDRV_MIXER);
731 }
732
733 /**************************************************************************
734  *                              mixerGetNumDevs                 [MMSYSTEM.800]
735  */
736 UINT16 WINAPI mixerGetNumDevs16(void) 
737 {
738     return MMDRV_GetNum(MMDRV_MIXER);
739 }
740
741 /**************************************************************************
742  *                              mixerGetDevCapsA                [WINMM.101]
743  */
744 UINT WINAPI mixerGetDevCapsA(UINT devid, LPMIXERCAPSA mixcaps, UINT size) 
745 {
746     LPWINE_MLD  wmld;
747
748     if ((wmld = MMDRV_Get(devid, MMDRV_MIXER, TRUE)) == NULL)
749         return MMSYSERR_BADDEVICEID;
750
751     return MMDRV_Message(wmld, MXDM_GETDEVCAPS, (DWORD)mixcaps, size, TRUE);
752 }
753
754 /**************************************************************************
755  *                              mixerGetDevCapsW                [WINMM.102]
756  */
757 UINT WINAPI mixerGetDevCapsW(UINT devid, LPMIXERCAPSW mixcaps, UINT size) 
758 {
759     MIXERCAPSA  micA;
760     UINT        ret = mixerGetDevCapsA(devid, &micA, sizeof(micA));
761
762     if (ret == MMSYSERR_NOERROR) {
763         mixcaps->wMid           = micA.wMid;
764         mixcaps->wPid           = micA.wPid;
765         mixcaps->vDriverVersion = micA.vDriverVersion;
766         lstrcpyAtoW(mixcaps->szPname, micA.szPname);
767         mixcaps->fdwSupport     = micA.fdwSupport;
768         mixcaps->cDestinations  = micA.cDestinations;
769     }
770     return ret;
771 }
772
773 /**************************************************************************
774  *                              mixerGetDevCaps                 [MMSYSTEM.801]
775  */
776 UINT16 WINAPI mixerGetDevCaps16(UINT16 devid, LPMIXERCAPS16 mixcaps, 
777                                 UINT16 size) 
778 {
779     MIXERCAPSA  micA;
780     UINT        ret = mixerGetDevCapsA(devid, &micA, sizeof(micA));
781     
782     if (ret == MMSYSERR_NOERROR) {
783         mixcaps->wMid           = micA.wMid;
784         mixcaps->wPid           = micA.wPid;
785         mixcaps->vDriverVersion = micA.vDriverVersion;
786         strcpy(mixcaps->szPname, micA.szPname);
787         mixcaps->fdwSupport     = micA.fdwSupport;
788         mixcaps->cDestinations  = micA.cDestinations;
789     }
790     return ret;
791 }
792
793 static  UINT  MMSYSTEM_mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD dwCallback,
794                                  DWORD dwInstance, DWORD fdwOpen, BOOL bFrom32) 
795 {
796     HANDLE              hMix;
797     LPWINE_MLD          wmld;
798     DWORD               dwRet = 0;
799     MIXEROPENDESC       mod;
800
801     TRACE("(%p, %d, %08lx, %08lx, %08lx)\n",
802           lphMix, uDeviceID, dwCallback, dwInstance, fdwOpen);
803
804     wmld = MMDRV_Alloc(sizeof(WINE_MIXER), MMDRV_MIXER, &hMix, &fdwOpen,
805                        &dwCallback, &dwInstance, bFrom32);
806
807     wmld->uDeviceID = uDeviceID;
808     mod.hmx = hMix;
809     mod.dwCallback = dwCallback;
810     mod.dwInstance = dwInstance;
811
812     dwRet = MMDRV_Open(wmld, MXDM_OPEN, (DWORD)&mod, fdwOpen);
813
814     if (dwRet != MMSYSERR_NOERROR) {
815         MMDRV_Free(hMix, wmld);
816         hMix = 0;
817     }
818     if (lphMix) *lphMix = hMix;
819     TRACE("=> %ld hMixer=%04x\n", dwRet, hMix);
820
821     return dwRet;
822 }
823
824 /**************************************************************************
825  *                              mixerOpen                       [WINMM.110]
826  */
827 UINT WINAPI mixerOpen(LPHMIXER lphMix, UINT uDeviceID, DWORD dwCallback,
828                       DWORD dwInstance, DWORD fdwOpen) 
829 {
830     return MMSYSTEM_mixerOpen(lphMix, uDeviceID, 
831                               dwCallback, dwInstance, fdwOpen, TRUE);
832 }
833
834 /**************************************************************************
835  *                              mixerOpen                       [MMSYSTEM.803]
836  */
837 UINT16 WINAPI mixerOpen16(LPHMIXER16 lphmix, UINT16 uDeviceID, DWORD dwCallback,
838                           DWORD dwInstance, DWORD fdwOpen) 
839 {
840     HMIXER      hmix;
841     UINT        ret;
842     
843     ret = MMSYSTEM_mixerOpen(&hmix, uDeviceID, 
844                              dwCallback, dwInstance, fdwOpen, FALSE);
845     if (lphmix) *lphmix = hmix;
846     return ret;
847 }
848
849 /**************************************************************************
850  *                              mixerClose                      [WINMM.98]
851  */
852 UINT WINAPI mixerClose(HMIXER hMix) 
853 {
854     LPWINE_MLD          wmld;
855     DWORD               dwRet;    
856     
857     TRACE("(%04x)\n", hMix);
858
859     if ((wmld = MMDRV_Get(hMix, MMDRV_MIXER, FALSE)) == NULL) return MMSYSERR_INVALHANDLE;
860
861     dwRet = MMDRV_Close(wmld, MXDM_CLOSE);
862     MMDRV_Free(hMix, wmld);
863
864     return dwRet;
865 }
866
867 /**************************************************************************
868  *                              mixerClose                      [MMSYSTEM.803]
869  */
870 UINT16 WINAPI mixerClose16(HMIXER16 hMix) 
871 {
872     return mixerClose(hMix);
873 }
874
875 /**************************************************************************
876  *                              mixerGetID                      [WINMM.103]
877  */
878 UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID) 
879 {
880     LPWINE_MIXER        lpwm;
881
882     TRACE("(%04x %p %08lx)\n", hmix, lpid, fdwID);
883
884     if ((lpwm = MIXER_GetDev(hmix, fdwID)) == NULL) {
885         return MMSYSERR_INVALHANDLE;
886     }
887
888     if (lpid)
889       *lpid = lpwm->mld.uDeviceID;
890
891     return MMSYSERR_NOERROR;
892 }
893
894 /**************************************************************************
895  *                              mixerGetID
896  */
897 UINT16 WINAPI mixerGetID16(HMIXEROBJ16 hmix, LPUINT16 lpid, DWORD fdwID) 
898 {
899     UINT        xid;    
900     UINT        ret = mixerGetID(hmix, &xid, fdwID);
901
902     if (lpid) 
903         *lpid = xid;
904     return ret;
905 }
906
907 /**************************************************************************
908  *                              mixerGetControlDetailsA         [WINMM.99]
909  */
910 UINT WINAPI mixerGetControlDetailsA(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA,
911                                     DWORD fdwDetails) 
912 {
913     LPWINE_MIXER        lpwm;
914
915     TRACE("(%04x, %p, %08lx)\n", hmix, lpmcdA, fdwDetails);
916
917     if ((lpwm = MIXER_GetDev(hmix, fdwDetails)) == NULL) 
918         return MMSYSERR_INVALHANDLE;
919
920     if (lpmcdA == NULL || lpmcdA->cbStruct != sizeof(*lpmcdA))
921         return MMSYSERR_INVALPARAM;
922
923     return MMDRV_Message(&lpwm->mld, MXDM_GETCONTROLDETAILS, (DWORD)lpmcdA, 
924                          fdwDetails, TRUE);
925 }
926
927 /**************************************************************************
928  *                              mixerGetControlDetailsW [WINMM.100]
929  */
930 UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcd, DWORD fdwDetails) 
931 {
932     DWORD                       ret = MMSYSERR_NOTENABLED;
933
934     TRACE("(%04x, %p, %08lx)\n", hmix, lpmcd, fdwDetails);
935
936     if (lpmcd == NULL || lpmcd->cbStruct != sizeof(*lpmcd))
937         return MMSYSERR_INVALPARAM;
938
939     switch (fdwDetails & MIXER_GETCONTROLDETAILSF_QUERYMASK) {
940     case MIXER_GETCONTROLDETAILSF_VALUE:
941         /* can savely use W structure as it is, no string inside */
942         ret = mixerGetControlDetailsA(hmix, lpmcd, fdwDetails);
943         break;
944     case MIXER_GETCONTROLDETAILSF_LISTTEXT:
945         {
946             LPVOID      paDetailsW = lpmcd->paDetails;
947             int         size = MAX(1, lpmcd->cChannels) * sizeof(MIXERCONTROLDETAILS_LISTTEXTA);
948
949             if (lpmcd->u.cMultipleItems != 0 && lpmcd->u.cMultipleItems != lpmcd->u.hwndOwner) {
950                 size *= lpmcd->u.cMultipleItems;
951             }
952             lpmcd->paDetails = HeapAlloc(GetProcessHeap(), 0, size);
953             /* set up lpmcd->paDetails */
954             ret = mixerGetControlDetailsA(hmix, lpmcd, fdwDetails);
955             /* copy from lpmcd->paDetails back to paDetailsW; */
956             HeapFree(GetProcessHeap(), 0, lpmcd->paDetails);
957             lpmcd->paDetails = paDetailsW;
958         }
959         break;
960     default:
961         ERR("Unsupported fdwDetails=0x%08lx\n", fdwDetails);
962     }
963
964     return ret;
965 }
966
967 /**************************************************************************
968  *                              mixerGetControlDetails  [MMSYSTEM.808]
969  */
970 UINT16 WINAPI mixerGetControlDetails16(HMIXEROBJ16 hmix, 
971                                        LPMIXERCONTROLDETAILS16 lpmcd, 
972                                        DWORD fdwDetails) 
973 {
974     DWORD       ret = MMSYSERR_NOTENABLED;
975     SEGPTR      sppaDetails;
976
977     TRACE("(%04x, %p, %08lx)\n", hmix, lpmcd, fdwDetails);
978
979     if (lpmcd == NULL || lpmcd->cbStruct != sizeof(*lpmcd))
980         return MMSYSERR_INVALPARAM;
981
982     sppaDetails = (SEGPTR)lpmcd->paDetails;
983     lpmcd->paDetails = PTR_SEG_TO_LIN(sppaDetails);
984     ret = mixerGetControlDetailsA(hmix, (LPMIXERCONTROLDETAILS)lpmcd, fdwDetails);
985     lpmcd->paDetails = (LPVOID)sppaDetails;
986
987     return ret;
988 }
989
990 /**************************************************************************
991  *                              mixerGetLineControlsA   [WINMM.104]
992  */
993 UINT WINAPI mixerGetLineControlsA(HMIXEROBJ hmix, LPMIXERLINECONTROLSA lpmlcA, 
994                                   DWORD fdwControls) 
995 {
996     LPWINE_MIXER        lpwm;
997
998     TRACE("(%04x, %p, %08lx)\n", hmix, lpmlcA, fdwControls);
999
1000     if ((lpwm = MIXER_GetDev(hmix, fdwControls)) == NULL) 
1001         return MMSYSERR_INVALHANDLE;
1002
1003     if (lpmlcA == NULL || lpmlcA->cbStruct != sizeof(*lpmlcA))
1004         return MMSYSERR_INVALPARAM;
1005
1006     return MMDRV_Message(&lpwm->mld, MXDM_GETLINECONTROLS, (DWORD)lpmlcA, 
1007                          fdwControls, TRUE);
1008 }
1009
1010 /**************************************************************************
1011  *                              mixerGetLineControlsW           [WINMM.105]
1012  */
1013 UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW, 
1014                                   DWORD fdwControls) 
1015 {
1016     MIXERLINECONTROLSA  mlcA;
1017     DWORD               ret;
1018     int                 i;
1019
1020     TRACE("(%04x, %p, %08lx)\n", hmix, lpmlcW, fdwControls);
1021
1022     if (lpmlcW == NULL || lpmlcW->cbStruct != sizeof(*lpmlcW) || 
1023         lpmlcW->cbmxctrl != sizeof(MIXERCONTROLW))
1024         return MMSYSERR_INVALPARAM;
1025
1026     mlcA.cbStruct = sizeof(mlcA);
1027     mlcA.dwLineID = lpmlcW->dwLineID;
1028     mlcA.u.dwControlID = lpmlcW->u.dwControlID;
1029     mlcA.u.dwControlType = lpmlcW->u.dwControlType;
1030     mlcA.cControls = lpmlcW->cControls;
1031     mlcA.cbmxctrl = sizeof(MIXERCONTROLA);
1032     mlcA.pamxctrl = HeapAlloc(GetProcessHeap(), 0, 
1033                               mlcA.cControls * mlcA.cbmxctrl);
1034
1035     ret = mixerGetLineControlsA(hmix, &mlcA, fdwControls);
1036
1037     if (ret == MMSYSERR_NOERROR) {
1038         lpmlcW->dwLineID = mlcA.dwLineID;
1039         lpmlcW->u.dwControlID = mlcA.u.dwControlID;
1040         lpmlcW->u.dwControlType = mlcA.u.dwControlType;
1041         lpmlcW->cControls = mlcA.cControls;
1042         
1043         for (i = 0; i < mlcA.cControls; i++) {
1044             lpmlcW->pamxctrl[i].cbStruct = sizeof(MIXERCONTROLW);
1045             lpmlcW->pamxctrl[i].dwControlID = mlcA.pamxctrl[i].dwControlID;
1046             lpmlcW->pamxctrl[i].dwControlType = mlcA.pamxctrl[i].dwControlType;
1047             lpmlcW->pamxctrl[i].fdwControl = mlcA.pamxctrl[i].fdwControl;
1048             lpmlcW->pamxctrl[i].cMultipleItems = mlcA.pamxctrl[i].cMultipleItems;
1049             lstrcpyAtoW(lpmlcW->pamxctrl[i].szShortName, 
1050                         mlcA.pamxctrl[i].szShortName);
1051             lstrcpyAtoW(lpmlcW->pamxctrl[i].szName, mlcA.pamxctrl[i].szName);
1052             /* sizeof(lpmlcW->pamxctrl[i].Bounds) == 
1053              * sizeof(mlcA.pamxctrl[i].Bounds) */
1054             memcpy(&lpmlcW->pamxctrl[i].Bounds, &mlcA.pamxctrl[i].Bounds, 
1055                    sizeof(mlcA.pamxctrl[i].Bounds));
1056             /* sizeof(lpmlcW->pamxctrl[i].Metrics) == 
1057              * sizeof(mlcA.pamxctrl[i].Metrics) */
1058             memcpy(&lpmlcW->pamxctrl[i].Metrics, &mlcA.pamxctrl[i].Metrics, 
1059                    sizeof(mlcA.pamxctrl[i].Metrics));
1060         }
1061     }
1062
1063     HeapFree(GetProcessHeap(), 0, mlcA.pamxctrl);
1064
1065     return ret;
1066 }
1067
1068 /**************************************************************************
1069  *                              mixerGetLineControls            [MMSYSTEM.807]
1070  */
1071 UINT16 WINAPI mixerGetLineControls16(HMIXEROBJ16 hmix, 
1072                                      LPMIXERLINECONTROLS16 lpmlc16, 
1073                                      DWORD fdwControls) 
1074 {
1075     MIXERLINECONTROLSA  mlcA;
1076     DWORD               ret;
1077     int                 i;
1078     LPMIXERCONTROL16    lpmc16;
1079
1080     TRACE("(%04x, %p, %08lx)\n", hmix, lpmlc16, fdwControls);
1081
1082     if (lpmlc16 == NULL || lpmlc16->cbStruct != sizeof(*lpmlc16) || 
1083         lpmlc16->cbmxctrl != sizeof(MIXERCONTROL16))
1084         return MMSYSERR_INVALPARAM;
1085
1086     mlcA.cbStruct = sizeof(mlcA);
1087     mlcA.dwLineID = lpmlc16->dwLineID;
1088     mlcA.u.dwControlID = lpmlc16->u.dwControlID;
1089     mlcA.u.dwControlType = lpmlc16->u.dwControlType;
1090     mlcA.cControls = lpmlc16->cControls;
1091     mlcA.cbmxctrl = sizeof(MIXERCONTROLA);
1092     mlcA.pamxctrl = HeapAlloc(GetProcessHeap(), 0, 
1093                               mlcA.cControls * mlcA.cbmxctrl);
1094
1095     ret = mixerGetLineControlsA(hmix, &mlcA, fdwControls);
1096
1097     if (ret == MMSYSERR_NOERROR) {
1098         lpmlc16->dwLineID = mlcA.dwLineID;
1099         lpmlc16->u.dwControlID = mlcA.u.dwControlID;
1100         lpmlc16->u.dwControlType = mlcA.u.dwControlType;
1101         lpmlc16->cControls = mlcA.cControls;
1102         
1103         lpmc16 = PTR_SEG_TO_LIN(lpmlc16->pamxctrl);
1104         
1105         for (i = 0; i < mlcA.cControls; i++) {
1106             lpmc16[i].cbStruct = sizeof(MIXERCONTROL16);
1107             lpmc16[i].dwControlID = mlcA.pamxctrl[i].dwControlID;
1108             lpmc16[i].dwControlType = mlcA.pamxctrl[i].dwControlType;
1109             lpmc16[i].fdwControl = mlcA.pamxctrl[i].fdwControl;
1110             lpmc16[i].cMultipleItems = mlcA.pamxctrl[i].cMultipleItems;
1111             strcpy(lpmc16[i].szShortName, mlcA.pamxctrl[i].szShortName);
1112             strcpy(lpmc16[i].szName, mlcA.pamxctrl[i].szName);
1113             /* sizeof(lpmc16[i].Bounds) == sizeof(mlcA.pamxctrl[i].Bounds) */
1114             memcpy(&lpmc16[i].Bounds, &mlcA.pamxctrl[i].Bounds, 
1115                    sizeof(mlcA.pamxctrl[i].Bounds));
1116             /* sizeof(lpmc16[i].Metrics) == sizeof(mlcA.pamxctrl[i].Metrics) */
1117             memcpy(&lpmc16[i].Metrics, &mlcA.pamxctrl[i].Metrics, 
1118                    sizeof(mlcA.pamxctrl[i].Metrics));
1119         }
1120     }
1121
1122     HeapFree(GetProcessHeap(), 0, mlcA.pamxctrl);
1123
1124     return ret;
1125 }
1126
1127 /**************************************************************************
1128  *                              mixerGetLineInfoA               [WINMM.106]
1129  */
1130 UINT WINAPI mixerGetLineInfoA(HMIXEROBJ hmix, LPMIXERLINEA lpmliW, DWORD fdwInfo)
1131 {
1132     LPWINE_MIXER        lpwm;
1133     
1134     TRACE("(%04x, %p, %08lx)\n", hmix, lpmliW, fdwInfo);
1135     
1136     if ((lpwm = MIXER_GetDev(hmix, fdwInfo)) == NULL) 
1137         return MMSYSERR_INVALHANDLE;
1138
1139     return MMDRV_Message(&lpwm->mld, MXDM_GETLINEINFO, (DWORD)lpmliW, 
1140                          fdwInfo, TRUE);
1141 }
1142
1143 /**************************************************************************
1144  *                              mixerGetLineInfoW               [WINMM.107]
1145  */
1146 UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, 
1147                               DWORD fdwInfo) 
1148 {
1149     MIXERLINEA          mliA;
1150     UINT                ret;
1151     
1152     TRACE("(%04x, %p, %08lx)\n", hmix, lpmliW, fdwInfo);
1153
1154     if (lpmliW == NULL || lpmliW->cbStruct != sizeof(*lpmliW)) 
1155         return MMSYSERR_INVALPARAM;
1156
1157     mliA.cbStruct = sizeof(mliA);
1158     switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
1159     case MIXER_GETLINEINFOF_COMPONENTTYPE:
1160         mliA.dwComponentType = lpmliW->dwComponentType;
1161         break;
1162     case MIXER_GETLINEINFOF_DESTINATION:
1163         mliA.dwDestination = lpmliW->dwDestination;
1164         break;
1165     case MIXER_GETLINEINFOF_LINEID:
1166         mliA.dwLineID = lpmliW->dwLineID;
1167         break;
1168     case MIXER_GETLINEINFOF_SOURCE:
1169         mliA.dwDestination = lpmliW->dwDestination;
1170         mliA.dwSource = lpmliW->dwSource;
1171         break;
1172     case MIXER_GETLINEINFOF_TARGETTYPE:
1173         mliA.Target.dwType = lpmliW->Target.dwType;
1174         mliA.Target.wMid = lpmliW->Target.wMid;
1175         mliA.Target.wPid = lpmliW->Target.wPid;
1176         mliA.Target.vDriverVersion = lpmliW->Target.vDriverVersion;
1177         lstrcpyWtoA(mliA.Target.szPname, lpmliW->Target.szPname);
1178         break;
1179     default:
1180         FIXME("Unsupported fdwControls=0x%08lx\n", fdwInfo);
1181     }
1182
1183     ret = mixerGetLineInfoA(hmix, &mliA, fdwInfo);
1184
1185     lpmliW->dwDestination = mliA.dwDestination;
1186     lpmliW->dwSource = mliA.dwSource;
1187     lpmliW->dwLineID = mliA.dwLineID;
1188     lpmliW->fdwLine = mliA.fdwLine;
1189     lpmliW->dwUser = mliA.dwUser;
1190     lpmliW->dwComponentType = mliA.dwComponentType;
1191     lpmliW->cChannels = mliA.cChannels;
1192     lpmliW->cConnections = mliA.cConnections;
1193     lpmliW->cControls = mliA.cControls;
1194     lstrcpyAtoW(lpmliW->szShortName, mliA.szShortName);
1195     lstrcpyAtoW(lpmliW->szName, mliA.szName);
1196     lpmliW->Target.dwType = mliA.Target.dwType;
1197     lpmliW->Target.dwDeviceID = mliA.Target.dwDeviceID;
1198     lpmliW->Target.wMid = mliA.Target.wMid;
1199     lpmliW->Target.wPid = mliA.Target.wPid;
1200     lpmliW->Target.vDriverVersion = mliA.Target.vDriverVersion;
1201     lstrcpyAtoW(lpmliW->Target.szPname, mliA.Target.szPname);
1202
1203     return ret;
1204 }
1205
1206 /**************************************************************************
1207  *                              mixerGetLineInfo        [MMSYSTEM.805]
1208  */
1209 UINT16 WINAPI mixerGetLineInfo16(HMIXEROBJ16 hmix, LPMIXERLINE16 lpmli16, 
1210                                  DWORD fdwInfo) 
1211 {
1212     MIXERLINEA          mliA;
1213     UINT                ret;
1214     
1215     TRACE("(%04x, %p, %08lx)\n", hmix, lpmli16, fdwInfo);
1216
1217     if (lpmli16 == NULL || lpmli16->cbStruct != sizeof(*lpmli16)) 
1218         return MMSYSERR_INVALPARAM;
1219
1220     mliA.cbStruct = sizeof(mliA);
1221     switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
1222     case MIXER_GETLINEINFOF_COMPONENTTYPE:
1223         mliA.dwComponentType = lpmli16->dwComponentType;
1224         break;
1225     case MIXER_GETLINEINFOF_DESTINATION:
1226         mliA.dwDestination = lpmli16->dwDestination;
1227         break;
1228     case MIXER_GETLINEINFOF_LINEID:
1229         mliA.dwLineID = lpmli16->dwLineID;
1230         break;
1231     case MIXER_GETLINEINFOF_SOURCE:
1232         mliA.dwDestination = lpmli16->dwDestination;
1233         mliA.dwSource = lpmli16->dwSource;
1234         break;
1235     case MIXER_GETLINEINFOF_TARGETTYPE:
1236         mliA.Target.dwType = lpmli16->Target.dwType;
1237         mliA.Target.wMid = lpmli16->Target.wMid;
1238         mliA.Target.wPid = lpmli16->Target.wPid;
1239         mliA.Target.vDriverVersion = lpmli16->Target.vDriverVersion;
1240         strcpy(mliA.Target.szPname, lpmli16->Target.szPname);
1241         break;
1242     default:
1243         FIXME("Unsupported fdwControls=0x%08lx\n", fdwInfo);
1244     }
1245
1246     ret = mixerGetLineInfoA(hmix, &mliA, fdwInfo);
1247
1248     lpmli16->dwDestination      = mliA.dwDestination;
1249     lpmli16->dwSource           = mliA.dwSource;
1250     lpmli16->dwLineID           = mliA.dwLineID;
1251     lpmli16->fdwLine            = mliA.fdwLine;
1252     lpmli16->dwUser             = mliA.dwUser;
1253     lpmli16->dwComponentType    = mliA.dwComponentType;
1254     lpmli16->cChannels          = mliA.cChannels;
1255     lpmli16->cConnections       = mliA.cConnections;
1256     lpmli16->cControls          = mliA.cControls;
1257     strcpy(lpmli16->szShortName, mliA.szShortName);
1258     strcpy(lpmli16->szName, mliA.szName);
1259     lpmli16->Target.dwType      = mliA.Target.dwType;
1260     lpmli16->Target.dwDeviceID  = mliA.Target.dwDeviceID;
1261     lpmli16->Target.wMid        = mliA.Target.wMid;
1262     lpmli16->Target.wPid        = mliA.Target.wPid;
1263     lpmli16->Target.vDriverVersion = mliA.Target.vDriverVersion;
1264     strcpy(lpmli16->Target.szPname, mliA.Target.szPname);
1265
1266     return ret;
1267 }
1268
1269 /**************************************************************************
1270  *                              mixerSetControlDetails  [WINMM.111]
1271  */
1272 UINT WINAPI mixerSetControlDetails(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdA, 
1273                                    DWORD fdwDetails) 
1274 {
1275     LPWINE_MIXER        lpwm;
1276
1277     TRACE("(%04x, %p, %08lx)\n", hmix, lpmcdA, fdwDetails);
1278
1279     if ((lpwm = MIXER_GetDev(hmix, fdwDetails)) == NULL) 
1280         return MMSYSERR_INVALHANDLE;
1281
1282     return MMDRV_Message(&lpwm->mld, MXDM_SETCONTROLDETAILS, (DWORD)lpmcdA, 
1283                          fdwDetails, TRUE);
1284 }
1285
1286 /**************************************************************************
1287  *                              mixerSetControlDetails  [MMSYSTEM.809]
1288  */
1289 UINT16 WINAPI mixerSetControlDetails16(HMIXEROBJ16 hmix, 
1290                                        LPMIXERCONTROLDETAILS16 lpmcd, 
1291                                        DWORD fdwDetails) 
1292 {
1293     TRACE("(%04x, %p, %08lx)\n", hmix, lpmcd, fdwDetails);
1294     return MMSYSERR_NOTENABLED;
1295 }
1296
1297 /**************************************************************************
1298  *                              mixerMessage            [WINMM.109]
1299  */
1300 UINT WINAPI mixerMessage(HMIXER hmix, UINT uMsg, DWORD dwParam1, DWORD dwParam2)
1301 {
1302     LPWINE_MLD          wmld;
1303     
1304     TRACE("(%04lx, %d, %08lx, %08lx): semi-stub?\n",
1305           (DWORD)hmix, uMsg, dwParam1, dwParam2);
1306
1307     if ((wmld = MMDRV_Get(hmix, MMDRV_MIXER, FALSE)) == NULL)
1308         return MMSYSERR_INVALHANDLE;
1309
1310     return MMDRV_Message(wmld, uMsg, dwParam1, dwParam2, TRUE);
1311 }
1312
1313 /**************************************************************************
1314  *                              mixerMessage            [MMSYSTEM.804]
1315  */
1316 UINT16 WINAPI mixerMessage16(HMIXER16 hmix, UINT16 uMsg, DWORD dwParam1, 
1317                              DWORD dwParam2) 
1318 {
1319     return mixerMessage(hmix, uMsg, dwParam1, dwParam2);
1320 }
1321
1322 /**************************************************************************
1323  *                              auxGetNumDevs           [WINMM.22]
1324  */
1325 UINT WINAPI auxGetNumDevs(void)
1326 {
1327     return MMDRV_GetNum(MMDRV_AUX);
1328 }
1329
1330 /**************************************************************************
1331  *                              auxGetNumDevs           [MMSYSTEM.350]
1332  */
1333 UINT16 WINAPI auxGetNumDevs16(void)
1334 {
1335     return MMDRV_GetNum(MMDRV_AUX);
1336 }
1337
1338 /**************************************************************************
1339  *                              auxGetDevCaps           [WINMM.20]
1340  */
1341 UINT WINAPI auxGetDevCapsW(UINT uDeviceID, LPAUXCAPSW lpCaps, UINT uSize)
1342 {
1343     AUXCAPSA    acA;
1344     UINT        ret = auxGetDevCapsA(uDeviceID, &acA, sizeof(acA));
1345     
1346     lpCaps->wMid = acA.wMid;
1347     lpCaps->wPid = acA.wPid;
1348     lpCaps->vDriverVersion = acA.vDriverVersion;
1349     lstrcpyAtoW(lpCaps->szPname, acA.szPname);
1350     lpCaps->wTechnology = acA.wTechnology;
1351     lpCaps->dwSupport = acA.dwSupport;
1352     return ret;
1353 }
1354
1355 /**************************************************************************
1356  *                              auxGetDevCaps           [WINMM.21]
1357  */
1358 UINT WINAPI auxGetDevCapsA(UINT uDeviceID, LPAUXCAPSA lpCaps, UINT uSize)
1359 {
1360     LPWINE_MLD          wmld;
1361
1362     TRACE("(%04X, %p, %d) !\n", uDeviceID, lpCaps, uSize);
1363
1364     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_AUX, TRUE)) == NULL)
1365         return MMSYSERR_INVALHANDLE;
1366     return MMDRV_Message(wmld, AUXDM_GETDEVCAPS, (DWORD)lpCaps, uSize, TRUE);
1367 }
1368
1369 /**************************************************************************
1370  *                              auxGetDevCaps           [MMSYSTEM.351]
1371  */
1372 UINT16 WINAPI auxGetDevCaps16(UINT16 uDeviceID, LPAUXCAPS16 lpCaps, UINT16 uSize)
1373 {
1374     LPWINE_MLD          wmld;
1375
1376     TRACE("(%04X, %p, %d) !\n", uDeviceID, lpCaps, uSize);
1377
1378     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_AUX, TRUE)) == NULL)
1379         return MMSYSERR_INVALHANDLE;
1380     return MMDRV_Message(wmld, AUXDM_GETDEVCAPS, (DWORD)lpCaps, uSize, TRUE);
1381 }
1382
1383 /**************************************************************************
1384  *                              auxGetVolume            [WINM.23]
1385  */
1386 UINT WINAPI auxGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
1387 {
1388     LPWINE_MLD          wmld;
1389
1390     TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);
1391
1392     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_AUX, TRUE)) == NULL)
1393         return MMSYSERR_INVALHANDLE;
1394     return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD)lpdwVolume, 0L, TRUE);
1395 }
1396
1397 /**************************************************************************
1398  *                              auxGetVolume            [MMSYSTEM.352]
1399  */
1400 UINT16 WINAPI auxGetVolume16(UINT16 uDeviceID, LPDWORD lpdwVolume)
1401 {
1402     LPWINE_MLD          wmld;
1403
1404     TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);
1405
1406     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_AUX, TRUE)) == NULL)
1407         return MMSYSERR_INVALHANDLE;
1408     return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD)lpdwVolume, 0L, TRUE);
1409 }
1410
1411 /**************************************************************************
1412  *                              auxSetVolume            [WINMM.25]
1413  */
1414 UINT WINAPI auxSetVolume(UINT uDeviceID, DWORD dwVolume)
1415 {
1416     LPWINE_MLD          wmld;
1417
1418     TRACE("(%04X, %lu) !\n", uDeviceID, dwVolume);
1419
1420     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_AUX, TRUE)) == NULL)
1421         return MMSYSERR_INVALHANDLE;
1422     return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L, TRUE);
1423 }
1424
1425 /**************************************************************************
1426  *                              auxSetVolume            [MMSYSTEM.353]
1427  */
1428 UINT16 WINAPI auxSetVolume16(UINT16 uDeviceID, DWORD dwVolume)
1429 {
1430     LPWINE_MLD          wmld;
1431
1432     TRACE("(%04X, %lu) !\n", uDeviceID, dwVolume);
1433
1434     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_AUX, TRUE)) == NULL)
1435         return MMSYSERR_INVALHANDLE;
1436     return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L, TRUE);
1437 }
1438
1439 /**************************************************************************
1440  *                              auxOutMessage           [MMSYSTEM.354]
1441  */
1442 DWORD WINAPI auxOutMessage(UINT uDeviceID, UINT uMessage, DWORD dw1, DWORD dw2)
1443 {
1444     LPWINE_MLD          wmld;
1445
1446     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_AUX, TRUE)) == NULL)
1447         return MMSYSERR_INVALHANDLE;
1448
1449     return MMDRV_Message(wmld, uMessage, dw1, dw2, TRUE);
1450 }
1451
1452 /**************************************************************************
1453  *                              auxOutMessage           [MMSYSTEM.354]
1454  */
1455 DWORD WINAPI auxOutMessage16(UINT16 uDeviceID, UINT16 uMessage, DWORD dw1, DWORD dw2)
1456 {
1457     LPWINE_MLD          wmld;
1458
1459     TRACE("(%04X, %04X, %08lX, %08lX)\n", uDeviceID, uMessage, dw1, dw2);
1460
1461     switch (uMessage) {
1462     case AUXDM_GETNUMDEVS:
1463     case AUXDM_SETVOLUME:
1464         /* no argument conversion needed */
1465         break;
1466     case AUXDM_GETVOLUME:
1467         return auxGetVolume16(uDeviceID, (LPDWORD)PTR_SEG_TO_LIN(dw1));
1468     case AUXDM_GETDEVCAPS:
1469         return auxGetDevCaps16(uDeviceID, (LPAUXCAPS16)PTR_SEG_TO_LIN(dw1), dw2);
1470     default:
1471         TRACE("(%04x, %04x, %08lx, %08lx): unhandled message\n",
1472               uDeviceID, uMessage, dw1, dw2);
1473         break;
1474     }
1475     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_AUX, TRUE)) == NULL)
1476         return MMSYSERR_INVALHANDLE;
1477
1478     return MMDRV_Message(wmld, uMessage, dw1, dw2, TRUE);
1479 }
1480
1481 /**************************************************************************
1482  *                              mciGetErrorStringW              [WINMM.46]
1483  */
1484 BOOL WINAPI mciGetErrorStringW(DWORD wError, LPWSTR lpstrBuffer, UINT uLength)
1485 {
1486     LPSTR       bufstr = HeapAlloc(GetProcessHeap(), 0, uLength);
1487     BOOL        ret = mciGetErrorStringA(wError, bufstr, uLength);
1488     
1489     lstrcpyAtoW(lpstrBuffer, bufstr);
1490     HeapFree(GetProcessHeap(), 0, bufstr);
1491     return ret;
1492 }
1493
1494 /**************************************************************************
1495  *                              mciGetErrorStringA              [WINMM.45]
1496  */
1497 BOOL WINAPI mciGetErrorStringA(DWORD wError, LPSTR lpstrBuffer, UINT uLength)
1498 {
1499     return mciGetErrorString16(wError, lpstrBuffer, uLength);
1500 }
1501
1502 /**************************************************************************
1503  *                              mciGetErrorString               [MMSYSTEM.706]
1504  */
1505 BOOL16 WINAPI mciGetErrorString16(DWORD dwError, LPSTR lpstrBuffer, UINT16 uLength)
1506 {
1507     BOOL16              ret = FALSE;
1508
1509     if (lpstrBuffer != NULL && uLength > 0 && 
1510         dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
1511
1512         if (LoadStringA(MULTIMEDIA_GetIData()->hWinMM32Instance, 
1513                         dwError, lpstrBuffer, uLength) > 0) {
1514             ret = TRUE;
1515         }
1516     }
1517     return ret;
1518 }
1519
1520 /**************************************************************************
1521  *                              mciDriverNotify                 [MMSYSTEM.711]
1522  */
1523 BOOL16 WINAPI mciDriverNotify16(HWND16 hWndCallBack, UINT16 wDevID, UINT16 wStatus)
1524 {
1525     TRACE("(%04X, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
1526
1527     if (!IsWindow(hWndCallBack)) {
1528         WARN("bad hWnd for call back (0x%04x)\n", hWndCallBack);
1529         return FALSE;
1530     }
1531     TRACE("before PostMessage\n");
1532     Callout.PostMessageA(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
1533     return TRUE;
1534 }
1535
1536 /**************************************************************************
1537  *                      mciDriverNotify                         [WINMM.36]
1538  */
1539 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, UINT wDevID, UINT wStatus)
1540 {
1541
1542     TRACE("(%08X, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
1543
1544     if (!IsWindow(hWndCallBack)) {
1545         WARN("bad hWnd for call back (0x%04x)\n", hWndCallBack);
1546         return FALSE;
1547     }
1548     TRACE("before PostMessage\n");
1549     Callout.PostMessageA(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
1550     return TRUE;
1551 }
1552
1553 /**************************************************************************
1554  *                      mciGetDriverData                        [MMSYSTEM.708]
1555  */
1556 DWORD WINAPI mciGetDriverData16(UINT16 uDeviceID) 
1557 {
1558     return mciGetDriverData(uDeviceID);
1559 }
1560
1561 /**************************************************************************
1562  *                      mciGetDriverData                        [WINMM.44]
1563  */
1564 DWORD WINAPI mciGetDriverData(UINT uDeviceID) 
1565 {
1566     LPWINE_MCIDRIVER    wmd;
1567
1568     TRACE("(%04x)\n", uDeviceID);
1569
1570     wmd = MCI_GetDriver(uDeviceID);
1571
1572     if (!wmd) {
1573         WARN("Bad uDeviceID\n");
1574         return 0L;
1575     }
1576     
1577     return wmd->dwPrivate;
1578 }
1579
1580 /**************************************************************************
1581  *                      mciSetDriverData                        [MMSYSTEM.707]
1582  */
1583 BOOL16 WINAPI mciSetDriverData16(UINT16 uDeviceID, DWORD data) 
1584 {
1585     return mciSetDriverData(uDeviceID, data);
1586 }
1587
1588 /**************************************************************************
1589  *                      mciSetDriverData                        [WINMM.53]
1590  */
1591 BOOL WINAPI mciSetDriverData(UINT uDeviceID, DWORD data) 
1592 {
1593     LPWINE_MCIDRIVER    wmd;
1594
1595     TRACE("(%04x, %08lx)\n", uDeviceID, data);
1596
1597     wmd = MCI_GetDriver(uDeviceID);
1598
1599     if (!wmd) {
1600         WARN("Bad uDeviceID\n");
1601         return FALSE;
1602     }
1603     
1604     wmd->dwPrivate = data;
1605     return TRUE;
1606 }
1607
1608 /**************************************************************************
1609  *                              mciSendCommandA                 [WINMM.49]
1610  */
1611 DWORD WINAPI mciSendCommandA(UINT wDevID, UINT wMsg, DWORD dwParam1, DWORD dwParam2)
1612 {
1613     DWORD       dwRet;
1614
1615     TRACE("(%08x, %s, %08lx, %08lx)\n", 
1616           wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
1617
1618     dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2, TRUE);
1619     dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2, TRUE);
1620     TRACE("=> %08lx\n", dwRet);
1621     return dwRet;
1622 }
1623
1624 /**************************************************************************
1625  *                              mciSendCommandW                 [WINMM.50]
1626  */
1627 DWORD WINAPI mciSendCommandW(UINT wDevID, UINT wMsg, DWORD dwParam1, DWORD dwParam2)
1628 {
1629     FIXME("(%08x, %s, %08lx, %08lx): stub\n", 
1630           wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
1631     return MCIERR_UNSUPPORTED_FUNCTION;
1632 }
1633
1634 /**************************************************************************
1635  *                              mciSendCommand                  [MMSYSTEM.701]
1636  */
1637 DWORD WINAPI mciSendCommand16(UINT16 wDevID, UINT16 wMsg, DWORD dwParam1, DWORD dwParam2)
1638 {
1639     DWORD               dwRet;
1640
1641     TRACE("(%04X, %s, %08lX, %08lX)\n", 
1642           wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
1643
1644     dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2, FALSE);
1645     dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2, FALSE);
1646     TRACE("=> %ld\n", dwRet);
1647     return dwRet;
1648 }
1649     
1650 /**************************************************************************
1651  *                              mciGetDeviceID                  [MMSYSTEM.703]
1652  */
1653 UINT16 WINAPI mciGetDeviceID16(LPCSTR lpstrName)
1654 {
1655     TRACE("(\"%s\")\n", lpstrName);
1656
1657     return MCI_GetDriverFromString(lpstrName);
1658 }
1659
1660 /**************************************************************************
1661  *                              mciGetDeviceIDA                 [WINMM.41]
1662  */
1663 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
1664 {
1665     return MCI_GetDriverFromString(lpstrName);
1666 }
1667
1668 /**************************************************************************
1669  *                              mciGetDeviceIDW                 [WINMM.43]
1670  */
1671 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
1672 {
1673     LPSTR       lpstrName;
1674     UINT        ret;
1675
1676     lpstrName = HEAP_strdupWtoA(GetProcessHeap(), 0, lpwstrName);
1677     ret = MCI_GetDriverFromString(lpstrName);
1678     HeapFree(GetProcessHeap(), 0, lpstrName);
1679     return ret;
1680 }
1681
1682 /**************************************************************************
1683  *                              MCI_DefYieldProc                [internal]
1684  */
1685 UINT16  WINAPI MCI_DefYieldProc(UINT16 wDevID, DWORD data)
1686 {
1687     INT16       ret;
1688     
1689     TRACE("(0x%04x, 0x%08lx)\n", wDevID, data);
1690
1691     if ((HIWORD(data) != 0 && GetActiveWindow16() != HIWORD(data)) ||
1692         (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
1693         UserYield16();
1694         ret = 0;
1695     } else {
1696         MSG             msg;
1697
1698         msg.hwnd = HIWORD(data);
1699         while (!PeekMessageA(&msg, HIWORD(data), WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
1700         ret = -1;
1701     }
1702     return ret;
1703 }
1704
1705 /**************************************************************************
1706  *                              mciSetYieldProc                 [MMSYSTEM.714]
1707  */
1708 BOOL16 WINAPI mciSetYieldProc16(UINT16 uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
1709 {
1710     LPWINE_MCIDRIVER    wmd;
1711
1712     TRACE("(%u, %p, %08lx)\n", uDeviceID, fpYieldProc, dwYieldData);
1713
1714     if (!(wmd = MCI_GetDriver(uDeviceID))) {
1715         WARN("Bad uDeviceID\n");
1716         return FALSE;
1717     }
1718     
1719     wmd->lpfnYieldProc = fpYieldProc;
1720     wmd->dwYieldData   = dwYieldData;
1721     wmd->bIs32         = FALSE;
1722
1723     return TRUE;
1724 }
1725
1726 /**************************************************************************
1727  *                              mciSetYieldProc                 [WINMM.54]
1728  */
1729 BOOL WINAPI mciSetYieldProc(UINT uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
1730 {
1731     LPWINE_MCIDRIVER    wmd;
1732
1733     TRACE("(%u, %p, %08lx)\n", uDeviceID, fpYieldProc, dwYieldData);
1734
1735     if (!(wmd = MCI_GetDriver(uDeviceID))) {
1736         WARN("Bad uDeviceID\n");
1737         return FALSE;
1738     }
1739     
1740     wmd->lpfnYieldProc = fpYieldProc;
1741     wmd->dwYieldData   = dwYieldData;
1742     wmd->bIs32         = TRUE;
1743
1744     return TRUE;
1745 }
1746
1747 /**************************************************************************
1748  *                              mciGetDeviceIDFromElementID     [MMSYSTEM.715]
1749  */
1750 UINT16 WINAPI mciGetDeviceIDFromElementID16(DWORD dwElementID, LPCSTR lpstrType)
1751 {
1752     FIXME("(%lu, %s) stub\n", dwElementID, lpstrType);
1753     return 0;
1754 }
1755         
1756 /**************************************************************************
1757  *                              mciGetDeviceIDFromElementIDW    [WINMM.42]
1758  */
1759 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
1760 {
1761     /* FIXME: that's rather strange, there is no 
1762      * mciGetDeviceIDFromElementID32A in winmm.spec
1763      */
1764     FIXME("(%lu, %p) stub\n", dwElementID, lpstrType);
1765     return 0;
1766 }
1767         
1768 /**************************************************************************
1769  *                              mciGetYieldProc                 [MMSYSTEM.716]
1770  */
1771 YIELDPROC WINAPI mciGetYieldProc16(UINT16 uDeviceID, DWORD* lpdwYieldData)
1772 {
1773     LPWINE_MCIDRIVER    wmd;
1774
1775     TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
1776
1777     if (!(wmd = MCI_GetDriver(uDeviceID))) {
1778         WARN("Bad uDeviceID\n");
1779         return NULL;
1780     }
1781     if (!wmd->lpfnYieldProc) {
1782         WARN("No proc set\n");
1783         return NULL;
1784     }
1785     if (wmd->bIs32) {
1786         WARN("Proc is 32 bit\n");
1787         return NULL;
1788     }
1789     return wmd->lpfnYieldProc;
1790 }
1791     
1792 /**************************************************************************
1793  *                              mciGetYieldProc                 [WINMM.47]
1794  */
1795 YIELDPROC WINAPI mciGetYieldProc(UINT uDeviceID, DWORD* lpdwYieldData)
1796 {
1797     LPWINE_MCIDRIVER    wmd;
1798
1799     TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
1800
1801     if (!(wmd = MCI_GetDriver(uDeviceID))) {
1802         WARN("Bad uDeviceID\n");
1803         return NULL;
1804     }
1805     if (!wmd->lpfnYieldProc) {
1806         WARN("No proc set\n");
1807         return NULL;
1808     }
1809     if (!wmd->bIs32) {
1810         WARN("Proc is 32 bit\n");
1811         return NULL;
1812     }
1813     return wmd->lpfnYieldProc;
1814 }
1815
1816 /**************************************************************************
1817  *                              mciGetCreatorTask               [MMSYSTEM.717]
1818  */
1819 HTASK16 WINAPI mciGetCreatorTask16(UINT16 uDeviceID)
1820 {
1821     return mciGetCreatorTask(uDeviceID);
1822 }
1823
1824 /**************************************************************************
1825  *                              mciGetCreatorTask               [WINMM.40]
1826  */
1827 HTASK WINAPI mciGetCreatorTask(UINT uDeviceID)
1828 {
1829     LPWINE_MCIDRIVER    wmd;
1830     HTASK               ret;
1831
1832     TRACE("(%u)\n", uDeviceID);
1833
1834     ret = (!(wmd = MCI_GetDriver(uDeviceID))) ? 0 : wmd->hCreatorTask;
1835
1836     TRACE("=> %04x\n", ret);
1837     return ret;
1838 }
1839
1840 /**************************************************************************
1841  *                              mciDriverYield                  [MMSYSTEM.710]
1842  */
1843 UINT16 WINAPI mciDriverYield16(UINT16 uDeviceID) 
1844 {
1845     LPWINE_MCIDRIVER    wmd;
1846     UINT16              ret = 0;
1847
1848     /*    TRACE("(%04x)\n", uDeviceID); */
1849
1850     if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc || wmd->bIs32) {
1851         UserYield16();
1852     } else {
1853         ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
1854     }
1855
1856     return ret;
1857 }
1858
1859 /**************************************************************************
1860  *                      mciDriverYield                          [WINMM.37]
1861  */
1862 UINT WINAPI mciDriverYield(UINT uDeviceID) 
1863 {
1864     LPWINE_MCIDRIVER    wmd;
1865     UINT                ret = 0;
1866
1867     TRACE("(%04x)\n", uDeviceID);
1868
1869     if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc || !wmd->bIs32) {
1870         UserYield16();
1871     } else {
1872         ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
1873     }
1874
1875     return ret;
1876 }
1877
1878 /**************************************************************************
1879  *                              midiOutGetNumDevs       [WINMM.80]
1880  */
1881 UINT WINAPI midiOutGetNumDevs(void)
1882 {
1883     return MMDRV_GetNum(MMDRV_MIDIOUT);
1884 }
1885
1886 /**************************************************************************
1887  *                              midiOutGetNumDevs       [MMSYSTEM.201]
1888  */
1889 UINT16 WINAPI midiOutGetNumDevs16(void)
1890 {
1891     return MMDRV_GetNum(MMDRV_MIDIOUT);
1892 }
1893
1894 /**************************************************************************
1895  *                              midiOutGetDevCapsW      [WINMM.76]
1896  */
1897 UINT WINAPI midiOutGetDevCapsW(UINT uDeviceID, LPMIDIOUTCAPSW lpCaps, 
1898                                UINT uSize)
1899 {
1900     MIDIOUTCAPSA        mocA;
1901     UINT                ret;
1902     
1903     ret = midiOutGetDevCapsA(uDeviceID, &mocA, sizeof(mocA));
1904     lpCaps->wMid                = mocA.wMid;
1905     lpCaps->wPid                = mocA.wPid;
1906     lpCaps->vDriverVersion      = mocA.vDriverVersion;
1907     lstrcpyAtoW(lpCaps->szPname, mocA.szPname);
1908     lpCaps->wTechnology         = mocA.wTechnology;
1909     lpCaps->wVoices             = mocA.wVoices;
1910     lpCaps->wNotes              = mocA.wNotes;
1911     lpCaps->wChannelMask        = mocA.wChannelMask;
1912     lpCaps->dwSupport           = mocA.dwSupport;
1913     return ret;
1914 }
1915
1916 /**************************************************************************
1917  *                              midiOutGetDevCapsA      [WINMM.75]
1918  */
1919 UINT WINAPI midiOutGetDevCapsA(UINT uDeviceID, LPMIDIOUTCAPSA lpCaps, 
1920                                UINT uSize)
1921 {
1922     LPWINE_MLD  wmld;
1923
1924     TRACE("(%u, %p, %u);\n", uDeviceID, lpCaps, uSize);
1925
1926     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
1927
1928     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL)
1929         return MMSYSERR_INVALHANDLE;
1930
1931     return MMDRV_Message(wmld, MODM_GETDEVCAPS, (DWORD)lpCaps, uSize, TRUE);
1932 }
1933
1934 /**************************************************************************
1935  *                              midiOutGetDevCaps       [MMSYSTEM.202]
1936  */
1937 UINT16 WINAPI midiOutGetDevCaps16(UINT16 uDeviceID, LPMIDIOUTCAPS16 lpCaps, 
1938                                   UINT16 uSize)
1939 {
1940     MIDIOUTCAPSA        capsA;
1941     UINT                dwRet;
1942
1943     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
1944
1945     dwRet = midiOutGetDevCapsA(uDeviceID, &capsA, sizeof(capsA));
1946     if (dwRet == MMSYSERR_NOERROR) {
1947         lpCaps->wMid            = capsA.wMid;
1948         lpCaps->wPid            = capsA.wPid;
1949         lpCaps->vDriverVersion  = capsA.vDriverVersion;
1950         strcpy(lpCaps->szPname, capsA.szPname);
1951         lpCaps->wTechnology     = capsA.wTechnology;
1952         lpCaps->wVoices         = capsA.wVoices;
1953         lpCaps->wNotes          = capsA.wNotes;
1954         lpCaps->wChannelMask    = capsA.wChannelMask;
1955         lpCaps->dwSupport       = capsA.dwSupport;
1956     }
1957     return dwRet;
1958  }
1959
1960 /**************************************************************************
1961  *                              MIDI_GetErrorText               [internal]
1962  */
1963 static  UINT16  MIDI_GetErrorText(UINT16 uError, LPSTR lpText, UINT16 uSize)
1964 {
1965     UINT16              ret = MMSYSERR_BADERRNUM;
1966
1967     if (lpText == NULL) {
1968         ret = MMSYSERR_INVALPARAM;
1969     } else if (uSize == 0) {
1970         ret = MMSYSERR_NOERROR;
1971     } else if (
1972                /* test has been removed 'coz MMSYSERR_BASE is 0, and gcc did emit
1973                 * a warning for the test was always true */
1974                (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
1975                (uError >= MIDIERR_BASE  && uError <= MIDIERR_LASTERROR)) {
1976         
1977         if (LoadStringA(MULTIMEDIA_GetIData()->hWinMM32Instance, 
1978                         uError, lpText, uSize) > 0) {
1979             ret = MMSYSERR_NOERROR;
1980         }
1981     }
1982     return ret;
1983 }
1984
1985 /**************************************************************************
1986  *                              midiOutGetErrorTextA    [WINMM.77]
1987  */
1988 UINT WINAPI midiOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
1989 {
1990     return MIDI_GetErrorText(uError, lpText, uSize);
1991 }
1992
1993 /**************************************************************************
1994  *                              midiOutGetErrorTextW    [WINMM.78]
1995  */
1996 UINT WINAPI midiOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
1997 {
1998     LPSTR       xstr = HeapAlloc(GetProcessHeap(), 0, uSize);
1999     UINT        ret;
2000     
2001     ret = MIDI_GetErrorText(uError, xstr, uSize);
2002     lstrcpyAtoW(lpText, xstr);
2003     HeapFree(GetProcessHeap(), 0, xstr);
2004     return ret;
2005 }
2006
2007 /**************************************************************************
2008  *                              midiOutGetErrorText     [MMSYSTEM.203]
2009  */
2010 UINT16 WINAPI midiOutGetErrorText16(UINT16 uError, LPSTR lpText, UINT16 uSize)
2011 {
2012     return MIDI_GetErrorText(uError, lpText, uSize);
2013 }
2014
2015 /**************************************************************************
2016  *                              MIDI_OutAlloc                   [internal]
2017  */
2018 static  LPWINE_MIDI     MIDI_OutAlloc(HMIDIOUT* lphMidiOut, LPDWORD lpdwCallback, 
2019                                       LPDWORD lpdwInstance, LPDWORD lpdwFlags, 
2020                                       DWORD cIDs, MIDIOPENSTRMID* lpIDs, BOOL bFrom32)
2021 {
2022     HANDLE              hMidiOut;
2023     LPWINE_MIDI         lpwm;
2024     UINT                size;
2025     
2026     size = sizeof(WINE_MIDI) + (cIDs ? (cIDs-1) : 0) * sizeof(MIDIOPENSTRMID);
2027
2028     lpwm = (LPWINE_MIDI)MMDRV_Alloc(size, MMDRV_MIDIOUT, &hMidiOut, lpdwFlags, 
2029                                     lpdwCallback, lpdwInstance, bFrom32);
2030
2031     if (lphMidiOut != NULL) 
2032         *lphMidiOut = hMidiOut;
2033
2034     if (lpwm) {
2035         lpwm->mod.hMidi = hMidiOut;
2036         lpwm->mod.dwCallback = *lpdwCallback;
2037         lpwm->mod.dwInstance = *lpdwInstance;
2038         lpwm->mod.dnDevNode = 0;
2039         lpwm->mod.cIds = cIDs;
2040         if (cIDs)
2041             memcpy(&(lpwm->mod.rgIds), lpIDs, cIDs * sizeof(MIDIOPENSTRMID));
2042     }
2043     return lpwm;
2044 }
2045
2046 UINT MMSYSTEM_midiOutOpen(HMIDIOUT* lphMidiOut, UINT uDeviceID, DWORD dwCallback, 
2047                           DWORD dwInstance, DWORD dwFlags, BOOL bFrom32)
2048 {
2049     HMIDIOUT            hMidiOut;
2050     LPWINE_MIDI         lpwm;
2051     UINT                dwRet = 0;
2052     
2053     TRACE("(%p, %d, %08lX, %08lX, %08lX);\n", 
2054           lphMidiOut, uDeviceID, dwCallback, dwInstance, dwFlags);
2055
2056     if (lphMidiOut != NULL) *lphMidiOut = 0;
2057
2058     lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &dwFlags, 
2059                          0, NULL, bFrom32);
2060
2061     if (lpwm == NULL)
2062         return MMSYSERR_NOMEM;
2063
2064     lpwm->mld.uDeviceID = uDeviceID;
2065
2066     dwRet = MMDRV_Open((LPWINE_MLD)lpwm, MODM_OPEN, (DWORD)&lpwm->mod, 
2067                        dwFlags);
2068
2069     if (dwRet != MMSYSERR_NOERROR) {
2070         MMDRV_Free(hMidiOut, (LPWINE_MLD)lpwm);
2071         hMidiOut = 0;
2072     }
2073
2074     if (lphMidiOut) *lphMidiOut = hMidiOut;
2075     TRACE("=> %d hMidi=%04x\n", dwRet, hMidiOut);
2076
2077     return dwRet;
2078 }
2079
2080 /**************************************************************************
2081  *                              midiOutOpen             [WINM.84]
2082  */
2083 UINT WINAPI midiOutOpen(HMIDIOUT* lphMidiOut, UINT uDeviceID,
2084                         DWORD dwCallback, DWORD dwInstance, DWORD dwFlags)
2085 {
2086     return MMSYSTEM_midiOutOpen(lphMidiOut, uDeviceID, dwCallback, 
2087                                 dwInstance, dwFlags, TRUE);
2088 }
2089
2090 /**************************************************************************
2091  *                              midiOutOpen             [MMSYSTEM.204]
2092  */
2093 UINT16 WINAPI midiOutOpen16(HMIDIOUT16* lphMidiOut, UINT16 uDeviceID,
2094                             DWORD dwCallback, DWORD dwInstance, DWORD dwFlags)
2095 {
2096     HMIDIOUT    hmo;
2097     UINT        ret;
2098     
2099     ret = MMSYSTEM_midiOutOpen(&hmo, uDeviceID, dwCallback, dwInstance, 
2100                                dwFlags, FALSE);
2101     
2102     if (lphMidiOut != NULL) *lphMidiOut = hmo;
2103     return ret;
2104 }
2105
2106 /**************************************************************************
2107  *                              midiOutClose            [WINMM.74]
2108  */
2109 UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
2110 {
2111     LPWINE_MLD          wmld;
2112     DWORD               dwRet;
2113
2114     TRACE("(%04X)\n", hMidiOut);
2115
2116     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2117         return MMSYSERR_INVALHANDLE;
2118
2119     dwRet = MMDRV_Close(wmld, MODM_CLOSE);
2120     MMDRV_Free(hMidiOut, wmld);
2121
2122     return dwRet;
2123 }
2124
2125 /**************************************************************************
2126  *                              midiOutClose            [MMSYSTEM.205]
2127  */
2128 UINT16 WINAPI midiOutClose16(HMIDIOUT16 hMidiOut)
2129 {
2130     return midiOutClose(hMidiOut);
2131 }
2132
2133 /**************************************************************************
2134  *                              midiOutPrepareHeader    [WINMM.85]
2135  */
2136 UINT WINAPI midiOutPrepareHeader(HMIDIOUT hMidiOut,
2137                                  MIDIHDR* lpMidiOutHdr, UINT uSize)
2138 {
2139     LPWINE_MLD          wmld;
2140
2141     TRACE("(%04X, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
2142
2143     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2144         return MMSYSERR_INVALHANDLE;
2145
2146     return MMDRV_Message(wmld, MODM_PREPARE, (DWORD)lpMidiOutHdr, uSize, TRUE);
2147 }
2148
2149 /**************************************************************************
2150  *                              midiOutPrepareHeader    [MMSYSTEM.206]
2151  */
2152 UINT16 WINAPI midiOutPrepareHeader16(HMIDIOUT16 hMidiOut,
2153                                      LPMIDIHDR16 /*SEGPTR*/ lpsegMidiOutHdr,    
2154                                      UINT16 uSize)
2155 {
2156     LPWINE_MLD          wmld;
2157     LPMIDIHDR16         lpMidiOutHdr = PTR_SEG_TO_LIN(lpsegMidiOutHdr);
2158
2159     TRACE("(%04X, %p, %d)\n", hMidiOut, lpsegMidiOutHdr, uSize);
2160
2161     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2162         return MMSYSERR_INVALHANDLE;
2163
2164     return MMDRV_Message(wmld, MODM_PREPARE, (DWORD)lpMidiOutHdr, uSize, FALSE);
2165 }
2166
2167 /**************************************************************************
2168  *                              midiOutUnprepareHeader  [WINMM.89]
2169  */
2170 UINT WINAPI midiOutUnprepareHeader(HMIDIOUT hMidiOut,
2171                                    MIDIHDR* lpMidiOutHdr, UINT uSize)
2172 {       
2173     LPWINE_MLD          wmld;
2174
2175     TRACE("(%04X, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
2176
2177     if (!(lpMidiOutHdr->dwFlags & MHDR_PREPARED)) {
2178         return MMSYSERR_NOERROR;
2179     }
2180
2181     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2182         return MMSYSERR_INVALHANDLE;
2183
2184     return MMDRV_Message(wmld, MODM_UNPREPARE, (DWORD)lpMidiOutHdr, uSize, TRUE);
2185 }
2186
2187 /**************************************************************************
2188  *                              midiOutUnprepareHeader  [MMSYSTEM.207]
2189  */
2190 UINT16 WINAPI midiOutUnprepareHeader16(HMIDIOUT16 hMidiOut,
2191                                        LPMIDIHDR16 /*SEGPTR*/ lpsegMidiOutHdr, 
2192                                        UINT16 uSize)
2193 {
2194     LPWINE_MLD          wmld;
2195     LPMIDIHDR16         lpMidiOutHdr = PTR_SEG_TO_LIN(lpsegMidiOutHdr);
2196
2197     TRACE("(%04X, %p, %d)\n", hMidiOut, lpsegMidiOutHdr, uSize);
2198
2199     if (!(lpMidiOutHdr->dwFlags & MHDR_PREPARED)) {
2200         return MMSYSERR_NOERROR;
2201     }
2202
2203     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2204         return MMSYSERR_INVALHANDLE;
2205
2206     return MMDRV_Message(wmld, MODM_UNPREPARE, (DWORD)lpsegMidiOutHdr, uSize, FALSE);
2207 }
2208
2209 /**************************************************************************
2210  *                              midiOutShortMsg         [WINMM.88]
2211  */
2212 UINT WINAPI midiOutShortMsg(HMIDIOUT hMidiOut, DWORD dwMsg)
2213 {
2214     LPWINE_MLD          wmld;
2215
2216     TRACE("(%04X, %08lX)\n", hMidiOut, dwMsg);
2217
2218     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2219         return MMSYSERR_INVALHANDLE;
2220
2221     return MMDRV_Message(wmld, MODM_DATA, dwMsg, 0L, FALSE);
2222 }
2223
2224 /**************************************************************************
2225  *                              midiOutShortMsg         [MMSYSTEM.208]
2226  */
2227 UINT16 WINAPI midiOutShortMsg16(HMIDIOUT16 hMidiOut, DWORD dwMsg)
2228 {
2229     return midiOutShortMsg(hMidiOut, dwMsg);
2230 }
2231
2232 /**************************************************************************
2233  *                              midiOutLongMsg          [WINMM.82]
2234  */
2235 UINT WINAPI midiOutLongMsg(HMIDIOUT hMidiOut,
2236                            MIDIHDR* lpMidiOutHdr, UINT uSize)
2237 {
2238     LPWINE_MLD          wmld;
2239
2240     TRACE("(%04X, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
2241
2242     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2243         return MMSYSERR_INVALHANDLE;
2244
2245     return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD)lpMidiOutHdr, uSize, TRUE);
2246 }
2247
2248 /**************************************************************************
2249  *                              midiOutLongMsg          [MMSYSTEM.209]
2250  */
2251 UINT16 WINAPI midiOutLongMsg16(HMIDIOUT16 hMidiOut,
2252                                LPMIDIHDR16 /* SEGPTR */ lpsegMidiOutHdr, 
2253                                UINT16 uSize)
2254 {
2255     LPWINE_MLD          wmld;
2256
2257     TRACE("(%04X, %p, %d)\n", hMidiOut, lpsegMidiOutHdr, uSize);
2258
2259     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2260         return MMSYSERR_INVALHANDLE;
2261
2262     return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD)lpsegMidiOutHdr, uSize, FALSE);
2263 }
2264
2265 /**************************************************************************
2266  *                              midiOutReset            [WINMM.86]
2267  */
2268 UINT WINAPI midiOutReset(HMIDIOUT hMidiOut)
2269 {
2270     LPWINE_MLD          wmld;
2271
2272     TRACE("(%04X)\n", hMidiOut);
2273
2274     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2275         return MMSYSERR_INVALHANDLE;
2276
2277     return MMDRV_Message(wmld, MODM_RESET, 0L, 0L, TRUE);
2278 }
2279
2280 /**************************************************************************
2281  *                              midiOutReset            [MMSYSTEM.210]
2282  */
2283 UINT16 WINAPI midiOutReset16(HMIDIOUT16 hMidiOut)
2284 {
2285     return midiOutReset(hMidiOut);
2286 }
2287
2288 /**************************************************************************
2289  *                              midiOutGetVolume        [WINM.81]
2290  */
2291 UINT WINAPI midiOutGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
2292 {
2293     LPWINE_MLD          wmld;
2294
2295     TRACE("(%04X, %p);\n", uDeviceID, lpdwVolume);
2296
2297     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL) 
2298         return MMSYSERR_INVALHANDLE;
2299
2300     return MMDRV_Message(wmld, MODM_GETVOLUME, (DWORD)lpdwVolume, 0L, TRUE);
2301 }
2302
2303 /**************************************************************************
2304  *                              midiOutGetVolume        [MMSYSTEM.211]
2305  */
2306 UINT16 WINAPI midiOutGetVolume16(UINT16 uDeviceID, DWORD* lpdwVolume)
2307 {
2308     return midiOutGetVolume(uDeviceID, lpdwVolume);
2309 }
2310
2311 /**************************************************************************
2312  *                              midiOutSetVolume        [WINMM.87]
2313  */
2314 UINT WINAPI midiOutSetVolume(UINT uDeviceID, DWORD dwVolume)
2315 {
2316     LPWINE_MLD          wmld;
2317
2318     TRACE("(%04X, %ld);\n", uDeviceID, dwVolume);
2319
2320     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL) 
2321         return MMSYSERR_INVALHANDLE;
2322
2323     return MMDRV_Message(wmld, MODM_SETVOLUME, dwVolume, 0L, TRUE);
2324 }
2325
2326 /**************************************************************************
2327  *                              midiOutSetVolume        [MMSYSTEM.212]
2328  */
2329 UINT16 WINAPI midiOutSetVolume16(UINT16 uDeviceID, DWORD dwVolume)
2330 {
2331     return midiOutSetVolume(uDeviceID, dwVolume);
2332 }
2333
2334 /**************************************************************************
2335  *                              midiOutCachePatches             [WINMM.73]
2336  */
2337 UINT WINAPI midiOutCachePatches(HMIDIOUT hMidiOut, UINT uBank,
2338                                 WORD* lpwPatchArray, UINT uFlags)
2339 {
2340     /* not really necessary to support this */
2341     FIXME("not supported yet\n");
2342     return MMSYSERR_NOTSUPPORTED;
2343 }
2344
2345 /**************************************************************************
2346  *                              midiOutCachePatches             [MMSYSTEM.213]
2347  */
2348 UINT16 WINAPI midiOutCachePatches16(HMIDIOUT16 hMidiOut, UINT16 uBank,
2349                                     WORD* lpwPatchArray, UINT16 uFlags)
2350 {
2351     return midiOutCachePatches(hMidiOut, uBank, lpwPatchArray, uFlags);
2352 }
2353
2354 /**************************************************************************
2355  *                              midiOutCacheDrumPatches [WINMM.72]
2356  */
2357 UINT WINAPI midiOutCacheDrumPatches(HMIDIOUT hMidiOut, UINT uPatch,
2358                                     WORD* lpwKeyArray, UINT uFlags)
2359 {
2360     FIXME("not supported yet\n");
2361     return MMSYSERR_NOTSUPPORTED;
2362 }
2363
2364 /**************************************************************************
2365  *                              midiOutCacheDrumPatches [MMSYSTEM.214]
2366  */
2367 UINT16 WINAPI midiOutCacheDrumPatches16(HMIDIOUT16 hMidiOut, UINT16 uPatch,
2368                                         WORD* lpwKeyArray, UINT16 uFlags)
2369 {
2370     return midiOutCacheDrumPatches16(hMidiOut, uPatch, lpwKeyArray, uFlags);
2371 }
2372
2373 /**************************************************************************
2374  *                              midiOutGetID            [WINMM.79]
2375  */
2376 UINT WINAPI midiOutGetID(HMIDIOUT hMidiOut, UINT* lpuDeviceID)
2377 {
2378     LPWINE_MLD          wmld;
2379
2380     TRACE("(%04X, %p)\n", hMidiOut, lpuDeviceID);
2381
2382     if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
2383     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2384         return MMSYSERR_INVALHANDLE;
2385
2386     *lpuDeviceID = wmld->uDeviceID;
2387     return MMSYSERR_NOERROR;
2388 }
2389
2390 /**************************************************************************
2391  *                              midiOutGetID            [MMSYSTEM.215]
2392  */
2393 UINT16 WINAPI midiOutGetID16(HMIDIOUT16 hMidiOut, UINT16* lpuDeviceID)
2394 {
2395     LPWINE_MLD          wmld;
2396
2397     TRACE("(%04X, %p)\n", hMidiOut, lpuDeviceID);
2398
2399     if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
2400     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2401         return MMSYSERR_INVALHANDLE;
2402
2403     *lpuDeviceID = wmld->uDeviceID;
2404     return MMSYSERR_NOERROR;
2405 }
2406
2407 /**************************************************************************
2408  *                              midiOutMessage          [WINMM.83]
2409  */
2410 DWORD WINAPI midiOutMessage(HMIDIOUT hMidiOut, UINT uMessage, 
2411                             DWORD dwParam1, DWORD dwParam2)
2412 {
2413     LPWINE_MLD          wmld;
2414
2415     TRACE("(%04X, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
2416
2417     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2418         return MMSYSERR_INVALHANDLE;
2419
2420     switch (uMessage) {
2421     case MODM_OPEN:
2422     case MODM_CLOSE:
2423         FIXME("can't handle OPEN or CLOSE message!\n");
2424         return MMSYSERR_NOTSUPPORTED;
2425     }
2426     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, TRUE);
2427 }
2428
2429 /**************************************************************************
2430  *                              midiOutMessage          [MMSYSTEM.216]
2431  */
2432 DWORD WINAPI midiOutMessage16(HMIDIOUT16 hMidiOut, UINT16 uMessage, 
2433                               DWORD dwParam1, DWORD dwParam2)
2434 {
2435     LPWINE_MLD          wmld;
2436
2437     TRACE("(%04X, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
2438
2439     if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) 
2440         return MMSYSERR_INVALHANDLE;
2441
2442     switch (uMessage) {
2443     case MODM_OPEN:
2444     case MODM_CLOSE:
2445         FIXME("can't handle OPEN or CLOSE message!\n");
2446         return MMSYSERR_NOTSUPPORTED;
2447
2448     case MODM_GETVOLUME:
2449         return midiOutGetVolume16(hMidiOut, (LPDWORD)PTR_SEG_TO_LIN(dwParam1));
2450     case MODM_LONGDATA:
2451         return midiOutLongMsg16(hMidiOut, (LPMIDIHDR16)PTR_SEG_TO_LIN(dwParam1), dwParam2);
2452     case MODM_PREPARE:
2453         /* lpMidiOutHdr is still a segmented pointer for this function */
2454         return midiOutPrepareHeader16(hMidiOut, (LPMIDIHDR16)dwParam1, dwParam2);
2455     case MODM_UNPREPARE:
2456         return midiOutUnprepareHeader16(hMidiOut, (LPMIDIHDR16)PTR_SEG_TO_LIN(dwParam1), dwParam2);     
2457     }
2458     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, TRUE);
2459 }
2460
2461 /**************************************************************************
2462  *                              midiInGetNumDevs        [WINMM.64]
2463  */
2464 UINT WINAPI midiInGetNumDevs(void)
2465 {
2466     return MMDRV_GetNum(MMDRV_MIDIIN);
2467 }
2468
2469 /**************************************************************************
2470  *                              midiInGetNumDevs        [MMSYSTEM.301]
2471  */
2472 UINT16 WINAPI midiInGetNumDevs16(void)
2473 {
2474     return MMDRV_GetNum(MMDRV_MIDIIN);
2475 }
2476
2477 /**************************************************************************
2478  *                              midiInGetDevCaps        [WINMM.60]
2479  */
2480 UINT WINAPI midiInGetDevCapsW(UINT uDeviceID, LPMIDIINCAPSW lpCaps, UINT uSize)
2481 {
2482     MIDIINCAPSA         micA;
2483     UINT                ret = midiInGetDevCapsA(uDeviceID, &micA, uSize);
2484     
2485     if (ret == MMSYSERR_NOERROR) {
2486         lpCaps->wMid = micA.wMid;
2487         lpCaps->wPid = micA.wPid;
2488         lpCaps->vDriverVersion = micA.vDriverVersion;
2489         lstrcpyAtoW(lpCaps->szPname, micA.szPname);
2490         lpCaps->dwSupport = micA.dwSupport;
2491     }
2492     return ret;
2493 }
2494
2495 /**************************************************************************
2496  *                              midiInGetDevCaps        [WINMM.59]
2497  */
2498 UINT WINAPI midiInGetDevCapsA(UINT uDeviceID, LPMIDIINCAPSA lpCaps, UINT uSize)
2499 {
2500     LPWINE_MLD  wmld;
2501
2502     TRACE("(%d, %p, %d);\n", uDeviceID, lpCaps, uSize);
2503
2504     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_MIDIIN, TRUE)) == NULL) 
2505         return MMSYSERR_INVALHANDLE;
2506
2507    return MMDRV_Message(wmld, MIDM_GETDEVCAPS, (DWORD)lpCaps, uSize, TRUE);
2508 }
2509
2510 /**************************************************************************
2511  *                              midiInGetDevCaps        [MMSYSTEM.302]
2512  */
2513 UINT16 WINAPI midiInGetDevCaps16(UINT16 uDeviceID, LPMIDIINCAPS16 lpCaps, 
2514                                  UINT16 uSize)
2515 {
2516     MIDIINCAPSA         micA;
2517     UINT                ret = midiInGetDevCapsA(uDeviceID, &micA, uSize);
2518     
2519     if (ret == MMSYSERR_NOERROR) {
2520         lpCaps->wMid = micA.wMid;
2521         lpCaps->wPid = micA.wPid;
2522         lpCaps->vDriverVersion = micA.vDriverVersion;
2523         strcpy(lpCaps->szPname, micA.szPname);
2524         lpCaps->dwSupport = micA.dwSupport;
2525     }
2526  
2527     return ret;
2528 }
2529
2530 /**************************************************************************
2531  *                              midiInGetErrorText              [WINMM.62]
2532  */
2533 UINT WINAPI midiInGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
2534 {
2535     LPSTR       xstr = HeapAlloc(GetProcessHeap(), 0, uSize);
2536     UINT        ret = MIDI_GetErrorText(uError, xstr, uSize);
2537
2538     lstrcpyAtoW(lpText, xstr);
2539     HeapFree(GetProcessHeap(), 0, xstr);
2540     return ret;
2541 }
2542
2543 /**************************************************************************
2544  *                              midiInGetErrorText              [WINMM.61]
2545  */
2546 UINT WINAPI midiInGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
2547 {
2548     return MIDI_GetErrorText(uError, lpText, uSize);
2549 }
2550
2551 /**************************************************************************
2552  *                              midiInGetErrorText              [MMSYSTEM.303]
2553  */
2554 UINT16 WINAPI midiInGetErrorText16(UINT16 uError, LPSTR lpText, UINT16 uSize)
2555 {
2556     return MIDI_GetErrorText(uError, lpText, uSize);
2557 }
2558
2559 static  UINT MMSYSTEM_midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID, DWORD dwCallback, 
2560                                  DWORD dwInstance, DWORD dwFlags, BOOL bFrom32)
2561 {
2562     HMIDI               hMidiIn;
2563     LPWINE_MIDI         lpwm;
2564     DWORD               dwRet = 0;
2565     
2566     TRACE("(%p, %d, %08lX, %08lX, %08lX);\n", 
2567           lphMidiIn, uDeviceID, dwCallback, dwInstance, dwFlags);
2568
2569     if (lphMidiIn != NULL) *lphMidiIn = 0;
2570
2571     lpwm = (LPWINE_MIDI)MMDRV_Alloc(sizeof(WINE_MIDI), MMDRV_MIDIIN, &hMidiIn,
2572                                     &dwFlags, &dwCallback, &dwInstance, bFrom32);
2573
2574     if (lpwm == NULL) 
2575         return MMSYSERR_NOMEM;
2576
2577     lpwm->mod.hMidi = hMidiIn;
2578     lpwm->mod.dwCallback = dwCallback;
2579     lpwm->mod.dwInstance = dwInstance;
2580
2581     lpwm->mld.uDeviceID = uDeviceID;
2582     dwRet = MMDRV_Open(&lpwm->mld, MIDM_OPEN, (DWORD)&lpwm->mod, dwFlags);
2583
2584     if (dwRet != MMSYSERR_NOERROR) {
2585         MMDRV_Free(hMidiIn, &lpwm->mld);
2586         hMidiIn = 0;
2587     }
2588     if (lphMidiIn != NULL) *lphMidiIn = hMidiIn;
2589     TRACE("=> %ld hMidi=%04x\n", dwRet, hMidiIn);
2590
2591     return dwRet;
2592 }
2593
2594 /**************************************************************************
2595  *                              midiInOpen              [WINMM.66]
2596  */
2597 UINT WINAPI midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID,
2598                        DWORD dwCallback, DWORD dwInstance, DWORD dwFlags)
2599 {
2600     return MMSYSTEM_midiInOpen(lphMidiIn, uDeviceID, dwCallback, 
2601                                dwInstance, dwFlags, TRUE);
2602 }
2603
2604 /**************************************************************************
2605  *                              midiInOpen              [MMSYSTEM.304]
2606  */
2607 UINT16 WINAPI midiInOpen16(HMIDIIN16* lphMidiIn, UINT16 uDeviceID,
2608                            DWORD dwCallback, DWORD dwInstance, DWORD dwFlags)
2609 {
2610     HMIDIIN     xhmid;
2611     UINT        ret;
2612
2613     ret = MMSYSTEM_midiInOpen(&xhmid, uDeviceID, dwCallback, dwInstance, 
2614                               dwFlags, FALSE);
2615     
2616     if (lphMidiIn) *lphMidiIn = xhmid;
2617     return ret;
2618 }
2619
2620 /**************************************************************************
2621  *                              midiInClose             [WINMM.58]
2622  */
2623 UINT WINAPI midiInClose(HMIDIIN hMidiIn)
2624 {
2625     LPWINE_MLD          wmld;
2626     DWORD               dwRet;    
2627
2628     TRACE("(%04X)\n", hMidiIn);
2629
2630     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2631         return MMSYSERR_INVALHANDLE;
2632
2633     dwRet = MMDRV_Close(wmld, MIDM_CLOSE);
2634     MMDRV_Free(hMidiIn, wmld);
2635     return dwRet;
2636 }
2637
2638 /**************************************************************************
2639  *                              midiInClose             [MMSYSTEM.305]
2640  */
2641 UINT16 WINAPI midiInClose16(HMIDIIN16 hMidiIn)
2642 {
2643     return midiInClose(hMidiIn);
2644 }
2645
2646 /**************************************************************************
2647  *                              midiInPrepareHeader     [WINMM.67]
2648  */
2649 UINT WINAPI midiInPrepareHeader(HMIDIIN hMidiIn, 
2650                                 MIDIHDR* lpMidiInHdr, UINT uSize)
2651 {
2652     LPWINE_MLD          wmld;
2653     
2654     TRACE("(%04X, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
2655
2656     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2657         return MMSYSERR_INVALHANDLE;
2658
2659     return MMDRV_Message(wmld, MIDM_PREPARE, (DWORD)lpMidiInHdr, uSize, TRUE);
2660 }
2661
2662 /**************************************************************************
2663  *                              midiInPrepareHeader     [MMSYSTEM.306]
2664  */
2665 UINT16 WINAPI midiInPrepareHeader16(HMIDIIN16 hMidiIn,
2666                                     MIDIHDR16* /*SEGPTR*/ lpsegMidiInHdr, 
2667                                     UINT16 uSize)
2668 {
2669     LPWINE_MLD          wmld;
2670
2671     TRACE("(%04X, %p, %d)\n", hMidiIn, lpsegMidiInHdr, uSize);
2672
2673     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2674         return MMSYSERR_INVALHANDLE;
2675
2676     return MMDRV_Message(wmld, MIDM_PREPARE, (DWORD)lpsegMidiInHdr, uSize, FALSE);
2677 }
2678
2679 /**************************************************************************
2680  *                              midiInUnprepareHeader   [WINMM.71]
2681  */
2682 UINT WINAPI midiInUnprepareHeader(HMIDIIN hMidiIn,
2683                                   MIDIHDR* lpMidiInHdr, UINT uSize)
2684 {
2685     LPWINE_MLD          wmld;
2686
2687     TRACE("(%04X, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
2688
2689     if (!(lpMidiInHdr->dwFlags & MHDR_PREPARED)) {
2690         return MMSYSERR_NOERROR;
2691     }
2692
2693     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2694         return MMSYSERR_INVALHANDLE;
2695
2696     return MMDRV_Message(wmld, MIDM_UNPREPARE, (DWORD)lpMidiInHdr, uSize, TRUE);
2697 }
2698
2699 /**************************************************************************
2700  *                              midiInUnprepareHeader   [MMSYSTEM.307]
2701  */
2702 UINT16 WINAPI midiInUnprepareHeader16(HMIDIIN16 hMidiIn,
2703                                       MIDIHDR16* /* SEGPTR */ lpsegMidiInHdr, 
2704                                       UINT16 uSize)
2705 {
2706     LPWINE_MLD          wmld;
2707     LPMIDIHDR16         lpMidiInHdr = PTR_SEG_TO_LIN(lpsegMidiInHdr);
2708
2709     TRACE("(%04X, %p, %d)\n", hMidiIn, lpsegMidiInHdr, uSize);
2710
2711     if (!(lpMidiInHdr->dwFlags & MHDR_PREPARED)) {
2712         return MMSYSERR_NOERROR;
2713     }
2714
2715     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2716         return MMSYSERR_INVALHANDLE;
2717
2718     return MMDRV_Message(wmld, MIDM_UNPREPARE, (DWORD)lpsegMidiInHdr, uSize, FALSE);
2719 }
2720
2721 /**************************************************************************
2722  *                              midiInAddBuffer         [WINMM.57]
2723  */
2724 UINT WINAPI midiInAddBuffer(HMIDIIN hMidiIn,
2725                             MIDIHDR* lpMidiInHdr, UINT uSize)
2726 {
2727     LPWINE_MLD          wmld;
2728
2729     TRACE("(%04X, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
2730
2731     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2732         return MMSYSERR_INVALHANDLE;
2733
2734     return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD)lpMidiInHdr, uSize, TRUE);
2735 }
2736
2737 /**************************************************************************
2738  *                              midiInAddBuffer         [MMSYSTEM.308]
2739  */
2740 UINT16 WINAPI midiInAddBuffer16(HMIDIIN16 hMidiIn,
2741                                 MIDIHDR16* /* SEGPTR */ lpsegMidiInHdr, 
2742                                 UINT16 uSize)
2743 {
2744     LPWINE_MLD          wmld;
2745
2746     TRACE("(%04X, %p, %d)\n", hMidiIn, lpsegMidiInHdr, uSize);
2747
2748     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2749         return MMSYSERR_INVALHANDLE;
2750
2751     return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD)lpsegMidiInHdr, uSize, FALSE);
2752 }
2753
2754 /**************************************************************************
2755  *                              midiInStart                     [WINMM.69]
2756  */
2757 UINT WINAPI midiInStart(HMIDIIN hMidiIn)
2758 {
2759     LPWINE_MLD          wmld;
2760
2761     TRACE("(%04X\n", hMidiIn);
2762
2763     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2764         return MMSYSERR_INVALHANDLE;
2765
2766     return MMDRV_Message(wmld, MIDM_START, 0L, 0L, TRUE);
2767 }
2768
2769 /**************************************************************************
2770  *                              midiInStart                     [MMSYSTEM.309]
2771  */
2772 UINT16 WINAPI midiInStart16(HMIDIIN16 hMidiIn)
2773 {
2774     return midiInStart(hMidiIn);
2775 }
2776
2777 /**************************************************************************
2778  *                              midiInStop                      [WINMM.70]
2779  */
2780 UINT WINAPI midiInStop(HMIDIIN hMidiIn)
2781 {
2782     LPWINE_MLD          wmld;
2783
2784     TRACE("(%04X\n", hMidiIn);
2785
2786     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2787         return MMSYSERR_INVALHANDLE;
2788
2789     return MMDRV_Message(wmld, MIDM_STOP, 0L, 0L, TRUE);
2790 }
2791
2792 /**************************************************************************
2793  *                              midiInStop                      [MMSYSTEM.310]
2794  */
2795 UINT16 WINAPI midiInStop16(HMIDIIN16 hMidiIn)
2796 {
2797     return midiInStop(hMidiIn);
2798 }
2799
2800 /**************************************************************************
2801  *                              midiInReset                     [WINMM.68]
2802  */
2803 UINT WINAPI midiInReset(HMIDIIN hMidiIn)
2804 {
2805     LPWINE_MLD          wmld;
2806
2807     TRACE("(%04X\n", hMidiIn);
2808
2809     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2810         return MMSYSERR_INVALHANDLE;
2811
2812     return MMDRV_Message(wmld, MIDM_RESET, 0L, 0L, TRUE);
2813 }
2814
2815 /**************************************************************************
2816  *                              midiInReset                     [MMSYSTEM.311]
2817  */
2818 UINT16 WINAPI midiInReset16(HMIDIIN16 hMidiIn)
2819 {
2820     return midiInReset(hMidiIn);
2821 }
2822
2823 /**************************************************************************
2824  *                              midiInGetID                     [WINMM.63]
2825  */
2826 UINT WINAPI midiInGetID(HMIDIIN hMidiIn, UINT* lpuDeviceID)
2827 {
2828     LPWINE_MLD          wmld;
2829
2830     TRACE("(%04X, %p)\n", hMidiIn, lpuDeviceID);
2831
2832     if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
2833
2834     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, TRUE)) == NULL) 
2835         return MMSYSERR_INVALHANDLE;
2836
2837     *lpuDeviceID = wmld->uDeviceID;
2838     
2839     return MMSYSERR_NOERROR;
2840 }
2841
2842 /**************************************************************************
2843  *                              midiInGetID                     [MMSYSTEM.312]
2844  */
2845 UINT16 WINAPI midiInGetID16(HMIDIIN16 hMidiIn, UINT16* lpuDeviceID)
2846 {
2847     LPWINE_MLD          wmld;
2848
2849     TRACE("(%04X, %p)\n", hMidiIn, lpuDeviceID);
2850
2851     if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
2852
2853     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, TRUE)) == NULL) 
2854         return MMSYSERR_INVALHANDLE;
2855
2856     *lpuDeviceID = wmld->uDeviceID;
2857     
2858     return MMSYSERR_NOERROR;
2859 }
2860
2861 /**************************************************************************
2862  *                              midiInMessage           [WINMM.65]
2863  */
2864 DWORD WINAPI midiInMessage(HMIDIIN hMidiIn, UINT uMessage, 
2865                            DWORD dwParam1, DWORD dwParam2)
2866 {
2867     LPWINE_MLD          wmld;
2868
2869     TRACE("(%04X, %04X, %08lX, %08lX)\n", hMidiIn, uMessage, dwParam1, dwParam2);
2870
2871     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2872         return MMSYSERR_INVALHANDLE;
2873
2874     switch (uMessage) {
2875     case MIDM_OPEN:
2876     case MIDM_CLOSE:
2877         FIXME("can't handle OPEN or CLOSE message!\n");
2878         return MMSYSERR_NOTSUPPORTED;
2879     }
2880     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, TRUE);
2881 }
2882
2883 /**************************************************************************
2884  *                              midiInMessage           [MMSYSTEM.313]
2885  */
2886 DWORD WINAPI midiInMessage16(HMIDIIN16 hMidiIn, UINT16 uMessage, 
2887                              DWORD dwParam1, DWORD dwParam2)
2888 {
2889     LPWINE_MLD          wmld;
2890
2891     TRACE("(%04X, %04X, %08lX, %08lX)\n", hMidiIn, uMessage, dwParam1, dwParam2);
2892
2893     switch (uMessage) {
2894     case MIDM_OPEN:
2895     case MIDM_CLOSE:
2896         FIXME("can't handle OPEN or CLOSE message!\n");
2897         return MMSYSERR_NOTSUPPORTED;
2898
2899     case MIDM_GETDEVCAPS:
2900         return midiInGetDevCaps16(hMidiIn, (LPMIDIINCAPS16)PTR_SEG_TO_LIN(dwParam1), dwParam2);
2901     case MIDM_PREPARE:
2902         return midiInPrepareHeader16(hMidiIn, (LPMIDIHDR16)PTR_SEG_TO_LIN(dwParam1), dwParam2);
2903     case MIDM_UNPREPARE:
2904         return midiInUnprepareHeader16(hMidiIn, (LPMIDIHDR16)PTR_SEG_TO_LIN(dwParam1), dwParam2);
2905     case MIDM_ADDBUFFER:
2906         return midiInAddBuffer16(hMidiIn, (LPMIDIHDR16)PTR_SEG_TO_LIN(dwParam1), dwParam2);    
2907     }
2908
2909     if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL) 
2910         return MMSYSERR_INVALHANDLE;
2911
2912     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, FALSE);
2913 }
2914
2915 typedef struct WINE_MIDIStream {
2916     HMIDIOUT                    hDevice;
2917     HANDLE                      hThread;
2918     DWORD                       dwThreadID;
2919     DWORD                       dwTempo;
2920     DWORD                       dwTimeDiv;
2921     DWORD                       dwPositionMS;
2922     DWORD                       dwPulses;
2923     DWORD                       dwStartTicks;
2924     WORD                        wFlags;
2925     HANDLE                      hEvent;
2926     LPMIDIHDR                   lpMidiHdr;
2927 } WINE_MIDIStream;
2928
2929 #define WINE_MSM_HEADER         (WM_USER+0)
2930 #define WINE_MSM_STOP           (WM_USER+1)
2931
2932 /**************************************************************************
2933  *                              MMSYSTEM_GetMidiStream          [internal]
2934  */
2935 static  BOOL    MMSYSTEM_GetMidiStream(HMIDISTRM hMidiStrm, WINE_MIDIStream** lpMidiStrm, WINE_MIDI** lplpwm)
2936 {
2937     WINE_MIDI* lpwm = (LPWINE_MIDI)MMDRV_Get(hMidiStrm, MMDRV_MIDIOUT, FALSE);
2938
2939     if (lplpwm)
2940         *lplpwm = lpwm;
2941
2942     if (lpwm == NULL) {
2943         return FALSE;
2944     }
2945
2946     *lpMidiStrm = (WINE_MIDIStream*)lpwm->mod.rgIds.dwStreamID;
2947
2948     return *lpMidiStrm != NULL;
2949 }
2950
2951 /**************************************************************************
2952  *                              MMSYSTEM_MidiStream_Convert     [internal]
2953  */
2954 static  DWORD   MMSYSTEM_MidiStream_Convert(WINE_MIDIStream* lpMidiStrm, DWORD pulse)
2955 {
2956     DWORD       ret = 0;
2957     
2958     if (lpMidiStrm->dwTimeDiv == 0) {
2959         FIXME("Shouldn't happen. lpMidiStrm->dwTimeDiv = 0\n");
2960     } else if (lpMidiStrm->dwTimeDiv > 0x8000) { /* SMPTE, unchecked FIXME? */
2961         int     nf = -(char)HIBYTE(lpMidiStrm->dwTimeDiv);      /* number of frames     */
2962         int     nsf = LOBYTE(lpMidiStrm->dwTimeDiv);            /* number of sub-frames */
2963         ret = (pulse * 1000) / (nf * nsf);
2964     } else {
2965         ret = (DWORD)((double)pulse * ((double)lpMidiStrm->dwTempo / 1000) /    
2966                       (double)lpMidiStrm->dwTimeDiv);
2967     }
2968     
2969     return ret;
2970 }
2971
2972 /**************************************************************************
2973  *                      MMSYSTEM_MidiStream_MessageHandler      [internal]
2974  */
2975 static  BOOL    MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream* lpMidiStrm, LPWINE_MIDI lpwm, LPMSG msg)
2976 {
2977     LPMIDIHDR   lpMidiHdr;
2978     LPMIDIHDR*  lpmh;
2979     LPBYTE      lpData;
2980
2981     switch (msg->message) {
2982     case WM_QUIT:
2983         SetEvent(lpMidiStrm->hEvent);
2984         return FALSE;
2985     case WINE_MSM_STOP:
2986         TRACE("STOP\n");
2987         /* this is not quite what MS doc says... */
2988         midiOutReset(lpMidiStrm->hDevice);
2989         /* empty list of already submitted buffers */
2990         for (lpMidiHdr = lpMidiStrm->lpMidiHdr; lpMidiHdr; lpMidiHdr = (LPMIDIHDR)lpMidiHdr->lpNext) {
2991             lpMidiHdr->dwFlags |= MHDR_DONE;
2992             lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
2993             
2994             DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags, lpMidiStrm->hDevice, 
2995                            MM_MOM_DONE, lpwm->mod.dwInstance, (DWORD)lpMidiHdr, 0L);
2996         }
2997         lpMidiStrm->lpMidiHdr = 0;
2998         SetEvent(lpMidiStrm->hEvent);
2999         break;
3000     case WINE_MSM_HEADER:
3001         /* sets initial tick count for first MIDIHDR */
3002         if (!lpMidiStrm->dwStartTicks)
3003             lpMidiStrm->dwStartTicks = GetTickCount();
3004         
3005         /* FIXME(EPP): "I don't understand the content of the first MIDIHDR sent
3006          * by native mcimidi, it doesn't look like a correct one".
3007          * this trick allows to throw it away... but I don't like it. 
3008          * It looks like part of the file I'm trying to play and definitively looks 
3009          * like raw midi content
3010          * I'd really like to understand why native mcimidi sends it. Perhaps a bad
3011          * synchronization issue where native mcimidi is still processing raw MIDI 
3012          * content before generating MIDIEVENTs ?
3013          *
3014          * 4c 04 89 3b 00 81 7c 99 3b 43 00 99 23 5e 04 89 L..;..|.;C..#^..
3015          * 3b 00 00 89 23 00 7c 99 3b 45 00 99 28 62 04 89 ;...#.|.;E..(b..
3016          * 3b 00 00 89 28 00 81 7c 99 3b 4e 00 99 23 5e 04 ;...(..|.;N..#^.
3017          * 89 3b 00 00 89 23 00 7c 99 3b 45 00 99 23 78 04 .;...#.|.;E..#x.
3018          * 89 3b 00 00 89 23 00 81 7c 99 3b 48 00 99 23 5e .;...#..|.;H..#^
3019          * 04 89 3b 00 00 89 23 00 7c 99 3b 4e 00 99 28 62 ..;...#.|.;N..(b
3020          * 04 89 3b 00 00 89 28 00 81 7c 99 39 4c 00 99 23 ..;...(..|.9L..#
3021          * 5e 04 89 39 00 00 89 23 00 82 7c 99 3b 4c 00 99 ^..9...#..|.;L..
3022          * 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 00 99 #^..;...#.|.;H..
3023          * 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b 3f 04 (b..;...(..|.;?.
3024          * 89 3b 00 1c 99 23 5e 04 89 23 00 5c 99 3b 45 00 .;...#^..#.\.;E.
3025          * 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 3b 46 .#x..;...#..|.;F
3026          * 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 ..#^..;...#.|.;H
3027          * 00 99 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b ..(b..;...(..|.;
3028          * 46 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b F..#^..;...#.|.;
3029          * 48 00 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 H..#x..;...#..|.
3030          * 3b 4c 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 ;L..#^..;...#.|.
3031          */
3032         lpMidiHdr = (LPMIDIHDR)msg->lParam;
3033         lpData = lpMidiHdr->lpData;
3034         TRACE("Adding %s lpMidiHdr=%p [lpData=0x%08lx dwBufferLength=%lu/%lu dwFlags=0x%08lx size=%u]\n", 
3035               (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular", lpMidiHdr, 
3036               (DWORD)lpMidiHdr, lpMidiHdr->dwBufferLength, lpMidiHdr->dwBytesRecorded, 
3037               lpMidiHdr->dwFlags, msg->wParam);
3038 #if 0
3039         /* dumps content of lpMidiHdr->lpData
3040          * FIXME: there should be a debug routine somewhere that already does this
3041          * I hate spreading this type of shit all around the code 
3042          */ 
3043         for (dwToGo = 0; dwToGo < lpMidiHdr->dwBufferLength; dwToGo += 16) {
3044             DWORD       i;
3045             BYTE        ch;
3046             
3047             for (i = 0; i < MIN(16, lpMidiHdr->dwBufferLength - dwToGo); i++)
3048                 printf("%02x ", lpData[dwToGo + i]);
3049             for (; i < 16; i++)
3050                 printf("   ");
3051             for (i = 0; i < MIN(16, lpMidiHdr->dwBufferLength - dwToGo); i++) {
3052                 ch = lpData[dwToGo + i];
3053                 printf("%c", (ch >= 0x20 && ch <= 0x7F) ? ch : '.');
3054             }
3055             printf("\n");
3056         }
3057 #endif
3058         if (((LPMIDIEVENT)lpData)->dwStreamID != 0 && 
3059             ((LPMIDIEVENT)lpData)->dwStreamID != 0xFFFFFFFF &&
3060             ((LPMIDIEVENT)lpData)->dwStreamID != (DWORD)lpMidiStrm) {
3061             FIXME("Dropping bad %s lpMidiHdr (streamID=%08lx)\n", 
3062                   (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular", 
3063                   ((LPMIDIEVENT)lpData)->dwStreamID);
3064             lpMidiHdr->dwFlags |= MHDR_DONE;
3065             lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
3066             
3067             DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags, lpMidiStrm->hDevice, 
3068                            MM_MOM_DONE, lpwm->mod.dwInstance, (DWORD)lpMidiHdr, 0L);
3069             break;
3070         } 
3071         
3072         for (lpmh = &lpMidiStrm->lpMidiHdr; *lpmh; lpmh = (LPMIDIHDR*)&((*lpmh)->lpNext));
3073         *lpmh = lpMidiHdr;
3074         lpMidiHdr = (LPMIDIHDR)msg->lParam;
3075         lpMidiHdr->lpNext = 0;
3076         lpMidiHdr->dwFlags |= MHDR_INQUEUE;
3077         lpMidiHdr->dwFlags &= MHDR_DONE;
3078         lpMidiHdr->dwOffset = 0;
3079         
3080         break;
3081     default:
3082         FIXME("Unknown message %d\n", msg->message);
3083         break;
3084     }
3085     return TRUE;
3086 }
3087
3088 /**************************************************************************
3089  *                              MMSYSTEM_MidiStream_Player      [internal]
3090  */
3091 static  DWORD   CALLBACK        MMSYSTEM_MidiStream_Player(LPVOID pmt)
3092 {
3093     WINE_MIDIStream*    lpMidiStrm = pmt;
3094     WINE_MIDI*          lpwm;
3095     MSG                 msg;
3096     DWORD               dwToGo;
3097     DWORD               dwCurrTC;
3098     LPMIDIHDR           lpMidiHdr;
3099     LPMIDIEVENT         me;
3100     LPBYTE              lpData = 0;
3101
3102     TRACE("(%p)!\n", lpMidiStrm);
3103
3104     if (!lpMidiStrm || 
3105         (lpwm = (LPWINE_MIDI)MMDRV_Get(lpMidiStrm->hDevice, MMDRV_MIDIOUT, FALSE)) == NULL)
3106         goto the_end;
3107
3108     /* force thread's queue creation */
3109     /* Used to be InitThreadInput16(0, 5); */
3110     /* but following works also with hack in midiStreamOpen */
3111     Callout.PeekMessageA(&msg, 0, 0, 0, 0);
3112
3113     /* FIXME: this next line must be called before midiStreamOut or midiStreamRestart are called */
3114     SetEvent(lpMidiStrm->hEvent);
3115     TRACE("Ready to go 1\n");
3116     /* thread is started in paused mode */
3117     SuspendThread(lpMidiStrm->hThread);
3118     TRACE("Ready to go 2\n");
3119
3120     lpMidiStrm->dwStartTicks = 0;    
3121     lpMidiStrm->dwPulses = 0;
3122
3123     lpMidiStrm->lpMidiHdr = 0;
3124
3125     for (;;) {
3126         lpMidiHdr = lpMidiStrm->lpMidiHdr;
3127         if (!lpMidiHdr) {
3128             /* for first message, block until one arrives, then process all that are available */
3129             Callout.GetMessageA(&msg, 0, 0, 0);
3130             do {
3131                 if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
3132                     goto the_end;
3133             } while (Callout.PeekMessageA(&msg, 0, 0, 0, PM_REMOVE));
3134             lpData = 0;
3135             continue;
3136         }
3137
3138         if (!lpData)
3139             lpData = lpMidiHdr->lpData;
3140             
3141         me = (LPMIDIEVENT)(lpData + lpMidiHdr->dwOffset);
3142         
3143         /* do we have to wait ? */
3144         if (me->dwDeltaTime) {
3145             lpMidiStrm->dwPositionMS += MMSYSTEM_MidiStream_Convert(lpMidiStrm, me->dwDeltaTime);
3146             lpMidiStrm->dwPulses += me->dwDeltaTime;
3147
3148             dwToGo = lpMidiStrm->dwStartTicks + lpMidiStrm->dwPositionMS;
3149                             
3150             TRACE("%ld/%ld/%ld\n", dwToGo, GetTickCount(), me->dwDeltaTime);
3151             while ((dwCurrTC = GetTickCount()) < dwToGo) {
3152                 if (MsgWaitForMultipleObjects(0, NULL, FALSE, dwToGo - dwCurrTC, QS_ALLINPUT) == WAIT_OBJECT_0) {
3153                     /* got a message, handle it */
3154                     while (Callout.PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
3155                         if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
3156                             goto the_end;
3157                     }
3158                     lpData = 0;
3159                 } else {
3160                     /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
3161                     break;
3162                 }
3163             }               
3164         }
3165         switch (MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK)) {
3166         case MEVT_COMMENT:
3167             FIXME("NIY: MEVT_COMMENT\n");
3168             /* do nothing, skip bytes */
3169             break;
3170         case MEVT_LONGMSG:
3171             FIXME("NIY: MEVT_LONGMSG, aka sending Sysex event\n");
3172             break;
3173         case MEVT_NOP:
3174             break;
3175         case MEVT_SHORTMSG:
3176             midiOutShortMsg(lpMidiStrm->hDevice, MEVT_EVENTPARM(me->dwEvent));
3177             break;
3178         case MEVT_TEMPO:
3179             lpMidiStrm->dwTempo = MEVT_EVENTPARM(me->dwEvent);
3180             break;
3181         case MEVT_VERSION:
3182             break;
3183         default:
3184             FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK));
3185             break;
3186         }
3187         if (me->dwEvent & MEVT_F_CALLBACK) {
3188             DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags, lpMidiStrm->hDevice, 
3189                            MM_MOM_POSITIONCB, lpwm->mod.dwInstance, (LPARAM)lpMidiHdr, 0L);
3190         }
3191         lpMidiHdr->dwOffset += sizeof(MIDIEVENT);
3192         if (me->dwEvent & MEVT_F_LONG) 
3193             lpMidiHdr->dwOffset += (MEVT_EVENTPARM(me->dwEvent) + 3) & ~3;
3194         if (lpMidiHdr->dwOffset >= lpMidiHdr->dwBufferLength) {
3195             /* done with this header */
3196             lpMidiHdr->dwFlags |= MHDR_DONE;
3197             lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
3198             
3199             lpMidiStrm->lpMidiHdr = (LPMIDIHDR)lpMidiHdr->lpNext;
3200             DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags, lpMidiStrm->hDevice, 
3201                            MM_MOM_DONE, lpwm->mod.dwInstance, (DWORD)lpMidiHdr, 0L);
3202             lpData = 0;
3203         }
3204     }
3205 the_end:
3206     TRACE("End of thread\n");
3207     ExitThread(0);
3208     return 0;   /* for removing the warning, never executed */
3209 }
3210
3211 /**************************************************************************
3212  *                              MMSYSTEM_MidiStream_PostMessage [internal]
3213  */
3214 static  BOOL MMSYSTEM_MidiStream_PostMessage(WINE_MIDIStream* lpMidiStrm, WORD msg, DWORD pmt1, DWORD pmt2)
3215 {
3216     if (Callout.PostThreadMessageA(lpMidiStrm->dwThreadID, msg, pmt1, pmt2)) {
3217         DWORD   count;
3218         BOOL    bHasWin16Lock;
3219
3220         /* FIXME: should use the new syslevel APIs */
3221         if ((bHasWin16Lock = _ConfirmWin16Lock()) != 0) {
3222             ReleaseThunkLock(&count);
3223         }
3224         WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
3225         if (bHasWin16Lock) {
3226             RestoreThunkLock(count);
3227         }
3228     } else {
3229         WARN("bad PostThreadMessageA\n");
3230         return FALSE;
3231     }   
3232     return TRUE;
3233 }
3234
3235 /**************************************************************************
3236  *                              midiStreamClose                 [WINMM.90]
3237  */
3238 MMRESULT WINAPI midiStreamClose(HMIDISTRM hMidiStrm)
3239 {
3240     WINE_MIDIStream*    lpMidiStrm;
3241
3242     TRACE("(%08x)!\n", hMidiStrm);
3243
3244     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
3245         return MMSYSERR_INVALHANDLE;
3246
3247     midiStreamStop(hMidiStrm);
3248     MMSYSTEM_MidiStream_PostMessage(lpMidiStrm, WM_QUIT, 0, 0);
3249     HeapFree(GetProcessHeap(), 0, lpMidiStrm);
3250     CloseHandle(lpMidiStrm->hEvent);
3251
3252     return midiOutClose(hMidiStrm);
3253 }
3254
3255 /**************************************************************************
3256  *                              MMSYSTEM_MidiStream_Open        [internal]
3257  */
3258 static  MMRESULT WINAPI MMSYSTEM_MidiStream_Open(HMIDISTRM* lphMidiStrm, LPUINT lpuDeviceID, 
3259                                                  DWORD cMidi, DWORD dwCallback, 
3260                                                  DWORD dwInstance, DWORD fdwOpen, BOOL bFrom32) 
3261 {
3262     WINE_MIDIStream*    lpMidiStrm;
3263     MMRESULT            ret;
3264     MIDIOPENSTRMID      mosm;
3265     LPWINE_MIDI         lpwm;
3266     HMIDIOUT            hMidiOut;
3267
3268     TRACE("(%p, %p, %ld, 0x%08lx, 0x%08lx, 0x%08lx)!\n",
3269           lphMidiStrm, lpuDeviceID, cMidi, dwCallback, dwInstance, fdwOpen);
3270
3271     if (cMidi != 1 || lphMidiStrm == NULL || lpuDeviceID == NULL)
3272         return MMSYSERR_INVALPARAM;
3273
3274     lpMidiStrm = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream));
3275     if (!lpMidiStrm)
3276         return MMSYSERR_NOMEM;
3277
3278     lpMidiStrm->dwTempo = 500000;
3279     lpMidiStrm->dwTimeDiv = 480;        /* 480 is 120 quater notes per minute *//* FIXME ??*/
3280     lpMidiStrm->dwPositionMS = 0;
3281
3282     mosm.dwStreamID = (DWORD)lpMidiStrm;
3283     /* FIXME: the correct value is not allocated yet for MAPPER */
3284     mosm.wDeviceID  = *lpuDeviceID;
3285     lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &fdwOpen, 1, &mosm, bFrom32);
3286     lpMidiStrm->hDevice = hMidiOut;
3287     if (lphMidiStrm)
3288         *lphMidiStrm = hMidiOut;
3289
3290     /* FIXME: is lpuDevice initialized upon entering midiStreamOpen ? */
3291     FIXME("*lpuDeviceID=%u\n", *lpuDeviceID);
3292     lpwm->mld.uDeviceID = *lpuDeviceID = 0;
3293
3294     ret = MMDRV_Open(&lpwm->mld, MODM_OPEN, (DWORD)&lpwm->mod, fdwOpen);
3295     lpMidiStrm->hEvent = CreateEventA(NULL, FALSE, FALSE, NULL);
3296     lpMidiStrm->wFlags = HIWORD(fdwOpen);
3297
3298     lpMidiStrm->hThread = CreateThread(NULL, 0, MMSYSTEM_MidiStream_Player, 
3299                                        lpMidiStrm, 0, &(lpMidiStrm->dwThreadID));
3300
3301     if (!lpMidiStrm->hThread) {
3302         midiStreamClose((HMIDISTRM)hMidiOut);
3303         return MMSYSERR_NOMEM;  
3304     }
3305
3306     /* wait for thread to have started, and for it's queue to be created */
3307     {
3308         DWORD   count;
3309         BOOL    bHasWin16Lock;
3310
3311         /* (Release|Restore)ThunkLock() is needed when this method is called from 16 bit code, 
3312          * (meaning the Win16Lock is set), so that it's released and the 32 bit thread running 
3313          * MMSYSTEM_MidiStreamPlayer can acquire Win16Lock to create its queue.
3314          */
3315         if ((bHasWin16Lock = _ConfirmWin16Lock()) != 0) {
3316             ReleaseThunkLock(&count);
3317         }
3318         WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
3319         if (bHasWin16Lock) {
3320             RestoreThunkLock(count);
3321         }
3322     }
3323
3324     TRACE("=> (%u/%d) hMidi=0x%04x ret=%d lpMidiStrm=%p\n", 
3325           *lpuDeviceID, lpwm->mld.uDeviceID, *lphMidiStrm, ret, lpMidiStrm);    
3326     return ret;
3327 }
3328
3329 /**************************************************************************
3330  *                              midiStreamOpen                  [WINMM.91]
3331  */
3332 MMRESULT WINAPI midiStreamOpen(HMIDISTRM* lphMidiStrm, LPUINT lpuDeviceID, 
3333                                DWORD cMidi, DWORD dwCallback, 
3334                                DWORD dwInstance, DWORD fdwOpen) 
3335 {
3336     return MMSYSTEM_MidiStream_Open(lphMidiStrm, lpuDeviceID, cMidi, dwCallback, 
3337                                     dwInstance, fdwOpen, TRUE);
3338 }
3339
3340 /**************************************************************************
3341  *                              midiStreamOut                   [WINMM.92]
3342  */
3343 MMRESULT WINAPI midiStreamOut(HMIDISTRM hMidiStrm, LPMIDIHDR lpMidiHdr, 
3344                               UINT cbMidiHdr) 
3345 {
3346     WINE_MIDIStream*    lpMidiStrm;
3347     DWORD               ret = MMSYSERR_NOERROR;
3348
3349     TRACE("(%08x, %p, %u)!\n", hMidiStrm, lpMidiHdr, cbMidiHdr);
3350
3351     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
3352         ret = MMSYSERR_INVALHANDLE;
3353     } else {
3354         if (!Callout.PostThreadMessageA(lpMidiStrm->dwThreadID, 
3355                                         WINE_MSM_HEADER, cbMidiHdr, 
3356                                         (DWORD)lpMidiHdr)) {
3357             WARN("bad PostThreadMessageA\n");
3358             ret = MMSYSERR_ERROR;
3359         }
3360     }
3361     return ret;
3362 }
3363
3364 /**************************************************************************
3365  *                              midiStreamPause                 [WINMM.93]
3366  */
3367 MMRESULT WINAPI midiStreamPause(HMIDISTRM hMidiStrm) 
3368 {
3369     WINE_MIDIStream*    lpMidiStrm;
3370     DWORD               ret = MMSYSERR_NOERROR;
3371
3372     TRACE("(%08x)!\n", hMidiStrm);
3373
3374     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
3375         ret = MMSYSERR_INVALHANDLE;
3376     } else {
3377         if (SuspendThread(lpMidiStrm->hThread) == 0xFFFFFFFF) {
3378             WARN("bad Suspend (%ld)\n", GetLastError());
3379             ret = MMSYSERR_ERROR;
3380         }  
3381     }
3382     return ret;
3383 }
3384
3385 /**************************************************************************
3386  *                              midiStreamPosition              [WINMM.94]
3387  */
3388 MMRESULT WINAPI midiStreamPosition(HMIDISTRM hMidiStrm, LPMMTIME lpMMT, UINT cbmmt) 
3389 {
3390     WINE_MIDIStream*    lpMidiStrm;
3391     DWORD               ret = MMSYSERR_NOERROR;
3392
3393     TRACE("(%08x, %p, %u)!\n", hMidiStrm, lpMMT, cbmmt);
3394
3395     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
3396         ret = MMSYSERR_INVALHANDLE;
3397     } else if (lpMMT == NULL || cbmmt != sizeof(MMTIME)) {
3398         ret = MMSYSERR_INVALPARAM;
3399     } else {
3400         switch (lpMMT->wType) {
3401         case TIME_MS:   
3402             lpMMT->u.ms = lpMidiStrm->dwPositionMS;     
3403             TRACE("=> %ld ms\n", lpMMT->u.ms);
3404             break;
3405         case TIME_TICKS:
3406             lpMMT->u.ticks = lpMidiStrm->dwPulses;      
3407             TRACE("=> %ld ticks\n", lpMMT->u.ticks);
3408             break;
3409         default:
3410             WARN("Unsupported time type %d\n", lpMMT->wType);
3411             lpMMT->wType = TIME_MS;
3412             ret = MMSYSERR_INVALPARAM;
3413             break;
3414         }
3415     }
3416     return ret;
3417 }
3418
3419 /**************************************************************************
3420  *                              midiStreamProperty              [WINMM.95]
3421  */
3422 MMRESULT WINAPI midiStreamProperty(HMIDISTRM hMidiStrm, LPBYTE lpPropData, DWORD dwProperty) 
3423 {
3424     WINE_MIDIStream*    lpMidiStrm;
3425     MMRESULT            ret = MMSYSERR_NOERROR;
3426
3427     TRACE("(%08x, %p, %lx)\n", hMidiStrm, lpPropData, dwProperty);
3428
3429     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
3430         ret = MMSYSERR_INVALHANDLE;
3431     } else if ((dwProperty & (MIDIPROP_GET|MIDIPROP_SET)) == 0) {
3432         ret = MMSYSERR_INVALPARAM;
3433     } else if (dwProperty & MIDIPROP_TEMPO) {
3434         MIDIPROPTEMPO*  mpt = (MIDIPROPTEMPO*)lpPropData;
3435         
3436         if (sizeof(MIDIPROPTEMPO) != mpt->cbStruct) {
3437             ret = MMSYSERR_INVALPARAM;
3438         } else if (dwProperty & MIDIPROP_SET) {
3439             lpMidiStrm->dwTempo = mpt->dwTempo;
3440             TRACE("Setting tempo to %ld\n", mpt->dwTempo);
3441         } else if (dwProperty & MIDIPROP_GET) {
3442             mpt->dwTempo = lpMidiStrm->dwTempo;
3443             TRACE("Getting tempo <= %ld\n", mpt->dwTempo);
3444         }
3445     } else if (dwProperty & MIDIPROP_TIMEDIV) {
3446         MIDIPROPTIMEDIV*        mptd = (MIDIPROPTIMEDIV*)lpPropData;
3447         
3448         if (sizeof(MIDIPROPTIMEDIV) != mptd->cbStruct) {
3449             ret = MMSYSERR_INVALPARAM;
3450         } else if (dwProperty & MIDIPROP_SET) {
3451             lpMidiStrm->dwTimeDiv = mptd->dwTimeDiv;
3452             TRACE("Setting time div to %ld\n", mptd->dwTimeDiv);
3453         } else if (dwProperty & MIDIPROP_GET) {
3454             mptd->dwTimeDiv = lpMidiStrm->dwTimeDiv;
3455             TRACE("Getting time div <= %ld\n", mptd->dwTimeDiv);
3456         }    
3457     } else {
3458         ret = MMSYSERR_INVALPARAM;
3459     }
3460
3461     return ret;
3462 }
3463
3464 /**************************************************************************
3465  *                              midiStreamRestart               [WINMM.96]
3466  */
3467 MMRESULT WINAPI midiStreamRestart(HMIDISTRM hMidiStrm) 
3468 {
3469     WINE_MIDIStream*    lpMidiStrm;
3470     MMRESULT            ret = MMSYSERR_NOERROR;
3471
3472     TRACE("(%08x)!\n", hMidiStrm);
3473
3474     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
3475         ret = MMSYSERR_INVALHANDLE;
3476     } else {
3477         if (ResumeThread(lpMidiStrm->hThread) == 0xFFFFFFFF) {
3478             WARN("bad Resume (%ld)\n", GetLastError());
3479             ret = MMSYSERR_ERROR;
3480         } else {
3481             lpMidiStrm->dwStartTicks = GetTickCount() - lpMidiStrm->dwPositionMS;
3482         }  
3483     }
3484     return ret;
3485 }
3486
3487 /**************************************************************************
3488  *                              midiStreamStop                  [WINMM.97]
3489  */
3490 MMRESULT WINAPI midiStreamStop(HMIDISTRM hMidiStrm) 
3491 {
3492     WINE_MIDIStream*    lpMidiStrm;
3493     MMRESULT            ret = MMSYSERR_NOERROR;
3494
3495     TRACE("(%08x)!\n", hMidiStrm);
3496
3497     if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
3498         ret = MMSYSERR_INVALHANDLE;
3499     } else {
3500         /* in case stream has been paused... FIXME is the current state correct ? */
3501         midiStreamRestart(hMidiStrm);
3502         MMSYSTEM_MidiStream_PostMessage(lpMidiStrm, WINE_MSM_STOP, 0, 0);
3503     }
3504     return ret;
3505 }
3506
3507 /**************************************************************************
3508  *                              midiStreamClose                 [MMSYSTEM.252]
3509  */
3510 MMRESULT16 WINAPI midiStreamClose16(HMIDISTRM16 hMidiStrm)
3511 {
3512     return midiStreamClose(hMidiStrm);
3513 }
3514
3515 /**************************************************************************
3516  *                              midiStreamOpen                  [MMSYSTEM.251]
3517  */
3518 MMRESULT16 WINAPI midiStreamOpen16(HMIDISTRM16* phMidiStrm, LPUINT16 devid, 
3519                                    DWORD cMidi, DWORD dwCallback, 
3520                                    DWORD dwInstance, DWORD fdwOpen) 
3521 {
3522     HMIDISTRM   hMidiStrm32;
3523     MMRESULT    ret;
3524     UINT        devid32;
3525
3526     if (!phMidiStrm || !devid)
3527         return MMSYSERR_INVALPARAM;
3528     devid32 = *devid;
3529     ret = MMSYSTEM_MidiStream_Open(&hMidiStrm32, &devid32, cMidi, dwCallback, 
3530                                    dwInstance, fdwOpen, FALSE);
3531     *phMidiStrm = hMidiStrm32;
3532     *devid = devid32;
3533     return ret;
3534 }
3535
3536 /**************************************************************************
3537  *                              midiStreamOut                   [MMSYSTEM.254]
3538  */
3539 MMRESULT16 WINAPI midiStreamOut16(HMIDISTRM16 hMidiStrm, LPMIDIHDR16 lpMidiHdr, UINT16 cbMidiHdr) 
3540 {
3541     return midiStreamOut(hMidiStrm, (LPMIDIHDR)lpMidiHdr, cbMidiHdr);
3542 }
3543
3544 /**************************************************************************
3545  *                              midiStreamPause                 [MMSYSTEM.255]
3546  */
3547 MMRESULT16 WINAPI midiStreamPause16(HMIDISTRM16 hMidiStrm) 
3548 {
3549     return midiStreamPause(hMidiStrm);
3550 }
3551
3552 /**************************************************************************
3553  *                              midiStreamPosition              [MMSYSTEM.253]
3554  */
3555 MMRESULT16 WINAPI midiStreamPosition16(HMIDISTRM16 hMidiStrm, LPMMTIME16 lpmmt16, UINT16 cbmmt) 
3556 {
3557     MMTIME      mmt32;
3558     MMRESULT    ret;
3559
3560     if (!lpmmt16)
3561         return MMSYSERR_INVALPARAM;
3562     MMSYSTEM_MMTIME16to32(&mmt32, lpmmt16);
3563     ret = midiStreamPosition(hMidiStrm, &mmt32, sizeof(MMTIME));
3564     MMSYSTEM_MMTIME32to16(lpmmt16, &mmt32);
3565     return ret;
3566 }
3567
3568 /**************************************************************************
3569  *                              midiStreamProperty              [MMSYSTEM.250]
3570  */
3571 MMRESULT16 WINAPI midiStreamProperty16(HMIDISTRM16 hMidiStrm, LPBYTE lpPropData, DWORD dwProperty) 
3572 {
3573     return midiStreamProperty(hMidiStrm, lpPropData, dwProperty);
3574 }
3575
3576 /**************************************************************************
3577  *                              midiStreamRestart               [MMSYSTEM.256]
3578  */
3579 MMRESULT16 WINAPI midiStreamRestart16(HMIDISTRM16 hMidiStrm) 
3580 {
3581     return midiStreamRestart(hMidiStrm);
3582 }
3583
3584 /**************************************************************************
3585  *                              midiStreamStop                  [MMSYSTEM.257]
3586  */
3587 MMRESULT16 WINAPI midiStreamStop16(HMIDISTRM16 hMidiStrm) 
3588 {
3589     return midiStreamStop(hMidiStrm);
3590 }
3591
3592 static  UINT WINAPI MMSYSTEM_waveOpen(HANDLE* lphndl, UINT uDeviceID, UINT uType,
3593                                       const LPWAVEFORMATEX lpFormat, 
3594                                       DWORD dwCallback, DWORD dwInstance, 
3595                                       DWORD dwFlags, BOOL bFrom32)
3596 {
3597     HANDLE              handle;
3598     LPWINE_MLD          wmld;
3599     DWORD               dwRet = MMSYSERR_NOERROR;
3600     WAVEOPENDESC        wod;
3601
3602     TRACE("(%p, %d, %s, %p, %08lX, %08lX, %08lX, %d);\n", 
3603           lphndl, (int)uDeviceID, (uType==MMDRV_WAVEOUT)?"Out":"In", lpFormat, dwCallback, 
3604           dwInstance, dwFlags, bFrom32?32:16);
3605
3606     if (dwFlags & WAVE_FORMAT_QUERY)    TRACE("WAVE_FORMAT_QUERY requested !\n");
3607
3608     if (lpFormat == NULL) return WAVERR_BADFORMAT;
3609
3610     TRACE("wFormatTag=%u, nChannels=%u, nSamplesPerSec=%lu, nAvgBytesPerSec=%lu, nBlockAlign=%u, wBitsPerSample=%u, cbSize=%u\n", 
3611           lpFormat->wFormatTag, lpFormat->nChannels, lpFormat->nSamplesPerSec, 
3612           lpFormat->nAvgBytesPerSec, lpFormat->nBlockAlign, lpFormat->wBitsPerSample, lpFormat->cbSize);
3613     
3614     if ((wmld = MMDRV_Alloc(sizeof(WINE_WAVE), uType, &handle, 
3615                             &dwFlags, &dwCallback, &dwInstance, bFrom32)) == NULL) 
3616         return MMSYSERR_NOMEM;
3617
3618     wod.hWave = handle;
3619     wod.lpFormat = lpFormat;  /* should the struct be copied iso pointer? */
3620     wod.dwCallback = dwCallback;
3621     wod.dwInstance = dwInstance;
3622     wod.uMappedDeviceID = 0;
3623     wod.dnDevNode = 0L;
3624
3625     /* when called from 16 bit code, mapper will be 0x0000FFFF instead of 0xFFFFFFFF */
3626     /* this should fix it */
3627     wmld->uDeviceID = (uDeviceID == (UINT16)-1 && !bFrom32) ? (UINT)-1 : uDeviceID;
3628
3629     dwRet = MMDRV_Open(wmld, (uType==MMDRV_WAVEOUT)?WODM_OPEN:WIDM_OPEN, (DWORD)&wod, dwFlags);
3630
3631     if ((dwFlags & WAVE_FORMAT_QUERY) || dwRet != MMSYSERR_NOERROR) {
3632         MMDRV_Free(handle, wmld);
3633         handle = 0;
3634     } 
3635
3636     if (lphndl != NULL) *lphndl = handle;
3637     TRACE("=> %ld hWave=%04x\n", dwRet, handle);
3638
3639     return dwRet;
3640 }
3641
3642 /**************************************************************************
3643  *                              waveOutGetNumDevs               [MMSYSTEM.401]
3644  */
3645 UINT WINAPI waveOutGetNumDevs(void) 
3646 {
3647     return MMDRV_GetNum(MMDRV_WAVEOUT);
3648 }
3649
3650 /**************************************************************************
3651  *                              waveOutGetNumDevs               [WINMM.167]
3652  */
3653 UINT16 WINAPI waveOutGetNumDevs16(void)
3654 {
3655     return MMDRV_GetNum(MMDRV_WAVEOUT);
3656 }
3657
3658 /**************************************************************************
3659  *                              waveOutGetDevCaps               [MMSYSTEM.402]
3660  */
3661 UINT16 WINAPI waveOutGetDevCaps16(UINT16 uDeviceID, 
3662                                   LPWAVEOUTCAPS16 lpCaps, UINT16 uSize)
3663 {
3664     WAVEOUTCAPSA        wocA;
3665     UINT                ret;
3666
3667     TRACE("(%u %p %u)!\n", uDeviceID, lpCaps, uSize);
3668     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
3669
3670     ret = waveOutGetDevCapsA(uDeviceID, &wocA, sizeof(wocA));
3671
3672     if (ret == MMSYSERR_NOERROR) {
3673         lpCaps->wMid = wocA.wMid;
3674         lpCaps->wPid = wocA.wPid;
3675         lpCaps->vDriverVersion = wocA.vDriverVersion;
3676         strcpy(lpCaps->szPname, wocA.szPname);
3677         lpCaps->dwFormats = wocA.dwFormats;
3678         lpCaps->wChannels = wocA.wChannels;
3679         lpCaps->dwSupport = wocA.dwSupport;
3680     }
3681     return ret;
3682 }
3683
3684 /**************************************************************************
3685  *                              waveOutGetDevCapsA              [WINMM.162]
3686  */
3687 UINT WINAPI waveOutGetDevCapsA(UINT uDeviceID, LPWAVEOUTCAPSA lpCaps,
3688                                UINT uSize)
3689 {
3690     LPWINE_MLD          wmld;
3691
3692     TRACE("(%u %p %u)!\n", uDeviceID, lpCaps, uSize);
3693
3694     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
3695     
3696     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_WAVEOUT, TRUE)) == NULL) 
3697         return MMSYSERR_INVALHANDLE;
3698
3699     return MMDRV_Message(wmld, WODM_GETDEVCAPS, (DWORD)lpCaps, uSize, TRUE);
3700
3701 }
3702
3703 /**************************************************************************
3704  *                              waveOutGetDevCapsW              [WINMM.163]
3705  */
3706 UINT WINAPI waveOutGetDevCapsW(UINT uDeviceID, LPWAVEOUTCAPSW lpCaps,
3707                                UINT uSize)
3708 {
3709     WAVEOUTCAPSA        wocA;
3710     UINT                ret;
3711
3712     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
3713     
3714     ret = waveOutGetDevCapsA(uDeviceID, &wocA, sizeof(wocA));
3715
3716     if (ret == MMSYSERR_NOERROR) {
3717         lpCaps->wMid = wocA.wMid;
3718         lpCaps->wPid = wocA.wPid;
3719         lpCaps->vDriverVersion = wocA.vDriverVersion;
3720         lstrcpyAtoW(lpCaps->szPname, wocA.szPname);
3721         lpCaps->dwFormats = wocA.dwFormats;
3722         lpCaps->wChannels = wocA.wChannels;
3723         lpCaps->dwSupport = wocA.dwSupport;
3724     }
3725     return ret;
3726 }
3727
3728 /**************************************************************************
3729  *                              WAVE_GetErrorText               [internal]
3730  */
3731 static  UINT16  WAVE_GetErrorText(UINT16 uError, LPSTR lpText, UINT16 uSize)
3732 {
3733     UINT16              ret = MMSYSERR_BADERRNUM;
3734
3735     if (lpText == NULL) {
3736         ret = MMSYSERR_INVALPARAM;
3737     } else if (uSize == 0) {
3738         ret = MMSYSERR_NOERROR;
3739     } else if (
3740                /* test has been removed 'coz MMSYSERR_BASE is 0, and gcc did emit
3741                 * a warning for the test was always true */
3742                (/*uError >= MMSYSERR_BASE && */uError <= MMSYSERR_LASTERROR) ||
3743                (uError >= WAVERR_BASE  && uError <= WAVERR_LASTERROR)) {
3744         
3745         if (LoadStringA(MULTIMEDIA_GetIData()->hWinMM32Instance, 
3746                         uError, lpText, uSize) > 0) {
3747             ret = MMSYSERR_NOERROR;
3748         }
3749     }
3750     return ret;
3751 }
3752
3753 /**************************************************************************
3754  *                              waveOutGetErrorText     [MMSYSTEM.403]
3755  */
3756 UINT16 WINAPI waveOutGetErrorText16(UINT16 uError, LPSTR lpText, UINT16 uSize)
3757 {
3758     return WAVE_GetErrorText(uError, lpText, uSize);
3759 }
3760
3761 /**************************************************************************
3762  *                              waveOutGetErrorTextA    [WINMM.164]
3763  */
3764 UINT WINAPI waveOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
3765 {
3766     return WAVE_GetErrorText(uError, lpText, uSize);
3767 }
3768
3769 /**************************************************************************
3770  *                              waveOutGetErrorTextW    [WINMM.165]
3771  */
3772 UINT WINAPI waveOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
3773 {
3774     LPSTR       xstr = HeapAlloc(GetProcessHeap(), 0, uSize);
3775     UINT        ret = WAVE_GetErrorText(uError, xstr, uSize);
3776     
3777     lstrcpyAtoW(lpText, xstr);
3778     HeapFree(GetProcessHeap(), 0, xstr);
3779     return ret;
3780 }
3781
3782 /**************************************************************************
3783  *                      waveOutOpen                     [WINMM.173]
3784  * All the args/structs have the same layout as the win16 equivalents
3785  */
3786 UINT WINAPI waveOutOpen(HWAVEOUT* lphWaveOut, UINT uDeviceID,
3787                         const LPWAVEFORMATEX lpFormat, DWORD dwCallback,
3788                         DWORD dwInstance, DWORD dwFlags)
3789 {
3790     return MMSYSTEM_waveOpen(lphWaveOut, uDeviceID, MMDRV_WAVEOUT, lpFormat, 
3791                              dwCallback, dwInstance, dwFlags, TRUE);
3792 }
3793
3794 /**************************************************************************
3795  *                      waveOutOpen                     [MMSYSTEM.404]
3796  */
3797 UINT16 WINAPI waveOutOpen16(HWAVEOUT16* lphWaveOut, UINT16 uDeviceID,
3798                             const LPWAVEFORMATEX lpFormat, DWORD dwCallback,
3799                             DWORD dwInstance, DWORD dwFlags)
3800 {
3801     HWAVEOUT            hWaveOut;
3802     UINT                ret;
3803
3804     /* since layout of WAVEOFORMATEX is the same for 16/32 bits, we directly
3805      * call the 32 bit version
3806      */
3807     ret = MMSYSTEM_waveOpen(&hWaveOut, uDeviceID, MMDRV_WAVEOUT, lpFormat, 
3808                             dwCallback, dwInstance, dwFlags, FALSE);
3809
3810     if (lphWaveOut != NULL) *lphWaveOut = hWaveOut;
3811     return ret;
3812 }
3813
3814 /**************************************************************************
3815  *                              waveOutClose            [WINMM.161]
3816  */
3817 UINT WINAPI waveOutClose(HWAVEOUT hWaveOut)
3818 {
3819     LPWINE_MLD          wmld;
3820     DWORD               dwRet;
3821     
3822     TRACE("(%04X)\n", hWaveOut);
3823
3824     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) 
3825         return MMSYSERR_INVALHANDLE;
3826
3827     dwRet = MMDRV_Close(wmld, WODM_CLOSE);
3828     MMDRV_Free(hWaveOut, wmld);
3829
3830     return dwRet;
3831 }
3832
3833 /**************************************************************************
3834  *                              waveOutClose            [MMSYSTEM.405]
3835  */
3836 UINT16 WINAPI waveOutClose16(HWAVEOUT16 hWaveOut)
3837 {
3838     return waveOutClose(hWaveOut);
3839 }
3840
3841 /**************************************************************************
3842  *                              waveOutPrepareHeader    [WINMM.175]
3843  */
3844 UINT WINAPI waveOutPrepareHeader(HWAVEOUT hWaveOut,
3845                                  WAVEHDR* lpWaveOutHdr, UINT uSize)
3846 {
3847     LPWINE_MLD          wmld;
3848     
3849     TRACE("(%04X, %p, %u);\n", hWaveOut, lpWaveOutHdr, uSize);
3850
3851     if (lpWaveOutHdr == NULL) return MMSYSERR_INVALPARAM;
3852
3853     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) 
3854         return MMSYSERR_INVALHANDLE;
3855
3856     return MMDRV_Message(wmld, WODM_PREPARE, (DWORD)lpWaveOutHdr, uSize, TRUE);
3857 }
3858
3859 /**************************************************************************
3860  *                              waveOutPrepareHeader    [MMSYSTEM.406]
3861  */
3862 UINT16 WINAPI waveOutPrepareHeader16(HWAVEOUT16 hWaveOut,
3863                                      WAVEHDR* /*SEGPTR*/ lpsegWaveOutHdr, 
3864                                      UINT16 uSize)
3865 {
3866     LPWINE_MLD          wmld;
3867     LPWAVEHDR           lpWaveOutHdr = (LPWAVEHDR)PTR_SEG_TO_LIN(lpsegWaveOutHdr);
3868     
3869     TRACE("(%04X, %p, %u);\n", hWaveOut, lpsegWaveOutHdr, uSize);
3870
3871     if (lpWaveOutHdr == NULL) return MMSYSERR_INVALPARAM;
3872
3873     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) 
3874         return MMSYSERR_INVALHANDLE;
3875
3876     return MMDRV_Message(wmld, WODM_PREPARE, (DWORD)lpsegWaveOutHdr, uSize, FALSE);
3877 }
3878
3879 /**************************************************************************
3880  *                              waveOutUnprepareHeader  [WINMM.181]
3881  */
3882 UINT WINAPI waveOutUnprepareHeader(HWAVEOUT hWaveOut,
3883                                    LPWAVEHDR lpWaveOutHdr, UINT uSize)
3884 {
3885     LPWINE_MLD          wmld;
3886     
3887     TRACE("(%04X, %p, %u);\n", hWaveOut, lpWaveOutHdr, uSize);
3888
3889     if (!(lpWaveOutHdr->dwFlags & WHDR_PREPARED)) {
3890         return MMSYSERR_NOERROR;
3891     }
3892
3893     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) 
3894         return MMSYSERR_INVALHANDLE;
3895
3896     return MMDRV_Message(wmld, WODM_UNPREPARE, (DWORD)lpWaveOutHdr, uSize, TRUE);
3897 }
3898
3899 /**************************************************************************
3900  *                              waveOutUnprepareHeader  [MMSYSTEM.407]
3901  */
3902 UINT16 WINAPI waveOutUnprepareHeader16(HWAVEOUT16 hWaveOut,
3903                                        LPWAVEHDR /*SEGPTR*/ lpsegWaveOutHdr, 
3904                                        UINT16 uSize)
3905 {
3906     LPWINE_MLD          wmld;
3907     LPWAVEHDR           lpWaveOutHdr = (LPWAVEHDR)PTR_SEG_TO_LIN(lpsegWaveOutHdr);
3908     
3909     TRACE("(%04X, %p, %u);\n", hWaveOut, lpsegWaveOutHdr, uSize);
3910
3911     if (!(lpWaveOutHdr->dwFlags & WHDR_PREPARED)) {
3912         return MMSYSERR_NOERROR;
3913     }
3914
3915     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) 
3916         return MMSYSERR_INVALHANDLE;
3917
3918     return MMDRV_Message(wmld, WODM_UNPREPARE, (DWORD)lpsegWaveOutHdr, uSize, FALSE);
3919 }
3920
3921 /**************************************************************************
3922  *                              waveOutWrite            [MMSYSTEM.408]
3923  */
3924 UINT WINAPI waveOutWrite(HWAVEOUT hWaveOut, LPWAVEHDR lpWaveOutHdr,
3925                          UINT uSize)
3926 {
3927     LPWINE_MLD          wmld;
3928     
3929     TRACE("(%04X, %p, %u);\n", hWaveOut, lpWaveOutHdr, uSize);
3930
3931     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) 
3932         return MMSYSERR_INVALHANDLE;
3933
3934     return MMDRV_Message(wmld, WODM_WRITE, (DWORD)lpWaveOutHdr, uSize, TRUE);
3935 }
3936
3937 /**************************************************************************
3938  *                              waveOutWrite            [MMSYSTEM.408]
3939  */
3940 UINT16 WINAPI waveOutWrite16(HWAVEOUT16 hWaveOut, 
3941                              LPWAVEHDR /*SEGPTR*/ lpsegWaveOutHdr,
3942                              UINT16 uSize)
3943 {
3944     LPWINE_MLD          wmld;
3945     
3946     TRACE("(%04X, %p, %u);\n", hWaveOut, lpsegWaveOutHdr, uSize);
3947
3948     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) 
3949         return MMSYSERR_INVALHANDLE;
3950
3951     return MMDRV_Message(wmld, WODM_WRITE, (DWORD)lpsegWaveOutHdr, uSize, FALSE);
3952 }
3953
3954 #define WAVEOUT_SHORTCUT_0(xx, XX)                              \
3955 UINT WINAPI waveOut##xx(HWAVEOUT hWaveOut)                      \
3956 {                                                               \
3957     LPWINE_MLD          wmld;                                   \
3958     TRACE("(%04X);\n", hWaveOut);                               \
3959     if ((wmld = MMDRV_Get(hWaveOut,MMDRV_WAVEOUT,FALSE))==NULL) \
3960         return MMSYSERR_INVALHANDLE;                            \
3961     return MMDRV_Message(wmld,WODM_##XX,0L,0L,TRUE);            \
3962 }                                                               \
3963 UINT16 WINAPI waveOut##xx##16(HWAVEOUT16 hWaveOut16)            \
3964 {return waveOut##xx(hWaveOut16);}
3965
3966 WAVEOUT_SHORTCUT_0(BreakLoop,   BREAKLOOP)
3967 WAVEOUT_SHORTCUT_0(Pause,       PAUSE)
3968 WAVEOUT_SHORTCUT_0(Reset,       RESET)
3969 WAVEOUT_SHORTCUT_0(Restart,     RESTART)
3970 #undef WAVEOUT_SHORTCUT_0
3971
3972 /**************************************************************************
3973  *                              waveOutGetPosition      [WINMM.170]
3974  */
3975 UINT WINAPI waveOutGetPosition(HWAVEOUT hWaveOut, LPMMTIME lpTime,
3976                                UINT uSize)
3977 {
3978     LPWINE_MLD          wmld;
3979
3980     TRACE("(%04X, %p, %u);\n", hWaveOut, lpTime, uSize);
3981
3982     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) 
3983         return MMSYSERR_INVALHANDLE;
3984
3985     return MMDRV_Message(wmld, WODM_GETPOS, (DWORD)lpTime, uSize, TRUE);
3986 }
3987
3988 /**************************************************************************
3989  *                              waveOutGetPosition      [MMSYSTEM.412]
3990  */
3991 UINT16 WINAPI waveOutGetPosition16(HWAVEOUT16 hWaveOut, LPMMTIME16 lpTime,
3992                                    UINT16 uSize)
3993 {
3994     UINT        ret;
3995     MMTIME      mmt;
3996
3997     mmt.wType = lpTime->wType;
3998     ret = waveOutGetPosition(hWaveOut, &mmt, sizeof(mmt));
3999     MMSYSTEM_MMTIME32to16(lpTime, &mmt);
4000     return ret;
4001 }
4002
4003 #define WAVEOUT_SHORTCUT_1(xx, XX, atype)                       \
4004 UINT WINAPI waveOut##xx(HWAVEOUT hWaveOut, atype x)             \
4005 {                                                               \
4006     LPWINE_MLD          wmld;                                   \
4007     TRACE("(%04X, %08lx);\n", hWaveOut, (DWORD)x);              \
4008     if ((wmld = MMDRV_Get(hWaveOut,MMDRV_WAVEOUT,FALSE))==NULL) \
4009         return MMSYSERR_INVALHANDLE;                            \
4010     return MMDRV_Message(wmld,WODM_##XX,(DWORD)x,0L,TRUE);      \
4011 }                                                               \
4012 UINT16 WINAPI waveOut##xx##16(HWAVEOUT16 hWaveOut16, atype x)   \
4013 {return waveOut##xx(hWaveOut16, x);}
4014
4015 WAVEOUT_SHORTCUT_1(GetPitch,        GETPITCH,        LPDWORD)
4016 WAVEOUT_SHORTCUT_1(SetPitch,        SETPITCH,        DWORD)
4017 WAVEOUT_SHORTCUT_1(GetPlaybackRate, GETPLAYBACKRATE, LPDWORD)
4018 WAVEOUT_SHORTCUT_1(SetPlaybackRate, SETPLAYBACKRATE, DWORD)
4019 #undef WAVEOUT_SHORTCUT_1
4020     
4021 #define WAVEOUT_SHORTCUT_2(xx, XX, atype)                       \
4022 UINT WINAPI waveOut##xx(UINT devid, atype x)                    \
4023 {                                                               \
4024     LPWINE_MLD          wmld;                                   \
4025     TRACE("(%04X, %08lx);\n", devid, (DWORD)x);                 \
4026      if ((wmld = MMDRV_Get(devid,MMDRV_WAVEOUT,TRUE))==NULL)    \
4027         return MMSYSERR_INVALHANDLE;                            \
4028     return MMDRV_Message(wmld, WODM_##XX, (DWORD)x, 0L, TRUE);  \
4029 }                                                               \
4030 UINT16 WINAPI waveOut##xx##16(UINT16 devid, atype x)            \
4031 {return waveOut##xx(devid, x);}
4032     
4033 WAVEOUT_SHORTCUT_2(GetVolume, GETVOLUME, LPDWORD)
4034 WAVEOUT_SHORTCUT_2(SetVolume, SETVOLUME, DWORD)
4035 #undef WAVEOUT_SHORTCUT_2
4036     
4037 /**************************************************************************
4038  *                              waveOutGetID            [MMSYSTEM.420]
4039  */
4040 UINT WINAPI waveOutGetID(HWAVEOUT hWaveOut, UINT* lpuDeviceID)
4041 {
4042     LPWINE_MLD          wmld;
4043
4044     TRACE("(%04X, %p);\n", hWaveOut, lpuDeviceID);
4045
4046     if (lpuDeviceID == NULL) return MMSYSERR_INVALHANDLE;
4047
4048     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) 
4049         return MMSYSERR_INVALHANDLE;
4050
4051     *lpuDeviceID = wmld->uDeviceID;
4052     return 0;
4053 }
4054
4055 /**************************************************************************
4056  *                              waveOutGetID            [MMSYSTEM.420]
4057  */
4058 UINT16 WINAPI waveOutGetID16(HWAVEOUT16 hWaveOut, UINT16* lpuDeviceID)
4059 {
4060     LPWINE_MLD          wmld;
4061
4062     TRACE("(%04X, %p);\n", hWaveOut, lpuDeviceID);
4063
4064     if (lpuDeviceID == NULL) return MMSYSERR_INVALHANDLE;
4065
4066     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) 
4067         return MMSYSERR_INVALHANDLE;
4068
4069     *lpuDeviceID = wmld->uDeviceID;
4070     return 0;
4071 }
4072
4073 /**************************************************************************
4074  *                              waveOutMessage          [WINMM.172]
4075  */
4076 DWORD WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage, 
4077                             DWORD dwParam1, DWORD dwParam2)
4078 {
4079     LPWINE_MLD          wmld;
4080     
4081     TRACE("(%04x, %u, %ld, %ld)\n", hWaveOut, uMessage, dwParam1, dwParam2);
4082
4083     /* from M$ KB */
4084     if (uMessage < DRVM_IOCTL || (uMessage >= DRVM_IOCTL_LAST && uMessage < DRVM_MAPPER))
4085         return MMSYSERR_INVALPARAM;
4086
4087     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) 
4088         return MMSYSERR_INVALHANDLE;
4089
4090     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, TRUE);
4091 }
4092
4093 /**************************************************************************
4094  *                              waveOutMessage          [MMSYSTEM.421]
4095  */
4096 DWORD WINAPI waveOutMessage16(HWAVEOUT16 hWaveOut, UINT16 uMessage, 
4097                               DWORD dwParam1, DWORD dwParam2)
4098 {
4099     LPWINE_MLD          wmld;
4100     
4101     TRACE("(%04x, %u, %ld, %ld)\n", hWaveOut, uMessage, dwParam1, dwParam2);
4102
4103     if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, FALSE)) == NULL) {
4104         if ((wmld = MMDRV_Get(hWaveOut, MMDRV_WAVEOUT, TRUE)) != NULL) {
4105             return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
4106         }
4107         return MMSYSERR_INVALHANDLE;
4108     }
4109
4110     /* from M$ KB */
4111     if (uMessage < DRVM_IOCTL || (uMessage >= DRVM_IOCTL_LAST && uMessage < DRVM_MAPPER))
4112         return MMSYSERR_INVALPARAM;
4113
4114     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, FALSE);
4115 }
4116
4117 /**************************************************************************
4118  *                              waveInGetNumDevs                [WINMM.151]
4119  */
4120 UINT WINAPI waveInGetNumDevs(void)
4121 {
4122     return MMDRV_GetNum(MMDRV_WAVEIN);
4123 }
4124
4125 /**************************************************************************
4126  *                              waveInGetNumDevs                [MMSYSTEM.501]
4127  */
4128 UINT16 WINAPI waveInGetNumDevs16(void)
4129 {
4130     return MMDRV_GetNum(MMDRV_WAVEIN);
4131 }
4132
4133 /**************************************************************************
4134  *                              waveInGetDevCapsA               [WINMM.147]
4135  */
4136 UINT WINAPI waveInGetDevCapsW(UINT uDeviceID, LPWAVEINCAPSW lpCaps, UINT uSize)
4137 {
4138     WAVEINCAPSA         wicA;
4139     UINT                ret = waveInGetDevCapsA(uDeviceID, &wicA, uSize);
4140
4141     if (ret == MMSYSERR_NOERROR) {
4142         lpCaps->wMid = wicA.wMid;
4143         lpCaps->wPid = wicA.wPid;
4144         lpCaps->vDriverVersion = wicA.vDriverVersion;
4145         lstrcpyAtoW(lpCaps->szPname, wicA.szPname);
4146         lpCaps->dwFormats = wicA.dwFormats;
4147         lpCaps->wChannels = wicA.wChannels;
4148     }
4149     
4150     return ret;
4151 }
4152
4153 /**************************************************************************
4154  *                              waveInGetDevCapsA               [WINMM.146]
4155  */
4156 UINT WINAPI waveInGetDevCapsA(UINT uDeviceID, LPWAVEINCAPSA lpCaps, UINT uSize)
4157 {
4158     LPWINE_MLD          wmld;
4159
4160     TRACE("(%u %p %u)!\n", uDeviceID, lpCaps, uSize);
4161
4162     if ((wmld = MMDRV_Get(uDeviceID, MMDRV_WAVEIN, TRUE)) == NULL) 
4163         return MMSYSERR_INVALHANDLE;
4164
4165     return MMDRV_Message(wmld, WIDM_GETDEVCAPS, (DWORD)lpCaps, uSize, TRUE);
4166 }
4167
4168 /**************************************************************************
4169  *                              waveInGetDevCaps                [MMSYSTEM.502]
4170  */
4171 UINT16 WINAPI waveInGetDevCaps16(UINT16 uDeviceID, LPWAVEINCAPS16 lpCaps, 
4172                                  UINT16 uSize)
4173 {
4174     WAVEINCAPSA wicA;
4175     UINT        ret = waveInGetDevCapsA(uDeviceID, &wicA, sizeof(wicA));
4176
4177     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
4178
4179     if (ret == MMSYSERR_NOERROR) {
4180         lpCaps->wMid = wicA.wMid;
4181         lpCaps->wPid = wicA.wPid;
4182         lpCaps->vDriverVersion = wicA.vDriverVersion;
4183         strcpy(lpCaps->szPname, wicA.szPname);
4184         lpCaps->dwFormats = wicA.dwFormats;
4185         lpCaps->wChannels = wicA.wChannels;
4186     }
4187     return ret;
4188 }
4189
4190 /**************************************************************************
4191  *                              waveInGetErrorTextA     [WINMM.148]
4192  */
4193 UINT WINAPI waveInGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
4194 {
4195     return WAVE_GetErrorText(uError, lpText, uSize);
4196 }
4197
4198 /**************************************************************************
4199  *                              waveInGetErrorTextW     [WINMM.149]
4200  */
4201 UINT WINAPI waveInGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
4202 {
4203     LPSTR txt = HeapAlloc(GetProcessHeap(), 0, uSize);
4204     UINT        ret = WAVE_GetErrorText(uError, txt, uSize);
4205     
4206     lstrcpyAtoW(lpText, txt);
4207     HeapFree(GetProcessHeap(), 0, txt);
4208     return ret;
4209 }
4210
4211 /**************************************************************************
4212  *                              waveInGetErrorText      [MMSYSTEM.503]
4213  */
4214 UINT16 WINAPI waveInGetErrorText16(UINT16 uError, LPSTR lpText, UINT16 uSize)
4215 {
4216     return WAVE_GetErrorText(uError, lpText, uSize);
4217 }
4218
4219 /**************************************************************************
4220  *                              waveInOpen                      [WINMM.154]
4221  */
4222 UINT WINAPI waveInOpen(HWAVEIN* lphWaveIn, UINT uDeviceID,
4223                        const LPWAVEFORMATEX lpFormat, DWORD dwCallback,
4224                        DWORD dwInstance, DWORD dwFlags)
4225 {
4226     return MMSYSTEM_waveOpen(lphWaveIn, uDeviceID, MMDRV_WAVEIN, lpFormat, 
4227                              dwCallback, dwInstance, dwFlags, TRUE);
4228 }
4229
4230 /**************************************************************************
4231  *                              waveInOpen                      [MMSYSTEM.504]
4232  */
4233 UINT16 WINAPI waveInOpen16(HWAVEIN16* lphWaveIn, UINT16 uDeviceID,
4234                            const LPWAVEFORMATEX lpFormat, DWORD dwCallback,
4235                            DWORD dwInstance, DWORD dwFlags)
4236 {
4237     HWAVEIN             hWaveIn;
4238     UINT                ret;
4239
4240     /* since layout of WAVEOFORMATEX is the same for 16/32 bits, we directly
4241      * call the 32 bit version
4242      */
4243     ret = MMSYSTEM_waveOpen(&hWaveIn, uDeviceID, MMDRV_WAVEIN, lpFormat, 
4244                             dwCallback, dwInstance, dwFlags, FALSE);
4245
4246     if (lphWaveIn != NULL) *lphWaveIn = hWaveIn;
4247     return ret;
4248 }
4249
4250 /**************************************************************************
4251  *                              waveInClose                     [WINMM.145]
4252  */
4253 UINT WINAPI waveInClose(HWAVEIN hWaveIn)
4254 {
4255     LPWINE_MLD          wmld;
4256     DWORD               dwRet;
4257     
4258     TRACE("(%04X)\n", hWaveIn);
4259
4260     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4261         return MMSYSERR_INVALHANDLE;
4262
4263     dwRet = MMDRV_Message(wmld, WIDM_CLOSE, 0L, 0L, TRUE);
4264     MMDRV_Free(hWaveIn, wmld);
4265     return dwRet;
4266 }
4267
4268 /**************************************************************************
4269  *                              waveInClose                     [MMSYSTEM.505]
4270  */
4271 UINT16 WINAPI waveInClose16(HWAVEIN16 hWaveIn)
4272 {
4273     return waveOutClose(hWaveIn);
4274 }
4275
4276 /**************************************************************************
4277  *                              waveInPrepareHeader             [WINMM.155]
4278  */
4279 UINT WINAPI waveInPrepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr, 
4280                                 UINT uSize)
4281 {
4282     LPWINE_MLD          wmld;
4283     
4284     TRACE("(%04X, %p, %u);\n", hWaveIn, lpWaveInHdr, uSize);
4285
4286     if (lpWaveInHdr == NULL) return MMSYSERR_INVALPARAM;
4287     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4288         return MMSYSERR_INVALHANDLE;
4289
4290     lpWaveInHdr->dwBytesRecorded = 0;
4291
4292     return MMDRV_Message(wmld, WIDM_PREPARE, (DWORD)lpWaveInHdr, uSize, TRUE);
4293 }
4294
4295 /**************************************************************************
4296  *                              waveInPrepareHeader             [MMSYSTEM.506]
4297  */
4298 UINT16 WINAPI waveInPrepareHeader16(HWAVEIN16 hWaveIn,
4299                                     WAVEHDR* /* SEGPTR */ lpsegWaveInHdr, 
4300                                     UINT16 uSize)
4301 {
4302     LPWINE_MLD          wmld;
4303     LPWAVEHDR           lpWaveInHdr = (LPWAVEHDR)PTR_SEG_TO_LIN(lpsegWaveInHdr);
4304     UINT16              ret;
4305     
4306     TRACE("(%04X, %p, %u);\n", hWaveIn, lpWaveInHdr, uSize);
4307
4308     if (lpWaveInHdr == NULL) return MMSYSERR_INVALHANDLE;
4309     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4310         return MMSYSERR_INVALHANDLE;
4311
4312     lpWaveInHdr->dwBytesRecorded = 0;
4313
4314     ret = MMDRV_Message(wmld, WIDM_PREPARE, (DWORD)lpsegWaveInHdr, uSize, FALSE);
4315     return ret;
4316 }
4317
4318 /**************************************************************************
4319  *                              waveInUnprepareHeader   [WINMM.159]
4320  */
4321 UINT WINAPI waveInUnprepareHeader(HWAVEIN hWaveIn, WAVEHDR* lpWaveInHdr, 
4322                                   UINT uSize)
4323 {
4324     LPWINE_MLD          wmld;
4325     
4326     TRACE("(%04X, %p, %u);\n", hWaveIn, lpWaveInHdr, uSize);
4327
4328     if (lpWaveInHdr == NULL) return MMSYSERR_INVALPARAM;
4329     if (!(lpWaveInHdr->dwFlags & WHDR_PREPARED)) {
4330         return MMSYSERR_NOERROR;
4331     }
4332
4333     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4334         return MMSYSERR_INVALHANDLE;
4335
4336     return MMDRV_Message(wmld, WIDM_UNPREPARE, (DWORD)lpWaveInHdr, uSize, TRUE);
4337 }
4338
4339 /**************************************************************************
4340  *                              waveInUnprepareHeader   [MMSYSTEM.507]
4341  */
4342 UINT16 WINAPI waveInUnprepareHeader16(HWAVEIN16 hWaveIn, 
4343                                       WAVEHDR* /* SEGPTR */ lpsegWaveInHdr, 
4344                                       UINT16 uSize)
4345 {
4346     LPWINE_MLD          wmld;
4347     LPWAVEHDR           lpWaveInHdr = (LPWAVEHDR)PTR_SEG_TO_LIN(lpsegWaveInHdr);
4348     
4349     TRACE("(%04X, %p, %u);\n", hWaveIn, lpsegWaveInHdr, uSize);
4350
4351     if (lpWaveInHdr == NULL) return MMSYSERR_INVALPARAM;
4352
4353     if (!(lpWaveInHdr->dwFlags & WHDR_PREPARED)) {
4354         return MMSYSERR_NOERROR;
4355     }
4356
4357     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4358         return MMSYSERR_INVALHANDLE;
4359
4360     return MMDRV_Message(wmld, WIDM_UNPREPARE, (DWORD)lpsegWaveInHdr, uSize, FALSE);
4361 }
4362
4363 /**************************************************************************
4364  *                              waveInAddBuffer         [WINMM.144]
4365  */
4366 UINT WINAPI waveInAddBuffer(HWAVEIN hWaveIn,
4367                             WAVEHDR* lpWaveInHdr, UINT uSize)
4368 {
4369     LPWINE_MLD          wmld;
4370     
4371     TRACE("(%04X, %p, %u);\n", hWaveIn, lpWaveInHdr, uSize);
4372
4373     if (lpWaveInHdr == NULL) return MMSYSERR_INVALPARAM;
4374     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4375         return MMSYSERR_INVALHANDLE;
4376
4377     return MMDRV_Message(wmld, WIDM_ADDBUFFER, (DWORD)lpWaveInHdr, uSize, TRUE);
4378 }
4379
4380 /**************************************************************************
4381  *                              waveInAddBuffer         [MMSYSTEM.508]
4382  */
4383 UINT16 WINAPI waveInAddBuffer16(HWAVEIN16 hWaveIn, 
4384                                 WAVEHDR* /* SEGPTR */ lpsegWaveInHdr, 
4385                                 UINT16 uSize)
4386 {
4387     LPWINE_MLD          wmld;
4388     
4389     TRACE("(%04X, %p, %u);\n", hWaveIn, lpsegWaveInHdr, uSize);
4390
4391     if (lpsegWaveInHdr == NULL) return MMSYSERR_INVALPARAM;
4392     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4393         return MMSYSERR_INVALHANDLE;
4394
4395     return MMDRV_Message(wmld, WIDM_ADDBUFFER, (DWORD)lpsegWaveInHdr, uSize, FALSE);
4396 }
4397
4398 #define WAVEIN_SHORTCUT_0(xx, XX)                               \
4399 UINT WINAPI waveIn##xx(HWAVEIN hWaveIn)                         \
4400 {                                                               \
4401     LPWINE_MLD          wmld;                                   \
4402     TRACE("(%04X);\n", hWaveIn);                                \
4403     if ((wmld=MMDRV_Get(hWaveIn,MMDRV_WAVEIN,FALSE)) == NULL)   \
4404         return MMSYSERR_INVALHANDLE;                            \
4405     return MMDRV_Message(wmld,WIDM_##XX,0L,0L,TRUE);            \
4406 }                                                               \
4407 UINT16 WINAPI waveIn##xx##16(HWAVEIN16 hWaveIn16)               \
4408 {return waveIn##xx(hWaveIn16);}
4409
4410 WAVEIN_SHORTCUT_0(Reset,        RESET)
4411 WAVEIN_SHORTCUT_0(Start,        START)
4412 WAVEIN_SHORTCUT_0(Stop,         STOP)
4413 #undef WAVEIN_SHORTCUT_0
4414
4415 /**************************************************************************
4416  *                              waveInGetPosition       [WINMM.152]
4417  */
4418 UINT WINAPI waveInGetPosition(HWAVEIN hWaveIn, LPMMTIME lpTime,
4419                               UINT uSize)
4420 {
4421     LPWINE_MLD          wmld;
4422
4423     TRACE("(%04X, %p, %u);\n", hWaveIn, lpTime, uSize);
4424
4425     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4426         return MMSYSERR_INVALHANDLE;
4427
4428     return MMDRV_Message(wmld, WIDM_GETPOS, (DWORD)lpTime, uSize, TRUE);
4429 }
4430
4431 /**************************************************************************
4432  *                              waveInGetPosition       [MMSYSTEM.512]
4433  */
4434 UINT16 WINAPI waveInGetPosition16(HWAVEIN16 hWaveIn, LPMMTIME16 lpTime,
4435                                   UINT16 uSize)
4436 {
4437     UINT        ret;
4438     MMTIME      mmt;
4439
4440     mmt.wType = lpTime->wType;
4441     ret = waveInGetPosition(hWaveIn, &mmt, sizeof(mmt));
4442     MMSYSTEM_MMTIME32to16(lpTime, &mmt);
4443     return ret;
4444 }
4445
4446 /**************************************************************************
4447  *                              waveInGetID                     [WINMM.150]
4448  */
4449 UINT WINAPI waveInGetID(HWAVEIN hWaveIn, UINT* lpuDeviceID)
4450 {
4451     LPWINE_MLD          wmld;
4452
4453     TRACE("(%04X, %p);\n", hWaveIn, lpuDeviceID);
4454
4455     if (lpuDeviceID == NULL) return MMSYSERR_INVALHANDLE;
4456
4457     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4458         return MMSYSERR_INVALHANDLE;
4459
4460     *lpuDeviceID = wmld->uDeviceID;
4461     return MMSYSERR_NOERROR;
4462 }
4463
4464 /**************************************************************************
4465  *                              waveInGetID                     [MMSYSTEM.513]
4466  */
4467 UINT16 WINAPI waveInGetID16(HWAVEIN16 hWaveIn, UINT16* lpuDeviceID)
4468 {
4469     LPWINE_MLD          wmld;
4470
4471     TRACE("(%04X, %p);\n", hWaveIn, lpuDeviceID);
4472
4473     if (lpuDeviceID == NULL) return MMSYSERR_INVALHANDLE;
4474
4475     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4476         return MMSYSERR_INVALHANDLE;
4477
4478     *lpuDeviceID = wmld->uDeviceID;
4479     return MMSYSERR_NOERROR;
4480 }
4481
4482 /**************************************************************************
4483  *                              waveInMessage           [WINMM.153]
4484  */
4485 DWORD WINAPI waveInMessage(HWAVEIN hWaveIn, UINT uMessage,
4486                            DWORD dwParam1, DWORD dwParam2)
4487 {
4488     LPWINE_MLD          wmld;
4489     
4490     TRACE("(%04x, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
4491
4492     /* from M$ KB */
4493     if (uMessage < DRVM_IOCTL || (uMessage >= DRVM_IOCTL_LAST && uMessage < DRVM_MAPPER))
4494         return MMSYSERR_INVALPARAM;
4495
4496     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4497         return MMSYSERR_INVALHANDLE;
4498
4499     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, TRUE);
4500 }
4501
4502 /**************************************************************************
4503  *                              waveInMessage           [MMSYSTEM.514]
4504  */
4505 DWORD WINAPI waveInMessage16(HWAVEIN16 hWaveIn, UINT16 uMessage,
4506                              DWORD dwParam1, DWORD dwParam2)
4507 {
4508     LPWINE_MLD          wmld;
4509
4510     TRACE("(%04x, %u, %ld, %ld)\n", hWaveIn, uMessage, dwParam1, dwParam2);
4511
4512     /* from M$ KB */
4513     if (uMessage < DRVM_IOCTL || (uMessage >= DRVM_IOCTL_LAST && uMessage < DRVM_MAPPER))
4514         return MMSYSERR_INVALPARAM;
4515
4516     if ((wmld = MMDRV_Get(hWaveIn, MMDRV_WAVEIN, FALSE)) == NULL) 
4517         return MMSYSERR_INVALHANDLE;
4518
4519     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, TRUE);
4520 }
4521
4522 /**************************************************************************
4523  *                              DrvOpen                 [MMSYSTEM.1100]
4524  */
4525 HDRVR16 WINAPI DrvOpen(LPSTR lpDriverName, LPSTR lpSectionName, LPARAM lParam)
4526 {
4527     TRACE("('%s','%s', %08lX);\n", lpDriverName, lpSectionName, lParam);
4528
4529     return OpenDriver16(lpDriverName, lpSectionName, lParam);
4530 }
4531
4532 /**************************************************************************
4533  *                              DrvClose                [MMSYSTEM.1101]
4534  */
4535 LRESULT WINAPI DrvClose(HDRVR16 hDrv, LPARAM lParam1, LPARAM lParam2)
4536 {
4537     TRACE("(%04X, %08lX, %08lX);\n", hDrv, lParam1, lParam2);
4538
4539     return CloseDriver16(hDrv, lParam1, lParam2);
4540 }
4541
4542 /**************************************************************************
4543  *                              DrvSendMessage          [MMSYSTEM.1102]
4544  */
4545 LRESULT WINAPI DrvSendMessage(HDRVR16 hDrv, WORD msg, LPARAM lParam1,
4546                               LPARAM lParam2)
4547 {
4548     return SendDriverMessage(hDrv, msg, lParam1, lParam2);
4549 }
4550
4551 /**************************************************************************
4552  *                              DrvGetModuleHandle      [MMSYSTEM.1103]
4553  */
4554 HANDLE16 WINAPI DrvGetModuleHandle16(HDRVR16 hDrv)
4555 {
4556     return GetDriverModuleHandle16(hDrv);
4557 }
4558
4559 /**************************************************************************
4560  *                              DrvDefDriverProc        [MMSYSTEM.1104]
4561  */
4562 LRESULT WINAPI DrvDefDriverProc(DWORD dwDriverID, HDRVR16 hDrv, WORD wMsg, 
4563                                 DWORD dwParam1, DWORD dwParam2)
4564 {
4565     /* FIXME : any mapping from 32 to 16 bit structure ? */
4566     return DefDriverProc16(dwDriverID, hDrv, wMsg, dwParam1, dwParam2);
4567 }
4568
4569 /**************************************************************************
4570  *                              DefDriverProc                     [WINMM.5]
4571  */
4572 LRESULT WINAPI DefDriverProc(DWORD dwDriverIdentifier, HDRVR hDrv,
4573                              UINT Msg, LPARAM lParam1, LPARAM lParam2)
4574 {
4575     switch (Msg) {
4576     case DRV_LOAD:
4577     case DRV_FREE:
4578     case DRV_ENABLE:
4579     case DRV_DISABLE:
4580         return 1;
4581     case DRV_INSTALL:
4582     case DRV_REMOVE:
4583         return DRV_SUCCESS;
4584     default:
4585         return 0;
4586     }
4587 }
4588
4589 /*#define USE_MM_TSK_WINE*/
4590
4591 /**************************************************************************
4592  *                              mmTaskCreate            [MMSYSTEM.900]
4593  *
4594  * Creates a 16 bit MM task. It's entry point is lpFunc, and it should be
4595  * called upon creation with dwPmt as parameter.
4596  */
4597 HINSTANCE16 WINAPI mmTaskCreate16(SEGPTR spProc, HINSTANCE16 *lphMmTask, DWORD dwPmt)
4598 {
4599     DWORD               showCmd = 0x40002;
4600     LPSTR               cmdline;
4601     WORD                sel1, sel2;
4602     LOADPARAMS16*       lp;
4603     HINSTANCE16         ret;
4604     HINSTANCE16         handle;
4605     
4606     TRACE("(%08lx, %p, %08lx);\n", spProc, lphMmTask, dwPmt);
4607     /* This to work requires NE modules to be started with a binary command line
4608      * which is not currently the case. A patch exists but has never been committed.
4609      * A workaround would be to integrate code for mmtask.tsk into Wine, but
4610      * this requires tremendous work (starting with patching tools/build to
4611      * create NE executables (and not only DLLs) for builtins modules.
4612      * EP 99/04/25
4613      */
4614     FIXME("This is currently broken. It will fail\n");
4615
4616     cmdline = (LPSTR)HeapAlloc(GetProcessHeap(), 0, 0x0d);
4617     cmdline[0] = 0x0d;
4618     *(LPDWORD)(cmdline + 1) = (DWORD)spProc;
4619     *(LPDWORD)(cmdline + 5) = dwPmt;
4620     *(LPDWORD)(cmdline + 9) = 0;
4621
4622     sel1 = SELECTOR_AllocBlock(cmdline, 0x0d, SEGMENT_DATA, FALSE, FALSE);
4623     sel2 = SELECTOR_AllocBlock(&showCmd, sizeof(showCmd),
4624                                SEGMENT_DATA, FALSE, FALSE);
4625     
4626     lp = (LOADPARAMS16*)HeapAlloc(GetProcessHeap(), 0, sizeof(LOADPARAMS16));
4627     lp->hEnvironment = 0;
4628     lp->cmdLine = PTR_SEG_OFF_TO_SEGPTR(sel1, 0);
4629     lp->showCmd = PTR_SEG_OFF_TO_SEGPTR(sel2, 0);
4630     lp->reserved = 0;
4631     
4632 #ifndef USE_MM_TSK_WINE
4633     handle = LoadModule16("c:\\windows\\system\\mmtask.tsk", lp);
4634 #else
4635     handle = LoadModule16("mmtask.tsk", lp);
4636 #endif
4637     if (handle < 32) {
4638         ret = (handle) ? 1 : 2;
4639         handle = 0;
4640     } else {
4641         ret = 0;
4642     }
4643     if (lphMmTask)
4644         *lphMmTask = handle;
4645     
4646     UnMapLS(PTR_SEG_OFF_TO_SEGPTR(sel2, 0));
4647     UnMapLS(PTR_SEG_OFF_TO_SEGPTR(sel1, 0));
4648     
4649     HeapFree(GetProcessHeap(), 0, lp);
4650     HeapFree(GetProcessHeap(), 0, cmdline);
4651
4652     TRACE("=> 0x%04x/%d\n", handle, ret);
4653     return ret;
4654 }
4655
4656 #ifdef USE_MM_TSK_WINE
4657 /* C equivalent to mmtask.tsk binary content */
4658 void    mmTaskEntryPoint16(LPSTR cmdLine, WORD di, WORD si)
4659 {
4660     int len = cmdLine[0x80];
4661
4662     if (len / 2 == 6) {
4663         void    (*fpProc)(DWORD) = (void (*)(DWORD))PTR_SEG_TO_LIN(*((DWORD*)(cmdLine + 1)));
4664         DWORD   dwPmt  = *((DWORD*)(cmdLine + 5));
4665
4666 #if 0
4667         InitTask16(); /* fixme: pmts / from context ? */
4668         InitApp(di);
4669 #endif
4670         if (SetMessageQueue16(0x40)) {
4671             WaitEvent16(0);
4672             if (HIWORD(fpProc)) {
4673                 OldYield16();
4674 /* EPP          StackEnter16(); */
4675                 (fpProc)(dwPmt);
4676             }
4677         }
4678     }
4679     OldYield16();
4680     OldYield16();
4681     OldYield16();
4682     ExitProcess(0);
4683 }
4684 #endif
4685
4686 /**************************************************************************
4687  *                              mmTaskBlock             [MMSYSTEM.902]
4688  */
4689 void    WINAPI  mmTaskBlock16(HINSTANCE16 WINE_UNUSED hInst)
4690 {
4691     MSG         msg;
4692
4693     do {
4694         GetMessageA(&msg, 0, 0, 0);
4695         if (msg.hwnd) {
4696             TranslateMessage(&msg);
4697             DispatchMessageA(&msg);
4698         }
4699     } while (msg.message < 0x3A0);
4700 }
4701
4702 /**************************************************************************
4703  *                              mmTaskSignal            [MMSYSTEM.903]
4704  */
4705 LRESULT WINAPI mmTaskSignal16(HTASK16 ht) 
4706 {
4707     TRACE("(%04x);\n", ht);
4708     return Callout.PostAppMessage16(ht, WM_USER, 0, 0);
4709 }
4710
4711 /**************************************************************************
4712  *                              mmTaskYield16           [MMSYSTEM.905]
4713  */
4714 void    WINAPI  mmTaskYield16(void)
4715 {
4716     MSG         msg;
4717
4718     if (PeekMessageA(&msg, 0, 0, 0, 0)) {
4719         Yield16();
4720     }
4721 }
4722
4723 DWORD   WINAPI  GetProcessFlags(DWORD);
4724
4725 /**************************************************************************
4726  *                              mmThreadCreate          [MMSYSTEM.1120]
4727  *
4728  * undocumented
4729  * Creates a MM thread, calling fpThreadAddr(dwPmt). 
4730  * dwFlags: 
4731  *      bit.0 set means create a 16 bit task instead of thread calling a 16 bit proc
4732  *      bit.1 set means to open a VxD for this thread (unsupported) 
4733  */
4734 LRESULT WINAPI mmThreadCreate16(FARPROC16 fpThreadAddr, LPHANDLE lpHndl, DWORD dwPmt, DWORD dwFlags) 
4735 {
4736     HANDLE16            hndl;
4737     LRESULT             ret;
4738
4739     TRACE("(%p, %p, %08lx, %08lx)!\n", fpThreadAddr, lpHndl, dwPmt, dwFlags);
4740
4741     hndl = GlobalAlloc16(sizeof(WINE_MMTHREAD), GMEM_SHARE|GMEM_ZEROINIT);
4742
4743     if (hndl == 0) {
4744         ret = 2;
4745     } else {
4746         WINE_MMTHREAD*  lpMMThd = (WINE_MMTHREAD*)PTR_SEG_OFF_TO_LIN(hndl, 0);
4747
4748 #if 0
4749         /* force mmtask routines even if mmthread is required */
4750         /* this will work only if the patch about binary cmd line and NE tasks 
4751          * is committed
4752          */
4753         dwFlags |= 1;
4754 #endif
4755
4756         lpMMThd->dwSignature    = WINE_MMTHREAD_CREATED;
4757         lpMMThd->dwCounter      = 0;
4758         lpMMThd->hThread        = 0;
4759         lpMMThd->dwThreadID     = 0;
4760         lpMMThd->fpThread       = fpThreadAddr;
4761         lpMMThd->dwThreadPmt    = dwPmt;
4762         lpMMThd->dwSignalCount  = 0;
4763         lpMMThd->hEvent         = 0;
4764         lpMMThd->hVxD           = 0;
4765         lpMMThd->dwStatus       = 0;
4766         lpMMThd->dwFlags        = dwFlags;
4767         lpMMThd->hTask          = 0;
4768         
4769         if ((dwFlags & 1) == 0 && (GetProcessFlags(GetCurrentThreadId()) & 8) == 0) {
4770             lpMMThd->hEvent = CreateEventA(0, 0, 1, 0);
4771
4772             TRACE("Let's go crazy... trying new MM thread. lpMMThd=%p\n", lpMMThd);
4773             if (lpMMThd->dwFlags & 2) {
4774                 /* as long as we don't support MM VxD in wine, we don't need 
4775                  * to care about this flag
4776                  */
4777                 /* FIXME("Don't know how to properly open VxD handles\n"); */
4778                 /* lpMMThd->hVxD = OpenVxDHandle(lpMMThd->hEvent); */
4779             }
4780
4781             lpMMThd->hThread = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)WINE_mmThreadEntryPoint, 
4782                                             (LPVOID)(DWORD)hndl, CREATE_SUSPENDED, &lpMMThd->dwThreadID);
4783             if (lpMMThd->hThread == 0) {
4784                 WARN("Couldn't create thread\n");
4785                 /* clean-up(VxDhandle...); devicedirectio... */
4786                 if (lpMMThd->hEvent != 0)
4787                     CloseHandle(lpMMThd->hEvent);
4788                 ret = 2;
4789             } else {
4790                 TRACE("Got a nice thread hndl=0x%04x id=0x%08lx\n", lpMMThd->hThread, lpMMThd->dwThreadID);
4791                 ret = 0;
4792             }
4793         } else {
4794             /* get WINE_mmThreadEntryPoint() 
4795              * 2047 is its ordinal in mmsystem.spec
4796              */
4797             FARPROC16   fp = GetProcAddress16(GetModuleHandle16("MMSYSTEM"), (SEGPTR)2047);
4798
4799             TRACE("farproc seg=0x%08lx lin=%p\n", (DWORD)fp, PTR_SEG_TO_LIN(fp));
4800
4801             ret = (fp == 0) ? 2 : mmTaskCreate16((DWORD)fp, 0, hndl);
4802         }
4803
4804         if (ret == 0) {
4805             if (lpMMThd->hThread && !ResumeThread(lpMMThd->hThread))
4806                 WARN("Couldn't resume thread\n");
4807
4808             while (lpMMThd->dwStatus != 0x10) { /* test also HIWORD of dwStatus */
4809                 UserYield16();
4810             }
4811         }
4812     }
4813
4814     if (ret != 0) {
4815         GlobalFree16(hndl);
4816         hndl = 0;
4817     }
4818
4819     if (lpHndl)
4820         *lpHndl = hndl;
4821
4822     TRACE("ok => %ld\n", ret);
4823     return ret;
4824 }
4825
4826 /**************************************************************************
4827  *                              mmThreadSignal          [MMSYSTEM.1121]
4828  */
4829 void WINAPI mmThreadSignal16(HANDLE16 hndl) 
4830 {
4831     TRACE("(%04x)!\n", hndl);
4832
4833     if (hndl) {
4834         WINE_MMTHREAD*  lpMMThd = (WINE_MMTHREAD*)PTR_SEG_OFF_TO_LIN(hndl, 0);
4835
4836         lpMMThd->dwCounter++;
4837         if (lpMMThd->hThread != 0) {
4838             InterlockedIncrement(&lpMMThd->dwSignalCount);
4839             SetEvent(lpMMThd->hEvent);
4840         } else {
4841             mmTaskSignal16(lpMMThd->hTask);
4842         }
4843         lpMMThd->dwCounter--;
4844     }
4845 }
4846
4847 /**************************************************************************
4848  *                              MMSYSTEM_ThreadBlock            [internal]
4849  */
4850 static  void    MMSYSTEM_ThreadBlock(WINE_MMTHREAD* lpMMThd)
4851 {
4852     MSG         msg;
4853     DWORD       ret;
4854
4855     if (lpMMThd->dwThreadID != GetCurrentThreadId())
4856         ERR("Not called by thread itself\n");
4857
4858     for (;;) {
4859         ResetEvent(lpMMThd->hEvent);
4860         if (InterlockedDecrement(&lpMMThd->dwSignalCount) >= 0)
4861             break;
4862         InterlockedIncrement(&lpMMThd->dwSignalCount);
4863         
4864         TRACE("S1\n");
4865         
4866         ret = MsgWaitForMultipleObjects(1, &lpMMThd->hEvent, FALSE, INFINITE, QS_ALLINPUT);
4867         switch (ret) {
4868         case WAIT_OBJECT_0:     /* Event */
4869             TRACE("S2.1\n");
4870             break;
4871         case WAIT_OBJECT_0 + 1: /* Msg */
4872             TRACE("S2.2\n");
4873             if (Callout.PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
4874                 Callout.TranslateMessage(&msg);
4875                 Callout.DispatchMessageA(&msg);
4876             }
4877             break;
4878         default:
4879             WARN("S2.x unsupported ret val 0x%08lx\n", ret);
4880         }
4881         TRACE("S3\n");
4882     }
4883 }
4884
4885 /**************************************************************************
4886  *                              mmThreadBlock           [MMSYSTEM.1122]
4887  */
4888 void    WINAPI mmThreadBlock16(HANDLE16 hndl) 
4889 {
4890     TRACE("(%04x)!\n", hndl);
4891
4892     if (hndl) {
4893         WINE_MMTHREAD*  lpMMThd = (WINE_MMTHREAD*)PTR_SEG_OFF_TO_LIN(hndl, 0);
4894         
4895         if (lpMMThd->hThread != 0) {
4896             SYSLEVEL_ReleaseWin16Lock();
4897             MMSYSTEM_ThreadBlock(lpMMThd);
4898             SYSLEVEL_RestoreWin16Lock();
4899         } else {
4900             mmTaskBlock16(lpMMThd->hTask);
4901         }
4902     }
4903     TRACE("done\n");
4904 }
4905
4906 /**************************************************************************
4907  *                              mmThreadIsCurrent       [MMSYSTEM.1123]
4908  */
4909 BOOL16  WINAPI mmThreadIsCurrent16(HANDLE16 hndl) 
4910 {
4911     BOOL16              ret = FALSE;
4912
4913     TRACE("(%04x)!\n", hndl);
4914
4915     if (hndl && mmThreadIsValid16(hndl)) {
4916         WINE_MMTHREAD*  lpMMThd = (WINE_MMTHREAD*)PTR_SEG_OFF_TO_LIN(hndl, 0);
4917         ret = (GetCurrentThreadId() == lpMMThd->dwThreadID);
4918     }
4919     TRACE("=> %d\n", ret);
4920     return ret;
4921 }
4922
4923 /**************************************************************************
4924  *                              mmThreadIsValid         [MMSYSTEM.1124]
4925  */
4926 BOOL16  WINAPI  mmThreadIsValid16(HANDLE16 hndl)
4927 {
4928     BOOL16              ret = FALSE;
4929
4930     TRACE("(%04x)!\n", hndl);
4931
4932     if (hndl) {
4933         WINE_MMTHREAD*  lpMMThd = (WINE_MMTHREAD*)PTR_SEG_OFF_TO_LIN(hndl, 0);
4934
4935         if (!IsBadWritePtr(lpMMThd, sizeof(WINE_MMTHREAD)) &&
4936             lpMMThd->dwSignature == WINE_MMTHREAD_CREATED &&
4937             IsTask16(lpMMThd->hTask)) {
4938             lpMMThd->dwCounter++;
4939             if (lpMMThd->hThread != 0) {
4940                 DWORD   dwThreadRet;
4941                 if (GetExitCodeThread(lpMMThd->hThread, &dwThreadRet) &&
4942                     dwThreadRet == STATUS_PENDING) {
4943                     ret = TRUE;
4944                 }
4945             } else {
4946                 ret = TRUE;
4947             }
4948             lpMMThd->dwCounter--;
4949         }
4950     }
4951     TRACE("=> %d\n", ret);
4952     return ret;
4953 }
4954
4955 /**************************************************************************
4956  *                              mmThreadGetTask         [MMSYSTEM.1125]
4957  */
4958 HANDLE16 WINAPI mmThreadGetTask16(HANDLE16 hndl) 
4959 {
4960     HANDLE16    ret = 0;
4961
4962     TRACE("(%04x)\n", hndl);
4963
4964     if (mmThreadIsValid16(hndl)) {
4965         WINE_MMTHREAD*  lpMMThd = (WINE_MMTHREAD*)PTR_SEG_OFF_TO_LIN(hndl, 0);
4966         ret = lpMMThd->hTask;
4967     }
4968     return ret;
4969 }
4970
4971 /* ### start build ### */
4972 extern LONG CALLBACK MMSYSTEM_CallTo16_long_l    (FARPROC16,LONG);
4973 /* ### stop build ### */
4974
4975 /**************************************************************************
4976  *                              mmThreadGetTask                 [internal]
4977  */
4978 void WINAPI WINE_mmThreadEntryPoint(DWORD _pmt)
4979 {
4980     HANDLE16            hndl = (HANDLE16)_pmt;
4981     WINE_MMTHREAD*      lpMMThd = (WINE_MMTHREAD*)PTR_SEG_OFF_TO_LIN(hndl, 0);
4982
4983     TRACE("(%04x %p)\n", hndl, lpMMThd);
4984
4985     lpMMThd->hTask = LOWORD(GetCurrentTask());
4986     TRACE("[10-%08x] setting hTask to 0x%08x\n", lpMMThd->hThread, lpMMThd->hTask);
4987     lpMMThd->dwStatus = 0x10;
4988     MMSYSTEM_ThreadBlock(lpMMThd);
4989     TRACE("[20-%08x]\n", lpMMThd->hThread);
4990     lpMMThd->dwStatus = 0x20;
4991     if (lpMMThd->fpThread) {
4992         MMSYSTEM_CallTo16_long_l(lpMMThd->fpThread, lpMMThd->dwThreadPmt);
4993     }
4994     lpMMThd->dwStatus = 0x30;
4995     TRACE("[30-%08x]\n", lpMMThd->hThread);
4996     while (lpMMThd->dwCounter) {
4997         Sleep(1);
4998         /* Yield16();*/
4999     }
5000     TRACE("[XX-%08x]\n", lpMMThd->hThread);
5001     /* paranoia */
5002     lpMMThd->dwSignature = WINE_MMTHREAD_DELETED;
5003     /* close lpMMThread->hVxD directio */
5004     if (lpMMThd->hEvent)
5005         CloseHandle(lpMMThd->hEvent);
5006     GlobalFree16(hndl);
5007     TRACE("done\n");
5008 }
5009
5010 typedef BOOL16 (WINAPI *MMCPLCALLBACK)(HWND, LPCSTR, LPCSTR, LPCSTR);
5011
5012 /**************************************************************************
5013  *                      mmShowMMCPLPropertySheet        [MMSYSTEM.1150]
5014  */
5015 BOOL16  WINAPI  mmShowMMCPLPropertySheet16(HWND hWnd, LPCSTR lpStrDevice, 
5016                                            LPCSTR lpStrTab, LPCSTR lpStrTitle)
5017 {
5018     HANDLE      hndl;
5019     BOOL16      ret = FALSE;
5020
5021     TRACE("(%04x \"%s\" \"%s\" \"%s\")\n", hWnd, lpStrDevice, lpStrTab, lpStrTitle);
5022
5023     hndl = LoadLibraryA("MMSYS.CPL");
5024     if (hndl != 0) {
5025         MMCPLCALLBACK   fp = (MMCPLCALLBACK)GetProcAddress(hndl, "ShowMMCPLPropertySheet");
5026         if (fp != NULL) {
5027             SYSLEVEL_ReleaseWin16Lock();
5028             ret = (fp)(hWnd, lpStrDevice, lpStrTab, lpStrTitle);
5029             SYSLEVEL_RestoreWin16Lock();
5030         }
5031         FreeLibrary(hndl);
5032     }
5033     
5034     return ret;
5035 }
5036
5037 /**************************************************************************
5038  *                      StackEnter & StackLeave         [MMSYSTEM.32][MMSYSTEM.33]
5039  */
5040 void    WINAPI  StackEnterLeave16(void)
5041 {
5042 #ifdef __i386__
5043     /* mmsystem.dll from Win 95 does only this: so does Wine */
5044     __asm__("stc");
5045 #endif
5046 }
5047
5048 /**************************************************************************
5049  *                      WMMMIDIRUNONCE  [MMSYSTEM.8]
5050  */
5051 void WINAPI WMMMidiRunOnce16(void)
5052 {
5053         FIXME("(), stub!\n");
5054 }