Linux: Implemented proper X async error handling

This commit is contained in:
Elias Naur 2004-05-11 20:34:47 +00:00
parent dd8c6e8a42
commit e709d0167a
3 changed files with 81 additions and 19 deletions

View File

@ -56,6 +56,8 @@
extern void resetCursor(int x, int y); extern void resetCursor(int x, int y);
extern bool checkXError(JNIEnv *env);
extern Atom getWarpAtom(void); extern Atom getWarpAtom(void);
/* /*

View File

@ -74,6 +74,15 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_getPbufferCaps
return extgl_Extensions.GLX13 ? org_lwjgl_opengl_Pbuffer_PBUFFER_SUPPORTED : 0; 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 * Class: org_lwjgl_opengl_Pbuffer
* Method: nCreate * Method: nCreate
@ -131,14 +140,14 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_nCreate
if (context == NULL) { if (context == NULL) {
XFree(configs); XFree(configs);
throwException(env, "Could not create a GLX context"); throwException(env, "Could not create a GLX context");
return false; return -1;
} }
jboolean allow_software_acceleration = getBooleanProperty(env, "org.lwjgl.opengl.Window.allowSoftwareOpenGL"); jboolean allow_software_acceleration = getBooleanProperty(env, "org.lwjgl.opengl.Window.allowSoftwareOpenGL");
if (!allow_software_acceleration && glXIsDirect(disp, context) == False) { if (!allow_software_acceleration && glXIsDirect(disp, context) == False) {
glXDestroyContext(disp, context); glXDestroyContext(disp, context);
XFree(configs); XFree(configs);
throwException(env, "Could not create a direct GLX context"); throwException(env, "Could not create a direct GLX context");
return false; return -1;
} }
const int buffer_attribs[] = {GLX_PBUFFER_WIDTH, width, const int buffer_attribs[] = {GLX_PBUFFER_WIDTH, width,
GLX_PBUFFER_HEIGHT, height, GLX_PBUFFER_HEIGHT, height,
@ -150,6 +159,10 @@ JNIEXPORT jint JNICALL Java_org_lwjgl_opengl_Pbuffer_nCreate
PbufferInfo *buffer_info = (PbufferInfo *)malloc(sizeof(PbufferInfo)); PbufferInfo *buffer_info = (PbufferInfo *)malloc(sizeof(PbufferInfo));
buffer_info->buffer = buffer; buffer_info->buffer = buffer;
buffer_info->context = context; buffer_info->context = context;
if (!checkXError(env)) {
destroyPbuffer(buffer_info);
return -1;
}
return (jint)buffer_info; return (jint)buffer_info;
} }
@ -178,12 +191,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Pbuffer_nDestroy
(JNIEnv *env, jclass clazz, jint handle) (JNIEnv *env, jclass clazz, jint handle)
{ {
PbufferInfo *buffer_info = (PbufferInfo *)handle; PbufferInfo *buffer_info = (PbufferInfo *)handle;
GLXPbuffer buffer = buffer_info->buffer; destroyPbuffer(buffer_info);
GLXContext context = buffer_info->context;
glXDestroyPbuffer(getDisplay(), buffer);
glXDestroyContext(getDisplay(), context);
free(buffer_info);
decDisplay();
} }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Pbuffer_nSetAttrib JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Pbuffer_nSetAttrib

View File

@ -54,6 +54,7 @@
#include "org_lwjgl_opengl_Window.h" #include "org_lwjgl_opengl_Window.h"
#define USEGLX13 extgl_Extensions.GLX13 #define USEGLX13 extgl_Extensions.GLX13
#define ERR_MSG_SIZE 1024
static GLXContext context = NULL; // OpenGL rendering context static GLXContext context = NULL; // OpenGL rendering context
static GLXWindow glx_window; static GLXWindow glx_window;
@ -79,6 +80,28 @@ static bool ignore_motion_events;
static Display *display_connection = NULL; static Display *display_connection = NULL;
static Atom warp_atom; static Atom warp_atom;
static int display_connection_usage = 0; 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) { Display *getDisplay(void) {
return display_connection; return display_connection;
@ -86,6 +109,8 @@ Display *getDisplay(void) {
Display *incDisplay(JNIEnv *env) { Display *incDisplay(JNIEnv *env) {
if (display_connection_usage == 0) { if (display_connection_usage == 0) {
async_x_error = false;
XSetErrorHandler(errorHandler);
display_connection = XOpenDisplay(NULL); display_connection = XOpenDisplay(NULL);
if (display_connection == NULL) { if (display_connection == NULL) {
throwException(env, "Could not open X display"); 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); 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; dirty = true;
focused = true; focused = true;
minimized = false; minimized = false;
@ -266,6 +296,10 @@ static void createWindow(JNIEnv* env, int screen, XVisualInfo *vis_info, jstring
attribs.override_redirect = True; attribs.override_redirect = True;
} }
win = XCreateWindow(getDisplay(), root_win, x, y, width, height, 0, vis_info->depth, InputOutput, vis_info->visual, attribmask, &attribs); 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"); printfDebug("Created window\n");
current_win = win; current_win = win;
Java_org_lwjgl_opengl_Window_nSetTitle(env, NULL, title); 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); waitMapped(win);
XClearWindow(getDisplay(), win); XClearWindow(getDisplay(), win);
setRepeatMode(AutoRepeatModeOff); setRepeatMode(AutoRepeatModeOff);
XSync(getDisplay(), True); if (!checkXError(env)) {
destroyWindow();
return false;
} }
return true;
static void destroyWindow() {
XDestroyWindow(getDisplay(), current_win);
XFreeColormap(getDisplay(), cmap);
} }
int getCurrentScreen(void) { 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"); throwException(env, "Could not create visual info from FB config");
return false; 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); glx_window = glXCreateWindow(getDisplay(), configs[0], getCurrentWindow(), NULL);
makeCurrent(); makeCurrent();
if (isDebugEnabled()) if (isDebugEnabled())
dumpVisualInfo(vis_info); dumpVisualInfo(vis_info);
XFree(configs); XFree(configs);
XFree(vis_info); if (!checkXError(env)) {
glXDestroyWindow(getDisplay(), glx_window);
glXDestroyContext(getDisplay(), context);
return false;
}
return true; 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"); throwException(env, "Could not create a direct GLX context");
return false; 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);
makeCurrent();
XFree(vis_info); XFree(vis_info);
if (!window_created) {
glXDestroyContext(getDisplay(), context);
return false;
}
makeCurrent();
if (!checkXError(env)) {
glXDestroyContext(getDisplay(), context);
destroyWindow();
return false;
}
return true; return true;
} }