home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 20 / amigaformatcd20.iso / -readerstuff- / steve_glover / source-code / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-20  |  7.3 KB  |  269 lines

  1.  
  2.    /***********************************************************************
  3.    *                                                                      *
  4.    *                            COPYRIGHTS                                *
  5.    *                                                                      *
  6.    *   Copyright (c) 1990  Commodore-Amiga, Inc.  All Rights Reserved.    *
  7.    *   This file was modified by © Zinneberg-Soft.                        *
  8.    ***********************************************************************/
  9.  
  10. /*
  11.  * main.c -- Skeleton program for a typical Commodities application.
  12.  */
  13. #include "local.h"
  14.  
  15. struct IntuitionBase *IntuitionBase = NULL;
  16. struct Library       *CxBase        = NULL;
  17. struct GfxBase       *GfxBase       = NULL;
  18. struct Library       *GadToolsBase  = NULL;
  19. struct Library       *IconBase      = NULL;
  20. struct Library       *CyberGfxBase  = NULL;
  21. struct Library       *DiskfontBase  = NULL;
  22.   
  23. char     **ttypes;
  24.  
  25.  
  26. /* these globals are the connection between the main program
  27.  * loop and the two message handling routines
  28.  */
  29.  
  30. struct MsgPort *cxport   = NULL; /* commodities messages here      */
  31. ULONG          cxsigflag = 0;    /* signal for above               */
  32.  
  33.  
  34. struct MsgPort *iport    = NULL; /* Intuition IDCMP messages here   */
  35. ULONG          isigflag  = 0;    /* signal for above                */
  36.  
  37. #if CSIGNAL
  38. LONG           csignal   = -1L;
  39. struct Task    *maintask = NULL;
  40. #endif
  41. ULONG          csigflag  = 0L;
  42.  
  43. VOID main(int,char **);
  44. VOID main( argc, argv )
  45. int  argc;
  46. char **argv;
  47. {
  48.    ULONG    sigrcvd;
  49.    struct   Message    *msg;
  50.    W( char  *str; )
  51.  
  52.    CxBase       =                        OpenLibrary("commodities.library",40L);
  53.    DOSBase      =(struct DosLibrary *)   OpenLibrary("dos.library",40L);
  54.    IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",40L);
  55.    GfxBase      =(struct GfxBase *)      OpenLibrary("graphics.library",40L);
  56.    GadToolsBase =                        OpenLibrary("gadtools.library",40L);
  57.    IconBase     =                        OpenLibrary("icon.library",40L);
  58.    CyberGfxBase =                        OpenLibrary("cybergraphics.library",40L);
  59.    DiskfontBase =                        OpenLibrary("diskfont.library",39L);
  60.  
  61.  
  62.  
  63. if ( ! ( IntuitionBase ) )
  64.    {
  65.  
  66.        ErrorF ("Can't open intuition.library 40L.");
  67.       terminate();
  68.    }
  69.  
  70. if ( ! ( CxBase ) )
  71.    {
  72.  
  73.        ErrorF ("Can't open commodities.library 40L.");
  74.       terminate();
  75.    }
  76.  
  77. if ( ! ( GfxBase ) )
  78.    {
  79.  
  80.        ErrorF ("Can't open graphics.library 40L.");
  81.       terminate();
  82.    }
  83.  
  84. if ( ! ( DOSBase  ) )
  85.    {
  86.  
  87.        ErrorF ("Can't open dos.library 40L.");
  88.       terminate();
  89.    }
  90.  
  91. if ( ! ( GadToolsBase ) )
  92.    {
  93.  
  94.        ErrorF ("Can't open gadtools.library 40L.");
  95.       terminate();
  96.    }
  97.  
  98. if ( ! ( IconBase ) )
  99.    {
  100.  
  101.        ErrorF ("Can't open icon.library 40L.");
  102.       terminate();
  103.    }
  104.  
  105. if ( ! ( CyberGfxBase ) )
  106.    {
  107.  
  108.        ErrorF ("Can't open cybergraphics.library 40L.");
  109.       terminate();
  110.    }
  111.  
  112. if ( ! ( DiskfontBase ) )
  113.    {
  114.  
  115.        ErrorF ("Can't open diskfont.library 39L.");
  116.       terminate();
  117.    }
  118.  
  119. #if CSIGNAL
  120.    if((csignal = AllocSignal(-1L))==-1)
  121.    {
  122.       D1( kprintf("main.c: main() Failed to get custom signal\n") );
  123.       terminate();
  124.    }
  125.    csigflag = 1L <<csignal;
  126.    maintask=FindTask(0L);
  127. #endif
  128.  
  129.    /* commodities support library function to find argv or tooltypes   */
  130.    ttypes = ArgArrayInit( argc, argv );
  131.    D1(
  132.       {
  133.          int xx=0;
  134.  
  135.          while(ttypes[xx])
  136.          {
  137.             printf("ttypes[0x%lx]=%s\n",xx,ttypes[xx]);
  138.             xx++;
  139.          }
  140.       }
  141.    )
  142.  
  143.    if ( ! setupCX( ttypes ) )
  144.    {
  145.       D1( kprintf("main.c: main() setupCX failed\n") );
  146.       terminate();
  147.    }
  148.    W(
  149.       str = ArgString( ttypes, POP_ON_START_TOOL_TYPE,CX_DEFAULT_POP_ON_START);
  150.       if(strcmpi(str,"yes")==0)
  151.          setupWindow();         /* will try to setup iport      */
  152.       D1( kprintf("main.c: main() After setupWindow\n") );
  153.    )
  154.  
  155.    for (;;)            /* exit by calling terminate   */
  156.    {
  157.       /* handling two ports:
  158.        * either will wake us up;
  159.        * simple approach often works.
  160.        */
  161.       D1( kprintf("main.c: main() Waiting for a signal\n") );
  162.       sigrcvd = Wait ( SIGBREAKF_CTRL_C | isigflag | cxsigflag | csigflag );
  163.       D1( kprintf("main.c: main() Recieved a signal\n") );
  164.  
  165.       /* commodities convention: easy to kill   */
  166.       if ( sigrcvd & SIGBREAKF_CTRL_C )
  167.       {
  168.          D1( kprintf("main.c: main() Recieved a SIGBREAKF_CTRL_C\n") );
  169.          terminate();
  170.       }
  171.  
  172. #if CSIGNAL
  173.       if ( sigrcvd & csigflag )
  174.       {
  175.          D1( kprintf("main.c: main() Recieved a Custom Signal\n") );
  176.          handleCustomSignal();
  177.       }
  178. #endif
  179.  
  180.       while(cxport && (msg=GetMsg(cxport)))
  181.          handleCxMsg(msg);
  182.       W(
  183.          while(iport && (msg=(struct Message *)GT_GetIMsg(iport)))
  184.             handleIMsg((struct IntuiMessage *)msg);
  185.       )
  186.    }
  187.  
  188. }
  189. /****i* Blank.ld/terminate() ******************************************
  190. *
  191. *   NAME
  192. *        terminate -- Cleanup all resources and exit the program.
  193. *
  194. *   SYNOPSIS
  195. *        terminate()
  196. *
  197. *        VOID terminate(VOID);
  198. *
  199. *   FUNCTION
  200. *        This function performs all the necessary cleanup for the
  201. *        commodity and calls exit() to return control to the OS.
  202. *        No matter how the program exits this should be the last function
  203. *        called.
  204. *
  205. *   INPUTS
  206. *        None.
  207. *
  208. *   RESULT
  209. *        All resources are freed and the program exits.
  210. *
  211. *   EXAMPLE
  212. *        if(!AllocWindow())
  213. *           terminate();
  214. *
  215. *   NOTES
  216. *        This function must be set up so that it can be called at any
  217. *        time regardless of the current state of the program.
  218. *
  219. *   BUGS
  220. *
  221. *   SEE ALSO
  222. *        shutdownCX();
  223. *        shutdownWindow();
  224. *
  225. *****************************************************************************
  226. *
  227. */
  228. VOID terminate()
  229. {
  230.    D1( kprintf("main.c: terminate() enter\n") );
  231.  
  232.    shutdownCX();
  233.    D1( kprintf("main.c: terminate(), after shutdownCX()\n") );
  234.  
  235.    W(
  236.       shutdownWindow();
  237.       D1( kprintf("main.c: terminate(), after shutdownWindow()\n") );
  238.    )
  239.  
  240.    ArgArrayDone();    /* cx_supp.lib function   */
  241.    D1( kprintf("main.c: terminate(), after ArgArrayDone()\n") );
  242.  
  243. #if CSIGNAL
  244.    if(csignal != -1) FreeSignal(csignal);
  245.    D1( kprintf("main.c: terminate(), after FreeSignal()()\n") );
  246. #endif
  247.  
  248.    D1( kprintf("main.c: terminate() CxBase        = %lx\n",CxBase) );
  249.    D1( kprintf("main.c: terminate() IntuitionBase = %lx\n",IntuitionBase) );
  250.    D1( kprintf("main.c: terminate() GfxBase       = %lx\n",GfxBase) );
  251.    D1( kprintf("main.c: terminate() DOSBase       = %lx\n",DOSBase) );
  252.    D1( kprintf("main.c: terminate() GadToolsBase  = %lx\n",GadToolsBase) );
  253.    D1( kprintf("main.c: terminate() IconBase      = %lx\n",IconBase) );
  254.    D1( kprintf("main.c: terminate() CyberGfxBase  = %lx\n",CyberGfxBase) );
  255.    D1( kprintf("main.c: terminate() DiskfontBase  = %lx\n",DiskfontBase) );
  256.  
  257.    if(CxBase)        CloseLibrary(CxBase);
  258.    if(IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
  259.    if(GfxBase)       CloseLibrary((struct Library *)GfxBase);
  260.    if(DOSBase)       CloseLibrary((struct Library *)DOSBase);
  261.    if(GadToolsBase)  CloseLibrary(GadToolsBase);
  262.    if(IconBase)      CloseLibrary(IconBase);
  263.    if(CyberGfxBase)  CloseLibrary((struct Library *)CyberGfxBase);
  264.    if (DiskfontBase) CloseLibrary((struct Library *)DiskfontBase);
  265.    D1( kprintf("main.c: terminate(), after CloseLibrarys()\n") );
  266.   
  267.    exit(0);
  268. }
  269.