| 1 |
/* |
| 2 |
* Modified by Jim Diamond ([email protected]) April 24, 2010 |
| 3 |
* to reset error_status to 0 at the beginning of open_channel(). |
| 4 |
* |
| 5 |
* Also replaced 0xfff with KeyPressMask and KeyReleaseMask (as |
| 6 |
* appropriate) in sendx_channel(); otherwise (at least in Slackware64 13.0) |
| 7 |
* the events also get sent to the terminal running pdfopen; this is a Bad |
| 8 |
* Thing. |
| 9 |
* |
| 10 |
* Also added set_focus() and reset_focus() functions. |
| 11 |
* |
| 12 |
* January 27, 2012 note: |
| 13 |
* NOTE: to make reset_focus() work, at least under FVWM2 on |
| 14 |
* Slackware64-13.37 when using AR9, I needed to call XOpenDisplay() |
| 15 |
* again (since close_channel() has generally been called since |
| 16 |
* set_focus() was called), I needed to sleep a bit (perhaps < 1 second |
| 17 |
* is fine, and perhaps other systems might need more, I dunno), and I |
| 18 |
* needed to call XFlush(). If someone who understands why the code |
| 19 |
* here opens and closes the display for every sendx_<something>() can |
| 20 |
* explain why that makes sense, or why I need the sleep() and the |
| 21 |
* XFlush(), I'd be happy to be enlightened. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> |
| 26 |
#include <ctype.h> |
| 27 |
#include <unistd.h> |
| 28 |
|
| 29 |
#include <X11/Xlib.h> |
| 30 |
#include <X11/Xatom.h> |
| 31 |
#include <X11/keysym.h> |
| 32 |
#include <X11/cursorfont.h> |
| 33 |
|
| 34 |
#include "sendx.h" |
| 35 |
|
| 36 |
static char * display_name = NULL; |
| 37 |
static Display * display; |
| 38 |
static Window root; |
| 39 |
static Window window = None; |
| 40 |
static XKeyEvent event; |
| 41 |
|
| 42 |
static char error_message[256]; |
| 43 |
static int error_status = 0; |
| 44 |
|
| 45 |
|
| 46 |
static void |
| 47 |
throw_exception3(const char * msg, const char * s, int i) |
| 48 |
{ |
| 49 |
sprintf(error_message, msg, s, i); |
| 50 |
error_status = 1; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
static void |
| 55 |
throw_exception3s(const char * msg1, const char * msg2, const char * msg3) |
| 56 |
{ |
| 57 |
sprintf(error_message, msg1, msg2, msg3); |
| 58 |
error_status = 1; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
static void |
| 63 |
throw_exception(const char * msg) |
| 64 |
{ |
| 65 |
strncpy(error_message, msg, 256); |
| 66 |
error_status = 1; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
/* |
| 72 |
* Added for window managers like swm and tvtwm that follow solbourne's |
| 73 |
* virtual root window concept |
| 74 |
*/ |
| 75 |
static Window |
| 76 |
GetRootWindow(Display * disp, int scrn) |
| 77 |
{ |
| 78 |
Atom __SWM_VROOT = None; |
| 79 |
Window root, rootReturn, parentReturn, * children; |
| 80 |
unsigned int numChildren; |
| 81 |
unsigned i; |
| 82 |
|
| 83 |
root = RootWindow(disp, scrn); |
| 84 |
|
| 85 |
/* |
| 86 |
* see if there is a virtual root |
| 87 |
*/ |
| 88 |
__SWM_VROOT = XInternAtom(disp, "__SWM_VROOT", False); |
| 89 |
XQueryTree(disp, root, &rootReturn, &parentReturn, &children, &numChildren); |
| 90 |
|
| 91 |
for (i = 0; i < numChildren; i++) |
| 92 |
{ |
| 93 |
Atom actual_type; |
| 94 |
int actual_format; |
| 95 |
unsigned long nitems, bytesafter; |
| 96 |
unsigned char * prop_return = NULL; |
| 97 |
|
| 98 |
if (XGetWindowProperty(disp, children[i], __SWM_VROOT, 0, 1, |
| 99 |
False, XA_WINDOW, &actual_type, &actual_format, |
| 100 |
&nitems, &bytesafter, &prop_return) |
| 101 |
== Success && prop_return) |
| 102 |
{ |
| 103 |
root = *(Window *)prop_return; |
| 104 |
break; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if (children) |
| 109 |
XFree((char *)children); |
| 110 |
|
| 111 |
return (root); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
/* |
| 117 |
* [These functions are from the file "dsimple.c" used with xwininfo.] |
| 118 |
* |
| 119 |
* Written by Mark Lillibridge. Last updated 7/1/87 |
| 120 |
* |
| 121 |
* Window_With_Name: routine to locate a window with a given name on a |
| 122 |
* display. If no window with the given name is found, 0 is returned. |
| 123 |
* If more than one window has the given name, the first one found |
| 124 |
* will be returned. Only top and its subwindows are looked at. |
| 125 |
* Normally, top should be the Root Window. |
| 126 |
*/ |
| 127 |
|
| 128 |
static Window |
| 129 |
Window_With_Name(Display * dpy, Window top, const char * name) |
| 130 |
{ |
| 131 |
Window * children, dummy; |
| 132 |
unsigned int nchildren; |
| 133 |
unsigned i; |
| 134 |
Window w = 0; |
| 135 |
char * window_name; |
| 136 |
|
| 137 |
if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name)) |
| 138 |
return (top); |
| 139 |
|
| 140 |
if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren)) |
| 141 |
return (0); |
| 142 |
|
| 143 |
for (i = 0; i < nchildren; i++) |
| 144 |
{ |
| 145 |
w = Window_With_Name(dpy, children[i], name); |
| 146 |
if (w) |
| 147 |
break; |
| 148 |
} |
| 149 |
|
| 150 |
if (children) |
| 151 |
XFree((char *)children); |
| 152 |
|
| 153 |
return (w); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
static int |
| 159 |
open_channel(const char * wname) |
| 160 |
{ |
| 161 |
/* |
| 162 |
* display_name = ":0.0"; |
| 163 |
*/ |
| 164 |
|
| 165 |
error_status = 0; |
| 166 |
|
| 167 |
if ((display = XOpenDisplay(display_name)) == NULL) |
| 168 |
{ |
| 169 |
throw_exception("can't open display"); |
| 170 |
return 1; |
| 171 |
} |
| 172 |
|
| 173 |
if ((root = GetRootWindow(display, DefaultScreen(display))) == 0) |
| 174 |
{ |
| 175 |
throw_exception("Cannot get DefaultScreen"); |
| 176 |
return 1; |
| 177 |
} |
| 178 |
|
| 179 |
if ((wname[0] == '\0') && (window != None)) |
| 180 |
{ |
| 181 |
} /* take selected window */ |
| 182 |
else if (wname[0] != '\0') |
| 183 |
{ |
| 184 |
if ((window = Window_With_Name(display, root, wname)) == None) |
| 185 |
{ |
| 186 |
throw_exception3s("Display %s: can't open window named \"%s\"", |
| 187 |
XDisplayName(display_name), wname); |
| 188 |
return 1; |
| 189 |
} |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
throw_exception3("bad condition in %s at line %d", __FILE__, __LINE__); |
| 194 |
return 1; |
| 195 |
} |
| 196 |
|
| 197 |
event.type = KeyPress; |
| 198 |
event.serial = 0; |
| 199 |
event.send_event = False; |
| 200 |
event.display = display; |
| 201 |
event.x = event.y = event.x_root = event.y_root = 0; |
| 202 |
event.time = CurrentTime; |
| 203 |
event.same_screen = True; |
| 204 |
event.subwindow = None; |
| 205 |
event.window = window; |
| 206 |
event.root = root; |
| 207 |
|
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
static void |
| 214 |
close_channel(void) |
| 215 |
{ |
| 216 |
/* |
| 217 |
* XFlush(display); |
| 218 |
*/ |
| 219 |
XCloseDisplay(display); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
static void |
| 225 |
sendx_channel(KeySym ks, int km) |
| 226 |
{ |
| 227 |
/* |
| 228 |
* km: 0=regular, 1=shift, 2=lock, 4=control |
| 229 |
*/ |
| 230 |
if (ks < 256) |
| 231 |
{ |
| 232 |
event.state = isupper((unsigned char)ks); |
| 233 |
switch (ks) |
| 234 |
{ |
| 235 |
case 0x08: |
| 236 |
ks = XK_BackSpace; |
| 237 |
break; |
| 238 |
case 0x09: |
| 239 |
ks = XK_Tab; |
| 240 |
break; |
| 241 |
case 0x0A: |
| 242 |
ks = XK_Linefeed; |
| 243 |
break; |
| 244 |
case 0x0B: |
| 245 |
ks = XK_Clear; |
| 246 |
break; |
| 247 |
case 0x0D: |
| 248 |
ks = XK_Return; |
| 249 |
break; |
| 250 |
case 0x13: |
| 251 |
ks = XK_Pause; |
| 252 |
break; |
| 253 |
case 0x14: |
| 254 |
ks = XK_Scroll_Lock; |
| 255 |
break; |
| 256 |
case 0x1B: |
| 257 |
ks = XK_Escape; |
| 258 |
break; |
| 259 |
} |
| 260 |
} |
| 261 |
else |
| 262 |
event.state = 0; |
| 263 |
|
| 264 |
event.type = KeyPress; |
| 265 |
event.state = km; // Mod1Mask |
| 266 |
event.keycode = XKeysymToKeycode(display, ks); |
| 267 |
|
| 268 |
if (XSendEvent(display, window, True, KeyPressMask, (XEvent *)&event) == 0) |
| 269 |
throw_exception("Error in XSendEvent"); |
| 270 |
|
| 271 |
event.type = KeyRelease; |
| 272 |
if (XSendEvent(display, window, True, KeyReleaseMask, (XEvent *)&event) |
| 273 |
== 0) |
| 274 |
throw_exception("Error in XSendEvent"); |
| 275 |
|
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
int |
| 282 |
sendx_string(const char * string, const char * window) |
| 283 |
{ |
| 284 |
const char * p; |
| 285 |
|
| 286 |
if (open_channel(window)) |
| 287 |
return error_status; |
| 288 |
|
| 289 |
p = string; |
| 290 |
while (*p) |
| 291 |
sendx_channel(*p++, 0); |
| 292 |
close_channel(); |
| 293 |
|
| 294 |
return error_status; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
int |
| 300 |
sendx_token(const char * string, const char * window) |
| 301 |
{ |
| 302 |
if (open_channel(window)) |
| 303 |
return error_status; |
| 304 |
|
| 305 |
sendx_channel(XStringToKeysym(string), 0); |
| 306 |
close_channel(); |
| 307 |
|
| 308 |
return error_status; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
int |
| 314 |
sendx_alt_token(const char * string, const char * window) |
| 315 |
{ |
| 316 |
if (open_channel(window)) |
| 317 |
return error_status; |
| 318 |
|
| 319 |
sendx_channel(XStringToKeysym(string), Mod1Mask); |
| 320 |
close_channel(); |
| 321 |
|
| 322 |
return error_status; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
int |
| 328 |
sendx_controlalt_token(const char * string, const char * window) |
| 329 |
{ |
| 330 |
if (open_channel(window)) |
| 331 |
return error_status; |
| 332 |
|
| 333 |
sendx_channel(XStringToKeysym(string), Mod1Mask | ControlMask); |
| 334 |
close_channel(); |
| 335 |
|
| 336 |
return error_status; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
int |
| 342 |
sendx_control_token(const char * string, const char * window) |
| 343 |
{ |
| 344 |
if (open_channel(window)) |
| 345 |
return error_status; |
| 346 |
|
| 347 |
sendx_channel(XStringToKeysym(string), ControlMask); |
| 348 |
close_channel(); |
| 349 |
|
| 350 |
return error_status; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
static Window previous_window; |
| 357 |
static int previous_window_set = 0; |
| 358 |
static int revert_to; |
| 359 |
|
| 360 |
/* |
| 361 |
* Attempt to give focus to the given window. |
| 362 |
* Return 0 on success, non-0 on failure. |
| 363 |
* Record the previous window for reset_focus() on success. |
| 364 |
*/ |
| 365 |
|
| 366 |
int |
| 367 |
set_focus(const char * wname) |
| 368 |
{ |
| 369 |
if ((display = XOpenDisplay(display_name)) == NULL) |
| 370 |
{ |
| 371 |
throw_exception("can't open display"); |
| 372 |
return 1; |
| 373 |
} |
| 374 |
|
| 375 |
if ((root = GetRootWindow(display, DefaultScreen(display))) == 0) |
| 376 |
{ |
| 377 |
throw_exception("Cannot get DefaultScreen"); |
| 378 |
return 1; |
| 379 |
} |
| 380 |
|
| 381 |
(void)XGetInputFocus(display, &previous_window, &revert_to); |
| 382 |
if (XSetInputFocus(display, Window_With_Name(display, root, wname), |
| 383 |
previous_window, CurrentTime)) |
| 384 |
{ |
| 385 |
previous_window_set = 1; |
| 386 |
return 0; |
| 387 |
} |
| 388 |
|
| 389 |
throw_exception3s("Display %s: can't focus window named \"%s\"", |
| 390 |
XDisplayName(display_name), wname); |
| 391 |
return 1; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
int |
| 397 |
reset_focus(void) |
| 398 |
{ |
| 399 |
if (previous_window_set) |
| 400 |
{ |
| 401 |
sleep(1); |
| 402 |
if ((display = XOpenDisplay(display_name)) == NULL) |
| 403 |
{ |
| 404 |
throw_exception("can't open display"); |
| 405 |
return 1; |
| 406 |
} |
| 407 |
|
| 408 |
XSetInputFocus(display, previous_window, revert_to, CurrentTime); |
| 409 |
XFlush(display); |
| 410 |
previous_window_set = 0; |
| 411 |
return 0; |
| 412 |
} |
| 413 |
|
| 414 |
throw_exception("reset_focus() called when previous window not saved"); |
| 415 |
return 1; |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
/* |
| 421 |
* void PrintKeySyms () { int i; for (i = 32; i < 127; i++) { printf ("%s[%c] |
| 422 |
* ", XKeysymToString (i), i); } for (i = 128 + 32; i < 128 + 127; i++) { |
| 423 |
* printf ("%s[%c] ", XKeysymToString (i), i); } printf ("\n"); } |
| 424 |
*/ |