home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 530b.lha / AMenu_v1.3 / AMenu.c < prev    next >
C/C++ Source or Header  |  1991-07-03  |  7KB  |  240 lines

  1. /*********************************************************************\
  2. **                               ________________________________    **
  3. **    A n t h o n y             |________    __    __    ________|   **
  4. **                                       |  |o_|  |o_|  |            **
  5. **            T h y s s e n            __|   __    __   |__          **
  6. **                                  __|   __|  |  |  |__   |__       **
  7. **   `` Dragon Computing ! ''    __|   __|     |  |     |__   |__    **
  8. **                              |_____|        |__|        |_____|   **
  9. **                                                                   **
  10. \*********************************************************************/
  11. /* This program puts customized menus into the WorkBench menubar.
  12.  * The program is split up into two parts, AMenu and AMenu-Handler.
  13.  * This section is AMenu and it initializes and starts up a process
  14.  * to run AMenu-Handler, and then terminates.  AMenu also terminates
  15.  * and cleans up after the suprocess.  AMenu will parse the users
  16.  * menus, initialize data to be passed to AMenu-Handler, and start
  17.  * up AMenu-Handler.  AMenu-Handler does very little memory
  18.  * allocation in order to save space, so AMenu does allocation and
  19.  * freeing for it.
  20.  */
  21.  
  22. #include "AMenu.h"            /* Handler Message Port definition */
  23. #include "AllocStr.h"         /* String Allocation macros */
  24. #include "ArpC.h"             /* Header for ArpC startup code */
  25.  
  26.  
  27. struct MPort *M;              /* Externel Handler Port (Named Port) */
  28.  
  29. extern void  FreeMenus();     /* External Routines called */
  30. extern BOOL  ParseMenus();
  31.  
  32. /*---------------------------------------------------*/
  33.  
  34.  
  35. void
  36. ExitAMenu(Str,Ret)
  37.   char *Str;
  38.   int Ret;
  39. /* Print string and exit. The Arp, Graphics, and Intuition
  40. ** libraries are automatically closed by ArpC startup code
  41. */
  42. {
  43.   Puts(Str); exit(Ret);
  44. }
  45.  
  46.  
  47.  
  48. void
  49. RemoveHandler()
  50. /* shutdown and clean up after handler */
  51. {
  52.   if (!M)
  53.     return;
  54.   Printf("Removing -- ", VERSION);
  55.   DB( Puts(""); )
  56.   if( M->Segment ) {
  57.     DB( Puts("Signaling Handler to Exit - Waiting"); )
  58.     M->Parent = (struct Task *)Process;   /* from ArpC startup code */
  59.     M->ParentSig = AllocSignal(-1L);
  60.     Signal( M->Handler, SIGBREAKF_CTRL_C);   /* don't advertize use of ^C */
  61.     Wait(1 << M->ParentSig);
  62.     FreeSignal(M->ParentSig);
  63.  
  64.     DB( Puts("Handler has reported in"); )
  65.     switch( M->ErrorCode ) {
  66.     case ERR_OK:
  67.       break;
  68.     case ERR_WBOPEN:  /* WB process still active */
  69.       ExitAMenu("Leaving handler in place...\n", 0);
  70.     default:           /* some strange error */
  71.       Printf("Handler reports error (%ld)", (LONG)M->ErrorCode);
  72.       ExitAMenu("",0);
  73.     }
  74.  
  75.     DB( Puts("Unloading Port/Handler"); )
  76.     RemPort( &M->Port );
  77.     UnLoadSeg( M->Segment );
  78.   }
  79.  
  80.     /* this is done here, not in handler... */
  81.   DB( Puts("Freeing Menus/Port"); )
  82.   FreeMenus();
  83.   if( M->Port.mp_Node.ln_Name )
  84.     FreeStr(M->Port.mp_Node.ln_Name);
  85.   if( M->Directory )
  86.     FreeStr(M->Directory);
  87.   if( M->Console )
  88.     FreeStr(M->Console);
  89.   FreeMem(M, sizeof(struct MPort));
  90.   ExitAMenu("Done", 0);
  91. }
  92.  
  93.  
  94. void
  95. HandlerReport()
  96. /* report the result from load or update of handler */
  97. {
  98.   switch( M->ErrorCode ) {
  99.   case ERR_OK:
  100.     ExitAMenu("OK", 0);
  101.   case ERR_DIR:
  102.     Puts("Bad Global Directory - Can't Lock\n");
  103.     break;
  104.   case ERR_LIB:
  105.     Puts("Handler unable to open Libraries\n");
  106.     break;
  107.   default:
  108.     Printf("Handler returned error (%ld)\n", (LONG)M->ErrorCode);
  109.   }
  110.   RemoveHandler();
  111. }
  112.  
  113.  
  114. void
  115. UpdateHandler()
  116. /* The Handler already exists so just replace the menus */
  117. {
  118.   Printf("Updating %s -- ", VERSION);
  119.  
  120.   DB( Puts("\nSignaling Handler for Update - Waiting"); )
  121.   M->Parent = (struct Task *)Process;  /* from ArpC startup code */
  122.   M->ParentSig = AllocSignal(-1L);
  123.   Signal(M->Handler, SIGBREAKF_CTRL_D);
  124.   Wait(1 << M->ParentSig);
  125.   FreeSignal(M->ParentSig);
  126.   FreeMenus();
  127.  
  128.   DB( Puts("Handler Waiting -- Updating Menus"); )
  129.   if( ParseMenus() )
  130.     RemoveHandler();
  131.  
  132.   DB( Puts("Signaling Handler to Continue"); )
  133.   Signal(M->Handler, SIGBREAKF_CTRL_D);
  134.  
  135.   HandlerReport();
  136. }
  137.  
  138.  
  139. void
  140. LoadHandler()
  141. /* Load, Read config and Start Handler */
  142. {
  143.   struct Screen *Scr;
  144.   ULONG         ILock;
  145.  
  146.   Printf("Installing %s -- ", VERSION);
  147.  
  148.   DB( Puts("\nCreating Port"); )      /* do NOT use ArpAlloc() */
  149.   M = (struct MPort *)AllocMem( sizeof(struct MPort), MEMF_PUBLIC|MEMF_CLEAR);
  150.  
  151.   DB( Puts("Locating WB Window"); )
  152.   Scr = (struct Screen *)OpenWorkBench();  /* this returns workbench */
  153.   if( !Scr ) {
  154.     FreeMem( M, sizeof(struct MPort) );
  155.     ExitAMenu("Unable to locate Workbench screen!",24);
  156.   }
  157.   ILock = LockIBase(NULL);
  158.   for( M->WBWindow=Scr->FirstWindow; M->WBWindow;
  159.              M->WBWindow = M->WBWindow->NextWindow )
  160.     if( M->WBWindow->Flags & WBENCHWINDOW ) break;
  161.   UnlockIBase(ILock);
  162.   if( !M->WBWindow ) {
  163.     FreeMem( M, sizeof(struct MPort) );
  164.     ExitAMenu("Unable to locate Workbench Window!",25);
  165.   }
  166.  
  167.   if( ParseMenus() )
  168.     RemoveHandler();
  169.  
  170.   DB( Puts("Adding Port"); )
  171.   M->Port.mp_Flags = PA_IGNORE;
  172.   M->Port.mp_Node.ln_Pri = 0;
  173.   M->Port.mp_Node.ln_Type = NT_MSGPORT;
  174.   M->Port.mp_Node.ln_Name = AllocStr(AMENU_PORT);
  175.   AddPort(&M->Port);
  176.  
  177.   DB( Puts("Loading Handler"); )
  178.   M->Segment = LoadPrg(AMENU_HANDLER);
  179.   if (!M->Segment)
  180.     M->Segment = LoadPrg(AMENU_HANDLER2);
  181.   if (!M->Segment) {
  182.     Printf("Can't find %s", AMENU_HANDLER);
  183.     RemoveHandler();
  184.   }
  185.  
  186.   DB( Puts("Starting Handler"); )
  187.   if( !CreateProc(AMENU_HANDLER2, 0, M->Segment, DEF_STACK) ) {
  188.     Puts("Can't Create Process!");
  189.     RemoveHandler();
  190.   }
  191.  
  192.     /* handshake so that we know it got started ok */
  193.   DB( Puts("Waiting for Handler Signal"); )
  194.   M->Parent = (struct Task *)Process; /* from ArpC startup code */
  195.   M->ParentSig = AllocSignal(-1L);
  196.   Wait(1 << M->ParentSig);
  197.   FreeSignal(M->ParentSig);
  198.  
  199.   DB( Puts("Handler Signal recieved"); )
  200.   HandlerReport();
  201. }
  202.  
  203.  
  204. /*-----------------------------------------------------------------*/
  205.  
  206.  
  207.  
  208. /* These globals are for ArpC the startup code used */
  209. char *CLI_Args[2];    /* the argc array -- in line with template below */
  210. char *CLI_Template = "Q=QUIT/s,V=VERSION/s";
  211. char *CLI_Help     = "Amenu [QUIT] -- AMenu by Anthony Thyssen";
  212.  
  213. void
  214. main(argc,argv)   /* called from ArpC */
  215.   int   argc;
  216.   char *argv[];
  217. {
  218.   /* the Arp library (thus Graphics and Intuition libraries) are
  219.   ** opened in the ArpC startup code. Any CLI arguments given in
  220.   ** the template is also parsed and placed in correct position.
  221.   */
  222.  
  223.    /* Locate Named Port -- see if handler is running */
  224.   M = (struct MPort *)FindPort(AMENU_PORT);
  225.  
  226.   if( CLI_Args[0] )                /* Was QUIT was given? */
  227.     RemoveHandler();               /* no return */
  228.  
  229.   if( CLI_Args[1] )                /* Was VERSION asked for? */
  230.     ExitAMenu(COPYRIGHT,0);        /* no return */
  231.  
  232.   if( M )
  233.     UpdateHandler();               /* no return */
  234.  
  235.   LoadHandler();                   /* no return */
  236.  
  237.   /* should never be reached */
  238. }
  239.  
  240.