15 DriverIface
*DriverList
= NULL
;
16 int DriverListSize
= 0;
17 static int DriverListSizeMax
= 0;
19 enum LogLevel LogLevel
= LogLevel_Error
;
22 static void LoadDriverList(void);
25 BOOL APIENTRY
DllMain(HINSTANCE
UNUSED(module
), DWORD reason
, void* UNUSED(reserved
))
32 case DLL_PROCESS_ATTACH
:
34 str
= getenv("ALROUTER_LOGFILE");
35 if(str
&& *str
!= '\0')
37 FILE *f
= fopen(str
, "w");
39 ERR("Could not open log file: %s\n", str
);
43 str
= getenv("ALROUTER_LOGLEVEL");
44 if(str
&& *str
!= '\0')
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
);
60 case DLL_THREAD_ATTACH
:
61 case DLL_THREAD_DETACH
:
64 case DLL_PROCESS_DETACH
:
67 for(i
= 0;i
< DriverListSize
;i
++)
69 if(DriverList
[i
].Module
)
70 FreeLibrary(DriverList
[i
].Module
);
75 DriverListSizeMax
= 0;
77 if(LogFile
&& LogFile
!= stderr
)
87 #define CAST_FUNC(x) (__typeof(x))
89 #define CAST_FUNC(x) (void*)
92 static void AddModule(HMODULE module
, const WCHAR
*name
)
98 for(i
= 0;i
< DriverListSize
;i
++)
100 if(DriverList
[i
].Module
== module
)
102 TRACE("Skipping already-loaded module %p\n", module
);
106 if(wcscmp(DriverList
[i
].Name
, name
) == 0)
108 TRACE("Skipping similarly-named module %ls\n", name
);
114 if(DriverListSize
== DriverListSizeMax
)
116 int newmax
= DriverListSizeMax
? DriverListSizeMax
<<1 : 4;
117 void *newlist
= al_calloc(DEF_ALIGN
, sizeof(DriverList
[0])*newmax
);
120 memcpy(newlist
, DriverList
, DriverListSize
*sizeof(DriverList
[0]));
122 DriverList
= newlist
;
123 DriverListSizeMax
= newmax
;
126 memset(&newdrv
, 0, sizeof(newdrv
));
127 #define LOAD_PROC(x) do { \
128 newdrv.x = CAST_FUNC(newdrv.x) GetProcAddress(module, #x); \
131 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
135 LOAD_PROC(alcCreateContext
);
136 LOAD_PROC(alcMakeContextCurrent
);
137 LOAD_PROC(alcProcessContext
);
138 LOAD_PROC(alcSuspendContext
);
139 LOAD_PROC(alcDestroyContext
);
140 LOAD_PROC(alcGetCurrentContext
);
141 LOAD_PROC(alcGetContextsDevice
);
142 LOAD_PROC(alcOpenDevice
);
143 LOAD_PROC(alcCloseDevice
);
144 LOAD_PROC(alcGetError
);
145 LOAD_PROC(alcIsExtensionPresent
);
146 LOAD_PROC(alcGetProcAddress
);
147 LOAD_PROC(alcGetEnumValue
);
148 LOAD_PROC(alcGetString
);
149 LOAD_PROC(alcGetIntegerv
);
150 LOAD_PROC(alcCaptureOpenDevice
);
151 LOAD_PROC(alcCaptureCloseDevice
);
152 LOAD_PROC(alcCaptureStart
);
153 LOAD_PROC(alcCaptureStop
);
154 LOAD_PROC(alcCaptureSamples
);
157 LOAD_PROC(alDisable
);
158 LOAD_PROC(alIsEnabled
);
159 LOAD_PROC(alGetString
);
160 LOAD_PROC(alGetBooleanv
);
161 LOAD_PROC(alGetIntegerv
);
162 LOAD_PROC(alGetFloatv
);
163 LOAD_PROC(alGetDoublev
);
164 LOAD_PROC(alGetBoolean
);
165 LOAD_PROC(alGetInteger
);
166 LOAD_PROC(alGetFloat
);
167 LOAD_PROC(alGetDouble
);
168 LOAD_PROC(alGetError
);
169 LOAD_PROC(alIsExtensionPresent
);
170 LOAD_PROC(alGetProcAddress
);
171 LOAD_PROC(alGetEnumValue
);
172 LOAD_PROC(alListenerf
);
173 LOAD_PROC(alListener3f
);
174 LOAD_PROC(alListenerfv
);
175 LOAD_PROC(alListeneri
);
176 LOAD_PROC(alListener3i
);
177 LOAD_PROC(alListeneriv
);
178 LOAD_PROC(alGetListenerf
);
179 LOAD_PROC(alGetListener3f
);
180 LOAD_PROC(alGetListenerfv
);
181 LOAD_PROC(alGetListeneri
);
182 LOAD_PROC(alGetListener3i
);
183 LOAD_PROC(alGetListeneriv
);
184 LOAD_PROC(alGenSources
);
185 LOAD_PROC(alDeleteSources
);
186 LOAD_PROC(alIsSource
);
187 LOAD_PROC(alSourcef
);
188 LOAD_PROC(alSource3f
);
189 LOAD_PROC(alSourcefv
);
190 LOAD_PROC(alSourcei
);
191 LOAD_PROC(alSource3i
);
192 LOAD_PROC(alSourceiv
);
193 LOAD_PROC(alGetSourcef
);
194 LOAD_PROC(alGetSource3f
);
195 LOAD_PROC(alGetSourcefv
);
196 LOAD_PROC(alGetSourcei
);
197 LOAD_PROC(alGetSource3i
);
198 LOAD_PROC(alGetSourceiv
);
199 LOAD_PROC(alSourcePlayv
);
200 LOAD_PROC(alSourceStopv
);
201 LOAD_PROC(alSourceRewindv
);
202 LOAD_PROC(alSourcePausev
);
203 LOAD_PROC(alSourcePlay
);
204 LOAD_PROC(alSourceStop
);
205 LOAD_PROC(alSourceRewind
);
206 LOAD_PROC(alSourcePause
);
207 LOAD_PROC(alSourceQueueBuffers
);
208 LOAD_PROC(alSourceUnqueueBuffers
);
209 LOAD_PROC(alGenBuffers
);
210 LOAD_PROC(alDeleteBuffers
);
211 LOAD_PROC(alIsBuffer
);
212 LOAD_PROC(alBufferf
);
213 LOAD_PROC(alBuffer3f
);
214 LOAD_PROC(alBufferfv
);
215 LOAD_PROC(alBufferi
);
216 LOAD_PROC(alBuffer3i
);
217 LOAD_PROC(alBufferiv
);
218 LOAD_PROC(alGetBufferf
);
219 LOAD_PROC(alGetBuffer3f
);
220 LOAD_PROC(alGetBufferfv
);
221 LOAD_PROC(alGetBufferi
);
222 LOAD_PROC(alGetBuffer3i
);
223 LOAD_PROC(alGetBufferiv
);
224 LOAD_PROC(alBufferData
);
225 LOAD_PROC(alDopplerFactor
);
226 LOAD_PROC(alDopplerVelocity
);
227 LOAD_PROC(alSpeedOfSound
);
228 LOAD_PROC(alDistanceModel
);
231 ALCint alc_ver
[2] = { 0, 0 };
232 wcsncpy(newdrv
.Name
, name
, 32);
233 newdrv
.Module
= module
;
234 newdrv
.alcGetIntegerv(NULL
, ALC_MAJOR_VERSION
, 1, &alc_ver
[0]);
235 newdrv
.alcGetIntegerv(NULL
, ALC_MINOR_VERSION
, 1, &alc_ver
[1]);
236 if(newdrv
.alcGetError(NULL
) == ALC_NO_ERROR
)
237 newdrv
.ALCVer
= MAKE_ALC_VER(alc_ver
[0], alc_ver
[1]);
239 newdrv
.ALCVer
= MAKE_ALC_VER(1, 0);
240 TRACE("Loaded module %p, %ls, ALC %d.%d\n", module
, name
,
241 newdrv
.ALCVer
>>8, newdrv
.ALCVer
&255);
242 DriverList
[DriverListSize
++] = newdrv
;
246 static void SearchDrivers(WCHAR
*path
)
248 WCHAR srchPath
[MAX_PATH
+1] = L
"";
249 WIN32_FIND_DATAW fdata
;
252 TRACE("Searching for drivers in %ls...\n", path
);
253 wcsncpy(srchPath
, path
, MAX_PATH
);
254 wcsncat(srchPath
, L
"\\*oal.dll", MAX_PATH
- lstrlenW(srchPath
));
255 srchHdl
= FindFirstFileW(srchPath
, &fdata
);
256 if(srchHdl
!= INVALID_HANDLE_VALUE
)
261 wcsncpy(srchPath
, path
, MAX_PATH
);
262 wcsncat(srchPath
, L
"\\", MAX_PATH
- lstrlenW(srchPath
));
263 wcsncat(srchPath
, fdata
.cFileName
, MAX_PATH
- lstrlenW(srchPath
));
264 TRACE("Found %ls\n", srchPath
);
266 mod
= LoadLibraryW(srchPath
);
268 WARN("Could not load %ls\n", srchPath
);
270 AddModule(mod
, fdata
.cFileName
);
271 } while(FindNextFileW(srchHdl
, &fdata
));
276 static WCHAR
*strrchrW(WCHAR
*str
, WCHAR ch
)
279 while(str
&& *str
!= '\0')
288 static int GetLoadedModuleDirectory(const WCHAR
*name
, WCHAR
*moddir
, DWORD length
)
290 HMODULE module
= NULL
;
295 module
= GetModuleHandleW(name
);
296 if(!module
) return 0;
299 if(GetModuleFileNameW(module
, moddir
, length
) == 0)
302 sep0
= strrchrW(moddir
, '/');
303 if(sep0
) sep1
= strrchrW(sep0
+1, '\\');
304 else sep1
= strrchrW(moddir
, '\\');
306 if(sep1
) *sep1
= '\0';
307 else if(sep0
) *sep0
= '\0';
313 void LoadDriverList(void)
315 WCHAR path
[MAX_PATH
+1] = L
"";
318 if(GetLoadedModuleDirectory(L
"OpenAL32.dll", path
, MAX_PATH
))
321 GetCurrentDirectoryW(MAX_PATH
, path
);
322 len
= lstrlenW(path
);
323 if(len
> 0 && (path
[len
-1] == '\\' || path
[len
-1] == '/'))
327 if(GetLoadedModuleDirectory(NULL
, path
, MAX_PATH
))
330 GetSystemDirectoryW(path
, MAX_PATH
);
331 len
= lstrlenW(path
);
332 if(len
> 0 && (path
[len
-1] == '\\' || path
[len
-1] == '/'))
338 void InitPtrIntMap(PtrIntMap
*map
)
344 RWLockInit(&map
->lock
);
347 void ResetPtrIntMap(PtrIntMap
*map
)
349 WriteLock(&map
->lock
);
355 WriteUnlock(&map
->lock
);
358 ALenum
InsertPtrIntMapEntry(PtrIntMap
*map
, ALvoid
*key
, ALint value
)
362 WriteLock(&map
->lock
);
365 ALsizei count
= map
->size
;
367 ALsizei step
= count
>>1;
368 ALsizei i
= pos
+step
;
369 if(!(map
->keys
[i
] < key
))
379 if(pos
== map
->size
|| map
->keys
[pos
] != key
)
381 if(map
->size
== map
->capacity
)
383 ALvoid
**keys
= NULL
;
387 newcap
= (map
->capacity
? (map
->capacity
<<1) : 4);
388 if(newcap
> map
->capacity
)
389 keys
= al_calloc(16, (sizeof(map
->keys
[0])+sizeof(map
->values
[0]))*newcap
);
392 WriteUnlock(&map
->lock
);
393 return AL_OUT_OF_MEMORY
;
395 values
= (ALint
*)&keys
[newcap
];
399 memcpy(keys
, map
->keys
, map
->size
*sizeof(map
->keys
[0]));
400 memcpy(values
, map
->values
, map
->size
*sizeof(map
->values
[0]));
404 map
->values
= values
;
405 map
->capacity
= newcap
;
410 memmove(&map
->keys
[pos
+1], &map
->keys
[pos
],
411 (map
->size
-pos
)*sizeof(map
->keys
[0]));
412 memmove(&map
->values
[pos
+1], &map
->values
[pos
],
413 (map
->size
-pos
)*sizeof(map
->values
[0]));
417 map
->keys
[pos
] = key
;
418 map
->values
[pos
] = value
;
419 WriteUnlock(&map
->lock
);
424 ALint
RemovePtrIntMapKey(PtrIntMap
*map
, ALvoid
*key
)
427 WriteLock(&map
->lock
);
431 ALsizei count
= map
->size
;
433 ALsizei step
= count
>>1;
434 ALsizei i
= pos
+step
;
435 if(!(map
->keys
[i
] < key
))
443 if(pos
< map
->size
&& map
->keys
[pos
] == key
)
445 ret
= map
->values
[pos
];
446 if(pos
< map
->size
-1)
448 memmove(&map
->keys
[pos
], &map
->keys
[pos
+1],
449 (map
->size
-1-pos
)*sizeof(map
->keys
[0]));
450 memmove(&map
->values
[pos
], &map
->values
[pos
+1],
451 (map
->size
-1-pos
)*sizeof(map
->values
[0]));
456 WriteUnlock(&map
->lock
);
460 ALint
LookupPtrIntMapKey(PtrIntMap
*map
, ALvoid
*key
)
463 ReadLock(&map
->lock
);
467 ALsizei count
= map
->size
;
469 ALsizei step
= count
>>1;
470 ALsizei i
= pos
+step
;
471 if(!(map
->keys
[i
] < key
))
479 if(pos
< map
->size
&& map
->keys
[pos
] == key
)
480 ret
= map
->values
[pos
];
482 ReadUnlock(&map
->lock
);