msvcp60: Synchronize the spec file.
[wine] / dlls / mmdevapi / main.c
1 /*
2  * Copyright 2009 Maarten Lankhorst
3  * Copyright 2011 Andrew Eikum for CodeWeavers
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <stdarg.h>
24
25 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "wine/library.h"
30
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"
45
46 #include "mmdevapi.h"
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
50
51 static HINSTANCE instance;
52
53 DriverFuncs drvs;
54
55 static BOOL load_driver(const WCHAR *name)
56 {
57     WCHAR driver_module[264];
58     static const WCHAR wineW[] = {'w','i','n','e',0};
59     static const WCHAR dotdrvW[] = {'.','d','r','v',0};
60
61     lstrcpyW(driver_module, wineW);
62     lstrcatW(driver_module, name);
63     lstrcatW(driver_module, dotdrvW);
64
65     TRACE("Attempting to load %s\n", wine_dbgstr_w(driver_module));
66
67     drvs.module = LoadLibraryW(driver_module);
68     if(!drvs.module){
69         TRACE("Unable to load %s: %u\n", wine_dbgstr_w(driver_module),
70                 GetLastError());
71         return FALSE;
72     }
73
74 #define LDFC(n) do { drvs.p##n = (void*)GetProcAddress(drvs.module, #n);\
75         if(!drvs.p##n) { FreeLibrary(drvs.module); return FALSE; } } while(0)
76     LDFC(GetEndpointIDs);
77     LDFC(GetAudioEndpoint);
78 #undef LDFC
79
80     TRACE("Successfully loaded %s\n", wine_dbgstr_w(driver_module));
81
82     return TRUE;
83 }
84
85 static BOOL init_driver(void)
86 {
87     static const WCHAR alsaW[] = {'a','l','s','a',0};
88     static const WCHAR ossW[] = {'o','s','s',0};
89     static const WCHAR coreaudioW[] = {'c','o','r','e','a','u','d','i','o',0};
90     static const WCHAR *default_drivers[] = { alsaW, coreaudioW, ossW };
91     static const WCHAR drv_key[] = {'S','o','f','t','w','a','r','e','\\',
92         'W','i','n','e','\\','D','r','i','v','e','r','s',0};
93     static const WCHAR drv_value[] = {'A','u','d','i','o',0};
94     HKEY key;
95     UINT i;
96
97     if(drvs.module)
98         return TRUE;
99
100     if(RegOpenKeyW(HKEY_CURRENT_USER, drv_key, &key) == ERROR_SUCCESS){
101         WCHAR driver_name[256];
102         DWORD size = sizeof(driver_name);
103
104         if(RegQueryValueExW(key, drv_value, 0, NULL, (BYTE*)driver_name,
105                     &size) == ERROR_SUCCESS){
106             BOOL ret = load_driver(driver_name);
107             RegCloseKey(key);
108             if(!ret)
109                 ERR("Failed to load driver: %s\n", wine_dbgstr_w(driver_name));
110             return ret;
111         }
112
113         RegCloseKey(key);
114     }
115
116     for(i = 0; i < sizeof(default_drivers)/sizeof(*default_drivers); ++i)
117         if(load_driver(default_drivers[i]))
118             return TRUE;
119
120     return FALSE;
121 }
122
123 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
124 {
125     TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
126
127     switch (fdwReason)
128     {
129         case DLL_PROCESS_ATTACH:
130             instance = hinstDLL;
131             DisableThreadLibraryCalls(hinstDLL);
132             break;
133         case DLL_PROCESS_DETACH:
134             MMDevEnum_Free();
135             break;
136     }
137
138     return TRUE;
139 }
140
141 HRESULT WINAPI DllCanUnloadNow(void)
142 {
143     return S_FALSE;
144 }
145
146 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
147
148 typedef struct {
149     IClassFactory IClassFactory_iface;
150     REFCLSID rclsid;
151     FnCreateInstance pfnCreateInstance;
152 } IClassFactoryImpl;
153
154 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
155 {
156     return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
157 }
158
159 static HRESULT WINAPI
160 MMCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
161 {
162     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
163     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
164     if (ppobj == NULL)
165         return E_POINTER;
166     if (IsEqualIID(riid, &IID_IUnknown) ||
167         IsEqualIID(riid, &IID_IClassFactory))
168     {
169         *ppobj = iface;
170         IUnknown_AddRef(iface);
171         return S_OK;
172     }
173     *ppobj = NULL;
174     return E_NOINTERFACE;
175 }
176
177 static ULONG WINAPI MMCF_AddRef(LPCLASSFACTORY iface)
178 {
179     return 2;
180 }
181
182 static ULONG WINAPI MMCF_Release(LPCLASSFACTORY iface)
183 {
184     /* static class, won't be freed */
185     return 1;
186 }
187
188 static HRESULT WINAPI MMCF_CreateInstance(
189     LPCLASSFACTORY iface,
190     LPUNKNOWN pOuter,
191     REFIID riid,
192     LPVOID *ppobj)
193 {
194     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
195     TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
196
197     if (pOuter)
198         return CLASS_E_NOAGGREGATION;
199
200     if (ppobj == NULL) {
201         WARN("invalid parameter\n");
202         return E_POINTER;
203     }
204     *ppobj = NULL;
205     return This->pfnCreateInstance(riid, ppobj);
206 }
207
208 static HRESULT WINAPI MMCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
209 {
210     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
211     FIXME("(%p, %d) stub!\n", This, dolock);
212     return S_OK;
213 }
214
215 static const IClassFactoryVtbl MMCF_Vtbl = {
216     MMCF_QueryInterface,
217     MMCF_AddRef,
218     MMCF_Release,
219     MMCF_CreateInstance,
220     MMCF_LockServer
221 };
222
223 static IClassFactoryImpl MMDEVAPI_CF[] = {
224     { { &MMCF_Vtbl }, &CLSID_MMDeviceEnumerator, (FnCreateInstance)MMDevEnum_Create }
225 };
226
227 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
228 {
229     int i = 0;
230     TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
231
232     if(!init_driver()){
233         ERR("Driver initialization failed\n");
234         return E_FAIL;
235     }
236
237     if (ppv == NULL) {
238         WARN("invalid parameter\n");
239         return E_INVALIDARG;
240     }
241
242     *ppv = NULL;
243
244     if (!IsEqualIID(riid, &IID_IClassFactory) &&
245         !IsEqualIID(riid, &IID_IUnknown)) {
246         WARN("no interface for %s\n", debugstr_guid(riid));
247         return E_NOINTERFACE;
248     }
249
250     for (i = 0; i < sizeof(MMDEVAPI_CF)/sizeof(MMDEVAPI_CF[0]); ++i)
251     {
252         if (IsEqualGUID(rclsid, MMDEVAPI_CF[i].rclsid)) {
253             IUnknown_AddRef(&MMDEVAPI_CF[i].IClassFactory_iface);
254             *ppv = &MMDEVAPI_CF[i];
255             return S_OK;
256         }
257         i++;
258     }
259
260     WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
261          debugstr_guid(riid), ppv);
262     return CLASS_E_CLASSNOTAVAILABLE;
263 }
264
265 /***********************************************************************
266  *              DllRegisterServer (MMDEVAPI.@)
267  */
268 HRESULT WINAPI DllRegisterServer(void)
269 {
270     return __wine_register_resources( instance, NULL );
271 }
272
273 /***********************************************************************
274  *              DllUnregisterServer (MMDEVAPI.@)
275  */
276 HRESULT WINAPI DllUnregisterServer(void)
277 {
278     return __wine_unregister_resources( instance, NULL );
279 }