wined3d: Locking partial blocks is not allowed in DEFAULT pool resources.
[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 const WCHAR drv_keyW[] = {'S','o','f','t','w','a','r','e','\\',
57     'W','i','n','e','\\','D','r','i','v','e','r','s',0};
58
59 static const char *get_priority_string(int prio)
60 {
61     switch(prio){
62     case Priority_Unavailable:
63         return "Unavailable";
64     case Priority_Low:
65         return "Low";
66     case Priority_Neutral:
67         return "Neutral";
68     case Priority_Preferred:
69         return "Preferred";
70     }
71     return "Invalid";
72 }
73
74 static BOOL load_driver(const WCHAR *name, DriverFuncs *driver)
75 {
76     WCHAR driver_module[264];
77     static const WCHAR wineW[] = {'w','i','n','e',0};
78     static const WCHAR dotdrvW[] = {'.','d','r','v',0};
79
80     lstrcpyW(driver_module, wineW);
81     lstrcatW(driver_module, name);
82     lstrcatW(driver_module, dotdrvW);
83
84     TRACE("Attempting to load %s\n", wine_dbgstr_w(driver_module));
85
86     driver->module = LoadLibraryW(driver_module);
87     if(!driver->module){
88         TRACE("Unable to load %s: %u\n", wine_dbgstr_w(driver_module),
89                 GetLastError());
90         return FALSE;
91     }
92
93 #define LDFC(n) do { driver->p##n = (void*)GetProcAddress(driver->module, #n);\
94         if(!driver->p##n) { FreeLibrary(driver->module); return FALSE; } } while(0)
95     LDFC(GetPriority);
96     LDFC(GetEndpointIDs);
97     LDFC(GetAudioEndpoint);
98     LDFC(GetAudioSessionManager);
99 #undef LDFC
100
101     driver->priority = driver->pGetPriority();
102     lstrcpyW(driver->module_name, driver_module);
103
104     TRACE("Successfully loaded %s with priority %s\n",
105             wine_dbgstr_w(driver_module), get_priority_string(driver->priority));
106
107     return TRUE;
108 }
109
110 static BOOL init_driver(void)
111 {
112     static const WCHAR drv_value[] = {'A','u','d','i','o',0};
113
114     static WCHAR default_list[] = {'a','l','s','a',',','o','s','s',',',
115         'c','o','r','e','a','u','d','i','o',0};
116
117     DriverFuncs driver;
118     HKEY key;
119     WCHAR reg_list[256], *p, *next, *driver_list = default_list;
120
121     if(drvs.module)
122         return TRUE;
123
124     if(RegOpenKeyW(HKEY_CURRENT_USER, drv_keyW, &key) == ERROR_SUCCESS){
125         DWORD size = sizeof(reg_list);
126
127         if(RegQueryValueExW(key, drv_value, 0, NULL, (BYTE*)reg_list,
128                     &size) == ERROR_SUCCESS){
129             if(reg_list[0] == '\0'){
130                 TRACE("User explicitly chose no driver\n");
131                 RegCloseKey(key);
132                 return TRUE;
133             }
134
135             driver_list = reg_list;
136         }
137
138         RegCloseKey(key);
139     }
140
141     TRACE("Loading driver list %s\n", wine_dbgstr_w(driver_list));
142     for(next = p = driver_list; next; p = next + 1){
143         next = strchrW(p, ',');
144         if(next)
145             *next = '\0';
146
147         driver.priority = Priority_Unavailable;
148         if(load_driver(p, &driver)){
149             if(driver.priority == Priority_Unavailable)
150                 FreeLibrary(driver.module);
151             else if(!drvs.module || driver.priority > drvs.priority){
152                 TRACE("Selecting driver %s with priority %s\n",
153                         wine_dbgstr_w(p), get_priority_string(driver.priority));
154                 if(drvs.module)
155                     FreeLibrary(drvs.module);
156                 drvs = driver;
157             }else
158                 FreeLibrary(driver.module);
159         }else
160             TRACE("Failed to load driver %s\n", wine_dbgstr_w(p));
161
162         if(next)
163             *next = ',';
164     }
165
166     return drvs.module ? TRUE : FALSE;
167 }
168
169 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
170 {
171     TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
172
173     switch (fdwReason)
174     {
175         case DLL_PROCESS_ATTACH:
176             instance = hinstDLL;
177             DisableThreadLibraryCalls(hinstDLL);
178             break;
179         case DLL_PROCESS_DETACH:
180             MMDevEnum_Free();
181             break;
182     }
183
184     return TRUE;
185 }
186
187 HRESULT WINAPI DllCanUnloadNow(void)
188 {
189     return S_FALSE;
190 }
191
192 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
193
194 typedef struct {
195     IClassFactory IClassFactory_iface;
196     REFCLSID rclsid;
197     FnCreateInstance pfnCreateInstance;
198 } IClassFactoryImpl;
199
200 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
201 {
202     return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
203 }
204
205 static HRESULT WINAPI
206 MMCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
207 {
208     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
209     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
210     if (ppobj == NULL)
211         return E_POINTER;
212     if (IsEqualIID(riid, &IID_IUnknown) ||
213         IsEqualIID(riid, &IID_IClassFactory))
214     {
215         *ppobj = iface;
216         IUnknown_AddRef(iface);
217         return S_OK;
218     }
219     *ppobj = NULL;
220     return E_NOINTERFACE;
221 }
222
223 static ULONG WINAPI MMCF_AddRef(LPCLASSFACTORY iface)
224 {
225     return 2;
226 }
227
228 static ULONG WINAPI MMCF_Release(LPCLASSFACTORY iface)
229 {
230     /* static class, won't be freed */
231     return 1;
232 }
233
234 static HRESULT WINAPI MMCF_CreateInstance(
235     LPCLASSFACTORY iface,
236     LPUNKNOWN pOuter,
237     REFIID riid,
238     LPVOID *ppobj)
239 {
240     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
241     TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
242
243     if (pOuter)
244         return CLASS_E_NOAGGREGATION;
245
246     if (ppobj == NULL) {
247         WARN("invalid parameter\n");
248         return E_POINTER;
249     }
250     *ppobj = NULL;
251     return This->pfnCreateInstance(riid, ppobj);
252 }
253
254 static HRESULT WINAPI MMCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
255 {
256     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
257     FIXME("(%p, %d) stub!\n", This, dolock);
258     return S_OK;
259 }
260
261 static const IClassFactoryVtbl MMCF_Vtbl = {
262     MMCF_QueryInterface,
263     MMCF_AddRef,
264     MMCF_Release,
265     MMCF_CreateInstance,
266     MMCF_LockServer
267 };
268
269 static IClassFactoryImpl MMDEVAPI_CF[] = {
270     { { &MMCF_Vtbl }, &CLSID_MMDeviceEnumerator, (FnCreateInstance)MMDevEnum_Create }
271 };
272
273 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
274 {
275     int i = 0;
276     TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
277
278     if(!init_driver()){
279         ERR("Driver initialization failed\n");
280         return E_FAIL;
281     }
282
283     if (ppv == NULL) {
284         WARN("invalid parameter\n");
285         return E_INVALIDARG;
286     }
287
288     *ppv = NULL;
289
290     if (!IsEqualIID(riid, &IID_IClassFactory) &&
291         !IsEqualIID(riid, &IID_IUnknown)) {
292         WARN("no interface for %s\n", debugstr_guid(riid));
293         return E_NOINTERFACE;
294     }
295
296     for (i = 0; i < sizeof(MMDEVAPI_CF)/sizeof(MMDEVAPI_CF[0]); ++i)
297     {
298         if (IsEqualGUID(rclsid, MMDEVAPI_CF[i].rclsid)) {
299             IUnknown_AddRef(&MMDEVAPI_CF[i].IClassFactory_iface);
300             *ppv = &MMDEVAPI_CF[i];
301             return S_OK;
302         }
303         i++;
304     }
305
306     WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
307          debugstr_guid(riid), ppv);
308     return CLASS_E_CLASSNOTAVAILABLE;
309 }
310
311 /***********************************************************************
312  *              DllRegisterServer (MMDEVAPI.@)
313  */
314 HRESULT WINAPI DllRegisterServer(void)
315 {
316     return __wine_register_resources( instance );
317 }
318
319 /***********************************************************************
320  *              DllUnregisterServer (MMDEVAPI.@)
321  */
322 HRESULT WINAPI DllUnregisterServer(void)
323 {
324     return __wine_unregister_resources( instance );
325 }