ws2_32/tests: Use LONG instead of long.
[wine] / dlls / winmm / mmsystem.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  * MMSYSTEM functions
5  *
6  * Copyright 1993      Martin Ayotte
7  *           1998-2003 Eric Pouech
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 /*
25  * Eric POUECH :
26  *      99/4    added mmTask and mmThread functions support
27  */
28
29 #include <stdarg.h>
30 #include <string.h>
31
32 #define NONAMELESSUNION
33 #define NONAMELESSSTRUCT
34 #include "windef.h"
35 #include "winbase.h"
36 #include "mmsystem.h"
37 #include "winternl.h"
38 #include "wownt32.h"
39 #include "winnls.h"
40
41 #include "wine/winuser16.h"
42 #include "winemm16.h"
43
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(mmsys);
47
48 static WINE_MMTHREAD*   WINMM_GetmmThread(HANDLE16);
49 static LPWINE_DRIVER    DRIVER_OpenDriver16(LPCWSTR, LPCWSTR, LPARAM);
50 static LRESULT          DRIVER_CloseDriver16(HDRVR16, LPARAM, LPARAM);
51 static LRESULT          DRIVER_SendMessage16(HDRVR16, UINT, LPARAM, LPARAM);
52 static LRESULT          MMIO_Callback16(SEGPTR, LPMMIOINFO, UINT, LPARAM, LPARAM);
53
54 /* ###################################################
55  * #                  LIBRARY                        #
56  * ###################################################
57  */
58
59 /**************************************************************************
60  *                      DllEntryPoint (MMSYSTEM.4)
61  *
62  * MMSYSTEM DLL entry point
63  *
64  */
65 BOOL WINAPI MMSYSTEM_LibMain(DWORD fdwReason, HINSTANCE hinstDLL, WORD ds,
66                              WORD wHeapSize, DWORD dwReserved1, WORD wReserved2)
67 {
68     TRACE("%p 0x%x\n", hinstDLL, fdwReason);
69
70     switch (fdwReason) {
71     case DLL_PROCESS_ATTACH:
72         /* need to load WinMM in order to:
73          * - initiate correctly shared variables (WINMM_Init())
74          */
75         if (!GetModuleHandleA("WINMM.DLL"))
76         {
77             ERR("Could not load sibling WinMM.dll\n");
78             return FALSE;
79         }
80         /* hook in our 16 bit function pointers */
81         pFnGetMMThread16    = WINMM_GetmmThread;
82         pFnOpenDriver16     = DRIVER_OpenDriver16;
83         pFnCloseDriver16    = DRIVER_CloseDriver16;
84         pFnSendMessage16    = DRIVER_SendMessage16;
85         pFnMmioCallback16   = MMIO_Callback16;
86         pFnReleaseThunkLock = ReleaseThunkLock;
87         pFnRestoreThunkLock = RestoreThunkLock;
88         MMDRV_Init16();
89         break;
90     case DLL_PROCESS_DETACH:
91         pFnGetMMThread16    = NULL;
92         pFnOpenDriver16     = NULL;
93         pFnCloseDriver16    = NULL;
94         pFnSendMessage16    = NULL;
95         pFnMmioCallback16   = NULL;
96         pFnReleaseThunkLock = NULL;
97         pFnRestoreThunkLock = NULL;
98         /* FIXME: add equivalent for MMDRV_Init16() */
99         break;
100     case DLL_THREAD_ATTACH:
101     case DLL_THREAD_DETACH:
102         break;
103     }
104     return TRUE;
105 }
106
107 /**************************************************************************
108  *                              WEP                     [MMSYSTEM.1]
109  */
110 int WINAPI MMSYSTEM_WEP(HINSTANCE16 hInstance, WORD wDataSeg,
111                         WORD cbHeapSize, LPSTR lpCmdLine)
112 {
113     TRACE("STUB: Unloading MMSystem DLL ... hInst=%04X\n", hInstance);
114     return TRUE;
115 }
116
117 /* ###################################################
118  * #                  PlaySound                      #
119  * ###################################################
120  */
121
122 /**************************************************************************
123  *                              PlaySound               [MMSYSTEM.3]
124  */
125 BOOL16 WINAPI PlaySound16(LPCSTR pszSound, HMODULE16 hmod, DWORD fdwSound)
126 {
127     BOOL16      retv;
128     DWORD       lc;
129
130     if ((fdwSound & SND_RESOURCE) == SND_RESOURCE)
131     {
132         HGLOBAL16 handle;
133         HRSRC16 res;
134
135         if (!(res = FindResource16( hmod, pszSound, "WAVE" ))) return FALSE;
136         if (!(handle = LoadResource16( hmod, res ))) return FALSE;
137         pszSound = LockResource16(handle);
138         fdwSound = (fdwSound & ~SND_RESOURCE) | SND_MEMORY;
139         /* FIXME: FreeResource16 */
140     }
141
142     ReleaseThunkLock(&lc);
143     retv = PlaySoundA(pszSound, 0, fdwSound);
144     RestoreThunkLock(lc);
145
146     return retv;
147 }
148
149 /**************************************************************************
150  *                              sndPlaySound            [MMSYSTEM.2]
151  */
152 BOOL16 WINAPI sndPlaySound16(LPCSTR lpszSoundName, UINT16 uFlags)
153 {
154     BOOL16      retv;
155     DWORD       lc;
156
157     ReleaseThunkLock(&lc);
158     retv = sndPlaySoundA(lpszSoundName, uFlags);
159     RestoreThunkLock(lc);
160
161     return retv;
162 }
163
164 /* ###################################################
165  * #                    MISC                         #
166  * ###################################################
167  */
168
169 /**************************************************************************
170  *                              mmsystemGetVersion      [MMSYSTEM.5]
171  *
172  */
173 UINT16 WINAPI mmsystemGetVersion16(void)
174 {
175     return mmsystemGetVersion();
176 }
177
178 /**************************************************************************
179  *                              DriverCallback                  [MMSYSTEM.31]
180  */
181 BOOL16 WINAPI DriverCallback16(DWORD dwCallBack, UINT16 uFlags, HDRVR16 hDev,
182                                WORD wMsg, DWORD dwUser, DWORD dwParam1,
183                                DWORD dwParam2)
184 {
185     return DriverCallback(dwCallBack, uFlags, HDRVR_32(hDev), wMsg, dwUser, dwParam1, dwParam2);
186 }
187
188 /**************************************************************************
189  *                      OutputDebugStr          [MMSYSTEM.30]
190  */
191 void WINAPI OutputDebugStr16(LPCSTR str)
192 {
193     OutputDebugStringA( str );
194 }
195
196
197 /* ###################################################
198  * #                    MIXER                        #
199  * ###################################################
200  */
201
202 /**************************************************************************
203  *      Mixer devices. New to Win95
204  */
205
206 /**************************************************************************
207  *                              mixerGetNumDevs                 [MMSYSTEM.800]
208  */
209 UINT16 WINAPI mixerGetNumDevs16(void)
210 {
211     return MMDRV_GetNum(MMDRV_MIXER);
212 }
213
214 /**************************************************************************
215  *                              mixerGetDevCaps                 [MMSYSTEM.801]
216  */
217 UINT16 WINAPI mixerGetDevCaps16(UINT16 uDeviceID, LPMIXERCAPS16 lpCaps,
218                                 UINT16 uSize)
219 {
220     MIXERCAPSA  micA;
221     UINT        ret;
222
223     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
224
225     ret = mixerGetDevCapsA(uDeviceID, &micA, sizeof(micA));
226     if (ret == MMSYSERR_NOERROR) {
227         MIXERCAPS16 mic16;
228         mic16.wMid           = micA.wMid;
229         mic16.wPid           = micA.wPid;
230         mic16.vDriverVersion = micA.vDriverVersion;
231         strcpy(mic16.szPname, micA.szPname);
232         mic16.fdwSupport     = micA.fdwSupport;
233         mic16.cDestinations  = micA.cDestinations;
234         memcpy(lpCaps, &mic16, min(uSize, sizeof(mic16)));
235     }
236     return ret;
237 }
238
239 /**************************************************************************
240  *                              mixerOpen                       [MMSYSTEM.802]
241  */
242 UINT16 WINAPI mixerOpen16(LPHMIXER16 lphmix, UINT16 uDeviceID, DWORD dwCallback,
243                           DWORD dwInstance, DWORD fdwOpen)
244 {
245     HMIXER      hmix;
246     UINT        ret;
247
248     ret = MIXER_Open(&hmix, uDeviceID, dwCallback, dwInstance, fdwOpen, FALSE);
249     if (lphmix) *lphmix = HMIXER_16(hmix);
250     return ret;
251 }
252
253 /**************************************************************************
254  *                              mixerClose                      [MMSYSTEM.803]
255  */
256 UINT16 WINAPI mixerClose16(HMIXER16 hMix)
257 {
258     return mixerClose(HMIXER_32(hMix));
259 }
260
261 /**************************************************************************
262  *                              mixerGetID (MMSYSTEM.806)
263  */
264 UINT16 WINAPI mixerGetID16(HMIXEROBJ16 hmix, LPUINT16 lpid, DWORD fdwID)
265 {
266     UINT        xid;
267     UINT        ret = mixerGetID(HMIXEROBJ_32(hmix), &xid, fdwID);
268
269     if (lpid)
270         *lpid = xid;
271     return ret;
272 }
273
274 /**************************************************************************
275  *                              mixerGetControlDetails  [MMSYSTEM.808]
276  */
277 UINT16 WINAPI mixerGetControlDetails16(HMIXEROBJ16 hmix,
278                                        LPMIXERCONTROLDETAILS16 lpmcd,
279                                        DWORD fdwDetails)
280 {
281     DWORD       ret = MMSYSERR_NOTENABLED;
282     SEGPTR      sppaDetails;
283
284     TRACE("(%04x, %p, %08x)\n", hmix, lpmcd, fdwDetails);
285
286     if (lpmcd == NULL || lpmcd->cbStruct != sizeof(*lpmcd))
287         return MMSYSERR_INVALPARAM;
288
289     sppaDetails = (SEGPTR)lpmcd->paDetails;
290     lpmcd->paDetails = MapSL(sppaDetails);
291     ret = mixerGetControlDetailsA(HMIXEROBJ_32(hmix),
292                                  (LPMIXERCONTROLDETAILS)lpmcd, fdwDetails);
293     lpmcd->paDetails = (LPVOID)sppaDetails;
294
295     return ret;
296 }
297
298 /**************************************************************************
299  *                              mixerGetLineControls            [MMSYSTEM.807]
300  */
301 UINT16 WINAPI mixerGetLineControls16(HMIXEROBJ16 hmix,
302                                      LPMIXERLINECONTROLS16 lpmlc16,
303                                      DWORD fdwControls)
304 {
305     MIXERLINECONTROLSA  mlcA;
306     DWORD               ret;
307     unsigned int        i;
308     LPMIXERCONTROL16    lpmc16;
309
310     TRACE("(%04x, %p, %08x)\n", hmix, lpmlc16, fdwControls);
311
312     if (lpmlc16 == NULL || lpmlc16->cbStruct != sizeof(*lpmlc16) ||
313         lpmlc16->cbmxctrl != sizeof(MIXERCONTROL16))
314         return MMSYSERR_INVALPARAM;
315
316     mlcA.cbStruct = sizeof(mlcA);
317     mlcA.dwLineID = lpmlc16->dwLineID;
318     mlcA.u.dwControlID = lpmlc16->u.dwControlID;
319     mlcA.u.dwControlType = lpmlc16->u.dwControlType;
320     mlcA.cControls = lpmlc16->cControls;
321     mlcA.cbmxctrl = sizeof(MIXERCONTROLA);
322     mlcA.pamxctrl = HeapAlloc(GetProcessHeap(), 0,
323                               mlcA.cControls * mlcA.cbmxctrl);
324
325     ret = mixerGetLineControlsA(HMIXEROBJ_32(hmix), &mlcA, fdwControls);
326
327     if (ret == MMSYSERR_NOERROR) {
328         lpmlc16->dwLineID = mlcA.dwLineID;
329         lpmlc16->u.dwControlID = mlcA.u.dwControlID;
330         lpmlc16->u.dwControlType = mlcA.u.dwControlType;
331         lpmlc16->cControls = mlcA.cControls;
332
333         lpmc16 = MapSL(lpmlc16->pamxctrl);
334
335         for (i = 0; i < mlcA.cControls; i++) {
336             lpmc16[i].cbStruct = sizeof(MIXERCONTROL16);
337             lpmc16[i].dwControlID = mlcA.pamxctrl[i].dwControlID;
338             lpmc16[i].dwControlType = mlcA.pamxctrl[i].dwControlType;
339             lpmc16[i].fdwControl = mlcA.pamxctrl[i].fdwControl;
340             lpmc16[i].cMultipleItems = mlcA.pamxctrl[i].cMultipleItems;
341             strcpy(lpmc16[i].szShortName, mlcA.pamxctrl[i].szShortName);
342             strcpy(lpmc16[i].szName, mlcA.pamxctrl[i].szName);
343             /* sizeof(lpmc16[i].Bounds) == sizeof(mlcA.pamxctrl[i].Bounds) */
344             memcpy(&lpmc16[i].Bounds, &mlcA.pamxctrl[i].Bounds,
345                    sizeof(mlcA.pamxctrl[i].Bounds));
346             /* sizeof(lpmc16[i].Metrics) == sizeof(mlcA.pamxctrl[i].Metrics) */
347             memcpy(&lpmc16[i].Metrics, &mlcA.pamxctrl[i].Metrics,
348                    sizeof(mlcA.pamxctrl[i].Metrics));
349         }
350     }
351
352     HeapFree(GetProcessHeap(), 0, mlcA.pamxctrl);
353
354     return ret;
355 }
356
357 /**************************************************************************
358  *                              mixerGetLineInfo        [MMSYSTEM.805]
359  */
360 UINT16 WINAPI mixerGetLineInfo16(HMIXEROBJ16 hmix, LPMIXERLINE16 lpmli16,
361                                  DWORD fdwInfo)
362 {
363     MIXERLINEA          mliA;
364     UINT                ret;
365
366     TRACE("(%04x, %p, %08x)\n", hmix, lpmli16, fdwInfo);
367
368     if (lpmli16 == NULL || lpmli16->cbStruct != sizeof(*lpmli16))
369         return MMSYSERR_INVALPARAM;
370
371     mliA.cbStruct = sizeof(mliA);
372     switch (fdwInfo & MIXER_GETLINEINFOF_QUERYMASK) {
373     case MIXER_GETLINEINFOF_COMPONENTTYPE:
374         mliA.dwComponentType = lpmli16->dwComponentType;
375         break;
376     case MIXER_GETLINEINFOF_DESTINATION:
377         mliA.dwDestination = lpmli16->dwDestination;
378         break;
379     case MIXER_GETLINEINFOF_LINEID:
380         mliA.dwLineID = lpmli16->dwLineID;
381         break;
382     case MIXER_GETLINEINFOF_SOURCE:
383         mliA.dwDestination = lpmli16->dwDestination;
384         mliA.dwSource = lpmli16->dwSource;
385         break;
386     case MIXER_GETLINEINFOF_TARGETTYPE:
387         mliA.Target.dwType = lpmli16->Target.dwType;
388         mliA.Target.wMid = lpmli16->Target.wMid;
389         mliA.Target.wPid = lpmli16->Target.wPid;
390         mliA.Target.vDriverVersion = lpmli16->Target.vDriverVersion;
391         strcpy(mliA.Target.szPname, lpmli16->Target.szPname);
392         break;
393     default:
394         FIXME("Unsupported fdwControls=0x%08x\n", fdwInfo);
395     }
396
397     ret = mixerGetLineInfoA(HMIXEROBJ_32(hmix), &mliA, fdwInfo);
398
399     lpmli16->dwDestination      = mliA.dwDestination;
400     lpmli16->dwSource           = mliA.dwSource;
401     lpmli16->dwLineID           = mliA.dwLineID;
402     lpmli16->fdwLine            = mliA.fdwLine;
403     lpmli16->dwUser             = mliA.dwUser;
404     lpmli16->dwComponentType    = mliA.dwComponentType;
405     lpmli16->cChannels          = mliA.cChannels;
406     lpmli16->cConnections       = mliA.cConnections;
407     lpmli16->cControls          = mliA.cControls;
408     strcpy(lpmli16->szShortName, mliA.szShortName);
409     strcpy(lpmli16->szName, mliA.szName);
410     lpmli16->Target.dwType      = mliA.Target.dwType;
411     lpmli16->Target.dwDeviceID  = mliA.Target.dwDeviceID;
412     lpmli16->Target.wMid        = mliA.Target.wMid;
413     lpmli16->Target.wPid        = mliA.Target.wPid;
414     lpmli16->Target.vDriverVersion = mliA.Target.vDriverVersion;
415     strcpy(lpmli16->Target.szPname, mliA.Target.szPname);
416
417     return ret;
418 }
419
420 /**************************************************************************
421  *                              mixerSetControlDetails  [MMSYSTEM.809]
422  */
423 UINT16 WINAPI mixerSetControlDetails16(HMIXEROBJ16 hmix,
424                                        LPMIXERCONTROLDETAILS16 lpmcd,
425                                        DWORD fdwDetails)
426 {
427     TRACE("(%04x, %p, %08x)\n", hmix, lpmcd, fdwDetails);
428     return MMSYSERR_NOTENABLED;
429 }
430
431 /**************************************************************************
432  *                              mixerMessage            [MMSYSTEM.804]
433  */
434 DWORD WINAPI mixerMessage16(HMIXER16 hmix, UINT16 uMsg, DWORD dwParam1,
435                              DWORD dwParam2)
436 {
437     return mixerMessage(HMIXER_32(hmix), uMsg, dwParam1, dwParam2);
438 }
439
440 /**************************************************************************
441  *                              auxGetNumDevs           [MMSYSTEM.350]
442  */
443 UINT16 WINAPI auxGetNumDevs16(void)
444 {
445     return MMDRV_GetNum(MMDRV_AUX);
446 }
447
448 /* ###################################################
449  * #                     AUX                         #
450  * ###################################################
451  */
452
453 /**************************************************************************
454  *                              auxGetDevCaps           [MMSYSTEM.351]
455  */
456 UINT16 WINAPI auxGetDevCaps16(UINT16 uDeviceID, LPAUXCAPS16 lpCaps, UINT16 uSize)
457 {
458     AUXCAPSA  acA;
459     UINT      ret;
460
461     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
462
463     ret = auxGetDevCapsA(uDeviceID, &acA, sizeof(acA));
464     if (ret == MMSYSERR_NOERROR) {
465         AUXCAPS16 ac16;
466         ac16.wMid           = acA.wMid; 
467         ac16.wPid           = acA.wPid; 
468         ac16.vDriverVersion = acA.vDriverVersion; 
469         strcpy(ac16.szPname, acA.szPname); 
470         ac16.wTechnology    = acA.wTechnology; 
471         ac16.dwSupport      = acA.dwSupport; 
472         memcpy(lpCaps, &ac16, min(uSize, sizeof(ac16)));
473     }
474     return ret;
475 }
476
477 /**************************************************************************
478  *                              auxGetVolume            [MMSYSTEM.352]
479  */
480 UINT16 WINAPI auxGetVolume16(UINT16 uDeviceID, LPDWORD lpdwVolume)
481 {
482     LPWINE_MLD          wmld;
483
484     TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);
485
486     if ((wmld = MMDRV_Get((HANDLE)(ULONG_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
487         return MMSYSERR_INVALHANDLE;
488     return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L, TRUE);
489 }
490
491 /**************************************************************************
492  *                              auxSetVolume            [MMSYSTEM.353]
493  */
494 UINT16 WINAPI auxSetVolume16(UINT16 uDeviceID, DWORD dwVolume)
495 {
496     LPWINE_MLD          wmld;
497
498     TRACE("(%04X, %u) !\n", uDeviceID, dwVolume);
499
500     if ((wmld = MMDRV_Get((HANDLE)(ULONG_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
501         return MMSYSERR_INVALHANDLE;
502     return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L, TRUE);
503 }
504
505 /**************************************************************************
506  *                              auxOutMessage           [MMSYSTEM.354]
507  */
508 DWORD WINAPI auxOutMessage16(UINT16 uDeviceID, UINT16 uMessage, DWORD dw1, DWORD dw2)
509 {
510     LPWINE_MLD          wmld;
511
512     TRACE("(%04X, %04X, %08X, %08X)\n", uDeviceID, uMessage, dw1, dw2);
513
514     switch (uMessage) {
515     case AUXDM_GETNUMDEVS:
516     case AUXDM_SETVOLUME:
517         /* no argument conversion needed */
518         break;
519     case AUXDM_GETVOLUME:
520         return auxGetVolume16(uDeviceID, MapSL(dw1));
521     case AUXDM_GETDEVCAPS:
522         return auxGetDevCaps16(uDeviceID, MapSL(dw1), dw2);
523     default:
524         TRACE("(%04x, %04x, %08x, %08x): unhandled message\n",
525               uDeviceID, uMessage, dw1, dw2);
526         break;
527     }
528     if ((wmld = MMDRV_Get((HANDLE)(ULONG_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
529         return MMSYSERR_INVALHANDLE;
530
531     return MMDRV_Message(wmld, uMessage, dw1, dw2, TRUE);
532 }
533
534 /* ###################################################
535  * #                     MCI                         #
536  * ###################################################
537  */
538
539 /**************************************************************************
540  *                              mciGetErrorString               [MMSYSTEM.706]
541  */
542 BOOL16 WINAPI mciGetErrorString16(DWORD wError, LPSTR lpstrBuffer, UINT16 uLength)
543 {
544     return mciGetErrorStringA(wError, lpstrBuffer, uLength);
545 }
546
547 /**************************************************************************
548  *                              mciDriverNotify                 [MMSYSTEM.711]
549  */
550 BOOL16 WINAPI mciDriverNotify16(HWND16 hWndCallBack, UINT16 wDevID, UINT16 wStatus)
551 {
552     TRACE("(%04X, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
553
554     return PostMessageA(HWND_32(hWndCallBack), MM_MCINOTIFY, wStatus, wDevID);
555 }
556
557 /**************************************************************************
558  *                      mciGetDriverData                        [MMSYSTEM.708]
559  */
560 DWORD WINAPI mciGetDriverData16(UINT16 uDeviceID)
561 {
562     return mciGetDriverData(uDeviceID);
563 }
564
565 /**************************************************************************
566  *                      mciSetDriverData                        [MMSYSTEM.707]
567  */
568 BOOL16 WINAPI mciSetDriverData16(UINT16 uDeviceID, DWORD data)
569 {
570     return mciSetDriverData(uDeviceID, data);
571 }
572
573 /**************************************************************************
574  *                              mciSendCommand                  [MMSYSTEM.701]
575  */
576 DWORD WINAPI mciSendCommand16(UINT16 wDevID, UINT16 wMsg, DWORD dwParam1, DWORD dwParam2)
577 {
578     DWORD               dwRet;
579
580     TRACE("(%04X, %s, %08X, %08X)\n",
581           wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
582
583     dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2, FALSE);
584     dwRet = MCI_CleanUp(dwRet, wMsg, (DWORD)MapSL(dwParam2));
585     TRACE("=> %d\n", dwRet);
586     return dwRet;
587 }
588
589 /**************************************************************************
590  *                              mciGetDeviceID                  [MMSYSTEM.703]
591  */
592 UINT16 WINAPI mciGetDeviceID16(LPCSTR lpstrName)
593 {
594     TRACE("(\"%s\")\n", lpstrName);
595
596     return mciGetDeviceIDA(lpstrName);
597 }
598
599 /**************************************************************************
600  *                              mciSetYieldProc                 [MMSYSTEM.714]
601  */
602 BOOL16 WINAPI mciSetYieldProc16(UINT16 uDeviceID, YIELDPROC16 fpYieldProc, DWORD dwYieldData)
603 {
604     LPWINE_MCIDRIVER    wmd;
605
606     TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
607
608     if (!(wmd = MCI_GetDriver(uDeviceID))) {
609         WARN("Bad uDeviceID\n");
610         return FALSE;
611     }
612
613     wmd->lpfnYieldProc = (YIELDPROC)fpYieldProc;
614     wmd->dwYieldData   = dwYieldData;
615     wmd->bIs32         = FALSE;
616
617     return TRUE;
618 }
619
620 /**************************************************************************
621  *                              mciGetDeviceIDFromElementID     [MMSYSTEM.715]
622  */
623 UINT16 WINAPI mciGetDeviceIDFromElementID16(DWORD dwElementID, LPCSTR lpstrType)
624 {
625     FIXME("(%u, %s) stub\n", dwElementID, lpstrType);
626     return 0;
627 }
628
629 /**************************************************************************
630  *                              mciGetYieldProc                 [MMSYSTEM.716]
631  */
632 YIELDPROC16 WINAPI mciGetYieldProc16(UINT16 uDeviceID, DWORD* lpdwYieldData)
633 {
634     LPWINE_MCIDRIVER    wmd;
635
636     TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
637
638     if (!(wmd = MCI_GetDriver(uDeviceID))) {
639         WARN("Bad uDeviceID\n");
640         return NULL;
641     }
642     if (!wmd->lpfnYieldProc) {
643         WARN("No proc set\n");
644         return NULL;
645     }
646     if (wmd->bIs32) {
647         WARN("Proc is 32 bit\n");
648         return NULL;
649     }
650     if (lpdwYieldData) *lpdwYieldData = wmd->dwYieldData;
651     return (YIELDPROC16)wmd->lpfnYieldProc;
652 }
653
654 /**************************************************************************
655  *                              mciGetCreatorTask               [MMSYSTEM.717]
656  */
657 HTASK16 WINAPI mciGetCreatorTask16(UINT16 uDeviceID)
658 {
659     LPWINE_MCIDRIVER wmd;
660     HTASK16 ret = 0;
661
662     if ((wmd = MCI_GetDriver(uDeviceID))) 
663         ret = HTASK_16(wmd->CreatorThread);
664
665     TRACE("(%u) => %04x\n", uDeviceID, ret);
666     return ret;
667 }
668
669 /**************************************************************************
670  *                              mciDriverYield                  [MMSYSTEM.710]
671  */
672 UINT16 WINAPI mciDriverYield16(UINT16 uDeviceID)
673 {
674     LPWINE_MCIDRIVER    wmd;
675     UINT16              ret = 0;
676
677     /*    TRACE("(%04x)\n", uDeviceID); */
678
679     if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc || wmd->bIs32) {
680         UserYield16();
681     } else {
682         ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
683     }
684
685     return ret;
686 }
687
688 /* ###################################################
689  * #                     MIDI                        #
690  * ###################################################
691  */
692
693 /**************************************************************************
694  *                              midiOutGetNumDevs       [MMSYSTEM.201]
695  */
696 UINT16 WINAPI midiOutGetNumDevs16(void)
697 {
698     return MMDRV_GetNum(MMDRV_MIDIOUT);
699 }
700
701 /**************************************************************************
702  *                              midiOutGetDevCaps       [MMSYSTEM.202]
703  */
704 UINT16 WINAPI midiOutGetDevCaps16(UINT16 uDeviceID, LPMIDIOUTCAPS16 lpCaps,
705                                   UINT16 uSize)
706 {
707     MIDIOUTCAPSA        mocA;
708     UINT                ret;
709
710     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
711
712     ret = midiOutGetDevCapsA(uDeviceID, &mocA, sizeof(mocA));
713     if (ret == MMSYSERR_NOERROR) {
714         MIDIOUTCAPS16 moc16;
715         moc16.wMid            = mocA.wMid;
716         moc16.wPid            = mocA.wPid;
717         moc16.vDriverVersion  = mocA.vDriverVersion;
718         strcpy(moc16.szPname, mocA.szPname);
719         moc16.wTechnology     = mocA.wTechnology;
720         moc16.wVoices         = mocA.wVoices;
721         moc16.wNotes          = mocA.wNotes;
722         moc16.wChannelMask    = mocA.wChannelMask;
723         moc16.dwSupport       = mocA.dwSupport;
724         memcpy(lpCaps, &moc16, min(uSize, sizeof(moc16)));
725     }
726     return ret;
727  }
728
729 /**************************************************************************
730  *                              midiOutGetErrorText     [MMSYSTEM.203]
731  *                              midiInGetErrorText      [MMSYSTEM.303]
732  */
733 UINT16 WINAPI midiOutGetErrorText16(UINT16 uError, LPSTR lpText, UINT16 uSize)
734 {
735     return midiOutGetErrorTextA(uError, lpText, uSize);
736 }
737
738 /**************************************************************************
739  *                              midiOutOpen             [MMSYSTEM.204]
740  */
741 UINT16 WINAPI midiOutOpen16(HMIDIOUT16* lphMidiOut, UINT16 uDeviceID,
742                             DWORD dwCallback, DWORD dwInstance, DWORD dwFlags)
743 {
744     HMIDIOUT    hmo;
745     UINT        ret;
746
747     ret = MIDI_OutOpen(&hmo, uDeviceID, dwCallback, dwInstance, dwFlags, FALSE);
748
749     if (lphMidiOut != NULL) *lphMidiOut = HMIDIOUT_16(hmo);
750     return ret;
751 }
752
753 /**************************************************************************
754  *                              midiOutClose            [MMSYSTEM.205]
755  */
756 UINT16 WINAPI midiOutClose16(HMIDIOUT16 hMidiOut)
757 {
758     return midiOutClose(HMIDIOUT_32(hMidiOut));
759 }
760
761 /**************************************************************************
762  *                              midiOutPrepareHeader    [MMSYSTEM.206]
763  */
764 UINT16 WINAPI midiOutPrepareHeader16(HMIDIOUT16 hMidiOut,         /* [in] */
765                                      SEGPTR lpsegMidiOutHdr,      /* [???] */
766                                      UINT16 uSize)                /* [in] */
767 {
768     LPWINE_MLD          wmld;
769
770     TRACE("(%04X, %08x, %d)\n", hMidiOut, lpsegMidiOutHdr, uSize);
771
772     if ((wmld = MMDRV_Get(HMIDIOUT_32(hMidiOut), MMDRV_MIDIOUT, FALSE)) == NULL)
773         return MMSYSERR_INVALHANDLE;
774
775     return MMDRV_Message(wmld, MODM_PREPARE, lpsegMidiOutHdr, uSize, FALSE);
776 }
777
778 /**************************************************************************
779  *                              midiOutUnprepareHeader  [MMSYSTEM.207]
780  */
781 UINT16 WINAPI midiOutUnprepareHeader16(HMIDIOUT16 hMidiOut,         /* [in] */
782                                        SEGPTR lpsegMidiOutHdr,      /* [???] */
783                                        UINT16 uSize)                /* [in] */
784 {
785     LPWINE_MLD          wmld;
786     LPMIDIHDR16         lpMidiOutHdr = MapSL(lpsegMidiOutHdr);
787
788     TRACE("(%04X, %08x, %d)\n", hMidiOut, lpsegMidiOutHdr, uSize);
789
790     if (!(lpMidiOutHdr->dwFlags & MHDR_PREPARED)) {
791         return MMSYSERR_NOERROR;
792     }
793
794     if ((wmld = MMDRV_Get(HMIDIOUT_32(hMidiOut), MMDRV_MIDIOUT, FALSE)) == NULL)
795         return MMSYSERR_INVALHANDLE;
796
797     return MMDRV_Message(wmld, MODM_UNPREPARE, lpsegMidiOutHdr, uSize, FALSE);
798 }
799
800 /**************************************************************************
801  *                              midiOutShortMsg         [MMSYSTEM.208]
802  */
803 UINT16 WINAPI midiOutShortMsg16(HMIDIOUT16 hMidiOut, DWORD dwMsg)
804 {
805     return midiOutShortMsg(HMIDIOUT_32(hMidiOut), dwMsg);
806 }
807
808 /**************************************************************************
809  *                              midiOutLongMsg          [MMSYSTEM.209]
810  */
811 UINT16 WINAPI midiOutLongMsg16(HMIDIOUT16 hMidiOut,          /* [in] */
812                                LPMIDIHDR16 lpsegMidiOutHdr,  /* [???] NOTE: SEGPTR */
813                                UINT16 uSize)                 /* [in] */
814 {
815     LPWINE_MLD          wmld;
816
817     TRACE("(%04X, %p, %d)\n", hMidiOut, lpsegMidiOutHdr, uSize);
818
819     if ((wmld = MMDRV_Get(HMIDIOUT_32(hMidiOut), MMDRV_MIDIOUT, FALSE)) == NULL)
820         return MMSYSERR_INVALHANDLE;
821
822     return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD_PTR)lpsegMidiOutHdr, uSize, FALSE);
823 }
824
825 /**************************************************************************
826  *                              midiOutReset            [MMSYSTEM.210]
827  */
828 UINT16 WINAPI midiOutReset16(HMIDIOUT16 hMidiOut)
829 {
830     return midiOutReset(HMIDIOUT_32(hMidiOut));
831 }
832
833 /**************************************************************************
834  *                              midiOutGetVolume        [MMSYSTEM.211]
835  */
836 UINT16 WINAPI midiOutGetVolume16(UINT16 uDeviceID, DWORD* lpdwVolume)
837 {
838     return midiOutGetVolume(HMIDIOUT_32(uDeviceID), lpdwVolume);
839 }
840
841 /**************************************************************************
842  *                              midiOutSetVolume        [MMSYSTEM.212]
843  */
844 UINT16 WINAPI midiOutSetVolume16(UINT16 uDeviceID, DWORD dwVolume)
845 {
846     return midiOutSetVolume(HMIDIOUT_32(uDeviceID), dwVolume);
847 }
848
849 /**************************************************************************
850  *                              midiOutCachePatches             [MMSYSTEM.213]
851  */
852 UINT16 WINAPI midiOutCachePatches16(HMIDIOUT16 hMidiOut, UINT16 uBank,
853                                     WORD* lpwPatchArray, UINT16 uFlags)
854 {
855     return midiOutCachePatches(HMIDIOUT_32(hMidiOut), uBank, lpwPatchArray,
856                                uFlags);
857 }
858
859 /**************************************************************************
860  *                              midiOutCacheDrumPatches [MMSYSTEM.214]
861  */
862 UINT16 WINAPI midiOutCacheDrumPatches16(HMIDIOUT16 hMidiOut, UINT16 uPatch,
863                                         WORD* lpwKeyArray, UINT16 uFlags)
864 {
865     return midiOutCacheDrumPatches(HMIDIOUT_32(hMidiOut), uPatch, lpwKeyArray, uFlags);
866 }
867
868 /**************************************************************************
869  *                              midiOutGetID            [MMSYSTEM.215]
870  */
871 UINT16 WINAPI midiOutGetID16(HMIDIOUT16 hMidiOut, UINT16* lpuDeviceID)
872 {
873     LPWINE_MLD          wmld;
874
875     TRACE("(%04X, %p)\n", hMidiOut, lpuDeviceID);
876
877     if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
878     if ((wmld = MMDRV_Get(HMIDIOUT_32(hMidiOut), MMDRV_MIDIOUT, FALSE)) == NULL)
879         return MMSYSERR_INVALHANDLE;
880
881     *lpuDeviceID = wmld->uDeviceID;
882     return MMSYSERR_NOERROR;
883 }
884
885 /**************************************************************************
886  *                              midiOutMessage          [MMSYSTEM.216]
887  */
888 DWORD WINAPI midiOutMessage16(HMIDIOUT16 hMidiOut, UINT16 uMessage,
889                               DWORD dwParam1, DWORD dwParam2)
890 {
891     LPWINE_MLD          wmld;
892
893     TRACE("(%04X, %04X, %08X, %08X)\n", hMidiOut, uMessage, dwParam1, dwParam2);
894
895     if ((wmld = MMDRV_Get(HMIDIOUT_32(hMidiOut), MMDRV_MIDIOUT, FALSE)) == NULL)
896         return MMSYSERR_INVALHANDLE;
897
898     switch (uMessage) {
899     case MODM_OPEN:
900     case MODM_CLOSE:
901         FIXME("can't handle OPEN or CLOSE message!\n");
902         return MMSYSERR_NOTSUPPORTED;
903
904     case MODM_GETVOLUME:
905         return midiOutGetVolume16(hMidiOut, MapSL(dwParam1));
906     case MODM_LONGDATA:
907         return midiOutLongMsg16(hMidiOut, MapSL(dwParam1), dwParam2);
908     case MODM_PREPARE:
909         /* lpMidiOutHdr is still a segmented pointer for this function */
910         return midiOutPrepareHeader16(hMidiOut, dwParam1, dwParam2);
911     case MODM_UNPREPARE:
912         return midiOutUnprepareHeader16(hMidiOut, dwParam1, dwParam2);
913     }
914     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, TRUE);
915 }
916
917 /**************************************************************************
918  *                              midiInGetNumDevs        [MMSYSTEM.301]
919  */
920 UINT16 WINAPI midiInGetNumDevs16(void)
921 {
922     return MMDRV_GetNum(MMDRV_MIDIIN);
923 }
924
925 /**************************************************************************
926  *                              midiInGetDevCaps        [MMSYSTEM.302]
927  */
928 UINT16 WINAPI midiInGetDevCaps16(UINT16 uDeviceID, LPMIDIINCAPS16 lpCaps,
929                                  UINT16 uSize)
930 {
931     MIDIINCAPSA         micA;
932     UINT                ret;
933
934     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
935
936     ret = midiInGetDevCapsA(uDeviceID, &micA, uSize);
937     if (ret == MMSYSERR_NOERROR) {
938         MIDIINCAPS16 mic16;
939         mic16.wMid           = micA.wMid;
940         mic16.wPid           = micA.wPid;
941         mic16.vDriverVersion = micA.vDriverVersion;
942         strcpy(mic16.szPname, micA.szPname);
943         mic16.dwSupport      = micA.dwSupport;
944         memcpy(lpCaps, &mic16, min(uSize, sizeof(mic16)));
945     }
946     return ret;
947 }
948
949 /**************************************************************************
950  *                              midiInOpen              [MMSYSTEM.304]
951  */
952 UINT16 WINAPI midiInOpen16(HMIDIIN16* lphMidiIn, UINT16 uDeviceID,
953                            DWORD dwCallback, DWORD dwInstance, DWORD dwFlags)
954 {
955     HMIDIIN     xhmid;
956     UINT        ret;
957
958     ret = MIDI_InOpen(&xhmid, uDeviceID, dwCallback, dwInstance, dwFlags, FALSE);
959
960     if (lphMidiIn) *lphMidiIn = HMIDIIN_16(xhmid);
961     return ret;
962 }
963
964 /**************************************************************************
965  *                              midiInClose             [MMSYSTEM.305]
966  */
967 UINT16 WINAPI midiInClose16(HMIDIIN16 hMidiIn)
968 {
969     return midiInClose(HMIDIIN_32(hMidiIn));
970 }
971
972 /**************************************************************************
973  *                              midiInPrepareHeader     [MMSYSTEM.306]
974  */
975 UINT16 WINAPI midiInPrepareHeader16(HMIDIIN16 hMidiIn,         /* [in] */
976                                     SEGPTR lpsegMidiInHdr,     /* [???] */
977                                     UINT16 uSize)              /* [in] */
978 {
979     LPWINE_MLD          wmld;
980
981     TRACE("(%04X, %08x, %d)\n", hMidiIn, lpsegMidiInHdr, uSize);
982
983     if ((wmld = MMDRV_Get(HMIDIIN_32(hMidiIn), MMDRV_MIDIIN, FALSE)) == NULL)
984         return MMSYSERR_INVALHANDLE;
985
986     return MMDRV_Message(wmld, MIDM_PREPARE, lpsegMidiInHdr, uSize, FALSE);
987 }
988
989 /**************************************************************************
990  *                              midiInUnprepareHeader   [MMSYSTEM.307]
991  */
992 UINT16 WINAPI midiInUnprepareHeader16(HMIDIIN16 hMidiIn,         /* [in] */
993                                       SEGPTR lpsegMidiInHdr,     /* [???] */
994                                       UINT16 uSize)              /* [in] */
995 {
996     LPWINE_MLD          wmld;
997     LPMIDIHDR16         lpMidiInHdr = MapSL(lpsegMidiInHdr);
998
999     TRACE("(%04X, %08x, %d)\n", hMidiIn, lpsegMidiInHdr, uSize);
1000
1001     if (!(lpMidiInHdr->dwFlags & MHDR_PREPARED)) {
1002         return MMSYSERR_NOERROR;
1003     }
1004
1005     if ((wmld = MMDRV_Get(HMIDIIN_32(hMidiIn), MMDRV_MIDIIN, FALSE)) == NULL)
1006         return MMSYSERR_INVALHANDLE;
1007
1008     return MMDRV_Message(wmld, MIDM_UNPREPARE, lpsegMidiInHdr, uSize, FALSE);
1009 }
1010
1011 /**************************************************************************
1012  *                              midiInAddBuffer         [MMSYSTEM.308]
1013  */
1014 UINT16 WINAPI midiInAddBuffer16(HMIDIIN16 hMidiIn,         /* [in] */
1015                                 MIDIHDR16* lpsegMidiInHdr, /* [???] NOTE: SEGPTR */
1016                                 UINT16 uSize)              /* [in] */
1017 {
1018     LPWINE_MLD          wmld;
1019
1020     TRACE("(%04X, %p, %d)\n", hMidiIn, lpsegMidiInHdr, uSize);
1021
1022     if ((wmld = MMDRV_Get(HMIDIIN_32(hMidiIn), MMDRV_MIDIIN, FALSE)) == NULL)
1023         return MMSYSERR_INVALHANDLE;
1024
1025     return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD_PTR)lpsegMidiInHdr, uSize, FALSE);
1026 }
1027
1028 /**************************************************************************
1029  *                              midiInStart                     [MMSYSTEM.309]
1030  */
1031 UINT16 WINAPI midiInStart16(HMIDIIN16 hMidiIn)
1032 {
1033     return midiInStart(HMIDIIN_32(hMidiIn));
1034 }
1035
1036 /**************************************************************************
1037  *                              midiInStop                      [MMSYSTEM.310]
1038  */
1039 UINT16 WINAPI midiInStop16(HMIDIIN16 hMidiIn)
1040 {
1041     return midiInStop(HMIDIIN_32(hMidiIn));
1042 }
1043
1044 /**************************************************************************
1045  *                              midiInReset                     [MMSYSTEM.311]
1046  */
1047 UINT16 WINAPI midiInReset16(HMIDIIN16 hMidiIn)
1048 {
1049     return midiInReset(HMIDIIN_32(hMidiIn));
1050 }
1051
1052 /**************************************************************************
1053  *                              midiInGetID                     [MMSYSTEM.312]
1054  */
1055 UINT16 WINAPI midiInGetID16(HMIDIIN16 hMidiIn, UINT16* lpuDeviceID)
1056 {
1057     LPWINE_MLD          wmld;
1058
1059     TRACE("(%04X, %p)\n", hMidiIn, lpuDeviceID);
1060
1061     if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
1062
1063     if ((wmld = MMDRV_Get(HMIDIIN_32(hMidiIn), MMDRV_MIDIIN, TRUE)) == NULL)
1064         return MMSYSERR_INVALHANDLE;
1065
1066     *lpuDeviceID = wmld->uDeviceID;
1067
1068     return MMSYSERR_NOERROR;
1069 }
1070
1071 /**************************************************************************
1072  *                              midiInMessage           [MMSYSTEM.313]
1073  */
1074 DWORD WINAPI midiInMessage16(HMIDIIN16 hMidiIn, UINT16 uMessage,
1075                              DWORD dwParam1, DWORD dwParam2)
1076 {
1077     LPWINE_MLD          wmld;
1078
1079     TRACE("(%04X, %04X, %08X, %08X)\n", hMidiIn, uMessage, dwParam1, dwParam2);
1080
1081     switch (uMessage) {
1082     case MIDM_OPEN:
1083     case MIDM_CLOSE:
1084         FIXME("can't handle OPEN or CLOSE message!\n");
1085         return MMSYSERR_NOTSUPPORTED;
1086
1087     case MIDM_GETDEVCAPS:
1088         return midiInGetDevCaps16(hMidiIn, MapSL(dwParam1), dwParam2);
1089     case MIDM_PREPARE:
1090         return midiInPrepareHeader16(hMidiIn, dwParam1, dwParam2);
1091     case MIDM_UNPREPARE:
1092         return midiInUnprepareHeader16(hMidiIn, dwParam1, dwParam2);
1093     case MIDM_ADDBUFFER:
1094         return midiInAddBuffer16(hMidiIn, MapSL(dwParam1), dwParam2);
1095     }
1096
1097     if ((wmld = MMDRV_Get(HMIDIIN_32(hMidiIn), MMDRV_MIDIIN, FALSE)) == NULL)
1098         return MMSYSERR_INVALHANDLE;
1099
1100     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, FALSE);
1101 }
1102
1103 /**************************************************************************
1104  *                              midiStreamClose                 [MMSYSTEM.252]
1105  */
1106 MMRESULT16 WINAPI midiStreamClose16(HMIDISTRM16 hMidiStrm)
1107 {
1108     return midiStreamClose(HMIDISTRM_32(hMidiStrm));
1109 }
1110
1111 /**************************************************************************
1112  *                              midiStreamOpen                  [MMSYSTEM.251]
1113  */
1114 MMRESULT16 WINAPI midiStreamOpen16(HMIDISTRM16* phMidiStrm, LPUINT16 devid,
1115                                    DWORD cMidi, DWORD dwCallback,
1116                                    DWORD dwInstance, DWORD fdwOpen)
1117 {
1118     HMIDISTRM   hMidiStrm32;
1119     MMRESULT    ret;
1120     UINT        devid32;
1121
1122     if (!phMidiStrm || !devid)
1123         return MMSYSERR_INVALPARAM;
1124     devid32 = *devid;
1125     ret = MIDI_StreamOpen(&hMidiStrm32, &devid32, cMidi, dwCallback,
1126                           dwInstance, fdwOpen, FALSE);
1127     *phMidiStrm = HMIDISTRM_16(hMidiStrm32);
1128     *devid = devid32;
1129     return ret;
1130 }
1131
1132 /**************************************************************************
1133  *                              midiStreamOut                   [MMSYSTEM.254]
1134  */
1135 MMRESULT16 WINAPI midiStreamOut16(HMIDISTRM16 hMidiStrm, LPMIDIHDR16 lpMidiHdr, UINT16 cbMidiHdr)
1136 {
1137     return midiStreamOut(HMIDISTRM_32(hMidiStrm), (LPMIDIHDR)lpMidiHdr,
1138                          cbMidiHdr);
1139 }
1140
1141 /**************************************************************************
1142  *                              midiStreamPause                 [MMSYSTEM.255]
1143  */
1144 MMRESULT16 WINAPI midiStreamPause16(HMIDISTRM16 hMidiStrm)
1145 {
1146     return midiStreamPause(HMIDISTRM_32(hMidiStrm));
1147 }
1148
1149 /**************************************************************************
1150  *                              midiStreamPosition              [MMSYSTEM.253]
1151  */
1152 MMRESULT16 WINAPI midiStreamPosition16(HMIDISTRM16 hMidiStrm, LPMMTIME16 lpmmt16, UINT16 cbmmt)
1153 {
1154     MMTIME      mmt32;
1155     MMRESULT    ret;
1156
1157     if (!lpmmt16)
1158         return MMSYSERR_INVALPARAM;
1159     MMSYSTEM_MMTIME16to32(&mmt32, lpmmt16);
1160     ret = midiStreamPosition(HMIDISTRM_32(hMidiStrm), &mmt32, sizeof(MMTIME));
1161     MMSYSTEM_MMTIME32to16(lpmmt16, &mmt32);
1162     return ret;
1163 }
1164
1165 /**************************************************************************
1166  *                              midiStreamProperty              [MMSYSTEM.250]
1167  */
1168 MMRESULT16 WINAPI midiStreamProperty16(HMIDISTRM16 hMidiStrm, LPBYTE lpPropData, DWORD dwProperty)
1169 {
1170     return midiStreamProperty(HMIDISTRM_32(hMidiStrm), lpPropData, dwProperty);
1171 }
1172
1173 /**************************************************************************
1174  *                              midiStreamRestart               [MMSYSTEM.256]
1175  */
1176 MMRESULT16 WINAPI midiStreamRestart16(HMIDISTRM16 hMidiStrm)
1177 {
1178     return midiStreamRestart(HMIDISTRM_32(hMidiStrm));
1179 }
1180
1181 /**************************************************************************
1182  *                              midiStreamStop                  [MMSYSTEM.257]
1183  */
1184 MMRESULT16 WINAPI midiStreamStop16(HMIDISTRM16 hMidiStrm)
1185 {
1186     return midiStreamStop(HMIDISTRM_32(hMidiStrm));
1187 }
1188
1189 /* ###################################################
1190  * #                     WAVE                        #
1191  * ###################################################
1192  */
1193
1194 /**************************************************************************
1195  *                              waveOutGetNumDevs               [MMSYSTEM.401]
1196  */
1197 UINT16 WINAPI waveOutGetNumDevs16(void)
1198 {
1199     return MMDRV_GetNum(MMDRV_WAVEOUT);
1200 }
1201
1202 /**************************************************************************
1203  *                              waveOutGetDevCaps               [MMSYSTEM.402]
1204  */
1205 UINT16 WINAPI waveOutGetDevCaps16(UINT16 uDeviceID,
1206                                   LPWAVEOUTCAPS16 lpCaps, UINT16 uSize)
1207 {
1208     WAVEOUTCAPSA        wocA;
1209     UINT                ret;
1210     TRACE("(%u %p %u)!\n", uDeviceID, lpCaps, uSize);
1211
1212     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
1213
1214     ret = waveOutGetDevCapsA(uDeviceID, &wocA, sizeof(wocA));
1215     if (ret == MMSYSERR_NOERROR) {
1216         WAVEOUTCAPS16 woc16;
1217         woc16.wMid           = wocA.wMid;
1218         woc16.wPid           = wocA.wPid;
1219         woc16.vDriverVersion = wocA.vDriverVersion;
1220         strcpy(woc16.szPname, wocA.szPname);
1221         woc16.dwFormats      = wocA.dwFormats;
1222         woc16.wChannels      = wocA.wChannels;
1223         woc16.dwSupport      = wocA.dwSupport;
1224         memcpy(lpCaps, &woc16, min(uSize, sizeof(woc16)));
1225     }
1226     return ret;
1227 }
1228
1229 /**************************************************************************
1230  *                              waveOutGetErrorText     [MMSYSTEM.403]
1231  *                              waveInGetErrorText      [MMSYSTEM.503]
1232  */
1233 UINT16 WINAPI waveOutGetErrorText16(UINT16 uError, LPSTR lpText, UINT16 uSize)
1234 {
1235     return waveOutGetErrorTextA(uError, lpText, uSize);
1236 }
1237
1238 /**************************************************************************
1239  *                      waveOutOpen                     [MMSYSTEM.404]
1240  */
1241 UINT16 WINAPI waveOutOpen16(HWAVEOUT16* lphWaveOut, UINT16 uDeviceID,
1242                             LPCWAVEFORMATEX lpFormat, DWORD dwCallback,
1243                             DWORD dwInstance, DWORD dwFlags)
1244 {
1245     HANDLE              hWaveOut;
1246     UINT                ret;
1247
1248     /* since layout of WAVEFORMATEX is the same for 16/32 bits, we directly
1249      * call the 32 bit version
1250      * however, we need to promote correctly the wave mapper id
1251      * (0xFFFFFFFF and not 0x0000FFFF)
1252      */
1253     ret = WAVE_Open(&hWaveOut, (uDeviceID == (UINT16)-1) ? (UINT)-1 : uDeviceID,
1254                     MMDRV_WAVEOUT, lpFormat, dwCallback, dwInstance, dwFlags, FALSE);
1255
1256     if (lphWaveOut != NULL) *lphWaveOut = HWAVEOUT_16(hWaveOut);
1257     return ret;
1258 }
1259
1260 /**************************************************************************
1261  *                              waveOutClose            [MMSYSTEM.405]
1262  */
1263 UINT16 WINAPI waveOutClose16(HWAVEOUT16 hWaveOut)
1264 {
1265     DWORD       level;
1266     UINT16      ret;
1267
1268     ReleaseThunkLock(&level);
1269     ret = waveOutClose(HWAVEOUT_32(hWaveOut));
1270     RestoreThunkLock(level);
1271     return ret;
1272 }
1273
1274 /**************************************************************************
1275  *                              waveOutPrepareHeader    [MMSYSTEM.406]
1276  */
1277 UINT16 WINAPI waveOutPrepareHeader16(HWAVEOUT16 hWaveOut,      /* [in] */
1278                                      SEGPTR lpsegWaveOutHdr,   /* [???] */
1279                                      UINT16 uSize)             /* [in] */
1280 {
1281     LPWINE_MLD          wmld;
1282     LPWAVEHDR           lpWaveOutHdr = MapSL(lpsegWaveOutHdr);
1283     UINT16              result;
1284
1285     TRACE("(%04X, %08x, %u);\n", hWaveOut, lpsegWaveOutHdr, uSize);
1286
1287     if (lpWaveOutHdr == NULL) return MMSYSERR_INVALPARAM;
1288
1289     if ((wmld = MMDRV_Get(HWAVEOUT_32(hWaveOut), MMDRV_WAVEOUT, FALSE)) == NULL)
1290         return MMSYSERR_INVALHANDLE;
1291
1292     if ((result = MMDRV_Message(wmld, WODM_PREPARE, lpsegWaveOutHdr,
1293                                 uSize, FALSE)) != MMSYSERR_NOTSUPPORTED)
1294         return result;
1295
1296     if (lpWaveOutHdr->dwFlags & WHDR_INQUEUE)
1297         return WAVERR_STILLPLAYING;
1298
1299     lpWaveOutHdr->dwFlags |= WHDR_PREPARED;
1300     lpWaveOutHdr->dwFlags &= ~WHDR_DONE;
1301
1302     return MMSYSERR_NOERROR;
1303 }
1304
1305 /**************************************************************************
1306  *                              waveOutUnprepareHeader  [MMSYSTEM.407]
1307  */
1308 UINT16 WINAPI waveOutUnprepareHeader16(HWAVEOUT16 hWaveOut,       /* [in] */
1309                                        SEGPTR lpsegWaveOutHdr,    /* [???] */
1310                                        UINT16 uSize)              /* [in] */
1311 {
1312     LPWINE_MLD          wmld;
1313     LPWAVEHDR           lpWaveOutHdr = MapSL(lpsegWaveOutHdr);
1314
1315     TRACE("(%04X, %08x, %u);\n", hWaveOut, lpsegWaveOutHdr, uSize);
1316
1317     if (!(lpWaveOutHdr->dwFlags & WHDR_PREPARED)) {
1318         return MMSYSERR_NOERROR;
1319     }
1320
1321     if ((wmld = MMDRV_Get(HWAVEOUT_32(hWaveOut), MMDRV_WAVEOUT, FALSE)) == NULL)
1322         return MMSYSERR_INVALHANDLE;
1323
1324     return MMDRV_Message(wmld, WODM_UNPREPARE, lpsegWaveOutHdr, uSize, FALSE);
1325 }
1326
1327 /**************************************************************************
1328  *                              waveOutWrite            [MMSYSTEM.408]
1329  */
1330 UINT16 WINAPI waveOutWrite16(HWAVEOUT16 hWaveOut,       /* [in] */
1331                              LPWAVEHDR lpsegWaveOutHdr, /* [???] NOTE: SEGPTR */
1332                              UINT16 uSize)              /* [in] */
1333 {
1334     LPWINE_MLD          wmld;
1335
1336     TRACE("(%04X, %p, %u);\n", hWaveOut, lpsegWaveOutHdr, uSize);
1337
1338     if ((wmld = MMDRV_Get(HWAVEOUT_32(hWaveOut), MMDRV_WAVEOUT, FALSE)) == NULL)
1339         return MMSYSERR_INVALHANDLE;
1340
1341     return MMDRV_Message(wmld, WODM_WRITE, (DWORD_PTR)lpsegWaveOutHdr, uSize, FALSE);
1342 }
1343
1344 /**************************************************************************
1345  *                              waveOutBreakLoop        [MMSYSTEM.419]
1346  */
1347 UINT16 WINAPI waveOutBreakLoop16(HWAVEOUT16 hWaveOut16)
1348 {
1349     DWORD       level;
1350     UINT16      ret;
1351
1352     ReleaseThunkLock(&level);
1353     ret = waveOutBreakLoop(HWAVEOUT_32(hWaveOut16));
1354     RestoreThunkLock(level);
1355     return ret;
1356 }
1357
1358 /**************************************************************************
1359  *                              waveOutPause            [MMSYSTEM.409]
1360  */
1361 UINT16 WINAPI waveOutPause16(HWAVEOUT16 hWaveOut16)
1362 {
1363     DWORD       level;
1364     UINT16      ret;
1365
1366     ReleaseThunkLock(&level);
1367     ret = waveOutPause(HWAVEOUT_32(hWaveOut16));
1368     RestoreThunkLock(level);
1369     return ret;
1370 }
1371
1372 /**************************************************************************
1373  *                              waveOutReset            [MMSYSTEM.411]
1374  */
1375 UINT16 WINAPI waveOutReset16(HWAVEOUT16 hWaveOut16)
1376 {
1377     DWORD       level;
1378     UINT16      ret;
1379
1380     ReleaseThunkLock(&level);
1381     ret = waveOutReset(HWAVEOUT_32(hWaveOut16));
1382     RestoreThunkLock(level);
1383     return ret;
1384 }
1385
1386 /**************************************************************************
1387  *                              waveOutRestart  [MMSYSTEM.410]
1388  */
1389 UINT16 WINAPI waveOutRestart16(HWAVEOUT16 hWaveOut16)
1390 {
1391     DWORD       level;
1392     UINT16      ret;
1393
1394     ReleaseThunkLock(&level);
1395     ret = waveOutRestart(HWAVEOUT_32(hWaveOut16));
1396     RestoreThunkLock(level);
1397     return ret;
1398 }
1399
1400 /**************************************************************************
1401  *                              waveOutGetPosition      [MMSYSTEM.412]
1402  */
1403 UINT16 WINAPI waveOutGetPosition16(HWAVEOUT16 hWaveOut, LPMMTIME16 lpTime,
1404                                    UINT16 uSize)
1405 {
1406     UINT        ret;
1407     MMTIME      mmt;
1408
1409     mmt.wType = lpTime->wType;
1410     ret = waveOutGetPosition(HWAVEOUT_32(hWaveOut), &mmt, sizeof(mmt));
1411     MMSYSTEM_MMTIME32to16(lpTime, &mmt);
1412     return ret;
1413 }
1414
1415 /**************************************************************************
1416  *                              waveOutGetPitch         [MMSYSTEM.413]
1417  */
1418 UINT16 WINAPI waveOutGetPitch16(HWAVEOUT16 hWaveOut16, LPDWORD lpdw)
1419 {
1420     return waveOutGetPitch(HWAVEOUT_32(hWaveOut16), lpdw);
1421 }
1422
1423 /**************************************************************************
1424  *                              waveOutSetPitch         [MMSYSTEM.414]
1425  */
1426 UINT16 WINAPI waveOutSetPitch16(HWAVEOUT16 hWaveOut16, DWORD dw)
1427 {
1428     return waveOutSetPitch(HWAVEOUT_32(hWaveOut16), dw);
1429 }
1430
1431 /**************************************************************************
1432  *                              waveOutGetPlaybackRate  [MMSYSTEM.417]
1433  */
1434 UINT16 WINAPI waveOutGetPlaybackRate16(HWAVEOUT16 hWaveOut16, LPDWORD lpdw)
1435 {
1436     return waveOutGetPlaybackRate(HWAVEOUT_32(hWaveOut16), lpdw);
1437 }
1438
1439 /**************************************************************************
1440  *                              waveOutSetPlaybackRate  [MMSYSTEM.418]
1441  */
1442 UINT16 WINAPI waveOutSetPlaybackRate16(HWAVEOUT16 hWaveOut16, DWORD dw)
1443 {
1444     return waveOutSetPlaybackRate(HWAVEOUT_32(hWaveOut16), dw);
1445 }
1446
1447 /**************************************************************************
1448  *                              waveOutGetVolume        [MMSYSTEM.415]
1449  */
1450 UINT16 WINAPI waveOutGetVolume16(UINT16 devid, LPDWORD lpdw)
1451 {
1452     return waveOutGetVolume(HWAVEOUT_32(devid), lpdw);
1453 }
1454
1455 /**************************************************************************
1456  *                              waveOutSetVolume        [MMSYSTEM.416]
1457  */
1458 UINT16 WINAPI waveOutSetVolume16(UINT16 devid, DWORD dw)
1459 {
1460     return waveOutSetVolume(HWAVEOUT_32(devid), dw);
1461 }
1462
1463 /**************************************************************************
1464  *                              waveOutGetID            [MMSYSTEM.420]
1465  */
1466 UINT16 WINAPI waveOutGetID16(HWAVEOUT16 hWaveOut, UINT16* lpuDeviceID)
1467 {
1468     LPWINE_MLD          wmld;
1469
1470     TRACE("(%04X, %p);\n", hWaveOut, lpuDeviceID);
1471
1472     if (lpuDeviceID == NULL) return MMSYSERR_INVALHANDLE;
1473
1474     if ((wmld = MMDRV_Get(HWAVEOUT_32(hWaveOut), MMDRV_WAVEOUT, FALSE)) == NULL)
1475         return MMSYSERR_INVALHANDLE;
1476
1477     *lpuDeviceID = wmld->uDeviceID;
1478     return 0;
1479 }
1480
1481 /**************************************************************************
1482  *                              waveOutMessage          [MMSYSTEM.421]
1483  */
1484 DWORD WINAPI waveOutMessage16(HWAVEOUT16 hWaveOut, UINT16 uMessage,
1485                               DWORD dwParam1, DWORD dwParam2)
1486 {
1487     LPWINE_MLD          wmld;
1488
1489     TRACE("(%04x, %u, %d, %d)\n", hWaveOut, uMessage, dwParam1, dwParam2);
1490
1491     if ((wmld = MMDRV_Get(HWAVEOUT_32(hWaveOut), MMDRV_WAVEOUT, FALSE)) == NULL) {
1492         if ((wmld = MMDRV_Get(HWAVEOUT_32(hWaveOut), MMDRV_WAVEOUT, TRUE)) != NULL) {
1493             if (uMessage == DRV_QUERYDRVENTRY || uMessage == DRV_QUERYDEVNODE)
1494                 dwParam1 = (DWORD)MapSL(dwParam1);
1495             return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
1496         }
1497         return MMSYSERR_INVALHANDLE;
1498     }
1499
1500     /* from M$ KB */
1501     if (uMessage < DRVM_IOCTL || (uMessage >= DRVM_IOCTL_LAST && uMessage < DRVM_MAPPER))
1502         return MMSYSERR_INVALPARAM;
1503
1504     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, FALSE);
1505 }
1506
1507 /**************************************************************************
1508  *                              waveInGetNumDevs                [MMSYSTEM.501]
1509  */
1510 UINT16 WINAPI waveInGetNumDevs16(void)
1511 {
1512     return MMDRV_GetNum(MMDRV_WAVEIN);
1513 }
1514
1515 /**************************************************************************
1516  *                              waveInGetDevCaps                [MMSYSTEM.502]
1517  */
1518 UINT16 WINAPI waveInGetDevCaps16(UINT16 uDeviceID, LPWAVEINCAPS16 lpCaps,
1519                                  UINT16 uSize)
1520 {
1521     WAVEINCAPSA wicA;
1522     UINT        ret;
1523
1524     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
1525
1526     ret = waveInGetDevCapsA(uDeviceID, &wicA, sizeof(wicA));
1527     if (ret == MMSYSERR_NOERROR) {
1528         WAVEINCAPS16 wic16;
1529         wic16.wMid           = wicA.wMid;
1530         wic16.wPid           = wicA.wPid;
1531         wic16.vDriverVersion = wicA.vDriverVersion;
1532         strcpy(wic16.szPname, wicA.szPname);
1533         wic16.dwFormats      = wicA.dwFormats;
1534         wic16.wChannels      = wicA.wChannels;
1535         memcpy(lpCaps, &wic16, min(uSize, sizeof(wic16)));
1536     }
1537     return ret;
1538 }
1539
1540 /**************************************************************************
1541  *                              waveInOpen                      [MMSYSTEM.504]
1542  */
1543 UINT16 WINAPI waveInOpen16(HWAVEIN16* lphWaveIn, UINT16 uDeviceID,
1544                            LPCWAVEFORMATEX lpFormat, DWORD dwCallback,
1545                            DWORD dwInstance, DWORD dwFlags)
1546 {
1547     HANDLE              hWaveIn;
1548     UINT                ret;
1549
1550     /* since layout of WAVEFORMATEX is the same for 16/32 bits, we directly
1551      * call the 32 bit version
1552      * however, we need to promote correctly the wave mapper id
1553      * (0xFFFFFFFF and not 0x0000FFFF)
1554      */
1555     ret = WAVE_Open(&hWaveIn, (uDeviceID == (UINT16)-1) ? (UINT)-1 : uDeviceID,
1556                     MMDRV_WAVEIN, lpFormat, dwCallback, dwInstance, dwFlags, FALSE);
1557
1558     if (lphWaveIn != NULL) *lphWaveIn = HWAVEIN_16(hWaveIn);
1559     return ret;
1560 }
1561
1562 /**************************************************************************
1563  *                              waveInClose                     [MMSYSTEM.505]
1564  */
1565 UINT16 WINAPI waveInClose16(HWAVEIN16 hWaveIn)
1566 {
1567     DWORD       level;
1568     UINT16      ret;
1569
1570     ReleaseThunkLock(&level);
1571     ret = waveInClose(HWAVEIN_32(hWaveIn));
1572     RestoreThunkLock(level);
1573     return ret;
1574 }
1575
1576 /**************************************************************************
1577  *                              waveInPrepareHeader             [MMSYSTEM.506]
1578  */
1579 UINT16 WINAPI waveInPrepareHeader16(HWAVEIN16 hWaveIn,       /* [in] */
1580                                     SEGPTR lpsegWaveInHdr,   /* [???] */
1581                                     UINT16 uSize)            /* [in] */
1582 {
1583     LPWINE_MLD          wmld;
1584     LPWAVEHDR           lpWaveInHdr = MapSL(lpsegWaveInHdr);
1585     UINT16              ret;
1586
1587     TRACE("(%04X, %p, %u);\n", hWaveIn, lpWaveInHdr, uSize);
1588
1589     if (lpWaveInHdr == NULL) return MMSYSERR_INVALHANDLE;
1590     if ((wmld = MMDRV_Get(HWAVEIN_32(hWaveIn), MMDRV_WAVEIN, FALSE)) == NULL)
1591         return MMSYSERR_INVALHANDLE;
1592
1593     lpWaveInHdr->dwBytesRecorded = 0;
1594
1595     ret = MMDRV_Message(wmld, WIDM_PREPARE, lpsegWaveInHdr, uSize, FALSE);
1596     return ret;
1597 }
1598
1599 /**************************************************************************
1600  *                              waveInUnprepareHeader   [MMSYSTEM.507]
1601  */
1602 UINT16 WINAPI waveInUnprepareHeader16(HWAVEIN16 hWaveIn,       /* [in] */
1603                                       SEGPTR lpsegWaveInHdr,   /* [???] */
1604                                       UINT16 uSize)            /* [in] */
1605 {
1606     LPWINE_MLD          wmld;
1607     LPWAVEHDR           lpWaveInHdr = MapSL(lpsegWaveInHdr);
1608
1609     TRACE("(%04X, %08x, %u);\n", hWaveIn, lpsegWaveInHdr, uSize);
1610
1611     if (lpWaveInHdr == NULL) return MMSYSERR_INVALPARAM;
1612
1613     if (!(lpWaveInHdr->dwFlags & WHDR_PREPARED)) {
1614         return MMSYSERR_NOERROR;
1615     }
1616
1617     if ((wmld = MMDRV_Get(HWAVEIN_32(hWaveIn), MMDRV_WAVEIN, FALSE)) == NULL)
1618         return MMSYSERR_INVALHANDLE;
1619
1620     return MMDRV_Message(wmld, WIDM_UNPREPARE, lpsegWaveInHdr, uSize, FALSE);
1621 }
1622
1623 /**************************************************************************
1624  *                              waveInAddBuffer         [MMSYSTEM.508]
1625  */
1626 UINT16 WINAPI waveInAddBuffer16(HWAVEIN16 hWaveIn,       /* [in] */
1627                                 WAVEHDR* lpsegWaveInHdr, /* [???] NOTE: SEGPTR */
1628                                 UINT16 uSize)            /* [in] */
1629 {
1630     LPWINE_MLD          wmld;
1631
1632     TRACE("(%04X, %p, %u);\n", hWaveIn, lpsegWaveInHdr, uSize);
1633
1634     if (lpsegWaveInHdr == NULL) return MMSYSERR_INVALPARAM;
1635     if ((wmld = MMDRV_Get(HWAVEIN_32(hWaveIn), MMDRV_WAVEIN, FALSE)) == NULL)
1636         return MMSYSERR_INVALHANDLE;
1637
1638     return MMDRV_Message(wmld, WIDM_ADDBUFFER, (DWORD_PTR)lpsegWaveInHdr, uSize, FALSE);
1639 }
1640
1641 /**************************************************************************
1642  *                              waveInReset             [MMSYSTEM.511]
1643  */
1644 UINT16 WINAPI waveInReset16(HWAVEIN16 hWaveIn16)
1645 {
1646     DWORD       level;
1647     UINT16      ret;
1648
1649     ReleaseThunkLock(&level);
1650     ret = waveInReset(HWAVEIN_32(hWaveIn16));
1651     RestoreThunkLock(level);
1652     return ret;
1653 }
1654
1655 /**************************************************************************
1656  *                              waveInStart             [MMSYSTEM.509]
1657  */
1658 UINT16 WINAPI waveInStart16(HWAVEIN16 hWaveIn16)
1659 {
1660     DWORD       level;
1661     UINT16      ret;
1662
1663     ReleaseThunkLock(&level);
1664     ret = waveInStart(HWAVEIN_32(hWaveIn16));
1665     RestoreThunkLock(level);
1666     return ret;
1667 }
1668
1669 /**************************************************************************
1670  *                              waveInStop              [MMSYSTEM.510]
1671  */
1672 UINT16 WINAPI waveInStop16(HWAVEIN16 hWaveIn16)
1673 {
1674     DWORD       level;
1675     UINT16      ret;
1676
1677     ReleaseThunkLock(&level);
1678     ret = waveInStop(HWAVEIN_32(hWaveIn16));
1679     RestoreThunkLock(level);
1680     return ret;
1681 }
1682
1683 /**************************************************************************
1684  *                              waveInGetPosition       [MMSYSTEM.512]
1685  */
1686 UINT16 WINAPI waveInGetPosition16(HWAVEIN16 hWaveIn, LPMMTIME16 lpTime,
1687                                   UINT16 uSize)
1688 {
1689     UINT        ret;
1690     MMTIME      mmt;
1691
1692     mmt.wType = lpTime->wType;
1693     ret = waveInGetPosition(HWAVEIN_32(hWaveIn), &mmt, sizeof(mmt));
1694     MMSYSTEM_MMTIME32to16(lpTime, &mmt);
1695     return ret;
1696 }
1697
1698 /**************************************************************************
1699  *                              waveInGetID                     [MMSYSTEM.513]
1700  */
1701 UINT16 WINAPI waveInGetID16(HWAVEIN16 hWaveIn, UINT16* lpuDeviceID)
1702 {
1703     LPWINE_MLD          wmld;
1704
1705     TRACE("(%04X, %p);\n", hWaveIn, lpuDeviceID);
1706
1707     if (lpuDeviceID == NULL) return MMSYSERR_INVALHANDLE;
1708
1709     if ((wmld = MMDRV_Get(HWAVEIN_32(hWaveIn), MMDRV_WAVEIN, FALSE)) == NULL)
1710         return MMSYSERR_INVALHANDLE;
1711
1712     *lpuDeviceID = wmld->uDeviceID;
1713     return MMSYSERR_NOERROR;
1714 }
1715
1716 /**************************************************************************
1717  *                              waveInMessage           [MMSYSTEM.514]
1718  */
1719 DWORD WINAPI waveInMessage16(HWAVEIN16 hWaveIn, UINT16 uMessage,
1720                              DWORD dwParam1, DWORD dwParam2)
1721 {
1722     LPWINE_MLD          wmld;
1723
1724     TRACE("(%04x, %u, %d, %d)\n", hWaveIn, uMessage, dwParam1, dwParam2);
1725
1726     if ((wmld = MMDRV_Get(HWAVEIN_32(hWaveIn), MMDRV_WAVEIN, FALSE)) == NULL) {
1727         if ((wmld = MMDRV_Get(HWAVEIN_32(hWaveIn), MMDRV_WAVEIN, TRUE)) != NULL) {
1728             if (uMessage == DRV_QUERYDRVENTRY || uMessage == DRV_QUERYDEVNODE)
1729                 dwParam1 = (DWORD)MapSL(dwParam1);
1730             return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
1731         }
1732         return MMSYSERR_INVALHANDLE;
1733     }
1734
1735     /* from M$ KB */
1736     if (uMessage < DRVM_IOCTL || (uMessage >= DRVM_IOCTL_LAST && uMessage < DRVM_MAPPER))
1737         return MMSYSERR_INVALPARAM;
1738
1739     return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2, FALSE);
1740 }
1741
1742 /* ###################################################
1743  * #                     TASK                        #
1744  * ###################################################
1745  */
1746
1747 /*#define USE_MM_TSK_WINE*/
1748
1749 /**************************************************************************
1750  *                              mmTaskCreate            [MMSYSTEM.900]
1751  *
1752  * Creates a 16 bit MM task. It's entry point is lpFunc, and it should be
1753  * called upon creation with dwPmt as parameter.
1754  */
1755 HINSTANCE16 WINAPI mmTaskCreate16(SEGPTR spProc, HINSTANCE16 *lphMmTask, DWORD dwPmt)
1756 {
1757     HINSTANCE16         ret;
1758     HINSTANCE16         handle;
1759     char cmdline[16];
1760     DWORD showCmd = 0x40002;
1761     LOADPARAMS16 lp;
1762
1763     TRACE("(%08x, %p, %08x);\n", spProc, lphMmTask, dwPmt);
1764     /* This to work requires NE modules to be started with a binary command line
1765      * which is not currently the case. A patch exists but has never been committed.
1766      * A workaround would be to integrate code for mmtask.tsk into Wine, but
1767      * this requires tremendous work (starting with patching tools/build to
1768      * create NE executables (and not only DLLs) for builtins modules.
1769      * EP 99/04/25
1770      */
1771     FIXME("This is currently broken. It will fail\n");
1772
1773     cmdline[0] = 0x0d;
1774     *(LPDWORD)(cmdline + 1) = (DWORD)spProc;
1775     *(LPDWORD)(cmdline + 5) = dwPmt;
1776     *(LPDWORD)(cmdline + 9) = 0;
1777
1778     lp.hEnvironment = 0;
1779     lp.cmdLine = MapLS(cmdline);
1780     lp.showCmd = MapLS(&showCmd);
1781     lp.reserved = 0;
1782
1783 #ifndef USE_MM_TSK_WINE
1784     handle = LoadModule16("c:\\windows\\system\\mmtask.tsk", &lp);
1785 #else
1786     handle = LoadModule16("mmtask.tsk", &lp);
1787 #endif
1788     if (handle < 32) {
1789         ret = (handle) ? 1 : 2;
1790         handle = 0;
1791     } else {
1792         ret = 0;
1793     }
1794     if (lphMmTask)
1795         *lphMmTask = handle;
1796
1797     UnMapLS( lp.cmdLine );
1798     UnMapLS( lp.showCmd );
1799     TRACE("=> 0x%04x/%d\n", handle, ret);
1800     return ret;
1801 }
1802
1803 #ifdef USE_MM_TSK_WINE
1804 /* C equivalent to mmtask.tsk binary content */
1805 void    mmTaskEntryPoint16(LPSTR cmdLine, WORD di, WORD si)
1806 {
1807     int len = cmdLine[0x80];
1808
1809     if (len / 2 == 6) {
1810         void    (*fpProc)(DWORD) = MapSL(*((DWORD*)(cmdLine + 1)));
1811         DWORD   dwPmt  = *((DWORD*)(cmdLine + 5));
1812
1813 #if 0
1814         InitTask16(); /* FIXME: pmts / from context ? */
1815         InitApp(di);
1816 #endif
1817         if (SetMessageQueue16(0x40)) {
1818             WaitEvent16(0);
1819             if (HIWORD(fpProc)) {
1820                 OldYield16();
1821 /* EPP          StackEnter16(); */
1822                 (fpProc)(dwPmt);
1823             }
1824         }
1825     }
1826     OldYield16();
1827     OldYield16();
1828     OldYield16();
1829     ExitProcess(0);
1830 }
1831 #endif
1832
1833 /**************************************************************************
1834  *                              mmTaskBlock             [MMSYSTEM.902]
1835  */
1836 void WINAPI mmTaskBlock16(HINSTANCE16 hInst)
1837 {
1838     MSG         msg;
1839
1840     do {
1841         GetMessageA(&msg, 0, 0, 0);
1842         if (msg.hwnd) {
1843             TranslateMessage(&msg);
1844             DispatchMessageA(&msg);
1845         }
1846     } while (msg.message < 0x3A0);
1847 }
1848
1849 /**************************************************************************
1850  *                              mmTaskSignal            [MMSYSTEM.903]
1851  */
1852 LRESULT WINAPI mmTaskSignal16(HTASK16 ht)
1853 {
1854     TRACE("(%04x);\n", ht);
1855     return PostThreadMessageW( HTASK_32(ht), WM_USER, 0, 0 );
1856 }
1857
1858 /**************************************************************************
1859  *                              mmGetCurrentTask        [MMSYSTEM.904]
1860  */
1861 HTASK16 WINAPI mmGetCurrentTask16(void)
1862 {
1863     return GetCurrentTask();
1864 }
1865
1866 /**************************************************************************
1867  *                              mmTaskYield             [MMSYSTEM.905]
1868  */
1869 void    WINAPI  mmTaskYield16(void)
1870 {
1871     MSG         msg;
1872
1873     if (PeekMessageA(&msg, 0, 0, 0, 0)) {
1874         WOWYield16();
1875     }
1876 }
1877
1878 extern DWORD    WINAPI  GetProcessFlags(DWORD);
1879
1880 /******************************************************************
1881  *              WINMM_GetmmThread
1882  *
1883  *
1884  */
1885 static  WINE_MMTHREAD*  WINMM_GetmmThread(HANDLE16 h)
1886 {
1887     return MapSL(MAKESEGPTR(h, 0));
1888 }
1889
1890 DWORD WINAPI WINE_mmThreadEntryPoint(LPVOID);
1891
1892 /**************************************************************************
1893  *                              mmThreadCreate          [MMSYSTEM.1120]
1894  *
1895  * undocumented
1896  * Creates a MM thread, calling fpThreadAddr(dwPmt).
1897  * dwFlags:
1898  *      bit.0 set means create a 16 bit task instead of thread calling a 16 bit proc
1899  *      bit.1 set means to open a VxD for this thread (unsupported)
1900  */
1901 LRESULT WINAPI mmThreadCreate16(FARPROC16 fpThreadAddr, LPHANDLE16 lpHndl, DWORD dwPmt, DWORD dwFlags)
1902 {
1903     HANDLE16            hndl;
1904     LRESULT             ret;
1905
1906     TRACE("(%p, %p, %08x, %08x)!\n", fpThreadAddr, lpHndl, dwPmt, dwFlags);
1907
1908     hndl = GlobalAlloc16(sizeof(WINE_MMTHREAD), GMEM_SHARE|GMEM_ZEROINIT);
1909
1910     if (hndl == 0) {
1911         ret = 2;
1912     } else {
1913         WINE_MMTHREAD*  lpMMThd = WINMM_GetmmThread(hndl);
1914
1915 #if 0
1916         /* force mmtask routines even if mmthread is required */
1917         /* this will work only if the patch about binary cmd line and NE tasks
1918          * is committed
1919          */
1920         dwFlags |= 1;
1921 #endif
1922
1923         lpMMThd->dwSignature    = WINE_MMTHREAD_CREATED;
1924         lpMMThd->dwCounter      = 0;
1925         lpMMThd->hThread        = 0;
1926         lpMMThd->dwThreadID     = 0;
1927         lpMMThd->fpThread       = (DWORD)fpThreadAddr;
1928         lpMMThd->dwThreadPmt    = dwPmt;
1929         lpMMThd->dwSignalCount  = 0;
1930         lpMMThd->hEvent         = 0;
1931         lpMMThd->hVxD           = 0;
1932         lpMMThd->dwStatus       = 0;
1933         lpMMThd->dwFlags        = dwFlags;
1934         lpMMThd->hTask          = 0;
1935
1936         if ((dwFlags & 1) == 0 && (GetProcessFlags(GetCurrentThreadId()) & 8) == 0) {
1937             lpMMThd->hEvent = CreateEventW(NULL, FALSE, TRUE, NULL);
1938
1939             TRACE("Let's go crazy... trying new MM thread. lpMMThd=%p\n", lpMMThd);
1940             if (lpMMThd->dwFlags & 2) {
1941                 /* as long as we don't support MM VxD in wine, we don't need
1942                  * to care about this flag
1943                  */
1944                 /* FIXME("Don't know how to properly open VxD handles\n"); */
1945                 /* lpMMThd->hVxD = OpenVxDHandle(lpMMThd->hEvent); */
1946             }
1947
1948             lpMMThd->hThread = CreateThread(0, 0, WINE_mmThreadEntryPoint,
1949                                             (LPVOID)(DWORD_PTR)hndl, CREATE_SUSPENDED, &lpMMThd->dwThreadID);
1950             if (lpMMThd->hThread == 0) {
1951                 WARN("Couldn't create thread\n");
1952                 /* clean-up(VxDhandle...); devicedirectio... */
1953                 if (lpMMThd->hEvent != 0)
1954                     CloseHandle(lpMMThd->hEvent);
1955                 ret = 2;
1956             } else {
1957                 SetThreadPriority(lpMMThd->hThread, THREAD_PRIORITY_TIME_CRITICAL);
1958                 TRACE("Got a nice thread hndl=%p id=0x%08x\n", lpMMThd->hThread, lpMMThd->dwThreadID);
1959                 ret = 0;
1960             }
1961         } else {
1962             /* get WINE_mmThreadEntryPoint()
1963              * 2047 is its ordinal in mmsystem.spec
1964              */
1965             FARPROC16   fp = GetProcAddress16(GetModuleHandle16("MMSYSTEM"), (LPCSTR)2047);
1966
1967             TRACE("farproc seg=0x%p lin=%p\n", fp, MapSL((SEGPTR)fp));
1968
1969             ret = (fp == 0) ? 2 : mmTaskCreate16((DWORD)fp, 0, hndl);
1970         }
1971
1972         if (ret == 0) {
1973             if (lpMMThd->hThread && !ResumeThread(lpMMThd->hThread))
1974                 WARN("Couldn't resume thread\n");
1975
1976             while (lpMMThd->dwStatus != 0x10) { /* test also HIWORD of dwStatus */
1977                 UserYield16();
1978             }
1979         }
1980     }
1981
1982     if (ret != 0) {
1983         GlobalFree16(hndl);
1984         hndl = 0;
1985     }
1986
1987     if (lpHndl)
1988         *lpHndl = hndl;
1989
1990     TRACE("ok => %ld\n", ret);
1991     return ret;
1992 }
1993
1994 /**************************************************************************
1995  *                              mmThreadSignal          [MMSYSTEM.1121]
1996  */
1997 void WINAPI mmThreadSignal16(HANDLE16 hndl)
1998 {
1999     TRACE("(%04x)!\n", hndl);
2000
2001     if (hndl) {
2002         WINE_MMTHREAD*  lpMMThd = WINMM_GetmmThread(hndl);
2003
2004         lpMMThd->dwCounter++;
2005         if (lpMMThd->hThread != 0) {
2006             InterlockedIncrement(&lpMMThd->dwSignalCount);
2007             SetEvent(lpMMThd->hEvent);
2008         } else {
2009             mmTaskSignal16(lpMMThd->hTask);
2010         }
2011         lpMMThd->dwCounter--;
2012     }
2013 }
2014
2015 static  void    MMSYSTEM_ThreadBlock(WINE_MMTHREAD* lpMMThd)
2016 {
2017     MSG         msg;
2018     DWORD       ret;
2019
2020     if (lpMMThd->dwThreadID != GetCurrentThreadId())
2021         ERR("Not called by thread itself\n");
2022
2023     for (;;) {
2024         ResetEvent(lpMMThd->hEvent);
2025         if (InterlockedDecrement(&lpMMThd->dwSignalCount) >= 0)
2026             break;
2027         InterlockedIncrement(&lpMMThd->dwSignalCount);
2028
2029         TRACE("S1\n");
2030
2031         ret = MsgWaitForMultipleObjects(1, &lpMMThd->hEvent, FALSE, INFINITE, QS_ALLINPUT);
2032         switch (ret) {
2033         case WAIT_OBJECT_0:     /* Event */
2034             TRACE("S2.1\n");
2035             break;
2036         case WAIT_OBJECT_0 + 1: /* Msg */
2037             TRACE("S2.2\n");
2038             if (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
2039                 TranslateMessage(&msg);
2040                 DispatchMessageA(&msg);
2041             }
2042             break;
2043         default:
2044             WARN("S2.x unsupported ret val 0x%08x\n", ret);
2045         }
2046         TRACE("S3\n");
2047     }
2048 }
2049
2050 /**************************************************************************
2051  *                              mmThreadBlock           [MMSYSTEM.1122]
2052  */
2053 void    WINAPI mmThreadBlock16(HANDLE16 hndl)
2054 {
2055     TRACE("(%04x)!\n", hndl);
2056
2057     if (hndl) {
2058         WINE_MMTHREAD*  lpMMThd = WINMM_GetmmThread(hndl);
2059
2060         if (lpMMThd->hThread != 0) {
2061             DWORD       lc;
2062
2063             ReleaseThunkLock(&lc);
2064             MMSYSTEM_ThreadBlock(lpMMThd);
2065             RestoreThunkLock(lc);
2066         } else {
2067             mmTaskBlock16(lpMMThd->hTask);
2068         }
2069     }
2070     TRACE("done\n");
2071 }
2072
2073 /**************************************************************************
2074  *                              mmThreadIsCurrent       [MMSYSTEM.1123]
2075  */
2076 BOOL16  WINAPI mmThreadIsCurrent16(HANDLE16 hndl)
2077 {
2078     BOOL16              ret = FALSE;
2079
2080     TRACE("(%04x)!\n", hndl);
2081
2082     if (hndl && mmThreadIsValid16(hndl)) {
2083         WINE_MMTHREAD*  lpMMThd = WINMM_GetmmThread(hndl);
2084         ret = (GetCurrentThreadId() == lpMMThd->dwThreadID);
2085     }
2086     TRACE("=> %d\n", ret);
2087     return ret;
2088 }
2089
2090 /**************************************************************************
2091  *                              mmThreadIsValid         [MMSYSTEM.1124]
2092  */
2093 BOOL16  WINAPI  mmThreadIsValid16(HANDLE16 hndl)
2094 {
2095     BOOL16              ret = FALSE;
2096
2097     TRACE("(%04x)!\n", hndl);
2098
2099     if (hndl) {
2100         WINE_MMTHREAD*  lpMMThd = WINMM_GetmmThread(hndl);
2101
2102         if (!IsBadWritePtr(lpMMThd, sizeof(WINE_MMTHREAD)) &&
2103             lpMMThd->dwSignature == WINE_MMTHREAD_CREATED &&
2104             IsTask16(lpMMThd->hTask)) {
2105             lpMMThd->dwCounter++;
2106             if (lpMMThd->hThread != 0) {
2107                 DWORD   dwThreadRet;
2108                 if (GetExitCodeThread(lpMMThd->hThread, &dwThreadRet) &&
2109                     dwThreadRet == STATUS_PENDING) {
2110                     ret = TRUE;
2111                 }
2112             } else {
2113                 ret = TRUE;
2114             }
2115             lpMMThd->dwCounter--;
2116         }
2117     }
2118     TRACE("=> %d\n", ret);
2119     return ret;
2120 }
2121
2122 /**************************************************************************
2123  *                              mmThreadGetTask         [MMSYSTEM.1125]
2124  */
2125 HANDLE16 WINAPI mmThreadGetTask16(HANDLE16 hndl)
2126 {
2127     HANDLE16    ret = 0;
2128
2129     TRACE("(%04x)\n", hndl);
2130
2131     if (mmThreadIsValid16(hndl)) {
2132         WINE_MMTHREAD*  lpMMThd = WINMM_GetmmThread(hndl);
2133         ret = lpMMThd->hTask;
2134     }
2135     return ret;
2136 }
2137
2138 /**************************************************************************
2139  *                              __wine_mmThreadEntryPoint (MMSYSTEM.2047)
2140  */
2141 DWORD WINAPI WINE_mmThreadEntryPoint(LPVOID p)
2142 {
2143     HANDLE16            hndl = (HANDLE16)(DWORD_PTR)p;
2144     WINE_MMTHREAD*      lpMMThd = WINMM_GetmmThread(hndl);
2145
2146     TRACE("(%04x %p)\n", hndl, lpMMThd);
2147
2148     lpMMThd->hTask = LOWORD(GetCurrentTask());
2149     TRACE("[10-%p] setting hTask to 0x%08x\n", lpMMThd->hThread, lpMMThd->hTask);
2150     lpMMThd->dwStatus = 0x10;
2151     MMSYSTEM_ThreadBlock(lpMMThd);
2152     TRACE("[20-%p]\n", lpMMThd->hThread);
2153     lpMMThd->dwStatus = 0x20;
2154     if (lpMMThd->fpThread) {
2155         WOWCallback16(lpMMThd->fpThread, lpMMThd->dwThreadPmt);
2156     }
2157     lpMMThd->dwStatus = 0x30;
2158     TRACE("[30-%p]\n", lpMMThd->hThread);
2159     while (lpMMThd->dwCounter) {
2160         Sleep(1);
2161         /* WOWYield16();*/
2162     }
2163     TRACE("[XX-%p]\n", lpMMThd->hThread);
2164     /* paranoia */
2165     lpMMThd->dwSignature = WINE_MMTHREAD_DELETED;
2166     /* close lpMMThread->hVxD directIO */
2167     if (lpMMThd->hEvent)
2168         CloseHandle(lpMMThd->hEvent);
2169     GlobalFree16(hndl);
2170     TRACE("done\n");
2171
2172     return 0;
2173 }
2174
2175 typedef BOOL16 (WINAPI *MMCPLCALLBACK)(HWND, LPCSTR, LPCSTR, LPCSTR);
2176
2177 /**************************************************************************
2178  *                      mmShowMMCPLPropertySheet        [MMSYSTEM.1150]
2179  */
2180 BOOL16  WINAPI  mmShowMMCPLPropertySheet16(HWND hWnd, LPCSTR lpStrDevice,
2181                                            LPCSTR lpStrTab, LPCSTR lpStrTitle)
2182 {
2183     HANDLE      hndl;
2184     BOOL16      ret = FALSE;
2185
2186     TRACE("(%p \"%s\" \"%s\" \"%s\")\n", hWnd, lpStrDevice, lpStrTab, lpStrTitle);
2187
2188     hndl = LoadLibraryA("MMSYS.CPL");
2189     if (hndl != 0) {
2190         MMCPLCALLBACK   fp = (MMCPLCALLBACK)GetProcAddress(hndl, "ShowMMCPLPropertySheet");
2191         if (fp != NULL) {
2192             DWORD       lc;
2193             ReleaseThunkLock(&lc);
2194             ret = (fp)(hWnd, lpStrDevice, lpStrTab, lpStrTitle);
2195             RestoreThunkLock(lc);
2196         }
2197         FreeLibrary(hndl);
2198     }
2199
2200     return ret;
2201 }
2202
2203 /**************************************************************************
2204  *                      StackEnter              [MMSYSTEM.32]
2205  */
2206 void WINAPI StackEnter16(void)
2207 {
2208 #ifdef __i386__
2209     /* mmsystem.dll from Win 95 does only this: so does Wine */
2210     __asm__("stc");
2211 #endif
2212 }
2213
2214 /**************************************************************************
2215  *                      StackLeave              [MMSYSTEM.33]
2216  */
2217 void WINAPI StackLeave16(void)
2218 {
2219 #ifdef __i386__
2220     /* mmsystem.dll from Win 95 does only this: so does Wine */
2221     __asm__("stc");
2222 #endif
2223 }
2224
2225 /**************************************************************************
2226  *                      WMMMidiRunOnce          [MMSYSTEM.8]
2227  */
2228 void WINAPI WMMMidiRunOnce16(void)
2229 {
2230     FIXME("(), stub!\n");
2231 }
2232
2233 /* ###################################################
2234  * #                    DRIVER                       #
2235  * ###################################################
2236  */
2237
2238 /**************************************************************************
2239  *                              DRIVER_MapMsg32To16             [internal]
2240  *
2241  * Map a 32 bit driver message to a 16 bit driver message.
2242  */
2243 static WINMM_MapType DRIVER_MapMsg32To16(WORD wMsg, LPARAM *lParam1, LPARAM *lParam2)
2244 {
2245     WINMM_MapType       ret = WINMM_MAP_MSGERROR;
2246
2247     switch (wMsg) {
2248     case DRV_LOAD:
2249     case DRV_ENABLE:
2250     case DRV_DISABLE:
2251     case DRV_FREE:
2252     case DRV_QUERYCONFIGURE:
2253     case DRV_REMOVE:
2254     case DRV_EXITSESSION:
2255     case DRV_EXITAPPLICATION:
2256     case DRV_POWER:
2257     case DRV_CLOSE:     /* should be 0/0 */
2258     case DRV_OPEN:      /* pass through */
2259         /* lParam1 and lParam2 are not used */
2260         ret = WINMM_MAP_OK;
2261         break;
2262     case DRV_CONFIGURE:
2263     case DRV_INSTALL:
2264         /* lParam1 is a handle to a window (conf) or to a driver (inst) or not used,
2265          * lParam2 is a pointer to DRVCONFIGINFO
2266          */
2267         if (*lParam2) {
2268             LPDRVCONFIGINFO16 dci16 = HeapAlloc( GetProcessHeap(), 0, sizeof(*dci16) );
2269             LPDRVCONFIGINFO     dci32 = (LPDRVCONFIGINFO)(*lParam2);
2270
2271             if (dci16) {
2272                 LPSTR str1 = NULL,str2;
2273                 INT len;
2274                 dci16->dwDCISize = sizeof(DRVCONFIGINFO16);
2275
2276                 if (dci32->lpszDCISectionName) {
2277                     len = WideCharToMultiByte( CP_ACP, 0, dci32->lpszDCISectionName, -1, NULL, 0, NULL, NULL );
2278                     str1 = HeapAlloc( GetProcessHeap(), 0, len );
2279                     if (str1) {
2280                         WideCharToMultiByte( CP_ACP, 0, dci32->lpszDCISectionName, -1, str1, len, NULL, NULL );
2281                         dci16->lpszDCISectionName = MapLS( str1 );
2282                     } else {
2283                         HeapFree( GetProcessHeap(), 0, dci16);
2284                         return WINMM_MAP_NOMEM;
2285                     }
2286                 } else {
2287                     dci16->lpszDCISectionName = 0L;
2288                 }
2289                 if (dci32->lpszDCIAliasName) {
2290                     len = WideCharToMultiByte( CP_ACP, 0, dci32->lpszDCIAliasName, -1, NULL, 0, NULL, NULL );
2291                     str2 = HeapAlloc( GetProcessHeap(), 0, len );
2292                     if (str2) {
2293                         WideCharToMultiByte( CP_ACP, 0, dci32->lpszDCIAliasName, -1, str2, len, NULL, NULL );
2294                         dci16->lpszDCIAliasName = MapLS( str2 );
2295                     } else {
2296                         HeapFree( GetProcessHeap(), 0, str1);
2297                         HeapFree( GetProcessHeap(), 0, dci16);
2298                         return WINMM_MAP_NOMEM;
2299                     }
2300                 } else {
2301                     dci16->lpszDCISectionName = 0L;
2302                 }
2303             } else {
2304                 return WINMM_MAP_NOMEM;
2305             }
2306             *lParam2 = MapLS( dci16 );
2307             ret = WINMM_MAP_OKMEM;
2308         } else {
2309             ret = WINMM_MAP_OK;
2310         }
2311         break;
2312     default:
2313         if (!((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100))) {
2314            FIXME("Unknown message 0x%04x\n", wMsg);
2315         }
2316         ret = WINMM_MAP_OK;
2317     }
2318     return ret;
2319 }
2320
2321 /**************************************************************************
2322  *                              DRIVER_UnMapMsg32To16           [internal]
2323  *
2324  * UnMap a 32 bit driver message to a 16 bit driver message.
2325  */
2326 static WINMM_MapType DRIVER_UnMapMsg32To16(WORD wMsg, DWORD lParam1, DWORD lParam2)
2327 {
2328     WINMM_MapType       ret = WINMM_MAP_MSGERROR;
2329
2330     switch (wMsg) {
2331     case DRV_LOAD:
2332     case DRV_ENABLE:
2333     case DRV_DISABLE:
2334     case DRV_FREE:
2335     case DRV_QUERYCONFIGURE:
2336     case DRV_REMOVE:
2337     case DRV_EXITSESSION:
2338     case DRV_EXITAPPLICATION:
2339     case DRV_POWER:
2340     case DRV_OPEN:
2341     case DRV_CLOSE:
2342         /* lParam1 and lParam2 are not used */
2343         break;
2344     case DRV_CONFIGURE:
2345     case DRV_INSTALL:
2346         /* lParam1 is a handle to a window (or not used), lParam2 is a pointer to DRVCONFIGINFO, lParam2 */
2347         if (lParam2) {
2348             LPDRVCONFIGINFO16   dci16 = MapSL(lParam2);
2349             HeapFree( GetProcessHeap(), 0, MapSL(dci16->lpszDCISectionName) );
2350             HeapFree( GetProcessHeap(), 0, MapSL(dci16->lpszDCIAliasName) );
2351             UnMapLS( lParam2 );
2352             UnMapLS( dci16->lpszDCISectionName );
2353             UnMapLS( dci16->lpszDCIAliasName );
2354             HeapFree( GetProcessHeap(), 0, dci16 );
2355         }
2356         ret = WINMM_MAP_OK;
2357         break;
2358     default:
2359         if (!((wMsg >= 0x800 && wMsg < 0x900) || (wMsg >= 0x4000 && wMsg < 0x4100))) {
2360             FIXME("Unknown message 0x%04x\n", wMsg);
2361         }
2362         ret = WINMM_MAP_OK;
2363     }
2364     return ret;
2365 }
2366
2367 /**************************************************************************
2368  *                              DRIVER_TryOpenDriver16          [internal]
2369  *
2370  * Tries to load a 16 bit driver whose DLL's (module) name is lpFileName.
2371  */
2372 static  LPWINE_DRIVER   DRIVER_OpenDriver16(LPCWSTR fn, LPCWSTR sn, LPARAM lParam2)
2373 {
2374     LPWINE_DRIVER       lpDrv = NULL;
2375     LPCSTR              cause = NULL;
2376     LPSTR               fnA = NULL, snA = NULL;
2377     unsigned            len;
2378
2379     TRACE("(%s, %s, %08lX);\n", debugstr_w(fn), debugstr_w(sn), lParam2);
2380
2381     lpDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_DRIVER));
2382     if (lpDrv == NULL) {cause = "OOM"; goto exit;}
2383
2384     if (fn)
2385     {
2386         len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
2387         fnA = HeapAlloc(GetProcessHeap(), 0, len);
2388         if (fnA == NULL) {cause = "OOM"; goto exit;}
2389         WideCharToMultiByte( CP_ACP, 0, fn, -1, fnA, len, NULL, NULL );
2390     }
2391
2392     if (sn)
2393     {
2394         len = WideCharToMultiByte( CP_ACP, 0, sn, -1, NULL, 0, NULL, NULL );
2395         snA = HeapAlloc(GetProcessHeap(), 0, len);
2396         if (snA == NULL) {cause = "OOM"; goto exit;}
2397         WideCharToMultiByte( CP_ACP, 0, sn, -1, snA, len, NULL, NULL );
2398     }
2399
2400     /* FIXME: shall we do some black magic here on sn ?
2401      *  drivers32 => drivers
2402      *  mci32 => mci
2403      * ...
2404      */
2405     lpDrv->d.d16.hDriver16 = OpenDriver16(fnA, snA, lParam2);
2406     if (lpDrv->d.d16.hDriver16 == 0) {cause = "Not a 16 bit driver"; goto exit;}
2407     lpDrv->dwFlags = WINE_GDF_16BIT;
2408
2409 exit:
2410     HeapFree(GetProcessHeap(), 0, fnA);
2411     HeapFree(GetProcessHeap(), 0, snA);
2412
2413     if (cause)
2414     {
2415         TRACE("Unable to load 16 bit module %s[%s]: %s\n",
2416             debugstr_w(fn), debugstr_w(sn), cause);
2417         HeapFree(GetProcessHeap(), 0, lpDrv);
2418         return NULL;
2419     }
2420
2421     TRACE("=> %p\n", lpDrv);
2422     return lpDrv;
2423 }
2424
2425 /******************************************************************
2426  *              DRIVER_SendMessage16
2427  *
2428  *
2429  */
2430 static LRESULT  DRIVER_SendMessage16(HDRVR16 hDrv16, UINT msg, 
2431                                      LPARAM lParam1, LPARAM lParam2)
2432 {
2433     LRESULT             ret = 0;
2434     WINMM_MapType       map;
2435
2436     TRACE("Before sdm16 call hDrv=%04x wMsg=%04x p1=%08lx p2=%08lx\n",
2437           hDrv16, msg, lParam1, lParam2);
2438
2439     switch (map = DRIVER_MapMsg32To16(msg, &lParam1, &lParam2)) {
2440     case WINMM_MAP_OKMEM:
2441     case WINMM_MAP_OK:
2442         ret = SendDriverMessage16(hDrv16, msg, lParam1, lParam2);
2443         if (map == WINMM_MAP_OKMEM)
2444             DRIVER_UnMapMsg32To16(msg, lParam1, lParam2);
2445     default:
2446         break;
2447     }
2448     return ret;
2449 }
2450
2451 /******************************************************************
2452  *              DRIVER_CloseDriver16
2453  *
2454  *
2455  */
2456 static LRESULT DRIVER_CloseDriver16(HDRVR16 hDrv16, LPARAM lParam1, LPARAM lParam2)
2457 {
2458     return CloseDriver16(hDrv16, lParam1, lParam2);
2459 }
2460
2461 /**************************************************************************
2462  *                              DrvOpen                 [MMSYSTEM.1100]
2463  */
2464 HDRVR16 WINAPI DrvOpen16(LPSTR lpDriverName, LPSTR lpSectionName, LPARAM lParam)
2465 {
2466     return OpenDriver16(lpDriverName, lpSectionName, lParam);
2467 }
2468
2469 /**************************************************************************
2470  *                              DrvClose                [MMSYSTEM.1101]
2471  */
2472 LRESULT WINAPI DrvClose16(HDRVR16 hDrv, LPARAM lParam1, LPARAM lParam2)
2473 {
2474     return CloseDriver16(hDrv, lParam1, lParam2);
2475 }
2476
2477 /**************************************************************************
2478  *                              DrvSendMessage          [MMSYSTEM.1102]
2479  */
2480 LRESULT WINAPI DrvSendMessage16(HDRVR16 hDrv, WORD msg, LPARAM lParam1,
2481                                 LPARAM lParam2)
2482 {
2483     return SendDriverMessage16(hDrv, msg, lParam1, lParam2);
2484 }
2485
2486 /**************************************************************************
2487  *                              DrvGetModuleHandle      [MMSYSTEM.1103]
2488  */
2489 HANDLE16 WINAPI DrvGetModuleHandle16(HDRVR16 hDrv)
2490 {
2491     return GetDriverModuleHandle16(hDrv);
2492 }
2493
2494 /**************************************************************************
2495  *                              DrvDefDriverProc        [MMSYSTEM.1104]
2496  */
2497 LRESULT WINAPI DrvDefDriverProc16(DWORD dwDriverID, HDRVR16 hDrv, WORD wMsg,
2498                                   DWORD dwParam1, DWORD dwParam2)
2499 {
2500     return DefDriverProc16(dwDriverID, hDrv, wMsg, dwParam1, dwParam2);
2501 }
2502
2503 /**************************************************************************
2504  *                              DriverProc                      [MMSYSTEM.6]
2505  */
2506 LRESULT WINAPI DriverProc16(DWORD dwDevID, HDRVR16 hDrv, WORD wMsg,
2507                             DWORD dwParam1, DWORD dwParam2)
2508 {
2509     TRACE("dwDevID=%08x hDrv=%04x wMsg=%04x dwParam1=%08x dwParam2=%08x\n",
2510           dwDevID, hDrv, wMsg, dwParam1, dwParam2);
2511
2512     return DrvDefDriverProc16(dwDevID, hDrv, wMsg, dwParam1, dwParam2);
2513 }
2514
2515 /* ###################################################
2516  * #                     TIME                        #
2517  * ###################################################
2518  */
2519
2520 /******************************************************************
2521  *              MMSYSTEM_MMTIME32to16
2522  *
2523  *
2524  */
2525 void MMSYSTEM_MMTIME32to16(LPMMTIME16 mmt16, const MMTIME* mmt32)
2526 {
2527     mmt16->wType = mmt32->wType;
2528     /* layout of rest is the same for 32/16,
2529      * Note: mmt16->u is 2 bytes smaller than mmt32->u, which has padding
2530      */
2531     memcpy(&(mmt16->u), &(mmt32->u), sizeof(mmt16->u));
2532 }
2533
2534 /******************************************************************
2535  *              MMSYSTEM_MMTIME16to32
2536  *
2537  *
2538  */
2539 void MMSYSTEM_MMTIME16to32(LPMMTIME mmt32, const MMTIME16* mmt16)
2540 {
2541     mmt32->wType = mmt16->wType;
2542     /* layout of rest is the same for 32/16,
2543      * Note: mmt16->u is 2 bytes smaller than mmt32->u, which has padding
2544      */
2545     memcpy(&(mmt32->u), &(mmt16->u), sizeof(mmt16->u));
2546 }
2547
2548 /**************************************************************************
2549  *                              timeGetSystemTime       [MMSYSTEM.601]
2550  */
2551 MMRESULT16 WINAPI timeGetSystemTime16(LPMMTIME16 lpTime, UINT16 wSize)
2552 {
2553     if (wSize >= sizeof(*lpTime)) {
2554         lpTime->wType = TIME_MS;
2555         lpTime->u.ms = GetTickCount();
2556
2557         TRACE("=> %u\n", lpTime->u.ms);
2558     }
2559
2560     return 0;
2561 }
2562
2563 /**************************************************************************
2564  *                              timeSetEvent            [MMSYSTEM.602]
2565  */
2566 MMRESULT16 WINAPI timeSetEvent16(UINT16 wDelay, UINT16 wResol, LPTIMECALLBACK16 lpFunc,
2567                                  DWORD dwUser, UINT16 wFlags)
2568 {
2569     if (wFlags & WINE_TIMER_IS32)
2570         WARN("Unknown windows flag... wine internally used.. ooch\n");
2571
2572     return TIME_SetEventInternal(wDelay, wResol, (LPTIMECALLBACK)lpFunc,
2573                                  dwUser, wFlags & ~WINE_TIMER_IS32);
2574 }
2575
2576 /**************************************************************************
2577  *                              timeKillEvent           [MMSYSTEM.603]
2578  */
2579 MMRESULT16 WINAPI timeKillEvent16(UINT16 wID)
2580 {
2581     return timeKillEvent(wID);
2582 }
2583
2584 /**************************************************************************
2585  *                              timeGetDevCaps          [MMSYSTEM.604]
2586  */
2587 MMRESULT16 WINAPI timeGetDevCaps16(LPTIMECAPS16 lpCaps, UINT16 wSize)
2588 {
2589     TIMECAPS    caps;
2590     MMRESULT    ret;
2591     TRACE("(%p, %u) !\n", lpCaps, wSize);
2592
2593     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
2594
2595     ret = timeGetDevCaps(&caps, sizeof(caps));
2596     if (ret == MMSYSERR_NOERROR) {
2597         TIMECAPS16 tc16;
2598         tc16.wPeriodMin = caps.wPeriodMin;
2599         tc16.wPeriodMax = caps.wPeriodMax;
2600         memcpy(lpCaps, &tc16, min(wSize, sizeof(tc16)));
2601     }
2602     return ret;
2603 }
2604
2605 /**************************************************************************
2606  *                              timeBeginPeriod [MMSYSTEM.605]
2607  */
2608 MMRESULT16 WINAPI timeBeginPeriod16(UINT16 wPeriod)
2609 {
2610     TRACE("(%u) !\n", wPeriod);
2611
2612     return timeBeginPeriod(wPeriod);
2613 }
2614
2615 /**************************************************************************
2616  *                              timeEndPeriod           [MMSYSTEM.606]
2617  */
2618 MMRESULT16 WINAPI timeEndPeriod16(UINT16 wPeriod)
2619 {
2620     TRACE("(%u) !\n", wPeriod);
2621
2622     return timeEndPeriod(wPeriod);
2623 }
2624
2625 /**************************************************************************
2626  *                              mciSendString                   [MMSYSTEM.702]
2627  */
2628 DWORD WINAPI mciSendString16(LPCSTR lpstrCommand, LPSTR lpstrRet,
2629                              UINT16 uRetLen, HWND16 hwndCallback)
2630 {
2631     return mciSendStringA(lpstrCommand, lpstrRet, uRetLen, HWND_32(hwndCallback));
2632 }
2633
2634 /**************************************************************************
2635  *                      mciLoadCommandResource                  [MMSYSTEM.705]
2636  */
2637 UINT16 WINAPI mciLoadCommandResource16(HINSTANCE16 hInst, LPCSTR resname, UINT16 type)
2638 {
2639     HRSRC16     res;
2640     HGLOBAL16   handle;
2641     const BYTE* ptr16;
2642     BYTE*       ptr32;
2643     unsigned    pos = 0, size = 1024, len;
2644     const char* str;
2645     DWORD       flg;
2646     WORD        eid;
2647     UINT16      ret = MCIERR_OUT_OF_MEMORY;
2648
2649     if (!(res = FindResource16( hInst, resname, (LPSTR)RT_RCDATA))) return MCI_NO_COMMAND_TABLE;
2650     if (!(handle = LoadResource16( hInst, res ))) return MCI_NO_COMMAND_TABLE;
2651     ptr16 = LockResource16(handle);
2652     /* converting the 16 bit resource table into a 32W one */
2653     if ((ptr32 = HeapAlloc(GetProcessHeap(), 0, size)))
2654     {
2655         do {
2656             str = (LPCSTR)ptr16;
2657             ptr16 += strlen(str) + 1;
2658             flg = *(const DWORD*)ptr16;
2659             eid = *(const WORD*)(ptr16 + sizeof(DWORD));
2660             ptr16 += sizeof(DWORD) + sizeof(WORD);
2661             len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0) * sizeof(WCHAR);
2662             if (pos + len + sizeof(DWORD) + sizeof(WORD) > size)
2663             {
2664                 while (pos + len * sizeof(WCHAR) + sizeof(DWORD) + sizeof(WORD) > size) size += 1024;
2665                 ptr32 = HeapReAlloc(GetProcessHeap(), 0, ptr32, size);
2666                 if (!ptr32) goto the_end;
2667             }
2668             MultiByteToWideChar(CP_ACP, 0, str, -1, (LPWSTR)(ptr32 + pos), len / sizeof(WCHAR));
2669             *(DWORD*)(ptr32 + pos + len) = flg;
2670             *(WORD*)(ptr32 + pos + len + sizeof(DWORD)) = eid;
2671             pos += len + sizeof(DWORD) + sizeof(WORD);
2672         } while (eid != MCI_END_COMMAND_LIST);
2673     }
2674 the_end:
2675     FreeResource16( handle );
2676     if (ptr32) ret = MCI_SetCommandTable(ptr32, type);
2677     return ret;
2678 }
2679
2680 /**************************************************************************
2681  *                      mciFreeCommandResource                  [MMSYSTEM.713]
2682  */
2683 BOOL16 WINAPI mciFreeCommandResource16(UINT16 uTable)
2684 {
2685     TRACE("(%04x)!\n", uTable);
2686
2687     return MCI_DeleteCommandTable(uTable, TRUE);
2688 }
2689
2690 /* ###################################################
2691  * #                     MMIO                        #
2692  * ###################################################
2693  */
2694
2695 /****************************************************************
2696  *                      MMIO_Map32To16                  [INTERNAL]
2697  */
2698 static LRESULT  MMIO_Map32To16(DWORD wMsg, LPARAM* lp1, LPARAM* lp2)
2699 {
2700     switch (wMsg) {
2701     case MMIOM_CLOSE:
2702     case MMIOM_SEEK:
2703         /* nothing to do */
2704         break;
2705     case MMIOM_OPEN:
2706     case MMIOM_READ:
2707     case MMIOM_WRITE:
2708     case MMIOM_WRITEFLUSH:
2709         *lp1 = MapLS( (void *)*lp1 );
2710         break;
2711     case MMIOM_RENAME:
2712         *lp1 = MapLS( (void *)*lp1 );
2713         *lp2 = MapLS( (void *)*lp2 );
2714         break;
2715     default:
2716         if (wMsg < MMIOM_USER)
2717             TRACE("Not a mappable message (%d)\n", wMsg);
2718     }
2719     return MMSYSERR_NOERROR;
2720 }
2721
2722 /****************************************************************
2723  *              MMIO_UnMap32To16                        [INTERNAL]
2724  */
2725 static LRESULT  MMIO_UnMap32To16(DWORD wMsg, LPARAM lParam1, LPARAM lParam2,
2726                                  LPARAM lp1, LPARAM lp2)
2727 {
2728     switch (wMsg) {
2729     case MMIOM_CLOSE:
2730     case MMIOM_SEEK:
2731         /* nothing to do */
2732         break;
2733     case MMIOM_OPEN:
2734     case MMIOM_READ:
2735     case MMIOM_WRITE:
2736     case MMIOM_WRITEFLUSH:
2737         UnMapLS( lp1 );
2738         break;
2739     case MMIOM_RENAME:
2740         UnMapLS( lp1 );
2741         UnMapLS( lp2 );
2742         break;
2743     default:
2744         if (wMsg < MMIOM_USER)
2745             TRACE("Not a mappable message (%d)\n", wMsg);
2746     }
2747     return MMSYSERR_NOERROR;
2748 }
2749
2750 /******************************************************************
2751  *              MMIO_Callback16
2752  *
2753  *
2754  */
2755 static LRESULT MMIO_Callback16(SEGPTR cb16, LPMMIOINFO lpmmioinfo, UINT uMessage,
2756                                LPARAM lParam1, LPARAM lParam2)
2757 {
2758     DWORD               result;
2759     MMIOINFO16          mmioInfo16;
2760     SEGPTR              segmmioInfo16;
2761     LPARAM              lp1 = lParam1, lp2 = lParam2;
2762     WORD args[7];
2763
2764     memset(&mmioInfo16, 0, sizeof(MMIOINFO16));
2765     mmioInfo16.lDiskOffset = lpmmioinfo->lDiskOffset;
2766     mmioInfo16.adwInfo[0]  = lpmmioinfo->adwInfo[0];
2767     mmioInfo16.adwInfo[1]  = lpmmioinfo->adwInfo[1];
2768     mmioInfo16.adwInfo[2]  = lpmmioinfo->adwInfo[2];
2769     /* map (lParam1, lParam2) into (lp1, lp2) 32=>16 */
2770     if ((result = MMIO_Map32To16(uMessage, &lp1, &lp2)) != MMSYSERR_NOERROR)
2771         return result;
2772
2773     segmmioInfo16 = MapLS(&mmioInfo16);
2774     args[6] = HIWORD(segmmioInfo16);
2775     args[5] = LOWORD(segmmioInfo16);
2776     args[4] = uMessage;
2777     args[3] = HIWORD(lp1);
2778     args[2] = LOWORD(lp1);
2779     args[1] = HIWORD(lp2);
2780     args[0] = LOWORD(lp2);
2781     WOWCallback16Ex( cb16, WCB16_PASCAL, sizeof(args), args, &result );
2782     UnMapLS(segmmioInfo16);
2783     MMIO_UnMap32To16(uMessage, lParam1, lParam2, lp1, lp2);
2784
2785     lpmmioinfo->lDiskOffset = mmioInfo16.lDiskOffset;
2786     lpmmioinfo->adwInfo[0]  = mmioInfo16.adwInfo[0];
2787     lpmmioinfo->adwInfo[1]  = mmioInfo16.adwInfo[1];
2788     lpmmioinfo->adwInfo[2]  = mmioInfo16.adwInfo[2];
2789
2790     return result;
2791 }
2792
2793 /******************************************************************
2794  *             MMIO_ResetSegmentedData
2795  *
2796  */
2797 static LRESULT     MMIO_SetSegmentedBuffer(HMMIO hmmio, SEGPTR ptr, BOOL release)
2798 {
2799     LPWINE_MMIO         wm;
2800
2801     if ((wm = MMIO_Get(hmmio)) == NULL)
2802         return MMSYSERR_INVALHANDLE;
2803     if (release) UnMapLS(wm->segBuffer16);
2804     wm->segBuffer16 = ptr;
2805     return MMSYSERR_NOERROR;
2806 }
2807
2808 /**************************************************************************
2809  *                              mmioOpen                [MMSYSTEM.1210]
2810  */
2811 HMMIO16 WINAPI mmioOpen16(LPSTR szFileName, MMIOINFO16* lpmmioinfo16,
2812                           DWORD dwOpenFlags)
2813 {
2814     HMMIO       ret;
2815
2816     if (lpmmioinfo16) {
2817         MMIOINFO        mmioinfo;
2818
2819         memset(&mmioinfo, 0, sizeof(mmioinfo));
2820
2821         mmioinfo.dwFlags     = lpmmioinfo16->dwFlags;
2822         mmioinfo.fccIOProc   = lpmmioinfo16->fccIOProc;
2823         mmioinfo.pIOProc     = (LPMMIOPROC)lpmmioinfo16->pIOProc;
2824         mmioinfo.cchBuffer   = lpmmioinfo16->cchBuffer;
2825         mmioinfo.pchBuffer   = MapSL((DWORD)lpmmioinfo16->pchBuffer);
2826         mmioinfo.adwInfo[0]  = lpmmioinfo16->adwInfo[0];
2827         /* if we don't have a file name, it's likely a passed open file descriptor */
2828         if (!szFileName) 
2829             mmioinfo.adwInfo[0] = (DWORD)DosFileHandleToWin32Handle(mmioinfo.adwInfo[0]);
2830         mmioinfo.adwInfo[1]  = lpmmioinfo16->adwInfo[1];
2831         mmioinfo.adwInfo[2]  = lpmmioinfo16->adwInfo[2];
2832
2833         ret = MMIO_Open(szFileName, &mmioinfo, dwOpenFlags, MMIO_PROC_16);
2834         MMIO_SetSegmentedBuffer(mmioinfo.hmmio, (SEGPTR)lpmmioinfo16->pchBuffer, FALSE);
2835
2836         lpmmioinfo16->wErrorRet = mmioinfo.wErrorRet;
2837         lpmmioinfo16->hmmio     = HMMIO_16(mmioinfo.hmmio);
2838     } else {
2839         ret = MMIO_Open(szFileName, NULL, dwOpenFlags, MMIO_PROC_32A);
2840     }
2841     return HMMIO_16(ret);
2842 }
2843
2844 /**************************************************************************
2845  *                              mmioClose               [MMSYSTEM.1211]
2846  */
2847 MMRESULT16 WINAPI mmioClose16(HMMIO16 hmmio, UINT16 uFlags)
2848 {
2849     MMIO_SetSegmentedBuffer(HMMIO_32(hmmio), 0, TRUE);
2850     return mmioClose(HMMIO_32(hmmio), uFlags);
2851 }
2852
2853 /**************************************************************************
2854  *                              mmioRead                [MMSYSTEM.1212]
2855  */
2856 LONG WINAPI mmioRead16(HMMIO16 hmmio, HPSTR pch, LONG cch)
2857 {
2858     return mmioRead(HMMIO_32(hmmio), pch, cch);
2859 }
2860
2861 /**************************************************************************
2862  *                              mmioWrite               [MMSYSTEM.1213]
2863  */
2864 LONG WINAPI mmioWrite16(HMMIO16 hmmio, HPCSTR pch, LONG cch)
2865 {
2866     return mmioWrite(HMMIO_32(hmmio),pch,cch);
2867 }
2868
2869 /**************************************************************************
2870  *                              mmioSeek                [MMSYSTEM.1214]
2871  */
2872 LONG WINAPI mmioSeek16(HMMIO16 hmmio, LONG lOffset, INT16 iOrigin)
2873 {
2874     return mmioSeek(HMMIO_32(hmmio), lOffset, iOrigin);
2875 }
2876
2877 /**************************************************************************
2878  *                              mmioGetInfo             [MMSYSTEM.1215]
2879  */
2880 MMRESULT16 WINAPI mmioGetInfo16(HMMIO16 hmmio, MMIOINFO16* lpmmioinfo, UINT16 uFlags)
2881 {
2882     MMIOINFO            mmioinfo;
2883     MMRESULT            ret;
2884     LPWINE_MMIO         wm;
2885
2886     TRACE("(0x%04x,%p,0x%08x)\n", hmmio, lpmmioinfo, uFlags);
2887
2888     if ((wm = MMIO_Get(HMMIO_32(hmmio))) == NULL)
2889         return MMSYSERR_INVALHANDLE;
2890
2891     ret = mmioGetInfo(HMMIO_32(hmmio), &mmioinfo, uFlags);
2892     if (ret != MMSYSERR_NOERROR) return ret;
2893
2894     lpmmioinfo->dwFlags     = mmioinfo.dwFlags;
2895     lpmmioinfo->fccIOProc   = mmioinfo.fccIOProc;
2896     lpmmioinfo->pIOProc     = (wm->ioProc->type == MMIO_PROC_16) ?
2897         (LPMMIOPROC16)wm->ioProc->pIOProc : NULL;
2898     lpmmioinfo->wErrorRet   = mmioinfo.wErrorRet;
2899     lpmmioinfo->hTask       = HTASK_16(mmioinfo.hTask);
2900     lpmmioinfo->cchBuffer   = mmioinfo.cchBuffer;
2901     lpmmioinfo->pchBuffer   = (void*)wm->segBuffer16;
2902     lpmmioinfo->pchNext     = (void*)(wm->segBuffer16 + (mmioinfo.pchNext - mmioinfo.pchBuffer));
2903     lpmmioinfo->pchEndRead  = (void*)(wm->segBuffer16 + (mmioinfo.pchEndRead - mmioinfo.pchBuffer));
2904     lpmmioinfo->pchEndWrite = (void*)(wm->segBuffer16 + (mmioinfo.pchEndWrite - mmioinfo.pchBuffer));
2905     lpmmioinfo->lBufOffset  = mmioinfo.lBufOffset;
2906     lpmmioinfo->lDiskOffset = mmioinfo.lDiskOffset;
2907     lpmmioinfo->adwInfo[0]  = mmioinfo.adwInfo[0];
2908     lpmmioinfo->adwInfo[1]  = mmioinfo.adwInfo[1];
2909     lpmmioinfo->adwInfo[2]  = mmioinfo.adwInfo[2];
2910     lpmmioinfo->dwReserved1 = 0;
2911     lpmmioinfo->dwReserved2 = 0;
2912     lpmmioinfo->hmmio = HMMIO_16(mmioinfo.hmmio);
2913
2914     return MMSYSERR_NOERROR;
2915 }
2916
2917 /**************************************************************************
2918  *                              mmioSetInfo             [MMSYSTEM.1216]
2919  */
2920 MMRESULT16 WINAPI mmioSetInfo16(HMMIO16 hmmio, const MMIOINFO16* lpmmioinfo, UINT16 uFlags)
2921 {
2922     MMIOINFO            mmioinfo;
2923     MMRESULT            ret;
2924
2925     TRACE("(0x%04x,%p,0x%08x)\n",hmmio,lpmmioinfo,uFlags);
2926
2927     ret = mmioGetInfo(HMMIO_32(hmmio), &mmioinfo, 0);
2928     if (ret != MMSYSERR_NOERROR) return ret;
2929
2930     /* check if seg and lin buffers are the same */
2931     if (mmioinfo.cchBuffer != lpmmioinfo->cchBuffer  ||
2932         mmioinfo.pchBuffer != MapSL((DWORD)lpmmioinfo->pchBuffer)) 
2933         return MMSYSERR_INVALPARAM;
2934
2935     /* check pointers coherence */
2936     if (lpmmioinfo->pchNext < lpmmioinfo->pchBuffer ||
2937         lpmmioinfo->pchNext > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer ||
2938         lpmmioinfo->pchEndRead < lpmmioinfo->pchBuffer ||
2939         lpmmioinfo->pchEndRead > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer ||
2940         lpmmioinfo->pchEndWrite < lpmmioinfo->pchBuffer ||
2941         lpmmioinfo->pchEndWrite > lpmmioinfo->pchBuffer + lpmmioinfo->cchBuffer)
2942         return MMSYSERR_INVALPARAM;
2943
2944     mmioinfo.pchNext     = mmioinfo.pchBuffer + (lpmmioinfo->pchNext     - lpmmioinfo->pchBuffer);
2945     mmioinfo.pchEndRead  = mmioinfo.pchBuffer + (lpmmioinfo->pchEndRead  - lpmmioinfo->pchBuffer);
2946     mmioinfo.pchEndWrite = mmioinfo.pchBuffer + (lpmmioinfo->pchEndWrite - lpmmioinfo->pchBuffer);
2947
2948     return mmioSetInfo(HMMIO_32(hmmio), &mmioinfo, uFlags);
2949 }
2950
2951 /**************************************************************************
2952  *                              mmioSetBuffer           [MMSYSTEM.1217]
2953  */
2954 MMRESULT16 WINAPI mmioSetBuffer16(HMMIO16 hmmio, LPSTR pchBuffer,
2955                                   LONG cchBuffer, UINT16 uFlags)
2956 {
2957     MMRESULT    ret = mmioSetBuffer(HMMIO_32(hmmio), MapSL((DWORD)pchBuffer), 
2958                                     cchBuffer, uFlags);
2959
2960     if (ret == MMSYSERR_NOERROR)
2961         MMIO_SetSegmentedBuffer(HMMIO_32(hmmio), (DWORD)pchBuffer, TRUE);
2962     else
2963         UnMapLS((DWORD)pchBuffer);
2964     return ret;
2965 }
2966
2967 /**************************************************************************
2968  *                              mmioFlush               [MMSYSTEM.1218]
2969  */
2970 MMRESULT16 WINAPI mmioFlush16(HMMIO16 hmmio, UINT16 uFlags)
2971 {
2972     return mmioFlush(HMMIO_32(hmmio), uFlags);
2973 }
2974
2975 /***********************************************************************
2976  *                              mmioAdvance             [MMSYSTEM.1219]
2977  */
2978 MMRESULT16 WINAPI mmioAdvance16(HMMIO16 hmmio, MMIOINFO16* lpmmioinfo, UINT16 uFlags)
2979 {
2980     MMIOINFO    mmioinfo;
2981     LRESULT     ret;
2982
2983     /* WARNING: this heavily relies on mmioAdvance implementation (for choosing which
2984      * fields to init
2985      */
2986     if (lpmmioinfo)
2987     {
2988         mmioinfo.pchBuffer = MapSL((DWORD)lpmmioinfo->pchBuffer);
2989         mmioinfo.pchNext = MapSL((DWORD)lpmmioinfo->pchNext);
2990         mmioinfo.dwFlags = lpmmioinfo->dwFlags;
2991         mmioinfo.lBufOffset = lpmmioinfo->lBufOffset;
2992         ret = mmioAdvance(HMMIO_32(hmmio), &mmioinfo, uFlags);
2993     }
2994     else
2995         ret = mmioAdvance(HMMIO_32(hmmio), NULL, uFlags);
2996         
2997     if (ret != MMSYSERR_NOERROR) return ret;
2998
2999     if (lpmmioinfo)
3000     {
3001         lpmmioinfo->dwFlags = mmioinfo.dwFlags;
3002         lpmmioinfo->pchNext     = (void*)(lpmmioinfo->pchBuffer + (mmioinfo.pchNext - mmioinfo.pchBuffer));
3003         lpmmioinfo->pchEndRead  = (void*)(lpmmioinfo->pchBuffer + (mmioinfo.pchEndRead - mmioinfo.pchBuffer));
3004         lpmmioinfo->pchEndWrite = (void*)(lpmmioinfo->pchBuffer + (mmioinfo.pchEndWrite - mmioinfo.pchBuffer));
3005         lpmmioinfo->lBufOffset  = mmioinfo.lBufOffset;
3006         lpmmioinfo->lDiskOffset = mmioinfo.lDiskOffset;
3007     }
3008
3009     return MMSYSERR_NOERROR;
3010 }
3011
3012 /**************************************************************************
3013  *                              mmioStringToFOURCC      [MMSYSTEM.1220]
3014  */
3015 FOURCC WINAPI mmioStringToFOURCC16(LPCSTR sz, UINT16 uFlags)
3016 {
3017     return mmioStringToFOURCCA(sz, uFlags);
3018 }
3019
3020 /**************************************************************************
3021  *              mmioInstallIOProc    [MMSYSTEM.1221]
3022  */
3023 LPMMIOPROC16 WINAPI mmioInstallIOProc16(FOURCC fccIOProc, LPMMIOPROC16 pIOProc,
3024                                         DWORD dwFlags)
3025 {
3026     return (LPMMIOPROC16)MMIO_InstallIOProc(fccIOProc, (LPMMIOPROC)pIOProc,
3027                                             dwFlags, MMIO_PROC_16);
3028 }
3029
3030 /**************************************************************************
3031  *                              mmioSendMessage [MMSYSTEM.1222]
3032  */
3033 LRESULT WINAPI mmioSendMessage16(HMMIO16 hmmio, UINT16 uMessage,
3034                                  LPARAM lParam1, LPARAM lParam2)
3035 {
3036     return MMIO_SendMessage(HMMIO_32(hmmio), uMessage, 
3037                             lParam1, lParam2, MMIO_PROC_16);
3038 }
3039
3040 /**************************************************************************
3041  *                              mmioDescend             [MMSYSTEM.1223]
3042  */
3043 MMRESULT16 WINAPI mmioDescend16(HMMIO16 hmmio, LPMMCKINFO lpck,
3044                                 const MMCKINFO* lpckParent, UINT16 uFlags)
3045 {
3046     return mmioDescend(HMMIO_32(hmmio), lpck, lpckParent, uFlags);
3047 }
3048
3049 /**************************************************************************
3050  *                              mmioAscend              [MMSYSTEM.1224]
3051  */
3052 MMRESULT16 WINAPI mmioAscend16(HMMIO16 hmmio, MMCKINFO* lpck, UINT16 uFlags)
3053 {
3054     return mmioAscend(HMMIO_32(hmmio),lpck,uFlags);
3055 }
3056
3057 /**************************************************************************
3058  *                              mmioCreateChunk         [MMSYSTEM.1225]
3059  */
3060 MMRESULT16 WINAPI mmioCreateChunk16(HMMIO16 hmmio, MMCKINFO* lpck, UINT16 uFlags)
3061 {
3062     return mmioCreateChunk(HMMIO_32(hmmio), lpck, uFlags);
3063 }
3064
3065 /**************************************************************************
3066  *                              mmioRename              [MMSYSTEM.1226]
3067  */
3068 MMRESULT16 WINAPI mmioRename16(LPCSTR szFileName, LPCSTR szNewFileName,
3069                                MMIOINFO16* lpmmioinfo, DWORD dwRenameFlags)
3070 {
3071     BOOL        inst = FALSE;
3072     MMRESULT    ret;
3073     MMIOINFO    mmioinfo;
3074
3075     if (lpmmioinfo != NULL && lpmmioinfo->pIOProc != NULL && 
3076         lpmmioinfo->fccIOProc == 0) {
3077         FIXME("Can't handle this case yet\n");
3078         return MMSYSERR_ERROR;
3079     }
3080      
3081     /* this is a bit hacky, but it'll work if we get a fourCC code or nothing.
3082      * but a non installed ioproc without a fourcc won't do
3083      */
3084     if (lpmmioinfo && lpmmioinfo->fccIOProc && lpmmioinfo->pIOProc) {
3085         MMIO_InstallIOProc(lpmmioinfo->fccIOProc, (LPMMIOPROC)lpmmioinfo->pIOProc,
3086                            MMIO_INSTALLPROC, MMIO_PROC_16);
3087         inst = TRUE;
3088     }
3089     memset(&mmioinfo, 0, sizeof(mmioinfo));
3090     mmioinfo.fccIOProc = lpmmioinfo->fccIOProc;
3091     ret = mmioRenameA(szFileName, szNewFileName, &mmioinfo, dwRenameFlags);
3092     if (inst) {
3093         MMIO_InstallIOProc(lpmmioinfo->fccIOProc, NULL,
3094                            MMIO_REMOVEPROC, MMIO_PROC_16);
3095     }
3096     return ret;
3097 }
3098
3099 /* ###################################################
3100  * #                     JOYSTICK                    #
3101  * ###################################################
3102  */
3103
3104 /**************************************************************************
3105  *                              joyGetNumDevs           [MMSYSTEM.101]
3106  */
3107 UINT16 WINAPI joyGetNumDevs16(void)
3108 {
3109     return joyGetNumDevs();
3110 }
3111
3112 /**************************************************************************
3113  *                              joyGetDevCaps           [MMSYSTEM.102]
3114  */
3115 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
3116 {
3117     JOYCAPSA    jca;
3118     MMRESULT    ret;
3119
3120     if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
3121
3122     ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
3123
3124     if (ret != JOYERR_NOERROR) return ret;
3125     lpCaps->wMid = jca.wMid;
3126     lpCaps->wPid = jca.wPid;
3127     strcpy(lpCaps->szPname, jca.szPname);
3128     lpCaps->wXmin = jca.wXmin;
3129     lpCaps->wXmax = jca.wXmax;
3130     lpCaps->wYmin = jca.wYmin;
3131     lpCaps->wYmax = jca.wYmax;
3132     lpCaps->wZmin = jca.wZmin;
3133     lpCaps->wZmax = jca.wZmax;
3134     lpCaps->wNumButtons = jca.wNumButtons;
3135     lpCaps->wPeriodMin = jca.wPeriodMin;
3136     lpCaps->wPeriodMax = jca.wPeriodMax;
3137
3138     if (wSize >= sizeof(JOYCAPS16)) { /* Win95 extensions ? */
3139         lpCaps->wRmin = jca.wRmin;
3140         lpCaps->wRmax = jca.wRmax;
3141         lpCaps->wUmin = jca.wUmin;
3142         lpCaps->wUmax = jca.wUmax;
3143         lpCaps->wVmin = jca.wVmin;
3144         lpCaps->wVmax = jca.wVmax;
3145         lpCaps->wCaps = jca.wCaps;
3146         lpCaps->wMaxAxes = jca.wMaxAxes;
3147         lpCaps->wNumAxes = jca.wNumAxes;
3148         lpCaps->wMaxButtons = jca.wMaxButtons;
3149         strcpy(lpCaps->szRegKey, jca.szRegKey);
3150         strcpy(lpCaps->szOEMVxD, jca.szOEMVxD);
3151     }
3152
3153     return ret;
3154 }
3155
3156 /**************************************************************************
3157  *                              joyGetPosEx           [MMSYSTEM.110]
3158  */
3159 MMRESULT16 WINAPI joyGetPosEx16(UINT16 wID, LPJOYINFOEX lpInfo)
3160 {
3161     return joyGetPosEx(wID, lpInfo);
3162 }
3163
3164 /**************************************************************************
3165  *                              joyGetPos               [MMSYSTEM.103]
3166  */
3167 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
3168 {
3169     JOYINFO     ji;
3170     MMRESULT    ret;
3171
3172     TRACE("(%d, %p);\n", wID, lpInfo);
3173
3174     if ((ret = joyGetPos(wID, &ji)) == JOYERR_NOERROR) {
3175         lpInfo->wXpos = ji.wXpos;
3176         lpInfo->wYpos = ji.wYpos;
3177         lpInfo->wZpos = ji.wZpos;
3178         lpInfo->wButtons = ji.wButtons;
3179     }
3180     return ret;
3181 }
3182
3183 /**************************************************************************
3184  *                              joyGetThreshold         [MMSYSTEM.104]
3185  */
3186 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
3187 {
3188     UINT        t;
3189     MMRESULT    ret;
3190
3191     ret = joyGetThreshold(wID, &t);
3192     if (ret == JOYERR_NOERROR)
3193         *lpThreshold = t;
3194     return ret;
3195 }
3196
3197 /**************************************************************************
3198  *                              joyReleaseCapture       [MMSYSTEM.105]
3199  */
3200 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
3201 {
3202     return joyReleaseCapture(wID);
3203 }
3204
3205 /**************************************************************************
3206  *                              joySetCapture           [MMSYSTEM.106]
3207  */
3208 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd, UINT16 wID, UINT16 wPeriod, BOOL16 bChanged)
3209 {
3210     return joySetCapture(HWND_32(hWnd), wID, wPeriod, bChanged);
3211 }
3212
3213 /**************************************************************************
3214  *                              joySetThreshold         [MMSYSTEM.107]
3215  */
3216 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
3217 {
3218     return joySetThreshold(wID,wThreshold);
3219 }
3220
3221 /**************************************************************************
3222  *                              joySetCalibration       [MMSYSTEM.109]
3223  */
3224 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
3225 {
3226     FIXME("(%04X): stub.\n", wID);
3227     return JOYERR_NOCANDO;
3228 }