mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / mmdevapi / devenum.c
blobfc33cac17ecf21421f05b8db01925951dc25040b
1 /*
2 * Copyright 2009 Maarten Lankhorst
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.
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.
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
19 #include <stdarg.h>
21 #define NONAMELESSUNION
22 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "winreg.h"
27 #include "wine/debug.h"
28 #include "wine/list.h"
30 #include "initguid.h"
31 #include "ole2.h"
32 #include "mmdeviceapi.h"
33 #include "dshow.h"
34 #include "dsound.h"
35 #include "audioclient.h"
36 #include "endpointvolume.h"
37 #include "audiopolicy.h"
38 #include "spatialaudioclient.h"
40 #include "mmdevapi.h"
41 #include "devpkey.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
45 static HKEY key_render;
46 static HKEY key_capture;
48 typedef struct MMDevPropStoreImpl
50 IPropertyStore IPropertyStore_iface;
51 LONG ref;
52 MMDevice *parent;
53 DWORD access;
54 } MMDevPropStore;
56 typedef struct MMDevEnumImpl
58 IMMDeviceEnumerator IMMDeviceEnumerator_iface;
59 LONG ref;
60 } MMDevEnumImpl;
62 static MMDevEnumImpl *MMDevEnumerator;
63 static MMDevice **MMDevice_head;
64 static MMDevice *MMDevice_def_rec, *MMDevice_def_play;
65 static DWORD MMDevice_count;
66 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl;
67 static const IMMDeviceCollectionVtbl MMDevColVtbl;
68 static const IMMDeviceVtbl MMDeviceVtbl;
69 static const IPropertyStoreVtbl MMDevPropVtbl;
70 static const IMMEndpointVtbl MMEndpointVtbl;
72 static IMMDevice info_device;
74 typedef struct MMDevColImpl
76 IMMDeviceCollection IMMDeviceCollection_iface;
77 LONG ref;
78 EDataFlow flow;
79 DWORD state;
80 } MMDevColImpl;
82 typedef struct IPropertyBagImpl {
83 IPropertyBag IPropertyBag_iface;
84 GUID devguid;
85 } IPropertyBagImpl;
87 static const IPropertyBagVtbl PB_Vtbl;
89 static HRESULT MMDevPropStore_Create(MMDevice *This, DWORD access, IPropertyStore **ppv);
91 static inline MMDevPropStore *impl_from_IPropertyStore(IPropertyStore *iface)
93 return CONTAINING_RECORD(iface, MMDevPropStore, IPropertyStore_iface);
96 static inline MMDevEnumImpl *impl_from_IMMDeviceEnumerator(IMMDeviceEnumerator *iface)
98 return CONTAINING_RECORD(iface, MMDevEnumImpl, IMMDeviceEnumerator_iface);
101 static inline MMDevColImpl *impl_from_IMMDeviceCollection(IMMDeviceCollection *iface)
103 return CONTAINING_RECORD(iface, MMDevColImpl, IMMDeviceCollection_iface);
106 static inline IPropertyBagImpl *impl_from_IPropertyBag(IPropertyBag *iface)
108 return CONTAINING_RECORD(iface, IPropertyBagImpl, IPropertyBag_iface);
111 static const WCHAR propkey_formatW[] = L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X},%d";
113 static HRESULT MMDevPropStore_OpenPropKey(const GUID *guid, DWORD flow, HKEY *propkey)
115 WCHAR buffer[39];
116 LONG ret;
117 HKEY key;
118 StringFromGUID2(guid, buffer, 39);
119 if ((ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, buffer, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, &key)) != ERROR_SUCCESS)
121 WARN("Opening key %s failed with %u\n", debugstr_w(buffer), ret);
122 return E_FAIL;
124 ret = RegOpenKeyExW(key, L"Properties", 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, propkey);
125 RegCloseKey(key);
126 if (ret != ERROR_SUCCESS)
128 WARN("Opening key Properties failed with %u\n", ret);
129 return E_FAIL;
131 return S_OK;
134 static HRESULT MMDevice_GetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, PROPVARIANT *pv)
136 WCHAR buffer[80];
137 const GUID *id = &key->fmtid;
138 DWORD type, size;
139 HRESULT hr = S_OK;
140 HKEY regkey;
141 LONG ret;
143 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
144 if (FAILED(hr))
145 return hr;
146 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
147 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
148 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
149 ret = RegGetValueW(regkey, NULL, buffer, RRF_RT_ANY, &type, NULL, &size);
150 if (ret != ERROR_SUCCESS)
152 WARN("Reading %s returned %d\n", debugstr_w(buffer), ret);
153 RegCloseKey(regkey);
154 PropVariantClear(pv);
155 return S_OK;
158 switch (type)
160 case REG_SZ:
162 pv->vt = VT_LPWSTR;
163 pv->pwszVal = CoTaskMemAlloc(size);
164 if (!pv->pwszVal)
165 hr = E_OUTOFMEMORY;
166 else
167 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_SZ, NULL, (BYTE*)pv->pwszVal, &size);
168 break;
170 case REG_DWORD:
172 pv->vt = VT_UI4;
173 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_DWORD, NULL, (BYTE*)&pv->ulVal, &size);
174 break;
176 case REG_BINARY:
178 pv->vt = VT_BLOB;
179 pv->blob.cbSize = size;
180 pv->blob.pBlobData = CoTaskMemAlloc(size);
181 if (!pv->blob.pBlobData)
182 hr = E_OUTOFMEMORY;
183 else
184 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_BINARY, NULL, (BYTE*)pv->blob.pBlobData, &size);
185 break;
187 default:
188 ERR("Unknown/unhandled type: %u\n", type);
189 PropVariantClear(pv);
190 break;
192 RegCloseKey(regkey);
193 return hr;
196 static HRESULT MMDevice_SetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, REFPROPVARIANT pv)
198 WCHAR buffer[80];
199 const GUID *id = &key->fmtid;
200 HRESULT hr;
201 HKEY regkey;
202 LONG ret;
204 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
205 if (FAILED(hr))
206 return hr;
207 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
208 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
209 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
210 switch (pv->vt)
212 case VT_UI4:
214 ret = RegSetValueExW(regkey, buffer, 0, REG_DWORD, (const BYTE*)&pv->ulVal, sizeof(DWORD));
215 break;
217 case VT_BLOB:
219 ret = RegSetValueExW(regkey, buffer, 0, REG_BINARY, pv->blob.pBlobData, pv->blob.cbSize);
220 TRACE("Blob %p %u\n", pv->blob.pBlobData, pv->blob.cbSize);
222 break;
224 case VT_LPWSTR:
226 ret = RegSetValueExW(regkey, buffer, 0, REG_SZ, (const BYTE*)pv->pwszVal, sizeof(WCHAR)*(1+lstrlenW(pv->pwszVal)));
227 break;
229 default:
230 ret = 0;
231 FIXME("Unhandled type %u\n", pv->vt);
232 hr = E_INVALIDARG;
233 break;
235 RegCloseKey(regkey);
236 TRACE("Writing %s returned %u\n", debugstr_w(buffer), ret);
237 return hr;
240 static HRESULT set_driver_prop_value(GUID *id, const EDataFlow flow, const PROPERTYKEY *prop)
242 HRESULT hr;
243 PROPVARIANT pv;
245 if (!drvs.pGetPropValue)
246 return E_NOTIMPL;
248 hr = drvs.pGetPropValue(id, prop, &pv);
250 if (SUCCEEDED(hr))
252 MMDevice_SetPropValue(id, flow, prop, &pv);
253 PropVariantClear(&pv);
256 return hr;
259 /* Creates or updates the state of a device
260 * If GUID is null, a random guid will be assigned
261 * and the device will be created
263 static MMDevice *MMDevice_Create(WCHAR *name, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault)
265 HKEY key, root;
266 MMDevice *cur = NULL;
267 WCHAR guidstr[39];
268 DWORD i;
270 static const PROPERTYKEY deviceinterface_key = {
271 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
274 static const PROPERTYKEY devicepath_key = {
275 {0xb3f8fa53, 0x0004, 0x438e, {0x90, 0x03, 0x51, 0xa4, 0x6e, 0x13, 0x9b, 0xfc}}, 2
278 for (i = 0; i < MMDevice_count; ++i)
280 MMDevice *device = MMDevice_head[i];
281 if (device->flow == flow && IsEqualGUID(&device->devguid, id)){
282 cur = device;
283 break;
287 if(!cur){
288 /* No device found, allocate new one */
289 cur = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cur));
290 if (!cur)
291 return NULL;
293 cur->IMMDevice_iface.lpVtbl = &MMDeviceVtbl;
294 cur->IMMEndpoint_iface.lpVtbl = &MMEndpointVtbl;
296 InitializeCriticalSection(&cur->crst);
297 cur->crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MMDevice.crst");
299 if (!MMDevice_head)
300 MMDevice_head = HeapAlloc(GetProcessHeap(), 0, sizeof(*MMDevice_head));
301 else
302 MMDevice_head = HeapReAlloc(GetProcessHeap(), 0, MMDevice_head, sizeof(*MMDevice_head)*(1+MMDevice_count));
303 MMDevice_head[MMDevice_count++] = cur;
304 }else if(cur->ref > 0)
305 WARN("Modifying an MMDevice with postitive reference count!\n");
307 HeapFree(GetProcessHeap(), 0, cur->drv_id);
308 cur->drv_id = name;
310 cur->flow = flow;
311 cur->state = state;
312 cur->devguid = *id;
314 StringFromGUID2(&cur->devguid, guidstr, ARRAY_SIZE(guidstr));
316 if (flow == eRender)
317 root = key_render;
318 else
319 root = key_capture;
321 if (RegCreateKeyExW(root, guidstr, 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &key, NULL) == ERROR_SUCCESS)
323 HKEY keyprop;
324 RegSetValueExW(key, L"DeviceState", 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
325 if (!RegCreateKeyExW(key, L"Properties", 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &keyprop, NULL))
327 PROPVARIANT pv;
329 pv.vt = VT_LPWSTR;
330 pv.pwszVal = name;
331 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
332 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_DeviceInterface_FriendlyName, &pv);
333 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_DeviceDesc, &pv);
335 pv.pwszVal = guidstr;
336 MMDevice_SetPropValue(id, flow, &deviceinterface_key, &pv);
338 set_driver_prop_value(id, flow, &devicepath_key);
340 if (FAILED(set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_FormFactor)))
342 pv.vt = VT_UI4;
343 pv.ulVal = (flow == eCapture) ? Microphone : Speakers;
345 MMDevice_SetPropValue(id, flow, &PKEY_AudioEndpoint_FormFactor, &pv);
348 if (flow != eCapture)
350 PROPVARIANT pv2;
352 PropVariantInit(&pv2);
354 /* make read-write by not overwriting if already set */
355 if (FAILED(MMDevice_GetPropValue(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers, &pv2)) || pv2.vt != VT_UI4)
356 set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers);
358 PropVariantClear(&pv2);
361 RegCloseKey(keyprop);
363 RegCloseKey(key);
366 if (setdefault)
368 if (flow == eRender)
369 MMDevice_def_play = cur;
370 else
371 MMDevice_def_rec = cur;
373 return cur;
376 static HRESULT load_devices_from_reg(void)
378 DWORD i = 0;
379 HKEY root, cur;
380 LONG ret;
381 DWORD curflow;
383 ret = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
384 L"Software\\Microsoft\\Windows\\CurrentVersion\\MMDevices\\Audio", 0, NULL, 0,
385 KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &root, NULL);
386 if (ret == ERROR_SUCCESS)
387 ret = RegCreateKeyExW(root, L"Capture", 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_capture, NULL);
388 if (ret == ERROR_SUCCESS)
389 ret = RegCreateKeyExW(root, L"Render", 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_render, NULL);
390 RegCloseKey(root);
391 cur = key_capture;
392 curflow = eCapture;
393 if (ret != ERROR_SUCCESS)
395 RegCloseKey(key_capture);
396 key_render = key_capture = NULL;
397 WARN("Couldn't create key: %u\n", ret);
398 return E_FAIL;
401 do {
402 WCHAR guidvalue[39];
403 GUID guid;
404 DWORD len;
405 PROPVARIANT pv = { VT_EMPTY };
407 len = ARRAY_SIZE(guidvalue);
408 ret = RegEnumKeyExW(cur, i++, guidvalue, &len, NULL, NULL, NULL, NULL);
409 if (ret == ERROR_NO_MORE_ITEMS)
411 if (cur == key_capture)
413 cur = key_render;
414 curflow = eRender;
415 i = 0;
416 continue;
418 break;
420 if (ret != ERROR_SUCCESS)
421 continue;
422 if (SUCCEEDED(CLSIDFromString(guidvalue, &guid))
423 && SUCCEEDED(MMDevice_GetPropValue(&guid, curflow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv))
424 && pv.vt == VT_LPWSTR)
426 DWORD size_bytes = (lstrlenW(pv.pwszVal) + 1) * sizeof(WCHAR);
427 WCHAR *name = HeapAlloc(GetProcessHeap(), 0, size_bytes);
428 memcpy(name, pv.pwszVal, size_bytes);
429 MMDevice_Create(name, &guid, curflow,
430 DEVICE_STATE_NOTPRESENT, FALSE);
431 CoTaskMemFree(pv.pwszVal);
433 } while (1);
435 return S_OK;
438 static HRESULT set_format(MMDevice *dev)
440 HRESULT hr;
441 IAudioClient *client;
442 WAVEFORMATEX *fmt;
443 PROPVARIANT pv = { VT_EMPTY };
445 hr = drvs.pGetAudioEndpoint(&dev->devguid, &dev->IMMDevice_iface, &client);
446 if(FAILED(hr))
447 return hr;
449 hr = IAudioClient_GetMixFormat(client, &fmt);
450 if(FAILED(hr)){
451 IAudioClient_Release(client);
452 return hr;
455 IAudioClient_Release(client);
457 pv.vt = VT_BLOB;
458 pv.blob.cbSize = sizeof(WAVEFORMATEX) + fmt->cbSize;
459 pv.blob.pBlobData = (BYTE*)fmt;
460 MMDevice_SetPropValue(&dev->devguid, dev->flow,
461 &PKEY_AudioEngine_DeviceFormat, &pv);
462 MMDevice_SetPropValue(&dev->devguid, dev->flow,
463 &PKEY_AudioEngine_OEMFormat, &pv);
464 CoTaskMemFree(fmt);
466 return S_OK;
469 static HRESULT load_driver_devices(EDataFlow flow)
471 WCHAR **ids;
472 GUID *guids;
473 UINT num, def, i;
474 HRESULT hr;
476 if(!drvs.pGetEndpointIDs)
477 return S_OK;
479 hr = drvs.pGetEndpointIDs(flow, &ids, &guids, &num, &def);
480 if(FAILED(hr))
481 return hr;
483 for(i = 0; i < num; ++i){
484 MMDevice *dev;
485 dev = MMDevice_Create(ids[i], &guids[i], flow, DEVICE_STATE_ACTIVE,
486 def == i);
487 set_format(dev);
490 HeapFree(GetProcessHeap(), 0, guids);
491 HeapFree(GetProcessHeap(), 0, ids);
493 return S_OK;
496 static void MMDevice_Destroy(MMDevice *This)
498 DWORD i;
499 TRACE("Freeing %s\n", debugstr_w(This->drv_id));
500 /* Since this function is called at destruction time, reordering of the list is unimportant */
501 for (i = 0; i < MMDevice_count; ++i)
503 if (MMDevice_head[i] == This)
505 MMDevice_head[i] = MMDevice_head[--MMDevice_count];
506 break;
509 This->crst.DebugInfo->Spare[0] = 0;
510 DeleteCriticalSection(&This->crst);
511 HeapFree(GetProcessHeap(), 0, This->drv_id);
512 HeapFree(GetProcessHeap(), 0, This);
515 static inline MMDevice *impl_from_IMMDevice(IMMDevice *iface)
517 return CONTAINING_RECORD(iface, MMDevice, IMMDevice_iface);
520 static HRESULT WINAPI MMDevice_QueryInterface(IMMDevice *iface, REFIID riid, void **ppv)
522 MMDevice *This = impl_from_IMMDevice(iface);
523 TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ppv);
525 if (!ppv)
526 return E_POINTER;
527 *ppv = NULL;
528 if (IsEqualIID(riid, &IID_IUnknown)
529 || IsEqualIID(riid, &IID_IMMDevice))
530 *ppv = &This->IMMDevice_iface;
531 else if (IsEqualIID(riid, &IID_IMMEndpoint))
532 *ppv = &This->IMMEndpoint_iface;
533 if (*ppv)
535 IUnknown_AddRef((IUnknown*)*ppv);
536 return S_OK;
538 WARN("Unknown interface %s\n", debugstr_guid(riid));
539 return E_NOINTERFACE;
542 static ULONG WINAPI MMDevice_AddRef(IMMDevice *iface)
544 MMDevice *This = impl_from_IMMDevice(iface);
545 LONG ref;
547 ref = InterlockedIncrement(&This->ref);
548 TRACE("Refcount now %i\n", ref);
549 return ref;
552 static ULONG WINAPI MMDevice_Release(IMMDevice *iface)
554 MMDevice *This = impl_from_IMMDevice(iface);
555 LONG ref;
557 ref = InterlockedDecrement(&This->ref);
558 TRACE("Refcount now %i\n", ref);
559 return ref;
562 static HRESULT WINAPI MMDevice_Activate(IMMDevice *iface, REFIID riid, DWORD clsctx, PROPVARIANT *params, void **ppv)
564 HRESULT hr = E_NOINTERFACE;
565 MMDevice *This = impl_from_IMMDevice(iface);
567 TRACE("(%p)->(%s, %x, %p, %p)\n", iface, debugstr_guid(riid), clsctx, params, ppv);
569 if (!ppv)
570 return E_POINTER;
572 if (IsEqualIID(riid, &IID_IAudioClient) ||
573 IsEqualIID(riid, &IID_IAudioClient2) ||
574 IsEqualIID(riid, &IID_IAudioClient3)){
575 hr = drvs.pGetAudioEndpoint(&This->devguid, iface, (IAudioClient**)ppv);
576 }else if (IsEqualIID(riid, &IID_IAudioEndpointVolume) ||
577 IsEqualIID(riid, &IID_IAudioEndpointVolumeEx))
578 hr = AudioEndpointVolume_Create(This, (IAudioEndpointVolumeEx**)ppv);
579 else if (IsEqualIID(riid, &IID_IAudioSessionManager)
580 || IsEqualIID(riid, &IID_IAudioSessionManager2))
582 hr = drvs.pGetAudioSessionManager(iface, (IAudioSessionManager2**)ppv);
584 else if (IsEqualIID(riid, &IID_IBaseFilter))
586 if (This->flow == eRender)
587 hr = CoCreateInstance(&CLSID_DSoundRender, NULL, clsctx, riid, ppv);
588 else
589 ERR("Not supported for recording?\n");
590 if (SUCCEEDED(hr))
592 IPersistPropertyBag *ppb;
593 hr = IUnknown_QueryInterface((IUnknown*)*ppv, &IID_IPersistPropertyBag, (void **)&ppb);
594 if (SUCCEEDED(hr))
596 /* ::Load cannot assume the interface stays alive after the function returns,
597 * so just create the interface on the stack, saves a lot of complicated code */
598 IPropertyBagImpl bag = { { &PB_Vtbl } };
599 bag.devguid = This->devguid;
600 hr = IPersistPropertyBag_Load(ppb, &bag.IPropertyBag_iface, NULL);
601 IPersistPropertyBag_Release(ppb);
602 if (FAILED(hr))
603 IBaseFilter_Release((IBaseFilter*)*ppv);
605 else
607 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
608 hr = S_OK;
612 else if (IsEqualIID(riid, &IID_IDeviceTopology))
614 FIXME("IID_IDeviceTopology unsupported\n");
616 else if (IsEqualIID(riid, &IID_IDirectSound)
617 || IsEqualIID(riid, &IID_IDirectSound8))
619 if (This->flow == eRender)
620 hr = CoCreateInstance(&CLSID_DirectSound8, NULL, clsctx, riid, ppv);
621 if (SUCCEEDED(hr))
623 hr = IDirectSound_Initialize((IDirectSound*)*ppv, &This->devguid);
624 if (FAILED(hr))
625 IDirectSound_Release((IDirectSound*)*ppv);
628 else if (IsEqualIID(riid, &IID_IDirectSoundCapture))
630 if (This->flow == eCapture)
631 hr = CoCreateInstance(&CLSID_DirectSoundCapture8, NULL, clsctx, riid, ppv);
632 if (SUCCEEDED(hr))
634 hr = IDirectSoundCapture_Initialize((IDirectSoundCapture*)*ppv, &This->devguid);
635 if (FAILED(hr))
636 IDirectSoundCapture_Release((IDirectSoundCapture*)*ppv);
639 else if (IsEqualIID(riid, &IID_ISpatialAudioClient))
641 hr = SpatialAudioClient_Create(iface, (ISpatialAudioClient**)ppv);
643 else
644 ERR("Invalid/unknown iid %s\n", debugstr_guid(riid));
646 if (FAILED(hr))
647 *ppv = NULL;
649 TRACE("Returning %08x\n", hr);
650 return hr;
653 static HRESULT WINAPI MMDevice_OpenPropertyStore(IMMDevice *iface, DWORD access, IPropertyStore **ppv)
655 MMDevice *This = impl_from_IMMDevice(iface);
656 TRACE("(%p)->(%x,%p)\n", This, access, ppv);
658 if (!ppv)
659 return E_POINTER;
660 return MMDevPropStore_Create(This, access, ppv);
663 static HRESULT WINAPI MMDevice_GetId(IMMDevice *iface, WCHAR **itemid)
665 MMDevice *This = impl_from_IMMDevice(iface);
666 WCHAR *str;
667 GUID *id = &This->devguid;
669 TRACE("(%p)->(%p)\n", This, itemid);
670 if (!itemid)
671 return E_POINTER;
672 *itemid = str = CoTaskMemAlloc(56 * sizeof(WCHAR));
673 if (!str)
674 return E_OUTOFMEMORY;
675 wsprintfW(str, L"{0.0.%u.00000000}.{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
676 This->flow, id->Data1, id->Data2, id->Data3,
677 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
678 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7]);
679 TRACE("returning %s\n", wine_dbgstr_w(str));
680 return S_OK;
683 static HRESULT WINAPI MMDevice_GetState(IMMDevice *iface, DWORD *state)
685 MMDevice *This = impl_from_IMMDevice(iface);
686 TRACE("(%p)->(%p)\n", iface, state);
688 if (!state)
689 return E_POINTER;
690 *state = This->state;
691 return S_OK;
694 static const IMMDeviceVtbl MMDeviceVtbl =
696 MMDevice_QueryInterface,
697 MMDevice_AddRef,
698 MMDevice_Release,
699 MMDevice_Activate,
700 MMDevice_OpenPropertyStore,
701 MMDevice_GetId,
702 MMDevice_GetState
705 static inline MMDevice *impl_from_IMMEndpoint(IMMEndpoint *iface)
707 return CONTAINING_RECORD(iface, MMDevice, IMMEndpoint_iface);
710 static HRESULT WINAPI MMEndpoint_QueryInterface(IMMEndpoint *iface, REFIID riid, void **ppv)
712 MMDevice *This = impl_from_IMMEndpoint(iface);
713 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
714 return IMMDevice_QueryInterface(&This->IMMDevice_iface, riid, ppv);
717 static ULONG WINAPI MMEndpoint_AddRef(IMMEndpoint *iface)
719 MMDevice *This = impl_from_IMMEndpoint(iface);
720 TRACE("(%p)\n", This);
721 return IMMDevice_AddRef(&This->IMMDevice_iface);
724 static ULONG WINAPI MMEndpoint_Release(IMMEndpoint *iface)
726 MMDevice *This = impl_from_IMMEndpoint(iface);
727 TRACE("(%p)\n", This);
728 return IMMDevice_Release(&This->IMMDevice_iface);
731 static HRESULT WINAPI MMEndpoint_GetDataFlow(IMMEndpoint *iface, EDataFlow *flow)
733 MMDevice *This = impl_from_IMMEndpoint(iface);
734 TRACE("(%p)->(%p)\n", This, flow);
735 if (!flow)
736 return E_POINTER;
737 *flow = This->flow;
738 return S_OK;
741 static const IMMEndpointVtbl MMEndpointVtbl =
743 MMEndpoint_QueryInterface,
744 MMEndpoint_AddRef,
745 MMEndpoint_Release,
746 MMEndpoint_GetDataFlow
749 static HRESULT MMDevCol_Create(IMMDeviceCollection **ppv, EDataFlow flow, DWORD state)
751 MMDevColImpl *This;
753 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
754 *ppv = NULL;
755 if (!This)
756 return E_OUTOFMEMORY;
757 This->IMMDeviceCollection_iface.lpVtbl = &MMDevColVtbl;
758 This->ref = 1;
759 This->flow = flow;
760 This->state = state;
761 *ppv = &This->IMMDeviceCollection_iface;
762 return S_OK;
765 static void MMDevCol_Destroy(MMDevColImpl *This)
767 HeapFree(GetProcessHeap(), 0, This);
770 static HRESULT WINAPI MMDevCol_QueryInterface(IMMDeviceCollection *iface, REFIID riid, void **ppv)
772 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
773 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
775 if (!ppv)
776 return E_POINTER;
777 if (IsEqualIID(riid, &IID_IUnknown)
778 || IsEqualIID(riid, &IID_IMMDeviceCollection))
779 *ppv = &This->IMMDeviceCollection_iface;
780 else
781 *ppv = NULL;
782 if (!*ppv)
783 return E_NOINTERFACE;
784 IUnknown_AddRef((IUnknown*)*ppv);
785 return S_OK;
788 static ULONG WINAPI MMDevCol_AddRef(IMMDeviceCollection *iface)
790 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
791 LONG ref = InterlockedIncrement(&This->ref);
792 TRACE("Refcount now %i\n", ref);
793 return ref;
796 static ULONG WINAPI MMDevCol_Release(IMMDeviceCollection *iface)
798 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
799 LONG ref = InterlockedDecrement(&This->ref);
800 TRACE("Refcount now %i\n", ref);
801 if (!ref)
802 MMDevCol_Destroy(This);
803 return ref;
806 static HRESULT WINAPI MMDevCol_GetCount(IMMDeviceCollection *iface, UINT *numdevs)
808 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
809 DWORD i;
811 TRACE("(%p)->(%p)\n", This, numdevs);
812 if (!numdevs)
813 return E_POINTER;
815 *numdevs = 0;
816 for (i = 0; i < MMDevice_count; ++i)
818 MMDevice *cur = MMDevice_head[i];
819 if ((cur->flow == This->flow || This->flow == eAll)
820 && (cur->state & This->state))
821 ++(*numdevs);
823 return S_OK;
826 static HRESULT WINAPI MMDevCol_Item(IMMDeviceCollection *iface, UINT n, IMMDevice **dev)
828 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
829 DWORD i = 0, j = 0;
831 TRACE("(%p)->(%u, %p)\n", This, n, dev);
832 if (!dev)
833 return E_POINTER;
835 for (j = 0; j < MMDevice_count; ++j)
837 MMDevice *cur = MMDevice_head[j];
838 if ((cur->flow == This->flow || This->flow == eAll)
839 && (cur->state & This->state)
840 && i++ == n)
842 *dev = &cur->IMMDevice_iface;
843 IMMDevice_AddRef(*dev);
844 return S_OK;
847 WARN("Could not obtain item %u\n", n);
848 *dev = NULL;
849 return E_INVALIDARG;
852 static const IMMDeviceCollectionVtbl MMDevColVtbl =
854 MMDevCol_QueryInterface,
855 MMDevCol_AddRef,
856 MMDevCol_Release,
857 MMDevCol_GetCount,
858 MMDevCol_Item
861 HRESULT MMDevEnum_Create(REFIID riid, void **ppv)
863 MMDevEnumImpl *This = MMDevEnumerator;
865 if (!This)
867 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
868 *ppv = NULL;
869 if (!This)
870 return E_OUTOFMEMORY;
871 This->ref = 1;
872 This->IMMDeviceEnumerator_iface.lpVtbl = &MMDevEnumVtbl;
873 MMDevEnumerator = This;
875 load_devices_from_reg();
876 load_driver_devices(eRender);
877 load_driver_devices(eCapture);
879 return IMMDeviceEnumerator_QueryInterface(&This->IMMDeviceEnumerator_iface, riid, ppv);
882 void MMDevEnum_Free(void)
884 while (MMDevice_count)
885 MMDevice_Destroy(MMDevice_head[0]);
886 RegCloseKey(key_render);
887 RegCloseKey(key_capture);
888 key_render = key_capture = NULL;
889 HeapFree(GetProcessHeap(), 0, MMDevEnumerator);
890 MMDevEnumerator = NULL;
893 static HRESULT WINAPI MMDevEnum_QueryInterface(IMMDeviceEnumerator *iface, REFIID riid, void **ppv)
895 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
896 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
898 if (!ppv)
899 return E_POINTER;
900 if (IsEqualIID(riid, &IID_IUnknown)
901 || IsEqualIID(riid, &IID_IMMDeviceEnumerator))
902 *ppv = &This->IMMDeviceEnumerator_iface;
903 else
904 *ppv = NULL;
905 if (!*ppv)
906 return E_NOINTERFACE;
907 IUnknown_AddRef((IUnknown*)*ppv);
908 return S_OK;
911 static ULONG WINAPI MMDevEnum_AddRef(IMMDeviceEnumerator *iface)
913 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
914 LONG ref = InterlockedIncrement(&This->ref);
915 TRACE("Refcount now %i\n", ref);
916 return ref;
919 static ULONG WINAPI MMDevEnum_Release(IMMDeviceEnumerator *iface)
921 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
922 LONG ref = InterlockedDecrement(&This->ref);
923 if (!ref)
924 MMDevEnum_Free();
925 TRACE("Refcount now %i\n", ref);
926 return ref;
929 static HRESULT WINAPI MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator *iface, EDataFlow flow, DWORD mask, IMMDeviceCollection **devices)
931 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
932 TRACE("(%p)->(%u,%u,%p)\n", This, flow, mask, devices);
933 if (!devices)
934 return E_POINTER;
935 *devices = NULL;
936 if (flow >= EDataFlow_enum_count)
937 return E_INVALIDARG;
938 if (mask & ~DEVICE_STATEMASK_ALL)
939 return E_INVALIDARG;
940 return MMDevCol_Create(devices, flow, mask);
943 static HRESULT WINAPI MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator *iface, EDataFlow flow, ERole role, IMMDevice **device)
945 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
946 WCHAR reg_key[256];
947 HKEY key;
948 HRESULT hr;
950 TRACE("(%p)->(%u,%u,%p)\n", This, flow, role, device);
952 if (!device)
953 return E_POINTER;
955 if((flow != eRender && flow != eCapture) ||
956 (role != eConsole && role != eMultimedia && role != eCommunications)){
957 WARN("Unknown flow (%u) or role (%u)\n", flow, role);
958 return E_INVALIDARG;
961 *device = NULL;
963 if(!drvs.module_name[0])
964 return E_NOTFOUND;
966 lstrcpyW(reg_key, drv_keyW);
967 lstrcatW(reg_key, L"\\");
968 lstrcatW(reg_key, drvs.module_name);
970 if(RegOpenKeyW(HKEY_CURRENT_USER, reg_key, &key) == ERROR_SUCCESS){
971 const WCHAR *reg_x_name, *reg_vx_name;
972 WCHAR def_id[256];
973 DWORD size = sizeof(def_id), state;
975 if(flow == eRender){
976 reg_x_name = L"DefaultOutput";
977 reg_vx_name = L"DefaultVoiceOutput";
978 }else{
979 reg_x_name = L"DefaultInput";
980 reg_vx_name = L"DefaultVoiceInput";
983 if(role == eCommunications &&
984 RegQueryValueExW(key, reg_vx_name, 0, NULL,
985 (BYTE*)def_id, &size) == ERROR_SUCCESS){
986 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
987 if(SUCCEEDED(hr)){
988 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
989 state == DEVICE_STATE_ACTIVE){
990 RegCloseKey(key);
991 return S_OK;
995 TRACE("Unable to find voice device %s\n", wine_dbgstr_w(def_id));
998 if(RegQueryValueExW(key, reg_x_name, 0, NULL,
999 (BYTE*)def_id, &size) == ERROR_SUCCESS){
1000 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
1001 if(SUCCEEDED(hr)){
1002 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
1003 state == DEVICE_STATE_ACTIVE){
1004 RegCloseKey(key);
1005 return S_OK;
1009 TRACE("Unable to find device %s\n", wine_dbgstr_w(def_id));
1012 RegCloseKey(key);
1015 if (flow == eRender)
1016 *device = &MMDevice_def_play->IMMDevice_iface;
1017 else
1018 *device = &MMDevice_def_rec->IMMDevice_iface;
1020 if (!*device)
1021 return E_NOTFOUND;
1022 IMMDevice_AddRef(*device);
1023 return S_OK;
1026 static HRESULT WINAPI MMDevEnum_GetDevice(IMMDeviceEnumerator *iface, const WCHAR *name, IMMDevice **device)
1028 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1029 DWORD i=0;
1030 IMMDevice *dev = NULL;
1032 TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), device);
1034 if(!name || !device)
1035 return E_POINTER;
1037 if(!lstrcmpW(name, L"Wine info device")){
1038 *device = &info_device;
1039 return S_OK;
1042 for (i = 0; i < MMDevice_count; ++i)
1044 HRESULT hr;
1045 WCHAR *str;
1046 dev = &MMDevice_head[i]->IMMDevice_iface;
1047 hr = IMMDevice_GetId(dev, &str);
1048 if (FAILED(hr))
1050 WARN("GetId failed: %08x\n", hr);
1051 continue;
1054 if (str && !lstrcmpW(str, name))
1056 CoTaskMemFree(str);
1057 IMMDevice_AddRef(dev);
1058 *device = dev;
1059 return S_OK;
1061 CoTaskMemFree(str);
1063 TRACE("Could not find device %s\n", debugstr_w(name));
1064 return E_INVALIDARG;
1067 struct NotificationClientWrapper {
1068 IMMNotificationClient *client;
1069 struct list entry;
1072 static struct list g_notif_clients = LIST_INIT(g_notif_clients);
1073 static HANDLE g_notif_thread;
1075 static CRITICAL_SECTION g_notif_lock;
1076 static CRITICAL_SECTION_DEBUG g_notif_lock_debug =
1078 0, 0, &g_notif_lock,
1079 { &g_notif_lock_debug.ProcessLocksList, &g_notif_lock_debug.ProcessLocksList },
1080 0, 0, { (DWORD_PTR)(__FILE__ ": g_notif_lock") }
1082 static CRITICAL_SECTION g_notif_lock = { &g_notif_lock_debug, -1, 0, 0, 0, 0 };
1084 static void notify_clients(EDataFlow flow, ERole role, const WCHAR *id)
1086 struct NotificationClientWrapper *wrapper;
1087 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients,
1088 struct NotificationClientWrapper, entry)
1089 IMMNotificationClient_OnDefaultDeviceChanged(wrapper->client, flow,
1090 role, id);
1092 /* Windows 7 treats changes to eConsole as changes to eMultimedia */
1093 if(role == eConsole)
1094 notify_clients(flow, eMultimedia, id);
1097 static BOOL notify_if_changed(EDataFlow flow, ERole role, HKEY key,
1098 const WCHAR *val_name, WCHAR *old_val, IMMDevice *def_dev)
1100 WCHAR new_val[64], *id;
1101 DWORD size;
1102 HRESULT hr;
1104 size = sizeof(new_val);
1105 if(RegQueryValueExW(key, val_name, 0, NULL,
1106 (BYTE*)new_val, &size) != ERROR_SUCCESS){
1107 if(old_val[0] != 0){
1108 /* set by user -> system default */
1109 if(def_dev){
1110 hr = IMMDevice_GetId(def_dev, &id);
1111 if(FAILED(hr)){
1112 ERR("GetId failed: %08x\n", hr);
1113 return FALSE;
1115 }else
1116 id = NULL;
1118 notify_clients(flow, role, id);
1119 old_val[0] = 0;
1120 CoTaskMemFree(id);
1122 return TRUE;
1125 /* system default -> system default, noop */
1126 return FALSE;
1129 if(!lstrcmpW(old_val, new_val)){
1130 /* set by user -> same value */
1131 return FALSE;
1134 if(new_val[0] != 0){
1135 /* set by user -> different value */
1136 notify_clients(flow, role, new_val);
1137 memcpy(old_val, new_val, sizeof(new_val));
1138 return TRUE;
1141 /* set by user -> system default */
1142 if(def_dev){
1143 hr = IMMDevice_GetId(def_dev, &id);
1144 if(FAILED(hr)){
1145 ERR("GetId failed: %08x\n", hr);
1146 return FALSE;
1148 }else
1149 id = NULL;
1151 notify_clients(flow, role, id);
1152 old_val[0] = 0;
1153 CoTaskMemFree(id);
1155 return TRUE;
1158 static DWORD WINAPI notif_thread_proc(void *user)
1160 HKEY key;
1161 WCHAR reg_key[256];
1162 WCHAR out_name[64], vout_name[64], in_name[64], vin_name[64];
1163 DWORD size;
1165 lstrcpyW(reg_key, drv_keyW);
1166 lstrcatW(reg_key, L"\\");
1167 lstrcatW(reg_key, drvs.module_name);
1169 if(RegCreateKeyExW(HKEY_CURRENT_USER, reg_key, 0, NULL, 0,
1170 MAXIMUM_ALLOWED, NULL, &key, NULL) != ERROR_SUCCESS){
1171 ERR("RegCreateKeyEx failed: %u\n", GetLastError());
1172 return 1;
1175 size = sizeof(out_name);
1176 if(RegQueryValueExW(key, L"DefaultOutput", 0, NULL, (BYTE*)out_name, &size) != ERROR_SUCCESS)
1177 out_name[0] = 0;
1179 size = sizeof(vout_name);
1180 if(RegQueryValueExW(key, L"DefaultVoiceOutput", 0, NULL, (BYTE*)vout_name, &size) != ERROR_SUCCESS)
1181 vout_name[0] = 0;
1183 size = sizeof(in_name);
1184 if(RegQueryValueExW(key, L"DefaultInput", 0, NULL, (BYTE*)in_name, &size) != ERROR_SUCCESS)
1185 in_name[0] = 0;
1187 size = sizeof(vin_name);
1188 if(RegQueryValueExW(key, L"DefaultVoiceInput", 0, NULL, (BYTE*)vin_name, &size) != ERROR_SUCCESS)
1189 vin_name[0] = 0;
1191 while(1){
1192 if(RegNotifyChangeKeyValue(key, FALSE, REG_NOTIFY_CHANGE_LAST_SET,
1193 NULL, FALSE) != ERROR_SUCCESS){
1194 ERR("RegNotifyChangeKeyValue failed: %u\n", GetLastError());
1195 RegCloseKey(key);
1196 g_notif_thread = NULL;
1197 return 1;
1200 EnterCriticalSection(&g_notif_lock);
1202 notify_if_changed(eRender, eConsole, key, L"DefaultOutput",
1203 out_name, &MMDevice_def_play->IMMDevice_iface);
1204 notify_if_changed(eRender, eCommunications, key, L"DefaultVoiceOutput",
1205 vout_name, &MMDevice_def_play->IMMDevice_iface);
1206 notify_if_changed(eCapture, eConsole, key, L"DefaultInput",
1207 in_name, &MMDevice_def_rec->IMMDevice_iface);
1208 notify_if_changed(eCapture, eCommunications, key, L"DefaultVoiceInput",
1209 vin_name, &MMDevice_def_rec->IMMDevice_iface);
1211 LeaveCriticalSection(&g_notif_lock);
1214 RegCloseKey(key);
1216 g_notif_thread = NULL;
1218 return 0;
1221 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1223 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1224 struct NotificationClientWrapper *wrapper;
1226 TRACE("(%p)->(%p)\n", This, client);
1228 if(!client)
1229 return E_POINTER;
1231 wrapper = HeapAlloc(GetProcessHeap(), 0, sizeof(*wrapper));
1232 if(!wrapper)
1233 return E_OUTOFMEMORY;
1235 wrapper->client = client;
1237 EnterCriticalSection(&g_notif_lock);
1239 list_add_tail(&g_notif_clients, &wrapper->entry);
1241 if(!g_notif_thread){
1242 g_notif_thread = CreateThread(NULL, 0, notif_thread_proc, NULL, 0, NULL);
1243 if(!g_notif_thread)
1244 ERR("CreateThread failed: %u\n", GetLastError());
1247 LeaveCriticalSection(&g_notif_lock);
1249 return S_OK;
1252 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1254 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1255 struct NotificationClientWrapper *wrapper;
1257 TRACE("(%p)->(%p)\n", This, client);
1259 if(!client)
1260 return E_POINTER;
1262 EnterCriticalSection(&g_notif_lock);
1264 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients, struct NotificationClientWrapper, entry){
1265 if(wrapper->client == client){
1266 list_remove(&wrapper->entry);
1267 HeapFree(GetProcessHeap(), 0, wrapper);
1268 LeaveCriticalSection(&g_notif_lock);
1269 return S_OK;
1273 LeaveCriticalSection(&g_notif_lock);
1275 return E_NOTFOUND;
1278 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl =
1280 MMDevEnum_QueryInterface,
1281 MMDevEnum_AddRef,
1282 MMDevEnum_Release,
1283 MMDevEnum_EnumAudioEndpoints,
1284 MMDevEnum_GetDefaultAudioEndpoint,
1285 MMDevEnum_GetDevice,
1286 MMDevEnum_RegisterEndpointNotificationCallback,
1287 MMDevEnum_UnregisterEndpointNotificationCallback
1290 static HRESULT MMDevPropStore_Create(MMDevice *parent, DWORD access, IPropertyStore **ppv)
1292 MMDevPropStore *This;
1293 if (access != STGM_READ
1294 && access != STGM_WRITE
1295 && access != STGM_READWRITE)
1297 WARN("Invalid access %08x\n", access);
1298 return E_INVALIDARG;
1300 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1301 *ppv = &This->IPropertyStore_iface;
1302 if (!This)
1303 return E_OUTOFMEMORY;
1304 This->IPropertyStore_iface.lpVtbl = &MMDevPropVtbl;
1305 This->ref = 1;
1306 This->parent = parent;
1307 This->access = access;
1308 return S_OK;
1311 static void MMDevPropStore_Destroy(MMDevPropStore *This)
1313 HeapFree(GetProcessHeap(), 0, This);
1316 static HRESULT WINAPI MMDevPropStore_QueryInterface(IPropertyStore *iface, REFIID riid, void **ppv)
1318 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1319 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1321 if (!ppv)
1322 return E_POINTER;
1323 if (IsEqualIID(riid, &IID_IUnknown)
1324 || IsEqualIID(riid, &IID_IPropertyStore))
1325 *ppv = &This->IPropertyStore_iface;
1326 else
1327 *ppv = NULL;
1328 if (!*ppv)
1329 return E_NOINTERFACE;
1330 IUnknown_AddRef((IUnknown*)*ppv);
1331 return S_OK;
1334 static ULONG WINAPI MMDevPropStore_AddRef(IPropertyStore *iface)
1336 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1337 LONG ref = InterlockedIncrement(&This->ref);
1338 TRACE("Refcount now %i\n", ref);
1339 return ref;
1342 static ULONG WINAPI MMDevPropStore_Release(IPropertyStore *iface)
1344 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1345 LONG ref = InterlockedDecrement(&This->ref);
1346 TRACE("Refcount now %i\n", ref);
1347 if (!ref)
1348 MMDevPropStore_Destroy(This);
1349 return ref;
1352 static HRESULT WINAPI MMDevPropStore_GetCount(IPropertyStore *iface, DWORD *nprops)
1354 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1355 WCHAR buffer[50];
1356 DWORD i = 0;
1357 HKEY propkey;
1358 HRESULT hr;
1360 TRACE("(%p)->(%p)\n", iface, nprops);
1361 if (!nprops)
1362 return E_POINTER;
1363 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1364 if (FAILED(hr))
1365 return hr;
1366 *nprops = 0;
1367 do {
1368 DWORD len = ARRAY_SIZE(buffer);
1369 if (RegEnumValueW(propkey, i, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
1370 break;
1371 i++;
1372 } while (1);
1373 RegCloseKey(propkey);
1374 TRACE("Returning %i\n", i);
1375 *nprops = i;
1376 return S_OK;
1379 static HRESULT WINAPI MMDevPropStore_GetAt(IPropertyStore *iface, DWORD prop, PROPERTYKEY *key)
1381 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1382 WCHAR buffer[50];
1383 DWORD len = ARRAY_SIZE(buffer);
1384 HRESULT hr;
1385 HKEY propkey;
1387 TRACE("(%p)->(%u,%p)\n", iface, prop, key);
1388 if (!key)
1389 return E_POINTER;
1391 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1392 if (FAILED(hr))
1393 return hr;
1395 if (RegEnumValueW(propkey, prop, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS
1396 || len <= 39)
1398 WARN("GetAt %u failed\n", prop);
1399 return E_INVALIDARG;
1401 RegCloseKey(propkey);
1402 buffer[38] = 0;
1403 CLSIDFromString(buffer, &key->fmtid);
1404 key->pid = wcstol(&buffer[39], NULL, 10);
1405 return S_OK;
1408 static HRESULT WINAPI MMDevPropStore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *pv)
1410 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1411 TRACE("(%p)->(\"%s,%u\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1413 if (!key || !pv)
1414 return E_POINTER;
1415 if (This->access != STGM_READ
1416 && This->access != STGM_READWRITE)
1417 return STG_E_ACCESSDENIED;
1419 /* Special case */
1420 if (IsEqualPropertyKey(*key, PKEY_AudioEndpoint_GUID))
1422 pv->vt = VT_LPWSTR;
1423 pv->pwszVal = CoTaskMemAlloc(39 * sizeof(WCHAR));
1424 if (!pv->pwszVal)
1425 return E_OUTOFMEMORY;
1426 StringFromGUID2(&This->parent->devguid, pv->pwszVal, 39);
1427 return S_OK;
1430 return MMDevice_GetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1433 static HRESULT WINAPI MMDevPropStore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT pv)
1435 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1436 TRACE("(%p)->(\"%s,%u\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1438 if (!key || !pv)
1439 return E_POINTER;
1441 if (This->access != STGM_WRITE
1442 && This->access != STGM_READWRITE)
1443 return STG_E_ACCESSDENIED;
1444 return MMDevice_SetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1447 static HRESULT WINAPI MMDevPropStore_Commit(IPropertyStore *iface)
1449 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1450 TRACE("(%p)\n", iface);
1452 if (This->access != STGM_WRITE
1453 && This->access != STGM_READWRITE)
1454 return STG_E_ACCESSDENIED;
1456 /* Does nothing - for mmdevapi, the propstore values are written on SetValue,
1457 * not on Commit. */
1459 return S_OK;
1462 static const IPropertyStoreVtbl MMDevPropVtbl =
1464 MMDevPropStore_QueryInterface,
1465 MMDevPropStore_AddRef,
1466 MMDevPropStore_Release,
1467 MMDevPropStore_GetCount,
1468 MMDevPropStore_GetAt,
1469 MMDevPropStore_GetValue,
1470 MMDevPropStore_SetValue,
1471 MMDevPropStore_Commit
1475 /* Property bag for IBaseFilter activation */
1476 static HRESULT WINAPI PB_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
1478 ERR("Should not be called\n");
1479 *ppv = NULL;
1480 return E_NOINTERFACE;
1483 static ULONG WINAPI PB_AddRef(IPropertyBag *iface)
1485 ERR("Should not be called\n");
1486 return 2;
1489 static ULONG WINAPI PB_Release(IPropertyBag *iface)
1491 ERR("Should not be called\n");
1492 return 1;
1495 static HRESULT WINAPI PB_Read(IPropertyBag *iface, LPCOLESTR name, VARIANT *var, IErrorLog *log)
1497 IPropertyBagImpl *This = impl_from_IPropertyBag(iface);
1498 TRACE("Trying to read %s, type %u\n", debugstr_w(name), var->n1.n2.vt);
1499 if (!lstrcmpW(name, L"DSGuid"))
1501 WCHAR guidstr[39];
1502 StringFromGUID2(&This->devguid, guidstr,ARRAY_SIZE(guidstr));
1503 var->n1.n2.vt = VT_BSTR;
1504 var->n1.n2.n3.bstrVal = SysAllocString(guidstr);
1505 return S_OK;
1507 ERR("Unknown property '%s' queried\n", debugstr_w(name));
1508 return E_FAIL;
1511 static HRESULT WINAPI PB_Write(IPropertyBag *iface, LPCOLESTR name, VARIANT *var)
1513 ERR("Should not be called\n");
1514 return E_FAIL;
1517 static const IPropertyBagVtbl PB_Vtbl =
1519 PB_QueryInterface,
1520 PB_AddRef,
1521 PB_Release,
1522 PB_Read,
1523 PB_Write
1526 static ULONG WINAPI info_device_ps_AddRef(IPropertyStore *iface)
1528 return 2;
1531 static ULONG WINAPI info_device_ps_Release(IPropertyStore *iface)
1533 return 1;
1536 static HRESULT WINAPI info_device_ps_GetValue(IPropertyStore *iface,
1537 REFPROPERTYKEY key, PROPVARIANT *pv)
1539 TRACE("(static)->(\"%s,%u\", %p)\n", debugstr_guid(&key->fmtid), key ? key->pid : 0, pv);
1541 if (!key || !pv)
1542 return E_POINTER;
1544 if (IsEqualPropertyKey(*key, DEVPKEY_Device_Driver))
1546 INT size = (lstrlenW(drvs.module_name) + 1) * sizeof(WCHAR);
1547 pv->vt = VT_LPWSTR;
1548 pv->pwszVal = CoTaskMemAlloc(size);
1549 if (!pv->pwszVal)
1550 return E_OUTOFMEMORY;
1551 memcpy(pv->pwszVal, drvs.module_name, size);
1552 return S_OK;
1555 return E_INVALIDARG;
1558 static const IPropertyStoreVtbl info_device_ps_Vtbl =
1560 NULL,
1561 info_device_ps_AddRef,
1562 info_device_ps_Release,
1563 NULL,
1564 NULL,
1565 info_device_ps_GetValue,
1566 NULL,
1567 NULL
1570 static IPropertyStore info_device_ps = {
1571 &info_device_ps_Vtbl
1574 static ULONG WINAPI info_device_AddRef(IMMDevice *iface)
1576 return 2;
1579 static ULONG WINAPI info_device_Release(IMMDevice *iface)
1581 return 1;
1584 static HRESULT WINAPI info_device_OpenPropertyStore(IMMDevice *iface,
1585 DWORD access, IPropertyStore **ppv)
1587 TRACE("(static)->(%x, %p)\n", access, ppv);
1588 *ppv = &info_device_ps;
1589 return S_OK;
1592 static const IMMDeviceVtbl info_device_Vtbl =
1594 NULL,
1595 info_device_AddRef,
1596 info_device_Release,
1597 NULL,
1598 info_device_OpenPropertyStore,
1599 NULL,
1600 NULL
1603 static IMMDevice info_device = {
1604 &info_device_Vtbl