Implement enumeration in the router
[openal-soft.git] / router / alc.c
blob438326bd4a77c4263c6af87b53fe3f706f1cccd8
2 #include "config.h"
4 #include <stddef.h>
5 #include <stdlib.h>
6 #include <string.h>
8 #include "AL/alc.h"
9 #include "router.h"
12 #define COUNTOF(x) (sizeof(x)/sizeof(x[0]))
14 #define DECL(x) { #x, (ALCvoid*)(x) }
15 static const struct {
16 const ALCchar *funcName;
17 ALCvoid *address;
18 } alcFunctions[] = {
19 DECL(alcCreateContext),
20 DECL(alcMakeContextCurrent),
21 DECL(alcProcessContext),
22 DECL(alcSuspendContext),
23 DECL(alcDestroyContext),
24 DECL(alcGetCurrentContext),
25 DECL(alcGetContextsDevice),
26 DECL(alcOpenDevice),
27 DECL(alcCloseDevice),
28 DECL(alcGetError),
29 DECL(alcIsExtensionPresent),
30 DECL(alcGetProcAddress),
31 DECL(alcGetEnumValue),
32 DECL(alcGetString),
33 DECL(alcGetIntegerv),
34 DECL(alcCaptureOpenDevice),
35 DECL(alcCaptureCloseDevice),
36 DECL(alcCaptureStart),
37 DECL(alcCaptureStop),
38 DECL(alcCaptureSamples),
40 DECL(alEnable),
41 DECL(alDisable),
42 DECL(alIsEnabled),
43 DECL(alGetString),
44 DECL(alGetBooleanv),
45 DECL(alGetIntegerv),
46 DECL(alGetFloatv),
47 DECL(alGetDoublev),
48 DECL(alGetBoolean),
49 DECL(alGetInteger),
50 DECL(alGetFloat),
51 DECL(alGetDouble),
52 DECL(alGetError),
53 DECL(alIsExtensionPresent),
54 DECL(alGetProcAddress),
55 DECL(alGetEnumValue),
56 DECL(alListenerf),
57 DECL(alListener3f),
58 DECL(alListenerfv),
59 DECL(alListeneri),
60 DECL(alListener3i),
61 DECL(alListeneriv),
62 DECL(alGetListenerf),
63 DECL(alGetListener3f),
64 DECL(alGetListenerfv),
65 DECL(alGetListeneri),
66 DECL(alGetListener3i),
67 DECL(alGetListeneriv),
68 DECL(alGenSources),
69 DECL(alDeleteSources),
70 DECL(alIsSource),
71 DECL(alSourcef),
72 DECL(alSource3f),
73 DECL(alSourcefv),
74 DECL(alSourcei),
75 DECL(alSource3i),
76 DECL(alSourceiv),
77 DECL(alGetSourcef),
78 DECL(alGetSource3f),
79 DECL(alGetSourcefv),
80 DECL(alGetSourcei),
81 DECL(alGetSource3i),
82 DECL(alGetSourceiv),
83 DECL(alSourcePlayv),
84 DECL(alSourceStopv),
85 DECL(alSourceRewindv),
86 DECL(alSourcePausev),
87 DECL(alSourcePlay),
88 DECL(alSourceStop),
89 DECL(alSourceRewind),
90 DECL(alSourcePause),
91 DECL(alSourceQueueBuffers),
92 DECL(alSourceUnqueueBuffers),
93 DECL(alGenBuffers),
94 DECL(alDeleteBuffers),
95 DECL(alIsBuffer),
96 DECL(alBufferData),
97 DECL(alBufferf),
98 DECL(alBuffer3f),
99 DECL(alBufferfv),
100 DECL(alBufferi),
101 DECL(alBuffer3i),
102 DECL(alBufferiv),
103 DECL(alGetBufferf),
104 DECL(alGetBuffer3f),
105 DECL(alGetBufferfv),
106 DECL(alGetBufferi),
107 DECL(alGetBuffer3i),
108 DECL(alGetBufferiv),
109 DECL(alDopplerFactor),
110 DECL(alDopplerVelocity),
111 DECL(alSpeedOfSound),
112 DECL(alDistanceModel),
114 #undef DECL
116 #define DECL(x) { #x, (x) }
117 static const struct {
118 const ALCchar *enumName;
119 ALCenum value;
120 } alcEnumerations[] = {
121 DECL(ALC_INVALID),
122 DECL(ALC_FALSE),
123 DECL(ALC_TRUE),
125 DECL(ALC_MAJOR_VERSION),
126 DECL(ALC_MINOR_VERSION),
127 DECL(ALC_ATTRIBUTES_SIZE),
128 DECL(ALC_ALL_ATTRIBUTES),
129 DECL(ALC_DEFAULT_DEVICE_SPECIFIER),
130 DECL(ALC_DEVICE_SPECIFIER),
131 DECL(ALC_ALL_DEVICES_SPECIFIER),
132 DECL(ALC_DEFAULT_ALL_DEVICES_SPECIFIER),
133 DECL(ALC_EXTENSIONS),
134 DECL(ALC_FREQUENCY),
135 DECL(ALC_REFRESH),
136 DECL(ALC_SYNC),
137 DECL(ALC_MONO_SOURCES),
138 DECL(ALC_STEREO_SOURCES),
139 DECL(ALC_CAPTURE_DEVICE_SPECIFIER),
140 DECL(ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER),
141 DECL(ALC_CAPTURE_SAMPLES),
143 DECL(ALC_NO_ERROR),
144 DECL(ALC_INVALID_DEVICE),
145 DECL(ALC_INVALID_CONTEXT),
146 DECL(ALC_INVALID_ENUM),
147 DECL(ALC_INVALID_VALUE),
148 DECL(ALC_OUT_OF_MEMORY),
150 DECL(AL_INVALID),
151 DECL(AL_NONE),
152 DECL(AL_FALSE),
153 DECL(AL_TRUE),
155 DECL(AL_SOURCE_RELATIVE),
156 DECL(AL_CONE_INNER_ANGLE),
157 DECL(AL_CONE_OUTER_ANGLE),
158 DECL(AL_PITCH),
159 DECL(AL_POSITION),
160 DECL(AL_DIRECTION),
161 DECL(AL_VELOCITY),
162 DECL(AL_LOOPING),
163 DECL(AL_BUFFER),
164 DECL(AL_GAIN),
165 DECL(AL_MIN_GAIN),
166 DECL(AL_MAX_GAIN),
167 DECL(AL_ORIENTATION),
168 DECL(AL_REFERENCE_DISTANCE),
169 DECL(AL_ROLLOFF_FACTOR),
170 DECL(AL_CONE_OUTER_GAIN),
171 DECL(AL_MAX_DISTANCE),
172 DECL(AL_SEC_OFFSET),
173 DECL(AL_SAMPLE_OFFSET),
174 DECL(AL_BYTE_OFFSET),
175 DECL(AL_SOURCE_TYPE),
176 DECL(AL_STATIC),
177 DECL(AL_STREAMING),
178 DECL(AL_UNDETERMINED),
180 DECL(AL_SOURCE_STATE),
181 DECL(AL_INITIAL),
182 DECL(AL_PLAYING),
183 DECL(AL_PAUSED),
184 DECL(AL_STOPPED),
186 DECL(AL_BUFFERS_QUEUED),
187 DECL(AL_BUFFERS_PROCESSED),
189 DECL(AL_FORMAT_MONO8),
190 DECL(AL_FORMAT_MONO16),
191 DECL(AL_FORMAT_STEREO8),
192 DECL(AL_FORMAT_STEREO16),
194 DECL(AL_FREQUENCY),
195 DECL(AL_BITS),
196 DECL(AL_CHANNELS),
197 DECL(AL_SIZE),
199 DECL(AL_UNUSED),
200 DECL(AL_PENDING),
201 DECL(AL_PROCESSED),
203 DECL(AL_NO_ERROR),
204 DECL(AL_INVALID_NAME),
205 DECL(AL_INVALID_ENUM),
206 DECL(AL_INVALID_VALUE),
207 DECL(AL_INVALID_OPERATION),
208 DECL(AL_OUT_OF_MEMORY),
210 DECL(AL_VENDOR),
211 DECL(AL_VERSION),
212 DECL(AL_RENDERER),
213 DECL(AL_EXTENSIONS),
215 DECL(AL_DOPPLER_FACTOR),
216 DECL(AL_DOPPLER_VELOCITY),
217 DECL(AL_DISTANCE_MODEL),
218 DECL(AL_SPEED_OF_SOUND),
220 DECL(AL_INVERSE_DISTANCE),
221 DECL(AL_INVERSE_DISTANCE_CLAMPED),
222 DECL(AL_LINEAR_DISTANCE),
223 DECL(AL_LINEAR_DISTANCE_CLAMPED),
224 DECL(AL_EXPONENT_DISTANCE),
225 DECL(AL_EXPONENT_DISTANCE_CLAMPED),
227 #undef DECL
229 static const ALCchar alcNoError[] = "No Error";
230 static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
231 static const ALCchar alcErrInvalidContext[] = "Invalid Context";
232 static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
233 static const ALCchar alcErrInvalidValue[] = "Invalid Value";
234 static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
235 static const ALCchar alcExtensionList[] =
236 "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE";
238 static const ALCint alcMajorVersion = 1;
239 static const ALCint alcMinorVersion = 1;
242 static ATOMIC(ALCenum) LastError = ATOMIC_INIT_STATIC(ALC_NO_ERROR);
243 PtrIntMap DeviceIfaceMap = PTRINTMAP_STATIC_INITIALIZE;
244 PtrIntMap ContextIfaceMap = PTRINTMAP_STATIC_INITIALIZE;
247 typedef struct EnumeratedList {
248 ALCchar *Names;
249 ALCchar *NamesEnd;
250 ALCint *Indicies;
251 ALCsizei IndexSize;
252 } EnumeratedList;
253 static EnumeratedList DevicesList = { NULL, NULL, NULL, 0 };
254 static EnumeratedList AllDevicesList = { NULL, NULL, NULL, 0 };
255 static EnumeratedList CaptureDevicesList = { NULL, NULL, NULL, 0 };
257 static void ClearDeviceList(EnumeratedList *list)
259 free(list->Names);
260 list->Names = NULL;
261 list->NamesEnd = NULL;
263 free(list->Indicies);
264 list->Indicies = NULL;
265 list->IndexSize = 0;
268 static void AppendDeviceList(EnumeratedList *list, const ALCchar *names, ALint idx)
270 const ALCchar *name_end = names;
271 ALCsizei count = 0;
272 ALCchar *new_list;
273 ALCint *new_indicies;
274 size_t len;
275 ALCsizei i;
277 if(!name_end)
278 return;
279 while(*name_end)
281 count++;
282 name_end += strlen(name_end)+1;
284 if(names == name_end)
285 return;
287 len = (list->NamesEnd - list->Names) + (name_end - names);
288 new_list = calloc(1, len + 1);
289 memcpy(new_list, list->Names, list->NamesEnd - list->Names);
290 memcpy(new_list + (list->NamesEnd - list->Names), names, name_end - names);
291 free(list->Names);
292 list->Names = new_list;
293 list->NamesEnd = list->Names + len;
295 new_indicies = calloc(sizeof(ALCint), list->IndexSize + count);
296 for(i = 0;i < list->IndexSize;i++)
297 new_indicies[i] = list->Indicies[i];
298 for(i = 0;i < count;i++)
299 new_indicies[list->IndexSize+i] = idx;
300 free(list->Indicies);
301 list->Indicies = new_indicies;
302 list->IndexSize += count;
306 ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *devicename)
308 return NULL;
311 ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *device)
313 return ALC_FALSE;
317 ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrlist)
319 return NULL;
322 ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
324 return ALC_FALSE;
327 ALC_API void ALC_APIENTRY alcProcessContext(ALCcontext *context)
329 if(context)
331 ALint idx = LookupPtrIntMapKey(&ContextIfaceMap, context);
332 if(idx >= 0)
333 return DriverList[idx].alcProcessContext(context);
335 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_CONTEXT);
338 ALC_API void ALC_APIENTRY alcSuspendContext(ALCcontext *context)
340 if(context)
342 ALint idx = LookupPtrIntMapKey(&ContextIfaceMap, context);
343 if(idx >= 0)
344 return DriverList[idx].alcSuspendContext(context);
346 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_CONTEXT);
349 ALC_API void ALC_APIENTRY alcDestroyContext(ALCcontext *context)
353 ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(void)
355 return NULL;
358 ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *context)
360 if(context)
362 ALint idx = LookupPtrIntMapKey(&ContextIfaceMap, context);
363 if(idx >= 0)
364 return DriverList[idx].alcGetContextsDevice(context);
366 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_CONTEXT);
367 return NULL;
371 ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
373 if(device)
375 ALint idx = LookupPtrIntMapKey(&DeviceIfaceMap, device);
376 if(idx < 0) return ALC_INVALID_DEVICE;
377 return DriverList[idx].alcGetError(device);
379 return ATOMIC_EXCHANGE_SEQ(&LastError, ALC_NO_ERROR);
382 ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extname)
384 const char *ptr;
385 size_t len;
387 if(device)
389 ALint idx = LookupPtrIntMapKey(&DeviceIfaceMap, device);
390 if(idx < 0)
392 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_DEVICE);
393 return ALC_FALSE;
395 return DriverList[idx].alcIsExtensionPresent(device, extname);
398 len = strlen(extname);
399 ptr = alcExtensionList;
400 while(ptr && *ptr)
402 if(strncasecmp(ptr, extname, len) == 0 && (ptr[len] == '\0' || isspace(ptr[len])))
403 return ALC_TRUE;
404 if((ptr=strchr(ptr, ' ')) != NULL)
406 do {
407 ++ptr;
408 } while(isspace(*ptr));
411 return ALC_FALSE;
414 ALC_API void* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcname)
416 size_t i;
418 if(device)
420 ALint idx = LookupPtrIntMapKey(&DeviceIfaceMap, device);
421 if(idx < 0)
423 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_DEVICE);
424 return NULL;
426 return DriverList[idx].alcGetProcAddress(device, funcname);
429 for(i = 0;i < COUNTOF(alcFunctions);i++)
431 if(strcmp(funcname, alcFunctions[i].funcName) == 0)
432 return alcFunctions[i].address;
434 return NULL;
437 ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumname)
439 size_t i;
441 if(device)
443 ALint idx = LookupPtrIntMapKey(&DeviceIfaceMap, device);
444 if(idx < 0)
446 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_DEVICE);
447 return 0;
449 return DriverList[idx].alcGetEnumValue(device, enumname);
452 for(i = 0;i < COUNTOF(alcEnumerations);i++)
454 if(strcmp(enumname, alcEnumerations[i].enumName) == 0)
455 return alcEnumerations[i].value;
457 return 0;
460 ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *device, ALCenum param)
462 ALsizei i = 0;
464 if(device)
466 ALint idx = LookupPtrIntMapKey(&DeviceIfaceMap, device);
467 if(idx < 0)
469 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_DEVICE);
470 return NULL;
472 return DriverList[idx].alcGetString(device, param);
475 switch(param)
477 case ALC_NO_ERROR:
478 return alcNoError;
479 case ALC_INVALID_ENUM:
480 return alcErrInvalidEnum;
481 case ALC_INVALID_VALUE:
482 return alcErrInvalidValue;
483 case ALC_INVALID_DEVICE:
484 return alcErrInvalidDevice;
485 case ALC_INVALID_CONTEXT:
486 return alcErrInvalidContext;
487 case ALC_OUT_OF_MEMORY:
488 return alcErrOutOfMemory;
489 case ALC_EXTENSIONS:
490 return "";
492 case ALC_DEVICE_SPECIFIER:
493 ClearDeviceList(&DevicesList);
494 for(i = 0;i < DriverListSize;i++)
496 /* Only enumerate names from drivers that support it.
497 * FIXME: Check for ALC 1.1 too, since that guarantees enumeration
498 * support.
500 if(DriverList[i].alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT"))
501 AppendDeviceList(&DevicesList,
502 DriverList[i].alcGetString(NULL, ALC_DEVICE_SPECIFIER), i
505 return DevicesList.Names;
507 case ALC_ALL_DEVICES_SPECIFIER:
508 ClearDeviceList(&AllDevicesList);
509 for(i = 0;i < DriverListSize;i++)
511 /* If the driver doesn't support ALC_ENUMERATE_ALL_EXT, substitute
512 * standard enumeration.
514 if(DriverList[i].alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT"))
515 AppendDeviceList(&AllDevicesList,
516 DriverList[i].alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER), i
518 else if(DriverList[i].alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT"))
519 AppendDeviceList(&AllDevicesList,
520 DriverList[i].alcGetString(NULL, ALC_DEVICE_SPECIFIER), i
523 return AllDevicesList.Names;
525 case ALC_CAPTURE_DEVICE_SPECIFIER:
526 ClearDeviceList(&CaptureDevicesList);
527 for(i = 0;i < DriverListSize;i++)
529 if(DriverList[i].alcIsExtensionPresent(NULL, "ALC_EXT_CAPTURE"))
530 AppendDeviceList(&CaptureDevicesList,
531 DriverList[i].alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER), i
534 return CaptureDevicesList.Names;
536 case ALC_DEFAULT_DEVICE_SPECIFIER:
537 case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
538 case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
539 if(DriverListSize > 0)
540 return DriverList[0].alcGetString(NULL, param);
541 return "";
543 default:
544 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_ENUM);
545 break;
547 return NULL;
550 ALC_API void ALC_APIENTRY alcGetIntegerv(ALCdevice *device, ALCenum param, ALCsizei size, ALCint *values)
552 if(device)
554 ALint idx = LookupPtrIntMapKey(&DeviceIfaceMap, device);
555 if(idx < 0)
557 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_DEVICE);
558 return;
560 return DriverList[idx].alcGetIntegerv(device, param, size, values);
563 if(size <= 0 || values == NULL)
565 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_VALUE);
566 return;
569 switch(param)
571 case ALC_MAJOR_VERSION:
572 if(size >= 1)
574 values[0] = alcMajorVersion;
575 return;
577 /*fall-through*/
578 case ALC_MINOR_VERSION:
579 if(size >= 1)
581 values[0] = alcMinorVersion;
582 return;
584 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_VALUE);
585 return;
587 case ALC_ATTRIBUTES_SIZE:
588 case ALC_ALL_ATTRIBUTES:
589 case ALC_FREQUENCY:
590 case ALC_REFRESH:
591 case ALC_SYNC:
592 case ALC_MONO_SOURCES:
593 case ALC_STEREO_SOURCES:
594 case ALC_CAPTURE_SAMPLES:
595 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_DEVICE);
596 return;
598 default:
599 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_ENUM);
600 return;
605 ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *devicename, ALCuint frequency, ALCenum format, ALCsizei buffersize)
607 return NULL;
610 ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *device)
612 return ALC_FALSE;
615 ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
617 if(device)
619 ALint idx = LookupPtrIntMapKey(&DeviceIfaceMap, device);
620 if(idx >= 0)
621 return DriverList[idx].alcCaptureStart(device);
623 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_DEVICE);
626 ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
628 if(device)
630 ALint idx = LookupPtrIntMapKey(&DeviceIfaceMap, device);
631 if(idx >= 0)
632 return DriverList[idx].alcCaptureStop(device);
634 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_DEVICE);
637 ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
639 if(device)
641 ALint idx = LookupPtrIntMapKey(&DeviceIfaceMap, device);
642 if(idx >= 0)
643 return DriverList[idx].alcCaptureSamples(device, buffer, samples);
645 ATOMIC_STORE_SEQ(&LastError, ALC_INVALID_DEVICE);