home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d527 / toolmanager.lha / ToolManager / Source / mainloop.c < prev    next >
C/C++ Source or Header  |  1991-08-05  |  7KB  |  208 lines

  1. /*
  2.  * mainloop.c   V1.4
  3.  *
  4.  * main event loop
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* structures for quit requester */
  12. static struct EasyStruct QuitES={sizeof(struct EasyStruct),0,MyName,
  13.                                  "Really quit?","Ok|Cancel"};
  14.  
  15. /* Set global quit flag, if no WB tool is currently active */
  16. /* Display a safety requester if the user wants it */
  17. void SetQuitFlag(void)
  18. {
  19.  if (running=(wbactive>0))       /* Is a WB tool currently active? */
  20.   DisplayBeep(NULL);             /* Yes, we can't quit! */
  21.  else if (ShowQuitReq)           /* Shall we show a quit requester? */
  22.   running=!EasyRequest(NULL,&QuitES,NULL,NULL);
  23. }
  24.  
  25. /* The main processing loop */
  26. void mainloop(void)
  27. {
  28.  ULONG sigs,bsigs,psigs,csigs;   /* Buffers for signals */
  29.  
  30.  /* Init icon position */
  31.  MyIcon->do_CurrentX=IconXPos;
  32.  MyIcon->do_CurrentY=IconYPos;
  33.  
  34.  if (ShowIcon)                   /* Display our icon? */
  35.   {
  36.    /* If we have an icon, we don't need the "Open TM Window" menu entry */
  37.    RemoveAppMenuItem(OTWAppMenuItem);
  38.  
  39.    /* Notify Workbench about icon */
  40.    if (!(MyAppIcon=AddAppIcon(NULL,NULL,MyName,MyMP,NULL,MyIcon,TAG_DONE)))
  41.     cleanup(9);
  42.   }
  43.  
  44.  /* Add TM pop up window hot key */
  45.  AttachCxObj(MyBroker,HotKey(PopUpHotKey,MyBrokerPort,NULL));
  46.  
  47.  /* Commodities error? */
  48.  if (CxObjError(MyBroker))
  49.   cleanup(10);
  50.  
  51.  /* All things set up --> activate our broker */
  52.  ActivateCxObj(MyBroker,1);
  53.  
  54.  bsigs=SIGBREAKF_CTRL_C|            /* break signals */
  55.        SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F;
  56.  psigs=1L<<MyMP->mp_SigBit;         /* port signal */
  57.  csigs=1L<<MyBrokerPort->mp_SigBit; /* Commodities Broker port signal */
  58.  sigs=bsigs|psigs|csigs;            /* merge signals */
  59.  
  60.  /* Show status window on startup? */
  61.  if (ShowStatusWindow) OpenStatusWindow();
  62.  
  63.  while (running)                    /* main event loop */
  64.   {
  65.    ULONG gotsigs;                   /* received signals */
  66.  
  67.    /* Wait for specified signals */
  68.    gotsigs=Wait(sigs|statwinsig|editwinsig);
  69.  
  70.    if ((gotsigs&bsigs))             /* Got break signals and is no WB tool */
  71.     SetQuitFlag();                  /* running? Yes, quit program */
  72.  
  73.    if (gotsigs&psigs)               /* Message arrived at message port? */
  74.     {
  75.      struct AppMessage *msg;        /* pointer to received message */
  76.  
  77.      while (msg=GetMsg(MyMP))       /* Yes, empty message queue */
  78.       if (msg->am_Message.mn_Node.ln_Type==NT_REPLYMSG) /* Replied message? */
  79.        {
  80.         /* This is the death message from a tool we started some time ago */
  81.         struct WBStartup *wbs=(struct WBStartup *) msg;
  82.         struct WBArg *wa=wbs->sm_ArgList;
  83.         int i=wbs->sm_NumArgs;
  84.  
  85.         while (i--)
  86.          {
  87.           UnLock(wa->wa_Lock);      /* Free WB argument */
  88.           if (wa->wa_Name) free(wa->wa_Name);
  89.           wa++;
  90.          }
  91.  
  92.         if (wbs->sm_ToolWindow)     /* Free tool window specification */
  93.          free(wbs->sm_ToolWindow);
  94.  
  95.         UnLoadSeg(wbs->sm_Segment); /* Unload code */
  96.         free(wbs);                  /* Free WBStartup */
  97.         wbactive--;                 /* One tool closed down */
  98.        }
  99.       else
  100.        {
  101.         switch(msg->am_Type)       /* Switch between message types */
  102.          {
  103.           case MTYPE_APPWINDOW:    /* Window action */
  104.                                    /* Add Workbench parameters to tool list */
  105.            if (!WBAddToolNode(msg->am_ArgList,msg->am_NumArgs))
  106.             DisplayBeep(NULL);
  107.            if (statwinsig)         /* Refresh status window if open */
  108.             RefreshStatusWindow();
  109.            break;
  110.  
  111.           case MTYPE_APPICON:      /* Icon action */
  112.            if (msg->am_ID)         /* Tool icon selected? */
  113.             /* Start Tool, ID == Address of ToolNode */
  114.             StartTool((struct ToolNode *) msg->am_ID,msg);
  115.            else                    /* User selected program icon */
  116.             if (msg->am_NumArgs==0) /* Did the user double click my icon? */
  117.              OpenStatusWindow();   /* Yes! Open status window */
  118.             else                   /* User dropped an icon on my icon */
  119.              {                     /* Add Workbench parameters to tool list */
  120.               if (!WBAddToolNode(msg->am_ArgList,msg->am_NumArgs))
  121.                DisplayBeep(NULL);
  122.               if (statwinsig)      /* Refresh status window if open */
  123.                RefreshStatusWindow();
  124.              }
  125.            break;
  126.  
  127.           case MTYPE_APPMENUITEM:  /* Menu action */
  128.            switch(msg->am_ID)
  129.             {
  130.              case 0:               /* "Quit ToolManager" menu item */
  131.               SetQuitFlag();
  132.               break;
  133.  
  134.              case 1:               /* "Open TM Window" menu item */
  135.               OpenStatusWindow();
  136.               break;
  137.  
  138.              default:              /* Default: Tool selected */
  139.               /* Start Tool, ID == Address of ToolNode */
  140.               StartTool((struct ToolNode *) msg->am_ID,msg);
  141.               break;
  142.             }
  143.            break;
  144.          } /* end of switch(msg->am_Type) */
  145.  
  146.         ReplyMsg((struct Message *) msg);    /* Reply message to sender */
  147.        } /* end of if (msg->......) */
  148.     } /* end of if (gotsigs&psigs) */
  149.  
  150.    if (gotsigs&csigs)                   /* Got broker port signal? */
  151.     {                                   /* Handle commodities event */
  152.      struct CxMsg *msg;
  153.  
  154.      while (msg=GetMsg(MyBrokerPort))   /* Retrieve Messages from port */
  155.       {
  156.        switch(CxMsgType(msg))
  157.         {
  158.          case CXM_IEVENT:               /* Received a hot key event */
  159.           struct ToolNode *tn;
  160.  
  161.           /* ID contains pointer to corresponding ToolNode */
  162.           if (tn=(struct ToolNode *) CxMsgID(msg))
  163.            StartTool(tn,NULL);
  164.           else OpenStatusWindow();      /* Pop up window hot key */
  165.           break;
  166.  
  167.          case CXM_COMMAND:              /* Received a commodities command */
  168.           switch(CxMsgID(msg))
  169.            {
  170.             case CXCMD_DISABLE:         /* Disable broker */
  171.              ActivateCxObj(MyBroker,0);
  172.              break;
  173.             case CXCMD_ENABLE:          /* Enable broker */
  174.              ActivateCxObj(MyBroker,1);
  175.              break;
  176.             case CXCMD_APPEAR:          /* Open status window */
  177.              OpenStatusWindow();
  178.              break;
  179.             case CXCMD_DISAPPEAR:       /* Close status window */
  180.              CloseStatusWindow();
  181.              break;
  182.             case CXCMD_KILL:            /* Quit application */
  183.              SetQuitFlag();
  184.              break;
  185.            }
  186.           break;
  187.         }
  188.  
  189.        ReplyMsg(msg);                   /* Reply message to sender */
  190.       } /* end of while (msg=....) */
  191.     } /* if (gotsigs&csigs) */
  192.  
  193.    if (gotsigs&statwinsig) /* Got status window signal? */
  194.     HandleStatWinEvent();  /* Handle status window event */
  195.  
  196.    if (gotsigs&editwinsig) /* Got edit window signal? */
  197.     HandleEditWinEvent();  /* Handle edit window event */
  198.   } /* end of main loop */
  199.  
  200.  /* If window open, close it */
  201.  CloseStatusWindow();
  202.  CloseEditWindow();
  203.  
  204.  /* Exit program */
  205.  cleanup(99);
  206. }
  207.  
  208.