home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games 1996 October / Amiga_Games_Extra_CD_10-96.bin / userbox / publicdomain / grabkey / source / gk_main.c < prev    next >
C/C++ Source or Header  |  1996-07-15  |  4KB  |  299 lines

  1. /*
  2. **    GrabKEY
  3. **
  4. **        © 1996 by Timo C. Nentwig
  5. **        All Rights Reserved !
  6. **
  7. **        Tcn@techbase.in-berlin.de
  8. **
  9. **
  10. */
  11.  
  12. /// #include
  13.  
  14. #include "gk_GST.h"
  15. #include "gk_Protos.h"
  16.  
  17. ///
  18. /// proto
  19.  
  20. static BOOL    OpenLibs     (VOID);
  21. static VOID    CloseLibs    (VOID);
  22.  
  23. ///
  24.  
  25.     // System libraries
  26.  
  27. struct    Library         *CxBase        = NULL;
  28. struct    Library         *DataTypesBase = NULL;
  29. struct    Library         *GadToolsBase  = NULL;
  30. struct    GfxBase         *GfxBase       = NULL;
  31. struct    Library         *IconBase      = NULL;
  32. struct    IntuitionBase   *IntuitionBase = NULL;
  33.  
  34.     // External libraries
  35.  
  36. struct    Library         *GTLayoutBase;
  37. struct    ReqToolsBase    *ReqToolsBase;
  38.  
  39.     // Settings structure
  40.  
  41. struct    Settings        *Set;
  42.  
  43.     // C:Version string
  44.  
  45. static const STRPTR    __ver = "$VER: " PRG_TITLE " " PRG_VERSION " " __AMIGADATE__;
  46.  
  47.     // Libraries
  48.  
  49. static const struct { STRPTR Name; ULONG Version; APTR *Library; } Table [] =
  50. {
  51.  
  52.     "commodities.library", 39, (APTR *) &CxBase,
  53.     "datatypes.library",   39, (APTR *) &DataTypesBase,
  54.     "dos.library",         39, (APTR *) &DOSBase,
  55.     "gadtools.library",    39, (APTR *) &GadToolsBase,
  56.     "graphics.library",    39, (APTR *) &GfxBase,
  57.     "gtlayout.library",    27, (APTR *) >LayoutBase,
  58.     "icon.library",        39, (APTR *) &IconBase,
  59.     "intuition.library",   39, (APTR *) &IntuitionBase,
  60.     "layers.library",      39, (APTR *) &LayersBase,
  61.  
  62.     NULL,
  63.  
  64. };
  65.  
  66. /// main ()
  67.  
  68. LONG
  69. main (VOID)
  70. {
  71.  
  72.     LONG    Result = RETURN_FAIL;
  73.  
  74.         // Open system libraries
  75.  
  76.     if (OpenLibs())
  77.     {
  78.  
  79.             // Cannot be opened by OpenLibs()
  80.  
  81.         if (ReqToolsBase = (struct ReqToolsBase *) OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION))
  82.         {
  83.  
  84.                 // Try to open locale stuff
  85.  
  86.             LocaleOpen (PRG_TITLE ".catalog", "english", 2);
  87.  
  88.                 // Setup settings structure
  89.  
  90.             if (Set = AllocStruct (Settings))
  91.             {
  92.  
  93.                     // Initialize settings
  94.  
  95.                 InitSettings();
  96.  
  97.                     // Evaluate ToolTypes
  98.  
  99.                 EvalTTypes();
  100.  
  101.                     // Evaluate Shell arguments
  102.  
  103.                 EvalArgs();
  104.  
  105.                     // Setup commodity
  106.  
  107.                 if (SetupCx())
  108.                 {
  109.  
  110.                     BOOL     Done = FALSE;
  111.                     ULONG    Signals;
  112.  
  113.                     Result = RETURN_OK;
  114.  
  115.                     if (Set -> Cx . PopUp)
  116.                     {
  117.  
  118.                         CreateGUI();
  119.  
  120.                     }
  121.  
  122.                     Signals = PortMask (CxPort) | GUIMask;
  123.  
  124.                     do
  125.                     {
  126.  
  127.                             // Got signal from GUI
  128.  
  129.                         if (Signals & GUIMask)
  130.                         {
  131.  
  132.                             Done = HandleGUI();
  133.  
  134.                         }
  135.  
  136.                             // Got commodity signal
  137.  
  138.                         if (Signals & PortMask (CxPort))
  139.                         {
  140.  
  141.                             CxMsg   *Message;
  142.  
  143.                             while (Message = (CxMsg *) GetMsg (CxPort))
  144.                             {
  145.  
  146.                                 if (HandleCx (Message))
  147.                                 {
  148.  
  149.                                     Done = TRUE;
  150.                                     break;
  151.  
  152.                                 }
  153.  
  154.                             }
  155.  
  156.                         }
  157.  
  158.                             // Got DOS signal: CTRL-C
  159.  
  160.                         if (Signals & SIGBREAKF_CTRL_C)
  161.                         {
  162.  
  163.                             Done = TRUE;
  164.  
  165.                         }
  166.  
  167.                             // Wait for signals
  168.  
  169.                         if ( ! (Done))
  170.                         {
  171.  
  172.                             Signals = Wait (GUIMask | SIGBREAKF_CTRL_C | PortMask (CxPort));
  173.  
  174.                         }
  175.  
  176.                     }
  177.                     while ( ! (Done));
  178.  
  179.                     RemoveGUI();
  180.  
  181.                 }
  182.  
  183.                     // Quit program
  184.  
  185.                 QuitCx();
  186.  
  187.                     // Free settings structure
  188.  
  189.                 FreeStruct (Set);
  190.  
  191.             }
  192.             else
  193.             {
  194.  
  195.                 ErrorRequest ("Quit", LocaleString (MSG_ERR_OUT_OF_MEMORY));
  196.  
  197.             }
  198.  
  199.                 // Close locale stuff
  200.  
  201.             LocaleClose();
  202.  
  203.             CloseLib (ReqToolsBase);
  204.  
  205.         }
  206.         else
  207.         {
  208.  
  209.             FPrintf (Output(), PRG_TITLE ": Failed to open reqtools.library v%ld+ !\n", REQTOOLSVERSION);
  210.  
  211.         }
  212.  
  213.     }
  214.  
  215.     CloseLibs();
  216.  
  217. #if MWDEBUG
  218.  
  219.     MWReport (PRG_TITLE " statistics:", MWR_FULL);
  220.  
  221. #endif
  222.  
  223.     return (Result);
  224.  
  225. }
  226.  
  227. ///
  228. /// OpenLibs ()
  229.  
  230.     /*
  231.      *    FUNCTION    Open a required libraries.
  232.      *
  233.      *    NOTE        <Table> is defined above.
  234.      *
  235.      *    EXAMPLE     OpenLibs ();
  236.      *
  237.      */
  238.  
  239.  
  240. static BOOL
  241. OpenLibs (VOID)
  242. {
  243.  
  244.     LONG    i;
  245.  
  246.         // Open libraries
  247.  
  248.     for (i = 0; Table [i] . Name != NULL; i++)
  249.     {
  250.  
  251.             // Failed to open a library
  252.  
  253.         if ( ! (*Table [i] . Library = OpenLibrary (Table [i] . Name, Table [i] . Version)))
  254.         {
  255.  
  256.             FPrintf (Output(), PRG_TITLE ": Failed to open \"%s\" V%ld+ !\n", Table [i] . Name, Table [i] . Version);
  257.             return (FALSE);
  258.  
  259.         }
  260.  
  261.     }
  262.  
  263.     return (TRUE);
  264.  
  265. }
  266.  
  267. ///
  268. /// CloseLibs ()
  269.  
  270.     /*
  271.      *    FUNCTION    Close all opened libraries.
  272.      *
  273.      *    NOTE        <Table> is defined above.
  274.      *
  275.      *    EXAMPLE     CloseLibs ();
  276.      *
  277.      */
  278.  
  279.  
  280. static VOID
  281. CloseLibs (VOID)
  282. {
  283.  
  284.     LONG    i;
  285.  
  286.         // Close libraries
  287.  
  288.     for (i = 0; Table [i] . Name != NULL; i++)
  289.     {
  290.  
  291.         CloseLib (*Table [i] . Library);
  292.  
  293.     }
  294.  
  295. }
  296.  
  297. ///
  298.  
  299.