1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/cursorfont.h>
static char *display_name = NULL;
static Display *display;
static Window root;
static Window window = None;
static XKeyEvent event;
static char error_message[256];
static int error_status = 0;
static void
throw_exception3(char *msg, char *s, int i) {
sprintf(error_message, msg, s, i);
error_status = 1;
}
static void
throw_exception3s(char *msg1, char *msg2, char *msg3) {
sprintf(error_message, msg1, msg2, msg3);
error_status = 1;
}
static void
throw_exception(char *msg) {
strncpy(error_message,msg,256);
error_status = 1;
}
/* Added for window managers like swm and tvtwm that follow solbourne's
* virtual root window concept
*/
static Window
GetRootWindow (Display * disp, int scrn)
{
Atom __SWM_VROOT = None;
Window root, rootReturn, parentReturn, *children;
unsigned int numChildren;
unsigned i;
root = RootWindow (disp, scrn);
/* see if there is a virtual root */
__SWM_VROOT = XInternAtom (disp, "__SWM_VROOT", False);
XQueryTree (disp, root, &rootReturn, &parentReturn, &children, &numChildren);
for (i = 0; i < numChildren; i++)
{
Atom actual_type;
int actual_format;
unsigned long nitems, bytesafter;
Window *newRoot = (Window) 0;
if (XGetWindowProperty (disp, children[i], __SWM_VROOT, 0, 1,
False, XA_WINDOW, &actual_type, &actual_format,
&nitems, &bytesafter,
(unsigned char**) &newRoot) ==
Success && newRoot)
{
root = *newRoot;
break;
}
}
if (children)
XFree ((char *) children);
return (root);
}
/* - - - - - - - - -
* [These functions are from the file "dsimple.c" used with xwininfo.]
*
* Written by Mark Lillibridge. Last updated 7/1/87
*
*
* 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 Root Window.
*/
static Window
Window_With_Name (Display * dpy, Window top, char *name)
{
Window *children, dummy;
unsigned int nchildren;
unsigned 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);
}
static int
open_channel (char *wname)
{
/* display_name = ":0.0"; */
if ((display = XOpenDisplay (display_name)) == NULL)
{
throw_exception ("can't open display");
return 1;
}
if ((root = GetRootWindow (display, DefaultScreen (display))) == 0)
{
throw_exception ("Cannot get DefaultScreen");
return 1;
}
if ((wname[0] == '\0') && (window != None))
{} /* take selected window */
else if (wname[0] != '\0')
{
if ((window = Window_With_Name (display, root, wname)) == None)
{
throw_exception3s ("Display %s: can't open window named \"%s\"",
XDisplayName (display_name), wname);
return 1;
}
}
else
{
throw_exception3 ("bad condition in %s at line %d", __FILE__, __LINE__);
return 1;
}
event.type = KeyPress;
event.serial = 0;
event.send_event = False;
event.display = display;
event.x = event.y = event.x_root = event.y_root = 0;
event.time = CurrentTime;
event.same_screen = True;
event.subwindow = None;
event.window = window;
event.root = root;
return 0;
}
static void
close_channel ()
{
/* XFlush(display); */
XCloseDisplay (display);
}
static void
sendx_channel (KeySym ks, int km)
{
/* 0=regular, 1=shift, 2=lock, 4=control */
if (ks < 256)
{
event.state = isupper ((char) ks);
switch (ks)
{
case 0x08:
ks = XK_BackSpace;
break;
case 0x09:
ks = XK_Tab;
break;
case 0x0A:
ks = XK_Linefeed;
break;
case 0x0B:
ks = XK_Clear;
break;
case 0x0D:
ks = XK_Return;
break;
case 0x13:
ks = XK_Pause;
break;
case 0x14:
ks = XK_Scroll_Lock;
break;
case 0x1B:
ks = XK_Escape;
break;
}
}
else
event.state = 0;
event.type = KeyPress;
event.state = km ; // Mod1Mask
event.keycode = XKeysymToKeycode (display, ks);
if (XSendEvent (display, window, True, 0xfff, (XEvent *) & event) == 0)
throw_exception ("Error in XSendEvent");
event.type = KeyRelease;
if (XSendEvent (display, window, True, 0xfff, (XEvent *) & event) == 0)
throw_exception ("Error in XSendEvent");
return;
}
int
sendx_string (char *string, char *window)
{
char *p;
if (open_channel (window))
return error_status;
p = string;
while (*p)
sendx_channel (*p++,0);
close_channel ();
return error_status;
}
int
sendx_token (char *string, char *window)
{
if (open_channel (window))
return error_status;
sendx_channel (XStringToKeysym (string),0);
close_channel ();
return error_status;
}
int
sendx_alt_token (char *string, char *window)
{
if (open_channel (window))
return error_status;
sendx_channel (XStringToKeysym (string),Mod1Mask);
close_channel ();
return error_status;
}
int
sendx_controlalt_token (char *string, char *window)
{
if (open_channel (window))
return error_status;
sendx_channel (XStringToKeysym (string), Mod1Mask | ControlMask);
close_channel ();
return error_status;
}
int
sendx_control_token (char *string, char *window)
{
if (open_channel (window))
return error_status;
sendx_channel (XStringToKeysym (string),ControlMask);
close_channel ();
return error_status;
}
/*
void
PrintKeySyms ()
{
int i;
for (i = 32; i < 127; i++)
{
printf ("%s[%c] ", XKeysymToString (i), i);
}
for (i = 128 + 32; i < 128 + 127; i++)
{
printf ("%s[%c] ", XKeysymToString (i), i);
}
printf ("\n");
}
*/
|