avifil32: The stream format string cannot really be translated so remove it from...
[wine/gsoc-2012-control.git] / dlls / mmdevapi / main.c
blobc694c6e1d9b2c2c84e254aa83c4bd914a7138b21
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 "initguid.h"
36 #include "propkeydef.h"
37 #include "mmdeviceapi.h"
38 #include "dshow.h"
39 #include "dsound.h"
40 #include "audioclient.h"
41 #include "endpointvolume.h"
42 #include "audiopolicy.h"
43 #include "devpkey.h"
44 #include "winreg.h"
46 #include "mmdevapi.h"
47 #include "wine/debug.h"
48 #include "wine/unicode.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
52 static HINSTANCE instance;
54 DriverFuncs drvs;
56 static BOOL load_driver(const WCHAR *name)
58 WCHAR driver_module[264];
59 static const WCHAR wineW[] = {'w','i','n','e',0};
60 static const WCHAR dotdrvW[] = {'.','d','r','v',0};
62 lstrcpyW(driver_module, wineW);
63 lstrcatW(driver_module, name);
64 lstrcatW(driver_module, dotdrvW);
66 TRACE("Attempting to load %s\n", wine_dbgstr_w(driver_module));
68 drvs.module = LoadLibraryW(driver_module);
69 if(!drvs.module){
70 TRACE("Unable to load %s: %u\n", wine_dbgstr_w(driver_module),
71 GetLastError());
72 return FALSE;
75 #define LDFC(n) do { drvs.p##n = (void*)GetProcAddress(drvs.module, #n);\
76 if(!drvs.p##n) { FreeLibrary(drvs.module); return FALSE; } } while(0)
77 LDFC(GetEndpointIDs);
78 LDFC(GetAudioEndpoint);
79 LDFC(GetAudioSessionManager);
80 #undef LDFC
82 TRACE("Successfully loaded %s\n", wine_dbgstr_w(driver_module));
84 return TRUE;
87 static BOOL init_driver(void)
89 static const WCHAR alsaW[] = {'a','l','s','a',0};
90 static const WCHAR ossW[] = {'o','s','s',0};
91 static const WCHAR coreaudioW[] = {'c','o','r','e','a','u','d','i','o',0};
92 static const WCHAR *default_drivers[] = { alsaW, coreaudioW, ossW };
93 static const WCHAR drv_key[] = {'S','o','f','t','w','a','r','e','\\',
94 'W','i','n','e','\\','D','r','i','v','e','r','s',0};
95 static const WCHAR drv_value[] = {'A','u','d','i','o',0};
96 HKEY key;
97 UINT i;
99 if(drvs.module)
100 return TRUE;
102 if(RegOpenKeyW(HKEY_CURRENT_USER, drv_key, &key) == ERROR_SUCCESS){
103 WCHAR driver_name[256], *p, *next;
104 DWORD size = sizeof(driver_name);
106 if(RegQueryValueExW(key, drv_value, 0, NULL, (BYTE*)driver_name,
107 &size) == ERROR_SUCCESS){
108 RegCloseKey(key);
110 if(driver_name[0] == '\0')
111 return TRUE;
113 for(next = p = driver_name; next; p = next + 1){
114 next = strchrW(p, ',');
115 if(next)
116 *next = '\0';
118 if(load_driver(p))
119 return TRUE;
121 TRACE("Failed to load driver: %s\n", wine_dbgstr_w(driver_name));
124 ERR("No drivers in the registry loaded successfully!\n");
125 return FALSE;
128 RegCloseKey(key);
131 for(i = 0; i < sizeof(default_drivers)/sizeof(*default_drivers); ++i)
132 if(load_driver(default_drivers[i]))
133 return TRUE;
135 return FALSE;
138 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
140 TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
142 switch (fdwReason)
144 case DLL_PROCESS_ATTACH:
145 instance = hinstDLL;
146 DisableThreadLibraryCalls(hinstDLL);
147 break;
148 case DLL_PROCESS_DETACH:
149 MMDevEnum_Free();
150 break;
153 return TRUE;
156 HRESULT WINAPI DllCanUnloadNow(void)
158 return S_FALSE;
161 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
163 typedef struct {
164 IClassFactory IClassFactory_iface;
165 REFCLSID rclsid;
166 FnCreateInstance pfnCreateInstance;
167 } IClassFactoryImpl;
169 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
171 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
174 static HRESULT WINAPI
175 MMCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
177 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
178 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
179 if (ppobj == NULL)
180 return E_POINTER;
181 if (IsEqualIID(riid, &IID_IUnknown) ||
182 IsEqualIID(riid, &IID_IClassFactory))
184 *ppobj = iface;
185 IUnknown_AddRef(iface);
186 return S_OK;
188 *ppobj = NULL;
189 return E_NOINTERFACE;
192 static ULONG WINAPI MMCF_AddRef(LPCLASSFACTORY iface)
194 return 2;
197 static ULONG WINAPI MMCF_Release(LPCLASSFACTORY iface)
199 /* static class, won't be freed */
200 return 1;
203 static HRESULT WINAPI MMCF_CreateInstance(
204 LPCLASSFACTORY iface,
205 LPUNKNOWN pOuter,
206 REFIID riid,
207 LPVOID *ppobj)
209 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
210 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
212 if (pOuter)
213 return CLASS_E_NOAGGREGATION;
215 if (ppobj == NULL) {
216 WARN("invalid parameter\n");
217 return E_POINTER;
219 *ppobj = NULL;
220 return This->pfnCreateInstance(riid, ppobj);
223 static HRESULT WINAPI MMCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
225 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
226 FIXME("(%p, %d) stub!\n", This, dolock);
227 return S_OK;
230 static const IClassFactoryVtbl MMCF_Vtbl = {
231 MMCF_QueryInterface,
232 MMCF_AddRef,
233 MMCF_Release,
234 MMCF_CreateInstance,
235 MMCF_LockServer
238 static IClassFactoryImpl MMDEVAPI_CF[] = {
239 { { &MMCF_Vtbl }, &CLSID_MMDeviceEnumerator, (FnCreateInstance)MMDevEnum_Create }
242 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
244 int i = 0;
245 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
247 if(!init_driver()){
248 ERR("Driver initialization failed\n");
249 return E_FAIL;
252 if (ppv == NULL) {
253 WARN("invalid parameter\n");
254 return E_INVALIDARG;
257 *ppv = NULL;
259 if (!IsEqualIID(riid, &IID_IClassFactory) &&
260 !IsEqualIID(riid, &IID_IUnknown)) {
261 WARN("no interface for %s\n", debugstr_guid(riid));
262 return E_NOINTERFACE;
265 for (i = 0; i < sizeof(MMDEVAPI_CF)/sizeof(MMDEVAPI_CF[0]); ++i)
267 if (IsEqualGUID(rclsid, MMDEVAPI_CF[i].rclsid)) {
268 IUnknown_AddRef(&MMDEVAPI_CF[i].IClassFactory_iface);
269 *ppv = &MMDEVAPI_CF[i];
270 return S_OK;
272 i++;
275 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
276 debugstr_guid(riid), ppv);
277 return CLASS_E_CLASSNOTAVAILABLE;
280 /***********************************************************************
281 * DllRegisterServer (MMDEVAPI.@)
283 HRESULT WINAPI DllRegisterServer(void)
285 return __wine_register_resources( instance );
288 /***********************************************************************
289 * DllUnregisterServer (MMDEVAPI.@)
291 HRESULT WINAPI DllUnregisterServer(void)
293 return __wine_unregister_resources( instance );