Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XFetchName始终返回0_C_Linux_Xlib - Fatal编程技术网

XFetchName始终返回0

XFetchName始终返回0,c,linux,xlib,C,Linux,Xlib,我正试图编写一个C代码来获取Linux系统中活动窗口的标题,但函数XFetchName总是返回零,我也尝试了XGetWMName,结果相同。。。 但是使用xprop,我可以看到“WM_NAME”属性中有一个字符串 谁能告诉我我的代码有什么问题吗 #include <X11/Xlib.h> #include <stdio.h> #include <stdarg.h> int main( int argc, char* argv[] ) { Dis

我正试图编写一个C代码来获取Linux系统中活动窗口的标题,但函数XFetchName总是返回零,我也尝试了XGetWMName,结果相同。。。 但是使用xprop,我可以看到“WM_NAME”属性中有一个字符串

谁能告诉我我的代码有什么问题吗

#include <X11/Xlib.h>
#include <stdio.h>
#include <stdarg.h>


int main( int argc, char* argv[] )
{
      Display *display;
      Window focus;
      char *window_name;
      int revert;

      display = XOpenDisplay(NULL);
      XGetInputFocus(display, &focus, &revert);
      int ret = XFetchName(display, focus, &window_name);
      printf("ret = %d\n", ret);
      if (window_name) printf("Title = %s\n", window_name);
      return 0;
}
#包括
#包括
#包括
int main(int argc,char*argv[])
{
显示*显示;
窗口焦点;
字符*窗口名称;
int还原;
display=XOpenDisplay(空);
XGetInputFocus(显示、聚焦和还原);
int-ret=XFetchName(显示、焦点和窗口名称);
printf(“ret=%d\n”,ret);
如果(窗口名称)printf(“标题=%s\n”,窗口名称);
返回0;
}

谢谢。

函数返回指定窗口的名称。如果成功,则返回非零状态;否则,没有为窗口设置名称,它将返回零

您需要为您的窗口设置一个名称

我启动了一个
xterm
会话并执行了您的代码,得到了以下输出:

sangeeth@home:~/work/test$ ./a.out 
ret = 1 
Title = sangeeth@home: ~/work/test
sangeeth@home:~/work/test$ 
OTOH,我试图编译您的程序,但出现以下错误:

