kernel32: Add a stub for SetProcessDEPPolicy.
[wine] / dlls / mmdevapi / main.c
1 /*
2  * Copyright 2009 Maarten Lankhorst
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21
22 #include <stdarg.h>
23
24 #ifdef HAVE_AL_AL_H
25 #include <AL/al.h>
26 #include <AL/alc.h>
27 #elif defined(HAVE_OPENAL_AL_H)
28 #include <OpenAL/al.h>
29 #include <OpenAL/alc.h>
30 #endif
31
32 #define COBJMACROS
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wingdi.h"
36 #include "wine/library.h"
37
38 #include "ole2.h"
39 #include "olectl.h"
40 #include "propsys.h"
41 #include "initguid.h"
42 #include "propkeydef.h"
43 #include "mmdeviceapi.h"
44 #include "dshow.h"
45 #include "dsound.h"
46 #include "audioclient.h"
47 #include "endpointvolume.h"
48 #include "audiopolicy.h"
49 #include "devpkey.h"
50
51 #include "mmdevapi.h"
52 #include "wine/debug.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(mmdevapi);
55
56 #ifdef HAVE_OPENAL
57
58 int local_contexts;
59
60 static CRITICAL_SECTION_DEBUG openal_crst_debug =
61 {
62     0, 0, &openal_crst,
63     { &openal_crst_debug.ProcessLocksList,
64       &openal_crst_debug.ProcessLocksList },
65       0, 0, { (DWORD_PTR)(__FILE__ ": openal_crst_debug") }
66 };
67 CRITICAL_SECTION openal_crst = { &openal_crst_debug, -1, 0, 0, 0, 0 };
68
69 static void *openal_handle = RTLD_DEFAULT;
70 int openal_loaded;
71 #ifdef SONAME_LIBOPENAL
72 LPALCCREATECONTEXT palcCreateContext = NULL;
73 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
74 LPALCPROCESSCONTEXT palcProcessContext = NULL;
75 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
76 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
77 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
78 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
79 LPALCOPENDEVICE palcOpenDevice = NULL;
80 LPALCCLOSEDEVICE palcCloseDevice = NULL;
81 LPALCGETERROR palcGetError = NULL;
82 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
83 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
84 LPALCGETENUMVALUE palcGetEnumValue = NULL;
85 LPALCGETSTRING palcGetString = NULL;
86 LPALCGETINTEGERV palcGetIntegerv = NULL;
87 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
88 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
89 LPALCCAPTURESTART palcCaptureStart = NULL;
90 LPALCCAPTURESTOP palcCaptureStop = NULL;
91 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
92 LPALENABLE palEnable = NULL;
93 LPALDISABLE palDisable = NULL;
94 LPALISENABLED palIsEnabled = NULL;
95 LPALGETSTRING palGetString = NULL;
96 LPALGETBOOLEANV palGetBooleanv = NULL;
97 LPALGETINTEGERV palGetIntegerv = NULL;
98 LPALGETFLOATV palGetFloatv = NULL;
99 LPALGETDOUBLEV palGetDoublev = NULL;
100 LPALGETBOOLEAN palGetBoolean = NULL;
101 LPALGETINTEGER palGetInteger = NULL;
102 LPALGETFLOAT palGetFloat = NULL;
103 LPALGETDOUBLE palGetDouble = NULL;
104 LPALGETERROR palGetError = NULL;
105 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
106 LPALGETPROCADDRESS palGetProcAddress = NULL;
107 LPALGETENUMVALUE palGetEnumValue = NULL;
108 LPALLISTENERF palListenerf = NULL;
109 LPALLISTENER3F palListener3f = NULL;
110 LPALLISTENERFV palListenerfv = NULL;
111 LPALLISTENERI palListeneri = NULL;
112 LPALLISTENER3I palListener3i = NULL;
113 LPALLISTENERIV palListeneriv = NULL;
114 LPALGETLISTENERF palGetListenerf = NULL;
115 LPALGETLISTENER3F palGetListener3f = NULL;
116 LPALGETLISTENERFV palGetListenerfv = NULL;
117 LPALGETLISTENERI palGetListeneri = NULL;
118 LPALGETLISTENER3I palGetListener3i = NULL;
119 LPALGETLISTENERIV palGetListeneriv = NULL;
120 LPALGENSOURCES palGenSources = NULL;
121 LPALDELETESOURCES palDeleteSources = NULL;
122 LPALISSOURCE palIsSource = NULL;
123 LPALSOURCEF palSourcef = NULL;
124 LPALSOURCE3F palSource3f = NULL;
125 LPALSOURCEFV palSourcefv = NULL;
126 LPALSOURCEI palSourcei = NULL;
127 LPALSOURCE3I palSource3i = NULL;
128 LPALSOURCEIV palSourceiv = NULL;
129 LPALGETSOURCEF palGetSourcef = NULL;
130 LPALGETSOURCE3F palGetSource3f = NULL;
131 LPALGETSOURCEFV palGetSourcefv = NULL;
132 LPALGETSOURCEI palGetSourcei = NULL;
133 LPALGETSOURCE3I palGetSource3i = NULL;
134 LPALGETSOURCEIV palGetSourceiv = NULL;
135 LPALSOURCEPLAYV palSourcePlayv = NULL;
136 LPALSOURCESTOPV palSourceStopv = NULL;
137 LPALSOURCEREWINDV palSourceRewindv = NULL;
138 LPALSOURCEPAUSEV palSourcePausev = NULL;
139 LPALSOURCEPLAY palSourcePlay = NULL;
140 LPALSOURCESTOP palSourceStop = NULL;
141 LPALSOURCEREWIND palSourceRewind = NULL;
142 LPALSOURCEPAUSE palSourcePause = NULL;
143 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
144 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
145 LPALGENBUFFERS palGenBuffers = NULL;
146 LPALDELETEBUFFERS palDeleteBuffers = NULL;
147 LPALISBUFFER palIsBuffer = NULL;
148 LPALBUFFERF palBufferf = NULL;
149 LPALBUFFER3F palBuffer3f = NULL;
150 LPALBUFFERFV palBufferfv = NULL;
151 LPALBUFFERI palBufferi = NULL;
152 LPALBUFFER3I palBuffer3i = NULL;
153 LPALBUFFERIV palBufferiv = NULL;
154 LPALGETBUFFERF palGetBufferf = NULL;
155 LPALGETBUFFER3F palGetBuffer3f = NULL;
156 LPALGETBUFFERFV palGetBufferfv = NULL;
157 LPALGETBUFFERI palGetBufferi = NULL;
158 LPALGETBUFFER3I palGetBuffer3i = NULL;
159 LPALGETBUFFERIV palGetBufferiv = NULL;
160 LPALBUFFERDATA palBufferData = NULL;
161 LPALDOPPLERFACTOR palDopplerFactor = NULL;
162 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
163 LPALDISTANCEMODEL palDistanceModel = NULL;
164 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
165 #endif
166
167 typeof(alcGetCurrentContext) *get_context;
168 typeof(alcMakeContextCurrent) *set_context;
169
170 static void load_libopenal(void)
171 {
172     DWORD failed = 0;
173
174 #ifdef SONAME_LIBOPENAL
175     char error[128];
176     openal_handle = wine_dlopen(SONAME_LIBOPENAL, RTLD_NOW, error, sizeof(error));
177     if (!openal_handle)
178     {
179         ERR("Couldn't load " SONAME_LIBOPENAL ": %s\n", error);
180         return;
181     }
182
183 #define LOAD_FUNCPTR(f) \
184     if((p##f = wine_dlsym(openal_handle, #f, NULL, 0)) == NULL) { \
185         ERR("Couldn't lookup %s in libopenal\n", #f); \
186         failed = 1; \
187     }
188
189     LOAD_FUNCPTR(alcCreateContext);
190     LOAD_FUNCPTR(alcMakeContextCurrent);
191     LOAD_FUNCPTR(alcProcessContext);
192     LOAD_FUNCPTR(alcSuspendContext);
193     LOAD_FUNCPTR(alcDestroyContext);
194     LOAD_FUNCPTR(alcGetCurrentContext);
195     LOAD_FUNCPTR(alcGetContextsDevice);
196     LOAD_FUNCPTR(alcOpenDevice);
197     LOAD_FUNCPTR(alcCloseDevice);
198     LOAD_FUNCPTR(alcGetError);
199     LOAD_FUNCPTR(alcIsExtensionPresent);
200     LOAD_FUNCPTR(alcGetProcAddress);
201     LOAD_FUNCPTR(alcGetEnumValue);
202     LOAD_FUNCPTR(alcGetString);
203     LOAD_FUNCPTR(alcGetIntegerv);
204     LOAD_FUNCPTR(alcCaptureOpenDevice);
205     LOAD_FUNCPTR(alcCaptureCloseDevice);
206     LOAD_FUNCPTR(alcCaptureStart);
207     LOAD_FUNCPTR(alcCaptureStop);
208     LOAD_FUNCPTR(alcCaptureSamples);
209     LOAD_FUNCPTR(alEnable);
210     LOAD_FUNCPTR(alDisable);
211     LOAD_FUNCPTR(alIsEnabled);
212     LOAD_FUNCPTR(alGetString);
213     LOAD_FUNCPTR(alGetBooleanv);
214     LOAD_FUNCPTR(alGetIntegerv);
215     LOAD_FUNCPTR(alGetFloatv);
216     LOAD_FUNCPTR(alGetDoublev);
217     LOAD_FUNCPTR(alGetBoolean);
218     LOAD_FUNCPTR(alGetInteger);
219     LOAD_FUNCPTR(alGetFloat);
220     LOAD_FUNCPTR(alGetDouble);
221     LOAD_FUNCPTR(alGetError);
222     LOAD_FUNCPTR(alIsExtensionPresent);
223     LOAD_FUNCPTR(alGetProcAddress);
224     LOAD_FUNCPTR(alGetEnumValue);
225     LOAD_FUNCPTR(alListenerf);
226     LOAD_FUNCPTR(alListener3f);
227     LOAD_FUNCPTR(alListenerfv);
228     LOAD_FUNCPTR(alListeneri);
229     LOAD_FUNCPTR(alListener3i);
230     LOAD_FUNCPTR(alListeneriv);
231     LOAD_FUNCPTR(alGetListenerf);
232     LOAD_FUNCPTR(alGetListener3f);
233     LOAD_FUNCPTR(alGetListenerfv);
234     LOAD_FUNCPTR(alGetListeneri);
235     LOAD_FUNCPTR(alGetListener3i);
236     LOAD_FUNCPTR(alGetListeneriv);
237     LOAD_FUNCPTR(alGenSources);
238     LOAD_FUNCPTR(alDeleteSources);
239     LOAD_FUNCPTR(alIsSource);
240     LOAD_FUNCPTR(alSourcef);
241     LOAD_FUNCPTR(alSource3f);
242     LOAD_FUNCPTR(alSourcefv);
243     LOAD_FUNCPTR(alSourcei);
244     LOAD_FUNCPTR(alSource3i);
245     LOAD_FUNCPTR(alSourceiv);
246     LOAD_FUNCPTR(alGetSourcef);
247     LOAD_FUNCPTR(alGetSource3f);
248     LOAD_FUNCPTR(alGetSourcefv);
249     LOAD_FUNCPTR(alGetSourcei);
250     LOAD_FUNCPTR(alGetSource3i);
251     LOAD_FUNCPTR(alGetSourceiv);
252     LOAD_FUNCPTR(alSourcePlayv);
253     LOAD_FUNCPTR(alSourceStopv);
254     LOAD_FUNCPTR(alSourceRewindv);
255     LOAD_FUNCPTR(alSourcePausev);
256     LOAD_FUNCPTR(alSourcePlay);
257     LOAD_FUNCPTR(alSourceStop);
258     LOAD_FUNCPTR(alSourceRewind);
259     LOAD_FUNCPTR(alSourcePause);
260     LOAD_FUNCPTR(alSourceQueueBuffers);
261     LOAD_FUNCPTR(alSourceUnqueueBuffers);
262     LOAD_FUNCPTR(alGenBuffers);
263     LOAD_FUNCPTR(alDeleteBuffers);
264     LOAD_FUNCPTR(alIsBuffer);
265     LOAD_FUNCPTR(alBufferf);
266     LOAD_FUNCPTR(alBuffer3f);
267     LOAD_FUNCPTR(alBufferfv);
268     LOAD_FUNCPTR(alBufferi);
269     LOAD_FUNCPTR(alBuffer3i);
270     LOAD_FUNCPTR(alBufferiv);
271     LOAD_FUNCPTR(alGetBufferf);
272     LOAD_FUNCPTR(alGetBuffer3f);
273     LOAD_FUNCPTR(alGetBufferfv);
274     LOAD_FUNCPTR(alGetBufferi);
275     LOAD_FUNCPTR(alGetBuffer3i);
276     LOAD_FUNCPTR(alGetBufferiv);
277     LOAD_FUNCPTR(alBufferData);
278     LOAD_FUNCPTR(alDopplerFactor);
279     LOAD_FUNCPTR(alDopplerVelocity);
280     LOAD_FUNCPTR(alDistanceModel);
281     LOAD_FUNCPTR(alSpeedOfSound);
282 #undef LOAD_FUNCPTR
283 #endif
284
285     if (failed)
286     {
287         WARN("Unloading openal\n");
288         if (openal_handle != RTLD_DEFAULT)
289             wine_dlclose(openal_handle, NULL, 0);
290         openal_handle = NULL;
291         openal_loaded = 0;
292     }
293     else
294     {
295         openal_loaded = 1;
296         local_contexts = palcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
297         if (local_contexts)
298         {
299             set_context = palcGetProcAddress(NULL, "alcSetThreadContext");
300             get_context = palcGetProcAddress(NULL, "alcGetThreadContext");
301             if (!set_context || !get_context)
302             {
303                 ERR("TLS advertised but functions not found, disabling thread local context\n");
304                 local_contexts = 0;
305             }
306         }
307         if (!local_contexts)
308         {
309             set_context = palcMakeContextCurrent;
310             get_context = palcGetCurrentContext;
311         }
312     }
313 }
314
315 #endif /*HAVE_OPENAL*/
316
317 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
318 {
319     TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);
320
321     switch (fdwReason)
322     {
323         case DLL_PROCESS_ATTACH:
324             DisableThreadLibraryCalls(hinstDLL);
325 #ifdef HAVE_OPENAL
326             load_libopenal();
327 #endif /*HAVE_OPENAL*/
328             break;
329         case DLL_PROCESS_DETACH:
330             MMDevEnum_Free();
331             break;
332     }
333
334     return TRUE;
335 }
336
337 HRESULT WINAPI DllCanUnloadNow(void)
338 {
339     return S_FALSE;
340 }
341
342 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
343
344 typedef struct {
345     const IClassFactoryVtbl *lpVtbl;
346     REFCLSID rclsid;
347     FnCreateInstance pfnCreateInstance;
348 } IClassFactoryImpl;
349
350 static HRESULT WINAPI
351 MMCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
352 {
353     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
354     TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
355     if (ppobj == NULL)
356         return E_POINTER;
357     if (IsEqualIID(riid, &IID_IUnknown) ||
358         IsEqualIID(riid, &IID_IClassFactory))
359     {
360         *ppobj = iface;
361         IUnknown_AddRef(iface);
362         return S_OK;
363     }
364     *ppobj = NULL;
365     return E_NOINTERFACE;
366 }
367
368 static ULONG WINAPI MMCF_AddRef(LPCLASSFACTORY iface)
369 {
370     return 2;
371 }
372
373 static ULONG WINAPI MMCF_Release(LPCLASSFACTORY iface)
374 {
375     /* static class, won't be freed */
376     return 1;
377 }
378
379 static HRESULT WINAPI MMCF_CreateInstance(
380     LPCLASSFACTORY iface,
381     LPUNKNOWN pOuter,
382     REFIID riid,
383     LPVOID *ppobj)
384 {
385     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
386     TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
387
388     if (pOuter)
389         return CLASS_E_NOAGGREGATION;
390
391     if (ppobj == NULL) {
392         WARN("invalid parameter\n");
393         return E_POINTER;
394     }
395     *ppobj = NULL;
396     return This->pfnCreateInstance(riid, ppobj);
397 }
398
399 static HRESULT WINAPI MMCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
400 {
401     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
402     FIXME("(%p, %d) stub!\n", This, dolock);
403     return S_OK;
404 }
405
406 static const IClassFactoryVtbl MMCF_Vtbl = {
407     MMCF_QueryInterface,
408     MMCF_AddRef,
409     MMCF_Release,
410     MMCF_CreateInstance,
411     MMCF_LockServer
412 };
413
414 static IClassFactoryImpl MMDEVAPI_CF[] = {
415     { &MMCF_Vtbl, &CLSID_MMDeviceEnumerator, (FnCreateInstance)MMDevEnum_Create }
416 };
417
418 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
419 {
420     int i = 0;
421     TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
422
423     if (ppv == NULL) {
424         WARN("invalid parameter\n");
425         return E_INVALIDARG;
426     }
427
428     *ppv = NULL;
429
430     if (!IsEqualIID(riid, &IID_IClassFactory) &&
431         !IsEqualIID(riid, &IID_IUnknown)) {
432         WARN("no interface for %s\n", debugstr_guid(riid));
433         return E_NOINTERFACE;
434     }
435
436     for (i = 0; i < sizeof(MMDEVAPI_CF)/sizeof(MMDEVAPI_CF[0]); ++i)
437     {
438         if (IsEqualGUID(rclsid, MMDEVAPI_CF[i].rclsid)) {
439             IUnknown_AddRef((IClassFactory*) &MMDEVAPI_CF[i]);
440             *ppv = &MMDEVAPI_CF[i];
441             return S_OK;
442         }
443         i++;
444     }
445
446     WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
447          debugstr_guid(riid), ppv);
448     return CLASS_E_CLASSNOTAVAILABLE;
449 }