Revert "Try all drivers for an unknown device name"
[openal-soft.git] / router / router.c
blob54f82245314109570ff262ccd5d423d3860d087f
2 #include "config.h"
4 #include "router.h"
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
10 #include "AL/alc.h"
11 #include "AL/al.h"
12 #include "almalloc.h"
15 DriverIface *DriverList = NULL;
16 int DriverListSize = 0;
17 static int DriverListSizeMax = 0;
19 enum LogLevel LogLevel = LogLevel_Error;
20 FILE *LogFile;
22 static void LoadDriverList(void);
25 BOOL APIENTRY DllMain(HINSTANCE UNUSED(module), DWORD reason, void* UNUSED(reserved))
27 const char *str;
28 int i;
30 switch(reason)
32 case DLL_PROCESS_ATTACH:
33 LogFile = stderr;
34 str = getenv("ALROUTER_LOGFILE");
35 if(str && *str != '\0')
37 FILE *f = fopen(str, "w");
38 if(f == NULL)
39 ERR("Could not open log file: %s\n", str);
40 else
41 LogFile = f;
43 str = getenv("ALROUTER_LOGLEVEL");
44 if(str && *str != '\0')
46 char *end = NULL;
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);
52 else
53 LogLevel = l;
55 LoadDriverList();
57 InitALC();
58 break;
60 case DLL_THREAD_ATTACH:
61 case DLL_THREAD_DETACH:
62 break;
64 case DLL_PROCESS_DETACH:
65 ReleaseALC();
67 for(i = 0;i < DriverListSize;i++)
69 if(DriverList[i].Module)
70 FreeLibrary(DriverList[i].Module);
72 al_free(DriverList);
73 DriverList = NULL;
74 DriverListSize = 0;
75 DriverListSizeMax = 0;
77 if(LogFile && LogFile != stderr)
78 fclose(LogFile);
79 LogFile = NULL;
80 break;
82 return TRUE;
86 #ifdef __GNUC__
87 #define CAST_FUNC(x) (__typeof(x))
88 #else
89 #define CAST_FUNC(x) (void*)
90 #endif
92 static void AddModule(HMODULE module, const WCHAR *name)
94 DriverIface newdrv;
95 int err = 0;
96 int i;
98 for(i = 0;i < DriverListSize;i++)
100 if(DriverList[i].Module == module)
102 TRACE("Skipping already-loaded module %p\n", module);
103 FreeLibrary(module);
104 return;
106 if(wcscmp(DriverList[i].Name, name) == 0)
108 TRACE("Skipping similarly-named module %ls\n", name);
109 FreeLibrary(module);
110 return;
114 if(DriverListSize == DriverListSizeMax)
116 int newmax = DriverListSizeMax ? DriverListSizeMax<<1 : 4;
117 void *newlist = al_calloc(DEF_ALIGN, sizeof(DriverList[0])*newmax);
118 if(!newlist) return;
120 memcpy(newlist, DriverList, DriverListSize*sizeof(DriverList[0]));
121 al_free(DriverList);
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); \
129 if(!newdrv.x) \
131 ERR("Failed to find entry point for %s in %ls\n", #x, name); \
132 err = 1; \
134 } while(0)
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);
156 LOAD_PROC(alEnable);
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);
229 if(!err)
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]);
238 else
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;
250 HANDLE srchHdl;
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)
258 do {
259 HMODULE mod;
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);
267 if(!mod)
268 WARN("Could not load %ls\n", srchPath);
269 else
270 AddModule(mod, fdata.cFileName);
271 } while(FindNextFileW(srchHdl, &fdata));
272 FindClose(srchHdl);
276 static WCHAR *strrchrW(WCHAR *str, WCHAR ch)
278 WCHAR *res = NULL;
279 while(str && *str != '\0')
281 if(*str == ch)
282 res = str;
283 ++str;
285 return res;
288 static int GetLoadedModuleDirectory(const WCHAR *name, WCHAR *moddir, DWORD length)
290 HMODULE module = NULL;
291 WCHAR *sep0, *sep1;
293 if(name)
295 module = GetModuleHandleW(name);
296 if(!module) return 0;
299 if(GetModuleFileNameW(module, moddir, length) == 0)
300 return 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';
308 else *moddir = '\0';
310 return 1;
313 void LoadDriverList(void)
315 WCHAR path[MAX_PATH+1] = L"";
316 int len;
318 if(GetLoadedModuleDirectory(L"OpenAL32.dll", path, MAX_PATH))
319 SearchDrivers(path);
321 GetCurrentDirectoryW(MAX_PATH, path);
322 len = lstrlenW(path);
323 if(len > 0 && (path[len-1] == '\\' || path[len-1] == '/'))
324 path[len-1] = '\0';
325 SearchDrivers(path);
327 if(GetLoadedModuleDirectory(NULL, path, MAX_PATH))
328 SearchDrivers(path);
330 GetSystemDirectoryW(path, MAX_PATH);
331 len = lstrlenW(path);
332 if(len > 0 && (path[len-1] == '\\' || path[len-1] == '/'))
333 path[len-1] = '\0';
334 SearchDrivers(path);
338 void InitPtrIntMap(PtrIntMap *map)
340 map->keys = NULL;
341 map->values = NULL;
342 map->size = 0;
343 map->capacity = 0;
344 RWLockInit(&map->lock);
347 void ResetPtrIntMap(PtrIntMap *map)
349 WriteLock(&map->lock);
350 al_free(map->keys);
351 map->keys = NULL;
352 map->values = NULL;
353 map->size = 0;
354 map->capacity = 0;
355 WriteUnlock(&map->lock);
358 ALenum InsertPtrIntMapEntry(PtrIntMap *map, ALvoid *key, ALint value)
360 ALsizei pos = 0;
362 WriteLock(&map->lock);
363 if(map->size > 0)
365 ALsizei count = map->size;
366 do {
367 ALsizei step = count>>1;
368 ALsizei i = pos+step;
369 if(!(map->keys[i] < key))
370 count = step;
371 else
373 pos = i+1;
374 count -= step+1;
376 } while(count > 0);
379 if(pos == map->size || map->keys[pos] != key)
381 if(map->size == map->capacity)
383 ALvoid **keys = NULL;
384 ALint *values;
385 ALsizei newcap;
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);
390 if(!keys)
392 WriteUnlock(&map->lock);
393 return AL_OUT_OF_MEMORY;
395 values = (ALint*)&keys[newcap];
397 if(map->keys)
399 memcpy(keys, map->keys, map->size*sizeof(map->keys[0]));
400 memcpy(values, map->values, map->size*sizeof(map->values[0]));
402 al_free(map->keys);
403 map->keys = keys;
404 map->values = values;
405 map->capacity = newcap;
408 if(pos < map->size)
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]));
415 map->size++;
417 map->keys[pos] = key;
418 map->values[pos] = value;
419 WriteUnlock(&map->lock);
421 return AL_NO_ERROR;
424 ALint RemovePtrIntMapKey(PtrIntMap *map, ALvoid *key)
426 ALint ret = -1;
427 WriteLock(&map->lock);
428 if(map->size > 0)
430 ALsizei pos = 0;
431 ALsizei count = map->size;
432 do {
433 ALsizei step = count>>1;
434 ALsizei i = pos+step;
435 if(!(map->keys[i] < key))
436 count = step;
437 else
439 pos = i+1;
440 count -= step+1;
442 } while(count > 0);
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]));
453 map->size--;
456 WriteUnlock(&map->lock);
457 return ret;
460 ALint LookupPtrIntMapKey(PtrIntMap *map, ALvoid *key)
462 ALint ret = -1;
463 ReadLock(&map->lock);
464 if(map->size > 0)
466 ALsizei pos = 0;
467 ALsizei count = map->size;
468 do {
469 ALsizei step = count>>1;
470 ALsizei i = pos+step;
471 if(!(map->keys[i] < key))
472 count = step;
473 else
475 pos = i+1;
476 count -= step+1;
478 } while(count > 0);
479 if(pos < map->size && map->keys[pos] == key)
480 ret = map->values[pos];
482 ReadUnlock(&map->lock);
483 return ret;