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