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 %d %u 0x%08lx 0x%08lx 0x%08lx)\n",
98 llTypes[mld->type].typestr, mld->uDeviceID, wMsg,
99 mld->dwDriverInstance, dwParam1, dwParam2);
101 if ((UINT16)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=%d 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->mmdIndex = llType->lpMlds[-1].mmdIndex;
206 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
207 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags);
209 if (mld->uDeviceID < llType->wMaxId) {
210 mld->mmdIndex = llType->lpMlds[mld->uDeviceID].mmdIndex;
211 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
212 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags);
215 if (dwRet == MMSYSERR_NOERROR)
216 mld->dwDriverInstance = dwInstance;
220 /**************************************************************************
221 * MMDRV_Close [internal]
223 DWORD MMDRV_Close(LPWINE_MLD mld, UINT wMsg)
225 TRACE("(%p, %04x)\n", mld, wMsg);
226 return MMDRV_Message(mld, wMsg, 0L, 0L);
229 /**************************************************************************
230 * MMDRV_GetByID [internal]
232 static LPWINE_MLD MMDRV_GetByID(UINT uDevID, UINT type)
234 TRACE("(%04x, %04x)\n", uDevID, type);
235 if (uDevID < llTypes[type].wMaxId)
236 return &llTypes[type].lpMlds[uDevID];
237 if ((uDevID == (UINT16)-1 || uDevID == (UINT)-1) && llTypes[type].nMapper != -1)
238 return &llTypes[type].lpMlds[-1];
242 /**************************************************************************
243 * MMDRV_Get [internal]
245 LPWINE_MLD MMDRV_Get(HANDLE _hndl, UINT type, BOOL bCanBeID)
247 LPWINE_MLD mld = NULL;
248 UINT_PTR hndl = (UINT_PTR)_hndl;
249 TRACE("(%p, %04x, %c)\n", _hndl, type, bCanBeID ? 'Y' : 'N');
251 assert(type < MMDRV_MAX);
252 MMDRV_InitSingleType(type);
254 if (hndl >= llTypes[type].wMaxId &&
255 hndl != (UINT16)-1 && hndl != (UINT)-1) {
257 UINT idx = hndl & ~0x8000;
258 if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
261 mld = MM_MLDrvs[idx];
262 if (mld && mld->type != type) mld = NULL;
272 if (mld == NULL && bCanBeID) {
273 mld = MMDRV_GetByID(hndl, type);
278 /**************************************************************************
279 * MMDRV_GetRelated [internal]
281 LPWINE_MLD MMDRV_GetRelated(HANDLE hndl, UINT srcType,
282 BOOL bSrcCanBeID, UINT dstType)
285 TRACE("(%p, %04x, %c, %04x)\n",
286 hndl, srcType, bSrcCanBeID ? 'Y' : 'N', dstType);
288 if ((mld = MMDRV_Get(hndl, srcType, bSrcCanBeID)) != NULL) {
289 WINE_MM_DRIVER_PART* part = &MMDrvs[mld->mmdIndex].parts[dstType];
290 if (part->nIDMin < part->nIDMax)
291 return MMDRV_GetByID(part->nIDMin, dstType);
296 /**************************************************************************
297 * MMDRV_PhysicalFeatures [internal]
299 UINT MMDRV_PhysicalFeatures(LPWINE_MLD mld, UINT uMsg,
300 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
302 WINE_MM_DRIVER* lpDrv = &MMDrvs[mld->mmdIndex];
304 TRACE("(%p, %04x, %08lx, %08lx)\n", mld, uMsg, dwParam1, dwParam2);
306 /* all those function calls are undocumented */
308 case DRV_QUERYDRVENTRY:
309 lstrcpynA((LPSTR)dwParam1, lpDrv->drvname, LOWORD(dwParam2));
311 case DRV_QUERYDEVNODE:
312 *(LPDWORD)dwParam1 = 0L; /* should be DevNode */
315 WARN("NIY QueryName\n");
317 case DRV_QUERYDRIVERIDS:
318 WARN("NIY call VxD\n");
319 /* should call VxD MMDEVLDR with (DevNode, dwParam1 and dwParam2) as pmts
320 * dwParam1 is buffer and dwParam2 is sizeof(buffer)
321 * I don't know where the result is stored though
324 case DRV_QUERYMAPPABLE:
325 return (lpDrv->bIsMapper) ? 2 : 0;
327 case DRVM_MAPPER_PREFERRED_GET:
328 /* FIXME: get from registry someday */
329 *((LPDWORD)dwParam1) = -1; /* No preferred device */
330 *((LPDWORD)dwParam2) = 0;
333 case DRV_QUERYDEVICEINTERFACE:
334 case DRV_QUERYDEVICEINTERFACESIZE:
335 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2);
337 case DRV_QUERYDSOUNDIFACE: /* Wine-specific: Retrieve DirectSound interface */
338 case DRV_QUERYDSOUNDDESC: /* Wine-specific: Retrieve DirectSound driver description*/
339 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2);
342 WARN("Unknown call %04x\n", uMsg);
343 return MMSYSERR_INVALPARAM;
348 /**************************************************************************
349 * MMDRV_InitPerType [internal]
351 static BOOL MMDRV_InitPerType(LPWINE_MM_DRIVER lpDrv, UINT type, UINT wMsg)
353 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
357 TRACE("(%p, %04x, %04x)\n", lpDrv, type, wMsg);
359 part->nIDMin = part->nIDMax = 0;
361 /* for DRVM_INIT and DRVM_ENABLE, dwParam2 should be PnP node */
362 /* the DRVM_ENABLE is only required when the PnP node is non zero */
363 if (part->fnMessage32) {
364 ret = part->fnMessage32(0, DRVM_INIT, 0L, 0L, 0L);
365 TRACE("DRVM_INIT => %s\n", WINMM_ErrorToString(ret));
367 ret = part->fnMessage32(0, DRVM_ENABLE, 0L, 0L, 0L);
368 TRACE("DRVM_ENABLE => %08lx\n", ret);
370 count = part->fnMessage32(0, wMsg, 0L, 0L, 0L);
374 TRACE("Got %u dev for (%s:%s)\n", count, lpDrv->drvname, llTypes[type].typestr);
379 /* got some drivers */
380 if (lpDrv->bIsMapper) {
381 llTypes[type].nMapper = MMDrvsHi;
385 part->nIDMin = llTypes[type].wMaxId;
386 llTypes[type].wMaxId += count;
387 part->nIDMax = llTypes[type].wMaxId;
389 TRACE("Setting min=%d max=%d (ttop=%d) for (%s:%s)\n",
390 part->nIDMin, part->nIDMax, llTypes[type].wMaxId,
391 lpDrv->drvname, llTypes[type].typestr);
392 /* realloc translation table */
393 if (llTypes[type].lpMlds)
394 llTypes[type].lpMlds = (LPWINE_MLD)
395 HeapReAlloc(GetProcessHeap(), 0, llTypes[type].lpMlds - 1,
396 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
398 llTypes[type].lpMlds = (LPWINE_MLD)
399 HeapAlloc(GetProcessHeap(), 0,
400 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
402 /* re-build the translation table */
403 if (lpDrv->bIsMapper) {
404 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, -1, MMDrvs[llTypes[type].nMapper].drvname);
405 llTypes[type].lpMlds[-1].uDeviceID = (UINT)-1;
406 llTypes[type].lpMlds[-1].type = type;
407 llTypes[type].lpMlds[-1].mmdIndex = llTypes[type].nMapper;
408 llTypes[type].lpMlds[-1].dwDriverInstance = 0;
410 for (i = k = 0; i <= MMDrvsHi; i++) {
411 while (MMDrvs[i].parts[type].nIDMin <= k && k < MMDrvs[i].parts[type].nIDMax) {
412 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, k, MMDrvs[i].drvname);
413 llTypes[type].lpMlds[k].uDeviceID = k;
414 llTypes[type].lpMlds[k].type = type;
415 llTypes[type].lpMlds[k].mmdIndex = i;
416 llTypes[type].lpMlds[k].dwDriverInstance = 0;
423 /**************************************************************************
424 * MMDRV_Install [internal]
426 static BOOL MMDRV_Install(LPCSTR drvRegName, LPCSTR drvFileName, BOOL bIsMapper)
429 LPWINE_MM_DRIVER lpDrv = &MMDrvs[MMDrvsHi];
431 WINEMM_msgFunc32 func;
433 TRACE("('%s', '%s', mapper=%c);\n", drvRegName, drvFileName, bIsMapper ? 'Y' : 'N');
435 for (i = 0; i < MMDrvsHi; i++) {
436 if (!strcmp(drvRegName, MMDrvs[i].drvname)) return FALSE;
439 /* Be sure that size of MMDrvs matches the max number of loadable
441 * If not just increase size of MMDrvs
443 assert(MMDrvsHi <= sizeof(MMDrvs)/sizeof(MMDrvs[0]));
445 memset(lpDrv, 0, sizeof(*lpDrv));
447 if (!(lpDrv->hDriver = OpenDriverA(drvFileName, 0, 0))) {
448 WARN("Couldn't open driver '%s'\n", drvFileName);
452 d = DRIVER_FindFromHDrvr(lpDrv->hDriver);
454 /* Then look for xxxMessage functions */
455 #define AA(_h,_w,_x,_y,_z) \
456 func = (WINEMM_msgFunc##_y) _z ((_h), #_x); \
458 { lpDrv->parts[_w].fnMessage##_y = func; count++; \
459 TRACE("Got %d bit func '%s'\n", _y, #_x); }
462 #define A(_x,_y) AA(d->hModule,_x,_y,32,GetProcAddress)
463 A(MMDRV_AUX, auxMessage);
464 A(MMDRV_MIXER, mxdMessage);
465 A(MMDRV_MIDIIN, midMessage);
466 A(MMDRV_MIDIOUT, modMessage);
467 A(MMDRV_WAVEIN, widMessage);
468 A(MMDRV_WAVEOUT, wodMessage);
474 CloseDriver(lpDrv->hDriver, 0, 0);
475 WARN("No message functions found\n");
479 /* FIXME: being a mapper or not should be known by another way */
480 /* it's known for NE drvs (the description is of the form '*mapper: *'
481 * I don't have any clue for PE drvs
483 lpDrv->bIsMapper = bIsMapper;
484 lpDrv->drvname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(drvRegName) + 1), drvRegName);
486 /* Finish init and get the count of the devices */
488 if (MMDRV_InitPerType(lpDrv, MMDRV_AUX, AUXDM_GETNUMDEVS)) i = 1;
489 if (MMDRV_InitPerType(lpDrv, MMDRV_MIXER, MXDM_GETNUMDEVS)) i = 1;
490 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIIN, MIDM_GETNUMDEVS)) i = 1;
491 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIOUT, MODM_GETNUMDEVS)) i = 1;
492 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEIN, WIDM_GETNUMDEVS)) i = 1;
493 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEOUT, WODM_GETNUMDEVS)) i = 1;
494 /* if all those func calls return FALSE, then the driver must be unloaded */
496 CloseDriver(lpDrv->hDriver, 0, 0);
497 HeapFree(GetProcessHeap(), 0, lpDrv->drvname);
498 WARN("Driver initialization failed\n");
507 /**************************************************************************
510 static void MMDRV_Init(void)
513 char driver_buffer[256];
517 strcpy(driver_buffer, WINE_DEFAULT_WINMM_DRIVER);
519 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
520 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hKey))
522 DWORD size = sizeof(driver_buffer);
523 if (RegQueryValueExA(hKey, "Audio", 0, NULL, (BYTE*)driver_buffer, &size))
524 strcpy(driver_buffer, WINE_DEFAULT_WINMM_DRIVER);
527 for (p = driver_buffer; p; p = next)
529 char filename[sizeof(driver_buffer)+10];
530 next = strchr(p, ',');
531 if (next) *next++ = 0;
532 sprintf( filename, "wine%s.drv", p );
533 if (MMDRV_Install(filename, filename, FALSE))
538 MMDRV_Install("wavemapper", "msacm32.drv", TRUE);
539 MMDRV_Install("midimapper", "midimap.dll", TRUE);
542 /******************************************************************
547 static BOOL MMDRV_ExitPerType(LPWINE_MM_DRIVER lpDrv, UINT type)
549 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
551 TRACE("(%p, %04x)\n", lpDrv, type);
553 if (part->fnMessage32) {
555 ret = part->fnMessage32(0, DRVM_DISABLE, 0L, 0L, 0L);
556 TRACE("DRVM_DISABLE => %08lx\n", ret);
558 ret = part->fnMessage32(0, DRVM_EXIT, 0L, 0L, 0L);
559 TRACE("DRVM_EXIT => %s\n", WINMM_ErrorToString(ret));
565 /******************************************************************
570 void MMDRV_Exit(void)
575 for (i = 0; i < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]); i++)
577 if (MM_MLDrvs[i] != NULL)
579 FIXME("Closing while ll-driver open\n");
581 /* FIXME: should generate a message depending on type */
582 MMDRV_Free((HANDLE)(i | 0x8000), MM_MLDrvs[i]);
587 /* unload driver, in reverse order of loading */
588 i = sizeof(MMDrvs) / sizeof(MMDrvs[0]);
591 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_AUX);
592 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIXER);
593 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIIN);
594 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIOUT);
595 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEIN);
596 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEOUT);
597 CloseDriver(MMDrvs[i].hDriver, 0, 0);
599 if (llTypes[MMDRV_AUX].lpMlds)
600 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_AUX].lpMlds - 1);
601 if (llTypes[MMDRV_MIXER].lpMlds)
602 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIXER].lpMlds - 1);
603 if (llTypes[MMDRV_MIDIIN].lpMlds)
604 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIIN].lpMlds - 1);
605 if (llTypes[MMDRV_MIDIOUT].lpMlds)
606 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIOUT].lpMlds - 1);
607 if (llTypes[MMDRV_WAVEIN].lpMlds)
608 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEIN].lpMlds - 1);
609 if (llTypes[MMDRV_WAVEOUT].lpMlds)
610 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEOUT].lpMlds - 1);