首页 > 解决方案 > X11:使用 xcb 获取主窗口列表

问题描述

我正在尝试使用使用 xcb 库的 C 程序获取已启动的 X 应用程序的主窗口列表。根据这个问题,这些窗口似乎是“顶级窗口”: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);
}

标签: x11xcb

解决方案


我终于找到了问题所在,在我在互联网上阅读的示例中,字段long_length设置xcb_get_property()0,但似乎要获得一致的回复,该字段的值必须高于或等于 32 位字的数量回复会有。所以我选择100了我的测试,但是如果指定根窗口的子树中有超过100个顶级窗口,那么回复将被截断为前100个窗口。

我还必须除以value_len得到4回复中的元素数量,因为value_len它以字节为单位,并且回复值是 xcb_window_t 元素的数组,每个元素的长度为 4 个字节。

为了获得每个顶级窗口的名称,我选择设置为1000的字段long_lengthxcb_get_property()以确保具有完整的名称。但似乎在回复中,字符串的真实大小是在没有最后一个\0字符的情况下分配的,所以我不得不分配一个长度为的内存块value_len2 + 1,然后用于strncpy()将字符串复制到这个新位置。最后,添加最后一个\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);
  }

推荐阅读