winex11: Remove support for owner-displayed clipboard formats.
[wine.git] / dlls / mmdevapi / main.c
blob110c8028c76f523a607a15bfc3ceeb92c9e74852
1 /*
2 * Copyright 2009 Maarten Lankhorst
3 * Copyright 2011 Andrew Eikum for CodeWeavers
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "config.h"
21 #include "wine/port.h"
23 #include <stdarg.h>
25 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "wine/library.h"
31 #include "ole2.h"
32 #include "olectl.h"
33 #include "rpcproxy.h"
34 #include "propsys.h"
35 #include "propkeydef.h"
36 #include "mmdeviceapi.h"
37 #include "mmsystem.h"
38 #include "dsound.h"
39 #include "audioclient.h"
40 #include "endpointvolume.h"
41 #include "audiopolicy.h"
42 #include "devpkey.h"
43 #include "winreg.h"
45 #include "mmdevapi.h"
46 #include "wine/debug.h"
47 #include "wine/unicode.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
51 static HINSTANCE instance;
53 DriverFuncs drvs;
55 const WCHAR drv_keyW[] = {'S','o','f','t','w','a','r','e','\\',
56 'W','i','n','e','\\','D','r','i','v','e','r','s',0};
58 static const char *get_priority_string(int prio)
60 switch(prio){
61 case Priority_Unavailable:
62 return "Unavailable";
63 case Priority_Low:
64 return "Low";
65 case Priority_Neutral:
66 return "Neutral";
67 case Priority_Preferred:
68 return "Preferred";
70 return "Invalid";
73 static BOOL load_driver(const WCHAR *name, DriverFuncs *driver)
75 WCHAR driver_module[264];
76 static const WCHAR wineW[] = {'w','i','n','e',0};
77 static const WCHAR dotdrvW[] = {'.','d','r','v',0};
79 lstrcpyW(driver_module, wineW);
80 lstrcatW(driver_module, name);
81 lstrcatW(driver_module, dotdrvW);
83 TRACE("Attempting to load %s\n", wine_dbgstr_w(driver_module));
85 driver->module = LoadLibraryW(driver_module);
86 if(!driver->module){
87 TRACE("Unable to load %s: %u\n", wine_dbgstr_w(driver_module),
88 GetLastError());
89 return FALSE;
92 #define LDFC(n) do { driver->p##n = (void*)GetProcAddress(driver->module, #n);\
93 if(!driver->p##n) { FreeLibrary(driver->module); return FALSE; } } while(0)
94 LDFC(GetPriority);
95 LDFC(GetEndpointIDs);
96 LDFC(GetAudioEndpoint);
97 LDFC(GetAudioSessionManager);
98 #undef LDFC
100 /* optional - do not fail if not found */
101 driver->pGetPropValue = (void*)GetProcAddress(driver->module, "GetPropValue");
103 driver->priority = driver->pGetPriority();
104 lstrcpyW(driver->module_name, driver_module);
106 TRACE("Successfully loaded %s with priority %s\n",
107 wine_dbgstr_w(driver_module), get_priority_string(driver->priority));
109 return TRUE;
112 static BOOL init_driver(void)
114 static const WCHAR drv_value[] = {'A','u','d','i','o',0};
116 static WCHAR default_list[] = {'p','u','l','s','e',',','a','l','s','a',',','o','s','s',',',
117 'c','o','r','e','a','u','d','i','o',0};
119 DriverFuncs driver;
120 HKEY key;
121 WCHAR reg_list[256], *p, *next, *driver_list = default_list;
123 if(drvs.module)
124 return TRUE;
126 if(RegOpenKeyW(HKEY_CURRENT_USER, drv_keyW, &key) == ERROR_SUCCESS){
127 DWORD size = sizeof(reg_list);
129 if(RegQueryValueExW(key, drv_value, 0, NULL, (BYTE*)reg_list,
130 &size) == ERROR_SUCCESS){
131 if(reg_list[0] == '\0'){
132 TRACE("User explicitly chose no driver\n");
133 RegCloseKey(key);
134 return TRUE;
137 driver_list = reg_list;
140 RegCloseKey(key);
143 TRACE("Loading driver list %s\n", wine_dbgstr_w(driver_list));
144 for(next = p = driver_list; next; p = next + 1){
145 next = strchrW(p, ',');
146 if(next)
147 *next = '\0';
149 driver.priority = Priority_Unavailable;
150 if(load_driver(p, &driver)){
151 if(driver.priority == Priority_Unavailable)
152 FreeLibrary(driver.module);
153 else if(!drvs.module || driver.priority > drvs.priority){
154 TRACE("Selecting driver %s with priority %s\n",
155 wine_dbgstr_w(p), get_priority_string(driver.priority));
156 if(drvs.module)
157 FreeLibrary(drvs.module);
158 drvs = driver;
159 }else
160 FreeLibrary(driver.module);
161 }else
162 TRACE("Failed to load driver %s\n", wine_dbgstr_w(p));
164 if(next)
165 *next = ',';
168 return drvs.module != 0;
171 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
173 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
175 switch (fdwReason)
177 case DLL_PROCESS_ATTACH:
178 instance = hinstDLL;
179 DisableThreadLibraryCalls(hinstDLL);
180 break;
181 case DLL_PROCESS_DETACH:
182 if(lpvReserved)
183 break;
184 MMDevEnum_Free();
185 break;
188 return TRUE;
191 HRESULT WINAPI DllCanUnloadNow(void)
193 return S_FALSE;
196 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
198 typedef struct {
199 IClassFactory IClassFactory_iface;
200 REFCLSID rclsid;
201 FnCreateInstance pfnCreateInstance;
202 } IClassFactoryImpl;
204 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
206 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
209 static HRESULT WINAPI
210 MMCF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppobj)
212 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
213 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
214 if (ppobj == NULL)
215 return E_POINTER;
216 if (IsEqualIID(riid, &IID_IUnknown) ||
217 IsEqualIID(riid, &IID_IClassFactory))
219 *ppobj = iface;
220 IClassFactory_AddRef(iface);
221 return S_OK;
223 *ppobj = NULL;
224 return E_NOINTERFACE;
227 static ULONG WINAPI MMCF_AddRef(LPCLASSFACTORY iface)
229 return 2;
232 static ULONG WINAPI MMCF_Release(LPCLASSFACTORY iface)
234 /* static class, won't be freed */
235 return 1;
238 static HRESULT WINAPI MMCF_CreateInstance(
239 LPCLASSFACTORY iface,
240 LPUNKNOWN pOuter,
241 REFIID riid,
242 LPVOID *ppobj)
244 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
245 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
247 if (pOuter)
248 return CLASS_E_NOAGGREGATION;
250 if (ppobj == NULL) {
251 WARN("invalid parameter\n");
252 return E_POINTER;
254 *ppobj = NULL;
255 return This->pfnCreateInstance(riid, ppobj);
258 static HRESULT WINAPI MMCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
260 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
261 FIXME("(%p, %d) stub!\n", This, dolock);
262 return S_OK;
265 static const IClassFactoryVtbl MMCF_Vtbl = {
266 MMCF_QueryInterface,
267 MMCF_AddRef,
268 MMCF_Release,
269 MMCF_CreateInstance,
270 MMCF_LockServer
273 static IClassFactoryImpl MMDEVAPI_CF[] = {
274 { { &MMCF_Vtbl }, &CLSID_MMDeviceEnumerator, (FnCreateInstance)MMDevEnum_Create }
277 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
279 unsigned int i = 0;
280 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
282 if(!init_driver()){
283 ERR("Driver initialization failed\n");
284 return E_FAIL;
287 if (ppv == NULL) {
288 WARN("invalid parameter\n");
289 return E_INVALIDARG;
292 *ppv = NULL;
294 if (!IsEqualIID(riid, &IID_IClassFactory) &&
295 !IsEqualIID(riid, &IID_IUnknown)) {
296 WARN("no interface for %s\n", debugstr_guid(riid));
297 return E_NOINTERFACE;
300 for (i = 0; i < sizeof(MMDEVAPI_CF)/sizeof(MMDEVAPI_CF[0]); ++i)
302 if (IsEqualGUID(rclsid, MMDEVAPI_CF[i].rclsid)) {
303 IClassFactory_AddRef(&MMDEVAPI_CF[i].IClassFactory_iface);
304 *ppv = &MMDEVAPI_CF[i];
305 return S_OK;
309 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
310 debugstr_guid(riid), ppv);
311 return CLASS_E_CLASSNOTAVAILABLE;
314 /***********************************************************************
315 * DllRegisterServer (MMDEVAPI.@)
317 HRESULT WINAPI DllRegisterServer(void)
319 return __wine_register_resources( instance );
322 /***********************************************************************
323 * DllUnregisterServer (MMDEVAPI.@)
325 HRESULT WINAPI DllUnregisterServer(void)
327 return __wine_unregister_resources( instance );