gdi32: Move the text metrics cache into the generic font structure.
[wine/zf.git] / dlls / mmdevapi / devenum.c
blob999ec2b0e78be4f32e74c65700fe0c0ad9c5de30
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"
39 #include "mmdevapi.h"
40 #include "devpkey.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
44 static const WCHAR software_mmdevapi[] =
45 { 'S','o','f','t','w','a','r','e','\\',
46 'M','i','c','r','o','s','o','f','t','\\',
47 'W','i','n','d','o','w','s','\\',
48 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
49 'M','M','D','e','v','i','c','e','s','\\',
50 'A','u','d','i','o',0};
51 static const WCHAR reg_render[] =
52 { 'R','e','n','d','e','r',0 };
53 static const WCHAR reg_capture[] =
54 { 'C','a','p','t','u','r','e',0 };
55 static const WCHAR reg_devicestate[] =
56 { 'D','e','v','i','c','e','S','t','a','t','e',0 };
57 static const WCHAR reg_properties[] =
58 { 'P','r','o','p','e','r','t','i','e','s',0 };
59 static const WCHAR slashW[] = {'\\',0};
60 static const WCHAR reg_out_nameW[] = {'D','e','f','a','u','l','t','O','u','t','p','u','t',0};
61 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};
62 static const WCHAR reg_in_nameW[] = {'D','e','f','a','u','l','t','I','n','p','u','t',0};
63 static const WCHAR reg_vin_nameW[] = {'D','e','f','a','u','l','t','V','o','i','c','e','I','n','p','u','t',0};
65 static HKEY key_render;
66 static HKEY key_capture;
68 typedef struct MMDevPropStoreImpl
70 IPropertyStore IPropertyStore_iface;
71 LONG ref;
72 MMDevice *parent;
73 DWORD access;
74 } MMDevPropStore;
76 typedef struct MMDevEnumImpl
78 IMMDeviceEnumerator IMMDeviceEnumerator_iface;
79 LONG ref;
80 } MMDevEnumImpl;
82 static MMDevEnumImpl *MMDevEnumerator;
83 static MMDevice **MMDevice_head;
84 static MMDevice *MMDevice_def_rec, *MMDevice_def_play;
85 static DWORD MMDevice_count;
86 static const IMMDeviceEnumeratorVtbl MMDevEnumVtbl;
87 static const IMMDeviceCollectionVtbl MMDevColVtbl;
88 static const IMMDeviceVtbl MMDeviceVtbl;
89 static const IPropertyStoreVtbl MMDevPropVtbl;
90 static const IMMEndpointVtbl MMEndpointVtbl;
92 static IMMDevice info_device;
94 typedef struct MMDevColImpl
96 IMMDeviceCollection IMMDeviceCollection_iface;
97 LONG ref;
98 EDataFlow flow;
99 DWORD state;
100 } MMDevColImpl;
102 typedef struct IPropertyBagImpl {
103 IPropertyBag IPropertyBag_iface;
104 GUID devguid;
105 } IPropertyBagImpl;
107 static const IPropertyBagVtbl PB_Vtbl;
109 static HRESULT MMDevPropStore_Create(MMDevice *This, DWORD access, IPropertyStore **ppv);
111 static inline MMDevPropStore *impl_from_IPropertyStore(IPropertyStore *iface)
113 return CONTAINING_RECORD(iface, MMDevPropStore, IPropertyStore_iface);
116 static inline MMDevEnumImpl *impl_from_IMMDeviceEnumerator(IMMDeviceEnumerator *iface)
118 return CONTAINING_RECORD(iface, MMDevEnumImpl, IMMDeviceEnumerator_iface);
121 static inline MMDevColImpl *impl_from_IMMDeviceCollection(IMMDeviceCollection *iface)
123 return CONTAINING_RECORD(iface, MMDevColImpl, IMMDeviceCollection_iface);
126 static inline IPropertyBagImpl *impl_from_IPropertyBag(IPropertyBag *iface)
128 return CONTAINING_RECORD(iface, IPropertyBagImpl, IPropertyBag_iface);
131 static const WCHAR propkey_formatW[] = {
132 '{','%','0','8','X','-','%','0','4','X','-',
133 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
134 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
135 '%','0','2','X','%','0','2','X','}',',','%','d',0 };
137 static HRESULT MMDevPropStore_OpenPropKey(const GUID *guid, DWORD flow, HKEY *propkey)
139 WCHAR buffer[39];
140 LONG ret;
141 HKEY key;
142 StringFromGUID2(guid, buffer, 39);
143 if ((ret = RegOpenKeyExW(flow == eRender ? key_render : key_capture, buffer, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, &key)) != ERROR_SUCCESS)
145 WARN("Opening key %s failed with %u\n", debugstr_w(buffer), ret);
146 return E_FAIL;
148 ret = RegOpenKeyExW(key, reg_properties, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, propkey);
149 RegCloseKey(key);
150 if (ret != ERROR_SUCCESS)
152 WARN("Opening key %s failed with %u\n", debugstr_w(reg_properties), ret);
153 return E_FAIL;
155 return S_OK;
158 static HRESULT MMDevice_GetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, PROPVARIANT *pv)
160 WCHAR buffer[80];
161 const GUID *id = &key->fmtid;
162 DWORD type, size;
163 HRESULT hr = S_OK;
164 HKEY regkey;
165 LONG ret;
167 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
168 if (FAILED(hr))
169 return hr;
170 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
171 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
172 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
173 ret = RegGetValueW(regkey, NULL, buffer, RRF_RT_ANY, &type, NULL, &size);
174 if (ret != ERROR_SUCCESS)
176 WARN("Reading %s returned %d\n", debugstr_w(buffer), ret);
177 RegCloseKey(regkey);
178 PropVariantClear(pv);
179 return S_OK;
182 switch (type)
184 case REG_SZ:
186 pv->vt = VT_LPWSTR;
187 pv->u.pwszVal = CoTaskMemAlloc(size);
188 if (!pv->u.pwszVal)
189 hr = E_OUTOFMEMORY;
190 else
191 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_SZ, NULL, (BYTE*)pv->u.pwszVal, &size);
192 break;
194 case REG_DWORD:
196 pv->vt = VT_UI4;
197 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_DWORD, NULL, (BYTE*)&pv->u.ulVal, &size);
198 break;
200 case REG_BINARY:
202 pv->vt = VT_BLOB;
203 pv->u.blob.cbSize = size;
204 pv->u.blob.pBlobData = CoTaskMemAlloc(size);
205 if (!pv->u.blob.pBlobData)
206 hr = E_OUTOFMEMORY;
207 else
208 RegGetValueW(regkey, NULL, buffer, RRF_RT_REG_BINARY, NULL, (BYTE*)pv->u.blob.pBlobData, &size);
209 break;
211 default:
212 ERR("Unknown/unhandled type: %u\n", type);
213 PropVariantClear(pv);
214 break;
216 RegCloseKey(regkey);
217 return hr;
220 static HRESULT MMDevice_SetPropValue(const GUID *devguid, DWORD flow, REFPROPERTYKEY key, REFPROPVARIANT pv)
222 WCHAR buffer[80];
223 const GUID *id = &key->fmtid;
224 HRESULT hr;
225 HKEY regkey;
226 LONG ret;
228 hr = MMDevPropStore_OpenPropKey(devguid, flow, &regkey);
229 if (FAILED(hr))
230 return hr;
231 wsprintfW( buffer, propkey_formatW, id->Data1, id->Data2, id->Data3,
232 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
233 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], key->pid );
234 switch (pv->vt)
236 case VT_UI4:
238 ret = RegSetValueExW(regkey, buffer, 0, REG_DWORD, (const BYTE*)&pv->u.ulVal, sizeof(DWORD));
239 break;
241 case VT_BLOB:
243 ret = RegSetValueExW(regkey, buffer, 0, REG_BINARY, pv->u.blob.pBlobData, pv->u.blob.cbSize);
244 TRACE("Blob %p %u\n", pv->u.blob.pBlobData, pv->u.blob.cbSize);
246 break;
248 case VT_LPWSTR:
250 ret = RegSetValueExW(regkey, buffer, 0, REG_SZ, (const BYTE*)pv->u.pwszVal, sizeof(WCHAR)*(1+lstrlenW(pv->u.pwszVal)));
251 break;
253 default:
254 ret = 0;
255 FIXME("Unhandled type %u\n", pv->vt);
256 hr = E_INVALIDARG;
257 break;
259 RegCloseKey(regkey);
260 TRACE("Writing %s returned %u\n", debugstr_w(buffer), ret);
261 return hr;
264 static HRESULT set_driver_prop_value(GUID *id, const EDataFlow flow, const PROPERTYKEY *prop)
266 HRESULT hr;
267 PROPVARIANT pv;
269 if (!drvs.pGetPropValue)
270 return E_NOTIMPL;
272 hr = drvs.pGetPropValue(id, prop, &pv);
274 if (SUCCEEDED(hr))
276 MMDevice_SetPropValue(id, flow, prop, &pv);
277 PropVariantClear(&pv);
280 return hr;
283 /* Creates or updates the state of a device
284 * If GUID is null, a random guid will be assigned
285 * and the device will be created
287 static MMDevice *MMDevice_Create(WCHAR *name, GUID *id, EDataFlow flow, DWORD state, BOOL setdefault)
289 HKEY key, root;
290 MMDevice *cur = NULL;
291 WCHAR guidstr[39];
292 DWORD i;
294 static const PROPERTYKEY deviceinterface_key = {
295 {0x233164c8, 0x1b2c, 0x4c7d, {0xbc, 0x68, 0xb6, 0x71, 0x68, 0x7a, 0x25, 0x67}}, 1
298 static const PROPERTYKEY devicepath_key = {
299 {0xb3f8fa53, 0x0004, 0x438e, {0x90, 0x03, 0x51, 0xa4, 0x6e, 0x13, 0x9b, 0xfc}}, 2
302 for (i = 0; i < MMDevice_count; ++i)
304 MMDevice *device = MMDevice_head[i];
305 if (device->flow == flow && IsEqualGUID(&device->devguid, id)){
306 cur = device;
307 break;
311 if(!cur){
312 /* No device found, allocate new one */
313 cur = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*cur));
314 if (!cur)
315 return NULL;
317 cur->IMMDevice_iface.lpVtbl = &MMDeviceVtbl;
318 cur->IMMEndpoint_iface.lpVtbl = &MMEndpointVtbl;
320 InitializeCriticalSection(&cur->crst);
321 cur->crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": MMDevice.crst");
323 if (!MMDevice_head)
324 MMDevice_head = HeapAlloc(GetProcessHeap(), 0, sizeof(*MMDevice_head));
325 else
326 MMDevice_head = HeapReAlloc(GetProcessHeap(), 0, MMDevice_head, sizeof(*MMDevice_head)*(1+MMDevice_count));
327 MMDevice_head[MMDevice_count++] = cur;
328 }else if(cur->ref > 0)
329 WARN("Modifying an MMDevice with postitive reference count!\n");
331 HeapFree(GetProcessHeap(), 0, cur->drv_id);
332 cur->drv_id = name;
334 cur->flow = flow;
335 cur->state = state;
336 cur->devguid = *id;
338 StringFromGUID2(&cur->devguid, guidstr, ARRAY_SIZE(guidstr));
340 if (flow == eRender)
341 root = key_render;
342 else
343 root = key_capture;
345 if (RegCreateKeyExW(root, guidstr, 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &key, NULL) == ERROR_SUCCESS)
347 HKEY keyprop;
348 RegSetValueExW(key, reg_devicestate, 0, REG_DWORD, (const BYTE*)&state, sizeof(DWORD));
349 if (!RegCreateKeyExW(key, reg_properties, 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &keyprop, NULL))
351 PROPVARIANT pv;
353 pv.vt = VT_LPWSTR;
354 pv.u.pwszVal = name;
355 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
356 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_DeviceInterface_FriendlyName, &pv);
357 MMDevice_SetPropValue(id, flow, (const PROPERTYKEY*)&DEVPKEY_Device_DeviceDesc, &pv);
359 pv.u.pwszVal = guidstr;
360 MMDevice_SetPropValue(id, flow, &deviceinterface_key, &pv);
362 set_driver_prop_value(id, flow, &devicepath_key);
364 if (FAILED(set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_FormFactor)))
366 pv.vt = VT_UI4;
367 pv.u.ulVal = (flow == eCapture) ? Microphone : Speakers;
369 MMDevice_SetPropValue(id, flow, &PKEY_AudioEndpoint_FormFactor, &pv);
372 if (flow != eCapture)
374 PROPVARIANT pv2;
376 PropVariantInit(&pv2);
378 /* make read-write by not overwriting if already set */
379 if (FAILED(MMDevice_GetPropValue(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers, &pv2)) || pv2.vt != VT_UI4)
380 set_driver_prop_value(id, flow, &PKEY_AudioEndpoint_PhysicalSpeakers);
382 PropVariantClear(&pv2);
385 RegCloseKey(keyprop);
387 RegCloseKey(key);
390 if (setdefault)
392 if (flow == eRender)
393 MMDevice_def_play = cur;
394 else
395 MMDevice_def_rec = cur;
397 return cur;
400 static HRESULT load_devices_from_reg(void)
402 DWORD i = 0;
403 HKEY root, cur;
404 LONG ret;
405 DWORD curflow;
407 ret = RegCreateKeyExW(HKEY_LOCAL_MACHINE, software_mmdevapi, 0, NULL, 0, KEY_WRITE|KEY_READ|KEY_WOW64_64KEY, NULL, &root, NULL);
408 if (ret == ERROR_SUCCESS)
409 ret = RegCreateKeyExW(root, reg_capture, 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_capture, NULL);
410 if (ret == ERROR_SUCCESS)
411 ret = RegCreateKeyExW(root, reg_render, 0, NULL, 0, KEY_READ|KEY_WRITE|KEY_WOW64_64KEY, NULL, &key_render, NULL);
412 RegCloseKey(root);
413 cur = key_capture;
414 curflow = eCapture;
415 if (ret != ERROR_SUCCESS)
417 RegCloseKey(key_capture);
418 key_render = key_capture = NULL;
419 WARN("Couldn't create key: %u\n", ret);
420 return E_FAIL;
423 do {
424 WCHAR guidvalue[39];
425 GUID guid;
426 DWORD len;
427 PROPVARIANT pv = { VT_EMPTY };
429 len = ARRAY_SIZE(guidvalue);
430 ret = RegEnumKeyExW(cur, i++, guidvalue, &len, NULL, NULL, NULL, NULL);
431 if (ret == ERROR_NO_MORE_ITEMS)
433 if (cur == key_capture)
435 cur = key_render;
436 curflow = eRender;
437 i = 0;
438 continue;
440 break;
442 if (ret != ERROR_SUCCESS)
443 continue;
444 if (SUCCEEDED(CLSIDFromString(guidvalue, &guid))
445 && SUCCEEDED(MMDevice_GetPropValue(&guid, curflow, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv))
446 && pv.vt == VT_LPWSTR)
448 DWORD size_bytes = (lstrlenW(pv.u.pwszVal) + 1) * sizeof(WCHAR);
449 WCHAR *name = HeapAlloc(GetProcessHeap(), 0, size_bytes);
450 memcpy(name, pv.u.pwszVal, size_bytes);
451 MMDevice_Create(name, &guid, curflow,
452 DEVICE_STATE_NOTPRESENT, FALSE);
453 CoTaskMemFree(pv.u.pwszVal);
455 } while (1);
457 return S_OK;
460 static HRESULT set_format(MMDevice *dev)
462 HRESULT hr;
463 IAudioClient *client;
464 WAVEFORMATEX *fmt;
465 PROPVARIANT pv = { VT_EMPTY };
467 hr = drvs.pGetAudioEndpoint(&dev->devguid, &dev->IMMDevice_iface, &client);
468 if(FAILED(hr))
469 return hr;
471 hr = IAudioClient_GetMixFormat(client, &fmt);
472 if(FAILED(hr)){
473 IAudioClient_Release(client);
474 return hr;
477 IAudioClient_Release(client);
479 pv.vt = VT_BLOB;
480 pv.u.blob.cbSize = sizeof(WAVEFORMATEX) + fmt->cbSize;
481 pv.u.blob.pBlobData = (BYTE*)fmt;
482 MMDevice_SetPropValue(&dev->devguid, dev->flow,
483 &PKEY_AudioEngine_DeviceFormat, &pv);
484 MMDevice_SetPropValue(&dev->devguid, dev->flow,
485 &PKEY_AudioEngine_OEMFormat, &pv);
486 CoTaskMemFree(fmt);
488 return S_OK;
491 static HRESULT load_driver_devices(EDataFlow flow)
493 WCHAR **ids;
494 GUID *guids;
495 UINT num, def, i;
496 HRESULT hr;
498 if(!drvs.pGetEndpointIDs)
499 return S_OK;
501 hr = drvs.pGetEndpointIDs(flow, &ids, &guids, &num, &def);
502 if(FAILED(hr))
503 return hr;
505 for(i = 0; i < num; ++i){
506 MMDevice *dev;
507 dev = MMDevice_Create(ids[i], &guids[i], flow, DEVICE_STATE_ACTIVE,
508 def == i);
509 set_format(dev);
512 HeapFree(GetProcessHeap(), 0, guids);
513 HeapFree(GetProcessHeap(), 0, ids);
515 return S_OK;
518 static void MMDevice_Destroy(MMDevice *This)
520 DWORD i;
521 TRACE("Freeing %s\n", debugstr_w(This->drv_id));
522 /* Since this function is called at destruction time, reordering of the list is unimportant */
523 for (i = 0; i < MMDevice_count; ++i)
525 if (MMDevice_head[i] == This)
527 MMDevice_head[i] = MMDevice_head[--MMDevice_count];
528 break;
531 This->crst.DebugInfo->Spare[0] = 0;
532 DeleteCriticalSection(&This->crst);
533 HeapFree(GetProcessHeap(), 0, This->drv_id);
534 HeapFree(GetProcessHeap(), 0, This);
537 static inline MMDevice *impl_from_IMMDevice(IMMDevice *iface)
539 return CONTAINING_RECORD(iface, MMDevice, IMMDevice_iface);
542 static HRESULT WINAPI MMDevice_QueryInterface(IMMDevice *iface, REFIID riid, void **ppv)
544 MMDevice *This = impl_from_IMMDevice(iface);
545 TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), ppv);
547 if (!ppv)
548 return E_POINTER;
549 *ppv = NULL;
550 if (IsEqualIID(riid, &IID_IUnknown)
551 || IsEqualIID(riid, &IID_IMMDevice))
552 *ppv = &This->IMMDevice_iface;
553 else if (IsEqualIID(riid, &IID_IMMEndpoint))
554 *ppv = &This->IMMEndpoint_iface;
555 if (*ppv)
557 IUnknown_AddRef((IUnknown*)*ppv);
558 return S_OK;
560 WARN("Unknown interface %s\n", debugstr_guid(riid));
561 return E_NOINTERFACE;
564 static ULONG WINAPI MMDevice_AddRef(IMMDevice *iface)
566 MMDevice *This = impl_from_IMMDevice(iface);
567 LONG ref;
569 ref = InterlockedIncrement(&This->ref);
570 TRACE("Refcount now %i\n", ref);
571 return ref;
574 static ULONG WINAPI MMDevice_Release(IMMDevice *iface)
576 MMDevice *This = impl_from_IMMDevice(iface);
577 LONG ref;
579 ref = InterlockedDecrement(&This->ref);
580 TRACE("Refcount now %i\n", ref);
581 return ref;
584 static HRESULT WINAPI MMDevice_Activate(IMMDevice *iface, REFIID riid, DWORD clsctx, PROPVARIANT *params, void **ppv)
586 HRESULT hr = E_NOINTERFACE;
587 MMDevice *This = impl_from_IMMDevice(iface);
589 TRACE("(%p)->(%s, %x, %p, %p)\n", iface, debugstr_guid(riid), clsctx, params, ppv);
591 if (!ppv)
592 return E_POINTER;
594 if (IsEqualIID(riid, &IID_IAudioClient) ||
595 IsEqualIID(riid, &IID_IAudioClient2) ||
596 IsEqualIID(riid, &IID_IAudioClient3)){
597 hr = drvs.pGetAudioEndpoint(&This->devguid, iface, (IAudioClient**)ppv);
598 }else if (IsEqualIID(riid, &IID_IAudioEndpointVolume) ||
599 IsEqualIID(riid, &IID_IAudioEndpointVolumeEx))
600 hr = AudioEndpointVolume_Create(This, (IAudioEndpointVolumeEx**)ppv);
601 else if (IsEqualIID(riid, &IID_IAudioSessionManager)
602 || IsEqualIID(riid, &IID_IAudioSessionManager2))
604 hr = drvs.pGetAudioSessionManager(iface, (IAudioSessionManager2**)ppv);
606 else if (IsEqualIID(riid, &IID_IBaseFilter))
608 if (This->flow == eRender)
609 hr = CoCreateInstance(&CLSID_DSoundRender, NULL, clsctx, riid, ppv);
610 else
611 ERR("Not supported for recording?\n");
612 if (SUCCEEDED(hr))
614 IPersistPropertyBag *ppb;
615 hr = IUnknown_QueryInterface((IUnknown*)*ppv, &IID_IPersistPropertyBag, (void **)&ppb);
616 if (SUCCEEDED(hr))
618 /* ::Load cannot assume the interface stays alive after the function returns,
619 * so just create the interface on the stack, saves a lot of complicated code */
620 IPropertyBagImpl bag = { { &PB_Vtbl } };
621 bag.devguid = This->devguid;
622 hr = IPersistPropertyBag_Load(ppb, &bag.IPropertyBag_iface, NULL);
623 IPersistPropertyBag_Release(ppb);
624 if (FAILED(hr))
625 IBaseFilter_Release((IBaseFilter*)*ppv);
627 else
629 FIXME("Wine doesn't support IPersistPropertyBag on DSoundRender yet, ignoring..\n");
630 hr = S_OK;
634 else if (IsEqualIID(riid, &IID_IDeviceTopology))
636 FIXME("IID_IDeviceTopology unsupported\n");
638 else if (IsEqualIID(riid, &IID_IDirectSound)
639 || IsEqualIID(riid, &IID_IDirectSound8))
641 if (This->flow == eRender)
642 hr = CoCreateInstance(&CLSID_DirectSound8, NULL, clsctx, riid, ppv);
643 if (SUCCEEDED(hr))
645 hr = IDirectSound_Initialize((IDirectSound*)*ppv, &This->devguid);
646 if (FAILED(hr))
647 IDirectSound_Release((IDirectSound*)*ppv);
650 else if (IsEqualIID(riid, &IID_IDirectSoundCapture))
652 if (This->flow == eCapture)
653 hr = CoCreateInstance(&CLSID_DirectSoundCapture8, NULL, clsctx, riid, ppv);
654 if (SUCCEEDED(hr))
656 hr = IDirectSoundCapture_Initialize((IDirectSoundCapture*)*ppv, &This->devguid);
657 if (FAILED(hr))
658 IDirectSoundCapture_Release((IDirectSoundCapture*)*ppv);
661 else
662 ERR("Invalid/unknown iid %s\n", debugstr_guid(riid));
664 if (FAILED(hr))
665 *ppv = NULL;
667 TRACE("Returning %08x\n", hr);
668 return hr;
671 static HRESULT WINAPI MMDevice_OpenPropertyStore(IMMDevice *iface, DWORD access, IPropertyStore **ppv)
673 MMDevice *This = impl_from_IMMDevice(iface);
674 TRACE("(%p)->(%x,%p)\n", This, access, ppv);
676 if (!ppv)
677 return E_POINTER;
678 return MMDevPropStore_Create(This, access, ppv);
681 static HRESULT WINAPI MMDevice_GetId(IMMDevice *iface, WCHAR **itemid)
683 MMDevice *This = impl_from_IMMDevice(iface);
684 WCHAR *str;
685 GUID *id = &This->devguid;
686 static const WCHAR formatW[] = { '{','0','.','0','.','%','u','.','0','0','0','0','0','0','0','0','}','.',
687 '{','%','0','8','X','-','%','0','4','X','-',
688 '%','0','4','X','-','%','0','2','X','%','0','2','X','-',
689 '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
690 '%','0','2','X','%','0','2','X','}',0 };
692 TRACE("(%p)->(%p)\n", This, itemid);
693 if (!itemid)
694 return E_POINTER;
695 *itemid = str = CoTaskMemAlloc(56 * sizeof(WCHAR));
696 if (!str)
697 return E_OUTOFMEMORY;
698 wsprintfW( str, formatW, This->flow, id->Data1, id->Data2, id->Data3,
699 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
700 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
701 TRACE("returning %s\n", wine_dbgstr_w(str));
702 return S_OK;
705 static HRESULT WINAPI MMDevice_GetState(IMMDevice *iface, DWORD *state)
707 MMDevice *This = impl_from_IMMDevice(iface);
708 TRACE("(%p)->(%p)\n", iface, state);
710 if (!state)
711 return E_POINTER;
712 *state = This->state;
713 return S_OK;
716 static const IMMDeviceVtbl MMDeviceVtbl =
718 MMDevice_QueryInterface,
719 MMDevice_AddRef,
720 MMDevice_Release,
721 MMDevice_Activate,
722 MMDevice_OpenPropertyStore,
723 MMDevice_GetId,
724 MMDevice_GetState
727 static inline MMDevice *impl_from_IMMEndpoint(IMMEndpoint *iface)
729 return CONTAINING_RECORD(iface, MMDevice, IMMEndpoint_iface);
732 static HRESULT WINAPI MMEndpoint_QueryInterface(IMMEndpoint *iface, REFIID riid, void **ppv)
734 MMDevice *This = impl_from_IMMEndpoint(iface);
735 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
736 return IMMDevice_QueryInterface(&This->IMMDevice_iface, riid, ppv);
739 static ULONG WINAPI MMEndpoint_AddRef(IMMEndpoint *iface)
741 MMDevice *This = impl_from_IMMEndpoint(iface);
742 TRACE("(%p)\n", This);
743 return IMMDevice_AddRef(&This->IMMDevice_iface);
746 static ULONG WINAPI MMEndpoint_Release(IMMEndpoint *iface)
748 MMDevice *This = impl_from_IMMEndpoint(iface);
749 TRACE("(%p)\n", This);
750 return IMMDevice_Release(&This->IMMDevice_iface);
753 static HRESULT WINAPI MMEndpoint_GetDataFlow(IMMEndpoint *iface, EDataFlow *flow)
755 MMDevice *This = impl_from_IMMEndpoint(iface);
756 TRACE("(%p)->(%p)\n", This, flow);
757 if (!flow)
758 return E_POINTER;
759 *flow = This->flow;
760 return S_OK;
763 static const IMMEndpointVtbl MMEndpointVtbl =
765 MMEndpoint_QueryInterface,
766 MMEndpoint_AddRef,
767 MMEndpoint_Release,
768 MMEndpoint_GetDataFlow
771 static HRESULT MMDevCol_Create(IMMDeviceCollection **ppv, EDataFlow flow, DWORD state)
773 MMDevColImpl *This;
775 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
776 *ppv = NULL;
777 if (!This)
778 return E_OUTOFMEMORY;
779 This->IMMDeviceCollection_iface.lpVtbl = &MMDevColVtbl;
780 This->ref = 1;
781 This->flow = flow;
782 This->state = state;
783 *ppv = &This->IMMDeviceCollection_iface;
784 return S_OK;
787 static void MMDevCol_Destroy(MMDevColImpl *This)
789 HeapFree(GetProcessHeap(), 0, This);
792 static HRESULT WINAPI MMDevCol_QueryInterface(IMMDeviceCollection *iface, REFIID riid, void **ppv)
794 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
795 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
797 if (!ppv)
798 return E_POINTER;
799 if (IsEqualIID(riid, &IID_IUnknown)
800 || IsEqualIID(riid, &IID_IMMDeviceCollection))
801 *ppv = &This->IMMDeviceCollection_iface;
802 else
803 *ppv = NULL;
804 if (!*ppv)
805 return E_NOINTERFACE;
806 IUnknown_AddRef((IUnknown*)*ppv);
807 return S_OK;
810 static ULONG WINAPI MMDevCol_AddRef(IMMDeviceCollection *iface)
812 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
813 LONG ref = InterlockedIncrement(&This->ref);
814 TRACE("Refcount now %i\n", ref);
815 return ref;
818 static ULONG WINAPI MMDevCol_Release(IMMDeviceCollection *iface)
820 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
821 LONG ref = InterlockedDecrement(&This->ref);
822 TRACE("Refcount now %i\n", ref);
823 if (!ref)
824 MMDevCol_Destroy(This);
825 return ref;
828 static HRESULT WINAPI MMDevCol_GetCount(IMMDeviceCollection *iface, UINT *numdevs)
830 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
831 DWORD i;
833 TRACE("(%p)->(%p)\n", This, numdevs);
834 if (!numdevs)
835 return E_POINTER;
837 *numdevs = 0;
838 for (i = 0; i < MMDevice_count; ++i)
840 MMDevice *cur = MMDevice_head[i];
841 if ((cur->flow == This->flow || This->flow == eAll)
842 && (cur->state & This->state))
843 ++(*numdevs);
845 return S_OK;
848 static HRESULT WINAPI MMDevCol_Item(IMMDeviceCollection *iface, UINT n, IMMDevice **dev)
850 MMDevColImpl *This = impl_from_IMMDeviceCollection(iface);
851 DWORD i = 0, j = 0;
853 TRACE("(%p)->(%u, %p)\n", This, n, dev);
854 if (!dev)
855 return E_POINTER;
857 for (j = 0; j < MMDevice_count; ++j)
859 MMDevice *cur = MMDevice_head[j];
860 if ((cur->flow == This->flow || This->flow == eAll)
861 && (cur->state & This->state)
862 && i++ == n)
864 *dev = &cur->IMMDevice_iface;
865 IMMDevice_AddRef(*dev);
866 return S_OK;
869 WARN("Could not obtain item %u\n", n);
870 *dev = NULL;
871 return E_INVALIDARG;
874 static const IMMDeviceCollectionVtbl MMDevColVtbl =
876 MMDevCol_QueryInterface,
877 MMDevCol_AddRef,
878 MMDevCol_Release,
879 MMDevCol_GetCount,
880 MMDevCol_Item
883 HRESULT MMDevEnum_Create(REFIID riid, void **ppv)
885 MMDevEnumImpl *This = MMDevEnumerator;
887 if (!This)
889 This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
890 *ppv = NULL;
891 if (!This)
892 return E_OUTOFMEMORY;
893 This->ref = 1;
894 This->IMMDeviceEnumerator_iface.lpVtbl = &MMDevEnumVtbl;
895 MMDevEnumerator = This;
897 load_devices_from_reg();
898 load_driver_devices(eRender);
899 load_driver_devices(eCapture);
901 return IMMDeviceEnumerator_QueryInterface(&This->IMMDeviceEnumerator_iface, riid, ppv);
904 void MMDevEnum_Free(void)
906 while (MMDevice_count)
907 MMDevice_Destroy(MMDevice_head[0]);
908 RegCloseKey(key_render);
909 RegCloseKey(key_capture);
910 key_render = key_capture = NULL;
911 HeapFree(GetProcessHeap(), 0, MMDevEnumerator);
912 MMDevEnumerator = NULL;
915 static HRESULT WINAPI MMDevEnum_QueryInterface(IMMDeviceEnumerator *iface, REFIID riid, void **ppv)
917 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
918 TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
920 if (!ppv)
921 return E_POINTER;
922 if (IsEqualIID(riid, &IID_IUnknown)
923 || IsEqualIID(riid, &IID_IMMDeviceEnumerator))
924 *ppv = &This->IMMDeviceEnumerator_iface;
925 else
926 *ppv = NULL;
927 if (!*ppv)
928 return E_NOINTERFACE;
929 IUnknown_AddRef((IUnknown*)*ppv);
930 return S_OK;
933 static ULONG WINAPI MMDevEnum_AddRef(IMMDeviceEnumerator *iface)
935 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
936 LONG ref = InterlockedIncrement(&This->ref);
937 TRACE("Refcount now %i\n", ref);
938 return ref;
941 static ULONG WINAPI MMDevEnum_Release(IMMDeviceEnumerator *iface)
943 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
944 LONG ref = InterlockedDecrement(&This->ref);
945 if (!ref)
946 MMDevEnum_Free();
947 TRACE("Refcount now %i\n", ref);
948 return ref;
951 static HRESULT WINAPI MMDevEnum_EnumAudioEndpoints(IMMDeviceEnumerator *iface, EDataFlow flow, DWORD mask, IMMDeviceCollection **devices)
953 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
954 TRACE("(%p)->(%u,%u,%p)\n", This, flow, mask, devices);
955 if (!devices)
956 return E_POINTER;
957 *devices = NULL;
958 if (flow >= EDataFlow_enum_count)
959 return E_INVALIDARG;
960 if (mask & ~DEVICE_STATEMASK_ALL)
961 return E_INVALIDARG;
962 return MMDevCol_Create(devices, flow, mask);
965 static HRESULT WINAPI MMDevEnum_GetDefaultAudioEndpoint(IMMDeviceEnumerator *iface, EDataFlow flow, ERole role, IMMDevice **device)
967 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
968 WCHAR reg_key[256];
969 HKEY key;
970 HRESULT hr;
972 TRACE("(%p)->(%u,%u,%p)\n", This, flow, role, device);
974 if (!device)
975 return E_POINTER;
977 if((flow != eRender && flow != eCapture) ||
978 (role != eConsole && role != eMultimedia && role != eCommunications)){
979 WARN("Unknown flow (%u) or role (%u)\n", flow, role);
980 return E_INVALIDARG;
983 *device = NULL;
985 if(!drvs.module_name[0])
986 return E_NOTFOUND;
988 lstrcpyW(reg_key, drv_keyW);
989 lstrcatW(reg_key, slashW);
990 lstrcatW(reg_key, drvs.module_name);
992 if(RegOpenKeyW(HKEY_CURRENT_USER, reg_key, &key) == ERROR_SUCCESS){
993 const WCHAR *reg_x_name, *reg_vx_name;
994 WCHAR def_id[256];
995 DWORD size = sizeof(def_id), state;
997 if(flow == eRender){
998 reg_x_name = reg_out_nameW;
999 reg_vx_name = reg_vout_nameW;
1000 }else{
1001 reg_x_name = reg_in_nameW;
1002 reg_vx_name = reg_vin_nameW;
1005 if(role == eCommunications &&
1006 RegQueryValueExW(key, reg_vx_name, 0, NULL,
1007 (BYTE*)def_id, &size) == ERROR_SUCCESS){
1008 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
1009 if(SUCCEEDED(hr)){
1010 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
1011 state == DEVICE_STATE_ACTIVE){
1012 RegCloseKey(key);
1013 return S_OK;
1017 TRACE("Unable to find voice device %s\n", wine_dbgstr_w(def_id));
1020 if(RegQueryValueExW(key, reg_x_name, 0, NULL,
1021 (BYTE*)def_id, &size) == ERROR_SUCCESS){
1022 hr = IMMDeviceEnumerator_GetDevice(iface, def_id, device);
1023 if(SUCCEEDED(hr)){
1024 if(SUCCEEDED(IMMDevice_GetState(*device, &state)) &&
1025 state == DEVICE_STATE_ACTIVE){
1026 RegCloseKey(key);
1027 return S_OK;
1031 TRACE("Unable to find device %s\n", wine_dbgstr_w(def_id));
1034 RegCloseKey(key);
1037 if (flow == eRender)
1038 *device = &MMDevice_def_play->IMMDevice_iface;
1039 else
1040 *device = &MMDevice_def_rec->IMMDevice_iface;
1042 if (!*device)
1043 return E_NOTFOUND;
1044 IMMDevice_AddRef(*device);
1045 return S_OK;
1048 static HRESULT WINAPI MMDevEnum_GetDevice(IMMDeviceEnumerator *iface, const WCHAR *name, IMMDevice **device)
1050 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1051 DWORD i=0;
1052 IMMDevice *dev = NULL;
1054 static const WCHAR wine_info_deviceW[] = {'W','i','n','e',' ',
1055 'i','n','f','o',' ','d','e','v','i','c','e',0};
1057 TRACE("(%p)->(%s,%p)\n", This, debugstr_w(name), device);
1059 if(!name || !device)
1060 return E_POINTER;
1062 if(!lstrcmpW(name, wine_info_deviceW)){
1063 *device = &info_device;
1064 return S_OK;
1067 for (i = 0; i < MMDevice_count; ++i)
1069 HRESULT hr;
1070 WCHAR *str;
1071 dev = &MMDevice_head[i]->IMMDevice_iface;
1072 hr = IMMDevice_GetId(dev, &str);
1073 if (FAILED(hr))
1075 WARN("GetId failed: %08x\n", hr);
1076 continue;
1079 if (str && !lstrcmpW(str, name))
1081 CoTaskMemFree(str);
1082 IMMDevice_AddRef(dev);
1083 *device = dev;
1084 return S_OK;
1086 CoTaskMemFree(str);
1088 TRACE("Could not find device %s\n", debugstr_w(name));
1089 return E_INVALIDARG;
1092 struct NotificationClientWrapper {
1093 IMMNotificationClient *client;
1094 struct list entry;
1097 static struct list g_notif_clients = LIST_INIT(g_notif_clients);
1098 static HANDLE g_notif_thread;
1100 static CRITICAL_SECTION g_notif_lock;
1101 static CRITICAL_SECTION_DEBUG g_notif_lock_debug =
1103 0, 0, &g_notif_lock,
1104 { &g_notif_lock_debug.ProcessLocksList, &g_notif_lock_debug.ProcessLocksList },
1105 0, 0, { (DWORD_PTR)(__FILE__ ": g_notif_lock") }
1107 static CRITICAL_SECTION g_notif_lock = { &g_notif_lock_debug, -1, 0, 0, 0, 0 };
1109 static void notify_clients(EDataFlow flow, ERole role, const WCHAR *id)
1111 struct NotificationClientWrapper *wrapper;
1112 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients,
1113 struct NotificationClientWrapper, entry)
1114 IMMNotificationClient_OnDefaultDeviceChanged(wrapper->client, flow,
1115 role, id);
1117 /* Windows 7 treats changes to eConsole as changes to eMultimedia */
1118 if(role == eConsole)
1119 notify_clients(flow, eMultimedia, id);
1122 static BOOL notify_if_changed(EDataFlow flow, ERole role, HKEY key,
1123 const WCHAR *val_name, WCHAR *old_val, IMMDevice *def_dev)
1125 WCHAR new_val[64], *id;
1126 DWORD size;
1127 HRESULT hr;
1129 size = sizeof(new_val);
1130 if(RegQueryValueExW(key, val_name, 0, NULL,
1131 (BYTE*)new_val, &size) != ERROR_SUCCESS){
1132 if(old_val[0] != 0){
1133 /* set by user -> system default */
1134 if(def_dev){
1135 hr = IMMDevice_GetId(def_dev, &id);
1136 if(FAILED(hr)){
1137 ERR("GetId failed: %08x\n", hr);
1138 return FALSE;
1140 }else
1141 id = NULL;
1143 notify_clients(flow, role, id);
1144 old_val[0] = 0;
1145 CoTaskMemFree(id);
1147 return TRUE;
1150 /* system default -> system default, noop */
1151 return FALSE;
1154 if(!lstrcmpW(old_val, new_val)){
1155 /* set by user -> same value */
1156 return FALSE;
1159 if(new_val[0] != 0){
1160 /* set by user -> different value */
1161 notify_clients(flow, role, new_val);
1162 memcpy(old_val, new_val, sizeof(new_val));
1163 return TRUE;
1166 /* set by user -> system default */
1167 if(def_dev){
1168 hr = IMMDevice_GetId(def_dev, &id);
1169 if(FAILED(hr)){
1170 ERR("GetId failed: %08x\n", hr);
1171 return FALSE;
1173 }else
1174 id = NULL;
1176 notify_clients(flow, role, id);
1177 old_val[0] = 0;
1178 CoTaskMemFree(id);
1180 return TRUE;
1183 static DWORD WINAPI notif_thread_proc(void *user)
1185 HKEY key;
1186 WCHAR reg_key[256];
1187 WCHAR out_name[64], vout_name[64], in_name[64], vin_name[64];
1188 DWORD size;
1190 lstrcpyW(reg_key, drv_keyW);
1191 lstrcatW(reg_key, slashW);
1192 lstrcatW(reg_key, drvs.module_name);
1194 if(RegCreateKeyExW(HKEY_CURRENT_USER, reg_key, 0, NULL, 0,
1195 MAXIMUM_ALLOWED, NULL, &key, NULL) != ERROR_SUCCESS){
1196 ERR("RegCreateKeyEx failed: %u\n", GetLastError());
1197 return 1;
1200 size = sizeof(out_name);
1201 if(RegQueryValueExW(key, reg_out_nameW, 0, NULL,
1202 (BYTE*)out_name, &size) != ERROR_SUCCESS)
1203 out_name[0] = 0;
1205 size = sizeof(vout_name);
1206 if(RegQueryValueExW(key, reg_vout_nameW, 0, NULL,
1207 (BYTE*)vout_name, &size) != ERROR_SUCCESS)
1208 vout_name[0] = 0;
1210 size = sizeof(in_name);
1211 if(RegQueryValueExW(key, reg_in_nameW, 0, NULL,
1212 (BYTE*)in_name, &size) != ERROR_SUCCESS)
1213 in_name[0] = 0;
1215 size = sizeof(vin_name);
1216 if(RegQueryValueExW(key, reg_vin_nameW, 0, NULL,
1217 (BYTE*)vin_name, &size) != ERROR_SUCCESS)
1218 vin_name[0] = 0;
1220 while(1){
1221 if(RegNotifyChangeKeyValue(key, FALSE, REG_NOTIFY_CHANGE_LAST_SET,
1222 NULL, FALSE) != ERROR_SUCCESS){
1223 ERR("RegNotifyChangeKeyValue failed: %u\n", GetLastError());
1224 RegCloseKey(key);
1225 g_notif_thread = NULL;
1226 return 1;
1229 EnterCriticalSection(&g_notif_lock);
1231 notify_if_changed(eRender, eConsole, key, reg_out_nameW,
1232 out_name, &MMDevice_def_play->IMMDevice_iface);
1233 notify_if_changed(eRender, eCommunications, key, reg_vout_nameW,
1234 vout_name, &MMDevice_def_play->IMMDevice_iface);
1235 notify_if_changed(eCapture, eConsole, key, reg_in_nameW,
1236 in_name, &MMDevice_def_rec->IMMDevice_iface);
1237 notify_if_changed(eCapture, eCommunications, key, reg_vin_nameW,
1238 vin_name, &MMDevice_def_rec->IMMDevice_iface);
1240 LeaveCriticalSection(&g_notif_lock);
1243 RegCloseKey(key);
1245 g_notif_thread = NULL;
1247 return 0;
1250 static HRESULT WINAPI MMDevEnum_RegisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1252 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1253 struct NotificationClientWrapper *wrapper;
1255 TRACE("(%p)->(%p)\n", This, client);
1257 if(!client)
1258 return E_POINTER;
1260 wrapper = HeapAlloc(GetProcessHeap(), 0, sizeof(*wrapper));
1261 if(!wrapper)
1262 return E_OUTOFMEMORY;
1264 wrapper->client = client;
1266 EnterCriticalSection(&g_notif_lock);
1268 list_add_tail(&g_notif_clients, &wrapper->entry);
1270 if(!g_notif_thread){
1271 g_notif_thread = CreateThread(NULL, 0, notif_thread_proc, NULL, 0, NULL);
1272 if(!g_notif_thread)
1273 ERR("CreateThread failed: %u\n", GetLastError());
1276 LeaveCriticalSection(&g_notif_lock);
1278 return S_OK;
1281 static HRESULT WINAPI MMDevEnum_UnregisterEndpointNotificationCallback(IMMDeviceEnumerator *iface, IMMNotificationClient *client)
1283 MMDevEnumImpl *This = impl_from_IMMDeviceEnumerator(iface);
1284 struct NotificationClientWrapper *wrapper;
1286 TRACE("(%p)->(%p)\n", This, client);
1288 if(!client)
1289 return E_POINTER;
1291 EnterCriticalSection(&g_notif_lock);
1293 LIST_FOR_EACH_ENTRY(wrapper, &g_notif_clients, 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 = ARRAY_SIZE(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 = ARRAY_SIZE(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 = wcstol(&buffer[39], NULL, 10);
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,ARRAY_SIZE(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