home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / XAP / XFM / XFM-1.3 / XFM-1 / xfm-1.3 / xfmc / xfmc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-16  |  2.7 KB  |  120 lines

  1. /*------------------------------------------------------------------------------
  2.   xfmc.c
  3.  
  4.   Created Thu Sep 17 12:49:49 BST 1992
  5.   by Simon Marlow (simonm@dcs.glasgow.ac.uk)
  6.  
  7.   Program to send control signals to a running xfm process.
  8. ------------------------------------------------------------------------------*/
  9.  
  10. #include <stdio.h>
  11.  
  12. #include <X11/Xlib.h>
  13. #include <X11/Xutil.h>
  14. #include <X11/Xatom.h>
  15.  
  16. #include "xfmc.h"
  17.  
  18. extern char *optarg;
  19. extern int optind;
  20.  
  21. Display *dpy;
  22. Atom open, update;
  23.  
  24. static Window searchWindow(Window win)
  25. {
  26.   int i, n_children;
  27.   Window *children, w;
  28.   XClassHint ch;
  29.  
  30.   XQueryTree(dpy, win, &w, &w, &children, &n_children);
  31.  
  32.   for (i=0; i < n_children; i++) {
  33.     XGetClassHint(dpy, children[i], &ch); 
  34.     if (ch.res_class && ch.res_name &&
  35.     !strcmp(ch.res_class, "Xfm") && !strcmp(ch.res_name,"xfm"))
  36.     return children[i];
  37.     if (w = searchWindow(children[i]))
  38.     return w;
  39.   }
  40.   return None;
  41. }  
  42.  
  43. void main(int argc, char *argv[])
  44. {
  45.   int i, j;
  46.   Window xfm;
  47.   XClientMessageEvent e;
  48.   char *s, c;
  49.  
  50.   dpy = XOpenDisplay(NULL);
  51.  
  52.   open   = XInternAtom(dpy, XFM_OPEN_WINDOW,   True);
  53.   update = XInternAtom(dpy, XFM_UPDATE_WINDOW, True);
  54.  
  55.   if (open == None || update == None) {
  56.     fprintf(stderr,
  57.         "%s: Unable to find atoms; is xfm running on this display?\n",
  58.         argv[0]);
  59.     exit(1);
  60.   }
  61.  
  62.  
  63.   /* Must have one of the arguments -u or -o. The rest of the arguments are
  64.    * directories to be opened/updated */
  65.  
  66.   e.message_type = None;
  67.   while ((c = getopt(argc,argv,"uo")) != -1)
  68.     switch (c) {
  69.     case 'u':
  70.     case 'o':
  71.       if (e.message_type)
  72.     goto usage;
  73.       e.message_type = c == 'u' ? update : open;
  74.       break;
  75.     default:
  76.       goto usage;
  77.     }
  78.  
  79.   if (optind == argc || !e.message_type)
  80.     goto usage;
  81.  
  82.   /* Look for xfm in the current window tree. This is a bit inefficient,
  83.    * but it seems to work. Searching the top level windows isn't sufficient,
  84.    * since window managers tend to reparent windows. */
  85.  
  86.   if (!(xfm = searchWindow(DefaultRootWindow(dpy)))) {
  87.     fprintf(stderr, "%s: xfm is not running on this display\n", argv[0]);
  88.     exit(1);
  89.   }
  90.   
  91.   e.type = ClientMessage;
  92.   e.display = dpy;
  93.   e.window = xfm;
  94.   e.format = 8;
  95.  
  96.   /* Send messages in batches of 20 characters each, because that's all that
  97.    * fits in the ClientMessage event structure */
  98.  
  99.   for (j = optind; j < argc; j++) {
  100.     s = argv[j];
  101.     for (i = strlen(s); i >= 0; i -= 20, s += 20) {    
  102.       strncpy(e.data.b,s,20);  
  103.       if (!XSendEvent(dpy, xfm, False, 0, (XEvent *)&e)) {
  104.     fprintf(stderr, "xfmc: XSendEvent failed\n");
  105.     exit(1);
  106.       }
  107.     }
  108.   }
  109.   
  110.   XFlush(dpy); /* doesn't work without this! */
  111.   exit(0);
  112.   
  113.  usage:
  114.   fprintf(stderr, "usage: %s -[u/o] message ...\n", argv[0]);
  115.   exit(1);
  116. }
  117.  
  118.   
  119.   
  120.