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