18 std::vector
<DriverIface
> DriverList
;
20 thread_local DriverIface
*ThreadCtxDriver
;
22 enum LogLevel LogLevel
= LogLevel_Error
;
25 static void LoadDriverList(void);
28 BOOL APIENTRY
DllMain(HINSTANCE
UNUSED(module
), DWORD reason
, void* UNUSED(reserved
))
34 case DLL_PROCESS_ATTACH
:
36 str
= getenv("ALROUTER_LOGFILE");
37 if(str
&& *str
!= '\0')
39 FILE *f
= fopen(str
, "w");
41 ERR("Could not open log file: %s\n", str
);
45 str
= getenv("ALROUTER_LOGLEVEL");
46 if(str
&& *str
!= '\0')
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
);
55 LogLevel
= static_cast<enum LogLevel
>(l
);
57 TRACE("Initializing router v0.1-%s %s\n", ALSOFT_GIT_COMMIT_HASH
, ALSOFT_GIT_BRANCH
);
62 case DLL_THREAD_ATTACH
:
64 case DLL_THREAD_DETACH
:
67 case DLL_PROCESS_DETACH
:
70 if(LogFile
&& LogFile
!= stderr
)
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
);
92 TRACE("Skipping similarly-named module %ls\n", name
);
98 DriverList
.emplace_back(name
, module
);
99 DriverIface
&newdrv
= DriverList
.back();
101 /* Load required functions. */
103 #define LOAD_PROC(x) do { \
104 newdrv.x = reinterpret_cast<decltype(newdrv.x)>( \
105 GetProcAddress(module, #x)); \
108 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
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
);
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
);
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]);
214 newdrv
.ALCVer
= MAKE_ALC_VER(1, 0);
217 #define LOAD_PROC(x) do { \
218 newdrv.x = reinterpret_cast<decltype(newdrv.x)>( \
219 newdrv.alcGetProcAddress(nullptr, #x)); \
222 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
226 if(newdrv
.alcIsExtensionPresent(nullptr, "ALC_EXT_thread_local_context"))
228 LOAD_PROC(alcSetThreadContext
);
229 LOAD_PROC(alcGetThreadContext
);
235 DriverList
.pop_back();
238 TRACE("Loaded module %p, %ls, ALC %d.%d\n", module
, name
,
239 newdrv
.ALCVer
>>8, newdrv
.ALCVer
&255);
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
)
259 srchPath
+= fdata
.cFileName
;
260 TRACE("Found %ls\n", srchPath
.c_str());
262 mod
= LoadLibraryW(srchPath
.c_str());
264 WARN("Could not load %ls\n", srchPath
.c_str());
266 AddModule(mod
, fdata
.cFileName
);
267 } while(FindNextFileW(srchHdl
, &fdata
));
272 static WCHAR
*strrchrW(WCHAR
*str
, WCHAR ch
)
274 WCHAR
*res
= nullptr;
275 while(str
&& *str
!= '\0')
284 static int GetLoadedModuleDirectory(const WCHAR
*name
, WCHAR
*moddir
, DWORD length
)
286 HMODULE module
= nullptr;
291 module
= GetModuleHandleW(name
);
292 if(!module
) return 0;
295 if(GetModuleFileNameW(module
, moddir
, length
) == 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';
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
"";
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).
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
);
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
);
351 SearchDrivers(sys_path
);
355 PtrIntMap::~PtrIntMap()
357 std::lock_guard
<std::mutex
> maplock
{mLock
};
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
)
382 return AL_OUT_OF_MEMORY
;
383 auto newvalues
= reinterpret_cast<ALint
*>(&newkeys
[newcap
]);
387 std::copy_n(mKeys
, mSize
, newkeys
);
388 std::copy_n(mValues
, mSize
, newvalues
);
398 std::copy_backward(mKeys
+pos
, mKeys
+mSize
, mKeys
+mSize
+1);
399 std::copy_backward(mValues
+pos
, mValues
+mSize
, mValues
+mSize
+1);
404 mValues
[pos
] = value
;
409 ALint
PtrIntMap::removeByKey(ALvoid
*key
)
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
)
421 std::copy(mKeys
+pos
+1, mKeys
+mSize
, mKeys
+pos
);
422 std::copy(mValues
+pos
+1, mValues
+mSize
, mValues
+pos
);
430 ALint
PtrIntMap::lookupByKey(ALvoid
*key
)
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
)