From 0ae705b7b0b02b27c8f3cca0516d3efbce65038f Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sun, 4 Jul 2004 13:58:11 +0000 Subject: [PATCH] Added more error checking in OpenAL. Unload native stubs on AL destroy. --- src/java/org/lwjgl/openal/AL.java | 88 ++++++++++++---------- src/java/org/lwjgl/openal/ALC.java | 35 +-------- src/native/common/common_tools.cpp | 2 +- src/native/common/org_lwjgl_openal_AL.cpp | 4 + src/native/common/org_lwjgl_openal_AL.h | 10 +++ src/native/common/org_lwjgl_openal_ALC.cpp | 13 ++-- 6 files changed, 74 insertions(+), 78 deletions(-) diff --git a/src/java/org/lwjgl/openal/AL.java b/src/java/org/lwjgl/openal/AL.java index 70c9f39f..df96c937 100644 --- a/src/java/org/lwjgl/openal/AL.java +++ b/src/java/org/lwjgl/openal/AL.java @@ -125,15 +125,7 @@ public abstract class AL { create(); } - /** - * Creates an OpenAL instance. The empty create will cause OpenAL to - * open the default device, and create a context using default values. - */ - public static void create() throws LWJGLException { - if(created) { - return; - } - + private static String[] getOALPaths() throws LWJGLException { // 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"); @@ -168,43 +160,58 @@ public abstract class AL { //add cwd path oalPaths[oalPaths.length - 1] = ""; + return oalPaths; + } + + /** + * Creates an OpenAL instance. The empty create will cause OpenAL to + * open the default device, and create a context using default values. + */ + public static void create() throws LWJGLException { + if (created) { + return; + } + String[] oalPaths = getOALPaths(); nCreate(oalPaths); - - AL10.initNativeStubs(); - ALC.create(); - - device = ALC.alcOpenDevice(deviceArguments); - if (device == null) { - ALC.destroy(); - throw new LWJGLException("Could not open ALC device"); - } - //check if doing default values or not - if (contextFrequency == -1) { - context = ALC.alcCreateContext(device.device, null); - } else { - context = ALC.alcCreateContext(device.device, - ALCcontext.createAttributeList(contextFrequency, contextRefresh, contextSynchronized)); - } - - ALC.alcMakeContextCurrent(context.context); - created = true; + + try { + AL10.initNativeStubs(); + ALC.initNativeStubs(); + + device = ALC.alcOpenDevice(deviceArguments); + if (device == null) + throw new LWJGLException("Could not open ALC device"); + //check if doing default values or not + if (contextFrequency == -1) { + context = ALC.alcCreateContext(device.device, null); + } else { + context = ALC.alcCreateContext(device.device, + ALCcontext.createAttributeList(contextFrequency, contextRefresh, contextSynchronized)); + } + if (ALC.alcMakeContextCurrent(context.context) != 0) + throw new LWJGLException("Could not make ALC context current"); + created = true; + } catch (LWJGLException e) { + destroy(); + throw e; + } } /** * Exit cleanly by calling destroy. */ public static void destroy() { - if(!created) { - return; + if (context != null) { + ALC.alcDestroyContext(context.context); + context = null; } - - ALC.alcDestroyContext(context.context); - ALC.alcCloseDevice(device.device); - ALC.destroy(); - - device = null; - context = null; + if (device != null) { + ALC.alcCloseDevice(device.device); + device = null; + } + resetNativeStubs(AL10.class); + resetNativeStubs(ALC.class); deviceArguments = null; @@ -212,8 +219,9 @@ public abstract class AL { contextRefresh = -1; contextSynchronized = ALC.ALC_FALSE; + if (created) + nDestroy(); created = false; - nDestroy(); } /** @@ -242,5 +250,7 @@ public abstract class AL { Sys.log("Failure locating OpenAL using classloader:" + e); } return null; - } + } + + private static native void resetNativeStubs(Class clazz); } diff --git a/src/java/org/lwjgl/openal/ALC.java b/src/java/org/lwjgl/openal/ALC.java index a52388fd..b28b1cfa 100644 --- a/src/java/org/lwjgl/openal/ALC.java +++ b/src/java/org/lwjgl/openal/ALC.java @@ -67,9 +67,6 @@ import org.lwjgl.LWJGLException; * @version $Revision$ */ public class ALC { - /** Has the ALC object been created? */ - protected static boolean created; - /** Bad value */ public static final int ALC_INVALID = -1; @@ -146,41 +143,13 @@ public class ALC { Sys.initialize(); } - /** Creates a new instance of ALC */ - protected ALC() { - } - /** * Override to provide any initialization code after creation. */ protected static void init() { } - /** - * Creates the ALC instance - * - * @throws LWJGLException if a failiure occured in the ALC creation process - */ - protected static void create() throws LWJGLException { - if (created) { - return; - } - initNativeStubs(); - init(); - created = true; - } - - private static native void initNativeStubs() throws LWJGLException; - - /** - * Calls whatever destruction rutines that are needed - */ - protected static void destroy() { - if (!created) { - return; - } - created = false; - } + static native void initNativeStubs() throws LWJGLException; /** * The application can obtain certain strings from ALC. @@ -284,7 +253,7 @@ public class ALC { * @param context address of context to make current * @return true if successfull, false if not */ - native static boolean alcMakeContextCurrent(int context); + native static int alcMakeContextCurrent(int context); /** * The current context is the only context accessible to state changes by AL commands diff --git a/src/native/common/common_tools.cpp b/src/native/common/common_tools.cpp index c51e5fca..da11168a 100644 --- a/src/native/common/common_tools.cpp +++ b/src/native/common/common_tools.cpp @@ -176,7 +176,7 @@ void ext_InitializeClass(JNIEnv *env, jclass clazz, ExtGetProcAddressPROC gpa, i method->signature = function->signature; method->fnPtr = function->method_pointer; } - jint result = env->RegisterNatives(clazz, methods, num_functions); + env->RegisterNatives(clazz, methods, num_functions); free(methods); } diff --git a/src/native/common/org_lwjgl_openal_AL.cpp b/src/native/common/org_lwjgl_openal_AL.cpp index d54b2a8e..6d56fa2b 100644 --- a/src/native/common/org_lwjgl_openal_AL.cpp +++ b/src/native/common/org_lwjgl_openal_AL.cpp @@ -43,3 +43,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_nCreate (JNIEnv *env, jclass cla JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_nDestroy(JNIEnv *env, jclass clazz) { DeInitializeOpenAL(); } + +JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_resetNativeStubs(JNIEnv *env, jclass clazz, jclass al_class) { + env->UnregisterNatives(al_class); +} diff --git a/src/native/common/org_lwjgl_openal_AL.h b/src/native/common/org_lwjgl_openal_AL.h index 7785ac23..ef663e11 100644 --- a/src/native/common/org_lwjgl_openal_AL.h +++ b/src/native/common/org_lwjgl_openal_AL.h @@ -15,6 +15,8 @@ extern "C" { /* Inaccessible static: contextRefresh */ /* Inaccessible static: contextSynchronized */ /* Inaccessible static: created */ +/* Inaccessible static: class_00024org_00024lwjgl_00024openal_00024AL10 */ +/* Inaccessible static: class_00024org_00024lwjgl_00024openal_00024ALC */ /* Inaccessible static: class_00024org_00024lwjgl_00024openal_00024AL */ /* Inaccessible static: class_00024java_00024lang_00024String */ /* @@ -33,6 +35,14 @@ JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_nCreate JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_nDestroy (JNIEnv *, jclass); +/* + * Class: org_lwjgl_openal_AL + * Method: resetNativeStubs + * Signature: (Ljava/lang/Class;)V + */ +JNIEXPORT void JNICALL Java_org_lwjgl_openal_AL_resetNativeStubs + (JNIEnv *, jclass, jclass); + #ifdef __cplusplus } #endif diff --git a/src/native/common/org_lwjgl_openal_ALC.cpp b/src/native/common/org_lwjgl_openal_ALC.cpp index db8f1f7c..eb66611c 100644 --- a/src/native/common/org_lwjgl_openal_ALC.cpp +++ b/src/native/common/org_lwjgl_openal_ALC.cpp @@ -49,7 +49,7 @@ typedef ALCvoid (ALCAPIENTRY *alcGetIntegervPROC)(ALCdevice *device,ALCenum typedef ALCdevice* (ALCAPIENTRY *alcOpenDevicePROC)(ALCubyte *deviceName); typedef ALCvoid (ALCAPIENTRY *alcCloseDevicePROC)(ALCdevice *device); typedef ALCcontext* (ALCAPIENTRY *alcCreateContextPROC)(ALCdevice *device,ALCint *attrList); -typedef ALCboolean (ALCAPIENTRY *alcMakeContextCurrentPROC)(ALCcontext *context); +typedef ALCenum (ALCAPIENTRY *alcMakeContextCurrentPROC)(ALCcontext *context); typedef ALCvoid (ALCAPIENTRY *alcProcessContextPROC)(ALCcontext *context); typedef ALCdevice* (ALCAPIENTRY *alcGetContextsDevicePROC)(ALCcontext *context); typedef ALCvoid (ALCAPIENTRY *alcSuspendContextPROC)(ALCcontext *context); @@ -205,12 +205,15 @@ static jobject JNICALL Java_org_lwjgl_openal_ALC_alcCreateContext (JNIEnv *env, * C Specification: * ALCboolean alcMakeContextCurrent(ALCcontext *context); */ -static jboolean JNICALL Java_org_lwjgl_openal_ALC_alcMakeContextCurrent (JNIEnv *env, jclass clazz, jint contextaddress) { +static jint JNICALL Java_org_lwjgl_openal_ALC_alcMakeContextCurrent (JNIEnv *env, jclass clazz, jint contextaddress) { ALCcontext* context = (ALCcontext*) contextaddress; + ALCenum result; if(context == NULL) { - return alcMakeContextCurrent(NULL); + result = alcMakeContextCurrent(NULL); + } else { + result = alcMakeContextCurrent(context); } - return alcMakeContextCurrent(context); + return result; } /** @@ -360,7 +363,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_openal_ALC_initNativeStubs(JNIEnv *env, jc {"alcOpenDevice", "(Ljava/lang/String;)Lorg/lwjgl/openal/ALCdevice;", (void*)&Java_org_lwjgl_openal_ALC_alcOpenDevice, "alcOpenDevice", (void**)&alcOpenDevice}, {"alcCloseDevice", "(I)V", (void*)&Java_org_lwjgl_openal_ALC_alcCloseDevice, "alcCloseDevice", (void**)&alcCloseDevice}, {"alcCreateContext", "(ILjava/nio/IntBuffer;)Lorg/lwjgl/openal/ALCcontext;", (void*)&Java_org_lwjgl_openal_ALC_alcCreateContext, "alcCreateContext", (void**)&alcCreateContext}, - {"alcMakeContextCurrent", "(I)Z", (void*)&Java_org_lwjgl_openal_ALC_alcMakeContextCurrent, "alcMakeContextCurrent", (void**)&alcMakeContextCurrent}, + {"alcMakeContextCurrent", "(I)I", (void*)&Java_org_lwjgl_openal_ALC_alcMakeContextCurrent, "alcMakeContextCurrent", (void**)&alcMakeContextCurrent}, {"nalcProcessContext", "(I)V", (void*)&Java_org_lwjgl_openal_ALC_nalcProcessContext, "alcProcessContext", (void**)&alcProcessContext}, {"alcGetCurrentContext", "()Lorg/lwjgl/openal/ALCcontext;", (void*)&Java_org_lwjgl_openal_ALC_alcGetCurrentContext, "alcGetCurrentContext", (void**)&alcGetCurrentContext}, {"alcGetContextsDevice", "(I)Lorg/lwjgl/openal/ALCdevice;", (void*)&Java_org_lwjgl_openal_ALC_alcGetContextsDevice, "alcGetContextsDevice", (void**)&alcGetContextsDevice},