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

  1. /*
  2.  * toolmanager.c   V1.3
  3.  *
  4.  * startup routines
  5.  *
  6.  * (c) 1991 by Stefan Becker
  7.  *
  8.  */
  9. #include "ToolManager.h"
  10.  
  11. /* Library pointers */
  12. extern struct Library *SysBase,*IntuitionBase,*GfxBase; /* Autoinit Libs */
  13. struct Library *WorkbenchBase,*IconBase,*GadToolsBase,*AslBase;
  14.  
  15. /* CLI command line options */
  16. static char NoIconOpt[]="-n";
  17. static char ConfigOpt[]="-f";
  18.  
  19. /* miscellaneous */
  20. extern char InternalConfigName[];
  21. static struct DiskObject *dobj=NULL;
  22. static struct AppMenuItem *QTMAppMenuItem;
  23. static struct AppMenuItem *OTWAppMenuItem;
  24. static APTR OldConsoleTask=NULL;
  25.  
  26. /* Open libraries and other stuff */
  27. static BOOL openstuff(void)
  28. {
  29.  if (SysBase->lib_Version<36)  /* Sanity check */
  30.   {
  31.    puts("You need a Kickstart 2.0 or better to run ToolManager!");
  32.    exit(20);
  33.   }
  34.  
  35.  /* Open libraries */
  36.  if (!(WorkbenchBase=OpenLibrary(WORKBENCH_NAME,0)))
  37.   cleanup(1);
  38.  
  39.  if (!(IconBase=OpenLibrary(ICONNAME,0)))
  40.   cleanup(2);
  41.  
  42.  if (!(GadToolsBase=OpenLibrary("gadtools.library",0)))
  43.   cleanup(3);
  44.  
  45.  if (!(AslBase=OpenLibrary(AslName,0)))
  46.   cleanup(4);
  47.  
  48.  Forbid();                     /* Is "ToolManager" already running? */
  49.  MyMP=FindPort(MyName);
  50.  Permit();
  51.  if (MyMP) return(TRUE);       /* Yes, don't start again! */
  52.  
  53.  if (!(MyMP=CreateMsgPort()))  /* No, create message port */
  54.   cleanup(5);
  55.  MyMP->mp_Node.ln_Pri=0;
  56.  MyMP->mp_Node.ln_Name=MyName; /* Our name */
  57.  AddPort(MyMP);                /* Announce our presence */
  58.  return(FALSE);                /* No, start ToolManager */
  59. }
  60.  
  61. /* Init tool stuff */
  62. static void inittool(void)
  63. {
  64.  SetConfigFileName(InternalConfigName); /* Copy config file name */
  65.  NewList(&ToolList);                    /* Initialize tool list */
  66.  
  67.  /* Notify Workbench about special menu items. */
  68.  /* 1. If this item is selected, the program will quit */
  69.  if (!(QTMAppMenuItem=AddAppMenuItem(0,NULL,"Quit ToolManager",MyMP,TAG_DONE)))
  70.   cleanup(6);
  71.  
  72.  /* 2. If this item is selected, the status window will open */
  73.  if (!(OTWAppMenuItem=AddAppMenuItem(1,NULL,"Open TM Window",MyMP,TAG_DONE)))
  74.   cleanup(7);
  75. }
  76.  
  77. /* Send an AppMessage with our parameters to a running ToolManager process */
  78. static void SendParameters(LONG nargs, struct WBArg *arg)
  79. {
  80.  struct MsgPort *sp,*rp;
  81.  struct AppMessage *msg;
  82.  
  83.  /* Allocate memory for AppMessage */
  84.  if (!(msg=malloc(sizeof(struct AppMessage)))) goto e1;
  85.  
  86.  /* Create a reply port */
  87.  if (!(rp=CreateMsgPort())) goto e2;
  88.  
  89.  /* Build AppMessage */
  90.  msg->am_Message.mn_Node.ln_Type=NT_MESSAGE;
  91.  msg->am_Message.mn_Node.ln_Pri=0;
  92.  msg->am_Message.mn_ReplyPort=rp;
  93.  msg->am_Type=MTYPE_APPICON;
  94.  msg->am_NumArgs=nargs;
  95.  msg->am_ArgList=arg;
  96.  
  97.  Forbid();                     /* Find "ToolManager" message port */
  98.  sp=FindPort(MyName);
  99.  if (sp) PutMsg(sp,(struct Message *) msg); /* Send AppMessage */
  100.  Permit();
  101.  
  102.  if (sp)
  103.   {                            /* We have send the message */
  104.    WaitPort(rp);               /* Wait on the reply */
  105.    GetMsg(rp);                 /* Remove reply from port */
  106.   }
  107.  
  108.     DeleteMsgPort(rp);
  109. e2: free(msg);
  110. e1: return;
  111. }
  112.  
  113. /* Workbench main entry point */
  114. void wbmain(struct WBStartup *wbarg)
  115. {
  116.  BPTR fl;
  117.  
  118.  if (openstuff())         /* common startup code */
  119.   {                       /* ToolManager already running, send parameters */
  120.    if (wbarg->sm_NumArgs>1)
  121.     SendParameters(wbarg->sm_NumArgs-1,wbarg->sm_ArgList+1);
  122.    cleanup(5);            /* All done */
  123.   }
  124.  
  125.  inittool();              /* Init tool stuff */
  126.  
  127.  /* Process WB startup parameters */
  128.  WBAddToolNode(wbarg->sm_ArgList+1,wbarg->sm_NumArgs-1);
  129.  
  130.  /* Get the program icon */
  131.  fl=CurrentDir(wbarg->sm_ArgList->wa_Lock);
  132.  if (dobj=GetDiskObject(wbarg->sm_ArgList->wa_Name))
  133.   {
  134.    char *cp,**ttp=dobj->do_ToolTypes;
  135.  
  136.    /* Retreive ToolTypes from icon */
  137.    /* 1. CONFIG=<name> Set configuration file name */
  138.    if (cp=FindToolType(ttp,"CONFIG")) SetConfigFileName(cp);
  139.  
  140.    /* 2. INTERNALICON Use the internal icon as AppIcon */
  141.    if (!FindToolType(ttp,"INTERNALICON")) MyIcon=dobj;
  142.  
  143.    /* 3. SHOWICON=YES|NO Show AppIcon */
  144.    if (cp=FindToolType(ttp,"SHOWICON"))
  145.     if (!(ShowIcon=MatchToolValue(cp,"YES")))
  146.      {
  147.       FreeDiskObject(dobj);
  148.       dobj=NULL;
  149.      }
  150.   }
  151.  CurrentDir(fl);
  152.  
  153.  /* Read configuration file */
  154.  ReadConfigFile(wbarg->sm_ArgList->wa_Lock);
  155.  
  156.  mainloop(); /* Go into main loop */
  157. }
  158.  
  159. /* CLI main entry point */
  160. void main(int argc, char **argv)
  161. {
  162.  int i;
  163.  BPTR fl;
  164.  struct Process *pr=FindTask(NULL);
  165.  
  166.  /* Help requested? */
  167.  if ((argc>1) && (*argv[1]=='?'))
  168.   { /* Print out usage, but DON'T start ToolManager */
  169.    puts("\nUsage: ToolManager [-n] [-f <file>] [program 1] ... [program n]");
  170.    puts("       -n : Don't show the program icon");
  171.    puts("       -f : Use <file> as configuration file");
  172.    puts("Requires Kickstart 2.0 or better!\n");
  173.    exit(0);
  174.   }
  175.  
  176.  freopen("*","w",stderr);     /* Reopen the stderr for banner line */
  177.  fputs(WindowTitle,stderr);   /* Put out banner line (only in CLI) */
  178.  fputc('\n',stderr);
  179.  fclose(stderr);
  180.  
  181.  if (openstuff())             /* common startup code */
  182.   {                           /* ToolManager already running, send parameters */
  183.    struct WBArg *wa,*wat;
  184.  
  185.    if (argc<2) cleanup(5);    /* No parameters to send */
  186.  
  187.    /* Allocate memory for WB parameters */
  188.    if (!(wa=malloc(sizeof(struct WBArg)*(argc-1)))) cleanup(5);
  189.  
  190.    fl=CurrentDir(NULL);       /* Get current directory lock */
  191.  
  192.    wat=wa;                    /* Build WBArgs */
  193.    for (i=argc-1; i; i--,wat++)
  194.     {
  195.      ++argv;
  196.  
  197.      if (strcmp(*argv,NoIconOpt) && strcmp(*argv,ConfigOpt))
  198.       {
  199.        wat->wa_Lock=fl;       /* Copy parameters */
  200.        wat->wa_Name=*argv;
  201.       }
  202.      else
  203.       {
  204.        argc--;                /* Filter out CLI command line options */
  205.        wat--;
  206.       }
  207.     }
  208.  
  209.    if (argc>1) SendParameters(argc-1,wa); /* Send parameters */
  210.  
  211.    CurrentDir(fl);            /* Go back to old directory */
  212.    free(wa);                  /* Free WB parameters */
  213.    cleanup(5);                /* All done */
  214.   }
  215.  
  216.  inittool();                  /* Init tool stuff */
  217.  fl=CurrentDir(NULL);         /* Get current directory lock */
  218.  
  219.  /* Process CLI startup parameters */
  220.  for (i=argc-1; i; i--)
  221.   {
  222.    ++argv; /* Next command line parameter */
  223.  
  224.    /* Process CLI options */
  225.    /* 1. "-n" Don't show AppIcon */
  226.    if (!strcmp(*argv,NoIconOpt)) ShowIcon=FALSE;
  227.  
  228.    /* 2. <name> Add tool */
  229.    else if (strcmp(*argv,ConfigOpt)) AddToolNode(fl,*argv,NULL,TRUE);
  230.  
  231.    /* 3. "-f <name>" Set configuration file name */
  232.    else if (i>1)
  233.          {
  234.           i--;
  235.           SetConfigFileName(*++argv);
  236.          }
  237.   }
  238.  
  239.  CurrentDir(DupLock(fl));     /* Go to current directory */
  240.  ReadConfigFile(fl);          /* Read configuration file */
  241.  
  242.  UnLock(CurrentDir(fl));      /* Go back to old directory */
  243.  fclose(stdin);               /* Detach from console window */
  244.  fclose(stdout);
  245.  OldConsoleTask=pr->pr_ConsoleTask;
  246.  pr->pr_ConsoleTask=NULL;
  247.  mainloop();                  /* Go into main loop */
  248. }
  249.  
  250. /* Final cleanup routine */
  251. void cleanup(int i)
  252. {
  253.  register struct Message *msg;
  254.  
  255.  switch(i)
  256.   {
  257.    case 99:
  258.            if (ShowIcon) RemoveAppIcon(MyAppIcon);
  259.            if (dobj) FreeDiskObject(dobj);
  260.    case  8:RemoveTools();
  261.            RemoveAppMenuItem(OTWAppMenuItem);
  262.    case  7:RemoveAppMenuItem(QTMAppMenuItem);
  263.    case  6:RemPort(MyMP);                           /* Remove message port */
  264.            while (msg=GetMsg(MyMP)) ReplyMsg(msg);  /* Reply all messages */
  265.            DeleteMsgPort(MyMP);
  266.    case  5:CloseLibrary(AslBase);
  267.    case  4:CloseLibrary(GadToolsBase);
  268.    case  3:CloseLibrary(IconBase);
  269.    case  2:CloseLibrary(WorkbenchBase);
  270.    case  1:break;
  271.   }
  272.  
  273.  /* Did we have a console window? */
  274.  if (OldConsoleTask)
  275.   {
  276.    struct Process *pr=FindTask(NULL);
  277.  
  278.    pr->pr_ConsoleTask=OldConsoleTask; /* Yes, restore old value */
  279.   }
  280.  
  281.  exit(RETURN_OK);    /* all o.k. */
  282. }
  283.