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

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