Torque3D Documentation / _generateds / x86UNIXOpenAL.client.cpp

x86UNIXOpenAL.client.cpp

Engine/source/platformX86UNIX/x86UNIXOpenAL.client.cpp

More...

Detailed Description

  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2012 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23#if 0
 24#include "platformX86UNIX/platformX86UNIX.h"
 25#include "console/console.h"
 26
 27#include <dlfcn.h>
 28
 29#include <al/altypes.h>
 30#include <al/alctypes.h>
 31#define INITGUID
 32#include <al/eaxtypes.h>
 33
 34
 35// Define the OpenAL and Extension Stub functions
 36#define AL_FUNCTION(fn_return, fn_name, fn_args, fn_value) fn_return stub_##fn_name fn_args{ fn_value }
 37#include <al/al_func.h>
 38#include <al/alc_func.h>
 39#include <al/eax_func.h>
 40#undef AL_FUNCTION
 41
 42
 43// Declare the OpenAL and Extension Function pointers
 44// And initialize them to the stub functions
 45#define AL_FUNCTION(fn_return,fn_name,fn_args, fn_value) fn_return (*fn_name)fn_args = stub_##fn_name;
 46#include <al/al_func.h>
 47#include <al/alc_func.h>
 48#include <al/eax_func.h>
 49#undef AL_FUNCTION
 50
 51// Declarations for the "emulated" functions (al functions that don't 
 52// exist in the loki openal implementation)
 53ALboolean emu_alGetBoolean(ALenum param);
 54ALint emu_alGetInteger(ALenum param);
 55ALfloat emu_alGetFloat(ALenum param);
 56ALdouble emu_alGetDouble(ALenum param);
 57void emu_alListeneri( ALenum param, ALint value );
 58void emu_alGetListener3f(ALenum pname,ALfloat *v1,ALfloat *v2,ALfloat *v3);
 59ALCdevice* emu_alcGetContextsDevice(ALCcontext *context);
 60
 61static void *dlHandle = NULL;
 62static char* dlError = "no error";
 63
 64/*!   Get an "emulated" function address and bind it to the function pointer
 65*/
 66static bool bindEmulatedFunction(void *&fnAddress, const char *name)
 67{
 68   fnAddress = NULL;
 69
 70   if (dStrcmp(name, "alGetBoolean")==0)
 71      fnAddress = (void*)&emu_alGetBoolean;
 72   else if (dStrcmp(name, "alGetInteger")==0)
 73      fnAddress = (void*)&emu_alGetInteger;
 74   else if (dStrcmp(name, "alGetFloat")==0)
 75      fnAddress = (void*)&emu_alGetFloat;
 76   else if (dStrcmp(name, "alGetDouble")==0)
 77      fnAddress = (void*)&emu_alGetDouble;
 78   else if (dStrcmp(name, "alListeneri")==0)
 79      fnAddress = (void*)&emu_alListeneri;
 80   else if (dStrcmp(name, "alGetListener3f")==0)
 81      fnAddress = (void*)&emu_alGetListener3f;
 82   else if (dStrcmp(name, "alcGetContextsDevice")==0)
 83      fnAddress = (void*)&emu_alcGetContextsDevice;
 84
 85   return fnAddress != NULL;
 86}
 87
 88/*!   Get a function address from the OpenAL DLL and bind it to the 
 89*     function pointer
 90*/
 91static bool bindFunction( void *&fnAddress, const char *name )
 92{
 93   fnAddress = dlsym(dlHandle, name);
 94   if( !fnAddress )
 95      if (bindEmulatedFunction(fnAddress, name))
 96         ConsoleLogEntry::General, " Missing OpenAL function '%s', using emulated function", name);
 97      else
 98         ConsoleLogEntry::General, " Missing OpenAL function '%s'", name);
 99   return (fnAddress != NULL);
