lolvldrv.c 25.1 KB
Newer Older
1 2 3
/* -*- tab-width: 8; c-basic-offset: 4 -*- */

/*
4
 * MMSYSTEM low level drivers handling functions
5 6
 *
 * Copyright 1999 Eric Pouech
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
21 22
 */

23 24 25
#include "config.h"
#include "wine/port.h"

26
#include <string.h>
27
#include <stdarg.h>
28
#include <stdio.h>
29
#include <assert.h>
30 31
#include "windef.h"
#include "winbase.h"
32
#include "winreg.h"
33
#include "winemm.h"
34
#include "wine/debug.h"
35
#include "wine/exception.h"
36

37
WINE_DEFAULT_DEBUG_CHANNEL(winmm);
38

39
LRESULT         (*pFnCallMMDrvFunc16)(DWORD,WORD,WORD,LONG,LONG,LONG) /* = NULL */;
40
unsigned        (*pFnLoadMMDrvFunc16)(LPCSTR,LPWINE_DRIVER, LPWINE_MM_DRIVER) /* = NULL */;
41 42 43

/* each known type of driver has an instance of this structure */
typedef struct tagWINE_LLTYPE {
44
    /* those attributes depend on the specification of the type */
45
    LPCSTR		typestr;	/* name (for debugging) */
46 47
    BOOL		bSupportMapper;	/* if type is allowed to support mapper */
    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 52
    LPDRVCALLBACK	Callback;       /* handles callback for a specified type */
    /* those attributes reflect the loaded/current situation for the type */
53
    UINT		wMaxId;		/* number of loaded devices (sum across all loaded drivers) */
54 55 56 57
    LPWINE_MLD		lpMlds;		/* "static" mlds to access the part though device IDs */
    int			nMapper;	/* index to mapper */
} WINE_LLTYPE;

58
static int		MMDrvsHi /* = 0 */;
59
static WINE_MM_DRIVER	MMDrvs[8];
60 61
static LPWINE_MLD	MM_MLDrvs[40];
#define MAX_MM_MLDRVS	(sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]))
62

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#define A(_x,_y) {#_y, _x, NULL, NULL, NULL, NULL, NULL, 0, NULL, -1}
/* Note: the indices of this array must match the definitions
 *	 of the MMDRV_???? manifest constants
 */
static WINE_LLTYPE	llTypes[MMDRV_MAX] = {
    A(TRUE,  Aux),
    A(FALSE, Mixer),
    A(TRUE,  MidiIn),
    A(TRUE,  MidiOut),
    A(TRUE,  WaveIn),
    A(TRUE,  WaveOut),
};
#undef A

/******************************************************************
 *		MMDRV_InstallMap
 *
 *
 */
void    MMDRV_InstallMap(unsigned int drv, 
83 84
                         MMDRV_MAPFUNC mp1632, MMDRV_UNMAPFUNC um1632,
                         MMDRV_MAPFUNC mp3216, MMDRV_UNMAPFUNC um3216,
85 86 87 88 89 90
                         LPDRVCALLBACK cb)
{
    assert(drv < MMDRV_MAX);
    llTypes[drv].Map16To32A   = mp1632;
    llTypes[drv].UnMap16To32A = um1632;
    llTypes[drv].Map32ATo16   = mp3216;
91
    llTypes[drv].UnMap32ATo16 = um3216;
92 93 94
    llTypes[drv].Callback     = cb;
}

95 96 97
/******************************************************************
 *		MMDRV_Is32
 *
98
 */
99
BOOL            MMDRV_Is32(unsigned int idx)
100
{
101
    TRACE("(%d)\n", idx);
102
    return MMDrvs[idx].bIs32;
103 104 105 106 107 108 109
}

/**************************************************************************
 * 			MMDRV_GetNum				[internal]
 */
UINT	MMDRV_GetNum(UINT type)
{
110
    TRACE("(%04x)\n", type);
111 112 113 114 115
    assert(type < MMDRV_MAX);
    return llTypes[type].wMaxId;
}

/**************************************************************************
116
 * 				MMDRV_Message			[internal]
117
 */
118 119
DWORD  MMDRV_Message(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1,
                     DWORD_PTR dwParam2, BOOL bFrom32)
