JNA联合结构映射

时间:2010-06-23 16:01:46

标签: java mapping structure union jna

在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;

我希望以后能够根据收到的事件类型将JNA中的XEvent转换为其他事件(如XKeyEvent,XButtonEvent,XMotionEvent等)。

我不是要求上面所有结构的完整映射。通过一个关于如何做到的小例子的清晰解释就足够了。

由于

2 个答案:

答案 0 :(得分:2)

使用JNA contrib(com.sun.jna.platform.X11)中定义的映射,然后执行以下操作:

  1. 使用您喜欢的任何方法获取XEvent(例如XNextEvent)。
  2. 使用类型字段确定事件的类型。
  3. 根据类型,使用字段名称(在字符串中)调用readFiled方法,并将返回的值强制转换为字段名称的事件类型。
  4. 示例:

    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
    }
    

答案 1 :(得分:1)

JNA的来源已经提供了xlib的示例。

这里描述。 here

可以在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,但我相信这个想法是检查类型值,然后只引用相应的事件字段。其他人应该为空。我认为不可能通过演员表达到这一点。