home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume10 / xt-examples / part01 / xmonitor.c < prev   
Encoding:
C/C++ Source or Header  |  1990-11-04  |  3.7 KB  |  141 lines

  1. /***********************************************************
  2. Copyright 1990 by Digital Equipment Corporation, Maynard, Massachusetts.
  3.  
  4.                         All Rights Reserved
  5.  
  6. Permission to use, copy, modify, and distribute these examples for any
  7. purpose and without fee is hereby granted, provided that the above
  8. copyright notice appear in all copies and that both that copyright
  9. notice and this permission notice appear in supporting documentation,
  10. and that the name of Digital not be used in advertising or publicity
  11. pertaining to distribution of the software without specific, written
  12. prior permission.
  13.  
  14. DIGITAL AND THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
  15. SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  16. FITNESS, IN NO EVENT SHALL DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT
  17. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  18. OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  19. OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  20. OR PERFORMANCE OF THIS SOFTWARE.
  21.  
  22. ******************************************************************/
  23.  
  24. #include <stdio.h>
  25. #include <X11/Intrinsic.h>
  26. #include <X11/StringDefs.h>
  27. #include "Graph.h"
  28. #include "BarDisplay.h"
  29.  
  30. XtAppContext app;
  31. Widget graph;
  32. int *fields;
  33.  
  34. void Timer(), CreateGraphWidget(), GetGraphData();
  35. typedef struct {
  36.     int timeout;
  37.     int num_fields;
  38.     String command;
  39. } OptionsRec;
  40.  
  41. OptionsRec options;
  42.  
  43. #define Offset(field) XtOffsetOf(OptionsRec, field)
  44.  
  45. XtResource resources[] = {
  46.     {"interval", "Interval", XtRInt, sizeof(int),
  47.     Offset(timeout), XtRImmediate, (XtPointer) 5},
  48.     {"numFields", "NumFields", XtRInt, sizeof(int),
  49.     Offset(num_fields), XtRImmediate, (XtPointer) 3},
  50.     {"command", "Command", XtRString, sizeof(String),
  51.     Offset(command), XtRString,
  52.     "vmstat 1 2 | awk '{if (NR == 4) print $(NF-2), $(NF-1), $(NF)}'"}
  53. };
  54.  
  55. #undef Offset
  56.  
  57. XrmOptionDescRec optionDesc[] = {
  58.     {"-interval", "*interval", XrmoptionSepArg, (XtPointer) NULL}
  59. };
  60.  
  61. main(argc, argv)
  62.     int argc;
  63.     char *argv[];
  64. {
  65.     Widget toplevel;
  66.  
  67.     toplevel = XtAppInitialize(&app, "DemoMonitor",
  68.         optionDesc, XtNumber(optionDesc), &argc, argv,
  69.             (String *) NULL, (ArgList) NULL, 0);
  70.  
  71.     XtGetApplicationResources(toplevel, (XtPointer) &options,
  72.         resources, XtNumber(resources), (Arg *) NULL, 0);
  73.  
  74.     fields = (int *) XtCalloc(options.num_fields, sizeof(int));
  75.  
  76.     CreateGraphWidget(toplevel);
  77.     XtRealizeWidget(toplevel);
  78.  
  79.     (void) XtAppAddTimeOut(app, options.timeout * 1000,
  80.         Timer, (XtPointer) NULL);
  81.  
  82.     XtAppMainLoop(app);
  83. }
  84.  
  85. void CreateGraphWidget(parent)
  86.     Widget parent;
  87. {
  88.     Arg args[10];
  89.     int n;
  90.  
  91.     GetGraphData();
  92.  
  93.     n = 0;
  94.     XtSetArg(args[n], XtNnumEntries, options.num_fields);   n++;
  95.     XtSetArg(args[n], XtNvalues, fields);            n++;
  96.     graph = XtCreateManagedWidget("graph", graphWidgetClass, parent,
  97.         args, n);
  98.  
  99.     (void) XtCreateWidget("bar", barDisplayObjectClass, graph,
  100.         (Arg *) NULL, 0);
  101. }
  102.  
  103. void Timer(client_data, id)
  104.     XtPointer client_data;
  105.     XtIntervalId *id;
  106. {
  107.     Arg args[10];
  108.     int n;
  109.  
  110.     GetGraphData();
  111.  
  112.     n = 0;
  113.     XtSetArg(args[n], XtNvalues, fields);    n++;
  114.     XtSetValues(graph, args, n);
  115.  
  116.     (void) XtAppAddTimeOut(app, options.timeout * 1000,
  117.         Timer, (XtPointer) NULL);
  118. }
  119.  
  120. void GetGraphData()
  121. {
  122.     int status;
  123.     FILE *f;
  124.     int i;
  125.  
  126.     f = popen(options.command, "r");
  127.  
  128.     for (i = 0; i < options.num_fields; i++) {
  129.     status = fscanf(f, "%d", &fields[i]);
  130.     if (status != 1) {
  131.             XtAppWarningMsg(app, "noData", "getGraphData",
  132.                     "DemoLoadError", "Not enough fields in data",
  133.             (String *) NULL, (Cardinal *) NULL);
  134.         for (; i < options.num_fields; i++) fields[i] = 0;
  135.         break;
  136.     }
  137.     }
  138.  
  139.     pclose(f);
  140. }
  141.