120 121 122 123 124
{
    LPWINE_MM_DRIVER 		lpDrv;
    DWORD			ret;
    WINE_MM_DRIVER_PART*	part;
    WINE_LLTYPE*		llType = &llTypes[mld->type];
125
    WINMM_MapType		map;
126 127
    int				devID;

128
    TRACE("(%s %u %u 0x%08lx 0x%08lx 0x%08lx %c)\n",
129
	  llTypes[mld->type].typestr, mld->uDeviceID, wMsg,
130 131 132 133
	  mld->dwDriverInstance, dwParam1, dwParam2, bFrom32?'Y':'N');

    if (mld->uDeviceID == (UINT16)-1) {
	if (!llType->bSupportMapper) {
134
	    WARN("uDev=-1 requested on non-mappable ll type %s\n",
135
		 llTypes[mld->type].typestr);
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	    return MMSYSERR_BADDEVICEID;
	}
	devID = -1;
    } else {
	if (mld->uDeviceID >= llType->wMaxId) {
	    WARN("uDev(%u) requested >= max (%d)\n", mld->uDeviceID, llType->wMaxId);
	    return MMSYSERR_BADDEVICEID;
	}
	devID = mld->uDeviceID;
    }

    lpDrv = &MMDrvs[mld->mmdIndex];
    part = &lpDrv->parts[mld->type];

#if 0
    /* some sanity checks */
    if (!(part->nIDMin <= devID))
	ERR("!(part->nIDMin(%d) <= devID(%d))\n", part->nIDMin, devID);
    if (!(devID < part->nIDMax))
	ERR("!(devID(%d) < part->nIDMax(%d))\n", devID, part->nIDMax);
#endif

    if (lpDrv->bIs32) {
	assert(part->u.fnMessage32);
160

161
	if (bFrom32) {
162
	    TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
163 164
		  mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
            ret = part->u.fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
165
	    TRACE("=> %s\n", WINMM_ErrorToString(ret));
166 167 168
	} else {
	    map = llType->Map16To32A(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
	    switch (map) {
169
	    case WINMM_MAP_NOMEM:
170 171
		ret = MMSYSERR_NOMEM;
		break;
172
	    case WINMM_MAP_MSGERROR:
173 174 175
		FIXME("NIY: no conversion yet 16->32 (%u)\n", wMsg);
		ret = MMSYSERR_ERROR;
		break;
176 177
	    case WINMM_MAP_OK:
	    case WINMM_MAP_OKMEM:
178
		TRACE("Calling message(dev=%u msg=%u usr=0x%08lx 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 186
		break;
	    default:
187
		FIXME("NIY\n");
188 189 190 191 192
		ret = MMSYSERR_NOTSUPPORTED;
		break;
	    }
	}
    } else {
193 194
	assert(part->u.fnMessage16 && pFnCallMMDrvFunc16);
        
195 196 197
	if (bFrom32) {
	    map = llType->Map32ATo16(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
	    switch (map) {
198
	    case WINMM_MAP_NOMEM:
199 200
		ret = MMSYSERR_NOMEM;
		break;
201
	    case WINMM_MAP_MSGERROR:
202 203 204
		FIXME("NIY: no conversion yet 32->16 (%u)\n", wMsg);
		ret = MMSYSERR_ERROR;
		break;
205 206
	    case WINMM_MAP_OK:
	    case WINMM_MAP_OKMEM:
207
		TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
208
		      mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
209
		ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16, 
210 211
                                         mld->uDeviceID, wMsg, mld->dwDriverInstance, 
                                         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 216
		break;
	    default:
217
		FIXME("NIY\n");
218 219 220 221
		ret = MMSYSERR_NOTSUPPORTED;
		break;
	    }
	} else {
222
	    TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
223
		  mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
224
            ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16, 
225 226
                                     mld->uDeviceID, wMsg, mld->dwDriverInstance, 
                                     dwParam1, dwParam2);
227
	    TRACE("=> %s\n", WINMM_ErrorToString(ret));
228 229 230 231 232 233 234 235
	}
    }
    return ret;
}

/**************************************************************************
 * 				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)
238 239
{
    LPWINE_MLD	mld;
240
    UINT_PTR i;
241 242
    TRACE("(%d, %04x, %p, %p, %p, %p, %c)\n",
          size, type, hndl, dwFlags, dwCallback, dwInstance, bFrom32?'Y':'N');
243

244
    mld = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
245
    if (!mld)	return NULL;
246 247

    /* find an empty slot in MM_MLDrvs table */
248 249 250
    for (i = 0; i < MAX_MM_MLDRVS; i++) if (!MM_MLDrvs[i]) break;

    if (i == MAX_MM_MLDRVS) {
251 252
	/* the MM_MLDrvs table could be made growable in the future if needed */
	ERR("Too many open drivers\n");
253
        HeapFree(GetProcessHeap(), 0, mld);
254 255
	return NULL;
    }
256 257
    MM_MLDrvs[i] = mld;
    *hndl = (HANDLE)(i | 0x8000);
258

259
    mld->type = type;
260
    if ((UINT_PTR)*hndl < MMDRV_GetNum(type) || ((UINT_PTR)*hndl >> 16)) {
261 262 263 264 265 266 267 268 269 270 271 272
	/* FIXME: those conditions must be fulfilled so that:
	 * - we can distinguish between device IDs and handles
	 * - we can use handles as 16 or 32 bit entities
	 */
	ERR("Shouldn't happen. Bad allocation scheme\n");
    }

    mld->bFrom32 = bFrom32;
    mld->dwFlags = HIWORD(*dwFlags);
    mld->dwCallback = *dwCallback;
    mld->dwClientInstance = *dwInstance;

273 274 275
    if (llTypes[type].Callback)
    {
        *dwFlags = LOWORD(*dwFlags) | CALLBACK_FUNCTION;
276 277
        *dwCallback = (DWORD_PTR)llTypes[type].Callback;
        *dwInstance = (DWORD_PTR)mld; /* FIXME: wouldn't some 16 bit drivers only use the loword ? */
278
    }
279 280 281 282 283 284 285 286 287

    return mld;
}

/**************************************************************************
 * 				MMDRV_Free			[internal]
 */
void	MMDRV_Free(HANDLE hndl, LPWINE_MLD mld)
{
288 289
    TRACE("(%p, %p)\n", hndl, mld);

290 291
    if ((UINT_PTR)hndl & 0x8000) {
	UINT_PTR idx = (UINT_PTR)hndl & ~0x8000;
292 293 294 295 296 297
	if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
	    MM_MLDrvs[idx] = NULL;
	    HeapFree(GetProcessHeap(), 0, mld);
	    return;
	}
    }
298
    ERR("Bad Handle %p at %p (not freed)\n", hndl, mld);
299 300 301 302 303
}

