20 std::vector
<DriverIface
> DriverList
;
22 thread_local DriverIface
*ThreadCtxDriver
;
24 enum LogLevel LogLevel
= LogLevel_Error
;
27 static void LoadDriverList(void);
30 BOOL APIENTRY
DllMain(HINSTANCE
, DWORD reason
, void*)
34 case DLL_PROCESS_ATTACH
:
36 if(auto logfname
= al::getenv("ALROUTER_LOGFILE"))
38 FILE *f
= fopen(logfname
->c_str(), "w");
40 ERR("Could not open log file: %s\n", logfname
->c_str());
44 if(auto loglev
= al::getenv("ALROUTER_LOGLEVEL"))
47 long l
= strtol(loglev
->c_str(), &end
, 0);
48 if(!end
|| *end
!= '\0')
49 ERR("Invalid log level value: %s\n", loglev
->c_str());
50 else if(l
< LogLevel_None
|| l
> LogLevel_Trace
)
51 ERR("Log level out of range: %s\n", loglev
->c_str());
53 LogLevel
= static_cast<enum LogLevel
>(l
);
55 TRACE("Initializing router v0.1-%s %s\n", ALSOFT_GIT_COMMIT_HASH
, ALSOFT_GIT_BRANCH
);
60 case DLL_THREAD_ATTACH
:
62 case DLL_THREAD_DETACH
:
65 case DLL_PROCESS_DETACH
:
68 if(LogFile
&& LogFile
!= stderr
)
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", decltype(std::declval
<void*>()){module
});
90 TRACE("Skipping similarly-named module %ls\n", name
);
96 DriverList
.emplace_back(name
, module
);
97 DriverIface
&newdrv
= DriverList
.back();
99 /* Load required functions. */
101 #define LOAD_PROC(x) do { \
102 newdrv.x = reinterpret_cast<decltype(newdrv.x)>(reinterpret_cast<void*>( \
103 GetProcAddress(module, #x))); \
106 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
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
);
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
);
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]);
212 newdrv
.ALCVer
= MAKE_ALC_VER(1, 0);
215 #define LOAD_PROC(x) do { \
216 newdrv.x = reinterpret_cast<decltype(newdrv.x)>( \
217 newdrv.alcGetProcAddress(nullptr, #x)); \
220 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
224 if(newdrv
.alcIsExtensionPresent(nullptr, "ALC_EXT_thread_local_context"))
226 LOAD_PROC(alcSetThreadContext
);
227 LOAD_PROC(alcGetThreadContext
);
233 DriverList
.pop_back();
236 TRACE("Loaded module %p, %ls, ALC %d.%d\n", decltype(std::declval
<void*>()){module
}, name
,
237 newdrv
.ALCVer
>>8, newdrv
.ALCVer
&255);
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
)
257 srchPath
+= fdata
.cFileName
;
258 TRACE("Found %ls\n", srchPath
.c_str());
260 mod
= LoadLibraryW(srchPath
.c_str());
262 WARN("Could not load %ls\n", srchPath
.c_str());
264 AddModule(mod
, fdata
.cFileName
);
265 } while(FindNextFileW(srchHdl
, &fdata
));
270 static WCHAR
*strrchrW(WCHAR
*str
, WCHAR ch
)
272 WCHAR
*res
= nullptr;
273 while(str
&& *str
!= '\0')
282 static int GetLoadedModuleDirectory(const WCHAR
*name
, WCHAR
*moddir
, DWORD length
)
284 HMODULE module
= nullptr;
289 module
= GetModuleHandleW(name
);
290 if(!module
) return 0;
293 if(GetModuleFileNameW(module
, moddir
, length
) == 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';
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
"";
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).
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
);
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
);
349 SearchDrivers(sys_path
);
353 PtrIntMap::~PtrIntMap()
355 std::lock_guard
<std::mutex
> maplock
{mLock
};
363 ALenum
PtrIntMap::insert(ALvoid
*key
, ALint value
)
365 std::lock_guard
<std::mutex
> maplock
{mLock
};
366 auto iter
= std::lower_bound(mKeys
, mKeys
+mSize
, key
);
367 auto pos
= static_cast<ALsizei
>(std::distance(mKeys
, iter
));
369 if(pos
== mSize
|| mKeys
[pos
] != key
)
371 if(mSize
== mCapacity
)
373 ALvoid
**newkeys
{nullptr};
374 ALsizei newcap
{mCapacity
? (mCapacity
<<1) : 4};
375 if(newcap
> mCapacity
)
376 newkeys
= static_cast<ALvoid
**>(
377 al_calloc(16, (sizeof(mKeys
[0])+sizeof(mValues
[0]))*newcap
)
380 return AL_OUT_OF_MEMORY
;
381 auto newvalues
= reinterpret_cast<ALint
*>(&newkeys
[newcap
]);
385 std::copy_n(mKeys
, mSize
, newkeys
);
386 std::copy_n(mValues
, mSize
, newvalues
);
396 std::copy_backward(mKeys
+pos
, mKeys
+mSize
, mKeys
+mSize
+1);
397 std::copy_backward(mValues
+pos
, mValues
+mSize
, mValues
+mSize
+1);
402 mValues
[pos
] = value
;
407 ALint
PtrIntMap::removeByKey(ALvoid
*key
)
411 std::lock_guard
<std::mutex
> maplock
{mLock
};
412 auto iter
= std::lower_bound(mKeys
, mKeys
+mSize
, key
);
413 auto pos
= static_cast<ALsizei
>(std::distance(mKeys
, iter
));
414 if(pos
< mSize
&& mKeys
[pos
] == key
)
419 std::copy(mKeys
+pos
+1, mKeys
+mSize
, mKeys
+pos
);
420 std::copy(mValues
+pos
+1, mValues
+mSize
, mValues
+pos
);
428 ALint
PtrIntMap::lookupByKey(ALvoid
*key
)
432 std::lock_guard
<std::mutex
> maplock
{mLock
};
433 auto iter
= std::lower_bound(mKeys
, mKeys
+mSize
, key
);
434 auto pos
= static_cast<ALsizei
>(std::distance(mKeys
, iter
));
435 if(pos
< mSize
&& mKeys
[pos
] == key
)