TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / devenum / createdevenum.c
blob828d661f54191fb7a85f8cd96f9a80214232ae85
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"
31 #include "aviriff.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35 #include "mmddk.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
39 extern HINSTANCE DEVENUM_hInstance;
41 const WCHAR wszInstanceKeyName[] ={'\\','I','n','s','t','a','n','c','e',0};
43 static const WCHAR wszRegSeparator[] = {'\\', 0 };
44 static const WCHAR wszActiveMovieKey[] = {'S','o','f','t','w','a','r','e','\\',
45 'M','i','c','r','o','s','o','f','t','\\',
46 'A','c','t','i','v','e','M','o','v','i','e','\\',
47 'd','e','v','e','n','u','m','\\',0};
48 static const WCHAR wszFilterKeyName[] = {'F','i','l','t','e','r',0};
49 static const WCHAR wszMeritName[] = {'M','e','r','i','t',0};
50 static const WCHAR wszPins[] = {'P','i','n','s',0};
51 static const WCHAR wszAllowedMany[] = {'A','l','l','o','w','e','d','M','a','n','y',0};
52 static const WCHAR wszAllowedZero[] = {'A','l','l','o','w','e','d','Z','e','r','o',0};
53 static const WCHAR wszDirection[] = {'D','i','r','e','c','t','i','o','n',0};
54 static const WCHAR wszIsRendered[] = {'I','s','R','e','n','d','e','r','e','d',0};
55 static const WCHAR wszTypes[] = {'T','y','p','e','s',0};
56 static const WCHAR wszFriendlyName[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
57 static const WCHAR wszWaveInID[] = {'W','a','v','e','I','n','I','D',0};
58 static const WCHAR wszWaveOutID[] = {'W','a','v','e','O','u','t','I','D',0};
60 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface);
61 static HRESULT DEVENUM_CreateSpecialCategories(void);
63 /**********************************************************************
64 * DEVENUM_ICreateDevEnum_QueryInterface (also IUnknown)
66 static HRESULT WINAPI DEVENUM_ICreateDevEnum_QueryInterface(ICreateDevEnum *iface, REFIID riid,
67 void **ppv)
69 TRACE("(%p)->(%s, %p)\n", iface, debugstr_guid(riid), ppv);
71 if (!ppv)
72 return E_POINTER;
74 if (IsEqualGUID(riid, &IID_IUnknown) ||
75 IsEqualGUID(riid, &IID_ICreateDevEnum))
77 *ppv = iface;
78 DEVENUM_ICreateDevEnum_AddRef(iface);
79 return S_OK;
82 FIXME("- no interface IID: %s\n", debugstr_guid(riid));
83 *ppv = NULL;
84 return E_NOINTERFACE;
87 /**********************************************************************
88 * DEVENUM_ICreateDevEnum_AddRef (also IUnknown)
90 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface)
92 TRACE("\n");
94 DEVENUM_LockModule();
96 return 2; /* non-heap based object */
99 /**********************************************************************
100 * DEVENUM_ICreateDevEnum_Release (also IUnknown)
102 static ULONG WINAPI DEVENUM_ICreateDevEnum_Release(ICreateDevEnum * iface)
104 TRACE("\n");
106 DEVENUM_UnlockModule();
108 return 1; /* non-heap based object */
111 static BOOL IsSpecialCategory(const CLSID *clsid)
113 return IsEqualGUID(clsid, &CLSID_AudioRendererCategory) ||
114 IsEqualGUID(clsid, &CLSID_AudioInputDeviceCategory) ||
115 IsEqualGUID(clsid, &CLSID_VideoInputDeviceCategory) ||
116 IsEqualGUID(clsid, &CLSID_VideoCompressorCategory) ||
117 IsEqualGUID(clsid, &CLSID_MidiRendererCategory);
120 HRESULT DEVENUM_GetCategoryKey(REFCLSID clsidDeviceClass, HKEY *pBaseKey, WCHAR *wszRegKeyName, UINT maxLen)
122 if (IsSpecialCategory(clsidDeviceClass))
124 *pBaseKey = HKEY_CURRENT_USER;
125 strcpyW(wszRegKeyName, wszActiveMovieKey);
127 if (!StringFromGUID2(clsidDeviceClass, wszRegKeyName + strlenW(wszRegKeyName), maxLen - strlenW(wszRegKeyName)))
128 return E_OUTOFMEMORY;
130 else
132 *pBaseKey = HKEY_CLASSES_ROOT;
133 strcpyW(wszRegKeyName, clsid_keyname);
134 strcatW(wszRegKeyName, wszRegSeparator);
136 if (!StringFromGUID2(clsidDeviceClass, wszRegKeyName + CLSID_STR_LEN, maxLen - CLSID_STR_LEN))
137 return E_OUTOFMEMORY;
139 strcatW(wszRegKeyName, wszInstanceKeyName);
142 return S_OK;
145 static HKEY open_category_key(const CLSID *clsid)
147 WCHAR key_name[sizeof(wszInstanceKeyName)/sizeof(WCHAR) + CHARS_IN_GUID-1 + 6 /* strlen("CLSID\") */], *ptr;
148 HKEY ret;
150 strcpyW(key_name, clsid_keyname);
151 ptr = key_name + strlenW(key_name);
152 *ptr++ = '\\';
154 if (!StringFromGUID2(clsid, ptr, CHARS_IN_GUID))
155 return NULL;
157 ptr += strlenW(ptr);
158 strcpyW(ptr, wszInstanceKeyName);
160 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, key_name, 0, KEY_READ, &ret) != ERROR_SUCCESS) {
161 WARN("Could not open %s\n", debugstr_w(key_name));
162 return NULL;
165 return ret;
168 static HKEY open_special_category_key(const CLSID *clsid, BOOL create)
170 WCHAR key_name[sizeof(wszActiveMovieKey)/sizeof(WCHAR) + CHARS_IN_GUID-1];
171 HKEY ret;
172 LONG res;
174 strcpyW(key_name, wszActiveMovieKey);
175 if (!StringFromGUID2(clsid, key_name + sizeof(wszActiveMovieKey)/sizeof(WCHAR)-1, CHARS_IN_GUID))
176 return NULL;
178 if(create)
179 res = RegCreateKeyW(HKEY_CURRENT_USER, key_name, &ret);
180 else
181 res = RegOpenKeyExW(HKEY_CURRENT_USER, key_name, 0, KEY_READ, &ret);
182 if (res != ERROR_SUCCESS) {
183 WARN("Could not open %s\n", debugstr_w(key_name));
184 return NULL;
187 return ret;
190 static void DEVENUM_ReadPinTypes(HKEY hkeyPinKey, REGFILTERPINS *rgPin)
192 HKEY hkeyTypes = NULL;
193 DWORD dwMajorTypes, i;
194 REGPINTYPES *lpMediaType = NULL;
195 DWORD dwMediaTypeSize = 0;
197 if (RegOpenKeyExW(hkeyPinKey, wszTypes, 0, KEY_READ, &hkeyTypes) != ERROR_SUCCESS)
198 return ;
200 if (RegQueryInfoKeyW(hkeyTypes, NULL, NULL, NULL, &dwMajorTypes, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
201 != ERROR_SUCCESS)
203 RegCloseKey(hkeyTypes);
204 return ;
207 for (i = 0; i < dwMajorTypes; i++)
209 HKEY hkeyMajorType = NULL;
210 WCHAR wszMajorTypeName[64];
211 DWORD cName = sizeof(wszMajorTypeName) / sizeof(WCHAR);
212 DWORD dwMinorTypes, i1;
214 if (RegEnumKeyExW(hkeyTypes, i, wszMajorTypeName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
216 if (RegOpenKeyExW(hkeyTypes, wszMajorTypeName, 0, KEY_READ, &hkeyMajorType) != ERROR_SUCCESS) continue;
218 if (RegQueryInfoKeyW(hkeyMajorType, NULL, NULL, NULL, &dwMinorTypes, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
219 != ERROR_SUCCESS)
221 RegCloseKey(hkeyMajorType);
222 continue;
225 for (i1 = 0; i1 < dwMinorTypes; i1++)
227 WCHAR wszMinorTypeName[64];
228 CLSID *clsMajorType = NULL, *clsMinorType = NULL;
229 HRESULT hr;
231 cName = sizeof(wszMinorTypeName) / sizeof(WCHAR);
232 if (RegEnumKeyExW(hkeyMajorType, i1, wszMinorTypeName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
234 clsMinorType = CoTaskMemAlloc(sizeof(CLSID));
235 if (!clsMinorType) continue;
237 clsMajorType = CoTaskMemAlloc(sizeof(CLSID));
238 if (!clsMajorType) goto error_cleanup_types;
240 hr = CLSIDFromString(wszMinorTypeName, clsMinorType);
241 if (FAILED(hr)) goto error_cleanup_types;
243 hr = CLSIDFromString(wszMajorTypeName, clsMajorType);
244 if (FAILED(hr)) goto error_cleanup_types;
246 if (rgPin->nMediaTypes == dwMediaTypeSize)
248 DWORD dwNewSize = dwMediaTypeSize + (dwMediaTypeSize < 2 ? 1 : dwMediaTypeSize / 2);
249 REGPINTYPES *lpNewMediaType;
251 lpNewMediaType = CoTaskMemRealloc(lpMediaType, sizeof(REGPINTYPES) * dwNewSize);
252 if (!lpNewMediaType) goto error_cleanup_types;
254 lpMediaType = lpNewMediaType;
255 dwMediaTypeSize = dwNewSize;
258 lpMediaType[rgPin->nMediaTypes].clsMajorType = clsMajorType;
259 lpMediaType[rgPin->nMediaTypes].clsMinorType = clsMinorType;
260 rgPin->nMediaTypes++;
261 continue;
263 error_cleanup_types:
265 if (clsMajorType) CoTaskMemFree(clsMajorType);
266 if (clsMinorType) CoTaskMemFree(clsMinorType);
269 RegCloseKey(hkeyMajorType);
272 RegCloseKey(hkeyTypes);
274 if (lpMediaType && !rgPin->nMediaTypes)
276 CoTaskMemFree(lpMediaType);
277 lpMediaType = NULL;
280 rgPin->lpMediaType = lpMediaType;
283 static void DEVENUM_ReadPins(HKEY hkeyFilterClass, REGFILTER2 *rgf2)
285 HKEY hkeyPins = NULL;
286 DWORD dwPinsSubkeys, i;
287 REGFILTERPINS *rgPins = NULL;
289 if (RegOpenKeyExW(hkeyFilterClass, wszPins, 0, KEY_READ, &hkeyPins) != ERROR_SUCCESS)
290 return ;
292 if (RegQueryInfoKeyW(hkeyPins, NULL, NULL, NULL, &dwPinsSubkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
293 != ERROR_SUCCESS)
295 RegCloseKey(hkeyPins);
296 return ;
299 if (dwPinsSubkeys)
301 rgPins = CoTaskMemAlloc(sizeof(REGFILTERPINS) * dwPinsSubkeys);
302 if (!rgPins)
304 RegCloseKey(hkeyPins);
305 return ;
309 for (i = 0; i < dwPinsSubkeys; i++)
311 HKEY hkeyPinKey = NULL;
312 WCHAR wszPinName[MAX_PATH];
313 DWORD cName = sizeof(wszPinName) / sizeof(WCHAR);
314 DWORD Type, cbData;
315 REGFILTERPINS *rgPin = &rgPins[rgf2->u.s1.cPins];
316 LONG lRet;
318 rgPin->strName = NULL;
319 rgPin->clsConnectsToFilter = &GUID_NULL;
320 rgPin->strConnectsToPin = NULL;
321 rgPin->nMediaTypes = 0;
322 rgPin->lpMediaType = NULL;
324 if (RegEnumKeyExW(hkeyPins, i, wszPinName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
326 if (RegOpenKeyExW(hkeyPins, wszPinName, 0, KEY_READ, &hkeyPinKey) != ERROR_SUCCESS) continue;
328 rgPin->strName = CoTaskMemAlloc((strlenW(wszPinName) + 1) * sizeof(WCHAR));
329 if (!rgPin->strName) goto error_cleanup;
331 strcpyW(rgPin->strName, wszPinName);
333 cbData = sizeof(rgPin->bMany);
334 lRet = RegQueryValueExW(hkeyPinKey, wszAllowedMany, NULL, &Type, (LPBYTE)&rgPin->bMany, &cbData);
335 if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
336 goto error_cleanup;
338 cbData = sizeof(rgPin->bZero);
339 lRet = RegQueryValueExW(hkeyPinKey, wszAllowedZero, NULL, &Type, (LPBYTE)&rgPin->bZero, &cbData);
340 if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
341 goto error_cleanup;
343 cbData = sizeof(rgPin->bOutput);
344 lRet = RegQueryValueExW(hkeyPinKey, wszDirection, NULL, &Type, (LPBYTE)&rgPin->bOutput, &cbData);
345 if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
346 goto error_cleanup;
348 cbData = sizeof(rgPin->bRendered);
349 lRet = RegQueryValueExW(hkeyPinKey, wszIsRendered, NULL, &Type, (LPBYTE)&rgPin->bRendered, &cbData);
350 if (lRet != ERROR_SUCCESS || Type != REG_DWORD)
351 goto error_cleanup;
353 DEVENUM_ReadPinTypes(hkeyPinKey, rgPin);
355 ++rgf2->u.s1.cPins;
356 continue;
358 error_cleanup:
360 RegCloseKey(hkeyPinKey);
361 if (rgPin->strName) CoTaskMemFree(rgPin->strName);
364 RegCloseKey(hkeyPins);
366 if (rgPins && !rgf2->u.s1.cPins)
368 CoTaskMemFree(rgPins);
369 rgPins = NULL;
372 rgf2->u.s1.rgPins = rgPins;
375 static HRESULT DEVENUM_RegisterLegacyAmFilters(void)
377 HKEY hkeyFilter = NULL;
378 DWORD dwFilterSubkeys, i;
379 LONG lRet;
380 IFilterMapper2 *pMapper = NULL;
381 HRESULT hr;
383 hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
384 &IID_IFilterMapper2, (void **) &pMapper);
385 if (SUCCEEDED(hr))
387 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszFilterKeyName, 0, KEY_READ, &hkeyFilter);
388 hr = HRESULT_FROM_WIN32(lRet);
391 if (SUCCEEDED(hr))
393 lRet = RegQueryInfoKeyW(hkeyFilter, NULL, NULL, NULL, &dwFilterSubkeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
394 hr = HRESULT_FROM_WIN32(lRet);
397 if (SUCCEEDED(hr))
399 for (i = 0; i < dwFilterSubkeys; i++)
401 WCHAR wszFilterSubkeyName[64];
402 DWORD cName = sizeof(wszFilterSubkeyName) / sizeof(WCHAR);
403 HKEY hkeyCategoryBaseKey;
404 WCHAR wszRegKey[MAX_PATH];
405 HKEY hkeyInstance = NULL;
407 if (RegEnumKeyExW(hkeyFilter, i, wszFilterSubkeyName, &cName, NULL, NULL, NULL, NULL) != ERROR_SUCCESS) continue;
409 hr = DEVENUM_GetCategoryKey(&CLSID_LegacyAmFilterCategory, &hkeyCategoryBaseKey, wszRegKey, MAX_PATH);
410 if (FAILED(hr)) continue;
412 strcatW(wszRegKey, wszRegSeparator);
413 strcatW(wszRegKey, wszFilterSubkeyName);
415 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszRegKey, 0, KEY_READ, &hkeyInstance) == ERROR_SUCCESS)
417 RegCloseKey(hkeyInstance);
419 else
421 /* Filter is registered the IFilterMapper(1)-way in HKCR\Filter. Needs to be added to
422 * legacy am filter category. */
423 HKEY hkeyFilterClass = NULL;
424 REGFILTER2 rgf2;
425 CLSID clsidFilter;
426 WCHAR wszFilterName[MAX_PATH];
427 DWORD Type;
428 DWORD cbData;
429 HRESULT res;
430 IMoniker *pMoniker = NULL;
432 TRACE("Registering %s\n", debugstr_w(wszFilterSubkeyName));
434 strcpyW(wszRegKey, clsid_keyname);
435 strcatW(wszRegKey, wszRegSeparator);
436 strcatW(wszRegKey, wszFilterSubkeyName);
438 if (RegOpenKeyExW(HKEY_CLASSES_ROOT, wszRegKey, 0, KEY_READ, &hkeyFilterClass) != ERROR_SUCCESS)
439 continue;
441 rgf2.dwVersion = 1;
442 rgf2.dwMerit = 0;
443 rgf2.u.s1.cPins = 0;
444 rgf2.u.s1.rgPins = NULL;
446 cbData = sizeof(wszFilterName);
447 if (RegQueryValueExW(hkeyFilterClass, NULL, NULL, &Type, (LPBYTE)wszFilterName, &cbData) != ERROR_SUCCESS ||
448 Type != REG_SZ)
449 goto cleanup;
451 cbData = sizeof(rgf2.dwMerit);
452 if (RegQueryValueExW(hkeyFilterClass, wszMeritName, NULL, &Type, (LPBYTE)&rgf2.dwMerit, &cbData) != ERROR_SUCCESS ||
453 Type != REG_DWORD)
454 goto cleanup;
456 DEVENUM_ReadPins(hkeyFilterClass, &rgf2);
458 res = CLSIDFromString(wszFilterSubkeyName, &clsidFilter);
459 if (FAILED(res)) goto cleanup;
461 IFilterMapper2_RegisterFilter(pMapper, &clsidFilter, wszFilterName, &pMoniker, NULL, NULL, &rgf2);
463 if (pMoniker)
464 IMoniker_Release(pMoniker);
466 cleanup:
468 if (hkeyFilterClass) RegCloseKey(hkeyFilterClass);
470 if (rgf2.u.s1.rgPins)
472 UINT iPin;
474 for (iPin = 0; iPin < rgf2.u.s1.cPins; iPin++)
476 CoTaskMemFree(rgf2.u.s1.rgPins[iPin].strName);
478 if (rgf2.u.s1.rgPins[iPin].lpMediaType)
480 UINT iType;
482 for (iType = 0; iType < rgf2.u.s1.rgPins[iPin].nMediaTypes; iType++)
484 CoTaskMemFree((void*)rgf2.u.s1.rgPins[iPin].lpMediaType[iType].clsMajorType);
485 CoTaskMemFree((void*)rgf2.u.s1.rgPins[iPin].lpMediaType[iType].clsMinorType);
488 CoTaskMemFree((void*)rgf2.u.s1.rgPins[iPin].lpMediaType);
492 CoTaskMemFree((void*)rgf2.u.s1.rgPins);
498 if (hkeyFilter) RegCloseKey(hkeyFilter);
500 if (pMapper)
501 IFilterMapper2_Release(pMapper);
503 return S_OK;
506 /**********************************************************************
507 * DEVENUM_ICreateDevEnum_CreateClassEnumerator
509 static HRESULT WINAPI DEVENUM_ICreateDevEnum_CreateClassEnumerator(
510 ICreateDevEnum * iface,
511 REFCLSID clsidDeviceClass,
512 IEnumMoniker **ppEnumMoniker,
513 DWORD dwFlags)
515 HKEY hkey, special_hkey = NULL;
516 HRESULT hr;
518 TRACE("(%p)->(%s, %p, %x)\n", iface, debugstr_guid(clsidDeviceClass), ppEnumMoniker, dwFlags);
520 if (!ppEnumMoniker)
521 return E_POINTER;
523 *ppEnumMoniker = NULL;
525 if (IsEqualGUID(clsidDeviceClass, &CLSID_LegacyAmFilterCategory))
527 DEVENUM_RegisterLegacyAmFilters();
530 if (IsSpecialCategory(clsidDeviceClass))
532 hr = DEVENUM_CreateSpecialCategories();
533 if (FAILED(hr))
534 return hr;
536 special_hkey = open_special_category_key(clsidDeviceClass, FALSE);
537 if (!special_hkey)
539 ERR("Couldn't open registry key for special device: %s\n",
540 debugstr_guid(clsidDeviceClass));
541 return S_FALSE;
545 hkey = open_category_key(clsidDeviceClass);
546 if (!hkey && !special_hkey)
548 FIXME("Category %s not found\n", debugstr_guid(clsidDeviceClass));
549 return S_FALSE;
552 return DEVENUM_IEnumMoniker_Construct(hkey, special_hkey, ppEnumMoniker);
555 /**********************************************************************
556 * ICreateDevEnum_Vtbl
558 static const ICreateDevEnumVtbl ICreateDevEnum_Vtbl =
560 DEVENUM_ICreateDevEnum_QueryInterface,
561 DEVENUM_ICreateDevEnum_AddRef,
562 DEVENUM_ICreateDevEnum_Release,
563 DEVENUM_ICreateDevEnum_CreateClassEnumerator,
566 /**********************************************************************
567 * static CreateDevEnum instance
569 ICreateDevEnum DEVENUM_CreateDevEnum = { &ICreateDevEnum_Vtbl };
571 /**********************************************************************
572 * DEVENUM_CreateAMCategoryKey (INTERNAL)
574 * Creates a registry key for a category at HKEY_CURRENT_USER\Software\
575 * Microsoft\ActiveMovie\devenum\{clsid}
577 static HRESULT DEVENUM_CreateAMCategoryKey(const CLSID * clsidCategory)
579 WCHAR wszRegKey[MAX_PATH];
580 HRESULT res = S_OK;
581 HKEY hkeyDummy = NULL;
583 strcpyW(wszRegKey, wszActiveMovieKey);
585 if (!StringFromGUID2(clsidCategory, wszRegKey + strlenW(wszRegKey), sizeof(wszRegKey)/sizeof(wszRegKey[0]) - strlenW(wszRegKey)))
586 res = E_INVALIDARG;
588 if (SUCCEEDED(res))
590 LONG lRes = RegCreateKeyW(HKEY_CURRENT_USER, wszRegKey, &hkeyDummy);
591 res = HRESULT_FROM_WIN32(lRes);
594 if (hkeyDummy)
595 RegCloseKey(hkeyDummy);
597 if (FAILED(res))
598 ERR("Failed to create key HKEY_CURRENT_USER\\%s\n", debugstr_w(wszRegKey));
600 return res;
603 static void register_vfw_codecs(void)
605 WCHAR avico_clsid_str[CHARS_IN_GUID];
606 HKEY basekey, key;
607 ICINFO icinfo;
608 DWORD i, res;
610 static const WCHAR CLSIDW[] = {'C','L','S','I','D',0};
611 static const WCHAR FccHandlerW[] = {'F','c','c','H','a','n','d','l','e','r',0};
612 static const WCHAR FriendlyNameW[] = {'F','r','i','e','n','d','l','y','N','a','m','e',0};
614 StringFromGUID2(&CLSID_AVICo, avico_clsid_str, sizeof(avico_clsid_str)/sizeof(WCHAR));
616 basekey = open_special_category_key(&CLSID_VideoCompressorCategory, TRUE);
617 if(!basekey) {
618 ERR("Could not create key\n");
619 return;
622 for(i=0; ICInfo(FCC('v','i','d','c'), i, &icinfo); i++) {
623 WCHAR fcc_str[5] = {LOBYTE(LOWORD(icinfo.fccHandler)), HIBYTE(LOWORD(icinfo.fccHandler)),
624 LOBYTE(HIWORD(icinfo.fccHandler)), HIBYTE(HIWORD(icinfo.fccHandler))};
626 res = RegCreateKeyW(basekey, fcc_str, &key);
627 if(res != ERROR_SUCCESS)
628 continue;
630 RegSetValueExW(key, CLSIDW, 0, REG_SZ, (const BYTE*)avico_clsid_str, sizeof(avico_clsid_str));
631 RegSetValueExW(key, FccHandlerW, 0, REG_SZ, (const BYTE*)fcc_str, sizeof(fcc_str));
632 RegSetValueExW(key, FriendlyNameW, 0, REG_SZ, (const BYTE*)icinfo.szName, (strlenW(icinfo.szName)+1)*sizeof(WCHAR));
633 /* FIXME: Set ClassManagerFlags and FilterData values */
635 RegCloseKey(key);
638 RegCloseKey(basekey);
641 static HANDLE DEVENUM_populate_handle;
642 static const WCHAR DEVENUM_populate_handle_nameW[] =
643 {'_','_','W','I','N','E','_',
644 'D','e','v','e','n','u','m','_',
645 'P','o','p','u','l','a','t','e',0};
647 /**********************************************************************
648 * DEVENUM_CreateSpecialCategories (INTERNAL)
650 * Creates the keys in the registry for the dynamic categories
652 static HRESULT DEVENUM_CreateSpecialCategories(void)
654 HRESULT res;
655 WCHAR szDSoundNameFormat[MAX_PATH + 1];
656 WCHAR szDSoundName[MAX_PATH + 1];
657 DWORD iDefaultDevice = -1;
658 UINT numDevs;
659 IFilterMapper2 * pMapper = NULL;
660 REGFILTER2 rf2;
661 REGFILTERPINS2 rfp2;
662 WCHAR path[MAX_PATH];
663 HKEY basekey;
665 if (DEVENUM_populate_handle)
666 return S_OK;
667 DEVENUM_populate_handle = CreateEventW(NULL, TRUE, FALSE, DEVENUM_populate_handle_nameW);
668 if (GetLastError() == ERROR_ALREADY_EXISTS)
670 /* Webcams can take some time to scan if the driver is badly written and it enables them,
671 * so have a 10 s timeout here
673 if (WaitForSingleObject(DEVENUM_populate_handle, 10000) == WAIT_TIMEOUT)
674 WARN("Waiting for object timed out\n");
675 TRACE("No need to rescan\n");
676 return S_OK;
678 TRACE("Scanning for devices\n");
680 /* Since devices can change between session, for example because you just plugged in a webcam
681 * or switched from pulseaudio to alsa, delete all old devices first
683 if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_AudioRendererCategory, &basekey, path, MAX_PATH)))
684 RegDeleteTreeW(basekey, path);
685 if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_AudioInputDeviceCategory, &basekey, path, MAX_PATH)))
686 RegDeleteTreeW(basekey, path);
687 if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_VideoInputDeviceCategory, &basekey, path, MAX_PATH)))
688 RegDeleteTreeW(basekey, path);
689 if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_MidiRendererCategory, &basekey, path, MAX_PATH)))
690 RegDeleteTreeW(basekey, path);
691 if (SUCCEEDED(DEVENUM_GetCategoryKey(&CLSID_VideoCompressorCategory, &basekey, path, MAX_PATH)))
692 RegDeleteTreeW(basekey, path);
694 rf2.dwVersion = 2;
695 rf2.dwMerit = MERIT_PREFERRED;
696 rf2.u.s2.cPins2 = 1;
697 rf2.u.s2.rgPins2 = &rfp2;
698 rfp2.cInstances = 1;
699 rfp2.nMediums = 0;
700 rfp2.lpMedium = NULL;
701 rfp2.clsPinCategory = &IID_NULL;
703 if (!LoadStringW(DEVENUM_hInstance, IDS_DEVENUM_DS, szDSoundNameFormat, sizeof(szDSoundNameFormat)/sizeof(szDSoundNameFormat[0])-1))
705 ERR("Couldn't get string resource (GetLastError() is %d)\n", GetLastError());
706 return HRESULT_FROM_WIN32(GetLastError());
709 res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
710 &IID_IFilterMapper2, (void **) &pMapper);
712 * Fill in info for devices
714 if (SUCCEEDED(res))
716 UINT i;
717 WAVEOUTCAPSW wocaps;
718 WAVEINCAPSW wicaps;
719 MIDIOUTCAPSW mocaps;
720 REGPINTYPES * pTypes;
721 IPropertyBag * pPropBag = NULL;
723 numDevs = waveOutGetNumDevs();
725 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
726 if (FAILED(res)) /* can't register any devices in this category */
727 numDevs = 0;
729 rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
730 for (i = 0; i < numDevs; i++)
732 if (waveOutGetDevCapsW(i, &wocaps, sizeof(WAVEOUTCAPSW))
733 == MMSYSERR_NOERROR)
735 IMoniker * pMoniker = NULL;
737 rfp2.nMediaTypes = 1;
738 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
739 if (!pTypes)
741 IFilterMapper2_Release(pMapper);
742 return E_OUTOFMEMORY;
744 /* FIXME: Native devenum seems to register a lot more types for
745 * DSound than we do. Not sure what purpose they serve */
746 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
747 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
749 rfp2.lpMediaType = pTypes;
751 res = IFilterMapper2_RegisterFilter(pMapper,
752 &CLSID_AudioRender,
753 wocaps.szPname,
754 &pMoniker,
755 &CLSID_AudioRendererCategory,
756 wocaps.szPname,
757 &rf2);
759 if (pMoniker)
761 VARIANT var;
763 V_VT(&var) = VT_I4;
764 V_I4(&var) = i;
765 res = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
766 if (SUCCEEDED(res))
767 res = IPropertyBag_Write(pPropBag, wszWaveOutID, &var);
768 else
769 pPropBag = NULL;
771 V_VT(&var) = VT_LPWSTR;
772 V_BSTR(&var) = wocaps.szPname;
773 if (SUCCEEDED(res))
774 res = IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
775 if (pPropBag)
776 IPropertyBag_Release(pPropBag);
777 IMoniker_Release(pMoniker);
778 pMoniker = NULL;
781 wsprintfW(szDSoundName, szDSoundNameFormat, wocaps.szPname);
782 res = IFilterMapper2_RegisterFilter(pMapper,
783 &CLSID_DSoundRender,
784 szDSoundName,
785 &pMoniker,
786 &CLSID_AudioRendererCategory,
787 szDSoundName,
788 &rf2);
790 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
792 if (pMoniker)
793 IMoniker_Release(pMoniker);
795 if (i == iDefaultDevice)
797 FIXME("Default device\n");
800 CoTaskMemFree(pTypes);
804 numDevs = waveInGetNumDevs();
806 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioInputDeviceCategory);
807 if (FAILED(res)) /* can't register any devices in this category */
808 numDevs = 0;
810 rfp2.dwFlags = REG_PINFLAG_B_OUTPUT;
811 for (i = 0; i < numDevs; i++)
813 if (waveInGetDevCapsW(i, &wicaps, sizeof(WAVEINCAPSW))
814 == MMSYSERR_NOERROR)
816 IMoniker * pMoniker = NULL;
818 rfp2.nMediaTypes = 1;
819 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
820 if (!pTypes)
822 IFilterMapper2_Release(pMapper);
823 return E_OUTOFMEMORY;
826 /* FIXME: Not sure if these are correct */
827 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
828 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
830 rfp2.lpMediaType = pTypes;
832 res = IFilterMapper2_RegisterFilter(pMapper,
833 &CLSID_AudioRecord,
834 wicaps.szPname,
835 &pMoniker,
836 &CLSID_AudioInputDeviceCategory,
837 wicaps.szPname,
838 &rf2);
841 if (pMoniker) {
842 VARIANT var;
844 V_VT(&var) = VT_I4;
845 V_I4(&var) = i;
846 res = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
847 if (SUCCEEDED(res))
848 res = IPropertyBag_Write(pPropBag, wszWaveInID, &var);
849 else
850 pPropBag = NULL;
852 V_VT(&var) = VT_LPWSTR;
853 V_BSTR(&var) = wicaps.szPname;
854 if (SUCCEEDED(res))
855 res = IPropertyBag_Write(pPropBag, wszFriendlyName, &var);
857 if (pPropBag)
858 IPropertyBag_Release(pPropBag);
859 IMoniker_Release(pMoniker);
862 CoTaskMemFree(pTypes);
866 numDevs = midiOutGetNumDevs();
868 res = DEVENUM_CreateAMCategoryKey(&CLSID_MidiRendererCategory);
869 if (FAILED(res)) /* can't register any devices in this category */
870 numDevs = 0;
872 rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
873 for (i = 0; i < numDevs; i++)
875 if (midiOutGetDevCapsW(i, &mocaps, sizeof(MIDIOUTCAPSW))
876 == MMSYSERR_NOERROR)
878 IMoniker * pMoniker = NULL;
880 rfp2.nMediaTypes = 1;
881 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
882 if (!pTypes)
884 IFilterMapper2_Release(pMapper);
885 return E_OUTOFMEMORY;
888 /* FIXME: Not sure if these are correct */
889 pTypes[0].clsMajorType = &MEDIATYPE_Midi;
890 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
892 rfp2.lpMediaType = pTypes;
894 res = IFilterMapper2_RegisterFilter(pMapper,
895 &CLSID_AVIMIDIRender,
896 mocaps.szPname,
897 &pMoniker,
898 &CLSID_MidiRendererCategory,
899 mocaps.szPname,
900 &rf2);
902 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
903 /* Native version sets MidiOutId */
905 if (pMoniker)
906 IMoniker_Release(pMoniker);
908 if (i == iDefaultDevice)
910 FIXME("Default device\n");
913 CoTaskMemFree(pTypes);
916 res = DEVENUM_CreateAMCategoryKey(&CLSID_VideoInputDeviceCategory);
917 if (SUCCEEDED(res))
918 for (i = 0; i < 10; i++)
920 WCHAR szDeviceName[32], szDeviceVersion[32], szDevicePath[10];
922 if (capGetDriverDescriptionW ((WORD) i,
923 szDeviceName, sizeof(szDeviceName)/sizeof(WCHAR),
924 szDeviceVersion, sizeof(szDeviceVersion)/sizeof(WCHAR)))
926 IMoniker * pMoniker = NULL;
927 WCHAR dprintf[] = { 'v','i','d','e','o','%','d',0 };
928 snprintfW(szDevicePath, sizeof(szDevicePath)/sizeof(WCHAR), dprintf, i);
929 /* The above code prevents 1 device with a different ID overwriting another */
931 rfp2.nMediaTypes = 1;
932 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
933 if (!pTypes) {
934 IFilterMapper2_Release(pMapper);
935 return E_OUTOFMEMORY;
938 pTypes[0].clsMajorType = &MEDIATYPE_Video;
939 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
941 rfp2.lpMediaType = pTypes;
943 res = IFilterMapper2_RegisterFilter(pMapper,
944 &CLSID_VfwCapture,
945 szDeviceName,
946 &pMoniker,
947 &CLSID_VideoInputDeviceCategory,
948 szDevicePath,
949 &rf2);
951 if (pMoniker) {
952 OLECHAR wszVfwIndex[] = { 'V','F','W','I','n','d','e','x',0 };
953 VARIANT var;
954 V_VT(&var) = VT_I4;
955 V_I4(&var) = i;
956 res = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
957 if (SUCCEEDED(res)) {
958 res = IPropertyBag_Write(pPropBag, wszVfwIndex, &var);
959 IPropertyBag_Release(pPropBag);
961 IMoniker_Release(pMoniker);
964 if (i == iDefaultDevice) FIXME("Default device\n");
965 CoTaskMemFree(pTypes);
970 if (pMapper)
971 IFilterMapper2_Release(pMapper);
973 register_vfw_codecs();
975 SetEvent(DEVENUM_populate_handle);
976 return res;