xmllite: Use buffer offset instead of pointers.
[wine.git] / dlls / devenum / createdevenum.c
blobb7e288baaafdc34ec78e47e5a5aa8f156c2f5238
1 /*
2 * ICreateDevEnum implementation for DEVENUM.dll
4 * Copyright (C) 2002 Robert Shearman
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.
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.
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
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
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
29 #include "devenum_private.h"
30 #include "vfw.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34 #include "mmddk.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
38 extern HINSTANCE DEVENUM_hInstance;
40 const WCHAR wszInstanceKeyName[] ={'I','n','s','t','a','n','c','e',0};
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 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
56 static const WCHAR wszWaveInID[] = {'W','a','v','e','I','n','I','D',0};
57 static const WCHAR wszWaveOutID[] = {'W','a','v','e','O','u','t','I','D',0};
59 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface);
60 static HRESULT DEVENUM_CreateSpecialCategories(void);
62 /**********************************************************************
63 * DEVENUM_ICreateDevEnum_QueryInterface (also IUnknown)
65 static HRESULT WINAPI DEVENUM_ICreateDevEnum_QueryInterface(ICreateDevEnum *iface, REFIID riid,
66 void **ppv)
68 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
70 if (!ppv)
71 return E_POINTER;
73 if (IsEqualGUID(riid, &IID_IUnknown) ||
74 IsEqualGUID(riid, &IID_ICreateDevEnum))
76 *ppv = iface;
77 DEVENUM_ICreateDevEnum_AddRef(iface);
78 return S_OK;
81 FIXME("- no interface IID: %s\n", debugstr_guid(riid));
82 *ppv = NULL;
83 return E_NOINTERFACE;
86 /**********************************************************************
87 * DEVENUM_ICreateDevEnum_AddRef (also IUnknown)
89 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface)
91 TRACE("\n");
93 DEVENUM_LockModule();
95 return 2; /* non-heap based object */
98 /**********************************************************************
99 * DEVENUM_ICreateDevEnum_Release (also IUnknown)
101 static ULONG WINAPI DEVENUM_ICreateDevEnum_Release(ICreateDevEnum * iface)
103 TRACE("\n");
105 DEVENUM_UnlockModule();
107 return 1; /* non-heap based object */
110 HRESULT DEVENUM_GetCategoryKey(REFCLSID clsidDeviceClass, HKEY *pBaseKey, WCHAR *wszRegKeyName, UINT maxLen)
112 if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
113 IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
114 IsEqualGUID(clsidDeviceClass, &CLSID_VideoInputDeviceCategory) ||
115 IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
117 *pBaseKey = HKEY_CURRENT_USER;
118 strcpyW(wszRegKeyName, wszActiveMovieKey);
120 if (!StringFromGUID2(clsidDeviceClass, wszRegKeyName + strlenW(wszRegKeyName), maxLen - strlenW(wszRegKeyName)))
121 return E_OUTOFMEMORY;
123 else
125 *pBaseKey = HKEY_CLASSES_ROOT;
126 strcpyW(wszRegKeyName, clsid_keyname);
127 strcatW(wszRegKeyName, wszRegSeparator);
129 if (!StringFromGUID2(clsidDeviceClass, wszRegKeyName + CLSID_STR_LEN, maxLen - CLSID_STR_LEN))
130 return E_OUTOFMEMORY;
132 strcatW(wszRegKeyName, wszRegSeparator);
133 strcatW(wszRegKeyName, wszInstanceKeyName);
136 return S_OK;
139 static void DEVENUM_ReadPinTypes(HKEY hkeyPinKey, REGFILTERPINS *rgPin)
141 HKEY hkeyTypes = NULL;
142 DWORD dwMajorTypes, i;
143 REGPINTYPES *lpMediaType = NULL;
144 DWORD dwMediaTypeSize = 0;
146 if (RegOpenKeyExW(hkeyPinKey, wszTypes, 0, KEY_READ, &hkeyTypes) != ERROR_SUCCESS)
147 return ;
149 if (RegQueryInfoKeyW(hkeyTypes, NULL, NULL, NULL, &dwMajorTypes, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
150 != ERROR_SUCCESS)
152 RegCloseKey(hkeyTypes);
153 return ;
156 for (i = 0; i < dwMajorTypes; i++)
158 HKEY hkeyMajorType = NULL;
159 WCHAR wszMajorTypeName[64];
160 DWORD cName = sizeof(wszMajorTypeName) / sizeof(WCHAR);
161 DWORD dwMinorTypes, i1;
163 if (RegEnumKeyExW(hkeyTypes, i, wszMajorTypeName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
165 if (RegOpenKeyExW(hkeyTypes, wszMajorTypeName, 0, KEY_READ, &hkeyMajorType) != ERROR_SUCCESS) continue;
167 if (RegQueryInfoKeyW(hkeyMajorType, NULL, NULL, NULL, &dwMinorTypes, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
168 != ERROR_SUCCESS)
170 RegCloseKey(hkeyMajorType);
171 continue;
174 for (i1 = 0; i1 < dwMinorTypes; i1++)
176 WCHAR wszMinorTypeName[64];
177 CLSID *clsMajorType = NULL, *clsMinorType = NULL;
178 HRESULT hr;
180 cName = sizeof(wszMinorTypeName) / sizeof(WCHAR);
181 if (RegEnumKeyExW(hkeyMajorType, i1, wszMinorTypeName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
183 clsMinorType = CoTaskMemAlloc(sizeof(CLSID));
184 if (!clsMinorType) continue;
186 clsMajorType = CoTaskMemAlloc(sizeof(CLSID));
187 if (!clsMajorType) goto error_cleanup_types;
189 hr = CLSIDFromString(wszMinorTypeName, clsMinorType);
190 if (FAILED(hr)) goto error_cleanup_types;
192 hr = CLSIDFromString(wszMajorTypeName, clsMajorType);
193 if (FAILED(hr)) goto error_cleanup_types;
195 if (rgPin->nMediaTypes == dwMediaTypeSize)
197 DWORD dwNewSize = dwMediaTypeSize + (dwMediaTypeSize < 2 ? 1 : dwMediaTypeSize / 2);
198 REGPINTYPES *lpNewMediaType;
200 lpNewMediaType = CoTaskMemRealloc(lpMediaType, sizeof(REGPINTYPES) * dwNewSize);
201 if (!lpNewMediaType) goto error_cleanup_types;
203 lpMediaType = lpNewMediaType;
204 dwMediaTypeSize = dwNewSize;
207 lpMediaType[rgPin->nMediaTypes].clsMajorType = clsMajorType;
208 lpMediaType[rgPin->nMediaTypes].clsMinorType = clsMinorType;
209 rgPin->nMediaTypes++;
210 continue;
212 error_cleanup_types:
214 if (clsMajorType) CoTaskMemFree(clsMajorType);
215 if (clsMinorType) CoTaskMemFree(clsMinorType);
218 RegCloseKey(hkeyMajorType);
221 RegCloseKey(hkeyTypes);
223 if (lpMediaType && !rgPin->nMediaTypes)
225 CoTaskMemFree(lpMediaType);
226 lpMediaType = NULL;
229 rgPin->lpMediaType = lpMediaType;
232 static void DEVENUM_ReadPins(HKEY hkeyFilterClass, REGFILTER2 *rgf2)
234 HKEY hkeyPins = NULL;
235 DWORD dwPinsSubkeys, i;
236 REGFILTERPINS *rgPins = NULL;
238 if (RegOpenKeyExW(hkeyFilterClass, wszPins, 0, KEY_READ, &hkeyPins) != ERROR_SUCCESS)
239 return ;
241 if (RegQueryInfoKeyW(hkeyPins, NULL, NULL, NULL, &dwPinsSubkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
242 != ERROR_SUCCESS)
244 RegCloseKey(hkeyPins);
245 return ;
248 if (dwPinsSubkeys)
250 rgPins = CoTaskMemAlloc(sizeof(REGFILTERPINS) * dwPinsSubkeys);
251 if (!rgPins)
253 RegCloseKey(hkeyPins);
254 return ;
258 for (i = 0; i < dwPinsSubkeys; i++)
260 HKEY hkeyPinKey = NULL;
261 WCHAR wszPinName[MAX_PATH];
262 DWORD cName = sizeof(wszPinName) / sizeof(WCHAR);
263 DWORD Type, cbData;
264 REGFILTERPINS *rgPin = &rgPins[rgf2->u.s1.cPins];
265 LONG lRet;
267 rgPin->strName = NULL;
268 rgPin->clsConnectsToFilter = &GUID_NULL;
269 rgPin->strConnectsToPin = NULL;
270 rgPin->nMediaTypes = 0;
271 rgPin->lpMediaType = NULL;
273 if (RegEnumKeyExW(hkeyPins, i, wszPinName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
275 if (RegOpenKeyExW(hkeyPins, wszPinName, 0, KEY_READ, &hkeyPinKey) != ERROR_SUCCESS) continue;
277 rgPin->strName = CoTaskMemAlloc((strlenW(wszPinName) + 1) * sizeof(WCHAR));
278 if (!rgPin->strName) goto error_cleanup;
280 strcpyW(rgPin->strName, wszPinName);
282 cbData = sizeof(rgPin->bMany);
283 lRet = RegQueryValueExW(hkeyPinKey, wszAllowedMany, NULL, &Type, (LPBYTE)&rgPin->bMany, &cbData);
284 if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
285 goto error_cleanup;
287 cbData = sizeof(rgPin->bZero);
288 lRet = RegQueryValueExW(hkeyPinKey, wszAllowedZero, NULL, &Type, (LPBYTE)&rgPin->bZero, &cbData);
289 if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
290 goto error_cleanup;
292 cbData = sizeof(rgPin->bOutput);
293 lRet = RegQueryValueExW(hkeyPinKey, wszDirection, NULL, &Type, (LPBYTE)&rgPin->bOutput, &cbData);
294 if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
295 goto error_cleanup;
297 cbData = sizeof(rgPin->bRendered);
298 lRet = RegQueryValueExW(hkeyPinKey, wszIsRendered, NULL, &Type, (LPBYTE)&rgPin->bRendered, &cbData);
299 if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
300 goto error_cleanup;
302 DEVENUM_ReadPinTypes(hkeyPinKey, rgPin);
304 ++rgf2->u.s1.cPins;
305 continue;
307 error_cleanup:
309 RegCloseKey(hkeyPinKey);
310 if (rgPin->strName) CoTaskMemFree(rgPin->strName);
313 RegCloseKey(hkeyPins);
315 if (rgPins && !rgf2->u.s1.cPins)
317 CoTaskMemFree(rgPins);
318 rgPins = NULL;
321 rgf2->u.s1.rgPins = rgPins;
324 static HRESULT DEVENUM_RegisterLegacyAmFilters(void)
326 HKEY hkeyFilter = NULL;
327 DWORD dwFilterSubkeys, i;
328 LONG lRet;
329 IFilterMapper2 *pMapper = NULL;
330 HRESULT hr;
332 hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
333 &IID_IFilterMapper2, (void **) &pMapper);
334 if (SUCCEEDED(hr))
336 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilterKeyName, 0, KEY_READ, &hkeyFilter);
337 hr = HRESULT_FROM_WIN32(lRet);
340 if (SUCCEEDED(hr))
342 lRet = RegQueryInfoKeyW(hkeyFilter, NULL, NULL, NULL, &dwFilterSubkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
343 hr = HRESULT_FROM_WIN32(lRet);
346 if (SUCCEEDED(hr))
348 for (i = 0; i < dwFilterSubkeys; i++)
350 WCHAR wszFilterSubkeyName[64];
351 DWORD cName = sizeof(wszFilterSubkeyName) / sizeof(WCHAR);
352 HKEY hkeyCategoryBaseKey;
353 WCHAR wszRegKey[MAX_PATH];
354 HKEY hkeyInstance = NULL;
356 if (RegEnumKeyExW(hkeyFilter, i, wszFilterSubkeyName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
358 hr = DEVENUM_GetCategoryKey(&CLSID_LegacyAmFilterCategory, &hkeyCategoryBaseKey, wszRegKey, MAX_PATH);
359 if (FAILED(hr)) continue;
361 strcatW(wszRegKey, wszRegSeparator);
362 strcatW(wszRegKey, wszFilterSubkeyName);
364 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszRegKey, 0, KEY_READ, &hkeyInstance) == ERROR_SUCCESS)
366 RegCloseKey(hkeyInstance);
368 else
370 /* Filter is registered the IFilterMapper(1)-way in HKCR\Filter. Needs to be added to
371 * legacy am filter category. */
372 HKEY hkeyFilterClass = NULL;
373 REGFILTER2 rgf2;
374 CLSID clsidFilter;
375 WCHAR wszFilterName[MAX_PATH];
376 DWORD Type;
377 DWORD cbData;
378 HRESULT res;
379 IMoniker *pMoniker = NULL;
381 TRACE("Registering %s\n", debugstr_w(wszFilterSubkeyName));
383 strcpyW(wszRegKey, clsid_keyname);
384 strcatW(wszRegKey, wszRegSeparator);
385 strcatW(wszRegKey, wszFilterSubkeyName);
387 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszRegKey, 0, KEY_READ, &hkeyFilterClass) != ERROR_SUCCESS)
388 continue;
390 rgf2.dwVersion = 1;
391 rgf2.dwMerit = 0;
392 rgf2.u.s1.cPins = 0;
393 rgf2.u.s1.rgPins = NULL;
395 cbData = sizeof(wszFilterName);
396 if (RegQueryValueExW(hkeyFilterClass, NULL, NULL, &Type, (LPBYTE)wszFilterName, &cbData) != ERROR_SUCCESS ||
397 Type != REG_SZ)
398 goto cleanup;
400 cbData = sizeof(rgf2.dwMerit);
401 if (RegQueryValueExW(hkeyFilterClass, wszMeritName, NULL, &Type, (LPBYTE)&rgf2.dwMerit, &cbData) != ERROR_SUCCESS ||
402 Type != REG_DWORD)
403 goto cleanup;
405 DEVENUM_ReadPins(hkeyFilterClass, &rgf2);
407 res = CLSIDFromString(wszFilterSubkeyName, &clsidFilter);
408 if (FAILED(res)) goto cleanup;
410 IFilterMapper2_RegisterFilter(pMapper, &clsidFilter, wszFilterName, &pMoniker, NULL, NULL, &rgf2);
412 if (pMoniker)
413 IMoniker_Release(pMoniker);
415 cleanup:
417 if (hkeyFilterClass) RegCloseKey(hkeyFilterClass);
419 if (rgf2.u.s1.rgPins)
421 UINT iPin;
423 for (iPin = 0; iPin < rgf2.u.s1.cPins; iPin++)
425 CoTaskMemFree(rgf2.u.s1.rgPins[iPin].strName);
427 if (rgf2.u.s1.rgPins[iPin].lpMediaType)
429 UINT iType;
431 for (iType = 0; iType < rgf2.u.s1.rgPins[iPin].nMediaTypes; iType++)
433 CoTaskMemFree((void*)rgf2.u.s1.rgPins[iPin].lpMediaType[iType].clsMajorType);
434 CoTaskMemFree((void*)rgf2.u.s1.rgPins[iPin].lpMediaType[iType].clsMinorType);
437 CoTaskMemFree((void*)rgf2.u.s1.rgPins[iPin].lpMediaType);
441 CoTaskMemFree((void*)rgf2.u.s1.rgPins);
447 if (hkeyFilter) RegCloseKey(hkeyFilter);
449 if (pMapper)
450 IFilterMapper2_Release(pMapper);
452 return S_OK;
455 /**********************************************************************
456 * DEVENUM_ICreateDevEnum_CreateClassEnumerator
458 static HRESULT WINAPI DEVENUM_ICreateDevEnum_CreateClassEnumerator(
459 ICreateDevEnum * iface,
460 REFCLSID clsidDeviceClass,
461 IEnumMoniker **ppEnumMoniker,
462 DWORD dwFlags)
464 WCHAR wszRegKey[MAX_PATH];
465 HKEY hkey;
466 HKEY hbasekey;
467 HRESULT hr;
469 TRACE("(%p)->(%s, %p, %x)\n", iface, debugstr_guid(clsidDeviceClass), ppEnumMoniker, dwFlags);
471 if (!ppEnumMoniker)
472 return E_POINTER;
474 *ppEnumMoniker = NULL;
476 if (IsEqualGUID(clsidDeviceClass, &CLSID_LegacyAmFilterCategory))
478 DEVENUM_RegisterLegacyAmFilters();
481 hr = DEVENUM_GetCategoryKey(clsidDeviceClass, &hbasekey, wszRegKey, MAX_PATH);
482 if (FAILED(hr))
483 return hr;
485 if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
486 IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
487 IsEqualGUID(clsidDeviceClass, &CLSID_VideoInputDeviceCategory) ||
488 IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
490 hr = DEVENUM_CreateSpecialCategories();
491 if (FAILED(hr))
492 return hr;
493 if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
495 ERR("Couldn't open registry key for special device: %s\n",
496 debugstr_guid(clsidDeviceClass));
497 return S_FALSE;
500 else if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
502 FIXME("Category %s not found\n", debugstr_guid(clsidDeviceClass));
503 return S_FALSE;
506 return DEVENUM_IEnumMoniker_Construct(hkey, ppEnumMoniker);
509 /**********************************************************************
510 * ICreateDevEnum_Vtbl
512 static const ICreateDevEnumVtbl ICreateDevEnum_Vtbl =
514 DEVENUM_ICreateDevEnum_QueryInterface,
515 DEVENUM_ICreateDevEnum_AddRef,
516 DEVENUM_ICreateDevEnum_Release,
517 DEVENUM_ICreateDevEnum_CreateClassEnumerator,
520 /**********************************************************************
521 * static CreateDevEnum instance
523 ICreateDevEnum DEVENUM_CreateDevEnum = { &ICreateDevEnum_Vtbl };
525 /**********************************************************************
526 * DEVENUM_CreateAMCategoryKey (INTERNAL)
528 * Creates a registry key for a category at HKEY_CURRENT_USER\Software\
529 * Microsoft\ActiveMovie\devenum\{clsid}
531 static HRESULT DEVENUM_CreateAMCategoryKey(const CLSID * clsidCategory)
533 WCHAR wszRegKey[MAX_PATH];
534 HRESULT res = S_OK;
535 HKEY hkeyDummy = NULL;
537 strcpyW(wszRegKey, wszActiveMovieKey);
539 if (!StringFromGUID2(clsidCategory, wszRegKey + strlenW(wszRegKey), sizeof(wszRegKey)/sizeof(wszRegKey[0]) - strlenW(wszRegKey)))
540 res = E_INVALIDARG;
542 if (SUCCEEDED(res))
544 LONG lRes = RegCreateKeyW(HKEY_CURRENT_USER, wszRegKey, &hkeyDummy);
545 res = HRESULT_FROM_WIN32(lRes);
548 if (hkeyDummy)
549 RegCloseKey(hkeyDummy);
551 if (FAILED(res))
552 ERR("Failed to create key HKEY_CURRENT_USER\\%s\n", debugstr_w(wszRegKey));
554 return res;
557 static HANDLE DEVENUM_populate_handle;
558 static const WCHAR DEVENUM_populate_handle_nameW[] =
559 {'_','_','W','I','N','E','_',
560 'D','e','v','e','n','u','m','_',
561 'P','o','p','u','l','a','t','e',0};
563 /**********************************************************************
564 * DEVENUM_CreateSpecialCategories (INTERNAL)
566 * Creates the keys in the registry for the dynamic categories
568 static HRESULT DEVENUM_CreateSpecialCategories(void)
570 HRESULT res;
571 WCHAR szDSoundNameFormat[MAX_PATH + 1];
572 WCHAR szDSoundName[MAX_PATH + 1];
573 DWORD iDefaultDevice = -1;
574 UINT numDevs;
575 IFilterMapper2 * pMapper = NULL;
576 REGFILTER2 rf2;
577 REGFILTERPINS2 rfp2;
578 WCHAR path[MAX_PATH];
579 HKEY basekey;
581 if (DEVENUM_populate_handle)
582 return S_OK;
583 DEVENUM_populate_handle = CreateEventW(NULL, TRUE, FALSE, DEVENUM_populate_handle_nameW);
584 if (GetLastError() == ERROR_ALREADY_EXISTS)
586 /* Webcams can take some time to scan if the driver is badly written and it enables them,
587 * so have a 10 s timeout here
589 if (WaitForSingleObject(DEVENUM_populate_handle, 10000) == WAIT_TIMEOUT)
590 WARN("Waiting for object timed out\n");
591 TRACE("No need to rescan\n");
592 return S_OK;
594 TRACE("Scanning for devices\n");
596 /* Since devices can change between session, for example because you just plugged in a webcam
597 * or switched from pulseaudio to alsa, delete all old devices first
599 if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_AudioRendererCategory, &basekey, path, MAX_PATH)))
600 RegDeleteTreeW(basekey, path);
601 if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_AudioInputDeviceCategory, &basekey, path, MAX_PATH)))
602 RegDeleteTreeW(basekey, path);
603 if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_VideoInputDeviceCategory, &basekey, path, MAX_PATH)))
604 RegDeleteTreeW(basekey, path);
605 if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_MidiRendererCategory, &basekey, path, MAX_PATH)))
606 RegDeleteTreeW(basekey, path);
608 rf2.dwVersion = 2;
609 rf2.dwMerit = MERIT_PREFERRED;
610 rf2.u.s2.cPins2 = 1;
611 rf2.u.s2.rgPins2 = &rfp2;
612 rfp2.cInstances = 1;
613 rfp2.nMediums = 0;
614 rfp2.lpMedium = NULL;
615 rfp2.clsPinCategory = &IID_NULL;
617 if (!LoadStringW(DEVENUM_hInstance, IDS_DEVENUM_DS, szDSoundNameFormat, sizeof(szDSoundNameFormat)/sizeof(szDSoundNameFormat[0])-1))
619 ERR("Couldn't get string resource (GetLastError() is %d)\n", GetLastError());
620 return HRESULT_FROM_WIN32(GetLastError());
623 res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
624 &IID_IFilterMapper2, (void **) &pMapper);
626 * Fill in info for devices
628 if (SUCCEEDED(res))
630 UINT i;
631 WAVEOUTCAPSW wocaps;
632 WAVEINCAPSW wicaps;
633 MIDIOUTCAPSW mocaps;
634 REGPINTYPES * pTypes;
635 IPropertyBag * pPropBag = NULL;
637 numDevs = waveOutGetNumDevs();
639 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
640 if (FAILED(res)) /* can't register any devices in this category */
641 numDevs = 0;
643 rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
644 for (i = 0; i < numDevs; i++)
646 if (waveOutGetDevCapsW(i, &wocaps, sizeof(WAVEOUTCAPSW))
647 == MMSYSERR_NOERROR)
649 IMoniker * pMoniker = NULL;
651 rfp2.nMediaTypes = 1;
652 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
653 if (!pTypes)
655 IFilterMapper2_Release(pMapper);
656 return E_OUTOFMEMORY;
658 /* FIXME: Native devenum seems to register a lot more types for
659 * DSound than we do. Not sure what purpose they serve */
660 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
661 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
663 rfp2.lpMediaType = pTypes;
665 res = IFilterMapper2_RegisterFilter(pMapper,
666 &CLSID_AudioRender,
667 wocaps.szPname,
668 &pMoniker,
669 &CLSID_AudioRendererCategory,
670 wocaps.szPname,
671 &rf2);
673 if (pMoniker)
675 VARIANT var;
677 V_VT(&var) = VT_I4;
678 V_UNION(&var, ulVal) = i;
679 res = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
680 if (SUCCEEDED(res))
681 res = IPropertyBag_Write(pPropBag, wszWaveOutID, &var);
682 else
683 pPropBag = NULL;
685 V_VT(&var) = VT_LPWSTR;
686 V_UNION(&var, bstrVal) = wocaps.szPname;
687 if (SUCCEEDED(res))
688 res = IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
689 if (pPropBag)
690 IPropertyBag_Release(pPropBag);
691 IMoniker_Release(pMoniker);
692 pMoniker = NULL;
695 wsprintfW(szDSoundName, szDSoundNameFormat, wocaps.szPname);
696 res = IFilterMapper2_RegisterFilter(pMapper,
697 &CLSID_DSoundRender,
698 szDSoundName,
699 &pMoniker,
700 &CLSID_AudioRendererCategory,
701 szDSoundName,
702 &rf2);
704 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
706 if (pMoniker)
707 IMoniker_Release(pMoniker);
709 if (i == iDefaultDevice)
711 FIXME("Default device\n");
714 CoTaskMemFree(pTypes);
718 numDevs = waveInGetNumDevs();
720 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioInputDeviceCategory);
721 if (FAILED(res)) /* can't register any devices in this category */
722 numDevs = 0;
724 rfp2.dwFlags = REG_PINFLAG_B_OUTPUT;
725 for (i = 0; i < numDevs; i++)
727 if (waveInGetDevCapsW(i, &wicaps, sizeof(WAVEINCAPSW))
728 == MMSYSERR_NOERROR)
730 IMoniker * pMoniker = NULL;
732 rfp2.nMediaTypes = 1;
733 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
734 if (!pTypes)
736 IFilterMapper2_Release(pMapper);
737 return E_OUTOFMEMORY;
740 /* FIXME: Not sure if these are correct */
741 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
742 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
744 rfp2.lpMediaType = pTypes;
746 res = IFilterMapper2_RegisterFilter(pMapper,
747 &CLSID_AudioRecord,
748 wicaps.szPname,
749 &pMoniker,
750 &CLSID_AudioInputDeviceCategory,
751 wicaps.szPname,
752 &rf2);
755 if (pMoniker) {
756 VARIANT var;
758 V_VT(&var) = VT_I4;
759 V_UNION(&var, ulVal) = i;
760 res = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
761 if (SUCCEEDED(res))
762 res = IPropertyBag_Write(pPropBag, wszWaveInID, &var);
763 else
764 pPropBag = NULL;
766 V_VT(&var) = VT_LPWSTR;
767 V_UNION(&var, bstrVal) = wicaps.szPname;
768 if (SUCCEEDED(res))
769 res = IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
771 if (pPropBag)
772 IPropertyBag_Release(pPropBag);
773 IMoniker_Release(pMoniker);
776 CoTaskMemFree(pTypes);
780 numDevs = midiOutGetNumDevs();
782 res = DEVENUM_CreateAMCategoryKey(&CLSID_MidiRendererCategory);
783 if (FAILED(res)) /* can't register any devices in this category */
784 numDevs = 0;
786 rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
787 for (i = 0; i < numDevs; i++)
789 if (midiOutGetDevCapsW(i, &mocaps, sizeof(MIDIOUTCAPSW))
790 == MMSYSERR_NOERROR)
792 IMoniker * pMoniker = NULL;
794 rfp2.nMediaTypes = 1;
795 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
796 if (!pTypes)
798 IFilterMapper2_Release(pMapper);
799 return E_OUTOFMEMORY;
802 /* FIXME: Not sure if these are correct */
803 pTypes[0].clsMajorType = &MEDIATYPE_Midi;
804 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
806 rfp2.lpMediaType = pTypes;
808 res = IFilterMapper2_RegisterFilter(pMapper,
809 &CLSID_AVIMIDIRender,
810 mocaps.szPname,
811 &pMoniker,
812 &CLSID_MidiRendererCategory,
813 mocaps.szPname,
814 &rf2);
816 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
817 /* Native version sets MidiOutId */
819 if (pMoniker)
820 IMoniker_Release(pMoniker);
822 if (i == iDefaultDevice)
824 FIXME("Default device\n");
827 CoTaskMemFree(pTypes);
830 res = DEVENUM_CreateAMCategoryKey(&CLSID_VideoInputDeviceCategory);
831 if (SUCCEEDED(res))
832 for (i = 0; i < 10; i++)
834 WCHAR szDeviceName[32], szDeviceVersion[32], szDevicePath[10];
836 if (capGetDriverDescriptionW ((WORD) i,
837 szDeviceName, sizeof(szDeviceName)/sizeof(WCHAR),
838 szDeviceVersion, sizeof(szDeviceVersion)/sizeof(WCHAR)))
840 IMoniker * pMoniker = NULL;
841 WCHAR dprintf[] = { 'v','i','d','e','o','%','d',0 };
842 snprintfW(szDevicePath, sizeof(szDevicePath)/sizeof(WCHAR), dprintf, i);
843 /* The above code prevents 1 device with a different ID overwriting another */
845 rfp2.nMediaTypes = 1;
846 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
847 if (!pTypes) {
848 IFilterMapper2_Release(pMapper);
849 return E_OUTOFMEMORY;
852 pTypes[0].clsMajorType = &MEDIATYPE_Video;
853 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
855 rfp2.lpMediaType = pTypes;
857 res = IFilterMapper2_RegisterFilter(pMapper,
858 &CLSID_VfwCapture,
859 szDeviceName,
860 &pMoniker,
861 &CLSID_VideoInputDeviceCategory,
862 szDevicePath,
863 &rf2);
865 if (pMoniker) {
866 OLECHAR wszVfwIndex[] = { 'V','F','W','I','n','d','e','x',0 };
867 VARIANT var;
868 V_VT(&var) = VT_I4;
869 V_UNION(&var, ulVal) = i;
870 res = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
871 if (SUCCEEDED(res)) {
872 res = IPropertyBag_Write(pPropBag, wszVfwIndex, &var);
873 IPropertyBag_Release(pPropBag);
875 IMoniker_Release(pMoniker);
878 if (i == iDefaultDevice) FIXME("Default device\n");
879 CoTaskMemFree(pTypes);
884 if (pMapper)
885 IFilterMapper2_Release(pMapper);
886 SetEvent(DEVENUM_populate_handle);
887 return res;