fix: load OpenAL based on java.library.path

This commit is contained in:
Brian Matzon 2003-03-22 23:17:11 +00:00
parent 0cf93ebe93
commit dd201b2f14
5 changed files with 63 additions and 27 deletions

View File

@ -31,6 +31,9 @@
*/ */
package org.lwjgl.openal; package org.lwjgl.openal;
import java.io.File;
import java.util.StringTokenizer;
/** /**
* $Id$ * $Id$
* *
@ -74,7 +77,31 @@ public abstract class BaseAL {
return; return;
} }
if (!nCreate()) {
// need to pass path of possible locations of OAL to native side
String libpath = System.getProperty("java.library.path");
String seperator = System.getProperty("path.separator");
String libname;
// libname is hardcoded atm - this will change in a near future...
libname = (System.getProperty("os.name").toLowerCase().indexOf("windows") == -1)
? "libopenal.so"
: "OpenAL32.dll";
StringTokenizer st = new StringTokenizer(libpath, seperator);
//create needed string array
String[] oalPaths = new String[st.countTokens()+1];
//build paths
for(int i=0;i<st.countTokens();i++) {
oalPaths[i] = st.nextToken() + File.separator + libname;
}
//add cwd path
oalPaths[oalPaths.length-1] = libname;
if (!nCreate(oalPaths)) {
throw new Exception("AL instance could not be created."); throw new Exception("AL instance could not be created.");
} }
created = true; created = true;
@ -86,7 +113,7 @@ public abstract class BaseAL {
* *
* @return true if the AL creation process succeeded * @return true if the AL creation process succeeded
*/ */
protected native boolean nCreate(); protected native boolean nCreate(String[] oalPaths);
/** /**
* Calls whatever destruction rutines that are needed * Calls whatever destruction rutines that are needed

View File

@ -163,26 +163,35 @@ void* GetFunctionPointer(const char* function) {
/** /**
* Loads the OpenAL Library * Loads the OpenAL Library
*/ */
void LoadOpenAL(JNIEnv *env) { void LoadOpenAL(JNIEnv *env, jobjectArray oalPaths) {
jsize pathcount = env->GetArrayLength(oalPaths);
#ifdef _DEBUG
printf("Found %d OpenAL paths\n", pathcount);
#endif
#ifdef _WIN32 #ifdef _WIN32
jstring propertykey; for(int i=0;i<pathcount;i++) {
jstring librarypath; jstring path = (jstring) env->GetObjectArrayElement(oalPaths, i);
jclass systemclass; #ifdef _DEBUG
jmethodID propertymethod; printf("Testing '%s'\n", env->GetStringUTFChars(path, NULL));
//compile bitch! #endif
//propertykey = env->NewStringUTF("java.library.path"); handleOAL = LoadLibrary(env->GetStringUTFChars(path, NULL));
///*systemclass = */env->FindClass("java/lang/System"); if (handleOAL != NULL) {
//propertymethod = env->GetStaticMethodID(systemclass, "getProperty", "(Ljava/lang/String;)Ljava/lang/String"); #ifdef _DEBUG
//librarypath = env->CallStaticObjectMethod(systemclass, jmethodID, propertykey); printf("Found OpenAL at '%s'\n", env->GetStringUTFChars(path, NULL));
printf("Loaded library path: %s", librarypath); #endif
break;
//parse string }
}
//try to load for each path
handleOAL = LoadLibrary("OpenAL32.dll");
#endif #endif
#ifdef _X11 #ifdef _X11
handleOAL = dlopen("libopenal.so", RTLD_LAZY); //handleOAL = dlopen("libopenal.so", RTLD_LAZY);
for(int i=0;i<pathcount;i++) {
jstring path = (jstring) env->GetObjectArrayElement(oalPaths, i);
handleOAL = dlopen(env->GetStringUTFChars(path, NULL), RTLD_LAZY);
if (handleOAL != NULL) {
break;
}
}
#endif #endif
} }
@ -201,13 +210,13 @@ void UnLoadOpenAL() {
/** /**
* Initializes OpenAL by loading the library * Initializes OpenAL by loading the library
*/ */
int InitializeOpenAL(JNIEnv *env) { int InitializeOpenAL(JNIEnv *env, jobjectArray oalPaths) {
if(handleOAL != 0) { if(handleOAL != 0) {
return JNI_TRUE; return JNI_TRUE;
} }
//load our library //load our library
LoadOpenAL(env); LoadOpenAL(env, oalPaths);
// if we couldn't load the library, get out // if we couldn't load the library, get out
if(handleOAL == 0) { if(handleOAL == 0) {

View File

@ -135,7 +135,7 @@ DEFINE_GUID(DSPROPSETID_EAX20_BufferProperties,
#define INITGUID #define INITGUID
#define OPENAL #define OPENAL
int InitializeOpenAL(JNIEnv *env); int InitializeOpenAL(JNIEnv *env, jobjectArray oalPaths);
void DeInitializeOpenAL(); void DeInitializeOpenAL();
//alc //alc

View File

@ -40,8 +40,8 @@
* Method: nCreate * Method: nCreate
* Signature: ()Z * Signature: ()Z
*/ */
JNIEXPORT jboolean JNICALL Java_org_lwjgl_openal_BaseAL_nCreate (JNIEnv *env, jobject obj) { JNIEXPORT jboolean JNICALL Java_org_lwjgl_openal_BaseAL_nCreate (JNIEnv *env, jobject obj, jobjectArray oalPaths) {
if(!InitializeOpenAL(env)) { if(!InitializeOpenAL(env, oalPaths)) {
jclass cls = env->FindClass("org/lwjgl/openal/OpenALException"); jclass cls = env->FindClass("org/lwjgl/openal/OpenALException");
env->ThrowNew(cls, "Unable to load function pointers to openal."); env->ThrowNew(cls, "Unable to load function pointers to openal.");
env->DeleteLocalRef(cls); env->DeleteLocalRef(cls);

View File

@ -46,7 +46,7 @@ extern "C" {
* Signature: ()Z * Signature: ()Z
*/ */
JNIEXPORT jboolean JNICALL Java_org_lwjgl_openal_BaseAL_nCreate JNIEXPORT jboolean JNICALL Java_org_lwjgl_openal_BaseAL_nCreate
(JNIEnv *, jobject); (JNIEnv *, jobject, jobjectArray);
/* /*
* Class: org_lwjgl_openal_BaseAL * Class: org_lwjgl_openal_BaseAL