- start assigning custom VK codes from VK_OEM_8 since this code
[wine/hacks.git] / dlls / winmm / lolvldrv.c
blob07ef45de9b0232e4dbca229f2a4eafc0f3a8cabc
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[3];
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 = um1632;
89 llTypes[drv].Callback = cb;
92 /******************************************************************
93 * MMDRV_Is32
96 BOOL MMDRV_Is32(unsigned int idx)
98 return MMDrvs[idx].bIs32;
101 /**************************************************************************
102 * MMDRV_GetDescription32 [internal]
104 static BOOL MMDRV_GetDescription32(const char* fname, char* buf, int buflen)
106 OFSTRUCT ofs;
107 DWORD h;
108 LPVOID ptr = 0;
109 LPVOID val;
110 DWORD dw;
111 BOOL ret = FALSE;
112 UINT u;
113 FARPROC pGetFileVersionInfoSizeA;
114 FARPROC pGetFileVersionInfoA;
115 FARPROC pVerQueryValueA;
116 HMODULE hmodule = 0;
118 #define E(_x) do {TRACE _x;goto theEnd;} while(0)
120 if (OpenFile(fname, &ofs, OF_EXIST)==HFILE_ERROR) E(("Can't find file %s\n", fname));
122 if (!(hmodule = LoadLibraryA( "version.dll" ))) goto theEnd;
123 if (!(pGetFileVersionInfoSizeA = GetProcAddress( hmodule, "GetFileVersionInfoSizeA" )))
124 goto theEnd;
125 if (!(pGetFileVersionInfoA = GetProcAddress( hmodule, "GetFileVersionInfoA" )))
126 goto theEnd;
127 if (!(pVerQueryValueA = GetProcAddress( hmodule, "VerQueryValueA" )))
128 goto theEnd;
130 if (!(dw = pGetFileVersionInfoSizeA(ofs.szPathName, &h))) E(("Can't get FVIS\n"));
131 if (!(ptr = HeapAlloc(GetProcessHeap(), 0, dw))) E(("OOM\n"));
132 if (!pGetFileVersionInfoA(ofs.szPathName, h, dw, ptr)) E(("Can't get FVI\n"));
134 #define A(_x) if (pVerQueryValueA(ptr, "\\StringFileInfo\\040904B0\\" #_x, &val, &u)) \
135 TRACE(#_x " => %s\n", (LPSTR)val); else TRACE(#_x " @\n")
137 A(CompanyName);
138 A(FileDescription);
139 A(FileVersion);
140 A(InternalName);
141 A(LegalCopyright);
142 A(OriginalFilename);
143 A(ProductName);
144 A(ProductVersion);
145 A(Comments);
146 A(LegalTrademarks);
147 A(PrivateBuild);
148 A(SpecialBuild);
149 #undef A
151 if (!pVerQueryValueA(ptr, "\\StringFileInfo\\040904B0\\ProductName", &val, &u)) E(("Can't get product name\n"));
152 lstrcpynA(buf, val, buflen);
154 #undef E
155 ret = TRUE;
156 theEnd:
157 HeapFree(GetProcessHeap(), 0, ptr);
158 if (hmodule) FreeLibrary( hmodule );
159 return ret;
162 /**************************************************************************
163 * MMDRV_GetNum [internal]
165 UINT MMDRV_GetNum(UINT type)
167 assert(type < MMDRV_MAX);
168 return llTypes[type].wMaxId;
171 /**************************************************************************
172 * MMDRV_Message [internal]
174 DWORD MMDRV_Message(LPWINE_MLD mld, UINT wMsg, DWORD_PTR dwParam1,
175 DWORD_PTR dwParam2, BOOL bFrom32)
177 LPWINE_MM_DRIVER lpDrv;
178 DWORD ret;
179 WINE_MM_DRIVER_PART* part;
180 WINE_LLTYPE* llType = &llTypes[mld->type];
181 WINMM_MapType map;
182 int devID;
184 TRACE("(%s %u %u 0x%08lx 0x%08lx 0x%08lx %c)\n",
185 llTypes[mld->type].typestr, mld->uDeviceID, wMsg,
186 mld->dwDriverInstance, dwParam1, dwParam2, bFrom32?'Y':'N');
188 if (mld->uDeviceID == (UINT16)-1) {
189 if (!llType->bSupportMapper) {
190 WARN("uDev=-1 requested on non-mappable ll type %s\n",
191 llTypes[mld->type].typestr);
192 return MMSYSERR_BADDEVICEID;
194 devID = -1;
195 } else {
196 if (mld->uDeviceID >= llType->wMaxId) {
197 WARN("uDev(%u) requested >= max (%d)\n", mld->uDeviceID, llType->wMaxId);
198 return MMSYSERR_BADDEVICEID;
200 devID = mld->uDeviceID;
203 lpDrv = &MMDrvs[mld->mmdIndex];
204 part = &lpDrv->parts[mld->type];
206 #if 0
207 /* some sanity checks */
208 if (!(part->nIDMin <= devID))
209 ERR("!(part->nIDMin(%d) <= devID(%d))\n", part->nIDMin, devID);
210 if (!(devID < part->nIDMax))
211 ERR("!(devID(%d) < part->nIDMax(%d))\n", devID, part->nIDMax);
212 #endif
214 if (lpDrv->bIs32) {
215 assert(part->u.fnMessage32);
217 if (bFrom32) {
218 TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
219 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
220 ret = part->u.fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
221 TRACE("=> %lu\n", ret);
222 } else {
223 map = llType->Map16To32A(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
224 switch (map) {
225 case WINMM_MAP_NOMEM:
226 ret = MMSYSERR_NOMEM;
227 break;
228 case WINMM_MAP_MSGERROR:
229 FIXME("NIY: no conversion yet 16->32 (%u)\n", wMsg);
230 ret = MMSYSERR_ERROR;
231 break;
232 case WINMM_MAP_OK:
233 case WINMM_MAP_OKMEM:
234 TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
235 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
236 ret = part->u.fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance,
237 dwParam1, dwParam2);
238 TRACE("=> %lu\n", ret);
239 if (map == WINMM_MAP_OKMEM)
240 llType->UnMap16To32A(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2, ret);
241 break;
242 default:
243 FIXME("NIY\n");
244 ret = MMSYSERR_NOTSUPPORTED;
245 break;
248 } else {
249 assert(part->u.fnMessage16 && pFnCallMMDrvFunc16);
251 if (bFrom32) {
252 map = llType->Map32ATo16(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
253 switch (map) {
254 case WINMM_MAP_NOMEM:
255 ret = MMSYSERR_NOMEM;
256 break;
257 case WINMM_MAP_MSGERROR:
258 FIXME("NIY: no conversion yet 32->16 (%u)\n", wMsg);
259 ret = MMSYSERR_ERROR;
260 break;
261 case WINMM_MAP_OK:
262 case WINMM_MAP_OKMEM:
263 TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
264 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
265 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
266 mld->uDeviceID, wMsg, mld->dwDriverInstance,
267 dwParam1, dwParam2);
268 TRACE("=> %lu\n", ret);
269 if (map == WINMM_MAP_OKMEM)
270 llType->UnMap32ATo16(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2, ret);
271 break;
272 default:
273 FIXME("NIY\n");
274 ret = MMSYSERR_NOTSUPPORTED;
275 break;
277 } else {
278 TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx)\n",
279 mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
280 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
281 mld->uDeviceID, wMsg, mld->dwDriverInstance,
282 dwParam1, dwParam2);
283 TRACE("=> %lu\n", ret);
286 return ret;
289 /**************************************************************************
290 * MMDRV_Alloc [internal]
292 LPWINE_MLD MMDRV_Alloc(UINT size, UINT type, LPHANDLE hndl, DWORD* dwFlags,
293 DWORD* dwCallback, DWORD* dwInstance, BOOL bFrom32)
295 LPWINE_MLD mld;
296 UINT i;
298 mld = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
299 if (!mld) return NULL;
301 /* find an empty slot in MM_MLDrvs table */
302 for (i = 0; i < MAX_MM_MLDRVS; i++) if (!MM_MLDrvs[i]) break;
304 if (i == MAX_MM_MLDRVS) {
305 /* the MM_MLDrvs table could be made growable in the future if needed */
306 ERR("Too many open drivers\n");
307 return NULL;
309 MM_MLDrvs[i] = mld;
310 *hndl = (HANDLE)(i | 0x8000);
312 mld->type = type;
313 if ((UINT)*hndl < MMDRV_GetNum(type) || HIWORD(*hndl) != 0) {
314 /* FIXME: those conditions must be fulfilled so that:
315 * - we can distinguish between device IDs and handles
316 * - we can use handles as 16 or 32 bit entities
318 ERR("Shouldn't happen. Bad allocation scheme\n");
321 mld->bFrom32 = bFrom32;
322 mld->dwFlags = HIWORD(*dwFlags);
323 mld->dwCallback = *dwCallback;
324 mld->dwClientInstance = *dwInstance;
326 if (llTypes[type].Callback)
328 *dwFlags = LOWORD(*dwFlags) | CALLBACK_FUNCTION;
329 *dwCallback = (DWORD)llTypes[type].Callback;
330 *dwInstance = (DWORD)mld; /* FIXME: wouldn't some 16 bit drivers only use the loword ? */
333 return mld;
336 /**************************************************************************
337 * MMDRV_Free [internal]
339 void MMDRV_Free(HANDLE hndl, LPWINE_MLD mld)
341 if ((UINT)hndl & 0x8000) {
342 unsigned idx = (UINT)hndl & ~0x8000;
343 if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
344 MM_MLDrvs[idx] = NULL;
345 HeapFree(GetProcessHeap(), 0, mld);
346 return;
349 ERR("Bad Handle %p at %p (not freed)\n", hndl, mld);
352 /**************************************************************************
353 * MMDRV_Open [internal]
355 DWORD MMDRV_Open(LPWINE_MLD mld, UINT wMsg, DWORD dwParam1, DWORD dwFlags)
357 DWORD dwRet = MMSYSERR_BADDEVICEID;
358 DWORD dwInstance;
359 WINE_LLTYPE* llType = &llTypes[mld->type];
361 mld->dwDriverInstance = (DWORD)&dwInstance;
363 if (mld->uDeviceID == (UINT)-1 || mld->uDeviceID == (UINT16)-1) {
364 TRACE("MAPPER mode requested !\n");
365 /* check if mapper is supported by type */
366 if (llType->bSupportMapper) {
367 if (llType->nMapper == -1) {
368 /* no driver for mapper has been loaded, try a dumb implementation */
369 TRACE("No mapper loaded, doing it by hand\n");
370 for (mld->uDeviceID = 0; mld->uDeviceID < llType->wMaxId; mld->uDeviceID++) {
371 if ((dwRet = MMDRV_Open(mld, wMsg, dwParam1, dwFlags)) == MMSYSERR_NOERROR) {
372 /* to share this function epilog */
373 dwInstance = mld->dwDriverInstance;
374 break;
377 } else {
378 mld->uDeviceID = (UINT16)-1;
379 mld->mmdIndex = llType->lpMlds[-1].mmdIndex;
380 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
381 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags, TRUE);
384 } else {
385 if (mld->uDeviceID < llType->wMaxId) {
386 mld->mmdIndex = llType->lpMlds[mld->uDeviceID].mmdIndex;
387 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
388 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags, TRUE);
391 if (dwRet == MMSYSERR_NOERROR)
392 mld->dwDriverInstance = dwInstance;
393 return dwRet;
396 /**************************************************************************
397 * MMDRV_Close [internal]
399 DWORD MMDRV_Close(LPWINE_MLD mld, UINT wMsg)
401 return MMDRV_Message(mld, wMsg, 0L, 0L, TRUE);
404 /**************************************************************************
405 * MMDRV_GetByID [internal]
407 LPWINE_MLD MMDRV_GetByID(UINT uDevID, UINT type)
409 if (uDevID < llTypes[type].wMaxId)
410 return &llTypes[type].lpMlds[uDevID];
411 if ((uDevID == (UINT16)-1 || uDevID == (UINT)-1) && llTypes[type].nMapper != -1)
412 return &llTypes[type].lpMlds[-1];
413 return NULL;
416 /**************************************************************************
417 * MMDRV_Get [internal]
419 LPWINE_MLD MMDRV_Get(HANDLE _hndl, UINT type, BOOL bCanBeID)
421 LPWINE_MLD mld = NULL;
422 UINT hndl = (UINT)_hndl;
424 assert(type < MMDRV_MAX);
426 if (hndl >= llTypes[type].wMaxId &&
427 hndl != (UINT16)-1 && hndl != (UINT)-1) {
428 if (hndl & 0x8000) {
429 hndl = hndl & ~0x8000;
430 if (hndl < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
431 mld = MM_MLDrvs[hndl];
432 if (!mld || !HeapValidate(GetProcessHeap(), 0, mld) || mld->type != type)
433 mld = NULL;
435 hndl = hndl | 0x8000;
438 if (mld == NULL && bCanBeID) {
439 mld = MMDRV_GetByID(hndl, type);
441 return mld;
444 /**************************************************************************
445 * MMDRV_GetRelated [internal]
447 LPWINE_MLD MMDRV_GetRelated(HANDLE hndl, UINT srcType,
448 BOOL bSrcCanBeID, UINT dstType)
450 LPWINE_MLD mld;
452 if ((mld = MMDRV_Get(hndl, srcType, bSrcCanBeID)) != NULL) {
453 WINE_MM_DRIVER_PART* part = &MMDrvs[mld->mmdIndex].parts[dstType];
454 if (part->nIDMin < part->nIDMax)
455 return MMDRV_GetByID(part->nIDMin, dstType);
457 return NULL;
460 /**************************************************************************
461 * MMDRV_PhysicalFeatures [internal]
463 UINT MMDRV_PhysicalFeatures(LPWINE_MLD mld, UINT uMsg, DWORD dwParam1,
464 DWORD dwParam2)
466 WINE_MM_DRIVER* lpDrv = &MMDrvs[mld->mmdIndex];
468 TRACE("(%p, %04x, %08lx, %08lx)\n", mld, uMsg, dwParam1, dwParam2);
470 /* all those function calls are undocumented */
471 switch (uMsg) {
472 case DRV_QUERYDRVENTRY:
473 lstrcpynA((LPSTR)dwParam1, lpDrv->drvname, LOWORD(dwParam2));
474 break;
475 case DRV_QUERYDEVNODE:
476 *(LPDWORD)dwParam1 = 0L; /* should be DevNode */
477 break;
478 case DRV_QUERYNAME:
479 WARN("NIY QueryName\n");
480 break;
481 case DRV_QUERYDRIVERIDS:
482 WARN("NIY call VxD\n");
483 /* should call VxD MMDEVLDR with (DevNode, dwParam1 and dwParam2) as pmts
484 * dwParam1 is buffer and dwParam2 is sizeof(buffer)
485 * I don't know where the result is stored though
487 break;
488 case DRV_QUERYMAPPABLE:
489 return (lpDrv->bIsMapper) ? 2 : 0;
491 case DRVM_MAPPER_PREFERRED_GET:
492 /* FIXME: get from registry someday */
493 *((LPDWORD)dwParam1) = -1; /* No preferred device */
494 break;
496 case DRV_QUERYDEVICEINTERFACE:
497 case DRV_QUERYDEVICEINTERFACESIZE:
498 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2, TRUE);
500 case DRV_QUERYDSOUNDIFACE: /* Wine-specific: Retrieve DirectSound interface */
501 case DRV_QUERYDSOUNDDESC: /* Wine-specific: Retrieve DirectSound driver description*/
502 case DRV_QUERYDSOUNDGUID: /* Wine-specific: Retrieve DirectSound driver GUID */
503 return MMDRV_Message(mld, uMsg, dwParam1, dwParam2, TRUE);
505 default:
506 WARN("Unknown call %04x\n", uMsg);
507 return MMSYSERR_INVALPARAM;
509 return 0L;
512 /**************************************************************************
513 * MMDRV_InitPerType [internal]
515 static BOOL MMDRV_InitPerType(LPWINE_MM_DRIVER lpDrv, UINT type, UINT wMsg)
517 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
518 DWORD ret;
519 UINT count = 0;
520 int i, k;
522 part->nIDMin = part->nIDMax = 0;
524 /* for DRVM_INIT and DRVM_ENABLE, dwParam2 should be PnP node */
525 /* the DRVM_ENABLE is only required when the PnP node is non zero */
527 if (lpDrv->bIs32 && part->u.fnMessage32) {
528 ret = part->u.fnMessage32(0, DRVM_INIT, 0L, 0L, 0L);
529 TRACE("DRVM_INIT => %08lx\n", ret);
530 #if 0
531 ret = part->u.fnMessage32(0, DRVM_ENABLE, 0L, 0L, 0L);
532 TRACE("DRVM_ENABLE => %08lx\n", ret);
533 #endif
534 count = part->u.fnMessage32(0, wMsg, 0L, 0L, 0L);
535 } else if (!lpDrv->bIs32 && part->u.fnMessage16 && pFnCallMMDrvFunc16) {
536 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
537 0, DRVM_INIT, 0L, 0L, 0L);
538 TRACE("DRVM_INIT => %08lx\n", ret);
539 #if 0
540 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
541 0, DRVM_ENABLE, 0L, 0L, 0L);
542 TRACE("DRVM_ENABLE => %08lx\n", ret);
543 #endif
544 count = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
545 0, wMsg, 0L, 0L, 0L);
546 } else {
547 return FALSE;
550 TRACE("Got %u dev for (%s:%s)\n", count, lpDrv->drvname, llTypes[type].typestr);
552 /* got some drivers */
553 if (lpDrv->bIsMapper) {
554 /* it seems native mappers return 0 devices :-( */
555 if (llTypes[type].nMapper != -1)
556 ERR("Two mappers for type %s (%d, %s)\n",
557 llTypes[type].typestr, llTypes[type].nMapper, lpDrv->drvname);
558 if (count > 1)
559 ERR("Strange: mapper with %d > 1 devices\n", count);
560 llTypes[type].nMapper = MMDrvsHi;
561 } else {
562 if (count == 0)
563 return FALSE;
564 part->nIDMin = llTypes[type].wMaxId;
565 llTypes[type].wMaxId += count;
566 part->nIDMax = llTypes[type].wMaxId;
568 TRACE("Setting min=%d max=%d (ttop=%d) for (%s:%s)\n",
569 part->nIDMin, part->nIDMax, llTypes[type].wMaxId,
570 lpDrv->drvname, llTypes[type].typestr);
571 /* realloc translation table */
572 if (llTypes[type].lpMlds)
573 llTypes[type].lpMlds = (LPWINE_MLD)
574 HeapReAlloc(GetProcessHeap(), 0, llTypes[type].lpMlds - 1,
575 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
576 else
577 llTypes[type].lpMlds = (LPWINE_MLD)
578 HeapAlloc(GetProcessHeap(), 0,
579 sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
581 /* re-build the translation table */
582 if (llTypes[type].nMapper != -1) {
583 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, -1, MMDrvs[llTypes[type].nMapper].drvname);
584 llTypes[type].lpMlds[-1].uDeviceID = (UINT16)-1;
585 llTypes[type].lpMlds[-1].type = type;
586 llTypes[type].lpMlds[-1].mmdIndex = llTypes[type].nMapper;
587 llTypes[type].lpMlds[-1].dwDriverInstance = 0;
589 for (i = k = 0; i <= MMDrvsHi; i++) {
590 while (MMDrvs[i].parts[type].nIDMin <= k && k < MMDrvs[i].parts[type].nIDMax) {
591 TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, k, MMDrvs[i].drvname);
592 llTypes[type].lpMlds[k].uDeviceID = k;
593 llTypes[type].lpMlds[k].type = type;
594 llTypes[type].lpMlds[k].mmdIndex = i;
595 llTypes[type].lpMlds[k].dwDriverInstance = 0;
596 k++;
599 return TRUE;
602 /**************************************************************************
603 * MMDRV_Install [internal]
605 static BOOL MMDRV_Install(LPCSTR drvRegName, LPCSTR drvFileName, BOOL bIsMapper)
607 int i, count = 0;
608 LPWINE_MM_DRIVER lpDrv = &MMDrvs[MMDrvsHi];
609 LPWINE_DRIVER d;
611 TRACE("('%s', '%s', mapper=%c);\n", drvRegName, drvFileName, bIsMapper ? 'Y' : 'N');
613 /* be sure that size of MMDrvs matches the max number of loadable drivers !!
614 * if not just increase size of MMDrvs */
615 assert(MMDrvsHi <= sizeof(MMDrvs)/sizeof(MMDrvs[0]));
617 for (i = 0; i < MMDrvsHi; i++) {
618 if (!strcmp(drvRegName, MMDrvs[i].drvname)) return FALSE;
621 memset(lpDrv, 0, sizeof(*lpDrv));
623 if (!(lpDrv->hDriver = OpenDriverA(drvFileName, 0, 0))) {
624 WARN("Couldn't open driver '%s'\n", drvFileName);
625 return FALSE;
628 d = DRIVER_FindFromHDrvr(lpDrv->hDriver);
629 lpDrv->bIs32 = (d->dwFlags & WINE_GDF_16BIT) ? FALSE : TRUE;
631 /* Then look for xxxMessage functions */
632 #define AA(_h,_w,_x,_y,_z) \
633 func = (WINEMM_msgFunc##_y) _z ((_h), #_x); \
634 if (func != NULL) \
635 { lpDrv->parts[_w].u.fnMessage##_y = func; count++; \
636 TRACE("Got %d bit func '%s'\n", _y, #_x); }
638 if (lpDrv->bIs32) {
639 WINEMM_msgFunc32 func;
640 char buffer[128];
642 if (d->d.d32.hModule) {
643 #define A(_x,_y) AA(d->d.d32.hModule,_x,_y,32,GetProcAddress)
644 A(MMDRV_AUX, auxMessage);
645 A(MMDRV_MIXER, mxdMessage);
646 A(MMDRV_MIDIIN, midMessage);
647 A(MMDRV_MIDIOUT, modMessage);
648 A(MMDRV_WAVEIN, widMessage);
649 A(MMDRV_WAVEOUT, wodMessage);
650 #undef A
652 if (TRACE_ON(winmm)) {
653 if (MMDRV_GetDescription32(drvFileName, buffer, sizeof(buffer)))
654 TRACE("%s => %s\n", drvFileName, buffer);
655 else
656 TRACE("%s => No description\n", drvFileName);
658 } else if (WINMM_CheckForMMSystem() && pFnLoadMMDrvFunc16) {
659 count += pFnLoadMMDrvFunc16(drvFileName, d, lpDrv);
661 #undef AA
663 if (!count) {
664 CloseDriver(lpDrv->hDriver, 0, 0);
665 WARN("No message functions found\n");
666 return FALSE;
669 /* FIXME: being a mapper or not should be known by another way */
670 /* it's known for NE drvs (the description is of the form '*mapper: *'
671 * I don't have any clue for PE drvs
673 lpDrv->bIsMapper = bIsMapper;
674 lpDrv->drvname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(drvRegName) + 1), drvRegName);
676 /* Finish init and get the count of the devices */
677 i = 0;
678 if (MMDRV_InitPerType(lpDrv, MMDRV_AUX, AUXDM_GETNUMDEVS)) i = 1;
679 if (MMDRV_InitPerType(lpDrv, MMDRV_MIXER, MXDM_GETNUMDEVS)) i = 1;
680 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIIN, MIDM_GETNUMDEVS)) i = 1;
681 if (MMDRV_InitPerType(lpDrv, MMDRV_MIDIOUT, MODM_GETNUMDEVS)) i = 1;
682 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEIN, WIDM_GETNUMDEVS)) i = 1;
683 if (MMDRV_InitPerType(lpDrv, MMDRV_WAVEOUT, WODM_GETNUMDEVS)) i = 1;
684 /* if all those func calls return FALSE, then the driver must be unloaded */
685 if (!i) {
686 CloseDriver(lpDrv->hDriver, 0, 0);
687 HeapFree(GetProcessHeap(), 0, lpDrv->drvname);
688 WARN("Driver initialization failed\n");
689 return FALSE;
692 MMDrvsHi++;
694 return TRUE;
697 /**************************************************************************
698 * MMDRV_InitFromRegistry [internal]
700 static BOOL MMDRV_InitFromRegistry(void)
702 HKEY hKey;
703 char buffer[256];
704 char* p1;
705 char* p2;
706 DWORD type, size;
707 BOOL ret = FALSE;
709 if (RegCreateKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\WinMM", &hKey)) {
710 TRACE("Cannot open WinMM config key\n");
711 return FALSE;
714 size = sizeof(buffer);
715 if (!RegQueryValueExA(hKey, "Drivers", 0, &type, (LPVOID)buffer, &size)) {
716 p1 = buffer;
717 while (p1) {
718 p2 = strchr(p1, ';');
719 if (p2) *p2++ = '\0';
720 ret |= MMDRV_Install(p1, p1, FALSE);
721 p1 = p2;
725 /* finish with mappers */
726 size = sizeof(buffer);
727 if (!RegQueryValueExA(hKey, "WaveMapper", 0, &type, (LPVOID)buffer, &size))
728 ret |= MMDRV_Install("wavemapper", buffer, TRUE);
729 size = sizeof(buffer);
730 if (!RegQueryValueExA(hKey, "MidiMapper", 0, &type, (LPVOID)buffer, &size))
731 ret |= MMDRV_Install("midimapper", buffer, TRUE);
733 RegCloseKey(hKey);
735 return ret;
738 /**************************************************************************
739 * MMDRV_InitHardcoded [internal]
741 static BOOL MMDRV_InitHardcoded(void)
743 /* first load hardware drivers */
744 MMDRV_Install("wineoss.drv", "wineoss.drv", FALSE);
746 /* finish with mappers */
747 MMDRV_Install("wavemapper", "msacm.drv", TRUE);
748 MMDRV_Install("midimapper", "midimap.drv", TRUE);
750 return TRUE;
753 /**************************************************************************
754 * MMDRV_Init [internal]
756 BOOL MMDRV_Init(void)
758 /* FIXME: MMDRV_InitFromRegistry shall be MMDRV_Init in a near future */
759 return MMDRV_InitFromRegistry() || MMDRV_InitHardcoded();
762 /******************************************************************
763 * ExitPerType
767 static BOOL MMDRV_ExitPerType(LPWINE_MM_DRIVER lpDrv, UINT type)
769 WINE_MM_DRIVER_PART* part = &lpDrv->parts[type];
770 DWORD ret;
772 if (lpDrv->bIs32 && part->u.fnMessage32) {
773 #if 0
774 ret = part->u.fnMessage32(0, DRVM_DISABLE, 0L, 0L, 0L);
775 TRACE("DRVM_DISABLE => %08lx\n", ret);
776 #endif
777 ret = part->u.fnMessage32(0, DRVM_EXIT, 0L, 0L, 0L);
778 TRACE("DRVM_EXIT => %08lx\n", ret);
779 } else if (!lpDrv->bIs32 && part->u.fnMessage16 && pFnCallMMDrvFunc16) {
780 #if 0
781 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
782 0, DRVM_DISABLE, 0L, 0L, 0L);
783 TRACE("DRVM_DISABLE => %08lx\n", ret);
784 #endif
785 ret = pFnCallMMDrvFunc16((DWORD)part->u.fnMessage16,
786 0, DRVM_EXIT, 0L, 0L, 0L);
787 TRACE("DRVM_EXIT => %08lx\n", ret);
788 } else {
789 return FALSE;
792 return TRUE;
795 /******************************************************************
796 * Exit
800 void MMDRV_Exit(void)
802 int i;
804 for (i = 0; i < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]); i++)
806 if (MM_MLDrvs[i] != NULL)
808 FIXME("Closing while ll-driver open\n");
809 #if 0
810 /* FIXME: should generate a message depending on type */
811 MMDRV_Free((HANDLE)(i | 0x8000), MM_MLDrvs[i]);
812 #endif
816 /* unload driver, in reverse order of loading */
817 for (i = sizeof(MMDrvs) / sizeof(MMDrvs[0]) - 1; i >= 0; i--)
819 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_AUX);
820 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIXER);
821 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIIN);
822 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_MIDIOUT);
823 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEIN);
824 MMDRV_ExitPerType(&MMDrvs[i], MMDRV_WAVEOUT);
825 CloseDriver(MMDrvs[i].hDriver, 0, 0);