JNA 联合结构映射

发布于 2024-09-06 10:35:15 字数 1308 浏览 8 评论 0原文

在 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 ...等)收到事件。

我并不是要求对上述所有结构进行完整映射。一个清晰的解释和一个关于如何做的小例子就足够了。

谢谢

In JNA, how do you map a union structure like the following XEvent from Xlib

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-09-13 10:35:15

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

  1. 使用您喜欢的任何方法(例如 XNextEvent)获取 XEvent。
  2. 使用类型字段确定事件的类型。
  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
}

Use the mappings defined in the JNA contrib (com.sun.jna.platform.X11) then do the following:

  1. Get the XEvent using whatever method yo prefer (e.g. XNextEvent).
  2. Determine the type of the event using the type field.
  3. Based on the type, call the method readFiled with the field name (in string) and cast the returned value to the event type of the field name.

Example:

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
}
末蓝 2024-09-13 10:35:15

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

The sources for JNA already provide examples for xlib.

This is described here. here

The implementation can be found in the jna sources under the contrib folder.

Specifically for XEvent it is defined as:

    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];
}

I'm still learning JNA myself but I believe the idea is check the type value and then only refer to the corresponding event field. The others should be null. I don't think it is possible to do this through a cast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文