home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 408.lha / MyMenu_v1.1 / sources / MyMenu-Handler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-04  |  5.8 KB  |  200 lines

  1. /* Copyright (c) Darin Johnson, 1989 */
  2.  
  3. /* MyMenu-Handler.c           */
  4. /* Modified by John Baker, 1990       */
  5.  
  6. /* Permission is granted to use    */
  7. /* this program and to freely copy */
  8. /* it and/or source code as long   */
  9. /* as these notices remain.        */
  10. /* No charges for these copies may */
  11. /* be made, except for handling    */
  12. /* and distribution fees.          */
  13.  
  14. /* This is the main process for MyMenu.  This gets loaded and run in the */
  15. /* background by MyMenu.  When started, it finishes initializing itself, */
  16. /* and starts monitoring the WorkBench IDCMP port.  If any messages show */
  17. /* up that we are interested in, we run them as a CLI or WorkBench       */
  18. /* process.  We don't do any memory allocation or cleanup and leave that */
  19. /* all to MyMenu (in order to save some space).  Communication with      */
  20. /* MyMenu is done via a public port - never used as a port, but it holds */
  21. /* "global" variables.                                                   */
  22.  
  23. #include <exec/types.h>
  24. #include <intuition/intuition.h>
  25. #include <libraries/dos.h>
  26. #include <libraries/dosextens.h>
  27. #include <workbench/icon.h>
  28. #include <functions.h>
  29. #include "mymenu.h"
  30.  
  31. static char *copyright = "Copyright (c) Darin Johnson, 1989";
  32.  
  33. struct IntuitionBase *IntuitionBase;
  34.  
  35. #ifdef DO_WB
  36. struct IconBase *IconBase;    /* already defined in Manx C */
  37. #endif
  38.  
  39. struct MMData *MM;        /* data shared with other tasks */
  40. struct Process *MyProcess;
  41. #ifdef DO_WB
  42. int wb_cnt;            /* number of wb processes run */
  43. struct MsgPort *wb_reply_port;    /* replies from terminating WB processes */
  44. #endif
  45.  
  46. #ifdef DO_WB
  47.   /* error messages */
  48. struct IntuiText wb_open_msg_5 = { 3,1,JAM2,24,36,NULL,
  49.     (UBYTE*)"Do you REALLY want to quit now?", NULL };
  50. struct IntuiText wb_open_msg_4 = { 0,1,JAM2,7,28,NULL,
  51.     (UBYTE*)"the program finishes after MyMenu.", &wb_open_msg_5 };
  52. struct IntuiText wb_open_msg_3 = { 0,1,JAM2,7,20,NULL,
  53.     (UBYTE*)"still be open.  This can cause a crash if", &wb_open_msg_4 };
  54. struct IntuiText wb_open_msg_2 = { 0,1,JAM2,7,12,NULL,
  55.     (UBYTE*)"A WorkBench program started by MyMenu may", &wb_open_msg_3 };
  56. struct IntuiText wb_open_msg = { 3,1,JAM2,142,4,NULL,
  57.     (UBYTE*)"WARNING!!", &wb_open_msg_2 };
  58. struct IntuiText true_msg = { 0,1,JAM2,7,3,NULL,
  59.     (UBYTE*)"Yes, quit MyMenu", NULL };
  60. struct IntuiText false_msg = { 0,1,JAM2,7,3,NULL,
  61.     (UBYTE*)"No! I didn't mean it", NULL };
  62. #endif
  63.  
  64. /* returns an error to MyMenu if we get one during initialization */
  65. error(code)
  66. char code;
  67. {
  68.   MM->error_code = code;
  69.   if (IntuitionBase)
  70.     CloseLibrary(IntuitionBase);
  71. #ifdef DO_WB
  72.   if (IconBase)
  73.     CloseLibrary(IconBase);
  74. #endif
  75.     /* coordinate with add_handler() in MyMenu */
  76.   Signal(MM->parent_task, 1 << MM->parent_sig);
  77.     /* now coordinate with remove_handler() in MyMenu */
  78.   Wait(SIGBREAKF_CTRL_C);    /* wait for parent */
  79.   Signal(MM->parent_task, 1 << MM->parent_sig);  
  80.   _exit(0);
  81. }
  82.  
  83. /* add personalized menus to menustrip */
  84. add_menu_strip() {
  85.   struct Menu *menu_strip;
  86.   ULONG ilock;
  87.   menu_strip = MM->WBWindow->MenuStrip;
  88.   ilock = LockIBase(0L);
  89.   ClearMenuStrip(MM->WBWindow);
  90.   MM->prev_menu->NextMenu = MM->my_menu;
  91.   SetMenuStrip(MM->WBWindow, menu_strip);
  92.   UnlockIBase(ilock);
  93. }
  94.  
  95. /* remove our personalized menus from menustrip */
  96. del_menu_strip() {
  97.   struct Menu *menu_strip;
  98.   ULONG ilock;
  99.   menu_strip = MM->WBWindow->MenuStrip;
  100.   ilock = LockIBase(0L);
  101.   ClearMenuStrip(MM->WBWindow);
  102.   MM->prev_menu->NextMenu = NULL;
  103.   SetMenuStrip(MM->WBWindow, menu_strip);
  104.   UnlockIBase(ilock);
  105. }
  106.  
  107. /* main program - initialize and start up monitor */
  108. _main() {
  109.   BPTR syslock, oldlock;
  110.     /* find data set up by parent */
  111.   MM = (struct MMData *)FindPort(MYMENU_NAME);
  112.   if (!MM)
  113.     _exit(0);    /* some one ran us directly, bail out */
  114.  
  115.     /* finish initialization */
  116.   MM->handler_task = FindTask(NULL);
  117.   MyProcess = (struct Process *)MM->handler_task;
  118.   MyProcess->pr_ConsoleTask = NULL;
  119.   MyProcess->pr_CLI = NULL;
  120.   MyProcess->pr_WindowPtr = (APTR)-1;    /* disable requesters for FindIt() */
  121.   MyProcess->pr_CurrentDir = NULL;
  122.  
  123.   syslock = (BPTR)Lock("SYS:",ACCESS_READ);
  124.   if (syslock)
  125.     oldlock = CurrentDir((struct FileLock *)syslock);
  126.  
  127.     /* open necessary libraries */  
  128.   IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library", 0);
  129.   if (IntuitionBase == NULL)
  130.     error(ERR_LIB);
  131. #ifdef DO_WB
  132.   IconBase = (struct IconBase *)OpenLibrary(ICONNAME, 0);
  133.   if (IconBase == NULL)
  134.     error(ERR_LIB);
  135. #endif
  136.  
  137.   if (!MM->WBWindow)
  138.     error(ERR_WIN);
  139.   if (!setup_mon())
  140.     error(ERR_MON);
  141.  
  142.   MM->error_code = ERR_OK;
  143.  
  144.     /* signal parent that we're fully weaned */
  145.   Signal(MM->parent_task, 1 << MM->parent_sig);
  146.  
  147.   add_menu_strip();
  148.  
  149. #ifdef DO_WB
  150.   wb_cnt = 0;
  151.     /* create port AFTER setup_mon() so we don't get same signal */
  152.   wb_reply_port = CreatePort(WBPORT_NAME, 0);
  153.   NewList(&wb_reply_port->mp_MsgList);    /* is this necessary? */
  154. #endif
  155.  
  156. restart:
  157.     monitor();
  158. #ifdef DO_WB
  159.       /* see if we can leave gracefully */
  160.       /* we don't want to terminate and then have a WB reply message */
  161.     if ((wb_cnt > 0) && (AutoRequest(NULL,&wb_open_msg,
  162.                 &true_msg,&false_msg,0,0,370,80)==FALSE))
  163.     {
  164.       MM->error_code = ERR_WB_OPEN;
  165.       Signal(MM->parent_task, 1 << MM->parent_sig);  
  166.       goto restart;
  167.     }
  168. #endif
  169.  
  170.     /* clean everything up */
  171.   MM->error_code = ERR_OK;    /* let parent know that we finished up */
  172.   finish_mon();
  173.  
  174.   del_menu_strip();
  175.   CloseLibrary(IntuitionBase);
  176. #ifdef DO_WB
  177.   CloseLibrary(IconBase);
  178. #endif
  179.  
  180.     /* Forbid until we terminate, so that we don't get UnLoadSeg'ed to soon */
  181.   Forbid();
  182.  
  183. #ifdef DO_WB
  184.   {
  185.     register struct Message *msg;
  186.     while ((msg=GetMsg(wb_reply_port))!=0)
  187.       wbfree(msg);
  188.     DeletePort(wb_reply_port);
  189.   }
  190. #endif
  191.  
  192.   if (syslock) {
  193.     CurrentDir((struct FileLock *)oldlock);
  194.     UnLock(syslock);
  195.   }
  196.  
  197.     /* signal parent that we are done */
  198.   Signal(MM->parent_task, 1 << MM->parent_sig);  
  199. }
  200.