xnet.c

Go to the documentation of this file.
00001 /******************************************************************************
00002  * Shamelessly ripped and modified from the below sources:
00003  ******************************************************************************/
00004 
00005 /* $XConsortium: xload.c,v 1.37 94/04/17 20:43:44 converse Exp $ */
00006 /* $XFree86: xc/programs/xload/xload.c,v 1.7 2002/09/18 17:11:57 tsi Exp $ */
00007 /*
00008 
00009 Copyright (c) 1989  X Consortium
00010 
00011 Permission is hereby granted, free of charge, to any person obtaining
00012 a copy of this software and associated documentation files (the
00013 "Software"), to deal in the Software without restriction, including
00014 without limitation the rights to use, copy, modify, merge, publish,
00015 distribute, sublicense, and/or sell copies of the Software, and to
00016 permit persons to whom the Software is furnished to do so, subject to
00017 the following conditions:
00018 
00019 The above copyright notice and this permission notice shall be included
00020 in all copies or substantial portions of the Software.
00021 
00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00023 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00024 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00025 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
00026 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00027 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00028 OTHER DEALINGS IN THE SOFTWARE.
00029 
00030 Except as contained in this notice, the name of the X Consortium shall
00031 not be used in advertising or otherwise to promote the sale, use or
00032 other dealings in this Software without prior written authorization
00033 from the X Consortium.
00034 
00035 */
00036 
00037 /*
00038  * xnet - display system network average in a window
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  * Command line options table.  Only resources are entered here...there is a
00068  * pass over the remaining options after XtParseCommand is let loose. 
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  * The structure containing the resource information for the
00085  * Xnet application resources.
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  * Exit with message describing command line format.
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     /* For security reasons, we reset our uid/gid after doing the necessary
00159        system initialization and before calling any X routines. */
00160     InitNetPoint();
00161     setgid(getgid());           /* reset gid first while still (maybe) root */
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                 fprintf(stderr, "resources.interface = %s\n", resources.interface);
00176                 fprintf(stderr, "resources.units = %s\n", resources.units);
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          * This is a hack so that f.delete will do something useful in this
00189          * single-window application.
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 

Generated on Thu Apr 27 14:28:40 2006 for xnet by  doxygen 1.4.2