home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 5 / FreshFish_July-August1994.bin / bbs / util / superdark-2.0a.lha / SuperDark-2.0a / programmer / prog / proc_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-24  |  10.6 KB  |  392 lines

  1. /****************************************************************/
  2. /*  File proc_main.c                        */
  3. /*                                */
  4. /*  For superdark project                    */
  5. /*                                */
  6. /* This file have to be linked with evry superdark module. The  */
  7. /* main function make various initialisation, test if superdark */
  8. /* is here, wait for a signal from superdark and then call the    */
  9. /* dark() function. Read the programmers.doc for more  info.    */
  10. /*                                */
  11. /*    @Thomas Landspurg 93                    */
  12. /*                                */
  13. /****************************************************************/
  14. /*  17/06/93 : v2.0  CPU timeout information added        */
  15. /*  20/06/93 : v2.1  SDwait() function added, and SDprintf()    */
  16. /*                   A 'could be a bug later' removed: sd watch.*/
  17. /*             can be stopped if the effect was not start.*/
  18. /*             before                    */
  19. /*                                */
  20. /****************************************************************/
  21.  
  22. #include    <stdio.h>
  23. #include    <exec/types.h>
  24. #include    <exec/tasks.h>
  25. #include    <exec/ports.h>
  26. #include    <graphics/displayinfo.h>
  27. #include    <exec/tasks.h>
  28. #include    <dos/dosextens.h>
  29.  
  30. #include    <proto/exec.h>
  31. #include    <proto/dos.h>
  32. #include    <proto/graphics.h>
  33. #include    <proto/intuition.h>
  34.  
  35. #include    "/includes/struct.h"
  36. #include    "/includes/tom_gadget.h"
  37.  
  38. #define    VERSION    2
  39.  
  40. WORD    flg_ctrl_c;
  41. struct    my_data    *p_info;        /* Private use                  */
  42. struct    appel_proc    *p_data_proc;    /* Communication structure with SDark */
  43. struct    Task    *my_t;
  44.  
  45. struct    Library    *IntuitionBase;
  46. struct    Library    *GfxBase;
  47.  
  48. /***********************************************************/
  49. /* These information SHOULD be give by the blanking module */
  50. /***********************************************************/
  51.  
  52. extern    struct    tom_gadget    my_gadg[]; /* Array of gadget for window configuration */
  53. extern    LONG    dark();            /* The main dark() function        */
  54. extern    void    proc_init();        /* The initialisation function        */
  55. extern    void    proc_end();        /* The end function            */
  56. extern    char    *p_text_info;        /* The text information, show in the    */
  57.                     /* info window                */
  58. void    wait_end();
  59. void    MyWrite(char    *pc);
  60.  
  61. UWORD    chip null_pointer[]={0,0,0,0,0,0};
  62. BPTR    hd=0;
  63. char    pr_buff[100];
  64.  
  65. /* Note: The folloing SPrintF Routine is taken from the program "FracBlank " */
  66. /* by:                                         */
  67. UWORD PutChar[2] = {0x16C0,0x4E75};
  68.  
  69. /* dirty hack to avoid assembler part :-)
  70.    16C0: move.b d0,(a3)+
  71.    4E75: rts
  72. */
  73.  
  74. #define VOID_STDARGS   void __stdargs
  75.  
  76. /************************************************************************/
  77. /* This exactly the same function than printf, but printf() can't be    */
  78. /* used in a blanker. Use the utility program show_info to see the result */
  79. /* NOTE: Always use long with number, like %ld,%lx and NOT %x or %d    */
  80. /************************************************************************/
  81.  
  82. VOID_STDARGS SDprintf(char *FormatString,...)
  83.  
  84. {
  85.     char    buffer[100];
  86.     if(hd){
  87.         RawDoFmt (FormatString,(APTR)((LONG *)&FormatString+1L),
  88.                (void *)PutChar,buffer);
  89.         MyWrite(buffer);
  90.     }
  91. }
  92.  
  93. /************************************************************************/
  94. /* Some variouse function used for debugging..use them at your own risk    */
  95. /* but do not use any printf function                    */
  96. /************************************************************************/
  97.  
  98. VOID_STDARGS SPrintF(char *pr_buff,char *FormatString,...)
  99.  
  100. {
  101.  RawDoFmt (FormatString,(APTR)((LONG *)&FormatString+1L),
  102.            (void *)PutChar,pr_buff);
  103. }
  104.  
  105. /************************************************************************/
  106.  
  107. mystrlen(char    *pc)
  108. {
  109.     int    ret;
  110.     ret=0;
  111.     while(*pc++!=0){
  112.         ret++;
  113.     }
  114.     return    ret;
  115. }
  116.  
  117. /************************************************************************/
  118.  
  119. void    MyWrite(char    *pc)
  120. {
  121.     if(hd!=0)Write(hd,pc,mystrlen(pc));
  122. }
  123. void    My_Print(struct RastPort *rp,UWORD x,UWORD y,char    *pc)
  124. {
  125.     Move(rp,x,y);
  126.     Text(rp,pc,mystrlen(pc));
  127. }
  128.  
  129. /****************************************************************/
  130. /* This old function should not be used now...            */
  131. /****************************************************************/
  132.  
  133. int    find_res(USHORT flg_depth,SHORT *p_depth,SHORT *p_tx,SHORT *p_ty,ULONG *p_d_id)
  134. {
  135.  struct ModeNode *ModeNode;
  136.  struct ModeNode *ModeNodeFound;
  137.  
  138.  ModeNode=(struct ModeNode *)p_data_proc->ml->lh_Head;
  139.  ModeNodeFound=ModeNode;
  140.  
  141.  while (ModeNode->mn_Node.ln_Succ)
  142.   {
  143.     if(flg_depth){
  144.         if(*p_depth<ModeNode->MaxDepth){
  145.             *p_depth=ModeNode->MaxDepth;
  146.             if(*p_tx<=ModeNode->MaxX){
  147.                 *p_tx=ModeNode->MaxX;
  148.                 if(*p_ty<=ModeNode->MaxY){
  149.                     *p_ty=ModeNode->MaxY;
  150.                 }
  151.             }
  152.             ModeNodeFound=ModeNode;
  153.         }
  154.     }else{
  155.         if(*p_tx<=ModeNode->MaxX){
  156.             *p_tx=ModeNode->MaxX;
  157.             if(*p_ty<=ModeNode->MaxY){
  158.                 *p_ty=ModeNode->MaxY;
  159.                 if(*p_depth<ModeNode->MaxDepth){
  160.                     *p_depth=ModeNode->MaxDepth;
  161.                 }
  162.             }
  163.             ModeNodeFound=ModeNode;
  164.         }
  165.     }
  166.     ModeNode=(struct ModeNode *)ModeNode->mn_Node.ln_Succ;
  167.   }
  168.   *p_depth=ModeNodeFound->MaxDepth;
  169.  
  170.   *p_tx=ModeNodeFound->MaxX;
  171.   *p_ty=ModeNodeFound->MaxY;
  172.  
  173.   *p_d_id=ModeNodeFound->DisplayID;
  174.  
  175.   return TRUE;
  176. }
  177.  
  178. /*******************************************************************/
  179. /* This function is used instead of CloseScreen, because if we are */
  180. /* in the random mode, we don't close immediatly the screen, but   */
  181. /* we give it back to superdark, so he can use it to create next   */
  182. /* screen                               */
  183. /*******************************************************************/
  184.  
  185. void    DCloseScreen(struct Screen *s)
  186. {
  187.     struct  Screen *st;
  188.  
  189.     st=p_data_proc->scr;
  190.     if(st!=NULL){
  191.         while(st->FirstWindow)CloseWindow(st->FirstWindow);
  192.         CloseScreen(st);
  193.     }
  194.  
  195.     if(p_info->flg_alea==TRUE){
  196.         p_data_proc->scr=s;
  197.     }else{
  198.         if(s){
  199.         while(s->FirstWindow)CloseWindow(s->FirstWindow);
  200.         CloseScreen(s);
  201.         p_data_proc->scr=NULL;
  202.        }
  203.     }
  204. }
  205.  
  206. /****************************************************************/
  207.  
  208. _main(line)
  209. register    char    *line;
  210. {
  211.     struct    MsgPort    *mp;
  212.     LONG    code_ret;
  213.     int        signal;
  214.  
  215.    /****************************************************************/
  216.    /* Look for the msg port wich contain informaion about superdark*/
  217.    /****************************************************************/
  218.  
  219.    IntuitionBase=(struct Library *)OpenLibrary("intuition.library",0);
  220.    GfxBase=(struct Library *)OpenLibrary("graphics.library",0);
  221.  
  222.    /* If superdark is not here, we start the blanker alone...Be careful! */
  223.    if((mp=FindPort("That's Dark!"))==0){
  224.         dark();
  225.    }else{
  226.     /*********************************************/
  227.     /* Give and get some info to/from SuperDark  */
  228.     /*********************************************/
  229.     p_data_proc=(struct appel_proc *)mp;
  230.     p_info=p_data_proc->p_info;
  231.     hd=p_data_proc->out_hd;
  232.     p_data_proc->p_gadg=my_gadg;
  233.     p_data_proc->p_texte=p_text_info;
  234.     p_data_proc->type_screen=SCR_OWN;/* By default, we create our own screen */
  235.                      /* can be changed in the proc_init fct  */
  236.     p_data_proc->enable_watchdog=TRUE;
  237.                     /* By default, CPU timeout (watchdog) is */
  238.                     /* enabled. Can be chaged in proc_init() */
  239.     p_data_proc->proc_main_ver=VERSION;
  240.                     /* proc_main version...            */
  241.     proc_init();            /* Call the program initialisation    */
  242.  
  243.     /* Note, this will be used by superdark to see if the module    */
  244.     /* is started                            */
  245.     if(p_data_proc->code_ret==-1){
  246.         p_data_proc->code_ret=DARK_WB_13;
  247.     }
  248.  
  249.     my_t=FindTask(0);
  250.     MyWrite("Avant le do\n");
  251.     SPrintF(pr_buff,"Tache mt:%lx\n",my_t);
  252.     MyWrite(pr_buff);
  253.  
  254.     do{
  255.  
  256.         /* We are waiting for a signal from superdark...        */
  257.         /* SIG_MAIN_DARK_DO_DARK mean start to blank screen    */
  258.         /* SIG_BREAKF_CTRL_C    mean stop the blank        */
  259.         /* SIG_MAIN_DARK_EXIT    mean exit from this module    */
  260.  
  261.         signal=Wait(SIG_MAIN_DARK_DO_DARK|SIG_MAIN_DARK_EXIT|SIGBREAKF_CTRL_C);
  262.         SDprintf("Signal:%lx\n",signal);
  263.         if((signal&SIG_MAIN_DARK_DO_DARK)!=0){
  264.             SDprintf("Signal dark...\n");
  265.             /* Clear old CTRL_C */
  266.             SetSignal(0,SIGBREAKF_CTRL_C);
  267.             flg_ctrl_c=FALSE;
  268.  
  269.             /* The main call to the dark() function  */
  270.             code_ret=dark();
  271.  
  272.             /* Note: this code ret is a new thing since V2.0 */
  273.             /* So try to put a return code in your dark()     */
  274.             /* function,0 if OK, Non zero if error         */
  275.             p_data_proc->code_ret=code_ret;
  276.  
  277.             SDprintf("Result:%ld\n",flg_ctrl_c);
  278.             /* If there was an error, wait for the end */
  279.             if(flg_ctrl_c==FALSE)wait_end();
  280.             wait_end();
  281.             MyWrite("ctrl-C receive,dark exited\n");
  282.  
  283.             /* We have to tell superdark that we have stopped */
  284.             /* the effect                      */
  285.  
  286.             Signal(my_t->tc_UserData,SIG_DARK_MAIN_STOPPED);
  287.         }
  288.         if(signal&SIGBREAKF_CTRL_C){
  289.             Signal(my_t->tc_UserData,SIG_DARK_MAIN_STOPPED);
  290.         }
  291.     }while((signal&SIG_MAIN_DARK_EXIT)==0);
  292.     proc_end();
  293.     SDprintf("Signal Exit..\n");
  294.      }
  295.     CloseLibrary(IntuitionBase);
  296.     CloseLibrary(GfxBase);
  297.     return 0;
  298. }
  299.  
  300. /****************************************/
  301. /* This function put a null pointer in    */
  302. /* in a window                */
  303. /****************************************/
  304.  
  305. void    ClearPtr(struct Window *win)
  306. {
  307.     SetPointer( win, null_pointer, 1, 1, 0, 0);
  308. }
  309.  
  310. /************************************************************/
  311. /* This function add a background window in a given screen, */
  312. /* and add a null pointer in this window.            */
  313. /************************************************************/
  314.  
  315. struct    Window    *AddWindow(struct Screen *s)
  316. {
  317.      struct Window *win;
  318.      win=OpenWindowTags(    NULL,
  319.                 WA_Left,    0,
  320.                 WA_Top,        0,
  321.                 WA_Width,    s->Width,
  322.                 WA_Height,    s->Height,
  323.                 WA_CustomScreen,s,
  324.                 WA_Flags,    WFLG_SIMPLE_REFRESH|WFLG_BACKDROP|WFLG_BORDERLESS|WFLG_ACTIVATE,
  325.                 TAG_DONE);
  326.     if(win){
  327.         ClearPtr(win);
  328.        }
  329.     return(win);
  330. }
  331.  
  332. /**************************************************/
  333. /* This function return TRUE if you have to stop, */
  334. /* FALSE elsewehre.                  */
  335. /**************************************************/
  336.  
  337. int    tst_end()
  338. {
  339.     ULONG    sig;
  340.  
  341.     
  342.     sig=SetSignal(0,0);
  343.     if(sig&SIGBREAKF_CTRL_C){
  344.         flg_ctrl_c=TRUE;
  345.         return TRUE;
  346.     }else{
  347.         if(sig&SIG_MAIN_DARK_WATCHDOG){
  348.             p_data_proc->flg_watchdog--;
  349.             SetSignal(0,SIG_MAIN_DARK_WATCHDOG);
  350.         }
  351.  
  352.         return FALSE;
  353.     }
  354. }
  355.  
  356. /****************************************************************/
  357. /* Like the exec Wait() function, but take care of the internal */
  358. /* superdark usage. If the return value is exactly 0, this mean */
  359. /* that you have to exit of the blanker                */
  360. /****************************************************************/
  361.  
  362. ULONG    SDwait(ULONG sigmask)
  363. {
  364.     ULONG    sig;
  365.  
  366.     /* If we have already a ctrl_c,  send a new ctrl_C, to prevent    */
  367.     /* a deadlock if dark() does not use correctly this fct        */
  368.  
  369.     if(flg_ctrl_c){
  370.         Signal(my_t,SIGBREAKF_CTRL_C);
  371.     }
  372.     do{
  373.         sig=Wait(SIGBREAKF_CTRL_C|SIG_MAIN_DARK_DO_DARK|SIG_MAIN_DARK_WATCHDOG|sigmask);
  374.         if(sig&SIG_MAIN_DARK_WATCHDOG){
  375.             p_data_proc->flg_watchdog--;
  376.         }
  377.     }while( (sig&(SIGBREAKF_CTRL_C|sigmask))==0);
  378.     if(sig&SIGBREAKF_CTRL_C){
  379.         flg_ctrl_c=TRUE;
  380.     }
  381.     return(sig&sigmask);
  382. }
  383.  
  384. /**********************************************************/
  385. /* This function only return, when superdark have to exit */
  386. /**********************************************************/
  387.  
  388. void    wait_end()
  389. {
  390.     SDwait(0);
  391. }
  392.