20 std::vector
<DriverIface
> DriverList
;
22 thread_local DriverIface
*ThreadCtxDriver
;
24 enum LogLevel LogLevel
= LogLevel_Error
;
28 DriverIface
*GetThreadDriver() noexcept
{ return ThreadCtxDriver
; }
29 void SetThreadDriver(DriverIface
*driver
) noexcept
{ ThreadCtxDriver
= driver
; }
32 static void LoadDriverList(void);
35 BOOL APIENTRY
DllMain(HINSTANCE
, DWORD reason
, void*)
39 case DLL_PROCESS_ATTACH
:
41 if(auto logfname
= al::getenv("ALROUTER_LOGFILE"))
43 FILE *f
= fopen(logfname
->c_str(), "w");
45 ERR("Could not open log file: %s\n", logfname
->c_str());
49 if(auto loglev
= al::getenv("ALROUTER_LOGLEVEL"))
52 long l
= strtol(loglev
->c_str(), &end
, 0);
53 if(!end
|| *end
!= '\0')
54 ERR("Invalid log level value: %s\n", loglev
->c_str());
55 else if(l
< LogLevel_None
|| l
> LogLevel_Trace
)
56 ERR("Log level out of range: %s\n", loglev
->c_str());
58 LogLevel
= static_cast<enum LogLevel
>(l
);
60 TRACE("Initializing router v0.1-%s %s\n", ALSOFT_GIT_COMMIT_HASH
, ALSOFT_GIT_BRANCH
);
65 case DLL_THREAD_ATTACH
:
67 case DLL_THREAD_DETACH
:
70 case DLL_PROCESS_DETACH
:
73 if(LogFile
&& LogFile
!= stderr
)
83 static void AddModule(HMODULE module
, const WCHAR
*name
)
85 for(auto &drv
: DriverList
)
87 if(drv
.Module
== module
)
89 TRACE("Skipping already-loaded module %p\n", decltype(std::declval
<void*>()){module
});
95 TRACE("Skipping similarly-named module %ls\n", name
);
101 DriverList
.emplace_back(name
, module
);
102 DriverIface
&newdrv
= DriverList
.back();
104 /* Load required functions. */
106 #define LOAD_PROC(x) do { \
107 newdrv.x = reinterpret_cast<decltype(newdrv.x)>(reinterpret_cast<void*>( \
108 GetProcAddress(module, #x))); \
111 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
115 LOAD_PROC(alcCreateContext
);
116 LOAD_PROC(alcMakeContextCurrent
);
117 LOAD_PROC(alcProcessContext
);
118 LOAD_PROC(alcSuspendContext
);
119 LOAD_PROC(alcDestroyContext
);
120 LOAD_PROC(alcGetCurrentContext
);
121 LOAD_PROC(alcGetContextsDevice
);
122 LOAD_PROC(alcOpenDevice
);
123 LOAD_PROC(alcCloseDevice
);
124 LOAD_PROC(alcGetError
);
125 LOAD_PROC(alcIsExtensionPresent
);
126 LOAD_PROC(alcGetProcAddress
);
127 LOAD_PROC(alcGetEnumValue
);
128 LOAD_PROC(alcGetString
);
129 LOAD_PROC(alcGetIntegerv
);
130 LOAD_PROC(alcCaptureOpenDevice
);
131 LOAD_PROC(alcCaptureCloseDevice
);
132 LOAD_PROC(alcCaptureStart
);
133 LOAD_PROC(alcCaptureStop
);
134 LOAD_PROC(alcCaptureSamples
);
137 LOAD_PROC(alDisable
);
138 LOAD_PROC(alIsEnabled
);
139 LOAD_PROC(alGetString
);
140 LOAD_PROC(alGetBooleanv
);
141 LOAD_PROC(alGetIntegerv
);
142 LOAD_PROC(alGetFloatv
);
143 LOAD_PROC(alGetDoublev
);
144 LOAD_PROC(alGetBoolean
);
145 LOAD_PROC(alGetInteger
);
146 LOAD_PROC(alGetFloat
);
147 LOAD_PROC(alGetDouble
);
148 LOAD_PROC(alGetError
);
149 LOAD_PROC(alIsExtensionPresent
);
150 LOAD_PROC(alGetProcAddress
);
151 LOAD_PROC(alGetEnumValue
);
152 LOAD_PROC(alListenerf
);
153 LOAD_PROC(alListener3f
);
154 LOAD_PROC(alListenerfv
);
155 LOAD_PROC(alListeneri
);
156 LOAD_PROC(alListener3i
);
157 LOAD_PROC(alListeneriv
);
158 LOAD_PROC(alGetListenerf
);
159 LOAD_PROC(alGetListener3f
);
160 LOAD_PROC(alGetListenerfv
);
161 LOAD_PROC(alGetListeneri
);
162 LOAD_PROC(alGetListener3i
);
163 LOAD_PROC(alGetListeneriv
);
164 LOAD_PROC(alGenSources
);
165 LOAD_PROC(alDeleteSources
);
166 LOAD_PROC(alIsSource
);
167 LOAD_PROC(alSourcef
);
168 LOAD_PROC(alSource3f
);
169 LOAD_PROC(alSourcefv
);
170 LOAD_PROC(alSourcei
);
171 LOAD_PROC(alSource3i
);
172 LOAD_PROC(alSourceiv
);
173 LOAD_PROC(alGetSourcef
);
174 LOAD_PROC(alGetSource3f
);
175 LOAD_PROC(alGetSourcefv
);
176 LOAD_PROC(alGetSourcei
);
177 LOAD_PROC(alGetSource3i
);
178 LOAD_PROC(alGetSourceiv
);
179 LOAD_PROC(alSourcePlayv
);
180 LOAD_PROC(alSourceStopv
);
181 LOAD_PROC(alSourceRewindv
);
182 LOAD_PROC(alSourcePausev
);
183 LOAD_PROC(alSourcePlay
);
184 LOAD_PROC(alSourceStop
);
185 LOAD_PROC(alSourceRewind
);
186 LOAD_PROC(alSourcePause
);
187 LOAD_PROC(alSourceQueueBuffers
);
188 LOAD_PROC(alSourceUnqueueBuffers
);
189 LOAD_PROC(alGenBuffers
);
190 LOAD_PROC(alDeleteBuffers
);
191 LOAD_PROC(alIsBuffer
);
192 LOAD_PROC(alBufferf
);
193 LOAD_PROC(alBuffer3f
);
194 LOAD_PROC(alBufferfv
);
195 LOAD_PROC(alBufferi
);
196 LOAD_PROC(alBuffer3i
);
197 LOAD_PROC(alBufferiv
);
198 LOAD_PROC(alGetBufferf
);
199 LOAD_PROC(alGetBuffer3f
);
200 LOAD_PROC(alGetBufferfv
);
201 LOAD_PROC(alGetBufferi
);
202 LOAD_PROC(alGetBuffer3i
);
203 LOAD_PROC(alGetBufferiv
);
204 LOAD_PROC(alBufferData
);
205 LOAD_PROC(alDopplerFactor
);
206 LOAD_PROC(alDopplerVelocity
);
207 LOAD_PROC(alSpeedOfSound
);
208 LOAD_PROC(alDistanceModel
);
211 ALCint alc_ver
[2] = { 0, 0 };
212 newdrv
.alcGetIntegerv(nullptr, ALC_MAJOR_VERSION
, 1, &alc_ver
[0]);
213 newdrv
.alcGetIntegerv(nullptr, ALC_MINOR_VERSION
, 1, &alc_ver
[1]);
214 if(newdrv
.alcGetError(nullptr) == ALC_NO_ERROR
)
215 newdrv
.ALCVer
= MAKE_ALC_VER(alc_ver
[0], alc_ver
[1]);
218 WARN("Failed to query ALC version for %ls, assuming 1.0\n", name
);
219 newdrv
.ALCVer
= MAKE_ALC_VER(1, 0);
223 #define LOAD_PROC(x) do { \
224 newdrv.x = reinterpret_cast<decltype(newdrv.x)>( \
225 newdrv.alcGetProcAddress(nullptr, #x)); \
228 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
232 if(newdrv
.alcIsExtensionPresent(nullptr, "ALC_EXT_thread_local_context"))
234 LOAD_PROC(alcSetThreadContext
);
235 LOAD_PROC(alcGetThreadContext
);
241 DriverList
.pop_back();
244 TRACE("Loaded module %p, %ls, ALC %d.%d\n", decltype(std::declval
<void*>()){module
}, name
,
245 newdrv
.ALCVer
>>8, newdrv
.ALCVer
&255);
249 static void SearchDrivers(WCHAR
*path
)
251 WIN32_FIND_DATAW fdata
;
253 TRACE("Searching for drivers in %ls...\n", path
);
254 std::wstring srchPath
= path
;
255 srchPath
+= L
"\\*oal.dll";
257 HANDLE srchHdl
= FindFirstFileW(srchPath
.c_str(), &fdata
);
258 if(srchHdl
!= INVALID_HANDLE_VALUE
)
265 srchPath
+= fdata
.cFileName
;
266 TRACE("Found %ls\n", srchPath
.c_str());
268 mod
= LoadLibraryW(srchPath
.c_str());
270 WARN("Could not load %ls\n", srchPath
.c_str());
272 AddModule(mod
, fdata
.cFileName
);
273 } while(FindNextFileW(srchHdl
, &fdata
));
278 static WCHAR
*strrchrW(WCHAR
*str
, WCHAR ch
)
280 WCHAR
*res
= nullptr;
281 while(str
&& *str
!= '\0')
290 static int GetLoadedModuleDirectory(const WCHAR
*name
, WCHAR
*moddir
, DWORD length
)
292 HMODULE module
= nullptr;
297 module
= GetModuleHandleW(name
);
298 if(!module
) return 0;
301 if(GetModuleFileNameW(module
, moddir
, length
) == 0)
304 sep0
= strrchrW(moddir
, '/');
305 if(sep0
) sep1
= strrchrW(sep0
+1, '\\');
306 else sep1
= strrchrW(moddir
, '\\');
308 if(sep1
) *sep1
= '\0';
309 else if(sep0
) *sep0
= '\0';
315 void LoadDriverList(void)
317 WCHAR dll_path
[MAX_PATH
+1] = L
"";
318 WCHAR cwd_path
[MAX_PATH
+1] = L
"";
319 WCHAR proc_path
[MAX_PATH
+1] = L
"";
320 WCHAR sys_path
[MAX_PATH
+1] = L
"";
323 if(GetLoadedModuleDirectory(L
"OpenAL32.dll", dll_path
, MAX_PATH
))
324 TRACE("Got DLL path %ls\n", dll_path
);
326 GetCurrentDirectoryW(MAX_PATH
, cwd_path
);
327 len
= lstrlenW(cwd_path
);
328 if(len
> 0 && (cwd_path
[len
-1] == '\\' || cwd_path
[len
-1] == '/'))
329 cwd_path
[len
-1] = '\0';
330 TRACE("Got current working directory %ls\n", cwd_path
);
332 if(GetLoadedModuleDirectory(nullptr, proc_path
, MAX_PATH
))
333 TRACE("Got proc path %ls\n", proc_path
);
335 GetSystemDirectoryW(sys_path
, MAX_PATH
);
336 len
= lstrlenW(sys_path
);
337 if(len
> 0 && (sys_path
[len
-1] == '\\' || sys_path
[len
-1] == '/'))
338 sys_path
[len
-1] = '\0';
339 TRACE("Got system path %ls\n", sys_path
);
341 /* Don't search the DLL's path if it is the same as the current working
342 * directory, app's path, or system path (don't want to do duplicate
343 * searches, or increase the priority of the app or system path).
346 (!cwd_path
[0] || wcscmp(dll_path
, cwd_path
) != 0) &&
347 (!proc_path
[0] || wcscmp(dll_path
, proc_path
) != 0) &&
348 (!sys_path
[0] || wcscmp(dll_path
, sys_path
) != 0))
349 SearchDrivers(dll_path
);
351 (!proc_path
[0] || wcscmp(cwd_path
, proc_path
) != 0) &&
352 (!sys_path
[0] || wcscmp(cwd_path
, sys_path
) != 0))
353 SearchDrivers(cwd_path
);
354 if(proc_path
[0] && (!sys_path
[0] || wcscmp(proc_path
, sys_path
) != 0))
355 SearchDrivers(proc_path
);
357 SearchDrivers(sys_path
);
361 PtrIntMap::~PtrIntMap()
363 std::lock_guard
<std::mutex
> maplock
{mLock
};
371 ALenum
PtrIntMap::insert(void *key
, int value
)
373 std::lock_guard
<std::mutex
> maplock
{mLock
};
374 auto iter
= std::lower_bound(mKeys
, mKeys
+mSize
, key
);
375 auto pos
= static_cast<ALsizei
>(std::distance(mKeys
, iter
));
377 if(pos
== mSize
|| mKeys
[pos
] != key
)
379 if(mSize
== mCapacity
)
381 void **newkeys
{nullptr};
382 ALsizei newcap
{mCapacity
? (mCapacity
<<1) : 4};
383 if(newcap
> mCapacity
)
384 newkeys
= static_cast<void**>(
385 al_calloc(16, (sizeof(mKeys
[0])+sizeof(mValues
[0]))*newcap
)
388 return AL_OUT_OF_MEMORY
;
389 auto newvalues
= reinterpret_cast<int*>(&newkeys
[newcap
]);
393 std::copy_n(mKeys
, mSize
, newkeys
);
394 std::copy_n(mValues
, mSize
, newvalues
);
404 std::copy_backward(mKeys
+pos
, mKeys
+mSize
, mKeys
+mSize
+1);
405 std::copy_backward(mValues
+pos
, mValues
+mSize
, mValues
+mSize
+1);
410 mValues
[pos
] = value
;
415 int PtrIntMap::removeByKey(void *key
)
419 std::lock_guard
<std::mutex
> maplock
{mLock
};
420 auto iter
= std::lower_bound(mKeys
, mKeys
+mSize
, key
);
421 auto pos
= static_cast<ALsizei
>(std::distance(mKeys
, iter
));
422 if(pos
< mSize
&& mKeys
[pos
] == key
)
427 std::copy(mKeys
+pos
+1, mKeys
+mSize
, mKeys
+pos
);
428 std::copy(mValues
+pos
+1, mValues
+mSize
, mValues
+pos
);
436 int PtrIntMap::lookupByKey(void *key
)
440 std::lock_guard
<std::mutex
> maplock
{mLock
};
441 auto iter
= std::lower_bound(mKeys
, mKeys
+mSize
, key
);
442 auto pos
= static_cast<ALsizei
>(std::distance(mKeys
, iter
));
443 if(pos
< mSize
&& mKeys
[pos
] == key
)