基本上,我的目标是获取所有的窗口(我说的是每个窗口,而不是程序所附带的窗口),并将它们编译成一个列表。我试图使我的应用程序尽可能的跨平台友好,所以我正在为Windows和Linux移植JNA。在Windows中使用JNA是非常简单和容易的。然而,缺乏使用XLib的JNA文档和示例并不能使编码过程变得更容易。
This question, regarding to coding with XLib in C++确实提供了一些关于如何使用XLib在X11环境中获取窗口的说明。--我基本上需要在Java中实现XQueryTree。由于缺乏知识,我无法很容易地使用这些代码,因为我正在用Java编写代码。我从未用C++进行过编码,也从未尝试过使用XLib进行编码。有什么想法吗?
发布于 2014-07-23 03:23:21
XQueryTree是用platform.jar定义的。
int XQueryTree(Display display, Window window, WindowByReference root, WindowByReference parent, PointerByReference children, IntByReference childCount);使用XOpenDisplay()获取显示。
因为您想要所有的窗口,所以您应该使用根窗口作为父窗口(使用XRootWindow()查找它)。
调用之后,children.getValue()将有一个指向窗口is块的指针;在根目录下,这些is是本机long类型的,因此使用基于Native.LONG_SIZE的Pointer.getIntArray(0, size)或Pointer.getLongArray(0, size),其中size是在childCount中返回的值。然后,您可以使用这个ID数组来初始化Window对象,这些对象可以传递给其他X11函数。
当您完成返回的数组内存时,需要使用XFree手动释放它。
发布于 2016-08-18 14:00:32
我发现我需要冷静下来。在Ubuntu16.04中,许多Windows不是直接根植于顶层,而是出现在几层以下。例如
null
null
xeyes
null
null
user@user-virtual-machine: ~
null
null
workspace-test - Java - JnaTest/src/FindWindows.java - Eclipse 代码
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);
}
}https://stackoverflow.com/questions/24898477
复制相似问题