/**************************************************************************
 * 				MMDRV_Open			[internal]
 */
304
DWORD MMDRV_Open(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1, DWORD dwFlags)
305 306
{
    DWORD		dwRet = MMSYSERR_BADDEVICEID;
307
    DWORD_PTR		dwInstance;
308
    WINE_LLTYPE*	llType = &llTypes[mld->type];
309
    TRACE("(%p, %04x, 0x%08lx, 0x%08x)\n", mld, wMsg, dwParam1, dwFlags);
310

311
    mld->dwDriverInstance = (DWORD_PTR)&dwInstance;
312

313
    if (mld->uDeviceID == (UINT)-1 || mld->uDeviceID == (UINT16)-1) {
314 315 316 317 318 319 320 321 322 323 324 325 326 327
	TRACE("MAPPER mode requested !\n");
	/* check if mapper is supported by type */
	if (llType->bSupportMapper) {
	    if (llType->nMapper == -1) {
		/* no driver for mapper has been loaded, try a dumb implementation */
		TRACE("No mapper loaded, doing it by hand\n");
		for (mld->uDeviceID = 0; mld->uDeviceID < llType->wMaxId; mld->uDeviceID++) {
		    if ((dwRet = MMDRV_Open(mld, wMsg, dwParam1, dwFlags)) == MMSYSERR_NOERROR) {
			/* to share this function epilog */
			dwInstance = mld->dwDriverInstance;
			break;
		    }
		}
	    } else {
328
		mld->uDeviceID = (UINT16)-1;
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
		mld->mmdIndex = llType->lpMlds[-1].mmdIndex;
		TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
		dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags, TRUE);
	    }
	}
    } else {
	if (mld->uDeviceID < llType->wMaxId) {
	    mld->mmdIndex = llType->lpMlds[mld->uDeviceID].mmdIndex;
	    TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
	    dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags, TRUE);
	}
    }
    if (dwRet == MMSYSERR_NOERROR)
	mld->dwDriverInstance = dwInstance;
    return dwRet;
}

/**************************************************************************
 * 				MMDRV_Close			[internal]
 */
