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