home *** CD-ROM | disk | FTP | other *** search
- /*
- * Xmon : Graphically monitor a list
- *
- * George Ferguson, ferguson@cs.rochester.edu, 4 May 1990.
- *
- * $Id: xmon.c,v 1.4 90/08/21 11:14:39 ferguson Exp $
- *
- */
- static char *rcsid = "$Id: xmon.c,v 1.4 90/08/21 11:14:39 ferguson Exp $";
- #include <stdio.h>
- #include <ctype.h>
- #include <X11/Intrinsic.h>
- #include <X11/StringDefs.h>
- #include <X11/Xaw/Label.h>
- #include <X11/Xaw/Toggle.h>
- #include <X11/Xaw/Cardinals.h>
- #include "app-resources.h"
- extern WidgetClass classNameToWidgetClass();
- extern char *getTextFromWidget();
-
- /* - - - - - - - - */
- /*
- * Functions defined in this file:
- */
- static void initGraphics(), initWidgets();
- static void quit(), update(), info(), reinit(), cd(), cmd();
- static void timeoutProc();
- static void syntax();
- void fail();
-
- /*
- * Action binding table
- */
- static XtActionsRec cmdActionsTable[] = {
- { "xmon-update", update },
- { "xmon-quit", quit },
- { "xmon-info", info },
- { "xmon-reinit", reinit },
- { "xmon-cd", cd },
- { "xmon-cmd", cmd },
- };
-
- /*
- * Global widget data
- */
- Widget toplevel,faceBox,infoLabel,radioGroup;
- AppResources appResources;
- static XtAppContext app_con;
- static XtIntervalId interval;
-
- /*
- * Global graphics data
- */
- GC gc;
-
- /*
- * Other global data
- */
- char *program;
-
- /*
- * Non-widget resources obtained from resource manager
- */
- static XtResource resources[] = {
- { "addNames", "AddNames", XtRBoolean, sizeof(Boolean),
- XtOffset(AppResources *,addNames), XtRImmediate, False },
- { "opaqueNames", "OpaqueNames", XtRBoolean, sizeof(Boolean),
- XtOffset(AppResources *,opaqueNames), XtRImmediate, False },
- { "smallFont", "SmallFont", XtRFont, sizeof(Font),
- XtOffset(AppResources *,smallFont), XtRString, SMALLFONT },
- { "updateInterval", "UpdateInterval", XtRInt, sizeof(int),
- XtOffset(AppResources *,updateInterval), XtRImmediate, (XtPointer)60 },
- { "faceDirectory", "FaceDirectory", XtRString, sizeof(String),
- XtOffset(AppResources *,faceDirectory), XtRImmediate, FACEDIR },
- { "updateCommand", "UpdateCommand", XtRString, sizeof(String),
- XtOffset(AppResources *,updateCommand), XtRImmediate, UPDATECOMMAND },
- { "updateCursor", "UpdateCursor", XtRCursor, sizeof(Cursor),
- XtOffset(AppResources *,updateCursor), XtRString, UPDATECURSOR },
- { "nameCommand", "nameCommand", XtRString, sizeof(String),
- XtOffset(AppResources *,nameCommand), XtRImmediate, NULL },
- { "debug", "Debug", XtRBoolean, sizeof(Boolean),
- XtOffset(AppResources *,debug), XtRImmediate, False },
- { "revision", "Revision", XtRString, sizeof(String),
- XtOffset(AppResources *,revision), XtRImmediate, "" },
- { "widgets", "Widgets", XtRString, sizeof(String),
- XtOffset(AppResources *,widgets), XtRImmediate, "" },
- };
-
- /*
- * Non-widget resources settable on command line.
- */
- static XrmOptionDescRec options[] = {
- { "-addNames", ".addNames", XrmoptionNoArg, "True" },
- { "-opaqueNames", ".opaqueNames", XrmoptionNoArg, "True" },
- { "-smallFont", ".smallFont", XrmoptionSepArg, SMALLFONT },
- { "-updateInterval",".updateInterval",XrmoptionSepArg, "60" },
- { "-faceDirectory", ".faceDirectory",XrmoptionSepArg, FACEDIR },
- { "-updateCommand", ".updateCommand",XrmoptionSepArg, UPDATECOMMAND},
- { "-nameCommand", ".nameCommand", XrmoptionSepArg, NULL},
- { "-debug", ".debug", XrmoptionNoArg, "True"},
- };
-
- /*
- * Widget and non-widget resources if the application defaults
- * file can't be found.
- * [ Generated automatically from Xmon.ad. ]
- */
- static String fallbackResources[] = {
- #include "Xmon.ad.h"
- NULL
- };
-
- /* - - - - - - - - */
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- program = *argv;
- initGraphics(argc,argv);
- initWidgets();
- initMemory();
- XtRealizeWidget(toplevel);
- updateFaces();
- if (appResources.updateInterval != 0)
- interval = XtAppAddTimeOut(app_con,appResources.updateInterval*1000,
- timeoutProc,NULL);
- XtAppMainLoop(app_con);
- }
-
- static void
- initGraphics(argc,argv)
- int argc;
- char **argv;
- {
- XGCValues gcvals;
- char *rev,*strchr();
-
- toplevel = XtAppInitialize(&app_con, "Xmon",
- options, XtNumber(options),
- &argc,argv,fallbackResources,NULL,ZERO);
- XtGetApplicationResources(toplevel,(XtPointer)&appResources,
- resources,XtNumber(resources),NULL,ZERO);
- rev = strchr(fallbackResources[0],'$');
- if (strcmp(appResources.revision,rev) != 0) {
- fprintf(stderr,"%s: app-defaults release not %s\n",program,rev);
- fprintf(stderr,"%s: you may have an outdated app-defaults file\n",
- program);
- }
- XtAppAddActions(app_con,cmdActionsTable,XtNumber(cmdActionsTable));
- if (argc != 1) {
- syntax(argc,argv);
- XtDestroyApplicationContext(app_con);
- exit(1);
- }
- gc = XDefaultGC(XtDisplay(toplevel),
- XScreenNumberOfScreen(XtScreen(toplevel)));
- XSetFont(XtDisplay(toplevel),gc,appResources.smallFont);
- }
-
- static void
- initWidgets()
- {
- char name[32],class[32],parent[32];
- char *s;
- int i;
- Boolean isShell;
- WidgetClass wc;
- Widget w;
-
- if ((s=appResources.widgets) == NULL)
- fail("no widgets specified!\n","");
- while (*s) {
- while (isspace(*s))
- s += 1;
- if (!*s)
- break;
- i = 0;
- while (*s && !isspace(*s))
- class[i++] = *s++;
- class[i] = '\0';
- while (isspace(*s))
- s += 1;
- i = 0;
- while (*s && !isspace(*s))
- name[i++] = *s++;
- name[i] = '\0';
- while (isspace(*s))
- s += 1;
- i = 0;
- while (*s && !isspace(*s))
- parent[i++] = *s++;
- parent[i] = '\0';
- isShell = False;
- if ((wc=classNameToWidgetClass(class,&isShell)) == NULL)
- fail("can't convert string \"%s\" to widgetClass\n",class);
- if (strcmp(parent,"toplevel") == 0)
- w = toplevel;
- else if ((w=XtNameToWidget(toplevel,parent)) == NULL)
- fail("can't convert string \"%s\" to widget\n",parent);
- if (isShell)
- w = XtCreatePopupShell(name,wc,w,NULL,ZERO);
- else
- w = XtCreateManagedWidget(name,wc,w,NULL,ZERO);
- }
- if ((faceBox=XtNameToWidget(toplevel,"*faceBox")) == NULL)
- fail("didn't create widget \"faceBox\"\n","");
- if ((infoLabel=XtNameToWidget(toplevel,"*infoLabel")) == NULL)
- fail("didn't create widget \"infoLabel\"\n","");
- }
-
- /* - - - - - - - - */
- /* Action procedures */
-
- /*
- * Default action for "quit" button: destroys application.
- */
- static void
- quit(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- XtDestroyApplicationContext(app_con);
- exit(0);
- }
-
- /*
- * Default action for all "face" buttons: sets infoLabel.
- */
- static void
- info(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- Arg arg[1];
-
- if ((w=(Widget)XawToggleGetCurrent(radioGroup)) == NULL)
- XtSetArg(arg[0],XtNlabel,"");
- else
- XtSetArg(arg[0],XtNlabel,getTextFromWidget(w));
- XtSetValues(infoLabel,arg,ONE);
- }
-
- /*
- * Default action for "update" button: Sets all buttons and resets timer.
- */
- static void
- update(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- updateFaces();
- if (appResources.updateInterval != NULL) {
- XtRemoveTimeOut(interval);
- interval = XtAppAddTimeOut(app_con,appResources.updateInterval*1000,
- timeoutProc,NULL);
- }
- }
-
- /*
- * Forget the bitmaps
- */
- static void
- reinit(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- if (radioGroup != NULL)
- XawToggleUnsetCurrent(radioGroup);
- clearBitmapCache();
- update(w,event,params,num_params);
- }
-
- /*
- * Change directory
- */
- static void
- cd(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- char *text,dirname[256];
- int i;
-
- text = getTextFromWidget((Widget)XawToggleGetCurrent(radioGroup));
- if (text == NULL)
- return;
- i = 0;
- while (*text && !isspace(*text) && i < 255)
- dirname[i++] = *text++;
- dirname[i] = '\0';
- if (chdir(dirname) < 0)
- fprintf(stderr,"%s: couldn't cd to \"%s\"\n",program,dirname);
- }
-
- /*
- * General action function for user translations
- * Only test for a selection if the fields are needed.
- */
- static char buf[1024];
-
- static void cmd(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- char *s,*t,*text;
- int i,field,f,seenDollar,hadBracket;
-
- if (*num_params < ONE)
- return;
- text = getTextFromWidget((Widget)XawToggleGetCurrent(radioGroup));
- s = *params;
- i = 0;
- seenDollar = 0;
- while (*s) {
- if (*s == '$') {
- if (seenDollar) {
- buf[i++] = *s;
- seenDollar = False;
- } else {
- seenDollar = True;
- }
- s += 1;
- } else if (seenDollar && (*s == '{' || isdigit(*s))) {
- if (text == NULL)
- return;
- field = 0;
- if ((hadBracket = (*s == '{')))
- s += 1;
- while (*s && isdigit(*s))
- field = field * 10 + *s++ - '0';
- if (hadBracket) {
- if (*s != '}') {
- fprintf(stderr,"bad field in cmd: \"%s\"\n",*params);
- return;
- } else
- s += 1;
- }
- if (field == 0) {
- t = text;
- while (*t)
- buf[i++] = *t++;
- } else {
- t = text;
- f = 1;
- while (*t && f < field)
- if (isspace(*t)) {
- while (*t && isspace(*t))
- t += 1;
- f += 1;
- } else
- t += 1;
- if (*t != '\0')
- while (*t && !isspace(*t))
- buf[i++] = *t++;
- }
- seenDollar = False;
- } else {
- if (seenDollar) {
- buf[i++] = '$';
- seenDollar = False;
- }
- buf[i++] = *s++;
- }
- }
- if (seenDollar)
- buf[i++] = '$'; /* final $ if needed */
- buf[i] = '\0';
- if (appResources.debug)
- printf("cmd: \"%s\"\n",buf);
- else
- system(buf);
- }
-
- /* - - - - - - - - */
- /*
- * Callback for timer: Sets all buttons and schedules next update.
- */
- static void
- timeoutProc(client_data,id)
- XtPointer client_data;
- XtIntervalId *id;
- {
- updateFaces();
- interval = XtAppAddTimeOut(app_con,appResources.updateInterval*1000,
- timeoutProc,NULL);
- }
-
- /* - - - - - - - - */
- /*
- * syntax() : print the usage message.
- */
- static void
- syntax(argc,argv)
- int argc;
- char **argv;
- {
- argv += 1;
- if (argc == 2)
- fprintf(stderr,"%s: bad argument: %s\n",program,*argv);
- else {
- fprintf(stderr,"%s: bad arguments: ",program);
- while (--argc)
- fprintf(stderr,"%s ",*argv++);
- fprintf(stderr,"\n");
- }
- fprintf(stderr,"usage: %s\t-addNames\t\tadd labels to icons\n",program);
- fprintf(stderr,"\t\t-opaqueNames\t\tprint labels opaquely\n");
- fprintf(stderr,"\t\t-updateInterval N\tseconds between updates\n");
- fprintf(stderr,"\t\t-faceDirectory dir\tlocation of icons\n");
- fprintf(stderr,"\t\t-updateCommand cmd\tcommand to monitor\n");
- fprintf(stderr,"\t\t-nameCommand cmd\tcommand to map names to icons\n");
- fprintf(stderr,"\t\t-debug\t\t\tprint command instead of executing\n");
- }
-
- /*
- * fail() : Print a message and die.
- */
- void
- fail(fmt,arg)
- char *fmt,*arg;
- {
- fprintf(stderr,fmt,arg);
- XtDestroyApplicationContext(app_con);
- exit(1);
- }
-
-