Making raw input data available to Java3D through the use of JNI. The device is a 3dConnexion Space Navigator, it registers itself as an event device through the input subsystem.
Our Java class
class ReadFile { native static int[] readEvent(); //Load the library static { System.loadLibrary("nativelib"); } public static void main(String args[]) { while(true){ int event[] = new ReadEvents().readEvent(); System.out.print("Event: ("+event.length+" bytes): "); for(int b: event) System.out.print(new Integer(b) + " "); System.out.println(); } } }
The Makefile used to build the shared library
CC=gcc JC=javac JH=javah CFLAGS= -shared -Wall INCLUDEPATH= \ -I/usr/lib/jvm/java-6-sun-1.6.0.16/include/ \ -I/usr/lib/jvm/java-6-sun-1.6.0.16/include/linux all: $(JC) ReadEvents.java $(JH) ReadEvents $(CC) $(CFLAGS) -o libnativelib.so $(INCLUDEPATH) events.c
This is really all the C code we need to pass the axes array
#define EVENTNODE "/dev/input/event6" int axes[6] = {0,0,0,0,0,0}; int btns[2] = {0,0}; int fd = 0; JNIEXPORT jintArray JNICALL Java_ReadEvents_readEvent( JNIEnv* env, jclass cl) { if(fd == 0){ fd = open(EVENTNODE , O_RDONLY); } struct input_event ev; read(fd, &ev, sizeof(struct input_event)); switch (ev.type){ case EV_KEY: btns[ev.code] = ev.value; break; case EV_REL: axes[ev.code] = ev.value; break; } jintArray jb; jb = (*env)->NewIntArray(env, 6); (*env)->SetIntArrayRegion(env, jb, 0, 6, (jint *)axes); return (jb); }