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