100}
101
102/*!   Get a function address for an OpenAL extension function and bind it 
103*     to it's function pointer
104*/
105static bool bindExtensionFunction( void *&fnAddress, const char *name )
106{
107   fnAddress = alGetProcAddress( (ALubyte*)name );
108   if( !fnAddress )
109      ConsoleLogEntry::General, " Missing OpenAL Extension function '%s'", name);
110   return (fnAddress != NULL);
111}
112
113/*!   Bind the functions in the OpenAL DLL to the al interface functions  
114*/
115static bool bindOpenALFunctions()
116{
117   bool result = true;
118   #define AL_FUNCTION(fn_return, fn_name, fn_args, fn_value) result &= bindFunction( *(void**)&fn_name, #fn_name);
119   #include <al/al_func.h>
120   #include <al/alc_func.h>
121   #undef AL_FUNCTION
122   return result;
123}
124
125/*!   Bind the stub functions to the al interface functions  
126*/
127static void unbindOpenALFunctions()
128{
129   #define AL_FUNCTION(fn_return, fn_name, fn_args, fn_value) fn_name = stub_##fn_name;
130   #include <al/al_func.h>
131   #include <al/alc_func.h>
132   #include <al/eax_func.h>
133   #undef AL_FUNCTION
134}
135
136/*!   Bind the EAX Extension functions to the EAX interface functions  
137*/
138static bool bindEAXFunctions()
139{
140   bool result = true;
141   #define AL_FUNCTION(fn_return, fn_name, fn_args, fn_value) result &= bindExtensionFunction( *(void**)&fn_name, #fn_name);
142   #include <al/eax_func.h>
143   #undef AL_FUNCTION
144   return result;
145}
146
147// Definitions for the emulated functions
148ALboolean emu_alGetBoolean(ALenum param)
149{
150   ALboolean alboolean;
151   alGetBooleanv(param, &alboolean);
152   return alboolean;
153}
154
155ALint emu_alGetInteger(ALenum param)
156{
157   ALint alint;
158   alGetIntegerv(param, &alint);
159   return alint;
160}
161
162ALfloat emu_alGetFloat(ALenum param)
163{
164   ALfloat alfloat;
165   alGetFloatv(param, &alfloat);
166   return alfloat;
167}
168
169ALdouble emu_alGetDouble(ALenum param)
170{
171   ALdouble aldouble;
172   alGetDoublev(param, &aldouble);
173   return aldouble;
174}
175
176void emu_alGetListener3f(ALenum pname,ALfloat *v0,ALfloat *v1,ALfloat *v2)
177{
178   ALfloat ptArray[10];
179   ptArray[0] = *v0;
180   ptArray[1] = *v1;
181   ptArray[2] = *v2;
182   alGetListenerfv(pname, ptArray);
183   *v0 = ptArray[0];
184   *v1 = ptArray[1];
185   *v2 = ptArray[2];
186}
187
188void emu_alListeneri( ALenum param, ALint value )
189{
190   alListenerf(param, static_cast<ALfloat>(value));
191}
192
193ALCdevice* emu_alcGetContextsDevice(ALCcontext *context)
194{
195   // this function isn't emulated
196   AssertFatal(false, "alcGetContextsDevice is not available");
197   return NULL;
198}
199
200namespace Audio
201{
202
203/*!   Shutdown and Unload the OpenAL DLL  
204*/
205void OpenALDLLShutdown()
206{
207   if (dlHandle != NULL)
208   {
209      dlclose(dlHandle);
210      // FreeBSD didn't like that const dlerror() was returning.
211      if ((dlError = (char *)dlerror()) != NULL)
212         ConsoleLogEntry::General, " Error unloading OpenAL Library: %s", dlError);
213   }
214   dlHandle = NULL;
215
216   unbindOpenALFunctions();   
217}   
218
219/*!   Dynamically Loads the OpenAL DLL if present and binds all the functions.
220*     If there is no DLL or an unexpected error occurs binding functions the
221*     stub functions are automatically bound.
222*/
223bool OpenALDLLInit()
224{
225   OpenALDLLShutdown();
226
227   const char* libName = "libopenal.so";
228
229   // these are relative to the current working directory
230   const char* searchPath[] = {
231      "lib",
232      "tplib", // superceeded by "lib", here for backass compatibility
233      "", // i.e.: current working directory
234      NULL // this must be last
235   }; 
236
237   char openalPath[4096];
238   for (int i)
239   {   
240      dSprintf(openalPath, sizeof(openalPath), "%s/%s/%s",
241         Platform::getWorkingDirectory(), 
242         searchPath[i],
243         libName);
244         
245      Con::printf("     Searching for OpenAl at location : %s", openalPath);
246      dlHandle = dlopen(openalPath, RTLD_NOW);
247      if (dlHandle != NULL)
248      {
249         // found it
250         Con::printf("   Loading OpenAL: %s", openalPath);
251         break;
252      }
253   }
254   
255   if (dlHandle == NULL)
256   {
257      // couldn't find it in our searchPath, try the system path
258      dlHandle = dlopen(libName, RTLD_NOW);
259      if (dlHandle != NULL)
260         Con::printf("   Loading OpenAL from system (dlopen) path");
261   }
262
263   if (dlHandle != NULL)
264   {
265      // if the DLL loaded bind the OpenAL function pointers
266      if(bindOpenALFunctions())
267      {
268         // if EAX is available bind it's function pointers
269         if (alIsExtensionPresent((ALubyte*)"EAX" ))
270            bindEAXFunctions();
271         return(true);
272      }
273
274      // an error occured, shutdown
275      OpenALDLLShutdown();
276   }
277   else
278   {
279      ConsoleLogEntry::General, " Error loading OpenAL Library: %s", dlerror());
280   }
281   return(false);
282}
283
284} // end namespace Audio
285
286#endif //0
287