DWORD	MMDRV_Close(LPWINE_MLD mld, UINT wMsg)
{
351
    TRACE("(%p, %04x)\n", mld, wMsg);
352 353 354 355
    return MMDRV_Message(mld, wMsg, 0L, 0L, TRUE);
}

/**************************************************************************
356
 * 				MMDRV_GetByID			[internal]
357
 */
Mike McCormack's avatar
Mike McCormack committed
358
static LPWINE_MLD MMDRV_GetByID(UINT uDevID, UINT type)
359
{
360
    TRACE("(%04x, %04x)\n", uDevID, type);
361 362 363 364 365
    if (uDevID < llTypes[type].wMaxId)
	return &llTypes[type].lpMlds[uDevID];
    if ((uDevID == (UINT16)-1 || uDevID == (UINT)-1) && llTypes[type].nMapper != -1)
	return &llTypes[type].lpMlds[-1];
    return NULL;
366 367 368 369 370
}

/**************************************************************************
 * 				MMDRV_Get			[internal]
 */
371
LPWINE_MLD	MMDRV_Get(HANDLE _hndl, UINT type, BOOL bCanBeID)
372 373
{
    LPWINE_MLD	mld = NULL;
374
    UINT_PTR    hndl = (UINT_PTR)_hndl;
375
    TRACE("(%p, %04x, %c)\n", _hndl, type, bCanBeID ? 'Y' : 'N');
376 377 378

    assert(type < MMDRV_MAX);

379
    if (hndl >= llTypes[type].wMaxId &&
380
	hndl != (UINT16)-1 && hndl != (UINT)-1) {
381
	if (hndl & 0x8000) {
382 383
	    UINT idx = hndl & ~0x8000;
	    if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
384 385
                __TRY
                {
386
                    mld = MM_MLDrvs[idx];
387 388
                    if (mld && mld->type != type) mld = NULL;
                }
389
                __EXCEPT_PAGE_FAULT
390 391 392 393
                {
                    mld = NULL;
                }
                __ENDTRY;
394 395
	    }
	}
396 397
    }
    if (mld == NULL && bCanBeID) {
398
	mld = MMDRV_GetByID(hndl, type);
399 400 401 402 403 404 405 406 407 408 409
    }
    return mld;
}

/**************************************************************************
 * 				MMDRV_GetRelated		[internal]
 */
LPWINE_MLD	MMDRV_GetRelated(HANDLE hndl, UINT srcType,
				 BOOL bSrcCanBeID, UINT dstType)
{
    LPWINE_MLD		mld;
410 411
    TRACE("(%p, %04x, %c, %04x)\n",
          hndl, srcType, bSrcCanBeID ? 'Y' : 'N', dstType);
412 413 414 415 416

    if ((mld = MMDRV_Get(hndl, srcType, bSrcCanBeID)) != NULL) {
	WINE_MM_DRIVER_PART*	part = &MMDrvs[mld->mmdIndex].parts[dstType];
	if (part->nIDMin < part->nIDMax)
	    return MMDRV_GetByID(part->nIDMin, dstType);
417
    }
418 419 420 421 422 423
    return NULL;
}

/**************************************************************************
 * 				MMDRV_PhysicalFeatures		[internal]
 */
424 425
UINT	MMDRV_PhysicalFeatures(LPWINE_MLD mld, UINT uMsg,
                               DWORD_PTR dwParam1, DWORD_PTR dwParam2)
426 427 428
{
    WINE_MM_DRIVER*	lpDrv = &MMDrvs[mld->mmdIndex];

429
    TRACE("(%p, %04x, %08lx, %08lx)\n", mld, uMsg, dwParam1, dwParam2);
430 431 432

    /* all those function calls are undocumented */
    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 440
    case DRV_QUERYNAME:
	WARN("NIY QueryName\n");
441
	break;
442
    case DRV_QUERYDRIVERIDS:
443 444
	WARN("NIY call VxD\n");
	/* should call VxD MMDEVLDR with (DevNode, dwParam1 and dwParam2) as pmts
445
	 * dwParam1 is buffer and dwParam2 is sizeof(buffer)
446 447 448
	 * I don't know where the result is stored though
	 */
	break;
449
    case DRV_QUERYMAPPABLE:
450
	return (lpDrv->bIsMapper) ? 2 : 0;
451

452 453
    case DRVM_MAPPER_PREFERRED_GET:
	/* FIXME: get from registry someday */
454
        *((LPDWORD)dwParam1) = -1;      /* No preferred device */
455
        *((LPDWORD)dwParam2) = 0;
456
        break;
457 458 459

    case DRV_QUERYDEVICEINTERFACE:
    case DRV_QUERYDEVICEINTERFACESIZE:
460
        return MMDRV_Message(mld, uMsg, dwParam1, dwParam2, TRUE);
461

462
    case DRV_QUERYDSOUNDIFACE: /* Wine-specific: Retrieve DirectSound interface */
463 464 465
    case DRV_QUERYDSOUNDDESC: /* Wine-specific: Retrieve DirectSound driver description*/
	return MMDRV_Message(mld, uMsg, dwParam1, dwParam2, TRUE);

466 467 468 469 470 471 472 473 474 475
    default:
	WARN("Unknown call %04x\n", uMsg);
	return MMSYSERR_INVALPARAM;
    }
    return 0L;
}

