3

JNAでは、Xlibから次のXEventのようなユニオン構造をどのようにマッピングしますか

typedef union _XEvent {
    int type;    /* must not be changed */
    XAnyEvent xany;
    XKeyEvent xkey;
    XButtonEvent xbutton;
    XMotionEvent xmotion;
    XCrossingEvent xcrossing;
    XFocusChangeEvent xfocus;
    XExposeEvent xexpose;
    XGraphicsExposeEvent xgraphicsexpose;
    XNoExposeEvent xnoexpose;
    XVisibilityEvent xvisibility;
    XCreateWindowEvent xcreatewindow;
    XDestroyWindowEvent xdestroywindow;
    XUnmapEvent xunmap;
    XMapEvent xmap;
    XMapRequestEvent xmaprequest;
    XReparentEvent xreparent;
    XConfigureEvent xconfigure;
    XGravityEvent xgravity;
    XResizeRequestEvent xresizerequest;
    XConfigureRequestEvent xconfigurerequest;
    XCirculateEvent xcirculate;
    XCirculateRequestEvent xcirculaterequest;
    XPropertyEvent xproperty;
    XSelectionClearEvent xselectionclear;
    XSelectionRequestEvent xselectionrequest;
    XSelectionEvent xselection;
    XColormapEvent xcolormap;
    XClientMessageEvent xclient;
    XMappingEvent xmapping;
    XErrorEvent xerror;
    XKeymapEvent xkeymap;
    long pad[24];
} XEvent;

I want to be able later on to cast the XEvent in JNA to other events (like XKeyEvent, XButtonEvent, XMotionEvent ...etc) based on the type of the event received.

I am not asking for a full mapping for all the structures above. A clear explanation with a small example on how to do it will be enough.

Thanks

4

2 に答える 2

2

JNA contrib (com.sun.jna.platform.X11) で定義されたマッピングを使用して、次の操作を行います。

  1. 好みの方法 (XNextEvent など) を使用して XEvent を取得します。
  2. type フィールドを使用して、イベントのタイプを決定します。
  3. 型に基づいて、メソッド readFiled をフィールド名 (文字列) で呼び出し、返された値をフィールド名のイベント型にキャストします。

例:

XEvent event = new XEvent();
X11.INSTANCE.XNextEvent(display, event);
if(event.type == X11.KeyPress) {
    XKeyEvent xKey = (XKeyEvent)event.readField("xkey");
    // you can now use xKey.keycode and other fields
}
于 2010-06-25T00:35:13.077 に答える
1

JNA のソースでは、xlib の例が既に提供されています。

これについては、こちらで説明しています。 ここ

実装は、contrib フォルダーの下の jna ソースにあります。

特に XEvent の場合、次のように定義されます。

    public static class XEvent extends Union {
    public int type;
    public XAnyEvent xany;
    public XKeyEvent xkey;
    public XButtonEvent xbutton;
    public XMotionEvent xmotion;
    public XCrossingEvent xcrossing;
    public XFocusChangeEvent xfocus;
    public XExposeEvent xexpose;
    public XGraphicsExposeEvent xgraphicsexpose;
    public XNoExposeEvent xnoexpose;
    public XVisibilityEvent xvisibility;
    public XCreateWindowEvent xcreatewindow;
    public XDestroyWindowEvent xdestroywindow;
    public XUnmapEvent xunmap;
    public XMapEvent xmap;
    public XMapRequestEvent xmaprequest;
    public XReparentEvent xreparent;
    public XConfigureEvent xconfigure;
    public XGravityEvent xgravity;
    public XResizeRequestEvent xresizerequest;
    public XConfigureRequestEvent xconfigurerequest;
    public XCirculateEvent xcirculate;
    public XCirculateRequestEvent xcirculaterequest;
    public XPropertyEvent xproperty;
    public XSelectionClearEvent xselectionclear;
    public XSelectionRequestEvent xselectionrequest;
    public XSelectionEvent xselection;
    public XColormapEvent xcolormap;
    public XClientMessageEvent xclient;
    public XMappingEvent xmapping;
    public XErrorEvent xerror;
    public XKeymapEvent xkeymap;
    public NativeLong[] pad = new NativeLong[24];
}

私はまだJNAを自分で学んでいますが、タイプの値をチェックしてから、対応するイベントフィールドのみを参照するという考え方だと思います。その他はヌルにする必要があります。キャストでこれを行うことは不可能だと思います。

于 2010-06-23T20:09:27.997 に答える