If using the default values, also set dwType to REG_SZ as our default
[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
27 #include "devenum_private.h"
28
29 #include "wine/debug.h"
30 #include "mmddk.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
33
34 extern ICOM_VTABLE(IEnumMoniker) IEnumMoniker_Vtbl;
35
36 extern HINSTANCE DEVENUM_hInstance;
37
38 const WCHAR wszInstanceKeyName[] ={'I','n','s','t','a','n','c','e',0};
39 const WCHAR wszRegSeperator[] =   {'\\', 0 };
40 const WCHAR wszActiveMovieKey[] = {'S','o','f','t','w','a','r','e','\\',
41                                    'M','i','c','r','o','s','o','f','t','\\',
42                                    'A','c','t','i','v','e','M','o','v','i','e','\\',
43                                    'd','e','v','e','n','u','m','\\',0};
44
45 static ULONG WINAPI DEVENUM_ICreateDevEnum_AddRef(ICreateDevEnum * iface);
46 static HRESULT DEVENUM_CreateSpecialCategories();
47
48 /**********************************************************************
49  * DEVENUM_ICreateDevEnum_QueryInterface (also IUnknown)
50  */
51 static HRESULT WINAPI DEVENUM_ICreateDevEnum_QueryInterface(
52     ICreateDevEnum * iface,
53     REFIID riid,
54     LPVOID *ppvObj)
55 {
56     ICOM_THIS(CreateDevEnumImpl, iface);
57     TRACE("\n\tIID:\t%s\n",debugstr_guid(riid));
58
59     if (This == NULL || 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     ICOM_THIS(CreateDevEnumImpl, iface);
79     TRACE("\n");
80
81     if (This == NULL) return E_POINTER;
82
83     if (InterlockedIncrement(&This->ref) == 1) {
84         InterlockedIncrement(&dll_ref);
85     }
86     return This->ref;
87 }
88
89 /**********************************************************************
90  * DEVENUM_ICreateDevEnum_Release (also IUnknown)
91  */
92 static ULONG WINAPI DEVENUM_ICreateDevEnum_Release(ICreateDevEnum * iface)
93 {
94     ICOM_THIS(CreateDevEnumImpl, iface);
95     TRACE("\n");
96
97     if (This == NULL) return E_POINTER;
98
99     if (InterlockedDecrement(&This->ref) == 0) {
100         InterlockedDecrement(&dll_ref);
101     }
102     return This->ref;
103 }
104
105 /**********************************************************************
106  * DEVENUM_ICreateDevEnum_CreateClassEnumerator
107  */
108 HRESULT WINAPI DEVENUM_ICreateDevEnum_CreateClassEnumerator(
109     ICreateDevEnum * iface,
110     REFCLSID clsidDeviceClass,
111     IEnumMoniker **ppEnumMoniker,
112     DWORD dwFlags)
113 {
114     WCHAR wszRegKey[MAX_PATH];
115     EnumMonikerImpl * pEnumMoniker;
116     HKEY hkey;
117     HKEY hbasekey;
118     ICOM_THIS(CreateDevEnumImpl, iface);
119
120     TRACE("(%p)->(%s, %p, %lx)\n\tDeviceClass:\t%s\n", This, debugstr_guid(clsidDeviceClass), ppEnumMoniker, dwFlags, debugstr_guid(clsidDeviceClass));
121
122     if (!ppEnumMoniker)
123         return E_POINTER;
124
125     *ppEnumMoniker = NULL;
126
127     if (IsEqualGUID(clsidDeviceClass, &CLSID_AudioRendererCategory) ||
128         IsEqualGUID(clsidDeviceClass, &CLSID_MidiRendererCategory))
129     {
130         if (FAILED(DEVENUM_CreateSpecialCategories()))
131              return E_FAIL;
132
133         hbasekey = HKEY_CURRENT_USER;
134         strcpyW(wszRegKey, wszActiveMovieKey);
135
136         if (!StringFromGUID2(clsidDeviceClass, wszRegKey + strlenW(wszRegKey), MAX_PATH - strlenW(wszRegKey)))
137             return E_OUTOFMEMORY;
138     }
139     else
140     {
141         hbasekey = HKEY_CLASSES_ROOT;
142         strcpyW(wszRegKey, clsid_keyname);
143         strcatW(wszRegKey, wszRegSeperator);
144
145         if (!StringFromGUID2(clsidDeviceClass, wszRegKey + CLSID_STR_LEN, MAX_PATH - CLSID_STR_LEN))
146             return E_OUTOFMEMORY;
147
148         strcatW(wszRegKey, wszRegSeperator);
149         strcatW(wszRegKey, wszInstanceKeyName);
150     }
151
152     if (RegOpenKeyW(hbasekey, wszRegKey, &hkey) != ERROR_SUCCESS)
153     {
154         FIXME("Category %s not found\n", debugstr_guid(clsidDeviceClass));
155         return S_FALSE;
156     }
157
158     pEnumMoniker = (EnumMonikerImpl *)CoTaskMemAlloc(sizeof(EnumMonikerImpl));
159     if (!pEnumMoniker)
160         return E_OUTOFMEMORY;
161
162     pEnumMoniker->lpVtbl = &IEnumMoniker_Vtbl;
163     pEnumMoniker->ref = 1;
164     pEnumMoniker->index = 0;
165     pEnumMoniker->hkey = hkey;
166
167     *ppEnumMoniker = (IEnumMoniker *)pEnumMoniker;
168
169     return S_OK;
170 }
171
172 /**********************************************************************
173  * ICreateDevEnum_Vtbl
174  */
175 static ICOM_VTABLE(ICreateDevEnum) ICreateDevEnum_Vtbl =
176 {
177     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
178     DEVENUM_ICreateDevEnum_QueryInterface,
179     DEVENUM_ICreateDevEnum_AddRef,
180     DEVENUM_ICreateDevEnum_Release,
181     DEVENUM_ICreateDevEnum_CreateClassEnumerator,
182 };
183
184 /**********************************************************************
185  * static CreateDevEnum instance
186  */
187 CreateDevEnumImpl DEVENUM_CreateDevEnum = { &ICreateDevEnum_Vtbl, 0 };
188
189
190 /**********************************************************************
191  * CreateSpecialCategories (INTERNAL)
192  *
193  * Creates the keys in the registry for the dynamic categories
194  */
195 static HRESULT DEVENUM_CreateSpecialCategories()
196 {
197     HRESULT res = S_OK;
198 /* this section below needs some attention - when it is enabled it appears to create
199  * a circular dependency. IE IFilterMapper2_RegisterFilter calls back into this library
200  * which again calls RegisterFilter.
201  */
202 #if 0
203     IMoniker * pMoniker = NULL;
204     WCHAR szAltNameFormat[MAX_PATH + 1];
205     WCHAR szAltName[MAX_PATH + 1];
206     DWORD iDefaultDevice = -1;
207     UINT numDevs;
208     IFilterMapper2 * pMapper = NULL;
209     REGFILTER2 rf2;
210     REGFILTERPINS2 rfp2;
211
212     rf2.dwVersion = 2;
213     rf2.dwMerit = MERIT_PREFERRED;
214     rf2.u.s1.cPins2 = 1;
215     rf2.u.s1.rgPins2 = &rfp2;
216     rfp2.dwFlags = REG_PINFLAG_B_RENDERER;
217     rfp2.cInstances = 1;
218     rfp2.nMediums = 0;
219     rfp2.lpMedium = NULL;
220     rfp2.clsPinCategory = &IID_NULL;
221
222     res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
223                            &IID_IFilterMapper2, (void **) &pMapper);
224     /*
225      * Fill in info for out devices
226      */
227     if (SUCCEEDED(res))
228     {
229         UINT i;
230         WAVEOUTCAPSW wocaps;
231         WAVEINCAPSW wicaps;
232         MIDIOUTCAPSW mocaps;
233         REGPINTYPES * pTypes;
234         numDevs = waveOutGetNumDevs();
235
236         for (i = 0; i < numDevs; i++)
237         {
238             LoadStringW(DEVENUM_hInstance, IDS_DEVENUM_DS, szAltNameFormat, MAX_PATH);
239             if (waveOutGetDevCapsW(i, &wocaps, sizeof(WAVEOUTCAPSW))
240                 == MMSYSERR_NOERROR)
241             {
242                 rfp2.nMediaTypes = 1;
243                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
244                 if (!pTypes)
245                 {
246                     IFilterMapper2_Release(pMapper);
247                     return E_OUTOFMEMORY;
248                 }
249                 /* FIXME: Native devenum seems to register a lot more types for
250                  * DSound than we do. Not sure what purpose they serve */
251                 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
252                 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
253
254                 rfp2.lpMediaType = pTypes;
255
256                 IFilterMapper2_RegisterFilter(pMapper,
257                                               &CLSID_AudioRender,
258                                               wocaps.szPname,
259                                               &pMoniker,
260                                               &CLSID_AudioRendererCategory,
261                                               wocaps.szPname,
262                                               &rf2);
263
264                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
265
266                 if (pMoniker)
267                     IMoniker_Release(pMoniker);
268
269                 wsprintfW(szAltName, szAltNameFormat, wocaps.szPname);
270                 IFilterMapper2_RegisterFilter(pMapper,
271                                               &CLSID_DSoundRender,
272                                               szAltName,
273                                               &pMoniker,
274                                               &CLSID_AudioRendererCategory,
275                                               szAltName,
276                                               &rf2);
277
278                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
279
280                 if (pMoniker)
281                     IMoniker_Release(pMoniker);
282
283                 if (i == iDefaultDevice)
284                 {
285                     FIXME("Default device\n");
286                 }
287
288                 CoTaskMemFree(pTypes);
289             }
290         }
291
292         numDevs = waveInGetNumDevs();
293
294         for (i = 0; i < numDevs; i++)
295         {
296             if (waveInGetDevCapsW(i, &wicaps, sizeof(WAVEINCAPSW))
297                 == MMSYSERR_NOERROR)
298             {
299                 rfp2.nMediaTypes = 1;
300                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
301                 if (!pTypes)
302                 {
303                     IFilterMapper2_Release(pMapper);
304                     return E_OUTOFMEMORY;
305                 }
306
307                 /* FIXME: Not sure if these are correct */
308                 pTypes[0].clsMajorType = &MEDIATYPE_Audio;
309                 pTypes[0].clsMinorType = &MEDIASUBTYPE_PCM;
310
311                 rfp2.lpMediaType = pTypes;
312
313                 IFilterMapper2_RegisterFilter(pMapper,
314                                               &CLSID_AudioRecord,
315                                               wicaps.szPname,
316                                               &pMoniker,
317                                               &CLSID_AudioInputDeviceCategory,
318                                               wicaps.szPname,
319                                               &rf2);
320
321                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
322
323                 if (pMoniker)
324                     IMoniker_Release(pMoniker);
325
326                 CoTaskMemFree(pTypes);
327             }
328         }
329         numDevs = midiOutGetNumDevs();
330
331         for (i = 0; i < numDevs; i++)
332         {
333             if (midiOutGetDevCapsW(i, &mocaps, sizeof(MIDIOUTCAPSW))
334                 == MMSYSERR_NOERROR)
335             {
336                 rfp2.nMediaTypes = 1;
337                 pTypes = CoTaskMemAlloc(rfp2.nMediaTypes * sizeof(REGPINTYPES));
338                 if (!pTypes)
339                 {
340                     IFilterMapper2_Release(pMapper);
341                     return E_OUTOFMEMORY;
342                 }
343
344                 /* FIXME: Not sure if these are correct */
345                 pTypes[0].clsMajorType = &MEDIATYPE_Midi;
346                 pTypes[0].clsMinorType = &MEDIASUBTYPE_None;
347
348                 rfp2.lpMediaType = pTypes;
349
350                 IFilterMapper2_RegisterFilter(pMapper,
351                                               &CLSID_AVIMIDIRender,
352                                               mocaps.szPname,
353                                               &pMoniker,
354                                               &CLSID_MidiRendererCategory,
355                                               mocaps.szPname,
356                                               &rf2);
357
358                 /* FIXME: do additional stuff with IMoniker here, depending on what RegisterFilter does */
359                 /* Native version sets MidiOutId */
360
361                 if (pMoniker)
362                     IMoniker_Release(pMoniker);
363
364                 if (i == iDefaultDevice)
365                 {
366                     FIXME("Default device\n");
367                 }
368
369                 CoTaskMemFree(pTypes);
370             }
371         }
372     }
373
374     if (pMapper)
375         IFilterMapper2_Release(pMapper);
376 #endif
377     return res;
378 }