00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include <stdio.h>
00042 #include <stdlib.h>
00043 #include <unistd.h>
00044 #include <X11/Intrinsic.h>
00045 #include <X11/Xatom.h>
00046 #include <X11/StringDefs.h>
00047 #include <X11/Shell.h>
00048
00049 #include <X11/Xaw/Cardinals.h>
00050 #include <X11/Xaw/Label.h>
00051 #include <X11/Xaw/Paned.h>
00052 #include <X11/Xmu/SysUtil.h>
00053 #include "UnitStripChart.h"
00054 #include "xmem.h"
00055
00056 #include "xmem.bit"
00057
00058 char *ProgramName;
00059
00060 static void quit(Widget w, XEvent *event, String *params, Cardinal *num_params);
00061
00062
00063
00064
00065
00066
00067 static XrmOptionDescRec options[] = {
00068 {"-scale", "*mem.minScale", XrmoptionSepArg, NULL},
00069 {"-update", "*mem.update", XrmoptionSepArg, NULL},
00070 {"-hl", "*mem.highlight", XrmoptionSepArg, NULL},
00071 {"-highlight", "*mem.highlight", XrmoptionSepArg, NULL},
00072 {"-label", "*label.label", XrmoptionSepArg, NULL},
00073 {"-nolabel", "*showLabel", XrmoptionNoArg, "False"},
00074 {"-showlabel", "*showLabel", XrmoptionNoArg, "True"},
00075 {"-jumpscroll", "*mem.jumpScroll", XrmoptionSepArg, NULL},
00076 {"-watch-total","*memTotal", XrmoptionSepArg, NULL},
00077 {"-watch-used", "*memUsed", XrmoptionSepArg, NULL},
00078 {"-watch-free", "*memFree", XrmoptionSepArg, NULL},
00079 {"-units", "*mem.units", XrmoptionSepArg, "B"},
00080 {"-unit-fn", "*mem.fontSet", XrmoptionSepArg, NULL},
00081 };
00082
00083
00084
00085
00086
00087
00088 #define Offset(field) (XtOffsetOf(XMemResources, field))
00089
00090 static XtResource my_resources[] = {
00091 {"showLabel", XtCBoolean, XtRBoolean, sizeof(Boolean),
00092 Offset(show_label), XtRImmediate, (XtPointer) TRUE},
00093 };
00094
00095 #undef Offset
00096
00097 XMemResources xmem_resources;
00098
00099 static XtActionsRec xmem_actions[] = {
00100 { "quit", quit },
00101 };
00102 static Atom wm_delete_window;
00103
00104
00105
00106
00107
00108 static void usage(void)
00109 {
00110 char *text[] = {
00111 "usage: %s [-options ...]",
00112 "",
00113 "where options include:",
00114 " -display dpy X server on which to display",
00115 " -geometry geom Size and location of window",
00116 " -fn font Font to use in label",
00117 " -scale number Minimum number of scale lines",
00118 " -update seconds Interval between updates",
00119 " -label string Annotation text",
00120 " -bg color Background color",
00121 " -fg color Graph color",
00122 " -hl color Scale and text color",
00123 " -nolabel Removes the label from above the chart.",
00124 " -showlabel Adds the label above the chart.",
00125 " -jumpscroll value Number of pixels to scroll on overflow.",
00126 " -units value Units measurement (default: B)",
00127 " -unit-fn font Font to use in units",
00128 "",
00129 0
00130 };
00131 int line = 0;
00132
00133 for (line = 0; text[line] != 0; line++) {
00134 fprintf(stderr, text[line], ProgramName);
00135 fprintf(stderr, "\n");
00136 }
00137
00138 exit(1);
00139 }
00140
00141 int
00142 main(int argc, char **argv)
00143 {
00144 XtAppContext app_con;
00145 Widget toplevel, mem, pane, label_wid, mem_parent;
00146 Arg args[1];
00147 Pixmap icon_pixmap = None;
00148 char *label, host[256], *units;
00149 int count;
00150
00151 ProgramName = argv[0];
00152
00153
00154
00155 InitMemPoint();
00156 setgid(getgid());
00157 setuid(getuid());
00158
00159 XtSetLanguageProc(NULL, (XtLanguageProc) NULL, NULL);
00160
00161 toplevel =
00162 XtAppInitialize(
00163 &app_con, "XMem", options, XtNumber(options),
00164 &argc, argv, NULL, NULL, (Cardinal) 0
00165 );
00166 if (argc != 1)
00167 usage();
00168
00169 XtGetApplicationResources(
00170 toplevel,
00171 (XtPointer)&xmem_resources,
00172 my_resources, XtNumber(my_resources),
00173 NULL, (Cardinal)0
00174 );
00175
00176
00177
00178
00179
00180 XtAppAddActions (app_con, xmem_actions, XtNumber(xmem_actions));
00181 XtOverrideTranslations(toplevel,
00182 XtParseTranslationTable ("<Message>WM_PROTOCOLS: quit()")
00183 );
00184
00185 XtSetArg (args[0], XtNiconPixmap, &icon_pixmap);
00186 XtGetValues(toplevel, args, ONE);
00187 if (icon_pixmap == None) {
00188 XtSetArg(args[0], XtNiconPixmap,
00189 XCreateBitmapFromData(XtDisplay(toplevel),
00190 XtScreen(toplevel)->root,
00191 (char *)xmem_bits,
00192 xmem_width, xmem_height)
00193 );
00194 XtSetValues (toplevel, args, ONE);
00195
00196 if (xmem_resources.show_label) {
00197 pane =
00198 XtCreateManagedWidget("paned", panedWidgetClass, toplevel, NULL, ZERO);
00199
00200 label_wid =
00201 XtCreateManagedWidget("label", labelWidgetClass, pane, NULL, ZERO);
00202
00203 XtSetArg (args[0], XtNlabel, &label);
00204 XtGetValues(label_wid, args, ONE);
00205
00206 if (strcmp("label", label) == 0) {
00207 (void)XmuGetHostname (host, 255);
00208 XtSetArg(args[0], XtNlabel, host);
00209 XtSetValues(label_wid, args, ONE);
00210 }
00211
00212 mem_parent = pane;
00213 }
00214 else
00215 mem_parent = toplevel;
00216
00217 mem =
00218 XtCreateManagedWidget("mem", unitStripChartWidgetClass,
00219 mem_parent, NULL, ZERO);
00220
00221 XtSetArg(args[0], XtNunits, &units);
00222 XtGetValues(mem, args, ONE);
00223 if (strcmp("", units) == 0) {
00224 XtSetArg(args[0], XtNunits, "B");
00225 XtSetValues(mem, args, ONE);
00226 }
00227
00228 XtAddCallback(mem, XtNgetValue, GetMemPoint, NULL);
00229
00230 XtRealizeWidget(toplevel);
00231 wm_delete_window =
00232 XInternAtom(XtDisplay(toplevel), "WM_DELETE_WINDOW", False);
00233 (void)XSetWMProtocols (XtDisplay(toplevel), XtWindow(toplevel),
00234 &wm_delete_window, 1);
00235 }
00236 XtAppMainLoop(app_con);
00237
00238 return 0;
00239 }
00240
00241 static void quit (w, event, params, num_params)
00242 Widget w;
00243 XEvent *event;
00244 String *params;
00245 Cardinal *num_params;
00246 {
00247 if (event->type == ClientMessage &&
00248 event->xclient.data.l[0] != wm_delete_window)
00249 {
00250 XBell (XtDisplay(w), 0);
00251 return;
00252 }
00253 XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
00254 exit (0);
00255 }
00256
00257
00258
00259
00260
00261