java - XGetInputFocus 的正确 JNA 映射是什么

标签 java x11 jna

我正在尝试通过 JNA 映射 X11 XGetInputFocus。原始方法签名是

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

我认为可以使用已定义的 JNA 平台类型将其映射到 Java 中的以下内容。

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

这与 documentation 中描述的建议相关。我现在使用以下代码调用它

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_return用于返回一个Window。 JNA 实现 Window非常像不可变类型, 因为在C语言中它是由typedef XID Window;定义的。 因此,C 中的 Window* 类型需要映射到 JNA 中的 WindowByReference
(这本质上与 C 中的 int* 需要映射的原因相同 到 JNA 中的 IntByReference。)

那么扩展的X11接口(interface)可以如下所示:

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

关于java - XGetInputFocus 的正确 JNA 映射是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54281347/

相关文章:

Java:为从文件存储到数组中的每个单词增加wordCount

XGetWindowProperty 的窗口类型的正确大小

java - 从 JNA jar 文件中提取 DLL 到自定义路径

java - 如何在 Windows 中使用 JNA 截屏?

java - 将Hadoop FS中的所有JARS添加到MapReduce类路径

java - 在 Karel Midpoint 练习中使用 Getter 和 Setter (Java)

java - 我可以从网页执行 shell 脚本吗?

c++ - XGrabKeyboard 仅在程序运行时阻止键盘

linux - 使用特殊光标时,在 Ubuntu 上重新启动 freeglut/OpenGL C 应用程序失败

java - 如何在 jna 中实现具有联合类型的结构体 sizeof()