home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 492.lha / ToolManager_v1.3 / src / mainloop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-06  |  5.1 KB  |  148 lines

  1. /*
  2.  * mainloop.c   V1.3
  3.  *
  4.  * main event loop
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* The main processing loop */
  12. void mainloop(void)
  13. {
  14.  BOOL end=TRUE;                  /* Flag for main loop */
  15.  BOOL windowopen=FALSE;          /* Flag for status window */
  16.  ULONG sigs,bsigs,psigs,wsigs;   /* Buffers for signals */
  17.  
  18.  /* Init icon position */
  19.  MyIcon->do_CurrentX=IconXPos;
  20.  MyIcon->do_CurrentY=IconYPos;
  21.  
  22.  /* Notify Workbench about icon, if ShowIcon is true */
  23.  if (ShowIcon)
  24.   if (!(MyAppIcon=AddAppIcon(NULL,NULL,MyName,MyMP,NULL,MyIcon,TAG_DONE)))
  25.    cleanup(8);
  26.  
  27.  bsigs=SIGBREAKF_CTRL_C|         /* break signals */
  28.        SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F;
  29.  psigs=1L<<MyMP->mp_SigBit;      /* port signal */
  30.  sigs=bsigs|psigs;               /* merge signals */
  31.  
  32.  while (end)                     /* main loop */
  33.   {
  34.    struct AppMessage *msg;       /* pointer to received message */
  35.    ULONG gotsigs;                /* received signals */
  36.  
  37.    gotsigs=Wait(sigs);           /* Wait for specified signals */
  38.  
  39.    if ((gotsigs&bsigs) && !wbactive) /* Got break signals and is no WB tool */
  40.     end=FALSE;                       /* running? Yes, quit program */
  41.  
  42.    if (gotsigs&psigs)            /* Message arrived at message port? */
  43.     while (msg=GetMsg(MyMP))     /* Yes, empty message queue */
  44.      if (msg->am_Message.mn_Node.ln_Type==NT_REPLYMSG) /* Replied message? */
  45.       {
  46.        /* This is the death message from a tool we started some time ago */
  47.        struct WBStartup *wbs=(struct WBStartup *) msg;
  48.        struct WBArg *wa=wbs->sm_ArgList;
  49.        int i=wbs->sm_NumArgs;
  50.  
  51.        while (i--)
  52.         {
  53.          UnLock(wa->wa_Lock);    /* Free WB argument */
  54.          if (wa->wa_Name) free(wa->wa_Name);
  55.          wa++;
  56.         }
  57.  
  58.        if (wbs->sm_ToolWindow)   /* Free tool window specification */
  59.         free(wbs->sm_ToolWindow);
  60.  
  61.        UnLoadSeg(wbs->sm_Segment); /* Unload code */
  62.        free(wbs);                  /* Free WBStartup */
  63.        wbactive--;                 /* One tool closed down */
  64.       }
  65.      else
  66.       {
  67.        switch(msg->am_Type)       /* Switch between message types */
  68.         {
  69.          case MTYPE_APPWINDOW:    /* Window action */
  70.          case MTYPE_APPICON:      /* Icon action */
  71.           if (msg->am_NumArgs==0) /* Did the user double click my icon? */
  72.            {                      /* Yes! If window not open, open it */
  73.             if ((!windowopen) && (wsigs=OpenStatusWindow()))
  74.              {
  75.               sigs|=wsigs;        /* Merge with other signals */
  76.               windowopen=TRUE;    /* Window is open */
  77.              }
  78.            }
  79.           else                    /* User dropped an icon on my icon */
  80.            {                      /* Add Workbench parameters to tool list */
  81.             if (!WBAddToolNode(msg->am_ArgList,msg->am_NumArgs))
  82.              DisplayBeep(NULL);
  83.             if (windowopen)       /* Refresh status window if open */
  84.              RefreshStatusWindow();
  85.            }
  86.           break;
  87.  
  88.          case MTYPE_APPMENUITEM:  /* Menu action */
  89.           switch(msg->am_ID)
  90.            {
  91.             case 0:               /* "Quit ToolManager" menu item */
  92.              if (wbactive)        /* Yes. Do we have a tool running? */
  93.               DisplayBeep(NULL);  /*      Yes, we can't quit! */
  94.              else end=FALSE;      /*      No, quit program */
  95.              break;
  96.  
  97.             case 1:               /* "Open TM Window" menu item */
  98.              if ((!windowopen) && (wsigs=OpenStatusWindow()))
  99.               {
  100.                sigs|=wsigs;       /* Merge with other signals */
  101.                windowopen=TRUE;   /* Window is open */
  102.               }
  103.              break;
  104.  
  105.             default:              /* Default: Tool selected */
  106.              struct ToolNode *tn;
  107.              char *cp;
  108.              BOOL rc;
  109.  
  110.              /* Scan list for selected tool */
  111.              for (tn=GetHead(&ToolList); tn; tn=GetSucc(tn))
  112.               if (tn->tn_ID==msg->am_ID)
  113.                {                            /* Corresponding tool found */
  114.                 if (!(cp=tn->tn_RealName))  /* Get tool name */
  115.                  cp=tn->tn_Node.ln_Name;
  116.  
  117.                 /* Tool type? */
  118.                 if (tn->tn_CLI) rc=StartCLITool(msg,tn,cp); /* CLI */
  119.                 else rc=StartWBTool(msg,tn,cp);             /* WB  */
  120.  
  121.                 if (!rc) DisplayBeep(NULL); /* An error occured */
  122.                 break;                      /* leave 'for' loop */
  123.                }
  124.              break;
  125.            }
  126.           break;
  127.         } /* end of switch(msg->am_Type) */
  128.  
  129.        ReplyMsg((struct Message *) msg); /* Reply message to sender */
  130.       } /* end of if (msg->......) */
  131.  
  132.    if (windowopen && (gotsigs&wsigs)) /* If window open, got window signal? */
  133.     if (HandleWindowEvent())          /* Handle window event */
  134.      {                                /* Window close event? */
  135.       CloseStatusWindow();            /* Yes, close window */
  136.       sigs&=~wsigs;                   /* Remove window signals */
  137.       windowopen=FALSE;               /* Window closed */
  138.      }
  139.   } /* end of main loop */
  140.  
  141.  /* If window open, close it */
  142.  if (windowopen) CloseStatusWindow();
  143.  
  144.  /* Exit program */
  145.  cleanup(99);
  146. }
  147.  
  148.