winmm: Support DRVM_MAPPER_PREFERRED_GET in waveXxxMessage.
[wine] / dlls / winmm / lolvldrv.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  * MMSYSTEM low level drivers handling functions
5  *
6  * Copyright 1999 Eric Pouech
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
28 #define COBJMACROS
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <assert.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winreg.h"
36 #include "winnls.h"
37 #include "winemm.h"
38 #include "wine/debug.h"
39 #include "wine/exception.h"
40
41 #include "wingdi.h"
42 #include "ole2.h"
43 #include "devpkey.h"
44 #include "mmdeviceapi.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
47
48 /* each known type of driver has an instance of this structure */
49 typedef struct tagWINE_LLTYPE {
50     /* those attributes depend on the specification of the type */
51     LPCSTR              typestr;        /* name (for debugging) */
52     /* those attributes reflect the loaded/current situation for the type */
53     UINT                wMaxId;         /* number of loaded devices (sum across all loaded drivers) */
54     LPWINE_MLD          lpMlds;         /* "static" mlds to access the part though device IDs */
55     int                 nMapper;        /* index to mapper */
56 } WINE_LLTYPE;
57
58 static WINE_LLTYPE llTypes[MMDRV_MAX] = {
59     { "Aux", 0, 0, -1 },
60     { "Mixer", 0, 0, -1 },
61     { "MidiIn", 0, 0, -1 },
62     { "MidiOut", 0, 0, -1 },
63     { "WaveIn", 0, 0, -1 },
64     { "WaveOut", 0, 0, -1 }
65 };
66
67 static int drivers_loaded, MMDrvsHi;
68 static WINE_MM_DRIVER   MMDrvs[8];
69 static LPWINE_MLD       MM_MLDrvs[40];
70 #define MAX_MM_MLDRVS   (sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]))
71
72 static void MMDRV_Init(void);
73
74 static void MMDRV_InitSingleType(UINT type) {
75     if (!drivers_loaded) {
76         drivers_loaded = 1;
77         MMDRV_Init();
78     }
79 }
80
81 /**************************************************************************
82  *                      MMDRV_GetNum                            [internal]
83  */
84 UINT    MMDRV_GetNum(UINT type)
85 {
86     TRACE("(%04x)\n", type);
87     assert(type < MMDRV_MAX);
88     MMDRV_InitSingleType(type);
89     return llTypes[type].wMaxId;
90 }
91
92 /**************************************************************************
93  *                              MMDRV_Message                   [internal]
94  */
95 DWORD  MMDRV_Message(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1,
96                      DWORD_PTR dwParam2)
97 {
98     LPWINE_MM_DRIVER            lpDrv;
99     DWORD                       ret;
100     WINE_MM_DRIVER_PART*        part;
101     WINE_LLTYPE*                llType = &llTypes[mld->type];
102
103     TRACE("(%s %d %u 0x%08lx 0x%08lx 0x%08lx)\n",
104           llTypes[mld->type].typestr, mld->uDeviceID, wMsg,
105           mld->dwDriverInstance, dwParam1, dwParam2);
106
107     if ((UINT16)mld->uDeviceID == (UINT16)-1) {
108         if (llType->nMapper == -1) {
109             WARN("uDev=-1 requested on non-mapped ll type %s\n",
110                  llTypes[mld->type].typestr);
111             return MMSYSERR_BADDEVICEID;
112         }
113     } else {
114         if (mld->uDeviceID >= llType->wMaxId) {
115             WARN("uDev(%u) requested >= max (%d)\n", mld->uDeviceID, llType->wMaxId);
116             return MMSYSERR_BADDEVICEID;
117         }
118     }
119
120     lpDrv = &MMDrvs[mld->mmdIndex];
121     part = &lpDrv->parts[mld->type];
122
123     assert(part->fnMessage32);
124
125     TRACE("Calling message(dev=%d msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
126           mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
127     ret = part->fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
128     TRACE("=> %s\n", WINMM_ErrorToString(ret));
129
130     return ret;
131 }
132
133 /**************************************************************************
134  *                              MMDRV_Alloc                     [internal]
135  */
136 LPWINE_MLD      MMDRV_Alloc(UINT size, UINT type, LPHANDLE hndl, DWORD* dwFlags,
137                             DWORD_PTR* dwCallback, DWORD_PTR* dwInstance)
138 {
139     LPWINE_MLD  mld;
140     UINT_PTR i;
141     TRACE("(%d, %04x, %p, %p, %p, %p)\n",
142           size, type, hndl, dwFlags, dwCallback, dwInstance);
143
144     mld = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
145     if (!mld)   return NULL;
146
147     /* find an empty slot in MM_MLDrvs table */
148     for (i = 0; i < MAX_MM_MLDRVS; i++) if (!MM_MLDrvs[i]) break;
149
150     if (i == MAX_MM_MLDRVS) {
151         /* the MM_MLDrvs table could be made growable in the future if needed */
152         ERR("Too many open drivers\n");
153         HeapFree(GetProcessHeap(), 0, mld);
154         return NULL;
155     }
156     MM_MLDrvs[i] = mld;
157     *hndl = (HANDLE)(i | 0x8000);
158
159     mld->type = type;
160     if ((UINT_PTR)*hndl < MMDRV_GetNum(type) || ((UINT_PTR)*hndl >> 16)) {
161         /* FIXME: those conditions must be fulfilled so that:
162          * - we can distinguish between device IDs and handles
163          * - we can use handles as 16 or 32 bit entities
164          */
165         ERR("Shouldn't happen. Bad allocation scheme\n");
166     }
167
168     mld->dwFlags = HIWORD(*dwFlags);
169     mld->dwCallback = *dwCallback;
170     mld->dwClientInstance = *dwInstance;
171
172     return mld;
173 }
174
175 /**************************************************************************
176  *                              MMDRV_Free                      [internal]
177  */
178 void    MMDRV_Free(HANDLE hndl, LPWINE_MLD mld)
179 {
180     TRACE("(%p, %p)\n", hndl, mld);
181
182     if ((UINT_PTR)hndl & 0x8000) {
183         UINT_PTR idx = (UINT_PTR)hndl & ~0x8000;
184         if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
185             MM_MLDrvs[idx] = NULL;
186             HeapFree(GetProcessHeap(), 0, mld);
187             return;
188         }
189     }
190     ERR("Bad Handle %p at %p (not freed)\n", hndl, mld);
191 }
192
193 /**************************************************************************
194  *                              MMDRV_Open                      [internal]
195  */
196 DWORD MMDRV_Open(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1, DWORD dwFlags)
197 {
198     DWORD               dwRet = MMSYSERR_BADDEVICEID;
199     DWORD_PTR           dwInstance;
200     WINE_LLTYPE*        llType = &llTypes[mld->type];
201     TRACE("(%p, %04x, 0x%08lx, 0x%08x)\n", mld, wMsg, dwParam1, dwFlags);
202
203     mld->dwDriverInstance = (DWORD_PTR)&dwInstance;
204
205     if (mld->uDeviceID == (UINT)-1 || mld->uDeviceID == (UINT16)-1) {
206         TRACE("MAPPER mode requested !\n");
207         if (llType->nMapper == -1) {
208             WARN("Mapper not supported for type %s\n", llTypes[mld->type].typestr);
209             return MMSYSERR_BADDEVICEID;
210         }
211         mld->mmdIndex = llType->lpMlds[-1].mmdIndex;
212         TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
213         dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags);
214     } else {
215         if (mld->uDeviceID < llType->wMaxId) {
216             mld->mmdIndex = llType->lpMlds[mld->uDeviceID].mmdIndex;
217             TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
218             dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags);
219         }
220     }
221     if (dwRet == MMSYSERR_NOERROR)
222         mld->dwDriverInstance = dwInstance;
223     return dwRet;
224 }
225
226 /**************************************************************************
227  *                              MMDRV_Close                     [internal]
228  */
229 DWORD   MMDRV_Close(LPWINE_MLD mld, UINT wMsg)
230 {
231     TRACE("(%p, %04x)\n", mld, wMsg);
232     return MMDRV_Message(mld, wMsg, 0L, 0L);
233 }
234
235 /**************************************************************************
236  *                              MMDRV_GetByID                   [internal]
237  */
238 static LPWINE_MLD MMDRV_GetByID(UINT uDevID, UINT type)
239 {
240     TRACE("(%04x, %04x)\n", uDevID, type);
241     if (uDevID < llTypes[type].wMaxId)
242         return &llTypes[type].lpMlds[uDevID];
243     if ((uDevID == (UINT16)-1 || uDevID == (UINT)-1) && llTypes[type].nMapper != -1)
244         return &llTypes[type].lpMlds[-1];
245     return NULL;
246 }
247
248 /**************************************************************************
249  *                              MMDRV_Get                       [internal]
250  */
251 LPWINE_MLD      MMDRV_Get(HANDLE _hndl, UINT type, BOOL bCanBeID)
252 {
253     LPWINE_MLD  mld = NULL;
254     UINT_PTR    hndl = (UINT_PTR)_hndl;
255     TRACE("(%p, %04x, %c)\n", _hndl, type, bCanBeID ? 'Y' : 'N');
256
257     assert(type < MMDRV_MAX);
258     MMDRV_InitSingleType(type);
259
260     if (hndl >= llTypes[type].wMaxId &&
261         hndl != (UINT16)-1 && hndl != (UINT)-1) {
262         if (hndl & 0x8000) {
263             UINT idx = hndl & ~0x8000;
264             if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
265                 __TRY
266                 {
267                     mld = MM_MLDrvs[idx];
268                     if (mld && mld->type != type) mld = NULL;
269                 }
270                 __EXCEPT_PAGE_FAULT
271                 {
272                     mld = NULL;
273                 }
274                 __ENDTRY;
275             }
276         }
277     }
278     if (mld == NULL && bCanBeID) {
279         mld = MMDRV_GetByID(hndl, type);
280     }
281     return mld;
282 }
283
284 /**************************************************************************
285  *                              MMDRV_GetRelated                [internal]
286  */
287 LPWINE_MLD      MMDRV_GetRelated(HANDLE hndl, UINT srcType,
288                                  BOOL bSrcCanBeID, UINT dstType)
289 {
290     LPWINE_MLD          mld;
291     TRACE("(%p, %04x, %c, %04x)\n",
292           hndl, srcType, bSrcCanBeID ? 'Y' : 'N', dstType);
293
294     if ((mld = MMDRV_Get(hndl, srcType, bSrcCanBeID)) != NULL) {
295         WINE_MM_DRIVER_PART*    part = &MMDrvs[mld->mmdIndex].parts[dstType];
296         if (part->nIDMin < part->nIDMax)
297             return MMDRV_GetByID(part->nIDMin, dstType);
298     }
299     return NULL;
300 }
301
302 /**************************************************************************
303  *                              MMDRV_PhysicalFeatures          [internal]
304  */
305 UINT    MMDRV_PhysicalFeatures(LPWINE_MLD mld, UINT uMsg,
306                                DWORD_PTR dwParam1, DWORD_PTR dwParam2)
307 {
308     WINE_MM_DRIVER*     lpDrv = &MMDrvs[mld->mmdIndex];
309
310     TRACE("(%p, %04x, %08lx, %08lx)\n", mld, uMsg, dwParam1, dwParam2);
311
312     /* all those function calls are undocumented */
313     switch (uMsg) {
314     case DRV_QUERYDRVENTRY:
315         lstrcpynA((LPSTR)dwParam1, lpDrv->drvname, LOWORD(dwParam2));
316         break;
317     case DRV_QUERYDEVNODE:
318         *(LPDWORD)dwParam1 = 0L; /* should be DevNode */
319         break;
320     case DRV_QUERYNAME:
321         WARN("NIY QueryName\n");
322         break;
323     case DRV_QUERYDRIVERIDS:
324         WARN("NIY call VxD\n");
325         /* should call VxD MMDEVLDR with (DevNode, dwParam1 and dwParam2) as pmts
326          * dwParam1 is buffer and dwParam2 is sizeof(buffer)
327          * I don't know where the result is stored though
328          */
329         break;
330     case DRV_QUERYMAPPABLE:
331         return (lpDrv->bIsMapper) ? 2 : 0;
332
333     case DRVM_MAPPER_PREFERRED_GET:
334         /* FIXME: get from registry someday */
335         *((LPDWORD)dwParam1) = -1;      /* No preferred device */
336         *((LPDWORD)dwParam2) = 0;
337         break;
338
339     case DRV_QUERYDEVICEINTERFACE:
340     case DRV_QUERYDEVICEINTERFACESIZE:
341         return MMDRV_Message(mld, uMsg, dwParam1, dwParam2);
342
343     case DRV_QUERYDSOUNDIFACE: /* Wine-specific: Retrieve DirectSound interface */
344     case DRV_QUERYDSOUNDDESC: /* Wine-specific: Retrieve DirectSound driver description*/
345         return MMDRV_Message(mld, uMsg, dwParam1, dwParam2);
346
347     default:
348         WARN("Unknown call %04x\n", uMsg);
349         return MMSYSERR_INVALPARAM;
350     }
351     return 0L;
352 }
353
354 /**************************************************************************
355  *                              MMDRV_InitPerType               [internal]
356  */
357 static  BOOL    MMDRV_InitPerType(LPWINE_MM_DRIVER lpDrv, UINT type, UINT wMsg)
358 {
359     WINE_MM_DRIVER_PART*        part = &lpDrv->parts[type];
360     DWORD                       ret;
361     UINT                        count = 0;
362     int                         i, k;
363     TRACE("(%p, %04x, %04x)\n", lpDrv, type, wMsg);
364
365     part->nIDMin = part->nIDMax = 0;
366
367     /* for DRVM_INIT and DRVM_ENABLE, dwParam2 should be PnP node */
368     /* the DRVM_ENABLE is only required when the PnP node is non zero */
369     if (part->fnMessage32) {
370         ret = part->fnMessage32(0, DRVM_INIT, 0L, 0L, 0L);
371         TRACE("DRVM_INIT => %s\n", WINMM_ErrorToString(ret));
372 #if 0
373         ret = part->fnMessage32(0, DRVM_ENABLE, 0L, 0L, 0L);
374         TRACE("DRVM_ENABLE => %08lx\n", ret);
375 #endif
376         count = part->fnMessage32(0, wMsg, 0L, 0L, 0L);
377     }
378     else return FALSE;
379
380     TRACE("Got %u dev for (%s:%s)\n", count, lpDrv->drvname, llTypes[type].typestr);
381     
382     if (HIWORD(count))
383         return FALSE;
384
385     /* got some drivers */
386     if (lpDrv->bIsMapper) {
387         llTypes[type].nMapper = MMDrvsHi;
388     } else {
389         if (count == 0)
390             return FALSE;
391         part->nIDMin = llTypes[type].wMaxId;
392         llTypes[type].wMaxId += count;
393         part->nIDMax = llTypes[type].wMaxId;
394     }
395     TRACE("Setting min=%d max=%d (ttop=%d) for (%s:%s)\n",
396           part->nIDMin, part->nIDMax, llTypes[type].wMaxId,
397           lpDrv->drvname, llTypes[type].typestr);
398     /* realloc translation table */
399     if (llTypes[type].lpMlds)
400         llTypes[type].lpMlds = (LPWINE_MLD)
401         HeapReAlloc(GetProcessHeap(), 0, llTypes[type].lpMlds - 1,
402                     sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
403     else
404         llTypes[type].lpMlds = (LPWINE_MLD)
405         HeapAlloc(GetProcessHeap(), 0,
406                     sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
407
408     /* re-build the translation table */
409     if (lpDrv->bIsMapper) {
410         TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, -1, MMDrvs[llTypes[type].nMapper].drvname);
411         llTypes[type].lpMlds[-1].uDeviceID = (UINT)-1;
412         llTypes[type].lpMlds[-1].type = type;
413         llTypes[type].lpMlds[-1].mmdIndex = llTypes[type].nMapper;
414         llTypes[type].lpMlds[-1].dwDriverInstance = 0;
415     }
416     for (i = k = 0; i <= MMDrvsHi; i++) {
417         while (MMDrvs[i].parts[type].nIDMin <= k && k < MMDrvs[i].parts[type].nIDMax) {
418             TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, k, MMDrvs[i].drvname);
419             llTypes[type].lpMlds[k].uDeviceID = k;
420             llTypes[type].lpMlds[k].type = type;
421             llTypes[type].lpMlds[k].mmdIndex = i;
422             llTypes[type].lpMlds[k].dwDriverInstance = 0;
423             k++;
424         }
425     }
426     return TRUE;
427 }
428
429 /**************************************************************************
430  *                              MMDRV_Install                   [internal]
431  */
432 static  BOOL    MMDRV_Install(LPCSTR drvRegName, LPCSTR drvFileName, BOOL bIsMapper)
433 {
434     int                 i, count = 0;
435     LPWINE_MM_DRIVER    lpDrv = &MMDrvs[MMDrvsHi];
436     LPWINE_DRIVER       d;
437     WINEMM_msgFunc32    func;
438
439     TRACE("('%s', '%s', mapper=%c);\n", drvRegName, drvFileName, bIsMapper ? 'Y' : 'N');
440
441     for (i = 0; i < MMDrvsHi; i++) {
442         if (!strcmp(drvRegName, MMDrvs[i].drvname)) return FALSE;
443     }
444
445     /* Be sure that size of MMDrvs matches the max number of loadable
446      * drivers !!
447      * If not just increase size of MMDrvs
448      */
449     assert(MMDrvsHi <= sizeof(MMDrvs)/sizeof(MMDrvs[0]));
450
451     memset(lpDrv, 0, sizeof(*lpDrv));
452
453     if (!(lpDrv->hDriver = OpenDriverA(drvFileName, 0, 0))) {
454         WARN("Couldn't open driver '%s'\n", drvFileName);
455         return FALSE;
456     }
457
458     d = DRIVER_FindFromHDrvr(lpDrv->hDriver);
459
460     /* Then look for xxxMessage functions */
461 #define AA(_h,_w,_x,_y,_z)                                      \
462     func = (WINEMM_msgFunc##_y) _z ((_h), #_x);                 \
463     if (func != NULL)                                           \
464         { lpDrv->parts[_w].fnMessage##_y = func; count++;       \
465           TRACE("Got %d bit func '%s'\n", _y, #_x);         }
466
467     if (d->hModule) {
468 #define A(_x,_y)        AA(d->hModule,_x,_y,32,GetProcAddress)
469             A(MMDRV_AUX,        auxMessage);
470             A(MMDRV_MIXER,      mxdMessage);
471             A(MMDRV_MIDIIN,     midMessage);
472             A(MMDRV_MIDIOUT,    modMessage);
473             A(MMDRV_WAVEIN,     widMessage);
474             A(MMDRV_WAVEOUT,    wodMessage);
475 #undef A
476     }
477 #undef AA
478
479     if (!count) {
480         CloseDriver(lpDrv->hDriver, 0, 0);
481         WARN("No message functions found\n");
482         return FALSE;
483     }
484
485     /* FIXME: being a mapper or not should be known by another way */
486     /* it's known for NE drvs (the description is of the form '*mapper: *'
487      * I don't have any clue for PE drvs
488      */
489     lpDrv->bIsMapper = bIsMapper;
490     lpDrv->drvname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(drvRegName) + 1), drvRegName);
491
492     /* Finish init and get the count of the devices */
493     i = 0;
494     if (MMDRV_InitPerType(lpDrv, MMDRV_AUX,     AUXDM_GETNUMDEVS))      i = 1;
495     if (MMDRV_InitPerType(lpDrv, MMDRV_MIXER,   MXDM_GETNUMDEVS))       i = 1;
496     if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIIN,  MIDM_GETNUMDEVS))       i = 1;
497     if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIOUT, MODM_GETNUMDEVS))       i = 1;
498     if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEIN,  WIDM_GETNUMDEVS))       i = 1;
499     if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEOUT, WODM_GETNUMDEVS))       i = 1;
500     /* if all those func calls return FALSE, then the driver must be unloaded */
501     if (!i) {
502         CloseDriver(lpDrv->hDriver, 0, 0);
503         HeapFree(GetProcessHeap(), 0, lpDrv->drvname);
504         WARN("Driver initialization failed\n");
505         return FALSE;
506     }
507
508     MMDrvsHi++;
509
510     return TRUE;
511 }
512
513 /**************************************************************************
514  *                              MMDRV_Init
515  */
516 static void MMDRV_Init(void)
517 {
518     IMMDeviceEnumerator *devenum;
519     IMMDevice *device;
520     IPropertyStore *ps;
521     PROPVARIANT pv;
522     DWORD size;
523     char *drvA;
524     HRESULT init_hr, hr;
525
526     static const WCHAR wine_info_deviceW[] = {'W','i','n','e',' ',
527         'i','n','f','o',' ','d','e','v','i','c','e',0};
528
529     TRACE("()\n");
530
531     init_hr = CoInitialize(NULL);
532
533     hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
534             CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&devenum);
535     if(FAILED(hr)){
536         ERR("CoCreateInstance failed: %08x\n", hr);
537         goto exit;
538     }
539
540     hr = IMMDeviceEnumerator_GetDevice(devenum, wine_info_deviceW, &device);
541     IMMDeviceEnumerator_Release(devenum);
542     if(FAILED(hr)){
543         ERR("GetDevice failed: %08x\n", hr);
544         goto exit;
545     }
546
547     hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
548     if(FAILED(hr)){
549         ERR("OpenPropertyStore failed: %08x\n", hr);
550         IMMDevice_Release(device);
551         goto exit;
552     }
553
554     hr = IPropertyStore_GetValue(ps,
555             (const PROPERTYKEY *)&DEVPKEY_Device_Driver, &pv);
556     IPropertyStore_Release(ps);
557     IMMDevice_Release(device);
558     if(FAILED(hr)){
559         ERR("GetValue failed: %08x\n", hr);
560         goto exit;
561     }
562
563     size = WideCharToMultiByte(CP_ACP, 0, pv.u.pwszVal, -1,
564             NULL, 0, NULL, NULL);
565     drvA = HeapAlloc(GetProcessHeap(), 0, size);
566     WideCharToMultiByte(CP_ACP, 0, pv.u.pwszVal, -1, drvA, size, NULL, NULL);
567
568     MMDRV_Install(drvA, drvA, FALSE);
569
570     HeapFree(GetProcessHeap(), 0, drvA);
571     PropVariantClear(&pv);
572
573     MMDRV_Install("wavemapper", "msacm32.drv", TRUE);
574     MMDRV_Install("midimapper", "midimap.dll", TRUE);
575
576 exit:
577     if(SUCCEEDED(init_hr))
578         CoUninitialize();
579 }
580
581 /******************************************************************
582  *              ExitPerType
583  *
584  *
585  */
586 static  BOOL    MMDRV_ExitPerType(LPWINE_MM_DRIVER lpDrv, UINT type)
587 {
588     WINE_MM_DRIVER_PART*        part = &lpDrv->parts[type];
589     DWORD                       ret;
590     TRACE("(%p, %04x)\n", lpDrv, type);
591
592     if (part->fnMessage32) {
593 #if 0
594         ret = part->fnMessage32(0, DRVM_DISABLE, 0L, 0L, 0L);
595         TRACE("DRVM_DISABLE => %08lx\n", ret);
596 #endif
597         ret = part->fnMessage32(0, DRVM_EXIT, 0L, 0L, 0L);
598         TRACE("DRVM_EXIT => %s\n", WINMM_ErrorToString(ret));
599     }
600
601     return TRUE;
602 }
603
604 /******************************************************************
605  *              Exit
606  *
607  *
608  */
609 void MMDRV_Exit(void)
610 {
611     unsigned int i;
612     TRACE("()\n");
613
614     for (i = 0; i < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]); i++)
615     {
616         if (MM_MLDrvs[i] != NULL)
617         {
618             FIXME("Closing while ll-driver open\n");
619 #if 0
620             /* FIXME: should generate a message depending on type */
621             MMDRV_Free((HANDLE)(i | 0x8000), MM_MLDrvs[i]);
622 #endif
623         }
624     }
625
626     /* unload driver, in reverse order of loading */
627     i = sizeof(MMDrvs) / sizeof(MMDrvs[0]);
628     while (i-- > 0)
629     {
630         MMDRV_ExitPerType(&MMDrvs[i], MMDRV_AUX);
631         MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIXER);
632         MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIIN);
633         MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIOUT);
634         MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEIN);
635         MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEOUT);
636         CloseDriver(MMDrvs[i].hDriver, 0, 0);
637     }
638     if (llTypes[MMDRV_AUX].lpMlds)
639         HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_AUX].lpMlds - 1);
640     if (llTypes[MMDRV_MIXER].lpMlds)
641         HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIXER].lpMlds - 1);
642     if (llTypes[MMDRV_MIDIIN].lpMlds)
643         HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIIN].lpMlds - 1);
644     if (llTypes[MMDRV_MIDIOUT].lpMlds)
645         HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIOUT].lpMlds - 1);
646     if (llTypes[MMDRV_WAVEIN].lpMlds)
647         HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEIN].lpMlds - 1);
648     if (llTypes[MMDRV_WAVEOUT].lpMlds)
649         HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEOUT].lpMlds - 1);
650 }