Use iphlpapi to implement SIO_GET_INTERFACE_LIST in WSAIoctl, and
[wine.git] / dlls / msacm / internal.c
blob5d413683293b84c370f2abd5c1ccc5d8df73e12e
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 <string.h>
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"
38 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
40 /**********************************************************************/
42 HANDLE MSACM_hHeap = NULL;
43 PWINE_ACMDRIVERID MSACM_pFirstACMDriverID = NULL;
44 PWINE_ACMDRIVERID MSACM_pLastACMDriverID = NULL;
46 #if 0
47 /***********************************************************************
48 * MSACM_DumpCache
50 static void MSACM_DumpCache(PWINE_ACMDRIVERID padid)
52 unsigned i;
54 TRACE("cFilterTags=%lu cFormatTags=%lu fdwSupport=%08lx\n",
55 padid->cFilterTags, padid->cFormatTags, padid->fdwSupport);
56 for (i = 0; i < padid->cache->cFormatTags; i++) {
57 TRACE("\tdwFormatTag=%lu cbwfx=%lu\n",
58 padid->aFormatTag[i].dwFormatTag, padid->aFormatTag[i].cbwfx);
61 #endif
63 /***********************************************************************
64 * MSACM_FindFormatTagInCache [internal]
66 * Returns TRUE is the format tag fmtTag is present in the cache.
67 * If so, idx is set to its index.
69 BOOL MSACM_FindFormatTagInCache(WINE_ACMDRIVERID* padid, DWORD fmtTag, LPDWORD idx)
71 unsigned i;
73 for (i = 0; i < padid->cFormatTags; i++) {
74 if (padid->aFormatTag[i].dwFormatTag == fmtTag) {
75 if (idx) *idx = i;
76 return TRUE;
79 return FALSE;
82 /***********************************************************************
83 * MSACM_FillCache
85 static BOOL MSACM_FillCache(PWINE_ACMDRIVERID padid)
87 HACMDRIVER had = 0;
88 int ntag;
89 ACMDRIVERDETAILSW add;
90 ACMFORMATTAGDETAILSW aftd;
92 if (acmDriverOpen(&had, (HACMDRIVERID)padid, 0) != 0)
93 return FALSE;
95 padid->aFormatTag = NULL;
96 add.cbStruct = sizeof(add);
97 if (MSACM_Message(had, ACMDM_DRIVER_DETAILS, (LPARAM)&add, 0))
98 goto errCleanUp;
100 if (add.cFormatTags > 0) {
101 padid->aFormatTag = HeapAlloc(MSACM_hHeap, HEAP_ZERO_MEMORY,
102 add.cFormatTags * sizeof(padid->aFormatTag[0]));
103 if (!padid->aFormatTag) goto errCleanUp;
106 padid->cFormatTags = add.cFormatTags;
107 padid->cFilterTags = add.cFilterTags;
108 padid->fdwSupport = add.fdwSupport;
110 aftd.cbStruct = sizeof(aftd);
112 for (ntag = 0; ntag < add.cFormatTags; ntag++) {
113 aftd.dwFormatTagIndex = ntag;
114 if (MSACM_Message(had, ACMDM_FORMATTAG_DETAILS, (LPARAM)&aftd, ACM_FORMATTAGDETAILSF_INDEX)) {
115 TRACE("IIOs (%s)\n", padid->pszDriverAlias);
116 goto errCleanUp;
118 padid->aFormatTag[ntag].dwFormatTag = aftd.dwFormatTag;
119 padid->aFormatTag[ntag].cbwfx = aftd.cbFormatSize;
122 acmDriverClose(had, 0);
124 return TRUE;
126 errCleanUp:
127 if (had) acmDriverClose(had, 0);
128 HeapFree(MSACM_hHeap, 0, padid->aFormatTag);
129 padid->aFormatTag = NULL;
130 return FALSE;
133 /***********************************************************************
134 * MSACM_GetRegistryKey
136 static LPSTR MSACM_GetRegistryKey(const WINE_ACMDRIVERID* padid)
138 static const char* baseKey = "Software\\Microsoft\\AudioCompressionManager\\DriverCache\\";
139 LPSTR ret;
140 int len;
142 if (!padid->pszDriverAlias) {
143 ERR("No alias needed for registry entry\n");
144 return NULL;
146 len = strlen(baseKey);
147 ret = HeapAlloc(MSACM_hHeap, 0, len + strlen(padid->pszDriverAlias) + 1);
148 if (!ret) return NULL;
150 strcpy(ret, baseKey);
151 strcpy(ret + len, padid->pszDriverAlias);
152 CharLowerA(ret + len);
153 return ret;
156 /***********************************************************************
157 * MSACM_ReadCache
159 static BOOL MSACM_ReadCache(PWINE_ACMDRIVERID padid)
161 LPSTR key = MSACM_GetRegistryKey(padid);
162 HKEY hKey;
163 DWORD type, size;
165 if (!key) return FALSE;
167 padid->aFormatTag = NULL;
169 if (RegCreateKeyA(HKEY_LOCAL_MACHINE, key, &hKey))
170 goto errCleanUp;
172 size = sizeof(padid->cFormatTags);
173 if (RegQueryValueExA(hKey, "cFormatTags", 0, &type, (void*)&padid->cFormatTags, &size))
174 goto errCleanUp;
175 size = sizeof(padid->cFilterTags);
176 if (RegQueryValueExA(hKey, "cFilterTags", 0, &type, (void*)&padid->cFilterTags, &size))
177 goto errCleanUp;
178 size = sizeof(padid->fdwSupport);
179 if (RegQueryValueExA(hKey, "fdwSupport", 0, &type, (void*)&padid->fdwSupport, &size))
180 goto errCleanUp;
182 if (padid->cFormatTags > 0) {
183 size = padid->cFormatTags * sizeof(padid->aFormatTag[0]);
184 padid->aFormatTag = HeapAlloc(MSACM_hHeap, HEAP_ZERO_MEMORY, size);
185 if (!padid->aFormatTag) goto errCleanUp;
186 if (RegQueryValueExA(hKey, "aFormatTagCache", 0, &type, (void*)padid->aFormatTag, &size))
187 goto errCleanUp;
189 HeapFree(MSACM_hHeap, 0, key);
190 return TRUE;
192 errCleanUp:
193 HeapFree(MSACM_hHeap, 0, key);
194 HeapFree(MSACM_hHeap, 0, padid->aFormatTag);
195 padid->aFormatTag = NULL;
196 RegCloseKey(hKey);
197 return FALSE;
200 /***********************************************************************
201 * MSACM_WriteCache
203 static BOOL MSACM_WriteCache(PWINE_ACMDRIVERID padid)
205 LPSTR key = MSACM_GetRegistryKey(padid);
206 HKEY hKey;
208 if (!key) return FALSE;
210 if (RegCreateKeyA(HKEY_LOCAL_MACHINE, key, &hKey))
211 goto errCleanUp;
213 if (RegSetValueExA(hKey, "cFormatTags", 0, REG_DWORD, (void*)&padid->cFormatTags, sizeof(DWORD)))
214 goto errCleanUp;
215 if (RegSetValueExA(hKey, "cFilterTags", 0, REG_DWORD, (void*)&padid->cFilterTags, sizeof(DWORD)))
216 goto errCleanUp;
217 if (RegSetValueExA(hKey, "fdwSupport", 0, REG_DWORD, (void*)&padid->fdwSupport, sizeof(DWORD)))
218 goto errCleanUp;
219 if (RegSetValueExA(hKey, "aFormatTagCache", 0, REG_BINARY,
220 (void*)padid->aFormatTag,
221 padid->cFormatTags * sizeof(padid->aFormatTag[0])))
222 goto errCleanUp;
223 HeapFree(MSACM_hHeap, 0, key);
224 return TRUE;
226 errCleanUp:
227 HeapFree(MSACM_hHeap, 0, key);
228 return FALSE;
231 /***********************************************************************
232 * MSACM_RegisterDriver()
234 PWINE_ACMDRIVERID MSACM_RegisterDriver(LPSTR pszDriverAlias, LPSTR pszFileName,
235 HINSTANCE hinstModule)
237 PWINE_ACMDRIVERID padid;
239 TRACE("('%s', '%s', %p)\n", pszDriverAlias, pszFileName, hinstModule);
241 padid = (PWINE_ACMDRIVERID) HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMDRIVERID));
242 padid->obj.dwType = WINE_ACMOBJ_DRIVERID;
243 padid->obj.pACMDriverID = padid;
244 padid->pszDriverAlias = NULL;
245 if (pszDriverAlias)
247 padid->pszDriverAlias = HeapAlloc( MSACM_hHeap, 0, strlen(pszDriverAlias)+1 );
248 strcpy( padid->pszDriverAlias, pszDriverAlias );
250 padid->pszFileName = NULL;
251 if (pszFileName)
253 padid->pszFileName = HeapAlloc( MSACM_hHeap, 0, strlen(pszFileName)+1 );
254 strcpy( padid->pszFileName, pszFileName );
256 padid->hInstModule = hinstModule;
258 padid->pACMDriverList = NULL;
259 padid->pNextACMDriverID = NULL;
260 padid->pPrevACMDriverID = MSACM_pLastACMDriverID;
261 if (MSACM_pLastACMDriverID)
262 MSACM_pLastACMDriverID->pNextACMDriverID = padid;
263 MSACM_pLastACMDriverID = padid;
264 if (!MSACM_pFirstACMDriverID)
265 MSACM_pFirstACMDriverID = padid;
266 /* disable the driver if we cannot load the cache */
267 if (!MSACM_ReadCache(padid) && !MSACM_FillCache(padid)) {
268 WARN("Couldn't load cache for ACM driver (%s)\n", pszFileName);
269 MSACM_UnregisterDriver(padid);
270 return NULL;
272 return padid;
275 /***********************************************************************
276 * MSACM_RegisterAllDrivers()
278 void MSACM_RegisterAllDrivers(void)
280 LPSTR pszBuffer;
281 DWORD dwBufferLength;
283 /* FIXME
284 * What if the user edits system.ini while the program is running?
285 * Does Windows handle that?
287 if (MSACM_pFirstACMDriverID)
288 return;
290 /* FIXME: Does not work! How do I determine the section length? */
291 dwBufferLength = 1024;
292 /* EPP GetPrivateProfileSectionA("drivers32", NULL, 0, "system.ini"); */
294 pszBuffer = (LPSTR) HeapAlloc(MSACM_hHeap, 0, dwBufferLength);
295 if (GetPrivateProfileSectionA("drivers32", pszBuffer, dwBufferLength, "system.ini")) {
296 char* s = pszBuffer;
297 while (*s) {
298 if (!strncasecmp("MSACM.", s, 6)) {
299 char *s2 = s;
300 while (*s2 != '\0' && *s2 != '=') s2++;
301 if (*s2) {
302 *s2 = '\0';
303 MSACM_RegisterDriver(s, s2 + 1, 0);
304 *s2 = '=';
307 s += strlen(s) + 1; /* Either next char or \0 */
311 HeapFree(MSACM_hHeap, 0, pszBuffer);
313 MSACM_RegisterDriver("msacm32.dll", "msacm32.dll", 0);
316 /***********************************************************************
317 * MSACM_UnregisterDriver()
319 PWINE_ACMDRIVERID MSACM_UnregisterDriver(PWINE_ACMDRIVERID p)
321 PWINE_ACMDRIVERID pNextACMDriverID;
323 while (p->pACMDriverList)
324 acmDriverClose((HACMDRIVER) p->pACMDriverList, 0);
326 if (p->pszDriverAlias)
327 HeapFree(MSACM_hHeap, 0, p->pszDriverAlias);
328 if (p->pszFileName)
329 HeapFree(MSACM_hHeap, 0, p->pszFileName);
330 HeapFree(MSACM_hHeap, 0, p->aFormatTag);
332 if (p == MSACM_pFirstACMDriverID)
333 MSACM_pFirstACMDriverID = p->pNextACMDriverID;
334 if (p == MSACM_pLastACMDriverID)
335 MSACM_pLastACMDriverID = p->pPrevACMDriverID;
337 if (p->pPrevACMDriverID)
338 p->pPrevACMDriverID->pNextACMDriverID = p->pNextACMDriverID;
339 if (p->pNextACMDriverID)
340 p->pNextACMDriverID->pPrevACMDriverID = p->pPrevACMDriverID;
342 pNextACMDriverID = p->pNextACMDriverID;
344 HeapFree(MSACM_hHeap, 0, p);
346 return pNextACMDriverID;
349 /***********************************************************************
350 * MSACM_UnregisterAllDrivers()
352 void MSACM_UnregisterAllDrivers(void)
354 PWINE_ACMDRIVERID p = MSACM_pFirstACMDriverID;
356 while (p) {
357 MSACM_WriteCache(p);
358 p = MSACM_UnregisterDriver(p);
362 /***********************************************************************
363 * MSACM_GetObj()
365 PWINE_ACMOBJ MSACM_GetObj(HACMOBJ hObj, DWORD type)
367 PWINE_ACMOBJ pao = (PWINE_ACMOBJ)hObj;
369 if (pao == NULL || IsBadReadPtr(pao, sizeof(WINE_ACMOBJ)) ||
370 ((type != WINE_ACMOBJ_DONTCARE) && (type != pao->dwType)))
371 return NULL;
372 return pao;
375 /***********************************************************************
376 * MSACM_GetDriverID()
378 PWINE_ACMDRIVERID MSACM_GetDriverID(HACMDRIVERID hDriverID)
380 return (PWINE_ACMDRIVERID)MSACM_GetObj((HACMOBJ)hDriverID, WINE_ACMOBJ_DRIVERID);
383 /***********************************************************************
384 * MSACM_GetDriver()
386 PWINE_ACMDRIVER MSACM_GetDriver(HACMDRIVER hDriver)
388 return (PWINE_ACMDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_DRIVER);
391 /***********************************************************************
392 * MSACM_Message()
394 MMRESULT MSACM_Message(HACMDRIVER had, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
396 PWINE_ACMDRIVER pad = MSACM_GetDriver(had);
398 return pad ? SendDriverMessage(pad->hDrvr, uMsg, lParam1, lParam2) : MMSYSERR_INVALHANDLE;