wined3d: Keep track of the current texture type for each texture unit.
[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     if(!drvs.pGetEndpointIDs)
449         return S_OK;
450
451     hr = drvs.pGetEndpointIDs(flow, &ids, &keys, &num, &def);
452     if(FAILED(hr))
453         return hr;
454
455     for(i = 0; i < num; ++i){
456         MMDevice *dev;
457         dev = MMDevice_Create(ids[i], keys[i], NULL, flow, DEVICE_STATE_ACTIVE,
458                 def == i);
459         set_format(dev);
460         HeapFree(GetProcessHeap(), 0, ids[i]);
461     }
462
463     HeapFree(GetProcessHeap(), 0, keys);
464     HeapFree(GetProcessHeap(), 0, ids);
465
466     return S_OK;
467 }
468
469 static void MMDevice_Destroy(MMDevice *This)
470 {
471     DWORD i;
472     TRACE("Freeing %s\n", debugstr_w(This->drv_id));
473     /* Since this function is called at destruction time, reordering of the list is unimportant */
474     for (i = 0; i < MMDevice_count; ++i)
475     {
476         if (MMDevice_head[i] == This)
477         {
478             MMDevice_head[i] = MMDevice_head[--MMDevice_count];
479             break;
480         }
481     }
482     This->crst.DebugInfo->Spare[0] = 0;
483     DeleteCriticalSection(&This->crst);
484     HeapFree(GetProcessHeap(), 0, This->drv_id);
485     HeapFree(GetProcessHeap(), 0, This->key);
486     HeapFree(GetProcessHeap(), 0, This);
487 }
488
489 static inline MMDevice *impl_from_IMMDevice(IMMDevice *iface)
490 {
491     return CONTAINING_RECORD(iface, MMDevice, IMMDevice_iface);
492 }
493
494 static HRESULT WINAPI MMDevice_QueryInterface(IMMDevice *iface, REFIID riid, void **ppv)
495 {
496     MMDevice *This = impl_from_IMMDevice(iface);
497     TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ppv);
498
499     if (!ppv)
500         return E_POINTER;
501     *ppv = NULL;
502     if (IsEqualIID(riid, &IID_IUnknown)
503         || IsEqualIID(riid, &IID_IMMDevice))
504         *ppv = This;
505     else if (IsEqualIID(riid, &IID_IMMEndpoint))
506         *ppv = &This->IMMEndpoint_iface;
507     if (*ppv)
508     {
509         IUnknown_AddRef((IUnknown*)*ppv);
510         return S_OK;
511     }
512     WARN("Unknown interface %s\n", debugstr_guid(riid));
513     return E_NOINTERFACE;
514 }
515
516 static ULONG WINAPI MMDevice_AddRef(IMMDevice *iface)
517 {
518     MMDevice *This = impl_from_IMMDevice(iface);
519     LONG ref;
520
521     ref = InterlockedIncrement(&This->ref);
522     TRACE("Refcount now %i\n", ref);
523     return ref;
524 }
525
526 static ULONG WINAPI MMDevice_Release(IMMDevice *iface)
527 {
528     MMDevice *This = impl_from_IMMDevice(iface);
529     LONG ref;
530
531     ref = InterlockedDecrement(&This->ref);
532     TRACE("Refcount now %i\n", ref);
533     return ref;
534 }
535
536 static HRESULT WINAPI MMDevice_Activate(IMMDevice *iface, REFIID riid, DWORD clsctx, PROPVARIANT *params, void **ppv)
537 {
538     HRESULT hr = E_NOINTERFACE;
539     MMDevice *This = impl_from_IMMDevice(iface);
540
541     TRACE("(%p)->(%p,%x,%p,%p)\n", iface, riid, clsctx, params, ppv);
542
543     if (!ppv)
544         return E_POINTER;
545
546     if (IsEqualIID(riid, &IID_IAudioClient)){
547         hr = drvs.pGetAudioEndpoint(This->key, iface, This->flow, (IAudioClient**)ppv);
548     }else if (IsEqualIID(riid, &IID_IAudioEndpointVolume))
549         hr = AudioEndpointVolume_Create(This, (IAudioEndpointVolume**)ppv);
550     else if (IsEqualIID(riid, &IID_IAudioSessionManager)
551              || IsEqualIID(riid, &IID_IAudioSessionManager2))
552     {
553         hr = drvs.pGetAudioSessionManager(iface, (IAudioSessionManager2**)ppv);
554     }
555     else if (IsEqualIID(riid, &IID_IBaseFilter))
556     {
557         if (This->flow == eRender)
558             hr = CoCreateInstance(&CLSID_DSoundRender, NULL, clsctx, riid, ppv);
559         else
560             ERR("Not supported for recording?\n");
561         if (SUCCEEDED(hr))
562         {
563             IPersistPropertyBag *ppb;
564             hr = IUnknown_QueryInterface((IUnknown*)*ppv, &IID_IPersistPropertyBag, (void*)&ppb);
565             if (SUCCEEDED(hr))
566             {
567                 /* ::Load cannot assume the interface stays alive after the function returns,
568                  * so just create the interface on the stack, saves a lot of complicated code */
569                 IPropertyBagImpl bag = { { &PB_Vtbl }, This->devguid };
570                 hr = IPersistPropertyBag_Load(ppb, &bag.IPropertyBag_iface, NULL);
571                 IPersistPropertyBag_Release(ppb);
572                 if (FAILED(hr))
573                     IBaseFilter_Release((IBaseFilter*)*ppv);
574             }
575             else
576             {
577                 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
578                 hr = S_OK;
579             }
580         }
581     }
582     else if (IsEqualIID(riid, &IID_IDeviceTopology))
583     {
584         FIXME("IID_IDeviceTopology unsupported\n");
585     }
586     else if (IsEqualIID(riid, &IID_IDirectSound)
587              || IsEqualIID(riid, &IID_IDirectSound8))
588     {
589         if (This->flow == eRender)
590             hr = CoCreateInstance(&CLSID_DirectSound8, NULL, clsctx, riid, ppv);
591         if (SUCCEEDED(hr))
592         {
593             hr = IDirectSound_Initialize((IDirectSound*)*ppv, &This->devguid);
594             if (FAILED(hr))
595                 IDirectSound_Release((IDirectSound*)*ppv);
596         }
597     }
598     else if (IsEqualIID(riid, &IID_IDirectSoundCapture)
599              || IsEqualIID(riid, &IID_IDirectSoundCapture8))
600     {
601         if (This->flow == eCapture)
602             hr = CoCreateInstance(&CLSID_DirectSoundCapture8, NULL, clsctx, riid, ppv);
603         if (SUCCEEDED(hr))
604         {
605             hr = IDirectSoundCapture_Initialize((IDirectSoundCapture*)*ppv, &This->devguid);
606             if (FAILED(hr))
607                 IDirectSoundCapture_Release((IDirectSoundCapture*)*ppv);
608         }
609     }
610     else
611         ERR("Invalid/unknown iid %s\n", debugstr_guid(riid));
612
613     if (FAILED(hr))
614         *ppv = NULL;
615
616     TRACE("Returning %08x\n", hr);
617     return hr;
618 }
619
620 static HRESULT WINAPI MMDevice_OpenPropertyStore(IMMDevice *iface, DWORD access, IPropertyStore **ppv)
621 {
622     MMDevice *This = impl_from_IMMDevice(iface);
623     TRACE("(%p)->(%x,%p)\n", This, access, ppv);
624
625     if (!ppv)
626         return E_POINTER;
627     return MMDevPropStore_Create(This, access, ppv);
628 }
629
630 static HRESULT WINAPI MMDevice_GetId(IMMDevice *iface, WCHAR **itemid)
631 {
632     MMDevice *This = impl_from_IMMDevice(iface);
633     WCHAR *str;
634     GUID *id = &This->devguid;
635     static const WCHAR formatW[] = { '{','0','.','0','.','0','.','0','0','0','0','0','0','0','0','}','.',
636                                      '{','%','0','8','X','-','%','0','4','X','-',
637                                      '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
638                                      '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
639                                      '%','0','2','X','%','0','2','X','}',0 };
640
641     TRACE("(%p)->(%p)\n", This, itemid);
642     if (!itemid)
643         return E_POINTER;
644     *itemid = str = CoTaskMemAlloc(56 * sizeof(WCHAR));
645     if (!str)
646         return E_OUTOFMEMORY;
647     wsprintfW( str, formatW, id->Data1, id->Data2, id->Data3,
648                id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
649                id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
650     return S_OK;
651 }
652
653 static HRESULT WINAPI MMDevice_GetState(IMMDevice *iface, DWORD *state)
654 {
655     MMDevice *This = impl_from_IMMDevice(iface);
656     TRACE("(%p)->(%p)\n", iface, state);
657
658     if (!state)
659         return E_POINTER;
660     *state = This->state;
661     return S_OK;
662 }
663
664 static const IMMDeviceVtbl MMDeviceVtbl =
665 {
666     MMDevice_QueryInterface,
667     MMDevice_AddRef,
668     MMDevice_Release,
669     MMDevice_Activate,
670     MMDevice_OpenPropertyStore,
671     MMDevice_GetId,
672     MMDevice_GetState
673 };
674
675 static inline MMDevice *impl_from_IMMEndpoint(IMMEndpoint *iface)
676 {
677     return CONTAINING_RECORD(iface, MMDevice, IMMEndpoint_iface);
678 }
679
680 static HRESULT WINAPI MMEndpoint_QueryInterface(IMMEndpoint *iface, REFIID riid, void **ppv)
681 {
682     MMDevice *This = impl_from_IMMEndpoint(iface);
683     return IMMDevice_QueryInterface(&This->IMMDevice_iface, riid, ppv);
684 }
685
686 static ULONG WINAPI MMEndpoint_AddRef(IMMEndpoint *iface)
687 {
688     MMDevice *This = impl_from_IMMEndpoint(iface);
689     return IMMDevice_AddRef(&This->IMMDevice_iface);
690 }
691
692 static ULONG WINAPI MMEndpoint_Release(IMMEndpoint *iface)
693 {
694     MMDevice *This = impl_from_IMMEndpoint(iface);
695     return IMMDevice_Release(&This->IMMDevice_iface);
696 }
697
698 static HRESULT WINAPI MMEndpoint_GetDataFlow(IMMEndpoint *iface, EDataFlow *flow)
699 {
700     MMDevice *This = impl_from_IMMEndpoint(iface);
701     if (!flow)
702         return E_POINTER;
703     *flow = This->flow;
704     return S_OK;
705 }
706
707 static const IMMEndpointVtbl MMEndpointVtbl =
708 {
709     MMEndpoint_QueryInterface,
710     MMEndpoint_AddRef,
711     MMEndpoint_Release,
712     MMEndpoint_GetDataFlow
713 };
714
715 static HRESULT MMDevCol_Create(IMMDeviceCollection **ppv, EDataFlow flow, DWORD state)
716 {
717     MMDevColImpl *This;
718
719     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
720     *ppv = NULL;
721     if (!This)
722         return E_OUTOFMEMORY;
723     This->IMMDeviceCollection_iface.lpVtbl = &MMDevColVtbl;
724     This->ref = 1;
725     This->flow = flow;
726     This->state = state;
727     *ppv = &This->IMMDeviceCollection_iface;
728     return S_OK;
729 }
730
731 static void MMDevCol_Destroy(MMDevColImpl *This)
732 {
733     HeapFree(GetProcessHeap(), 0, This);
734 }
735
736 static HRESULT WINAPI MMDevCol_QueryInterface(IMMDeviceCollection *iface, REFIID riid, void **ppv)
737 {
738     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
739
740     if (!ppv)
741         return E_POINTER;
742     if (IsEqualIID(riid, &IID_IUnknown)
743         || IsEqualIID(riid, &IID_IMMDeviceCollection))
744         *ppv = This;
745     else
746         *ppv = NULL;
747     if (!*ppv)
748         return E_NOINTERFACE;
749     IUnknown_AddRef((IUnknown*)*ppv);
750     return S_OK;
751 }
752
753 static ULONG WINAPI MMDevCol_AddRef(IMMDeviceCollection *iface)
754 {
755     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
756     LONG ref = InterlockedIncrement(&This->ref);
757     TRACE("Refcount now %i\n", ref);
758     return ref;
759 }
760
761 static ULONG WINAPI MMDevCol_Release(IMMDeviceCollection *iface)
762 {
763     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
764     LONG ref = InterlockedDecrement(&This->ref);
765     TRACE("Refcount now %i\n", ref);
766     if (!ref)
767         MMDevCol_Destroy(This);
768     return ref;
769 }
770
771 static HRESULT WINAPI MMDevCol_GetCount(IMMDeviceCollection *iface, UINT *numdevs)
772 {
773     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
774     DWORD i;
775
776     TRACE("(%p)->(%p)\n", This, numdevs);
777     if (!numdevs)
778         return E_POINTER;
779
780     *numdevs = 0;
781     for (i = 0; i < MMDevice_count; ++i)
782     {
783         MMDevice *cur = MMDevice_head[i];
784         if ((cur->flow == This->flow || This->flow == eAll)
785             && (cur->state & This->state))
786             ++(*numdevs);
787     }
788     return S_OK;
789 }
790
791 static HRESULT WINAPI MMDevCol_Item(IMMDeviceCollection *iface, UINT n, IMMDevice **dev)
792 {
793     MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
794     DWORD i = 0, j = 0;
795
796     TRACE("(%p)->(%u, %p)\n", This, n, dev);
797     if (!dev)
798         return E_POINTER;
799
800     for (j = 0; j < MMDevice_count; ++j)
801     {
802         MMDevice *cur = MMDevice_head[j];
803         if ((cur->flow == This->flow || This->flow == eAll)
804             && (cur->state & This->state)
805             && i++ == n)
806         {
807             *dev = &cur->IMMDevice_iface;
808             IMMDevice_AddRef(*dev);
809             return S_OK;
810         }
811     }
812     WARN("Could not obtain item %u\n", n);
813     *dev = NULL;
814     return E_INVALIDARG;
815 }
816
817 static const IMMDeviceCollectionVtbl MMDevColVtbl =
818 {
819     MMDevCol_QueryInterface,
820     MMDevCol_AddRef,
821     MMDevCol_Release,
822     MMDevCol_GetCount,
823     MMDevCol_Item
824 };
825
826 HRESULT MMDevEnum_Create(REFIID riid, void **ppv)
827 {
828     MMDevEnumImpl *This = MMDevEnumerator;
829
830     if (!This)
831     {
832         This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
833         *ppv = NULL;
834         if (!This)
835             return E_OUTOFMEMORY;
836         This->ref = 1;
837         This->IMMDeviceEnumerator_iface.lpVtbl = &MMDevEnumVtbl;
838         MMDevEnumerator = This;
839
840         load_devices_from_reg();
841         load_driver_devices(eRender);
842         load_driver_devices(eCapture);
843     }
844     return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
845 }
846
847 void MMDevEnum_Free(void)
848 {
849     while (MMDevice_count)
850         MMDevice_Destroy(MMDevice_head[0]);
851     RegCloseKey(key_render);
852     RegCloseKey(key_capture);
853     key_render = key_capture = NULL;
854     HeapFree(GetProcessHeap(), 0, MMDevEnumerator);
855     MMDevEnumerator = NULL;
856 }
857
858 static HRESULT WINAPI MMDevEnum_QueryInterface(IMMDeviceEnumerator *iface, REFIID riid, void **ppv)
859 {
860     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
861
862     if (!ppv)
863         return E_POINTER;
864     if (IsEqualIID(riid, &IID_IUnknown)
865         || IsEqualIID(riid, &IID_IMMDeviceEnumerator))
866         *ppv = This;
867     else
868         *ppv = NULL;
869     if (!*ppv)
870         return E_NOINTERFACE;
871     IUnknown_AddRef((IUnknown*)*ppv);
872     return S_OK;
873 }
874
875 static ULONG WINAPI MMDevEnum_AddRef(IMMDeviceEnumerator *iface)
876 {
877     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
878     LONG ref = InterlockedIncrement(&This->ref);
879     TRACE("Refcount now %i\n", ref);
880     return ref;
881 }
882
883 static ULONG WINAPI MMDevEnum_Release(IMMDeviceEnumerator *iface)
884 {
885     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
886     LONG ref = InterlockedDecrement(&This->ref);
887     if (!ref)
888         MMDevEnum_Free();
889     TRACE("Refcount now %i\n", ref);
890     return ref;
891 }
892
893 static HRESULT WINAPI MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator *iface, EDataFlow flow, DWORD mask, IMMDeviceCollection **devices)
894 {
895     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
896     TRACE("(%p)->(%u,%u,%p)\n", This, flow, mask, devices);
897     if (!devices)
898         return E_POINTER;
899     *devices = NULL;
900     if (flow >= EDataFlow_enum_count)
901         return E_INVALIDARG;
902     if (mask & ~DEVICE_STATEMASK_ALL)
903         return E_INVALIDARG;
904     return MMDevCol_Create(devices, flow, mask);
905 }
906
907 static HRESULT WINAPI MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator *iface, EDataFlow flow, ERole role, IMMDevice **device)
908 {
909     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
910     TRACE("(%p)->(%u,%u,%p)\n", This, flow, role, device);
911
912     if (!device)
913         return E_POINTER;
914     *device = NULL;
915
916     if (flow == eRender)
917         *device = &MMDevice_def_play->IMMDevice_iface;
918     else if (flow == eCapture)
919         *device = &MMDevice_def_rec->IMMDevice_iface;
920     else
921     {
922         WARN("Unknown flow %u\n", flow);
923         return E_INVALIDARG;
924     }
925
926     if (!*device)
927         return E_NOTFOUND;
928     IMMDevice_AddRef(*device);
929     return S_OK;
930 }
931
932 static HRESULT WINAPI MMDevEnum_GetDevice(IMMDeviceEnumerator *iface, const WCHAR *name, IMMDevice **device)
933 {
934     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
935     DWORD i=0;
936     IMMDevice *dev = NULL;
937
938     if(!name || !device)
939         return E_POINTER;
940
941     TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), device);
942     for (i = 0; i < MMDevice_count; ++i)
943     {
944         WCHAR *str;
945         dev = &MMDevice_head[i]->IMMDevice_iface;
946         IMMDevice_GetId(dev, &str);
947
948         if (str && !lstrcmpW(str, name))
949         {
950             CoTaskMemFree(str);
951             IUnknown_AddRef(dev);
952             *device = dev;
953             return S_OK;
954         }
955         CoTaskMemFree(str);
956     }
957     TRACE("Could not find device %s\n", debugstr_w(name));
958     return E_INVALIDARG;
959 }
960
961 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
962 {
963     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
964     TRACE("(%p)->(%p)\n", This, client);
965     FIXME("stub\n");
966     return S_OK;
967 }
968
969 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
970 {
971     MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
972     TRACE("(%p)->(%p)\n", This, client);
973     FIXME("stub\n");
974     return S_OK;
975 }
976
977 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl =
978 {
979     MMDevEnum_QueryInterface,
980     MMDevEnum_AddRef,
981     MMDevEnum_Release,
982     MMDevEnum_EnumAudioEndpoints,
983     MMDevEnum_GetDefaultAudioEndpoint,
984     MMDevEnum_GetDevice,
985     MMDevEnum_RegisterEndpointNotificationCallback,
986     MMDevEnum_UnregisterEndpointNotificationCallback
987 };
988
989 static HRESULT MMDevPropStore_Create(MMDevice *parent, DWORD access, IPropertyStore **ppv)
990 {
991     MMDevPropStore *This;
992     if (access != STGM_READ
993         && access != STGM_WRITE
994         && access != STGM_READWRITE)
995     {
996         WARN("Invalid access %08x\n", access);
997         return E_INVALIDARG;
998     }
999     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1000     *ppv = &This->IPropertyStore_iface;
1001     if (!This)
1002         return E_OUTOFMEMORY;
1003     This->IPropertyStore_iface.lpVtbl = &MMDevPropVtbl;
1004     This->ref = 1;
1005     This->parent = parent;
1006     This->access = access;
1007     return S_OK;
1008 }
1009
1010 static void MMDevPropStore_Destroy(MMDevPropStore *This)
1011 {
1012     HeapFree(GetProcessHeap(), 0, This);
1013 }
1014
1015 static HRESULT WINAPI MMDevPropStore_QueryInterface(IPropertyStore *iface, REFIID riid, void **ppv)
1016 {
1017     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1018
1019     if (!ppv)
1020         return E_POINTER;
1021     if (IsEqualIID(riid, &IID_IUnknown)
1022         || IsEqualIID(riid, &IID_IPropertyStore))
1023         *ppv = This;
1024     else
1025         *ppv = NULL;
1026     if (!*ppv)
1027         return E_NOINTERFACE;
1028     IUnknown_AddRef((IUnknown*)*ppv);
1029     return S_OK;
1030 }
1031
1032 static ULONG WINAPI MMDevPropStore_AddRef(IPropertyStore *iface)
1033 {
1034     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1035     LONG ref = InterlockedIncrement(&This->ref);
1036     TRACE("Refcount now %i\n", ref);
1037     return ref;
1038 }
1039
1040 static ULONG WINAPI MMDevPropStore_Release(IPropertyStore *iface)
1041 {
1042     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1043     LONG ref = InterlockedDecrement(&This->ref);
1044     TRACE("Refcount now %i\n", ref);
1045     if (!ref)
1046         MMDevPropStore_Destroy(This);
1047     return ref;
1048 }
1049
1050 static HRESULT WINAPI MMDevPropStore_GetCount(IPropertyStore *iface, DWORD *nprops)
1051 {
1052     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1053     WCHAR buffer[50];
1054     DWORD i = 0;
1055     HKEY propkey;
1056     HRESULT hr;
1057
1058     TRACE("(%p)->(%p)\n", iface, nprops);
1059     if (!nprops)
1060         return E_POINTER;
1061     hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1062     if (FAILED(hr))
1063         return hr;
1064     *nprops = 0;
1065     do {
1066         DWORD len = sizeof(buffer)/sizeof(*buffer);
1067         if (RegEnumKeyExW(propkey, i, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
1068             break;
1069         i++;
1070     } while (0);
1071     RegCloseKey(propkey);
1072     TRACE("Returning %i\n", i);
1073     *nprops = i;
1074     return S_OK;
1075 }
1076
1077 static HRESULT WINAPI MMDevPropStore_GetAt(IPropertyStore *iface, DWORD prop, PROPERTYKEY *key)
1078 {
1079     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1080     WCHAR buffer[50];
1081     DWORD len = sizeof(buffer)/sizeof(*buffer);
1082     HRESULT hr;
1083     HKEY propkey;
1084
1085     TRACE("(%p)->(%u,%p)\n", iface, prop, key);
1086     if (!key)
1087         return E_POINTER;
1088
1089     hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1090     if (FAILED(hr))
1091         return hr;
1092
1093     if (RegEnumKeyExW(propkey, prop, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS
1094         || len <= 40)
1095     {
1096         WARN("GetAt %u failed\n", prop);
1097         return E_INVALIDARG;
1098     }
1099     RegCloseKey(propkey);
1100     buffer[39] = 0;
1101     CLSIDFromString(buffer, &key->fmtid);
1102     key->pid = atoiW(&buffer[40]);
1103     return S_OK;
1104 }
1105
1106 static HRESULT WINAPI MMDevPropStore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *pv)
1107 {
1108     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1109     TRACE("(%p)->(\"%s,%u\", %p\n", This, debugstr_guid(&key->fmtid), key ? key->pid : 0, pv);
1110
1111     if (!key || !pv)
1112         return E_POINTER;
1113     if (This->access != STGM_READ
1114         && This->access != STGM_READWRITE)
1115         return STG_E_ACCESSDENIED;
1116
1117     /* Special case */
1118     if (IsEqualPropertyKey(*key, PKEY_AudioEndpoint_GUID))
1119     {
1120         pv->vt = VT_LPWSTR;
1121         pv->u.pwszVal = CoTaskMemAlloc(39 * sizeof(WCHAR));
1122         if (!pv->u.pwszVal)
1123             return E_OUTOFMEMORY;
1124         StringFromGUID2(&This->parent->devguid, pv->u.pwszVal, 39);
1125         return S_OK;
1126     }
1127
1128     return MMDevice_GetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1129 }
1130
1131 static HRESULT WINAPI MMDevPropStore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT pv)
1132 {
1133     MMDevPropStore *This = impl_from_IPropertyStore(iface);
1134
1135     if (!key || !pv)
1136         return E_POINTER;
1137
1138     if (This->access != STGM_WRITE
1139         && This->access != STGM_READWRITE)
1140         return STG_E_ACCESSDENIED;
1141     return MMDevice_SetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1142 }
1143
1144 static HRESULT WINAPI MMDevPropStore_Commit(IPropertyStore *iface)
1145 {
1146     FIXME("stub\n");
1147     return E_NOTIMPL;
1148 }
1149
1150 static const IPropertyStoreVtbl MMDevPropVtbl =
1151 {
1152     MMDevPropStore_QueryInterface,
1153     MMDevPropStore_AddRef,
1154     MMDevPropStore_Release,
1155     MMDevPropStore_GetCount,
1156     MMDevPropStore_GetAt,
1157     MMDevPropStore_GetValue,
1158     MMDevPropStore_SetValue,
1159     MMDevPropStore_Commit
1160 };
1161
1162
1163 /* Property bag for IBaseFilter activation */
1164 static HRESULT WINAPI PB_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
1165 {
1166     ERR("Should not be called\n");
1167     *ppv = NULL;
1168     return E_NOINTERFACE;
1169 }
1170
1171 static ULONG WINAPI PB_AddRef(IPropertyBag *iface)
1172 {
1173     ERR("Should not be called\n");
1174     return 2;
1175 }
1176
1177 static ULONG WINAPI PB_Release(IPropertyBag *iface)
1178 {
1179     ERR("Should not be called\n");
1180     return 1;
1181 }
1182
1183 static HRESULT WINAPI PB_Read(IPropertyBag *iface, LPCOLESTR name, VARIANT *var, IErrorLog *log)
1184 {
1185     static const WCHAR dsguid[] = { 'D','S','G','u','i','d', 0 };
1186     IPropertyBagImpl *This = impl_from_IPropertyBag(iface);
1187     TRACE("Trying to read %s, type %u\n", debugstr_w(name), var->n1.n2.vt);
1188     if (!lstrcmpW(name, dsguid))
1189     {
1190         WCHAR guidstr[39];
1191         StringFromGUID2(&This->devguid, guidstr,sizeof(guidstr)/sizeof(*guidstr));
1192         var->n1.n2.vt = VT_BSTR;
1193         var->n1.n2.n3.bstrVal = SysAllocString(guidstr);
1194         return S_OK;
1195     }
1196     ERR("Unknown property '%s' queried\n", debugstr_w(name));
1197     return E_FAIL;
1198 }
1199
1200 static HRESULT WINAPI PB_Write(IPropertyBag *iface, LPCOLESTR name, VARIANT *var)
1201 {
1202     ERR("Should not be called\n");
1203     return E_FAIL;
1204 }
1205
1206 static const IPropertyBagVtbl PB_Vtbl =
1207 {
1208     PB_QueryInterface,
1209     PB_AddRef,
1210     PB_Release,
1211     PB_Read,
1212     PB_Write
1213 };