其他分享
首页 > 其他分享> > c – Xlib测试窗口名称

c – Xlib测试窗口名称

作者:互联网

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

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有机会之前找到窗口.在这种情况下,一个糟糕的解决方案是稍等一会再试几次.

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

#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窗口必须在其中.如果不是,请返回开头(关于可能的竞争条件的位).如果那不是它,那么有些事情很奇怪.

标签:xlib,c,linux,title,xterm
来源: https://codeday.me/bug/20191007/1867967.html