Clean up the JACK backend
[openal-soft.git] / router / router.cpp
blob5b976e95ab01f4153ed8967358faeb8ad1d85ace
2 #include "config.h"
4 #include "router.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
10 #include <algorithm>
12 #include "AL/alc.h"
13 #include "AL/al.h"
14 #include "almalloc.h"
16 #include "version.h"
18 std::vector<DriverIface> DriverList;
20 thread_local DriverIface *ThreadCtxDriver;
22 enum LogLevel LogLevel = LogLevel_Error;
23 FILE *LogFile;
25 static void LoadDriverList(void);
28 BOOL APIENTRY DllMain(HINSTANCE UNUSED(module), DWORD reason, void* UNUSED(reserved))
30 const char *str;
32 switch(reason)
34 case DLL_PROCESS_ATTACH:
35 LogFile = stderr;
36 str = getenv("ALROUTER_LOGFILE");
37 if(str && *str != '\0')
39 FILE *f = fopen(str, "w");
40 if(f == nullptr)
41 ERR("Could not open log file: %s\n", str);
42 else
43 LogFile = f;
45 str = getenv("ALROUTER_LOGLEVEL");
46 if(str && *str != '\0')
48 char *end = nullptr;
49 long l = strtol(str, &end, 0);
50 if(!end || *end != '\0')
51 ERR("Invalid log level value: %s\n", str);
52 else if(l < LogLevel_None || l > LogLevel_Trace)
53 ERR("Log level out of range: %s\n", str);
54 else
55 LogLevel = static_cast<enum LogLevel>(l);
57 TRACE("Initializing router v0.1-%s %s\n", ALSOFT_GIT_COMMIT_HASH, ALSOFT_GIT_BRANCH);
58 LoadDriverList();
60 break;
62 case DLL_THREAD_ATTACH:
63 break;
64 case DLL_THREAD_DETACH:
65 break;
67 case DLL_PROCESS_DETACH:
68 DriverList.clear();
70 if(LogFile && LogFile != stderr)
71 fclose(LogFile);
72 LogFile = nullptr;
74 break;
76 return TRUE;
80 static void AddModule(HMODULE module, const WCHAR *name)
82 for(auto &drv : DriverList)
84 if(drv.Module == module)
86 TRACE("Skipping already-loaded module %p\n", module);
87 FreeLibrary(module);
88 return;
90 if(drv.Name == name)
92 TRACE("Skipping similarly-named module %ls\n", name);
93 FreeLibrary(module);
94 return;
98 DriverList.emplace_back(name, module);
99 DriverIface &newdrv = DriverList.back();
101 /* Load required functions. */
102 int err = 0;
103 #define LOAD_PROC(x) do { \
104 newdrv.x = reinterpret_cast<decltype(newdrv.x)>( \
105 GetProcAddress(module, #x)); \
106 if(!newdrv.x) \
108 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
109 err = 1; \
111 } while(0)
112 LOAD_PROC(alcCreateContext);
113 LOAD_PROC(alcMakeContextCurrent);
114 LOAD_PROC(alcProcessContext);
115 LOAD_PROC(alcSuspendContext);
116 LOAD_PROC(alcDestroyContext);
117 LOAD_PROC(alcGetCurrentContext);
118 LOAD_PROC(alcGetContextsDevice);
119 LOAD_PROC(alcOpenDevice);
120 LOAD_PROC(alcCloseDevice);
121 LOAD_PROC(alcGetError);
122 LOAD_PROC(alcIsExtensionPresent);
123 LOAD_PROC(alcGetProcAddress);
124 LOAD_PROC(alcGetEnumValue);
125 LOAD_PROC(alcGetString);
126 LOAD_PROC(alcGetIntegerv);
127 LOAD_PROC(alcCaptureOpenDevice);
128 LOAD_PROC(alcCaptureCloseDevice);
129 LOAD_PROC(alcCaptureStart);
130 LOAD_PROC(alcCaptureStop);
131 LOAD_PROC(alcCaptureSamples);
133 LOAD_PROC(alEnable);
134 LOAD_PROC(alDisable);
135 LOAD_PROC(alIsEnabled);
136 LOAD_PROC(alGetString);
137 LOAD_PROC(alGetBooleanv);
138 LOAD_PROC(alGetIntegerv);
139 LOAD_PROC(alGetFloatv);
140 LOAD_PROC(alGetDoublev);
141 LOAD_PROC(alGetBoolean);
142 LOAD_PROC(alGetInteger);
143 LOAD_PROC(alGetFloat);
144 LOAD_PROC(alGetDouble);
145 LOAD_PROC(alGetError);
146 LOAD_PROC(alIsExtensionPresent);
147 LOAD_PROC(alGetProcAddress);
148 LOAD_PROC(alGetEnumValue);
149 LOAD_PROC(alListenerf);
150 LOAD_PROC(alListener3f);
151 LOAD_PROC(alListenerfv);
152 LOAD_PROC(alListeneri);
153 LOAD_PROC(alListener3i);
154 LOAD_PROC(alListeneriv);
155 LOAD_PROC(alGetListenerf);
156 LOAD_PROC(alGetListener3f);
157 LOAD_PROC(alGetListenerfv);
158 LOAD_PROC(alGetListeneri);
159 LOAD_PROC(alGetListener3i);
160 LOAD_PROC(alGetListeneriv);
161 LOAD_PROC(alGenSources);
162 LOAD_PROC(alDeleteSources);
163 LOAD_PROC(alIsSource);
164 LOAD_PROC(alSourcef);
165 LOAD_PROC(alSource3f);
166 LOAD_PROC(alSourcefv);
167 LOAD_PROC(alSourcei);
168 LOAD_PROC(alSource3i);
169 LOAD_PROC(alSourceiv);
170 LOAD_PROC(alGetSourcef);
171 LOAD_PROC(alGetSource3f);
172 LOAD_PROC(alGetSourcefv);
173 LOAD_PROC(alGetSourcei);
174 LOAD_PROC(alGetSource3i);
175 LOAD_PROC(alGetSourceiv);
176 LOAD_PROC(alSourcePlayv);
177 LOAD_PROC(alSourceStopv);
178 LOAD_PROC(alSourceRewindv);
179 LOAD_PROC(alSourcePausev);
180 LOAD_PROC(alSourcePlay);
181 LOAD_PROC(alSourceStop);
182 LOAD_PROC(alSourceRewind);
183 LOAD_PROC(alSourcePause);
184 LOAD_PROC(alSourceQueueBuffers);
185 LOAD_PROC(alSourceUnqueueBuffers);
186 LOAD_PROC(alGenBuffers);
187 LOAD_PROC(alDeleteBuffers);
188 LOAD_PROC(alIsBuffer);
189 LOAD_PROC(alBufferf);
190 LOAD_PROC(alBuffer3f);
191 LOAD_PROC(alBufferfv);
192 LOAD_PROC(alBufferi);
193 LOAD_PROC(alBuffer3i);
194 LOAD_PROC(alBufferiv);
195 LOAD_PROC(alGetBufferf);
196 LOAD_PROC(alGetBuffer3f);
197 LOAD_PROC(alGetBufferfv);
198 LOAD_PROC(alGetBufferi);
199 LOAD_PROC(alGetBuffer3i);
200 LOAD_PROC(alGetBufferiv);
201 LOAD_PROC(alBufferData);
202 LOAD_PROC(alDopplerFactor);
203 LOAD_PROC(alDopplerVelocity);
204 LOAD_PROC(alSpeedOfSound);
205 LOAD_PROC(alDistanceModel);
206 if(!err)
208 ALCint alc_ver[2] = { 0, 0 };
209 newdrv.alcGetIntegerv(nullptr, ALC_MAJOR_VERSION, 1, &alc_ver[0]);
210 newdrv.alcGetIntegerv(nullptr, ALC_MINOR_VERSION, 1, &alc_ver[1]);
211 if(newdrv.alcGetError(nullptr) == ALC_NO_ERROR)
212 newdrv.ALCVer = MAKE_ALC_VER(alc_ver[0], alc_ver[1]);
213 else
214 newdrv.ALCVer = MAKE_ALC_VER(1, 0);
216 #undef LOAD_PROC
217 #define LOAD_PROC(x) do { \
218 newdrv.x = reinterpret_cast<decltype(newdrv.x)>( \
219 newdrv.alcGetProcAddress(nullptr, #x)); \
220 if(!newdrv.x) \
222 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
223 err = 1; \
225 } while(0)
226 if(newdrv.alcIsExtensionPresent(nullptr, "ALC_EXT_thread_local_context"))
228 LOAD_PROC(alcSetThreadContext);
229 LOAD_PROC(alcGetThreadContext);
233 if(err)
235 DriverList.pop_back();
236 return;
238 TRACE("Loaded module %p, %ls, ALC %d.%d\n", module, name,
239 newdrv.ALCVer>>8, newdrv.ALCVer&255);
240 #undef LOAD_PROC
243 static void SearchDrivers(WCHAR *path)
245 WIN32_FIND_DATAW fdata;
247 TRACE("Searching for drivers in %ls...\n", path);
248 std::wstring srchPath = path;
249 srchPath += L"\\*oal.dll";
251 HANDLE srchHdl = FindFirstFileW(srchPath.c_str(), &fdata);
252 if(srchHdl != INVALID_HANDLE_VALUE)
254 do {
255 HMODULE mod;
257 srchPath = path;
258 srchPath += L"\\";
259 srchPath += fdata.cFileName;
260 TRACE("Found %ls\n", srchPath.c_str());
262 mod = LoadLibraryW(srchPath.c_str());
263 if(!mod)
264 WARN("Could not load %ls\n", srchPath.c_str());
265 else
266 AddModule(mod, fdata.cFileName);
267 } while(FindNextFileW(srchHdl, &fdata));
268 FindClose(srchHdl);
272 static WCHAR *strrchrW(WCHAR *str, WCHAR ch)
274 WCHAR *res = nullptr;
275 while(str && *str != '\0')
277 if(*str == ch)
278 res = str;
279 ++str;
281 return res;
284 static int GetLoadedModuleDirectory(const WCHAR *name, WCHAR *moddir, DWORD length)
286 HMODULE module = nullptr;
287 WCHAR *sep0, *sep1;
289 if(name)
291 module = GetModuleHandleW(name);
292 if(!module) return 0;
295 if(GetModuleFileNameW(module, moddir, length) == 0)
296 return 0;
298 sep0 = strrchrW(moddir, '/');
299 if(sep0) sep1 = strrchrW(sep0+1, '\\');
300 else sep1 = strrchrW(moddir, '\\');
302 if(sep1) *sep1 = '\0';
303 else if(sep0) *sep0 = '\0';
304 else *moddir = '\0';
306 return 1;
309 void LoadDriverList(void)
311 WCHAR dll_path[MAX_PATH+1] = L"";
312 WCHAR cwd_path[MAX_PATH+1] = L"";
313 WCHAR proc_path[MAX_PATH+1] = L"";
314 WCHAR sys_path[MAX_PATH+1] = L"";
315 int len;
317 if(GetLoadedModuleDirectory(L"OpenAL32.dll", dll_path, MAX_PATH))
318 TRACE("Got DLL path %ls\n", dll_path);
320 GetCurrentDirectoryW(MAX_PATH, cwd_path);
321 len = lstrlenW(cwd_path);
322 if(len > 0 && (cwd_path[len-1] == '\\' || cwd_path[len-1] == '/'))
323 cwd_path[len-1] = '\0';
324 TRACE("Got current working directory %ls\n", cwd_path);
326 if(GetLoadedModuleDirectory(nullptr, proc_path, MAX_PATH))
327 TRACE("Got proc path %ls\n", proc_path);
329 GetSystemDirectoryW(sys_path, MAX_PATH);
330 len = lstrlenW(sys_path);
331 if(len > 0 && (sys_path[len-1] == '\\' || sys_path[len-1] == '/'))
332 sys_path[len-1] = '\0';
333 TRACE("Got system path %ls\n", sys_path);
335 /* Don't search the DLL's path if it is the same as the current working
336 * directory, app's path, or system path (don't want to do duplicate
337 * searches, or increase the priority of the app or system path).
339 if(dll_path[0] &&
340 (!cwd_path[0] || wcscmp(dll_path, cwd_path) != 0) &&
341 (!proc_path[0] || wcscmp(dll_path, proc_path) != 0) &&
342 (!sys_path[0] || wcscmp(dll_path, sys_path) != 0))
343 SearchDrivers(dll_path);
344 if(cwd_path[0] &&
345 (!proc_path[0] || wcscmp(cwd_path, proc_path) != 0) &&
346 (!sys_path[0] || wcscmp(cwd_path, sys_path) != 0))
347 SearchDrivers(cwd_path);
348 if(proc_path[0] && (!sys_path[0] || wcscmp(proc_path, sys_path) != 0))
349 SearchDrivers(proc_path);
350 if(sys_path[0])
351 SearchDrivers(sys_path);
355 PtrIntMap::~PtrIntMap()
357 std::lock_guard<std::mutex> maplock{mLock};
358 al_free(mKeys);
359 mKeys = nullptr;
360 mValues = nullptr;
361 mSize = 0;
362 mCapacity = 0;
365 ALenum PtrIntMap::insert(ALvoid *key, ALint value)
367 std::lock_guard<std::mutex> maplock{mLock};
368 auto iter = std::lower_bound(mKeys, mKeys+mSize, key);
369 auto pos = static_cast<ALsizei>(std::distance(mKeys, iter));
371 if(pos == mSize || mKeys[pos] != key)
373 if(mSize == mCapacity)
375 ALvoid **newkeys{nullptr};
376 ALsizei newcap{mCapacity ? (mCapacity<<1) : 4};
377 if(newcap > mCapacity)
378 newkeys = static_cast<ALvoid**>(
379 al_calloc(16, (sizeof(mKeys[0])+sizeof(mValues[0]))*newcap)
381 if(!newkeys)
382 return AL_OUT_OF_MEMORY;
383 auto newvalues = reinterpret_cast<ALint*>(&newkeys[newcap]);
385 if(mKeys)
387 std::copy_n(mKeys, mSize, newkeys);
388 std::copy_n(mValues, mSize, newvalues);
390 al_free(mKeys);
391 mKeys = newkeys;
392 mValues = newvalues;
393 mCapacity = newcap;
396 if(pos < mSize)
398 std::copy_backward(mKeys+pos, mKeys+mSize, mKeys+mSize+1);
399 std::copy_backward(mValues+pos, mValues+mSize, mValues+mSize+1);
401 mSize++;
403 mKeys[pos] = key;
404 mValues[pos] = value;
406 return AL_NO_ERROR;
409 ALint PtrIntMap::removeByKey(ALvoid *key)
411 ALint ret = -1;
413 std::lock_guard<std::mutex> maplock{mLock};
414 auto iter = std::lower_bound(mKeys, mKeys+mSize, key);
415 auto pos = static_cast<ALsizei>(std::distance(mKeys, iter));
416 if(pos < mSize && mKeys[pos] == key)
418 ret = mValues[pos];
419 if(pos+1 < mSize)
421 std::copy(mKeys+pos+1, mKeys+mSize, mKeys+pos);
422 std::copy(mValues+pos+1, mValues+mSize, mValues+pos);
424 mSize--;
427 return ret;
430 ALint PtrIntMap::lookupByKey(ALvoid *key)
432 ALint ret = -1;
434 std::lock_guard<std::mutex> maplock{mLock};
435 auto iter = std::lower_bound(mKeys, mKeys+mSize, key);
436 auto pos = static_cast<ALsizei>(std::distance(mKeys, iter));
437 if(pos < mSize && mKeys[pos] == key)
438 ret = mValues[pos];
440 return ret;