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