Hi,<br><br>I have the following situation:<br>I need to call a Java function via JNI from a C++ program that talks to an X server. More precisely, whenever a FocusIn or FocusOut event occurs, I want a callback to be invoked on a Java object I supply. However, to process events I have to actively wait for any focus events that occur (very naughty) so in order to not block the whole system, this must happen in a separate thread. I start this thread from within my Java connector. In this thread I simply store a reference to that Java object and enter the X11 event loop. Whenever I receive a focus event, I call back to some method of that object.
<br><br>This works.... sometimes. Whenever it doesn't work however, X11 outputs the message "Xlib: unexpected async reply (sequence 0x7)" and from there on no events will be reported anymore whatsoever.<br><br>
I have no idea what is going wrong here, being very new to X11 programming.<br><br>Here are some relevant code snippets:<br><br>// NetWmConnector.java<br>public class NetWmConnector {<br><br> private static NetWmConnector instance;
<br><br> ...<br><br> public static NetWmConnector getInstance() {<br> if (instance == null) {<br> instance = new NetWmConnector();<br> Thread eventThread = new Thread(new Runnable() {<br>
<br> public void run() {<br> // this invokes a native method, see jni-connector.cc<br> instance.registerSelf();<br> }<br> });<br> eventThread.start
();<br> }<br> return instance;<br> }<br><br> ...<br><br> public native void registerSelf();<br><br> // this is the callback method<br> public void focusChanged(int window) {<br> System.out.println
("Focus has changed to window with ID = " + window);<br> }<br>}<br><br>// jni-connector.cc<br>JNIEXPORT void JNICALL Java_de_dfki_km_mydesk_nativeInterfaces_linux_NetWmConnector_registerSelf<br>(JNIEnv *env, jobject connector_)
<br>{<br> // store the NetWmConnector instance in a global variable<br> jclass connectorClass = env->GetObjectClass(connector_);<br> connector = connector_;<br> focusEventCallback = env->GetMethodID(connectorClass, "focusChanged", "(I)V");
<br> jvm = env;<br><br> enterEventLoop();<br>}<br><br>void enterEventLoop()<br>{<br> XEvent event;<br> while (true)<br> {<br> XNextEvent(display, &event);<br><br> switch (event.type)<br> {
<br> case FocusOut:<br> // noop<br> break;<br> case FocusIn:<br> if (event.xfocus.window != lastFocusedWindow) {<br> lastFocusedWindow = event.xfocus.window;<br>
// call back to the Java method<br> jvm->CallVoidMethod(connector, focusEventCallback, lastFocusedWindow);<br> }<br> default:<br> break;<br> }<br> }
<br>}<br><br>Thanks in advance,<br>Matthias<br clear="all"><br>