Move low level multimedia message mapping out of 32 bit code.
[wine] / dlls / winmm / lolvldrv.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  * MMSYTEM low level drivers handling functions
5  *
6  * Copyright 1999 Eric Pouech
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <string.h>
24 #include <stdio.h>
25 #include <assert.h>
26 #include "wine/winbase16.h"
27 #include "winreg.h"
28 #include "winver.h"
29 #include "winemm.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(mmsys);
33
34 extern WORD CALLBACK MMDRV_CallTo16_word_wwlll(FARPROC16,WORD,WORD,LONG,LONG,LONG);
35
36 /* each known type of driver has an instance of this structure */
37 typedef struct tagWINE_LLTYPE {
38     /* those attributes depend on the specification of the type */
39     LPSTR               typestr;        /* name (for debugging) */
40     BOOL                bSupportMapper; /* if type is allowed to support mapper */
41     MMDRV_MAPFUNC       Map16To32A;     /* those are function pointers to handle */
42     MMDRV_MAPFUNC       UnMap16To32A;   /*   the parameter conversion (16 vs 32 bit) */
43     MMDRV_MAPFUNC       Map32ATo16;     /*   when hi-func (in mmsystem or winmm) and */
44     MMDRV_MAPFUNC       UnMap32ATo16;   /*   low-func (in .drv) do not match */
45     LPDRVCALLBACK       Callback;       /* handles callback for a specified type */
46     /* those attributes reflect the loaded/current situation for the type */
47     UINT                wMaxId;         /* number of loaded devices (sum across all loaded drivers */
48     LPWINE_MLD          lpMlds;         /* "static" mlds to access the part though device IDs */
49     int                 nMapper;        /* index to mapper */
50 } WINE_LLTYPE;
51
52 static int              MMDrvsHi /* = 0 */;
53 static WINE_MM_DRIVER   MMDrvs[3];
54 static LPWINE_MLD       MM_MLDrvs[40];
55 #define MAX_MM_MLDRVS   (sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0]))
56
57 /******************************************************************
58  *              MMDRV_Is32
59  *
60  */
61 BOOL                    MMDRV_Is32(unsigned int idx)
62 {
63     return MMDrvs[idx].bIs32;
64 }
65
66 /**************************************************************************
67  *                              MMDRV_GetDescription32          [internal]
68  */
69 static  BOOL    MMDRV_GetDescription32(const char* fname, char* buf, int buflen)
70 {
71     OFSTRUCT    ofs;
72     DWORD       h;
73     LPVOID      ptr = 0;
74     LPVOID      val;
75     DWORD       dw;
76     BOOL        ret = FALSE;
77     UINT        u;
78     FARPROC pGetFileVersionInfoSizeA;
79     FARPROC pGetFileVersionInfoA;
80     FARPROC pVerQueryValueA;
81     HMODULE hmodule = 0;
82
83 #define E(_x)   do {TRACE _x;goto theEnd;} while(0)
84
85     if (OpenFile(fname, &ofs, OF_EXIST)==HFILE_ERROR)           E(("Can't find file %s\n", fname));
86
87     if (!(hmodule = LoadLibraryA( "version.dll" ))) goto theEnd;
88     if (!(pGetFileVersionInfoSizeA = GetProcAddress( hmodule, "GetFileVersionInfoSizeA" )))
89         goto theEnd;
90     if (!(pGetFileVersionInfoA = GetProcAddress( hmodule, "GetFileVersionInfoA" )))
91         goto theEnd;
92     if (!(pVerQueryValueA = GetProcAddress( hmodule, "pVerQueryValueA" )))
93         goto theEnd;
94
95     if (!(dw = pGetFileVersionInfoSizeA(ofs.szPathName, &h)))   E(("Can't get FVIS\n"));
96     if (!(ptr = HeapAlloc(GetProcessHeap(), 0, dw)))            E(("OOM\n"));
97     if (!pGetFileVersionInfoA(ofs.szPathName, h, dw, ptr))      E(("Can't get FVI\n"));
98
99 #define A(_x) if (pVerQueryValueA(ptr, "\\StringFileInfo\\040904B0\\" #_x, &val, &u)) \
100                   TRACE(#_x " => %s\n", (LPSTR)val); else TRACE(#_x " @\n")
101
102     A(CompanyName);
103     A(FileDescription);
104     A(FileVersion);
105     A(InternalName);
106     A(LegalCopyright);
107     A(OriginalFilename);
108     A(ProductName);
109     A(ProductVersion);
110     A(Comments);
111     A(LegalTrademarks);
112     A(PrivateBuild);
113     A(SpecialBuild);
114 #undef A
115
116     if (!pVerQueryValueA(ptr, "\\StringFileInfo\\040904B0\\ProductName", &val, &u)) E(("Can't get product name\n"));
117     lstrcpynA(buf, val, buflen);
118
119 #undef E
120     ret = TRUE;
121 theEnd:
122     HeapFree(GetProcessHeap(), 0, ptr);
123     if (hmodule) FreeLibrary( hmodule );
124     return ret;
125 }
126
127 #define A(_x,_y) {#_y, _x, \
128 MMDRV_##_y##_Map16To32A, MMDRV_##_y##_UnMap16To32A, \
129 MMDRV_##_y##_Map32ATo16, MMDRV_##_y##_UnMap32ATo16, \
130 MMDRV_##_y##_Callback, 0, NULL, -1}
131
132 /* Note: the indices of this array must match the definitions
133  *       of the MMDRV_???? manifest constants
134  */
135 static WINE_LLTYPE      llTypes[MMDRV_MAX] = {
136     A(TRUE,  Aux),
137     A(FALSE, Mixer),
138     A(TRUE,  MidiIn),
139     A(TRUE,  MidiOut),
140     A(TRUE,  WaveIn),
141     A(TRUE,  WaveOut),
142 };
143 #undef A
144
145 /**************************************************************************
146  *                      MMDRV_GetNum                            [internal]
147  */
148 UINT    MMDRV_GetNum(UINT type)
149 {
150     assert(type < MMDRV_MAX);
151     return llTypes[type].wMaxId;
152 }
153
154 /**************************************************************************
155  *                              WINE_Message                    [internal]
156  */
157 DWORD   MMDRV_Message(LPWINE_MLD mld, WORD wMsg, DWORD dwParam1,
158                       DWORD dwParam2, BOOL bFrom32)
159 {
160     LPWINE_MM_DRIVER            lpDrv;
161     DWORD                       ret;
162     WINE_MM_DRIVER_PART*        part;
163     WINE_LLTYPE*                llType = &llTypes[mld->type];
164     WINMM_MapType               map;
165     int                         devID;
166
167     TRACE("(%s %u %u 0x%08lx 0x%08lx 0x%08lx %c)!\n",
168           llTypes[mld->type].typestr, mld->uDeviceID, wMsg,
169           mld->dwDriverInstance, dwParam1, dwParam2, bFrom32?'Y':'N');
170
171     if (mld->uDeviceID == (UINT16)-1) {
172         if (!llType->bSupportMapper) {
173             WARN("uDev=-1 requested on non-mappable ll type %s\n",
174                  llTypes[mld->type].typestr);
175             return MMSYSERR_BADDEVICEID;
176         }
177         devID = -1;
178     } else {
179         if (mld->uDeviceID >= llType->wMaxId) {
180             WARN("uDev(%u) requested >= max (%d)\n", mld->uDeviceID, llType->wMaxId);
181             return MMSYSERR_BADDEVICEID;
182         }
183         devID = mld->uDeviceID;
184     }
185
186     lpDrv = &MMDrvs[mld->mmdIndex];
187     part = &lpDrv->parts[mld->type];
188
189 #if 0
190     /* some sanity checks */
191     if (!(part->nIDMin <= devID))
192         ERR("!(part->nIDMin(%d) <= devID(%d))\n", part->nIDMin, devID);
193     if (!(devID < part->nIDMax))
194         ERR("!(devID(%d) < part->nIDMax(%d))\n", devID, part->nIDMax);
195 #endif
196
197     if (lpDrv->bIs32) {
198         assert(part->u.fnMessage32);
199
200         if (bFrom32) {
201             TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx\n",
202                   mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
203             ret = part->u.fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
204             TRACE("=> %lu\n", ret);
205         } else {
206             map = llType->Map16To32A(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
207             switch (map) {
208             case WINMM_MAP_NOMEM:
209                 ret = MMSYSERR_NOMEM;
210                 break;
211             case WINMM_MAP_MSGERROR:
212                 FIXME("NIY: no conversion yet 16->32 (%u)\n", wMsg);
213                 ret = MMSYSERR_ERROR;
214                 break;
215             case WINMM_MAP_OK:
216             case WINMM_MAP_OKMEM:
217                 TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx\n",
218                       mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
219                 ret = part->u.fnMessage32(mld->uDeviceID, wMsg, mld->dwDriverInstance,
220                                           dwParam1, dwParam2);
221                 TRACE("=> %lu\n", ret);
222                 if (map == WINMM_MAP_OKMEM)
223                     llType->UnMap16To32A(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
224                 break;
225             default:
226                 FIXME("NIY ?\n");
227                 ret = MMSYSERR_NOTSUPPORTED;
228                 break;
229             }
230         }
231     } else {
232         assert(part->u.fnMessage16);
233
234         if (bFrom32) {
235             map = llType->Map32ATo16(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
236             switch (map) {
237             case WINMM_MAP_NOMEM:
238                 ret = MMSYSERR_NOMEM;
239                 break;
240             case WINMM_MAP_MSGERROR:
241                 FIXME("NIY: no conversion yet 32->16 (%u)\n", wMsg);
242                 ret = MMSYSERR_ERROR;
243                 break;
244             case WINMM_MAP_OK:
245             case WINMM_MAP_OKMEM:
246                 TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx\n",
247                       mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
248                 ret = MMDRV_CallTo16_word_wwlll((FARPROC16)part->u.fnMessage16, mld->uDeviceID,
249                                                 wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
250                 TRACE("=> %lu\n", ret);
251                 if (map == WINMM_MAP_OKMEM)
252                     llType->UnMap32ATo16(wMsg, &mld->dwDriverInstance, &dwParam1, &dwParam2);
253                 break;
254             default:
255                 FIXME("NIY\n");
256                 ret = MMSYSERR_NOTSUPPORTED;
257                 break;
258             }
259         } else {
260             TRACE("Calling message(dev=%u msg=%u usr=0x%08lx p1=0x%08lx p2=0x%08lx\n",
261                   mld->uDeviceID, wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
262             ret = MMDRV_CallTo16_word_wwlll((FARPROC16)part->u.fnMessage16, mld->uDeviceID,
263                                             wMsg, mld->dwDriverInstance, dwParam1, dwParam2);
264             TRACE("=> %lu\n", ret);
265         }
266     }
267     return ret;
268 }
269
270 /**************************************************************************
271  *                              MMDRV_Alloc                     [internal]
272  */
273 LPWINE_MLD      MMDRV_Alloc(UINT size, UINT type, LPHANDLE hndl, DWORD* dwFlags,
274                             DWORD* dwCallback, DWORD* dwInstance, BOOL bFrom32)
275 {
276     LPWINE_MLD  mld;
277
278     mld = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
279     if (!mld)   return NULL;
280
281     /* find an empty slot in MM_MLDrvs table */
282     for (*hndl = 0; *hndl < MAX_MM_MLDRVS; (*hndl)++) {
283         if (!MM_MLDrvs[(UINT)*hndl]) break;
284     }
285     if (*hndl == MAX_MM_MLDRVS) {
286         /* the MM_MLDrvs table could be made growable in the future if needed */
287         ERR("Too many open drivers\n");
288         return NULL;
289     }
290     MM_MLDrvs[(UINT)*hndl] = mld;
291     *hndl = (HANDLE)((UINT)*hndl | 0x8000);
292
293     mld->type = type;
294     if ((UINT)*hndl < MMDRV_GetNum(type) || HIWORD(*hndl) != 0) {
295         /* FIXME: those conditions must be fulfilled so that:
296          * - we can distinguish between device IDs and handles
297          * - we can use handles as 16 or 32 bit entities
298          */
299         ERR("Shouldn't happen. Bad allocation scheme\n");
300     }
301
302     mld->bFrom32 = bFrom32;
303     mld->dwFlags = HIWORD(*dwFlags);
304     mld->dwCallback = *dwCallback;
305     mld->dwClientInstance = *dwInstance;
306
307     *dwFlags = LOWORD(*dwFlags) | CALLBACK_FUNCTION;
308     *dwCallback = (DWORD)llTypes[type].Callback;
309     *dwInstance = (DWORD)mld; /* FIXME: wouldn't some 16 bit drivers only use the loword ? */
310
311     return mld;
312 }
313
314 /**************************************************************************
315  *                              MMDRV_Free                      [internal]
316  */
317 void    MMDRV_Free(HANDLE hndl, LPWINE_MLD mld)
318 {
319     if ((UINT)hndl & 0x8000) {
320         unsigned idx = (UINT)hndl & ~0x8000;
321         if (idx < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
322             MM_MLDrvs[idx] = NULL;
323             HeapFree(GetProcessHeap(), 0, mld);
324             return;
325         }
326     }
327     ERR("Bad Handle %08x at %p (not freed)\n", hndl, mld);
328 }
329
330 /**************************************************************************
331  *                              MMDRV_Open                      [internal]
332  */
333 DWORD   MMDRV_Open(LPWINE_MLD mld, UINT wMsg, DWORD dwParam1, DWORD dwFlags)
334 {
335     DWORD               dwRet = MMSYSERR_BADDEVICEID;
336     DWORD               dwInstance;
337     WINE_LLTYPE*        llType = &llTypes[mld->type];
338
339     mld->dwDriverInstance = (DWORD)&dwInstance;
340
341     if (mld->uDeviceID == (UINT)-1 || mld->uDeviceID == (UINT16)-1) {
342         TRACE("MAPPER mode requested !\n");
343         /* check if mapper is supported by type */
344         if (llType->bSupportMapper) {
345             if (llType->nMapper == -1) {
346                 /* no driver for mapper has been loaded, try a dumb implementation */
347                 TRACE("No mapper loaded, doing it by hand\n");
348                 for (mld->uDeviceID = 0; mld->uDeviceID < llType->wMaxId; mld->uDeviceID++) {
349                     if ((dwRet = MMDRV_Open(mld, wMsg, dwParam1, dwFlags)) == MMSYSERR_NOERROR) {
350                         /* to share this function epilog */
351                         dwInstance = mld->dwDriverInstance;
352                         break;
353                     }
354                 }
355             } else {
356                 mld->uDeviceID = (UINT16)-1;
357                 mld->mmdIndex = llType->lpMlds[-1].mmdIndex;
358                 TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
359                 dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags, TRUE);
360             }
361         }
362     } else {
363         if (mld->uDeviceID < llType->wMaxId) {
364             mld->mmdIndex = llType->lpMlds[mld->uDeviceID].mmdIndex;
365             TRACE("Setting mmdIndex to %u\n", mld->mmdIndex);
366             dwRet = MMDRV_Message(mld, wMsg, dwParam1, dwFlags, TRUE);
367         }
368     }
369     if (dwRet == MMSYSERR_NOERROR)
370         mld->dwDriverInstance = dwInstance;
371     return dwRet;
372 }
373
374 /**************************************************************************
375  *                              MMDRV_Close                     [internal]
376  */
377 DWORD   MMDRV_Close(LPWINE_MLD mld, UINT wMsg)
378 {
379     return MMDRV_Message(mld, wMsg, 0L, 0L, TRUE);
380 }
381
382 /**************************************************************************
383  *                              MMDRV_GetByID                   [internal]
384  */
385 LPWINE_MLD      MMDRV_GetByID(UINT uDevID, UINT type)
386 {
387     if (uDevID < llTypes[type].wMaxId)
388         return &llTypes[type].lpMlds[uDevID];
389     if ((uDevID == (UINT16)-1 || uDevID == (UINT)-1) && llTypes[type].nMapper != -1)
390         return &llTypes[type].lpMlds[-1];
391     return NULL;
392 }
393
394 /**************************************************************************
395  *                              MMDRV_Get                       [internal]
396  */
397 LPWINE_MLD      MMDRV_Get(HANDLE hndl, UINT type, BOOL bCanBeID)
398 {
399     LPWINE_MLD  mld = NULL;
400
401     assert(type < MMDRV_MAX);
402
403     if ((UINT)hndl >= llTypes[type].wMaxId &&
404         hndl != (UINT16)-1 && hndl != (UINT)-1) {
405         if ((UINT)hndl & 0x8000) {
406             hndl = (HANDLE)((UINT)hndl & ~0x8000);
407             if (hndl < sizeof(MM_MLDrvs) / sizeof(MM_MLDrvs[0])) {
408                 mld = MM_MLDrvs[(UINT)hndl];
409                 if (!mld || !HeapValidate(GetProcessHeap(), 0, mld) || mld->type != type)
410                     mld = NULL;
411             }
412             hndl = (HANDLE)((UINT)hndl | 0x8000);
413         }
414     }
415     if (mld == NULL && bCanBeID) {
416         mld = MMDRV_GetByID((UINT)hndl, type);
417     }
418     return mld;
419 }
420
421 /**************************************************************************
422  *                              MMDRV_GetRelated                [internal]
423  */
424 LPWINE_MLD      MMDRV_GetRelated(HANDLE hndl, UINT srcType,
425                                  BOOL bSrcCanBeID, UINT dstType)
426 {
427     LPWINE_MLD          mld;
428
429     if ((mld = MMDRV_Get(hndl, srcType, bSrcCanBeID)) != NULL) {
430         WINE_MM_DRIVER_PART*    part = &MMDrvs[mld->mmdIndex].parts[dstType];
431         if (part->nIDMin < part->nIDMax)
432             return MMDRV_GetByID(part->nIDMin, dstType);
433     }
434     return NULL;
435 }
436
437 /**************************************************************************
438  *                              MMDRV_PhysicalFeatures          [internal]
439  */
440 UINT    MMDRV_PhysicalFeatures(LPWINE_MLD mld, UINT uMsg, DWORD dwParam1,
441                                DWORD dwParam2)
442 {
443     WINE_MM_DRIVER*     lpDrv = &MMDrvs[mld->mmdIndex];
444
445     TRACE("(%p, %04x, %08lx, %08lx)\n", mld, uMsg, dwParam1, dwParam2);
446
447     /* all those function calls are undocumented */
448     switch (uMsg) {
449     case DRV_QUERYDRVENTRY:
450         lstrcpynA((LPSTR)dwParam1, lpDrv->drvname, LOWORD(dwParam2));
451         break;
452     case DRV_QUERYDEVNODE:
453         *(LPDWORD)dwParam1 = 0L; /* should be DevNode */
454         break;
455     case DRV_QUERYNAME:
456         WARN("NIY QueryName\n");
457         break;
458     case DRV_QUERYDRIVERIDS:
459         WARN("NIY call VxD\n");
460         /* should call VxD MMDEVLDR with (DevNode, dwParam1 and dwParam2) as pmts
461          * dwParam1 is buffer and dwParam2 is sizeof buffer
462          * I don't know where the result is stored though
463          */
464         break;
465     case DRV_QUERYMAPPABLE:
466         return (lpDrv->bIsMapper) ? 2 : 0;
467
468     case DRV_QUERYDSOUNDIFACE: /* Wine-specific: Retrieve DirectSound interface */
469         return MMDRV_Message(mld, uMsg, dwParam1, dwParam2, TRUE);
470
471     default:
472         WARN("Unknown call %04x\n", uMsg);
473         return MMSYSERR_INVALPARAM;
474     }
475     return 0L;
476 }
477
478 /**************************************************************************
479  *                              MMDRV_InitPerType               [internal]
480  */
481 static  BOOL    MMDRV_InitPerType(LPWINE_MM_DRIVER lpDrv, UINT type, UINT wMsg)
482 {
483     WINE_MM_DRIVER_PART*        part = &lpDrv->parts[type];
484     DWORD                       ret;
485     UINT                        count = 0;
486     int                         i, k;
487
488     part->nIDMin = part->nIDMax = 0;
489
490     /* for DRVM_INIT and DRVM_ENABLE, dwParam2 should be PnP node */
491     /* the DRVM_ENABLE is only required when the PnP node is non zero */
492
493     if (lpDrv->bIs32 && part->u.fnMessage32) {
494         ret = part->u.fnMessage32(0, DRVM_INIT, 0L, 0L, 0L);
495         TRACE("DRVM_INIT => %08lx\n", ret);
496 #if 0
497         ret = part->u.fnMessage32(0, DRVM_ENABLE, 0L, 0L, 0L);
498         TRACE("DRVM_ENABLE => %08lx\n", ret);
499 #endif
500         count = part->u.fnMessage32(0, wMsg, 0L, 0L, 0L);
501     } else if (!lpDrv->bIs32 && part->u.fnMessage16) {
502         ret = MMDRV_CallTo16_word_wwlll((FARPROC16)part->u.fnMessage16,
503                                         0, DRVM_INIT, 0L, 0L, 0L);
504         TRACE("DRVM_INIT => %08lx\n", ret);
505 #if 0
506         ret = MMDRV_CallTo16_word_wwlll((FARPROC16)part->u.fnMessage16,
507                                         0, DRVM_ENABLE, 0L, 0L, 0L);
508         TRACE("DRVM_ENABLE => %08lx\n", ret);
509 #endif
510         count = MMDRV_CallTo16_word_wwlll((FARPROC16)part->u.fnMessage16,
511                                           0, wMsg, 0L, 0L, 0L);
512     } else {
513         return FALSE;
514     }
515
516     TRACE("Got %u dev for (%s:%s)\n", count, lpDrv->drvname, llTypes[type].typestr);
517
518     /* got some drivers */
519     if (lpDrv->bIsMapper) {
520         /* it seems native mappers return 0 devices :-( */
521         if (llTypes[type].nMapper != -1)
522             ERR("Two mappers for type %s (%d, %s)\n",
523                 llTypes[type].typestr, llTypes[type].nMapper, lpDrv->drvname);
524         if (count > 1)
525             ERR("Strange: mapper with %d > 1 devices\n", count);
526         llTypes[type].nMapper = MMDrvsHi;
527     } else {
528         if (count == 0)
529             return FALSE;
530         part->nIDMin = llTypes[type].wMaxId;
531         llTypes[type].wMaxId += count;
532         part->nIDMax = llTypes[type].wMaxId;
533     }
534     TRACE("Setting min=%d max=%d (ttop=%d) for (%s:%s)\n",
535           part->nIDMin, part->nIDMax, llTypes[type].wMaxId,
536           lpDrv->drvname, llTypes[type].typestr);
537     /* realloc translation table */
538     llTypes[type].lpMlds = (LPWINE_MLD)
539         HeapReAlloc(GetProcessHeap(), 0, (llTypes[type].lpMlds) ? llTypes[type].lpMlds - 1 : NULL,
540                     sizeof(WINE_MLD) * (llTypes[type].wMaxId + 1)) + 1;
541     /* re-build the translation table */
542     if (llTypes[type].nMapper != -1) {
543         TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, -1, MMDrvs[llTypes[type].nMapper].drvname);
544         llTypes[type].lpMlds[-1].uDeviceID = (UINT16)-1;
545         llTypes[type].lpMlds[-1].type = type;
546         llTypes[type].lpMlds[-1].mmdIndex = llTypes[type].nMapper;
547         llTypes[type].lpMlds[-1].dwDriverInstance = 0;
548     }
549     for (i = k = 0; i <= MMDrvsHi; i++) {
550         while (MMDrvs[i].parts[type].nIDMin <= k && k < MMDrvs[i].parts[type].nIDMax) {
551             TRACE("%s:Trans[%d] -> %s\n", llTypes[type].typestr, k, MMDrvs[i].drvname);
552             llTypes[type].lpMlds[k].uDeviceID = k;
553             llTypes[type].lpMlds[k].type = type;
554             llTypes[type].lpMlds[k].mmdIndex = i;
555             llTypes[type].lpMlds[k].dwDriverInstance = 0;
556             k++;
557         }
558     }
559     return TRUE;
560 }
561
562 /**************************************************************************
563  *                              MMDRV_Install                   [internal]
564  */
565 static  BOOL    MMDRV_Install(LPCSTR drvRegName, LPCSTR drvFileName, BOOL bIsMapper)
566 {
567     int                 i, count = 0;
568     char                buffer[128];
569     LPWINE_MM_DRIVER    lpDrv = &MMDrvs[MMDrvsHi];
570     LPWINE_DRIVER       d;
571
572     TRACE("('%s', '%s', mapper=%c);\n", drvRegName, drvFileName, bIsMapper ? 'Y' : 'N');
573
574     /* be sure that size of MMDrvs matches the max number of loadable drivers !!
575      * if not just increase size of MMDrvs */
576     assert(MMDrvsHi <= sizeof(MMDrvs)/sizeof(MMDrvs[0]));
577
578     for (i = 0; i < MMDrvsHi; i++) {
579         if (!strcmp(drvRegName, MMDrvs[i].drvname)) return FALSE;
580     }
581
582     memset(lpDrv, 0, sizeof(*lpDrv));
583
584     if (!(lpDrv->hDriver = OpenDriverA(drvFileName, 0, 0))) {
585         WARN("Couldn't open driver '%s'\n", drvFileName);
586         return FALSE;
587     }
588
589     d = DRIVER_FindFromHDrvr(lpDrv->hDriver);
590     lpDrv->bIs32 = (d->dwFlags & WINE_GDF_16BIT) ? FALSE : TRUE;
591
592     /* Then look for xxxMessage functions */
593 #define AA(_h,_w,_x,_y,_z)                                      \
594     func = (WINEMM_msgFunc##_y) _z ((_h), #_x);                 \
595     if (func != NULL)                                           \
596         { lpDrv->parts[_w].u.fnMessage##_y = func; count++;     \
597           TRACE("Got %d bit func '%s'\n", _y, #_x);         }
598
599     if (lpDrv->bIs32) {
600         WINEMM_msgFunc32        func;
601
602         if (d->d.d32.hModule) {
603 #define A(_x,_y)        AA(d->d.d32.hModule,_x,_y,32,GetProcAddress)
604             A(MMDRV_AUX,        auxMessage);
605             A(MMDRV_MIXER,      mixMessage);
606             A(MMDRV_MIDIIN,     midMessage);
607             A(MMDRV_MIDIOUT,    modMessage);
608             A(MMDRV_WAVEIN,     widMessage);
609             A(MMDRV_WAVEOUT,    wodMessage);
610 #undef A
611         }
612     } else {
613         WINEMM_msgFunc16        func;
614
615         /*
616          * DESCRIPTION 'wave,aux,mixer:Creative Labs Sound Blaster 16 Driver'
617          * The beginning of the module description indicates the driver supports
618          * waveform, auxiliary, and mixer devices. Use one of the following
619          * device-type names, followed by a colon (:) to indicate the type of
620          * device your driver supports. If the driver supports more than one
621          * type of device, separate each device-type name with a comma (,).
622          *
623          * wave for waveform audio devices
624          * wavemapper for wave mappers
625          * midi for MIDI audio devices
626          * midimapper for midi mappers
627          * aux for auxiliary audio devices
628          * mixer for mixer devices
629          */
630
631         if (d->d.d16.hDriver16) {
632             HMODULE16   hMod16 = GetDriverModuleHandle16(d->d.d16.hDriver16);
633
634 #define A(_x,_y)        AA(hMod16,_x,_y,16,GetProcAddress16)
635             A(MMDRV_AUX,        auxMessage);
636             A(MMDRV_MIXER,      mixMessage);
637             A(MMDRV_MIDIIN,     midMessage);
638             A(MMDRV_MIDIOUT,    modMessage);
639             A(MMDRV_WAVEIN,     widMessage);
640             A(MMDRV_WAVEOUT,    wodMessage);
641 #undef A
642         }
643     }
644 #undef AA
645
646     if (TRACE_ON(mmsys)) {
647         if ((lpDrv->bIs32) ? MMDRV_GetDescription32(drvFileName, buffer, sizeof(buffer)) :
648                              MMDRV_GetDescription16(drvFileName, buffer, sizeof(buffer)))
649             TRACE("%s => %s\n", drvFileName, buffer);
650         else
651             TRACE("%s => No description\n", drvFileName);
652     }
653
654     if (!count) {
655         CloseDriver(lpDrv->hDriver, 0, 0);
656         WARN("No message functions found\n");
657         return FALSE;
658     }
659
660     /* FIXME: being a mapper or not should be known by another way */
661     /* it's known for NE drvs (the description is of the form '*mapper: *'
662      * I don't have any clue for PE drvs
663      */
664     lpDrv->bIsMapper = bIsMapper;
665     lpDrv->drvname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(drvRegName) + 1), drvRegName);
666
667     /* Finish init and get the count of the devices */
668     MMDRV_InitPerType(lpDrv, MMDRV_AUX,         AUXDM_GETNUMDEVS);
669     MMDRV_InitPerType(lpDrv, MMDRV_MIXER,       MXDM_GETNUMDEVS);
670     MMDRV_InitPerType(lpDrv, MMDRV_MIDIIN,      MIDM_GETNUMDEVS);
671     MMDRV_InitPerType(lpDrv, MMDRV_MIDIOUT,     MODM_GETNUMDEVS);
672     MMDRV_InitPerType(lpDrv, MMDRV_WAVEIN,      WIDM_GETNUMDEVS);
673     MMDRV_InitPerType(lpDrv, MMDRV_WAVEOUT,     WODM_GETNUMDEVS);
674     /* FIXME: if all those func calls return FALSE,
675      * then the driver must be unloaded
676      */
677
678     MMDrvsHi++;
679
680     return TRUE;
681 }
682
683 /**************************************************************************
684  *                              MMDRV_InitFromRegistry          [internal]
685  */
686 static BOOL     MMDRV_InitFromRegistry(void)
687 {
688     HKEY        hKey;
689     char        buffer[256];
690     char*       p1;
691     char*       p2;
692     DWORD       type, size;
693     BOOL        ret = FALSE;
694
695     if (RegCreateKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\WinMM", &hKey)) {
696         TRACE("Cannot open WinMM config key\n");
697         return FALSE;
698     }
699
700     size = sizeof(buffer);
701     if (!RegQueryValueExA(hKey, "Drivers", 0, &type, (LPVOID)buffer, &size)) {
702         p1 = buffer;
703         while (p1) {
704             p2 = strchr(p1, ';');
705             if (p2) *p2++ = '\0';
706             ret |= MMDRV_Install(p1, p1, FALSE);
707             p1 = p2;
708         }
709     }
710
711     /* finish with mappers */
712     size = sizeof(buffer);
713     if (!RegQueryValueExA(hKey, "WaveMapper", 0, &type, (LPVOID)buffer, &size))
714         ret |= MMDRV_Install("wavemapper", buffer, TRUE);
715     size = sizeof(buffer);
716     if (!RegQueryValueExA(hKey, "MidiMapper", 0, &type, (LPVOID)buffer, &size))
717         ret |= MMDRV_Install("midimapper", buffer, TRUE);
718
719     RegCloseKey(hKey);
720
721     return ret;
722 }
723
724 /**************************************************************************
725  *                              MMDRV_InitHardcoded             [internal]
726  */
727 static BOOL     MMDRV_InitHardcoded(void)
728 {
729     /* first load hardware drivers */
730     MMDRV_Install("wineoss.drv",        "wineoss.drv",  FALSE);
731
732     /* finish with mappers */
733     MMDRV_Install("wavemapper",         "msacm.drv",    TRUE);
734     MMDRV_Install("midimapper",         "midimap.drv",  TRUE);
735
736     return TRUE;
737 }
738
739 /**************************************************************************
740  *                              MMDRV_Init                      [internal]
741  */
742 BOOL    MMDRV_Init(void)
743 {
744     /* FIXME: MMDRV_InitFromRegistry shall be MMDRV_Init in a near future */
745     return MMDRV_InitFromRegistry() || MMDRV_InitHardcoded();
746 }