home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff222.lzh / MemGauge / MemGauge.c < prev    next >
C/C++ Source or Header  |  1989-06-19  |  10KB  |  544 lines

  1. /* MemGauge.c ***************************************************************
  2. *
  3. *    MemGauge - The main program handling initializations and
  4. *               display.
  5. *
  6. *    Author...  Olaf 'Olsen' Barthel, ED Electronic Design Hannover
  7. *               Brabeckstrasse 35
  8. *               D-3000 Hannover 71
  9. *
  10. *               Federal Republic of Germany
  11. *
  12. *****************************************************************************
  13. *
  14. *    MemGauge can be invoked via Workbench or from CLI. If you
  15. *    chose the latter you may add up to three arguments defining
  16. *    the location the window will be opened at.
  17. *
  18. *    For example: MemGauge 624 11 179
  19. *
  20. ****************************************************************************/
  21.  
  22. #include <intuition/intuitionbase.h>
  23. #include <graphics/gfxbase.h>
  24. #include <exec/execbase.h>
  25. #include <exec/memory.h>
  26.  
  27.     /* This will send a message to a DOS-stream. */
  28.  
  29. #define Send(Stream,String)    Write(Stream,String,strlen(String))
  30.  
  31.     /* Some forward declarations. */
  32.  
  33. extern struct Library    *OpenLibrary();
  34. extern struct Window    *OpenWindow();
  35. extern struct MenuItem    *ItemAddress();
  36. extern struct Message    *GetMsg();
  37. extern ULONG        AvailMem();
  38.  
  39.     /* Timer device control. */
  40.  
  41. extern void        CloseTimerDevice();
  42. extern BOOL        OpenTimerDevice();
  43. extern void        WaitTime();
  44.  
  45.     /* The following variables are imported from the various
  46.      * link-modules.
  47.      */
  48.  
  49. extern struct ExecBase    *SysBase;
  50. extern struct Menu    GaugeMenu;
  51. extern struct IntuiText    AboutTxt[];
  52. extern struct IntuiText    Proceed;
  53.  
  54.     /* The menunumber. */
  55.  
  56. #define GAUGE 0
  57.  
  58.     /* How often will we check the window for incoming
  59.      * messages?
  60.      */
  61.  
  62. #define TIMEFRAG 8
  63.  
  64.     /* Some libraries... */
  65.  
  66. struct IntuitionBase    *IntuitionBase;
  67. struct GfxBase        *GfxBase;
  68.  
  69.     /* Window and related structures. */
  70.  
  71. struct Window        *Window;
  72. struct IntuiMessage    *Massage;
  73. struct RastPort        *RPort;
  74.  
  75.     /* This is what we will get from the window. */
  76.  
  77. ULONG  Class;
  78. USHORT Code;
  79.  
  80.     /* To replace the dragbar and to add some comfort
  81.      * we use this gadget to allow the user to drag the
  82.      * window around at any location.
  83.      */
  84.  
  85. struct Gadget DragGadget =
  86. {
  87.     (struct Gadget *)NULL,
  88.     0,0,
  89.     16,512,
  90.     GADGHNONE,
  91.     GADGIMMEDIATE,
  92.     WDRAGGING,
  93.     (APTR)NULL,
  94.     (APTR)NULL,
  95.     (struct IntuiText *)NULL,
  96.     0,
  97.     (APTR)NULL,
  98.     0,
  99.     (APTR)NULL
  100. };
  101.  
  102.     /* We will open this window. */
  103.  
  104. struct NewWindow NewWindow =
  105. {
  106.     0,0,
  107.     16,0,
  108.     2,1,
  109.     REFRESHWINDOW | NEWSIZE | ACTIVEWINDOW | INACTIVEWINDOW | MENUPICK,
  110.     WINDOWSIZING,
  111.     (struct Gadget *)&DragGadget,
  112.     (struct Image  *)NULL,
  113.     (UBYTE *)NULL,
  114.     (struct Screen *)NULL,
  115.     (struct BitMap *)NULL,
  116.     16,64,
  117.     16,512,
  118.     WBENCHSCREEN
  119. };
  120.     /* Length of the block to draw. */
  121.  
  122. short BarLength = 0;
  123.  
  124.     /* Available memory and used memory. */
  125.  
  126. ULONG MaxMem,CurMem;
  127.  
  128.     /* Bitmapdata for the "E" (Empty) symbol. */
  129.  
  130. USHORT EptyData[16] =
  131. {
  132.     /* Plane 0 */
  133.  
  134.     0xC003,0xC7E3,0xC603,0xC783,
  135.     0xC603,0xC7E3,0xC003,0xFFFF,
  136.  
  137.     /* Plane 1 */
  138.  
  139.     0x3FFC,0x381C,0x39FC,0x387C,
  140.     0x39FC,0x381C,0x3FFC,0x0000
  141. };
  142.  
  143.     /* Bitmapdata for the"F" (Full) symbol. */
  144.  
  145. USHORT FullData[16] =
  146. {
  147.     /* Plane 0 */
  148.  
  149.     0xFFFF,0xC003,0xC7E3,0xC603,
  150.     0xC783,0xC603,0xC603,0xC003,
  151.  
  152.     /* Plane 1 */
  153.  
  154.     0x0000,0x3FFC,0x381C,0x39FC,
  155.     0x387C,0x39FC,0x39FC,0x3FFC
  156. };
  157.  
  158.     /* Imagedefinitions for "Empty". */
  159.  
  160. struct Image EptyImage =
  161. {
  162.     0,0,
  163.     16,8,2,
  164.     (USHORT *)&EptyData,
  165.     0x03,0x00,
  166.     (struct Image *)NULL
  167. };
  168.  
  169.     /* Imagedefinitions for "Full". */
  170.  
  171. struct Image FullImage =
  172. {
  173.     0,0,
  174.     16,8,2,
  175.     (USHORT *)&FullData,
  176.     0x03,0x00,
  177.     (struct Image *)NULL
  178. };
  179.  
  180.     /* Some definitions for the detach functions supported
  181.      * by Aztec and Lattice. Lattice needs some more data to
  182.      * send a text to stdout, that's why _Backstdout is part
  183.      * of this code.
  184.      */
  185.  
  186. #ifndef AZTEC_C
  187. extern long _Backstdout;
  188. #endif AZTEC_C
  189.  
  190. long  _stack        = 4000;
  191. long  _priority     = 0;
  192. char *_procname     = "MemGauge v1.4";
  193.  
  194.     /* You will have to know if you need it: output to the
  195.      * current CLI console. If you are using Aztec C
  196.      * this will prevent you from closing the CLI window
  197.      * MemGauge was started from. Lattice is a bit more
  198.      * tolerant since you will have to close stdout on your
  199.      * own. To avoid conflicts this variable (_BackGroundIO)
  200.      * has been set to 0.
  201.      */
  202.  
  203. long  _BackGroundIO = 0;
  204.  
  205.     /* CloseAll(ReturnCode) :
  206.      *
  207.      *    Closes anything and exits.
  208.      */
  209.  
  210. void
  211. CloseAll(ReturnCode)
  212. register short ReturnCode;
  213. {
  214.     if(Window)
  215.     {
  216.         while(Massage = (struct IntuiMessage *)GetMsg(Window -> UserPort))
  217.             ReplyMsg(Massage);
  218.  
  219.         ClearMenuStrip(Window);
  220.  
  221.         CloseWindow(Window);
  222.     }
  223.  
  224.     if(IntuitionBase)
  225.         CloseLibrary(IntuitionBase);
  226.  
  227.     if(GfxBase)
  228.         CloseLibrary(GfxBase);
  229.  
  230.     CloseTimerDevice();
  231.  
  232.     exit(ReturnCode);
  233. }
  234.  
  235.     /* OpenAll() :
  236.      *
  237.      *    Opens everything we need.
  238.      */
  239.  
  240. void
  241. OpenAll()
  242. {
  243.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",LIBRARY_VERSION)))
  244.         CloseAll(20);
  245.  
  246.     if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",LIBRARY_VERSION)))
  247.         CloseAll(20);
  248.  
  249.     if(!(Window = (struct Window *)OpenWindow(&NewWindow)))
  250.         CloseAll(20);
  251.  
  252.     if(!SetMenuStrip(Window,&GaugeMenu))
  253.         CloseAll(20);
  254.  
  255.     if(!OpenTimerDevice())
  256.         CloseAll(20);
  257.  
  258.     RPort = Window -> RPort;
  259. }
  260.  
  261.     /* MaxMemSize(MemType) :
  262.      *
  263.      *    This one was borrowed from GfxMem 0.4 by Louis A.
  264.      *    Mamakos. MaxMemSize() returns the size of a block
  265.      *    of memory in the MemList structures to be found
  266.      *    in ExecBase.
  267.      */
  268.  
  269. ULONG
  270. MaxMemSize(MemType)
  271. register unsigned long MemType;
  272. {
  273.     register ULONG BlockSize = 0;
  274.     register struct MemHeader *MemHeader;
  275.     register struct ExecBase *ExecBase = SysBase;
  276.  
  277.     Forbid();
  278.  
  279.     for(MemHeader = (struct MemHeader *)ExecBase -> MemList . lh_Head ; MemHeader -> mh_Node . ln_Succ ; MemHeader = (struct MemHeader *)MemHeader -> mh_Node . ln_Succ)
  280.         if(MemHeader -> mh_Attributes & MemType)
  281.             BlockSize += ((ULONG)MemHeader -> mh_Upper - (ULONG)MemHeader -> mh_Lower);
  282.  
  283.     Permit();
  284.  
  285.     return(BlockSize);
  286. }
  287.  
  288.     /* RedrawBar(ReallyDoIt) :
  289.      *
  290.      *    Displays the memory usage according to the size
  291.      *    of our window.
  292.      */
  293.  
  294. void
  295. RedrawBar(ReallyDoIt)
  296. BOOL ReallyDoIt;
  297. {
  298.     register short Length;
  299.  
  300.     static ULONG LastCurMem = 0;
  301.  
  302.     CurMem = AvailMem(MEMF_CHIP) + AvailMem(MEMF_FAST);
  303.     Length = ((Window -> Height - 16) * CurMem) / MaxMem;
  304.  
  305.         /* If we don't really need to redraw the graph
  306.          * we return the compliment.
  307.          */
  308.  
  309.     if(CurMem == LastCurMem && !ReallyDoIt)
  310.         return;
  311.  
  312.         /* Draw occupied memory... */
  313.  
  314.     SetAPen(RPort,3);
  315.     RectFill(RPort,2,8 + Length,13,Window -> Height - 9);
  316.  
  317.         /* Add the empty rest. */
  318.  
  319.     SetAPen(RPort,2);
  320.     RectFill(RPort,2,8,13,8 + Length);
  321.  
  322.         /* Draw the images. */
  323.  
  324.     DrawImage(RPort,&FullImage,0,0);
  325.     DrawImage(RPort,&EptyImage,0,Window -> Height - 8);
  326.  
  327.     LastCurMem = CurMem;
  328. }
  329.  
  330.     /* This will save some memory. */
  331.  
  332. void _wb_parse(){}
  333. void MemCleanup(){}
  334.  
  335.     /* main(argc,argv) :
  336.      *
  337.      *    This is where it starts.
  338.      */
  339.  
  340. void
  341. main(argc,argv)
  342. long argc;
  343. char *argv[];
  344. {
  345.         /* Timer delay and number of runs */
  346.  
  347.     short MaxSteps = 1,Steps = TIMEFRAG * MaxSteps;
  348.  
  349.         /* Some working data for the menu stuff. */
  350.  
  351.     USHORT MenuNum;
  352.     struct MenuItem *Item;
  353.  
  354.         /* Position and height of the window. */
  355.  
  356.     SHORT LeftEdge = 0,TopEdge = 11,Height = 189;
  357.  
  358.         /* User wants help? */
  359.  
  360.     if(argv[1][0] == '?')
  361.     {
  362. #ifdef AZTEC_C
  363.         if(_BackGroundIO)
  364.         {
  365.             Send(Output(),"\nMemGauge v1.4 © 1989 by Olaf 'Olsen' Barthel & ED Electronic Design Hannover\n\n");
  366.  
  367.             Send(Output(),"Useage: MEMGAUGE <Left edge> <Top edge> <Height>\n");
  368.         }
  369. #else
  370.         Send(_Backstdout,"\nMemGauge v1.4 © 1989 by Olaf 'Olsen' Barthel & ED Electronic Design Hannover\n\n");
  371.  
  372.         Send(_Backstdout,"Useage: MEMGAUGE <Left edge> <Top edge> <Height>\n");
  373. #endif AZTEC_C
  374.         exit(0);
  375.     }
  376.  
  377. #ifndef AZTEC_C
  378.  
  379.         /* Lattice 'C' needs some more lines to get rid of
  380.          * stdout.
  381.          */
  382.  
  383.     if(_Backstdout)
  384.         Close(_Backstdout);
  385.  
  386.     _Backstdout = 0;
  387.  
  388. #endif AZTEC_C
  389.  
  390.         /* Adjust X-position... */
  391.  
  392.     if(argc > 1)
  393.     {
  394.         LeftEdge = atoi(argv[1]);
  395.  
  396.         if(LeftEdge < 0)
  397.             LeftEdge = 0;
  398.  
  399.         if(LeftEdge > 624)
  400.             LeftEdge = 624;
  401.     }
  402.  
  403.         /* Adjust Y-position... */
  404.  
  405.     if(argc > 2)
  406.         if((TopEdge = atoi(argv[2])) < 0)
  407.             Height = 0;
  408.  
  409.         /* Adjust dimensions... */
  410.  
  411.     if(argc > 3)
  412.         if((Height = atoi(argv[3])) < 64)
  413.             Height = 64;
  414.  
  415.     NewWindow . LeftEdge    = LeftEdge;
  416.     NewWindow . TopEdge    = TopEdge;
  417.     NewWindow . Height    = Height;
  418.  
  419.         /* Say what we need. */
  420.  
  421.     OpenAll();
  422.  
  423.         /* How much memory is there in this Amiga? */
  424.  
  425.     MaxMem = MaxMemSize(MEMF_CHIP) + MaxMemSize(MEMF_FAST);
  426.  
  427.         /* The BIG loop. */
  428.  
  429.     FOREVER
  430.     {
  431.             /* Wait a while... */
  432.  
  433.         WaitTime(0,1000000 / TIMEFRAG);
  434.  
  435.             /* Check window for messages. */
  436.  
  437.         if(Massage = (struct IntuiMessage *)GetMsg(Window -> UserPort))
  438.         {
  439.             Class    = Massage -> Class;
  440.             Code    = Massage -> Code;
  441.  
  442.             ReplyMsg(Massage);
  443.  
  444.                 /* Are we to redraw the window? */
  445.  
  446.             if(Class == NEWSIZE || Class == ACTIVEWINDOW || Class == INACTIVEWINDOW)
  447.             {
  448.                 Steps = 0;
  449.                 RedrawBar(TRUE);
  450.             }
  451.  
  452.                 /* Intuition wants us to redraw the
  453.                  * window.
  454.                  */
  455.  
  456.             if(Class == REFRESHWINDOW)
  457.             {
  458.                 BeginRefresh(Window);
  459.  
  460.                 Steps = 0;
  461.                 RedrawBar(TRUE);
  462.  
  463.                 EndRefresh(Window,TRUE);
  464.             }
  465.  
  466.                 /* User selected a menu item. */
  467.  
  468.             if(Class == MENUPICK)
  469.             {
  470.                 MenuNum = Code;
  471.  
  472.                 while(MenuNum != MENUNULL)
  473.                 {
  474.                     if(MENUNUM(MenuNum) == GAUGE)
  475.                     {
  476.                         switch(ITEMNUM(MenuNum))
  477.                         {
  478.                                 /* About... */
  479.  
  480.                             case 0:    AutoRequest(NULL,&AboutTxt[0],NULL,&Proceed,NULL,NULL,274,102);
  481.                                 break;
  482.  
  483.                                 /* Window to front? */
  484.  
  485.                             case 2:    WindowToFront(Window);
  486.                                 break;
  487.  
  488.                                 /* Window to back? */
  489.  
  490.                             case 3:    WindowToBack(Window);
  491.                                 break;
  492.  
  493.                                 /* Close window? */
  494.  
  495.                             case 5: CloseAll(0);
  496.  
  497.                                 /* 1 Second update? */
  498.  
  499.                             case 7:    MaxSteps = 1;
  500.                                 break;
  501.  
  502.                                 /* 2 Seconds update? */
  503.  
  504.                             case 8:    MaxSteps = 2;
  505.                                 break;
  506.  
  507.                                 /* 5 Seconds update? */
  508.  
  509.                             case 9:    MaxSteps = 5;
  510.                                 break;
  511.  
  512.                                 /* 10 Seconds update? */
  513.  
  514.                             case 10:MaxSteps = 10;
  515.                                 break;
  516.  
  517.                             default:break;
  518.                         }
  519.                     }
  520.  
  521.                         /* Check for next entry. */
  522.  
  523.                     Item = (struct MenuItem *)ItemAddress(&GaugeMenu,MenuNum);
  524.  
  525.                     MenuNum = Item -> NextSelect;
  526.                 }
  527.             }
  528.         }
  529.  
  530.             /* Are we to redraw the window? */
  531.  
  532.         if((Steps++) < TIMEFRAG * MaxSteps)
  533.             continue;
  534.  
  535.             /* Reset the counter. */
  536.  
  537.         Steps = 0;
  538.  
  539.             /* Redraw the window. */
  540.  
  541.         RedrawBar(FALSE);
  542.     }
  543. }
  544.