home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 458.lha / ToolManager_v1.1 / ToolManager.c < prev    next >
C/C++ Source or Header  |  1991-01-02  |  8KB  |  244 lines

  1. #include "ToolManager.h"
  2.  
  3. /* Library pointers */
  4. extern struct Library *SysBase,*IntuitionBase,*GfxBase; /* Autoinit Libs */
  5. struct Library *WorkbenchBase,*IconBase,*GadToolsBase;
  6.  
  7. /* Structures for icon */
  8. extern struct DiskObject MyIcon;
  9. static struct AppIcon *MyAppIcon;
  10.  
  11. /* Name of the message port */
  12. static char *MyMPName="ToolManager";
  13.  
  14. /* Tags for System() */
  15. extern struct TagItem MyTags[];
  16. #define SYSTAGS_IN  0
  17. #define SYSTAGS_OUT 1
  18.  
  19. /* miscellaneous */
  20. static char *MyName;
  21. static struct AppMenuItem *MyAppMenuItem;
  22.  
  23. /* Workbench main entry point */
  24. void wbmain(struct WBStartup *wbarg)
  25. {
  26.  BPTR fl;
  27.  
  28.  MyName=wbarg->sm_ArgList->wa_Name;         /* What is my name? */
  29.  fl=CurrentDir(wbarg->sm_ArgList->wa_Lock); /* Goto a valid directory */
  30.  startup();                                 /* common startup code */
  31.  CurrentDir(fl);                            /* Go back to old directory */
  32.                                             /* Process WB startup parameters */
  33.  WBAddToolNode(&wbarg->sm_ArgList[1],wbarg->sm_NumArgs-1);
  34.  mainloop();                                /* Go into main loop */
  35. }
  36.  
  37. /* CLI main entry point */
  38. void main(int argc, char **argv)
  39. {
  40.  int i;
  41.  BPTR fl;
  42.  
  43.  MyName=argv[0];      /* What is my name? */
  44.  startup();           /* common startup code */
  45.  fl=CurrentDir(NULL); /* Get current directory lock */
  46.                       /* Process CLI startup parameters */
  47.  for (i=1; i<argc; i++) AddToolNode(fl,argv[i],NULL);
  48.  CurrentDir(fl);      /* Go back to old directory */
  49.  mainloop();          /* Go into main loop */
  50. }
  51.  
  52. /* Buffer length for one config file line */
  53. #define BUFLEN 100
  54.  
  55. /* Handle common startup operations */
  56. void startup(void)
  57. {
  58.  BPTR fl;
  59.  FILE *fh;                /* Filehandle for config file */
  60.  char ConfigLine[BUFLEN]; /* Buffer for one config file line */
  61.  
  62.  if (SysBase->lib_Version<36)  /* Sanity check */
  63.   {
  64.    puts("You need a Kickstart 2.0 or better to run ToolManager!");
  65.    exit(20);
  66.   }
  67.  
  68.  Forbid();                     /* Is "ToolManager" already running? */
  69.  MyMP=FindPort(MyMPName);
  70.  Permit();
  71.  if (MyMP)
  72.   {                            /* Yes, exit gracefully */
  73.    puts("ToolManager already running!");
  74.    exit(5);
  75.   }
  76.  
  77.  /* Open libraries */
  78.  if (!(WorkbenchBase=OpenLibrary(WORKBENCH_NAME,0)))
  79.   cleanup(1);
  80.  
  81.  if (!(IconBase=OpenLibrary(ICONNAME,0)))
  82.   cleanup(2);
  83.  
  84.  if (!(GadToolsBase=OpenLibrary("gadtools.library",0)))
  85.   cleanup(3);
  86.  
  87.  /* Create message port */
  88.  if (!(MyMP=CreateMsgPort()))
  89.   cleanup(4);
  90.  MyMP->mp_Node.ln_Pri=0;
  91.  MyMP->mp_Node.ln_Name=MyMPName; /* Our name */
  92.  AddPort(MyMP);                  /* Announce our presence */
  93.  
  94.  /* Notify Workbench about icon */
  95.  if (!(MyAppIcon=AddAppIcon(NULL,NULL,MyName,MyMP,NULL,&MyIcon,NULL)))
  96.   cleanup(5);
  97.  
  98.  /* Notify Workbench about special menu item. If this item is selected,
  99.     the program will quit */
  100.  if (!(MyAppMenuItem=AddAppMenuItem(NULL,NULL,"Quit ToolManager",MyMP,NULL)))
  101.   cleanup(6);
  102.  
  103.  NewList(&ToolList);  /* Initialize tool list */
  104.  fl=CurrentDir(NULL); /* Get the current directory */
  105.  
  106.  if (fh=fopen("S:ToolManager.Config","r")) /* Scan config file */
  107.   {
  108.    while (!feof(fh)) /* if not end of file, read one line into buffer */
  109.     if (fgets(ConfigLine,BUFLEN,fh) && (strlen(ConfigLine)>1))
  110.      {
  111.       char *tp;
  112.  
  113.       ConfigLine[strlen(ConfigLine)-1]='\0'; /* Strip newline */
  114.       if (tp=strchr(ConfigLine,';'))         /* scan config line */
  115.        {
  116.         *tp='\0';                        /* Menu entry ; real program name */
  117.         AddToolNode(fl,ConfigLine,++tp);
  118.        }
  119.       else
  120.        AddToolNode(fl,ConfigLine,NULL);  /* Menu entry == real program name */
  121.      }
  122.    fclose(fh);  /* close config file */
  123.   }
  124.  CurrentDir(fl); /* go back to old directory */
  125. }
  126.  
  127. /* The main processing loop */
  128. void mainloop(void)
  129. {
  130.  BOOL end=TRUE;                  /* Flag for main loop */
  131.  BOOL windowopen=FALSE;          /* Flag for status window */
  132.  ULONG sigs,bsigs,psigs,wsigs;   /* Buffers for signals */
  133.  
  134.  bsigs=SIGBREAKF_CTRL_C|         /* break signals */
  135.        SIGBREAKF_CTRL_D|SIGBREAKF_CTRL_E|SIGBREAKF_CTRL_F;
  136.  psigs=1L<<MyMP->mp_SigBit;      /* port signal */
  137.  sigs=bsigs|psigs;               /* merge signals */
  138.  
  139.  while (end)                     /* main loop */
  140.   {
  141.    struct AppMessage *msg;       /* pointer to received message */
  142.    ULONG gotsigs;                /* received signals */
  143.  
  144.    gotsigs=Wait(sigs);           /* Wait for specified signals */
  145.  
  146.    if (gotsigs&bsigs)            /* Got break signals? */
  147.     end=FALSE;                   /* Yes, quit program */
  148.  
  149.    if (gotsigs&psigs)            /* Message arrived at message port? */
  150.     while (msg=GetMsg(MyMP))     /* Yes, empty message queue */
  151.      {
  152.       switch(msg->am_Type)       /* Switch between message types */
  153.        {
  154.         case MTYPE_APPWINDOW:    /* Window action */
  155.         case MTYPE_APPICON:      /* Icon action */
  156.          if (msg->am_NumArgs==0) /* Did the user double click my icon? */
  157.           {                      /* Yes! If window not open, open it */
  158.            if ((!windowopen) && (wsigs=OpenStatusWindow()))
  159.             {
  160.              sigs|=wsigs;        /* Merge with other signals */
  161.              windowopen=TRUE;    /* Window is open */
  162.             }
  163.           }
  164.          else                    /* User dropped an icon on my icon */
  165.           {                      /* Add Workbench parameters to tool list */
  166.            if (!WBAddToolNode(msg->am_ArgList,msg->am_NumArgs))
  167.             DisplayBeep(NULL);
  168.            if (windowopen)       /* Refresh status window if open */
  169.             RefreshStatusWindow();
  170.           }
  171.          break;
  172.  
  173.         case MTYPE_APPMENUITEM:  /* Menu action */
  174.          if (msg->am_ID==NULL)   /* Special menu item selected? */
  175.           end=FALSE;             /* Yes, quit program */
  176.          else                    /* No, scan list for selected tool */
  177.           {
  178.            struct ToolNode *tn;
  179.  
  180.            for (tn=GetHead(&ToolList); tn; tn=GetSucc(tn))
  181.             if (tn->tn_ID==msg->am_ID)
  182.              {                                /* Corresponding tool found */
  183.               BPTR fl,dfh;                    /* AmigaDOS Filehandles */
  184.               char *cp;
  185.  
  186.               fl=CurrentDir(tn->tn_DirLock);  /* Change to tool's directory */
  187.               dfh=Open("NIL:",MODE_NEWFILE);  /* Open dummy files for */
  188.               MyTags[SYSTAGS_IN].ti_Data=dfh; /* program I/O */
  189.               dfh=Open("NIL:",MODE_NEWFILE);
  190.               MyTags[SYSTAGS_OUT].ti_Data=dfh;
  191.               if (!(cp=tn->tn_RealName))      /* Get tool name */
  192.                cp=tn->tn_Node.ln_Name;
  193.               if (System(cp,MyTags)==-1)      /* Start tool */
  194.                 DisplayBeep(NULL);
  195.               CurrentDir(fl);                 /* Change to old directory */
  196.               break;                          /* quit loop */
  197.              }
  198.           }
  199.          break;
  200.        } /* end of switch(msg->am_Type) */
  201.  
  202.       ReplyMsg(msg); /* Reply message to sender */
  203.      } /* end of while(msg=GetMsg(MyMP)) */
  204.  
  205.    if (windowopen && (gotsigs&wsigs)) /* If window open, got window signal? */
  206.     if (HandleWindowEvent())          /* Handle window event */
  207.     {                                 /* Window close event? */
  208.      CloseStatusWindow();             /* Yes, close window */
  209.      sigs&=~wsigs;                    /* Remove window signals */
  210.      windowopen=FALSE;                /* Window closed */
  211.     }
  212.   } /* end of main loop */
  213.  
  214.  /* If window open, close it */
  215.  if (windowopen) CloseStatusWindow();
  216.  
  217.  /* Exit program */
  218.  cleanup(99);
  219. }
  220.  
  221. /* Final cleanup routine */
  222. void cleanup(int i)
  223. {
  224.  register struct Message *msg;
  225.  
  226.  switch(i)
  227.   {
  228.    case 99:
  229.            RemoveTools();
  230.            RemoveAppMenuItem(MyAppMenuItem);
  231.    case  6:RemoveAppIcon(MyAppIcon);
  232.    case  5:RemPort(MyMP);                           /* Remove message port */
  233.            while (msg=GetMsg(MyMP)) ReplyMsg(MyMP); /* Reply all messages */
  234.            DeleteMsgPort(MyMP);
  235.    case  4:CloseLibrary(GadToolsBase);
  236.    case  3:CloseLibrary(IconBase);
  237.    case  2:CloseLibrary(WorkbenchBase);
  238.    case  1:break;
  239.   }
  240.  
  241.  if (i!=99) exit(i); /* error if i!=99 */
  242.  exit(RETURN_OK);    /* all o.k. */
  243. }
  244.