diff --git a/src/java/org/lwjgl/opengl/AWTSurfaceLock.java b/src/java/org/lwjgl/opengl/AWTSurfaceLock.java index 14475247..f079aced 100644 --- a/src/java/org/lwjgl/opengl/AWTSurfaceLock.java +++ b/src/java/org/lwjgl/opengl/AWTSurfaceLock.java @@ -35,6 +35,7 @@ import java.nio.ByteBuffer; import org.lwjgl.BufferUtils; import org.lwjgl.LWJGLException; +import org.lwjgl.LWJGLUtil; import org.lwjgl.Sys; import java.awt.Canvas; @@ -46,6 +47,8 @@ import java.awt.Canvas; * @version $Revision$ */ final class AWTSurfaceLock { + private final static int WAIT_DELAY_MILLIS = 100; + private final ByteBuffer lock_buffer; public AWTSurfaceLock() { @@ -54,10 +57,17 @@ final class AWTSurfaceLock { private static native ByteBuffer createHandle(); public ByteBuffer lockAndGetHandle(Canvas canvas) throws LWJGLException { - lockAndInitHandle(lock_buffer, canvas); + while (!lockAndInitHandle(lock_buffer, canvas)) { + LWJGLUtil.log("Could not get drawing surface info, retrying..."); + try { + Thread.sleep(WAIT_DELAY_MILLIS); + } catch (InterruptedException e) { + LWJGLUtil.log("Interrupted while retrying: " + e); + } + } return lock_buffer; } - private static native void lockAndInitHandle(ByteBuffer lock_buffer, Canvas canvas) throws LWJGLException; + private static native boolean lockAndInitHandle(ByteBuffer lock_buffer, Canvas canvas) throws LWJGLException; protected void unlock() throws LWJGLException { nUnlock(lock_buffer); diff --git a/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c b/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c index a3eee297..70e7628c 100644 --- a/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c +++ b/src/native/common/org_lwjgl_opengl_AWTSurfaceLock.c @@ -39,63 +39,49 @@ #include #include -#ifdef _WIN32 -#include -#else -#include -#endif #include "org_lwjgl_opengl_AWTSurfaceLock.h" #include "awt_tools.h" #include "common_tools.h" -#define WAIT_DELAY 100 - JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_createHandle (JNIEnv *env, jclass clazz) { return newJavaManagedByteBuffer(env, sizeof(AWTSurfaceLock)); } -JNIEXPORT void JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_lockAndInitHandle +JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_lockAndInitHandle (JNIEnv *env, jclass clazz, jobject lock_buffer_handle, jobject canvas) { JAWT awt; JAWT_DrawingSurface* ds; JAWT_DrawingSurfaceInfo *dsi; AWTSurfaceLock *awt_lock = (AWTSurfaceLock *)(*env)->GetDirectBufferAddress(env, lock_buffer_handle); awt.version = JAWT_VERSION_1_4; - while (true) { - if (JAWT_GetAWT(env, &awt) == JNI_FALSE) { - throwException(env, "Could not get the JAWT interface"); - return; - } - - ds = awt.GetDrawingSurface(env, canvas); - if (ds == NULL) { - throwException(env, "Could not get the drawing surface"); - return; - } - - if((ds->Lock(ds) & JAWT_LOCK_ERROR) != 0) { - awt.FreeDrawingSurface(ds); - throwException(env, "Could not lock the drawing surface"); - return; - } - - dsi = ds->GetDrawingSurfaceInfo(ds); - if (dsi != NULL) - break; - - printfDebug("Could not get drawing surface info, retrying... \n"); - ds->Unlock(ds); - awt.FreeDrawingSurface(ds); -#ifdef _WIN32 - Sleep(WAIT_DELAY); -#else - usleep(WAIT_DELAY*1000); -#endif + if (JAWT_GetAWT(env, &awt) == JNI_FALSE) { + throwException(env, "Could not get the JAWT interface"); + return JNI_FALSE; } - awt_lock->awt = awt; - awt_lock->ds = ds; - awt_lock->dsi = dsi; + + ds = awt.GetDrawingSurface(env, canvas); + if (ds == NULL) { + throwException(env, "Could not get the drawing surface"); + return JNI_FALSE; + } + + if((ds->Lock(ds) & JAWT_LOCK_ERROR) != 0) { + awt.FreeDrawingSurface(ds); + throwException(env, "Could not lock the drawing surface"); + return JNI_FALSE; + } + + dsi = ds->GetDrawingSurfaceInfo(ds); + if (dsi != NULL) { + awt_lock->awt = awt; + awt_lock->ds = ds; + awt_lock->dsi = dsi; + return JNI_TRUE; + } + ds->Unlock(ds); + awt.FreeDrawingSurface(ds); + return JNI_FALSE; } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_AWTSurfaceLock_nUnlock