我正在尝试获取已启动的 X 应用程序的主窗口列表,其中包含使用 xcb 库的 C 程序。根据这个问题,这些窗口似乎是“顶级窗口”:X11: list top level windows
所以我的程序要求 Openbox 窗口管理器提供这些窗口的列表,然后询问每个窗口的名称,但它不起作用。我正在使用 EWMH 原子,并且我读到 Openbox 符合 EWMH 标准。
编辑:当我运行控制台命令:时xprop -root _NET_CLIENT_LIST,它给出了几个窗口的标识符。所以看来 Openbox 支持这个原子。我看了下的代码xprop,不过是用Xlib写的,因为多线程支持所以需要使用xcb。
当我的程序从Openbox收到回复时,回复的长度为0。
这是源代码:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <stdlib.h>
#include <xcb/xcb.h>
xcb_atom_t getatom(xcb_connection_t* c, char *atom_name)
{
xcb_intern_atom_cookie_t atom_cookie;
xcb_atom_t atom;
xcb_intern_atom_reply_t *rep;
atom_cookie = xcb_intern_atom(c, 0, strlen(atom_name), atom_name);
rep = xcb_intern_atom_reply(c, atom_cookie, NULL);
if (NULL != rep)
{
atom = rep->atom;
free(rep);
printf("\natom: %ld",atom);
fflush(stdout);
return atom;
}
printf("\nError getting atom.\n");
exit(1);
}
int main() {
xcb_generic_error_t *e;
int i,j,k;
xcb_connection_t* c = xcb_connect(NULL, NULL);
xcb_atom_t net_client_list = getatom(c,"_NET_CLIENT_LIST");
xcb_atom_t net_wm_visible_name = getatom(c,"_NET_WM_VISIBLE_NAME");
xcb_screen_t* screen = xcb_setup_roots_iterator(xcb_get_setup(c)).data;
xcb_get_property_cookie_t prop_cookie_list,prop_cookie;
xcb_get_property_reply_t *reply_prop_list,*reply_prop;
prop_cookie_list = xcb_get_property(c, 0, screen->root, net_client_list, XCB_GET_PROPERTY_TYPE_ANY, 0, 0);
reply_prop_list = xcb_get_property_reply(c, prop_cookie_list, &e);
if(e) {
printf("\nError: %d",e->error_code);
free(e);
}
if(reply_prop_list) {
int value_len = xcb_get_property_value_length(reply_prop_list);
printf("\nvalue_len: %d",value_len);
if(value_len) {
xcb_window_t* win = xcb_get_property_value(reply_prop_list);
for(i=0; i<value_len; i++) {
prop_cookie = xcb_get_property(c, 0, win[i], net_wm_visible_name, XCB_GET_PROPERTY_TYPE_ANY, 0, 0);
reply_prop = xcb_get_property_reply(c, prop_cookie, &e);
if(e) {
printf("\nError: %d",e->error_code);
free(e);
}
if(reply_prop) {
int value_len2 = xcb_get_property_value_length(reply_prop);
printf("\nvalue_len2: %d",value_len2);
if(value_len2) {
char* name = xcb_get_property_value(reply_prop);
printf("\nName: %s",name);
fflush(stdout);
}
free(reply_prop);
}
}
}
free(reply_prop_list);
}
printf("\n\n");
fflush(stdout);
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
我终于发现了问题,在我在网上读到的示例中,字段long_length被xcb_get_property()设置为0,但似乎为了得到一致的回复,该字段的值必须大于或等于 32 位字的数量回复就会有。所以我为我的测试选择了100,但如果指定根窗口的子树中有超过 100 个顶级窗口,则回复将被截断为前 100 个窗口。
我还必须除以value_len才能4获取回复中的元素数量,因为value_len以字节为单位,并且回复值是 xcb_window_t 元素的数组,每个元素长 4 个字节。
为了获取每个顶级窗口的名称,我选择设置为字段1000,以确保拥有完整的名称。但似乎在回复中,字符串的真实大小是在没有最终字符的情况下分配的,因此我必须分配一个 length 的内存块,然后使用将字符串复制到这个新位置。最后,添加最终角色。long_lengthxcb_get_property()\0value_len2 + 1strncpy()\0
这是获取属性的正确代码:
prop_cookie_list = xcb_get_property(c, 0, screen->root, net_client_list, XCB_GET_PROPERTY_TYPE_ANY, 0, 100);
reply_prop_list = xcb_get_property_reply(c, prop_cookie_list, &e);
if(e) {
printf("\nError: %d",e->error_code);
free(e);
}
if(reply_prop_list) {
int value_len = xcb_get_property_value_length(reply_prop_list);
printf("\nvalue_len: %d",value_len);
if(value_len) {
xcb_window_t* win = xcb_get_property_value(reply_prop_list);
for(i=0; i<value_len/4; i++) {
printf("\n--------------------------------\nwin id: %d",win[i]);
prop_cookie = xcb_get_property(c, 0, win[i], net_wm_visible_name, XCB_GET_PROPERTY_TYPE_ANY, 0, 1000);
reply_prop = xcb_get_property_reply(c, prop_cookie, &e);
if(e) {
printf("\nError: %d",e->error_code);
free(e);
}
if(reply_prop) {
int value_len2 = xcb_get_property_value_length(reply_prop);
printf("\nvalue_len2: %d",value_len2);
if(value_len2) {
char* name = malloc(value_len2+1);
strncpy(name,xcb_get_property_value(reply_prop),value_len2);
name[value_len2] = '\0';
printf("\nName: %s",name);
fflush(stdout);
free(name);
}
free(reply_prop);
}
}
}
free(reply_prop_list);
}
Run Code Online (Sandbox Code Playgroud)