wined3d: Rename WineD3D_ChoosePixelFormat() to context_choose_pixel_format().
[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 #include "wine/unicode.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
51
52 static HINSTANCE instance;
53
54 DriverFuncs drvs;
55
56 static BOOL load_driver(const WCHAR *name)
57 {
58     WCHAR driver_module[264];
59     static const WCHAR wineW[] = {'w','i','n','e',0};
60     static const WCHAR dotdrvW[] = {'.','d','r','v',0};
61
62     lstrcpyW(driver_module, wineW);
63     lstrcatW(driver_module, name);
64     lstrcatW(driver_module, dotdrvW);
65
66     TRACE("Attempting to load %s\n", wine_dbgstr_w(driver_module));
67
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;
73     }
74
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
81
82     TRACE("Successfully loaded %s\n", wine_dbgstr_w(driver_module));
83
84     return TRUE;
85 }
86
87 static BOOL init_driver(void)
88 {
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;
98
99     if(drvs.module)
100         return TRUE;
101
102     if(RegOpenKeyW(HKEY_CURRENT_USER, drv_key, &key) == ERROR_SUCCESS){
103         WCHAR driver_name[256], *p, *next;
104         DWORD size = sizeof(driver_name);
105
106         if(RegQueryValueExW(key, drv_value, 0, NULL, (BYTE*)driver_name,
107                     &size) == ERROR_SUCCESS && driver_name[0] != '\0'){
108             RegCloseKey(key);
109
110             for(next = p = driver_name; next; p = next + 1){
111                 next = strchrW(p, ',');
112                 if(next)
113                     *next = '\0';
114
115                 if(load_driver(p))
116                     return TRUE;
117
118                 TRACE("Failed to load driver: %s\n", wine_dbgstr_w(driver_name));
119             }
120
121             ERR("No drivers in the registry loaded successfully!\n");
122             return FALSE;
123         }
124
125         RegCloseKey(key);
126     }
127
128     for(i = 0; i < sizeof(default_drivers)/sizeof(*default_drivers); ++i)
129         if(load_driver(default_drivers[i]))
130             return TRUE;
131
132     return FALSE;
133 }
134
135 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
136 {
137     TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
138
139     switch (fdwReason)
140     {
141         case DLL_PROCESS_ATTACH:
142             instance = hinstDLL;
143             DisableThreadLibraryCalls(hinstDLL);
144             break;
145         case DLL_PROCESS_DETACH:
146             MMDevEnum_Free();
147             break;
148     }
149
150     return TRUE;
151 }
152
153 HRESULT WINAPI DllCanUnloadNow(void)
154 {
155     return S_FALSE;
156 }
157
158 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
159
160 typedef struct {
161     IClassFactory IClassFactory_iface;
162     REFCLSID rclsid;
163     FnCreateInstance pfnCreateInstance;
164 } IClassFactoryImpl;
165
166 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
167 {
168     return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
169 }
170
171 static HRESULT WINAPI
172 MMCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
173 {
174     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
175     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
176     if (ppobj == NULL)
177         return E_POINTER;
178     if (IsEqualIID(riid, &IID_IUnknown) ||
179         IsEqualIID(riid, &IID_IClassFactory))
180     {
181         *ppobj = iface;
182         IUnknown_AddRef(iface);
183         return S_OK;
184     }
185     *ppobj = NULL;
186     return E_NOINTERFACE;
187 }
188
189 static ULONG WINAPI MMCF_AddRef(LPCLASSFACTORY iface)
190 {
191     return 2;
192 }
193
194 static ULONG WINAPI MMCF_Release(LPCLASSFACTORY iface)
195 {
196     /* static class, won't be freed */
197     return 1;
198 }
199
200 static HRESULT WINAPI MMCF_CreateInstance(
201     LPCLASSFACTORY iface,
202     LPUNKNOWN pOuter,
203     REFIID riid,
204     LPVOID *ppobj)
205 {
206     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
207     TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
208
209     if (pOuter)
210         return CLASS_E_NOAGGREGATION;
211
212     if (ppobj == NULL) {
213         WARN("invalid parameter\n");
214         return E_POINTER;
215     }
216     *ppobj = NULL;
217     return This->pfnCreateInstance(riid, ppobj);
218 }
219
220 static HRESULT WINAPI MMCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
221 {
222     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
223     FIXME("(%p, %d) stub!\n", This, dolock);
224     return S_OK;
225 }
226
227 static const IClassFactoryVtbl MMCF_Vtbl = {
228     MMCF_QueryInterface,
229     MMCF_AddRef,
230     MMCF_Release,
231     MMCF_CreateInstance,
232     MMCF_LockServer
233 };
234
235 static IClassFactoryImpl MMDEVAPI_CF[] = {
236     { { &MMCF_Vtbl }, &CLSID_MMDeviceEnumerator, (FnCreateInstance)MMDevEnum_Create }
237 };
238
239 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
240 {
241     int i = 0;
242     TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
243
244     if(!init_driver()){
245         ERR("Driver initialization failed\n");
246         return E_FAIL;
247     }
248
249     if (ppv == NULL) {
250         WARN("invalid parameter\n");
251         return E_INVALIDARG;
252     }
253
254     *ppv = NULL;
255
256     if (!IsEqualIID(riid, &IID_IClassFactory) &&
257         !IsEqualIID(riid, &IID_IUnknown)) {
258         WARN("no interface for %s\n", debugstr_guid(riid));
259         return E_NOINTERFACE;
260     }
261
262     for (i = 0; i < sizeof(MMDEVAPI_CF)/sizeof(MMDEVAPI_CF[0]); ++i)
263     {
264         if (IsEqualGUID(rclsid, MMDEVAPI_CF[i].rclsid)) {
265             IUnknown_AddRef(&MMDEVAPI_CF[i].IClassFactory_iface);
266             *ppv = &MMDEVAPI_CF[i];
267             return S_OK;
268         }
269         i++;
270     }
271
272     WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
273          debugstr_guid(riid), ppv);
274     return CLASS_E_CLASSNOTAVAILABLE;
275 }
276
277 /***********************************************************************
278  *              DllRegisterServer (MMDEVAPI.@)
279  */
280 HRESULT WINAPI DllRegisterServer(void)
281 {
282     return __wine_register_resources( instance );
283 }
284
285 /***********************************************************************
286  *              DllUnregisterServer (MMDEVAPI.@)
287  */
288 HRESULT WINAPI DllUnregisterServer(void)
289 {
290     return __wine_unregister_resources( instance );
291 }