xio.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  * xio - display system I/O 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/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  * Command line options table.  Only resources are entered here...there is a
00065  * pass over the remaining options after XtParseCommand is let loose. 
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  * The structure containing the resource information for the
00084  * Xio application resources.
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  * Exit with message describing command line format.
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         /* For security reasons, we reset our uid/gid after doing the necessary
00156            system initialization and before calling any X routines. */
00157         InitIoPoint();
00158         setgid(getgid());               /* reset gid first while still (maybe) root */
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          * This is a hack so that f.delete will do something useful in this
00180          * single-window application.
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 

Generated on Mon May 8 16:47:49 2006 for xmonitors by  doxygen 1.4.2