Status update.
[wine.git] / dlls / msacm / internal.c
blob0a06473880b6e05c242267c3339a986ce83ad42f
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * MSACM32 library
6 * Copyright 1998 Patrik Stridvall
7 * 1999 Eric Pouech
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.
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.
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
24 #include <stdarg.h>
25 #include <string.h>
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"
41 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
43 /**********************************************************************/
45 HANDLE MSACM_hHeap = NULL;
46 PWINE_ACMDRIVERID MSACM_pFirstACMDriverID = NULL;
47 PWINE_ACMDRIVERID MSACM_pLastACMDriverID = NULL;
49 #if 0
50 /***********************************************************************
51 * MSACM_DumpCache
53 static void MSACM_DumpCache(PWINE_ACMDRIVERID padid)
55 unsigned i;
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);
64 #endif
66 /***********************************************************************
67 * MSACM_FindFormatTagInCache [internal]
69 * Returns TRUE is the format tag fmtTag is present in the cache.
70 * If so, idx is set to its index.
72 BOOL MSACM_FindFormatTagInCache(WINE_ACMDRIVERID* padid, DWORD fmtTag, LPDWORD idx)
74 unsigned i;
76 for (i = 0; i < padid->cFormatTags; i++) {
77 if (padid->aFormatTag[i].dwFormatTag == fmtTag) {
78 if (idx) *idx = i;
79 return TRUE;
82 return FALSE;
85 /***********************************************************************
86 * MSACM_FillCache
88 static BOOL MSACM_FillCache(PWINE_ACMDRIVERID padid)
90 HACMDRIVER had = 0;
91 unsigned int ntag;
92 ACMDRIVERDETAILSW add;
93 ACMFORMATTAGDETAILSW aftd;
95 if (acmDriverOpen(&had, (HACMDRIVERID)padid, 0) != 0)
96 return FALSE;
98 padid->aFormatTag = NULL;
99 add.cbStruct = sizeof(add);
100 if (MSACM_Message(had, ACMDM_DRIVER_DETAILS, (LPARAM)&add, 0))
101 goto errCleanUp;
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;
109 padid->cFormatTags = add.cFormatTags;
110 padid->cFilterTags = add.cFilterTags;
111 padid->fdwSupport = add.fdwSupport;
113 aftd.cbStruct = sizeof(aftd);
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;
121 padid->aFormatTag[ntag].dwFormatTag = aftd.dwFormatTag;
122 padid->aFormatTag[ntag].cbwfx = aftd.cbFormatSize;
125 acmDriverClose(had, 0);
127 return TRUE;
129 errCleanUp:
130 if (had) acmDriverClose(had, 0);
131 HeapFree(MSACM_hHeap, 0, padid->aFormatTag);
132 padid->aFormatTag = NULL;
133 return FALSE;
136 /***********************************************************************
137 * MSACM_GetRegistryKey
139 static LPWSTR MSACM_GetRegistryKey(const WINE_ACMDRIVERID* padid)
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;
147 if (!padid->pszDriverAlias) {
148 ERR("No alias needed for registry entry\n");
149 return NULL;
151 len = strlenW(baseKey);
152 ret = HeapAlloc(MSACM_hHeap, 0, (len + strlenW(padid->pszDriverAlias) + 1) * sizeof(WCHAR));
153 if (!ret) return NULL;
155 strcpyW(ret, baseKey);
156 strcpyW(ret + len, padid->pszDriverAlias);
157 CharLowerW(ret + len);
158 return ret;
161 /***********************************************************************
162 * MSACM_ReadCache
164 static BOOL MSACM_ReadCache(PWINE_ACMDRIVERID padid)
166 LPWSTR key = MSACM_GetRegistryKey(padid);
167 HKEY hKey;
168 DWORD type, size;
170 if (!key) return FALSE;
172 padid->aFormatTag = NULL;
174 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, key, &hKey))
175 goto errCleanUp;
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;
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;
194 HeapFree(MSACM_hHeap, 0, key);
195 return TRUE;
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;
205 /***********************************************************************
206 * MSACM_WriteCache
208 static BOOL MSACM_WriteCache(PWINE_ACMDRIVERID padid)
210 LPWSTR key = MSACM_GetRegistryKey(padid);
211 HKEY hKey;
213 if (!key) return FALSE;
215 if (RegCreateKeyW(HKEY_LOCAL_MACHINE, key, &hKey))
216 goto errCleanUp;
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;
231 errCleanUp:
232 HeapFree(MSACM_hHeap, 0, key);
233 return FALSE;
236 /***********************************************************************
237 * MSACM_RegisterDriver()
239 PWINE_ACMDRIVERID MSACM_RegisterDriver(LPCWSTR pszDriverAlias, LPCWSTR pszFileName,
240 HINSTANCE hinstModule)
242 PWINE_ACMDRIVERID padid;
244 TRACE("(%s, %s, %p)\n",
245 debugstr_w(pszDriverAlias), debugstr_w(pszFileName), hinstModule);
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)
253 padid->pszDriverAlias = HeapAlloc( MSACM_hHeap, 0, (strlenW(pszDriverAlias)+1) * sizeof(WCHAR) );
254 strcpyW( padid->pszDriverAlias, pszDriverAlias );
256 padid->pszFileName = NULL;
257 if (pszFileName)
259 padid->pszFileName = HeapAlloc( MSACM_hHeap, 0, (strlenW(pszFileName)+1) * sizeof(WCHAR) );
260 strcpyW( padid->pszFileName, pszFileName );
262 padid->hInstModule = hinstModule;
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;
278 return padid;
281 /***********************************************************************
282 * MSACM_RegisterAllDrivers()
284 void MSACM_RegisterAllDrivers(void)
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;
300 /* FIXME: What if the user edits system.ini while the program is running?
301 * Does Windows handle that? */
302 if (MSACM_pFirstACMDriverID) return;
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);
316 RegCloseKey( hKey );
319 if (GetPrivateProfileSectionW(drv32, buf, sizeof(buf)/sizeof(buf[0]), sys))
321 for(s = buf; *s; s += strlenW(s) + 1)
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 = '=';
331 MSACM_RegisterDriver(msacm32, msacm32, 0);
334 /***********************************************************************
335 * MSACM_UnregisterDriver()
337 PWINE_ACMDRIVERID MSACM_UnregisterDriver(PWINE_ACMDRIVERID p)
339 PWINE_ACMDRIVERID pNextACMDriverID;
341 while (p->pACMDriverList)
342 acmDriverClose((HACMDRIVER) p->pACMDriverList, 0);
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);
350 if (p == MSACM_pFirstACMDriverID)
351 MSACM_pFirstACMDriverID = p->pNextACMDriverID;
352 if (p == MSACM_pLastACMDriverID)
353 MSACM_pLastACMDriverID = p->pPrevACMDriverID;
355 if (p->pPrevACMDriverID)
356 p->pPrevACMDriverID->pNextACMDriverID = p->pNextACMDriverID;
357 if (p->pNextACMDriverID)
358 p->pNextACMDriverID->pPrevACMDriverID = p->pPrevACMDriverID;
360 pNextACMDriverID = p->pNextACMDriverID;
362 HeapFree(MSACM_hHeap, 0, p);
364 return pNextACMDriverID;
367 /***********************************************************************
368 * MSACM_UnregisterAllDrivers()
370 void MSACM_UnregisterAllDrivers(void)
372 PWINE_ACMDRIVERID p = MSACM_pFirstACMDriverID;
374 while (p) {
375 MSACM_WriteCache(p);
376 p = MSACM_UnregisterDriver(p);
380 /***********************************************************************
381 * MSACM_GetObj()
383 PWINE_ACMOBJ MSACM_GetObj(HACMOBJ hObj, DWORD type)
385 PWINE_ACMOBJ pao = (PWINE_ACMOBJ)hObj;
387 if (pao == NULL || IsBadReadPtr(pao, sizeof(WINE_ACMOBJ)) ||
388 ((type != WINE_ACMOBJ_DONTCARE) && (type != pao->dwType)))
389 return NULL;
390 return pao;
393 /***********************************************************************
394 * MSACM_GetDriverID()
396 PWINE_ACMDRIVERID MSACM_GetDriverID(HACMDRIVERID hDriverID)
398 return (PWINE_ACMDRIVERID)MSACM_GetObj((HACMOBJ)hDriverID, WINE_ACMOBJ_DRIVERID);
401 /***********************************************************************
402 * MSACM_GetDriver()
404 PWINE_ACMDRIVER MSACM_GetDriver(HACMDRIVER hDriver)
406 return (PWINE_ACMDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_DRIVER);
409 /***********************************************************************
410 * MSACM_Message()
412 MMRESULT MSACM_Message(HACMDRIVER had, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
414 PWINE_ACMDRIVER pad = MSACM_GetDriver(had);
416 return pad ? SendDriverMessage(pad->hDrvr, uMsg, lParam1, lParam2) : MMSYSERR_INVALHANDLE;