bch
/
xmem
1
0
Fork 0
xmem/xmem.c

227 lines
7.0 KiB
C
Raw Normal View History

2017-02-18 21:03:44 +00:00
/*
2017-02-18 21:11:06 +00:00
* xmem - display memory/swap usage utility for X
2017-02-18 21:03:44 +00:00
*
2018-12-26 18:46:04 +00:00
* Updated by Christian Barthel <bch@online.de>
*
2017-02-18 21:03:44 +00:00
* Copyright 1989 Massachusetts Institute of Technology
*
* $XConsortium: xload.c,v 1.36 91/05/24 16:57:46 converse Exp $
*/
#include <unistd.h>
2017-02-18 21:12:30 +00:00
#include <stdio.h>
#include <err.h>
#include <errno.h>
2017-02-18 21:03:44 +00:00
#include <X11/Intrinsic.h>
#include <X11/Xatom.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Xaw/Cardinals.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/StripChart.h>
#include <X11/Xmu/SysUtil.h>
2017-02-18 21:11:06 +00:00
#include "MemStripChart.h"
2017-02-18 21:03:44 +00:00
#include "xmem.bit"
extern void exit();
#include "get_mem.h"
static void quit();
/*
* Definition of the Application resources structure.
*/
typedef struct _XLoadResources {
Boolean show_label;
Boolean use_lights;
} XLoadResources;
/*
* Command line options table. Only resources are entered here...there is a
* pass over the remaining options after XtParseCommand is let loose.
*/
static XrmOptionDescRec options_mem[] = {
2018-12-26 18:46:04 +00:00
{"-scale", "*mem.minScale", XrmoptionSepArg, NULL},
{"-update", "*mem.update", XrmoptionSepArg, NULL},
{"-hl", "*mem.highlight", XrmoptionSepArg, NULL},
{"-highlight", "*mem.highlight", XrmoptionSepArg, NULL},
{"-codecolor", "*mem.codecolor", XrmoptionSepArg, NULL},
{"-cachedcolor", "*mem.cachedcolor", XrmoptionSepArg, NULL},
{"-buffercolor", "*mem.buffercolor", XrmoptionSepArg, NULL},
{"-freecolor", "*mem.freecolor", XrmoptionSepArg, NULL},
{"-swapcolor", "*mem.swapcolor", XrmoptionSepArg, NULL},
{"-jumpscroll", "*mem.jumpScroll", XrmoptionSepArg, NULL},
{"-label", "*label.label", XrmoptionSepArg, NULL},
{"-nolabel", "*showLabel", XrmoptionNoArg, "False"},
{"-lights", "*useLights", XrmoptionNoArg, "True"},
2017-02-18 21:03:44 +00:00
};
/*
* The structure containing the resource information for the
* Xload application resources.
*/
#define Offset(field) (XtOffsetOf(XLoadResources, field))
static XtResource my_resources_mem[] = {
{"showLabel", XtCBoolean, XtRBoolean, sizeof(Boolean),
2018-12-26 18:46:04 +00:00
Offset(show_label), XtRImmediate, (XtPointer) TRUE},
2017-02-18 21:03:44 +00:00
};
#undef Offset
static XLoadResources resources;
static XtActionsRec xload_actions[] = {
2018-12-26 18:46:04 +00:00
{ "quit", quit },
2017-02-18 21:03:44 +00:00
};
static Atom wm_delete_window;
/*
* Exit with message describing command line format.
*/
void usage(char *progname)
{
fprintf (stderr, "usage: %s [-options ...]\n\n", progname);
fprintf (stderr, "where options include:\n");
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -display dpy X server on which to display\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -geometry geom size and location of window\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -fn font font to use in label\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -update seconds interval between updates\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -label string annotation text\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -bg color background color\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -fg color text color\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -hl color scale color\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -codecolor color used code and stack memory color\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -cachedcolor color used cached memory color\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -buffercolor color used buffer memory color\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -freecolor color used free memory color\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -swapcolor color used swap memory color\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -nolabel removes the label from above the chart.\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
" -jumpscroll value number of pixels to scroll on overflow\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr,
2018-12-26 18:46:04 +00:00
"The reference line refers to the avaible installed ram\n");
2017-02-18 21:03:44 +00:00
fprintf (stderr, "\n");
exit(1);
}
2017-02-18 21:11:06 +00:00
int main(int argc, char **argv)
2017-02-18 21:03:44 +00:00
{
2018-12-26 18:46:04 +00:00
XtAppContext app_con;
Widget toplevel, load, pane, label_wid, load_parent;
Arg args[1];
Pixmap icon_pixmap = None;
char *label, host[256];
/* For security reasons, we reset our uid/gid after doing the necessary
system initialization and before calling any X routines. */
if (setgid(getgid()) == -1)
errx(1, "%s: setgid failed: %s\n", argv[0], strerror(errno));
if (setuid(getuid()) == -1)
errx(1, "%s: setuid failed: %s\n", argv[0], strerror(errno));
toplevel = XtAppInitialize(&app_con, "XMem", options_mem, XtNumber(options_mem),
&argc, argv, NULL, NULL, (Cardinal) 0);
if (argc != 1) usage(argv[0]);
XtGetApplicationResources( toplevel, (XtPointer) &resources,
my_resources_mem, XtNumber(my_resources_mem),
NULL, (Cardinal) 0);
/*
* This is a hack so that f.delete will do something useful in this
* single-window application.
*/
XtAppAddActions (app_con, xload_actions, XtNumber(xload_actions));
XtOverrideTranslations(toplevel,
XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()"));
XtSetArg (args[0], XtNiconPixmap, &icon_pixmap);
XtGetValues(toplevel, args, ONE);
if (icon_pixmap == None) {
XtSetArg(args[0], XtNiconPixmap,
XCreateBitmapFromData(XtDisplay(toplevel),
XtScreen(toplevel)->root,
(char *)xload_bits,
xload_width, xload_height));
XtSetValues (toplevel, args, ONE);
}
if (resources.show_label) {
pane = XtCreateManagedWidget ("paned", panedWidgetClass,
toplevel, NULL, ZERO);
label_wid = XtCreateManagedWidget ("label", labelWidgetClass,
pane, NULL, ZERO);
XtSetArg (args[0], XtNlabel, &label);
XtGetValues(label_wid, args, ONE);
if ( strcmp("label", label) == 0 ) {
(void) XmuGetHostname (host, 255);
XtSetArg (args[0], XtNlabel, host);
XtSetValues (label_wid, args, ONE);
}
load_parent = pane;
}
else
load_parent = toplevel;
load = XtCreateManagedWidget ("mem", memStripChartWidgetClass,
load_parent, NULL, ZERO);
XtAddCallback(load, XtNgetValue, (void*)GetMemLoadPoint, NULL);
XtRealizeWidget (toplevel);
wm_delete_window = XInternAtom (XtDisplay(toplevel), "WM_DELETE_WINDOW",
False);
(void) XSetWMProtocols (XtDisplay(toplevel), XtWindow(toplevel),
&wm_delete_window, 1);
#if __OpenBSD__
if (pledge("ps vminfo stdio", NULL) == -1)
errx(1, "pledge failed: %s", strerror(errno));
#endif
XtAppMainLoop(app_con);
return 0;
2017-02-18 21:03:44 +00:00
}
2017-02-18 21:11:06 +00:00
static void quit (Widget w, XEvent *event, String *params, Cardinal *num_params)
2017-02-18 21:03:44 +00:00
{
2018-12-26 18:46:04 +00:00
if (event->type == ClientMessage &&
event->xclient.data.l[0] != wm_delete_window) {
XBell (XtDisplay(w), 0);
return;
}
XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
exit (0);
2017-02-18 21:03:44 +00:00
}