opengl32: Preserve the remainder of the version string when limiting the version...
[wine.git] / dlls / mmdevapi / devenum.c
blob6344ec66d345f8f3629ed699892ffe368c180277
1 /*
2 * Copyright 2009 Maarten Lankhorst
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #define NONAMELESSUNION
22 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winnls.h"
26 #include "winreg.h"
27 #include "wine/debug.h"
28 #include "wine/list.h"
30 #include "initguid.h"
31 #include "ole2.h"
32 #include "mmdeviceapi.h"
33 #include "dshow.h"
34 #include "dsound.h"
35 #include "audioclient.h"
36 #include "endpointvolume.h"
37 #include "audiopolicy.h"
38 #include "spatialaudioclient.h"
40 #include "mmdevapi_private.h"
41 #include "devpkey.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
45 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 EDataFlow flow;
80 DWORD state;
81 } MMDevColImpl;
83 typedef struct IPropertyBagImpl {
84 IPropertyBag IPropertyBag_iface;
85 GUID devguid;
86 } IPropertyBagImpl;
88 static const IPropertyBagVtbl PB_Vtbl;
90 static HRESULT MMDevPropStore_Create(MMDevice *This, DWORD access, IPropertyStore **ppv);
92 static inline MMDevPropStore *impl_from_IPropertyStore(IPropertyStore *iface)
94 return CONTAINING_RECORD(iface, MMDevPropStore, IPropertyStore_iface);
97 static inline MMDevEnumImpl *impl_from_IMMDeviceEnumerator(IMMDeviceEnumerator *iface)
99 return CONTAINING_RECORD(iface, MMDevEnumImpl, IMMDeviceEnumerator_iface);
102 static inline MMDevColImpl *impl_from_IMMDeviceCollection(IMMDeviceCollection *iface)
104 return CONTAINING_RECORD(iface, MMDevColImpl, IMMDeviceCollection_iface);
107 static inline IPropertyBagImpl *impl_from_IPropertyBag(IPropertyBag *iface)
109 return CONTAINING_RECORD(iface, IPropertyBagImpl, IPropertyBag_iface);
112 static const WCHAR propkey_formatW[] = L"{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X},%d";
114 static HRESULT MMDevPropStore_OpenPropKey(const GUID *guid, DWORD flow, HKEY *propkey)
116 WCHAR buffer[39];
117 LONG ret;
118 HKEY key;
119 StringFromGUID2(guid, buffer, 39);
120 if ((ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, buffer, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, &key)) != ERROR_SUCCESS)
122 WARN("Opening key %s failed with %lu\n", debugstr_w(buffer), ret);
123 return E_FAIL;
125 ret = RegOpenKeyExW(key, L"Properties", 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, propkey);
126 RegCloseKey(key);
127 if (ret != ERROR_SUCCESS)
129 WARN("Opening key Properties failed with %lu\n", ret);
130 return E_FAIL;
132 return S_OK;
135 static HRESULT MMDevice_GetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, PROPVARIANT *pv)
137 WCHAR buffer[80];
138 const GUID *id = &key->fmtid;
139 DWORD type, size;
140 HRESULT hr = S_OK;
141 HKEY regkey;
142 LONG ret;
144 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
145 if (FAILED(hr))
146 return hr;
147 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
148 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
149 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
150 ret = RegGetValueW(regkey, NULL, buffer, RRF_RT_ANY, &type, NULL, &size);
151 if (ret != ERROR_SUCCESS)
153 WARN("Reading %s returned %ld\n", debugstr_w(buffer), ret);
154 RegCloseKey(regkey);
155 pv->vt = VT_EMPTY;
156 return S_OK;
159 switch (type)
161 case REG_SZ:
163 pv->vt = VT_LPWSTR;
164 pv->pwszVal = CoTaskMemAlloc(size);
165 if (!pv->pwszVal)
166 hr = E_OUTOFMEMORY;
167 else
168 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_SZ, NULL, (BYTE*)pv->pwszVal, &size);
169 break;
171 case REG_DWORD:
173 pv->vt = VT_UI4;
174 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_DWORD, NULL, (BYTE*)&pv->ulVal, &size);
175 break;
177 case REG_BINARY:
179 pv->vt = VT_BLOB;
180 pv->blob.cbSize = size;
181 pv->blob.pBlobData = CoTaskMemAlloc(size);
182 if (!pv->blob.pBlobData)
183 hr = E_OUTOFMEMORY;
184 else
185 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_BINARY, NULL, (BYTE*)pv->blob.pBlobData, &size);
186 break;
188 default:
189 ERR("Unknown/unhandled type: %lu\n", type);
190 PropVariantClear(pv);
191 break;
193 RegCloseKey(regkey);
194 return hr;
197 static HRESULT MMDevice_SetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, REFPROPVARIANT pv)
199 WCHAR buffer[80];
200 const GUID *id = &key->fmtid;
201 HRESULT hr;
202 HKEY regkey;
203 LONG ret;
205 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
206 if (FAILED(hr))
207 return hr;
208 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
209 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
210 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
211 switch (pv->vt)
213 case VT_UI4:
215 ret = RegSetValueExW(regkey, buffer, 0, REG_DWORD, (const BYTE*)&pv->ulVal, sizeof(DWORD));
216 break;
218 case VT_BLOB:
220 ret = RegSetValueExW(regkey, buffer, 0, REG_BINARY, pv->blob.pBlobData, pv->blob.cbSize);
221 TRACE("Blob %p %lu\n", pv->blob.pBlobData, pv->blob.cbSize);
223 break;
225 case VT_LPWSTR:
227 ret = RegSetValueExW(regkey, buffer, 0, REG_SZ, (const BYTE*)pv->pwszVal, sizeof(WCHAR)*(1+lstrlenW(pv->pwszVal)));
228 break;
230 default:
231 ret = 0;
232 FIXME("Unhandled type %u\n", pv->vt);
233 hr = E_INVALIDARG;
234 break;
236 RegCloseKey(regkey);
237 TRACE("Writing %s returned %lu\n", debugstr_w(buffer), ret);
238 return hr;
241 static HRESULT set_driver_prop_value(GUID *id, const EDataFlow flow, const PROPERTYKEY *prop)
243 struct get_prop_value_params params;
244 PROPVARIANT pv;
245 char *dev_name;
246 unsigned int size = 0;
248 TRACE("%s, (%s,%lu)\n", wine_dbgstr_guid(id), wine_dbgstr_guid(&prop->fmtid), prop->pid);
250 if (!drvs.pget_device_name_from_guid(id, &dev_name, &params.flow))
251 return E_FAIL;
253 params.device = dev_name;
254 params.guid = id;
255 params.prop = prop;
256 params.value = &pv;
257 params.buffer = NULL;
258 params.buffer_size = &size;
260 while (1) {
261 __wine_unix_call(drvs.module_unixlib, get_prop_value, &params);
263 if (params.result != E_NOT_SUFFICIENT_BUFFER)
264 break;
266 CoTaskMemFree(params.buffer);
267 params.buffer = CoTaskMemAlloc(*params.buffer_size);
268 if (!params.buffer) {
269 free(dev_name);
270 return E_OUTOFMEMORY;
274 if (FAILED(params.result))
275 CoTaskMemFree(params.buffer);
277 free(dev_name);
279 if (SUCCEEDED(params.result)) {
280 MMDevice_SetPropValue(id, flow, prop, &pv);
281 PropVariantClear(&pv);
284 return params.result;
287 struct product_name_overrides
289 const WCHAR *id;
290 const WCHAR *product;
293 static const struct product_name_overrides product_name_overrides[] =
295 /* Sony controllers */
296 { .id = L"VID_054C&PID_0CE6", .product = L"Wireless Controller" },
299 static const WCHAR *find_product_name_override(const WCHAR *device_id)
301 const WCHAR *match_id = wcschr( device_id, '\\' ) + 1;
302 DWORD i;
304 for (i = 0; i < ARRAY_SIZE(product_name_overrides); ++i)
305 if (!wcsnicmp( product_name_overrides[i].id, match_id, 17 ))
306 return product_name_overrides[i].product;
308 return NULL;
311 /* Creates or updates the state of a device
312 * If GUID is null, a random guid will be assigned
313 * and the device will be created
315 static MMDevice *MMDevice_Create(WCHAR *name, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault)
317 HKEY key, root;
318 MMDevice *device, *cur = NULL;
319 WCHAR guidstr[39];
321 static const PROPERTYKEY deviceinterface_key = {
322 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
325 static const PROPERTYKEY devicepath_key = {
326 {0xb3f8fa53, 0x0004, 0x438e, {0x90, 0x03, 0x51, 0xa4, 0x6e, 0x13, 0x9b, 0xfc}}, 2
329 LIST_FOR_EACH_ENTRY(device, &device_list, MMDevice, entry)
331 if (device->flow == flow && IsEqualGUID(&device->devguid, id)){
332 cur = device;
333 break;
337 if(!cur){
338 /* No device found, allocate new one */
339 cur = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cur));
340 if (!cur)
341 return NULL;
343 cur->IMMDevice_iface.lpVtbl = &MMDeviceVtbl;
344 cur->IMMEndpoint_iface.lpVtbl = &MMEndpointVtbl;
346 InitializeCriticalSection(&cur->crst);
347 cur->crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MMDevice.crst");
349 list_add_tail(&device_list, &cur->entry);
350 }else if(cur->ref > 0)
351 WARN("Modifying an MMDevice with postitive reference count!\n");
353 HeapFree(GetProcessHeap(), 0, cur->drv_id);
354 cur->drv_id = name;
356 cur->flow = flow;
357 cur->state = state;
358 cur->devguid = *id;
360 StringFromGUID2(&cur->devguid, guidstr, ARRAY_SIZE(guidstr));
362 if (flow == eRender)
363 root = key_render;
364 else
365 root = key_capture;
367 if (RegCreateKeyExW(root, guidstr, 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &key, NULL) == ERROR_SUCCESS)
369 HKEY keyprop;
370 RegSetValueExW(key, L"DeviceState", 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
371 if (!RegCreateKeyExW(key, L"Properties", 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &keyprop, NULL))
373 PROPVARIANT pv;
375 pv.vt = VT_LPWSTR;
376 pv.pwszVal = name;
378 if (SUCCEEDED(set_driver_prop_value(id, flow, &devicepath_key))) {
379 PROPVARIANT pv2;
381 PropVariantInit(&pv2);
383 if (SUCCEEDED(MMDevice_GetPropValue(id, flow, &devicepath_key, &pv2)) && pv2.vt == VT_LPWSTR) {
384 const WCHAR *override;
385 if ((override = find_product_name_override(pv2.pwszVal)) != NULL)
386 pv.pwszVal = (WCHAR*) override;
389 PropVariantClear(&pv2);
392 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
393 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_DeviceInterface_FriendlyName, &pv);
394 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_DeviceDesc, &pv);
396 pv.pwszVal = guidstr;
397 MMDevice_SetPropValue(id, flow, &deviceinterface_key, &pv);
399 if (FAILED(set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_FormFactor)))
401 pv.vt = VT_UI4;
402 pv.ulVal = (flow == eCapture) ? Microphone : Speakers;
404 MMDevice_SetPropValue(id, flow, &PKEY_AudioEndpoint_FormFactor, &pv);
407 if (flow != eCapture)
409 PROPVARIANT pv2;
411 PropVariantInit(&pv2);
413 /* make read-write by not overwriting if already set */
414 if (FAILED(MMDevice_GetPropValue(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers, &pv2)) || pv2.vt != VT_UI4)
415 set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers);
417 PropVariantClear(&pv2);
420 RegCloseKey(keyprop);
422 RegCloseKey(key);
425 if (setdefault)
427 if (flow == eRender)
428 MMDevice_def_play = cur;
429 else
430 MMDevice_def_rec = cur;
432 return cur;
435 HRESULT load_devices_from_reg(void)
437 DWORD i = 0;
438 HKEY root, cur;
439 LONG ret;
440 DWORD curflow;
442 ret = RegCreateKeyExW(HKEY_LOCAL_MACHINE,
443 L"Software\\Microsoft\\Windows\\CurrentVersion\\MMDevices\\Audio", 0, NULL, 0,
444 KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &root, NULL);
445 if (ret == ERROR_SUCCESS)
446 ret = RegCreateKeyExW(root, L"Capture", 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_capture, NULL);
447 if (ret == ERROR_SUCCESS)
448 ret = RegCreateKeyExW(root, L"Render", 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_render, NULL);
449 RegCloseKey(root);
450 cur = key_capture;
451 curflow = eCapture;
452 if (ret != ERROR_SUCCESS)
454 RegCloseKey(key_capture);
455 key_render = key_capture = NULL;
456 WARN("Couldn't create key: %lu\n", ret);
457 return E_FAIL;
460 do {
461 WCHAR guidvalue[39];
462 GUID guid;
463 DWORD len;
464 PROPVARIANT pv = { VT_EMPTY };
466 len = ARRAY_SIZE(guidvalue);
467 ret = RegEnumKeyExW(cur, i++, guidvalue, &len, NULL, NULL, NULL, NULL);
468 if (ret == ERROR_NO_MORE_ITEMS)
470 if (cur == key_capture)
472 cur = key_render;
473 curflow = eRender;
474 i = 0;
475 continue;
477 break;
479 if (ret != ERROR_SUCCESS)
480 continue;
481 if (SUCCEEDED(CLSIDFromString(guidvalue, &guid))
482 && SUCCEEDED(MMDevice_GetPropValue(&guid, curflow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv))
483 && pv.vt == VT_LPWSTR)
485 DWORD size_bytes = (lstrlenW(pv.pwszVal) + 1) * sizeof(WCHAR);
486 WCHAR *name = HeapAlloc(GetProcessHeap(), 0, size_bytes);
487 memcpy(name, pv.pwszVal, size_bytes);
488 MMDevice_Create(name, &guid, curflow,
489 DEVICE_STATE_NOTPRESENT, FALSE);
490 CoTaskMemFree(pv.pwszVal);
492 } while (1);
494 return S_OK;
497 static HRESULT set_format(MMDevice *dev)
499 HRESULT hr;
500 IAudioClient *client;
501 WAVEFORMATEX *fmt;
502 PROPVARIANT pv = { VT_EMPTY };
504 hr = AudioClient_Create(&dev->devguid, &dev->IMMDevice_iface, &client);
505 if(FAILED(hr))
506 return hr;
508 hr = IAudioClient_GetMixFormat(client, &fmt);
509 if(FAILED(hr)){
510 IAudioClient_Release(client);
511 return hr;
514 IAudioClient_Release(client);
516 pv.vt = VT_BLOB;
517 pv.blob.cbSize = sizeof(WAVEFORMATEX) + fmt->cbSize;
518 pv.blob.pBlobData = (BYTE*)fmt;
519 MMDevice_SetPropValue(&dev->devguid, dev->flow,
520 &PKEY_AudioEngine_DeviceFormat, &pv);
521 MMDevice_SetPropValue(&dev->devguid, dev->flow,
522 &PKEY_AudioEngine_OEMFormat, &pv);
523 CoTaskMemFree(fmt);
525 return S_OK;
528 HRESULT load_driver_devices(EDataFlow flow)
530 WCHAR **ids;
531 GUID *guids;
532 UINT num, def, i;
533 HRESULT hr;
535 if(!drvs.pGetEndpointIDs)
536 return S_OK;
538 hr = drvs.pGetEndpointIDs(flow, &ids, &guids, &num, &def);
539 if(FAILED(hr))
540 return hr;
542 for(i = 0; i < num; ++i){
543 MMDevice *dev;
544 dev = MMDevice_Create(ids[i], &guids[i], flow, DEVICE_STATE_ACTIVE,
545 def == i);
546 set_format(dev);
549 HeapFree(GetProcessHeap(), 0, guids);
550 HeapFree(GetProcessHeap(), 0, ids);
552 return S_OK;
555 static void MMDevice_Destroy(MMDevice *This)
557 TRACE("Freeing %s\n", debugstr_w(This->drv_id));
558 list_remove(&This->entry);
559 This->crst.DebugInfo->Spare[0] = 0;
560 DeleteCriticalSection(&This->crst);
561 HeapFree(GetProcessHeap(), 0, This->drv_id);
562 HeapFree(GetProcessHeap(), 0, This);
565 static inline MMDevice *impl_from_IMMDevice(IMMDevice *iface)
567 return CONTAINING_RECORD(iface, MMDevice, IMMDevice_iface);
570 static HRESULT WINAPI MMDevice_QueryInterface(IMMDevice *iface, REFIID riid, void **ppv)
572 MMDevice *This = impl_from_IMMDevice(iface);
573 TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ppv);
575 if (!ppv)
576 return E_POINTER;
577 *ppv = NULL;
578 if (IsEqualIID(riid, &IID_IUnknown)
579 || IsEqualIID(riid, &IID_IMMDevice))
580 *ppv = &This->IMMDevice_iface;
581 else if (IsEqualIID(riid, &IID_IMMEndpoint))
582 *ppv = &This->IMMEndpoint_iface;
583 if (*ppv)
585 IUnknown_AddRef((IUnknown*)*ppv);
586 return S_OK;
588 WARN("Unknown interface %s\n", debugstr_guid(riid));
589 return E_NOINTERFACE;
592 static ULONG WINAPI MMDevice_AddRef(IMMDevice *iface)
594 MMDevice *This = impl_from_IMMDevice(iface);
595 LONG ref;
597 ref = InterlockedIncrement(&This->ref);
598 TRACE("Refcount now %li\n", ref);
599 return ref;
602 static ULONG WINAPI MMDevice_Release(IMMDevice *iface)
604 MMDevice *This = impl_from_IMMDevice(iface);
605 LONG ref;
607 ref = InterlockedDecrement(&This->ref);
608 TRACE("Refcount now %li\n", ref);
609 return ref;
612 static HRESULT WINAPI MMDevice_Activate(IMMDevice *iface, REFIID riid, DWORD clsctx, PROPVARIANT *params, void **ppv)
614 HRESULT hr = E_NOINTERFACE;
615 MMDevice *This = impl_from_IMMDevice(iface);
617 TRACE("(%p)->(%s, %lx, %p, %p)\n", iface, debugstr_guid(riid), clsctx, params, ppv);
619 if (!ppv)
620 return E_POINTER;
622 if (IsEqualIID(riid, &IID_IAudioClient) ||
623 IsEqualIID(riid, &IID_IAudioClient2) ||
624 IsEqualIID(riid, &IID_IAudioClient3)){
625 hr = AudioClient_Create(&This->devguid, iface, (IAudioClient**)ppv);
626 }else if (IsEqualIID(riid, &IID_IAudioEndpointVolume) ||
627 IsEqualIID(riid, &IID_IAudioEndpointVolumeEx))
628 hr = AudioEndpointVolume_Create(This, (IAudioEndpointVolumeEx**)ppv);
629 else if (IsEqualIID(riid, &IID_IAudioSessionManager)
630 || IsEqualIID(riid, &IID_IAudioSessionManager2))
632 hr = AudioSessionManager_Create(iface, (IAudioSessionManager2**)ppv);
634 else if (IsEqualIID(riid, &IID_IBaseFilter))
636 if (This->flow == eRender)
637 hr = CoCreateInstance(&CLSID_DSoundRender, NULL, clsctx, riid, ppv);
638 else
639 ERR("Not supported for recording?\n");
640 if (SUCCEEDED(hr))
642 IPersistPropertyBag *ppb;
643 hr = IUnknown_QueryInterface((IUnknown*)*ppv, &IID_IPersistPropertyBag, (void **)&ppb);
644 if (SUCCEEDED(hr))
646 /* ::Load cannot assume the interface stays alive after the function returns,
647 * so just create the interface on the stack, saves a lot of complicated code */
648 IPropertyBagImpl bag = { { &PB_Vtbl } };
649 bag.devguid = This->devguid;
650 hr = IPersistPropertyBag_Load(ppb, &bag.IPropertyBag_iface, NULL);
651 IPersistPropertyBag_Release(ppb);
652 if (FAILED(hr))
653 IBaseFilter_Release((IBaseFilter*)*ppv);
655 else
657 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
658 hr = S_OK;
662 else if (IsEqualIID(riid, &IID_IDeviceTopology))
664 FIXME("IID_IDeviceTopology unsupported\n");
666 else if (IsEqualIID(riid, &IID_IDirectSound)
667 || IsEqualIID(riid, &IID_IDirectSound8))
669 if (This->flow == eRender)
670 hr = CoCreateInstance(&CLSID_DirectSound8, NULL, clsctx, riid, ppv);
671 if (SUCCEEDED(hr))
673 hr = IDirectSound_Initialize((IDirectSound*)*ppv, &This->devguid);
674 if (FAILED(hr))
675 IDirectSound_Release((IDirectSound*)*ppv);
678 else if (IsEqualIID(riid, &IID_IDirectSoundCapture))
680 if (This->flow == eCapture)
681 hr = CoCreateInstance(&CLSID_DirectSoundCapture8, NULL, clsctx, riid, ppv);
682 if (SUCCEEDED(hr))
684 hr = IDirectSoundCapture_Initialize((IDirectSoundCapture*)*ppv, &This->devguid);
685 if (FAILED(hr))
686 IDirectSoundCapture_Release((IDirectSoundCapture*)*ppv);
689 else if (IsEqualIID(riid, &IID_ISpatialAudioClient))
691 hr = SpatialAudioClient_Create(iface, (ISpatialAudioClient**)ppv);
693 else
694 ERR("Invalid/unknown iid %s\n", debugstr_guid(riid));
696 if (FAILED(hr))
697 *ppv = NULL;
699 TRACE("Returning %08lx\n", hr);
700 return hr;
703 static HRESULT WINAPI MMDevice_OpenPropertyStore(IMMDevice *iface, DWORD access, IPropertyStore **ppv)
705 MMDevice *This = impl_from_IMMDevice(iface);
706 TRACE("(%p)->(%lx,%p)\n", This, access, ppv);
708 if (!ppv)
709 return E_POINTER;
710 return MMDevPropStore_Create(This, access, ppv);
713 static HRESULT WINAPI MMDevice_GetId(IMMDevice *iface, WCHAR **itemid)
715 MMDevice *This = impl_from_IMMDevice(iface);
716 WCHAR *str;
717 GUID *id = &This->devguid;
719 TRACE("(%p)->(%p)\n", This, itemid);
720 if (!itemid)
721 return E_POINTER;
722 *itemid = str = CoTaskMemAlloc(56 * sizeof(WCHAR));
723 if (!str)
724 return E_OUTOFMEMORY;
725 wsprintfW(str, L"{0.0.%u.00000000}.{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
726 This->flow, id->Data1, id->Data2, id->Data3,
727 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
728 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7]);
729 TRACE("returning %s\n", wine_dbgstr_w(str));
730 return S_OK;
733 static HRESULT WINAPI MMDevice_GetState(IMMDevice *iface, DWORD *state)
735 MMDevice *This = impl_from_IMMDevice(iface);
736 TRACE("(%p)->(%p)\n", iface, state);
738 if (!state)
739 return E_POINTER;
740 *state = This->state;
741 return S_OK;
744 static const IMMDeviceVtbl MMDeviceVtbl =
746 MMDevice_QueryInterface,
747 MMDevice_AddRef,
748 MMDevice_Release,
749 MMDevice_Activate,
750 MMDevice_OpenPropertyStore,
751 MMDevice_GetId,
752 MMDevice_GetState
755 static inline MMDevice *impl_from_IMMEndpoint(IMMEndpoint *iface)
757 return CONTAINING_RECORD(iface, MMDevice, IMMEndpoint_iface);
760 static HRESULT WINAPI MMEndpoint_QueryInterface(IMMEndpoint *iface, REFIID riid, void **ppv)
762 MMDevice *This = impl_from_IMMEndpoint(iface);
763 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
764 return IMMDevice_QueryInterface(&This->IMMDevice_iface, riid, ppv);
767 static ULONG WINAPI MMEndpoint_AddRef(IMMEndpoint *iface)
769 MMDevice *This = impl_from_IMMEndpoint(iface);
770 TRACE("(%p)\n", This);
771 return IMMDevice_AddRef(&This->IMMDevice_iface);
774 static ULONG WINAPI MMEndpoint_Release(IMMEndpoint *iface)
776 MMDevice *This = impl_from_IMMEndpoint(iface);
777 TRACE("(%p)\n", This);
778 return IMMDevice_Release(&This->IMMDevice_iface);
781 static HRESULT WINAPI MMEndpoint_GetDataFlow(IMMEndpoint *iface, EDataFlow *flow)
783 MMDevice *This = impl_from_IMMEndpoint(iface);
784 TRACE("(%p)->(%p)\n", This, flow);
785 if (!flow)
786 return E_POINTER;
787 *flow = This->flow;
788 return S_OK;
791 static const IMMEndpointVtbl MMEndpointVtbl =
793 MMEndpoint_QueryInterface,
794 MMEndpoint_AddRef,
795 MMEndpoint_Release,
796 MMEndpoint_GetDataFlow
799 static HRESULT MMDevCol_Create(IMMDeviceCollection **ppv, EDataFlow flow, DWORD state)
801 MMDevColImpl *This;
803 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
804 *ppv = NULL;
805 if (!This)
806 return E_OUTOFMEMORY;
807 This->IMMDeviceCollection_iface.lpVtbl = &MMDevColVtbl;
808 This->ref = 1;
809 This->flow = flow;
810 This->state = state;
811 *ppv = &This->IMMDeviceCollection_iface;
812 return S_OK;
815 static void MMDevCol_Destroy(MMDevColImpl *This)
817 HeapFree(GetProcessHeap(), 0, This);
820 static HRESULT WINAPI MMDevCol_QueryInterface(IMMDeviceCollection *iface, REFIID riid, void **ppv)
822 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
823 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
825 if (!ppv)
826 return E_POINTER;
827 if (IsEqualIID(riid, &IID_IUnknown)
828 || IsEqualIID(riid, &IID_IMMDeviceCollection))
829 *ppv = &This->IMMDeviceCollection_iface;
830 else
831 *ppv = NULL;
832 if (!*ppv)
833 return E_NOINTERFACE;
834 IUnknown_AddRef((IUnknown*)*ppv);
835 return S_OK;
838 static ULONG WINAPI MMDevCol_AddRef(IMMDeviceCollection *iface)
840 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
841 LONG ref = InterlockedIncrement(&This->ref);
842 TRACE("Refcount now %li\n", ref);
843 return ref;
846 static ULONG WINAPI MMDevCol_Release(IMMDeviceCollection *iface)
848 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
849 LONG ref = InterlockedDecrement(&This->ref);
850 TRACE("Refcount now %li\n", ref);
851 if (!ref)
852 MMDevCol_Destroy(This);
853 return ref;
856 static HRESULT WINAPI MMDevCol_GetCount(IMMDeviceCollection *iface, UINT *numdevs)
858 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
859 MMDevice *cur;
861 TRACE("(%p)->(%p)\n", This, numdevs);
862 if (!numdevs)
863 return E_POINTER;
865 *numdevs = 0;
866 LIST_FOR_EACH_ENTRY(cur, &device_list, MMDevice, entry)
868 if ((cur->flow == This->flow || This->flow == eAll)
869 && (cur->state & This->state))
870 ++(*numdevs);
872 return S_OK;
875 static HRESULT WINAPI MMDevCol_Item(IMMDeviceCollection *iface, UINT n, IMMDevice **dev)
877 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
878 MMDevice *cur;
879 DWORD i = 0;
881 TRACE("(%p)->(%u, %p)\n", This, n, dev);
882 if (!dev)
883 return E_POINTER;
885 LIST_FOR_EACH_ENTRY(cur, &device_list, MMDevice, entry)
887 if ((cur->flow == This->flow || This->flow == eAll)
888 && (cur->state & This->state)
889 && i++ == n)
891 *dev = &cur->IMMDevice_iface;
892 IMMDevice_AddRef(*dev);
893 return S_OK;
896 WARN("Could not obtain item %u\n", n);
897 *dev = NULL;
898 return E_INVALIDARG;
901 static const IMMDeviceCollectionVtbl MMDevColVtbl =
903 MMDevCol_QueryInterface,
904 MMDevCol_AddRef,
905 MMDevCol_Release,
906 MMDevCol_GetCount,
907 MMDevCol_Item
910 HRESULT MMDevEnum_Create(REFIID riid, void **ppv)
912 return IMMDeviceEnumerator_QueryInterface(&enumerator.IMMDeviceEnumerator_iface, riid, ppv);
915 void MMDevEnum_Free(void)
917 MMDevice *device, *next;
918 LIST_FOR_EACH_ENTRY_SAFE(device, next, &device_list, MMDevice, entry)
919 MMDevice_Destroy(device);
920 RegCloseKey(key_render);
921 RegCloseKey(key_capture);
922 key_render = key_capture = NULL;
925 static HRESULT WINAPI MMDevEnum_QueryInterface(IMMDeviceEnumerator *iface, REFIID riid, void **ppv)
927 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
928 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
930 if (!ppv)
931 return E_POINTER;
932 if (IsEqualIID(riid, &IID_IUnknown)
933 || IsEqualIID(riid, &IID_IMMDeviceEnumerator))
934 *ppv = &This->IMMDeviceEnumerator_iface;
935 else
936 *ppv = NULL;
937 if (!*ppv)
938 return E_NOINTERFACE;
939 IUnknown_AddRef((IUnknown*)*ppv);
940 return S_OK;
943 static ULONG WINAPI MMDevEnum_AddRef(IMMDeviceEnumerator *iface)
945 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
946 LONG ref = InterlockedIncrement(&This->ref);
947 TRACE("Refcount now %li\n", ref);
948 return ref;
951 static ULONG WINAPI MMDevEnum_Release(IMMDeviceEnumerator *iface)
953 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
954 LONG ref = InterlockedDecrement(&This->ref);
955 TRACE("Refcount now %li\n", ref);
956 return ref;
959 static HRESULT WINAPI MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator *iface, EDataFlow flow, DWORD mask, IMMDeviceCollection **devices)
961 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
962 TRACE("(%p)->(%u,%lu,%p)\n", This, flow, mask, devices);
963 if (!devices)
964 return E_POINTER;
965 *devices = NULL;
966 if (flow >= EDataFlow_enum_count)
967 return E_INVALIDARG;
968 if (mask & ~DEVICE_STATEMASK_ALL)
969 return E_INVALIDARG;
970 return MMDevCol_Create(devices, flow, mask);
973 static HRESULT WINAPI MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator *iface, EDataFlow flow, ERole role, IMMDevice **device)
975 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
976 WCHAR reg_key[256];
977 HKEY key;
978 HRESULT hr;
980 TRACE("(%p)->(%u,%u,%p)\n", This, flow, role, device);
982 if (!device)
983 return E_POINTER;
985 if((flow != eRender && flow != eCapture) ||
986 (role != eConsole && role != eMultimedia && role != eCommunications)){
987 WARN("Unknown flow (%u) or role (%u)\n", flow, role);
988 return E_INVALIDARG;
991 *device = NULL;
993 if(!drvs.module_name[0])
994 return E_NOTFOUND;
996 lstrcpyW(reg_key, drv_keyW);
997 lstrcatW(reg_key, L"\\");
998 lstrcatW(reg_key, drvs.module_name);
1000 if(RegOpenKeyW(HKEY_CURRENT_USER, reg_key, &key) == ERROR_SUCCESS){
1001 const WCHAR *reg_x_name, *reg_vx_name;
1002 WCHAR def_id[256];
1003 DWORD size = sizeof(def_id), state;
1005 if(flow == eRender){
1006 reg_x_name = L"DefaultOutput";
1007 reg_vx_name = L"DefaultVoiceOutput";
1008 }else{
1009 reg_x_name = L"DefaultInput";
1010 reg_vx_name = L"DefaultVoiceInput";
1013 if(role == eCommunications &&
1014 RegQueryValueExW(key, reg_vx_name, 0, NULL,
1015 (BYTE*)def_id, &size) == ERROR_SUCCESS){
1016 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
1017 if(SUCCEEDED(hr)){
1018 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
1019 state == DEVICE_STATE_ACTIVE){
1020 RegCloseKey(key);
1021 return S_OK;
1025 TRACE("Unable to find voice device %s\n", wine_dbgstr_w(def_id));
1028 if(RegQueryValueExW(key, reg_x_name, 0, NULL,
1029 (BYTE*)def_id, &size) == ERROR_SUCCESS){
1030 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
1031 if(SUCCEEDED(hr)){
1032 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
1033 state == DEVICE_STATE_ACTIVE){
1034 RegCloseKey(key);
1035 return S_OK;
1039 TRACE("Unable to find device %s\n", wine_dbgstr_w(def_id));
1042 RegCloseKey(key);
1045 if (flow == eRender)
1046 *device = &MMDevice_def_play->IMMDevice_iface;
1047 else
1048 *device = &MMDevice_def_rec->IMMDevice_iface;
1050 if (!*device)
1051 return E_NOTFOUND;
1052 IMMDevice_AddRef(*device);
1053 return S_OK;
1056 static HRESULT WINAPI MMDevEnum_GetDevice(IMMDeviceEnumerator *iface, const WCHAR *name, IMMDevice **device)
1058 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1059 MMDevice *impl;
1060 IMMDevice *dev = NULL;
1062 TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), device);
1064 if(!name || !device)
1065 return E_POINTER;
1067 if(!lstrcmpW(name, L"Wine info device")){
1068 *device = &info_device;
1069 return S_OK;
1072 LIST_FOR_EACH_ENTRY(impl, &device_list, MMDevice, entry)
1074 HRESULT hr;
1075 WCHAR *str;
1076 dev = &impl->IMMDevice_iface;
1077 hr = IMMDevice_GetId(dev, &str);
1078 if (FAILED(hr))
1080 WARN("GetId failed: %08lx\n", hr);
1081 continue;
1084 if (str && !lstrcmpW(str, name))
1086 CoTaskMemFree(str);
1087 IMMDevice_AddRef(dev);
1088 *device = dev;
1089 return S_OK;
1091 CoTaskMemFree(str);
1093 TRACE("Could not find device %s\n", debugstr_w(name));
1094 return E_INVALIDARG;
1097 struct NotificationClientWrapper {
1098 IMMNotificationClient *client;
1099 struct list entry;
1102 static struct list g_notif_clients = LIST_INIT(g_notif_clients);
1103 static HANDLE g_notif_thread;
1105 static CRITICAL_SECTION g_notif_lock;
1106 static CRITICAL_SECTION_DEBUG g_notif_lock_debug =
1108 0, 0, &g_notif_lock,
1109 { &g_notif_lock_debug.ProcessLocksList, &g_notif_lock_debug.ProcessLocksList },
1110 0, 0, { (DWORD_PTR)(__FILE__ ": g_notif_lock") }
1112 static CRITICAL_SECTION g_notif_lock = { &g_notif_lock_debug, -1, 0, 0, 0, 0 };
1114 static void notify_clients(EDataFlow flow, ERole role, const WCHAR *id)
1116 struct NotificationClientWrapper *wrapper;
1117 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients,
1118 struct NotificationClientWrapper, entry)
1119 IMMNotificationClient_OnDefaultDeviceChanged(wrapper->client, flow,
1120 role, id);
1122 /* Windows 7 treats changes to eConsole as changes to eMultimedia */
1123 if(role == eConsole)
1124 notify_clients(flow, eMultimedia, id);
1127 static BOOL notify_if_changed(EDataFlow flow, ERole role, HKEY key,
1128 const WCHAR *val_name, WCHAR *old_val, IMMDevice *def_dev)
1130 WCHAR new_val[64], *id;
1131 DWORD size;
1132 HRESULT hr;
1134 size = sizeof(new_val);
1135 if(RegQueryValueExW(key, val_name, 0, NULL,
1136 (BYTE*)new_val, &size) != ERROR_SUCCESS){
1137 if(old_val[0] != 0){
1138 /* set by user -> system default */
1139 if(def_dev){
1140 hr = IMMDevice_GetId(def_dev, &id);
1141 if(FAILED(hr)){
1142 ERR("GetId failed: %08lx\n", hr);
1143 return FALSE;
1145 }else
1146 id = NULL;
1148 notify_clients(flow, role, id);
1149 old_val[0] = 0;
1150 CoTaskMemFree(id);
1152 return TRUE;
1155 /* system default -> system default, noop */
1156 return FALSE;
1159 if(!lstrcmpW(old_val, new_val)){
1160 /* set by user -> same value */
1161 return FALSE;
1164 if(new_val[0] != 0){
1165 /* set by user -> different value */
1166 notify_clients(flow, role, new_val);
1167 memcpy(old_val, new_val, sizeof(new_val));
1168 return TRUE;
1171 /* set by user -> system default */
1172 if(def_dev){
1173 hr = IMMDevice_GetId(def_dev, &id);
1174 if(FAILED(hr)){
1175 ERR("GetId failed: %08lx\n", hr);
1176 return FALSE;
1178 }else
1179 id = NULL;
1181 notify_clients(flow, role, id);
1182 old_val[0] = 0;
1183 CoTaskMemFree(id);
1185 return TRUE;
1188 static DWORD WINAPI notif_thread_proc(void *user)
1190 HKEY key;
1191 WCHAR reg_key[256];
1192 WCHAR out_name[64], vout_name[64], in_name[64], vin_name[64];
1193 DWORD size;
1195 SetThreadDescription(GetCurrentThread(), L"wine_mmdevapi_notification");
1197 lstrcpyW(reg_key, drv_keyW);
1198 lstrcatW(reg_key, L"\\");
1199 lstrcatW(reg_key, drvs.module_name);
1201 if(RegCreateKeyExW(HKEY_CURRENT_USER, reg_key, 0, NULL, 0,
1202 MAXIMUM_ALLOWED, NULL, &key, NULL) != ERROR_SUCCESS){
1203 ERR("RegCreateKeyEx failed: %lu\n", GetLastError());
1204 return 1;
1207 size = sizeof(out_name);
1208 if(RegQueryValueExW(key, L"DefaultOutput", 0, NULL, (BYTE*)out_name, &size) != ERROR_SUCCESS)
1209 out_name[0] = 0;
1211 size = sizeof(vout_name);
1212 if(RegQueryValueExW(key, L"DefaultVoiceOutput", 0, NULL, (BYTE*)vout_name, &size) != ERROR_SUCCESS)
1213 vout_name[0] = 0;
1215 size = sizeof(in_name);
1216 if(RegQueryValueExW(key, L"DefaultInput", 0, NULL, (BYTE*)in_name, &size) != ERROR_SUCCESS)
1217 in_name[0] = 0;
1219 size = sizeof(vin_name);
1220 if(RegQueryValueExW(key, L"DefaultVoiceInput", 0, NULL, (BYTE*)vin_name, &size) != ERROR_SUCCESS)
1221 vin_name[0] = 0;
1223 while(1){
1224 if(RegNotifyChangeKeyValue(key, FALSE, REG_NOTIFY_CHANGE_LAST_SET,
1225 NULL, FALSE) != ERROR_SUCCESS){
1226 ERR("RegNotifyChangeKeyValue failed: %lu\n", GetLastError());
1227 RegCloseKey(key);
1228 g_notif_thread = NULL;
1229 return 1;
1232 EnterCriticalSection(&g_notif_lock);
1234 notify_if_changed(eRender, eConsole, key, L"DefaultOutput",
1235 out_name, &MMDevice_def_play->IMMDevice_iface);
1236 notify_if_changed(eRender, eCommunications, key, L"DefaultVoiceOutput",
1237 vout_name, &MMDevice_def_play->IMMDevice_iface);
1238 notify_if_changed(eCapture, eConsole, key, L"DefaultInput",
1239 in_name, &MMDevice_def_rec->IMMDevice_iface);
1240 notify_if_changed(eCapture, eCommunications, key, L"DefaultVoiceInput",
1241 vin_name, &MMDevice_def_rec->IMMDevice_iface);
1243 LeaveCriticalSection(&g_notif_lock);
1246 RegCloseKey(key);
1248 g_notif_thread = NULL;
1250 return 0;
1253 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(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 wrapper = HeapAlloc(GetProcessHeap(), 0, sizeof(*wrapper));
1264 if(!wrapper)
1265 return E_OUTOFMEMORY;
1267 wrapper->client = client;
1269 EnterCriticalSection(&g_notif_lock);
1271 list_add_tail(&g_notif_clients, &wrapper->entry);
1273 if(!g_notif_thread){
1274 g_notif_thread = CreateThread(NULL, 0, notif_thread_proc, NULL, 0, NULL);
1275 if(!g_notif_thread)
1276 ERR("CreateThread failed: %lu\n", GetLastError());
1279 LeaveCriticalSection(&g_notif_lock);
1281 return S_OK;
1284 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1286 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1287 struct NotificationClientWrapper *wrapper;
1289 TRACE("(%p)->(%p)\n", This, client);
1291 if(!client)
1292 return E_POINTER;
1294 EnterCriticalSection(&g_notif_lock);
1296 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients, struct NotificationClientWrapper, entry){
1297 if(wrapper->client == client){
1298 list_remove(&wrapper->entry);
1299 HeapFree(GetProcessHeap(), 0, wrapper);
1300 LeaveCriticalSection(&g_notif_lock);
1301 return S_OK;
1305 LeaveCriticalSection(&g_notif_lock);
1307 return E_NOTFOUND;
1310 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl =
1312 MMDevEnum_QueryInterface,
1313 MMDevEnum_AddRef,
1314 MMDevEnum_Release,
1315 MMDevEnum_EnumAudioEndpoints,
1316 MMDevEnum_GetDefaultAudioEndpoint,
1317 MMDevEnum_GetDevice,
1318 MMDevEnum_RegisterEndpointNotificationCallback,
1319 MMDevEnum_UnregisterEndpointNotificationCallback
1322 static MMDevEnumImpl enumerator =
1324 {&MMDevEnumVtbl},
1328 static HRESULT MMDevPropStore_Create(MMDevice *parent, DWORD access, IPropertyStore **ppv)
1330 MMDevPropStore *This;
1331 if (access != STGM_READ
1332 && access != STGM_WRITE
1333 && access != STGM_READWRITE)
1335 WARN("Invalid access %08lx\n", access);
1336 return E_INVALIDARG;
1338 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1339 *ppv = &This->IPropertyStore_iface;
1340 if (!This)
1341 return E_OUTOFMEMORY;
1342 This->IPropertyStore_iface.lpVtbl = &MMDevPropVtbl;
1343 This->ref = 1;
1344 This->parent = parent;
1345 This->access = access;
1346 return S_OK;
1349 static void MMDevPropStore_Destroy(MMDevPropStore *This)
1351 HeapFree(GetProcessHeap(), 0, This);
1354 static HRESULT WINAPI MMDevPropStore_QueryInterface(IPropertyStore *iface, REFIID riid, void **ppv)
1356 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1357 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1359 if (!ppv)
1360 return E_POINTER;
1361 if (IsEqualIID(riid, &IID_IUnknown)
1362 || IsEqualIID(riid, &IID_IPropertyStore))
1363 *ppv = &This->IPropertyStore_iface;
1364 else
1365 *ppv = NULL;
1366 if (!*ppv)
1367 return E_NOINTERFACE;
1368 IUnknown_AddRef((IUnknown*)*ppv);
1369 return S_OK;
1372 static ULONG WINAPI MMDevPropStore_AddRef(IPropertyStore *iface)
1374 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1375 LONG ref = InterlockedIncrement(&This->ref);
1376 TRACE("Refcount now %li\n", ref);
1377 return ref;
1380 static ULONG WINAPI MMDevPropStore_Release(IPropertyStore *iface)
1382 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1383 LONG ref = InterlockedDecrement(&This->ref);
1384 TRACE("Refcount now %li\n", ref);
1385 if (!ref)
1386 MMDevPropStore_Destroy(This);
1387 return ref;
1390 static HRESULT WINAPI MMDevPropStore_GetCount(IPropertyStore *iface, DWORD *nprops)
1392 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1393 WCHAR buffer[50];
1394 DWORD i = 0;
1395 HKEY propkey;
1396 HRESULT hr;
1398 TRACE("(%p)->(%p)\n", iface, nprops);
1399 if (!nprops)
1400 return E_POINTER;
1401 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1402 if (FAILED(hr))
1403 return hr;
1404 *nprops = 0;
1405 do {
1406 DWORD len = ARRAY_SIZE(buffer);
1407 if (RegEnumValueW(propkey, i, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
1408 break;
1409 i++;
1410 } while (1);
1411 RegCloseKey(propkey);
1412 TRACE("Returning %li\n", i);
1413 *nprops = i;
1414 return S_OK;
1417 static HRESULT WINAPI MMDevPropStore_GetAt(IPropertyStore *iface, DWORD prop, PROPERTYKEY *key)
1419 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1420 WCHAR buffer[50];
1421 DWORD len = ARRAY_SIZE(buffer);
1422 HRESULT hr;
1423 HKEY propkey;
1425 TRACE("(%p)->(%lu,%p)\n", iface, prop, key);
1426 if (!key)
1427 return E_POINTER;
1429 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1430 if (FAILED(hr))
1431 return hr;
1433 if (RegEnumValueW(propkey, prop, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS
1434 || len <= 39)
1436 WARN("GetAt %lu failed\n", prop);
1437 return E_INVALIDARG;
1439 RegCloseKey(propkey);
1440 buffer[38] = 0;
1441 CLSIDFromString(buffer, &key->fmtid);
1442 key->pid = wcstol(&buffer[39], NULL, 10);
1443 return S_OK;
1446 static HRESULT WINAPI MMDevPropStore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *pv)
1448 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1449 HRESULT hres;
1450 TRACE("(%p)->(\"%s,%lu\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1452 if (!key || !pv)
1453 return E_POINTER;
1454 if (This->access != STGM_READ
1455 && This->access != STGM_READWRITE)
1456 return STG_E_ACCESSDENIED;
1458 /* Special case */
1459 if (IsEqualPropertyKey(*key, PKEY_AudioEndpoint_GUID))
1461 pv->vt = VT_LPWSTR;
1462 pv->pwszVal = CoTaskMemAlloc(39 * sizeof(WCHAR));
1463 if (!pv->pwszVal)
1464 return E_OUTOFMEMORY;
1465 StringFromGUID2(&This->parent->devguid, pv->pwszVal, 39);
1466 return S_OK;
1469 hres = MMDevice_GetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1470 if (FAILED(hres))
1471 return hres;
1473 if (WARN_ON(mmdevapi))
1475 if ((IsEqualPropertyKey(*key, DEVPKEY_Device_FriendlyName) ||
1476 IsEqualPropertyKey(*key, DEVPKEY_DeviceInterface_FriendlyName) ||
1477 IsEqualPropertyKey(*key, DEVPKEY_Device_DeviceDesc)) &&
1478 pv->vt == VT_LPWSTR && wcslen(pv->pwszVal) > 62)
1479 WARN("Returned name exceeds length limit of some broken apps/libs, might crash: %s\n", debugstr_w(pv->pwszVal));
1481 return hres;
1484 static HRESULT WINAPI MMDevPropStore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT pv)
1486 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1487 TRACE("(%p)->(\"%s,%lu\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1489 if (!key || !pv)
1490 return E_POINTER;
1492 if (This->access != STGM_WRITE
1493 && This->access != STGM_READWRITE)
1494 return STG_E_ACCESSDENIED;
1495 return MMDevice_SetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1498 static HRESULT WINAPI MMDevPropStore_Commit(IPropertyStore *iface)
1500 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1501 TRACE("(%p)\n", iface);
1503 if (This->access != STGM_WRITE
1504 && This->access != STGM_READWRITE)
1505 return STG_E_ACCESSDENIED;
1507 /* Does nothing - for mmdevapi, the propstore values are written on SetValue,
1508 * not on Commit. */
1510 return S_OK;
1513 static const IPropertyStoreVtbl MMDevPropVtbl =
1515 MMDevPropStore_QueryInterface,
1516 MMDevPropStore_AddRef,
1517 MMDevPropStore_Release,
1518 MMDevPropStore_GetCount,
1519 MMDevPropStore_GetAt,
1520 MMDevPropStore_GetValue,
1521 MMDevPropStore_SetValue,
1522 MMDevPropStore_Commit
1526 /* Property bag for IBaseFilter activation */
1527 static HRESULT WINAPI PB_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
1529 ERR("Should not be called\n");
1530 *ppv = NULL;
1531 return E_NOINTERFACE;
1534 static ULONG WINAPI PB_AddRef(IPropertyBag *iface)
1536 ERR("Should not be called\n");
1537 return 2;
1540 static ULONG WINAPI PB_Release(IPropertyBag *iface)
1542 ERR("Should not be called\n");
1543 return 1;
1546 static HRESULT WINAPI PB_Read(IPropertyBag *iface, LPCOLESTR name, VARIANT *var, IErrorLog *log)
1548 IPropertyBagImpl *This = impl_from_IPropertyBag(iface);
1549 TRACE("Trying to read %s, type %u\n", debugstr_w(name), var->n1.n2.vt);
1550 if (!lstrcmpW(name, L"DSGuid"))
1552 WCHAR guidstr[39];
1553 StringFromGUID2(&This->devguid, guidstr,ARRAY_SIZE(guidstr));
1554 var->n1.n2.vt = VT_BSTR;
1555 var->n1.n2.n3.bstrVal = SysAllocString(guidstr);
1556 return S_OK;
1558 ERR("Unknown property '%s' queried\n", debugstr_w(name));
1559 return E_FAIL;
1562 static HRESULT WINAPI PB_Write(IPropertyBag *iface, LPCOLESTR name, VARIANT *var)
1564 ERR("Should not be called\n");
1565 return E_FAIL;
1568 static const IPropertyBagVtbl PB_Vtbl =
1570 PB_QueryInterface,
1571 PB_AddRef,
1572 PB_Release,
1573 PB_Read,
1574 PB_Write
1577 static ULONG WINAPI info_device_ps_AddRef(IPropertyStore *iface)
1579 return 2;
1582 static ULONG WINAPI info_device_ps_Release(IPropertyStore *iface)
1584 return 1;
1587 static HRESULT WINAPI info_device_ps_GetValue(IPropertyStore *iface,
1588 REFPROPERTYKEY key, PROPVARIANT *pv)
1590 TRACE("(static)->(\"%s,%lu\", %p)\n", debugstr_guid(&key->fmtid), key ? key->pid : 0, pv);
1592 if (!key || !pv)
1593 return E_POINTER;
1595 if (IsEqualPropertyKey(*key, DEVPKEY_Device_Driver))
1597 INT size = (lstrlenW(drvs.module_name) + 1) * sizeof(WCHAR);
1598 pv->vt = VT_LPWSTR;
1599 pv->pwszVal = CoTaskMemAlloc(size);
1600 if (!pv->pwszVal)
1601 return E_OUTOFMEMORY;
1602 memcpy(pv->pwszVal, drvs.module_name, size);
1603 return S_OK;
1606 return E_INVALIDARG;
1609 static const IPropertyStoreVtbl info_device_ps_Vtbl =
1611 NULL,
1612 info_device_ps_AddRef,
1613 info_device_ps_Release,
1614 NULL,
1615 NULL,
1616 info_device_ps_GetValue,
1617 NULL,
1618 NULL
1621 static IPropertyStore info_device_ps = {
1622 &info_device_ps_Vtbl
1625 static ULONG WINAPI info_device_AddRef(IMMDevice *iface)
1627 return 2;
1630 static ULONG WINAPI info_device_Release(IMMDevice *iface)
1632 return 1;
1635 static HRESULT WINAPI info_device_OpenPropertyStore(IMMDevice *iface,
1636 DWORD access, IPropertyStore **ppv)
1638 TRACE("(static)->(%lx, %p)\n", access, ppv);
1639 *ppv = &info_device_ps;
1640 return S_OK;
1643 static const IMMDeviceVtbl info_device_Vtbl =
1645 NULL,
1646 info_device_AddRef,
1647 info_device_Release,
1648 NULL,
1649 info_device_OpenPropertyStore,
1650 NULL,
1651 NULL
1654 static IMMDevice info_device = {
1655 &info_device_Vtbl