X11:使用xcb获取主窗口的列表

时间:2018-10-25 09:49:33

标签: 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);
}

1 个答案:

答案 0 :(得分:1)

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

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

要获取每个顶级窗口的名称,我选择将1000的字段long_length设置为xcb_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);
  }
相关问题