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

  1. /*
  2.  *  SCREENTOBACK    A small utility to manipulate the screen positions
  3.  *                  via a CLI command.  Can be used within EXECUTE files,
  4.  *                  and the STARTUP-SEQUENCE.
  5.  *
  6.  *  Copyright 1990 by Davide P. Cervone, all rights reserved.
  7.  *  You may use this code, provided this copyright notice is kept intact.
  8.  */
  9.  
  10. #define INTUITION_PREFERENCES_H             /* don't need 'em */
  11. #include <intuition/intuitionbase.h>
  12.  
  13. #define USAGE   "ScreenToBack [ACTIVATE] [screen]"
  14.  
  15. static char *program = "ScreenToBack";
  16. static char *version = "v1.3";
  17. static char *copyright =
  18.    "Copyright (C) 1990 by Davide P. Cervone, all rights reserved.";
  19.  
  20.  
  21. #define INTUITION_REV   0L
  22. extern struct IntuitionBase *IntuitionBase;
  23. extern struct IntuitionBase *OpenLibrary();
  24.  
  25. #define ARGMATCH(s)     (stricmp(argv[1],s) == 0)
  26. #define TOUPPER(c)      (((c)>='a'&&(c)<='z')?(c)-'a'+'A':c)
  27.  
  28.  
  29. /*
  30.  *  PrefixMatch()
  31.  *
  32.  *  Case-insensitive prefix match.  NULL pointers and empty strings
  33.  *  only match themselves, and are not considered prefixes to any string.
  34.  *
  35.  *  Return <0 if the first is smaller than the second,
  36.  *         =0 if the first is a prefix of the second,
  37.  *         >1 if the first is larger than the second.
  38.  */
  39.  
  40. int PrefixMatch(s1,s2)
  41. char *s1,*s2;
  42. {
  43.    int match = -1;
  44.    
  45.    if (s1 && *s1)
  46.    {
  47.       if (s2)
  48.       {
  49.          while (TOUPPER(*s1) == TOUPPER(*s2) && *s2 != 0) s1++,s2++;
  50.          if (*s1 == 0) match = 0; else match = *s1 - *s2;
  51.       } else match = 1;
  52.    } else if (s2 == NULL || *s2 == 0) match = 0;
  53.    return(match);
  54. }
  55.  
  56.  
  57. /*
  58.  *  *FindScreen()
  59.  *
  60.  *  Lock IntuitionBase so nothing happens while we look.
  61.  *  If there is a name to look for,
  62.  *    Start at the first screen, and look for a screen whose title
  63.  *      matches the given name.  If none, return NULL.
  64.  *  Otherwise, use the active screen.
  65.  *  Unlock Intuition.
  66.  *  Return the screen pointer found, if any.
  67.  */
  68.  
  69. static struct Screen *FindScreen(ScreenName)
  70. char *ScreenName;
  71. {
  72.    struct Screen *theScreen;
  73.    long ILock, LockIBase();
  74.  
  75.    ILock = LockIBase(0L);
  76.    if (ScreenName)
  77.    {
  78.       theScreen = IntuitionBase->FirstScreen;
  79.       while (theScreen && PrefixMatch(ScreenName,theScreen->Title))
  80.          theScreen = theScreen->NextScreen;
  81.    } else {
  82.       theScreen = IntuitionBase->ActiveScreen;
  83.    }
  84.    UnlockIBase(ILock);
  85.    return(theScreen);
  86. }
  87.  
  88.  
  89. /*
  90.  *  Print()
  91.  *
  92.  *  Find the standard AmigaDOS output file and write the output string
  93.  *  to the output file.  This is intended as a substitute for printf()
  94.  *  when no formatting is required, and when you want a small executable.
  95.  */
  96.  
  97. static void Print(s)
  98. char *s;
  99. {
  100.    ULONG OutFile;
  101.    extern ULONG Output();
  102.    
  103.    OutFile = Output();
  104.    if (OutFile && s) Write(OutFile,s,strlen(s));
  105. }
  106.  
  107.  
  108. /*
  109.  *  Error()
  110.  *
  111.  *  Print the character string, and up to two optional strings, then
  112.  *  go to a new output line, close Intuition, and exit with an error.
  113.  */
  114.  
  115. static void Error(s,x1,x2)
  116. char *s,*x1,*x2;
  117. {
  118.    Print(s);
  119.    if (x1) Print(x1);
  120.    if (x2) Print(x2);
  121.    Print("\n");
  122.    if (IntuitionBase) CloseLibrary(IntuitionBase);
  123.    _exit(10L);
  124. }
  125.  
  126.  
  127. /*
  128.  *  main()
  129.  *
  130.  *  If the first argument is ACTIVATE, set the flag and skip the argument.
  131.  *  If there are too many arguements, show the Usage message.
  132.  *  If there is one arguemtn make it the screen name.
  133.  *  Open Intuition; if successful,
  134.  *    Find the spcified screen (use the active screen if none specified).
  135.  *    If found, then
  136.  *      Send it to the back, and get the new top screen.
  137.  *      If we are to activate the new screen and it is not already
  138.  *       active, then activate its first window.
  139.  *    Otherwise, say we can't find the screen.
  140.  *    Close Intuition.
  141.  *  Otherwise, say we can't get Intuition.
  142.  */
  143.  
  144. void main(argc,argv)
  145. int argc;
  146. char *argv[];
  147. {
  148.    char *ScreenName = NULL;
  149.    struct Screen *theScreen;
  150.    int Activate = FALSE;
  151.  
  152.    if (argc > 1 && ARGMATCH("ACTIVATE")) Activate = TRUE, argv++, argc--;
  153.    if (argc > 2) Error("Usage:  ",USAGE,NULL);
  154.    if (argc > 1) ScreenName = argv[1];
  155.  
  156.    IntuitionBase = OpenLibrary("intuition.library",INTUITION_REV);
  157.    if (IntuitionBase)
  158.    {
  159.       theScreen = FindScreen(ScreenName);
  160.       if (theScreen)
  161.       {
  162.          ScreenToBack(theScreen);
  163.          Forbid();
  164.          theScreen = IntuitionBase->FirstScreen;
  165.          if (Activate && theScreen != IntuitionBase->ActiveScreen)
  166.             if (theScreen->FirstWindow) ActivateWindow(theScreen->FirstWindow);
  167.          Permit();
  168.       } else Error("Can't find screen '",ScreenName,"'");
  169.       CloseLibrary(IntuitionBase);
  170.    } else Error("Can't Open Intuition Library!",NULL,NULL);
  171. }
  172.