rpcrt4: Free the memory for a registered interface that is in use and when WaitForCal...
[wine.git] / dlls / winmm / lolvldrv.c
blobd50f32963322a0ebdd8d001f3a53580164ab4931
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "winemm.h"
34 #include "wine/debug.h"
35 #include "wine/exception.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
39 LRESULT (*pFnCallMMDrvFunc16)(DWORD,WORD,WORD,LONG,LONG,LONG) /* = NULL */;
40 unsigned (*pFnLoadMMDrvFunc16)(LPCSTR,LPWINE_DRIVER, LPWINE_MM_DRIVER) /* = NULL */;
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 BOOL bSupportMapper; /* if type is allowed to support mapper */
47 MMDRV_MAPFUNC Map16To32A; /* those are function pointers to handle */
48 MMDRV_UNMAPFUNC UnMap16To32A; /* the parameter conversion (16 vs 32 bit) */
49 MMDRV_MAPFUNC Map32ATo16; /* when hi-func (in mmsystem or winmm) and */
50 MMDRV_UNMAPFUNC UnMap32ATo16; /* low-func (in .drv) do not match */
51 LPDRVCALLBACK Callback; /* handles callback for a specified type */
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;
58 static int MMDrvsHi /* = 0 */;
59 static WINE_MM_DRIVER MMDrvs[8];
60 static LPWINE_MLD MM_MLDrvs[40];
61 #define MAX_MM_MLDRVS (sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]))
63 #define A(_x,_y) {#_y, _x, NULL, NULL, NULL, NULL, NULL, 0, NULL, -1}
64 /* Note: the indices of this array must match the definitions
65 * of the MMDRV_???? manifest constants
67 static WINE_LLTYPE llTypes[MMDRV_MAX] = {
68 A(TRUE, Aux),
69 A(FALSE, Mixer),
70 A(TRUE, MidiIn),
71 A(TRUE, MidiOut),
72 A(TRUE, WaveIn),
73 A(TRUE, WaveOut),
75 #undef A
77 /******************************************************************
78 * MMDRV_InstallMap
82 void MMDRV_InstallMap(unsigned int drv,
83 MMDRV_MAPFUNC mp1632, MMDRV_UNMAPFUNC um1632,
84 MMDRV_MAPFUNC mp3216, MMDRV_UNMAPFUNC um3216,
85 LPDRVCALLBACK cb)
87 assert(drv < MMDRV_MAX);
88 llTypes[drv].Map16To32A = mp1632;
89 llTypes[drv].UnMap16To32A = um1632;
90 llTypes[drv].Map32ATo16 = mp3216;
91 llTypes[drv].UnMap32ATo16 = um3216;
92 llTypes[drv].Callback = cb;
95 /******************************************************************
96 * MMDRV_Is32
99 BOOL MMDRV_Is32(unsigned int idx)
101 TRACE("(%d)\n", idx);
102 return MMDrvs[idx].bIs32;
105 /**************************************************************************
106 * MMDRV_GetNum [internal]
108 UINT MMDRV_GetNum(UINT type)
110 TRACE("(%04x)\n", type);
111 assert(type < MMDRV_MAX);
112 return llTypes[type].wMaxId;
115 /**************************************************************************
116 * MMDRV_Message [internal]
118 DWORD MMDRV_Message(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1,
119 DWORD_PTR dwParam2, BOOL bFrom32)
121 LPWINE_MM_DRIVER lpDrv;
122 DWORD ret;
123 WINE_MM_DRIVER_PART* part;
124 WINE_LLTYPE* llType = &llTypes[mld->type];
125 WINMM_MapType map;
126 int devID;
128 TRACE("(%s %u %u 0x%08x 0x%08lx 0x%08lx %c)\n",
129 llTypes[mld->type].typestr, mld->uDeviceID, wMsg,
130 mld->dwDriverInstance, dwParam1, dwParam2, bFrom32?'Y':'N');
132 if (mld->uDeviceID == (UINT16)-1) {
133 if (!llType->bSupportMapper) {
134 WARN("uDev=-1 requested on non-mappable ll type %s\n",
135 llTypes[mld->type].typestr);
136 return MMSYSERR_BADDEVICEID;
138 devID = -1;
139 } else {
140 if (mld->uDeviceID >= llType->wMaxId) {
141 WARN("uDev(%u) requested >= max (%d)\n", mld->uDeviceID, llType->wMaxId);
142 return MMSYSERR_BADDEVICEID;
144 devID = mld->uDeviceID;
147 lpDrv = &MMDrvs[mld->mmdIndex];
148 part = &lpDrv->parts[mld->type];
150 #if 0
151 /* some sanity checks */
152 if (!(part->nIDMin <= devID))
153 ERR("!(part->nIDMin(%d) <= devID(%d))\n", part->nIDMin, devID);
154 if (!(devID < part->nIDMax))
155 ERR("!(devID(%d) < part->nIDMax(%d))\n", devID, part->nIDMax);
156 #endif
158 if (lpDrv->bIs32) {
159 assert(part->u.fnMessage32);
161 if (bFrom32) {
162 TRACE("Calling message(dev=%u msg=%u usr=0x%08x p1=0x%08lx p2=0x%08lx)\n",
163 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
164 ret = part->u.fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
165 TRACE("=> %s\n", WINMM_ErrorToString(ret));
166 } else {
167 map = llType->Map16To32A(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
168 switch (map) {
169 case WINMM_MAP_NOMEM:
170 ret = MMSYSERR_NOMEM;
171 break;
172 case WINMM_MAP_MSGERROR:
173 FIXME("NIY: no conversion yet 16->32 (%u)\n", wMsg);
174 ret = MMSYSERR_ERROR;
175 break;
176 case WINMM_MAP_OK:
177 case WINMM_MAP_OKMEM:
178 TRACE("Calling message(dev=%u msg=%u usr=0x%08x p1=0x%08lx p2=0x%08lx)\n",
179 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
180 ret = part->u.fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance,
181 dwParam1, dwParam2);
182 TRACE("=> %s\n", WINMM_ErrorToString(ret));
183 if (map == WINMM_MAP_OKMEM)
184 llType->UnMap16To32A(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2, ret);
185 break;
186 default:
187 FIXME("NIY\n");
188 ret = MMSYSERR_NOTSUPPORTED;
189 break;
192 } else {
193 assert(part->u.fnMessage16 && pFnCallMMDrvFunc16);
195 if (bFrom32) {
196 map = llType->Map32ATo16(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
197 switch (map) {
198 case WINMM_MAP_NOMEM:
199 ret = MMSYSERR_NOMEM;
200 break;
201 case WINMM_MAP_MSGERROR:
202 FIXME("NIY: no conversion yet 32->16 (%u)\n", wMsg);
203 ret = MMSYSERR_ERROR;
204 break;
205 case WINMM_MAP_OK:
206 case WINMM_MAP_OKMEM:
207 TRACE("Calling message(dev=%u msg=%u usr=0x%08x p1=0x%08lx p2=0x%08lx)\n",
208 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
209 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
210 mld->uDeviceID, wMsg, mld->dwDriverInstance,
211 dwParam1, dwParam2);
212 TRACE("=> %s\n", WINMM_ErrorToString(ret));
213 if (map == WINMM_MAP_OKMEM)
214 llType->UnMap32ATo16(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2, ret);
215 break;
216 default:
217 FIXME("NIY\n");
218 ret = MMSYSERR_NOTSUPPORTED;
219 break;
221 } else {
222 TRACE("Calling message(dev=%u msg=%u usr=0x%08x p1=0x%08lx p2=0x%08lx)\n",
223 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
224 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
225 mld->uDeviceID, wMsg, mld->dwDriverInstance,
226 dwParam1, dwParam2);
227 TRACE("=> %s\n", WINMM_ErrorToString(ret));
230 return ret;
233 /**************************************************************************
234 * MMDRV_Alloc [internal]
236 LPWINE_MLD MMDRV_Alloc(UINT size, UINT type, LPHANDLE hndl, DWORD* dwFlags,
237 DWORD_PTR* dwCallback, DWORD_PTR* dwInstance, BOOL bFrom32)
239 LPWINE_MLD mld;
240 UINT i;
241 TRACE("(%d, %04x, %p, %p, %p, %p, %c)\n",
242 size, type, hndl, dwFlags, dwCallback, dwInstance, bFrom32?'Y':'N');
244 mld = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
245 if (!mld) return NULL;
247 /* find an empty slot in MM_MLDrvs table */
248 for (i = 0; i < MAX_MM_MLDRVS; i++) if (!MM_MLDrvs[i]) break;
250 if (i == MAX_MM_MLDRVS) {
251 /* the MM_MLDrvs table could be made growable in the future if needed */
252 ERR("Too many open drivers\n");
253 HeapFree(GetProcessHeap(), 0, mld);
254 return NULL;
256 MM_MLDrvs[i] = mld;
257 *hndl = (HANDLE)(i | 0x8000);
259 mld->type = type;
260 if ((UINT)*hndl < MMDRV_GetNum(type) || HIWORD(*hndl) != 0) {
261 /* FIXME: those conditions must be fulfilled so that:
262 * - we can distinguish between device IDs and handles
263 * - we can use handles as 16 or 32 bit entities
265 ERR("Shouldn't happen. Bad allocation scheme\n");
268 mld->bFrom32 = bFrom32;
269 mld->dwFlags = HIWORD(*dwFlags);
270 mld->dwCallback = *dwCallback;
271 mld->dwClientInstance = *dwInstance;
273 if (llTypes[type].Callback)
275 *dwFlags = LOWORD(*dwFlags) | CALLBACK_FUNCTION;
276 *dwCallback = (DWORD)llTypes[type].Callback;
277 *dwInstance = (DWORD)mld; /* FIXME: wouldn't some 16 bit drivers only use the loword ? */
280 return mld;
283 /**************************************************************************
284 * MMDRV_Free [internal]
286 void MMDRV_Free(HANDLE hndl, LPWINE_MLD mld)
288 TRACE("(%p, %p)\n", hndl, mld);
290 if ((UINT)hndl & 0x8000) {
291 unsigned idx = (UINT)hndl & ~0x8000;
292 if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
293 MM_MLDrvs[idx] = NULL;
294 HeapFree(GetProcessHeap(), 0, mld);
295 return;
298 ERR("Bad Handle %p at %p (not freed)\n", hndl, mld);
301 /**************************************************************************
302 * MMDRV_Open [internal]
304 DWORD MMDRV_Open(LPWINE_MLD mld, UINT wMsg, DWORD dwParam1, DWORD dwFlags)
306 DWORD dwRet = MMSYSERR_BADDEVICEID;
307 DWORD dwInstance;
308 WINE_LLTYPE* llType = &llTypes[mld->type];
309 TRACE("(%p, %04x, 0x%08x, 0x%08x)\n", mld, wMsg, dwParam1, dwFlags);
311 mld->dwDriverInstance = (DWORD)&dwInstance;
313 if (mld->uDeviceID == (UINT)-1 || mld->uDeviceID == (UINT16)-1) {
314 TRACE("MAPPER mode requested !\n");
315 /* check if mapper is supported by type */
316 if (llType->bSupportMapper) {
317 if (llType->nMapper == -1) {
318 /* no driver for mapper has been loaded, try a dumb implementation */
319 TRACE("No mapper loaded, doing it by hand\n");
320 for (mld->uDeviceID = 0; mld->uDeviceID < llType->wMaxId; mld->uDeviceID++) {
321 if ((dwRet = MMDRV_Open(mld, wMsg, dwParam1, dwFlags)) == MMSYSERR_NOERROR) {
322 /* to share this function epilog */
323 dwInstance = mld->dwDriverInstance;
324 break;
327 } else {
328 mld->uDeviceID = (UINT16)-1;
329 mld->mmdIndex = llType->lpMlds[-1].mmdIndex;
330 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
331 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags, TRUE);
334 } else {
335 if (mld->uDeviceID < llType->wMaxId) {
336 mld->mmdIndex = llType->lpMlds[mld->uDeviceID].mmdIndex;
337 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
338 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags, TRUE);
341 if (dwRet == MMSYSERR_NOERROR)
342 mld->dwDriverInstance = dwInstance;
343 return dwRet;
346 /**************************************************************************
347 * MMDRV_Close [internal]
349 DWORD MMDRV_Close(LPWINE_MLD mld, UINT wMsg)
351 TRACE("(%p, %04x)\n", mld, wMsg);
352 return MMDRV_Message(mld, wMsg, 0L, 0L, TRUE);
355 /**************************************************************************
356 * MMDRV_GetByID [internal]
358 static LPWINE_MLD MMDRV_GetByID(UINT uDevID, UINT type)
360 TRACE("(%04x, %04x)\n", uDevID, type);
361 if (uDevID < llTypes[type].wMaxId)
362 return &llTypes[type].lpMlds[uDevID];
363 if ((uDevID == (UINT16)-1 || uDevID == (UINT)-1) && llTypes[type].nMapper != -1)
364 return &llTypes[type].lpMlds[-1];
365 return NULL;
368 /**************************************************************************
369 * MMDRV_Get [internal]
371 LPWINE_MLD MMDRV_Get(HANDLE _hndl, UINT type, BOOL bCanBeID)
373 LPWINE_MLD mld = NULL;
374 UINT hndl = (UINT)_hndl;
375 TRACE("(%p, %04x, %c)\n", _hndl, type, bCanBeID ? 'Y' : 'N');
377 assert(type < MMDRV_MAX);
379 if (hndl >= llTypes[type].wMaxId &&
380 hndl != (UINT16)-1 && hndl != (UINT)-1) {
381 if (hndl & 0x8000) {
382 UINT idx = hndl & ~0x8000;
383 if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
384 __TRY
386 mld = MM_MLDrvs[idx];
387 if (mld && mld->type != type) mld = NULL;
389 __EXCEPT_PAGE_FAULT
391 mld = NULL;
393 __ENDTRY;
397 if (mld == NULL && bCanBeID) {
398 mld = MMDRV_GetByID(hndl, type);
400 return mld;
403 /**************************************************************************
404 * MMDRV_GetRelated [internal]
406 LPWINE_MLD MMDRV_GetRelated(HANDLE hndl, UINT srcType,
407 BOOL bSrcCanBeID, UINT dstType)
409 LPWINE_MLD mld;
410 TRACE("(%p, %04x, %c, %04x)\n",
411 hndl, srcType, bSrcCanBeID ? 'Y' : 'N', dstType);
413 if ((mld = MMDRV_Get(hndl, srcType, bSrcCanBeID)) != NULL) {
414 WINE_MM_DRIVER_PART* part = &MMDrvs[mld->mmdIndex].parts[dstType];
415 if (part->nIDMin < part->nIDMax)
416 return MMDRV_GetByID(part->nIDMin, dstType);
418 return NULL;
421 /**************************************************************************
422 * MMDRV_PhysicalFeatures [internal]
424 UINT MMDRV_PhysicalFeatures(LPWINE_MLD mld, UINT uMsg, DWORD dwParam1,
425 DWORD dwParam2)
427 WINE_MM_DRIVER* lpDrv = &MMDrvs[mld->mmdIndex];
429 TRACE("(%p, %04x, %08x, %08x)\n", mld, uMsg, dwParam1, dwParam2);
431 /* all those function calls are undocumented */
432 switch (uMsg) {
433 case DRV_QUERYDRVENTRY:
434 lstrcpynA((LPSTR)dwParam1, lpDrv->drvname, LOWORD(dwParam2));
435 break;
436 case DRV_QUERYDEVNODE:
437 *(LPDWORD)dwParam1 = 0L; /* should be DevNode */
438 break;
439 case DRV_QUERYNAME:
440 WARN("NIY QueryName\n");
441 break;
442 case DRV_QUERYDRIVERIDS:
443 WARN("NIY call VxD\n");
444 /* should call VxD MMDEVLDR with (DevNode, dwParam1 and dwParam2) as pmts
445 * dwParam1 is buffer and dwParam2 is sizeof(buffer)
446 * I don't know where the result is stored though
448 break;
449 case DRV_QUERYMAPPABLE:
450 return (lpDrv->bIsMapper) ? 2 : 0;
452 case DRVM_MAPPER_PREFERRED_GET:
453 /* FIXME: get from registry someday */
454 *((LPDWORD)dwParam1) = -1; /* No preferred device */
455 *((LPDWORD)dwParam2) = 0;
456 break;
458 case DRV_QUERYDEVICEINTERFACE:
459 case DRV_QUERYDEVICEINTERFACESIZE:
460 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2, TRUE);
462 case DRV_QUERYDSOUNDIFACE: /* Wine-specific: Retrieve DirectSound interface */
463 case DRV_QUERYDSOUNDDESC: /* Wine-specific: Retrieve DirectSound driver description*/
464 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2, TRUE);
466 default:
467 WARN("Unknown call %04x\n", uMsg);
468 return MMSYSERR_INVALPARAM;
470 return 0L;
473 /**************************************************************************
474 * MMDRV_InitPerType [internal]
476 static BOOL MMDRV_InitPerType(LPWINE_MM_DRIVER lpDrv, UINT type, UINT wMsg)
478 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
479 DWORD ret;
480 UINT count = 0;
481 int i, k;
482 TRACE("(%p, %04x, %04x)\n", lpDrv, type, wMsg);
484 part->nIDMin = part->nIDMax = 0;
486 /* for DRVM_INIT and DRVM_ENABLE, dwParam2 should be PnP node */
487 /* the DRVM_ENABLE is only required when the PnP node is non zero */
489 if (lpDrv->bIs32 && part->u.fnMessage32) {
490 ret = part->u.fnMessage32(0, DRVM_INIT, 0L, 0L, 0L);
491 TRACE("DRVM_INIT => %s\n", WINMM_ErrorToString(ret));
492 #if 0
493 ret = part->u.fnMessage32(0, DRVM_ENABLE, 0L, 0L, 0L);
494 TRACE("DRVM_ENABLE => %08lx\n", ret);
495 #endif
496 count = part->u.fnMessage32(0, wMsg, 0L, 0L, 0L);
497 } else if (!lpDrv->bIs32 && part->u.fnMessage16 && pFnCallMMDrvFunc16) {
498 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
499 0, DRVM_INIT, 0L, 0L, 0L);
500 TRACE("DRVM_INIT => %s\n", WINMM_ErrorToString(ret));
501 #if 0
502 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
503 0, DRVM_ENABLE, 0L, 0L, 0L);
504 TRACE("DRVM_ENABLE => %08lx\n", ret);
505 #endif
506 count = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
507 0, wMsg, 0L, 0L, 0L);
508 } else {
509 return FALSE;
512 TRACE("Got %u dev for (%s:%s)\n", count, lpDrv->drvname, llTypes[type].typestr);
514 if (HIWORD(count))
515 return FALSE;
517 /* got some drivers */
518 if (lpDrv->bIsMapper) {
519 /* it seems native mappers return 0 devices :-( */
520 if (llTypes[type].nMapper != -1)
521 ERR("Two mappers for type %s (%d, %s)\n",
522 llTypes[type].typestr, llTypes[type].nMapper, lpDrv->drvname);
523 if (count > 1)
524 ERR("Strange: mapper with %d > 1 devices\n", count);
525 llTypes[type].nMapper = MMDrvsHi;
526 } else {
527 if (count == 0)
528 return FALSE;
529 part->nIDMin = llTypes[type].wMaxId;
530 llTypes[type].wMaxId += count;
531 part->nIDMax = llTypes[type].wMaxId;
533 TRACE("Setting min=%d max=%d (ttop=%d) for (%s:%s)\n",
534 part->nIDMin, part->nIDMax, llTypes[type].wMaxId,
535 lpDrv->drvname, llTypes[type].typestr);
536 /* realloc translation table */
537 if (llTypes[type].lpMlds)
538 llTypes[type].lpMlds = (LPWINE_MLD)
539 HeapReAlloc(GetProcessHeap(), 0, llTypes[type].lpMlds - 1,
540 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
541 else
542 llTypes[type].lpMlds = (LPWINE_MLD)
543 HeapAlloc(GetProcessHeap(), 0,
544 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
546 /* re-build the translation table */
547 if (llTypes[type].nMapper != -1) {
548 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, -1, MMDrvs[llTypes[type].nMapper].drvname);
549 llTypes[type].lpMlds[-1].uDeviceID = (UINT16)-1;
550 llTypes[type].lpMlds[-1].type = type;
551 llTypes[type].lpMlds[-1].mmdIndex = llTypes[type].nMapper;
552 llTypes[type].lpMlds[-1].dwDriverInstance = 0;
554 for (i = k = 0; i <= MMDrvsHi; i++) {
555 while (MMDrvs[i].parts[type].nIDMin <= k && k < MMDrvs[i].parts[type].nIDMax) {
556 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, k, MMDrvs[i].drvname);
557 llTypes[type].lpMlds[k].uDeviceID = k;
558 llTypes[type].lpMlds[k].type = type;
559 llTypes[type].lpMlds[k].mmdIndex = i;
560 llTypes[type].lpMlds[k].dwDriverInstance = 0;
561 k++;
564 return TRUE;
567 /**************************************************************************
568 * MMDRV_Install [internal]
570 static BOOL MMDRV_Install(LPCSTR drvRegName, LPCSTR drvFileName, BOOL bIsMapper)
572 int i, count = 0;
573 LPWINE_MM_DRIVER lpDrv = &MMDrvs[MMDrvsHi];
574 LPWINE_DRIVER d;
576 TRACE("('%s', '%s', mapper=%c);\n", drvRegName, drvFileName, bIsMapper ? 'Y' : 'N');
578 for (i = 0; i < MMDrvsHi; i++) {
579 if (!strcmp(drvRegName, MMDrvs[i].drvname)) return FALSE;
582 /* Be sure that size of MMDrvs matches the max number of loadable
583 * drivers !!
584 * If not just increase size of MMDrvs
586 assert(MMDrvsHi <= sizeof(MMDrvs)/sizeof(MMDrvs[0]));
588 memset(lpDrv, 0, sizeof(*lpDrv));
590 if (!(lpDrv->hDriver = OpenDriverA(drvFileName, 0, 0))) {
591 WARN("Couldn't open driver '%s'\n", drvFileName);
592 return FALSE;
595 d = DRIVER_FindFromHDrvr(lpDrv->hDriver);
596 lpDrv->bIs32 = (d->dwFlags & WINE_GDF_16BIT) ? FALSE : TRUE;
598 /* Then look for xxxMessage functions */
599 #define AA(_h,_w,_x,_y,_z) \
600 func = (WINEMM_msgFunc##_y) _z ((_h), #_x); \
601 if (func != NULL) \
602 { lpDrv->parts[_w].u.fnMessage##_y = func; count++; \
603 TRACE("Got %d bit func '%s'\n", _y, #_x); }
605 if (lpDrv->bIs32) {
606 WINEMM_msgFunc32 func;
608 if (d->d.d32.hModule) {
609 #define A(_x,_y) AA(d->d.d32.hModule,_x,_y,32,GetProcAddress)
610 A(MMDRV_AUX, auxMessage);
611 A(MMDRV_MIXER, mxdMessage);
612 A(MMDRV_MIDIIN, midMessage);
613 A(MMDRV_MIDIOUT, modMessage);
614 A(MMDRV_WAVEIN, widMessage);
615 A(MMDRV_WAVEOUT, wodMessage);
616 #undef A
618 } else if (WINMM_CheckForMMSystem() && pFnLoadMMDrvFunc16) {
619 count += pFnLoadMMDrvFunc16(drvFileName, d, lpDrv);
621 #undef AA
623 if (!count) {
624 CloseDriver(lpDrv->hDriver, 0, 0);
625 WARN("No message functions found\n");
626 return FALSE;
629 /* FIXME: being a mapper or not should be known by another way */
630 /* it's known for NE drvs (the description is of the form '*mapper: *'
631 * I don't have any clue for PE drvs
633 lpDrv->bIsMapper = bIsMapper;
634 lpDrv->drvname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(drvRegName) + 1), drvRegName);
636 /* Finish init and get the count of the devices */
637 i = 0;
638 if (MMDRV_InitPerType(lpDrv, MMDRV_AUX, AUXDM_GETNUMDEVS)) i = 1;
639 if (MMDRV_InitPerType(lpDrv, MMDRV_MIXER, MXDM_GETNUMDEVS)) i = 1;
640 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIIN, MIDM_GETNUMDEVS)) i = 1;
641 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIOUT, MODM_GETNUMDEVS)) i = 1;
642 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEIN, WIDM_GETNUMDEVS)) i = 1;
643 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEOUT, WODM_GETNUMDEVS)) i = 1;
644 /* if all those func calls return FALSE, then the driver must be unloaded */
645 if (!i) {
646 CloseDriver(lpDrv->hDriver, 0, 0);
647 HeapFree(GetProcessHeap(), 0, lpDrv->drvname);
648 WARN("Driver initialization failed\n");
649 return FALSE;
652 MMDrvsHi++;
654 return TRUE;
657 /**************************************************************************
658 * MMDRV_Init
660 BOOL MMDRV_Init(void)
662 HKEY hKey;
663 char driver_buffer[256];
664 char mapper_buffer[256];
665 char midi_buffer[256];
666 char* p;
667 DWORD type, size;
668 BOOL ret = FALSE;
669 TRACE("()\n");
671 strcpy(driver_buffer, WINE_DEFAULT_WINMM_DRIVER);
672 strcpy(mapper_buffer, WINE_DEFAULT_WINMM_MAPPER);
673 strcpy(midi_buffer, WINE_DEFAULT_WINMM_MIDI);
675 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
676 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hKey))
678 size = sizeof(driver_buffer);
679 if (RegQueryValueExA(hKey, "Audio", 0, &type, (LPVOID)driver_buffer, &size))
680 strcpy(driver_buffer, WINE_DEFAULT_WINMM_DRIVER);
683 p = driver_buffer;
684 while (p)
686 char filename[sizeof(driver_buffer)+10];
687 char *next = strchr(p, ',');
688 if (next) *next++ = 0;
689 sprintf( filename, "wine%s.drv", p );
690 if ((ret = MMDRV_Install( filename, filename, FALSE ))) break;
691 p = next;
694 ret |= MMDRV_Install("wavemapper", WINE_DEFAULT_WINMM_MAPPER, TRUE);
695 ret |= MMDRV_Install("midimapper", WINE_DEFAULT_WINMM_MIDI, TRUE);
696 return ret;
699 /******************************************************************
700 * ExitPerType
704 static BOOL MMDRV_ExitPerType(LPWINE_MM_DRIVER lpDrv, UINT type)
706 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
707 DWORD ret;
708 TRACE("(%p, %04x)\n", lpDrv, type);
710 if (lpDrv->bIs32 && part->u.fnMessage32) {
711 #if 0
712 ret = part->u.fnMessage32(0, DRVM_DISABLE, 0L, 0L, 0L);
713 TRACE("DRVM_DISABLE => %08lx\n", ret);
714 #endif
715 ret = part->u.fnMessage32(0, DRVM_EXIT, 0L, 0L, 0L);
716 TRACE("DRVM_EXIT => %s\n", WINMM_ErrorToString(ret));
717 } else if (!lpDrv->bIs32 && part->u.fnMessage16 && pFnCallMMDrvFunc16) {
718 #if 0
719 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
720 0, DRVM_DISABLE, 0L, 0L, 0L);
721 TRACE("DRVM_DISABLE => %08lx\n", ret);
722 #endif
723 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
724 0, DRVM_EXIT, 0L, 0L, 0L);
725 TRACE("DRVM_EXIT => %s\n", WINMM_ErrorToString(ret));
726 } else {
727 return FALSE;
730 return TRUE;
733 /******************************************************************
734 * Exit
738 void MMDRV_Exit(void)
740 int i;
741 TRACE("()\n");
743 for (i = 0; i < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]); i++)
745 if (MM_MLDrvs[i] != NULL)
747 FIXME("Closing while ll-driver open\n");
748 #if 0
749 /* FIXME: should generate a message depending on type */
750 MMDRV_Free((HANDLE)(i | 0x8000), MM_MLDrvs[i]);
751 #endif
755 /* unload driver, in reverse order of loading */
756 for (i = sizeof(MMDrvs) / sizeof(MMDrvs[0]) - 1; i >= 0; i--)
758 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_AUX);
759 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIXER);
760 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIIN);
761 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIOUT);
762 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEIN);
763 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEOUT);
764 CloseDriver(MMDrvs[i].hDriver, 0, 0);
766 if (llTypes[MMDRV_AUX].lpMlds)
767 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_AUX].lpMlds - 1);
768 if (llTypes[MMDRV_MIXER].lpMlds)
769 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIXER].lpMlds - 1);
770 if (llTypes[MMDRV_MIDIIN].lpMlds)
771 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIIN].lpMlds - 1);
772 if (llTypes[MMDRV_MIDIOUT].lpMlds)
773 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIOUT].lpMlds - 1);
774 if (llTypes[MMDRV_WAVEIN].lpMlds)
775 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEIN].lpMlds - 1);
776 if (llTypes[MMDRV_WAVEOUT].lpMlds)
777 HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEOUT].lpMlds - 1);