dbghelp: Implement SymEnumSourceFilesW.
[wine] / dlls / mmdevapi / devenum.c
1 /*
2  * Copyright 2009 Maarten Lankhorst
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22
23 #define NONAMELESSUNION
24 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31
32 #include "ole2.h"
33 #include "mmdeviceapi.h"
34 #include "dshow.h"
35 #include "dsound.h"
36 #include "audioclient.h"
37 #include "endpointvolume.h"
38 #include "audiopolicy.h"
39
40 #include "mmdevapi.h"
41 #include "devpkey.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
44
45 static const WCHAR software_mmdevapi[] =
46     { 'S','o','f','t','w','a','r','e','\\',
47       'M','i','c','r','o','s','o','f','t','\\',
48       'W','i','n','d','o','w','s','\\',
49       'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
50       'M','M','D','e','v','i','c','e','s','\\',
51       'A','u','d','i','o',0};
52 static const WCHAR reg_render[] =
53     { 'R','e','n','d','e','r',0 };
54 static const WCHAR reg_capture[] =
55     { 'C','a','p','t','u','r','e',0 };
56 static const WCHAR reg_devicestate[] =
57     { 'D','e','v','i','c','e','S','t','a','t','e',0 };
58 static const WCHAR reg_properties[] =
59     { 'P','r','o','p','e','r','t','i','e','s',0 };
60
61 static HKEY key_render;
62 static HKEY key_capture;
63
64 typedef struct MMDevPropStoreImpl
65 {
66     IPropertyStore IPropertyStore_iface;
67     LONG ref;
68     MMDevice *parent;
69     DWORD access;
70 } MMDevPropStore;
71
72 typedef struct MMDevEnumImpl
73 {
74     IMMDeviceEnumerator IMMDeviceEnumerator_iface;
75     LONG ref;
76 } MMDevEnumImpl;
77
78 static MMDevEnumImpl *MMDevEnumerator;
79 static MMDevice **MMDevice_head;
80 static MMDevice *MMDevice_def_rec, *MMDevice_def_play;
81 static DWORD MMDevice_count;
82 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl;
83 static const IMMDeviceCollectionVtbl MMDevColVtbl;
84 static const IMMDeviceVtbl MMDeviceVtbl;
85 static const IPropertyStoreVtbl MMDevPropVtbl;
86 static const IMMEndpointVtbl MMEndpointVtbl;
87
88 typedef struct MMDevColImpl
89 {
90     IMMDeviceCollection IMMDeviceCollection_iface;
91     LONG ref;
92     EDataFlow flow;
93     DWORD state;
94 } MMDevColImpl;
95
96 typedef struct IPropertyBagImpl {
97     IPropertyBag IPropertyBag_iface;
98     GUID devguid;
99 } IPropertyBagImpl;
100
101 static const IPropertyBagVtbl PB_Vtbl;
102
103 static HRESULT MMDevPropStore_Create(MMDevice *This, DWORD access, IPropertyStore **ppv);
104
105 static inline MMDevPropStore *impl_from_IPropertyStore(IPropertyStore *iface)
106 {
107     return CONTAINING_RECORD(iface, MMDevPropStore, IPropertyStore_iface);
108 }
109
110 static inline MMDevEnumImpl *impl_from_IMMDeviceEnumerator(IMMDeviceEnumerator *iface)
111 {
112     return CONTAINING_RECORD(iface, MMDevEnumImpl, IMMDeviceEnumerator_iface);
113 }
114
115 static inline MMDevColImpl *impl_from_IMMDeviceCollection(IMMDeviceCollection *iface)
116 {
117     return CONTAINING_RECORD(iface, MMDevColImpl, IMMDeviceCollection_iface);
118 }
119
120 static inline IPropertyBagImpl *impl_from_IPropertyBag(IPropertyBag *iface)
121 {
122     return CONTAINING_RECORD(iface, IPropertyBagImpl, IPropertyBag_iface);
123 }
124
125 static const WCHAR propkey_formatW[] = {
126     '{','%','0','8','X','-','%','0','4','X','-',
127     '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
128     '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
129     '%','0','2','X','%','0','2','X','}',',','%','d',0 };
130
131 static HRESULT MMDevPropStore_OpenPropKey(const GUID *guid, DWORD flow, HKEY *propkey)
132 {
133     WCHAR buffer[39];
134     LONG ret;
135     HKEY key;
136     StringFromGUID2(guid, buffer, 39);
137     if ((ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, buffer, 0, KEY_READ|KEY_WRITE, &key)) != ERROR_SUCCESS)
138     {
139         WARN("Opening key %s failed with %u\n", debugstr_w(buffer), ret);
140         return E_FAIL;
141     }
142     ret = RegOpenKeyExW(key, reg_properties, 0, KEY_READ|KEY_WRITE, propkey);
143     RegCloseKey(key);
144     if (ret != ERROR_SUCCESS)
145     {
146         WARN("Opening key %s failed with %u\n", debugstr_w(reg_properties), ret);
147         return E_FAIL;
148     }
149     return S_OK;
150 }
151
152 HRESULT MMDevice_GetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, PROPVARIANT *pv)
153 {
154     WCHAR buffer[80];
155     const GUID *id = &key->fmtid;
156     DWORD type, size;
157     HRESULT hr = S_OK;
158     HKEY regkey;
159     LONG ret;
160
161     hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
162     if (FAILED(hr))
163         return hr;
164     wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
165                id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
166                id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
167     ret = RegGetValueW(regkey, NULL, buffer, RRF_RT_ANY, &type, NULL, &size);
168     if (ret != ERROR_SUCCESS)
169     {
170         WARN("Reading %s returned %d\n", debugstr_w(buffer), ret);
171         RegCloseKey(regkey);
172         PropVariantClear(pv);
173         return S_OK;
174     }
175
176     switch (type)
177     {
178         case REG_SZ:
179         {
180             pv->vt = VT_LPWSTR;
181             pv->u.pwszVal = CoTaskMemAlloc(size);
182             if (!pv->u.pwszVal)
183                 hr = E_OUTOFMEMORY;
184             else
185                 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_SZ, NULL, (BYTE*)pv->u.pwszVal, &size);
186             break;
187         }
188         case REG_DWORD:
189         {
190             pv->vt = VT_UI4;
191             RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_DWORD, NULL, (BYTE*)&pv->u.ulVal, &size);
192             break;
193         }
194         case REG_BINARY:
195         {
196             pv->vt = VT_BLOB;
197             pv->u.blob.cbSize = size;
198             pv->u.blob.pBlobData = CoTaskMemAlloc(size);
199             if (!pv->u.blob.pBlobData)
200                 hr = E_OUTOFMEMORY;
201             else
202                 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_BINARY, NULL, (BYTE*)pv->u.blob.pBlobData, &size);
203             break;
204         }
205         default:
206             ERR("Unknown/unhandled type: %u\n", type);
207             PropVariantClear(pv);
208             break;
209     }
210     RegCloseKey(regkey);
211     return hr;
212 }
213
214 static HRESULT MMDevice_SetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, REFPROPVARIANT pv)
215 {
216     WCHAR buffer[80];
217     const GUID *id = &key->fmtid;
218     HRESULT hr;
219     HKEY regkey;
220     LONG ret;
221
222     hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
223     if (FAILED(hr))
224         return hr;
225     wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
226                id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
227                id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
228     switch (pv->vt)
229     {
230         case VT_UI4:
231         {
232             ret = RegSetValueExW(regkey, buffer, 0, REG_DWORD, (const BYTE*)&pv->u.ulVal, sizeof(DWORD));
233             break;
234         }
235         case VT_BLOB:
236         {
237             ret = RegSetValueExW(regkey, buffer, 0, REG_BINARY, pv->u.blob.pBlobData, pv->u.blob.cbSize);
238             TRACE("Blob %p %u\n", pv->u.blob.pBlobData, pv->u.blob.cbSize);
239
240             break;
241         }
242         case VT_LPWSTR:
243         {
244             ret = RegSetValueExW(regkey, buffer, 0, REG_SZ, (const BYTE*)pv->u.pwszVal, sizeof(WCHAR)*(1+lstrlenW(pv->u.pwszVal)));
245             break;
246         }
247         default:
248             ret = 0;
249             FIXME("Unhandled type %u\n", pv->vt);
250             hr = E_INVALIDARG;
251             break;
252     }
253     RegCloseKey(regkey);
254     TRACE("Writing %s returned %u\n", debugstr_w(buffer), ret);
255     return hr;
256 }
257
258 /* Creates or updates the state of a device
259  * If GUID is null, a random guid will be assigned
260  * and the device will be created
261  */
262 static MMDevice *MMDevice_Create(WCHAR *name, void *devkey, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault)
263 {
264     HKEY key, root;
265     MMDevice *cur;
266     WCHAR guidstr[39];
267     DWORD i;
268
269     for (i = 0; i < MMDevice_count; ++i)
270     {
271         cur = MMDevice_head[i];
272         if (cur->flow == flow && !lstrcmpW(cur->drv_id, name))
273         {
274             LONG ret;
275             /* Same device, update state */
276             cur->state = state;
277             cur->key = devkey;
278             StringFromGUID2(&cur->devguid, guidstr, sizeof(guidstr)/sizeof(*guidstr));
279             ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, guidstr, 0, KEY_WRITE, &key);
280             if (ret == ERROR_SUCCESS)
281             {
282                 RegSetValueExW(key, reg_devicestate, 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
283                 RegCloseKey(key);
284             }
285             goto done;
286         }
287     }
288
289     /* No device found, allocate new one */
290     cur = HeapAlloc(GetProcessHeap(), 0, sizeof(*cur));
291     if (!cur){
292         HeapFree(GetProcessHeap(), 0, devkey);
293         return NULL;
294     }
295     cur->drv_id = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(name)+1)*sizeof(WCHAR));
296     if (!cur->drv_id)
297     {
298         HeapFree(GetProcessHeap(), 0, cur);
299         HeapFree(GetProcessHeap(), 0, devkey);
300         return NULL;
301     }
302     lstrcpyW(cur->drv_id, name);
303     cur->key = devkey;
304     cur->IMMDevice_iface.lpVtbl = &MMDeviceVtbl;
305     cur->IMMEndpoint_iface.lpVtbl = &MMEndpointVtbl;
306     cur->ref = 0;
307     InitializeCriticalSection(&cur->crst);
308     cur->crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MMDevice.crst");
309     cur->flow = flow;
310     cur->state = state;
311     if (!id)
312     {
313         id = &cur->devguid;
314         CoCreateGuid(id);
315     }
316     cur->devguid = *id;
317     StringFromGUID2(id, guidstr, sizeof(guidstr)/sizeof(*guidstr));
318     if (flow == eRender)
319         root = key_render;
320     else
321         root = key_capture;
322     if (!RegCreateKeyExW(root, guidstr, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &key, NULL))
323     {
324         HKEY keyprop;
325         RegSetValueExW(key, reg_devicestate, 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
326         if (!RegCreateKeyExW(key, reg_properties, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &keyprop, NULL))
327         {
328             PROPVARIANT pv;
329             pv.vt = VT_LPWSTR;
330             pv.u.pwszVal = name;
331             MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
332             MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_DeviceDesc, &pv);
333             RegCloseKey(keyprop);
334         }
335         RegCloseKey(key);
336     }
337     if (!MMDevice_head)
338         MMDevice_head = HeapAlloc(GetProcessHeap(), 0, sizeof(*MMDevice_head));
339     else
340         MMDevice_head = HeapReAlloc(GetProcessHeap(), 0, MMDevice_head, sizeof(*MMDevice_head)*(1+MMDevice_count));
341     MMDevice_head[MMDevice_count++] = cur;
342
343 done:
344     if (setdefault)
345     {
346         if (flow == eRender)
347             MMDevice_def_play = cur;
348         else
349             MMDevice_def_rec = cur;
350     }
351     return cur;
352 }
353
354 static HRESULT load_devices_from_reg(void)
355 {
356     DWORD i = 0;
357     HKEY root, cur;
358     LONG ret;
359     DWORD curflow;
360
361     ret = RegCreateKeyExW(HKEY_LOCAL_MACHINE, software_mmdevapi, 0, NULL, 0, KEY_WRITE|KEY_READ, NULL, &root, NULL);
362     if (ret == ERROR_SUCCESS)
363         ret = RegCreateKeyExW(root, reg_capture, 0, NULL, 0, KEY_READ|KEY_WRITE, NULL, &key_capture, NULL);
364     if (ret == ERROR_SUCCESS)
365         ret = RegCreateKeyExW(root, reg_render, 0, NULL, 0, KEY_READ|KEY_WRITE, NULL, &key_render, NULL);
366     RegCloseKey(root);
367     cur = key_capture;
368     curflow = eCapture;
369     if (ret != ERROR_SUCCESS)
370     {
371         RegCloseKey(key_capture);
372         key_render = key_capture = NULL;
373         WARN("Couldn't create key: %u\n", ret);
374         return E_FAIL;
375     }
376
377     do {
378         WCHAR guidvalue[39];
379         GUID guid;
380         DWORD len;
381         PROPVARIANT pv = { VT_EMPTY };
382
383         len = sizeof(guidvalue)/sizeof(guidvalue[0]);
384         ret = RegEnumKeyExW(cur, i++, guidvalue, &len, NULL, NULL, NULL, NULL);
385         if (ret == ERROR_NO_MORE_ITEMS)
386         {
387             if (cur == key_capture)
388             {
389                 cur = key_render;
390                 curflow = eRender;
391                 i = 0;
392                 continue;
393             }
394             break;
395         }
396         if (ret != ERROR_SUCCESS)
397             continue;
398         if (SUCCEEDED(CLSIDFromString(guidvalue, &guid))
399             && SUCCEEDED(MMDevice_GetPropValue(&guid, curflow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv))
400             && pv.vt == VT_LPWSTR)
401         {
402             MMDevice_Create(pv.u.pwszVal, NULL, &guid, curflow,
403                     DEVICE_STATE_NOTPRESENT, FALSE);
404             CoTaskMemFree(pv.u.pwszVal);
405         }
406     } while (1);
407
408     return S_OK;
409 }
410
411 static HRESULT set_format(MMDevice *dev)
412 {
413     HRESULT hr;
414     IAudioClient *client;
415     WAVEFORMATEX *fmt;
416     PROPVARIANT pv = { VT_EMPTY };
417
418     hr = drvs.pGetAudioEndpoint(dev->key, &dev->IMMDevice_iface, dev->flow, &client);
419     if(FAILED(hr))
420         return hr;
421
422     hr = IAudioClient_GetMixFormat(client, &fmt);
423     if(FAILED(hr)){
424         IAudioClient_Release(client);
425         return hr;
426     }
427
428     IAudioClient_Release(client);
429
430     pv.vt = VT_BLOB;
431     pv.u.blob.cbSize = sizeof(WAVEFORMATEX) + fmt->cbSize;
432     pv.u.blob.pBlobData = (BYTE*)fmt;
433     MMDevice_SetPropValue(&dev->devguid, dev->flow,
434             &PKEY_AudioEngine_DeviceFormat, &pv);
435     MMDevice_SetPropValue(&dev->devguid, dev->flow,
436             &PKEY_AudioEngine_OEMFormat, &pv);
437
438     return S_OK;
439 }
440
441 static HRESULT load_driver_devices(EDataFlow flow)
442 {
443     WCHAR **ids;
444     void **keys;
445     UINT num, def, i;
446     HRESULT hr;
447
448     hr = drvs.pGetEndpointIDs(flow, &ids, &keys, &num, &def);
449     if(FAILED(hr))
450         return hr;
451
452     for(i = 0; i < num; ++i){
453         MMDevice *dev;
454         dev = MMDevice_Create(ids[i], keys[i], NULL, flow, DEVICE_STATE_ACTIVE,
455                 def == i);
456         set_format(dev);
457         HeapFree(GetProcessHeap(), 0, ids[i]);
458     }
459
460     HeapFree(GetProcessHeap(), 0, keys);
461     HeapFree(GetProcessHeap(), 0, ids);
462
463     return S_OK;
464 }
465
466 static void MMDevice_Destroy(MMDevice *This)
467 {
468     DWORD i;
469     TRACE("Freeing %s\n", debugstr_w(This->drv_id));
470     /* Since this function is called at destruction time, reordering of the list is unimportant */
471     for (i = 0; i < MMDevice_count; ++i)
472     {
473         if (MMDevice_head[i] == This)
474         {
475             MMDevice_head[i] = MMDevice_head[--MMDevice_count];
476             break;
477         }
478     }
479     This->crst.DebugInfo->Spare[0] = 0;
480     DeleteCriticalSection(&This->crst);
481     HeapFree(GetProcessHeap(), 0, This->drv_id);
482     HeapFree(GetProcessHeap(), 0, This->key);
483     HeapFree(GetProcessHeap(), 0, This);
484 }
485
486 static inline MMDevice *impl_from_IMMDevice(IMMDevice *iface)
487 {
488     return CONTAINING_RECORD(iface, MMDevice, IMMDevice_iface);
489 }
490
491 static HRESULT WINAPI MMDevice_QueryInterface(IMMDevice *iface, REFIID riid, void **ppv)
492 {
493     MMDevice *This = impl_from_IMMDevice(iface);
494     TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ppv);
495
496     if (!ppv)
497         return E_POINTER;
498     *ppv = NULL;
499     if (IsEqualIID(riid, &IID_IUnknown)
500         || IsEqualIID(riid, &IID_IMMDevice))
501         *ppv = This;
502     else if (IsEqualIID(riid, &IID_IMMEndpoint))
503         *ppv = &This->IMMEndpoint_iface;
504     if (*ppv)
505     {
506         IUnknown_AddRef((IUnknown*)*ppv);
507         return S_OK;
508     }
509     WARN("Unknown interface %s\n", debugstr_guid(riid));
510     return E_NOINTERFACE;
511 }
512
513 static ULONG WINAPI MMDevice_AddRef(IMMDevice *iface)
514 {
515     MMDevice *This = impl_from_IMMDevice(iface);
516     LONG ref;
517
518     ref = InterlockedIncrement(&This->ref);
519     TRACE("Refcount now %i\n", ref);
520     return ref;
521 }
522
523 static ULONG WINAPI MMDevice_Release(IMMDevice *iface)
524 {
525     MMDevice *This = impl_from_IMMDevice(iface);
526     LONG ref;
527
528     ref = InterlockedDecrement(&This->ref);
529     TRACE("Refcount now %i\n", ref);
530     return ref;
531 }
532
533 static HRESULT WINAPI MMDevice_Activate(IMMDevice *iface, REFIID riid, DWORD clsctx, PROPVARIANT *params, void **ppv)
534 {
535     HRESULT hr = E_NOINTERFACE;
536     MMDevice *This = impl_from_IMMDevice(iface);
537
538     TRACE("(%p)->(%p,%x,%p,%p)\n", iface, riid, clsctx, params, ppv);
539
540     if (!ppv)
541         return E_POINTER;
542
543     if (IsEqualIID(riid, &IID_IAudioClient)){
544         hr = drvs.pGetAudioEndpoint(This->key, iface, This->flow, (IAudioClient**)ppv);
545     }else if (IsEqualIID(riid, &IID_IAudioEndpointVolume))
546         hr = AudioEndpointVolume_Create(This, (IAudioEndpointVolume**)ppv);
547     else if (IsEqualIID(riid, &IID_IAudioSessionManager)
548              || IsEqualIID(riid, &IID_IAudioSessionManager2))
549     {
550         FIXME("IID_IAudioSessionManager unsupported\n");
551     }
552     else if (IsEqualIID(riid, &IID_IBaseFilter))
553     {
554         if (This->flow == eRender)
555             hr = CoCreateInstance(&CLSID_DSoundRender, NULL, clsctx, riid, ppv);
556         else
557             ERR("Not supported for recording?\n");
558         if (SUCCEEDED(hr))
559         {
560             IPersistPropertyBag *ppb;
561             hr = IUnknown_QueryInterface((IUnknown*)*ppv, &IID_IPersistPropertyBag, (void*)&ppb);
562             if (SUCCEEDED(hr))
563             {
564                 /* ::Load cannot assume the interface stays alive after the function returns,
565                  * so just create the interface on the stack, saves a lot of complicated code */
566                 IPropertyBagImpl bag = { { &PB_Vtbl }, This->devguid };
567                 hr = IPersistPropertyBag_Load(ppb, &bag.IPropertyBag_iface, NULL);
568                 IPersistPropertyBag_Release(ppb);
569                 if (FAILED(hr))
570                     IBaseFilter_Release((IBaseFilter*)*ppv);
571             }
572             else
573             {
574                 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
575                 hr = S_OK;
576             }
577         }
578     }
579     else if (IsEqualIID(riid, &IID_IDeviceTopology))
580     {
581         FIXME("IID_IDeviceTopology unsupported\n");
582     }
583     else if (IsEqualIID(riid, &IID_IDirectSound)
584              || IsEqualIID(riid, &IID_IDirectSound8))
585     {
586         if (This->flow == eRender)
587             hr = CoCreateInstance(&CLSID_DirectSound8, NULL, clsctx, riid, ppv);
588         if (SUCCEEDED(hr))
589         {
590             hr = IDirectSound_Initialize((IDirectSound*)*ppv, &This->devguid);
591             if (FAILED(hr))
592                 IDirectSound_Release((IDirectSound*)*ppv);
593         }
594     }
595     else if (IsEqualIID(riid, &IID_IDirectSoundCapture)
596              || IsEqualIID(riid, &IID_IDirectSoundCapture8))
597     {
598         if (This->flow == eCapture)
599             hr = CoCreateInstance(&CLSID_DirectSoundCapture8, NULL, clsctx, riid, ppv);
600         if (SUCCEEDED(hr))
601         {
602             hr = IDirectSoundCapture_Initialize((IDirectSoundCapture*)*ppv, &This->devguid);
603             if (FAILED(hr))
604                 IDirectSoundCapture_Release((IDirectSoundCapture*)*ppv);
605         }
606     }
607     else
608         ERR("Invalid/unknown iid %s\n", debugstr_guid(riid));
609
610     if (FAILED(hr))
611         *ppv = NULL;
612
613     TRACE("Returning %08x\n", hr);
614     return hr;
615 }
616
617 static HRESULT WINAPI MMDevice_OpenPropertyStore(IMMDevice *iface, DWORD access, IPropertyStore **ppv)
618 {
619     MMDevice *This = impl_from_IMMDevice(iface);
620     TRACE("(%p)->(%x,%p)\n", This, access, ppv);
621
622     if (!ppv)
623         return E_POINTER;
624     return MMDevPropStore_Create(This, access, ppv);
625 }
626
627 static HRESULT WINAPI MMDevice_GetId(IMMDevice *iface, WCHAR **itemid)
628 {
629     MMDevice *This = impl_from_IMMDevice(iface);
630     WCHAR *str;
631     GUID *id = &This->devguid;
632     static const WCHAR formatW[] = { '{','0','.','0','.','0','.','0','0','0','0','0','0','0','0','}','.',
633                                      '{','%','0','8','X','-','%','0','4','X','-',
634                                      '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
635                                      '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
636                                      '%','0','2','X','%','0','2','X','}',0 };
637
638     TRACE("(%p)->(%p)\n", This, itemid);
639     if (!itemid)
640         return E_POINTER;
641     *itemid = str = CoTaskMemAlloc(56 * sizeof(WCHAR));
642     if (!str)
643         return E_OUTOFMEMORY;
644     wsprintfW( str, formatW, id->Data1, id->Data2, id->Data3,
645                id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
646                id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
647     return S_OK;
648 }
649
650 static HRESULT WINAPI MMDevice_GetState(IMMDevice *iface, DWORD *state)
651 {
652     MMDevice *This = impl_from_IMMDevice(iface);
653     TRACE("(%p)->(%p)\n", iface, state);
654
655     if (!state)
656         return E_POINTER;
657     *state = This->state;
658     return S_OK;
659 }
660
661 static const IMMDeviceVtbl MMDeviceVtbl =
662 {
663     MMDevice_QueryInterface,
664     MMDevice_AddRef,
665     MMDevice_Release,
666     MMDevice_Activate,
667     MMDevice_OpenPropertyStore,
668     MMDevice_GetId,
669     MMDevice_GetState
670 };
671
672 static inline MMDevice *impl_from_IMMEndpoint(IMMEndpoint *iface)
673 {
674     return CONTAINING_RECORD(iface, MMDevice, IMMEndpoint_iface);
675 }
676
677 static HRESULT WINAPI MMEndpoint_QueryInterface(IMMEndpoint *iface, REFIID riid, void **ppv)
678 {
679     MMDevice *This = impl_from_IMMEndpoint(iface);
680     return IMMDevice_QueryInterface(&This->IMMDevice_iface, riid, ppv);
681 }
682
683 static ULONG WINAPI MMEndpoint_AddRef(IMMEndpoint *iface)
684 {
685     MMDevice *This = impl_from_IMMEndpoint(iface);
686     return IMMDevice_AddRef(&This->IMMDevice_iface);
687 }
688
689 static ULONG WINAPI MMEndpoint_Release(IMMEndpoint *iface)
690 {
691     MMDevice *This = impl_from_IMMEndpoint(iface);
692     return IMMDevice_Release(&This->IMMDevice_iface);
693 }
694
695 static HRESULT WINAPI MMEndpoint_GetDataFlow(IMMEndpoint *iface, EDataFlow *flow)
696 {
697     MMDevice *This = impl_from_IMMEndpoint(iface);
698     if (!flow)
699         return E_POINTER;
700     *flow = This->flow;
701     return S_OK;
702 }
703
704 static const IMMEndpointVtbl MMEndpointVtbl =
705 {
706     MMEndpoint_QueryInterface,
707     MMEndpoint_AddRef,
708     MMEndpoint_Release,
709     MMEndpoint_GetDataFlow
710 };
711
712 static HRESULT MMDevCol_Create(IMMDeviceCollection **ppv, EDataFlow flow, DWORD state)
713 {
714     MMDevColImpl *This;
715
716     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
717     *ppv = NULL;
718     if (!This)
719         return E_OUTOFMEMORY;
720     This->IMMDeviceCollection_iface.lpVtbl = &MMDevColVtbl;
721     This->ref = 1;
722     This->flow = flow;
723     This->state = state;
724     *ppv = &This->IMMDeviceCollection_iface;
725     return S_OK;
726 }
727
728 static void MMDevCol_Destroy(MMDevColImpl *This)
729 {
730     HeapFree(GetProcessHeap(), 0, This);
731 }
732
733 static HRESULT WINAPI MMDevCol_QueryInterface(IMMDeviceCollection *iface, REFIID riid, void **ppv)
734 {
735     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
736
737     if (!ppv)
738         return E_POINTER;
739     if (IsEqualIID(riid, &IID_IUnknown)
740         || IsEqualIID(riid, &IID_IMMDeviceCollection))
741         *ppv = This;
742     else
743         *ppv = NULL;
744     if (!*ppv)
745         return E_NOINTERFACE;
746     IUnknown_AddRef((IUnknown*)*ppv);
747     return S_OK;
748 }
749
750 static ULONG WINAPI MMDevCol_AddRef(IMMDeviceCollection *iface)
751 {
752     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
753     LONG ref = InterlockedIncrement(&This->ref);
754     TRACE("Refcount now %i\n", ref);
755     return ref;
756 }
757
758 static ULONG WINAPI MMDevCol_Release(IMMDeviceCollection *iface)
759 {
760     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
761     LONG ref = InterlockedDecrement(&This->ref);
762     TRACE("Refcount now %i\n", ref);
763     if (!ref)
764         MMDevCol_Destroy(This);
765     return ref;
766 }
767
768 static HRESULT WINAPI MMDevCol_GetCount(IMMDeviceCollection *iface, UINT *numdevs)
769 {
770     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
771     DWORD i;
772
773     TRACE("(%p)->(%p)\n", This, numdevs);
774     if (!numdevs)
775         return E_POINTER;
776
777     *numdevs = 0;
778     for (i = 0; i < MMDevice_count; ++i)
779     {
780         MMDevice *cur = MMDevice_head[i];
781         if ((cur->flow == This->flow || This->flow == eAll)
782             && (cur->state & This->state))
783             ++(*numdevs);
784     }
785     return S_OK;
786 }
787
788 static HRESULT WINAPI MMDevCol_Item(IMMDeviceCollection *iface, UINT n, IMMDevice **dev)
789 {
790     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
791     DWORD i = 0, j = 0;
792
793     TRACE("(%p)->(%u, %p)\n", This, n, dev);
794     if (!dev)
795         return E_POINTER;
796
797     for (j = 0; j < MMDevice_count; ++j)
798     {
799         MMDevice *cur = MMDevice_head[j];
800         if ((cur->flow == This->flow || This->flow == eAll)
801             && (cur->state & This->state)
802             && i++ == n)
803         {
804             *dev = &cur->IMMDevice_iface;
805             IMMDevice_AddRef(*dev);
806             return S_OK;
807         }
808     }
809     WARN("Could not obtain item %u\n", n);
810     *dev = NULL;
811     return E_INVALIDARG;
812 }
813
814 static const IMMDeviceCollectionVtbl MMDevColVtbl =
815 {
816     MMDevCol_QueryInterface,
817     MMDevCol_AddRef,
818     MMDevCol_Release,
819     MMDevCol_GetCount,
820     MMDevCol_Item
821 };
822
823 HRESULT MMDevEnum_Create(REFIID riid, void **ppv)
824 {
825     MMDevEnumImpl *This = MMDevEnumerator;
826
827     if (!This)
828     {
829         This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
830         *ppv = NULL;
831         if (!This)
832             return E_OUTOFMEMORY;
833         This->ref = 1;
834         This->IMMDeviceEnumerator_iface.lpVtbl = &MMDevEnumVtbl;
835         MMDevEnumerator = This;
836
837         load_devices_from_reg();
838         load_driver_devices(eRender);
839         load_driver_devices(eCapture);
840     }
841     return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
842 }
843
844 void MMDevEnum_Free(void)
845 {
846     while (MMDevice_count)
847         MMDevice_Destroy(MMDevice_head[0]);
848     RegCloseKey(key_render);
849     RegCloseKey(key_capture);
850     key_render = key_capture = NULL;
851     HeapFree(GetProcessHeap(), 0, MMDevEnumerator);
852     MMDevEnumerator = NULL;
853 }
854
855 static HRESULT WINAPI MMDevEnum_QueryInterface(IMMDeviceEnumerator *iface, REFIID riid, void **ppv)
856 {
857     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
858
859     if (!ppv)
860         return E_POINTER;
861     if (IsEqualIID(riid, &IID_IUnknown)
862         || IsEqualIID(riid, &IID_IMMDeviceEnumerator))
863         *ppv = This;
864     else
865         *ppv = NULL;
866     if (!*ppv)
867         return E_NOINTERFACE;
868     IUnknown_AddRef((IUnknown*)*ppv);
869     return S_OK;
870 }
871
872 static ULONG WINAPI MMDevEnum_AddRef(IMMDeviceEnumerator *iface)
873 {
874     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
875     LONG ref = InterlockedIncrement(&This->ref);
876     TRACE("Refcount now %i\n", ref);
877     return ref;
878 }
879
880 static ULONG WINAPI MMDevEnum_Release(IMMDeviceEnumerator *iface)
881 {
882     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
883     LONG ref = InterlockedDecrement(&This->ref);
884     if (!ref)
885         MMDevEnum_Free();
886     TRACE("Refcount now %i\n", ref);
887     return ref;
888 }
889
890 static HRESULT WINAPI MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator *iface, EDataFlow flow, DWORD mask, IMMDeviceCollection **devices)
891 {
892     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
893     TRACE("(%p)->(%u,%u,%p)\n", This, flow, mask, devices);
894     if (!devices)
895         return E_POINTER;
896     *devices = NULL;
897     if (flow >= EDataFlow_enum_count)
898         return E_INVALIDARG;
899     if (mask & ~DEVICE_STATEMASK_ALL)
900         return E_INVALIDARG;
901     return MMDevCol_Create(devices, flow, mask);
902 }
903
904 static HRESULT WINAPI MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator *iface, EDataFlow flow, ERole role, IMMDevice **device)
905 {
906     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
907     TRACE("(%p)->(%u,%u,%p)\n", This, flow, role, device);
908
909     if (!device)
910         return E_POINTER;
911     *device = NULL;
912
913     if (flow == eRender)
914         *device = &MMDevice_def_play->IMMDevice_iface;
915     else if (flow == eCapture)
916         *device = &MMDevice_def_rec->IMMDevice_iface;
917     else
918     {
919         WARN("Unknown flow %u\n", flow);
920         return E_INVALIDARG;
921     }
922
923     if (!*device)
924         return E_NOTFOUND;
925     IMMDevice_AddRef(*device);
926     return S_OK;
927 }
928
929 static HRESULT WINAPI MMDevEnum_GetDevice(IMMDeviceEnumerator *iface, const WCHAR *name, IMMDevice **device)
930 {
931     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
932     DWORD i=0;
933     IMMDevice *dev = NULL;
934
935     if(!name || !device)
936         return E_POINTER;
937
938     TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), device);
939     for (i = 0; i < MMDevice_count; ++i)
940     {
941         WCHAR *str;
942         dev = &MMDevice_head[i]->IMMDevice_iface;
943         IMMDevice_GetId(dev, &str);
944
945         if (str && !lstrcmpW(str, name))
946         {
947             CoTaskMemFree(str);
948             IUnknown_AddRef(dev);
949             *device = dev;
950             return S_OK;
951         }
952         CoTaskMemFree(str);
953     }
954     TRACE("Could not find device %s\n", debugstr_w(name));
955     return E_INVALIDARG;
956 }
957
958 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
959 {
960     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
961     TRACE("(%p)->(%p)\n", This, client);
962     FIXME("stub\n");
963     return S_OK;
964 }
965
966 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
967 {
968     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
969     TRACE("(%p)->(%p)\n", This, client);
970     FIXME("stub\n");
971     return S_OK;
972 }
973
974 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl =
975 {
976     MMDevEnum_QueryInterface,
977     MMDevEnum_AddRef,
978     MMDevEnum_Release,
979     MMDevEnum_EnumAudioEndpoints,
980     MMDevEnum_GetDefaultAudioEndpoint,
981     MMDevEnum_GetDevice,
982     MMDevEnum_RegisterEndpointNotificationCallback,
983     MMDevEnum_UnregisterEndpointNotificationCallback
984 };
985
986 static HRESULT MMDevPropStore_Create(MMDevice *parent, DWORD access, IPropertyStore **ppv)
987 {
988     MMDevPropStore *This;
989     if (access != STGM_READ
990         && access != STGM_WRITE
991         && access != STGM_READWRITE)
992     {
993         WARN("Invalid access %08x\n", access);
994         return E_INVALIDARG;
995     }
996     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
997     *ppv = &This->IPropertyStore_iface;
998     if (!This)
999         return E_OUTOFMEMORY;
1000     This->IPropertyStore_iface.lpVtbl = &MMDevPropVtbl;
1001     This->ref = 1;
1002     This->parent = parent;
1003     This->access = access;
1004     return S_OK;
1005 }
1006
1007 static void MMDevPropStore_Destroy(MMDevPropStore *This)
1008 {
1009     HeapFree(GetProcessHeap(), 0, This);
1010 }
1011
1012 static HRESULT WINAPI MMDevPropStore_QueryInterface(IPropertyStore *iface, REFIID riid, void **ppv)
1013 {
1014     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1015
1016     if (!ppv)
1017         return E_POINTER;
1018     if (IsEqualIID(riid, &IID_IUnknown)
1019         || IsEqualIID(riid, &IID_IPropertyStore))
1020         *ppv = This;
1021     else
1022         *ppv = NULL;
1023     if (!*ppv)
1024         return E_NOINTERFACE;
1025     IUnknown_AddRef((IUnknown*)*ppv);
1026     return S_OK;
1027 }
1028
1029 static ULONG WINAPI MMDevPropStore_AddRef(IPropertyStore *iface)
1030 {
1031     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1032     LONG ref = InterlockedIncrement(&This->ref);
1033     TRACE("Refcount now %i\n", ref);
1034     return ref;
1035 }
1036
1037 static ULONG WINAPI MMDevPropStore_Release(IPropertyStore *iface)
1038 {
1039     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1040     LONG ref = InterlockedDecrement(&This->ref);
1041     TRACE("Refcount now %i\n", ref);
1042     if (!ref)
1043         MMDevPropStore_Destroy(This);
1044     return ref;
1045 }
1046
1047 static HRESULT WINAPI MMDevPropStore_GetCount(IPropertyStore *iface, DWORD *nprops)
1048 {
1049     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1050     WCHAR buffer[50];
1051     DWORD i = 0;
1052     HKEY propkey;
1053     HRESULT hr;
1054
1055     TRACE("(%p)->(%p)\n", iface, nprops);
1056     if (!nprops)
1057         return E_POINTER;
1058     hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1059     if (FAILED(hr))
1060         return hr;
1061     *nprops = 0;
1062     do {
1063         DWORD len = sizeof(buffer)/sizeof(*buffer);
1064         if (RegEnumKeyExW(propkey, i, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
1065             break;
1066         i++;
1067     } while (0);
1068     RegCloseKey(propkey);
1069     TRACE("Returning %i\n", i);
1070     *nprops = i;
1071     return S_OK;
1072 }
1073
1074 static HRESULT WINAPI MMDevPropStore_GetAt(IPropertyStore *iface, DWORD prop, PROPERTYKEY *key)
1075 {
1076     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1077     WCHAR buffer[50];
1078     DWORD len = sizeof(buffer)/sizeof(*buffer);
1079     HRESULT hr;
1080     HKEY propkey;
1081
1082     TRACE("(%p)->(%u,%p)\n", iface, prop, key);
1083     if (!key)
1084         return E_POINTER;
1085
1086     hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1087     if (FAILED(hr))
1088         return hr;
1089
1090     if (RegEnumKeyExW(propkey, prop, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS
1091         || len <= 40)
1092     {
1093         WARN("GetAt %u failed\n", prop);
1094         return E_INVALIDARG;
1095     }
1096     RegCloseKey(propkey);
1097     buffer[39] = 0;
1098     CLSIDFromString(buffer, &key->fmtid);
1099     key->pid = atoiW(&buffer[40]);
1100     return S_OK;
1101 }
1102
1103 static HRESULT WINAPI MMDevPropStore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *pv)
1104 {
1105     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1106     TRACE("(%p)->(\"%s,%u\", %p\n", This, debugstr_guid(&key->fmtid), key ? key->pid : 0, pv);
1107
1108     if (!key || !pv)
1109         return E_POINTER;
1110     if (This->access != STGM_READ
1111         && This->access != STGM_READWRITE)
1112         return STG_E_ACCESSDENIED;
1113
1114     /* Special case */
1115     if (IsEqualPropertyKey(*key, PKEY_AudioEndpoint_GUID))
1116     {
1117         pv->vt = VT_LPWSTR;
1118         pv->u.pwszVal = CoTaskMemAlloc(39 * sizeof(WCHAR));
1119         if (!pv->u.pwszVal)
1120             return E_OUTOFMEMORY;
1121         StringFromGUID2(&This->parent->devguid, pv->u.pwszVal, 39);
1122         return S_OK;
1123     }
1124
1125     return MMDevice_GetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1126 }
1127
1128 static HRESULT WINAPI MMDevPropStore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT pv)
1129 {
1130     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1131
1132     if (!key || !pv)
1133         return E_POINTER;
1134
1135     if (This->access != STGM_WRITE
1136         && This->access != STGM_READWRITE)
1137         return STG_E_ACCESSDENIED;
1138     return MMDevice_SetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1139 }
1140
1141 static HRESULT WINAPI MMDevPropStore_Commit(IPropertyStore *iface)
1142 {
1143     FIXME("stub\n");
1144     return E_NOTIMPL;
1145 }
1146
1147 static const IPropertyStoreVtbl MMDevPropVtbl =
1148 {
1149     MMDevPropStore_QueryInterface,
1150     MMDevPropStore_AddRef,
1151     MMDevPropStore_Release,
1152     MMDevPropStore_GetCount,
1153     MMDevPropStore_GetAt,
1154     MMDevPropStore_GetValue,
1155     MMDevPropStore_SetValue,
1156     MMDevPropStore_Commit
1157 };
1158
1159
1160 /* Property bag for IBaseFilter activation */
1161 static HRESULT WINAPI PB_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
1162 {
1163     ERR("Should not be called\n");
1164     *ppv = NULL;
1165     return E_NOINTERFACE;
1166 }
1167
1168 static ULONG WINAPI PB_AddRef(IPropertyBag *iface)
1169 {
1170     ERR("Should not be called\n");
1171     return 2;
1172 }
1173
1174 static ULONG WINAPI PB_Release(IPropertyBag *iface)
1175 {
1176     ERR("Should not be called\n");
1177     return 1;
1178 }
1179
1180 static HRESULT WINAPI PB_Read(IPropertyBag *iface, LPCOLESTR name, VARIANT *var, IErrorLog *log)
1181 {
1182     static const WCHAR dsguid[] = { 'D','S','G','u','i','d', 0 };
1183     IPropertyBagImpl *This = impl_from_IPropertyBag(iface);
1184     TRACE("Trying to read %s, type %u\n", debugstr_w(name), var->n1.n2.vt);
1185     if (!lstrcmpW(name, dsguid))
1186     {
1187         WCHAR guidstr[39];
1188         StringFromGUID2(&This->devguid, guidstr,sizeof(guidstr)/sizeof(*guidstr));
1189         var->n1.n2.vt = VT_BSTR;
1190         var->n1.n2.n3.bstrVal = SysAllocString(guidstr);
1191         return S_OK;
1192     }
1193     ERR("Unknown property '%s' queried\n", debugstr_w(name));
1194     return E_FAIL;
1195 }
1196
1197 static HRESULT WINAPI PB_Write(IPropertyBag *iface, LPCOLESTR name, VARIANT *var)
1198 {
1199     ERR("Should not be called\n");
1200     return E_FAIL;
1201 }
1202
1203 static const IPropertyBagVtbl PB_Vtbl =
1204 {
1205     PB_QueryInterface,
1206     PB_AddRef,
1207     PB_Release,
1208     PB_Read,
1209     PB_Write
1210 };