Now that we have printer device it makes sense to build Print module.
[AROS-Contrib.git] / fish / memsnap / memsnap.c
blob886a84b0827a686ec0f7a2d88a691059c964fc32
1 #include <aros/oldprograms.h>
3 /*
4 * MemSnap.c
6 * Just like MeMeter (a program I've been using for ages now).
7 * Why write it? The MeMeter I've been using crashes under 2.0,
8 * and I thought I'd take the time to learn (a bit) about 2.0
9 * by programming with it. Nothing groundbreaking here, though.
11 * Martin W Scott, 3/92.
13 #include <exec/types.h>
14 #include <exec/memory.h>
15 #include <intuition/intuition.h>
16 #include <proto/exec.h>
17 #include <proto/dos.h>
18 #include <proto/intuition.h>
19 #include "wintext.h" /* font-independent positioning of text */
20 #include "icon.h"
21 #include "menu.h"
23 char version_str[] = "$VER: MemSnapII 1.0 (" __DATE__ ")";
25 #define CHARS_ACROSS 32 /* max chars across for window texts */
26 #define CHARS_DOWN 4 /* how many rows of text in window */
27 #define REMOVECHARS 18 /* how many cols removed when window small */
29 #define LARGE_HEADER " Current Snapshot Used"
30 #define SMALL_HEADER " Memory"
32 #define ABOUT_TXT "\
33 MemSnapII v1.0\n\
34 by Martin W. Scott\n\
35 \n\
36 Freeware"
38 WINTEXT wtexts[] =
40 {&wtexts[1], " Current Snapshot Used", 0, 0, 3, 0, JAM2},
41 {&wtexts[2], " Chip", 0, 1, 2, 0, JAM2},
42 {&wtexts[3], " Fast", 0, 2, 2, 0, JAM2},
43 {NULL, "Total", 0, 3, 2, 0, JAM2}
46 char cbuf[3][9], sbuf[3][9], ubuf[3][9];
48 WINTEXT ctexts[] =
50 {&ctexts[1], &cbuf[0][0], 6, 1, 1, 0, JAM2},
51 {&ctexts[2], &cbuf[1][0], 6, 2, 1, 0, JAM2},
52 {NULL, &cbuf[2][0], 6, 3, 1, 0, JAM2}
55 WINTEXT stexts[] =
57 {&stexts[1], &sbuf[0][0], 15, 1, 1, 0, JAM2},
58 {&stexts[2], &sbuf[1][0], 15, 2, 1, 0, JAM2},
59 {NULL, &sbuf[2][0], 15, 3, 1, 0, JAM2}
62 WINTEXT utexts[] =
64 {&utexts[1], &ubuf[0][0], 24, 1, 1, 0, JAM2},
65 {&utexts[2], &ubuf[1][0], 24, 2, 1, 0, JAM2},
66 {NULL, &ubuf[2][0], 24, 3, 1, 0, JAM2}
69 /* back to normal stuff */
71 struct Gadget drag_gadget =
73 /* whole window */
74 NULL,
75 0, 0, 0, 0,
76 GRELWIDTH | GRELHEIGHT | GADGHNONE,
77 GADGIMMEDIATE,
78 GTYP_WDRAGGING
79 /* rest is 0/NULL */
82 struct Gadget biggadget = /* this will be snapshot gadget */
84 &drag_gadget,
85 0, 0, 0, 0,
86 GRELWIDTH | GADGHCOMP,
87 RELVERIFY,
88 GTYP_BOOLGADGET
89 /* rest is 0/NULL */
92 #define LEFTEDGE 40 /* window coordinates */
93 #define TOPEDGE 20
95 struct NewWindow nw =
97 LEFTEDGE, TOPEDGE, 0, 0, -1, -1, /* dimension, pens */
98 NEWSIZE | MENUPICK | GADGETUP, /* IDCMP flags */
99 WFLG_SMART_REFRESH, /* window flags */
100 &biggadget, NULL, /* gadgets, checkmark */
101 NULL, /* title */
102 NULL, NULL, /* screen, bitmap */
103 0, 0, 0, 0, /* extrema of dimensions */
104 WBENCHSCREEN /* screen to open onto */
107 struct TagItem wtags[] =
109 {WA_AutoAdjust, TRUE},
110 {TAG_DONE}
113 struct Window *w; /* screen pointer */
114 struct Menu *menu;
115 struct GfxBase *GfxBase; /* graphics pointer */
116 struct IntuitionBase *IntuitionBase; /* intuition pointer */
117 struct Library *IconBase, *DiskfontBase, *GadToolsBase;
118 WINTEXTINFO wtinfo;
119 struct WBStartup *WBenchMsg = NULL;
121 #define MEMSNAP_TIME 10L
122 #define MEMONLY_TIME 25L
124 /******************************************************************************/
126 /* prototypes for general routines */
128 int main (int, char **);
129 BOOL OpenLibs(void);
130 void CloseAll(void); //, main(int, char **);
131 BOOL long2str(LONG, char *, UWORD);
133 #ifdef LATTICE /* save the odd byte */
134 #include <stdlib.h>
135 void MemCleanup(void){}
136 #endif
138 BOOL
139 OpenLibs() /* open required libraries */
141 if ((GfxBase = (void *) OpenLibrary("graphics.library", 0L)) &&
142 (IntuitionBase = (void *) OpenLibrary("intuition.library", 37L)) &&
143 (DiskfontBase = (void *) OpenLibrary("diskfont.library", 36L)) &&
144 (GadToolsBase = (void *) OpenLibrary("gadtools.library", 37L)) &&
145 (IconBase = (void *) OpenLibrary("icon.library", 37L)))
146 return TRUE;
147 CloseAll();
148 return FALSE;
152 void
153 CloseAll() /* close opened libraries */
155 if (menu)
157 ClearMenuStrip(w);
158 FreeMemSnapMenu();
160 if (wtinfo.tf)
161 CloseFont(wtinfo.tf);
162 if (w)
163 CloseWindow(w);
164 if (IconBase)
165 CloseLibrary(IconBase);
166 if (DiskfontBase)
167 CloseLibrary(DiskfontBase);
168 if (GadToolsBase)
169 CloseLibrary(GadToolsBase);
170 if (GfxBase)
171 CloseLibrary((struct Library *)GfxBase);
172 if (IntuitionBase)
173 CloseLibrary((struct Library *)IntuitionBase);
177 /* and this one is rather specific to this program... */
179 BOOL
180 long2str(LONG n, char *s, UWORD len) /* long to string, right-adjusted */
181 { /* will NOT null-terminate */
182 /* len is space in buffer (excl. '\0') */
183 /* also, prints nothin if zero */
184 short sign; /* minus sign required? */
185 char *t = &s[len - 1]; /* get last space in string */
187 if (n < 0) /* get sign of n */
189 n = -n;
190 sign = -1;
191 len--; /* reduce space (we'll need it for '-') */
193 else
194 sign = 0;
196 while (n && len) /* work to do and space to do it */
198 *t = '0' + (n % 10); /* get rightmost digit */
199 t--; /* mave back up string */
200 len--;
201 n /= 10;
204 if (sign)
205 *t-- = '-'; /* put sign in now */
207 while (len--) /* fill remainder with spaces */
208 *t-- = ' ';
210 if (n)
211 return FALSE; /* failure */
212 return TRUE;
216 /******************************************************************************/
218 /* Memory data management/manipulation routines */
220 void obtainmem(ULONG *), submem(ULONG *, ULONG *, ULONG *), updatemem(ULONG *, WINTEXT *);
222 #define CHIP 0
223 #define FAST 1
224 #define TOTAL 2
226 #define clearmem(mem) mem[CHIP] = mem[FAST] = mem[TOTAL] = 0L
228 void
229 obtainmem(ULONG * mem) /* store current memory */
231 mem[TOTAL] = mem[CHIP] = AvailMem(MEMF_CHIP);
232 mem[TOTAL] += (mem[FAST] = AvailMem(MEMF_FAST));
236 void
237 submem(ULONG * to, ULONG * from, ULONG * howmuch) /* to = from - howmuch */
239 to[CHIP] = from[CHIP] - howmuch[CHIP];
240 to[FAST] = from[FAST] - howmuch[FAST];
241 to[TOTAL] = from[TOTAL] - howmuch[TOTAL];
245 void
246 updatemem(ULONG * mem, WINTEXT * memtext) /* update specified display */
248 long2str(mem[CHIP], memtext[CHIP].text, 8);
249 long2str(mem[FAST], memtext[FAST].text, 8);
250 long2str(mem[TOTAL], memtext[TOTAL].text, 8);
252 RenderWinTexts(&wtinfo, memtext);
255 /* pop up a requester */
256 void
257 EasyEasyRequest(char *str)
259 struct EasyStruct es;
261 es.es_StructSize = sizeof(struct EasyStruct);
263 es.es_Flags = 0L;
264 es.es_Title = "MemSnap Message";
265 es.es_TextFormat = str;
266 es.es_GadgetFormat = "OK";
267 EasyRequestArgs(NULL, &es, NULL, NULL);
271 #define Msg(s) EasyEasyRequest(s)
273 /******************************************************************************/
277 main (int argc, char **argv) /* provide a memory 'meter' */
279 struct IntuiMessage *msg; /* our window messages */
280 ULONG cmem[3], smem[3], umem[3]; /* storage of memory information */
281 ULONG class; /* message class */
282 UWORD code; /* and code */
283 WORD smallwidth, largewidth; /* possible window sizes */
284 BOOL small; /* are we small? */
287 if (argc == 0)
288 WBenchMsg = (struct WBStartup *) argv;
290 if (!OpenLibs()) /* failure => under 1.3 */
291 return RETURN_FAIL;
293 GetOurIcon(WBenchMsg);
294 if (InitWinTextInfo(&wtinfo))
296 /* size window to fit screen font */
297 nw.LeftEdge = TTInt("LEFTEDGE", LEFTEDGE);
298 nw.TopEdge = TTInt("TOPEDGE", TOPEDGE);
299 small = TTBool("SMALL", FALSE);
300 FreeOurIcon(); /* finished with it */
302 nw.Height = CHARS_DOWN * wtinfo.font_y + wtinfo.toffset + wtinfo.boffset;
303 largewidth = CHARS_ACROSS * wtinfo.font_x + wtinfo.loffset + wtinfo.roffset;
304 smallwidth = largewidth - REMOVECHARS * wtinfo.font_x;
305 nw.Width = small ? smallwidth : largewidth;
306 wtexts[0].text = small ? SMALL_HEADER : LARGE_HEADER;
308 /* and set up big gadget */
309 biggadget.LeftEdge = wtinfo.loffset;
310 biggadget.TopEdge = wtinfo.toffset;
311 biggadget.Width = -wtinfo.roffset-wtinfo.loffset;
312 biggadget.Height = wtinfo.font_y;
314 if ((w = OpenWindowTagList(&nw, wtags)))
316 if (w->Width != nw.Width)
318 Msg("Window too small for text\nChoose a smaller font");
319 CloseAll();
320 return RETURN_FAIL;
323 if ((menu = AllocMemSnapMenu(w)))
324 SetMenuStrip(w, menu);
325 else
327 Msg("Couldn't create menus");
328 CloseAll();
329 return RETURN_FAIL;
332 wtinfo.window = w;
333 SetFont(w->RPort, wtinfo.tf);
334 RenderWinTexts(&wtinfo, wtexts); /* draw initial texts */
336 clearmem(smem); /* initialize memory display */
338 for (;;) /* main event loop */
340 while ((msg = (struct IntuiMessage *) GetMsg(w->UserPort)))
342 class = msg->Class;
343 code = msg->Code;
344 ReplyMsg((struct Message *) msg);
346 if (class == GADGETUP)
348 if (!small)
350 /* new snapshot */
351 obtainmem(smem);
352 updatemem(smem, stexts);
354 else goto makelarge; /* naughty but so what? */
356 else if (class == NEWSIZE)
358 RenderWinTexts(&wtinfo, wtexts); /* redraw texts */
359 if (!small)
360 updatemem(smem, stexts);
362 else if (class == MENUPICK)
364 switch (ITEMNUM(code))
366 case SMALL:
367 if (!small)
369 wtexts[0].text = SMALL_HEADER;
370 SizeWindow(w, smallwidth-largewidth, 0);
371 small = TRUE;
373 break;
375 case LARGE:
376 makelarge: if (small) /* to do: move if nec. */
378 WORD movex;
379 wtexts[0].text = LARGE_HEADER;
380 movex = w->WScreen->Width - w->LeftEdge - largewidth;
381 if (movex < 0)
382 MoveWindow(w, movex, 0);
383 SizeWindow(w, largewidth-smallwidth, 0);
384 small = FALSE;
386 break;
388 case ABOUT:
389 Msg(ABOUT_TXT);
390 break;
392 case QUIT:
393 CloseAll();
394 return RETURN_OK;
398 } /* while */
400 /* do memory window stuff */
401 obtainmem(cmem);
402 updatemem(cmem, ctexts);
404 if (small)
406 Delay(MEMONLY_TIME);
408 else
410 submem(umem, smem, cmem);
411 updatemem(umem, utexts);
413 Delay(MEMSNAP_TIME);
416 } /* for */
418 else
419 Msg("Can't open window");
421 else
422 Msg("Can't open font");
424 FreeOurIcon();
426 CloseAll();
427 return RETURN_FAIL;