首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用JNA XLib在X11环境中获取所有Windows

使用JNA XLib在X11环境中获取所有Windows
EN

Stack Overflow用户
提问于 2014-07-22 21:49:55
回答 2查看 2.7K关注 0票数 2

基本上,我的目标是获取所有的窗口(我说的是每个窗口,而不是程序所附带的窗口),并将它们编译成一个列表。我试图使我的应用程序尽可能的跨平台友好,所以我正在为Windows和Linux移植JNA。在Windows中使用JNA是非常简单和容易的。然而,缺乏使用XLib的JNA文档和示例并不能使编码过程变得更容易。

This question, regarding to coding with XLib in C++确实提供了一些关于如何使用XLib在X11环境中获取窗口的说明。--我基本上需要在Java中实现XQueryTree。由于缺乏知识,我无法很容易地使用这些代码,因为我正在用Java编写代码。我从未用C++进行过编码,也从未尝试过使用XLib进行编码。有什么想法吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-23 03:23:21

XQueryTree是用platform.jar定义的。

代码语言:javascript
复制
int XQueryTree(Display display, Window window, WindowByReference root, WindowByReference parent, PointerByReference children, IntByReference childCount);

使用XOpenDisplay()获取显示。

因为您想要所有的窗口,所以您应该使用根窗口作为父窗口(使用XRootWindow()查找它)。

调用之后,children.getValue()将有一个指向窗口is块的指针;在根目录下,这些is是本机long类型的,因此使用基于Native.LONG_SIZEPointer.getIntArray(0, size)Pointer.getLongArray(0, size),其中size是在childCount中返回的值。然后,您可以使用这个ID数组来初始化Window对象,这些对象可以传递给其他X11函数。

当您完成返回的数组内存时,需要使用XFree手动释放它。

票数 2
EN

Stack Overflow用户

发布于 2016-08-18 14:00:32

我发现我需要冷静下来。在Ubuntu16.04中,许多Windows不是直接根植于顶层,而是出现在几层以下。例如

代码语言:javascript
复制
null
  null
    xeyes
null
  null
    user@user-virtual-machine: ~
null
  null
    workspace-test - Java - JnaTest/src/FindWindows.java - Eclipse 
  • 此外,有时childrenRef返回null

代码

代码语言:javascript
复制
public static void main(String[] args) {
    X11 x11 = X11.INSTANCE;
    Display display = x11.XOpenDisplay(null);

    Window root = x11.XDefaultRootWindow(display);
    recurse(x11, display, root, 0);
}

private static void recurse(X11 x11, Display display, Window root, int depth) {
    X11.WindowByReference windowRef = new X11.WindowByReference();
    X11.WindowByReference parentRef = new X11.WindowByReference();
    PointerByReference childrenRef = new PointerByReference();
    IntByReference childCountRef = new IntByReference();

    x11.XQueryTree(display, root, windowRef, parentRef, childrenRef, childCountRef);
    if (childrenRef.getValue() == null) {
        return;
    }

    long[] ids;

    if (Native.LONG_SIZE == Long.BYTES) {
        ids = childrenRef.getValue().getLongArray(0, childCountRef.getValue());
    } else if (Native.LONG_SIZE == Integer.BYTES) {
        int[] intIds = childrenRef.getValue().getIntArray(0, childCountRef.getValue());
        ids = new long[intIds.length];
        for (int i = 0; i < intIds.length; i++) {
            ids[i] = intIds[i];
        }
    } else {
        throw new IllegalStateException("Unexpected size for Native.LONG_SIZE" + Native.LONG_SIZE);
    }

    for (long id : ids) {
        if (id == 0) {
            continue;
        }
        Window window = new Window(id);
        X11.XTextProperty name = new X11.XTextProperty();
        x11.XGetWMName(display, window, name);

        System.out.println(String.join("", Collections.nCopies(depth, "  ")) + name.value);
        x11.XFree(name.getPointer());

        recurse(x11, display, window, depth + 1);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24898477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档