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