mmdevapi: Implement AudioSessionManager.
[wine.git] / dlls / mmdevapi / devenum.c
blob2b7833edd0829c33427da3b5974283e649552b00
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_private.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 MMDevice *MMDevice_def_rec, *MMDevice_def_play;
63 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl;
64 static const IMMDeviceCollectionVtbl MMDevColVtbl;
65 static const IMMDeviceVtbl MMDeviceVtbl;
66 static const IPropertyStoreVtbl MMDevPropVtbl;
67 static const IMMEndpointVtbl MMEndpointVtbl;
69 static MMDevEnumImpl enumerator;
70 static struct list device_list = LIST_INIT(device_list);
71 static IMMDevice info_device;
73 typedef struct MMDevColImpl
75 IMMDeviceCollection IMMDeviceCollection_iface;
76 LONG ref;
77 EDataFlow flow;
78 DWORD state;
79 } MMDevColImpl;
81 typedef struct IPropertyBagImpl {
82 IPropertyBag IPropertyBag_iface;
83 GUID devguid;
84 } IPropertyBagImpl;
86 static const IPropertyBagVtbl PB_Vtbl;
88 static HRESULT MMDevPropStore_Create(MMDevice *This, DWORD access, IPropertyStore **ppv);
90 static inline MMDevPropStore *impl_from_IPropertyStore(IPropertyStore *iface)
92 return CONTAINING_RECORD(iface, MMDevPropStore, IPropertyStore_iface);
95 static inline MMDevEnumImpl *impl_from_IMMDeviceEnumerator(IMMDeviceEnumerator *iface)
97 return CONTAINING_RECORD(iface, MMDevEnumImpl, IMMDeviceEnumerator_iface);
100 static inline MMDevColImpl *impl_from_IMMDeviceCollection(IMMDeviceCollection *iface)
102 return CONTAINING_RECORD(iface, MMDevColImpl, IMMDeviceCollection_iface);
105 static inline IPropertyBagImpl *impl_from_IPropertyBag(IPropertyBag *iface)
107 return CONTAINING_RECORD(iface, IPropertyBagImpl, IPropertyBag_iface);
110 static const WCHAR propkey_formatW[] = L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X},%d";
112 static HRESULT MMDevPropStore_OpenPropKey(const GUID *guid, DWORD flow, HKEY *propkey)
114 WCHAR buffer[39];
115 LONG ret;
116 HKEY key;
117 StringFromGUID2(guid, buffer, 39);
118 if ((ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, buffer, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, &key)) != ERROR_SUCCESS)
120 WARN("Opening key %s failed with %lu\n", debugstr_w(buffer), ret);
121 return E_FAIL;
123 ret = RegOpenKeyExW(key, L"Properties", 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, propkey);
124 RegCloseKey(key);
125 if (ret != ERROR_SUCCESS)
127 WARN("Opening key Properties failed with %lu\n", ret);
128 return E_FAIL;
130 return S_OK;
133 static HRESULT MMDevice_GetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, PROPVARIANT *pv)
135 WCHAR buffer[80];
136 const GUID *id = &key->fmtid;
137 DWORD type, size;
138 HRESULT hr = S_OK;
139 HKEY regkey;
140 LONG ret;
142 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
143 if (FAILED(hr))
144 return hr;
145 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
146 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
147 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
148 ret = RegGetValueW(regkey, NULL, buffer, RRF_RT_ANY, &type, NULL, &size);
149 if (ret != ERROR_SUCCESS)
151 WARN("Reading %s returned %ld\n", debugstr_w(buffer), ret);
152 RegCloseKey(regkey);
153 pv->vt = VT_EMPTY;
154 return S_OK;
157 switch (type)
159 case REG_SZ:
161 pv->vt = VT_LPWSTR;
162 pv->pwszVal = CoTaskMemAlloc(size);
163 if (!pv->pwszVal)
164 hr = E_OUTOFMEMORY;
165 else
166 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_SZ, NULL, (BYTE*)pv->pwszVal, &size);
167 break;
169 case REG_DWORD:
171 pv->vt = VT_UI4;
172 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_DWORD, NULL, (BYTE*)&pv->ulVal, &size);
173 break;
175 case REG_BINARY:
177 pv->vt = VT_BLOB;
178 pv->blob.cbSize = size;
179 pv->blob.pBlobData = CoTaskMemAlloc(size);
180 if (!pv->blob.pBlobData)
181 hr = E_OUTOFMEMORY;
182 else
183 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_BINARY, NULL, (BYTE*)pv->blob.pBlobData, &size);
184 break;
186 default:
187 ERR("Unknown/unhandled type: %lu\n", type);
188 PropVariantClear(pv);
189 break;
191 RegCloseKey(regkey);
192 return hr;
195 static HRESULT MMDevice_SetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, REFPROPVARIANT pv)
197 WCHAR buffer[80];
198 const GUID *id = &key->fmtid;
199 HRESULT hr;
200 HKEY regkey;
201 LONG ret;
203 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
204 if (FAILED(hr))
205 return hr;
206 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
207 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
208 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
209 switch (pv->vt)
211 case VT_UI4:
213 ret = RegSetValueExW(regkey, buffer, 0, REG_DWORD, (const BYTE*)&pv->ulVal, sizeof(DWORD));
214 break;
216 case VT_BLOB:
218 ret = RegSetValueExW(regkey, buffer, 0, REG_BINARY, pv->blob.pBlobData, pv->blob.cbSize);
219 TRACE("Blob %p %lu\n", pv->blob.pBlobData, pv->blob.cbSize);
221 break;
223 case VT_LPWSTR:
225 ret = RegSetValueExW(regkey, buffer, 0, REG_SZ, (const BYTE*)pv->pwszVal, sizeof(WCHAR)*(1+lstrlenW(pv->pwszVal)));
226 break;
228 default:
229 ret = 0;
230 FIXME("Unhandled type %u\n", pv->vt);
231 hr = E_INVALIDARG;
232 break;
234 RegCloseKey(regkey);
235 TRACE("Writing %s returned %lu\n", debugstr_w(buffer), ret);
236 return hr;
239 static HRESULT set_driver_prop_value(GUID *id, const EDataFlow flow, const PROPERTYKEY *prop)
241 HRESULT hr;
242 PROPVARIANT pv;
244 if (!drvs.pGetPropValue)
245 return E_NOTIMPL;
247 hr = drvs.pGetPropValue(id, prop, &pv);
249 if (SUCCEEDED(hr))
251 MMDevice_SetPropValue(id, flow, prop, &pv);
252 PropVariantClear(&pv);
255 return hr;
258 struct product_name_overrides
260 const WCHAR *id;
261 const WCHAR *product;
264 static const struct product_name_overrides product_name_overrides[] =
266 /* Sony controllers */
267 { .id = L"VID_054C&PID_0CE6", .product = L"Wireless Controller" },
270 static const WCHAR *find_product_name_override(const WCHAR *device_id)
272 const WCHAR *match_id = wcschr( device_id, '\\' ) + 1;
273 DWORD i;
275 for (i = 0; i < ARRAY_SIZE(product_name_overrides); ++i)
276 if (!wcsnicmp( product_name_overrides[i].id, match_id, 17 ))
277 return product_name_overrides[i].product;
279 return NULL;
282 /* Creates or updates the state of a device
283 * If GUID is null, a random guid will be assigned
284 * and the device will be created
286 static MMDevice *MMDevice_Create(WCHAR *name, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault)
288 HKEY key, root;
289 MMDevice *device, *cur = NULL;
290 WCHAR guidstr[39];
292 static const PROPERTYKEY deviceinterface_key = {
293 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
296 static const PROPERTYKEY devicepath_key = {
297 {0xb3f8fa53, 0x0004, 0x438e, {0x90, 0x03, 0x51, 0xa4, 0x6e, 0x13, 0x9b, 0xfc}}, 2
300 LIST_FOR_EACH_ENTRY(device, &device_list, MMDevice, entry)
302 if (device->flow == flow && IsEqualGUID(&device->devguid, id)){
303 cur = device;
304 break;
308 if(!cur){
309 /* No device found, allocate new one */
310 cur = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cur));
311 if (!cur)
312 return NULL;
314 cur->IMMDevice_iface.lpVtbl = &MMDeviceVtbl;
315 cur->IMMEndpoint_iface.lpVtbl = &MMEndpointVtbl;
317 InitializeCriticalSection(&cur->crst);
318 cur->crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MMDevice.crst");
320 list_add_tail(&device_list, &cur->entry);
321 }else if(cur->ref > 0)
322 WARN("Modifying an MMDevice with postitive reference count!\n");
324 HeapFree(GetProcessHeap(), 0, cur->drv_id);
325 cur->drv_id = name;
327 cur->flow = flow;
328 cur->state = state;
329 cur->devguid = *id;
331 StringFromGUID2(&cur->devguid, guidstr, ARRAY_SIZE(guidstr));
333 if (flow == eRender)
334 root = key_render;
335 else
336 root = key_capture;
338 if (RegCreateKeyExW(root, guidstr, 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &key, NULL) == ERROR_SUCCESS)
340 HKEY keyprop;
341 RegSetValueExW(key, L"DeviceState", 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
342 if (!RegCreateKeyExW(key, L"Properties", 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &keyprop, NULL))
344 PROPVARIANT pv;
346 pv.vt = VT_LPWSTR;
347 pv.pwszVal = name;
349 if (SUCCEEDED(set_driver_prop_value(id, flow, &devicepath_key))) {
350 PROPVARIANT pv2;
352 PropVariantInit(&pv2);
354 if (SUCCEEDED(MMDevice_GetPropValue(id, flow, &devicepath_key, &pv2)) && pv2.vt == VT_LPWSTR) {
355 const WCHAR *override;
356 if ((override = find_product_name_override(pv2.pwszVal)) != NULL)
357 pv.pwszVal = (WCHAR*) override;
360 PropVariantClear(&pv2);
363 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
364 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_DeviceInterface_FriendlyName, &pv);
365 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_DeviceDesc, &pv);
367 pv.pwszVal = guidstr;
368 MMDevice_SetPropValue(id, flow, &deviceinterface_key, &pv);
370 if (FAILED(set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_FormFactor)))
372 pv.vt = VT_UI4;
373 pv.ulVal = (flow == eCapture) ? Microphone : Speakers;
375 MMDevice_SetPropValue(id, flow, &PKEY_AudioEndpoint_FormFactor, &pv);
378 if (flow != eCapture)
380 PROPVARIANT pv2;
382 PropVariantInit(&pv2);
384 /* make read-write by not overwriting if already set */
385 if (FAILED(MMDevice_GetPropValue(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers, &pv2)) || pv2.vt != VT_UI4)
386 set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers);
388 PropVariantClear(&pv2);
391 RegCloseKey(keyprop);
393 RegCloseKey(key);
396 if (setdefault)
398 if (flow == eRender)
399 MMDevice_def_play = cur;
400 else
401 MMDevice_def_rec = cur;
403 return cur;
406 HRESULT load_devices_from_reg(void)
408 DWORD i = 0;
409 HKEY root, cur;
410 LONG ret;
411 DWORD curflow;
413 ret = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
414 L"Software\\Microsoft\\Windows\\CurrentVersion\\MMDevices\\Audio", 0, NULL, 0,
415 KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &root, NULL);
416 if (ret == ERROR_SUCCESS)
417 ret = RegCreateKeyExW(root, L"Capture", 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_capture, NULL);
418 if (ret == ERROR_SUCCESS)
419 ret = RegCreateKeyExW(root, L"Render", 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_render, NULL);
420 RegCloseKey(root);
421 cur = key_capture;
422 curflow = eCapture;
423 if (ret != ERROR_SUCCESS)
425 RegCloseKey(key_capture);
426 key_render = key_capture = NULL;
427 WARN("Couldn't create key: %lu\n", ret);
428 return E_FAIL;
431 do {
432 WCHAR guidvalue[39];
433 GUID guid;
434 DWORD len;
435 PROPVARIANT pv = { VT_EMPTY };
437 len = ARRAY_SIZE(guidvalue);
438 ret = RegEnumKeyExW(cur, i++, guidvalue, &len, NULL, NULL, NULL, NULL);
439 if (ret == ERROR_NO_MORE_ITEMS)
441 if (cur == key_capture)
443 cur = key_render;
444 curflow = eRender;
445 i = 0;
446 continue;
448 break;
450 if (ret != ERROR_SUCCESS)
451 continue;
452 if (SUCCEEDED(CLSIDFromString(guidvalue, &guid))
453 && SUCCEEDED(MMDevice_GetPropValue(&guid, curflow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv))
454 && pv.vt == VT_LPWSTR)
456 DWORD size_bytes = (lstrlenW(pv.pwszVal) + 1) * sizeof(WCHAR);
457 WCHAR *name = HeapAlloc(GetProcessHeap(), 0, size_bytes);
458 memcpy(name, pv.pwszVal, size_bytes);
459 MMDevice_Create(name, &guid, curflow,
460 DEVICE_STATE_NOTPRESENT, FALSE);
461 CoTaskMemFree(pv.pwszVal);
463 } while (1);
465 return S_OK;
468 static HRESULT set_format(MMDevice *dev)
470 HRESULT hr;
471 IAudioClient *client;
472 WAVEFORMATEX *fmt;
473 PROPVARIANT pv = { VT_EMPTY };
475 hr = drvs.pGetAudioEndpoint(&dev->devguid, &dev->IMMDevice_iface, &client);
476 if(FAILED(hr))
477 return hr;
479 hr = IAudioClient_GetMixFormat(client, &fmt);
480 if(FAILED(hr)){
481 IAudioClient_Release(client);
482 return hr;
485 IAudioClient_Release(client);
487 pv.vt = VT_BLOB;
488 pv.blob.cbSize = sizeof(WAVEFORMATEX) + fmt->cbSize;
489 pv.blob.pBlobData = (BYTE*)fmt;
490 MMDevice_SetPropValue(&dev->devguid, dev->flow,
491 &PKEY_AudioEngine_DeviceFormat, &pv);
492 MMDevice_SetPropValue(&dev->devguid, dev->flow,
493 &PKEY_AudioEngine_OEMFormat, &pv);
494 CoTaskMemFree(fmt);
496 return S_OK;
499 HRESULT load_driver_devices(EDataFlow flow)
501 WCHAR **ids;
502 GUID *guids;
503 UINT num, def, i;
504 HRESULT hr;
506 if(!drvs.pGetEndpointIDs)
507 return S_OK;
509 hr = drvs.pGetEndpointIDs(flow, &ids, &guids, &num, &def);
510 if(FAILED(hr))
511 return hr;
513 for(i = 0; i < num; ++i){
514 MMDevice *dev;
515 dev = MMDevice_Create(ids[i], &guids[i], flow, DEVICE_STATE_ACTIVE,
516 def == i);
517 set_format(dev);
520 HeapFree(GetProcessHeap(), 0, guids);
521 HeapFree(GetProcessHeap(), 0, ids);
523 return S_OK;
526 static void MMDevice_Destroy(MMDevice *This)
528 TRACE("Freeing %s\n", debugstr_w(This->drv_id));
529 list_remove(&This->entry);
530 This->crst.DebugInfo->Spare[0] = 0;
531 DeleteCriticalSection(&This->crst);
532 HeapFree(GetProcessHeap(), 0, This->drv_id);
533 HeapFree(GetProcessHeap(), 0, This);
536 static inline MMDevice *impl_from_IMMDevice(IMMDevice *iface)
538 return CONTAINING_RECORD(iface, MMDevice, IMMDevice_iface);
541 static HRESULT WINAPI MMDevice_QueryInterface(IMMDevice *iface, REFIID riid, void **ppv)
543 MMDevice *This = impl_from_IMMDevice(iface);
544 TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ppv);
546 if (!ppv)
547 return E_POINTER;
548 *ppv = NULL;
549 if (IsEqualIID(riid, &IID_IUnknown)
550 || IsEqualIID(riid, &IID_IMMDevice))
551 *ppv = &This->IMMDevice_iface;
552 else if (IsEqualIID(riid, &IID_IMMEndpoint))
553 *ppv = &This->IMMEndpoint_iface;
554 if (*ppv)
556 IUnknown_AddRef((IUnknown*)*ppv);
557 return S_OK;
559 WARN("Unknown interface %s\n", debugstr_guid(riid));
560 return E_NOINTERFACE;
563 static ULONG WINAPI MMDevice_AddRef(IMMDevice *iface)
565 MMDevice *This = impl_from_IMMDevice(iface);
566 LONG ref;
568 ref = InterlockedIncrement(&This->ref);
569 TRACE("Refcount now %li\n", ref);
570 return ref;
573 static ULONG WINAPI MMDevice_Release(IMMDevice *iface)
575 MMDevice *This = impl_from_IMMDevice(iface);
576 LONG ref;
578 ref = InterlockedDecrement(&This->ref);
579 TRACE("Refcount now %li\n", ref);
580 return ref;
583 static HRESULT WINAPI MMDevice_Activate(IMMDevice *iface, REFIID riid, DWORD clsctx, PROPVARIANT *params, void **ppv)
585 HRESULT hr = E_NOINTERFACE;
586 MMDevice *This = impl_from_IMMDevice(iface);
588 TRACE("(%p)->(%s, %lx, %p, %p)\n", iface, debugstr_guid(riid), clsctx, params, ppv);
590 if (!ppv)
591 return E_POINTER;
593 if (IsEqualIID(riid, &IID_IAudioClient) ||
594 IsEqualIID(riid, &IID_IAudioClient2) ||
595 IsEqualIID(riid, &IID_IAudioClient3)){
596 hr = drvs.pGetAudioEndpoint(&This->devguid, iface, (IAudioClient**)ppv);
597 }else if (IsEqualIID(riid, &IID_IAudioEndpointVolume) ||
598 IsEqualIID(riid, &IID_IAudioEndpointVolumeEx))
599 hr = AudioEndpointVolume_Create(This, (IAudioEndpointVolumeEx**)ppv);
600 else if (IsEqualIID(riid, &IID_IAudioSessionManager)
601 || IsEqualIID(riid, &IID_IAudioSessionManager2))
603 hr = AudioSessionManager_Create(iface, (IAudioSessionManager2**)ppv);
605 else if (IsEqualIID(riid, &IID_IBaseFilter))
607 if (This->flow == eRender)
608 hr = CoCreateInstance(&CLSID_DSoundRender, NULL, clsctx, riid, ppv);
609 else
610 ERR("Not supported for recording?\n");
611 if (SUCCEEDED(hr))
613 IPersistPropertyBag *ppb;
614 hr = IUnknown_QueryInterface((IUnknown*)*ppv, &IID_IPersistPropertyBag, (void **)&ppb);
615 if (SUCCEEDED(hr))
617 /* ::Load cannot assume the interface stays alive after the function returns,
618 * so just create the interface on the stack, saves a lot of complicated code */
619 IPropertyBagImpl bag = { { &PB_Vtbl } };
620 bag.devguid = This->devguid;
621 hr = IPersistPropertyBag_Load(ppb, &bag.IPropertyBag_iface, NULL);
622 IPersistPropertyBag_Release(ppb);
623 if (FAILED(hr))
624 IBaseFilter_Release((IBaseFilter*)*ppv);
626 else
628 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
629 hr = S_OK;
633 else if (IsEqualIID(riid, &IID_IDeviceTopology))
635 FIXME("IID_IDeviceTopology unsupported\n");
637 else if (IsEqualIID(riid, &IID_IDirectSound)
638 || IsEqualIID(riid, &IID_IDirectSound8))
640 if (This->flow == eRender)
641 hr = CoCreateInstance(&CLSID_DirectSound8, NULL, clsctx, riid, ppv);
642 if (SUCCEEDED(hr))
644 hr = IDirectSound_Initialize((IDirectSound*)*ppv, &This->devguid);
645 if (FAILED(hr))
646 IDirectSound_Release((IDirectSound*)*ppv);
649 else if (IsEqualIID(riid, &IID_IDirectSoundCapture))
651 if (This->flow == eCapture)
652 hr = CoCreateInstance(&CLSID_DirectSoundCapture8, NULL, clsctx, riid, ppv);
653 if (SUCCEEDED(hr))
655 hr = IDirectSoundCapture_Initialize((IDirectSoundCapture*)*ppv, &This->devguid);
656 if (FAILED(hr))
657 IDirectSoundCapture_Release((IDirectSoundCapture*)*ppv);
660 else if (IsEqualIID(riid, &IID_ISpatialAudioClient))
662 hr = SpatialAudioClient_Create(iface, (ISpatialAudioClient**)ppv);
664 else
665 ERR("Invalid/unknown iid %s\n", debugstr_guid(riid));
667 if (FAILED(hr))
668 *ppv = NULL;
670 TRACE("Returning %08lx\n", hr);
671 return hr;
674 static HRESULT WINAPI MMDevice_OpenPropertyStore(IMMDevice *iface, DWORD access, IPropertyStore **ppv)
676 MMDevice *This = impl_from_IMMDevice(iface);
677 TRACE("(%p)->(%lx,%p)\n", This, access, ppv);
679 if (!ppv)
680 return E_POINTER;
681 return MMDevPropStore_Create(This, access, ppv);
684 static HRESULT WINAPI MMDevice_GetId(IMMDevice *iface, WCHAR **itemid)
686 MMDevice *This = impl_from_IMMDevice(iface);
687 WCHAR *str;
688 GUID *id = &This->devguid;
690 TRACE("(%p)->(%p)\n", This, itemid);
691 if (!itemid)
692 return E_POINTER;
693 *itemid = str = CoTaskMemAlloc(56 * sizeof(WCHAR));
694 if (!str)
695 return E_OUTOFMEMORY;
696 wsprintfW(str, L"{0.0.%u.00000000}.{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
697 This->flow, id->Data1, id->Data2, id->Data3,
698 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
699 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7]);
700 TRACE("returning %s\n", wine_dbgstr_w(str));
701 return S_OK;
704 static HRESULT WINAPI MMDevice_GetState(IMMDevice *iface, DWORD *state)
706 MMDevice *This = impl_from_IMMDevice(iface);
707 TRACE("(%p)->(%p)\n", iface, state);
709 if (!state)
710 return E_POINTER;
711 *state = This->state;
712 return S_OK;
715 static const IMMDeviceVtbl MMDeviceVtbl =
717 MMDevice_QueryInterface,
718 MMDevice_AddRef,
719 MMDevice_Release,
720 MMDevice_Activate,
721 MMDevice_OpenPropertyStore,
722 MMDevice_GetId,
723 MMDevice_GetState
726 static inline MMDevice *impl_from_IMMEndpoint(IMMEndpoint *iface)
728 return CONTAINING_RECORD(iface, MMDevice, IMMEndpoint_iface);
731 static HRESULT WINAPI MMEndpoint_QueryInterface(IMMEndpoint *iface, REFIID riid, void **ppv)
733 MMDevice *This = impl_from_IMMEndpoint(iface);
734 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
735 return IMMDevice_QueryInterface(&This->IMMDevice_iface, riid, ppv);
738 static ULONG WINAPI MMEndpoint_AddRef(IMMEndpoint *iface)
740 MMDevice *This = impl_from_IMMEndpoint(iface);
741 TRACE("(%p)\n", This);
742 return IMMDevice_AddRef(&This->IMMDevice_iface);
745 static ULONG WINAPI MMEndpoint_Release(IMMEndpoint *iface)
747 MMDevice *This = impl_from_IMMEndpoint(iface);
748 TRACE("(%p)\n", This);
749 return IMMDevice_Release(&This->IMMDevice_iface);
752 static HRESULT WINAPI MMEndpoint_GetDataFlow(IMMEndpoint *iface, EDataFlow *flow)
754 MMDevice *This = impl_from_IMMEndpoint(iface);
755 TRACE("(%p)->(%p)\n", This, flow);
756 if (!flow)
757 return E_POINTER;
758 *flow = This->flow;
759 return S_OK;
762 static const IMMEndpointVtbl MMEndpointVtbl =
764 MMEndpoint_QueryInterface,
765 MMEndpoint_AddRef,
766 MMEndpoint_Release,
767 MMEndpoint_GetDataFlow
770 static HRESULT MMDevCol_Create(IMMDeviceCollection **ppv, EDataFlow flow, DWORD state)
772 MMDevColImpl *This;
774 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
775 *ppv = NULL;
776 if (!This)
777 return E_OUTOFMEMORY;
778 This->IMMDeviceCollection_iface.lpVtbl = &MMDevColVtbl;
779 This->ref = 1;
780 This->flow = flow;
781 This->state = state;
782 *ppv = &This->IMMDeviceCollection_iface;
783 return S_OK;
786 static void MMDevCol_Destroy(MMDevColImpl *This)
788 HeapFree(GetProcessHeap(), 0, This);
791 static HRESULT WINAPI MMDevCol_QueryInterface(IMMDeviceCollection *iface, REFIID riid, void **ppv)
793 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
794 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
796 if (!ppv)
797 return E_POINTER;
798 if (IsEqualIID(riid, &IID_IUnknown)
799 || IsEqualIID(riid, &IID_IMMDeviceCollection))
800 *ppv = &This->IMMDeviceCollection_iface;
801 else
802 *ppv = NULL;
803 if (!*ppv)
804 return E_NOINTERFACE;
805 IUnknown_AddRef((IUnknown*)*ppv);
806 return S_OK;
809 static ULONG WINAPI MMDevCol_AddRef(IMMDeviceCollection *iface)
811 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
812 LONG ref = InterlockedIncrement(&This->ref);
813 TRACE("Refcount now %li\n", ref);
814 return ref;
817 static ULONG WINAPI MMDevCol_Release(IMMDeviceCollection *iface)
819 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
820 LONG ref = InterlockedDecrement(&This->ref);
821 TRACE("Refcount now %li\n", ref);
822 if (!ref)
823 MMDevCol_Destroy(This);
824 return ref;
827 static HRESULT WINAPI MMDevCol_GetCount(IMMDeviceCollection *iface, UINT *numdevs)
829 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
830 MMDevice *cur;
832 TRACE("(%p)->(%p)\n", This, numdevs);
833 if (!numdevs)
834 return E_POINTER;
836 *numdevs = 0;
837 LIST_FOR_EACH_ENTRY(cur, &device_list, MMDevice, entry)
839 if ((cur->flow == This->flow || This->flow == eAll)
840 && (cur->state & This->state))
841 ++(*numdevs);
843 return S_OK;
846 static HRESULT WINAPI MMDevCol_Item(IMMDeviceCollection *iface, UINT n, IMMDevice **dev)
848 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
849 MMDevice *cur;
850 DWORD i = 0;
852 TRACE("(%p)->(%u, %p)\n", This, n, dev);
853 if (!dev)
854 return E_POINTER;
856 LIST_FOR_EACH_ENTRY(cur, &device_list, MMDevice, entry)
858 if ((cur->flow == This->flow || This->flow == eAll)
859 && (cur->state & This->state)
860 && i++ == n)
862 *dev = &cur->IMMDevice_iface;
863 IMMDevice_AddRef(*dev);
864 return S_OK;
867 WARN("Could not obtain item %u\n", n);
868 *dev = NULL;
869 return E_INVALIDARG;
872 static const IMMDeviceCollectionVtbl MMDevColVtbl =
874 MMDevCol_QueryInterface,
875 MMDevCol_AddRef,
876 MMDevCol_Release,
877 MMDevCol_GetCount,
878 MMDevCol_Item
881 HRESULT MMDevEnum_Create(REFIID riid, void **ppv)
883 return IMMDeviceEnumerator_QueryInterface(&enumerator.IMMDeviceEnumerator_iface, riid, ppv);
886 void MMDevEnum_Free(void)
888 MMDevice *device, *next;
889 LIST_FOR_EACH_ENTRY_SAFE(device, next, &device_list, MMDevice, entry)
890 MMDevice_Destroy(device);
891 RegCloseKey(key_render);
892 RegCloseKey(key_capture);
893 key_render = key_capture = NULL;
896 static HRESULT WINAPI MMDevEnum_QueryInterface(IMMDeviceEnumerator *iface, REFIID riid, void **ppv)
898 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
899 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
901 if (!ppv)
902 return E_POINTER;
903 if (IsEqualIID(riid, &IID_IUnknown)
904 || IsEqualIID(riid, &IID_IMMDeviceEnumerator))
905 *ppv = &This->IMMDeviceEnumerator_iface;
906 else
907 *ppv = NULL;
908 if (!*ppv)
909 return E_NOINTERFACE;
910 IUnknown_AddRef((IUnknown*)*ppv);
911 return S_OK;
914 static ULONG WINAPI MMDevEnum_AddRef(IMMDeviceEnumerator *iface)
916 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
917 LONG ref = InterlockedIncrement(&This->ref);
918 TRACE("Refcount now %li\n", ref);
919 return ref;
922 static ULONG WINAPI MMDevEnum_Release(IMMDeviceEnumerator *iface)
924 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
925 LONG ref = InterlockedDecrement(&This->ref);
926 TRACE("Refcount now %li\n", ref);
927 return ref;
930 static HRESULT WINAPI MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator *iface, EDataFlow flow, DWORD mask, IMMDeviceCollection **devices)
932 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
933 TRACE("(%p)->(%u,%lu,%p)\n", This, flow, mask, devices);
934 if (!devices)
935 return E_POINTER;
936 *devices = NULL;
937 if (flow >= EDataFlow_enum_count)
938 return E_INVALIDARG;
939 if (mask & ~DEVICE_STATEMASK_ALL)
940 return E_INVALIDARG;
941 return MMDevCol_Create(devices, flow, mask);
944 static HRESULT WINAPI MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator *iface, EDataFlow flow, ERole role, IMMDevice **device)
946 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
947 WCHAR reg_key[256];
948 HKEY key;
949 HRESULT hr;
951 TRACE("(%p)->(%u,%u,%p)\n", This, flow, role, device);
953 if (!device)
954 return E_POINTER;
956 if((flow != eRender && flow != eCapture) ||
957 (role != eConsole && role != eMultimedia && role != eCommunications)){
958 WARN("Unknown flow (%u) or role (%u)\n", flow, role);
959 return E_INVALIDARG;
962 *device = NULL;
964 if(!drvs.module_name[0])
965 return E_NOTFOUND;
967 lstrcpyW(reg_key, drv_keyW);
968 lstrcatW(reg_key, L"\\");
969 lstrcatW(reg_key, drvs.module_name);
971 if(RegOpenKeyW(HKEY_CURRENT_USER, reg_key, &key) == ERROR_SUCCESS){
972 const WCHAR *reg_x_name, *reg_vx_name;
973 WCHAR def_id[256];
974 DWORD size = sizeof(def_id), state;
976 if(flow == eRender){
977 reg_x_name = L"DefaultOutput";
978 reg_vx_name = L"DefaultVoiceOutput";
979 }else{
980 reg_x_name = L"DefaultInput";
981 reg_vx_name = L"DefaultVoiceInput";
984 if(role == eCommunications &&
985 RegQueryValueExW(key, reg_vx_name, 0, NULL,
986 (BYTE*)def_id, &size) == ERROR_SUCCESS){
987 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
988 if(SUCCEEDED(hr)){
989 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
990 state == DEVICE_STATE_ACTIVE){
991 RegCloseKey(key);
992 return S_OK;
996 TRACE("Unable to find voice device %s\n", wine_dbgstr_w(def_id));
999 if(RegQueryValueExW(key, reg_x_name, 0, NULL,
1000 (BYTE*)def_id, &size) == ERROR_SUCCESS){
1001 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
1002 if(SUCCEEDED(hr)){
1003 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
1004 state == DEVICE_STATE_ACTIVE){
1005 RegCloseKey(key);
1006 return S_OK;
1010 TRACE("Unable to find device %s\n", wine_dbgstr_w(def_id));
1013 RegCloseKey(key);
1016 if (flow == eRender)
1017 *device = &MMDevice_def_play->IMMDevice_iface;
1018 else
1019 *device = &MMDevice_def_rec->IMMDevice_iface;
1021 if (!*device)
1022 return E_NOTFOUND;
1023 IMMDevice_AddRef(*device);
1024 return S_OK;
1027 static HRESULT WINAPI MMDevEnum_GetDevice(IMMDeviceEnumerator *iface, const WCHAR *name, IMMDevice **device)
1029 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1030 MMDevice *impl;
1031 IMMDevice *dev = NULL;
1033 TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), device);
1035 if(!name || !device)
1036 return E_POINTER;
1038 if(!lstrcmpW(name, L"Wine info device")){
1039 *device = &info_device;
1040 return S_OK;
1043 LIST_FOR_EACH_ENTRY(impl, &device_list, MMDevice, entry)
1045 HRESULT hr;
1046 WCHAR *str;
1047 dev = &impl->IMMDevice_iface;
1048 hr = IMMDevice_GetId(dev, &str);
1049 if (FAILED(hr))
1051 WARN("GetId failed: %08lx\n", hr);
1052 continue;
1055 if (str && !lstrcmpW(str, name))
1057 CoTaskMemFree(str);
1058 IMMDevice_AddRef(dev);
1059 *device = dev;
1060 return S_OK;
1062 CoTaskMemFree(str);
1064 TRACE("Could not find device %s\n", debugstr_w(name));
1065 return E_INVALIDARG;
1068 struct NotificationClientWrapper {
1069 IMMNotificationClient *client;
1070 struct list entry;
1073 static struct list g_notif_clients = LIST_INIT(g_notif_clients);
1074 static HANDLE g_notif_thread;
1076 static CRITICAL_SECTION g_notif_lock;
1077 static CRITICAL_SECTION_DEBUG g_notif_lock_debug =
1079 0, 0, &g_notif_lock,
1080 { &g_notif_lock_debug.ProcessLocksList, &g_notif_lock_debug.ProcessLocksList },
1081 0, 0, { (DWORD_PTR)(__FILE__ ": g_notif_lock") }
1083 static CRITICAL_SECTION g_notif_lock = { &g_notif_lock_debug, -1, 0, 0, 0, 0 };
1085 static void notify_clients(EDataFlow flow, ERole role, const WCHAR *id)
1087 struct NotificationClientWrapper *wrapper;
1088 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients,
1089 struct NotificationClientWrapper, entry)
1090 IMMNotificationClient_OnDefaultDeviceChanged(wrapper->client, flow,
1091 role, id);
1093 /* Windows 7 treats changes to eConsole as changes to eMultimedia */
1094 if(role == eConsole)
1095 notify_clients(flow, eMultimedia, id);
1098 static BOOL notify_if_changed(EDataFlow flow, ERole role, HKEY key,
1099 const WCHAR *val_name, WCHAR *old_val, IMMDevice *def_dev)
1101 WCHAR new_val[64], *id;
1102 DWORD size;
1103 HRESULT hr;
1105 size = sizeof(new_val);
1106 if(RegQueryValueExW(key, val_name, 0, NULL,
1107 (BYTE*)new_val, &size) != ERROR_SUCCESS){
1108 if(old_val[0] != 0){
1109 /* set by user -> system default */
1110 if(def_dev){
1111 hr = IMMDevice_GetId(def_dev, &id);
1112 if(FAILED(hr)){
1113 ERR("GetId failed: %08lx\n", hr);
1114 return FALSE;
1116 }else
1117 id = NULL;
1119 notify_clients(flow, role, id);
1120 old_val[0] = 0;
1121 CoTaskMemFree(id);
1123 return TRUE;
1126 /* system default -> system default, noop */
1127 return FALSE;
1130 if(!lstrcmpW(old_val, new_val)){
1131 /* set by user -> same value */
1132 return FALSE;
1135 if(new_val[0] != 0){
1136 /* set by user -> different value */
1137 notify_clients(flow, role, new_val);
1138 memcpy(old_val, new_val, sizeof(new_val));
1139 return TRUE;
1142 /* set by user -> system default */
1143 if(def_dev){
1144 hr = IMMDevice_GetId(def_dev, &id);
1145 if(FAILED(hr)){
1146 ERR("GetId failed: %08lx\n", hr);
1147 return FALSE;
1149 }else
1150 id = NULL;
1152 notify_clients(flow, role, id);
1153 old_val[0] = 0;
1154 CoTaskMemFree(id);
1156 return TRUE;
1159 static DWORD WINAPI notif_thread_proc(void *user)
1161 HKEY key;
1162 WCHAR reg_key[256];
1163 WCHAR out_name[64], vout_name[64], in_name[64], vin_name[64];
1164 DWORD size;
1166 SetThreadDescription(GetCurrentThread(), L"wine_mmdevapi_notification");
1168 lstrcpyW(reg_key, drv_keyW);
1169 lstrcatW(reg_key, L"\\");
1170 lstrcatW(reg_key, drvs.module_name);
1172 if(RegCreateKeyExW(HKEY_CURRENT_USER, reg_key, 0, NULL, 0,
1173 MAXIMUM_ALLOWED, NULL, &key, NULL) != ERROR_SUCCESS){
1174 ERR("RegCreateKeyEx failed: %lu\n", GetLastError());
1175 return 1;
1178 size = sizeof(out_name);
1179 if(RegQueryValueExW(key, L"DefaultOutput", 0, NULL, (BYTE*)out_name, &size) != ERROR_SUCCESS)
1180 out_name[0] = 0;
1182 size = sizeof(vout_name);
1183 if(RegQueryValueExW(key, L"DefaultVoiceOutput", 0, NULL, (BYTE*)vout_name, &size) != ERROR_SUCCESS)
1184 vout_name[0] = 0;
1186 size = sizeof(in_name);
1187 if(RegQueryValueExW(key, L"DefaultInput", 0, NULL, (BYTE*)in_name, &size) != ERROR_SUCCESS)
1188 in_name[0] = 0;
1190 size = sizeof(vin_name);
1191 if(RegQueryValueExW(key, L"DefaultVoiceInput", 0, NULL, (BYTE*)vin_name, &size) != ERROR_SUCCESS)
1192 vin_name[0] = 0;
1194 while(1){
1195 if(RegNotifyChangeKeyValue(key, FALSE, REG_NOTIFY_CHANGE_LAST_SET,
1196 NULL, FALSE) != ERROR_SUCCESS){
1197 ERR("RegNotifyChangeKeyValue failed: %lu\n", GetLastError());
1198 RegCloseKey(key);
1199 g_notif_thread = NULL;
1200 return 1;
1203 EnterCriticalSection(&g_notif_lock);
1205 notify_if_changed(eRender, eConsole, key, L"DefaultOutput",
1206 out_name, &MMDevice_def_play->IMMDevice_iface);
1207 notify_if_changed(eRender, eCommunications, key, L"DefaultVoiceOutput",
1208 vout_name, &MMDevice_def_play->IMMDevice_iface);
1209 notify_if_changed(eCapture, eConsole, key, L"DefaultInput",
1210 in_name, &MMDevice_def_rec->IMMDevice_iface);
1211 notify_if_changed(eCapture, eCommunications, key, L"DefaultVoiceInput",
1212 vin_name, &MMDevice_def_rec->IMMDevice_iface);
1214 LeaveCriticalSection(&g_notif_lock);
1217 RegCloseKey(key);
1219 g_notif_thread = NULL;
1221 return 0;
1224 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1226 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1227 struct NotificationClientWrapper *wrapper;
1229 TRACE("(%p)->(%p)\n", This, client);
1231 if(!client)
1232 return E_POINTER;
1234 wrapper = HeapAlloc(GetProcessHeap(), 0, sizeof(*wrapper));
1235 if(!wrapper)
1236 return E_OUTOFMEMORY;
1238 wrapper->client = client;
1240 EnterCriticalSection(&g_notif_lock);
1242 list_add_tail(&g_notif_clients, &wrapper->entry);
1244 if(!g_notif_thread){
1245 g_notif_thread = CreateThread(NULL, 0, notif_thread_proc, NULL, 0, NULL);
1246 if(!g_notif_thread)
1247 ERR("CreateThread failed: %lu\n", GetLastError());
1250 LeaveCriticalSection(&g_notif_lock);
1252 return S_OK;
1255 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1257 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1258 struct NotificationClientWrapper *wrapper;
1260 TRACE("(%p)->(%p)\n", This, client);
1262 if(!client)
1263 return E_POINTER;
1265 EnterCriticalSection(&g_notif_lock);
1267 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients, struct NotificationClientWrapper, entry){
1268 if(wrapper->client == client){
1269 list_remove(&wrapper->entry);
1270 HeapFree(GetProcessHeap(), 0, wrapper);
1271 LeaveCriticalSection(&g_notif_lock);
1272 return S_OK;
1276 LeaveCriticalSection(&g_notif_lock);
1278 return E_NOTFOUND;
1281 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl =
1283 MMDevEnum_QueryInterface,
1284 MMDevEnum_AddRef,
1285 MMDevEnum_Release,
1286 MMDevEnum_EnumAudioEndpoints,
1287 MMDevEnum_GetDefaultAudioEndpoint,
1288 MMDevEnum_GetDevice,
1289 MMDevEnum_RegisterEndpointNotificationCallback,
1290 MMDevEnum_UnregisterEndpointNotificationCallback
1293 static MMDevEnumImpl enumerator =
1295 {&MMDevEnumVtbl},
1299 static HRESULT MMDevPropStore_Create(MMDevice *parent, DWORD access, IPropertyStore **ppv)
1301 MMDevPropStore *This;
1302 if (access != STGM_READ
1303 && access != STGM_WRITE
1304 && access != STGM_READWRITE)
1306 WARN("Invalid access %08lx\n", access);
1307 return E_INVALIDARG;
1309 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1310 *ppv = &This->IPropertyStore_iface;
1311 if (!This)
1312 return E_OUTOFMEMORY;
1313 This->IPropertyStore_iface.lpVtbl = &MMDevPropVtbl;
1314 This->ref = 1;
1315 This->parent = parent;
1316 This->access = access;
1317 return S_OK;
1320 static void MMDevPropStore_Destroy(MMDevPropStore *This)
1322 HeapFree(GetProcessHeap(), 0, This);
1325 static HRESULT WINAPI MMDevPropStore_QueryInterface(IPropertyStore *iface, REFIID riid, void **ppv)
1327 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1328 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1330 if (!ppv)
1331 return E_POINTER;
1332 if (IsEqualIID(riid, &IID_IUnknown)
1333 || IsEqualIID(riid, &IID_IPropertyStore))
1334 *ppv = &This->IPropertyStore_iface;
1335 else
1336 *ppv = NULL;
1337 if (!*ppv)
1338 return E_NOINTERFACE;
1339 IUnknown_AddRef((IUnknown*)*ppv);
1340 return S_OK;
1343 static ULONG WINAPI MMDevPropStore_AddRef(IPropertyStore *iface)
1345 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1346 LONG ref = InterlockedIncrement(&This->ref);
1347 TRACE("Refcount now %li\n", ref);
1348 return ref;
1351 static ULONG WINAPI MMDevPropStore_Release(IPropertyStore *iface)
1353 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1354 LONG ref = InterlockedDecrement(&This->ref);
1355 TRACE("Refcount now %li\n", ref);
1356 if (!ref)
1357 MMDevPropStore_Destroy(This);
1358 return ref;
1361 static HRESULT WINAPI MMDevPropStore_GetCount(IPropertyStore *iface, DWORD *nprops)
1363 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1364 WCHAR buffer[50];
1365 DWORD i = 0;
1366 HKEY propkey;
1367 HRESULT hr;
1369 TRACE("(%p)->(%p)\n", iface, nprops);
1370 if (!nprops)
1371 return E_POINTER;
1372 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1373 if (FAILED(hr))
1374 return hr;
1375 *nprops = 0;
1376 do {
1377 DWORD len = ARRAY_SIZE(buffer);
1378 if (RegEnumValueW(propkey, i, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
1379 break;
1380 i++;
1381 } while (1);
1382 RegCloseKey(propkey);
1383 TRACE("Returning %li\n", i);
1384 *nprops = i;
1385 return S_OK;
1388 static HRESULT WINAPI MMDevPropStore_GetAt(IPropertyStore *iface, DWORD prop, PROPERTYKEY *key)
1390 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1391 WCHAR buffer[50];
1392 DWORD len = ARRAY_SIZE(buffer);
1393 HRESULT hr;
1394 HKEY propkey;
1396 TRACE("(%p)->(%lu,%p)\n", iface, prop, key);
1397 if (!key)
1398 return E_POINTER;
1400 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1401 if (FAILED(hr))
1402 return hr;
1404 if (RegEnumValueW(propkey, prop, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS
1405 || len <= 39)
1407 WARN("GetAt %lu failed\n", prop);
1408 return E_INVALIDARG;
1410 RegCloseKey(propkey);
1411 buffer[38] = 0;
1412 CLSIDFromString(buffer, &key->fmtid);
1413 key->pid = wcstol(&buffer[39], NULL, 10);
1414 return S_OK;
1417 static HRESULT WINAPI MMDevPropStore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *pv)
1419 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1420 HRESULT hres;
1421 TRACE("(%p)->(\"%s,%lu\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1423 if (!key || !pv)
1424 return E_POINTER;
1425 if (This->access != STGM_READ
1426 && This->access != STGM_READWRITE)
1427 return STG_E_ACCESSDENIED;
1429 /* Special case */
1430 if (IsEqualPropertyKey(*key, PKEY_AudioEndpoint_GUID))
1432 pv->vt = VT_LPWSTR;
1433 pv->pwszVal = CoTaskMemAlloc(39 * sizeof(WCHAR));
1434 if (!pv->pwszVal)
1435 return E_OUTOFMEMORY;
1436 StringFromGUID2(&This->parent->devguid, pv->pwszVal, 39);
1437 return S_OK;
1440 hres = MMDevice_GetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1441 if (FAILED(hres))
1442 return hres;
1444 if (WARN_ON(mmdevapi))
1446 if ((IsEqualPropertyKey(*key, DEVPKEY_Device_FriendlyName) ||
1447 IsEqualPropertyKey(*key, DEVPKEY_DeviceInterface_FriendlyName) ||
1448 IsEqualPropertyKey(*key, DEVPKEY_Device_DeviceDesc)) &&
1449 pv->vt == VT_LPWSTR && wcslen(pv->pwszVal) > 62)
1450 WARN("Returned name exceeds length limit of some broken apps/libs, might crash: %s\n", debugstr_w(pv->pwszVal));
1452 return hres;
1455 static HRESULT WINAPI MMDevPropStore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT pv)
1457 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1458 TRACE("(%p)->(\"%s,%lu\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1460 if (!key || !pv)
1461 return E_POINTER;
1463 if (This->access != STGM_WRITE
1464 && This->access != STGM_READWRITE)
1465 return STG_E_ACCESSDENIED;
1466 return MMDevice_SetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1469 static HRESULT WINAPI MMDevPropStore_Commit(IPropertyStore *iface)
1471 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1472 TRACE("(%p)\n", iface);
1474 if (This->access != STGM_WRITE
1475 && This->access != STGM_READWRITE)
1476 return STG_E_ACCESSDENIED;
1478 /* Does nothing - for mmdevapi, the propstore values are written on SetValue,
1479 * not on Commit. */
1481 return S_OK;
1484 static const IPropertyStoreVtbl MMDevPropVtbl =
1486 MMDevPropStore_QueryInterface,
1487 MMDevPropStore_AddRef,
1488 MMDevPropStore_Release,
1489 MMDevPropStore_GetCount,
1490 MMDevPropStore_GetAt,
1491 MMDevPropStore_GetValue,
1492 MMDevPropStore_SetValue,
1493 MMDevPropStore_Commit
1497 /* Property bag for IBaseFilter activation */
1498 static HRESULT WINAPI PB_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
1500 ERR("Should not be called\n");
1501 *ppv = NULL;
1502 return E_NOINTERFACE;
1505 static ULONG WINAPI PB_AddRef(IPropertyBag *iface)
1507 ERR("Should not be called\n");
1508 return 2;
1511 static ULONG WINAPI PB_Release(IPropertyBag *iface)
1513 ERR("Should not be called\n");
1514 return 1;
1517 static HRESULT WINAPI PB_Read(IPropertyBag *iface, LPCOLESTR name, VARIANT *var, IErrorLog *log)
1519 IPropertyBagImpl *This = impl_from_IPropertyBag(iface);
1520 TRACE("Trying to read %s, type %u\n", debugstr_w(name), var->n1.n2.vt);
1521 if (!lstrcmpW(name, L"DSGuid"))
1523 WCHAR guidstr[39];
1524 StringFromGUID2(&This->devguid, guidstr,ARRAY_SIZE(guidstr));
1525 var->n1.n2.vt = VT_BSTR;
1526 var->n1.n2.n3.bstrVal = SysAllocString(guidstr);
1527 return S_OK;
1529 ERR("Unknown property '%s' queried\n", debugstr_w(name));
1530 return E_FAIL;
1533 static HRESULT WINAPI PB_Write(IPropertyBag *iface, LPCOLESTR name, VARIANT *var)
1535 ERR("Should not be called\n");
1536 return E_FAIL;
1539 static const IPropertyBagVtbl PB_Vtbl =
1541 PB_QueryInterface,
1542 PB_AddRef,
1543 PB_Release,
1544 PB_Read,
1545 PB_Write
1548 static ULONG WINAPI info_device_ps_AddRef(IPropertyStore *iface)
1550 return 2;
1553 static ULONG WINAPI info_device_ps_Release(IPropertyStore *iface)
1555 return 1;
1558 static HRESULT WINAPI info_device_ps_GetValue(IPropertyStore *iface,
1559 REFPROPERTYKEY key, PROPVARIANT *pv)
1561 TRACE("(static)->(\"%s,%lu\", %p)\n", debugstr_guid(&key->fmtid), key ? key->pid : 0, pv);
1563 if (!key || !pv)
1564 return E_POINTER;
1566 if (IsEqualPropertyKey(*key, DEVPKEY_Device_Driver))
1568 INT size = (lstrlenW(drvs.module_name) + 1) * sizeof(WCHAR);
1569 pv->vt = VT_LPWSTR;
1570 pv->pwszVal = CoTaskMemAlloc(size);
1571 if (!pv->pwszVal)
1572 return E_OUTOFMEMORY;
1573 memcpy(pv->pwszVal, drvs.module_name, size);
1574 return S_OK;
1577 return E_INVALIDARG;
1580 static const IPropertyStoreVtbl info_device_ps_Vtbl =
1582 NULL,
1583 info_device_ps_AddRef,
1584 info_device_ps_Release,
1585 NULL,
1586 NULL,
1587 info_device_ps_GetValue,
1588 NULL,
1589 NULL
1592 static IPropertyStore info_device_ps = {
1593 &info_device_ps_Vtbl
1596 static ULONG WINAPI info_device_AddRef(IMMDevice *iface)
1598 return 2;
1601 static ULONG WINAPI info_device_Release(IMMDevice *iface)
1603 return 1;
1606 static HRESULT WINAPI info_device_OpenPropertyStore(IMMDevice *iface,
1607 DWORD access, IPropertyStore **ppv)
1609 TRACE("(static)->(%lx, %p)\n", access, ppv);
1610 *ppv = &info_device_ps;
1611 return S_OK;
1614 static const IMMDeviceVtbl info_device_Vtbl =
1616 NULL,
1617 info_device_AddRef,
1618 info_device_Release,
1619 NULL,
1620 info_device_OpenPropertyStore,
1621 NULL,
1622 NULL
1625 static IMMDevice info_device = {
1626 &info_device_Vtbl