Bertrand125
Bertrand125

Reputation: 1002

X11: Get the list of main windows using xcb

I'm trying to get the list of the main windows of already launched X applications, with a C program using xcb library. It seems these windows are the "top-level windows" according to this question: X11: list top level windows

So my program asks Openbox window manager to give the list of these windows, and then asks the name of each window, but it doesn't work. I'm using EWMH atoms, and I have read that Openbox is EWMH compliant.

Edit: And when I run the console command: xprop -root _NET_CLIENT_LIST, it gives the identifier of a few windows. So it seems Openbox support this atom. I looked at the code of xprop, but it is written with Xlib, and I need to use xcb because of multithreading support.

When My program get a reply from Openbox, the length of the reply is 0.

Here is the source code:

#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);
}

Upvotes: 4

Views: 3051

Answers (1)

Bertrand125
Bertrand125

Reputation: 1002

I finally found the problem, in the examples I have read on internet, the field long_length of xcb_get_property() was set to 0, but it seems that to get a consistant reply, the field must have a value higher or equal to the number of 32 bits words that the reply will have. So I have chosen 100 for my test, but if there is more than 100 top-level windows in the children tree of the specified root window, then the reply will be truncated to the 100 first windows.

I also had to divide value_len by 4 to get the number of elements in the reply, because value_len is in bytes, and the reply value is an array of xcb_window_t elements, each beiing 4 bytes long.

To get the name of each top-level window, I have chosen to set to 1000 the field long_length of xcb_get_property(), in order to be sure to have the complete name. But it seems that in the reply, the true size of the string is allocated without the final \0 character, so I had to allocate a memory block of length value_len2 + 1, and then use strncpy() to copy the string to this new location. And finally, add the final \0 character.

Here is the correct code to get the property:

  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);
  }

Upvotes: 5

Related Questions