winsock: Rewrote mappers to use sizeof() based loops.
[wine/wine-kai.git] / dlls / devenum / createdevenum.c
blob15948cfae6676f7b2b3506e146d886225480b0f7
1 /*
2 * ICreateDevEnum implementation for DEVENUM.dll
4 * Copyright (C) 2002 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * NOTES ON THIS FILE:
21 * - Implements ICreateDevEnum interface which creates an IEnumMoniker
22 * implementation
23 * - Also creates the special registry keys created at run-time
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
29 #include "devenum_private.h"
30 #include "vfw.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34 #include "mmddk.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
38 extern HINSTANCE DEVENUM_hInstance;
40 const WCHAR wszInstanceKeyName[] ={'I','n','s','t','a','n','c','e',0};
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};
48 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface);
49 static HRESULT DEVENUM_CreateSpecialCategories(void);
51 /**********************************************************************
52 * DEVENUM_ICreateDevEnum_QueryInterface (also IUnknown)
54 static HRESULT WINAPI DEVENUM_ICreateDevEnum_QueryInterface(
55 ICreateDevEnum * iface,
56 REFIID riid,
57 LPVOID *ppvObj)
59 TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
61 if (ppvObj == NULL) return E_POINTER;
63 if (IsEqualGUID(riid, &IID_IUnknown) ||
64 IsEqualGUID(riid, &IID_ICreateDevEnum))
66 *ppvObj = (LPVOID)iface;
67 DEVENUM_ICreateDevEnum_AddRef(iface);
68 return S_OK;
71 FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
72 return E_NOINTERFACE;
75 /**********************************************************************
76 * DEVENUM_ICreateDevEnum_AddRef (also IUnknown)
78 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface)
80 TRACE("\n");
82 DEVENUM_LockModule();
84 return 2; /* non-heap based object */
87 /**********************************************************************
88 * DEVENUM_ICreateDevEnum_Release (also IUnknown)
90 static ULONG WINAPI DEVENUM_ICreateDevEnum_Release(ICreateDevEnum * iface)
92 TRACE("\n");
94 DEVENUM_UnlockModule();
96 return 1; /* non-heap based object */
99 /**********************************************************************
100 * DEVENUM_ICreateDevEnum_CreateClassEnumerator
102 HRESULT WINAPI DEVENUM_ICreateDevEnum_CreateClassEnumerator(
103 ICreateDevEnum * iface,
104 REFCLSID clsidDeviceClass,
105 IEnumMoniker **ppEnumMoniker,
106 DWORD dwFlags)
108 WCHAR wszRegKey[MAX_PATH];
109 HKEY hkey;
110 HKEY hbasekey;
111 CreateDevEnumImpl *This = (CreateDevEnumImpl *)iface;
113 TRACE("(%p)->(%s, %p, %lx)\n\tDeviceClass:\t%s\n", This, debugstr_guid(clsidDeviceClass), ppEnumMoniker, dwFlags, debugstr_guid(clsidDeviceClass));
115 if (!ppEnumMoniker)
116 return E_POINTER;
118 *ppEnumMoniker = NULL;
120 if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
121 IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
122 IsEqualGUID(clsidDeviceClass, &CLSID_VideoInputDeviceCategory) ||
123 IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
125 hbasekey = HKEY_CURRENT_USER;
126 strcpyW(wszRegKey, wszActiveMovieKey);
128 if (!StringFromGUID2(clsidDeviceClass, wszRegKey + strlenW(wszRegKey), MAX_PATH - strlenW(wszRegKey)))
129 return E_OUTOFMEMORY;
131 else
133 hbasekey = HKEY_CLASSES_ROOT;
134 strcpyW(wszRegKey, clsid_keyname);
135 strcatW(wszRegKey, wszRegSeparator);
137 if (!StringFromGUID2(clsidDeviceClass, wszRegKey + CLSID_STR_LEN, MAX_PATH - CLSID_STR_LEN))
138 return E_OUTOFMEMORY;
140 strcatW(wszRegKey, wszRegSeparator);
141 strcatW(wszRegKey, wszInstanceKeyName);
144 if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
146 if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
147 IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
148 IsEqualGUID(clsidDeviceClass, &CLSID_VideoInputDeviceCategory) ||
149 IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
151 HRESULT hr = DEVENUM_CreateSpecialCategories();
152 if (FAILED(hr))
153 return hr;
154 if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
156 ERR("Couldn't open registry key for special device: %s\n",
157 debugstr_guid(clsidDeviceClass));
158 return S_FALSE;
161 else
163 FIXME("Category %s not found\n", debugstr_guid(clsidDeviceClass));
164 return S_FALSE;
168 return DEVENUM_IEnumMoniker_Construct(hkey, ppEnumMoniker);
171 /**********************************************************************
172 * ICreateDevEnum_Vtbl
174 static const ICreateDevEnumVtbl ICreateDevEnum_Vtbl =
176 DEVENUM_ICreateDevEnum_QueryInterface,
177 DEVENUM_ICreateDevEnum_AddRef,
178 DEVENUM_ICreateDevEnum_Release,
179 DEVENUM_ICreateDevEnum_CreateClassEnumerator,
182 /**********************************************************************
183 * static CreateDevEnum instance
185 CreateDevEnumImpl DEVENUM_CreateDevEnum = { &ICreateDevEnum_Vtbl };
187 /**********************************************************************
188 * DEVENUM_CreateAMCategoryKey (INTERNAL)
190 * Creates a registry key for a category at HKEY_CURRENT_USER\Software\
191 * Microsoft\ActiveMovie\devenum\{clsid}
193 static HRESULT DEVENUM_CreateAMCategoryKey(const CLSID * clsidCategory)
195 WCHAR wszRegKey[MAX_PATH];
196 HRESULT res = S_OK;
197 HKEY hkeyDummy = NULL;
199 strcpyW(wszRegKey, wszActiveMovieKey);
201 if (!StringFromGUID2(clsidCategory, wszRegKey + strlenW(wszRegKey), sizeof(wszRegKey)/sizeof(wszRegKey[0]) - strlenW(wszRegKey)))
202 res = E_INVALIDARG;
204 if (SUCCEEDED(res))
205 res = HRESULT_FROM_WIN32(
206 RegCreateKeyW(HKEY_CURRENT_USER, wszRegKey, &hkeyDummy));
208 if (hkeyDummy)
209 RegCloseKey(hkeyDummy);
211 if (FAILED(res))
212 ERR("Failed to create key HKEY_CURRENT_USER\\%s\n", debugstr_w(wszRegKey));
214 return res;
217 /**********************************************************************
218 * DEVENUM_CreateSpecialCategories (INTERNAL)
220 * Creates the keys in the registry for the dynamic categories
222 static HRESULT DEVENUM_CreateSpecialCategories()
224 HRESULT res;
225 WCHAR szDSoundNameFormat[MAX_PATH + 1];
226 WCHAR szDSoundName[MAX_PATH + 1];
227 DWORD iDefaultDevice = -1;
228 UINT numDevs;
229 IFilterMapper2 * pMapper = NULL;
230 REGFILTER2 rf2;
231 REGFILTERPINS2 rfp2;
233 rf2.dwVersion = 2;
234 rf2.dwMerit = MERIT_PREFERRED;
235 rf2.u.s1.cPins2 = 1;
236 rf2.u.s1.rgPins2 = &rfp2;
237 rfp2.cInstances = 1;
238 rfp2.nMediums = 0;
239 rfp2.lpMedium = NULL;
240 rfp2.clsPinCategory = &IID_NULL;
242 if (!LoadStringW(DEVENUM_hInstance, IDS_DEVENUM_DS, szDSoundNameFormat, sizeof(szDSoundNameFormat)/sizeof(szDSoundNameFormat[0])-1))
244 ERR("Couldn't get string resource (GetLastError() is %ld)\n", GetLastError());
245 return HRESULT_FROM_WIN32(GetLastError());
248 res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
249 &IID_IFilterMapper2, (void **) &pMapper);
251 * Fill in info for devices
253 if (SUCCEEDED(res))
255 UINT i;
256 WAVEOUTCAPSW wocaps;
257 WAVEINCAPSW wicaps;
258 MIDIOUTCAPSW mocaps;
259 REGPINTYPES * pTypes;
261 numDevs = waveOutGetNumDevs();
263 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
264 if (FAILED(res)) /* can't register any devices in this category */
265 numDevs = 0;
267 rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
268 for (i = 0; i < numDevs; i++)
270 if (waveOutGetDevCapsW(i, &wocaps, sizeof(WAVEOUTCAPSW))
271 == MMSYSERR_NOERROR)
273 IMoniker * pMoniker = NULL;
275 rfp2.nMediaTypes = 1;
276 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
277 if (!pTypes)
279 IFilterMapper2_Release(pMapper);
280 return E_OUTOFMEMORY;
282 /* FIXME: Native devenum seems to register a lot more types for
283 * DSound than we do. Not sure what purpose they serve */
284 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
285 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
287 rfp2.lpMediaType = pTypes;
289 res = IFilterMapper2_RegisterFilter(pMapper,
290 &CLSID_AudioRender,
291 wocaps.szPname,
292 &pMoniker,
293 &CLSID_AudioRendererCategory,
294 wocaps.szPname,
295 &rf2);
297 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
299 if (pMoniker)
300 IMoniker_Release(pMoniker);
302 wsprintfW(szDSoundName, szDSoundNameFormat, wocaps.szPname);
303 res = IFilterMapper2_RegisterFilter(pMapper,
304 &CLSID_DSoundRender,
305 szDSoundName,
306 &pMoniker,
307 &CLSID_AudioRendererCategory,
308 szDSoundName,
309 &rf2);
311 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
313 if (pMoniker)
314 IMoniker_Release(pMoniker);
316 if (i == iDefaultDevice)
318 FIXME("Default device\n");
321 CoTaskMemFree(pTypes);
325 numDevs = waveInGetNumDevs();
327 res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioInputDeviceCategory);
328 if (FAILED(res)) /* can't register any devices in this category */
329 numDevs = 0;
331 rfp2.dwFlags = REG_PINFLAG_B_OUTPUT;
332 for (i = 0; i < numDevs; i++)
334 if (waveInGetDevCapsW(i, &wicaps, sizeof(WAVEINCAPSW))
335 == MMSYSERR_NOERROR)
337 IMoniker * pMoniker = NULL;
339 rfp2.nMediaTypes = 1;
340 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
341 if (!pTypes)
343 IFilterMapper2_Release(pMapper);
344 return E_OUTOFMEMORY;
347 /* FIXME: Not sure if these are correct */
348 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
349 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
351 rfp2.lpMediaType = pTypes;
353 res = IFilterMapper2_RegisterFilter(pMapper,
354 &CLSID_AudioRecord,
355 wicaps.szPname,
356 &pMoniker,
357 &CLSID_AudioInputDeviceCategory,
358 wicaps.szPname,
359 &rf2);
361 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
363 if (pMoniker)
364 IMoniker_Release(pMoniker);
366 CoTaskMemFree(pTypes);
370 numDevs = midiOutGetNumDevs();
372 res = DEVENUM_CreateAMCategoryKey(&CLSID_MidiRendererCategory);
373 if (FAILED(res)) /* can't register any devices in this category */
374 numDevs = 0;
376 rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
377 for (i = 0; i < numDevs; i++)
379 if (midiOutGetDevCapsW(i, &mocaps, sizeof(MIDIOUTCAPSW))
380 == MMSYSERR_NOERROR)
382 IMoniker * pMoniker = NULL;
384 rfp2.nMediaTypes = 1;
385 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
386 if (!pTypes)
388 IFilterMapper2_Release(pMapper);
389 return E_OUTOFMEMORY;
392 /* FIXME: Not sure if these are correct */
393 pTypes[0].clsMajorType = &MEDIATYPE_Midi;
394 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
396 rfp2.lpMediaType = pTypes;
398 res = IFilterMapper2_RegisterFilter(pMapper,
399 &CLSID_AVIMIDIRender,
400 mocaps.szPname,
401 &pMoniker,
402 &CLSID_MidiRendererCategory,
403 mocaps.szPname,
404 &rf2);
406 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
407 /* Native version sets MidiOutId */
409 if (pMoniker)
410 IMoniker_Release(pMoniker);
412 if (i == iDefaultDevice)
414 FIXME("Default device\n");
417 CoTaskMemFree(pTypes);
420 res = DEVENUM_CreateAMCategoryKey(&CLSID_VideoInputDeviceCategory);
421 if (SUCCEEDED(res))
422 for (i = 0; i < 10; i++)
424 WCHAR szDeviceName[32], szDeviceVersion[32], szDevicePath[10];
426 if (capGetDriverDescriptionW ((WORD) i,
427 szDeviceName, sizeof(szDeviceName)/sizeof(WCHAR),
428 szDeviceVersion, sizeof(szDeviceVersion)/sizeof(WCHAR)))
430 IMoniker * pMoniker = NULL;
431 IPropertyBag * pPropBag = NULL;
432 WCHAR dprintf[] = { 'v','i','d','e','o','%','d',0 };
433 snprintfW(szDevicePath, sizeof(szDevicePath)/sizeof(WCHAR), dprintf, i);
434 /* The above code prevents 1 device with a different ID overwriting another */
436 rfp2.nMediaTypes = 1;
437 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
438 if (!pTypes) {
439 IFilterMapper2_Release(pMapper);
440 return E_OUTOFMEMORY;
443 pTypes[0].clsMajorType = &MEDIATYPE_Video;
444 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
446 rfp2.lpMediaType = pTypes;
448 res = IFilterMapper2_RegisterFilter(pMapper,
449 &CLSID_VfwCapture,
450 szDeviceName,
451 &pMoniker,
452 &CLSID_VideoInputDeviceCategory,
453 szDevicePath,
454 &rf2);
456 if (pMoniker) {
457 OLECHAR wszVfwIndex[] = { 'V','F','W','I','n','d','e','x',0 };
458 VARIANT var;
459 V_VT(&var) = VT_I4;
460 V_UNION(&var, ulVal) = (ULONG)i;
461 res = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
462 if (SUCCEEDED(res))
463 res = IPropertyBag_Write(pPropBag, wszVfwIndex, &var);
464 IMoniker_Release(pMoniker);
467 if (i == iDefaultDevice) FIXME("Default device\n");
468 CoTaskMemFree(pTypes);
473 if (pMapper)
474 IFilterMapper2_Release(pMapper);
475 return res;