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