home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Extras / Development / Example_Code_v37 / Libraries / Commodities / Common / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-06  |  6.2 KB  |  210 lines

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