Xlib: unexpected async reply (sequence 0x7)

Matthias Käppler m.kaeppler at googlemail.com
Wed Nov 14 06:05:57 PST 2007


Hi,

I have the following situation:
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.

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.

I have no idea what is going wrong here, being very new to X11 programming.

Here are some relevant code snippets:

// NetWmConnector.java
public class NetWmConnector {

    private static NetWmConnector instance;

   ...

    public static NetWmConnector getInstance() {
        if (instance == null) {
            instance = new NetWmConnector();
            Thread eventThread = new Thread(new Runnable() {

                public void run() {
                    // this invokes a native method, see jni-connector.cc
                    instance.registerSelf();
                }
            });
            eventThread.start();
        }
        return instance;
    }

    ...

    public native void registerSelf();

    // this is the callback method
    public void focusChanged(int window) {
        System.out.println("Focus has changed to window with ID = " +
window);
    }
}

// jni-connector.cc
JNIEXPORT void JNICALL
Java_de_dfki_km_mydesk_nativeInterfaces_linux_NetWmConnector_registerSelf
(JNIEnv *env, jobject connector_)
{
    // store the NetWmConnector instance in a global variable
    jclass connectorClass = env->GetObjectClass(connector_);
    connector = connector_;
    focusEventCallback = env->GetMethodID(connectorClass, "focusChanged",
"(I)V");
    jvm = env;

    enterEventLoop();
}

void enterEventLoop()
{
    XEvent event;
    while (true)
    {
        XNextEvent(display, &event);

        switch (event.type)
        {
        case FocusOut:
            // noop
            break;
        case FocusIn:
            if (event.xfocus.window != lastFocusedWindow) {
                lastFocusedWindow = event.xfocus.window;
                // call back to the Java method
                jvm->CallVoidMethod(connector, focusEventCallback,
lastFocusedWindow);
            }
        default:
            break;
        }
    }
}

Thanks in advance,
Matthias
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.x.org/archives/xorg/attachments/20071114/ba0e5877/attachment.html>


More information about the xorg mailing list