home *** CD-ROM | disk | FTP | other *** search
- /*
- * xse - an interface to XSendEvent()
- *
- * George Ferguson, ferguson@cs.rochester.edu, 1 Jun 1990.
- *
- * $Id: xse.c,v 1.4 90/08/15 11:32:30 ferguson Exp $
- *
- */
- static char *rcsid = "$Id: xse.c,v 1.4 90/08/15 11:32:30 ferguson Exp $";
- #include <stdio.h>
- #include <ctype.h>
- #include <X11/Intrinsic.h>
- #include <X11/StringDefs.h>
- #include <X11/Shell.h>
- #include <X11/Xaw/Form.h>
- #include <X11/Xaw/Command.h>
- #include <X11/Xaw/Cardinals.h>
- #include "app-resources.h"
- #include "translate.h"
- #include "parse.h"
-
- /* - - - - - - - - */
- /*
- * Functions defined in this file
- */
- static void initGraphics(),initWidgets();
- static void quit(),send();
- static void parseAndSendEvents(), fail();
- static Window windowFromString(), Window_With_Name(), Select_Window();
-
- /*
- * Action binding table
- */
- static XtActionsRec cmdActionsTable[] = {
- { "xse-quit", quit },
- { "xse-send", send },
- };
-
- /*
- * Global graphics data, needed in parse.c
- */
- Display *display;
- Window root;
-
- /*
- * Global widget data
- */
- static XtAppContext app_con;
- static Widget toplevel;
-
- /*
- * The application resources struct
- */
- static AppResources appResources;
-
- /*
- * Non-widget resources obtained from resource manager
- */
- static XtResource resources[] = {
- { "widgets", "Widgets", XtRString, sizeof(String),
- XtOffset(AppResources *,widgets), XtRImmediate, NULL },
- { "window", "Window", XtRString, sizeof(String),
- XtOffset(AppResources *,window), XtRImmediate, "InputFocus" },
- { "revision", "Revision", XtRString, sizeof(String),
- XtOffset(AppResources *,revision), XtRImmediate, "" },
- };
-
- /*
- * Non-widget resources set on command line.
- */
- static XrmOptionDescRec options[] = {
- { "-window", "window", XrmoptionSepArg, "InputFocus"},
- };
-
- /*
- * Widget and non-widget resources if the application defaults
- * file can't be found.
- * [ Generated automatically from Xse.ad. ]
- */
- static String fallbackResources[] = {
- #include "Xse.ad.h"
- NULL
- };
-
- /* - - - - - - - - */
- /*
- * main() : Initialize the graphics, then if args remain send them directly
- * (ie. we are in command mode) otherwise initialize the widgets
- * (ie. we are in widget mode) and call XtAppMainLoop().
- */
- main(argc, argv)
- int argc;
- char **argv;
- {
- Window win;
-
- initGraphics(&argc,argv);
- argv += 1;
- argc -= 1;
- if (argc > 0) {
- win = windowFromString(appResources.window);
- while (argc--)
- parseAndSendEvents(win,*argv++);
- XtDestroyApplicationContext(app_con);
- exit(0);
- }
- initWidgets();
- XtRealizeWidget(toplevel);
- XtAppMainLoop(app_con);
- }
-
- /*
- * initGraphics() : Initialize the application context and set global
- * graphics variables.
- */
- static void
- initGraphics(argcp,argv)
- int *argcp;
- char **argv;
- {
- toplevel = XtAppInitialize(&app_con, "Xse",
- options, XtNumber(options),
- argcp,argv,fallbackResources,NULL,ZERO);
- XtGetApplicationResources(toplevel,(XtPointer)&appResources,
- resources,XtNumber(resources),NULL,ZERO);
- display = XtDisplay(toplevel);
- root = RootWindowOfScreen(XtScreen(toplevel));
- XtAppAddActions(app_con,cmdActionsTable,XtNumber(cmdActionsTable));
- }
-
- /*
- * initWidgets() : Parse the widgets resource and create whatever widgets
- * specified in it as children of toplevel (or sub-children,
- * etc.).
- */
- 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);
- }
- }
-
- /* - - - - - - - - */
- /*
- * windowFromString() : Convert a string to a Window, handling our special
- * cases of PointerWindow, InputFocus, or ClickWindow.
- */
- static Window
- windowFromString(str)
- char *str;
- {
- Window w;
-
- if (strcmp(str,"PointerWindow") == 0)
- return(PointerWindow);
- else if (strcmp(str,"InputFocus") == 0)
- return(InputFocus);
- else if (strcmp(str,"ClickWindow") == 0)
- return(Select_Window(display));
- else if ((w=Window_With_Name(display,root,str)) != NULL)
- return(w);
- else
- return((Window)strtol(str,NULL,0));
- }
-
- /* - - - - - - - - */
- /* Action procedures */
-
- /*
- * quit() : Quit the tool.
- */
- static void
- quit(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- XtDestroyApplicationContext(app_con);
- exit(0);
- }
-
- /*
- * send() : With one argument, send the event sequence given by the first
- * argument to the default window. With two arguments, the first
- * is the window to send the event sequence given by the second
- * to.
- */
- static void
- send(w,event,params,num_params)
- Widget w;
- XEvent *event;
- String *params;
- Cardinal *num_params;
- {
- char *estr,*wstr;
-
- if (*num_params == ONE) {
- wstr = appResources.window;
- estr = params[0];
- } else if (*num_params == TWO) {
- wstr = params[0];
- estr = params[1];
- } else {
- fprintf(stderr,"xse: too many arguments to xse-send()\n");
- return;
- }
- parseAndSendEvents(windowFromString(wstr),estr);
- }
-
- /*
- * parseAndSendEvents() : Calls parseEventList() then dispatches the list
- * of returned events.
- */
- static void
- parseAndSendEvents(window,str)
- Window window;
- char *str;
- {
- char *s;
- EventListPtr list = NULL;
-
- s = parseEventList(str,&list);
- if (*s != '\0') {
- fprintf("xse: garbage at end of event spec: \"%s\"\n",s);
- return;
- }
- while (list != NULL) {
- if (list->event.xany.type != -1) {
- list->event.xany.display = display;
- list->event.xany.window = window;
- while (list->count-- != 0)
- XSendEvent(display,window,True,0xfff,&(list->event));
- }
- list = list->next;
- }
- freeEventList(&list);
- }
-
- /* - - - - - - - - */
- /*
- * fail() : Print a message and die.
- */
- static void
- fail(fmt,arg)
- char *fmt,*arg;
- {
- fprintf(stderr,fmt,arg);
- XtDestroyApplicationContext(app_con);
- exit(1);
- }
-
- /* - - - - - - - - */
- /* - - - - - - - - */
- /*
- * [These functions are from the file "dsimple.c" used with xwininfo.]
- *
- * Written by Mark Lillibridge. Last updated 7/1/87
- *
- * Send bugs, etc. to chariot@athena.mit.edu.
- *
- * Window_With_Name: routine to locate a window with a given name on a display.
- * If no window with the given name is found, 0 is returned.
- * If more than one window has the given name, the first
- * one found will be returned. Only top and its subwindows
- * are looked at. Normally, top should be the RootWindow.
- */
- static Window
- Window_With_Name(dpy, top, name)
- Display *dpy;
- Window top;
- char *name;
- {
- Window *children, dummy;
- unsigned int nchildren;
- int i;
- Window w=0;
- char *window_name;
-
- if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name))
- return(top);
-
- if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
- return(0);
-
- for (i=0; i<nchildren; i++) {
- w = Window_With_Name(dpy, children[i], name);
- if (w)
- break;
- }
- if (children) XFree ((char *)children);
- return(w);
- }
-
- /*
- * Routine to let user select a window using the mouse
- * gf: Doesn't need "screen" defined.
- */
- #include <X11/cursorfont.h>
-
- static Window
- Select_Window(dpy)
- Display *dpy;
- {
- static Cursor cursor = NULL;
- Window target_win = None, root = DefaultRootWindow(dpy);
- XEvent event;
- int status;
- int buttons = 0;
-
- if (cursor == NULL)
- cursor = XCreateFontCursor(dpy, XC_crosshair);
- /* Grab the pointer using target cursor, letting it room all over */
- status = XGrabPointer(dpy, root, False,
- ButtonPressMask|ButtonReleaseMask, GrabModeSync,
- GrabModeAsync, root, cursor, CurrentTime);
- if (status != GrabSuccess) {
- fprintf(stderr,"xse: can't grab pointer");
- return(None);
- }
- /* Let the user select a window... */
- while ((target_win == None) || (buttons != 0)) {
- /* allow one more event */
- XAllowEvents(dpy, SyncPointer, CurrentTime);
- XWindowEvent(dpy, root, ButtonPressMask|ButtonReleaseMask, &event);
- switch (event.type) {
- case ButtonPress:
- if (target_win == None) {
- target_win = event.xbutton.subwindow; /* window selected */
- if (target_win == None) target_win = root;
- }
- buttons++;
- break;
- case ButtonRelease:
- if (buttons > 0) /* there may have been some down before we started */
- buttons--;
- break;
- }
- }
- XUngrabPointer(dpy, CurrentTime); /* Done with pointer */
- return(target_win);
- }
-