Why is XGetWindowProperty returning null?

2019-04-02 08:05发布

I'm using the following to get the names of all X windows:

  Atom nameAtom = XInternAtom(dpy,"_NET_WM_NAME",false);
  Atom type;
  int format;
  unsigned long nitems, after;
  unsigned char *data = 0;

  if (Success == XGetWindowProperty(dpy, window, nameAtom, 0, 65536,
                                    false, XA_ATOM, &type, &format,
                                    &nitems, &after, &data)) {
    if (data) {
      Atom windowName = *(Atom*)data;
      const char* name = XGetAtomName(dpy, windowName);
      log.debug("Name: %s", name);
      XFree(data);
    }
  }

But in my log I'm just getting (null) for every single window. What am I doing wrong?

标签: c++ x11
1条回答
迷人小祖宗
2楼-- · 2019-04-02 08:40

What was required was to specify the req_type as UTF8_STRING accordingly:

  Atom nameAtom = XInternAtom(dpy,"_NET_WM_NAME",false);
  Atom utf8Atom = XInternAtom(dpy,"UTF8_STRING",false);
  Atom type;
  int format;
  unsigned long nitems, after;
  unsigned char *data = 0;

  if (Success == XGetWindowProperty(dpy, window, nameAtom, 0, 65536,
                                    false, utf8Atom, &type, &format,
                                    &nitems, &after, &data)) {
    if (data) {
      log.debug("Name: %s", data);
      XFree(data);
    }
  }
查看更多
登录 后发表回答