Changed the CreateDC driver entry point to use an HDC instead of a DC
[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(LPWSTR pszDriverAlias, LPWSTR 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     LPWSTR pszBuffer;
287     DWORD dwBufferLength;
288     static WCHAR msacm32[] = {'m','s','a','c','m','3','2','.','d','l','l','\0'};
289     static WCHAR msacmW[] = {'M','S','A','C','M','.'};
290     static WCHAR drv32[] = {'d','r','i','v','e','r','s','3','2','\0'};
291     static WCHAR sys[] = {'s','y','s','t','e','m','.','i','n','i','\0'};
292
293     /* FIXME
294      *  What if the user edits system.ini while the program is running?
295      *  Does Windows handle that?
296      */
297     if (MSACM_pFirstACMDriverID)
298         return;
299
300     /* FIXME: Does not work! How do I determine the section length? */
301     dwBufferLength = 1024;
302 /* EPP  GetPrivateProfileSectionA("drivers32", NULL, 0, "system.ini"); */
303
304     pszBuffer = (LPWSTR) HeapAlloc(MSACM_hHeap, 0, dwBufferLength * sizeof(WCHAR));
305     if (GetPrivateProfileSectionW(drv32, pszBuffer, dwBufferLength, sys))
306     {
307         LPWSTR s = pszBuffer, s2;
308
309         while (*s)
310         {
311             CharUpperBuffW(s, 6);
312             if (memcmp(s, msacmW, 6 * sizeof(WCHAR)) == 0)
313             {
314                 s2 = s;
315                 while (*s2 != '\0' && *s2 != '=') s2++;
316                 if (*s2)
317                 {
318                     *s2 = '\0';
319                     MSACM_RegisterDriver(s, s2 + 1, 0);
320                     *s2 = '=';
321                 }
322             }
323             s += strlenW(s) + 1; /* Either next char or \0 */
324         }
325     }
326
327     HeapFree(MSACM_hHeap, 0, pszBuffer);
328
329     MSACM_RegisterDriver(msacm32, msacm32, 0);
330 }
331
332 /***********************************************************************
333  *           MSACM_UnregisterDriver()
334  */
335 PWINE_ACMDRIVERID MSACM_UnregisterDriver(PWINE_ACMDRIVERID p)
336 {
337     PWINE_ACMDRIVERID pNextACMDriverID;
338
339     while (p->pACMDriverList)
340         acmDriverClose((HACMDRIVER) p->pACMDriverList, 0);
341
342     if (p->pszDriverAlias)
343         HeapFree(MSACM_hHeap, 0, p->pszDriverAlias);
344     if (p->pszFileName)
345         HeapFree(MSACM_hHeap, 0, p->pszFileName);
346     HeapFree(MSACM_hHeap, 0, p->aFormatTag);
347
348     if (p == MSACM_pFirstACMDriverID)
349         MSACM_pFirstACMDriverID = p->pNextACMDriverID;
350     if (p == MSACM_pLastACMDriverID)
351         MSACM_pLastACMDriverID = p->pPrevACMDriverID;
352
353     if (p->pPrevACMDriverID)
354         p->pPrevACMDriverID->pNextACMDriverID = p->pNextACMDriverID;
355     if (p->pNextACMDriverID)
356         p->pNextACMDriverID->pPrevACMDriverID = p->pPrevACMDriverID;
357
358     pNextACMDriverID = p->pNextACMDriverID;
359
360     HeapFree(MSACM_hHeap, 0, p);
361
362     return pNextACMDriverID;
363 }
364
365 /***********************************************************************
366  *           MSACM_UnregisterAllDrivers()
367  */
368 void MSACM_UnregisterAllDrivers(void)
369 {
370     PWINE_ACMDRIVERID p = MSACM_pFirstACMDriverID;
371
372     while (p) {
373         MSACM_WriteCache(p);
374         p = MSACM_UnregisterDriver(p);
375     }
376 }
377
378 /***********************************************************************
379  *           MSACM_GetObj()
380  */
381 PWINE_ACMOBJ MSACM_GetObj(HACMOBJ hObj, DWORD type)
382 {
383     PWINE_ACMOBJ        pao = (PWINE_ACMOBJ)hObj;
384
385     if (pao == NULL || IsBadReadPtr(pao, sizeof(WINE_ACMOBJ)) ||
386         ((type != WINE_ACMOBJ_DONTCARE) && (type != pao->dwType)))
387         return NULL;
388     return pao;
389 }
390
391 /***********************************************************************
392  *           MSACM_GetDriverID()
393  */
394 PWINE_ACMDRIVERID MSACM_GetDriverID(HACMDRIVERID hDriverID)
395 {
396     return (PWINE_ACMDRIVERID)MSACM_GetObj((HACMOBJ)hDriverID, WINE_ACMOBJ_DRIVERID);
397 }
398
399 /***********************************************************************
400  *           MSACM_GetDriver()
401  */
402 PWINE_ACMDRIVER MSACM_GetDriver(HACMDRIVER hDriver)
403 {
404     return (PWINE_ACMDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_DRIVER);
405 }
406
407 /***********************************************************************
408  *           MSACM_Message()
409  */
410 MMRESULT MSACM_Message(HACMDRIVER had, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
411 {
412     PWINE_ACMDRIVER     pad = MSACM_GetDriver(had);
413
414     return pad ? SendDriverMessage(pad->hDrvr, uMsg, lParam1, lParam2) : MMSYSERR_INVALHANDLE;
415 }