home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d8xx / d826 / memsnap.lha / MemSnap / source / memsnap.c < prev    next >
C/C++ Source or Header  |  2001-02-25  |  10KB  |  421 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. #include "wintext.h"        /* font-independent positioning of text */
  18. #include "icon.h"
  19. #include "menu.h"
  20.  
  21. char version_str[] = "$VER: MemSnapII 1.0 (" __DATE__ ")";
  22.  
  23. #define CHARS_ACROSS    32    /* max chars across for window texts */
  24. #define CHARS_DOWN     4    /* how many rows of text in window */
  25. #define REMOVECHARS    18    /* how many cols removed when window small */
  26.  
  27. #define LARGE_HEADER "       Current Snapshot     Used"
  28. #define SMALL_HEADER "    Memory"
  29.  
  30. #define ABOUT_TXT "\
  31.   MemSnapII v1.0\n\
  32. by Martin W. Scott\n\
  33. \n\
  34.      Freeware"
  35.  
  36. WINTEXT wtexts[] =
  37. {
  38.     {&wtexts[1], "       Current Snapshot     Used", 0, 0, 3, 0, JAM2},
  39.     {&wtexts[2], " Chip", 0, 1, 2, 0, JAM2},
  40.     {&wtexts[3], " Fast", 0, 2, 2, 0, JAM2},
  41.     {NULL, "Total", 0, 3, 2, 0, JAM2}
  42. };
  43.  
  44. char cbuf[3][9], sbuf[3][9], ubuf[3][9];
  45.  
  46. WINTEXT ctexts[] =
  47. {
  48.     {&ctexts[1], &cbuf[0][0], 6, 1, 1, 0, JAM2},
  49.     {&ctexts[2], &cbuf[1][0], 6, 2, 1, 0, JAM2},
  50.     {NULL, &cbuf[2][0], 6, 3, 1, 0, JAM2}
  51. };
  52.  
  53. WINTEXT stexts[] =
  54. {
  55.     {&stexts[1], &sbuf[0][0], 15, 1, 1, 0, JAM2},
  56.     {&stexts[2], &sbuf[1][0], 15, 2, 1, 0, JAM2},
  57.     {NULL, &sbuf[2][0], 15, 3, 1, 0, JAM2}
  58. };
  59.  
  60. WINTEXT utexts[] =
  61. {
  62.     {&utexts[1], &ubuf[0][0], 24, 1, 1, 0, JAM2},
  63.     {&utexts[2], &ubuf[1][0], 24, 2, 1, 0, JAM2},
  64.     {NULL, &ubuf[2][0], 24, 3, 1, 0, JAM2}
  65. };
  66.  
  67. /* back to normal stuff */
  68.  
  69. struct Gadget drag_gadget =
  70. {
  71. /* whole window */
  72.     NULL,
  73.     0, 0, 0, 0,
  74.     GRELWIDTH | GRELHEIGHT | GADGHNONE,
  75.     GADGIMMEDIATE,
  76.     GTYP_WDRAGGING
  77. /* rest is 0/NULL */
  78. };
  79.  
  80. struct Gadget biggadget =    /* this will be snapshot gadget */
  81. {
  82.     &drag_gadget,
  83.     0, 0, 0, 0,
  84.     GRELWIDTH | GADGHCOMP,
  85.     RELVERIFY
  86. /* rest is 0/NULL */
  87. };
  88.  
  89. #define LEFTEDGE 40        /* window coordinates */
  90. #define TOPEDGE  20
  91.  
  92. struct NewWindow nw =
  93. {
  94.     LEFTEDGE, TOPEDGE, 0, 0, -1, -1,    /* dimension, pens */
  95.     NEWSIZE | MENUPICK | GADGETUP,    /* IDCMP flags */
  96.     WFLG_SMART_REFRESH,        /* window flags */
  97.     &biggadget, NULL,        /* gadgets, checkmark */
  98.     NULL,            /* title */
  99.     NULL, NULL,            /* screen, bitmap */
  100.     0, 0, 0, 0,            /* extrema of dimensions */
  101.     WBENCHSCREEN        /* screen to open onto */
  102. };
  103.  
  104. struct TagItem wtags[] =
  105. {
  106.     {WA_AutoAdjust, TRUE},
  107.     {TAG_DONE}
  108. };
  109.  
  110. struct Window *w;        /* screen pointer */
  111. struct Menu *menu;
  112. struct GfxBase *GfxBase;    /* graphics pointer */
  113. struct IntuitionBase *IntuitionBase;    /* intuition pointer */
  114. struct Library *IconBase, *DiskfontBase, *GadToolsBase;
  115. WINTEXTINFO wtinfo;
  116. extern struct WBStartup *WBenchMsg;
  117.  
  118. #define MEMSNAP_TIME    10L
  119. #define MEMONLY_TIME    25L
  120.  
  121. /******************************************************************************/
  122.  
  123. /* prototypes for general routines */
  124.  
  125. void _main(char *);
  126. BOOL OpenLibs(void);
  127. void CloseAll(void), main(int, char **);
  128. BOOL long2str(LONG, char *, UWORD);
  129.  
  130. #ifdef LATTICE    /* save the odd byte */
  131. #include <stdlib.h>
  132. void MemCleanup(void){}
  133. #endif
  134.  
  135. BOOL 
  136. OpenLibs()            /* open required libraries */
  137. {
  138.     if ((GfxBase = (void *) OpenLibrary("graphics.library", 0L)) &&
  139.     (IntuitionBase = (void *) OpenLibrary("intuition.library", 37L)) &&
  140.     (DiskfontBase = (void *) OpenLibrary("diskfont.library", 36L)) &&
  141.     (GadToolsBase = (void *) OpenLibrary("gadtools.library", 37L)) &&
  142.     (IconBase = (void *) OpenLibrary("icon.library", 37L)))
  143.     return TRUE;
  144.     CloseAll();
  145.     return FALSE;
  146. }
  147.  
  148.  
  149. void 
  150. CloseAll()            /* close opened libraries */
  151. {
  152.     if (menu)
  153.     {
  154.     ClearMenuStrip(w);
  155.     FreeMemSnapMenu();
  156.     }
  157.     if (wtinfo.tf)
  158.     CloseFont(wtinfo.tf);
  159.     if (w)
  160.     CloseWindow(w);
  161.     if (IconBase)
  162.     CloseLibrary(IconBase);
  163.     if (DiskfontBase)
  164.     CloseLibrary(DiskfontBase);
  165.     if (GadToolsBase)
  166.     CloseLibrary(GadToolsBase);
  167.     if (GfxBase)
  168.     CloseLibrary(GfxBase);
  169.     if (IntuitionBase)
  170.     CloseLibrary(IntuitionBase);
  171. }
  172.  
  173.  
  174. /* and this one is rather specific to this program... */
  175.  
  176. BOOL 
  177. long2str(LONG n, char *s, UWORD len)    /* long to string, right-adjusted */
  178. {                /* will NOT null-terminate */
  179.     /* len is space in buffer (excl. '\0') */
  180.     /* also, prints nothin if zero */
  181.     short sign;            /* minus sign required? */
  182.     char *t = &s[len - 1];    /* get last space in string */
  183.  
  184.     if (n < 0)            /* get sign of n */
  185.     {
  186.     n = -n;
  187.     sign = -1;
  188.     len--;            /* reduce space (we'll need it for '-') */
  189.     }
  190.     else
  191.     sign = 0;
  192.  
  193.     while (n && len)        /* work to do and space to do it */
  194.     {
  195.     *t = '0' + (n % 10);    /* get rightmost digit */
  196.     t--;            /* mave back up string */
  197.     len--;
  198.     n /= 10;
  199.     }
  200.  
  201.     if (sign)
  202.     *t-- = '-';        /* put sign in now */
  203.  
  204.     while (len--)        /* fill remainder with spaces */
  205.     *t-- = ' ';
  206.  
  207.     if (n)
  208.     return FALSE;        /* failure */
  209.     return TRUE;
  210. }
  211.  
  212.  
  213. /******************************************************************************/
  214.  
  215. /* Memory data management/manipulation routines */
  216.  
  217. void obtainmem(ULONG *), submem(ULONG *, ULONG *, ULONG *), updatemem(ULONG *, WINTEXT *);
  218.  
  219. #define CHIP 0
  220. #define FAST 1
  221. #define TOTAL 2
  222.  
  223. #define clearmem(mem)         mem[CHIP] = mem[FAST] = mem[TOTAL] = 0L
  224.  
  225. void 
  226. obtainmem(ULONG * mem)        /* store current memory */
  227. {
  228.     mem[TOTAL] = mem[CHIP] = AvailMem(MEMF_CHIP);
  229.     mem[TOTAL] += (mem[FAST] = AvailMem(MEMF_FAST));
  230. }
  231.  
  232.  
  233. void 
  234. submem(ULONG * to, ULONG * from, ULONG * howmuch)    /* to = from - howmuch */
  235. {
  236.     to[CHIP] = from[CHIP] - howmuch[CHIP];
  237.     to[FAST] = from[FAST] - howmuch[FAST];
  238.     to[TOTAL] = from[TOTAL] - howmuch[TOTAL];
  239. }
  240.  
  241.  
  242. void 
  243. updatemem(ULONG * mem, WINTEXT * memtext)    /* update specified display */
  244. {
  245.     long2str(mem[CHIP], memtext[CHIP].text, 8);
  246.     long2str(mem[FAST], memtext[FAST].text, 8);
  247.     long2str(mem[TOTAL], memtext[TOTAL].text, 8);
  248.  
  249.     RenderWinTexts(&wtinfo, memtext);
  250. }
  251.  
  252. /* pop up a requester */
  253. void
  254. EasyEasyRequest(char *str)
  255. {
  256.     struct EasyStruct es;
  257.  
  258.     es.es_StructSize = sizeof(struct EasyStruct);
  259.  
  260.     es.es_Flags = 0L;
  261.     es.es_Title = "MemSnap Message";
  262.     es.es_TextFormat = str;
  263.     es.es_GadgetFormat = "OK";
  264.     EasyRequestArgs(NULL, &es, NULL, NULL);
  265. }
  266.  
  267.  
  268. #define Msg(s) EasyEasyRequest(s)
  269.  
  270. /******************************************************************************/
  271.  
  272.  
  273. void 
  274. _main(char *args)        /* provide a memory 'meter' */
  275. {
  276.     struct IntuiMessage *msg;        /* our window messages */
  277.     ULONG cmem[3], smem[3], umem[3];    /* storage of memory information */
  278.     ULONG class;            /* message class */
  279.     UWORD code;                /* and code */
  280.     WORD smallwidth, largewidth;    /* possible window sizes */
  281.     BOOL small;                /* are we small? */
  282.  
  283.  
  284.     if (!OpenLibs())        /* failure => under 1.3 */
  285.     return;
  286.  
  287.     GetOurIcon(WBenchMsg);
  288.     if (InitWinTextInfo(&wtinfo))
  289.     {
  290.     /* size window to fit screen font */
  291.     nw.LeftEdge = TTInt("LEFTEDGE", LEFTEDGE);
  292.     nw.TopEdge = TTInt("TOPEDGE", TOPEDGE);
  293.     small = TTBool("SMALL", FALSE);
  294.     FreeOurIcon();        /* finished with it */
  295.  
  296.     nw.Height = CHARS_DOWN * wtinfo.font_y + wtinfo.toffset + wtinfo.boffset;
  297.     largewidth = CHARS_ACROSS * wtinfo.font_x + wtinfo.loffset + wtinfo.roffset;
  298.     smallwidth = largewidth - REMOVECHARS * wtinfo.font_x;
  299.     nw.Width = small ? smallwidth : largewidth;
  300.     wtexts[0].text = small ? SMALL_HEADER : LARGE_HEADER;
  301.  
  302.     /* and set up big gadget */
  303.     biggadget.LeftEdge = wtinfo.loffset;
  304.     biggadget.TopEdge = wtinfo.toffset;
  305.     biggadget.Width = -wtinfo.roffset-wtinfo.loffset;
  306.     biggadget.Height = wtinfo.font_y;
  307.  
  308.     if (w = OpenWindowTagList(&nw, wtags))
  309.     {
  310.         if (w->Width != nw.Width)
  311.         {
  312.         Msg("Window too small for text\nChoose a smaller font");
  313.         CloseAll();
  314.         return;
  315.         }
  316.  
  317.         if (menu = AllocMemSnapMenu(w))
  318.         SetMenuStrip(w, menu);
  319.         else
  320.         {
  321.         Msg("Couldn't create menus");
  322.         CloseAll();
  323.         return;
  324.         }
  325.  
  326.         wtinfo.window = w;
  327.         SetFont(w->RPort, wtinfo.tf);
  328.         RenderWinTexts(&wtinfo, wtexts);    /* draw initial texts */
  329.  
  330.         clearmem(smem);    /* initialize memory display */
  331.  
  332.         for (;;)        /* main event loop */
  333.         {
  334.         while (msg = (struct IntuiMessage *) GetMsg(w->UserPort))
  335.         {
  336.             class = msg->Class;
  337.             code = msg->Code;
  338.             ReplyMsg((struct Message *) msg);
  339.  
  340.             if (class == GADGETUP)
  341.             {
  342.             if (!small)
  343.             {
  344.                 /* new snapshot */
  345.                 obtainmem(smem);
  346.                 updatemem(smem, stexts);
  347.             }
  348.             else goto makelarge;    /* naughty but so what? */
  349.             }
  350.             else if (class == NEWSIZE)
  351.             {
  352.                 RenderWinTexts(&wtinfo, wtexts);    /* redraw texts */
  353.             if (!small)
  354.                 updatemem(smem, stexts);
  355.             }
  356.             else if (class == MENUPICK)
  357.             {
  358.             switch (ITEMNUM(code))
  359.             {
  360.             case SMALL:
  361.                 if (!small)
  362.                 {
  363.                 wtexts[0].text = SMALL_HEADER;
  364.                 SizeWindow(w, smallwidth-largewidth, 0);
  365.                 small = TRUE;
  366.                 }
  367.                 break;
  368.  
  369.             case LARGE:
  370. makelarge:            if (small)    /* to do: move if nec. */
  371.                 {
  372.                 WORD movex;
  373.                 wtexts[0].text = LARGE_HEADER;
  374.                 movex = w->WScreen->Width - w->LeftEdge - largewidth;
  375.                 if (movex < 0)
  376.                     MoveWindow(w, movex, 0);
  377.                 SizeWindow(w, largewidth-smallwidth, 0);
  378.                 small = FALSE;
  379.                 }
  380.                 break;
  381.  
  382.             case ABOUT:
  383.                 Msg(ABOUT_TXT);
  384.                 break;
  385.  
  386.             case QUIT:
  387.                 CloseAll();
  388.                 return;
  389.             }
  390.             }
  391.  
  392.         }        /* while */
  393.  
  394.         /* do memory window stuff */
  395.         obtainmem(cmem);
  396.         updatemem(cmem, ctexts);
  397.  
  398.         if (small)
  399.         {
  400.             Delay(MEMONLY_TIME);
  401.         }
  402.         else
  403.         {
  404.             submem(umem, smem, cmem);
  405.             updatemem(umem, utexts);
  406.  
  407.             Delay(MEMSNAP_TIME);
  408.         }
  409.  
  410.         } /* for */
  411.     }
  412.     else
  413.         Msg("Can't open window");
  414.     }
  415.     else
  416.     Msg("Can't open font");
  417.  
  418.     FreeOurIcon();
  419.  
  420.     CloseAll();
  421. }