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