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