x86UNIXOpenAL.cc 8.9 KB

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