]> git.tdb.fi Git - ext/openal.git/blob - router/router.cpp
Import OpenAL Soft 1.23.1 sources
[ext/openal.git] / router / router.cpp
1
2 #include "config.h"
3
4 #include "router.h"
5
6 #include <algorithm>
7 #include <cstdio>
8 #include <cstdlib>
9 #include <cstring>
10
11 #include "AL/alc.h"
12 #include "AL/al.h"
13
14 #include "almalloc.h"
15 #include "strutils.h"
16
17 #include "version.h"
18
19
20 std::vector<DriverIfacePtr> DriverList;
21
22 thread_local DriverIface *ThreadCtxDriver;
23
24 enum LogLevel LogLevel = LogLevel_Error;
25 FILE *LogFile;
26
27 #ifdef __MINGW32__
28 DriverIface *GetThreadDriver() noexcept { return ThreadCtxDriver; }
29 void SetThreadDriver(DriverIface *driver) noexcept { ThreadCtxDriver = driver; }
30 #endif
31
32 static void LoadDriverList(void);
33
34
35 BOOL APIENTRY DllMain(HINSTANCE, DWORD reason, void*)
36 {
37     switch(reason)
38     {
39     case DLL_PROCESS_ATTACH:
40         LogFile = stderr;
41         if(auto logfname = al::getenv("ALROUTER_LOGFILE"))
42         {
43             FILE *f = fopen(logfname->c_str(), "w");
44             if(f == nullptr)
45                 ERR("Could not open log file: %s\n", logfname->c_str());
46             else
47                 LogFile = f;
48         }
49         if(auto loglev = al::getenv("ALROUTER_LOGLEVEL"))
50         {
51             char *end = nullptr;
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());
57             else
58                 LogLevel = static_cast<enum LogLevel>(l);
59         }
60         TRACE("Initializing router v0.1-%s %s\n", ALSOFT_GIT_COMMIT_HASH, ALSOFT_GIT_BRANCH);
61         LoadDriverList();
62
63         break;
64
65     case DLL_THREAD_ATTACH:
66         break;
67     case DLL_THREAD_DETACH:
68         break;
69
70     case DLL_PROCESS_DETACH:
71         DriverList.clear();
72
73         if(LogFile && LogFile != stderr)
74             fclose(LogFile);
75         LogFile = nullptr;
76
77         break;
78     }
79     return TRUE;
80 }
81
82
83 static void AddModule(HMODULE module, const WCHAR *name)
84 {
85     for(auto &drv : DriverList)
86     {
87         if(drv->Module == module)
88         {
89             TRACE("Skipping already-loaded module %p\n", decltype(std::declval<void*>()){module});
90             FreeLibrary(module);
91             return;
92         }
93         if(drv->Name == name)
94         {
95             TRACE("Skipping similarly-named module %ls\n", name);
96             FreeLibrary(module);
97             return;
98         }
99     }
100
101     DriverList.emplace_back(std::make_unique<DriverIface>(name, module));
102     DriverIface &newdrv = *DriverList.back();
103
104     /* Load required functions. */
105     int err = 0;
106 #define LOAD_PROC(x) do {                                                     \
107     newdrv.x = reinterpret_cast<decltype(newdrv.x)>(reinterpret_cast<void*>(  \
108         GetProcAddress(module, #x)));                                         \
109     if(!newdrv.x)                                                             \
110     {                                                                         \
111         ERR("Failed to find entry point for %s in %ls\n", #x, name);          \
112         err = 1;                                                              \
113     }                                                                         \
114 } while(0)
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);
135
136     LOAD_PROC(alEnable);
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(alBufferData);
193     LOAD_PROC(alDopplerFactor);
194     LOAD_PROC(alDopplerVelocity);
195     LOAD_PROC(alSpeedOfSound);
196     LOAD_PROC(alDistanceModel);
197     if(!err)
198     {
199         ALCint alc_ver[2] = { 0, 0 };
200         newdrv.alcGetIntegerv(nullptr, ALC_MAJOR_VERSION, 1, &alc_ver[0]);
201         newdrv.alcGetIntegerv(nullptr, ALC_MINOR_VERSION, 1, &alc_ver[1]);
202         if(newdrv.alcGetError(nullptr) == ALC_NO_ERROR)
203             newdrv.ALCVer = MAKE_ALC_VER(alc_ver[0], alc_ver[1]);
204         else
205         {
206             WARN("Failed to query ALC version for %ls, assuming 1.0\n", name);
207             newdrv.ALCVer = MAKE_ALC_VER(1, 0);
208         }
209
210 #undef LOAD_PROC
211 #define LOAD_PROC(x) do {                                                      \
212     newdrv.x = reinterpret_cast<decltype(newdrv.x)>(reinterpret_cast<void*>(   \
213         GetProcAddress(module, #x)));                                          \
214     if(!newdrv.x)                                                              \
215     {                                                                          \
216         WARN("Failed to find optional entry point for %s in %ls\n", #x, name); \
217     }                                                                          \
218 } while(0)
219     LOAD_PROC(alBufferf);
220     LOAD_PROC(alBuffer3f);
221     LOAD_PROC(alBufferfv);
222     LOAD_PROC(alBufferi);
223     LOAD_PROC(alBuffer3i);
224     LOAD_PROC(alBufferiv);
225     LOAD_PROC(alGetBufferf);
226     LOAD_PROC(alGetBuffer3f);
227     LOAD_PROC(alGetBufferfv);
228     LOAD_PROC(alGetBufferi);
229     LOAD_PROC(alGetBuffer3i);
230     LOAD_PROC(alGetBufferiv);
231
232 #undef LOAD_PROC
233 #define LOAD_PROC(x) do {                                                     \
234     newdrv.x = reinterpret_cast<decltype(newdrv.x)>(                          \
235         newdrv.alcGetProcAddress(nullptr, #x));                               \
236     if(!newdrv.x)                                                             \
237     {                                                                         \
238         ERR("Failed to find entry point for %s in %ls\n", #x, name);          \
239         err = 1;                                                              \
240     }                                                                         \
241 } while(0)
242         if(newdrv.alcIsExtensionPresent(nullptr, "ALC_EXT_thread_local_context"))
243         {
244             LOAD_PROC(alcSetThreadContext);
245             LOAD_PROC(alcGetThreadContext);
246         }
247     }
248
249     if(err)
250     {
251         DriverList.pop_back();
252         return;
253     }
254     TRACE("Loaded module %p, %ls, ALC %d.%d\n", decltype(std::declval<void*>()){module}, name,
255           newdrv.ALCVer>>8, newdrv.ALCVer&255);
256 #undef LOAD_PROC
257 }
258
259 static void SearchDrivers(WCHAR *path)
260 {
261     WIN32_FIND_DATAW fdata;
262
263     TRACE("Searching for drivers in %ls...\n", path);
264     std::wstring srchPath = path;
265     srchPath += L"\\*oal.dll";
266
267     HANDLE srchHdl = FindFirstFileW(srchPath.c_str(), &fdata);
268     if(srchHdl != INVALID_HANDLE_VALUE)
269     {
270         do {
271             HMODULE mod;
272
273             srchPath = path;
274             srchPath += L"\\";
275             srchPath += fdata.cFileName;
276             TRACE("Found %ls\n", srchPath.c_str());
277
278             mod = LoadLibraryW(srchPath.c_str());
279             if(!mod)
280                 WARN("Could not load %ls\n", srchPath.c_str());
281             else
282                 AddModule(mod, fdata.cFileName);
283         } while(FindNextFileW(srchHdl, &fdata));
284         FindClose(srchHdl);
285     }
286 }
287
288 static WCHAR *strrchrW(WCHAR *str, WCHAR ch)
289 {
290     WCHAR *res = nullptr;
291     while(str && *str != '\0')
292     {
293         if(*str == ch)
294             res = str;
295         ++str;
296     }
297     return res;
298 }
299
300 static int GetLoadedModuleDirectory(const WCHAR *name, WCHAR *moddir, DWORD length)
301 {
302     HMODULE module = nullptr;
303     WCHAR *sep0, *sep1;
304
305     if(name)
306     {
307         module = GetModuleHandleW(name);
308         if(!module) return 0;
309     }
310
311     if(GetModuleFileNameW(module, moddir, length) == 0)
312         return 0;
313
314     sep0 = strrchrW(moddir, '/');
315     if(sep0) sep1 = strrchrW(sep0+1, '\\');
316     else sep1 = strrchrW(moddir, '\\');
317
318     if(sep1) *sep1 = '\0';
319     else if(sep0) *sep0 = '\0';
320     else *moddir = '\0';
321
322     return 1;
323 }
324
325 void LoadDriverList(void)
326 {
327     WCHAR dll_path[MAX_PATH+1] = L"";
328     WCHAR cwd_path[MAX_PATH+1] = L"";
329     WCHAR proc_path[MAX_PATH+1] = L"";
330     WCHAR sys_path[MAX_PATH+1] = L"";
331     int len;
332
333     if(GetLoadedModuleDirectory(L"OpenAL32.dll", dll_path, MAX_PATH))
334         TRACE("Got DLL path %ls\n", dll_path);
335
336     GetCurrentDirectoryW(MAX_PATH, cwd_path);
337     len = lstrlenW(cwd_path);
338     if(len > 0 && (cwd_path[len-1] == '\\' || cwd_path[len-1] == '/'))
339         cwd_path[len-1] = '\0';
340     TRACE("Got current working directory %ls\n", cwd_path);
341
342     if(GetLoadedModuleDirectory(nullptr, proc_path, MAX_PATH))
343         TRACE("Got proc path %ls\n", proc_path);
344
345     GetSystemDirectoryW(sys_path, MAX_PATH);
346     len = lstrlenW(sys_path);
347     if(len > 0 && (sys_path[len-1] == '\\' || sys_path[len-1] == '/'))
348         sys_path[len-1] = '\0';
349     TRACE("Got system path %ls\n", sys_path);
350
351     /* Don't search the DLL's path if it is the same as the current working
352      * directory, app's path, or system path (don't want to do duplicate
353      * searches, or increase the priority of the app or system path).
354      */
355     if(dll_path[0] &&
356        (!cwd_path[0] || wcscmp(dll_path, cwd_path) != 0) &&
357        (!proc_path[0] || wcscmp(dll_path, proc_path) != 0) &&
358        (!sys_path[0] || wcscmp(dll_path, sys_path) != 0))
359         SearchDrivers(dll_path);
360     if(cwd_path[0] &&
361        (!proc_path[0] || wcscmp(cwd_path, proc_path) != 0) &&
362        (!sys_path[0] || wcscmp(cwd_path, sys_path) != 0))
363         SearchDrivers(cwd_path);
364     if(proc_path[0] && (!sys_path[0] || wcscmp(proc_path, sys_path) != 0))
365         SearchDrivers(proc_path);
366     if(sys_path[0])
367         SearchDrivers(sys_path);
368 }
369
370
371 PtrIntMap::~PtrIntMap()
372 {
373     std::lock_guard<std::mutex> maplock{mLock};
374     al_free(mKeys);
375     mKeys = nullptr;
376     mValues = nullptr;
377     mSize = 0;
378     mCapacity = 0;
379 }
380
381 ALenum PtrIntMap::insert(void *key, int value)
382 {
383     std::lock_guard<std::mutex> maplock{mLock};
384     auto iter = std::lower_bound(mKeys, mKeys+mSize, key);
385     auto pos = static_cast<ALsizei>(std::distance(mKeys, iter));
386
387     if(pos == mSize || mKeys[pos] != key)
388     {
389         if(mSize == mCapacity)
390         {
391             void **newkeys{nullptr};
392             ALsizei newcap{mCapacity ? (mCapacity<<1) : 4};
393             if(newcap > mCapacity)
394                 newkeys = static_cast<void**>(
395                     al_calloc(16, (sizeof(mKeys[0])+sizeof(mValues[0]))*newcap)
396                 );
397             if(!newkeys)
398                 return AL_OUT_OF_MEMORY;
399             auto newvalues = reinterpret_cast<int*>(&newkeys[newcap]);
400
401             if(mKeys)
402             {
403                 std::copy_n(mKeys, mSize, newkeys);
404                 std::copy_n(mValues, mSize, newvalues);
405             }
406             al_free(mKeys);
407             mKeys = newkeys;
408             mValues = newvalues;
409             mCapacity = newcap;
410         }
411
412         if(pos < mSize)
413         {
414             std::copy_backward(mKeys+pos, mKeys+mSize, mKeys+mSize+1);
415             std::copy_backward(mValues+pos, mValues+mSize, mValues+mSize+1);
416         }
417         mSize++;
418     }
419     mKeys[pos] = key;
420     mValues[pos] = value;
421
422     return AL_NO_ERROR;
423 }
424
425 int PtrIntMap::removeByKey(void *key)
426 {
427     int ret = -1;
428
429     std::lock_guard<std::mutex> maplock{mLock};
430     auto iter = std::lower_bound(mKeys, mKeys+mSize, key);
431     auto pos = static_cast<ALsizei>(std::distance(mKeys, iter));
432     if(pos < mSize && mKeys[pos] == key)
433     {
434         ret = mValues[pos];
435         if(pos+1 < mSize)
436         {
437             std::copy(mKeys+pos+1, mKeys+mSize, mKeys+pos);
438             std::copy(mValues+pos+1, mValues+mSize, mValues+pos);
439         }
440         mSize--;
441     }
442
443     return ret;
444 }
445
446 int PtrIntMap::lookupByKey(void *key)
447 {
448     int ret = -1;
449
450     std::lock_guard<std::mutex> maplock{mLock};
451     auto iter = std::lower_bound(mKeys, mKeys+mSize, key);
452     auto pos = static_cast<ALsizei>(std::distance(mKeys, iter));
453     if(pos < mSize && mKeys[pos] == key)
454         ret = mValues[pos];
455
456     return ret;
457 }