atl80: Added AtlComModuleRegisterServer implementation (based on AtlModuleRegisterSer...
[wine] / dlls / devenum / createdevenum.c
1 /*
2  *      ICreateDevEnum implementation for DEVENUM.dll
3  *
4  * Copyright (C) 2002 Robert Shearman
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * NOTES ON THIS FILE:
21  * - Implements ICreateDevEnum interface which creates an IEnumMoniker
22  *   implementation
23  * - Also creates the special registry keys created at run-time
24  */
25
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
28
29 #include "devenum_private.h"
30 #include "vfw.h"
31
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34 #include "mmddk.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
37
38 extern HINSTANCE DEVENUM_hInstance;
39
40 const WCHAR wszInstanceKeyName[] ={'I','n','s','t','a','n','c','e',0};
41
42 static const WCHAR wszRegSeparator[] =   {'\\', 0 };
43 static const WCHAR wszActiveMovieKey[] = {'S','o','f','t','w','a','r','e','\\',
44                                    'M','i','c','r','o','s','o','f','t','\\',
45                                    'A','c','t','i','v','e','M','o','v','i','e','\\',
46                                    'd','e','v','e','n','u','m','\\',0};
47 static const WCHAR wszFilterKeyName[] = {'F','i','l','t','e','r',0};
48 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
49 static const WCHAR wszPins[] = {'P','i','n','s',0};
50 static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
51 static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
52 static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
53 static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
54 static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
55
56 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface);
57 static HRESULT DEVENUM_CreateSpecialCategories(void);
58
59 /**********************************************************************
60  * DEVENUM_ICreateDevEnum_QueryInterface (also IUnknown)
61  */
62 static HRESULT WINAPI DEVENUM_ICreateDevEnum_QueryInterface(ICreateDevEnum *iface, REFIID riid,
63         void **ppv)
64 {
65     TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
66
67     if (!ppv)
68         return E_POINTER;
69
70     if (IsEqualGUID(riid, &IID_IUnknown) ||
71         IsEqualGUID(riid, &IID_ICreateDevEnum))
72     {
73         *ppv = iface;
74         DEVENUM_ICreateDevEnum_AddRef(iface);
75         return S_OK;
76     }
77
78     FIXME("- no interface IID: %s\n", debugstr_guid(riid));
79     *ppv = NULL;
80     return E_NOINTERFACE;
81 }
82
83 /**********************************************************************
84  * DEVENUM_ICreateDevEnum_AddRef (also IUnknown)
85  */
86 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface)
87 {
88     TRACE("\n");
89
90     DEVENUM_LockModule();
91
92     return 2; /* non-heap based object */
93 }
94
95 /**********************************************************************
96  * DEVENUM_ICreateDevEnum_Release (also IUnknown)
97  */
98 static ULONG WINAPI DEVENUM_ICreateDevEnum_Release(ICreateDevEnum * iface)
99 {
100     TRACE("\n");
101
102     DEVENUM_UnlockModule();
103
104     return 1; /* non-heap based object */
105 }
106
107 HRESULT DEVENUM_GetCategoryKey(REFCLSID clsidDeviceClass, HKEY *pBaseKey, WCHAR *wszRegKeyName, UINT maxLen)
108 {
109     if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
110         IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
111         IsEqualGUID(clsidDeviceClass, &CLSID_VideoInputDeviceCategory) ||
112         IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
113     {
114         *pBaseKey = HKEY_CURRENT_USER;
115         strcpyW(wszRegKeyName, wszActiveMovieKey);
116
117         if (!StringFromGUID2(clsidDeviceClass, wszRegKeyName + strlenW(wszRegKeyName), maxLen - strlenW(wszRegKeyName)))
118             return E_OUTOFMEMORY;
119     }
120     else
121     {
122         *pBaseKey = HKEY_CLASSES_ROOT;
123         strcpyW(wszRegKeyName, clsid_keyname);
124         strcatW(wszRegKeyName, wszRegSeparator);
125
126         if (!StringFromGUID2(clsidDeviceClass, wszRegKeyName + CLSID_STR_LEN, maxLen - CLSID_STR_LEN))
127             return E_OUTOFMEMORY;
128
129         strcatW(wszRegKeyName, wszRegSeparator);
130         strcatW(wszRegKeyName, wszInstanceKeyName);
131     }
132
133     return S_OK;
134 }
135
136 static void DEVENUM_ReadPinTypes(HKEY hkeyPinKey, REGFILTERPINS *rgPin)
137 {
138     HKEY hkeyTypes = NULL;
139     DWORD dwMajorTypes, i;
140     REGPINTYPES *lpMediaType = NULL;
141     DWORD dwMediaTypeSize = 0;
142
143     if (RegOpenKeyExW(hkeyPinKey, wszTypes, 0, KEY_READ, &hkeyTypes) != ERROR_SUCCESS)
144         return ;
145
146     if (RegQueryInfoKeyW(hkeyTypes, NULL, NULL, NULL, &dwMajorTypes, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
147                 != ERROR_SUCCESS)
148     {
149         RegCloseKey(hkeyTypes);
150         return ;
151     }
152
153     for (i = 0; i < dwMajorTypes; i++)
154     {
155         HKEY hkeyMajorType = NULL;
156         WCHAR wszMajorTypeName[64];
157         DWORD cName = sizeof(wszMajorTypeName) / sizeof(WCHAR);
158         DWORD dwMinorTypes, i1;
159
160         if (RegEnumKeyExW(hkeyTypes, i, wszMajorTypeName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
161
162         if (RegOpenKeyExW(hkeyTypes, wszMajorTypeName, 0, KEY_READ, &hkeyMajorType) != ERROR_SUCCESS) continue;
163
164         if (RegQueryInfoKeyW(hkeyMajorType, NULL, NULL, NULL, &dwMinorTypes, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
165                     != ERROR_SUCCESS)
166         {
167             RegCloseKey(hkeyMajorType);
168             continue;
169         }
170
171         for (i1 = 0; i1 < dwMinorTypes; i1++)
172         {
173             WCHAR wszMinorTypeName[64];
174             CLSID *clsMajorType = NULL, *clsMinorType = NULL;
175             HRESULT hr;
176
177             cName = sizeof(wszMinorTypeName) / sizeof(WCHAR);
178             if (RegEnumKeyExW(hkeyMajorType, i1, wszMinorTypeName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
179
180             clsMinorType = CoTaskMemAlloc(sizeof(CLSID));
181             if (!clsMinorType) continue;
182
183             clsMajorType = CoTaskMemAlloc(sizeof(CLSID));
184             if (!clsMajorType) goto error_cleanup_types;
185
186             hr = CLSIDFromString(wszMinorTypeName, clsMinorType);
187             if (FAILED(hr)) goto error_cleanup_types;
188
189             hr = CLSIDFromString(wszMajorTypeName, clsMajorType);
190             if (FAILED(hr)) goto error_cleanup_types;
191
192             if (rgPin->nMediaTypes == dwMediaTypeSize)
193             {
194                 DWORD dwNewSize = dwMediaTypeSize + (dwMediaTypeSize < 2 ? 1 : dwMediaTypeSize / 2);
195                 REGPINTYPES *lpNewMediaType;
196
197                 lpNewMediaType = CoTaskMemRealloc(lpMediaType, sizeof(REGPINTYPES) * dwNewSize);
198                 if (!lpNewMediaType) goto error_cleanup_types;
199
200                 lpMediaType = lpNewMediaType;
201                 dwMediaTypeSize = dwNewSize;
202              }
203
204             lpMediaType[rgPin->nMediaTypes].clsMajorType = clsMajorType;
205             lpMediaType[rgPin->nMediaTypes].clsMinorType = clsMinorType;
206             rgPin->nMediaTypes++;
207             continue;
208
209             error_cleanup_types:
210
211             if (clsMajorType) CoTaskMemFree(clsMajorType);
212             if (clsMinorType) CoTaskMemFree(clsMinorType);
213         }
214
215         RegCloseKey(hkeyMajorType);
216     }
217
218     RegCloseKey(hkeyTypes);
219
220     if (lpMediaType && !rgPin->nMediaTypes)
221     {
222         CoTaskMemFree(lpMediaType);
223         lpMediaType = NULL;
224     }
225
226     rgPin->lpMediaType = lpMediaType;
227 }
228
229 static void DEVENUM_ReadPins(HKEY hkeyFilterClass, REGFILTER2 *rgf2)
230 {
231     HKEY hkeyPins = NULL;
232     DWORD dwPinsSubkeys, i;
233     REGFILTERPINS *rgPins = NULL;
234
235     if (RegOpenKeyExW(hkeyFilterClass, wszPins, 0, KEY_READ, &hkeyPins) != ERROR_SUCCESS)
236         return ;
237
238     if (RegQueryInfoKeyW(hkeyPins, NULL, NULL, NULL, &dwPinsSubkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
239                 != ERROR_SUCCESS)
240     {
241         RegCloseKey(hkeyPins);
242         return ;
243     }
244
245     if (dwPinsSubkeys)
246     {
247         rgPins = CoTaskMemAlloc(sizeof(REGFILTERPINS) * dwPinsSubkeys);
248         if (!rgPins)
249         {
250             RegCloseKey(hkeyPins);
251             return ;
252         }
253     }
254
255     for (i = 0; i < dwPinsSubkeys; i++)
256     {
257         HKEY hkeyPinKey = NULL;
258         WCHAR wszPinName[MAX_PATH];
259         DWORD cName = sizeof(wszPinName) / sizeof(WCHAR);
260         DWORD Type, cbData;
261         REGFILTERPINS *rgPin = &rgPins[rgf2->u.s1.cPins];
262         LONG lRet;
263
264         rgPin->strName = NULL;
265         rgPin->clsConnectsToFilter = &GUID_NULL;
266         rgPin->strConnectsToPin = NULL;
267         rgPin->nMediaTypes = 0;
268         rgPin->lpMediaType = NULL;
269
270         if (RegEnumKeyExW(hkeyPins, i, wszPinName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
271
272         if (RegOpenKeyExW(hkeyPins, wszPinName, 0, KEY_READ, &hkeyPinKey) != ERROR_SUCCESS) continue;
273
274         rgPin->strName = CoTaskMemAlloc((strlenW(wszPinName) + 1) * sizeof(WCHAR));
275         if (!rgPin->strName) goto error_cleanup;
276
277         strcpyW(rgPin->strName, wszPinName);
278
279         cbData = sizeof(rgPin->bMany);
280         lRet = RegQueryValueExW(hkeyPinKey, wszAllowedMany, NULL, &Type, (LPBYTE)&rgPin->bMany, &cbData);
281         if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
282             goto error_cleanup;
283
284         cbData = sizeof(rgPin->bZero);
285         lRet = RegQueryValueExW(hkeyPinKey, wszAllowedZero, NULL, &Type, (LPBYTE)&rgPin->bZero, &cbData);
286         if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
287             goto error_cleanup;
288
289         cbData = sizeof(rgPin->bOutput);
290         lRet = RegQueryValueExW(hkeyPinKey, wszDirection, NULL, &Type, (LPBYTE)&rgPin->bOutput, &cbData);
291         if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
292             goto error_cleanup;
293
294         cbData = sizeof(rgPin->bRendered);
295         lRet = RegQueryValueExW(hkeyPinKey, wszIsRendered, NULL, &Type, (LPBYTE)&rgPin->bRendered, &cbData);
296         if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
297             goto error_cleanup;
298
299         DEVENUM_ReadPinTypes(hkeyPinKey, rgPin);
300
301         ++rgf2->u.s1.cPins;
302         continue;
303
304         error_cleanup:
305
306         RegCloseKey(hkeyPinKey);
307         if (rgPin->strName) CoTaskMemFree(rgPin->strName);
308     }
309
310     RegCloseKey(hkeyPins);
311
312     if (rgPins && !rgf2->u.s1.cPins)
313     {
314         CoTaskMemFree(rgPins);
315         rgPins = NULL;
316     }
317
318     rgf2->u.s1.rgPins = rgPins;
319 }
320
321 static HRESULT DEVENUM_RegisterLegacyAmFilters(void)
322 {
323     HKEY hkeyFilter = NULL;
324     DWORD dwFilterSubkeys, i;
325     LONG lRet;
326     IFilterMapper2 *pMapper = NULL;
327     HRESULT hr;
328
329     hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
330                            &IID_IFilterMapper2, (void **) &pMapper);
331     if (SUCCEEDED(hr))
332     {
333         lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilterKeyName, 0, KEY_READ, &hkeyFilter);
334         hr = HRESULT_FROM_WIN32(lRet);
335     }
336
337     if (SUCCEEDED(hr))
338     {
339         lRet = RegQueryInfoKeyW(hkeyFilter, NULL, NULL, NULL, &dwFilterSubkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
340         hr = HRESULT_FROM_WIN32(lRet);
341     }
342
343     if (SUCCEEDED(hr))
344     {
345         for (i = 0; i < dwFilterSubkeys; i++)
346         {
347             WCHAR wszFilterSubkeyName[64];
348             DWORD cName = sizeof(wszFilterSubkeyName) / sizeof(WCHAR);
349             HKEY hkeyCategoryBaseKey;
350             WCHAR wszRegKey[MAX_PATH];
351             HKEY hkeyInstance = NULL;
352
353             if (RegEnumKeyExW(hkeyFilter, i, wszFilterSubkeyName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
354
355             hr = DEVENUM_GetCategoryKey(&CLSID_LegacyAmFilterCategory, &hkeyCategoryBaseKey, wszRegKey, MAX_PATH);
356             if (FAILED(hr)) continue;
357
358             strcatW(wszRegKey, wszRegSeparator);
359             strcatW(wszRegKey, wszFilterSubkeyName);
360
361             if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszRegKey, 0, KEY_READ, &hkeyInstance) == ERROR_SUCCESS)
362             {
363                 RegCloseKey(hkeyInstance);
364             }
365             else
366             {
367                 /* Filter is registered the IFilterMapper(1)-way in HKCR\Filter. Needs to be added to
368                  * legacy am filter category. */
369                 HKEY hkeyFilterClass = NULL;
370                 REGFILTER2 rgf2;
371                 CLSID clsidFilter;
372                 WCHAR wszFilterName[MAX_PATH];
373                 DWORD Type;
374                 DWORD cbData;
375                 HRESULT res;
376                 IMoniker *pMoniker = NULL;
377
378                 TRACE("Registering %s\n", debugstr_w(wszFilterSubkeyName));
379
380                 strcpyW(wszRegKey, clsid_keyname);
381                 strcatW(wszRegKey, wszRegSeparator);
382                 strcatW(wszRegKey, wszFilterSubkeyName);
383
384                 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszRegKey, 0, KEY_READ, &hkeyFilterClass) != ERROR_SUCCESS)
385                     continue;
386
387                 rgf2.dwVersion = 1;
388                 rgf2.dwMerit = 0;
389                 rgf2.u.s1.cPins = 0;
390                 rgf2.u.s1.rgPins = NULL;
391
392                 cbData = sizeof(wszFilterName);
393                 if (RegQueryValueExW(hkeyFilterClass, NULL, NULL, &Type, (LPBYTE)wszFilterName, &cbData) != ERROR_SUCCESS ||
394                     Type != REG_SZ)
395                     goto cleanup;
396
397                 cbData = sizeof(rgf2.dwMerit);
398                 if (RegQueryValueExW(hkeyFilterClass, wszMeritName, NULL, &Type, (LPBYTE)&rgf2.dwMerit, &cbData) != ERROR_SUCCESS ||
399                     Type != REG_DWORD)
400                     goto cleanup;
401
402                 DEVENUM_ReadPins(hkeyFilterClass, &rgf2);
403
404                 res = CLSIDFromString(wszFilterSubkeyName, &clsidFilter);
405                 if (FAILED(res)) goto cleanup;
406
407                 IFilterMapper2_RegisterFilter(pMapper, &clsidFilter, wszFilterName, &pMoniker, NULL, NULL, &rgf2);
408
409                 if (pMoniker)
410                     IMoniker_Release(pMoniker);
411
412                 cleanup:
413
414                 if (hkeyFilterClass) RegCloseKey(hkeyFilterClass);
415
416                 if (rgf2.u.s1.rgPins)
417                 {
418                     UINT iPin;
419
420                     for (iPin = 0; iPin < rgf2.u.s1.cPins; iPin++)
421                     {
422                         CoTaskMemFree(rgf2.u.s1.rgPins[iPin].strName);
423
424                         if (rgf2.u.s1.rgPins[iPin].lpMediaType)
425                         {
426                             UINT iType;
427
428                             for (iType = 0; iType < rgf2.u.s1.rgPins[iPin].nMediaTypes; iType++)
429                             {
430                                 CoTaskMemFree((void*)rgf2.u.s1.rgPins[iPin].lpMediaType[iType].clsMajorType);
431                                 CoTaskMemFree((void*)rgf2.u.s1.rgPins[iPin].lpMediaType[iType].clsMinorType);
432                             }
433
434                             CoTaskMemFree((void*)rgf2.u.s1.rgPins[iPin].lpMediaType);
435                         }
436                     }
437
438                     CoTaskMemFree((void*)rgf2.u.s1.rgPins);
439                 }
440             }
441         }
442     }
443
444     if (hkeyFilter) RegCloseKey(hkeyFilter);
445
446     if (pMapper)
447         IFilterMapper2_Release(pMapper);
448
449     return S_OK;
450 }
451
452 /**********************************************************************
453  * DEVENUM_ICreateDevEnum_CreateClassEnumerator
454  */
455 static HRESULT WINAPI DEVENUM_ICreateDevEnum_CreateClassEnumerator(
456     ICreateDevEnum * iface,
457     REFCLSID clsidDeviceClass,
458     IEnumMoniker **ppEnumMoniker,
459     DWORD dwFlags)
460 {
461     WCHAR wszRegKey[MAX_PATH];
462     HKEY hkey;
463     HKEY hbasekey;
464     HRESULT hr;
465
466     TRACE("(%p)->(%s, %p, %x)\n", iface, debugstr_guid(clsidDeviceClass), ppEnumMoniker, dwFlags);
467
468     if (!ppEnumMoniker)
469         return E_POINTER;
470
471     *ppEnumMoniker = NULL;
472
473     if (IsEqualGUID(clsidDeviceClass, &CLSID_LegacyAmFilterCategory))
474     {
475         DEVENUM_RegisterLegacyAmFilters();
476     }
477
478     hr = DEVENUM_GetCategoryKey(clsidDeviceClass, &hbasekey, wszRegKey, MAX_PATH);
479     if (FAILED(hr))
480         return hr;
481
482     if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
483         IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
484         IsEqualGUID(clsidDeviceClass, &CLSID_VideoInputDeviceCategory) ||
485         IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
486     {
487          hr = DEVENUM_CreateSpecialCategories();
488          if (FAILED(hr))
489              return hr;
490          if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
491          {
492              ERR("Couldn't open registry key for special device: %s\n",
493                  debugstr_guid(clsidDeviceClass));
494              return S_FALSE;
495          }
496     }
497     else if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
498     {
499         FIXME("Category %s not found\n", debugstr_guid(clsidDeviceClass));
500         return S_FALSE;
501     }
502
503     return DEVENUM_IEnumMoniker_Construct(hkey, ppEnumMoniker);
504 }
505
506 /**********************************************************************
507  * ICreateDevEnum_Vtbl
508  */
509 static const ICreateDevEnumVtbl ICreateDevEnum_Vtbl =
510 {
511     DEVENUM_ICreateDevEnum_QueryInterface,
512     DEVENUM_ICreateDevEnum_AddRef,
513     DEVENUM_ICreateDevEnum_Release,
514     DEVENUM_ICreateDevEnum_CreateClassEnumerator,
515 };
516
517 /**********************************************************************
518  * static CreateDevEnum instance
519  */
520 ICreateDevEnum DEVENUM_CreateDevEnum = { &ICreateDevEnum_Vtbl };
521
522 /**********************************************************************
523  * DEVENUM_CreateAMCategoryKey (INTERNAL)
524  *
525  * Creates a registry key for a category at HKEY_CURRENT_USER\Software\
526  * Microsoft\ActiveMovie\devenum\{clsid}
527  */
528 static HRESULT DEVENUM_CreateAMCategoryKey(const CLSID * clsidCategory)
529 {
530     WCHAR wszRegKey[MAX_PATH];
531     HRESULT res = S_OK;
532     HKEY hkeyDummy = NULL;
533
534     strcpyW(wszRegKey, wszActiveMovieKey);
535
536     if (!StringFromGUID2(clsidCategory, wszRegKey + strlenW(wszRegKey), sizeof(wszRegKey)/sizeof(wszRegKey[0]) - strlenW(wszRegKey)))
537         res = E_INVALIDARG;
538
539     if (SUCCEEDED(res))
540     {
541         LONG lRes = RegCreateKeyW(HKEY_CURRENT_USER, wszRegKey, &hkeyDummy);
542         res = HRESULT_FROM_WIN32(lRes);
543     }
544
545     if (hkeyDummy)
546         RegCloseKey(hkeyDummy);
547
548     if (FAILED(res))
549         ERR("Failed to create key HKEY_CURRENT_USER\\%s\n", debugstr_w(wszRegKey));
550
551     return res;
552 }
553
554 static HANDLE DEVENUM_populate_handle;
555 static const WCHAR DEVENUM_populate_handle_nameW[] =
556     {'_','_','W','I','N','E','_',
557      'D','e','v','e','n','u','m','_',
558      'P','o','p','u','l','a','t','e',0};
559
560 /**********************************************************************
561  * DEVENUM_CreateSpecialCategories (INTERNAL)
562  *
563  * Creates the keys in the registry for the dynamic categories
564  */
565 static HRESULT DEVENUM_CreateSpecialCategories(void)
566 {
567     HRESULT res;
568     WCHAR szDSoundNameFormat[MAX_PATH + 1];
569     WCHAR szDSoundName[MAX_PATH + 1];
570     DWORD iDefaultDevice = -1;
571     UINT numDevs;
572     IFilterMapper2 * pMapper = NULL;
573     REGFILTER2 rf2;
574     REGFILTERPINS2 rfp2;
575     WCHAR path[MAX_PATH];
576     HKEY basekey;
577
578     if (DEVENUM_populate_handle)
579         return S_OK;
580     DEVENUM_populate_handle = CreateEventW(NULL, TRUE, FALSE, DEVENUM_populate_handle_nameW);
581     if (GetLastError() == ERROR_ALREADY_EXISTS)
582     {
583         /* Webcams can take some time to scan if the driver is badly written and it enables them,
584          * so have a 10 s timeout here
585          */
586         if (WaitForSingleObject(DEVENUM_populate_handle, 10000) == WAIT_TIMEOUT)
587             WARN("Waiting for object timed out\n");
588         TRACE("No need to rescan\n");
589         return S_OK;
590     }
591     TRACE("Scanning for devices\n");
592
593     /* Since devices can change between session, for example because you just plugged in a webcam
594      * or switched from pulseaudio to alsa, delete all old devices first
595      */
596     if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_AudioRendererCategory, &basekey, path, MAX_PATH)))
597         RegDeleteTreeW(basekey, path);
598     if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_AudioInputDeviceCategory, &basekey, path, MAX_PATH)))
599         RegDeleteTreeW(basekey, path);
600     if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_VideoInputDeviceCategory, &basekey, path, MAX_PATH)))
601         RegDeleteTreeW(basekey, path);
602     if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_MidiRendererCategory, &basekey, path, MAX_PATH)))
603         RegDeleteTreeW(basekey, path);
604
605     rf2.dwVersion = 2;
606     rf2.dwMerit = MERIT_PREFERRED;
607     rf2.u.s2.cPins2 = 1;
608     rf2.u.s2.rgPins2 = &rfp2;
609     rfp2.cInstances = 1;
610     rfp2.nMediums = 0;
611     rfp2.lpMedium = NULL;
612     rfp2.clsPinCategory = &IID_NULL;
613
614     if (!LoadStringW(DEVENUM_hInstance, IDS_DEVENUM_DS, szDSoundNameFormat, sizeof(szDSoundNameFormat)/sizeof(szDSoundNameFormat[0])-1))
615     {
616         ERR("Couldn't get string resource (GetLastError() is %d)\n", GetLastError());
617         return HRESULT_FROM_WIN32(GetLastError());
618     }
619
620     res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
621                            &IID_IFilterMapper2, (void **) &pMapper);
622     /*
623      * Fill in info for devices
624      */
625     if (SUCCEEDED(res))
626     {
627         UINT i;
628         WAVEOUTCAPSW wocaps;
629         WAVEINCAPSW wicaps;
630         MIDIOUTCAPSW mocaps;
631         REGPINTYPES * pTypes;
632
633         numDevs = waveOutGetNumDevs();
634
635         res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
636         if (FAILED(res)) /* can't register any devices in this category */
637             numDevs = 0;
638
639         rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
640         for (i = 0; i < numDevs; i++)
641         {
642             if (waveOutGetDevCapsW(i, &wocaps, sizeof(WAVEOUTCAPSW))
643                 == MMSYSERR_NOERROR)
644             {
645                 IMoniker * pMoniker = NULL;
646
647                 rfp2.nMediaTypes = 1;
648                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
649                 if (!pTypes)
650                 {
651                     IFilterMapper2_Release(pMapper);
652                     return E_OUTOFMEMORY;
653                 }
654                 /* FIXME: Native devenum seems to register a lot more types for
655                  * DSound than we do. Not sure what purpose they serve */
656                 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
657                 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
658
659                 rfp2.lpMediaType = pTypes;
660
661                 res = IFilterMapper2_RegisterFilter(pMapper,
662                                               &CLSID_AudioRender,
663                                               wocaps.szPname,
664                                               &pMoniker,
665                                               &CLSID_AudioRendererCategory,
666                                               wocaps.szPname,
667                                               &rf2);
668
669                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
670
671                 if (pMoniker)
672                 {
673                     IMoniker_Release(pMoniker);
674                     pMoniker = NULL;
675                 }
676
677                 wsprintfW(szDSoundName, szDSoundNameFormat, wocaps.szPname);
678                 res = IFilterMapper2_RegisterFilter(pMapper,
679                                               &CLSID_DSoundRender,
680                                               szDSoundName,
681                                               &pMoniker,
682                                               &CLSID_AudioRendererCategory,
683                                               szDSoundName,
684                                               &rf2);
685
686                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
687
688                 if (pMoniker)
689                     IMoniker_Release(pMoniker);
690
691                 if (i == iDefaultDevice)
692                 {
693                     FIXME("Default device\n");
694                 }
695
696                 CoTaskMemFree(pTypes);
697             }
698         }
699
700         numDevs = waveInGetNumDevs();
701
702         res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioInputDeviceCategory);
703         if (FAILED(res)) /* can't register any devices in this category */
704             numDevs = 0;
705
706         rfp2.dwFlags = REG_PINFLAG_B_OUTPUT;
707         for (i = 0; i < numDevs; i++)
708         {
709             if (waveInGetDevCapsW(i, &wicaps, sizeof(WAVEINCAPSW))
710                 == MMSYSERR_NOERROR)
711             {
712                 IMoniker * pMoniker = NULL;
713
714                 rfp2.nMediaTypes = 1;
715                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
716                 if (!pTypes)
717                 {
718                     IFilterMapper2_Release(pMapper);
719                     return E_OUTOFMEMORY;
720                 }
721
722                 /* FIXME: Not sure if these are correct */
723                 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
724                 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
725
726                 rfp2.lpMediaType = pTypes;
727
728                 res = IFilterMapper2_RegisterFilter(pMapper,
729                                               &CLSID_AudioRecord,
730                                               wicaps.szPname,
731                                               &pMoniker,
732                                               &CLSID_AudioInputDeviceCategory,
733                                               wicaps.szPname,
734                                               &rf2);
735
736                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
737
738                 if (pMoniker)
739                     IMoniker_Release(pMoniker);
740
741                 CoTaskMemFree(pTypes);
742             }
743         }
744
745         numDevs = midiOutGetNumDevs();
746
747         res = DEVENUM_CreateAMCategoryKey(&CLSID_MidiRendererCategory);
748         if (FAILED(res)) /* can't register any devices in this category */
749             numDevs = 0;
750
751         rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
752         for (i = 0; i < numDevs; i++)
753         {
754             if (midiOutGetDevCapsW(i, &mocaps, sizeof(MIDIOUTCAPSW))
755                 == MMSYSERR_NOERROR)
756             {
757                 IMoniker * pMoniker = NULL;
758
759                 rfp2.nMediaTypes = 1;
760                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
761                 if (!pTypes)
762                 {
763                     IFilterMapper2_Release(pMapper);
764                     return E_OUTOFMEMORY;
765                 }
766
767                 /* FIXME: Not sure if these are correct */
768                 pTypes[0].clsMajorType = &MEDIATYPE_Midi;
769                 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
770
771                 rfp2.lpMediaType = pTypes;
772
773                 res = IFilterMapper2_RegisterFilter(pMapper,
774                                               &CLSID_AVIMIDIRender,
775                                               mocaps.szPname,
776                                               &pMoniker,
777                                               &CLSID_MidiRendererCategory,
778                                               mocaps.szPname,
779                                               &rf2);
780
781                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
782                 /* Native version sets MidiOutId */
783
784                 if (pMoniker)
785                     IMoniker_Release(pMoniker);
786
787                 if (i == iDefaultDevice)
788                 {
789                     FIXME("Default device\n");
790                 }
791
792                 CoTaskMemFree(pTypes);
793             }
794         }
795         res = DEVENUM_CreateAMCategoryKey(&CLSID_VideoInputDeviceCategory);
796         if (SUCCEEDED(res))
797             for (i = 0; i < 10; i++)
798             {
799                 WCHAR szDeviceName[32], szDeviceVersion[32], szDevicePath[10];
800
801                 if (capGetDriverDescriptionW ((WORD) i,
802                                               szDeviceName, sizeof(szDeviceName)/sizeof(WCHAR),
803                                               szDeviceVersion, sizeof(szDeviceVersion)/sizeof(WCHAR)))
804                 {
805                     IMoniker * pMoniker = NULL;
806                     IPropertyBag * pPropBag = NULL;
807                     WCHAR dprintf[] = { 'v','i','d','e','o','%','d',0 };
808                     snprintfW(szDevicePath, sizeof(szDevicePath)/sizeof(WCHAR), dprintf, i);
809                     /* The above code prevents 1 device with a different ID overwriting another */
810
811                     rfp2.nMediaTypes = 1;
812                     pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
813                     if (!pTypes) {
814                         IFilterMapper2_Release(pMapper);
815                         return E_OUTOFMEMORY;
816                     }
817
818                     pTypes[0].clsMajorType = &MEDIATYPE_Video;
819                     pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
820
821                     rfp2.lpMediaType = pTypes;
822
823                     res = IFilterMapper2_RegisterFilter(pMapper,
824                                                         &CLSID_VfwCapture,
825                                                         szDeviceName,
826                                                         &pMoniker,
827                                                         &CLSID_VideoInputDeviceCategory,
828                                                         szDevicePath,
829                                                         &rf2);
830
831                     if (pMoniker) {
832                        OLECHAR wszVfwIndex[] = { 'V','F','W','I','n','d','e','x',0 };
833                        VARIANT var;
834                        V_VT(&var) = VT_I4;
835                        V_UNION(&var, ulVal) = i;
836                        res = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
837                        if (SUCCEEDED(res))
838                           res = IPropertyBag_Write(pPropBag, wszVfwIndex, &var);
839                        IMoniker_Release(pMoniker);
840                     }
841
842                     if (i == iDefaultDevice) FIXME("Default device\n");
843                     CoTaskMemFree(pTypes);
844                 }
845             }
846     }
847
848     if (pMapper)
849         IFilterMapper2_Release(pMapper);
850     SetEvent(DEVENUM_populate_handle);
851     return res;
852 }