Java XGetInputFocus的正确JNA映射是什么
我试图通过JNA映射X11Java XGetInputFocus的正确JNA映射是什么,java,x11,jna,Java,X11,Jna,我试图通过JNA映射X11XGetInputFocus。原始方法签名是 XGetInputFocus(Display *display, Window *focus_return, int *revert_to_return) 我假设可以使用已经定义的JNA平台类型在Java中映射到以下内容 void XGetInputFocus(Display display, Window focus_return, IntByReference revert_to_return); 与中描述的建议相关
XGetInputFocus
。原始方法签名是
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
我缺少什么?在本机X11函数中
XGetInputFocus(Display *display, Window *focus_return, int *revert_to_return)
参数Window*focus\u return
用于返回窗口。
JNA的实现非常类似于不可变类型,
因为在C语言中,它是由定义的。
因此,C中的typeWindow*
需要映射到JNA中的WindowByReference
。
(这与C中的int*
需要映射的原因基本相同
到JNA中的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
可能在XGetInputFocus
的声明中,您应该使用它而不是窗口。谢谢,我仍在试图弄清楚为什么这个差异是必要的。对于一个基本类型,它是有意义的,但是一个对象不是不可变的,除非你这样做?但我只是对JNA略知一二。@Xunkar我补充了一些关于这方面的解释。