home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / System / WBStartup+ / Source / WBStartup+OS3.x / WBStartup+.c < prev    next >
C/C++ Source or Header  |  1998-10-26  |  16KB  |  485 lines

  1. #define VERSION   "2.8"
  2. #define DATENUM   "(12.12.96)"
  3. #define DATETEXT  "December 12, 1996"
  4. #define COPYRIGHT "Copyright © 1996 John Hughes"
  5.  
  6. /* THIS VERSION REQUIRES EXEC.LIBRARY V39  ie, WB3.0 */
  7.  
  8. //#define DEBUG 1
  9.  
  10. /*********************************************************************************/
  11.  
  12. #include <exec/types.h>
  13. #include <clib/dos_protos.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <utility/tagitem.h>    /* TAG_DONE */
  17. #include <dos/exall.h>
  18. #include <clib/alib_protos.h>   /* List */
  19. #include <clib/exec_protos.h>   /* AllocVec() */
  20. #include <string.h>
  21. #include <exec/memory.h>        /* MEMF_PUBLIC */
  22. #include <workbench/startup.h>
  23. #include <dos/dostags.h>
  24. #include <prefs/wbpattern.h>
  25.  
  26. #include "WBStartup+.h"
  27.  
  28. #include <exec/execbase.h>
  29.  
  30. #include <proto/dos.h>
  31. #include <proto/icon.h>
  32. #include <proto/exec.h>
  33. #include <proto/intuition.h>
  34. #include <proto/commodities.h>
  35. #include <proto/utility.h>
  36. #include <proto/input.h>
  37. #include <proto/gadtools.h>
  38. #include <proto/graphics.h>
  39. #include <proto/iffparse.h>
  40. #include <proto/layers.h>
  41.  
  42. #include "ReadKeyboard.h"
  43. #include "ProgressWindow.h"
  44.  
  45. #include <proto/wbstart.h>
  46. struct Library *WBStartBase;
  47.  
  48.  
  49. char WBSTARTUPPLUS[] = "WBStartup+";
  50.  
  51. char const version[]="\0$VER: WBStartup+ "VERSION" "DATENUM"\r\n"COPYRIGHT"\r\njohughes@heartland.bradley.edu\r\nThis is the OS3.x version.\0";
  52.  
  53.  
  54. void GetFilenames(struct List *filenamequeue, APTR memPool, char *directory, BOOL ShowWindow);
  55. void RunPrograms(struct List *filenamequeue, struct WBStartupPrefs *prefs);
  56. void GetArguments(int argc, char **argv, struct WBStartupPrefs *prefs);
  57. void ShowRequester(STRPTR RequesterText);
  58. void DisplayVars(void);
  59. void FreeIcons(struct List *list);
  60. void RunPrefs(struct WBStartupPrefs *prefs);
  61. struct Node *FindNameNoCase(struct List *list, char *name);
  62. BOOL CheckSemaphore(void);
  63.  
  64. extern struct ExecBase *SysBase;
  65.  
  66. struct IntuitionBase *IntuitionBase;
  67. struct Library *CxBase, *IconBase, *UtilityBase, *GadToolsBase, *IFFParseBase, *DataTypesBase, *LayersBase;
  68. struct GfxBase *GfxBase;
  69.  
  70.  
  71. main (int argc, char **argv)
  72. {
  73.   struct List filenamequeue;
  74.   struct WBStartupPrefs prefs={NULL,NULL,0,0,0,0,0,0,0,0,0,0,0};
  75.   APTR   filenamePool;
  76.  
  77.   if (SysBase->LibNode.lib_Version>=39)
  78.   {
  79.     if (IntuitionBase=(struct IntuitionBase *)OpenLibrary("intuition.library",37))
  80.     {
  81.       if (CxBase = OpenLibrary("commodities.library", 37L))
  82.       {
  83.         if (IconBase = OpenLibrary("icon.library", 36L))
  84.         {
  85.           if (UtilityBase = OpenLibrary("utility.library", 37L))
  86.           {
  87.             if (GfxBase = (struct GfxBase *)OpenLibrary("graphics.library", 37L))
  88.             {
  89.               if (GadToolsBase = OpenLibrary("gadtools.library", 37L))
  90.               {
  91.                 if (IFFParseBase = OpenLibrary("iffparse.library", 37L))
  92.                 {
  93.                   if (DataTypesBase=OpenLibrary("datatypes.library",39L))
  94.                   {
  95.                     if (LayersBase=OpenLibrary("layers.library",37L))
  96.                     {
  97.                       if (WBStartBase=OpenLibrary("wbstart.library",0L))
  98.                       {
  99.                         if (filenamePool=CreatePool(MEMF_PUBLIC,31,31))  /* Node is 14 bytes, largest filename is 30 */
  100.                         {
  101.  
  102.                           GetArguments(argc, argv, &prefs);
  103.  
  104.                           if (!IsQualifierDepressed(&prefs))
  105.                           {
  106. //DisplayVars();
  107.  
  108.                             if (CheckSemaphore())
  109.                             {
  110.  
  111.                               NewList(&filenamequeue);
  112.  
  113.                               GetFilenames(&filenamequeue, filenamePool, prefs.ExecutePath, prefs.ShowWindow);
  114.  
  115.                               if (!IsListEmpty(&filenamequeue))
  116.                                 RunPrograms(&filenamequeue, &prefs);
  117.  
  118.                               if (prefs.ShowWindow)
  119.                                 FreeIcons(&filenamequeue);
  120.                             }
  121.                           }
  122.                           DeletePool(filenamePool);
  123.                         }
  124.                         CloseLibrary(WBStartBase);
  125.                       }
  126.                       CloseLibrary(LayersBase);
  127.                     }
  128.                     CloseLibrary(DataTypesBase);
  129.                   }
  130.                   CloseLibrary(IFFParseBase);
  131.                 }
  132.                 CloseLibrary((struct Library *)GfxBase);
  133.               }
  134.               CloseLibrary(GadToolsBase);
  135.             }
  136.             CloseLibrary(UtilityBase);
  137.           }
  138.           CloseLibrary(IconBase);
  139.         }
  140.         CloseLibrary(CxBase);
  141.       }
  142.       CloseLibrary((struct Library *)IntuitionBase);
  143.     }
  144.   }
  145.   else
  146.     ShowRequester("This is the Amiga OS 3.0 version of WBStartup+, please install the OS 2.0 version.");
  147.  
  148.   exit(0);
  149. }
  150.  
  151. void GetFilenames(struct List *filenamequeue, APTR memPool, char *directory, BOOL ShowWindow)
  152. {
  153.   struct ExAllControl *myexallctrl;  /* The ExAllControl structure */
  154.   struct ExAllData    *eadptr;       /* temp pointer to data structure */
  155.   BPTR mylock;                       /* Lock on the directory */
  156.   int more;                          /* Are there more entries ? */
  157.   struct ExAllData ExAllBuf[20];     /* hold the filenames and types */
  158.   struct Node *node;                 /* hold the filenames and proiority for the List */  /* Is actually WBStartupNode */
  159.   struct DiskObject *diskobj;        /* Pointer to the icon info */
  160.   char *toolstring;                  /* Pointer to a tooltype string */
  161.   char pathname[500];                /* path and filename of the icon */
  162.   char filename[32];                 /* parsed match string 2*length+2=22 */
  163.  
  164.  
  165.   if (mylock=Lock(directory,SHARED_LOCK))
  166.   {
  167.     if (myexallctrl=(struct ExAllControl *)AllocDosObjectTags(DOS_EXALLCONTROL,TAG_END))
  168.     {
  169.       myexallctrl->eac_LastKey = 0;
  170.       myexallctrl->eac_Entries = 0;
  171.       myexallctrl->eac_MatchString = NULL;
  172.       myexallctrl->eac_MatchFunc = NULL;
  173.  
  174.       do   /* go until more==0 */
  175.       {
  176.         more = ExAll(mylock, ExAllBuf, sizeof(ExAllBuf), ED_TYPE, myexallctrl);
  177.         if ((!more) && (IoErr() != ERROR_NO_MORE_ENTRIES))
  178.         {
  179.           ShowRequester("ExAll failed abnormally");//: %ld.", IoErr());
  180.           break;
  181.         }
  182.  
  183.         if (myexallctrl->eac_Entries)
  184.         {
  185.           eadptr=ExAllBuf;
  186.  
  187.           while (eadptr)
  188.           {
  189.             if (eadptr->ed_Type < 0)  /* make sure it is a file, not a directory, see end of dos/dosextens.h */
  190.             {
  191.               strcpy(filename, eadptr->ed_Name);
  192.  
  193.               /* truncate the filename if it is an icon, ie. remove the ".info" */
  194.               if (!Stricmp(filename+strlen(filename)-5,".info"))
  195.                 filename[strlen(filename) - 5] = NULL;  /* take the .info off of the string */
  196.               else
  197.                 filename[strlen(filename)] = NULL;  /* take the .info off of the string */
  198.  
  199.               /* See if the file is already in the list */
  200.               if (!FindNameNoCase(filenamequeue,filename))
  201.               {
  202.                 if (Stricmp(filename,WBSTARTUPPLUS))  /* Make sure we don't recursively run ourself */
  203.                 {
  204.                   strcpy(pathname,directory);
  205.                   AddPart(pathname, filename, 500);
  206.                   if (diskobj=GetDiskObjectNew(pathname))
  207.                   {
  208.                     if ((diskobj->do_Type==WBPROJECT) || (diskobj->do_Type==WBTOOL))  /* Make sure we got an icon that isn't a drawer icon */
  209.                     {
  210.                       if (node=(struct Node *)AllocPooled(memPool,sizeof(struct WBStartupNode)))
  211.                       {
  212.                         if (node->ln_Name=(char *)AllocPooled(memPool, strlen(pathname)+1))
  213.                         {
  214.                           strcpy(node->ln_Name, filename);
  215.                           if (toolstring=FindToolType(diskobj->do_ToolTypes,"STARTPRI"))
  216.                             node->ln_Pri=atoi((char *)toolstring);
  217.                           else
  218.                             node->ln_Pri=0;
  219.                           ((struct WBStartupNode *)node)->StackSize = diskobj->do_StackSize;
  220.                           if (toolstring=FindToolType(diskobj->do_ToolTypes,"TOOLPRI"))
  221.                             ((struct WBStartupNode *)node)->toolpri = strtol(toolstring,NULL,10);
  222.                           else
  223.                             ((struct WBStartupNode *)node)->toolpri = 0;
  224.                           if (toolstring=FindToolType(diskobj->do_ToolTypes,"WAIT"))
  225.                             ((struct WBStartupNode *)node)->wait = strtoul(toolstring,NULL,10);
  226.                           else
  227.                             ((struct WBStartupNode *)node)->wait = 0;
  228.                           if (ShowWindow)
  229.                             ((struct WBStartupNode *)node)->diskobj = diskobj;
  230.                           else
  231.                             FreeDiskObject(diskobj);
  232.                           Enqueue(filenamequeue,node);
  233.                         }
  234.                       }
  235.                     }
  236.                     else
  237.                       FreeDiskObject(diskobj);
  238.                   }
  239.                 }
  240.               }
  241.             }
  242.             eadptr=eadptr->ed_Next;
  243.           }
  244.         }
  245.  
  246.       } while (more);
  247.  
  248.       FreeDosObject(DOS_EXALLCONTROL,myexallctrl);
  249.     }
  250.     else
  251.       ShowRequester("Not enough memory for ExAllControl.");
  252.     UnLock(mylock);
  253.   }
  254.   else
  255.     ShowRequester("Couldn't lock directory.");
  256. }
  257.  
  258.  
  259. void RunPrograms(struct List *filenamequeue, struct WBStartupPrefs *prefs)
  260. {
  261.   struct Node *node;           /* used to go through the filenames queue */
  262.   BPTR fl;
  263.   BPTR olddirlock;
  264.   LONG numprograms=0,currentprogram=0;      // used for the progress bar
  265.   struct ProgressWindowData *windata=NULL;  // this must be initialized to NULL!
  266.  
  267.  
  268.   if (prefs->ShowWindow)
  269.   {
  270.     /* Count the # of programs to run */
  271.     for (node=filenamequeue->lh_Head; node->ln_Succ; node = node->ln_Succ)
  272.       numprograms++;
  273.     /* Open and set up the Progress Window */
  274.     windata=CreateProgressWindow(prefs);
  275.   }
  276.  
  277.   fl=Lock(prefs->ExecutePath,SHARED_LOCK);
  278.   olddirlock=CurrentDir(fl);
  279.  
  280.   for (node=filenamequeue->lh_Head; node->ln_Succ; node=node->ln_Succ)
  281.   {
  282.     currentprogram++;
  283.  
  284.     /* Update Progress Bar in Window */
  285.     if (windata)
  286.     {
  287.       UpdateProgressBar(windata->win,currentprogram,numprograms);
  288.       ShowIconImage(windata,((struct WBStartupNode *)node)->diskobj->do_Gadget.GadgetRender);
  289.       DisplayProgramName(windata,node->ln_Name, (prefs->Interactive) ? TRUE : FALSE);
  290.       /* Interactive Mode */
  291.       if (prefs->Interactive)
  292.         if (!InteractiveRunProgram(windata->win))
  293.           continue;  //Skip this icon
  294.     }
  295.  
  296.     WBStartTags(WBStart_Name, node->ln_Name, WBStart_DirectoryLock, fl, TAG_DONE);
  297.  
  298.     /* Wait the amount of time specified in the WAIT= tooltype */
  299.     if (((struct WBStartupNode *)node)->wait)
  300.       Delay(((struct WBStartupNode *)node)->wait*50);
  301.  
  302.   }
  303.   /* Free resources */
  304.   CurrentDir(olddirlock);
  305.   UnLock(fl);
  306.  
  307.   if (windata)
  308.     CloseProgressWindow(windata);
  309.  
  310.   /* All OK! */
  311.   return;
  312. }
  313.  
  314. void GetArguments(int argc, char **argv, struct WBStartupPrefs *prefs)
  315. {
  316.   UBYTE **ttypes;
  317.   UBYTE *toolstring;
  318.  
  319.   ttypes = ArgArrayInit(argc,argv);
  320.   strcpy(prefs->ExecutePath , ArgString(ttypes,"ENABLEDPATH","SYS:WBStartup/WBStartup (Enabled)"));
  321.   strcpy(prefs->StoragePath , ArgString(ttypes,"DISABLEDPATH","SYS:WBStartup/WBStartup (Disabled)"));
  322.   prefs->ShowWindow = ((FindToolType(ttypes,"PROGRESSWINDOW")) ? TRUE : FALSE);
  323.   prefs->BackgroundFilename[0]=NULL;
  324.   if (toolstring=FindToolType(ttypes,"BACKGROUNDPIC"))
  325.   {
  326.     if (MatchToolValue(toolstring,"WORKBENCH"))
  327.       prefs->BackgroundType=WORKBENCH;
  328.     else if (MatchToolValue(toolstring,"SCREEN"))
  329.       prefs->BackgroundType=SCREEN;
  330.     else if (MatchToolValue(toolstring,"WINDOWS"))
  331.       prefs->BackgroundType=WINDOWS;
  332.     else if (MatchToolValue(toolstring,"NONE"))
  333.       prefs->BackgroundType=NONE;
  334.     else
  335.     {
  336.       strncpy(prefs->BackgroundFilename,toolstring,200);
  337.       prefs->BackgroundFilename[strlen(toolstring)]=NULL;
  338.       prefs->BackgroundType=USERDEFINED;
  339.     }
  340.   }
  341.   else
  342.   {
  343.     prefs->BackgroundType=WINDOWS;
  344.   }
  345.   strcpy(prefs->PrefsPath , ArgString(ttypes,"PREFSPATH","SYS:Prefs/WBStartup+Prefs"));
  346.   strcpy(prefs->PubScreenName , ArgString(ttypes,"PUBSCREEN","Workbench"));
  347.   ArgArrayDone();
  348. }
  349.  
  350. void ShowRequester(STRPTR RequesterText)
  351. {
  352.   struct EasyStruct myRequestStruct={
  353.     sizeof (struct EasyStruct ),
  354.     0,
  355.     WBSTARTUPPLUS,
  356.     NULL,
  357.     "Ok"};
  358.  
  359.     myRequestStruct.es_TextFormat=RequesterText;
  360.     EasyRequestArgs(NULL,&myRequestStruct,NULL,NULL);
  361. }
  362.  
  363. /*
  364. #include <dos/dosextens.h>
  365. #include <dos/var.h>
  366. void DisplayVars(void)
  367. {
  368.   struct Process *pr;
  369.   struct LocalVar *node;
  370.   char   buffer[1000];
  371.   BPTR file;
  372.  
  373.   if (file=Open("CON:////",MODE_NEWFILE))
  374.   {
  375.     Write(file,"These are vars in WBStartup+:\n",30);
  376.     if (pr = (struct Process *)FindTask(NULL))
  377.     {
  378.       for (node = (struct LocalVar *)pr->pr_LocalVars.mlh_Head; node->lv_Node.ln_Succ; node = (struct LocalVar *)node->lv_Node.ln_Succ)
  379.       {
  380.         sprintf(buffer,"%s\n",node->lv_Node.ln_Name);
  381.         Write(file,buffer,strlen(buffer));
  382.       }
  383.       Delay(50*5);
  384.     }
  385.     Close(file);
  386.   }
  387. }
  388.  
  389. */
  390.  
  391.  
  392. void FreeIcons(struct List *list)
  393. {
  394.   struct Node *node;
  395.  
  396.   for (node=list->lh_Head; node->ln_Succ; node=node->ln_Succ)
  397.     FreeDiskObject(((struct WBStartupNode *)node)->diskobj);
  398. }
  399.  
  400. void RunPrefs(struct WBStartupPrefs *prefs)
  401. {
  402.   struct WBStartupNode node;
  403.   struct List list;
  404.   struct MsgPort *mp;
  405.   struct WBStartupPrefs newprefs;
  406.   char filename[35];
  407.  
  408.   /* Set up filename list, which only consists of the WBStartup+Prefs program */
  409.   NewList(&list);
  410.   node.diskobj=NULL;
  411.   node.StackSize=4096;
  412.   node.wait=0;
  413.   node.toolpri=0;
  414.   node.node.ln_Name=filename;
  415.   strcpy(filename,FilePart(prefs->PrefsPath));
  416.   AddHead(&list,&node);
  417.  
  418.   /* Set up New Prefs structure */
  419.   strncpy(newprefs.ExecutePath,prefs->PrefsPath,PathPart(prefs->PrefsPath)-prefs->PrefsPath);
  420.   newprefs.StoragePath[0]=NULL;
  421.   newprefs.ShowWindow=FALSE;
  422.   newprefs.Interactive=FALSE;
  423.   newprefs.PrefsPath[0]=NULL;
  424.  
  425.   RunPrograms(&list,&newprefs);
  426.  
  427.   if (mp=CreatePort(WBSTARTUPPLUS,0))
  428.   {
  429.     Wait( (1 << mp->mp_SigBit) | SIGBREAKF_CTRL_C);   /* Wait for WBStartup+Prefs to complete OR a CTRL-C */
  430.     DeletePort(mp);
  431.   }
  432. }
  433.  
  434. struct Node *FindNameNoCase(struct List *list, char *name)
  435. {
  436.   struct Node *node;
  437.   struct Node *result=NULL;
  438.  
  439.   for (node=list->lh_Head; node->ln_Succ; node = node->ln_Succ)
  440.     if (!Stricmp(name,node->ln_Name))
  441.     {
  442.       result = node;
  443.       break;
  444.     }
  445.   return(result);
  446. }
  447.  
  448. BOOL CheckSemaphore(void)
  449. {
  450.   /* RETURN TRUE if this is the 1st time WBStartup+ has been run, OR if the user says it is ok to run it again */
  451.   struct SignalSemaphore *sema;
  452.   char   *name;
  453.   BOOL ContinueRunning = TRUE;
  454.  
  455.   Forbid();
  456.   if (!FindSemaphore(WBSTARTUPPLUS))
  457.   {
  458.     if (name=(char *)AllocMem(11,MEMF_PUBLIC))
  459.     {
  460.       strcpy(name,WBSTARTUPPLUS);
  461.       if (sema=(struct SignalSemaphore *)AllocMem(sizeof(struct SignalSemaphore),MEMF_PUBLIC|MEMF_CLEAR))
  462.       {
  463.         sema->ss_Link.ln_Name=name;
  464.         AddSemaphore(sema);
  465.       }
  466.     }
  467.     Permit();
  468.   }
  469.   else  /* Semaphore was found, meaning WBStartup+ has already been run */
  470.   {
  471.     struct EasyStruct myRequestStruct={
  472.       sizeof (struct EasyStruct ),
  473.       0,
  474.       WBSTARTUPPLUS,
  475.       "Run WBStartup+ again?",
  476.       "Ok|Cancel"};
  477.  
  478.     ContinueRunning = (BOOL)EasyRequestArgs(NULL,&myRequestStruct,NULL,NULL);
  479.  
  480.     Permit();
  481.   }
  482.  
  483.   return(ContinueRunning);
  484. }
  485.