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 /* Default set of drivers to be loaded */
49 #define WINE_DEFAULT_WINMM_DRIVER "alsa,oss,coreaudio"
51 /* each known type of driver has an instance of this structure */
52 typedef struct tagWINE_LLTYPE {
53 /* those attributes depend on the specification of the type */
54 LPCSTR typestr; /* name (for debugging) */
55 /* those attributes reflect the loaded/current situation for the type */
56 UINT wMaxId; /* number of loaded devices (sum across all loaded drivers) */
57 LPWINE_MLD lpMlds; /* "static" mlds to access the part though device IDs */
58 int nMapper; /* index to mapper */
61 static WINE_LLTYPE llTypes[MMDRV_MAX] = {
63 { "Mixer", 0, 0, -1 },
64 { "MidiIn", 0, 0, -1 },
65 { "MidiOut", 0, 0, -1 },
66 { "WaveIn", 0, 0, -1 },
67 { "WaveOut", 0, 0, -1 }
70 static int drivers_loaded, MMDrvsHi;
71 static WINE_MM_DRIVER MMDrvs[8];
72 static LPWINE_MLD MM_MLDrvs[40];
73 #define MAX_MM_MLDRVS (sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]))
75 static void MMDRV_Init(void);
77 static void MMDRV_InitSingleType(UINT type) {
78 if (!drivers_loaded) {
84 /**************************************************************************
85 * MMDRV_GetNum [internal]
87 UINT MMDRV_GetNum(UINT type)
89 TRACE("(%04x)\n", type);
90 assert(type < MMDRV_MAX);
91 MMDRV_InitSingleType(type);
92 return llTypes[type].wMaxId;
95 /**************************************************************************
96 * MMDRV_Message [internal]
98 DWORD MMDRV_Message(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1,
101 LPWINE_MM_DRIVER lpDrv;
103 WINE_MM_DRIVER_PART* part;
104 WINE_LLTYPE* llType = &llTypes[mld->type];
106 TRACE("(%s %d %u 0x%08lx 0x%08lx 0x%08lx)\n",
107 llTypes[mld->type].typestr, mld->uDeviceID, wMsg,
108 mld->dwDriverInstance, dwParam1, dwParam2);
110 if ((UINT16)mld->uDeviceID == (UINT16)-1) {
111 if (llType->nMapper == -1) {
112 WARN("uDev=-1 requested on non-mapped ll type %s\n",
113 llTypes[mld->type].typestr);
114 return MMSYSERR_BADDEVICEID;
117 if (mld->uDeviceID >= llType->wMaxId) {
118 WARN("uDev(%u) requested >= max (%d)\n", mld->uDeviceID, llType->wMaxId);
119 return MMSYSERR_BADDEVICEID;
123 lpDrv = &MMDrvs[mld->mmdIndex];
124 part = &lpDrv->parts[mld->type];
126 assert(part->fnMessage32);
128 TRACE("Calling message(dev=%d msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
129 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
130 ret = part->fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
131 TRACE("=> %s\n", WINMM_ErrorToString(ret));
136 /**************************************************************************
137 * MMDRV_Alloc [internal]
139 LPWINE_MLD MMDRV_Alloc(UINT size, UINT type, LPHANDLE hndl, DWORD* dwFlags,
140 DWORD_PTR* dwCallback, DWORD_PTR* dwInstance)
144 TRACE("(%d, %04x, %p, %p, %p, %p)\n",
145 size, type, hndl, dwFlags, dwCallback, dwInstance);
147 mld = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
148 if (!mld) return NULL;
150 /* find an empty slot in MM_MLDrvs table */
151 for (i = 0; i < MAX_MM_MLDRVS; i++) if (!MM_MLDrvs[i]) break;
153 if (i == MAX_MM_MLDRVS) {
154 /* the MM_MLDrvs table could be made growable in the future if needed */
155 ERR("Too many open drivers\n");
156 HeapFree(GetProcessHeap(), 0, mld);
160 *hndl = (HANDLE)(i | 0x8000);
163 if ((UINT_PTR)*hndl < MMDRV_GetNum(type) || ((UINT_PTR)*hndl >> 16)) {
164 /* FIXME: those conditions must be fulfilled so that:
165 * - we can distinguish between device IDs and handles
166 * - we can use handles as 16 or 32 bit entities
168 ERR("Shouldn't happen. Bad allocation scheme\n");
171 mld->dwFlags = HIWORD(*dwFlags);
172 mld->dwCallback = *dwCallback;
173 mld->dwClientInstance = *dwInstance;
178 /**************************************************************************
179 * MMDRV_Free [internal]
181 void MMDRV_Free(HANDLE hndl, LPWINE_MLD mld)
183 TRACE("(%p, %p)\n", hndl, mld);
185 if ((UINT_PTR)hndl & 0x8000) {
186 UINT_PTR idx = (UINT_PTR)hndl & ~0x8000;
187 if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
188 MM_MLDrvs[idx] = NULL;
189 HeapFree(GetProcessHeap(), 0, mld);
193 ERR("Bad Handle %p at %p (not freed)\n", hndl, mld);
196 /**************************************************************************
197 * MMDRV_Open [internal]
199 DWORD MMDRV_Open(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1, DWORD dwFlags)
201 DWORD dwRet = MMSYSERR_BADDEVICEID;
202 DWORD_PTR dwInstance;
203 WINE_LLTYPE* llType = &llTypes[mld->type];
204 TRACE("(%p, %04x, 0x%08lx, 0x%08x)\n", mld, wMsg, dwParam1, dwFlags);
206 mld->dwDriverInstance = (DWORD_PTR)&dwInstance;
208 if (mld->uDeviceID == (UINT)-1 || mld->uDeviceID == (UINT16)-1) {
209 TRACE("MAPPER mode requested !\n");
210 if (llType->nMapper == -1) {
211 WARN("Mapper not supported for type %s\n", llTypes[mld->type].typestr);
212 return MMSYSERR_BADDEVICEID;
214 mld->mmdIndex = llType->lpMlds[-1].mmdIndex;
215 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
216 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags);
218 if (mld->uDeviceID < llType->wMaxId) {
219 mld->mmdIndex = llType->lpMlds[mld->uDeviceID].mmdIndex;
220 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
221 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags);
224 if (dwRet == MMSYSERR_NOERROR)
225 mld->dwDriverInstance = dwInstance;
229 /**************************************************************************
230 * MMDRV_Close [internal]
232 DWORD MMDRV_Close(LPWINE_MLD mld, UINT wMsg)
234 TRACE("(%p, %04x)\n", mld, wMsg);
235 return MMDRV_Message(mld, wMsg, 0L, 0L);
238 /**************************************************************************
239 * MMDRV_GetByID [internal]
241 static LPWINE_MLD MMDRV_GetByID(UINT uDevID, UINT type)
243 TRACE("(%04x, %04x)\n", uDevID, type);
244 if (uDevID < llTypes[type].wMaxId)
245 return &llTypes[type].lpMlds[uDevID];
246 if ((uDevID == (UINT16)-1 || uDevID == (UINT)-1) && llTypes[type].nMapper != -1)
247 return &llTypes[type].lpMlds[-1];
251 /**************************************************************************
252 * MMDRV_Get [internal]
254 LPWINE_MLD MMDRV_Get(HANDLE _hndl, UINT type, BOOL bCanBeID)
256 LPWINE_MLD mld = NULL;
257 UINT_PTR hndl = (UINT_PTR)_hndl;
258 TRACE("(%p, %04x, %c)\n", _hndl, type, bCanBeID ? 'Y' : 'N');
260 assert(type < MMDRV_MAX);
261 MMDRV_InitSingleType(type);
263 if (hndl >= llTypes[type].wMaxId &&
264 hndl != (UINT16)-1 && hndl != (UINT)-1) {
266 UINT idx = hndl & ~0x8000;
267 if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
270 mld = MM_MLDrvs[idx];
271 if (mld && mld->type != type) mld = NULL;
281 if (mld == NULL && bCanBeID) {
282 mld = MMDRV_GetByID(hndl, type);
287 /**************************************************************************
288 * MMDRV_GetRelated [internal]
290 LPWINE_MLD MMDRV_GetRelated(HANDLE hndl, UINT srcType,
291 BOOL bSrcCanBeID, UINT dstType)
294 TRACE("(%p, %04x, %c, %04x)\n",
295 hndl, srcType, bSrcCanBeID ? 'Y' : 'N', dstType);
297 if ((mld = MMDRV_Get(hndl, srcType, bSrcCanBeID)) != NULL) {
298 WINE_MM_DRIVER_PART* part = &MMDrvs[mld->mmdIndex].parts[dstType];
299 if (part->nIDMin < part->nIDMax)
300 return MMDRV_GetByID(part->nIDMin, dstType);
305 /**************************************************************************
306 * MMDRV_PhysicalFeatures [internal]
308 UINT MMDRV_PhysicalFeatures(LPWINE_MLD mld, UINT uMsg,
309 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
311 WINE_MM_DRIVER* lpDrv = &MMDrvs[mld->mmdIndex];
313 TRACE("(%p, %04x, %08lx, %08lx)\n", mld, uMsg, dwParam1, dwParam2);
315 /* all those function calls are undocumented */
317 case DRV_QUERYDRVENTRY:
318 lstrcpynA((LPSTR)dwParam1, lpDrv->drvname, LOWORD(dwParam2));
320 case DRV_QUERYDEVNODE:
321 *(LPDWORD)dwParam1 = 0L; /* should be DevNode */
324 WARN("NIY QueryName\n");
326 case DRV_QUERYDRIVERIDS:
327 WARN("NIY call VxD\n");
328 /* should call VxD MMDEVLDR with (DevNode, dwParam1 and dwParam2) as pmts
329 * dwParam1 is buffer and dwParam2 is sizeof(buffer)
330 * I don't know where the result is stored though
333 case DRV_QUERYMAPPABLE:
334 return (lpDrv->bIsMapper) ? 2 : 0;
336 case DRVM_MAPPER_PREFERRED_GET:
337 /* FIXME: get from registry someday */
338 *((LPDWORD)dwParam1) = -1; /* No preferred device */
339 *((LPDWORD)dwParam2) = 0;
342 case DRV_QUERYDEVICEINTERFACE:
343 case DRV_QUERYDEVICEINTERFACESIZE:
344 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2);
346 case DRV_QUERYDSOUNDIFACE: /* Wine-specific: Retrieve DirectSound interface */
347 case DRV_QUERYDSOUNDDESC: /* Wine-specific: Retrieve DirectSound driver description*/
348 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2);
351 WARN("Unknown call %04x\n", uMsg);
352 return MMSYSERR_INVALPARAM;
357 /**************************************************************************
358 * MMDRV_InitPerType [internal]
360 static BOOL MMDRV_InitPerType(LPWINE_MM_DRIVER lpDrv, UINT type, UINT wMsg)
362 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
366 TRACE("(%p, %04x, %04x)\n", lpDrv, type, wMsg);
368 part->nIDMin = part->nIDMax = 0;
370 /* for DRVM_INIT and DRVM_ENABLE, dwParam2 should be PnP node */
371 /* the DRVM_ENABLE is only required when the PnP node is non zero */
372 if (part->fnMessage32) {
373 ret = part->fnMessage32(0, DRVM_INIT, 0L, 0L, 0L);
374 TRACE("DRVM_INIT => %s\n", WINMM_ErrorToString(ret));
376 ret = part->fnMessage32(0, DRVM_ENABLE, 0L, 0L, 0L);
377 TRACE("DRVM_ENABLE => %08lx\n", ret);
379 count = part->fnMessage32(0, wMsg, 0L, 0L, 0L);
383 TRACE("Got %u dev for (%s:%s)\n", count, lpDrv->drvname, llTypes[type].typestr);
388 /* got some drivers */
389 if (lpDrv->bIsMapper) {
390 llTypes[type].nMapper = MMDrvsHi;
394 part->nIDMin = llTypes[type].wMaxId;
395 llTypes[type].wMaxId += count;
396 part->nIDMax = llTypes[type].wMaxId;
398 TRACE("Setting min=%d max=%d (ttop=%d) for (%s:%s)\n",
399 part->nIDMin, part->nIDMax, llTypes[type].wMaxId,
400 lpDrv->drvname, llTypes[type].typestr);
401 /* realloc translation table */
402 if (llTypes[type].lpMlds)
403 llTypes[type].lpMlds = (LPWINE_MLD)
404 HeapReAlloc(GetProcessHeap(), 0, llTypes[type].lpMlds - 1,
405 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
407 llTypes[type].lpMlds = (LPWINE_MLD)
408 HeapAlloc(GetProcessHeap(), 0,
409 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
411 /* re-build the translation table */
412 if (lpDrv->bIsMapper) {
413 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, -1, MMDrvs[llTypes[type].nMapper].drvname);
414 llTypes[type].lpMlds[-1].uDeviceID = (UINT)-1;
415 llTypes[type].lpMlds[-1].type = type;
416 llTypes[type].lpMlds[-1].mmdIndex = llTypes[type].nMapper;
417 llTypes[type].lpMlds[-1].dwDriverInstance = 0;
419 for (i = k = 0; i <= MMDrvsHi; i++) {
420 while (MMDrvs[i].parts[type].nIDMin <= k && k < MMDrvs[i].parts[type].nIDMax) {
421 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, k, MMDrvs[i].drvname);
422 llTypes[type].lpMlds[k].uDeviceID = k;
423 llTypes[type].lpMlds[k].type = type;
424 llTypes[type].lpMlds[k].mmdIndex = i;
425 llTypes[type].lpMlds[k].dwDriverInstance = 0;
432 /**************************************************************************
433 * MMDRV_Install [internal]
435 static BOOL MMDRV_Install(LPCSTR drvRegName, LPCSTR drvFileName, BOOL bIsMapper)
438 LPWINE_MM_DRIVER lpDrv = &MMDrvs[MMDrvsHi];
440 WINEMM_msgFunc32 func;
442 TRACE("('%s', '%s', mapper=%c);\n", drvRegName, drvFileName, bIsMapper ? 'Y' : 'N');
444 for (i = 0; i < MMDrvsHi; i++) {
445 if (!strcmp(drvRegName, MMDrvs[i].drvname)) return FALSE;
448 /* Be sure that size of MMDrvs matches the max number of loadable
450 * If not just increase size of MMDrvs
452 assert(MMDrvsHi <= sizeof(MMDrvs)/sizeof(MMDrvs[0]));
454 memset(lpDrv, 0, sizeof(*lpDrv));
456 if (!(lpDrv->hDriver = OpenDriverA(drvFileName, 0, 0))) {
457 WARN("Couldn't open driver '%s'\n", drvFileName);
461 d = DRIVER_FindFromHDrvr(lpDrv->hDriver);
463 /* Then look for xxxMessage functions */
464 #define AA(_h,_w,_x,_y,_z) \
465 func = (WINEMM_msgFunc##_y) _z ((_h), #_x); \
467 { lpDrv->parts[_w].fnMessage##_y = func; count++; \
468 TRACE("Got %d bit func '%s'\n", _y, #_x); }
471 #define A(_x,_y) AA(d->hModule,_x,_y,32,GetProcAddress)
472 A(MMDRV_AUX, auxMessage);
473 A(MMDRV_MIXER, mxdMessage);
474 A(MMDRV_MIDIIN, midMessage);
475 A(MMDRV_MIDIOUT, modMessage);
476 A(MMDRV_WAVEIN, widMessage);
477 A(MMDRV_WAVEOUT, wodMessage);
483 CloseDriver(lpDrv->hDriver, 0, 0);
484 WARN("No message functions found\n");
488 /* FIXME: being a mapper or not should be known by another way */
489 /* it's known for NE drvs (the description is of the form '*mapper: *'
490 * I don't have any clue for PE drvs
492 lpDrv->bIsMapper = bIsMapper;
493 lpDrv->drvname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(drvRegName) + 1), drvRegName);
495 /* Finish init and get the count of the devices */
497 if (MMDRV_InitPerType(lpDrv, MMDRV_AUX, AUXDM_GETNUMDEVS)) i = 1;
498 if (MMDRV_InitPerType(lpDrv, MMDRV_MIXER, MXDM_GETNUMDEVS)) i = 1;
499 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIIN, MIDM_GETNUMDEVS)) i = 1;
500 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIOUT, MODM_GETNUMDEVS)) i = 1;
501 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEIN, WIDM_GETNUMDEVS)) i = 1;
502 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEOUT, WODM_GETNUMDEVS)) i = 1;
503 /* if all those func calls return FALSE, then the driver must be unloaded */
505 CloseDriver(lpDrv->hDriver, 0, 0);
506 HeapFree(GetProcessHeap(), 0, lpDrv->drvname);
507 WARN("Driver initialization failed\n");
516 /**************************************************************************
519 static void MMDRV_Init(void)
521 IMMDeviceEnumerator *devenum;
529 static const WCHAR wine_info_deviceW[] = {'W','i','n','e',' ',
530 'i','n','f','o',' ','d','e','v','i','c','e',0};
534 init_hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
536 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
537 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&devenum);
539 ERR("CoCreateInstance failed: %08x\n", hr);
543 hr = IMMDeviceEnumerator_GetDevice(devenum, wine_info_deviceW, &device);
544 IMMDeviceEnumerator_Release(devenum);
546 ERR("GetDevice failed: %08x\n", hr);
550 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
552 ERR("OpenPropertyStore failed: %08x\n", hr);
553 IMMDevice_Release(device);
557 hr = IPropertyStore_GetValue(ps,
558 (const PROPERTYKEY *)&DEVPKEY_Device_Driver, &pv);
559 IPropertyStore_Release(ps);
560 IMMDevice_Release(device);
562 ERR("GetValue failed: %08x\n", hr);
566 size = WideCharToMultiByte(CP_ACP, 0, pv.u.pwszVal, -1,
567 NULL, 0, NULL, NULL);
568 drvA = HeapAlloc(GetProcessHeap(), 0, size);
569 WideCharToMultiByte(CP_ACP, 0, pv.u.pwszVal, -1, drvA, size, NULL, NULL);
571 MMDRV_Install(drvA, drvA, FALSE);
573 HeapFree(GetProcessHeap(), 0, drvA);
574 PropVariantClear(&pv);
576 MMDRV_Install("wavemapper", "msacm32.drv", TRUE);
577 MMDRV_Install("midimapper", "midimap.dll", TRUE);
580 if(SUCCEEDED(init_hr))
584 /******************************************************************
589 static BOOL MMDRV_ExitPerType(LPWINE_MM_DRIVER lpDrv, UINT type)
591 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
593 TRACE("(%p, %04x)\n", lpDrv, type);
595 if (part->fnMessage32) {
597 ret = part->fnMessage32(0, DRVM_DISABLE, 0L, 0L, 0L);
598 TRACE("DRVM_DISABLE => %08lx\n", ret);
600 ret = part->fnMessage32(0, DRVM_EXIT, 0L, 0L, 0L);
601 TRACE("DRVM_EXIT => %s\n", WINMM_ErrorToString(ret));
607 /******************************************************************
612 void MMDRV_Exit(void)
617 for (i = 0; i < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]); i++)
619 if (MM_MLDrvs[i] != NULL)
621 FIXME("Closing while ll-driver open\n");
623 /* FIXME: should generate a message depending on type */
624 MMDRV_Free((HANDLE)(i | 0x8000), MM_MLDrvs[i]);
629 /* unload driver, in reverse order of loading */
630 i = sizeof(MMDrvs) / sizeof(MMDrvs[0]);
633 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_AUX);
634 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIXER);
635 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIIN);
636 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIOUT);
637 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEIN);
638 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEOUT);
639 CloseDriver(MMDrvs[i].hDriver, 0, 0);
641 if (llTypes[MMDRV_AUX].lpMlds)
642 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_AUX].lpMlds - 1);
643 if (llTypes[MMDRV_MIXER].lpMlds)
644 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIXER].lpMlds - 1);
645 if (llTypes[MMDRV_MIDIIN].lpMlds)
646 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIIN].lpMlds - 1);
647 if (llTypes[MMDRV_MIDIOUT].lpMlds)
648 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIOUT].lpMlds - 1);
649 if (llTypes[MMDRV_WAVEIN].lpMlds)
650 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEIN].lpMlds - 1);
651 if (llTypes[MMDRV_WAVEOUT].lpMlds)
652 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEOUT].lpMlds - 1);