home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d500 / wiconify.lha / wIconify / wUtilities.lzh / wIconVerify / wIconVerify.c < prev    next >
C/C++ Source or Header  |  1991-04-19  |  7KB  |  269 lines

  1. /*
  2.  *  WICONVERIFY A companion utility to wIconify that checks the windows
  3.  *              on a given screen (or all screens) to make sure that
  4.  *              all the iconified ones are invisible, and all the uniconified
  5.  *              ones are accessible.
  6.  *
  7.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  8.  *  You may use this code, provided this copyright notice is kept intact.
  9.  */
  10.  
  11.  
  12. #define INTUITION_PREFERENCES_H             /* don't need 'em */
  13. #include <intuition/intuitionbase.h>
  14.  
  15. #define USAGE   "wIconVerify [screen]"
  16.  
  17. static char *program = "wIconVerify";
  18. static char *version = "v1.0";
  19. static char *copyright =
  20.    "Copyright (C) 1990 by Davide P. Cervone, all rights reserved.";
  21.  
  22.  
  23. #define INTUITION_REV   0L
  24. extern struct IntuitionBase *IntuitionBase;
  25. extern struct IntuitionBase *OpenLibrary();
  26. extern struct Window *wBackDropOf();
  27.  
  28. #define MAXWINDOW       32          /* largest number of windows per screen */
  29.  
  30. #define TOUPPER(c)      (((c)>='a'&&(c)<='z')?(c)-'a'+'A':c)
  31.  
  32. /*
  33.  *  PrefixMatch()
  34.  *
  35.  *  Case-insensitive prefix match.  NULL pointers and empty strings
  36.  *  only match themselves, and are not considered prefixes to any string.
  37.  *
  38.  *  Return <0 if the first is smaller than the second,
  39.  *         =0 if the first is a prefix of the second,
  40.  *         >1 if the first is larger than the second.
  41.  */
  42.  
  43. static int PrefixMatch(s1,s2)
  44. char *s1,*s2;
  45. {
  46.    int match = -1;
  47.    
  48.    if (s1 && *s1)
  49.    {
  50.       if (s2)
  51.       {
  52.          while (TOUPPER(*s1) == TOUPPER(*s2) && *s2 != 0) s1++,s2++;
  53.          if (*s1 == 0) match = 0; else match = *s1 - *s2;
  54.       } else match = 1;
  55.    } else if (s2 == NULL || *s2 == 0) match = 0;
  56.    return(match);
  57. }
  58.  
  59.  
  60. /*
  61.  *  *FindScreen()
  62.  *
  63.  *  Lock IntuitionBase so nothing happens while we look.
  64.  *  If there is a name to look for,
  65.  *    Start at the first screen, and look for a screen whose title
  66.  *      matches the given name.  If none, return NULL.
  67.  *  Otherwise, use the first screen.
  68.  *  Unlock Intuition.
  69.  *  Return the screen pointer found, if any.
  70.  */
  71.  
  72. static struct Screen *FindScreen(ScreenName)
  73. char *ScreenName;
  74. {
  75.    struct Screen *theScreen;
  76.    long ILock, LockIBase();
  77.  
  78.    ILock = LockIBase(0L);
  79.    if (ScreenName)
  80.    {
  81.       theScreen = IntuitionBase->FirstScreen;
  82.       while (theScreen && PrefixMatch(ScreenName,theScreen->Title))
  83.          theScreen = theScreen->NextScreen;
  84.    } else {
  85.       theScreen = IntuitionBase->FirstScreen;
  86.    }
  87.    UnlockIBase(ILock);
  88.    return(theScreen);
  89. }
  90.  
  91.  
  92. /*
  93.  *  Print()
  94.  *
  95.  *  Find the standard AmigaDOS output file and write the output string
  96.  *  to the output file.  This is intended as a substitute for printf()
  97.  *  when no formatting is required, and when you want a small executable.
  98.  */
  99.  
  100. static void Print(s)
  101. char *s;
  102. {
  103.    ULONG OutFile;
  104.    extern ULONG Output();
  105.    
  106.    OutFile = Output();
  107.    if (OutFile && s) Write(OutFile,s,strlen(s));
  108. }
  109.  
  110.  
  111. /*
  112.  *  Error()
  113.  *
  114.  *  Print the character string, and up to two optional strings, then
  115.  *  go to a new output line, close Intuition, and exit with an error.
  116.  */
  117.  
  118. static void Error(s,x1,x2)
  119. char *s,*x1,*x2;
  120. {
  121.    Print(s);
  122.    if (x1) Print(x1);
  123.    if (x2) Print(x2);
  124.    Print("\n");
  125.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  126.    _exit(10L);
  127. }
  128.  
  129.  
  130. /*
  131.  *  CheckWindows()
  132.  *
  133.  *  Get the wIconify BackDrop window of the given screen
  134.  *  While there are more windows to check
  135.  *    If the window is the BackDrop window, note that we've moved behind it
  136.  *    Otherwise, if the window is iconified
  137.  *      but it is not behind the backdrop window
  138.  *        Print a message saying that it is being restored
  139.  *        Restore it, and mark the change
  140.  *    Otherwise, the window is not iconified
  141.  *      If it is behind the backdrop window
  142.  *        Print a message saying that is is being iconified
  143.  *        Iconify it and note the change
  144.  *    Go on to the next window
  145.  *  Return TRUE if any changes occured
  146.  */
  147.  
  148. static int CheckWindows(WindowPtr,i,theScreen)
  149. struct Window **WindowPtr;
  150. short i;
  151. struct Screen *theScreen;
  152. {
  153.    struct Window *BackDrop = wBackDropOf(theScreen);
  154.    int BehindBackDrop = FALSE;
  155.    int VerifyFail = FALSE; 
  156.    
  157.    while (i--)
  158.    {
  159.       if (*WindowPtr == BackDrop) BehindBackDrop = TRUE;
  160.       else if (wIsIconified(*WindowPtr))
  161.       {
  162.          if (!BehindBackDrop)
  163.          {
  164.             Print("Restoring window '");
  165.             Print((*WindowPtr)->Title);
  166.             Print("'\n");
  167.             wRestore(wIconOf(*WindowPtr));
  168.             VerifyFail = TRUE;
  169.          }
  170.       } else {
  171.          if (BehindBackDrop)
  172.          {
  173.             Print("Iconifying window '");
  174.             Print((*WindowPtr)->Title);
  175.             Print("'\n");
  176.             wIconify(*WindowPtr);
  177.             VerifyFail = TRUE;
  178.          }
  179.       }
  180.       WindowPtr++;
  181.    }
  182.    return(VerifyFail);
  183. }
  184.  
  185.  
  186. /*
  187.  *  Verify()
  188.  *
  189.  *  While there are more screens to look at
  190.  *    Lock Intuition so that nothing changes while we look
  191.  *    Start at the top layer of the screen (the window list is not in front-
  192.  *      to-back order, so we have to use the layer list)
  193.  *    While there are more layers to check and we can hold more windows
  194.  *      If the layer has a window and it is not the last one we recorded
  195.  *        Record the layer's window
  196.  *      Go to the next layer
  197.  *    Unlock Intuition for the moment
  198.  *    Check if any of the windows are in the wrong place
  199.  *    If we are checking a single screen, end the loop,
  200.  *      otherwise go on to the next screen
  201.  */
  202.  
  203. static void Verify(theScreen,Name)
  204. struct Screen *theScreen;
  205. char *Name;
  206. {
  207.    APTR theWindow = NULL;
  208.    APTR WindowPtr[MAXWINDOW];
  209.    struct Layer *theLayer;
  210.    short i = 0;
  211.    long ILock, LockIBase();
  212.    int VerifyFail = FALSE;
  213.    
  214.    while (theScreen)
  215.    {
  216.       ILock = LockIBase(0L);
  217.       theLayer = theScreen->LayerInfo.top_layer;
  218.       while (theLayer && i < MAXWINDOW)
  219.       {
  220.          if (theLayer->Window && theLayer->Window != theWindow)
  221.             WindowPtr[i++] = theWindow = theLayer->Window;
  222.          theLayer = theLayer->back;
  223.       }
  224.       UnlockIBase(ILock);
  225.       VerifyFail |= CheckWindows(&WindowPtr[0],i,theScreen);
  226.       if (Name) theScreen = NULL; else theScreen = theScreen->NextScreen;
  227.    }
  228.    if (VerifyFail == FALSE) Print("No incorrect windows\n");
  229. }
  230.  
  231.  
  232. /*
  233.  *  main()
  234.  *
  235.  *  If there are too many arguments, print the Usage message.
  236.  *  If a screen name is given, use it.
  237.  *  Open Intuition; if successful,
  238.  *    Find the specified screen.
  239.  *    If found, then
  240.  *      If wIconify is running, verify the given screen(s)
  241.  *      Otherwise say that wIconify is not running.
  242.  *    Otherwise say that the screen can't be found.
  243.  *    Close Intuition.
  244.  *  Otherwise say that Intuition can't be openned.
  245.  */
  246.  
  247. void main(argc,argv)
  248. int argc;
  249. char *argv[];
  250. {
  251.    char *ScreenName = NULL;
  252.    struct Screen *theScreen;
  253.  
  254.    if (argc > 2) Error("Usage:  ",USAGE,NULL);
  255.    if (argc > 1 && argv[1]) ScreenName = argv[1];
  256.  
  257.    IntuitionBase = OpenLibrary("intuition.library",INTUITION_REV);
  258.    if (IntuitionBase)
  259.    {
  260.       theScreen = FindScreen(ScreenName);
  261.       if (theScreen)
  262.       {
  263.          if (wIconifyActive()) Verify(theScreen,ScreenName);
  264.           else Error("wIconify not running or incompatible version",NULL,NULL);
  265.       } else Error("Can't find screen '",ScreenName,"'");
  266.       CloseLibrary(IntuitionBase);
  267.    } else Error("Can't Open Intuition Library!",NULL,NULL);
  268. }
  269.