/**************************************************************************
 * 				MMDRV_InitPerType		[internal]
 */
476
static  BOOL	MMDRV_InitPerType(LPWINE_MM_DRIVER lpDrv, UINT type, UINT wMsg)
477 478
{
    WINE_MM_DRIVER_PART*	part = &lpDrv->parts[type];
479 480 481
    DWORD			ret;
    UINT			count = 0;
    int				i, k;
482
    TRACE("(%p, %04x, %04x)\n", lpDrv, type, wMsg);
483 484 485 486 487 488 489 490

    part->nIDMin = part->nIDMax = 0;

    /* for DRVM_INIT and DRVM_ENABLE, dwParam2 should be PnP node */
    /* the DRVM_ENABLE is only required when the PnP node is non zero */

    if (lpDrv->bIs32 && part->u.fnMessage32) {
	ret = part->u.fnMessage32(0, DRVM_INIT, 0L, 0L, 0L);
491
	TRACE("DRVM_INIT => %s\n", WINMM_ErrorToString(ret));
492 493 494 495 496
#if 0
	ret = part->u.fnMessage32(0, DRVM_ENABLE, 0L, 0L, 0L);
	TRACE("DRVM_ENABLE => %08lx\n", ret);
#endif
        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 505
	TRACE("DRVM_ENABLE => %08lx\n", ret);
#endif
506
        count = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
507
                                   0, wMsg, 0L, 0L, 0L);
508 509
    } else {
	return FALSE;
510 511
    }

512
    TRACE("Got %u dev for (%s:%s)\n", count, lpDrv->drvname, llTypes[type].typestr);
513 514 515
    
    if (HIWORD(count))
        return FALSE;
516 517 518

    /* got some drivers */
    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 524
	if (count > 1)
	    ERR("Strange: mapper with %d > 1 devices\n", count);
525
	llTypes[type].nMapper = MMDrvsHi;
526
    } else {
527 528
	if (count == 0)
	    return FALSE;
529 530 531 532 533
	part->nIDMin = llTypes[type].wMaxId;
	llTypes[type].wMaxId += count;
	part->nIDMax = llTypes[type].wMaxId;
    }
    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 538 539
    if (llTypes[type].lpMlds)
        llTypes[type].lpMlds = (LPWINE_MLD)
	HeapReAlloc(GetProcessHeap(), 0, llTypes[type].lpMlds - 1,
540
		    sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
541 542 543 544 545
    else
        llTypes[type].lpMlds = (LPWINE_MLD)
	HeapAlloc(GetProcessHeap(), 0,
		    sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;

546 547
    /* re-build the translation table */
    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 551 552 553
	llTypes[type].lpMlds[-1].type = type;
	llTypes[type].lpMlds[-1].mmdIndex = llTypes[type].nMapper;
	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 558 559 560 561
	    llTypes[type].lpMlds[k].uDeviceID = k;
	    llTypes[type].lpMlds[k].type = type;
	    llTypes[type].lpMlds[k].mmdIndex = i;
	    llTypes[type].lpMlds[k].dwDriverInstance = 0;
	    k++;
562 563
	}
    }
564
    return TRUE;
565 566 567 568 569
}

/**************************************************************************
 * 				MMDRV_Install			[internal]
 */
