1 #include <aros/oldprograms.h>
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 */
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"
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];
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
}
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
}
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
=
76 GRELWIDTH
| GRELHEIGHT
| GADGHNONE
,
82 struct Gadget biggadget
= /* this will be snapshot gadget */
86 GRELWIDTH
| GADGHCOMP
,
92 #define LEFTEDGE 40 /* window coordinates */
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 */
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
},
113 struct Window
*w
; /* screen pointer */
115 struct GfxBase
*GfxBase
; /* graphics pointer */
116 struct IntuitionBase
*IntuitionBase
; /* intuition pointer */
117 struct Library
*IconBase
, *DiskfontBase
, *GadToolsBase
;
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 **);
130 void CloseAll(void); //, main(int, char **);
131 BOOL
long2str(LONG
, char *, UWORD
);
133 #ifdef LATTICE /* save the odd byte */
135 void MemCleanup(void){}
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)))
153 CloseAll() /* close opened libraries */
161 CloseFont(wtinfo
.tf
);
165 CloseLibrary(IconBase
);
167 CloseLibrary(DiskfontBase
);
169 CloseLibrary(GadToolsBase
);
171 CloseLibrary((struct Library
*)GfxBase
);
173 CloseLibrary((struct Library
*)IntuitionBase
);
177 /* and this one is rather specific to this program... */
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 */
191 len
--; /* reduce space (we'll need it for '-') */
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 */
205 *t
-- = '-'; /* put sign in now */
207 while (len
--) /* fill remainder with spaces */
211 return FALSE
; /* failure */
216 /******************************************************************************/
218 /* Memory data management/manipulation routines */
220 void obtainmem(ULONG
*), submem(ULONG
*, ULONG
*, ULONG
*), updatemem(ULONG
*, WINTEXT
*);
226 #define clearmem(mem) mem[CHIP] = mem[FAST] = mem[TOTAL] = 0L
229 obtainmem(ULONG
* mem
) /* store current memory */
231 mem
[TOTAL
] = mem
[CHIP
] = AvailMem(MEMF_CHIP
);
232 mem
[TOTAL
] += (mem
[FAST
] = AvailMem(MEMF_FAST
));
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
];
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 */
257 EasyEasyRequest(char *str
)
259 struct EasyStruct es
;
261 es
.es_StructSize
= sizeof(struct EasyStruct
);
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? */
288 WBenchMsg
= (struct WBStartup
*) argv
;
290 if (!OpenLibs()) /* failure => under 1.3 */
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");
323 if ((menu
= AllocMemSnapMenu(w
)))
324 SetMenuStrip(w
, menu
);
327 Msg("Couldn't create menus");
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
)))
344 ReplyMsg((struct Message
*) msg
);
346 if (class == GADGETUP
)
352 updatemem(smem
, stexts
);
354 else goto makelarge
; /* naughty but so what? */
356 else if (class == NEWSIZE
)
358 RenderWinTexts(&wtinfo
, wtexts
); /* redraw texts */
360 updatemem(smem
, stexts
);
362 else if (class == MENUPICK
)
364 switch (ITEMNUM(code
))
369 wtexts
[0].text
= SMALL_HEADER
;
370 SizeWindow(w
, smallwidth
-largewidth
, 0);
376 makelarge
: if (small
) /* to do: move if nec. */
379 wtexts
[0].text
= LARGE_HEADER
;
380 movex
= w
->WScreen
->Width
- w
->LeftEdge
- largewidth
;
382 MoveWindow(w
, movex
, 0);
383 SizeWindow(w
, largewidth
-smallwidth
, 0);
400 /* do memory window stuff */
402 updatemem(cmem
, ctexts
);
410 submem(umem
, smem
, cmem
);
411 updatemem(umem
, utexts
);
419 Msg("Can't open window");
422 Msg("Can't open font");