Make all newly created surfces dirty, so that they are loaded properly
[wine/multimedia.git] / dlls / winmm / lolvldrv.c
blob8dbee9380c06c2ce625374cbd8aeb7beb866d7ab
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * MMSYTEM 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <string.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <assert.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include "winver.h"
31 #include "winemm.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
36 LRESULT (*pFnCallMMDrvFunc16)(DWORD,WORD,WORD,LONG,LONG,LONG) /* = NULL */;
37 unsigned (*pFnLoadMMDrvFunc16)(LPCSTR,LPWINE_DRIVER, LPWINE_MM_DRIVER) /* = NULL */;
39 /* each known type of driver has an instance of this structure */
40 typedef struct tagWINE_LLTYPE {
41 /* those attributes depend on the specification of the type */
42 LPCSTR typestr; /* name (for debugging) */
43 BOOL bSupportMapper; /* if type is allowed to support mapper */
44 MMDRV_MAPFUNC Map16To32A; /* those are function pointers to handle */
45 MMDRV_UNMAPFUNC UnMap16To32A; /* the parameter conversion (16 vs 32 bit) */
46 MMDRV_MAPFUNC Map32ATo16; /* when hi-func (in mmsystem or winmm) and */
47 MMDRV_UNMAPFUNC UnMap32ATo16; /* low-func (in .drv) do not match */
48 LPDRVCALLBACK Callback; /* handles callback for a specified type */
49 /* those attributes reflect the loaded/current situation for the type */
50 UINT wMaxId; /* number of loaded devices (sum across all loaded drivers */
51 LPWINE_MLD lpMlds; /* "static" mlds to access the part though device IDs */
52 int nMapper; /* index to mapper */
53 } WINE_LLTYPE;
55 static int MMDrvsHi /* = 0 */;
56 static WINE_MM_DRIVER MMDrvs[8];
57 static LPWINE_MLD MM_MLDrvs[40];
58 #define MAX_MM_MLDRVS (sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]))
60 #define A(_x,_y) {#_y, _x, NULL, NULL, NULL, NULL, NULL, 0, NULL, -1}
61 /* Note: the indices of this array must match the definitions
62 * of the MMDRV_???? manifest constants
64 static WINE_LLTYPE llTypes[MMDRV_MAX] = {
65 A(TRUE, Aux),
66 A(FALSE, Mixer),
67 A(TRUE, MidiIn),
68 A(TRUE, MidiOut),
69 A(TRUE, WaveIn),
70 A(TRUE, WaveOut),
72 #undef A
74 /******************************************************************
75 * MMDRV_InstallMap
79 void MMDRV_InstallMap(unsigned int drv,
80 MMDRV_MAPFUNC mp1632, MMDRV_UNMAPFUNC um1632,
81 MMDRV_MAPFUNC mp3216, MMDRV_UNMAPFUNC um3216,
82 LPDRVCALLBACK cb)
84 assert(drv < MMDRV_MAX);
85 llTypes[drv].Map16To32A = mp1632;
86 llTypes[drv].UnMap16To32A = um1632;
87 llTypes[drv].Map32ATo16 = mp3216;
88 llTypes[drv].UnMap32ATo16 = um3216;
89 llTypes[drv].Callback = cb;
92 /******************************************************************
93 * MMDRV_Is32
96 BOOL MMDRV_Is32(unsigned int idx)
98 TRACE("(%d)\n", idx);
99 return MMDrvs[idx].bIs32;
102 /**************************************************************************
103 * MMDRV_GetDescription32 [internal]
105 static BOOL MMDRV_GetDescription32(const char* fname, char* buf, int buflen)
107 OFSTRUCT ofs;
108 DWORD h;
109 LPVOID ptr = 0;
110 LPVOID val;
111 DWORD dw;
112 BOOL ret = FALSE;
113 UINT u;
114 FARPROC pGetFileVersionInfoSizeA;
115 FARPROC pGetFileVersionInfoA;
116 FARPROC pVerQueryValueA;
117 HMODULE hmodule = 0;
118 TRACE("(%p, %p, %d)\n", fname, buf, buflen);
120 #define E(_x) do {TRACE _x;goto theEnd;} while(0)
122 if (OpenFile(fname, &ofs, OF_EXIST)==HFILE_ERROR) E(("Can't find file %s\n", fname));
124 if (!(hmodule = LoadLibraryA( "version.dll" ))) goto theEnd;
125 if (!(pGetFileVersionInfoSizeA = GetProcAddress( hmodule, "GetFileVersionInfoSizeA" )))
126 goto theEnd;
127 if (!(pGetFileVersionInfoA = GetProcAddress( hmodule, "GetFileVersionInfoA" )))
128 goto theEnd;
129 if (!(pVerQueryValueA = GetProcAddress( hmodule, "VerQueryValueA" )))
130 goto theEnd;
132 if (!(dw = pGetFileVersionInfoSizeA(ofs.szPathName, &h))) E(("Can't get FVIS\n"));
133 if (!(ptr = HeapAlloc(GetProcessHeap(), 0, dw))) E(("OOM\n"));
134 if (!pGetFileVersionInfoA(ofs.szPathName, h, dw, ptr)) E(("Can't get FVI\n"));
136 #define A(_x) if (pVerQueryValueA(ptr, "\\StringFileInfo\\040904B0\\" #_x, &val, &u)) \
137 TRACE(#_x " => %s\n", (LPSTR)val); else TRACE(#_x " @\n")
139 A(CompanyName);
140 A(FileDescription);
141 A(FileVersion);
142 A(InternalName);
143 A(LegalCopyright);
144 A(OriginalFilename);
145 A(ProductName);
146 A(ProductVersion);
147 A(Comments);
148 A(LegalTrademarks);
149 A(PrivateBuild);
150 A(SpecialBuild);
151 #undef A
153 if (!pVerQueryValueA(ptr, "\\StringFileInfo\\040904B0\\ProductName", &val, &u)) E(("Can't get product name\n"));
154 lstrcpynA(buf, val, buflen);
156 #undef E
157 ret = TRUE;
158 theEnd:
159 HeapFree(GetProcessHeap(), 0, ptr);
160 if (hmodule) FreeLibrary( hmodule );
161 return ret;
164 /**************************************************************************
165 * MMDRV_GetNum [internal]
167 UINT MMDRV_GetNum(UINT type)
169 TRACE("(%04x)\n", type);
170 assert(type < MMDRV_MAX);
171 return llTypes[type].wMaxId;
174 /**************************************************************************
175 * MMDRV_Message [internal]
177 DWORD MMDRV_Message(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1,
178 DWORD_PTR dwParam2, BOOL bFrom32)
180 LPWINE_MM_DRIVER lpDrv;
181 DWORD ret;
182 WINE_MM_DRIVER_PART* part;
183 WINE_LLTYPE* llType = &llTypes[mld->type];
184 WINMM_MapType map;
185 int devID;
187 TRACE("(%s %u %u 0x%08lx 0x%08lx 0x%08lx %c)\n",
188 llTypes[mld->type].typestr, mld->uDeviceID, wMsg,
189 mld->dwDriverInstance, dwParam1, dwParam2, bFrom32?'Y':'N');
191 if (mld->uDeviceID == (UINT16)-1) {
192 if (!llType->bSupportMapper) {
193 WARN("uDev=-1 requested on non-mappable ll type %s\n",
194 llTypes[mld->type].typestr);
195 return MMSYSERR_BADDEVICEID;
197 devID = -1;
198 } else {
199 if (mld->uDeviceID >= llType->wMaxId) {
200 WARN("uDev(%u) requested >= max (%d)\n", mld->uDeviceID, llType->wMaxId);
201 return MMSYSERR_BADDEVICEID;
203 devID = mld->uDeviceID;
206 lpDrv = &MMDrvs[mld->mmdIndex];
207 part = &lpDrv->parts[mld->type];
209 #if 0
210 /* some sanity checks */
211 if (!(part->nIDMin <= devID))
212 ERR("!(part->nIDMin(%d) <= devID(%d))\n", part->nIDMin, devID);
213 if (!(devID < part->nIDMax))
214 ERR("!(devID(%d) < part->nIDMax(%d))\n", devID, part->nIDMax);
215 #endif
217 if (lpDrv->bIs32) {
218 assert(part->u.fnMessage32);
220 if (bFrom32) {
221 TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
222 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
223 ret = part->u.fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
224 TRACE("=> %s\n", WINMM_ErrorToString(ret));
225 } else {
226 map = llType->Map16To32A(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
227 switch (map) {
228 case WINMM_MAP_NOMEM:
229 ret = MMSYSERR_NOMEM;
230 break;
231 case WINMM_MAP_MSGERROR:
232 FIXME("NIY: no conversion yet 16->32 (%u)\n", wMsg);
233 ret = MMSYSERR_ERROR;
234 break;
235 case WINMM_MAP_OK:
236 case WINMM_MAP_OKMEM:
237 TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
238 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
239 ret = part->u.fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance,
240 dwParam1, dwParam2);
241 TRACE("=> %s\n", WINMM_ErrorToString(ret));
242 if (map == WINMM_MAP_OKMEM)
243 llType->UnMap16To32A(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2, ret);
244 break;
245 default:
246 FIXME("NIY\n");
247 ret = MMSYSERR_NOTSUPPORTED;
248 break;
251 } else {
252 assert(part->u.fnMessage16 && pFnCallMMDrvFunc16);
254 if (bFrom32) {
255 map = llType->Map32ATo16(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
256 switch (map) {
257 case WINMM_MAP_NOMEM:
258 ret = MMSYSERR_NOMEM;
259 break;
260 case WINMM_MAP_MSGERROR:
261 FIXME("NIY: no conversion yet 32->16 (%u)\n", wMsg);
262 ret = MMSYSERR_ERROR;
263 break;
264 case WINMM_MAP_OK:
265 case WINMM_MAP_OKMEM:
266 TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
267 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
268 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
269 mld->uDeviceID, wMsg, mld->dwDriverInstance,
270 dwParam1, dwParam2);
271 TRACE("=> %s\n", WINMM_ErrorToString(ret));
272 if (map == WINMM_MAP_OKMEM)
273 llType->UnMap32ATo16(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2, ret);
274 break;
275 default:
276 FIXME("NIY\n");
277 ret = MMSYSERR_NOTSUPPORTED;
278 break;
280 } else {
281 TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
282 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
283 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
284 mld->uDeviceID, wMsg, mld->dwDriverInstance,
285 dwParam1, dwParam2);
286 TRACE("=> %s\n", WINMM_ErrorToString(ret));
289 return ret;
292 /**************************************************************************
293 * MMDRV_Alloc [internal]
295 LPWINE_MLD MMDRV_Alloc(UINT size, UINT type, LPHANDLE hndl, DWORD* dwFlags,
296 DWORD* dwCallback, DWORD* dwInstance, BOOL bFrom32)
298 LPWINE_MLD mld;
299 UINT i;
300 TRACE("(%d, %04x, %p, %p, %p, %p, %c)\n",
301 size, type, hndl, dwFlags, dwCallback, dwInstance, bFrom32?'Y':'N');
303 mld = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
304 if (!mld) return NULL;
306 /* find an empty slot in MM_MLDrvs table */
307 for (i = 0; i < MAX_MM_MLDRVS; i++) if (!MM_MLDrvs[i]) break;
309 if (i == MAX_MM_MLDRVS) {
310 /* the MM_MLDrvs table could be made growable in the future if needed */
311 ERR("Too many open drivers\n");
312 HeapFree(GetProcessHeap(), 0, mld);
313 return NULL;
315 MM_MLDrvs[i] = mld;
316 *hndl = (HANDLE)(i | 0x8000);
318 mld->type = type;
319 if ((UINT)*hndl < MMDRV_GetNum(type) || HIWORD(*hndl) != 0) {
320 /* FIXME: those conditions must be fulfilled so that:
321 * - we can distinguish between device IDs and handles
322 * - we can use handles as 16 or 32 bit entities
324 ERR("Shouldn't happen. Bad allocation scheme\n");
327 mld->bFrom32 = bFrom32;
328 mld->dwFlags = HIWORD(*dwFlags);
329 mld->dwCallback = *dwCallback;
330 mld->dwClientInstance = *dwInstance;
332 if (llTypes[type].Callback)
334 *dwFlags = LOWORD(*dwFlags) | CALLBACK_FUNCTION;
335 *dwCallback = (DWORD)llTypes[type].Callback;
336 *dwInstance = (DWORD)mld; /* FIXME: wouldn't some 16 bit drivers only use the loword ? */
339 return mld;
342 /**************************************************************************
343 * MMDRV_Free [internal]
345 void MMDRV_Free(HANDLE hndl, LPWINE_MLD mld)
347 TRACE("(%p, %p)\n", hndl, mld);
349 if ((UINT)hndl & 0x8000) {
350 unsigned idx = (UINT)hndl & ~0x8000;
351 if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
352 MM_MLDrvs[idx] = NULL;
353 HeapFree(GetProcessHeap(), 0, mld);
354 return;
357 ERR("Bad Handle %p at %p (not freed)\n", hndl, mld);
360 /**************************************************************************
361 * MMDRV_Open [internal]
363 DWORD MMDRV_Open(LPWINE_MLD mld, UINT wMsg, DWORD dwParam1, DWORD dwFlags)
365 DWORD dwRet = MMSYSERR_BADDEVICEID;
366 DWORD dwInstance;
367 WINE_LLTYPE* llType = &llTypes[mld->type];
368 TRACE("(%p, %04x, 0x%08lx, 0x%08lx)\n", mld, wMsg, dwParam1, dwFlags);
370 mld->dwDriverInstance = (DWORD)&dwInstance;
372 if (mld->uDeviceID == (UINT)-1 || mld->uDeviceID == (UINT16)-1) {
373 TRACE("MAPPER mode requested !\n");
374 /* check if mapper is supported by type */
375 if (llType->bSupportMapper) {
376 if (llType->nMapper == -1) {
377 /* no driver for mapper has been loaded, try a dumb implementation */
378 TRACE("No mapper loaded, doing it by hand\n");
379 for (mld->uDeviceID = 0; mld->uDeviceID < llType->wMaxId; mld->uDeviceID++) {
380 if ((dwRet = MMDRV_Open(mld, wMsg, dwParam1, dwFlags)) == MMSYSERR_NOERROR) {
381 /* to share this function epilog */
382 dwInstance = mld->dwDriverInstance;
383 break;
386 } else {
387 mld->uDeviceID = (UINT16)-1;
388 mld->mmdIndex = llType->lpMlds[-1].mmdIndex;
389 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
390 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags, TRUE);
393 } else {
394 if (mld->uDeviceID < llType->wMaxId) {
395 mld->mmdIndex = llType->lpMlds[mld->uDeviceID].mmdIndex;
396 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
397 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags, TRUE);
400 if (dwRet == MMSYSERR_NOERROR)
401 mld->dwDriverInstance = dwInstance;
402 return dwRet;
405 /**************************************************************************
406 * MMDRV_Close [internal]
408 DWORD MMDRV_Close(LPWINE_MLD mld, UINT wMsg)
410 TRACE("(%p, %04x)\n", mld, wMsg);
411 return MMDRV_Message(mld, wMsg, 0L, 0L, TRUE);
414 /**************************************************************************
415 * MMDRV_GetByID [internal]
417 static LPWINE_MLD MMDRV_GetByID(UINT uDevID, UINT type)
419 TRACE("(%04x, %04x)\n", uDevID, type);
420 if (uDevID < llTypes[type].wMaxId)
421 return &llTypes[type].lpMlds[uDevID];
422 if ((uDevID == (UINT16)-1 || uDevID == (UINT)-1) && llTypes[type].nMapper != -1)
423 return &llTypes[type].lpMlds[-1];
424 return NULL;
427 /**************************************************************************
428 * MMDRV_Get [internal]
430 LPWINE_MLD MMDRV_Get(HANDLE _hndl, UINT type, BOOL bCanBeID)
432 LPWINE_MLD mld = NULL;
433 UINT hndl = (UINT)_hndl;
434 TRACE("(%p, %04x, %c)\n", _hndl, type, bCanBeID ? 'Y' : 'N');
436 assert(type < MMDRV_MAX);
438 if (hndl >= llTypes[type].wMaxId &&
439 hndl != (UINT16)-1 && hndl != (UINT)-1) {
440 if (hndl & 0x8000) {
441 hndl = hndl & ~0x8000;
442 if (hndl < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
443 mld = MM_MLDrvs[hndl];
444 if (!mld || !HeapValidate(GetProcessHeap(), 0, mld) || mld->type != type)
445 mld = NULL;
447 hndl = hndl | 0x8000;
450 if (mld == NULL && bCanBeID) {
451 mld = MMDRV_GetByID(hndl, type);
453 return mld;
456 /**************************************************************************
457 * MMDRV_GetRelated [internal]
459 LPWINE_MLD MMDRV_GetRelated(HANDLE hndl, UINT srcType,
460 BOOL bSrcCanBeID, UINT dstType)
462 LPWINE_MLD mld;
463 TRACE("(%p, %04x, %c, %04x)\n",
464 hndl, srcType, bSrcCanBeID ? 'Y' : 'N', dstType);
466 if ((mld = MMDRV_Get(hndl, srcType, bSrcCanBeID)) != NULL) {
467 WINE_MM_DRIVER_PART* part = &MMDrvs[mld->mmdIndex].parts[dstType];
468 if (part->nIDMin < part->nIDMax)
469 return MMDRV_GetByID(part->nIDMin, dstType);
471 return NULL;
474 /**************************************************************************
475 * MMDRV_PhysicalFeatures [internal]
477 UINT MMDRV_PhysicalFeatures(LPWINE_MLD mld, UINT uMsg, DWORD dwParam1,
478 DWORD dwParam2)
480 WINE_MM_DRIVER* lpDrv = &MMDrvs[mld->mmdIndex];
482 TRACE("(%p, %04x, %08lx, %08lx)\n", mld, uMsg, dwParam1, dwParam2);
484 /* all those function calls are undocumented */
485 switch (uMsg) {
486 case DRV_QUERYDRVENTRY:
487 lstrcpynA((LPSTR)dwParam1, lpDrv->drvname, LOWORD(dwParam2));
488 break;
489 case DRV_QUERYDEVNODE:
490 *(LPDWORD)dwParam1 = 0L; /* should be DevNode */
491 break;
492 case DRV_QUERYNAME:
493 WARN("NIY QueryName\n");
494 break;
495 case DRV_QUERYDRIVERIDS:
496 WARN("NIY call VxD\n");
497 /* should call VxD MMDEVLDR with (DevNode, dwParam1 and dwParam2) as pmts
498 * dwParam1 is buffer and dwParam2 is sizeof(buffer)
499 * I don't know where the result is stored though
501 break;
502 case DRV_QUERYMAPPABLE:
503 return (lpDrv->bIsMapper) ? 2 : 0;
505 case DRVM_MAPPER_PREFERRED_GET:
506 /* FIXME: get from registry someday */
507 *((LPDWORD)dwParam1) = -1; /* No preferred device */
508 *((LPDWORD)dwParam2) = 0;
509 break;
511 case DRV_QUERYDEVICEINTERFACE:
512 case DRV_QUERYDEVICEINTERFACESIZE:
513 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2, TRUE);
515 case DRV_QUERYDSOUNDIFACE: /* Wine-specific: Retrieve DirectSound interface */
516 case DRV_QUERYDSOUNDDESC: /* Wine-specific: Retrieve DirectSound driver description*/
517 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2, TRUE);
519 default:
520 WARN("Unknown call %04x\n", uMsg);
521 return MMSYSERR_INVALPARAM;
523 return 0L;
526 /**************************************************************************
527 * MMDRV_InitPerType [internal]
529 static BOOL MMDRV_InitPerType(LPWINE_MM_DRIVER lpDrv, UINT type, UINT wMsg)
531 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
532 DWORD ret;
533 UINT count = 0;
534 int i, k;
535 TRACE("(%p, %04x, %04x)\n", lpDrv, type, wMsg);
537 part->nIDMin = part->nIDMax = 0;
539 /* for DRVM_INIT and DRVM_ENABLE, dwParam2 should be PnP node */
540 /* the DRVM_ENABLE is only required when the PnP node is non zero */
542 if (lpDrv->bIs32 && part->u.fnMessage32) {
543 ret = part->u.fnMessage32(0, DRVM_INIT, 0L, 0L, 0L);
544 TRACE("DRVM_INIT => %s\n", WINMM_ErrorToString(ret));
545 #if 0
546 ret = part->u.fnMessage32(0, DRVM_ENABLE, 0L, 0L, 0L);
547 TRACE("DRVM_ENABLE => %08lx\n", ret);
548 #endif
549 count = part->u.fnMessage32(0, wMsg, 0L, 0L, 0L);
550 } else if (!lpDrv->bIs32 && part->u.fnMessage16 && pFnCallMMDrvFunc16) {
551 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
552 0, DRVM_INIT, 0L, 0L, 0L);
553 TRACE("DRVM_INIT => %s\n", WINMM_ErrorToString(ret));
554 #if 0
555 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
556 0, DRVM_ENABLE, 0L, 0L, 0L);
557 TRACE("DRVM_ENABLE => %08lx\n", ret);
558 #endif
559 count = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
560 0, wMsg, 0L, 0L, 0L);
561 } else {
562 return FALSE;
565 TRACE("Got %u dev for (%s:%s)\n", count, lpDrv->drvname, llTypes[type].typestr);
567 if (HIWORD(count))
568 return FALSE;
570 /* got some drivers */
571 if (lpDrv->bIsMapper) {
572 /* it seems native mappers return 0 devices :-( */
573 if (llTypes[type].nMapper != -1)
574 ERR("Two mappers for type %s (%d, %s)\n",
575 llTypes[type].typestr, llTypes[type].nMapper, lpDrv->drvname);
576 if (count > 1)
577 ERR("Strange: mapper with %d > 1 devices\n", count);
578 llTypes[type].nMapper = MMDrvsHi;
579 } else {
580 if (count == 0)
581 return FALSE;
582 part->nIDMin = llTypes[type].wMaxId;
583 llTypes[type].wMaxId += count;
584 part->nIDMax = llTypes[type].wMaxId;
586 TRACE("Setting min=%d max=%d (ttop=%d) for (%s:%s)\n",
587 part->nIDMin, part->nIDMax, llTypes[type].wMaxId,
588 lpDrv->drvname, llTypes[type].typestr);
589 /* realloc translation table */
590 if (llTypes[type].lpMlds)
591 llTypes[type].lpMlds = (LPWINE_MLD)
592 HeapReAlloc(GetProcessHeap(), 0, llTypes[type].lpMlds - 1,
593 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
594 else
595 llTypes[type].lpMlds = (LPWINE_MLD)
596 HeapAlloc(GetProcessHeap(), 0,
597 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
599 /* re-build the translation table */
600 if (llTypes[type].nMapper != -1) {
601 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, -1, MMDrvs[llTypes[type].nMapper].drvname);
602 llTypes[type].lpMlds[-1].uDeviceID = (UINT16)-1;
603 llTypes[type].lpMlds[-1].type = type;
604 llTypes[type].lpMlds[-1].mmdIndex = llTypes[type].nMapper;
605 llTypes[type].lpMlds[-1].dwDriverInstance = 0;
607 for (i = k = 0; i <= MMDrvsHi; i++) {
608 while (MMDrvs[i].parts[type].nIDMin <= k && k < MMDrvs[i].parts[type].nIDMax) {
609 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, k, MMDrvs[i].drvname);
610 llTypes[type].lpMlds[k].uDeviceID = k;
611 llTypes[type].lpMlds[k].type = type;
612 llTypes[type].lpMlds[k].mmdIndex = i;
613 llTypes[type].lpMlds[k].dwDriverInstance = 0;
614 k++;
617 return TRUE;
620 /**************************************************************************
621 * MMDRV_Install [internal]
623 static BOOL MMDRV_Install(LPCSTR drvRegName, LPCSTR drvFileName, BOOL bIsMapper)
625 int i, count = 0;
626 LPWINE_MM_DRIVER lpDrv = &MMDrvs[MMDrvsHi];
627 LPWINE_DRIVER d;
629 TRACE("('%s', '%s', mapper=%c);\n", drvRegName, drvFileName, bIsMapper ? 'Y' : 'N');
631 for (i = 0; i < MMDrvsHi; i++) {
632 if (!strcmp(drvRegName, MMDrvs[i].drvname)) return FALSE;
635 /* Be sure that size of MMDrvs matches the max number of loadable
636 * drivers !!
637 * If not just increase size of MMDrvs
639 assert(MMDrvsHi <= sizeof(MMDrvs)/sizeof(MMDrvs[0]));
641 memset(lpDrv, 0, sizeof(*lpDrv));
643 if (!(lpDrv->hDriver = OpenDriverA(drvFileName, 0, 0))) {
644 WARN("Couldn't open driver '%s'\n", drvFileName);
645 return FALSE;
648 d = DRIVER_FindFromHDrvr(lpDrv->hDriver);
649 lpDrv->bIs32 = (d->dwFlags & WINE_GDF_16BIT) ? FALSE : TRUE;
651 /* Then look for xxxMessage functions */
652 #define AA(_h,_w,_x,_y,_z) \
653 func = (WINEMM_msgFunc##_y) _z ((_h), #_x); \
654 if (func != NULL) \
655 { lpDrv->parts[_w].u.fnMessage##_y = func; count++; \
656 TRACE("Got %d bit func '%s'\n", _y, #_x); }
658 if (lpDrv->bIs32) {
659 WINEMM_msgFunc32 func;
660 char buffer[128];
662 if (d->d.d32.hModule) {
663 #define A(_x,_y) AA(d->d.d32.hModule,_x,_y,32,GetProcAddress)
664 A(MMDRV_AUX, auxMessage);
665 A(MMDRV_MIXER, mxdMessage);
666 A(MMDRV_MIDIIN, midMessage);
667 A(MMDRV_MIDIOUT, modMessage);
668 A(MMDRV_WAVEIN, widMessage);
669 A(MMDRV_WAVEOUT, wodMessage);
670 #undef A
672 if (TRACE_ON(winmm)) {
673 if (MMDRV_GetDescription32(drvFileName, buffer, sizeof(buffer)))
674 TRACE("%s => %s\n", drvFileName, buffer);
675 else
676 TRACE("%s => No description\n", drvFileName);
678 } else if (WINMM_CheckForMMSystem() && pFnLoadMMDrvFunc16) {
679 count += pFnLoadMMDrvFunc16(drvFileName, d, lpDrv);
681 #undef AA
683 if (!count) {
684 CloseDriver(lpDrv->hDriver, 0, 0);
685 WARN("No message functions found\n");
686 return FALSE;
689 /* FIXME: being a mapper or not should be known by another way */
690 /* it's known for NE drvs (the description is of the form '*mapper: *'
691 * I don't have any clue for PE drvs
693 lpDrv->bIsMapper = bIsMapper;
694 lpDrv->drvname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(drvRegName) + 1), drvRegName);
696 /* Finish init and get the count of the devices */
697 i = 0;
698 if (MMDRV_InitPerType(lpDrv, MMDRV_AUX, AUXDM_GETNUMDEVS)) i = 1;
699 if (MMDRV_InitPerType(lpDrv, MMDRV_MIXER, MXDM_GETNUMDEVS)) i = 1;
700 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIIN, MIDM_GETNUMDEVS)) i = 1;
701 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIOUT, MODM_GETNUMDEVS)) i = 1;
702 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEIN, WIDM_GETNUMDEVS)) i = 1;
703 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEOUT, WODM_GETNUMDEVS)) i = 1;
704 /* if all those func calls return FALSE, then the driver must be unloaded */
705 if (!i) {
706 CloseDriver(lpDrv->hDriver, 0, 0);
707 HeapFree(GetProcessHeap(), 0, lpDrv->drvname);
708 WARN("Driver initialization failed\n");
709 return FALSE;
712 MMDrvsHi++;
714 return TRUE;
717 /**************************************************************************
718 * MMDRV_Init
720 BOOL MMDRV_Init(void)
722 HKEY hKey;
723 char driver_buffer[256];
724 char mapper_buffer[256];
725 char midi_buffer[256];
726 char* p;
727 DWORD type, size;
728 BOOL ret = FALSE;
729 TRACE("()\n");
731 strcpy(driver_buffer, WINE_DEFAULT_WINMM_DRIVER);
732 strcpy(mapper_buffer, WINE_DEFAULT_WINMM_MAPPER);
733 strcpy(midi_buffer, WINE_DEFAULT_WINMM_MIDI);
735 /* @@ Wine registry key: HKCU\Software\Wine\Drivers */
736 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Drivers", &hKey))
738 size = sizeof(driver_buffer);
739 if (RegQueryValueExA(hKey, "Audio", 0, &type, (LPVOID)driver_buffer, &size))
740 strcpy(driver_buffer, WINE_DEFAULT_WINMM_DRIVER);
743 p = driver_buffer;
744 while (p)
746 char filename[sizeof(driver_buffer)+10];
747 char *next = strchr(p, ',');
748 if (next) *next++ = 0;
749 sprintf( filename, "wine%s.drv", p );
750 ret |= MMDRV_Install( filename, filename, FALSE );
751 p = next;
754 /* finish with mappers */
756 /* @@ Wine registry key: HKLM\Software\Wine\Wine\Config\WinMM */
757 if (! RegCreateKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\WinMM", &hKey)) {
758 size = sizeof(mapper_buffer);
759 if (RegQueryValueExA(hKey, "WaveMapper", 0, &type, (LPVOID)mapper_buffer, &size))
760 strcpy(mapper_buffer, WINE_DEFAULT_WINMM_MAPPER);
762 size = sizeof(midi_buffer);
763 if (RegQueryValueExA(hKey, "MidiMapper", 0, &type, (LPVOID)midi_buffer, &size))
764 strcpy(midi_buffer, WINE_DEFAULT_WINMM_MIDI);
766 RegCloseKey(hKey);
769 ret |= MMDRV_Install("wavemapper", mapper_buffer, TRUE);
770 ret |= MMDRV_Install("midimapper", midi_buffer, TRUE);
771 return ret;
774 /******************************************************************
775 * ExitPerType
779 static BOOL MMDRV_ExitPerType(LPWINE_MM_DRIVER lpDrv, UINT type)
781 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
782 DWORD ret;
783 TRACE("(%p, %04x)\n", lpDrv, type);
785 if (lpDrv->bIs32 && part->u.fnMessage32) {
786 #if 0
787 ret = part->u.fnMessage32(0, DRVM_DISABLE, 0L, 0L, 0L);
788 TRACE("DRVM_DISABLE => %08lx\n", ret);
789 #endif
790 ret = part->u.fnMessage32(0, DRVM_EXIT, 0L, 0L, 0L);
791 TRACE("DRVM_EXIT => %s\n", WINMM_ErrorToString(ret));
792 } else if (!lpDrv->bIs32 && part->u.fnMessage16 && pFnCallMMDrvFunc16) {
793 #if 0
794 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
795 0, DRVM_DISABLE, 0L, 0L, 0L);
796 TRACE("DRVM_DISABLE => %08lx\n", ret);
797 #endif
798 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
799 0, DRVM_EXIT, 0L, 0L, 0L);
800 TRACE("DRVM_EXIT => %s\n", WINMM_ErrorToString(ret));
801 } else {
802 return FALSE;
805 return TRUE;
808 /******************************************************************
809 * Exit
813 void MMDRV_Exit(void)
815 int i;
816 TRACE("()\n");
818 for (i = 0; i < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]); i++)
820 if (MM_MLDrvs[i] != NULL)
822 FIXME("Closing while ll-driver open\n");
823 #if 0
824 /* FIXME: should generate a message depending on type */
825 MMDRV_Free((HANDLE)(i | 0x8000), MM_MLDrvs[i]);
826 #endif
830 /* unload driver, in reverse order of loading */
831 for (i = sizeof(MMDrvs) / sizeof(MMDrvs[0]) - 1; i >= 0; i--)
833 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_AUX);
834 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIXER);
835 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIIN);
836 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIOUT);
837 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEIN);
838 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEOUT);
839 CloseDriver(MMDrvs[i].hDriver, 0, 0);