为了账号安全,请及时绑定邮箱和手机立即绑定

XGetInputFocus 的正确 JNA 映射是什么

XGetInputFocus 的正确 JNA 映射是什么

慕娘9325324 2022-05-12 18:46:37
我正在尝试XGetInputFocus通过 JNA 映射 X11。原始方法签名是XGetInputFocus(Display *display, Window *focus_return, int *revert_to_return)我假设可以使用已经定义的 JNA 平台类型将其映射到 Java 中的以下内容。void XGetInputFocus(Display display, Window focus_return, IntByReference revert_to_return);这与文档中描述的建议相关。我现在使用以下代码调用它final X11 XLIB = X11.INSTANCE;Window current = new Window();Display display = XLIB.XOpenDisplay(null);if (display != null) {   IntByReference revert_to_return = new IntByReference();   XLIB.XGetInputFocus(display, current, revert_to_return);}但是,它会使JVM崩溃# Problematic frame:# C  [libX11.so.6+0x285b7]  XGetInputFocus+0x57我错过了什么?
查看完整描述

1 回答

?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

在原生 X11 函数中


XGetInputFocus(Display *display, Window *focus_return, int *revert_to_return)

该参数Window *focus_return用于返回一个Window. JNA 实现Window起来非常像不可变类型,因为在 C 语言中它是由typedef XID Window;. 因此Window*,C 中的类型需要映射到WindowByReferenceJNA 中。(这与C 中需要映射到JNA中

的原因基本相同。)int*IntByReference


那么扩展X11界面可以是这样的:


public interface X11Extended extends X11 {

    X11Extended INSTANCE = (X11Extended) Native.loadLibrary("X11", X11Extended.class);


    void XGetInputFocus(Display display, WindowByReference focus_return, IntByReference revert_to_return);

}

并且您的代码应该相应地修改:


X11Extended xlib = X11Extended.INSTANCE;

WindowByReference current_ref = new WindowByReference();

Display display = xlib.XOpenDisplay(null);

if (display != null) {

    IntByReference revert_to_return = new IntByReference();

    xlib.XGetInputFocus(display, current_ref, revert_to_return);

    Window current = current_ref.getValue();

    System.out.println(current);

}

现在程序不再崩溃了。对我来说,它打印出来0x3c00605。


查看完整回答
反对 回复 2022-05-12
  • 1 回答
  • 0 关注
  • 269 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号