home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d564 / aload.lha / ALoad / src.lzh / src4 / aload.c < prev    next >
C/C++ Source or Header  |  1991-09-24  |  19KB  |  716 lines

  1. /* :ts=4 ***************************************************
  2.    ALoad
  3.  
  4.    (c) by Alexandru-Aurel Balmosan [aug 1991]
  5. ************************************************************/
  6.  
  7.  
  8.  
  9. #include "adef.h"
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdio.h>
  13. #include <ctype.h>
  14. #include <exec/tasks.h>
  15. #include <exec/memory.h>
  16. #include <hardware/custom.h>
  17. #include <hardware/intbits.h>
  18. #include <hardware/dmabits.h>
  19. #include <graphics/gfxbase.h>
  20. #include <graphics/gfxmacros.h>
  21. #include <graphics/layers.h>
  22. #include <workbench/icon.h>
  23. #include <workbench/workbench.h>
  24. #include <workbench/startup.h>
  25. #include <intuition/screens.h>     
  26. #include <intuition/gadgetclass.h> 
  27. #include <libraries/gadtools.h>
  28. #include <proto/intuition.h>
  29. #include <proto/graphics.h>
  30. #include <proto/exec.h>
  31. #include <proto/dos.h>
  32. #include <proto/icon.h>
  33. #include <proto/gadtools.h>
  34.  
  35.  
  36. long                                _BackGroundIO  =  0;          
  37. char                            *    _procname     =  "Aurel's ALoad v1.1c"; 
  38. long                                _priority      =  11;             
  39. long                                _old_pri       =  0;
  40. long                                _stack            =    4000;
  41.  
  42.  
  43. extern struct GfxBase           *    GfxBase;
  44. extern struct IntuitionBase     *    IntuitionBase;
  45. struct Library                   *    IconBase=NULL;
  46. struct Library                   *    GadToolsBase=NULL;
  47.  
  48. struct   Interrupt                    VBankIT;
  49. struct      MsgPort                *    port;
  50.  
  51. extern char                       *     _ProgramName;
  52. extern void                            XCEXIT(long);
  53.  
  54. struct WIN_DEF                        wd;
  55.  
  56. #define NG_1 (NGAry + 0)
  57. #define NG_2 (NGAry + 1)
  58. #define NG_3 (NGAry + 2)
  59. #define NG_4 (NGAry + 3)
  60. #define GAD_1 0
  61. #define GAD_2 1
  62. #define GAD_3 2
  63. #define GAD_4 3
  64.  
  65. struct Gadget             *            GList;
  66. struct VisualInfo         *            VisInfo;
  67.  
  68. struct TextAttr MyFont =
  69.     {    "topaz.font",
  70.         8,
  71.         FS_NORMAL,
  72.         FPF_ROMFONT
  73.     };
  74. struct NewGadget NGAry[4] = {
  75.     { 0,0,16,12,"S",NULL,GAD_1,PLACETEXT_IN,NULL},
  76.     { 0,0,16,12,"R",NULL,GAD_4,PLACETEXT_IN,NULL},
  77.     { 0,0,42,12,"Time",NULL,GAD_2,PLACETEXT_BELOW,NULL},
  78.     { 0,0,42,12,"Factor",NULL,GAD_3,PLACETEXT_BELOW,NULL}
  79. };
  80.  
  81. struct Gadget             *            Gad_1;
  82. struct Gadget             *            Gad_2;
  83. struct Gadget             *            Gad_3;
  84. struct Gadget             *            Gad_4;
  85. char                     *            Buf_2;
  86. char                     *            Buf_3;
  87. int                                 DefNumber_2 = 5*50;
  88. int                                    DefNumber_3 = 26;
  89.  
  90. int                                    GadSet=FALSE;
  91.  
  92. int                                 width, height, x, y, time, factor;
  93. int                                    size_abuffer=0,size_buffer=0;
  94.  
  95. #define    MASK_CURVE                    (0x1)
  96. #define    MASK_LEVEL                    (0x2)
  97. #define    LEVEL_COLOR                    (0x2)
  98. #define    BACK_FILL_COLOR                (0x0)
  99.  
  100. struct Gadget *
  101. InitGads(scr)
  102. struct Screen *scr;
  103. {
  104.     struct Gadget *gad,*CreateGadget(),*CreateContext();
  105.     struct VisualInfo         *GetVisualInfo();
  106.     short i;
  107.  
  108.  
  109.     GList = NULL;
  110.     if ((VisInfo = GetVisualInfo(scr, TAG_END)) == NULL) return(NULL);
  111.  
  112.     {     struct NewGadget *ng;
  113.         for (i = 0, ng = NGAry; i < sizeof(NGAry)/sizeof(NGAry[0]); ++i, ++ng) 
  114.         {     ng->ng_VisualInfo = (APTR)VisInfo;
  115.             ng->ng_TextAttr = &MyFont;
  116.         }
  117.     }
  118.     if ((gad = CreateContext(&GList)) == NULL) return(NULL);
  119.     GadSet = TRUE;
  120.     Gad_1 = gad = CreateGadget(BUTTON_KIND, gad, NGAry + 0,
  121.     TAG_END
  122.     );
  123.     if (gad == NULL)
  124.     return(NULL);
  125.  
  126.     Gad_2 = gad = CreateGadget(BUTTON_KIND, gad, NGAry + 1,
  127.     TAG_END
  128.     );
  129.     if (gad == NULL)
  130.     return(NULL);
  131.  
  132.     Gad_3 = gad = CreateGadget(INTEGER_KIND, gad, NGAry + 2,
  133.     GTIN_Number, DefNumber_2,
  134.     TAG_END
  135.     );
  136.     if (gad == NULL)
  137.     return(NULL);
  138.     Buf_2 = ((struct StringInfo *)gad->SpecialInfo)->Buffer;
  139.  
  140.     Gad_4 = gad = CreateGadget(INTEGER_KIND, gad, NGAry + 3,
  141.     GTIN_Number, DefNumber_3,
  142.     TAG_END
  143.     );
  144.     if (gad == NULL)
  145.     return(NULL);
  146.     Buf_3 = ((struct StringInfo *)gad->SpecialInfo)->Buffer;
  147.  
  148.     return(GList);
  149. }
  150.  
  151. void
  152. FreeGads()
  153. {
  154.     if (GList)
  155.         FreeGadgets(GList);
  156.     if (VisInfo)
  157.         FreeVisualInfo(VisInfo);
  158.     VisInfo = NULL;
  159.     GList = NULL;
  160.     GadSet=FALSE;
  161. }
  162.  
  163. #define malloc(s) AllocMem(size_abuffer=(s),MEMF_PUBLIC)
  164. #define calloc(s,w) AllocMem(size_buffer=((s)*(w)),MEMF_PUBLIC | MEMF_CLEAR)
  165.  
  166. void  exit_all(er)
  167. int      er;
  168. {  
  169.       Forbid();
  170.       (void)SetFunction((struct Library *)IntuitionBase, -78,  (APTR)GetOldCloseWB());
  171.       (void)SetFunction((struct Library *)IntuitionBase, -210, (APTR)GetOldOpenWB());
  172.       Permit();
  173.  
  174.    if (er >= 0)      RemIntServer(INTB_VERTB, &VBankIT);
  175.    if (wd.raster)    FreeRaster((PLANEPTR)wd.raster,wd.width,wd.height);
  176.    if (wd.abuffer)   FreeMem((char *)wd.abuffer,size_abuffer);
  177.    if (wd.buffer)    FreeMem((char *)wd.buffer,size_buffer);
  178.    if (wd.win)       CloseWindow(wd.win);    
  179.    if (GadSet)         FreeGads();
  180.    if (GadToolsBase) CloseLibrary((struct Library *)GadToolsBase);
  181.    if (IconBase)     CloseLibrary((struct Library *)IconBase);
  182.    if (IntuitionBase)CloseLibrary((struct Library *)IntuitionBase);
  183.    if (GfxBase)      CloseLibrary((struct Library *)GfxBase);
  184.    SetTaskPri(FindTask(0),_old_pri);
  185.    XCEXIT(er);
  186. }
  187.  
  188. void    CloseWin(wd)
  189. register struct WIN_DEF    *wd;
  190. {    Signal(wd->owner,wd->signal_close);
  191.     while (wd->ss_rast!=2) ;
  192. }
  193.  
  194. void    OpenWin(wd)
  195. register struct WIN_DEF    *wd;
  196. {    if (wd->ss_rast == 2)
  197.     {    Signal(wd->owner, wd->signal_open);
  198.         while(wd->ss_rast == 2);
  199.     }
  200. }
  201.  
  202. struct Window    *OpenDisplayWindow(x,y,width,height,name,size)
  203. int        x, y, width, height;
  204. char    *name;
  205. int        size;
  206. {    register struct Window    *win;
  207.     TitlePtr = name;
  208.     SizeFlag = size;
  209.     WIN_XPOS = x;
  210.     WIN_YPOS = y;
  211.     WIN_WIDTH = width;
  212.     WIN_HEIGHT= height;
  213.     IDCMPFlags = IDCMP_CLOSEWINDOW ;
  214.     if (size)
  215.         IDCMPFlags |= BUTTONIDCMP|IDCMP_REFRESHWINDOW;
  216.  
  217.     win = (struct Window *)OpenWindowTagList(NULL, WinTagList);
  218.     if (win != NULL)
  219.     {    win->RPort->AreaInfo = &wd.ainfo;
  220.         win->RPort->TmpRas   = &wd.tmpras;
  221.         SetDrMd(win->RPort, JAM1);
  222.     }
  223.     return (win);
  224. }
  225.  
  226. int        NewPrefs()
  227. {    struct Window            *win;
  228.     struct DiskObject        *di;
  229.     char                    **ToolArray;
  230.     static char                wstr[80],tstr[40],fstr[40];
  231.     int                        not_done=1,cont=1,x,i;
  232.     struct IntuiMessage        *im;
  233.     struct Gadget            *Gad;
  234.     char                    *c,**s,*w,*ti,*fa,**p_wd,**p_ti,**p_fa;
  235.  
  236.     width = wd.width;
  237.     height = wd.height;
  238.     win = OpenDisplayWindow(wd.x,wd.y,wd.width,wd.height,"Set new preferences",TRUE);
  239.  
  240.     if (win)
  241.     {    x=win->BorderLeft;
  242.         for (i=0;i<4;i++)
  243.         {    NGAry[i].ng_TopEdge = win->BorderTop;
  244.             NGAry[i].ng_LeftEdge=x;
  245.             x += NGAry[i].ng_Width;
  246.         }
  247.         DefNumber_2 = wd.End_CNT;
  248.         DefNumber_3 = wd.factor;
  249.         if (InitGads(win->WScreen))
  250.         {    AddGList(win,GList,0,-1,NULL);
  251.             RefreshGList(GList, win, NULL, -1);
  252.             GT_RefreshWindow(win,NULL);
  253.             RefreshWindowFrame(win);
  254.             while (not_done)
  255.             {    WaitPort(win->UserPort);
  256.                 while (im = (struct IntuiMessage *)GT_GetIMsg(win->UserPort))
  257.                 {    switch (im->Class)
  258.                     {    case IDCMP_REFRESHWINDOW:
  259.                             GT_BeginRefresh(win);    
  260.                             GT_EndRefresh(win, TRUE);
  261.                             break;
  262.                         case IDCMP_CLOSEWINDOW:
  263.                             not_done=0;
  264.                             cont=0;
  265.                             break;
  266.                         case BUTTONIDCMP:
  267.                             Gad = (struct Gadget *)im->IAddress;
  268.                             wd.x = win->LeftEdge;
  269.                             wd.y = win->TopEdge;
  270.                             wd.width = win->Width;
  271.                             wd.height = win->Height;
  272.                             stcd_i(Buf_3,&factor);
  273.                             if (factor <1 || factor >31) factor = 26;
  274.                             wd.factor = factor;
  275.                             stcd_i(Buf_2,&time);
  276.                             wd.End_CNT = time;
  277.                             switch(Gad->GadgetID)
  278.                             {    case GAD_1:
  279.                                        if (di = (struct DiskObject *) GetDiskObject(_ProgramName))
  280.                                        {    ToolArray = di->do_ToolTypes;
  281.                                           c=(char *)FindToolType(ToolArray,"WINDOW");
  282.                                           if (c)
  283.                                         {    s = ToolArray;
  284.                                             while (*s != 0 && 
  285.                                                 (c<*s  || c> (*s + strlen(*s)))) s++;
  286.                                             w = *s;
  287.                                             p_wd = s;
  288.                                             strcpy(wstr,"WINDOW=");
  289.                                             c = wstr + strlen("WINDOW=");
  290.                                             c += stci_d(c,wd.x); *c++ = '/';
  291.                                             c += stci_d(c,wd.y); *c++ = '/';
  292.                                             c += stci_d(c,wd.width); *c++ = '/';
  293.                                             (void) stci_d(c,wd.height);
  294.                                             *s = wstr;
  295.                                         }
  296.                                         else  w = NULL;
  297.                                         c = (char *)FindToolType(ToolArray,"TIME");
  298.                                         if (c)
  299.                                         {   s = ToolArray;                           
  300.                                             while (*s != 0 &&                        
  301.                                                     (c<*s  || c> (*s + strlen(*s)))) s++;
  302.                                             ti = *s;
  303.                                             p_ti = s;
  304.                                             strcpy(tstr,"TIME=");
  305.                                             (void) stci_d(tstr + strlen("TIME="),wd.End_CNT);
  306.                                             *s = tstr;
  307.                                         }
  308.                                         else ti = NULL;
  309.                                         c = (char *)FindToolType(ToolArray,"FACTOR");
  310.                                         if (c)
  311.                                         {   s = ToolArray;                           
  312.                                             while (*s != 0 &&                        
  313.                                                     (c<*s  || c> (*s + strlen(*s)))) s++;
  314.                                             fa = *s;
  315.                                             p_fa = s;
  316.                                             strcpy(fstr,"FACTOR=");
  317.                                             (void) stci_d(fstr + strlen("FACTOR="),wd.factor);
  318.                                             *s = fstr;
  319.                                         }
  320.                                         else fa = NULL;
  321.                                         PutDiskObject(_ProgramName,di);
  322.                                         if (w) {    *p_wd = w; w = 0; }
  323.                                         if (ti) {    *p_ti = ti; ti = 0; }
  324.                                         if (fa) {    *p_fa = fa; fa = 0; }
  325.                                         FreeDiskObject(di);
  326.                                     }
  327.                                     break;
  328.                                 case GAD_4:
  329.                                     not_done=0;
  330.                                     cont=1;
  331.                                 default: break;
  332.                             }
  333.                         default:    break;
  334.                     }
  335.                     GT_ReplyIMsg(im);
  336.                 }
  337.             }
  338.         }    
  339.         CloseWindow(win);
  340.         FreeGads();
  341.     }
  342.     return cont;
  343. }
  344.  
  345. void    set_values()
  346. {
  347.       wd.xstart   =  wd.win->BorderLeft;                      
  348.       wd.ystart   =  wd.height - wd.win->BorderBottom-1;
  349.       wd.dy       =  wd.ystart - wd.win->BorderTop;
  350.       wd.dx    =  wd.width - wd.xstart - wd.win->BorderRight;
  351.       if (wd.dx <=0) wd.dx = 1;
  352.       if (wd.dy <=0) wd.dy = 1;
  353.       wd.buffer_size = wd.dx;
  354. }
  355.  
  356. void  init_all() 
  357. {  struct  DiskObject   *di;
  358.    char     **ToolArray;
  359.    char     *c;
  360.  
  361.    memset((char *)&wd,0,sizeof(wd));
  362.    memset((char *)&VBankIT, 0, sizeof(VBankIT));
  363.    GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",36);
  364.    if (GfxBase == NULL) exit_all(-100);
  365.    IntuitionBase = (struct IntuitionBase *) 
  366.                OpenLibrary("intuition.library",36);
  367.    if (IntuitionBase == NULL) exit_all(-99);
  368.    IconBase = (struct Library *)OpenLibrary("icon.library",36);
  369.    if (IconBase == NULL) exit_all(-98);
  370.    GadToolsBase = (struct Library *)OpenLibrary("gadtools.library",36);
  371.    if (GadToolsBase == NULL) exit_all(-97);
  372.  
  373.    if (di = (struct DiskObject *) GetDiskObject(_ProgramName))
  374.    {  ToolArray = di->do_ToolTypes;
  375.       c=(char *)FindToolType(ToolArray,"WINDOW");
  376.       if (c != 0)
  377.       {  c += stcd_i(c,&x);
  378.          if (*c++ == '/' )
  379.          {  c += stcd_i(c,&y);
  380.             if (*c++ == '/')
  381.             {  c += stcd_i(c, &width);
  382.                if (*c++ == '/') (void) stcd_i(c, &height);
  383.                else goto a;
  384.             } else goto a;
  385.          } else goto a;
  386.       }
  387.       else
  388.       {  
  389. a:       x=y=0;
  390.          height = 64;
  391.          width  = 256;
  392.       }
  393.  
  394.       c = (char *)FindToolType(ToolArray,"TIME");
  395.       if (c != 0)
  396.       {  stcd_i(c,&time);
  397.          if (time <=0 ) time = 5*50;
  398.       }
  399.       else  time = 5*50;
  400.  
  401.       c = (char *)FindToolType(ToolArray,"FACTOR");
  402.       if (c != 0)
  403.       { stcd_i(c, &factor);
  404.         if (factor <=0 || factor >31) factor = 26;
  405.       }
  406.       else    factor = 26;
  407.       wd.factor = factor;
  408.  
  409.  
  410.       FreeDiskObject(di);
  411.     }
  412.     else    
  413.     {    x=y=0;       
  414.         height = 64; 
  415.         width  = 256;
  416.         time = 5*50;
  417.         wd.factor = 26;
  418.     }
  419.     SetBase(&wd);
  420.     wd.owner = (struct Task *)FindTask(0);
  421.     wd.signal= 1L << AllocSignal(-1);
  422.     wd.signal_open= 1L << AllocSignal(-1);
  423.     wd.signal_close= 1L << AllocSignal(-1);
  424.     _old_pri=SetTaskPri(wd.owner, _priority);
  425.     wd.cnt = wd.End_CNT = time;
  426.     wd.win = OpenDisplayWindow(x,y,width,height,_procname,0);
  427.     if (wd.win == NULL) exit_all(-50);
  428.     wd.width = width;
  429.     wd.height = height;
  430.     wd.x = x;
  431.     wd.y = y;
  432.     wd.abuffer = (APTR) malloc((width+3)*5);
  433.     if (wd.abuffer == NULL) exit_all(-49);
  434.     wd.raster = (APTR) AllocRaster(width,height);
  435.     if (wd.raster == NULL) exit_all(-48);
  436.     InitArea(&wd.ainfo,(short *)wd.abuffer,(long)width);
  437.     InitTmpRas(&wd.tmpras,(PLANEPTR)wd.raster,RASSIZE(width,height));
  438.     set_values();
  439.     if ( (wd.buffer   = (long *) calloc(wd.dx,4)) == NULL) exit_all(-47);
  440.     wd.buffer_end = wd.buffer + (wd.dx-1);
  441.  
  442.       VBankIT.is_Node.ln_Type = NT_INTERRUPT;         
  443.       VBankIT.is_Node.ln_Pri  = 126;                  
  444.       VBankIT.is_Node.ln_Name = "ALoad V1.03a VBlank Sumer"; 
  445.       VBankIT.is_Data         = (APTR) &wd;
  446.       VBankIT.is_Code         = VertBServer;
  447.       AddIntServer(INTB_VERTB, &VBankIT);
  448.       wd.open = OpenWin;
  449.       wd.close = CloseWin;
  450.       wd.ss_rast = 0;
  451.       Forbid();
  452.       SetOldCloseWB(SetFunction((struct Library *)IntuitionBase, -78, NewClose));
  453.       SetOldOpenWB(SetFunction((struct Library *)IntuitionBase, -210, NewOpen));
  454.       Permit();
  455. }
  456.  
  457.  
  458. #define FreeRasterSem(ww)  (ww)->ss_rast=0
  459.  
  460. int      GetRasterSem(wd)
  461. struct WIN_DEF *wd;
  462. {  register int   value;
  463.  
  464.    if ((value=GfxBase->BlitLock) && (wd->ss_rast==0) )
  465.    {  wd->ss_rast = 1;
  466.       if (wd->win->WScreen->LayerInfo.Lock.ss_QueueCount>=0)
  467.       {  value = 0;
  468.          FreeRasterSem(wd);
  469.       }
  470.    }
  471.    else value = 0;
  472.    return value;
  473. }
  474.  
  475.  
  476. void  ReDraw(scale,start)
  477. register short        scale;
  478. short               start;
  479. {  register struct RastPort       *RP=wd.win->RPort;
  480.    register short                 delta=wd.dy-1;
  481.    register short                 rb,i=wd.xstart+start,h;
  482.    short                           ybase = wd.ystart;
  483.    short                           pos = wd.b_pos-2-start;
  484.    short                        cnt;
  485.    register long                  *ptr = wd.buffer + ((wd.pos+start)%wd.dx);
  486.  
  487. #define        RB()          CalcShift(delta*(*ptr)/scale)
  488. #define        NEXT()        if ((++ptr)>wd.buffer_end) ptr=wd.buffer
  489.  
  490.     wd.s_rast++;
  491.     AreaMove(RP, i, ybase);
  492.     rb = RB(); NEXT();
  493.     if (rb>delta) rb = delta;
  494.     h = rb;
  495.     AreaDraw(RP, i, ybase - rb);
  496.     cnt=0;
  497.  
  498.     for(i++;pos>0;i++,pos--)
  499.     {    rb = RB(); NEXT();
  500.         if (rb>delta) rb = delta;
  501.         if (h != rb)
  502.         {    if (cnt) AreaDraw(RP, i-1, ybase - h);
  503.             h = rb; cnt=0;
  504.             AreaDraw(RP, i, ybase - rb);
  505.         }
  506.         else cnt++;
  507.     }
  508.     rb = RB();
  509.     if (rb>delta) rb = delta;
  510.     if (h != rb && cnt) AreaDraw(RP, i-1, ybase - h);
  511.     AreaDraw(RP, i, ybase - rb);
  512.     AreaDraw(RP, i, ybase);
  513.     wd.s_rast--;
  514.  
  515.     if (wd.scale != scale)
  516.     {    if (scale < wd.dy)
  517.         {     SetWrMsk(RP,MASK_CURVE | MASK_LEVEL);
  518.             SetAPen(RP, BACK_FILL_COLOR);
  519.             RectFill(RP, wd.xstart+start, ybase-delta, h=wd.xstart+wd.dx-1, ybase);
  520.  
  521.             SetWrMsk(RP, MASK_LEVEL);
  522.             SetAPen(RP, LEVEL_COLOR);
  523.             for(i=scale-1; i>0; i--)
  524.             {    rb = delta * i / scale;
  525.                 Move(RP, h, ybase - rb);
  526.                 Draw(RP, wd.xstart+start, ybase - rb);
  527.             }
  528.         }
  529.         else
  530.         {    SetWrMsk(RP, MASK_CURVE | MASK_LEVEL);
  531.             SetAPen(RP, LEVEL_COLOR);
  532.             RectFill(RP, wd.xstart+start, ybase-delta, wd.xstart+wd.dx-1, ybase);
  533.         }
  534.     }
  535.     wd.scale = scale;
  536.     SetWrMsk(RP, MASK_CURVE);
  537.     SetAPen(RP, MASK_CURVE);
  538.     AreaEnd(RP);
  539. }
  540.  
  541. void Update(wd)
  542. register struct WIN_DEF *wd;
  543. {      register short dx = wd->dx;
  544.     register long    h;
  545.  
  546.     if (wd->Ready_CNT < ((1<<15)-1))
  547.         h = (wd->Ready_CNT<<16);
  548.     else
  549.         h = (1<<31)-1;
  550.     if (wd->Count_CNT>0) h /= wd->Count_CNT;
  551.     wd->Ready_CNT = wd->Count_CNT = 0;
  552.     if ( (wd->value = h - (((h - wd->value)*wd->factor)>>5)-1) <0) wd->value=0;
  553.        if ( wd->b_pos<dx ) 
  554.        {      wd->buffer[wd->b_pos] = wd->value;
  555.           wd->b_pos++;
  556.        }
  557.        else 
  558.        {      wd->buffer[(wd->pos+dx)%dx] =  wd->value ;
  559.           wd->pos++;
  560.        }
  561.        Signal(wd->owner,wd->signal);
  562. }
  563.  
  564. void  DrawPeak(rp,i)
  565. register struct RastPort    *rp;
  566. register int    i;
  567. {    if (i>(wd.dy-1)) i=wd.dy-1;
  568.     SetWrMsk(rp,MASK_CURVE);
  569.     SetAPen(rp,MASK_CURVE);
  570.     Move(rp, wd.xstart + wd.b_pos-1, wd.ystart - i);
  571.     Draw(rp, wd.xstart + wd.b_pos-1, wd.ystart);
  572. }
  573.  
  574. void _main()
  575. {    int                            old_pos, old_bpos,pos,bpos;
  576.     register unsigned long       h;
  577.     long                        value;
  578.     register short                 scale, maxscale;
  579.     register struct    RastPort   *RP;
  580.     struct Message                 *msg;
  581.  
  582.     init_all();
  583.     old_pos      =  0;
  584.     old_bpos        =  1;
  585.     wd.scale     =  1;
  586.     scale        =  1;
  587.     wd.s_int     =  1;
  588.  
  589.     RP = wd.win->RPort;
  590.     SetWrMsk(RP,MASK_CURVE | MASK_LEVEL);
  591.     SetAPen(RP, BACK_FILL_COLOR);
  592.     RectFill(RP, wd.xstart, wd.ystart - wd.dy, wd.xstart+wd.dx-1, wd.ystart);
  593.  
  594.     for (;;)
  595.     {    if (wd.win) h = 1 << wd.win->UserPort->mp_SigBit;
  596.           else h = 0;
  597.           h = Wait( h | wd.signal | wd.signal_open | wd.signal_close);
  598.           if (h & wd.signal_close && wd.win )
  599.           {    wd.width = wd.win->Width;
  600.             wd.height= wd.win->Height;
  601.             wd.x     = wd.win->LeftEdge;
  602.             wd.y     = wd.win->TopEdge;
  603.             CloseWindow(wd.win);
  604.             wd.win = NULL;
  605.             wd.ss_rast = 2;
  606.           }
  607.         if (wd.win == NULL && h & wd.signal_open)
  608.         {    wd.win = OpenDisplayWindow(wd.x,wd.y,wd.width,wd.height,_procname,0);
  609.             if (wd.win != NULL) 
  610.             {    FreeRasterSem(&wd);
  611.                 RP = wd.win->RPort;
  612.                 SetWrMsk(RP,MASK_CURVE | MASK_LEVEL);
  613.                 SetAPen(RP, BACK_FILL_COLOR);
  614.                 RectFill(RP, wd.xstart, wd.ystart - wd.dy, wd.xstart+wd.dx-1, wd.ystart);
  615.                 wd.s_rast++;
  616.                 scale = CalcShift( FindMaxValue(&wd))+1;
  617.                 wd.scale = 1;
  618.                 wd.s_rast--;
  619.                 ReDraw(scale,0);
  620.             }
  621.         }
  622.         if (wd.win && (h & (1<< wd.win->UserPort->mp_SigBit)))
  623.         {    msg = (struct Message *)GetMsg(wd.win->UserPort);
  624.             ReplyMsg(msg);
  625.               wd.width = wd.win->Width;
  626.             wd.height= wd.win->Height;
  627.             wd.x     = wd.win->LeftEdge;
  628.             wd.y     = wd.win->TopEdge;
  629.             wd.s_int=0;
  630.             wd.ss_rast = 1;
  631.             wd.s_rast++;
  632.             CloseWindow(wd.win);
  633.              wd.win = NULL;
  634.             if (NewPrefs())
  635.             {    FreeMem((char *)wd.buffer,size_buffer);
  636.                 FreeMem((char *)wd.abuffer,size_abuffer);
  637.                 wd.abuffer = (APTR) malloc((wd.width+3)*5);
  638.                 if (wd.abuffer == NULL) exit_all(-49);
  639.                 FreeRaster((PLANEPTR)wd.raster,width,height);
  640.                 wd.raster = (APTR) AllocRaster(wd.width,wd.height);
  641.                 if (wd.raster == NULL) exit_all(-48);
  642.                 InitArea(&wd.ainfo,(short *)wd.abuffer,(long)wd.width);
  643.                 InitTmpRas(&wd.tmpras,(PLANEPTR)wd.raster,RASSIZE(wd.width,wd.height));
  644.                 wd.win = OpenDisplayWindow(wd.x,wd.y,wd.width,wd.height,_procname,0);
  645.                 if (wd.win != NULL)                                                   
  646.                 {    set_values();
  647.                     if ( (wd.buffer   = (long *) calloc(wd.dx,4)) == NULL) 
  648.                     {    
  649.                         exit_all(-47);
  650.                     }
  651.                     wd.buffer_end = wd.buffer + (wd.dx-1);
  652.                     wd.b_pos = wd.pos = 0;
  653.                     wd.scale = -1;
  654.                     wd.value = 0;
  655.                     old_bpos = 1;
  656.                     old_pos = 0;
  657.                     scale = 1;
  658.                     RP = wd.win->RPort;
  659.                     SetWrMsk(RP,MASK_CURVE | MASK_LEVEL);
  660.                     SetAPen(RP, BACK_FILL_COLOR);
  661.                     RectFill(RP, wd.xstart, wd.ystart - wd.dy, wd.xstart+wd.dx-1, wd.ystart);
  662.                 }
  663.             }
  664.             else exit_all(0);
  665.             wd.s_rast--;
  666.             wd.ss_rast = 0;
  667.             wd.s_int=1;
  668.         }
  669.         if (wd.win && (h & wd.signal) && GetRasterSem(&wd))
  670.         {    RP = wd.win->RPort;
  671.             wd.s_rast++;
  672.             bpos = wd.b_pos;
  673.             pos = wd.pos;
  674.             value = wd.value;
  675.             maxscale = CalcShift( FindMaxValue(&wd))+1;
  676.             wd.s_rast--;
  677.             if (bpos < wd.dx)
  678.             {    scale    =  CalcShift(value)+1;
  679.                 if (scale > wd.scale) ReDraw(scale,0);
  680.                 else
  681.                 {    h = bpos - old_bpos+1;
  682.                     if (h==1) 
  683.                         DrawPeak(RP,CalcShift((wd.dy-1)*value/wd.scale));
  684.                     else
  685.                     {    if (maxscale > wd.scale) 
  686.                             ReDraw(maxscale, 0);
  687.                         else
  688.                             ReDraw(wd.scale, old_bpos-1);
  689.                     }
  690.                 }
  691.             }
  692.             else
  693.             {    h = pos - old_pos;
  694.                 if (maxscale != wd.scale) 
  695.                     ReDraw(maxscale, 0);
  696.                 else  
  697.                 {    if (h >= wd.dx  ||  old_bpos < bpos)  
  698.                         ReDraw(maxscale, 0);
  699.                     else  
  700.                     {    SetWrMsk(RP, MASK_CURVE);
  701.                         ScrollRaster(RP, h, 0, wd.xstart, wd.ystart - wd.dy + 1, 
  702.                                     wd.xstart + wd.dx -1, wd.ystart);
  703.                         if (h>1) 
  704.                             ReDraw(maxscale, wd.dx-h);
  705.                         else
  706.                             DrawPeak(RP,CalcShift((wd.dy-1)*value/wd.scale));
  707.                     }
  708.                 }
  709.             }
  710.             old_bpos = bpos+1;
  711.             old_pos = pos;
  712.             FreeRasterSem(&wd);
  713.         }
  714.     }
  715. }
  716.