home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 635.lha / MemSnap_v1.0 / memsnap.c < prev    next >
C/C++ Source or Header  |  1992-03-30  |  8KB  |  296 lines

  1. /*
  2. *    MemSnap.c
  3. *
  4. *    Just like MeMeter (a program I've been using for ages now).
  5. *    Why write it? The MeMeter I've been using crashes under 2.0,
  6. *    and I thought I'd take the time to learn (a bit) about 2.0
  7. *    by programming with it. Nothing groundbreaking here, though.
  8. *
  9. *    Martin W Scott, 3/92.
  10. */
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <intuition/intuition.h>
  14. #include <proto/exec.h>
  15. #include <proto/dos.h>
  16. #include <proto/intuition.h>
  17.  
  18. #include "wintext.h"    /* font-independent positioning of text */
  19.  
  20. char version_str[] = "$VER: MemSnap 1.0 (" __DATE__ ")";
  21.  
  22. #define CHARS_ACROSS    32    /* max chars across for window texts */
  23. #define CHARS_DOWN     4    /* how many rows of text in window */
  24.  
  25. WINTEXT wtexts[] = {
  26.     { &wtexts[1], " Current",  6, 0, 3, JAM2 },
  27.     { &wtexts[2], "Snapshot", 15, 0, 3, JAM2 },
  28.     { &wtexts[3], "    Used", 24, 0, 3, JAM2 },
  29.     { &wtexts[4], " Chip",     0, 1, 2, JAM2 },
  30.     { &wtexts[5], " Fast",     0, 2, 2, JAM2 },
  31.     { NULL,       "Total",     0, 3, 2, JAM2 }
  32. };
  33.  
  34. char cbuf[3][9], sbuf[3][9], ubuf[3][9];
  35.  
  36. WINTEXT ctexts[] = {
  37.     { &ctexts[1], &cbuf[0][0], 6, 1, 1, JAM2 },
  38.     { &ctexts[2], &cbuf[1][0], 6, 2, 1, JAM2 },
  39.     { NULL,       &cbuf[2][0], 6, 3, 1, JAM2 }
  40. };
  41.  
  42. WINTEXT stexts[] = {
  43.     { &stexts[1], &sbuf[0][0], 15, 1, 1, JAM2 },
  44.     { &stexts[2], &sbuf[1][0], 15, 2, 1, JAM2 },
  45.     { NULL,       &sbuf[2][0], 15, 3, 1, JAM2 }
  46. };
  47.  
  48. WINTEXT utexts[] = {
  49.     { &utexts[1], &ubuf[0][0], 24, 1, 1, JAM2 },
  50.     { &utexts[2], &ubuf[1][0], 24, 2, 1, JAM2 },
  51.     { NULL,       &ubuf[2][0], 24, 3, 1, JAM2 }
  52. };
  53.  
  54. /* back to normal stuff */
  55.  
  56. struct Gadget biggadget;    /* this will be snapshot gadget */
  57.  
  58. #define LEFTEDGE 40        /* window coordinates */
  59. #define TOPEDGE  20
  60.  
  61. struct NewWindow nw = {
  62.     LEFTEDGE,TOPEDGE,0,0,-1,-1,        /* dimension, pens */
  63.     CLOSEWINDOW | GADGETUP | NEWSIZE,    /* IDCMP flags */
  64.     WFLG_CLOSEGADGET |  WFLG_DRAGBAR    /* window flags */
  65.     | WFLG_DEPTHGADGET | WFLG_SMART_REFRESH,
  66.     &biggadget, NULL,            /* gadgets, checkmark */
  67.     "MemSnap",                /* title */
  68.     NULL, NULL,                /* screen, bitmap */
  69.     0,0,0,0,                /* extrema of dimensions */
  70.     WBENCHSCREEN                /* screen to open onto */
  71. };
  72. WORD zipdata[4] = {LEFTEDGE, TOPEDGE};
  73. struct TagItem wtags[] = {
  74.     { WA_Zoom, (LONG)&zipdata[0] },
  75.     { TAG_DONE }
  76. };
  77.  
  78. struct TextFont *font;            /* screen's default font (and so ours) */
  79. struct Window *w;            /* screen pointer */
  80. struct GfxBase *GfxBase;        /* graphics pointer */
  81. struct IntuitionBase *IntuitionBase;    /* intuition pointer */
  82. WINTEXTINFO wtinfo;
  83. #define DELAY_TIME    10L
  84.  
  85. /******************************************************************************/
  86. /* My stub for dos.library's VPrintf - amiga.lib one doesn't work for me      */
  87. /******************************************************************************/
  88.  
  89. #include <stdarg.h>
  90.  
  91. LONG __stdargs DosPrintf(char *s, ...);
  92.  
  93. LONG __stdargs DosPrintf(char *s, ...)
  94. {
  95.     va_list ap;
  96.     va_start(ap,s);
  97.     return VPrintf(s, (long *)ap);
  98. }
  99.  
  100. /******************************************************************************/
  101.  
  102. /* prototypes for general routines */
  103.  
  104. void _main(char *);
  105. BOOL OpenLibs(void);
  106. void CloseAll(void), main(int, char **);
  107. BOOL long2str(LONG, char *, UWORD);
  108.  
  109.  
  110. BOOL OpenLibs()        /* open required libraries */
  111. {
  112.     if ((GfxBase = (void *)OpenLibrary("graphics.library", 0L)) &&
  113.         (IntuitionBase = (void *)OpenLibrary("intuition.library", 37L)))
  114.         return TRUE;
  115.     CloseAll();
  116.     return FALSE;
  117. }
  118.  
  119.  
  120. void CloseAll()        /* close opened libraries */
  121. {
  122.     if (font) CloseFont(font);
  123.     if (w) CloseWindow(w);
  124.     if (GfxBase) CloseLibrary(GfxBase);
  125.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  126. }
  127.  
  128.  
  129. /* and this one is rather specific to this program... */
  130.  
  131. BOOL long2str(LONG n, char *s, UWORD len)    /* long to string, right-adjusted */
  132. {                        /* will NOT null-terminate */
  133.                         /* len is space in buffer (excl. '\0') */
  134.                         /* also, prints nothin if zero */
  135.     short sign;        /* minus sign required? */
  136.     char *t = &s[len-1];    /* get last space in string */
  137.  
  138.     if (n < 0)        /* get sign of n */
  139.     {
  140.         n = -n;
  141.         sign = -1;
  142.         len--;        /* reduce space (we'll need it for '-') */
  143.     }
  144.     else sign = 0;
  145.  
  146.     while (n && len)    /* work to do and space to do it */
  147.     {
  148.         *t = '0' + (n % 10);    /* get rightmost digit */
  149.         t--;            /* mave back up string */
  150.         len--;
  151.         n /= 10;
  152.     }
  153.  
  154.     if (sign)
  155.         *t-- = '-';        /* put sign in now */
  156.  
  157.     while (len--)            /* fill remainder with spaces */
  158.         *t-- = ' ';
  159.  
  160.     if (n) return FALSE;        /* failure */
  161.     return TRUE;
  162. }
  163.     
  164.         
  165. /******************************************************************************/
  166.  
  167. /* Memory data management/manipulation routines */
  168.  
  169. void getmem(ULONG *), submem(ULONG *, ULONG *, ULONG *), updatemem(ULONG *, WINTEXT *); 
  170.  
  171. #define CHIP 0
  172. #define FAST 1
  173. #define TOTAL 2
  174.  
  175. #define clearmem(mem)         mem[CHIP] = mem[FAST] = mem[TOTAL] = 0L
  176.  
  177. void getmem(ULONG *mem)        /* store current memory */
  178. {
  179.     mem[TOTAL] = mem[CHIP] = AvailMem(MEMF_CHIP);
  180.     mem[TOTAL] += (mem[FAST] = AvailMem(MEMF_FAST));
  181. }
  182.  
  183.  
  184. void submem(ULONG *to, ULONG *from, ULONG *howmuch) /* to = from - howmuch */
  185. {
  186.     to[CHIP] = from[CHIP] - howmuch[CHIP];
  187.     to[FAST] = from[FAST] - howmuch[FAST];
  188.     to[TOTAL] = from[TOTAL] - howmuch[TOTAL];
  189. }
  190.  
  191.  
  192. void updatemem(ULONG *mem, WINTEXT *memtext)    /* update specified display */
  193. {
  194.     long2str(mem[CHIP], memtext[CHIP].text, 8);
  195.     long2str(mem[FAST], memtext[FAST].text, 8);
  196.     long2str(mem[TOTAL], memtext[TOTAL].text, 8);
  197.  
  198.     RenderWinTexts(&wtinfo, memtext);
  199. }
  200.  
  201. /******************************************************************************/
  202.  
  203.  
  204. void _main(char *args)        /* provide a memory 'meter' */
  205. {
  206.     struct IntuiMessage *msg;    /* our window messages */
  207.     ULONG cmem[3],smem[3],umem[3];    /* storage of memory information */
  208.     ULONG class;            /* message class */
  209.     BOOL iconified = FALSE;        /* are we iconified? */
  210.  
  211.  
  212.     if (!OpenLibs())    /* failure => under 1.3 */
  213.     {
  214.         if (Output())    /* got a CLI */
  215.             Write(Output(), "Needs KS 2.0\n", -1);
  216.     }
  217.     else if (InitWinTextInfo(&wtinfo))
  218.     {
  219.         /* size window to fit screen font */
  220.         nw.Height = CHARS_DOWN*wtinfo.font_y + wtinfo.toffset + wtinfo.boffset;
  221.         nw.Width = CHARS_ACROSS*wtinfo.font_x + wtinfo.loffset + wtinfo.roffset;
  222.         zipdata[2] = 20*wtinfo.font_x;
  223.          zipdata[3] = wtinfo.toffset;
  224.  
  225.         /* and set up big gadget */
  226.         biggadget.LeftEdge = wtinfo.loffset;
  227.         biggadget.TopEdge = wtinfo.toffset;
  228.         biggadget.Width = CHARS_ACROSS*wtinfo.font_x;
  229.         biggadget.Height = wtinfo.font_y;
  230.         biggadget.Flags = GADGHCOMP;
  231.         biggadget.Activation = RELVERIFY;
  232.  
  233.         if (w = OpenWindowTagList(&nw, wtags))
  234.         {
  235.             wtinfo.window = w;
  236.             if (!(font = OpenFont(wtinfo.tattr)))
  237.             {
  238.                 DosPrintf("Can't get screen's font!\n");
  239.                 CloseAll();    /* unlikely event... */
  240.             }
  241.             SetFont(w->RPort, font);
  242.             RenderWinTexts(&wtinfo, wtexts);    /* draw initial texts */
  243.  
  244.             clearmem(smem);        /* initialize memory display */
  245.  
  246.             for (;;)    /* main event loop */
  247.             {
  248.                 while (msg = (struct IntuiMessage *)GetMsg(w->UserPort))
  249.                 {
  250.                     class = msg->Class;
  251.                     ReplyMsg((struct Message *)msg);
  252.  
  253.                     if (class == GADGETUP)
  254.                     {
  255.                         /* new snapshot */
  256.                         getmem(smem);
  257.                         updatemem(smem, stexts);
  258.                     }
  259.                     else if (class == NEWSIZE)
  260.                     {
  261.                         if (iconified)
  262.                         {
  263.                             iconified = FALSE;
  264.                             RenderWinTexts(&wtinfo, wtexts);
  265.                             RenderWinTexts(&wtinfo, stexts);
  266.                         }
  267.                         else iconified = TRUE;
  268.                     }
  269.                     else /* class == CLOSEWINDOW */
  270.                     {
  271.                         CloseAll();
  272.                         return;
  273.                     }
  274.  
  275.                 } /* while */
  276.  
  277.                 if (iconified)
  278.                     Wait(1 <<w->UserPort->mp_SigBit);
  279.                 else
  280.                 {
  281.                     getmem(cmem);
  282.                     submem(umem, smem, cmem);
  283.                     updatemem(cmem, ctexts);
  284.                     updatemem(umem, utexts);
  285.  
  286.                     Delay(DELAY_TIME);
  287.                 }
  288.  
  289.             } /* for */
  290.         }
  291.         else DosPrintf("Can't open window\n");
  292.     }
  293.     else DosPrintf("Can't get screen info\n");
  294.  
  295.     CloseAll();
  296. } /* main */