From cc680cbd179fea5118dd41c75bb5647730905571 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 23 Jan 2003 19:00:44 +0000 Subject: [PATCH] Give the native keyboard library a chance to initialize itself and determine whether or not all is well. Use that opportunity in the Linux keyboard library to determine whether or not we can open a connection to the X server, and if not, disable the library rather than throwing a RuntimeException the first time we are called and generally sticking a fork in the whole program. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2213 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/util/keybd/Keyboard.java | 15 ++++++-- .../com_threerings_util_keybd_Keyboard.c | 35 ++++++++----------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/java/com/threerings/util/keybd/Keyboard.java b/src/java/com/threerings/util/keybd/Keyboard.java index a7e950776..fcbd91020 100644 --- a/src/java/com/threerings/util/keybd/Keyboard.java +++ b/src/java/com/threerings/util/keybd/Keyboard.java @@ -1,5 +1,5 @@ // -// $Id: Keyboard.java,v 1.1 2003/01/12 00:26:39 shaper Exp $ +// $Id: Keyboard.java,v 1.2 2003/01/23 19:00:44 mdb Exp $ package com.threerings.util.keybd; @@ -38,14 +38,23 @@ public class Keyboard return _haveLib; } + /** + * Initializes the library and returns true if it successfully did so. + */ + protected static native boolean init (); + /** Whether the keyboard native library was successfully loaded. */ protected static boolean _haveLib; static { try { System.loadLibrary("keybd"); - _haveLib = true; - Log.info("Loaded native keyboard library."); + _haveLib = init(); + if (_haveLib) { + Log.info("Loaded native keyboard library."); + } else { + Log.info("Native keyboard library initialization failed."); + } } catch (UnsatisfiedLinkError e) { Log.warning("Failed to load native keyboard library " + diff --git a/src/java/com/threerings/util/keybd/i686-Linux/com_threerings_util_keybd_Keyboard.c b/src/java/com/threerings/util/keybd/i686-Linux/com_threerings_util_keybd_Keyboard.c index 7cfc7ace7..3d4bdb285 100644 --- a/src/java/com/threerings/util/keybd/i686-Linux/com_threerings_util_keybd_Keyboard.c +++ b/src/java/com/threerings/util/keybd/i686-Linux/com_threerings_util_keybd_Keyboard.c @@ -1,5 +1,5 @@ /* - * $Id: com_threerings_util_keybd_Keyboard.c,v 1.1 2003/01/12 00:26:39 shaper Exp $ + * $Id: com_threerings_util_keybd_Keyboard.c,v 1.2 2003/01/23 19:00:44 mdb Exp $ */ #include @@ -12,7 +12,20 @@ /* prototype definitions */ Display* getXDisplay (JNIEnv* env); -void throwRuntimeException (JNIEnv* env, char* message); + +JNIEXPORT jboolean JNICALL +Java_com_threerings_util_keybd_Keyboard_init ( + JNIEnv* env, jclass class, jboolean enabled) +{ + Display* display = getXDisplay(env); + if (display == NULL) { + /* If we are unable to open a display, we can't function. */ + return JNI_FALSE; + } else { + XCloseDisplay(display); + return JNI_TRUE; + } +} JNIEXPORT void JNICALL Java_com_threerings_util_keybd_Keyboard_setKeyRepeat ( @@ -66,27 +79,9 @@ getXDisplay (JNIEnv* env) char message[MESSAGE_LENGTH]; snprintf(message, MESSAGE_LENGTH, "Unable to open display [display=%s].\n", XDisplayName(disp)); - throwRuntimeException(env, message); return NULL; } /* printf("Opened display [disp=%s].\n", XDisplayName(disp)); */ return dpy; } - -/* - * Throws a runtime exception with the supplied message. - */ -void -throwRuntimeException (JNIEnv* env, char* message) -{ - jclass eclass = (*env)->FindClass(env, "java/lang/RuntimeException"); - if (eclass == 0) { - /* unable to find the exception class */ - fprintf(stderr, "Can't get runtime exception class?! [message=%s].\n", - message); - return; - } - - (*env)->ThrowNew(env, eclass, message); -}