ws2_32: Return a valid value for WSAIoctl SIO_IDEAL_SEND_BACKLOG_QUERY.
[wine.git] / dlls / mmdevapi / devenum.c
blobbfeb3f3ecd8469b67bd0c21249cbfdba2d0d7260
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 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 = drvs.pGetAudioSessionManager(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 lstrcpyW(reg_key, drv_keyW);
1167 lstrcatW(reg_key, L"\\");
1168 lstrcatW(reg_key, drvs.module_name);
1170 if(RegCreateKeyExW(HKEY_CURRENT_USER, reg_key, 0, NULL, 0,
1171 MAXIMUM_ALLOWED, NULL, &key, NULL) != ERROR_SUCCESS){
1172 ERR("RegCreateKeyEx failed: %lu\n", GetLastError());
1173 return 1;
1176 size = sizeof(out_name);
1177 if(RegQueryValueExW(key, L"DefaultOutput", 0, NULL, (BYTE*)out_name, &size) != ERROR_SUCCESS)
1178 out_name[0] = 0;
1180 size = sizeof(vout_name);
1181 if(RegQueryValueExW(key, L"DefaultVoiceOutput", 0, NULL, (BYTE*)vout_name, &size) != ERROR_SUCCESS)
1182 vout_name[0] = 0;
1184 size = sizeof(in_name);
1185 if(RegQueryValueExW(key, L"DefaultInput", 0, NULL, (BYTE*)in_name, &size) != ERROR_SUCCESS)
1186 in_name[0] = 0;
1188 size = sizeof(vin_name);
1189 if(RegQueryValueExW(key, L"DefaultVoiceInput", 0, NULL, (BYTE*)vin_name, &size) != ERROR_SUCCESS)
1190 vin_name[0] = 0;
1192 while(1){
1193 if(RegNotifyChangeKeyValue(key, FALSE, REG_NOTIFY_CHANGE_LAST_SET,
1194 NULL, FALSE) != ERROR_SUCCESS){
1195 ERR("RegNotifyChangeKeyValue failed: %lu\n", GetLastError());
1196 RegCloseKey(key);
1197 g_notif_thread = NULL;
1198 return 1;
1201 EnterCriticalSection(&g_notif_lock);
1203 notify_if_changed(eRender, eConsole, key, L"DefaultOutput",
1204 out_name, &MMDevice_def_play->IMMDevice_iface);
1205 notify_if_changed(eRender, eCommunications, key, L"DefaultVoiceOutput",
1206 vout_name, &MMDevice_def_play->IMMDevice_iface);
1207 notify_if_changed(eCapture, eConsole, key, L"DefaultInput",
1208 in_name, &MMDevice_def_rec->IMMDevice_iface);
1209 notify_if_changed(eCapture, eCommunications, key, L"DefaultVoiceInput",
1210 vin_name, &MMDevice_def_rec->IMMDevice_iface);
1212 LeaveCriticalSection(&g_notif_lock);
1215 RegCloseKey(key);
1217 g_notif_thread = NULL;
1219 return 0;
1222 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1224 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1225 struct NotificationClientWrapper *wrapper;
1227 TRACE("(%p)->(%p)\n", This, client);
1229 if(!client)
1230 return E_POINTER;
1232 wrapper = HeapAlloc(GetProcessHeap(), 0, sizeof(*wrapper));
1233 if(!wrapper)
1234 return E_OUTOFMEMORY;
1236 wrapper->client = client;
1238 EnterCriticalSection(&g_notif_lock);
1240 list_add_tail(&g_notif_clients, &wrapper->entry);
1242 if(!g_notif_thread){
1243 g_notif_thread = CreateThread(NULL, 0, notif_thread_proc, NULL, 0, NULL);
1244 if(!g_notif_thread)
1245 ERR("CreateThread failed: %lu\n", GetLastError());
1248 LeaveCriticalSection(&g_notif_lock);
1250 return S_OK;
1253 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1255 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1256 struct NotificationClientWrapper *wrapper;
1258 TRACE("(%p)->(%p)\n", This, client);
1260 if(!client)
1261 return E_POINTER;
1263 EnterCriticalSection(&g_notif_lock);
1265 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients, struct NotificationClientWrapper, entry){
1266 if(wrapper->client == client){
1267 list_remove(&wrapper->entry);
1268 HeapFree(GetProcessHeap(), 0, wrapper);
1269 LeaveCriticalSection(&g_notif_lock);
1270 return S_OK;
1274 LeaveCriticalSection(&g_notif_lock);
1276 return E_NOTFOUND;
1279 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl =
1281 MMDevEnum_QueryInterface,
1282 MMDevEnum_AddRef,
1283 MMDevEnum_Release,
1284 MMDevEnum_EnumAudioEndpoints,
1285 MMDevEnum_GetDefaultAudioEndpoint,
1286 MMDevEnum_GetDevice,
1287 MMDevEnum_RegisterEndpointNotificationCallback,
1288 MMDevEnum_UnregisterEndpointNotificationCallback
1291 static MMDevEnumImpl enumerator =
1293 {&MMDevEnumVtbl},
1297 static HRESULT MMDevPropStore_Create(MMDevice *parent, DWORD access, IPropertyStore **ppv)
1299 MMDevPropStore *This;
1300 if (access != STGM_READ
1301 && access != STGM_WRITE
1302 && access != STGM_READWRITE)
1304 WARN("Invalid access %08lx\n", access);
1305 return E_INVALIDARG;
1307 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1308 *ppv = &This->IPropertyStore_iface;
1309 if (!This)
1310 return E_OUTOFMEMORY;
1311 This->IPropertyStore_iface.lpVtbl = &MMDevPropVtbl;
1312 This->ref = 1;
1313 This->parent = parent;
1314 This->access = access;
1315 return S_OK;
1318 static void MMDevPropStore_Destroy(MMDevPropStore *This)
1320 HeapFree(GetProcessHeap(), 0, This);
1323 static HRESULT WINAPI MMDevPropStore_QueryInterface(IPropertyStore *iface, REFIID riid, void **ppv)
1325 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1326 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1328 if (!ppv)
1329 return E_POINTER;
1330 if (IsEqualIID(riid, &IID_IUnknown)
1331 || IsEqualIID(riid, &IID_IPropertyStore))
1332 *ppv = &This->IPropertyStore_iface;
1333 else
1334 *ppv = NULL;
1335 if (!*ppv)
1336 return E_NOINTERFACE;
1337 IUnknown_AddRef((IUnknown*)*ppv);
1338 return S_OK;
1341 static ULONG WINAPI MMDevPropStore_AddRef(IPropertyStore *iface)
1343 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1344 LONG ref = InterlockedIncrement(&This->ref);
1345 TRACE("Refcount now %li\n", ref);
1346 return ref;
1349 static ULONG WINAPI MMDevPropStore_Release(IPropertyStore *iface)
1351 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1352 LONG ref = InterlockedDecrement(&This->ref);
1353 TRACE("Refcount now %li\n", ref);
1354 if (!ref)
1355 MMDevPropStore_Destroy(This);
1356 return ref;
1359 static HRESULT WINAPI MMDevPropStore_GetCount(IPropertyStore *iface, DWORD *nprops)
1361 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1362 WCHAR buffer[50];
1363 DWORD i = 0;
1364 HKEY propkey;
1365 HRESULT hr;
1367 TRACE("(%p)->(%p)\n", iface, nprops);
1368 if (!nprops)
1369 return E_POINTER;
1370 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1371 if (FAILED(hr))
1372 return hr;
1373 *nprops = 0;
1374 do {
1375 DWORD len = ARRAY_SIZE(buffer);
1376 if (RegEnumValueW(propkey, i, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
1377 break;
1378 i++;
1379 } while (1);
1380 RegCloseKey(propkey);
1381 TRACE("Returning %li\n", i);
1382 *nprops = i;
1383 return S_OK;
1386 static HRESULT WINAPI MMDevPropStore_GetAt(IPropertyStore *iface, DWORD prop, PROPERTYKEY *key)
1388 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1389 WCHAR buffer[50];
1390 DWORD len = ARRAY_SIZE(buffer);
1391 HRESULT hr;
1392 HKEY propkey;
1394 TRACE("(%p)->(%lu,%p)\n", iface, prop, key);
1395 if (!key)
1396 return E_POINTER;
1398 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1399 if (FAILED(hr))
1400 return hr;
1402 if (RegEnumValueW(propkey, prop, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS
1403 || len <= 39)
1405 WARN("GetAt %lu failed\n", prop);
1406 return E_INVALIDARG;
1408 RegCloseKey(propkey);
1409 buffer[38] = 0;
1410 CLSIDFromString(buffer, &key->fmtid);
1411 key->pid = wcstol(&buffer[39], NULL, 10);
1412 return S_OK;
1415 static HRESULT WINAPI MMDevPropStore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *pv)
1417 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1418 HRESULT hres;
1419 TRACE("(%p)->(\"%s,%lu\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1421 if (!key || !pv)
1422 return E_POINTER;
1423 if (This->access != STGM_READ
1424 && This->access != STGM_READWRITE)
1425 return STG_E_ACCESSDENIED;
1427 /* Special case */
1428 if (IsEqualPropertyKey(*key, PKEY_AudioEndpoint_GUID))
1430 pv->vt = VT_LPWSTR;
1431 pv->pwszVal = CoTaskMemAlloc(39 * sizeof(WCHAR));
1432 if (!pv->pwszVal)
1433 return E_OUTOFMEMORY;
1434 StringFromGUID2(&This->parent->devguid, pv->pwszVal, 39);
1435 return S_OK;
1438 hres = MMDevice_GetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1439 if (FAILED(hres))
1440 return hres;
1442 if (WARN_ON(mmdevapi))
1444 if ((IsEqualPropertyKey(*key, DEVPKEY_Device_FriendlyName) ||
1445 IsEqualPropertyKey(*key, DEVPKEY_DeviceInterface_FriendlyName) ||
1446 IsEqualPropertyKey(*key, DEVPKEY_Device_DeviceDesc)) &&
1447 pv->vt == VT_LPWSTR && wcslen(pv->pwszVal) > 62)
1448 WARN("Returned name exceeds length limit of some broken apps/libs, might crash: %s\n", debugstr_w(pv->pwszVal));
1450 return hres;
1453 static HRESULT WINAPI MMDevPropStore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT pv)
1455 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1456 TRACE("(%p)->(\"%s,%lu\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1458 if (!key || !pv)
1459 return E_POINTER;
1461 if (This->access != STGM_WRITE
1462 && This->access != STGM_READWRITE)
1463 return STG_E_ACCESSDENIED;
1464 return MMDevice_SetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1467 static HRESULT WINAPI MMDevPropStore_Commit(IPropertyStore *iface)
1469 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1470 TRACE("(%p)\n", iface);
1472 if (This->access != STGM_WRITE
1473 && This->access != STGM_READWRITE)
1474 return STG_E_ACCESSDENIED;
1476 /* Does nothing - for mmdevapi, the propstore values are written on SetValue,
1477 * not on Commit. */
1479 return S_OK;
1482 static const IPropertyStoreVtbl MMDevPropVtbl =
1484 MMDevPropStore_QueryInterface,
1485 MMDevPropStore_AddRef,
1486 MMDevPropStore_Release,
1487 MMDevPropStore_GetCount,
1488 MMDevPropStore_GetAt,
1489 MMDevPropStore_GetValue,
1490 MMDevPropStore_SetValue,
1491 MMDevPropStore_Commit
1495 /* Property bag for IBaseFilter activation */
1496 static HRESULT WINAPI PB_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
1498 ERR("Should not be called\n");
1499 *ppv = NULL;
1500 return E_NOINTERFACE;
1503 static ULONG WINAPI PB_AddRef(IPropertyBag *iface)
1505 ERR("Should not be called\n");
1506 return 2;
1509 static ULONG WINAPI PB_Release(IPropertyBag *iface)
1511 ERR("Should not be called\n");
1512 return 1;
1515 static HRESULT WINAPI PB_Read(IPropertyBag *iface, LPCOLESTR name, VARIANT *var, IErrorLog *log)
1517 IPropertyBagImpl *This = impl_from_IPropertyBag(iface);
1518 TRACE("Trying to read %s, type %u\n", debugstr_w(name), var->n1.n2.vt);
1519 if (!lstrcmpW(name, L"DSGuid"))
1521 WCHAR guidstr[39];
1522 StringFromGUID2(&This->devguid, guidstr,ARRAY_SIZE(guidstr));
1523 var->n1.n2.vt = VT_BSTR;
1524 var->n1.n2.n3.bstrVal = SysAllocString(guidstr);
1525 return S_OK;
1527 ERR("Unknown property '%s' queried\n", debugstr_w(name));
1528 return E_FAIL;
1531 static HRESULT WINAPI PB_Write(IPropertyBag *iface, LPCOLESTR name, VARIANT *var)
1533 ERR("Should not be called\n");
1534 return E_FAIL;
1537 static const IPropertyBagVtbl PB_Vtbl =
1539 PB_QueryInterface,
1540 PB_AddRef,
1541 PB_Release,
1542 PB_Read,
1543 PB_Write
1546 static ULONG WINAPI info_device_ps_AddRef(IPropertyStore *iface)
1548 return 2;
1551 static ULONG WINAPI info_device_ps_Release(IPropertyStore *iface)
1553 return 1;
1556 static HRESULT WINAPI info_device_ps_GetValue(IPropertyStore *iface,
1557 REFPROPERTYKEY key, PROPVARIANT *pv)
1559 TRACE("(static)->(\"%s,%lu\", %p)\n", debugstr_guid(&key->fmtid), key ? key->pid : 0, pv);
1561 if (!key || !pv)
1562 return E_POINTER;
1564 if (IsEqualPropertyKey(*key, DEVPKEY_Device_Driver))
1566 INT size = (lstrlenW(drvs.module_name) + 1) * sizeof(WCHAR);
1567 pv->vt = VT_LPWSTR;
1568 pv->pwszVal = CoTaskMemAlloc(size);
1569 if (!pv->pwszVal)
1570 return E_OUTOFMEMORY;
1571 memcpy(pv->pwszVal, drvs.module_name, size);
1572 return S_OK;
1575 return E_INVALIDARG;
1578 static const IPropertyStoreVtbl info_device_ps_Vtbl =
1580 NULL,
1581 info_device_ps_AddRef,
1582 info_device_ps_Release,
1583 NULL,
1584 NULL,
1585 info_device_ps_GetValue,
1586 NULL,
1587 NULL
1590 static IPropertyStore info_device_ps = {
1591 &info_device_ps_Vtbl
1594 static ULONG WINAPI info_device_AddRef(IMMDevice *iface)
1596 return 2;
1599 static ULONG WINAPI info_device_Release(IMMDevice *iface)
1601 return 1;
1604 static HRESULT WINAPI info_device_OpenPropertyStore(IMMDevice *iface,
1605 DWORD access, IPropertyStore **ppv)
1607 TRACE("(static)->(%lx, %p)\n", access, ppv);
1608 *ppv = &info_device_ps;
1609 return S_OK;
1612 static const IMMDeviceVtbl info_device_Vtbl =
1614 NULL,
1615 info_device_AddRef,
1616 info_device_Release,
1617 NULL,
1618 info_device_OpenPropertyStore,
1619 NULL,
1620 NULL
1623 static IMMDevice info_device = {
1624 &info_device_Vtbl