1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * MMSYSTEM low level drivers handling functions
6 * Copyright 1999 Eric Pouech
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.
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.
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
24 #include "wine/port.h"
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
38 #include "wine/debug.h"
39 #include "wine/exception.h"
44 #include "mmdeviceapi.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
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 */
58 static WINE_LLTYPE llTypes[MMDRV_MAX] = {
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 }
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]))
72 static void MMDRV_Init(void);
74 static void MMDRV_InitSingleType(UINT type) {
75 if (!drivers_loaded) {
81 /**************************************************************************
82 * MMDRV_GetNum [internal]
84 UINT MMDRV_GetNum(UINT type)
86 TRACE("(%04x)\n", type);
87 assert(type < MMDRV_MAX);
88 MMDRV_InitSingleType(type);
89 return llTypes[type].wMaxId;
92 /**************************************************************************
93 * MMDRV_Message [internal]
95 DWORD MMDRV_Message(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1,
98 LPWINE_MM_DRIVER lpDrv;
100 WINE_MM_DRIVER_PART* part;
101 WINE_LLTYPE* llType = &llTypes[mld->type];
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);
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;
114 if (mld->uDeviceID >= llType->wMaxId) {
115 WARN("uDev(%u) requested >= max (%d)\n", mld->uDeviceID, llType->wMaxId);
116 return MMSYSERR_BADDEVICEID;
120 lpDrv = &MMDrvs[mld->mmdIndex];
121 part = &lpDrv->parts[mld->type];
123 assert(part->fnMessage32);
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));
133 /**************************************************************************
134 * MMDRV_Alloc [internal]
136 LPWINE_MLD MMDRV_Alloc(UINT size, UINT type, LPHANDLE hndl, DWORD* dwFlags,
137 DWORD_PTR* dwCallback, DWORD_PTR* dwInstance)
141 TRACE("(%d, %04x, %p, %p, %p, %p)\n",
142 size, type, hndl, dwFlags, dwCallback, dwInstance);
144 mld = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
145 if (!mld) return NULL;
147 /* find an empty slot in MM_MLDrvs table */
148 for (i = 0; i < MAX_MM_MLDRVS; i++) if (!MM_MLDrvs[i]) break;
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);
157 *hndl = (HANDLE)(i | 0x8000);
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
165 ERR("Shouldn't happen. Bad allocation scheme\n");
168 mld->dwFlags = HIWORD(*dwFlags);
169 mld->dwCallback = *dwCallback;
170 mld->dwClientInstance = *dwInstance;
175 /**************************************************************************
176 * MMDRV_Free [internal]
178 void MMDRV_Free(HANDLE hndl, LPWINE_MLD mld)
180 TRACE("(%p, %p)\n", hndl, mld);
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);
190 ERR("Bad Handle %p at %p (not freed)\n", hndl, mld);
193 /**************************************************************************
194 * MMDRV_Open [internal]
196 DWORD MMDRV_Open(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1, DWORD dwFlags)
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);
203 mld->dwDriverInstance = (DWORD_PTR)&dwInstance;
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;
211 mld->mmdIndex = llType->lpMlds[-1].mmdIndex;
212 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
213 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags);
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);
221 if (dwRet == MMSYSERR_NOERROR)
222 mld->dwDriverInstance = dwInstance;
226 /**************************************************************************
227 * MMDRV_Close [internal]
229 DWORD MMDRV_Close(LPWINE_MLD mld, UINT wMsg)
231 TRACE("(%p, %04x)\n", mld, wMsg);
232 return MMDRV_Message(mld, wMsg, 0L, 0L);
235 /**************************************************************************
236 * MMDRV_GetByID [internal]
238 static LPWINE_MLD MMDRV_GetByID(UINT uDevID, UINT type)
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];
248 /**************************************************************************
249 * MMDRV_Get [internal]
251 LPWINE_MLD MMDRV_Get(HANDLE _hndl, UINT type, BOOL bCanBeID)
253 LPWINE_MLD mld = NULL;
254 UINT_PTR hndl = (UINT_PTR)_hndl;
255 TRACE("(%p, %04x, %c)\n", _hndl, type, bCanBeID ? 'Y' : 'N');
257 assert(type < MMDRV_MAX);
258 MMDRV_InitSingleType(type);
260 if (hndl >= llTypes[type].wMaxId &&
261 hndl != (UINT16)-1 && hndl != (UINT)-1) {
263 UINT idx = hndl & ~0x8000;
264 if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
267 mld = MM_MLDrvs[idx];
268 if (mld && mld->type != type) mld = NULL;
278 if (mld == NULL && bCanBeID) {
279 mld = MMDRV_GetByID(hndl, type);
284 /**************************************************************************
285 * MMDRV_PhysicalFeatures [internal]
287 UINT MMDRV_PhysicalFeatures(LPWINE_MLD mld, UINT uMsg,
288 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
290 WINE_MM_DRIVER* lpDrv = &MMDrvs[mld->mmdIndex];
292 TRACE("(%p, %04x, %08lx, %08lx)\n", mld, uMsg, dwParam1, dwParam2);
294 /* all those function calls are undocumented */
296 case DRV_QUERYDRVENTRY:
297 lstrcpynA((LPSTR)dwParam1, lpDrv->drvname, LOWORD(dwParam2));
299 case DRV_QUERYDEVNODE:
300 *(LPDWORD)dwParam1 = 0L; /* should be DevNode */
303 WARN("NIY QueryName\n");
305 case DRV_QUERYDRIVERIDS:
306 WARN("NIY call VxD\n");
307 /* should call VxD MMDEVLDR with (DevNode, dwParam1 and dwParam2) as pmts
308 * dwParam1 is buffer and dwParam2 is sizeof(buffer)
309 * I don't know where the result is stored though
312 case DRV_QUERYMAPPABLE:
313 return (lpDrv->bIsMapper) ? 2 : 0;
315 case DRVM_MAPPER_PREFERRED_GET:
316 /* FIXME: get from registry someday */
317 *((LPDWORD)dwParam1) = -1; /* No preferred device */
318 *((LPDWORD)dwParam2) = 0;
321 case DRV_QUERYDEVICEINTERFACE:
322 case DRV_QUERYDEVICEINTERFACESIZE:
323 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2);
326 WARN("Unknown call %04x\n", uMsg);
327 return MMSYSERR_INVALPARAM;
332 /**************************************************************************
333 * MMDRV_InitPerType [internal]
335 static BOOL MMDRV_InitPerType(LPWINE_MM_DRIVER lpDrv, UINT type, UINT wMsg)
337 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
341 TRACE("(%p, %04x, %04x)\n", lpDrv, type, wMsg);
343 part->nIDMin = part->nIDMax = 0;
345 /* for DRVM_INIT and DRVM_ENABLE, dwParam2 should be PnP node */
346 /* the DRVM_ENABLE is only required when the PnP node is non zero */
347 if (part->fnMessage32) {
348 ret = part->fnMessage32(0, DRVM_INIT, 0L, 0L, 0L);
349 TRACE("DRVM_INIT => %s\n", WINMM_ErrorToString(ret));
351 ret = part->fnMessage32(0, DRVM_ENABLE, 0L, 0L, 0L);
352 TRACE("DRVM_ENABLE => %08lx\n", ret);
354 count = part->fnMessage32(0, wMsg, 0L, 0L, 0L);
358 TRACE("Got %u dev for (%s:%s)\n", count, lpDrv->drvname, llTypes[type].typestr);
363 /* got some drivers */
364 if (lpDrv->bIsMapper) {
365 llTypes[type].nMapper = MMDrvsHi;
369 part->nIDMin = llTypes[type].wMaxId;
370 llTypes[type].wMaxId += count;
371 part->nIDMax = llTypes[type].wMaxId;
373 TRACE("Setting min=%d max=%d (ttop=%d) for (%s:%s)\n",
374 part->nIDMin, part->nIDMax, llTypes[type].wMaxId,
375 lpDrv->drvname, llTypes[type].typestr);
376 /* realloc translation table */
377 if (llTypes[type].lpMlds)
378 llTypes[type].lpMlds = (LPWINE_MLD)
379 HeapReAlloc(GetProcessHeap(), 0, llTypes[type].lpMlds - 1,
380 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
382 llTypes[type].lpMlds = (LPWINE_MLD)
383 HeapAlloc(GetProcessHeap(), 0,
384 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
386 /* re-build the translation table */
387 if (lpDrv->bIsMapper) {
388 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, -1, MMDrvs[llTypes[type].nMapper].drvname);
389 llTypes[type].lpMlds[-1].uDeviceID = (UINT)-1;
390 llTypes[type].lpMlds[-1].type = type;
391 llTypes[type].lpMlds[-1].mmdIndex = llTypes[type].nMapper;
392 llTypes[type].lpMlds[-1].dwDriverInstance = 0;
394 for (i = k = 0; i <= MMDrvsHi; i++) {
395 while (MMDrvs[i].parts[type].nIDMin <= k && k < MMDrvs[i].parts[type].nIDMax) {
396 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, k, MMDrvs[i].drvname);
397 llTypes[type].lpMlds[k].uDeviceID = k;
398 llTypes[type].lpMlds[k].type = type;
399 llTypes[type].lpMlds[k].mmdIndex = i;
400 llTypes[type].lpMlds[k].dwDriverInstance = 0;
407 /**************************************************************************
408 * MMDRV_Install [internal]
410 static BOOL MMDRV_Install(LPCSTR drvRegName, LPCSTR drvFileName, BOOL bIsMapper)
413 LPWINE_MM_DRIVER lpDrv = &MMDrvs[MMDrvsHi];
415 WINEMM_msgFunc32 func;
417 TRACE("('%s', '%s', mapper=%c);\n", drvRegName, drvFileName, bIsMapper ? 'Y' : 'N');
419 for (i = 0; i < MMDrvsHi; i++) {
420 if (!strcmp(drvRegName, MMDrvs[i].drvname)) return FALSE;
423 /* Be sure that size of MMDrvs matches the max number of loadable
425 * If not just increase size of MMDrvs
427 assert(MMDrvsHi <= sizeof(MMDrvs)/sizeof(MMDrvs[0]));
429 memset(lpDrv, 0, sizeof(*lpDrv));
431 if (!(lpDrv->hDriver = OpenDriverA(drvFileName, 0, 0))) {
432 WARN("Couldn't open driver '%s'\n", drvFileName);
436 d = DRIVER_FindFromHDrvr(lpDrv->hDriver);
438 /* Then look for xxxMessage functions */
439 #define AA(_h,_w,_x,_y,_z) \
440 func = (WINEMM_msgFunc##_y) _z ((_h), #_x); \
442 { lpDrv->parts[_w].fnMessage##_y = func; count++; \
443 TRACE("Got %d bit func '%s'\n", _y, #_x); }
446 #define A(_x,_y) AA(d->hModule,_x,_y,32,GetProcAddress)
447 A(MMDRV_AUX, auxMessage);
448 A(MMDRV_MIXER, mxdMessage);
449 A(MMDRV_MIDIIN, midMessage);
450 A(MMDRV_MIDIOUT, modMessage);
451 A(MMDRV_WAVEIN, widMessage);
452 A(MMDRV_WAVEOUT, wodMessage);
458 CloseDriver(lpDrv->hDriver, 0, 0);
459 WARN("No message functions found\n");
463 /* FIXME: being a mapper or not should be known by another way */
464 /* it's known for NE drvs (the description is of the form '*mapper: *'
465 * I don't have any clue for PE drvs
467 lpDrv->bIsMapper = bIsMapper;
468 lpDrv->drvname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(drvRegName) + 1), drvRegName);
470 /* Finish init and get the count of the devices */
472 if (MMDRV_InitPerType(lpDrv, MMDRV_AUX, AUXDM_GETNUMDEVS)) i = 1;
473 if (MMDRV_InitPerType(lpDrv, MMDRV_MIXER, MXDM_GETNUMDEVS)) i = 1;
474 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIIN, MIDM_GETNUMDEVS)) i = 1;
475 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIOUT, MODM_GETNUMDEVS)) i = 1;
476 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEIN, WIDM_GETNUMDEVS)) i = 1;
477 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEOUT, WODM_GETNUMDEVS)) i = 1;
478 /* if all those func calls return FALSE, then the driver must be unloaded */
480 CloseDriver(lpDrv->hDriver, 0, 0);
481 HeapFree(GetProcessHeap(), 0, lpDrv->drvname);
482 WARN("Driver initialization failed\n");
491 /**************************************************************************
494 static void MMDRV_Init(void)
496 IMMDeviceEnumerator *devenum;
504 static const WCHAR wine_info_deviceW[] = {'W','i','n','e',' ',
505 'i','n','f','o',' ','d','e','v','i','c','e',0};
509 init_hr = CoInitialize(NULL);
511 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
512 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&devenum);
514 ERR("CoCreateInstance failed: %08x\n", hr);
518 hr = IMMDeviceEnumerator_GetDevice(devenum, wine_info_deviceW, &device);
519 IMMDeviceEnumerator_Release(devenum);
521 ERR("GetDevice failed: %08x\n", hr);
525 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
527 ERR("OpenPropertyStore failed: %08x\n", hr);
528 IMMDevice_Release(device);
532 hr = IPropertyStore_GetValue(ps,
533 (const PROPERTYKEY *)&DEVPKEY_Device_Driver, &pv);
534 IPropertyStore_Release(ps);
535 IMMDevice_Release(device);
537 ERR("GetValue failed: %08x\n", hr);
541 size = WideCharToMultiByte(CP_ACP, 0, pv.u.pwszVal, -1,
542 NULL, 0, NULL, NULL);
543 drvA = HeapAlloc(GetProcessHeap(), 0, size);
544 WideCharToMultiByte(CP_ACP, 0, pv.u.pwszVal, -1, drvA, size, NULL, NULL);
546 MMDRV_Install(drvA, drvA, FALSE);
548 HeapFree(GetProcessHeap(), 0, drvA);
549 PropVariantClear(&pv);
551 MMDRV_Install("wavemapper", "msacm32.drv", TRUE);
552 MMDRV_Install("midimapper", "midimap.dll", TRUE);
555 if(SUCCEEDED(init_hr))
559 /******************************************************************
564 static BOOL MMDRV_ExitPerType(LPWINE_MM_DRIVER lpDrv, UINT type)
566 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
568 TRACE("(%p, %04x)\n", lpDrv, type);
570 if (part->fnMessage32) {
572 ret = part->fnMessage32(0, DRVM_DISABLE, 0L, 0L, 0L);
573 TRACE("DRVM_DISABLE => %08lx\n", ret);
575 ret = part->fnMessage32(0, DRVM_EXIT, 0L, 0L, 0L);
576 TRACE("DRVM_EXIT => %s\n", WINMM_ErrorToString(ret));
582 /******************************************************************
587 void MMDRV_Exit(void)
592 for (i = 0; i < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]); i++)
594 if (MM_MLDrvs[i] != NULL)
596 FIXME("Closing while ll-driver open\n");
598 /* FIXME: should generate a message depending on type */
599 MMDRV_Free((HANDLE)(i | 0x8000), MM_MLDrvs[i]);
604 /* unload driver, in reverse order of loading */
605 i = sizeof(MMDrvs) / sizeof(MMDrvs[0]);
608 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_AUX);
609 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIXER);
610 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIIN);
611 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIOUT);
612 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEIN);
613 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEOUT);
614 CloseDriver(MMDrvs[i].hDriver, 0, 0);
616 if (llTypes[MMDRV_AUX].lpMlds)
617 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_AUX].lpMlds - 1);
618 if (llTypes[MMDRV_MIXER].lpMlds)
619 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIXER].lpMlds - 1);
620 if (llTypes[MMDRV_MIDIIN].lpMlds)
621 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIIN].lpMlds - 1);
622 if (llTypes[MMDRV_MIDIOUT].lpMlds)
623 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIOUT].lpMlds - 1);
624 if (llTypes[MMDRV_WAVEIN].lpMlds)
625 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEIN].lpMlds - 1);
626 if (llTypes[MMDRV_WAVEOUT].lpMlds)
627 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEOUT].lpMlds - 1);