11 #include <sys/types.h>
14 #include <X11/Xutil.h>
15 #include <X11/keysym.h>
26 /*******************************************************************
27 send one keysym to a window
28 ********************************************************************/
29 int send_key(Display *dpy,Window w,int state,int keysym)
31 static XKeyEvent event;
33 event.serial = count++;
36 event.x = event.y = 0;
37 event.x_root = event.y_root = 0;
38 event.same_screen = 1;
44 event.keycode = XKeysymToKeycode(dpy,keysym);
45 XSendEvent(dpy,w, True, KeyPressMask, &event);
50 /*******************************************************************
51 find a window that matches a name
52 ********************************************************************/
53 int find_window(Display *dpy, Window w,char *match)
56 unsigned int nchildren;
57 Window *children = NULL;
62 XFetchName (dpy, w, &name);
66 XWindowAttributes wattr;
67 XGetWindowAttributes(dpy,w,&wattr); /* do check return - Linus */
68 sprintf(aname,"%dx%d",wattr.width,wattr.height);
72 if (name) Debug(3,"Found window '%s'\n",name);
74 if (name && strncmp(name,match,strlen(match)) == 0)
76 Debug(1,"matched %s to %s\n",name,match);
77 if (name && name != aname) XFree ((char *) name);
78 if (children) XFree ((char *) children);
82 if (name && name != aname) XFree ((char *) name);
84 if (XQueryTree (dpy, w, &root, &parent, &children, &nchildren))
85 for (n = 0; n < nchildren; n++)
87 Window Ww = find_window (dpy, children[n],match);
88 if (Ww != 0) return(Ww);
90 if (children) XFree ((char *) children);
96 /*******************************************************************
97 open a window for writing keystrokes to
98 ********************************************************************/
99 FN BOOL X_open_window(char *match)
101 Dpy = XOpenDisplay (NULL);
103 Error("unable to open display \"%s\"\n",XDisplayName (NULL));
107 ww = find_window (Dpy, RootWindow(Dpy, DefaultScreen(Dpy)),match);
111 Error("can't find window matching %s\n",match);
119 /*******************************************************************
120 close the keystroke window
121 ********************************************************************/
122 FN void X_close_window(void)
131 /*******************************************************************
132 send a string to a window
133 ********************************************************************/
134 FN BOOL X_send_string(char *str)
137 static int state = 0;
145 tok = strtok(s," \t\n\r");
148 int keysym = XStringToKeysym(tok);
149 if (strlen(tok) == 1 && *tok>='A' && *tok <='Z')
152 send_key(Dpy,ww,state,XK_Shift_L);
153 send_key(Dpy,ww,state,keysym);
155 send_key(Dpy,ww,state,XK_Shift_L);
159 if (IsModifierKey(keysym))
161 Debug(3,"modifier %d\n",keysym);
170 state ^= ControlMask;
178 send_key(Dpy,ww,state,keysym);
181 while ((tok = strtok(NULL," \t\n\r")));