简体   繁体   English

XGetInputFocus 的正确 JNA 映射是什么

[英]What is the correct JNA mapping for XGetInputFocus

I am trying to map X11 XGetInputFocus through JNA.我正在尝试通过 JNA 映射 X11 XGetInputFocus The original method signature is原始方法签名是

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

Which I assumed could be mapped to the following in Java, using the already defined JNA platform types.我认为可以使用已经定义的 JNA 平台类型在 Java 中映射到以下内容。

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

Which correlates to the recommendation described in the documentation .这与文档中描述的建议相关。 I now invoke it using the following code我现在使用以下代码调用它

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

However, it crashes the JVM with但是,它使 JVM 崩溃

# Problematic frame:
# C  [libX11.so.6+0x285b7]  XGetInputFocus+0x57

What am I missing?我错过了什么?

In the native X11 function在原生 X11 函数中

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

the parameter Window *focus_return is used to return a Window .参数Window *focus_return用于返回一个Window JNA implemented Window very much like an immutable type, because in C language it is defined by typedef XID Window; JNA 实现的Window非常像一个不可变类型,因为在 C 语言中它是由typedef XID Window;定义的typedef XID Window; . . Therefore type Window* in C needs to be mapped to WindowByReference in JNA.因此,C 中的类型Window*需要映射到 JNA 中的WindowByReference
(This is essentially the same reason why int* in C needed to be mapped to IntByReference in JNA.) (这与为什么 C 中的int*需要映射到 JNA 中的IntByReference原因基本相同。)

Then the extended X11 interface can look like this:那么扩展的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);
}

And your code should be modified accordingly:并且您的代码应该相应地修改:

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

Now the program doesn't crash anymore.现在程序不再崩溃了。 For me it prints 0x3c00605 .对我来说,它打印0x3c00605

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM