home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume14 / xtoolplaces / part01 / is.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-26  |  8.5 KB  |  264 lines

  1. /*Copyright (c) 1991 Xerox Corporation.  All Rights Reserved.
  2.  
  3.   Permission to use,  copy,  modify  and  distribute  without
  4.   charge this software, documentation, images, etc. is grant-
  5.   ed, provided that this copyright and the author's  name  is
  6.   retained.
  7.  
  8.   A fee may be charged for this program ONLY to recover costs
  9.   for distribution (i.e. media costs).  No profit can be made
  10.   on this program.
  11.  
  12.   The author assumes no responsibility for disasters (natural
  13.   or otherwise) as a consequence of use of this software.
  14.  
  15.   Adam Stein (stein.wbst129@xerox.com)
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <X11/Xlib.h>
  20. #include <X11/Xutil.h>
  21. #include <X11/Xatom.h>
  22. #include "xtoolplaces.h"
  23.  
  24. extern char *remote,hostname[];
  25. extern Atom wm_save_yourself,wm_client_machine;
  26. extern Display *display;
  27. extern FILE *fp;
  28.  
  29. /*This function will check a list of protocols to see if the WM_SAVE_YOURSELF
  30.   protocol is in the list
  31.  
  32.   Inputs:  count            - number of protocols in list
  33.        protocols        - protocol list
  34.   Outputs: 1 if WM_SAVE_YOURSELF is found, 0 otherwise
  35.   Locals:  none
  36.   Globals: wm_save_yourself - WM_SAVE_YOURSELF atom
  37. */
  38. is_save_yourself(protocols,count)
  39. register int count;
  40. register Atom *protocols;
  41. {
  42.     for(;count;--count)
  43.       if(protocols[count-1] == wm_save_yourself)
  44.         return(1);
  45.  
  46.     return(0);
  47. }
  48.  
  49. /*This function will determine if a window is acting as a console window.  It
  50.   does this by first checking the class of the window.  Since there is no
  51.   standard class for consoles, maybe this program will start one.  The window
  52.   is considered a console if it's class is XConsole.  If the class isn't
  53.   XConsole (which is most likely the case), functions are called to check
  54.   for each console type that I could think of.  This includes contool,
  55.   Sun's shelltool & cmdtool, and xterm.  Each (of course) has a different
  56.   way of identifying when it's on console mode.  To add other consoles, just
  57.   add another function call here and the function to make the check.  A 1
  58.   is returned if the window is a console, 0 otherwise.
  59.  
  60.   Inputs:  command_line  - WM_COMMAND arguments in a single string
  61.        window        - ID of the window that we are going to check
  62.   Outputs: console       - indicates if a window is a console
  63.   Locals:  class_hints   - window class
  64.        console       - indicates if a window is a console
  65.        status        - status of get call
  66.   Globals: display       - interface info to X display
  67.        CONSOLE_CLASS - class name for console windows
  68. */
  69. is_console(window,command_line)
  70. register char *command_line;
  71. register Window window;
  72. {
  73.     register int console = 0;
  74.     register Status status;
  75.     XClassHint class_hints;
  76.  
  77.     /*Get window class and check it against what a class for a console
  78.       window should be*/
  79.     status = XGetClassHint(display,window,&class_hints);
  80.     if(status && !strcmp(class_hints.res_class,CONSOLE_CLASS))
  81.       console = 1;
  82.     XFree(class_hints.res_name);
  83.     XFree(class_hints.res_class);
  84.  
  85.     /*If the class doesn't match, check for each type of console
  86.       window possible*/
  87.     if(!console)
  88.       if(!(console = check_contool(window)))
  89.         if(!(console = check_sun_tools(window)))
  90.           console = check_xterm(window,command_line);
  91.  
  92.     return(console);
  93. }
  94.  
  95. /*This function will check to see if a window is the contool window.  Since
  96.   contool is always a console, we only have to check the window name to see
  97.   if the word 'Contool' is in the name.  A 1 is returned if it is a contool
  98.   window, 0 otherwise.
  99.  
  100.   Inputs:  window       - ID of the window that we are going to check
  101.   Outputs: contool      - flag indicating if window is contool window or not
  102.   Locals:  contool      - flag indicating if window is contool window or not
  103.        name        - X window name
  104.   Globals: display      - interface info to X display
  105.        CONTOOL_NAME - name of contool window
  106. */
  107. check_contool(window)
  108. register Window window;
  109. {
  110.     register int contool = 0;
  111.     register char *name;
  112.     char *getname();
  113.  
  114.     /*Get the name of the window to compare it against the name contool
  115.       gives it's window*/
  116.     name = getname(window);
  117.     if(name && strstr(name,CONTOOL_NAME)) {
  118.       contool = 1;
  119.       XFree(name);
  120.     }
  121.  
  122.     return(contool);
  123. }
  124.  
  125. /*This function will check to see if a window is either a shelltool or
  126.   cmdtool in the console mode.  Since the icon name is CONSOLE only when
  127.   either is started up in console mode, we can check that.  A 1 is returned
  128.   if it is in console mode, 0 otherwise.
  129.  
  130.   Inputs:  window              - ID of the window that we are going to check
  131.   Outputs: sun_tools           - flag indicating if window is in console mode
  132.   Locals:  status              - status of get call
  133.        sun_tools            - flag indicating if window is in console mode
  134.        tp                   - window's text property
  135.   Globals: display           - interface info to X display
  136.        SUN_TOOLS_ICON_NAME - icon name for {shell,cmd}tool in console mode
  137. */
  138. check_sun_tools(window)
  139. register Window window;
  140. {
  141.     register int sun_tools = 0;
  142.     register Status status;
  143.     XTextProperty tp;
  144.  
  145.     /*Get window's icon name and check it against what Sun labels the icon
  146.       of either shelltool or cmdtool in console mode*/
  147.     status = XGetWMIconName(display,window,&tp);
  148.     if(status && !strcmp(tp.value,SUN_TOOLS_ICON_NAME))
  149.       sun_tools = 1;
  150.  
  151.     XFree(tp.value);
  152.  
  153.     return(sun_tools);
  154. }
  155.  
  156. /*This function will check to see if a window is xterm in the console mode.
  157.   The only way to determine if xterm is in console mode is to check the
  158.   options to see if it was started with the -C option.  A 1 is returned
  159.   if it is in console mode, 0 otherwise.
  160.  
  161.   Inputs:  command_line - WM_COMMAND arguments in a single string
  162.        window       - ID of the window that we are going to check
  163.   Outputs: xterm        - flag indicating if window is xterm in console mode
  164.   Locals:  xterm        - flag indicating if window is xterm in console mode
  165.   Globals: XTERM_OPTION - xterm option to put it into console mode
  166. */
  167. check_xterm(window,command_line)
  168. register char *command_line;
  169. register Window window;
  170. {
  171.     register int xterm = 0;
  172.  
  173.     /*See if it's xterm*/
  174.     if(is_xterm(window) == 1)
  175.       /*If window is xterm, then check argument list for xterm console
  176.         mode option*/
  177.       if(strstr(command_line,XTERM_OPTION)) xterm = 1;
  178.  
  179.     return(xterm);
  180. }
  181.  
  182. /*This function determines if a particular window is an xterm window.  To
  183.   see if a window is an xterm window, we check it's class.  Xterm always sets
  184.   it class to "XTerm".  1 is returned if it is an xterm window, 0 if it
  185.   isn't, -1 if there is an error in getting the name of the window.
  186.  
  187.   Inputs:  window   - ID of the window that we are going to check
  188.   Outputs: 1 if xterm, 0 if not xterm, -1 if error in getting name
  189.   Locals:  match    - flag indicating if window is xterm window or not
  190.        status   - status of get call
  191.        tp        - window's text property
  192.   Globals: display  - interface info to X display
  193. */
  194. is_xterm(window)
  195. register Window window;
  196. {
  197.     register int match;
  198.     register Status status;
  199.     XClassHint class_hints;
  200.  
  201.     /*Get window class to see if it's xterm*/
  202.     status = XGetClassHint(display,window,&class_hints);
  203.  
  204.     if(status) {
  205.       match = strcmp(class_hints.res_class,"XTerm");
  206.       XFree(class_hints.res_name);
  207.       XFree(class_hints.res_class);
  208.  
  209.       return((!match)?1:0);
  210.     } else return(-1);
  211. }
  212.  
  213.  
  214. /*This function checks to see if an application is being executed remotely.
  215.   If it is, it prints out the remote execution command (typically 'rsh -n') and
  216.   the name of the remote machine to execute on.
  217.  
  218.   Inputs:  window          - ID of the window that we are going to check
  219.   Outputs: none
  220.   Locals:  status          - status of get call
  221.        tp              - window's text property
  222.   Globals: display         - interface info to X display
  223.        fp             - file pointer to write window information to
  224.        hostname         - name of host
  225.        remote         - command to use for remote applications
  226.        wm_client_machine - WM_CLIENT_MACHINE atom
  227. */
  228. is_remote(window)
  229. register Window window;
  230. {
  231.     register Status status;
  232.     XTextProperty tp;
  233.  
  234.     status = XGetTextProperty(display,window,&tp,wm_client_machine);
  235.  
  236.     /*Check if WM_CLIENT_MACHINE exists and isn't the name of the
  237.       machine we are currently executing on*/
  238.     if(status && strcmp(tp.value,hostname))
  239.       fprintf(fp,"%s %s ",remote,tp.value);
  240. }
  241.  
  242. /*This function gets the name of an X window and returns it.
  243.  
  244.   Inputs:  window   - ID of the window that we are going to check
  245.   Outputs: tp.value - name of X window
  246.   Locals:  status   - status of get call
  247.        tp       - window's text property
  248.   Globals: display  - interface info to X display
  249.        NULL     - 0
  250. */
  251. char *getname(window)
  252. register Window window;
  253. {
  254.     register Status status;
  255.     XTextProperty tp;
  256.  
  257.     /*Get the name of the window*/
  258.     status = XGetWMName(display,window,&tp);
  259.  
  260.     if(status) return((char *) tp.value);
  261.     else return(NULL);
  262. }
  263.  
  264.