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