(.text+0x18): undefined reference to `main'
你需要改变

int _main( int argc, char* argv[] )

您可以尝试使用函数。虽然和的描述都表示将返回
WM_NAME
属性,但它们似乎彼此不同。有时,它们返回相同的名称。有时,只有
XGetWMName
返回名称

您还可以使用
xwininfo-root-tree
获取所有窗口的名称,并与
XFetchName
XGetWMName
的结果进行比较

此代码可以列出所有窗口,并打印
XFetchName
XGetWMName
的窗口id和结果。您可以使用窗口id在
xwininfo-root-tree
的输出中查找

#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

void enum_windows(Display* display, Window window, int depth) {
  int i;

  XTextProperty text;
  XGetWMName(display, window, &text);
  char* name;
  XFetchName(display, window, &name);
  for (i = 0; i < depth; i++)
    printf("\t");
  printf("id=0x%x, XFetchName=\"%s\", XGetWMName=\"%s\"\n", window, name != NULL ? name : "(no name)", text.value);

  Window root, parent;
  Window* children;
  int n;
  XQueryTree(display, window, &root, &parent, &children, &n);
  if (children != NULL) {
    for (i = 0; i < n; i++) {
      enum_windows(display, children[i], depth + 1);
    }
    XFree(children);
  }
}

int main() {
  Display* display = XOpenDisplay(NULL);
  Window root = XDefaultRootWindow(display);
  enum_windows(display, root, 0);
}
下面是
xwininfo-root-tree
的一段输出,显示了这些窗口的名称。 xwininfo:windowid:0x2c7(根窗口)(没有名称)

/*
*以下函数是国际化的替换
*使用XGetWMName和
*XGetWMIconName。
*
*请注意,必须使用free()释放第三个参数,
*不是XFree()。
*/
地位
I18N_FetchName(dpy、w、winname)
显示*dpy;
窗口w;
字符**winname;
{
智力状态;
XTextProperty text_prop;
字符**列表;
int-num;
status=XGetWMName(dpy、w和text_prop);
如果(!status | | |!text|u prop.value | |!text|u prop.nitems)返回0;
状态=XmbTextPropertyToTextList(dpy,&text\u prop,&list,&num);
if(status
//XFetchName使用XGetWMName


请参阅:

当从Xterm(默认名称为
Xterm
set)运行时,您的代码在我当前使用的机器(OSX Lion)上运行得非常好。此外,我添加了一个对
XStoreName()
的调用,首先将其设置为其他名称,这与您的代码检索新名称时的预期效果一样。这当然是在将
\u main()
重命名为
main()
之后-你是如何运行你的程序的?这个“\u”只是用命令行“-Wl,-e\u main”定义一个入口点名称,因为我不使用stdlib,我认为这不需要做任何事情。当从Ubuntu的终端执行时,它仍然返回0,但从xterm运行,这有什么原因吗?我怀疑
\u main()
是因为它是一个更大框架的挂钩,这可能是实际问题。@BrianRoach这是一个很好的观点。我们需要从OP中听到更多细节,“uu”只是定义一个入口点名称“-Wl,-e_u_umain”,因为我没有使用stdlibwell,它在xterm下执行时有效,但在普通终端上执行时无效!也许“普通终端”只是设置了现代的网络名称,而不是旧的网络名称?谢谢这真的很有帮助:)
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

void enum_windows(Display* display, Window window, int depth) {
  int i;

  XTextProperty text;
  XGetWMName(display, window, &text);
  char* name;
  XFetchName(display, window, &name);
  for (i = 0; i < depth; i++)
    printf("\t");
  printf("id=0x%x, XFetchName=\"%s\", XGetWMName=\"%s\"\n", window, name != NULL ? name : "(no name)", text.value);

  Window root, parent;
  Window* children;
  int n;
  XQueryTree(display, window, &root, &parent, &children, &n);
  if (children != NULL) {
    for (i = 0; i < n; i++) {
      enum_windows(display, children[i], depth + 1);
    }
    XFree(children);
  }
}

int main() {
  Display* display = XOpenDisplay(NULL);
  Window root = XDefaultRootWindow(display);
  enum_windows(display, root, 0);
}
id=0x2c7, XFetchName="(no name)", XGetWMName="(null)"
    id=0x400001, XFetchName="(no name)", XGetWMName="(null)"
    id=0x800036, XFetchName="(no name)", XGetWMName="(null)"
        id=0x1400001, XFetchName="(no name)", XGetWMName="c - XFetchName always returns 0 - Stack Overflow - Chromium"
    id=0x1000001, XFetchName="terminator", XGetWMName="terminator"
        id=0x1000002, XFetchName="(no name)", XGetWMName="(null)"
    id=0x1200001, XFetchName="chromium", XGetWMName="chromium"
        id=0x1200002, XFetchName="(no name)", XGetWMName="(null)"
  Root window id: 0x2c7 (the root window) (has no name)
  Parent window id: 0x0 (none)
     29 children:
     0x1200001 "chromium": ("chromium" "Chromium")  10x10+10+10  +10+10
        1 child:
        0x1200002 (has no name): ()  1x1+-1+-1  +9+9
     0x1000001 "terminator": ("terminator" "Terminator")  10x10+10+10  +10+10
        1 child:
        0x1000002 (has no name): ()  1x1+-1+-1  +9+9
     0x800036 (has no name): ()  1364x741+0+25  +0+25
        1 child:
        0x1400001 "c - XFetchName always returns 0 - Stack Overflow - Chromium": ("Chromium" "Chromium")  1364x741+0+0  +1+26
     0x400001 (has no name): ()  10x10+-20+-20  +-20+-20
 /*
  * The following functions are internationalized substitutions
  * for XFetchName and XGetIconName using XGetWMName and
  * XGetWMIconName.  
  *
  * Please note that the third arguments have to be freed using free(), 
  * not XFree().
  */
 Status
 I18N_FetchName(dpy, w, winname)
     Display *dpy;
     Window w;
     char ** winname;
 {
     int    status;
     XTextProperty text_prop;
     char **list;
     int    num;

     status = XGetWMName(dpy, w, &text_prop);
     if (!status || !text_prop.value || !text_prop.nitems) return 0;
     status = XmbTextPropertyToTextList(dpy, &text_prop, &list, &num);
     if (status < Success || !num || !*list) return 0;
     XFree(text_prop.value);
     *winname = (char *)strdup(*list);
     XFreeStringList(list);
     return 1;
 }