Don't lock the device state when preparing a new effect state
[openal-soft.git] / router / router.h
blobbd5c907a025338e77a187a610a30ed88eb03b0ec
1 #ifndef ROUTER_ROUTER_H
2 #define ROUTER_ROUTER_H
4 #define WIN32_LEAN_AND_MEAN
5 #include <windows.h>
6 #include <winnt.h>
8 #include <atomic>
9 #include <cstdio>
10 #include <memory>
11 #include <mutex>
12 #include <string>
13 #include <utility>
14 #include <vector>
16 #include "AL/alc.h"
17 #include "AL/al.h"
18 #include "AL/alext.h"
20 #include "almalloc.h"
23 #define MAKE_ALC_VER(major, minor) (((major)<<8) | (minor))
25 struct DriverIface {
26 LPALCCREATECONTEXT alcCreateContext{nullptr};
27 LPALCMAKECONTEXTCURRENT alcMakeContextCurrent{nullptr};
28 LPALCPROCESSCONTEXT alcProcessContext{nullptr};
29 LPALCSUSPENDCONTEXT alcSuspendContext{nullptr};
30 LPALCDESTROYCONTEXT alcDestroyContext{nullptr};
31 LPALCGETCURRENTCONTEXT alcGetCurrentContext{nullptr};
32 LPALCGETCONTEXTSDEVICE alcGetContextsDevice{nullptr};
33 LPALCOPENDEVICE alcOpenDevice{nullptr};
34 LPALCCLOSEDEVICE alcCloseDevice{nullptr};
35 LPALCGETERROR alcGetError{nullptr};
36 LPALCISEXTENSIONPRESENT alcIsExtensionPresent{nullptr};
37 LPALCGETPROCADDRESS alcGetProcAddress{nullptr};
38 LPALCGETENUMVALUE alcGetEnumValue{nullptr};
39 LPALCGETSTRING alcGetString{nullptr};
40 LPALCGETINTEGERV alcGetIntegerv{nullptr};
41 LPALCCAPTUREOPENDEVICE alcCaptureOpenDevice{nullptr};
42 LPALCCAPTURECLOSEDEVICE alcCaptureCloseDevice{nullptr};
43 LPALCCAPTURESTART alcCaptureStart{nullptr};
44 LPALCCAPTURESTOP alcCaptureStop{nullptr};
45 LPALCCAPTURESAMPLES alcCaptureSamples{nullptr};
47 PFNALCSETTHREADCONTEXTPROC alcSetThreadContext{nullptr};
48 PFNALCGETTHREADCONTEXTPROC alcGetThreadContext{nullptr};
50 LPALENABLE alEnable{nullptr};
51 LPALDISABLE alDisable{nullptr};
52 LPALISENABLED alIsEnabled{nullptr};
53 LPALGETSTRING alGetString{nullptr};
54 LPALGETBOOLEANV alGetBooleanv{nullptr};
55 LPALGETINTEGERV alGetIntegerv{nullptr};
56 LPALGETFLOATV alGetFloatv{nullptr};
57 LPALGETDOUBLEV alGetDoublev{nullptr};
58 LPALGETBOOLEAN alGetBoolean{nullptr};
59 LPALGETINTEGER alGetInteger{nullptr};
60 LPALGETFLOAT alGetFloat{nullptr};
61 LPALGETDOUBLE alGetDouble{nullptr};
62 LPALGETERROR alGetError{nullptr};
63 LPALISEXTENSIONPRESENT alIsExtensionPresent{nullptr};
64 LPALGETPROCADDRESS alGetProcAddress{nullptr};
65 LPALGETENUMVALUE alGetEnumValue{nullptr};
66 LPALLISTENERF alListenerf{nullptr};
67 LPALLISTENER3F alListener3f{nullptr};
68 LPALLISTENERFV alListenerfv{nullptr};
69 LPALLISTENERI alListeneri{nullptr};
70 LPALLISTENER3I alListener3i{nullptr};
71 LPALLISTENERIV alListeneriv{nullptr};
72 LPALGETLISTENERF alGetListenerf{nullptr};
73 LPALGETLISTENER3F alGetListener3f{nullptr};
74 LPALGETLISTENERFV alGetListenerfv{nullptr};
75 LPALGETLISTENERI alGetListeneri{nullptr};
76 LPALGETLISTENER3I alGetListener3i{nullptr};
77 LPALGETLISTENERIV alGetListeneriv{nullptr};
78 LPALGENSOURCES alGenSources{nullptr};
79 LPALDELETESOURCES alDeleteSources{nullptr};
80 LPALISSOURCE alIsSource{nullptr};
81 LPALSOURCEF alSourcef{nullptr};
82 LPALSOURCE3F alSource3f{nullptr};
83 LPALSOURCEFV alSourcefv{nullptr};
84 LPALSOURCEI alSourcei{nullptr};
85 LPALSOURCE3I alSource3i{nullptr};
86 LPALSOURCEIV alSourceiv{nullptr};
87 LPALGETSOURCEF alGetSourcef{nullptr};
88 LPALGETSOURCE3F alGetSource3f{nullptr};
89 LPALGETSOURCEFV alGetSourcefv{nullptr};
90 LPALGETSOURCEI alGetSourcei{nullptr};
91 LPALGETSOURCE3I alGetSource3i{nullptr};
92 LPALGETSOURCEIV alGetSourceiv{nullptr};
93 LPALSOURCEPLAYV alSourcePlayv{nullptr};
94 LPALSOURCESTOPV alSourceStopv{nullptr};
95 LPALSOURCEREWINDV alSourceRewindv{nullptr};
96 LPALSOURCEPAUSEV alSourcePausev{nullptr};
97 LPALSOURCEPLAY alSourcePlay{nullptr};
98 LPALSOURCESTOP alSourceStop{nullptr};
99 LPALSOURCEREWIND alSourceRewind{nullptr};
100 LPALSOURCEPAUSE alSourcePause{nullptr};
101 LPALSOURCEQUEUEBUFFERS alSourceQueueBuffers{nullptr};
102 LPALSOURCEUNQUEUEBUFFERS alSourceUnqueueBuffers{nullptr};
103 LPALGENBUFFERS alGenBuffers{nullptr};
104 LPALDELETEBUFFERS alDeleteBuffers{nullptr};
105 LPALISBUFFER alIsBuffer{nullptr};
106 LPALBUFFERF alBufferf{nullptr};
107 LPALBUFFER3F alBuffer3f{nullptr};
108 LPALBUFFERFV alBufferfv{nullptr};
109 LPALBUFFERI alBufferi{nullptr};
110 LPALBUFFER3I alBuffer3i{nullptr};
111 LPALBUFFERIV alBufferiv{nullptr};
112 LPALGETBUFFERF alGetBufferf{nullptr};
113 LPALGETBUFFER3F alGetBuffer3f{nullptr};
114 LPALGETBUFFERFV alGetBufferfv{nullptr};
115 LPALGETBUFFERI alGetBufferi{nullptr};
116 LPALGETBUFFER3I alGetBuffer3i{nullptr};
117 LPALGETBUFFERIV alGetBufferiv{nullptr};
118 LPALBUFFERDATA alBufferData{nullptr};
119 LPALDOPPLERFACTOR alDopplerFactor{nullptr};
120 LPALDOPPLERVELOCITY alDopplerVelocity{nullptr};
121 LPALSPEEDOFSOUND alSpeedOfSound{nullptr};
122 LPALDISTANCEMODEL alDistanceModel{nullptr};
124 /* Functions to load after first context creation. */
125 LPALGENFILTERS alGenFilters{nullptr};
126 LPALDELETEFILTERS alDeleteFilters{nullptr};
127 LPALISFILTER alIsFilter{nullptr};
128 LPALFILTERF alFilterf{nullptr};
129 LPALFILTERFV alFilterfv{nullptr};
130 LPALFILTERI alFilteri{nullptr};
131 LPALFILTERIV alFilteriv{nullptr};
132 LPALGETFILTERF alGetFilterf{nullptr};
133 LPALGETFILTERFV alGetFilterfv{nullptr};
134 LPALGETFILTERI alGetFilteri{nullptr};
135 LPALGETFILTERIV alGetFilteriv{nullptr};
136 LPALGENEFFECTS alGenEffects{nullptr};
137 LPALDELETEEFFECTS alDeleteEffects{nullptr};
138 LPALISEFFECT alIsEffect{nullptr};
139 LPALEFFECTF alEffectf{nullptr};
140 LPALEFFECTFV alEffectfv{nullptr};
141 LPALEFFECTI alEffecti{nullptr};
142 LPALEFFECTIV alEffectiv{nullptr};
143 LPALGETEFFECTF alGetEffectf{nullptr};
144 LPALGETEFFECTFV alGetEffectfv{nullptr};
145 LPALGETEFFECTI alGetEffecti{nullptr};
146 LPALGETEFFECTIV alGetEffectiv{nullptr};
147 LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots{nullptr};
148 LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots{nullptr};
149 LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot{nullptr};
150 LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf{nullptr};
151 LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv{nullptr};
152 LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti{nullptr};
153 LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv{nullptr};
154 LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf{nullptr};
155 LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv{nullptr};
156 LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti{nullptr};
157 LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv{nullptr};
159 std::wstring Name;
160 HMODULE Module{nullptr};
161 int ALCVer{0};
162 std::once_flag InitOnceCtx{};
164 template<typename T>
165 DriverIface(T&& name, HMODULE mod) : Name(std::forward<T>(name)), Module(mod) { }
166 ~DriverIface() { if(Module) FreeLibrary(Module); }
168 DriverIface(const DriverIface&) = delete;
169 DriverIface(DriverIface&&) = delete;
170 DriverIface& operator=(const DriverIface&) = delete;
171 DriverIface& operator=(DriverIface&&) = delete;
173 using DriverIfacePtr = std::unique_ptr<DriverIface>;
175 inline std::vector<DriverIfacePtr> DriverList;
177 inline thread_local DriverIface *ThreadCtxDriver{};
178 inline std::atomic<DriverIface*> CurrentCtxDriver{};
180 inline DriverIface *GetThreadDriver() noexcept { return ThreadCtxDriver; }
181 inline void SetThreadDriver(DriverIface *driver) noexcept { ThreadCtxDriver = driver; }
184 enum class eLogLevel {
185 None = 0,
186 Error = 1,
187 Warn = 2,
188 Trace = 3,
190 extern eLogLevel LogLevel;
191 extern gsl::owner<std::FILE*> LogFile;
193 #define TRACE(...) do { \
194 if(LogLevel >= eLogLevel::Trace) \
196 std::FILE *file{LogFile ? LogFile : stderr}; \
197 fprintf(file, "AL Router (II): " __VA_ARGS__); \
198 fflush(file); \
200 } while(0)
201 #define WARN(...) do { \
202 if(LogLevel >= eLogLevel::Warn) \
204 std::FILE *file{LogFile ? LogFile : stderr}; \
205 fprintf(file, "AL Router (WW): " __VA_ARGS__); \
206 fflush(file); \
208 } while(0)
209 #define ERR(...) do { \
210 if(LogLevel >= eLogLevel::Error) \
212 std::FILE *file{LogFile ? LogFile : stderr}; \
213 fprintf(file, "AL Router (EE): " __VA_ARGS__); \
214 fflush(file); \
216 } while(0)
218 #endif /* ROUTER_ROUTER_H */