1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
6 * Copyright 1998 Patrik Stridvall
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 /**********************************************************************/
30 HANDLE MSACM_hHeap
= NULL
;
31 PWINE_ACMDRIVERID MSACM_pFirstACMDriverID
= NULL
;
32 static PWINE_ACMDRIVERID MSACM_pLastACMDriverID
;
34 static DWORD MSACM_suspendBroadcastCount
= 0;
35 static BOOL MSACM_pendingBroadcast
= FALSE
;
36 static PWINE_ACMNOTIFYWND MSACM_pFirstACMNotifyWnd
= NULL
;
37 static PWINE_ACMNOTIFYWND MSACM_pLastACMNotifyWnd
= NULL
;
39 static void MSACM_ReorderDriversByPriority(void);
41 /***********************************************************************
42 * MSACM_RegisterDriverFromRegistry()
44 PWINE_ACMDRIVERID
MSACM_RegisterDriverFromRegistry(LPCWSTR pszRegEntry
)
46 static const WCHAR msacmW
[] = {'M','S','A','C','M','.'};
47 static const WCHAR drvkey
[] = {'S','o','f','t','w','a','r','e','\\',
48 'M','i','c','r','o','s','o','f','t','\\',
49 'W','i','n','d','o','w','s',' ','N','T','\\',
50 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
51 'D','r','i','v','e','r','s','3','2','\0'};
55 PWINE_ACMDRIVERID padid
= NULL
;
57 /* The requested registry entry must have the format msacm.XXXXX in order to
58 be recognized in any future sessions of msacm
60 if (0 == strncmpiW(pszRegEntry
, msacmW
, sizeof(msacmW
)/sizeof(WCHAR
))) {
61 lRet
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, drvkey
, 0, KEY_QUERY_VALUE
, &hKey
);
62 if (lRet
!= ERROR_SUCCESS
) {
63 WARN("unable to open registry key - 0x%08x\n", lRet
);
66 lRet
= RegQueryValueExW(hKey
, pszRegEntry
, NULL
, NULL
, (LPBYTE
)buf
, &bufLen
);
67 if (lRet
!= ERROR_SUCCESS
) {
68 WARN("unable to query requested subkey %s - 0x%08x\n", debugstr_w(pszRegEntry
), lRet
);
70 MSACM_RegisterDriver(pszRegEntry
, buf
, 0);
79 /***********************************************************************
82 static void MSACM_DumpCache(PWINE_ACMDRIVERID padid
)
86 TRACE("cFilterTags=%lu cFormatTags=%lu fdwSupport=%08lx\n",
87 padid
->cFilterTags
, padid
->cFormatTags
, padid
->fdwSupport
);
88 for (i
= 0; i
< padid
->cache
->cFormatTags
; i
++) {
89 TRACE("\tdwFormatTag=%lu cbwfx=%lu\n",
90 padid
->aFormatTag
[i
].dwFormatTag
, padid
->aFormatTag
[i
].cbwfx
);
95 /***********************************************************************
96 * MSACM_FindFormatTagInCache [internal]
98 * Returns TRUE is the format tag fmtTag is present in the cache.
99 * If so, idx is set to its index.
101 BOOL
MSACM_FindFormatTagInCache(const WINE_ACMDRIVERID
* padid
, DWORD fmtTag
, LPDWORD idx
)
105 for (i
= 0; i
< padid
->cFormatTags
; i
++) {
106 if (padid
->aFormatTag
[i
].dwFormatTag
== fmtTag
) {
114 /***********************************************************************
117 static BOOL
MSACM_FillCache(PWINE_ACMDRIVERID padid
)
121 ACMDRIVERDETAILSW add
;
122 ACMFORMATTAGDETAILSW aftd
;
124 if (acmDriverOpen(&had
, (HACMDRIVERID
)padid
, 0) != 0)
127 padid
->aFormatTag
= NULL
;
128 add
.cbStruct
= sizeof(add
);
129 if (MSACM_Message(had
, ACMDM_DRIVER_DETAILS
, (LPARAM
)&add
, 0))
132 if (add
.cFormatTags
> 0) {
133 padid
->aFormatTag
= HeapAlloc(MSACM_hHeap
, HEAP_ZERO_MEMORY
,
134 add
.cFormatTags
* sizeof(padid
->aFormatTag
[0]));
135 if (!padid
->aFormatTag
) goto errCleanUp
;
138 padid
->cFormatTags
= add
.cFormatTags
;
139 padid
->cFilterTags
= add
.cFilterTags
;
140 padid
->fdwSupport
= add
.fdwSupport
;
142 aftd
.cbStruct
= sizeof(aftd
);
144 for (ntag
= 0; ntag
< add
.cFormatTags
; ntag
++) {
145 aftd
.dwFormatTagIndex
= ntag
;
146 if (MSACM_Message(had
, ACMDM_FORMATTAG_DETAILS
, (LPARAM
)&aftd
, ACM_FORMATTAGDETAILSF_INDEX
)) {
147 TRACE("IIOs (%s)\n", debugstr_w(padid
->pszDriverAlias
));
150 padid
->aFormatTag
[ntag
].dwFormatTag
= aftd
.dwFormatTag
;
151 padid
->aFormatTag
[ntag
].cbwfx
= aftd
.cbFormatSize
;
154 acmDriverClose(had
, 0);
159 if (had
) acmDriverClose(had
, 0);
160 HeapFree(MSACM_hHeap
, 0, padid
->aFormatTag
);
161 padid
->aFormatTag
= NULL
;
165 /***********************************************************************
166 * MSACM_GetRegistryKey
168 static LPWSTR
MSACM_GetRegistryKey(const WINE_ACMDRIVERID
* padid
)
170 static const WCHAR baseKey
[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
171 'A','u','d','i','o','C','o','m','p','r','e','s','s','i','o','n','M','a','n','a','g','e','r','\\',
172 'D','r','i','v','e','r','C','a','c','h','e','\\','\0'};
176 if (!padid
->pszDriverAlias
) {
177 ERR("No alias needed for registry entry\n");
180 len
= strlenW(baseKey
);
181 ret
= HeapAlloc(MSACM_hHeap
, 0, (len
+ strlenW(padid
->pszDriverAlias
) + 1) * sizeof(WCHAR
));
182 if (!ret
) return NULL
;
184 strcpyW(ret
, baseKey
);
185 strcpyW(ret
+ len
, padid
->pszDriverAlias
);
186 CharLowerW(ret
+ len
);
190 /***********************************************************************
193 static BOOL
MSACM_ReadCache(PWINE_ACMDRIVERID padid
)
195 LPWSTR key
= MSACM_GetRegistryKey(padid
);
199 if (!key
) return FALSE
;
201 padid
->aFormatTag
= NULL
;
203 if (RegCreateKeyW(HKEY_LOCAL_MACHINE
, key
, &hKey
))
206 size
= sizeof(padid
->cFormatTags
);
207 if (RegQueryValueExA(hKey
, "cFormatTags", 0, &type
, (void*)&padid
->cFormatTags
, &size
))
209 size
= sizeof(padid
->cFilterTags
);
210 if (RegQueryValueExA(hKey
, "cFilterTags", 0, &type
, (void*)&padid
->cFilterTags
, &size
))
212 size
= sizeof(padid
->fdwSupport
);
213 if (RegQueryValueExA(hKey
, "fdwSupport", 0, &type
, (void*)&padid
->fdwSupport
, &size
))
216 if (padid
->cFormatTags
> 0) {
217 size
= padid
->cFormatTags
* sizeof(padid
->aFormatTag
[0]);
218 padid
->aFormatTag
= HeapAlloc(MSACM_hHeap
, HEAP_ZERO_MEMORY
, size
);
219 if (!padid
->aFormatTag
) goto errCleanUp
;
220 if (RegQueryValueExA(hKey
, "aFormatTagCache", 0, &type
, (void*)padid
->aFormatTag
, &size
))
223 HeapFree(MSACM_hHeap
, 0, key
);
227 HeapFree(MSACM_hHeap
, 0, key
);
228 HeapFree(MSACM_hHeap
, 0, padid
->aFormatTag
);
229 padid
->aFormatTag
= NULL
;
234 /***********************************************************************
237 static BOOL
MSACM_WriteCache(const WINE_ACMDRIVERID
*padid
)
239 LPWSTR key
= MSACM_GetRegistryKey(padid
);
242 if (!key
) return FALSE
;
244 if (RegCreateKeyW(HKEY_LOCAL_MACHINE
, key
, &hKey
))
247 if (RegSetValueExA(hKey
, "cFormatTags", 0, REG_DWORD
, (const void*)&padid
->cFormatTags
, sizeof(DWORD
)))
249 if (RegSetValueExA(hKey
, "cFilterTags", 0, REG_DWORD
, (const void*)&padid
->cFilterTags
, sizeof(DWORD
)))
251 if (RegSetValueExA(hKey
, "fdwSupport", 0, REG_DWORD
, (const void*)&padid
->fdwSupport
, sizeof(DWORD
)))
253 if (RegSetValueExA(hKey
, "aFormatTagCache", 0, REG_BINARY
,
254 (void*)padid
->aFormatTag
,
255 padid
->cFormatTags
* sizeof(padid
->aFormatTag
[0])))
257 HeapFree(MSACM_hHeap
, 0, key
);
261 HeapFree(MSACM_hHeap
, 0, key
);
265 /***********************************************************************
266 * MSACM_RegisterDriver()
268 PWINE_ACMDRIVERID
MSACM_RegisterDriver(LPCWSTR pszDriverAlias
, LPCWSTR pszFileName
,
269 PWINE_ACMLOCALDRIVER pLocalDriver
)
271 PWINE_ACMDRIVERID padid
;
273 TRACE("(%s, %s, %p)\n",
274 debugstr_w(pszDriverAlias
), debugstr_w(pszFileName
), pLocalDriver
);
276 padid
= HeapAlloc(MSACM_hHeap
, 0, sizeof(WINE_ACMDRIVERID
));
279 padid
->obj
.dwType
= WINE_ACMOBJ_DRIVERID
;
280 padid
->obj
.pACMDriverID
= padid
;
281 padid
->pszDriverAlias
= NULL
;
284 padid
->pszDriverAlias
= HeapAlloc( MSACM_hHeap
, 0, (strlenW(pszDriverAlias
)+1) * sizeof(WCHAR
) );
285 if (!padid
->pszDriverAlias
) {
286 HeapFree(MSACM_hHeap
, 0, padid
);
289 strcpyW( padid
->pszDriverAlias
, pszDriverAlias
);
291 padid
->pszFileName
= NULL
;
294 padid
->pszFileName
= HeapAlloc( MSACM_hHeap
, 0, (strlenW(pszFileName
)+1) * sizeof(WCHAR
) );
295 if (!padid
->pszFileName
) {
296 HeapFree(MSACM_hHeap
, 0, padid
->pszDriverAlias
);
297 HeapFree(MSACM_hHeap
, 0, padid
);
300 strcpyW( padid
->pszFileName
, pszFileName
);
302 padid
->pLocalDriver
= pLocalDriver
;
304 padid
->pACMDriverList
= NULL
;
307 padid
->pPrevACMDriverID
= NULL
;
308 padid
->pNextACMDriverID
= MSACM_pFirstACMDriverID
;
309 if (MSACM_pFirstACMDriverID
)
310 MSACM_pFirstACMDriverID
->pPrevACMDriverID
= padid
;
311 MSACM_pFirstACMDriverID
= padid
;
312 if (!MSACM_pLastACMDriverID
)
313 MSACM_pLastACMDriverID
= padid
;
315 padid
->pNextACMDriverID
= NULL
;
316 padid
->pPrevACMDriverID
= MSACM_pLastACMDriverID
;
317 if (MSACM_pLastACMDriverID
)
318 MSACM_pLastACMDriverID
->pNextACMDriverID
= padid
;
319 MSACM_pLastACMDriverID
= padid
;
320 if (!MSACM_pFirstACMDriverID
)
321 MSACM_pFirstACMDriverID
= padid
;
323 /* disable the driver if we cannot load the cache */
324 if ((!padid
->pszDriverAlias
|| !MSACM_ReadCache(padid
)) && !MSACM_FillCache(padid
)) {
325 WARN("Couldn't load cache for ACM driver (%s)\n", debugstr_w(pszFileName
));
326 MSACM_UnregisterDriver(padid
);
330 if (pLocalDriver
) padid
->fdwSupport
|= ACMDRIVERDETAILS_SUPPORTF_LOCAL
;
334 /***********************************************************************
335 * MSACM_RegisterAllDrivers()
337 void MSACM_RegisterAllDrivers(void)
339 static const WCHAR msacm32
[] = {'m','s','a','c','m','3','2','.','d','l','l','\0'};
340 static const WCHAR msacmW
[] = {'M','S','A','C','M','.'};
341 static const WCHAR drv32
[] = {'d','r','i','v','e','r','s','3','2','\0'};
342 static const WCHAR sys
[] = {'s','y','s','t','e','m','.','i','n','i','\0'};
343 static const WCHAR drvkey
[] = {'S','o','f','t','w','a','r','e','\\',
344 'M','i','c','r','o','s','o','f','t','\\',
345 'W','i','n','d','o','w','s',' ','N','T','\\',
346 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
347 'D','r','i','v','e','r','s','3','2','\0'};
348 DWORD i
, cnt
, bufLen
, lRet
, type
;
349 WCHAR buf
[2048], valname
[64], *name
, *s
;
353 /* FIXME: What if the user edits system.ini while the program is running?
354 * Does Windows handle that? */
355 if (MSACM_pFirstACMDriverID
) return;
357 lRet
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, drvkey
, 0, KEY_QUERY_VALUE
, &hKey
);
358 if (lRet
== ERROR_SUCCESS
) {
359 RegQueryInfoKeyW( hKey
, 0, 0, 0, &cnt
, 0, 0, 0, 0, 0, 0, 0);
360 for (i
= 0; i
< cnt
; i
++) {
361 bufLen
= sizeof(buf
) / sizeof(buf
[0]);
362 lRet
= RegEnumKeyExW(hKey
, i
, buf
, &bufLen
, 0, 0, 0, &lastWrite
);
363 if (lRet
!= ERROR_SUCCESS
) continue;
364 if (strncmpiW(buf
, msacmW
, sizeof(msacmW
)/sizeof(msacmW
[0]))) continue;
365 if (!(name
= strchrW(buf
, '='))) continue;
367 MSACM_RegisterDriver(buf
, name
+ 1, 0);
370 cnt
= sizeof(valname
) / sizeof(*valname
);
371 bufLen
= sizeof(buf
);
372 while(RegEnumValueW(hKey
, i
, valname
, &cnt
, 0,
373 &type
, (BYTE
*)buf
, &bufLen
) == ERROR_SUCCESS
){
374 if(!strncmpiW(valname
, msacmW
, sizeof(msacmW
) / sizeof(*msacmW
)))
375 MSACM_RegisterDriver(valname
, buf
, 0);
381 if (GetPrivateProfileSectionW(drv32
, buf
, sizeof(buf
)/sizeof(buf
[0]), sys
))
383 for(s
= buf
; *s
; s
+= strlenW(s
) + 1)
385 if (strncmpiW(s
, msacmW
, sizeof(msacmW
)/sizeof(msacmW
[0]))) continue;
386 if (!(name
= strchrW(s
, '='))) continue;
388 MSACM_RegisterDriver(s
, name
+ 1, 0);
392 MSACM_ReorderDriversByPriority();
393 MSACM_RegisterDriver(msacm32
, msacm32
, 0);
396 /***********************************************************************
397 * MSACM_RegisterNotificationWindow()
399 PWINE_ACMNOTIFYWND
MSACM_RegisterNotificationWindow(HWND hNotifyWnd
, DWORD dwNotifyMsg
)
401 PWINE_ACMNOTIFYWND panwnd
;
403 TRACE("(%p,0x%08x)\n", hNotifyWnd
, dwNotifyMsg
);
405 panwnd
= HeapAlloc(MSACM_hHeap
, 0, sizeof(WINE_ACMNOTIFYWND
));
406 panwnd
->obj
.dwType
= WINE_ACMOBJ_NOTIFYWND
;
407 panwnd
->obj
.pACMDriverID
= 0;
408 panwnd
->hNotifyWnd
= hNotifyWnd
;
409 panwnd
->dwNotifyMsg
= dwNotifyMsg
;
410 panwnd
->fdwSupport
= 0;
412 panwnd
->pNextACMNotifyWnd
= NULL
;
413 panwnd
->pPrevACMNotifyWnd
= MSACM_pLastACMNotifyWnd
;
414 if (MSACM_pLastACMNotifyWnd
)
415 MSACM_pLastACMNotifyWnd
->pNextACMNotifyWnd
= panwnd
;
416 MSACM_pLastACMNotifyWnd
= panwnd
;
417 if (!MSACM_pFirstACMNotifyWnd
)
418 MSACM_pFirstACMNotifyWnd
= panwnd
;
423 /***********************************************************************
424 * MSACM_BroadcastNotification()
426 void MSACM_BroadcastNotification(void)
428 if (MSACM_suspendBroadcastCount
<= 0) {
429 PWINE_ACMNOTIFYWND panwnd
;
431 for (panwnd
= MSACM_pFirstACMNotifyWnd
; panwnd
; panwnd
= panwnd
->pNextACMNotifyWnd
)
432 if (!(panwnd
->fdwSupport
& ACMDRIVERDETAILS_SUPPORTF_DISABLED
))
433 SendMessageW(panwnd
->hNotifyWnd
, panwnd
->dwNotifyMsg
, 0, 0);
435 MSACM_pendingBroadcast
= TRUE
;
439 /***********************************************************************
440 * MSACM_DisableNotifications()
442 void MSACM_DisableNotifications(void)
444 MSACM_suspendBroadcastCount
++;
447 /***********************************************************************
448 * MSACM_EnableNotifications()
450 void MSACM_EnableNotifications(void)
452 if (MSACM_suspendBroadcastCount
> 0) {
453 MSACM_suspendBroadcastCount
--;
454 if (MSACM_suspendBroadcastCount
== 0 && MSACM_pendingBroadcast
) {
455 MSACM_pendingBroadcast
= FALSE
;
456 MSACM_BroadcastNotification();
461 /***********************************************************************
462 * MSACM_UnRegisterNotificationWindow()
464 PWINE_ACMNOTIFYWND
MSACM_UnRegisterNotificationWindow(const WINE_ACMNOTIFYWND
*panwnd
)
466 PWINE_ACMNOTIFYWND p
;
468 for (p
= MSACM_pFirstACMNotifyWnd
; p
; p
= p
->pNextACMNotifyWnd
) {
470 PWINE_ACMNOTIFYWND pNext
= p
->pNextACMNotifyWnd
;
472 if (p
->pPrevACMNotifyWnd
) p
->pPrevACMNotifyWnd
->pNextACMNotifyWnd
= p
->pNextACMNotifyWnd
;
473 if (p
->pNextACMNotifyWnd
) p
->pNextACMNotifyWnd
->pPrevACMNotifyWnd
= p
->pPrevACMNotifyWnd
;
474 if (MSACM_pFirstACMNotifyWnd
== p
) MSACM_pFirstACMNotifyWnd
= p
->pNextACMNotifyWnd
;
475 if (MSACM_pLastACMNotifyWnd
== p
) MSACM_pLastACMNotifyWnd
= p
->pPrevACMNotifyWnd
;
476 HeapFree(MSACM_hHeap
, 0, p
);
484 /***********************************************************************
485 * MSACM_RePositionDriver()
487 void MSACM_RePositionDriver(PWINE_ACMDRIVERID padid
, DWORD dwPriority
)
489 PWINE_ACMDRIVERID pTargetPosition
= NULL
;
491 /* Remove selected driver from linked list */
492 if (MSACM_pFirstACMDriverID
== padid
) {
493 MSACM_pFirstACMDriverID
= padid
->pNextACMDriverID
;
495 if (MSACM_pLastACMDriverID
== padid
) {
496 MSACM_pLastACMDriverID
= padid
->pPrevACMDriverID
;
498 if (padid
->pPrevACMDriverID
!= NULL
) {
499 padid
->pPrevACMDriverID
->pNextACMDriverID
= padid
->pNextACMDriverID
;
501 if (padid
->pNextACMDriverID
!= NULL
) {
502 padid
->pNextACMDriverID
->pPrevACMDriverID
= padid
->pPrevACMDriverID
;
505 /* Look up position where selected driver should be */
506 if (dwPriority
== 1) {
507 pTargetPosition
= padid
->pPrevACMDriverID
;
508 while (pTargetPosition
->pPrevACMDriverID
!= NULL
&&
509 !(pTargetPosition
->pPrevACMDriverID
->fdwSupport
& ACMDRIVERDETAILS_SUPPORTF_LOCAL
)) {
510 pTargetPosition
= pTargetPosition
->pPrevACMDriverID
;
512 } else if (dwPriority
== -1) {
513 pTargetPosition
= padid
->pNextACMDriverID
;
514 while (pTargetPosition
->pNextACMDriverID
!= NULL
) {
515 pTargetPosition
= pTargetPosition
->pNextACMDriverID
;
519 /* Place selected driver in selected position */
520 padid
->pPrevACMDriverID
= pTargetPosition
->pPrevACMDriverID
;
521 padid
->pNextACMDriverID
= pTargetPosition
;
522 if (padid
->pPrevACMDriverID
!= NULL
) {
523 padid
->pPrevACMDriverID
->pNextACMDriverID
= padid
;
525 MSACM_pFirstACMDriverID
= padid
;
527 if (padid
->pNextACMDriverID
!= NULL
) {
528 padid
->pNextACMDriverID
->pPrevACMDriverID
= padid
;
530 MSACM_pLastACMDriverID
= padid
;
534 /***********************************************************************
535 * MSACM_ReorderDriversByPriority()
536 * Reorders all drivers based on the priority list indicated by the registry key:
537 * HKCU\\Software\\Microsoft\\Multimedia\\Audio Compression Manager\\Priority v4.00
539 static void MSACM_ReorderDriversByPriority(void)
541 PWINE_ACMDRIVERID padid
;
542 unsigned int iNumDrivers
;
543 PWINE_ACMDRIVERID
* driverList
= NULL
;
544 HKEY hPriorityKey
= NULL
;
548 /* Count drivers && alloc corresponding memory for list */
550 for (padid
= MSACM_pFirstACMDriverID
; padid
; padid
= padid
->pNextACMDriverID
) iNumDrivers
++;
554 static const WCHAR basePriorityKey
[] = {
555 'S','o','f','t','w','a','r','e','\\',
556 'M','i','c','r','o','s','o','f','t','\\',
557 'M','u','l','t','i','m','e','d','i','a','\\',
558 'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
559 'P','r','i','o','r','i','t','y',' ','v','4','.','0','0','\0'
565 driverList
= HeapAlloc(MSACM_hHeap
, 0, iNumDrivers
* sizeof(PWINE_ACMDRIVERID
));
568 ERR("out of memory\n");
572 lError
= RegOpenKeyW(HKEY_CURRENT_USER
, basePriorityKey
, &hPriorityKey
);
573 if (lError
!= ERROR_SUCCESS
) {
574 TRACE("RegOpenKeyW failed, possibly key does not exist yet\n");
579 /* Copy drivers into list to simplify linked list modification */
580 for (i
= 0, padid
= MSACM_pFirstACMDriverID
; padid
; padid
= padid
->pNextACMDriverID
, i
++)
582 driverList
[i
] = padid
;
585 /* Query each of the priorities in turn. Alias key is in lowercase.
586 The general form of the priority record is the following:
587 "PriorityN" --> "1, msacm.driveralias"
588 where N is an integer, and the value is a string of the driver
589 alias, prefixed by "1, " for an enabled driver, or "0, " for a
592 for (i
= 0; i
< iNumDrivers
; i
++)
594 static const WCHAR priorityTmpl
[] = {'P','r','i','o','r','i','t','y','%','l','d','\0'};
596 unsigned int iTargetPosition
;
597 unsigned int iCurrentPosition
;
599 static const WCHAR sPrefix
[] = {'m','s','a','c','m','.','\0'};
601 /* Build expected entry name */
602 snprintfW(szSubKey
, 17, priorityTmpl
, i
+ 1);
603 lBufferLength
= sizeof(szBuffer
);
604 lError
= RegQueryValueExW(hPriorityKey
, szSubKey
, NULL
, NULL
, (LPBYTE
)szBuffer
, (LPDWORD
)&lBufferLength
);
605 if (lError
!= ERROR_SUCCESS
) continue;
607 /* Recovered driver alias should be at this position */
610 /* Locate driver alias in driver list */
611 pAlias
= strstrW(szBuffer
, sPrefix
);
612 if (pAlias
== NULL
) continue;
614 for (iCurrentPosition
= 0; iCurrentPosition
< iNumDrivers
; iCurrentPosition
++) {
615 if (strcmpiW(driverList
[iCurrentPosition
]->pszDriverAlias
, pAlias
) == 0)
618 if (iCurrentPosition
< iNumDrivers
&& iTargetPosition
!= iCurrentPosition
) {
619 padid
= driverList
[iTargetPosition
];
620 driverList
[iTargetPosition
] = driverList
[iCurrentPosition
];
621 driverList
[iCurrentPosition
] = padid
;
623 /* Locate enabled status */
624 if (szBuffer
[0] == '1') {
625 driverList
[iTargetPosition
]->fdwSupport
&= ~ACMDRIVERDETAILS_SUPPORTF_DISABLED
;
626 } else if (szBuffer
[0] == '0') {
627 driverList
[iTargetPosition
]->fdwSupport
|= ACMDRIVERDETAILS_SUPPORTF_DISABLED
;
632 /* Re-assign pointers so that linked list traverses the ordered array */
633 for (i
= 0; i
< iNumDrivers
; i
++) {
634 driverList
[i
]->pPrevACMDriverID
= (i
> 0) ? driverList
[i
- 1] : NULL
;
635 driverList
[i
]->pNextACMDriverID
= (i
< iNumDrivers
- 1) ? driverList
[i
+ 1] : NULL
;
637 MSACM_pFirstACMDriverID
= driverList
[0];
638 MSACM_pLastACMDriverID
= driverList
[iNumDrivers
- 1];
642 if (hPriorityKey
!= NULL
) RegCloseKey(hPriorityKey
);
643 HeapFree(MSACM_hHeap
, 0, driverList
);
646 /***********************************************************************
647 * MSACM_WriteCurrentPriorities()
648 * Writes out current order of driver priorities to registry key:
649 * HKCU\\Software\\Microsoft\\Multimedia\\Audio Compression Manager\\Priority v4.00
651 void MSACM_WriteCurrentPriorities(void)
655 static const WCHAR basePriorityKey
[] = {
656 'S','o','f','t','w','a','r','e','\\',
657 'M','i','c','r','o','s','o','f','t','\\',
658 'M','u','l','t','i','m','e','d','i','a','\\',
659 'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
660 'P','r','i','o','r','i','t','y',' ','v','4','.','0','0','\0'
662 PWINE_ACMDRIVERID padid
;
663 DWORD dwPriorityCounter
;
664 static const WCHAR priorityTmpl
[] = {'P','r','i','o','r','i','t','y','%','l','d','\0'};
665 static const WCHAR valueTmpl
[] = {'%','c',',',' ','%','s','\0'};
666 static const WCHAR converterAlias
[] = {'I','n','t','e','r','n','a','l',' ','P','C','M',' ','C','o','n','v','e','r','t','e','r','\0'};
670 /* Delete ACM priority key and create it anew */
671 lError
= RegDeleteKeyW(HKEY_CURRENT_USER
, basePriorityKey
);
672 if (lError
!= ERROR_SUCCESS
&& lError
!= ERROR_FILE_NOT_FOUND
) {
673 ERR("unable to remove current key %s (0x%08x) - priority changes won't persist past application end.\n",
674 debugstr_w(basePriorityKey
), lError
);
677 lError
= RegCreateKeyW(HKEY_CURRENT_USER
, basePriorityKey
, &hPriorityKey
);
678 if (lError
!= ERROR_SUCCESS
) {
679 ERR("unable to create key %s (0x%08x) - priority changes won't persist past application end.\n",
680 debugstr_w(basePriorityKey
), lError
);
684 /* Write current list of priorities */
685 for (dwPriorityCounter
= 0, padid
= MSACM_pFirstACMDriverID
; padid
; padid
= padid
->pNextACMDriverID
) {
686 if (padid
->fdwSupport
& ACMDRIVERDETAILS_SUPPORTF_LOCAL
) continue;
687 if (padid
->pszDriverAlias
== NULL
) continue; /* internal PCM converter is last */
689 /* Build required value name */
691 snprintfW(szSubKey
, 17, priorityTmpl
, dwPriorityCounter
);
693 /* Value has a 1 in front for enabled drivers and 0 for disabled drivers */
694 snprintfW(szBuffer
, 256, valueTmpl
, (padid
->fdwSupport
& ACMDRIVERDETAILS_SUPPORTF_DISABLED
) ? '0' : '1', padid
->pszDriverAlias
);
697 lError
= RegSetValueExW(hPriorityKey
, szSubKey
, 0, REG_SZ
, (BYTE
*)szBuffer
, (strlenW(szBuffer
) + 1) * sizeof(WCHAR
));
698 if (lError
!= ERROR_SUCCESS
) {
699 ERR("unable to write value for %s under key %s (0x%08x)\n",
700 debugstr_w(padid
->pszDriverAlias
), debugstr_w(basePriorityKey
), lError
);
704 /* Build required value name */
706 snprintfW(szSubKey
, 17, priorityTmpl
, dwPriorityCounter
);
708 /* Value has a 1 in front for enabled drivers and 0 for disabled drivers */
709 snprintfW(szBuffer
, 256, valueTmpl
, '1', converterAlias
);
711 lError
= RegSetValueExW(hPriorityKey
, szSubKey
, 0, REG_SZ
, (BYTE
*)szBuffer
, (strlenW(szBuffer
) + 1) * sizeof(WCHAR
));
712 if (lError
!= ERROR_SUCCESS
) {
713 ERR("unable to write value for %s under key %s (0x%08x)\n",
714 debugstr_w(converterAlias
), debugstr_w(basePriorityKey
), lError
);
716 RegCloseKey(hPriorityKey
);
719 static PWINE_ACMLOCALDRIVER MSACM_pFirstACMLocalDriver
;
720 static PWINE_ACMLOCALDRIVER MSACM_pLastACMLocalDriver
;
722 static PWINE_ACMLOCALDRIVER
MSACM_UnregisterLocalDriver(PWINE_ACMLOCALDRIVER paldrv
)
724 PWINE_ACMLOCALDRIVER pNextACMLocalDriver
;
726 if (paldrv
->pACMInstList
) {
727 ERR("local driver instances still present after closing all drivers - memory leak\n");
731 if (paldrv
== MSACM_pFirstACMLocalDriver
)
732 MSACM_pFirstACMLocalDriver
= paldrv
->pNextACMLocalDrv
;
733 if (paldrv
== MSACM_pLastACMLocalDriver
)
734 MSACM_pLastACMLocalDriver
= paldrv
->pPrevACMLocalDrv
;
736 if (paldrv
->pPrevACMLocalDrv
)
737 paldrv
->pPrevACMLocalDrv
->pNextACMLocalDrv
= paldrv
->pNextACMLocalDrv
;
738 if (paldrv
->pNextACMLocalDrv
)
739 paldrv
->pNextACMLocalDrv
->pPrevACMLocalDrv
= paldrv
->pPrevACMLocalDrv
;
741 pNextACMLocalDriver
= paldrv
->pNextACMLocalDrv
;
743 HeapFree(MSACM_hHeap
, 0, paldrv
);
745 return pNextACMLocalDriver
;
748 /***********************************************************************
749 * MSACM_UnregisterDriver()
751 PWINE_ACMDRIVERID
MSACM_UnregisterDriver(PWINE_ACMDRIVERID p
)
753 PWINE_ACMDRIVERID pNextACMDriverID
;
755 while (p
->pACMDriverList
)
756 acmDriverClose((HACMDRIVER
) p
->pACMDriverList
, 0);
758 HeapFree(MSACM_hHeap
, 0, p
->pszDriverAlias
);
759 HeapFree(MSACM_hHeap
, 0, p
->pszFileName
);
760 HeapFree(MSACM_hHeap
, 0, p
->aFormatTag
);
762 if (p
== MSACM_pFirstACMDriverID
)
763 MSACM_pFirstACMDriverID
= p
->pNextACMDriverID
;
764 if (p
== MSACM_pLastACMDriverID
)
765 MSACM_pLastACMDriverID
= p
->pPrevACMDriverID
;
767 if (p
->pPrevACMDriverID
)
768 p
->pPrevACMDriverID
->pNextACMDriverID
= p
->pNextACMDriverID
;
769 if (p
->pNextACMDriverID
)
770 p
->pNextACMDriverID
->pPrevACMDriverID
= p
->pPrevACMDriverID
;
772 pNextACMDriverID
= p
->pNextACMDriverID
;
774 if (p
->pLocalDriver
) MSACM_UnregisterLocalDriver(p
->pLocalDriver
);
775 HeapFree(MSACM_hHeap
, 0, p
);
777 return pNextACMDriverID
;
780 /***********************************************************************
781 * MSACM_UnregisterAllDrivers()
783 void MSACM_UnregisterAllDrivers(void)
785 PWINE_ACMNOTIFYWND panwnd
= MSACM_pFirstACMNotifyWnd
;
786 PWINE_ACMDRIVERID p
= MSACM_pFirstACMDriverID
;
790 p
= MSACM_UnregisterDriver(p
);
794 panwnd
= MSACM_UnRegisterNotificationWindow(panwnd
);
798 /***********************************************************************
801 PWINE_ACMOBJ
MSACM_GetObj(HACMOBJ hObj
, DWORD type
)
803 PWINE_ACMOBJ pao
= (PWINE_ACMOBJ
)hObj
;
805 if (pao
== NULL
|| IsBadReadPtr(pao
, sizeof(WINE_ACMOBJ
)) ||
806 ((type
!= WINE_ACMOBJ_DONTCARE
) && (type
!= pao
->dwType
)))
811 /***********************************************************************
812 * MSACM_GetDriverID()
814 PWINE_ACMDRIVERID
MSACM_GetDriverID(HACMDRIVERID hDriverID
)
816 return (PWINE_ACMDRIVERID
)MSACM_GetObj((HACMOBJ
)hDriverID
, WINE_ACMOBJ_DRIVERID
);
819 /***********************************************************************
822 PWINE_ACMDRIVER
MSACM_GetDriver(HACMDRIVER hDriver
)
824 return (PWINE_ACMDRIVER
)MSACM_GetObj((HACMOBJ
)hDriver
, WINE_ACMOBJ_DRIVER
);
827 /***********************************************************************
828 * MSACM_GetNotifyWnd()
830 PWINE_ACMNOTIFYWND
MSACM_GetNotifyWnd(HACMDRIVERID hDriver
)
832 return (PWINE_ACMNOTIFYWND
)MSACM_GetObj((HACMOBJ
)hDriver
, WINE_ACMOBJ_NOTIFYWND
);
835 /***********************************************************************
836 * MSACM_GetLocalDriver()
839 PWINE_ACMLOCALDRIVER MSACM_GetLocalDriver(HACMDRIVER hDriver)
841 return (PWINE_ACMLOCALDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_LOCALDRIVER);
844 #define MSACM_DRIVER_SendMessage(PDRVRINST, msg, lParam1, lParam2) \
845 (PDRVRINST)->pLocalDriver->lpDrvProc((PDRVRINST)->dwDriverID, (HDRVR)(PDRVRINST), msg, lParam1, lParam2)
847 /***********************************************************************
850 MMRESULT
MSACM_Message(HACMDRIVER had
, UINT uMsg
, LPARAM lParam1
, LPARAM lParam2
)
852 PWINE_ACMDRIVER pad
= MSACM_GetDriver(had
);
854 if (!pad
) return MMSYSERR_INVALHANDLE
;
855 if (pad
->hDrvr
) return SendDriverMessage(pad
->hDrvr
, uMsg
, lParam1
, lParam2
);
856 if (pad
->pLocalDrvrInst
) return MSACM_DRIVER_SendMessage(pad
->pLocalDrvrInst
, uMsg
, lParam1
, lParam2
);
858 return MMSYSERR_INVALHANDLE
;
861 PWINE_ACMLOCALDRIVER
MSACM_RegisterLocalDriver(HMODULE hModule
, DRIVERPROC lpDriverProc
)
863 PWINE_ACMLOCALDRIVER paldrv
;
865 TRACE("(%p, %p)\n", hModule
, lpDriverProc
);
866 if (!hModule
|| !lpDriverProc
) return NULL
;
868 /* look up previous instance of local driver module */
869 for (paldrv
= MSACM_pFirstACMLocalDriver
; paldrv
; paldrv
= paldrv
->pNextACMLocalDrv
)
871 if (paldrv
->hModule
== hModule
&& paldrv
->lpDrvProc
== lpDriverProc
) return paldrv
;
874 paldrv
= HeapAlloc(MSACM_hHeap
, 0, sizeof(WINE_ACMLOCALDRIVER
));
875 paldrv
->obj
.dwType
= WINE_ACMOBJ_LOCALDRIVER
;
876 paldrv
->obj
.pACMDriverID
= 0;
877 paldrv
->hModule
= hModule
;
878 paldrv
->lpDrvProc
= lpDriverProc
;
879 paldrv
->pACMInstList
= NULL
;
881 paldrv
->pNextACMLocalDrv
= NULL
;
882 paldrv
->pPrevACMLocalDrv
= MSACM_pLastACMLocalDriver
;
883 if (MSACM_pLastACMLocalDriver
)
884 MSACM_pLastACMLocalDriver
->pNextACMLocalDrv
= paldrv
;
885 MSACM_pLastACMLocalDriver
= paldrv
;
886 if (!MSACM_pFirstACMLocalDriver
)
887 MSACM_pFirstACMLocalDriver
= paldrv
;
892 /**************************************************************************
893 * MSACM_GetNumberOfModuleRefs [internal]
895 * Returns the number of open drivers which share the same module.
896 * Inspired from implementation in dlls/winmm/driver.c
898 static unsigned MSACM_GetNumberOfModuleRefs(HMODULE hModule
, DRIVERPROC lpDrvProc
, WINE_ACMLOCALDRIVERINST
** found
)
900 PWINE_ACMLOCALDRIVER lpDrv
;
903 if (found
) *found
= NULL
;
904 for (lpDrv
= MSACM_pFirstACMLocalDriver
; lpDrv
; lpDrv
= lpDrv
->pNextACMLocalDrv
)
906 if (lpDrv
->hModule
== hModule
&& lpDrv
->lpDrvProc
== lpDrvProc
)
908 PWINE_ACMLOCALDRIVERINST pInst
= lpDrv
->pACMInstList
;
911 if (found
&& !*found
) *found
= pInst
;
913 pInst
= pInst
->pNextACMInst
;
920 /**************************************************************************
921 * MSACM_RemoveFromList [internal]
923 * Generates all the logic to handle driver closure / deletion
924 * Removes a driver struct to the list of open drivers.
926 static BOOL
MSACM_RemoveFromList(PWINE_ACMLOCALDRIVERINST lpDrv
)
928 PWINE_ACMLOCALDRIVER pDriverBase
= lpDrv
->pLocalDriver
;
929 PWINE_ACMLOCALDRIVERINST pPrevInst
;
931 /* last of this driver in list ? */
932 if (MSACM_GetNumberOfModuleRefs(pDriverBase
->hModule
, pDriverBase
->lpDrvProc
, NULL
) == 1) {
933 MSACM_DRIVER_SendMessage(lpDrv
, DRV_DISABLE
, 0L, 0L);
934 MSACM_DRIVER_SendMessage(lpDrv
, DRV_FREE
, 0L, 0L);
938 if (pDriverBase
->pACMInstList
!= lpDrv
) {
939 pPrevInst
= pDriverBase
->pACMInstList
;
940 while (pPrevInst
&& pPrevInst
->pNextACMInst
!= lpDrv
)
941 pPrevInst
= pPrevInst
->pNextACMInst
;
943 ERR("requested to remove invalid instance %p\n", pPrevInst
);
948 /* first driver instance on list */
949 pDriverBase
->pACMInstList
= lpDrv
->pNextACMInst
;
951 pPrevInst
->pNextACMInst
= lpDrv
->pNextACMInst
;
956 /**************************************************************************
957 * MSACM_AddToList [internal]
959 * Adds a driver struct to the list of open drivers.
960 * Generates all the logic to handle driver creation / open.
962 static BOOL
MSACM_AddToList(PWINE_ACMLOCALDRIVERINST lpNewDrv
, LPARAM lParam2
)
964 PWINE_ACMLOCALDRIVER pDriverBase
= lpNewDrv
->pLocalDriver
;
966 /* first of this driver in list ? */
967 if (MSACM_GetNumberOfModuleRefs(pDriverBase
->hModule
, pDriverBase
->lpDrvProc
, NULL
) == 0) {
968 if (MSACM_DRIVER_SendMessage(lpNewDrv
, DRV_LOAD
, 0L, 0L) != DRV_SUCCESS
) {
969 FIXME("DRV_LOAD failed on driver %p\n", lpNewDrv
);
972 /* returned value is not checked */
973 MSACM_DRIVER_SendMessage(lpNewDrv
, DRV_ENABLE
, 0L, 0L);
976 lpNewDrv
->pNextACMInst
= NULL
;
977 if (pDriverBase
->pACMInstList
== NULL
) {
978 pDriverBase
->pACMInstList
= lpNewDrv
;
980 PWINE_ACMLOCALDRIVERINST lpDrvInst
= pDriverBase
->pACMInstList
;
982 while (lpDrvInst
->pNextACMInst
!= NULL
)
983 lpDrvInst
= lpDrvInst
->pNextACMInst
;
985 lpDrvInst
->pNextACMInst
= lpNewDrv
;
988 /* Now just open a new instance of a driver on this module */
989 lpNewDrv
->dwDriverID
= MSACM_DRIVER_SendMessage(lpNewDrv
, DRV_OPEN
, 0, lParam2
);
991 if (lpNewDrv
->dwDriverID
== 0) {
992 FIXME("DRV_OPEN failed on driver %p\n", lpNewDrv
);
993 MSACM_RemoveFromList(lpNewDrv
);
999 PWINE_ACMLOCALDRIVERINST
MSACM_OpenLocalDriver(PWINE_ACMLOCALDRIVER paldrv
, LPARAM lParam2
)
1001 PWINE_ACMLOCALDRIVERINST pDrvInst
;
1003 pDrvInst
= HeapAlloc(MSACM_hHeap
, 0, sizeof(WINE_ACMLOCALDRIVERINST
));
1007 pDrvInst
->pLocalDriver
= paldrv
;
1008 pDrvInst
->dwDriverID
= 0;
1009 pDrvInst
->pNextACMInst
= NULL
;
1010 pDrvInst
->bSession
= FALSE
;
1012 /* Win32 installable drivers must support a two phase opening scheme:
1013 * + first open with NULL as lParam2 (session instance),
1014 * + then do a second open with the real non null lParam2)
1016 if (MSACM_GetNumberOfModuleRefs(paldrv
->hModule
, paldrv
->lpDrvProc
, NULL
) == 0 && lParam2
)
1018 PWINE_ACMLOCALDRIVERINST ret
;
1020 if (!MSACM_AddToList(pDrvInst
, 0L))
1022 ERR("load0 failed\n");
1025 ret
= MSACM_OpenLocalDriver(paldrv
, lParam2
);
1028 ERR("load1 failed\n");
1029 /* If MSACM_CloseLocalDriver returns TRUE,
1030 * then pDrvInst has been freed
1032 if (!MSACM_CloseLocalDriver(pDrvInst
))
1037 pDrvInst
->bSession
= TRUE
;
1041 if (!MSACM_AddToList(pDrvInst
, lParam2
))
1043 ERR("load failed\n");
1047 TRACE("=> %p\n", pDrvInst
);
1050 HeapFree(MSACM_hHeap
, 0, pDrvInst
);
1054 LRESULT
MSACM_CloseLocalDriver(PWINE_ACMLOCALDRIVERINST paldrv
)
1056 if (MSACM_RemoveFromList(paldrv
)) {
1057 PWINE_ACMLOCALDRIVERINST lpDrv0
;
1058 PWINE_ACMLOCALDRIVER pDriverBase
= paldrv
->pLocalDriver
;
1060 MSACM_DRIVER_SendMessage(paldrv
, DRV_CLOSE
, 0, 0);
1061 paldrv
->dwDriverID
= 0;
1063 if (paldrv
->bSession
)
1064 ERR("should not directly close session instance (%p)\n", paldrv
);
1066 /* if driver has an opened session instance, we have to close it too */
1067 if (MSACM_GetNumberOfModuleRefs(pDriverBase
->hModule
, pDriverBase
->lpDrvProc
, &lpDrv0
) == 1 &&
1070 MSACM_DRIVER_SendMessage(lpDrv0
, DRV_CLOSE
, 0L, 0L);
1071 lpDrv0
->dwDriverID
= 0;
1072 MSACM_RemoveFromList(lpDrv0
);
1073 HeapFree(GetProcessHeap(), 0, lpDrv0
);
1076 HeapFree(MSACM_hHeap
, 0, paldrv
);
1079 ERR("unable to close driver instance\n");