00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "sound/driver/sound_driver.h"
00027 #include "nel/misc/debug.h"
00028
00029
00030 #ifdef NL_OS_WINDOWS
00031
00032 #include <windows.h>
00033
00034 #else // NL_OS_WINDOWS
00035
00036 #include <dlfcn.h>
00037
00038 #endif // NL_OS_WINDOWS
00039
00040
00041 namespace NLSOUND
00042 {
00043
00044
00045
00046 const uint32 ISoundDriver::InterfaceVersion = 0x04;
00047
00048 typedef ISoundDriver* (*ISDRV_CREATE_PROC)(void);
00049 const char *IDRV_CREATE_PROC_NAME = "NLSOUND_createISoundDriverInstance";
00050
00051 typedef uint32 (*ISDRV_VERSION_PROC)(void);
00052 const char *IDRV_VERSION_PROC_NAME = "NLSOUND_interfaceVersion";
00053
00054
00055
00056
00057
00058
00059 ISoundDriver *ISoundDriver::createDriver()
00060 {
00061 ISDRV_CREATE_PROC createSoundDriver = NULL;
00062 ISDRV_VERSION_PROC versionDriver = NULL;
00063
00064 #ifdef NL_OS_WINDOWS
00065
00066
00067 HINSTANCE hInst;
00068
00069 hInst = LoadLibrary(NLSOUND_DLL_NAME);
00070
00071 if (!hInst)
00072 {
00073 throw ESoundDriverNotFound();
00074 }
00075
00076 char buffer[1024], *ptr;
00077 SearchPath (NULL, NLSOUND_DLL_NAME, NULL, 1023, buffer, &ptr);
00078 nlinfo ("Using the library '"NLSOUND_DLL_NAME"' that is in the directory: '%s'", buffer);
00079
00080 createSoundDriver = (ISDRV_CREATE_PROC) GetProcAddress (hInst, IDRV_CREATE_PROC_NAME);
00081 if (createSoundDriver == NULL)
00082 {
00083 nlinfo( "Error: %u", GetLastError() );
00084 throw ESoundDriverCorrupted();
00085 }
00086
00087 versionDriver = (ISDRV_VERSION_PROC) GetProcAddress (hInst, IDRV_VERSION_PROC_NAME);
00088 if (versionDriver != NULL)
00089 {
00090 if (versionDriver()<ISoundDriver::InterfaceVersion)
00091 throw ESoundDriverOldVersion();
00092 else if (versionDriver()>ISoundDriver::InterfaceVersion)
00093 throw ESoundDriverUnknownVersion();
00094 }
00095
00096 #elif defined (NL_OS_UNIX)
00097
00098
00099 void *handle = dlopen(NLSOUND_DLL_NAME, RTLD_NOW);
00100
00101 if (handle == NULL)
00102 {
00103 nlwarning ("when loading dynamic library '%s': %s", NLSOUND_DLL_NAME, dlerror());
00104 throw ESoundDriverNotFound();
00105 }
00106
00107
00108 createSoundDriver = (ISDRV_CREATE_PROC) dlsym (handle, IDRV_CREATE_PROC_NAME);
00109 if (createSoundDriver == NULL)
00110 {
00111 nlwarning ("when getting function in dynamic library '%s': %s", NLSOUND_DLL_NAME, dlerror());
00112 throw ESoundDriverCorrupted();
00113 }
00114
00115 versionDriver = (ISDRV_VERSION_PROC) dlsym (handle, IDRV_VERSION_PROC_NAME);
00116 if (versionDriver != NULL)
00117 {
00118 if (versionDriver()<ISoundDriver::InterfaceVersion)
00119 throw ESoundDriverOldVersion();
00120 else if (versionDriver()>ISoundDriver::InterfaceVersion)
00121 throw ESoundDriverUnknownVersion();
00122 }
00123
00124 #else // NL_OS_LINUX
00125 #error "Dynamic DLL loading not implemented!"
00126 #endif // NL_OS_LINUX
00127
00128 ISoundDriver *ret = createSoundDriver();
00129 if ( ret == NULL )
00130 {
00131 throw ESoundDriverCantCreateDriver();
00132 }
00133 return ret;
00134 }
00135
00136
00137 }