include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / mmdevapi / devenum.c
blob52c88ad0a034746cc652a6cc53a095bd1f669ecc
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>
20 #include <wchar.h>
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 DEFINE_GUID(GUID_NULL,0,0,0,0,0,0,0,0,0,0,0);
47 static HKEY key_render;
48 static HKEY key_capture;
50 typedef struct MMDevPropStoreImpl
52 IPropertyStore IPropertyStore_iface;
53 LONG ref;
54 MMDevice *parent;
55 DWORD access;
56 } MMDevPropStore;
58 typedef struct MMDevEnumImpl
60 IMMDeviceEnumerator IMMDeviceEnumerator_iface;
61 LONG ref;
62 } MMDevEnumImpl;
64 static MMDevice *MMDevice_def_rec, *MMDevice_def_play;
65 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl;
66 static const IMMDeviceCollectionVtbl MMDevColVtbl;
67 static const IMMDeviceVtbl MMDeviceVtbl;
68 static const IPropertyStoreVtbl MMDevPropVtbl;
69 static const IMMEndpointVtbl MMEndpointVtbl;
71 static MMDevEnumImpl enumerator;
72 static struct list device_list = LIST_INIT(device_list);
73 static IMMDevice info_device;
75 typedef struct MMDevColImpl
77 IMMDeviceCollection IMMDeviceCollection_iface;
78 LONG ref;
79 IMMDevice **devices;
80 UINT devices_count;
81 } MMDevColImpl;
83 typedef struct IPropertyBagImpl {
84 IPropertyBag IPropertyBag_iface;
85 GUID devguid;
86 } IPropertyBagImpl;
88 static const IPropertyBagVtbl PB_Vtbl;
90 typedef struct IConnectorImpl {
91 IConnector IConnector_iface;
92 LONG ref;
93 } IConnectorImpl;
95 typedef struct IDeviceTopologyImpl {
96 IDeviceTopology IDeviceTopology_iface;
97 LONG ref;
98 } IDeviceTopologyImpl;
100 static HRESULT MMDevPropStore_Create(MMDevice *This, DWORD access, IPropertyStore **ppv);
102 static inline MMDevPropStore *impl_from_IPropertyStore(IPropertyStore *iface)
104 return CONTAINING_RECORD(iface, MMDevPropStore, IPropertyStore_iface);
107 static inline MMDevEnumImpl *impl_from_IMMDeviceEnumerator(IMMDeviceEnumerator *iface)
109 return CONTAINING_RECORD(iface, MMDevEnumImpl, IMMDeviceEnumerator_iface);
112 static inline MMDevColImpl *impl_from_IMMDeviceCollection(IMMDeviceCollection *iface)
114 return CONTAINING_RECORD(iface, MMDevColImpl, IMMDeviceCollection_iface);
117 static inline IPropertyBagImpl *impl_from_IPropertyBag(IPropertyBag *iface)
119 return CONTAINING_RECORD(iface, IPropertyBagImpl, IPropertyBag_iface);
122 static HRESULT DeviceTopology_Create(IMMDevice *device, IDeviceTopology **ppv);
124 static inline IConnectorImpl *impl_from_IConnector(IConnector *iface)
126 return CONTAINING_RECORD(iface, IConnectorImpl, IConnector_iface);
129 static inline IDeviceTopologyImpl *impl_from_IDeviceTopology(IDeviceTopology *iface)
131 return CONTAINING_RECORD(iface, IDeviceTopologyImpl, IDeviceTopology_iface);
134 static const WCHAR propkey_formatW[] = L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X},%d";
136 static HRESULT MMDevPropStore_OpenPropKey(const GUID *guid, DWORD flow, HKEY *propkey)
138 WCHAR buffer[39];
139 LONG ret;
140 HKEY key;
141 StringFromGUID2(guid, buffer, 39);
142 if ((ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, buffer, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, &key)) != ERROR_SUCCESS)
144 WARN("Opening key %s failed with %lu\n", debugstr_w(buffer), ret);
145 return E_FAIL;
147 ret = RegOpenKeyExW(key, L"Properties", 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, propkey);
148 RegCloseKey(key);
149 if (ret != ERROR_SUCCESS)
151 WARN("Opening key Properties failed with %lu\n", ret);
152 return E_FAIL;
154 return S_OK;
157 static HRESULT MMDevice_GetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, PROPVARIANT *pv)
159 WCHAR buffer[80];
160 const GUID *id = &key->fmtid;
161 DWORD type, size;
162 HRESULT hr = S_OK;
163 HKEY regkey;
164 LONG ret;
166 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
167 if (FAILED(hr))
168 return hr;
169 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
170 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
171 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
172 ret = RegGetValueW(regkey, NULL, buffer, RRF_RT_ANY, &type, NULL, &size);
173 if (ret != ERROR_SUCCESS)
175 WARN("Reading %s returned %ld\n", debugstr_w(buffer), ret);
176 RegCloseKey(regkey);
177 pv->vt = VT_EMPTY;
178 return S_OK;
181 switch (type)
183 case REG_SZ:
185 pv->vt = VT_LPWSTR;
186 pv->pwszVal = CoTaskMemAlloc(size);
187 if (!pv->pwszVal)
188 hr = E_OUTOFMEMORY;
189 else
190 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_SZ, NULL, (BYTE*)pv->pwszVal, &size);
191 break;
193 case REG_DWORD:
195 pv->vt = VT_UI4;
196 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_DWORD, NULL, (BYTE*)&pv->ulVal, &size);
197 break;
199 case REG_BINARY:
201 pv->vt = VT_BLOB;
202 pv->blob.cbSize = size;
203 pv->blob.pBlobData = CoTaskMemAlloc(size);
204 if (!pv->blob.pBlobData)
205 hr = E_OUTOFMEMORY;
206 else
207 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_BINARY, NULL, (BYTE*)pv->blob.pBlobData, &size);
208 break;
210 default:
211 ERR("Unknown/unhandled type: %lu\n", type);
212 PropVariantClear(pv);
213 break;
215 RegCloseKey(regkey);
216 return hr;
219 static HRESULT MMDevice_SetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, REFPROPVARIANT pv)
221 WCHAR buffer[80];
222 const GUID *id = &key->fmtid;
223 HRESULT hr;
224 HKEY regkey;
225 LONG ret;
227 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
228 if (FAILED(hr))
229 return hr;
230 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
231 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
232 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
233 switch (pv->vt)
235 case VT_UI4:
237 ret = RegSetValueExW(regkey, buffer, 0, REG_DWORD, (const BYTE*)&pv->ulVal, sizeof(DWORD));
238 break;
240 case VT_BLOB:
242 ret = RegSetValueExW(regkey, buffer, 0, REG_BINARY, pv->blob.pBlobData, pv->blob.cbSize);
243 TRACE("Blob %p %lu\n", pv->blob.pBlobData, pv->blob.cbSize);
245 break;
247 case VT_LPWSTR:
249 ret = RegSetValueExW(regkey, buffer, 0, REG_SZ, (const BYTE*)pv->pwszVal, sizeof(WCHAR)*(1+lstrlenW(pv->pwszVal)));
250 break;
252 default:
253 ret = 0;
254 FIXME("Unhandled type %u\n", pv->vt);
255 hr = E_INVALIDARG;
256 break;
258 RegCloseKey(regkey);
259 TRACE("Writing %s returned %lu\n", debugstr_w(buffer), ret);
260 return hr;
263 static HRESULT set_driver_prop_value(GUID *id, const EDataFlow flow, const PROPERTYKEY *prop)
265 struct get_prop_value_params params;
266 PROPVARIANT pv;
267 char *dev_name;
268 unsigned int size = 0;
270 TRACE("%s, (%s,%lu)\n", wine_dbgstr_guid(id), wine_dbgstr_guid(&prop->fmtid), prop->pid);
272 if (!drvs.pget_device_name_from_guid(id, &dev_name, &params.flow))
273 return E_FAIL;
275 params.device = dev_name;
276 params.guid = id;
277 params.prop = prop;
278 params.value = &pv;
279 params.buffer = NULL;
280 params.buffer_size = &size;
282 while (1) {
283 __wine_unix_call(drvs.module_unixlib, get_prop_value, &params);
285 if (params.result != E_NOT_SUFFICIENT_BUFFER)
286 break;
288 CoTaskMemFree(params.buffer);
289 params.buffer = CoTaskMemAlloc(*params.buffer_size);
290 if (!params.buffer) {
291 free(dev_name);
292 return E_OUTOFMEMORY;
296 if (FAILED(params.result))
297 CoTaskMemFree(params.buffer);
299 free(dev_name);
301 if (SUCCEEDED(params.result)) {
302 MMDevice_SetPropValue(id, flow, prop, &pv);
303 PropVariantClear(&pv);
306 return params.result;
309 struct product_name_overrides
311 const WCHAR *id;
312 const WCHAR *product;
315 static const struct product_name_overrides product_name_overrides[] =
317 /* Sony controllers */
318 { .id = L"VID_054C&PID_0CE6", .product = L"Wireless Controller" },
319 { .id = L"VID_054C&PID_0DF2", .product = L"Wireless Controller" },
322 static const WCHAR *find_product_name_override(const WCHAR *device_id)
324 const WCHAR *match_id = wcschr( device_id, '\\' ) + 1;
325 DWORD i;
327 for (i = 0; i < ARRAY_SIZE(product_name_overrides); ++i)
328 if (!wcsnicmp( product_name_overrides[i].id, match_id, 17 ))
329 return product_name_overrides[i].product;
331 return NULL;
334 /* Creates or updates the state of a device
335 * If GUID is null, a random guid will be assigned
336 * and the device will be created
338 static MMDevice *MMDevice_Create(const WCHAR *name, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault)
340 HKEY key, root;
341 MMDevice *device, *cur = NULL;
342 WCHAR guidstr[39];
344 static const PROPERTYKEY deviceinterface_key = {
345 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
348 static const PROPERTYKEY devicepath_key = {
349 {0xb3f8fa53, 0x0004, 0x438e, {0x90, 0x03, 0x51, 0xa4, 0x6e, 0x13, 0x9b, 0xfc}}, 2
352 LIST_FOR_EACH_ENTRY(device, &device_list, MMDevice, entry)
354 if (device->flow == flow && IsEqualGUID(&device->devguid, id)){
355 cur = device;
356 break;
360 if(!cur){
361 /* No device found, allocate new one */
362 cur = calloc(1, sizeof(*cur));
363 if (!cur)
364 return NULL;
366 cur->IMMDevice_iface.lpVtbl = &MMDeviceVtbl;
367 cur->IMMEndpoint_iface.lpVtbl = &MMEndpointVtbl;
369 InitializeCriticalSectionEx(&cur->crst, 0, RTL_CRITICAL_SECTION_FLAG_FORCE_DEBUG_INFO);
370 cur->crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MMDevice.crst");
372 list_add_tail(&device_list, &cur->entry);
373 }else if(cur->ref > 0)
374 WARN("Modifying an MMDevice with postitive reference count!\n");
376 free(cur->drv_id);
377 cur->drv_id = wcsdup(name);
379 cur->flow = flow;
380 cur->state = state;
381 cur->devguid = *id;
383 StringFromGUID2(&cur->devguid, guidstr, ARRAY_SIZE(guidstr));
385 if (flow == eRender)
386 root = key_render;
387 else
388 root = key_capture;
390 if (RegCreateKeyExW(root, guidstr, 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &key, NULL) == ERROR_SUCCESS)
392 HKEY keyprop;
393 RegSetValueExW(key, L"DeviceState", 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
394 if (!RegCreateKeyExW(key, L"Properties", 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &keyprop, NULL))
396 PROPVARIANT pv;
398 pv.vt = VT_LPWSTR;
399 pv.pwszVal = cur->drv_id;
401 if (SUCCEEDED(set_driver_prop_value(id, flow, &devicepath_key))) {
402 PROPVARIANT pv2;
404 PropVariantInit(&pv2);
406 if (SUCCEEDED(MMDevice_GetPropValue(id, flow, &devicepath_key, &pv2)) && pv2.vt == VT_LPWSTR) {
407 const WCHAR *override;
408 if ((override = find_product_name_override(pv2.pwszVal)) != NULL)
409 pv.pwszVal = (WCHAR*) override;
412 PropVariantClear(&pv2);
415 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
416 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_DeviceInterface_FriendlyName, &pv);
417 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_DeviceDesc, &pv);
419 pv.pwszVal = guidstr;
420 MMDevice_SetPropValue(id, flow, &deviceinterface_key, &pv);
422 if (FAILED(set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_FormFactor)))
424 pv.vt = VT_UI4;
425 pv.ulVal = (flow == eCapture) ? Microphone : Speakers;
427 MMDevice_SetPropValue(id, flow, &PKEY_AudioEndpoint_FormFactor, &pv);
430 if (flow != eCapture)
432 PROPVARIANT pv2;
434 PropVariantInit(&pv2);
436 /* make read-write by not overwriting if already set */
437 if (FAILED(MMDevice_GetPropValue(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers, &pv2)) || pv2.vt != VT_UI4)
438 set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers);
440 PropVariantClear(&pv2);
443 RegCloseKey(keyprop);
445 RegCloseKey(key);
448 if (setdefault)
450 if (flow == eRender)
451 MMDevice_def_play = cur;
452 else
453 MMDevice_def_rec = cur;
455 return cur;
458 HRESULT load_devices_from_reg(void)
460 DWORD i = 0;
461 HKEY root, cur;
462 LONG ret;
463 DWORD curflow;
465 ret = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
466 L"Software\\Microsoft\\Windows\\CurrentVersion\\MMDevices\\Audio", 0, NULL, 0,
467 KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &root, NULL);
468 if (ret == ERROR_SUCCESS)
469 ret = RegCreateKeyExW(root, L"Capture", 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_capture, NULL);
470 if (ret == ERROR_SUCCESS)
471 ret = RegCreateKeyExW(root, L"Render", 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_render, NULL);
472 RegCloseKey(root);
473 cur = key_capture;
474 curflow = eCapture;
475 if (ret != ERROR_SUCCESS)
477 RegCloseKey(key_capture);
478 key_render = key_capture = NULL;
479 WARN("Couldn't create key: %lu\n", ret);
480 return E_FAIL;
483 do {
484 WCHAR guidvalue[39];
485 GUID guid;
486 DWORD len;
487 PROPVARIANT pv = { VT_EMPTY };
489 len = ARRAY_SIZE(guidvalue);
490 ret = RegEnumKeyExW(cur, i++, guidvalue, &len, NULL, NULL, NULL, NULL);
491 if (ret == ERROR_NO_MORE_ITEMS)
493 if (cur == key_capture)
495 cur = key_render;
496 curflow = eRender;
497 i = 0;
498 continue;
500 break;
502 if (ret != ERROR_SUCCESS)
503 continue;
504 if (SUCCEEDED(CLSIDFromString(guidvalue, &guid))
505 && SUCCEEDED(MMDevice_GetPropValue(&guid, curflow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv))
506 && pv.vt == VT_LPWSTR)
508 MMDevice_Create(pv.pwszVal, &guid, curflow,
509 DEVICE_STATE_NOTPRESENT, FALSE);
510 CoTaskMemFree(pv.pwszVal);
512 } while (1);
514 return S_OK;
517 static HRESULT set_format(MMDevice *dev)
519 HRESULT hr;
520 IAudioClient *client;
521 WAVEFORMATEX *fmt;
522 PROPVARIANT pv = { VT_EMPTY };
524 hr = AudioClient_Create(&dev->devguid, &dev->IMMDevice_iface, &client);
525 if(FAILED(hr))
526 return hr;
528 hr = IAudioClient_GetMixFormat(client, &fmt);
529 if(FAILED(hr)){
530 IAudioClient_Release(client);
531 return hr;
534 IAudioClient_Release(client);
536 pv.vt = VT_BLOB;
537 pv.blob.cbSize = sizeof(WAVEFORMATEX) + fmt->cbSize;
538 pv.blob.pBlobData = (BYTE*)fmt;
539 MMDevice_SetPropValue(&dev->devguid, dev->flow,
540 &PKEY_AudioEngine_DeviceFormat, &pv);
541 MMDevice_SetPropValue(&dev->devguid, dev->flow,
542 &PKEY_AudioEngine_OEMFormat, &pv);
543 CoTaskMemFree(fmt);
545 return S_OK;
548 HRESULT load_driver_devices(EDataFlow flow)
550 struct get_endpoint_ids_params params;
551 UINT i;
553 params.flow = flow;
554 params.size = 1024;
555 params.endpoints = NULL;
556 do {
557 free(params.endpoints);
558 params.endpoints = malloc(params.size);
559 __wine_unix_call(drvs.module_unixlib, get_endpoint_ids, &params);
560 } while (params.result == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER));
562 if (FAILED(params.result))
563 goto end;
565 for (i = 0; i < params.num; i++) {
566 GUID guid;
567 MMDevice *dev;
568 const WCHAR *name = (WCHAR *)((char *)params.endpoints + params.endpoints[i].name);
569 const char *dev_name = (char *)params.endpoints + params.endpoints[i].device;
571 drvs.pget_device_guid(flow, dev_name, &guid);
573 dev = MMDevice_Create(name, &guid, flow, DEVICE_STATE_ACTIVE, params.default_idx == i);
574 set_format(dev);
577 end:
578 free(params.endpoints);
580 return params.result;
583 static void MMDevice_Destroy(MMDevice *This)
585 TRACE("Freeing %s\n", debugstr_w(This->drv_id));
586 list_remove(&This->entry);
587 This->crst.DebugInfo->Spare[0] = 0;
588 DeleteCriticalSection(&This->crst);
589 free(This->drv_id);
590 free(This);
593 static inline MMDevice *impl_from_IMMDevice(IMMDevice *iface)
595 return CONTAINING_RECORD(iface, MMDevice, IMMDevice_iface);
598 static HRESULT WINAPI MMDevice_QueryInterface(IMMDevice *iface, REFIID riid, void **ppv)
600 MMDevice *This = impl_from_IMMDevice(iface);
601 TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ppv);
603 if (!ppv)
604 return E_POINTER;
605 *ppv = NULL;
606 if (IsEqualIID(riid, &IID_IUnknown)
607 || IsEqualIID(riid, &IID_IMMDevice))
608 *ppv = &This->IMMDevice_iface;
609 else if (IsEqualIID(riid, &IID_IMMEndpoint))
610 *ppv = &This->IMMEndpoint_iface;
611 if (*ppv)
613 IUnknown_AddRef((IUnknown*)*ppv);
614 return S_OK;
616 WARN("Unknown interface %s\n", debugstr_guid(riid));
617 return E_NOINTERFACE;
620 static ULONG WINAPI MMDevice_AddRef(IMMDevice *iface)
622 MMDevice *This = impl_from_IMMDevice(iface);
623 LONG ref;
625 ref = InterlockedIncrement(&This->ref);
626 TRACE("Refcount now %li\n", ref);
627 return ref;
630 static ULONG WINAPI MMDevice_Release(IMMDevice *iface)
632 MMDevice *This = impl_from_IMMDevice(iface);
633 LONG ref;
635 ref = InterlockedDecrement(&This->ref);
636 TRACE("Refcount now %li\n", ref);
637 return ref;
640 static HRESULT WINAPI MMDevice_Activate(IMMDevice *iface, REFIID riid, DWORD clsctx, PROPVARIANT *params, void **ppv)
642 HRESULT hr = E_NOINTERFACE;
643 MMDevice *This = impl_from_IMMDevice(iface);
645 TRACE("(%p)->(%s, %lx, %p, %p)\n", iface, debugstr_guid(riid), clsctx, params, ppv);
647 if (!ppv)
648 return E_POINTER;
650 if (IsEqualIID(riid, &IID_IAudioClient) ||
651 IsEqualIID(riid, &IID_IAudioClient2) ||
652 IsEqualIID(riid, &IID_IAudioClient3)){
653 hr = AudioClient_Create(&This->devguid, iface, (IAudioClient**)ppv);
654 }else if (IsEqualIID(riid, &IID_IAudioEndpointVolume) ||
655 IsEqualIID(riid, &IID_IAudioEndpointVolumeEx))
656 hr = AudioEndpointVolume_Create(This, (IAudioEndpointVolumeEx**)ppv);
657 else if (IsEqualIID(riid, &IID_IAudioSessionManager)
658 || IsEqualIID(riid, &IID_IAudioSessionManager2))
660 hr = AudioSessionManager_Create(iface, (IAudioSessionManager2**)ppv);
662 else if (IsEqualIID(riid, &IID_IBaseFilter))
664 if (This->flow == eRender)
665 hr = CoCreateInstance(&CLSID_DSoundRender, NULL, clsctx, riid, ppv);
666 else
667 ERR("Not supported for recording?\n");
668 if (SUCCEEDED(hr))
670 IPersistPropertyBag *ppb;
671 hr = IUnknown_QueryInterface((IUnknown*)*ppv, &IID_IPersistPropertyBag, (void **)&ppb);
672 if (SUCCEEDED(hr))
674 /* ::Load cannot assume the interface stays alive after the function returns,
675 * so just create the interface on the stack, saves a lot of complicated code */
676 IPropertyBagImpl bag = { { &PB_Vtbl } };
677 bag.devguid = This->devguid;
678 hr = IPersistPropertyBag_Load(ppb, &bag.IPropertyBag_iface, NULL);
679 IPersistPropertyBag_Release(ppb);
680 if (FAILED(hr))
681 IBaseFilter_Release((IBaseFilter*)*ppv);
683 else
685 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
686 hr = S_OK;
690 else if (IsEqualIID(riid, &IID_IDeviceTopology))
692 hr = DeviceTopology_Create(iface, (IDeviceTopology**)ppv);
694 else if (IsEqualIID(riid, &IID_IDirectSound)
695 || IsEqualIID(riid, &IID_IDirectSound8))
697 if (This->flow == eRender)
698 hr = CoCreateInstance(&CLSID_DirectSound8, NULL, clsctx, riid, ppv);
699 if (SUCCEEDED(hr))
701 hr = IDirectSound_Initialize((IDirectSound*)*ppv, &This->devguid);
702 if (FAILED(hr))
703 IDirectSound_Release((IDirectSound*)*ppv);
706 else if (IsEqualIID(riid, &IID_IDirectSoundCapture))
708 if (This->flow == eCapture)
709 hr = CoCreateInstance(&CLSID_DirectSoundCapture8, NULL, clsctx, riid, ppv);
710 if (SUCCEEDED(hr))
712 hr = IDirectSoundCapture_Initialize((IDirectSoundCapture*)*ppv, &This->devguid);
713 if (FAILED(hr))
714 IDirectSoundCapture_Release((IDirectSoundCapture*)*ppv);
717 else if (IsEqualIID(riid, &IID_ISpatialAudioClient))
719 hr = SpatialAudioClient_Create(iface, (ISpatialAudioClient**)ppv);
721 else
722 ERR("Invalid/unknown iid %s\n", debugstr_guid(riid));
724 if (FAILED(hr))
725 *ppv = NULL;
727 TRACE("Returning %08lx\n", hr);
728 return hr;
731 static HRESULT WINAPI MMDevice_OpenPropertyStore(IMMDevice *iface, DWORD access, IPropertyStore **ppv)
733 MMDevice *This = impl_from_IMMDevice(iface);
734 TRACE("(%p)->(%lx,%p)\n", This, access, ppv);
736 if (!ppv)
737 return E_POINTER;
738 return MMDevPropStore_Create(This, access, ppv);
741 static HRESULT WINAPI MMDevice_GetId(IMMDevice *iface, WCHAR **itemid)
743 MMDevice *This = impl_from_IMMDevice(iface);
744 WCHAR *str;
745 GUID *id = &This->devguid;
747 TRACE("(%p)->(%p)\n", This, itemid);
748 if (!itemid)
749 return E_POINTER;
750 *itemid = str = CoTaskMemAlloc(56 * sizeof(WCHAR));
751 if (!str)
752 return E_OUTOFMEMORY;
753 wsprintfW(str, L"{0.0.%u.00000000}.{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
754 This->flow, id->Data1, id->Data2, id->Data3,
755 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
756 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7]);
757 TRACE("returning %s\n", wine_dbgstr_w(str));
758 return S_OK;
761 static HRESULT WINAPI MMDevice_GetState(IMMDevice *iface, DWORD *state)
763 MMDevice *This = impl_from_IMMDevice(iface);
764 TRACE("(%p)->(%p)\n", iface, state);
766 if (!state)
767 return E_POINTER;
768 *state = This->state;
769 return S_OK;
772 static const IMMDeviceVtbl MMDeviceVtbl =
774 MMDevice_QueryInterface,
775 MMDevice_AddRef,
776 MMDevice_Release,
777 MMDevice_Activate,
778 MMDevice_OpenPropertyStore,
779 MMDevice_GetId,
780 MMDevice_GetState
783 static inline MMDevice *impl_from_IMMEndpoint(IMMEndpoint *iface)
785 return CONTAINING_RECORD(iface, MMDevice, IMMEndpoint_iface);
788 static HRESULT WINAPI MMEndpoint_QueryInterface(IMMEndpoint *iface, REFIID riid, void **ppv)
790 MMDevice *This = impl_from_IMMEndpoint(iface);
791 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
792 return IMMDevice_QueryInterface(&This->IMMDevice_iface, riid, ppv);
795 static ULONG WINAPI MMEndpoint_AddRef(IMMEndpoint *iface)
797 MMDevice *This = impl_from_IMMEndpoint(iface);
798 TRACE("(%p)\n", This);
799 return IMMDevice_AddRef(&This->IMMDevice_iface);
802 static ULONG WINAPI MMEndpoint_Release(IMMEndpoint *iface)
804 MMDevice *This = impl_from_IMMEndpoint(iface);
805 TRACE("(%p)\n", This);
806 return IMMDevice_Release(&This->IMMDevice_iface);
809 static HRESULT WINAPI MMEndpoint_GetDataFlow(IMMEndpoint *iface, EDataFlow *flow)
811 MMDevice *This = impl_from_IMMEndpoint(iface);
812 TRACE("(%p)->(%p)\n", This, flow);
813 if (!flow)
814 return E_POINTER;
815 *flow = This->flow;
816 return S_OK;
819 static const IMMEndpointVtbl MMEndpointVtbl =
821 MMEndpoint_QueryInterface,
822 MMEndpoint_AddRef,
823 MMEndpoint_Release,
824 MMEndpoint_GetDataFlow
827 static HRESULT MMDevCol_Create(IMMDeviceCollection **ppv, EDataFlow flow, DWORD state)
829 MMDevColImpl *This;
830 MMDevice *cur;
831 UINT i = 0;
833 This = malloc(sizeof(*This));
834 *ppv = NULL;
835 if (!This)
836 return E_OUTOFMEMORY;
837 This->IMMDeviceCollection_iface.lpVtbl = &MMDevColVtbl;
838 This->ref = 1;
839 This->devices = NULL;
840 This->devices_count = 0;
841 *ppv = &This->IMMDeviceCollection_iface;
843 LIST_FOR_EACH_ENTRY(cur, &device_list, MMDevice, entry)
845 if ((cur->flow == flow || flow == eAll) && (cur->state & state))
846 This->devices_count++;
849 if (This->devices_count)
851 This->devices = malloc(This->devices_count * sizeof(IMMDevice *));
852 if (!This->devices_count)
853 return E_OUTOFMEMORY;
855 LIST_FOR_EACH_ENTRY(cur, &device_list, MMDevice, entry)
857 if ((cur->flow == flow || flow == eAll) && (cur->state & state))
859 This->devices[i] = &cur->IMMDevice_iface;
860 IMMDevice_AddRef(This->devices[i]);
861 i++;
866 return S_OK;
869 static void MMDevCol_Destroy(MMDevColImpl *This)
871 UINT i;
872 for (i = 0; i < This->devices_count; i++)
873 IMMDevice_Release(This->devices[i]);
875 free(This->devices);
876 free(This);
879 static HRESULT WINAPI MMDevCol_QueryInterface(IMMDeviceCollection *iface, REFIID riid, void **ppv)
881 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
882 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
884 if (!ppv)
885 return E_POINTER;
886 if (IsEqualIID(riid, &IID_IUnknown)
887 || IsEqualIID(riid, &IID_IMMDeviceCollection))
888 *ppv = &This->IMMDeviceCollection_iface;
889 else
890 *ppv = NULL;
891 if (!*ppv)
892 return E_NOINTERFACE;
893 IUnknown_AddRef((IUnknown*)*ppv);
894 return S_OK;
897 static ULONG WINAPI MMDevCol_AddRef(IMMDeviceCollection *iface)
899 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
900 LONG ref = InterlockedIncrement(&This->ref);
901 TRACE("Refcount now %li\n", ref);
902 return ref;
905 static ULONG WINAPI MMDevCol_Release(IMMDeviceCollection *iface)
907 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
908 LONG ref = InterlockedDecrement(&This->ref);
909 TRACE("Refcount now %li\n", ref);
910 if (!ref)
911 MMDevCol_Destroy(This);
912 return ref;
915 static HRESULT WINAPI MMDevCol_GetCount(IMMDeviceCollection *iface, UINT *numdevs)
917 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
919 TRACE("(%p)->(%p)\n", This, numdevs);
920 if (!numdevs)
921 return E_POINTER;
923 *numdevs = This->devices_count;
924 return S_OK;
927 static HRESULT WINAPI MMDevCol_Item(IMMDeviceCollection *iface, UINT n, IMMDevice **dev)
929 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
931 TRACE("(%p)->(%u, %p)\n", This, n, dev);
932 if (!dev)
933 return E_POINTER;
935 if (n >= This->devices_count)
937 WARN("Could not obtain item %u\n", n);
938 *dev = NULL;
939 return E_INVALIDARG;
942 *dev = This->devices[n];
943 IMMDevice_AddRef(*dev);
944 return S_OK;
947 static const IMMDeviceCollectionVtbl MMDevColVtbl =
949 MMDevCol_QueryInterface,
950 MMDevCol_AddRef,
951 MMDevCol_Release,
952 MMDevCol_GetCount,
953 MMDevCol_Item
956 HRESULT MMDevEnum_Create(REFIID riid, void **ppv)
958 return IMMDeviceEnumerator_QueryInterface(&enumerator.IMMDeviceEnumerator_iface, riid, ppv);
961 void MMDevEnum_Free(void)
963 MMDevice *device, *next;
964 LIST_FOR_EACH_ENTRY_SAFE(device, next, &device_list, MMDevice, entry)
965 MMDevice_Destroy(device);
966 RegCloseKey(key_render);
967 RegCloseKey(key_capture);
968 key_render = key_capture = NULL;
971 static HRESULT WINAPI MMDevEnum_QueryInterface(IMMDeviceEnumerator *iface, REFIID riid, void **ppv)
973 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
974 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
976 if (!ppv)
977 return E_POINTER;
978 if (IsEqualIID(riid, &IID_IUnknown)
979 || IsEqualIID(riid, &IID_IMMDeviceEnumerator))
980 *ppv = &This->IMMDeviceEnumerator_iface;
981 else
982 *ppv = NULL;
983 if (!*ppv)
984 return E_NOINTERFACE;
985 IUnknown_AddRef((IUnknown*)*ppv);
986 return S_OK;
989 static ULONG WINAPI MMDevEnum_AddRef(IMMDeviceEnumerator *iface)
991 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
992 LONG ref = InterlockedIncrement(&This->ref);
993 TRACE("Refcount now %li\n", ref);
994 return ref;
997 static ULONG WINAPI MMDevEnum_Release(IMMDeviceEnumerator *iface)
999 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1000 LONG ref = InterlockedDecrement(&This->ref);
1001 TRACE("Refcount now %li\n", ref);
1002 return ref;
1005 static HRESULT WINAPI MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator *iface, EDataFlow flow, DWORD mask, IMMDeviceCollection **devices)
1007 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1008 TRACE("(%p)->(%u,%lu,%p)\n", This, flow, mask, devices);
1009 if (!devices)
1010 return E_POINTER;
1011 *devices = NULL;
1012 if (flow >= EDataFlow_enum_count)
1013 return E_INVALIDARG;
1014 if (mask & ~DEVICE_STATEMASK_ALL)
1015 return E_INVALIDARG;
1016 return MMDevCol_Create(devices, flow, mask);
1019 static HRESULT WINAPI MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator *iface, EDataFlow flow, ERole role, IMMDevice **device)
1021 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1022 WCHAR reg_key[256];
1023 HKEY key;
1024 HRESULT hr;
1026 TRACE("(%p)->(%u,%u,%p)\n", This, flow, role, device);
1028 if (!device)
1029 return E_POINTER;
1031 if((flow != eRender && flow != eCapture) ||
1032 (role != eConsole && role != eMultimedia && role != eCommunications)){
1033 WARN("Unknown flow (%u) or role (%u)\n", flow, role);
1034 return E_INVALIDARG;
1037 *device = NULL;
1039 if(!drvs.module_name[0])
1040 return E_NOTFOUND;
1042 lstrcpyW(reg_key, drv_keyW);
1043 lstrcatW(reg_key, L"\\");
1044 lstrcatW(reg_key, drvs.module_name);
1046 if(RegOpenKeyW(HKEY_CURRENT_USER, reg_key, &key) == ERROR_SUCCESS){
1047 const WCHAR *reg_x_name, *reg_vx_name;
1048 WCHAR def_id[256];
1049 DWORD size = sizeof(def_id), state;
1051 if(flow == eRender){
1052 reg_x_name = L"DefaultOutput";
1053 reg_vx_name = L"DefaultVoiceOutput";
1054 }else{
1055 reg_x_name = L"DefaultInput";
1056 reg_vx_name = L"DefaultVoiceInput";
1059 if(role == eCommunications &&
1060 RegQueryValueExW(key, reg_vx_name, 0, NULL,
1061 (BYTE*)def_id, &size) == ERROR_SUCCESS){
1062 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
1063 if(SUCCEEDED(hr)){
1064 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
1065 state == DEVICE_STATE_ACTIVE){
1066 RegCloseKey(key);
1067 return S_OK;
1071 TRACE("Unable to find voice device %s\n", wine_dbgstr_w(def_id));
1074 if(RegQueryValueExW(key, reg_x_name, 0, NULL,
1075 (BYTE*)def_id, &size) == ERROR_SUCCESS){
1076 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
1077 if(SUCCEEDED(hr)){
1078 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
1079 state == DEVICE_STATE_ACTIVE){
1080 RegCloseKey(key);
1081 return S_OK;
1085 TRACE("Unable to find device %s\n", wine_dbgstr_w(def_id));
1088 RegCloseKey(key);
1091 if (flow == eRender)
1092 *device = &MMDevice_def_play->IMMDevice_iface;
1093 else
1094 *device = &MMDevice_def_rec->IMMDevice_iface;
1096 if (!*device)
1097 return E_NOTFOUND;
1098 IMMDevice_AddRef(*device);
1099 return S_OK;
1102 static HRESULT WINAPI MMDevEnum_GetDevice(IMMDeviceEnumerator *iface, const WCHAR *name, IMMDevice **device)
1104 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1105 MMDevice *impl;
1106 IMMDevice *dev = NULL;
1108 TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), device);
1110 if(!name || !device)
1111 return E_POINTER;
1113 if(!lstrcmpW(name, L"Wine info device")){
1114 *device = &info_device;
1115 return S_OK;
1118 LIST_FOR_EACH_ENTRY(impl, &device_list, MMDevice, entry)
1120 HRESULT hr;
1121 WCHAR *str;
1122 dev = &impl->IMMDevice_iface;
1123 hr = IMMDevice_GetId(dev, &str);
1124 if (FAILED(hr))
1126 WARN("GetId failed: %08lx\n", hr);
1127 continue;
1130 if (str && !lstrcmpW(str, name))
1132 CoTaskMemFree(str);
1133 IMMDevice_AddRef(dev);
1134 *device = dev;
1135 return S_OK;
1137 CoTaskMemFree(str);
1139 TRACE("Could not find device %s\n", debugstr_w(name));
1140 return E_INVALIDARG;
1143 struct NotificationClientWrapper {
1144 IMMNotificationClient *client;
1145 struct list entry;
1148 static struct list g_notif_clients = LIST_INIT(g_notif_clients);
1149 static HANDLE g_notif_thread;
1151 static CRITICAL_SECTION g_notif_lock;
1152 static CRITICAL_SECTION_DEBUG g_notif_lock_debug =
1154 0, 0, &g_notif_lock,
1155 { &g_notif_lock_debug.ProcessLocksList, &g_notif_lock_debug.ProcessLocksList },
1156 0, 0, { (DWORD_PTR)(__FILE__ ": g_notif_lock") }
1158 static CRITICAL_SECTION g_notif_lock = { &g_notif_lock_debug, -1, 0, 0, 0, 0 };
1160 static void notify_clients(EDataFlow flow, ERole role, const WCHAR *id)
1162 struct NotificationClientWrapper *wrapper;
1163 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients,
1164 struct NotificationClientWrapper, entry)
1165 IMMNotificationClient_OnDefaultDeviceChanged(wrapper->client, flow,
1166 role, id);
1168 /* Windows 7 treats changes to eConsole as changes to eMultimedia */
1169 if(role == eConsole)
1170 notify_clients(flow, eMultimedia, id);
1173 static BOOL notify_if_changed(EDataFlow flow, ERole role, HKEY key,
1174 const WCHAR *val_name, WCHAR *old_val, IMMDevice *def_dev)
1176 WCHAR new_val[64], *id;
1177 DWORD size;
1178 HRESULT hr;
1180 size = sizeof(new_val);
1181 if(RegQueryValueExW(key, val_name, 0, NULL,
1182 (BYTE*)new_val, &size) != ERROR_SUCCESS){
1183 if(old_val[0] != 0){
1184 /* set by user -> system default */
1185 if(def_dev){
1186 hr = IMMDevice_GetId(def_dev, &id);
1187 if(FAILED(hr)){
1188 ERR("GetId failed: %08lx\n", hr);
1189 return FALSE;
1191 }else
1192 id = NULL;
1194 notify_clients(flow, role, id);
1195 old_val[0] = 0;
1196 CoTaskMemFree(id);
1198 return TRUE;
1201 /* system default -> system default, noop */
1202 return FALSE;
1205 if(!lstrcmpW(old_val, new_val)){
1206 /* set by user -> same value */
1207 return FALSE;
1210 if(new_val[0] != 0){
1211 /* set by user -> different value */
1212 notify_clients(flow, role, new_val);
1213 memcpy(old_val, new_val, sizeof(new_val));
1214 return TRUE;
1217 /* set by user -> system default */
1218 if(def_dev){
1219 hr = IMMDevice_GetId(def_dev, &id);
1220 if(FAILED(hr)){
1221 ERR("GetId failed: %08lx\n", hr);
1222 return FALSE;
1224 }else
1225 id = NULL;
1227 notify_clients(flow, role, id);
1228 old_val[0] = 0;
1229 CoTaskMemFree(id);
1231 return TRUE;
1234 static DWORD WINAPI notif_thread_proc(void *user)
1236 HKEY key;
1237 WCHAR reg_key[256];
1238 WCHAR out_name[64], vout_name[64], in_name[64], vin_name[64];
1239 DWORD size;
1241 SetThreadDescription(GetCurrentThread(), L"wine_mmdevapi_notification");
1243 lstrcpyW(reg_key, drv_keyW);
1244 lstrcatW(reg_key, L"\\");
1245 lstrcatW(reg_key, drvs.module_name);
1247 if(RegCreateKeyExW(HKEY_CURRENT_USER, reg_key, 0, NULL, 0,
1248 MAXIMUM_ALLOWED, NULL, &key, NULL) != ERROR_SUCCESS){
1249 ERR("RegCreateKeyEx failed: %lu\n", GetLastError());
1250 return 1;
1253 size = sizeof(out_name);
1254 if(RegQueryValueExW(key, L"DefaultOutput", 0, NULL, (BYTE*)out_name, &size) != ERROR_SUCCESS)
1255 out_name[0] = 0;
1257 size = sizeof(vout_name);
1258 if(RegQueryValueExW(key, L"DefaultVoiceOutput", 0, NULL, (BYTE*)vout_name, &size) != ERROR_SUCCESS)
1259 vout_name[0] = 0;
1261 size = sizeof(in_name);
1262 if(RegQueryValueExW(key, L"DefaultInput", 0, NULL, (BYTE*)in_name, &size) != ERROR_SUCCESS)
1263 in_name[0] = 0;
1265 size = sizeof(vin_name);
1266 if(RegQueryValueExW(key, L"DefaultVoiceInput", 0, NULL, (BYTE*)vin_name, &size) != ERROR_SUCCESS)
1267 vin_name[0] = 0;
1269 while(1){
1270 if(RegNotifyChangeKeyValue(key, FALSE, REG_NOTIFY_CHANGE_LAST_SET,
1271 NULL, FALSE) != ERROR_SUCCESS){
1272 ERR("RegNotifyChangeKeyValue failed: %lu\n", GetLastError());
1273 RegCloseKey(key);
1274 g_notif_thread = NULL;
1275 return 1;
1278 EnterCriticalSection(&g_notif_lock);
1280 notify_if_changed(eRender, eConsole, key, L"DefaultOutput",
1281 out_name, &MMDevice_def_play->IMMDevice_iface);
1282 notify_if_changed(eRender, eCommunications, key, L"DefaultVoiceOutput",
1283 vout_name, &MMDevice_def_play->IMMDevice_iface);
1284 notify_if_changed(eCapture, eConsole, key, L"DefaultInput",
1285 in_name, &MMDevice_def_rec->IMMDevice_iface);
1286 notify_if_changed(eCapture, eCommunications, key, L"DefaultVoiceInput",
1287 vin_name, &MMDevice_def_rec->IMMDevice_iface);
1289 LeaveCriticalSection(&g_notif_lock);
1292 RegCloseKey(key);
1294 g_notif_thread = NULL;
1296 return 0;
1299 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1301 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1302 struct NotificationClientWrapper *wrapper;
1304 TRACE("(%p)->(%p)\n", This, client);
1306 if(!client)
1307 return E_POINTER;
1309 wrapper = malloc(sizeof(*wrapper));
1310 if(!wrapper)
1311 return E_OUTOFMEMORY;
1313 wrapper->client = client;
1315 EnterCriticalSection(&g_notif_lock);
1317 list_add_tail(&g_notif_clients, &wrapper->entry);
1319 if(!g_notif_thread){
1320 g_notif_thread = CreateThread(NULL, 0, notif_thread_proc, NULL, 0, NULL);
1321 if(!g_notif_thread)
1322 ERR("CreateThread failed: %lu\n", GetLastError());
1325 LeaveCriticalSection(&g_notif_lock);
1327 return S_OK;
1330 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1332 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1333 struct NotificationClientWrapper *wrapper;
1335 TRACE("(%p)->(%p)\n", This, client);
1337 if(!client)
1338 return E_POINTER;
1340 EnterCriticalSection(&g_notif_lock);
1342 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients, struct NotificationClientWrapper, entry){
1343 if(wrapper->client == client){
1344 list_remove(&wrapper->entry);
1345 free(wrapper);
1346 LeaveCriticalSection(&g_notif_lock);
1347 return S_OK;
1351 LeaveCriticalSection(&g_notif_lock);
1353 return E_NOTFOUND;
1356 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl =
1358 MMDevEnum_QueryInterface,
1359 MMDevEnum_AddRef,
1360 MMDevEnum_Release,
1361 MMDevEnum_EnumAudioEndpoints,
1362 MMDevEnum_GetDefaultAudioEndpoint,
1363 MMDevEnum_GetDevice,
1364 MMDevEnum_RegisterEndpointNotificationCallback,
1365 MMDevEnum_UnregisterEndpointNotificationCallback
1368 static MMDevEnumImpl enumerator =
1370 {&MMDevEnumVtbl},
1374 static HRESULT MMDevPropStore_Create(MMDevice *parent, DWORD access, IPropertyStore **ppv)
1376 MMDevPropStore *This;
1377 if (access != STGM_READ
1378 && access != STGM_WRITE
1379 && access != STGM_READWRITE)
1381 WARN("Invalid access %08lx\n", access);
1382 return E_INVALIDARG;
1384 This = malloc(sizeof(*This));
1385 *ppv = &This->IPropertyStore_iface;
1386 if (!This)
1387 return E_OUTOFMEMORY;
1388 This->IPropertyStore_iface.lpVtbl = &MMDevPropVtbl;
1389 This->ref = 1;
1390 This->parent = parent;
1391 This->access = access;
1392 return S_OK;
1395 static void MMDevPropStore_Destroy(MMDevPropStore *This)
1397 free(This);
1400 static HRESULT WINAPI MMDevPropStore_QueryInterface(IPropertyStore *iface, REFIID riid, void **ppv)
1402 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1403 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1405 if (!ppv)
1406 return E_POINTER;
1407 if (IsEqualIID(riid, &IID_IUnknown)
1408 || IsEqualIID(riid, &IID_IPropertyStore))
1409 *ppv = &This->IPropertyStore_iface;
1410 else
1411 *ppv = NULL;
1412 if (!*ppv)
1413 return E_NOINTERFACE;
1414 IUnknown_AddRef((IUnknown*)*ppv);
1415 return S_OK;
1418 static ULONG WINAPI MMDevPropStore_AddRef(IPropertyStore *iface)
1420 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1421 LONG ref = InterlockedIncrement(&This->ref);
1422 TRACE("Refcount now %li\n", ref);
1423 return ref;
1426 static ULONG WINAPI MMDevPropStore_Release(IPropertyStore *iface)
1428 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1429 LONG ref = InterlockedDecrement(&This->ref);
1430 TRACE("Refcount now %li\n", ref);
1431 if (!ref)
1432 MMDevPropStore_Destroy(This);
1433 return ref;
1436 static HRESULT WINAPI MMDevPropStore_GetCount(IPropertyStore *iface, DWORD *nprops)
1438 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1439 WCHAR buffer[50];
1440 DWORD i = 0;
1441 HKEY propkey;
1442 HRESULT hr;
1444 TRACE("(%p)->(%p)\n", iface, nprops);
1445 if (!nprops)
1446 return E_POINTER;
1447 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1448 if (FAILED(hr))
1449 return hr;
1450 *nprops = 0;
1451 do {
1452 DWORD len = ARRAY_SIZE(buffer);
1453 if (RegEnumValueW(propkey, i, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
1454 break;
1455 i++;
1456 } while (1);
1457 RegCloseKey(propkey);
1458 TRACE("Returning %li\n", i);
1459 *nprops = i;
1460 return S_OK;
1463 static HRESULT WINAPI MMDevPropStore_GetAt(IPropertyStore *iface, DWORD prop, PROPERTYKEY *key)
1465 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1466 WCHAR buffer[50];
1467 DWORD len = ARRAY_SIZE(buffer);
1468 HRESULT hr;
1469 HKEY propkey;
1471 TRACE("(%p)->(%lu,%p)\n", iface, prop, key);
1472 if (!key)
1473 return E_POINTER;
1475 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1476 if (FAILED(hr))
1477 return hr;
1479 if (RegEnumValueW(propkey, prop, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS
1480 || len <= 39)
1482 WARN("GetAt %lu failed\n", prop);
1483 return E_INVALIDARG;
1485 RegCloseKey(propkey);
1486 buffer[38] = 0;
1487 CLSIDFromString(buffer, &key->fmtid);
1488 key->pid = wcstol(&buffer[39], NULL, 10);
1489 return S_OK;
1492 static HRESULT WINAPI MMDevPropStore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *pv)
1494 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1495 HRESULT hres;
1496 TRACE("(%p)->(\"%s,%lu\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1498 if (!key || !pv)
1499 return E_POINTER;
1500 if (This->access != STGM_READ
1501 && This->access != STGM_READWRITE)
1502 return STG_E_ACCESSDENIED;
1504 /* Special case */
1505 if (IsEqualPropertyKey(*key, PKEY_AudioEndpoint_GUID))
1507 pv->vt = VT_LPWSTR;
1508 pv->pwszVal = CoTaskMemAlloc(39 * sizeof(WCHAR));
1509 if (!pv->pwszVal)
1510 return E_OUTOFMEMORY;
1511 StringFromGUID2(&This->parent->devguid, pv->pwszVal, 39);
1512 return S_OK;
1515 hres = MMDevice_GetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1516 if (FAILED(hres))
1517 return hres;
1519 if (WARN_ON(mmdevapi))
1521 if ((IsEqualPropertyKey(*key, DEVPKEY_Device_FriendlyName) ||
1522 IsEqualPropertyKey(*key, DEVPKEY_DeviceInterface_FriendlyName) ||
1523 IsEqualPropertyKey(*key, DEVPKEY_Device_DeviceDesc)) &&
1524 pv->vt == VT_LPWSTR && wcslen(pv->pwszVal) > 62)
1525 WARN("Returned name exceeds length limit of some broken apps/libs, might crash: %s\n", debugstr_w(pv->pwszVal));
1527 return hres;
1530 static HRESULT WINAPI MMDevPropStore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT pv)
1532 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1533 TRACE("(%p)->(\"%s,%lu\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1535 if (!key || !pv)
1536 return E_POINTER;
1538 if (This->access != STGM_WRITE
1539 && This->access != STGM_READWRITE)
1540 return STG_E_ACCESSDENIED;
1541 return MMDevice_SetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1544 static HRESULT WINAPI MMDevPropStore_Commit(IPropertyStore *iface)
1546 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1547 TRACE("(%p)\n", iface);
1549 if (This->access != STGM_WRITE
1550 && This->access != STGM_READWRITE)
1551 return STG_E_ACCESSDENIED;
1553 /* Does nothing - for mmdevapi, the propstore values are written on SetValue,
1554 * not on Commit. */
1556 return S_OK;
1559 static const IPropertyStoreVtbl MMDevPropVtbl =
1561 MMDevPropStore_QueryInterface,
1562 MMDevPropStore_AddRef,
1563 MMDevPropStore_Release,
1564 MMDevPropStore_GetCount,
1565 MMDevPropStore_GetAt,
1566 MMDevPropStore_GetValue,
1567 MMDevPropStore_SetValue,
1568 MMDevPropStore_Commit
1572 /* Property bag for IBaseFilter activation */
1573 static HRESULT WINAPI PB_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
1575 ERR("Should not be called\n");
1576 *ppv = NULL;
1577 return E_NOINTERFACE;
1580 static ULONG WINAPI PB_AddRef(IPropertyBag *iface)
1582 ERR("Should not be called\n");
1583 return 2;
1586 static ULONG WINAPI PB_Release(IPropertyBag *iface)
1588 ERR("Should not be called\n");
1589 return 1;
1592 static HRESULT WINAPI PB_Read(IPropertyBag *iface, LPCOLESTR name, VARIANT *var, IErrorLog *log)
1594 IPropertyBagImpl *This = impl_from_IPropertyBag(iface);
1595 TRACE("Trying to read %s, type %u\n", debugstr_w(name), var->vt);
1596 if (!lstrcmpW(name, L"DSGuid"))
1598 WCHAR guidstr[39];
1599 StringFromGUID2(&This->devguid, guidstr,ARRAY_SIZE(guidstr));
1600 var->vt = VT_BSTR;
1601 var->bstrVal = SysAllocString(guidstr);
1602 return S_OK;
1604 ERR("Unknown property '%s' queried\n", debugstr_w(name));
1605 return E_FAIL;
1608 static HRESULT WINAPI PB_Write(IPropertyBag *iface, LPCOLESTR name, VARIANT *var)
1610 ERR("Should not be called\n");
1611 return E_FAIL;
1614 static const IPropertyBagVtbl PB_Vtbl =
1616 PB_QueryInterface,
1617 PB_AddRef,
1618 PB_Release,
1619 PB_Read,
1620 PB_Write
1623 static ULONG WINAPI info_device_ps_AddRef(IPropertyStore *iface)
1625 return 2;
1628 static ULONG WINAPI info_device_ps_Release(IPropertyStore *iface)
1630 return 1;
1633 static HRESULT WINAPI info_device_ps_GetValue(IPropertyStore *iface,
1634 REFPROPERTYKEY key, PROPVARIANT *pv)
1636 TRACE("(static)->(\"%s,%lu\", %p)\n", debugstr_guid(&key->fmtid), key ? key->pid : 0, pv);
1638 if (!key || !pv)
1639 return E_POINTER;
1641 if (IsEqualPropertyKey(*key, DEVPKEY_Device_Driver))
1643 INT size = (lstrlenW(drvs.module_name) + 1) * sizeof(WCHAR);
1644 pv->vt = VT_LPWSTR;
1645 pv->pwszVal = CoTaskMemAlloc(size);
1646 if (!pv->pwszVal)
1647 return E_OUTOFMEMORY;
1648 memcpy(pv->pwszVal, drvs.module_name, size);
1649 return S_OK;
1652 return E_INVALIDARG;
1655 static const IPropertyStoreVtbl info_device_ps_Vtbl =
1657 NULL,
1658 info_device_ps_AddRef,
1659 info_device_ps_Release,
1660 NULL,
1661 NULL,
1662 info_device_ps_GetValue,
1663 NULL,
1664 NULL
1667 static IPropertyStore info_device_ps = {
1668 &info_device_ps_Vtbl
1671 static ULONG WINAPI info_device_AddRef(IMMDevice *iface)
1673 return 2;
1676 static ULONG WINAPI info_device_Release(IMMDevice *iface)
1678 return 1;
1681 static HRESULT WINAPI info_device_OpenPropertyStore(IMMDevice *iface,
1682 DWORD access, IPropertyStore **ppv)
1684 TRACE("(static)->(%lx, %p)\n", access, ppv);
1685 *ppv = &info_device_ps;
1686 return S_OK;
1689 static const IMMDeviceVtbl info_device_Vtbl =
1691 NULL,
1692 info_device_AddRef,
1693 info_device_Release,
1694 NULL,
1695 info_device_OpenPropertyStore,
1696 NULL,
1697 NULL
1700 static IMMDevice info_device = {
1701 &info_device_Vtbl
1704 static HRESULT WINAPI Connector_QueryInterface(IConnector *iface, REFIID riid, void **ppv)
1706 IConnectorImpl *This = impl_from_IConnector(iface);
1707 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1709 if (!ppv)
1710 return E_POINTER;
1712 if (IsEqualIID(riid, &IID_IUnknown) ||
1713 IsEqualIID(riid, &IID_IConnector))
1714 *ppv = &This->IConnector_iface;
1715 else {
1716 *ppv = NULL;
1717 return E_NOINTERFACE;
1720 IUnknown_AddRef((IUnknown *)*ppv);
1722 return S_OK;
1725 static ULONG WINAPI Connector_AddRef(IConnector *iface)
1727 IConnectorImpl *This = impl_from_IConnector(iface);
1728 ULONG ref = InterlockedIncrement(&This->ref);
1729 TRACE("(%p) new ref %lu\n", This, ref);
1730 return ref;
1733 static ULONG WINAPI Connector_Release(IConnector *iface)
1735 IConnectorImpl *This = impl_from_IConnector(iface);
1736 ULONG ref = InterlockedDecrement(&This->ref);
1737 TRACE("(%p) new ref %lu\n", This, ref);
1739 if (!ref)
1740 HeapFree(GetProcessHeap(), 0, This);
1742 return ref;
1745 static HRESULT WINAPI Connector_GetType(
1746 IConnector *This,
1747 ConnectorType *pType)
1749 FIXME("(%p) - partial stub\n", This);
1750 *pType = Physical_Internal;
1751 return S_OK;
1754 static HRESULT WINAPI Connector_GetDataFlow(
1755 IConnector *This,
1756 DataFlow *pFlow)
1758 FIXME("(%p) - stub\n", This);
1759 return E_NOTIMPL;
1762 static HRESULT WINAPI Connector_ConnectTo(
1763 IConnector *This,
1764 IConnector *pConnectTo)
1766 FIXME("(%p) - stub\n", This);
1767 return E_NOTIMPL;
1770 static HRESULT WINAPI Connector_Disconnect(
1771 IConnector *This)
1773 FIXME("(%p) - stub\n", This);
1774 return E_NOTIMPL;
1777 static HRESULT WINAPI Connector_IsConnected(
1778 IConnector *This,
1779 BOOL *pbConnected)
1781 FIXME("(%p) - stub\n", This);
1782 return E_NOTIMPL;
1785 static HRESULT WINAPI Connector_GetConnectedTo(
1786 IConnector *This,
1787 IConnector **ppConTo)
1789 FIXME("(%p) - stub\n", This);
1790 return E_NOTIMPL;
1793 static HRESULT WINAPI Connector_GetConnectorIdConnectedTo(
1794 IConnector *This,
1795 LPWSTR *ppwstrConnectorId)
1797 FIXME("(%p) - stub\n", This);
1798 return E_NOTIMPL;
1801 static HRESULT WINAPI Connector_GetDeviceIdConnectedTo(
1802 IConnector *This,
1803 LPWSTR *ppwstrDeviceId)
1805 FIXME("(%p) - stub\n", This);
1806 return E_NOTIMPL;
1809 static const IConnectorVtbl Connector_Vtbl =
1811 Connector_QueryInterface,
1812 Connector_AddRef,
1813 Connector_Release,
1814 Connector_GetType,
1815 Connector_GetDataFlow,
1816 Connector_ConnectTo,
1817 Connector_Disconnect,
1818 Connector_IsConnected,
1819 Connector_GetConnectedTo,
1820 Connector_GetConnectorIdConnectedTo,
1821 Connector_GetDeviceIdConnectedTo,
1824 HRESULT Connector_Create(IConnector **ppv)
1826 IConnectorImpl *This;
1828 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
1829 if (!This)
1830 return E_OUTOFMEMORY;
1832 This->IConnector_iface.lpVtbl = &Connector_Vtbl;
1833 This->ref = 1;
1835 *ppv = &This->IConnector_iface;
1837 return S_OK;
1840 static HRESULT WINAPI DT_QueryInterface(IDeviceTopology *iface, REFIID riid, void **ppv)
1842 IDeviceTopologyImpl *This = impl_from_IDeviceTopology(iface);
1843 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1845 if (!ppv)
1846 return E_POINTER;
1848 if (IsEqualIID(riid, &IID_IUnknown) ||
1849 IsEqualIID(riid, &IID_IDeviceTopology))
1850 *ppv = &This->IDeviceTopology_iface;
1851 else {
1852 *ppv = NULL;
1853 return E_NOINTERFACE;
1856 IUnknown_AddRef((IUnknown *)*ppv);
1858 return S_OK;
1861 static ULONG WINAPI DT_AddRef(IDeviceTopology *iface)
1863 IDeviceTopologyImpl *This = impl_from_IDeviceTopology(iface);
1864 ULONG ref = InterlockedIncrement(&This->ref);
1865 TRACE("(%p) new ref %lu\n", This, ref);
1866 return ref;
1869 static ULONG WINAPI DT_Release(IDeviceTopology *iface)
1871 IDeviceTopologyImpl *This = impl_from_IDeviceTopology(iface);
1872 ULONG ref = InterlockedDecrement(&This->ref);
1873 TRACE("(%p) new ref %lu\n", This, ref);
1875 if (!ref)
1876 HeapFree(GetProcessHeap(), 0, This);
1878 return ref;
1881 static HRESULT WINAPI DT_GetConnectorCount(IDeviceTopology *This,
1882 UINT *pCount)
1884 FIXME("(%p)->(%p) - partial stub\n", This, pCount);
1886 if (!pCount)
1887 return E_POINTER;
1889 *pCount = 1;
1890 return S_OK;
1893 static HRESULT WINAPI DT_GetConnector(IDeviceTopology *This,
1894 UINT nIndex,
1895 IConnector **ppConnector)
1897 FIXME("(%p)->(%u, %p) - partial stub\n", This, nIndex, ppConnector);
1899 if (nIndex == 0)
1901 return Connector_Create(ppConnector);
1904 return E_INVALIDARG;
1907 static HRESULT WINAPI DT_GetSubunitCount(IDeviceTopology *This,
1908 UINT *pCount)
1910 FIXME("(%p)->(%p) - stub\n", This, pCount);
1911 return E_NOTIMPL;
1914 static HRESULT WINAPI DT_GetSubunit(IDeviceTopology *This,
1915 UINT nIndex,
1916 ISubUnit **ppConnector)
1918 FIXME("(%p)->(%u, %p) - stub\n", This, nIndex, ppConnector);
1919 return E_NOTIMPL;
1922 static HRESULT WINAPI DT_GetPartById(IDeviceTopology *This,
1923 UINT nId,
1924 IPart **ppPart)
1926 FIXME("(%p)->(%u, %p) - stub\n", This, nId, ppPart);
1927 return E_NOTIMPL;
1930 static HRESULT WINAPI DT_GetDeviceId(IDeviceTopology *This,
1931 LPWSTR *ppwstrDeviceId)
1933 FIXME("(%p)->(%p) - stub\n", This, ppwstrDeviceId);
1934 return E_NOTIMPL;
1937 static HRESULT WINAPI DT_GetSignalPath(IDeviceTopology *This,
1938 IPart *pIPartFrom,
1939 IPart *pIPartTo,
1940 BOOL bRejectMixedPaths,
1941 IPartsList **ppParts)
1943 FIXME("(%p)->(%p, %p, %s, %p) - stub\n",
1944 This, pIPartFrom, pIPartTo, bRejectMixedPaths ? "TRUE" : "FALSE", ppParts);
1945 return E_NOTIMPL;
1948 static const IDeviceTopologyVtbl DeviceTopology_Vtbl =
1950 DT_QueryInterface,
1951 DT_AddRef,
1952 DT_Release,
1953 DT_GetConnectorCount,
1954 DT_GetConnector,
1955 DT_GetSubunitCount,
1956 DT_GetSubunit,
1957 DT_GetPartById,
1958 DT_GetDeviceId,
1959 DT_GetSignalPath,
1962 static HRESULT DeviceTopology_Create(IMMDevice *device, IDeviceTopology **ppv)
1964 IDeviceTopologyImpl *This;
1966 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*This));
1967 if (!This)
1968 return E_OUTOFMEMORY;
1970 This->IDeviceTopology_iface.lpVtbl = &DeviceTopology_Vtbl;
1971 This->ref = 1;
1973 *ppv = &This->IDeviceTopology_iface;
1975 return S_OK;