您可以尝试使用XGetWMName功能。虽然描述XGetWMName and XFetchName双方均表示将归还WM_NAME
属性,似乎各有不同。有时,它们返回相同的名称。有些时候,仅仅XGetWMName
返回名称。
您还可以使用xwininfo -root -tree
获取所有窗口的名称,并与结果进行比较XFetchName
and XGetWMName
.
此代码可以列出所有窗口并打印窗口 ID 和结果XFetchName
and XGetWMName
。您可以使用窗口 ID 在输出中查找xwininfo -root -tree
.
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
void enum_windows(Display* display, Window window, int depth) {
int i;
XTextProperty text;
XGetWMName(display, window, &text);
char* name;
XFetchName(display, window, &name);
for (i = 0; i < depth; i++)
printf("\t");
printf("id=0x%x, XFetchName=\"%s\", XGetWMName=\"%s\"\n", window, name != NULL ? name : "(no name)", text.value);
Window root, parent;
Window* children;
int n;
XQueryTree(display, window, &root, &parent, &children, &n);
if (children != NULL) {
for (i = 0; i < n; i++) {
enum_windows(display, children[i], depth + 1);
}
XFree(children);
}
}
int main() {
Display* display = XOpenDisplay(NULL);
Window root = XDefaultRootWindow(display);
enum_windows(display, root, 0);
}
下面的输出显示两个函数的结果可能不同。
id=0x2c7, XFetchName="(no name)", XGetWMName="(null)"
id=0x400001, XFetchName="(no name)", XGetWMName="(null)"
id=0x800036, XFetchName="(no name)", XGetWMName="(null)"
id=0x1400001, XFetchName="(no name)", XGetWMName="c - XFetchName always returns 0 - Stack Overflow - Chromium"
id=0x1000001, XFetchName="terminator", XGetWMName="terminator"
id=0x1000002, XFetchName="(no name)", XGetWMName="(null)"
id=0x1200001, XFetchName="chromium", XGetWMName="chromium"
id=0x1200002, XFetchName="(no name)", XGetWMName="(null)"
这是输出的一部分xwininfo -root -tree
显示这些窗口的名称。
xwininfo:窗口 ID:0x2c7(根窗口)(没有名称)
Root window id: 0x2c7 (the root window) (has no name)
Parent window id: 0x0 (none)
29 children:
0x1200001 "chromium": ("chromium" "Chromium") 10x10+10+10 +10+10
1 child:
0x1200002 (has no name): () 1x1+-1+-1 +9+9
0x1000001 "terminator": ("terminator" "Terminator") 10x10+10+10 +10+10
1 child:
0x1000002 (has no name): () 1x1+-1+-1 +9+9
0x800036 (has no name): () 1364x741+0+25 +0+25
1 child:
0x1400001 "c - XFetchName always returns 0 - Stack Overflow - Chromium": ("Chromium" "Chromium") 1364x741+0+0 +1+26
0x400001 (has no name): () 10x10+-20+-20 +-20+-20