Xlib测试窗口名称 [英] Xlib test window names

查看:264
本文介绍了Xlib测试窗口名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Xlib来控制Xterm. 为此,我正在制作一个带有奇怪标题的Xterm.在我列出所有窗口并检查它们的名称之后.但是有一些错误,我的Xterm的名字在列出时没有出现.这是列出所有窗口的代码:

I'm trying to get control of the the Xterm with Xlib. For that i'm making a Xterm with a strange title. After I list all window and check they names. But something is bugged the nammes of my Xterm does not appear when they are listed. Here is the code for list all window :

void CMD::getWindowTerminal()
{
    Atom a = XInternAtom(m_display, "_NET_CLIENT_LIST", true);
    Atom actualType;
    int format;
    unsigned long numItems, bytesAfter;
    unsigned char *data = 0;
    int status = XGetWindowProperty(m_display, m_root_win, a, 0L, (~0L), false,
                                AnyPropertyType, &actualType, &format, &numItems,
                                &bytesAfter, &data);

    if (status >= Success && numItems)
    {
        long *array = (long*) data;
        for (unsigned long k = 0; k < numItems; k++)
        {
            // get window Id:
            Window w = (Window) array[k];

            char* name = '\0';
            status = XFetchName(m_display, w, &name);
            if (status >= Success)
            {
                std::cout << w << " " << name << std::endl;
                if (name == NULL)
                {
                    m_window_terminal = w;
                    std::cout << "TERMINAL FOUND" << std::endl;
                }
            }
            XFree(name);
        }
        XFree(data);
    }
}

推荐答案

我无法重现该错误;您的代码找到了我的xterm窗口.您是否在查询刚刚生成的xterm的窗口?如果是这样,您可能会遇到竞态条件,因为您尝试在xterm有机会找到它之前找到该窗口.在这种情况下,一种糟糕的解决方案是稍等片刻,然后重试几次.

I cannot reproduce the error; your code finds my xterm windows. Are you querying the window of an xterm you only just spawned? If so, you might have a race condition in that you try to find the window before xterm had a chance of making it. In that case, a crummy solution would be to wait a bit and try again several times.

如果不是这种情况,我只能推测(更多)原因(我的推测涉及行为异常的窗口管理器或非常老的软件),但也许我可以建议一个解决方案:如果xterm不在其中显示根窗口的_NET_CLIENT_LIST,让我们直接深入到窗口树中,看看是否可以在其中找到它.这段C代码(移植到C ++应该不难,无论如何都可以尝试)用XQueryTree递归遍历窗口树,因此它查询所有窗口:

If that is not the case, I can only speculate (more) about causes (my speculations involve a misbehaving window manager or very old software), but perhaps I can suggest a solution: if the xterm doesn't show up in the root window's _NET_CLIENT_LIST, let's dig directly into the window tree and see if we can find it there. This bit of C code (porting to C++ should not be difficult, and anyway it's enough to try it out) traverses the window tree recursively with XQueryTree, so it queries all windows:

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>

Window window_from_name_search(Display *display, Window current, char const *needle) {
  Window retval, root, parent, *children;
  unsigned children_count;
  char *name = NULL;

  /* Check if this window has the name we seek */
  if(XFetchName(display, current, &name) > 0) {
    int r = strcmp(needle, name);
    XFree(name);
    if(r == 0) {
      return current;
    }
  }

  retval = 0;

  /* If it does not: check all subwindows recursively. */
  if(0 != XQueryTree(display, current, &root, &parent, &children, &children_count)) {
    unsigned i;
    for(i = 0; i < children_count; ++i) {
      Window win = window_from_name_search(display, children[i], needle);

      if(win != 0) {
        retval = win;
        break;
      }
    }

    XFree(children);
  }

  return retval;
}

// frontend function: open display connection, start searching from the root window.
Window window_from_name(char const *name) {
  Display *display = XOpenDisplay(NULL);
  Window w = window_from_name_search(display, XDefaultRootWindow(display), name);
  XCloseDisplay(display);
  return w;
}

由于它可以处理所有窗口,因此xterm窗口必须在其中.如果不是,请重新开始(有关可能的竞赛条件的内容).如果不是那样,那就很奇怪了.

Since it handles all windows, your xterm window has to be among them. If it is not, refer back to the beginning (the bit about a possible race condition). And if that's not it, then something's very strange.

这篇关于Xlib测试窗口名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