码迷,mamicode.com
首页 > 其他好文 > 详细

Xlib 查找窗口

时间:2014-11-24 23:57:33      阅读:329      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

  1 /*
  2     xraise 0.2 - A small tool to send a X application
  3                      to the foreground
  4     Copyright 2010 - socol
  5     Copyright 2007 - Hd Luc
  6 */
  7 /**
  8  * gcc -g -o xraise xraise.c `pkg-config gtk+-2.0 --cflags --libs gthread-2.0 x11`
  9  */
 10 
 11 
 12 
 13 #include <X11/Xos.h>
 14 #include <X11/Xlib.h>
 15 #include <X11/Xutil.h>
 16 #include <stdio.h>
 17 #include <unistd.h>
 18 #include <stdlib.h>
 19 
 20 /* "borrowed" from xwininfo/dsimple.c */
 21 
 22 Window Window_With_Name(dpy, top, name)
 23      Display *dpy;
 24      Window top;
 25      char *name;
 26 {
 27     Window *children, dummy;
 28     unsigned int nchildren;
 29     int i;
 30     Window w=0;
 31     char *window_name;
 32         XClassHint *class_hint;
 33     class_hint = XAllocClassHint();
 34     int ret = 0;
 35 #if 0
 36     // find by WM_NAME(STRING) = "window - tilte"
 37     if (XFetchName(dpy, top, &window_name) && (strstr(window_name, name) == window_name))
 38       return(top);
 39 #else
 40     // find by WM_CLASS(STRING) = "mywindow", "MyWindow"
 41     if (XGetClassHint(dpy, top, class_hint)) {
 42         if (strstr(class_hint->res_name, name) == class_hint->res_name) {
 43             ret = 1;
 44         }
 45         if (!class_hint->res_class) {
 46             XFree(class_hint->res_class);
 47             XFree(class_hint->res_name);
 48         }
 49         if (ret)
 50             return(top);
 51     }
 52 #endif
 53     
 54     if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
 55       return(0);
 56 
 57     for (i=0; i<nchildren; i++)
 58            {
 59         w = Window_With_Name(dpy, children[i], name);
 60         if (w)
 61           break;
 62     }
 63     if (children) XFree ((char *)children);
 64       return(w);
 65 }
 66 
 67 int main(int argc, char **argv)
 68 {
 69     Window window;
 70     Display *dpy;
 71     
 72     if (argc < 2) 
 73     {
 74         fprintf(stderr, "Usage: %s window-name\n", argv[0]);
 75         exit(1);
 76     }
 77     
 78     dpy = XOpenDisplay(":0");
 79     if (!dpy) 
 80     {
 81         fprintf(stderr, "Cannot open display.\n");
 82         exit(1);
 83     }
 84     
 85     window = Window_With_Name(dpy, DefaultRootWindow(dpy), argv[1]);
 86     if (!window) 
 87     {
 88         fprintf(stderr, "Cannot find a window by that name.\n");
 89         exit(1);
 90     }
 91     
 92     XSetInputFocus(dpy, window, RevertToPointerRoot, CurrentTime);
 93 
 94     if(!XRaiseWindow(dpy, window))
 95     {
 96         fprintf(stderr, "XRaiseWindow error?\n");
 97         exit(1);
 98     }
 99     XCloseDisplay(dpy);
100     return 0;
101 }

转载fromhttp://socol.iteye.com/blog/788653

Xlib 查找窗口

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/sunlb/p/4119810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!