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

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