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"
34 #include "wine/debug.h"
35 #include "wine/exception.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
39 /* Default set of drivers to be loaded */
40 #define WINE_DEFAULT_WINMM_DRIVER "alsa,oss,coreaudio,esd"
42 /* each known type of driver has an instance of this structure */
43 typedef struct tagWINE_LLTYPE {
44 /* those attributes depend on the specification of the type */
45 LPCSTR typestr; /* name (for debugging) */
46 /* those attributes reflect the loaded/current situation for the type */
47 UINT wMaxId; /* number of loaded devices (sum across all loaded drivers) */
48 LPWINE_MLD lpMlds; /* "static" mlds to access the part though device IDs */
49 int nMapper; /* index to mapper */
52 static WINE_LLTYPE llTypes[MMDRV_MAX] = {
54 { "Mixer", 0, 0, -1 },
55 { "MidiIn", 0, 0, -1 },
56 { "MidiOut", 0, 0, -1 },
57 { "WaveIn", 0, 0, -1 },
58 { "WaveOut", 0, 0, -1 }
61 static int drivers_loaded, MMDrvsHi;
62 static WINE_MM_DRIVER MMDrvs[8];
63 static LPWINE_MLD MM_MLDrvs[40];
64 #define MAX_MM_MLDRVS (sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]))
66 static void MMDRV_Init(void);
68 static void MMDRV_InitSingleType(UINT type) {
69 if (!drivers_loaded) {
75 /**************************************************************************
76 * MMDRV_GetNum [internal]
78 UINT MMDRV_GetNum(UINT type)
80 TRACE("(%04x)\n", type);
81 assert(type < MMDRV_MAX);
82 MMDRV_InitSingleType(type);
83 return llTypes[type].wMaxId;
86 /**************************************************************************
87 * MMDRV_Message [internal]
89 DWORD MMDRV_Message(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1,
92 LPWINE_MM_DRIVER lpDrv;
94 WINE_MM_DRIVER_PART* part;
95 WINE_LLTYPE* llType = &llTypes[mld->type];
97 TRACE("(%s %u %u 0x%08lx 0x%08lx 0x%08lx)\n",
98 llTypes[mld->type].typestr, mld->uDeviceID, wMsg,
99 mld->dwDriverInstance, dwParam1, dwParam2);
101 if (mld->uDeviceID == (UINT16)-1) {
102 if (llType->nMapper == -1) {
103 WARN("uDev=-1 requested on non-mapped ll type %s\n",
104 llTypes[mld->type].typestr);
105 return MMSYSERR_BADDEVICEID;
108 if (mld->uDeviceID >= llType->wMaxId) {
109 WARN("uDev(%u) requested >= max (%d)\n", mld->uDeviceID, llType->wMaxId);
110 return MMSYSERR_BADDEVICEID;
114 lpDrv = &MMDrvs[mld->mmdIndex];
115 part = &lpDrv->parts[mld->type];
117 assert(part->fnMessage32);
119 TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
120 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
121 ret = part->fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
122 TRACE("=> %s\n", WINMM_ErrorToString(ret));
127 /**************************************************************************
128 * MMDRV_Alloc [internal]
130 LPWINE_MLD MMDRV_Alloc(UINT size, UINT type, LPHANDLE hndl, DWORD* dwFlags,
131 DWORD_PTR* dwCallback, DWORD_PTR* dwInstance)
135 TRACE("(%d, %04x, %p, %p, %p, %p)\n",
136 size, type, hndl, dwFlags, dwCallback, dwInstance);
138 mld = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
139 if (!mld) return NULL;
141 /* find an empty slot in MM_MLDrvs table */
142 for (i = 0; i < MAX_MM_MLDRVS; i++) if (!MM_MLDrvs[i]) break;
144 if (i == MAX_MM_MLDRVS) {
145 /* the MM_MLDrvs table could be made growable in the future if needed */
146 ERR("Too many open drivers\n");
147 HeapFree(GetProcessHeap(), 0, mld);
151 *hndl = (HANDLE)(i | 0x8000);
154 if ((UINT_PTR)*hndl < MMDRV_GetNum(type) || ((UINT_PTR)*hndl >> 16)) {
155 /* FIXME: those conditions must be fulfilled so that:
156 * - we can distinguish between device IDs and handles
157 * - we can use handles as 16 or 32 bit entities
159 ERR("Shouldn't happen. Bad allocation scheme\n");
162 mld->dwFlags = HIWORD(*dwFlags);
163 mld->dwCallback = *dwCallback;
164 mld->dwClientInstance = *dwInstance;
169 /**************************************************************************
170 * MMDRV_Free [internal]
172 void MMDRV_Free(HANDLE hndl, LPWINE_MLD mld)
174 TRACE("(%p, %p)\n", hndl, mld);
176 if ((UINT_PTR)hndl & 0x8000) {
177 UINT_PTR idx = (UINT_PTR)hndl & ~0x8000;
178 if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
179 MM_MLDrvs[idx] = NULL;
180 HeapFree(GetProcessHeap(), 0, mld);
184 ERR("Bad Handle %p at %p (not freed)\n", hndl, mld);
187 /**************************************************************************
188 * MMDRV_Open [internal]
190 DWORD MMDRV_Open(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1, DWORD dwFlags)
192 DWORD dwRet = MMSYSERR_BADDEVICEID;
193 DWORD_PTR dwInstance;
194 WINE_LLTYPE* llType = &llTypes[mld->type];
195 TRACE("(%p, %04x, 0x%08lx, 0x%08x)\n", mld, wMsg, dwParam1, dwFlags);
197 mld->dwDriverInstance = (DWORD_PTR)&dwInstance;
199 if (mld->uDeviceID == (UINT)-1 || mld->uDeviceID == (UINT16)-1) {
200 TRACE("MAPPER mode requested !\n");
201 if (llType->nMapper == -1) {
202 WARN("Mapper not supported for type %s\n", llTypes[mld->type].typestr);
203 return MMSYSERR_BADDEVICEID;
205 mld->uDeviceID = (UINT16)-1;
206 mld->mmdIndex = llType->lpMlds[-1].mmdIndex;
207 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
208 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags);
210 if (mld->uDeviceID < llType->wMaxId) {
211 mld->mmdIndex = llType->lpMlds[mld->uDeviceID].mmdIndex;
212 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
213 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags);
216 if (dwRet == MMSYSERR_NOERROR)
217 mld->dwDriverInstance = dwInstance;
221 /**************************************************************************
222 * MMDRV_Close [internal]
224 DWORD MMDRV_Close(LPWINE_MLD mld, UINT wMsg)
226 TRACE("(%p, %04x)\n", mld, wMsg);
227 return MMDRV_Message(mld, wMsg, 0L, 0L);
230 /**************************************************************************
231 * MMDRV_GetByID [internal]
233 static LPWINE_MLD MMDRV_GetByID(UINT uDevID, UINT type)
235 TRACE("(%04x, %04x)\n", uDevID, type);
236 if (uDevID < llTypes[type].wMaxId)
237 return &llTypes[type].lpMlds[uDevID];
238 if ((uDevID == (UINT16)-1 || uDevID == (UINT)-1) && llTypes[type].nMapper != -1)
239 return &llTypes[type].lpMlds[-1];
243 /**************************************************************************
244 * MMDRV_Get [internal]
246 LPWINE_MLD MMDRV_Get(HANDLE _hndl, UINT type, BOOL bCanBeID)
248 LPWINE_MLD mld = NULL;
249 UINT_PTR hndl = (UINT_PTR)_hndl;
250 TRACE("(%p, %04x, %c)\n", _hndl, type, bCanBeID ? 'Y' : 'N');
252 assert(type < MMDRV_MAX);
253 MMDRV_InitSingleType(type);
255 if (hndl >= llTypes[type].wMaxId &&
256 hndl != (UINT16)-1 && hndl != (UINT)-1) {
258 UINT idx = hndl & ~0x8000;
259 if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
262 mld = MM_MLDrvs[idx];
263 if (mld && mld->type != type) mld = NULL;
273 if (mld == NULL && bCanBeID) {
274 mld = MMDRV_GetByID(hndl, type);
279 /**************************************************************************
280 * MMDRV_GetRelated [internal]
282 LPWINE_MLD MMDRV_GetRelated(HANDLE hndl, UINT srcType,
283 BOOL bSrcCanBeID, UINT dstType)
286 TRACE("(%p, %04x, %c, %04x)\n",
287 hndl, srcType, bSrcCanBeID ? 'Y' : 'N', dstType);
289 if ((mld = MMDRV_Get(hndl, srcType, bSrcCanBeID)) != NULL) {
290 WINE_MM_DRIVER_PART* part = &MMDrvs[mld->mmdIndex].parts[dstType];
291 if (part->nIDMin < part->nIDMax)
292 return MMDRV_GetByID(part->nIDMin, dstType);
297 /**************************************************************************
298 * MMDRV_PhysicalFeatures [internal]
300 UINT MMDRV_PhysicalFeatures(LPWINE_MLD mld, UINT uMsg,
301 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
303 WINE_MM_DRIVER* lpDrv = &MMDrvs[mld->mmdIndex];
305 TRACE("(%p, %04x, %08lx, %08lx)\n", mld, uMsg, dwParam1, dwParam2);
307 /* all those function calls are undocumented */
309 case DRV_QUERYDRVENTRY:
310 lstrcpynA((LPSTR)dwParam1, lpDrv->drvname, LOWORD(dwParam2));
312 case DRV_QUERYDEVNODE:
313 *(LPDWORD)dwParam1 = 0L; /* should be DevNode */
316 WARN("NIY QueryName\n");
318 case DRV_QUERYDRIVERIDS:
319 WARN("NIY call VxD\n");
320 /* should call VxD MMDEVLDR with (DevNode, dwParam1 and dwParam2) as pmts
321 * dwParam1 is buffer and dwParam2 is sizeof(buffer)
322 * I don't know where the result is stored though
325 case DRV_QUERYMAPPABLE:
326 return (lpDrv->bIsMapper) ? 2 : 0;
328 case DRVM_MAPPER_PREFERRED_GET:
329 /* FIXME: get from registry someday */
330 *((LPDWORD)dwParam1) = -1; /* No preferred device */
331 *((LPDWORD)dwParam2) = 0;
334 case DRV_QUERYDEVICEINTERFACE:
335 case DRV_QUERYDEVICEINTERFACESIZE:
336 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2);
338 case DRV_QUERYDSOUNDIFACE: /* Wine-specific: Retrieve DirectSound interface */
339 case DRV_QUERYDSOUNDDESC: /* Wine-specific: Retrieve DirectSound driver description*/
340 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2);
343 WARN("Unknown call %04x\n", uMsg);
344 return MMSYSERR_INVALPARAM;
349 /**************************************************************************
350 * MMDRV_InitPerType [internal]
352 static BOOL MMDRV_InitPerType(LPWINE_MM_DRIVER lpDrv, UINT type, UINT wMsg)
354 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
358 TRACE("(%p, %04x, %04x)\n", lpDrv, type, wMsg);
360 part->nIDMin = part->nIDMax = 0;
362 /* for DRVM_INIT and DRVM_ENABLE, dwParam2 should be PnP node */
363 /* the DRVM_ENABLE is only required when the PnP node is non zero */
364 if (part->fnMessage32) {
365 ret = part->fnMessage32(0, DRVM_INIT, 0L, 0L, 0L);
366 TRACE("DRVM_INIT => %s\n", WINMM_ErrorToString(ret));
368 ret = part->fnMessage32(0, DRVM_ENABLE, 0L, 0L, 0L);
369 TRACE("DRVM_ENABLE => %08lx\n", ret);
371 count = part->fnMessage32(0, wMsg, 0L, 0L, 0L);
375 TRACE("Got %u dev for (%s:%s)\n", count, lpDrv->drvname, llTypes[type].typestr);
380 /* got some drivers */
381 if (lpDrv->bIsMapper) {
382 llTypes[type].nMapper = MMDrvsHi;
386 part->nIDMin = llTypes[type].wMaxId;
387 llTypes[type].wMaxId += count;
388 part->nIDMax = llTypes[type].wMaxId;
390 TRACE("Setting min=%d max=%d (ttop=%d) for (%s:%s)\n",
391 part->nIDMin, part->nIDMax, llTypes[type].wMaxId,
392 lpDrv->drvname, llTypes[type].typestr);
393 /* realloc translation table */
394 if (llTypes[type].lpMlds)
395 llTypes[type].lpMlds = (LPWINE_MLD)
396 HeapReAlloc(GetProcessHeap(), 0, llTypes[type].lpMlds - 1,
397 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
399 llTypes[type].lpMlds = (LPWINE_MLD)
400 HeapAlloc(GetProcessHeap(), 0,
401 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
403 /* re-build the translation table */
404 if (lpDrv->bIsMapper) {
405 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, -1, MMDrvs[llTypes[type].nMapper].drvname);
406 llTypes[type].lpMlds[-1].uDeviceID = (UINT16)-1;
407 llTypes[type].lpMlds[-1].type = type;
408 llTypes[type].lpMlds[-1].mmdIndex = llTypes[type].nMapper;
409 llTypes[type].lpMlds[-1].dwDriverInstance = 0;
411 for (i = k = 0; i <= MMDrvsHi; i++) {
412 while (MMDrvs[i].parts[type].nIDMin <= k && k < MMDrvs[i].parts[type].nIDMax) {
413 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, k, MMDrvs[i].drvname);
414 llTypes[type].lpMlds[k].uDeviceID = k;
415 llTypes[type].lpMlds[k].type = type;
416 llTypes[type].lpMlds[k].mmdIndex = i;
417 llTypes[type].lpMlds[k].dwDriverInstance = 0;
424 /**************************************************************************
425 * MMDRV_Install [internal]
427 static BOOL MMDRV_Install(LPCSTR drvRegName, LPCSTR drvFileName, BOOL bIsMapper)
430 LPWINE_MM_DRIVER lpDrv = &MMDrvs[MMDrvsHi];
432 WINEMM_msgFunc32 func;
434 TRACE("('%s', '%s', mapper=%c);\n", drvRegName, drvFileName, bIsMapper ? 'Y' : 'N');
436 for (i = 0; i < MMDrvsHi; i++) {
437 if (!strcmp(drvRegName, MMDrvs[i].drvname)) return FALSE;
440 /* Be sure that size of MMDrvs matches the max number of loadable
442 * If not just increase size of MMDrvs
444 assert(MMDrvsHi <= sizeof(MMDrvs)/sizeof(MMDrvs[0]));
446 memset(lpDrv, 0, sizeof(*lpDrv));
448 if (!(lpDrv->hDriver = OpenDriverA(drvFileName, 0, 0))) {
449 WARN("Couldn't open driver '%s'\n", drvFileName);
453 d = DRIVER_FindFromHDrvr(lpDrv->hDriver);
455 /* Then look for xxxMessage functions */
456 #define AA(_h,_w,_x,_y,_z) \
457 func = (WINEMM_msgFunc##_y) _z ((_h), #_x); \
459 { lpDrv->parts[_w].fnMessage##_y = func; count++; \
460 TRACE("Got %d bit func '%s'\n", _y, #_x); }
463 #define A(_x,_y) AA(d->hModule,_x,_y,32,GetProcAddress)
464 A(MMDRV_AUX, auxMessage);
465 A(MMDRV_MIXER, mxdMessage);
466 A(MMDRV_MIDIIN, midMessage);
467 A(MMDRV_MIDIOUT, modMessage);
468 A(MMDRV_WAVEIN, widMessage);
469 A(MMDRV_WAVEOUT, wodMessage);
475 CloseDriver(lpDrv->hDriver, 0, 0);
476 WARN("No message functions found\n");
480 /* FIXME: being a mapper or not should be known by another way */
481 /* it's known for NE drvs (the description is of the form '*mapper: *'
482 * I don't have any clue for PE drvs
484 lpDrv->bIsMapper = bIsMapper;
485 lpDrv->drvname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(drvRegName) + 1), drvRegName);
487 /* Finish init and get the count of the devices */
489 if (MMDRV_InitPerType(lpDrv, MMDRV_AUX, AUXDM_GETNUMDEVS)) i = 1;
490 if (MMDRV_InitPerType(lpDrv, MMDRV_MIXER, MXDM_GETNUMDEVS)) i = 1;
491 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIIN, MIDM_GETNUMDEVS)) i = 1;
492 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIOUT, MODM_GETNUMDEVS)) i = 1;
493 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEIN, WIDM_GETNUMDEVS)) i = 1;
494 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEOUT, WODM_GETNUMDEVS)) i = 1;
495 /* if all those func calls return FALSE, then the driver must be unloaded */
497 CloseDriver(lpDrv->hDriver, 0, 0);
498 HeapFree(GetProcessHeap(), 0, lpDrv->drvname);
499 WARN("Driver initialization failed\n");
508 /**************************************************************************
511 static void MMDRV_Init(void)
514 char driver_buffer[256];
518 strcpy(driver_buffer, WINE_DEFAULT_WINMM_DRIVER);
520 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
521 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hKey))
523 DWORD size = sizeof(driver_buffer);
524 if (RegQueryValueExA(hKey, "Audio", 0, NULL, (BYTE*)driver_buffer, &size))
525 strcpy(driver_buffer, WINE_DEFAULT_WINMM_DRIVER);
528 for (p = driver_buffer; p; p = next)
530 char filename[sizeof(driver_buffer)+10];
531 next = strchr(p, ',');
532 if (next) *next++ = 0;
533 sprintf( filename, "wine%s.drv", p );
534 if (MMDRV_Install(filename, filename, FALSE))
539 MMDRV_Install("wavemapper", "msacm32.drv", TRUE);
540 MMDRV_Install("midimapper", "midimap.dll", TRUE);
543 /******************************************************************
548 static BOOL MMDRV_ExitPerType(LPWINE_MM_DRIVER lpDrv, UINT type)
550 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
552 TRACE("(%p, %04x)\n", lpDrv, type);
554 if (part->fnMessage32) {
556 ret = part->fnMessage32(0, DRVM_DISABLE, 0L, 0L, 0L);
557 TRACE("DRVM_DISABLE => %08lx\n", ret);
559 ret = part->fnMessage32(0, DRVM_EXIT, 0L, 0L, 0L);
560 TRACE("DRVM_EXIT => %s\n", WINMM_ErrorToString(ret));
566 /******************************************************************
571 void MMDRV_Exit(void)
576 for (i = 0; i < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]); i++)
578 if (MM_MLDrvs[i] != NULL)
580 FIXME("Closing while ll-driver open\n");
582 /* FIXME: should generate a message depending on type */
583 MMDRV_Free((HANDLE)(i | 0x8000), MM_MLDrvs[i]);
588 /* unload driver, in reverse order of loading */
589 i = sizeof(MMDrvs) / sizeof(MMDrvs[0]);
592 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_AUX);
593 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIXER);
594 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIIN);
595 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIOUT);
596 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEIN);
597 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEOUT);
598 CloseDriver(MMDrvs[i].hDriver, 0, 0);
600 if (llTypes[MMDRV_AUX].lpMlds)
601 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_AUX].lpMlds - 1);
602 if (llTypes[MMDRV_MIXER].lpMlds)
603 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIXER].lpMlds - 1);
604 if (llTypes[MMDRV_MIDIIN].lpMlds)
605 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIIN].lpMlds - 1);
606 if (llTypes[MMDRV_MIDIOUT].lpMlds)
607 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIOUT].lpMlds - 1);
608 if (llTypes[MMDRV_WAVEIN].lpMlds)
609 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEIN].lpMlds - 1);
610 if (llTypes[MMDRV_WAVEOUT].lpMlds)
611 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEOUT].lpMlds - 1);