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