From e709d0167ab54c650bf7690f2b97557f3e272633 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Tue, 11 May 2004 20:34:47 +0000 Subject: [PATCH] Linux: Implemented proper X async error handling --- src/native/linux/Window.h | 2 + src/native/linux/org_lwjgl_opengl_Pbuffer.cpp | 24 ++++-- src/native/linux/org_lwjgl_opengl_Window.cpp | 74 ++++++++++++++++--- 3 files changed, 81 insertions(+), 19 deletions(-) diff --git a/src/native/linux/Window.h b/src/native/linux/Window.h index eea55b6d..f682df0a 100644 --- a/src/native/linux/Window.h +++ b/src/native/linux/Window.h @@ -56,6 +56,8 @@ extern void resetCursor(int x, int y); + extern bool checkXError(JNIEnv *env); + extern Atom getWarpAtom(void); /* diff --git a/src/native/linux/org_lwjgl_opengl_Pbuffer.cpp b/src/native/linux/org_lwjgl_opengl_Pbuffer.cpp index 787f8961..0dbd7819 100644 --- a/src/native/linux/org_lwjgl_opengl_Pbuffer.cpp +++ b/src/native/linux/org_lwjgl_opengl_Pbuffer.cpp @@ -74,6 +74,15 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_getPbufferCaps return extgl_Extensions.GLX13 ? org_lwjgl_opengl_Pbuffer_PBUFFER_SUPPORTED : 0; } +static void destroyPbuffer(PbufferInfo *buffer_info) { + GLXPbuffer buffer = buffer_info->buffer; + GLXContext context = buffer_info->context; + glXDestroyPbuffer(getDisplay(), buffer); + glXDestroyContext(getDisplay(), context); + free(buffer_info); + decDisplay(); +} + /* * Class: org_lwjgl_opengl_Pbuffer * Method: nCreate @@ -131,14 +140,14 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_nCreate if (context == NULL) { XFree(configs); throwException(env, "Could not create a GLX context"); - return false; + return -1; } jboolean allow_software_acceleration = getBooleanProperty(env, "org.lwjgl.opengl.Window.allowSoftwareOpenGL"); if (!allow_software_acceleration && glXIsDirect(disp, context) == False) { glXDestroyContext(disp, context); XFree(configs); throwException(env, "Could not create a direct GLX context"); - return false; + return -1; } const int buffer_attribs[] = {GLX_PBUFFER_WIDTH, width, GLX_PBUFFER_HEIGHT, height, @@ -150,6 +159,10 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_nCreate PbufferInfo *buffer_info = (PbufferInfo *)malloc(sizeof(PbufferInfo)); buffer_info->buffer = buffer; buffer_info->context = context; + if (!checkXError(env)) { + destroyPbuffer(buffer_info); + return -1; + } return (jint)buffer_info; } @@ -178,12 +191,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Pbuffer_nDestroy (JNIEnv *env, jclass clazz, jint handle) { PbufferInfo *buffer_info = (PbufferInfo *)handle; - GLXPbuffer buffer = buffer_info->buffer; - GLXContext context = buffer_info->context; - glXDestroyPbuffer(getDisplay(), buffer); - glXDestroyContext(getDisplay(), context); - free(buffer_info); - decDisplay(); + destroyPbuffer(buffer_info); } JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Pbuffer_nSetAttrib diff --git a/src/native/linux/org_lwjgl_opengl_Window.cpp b/src/native/linux/org_lwjgl_opengl_Window.cpp index 97951c7b..6ffc1301 100644 --- a/src/native/linux/org_lwjgl_opengl_Window.cpp +++ b/src/native/linux/org_lwjgl_opengl_Window.cpp @@ -54,6 +54,7 @@ #include "org_lwjgl_opengl_Window.h" #define USEGLX13 extgl_Extensions.GLX13 +#define ERR_MSG_SIZE 1024 static GLXContext context = NULL; // OpenGL rendering context static GLXWindow glx_window; @@ -79,6 +80,28 @@ static bool ignore_motion_events; static Display *display_connection = NULL; static Atom warp_atom; static int display_connection_usage = 0; +static bool async_x_error; +static char error_message[ERR_MSG_SIZE]; + +bool checkXError(JNIEnv *env) { + XSync(getDisplay(), False); + if (async_x_error) { + async_x_error = false; + throwException(env, error_message); + return false; + } else + return true; +} + +static int errorHandler(Display *disp, XErrorEvent *error) { + char err_msg_buffer[ERR_MSG_SIZE]; + XGetErrorText(disp, error->error_code, err_msg_buffer, ERR_MSG_SIZE); + err_msg_buffer[ERR_MSG_SIZE - 1] = '\0'; + snprintf(error_message, ERR_MSG_SIZE, "X Error - serial: %d, error_code: %s, request_code: %d, minor_code: %d", (int)error->serial, err_msg_buffer, (int)error->request_code, (int)error->minor_code); + error_message[ERR_MSG_SIZE - 1] = '\0'; + async_x_error = true; + return 0; +} Display *getDisplay(void) { return display_connection; @@ -86,6 +109,8 @@ Display *getDisplay(void) { Display *incDisplay(JNIEnv *env) { if (display_connection_usage == 0) { + async_x_error = false; + XSetErrorHandler(errorHandler); display_connection = XOpenDisplay(NULL); if (display_connection == NULL) { throwException(env, "Could not open X display"); @@ -236,7 +261,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nSetTitle env->ReleaseStringUTFChars(title_obj, title); } -static void createWindow(JNIEnv* env, int screen, XVisualInfo *vis_info, jstring title, int x, int y, int width, int height, bool fullscreen, bool undecorated) { +static void destroyWindow() { + XDestroyWindow(getDisplay(), current_win); + XFreeColormap(getDisplay(), cmap); +} + +static bool createWindow(JNIEnv* env, int screen, XVisualInfo *vis_info, jstring title, int x, int y, int width, int height, bool fullscreen, bool undecorated) { dirty = true; focused = true; minimized = false; @@ -266,6 +296,10 @@ static void createWindow(JNIEnv* env, int screen, XVisualInfo *vis_info, jstring attribs.override_redirect = True; } win = XCreateWindow(getDisplay(), root_win, x, y, width, height, 0, vis_info->depth, InputOutput, vis_info->visual, attribmask, &attribs); + if (!checkXError(env)) { + XFreeColormap(getDisplay(), cmap); + return false; + } printfDebug("Created window\n"); current_win = win; Java_org_lwjgl_opengl_Window_nSetTitle(env, NULL, title); @@ -283,12 +317,11 @@ static void createWindow(JNIEnv* env, int screen, XVisualInfo *vis_info, jstring waitMapped(win); XClearWindow(getDisplay(), win); setRepeatMode(AutoRepeatModeOff); - XSync(getDisplay(), True); -} - -static void destroyWindow() { - XDestroyWindow(getDisplay(), current_win); - XFreeColormap(getDisplay(), cmap); + if (!checkXError(env)) { + destroyWindow(); + return false; + } + return true; } int getCurrentScreen(void) { @@ -470,13 +503,23 @@ static bool initWindowGLX13(JNIEnv *env, int screen, jstring title, int x, int y throwException(env, "Could not create visual info from FB config"); return false; } - createWindow(env, screen, vis_info, title, x, y, width, height, fscreen, undecorated); + bool window_created = createWindow(env, screen, vis_info, title, x, y, width, height, fscreen, undecorated); + XFree(vis_info); + if (!window_created) { + glXDestroyContext(getDisplay(), context); + XFree(configs); + return false; + } glx_window = glXCreateWindow(getDisplay(), configs[0], getCurrentWindow(), NULL); makeCurrent(); if (isDebugEnabled()) dumpVisualInfo(vis_info); XFree(configs); - XFree(vis_info); + if (!checkXError(env)) { + glXDestroyWindow(getDisplay(), glx_window); + glXDestroyContext(getDisplay(), context); + return false; + } return true; } @@ -501,9 +544,18 @@ static bool initWindowGLX(JNIEnv *env, int screen, jstring title, int x, int y, throwException(env, "Could not create a direct GLX context"); return false; } - createWindow(env, screen, vis_info, title, x, y, width, height, fscreen, undecorated); - makeCurrent(); + bool window_created = createWindow(env, screen, vis_info, title, x, y, width, height, fscreen, undecorated); XFree(vis_info); + if (!window_created) { + glXDestroyContext(getDisplay(), context); + return false; + } + makeCurrent(); + if (!checkXError(env)) { + glXDestroyContext(getDisplay(), context); + destroyWindow(); + return false; + } return true; }