Look up driver info in the registry as well as in system.ini.
[wine] / dlls / msacm / internal.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  *      MSACM32 library
5  *
6  *      Copyright 1998  Patrik Stridvall
7  *                1999  Eric Pouech
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include <stdarg.h>
25 #include <string.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wingdi.h"
30 #include "winuser.h"
31 #include "winerror.h"
32 #include "winreg.h"
33 #include "mmsystem.h"
34 #include "mmreg.h"
35 #include "msacm.h"
36 #include "msacmdrv.h"
37 #include "wineacm.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
42
43 /**********************************************************************/
44
45 HANDLE MSACM_hHeap = NULL;
46 PWINE_ACMDRIVERID MSACM_pFirstACMDriverID = NULL;
47 PWINE_ACMDRIVERID MSACM_pLastACMDriverID = NULL;
48
49 #if 0
50 /***********************************************************************
51  *           MSACM_DumpCache
52  */
53 static  void MSACM_DumpCache(PWINE_ACMDRIVERID padid)
54 {
55     unsigned    i;
56
57     TRACE("cFilterTags=%lu cFormatTags=%lu fdwSupport=%08lx\n",
58           padid->cFilterTags, padid->cFormatTags, padid->fdwSupport);
59     for (i = 0; i < padid->cache->cFormatTags; i++) {
60         TRACE("\tdwFormatTag=%lu cbwfx=%lu\n",
61               padid->aFormatTag[i].dwFormatTag, padid->aFormatTag[i].cbwfx);
62     }
63 }
64 #endif
65
66 /***********************************************************************
67  *           MSACM_FindFormatTagInCache                 [internal]
68  *
69  *      Returns TRUE is the format tag fmtTag is present in the cache.
70  *      If so, idx is set to its index.
71  */
72 BOOL MSACM_FindFormatTagInCache(WINE_ACMDRIVERID* padid, DWORD fmtTag, LPDWORD idx)
73 {
74     unsigned    i;
75
76     for (i = 0; i < padid->cFormatTags; i++) {
77         if (padid->aFormatTag[i].dwFormatTag == fmtTag) {
78             if (idx) *idx = i;
79             return TRUE;
80         }
81     }
82     return FALSE;
83 }
84
85 /***********************************************************************
86  *           MSACM_FillCache
87  */
88 static BOOL MSACM_FillCache(PWINE_ACMDRIVERID padid)
89 {
90     HACMDRIVER                  had = 0;
91     int                         ntag;
92     ACMDRIVERDETAILSW           add;
93     ACMFORMATTAGDETAILSW        aftd;
94
95     if (acmDriverOpen(&had, (HACMDRIVERID)padid, 0) != 0)
96         return FALSE;
97
98     padid->aFormatTag = NULL;
99     add.cbStruct = sizeof(add);
100     if (MSACM_Message(had, ACMDM_DRIVER_DETAILS, (LPARAM)&add,  0))
101         goto errCleanUp;
102
103     if (add.cFormatTags > 0) {
104         padid->aFormatTag = HeapAlloc(MSACM_hHeap, HEAP_ZERO_MEMORY,
105                                       add.cFormatTags * sizeof(padid->aFormatTag[0]));
106         if (!padid->aFormatTag) goto errCleanUp;
107     }
108
109     padid->cFormatTags = add.cFormatTags;
110     padid->cFilterTags = add.cFilterTags;
111     padid->fdwSupport  = add.fdwSupport;
112
113     aftd.cbStruct = sizeof(aftd);
114
115     for (ntag = 0; ntag < add.cFormatTags; ntag++) {
116         aftd.dwFormatTagIndex = ntag;
117         if (MSACM_Message(had, ACMDM_FORMATTAG_DETAILS, (LPARAM)&aftd, ACM_FORMATTAGDETAILSF_INDEX)) {
118             TRACE("IIOs (%s)\n", debugstr_w(padid->pszDriverAlias));
119             goto errCleanUp;
120         }
121         padid->aFormatTag[ntag].dwFormatTag = aftd.dwFormatTag;
122         padid->aFormatTag[ntag].cbwfx = aftd.cbFormatSize;
123     }
124
125     acmDriverClose(had, 0);
126
127     return TRUE;
128
129 errCleanUp:
130     if (had) acmDriverClose(had, 0);
131     HeapFree(MSACM_hHeap, 0, padid->aFormatTag);
132     padid->aFormatTag = NULL;
133     return FALSE;
134 }
135
136 /***********************************************************************
137  *           MSACM_GetRegistryKey
138  */
139 static  LPWSTR  MSACM_GetRegistryKey(const WINE_ACMDRIVERID* padid)
140 {
141     static const WCHAR  baseKey[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
142                                      'A','u','d','i','o','C','o','m','p','r','e','s','s','i','o','n','M','a','n','a','g','e','r','\\',
143                                      'D','r','i','v','e','r','C','a','c','h','e','\\','\0'};
144     LPWSTR      ret;
145     int         len;
146
147     if (!padid->pszDriverAlias) {
148         ERR("No alias needed for registry entry\n");
149         return NULL;
150     }
151     len = strlenW(baseKey);
152     ret = HeapAlloc(MSACM_hHeap, 0, (len + strlenW(padid->pszDriverAlias) + 1) * sizeof(WCHAR));
153     if (!ret) return NULL;
154
155     strcpyW(ret, baseKey);
156     strcpyW(ret + len, padid->pszDriverAlias);
157     CharLowerW(ret + len);
158     return ret;
159 }
160
161 /***********************************************************************
162  *           MSACM_ReadCache
163  */
164 static BOOL MSACM_ReadCache(PWINE_ACMDRIVERID padid)
165 {
166     LPWSTR      key = MSACM_GetRegistryKey(padid);
167     HKEY        hKey;
168     DWORD       type, size;
169
170     if (!key) return FALSE;
171
172     padid->aFormatTag = NULL;
173
174     if (RegCreateKeyW(HKEY_LOCAL_MACHINE, key, &hKey))
175         goto errCleanUp;
176
177     size = sizeof(padid->cFormatTags);
178     if (RegQueryValueExA(hKey, "cFormatTags", 0, &type, (void*)&padid->cFormatTags, &size))
179         goto errCleanUp;
180     size = sizeof(padid->cFilterTags);
181     if (RegQueryValueExA(hKey, "cFilterTags", 0, &type, (void*)&padid->cFilterTags, &size))
182         goto errCleanUp;
183     size = sizeof(padid->fdwSupport);
184     if (RegQueryValueExA(hKey, "fdwSupport", 0, &type, (void*)&padid->fdwSupport, &size))
185         goto errCleanUp;
186
187     if (padid->cFormatTags > 0) {
188         size = padid->cFormatTags * sizeof(padid->aFormatTag[0]);
189         padid->aFormatTag = HeapAlloc(MSACM_hHeap, HEAP_ZERO_MEMORY, size);
190         if (!padid->aFormatTag) goto errCleanUp;
191         if (RegQueryValueExA(hKey, "aFormatTagCache", 0, &type, (void*)padid->aFormatTag, &size))
192             goto errCleanUp;
193     }
194     HeapFree(MSACM_hHeap, 0, key);
195     return TRUE;
196
197  errCleanUp:
198     HeapFree(MSACM_hHeap, 0, key);
199     HeapFree(MSACM_hHeap, 0, padid->aFormatTag);
200     padid->aFormatTag = NULL;
201     RegCloseKey(hKey);
202     return FALSE;
203 }
204
205 /***********************************************************************
206  *           MSACM_WriteCache
207  */
208 static  BOOL MSACM_WriteCache(PWINE_ACMDRIVERID padid)
209 {
210     LPWSTR      key = MSACM_GetRegistryKey(padid);
211     HKEY        hKey;
212
213     if (!key) return FALSE;
214
215     if (RegCreateKeyW(HKEY_LOCAL_MACHINE, key, &hKey))
216         goto errCleanUp;
217
218     if (RegSetValueExA(hKey, "cFormatTags", 0, REG_DWORD, (void*)&padid->cFormatTags, sizeof(DWORD)))
219         goto errCleanUp;
220     if (RegSetValueExA(hKey, "cFilterTags", 0, REG_DWORD, (void*)&padid->cFilterTags, sizeof(DWORD)))
221         goto errCleanUp;
222     if (RegSetValueExA(hKey, "fdwSupport", 0, REG_DWORD, (void*)&padid->fdwSupport, sizeof(DWORD)))
223         goto errCleanUp;
224     if (RegSetValueExA(hKey, "aFormatTagCache", 0, REG_BINARY,
225                        (void*)padid->aFormatTag,
226                        padid->cFormatTags * sizeof(padid->aFormatTag[0])))
227         goto errCleanUp;
228     HeapFree(MSACM_hHeap, 0, key);
229     return TRUE;
230
231  errCleanUp:
232     HeapFree(MSACM_hHeap, 0, key);
233     return FALSE;
234 }
235
236 /***********************************************************************
237  *           MSACM_RegisterDriver()
238  */
239 PWINE_ACMDRIVERID MSACM_RegisterDriver(LPCWSTR pszDriverAlias, LPCWSTR pszFileName,
240                                        HINSTANCE hinstModule)
241 {
242     PWINE_ACMDRIVERID   padid;
243
244     TRACE("(%s, %s, %p)\n", 
245           debugstr_w(pszDriverAlias), debugstr_w(pszFileName), hinstModule);
246
247     padid = (PWINE_ACMDRIVERID) HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMDRIVERID));
248     padid->obj.dwType = WINE_ACMOBJ_DRIVERID;
249     padid->obj.pACMDriverID = padid;
250     padid->pszDriverAlias = NULL;
251     if (pszDriverAlias)
252     {
253         padid->pszDriverAlias = HeapAlloc( MSACM_hHeap, 0, (strlenW(pszDriverAlias)+1) * sizeof(WCHAR) );
254         strcpyW( padid->pszDriverAlias, pszDriverAlias );
255     }
256     padid->pszFileName = NULL;
257     if (pszFileName)
258     {
259         padid->pszFileName = HeapAlloc( MSACM_hHeap, 0, (strlenW(pszFileName)+1) * sizeof(WCHAR) );
260         strcpyW( padid->pszFileName, pszFileName );
261     }
262     padid->hInstModule = hinstModule;
263
264     padid->pACMDriverList = NULL;
265     padid->pNextACMDriverID = NULL;
266     padid->pPrevACMDriverID = MSACM_pLastACMDriverID;
267     if (MSACM_pLastACMDriverID)
268         MSACM_pLastACMDriverID->pNextACMDriverID = padid;
269     MSACM_pLastACMDriverID = padid;
270     if (!MSACM_pFirstACMDriverID)
271         MSACM_pFirstACMDriverID = padid;
272     /* disable the driver if we cannot load the cache */
273     if (!MSACM_ReadCache(padid) && !MSACM_FillCache(padid)) {
274         WARN("Couldn't load cache for ACM driver (%s)\n", debugstr_w(pszFileName));
275         MSACM_UnregisterDriver(padid);
276         return NULL;
277     }
278     return padid;
279 }
280
281 /***********************************************************************
282  *           MSACM_RegisterAllDrivers()
283  */
284 void MSACM_RegisterAllDrivers(void)
285 {
286     static const WCHAR msacm32[] = {'m','s','a','c','m','3','2','.','d','l','l','\0'};
287     static const WCHAR msacmW[] = {'M','S','A','C','M','.'};
288     static const WCHAR drv32[] = {'d','r','i','v','e','r','s','3','2','\0'};
289     static const WCHAR sys[] = {'s','y','s','t','e','m','.','i','n','i','\0'};
290     static const WCHAR drvkey[] = {'S','o','f','t','w','a','r','e','\\',
291                                    'M','i','c','r','o','s','o','f','t','\\',
292                                    'W','i','n','d','o','w','s',' ','N','T','\\',
293                                    'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
294                                    'D','r','i','v','e','r','s','3','2','\0'};
295     DWORD i, cnt = 0, bufLen, lRet;
296     WCHAR buf[2048], *name, *s;
297     FILETIME lastWrite;
298     HKEY hKey;
299
300     /* FIXME: What if the user edits system.ini while the program is running?
301      * Does Windows handle that?  */
302     if (MSACM_pFirstACMDriverID) return;
303
304     lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, drvkey, 0, KEY_QUERY_VALUE, &hKey);
305     if (lRet == ERROR_SUCCESS) {
306         RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
307         for (i = 0; i < cnt; i++) {
308             bufLen = sizeof(buf) / sizeof(buf[0]);
309             lRet = RegEnumKeyExW(hKey, i, buf, &bufLen, 0, 0, 0, &lastWrite);
310             if (lRet != ERROR_SUCCESS) continue;
311             if (strncmpiW(buf, msacmW, sizeof(msacmW)/sizeof(msacmW[0]))) continue;
312             if (!(name = strchrW(buf, '='))) continue;
313             *name = 0;
314             MSACM_RegisterDriver(buf, name + 1, 0);
315         }
316         RegCloseKey( hKey );
317     }
318
319     if (GetPrivateProfileSectionW(drv32, buf, sizeof(buf)/sizeof(buf[0]), sys))
320     {
321         for(s = buf; *s;  s += strlenW(s) + 1)
322         {
323             if (strncmpiW(s, msacmW, sizeof(msacmW)/sizeof(msacmW[0]))) continue;
324             if (!(name = strchrW(s, '='))) continue;
325             *name = 0;
326             MSACM_RegisterDriver(s, name + 1, 0);
327             *name = '=';
328         }
329     }
330
331     MSACM_RegisterDriver(msacm32, msacm32, 0);
332 }
333
334 /***********************************************************************
335  *           MSACM_UnregisterDriver()
336  */
337 PWINE_ACMDRIVERID MSACM_UnregisterDriver(PWINE_ACMDRIVERID p)
338 {
339     PWINE_ACMDRIVERID pNextACMDriverID;
340
341     while (p->pACMDriverList)
342         acmDriverClose((HACMDRIVER) p->pACMDriverList, 0);
343
344     if (p->pszDriverAlias)
345         HeapFree(MSACM_hHeap, 0, p->pszDriverAlias);
346     if (p->pszFileName)
347         HeapFree(MSACM_hHeap, 0, p->pszFileName);
348     HeapFree(MSACM_hHeap, 0, p->aFormatTag);
349
350     if (p == MSACM_pFirstACMDriverID)
351         MSACM_pFirstACMDriverID = p->pNextACMDriverID;
352     if (p == MSACM_pLastACMDriverID)
353         MSACM_pLastACMDriverID = p->pPrevACMDriverID;
354
355     if (p->pPrevACMDriverID)
356         p->pPrevACMDriverID->pNextACMDriverID = p->pNextACMDriverID;
357     if (p->pNextACMDriverID)
358         p->pNextACMDriverID->pPrevACMDriverID = p->pPrevACMDriverID;
359
360     pNextACMDriverID = p->pNextACMDriverID;
361
362     HeapFree(MSACM_hHeap, 0, p);
363
364     return pNextACMDriverID;
365 }
366
367 /***********************************************************************
368  *           MSACM_UnregisterAllDrivers()
369  */
370 void MSACM_UnregisterAllDrivers(void)
371 {
372     PWINE_ACMDRIVERID p = MSACM_pFirstACMDriverID;
373
374     while (p) {
375         MSACM_WriteCache(p);
376         p = MSACM_UnregisterDriver(p);
377     }
378 }
379
380 /***********************************************************************
381  *           MSACM_GetObj()
382  */
383 PWINE_ACMOBJ MSACM_GetObj(HACMOBJ hObj, DWORD type)
384 {
385     PWINE_ACMOBJ        pao = (PWINE_ACMOBJ)hObj;
386
387     if (pao == NULL || IsBadReadPtr(pao, sizeof(WINE_ACMOBJ)) ||
388         ((type != WINE_ACMOBJ_DONTCARE) && (type != pao->dwType)))
389         return NULL;
390     return pao;
391 }
392
393 /***********************************************************************
394  *           MSACM_GetDriverID()
395  */
396 PWINE_ACMDRIVERID MSACM_GetDriverID(HACMDRIVERID hDriverID)
397 {
398     return (PWINE_ACMDRIVERID)MSACM_GetObj((HACMOBJ)hDriverID, WINE_ACMOBJ_DRIVERID);
399 }
400
401 /***********************************************************************
402  *           MSACM_GetDriver()
403  */
404 PWINE_ACMDRIVER MSACM_GetDriver(HACMDRIVER hDriver)
405 {
406     return (PWINE_ACMDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_DRIVER);
407 }
408
409 /***********************************************************************
410  *           MSACM_Message()
411  */
412 MMRESULT MSACM_Message(HACMDRIVER had, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
413 {
414     PWINE_ACMDRIVER     pad = MSACM_GetDriver(had);
415
416     return pad ? SendDriverMessage(pad->hDrvr, uMsg, lParam1, lParam2) : MMSYSERR_INVALHANDLE;
417 }