winedevice: Get rid of global driver_hkey variable and fix some leaks.
[wine.git] / dlls / mmdevapi / devenum.c
blobe63a19949c11dbc06ae6f57debd8749fdf9c88d1
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 "config.h"
21 #include <stdarg.h>
23 #define NONAMELESSUNION
24 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 #include "wine/debug.h"
30 #include "wine/list.h"
31 #include "wine/unicode.h"
33 #include "initguid.h"
34 #include "ole2.h"
35 #include "mmdeviceapi.h"
36 #include "dshow.h"
37 #include "dsound.h"
38 #include "audioclient.h"
39 #include "endpointvolume.h"
40 #include "audiopolicy.h"
42 #include "mmdevapi.h"
43 #include "devpkey.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
47 static const WCHAR software_mmdevapi[] =
48 { 'S','o','f','t','w','a','r','e','\\',
49 'M','i','c','r','o','s','o','f','t','\\',
50 'W','i','n','d','o','w','s','\\',
51 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
52 'M','M','D','e','v','i','c','e','s','\\',
53 'A','u','d','i','o',0};
54 static const WCHAR reg_render[] =
55 { 'R','e','n','d','e','r',0 };
56 static const WCHAR reg_capture[] =
57 { 'C','a','p','t','u','r','e',0 };
58 static const WCHAR reg_devicestate[] =
59 { 'D','e','v','i','c','e','S','t','a','t','e',0 };
60 static const WCHAR reg_properties[] =
61 { 'P','r','o','p','e','r','t','i','e','s',0 };
62 static const WCHAR slashW[] = {'\\',0};
63 static const WCHAR reg_out_nameW[] = {'D','e','f','a','u','l','t','O','u','t','p','u','t',0};
64 static const WCHAR reg_vout_nameW[] = {'D','e','f','a','u','l','t','V','o','i','c','e','O','u','t','p','u','t',0};
65 static const WCHAR reg_in_nameW[] = {'D','e','f','a','u','l','t','I','n','p','u','t',0};
66 static const WCHAR reg_vin_nameW[] = {'D','e','f','a','u','l','t','V','o','i','c','e','I','n','p','u','t',0};
68 static HKEY key_render;
69 static HKEY key_capture;
71 typedef struct MMDevPropStoreImpl
73 IPropertyStore IPropertyStore_iface;
74 LONG ref;
75 MMDevice *parent;
76 DWORD access;
77 } MMDevPropStore;
79 typedef struct MMDevEnumImpl
81 IMMDeviceEnumerator IMMDeviceEnumerator_iface;
82 LONG ref;
83 } MMDevEnumImpl;
85 static MMDevEnumImpl *MMDevEnumerator;
86 static MMDevice **MMDevice_head;
87 static MMDevice *MMDevice_def_rec, *MMDevice_def_play;
88 static DWORD MMDevice_count;
89 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl;
90 static const IMMDeviceCollectionVtbl MMDevColVtbl;
91 static const IMMDeviceVtbl MMDeviceVtbl;
92 static const IPropertyStoreVtbl MMDevPropVtbl;
93 static const IMMEndpointVtbl MMEndpointVtbl;
95 static IMMDevice info_device;
97 typedef struct MMDevColImpl
99 IMMDeviceCollection IMMDeviceCollection_iface;
100 LONG ref;
101 EDataFlow flow;
102 DWORD state;
103 } MMDevColImpl;
105 typedef struct IPropertyBagImpl {
106 IPropertyBag IPropertyBag_iface;
107 GUID devguid;
108 } IPropertyBagImpl;
110 static const IPropertyBagVtbl PB_Vtbl;
112 static HRESULT MMDevPropStore_Create(MMDevice *This, DWORD access, IPropertyStore **ppv);
114 static inline MMDevPropStore *impl_from_IPropertyStore(IPropertyStore *iface)
116 return CONTAINING_RECORD(iface, MMDevPropStore, IPropertyStore_iface);
119 static inline MMDevEnumImpl *impl_from_IMMDeviceEnumerator(IMMDeviceEnumerator *iface)
121 return CONTAINING_RECORD(iface, MMDevEnumImpl, IMMDeviceEnumerator_iface);
124 static inline MMDevColImpl *impl_from_IMMDeviceCollection(IMMDeviceCollection *iface)
126 return CONTAINING_RECORD(iface, MMDevColImpl, IMMDeviceCollection_iface);
129 static inline IPropertyBagImpl *impl_from_IPropertyBag(IPropertyBag *iface)
131 return CONTAINING_RECORD(iface, IPropertyBagImpl, IPropertyBag_iface);
134 static const WCHAR propkey_formatW[] = {
135 '{','%','0','8','X','-','%','0','4','X','-',
136 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
137 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
138 '%','0','2','X','%','0','2','X','}',',','%','d',0 };
140 static HRESULT MMDevPropStore_OpenPropKey(const GUID *guid, DWORD flow, HKEY *propkey)
142 WCHAR buffer[39];
143 LONG ret;
144 HKEY key;
145 StringFromGUID2(guid, buffer, 39);
146 if ((ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, buffer, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, &key)) != ERROR_SUCCESS)
148 WARN("Opening key %s failed with %u\n", debugstr_w(buffer), ret);
149 return E_FAIL;
151 ret = RegOpenKeyExW(key, reg_properties, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, propkey);
152 RegCloseKey(key);
153 if (ret != ERROR_SUCCESS)
155 WARN("Opening key %s failed with %u\n", debugstr_w(reg_properties), ret);
156 return E_FAIL;
158 return S_OK;
161 static HRESULT MMDevice_GetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, PROPVARIANT *pv)
163 WCHAR buffer[80];
164 const GUID *id = &key->fmtid;
165 DWORD type, size;
166 HRESULT hr = S_OK;
167 HKEY regkey;
168 LONG ret;
170 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
171 if (FAILED(hr))
172 return hr;
173 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
174 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
175 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
176 ret = RegGetValueW(regkey, NULL, buffer, RRF_RT_ANY, &type, NULL, &size);
177 if (ret != ERROR_SUCCESS)
179 WARN("Reading %s returned %d\n", debugstr_w(buffer), ret);
180 RegCloseKey(regkey);
181 PropVariantClear(pv);
182 return S_OK;
185 switch (type)
187 case REG_SZ:
189 pv->vt = VT_LPWSTR;
190 pv->u.pwszVal = CoTaskMemAlloc(size);
191 if (!pv->u.pwszVal)
192 hr = E_OUTOFMEMORY;
193 else
194 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_SZ, NULL, (BYTE*)pv->u.pwszVal, &size);
195 break;
197 case REG_DWORD:
199 pv->vt = VT_UI4;
200 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_DWORD, NULL, (BYTE*)&pv->u.ulVal, &size);
201 break;
203 case REG_BINARY:
205 pv->vt = VT_BLOB;
206 pv->u.blob.cbSize = size;
207 pv->u.blob.pBlobData = CoTaskMemAlloc(size);
208 if (!pv->u.blob.pBlobData)
209 hr = E_OUTOFMEMORY;
210 else
211 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_BINARY, NULL, (BYTE*)pv->u.blob.pBlobData, &size);
212 break;
214 default:
215 ERR("Unknown/unhandled type: %u\n", type);
216 PropVariantClear(pv);
217 break;
219 RegCloseKey(regkey);
220 return hr;
223 static HRESULT MMDevice_SetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, REFPROPVARIANT pv)
225 WCHAR buffer[80];
226 const GUID *id = &key->fmtid;
227 HRESULT hr;
228 HKEY regkey;
229 LONG ret;
231 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
232 if (FAILED(hr))
233 return hr;
234 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
235 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
236 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
237 switch (pv->vt)
239 case VT_UI4:
241 ret = RegSetValueExW(regkey, buffer, 0, REG_DWORD, (const BYTE*)&pv->u.ulVal, sizeof(DWORD));
242 break;
244 case VT_BLOB:
246 ret = RegSetValueExW(regkey, buffer, 0, REG_BINARY, pv->u.blob.pBlobData, pv->u.blob.cbSize);
247 TRACE("Blob %p %u\n", pv->u.blob.pBlobData, pv->u.blob.cbSize);
249 break;
251 case VT_LPWSTR:
253 ret = RegSetValueExW(regkey, buffer, 0, REG_SZ, (const BYTE*)pv->u.pwszVal, sizeof(WCHAR)*(1+lstrlenW(pv->u.pwszVal)));
254 break;
256 default:
257 ret = 0;
258 FIXME("Unhandled type %u\n", pv->vt);
259 hr = E_INVALIDARG;
260 break;
262 RegCloseKey(regkey);
263 TRACE("Writing %s returned %u\n", debugstr_w(buffer), ret);
264 return hr;
267 static HRESULT set_driver_prop_value(GUID *id, const EDataFlow flow, const PROPERTYKEY *prop)
269 HRESULT hr;
270 PROPVARIANT pv;
272 if (!drvs.pGetPropValue)
273 return E_NOTIMPL;
275 hr = drvs.pGetPropValue(id, prop, &pv);
277 if (SUCCEEDED(hr))
279 MMDevice_SetPropValue(id, flow, prop, &pv);
280 PropVariantClear(&pv);
283 return hr;
286 /* Creates or updates the state of a device
287 * If GUID is null, a random guid will be assigned
288 * and the device will be created
290 static MMDevice *MMDevice_Create(WCHAR *name, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault)
292 HKEY key, root;
293 MMDevice *cur = NULL;
294 WCHAR guidstr[39];
295 DWORD i;
297 static const PROPERTYKEY deviceinterface_key = {
298 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
301 static const PROPERTYKEY devicepath_key = {
302 {0xb3f8fa53, 0x0004, 0x438e, {0x90, 0x03, 0x51, 0xa4, 0x6e, 0x13, 0x9b, 0xfc}}, 2
305 for (i = 0; i < MMDevice_count; ++i)
307 MMDevice *device = MMDevice_head[i];
308 if (device->flow == flow && IsEqualGUID(&device->devguid, id)){
309 cur = device;
310 break;
314 if(!cur){
315 /* No device found, allocate new one */
316 cur = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cur));
317 if (!cur)
318 return NULL;
320 cur->IMMDevice_iface.lpVtbl = &MMDeviceVtbl;
321 cur->IMMEndpoint_iface.lpVtbl = &MMEndpointVtbl;
323 InitializeCriticalSection(&cur->crst);
324 cur->crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MMDevice.crst");
326 if (!MMDevice_head)
327 MMDevice_head = HeapAlloc(GetProcessHeap(), 0, sizeof(*MMDevice_head));
328 else
329 MMDevice_head = HeapReAlloc(GetProcessHeap(), 0, MMDevice_head, sizeof(*MMDevice_head)*(1+MMDevice_count));
330 MMDevice_head[MMDevice_count++] = cur;
331 }else if(cur->ref > 0)
332 WARN("Modifying an MMDevice with postitive reference count!\n");
334 HeapFree(GetProcessHeap(), 0, cur->drv_id);
335 cur->drv_id = name;
337 cur->flow = flow;
338 cur->state = state;
339 cur->devguid = *id;
341 StringFromGUID2(&cur->devguid, guidstr, sizeof(guidstr)/sizeof(*guidstr));
343 if (flow == eRender)
344 root = key_render;
345 else
346 root = key_capture;
348 if (RegCreateKeyExW(root, guidstr, 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &key, NULL) == ERROR_SUCCESS)
350 HKEY keyprop;
351 RegSetValueExW(key, reg_devicestate, 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
352 if (!RegCreateKeyExW(key, reg_properties, 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &keyprop, NULL))
354 PROPVARIANT pv;
356 pv.vt = VT_LPWSTR;
357 pv.u.pwszVal = name;
358 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
359 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_DeviceDesc, &pv);
361 pv.u.pwszVal = guidstr;
362 MMDevice_SetPropValue(id, flow, &deviceinterface_key, &pv);
364 set_driver_prop_value(id, flow, &devicepath_key);
366 if (FAILED(set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_FormFactor)))
368 pv.vt = VT_UI4;
369 pv.u.ulVal = (flow == eCapture) ? Microphone : Speakers;
371 MMDevice_SetPropValue(id, flow, &PKEY_AudioEndpoint_FormFactor, &pv);
374 if (flow != eCapture)
376 PROPVARIANT pv2;
378 PropVariantInit(&pv2);
380 /* make read-write by not overwriting if already set */
381 if (FAILED(MMDevice_GetPropValue(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers, &pv2)) || pv2.vt != VT_UI4)
382 set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers);
384 PropVariantClear(&pv2);
387 RegCloseKey(keyprop);
389 RegCloseKey(key);
392 if (setdefault)
394 if (flow == eRender)
395 MMDevice_def_play = cur;
396 else
397 MMDevice_def_rec = cur;
399 return cur;
402 static HRESULT load_devices_from_reg(void)
404 DWORD i = 0;
405 HKEY root, cur;
406 LONG ret;
407 DWORD curflow;
409 ret = RegCreateKeyExW(HKEY_LOCAL_MACHINE, software_mmdevapi, 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &root, NULL);
410 if (ret == ERROR_SUCCESS)
411 ret = RegCreateKeyExW(root, reg_capture, 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_capture, NULL);
412 if (ret == ERROR_SUCCESS)
413 ret = RegCreateKeyExW(root, reg_render, 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_render, NULL);
414 RegCloseKey(root);
415 cur = key_capture;
416 curflow = eCapture;
417 if (ret != ERROR_SUCCESS)
419 RegCloseKey(key_capture);
420 key_render = key_capture = NULL;
421 WARN("Couldn't create key: %u\n", ret);
422 return E_FAIL;
425 do {
426 WCHAR guidvalue[39];
427 GUID guid;
428 DWORD len;
429 PROPVARIANT pv = { VT_EMPTY };
431 len = sizeof(guidvalue)/sizeof(guidvalue[0]);
432 ret = RegEnumKeyExW(cur, i++, guidvalue, &len, NULL, NULL, NULL, NULL);
433 if (ret == ERROR_NO_MORE_ITEMS)
435 if (cur == key_capture)
437 cur = key_render;
438 curflow = eRender;
439 i = 0;
440 continue;
442 break;
444 if (ret != ERROR_SUCCESS)
445 continue;
446 if (SUCCEEDED(CLSIDFromString(guidvalue, &guid))
447 && SUCCEEDED(MMDevice_GetPropValue(&guid, curflow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv))
448 && pv.vt == VT_LPWSTR)
450 DWORD size_bytes = (strlenW(pv.u.pwszVal) + 1) * sizeof(WCHAR);
451 WCHAR *name = HeapAlloc(GetProcessHeap(), 0, size_bytes);
452 memcpy(name, pv.u.pwszVal, size_bytes);
453 MMDevice_Create(name, &guid, curflow,
454 DEVICE_STATE_NOTPRESENT, FALSE);
455 CoTaskMemFree(pv.u.pwszVal);
457 } while (1);
459 return S_OK;
462 static HRESULT set_format(MMDevice *dev)
464 HRESULT hr;
465 IAudioClient *client;
466 WAVEFORMATEX *fmt;
467 PROPVARIANT pv = { VT_EMPTY };
469 hr = drvs.pGetAudioEndpoint(&dev->devguid, &dev->IMMDevice_iface, &client);
470 if(FAILED(hr))
471 return hr;
473 hr = IAudioClient_GetMixFormat(client, &fmt);
474 if(FAILED(hr)){
475 IAudioClient_Release(client);
476 return hr;
479 IAudioClient_Release(client);
481 pv.vt = VT_BLOB;
482 pv.u.blob.cbSize = sizeof(WAVEFORMATEX) + fmt->cbSize;
483 pv.u.blob.pBlobData = (BYTE*)fmt;
484 MMDevice_SetPropValue(&dev->devguid, dev->flow,
485 &PKEY_AudioEngine_DeviceFormat, &pv);
486 MMDevice_SetPropValue(&dev->devguid, dev->flow,
487 &PKEY_AudioEngine_OEMFormat, &pv);
488 CoTaskMemFree(fmt);
490 return S_OK;
493 static HRESULT load_driver_devices(EDataFlow flow)
495 WCHAR **ids;
496 GUID *guids;
497 UINT num, def, i;
498 HRESULT hr;
500 if(!drvs.pGetEndpointIDs)
501 return S_OK;
503 hr = drvs.pGetEndpointIDs(flow, &ids, &guids, &num, &def);
504 if(FAILED(hr))
505 return hr;
507 for(i = 0; i < num; ++i){
508 MMDevice *dev;
509 dev = MMDevice_Create(ids[i], &guids[i], flow, DEVICE_STATE_ACTIVE,
510 def == i);
511 set_format(dev);
514 HeapFree(GetProcessHeap(), 0, guids);
515 HeapFree(GetProcessHeap(), 0, ids);
517 return S_OK;
520 static void MMDevice_Destroy(MMDevice *This)
522 DWORD i;
523 TRACE("Freeing %s\n", debugstr_w(This->drv_id));
524 /* Since this function is called at destruction time, reordering of the list is unimportant */
525 for (i = 0; i < MMDevice_count; ++i)
527 if (MMDevice_head[i] == This)
529 MMDevice_head[i] = MMDevice_head[--MMDevice_count];
530 break;
533 This->crst.DebugInfo->Spare[0] = 0;
534 DeleteCriticalSection(&This->crst);
535 HeapFree(GetProcessHeap(), 0, This->drv_id);
536 HeapFree(GetProcessHeap(), 0, This);
539 static inline MMDevice *impl_from_IMMDevice(IMMDevice *iface)
541 return CONTAINING_RECORD(iface, MMDevice, IMMDevice_iface);
544 static HRESULT WINAPI MMDevice_QueryInterface(IMMDevice *iface, REFIID riid, void **ppv)
546 MMDevice *This = impl_from_IMMDevice(iface);
547 TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ppv);
549 if (!ppv)
550 return E_POINTER;
551 *ppv = NULL;
552 if (IsEqualIID(riid, &IID_IUnknown)
553 || IsEqualIID(riid, &IID_IMMDevice))
554 *ppv = &This->IMMDevice_iface;
555 else if (IsEqualIID(riid, &IID_IMMEndpoint))
556 *ppv = &This->IMMEndpoint_iface;
557 if (*ppv)
559 IUnknown_AddRef((IUnknown*)*ppv);
560 return S_OK;
562 WARN("Unknown interface %s\n", debugstr_guid(riid));
563 return E_NOINTERFACE;
566 static ULONG WINAPI MMDevice_AddRef(IMMDevice *iface)
568 MMDevice *This = impl_from_IMMDevice(iface);
569 LONG ref;
571 ref = InterlockedIncrement(&This->ref);
572 TRACE("Refcount now %i\n", ref);
573 return ref;
576 static ULONG WINAPI MMDevice_Release(IMMDevice *iface)
578 MMDevice *This = impl_from_IMMDevice(iface);
579 LONG ref;
581 ref = InterlockedDecrement(&This->ref);
582 TRACE("Refcount now %i\n", ref);
583 return ref;
586 static HRESULT WINAPI MMDevice_Activate(IMMDevice *iface, REFIID riid, DWORD clsctx, PROPVARIANT *params, void **ppv)
588 HRESULT hr = E_NOINTERFACE;
589 MMDevice *This = impl_from_IMMDevice(iface);
591 TRACE("(%p)->(%p,%x,%p,%p)\n", iface, riid, clsctx, params, ppv);
593 if (!ppv)
594 return E_POINTER;
596 if (IsEqualIID(riid, &IID_IAudioClient)){
597 hr = drvs.pGetAudioEndpoint(&This->devguid, iface, (IAudioClient**)ppv);
598 }else if (IsEqualIID(riid, &IID_IAudioEndpointVolume))
599 hr = AudioEndpointVolume_Create(This, (IAudioEndpointVolume**)ppv);
600 else if (IsEqualIID(riid, &IID_IAudioSessionManager)
601 || IsEqualIID(riid, &IID_IAudioSessionManager2))
603 hr = drvs.pGetAudioSessionManager(iface, (IAudioSessionManager2**)ppv);
605 else if (IsEqualIID(riid, &IID_IBaseFilter))
607 if (This->flow == eRender)
608 hr = CoCreateInstance(&CLSID_DSoundRender, NULL, clsctx, riid, ppv);
609 else
610 ERR("Not supported for recording?\n");
611 if (SUCCEEDED(hr))
613 IPersistPropertyBag *ppb;
614 hr = IUnknown_QueryInterface((IUnknown*)*ppv, &IID_IPersistPropertyBag, (void*)&ppb);
615 if (SUCCEEDED(hr))
617 /* ::Load cannot assume the interface stays alive after the function returns,
618 * so just create the interface on the stack, saves a lot of complicated code */
619 IPropertyBagImpl bag = { { &PB_Vtbl } };
620 bag.devguid = This->devguid;
621 hr = IPersistPropertyBag_Load(ppb, &bag.IPropertyBag_iface, NULL);
622 IPersistPropertyBag_Release(ppb);
623 if (FAILED(hr))
624 IBaseFilter_Release((IBaseFilter*)*ppv);
626 else
628 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
629 hr = S_OK;
633 else if (IsEqualIID(riid, &IID_IDeviceTopology))
635 FIXME("IID_IDeviceTopology unsupported\n");
637 else if (IsEqualIID(riid, &IID_IDirectSound)
638 || IsEqualIID(riid, &IID_IDirectSound8))
640 if (This->flow == eRender)
641 hr = CoCreateInstance(&CLSID_DirectSound8, NULL, clsctx, riid, ppv);
642 if (SUCCEEDED(hr))
644 hr = IDirectSound_Initialize((IDirectSound*)*ppv, &This->devguid);
645 if (FAILED(hr))
646 IDirectSound_Release((IDirectSound*)*ppv);
649 else if (IsEqualIID(riid, &IID_IDirectSoundCapture))
651 if (This->flow == eCapture)
652 hr = CoCreateInstance(&CLSID_DirectSoundCapture8, NULL, clsctx, riid, ppv);
653 if (SUCCEEDED(hr))
655 hr = IDirectSoundCapture_Initialize((IDirectSoundCapture*)*ppv, &This->devguid);
656 if (FAILED(hr))
657 IDirectSoundCapture_Release((IDirectSoundCapture*)*ppv);
660 else
661 ERR("Invalid/unknown iid %s\n", debugstr_guid(riid));
663 if (FAILED(hr))
664 *ppv = NULL;
666 TRACE("Returning %08x\n", hr);
667 return hr;
670 static HRESULT WINAPI MMDevice_OpenPropertyStore(IMMDevice *iface, DWORD access, IPropertyStore **ppv)
672 MMDevice *This = impl_from_IMMDevice(iface);
673 TRACE("(%p)->(%x,%p)\n", This, access, ppv);
675 if (!ppv)
676 return E_POINTER;
677 return MMDevPropStore_Create(This, access, ppv);
680 static HRESULT WINAPI MMDevice_GetId(IMMDevice *iface, WCHAR **itemid)
682 MMDevice *This = impl_from_IMMDevice(iface);
683 WCHAR *str;
684 GUID *id = &This->devguid;
685 static const WCHAR formatW[] = { '{','0','.','0','.','%','u','.','0','0','0','0','0','0','0','0','}','.',
686 '{','%','0','8','X','-','%','0','4','X','-',
687 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
688 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
689 '%','0','2','X','%','0','2','X','}',0 };
691 TRACE("(%p)->(%p)\n", This, itemid);
692 if (!itemid)
693 return E_POINTER;
694 *itemid = str = CoTaskMemAlloc(56 * sizeof(WCHAR));
695 if (!str)
696 return E_OUTOFMEMORY;
697 wsprintfW( str, formatW, This->flow, id->Data1, id->Data2, id->Data3,
698 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
699 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
700 TRACE("returning %s\n", wine_dbgstr_w(str));
701 return S_OK;
704 static HRESULT WINAPI MMDevice_GetState(IMMDevice *iface, DWORD *state)
706 MMDevice *This = impl_from_IMMDevice(iface);
707 TRACE("(%p)->(%p)\n", iface, state);
709 if (!state)
710 return E_POINTER;
711 *state = This->state;
712 return S_OK;
715 static const IMMDeviceVtbl MMDeviceVtbl =
717 MMDevice_QueryInterface,
718 MMDevice_AddRef,
719 MMDevice_Release,
720 MMDevice_Activate,
721 MMDevice_OpenPropertyStore,
722 MMDevice_GetId,
723 MMDevice_GetState
726 static inline MMDevice *impl_from_IMMEndpoint(IMMEndpoint *iface)
728 return CONTAINING_RECORD(iface, MMDevice, IMMEndpoint_iface);
731 static HRESULT WINAPI MMEndpoint_QueryInterface(IMMEndpoint *iface, REFIID riid, void **ppv)
733 MMDevice *This = impl_from_IMMEndpoint(iface);
734 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
735 return IMMDevice_QueryInterface(&This->IMMDevice_iface, riid, ppv);
738 static ULONG WINAPI MMEndpoint_AddRef(IMMEndpoint *iface)
740 MMDevice *This = impl_from_IMMEndpoint(iface);
741 TRACE("(%p)\n", This);
742 return IMMDevice_AddRef(&This->IMMDevice_iface);
745 static ULONG WINAPI MMEndpoint_Release(IMMEndpoint *iface)
747 MMDevice *This = impl_from_IMMEndpoint(iface);
748 TRACE("(%p)\n", This);
749 return IMMDevice_Release(&This->IMMDevice_iface);
752 static HRESULT WINAPI MMEndpoint_GetDataFlow(IMMEndpoint *iface, EDataFlow *flow)
754 MMDevice *This = impl_from_IMMEndpoint(iface);
755 TRACE("(%p)->(%p)\n", This, flow);
756 if (!flow)
757 return E_POINTER;
758 *flow = This->flow;
759 return S_OK;
762 static const IMMEndpointVtbl MMEndpointVtbl =
764 MMEndpoint_QueryInterface,
765 MMEndpoint_AddRef,
766 MMEndpoint_Release,
767 MMEndpoint_GetDataFlow
770 static HRESULT MMDevCol_Create(IMMDeviceCollection **ppv, EDataFlow flow, DWORD state)
772 MMDevColImpl *This;
774 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
775 *ppv = NULL;
776 if (!This)
777 return E_OUTOFMEMORY;
778 This->IMMDeviceCollection_iface.lpVtbl = &MMDevColVtbl;
779 This->ref = 1;
780 This->flow = flow;
781 This->state = state;
782 *ppv = &This->IMMDeviceCollection_iface;
783 return S_OK;
786 static void MMDevCol_Destroy(MMDevColImpl *This)
788 HeapFree(GetProcessHeap(), 0, This);
791 static HRESULT WINAPI MMDevCol_QueryInterface(IMMDeviceCollection *iface, REFIID riid, void **ppv)
793 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
794 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
796 if (!ppv)
797 return E_POINTER;
798 if (IsEqualIID(riid, &IID_IUnknown)
799 || IsEqualIID(riid, &IID_IMMDeviceCollection))
800 *ppv = &This->IMMDeviceCollection_iface;
801 else
802 *ppv = NULL;
803 if (!*ppv)
804 return E_NOINTERFACE;
805 IUnknown_AddRef((IUnknown*)*ppv);
806 return S_OK;
809 static ULONG WINAPI MMDevCol_AddRef(IMMDeviceCollection *iface)
811 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
812 LONG ref = InterlockedIncrement(&This->ref);
813 TRACE("Refcount now %i\n", ref);
814 return ref;
817 static ULONG WINAPI MMDevCol_Release(IMMDeviceCollection *iface)
819 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
820 LONG ref = InterlockedDecrement(&This->ref);
821 TRACE("Refcount now %i\n", ref);
822 if (!ref)
823 MMDevCol_Destroy(This);
824 return ref;
827 static HRESULT WINAPI MMDevCol_GetCount(IMMDeviceCollection *iface, UINT *numdevs)
829 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
830 DWORD i;
832 TRACE("(%p)->(%p)\n", This, numdevs);
833 if (!numdevs)
834 return E_POINTER;
836 *numdevs = 0;
837 for (i = 0; i < MMDevice_count; ++i)
839 MMDevice *cur = MMDevice_head[i];
840 if ((cur->flow == This->flow || This->flow == eAll)
841 && (cur->state & This->state))
842 ++(*numdevs);
844 return S_OK;
847 static HRESULT WINAPI MMDevCol_Item(IMMDeviceCollection *iface, UINT n, IMMDevice **dev)
849 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
850 DWORD i = 0, j = 0;
852 TRACE("(%p)->(%u, %p)\n", This, n, dev);
853 if (!dev)
854 return E_POINTER;
856 for (j = 0; j < MMDevice_count; ++j)
858 MMDevice *cur = MMDevice_head[j];
859 if ((cur->flow == This->flow || This->flow == eAll)
860 && (cur->state & This->state)
861 && i++ == n)
863 *dev = &cur->IMMDevice_iface;
864 IMMDevice_AddRef(*dev);
865 return S_OK;
868 WARN("Could not obtain item %u\n", n);
869 *dev = NULL;
870 return E_INVALIDARG;
873 static const IMMDeviceCollectionVtbl MMDevColVtbl =
875 MMDevCol_QueryInterface,
876 MMDevCol_AddRef,
877 MMDevCol_Release,
878 MMDevCol_GetCount,
879 MMDevCol_Item
882 HRESULT MMDevEnum_Create(REFIID riid, void **ppv)
884 MMDevEnumImpl *This = MMDevEnumerator;
886 if (!This)
888 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
889 *ppv = NULL;
890 if (!This)
891 return E_OUTOFMEMORY;
892 This->ref = 1;
893 This->IMMDeviceEnumerator_iface.lpVtbl = &MMDevEnumVtbl;
894 MMDevEnumerator = This;
896 load_devices_from_reg();
897 load_driver_devices(eRender);
898 load_driver_devices(eCapture);
900 return IMMDeviceEnumerator_QueryInterface(&This->IMMDeviceEnumerator_iface, riid, ppv);
903 void MMDevEnum_Free(void)
905 while (MMDevice_count)
906 MMDevice_Destroy(MMDevice_head[0]);
907 RegCloseKey(key_render);
908 RegCloseKey(key_capture);
909 key_render = key_capture = NULL;
910 HeapFree(GetProcessHeap(), 0, MMDevEnumerator);
911 MMDevEnumerator = NULL;
914 static HRESULT WINAPI MMDevEnum_QueryInterface(IMMDeviceEnumerator *iface, REFIID riid, void **ppv)
916 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
917 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
919 if (!ppv)
920 return E_POINTER;
921 if (IsEqualIID(riid, &IID_IUnknown)
922 || IsEqualIID(riid, &IID_IMMDeviceEnumerator))
923 *ppv = &This->IMMDeviceEnumerator_iface;
924 else
925 *ppv = NULL;
926 if (!*ppv)
927 return E_NOINTERFACE;
928 IUnknown_AddRef((IUnknown*)*ppv);
929 return S_OK;
932 static ULONG WINAPI MMDevEnum_AddRef(IMMDeviceEnumerator *iface)
934 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
935 LONG ref = InterlockedIncrement(&This->ref);
936 TRACE("Refcount now %i\n", ref);
937 return ref;
940 static ULONG WINAPI MMDevEnum_Release(IMMDeviceEnumerator *iface)
942 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
943 LONG ref = InterlockedDecrement(&This->ref);
944 if (!ref)
945 MMDevEnum_Free();
946 TRACE("Refcount now %i\n", ref);
947 return ref;
950 static HRESULT WINAPI MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator *iface, EDataFlow flow, DWORD mask, IMMDeviceCollection **devices)
952 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
953 TRACE("(%p)->(%u,%u,%p)\n", This, flow, mask, devices);
954 if (!devices)
955 return E_POINTER;
956 *devices = NULL;
957 if (flow >= EDataFlow_enum_count)
958 return E_INVALIDARG;
959 if (mask & ~DEVICE_STATEMASK_ALL)
960 return E_INVALIDARG;
961 return MMDevCol_Create(devices, flow, mask);
964 static HRESULT WINAPI MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator *iface, EDataFlow flow, ERole role, IMMDevice **device)
966 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
967 WCHAR reg_key[256];
968 HKEY key;
969 HRESULT hr;
971 TRACE("(%p)->(%u,%u,%p)\n", This, flow, role, device);
973 if (!device)
974 return E_POINTER;
976 if((flow != eRender && flow != eCapture) ||
977 (role != eConsole && role != eMultimedia && role != eCommunications)){
978 WARN("Unknown flow (%u) or role (%u)\n", flow, role);
979 return E_INVALIDARG;
982 *device = NULL;
984 if(!drvs.module_name[0])
985 return E_NOTFOUND;
987 lstrcpyW(reg_key, drv_keyW);
988 lstrcatW(reg_key, slashW);
989 lstrcatW(reg_key, drvs.module_name);
991 if(RegOpenKeyW(HKEY_CURRENT_USER, reg_key, &key) == ERROR_SUCCESS){
992 const WCHAR *reg_x_name, *reg_vx_name;
993 WCHAR def_id[256];
994 DWORD size = sizeof(def_id), state;
996 if(flow == eRender){
997 reg_x_name = reg_out_nameW;
998 reg_vx_name = reg_vout_nameW;
999 }else{
1000 reg_x_name = reg_in_nameW;
1001 reg_vx_name = reg_vin_nameW;
1004 if(role == eCommunications &&
1005 RegQueryValueExW(key, reg_vx_name, 0, NULL,
1006 (BYTE*)def_id, &size) == ERROR_SUCCESS){
1007 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
1008 if(SUCCEEDED(hr)){
1009 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
1010 state == DEVICE_STATE_ACTIVE){
1011 RegCloseKey(key);
1012 return S_OK;
1016 TRACE("Unable to find voice device %s\n", wine_dbgstr_w(def_id));
1019 if(RegQueryValueExW(key, reg_x_name, 0, NULL,
1020 (BYTE*)def_id, &size) == ERROR_SUCCESS){
1021 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
1022 if(SUCCEEDED(hr)){
1023 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
1024 state == DEVICE_STATE_ACTIVE){
1025 RegCloseKey(key);
1026 return S_OK;
1030 TRACE("Unable to find device %s\n", wine_dbgstr_w(def_id));
1033 RegCloseKey(key);
1036 if (flow == eRender)
1037 *device = &MMDevice_def_play->IMMDevice_iface;
1038 else
1039 *device = &MMDevice_def_rec->IMMDevice_iface;
1041 if (!*device)
1042 return E_NOTFOUND;
1043 IMMDevice_AddRef(*device);
1044 return S_OK;
1047 static HRESULT WINAPI MMDevEnum_GetDevice(IMMDeviceEnumerator *iface, const WCHAR *name, IMMDevice **device)
1049 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1050 DWORD i=0;
1051 IMMDevice *dev = NULL;
1053 static const WCHAR wine_info_deviceW[] = {'W','i','n','e',' ',
1054 'i','n','f','o',' ','d','e','v','i','c','e',0};
1056 TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), device);
1058 if(!name || !device)
1059 return E_POINTER;
1061 if(!lstrcmpW(name, wine_info_deviceW)){
1062 *device = &info_device;
1063 return S_OK;
1066 for (i = 0; i < MMDevice_count; ++i)
1068 HRESULT hr;
1069 WCHAR *str;
1070 dev = &MMDevice_head[i]->IMMDevice_iface;
1071 hr = IMMDevice_GetId(dev, &str);
1072 if (FAILED(hr))
1074 WARN("GetId failed: %08x\n", hr);
1075 continue;
1078 if (str && !lstrcmpW(str, name))
1080 CoTaskMemFree(str);
1081 IMMDevice_AddRef(dev);
1082 *device = dev;
1083 return S_OK;
1085 CoTaskMemFree(str);
1087 TRACE("Could not find device %s\n", debugstr_w(name));
1088 return E_INVALIDARG;
1091 struct NotificationClientWrapper {
1092 IMMNotificationClient *client;
1093 struct list entry;
1096 static struct list g_notif_clients = LIST_INIT(g_notif_clients);
1097 static HANDLE g_notif_thread;
1099 static CRITICAL_SECTION g_notif_lock;
1100 static CRITICAL_SECTION_DEBUG g_notif_lock_debug =
1102 0, 0, &g_notif_lock,
1103 { &g_notif_lock_debug.ProcessLocksList, &g_notif_lock_debug.ProcessLocksList },
1104 0, 0, { (DWORD_PTR)(__FILE__ ": g_notif_lock") }
1106 static CRITICAL_SECTION g_notif_lock = { &g_notif_lock_debug, -1, 0, 0, 0, 0 };
1108 static void notify_clients(EDataFlow flow, ERole role, const WCHAR *id)
1110 struct NotificationClientWrapper *wrapper;
1111 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients,
1112 struct NotificationClientWrapper, entry)
1113 IMMNotificationClient_OnDefaultDeviceChanged(wrapper->client, flow,
1114 role, id);
1116 /* Windows 7 treats changes to eConsole as changes to eMultimedia */
1117 if(role == eConsole)
1118 notify_clients(flow, eMultimedia, id);
1121 static BOOL notify_if_changed(EDataFlow flow, ERole role, HKEY key,
1122 const WCHAR *val_name, WCHAR *old_val, IMMDevice *def_dev)
1124 WCHAR new_val[64], *id;
1125 DWORD size;
1126 HRESULT hr;
1128 size = sizeof(new_val);
1129 if(RegQueryValueExW(key, val_name, 0, NULL,
1130 (BYTE*)new_val, &size) != ERROR_SUCCESS){
1131 if(old_val[0] != 0){
1132 /* set by user -> system default */
1133 if(def_dev){
1134 hr = IMMDevice_GetId(def_dev, &id);
1135 if(FAILED(hr)){
1136 ERR("GetId failed: %08x\n", hr);
1137 return FALSE;
1139 }else
1140 id = NULL;
1142 notify_clients(flow, role, id);
1143 old_val[0] = 0;
1144 CoTaskMemFree(id);
1146 return TRUE;
1149 /* system default -> system default, noop */
1150 return FALSE;
1153 if(!lstrcmpW(old_val, new_val)){
1154 /* set by user -> same value */
1155 return FALSE;
1158 if(new_val[0] != 0){
1159 /* set by user -> different value */
1160 notify_clients(flow, role, new_val);
1161 memcpy(old_val, new_val, sizeof(new_val));
1162 return TRUE;
1165 /* set by user -> system default */
1166 if(def_dev){
1167 hr = IMMDevice_GetId(def_dev, &id);
1168 if(FAILED(hr)){
1169 ERR("GetId failed: %08x\n", hr);
1170 return FALSE;
1172 }else
1173 id = NULL;
1175 notify_clients(flow, role, id);
1176 old_val[0] = 0;
1177 CoTaskMemFree(id);
1179 return TRUE;
1182 static DWORD WINAPI notif_thread_proc(void *user)
1184 HKEY key;
1185 WCHAR reg_key[256];
1186 WCHAR out_name[64], vout_name[64], in_name[64], vin_name[64];
1187 DWORD size;
1189 lstrcpyW(reg_key, drv_keyW);
1190 lstrcatW(reg_key, slashW);
1191 lstrcatW(reg_key, drvs.module_name);
1193 if(RegCreateKeyExW(HKEY_CURRENT_USER, reg_key, 0, NULL, 0,
1194 MAXIMUM_ALLOWED, NULL, &key, NULL) != ERROR_SUCCESS){
1195 ERR("RegCreateKeyEx failed: %u\n", GetLastError());
1196 return 1;
1199 size = sizeof(out_name);
1200 if(RegQueryValueExW(key, reg_out_nameW, 0, NULL,
1201 (BYTE*)out_name, &size) != ERROR_SUCCESS)
1202 out_name[0] = 0;
1204 size = sizeof(vout_name);
1205 if(RegQueryValueExW(key, reg_vout_nameW, 0, NULL,
1206 (BYTE*)vout_name, &size) != ERROR_SUCCESS)
1207 vout_name[0] = 0;
1209 size = sizeof(in_name);
1210 if(RegQueryValueExW(key, reg_in_nameW, 0, NULL,
1211 (BYTE*)in_name, &size) != ERROR_SUCCESS)
1212 in_name[0] = 0;
1214 size = sizeof(vin_name);
1215 if(RegQueryValueExW(key, reg_vin_nameW, 0, NULL,
1216 (BYTE*)vin_name, &size) != ERROR_SUCCESS)
1217 vin_name[0] = 0;
1219 while(1){
1220 if(RegNotifyChangeKeyValue(key, FALSE, REG_NOTIFY_CHANGE_LAST_SET,
1221 NULL, FALSE) != ERROR_SUCCESS){
1222 ERR("RegNotifyChangeKeyValue failed: %u\n", GetLastError());
1223 RegCloseKey(key);
1224 g_notif_thread = NULL;
1225 return 1;
1228 EnterCriticalSection(&g_notif_lock);
1230 notify_if_changed(eRender, eConsole, key, reg_out_nameW,
1231 out_name, &MMDevice_def_play->IMMDevice_iface);
1232 notify_if_changed(eRender, eCommunications, key, reg_vout_nameW,
1233 vout_name, &MMDevice_def_play->IMMDevice_iface);
1234 notify_if_changed(eCapture, eConsole, key, reg_in_nameW,
1235 in_name, &MMDevice_def_rec->IMMDevice_iface);
1236 notify_if_changed(eCapture, eCommunications, key, reg_vin_nameW,
1237 vin_name, &MMDevice_def_rec->IMMDevice_iface);
1239 LeaveCriticalSection(&g_notif_lock);
1242 RegCloseKey(key);
1244 g_notif_thread = NULL;
1246 return 0;
1249 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1251 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1252 struct NotificationClientWrapper *wrapper;
1254 TRACE("(%p)->(%p)\n", This, client);
1256 if(!client)
1257 return E_POINTER;
1259 wrapper = HeapAlloc(GetProcessHeap(), 0, sizeof(*wrapper));
1260 if(!wrapper)
1261 return E_OUTOFMEMORY;
1263 wrapper->client = client;
1265 EnterCriticalSection(&g_notif_lock);
1267 list_add_tail(&g_notif_clients, &wrapper->entry);
1269 if(!g_notif_thread){
1270 g_notif_thread = CreateThread(NULL, 0, notif_thread_proc, NULL, 0, NULL);
1271 if(!g_notif_thread)
1272 ERR("CreateThread failed: %u\n", GetLastError());
1275 LeaveCriticalSection(&g_notif_lock);
1277 return S_OK;
1280 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1282 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1283 struct NotificationClientWrapper *wrapper, *wrapper2;
1285 TRACE("(%p)->(%p)\n", This, client);
1287 if(!client)
1288 return E_POINTER;
1290 EnterCriticalSection(&g_notif_lock);
1292 LIST_FOR_EACH_ENTRY_SAFE(wrapper, wrapper2, &g_notif_clients,
1293 struct NotificationClientWrapper, entry){
1294 if(wrapper->client == client){
1295 list_remove(&wrapper->entry);
1296 HeapFree(GetProcessHeap(), 0, wrapper);
1297 LeaveCriticalSection(&g_notif_lock);
1298 return S_OK;
1302 LeaveCriticalSection(&g_notif_lock);
1304 return E_NOTFOUND;
1307 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl =
1309 MMDevEnum_QueryInterface,
1310 MMDevEnum_AddRef,
1311 MMDevEnum_Release,
1312 MMDevEnum_EnumAudioEndpoints,
1313 MMDevEnum_GetDefaultAudioEndpoint,
1314 MMDevEnum_GetDevice,
1315 MMDevEnum_RegisterEndpointNotificationCallback,
1316 MMDevEnum_UnregisterEndpointNotificationCallback
1319 static HRESULT MMDevPropStore_Create(MMDevice *parent, DWORD access, IPropertyStore **ppv)
1321 MMDevPropStore *This;
1322 if (access != STGM_READ
1323 && access != STGM_WRITE
1324 && access != STGM_READWRITE)
1326 WARN("Invalid access %08x\n", access);
1327 return E_INVALIDARG;
1329 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
1330 *ppv = &This->IPropertyStore_iface;
1331 if (!This)
1332 return E_OUTOFMEMORY;
1333 This->IPropertyStore_iface.lpVtbl = &MMDevPropVtbl;
1334 This->ref = 1;
1335 This->parent = parent;
1336 This->access = access;
1337 return S_OK;
1340 static void MMDevPropStore_Destroy(MMDevPropStore *This)
1342 HeapFree(GetProcessHeap(), 0, This);
1345 static HRESULT WINAPI MMDevPropStore_QueryInterface(IPropertyStore *iface, REFIID riid, void **ppv)
1347 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1348 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
1350 if (!ppv)
1351 return E_POINTER;
1352 if (IsEqualIID(riid, &IID_IUnknown)
1353 || IsEqualIID(riid, &IID_IPropertyStore))
1354 *ppv = &This->IPropertyStore_iface;
1355 else
1356 *ppv = NULL;
1357 if (!*ppv)
1358 return E_NOINTERFACE;
1359 IUnknown_AddRef((IUnknown*)*ppv);
1360 return S_OK;
1363 static ULONG WINAPI MMDevPropStore_AddRef(IPropertyStore *iface)
1365 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1366 LONG ref = InterlockedIncrement(&This->ref);
1367 TRACE("Refcount now %i\n", ref);
1368 return ref;
1371 static ULONG WINAPI MMDevPropStore_Release(IPropertyStore *iface)
1373 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1374 LONG ref = InterlockedDecrement(&This->ref);
1375 TRACE("Refcount now %i\n", ref);
1376 if (!ref)
1377 MMDevPropStore_Destroy(This);
1378 return ref;
1381 static HRESULT WINAPI MMDevPropStore_GetCount(IPropertyStore *iface, DWORD *nprops)
1383 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1384 WCHAR buffer[50];
1385 DWORD i = 0;
1386 HKEY propkey;
1387 HRESULT hr;
1389 TRACE("(%p)->(%p)\n", iface, nprops);
1390 if (!nprops)
1391 return E_POINTER;
1392 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1393 if (FAILED(hr))
1394 return hr;
1395 *nprops = 0;
1396 do {
1397 DWORD len = sizeof(buffer)/sizeof(*buffer);
1398 if (RegEnumValueW(propkey, i, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
1399 break;
1400 i++;
1401 } while (1);
1402 RegCloseKey(propkey);
1403 TRACE("Returning %i\n", i);
1404 *nprops = i;
1405 return S_OK;
1408 static HRESULT WINAPI MMDevPropStore_GetAt(IPropertyStore *iface, DWORD prop, PROPERTYKEY *key)
1410 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1411 WCHAR buffer[50];
1412 DWORD len = sizeof(buffer)/sizeof(*buffer);
1413 HRESULT hr;
1414 HKEY propkey;
1416 TRACE("(%p)->(%u,%p)\n", iface, prop, key);
1417 if (!key)
1418 return E_POINTER;
1420 hr = MMDevPropStore_OpenPropKey(&This->parent->devguid, This->parent->flow, &propkey);
1421 if (FAILED(hr))
1422 return hr;
1424 if (RegEnumValueW(propkey, prop, buffer, &len, NULL, NULL, NULL, NULL) != ERROR_SUCCESS
1425 || len <= 39)
1427 WARN("GetAt %u failed\n", prop);
1428 return E_INVALIDARG;
1430 RegCloseKey(propkey);
1431 buffer[38] = 0;
1432 CLSIDFromString(buffer, &key->fmtid);
1433 key->pid = atoiW(&buffer[39]);
1434 return S_OK;
1437 static HRESULT WINAPI MMDevPropStore_GetValue(IPropertyStore *iface, REFPROPERTYKEY key, PROPVARIANT *pv)
1439 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1440 TRACE("(%p)->(\"%s,%u\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1442 if (!key || !pv)
1443 return E_POINTER;
1444 if (This->access != STGM_READ
1445 && This->access != STGM_READWRITE)
1446 return STG_E_ACCESSDENIED;
1448 /* Special case */
1449 if (IsEqualPropertyKey(*key, PKEY_AudioEndpoint_GUID))
1451 pv->vt = VT_LPWSTR;
1452 pv->u.pwszVal = CoTaskMemAlloc(39 * sizeof(WCHAR));
1453 if (!pv->u.pwszVal)
1454 return E_OUTOFMEMORY;
1455 StringFromGUID2(&This->parent->devguid, pv->u.pwszVal, 39);
1456 return S_OK;
1459 return MMDevice_GetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1462 static HRESULT WINAPI MMDevPropStore_SetValue(IPropertyStore *iface, REFPROPERTYKEY key, REFPROPVARIANT pv)
1464 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1465 TRACE("(%p)->(\"%s,%u\", %p)\n", This, key ? debugstr_guid(&key->fmtid) : NULL, key ? key->pid : 0, pv);
1467 if (!key || !pv)
1468 return E_POINTER;
1470 if (This->access != STGM_WRITE
1471 && This->access != STGM_READWRITE)
1472 return STG_E_ACCESSDENIED;
1473 return MMDevice_SetPropValue(&This->parent->devguid, This->parent->flow, key, pv);
1476 static HRESULT WINAPI MMDevPropStore_Commit(IPropertyStore *iface)
1478 MMDevPropStore *This = impl_from_IPropertyStore(iface);
1479 TRACE("(%p)\n", iface);
1481 if (This->access != STGM_WRITE
1482 && This->access != STGM_READWRITE)
1483 return STG_E_ACCESSDENIED;
1485 /* Does nothing - for mmdevapi, the propstore values are written on SetValue,
1486 * not on Commit. */
1488 return S_OK;
1491 static const IPropertyStoreVtbl MMDevPropVtbl =
1493 MMDevPropStore_QueryInterface,
1494 MMDevPropStore_AddRef,
1495 MMDevPropStore_Release,
1496 MMDevPropStore_GetCount,
1497 MMDevPropStore_GetAt,
1498 MMDevPropStore_GetValue,
1499 MMDevPropStore_SetValue,
1500 MMDevPropStore_Commit
1504 /* Property bag for IBaseFilter activation */
1505 static HRESULT WINAPI PB_QueryInterface(IPropertyBag *iface, REFIID riid, void **ppv)
1507 ERR("Should not be called\n");
1508 *ppv = NULL;
1509 return E_NOINTERFACE;
1512 static ULONG WINAPI PB_AddRef(IPropertyBag *iface)
1514 ERR("Should not be called\n");
1515 return 2;
1518 static ULONG WINAPI PB_Release(IPropertyBag *iface)
1520 ERR("Should not be called\n");
1521 return 1;
1524 static HRESULT WINAPI PB_Read(IPropertyBag *iface, LPCOLESTR name, VARIANT *var, IErrorLog *log)
1526 static const WCHAR dsguid[] = { 'D','S','G','u','i','d', 0 };
1527 IPropertyBagImpl *This = impl_from_IPropertyBag(iface);
1528 TRACE("Trying to read %s, type %u\n", debugstr_w(name), var->n1.n2.vt);
1529 if (!lstrcmpW(name, dsguid))
1531 WCHAR guidstr[39];
1532 StringFromGUID2(&This->devguid, guidstr,sizeof(guidstr)/sizeof(*guidstr));
1533 var->n1.n2.vt = VT_BSTR;
1534 var->n1.n2.n3.bstrVal = SysAllocString(guidstr);
1535 return S_OK;
1537 ERR("Unknown property '%s' queried\n", debugstr_w(name));
1538 return E_FAIL;
1541 static HRESULT WINAPI PB_Write(IPropertyBag *iface, LPCOLESTR name, VARIANT *var)
1543 ERR("Should not be called\n");
1544 return E_FAIL;
1547 static const IPropertyBagVtbl PB_Vtbl =
1549 PB_QueryInterface,
1550 PB_AddRef,
1551 PB_Release,
1552 PB_Read,
1553 PB_Write
1556 static ULONG WINAPI info_device_ps_AddRef(IPropertyStore *iface)
1558 return 2;
1561 static ULONG WINAPI info_device_ps_Release(IPropertyStore *iface)
1563 return 1;
1566 static HRESULT WINAPI info_device_ps_GetValue(IPropertyStore *iface,
1567 REFPROPERTYKEY key, PROPVARIANT *pv)
1569 TRACE("(static)->(\"%s,%u\", %p)\n", debugstr_guid(&key->fmtid), key ? key->pid : 0, pv);
1571 if (!key || !pv)
1572 return E_POINTER;
1574 if (IsEqualPropertyKey(*key, DEVPKEY_Device_Driver))
1576 INT size = (lstrlenW(drvs.module_name) + 1) * sizeof(WCHAR);
1577 pv->vt = VT_LPWSTR;
1578 pv->u.pwszVal = CoTaskMemAlloc(size);
1579 if (!pv->u.pwszVal)
1580 return E_OUTOFMEMORY;
1581 memcpy(pv->u.pwszVal, drvs.module_name, size);
1582 return S_OK;
1585 return E_INVALIDARG;
1588 static const IPropertyStoreVtbl info_device_ps_Vtbl =
1590 NULL,
1591 info_device_ps_AddRef,
1592 info_device_ps_Release,
1593 NULL,
1594 NULL,
1595 info_device_ps_GetValue,
1596 NULL,
1597 NULL
1600 static IPropertyStore info_device_ps = {
1601 &info_device_ps_Vtbl
1604 static ULONG WINAPI info_device_AddRef(IMMDevice *iface)
1606 return 2;
1609 static ULONG WINAPI info_device_Release(IMMDevice *iface)
1611 return 1;
1614 static HRESULT WINAPI info_device_OpenPropertyStore(IMMDevice *iface,
1615 DWORD access, IPropertyStore **ppv)
1617 TRACE("(static)->(%x, %p)\n", access, ppv);
1618 *ppv = &info_device_ps;
1619 return S_OK;
1622 static const IMMDeviceVtbl info_device_Vtbl =
1624 NULL,
1625 info_device_AddRef,
1626 info_device_Release,
1627 NULL,
1628 info_device_OpenPropertyStore,
1629 NULL,
1630 NULL
1633 static IMMDevice info_device = {
1634 &info_device_Vtbl