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
This commit is contained in:
@@ -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;
|
||||
_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 " +
|
||||
|
||||
+15
-20
@@ -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 <stdio.h>
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user