570
static	BOOL	MMDRV_Install(LPCSTR drvRegName, LPCSTR drvFileName, BOOL bIsMapper)
571
{
572 573
    int			i, count = 0;
    LPWINE_MM_DRIVER	lpDrv = &MMDrvs[MMDrvsHi];
574
    LPWINE_DRIVER	d;
575

576 577 578
    TRACE("('%s', '%s', mapper=%c);\n", drvRegName, drvFileName, bIsMapper ? 'Y' : 'N');

    for (i = 0; i < MMDrvsHi; i++) {
579
        if (!strcmp(drvRegName, MMDrvs[i].drvname)) return FALSE;
580 581
    }

582 583 584 585 586 587
    /* Be sure that size of MMDrvs matches the max number of loadable
     * drivers !!
     * If not just increase size of MMDrvs
     */
    assert(MMDrvsHi <= sizeof(MMDrvs)/sizeof(MMDrvs[0]));

588 589
    memset(lpDrv, 0, sizeof(*lpDrv));

590 591
    if (!(lpDrv->hDriver = OpenDriverA(drvFileName, 0, 0))) {
	WARN("Couldn't open driver '%s'\n", drvFileName);
592 593
	return FALSE;
    }
594

595 596
    d = DRIVER_FindFromHDrvr(lpDrv->hDriver);
    lpDrv->bIs32 = (d->dwFlags & WINE_GDF_16BIT) ? FALSE : TRUE;
597 598

    /* Then look for xxxMessage functions */
599 600
#define	AA(_h,_w,_x,_y,_z)					\
    func = (WINEMM_msgFunc##_y) _z ((_h), #_x);			\
601 602 603 604
    if (func != NULL) 						\
        { lpDrv->parts[_w].u.fnMessage##_y = func; count++; 	\
          TRACE("Got %d bit func '%s'\n", _y, #_x);         }

605
    if (lpDrv->bIs32) {
606 607
	WINEMM_msgFunc32	func;

608 609
	if (d->d.d32.hModule) {
#define A(_x,_y)	AA(d->d.d32.hModule,_x,_y,32,GetProcAddress)
610
	    A(MMDRV_AUX,	auxMessage);
611
	    A(MMDRV_MIXER,	mxdMessage);
612 613 614 615 616 617
	    A(MMDRV_MIDIIN,	midMessage);
	    A(MMDRV_MIDIOUT, 	modMessage);
	    A(MMDRV_WAVEIN,	widMessage);
	    A(MMDRV_WAVEOUT,	wodMessage);
#undef A
	}
618 619
    } else if (WINMM_CheckForMMSystem() && pFnLoadMMDrvFunc16) {
        count += pFnLoadMMDrvFunc16(drvFileName, d, lpDrv);
620
    }
621
#undef AA
622 623

    if (!count) {
624
	CloseDriver(lpDrv->hDriver, 0, 0);
625 626 627 628 629 630 631 632 633
	WARN("No message functions found\n");
	return FALSE;
    }

    /* FIXME: being a mapper or not should be known by another way */
    /* it's known for NE drvs (the description is of the form '*mapper: *'
     * I don't have any clue for PE drvs
     */
    lpDrv->bIsMapper = bIsMapper;
634
    lpDrv->drvname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(drvRegName) + 1), drvRegName);
635 636

    /* Finish init and get the count of the devices */
637 638 639 640 641 642 643 644 645 646 647 648 649 650
    i = 0;
    if (MMDRV_InitPerType(lpDrv, MMDRV_AUX,     AUXDM_GETNUMDEVS))	i = 1;
    if (MMDRV_InitPerType(lpDrv, MMDRV_MIXER,   MXDM_GETNUMDEVS))	i = 1;
    if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIIN,  MIDM_GETNUMDEVS))	i = 1;
    if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIOUT, MODM_GETNUMDEVS))	i = 1;
    if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEIN,  WIDM_GETNUMDEVS))	i = 1;
    if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEOUT, WODM_GETNUMDEVS))	i = 1;
    /* if all those func calls return FALSE, then the driver must be unloaded */
    if (!i) {
	CloseDriver(lpDrv->hDriver, 0, 0);
	HeapFree(GetProcessHeap(), 0, lpDrv->drvname);
	WARN("Driver initialization failed\n");
	return FALSE;
    }
651 652 653

    MMDrvsHi++;

654 655 656 657
    return TRUE;
}

/**************************************************************************
658
 * 				MMDRV_Init
659
 */
