home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / commodities / superdark / main / main.c < prev    next >
C/C++ Source or Header  |  1993-03-21  |  16KB  |  616 lines

  1. /************************************************************************/
  2. /*                                    */
  3. /*    File main.c                            */
  4. /*                                    */
  5. /************************************************************************/
  6. /*  This is the main entry point of SuperDark. Open all the ressources, */
  7. /* and call the other function. Also look for all the availables    */
  8. /* modules and launch them                        */
  9. /*                                    */
  10. /************************************************************************/
  11.  
  12. #include <exec/types.h>
  13. #include <exec/nodes.h>
  14. #include <exec/ports.h>
  15. #include <exec/memory.h>
  16. #include <intuition/intuition.h>
  17. #include <intuition/gadgetclass.h>
  18. #include <libraries/gadtools.h>
  19. #include <libraries/reqtools.h>
  20. #include <clib/exec_protos.h>
  21. #include <graphics/displayinfo.h>
  22. #include <dos/dos.h>
  23. #include <dos/dosextens.h>
  24. #include <clib/dos_protos.h>
  25. #include <workbench/startup.h>
  26. #include "/includes/struct.h"
  27. #include "/includes/tom_gadget.h"
  28. #include "gid_main.h"
  29.  
  30. #ifndef    WB_13
  31. struct    Library *CxBase;
  32. #endif
  33. struct    Library    *GadToolsBase;
  34. struct    Library    *IconBase;
  35. struct    Library    *IntuitionBase;
  36. struct    Library    *GfxBase;
  37. struct    Library    *CxBase;
  38. struct    ReqToolsBase *ReqToolsBase;
  39.  
  40. extern    struct my_data    my_info;
  41. extern    struct    tom_gadget    my_gadg[];
  42. extern    struct    tom_gadget    gadg_alea[];
  43.  
  44.  
  45. #define    STRLEN_DARK    5        /* Equivalent to strlen(".dark") */
  46. struct    contexe    big_ctx;
  47.  
  48. /* Structure qui sauvegarde les parametres */
  49.  
  50. int    selection=-1;            /* Numero de l'effet selectionne */
  51. int    last_sel=-1;            /* dernier numero selecitonne     */
  52. BPTR    segment=0;
  53. int    flg_end;            /* Drapeau indiquant la sortie du prog */
  54. UBYTE    flg_debug=FALSE;
  55. char    *p_name;
  56. UBYTE    flg_prog_dark;
  57. UBYTE    flg_port_created;
  58. struct    WBStartup    my_msg;
  59.  
  60. struct    Process    *process;
  61. extern    struct    appel_proc    data_proc;
  62. char    p_dark[]="That's Dark!";
  63. int    nb_module=0;
  64. char    *p_nom[MAX_NOM];
  65. struct    Node ModulesNodes[MAX_NOM];
  66. struct    List    *mode_liste;
  67.  
  68. #define    RANDOM    "Random"
  69.  
  70. char    *p_alea=RANDOM;
  71. struct    Node alea_node={0,0,0,0,
  72. RANDOM};
  73. BPTR    old_lock=-1;
  74. struct    MsgPort    *rep=0;
  75. char    buffer[100];
  76. extern    char    buffer_effet[];
  77. LONG    dir_save=0;
  78.  
  79. char    p_ver[]="\0$VER: SuperDark  1.4 (20/03/93)";
  80.  
  81. struct List ModulesList = {
  82.     ( struct Node * )&ModulesList.lh_Tail,
  83.     ( struct Node * )0l,
  84.     ( struct Node * )&ModulesList.lh_Head,0,0 };
  85.  
  86. /*********************************************************** aff_error() */
  87.  
  88. void    aff_error(char *p_texte)
  89. {
  90.     affiche_texte(p_texte,NULL,NULL);
  91.  
  92. }
  93.  
  94. /************************************************************ set_ext() */
  95. /* remove evrything */
  96. void    stop()
  97. {
  98.     big_ctx.flg_end=TRUE;
  99. }
  100. /************************************************************ set_ext() */
  101. /* open window */
  102. void    show()
  103. {
  104.     show_win(&big_ctx);
  105. }
  106.  
  107. /************************************************************ set_ext() */
  108.  
  109. void    set_ext(char *pc)
  110. {
  111.     pc[strlen(pc)]='.';
  112. }
  113.  
  114. /************************************************************ unset_ext() */
  115.  
  116. void    unset_ext(char *pc)
  117. {
  118.     pc[strlen(pc)-STRLEN_DARK]=0;
  119. }
  120.  
  121. /************************************************************ fin_mod() */
  122.  
  123. /*****************************************************/
  124. /* This function look for the availables modules in: */
  125. /*   - the directory saved in the .info file as a    */
  126. /*     tool type                     */
  127. /*   - then look in the current directory         */
  128. /*****************************************************/
  129.  
  130. int    find_mod(char *name_dir)
  131. {
  132.     struct Process *process;
  133.     int    flg_err;
  134.     int    etape,i,len;
  135.     struct    FileInfoBlock    *fib;
  136.     BPTR    lt;
  137.     char    *p;
  138.     UBYTE    flg_deuxieme_passe;
  139.  
  140.     flg_err=FALSE;
  141.     etape=0;
  142.     lt=0;
  143.     fib=0;
  144.     flg_deuxieme_passe=FALSE;
  145.         process = (struct Process *)FindTask(0);
  146.     do{
  147.         printd("find mod,etape:%ld\n",etape);
  148.         switch(etape){
  149.             case    0:
  150.                 /* Allocation memoire pour le FileInfo Block    */
  151.                 /* Je rapelle aue le fib doit etre aligne sur    */
  152.                 /* un mot long....merci les BPTR            */
  153.                 if(fib==0){
  154.                     if((fib=(struct FileInfoBlock *)AllocMem(sizeof(struct FileInfoBlock),0))==0){
  155.                         flg_err=TRUE;
  156.                     }
  157.                 }
  158.                 break;
  159.             case    1:
  160.  
  161.                 /* On cherche dans le repertoire passe dans le    */
  162.                 /* .info, si celui -ci existe..            */
  163.  
  164.                 if(name_dir!=0){
  165.                    if(*name_dir!=0){
  166.                     len=strlen(name_dir);
  167.                     if(name_dir[len-1]=='/'){
  168.                         name_dir[len-1]=0;
  169.                     }
  170.                     lt=Lock(name_dir,ACCESS_READ);
  171.                     if(lt!=0){
  172.                         old_lock=CurrentDir(lt);
  173.                         printd("new dir ok %lx old:%lx\n",lt,old_lock);
  174.                     }else{
  175.                         printd("Lock de %s failed!\n",name_dir);
  176.                     }
  177.                    }
  178.                 }
  179.                 break;
  180.  
  181.             case    2:
  182.                 /* Si on n'a pas reussi a locke le repertoire donne */
  183.                 /* on cherche dans le repertoire courant         */
  184.                 if(lt==0){
  185.                     flg_deuxieme_passe=TRUE;
  186.                     lt=process->pr_CurrentDir;
  187.                     if(lt==0){
  188.                         flg_err=TRUE;
  189.                         aff_error("Erreur avec current dir");
  190.                     }
  191.                 }
  192.                 break;
  193.             case 3:
  194.                 /* On examine le repertoire */
  195.                 printd("Avant le examine:lt:%lx fib:%lx\n",lt,fib);
  196.                 if(Examine(lt,fib)==0){
  197.                     aff_error("Erreur examine....\n");
  198.                     flg_err=TRUE;
  199.                 }
  200.                 break;
  201.             case 4:
  202.                 /* On examine d'abord les fichiers    */
  203.                 while(ExNext(lt,fib)!=NULL){
  204.                     
  205.                     if(fib->fib_DirEntryType<0){
  206.                         if(strlen(fib->fib_FileName)>5){
  207.                         p=fib->fib_FileName+strlen(fib->fib_FileName)-5;
  208.                         /* This is the correction of the 'Usnet' */
  209.                         /* bug....so superdark if case indepdt.  */
  210.                         for(i=1;i<5;i++){
  211.                             p[i]=tolower(p[i]);
  212.                         }
  213.                         if(strcmp(p,".dark")==0){
  214.                             if(nb_module<MAX_NOM-1){
  215.                                 p_nom[nb_module]=(char *)malloc(strlen(fib->fib_FileName)+1);
  216.                                 strcpy(p_nom[nb_module],fib->fib_FileName);
  217.                                 unset_ext(p_nom[nb_module]);
  218.                                 nb_module++;
  219.                             }else{
  220.                                 SPrintF(buffer,"ERROR:Max number of modules exceded(%ld)\n",MAX_NOM);
  221.                                 aff_error(buffer);
  222.                             }
  223.                         }
  224.                     }
  225.                    }
  226.                 }
  227.                 break;
  228.             case 5:
  229.                 /* Si on n'avait trouve aucun module, on re-essaye */
  230.                 /* une deuxieme fois dans le repertoire courant       */
  231.  
  232.                 if((nb_module==0)&&(flg_deuxieme_passe==FALSE)&&(lt!=0)){
  233.                     printd("Deuxieme passage:lt:%x,old_lock:%x\n",lt,old_lock);
  234.                     CurrentDir(lt);
  235.                     printd("Je fais un CurrentDir....new:%lx\n",lt);
  236.                     lt=0;
  237.                     etape=3-1;
  238.                 }
  239.                 break;
  240.  
  241.         }
  242.         etape++;
  243.     /* Tant qu'il y a des etapes a effectuer    */
  244.     }while((etape<6)&&(flg_err==FALSE));
  245.  
  246.     if(nb_module!=0){
  247.  
  248.         tqsort(p_nom,nb_module);
  249.  
  250.         p_nom[nb_module]=p_alea;
  251.  
  252.         for(i=0;i<nb_module;i++){
  253.             ModulesNodes[i].ln_Name=p_nom[i];
  254.             ModulesNodes[i].ln_Type=0;
  255.             AddTail(&ModulesList,&ModulesNodes[i]);
  256.             if(strcmp(p_nom[i],buffer_effet)==0){
  257.                 selection=i;
  258.             }
  259.         }
  260.  
  261.         AddTail(&ModulesList,&alea_node);
  262.         my_info.tempo_end=10;
  263.  
  264.         if(selection==-1){
  265.             my_info.flg_alea=TRUE;
  266.             selection=nb_module;
  267.         }
  268.         my_gadg[GID_MODULES].value=selection;
  269.     }else{
  270. /*
  271.         flg_err=1;
  272. */
  273.         if(name_dir!=0){
  274.             SPrintF(buffer,"No dark modules was found in the dir:\n\n%s\n\nHave you put the good directory in the\nTool type DARK DIRECTORY ?\n",name_dir);
  275.         }else{
  276.             SPrintF(buffer,"No dark modules was found!\nHave you put the good directory in the\nTool type DARK DIRECTORY ?\n",name_dir);
  277.         }
  278.         if(old_lock!=-1){
  279.             printd("je remt old_lock:%x\n",old_lock);
  280.             CurrentDir(old_lock);
  281.         }
  282.         aff_error(buffer);
  283.     }
  284.  
  285.     if(fib!=0)FreeMem(fib,sizeof(struct FileInfoBlock));
  286.     return(flg_err);
  287. }
  288.  
  289. /************************************************************ init_port *****/
  290.  
  291. int    init_port()
  292. {
  293.     struct    MsgPort    *mp;
  294.     struct    appel_proc *ap;
  295.  
  296.     srand(VBeamPos());
  297.     my_info.sigstart=-1;
  298.     my_info.sigshow=-1;
  299.     my_info.sigstop=-1;
  300.     my_info.signext=-1;
  301.     rep=(struct MsgPort *)CreatePort("Reponse",NULL);
  302.     my_info.flg_alea=FALSE;
  303.     flg_port_created=FALSE;
  304.     if((ap=(struct appel_proc *)FindPort(p_dark))==0){
  305.         mp=&data_proc.mp;
  306.         mp->mp_Node.ln_Name=p_dark;
  307.         mp->mp_Node.ln_Pri=0;
  308.         mp->mp_Node.ln_Type=NT_MSGPORT;
  309.         mp->mp_Flags=PA_IGNORE;
  310.         AddPort(&data_proc.mp);    
  311.  
  312.         my_info.tempo=100;
  313.         data_proc.p_info=&my_info;
  314.         flg_port_created=TRUE;
  315.  
  316.         return(FALSE);
  317.     }else{
  318.         printd("Dark deja demarre!\n");
  319.  
  320.         /**********************************/
  321.         /* On force la fenetre a s'ouvrir */
  322.         /**********************************/
  323. #ifdef    WB_13
  324.         /* En 1.3, sans commodities, on envoi le signal a l'autre */
  325.         /* tache et on sort                      */
  326.         Signal(ap->p_info->task,1<<(ap->p_info->sigshow));
  327.         return(TRUE);
  328. #else
  329.         /* en 2.0, l'autre blanker recevral e msg UNIQUE, et celui */
  330.         /* (le nouveau) sortira lors du CxBroker           */
  331.         return(FALSE);
  332. #endif
  333.     }
  334. }
  335.  
  336. /************************************************************ remove_seg() ***/
  337. void    remove_seg()
  338. {
  339.     if(segment!=0){
  340.         Signal(&process->pr_Task,SIG_MAIN_DARK_EXIT);
  341.         WaitPort(rep);
  342.         GetMsg(rep);
  343.         UnLoadSeg(segment);
  344.         segment=0;
  345.     }
  346. }
  347. /*************************************************************** free_mod() **/
  348. void    free_mod()
  349. {
  350.     int    i;
  351.     for(i=0;i<nb_module;i++){
  352.         free(p_nom[i]);
  353.     }
  354.     while(RemHead(&ModulesList));
  355.     nb_module=0;
  356.     remove_seg();
  357. }
  358.  
  359. /*************************************************************** do_dark() **/
  360.  
  361. void    do_dark()
  362. {
  363.     int    num_module;
  364.     struct    Task    *mt;
  365.     int    i;
  366.  
  367.     if(((last_sel!=selection)||(segment==0))&&(nb_module!=0)){
  368.         if(segment!=0){
  369.             Signal(&process->pr_Task,SIG_MAIN_DARK_EXIT);
  370.             printd("J'attend le message de l'exit!\n");
  371.             WaitPort(rep);
  372.             GetMsg(rep);
  373.             UnLoadSeg(segment);
  374.         }
  375.         num_module=selection%nb_module;
  376.         last_sel=selection;
  377.  
  378.         data_proc.p_texte=0;
  379.         set_ext(p_nom[num_module]);
  380.         printd("Je charge le %ld qui est:[%s]\n",num_module,p_nom[num_module]);
  381.         segment=LoadSeg(p_nom[num_module]);
  382.  
  383.         if(segment!=0){
  384.     
  385.             /************************************************/
  386.             /* We create a new task, with a given taskpri    */
  387.             /* Note: be carefull, the processe struct is not*/
  388.             /* at the same adresse than the task struct.    */
  389.             /* Note2: The task is created with a high    */
  390.             /* priority, but this priority is changed just    */
  391.             /* after.                    */
  392.             /************************************************/
  393.  
  394.             process=(struct Process *)((char *)CreateProc(p_nom[num_module],
  395.                             0,
  396.                             segment,
  397.                             8192)-sizeof(struct Task));
  398.             mt=FindTask(0);
  399.             my_msg.sm_Message.mn_ReplyPort=&data_proc.mp;
  400.             my_msg.sm_Message.mn_ReplyPort=rep;
  401.  
  402.             my_msg.sm_ToolWindow=0;
  403.             my_msg.sm_ArgList=0;
  404.             my_msg.sm_NumArgs=0;
  405.             process->pr_Task.tc_UserData=mt;
  406.             my_info.task_dark=&process->pr_Task;
  407.             PutMsg(&process->pr_MsgPort,(struct Msg *)&my_msg);
  408.     
  409.             /***********************************************************/
  410.             /* On positionne le code retour, et on attend qu'il change */
  411.             /* d'etat, indiquant que l'init est OK. On ne peut pas     */
  412.             /* de 'meilleur' type de communication, car on n'est pas   */
  413.             /* sur que c'est un module pour dark, et on risquerqais    */
  414.             /* alors de bloquer en attente de reponse           */
  415.             /***********************************************************/
  416.     
  417.             i=500;
  418.             data_proc.code_ret=-1;
  419.             flg_prog_dark=TRUE;
  420. /*            while((data_proc.code_ret==-1)&&(i-->0)){*/
  421.             while(data_proc.code_ret==-1){
  422.                 Delay(1);
  423.             }
  424.             if(data_proc.code_ret==-1){
  425.                 aff_error("ERROR:This is not a Dark Module!\n");
  426.                 flg_prog_dark=FALSE;
  427.             }else{
  428.                 SetTaskPri(    &process->pr_Task,
  429.                         my_gadg[GID_TASK_PRI].value);
  430.                 printd("Loading conf\n");
  431.                 LoadConfig(p_nom[num_module],data_proc.p_gadg);
  432.             }
  433. #ifdef    WB_13
  434.             /* Si c'est un module qui a besoin du WB2.0 */
  435.             /* il ne faut pas l'executer...            */
  436.             if((data_proc.code_ret==DARK_WB_20)){
  437.                 aff_error("This one need 2.0 to run!\n");
  438.                 flg_prog_dark=FALSE;            
  439.             }
  440. #endif
  441.         }else{
  442.             aff_error("segment not found\n");
  443.             selection=-1;
  444.         }
  445.         unset_ext(p_nom[num_module]);
  446.     }
  447. }
  448.  
  449. /*********************************************** Liberation des ressources **/
  450.  
  451. void    free_all()
  452. {
  453.     if(mode_liste!=0)free_modes(mode_liste);
  454.     remove_seg();
  455.     if(flg_port_created)RemPort(&data_proc.mp);
  456.     if(rep)RemPort(rep);
  457.  
  458.     free_input_ev();
  459.     CleanStuff_gadg(&big_ctx);
  460.     if(old_lock!=-1){
  461.         CurrentDir(old_lock);
  462.     }
  463.     if(IconBase)CloseLibrary(IconBase);
  464. #ifndef    WB_13
  465.     if(CxBase)CloseLibrary(CxBase);
  466.     if(GadToolsBase)CloseLibrary(GadToolsBase);
  467. #endif
  468.     if(IntuitionBase)CloseLibrary(IntuitionBase);
  469.     if(GfxBase)CloseLibrary(GfxBase);
  470.     if(ReqToolsBase)CloseLibrary ((struct Library *)ReqToolsBase);
  471. }
  472.  
  473. /************************************************************ main     *****/
  474.  
  475. /************************************************/
  476. /* main function. Open the libs, and if all is    */
  477. /* ok, call the other functions            */
  478. /************************************************/
  479.  
  480. main(argc,argv)
  481. int    argc;
  482. char    *argv[];
  483. {
  484.     int    etape,flg_err,i;
  485.     struct    WBStartup *WBenchMsg;
  486.     struct    WBArg    *wbarg;
  487.     LONG    olddir;
  488.     extern    struct    List    *get_modes();
  489.  
  490.     i=1;
  491.     while(i<argc){
  492.  
  493.         if(strcmp(argv[i],"-d")==0){
  494.             flg_debug=TRUE;
  495.         }
  496.         i++;
  497.     }
  498.     p_name=argv[0];
  499.     olddir=-1;
  500.  
  501.         etape=0;
  502.     flg_err=FALSE;
  503.     while((flg_err==FALSE)&&(etape<7)){
  504.         printd("Etape:%ld\n",etape);
  505.         switch(etape){
  506.  
  507.            case 0:
  508.  
  509.             /* First, we open all the needed library    */
  510.  
  511.             IntuitionBase=(struct Library *)OpenLibrary("intuition.library",0);
  512.             GfxBase=(struct Library *)OpenLibrary("graphics.library",0);
  513.             IconBase=(struct Library *)OpenLibrary("icon.library",0);
  514. #ifndef    WB_13
  515. /*            CxBase=OpenLibrary("commodities.library",37L);*/
  516.             GadToolsBase=(struct Library *)OpenLibrary("gadtools.library",0);
  517.             if(GadToolsBase==0){
  518.                 flg_err=TRUE;
  519.                 aff_error("This doesn't seem to be a\nWB 2.0 or higher\n\nUse SuperDark_1.3\n\n");
  520.             }
  521.             CxBase=OpenLibrary("commodities.library",37L);
  522. #endif
  523.             ReqToolsBase = (struct ReqToolsBase *)OpenLibrary (REQTOOLSNAME, REQTOOLSVERSION);
  524.             break;
  525.  
  526.             /* initialisation du port de communication */
  527.            case 1:
  528.             flg_err=init_port();
  529.             break;
  530.             /* chargement des parametres de config */
  531.            case 2:
  532.             /* Du WB? */
  533.             if(argc==0){
  534.                 WBenchMsg=(struct WBStartup *)argv;
  535.                 for(i=0,wbarg=WBenchMsg->sm_ArgList;
  536.                     i<WBenchMsg->sm_NumArgs;
  537.                     i++,wbarg++){
  538.                     if((wbarg->wa_Lock)&&(*wbarg->wa_Name)){
  539.                         p_name=wbarg->wa_Name;
  540.                         dir_save=wbarg->wa_Lock;
  541.                         olddir=CurrentDir(dir_save);
  542.                         printd("Je fais un CurrentDir..old:%lx new:%lx\n",olddir,dir_save);
  543.                     }
  544.                 }
  545.                 
  546.             }
  547.             LoadConfig(p_name,&my_gadg[GID_TEMPO]);
  548.             my_info.tempo=my_gadg[GID_TEMPO].value;
  549.             printd("Tempo:%ld\n",my_info.tempo);
  550.  
  551.             /* Rem: les parametres du module aleatoire sont sauvegarde*/
  552.             /* avec ceux du fichier principal, car le mode aleatoire  */
  553.             /* n'ayant pas d'icone...                  */
  554.             LoadConfig(p_name,gadg_alea);
  555.             break;
  556.  
  557.             /* recherche des modules existants     */
  558.            case 4:
  559.             flg_err=find_mod(my_gadg[GID_DIR].p_data);
  560.             break;
  561.  
  562.             /* initialisation de l'input ev */
  563.            case 3:
  564. #ifdef    WB_13
  565.             flg_err=init_input_ev();
  566. #endif
  567. #ifndef    WB_13
  568.             flg_err=init_input_ev(argc,argv);
  569. #endif
  570.             break;
  571.            case 5:
  572.  
  573.             /* chargement de la suite de la config du module aleatoire*/
  574.             load_alea_cfg();
  575.             /* We open the main window    */
  576.             big_ctx.t_g=my_gadg;
  577.             big_ctx.GList=0;
  578.             big_ctx.p_name=p_name;
  579.             if(nb_module!=0)my_gadg[GID_MODULES].d2=nb_module-1;
  580.             break;
  581.            case 6:
  582.  
  583.             /* Liste des modes dispo */
  584.             if((mode_liste=get_modes())==0){
  585.                 flg_err=TRUE;
  586.             }
  587.             data_proc.ml=mode_liste;
  588.             break;
  589.            }
  590.            etape++;
  591.     }
  592.     
  593.     if(flg_err==FALSE){
  594.  
  595.         /* Si il n'y a aucun module, on ouvre la fentre, pour que  */
  596.         /* l'utilisateur voi qq chose!                   */
  597.  
  598.         if(nb_module==0){
  599.             InitStuff_gadg(&big_ctx,TRUE);
  600.         }else{
  601.             if(my_gadg[GID_QUIET].value==FALSE){
  602.                 affiche_texte("SuperDark Installed...\nPress HotKey for options",NULL,TRUE);
  603.             }
  604.         }
  605.         main_loop(&big_ctx);
  606.         my_gadg[GID_MODULES].value=selection;
  607.         if(big_ctx.flg_save==TRUE){
  608.             SaveConfig(p_name,&my_gadg[GID_DEBUT]);
  609.             save_alea_cfg();
  610.         }
  611.     }
  612.     if(olddir!=-1)CurrentDir(olddir);
  613.     free_all();
  614.  
  615. }
  616.