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
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msacm
);
45 /**********************************************************************/
47 HANDLE MSACM_hHeap
= NULL
;
48 PWINE_ACMDRIVERID MSACM_pFirstACMDriverID
= NULL
;
49 static PWINE_ACMDRIVERID MSACM_pLastACMDriverID
;
51 static DWORD MSACM_suspendBroadcastCount
= 0;
52 static BOOL MSACM_pendingBroadcast
= FALSE
;
53 static PWINE_ACMNOTIFYWND MSACM_pFirstACMNotifyWnd
= NULL
;
54 static PWINE_ACMNOTIFYWND MSACM_pLastACMNotifyWnd
= NULL
;
56 static void MSACM_ReorderDriversByPriority(void);
58 /***********************************************************************
59 * MSACM_RegisterDriverFromRegistry()
61 PWINE_ACMDRIVERID
MSACM_RegisterDriverFromRegistry(LPCWSTR pszRegEntry
)
63 static const WCHAR msacmW
[] = {'M','S','A','C','M','.'};
64 static const WCHAR drvkey
[] = {'S','o','f','t','w','a','r','e','\\',
65 'M','i','c','r','o','s','o','f','t','\\',
66 'W','i','n','d','o','w','s',' ','N','T','\\',
67 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
68 'D','r','i','v','e','r','s','3','2','\0'};
72 PWINE_ACMDRIVERID padid
= NULL
;
74 /* The requested registry entry must have the format msacm.XXXXX in order to
75 be recognized in any future sessions of msacm
77 if (0 == _wcsnicmp(pszRegEntry
, msacmW
, ARRAY_SIZE(msacmW
))) {
78 lRet
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, drvkey
, 0, KEY_QUERY_VALUE
, &hKey
);
79 if (lRet
!= ERROR_SUCCESS
) {
80 WARN("unable to open registry key - 0x%08x\n", lRet
);
83 lRet
= RegQueryValueExW(hKey
, pszRegEntry
, NULL
, NULL
, (LPBYTE
)buf
, &bufLen
);
84 if (lRet
!= ERROR_SUCCESS
) {
85 WARN("unable to query requested subkey %s - 0x%08x\n", debugstr_w(pszRegEntry
), lRet
);
87 MSACM_RegisterDriver(pszRegEntry
, buf
, 0);
96 /***********************************************************************
99 static void MSACM_DumpCache(PWINE_ACMDRIVERID padid
)
103 TRACE("cFilterTags=%lu cFormatTags=%lu fdwSupport=%08lx\n",
104 padid
->cFilterTags
, padid
->cFormatTags
, padid
->fdwSupport
);
105 for (i
= 0; i
< padid
->cache
->cFormatTags
; i
++) {
106 TRACE("\tdwFormatTag=%lu cbwfx=%lu\n",
107 padid
->aFormatTag
[i
].dwFormatTag
, padid
->aFormatTag
[i
].cbwfx
);
112 /***********************************************************************
113 * MSACM_FindFormatTagInCache [internal]
115 * Returns TRUE is the format tag fmtTag is present in the cache.
116 * If so, idx is set to its index.
118 BOOL
MSACM_FindFormatTagInCache(const WINE_ACMDRIVERID
* padid
, DWORD fmtTag
, LPDWORD idx
)
122 for (i
= 0; i
< padid
->cFormatTags
; i
++) {
123 if (padid
->aFormatTag
[i
].dwFormatTag
== fmtTag
) {
131 /***********************************************************************
134 static BOOL
MSACM_FillCache(PWINE_ACMDRIVERID padid
)
138 ACMDRIVERDETAILSW add
;
139 ACMFORMATTAGDETAILSW aftd
;
141 if (acmDriverOpen(&had
, (HACMDRIVERID
)padid
, 0) != 0)
144 padid
->aFormatTag
= NULL
;
145 add
.cbStruct
= sizeof(add
);
146 if (MSACM_Message(had
, ACMDM_DRIVER_DETAILS
, (LPARAM
)&add
, 0))
149 if (add
.cFormatTags
> 0) {
150 padid
->aFormatTag
= HeapAlloc(MSACM_hHeap
, HEAP_ZERO_MEMORY
,
151 add
.cFormatTags
* sizeof(padid
->aFormatTag
[0]));
152 if (!padid
->aFormatTag
) goto errCleanUp
;
155 padid
->cFormatTags
= add
.cFormatTags
;
156 padid
->cFilterTags
= add
.cFilterTags
;
157 padid
->fdwSupport
= add
.fdwSupport
;
159 aftd
.cbStruct
= sizeof(aftd
);
161 for (ntag
= 0; ntag
< add
.cFormatTags
; ntag
++) {
162 aftd
.dwFormatTagIndex
= ntag
;
163 if (MSACM_Message(had
, ACMDM_FORMATTAG_DETAILS
, (LPARAM
)&aftd
, ACM_FORMATTAGDETAILSF_INDEX
)) {
164 TRACE("IIOs (%s)\n", debugstr_w(padid
->pszDriverAlias
));
167 padid
->aFormatTag
[ntag
].dwFormatTag
= aftd
.dwFormatTag
;
168 padid
->aFormatTag
[ntag
].cbwfx
= aftd
.cbFormatSize
;
171 acmDriverClose(had
, 0);
176 if (had
) acmDriverClose(had
, 0);
177 HeapFree(MSACM_hHeap
, 0, padid
->aFormatTag
);
178 padid
->aFormatTag
= NULL
;
182 /***********************************************************************
183 * MSACM_GetRegistryKey
185 static LPWSTR
MSACM_GetRegistryKey(const WINE_ACMDRIVERID
* padid
)
187 static const WCHAR baseKey
[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
188 'A','u','d','i','o','C','o','m','p','r','e','s','s','i','o','n','M','a','n','a','g','e','r','\\',
189 'D','r','i','v','e','r','C','a','c','h','e','\\','\0'};
193 if (!padid
->pszDriverAlias
) {
194 ERR("No alias needed for registry entry\n");
197 len
= lstrlenW(baseKey
);
198 ret
= HeapAlloc(MSACM_hHeap
, 0, (len
+ lstrlenW(padid
->pszDriverAlias
) + 1) * sizeof(WCHAR
));
199 if (!ret
) return NULL
;
201 lstrcpyW(ret
, baseKey
);
202 lstrcpyW(ret
+ len
, padid
->pszDriverAlias
);
203 CharLowerW(ret
+ len
);
207 /***********************************************************************
210 static BOOL
MSACM_ReadCache(PWINE_ACMDRIVERID padid
)
212 LPWSTR key
= MSACM_GetRegistryKey(padid
);
216 if (!key
) return FALSE
;
218 padid
->aFormatTag
= NULL
;
220 if (RegCreateKeyW(HKEY_LOCAL_MACHINE
, key
, &hKey
))
223 size
= sizeof(padid
->cFormatTags
);
224 if (RegQueryValueExA(hKey
, "cFormatTags", 0, &type
, (void*)&padid
->cFormatTags
, &size
))
226 size
= sizeof(padid
->cFilterTags
);
227 if (RegQueryValueExA(hKey
, "cFilterTags", 0, &type
, (void*)&padid
->cFilterTags
, &size
))
229 size
= sizeof(padid
->fdwSupport
);
230 if (RegQueryValueExA(hKey
, "fdwSupport", 0, &type
, (void*)&padid
->fdwSupport
, &size
))
233 if (padid
->cFormatTags
> 0) {
234 size
= padid
->cFormatTags
* sizeof(padid
->aFormatTag
[0]);
235 padid
->aFormatTag
= HeapAlloc(MSACM_hHeap
, HEAP_ZERO_MEMORY
, size
);
236 if (!padid
->aFormatTag
) goto errCleanUp
;
237 if (RegQueryValueExA(hKey
, "aFormatTagCache", 0, &type
, (void*)padid
->aFormatTag
, &size
))
240 HeapFree(MSACM_hHeap
, 0, key
);
244 HeapFree(MSACM_hHeap
, 0, key
);
245 HeapFree(MSACM_hHeap
, 0, padid
->aFormatTag
);
246 padid
->aFormatTag
= NULL
;
251 /***********************************************************************
254 static BOOL
MSACM_WriteCache(const WINE_ACMDRIVERID
*padid
)
256 LPWSTR key
= MSACM_GetRegistryKey(padid
);
259 if (!key
) return FALSE
;
261 if (RegCreateKeyW(HKEY_LOCAL_MACHINE
, key
, &hKey
))
264 if (RegSetValueExA(hKey
, "cFormatTags", 0, REG_DWORD
, (const void*)&padid
->cFormatTags
, sizeof(DWORD
)))
266 if (RegSetValueExA(hKey
, "cFilterTags", 0, REG_DWORD
, (const void*)&padid
->cFilterTags
, sizeof(DWORD
)))
268 if (RegSetValueExA(hKey
, "fdwSupport", 0, REG_DWORD
, (const void*)&padid
->fdwSupport
, sizeof(DWORD
)))
270 if (RegSetValueExA(hKey
, "aFormatTagCache", 0, REG_BINARY
,
271 (void*)padid
->aFormatTag
,
272 padid
->cFormatTags
* sizeof(padid
->aFormatTag
[0])))
274 HeapFree(MSACM_hHeap
, 0, key
);
278 HeapFree(MSACM_hHeap
, 0, key
);
282 /***********************************************************************
283 * MSACM_RegisterDriver()
285 PWINE_ACMDRIVERID
MSACM_RegisterDriver(LPCWSTR pszDriverAlias
, LPCWSTR pszFileName
,
286 PWINE_ACMLOCALDRIVER pLocalDriver
)
288 PWINE_ACMDRIVERID padid
;
290 TRACE("(%s, %s, %p)\n",
291 debugstr_w(pszDriverAlias
), debugstr_w(pszFileName
), pLocalDriver
);
293 padid
= HeapAlloc(MSACM_hHeap
, 0, sizeof(WINE_ACMDRIVERID
));
296 padid
->obj
.dwType
= WINE_ACMOBJ_DRIVERID
;
297 padid
->obj
.pACMDriverID
= padid
;
298 padid
->pszDriverAlias
= NULL
;
301 padid
->pszDriverAlias
= HeapAlloc( MSACM_hHeap
, 0, (lstrlenW(pszDriverAlias
)+1) * sizeof(WCHAR
) );
302 if (!padid
->pszDriverAlias
) {
303 HeapFree(MSACM_hHeap
, 0, padid
);
306 lstrcpyW( padid
->pszDriverAlias
, pszDriverAlias
);
308 padid
->pszFileName
= NULL
;
311 padid
->pszFileName
= HeapAlloc( MSACM_hHeap
, 0, (lstrlenW(pszFileName
)+1) * sizeof(WCHAR
) );
312 if (!padid
->pszFileName
) {
313 HeapFree(MSACM_hHeap
, 0, padid
->pszDriverAlias
);
314 HeapFree(MSACM_hHeap
, 0, padid
);
317 lstrcpyW( padid
->pszFileName
, pszFileName
);
319 padid
->pLocalDriver
= pLocalDriver
;
321 padid
->pACMDriverList
= NULL
;
324 padid
->pPrevACMDriverID
= NULL
;
325 padid
->pNextACMDriverID
= MSACM_pFirstACMDriverID
;
326 if (MSACM_pFirstACMDriverID
)
327 MSACM_pFirstACMDriverID
->pPrevACMDriverID
= padid
;
328 MSACM_pFirstACMDriverID
= padid
;
329 if (!MSACM_pLastACMDriverID
)
330 MSACM_pLastACMDriverID
= padid
;
332 padid
->pNextACMDriverID
= NULL
;
333 padid
->pPrevACMDriverID
= MSACM_pLastACMDriverID
;
334 if (MSACM_pLastACMDriverID
)
335 MSACM_pLastACMDriverID
->pNextACMDriverID
= padid
;
336 MSACM_pLastACMDriverID
= padid
;
337 if (!MSACM_pFirstACMDriverID
)
338 MSACM_pFirstACMDriverID
= padid
;
340 /* disable the driver if we cannot load the cache */
341 if ((!padid
->pszDriverAlias
|| !MSACM_ReadCache(padid
)) && !MSACM_FillCache(padid
)) {
342 WARN("Couldn't load cache for ACM driver (%s)\n", debugstr_w(pszFileName
));
343 MSACM_UnregisterDriver(padid
);
347 if (pLocalDriver
) padid
->fdwSupport
|= ACMDRIVERDETAILS_SUPPORTF_LOCAL
;
351 /***********************************************************************
352 * MSACM_RegisterAllDrivers()
354 void MSACM_RegisterAllDrivers(void)
356 static const WCHAR msacm32
[] = {'m','s','a','c','m','3','2','.','d','l','l','\0'};
357 static const WCHAR msacmW
[] = {'M','S','A','C','M','.'};
358 static const WCHAR drv32
[] = {'d','r','i','v','e','r','s','3','2','\0'};
359 static const WCHAR sys
[] = {'s','y','s','t','e','m','.','i','n','i','\0'};
360 static const WCHAR drvkey
[] = {'S','o','f','t','w','a','r','e','\\',
361 'M','i','c','r','o','s','o','f','t','\\',
362 'W','i','n','d','o','w','s',' ','N','T','\\',
363 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
364 'D','r','i','v','e','r','s','3','2','\0'};
365 DWORD i
, cnt
, bufLen
, lRet
, type
;
366 WCHAR buf
[2048], valname
[64], *name
, *s
;
370 /* FIXME: What if the user edits system.ini while the program is running?
371 * Does Windows handle that? */
372 if (MSACM_pFirstACMDriverID
) return;
374 lRet
= RegOpenKeyExW(HKEY_LOCAL_MACHINE
, drvkey
, 0, KEY_QUERY_VALUE
, &hKey
);
375 if (lRet
== ERROR_SUCCESS
) {
376 RegQueryInfoKeyW( hKey
, 0, 0, 0, &cnt
, 0, 0, 0, 0, 0, 0, 0);
377 for (i
= 0; i
< cnt
; i
++) {
378 bufLen
= ARRAY_SIZE(buf
);
379 lRet
= RegEnumKeyExW(hKey
, i
, buf
, &bufLen
, 0, 0, 0, &lastWrite
);
380 if (lRet
!= ERROR_SUCCESS
) continue;
381 if (_wcsnicmp(buf
, msacmW
, ARRAY_SIZE(msacmW
))) continue;
382 if (!(name
= wcschr(buf
, '='))) continue;
384 MSACM_RegisterDriver(buf
, name
+ 1, 0);
387 cnt
= ARRAY_SIZE(valname
);
388 bufLen
= sizeof(buf
);
389 while(RegEnumValueW(hKey
, i
, valname
, &cnt
, 0,
390 &type
, (BYTE
*)buf
, &bufLen
) == ERROR_SUCCESS
){
391 if (!_wcsnicmp(valname
, msacmW
, ARRAY_SIZE(msacmW
)))
392 MSACM_RegisterDriver(valname
, buf
, 0);
398 if (GetPrivateProfileSectionW(drv32
, buf
, ARRAY_SIZE(buf
), sys
))
400 for(s
= buf
; *s
; s
+= lstrlenW(s
) + 1)
402 if (_wcsnicmp(s
, msacmW
, ARRAY_SIZE(msacmW
))) continue;
403 if (!(name
= wcschr(s
, '='))) continue;
405 MSACM_RegisterDriver(s
, name
+ 1, 0);
409 MSACM_ReorderDriversByPriority();
410 MSACM_RegisterDriver(msacm32
, msacm32
, 0);
413 /***********************************************************************
414 * MSACM_RegisterNotificationWindow()
416 PWINE_ACMNOTIFYWND
MSACM_RegisterNotificationWindow(HWND hNotifyWnd
, DWORD dwNotifyMsg
)
418 PWINE_ACMNOTIFYWND panwnd
;
420 TRACE("(%p,0x%08x)\n", hNotifyWnd
, dwNotifyMsg
);
422 panwnd
= HeapAlloc(MSACM_hHeap
, 0, sizeof(WINE_ACMNOTIFYWND
));
423 panwnd
->obj
.dwType
= WINE_ACMOBJ_NOTIFYWND
;
424 panwnd
->obj
.pACMDriverID
= 0;
425 panwnd
->hNotifyWnd
= hNotifyWnd
;
426 panwnd
->dwNotifyMsg
= dwNotifyMsg
;
427 panwnd
->fdwSupport
= 0;
429 panwnd
->pNextACMNotifyWnd
= NULL
;
430 panwnd
->pPrevACMNotifyWnd
= MSACM_pLastACMNotifyWnd
;
431 if (MSACM_pLastACMNotifyWnd
)
432 MSACM_pLastACMNotifyWnd
->pNextACMNotifyWnd
= panwnd
;
433 MSACM_pLastACMNotifyWnd
= panwnd
;
434 if (!MSACM_pFirstACMNotifyWnd
)
435 MSACM_pFirstACMNotifyWnd
= panwnd
;
440 /***********************************************************************
441 * MSACM_BroadcastNotification()
443 void MSACM_BroadcastNotification(void)
445 if (MSACM_suspendBroadcastCount
<= 0) {
446 PWINE_ACMNOTIFYWND panwnd
;
448 for (panwnd
= MSACM_pFirstACMNotifyWnd
; panwnd
; panwnd
= panwnd
->pNextACMNotifyWnd
)
449 if (!(panwnd
->fdwSupport
& ACMDRIVERDETAILS_SUPPORTF_DISABLED
))
450 SendMessageW(panwnd
->hNotifyWnd
, panwnd
->dwNotifyMsg
, 0, 0);
452 MSACM_pendingBroadcast
= TRUE
;
456 /***********************************************************************
457 * MSACM_DisableNotifications()
459 void MSACM_DisableNotifications(void)
461 MSACM_suspendBroadcastCount
++;
464 /***********************************************************************
465 * MSACM_EnableNotifications()
467 void MSACM_EnableNotifications(void)
469 if (MSACM_suspendBroadcastCount
> 0) {
470 MSACM_suspendBroadcastCount
--;
471 if (MSACM_suspendBroadcastCount
== 0 && MSACM_pendingBroadcast
) {
472 MSACM_pendingBroadcast
= FALSE
;
473 MSACM_BroadcastNotification();
478 /***********************************************************************
479 * MSACM_UnRegisterNotificationWindow()
481 PWINE_ACMNOTIFYWND
MSACM_UnRegisterNotificationWindow(const WINE_ACMNOTIFYWND
*panwnd
)
483 PWINE_ACMNOTIFYWND p
;
485 for (p
= MSACM_pFirstACMNotifyWnd
; p
; p
= p
->pNextACMNotifyWnd
) {
487 PWINE_ACMNOTIFYWND pNext
= p
->pNextACMNotifyWnd
;
489 if (p
->pPrevACMNotifyWnd
) p
->pPrevACMNotifyWnd
->pNextACMNotifyWnd
= p
->pNextACMNotifyWnd
;
490 if (p
->pNextACMNotifyWnd
) p
->pNextACMNotifyWnd
->pPrevACMNotifyWnd
= p
->pPrevACMNotifyWnd
;
491 if (MSACM_pFirstACMNotifyWnd
== p
) MSACM_pFirstACMNotifyWnd
= p
->pNextACMNotifyWnd
;
492 if (MSACM_pLastACMNotifyWnd
== p
) MSACM_pLastACMNotifyWnd
= p
->pPrevACMNotifyWnd
;
493 HeapFree(MSACM_hHeap
, 0, p
);
501 /***********************************************************************
502 * MSACM_RePositionDriver()
504 void MSACM_RePositionDriver(PWINE_ACMDRIVERID padid
, DWORD dwPriority
)
506 PWINE_ACMDRIVERID pTargetPosition
= NULL
;
508 /* Remove selected driver from linked list */
509 if (MSACM_pFirstACMDriverID
== padid
) {
510 MSACM_pFirstACMDriverID
= padid
->pNextACMDriverID
;
512 if (MSACM_pLastACMDriverID
== padid
) {
513 MSACM_pLastACMDriverID
= padid
->pPrevACMDriverID
;
515 if (padid
->pPrevACMDriverID
!= NULL
) {
516 padid
->pPrevACMDriverID
->pNextACMDriverID
= padid
->pNextACMDriverID
;
518 if (padid
->pNextACMDriverID
!= NULL
) {
519 padid
->pNextACMDriverID
->pPrevACMDriverID
= padid
->pPrevACMDriverID
;
522 /* Look up position where selected driver should be */
523 if (dwPriority
== 1) {
524 pTargetPosition
= padid
->pPrevACMDriverID
;
525 while (pTargetPosition
->pPrevACMDriverID
!= NULL
&&
526 !(pTargetPosition
->pPrevACMDriverID
->fdwSupport
& ACMDRIVERDETAILS_SUPPORTF_LOCAL
)) {
527 pTargetPosition
= pTargetPosition
->pPrevACMDriverID
;
529 } else if (dwPriority
== -1) {
530 pTargetPosition
= padid
->pNextACMDriverID
;
531 while (pTargetPosition
->pNextACMDriverID
!= NULL
) {
532 pTargetPosition
= pTargetPosition
->pNextACMDriverID
;
536 /* Place selected driver in selected position */
537 padid
->pPrevACMDriverID
= pTargetPosition
->pPrevACMDriverID
;
538 padid
->pNextACMDriverID
= pTargetPosition
;
539 if (padid
->pPrevACMDriverID
!= NULL
) {
540 padid
->pPrevACMDriverID
->pNextACMDriverID
= padid
;
542 MSACM_pFirstACMDriverID
= padid
;
544 if (padid
->pNextACMDriverID
!= NULL
) {
545 padid
->pNextACMDriverID
->pPrevACMDriverID
= padid
;
547 MSACM_pLastACMDriverID
= padid
;
551 /***********************************************************************
552 * MSACM_ReorderDriversByPriority()
553 * Reorders all drivers based on the priority list indicated by the registry key:
554 * HKCU\\Software\\Microsoft\\Multimedia\\Audio Compression Manager\\Priority v4.00
556 static void MSACM_ReorderDriversByPriority(void)
558 PWINE_ACMDRIVERID padid
;
559 unsigned int iNumDrivers
;
560 PWINE_ACMDRIVERID
* driverList
= NULL
;
561 HKEY hPriorityKey
= NULL
;
565 /* Count drivers && alloc corresponding memory for list */
567 for (padid
= MSACM_pFirstACMDriverID
; padid
; padid
= padid
->pNextACMDriverID
) iNumDrivers
++;
571 static const WCHAR basePriorityKey
[] = {
572 'S','o','f','t','w','a','r','e','\\',
573 'M','i','c','r','o','s','o','f','t','\\',
574 'M','u','l','t','i','m','e','d','i','a','\\',
575 'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
576 'P','r','i','o','r','i','t','y',' ','v','4','.','0','0','\0'
582 driverList
= HeapAlloc(MSACM_hHeap
, 0, iNumDrivers
* sizeof(PWINE_ACMDRIVERID
));
585 ERR("out of memory\n");
589 lError
= RegOpenKeyW(HKEY_CURRENT_USER
, basePriorityKey
, &hPriorityKey
);
590 if (lError
!= ERROR_SUCCESS
) {
591 TRACE("RegOpenKeyW failed, possibly key does not exist yet\n");
596 /* Copy drivers into list to simplify linked list modification */
597 for (i
= 0, padid
= MSACM_pFirstACMDriverID
; padid
; padid
= padid
->pNextACMDriverID
, i
++)
599 driverList
[i
] = padid
;
602 /* Query each of the priorities in turn. Alias key is in lowercase.
603 The general form of the priority record is the following:
604 "PriorityN" --> "1, msacm.driveralias"
605 where N is an integer, and the value is a string of the driver
606 alias, prefixed by "1, " for an enabled driver, or "0, " for a
609 for (i
= 0; i
< iNumDrivers
; i
++)
611 static const WCHAR priorityTmpl
[] = {'P','r','i','o','r','i','t','y','%','l','d','\0'};
613 unsigned int iTargetPosition
;
614 unsigned int iCurrentPosition
;
616 static const WCHAR sPrefix
[] = {'m','s','a','c','m','.','\0'};
618 /* Build expected entry name */
619 swprintf(szSubKey
, priorityTmpl
, i
+ 1);
620 lBufferLength
= sizeof(szBuffer
);
621 lError
= RegQueryValueExW(hPriorityKey
, szSubKey
, NULL
, NULL
, (LPBYTE
)szBuffer
, (LPDWORD
)&lBufferLength
);
622 if (lError
!= ERROR_SUCCESS
) continue;
624 /* Recovered driver alias should be at this position */
627 /* Locate driver alias in driver list */
628 pAlias
= wcsstr(szBuffer
, sPrefix
);
629 if (pAlias
== NULL
) continue;
631 for (iCurrentPosition
= 0; iCurrentPosition
< iNumDrivers
; iCurrentPosition
++) {
632 if (wcsicmp(driverList
[iCurrentPosition
]->pszDriverAlias
, pAlias
) == 0)
635 if (iCurrentPosition
< iNumDrivers
&& iTargetPosition
!= iCurrentPosition
) {
636 padid
= driverList
[iTargetPosition
];
637 driverList
[iTargetPosition
] = driverList
[iCurrentPosition
];
638 driverList
[iCurrentPosition
] = padid
;
640 /* Locate enabled status */
641 if (szBuffer
[0] == '1') {
642 driverList
[iTargetPosition
]->fdwSupport
&= ~ACMDRIVERDETAILS_SUPPORTF_DISABLED
;
643 } else if (szBuffer
[0] == '0') {
644 driverList
[iTargetPosition
]->fdwSupport
|= ACMDRIVERDETAILS_SUPPORTF_DISABLED
;
649 /* Re-assign pointers so that linked list traverses the ordered array */
650 for (i
= 0; i
< iNumDrivers
; i
++) {
651 driverList
[i
]->pPrevACMDriverID
= (i
> 0) ? driverList
[i
- 1] : NULL
;
652 driverList
[i
]->pNextACMDriverID
= (i
< iNumDrivers
- 1) ? driverList
[i
+ 1] : NULL
;
654 MSACM_pFirstACMDriverID
= driverList
[0];
655 MSACM_pLastACMDriverID
= driverList
[iNumDrivers
- 1];
659 if (hPriorityKey
!= NULL
) RegCloseKey(hPriorityKey
);
660 HeapFree(MSACM_hHeap
, 0, driverList
);
663 /***********************************************************************
664 * MSACM_WriteCurrentPriorities()
665 * Writes out current order of driver priorities to registry key:
666 * HKCU\\Software\\Microsoft\\Multimedia\\Audio Compression Manager\\Priority v4.00
668 void MSACM_WriteCurrentPriorities(void)
672 static const WCHAR basePriorityKey
[] = {
673 'S','o','f','t','w','a','r','e','\\',
674 'M','i','c','r','o','s','o','f','t','\\',
675 'M','u','l','t','i','m','e','d','i','a','\\',
676 'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
677 'P','r','i','o','r','i','t','y',' ','v','4','.','0','0','\0'
679 PWINE_ACMDRIVERID padid
;
680 DWORD dwPriorityCounter
;
681 static const WCHAR priorityTmpl
[] = {'P','r','i','o','r','i','t','y','%','l','d','\0'};
682 static const WCHAR valueTmpl
[] = {'%','c',',',' ','%','s','\0'};
683 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'};
687 /* Delete ACM priority key and create it anew */
688 lError
= RegDeleteKeyW(HKEY_CURRENT_USER
, basePriorityKey
);
689 if (lError
!= ERROR_SUCCESS
&& lError
!= ERROR_FILE_NOT_FOUND
) {
690 ERR("unable to remove current key %s (0x%08x) - priority changes won't persist past application end.\n",
691 debugstr_w(basePriorityKey
), lError
);
694 lError
= RegCreateKeyW(HKEY_CURRENT_USER
, basePriorityKey
, &hPriorityKey
);
695 if (lError
!= ERROR_SUCCESS
) {
696 ERR("unable to create key %s (0x%08x) - priority changes won't persist past application end.\n",
697 debugstr_w(basePriorityKey
), lError
);
701 /* Write current list of priorities */
702 for (dwPriorityCounter
= 0, padid
= MSACM_pFirstACMDriverID
; padid
; padid
= padid
->pNextACMDriverID
) {
703 if (padid
->fdwSupport
& ACMDRIVERDETAILS_SUPPORTF_LOCAL
) continue;
704 if (padid
->pszDriverAlias
== NULL
) continue; /* internal PCM converter is last */
706 /* Build required value name */
708 swprintf(szSubKey
, priorityTmpl
, dwPriorityCounter
);
710 /* Value has a 1 in front for enabled drivers and 0 for disabled drivers */
711 swprintf(szBuffer
, valueTmpl
, (padid
->fdwSupport
& ACMDRIVERDETAILS_SUPPORTF_DISABLED
) ? '0' : '1', padid
->pszDriverAlias
);
714 lError
= RegSetValueExW(hPriorityKey
, szSubKey
, 0, REG_SZ
, (BYTE
*)szBuffer
, (lstrlenW(szBuffer
) + 1) * sizeof(WCHAR
));
715 if (lError
!= ERROR_SUCCESS
) {
716 ERR("unable to write value for %s under key %s (0x%08x)\n",
717 debugstr_w(padid
->pszDriverAlias
), debugstr_w(basePriorityKey
), lError
);
721 /* Build required value name */
723 swprintf(szSubKey
, priorityTmpl
, dwPriorityCounter
);
725 /* Value has a 1 in front for enabled drivers and 0 for disabled drivers */
726 swprintf(szBuffer
, valueTmpl
, '1', converterAlias
);
728 lError
= RegSetValueExW(hPriorityKey
, szSubKey
, 0, REG_SZ
, (BYTE
*)szBuffer
, (lstrlenW(szBuffer
) + 1) * sizeof(WCHAR
));
729 if (lError
!= ERROR_SUCCESS
) {
730 ERR("unable to write value for %s under key %s (0x%08x)\n",
731 debugstr_w(converterAlias
), debugstr_w(basePriorityKey
), lError
);
733 RegCloseKey(hPriorityKey
);
736 static PWINE_ACMLOCALDRIVER MSACM_pFirstACMLocalDriver
;
737 static PWINE_ACMLOCALDRIVER MSACM_pLastACMLocalDriver
;
739 static PWINE_ACMLOCALDRIVER
MSACM_UnregisterLocalDriver(PWINE_ACMLOCALDRIVER paldrv
)
741 PWINE_ACMLOCALDRIVER pNextACMLocalDriver
;
744 if (paldrv
->pACMInstList
) {
745 ERR("local driver instances still present after closing all drivers - memory leak\n");
749 ref
= InterlockedDecrement(&paldrv
->ref
);
753 if (paldrv
== MSACM_pFirstACMLocalDriver
)
754 MSACM_pFirstACMLocalDriver
= paldrv
->pNextACMLocalDrv
;
755 if (paldrv
== MSACM_pLastACMLocalDriver
)
756 MSACM_pLastACMLocalDriver
= paldrv
->pPrevACMLocalDrv
;
758 if (paldrv
->pPrevACMLocalDrv
)
759 paldrv
->pPrevACMLocalDrv
->pNextACMLocalDrv
= paldrv
->pNextACMLocalDrv
;
760 if (paldrv
->pNextACMLocalDrv
)
761 paldrv
->pNextACMLocalDrv
->pPrevACMLocalDrv
= paldrv
->pPrevACMLocalDrv
;
763 pNextACMLocalDriver
= paldrv
->pNextACMLocalDrv
;
765 HeapFree(MSACM_hHeap
, 0, paldrv
);
767 return pNextACMLocalDriver
;
770 /***********************************************************************
771 * MSACM_UnregisterDriver()
773 PWINE_ACMDRIVERID
MSACM_UnregisterDriver(PWINE_ACMDRIVERID p
)
775 PWINE_ACMDRIVERID pNextACMDriverID
;
777 while (p
->pACMDriverList
)
778 acmDriverClose((HACMDRIVER
) p
->pACMDriverList
, 0);
780 HeapFree(MSACM_hHeap
, 0, p
->pszDriverAlias
);
781 HeapFree(MSACM_hHeap
, 0, p
->pszFileName
);
782 HeapFree(MSACM_hHeap
, 0, p
->aFormatTag
);
784 if (p
== MSACM_pFirstACMDriverID
)
785 MSACM_pFirstACMDriverID
= p
->pNextACMDriverID
;
786 if (p
== MSACM_pLastACMDriverID
)
787 MSACM_pLastACMDriverID
= p
->pPrevACMDriverID
;
789 if (p
->pPrevACMDriverID
)
790 p
->pPrevACMDriverID
->pNextACMDriverID
= p
->pNextACMDriverID
;
791 if (p
->pNextACMDriverID
)
792 p
->pNextACMDriverID
->pPrevACMDriverID
= p
->pPrevACMDriverID
;
794 pNextACMDriverID
= p
->pNextACMDriverID
;
796 if (p
->pLocalDriver
) MSACM_UnregisterLocalDriver(p
->pLocalDriver
);
797 HeapFree(MSACM_hHeap
, 0, p
);
799 return pNextACMDriverID
;
802 /***********************************************************************
803 * MSACM_UnregisterAllDrivers()
805 void MSACM_UnregisterAllDrivers(void)
807 PWINE_ACMNOTIFYWND panwnd
= MSACM_pFirstACMNotifyWnd
;
808 PWINE_ACMDRIVERID p
= MSACM_pFirstACMDriverID
;
812 p
= MSACM_UnregisterDriver(p
);
816 panwnd
= MSACM_UnRegisterNotificationWindow(panwnd
);
820 /***********************************************************************
823 PWINE_ACMOBJ
MSACM_GetObj(HACMOBJ hObj
, DWORD type
)
825 PWINE_ACMOBJ pao
= (PWINE_ACMOBJ
)hObj
;
827 if (pao
== NULL
|| IsBadReadPtr(pao
, sizeof(WINE_ACMOBJ
)) ||
828 ((type
!= WINE_ACMOBJ_DONTCARE
) && (type
!= pao
->dwType
)))
833 /***********************************************************************
834 * MSACM_GetDriverID()
836 PWINE_ACMDRIVERID
MSACM_GetDriverID(HACMDRIVERID hDriverID
)
838 return (PWINE_ACMDRIVERID
)MSACM_GetObj((HACMOBJ
)hDriverID
, WINE_ACMOBJ_DRIVERID
);
841 /***********************************************************************
844 PWINE_ACMDRIVER
MSACM_GetDriver(HACMDRIVER hDriver
)
846 return (PWINE_ACMDRIVER
)MSACM_GetObj((HACMOBJ
)hDriver
, WINE_ACMOBJ_DRIVER
);
849 /***********************************************************************
850 * MSACM_GetNotifyWnd()
852 PWINE_ACMNOTIFYWND
MSACM_GetNotifyWnd(HACMDRIVERID hDriver
)
854 return (PWINE_ACMNOTIFYWND
)MSACM_GetObj((HACMOBJ
)hDriver
, WINE_ACMOBJ_NOTIFYWND
);
857 /***********************************************************************
858 * MSACM_GetLocalDriver()
861 PWINE_ACMLOCALDRIVER MSACM_GetLocalDriver(HACMDRIVER hDriver)
863 return (PWINE_ACMLOCALDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_LOCALDRIVER);
866 #define MSACM_DRIVER_SendMessage(PDRVRINST, msg, lParam1, lParam2) \
867 (PDRVRINST)->pLocalDriver->lpDrvProc((PDRVRINST)->dwDriverID, (HDRVR)(PDRVRINST), msg, lParam1, lParam2)
869 /***********************************************************************
872 MMRESULT
MSACM_Message(HACMDRIVER had
, UINT uMsg
, LPARAM lParam1
, LPARAM lParam2
)
874 PWINE_ACMDRIVER pad
= MSACM_GetDriver(had
);
876 if (!pad
) return MMSYSERR_INVALHANDLE
;
877 if (pad
->hDrvr
) return SendDriverMessage(pad
->hDrvr
, uMsg
, lParam1
, lParam2
);
878 if (pad
->pLocalDrvrInst
) return MSACM_DRIVER_SendMessage(pad
->pLocalDrvrInst
, uMsg
, lParam1
, lParam2
);
880 return MMSYSERR_INVALHANDLE
;
883 PWINE_ACMLOCALDRIVER
MSACM_RegisterLocalDriver(HMODULE hModule
, DRIVERPROC lpDriverProc
)
885 PWINE_ACMLOCALDRIVER paldrv
;
887 TRACE("(%p, %p)\n", hModule
, lpDriverProc
);
888 if (!hModule
|| !lpDriverProc
) return NULL
;
890 /* look up previous instance of local driver module */
891 for (paldrv
= MSACM_pFirstACMLocalDriver
; paldrv
; paldrv
= paldrv
->pNextACMLocalDrv
)
893 if (paldrv
->hModule
== hModule
&& paldrv
->lpDrvProc
== lpDriverProc
)
895 InterlockedIncrement(&paldrv
->ref
);
900 paldrv
= HeapAlloc(MSACM_hHeap
, 0, sizeof(WINE_ACMLOCALDRIVER
));
901 paldrv
->obj
.dwType
= WINE_ACMOBJ_LOCALDRIVER
;
902 paldrv
->obj
.pACMDriverID
= 0;
903 paldrv
->hModule
= hModule
;
904 paldrv
->lpDrvProc
= lpDriverProc
;
905 paldrv
->pACMInstList
= NULL
;
908 paldrv
->pNextACMLocalDrv
= NULL
;
909 paldrv
->pPrevACMLocalDrv
= MSACM_pLastACMLocalDriver
;
910 if (MSACM_pLastACMLocalDriver
)
911 MSACM_pLastACMLocalDriver
->pNextACMLocalDrv
= paldrv
;
912 MSACM_pLastACMLocalDriver
= paldrv
;
913 if (!MSACM_pFirstACMLocalDriver
)
914 MSACM_pFirstACMLocalDriver
= paldrv
;
919 /**************************************************************************
920 * MSACM_GetNumberOfModuleRefs [internal]
922 * Returns the number of open drivers which share the same module.
923 * Inspired from implementation in dlls/winmm/driver.c
925 static unsigned MSACM_GetNumberOfModuleRefs(HMODULE hModule
, DRIVERPROC lpDrvProc
, WINE_ACMLOCALDRIVERINST
** found
)
927 PWINE_ACMLOCALDRIVER lpDrv
;
930 if (found
) *found
= NULL
;
931 for (lpDrv
= MSACM_pFirstACMLocalDriver
; lpDrv
; lpDrv
= lpDrv
->pNextACMLocalDrv
)
933 if (lpDrv
->hModule
== hModule
&& lpDrv
->lpDrvProc
== lpDrvProc
)
935 PWINE_ACMLOCALDRIVERINST pInst
= lpDrv
->pACMInstList
;
938 if (found
&& !*found
) *found
= pInst
;
940 pInst
= pInst
->pNextACMInst
;
947 /**************************************************************************
948 * MSACM_RemoveFromList [internal]
950 * Generates all the logic to handle driver closure / deletion
951 * Removes a driver struct to the list of open drivers.
953 static BOOL
MSACM_RemoveFromList(PWINE_ACMLOCALDRIVERINST lpDrv
)
955 PWINE_ACMLOCALDRIVER pDriverBase
= lpDrv
->pLocalDriver
;
956 PWINE_ACMLOCALDRIVERINST pPrevInst
;
958 /* last of this driver in list ? */
959 if (MSACM_GetNumberOfModuleRefs(pDriverBase
->hModule
, pDriverBase
->lpDrvProc
, NULL
) == 1) {
960 MSACM_DRIVER_SendMessage(lpDrv
, DRV_DISABLE
, 0L, 0L);
961 MSACM_DRIVER_SendMessage(lpDrv
, DRV_FREE
, 0L, 0L);
965 if (pDriverBase
->pACMInstList
!= lpDrv
) {
966 pPrevInst
= pDriverBase
->pACMInstList
;
967 while (pPrevInst
&& pPrevInst
->pNextACMInst
!= lpDrv
)
968 pPrevInst
= pPrevInst
->pNextACMInst
;
970 ERR("requested to remove invalid instance %p\n", pPrevInst
);
975 /* first driver instance on list */
976 pDriverBase
->pACMInstList
= lpDrv
->pNextACMInst
;
978 pPrevInst
->pNextACMInst
= lpDrv
->pNextACMInst
;
983 /**************************************************************************
984 * MSACM_AddToList [internal]
986 * Adds a driver struct to the list of open drivers.
987 * Generates all the logic to handle driver creation / open.
989 static BOOL
MSACM_AddToList(PWINE_ACMLOCALDRIVERINST lpNewDrv
, LPARAM lParam2
)
991 PWINE_ACMLOCALDRIVER pDriverBase
= lpNewDrv
->pLocalDriver
;
993 /* first of this driver in list ? */
994 if (MSACM_GetNumberOfModuleRefs(pDriverBase
->hModule
, pDriverBase
->lpDrvProc
, NULL
) == 0) {
995 if (MSACM_DRIVER_SendMessage(lpNewDrv
, DRV_LOAD
, 0L, 0L) != DRV_SUCCESS
) {
996 FIXME("DRV_LOAD failed on driver %p\n", lpNewDrv
);
999 /* returned value is not checked */
1000 MSACM_DRIVER_SendMessage(lpNewDrv
, DRV_ENABLE
, 0L, 0L);
1003 lpNewDrv
->pNextACMInst
= NULL
;
1004 if (pDriverBase
->pACMInstList
== NULL
) {
1005 pDriverBase
->pACMInstList
= lpNewDrv
;
1007 PWINE_ACMLOCALDRIVERINST lpDrvInst
= pDriverBase
->pACMInstList
;
1009 while (lpDrvInst
->pNextACMInst
!= NULL
)
1010 lpDrvInst
= lpDrvInst
->pNextACMInst
;
1012 lpDrvInst
->pNextACMInst
= lpNewDrv
;
1015 /* Now just open a new instance of a driver on this module */
1016 lpNewDrv
->dwDriverID
= MSACM_DRIVER_SendMessage(lpNewDrv
, DRV_OPEN
, 0, lParam2
);
1018 if (lpNewDrv
->dwDriverID
== 0) {
1019 FIXME("DRV_OPEN failed on driver %p\n", lpNewDrv
);
1020 MSACM_RemoveFromList(lpNewDrv
);
1026 PWINE_ACMLOCALDRIVERINST
MSACM_OpenLocalDriver(PWINE_ACMLOCALDRIVER paldrv
, LPARAM lParam2
)
1028 PWINE_ACMLOCALDRIVERINST pDrvInst
;
1030 pDrvInst
= HeapAlloc(MSACM_hHeap
, 0, sizeof(WINE_ACMLOCALDRIVERINST
));
1034 pDrvInst
->pLocalDriver
= paldrv
;
1035 pDrvInst
->dwDriverID
= 0;
1036 pDrvInst
->pNextACMInst
= NULL
;
1037 pDrvInst
->bSession
= FALSE
;
1039 /* Win32 installable drivers must support a two phase opening scheme:
1040 * + first open with NULL as lParam2 (session instance),
1041 * + then do a second open with the real non null lParam2)
1043 if (MSACM_GetNumberOfModuleRefs(paldrv
->hModule
, paldrv
->lpDrvProc
, NULL
) == 0 && lParam2
)
1045 PWINE_ACMLOCALDRIVERINST ret
;
1047 if (!MSACM_AddToList(pDrvInst
, 0L))
1049 ERR("load0 failed\n");
1052 ret
= MSACM_OpenLocalDriver(paldrv
, lParam2
);
1055 ERR("load1 failed\n");
1056 /* If MSACM_CloseLocalDriver returns TRUE,
1057 * then pDrvInst has been freed
1059 if (!MSACM_CloseLocalDriver(pDrvInst
))
1064 pDrvInst
->bSession
= TRUE
;
1068 if (!MSACM_AddToList(pDrvInst
, lParam2
))
1070 ERR("load failed\n");
1074 TRACE("=> %p\n", pDrvInst
);
1077 HeapFree(MSACM_hHeap
, 0, pDrvInst
);
1081 LRESULT
MSACM_CloseLocalDriver(PWINE_ACMLOCALDRIVERINST paldrv
)
1083 if (MSACM_RemoveFromList(paldrv
)) {
1084 PWINE_ACMLOCALDRIVERINST lpDrv0
;
1085 PWINE_ACMLOCALDRIVER pDriverBase
= paldrv
->pLocalDriver
;
1087 MSACM_DRIVER_SendMessage(paldrv
, DRV_CLOSE
, 0, 0);
1088 paldrv
->dwDriverID
= 0;
1090 if (paldrv
->bSession
)
1091 ERR("should not directly close session instance (%p)\n", paldrv
);
1093 /* if driver has an opened session instance, we have to close it too */
1094 if (MSACM_GetNumberOfModuleRefs(pDriverBase
->hModule
, pDriverBase
->lpDrvProc
, &lpDrv0
) == 1 &&
1097 MSACM_DRIVER_SendMessage(lpDrv0
, DRV_CLOSE
, 0L, 0L);
1098 lpDrv0
->dwDriverID
= 0;
1099 MSACM_RemoveFromList(lpDrv0
);
1100 HeapFree(MSACM_hHeap
, 0, lpDrv0
);
1103 HeapFree(MSACM_hHeap
, 0, paldrv
);
1106 ERR("unable to close driver instance\n");