home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / amiga / utility / misc / toolmng.lzh / ToolManager / Source / mainloop.c < prev    next >
C/C++ Source or Header  |  1991-10-10  |  9KB  |  262 lines

  1. /*
  2.  * mainloop.c   V1.5
  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
  22.   {
  23.    struct Screen *pubsc=LockPubScreen(WBScreenName);
  24.    struct Window *w=pubsc?pubsc->FirstWindow:NULL;
  25.  
  26.    /* Shall we show a quit requester? */
  27.    if (ShowQuitReq)
  28.     running=!EasyRequest(w,&QuitES,NULL,"");
  29.  
  30.    /* Check if config has changed */
  31.    if (!running) running=ShowSaveConfigRequester(w);
  32.  
  33.    if (pubsc) UnlockPubScreen(NULL,pubsc);
  34.   }
  35. }
  36.  
  37. /* The main processing loop */
  38. void mainloop(void)
  39. {
  40.  ULONG bsigs,psigs,csigs;   /* Buffers for signals */
  41.  
  42.  /* Init icon position */
  43.  MyIcon->do_CurrentX=IconXPos;
  44.  MyIcon->do_CurrentY=IconYPos;
  45.  
  46.  if (ShowIcon)                   /* Display our icon? */
  47.   {
  48.    /* If we have an icon, we don't need the "Open TM Window" menu entry */
  49.    RemoveAppMenuItem(OTWAppMenuItem);
  50.    OTWAppMenuItem=NULL;
  51.  
  52.    /* Notify Workbench about icon */
  53.    if (!(MyAppIcon=AddAppIconA(NULL,NULL,IconName,MyMP,NULL,MyIcon,NULL)))
  54.     cleanup(9);
  55.   }
  56.  
  57.  /* Add TM pop up window hot key */
  58.  AttachCxObj(MyBroker,HotKey(PopUpHotKey,MyBrokerPort,NULL));
  59.  
  60.  /* Commodities error? */
  61.  if (CxObjError(MyBroker))
  62.   cleanup(10);
  63.  
  64.  /* All things set up --> activate our broker */
  65.  SetBrokerState(TRUE);
  66.  
  67.  bsigs=SIGBREAKF_CTRL_C|            /* break signals */
  68.        SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F;
  69.  psigs=1L<<MyMP->mp_SigBit;         /* port signal */
  70.  csigs=1L<<MyBrokerPort->mp_SigBit; /* Commodities Broker port signal */
  71.  globalsigs=bsigs|psigs|csigs;      /* merge signals */
  72.  
  73.  /* Open dock window */
  74.  OpenDockWindow();
  75.  ShowDock=TRUE;
  76.  
  77.  /* Show status window on startup? */
  78.  if (ShowStatusWindow) OpenStatusWindow(FALSE);
  79.  
  80.  while (running)                    /* main event loop */
  81.   {
  82.    ULONG gotsigs;                   /* received signals */
  83.  
  84.    /* Wait for specified signals */
  85.    gotsigs=Wait(globalsigs);
  86.  
  87.    if ((gotsigs&bsigs))             /* Got break signals and is no WB tool */
  88.     SetQuitFlag();                  /* running? Yes, quit program */
  89.  
  90.    if (gotsigs&psigs)               /* Message arrived at message port? */
  91.     {
  92.      struct AppMessage *msg;        /* pointer to received message */
  93.  
  94.      while (msg=GetMsg(MyMP))       /* Yes, empty message queue */
  95.       if (msg->am_Message.mn_Node.ln_Type==NT_REPLYMSG) /* Replied message? */
  96.        {
  97.         /* This is the death message from a tool we started some time ago */
  98.         struct WBStartup *wbs=(struct WBStartup *) msg;
  99.         struct WBArg *wa=wbs->sm_ArgList;
  100.         int i=wbs->sm_NumArgs;
  101.  
  102.         while (i--)
  103.          {
  104.           UnLock(wa->wa_Lock);      /* Free WB argument */
  105.           if (wa->wa_Name) free(wa->wa_Name);
  106.           wa++;
  107.          }
  108.  
  109.         if (wbs->sm_ToolWindow)     /* Free tool window specification */
  110.          free(wbs->sm_ToolWindow);
  111.  
  112.         UnLoadSeg(wbs->sm_Segment); /* Unload code */
  113.         free(wbs);                  /* Free WBStartup */
  114.         wbactive--;                 /* One tool closed down */
  115.        }
  116.       else
  117.        {
  118.         switch(msg->am_Type)       /* Switch between message types */
  119.          {
  120.           case MTYPE_APPWINDOW:    /* Window action */
  121.            switch(msg->am_ID)      /* Which AppWindow? */
  122.             {
  123.              case STATWINAPPID:    /* Status window */
  124.                                    /* Add Workbench parameters to tool list */
  125.               if (!WBAddToolNode(msg->am_ArgList,msg->am_NumArgs))
  126.                DisplayBeep(NULL);
  127.               RefreshStatusWindow(); /* Refresh status window if open */
  128.               break;
  129.  
  130.              case DOCKWINAPPID:    /* Dock window */
  131.               struct ToolNode *tn;
  132.               WORD x=msg->am_MouseX,y=msg->am_MouseY;
  133.  
  134.               /* Find tool and start it with parameters */
  135.               if (tn=FindDock(x,y))
  136.                {
  137.                 SelectDock(tn,x,y,TRUE);
  138.                 StartTool(tn,msg);
  139.                 SelectDock(tn,x,y,FALSE);
  140.                }
  141.               break;
  142.             }
  143.            break;
  144.  
  145.           case MTYPE_APPICON:      /* Icon action */
  146.            if (msg->am_ID)         /* Tool icon selected? */
  147.             /* Start Tool, ID == Address of ToolNode */
  148.             StartTool((struct ToolNode *) msg->am_ID,msg);
  149.            else                    /* User selected program icon */
  150.             if (msg->am_NumArgs==0) /* Did the user double click my icon? */
  151.              OpenStatusWindow(FALSE); /* Yes! Open status window */
  152.             else                   /* User dropped an icon on my icon */
  153.              {                     /* Add Workbench parameters to tool list */
  154.               if (!WBAddToolNode(msg->am_ArgList,msg->am_NumArgs))
  155.                DisplayBeep(NULL);
  156.               RefreshStatusWindow(); /* Refresh status window if open */
  157.              }
  158.            break;
  159.  
  160.           case MTYPE_APPMENUITEM:  /* Menu action */
  161.            switch(msg->am_ID)
  162.             {
  163.              case 0:               /* "Quit ToolManager" menu item */
  164.               SetQuitFlag();
  165.               break;
  166.  
  167.              case 1:               /* "Open TM Window" menu item */
  168.               OpenStatusWindow(FALSE);
  169.               break;
  170.  
  171.              default:              /* Default: Tool selected */
  172.               /* Start Tool, ID == Address of ToolNode */
  173.               StartTool((struct ToolNode *) msg->am_ID,msg);
  174.               break;
  175.             }
  176.            break;
  177.          } /* end of switch(msg->am_Type) */
  178.  
  179.         ReplyMsg((struct Message *) msg);    /* Reply message to sender */
  180.        } /* end of if (msg->......) */
  181.     } /* end of if (gotsigs&psigs) */
  182.  
  183.    if (gotsigs&csigs)                   /* Got broker port signal? */
  184.     {                                   /* Handle commodities event */
  185.      struct CxMsg *msg;
  186.  
  187.      while (msg=GetMsg(MyBrokerPort))   /* Retrieve Messages from port */
  188.       {
  189.        switch(CxMsgType(msg))
  190.         {
  191.          case CXM_IEVENT:               /* Received a hot key event */
  192.           struct ToolNode *tn;
  193.  
  194.           /* ID contains pointer to corresponding ToolNode */
  195.           if (tn=(struct ToolNode *) CxMsgID(msg))
  196.            {
  197.             if (tn->tn_Flags&TNFLAGS_WBTF) /* Workbench Screen to front ? */
  198.              {
  199.               struct Screen *pubsc=LockPubScreen(WBScreenName);
  200.  
  201.               if (pubsc)
  202.                {
  203.                 ScreenToFront(pubsc);
  204.                 UnlockPubScreen(NULL,pubsc);
  205.                }
  206.              }
  207.  
  208.             StartTool(tn,NULL);
  209.            }
  210.           else OpenStatusWindow(TRUE);  /* Pop up window hot key */
  211.           break;
  212.  
  213.          case CXM_COMMAND:              /* Received a commodities command */
  214.           switch(CxMsgID(msg))
  215.            {
  216.             case CXCMD_DISABLE:         /* Disable broker */
  217.              SetBrokerState(FALSE);
  218.              break;
  219.             case CXCMD_ENABLE:          /* Enable broker */
  220.              SetBrokerState(TRUE);
  221.              break;
  222.             case CXCMD_APPEAR:          /* Open status window */
  223.              OpenStatusWindow(TRUE);
  224.              break;
  225.             case CXCMD_DISAPPEAR:       /* Close status window */
  226.              CloseStatusWindow();
  227.              break;
  228.             case CXCMD_KILL:            /* Quit application */
  229.              SetQuitFlag();
  230.              break;
  231.            }
  232.           break;
  233.         }
  234.  
  235.        ReplyMsg(msg);                   /* Reply message to sender */
  236.       } /* end of while (msg=....) */
  237.     } /* if (gotsigs&csigs) */
  238.  
  239.    if (gotsigs&statwinsig) /* Got status window signal? */
  240.     HandleStatWinEvent();  /* Handle status window event */
  241.  
  242.    if (gotsigs&editwinsig) /* Got edit window signal? */
  243.     HandleEditWinEvent();  /* Handle edit window event */
  244.  
  245.    if (gotsigs&hkeywinsig)   /* Got HotKeys window signal? */
  246.     HandleHotKeysWinEvent(); /* Handle HotKeys window event */
  247.  
  248.    if (gotsigs&dockwinsig) /* Got dock window signal? */
  249.     HandleDockWinEvent();  /* Handle dock window event */
  250.   } /* end of main loop */
  251.  
  252.  /* If window open, close it */
  253.  CloseStatusWindow();
  254.  CloseEditWindow();
  255.  CloseHotKeysWindow();
  256.  CloseDockWindow();
  257.  
  258.  /* Exit program */
  259.  cleanup(99);
  260. }
  261.  
  262.