Use std::isfinite instead of isfinite
[openal-soft.git] / router / router.cpp
blob66bf5dd3b867e708db13ba527ac59f1ff5458c71
2 #include "config.h"
4 #include "router.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
10 #include "AL/alc.h"
11 #include "AL/al.h"
12 #include "almalloc.h"
14 #include "version.h"
16 std::vector<DriverIface> DriverList;
18 thread_local DriverIface *ThreadCtxDriver;
20 enum LogLevel LogLevel = LogLevel_Error;
21 FILE *LogFile;
23 static void LoadDriverList(void);
26 BOOL APIENTRY DllMain(HINSTANCE UNUSED(module), DWORD reason, void* UNUSED(reserved))
28 const char *str;
30 switch(reason)
32 case DLL_PROCESS_ATTACH:
33 LogFile = stderr;
34 str = getenv("ALROUTER_LOGFILE");
35 if(str && *str != '\0')
37 FILE *f = fopen(str, "w");
38 if(f == nullptr)
39 ERR("Could not open log file: %s\n", str);
40 else
41 LogFile = f;
43 str = getenv("ALROUTER_LOGLEVEL");
44 if(str && *str != '\0')
46 char *end = nullptr;
47 long l = strtol(str, &end, 0);
48 if(!end || *end != '\0')
49 ERR("Invalid log level value: %s\n", str);
50 else if(l < LogLevel_None || l > LogLevel_Trace)
51 ERR("Log level out of range: %s\n", str);
52 else
53 LogLevel = static_cast<enum LogLevel>(l);
55 TRACE("Initializing router v0.1-%s %s\n", ALSOFT_GIT_COMMIT_HASH, ALSOFT_GIT_BRANCH);
56 LoadDriverList();
58 break;
60 case DLL_THREAD_ATTACH:
61 break;
62 case DLL_THREAD_DETACH:
63 break;
65 case DLL_PROCESS_DETACH:
66 DriverList.clear();
68 if(LogFile && LogFile != stderr)
69 fclose(LogFile);
70 LogFile = nullptr;
72 break;
74 return TRUE;
78 static void AddModule(HMODULE module, const WCHAR *name)
80 for(auto &drv : DriverList)
82 if(drv.Module == module)
84 TRACE("Skipping already-loaded module %p\n", module);
85 FreeLibrary(module);
86 return;
88 if(drv.Name == name)
90 TRACE("Skipping similarly-named module %ls\n", name);
91 FreeLibrary(module);
92 return;
96 DriverList.emplace_back(name, module);
97 DriverIface &newdrv = DriverList.back();
99 /* Load required functions. */
100 int err = 0;
101 #define LOAD_PROC(x) do { \
102 newdrv.x = reinterpret_cast<decltype(newdrv.x)>( \
103 GetProcAddress(module, #x)); \
104 if(!newdrv.x) \
106 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
107 err = 1; \
109 } while(0)
110 LOAD_PROC(alcCreateContext);
111 LOAD_PROC(alcMakeContextCurrent);
112 LOAD_PROC(alcProcessContext);
113 LOAD_PROC(alcSuspendContext);
114 LOAD_PROC(alcDestroyContext);
115 LOAD_PROC(alcGetCurrentContext);
116 LOAD_PROC(alcGetContextsDevice);
117 LOAD_PROC(alcOpenDevice);
118 LOAD_PROC(alcCloseDevice);
119 LOAD_PROC(alcGetError);
120 LOAD_PROC(alcIsExtensionPresent);
121 LOAD_PROC(alcGetProcAddress);
122 LOAD_PROC(alcGetEnumValue);
123 LOAD_PROC(alcGetString);
124 LOAD_PROC(alcGetIntegerv);
125 LOAD_PROC(alcCaptureOpenDevice);
126 LOAD_PROC(alcCaptureCloseDevice);
127 LOAD_PROC(alcCaptureStart);
128 LOAD_PROC(alcCaptureStop);
129 LOAD_PROC(alcCaptureSamples);
131 LOAD_PROC(alEnable);
132 LOAD_PROC(alDisable);
133 LOAD_PROC(alIsEnabled);
134 LOAD_PROC(alGetString);
135 LOAD_PROC(alGetBooleanv);
136 LOAD_PROC(alGetIntegerv);
137 LOAD_PROC(alGetFloatv);
138 LOAD_PROC(alGetDoublev);
139 LOAD_PROC(alGetBoolean);
140 LOAD_PROC(alGetInteger);
141 LOAD_PROC(alGetFloat);
142 LOAD_PROC(alGetDouble);
143 LOAD_PROC(alGetError);
144 LOAD_PROC(alIsExtensionPresent);
145 LOAD_PROC(alGetProcAddress);
146 LOAD_PROC(alGetEnumValue);
147 LOAD_PROC(alListenerf);
148 LOAD_PROC(alListener3f);
149 LOAD_PROC(alListenerfv);
150 LOAD_PROC(alListeneri);
151 LOAD_PROC(alListener3i);
152 LOAD_PROC(alListeneriv);
153 LOAD_PROC(alGetListenerf);
154 LOAD_PROC(alGetListener3f);
155 LOAD_PROC(alGetListenerfv);
156 LOAD_PROC(alGetListeneri);
157 LOAD_PROC(alGetListener3i);
158 LOAD_PROC(alGetListeneriv);
159 LOAD_PROC(alGenSources);
160 LOAD_PROC(alDeleteSources);
161 LOAD_PROC(alIsSource);
162 LOAD_PROC(alSourcef);
163 LOAD_PROC(alSource3f);
164 LOAD_PROC(alSourcefv);
165 LOAD_PROC(alSourcei);
166 LOAD_PROC(alSource3i);
167 LOAD_PROC(alSourceiv);
168 LOAD_PROC(alGetSourcef);
169 LOAD_PROC(alGetSource3f);
170 LOAD_PROC(alGetSourcefv);
171 LOAD_PROC(alGetSourcei);
172 LOAD_PROC(alGetSource3i);
173 LOAD_PROC(alGetSourceiv);
174 LOAD_PROC(alSourcePlayv);
175 LOAD_PROC(alSourceStopv);
176 LOAD_PROC(alSourceRewindv);
177 LOAD_PROC(alSourcePausev);
178 LOAD_PROC(alSourcePlay);
179 LOAD_PROC(alSourceStop);
180 LOAD_PROC(alSourceRewind);
181 LOAD_PROC(alSourcePause);
182 LOAD_PROC(alSourceQueueBuffers);
183 LOAD_PROC(alSourceUnqueueBuffers);
184 LOAD_PROC(alGenBuffers);
185 LOAD_PROC(alDeleteBuffers);
186 LOAD_PROC(alIsBuffer);
187 LOAD_PROC(alBufferf);
188 LOAD_PROC(alBuffer3f);
189 LOAD_PROC(alBufferfv);
190 LOAD_PROC(alBufferi);
191 LOAD_PROC(alBuffer3i);
192 LOAD_PROC(alBufferiv);
193 LOAD_PROC(alGetBufferf);
194 LOAD_PROC(alGetBuffer3f);
195 LOAD_PROC(alGetBufferfv);
196 LOAD_PROC(alGetBufferi);
197 LOAD_PROC(alGetBuffer3i);
198 LOAD_PROC(alGetBufferiv);
199 LOAD_PROC(alBufferData);
200 LOAD_PROC(alDopplerFactor);
201 LOAD_PROC(alDopplerVelocity);
202 LOAD_PROC(alSpeedOfSound);
203 LOAD_PROC(alDistanceModel);
204 if(!err)
206 ALCint alc_ver[2] = { 0, 0 };
207 newdrv.alcGetIntegerv(nullptr, ALC_MAJOR_VERSION, 1, &alc_ver[0]);
208 newdrv.alcGetIntegerv(nullptr, ALC_MINOR_VERSION, 1, &alc_ver[1]);
209 if(newdrv.alcGetError(nullptr) == ALC_NO_ERROR)
210 newdrv.ALCVer = MAKE_ALC_VER(alc_ver[0], alc_ver[1]);
211 else
212 newdrv.ALCVer = MAKE_ALC_VER(1, 0);
214 #undef LOAD_PROC
215 #define LOAD_PROC(x) do { \
216 newdrv.x = reinterpret_cast<decltype(newdrv.x)>( \
217 newdrv.alcGetProcAddress(nullptr, #x)); \
218 if(!newdrv.x) \
220 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
221 err = 1; \
223 } while(0)
224 if(newdrv.alcIsExtensionPresent(nullptr, "ALC_EXT_thread_local_context"))
226 LOAD_PROC(alcSetThreadContext);
227 LOAD_PROC(alcGetThreadContext);
231 if(err)
233 DriverList.pop_back();
234 return;
236 TRACE("Loaded module %p, %ls, ALC %d.%d\n", module, name,
237 newdrv.ALCVer>>8, newdrv.ALCVer&255);
238 #undef LOAD_PROC
241 static void SearchDrivers(WCHAR *path)
243 WIN32_FIND_DATAW fdata;
245 TRACE("Searching for drivers in %ls...\n", path);
246 std::wstring srchPath = path;
247 srchPath += L"\\*oal.dll";
249 HANDLE srchHdl = FindFirstFileW(srchPath.c_str(), &fdata);
250 if(srchHdl != INVALID_HANDLE_VALUE)
252 do {
253 HMODULE mod;
255 srchPath = path;
256 srchPath += L"\\";
257 srchPath += fdata.cFileName;
258 TRACE("Found %ls\n", srchPath.c_str());
260 mod = LoadLibraryW(srchPath.c_str());
261 if(!mod)
262 WARN("Could not load %ls\n", srchPath.c_str());
263 else
264 AddModule(mod, fdata.cFileName);
265 } while(FindNextFileW(srchHdl, &fdata));
266 FindClose(srchHdl);
270 static WCHAR *strrchrW(WCHAR *str, WCHAR ch)
272 WCHAR *res = nullptr;
273 while(str && *str != '\0')
275 if(*str == ch)
276 res = str;
277 ++str;
279 return res;
282 static int GetLoadedModuleDirectory(const WCHAR *name, WCHAR *moddir, DWORD length)
284 HMODULE module = nullptr;
285 WCHAR *sep0, *sep1;
287 if(name)
289 module = GetModuleHandleW(name);
290 if(!module) return 0;
293 if(GetModuleFileNameW(module, moddir, length) == 0)
294 return 0;
296 sep0 = strrchrW(moddir, '/');
297 if(sep0) sep1 = strrchrW(sep0+1, '\\');
298 else sep1 = strrchrW(moddir, '\\');
300 if(sep1) *sep1 = '\0';
301 else if(sep0) *sep0 = '\0';
302 else *moddir = '\0';
304 return 1;
307 void LoadDriverList(void)
309 WCHAR dll_path[MAX_PATH+1] = L"";
310 WCHAR cwd_path[MAX_PATH+1] = L"";
311 WCHAR proc_path[MAX_PATH+1] = L"";
312 WCHAR sys_path[MAX_PATH+1] = L"";
313 int len;
315 if(GetLoadedModuleDirectory(L"OpenAL32.dll", dll_path, MAX_PATH))
316 TRACE("Got DLL path %ls\n", dll_path);
318 GetCurrentDirectoryW(MAX_PATH, cwd_path);
319 len = lstrlenW(cwd_path);
320 if(len > 0 && (cwd_path[len-1] == '\\' || cwd_path[len-1] == '/'))
321 cwd_path[len-1] = '\0';
322 TRACE("Got current working directory %ls\n", cwd_path);
324 if(GetLoadedModuleDirectory(nullptr, proc_path, MAX_PATH))
325 TRACE("Got proc path %ls\n", proc_path);
327 GetSystemDirectoryW(sys_path, MAX_PATH);
328 len = lstrlenW(sys_path);
329 if(len > 0 && (sys_path[len-1] == '\\' || sys_path[len-1] == '/'))
330 sys_path[len-1] = '\0';
331 TRACE("Got system path %ls\n", sys_path);
333 /* Don't search the DLL's path if it is the same as the current working
334 * directory, app's path, or system path (don't want to do duplicate
335 * searches, or increase the priority of the app or system path).
337 if(dll_path[0] &&
338 (!cwd_path[0] || wcscmp(dll_path, cwd_path) != 0) &&
339 (!proc_path[0] || wcscmp(dll_path, proc_path) != 0) &&
340 (!sys_path[0] || wcscmp(dll_path, sys_path) != 0))
341 SearchDrivers(dll_path);
342 if(cwd_path[0] &&
343 (!proc_path[0] || wcscmp(cwd_path, proc_path) != 0) &&
344 (!sys_path[0] || wcscmp(cwd_path, sys_path) != 0))
345 SearchDrivers(cwd_path);
346 if(proc_path[0] && (!sys_path[0] || wcscmp(proc_path, sys_path) != 0))
347 SearchDrivers(proc_path);
348 if(sys_path[0])
349 SearchDrivers(sys_path);
353 PtrIntMap::~PtrIntMap()
355 std::lock_guard<std::mutex> maplock{mLock};
356 al_free(mKeys);
357 mKeys = nullptr;
358 mValues = nullptr;
359 mSize = 0;
360 mCapacity = 0;
363 ALenum PtrIntMap::insert(ALvoid *key, ALint value)
365 ALsizei pos = 0;
367 std::lock_guard<std::mutex> maplock{mLock};
368 if(mSize > 0)
370 ALsizei count = mSize;
371 do {
372 ALsizei step = count>>1;
373 ALsizei i = pos+step;
374 if(mKeys[i] >= key)
375 count = step;
376 else
378 pos = i+1;
379 count -= step+1;
381 } while(count > 0);
384 if(pos == mSize || mKeys[pos] != key)
386 if(mSize == mCapacity)
388 ALvoid **newkeys = nullptr;
389 ALint *newvalues;
390 ALsizei newcap;
392 newcap = (mCapacity ? (mCapacity<<1) : 4);
393 if(newcap > mCapacity)
394 newkeys = reinterpret_cast<ALvoid**>(
395 al_calloc(16, (sizeof(mKeys[0])+sizeof(mValues[0]))*newcap)
397 if(!newkeys)
398 return AL_OUT_OF_MEMORY;
399 newvalues = (ALint*)&newkeys[newcap];
401 if(mKeys)
403 memcpy(newkeys, mKeys, mSize*sizeof(mKeys[0]));
404 memcpy(newvalues, mValues, mSize*sizeof(mValues[0]));
406 al_free(mKeys);
407 mKeys = newkeys;
408 mValues = newvalues;
409 mCapacity = newcap;
412 if(pos < mSize)
414 memmove(&mKeys[pos+1], &mKeys[pos], (mSize-pos)*sizeof(mKeys[0]));
415 memmove(&mValues[pos+1], &mValues[pos], (mSize-pos)*sizeof(mValues[0]));
417 mSize++;
419 mKeys[pos] = key;
420 mValues[pos] = value;
422 return AL_NO_ERROR;
425 ALint PtrIntMap::removeByKey(ALvoid *key)
427 ALint ret = -1;
429 std::lock_guard<std::mutex> maplock{mLock};
430 if(mSize > 0)
432 ALsizei pos = 0;
433 ALsizei count = mSize;
434 do {
435 ALsizei step = count>>1;
436 ALsizei i = pos+step;
437 if(mKeys[i] >= key)
438 count = step;
439 else
441 pos = i+1;
442 count -= step+1;
444 } while(count > 0);
445 if(pos < mSize && mKeys[pos] == key)
447 ret = mValues[pos];
448 if(pos < mSize-1)
450 memmove(&mKeys[pos], &mKeys[pos+1], (mSize-1-pos)*sizeof(mKeys[0]));
451 memmove(&mValues[pos], &mValues[pos+1], (mSize-1-pos)*sizeof(mValues[0]));
453 mSize--;
456 return ret;
459 ALint PtrIntMap::lookupByKey(ALvoid* key)
461 ALint ret = -1;
463 std::lock_guard<std::mutex> maplock{mLock};
464 if(mSize > 0)
466 ALsizei pos = 0;
467 ALsizei count = mSize;
468 do {
469 ALsizei step = count>>1;
470 ALsizei i = pos+step;
471 if(mKeys[i] >= key)
472 count = step;
473 else
475 pos = i+1;
476 count -= step+1;
478 } while(count > 0);
479 if(pos < mSize && mKeys[pos] == key)
480 ret = mValues[pos];
482 return ret;