为什么XGetWindowProperty返回null?

时间:2012-01-19 11:35:56

标签: c++ x11

我正在使用以下内容获取所有X窗口的名称:

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

但是在我的日志中,我只是为每个窗口获取(null)。我做错了什么?

1 个答案:

答案 0 :(得分:5)

所需要的是相应地将req_type指定为UTF8_STRING

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