1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
6 * Copyright 1998 Patrik Stridvall
22 #include "debugtools.h"
24 DEFAULT_DEBUG_CHANNEL(msacm);
26 /**********************************************************************/
28 HANDLE MSACM_hHeap = (HANDLE) NULL;
29 PWINE_ACMDRIVERID MSACM_pFirstACMDriverID = NULL;
30 PWINE_ACMDRIVERID MSACM_pLastACMDriverID = NULL;
33 /***********************************************************************
36 static void MSACM_DumpCache(PWINE_ACMDRIVERID padid)
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);
49 /***********************************************************************
50 * MSACM_FindFormatTagInCache [internal]
52 * Returns TRUE is the format tag fmtTag is present in the cache.
53 * If so, idx is set to its index.
55 BOOL MSACM_FindFormatTagInCache(WINE_ACMDRIVERID* padid, DWORD fmtTag, LPDWORD idx)
59 for (i = 0; i < padid->cFormatTags; i++) {
60 if (padid->aFormatTag[i].dwFormatTag == fmtTag) {
68 /***********************************************************************
71 static BOOL MSACM_FillCache(PWINE_ACMDRIVERID padid)
75 ACMDRIVERDETAILSW add;
76 ACMFORMATDETAILSW aftd;
79 if (acmDriverOpen(&had, (HACMDRIVERID)padid, 0) != 0)
82 padid->aFormatTag = NULL;
83 add.cbStruct = sizeof(add);
84 if (MSACM_Message(had, ACMDM_DRIVER_DETAILS, (LPARAM)&add, 0))
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;
93 padid->cFormatTags = add.cFormatTags;
94 padid->cFilterTags = add.cFilterTags;
95 padid->fdwSupport = add.fdwSupport;
97 aftd.cbStruct = sizeof(aftd);
98 /* don't care about retrieving full struct... so a bare WAVEFORMATEX should do */
100 aftd.cbwfx = sizeof(wfx);
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);
108 padid->aFormatTag[ntag].dwFormatTag = aftd.dwFormatTag;
109 padid->aFormatTag[ntag].cbwfx = aftd.cbwfx;
112 acmDriverClose(had, 0);
117 if (had) acmDriverClose(had, 0);
118 HeapFree(MSACM_hHeap, 0, padid->aFormatTag);
119 padid->aFormatTag = NULL;
123 /***********************************************************************
124 * MSACM_GetRegistryKey
126 static LPSTR MSACM_GetRegistryKey(const WINE_ACMDRIVERID* padid)
128 static const char* baseKey = "Software\\Microsoft\\AudioCompressionManager\\DriverCache\\";
132 if (!padid->pszDriverAlias) {
133 ERR("No alias needed for registry entry\n");
136 len = strlen(baseKey);
137 ret = HeapAlloc(MSACM_hHeap, 0, len + strlen(padid->pszDriverAlias) + 1);
138 if (!ret) return NULL;
140 strcpy(ret, baseKey);
141 strcpy(ret + len, padid->pszDriverAlias);
142 CharLowerA(ret + len);
146 /***********************************************************************
149 static BOOL MSACM_ReadCache(PWINE_ACMDRIVERID padid)
151 LPSTR key = MSACM_GetRegistryKey(padid);
155 if (!key) return FALSE;
157 padid->aFormatTag = NULL;
159 if (RegCreateKeyA(HKEY_LOCAL_MACHINE, key, &hKey))
162 size = sizeof(padid->cFormatTags);
163 if (RegQueryValueExA(hKey, "cFormatTags", 0, &type, (void*)&padid->cFormatTags, &size))
165 size = sizeof(padid->cFilterTags);
166 if (RegQueryValueExA(hKey, "cFilterTags", 0, &type, (void*)&padid->cFilterTags, &size))
168 size = sizeof(padid->fdwSupport);
169 if (RegQueryValueExA(hKey, "fdwSupport", 0, &type, (void*)&padid->fdwSupport, &size))
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))
179 HeapFree(MSACM_hHeap, 0, key);
183 HeapFree(MSACM_hHeap, 0, key);
184 HeapFree(MSACM_hHeap, 0, padid->aFormatTag);
185 padid->aFormatTag = NULL;
190 /***********************************************************************
193 static BOOL MSACM_WriteCache(PWINE_ACMDRIVERID padid)
195 LPSTR key = MSACM_GetRegistryKey(padid);
198 if (!key) return FALSE;
200 if (RegCreateKeyA(HKEY_LOCAL_MACHINE, key, &hKey))
203 if (RegSetValueExA(hKey, "cFormatTags", 0, REG_DWORD, (void*)&padid->cFormatTags, sizeof(DWORD)))
205 if (RegSetValueExA(hKey, "cFilterTags", 0, REG_DWORD, (void*)&padid->cFilterTags, sizeof(DWORD)))
207 if (RegSetValueExA(hKey, "fdwSupport", 0, REG_DWORD, (void*)&padid->fdwSupport, sizeof(DWORD)))
209 if (RegSetValueExA(hKey, "aFormatTagCache", 0, REG_BINARY,
210 (void*)padid->aFormatTag,
211 padid->cFormatTags * sizeof(padid->aFormatTag[0])))
213 HeapFree(MSACM_hHeap, 0, key);
217 HeapFree(MSACM_hHeap, 0, key);
221 /***********************************************************************
222 * MSACM_RegisterDriver()
224 PWINE_ACMDRIVERID MSACM_RegisterDriver(LPSTR pszDriverAlias, LPSTR pszFileName,
225 HINSTANCE hinstModule)
227 PWINE_ACMDRIVERID padid;
229 TRACE("('%s', '%s', 0x%08x)\n", pszDriverAlias, pszFileName, hinstModule);
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;
237 padid->pszDriverAlias = HeapAlloc( MSACM_hHeap, 0, strlen(pszDriverAlias)+1 );
238 strcpy( padid->pszDriverAlias, pszDriverAlias );
240 padid->pszFileName = NULL;
243 padid->pszFileName = HeapAlloc( MSACM_hHeap, 0, strlen(pszFileName)+1 );
244 strcpy( padid->pszFileName, pszFileName );
246 padid->hInstModule = hinstModule;
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);
265 /***********************************************************************
266 * MSACM_RegisterAllDrivers()
268 void MSACM_RegisterAllDrivers(void)
271 DWORD dwBufferLength;
274 * What if the user edits system.ini while the program is running?
275 * Does Windows handle that?
277 if (MSACM_pFirstACMDriverID)
280 /* FIXME: Does not work! How do I determine the section length? */
281 dwBufferLength = 1024;
282 /* EPP GetPrivateProfileSectionA("drivers32", NULL, 0, "system.ini"); */
284 pszBuffer = (LPSTR) HeapAlloc(MSACM_hHeap, 0, dwBufferLength);
285 if (GetPrivateProfileSectionA("drivers32", pszBuffer, dwBufferLength, "system.ini")) {
288 if (!strncasecmp("MSACM.", s, 6)) {
290 while (*s2 != '\0' && *s2 != '=') s2++;
293 MSACM_RegisterDriver(s, s2 + 1, 0);
297 s += strlen(s) + 1; /* Either next char or \0 */
301 HeapFree(MSACM_hHeap, 0, pszBuffer);
303 MSACM_RegisterDriver("msacm32.dll", "msacm32.dll", 0);
306 /***********************************************************************
307 * MSACM_UnregisterDriver()
309 PWINE_ACMDRIVERID MSACM_UnregisterDriver(PWINE_ACMDRIVERID p)
311 PWINE_ACMDRIVERID pNextACMDriverID;
313 while (p->pACMDriverList)
314 acmDriverClose((HACMDRIVER) p->pACMDriverList, 0);
316 if (p->pszDriverAlias)
317 HeapFree(MSACM_hHeap, 0, p->pszDriverAlias);
319 HeapFree(MSACM_hHeap, 0, p->pszFileName);
320 HeapFree(MSACM_hHeap, 0, p->aFormatTag);
322 if (p == MSACM_pFirstACMDriverID)
323 MSACM_pFirstACMDriverID = p->pNextACMDriverID;
324 if (p == MSACM_pLastACMDriverID)
325 MSACM_pLastACMDriverID = p->pPrevACMDriverID;
327 if (p->pPrevACMDriverID)
328 p->pPrevACMDriverID->pNextACMDriverID = p->pNextACMDriverID;
329 if (p->pNextACMDriverID)
330 p->pNextACMDriverID->pPrevACMDriverID = p->pPrevACMDriverID;
332 pNextACMDriverID = p->pNextACMDriverID;
334 HeapFree(MSACM_hHeap, 0, p);
336 return pNextACMDriverID;
339 /***********************************************************************
340 * MSACM_UnregisterAllDrivers()
342 void MSACM_UnregisterAllDrivers(void)
344 PWINE_ACMDRIVERID p = MSACM_pFirstACMDriverID;
348 p = MSACM_UnregisterDriver(p);
352 /***********************************************************************
355 PWINE_ACMOBJ MSACM_GetObj(HACMOBJ hObj, DWORD type)
357 PWINE_ACMOBJ pao = (PWINE_ACMOBJ)hObj;
359 if (pao == NULL || IsBadReadPtr(pao, sizeof(WINE_ACMOBJ)) ||
360 ((type != WINE_ACMOBJ_DONTCARE) && (type != pao->dwType)))
365 /***********************************************************************
366 * MSACM_GetDriverID()
368 PWINE_ACMDRIVERID MSACM_GetDriverID(HACMDRIVERID hDriverID)
370 return (PWINE_ACMDRIVERID)MSACM_GetObj((HACMOBJ)hDriverID, WINE_ACMOBJ_DRIVERID);
373 /***********************************************************************
376 PWINE_ACMDRIVER MSACM_GetDriver(HACMDRIVER hDriver)
378 return (PWINE_ACMDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_DRIVER);
381 /***********************************************************************
384 MMRESULT MSACM_Message(HACMDRIVER had, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
386 PWINE_ACMDRIVER pad = MSACM_GetDriver(had);
388 return pad ? SendDriverMessage(pad->hDrvr, uMsg, lParam1, lParam2) : MMSYSERR_INVALHANDLE;