Implement enumeration in the router
[openal-soft.git] / router / router.c
blob3adf64e2218850164a0f5bea158fcad5ef699f26
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"
14 DriverIface *DriverList = NULL;
15 int DriverListSize = 0;
16 static int DriverListSizeMax = 0;
18 static void LoadDriverList(void);
21 BOOL APIENTRY DllMain(HINSTANCE module, DWORD reason, void *reserved)
23 int i;
25 switch(reason)
27 case DLL_PROCESS_ATTACH:
28 LoadDriverList();
29 break;
31 case DLL_THREAD_ATTACH:
32 case DLL_THREAD_DETACH:
33 break;
35 case DLL_PROCESS_DETACH:
36 ResetPtrIntMap(&ContextIfaceMap);
37 ResetPtrIntMap(&DeviceIfaceMap);
38 for(i = 0;i < DriverListSize;i++)
40 if(DriverList[i].Module)
41 FreeLibrary(DriverList[i].Module);
43 free(DriverList);
44 DriverList = NULL;
45 DriverListSize = 0;
46 DriverListSizeMax = 0;
47 break;
49 return TRUE;
53 #ifdef __GNUC__
54 #define CAST_FUNC(x) (__typeof(x))
55 #else
56 #define CAST_FUNC(x) (void*)
57 #endif
59 static void AddModule(HMODULE module, const WCHAR *name)
61 int err = 0;
62 int i;
64 for(i = 0;i < DriverListSize;i++)
66 if(DriverList[i].Module == module || wcscmp(DriverList[i].Name, name) == 0)
68 FreeLibrary(module);
69 return;
73 if(DriverListSize == DriverListSizeMax)
75 int newmax = DriverListSizeMax ? DriverListSizeMax<<1 : 4;
76 void *newlist = calloc(sizeof(DriverList[0]), newmax);
77 if(!newlist) return;
79 memcpy(newlist, DriverList, DriverListSize*sizeof(DriverList[0]));
80 free(DriverList);
81 DriverList = newlist;
82 DriverListSizeMax = newmax;
85 #define LOAD_PROC(x) do { \
86 DriverList[DriverListSize].x = CAST_FUNC(DriverList[DriverListSize].x) \
87 GetProcAddress(module, #x); \
88 if(!DriverList[DriverListSize].x) \
89 { \
90 fprintf(stderr, "Failed to find entry point for %s in %ls\n", \
91 #x, name); \
92 err = 1; \
93 } \
94 } while(0)
95 LOAD_PROC(alcCreateContext);
96 LOAD_PROC(alcMakeContextCurrent);
97 LOAD_PROC(alcProcessContext);
98 LOAD_PROC(alcSuspendContext);
99 LOAD_PROC(alcDestroyContext);
100 LOAD_PROC(alcGetCurrentContext);
101 LOAD_PROC(alcGetContextsDevice);
102 LOAD_PROC(alcOpenDevice);
103 LOAD_PROC(alcCloseDevice);
104 LOAD_PROC(alcGetError);
105 LOAD_PROC(alcIsExtensionPresent);
106 LOAD_PROC(alcGetProcAddress);
107 LOAD_PROC(alcGetEnumValue);
108 LOAD_PROC(alcGetString);
109 LOAD_PROC(alcGetIntegerv);
110 LOAD_PROC(alcCaptureOpenDevice);
111 LOAD_PROC(alcCaptureCloseDevice);
112 LOAD_PROC(alcCaptureStart);
113 LOAD_PROC(alcCaptureStop);
114 LOAD_PROC(alcCaptureSamples);
116 LOAD_PROC(alEnable);
117 LOAD_PROC(alDisable);
118 LOAD_PROC(alIsEnabled);
119 LOAD_PROC(alGetString);
120 LOAD_PROC(alGetBooleanv);
121 LOAD_PROC(alGetIntegerv);
122 LOAD_PROC(alGetFloatv);
123 LOAD_PROC(alGetDoublev);
124 LOAD_PROC(alGetBoolean);
125 LOAD_PROC(alGetInteger);
126 LOAD_PROC(alGetFloat);
127 LOAD_PROC(alGetDouble);
128 LOAD_PROC(alGetError);
129 LOAD_PROC(alIsExtensionPresent);
130 LOAD_PROC(alGetProcAddress);
131 LOAD_PROC(alGetEnumValue);
132 LOAD_PROC(alListenerf);
133 LOAD_PROC(alListener3f);
134 LOAD_PROC(alListenerfv);
135 LOAD_PROC(alListeneri);
136 LOAD_PROC(alListener3i);
137 LOAD_PROC(alListeneriv);
138 LOAD_PROC(alGetListenerf);
139 LOAD_PROC(alGetListener3f);
140 LOAD_PROC(alGetListenerfv);
141 LOAD_PROC(alGetListeneri);
142 LOAD_PROC(alGetListener3i);
143 LOAD_PROC(alGetListeneriv);
144 LOAD_PROC(alGenSources);
145 LOAD_PROC(alDeleteSources);
146 LOAD_PROC(alIsSource);
147 LOAD_PROC(alSourcef);
148 LOAD_PROC(alSource3f);
149 LOAD_PROC(alSourcefv);
150 LOAD_PROC(alSourcei);
151 LOAD_PROC(alSource3i);
152 LOAD_PROC(alSourceiv);
153 LOAD_PROC(alGetSourcef);
154 LOAD_PROC(alGetSource3f);
155 LOAD_PROC(alGetSourcefv);
156 LOAD_PROC(alGetSourcei);
157 LOAD_PROC(alGetSource3i);
158 LOAD_PROC(alGetSourceiv);
159 LOAD_PROC(alSourcePlayv);
160 LOAD_PROC(alSourceStopv);
161 LOAD_PROC(alSourceRewindv);
162 LOAD_PROC(alSourcePausev);
163 LOAD_PROC(alSourcePlay);
164 LOAD_PROC(alSourceStop);
165 LOAD_PROC(alSourceRewind);
166 LOAD_PROC(alSourcePause);
167 LOAD_PROC(alSourceQueueBuffers);
168 LOAD_PROC(alSourceUnqueueBuffers);
169 LOAD_PROC(alGenBuffers);
170 LOAD_PROC(alDeleteBuffers);
171 LOAD_PROC(alIsBuffer);
172 LOAD_PROC(alBufferf);
173 LOAD_PROC(alBuffer3f);
174 LOAD_PROC(alBufferfv);
175 LOAD_PROC(alBufferi);
176 LOAD_PROC(alBuffer3i);
177 LOAD_PROC(alBufferiv);
178 LOAD_PROC(alGetBufferf);
179 LOAD_PROC(alGetBuffer3f);
180 LOAD_PROC(alGetBufferfv);
181 LOAD_PROC(alGetBufferi);
182 LOAD_PROC(alGetBuffer3i);
183 LOAD_PROC(alGetBufferiv);
184 LOAD_PROC(alBufferData);
185 LOAD_PROC(alDopplerFactor);
186 LOAD_PROC(alDopplerVelocity);
187 LOAD_PROC(alSpeedOfSound);
188 LOAD_PROC(alDistanceModel);
189 if(!err)
191 wcsncpy(DriverList[DriverListSize].Name, name, 32);
192 DriverList[DriverListSize++].Module = module;
196 static void SearchDrivers(WCHAR *path)
198 WCHAR srchPath[MAX_PATH+1] = L"";
199 WIN32_FIND_DATAW fdata;
200 HANDLE srchHdl;
202 wcsncpy(srchPath, path, MAX_PATH);
203 wcsncat(srchPath, L"\\*oal.dll", MAX_PATH - lstrlenW(srchPath));
204 srchHdl = FindFirstFileW(srchPath, &fdata);
205 if(srchHdl != INVALID_HANDLE_VALUE)
207 do {
208 HMODULE mod;
210 wcsncpy(srchPath, path, MAX_PATH);
211 wcsncat(srchPath, L"\\", MAX_PATH - lstrlenW(srchPath));
212 wcsncat(srchPath, fdata.cFileName, MAX_PATH - lstrlenW(srchPath));
214 mod = LoadLibraryW(srchPath);
215 if(mod) AddModule(mod, fdata.cFileName);
216 } while(FindNextFileW(srchHdl, &fdata));
217 FindClose(srchHdl);
221 static WCHAR *strrchrW(WCHAR *str, WCHAR ch)
223 WCHAR *res = NULL;
224 while(str && *str != '\0')
226 if(*str == ch)
227 res = str;
228 ++str;
230 return res;
233 static int GetLoadedModuleDirectory(const WCHAR *name, WCHAR *moddir, DWORD length)
235 HMODULE module = NULL;
236 WCHAR *sep0, *sep1;
238 if(name)
240 module = GetModuleHandleW(name);
241 if(!module) return 0;
244 if(GetModuleFileNameW(module, moddir, length) == 0)
245 return 0;
247 sep0 = strrchrW(moddir, '/');
248 if(sep0) sep1 = strrchrW(sep0+1, '\\');
249 else sep1 = strrchrW(moddir, '\\');
251 if(sep1) *sep1 = '\0';
252 else if(sep0) *sep0 = '\0';
253 else *moddir = '\0';
255 return 1;
258 void LoadDriverList(void)
260 WCHAR path[MAX_PATH+1] = L"";
261 int len;
263 if(GetLoadedModuleDirectory(L"OpenAL32.dll", path, MAX_PATH))
264 SearchDrivers(path);
266 GetCurrentDirectoryW(MAX_PATH, path);
267 len = lstrlenW(path);
268 if(len > 0 && (path[len-1] == '\\' || path[len-1] == '/'))
269 path[len-1] = '\0';
270 SearchDrivers(path);
272 if(GetLoadedModuleDirectory(NULL, path, MAX_PATH))
273 SearchDrivers(path);
275 GetSystemDirectoryW(path, MAX_PATH);
276 len = lstrlenW(path);
277 if(len > 0 && (path[len-1] == '\\' || path[len-1] == '/'))
278 path[len-1] = '\0';
279 SearchDrivers(path);
283 void InitPtrIntMap(PtrIntMap *map)
285 map->keys = NULL;
286 map->values = NULL;
287 map->size = 0;
288 map->capacity = 0;
289 RWLockInit(&map->lock);
292 void ResetPtrIntMap(PtrIntMap *map)
294 WriteLock(&map->lock);
295 free(map->keys);
296 map->keys = NULL;
297 map->values = NULL;
298 map->size = 0;
299 map->capacity = 0;
300 WriteUnlock(&map->lock);
303 ALenum InsertPtrIntMapEntry(PtrIntMap *map, ALvoid *key, ALint value)
305 ALsizei pos = 0;
307 WriteLock(&map->lock);
308 if(map->size > 0)
310 ALsizei count = map->size;
311 do {
312 ALsizei step = count>>1;
313 ALsizei i = pos+step;
314 if(!(map->keys[i] < key))
315 count = step;
316 else
318 pos = i+1;
319 count -= step+1;
321 } while(count > 0);
324 if(pos == map->size || map->keys[pos] != key)
326 if(map->size == map->capacity)
328 ALvoid **keys = NULL;
329 ALint *values;
330 ALsizei newcap;
332 newcap = (map->capacity ? (map->capacity<<1) : 4);
333 if(newcap > map->capacity)
334 keys = calloc(sizeof(map->keys[0])+sizeof(map->values[0]), newcap);
335 if(!keys)
337 WriteUnlock(&map->lock);
338 return AL_OUT_OF_MEMORY;
340 values = (ALint*)&keys[newcap];
342 if(map->keys)
344 memcpy(keys, map->keys, map->size*sizeof(map->keys[0]));
345 memcpy(values, map->values, map->size*sizeof(map->values[0]));
347 free(map->keys);
348 map->keys = keys;
349 map->values = values;
350 map->capacity = newcap;
353 if(pos < map->size)
355 memmove(&map->keys[pos+1], &map->keys[pos],
356 (map->size-pos)*sizeof(map->keys[0]));
357 memmove(&map->values[pos+1], &map->values[pos],
358 (map->size-pos)*sizeof(map->values[0]));
360 map->size++;
362 map->keys[pos] = key;
363 map->values[pos] = value;
364 WriteUnlock(&map->lock);
366 return AL_NO_ERROR;
369 ALint RemovePtrIntMapKey(PtrIntMap *map, ALvoid *key)
371 ALint ret = -1;
372 WriteLock(&map->lock);
373 if(map->size > 0)
375 ALsizei pos = 0;
376 ALsizei count = map->size;
377 do {
378 ALsizei step = count>>1;
379 ALsizei i = pos+step;
380 if(!(map->keys[i] < key))
381 count = step;
382 else
384 pos = i+1;
385 count -= step+1;
387 } while(count > 0);
388 if(pos < map->size && map->keys[pos] == key)
390 ret = map->values[pos];
391 if(pos < map->size-1)
393 memmove(&map->keys[pos], &map->keys[pos+1],
394 (map->size-1-pos)*sizeof(map->keys[0]));
395 memmove(&map->values[pos], &map->values[pos+1],
396 (map->size-1-pos)*sizeof(map->values[0]));
398 map->size--;
401 WriteUnlock(&map->lock);
402 return ret;
405 ALint LookupPtrIntMapKey(PtrIntMap *map, ALvoid *key)
407 ALint ret = -1;
408 ReadLock(&map->lock);
409 if(map->size > 0)
411 ALsizei pos = 0;
412 ALsizei count = map->size;
413 do {
414 ALsizei step = count>>1;
415 ALsizei i = pos+step;
416 if(!(map->keys[i] < key))
417 count = step;
418 else
420 pos = i+1;
421 count -= step+1;
423 } while(count > 0);
424 if(pos < map->size && map->keys[pos] == key)
425 ret = map->values[pos];
427 ReadUnlock(&map->lock);
428 return ret;