ntdll: Fix a return code from RtlVerifyVersionInfo and change the FIXME to a TRACE...
[wine] / dlls / 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
48 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface);
49 static HRESULT DEVENUM_CreateSpecialCategories(void);
50
51 /**********************************************************************
52  * DEVENUM_ICreateDevEnum_QueryInterface (also IUnknown)
53  */
54 static HRESULT WINAPI DEVENUM_ICreateDevEnum_QueryInterface(
55     ICreateDevEnum * iface,
56     REFIID riid,
57     LPVOID *ppvObj)
58 {
59     TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
60
61     if (ppvObj == NULL) return E_POINTER;
62
63     if (IsEqualGUID(riid, &IID_IUnknown) ||
64         IsEqualGUID(riid, &IID_ICreateDevEnum))
65     {
66         *ppvObj = (LPVOID)iface;
67         DEVENUM_ICreateDevEnum_AddRef(iface);
68         return S_OK;
69     }
70
71     FIXME("- no interface\n\tIID:\t%s\n", debugstr_guid(riid));
72     return E_NOINTERFACE;
73 }
74
75 /**********************************************************************
76  * DEVENUM_ICreateDevEnum_AddRef (also IUnknown)
77  */
78 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface)
79 {
80     TRACE("\n");
81
82     DEVENUM_LockModule();
83
84     return 2; /* non-heap based object */
85 }
86
87 /**********************************************************************
88  * DEVENUM_ICreateDevEnum_Release (also IUnknown)
89  */
90 static ULONG WINAPI DEVENUM_ICreateDevEnum_Release(ICreateDevEnum * iface)
91 {
92     TRACE("\n");
93
94     DEVENUM_UnlockModule();
95
96     return 1; /* non-heap based object */
97 }
98
99 /**********************************************************************
100  * DEVENUM_ICreateDevEnum_CreateClassEnumerator
101  */
102 HRESULT WINAPI DEVENUM_ICreateDevEnum_CreateClassEnumerator(
103     ICreateDevEnum * iface,
104     REFCLSID clsidDeviceClass,
105     IEnumMoniker **ppEnumMoniker,
106     DWORD dwFlags)
107 {
108     WCHAR wszRegKey[MAX_PATH];
109     HKEY hkey;
110     HKEY hbasekey;
111     CreateDevEnumImpl *This = (CreateDevEnumImpl *)iface;
112
113     TRACE("(%p)->(%s, %p, %lx)\n\tDeviceClass:\t%s\n", This, debugstr_guid(clsidDeviceClass), ppEnumMoniker, dwFlags, debugstr_guid(clsidDeviceClass));
114
115     if (!ppEnumMoniker)
116         return E_POINTER;
117
118     *ppEnumMoniker = NULL;
119
120     if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
121         IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
122         IsEqualGUID(clsidDeviceClass, &CLSID_VideoInputDeviceCategory) ||
123         IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
124     {
125         hbasekey = HKEY_CURRENT_USER;
126         strcpyW(wszRegKey, wszActiveMovieKey);
127
128         if (!StringFromGUID2(clsidDeviceClass, wszRegKey + strlenW(wszRegKey), MAX_PATH - strlenW(wszRegKey)))
129             return E_OUTOFMEMORY;
130     }
131     else
132     {
133         hbasekey = HKEY_CLASSES_ROOT;
134         strcpyW(wszRegKey, clsid_keyname);
135         strcatW(wszRegKey, wszRegSeparator);
136
137         if (!StringFromGUID2(clsidDeviceClass, wszRegKey + CLSID_STR_LEN, MAX_PATH - CLSID_STR_LEN))
138             return E_OUTOFMEMORY;
139
140         strcatW(wszRegKey, wszRegSeparator);
141         strcatW(wszRegKey, wszInstanceKeyName);
142     }
143
144     if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
145     {
146         if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
147             IsEqualGUID(clsidDeviceClass, &CLSID_AudioInputDeviceCategory) ||
148             IsEqualGUID(clsidDeviceClass, &CLSID_VideoInputDeviceCategory) ||
149             IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
150         {
151              HRESULT hr = DEVENUM_CreateSpecialCategories();
152              if (FAILED(hr))
153                  return hr;
154              if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
155              {
156                  ERR("Couldn't open registry key for special device: %s\n",
157                      debugstr_guid(clsidDeviceClass));
158                  return S_FALSE;
159              }
160         }
161         else
162         {
163             FIXME("Category %s not found\n", debugstr_guid(clsidDeviceClass));
164             return S_FALSE;
165         }
166     }
167
168     return DEVENUM_IEnumMoniker_Construct(hkey, ppEnumMoniker);
169 }
170
171 /**********************************************************************
172  * ICreateDevEnum_Vtbl
173  */
174 static const ICreateDevEnumVtbl ICreateDevEnum_Vtbl =
175 {
176     DEVENUM_ICreateDevEnum_QueryInterface,
177     DEVENUM_ICreateDevEnum_AddRef,
178     DEVENUM_ICreateDevEnum_Release,
179     DEVENUM_ICreateDevEnum_CreateClassEnumerator,
180 };
181
182 /**********************************************************************
183  * static CreateDevEnum instance
184  */
185 CreateDevEnumImpl DEVENUM_CreateDevEnum = { &ICreateDevEnum_Vtbl };
186
187 /**********************************************************************
188  * DEVENUM_CreateAMCategoryKey (INTERNAL)
189  *
190  * Creates a registry key for a category at HKEY_CURRENT_USER\Software\
191  * Microsoft\ActiveMovie\devenum\{clsid}
192  */
193 static HRESULT DEVENUM_CreateAMCategoryKey(const CLSID * clsidCategory)
194 {
195     WCHAR wszRegKey[MAX_PATH];
196     HRESULT res = S_OK;
197     HKEY hkeyDummy = NULL;
198
199     strcpyW(wszRegKey, wszActiveMovieKey);
200
201     if (!StringFromGUID2(clsidCategory, wszRegKey + strlenW(wszRegKey), sizeof(wszRegKey)/sizeof(wszRegKey[0]) - strlenW(wszRegKey)))
202         res = E_INVALIDARG;
203
204     if (SUCCEEDED(res))
205     {
206         LONG lRes = RegCreateKeyW(HKEY_CURRENT_USER, wszRegKey, &hkeyDummy);
207         res = HRESULT_FROM_WIN32(lRes);
208     }
209
210     if (hkeyDummy)
211         RegCloseKey(hkeyDummy);
212
213     if (FAILED(res))
214         ERR("Failed to create key HKEY_CURRENT_USER\\%s\n", debugstr_w(wszRegKey));
215
216     return res;
217 }
218
219 /**********************************************************************
220  * DEVENUM_CreateSpecialCategories (INTERNAL)
221  *
222  * Creates the keys in the registry for the dynamic categories
223  */
224 static HRESULT DEVENUM_CreateSpecialCategories()
225 {
226     HRESULT res;
227     WCHAR szDSoundNameFormat[MAX_PATH + 1];
228     WCHAR szDSoundName[MAX_PATH + 1];
229     DWORD iDefaultDevice = -1;
230     UINT numDevs;
231     IFilterMapper2 * pMapper = NULL;
232     REGFILTER2 rf2;
233     REGFILTERPINS2 rfp2;
234
235     rf2.dwVersion = 2;
236     rf2.dwMerit = MERIT_PREFERRED;
237     rf2.u.s1.cPins2 = 1;
238     rf2.u.s1.rgPins2 = &rfp2;
239     rfp2.cInstances = 1;
240     rfp2.nMediums = 0;
241     rfp2.lpMedium = NULL;
242     rfp2.clsPinCategory = &IID_NULL;
243
244     if (!LoadStringW(DEVENUM_hInstance, IDS_DEVENUM_DS, szDSoundNameFormat, sizeof(szDSoundNameFormat)/sizeof(szDSoundNameFormat[0])-1))
245     {
246         ERR("Couldn't get string resource (GetLastError() is %ld)\n", GetLastError());
247         return HRESULT_FROM_WIN32(GetLastError());
248     }
249
250     res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
251                            &IID_IFilterMapper2, (void **) &pMapper);
252     /*
253      * Fill in info for devices
254      */
255     if (SUCCEEDED(res))
256     {
257         UINT i;
258         WAVEOUTCAPSW wocaps;
259         WAVEINCAPSW wicaps;
260         MIDIOUTCAPSW mocaps;
261         REGPINTYPES * pTypes;
262
263         numDevs = waveOutGetNumDevs();
264
265         res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioRendererCategory);
266         if (FAILED(res)) /* can't register any devices in this category */
267             numDevs = 0;
268
269         rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
270         for (i = 0; i < numDevs; i++)
271         {
272             if (waveOutGetDevCapsW(i, &wocaps, sizeof(WAVEOUTCAPSW))
273                 == MMSYSERR_NOERROR)
274             {
275                 IMoniker * pMoniker = NULL;
276
277                 rfp2.nMediaTypes = 1;
278                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
279                 if (!pTypes)
280                 {
281                     IFilterMapper2_Release(pMapper);
282                     return E_OUTOFMEMORY;
283                 }
284                 /* FIXME: Native devenum seems to register a lot more types for
285                  * DSound than we do. Not sure what purpose they serve */
286                 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
287                 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
288
289                 rfp2.lpMediaType = pTypes;
290
291                 res = IFilterMapper2_RegisterFilter(pMapper,
292                                               &CLSID_AudioRender,
293                                               wocaps.szPname,
294                                               &pMoniker,
295                                               &CLSID_AudioRendererCategory,
296                                               wocaps.szPname,
297                                               &rf2);
298
299                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
300
301                 if (pMoniker)
302                     IMoniker_Release(pMoniker);
303
304                 wsprintfW(szDSoundName, szDSoundNameFormat, wocaps.szPname);
305                 res = IFilterMapper2_RegisterFilter(pMapper,
306                                               &CLSID_DSoundRender,
307                                               szDSoundName,
308                                               &pMoniker,
309                                               &CLSID_AudioRendererCategory,
310                                               szDSoundName,
311                                               &rf2);
312
313                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
314
315                 if (pMoniker)
316                     IMoniker_Release(pMoniker);
317
318                 if (i == iDefaultDevice)
319                 {
320                     FIXME("Default device\n");
321                 }
322
323                 CoTaskMemFree(pTypes);
324             }
325         }
326
327         numDevs = waveInGetNumDevs();
328
329         res = DEVENUM_CreateAMCategoryKey(&CLSID_AudioInputDeviceCategory);
330         if (FAILED(res)) /* can't register any devices in this category */
331             numDevs = 0;
332
333         rfp2.dwFlags = REG_PINFLAG_B_OUTPUT;
334         for (i = 0; i < numDevs; i++)
335         {
336             if (waveInGetDevCapsW(i, &wicaps, sizeof(WAVEINCAPSW))
337                 == MMSYSERR_NOERROR)
338             {
339                 IMoniker * pMoniker = NULL;
340
341                 rfp2.nMediaTypes = 1;
342                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
343                 if (!pTypes)
344                 {
345                     IFilterMapper2_Release(pMapper);
346                     return E_OUTOFMEMORY;
347                 }
348
349                 /* FIXME: Not sure if these are correct */
350                 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
351                 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
352
353                 rfp2.lpMediaType = pTypes;
354
355                 res = IFilterMapper2_RegisterFilter(pMapper,
356                                               &CLSID_AudioRecord,
357                                               wicaps.szPname,
358                                               &pMoniker,
359                                               &CLSID_AudioInputDeviceCategory,
360                                               wicaps.szPname,
361                                               &rf2);
362
363                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
364
365                 if (pMoniker)
366                     IMoniker_Release(pMoniker);
367
368                 CoTaskMemFree(pTypes);
369             }
370         }
371
372         numDevs = midiOutGetNumDevs();
373
374         res = DEVENUM_CreateAMCategoryKey(&CLSID_MidiRendererCategory);
375         if (FAILED(res)) /* can't register any devices in this category */
376             numDevs = 0;
377
378         rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
379         for (i = 0; i < numDevs; i++)
380         {
381             if (midiOutGetDevCapsW(i, &mocaps, sizeof(MIDIOUTCAPSW))
382                 == MMSYSERR_NOERROR)
383             {
384                 IMoniker * pMoniker = NULL;
385
386                 rfp2.nMediaTypes = 1;
387                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
388                 if (!pTypes)
389                 {
390                     IFilterMapper2_Release(pMapper);
391                     return E_OUTOFMEMORY;
392                 }
393
394                 /* FIXME: Not sure if these are correct */
395                 pTypes[0].clsMajorType = &MEDIATYPE_Midi;
396                 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
397
398                 rfp2.lpMediaType = pTypes;
399
400                 res = IFilterMapper2_RegisterFilter(pMapper,
401                                               &CLSID_AVIMIDIRender,
402                                               mocaps.szPname,
403                                               &pMoniker,
404                                               &CLSID_MidiRendererCategory,
405                                               mocaps.szPname,
406                                               &rf2);
407
408                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
409                 /* Native version sets MidiOutId */
410
411                 if (pMoniker)
412                     IMoniker_Release(pMoniker);
413
414                 if (i == iDefaultDevice)
415                 {
416                     FIXME("Default device\n");
417                 }
418
419                 CoTaskMemFree(pTypes);
420             }
421         }
422         res = DEVENUM_CreateAMCategoryKey(&CLSID_VideoInputDeviceCategory);
423         if (SUCCEEDED(res))
424             for (i = 0; i < 10; i++)
425             {
426                 WCHAR szDeviceName[32], szDeviceVersion[32], szDevicePath[10];
427
428                 if (capGetDriverDescriptionW ((WORD) i,
429                                               szDeviceName, sizeof(szDeviceName)/sizeof(WCHAR),
430                                               szDeviceVersion, sizeof(szDeviceVersion)/sizeof(WCHAR)))
431                 {
432                     IMoniker * pMoniker = NULL;
433                     IPropertyBag * pPropBag = NULL;
434                     WCHAR dprintf[] = { 'v','i','d','e','o','%','d',0 };
435                     snprintfW(szDevicePath, sizeof(szDevicePath)/sizeof(WCHAR), dprintf, i);
436                     /* The above code prevents 1 device with a different ID overwriting another */
437
438                     rfp2.nMediaTypes = 1;
439                     pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
440                     if (!pTypes) {
441                         IFilterMapper2_Release(pMapper);
442                         return E_OUTOFMEMORY;
443                     }
444
445                     pTypes[0].clsMajorType = &MEDIATYPE_Video;
446                     pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
447
448                     rfp2.lpMediaType = pTypes;
449
450                     res = IFilterMapper2_RegisterFilter(pMapper,
451                                                         &CLSID_VfwCapture,
452                                                         szDeviceName,
453                                                         &pMoniker,
454                                                         &CLSID_VideoInputDeviceCategory,
455                                                         szDevicePath,
456                                                         &rf2);
457
458                     if (pMoniker) {
459                        OLECHAR wszVfwIndex[] = { 'V','F','W','I','n','d','e','x',0 };
460                        VARIANT var;
461                        V_VT(&var) = VT_I4;
462                        V_UNION(&var, ulVal) = (ULONG)i;
463                        res = IMoniker_BindToStorage(pMoniker, NULL, NULL, &IID_IPropertyBag, (LPVOID)&pPropBag);
464                        if (SUCCEEDED(res))
465                           res = IPropertyBag_Write(pPropBag, wszVfwIndex, &var);
466                        IMoniker_Release(pMoniker);
467                     }
468
469                     if (i == iDefaultDevice) FIXME("Default device\n");
470                     CoTaskMemFree(pTypes);
471                 }
472             }
473     }
474
475     if (pMapper)
476         IFilterMapper2_Release(pMapper);
477     return res;
478 }