home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / clients / xlsclients / xlsclients.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  7.5 KB  |  319 lines

  1. /*
  2.  * $XConsortium: xlsclients.c,v 1.6 90/12/18 15:01:56 gildea Exp $
  3.  *
  4.  * Copyright 1989 Massachusetts Institute of Technology
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software and its
  7.  * documentation for any purpose and without fee is hereby granted, provided
  8.  * that the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of M.I.T. not be used in advertising
  11.  * or publicity pertaining to distribution of the software without specific,
  12.  * written prior permission.  M.I.T. makes no representations about the
  13.  * suitability of this software for any purpose.  It is provided "as is"
  14.  * without express or implied warranty.
  15.  *
  16.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  17.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  18.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  21.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  *
  23.  * Author:  Jim Fulton, MIT X Consortium
  24.  */
  25.  
  26. #include <stdio.h>
  27. #include <ctype.h>
  28. #include <X11/Xos.h>
  29. #include <X11/Xlib.h>
  30. #include <X11/Xatom.h>
  31. #include <X11/Xutil.h>
  32.  
  33. char *ProgramName;
  34.  
  35. static void usage ()
  36. {
  37.     fprintf (stderr,
  38.          "usage:  %s  [-display dpy] [-m len] [-[a][l]]\n", ProgramName);
  39.     exit (1);
  40. }
  41.  
  42. main (argc, argv)
  43.     int argc;
  44.     char **argv;
  45. {
  46.     int i;
  47.     char *displayname = NULL;
  48.     Bool all_screens = False;
  49.     Bool verbose = False;
  50.     Display *dpy;
  51.     int maxcmdlen = 10000;
  52.  
  53.     ProgramName = argv[0];
  54.  
  55.     for (i = 1; i < argc; i++) {
  56.     char *arg = argv[i];
  57.  
  58.     if (arg[0] == '-') {
  59.         char *cp;
  60.  
  61.         switch (arg[1]) {
  62.           case 'd':            /* -display dpyname */
  63.         if (++i >= argc) usage ();
  64.         displayname = argv[i];
  65.         continue;
  66.           case 'm':            /* -max maxcmdlen */
  67.         if (++i >= argc) usage ();
  68.         maxcmdlen = atoi (argv[i]);
  69.         continue;
  70.         }
  71.  
  72.         for (cp = &arg[1]; *cp; cp++) {
  73.         switch (*cp) {
  74.           case 'a':        /* -all */
  75.             all_screens = True;
  76.             continue;
  77.           case 'l':        /* -long */
  78.             verbose = True;
  79.             continue;
  80.           default:
  81.             usage ();
  82.         }
  83.         }
  84.     } else {
  85.         usage ();
  86.     }
  87.     }
  88.  
  89.     dpy = XOpenDisplay (displayname);
  90.     if (!dpy) {
  91.     fprintf (stderr, "%s:  unable to open display \"%s\"\r\n",
  92.          ProgramName, XDisplayName (displayname));
  93.     exit (1);
  94.     }
  95.  
  96.     if (all_screens) {
  97.     for (i = 0; i < ScreenCount(dpy); i++) {
  98.         lookat (dpy, RootWindow(dpy,i), verbose, maxcmdlen);
  99.     }
  100.     } else {
  101.     lookat (dpy, DefaultRootWindow(dpy), verbose, maxcmdlen);
  102.     }
  103.  
  104.     XCloseDisplay (dpy);
  105.     exit (0);
  106. }
  107.  
  108. lookat (dpy, root, verbose, maxcmdlen)
  109.     Display *dpy;
  110.     Window root;
  111.     Bool verbose;
  112.     int maxcmdlen;
  113. {
  114.     Window dummy, *children = NULL, client;
  115.     unsigned int i, nchildren = 0;
  116.  
  117.     /*
  118.      * clients are not allowed to stomp on the root and ICCCM doesn't yet
  119.      * say anything about window managers putting stuff there; but, try
  120.      * anyway.
  121.      */
  122.     print_client_properties (dpy, root, verbose, maxcmdlen);
  123.  
  124.     /*
  125.      * then, get the list of windows
  126.      */
  127.     if (!XQueryTree (dpy, root, &dummy, &dummy, &children, &nchildren)) {
  128.     return;
  129.     }
  130.  
  131.     for (i = 0; i < nchildren; i++) {
  132.     client = XmuClientWindow (dpy, children[i]);
  133.     if (client != None)
  134.       print_client_properties (dpy, client, verbose, maxcmdlen);
  135.     }
  136. }
  137.  
  138. static char *Nil = "(nil)";
  139.  
  140. print_client_properties (dpy, w, verbose, maxcmdlen)
  141.     Display *dpy;
  142.     Window w;
  143.     Bool verbose;
  144.     int maxcmdlen;
  145. {
  146.     char **cliargv = NULL;
  147.     int i, cliargc;
  148.     XTextProperty nametp, machtp, tp;
  149.     int charsleft = maxcmdlen;
  150.  
  151.     /*
  152.      * get the WM_MACHINE and WM_COMMAND list of strings
  153.      */
  154.     if (!XGetWMClientMachine (dpy, w, &machtp)) {
  155.     machtp.value = NULL;
  156.     machtp.encoding = None;
  157.     }
  158.  
  159.     if (!XGetCommand (dpy, w, &cliargv, &cliargc)) {
  160.     if (machtp.value) XFree ((char *) machtp.value);
  161.     return;
  162.     }
  163.  
  164.     /*
  165.      * do header information
  166.      */
  167.     if (verbose) {
  168.     printf ("Window 0x%lx:\n", w);
  169.     print_text_field (dpy, "  Machine:  ", &machtp);
  170.     if (XGetWMName (dpy, w, &nametp)) {
  171.         print_text_field (dpy, "  Name:  ", &nametp);
  172.         if (nametp.value) XFree ((char *) nametp.value);
  173.     }
  174.     } else {
  175.     print_text_field (dpy, NULL, &machtp);
  176.     putchar (' ');
  177.     putchar (' ');
  178.     }
  179.     if (machtp.value) XFree ((char *) machtp.value);
  180.  
  181.     if (verbose) {
  182.     if (XGetWMIconName (dpy, w, &tp)) {
  183.         print_text_field (dpy, "  Icon Name:  ", &tp);
  184.         if (tp.value) XFree ((char *) tp.value);
  185.     }
  186.     }
  187.  
  188.  
  189.     /*
  190.      * do the command
  191.      */
  192.     if (verbose) {
  193.     printf ("  Command:  ");
  194.     }
  195.     for (i = 0; i < cliargc && charsleft > 0; ) {
  196.     charsleft -= print_quoted_word (cliargv[i], charsleft);
  197.     i++;
  198.     if (i < cliargc  &&  charsleft > 0) {
  199.         putchar (' '); charsleft--;
  200.     }
  201.     }
  202.     putchar ('\n');
  203.     XFreeStringList (cliargv);
  204.  
  205.  
  206.     /*
  207.      * do trailer information
  208.      */
  209.     if (verbose) {
  210.     XClassHint clh;
  211.     if (XGetClassHint (dpy, w, &clh)) {
  212.         printf ("  Instance/Class:  %s/%s",
  213.             clh.res_name ? clh.res_name : Nil,
  214.             clh.res_class ? clh.res_class : Nil);
  215.         if (clh.res_name) XFree (clh.res_name);
  216.         if (clh.res_class) XFree (clh.res_class);
  217.         putchar ('\n');
  218.     }
  219.     }
  220. }
  221.  
  222.  
  223. print_text_field (dpy, s, tp)
  224.     Display *dpy;
  225.     char *s;
  226.     XTextProperty *tp;
  227. {
  228.     if (tp->encoding == None || tp->format == 0) {
  229.     printf ("''");
  230.     return;
  231.     }
  232.  
  233.     if (s) printf ("%s", s);
  234.     if (tp->encoding == XA_STRING && tp->format == 8) {
  235.     printf ("%s", tp->value ? (char *) tp->value : Nil);
  236.     } else {
  237.     unknown (dpy, tp->encoding, tp->format);
  238.     }
  239.     if (s) putchar ('\n');
  240. }
  241.  
  242. /* returns the number of characters printed */
  243. int
  244. print_quoted_word (s, maxlen)
  245.     char *s;
  246.     int maxlen;            /* max number of chars we can print */
  247. {
  248.     register char *cp;
  249.     Bool need_quote = False, in_quote = False;
  250.     char quote_char = '\'', other_quote = '"';
  251.     int charsprinted = 0;
  252.  
  253.     /*
  254.      * walk down seeing whether or not we need to quote
  255.      */
  256.     for (cp = s; *cp; cp++) {
  257.  
  258.     if (! ((isascii(*cp) && isalnum(*cp)) || 
  259.            (*cp == '-' || *cp == '_' || *cp == '.' || *cp == '+' ||
  260.         *cp == '/' || *cp == '=' || *cp == ':' || *cp == ','))) {
  261.         need_quote = True;
  262.         break;
  263.     }
  264.     }
  265.  
  266.     /*
  267.      * write out the string: if we hit a quote, then close any previous quote,
  268.      * emit the other quote, swap quotes and continue on.
  269.      */
  270.     in_quote = need_quote;
  271.     if (need_quote) {
  272.     putchar (quote_char);
  273.     charsprinted++; maxlen--;
  274.     }
  275.     for (cp = s; *cp && maxlen>0; cp++) {
  276.     if (*cp == quote_char) {
  277.         if (in_quote) {
  278.         putchar (quote_char);
  279.         charsprinted++; maxlen--;
  280.         }
  281.         putchar (other_quote);
  282.         charsprinted++; maxlen--;
  283.         { 
  284.         char tmp = other_quote; 
  285.         other_quote = quote_char; quote_char = tmp;
  286.         }
  287.         in_quote = True;
  288.     }
  289.     putchar (*cp);
  290.     charsprinted++; maxlen--;
  291.     }
  292.     /* close the quote if we opened one and if we printed the whole string */
  293.     if (in_quote && maxlen>0) {
  294.     putchar (quote_char);
  295.     charsprinted++; maxlen--;
  296.     }
  297.  
  298.     return charsprinted;
  299. }
  300.  
  301. unknown (dpy, actual_type, actual_format)
  302.     Display *dpy;
  303.     Atom actual_type;
  304.     int actual_format;
  305. {
  306.     char *s;
  307.  
  308.     printf ("<unknown type ");
  309.     if (actual_type == None) printf ("None");
  310.     else if ((s = XGetAtomName (dpy, actual_type)) != NULL) {
  311.     fputs (s, stdout);
  312.     XFree (s);
  313.     } else {
  314.     fputs (Nil, stdout);
  315.     }
  316.     printf (" (%ld) or format %d>", actual_type, actual_format);
  317. }
  318.  
  319.