一个xlib得到窗口信息的 方法,还是值得研究的

本文介绍了一个名为XWinSpy的工具,用于连接到X服务器并遍历所有窗口,获取窗口属性和名称。该工具展示了如何使用X11 API来查询窗口树、获取窗口名称和属性,以及如何在不同层次上枚举窗口。通过XSetErrorHandler和XGetWMName等函数,实现了错误处理和窗口信息的获取。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <string.h>
// ERROR HANDLER, GENERIC
static int ErrorHandler (Display *display, XErrorEvent *error)
{
   //printf ("/r/n error! /r/n");
   return 0;
}
// END ERROR HANDLER

// GET THE ACTUAL TEXT!!! - This is where it all comes unstuck
void GetWindowProperties (Display *display, Window window)
{
   Atom *atoms;
   int i, j;
   Atom type;
   int format, result, status;
   unsigned long len, bytesLeft;
   unsigned char *data;
   char *atomName;
   XTextProperty textData;
  
   atoms = XListProperties (display, window, &i);
   if (i)
   {
      for (j=0; j < i; j++)
      {
         atomName = XGetAtomName(display, atoms[i]);
       if (atomName)
            printf ("Atom name: %s/r/n", atomName);
         
       status = XGetTextProperty (display, window, &textData, atoms[i]);
       if (status)
       {
             printf ("Atom text: %s/r/n", textData.value);
       }
      }
   }
}
// END GET THE ACTUAL TEXT!!! - This is where it all comes unstuck

// ENUMARATE THROUGH WINDOWS AND DISPLAY THEIR TITLES
void EnumerateWindows (Display *display, Window rootWindow, int showErrors, int showStatus)
{
   static int level = 0;
   Window parent;
   Window *children;
   Window *child;
   unsigned int noOfChildren;
   int status;
   int i;
  
   XTextProperty wmName;
   char **list;
  
   status = XGetWMName (display, rootWindow, &wmName);
   if ((status) && (wmName.value) && (wmName.nitems))
   {
      status = XmbTextPropertyToTextList (display, &wmName, &list, &i);
      if ((status >= Success) && (i) && (*list))
         printf ("INFO - Found window with name '%s' /r/n", (char*) strdup (*list));
   }
  
   GetWindowProperties (display, rootWindow);
  
   level++;
  
   status = XQueryTree (display, rootWindow, &rootWindow, &parent, &children, &noOfChildren);
  
   if (status == 0)
   {
      if (showErrors)
         printf ("ERROR - Could not query the window tree. Aborting./r/n");
      return;
   }
  
   if (noOfChildren == 0)
   {
      if (showErrors)
         printf ("ERROR - No children found. Aborting./r/n");
      return;
   }
   else
   {
      if (showStatus)
         printf ("STATUS - %i number of child windows found./r/n", noOfChildren);
   }
  
   for (i=0; i < noOfChildren; i++)
   {
      EnumerateWindows (display, children[i], showErrors, showStatus);
   }
  
   XFree ((char*) children);
}
// END ENUMERATE WINDOWS

int main(int argc, char *argv[])
{
   // CONNECT TO THE XSERVER
   Display *display;
   int depth;
   int screen;
   int connection;
  
   display = XOpenDisplay (NULL);
   screen = DefaultScreen (display);
   depth = DefaultDepth (display, screen);
   connection = ConnectionNumber (display);
   XSetErrorHandler (ErrorHandler);
  
   printf ("XWinSpy - by van/r/n");
   printf ("----------------/r/n");
   printf ("Display: %s/r/n", XDisplayName((char*)display));
   printf ("Width: %d/r/n", DisplayWidth(display, screen));
   printf ("Height: %d/r/n", DisplayHeight(display, screen));
   printf ("Connection: %d/r/n", connection);
   printf ("Color Depth: %d/r/n", depth);
   // END CONNECT TO THE XSERVER
  
   // RETRIEVE ROOT WINDOW
   Window rootWindow;
  
   rootWindow = RootWindow (display, screen); 
   // END RETRIEVE ROOT WINDOW
  
   // LOOP THROUGH ALL WINDOWS
   EnumerateWindows (display, rootWindow, 0, 0);
   // END LOOP THROUGH ALL WINDOWS
  
   // DISCONNECT FROM THE XSERVER
   XCloseDisplay (display);
   // END DISCONNECT FROM THE XSERVER

  return EXIT_SUCCESS;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值