660
BOOL	MMDRV_Init(void)
661
{
662
    HKEY	hKey;
663 664 665
    char	driver_buffer[256];
    char	mapper_buffer[256];
    char	midi_buffer[256];
666
    char*	p;
667 668
    DWORD	type, size;
    BOOL	ret = FALSE;
669
    TRACE("()\n");
670

671 672 673
    strcpy(driver_buffer, WINE_DEFAULT_WINMM_DRIVER);
    strcpy(mapper_buffer, WINE_DEFAULT_WINMM_MAPPER);
    strcpy(midi_buffer, WINE_DEFAULT_WINMM_MIDI);
674

675 676 677
    /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
    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);
681 682 683 684 685 686 687 688 689
    }

    p = driver_buffer;
    while (p)
    {
        char filename[sizeof(driver_buffer)+10];
        char *next = strchr(p, ',');
        if (next) *next++ = 0;
        sprintf( filename, "wine%s.drv", p );
690
        if ((ret = MMDRV_Install( filename, filename, FALSE ))) break;
691 692 693
        p = next;
    }

694 695
    ret |= MMDRV_Install("wavemapper", WINE_DEFAULT_WINMM_MAPPER, TRUE);
    ret |= MMDRV_Install("midimapper", WINE_DEFAULT_WINMM_MIDI, TRUE);
696
    return ret;
697
}
698 699 700 701 702 703 704 705 706 707

/******************************************************************
 *		ExitPerType
 *
 *
 */
static  BOOL	MMDRV_ExitPerType(LPWINE_MM_DRIVER lpDrv, UINT type)
{
    WINE_MM_DRIVER_PART*	part = &lpDrv->parts[type];
    DWORD			ret;
708
    TRACE("(%p, %04x)\n", lpDrv, type);
709 710 711 712 713 714 715

    if (lpDrv->bIs32 && part->u.fnMessage32) {
#if 0
	ret = part->u.fnMessage32(0, DRVM_DISABLE, 0L, 0L, 0L);
	TRACE("DRVM_DISABLE => %08lx\n", ret);
#endif
	ret = part->u.fnMessage32(0, DRVM_EXIT, 0L, 0L, 0L);
716
	TRACE("DRVM_EXIT => %s\n", WINMM_ErrorToString(ret));
717 718
    } else if (!lpDrv->bIs32 && part->u.fnMessage16 && pFnCallMMDrvFunc16) {
#if 0
719
	ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
720 721 722
                                 0, DRVM_DISABLE, 0L, 0L, 0L);
	TRACE("DRVM_DISABLE => %08lx\n", ret);
#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 727 728 729 730 731 732 733 734 735 736 737 738 739
    } else {
	return FALSE;
    }

    return TRUE;
}

/******************************************************************
 *		Exit
 *
 *
 */
void    MMDRV_Exit(void)
{
740
    unsigned int i;
741
    TRACE("()\n");
742 743 744 745 746 747 748 749 750 751 752 753 754 755

    for (i = 0; i < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]); i++)
    {
        if (MM_MLDrvs[i] != NULL)
        {
            FIXME("Closing while ll-driver open\n");
#if 0
            /* FIXME: should generate a message depending on type */
            MMDRV_Free((HANDLE)(i | 0x8000), MM_MLDrvs[i]);
#endif
        }
    }

    /* unload driver, in reverse order of loading */
756 757
    i = sizeof(MMDrvs) / sizeof(MMDrvs[0]);
    while (i-- > 0)
758 759 760 761 762 763 764 765 766
    {
        MMDRV_ExitPerType(&MMDrvs[i], MMDRV_AUX);
        MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIXER);
        MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIIN);
        MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIOUT);
        MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEIN);
        MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEOUT);
        CloseDriver(MMDrvs[i].hDriver, 0, 0);
    }
767 768 769 770 771 772 773 774 775 776 777 778
    if (llTypes[MMDRV_AUX].lpMlds)
        HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_AUX].lpMlds - 1);
    if (llTypes[MMDRV_MIXER].lpMlds)
        HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIXER].lpMlds - 1);
    if (llTypes[MMDRV_MIDIIN].lpMlds)
        HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIIN].lpMlds - 1);
    if (llTypes[MMDRV_MIDIOUT].lpMlds)
        HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_MIDIOUT].lpMlds - 1);
    if (llTypes[MMDRV_WAVEIN].lpMlds)
        HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEIN].lpMlds - 1);
    if (llTypes[MMDRV_WAVEOUT].lpMlds)
        HeapFree(GetProcessHeap(), 0, llTypes[MMDRV_WAVEOUT].lpMlds - 1);
779
}