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