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