msacm: acmDriverAdd[AW] support for ACM_DRIVERADDF_NOTIFYHWND.
[wine] / dlls / msacm / internal.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  *      MSACM32 library
5  *
6  *      Copyright 1998  Patrik Stridvall
7  *                1999  Eric Pouech
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include <stdarg.h>
25 #include <string.h>
26
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"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msacm);
42
43 /**********************************************************************/
44
45 HANDLE MSACM_hHeap = NULL;
46 PWINE_ACMDRIVERID MSACM_pFirstACMDriverID = NULL;
47 PWINE_ACMDRIVERID MSACM_pLastACMDriverID = NULL;
48
49 static DWORD MSACM_suspendBroadcastCount = 0;
50 static BOOL MSACM_pendingBroadcast = FALSE;
51 static PWINE_ACMNOTIFYWND MSACM_pFirstACMNotifyWnd = NULL;
52 static PWINE_ACMNOTIFYWND MSACM_pLastACMNotifyWnd = NULL;
53
54 static void MSACM_ReorderDriversByPriority(void);
55
56 /***********************************************************************
57  *           MSACM_RegisterDriverFromRegistry()
58  */
59 PWINE_ACMDRIVERID MSACM_RegisterDriverFromRegistry(LPCWSTR pszRegEntry)
60 {
61     static const WCHAR msacmW[] = {'M','S','A','C','M','.'};
62     static const WCHAR drvkey[] = {'S','o','f','t','w','a','r','e','\\',
63                                    'M','i','c','r','o','s','o','f','t','\\',
64                                    'W','i','n','d','o','w','s',' ','N','T','\\',
65                                    'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
66                                    'D','r','i','v','e','r','s','3','2','\0'};
67     WCHAR buf[2048];
68     DWORD bufLen, lRet;
69     HKEY hKey;
70     PWINE_ACMDRIVERID padid = NULL;
71     
72     /* The requested registry entry must have the format msacm.XXXXX in order to
73        be recognized in any future sessions of msacm
74      */
75     if (0 == strncmpiW(buf, msacmW, sizeof(msacmW)/sizeof(WCHAR))) {
76         lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, drvkey, 0, KEY_QUERY_VALUE, &hKey);
77         if (lRet != ERROR_SUCCESS) {
78             WARN("unable to open registry key - 0x%08lx\n", lRet);
79         } else {
80             bufLen = sizeof(buf);
81             lRet = RegQueryValueExW(hKey, pszRegEntry, NULL, NULL, (LPBYTE)buf, &bufLen);
82             if (lRet != ERROR_SUCCESS) {
83                 WARN("unable to query requested subkey %s - 0x%08lx\n", debugstr_w(pszRegEntry), lRet);
84             } else {
85                 MSACM_RegisterDriver(pszRegEntry, buf, 0);
86             }
87             RegCloseKey( hKey );
88         }
89     }
90     return padid;
91 }
92
93 #if 0
94 /***********************************************************************
95  *           MSACM_DumpCache
96  */
97 static  void MSACM_DumpCache(PWINE_ACMDRIVERID padid)
98 {
99     unsigned    i;
100
101     TRACE("cFilterTags=%lu cFormatTags=%lu fdwSupport=%08lx\n",
102           padid->cFilterTags, padid->cFormatTags, padid->fdwSupport);
103     for (i = 0; i < padid->cache->cFormatTags; i++) {
104         TRACE("\tdwFormatTag=%lu cbwfx=%lu\n",
105               padid->aFormatTag[i].dwFormatTag, padid->aFormatTag[i].cbwfx);
106     }
107 }
108 #endif
109
110 /***********************************************************************
111  *           MSACM_FindFormatTagInCache                 [internal]
112  *
113  *      Returns TRUE is the format tag fmtTag is present in the cache.
114  *      If so, idx is set to its index.
115  */
116 BOOL MSACM_FindFormatTagInCache(WINE_ACMDRIVERID* padid, DWORD fmtTag, LPDWORD idx)
117 {
118     unsigned    i;
119
120     for (i = 0; i < padid->cFormatTags; i++) {
121         if (padid->aFormatTag[i].dwFormatTag == fmtTag) {
122             if (idx) *idx = i;
123             return TRUE;
124         }
125     }
126     return FALSE;
127 }
128
129 /***********************************************************************
130  *           MSACM_FillCache
131  */
132 static BOOL MSACM_FillCache(PWINE_ACMDRIVERID padid)
133 {
134     HACMDRIVER                  had = 0;
135     unsigned int                        ntag;
136     ACMDRIVERDETAILSW           add;
137     ACMFORMATTAGDETAILSW        aftd;
138
139     if (acmDriverOpen(&had, (HACMDRIVERID)padid, 0) != 0)
140         return FALSE;
141
142     padid->aFormatTag = NULL;
143     add.cbStruct = sizeof(add);
144     if (MSACM_Message(had, ACMDM_DRIVER_DETAILS, (LPARAM)&add,  0))
145         goto errCleanUp;
146
147     if (add.cFormatTags > 0) {
148         padid->aFormatTag = HeapAlloc(MSACM_hHeap, HEAP_ZERO_MEMORY,
149                                       add.cFormatTags * sizeof(padid->aFormatTag[0]));
150         if (!padid->aFormatTag) goto errCleanUp;
151     }
152
153     padid->cFormatTags = add.cFormatTags;
154     padid->cFilterTags = add.cFilterTags;
155     padid->fdwSupport  = add.fdwSupport;
156
157     aftd.cbStruct = sizeof(aftd);
158
159     for (ntag = 0; ntag < add.cFormatTags; ntag++) {
160         aftd.dwFormatTagIndex = ntag;
161         if (MSACM_Message(had, ACMDM_FORMATTAG_DETAILS, (LPARAM)&aftd, ACM_FORMATTAGDETAILSF_INDEX)) {
162             TRACE("IIOs (%s)\n", debugstr_w(padid->pszDriverAlias));
163             goto errCleanUp;
164         }
165         padid->aFormatTag[ntag].dwFormatTag = aftd.dwFormatTag;
166         padid->aFormatTag[ntag].cbwfx = aftd.cbFormatSize;
167     }
168
169     acmDriverClose(had, 0);
170
171     return TRUE;
172
173 errCleanUp:
174     if (had) acmDriverClose(had, 0);
175     HeapFree(MSACM_hHeap, 0, padid->aFormatTag);
176     padid->aFormatTag = NULL;
177     return FALSE;
178 }
179
180 /***********************************************************************
181  *           MSACM_GetRegistryKey
182  */
183 static  LPWSTR  MSACM_GetRegistryKey(const WINE_ACMDRIVERID* padid)
184 {
185     static const WCHAR  baseKey[] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
186                                      'A','u','d','i','o','C','o','m','p','r','e','s','s','i','o','n','M','a','n','a','g','e','r','\\',
187                                      'D','r','i','v','e','r','C','a','c','h','e','\\','\0'};
188     LPWSTR      ret;
189     int         len;
190
191     if (!padid->pszDriverAlias) {
192         ERR("No alias needed for registry entry\n");
193         return NULL;
194     }
195     len = strlenW(baseKey);
196     ret = HeapAlloc(MSACM_hHeap, 0, (len + strlenW(padid->pszDriverAlias) + 1) * sizeof(WCHAR));
197     if (!ret) return NULL;
198
199     strcpyW(ret, baseKey);
200     strcpyW(ret + len, padid->pszDriverAlias);
201     CharLowerW(ret + len);
202     return ret;
203 }
204
205 /***********************************************************************
206  *           MSACM_ReadCache
207  */
208 static BOOL MSACM_ReadCache(PWINE_ACMDRIVERID padid)
209 {
210     LPWSTR      key = MSACM_GetRegistryKey(padid);
211     HKEY        hKey;
212     DWORD       type, size;
213
214     if (!key) return FALSE;
215
216     padid->aFormatTag = NULL;
217
218     if (RegCreateKeyW(HKEY_LOCAL_MACHINE, key, &hKey))
219         goto errCleanUp;
220
221     size = sizeof(padid->cFormatTags);
222     if (RegQueryValueExA(hKey, "cFormatTags", 0, &type, (void*)&padid->cFormatTags, &size))
223         goto errCleanUp;
224     size = sizeof(padid->cFilterTags);
225     if (RegQueryValueExA(hKey, "cFilterTags", 0, &type, (void*)&padid->cFilterTags, &size))
226         goto errCleanUp;
227     size = sizeof(padid->fdwSupport);
228     if (RegQueryValueExA(hKey, "fdwSupport", 0, &type, (void*)&padid->fdwSupport, &size))
229         goto errCleanUp;
230
231     if (padid->cFormatTags > 0) {
232         size = padid->cFormatTags * sizeof(padid->aFormatTag[0]);
233         padid->aFormatTag = HeapAlloc(MSACM_hHeap, HEAP_ZERO_MEMORY, size);
234         if (!padid->aFormatTag) goto errCleanUp;
235         if (RegQueryValueExA(hKey, "aFormatTagCache", 0, &type, (void*)padid->aFormatTag, &size))
236             goto errCleanUp;
237     }
238     HeapFree(MSACM_hHeap, 0, key);
239     return TRUE;
240
241  errCleanUp:
242     HeapFree(MSACM_hHeap, 0, key);
243     HeapFree(MSACM_hHeap, 0, padid->aFormatTag);
244     padid->aFormatTag = NULL;
245     RegCloseKey(hKey);
246     return FALSE;
247 }
248
249 /***********************************************************************
250  *           MSACM_WriteCache
251  */
252 static  BOOL MSACM_WriteCache(PWINE_ACMDRIVERID padid)
253 {
254     LPWSTR      key = MSACM_GetRegistryKey(padid);
255     HKEY        hKey;
256
257     if (!key) return FALSE;
258
259     if (RegCreateKeyW(HKEY_LOCAL_MACHINE, key, &hKey))
260         goto errCleanUp;
261
262     if (RegSetValueExA(hKey, "cFormatTags", 0, REG_DWORD, (void*)&padid->cFormatTags, sizeof(DWORD)))
263         goto errCleanUp;
264     if (RegSetValueExA(hKey, "cFilterTags", 0, REG_DWORD, (void*)&padid->cFilterTags, sizeof(DWORD)))
265         goto errCleanUp;
266     if (RegSetValueExA(hKey, "fdwSupport", 0, REG_DWORD, (void*)&padid->fdwSupport, sizeof(DWORD)))
267         goto errCleanUp;
268     if (RegSetValueExA(hKey, "aFormatTagCache", 0, REG_BINARY,
269                        (void*)padid->aFormatTag,
270                        padid->cFormatTags * sizeof(padid->aFormatTag[0])))
271         goto errCleanUp;
272     HeapFree(MSACM_hHeap, 0, key);
273     return TRUE;
274
275  errCleanUp:
276     HeapFree(MSACM_hHeap, 0, key);
277     return FALSE;
278 }
279
280 /***********************************************************************
281  *           MSACM_RegisterDriver()
282  */
283 PWINE_ACMDRIVERID MSACM_RegisterDriver(LPCWSTR pszDriverAlias, LPCWSTR pszFileName,
284                                        HINSTANCE hinstModule)
285 {
286     PWINE_ACMDRIVERID   padid;
287
288     TRACE("(%s, %s, %p)\n", 
289           debugstr_w(pszDriverAlias), debugstr_w(pszFileName), hinstModule);
290
291     padid = HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMDRIVERID));
292     padid->obj.dwType = WINE_ACMOBJ_DRIVERID;
293     padid->obj.pACMDriverID = padid;
294     padid->pszDriverAlias = NULL;
295     if (pszDriverAlias)
296     {
297         padid->pszDriverAlias = HeapAlloc( MSACM_hHeap, 0, (strlenW(pszDriverAlias)+1) * sizeof(WCHAR) );
298         strcpyW( padid->pszDriverAlias, pszDriverAlias );
299     }
300     padid->pszFileName = NULL;
301     if (pszFileName)
302     {
303         padid->pszFileName = HeapAlloc( MSACM_hHeap, 0, (strlenW(pszFileName)+1) * sizeof(WCHAR) );
304         strcpyW( padid->pszFileName, pszFileName );
305     }
306     padid->hInstModule = hinstModule;
307
308     padid->pACMDriverList = NULL;
309     padid->pNextACMDriverID = NULL;
310     padid->pPrevACMDriverID = MSACM_pLastACMDriverID;
311     if (MSACM_pLastACMDriverID)
312         MSACM_pLastACMDriverID->pNextACMDriverID = padid;
313     MSACM_pLastACMDriverID = padid;
314     if (!MSACM_pFirstACMDriverID)
315         MSACM_pFirstACMDriverID = padid;
316     /* disable the driver if we cannot load the cache */
317     if (!MSACM_ReadCache(padid) && !MSACM_FillCache(padid)) {
318         WARN("Couldn't load cache for ACM driver (%s)\n", debugstr_w(pszFileName));
319         MSACM_UnregisterDriver(padid);
320         return NULL;
321     }
322     return padid;
323 }
324
325 /***********************************************************************
326  *           MSACM_RegisterAllDrivers()
327  */
328 void MSACM_RegisterAllDrivers(void)
329 {
330     static const WCHAR msacm32[] = {'m','s','a','c','m','3','2','.','d','l','l','\0'};
331     static const WCHAR msacmW[] = {'M','S','A','C','M','.'};
332     static const WCHAR drv32[] = {'d','r','i','v','e','r','s','3','2','\0'};
333     static const WCHAR sys[] = {'s','y','s','t','e','m','.','i','n','i','\0'};
334     static const WCHAR drvkey[] = {'S','o','f','t','w','a','r','e','\\',
335                                    'M','i','c','r','o','s','o','f','t','\\',
336                                    'W','i','n','d','o','w','s',' ','N','T','\\',
337                                    'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
338                                    'D','r','i','v','e','r','s','3','2','\0'};
339     DWORD i, cnt = 0, bufLen, lRet;
340     WCHAR buf[2048], *name, *s;
341     FILETIME lastWrite;
342     HKEY hKey;
343
344     /* FIXME: What if the user edits system.ini while the program is running?
345      * Does Windows handle that?  */
346     if (MSACM_pFirstACMDriverID) return;
347
348     lRet = RegOpenKeyExW(HKEY_LOCAL_MACHINE, drvkey, 0, KEY_QUERY_VALUE, &hKey);
349     if (lRet == ERROR_SUCCESS) {
350         RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
351         for (i = 0; i < cnt; i++) {
352             bufLen = sizeof(buf) / sizeof(buf[0]);
353             lRet = RegEnumKeyExW(hKey, i, buf, &bufLen, 0, 0, 0, &lastWrite);
354             if (lRet != ERROR_SUCCESS) continue;
355             if (strncmpiW(buf, msacmW, sizeof(msacmW)/sizeof(msacmW[0]))) continue;
356             if (!(name = strchrW(buf, '='))) continue;
357             *name = 0;
358             MSACM_RegisterDriver(buf, name + 1, 0);
359         }
360         RegCloseKey( hKey );
361     }
362
363     if (GetPrivateProfileSectionW(drv32, buf, sizeof(buf)/sizeof(buf[0]), sys))
364     {
365         for(s = buf; *s;  s += strlenW(s) + 1)
366         {
367             if (strncmpiW(s, msacmW, sizeof(msacmW)/sizeof(msacmW[0]))) continue;
368             if (!(name = strchrW(s, '='))) continue;
369             *name = 0;
370             MSACM_RegisterDriver(s, name + 1, 0);
371             *name = '=';
372         }
373     }
374     MSACM_ReorderDriversByPriority();
375     MSACM_RegisterDriver(msacm32, msacm32, 0);
376 }
377
378 /***********************************************************************
379  *           MSACM_RegisterNotificationWindow()
380  */
381 PWINE_ACMNOTIFYWND MSACM_RegisterNotificationWindow(HWND hNotifyWnd, DWORD dwNotifyMsg)
382 {
383     PWINE_ACMNOTIFYWND  panwnd;
384
385     TRACE("(%p,0x%08lx)\n", hNotifyWnd, dwNotifyMsg);
386
387     panwnd = HeapAlloc(MSACM_hHeap, 0, sizeof(WINE_ACMNOTIFYWND));
388     panwnd->obj.dwType = WINE_ACMOBJ_NOTIFYWND;
389     panwnd->obj.pACMDriverID = 0;
390     panwnd->hNotifyWnd = hNotifyWnd;
391     panwnd->dwNotifyMsg = dwNotifyMsg;
392     panwnd->fdwSupport = 0;
393     
394     panwnd->pNextACMNotifyWnd = NULL;
395     panwnd->pPrevACMNotifyWnd = MSACM_pLastACMNotifyWnd;
396     if (MSACM_pLastACMNotifyWnd)
397         MSACM_pLastACMNotifyWnd->pNextACMNotifyWnd = panwnd;
398     MSACM_pLastACMNotifyWnd = panwnd;
399     if (!MSACM_pFirstACMNotifyWnd)
400         MSACM_pFirstACMNotifyWnd = panwnd;
401
402     return panwnd;
403 }
404
405 /***********************************************************************
406  *           MSACM_BroadcastNotification()
407  */
408 void MSACM_BroadcastNotification(void)
409 {
410     if (MSACM_suspendBroadcastCount <= 0) {
411         PWINE_ACMNOTIFYWND panwnd;
412
413         for (panwnd = MSACM_pFirstACMNotifyWnd; panwnd; panwnd = panwnd->pNextACMNotifyWnd) 
414         if (!(panwnd->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED))
415             SendMessageW(panwnd->hNotifyWnd, panwnd->dwNotifyMsg, 0, 0);
416     } else {
417         MSACM_pendingBroadcast = TRUE;
418     }
419 }
420
421 /***********************************************************************
422  *           MSACM_DisableNotifications()
423  */
424 void MSACM_DisableNotifications(void)
425 {
426     MSACM_suspendBroadcastCount++;
427 }
428
429 /***********************************************************************
430  *           MSACM_EnableNotifications()
431  */
432 void MSACM_EnableNotifications(void)
433 {
434     if (MSACM_suspendBroadcastCount > 0) {
435         MSACM_suspendBroadcastCount--;
436         if (MSACM_suspendBroadcastCount == 0 && MSACM_pendingBroadcast) {
437             MSACM_pendingBroadcast = FALSE;
438             MSACM_BroadcastNotification();
439         }
440     }
441 }
442
443 /***********************************************************************
444  *           MSACM_UnRegisterNotificationWindow()
445  */
446 PWINE_ACMNOTIFYWND MSACM_UnRegisterNotificationWindow(PWINE_ACMNOTIFYWND panwnd)
447 {
448     PWINE_ACMNOTIFYWND p;
449
450     for (p = MSACM_pFirstACMNotifyWnd; p; p = p->pNextACMNotifyWnd) {
451         if (p == panwnd) {
452             PWINE_ACMNOTIFYWND pNext = p->pNextACMNotifyWnd;
453
454             if (p->pPrevACMNotifyWnd) p->pPrevACMNotifyWnd->pNextACMNotifyWnd = p->pNextACMNotifyWnd;
455             if (p->pNextACMNotifyWnd) p->pNextACMNotifyWnd->pPrevACMNotifyWnd = p->pPrevACMNotifyWnd;
456             if (MSACM_pFirstACMNotifyWnd == p) MSACM_pFirstACMNotifyWnd = p->pNextACMNotifyWnd;
457             if (MSACM_pLastACMNotifyWnd == p) MSACM_pLastACMNotifyWnd = p->pPrevACMNotifyWnd;
458             HeapFree(MSACM_hHeap, 0, p);
459             
460             return pNext;
461         }
462     }
463     return NULL;
464 }
465
466 /***********************************************************************
467  *           MSACM_RePositionDriver()
468  */
469 void MSACM_RePositionDriver(PWINE_ACMDRIVERID padid, DWORD dwPriority)
470 {
471     PWINE_ACMDRIVERID pTargetPosition = NULL;
472                 
473     /* Remove selected driver from linked list */
474     if (MSACM_pFirstACMDriverID == padid) {
475         MSACM_pFirstACMDriverID = padid->pNextACMDriverID;
476     }
477     if (MSACM_pLastACMDriverID == padid) {
478         MSACM_pLastACMDriverID = padid->pPrevACMDriverID;
479     }
480     if (padid->pPrevACMDriverID != NULL) {
481         padid->pPrevACMDriverID->pNextACMDriverID = padid->pNextACMDriverID;
482     }
483     if (padid->pNextACMDriverID != NULL) {
484         padid->pNextACMDriverID->pPrevACMDriverID = padid->pPrevACMDriverID;
485     }
486     
487     /* Look up position where selected driver should be */
488     if (dwPriority == 1) {
489         pTargetPosition = padid->pPrevACMDriverID;
490         while (pTargetPosition->pPrevACMDriverID != NULL &&
491             !(pTargetPosition->pPrevACMDriverID->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_LOCAL)) {
492             pTargetPosition = pTargetPosition->pPrevACMDriverID;
493         }
494     } else if (dwPriority == -1) {
495         pTargetPosition = padid->pNextACMDriverID;
496         while (pTargetPosition->pNextACMDriverID != NULL) {
497             pTargetPosition = pTargetPosition->pNextACMDriverID;
498         }
499     }
500     
501     /* Place selected driver in selected position */
502     padid->pPrevACMDriverID = pTargetPosition->pPrevACMDriverID;
503     padid->pNextACMDriverID = pTargetPosition;
504     if (padid->pPrevACMDriverID != NULL) {
505         padid->pPrevACMDriverID->pNextACMDriverID = padid;
506     } else {
507         MSACM_pFirstACMDriverID = padid;
508     }
509     if (padid->pNextACMDriverID != NULL) {
510         padid->pNextACMDriverID->pPrevACMDriverID = padid;
511     } else {
512         MSACM_pLastACMDriverID = padid;
513     }
514 }
515
516 /***********************************************************************
517  *           MSACM_ReorderDriversByPriority()
518  * Reorders all drivers based on the priority list indicated by the registry key:
519  * HKCU\\Software\\Microsoft\\Multimedia\\Audio Compression Manager\\Priority v4.00
520  */
521 static void MSACM_ReorderDriversByPriority(void)
522 {
523     PWINE_ACMDRIVERID   padid;
524     unsigned int iNumDrivers;
525     PWINE_ACMDRIVERID * driverList = NULL;
526     HKEY hPriorityKey = NULL;
527     
528     TRACE("\n");
529     
530     /* Count drivers && alloc corresponding memory for list */
531     iNumDrivers = 0;
532     for (padid = MSACM_pFirstACMDriverID; padid; padid = padid->pNextACMDriverID) iNumDrivers++;
533     if (iNumDrivers > 1)
534     {
535         LONG lError;
536         static const WCHAR basePriorityKey[] = {
537             'S','o','f','t','w','a','r','e','\\',
538             'M','i','c','r','o','s','o','f','t','\\',
539             'M','u','l','t','i','m','e','d','i','a','\\',
540             'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
541             'P','r','i','o','r','i','t','y',' ','v','4','.','0','0','\0'
542         };
543         unsigned int i;
544         LONG lBufferLength;
545         WCHAR szBuffer[256];
546         
547         driverList = HeapAlloc(MSACM_hHeap, 0, iNumDrivers * sizeof(PWINE_ACMDRIVERID));
548         if (!driverList)
549         {
550             ERR("out of memory\n");
551             goto errCleanUp;
552         }
553
554         lError = RegOpenKeyW(HKEY_CURRENT_USER, basePriorityKey, &hPriorityKey);
555         if (lError != ERROR_SUCCESS) {
556             TRACE("RegOpenKeyW failed, possibly key does not exist yet\n");
557             hPriorityKey = NULL;
558             goto errCleanUp;
559         } 
560             
561         /* Copy drivers into list to simplify linked list modification */
562         for (i = 0, padid = MSACM_pFirstACMDriverID; padid; padid = padid->pNextACMDriverID, i++)
563         {
564             driverList[i] = padid;
565         }
566
567         /* Query each of the priorities in turn. Alias key is in lowercase. 
568             The general form of the priority record is the following:
569             "PriorityN" --> "1, msacm.driveralias"
570             where N is an integer, and the value is a string of the driver
571             alias, prefixed by "1, " for an enabled driver, or "0, " for a
572             disabled driver.
573             */
574         for (i = 0; i < iNumDrivers; i++)
575         {
576             static const WCHAR priorityTmpl[] = {'P','r','i','o','r','i','t','y','%','l','d','\0'};
577             WCHAR szSubKey[17];
578             unsigned int iTargetPosition;
579             unsigned int iCurrentPosition;
580             WCHAR * pAlias;
581             static const WCHAR sPrefix[] = {'m','s','a','c','m','.','\0'};
582             
583             /* Build expected entry name */
584             snprintfW(szSubKey, 17, priorityTmpl, i + 1);
585             lBufferLength = sizeof(szBuffer);
586             lError = RegQueryValueExW(hPriorityKey, szSubKey, NULL, NULL, (LPBYTE)szBuffer, (LPDWORD)&lBufferLength);
587             if (lError != ERROR_SUCCESS) continue;
588
589             /* Recovered driver alias should be at this position */
590             iTargetPosition = i;
591             
592             /* Locate driver alias in driver list */
593             pAlias = strstrW(szBuffer, sPrefix);
594             if (pAlias == NULL) continue;
595             
596             for (iCurrentPosition = 0; iCurrentPosition < iNumDrivers; iCurrentPosition++) {
597                 if (strcmpiW(driverList[iCurrentPosition]->pszDriverAlias, pAlias) == 0) 
598                     break;
599             }
600             if (iCurrentPosition < iNumDrivers && iTargetPosition != iCurrentPosition) {
601                 padid = driverList[iTargetPosition];
602                 driverList[iTargetPosition] = driverList[iCurrentPosition];
603                 driverList[iCurrentPosition] = padid;
604
605                 /* Locate enabled status */
606                 if (szBuffer[0] == '1') {
607                     driverList[iTargetPosition]->fdwSupport &= ~ACMDRIVERDETAILS_SUPPORTF_DISABLED;
608                 } else if (szBuffer[0] == '0') {
609                     driverList[iTargetPosition]->fdwSupport |= ACMDRIVERDETAILS_SUPPORTF_DISABLED;
610                 }
611             }
612         }
613         
614         /* Re-assign pointers so that linked list traverses the ordered array */
615         for (i = 0; i < iNumDrivers; i++) {
616             driverList[i]->pPrevACMDriverID = (i > 0) ? driverList[i - 1] : NULL;
617             driverList[i]->pNextACMDriverID = (i < iNumDrivers - 1) ? driverList[i + 1] : NULL;
618         }
619         MSACM_pFirstACMDriverID = driverList[0];
620         MSACM_pLastACMDriverID = driverList[iNumDrivers - 1];
621     }
622     
623 errCleanUp:
624     if (hPriorityKey != NULL) RegCloseKey(hPriorityKey);
625     if (driverList != NULL) HeapFree(MSACM_hHeap, 0, driverList);
626 }
627
628 /***********************************************************************
629  *           MSACM_WriteCurrentPriorities()
630  * Writes out current order of driver priorities to registry key:
631  * HKCU\\Software\\Microsoft\\Multimedia\\Audio Compression Manager\\Priority v4.00
632  */
633 void MSACM_WriteCurrentPriorities(void)
634 {
635     LONG lError;
636     HKEY hPriorityKey;
637     static const WCHAR basePriorityKey[] = {
638         'S','o','f','t','w','a','r','e','\\',
639         'M','i','c','r','o','s','o','f','t','\\',
640         'M','u','l','t','i','m','e','d','i','a','\\',
641         'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','i','o','n',' ','M','a','n','a','g','e','r','\\',
642         'P','r','i','o','r','i','t','y',' ','v','4','.','0','0','\0'
643     };
644     PWINE_ACMDRIVERID padid;
645     DWORD dwPriorityCounter;
646     static const WCHAR priorityTmpl[] = {'P','r','i','o','r','i','t','y','%','l','d','\0'};
647     static const WCHAR valueTmpl[] = {'%','c',',',' ','%','s','\0'};
648     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'};
649     WCHAR szSubKey[17];
650     WCHAR szBuffer[256];
651
652     /* Delete ACM priority key and create it anew */
653     lError = RegDeleteKeyW(HKEY_CURRENT_USER, basePriorityKey);
654     if (lError != ERROR_SUCCESS && lError != ERROR_FILE_NOT_FOUND) {
655         ERR("unable to remove current key %s (0x%08lx) - priority changes won't persist past application end.\n",
656             debugstr_w(basePriorityKey), lError);
657         return;
658     }
659     lError = RegCreateKeyW(HKEY_CURRENT_USER, basePriorityKey, &hPriorityKey);
660     if (lError != ERROR_SUCCESS) {
661         ERR("unable to create key %s (0x%08lx) - priority changes won't persist past application end.\n",
662             debugstr_w(basePriorityKey), lError);
663         return;
664     }
665     
666     /* Write current list of priorities */
667     for (dwPriorityCounter = 0, padid = MSACM_pFirstACMDriverID; padid; padid = padid->pNextACMDriverID) {        
668         if (padid->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_LOCAL) continue;
669         if (padid->pszDriverAlias == NULL) continue;    /* internal PCM converter is last */
670
671         /* Build required value name */
672         dwPriorityCounter++;
673         snprintfW(szSubKey, 17, priorityTmpl, dwPriorityCounter);
674         
675         /* Value has a 1 in front for enabled drivers and 0 for disabled drivers */
676         snprintfW(szBuffer, 256, valueTmpl, (padid->fdwSupport & ACMDRIVERDETAILS_SUPPORTF_DISABLED) ? '0' : '1', padid->pszDriverAlias);
677         strlwrW(szBuffer);
678         
679         lError = RegSetValueExW(hPriorityKey, szSubKey, 0, REG_SZ, (BYTE *)szBuffer, (strlenW(szBuffer) + 1) * sizeof(WCHAR));
680         if (lError != ERROR_SUCCESS) {
681             ERR("unable to write value for %s under key %s (0x%08lx)\n",
682                 debugstr_w(padid->pszDriverAlias), debugstr_w(basePriorityKey), lError);
683         }
684     }
685     
686     /* Build required value name */
687     dwPriorityCounter++;
688     snprintfW(szSubKey, 17, priorityTmpl, dwPriorityCounter);
689         
690     /* Value has a 1 in front for enabled drivers and 0 for disabled drivers */
691     snprintfW(szBuffer, 256, valueTmpl, '1', converterAlias);
692         
693     lError = RegSetValueExW(hPriorityKey, szSubKey, 0, REG_SZ, (BYTE *)szBuffer, (strlenW(szBuffer) + 1) * sizeof(WCHAR));
694     if (lError != ERROR_SUCCESS) {
695         ERR("unable to write value for %s under key %s (0x%08lx)\n",
696             debugstr_w(converterAlias), debugstr_w(basePriorityKey), lError);
697     }
698     RegCloseKey(hPriorityKey);
699 }
700
701 /***********************************************************************
702  *           MSACM_UnregisterDriver()
703  */
704 PWINE_ACMDRIVERID MSACM_UnregisterDriver(PWINE_ACMDRIVERID p)
705 {
706     PWINE_ACMDRIVERID pNextACMDriverID;
707
708     while (p->pACMDriverList)
709         acmDriverClose((HACMDRIVER) p->pACMDriverList, 0);
710
711     HeapFree(MSACM_hHeap, 0, p->pszDriverAlias);
712     HeapFree(MSACM_hHeap, 0, p->pszFileName);
713     HeapFree(MSACM_hHeap, 0, p->aFormatTag);
714
715     if (p == MSACM_pFirstACMDriverID)
716         MSACM_pFirstACMDriverID = p->pNextACMDriverID;
717     if (p == MSACM_pLastACMDriverID)
718         MSACM_pLastACMDriverID = p->pPrevACMDriverID;
719
720     if (p->pPrevACMDriverID)
721         p->pPrevACMDriverID->pNextACMDriverID = p->pNextACMDriverID;
722     if (p->pNextACMDriverID)
723         p->pNextACMDriverID->pPrevACMDriverID = p->pPrevACMDriverID;
724
725     pNextACMDriverID = p->pNextACMDriverID;
726
727     HeapFree(MSACM_hHeap, 0, p);
728
729     return pNextACMDriverID;
730 }
731
732 /***********************************************************************
733  *           MSACM_UnregisterAllDrivers()
734  */
735 void MSACM_UnregisterAllDrivers(void)
736 {
737     PWINE_ACMNOTIFYWND panwnd = MSACM_pFirstACMNotifyWnd;
738     PWINE_ACMDRIVERID p = MSACM_pFirstACMDriverID;
739
740     while (p) {
741         MSACM_WriteCache(p);
742         p = MSACM_UnregisterDriver(p);
743     }
744     
745     while (panwnd) {
746         panwnd = MSACM_UnRegisterNotificationWindow(panwnd);
747     }    
748 }
749
750 /***********************************************************************
751  *           MSACM_GetObj()
752  */
753 PWINE_ACMOBJ MSACM_GetObj(HACMOBJ hObj, DWORD type)
754 {
755     PWINE_ACMOBJ        pao = (PWINE_ACMOBJ)hObj;
756
757     if (pao == NULL || IsBadReadPtr(pao, sizeof(WINE_ACMOBJ)) ||
758         ((type != WINE_ACMOBJ_DONTCARE) && (type != pao->dwType)))
759         return NULL;
760     return pao;
761 }
762
763 /***********************************************************************
764  *           MSACM_GetDriverID()
765  */
766 PWINE_ACMDRIVERID MSACM_GetDriverID(HACMDRIVERID hDriverID)
767 {
768     return (PWINE_ACMDRIVERID)MSACM_GetObj((HACMOBJ)hDriverID, WINE_ACMOBJ_DRIVERID);
769 }
770
771 /***********************************************************************
772  *           MSACM_GetDriver()
773  */
774 PWINE_ACMDRIVER MSACM_GetDriver(HACMDRIVER hDriver)
775 {
776     return (PWINE_ACMDRIVER)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_DRIVER);
777 }
778
779 /***********************************************************************
780  *           MSACM_GetNotifyWnd()
781  */
782 PWINE_ACMNOTIFYWND MSACM_GetNotifyWnd(HACMDRIVERID hDriver)
783 {
784     return (PWINE_ACMNOTIFYWND)MSACM_GetObj((HACMOBJ)hDriver, WINE_ACMOBJ_NOTIFYWND);
785 }
786
787
788 /***********************************************************************
789  *           MSACM_Message()
790  */
791 MMRESULT MSACM_Message(HACMDRIVER had, UINT uMsg, LPARAM lParam1, LPARAM lParam2)
792 {
793     PWINE_ACMDRIVER     pad = MSACM_GetDriver(had);
794
795     return pad ? SendDriverMessage(pad->hDrvr, uMsg, lParam1, lParam2) : MMSYSERR_INVALHANDLE;
796 }