added some test results
[tridge/junkcode.git] / xkeys.c
1 #ifdef XWINDOWS
2
3 #ifdef __TURBOC__
4 #include "tcinc.h"
5 #else
6 #include <stdio.h>
7 #include <math.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <ctype.h>
11 #include <sys/types.h>
12 #include <X11/Xos.h>
13 #include <X11/Xlib.h>
14 #include <X11/Xutil.h>
15 #include <X11/keysym.h>
16 #endif
17
18 #define EXTERN extern
19 #include "bug.h"
20
21
22 Display *Dpy=NULL;
23 Window ww=0;
24
25
26 /*******************************************************************
27 send one keysym to a window
28 ********************************************************************/
29 int send_key(Display *dpy,Window w,int state,int keysym)
30 {
31   static XKeyEvent event;
32   static count=0;
33   event.serial = count++;
34   event.type = 2;
35   event.send_event = 1;
36   event.x = event.y = 0;
37   event.x_root = event.y_root = 0;
38   event.same_screen = 1;
39   event.time = 0;
40   event.window = w;
41   event.subwindow = w;
42   event.root = 0;
43   event.state = state;
44   event.keycode = XKeysymToKeycode(dpy,keysym);
45   XSendEvent(dpy,w, True, KeyPressMask, &event);
46   return 0;
47 }
48
49
50 /*******************************************************************
51 find a window that matches a name
52 ********************************************************************/
53 int find_window(Display *dpy, Window w,char *match)
54 {
55   Window root, parent;
56   unsigned int nchildren;
57   Window *children = NULL;
58   int n;
59   char *name = NULL;
60   sstring aname;
61   
62   XFetchName (dpy, w, &name);
63
64   if (!name)
65     {
66       XWindowAttributes wattr;
67       XGetWindowAttributes(dpy,w,&wattr); /* do check return - Linus */
68       sprintf(aname,"%dx%d",wattr.width,wattr.height);
69       name = aname;
70     }
71
72   if (name) Debug(3,"Found window '%s'\n",name);
73   
74   if (name && strncmp(name,match,strlen(match)) == 0)
75     {
76       Debug(1,"matched %s to %s\n",name,match);
77       if (name && name != aname) XFree ((char *) name);
78       if (children) XFree ((char *) children);
79       return(w);
80     }
81
82   if (name && name != aname) XFree ((char *) name);   
83   
84   if (XQueryTree (dpy, w, &root, &parent, &children, &nchildren))
85     for (n = 0; n < nchildren; n++)
86       {
87         Window Ww = find_window (dpy, children[n],match);
88         if (Ww != 0) return(Ww);
89       }
90   if (children) XFree ((char *) children);
91   return 0;
92 }
93
94
95
96 /*******************************************************************
97 open a window for writing keystrokes to
98 ********************************************************************/
99 FN BOOL X_open_window(char *match)
100 {
101   Dpy = XOpenDisplay (NULL);
102   if (!Dpy) {
103     Error("unable to open display \"%s\"\n",XDisplayName (NULL));
104     return False;
105   }
106
107   ww = find_window (Dpy, RootWindow(Dpy, DefaultScreen(Dpy)),match);
108
109   if (!ww)
110     {
111       Error("can't find window matching %s\n",match);
112       return False;
113     }
114
115   return True;
116 }
117
118
119 /*******************************************************************
120 close the keystroke window
121 ********************************************************************/
122 FN void X_close_window(void)
123 {
124   if (Dpy)
125     XCloseDisplay(Dpy);
126   Dpy = NULL;
127   ww = 0;
128 }
129
130
131 /*******************************************************************
132 send a string to a window
133 ********************************************************************/
134 FN BOOL X_send_string(char *str)
135 {
136   char s[1024];
137   static int state = 0;
138   char *tok;
139
140   if (!Dpy || !ww) 
141     return(False);
142
143   strcpy(s,str);
144   
145   tok = strtok(s," \t\n\r");
146   do
147     {
148       int keysym = XStringToKeysym(tok);
149       if (strlen(tok) == 1 && *tok>='A' && *tok <='Z')
150         {
151           state ^= ShiftMask;     
152           send_key(Dpy,ww,state,XK_Shift_L);
153           send_key(Dpy,ww,state,keysym);
154           state ^= ShiftMask;     
155           send_key(Dpy,ww,state,XK_Shift_L);
156         }      
157       else
158         {
159           if (IsModifierKey(keysym))       
160             {
161               Debug(3,"modifier %d\n",keysym);
162               switch (keysym)
163                 {
164                 case XK_Shift_L:
165                 case XK_Shift_R:
166                   state ^= ShiftMask;
167                   break;
168                 case XK_Control_L:
169                 case XK_Control_R:
170                   state ^= ControlMask;
171                   break;
172                 case XK_Alt_L:
173                 case XK_Alt_R:
174                   state ^= Mod1Mask;
175                   break;
176                 }
177             }
178           send_key(Dpy,ww,state,keysym);
179         }
180     }
181   while ((tok = strtok(NULL," \t\n\r")));
182
183   XFlush(Dpy);
184   return True;
185 }
186
187 #endif