#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
/* sender.c */
int getWinID();
void sendIt(Display* disp, int win, char* text);
int main (void)
{
int win[3] = {0, 0, 0};
char text[300];
int i;
puts("Please click on this window once.");
win[0] = getWinID();
puts("Please click on the first of the windows to send events to.");
win[1] = getWinID();
puts("Please click on the second window to send events to.");
win[2] = getWinID();
Display* disp = XOpenDisplay(NULL);
if(disp == NULL)
{
fprintf(stderr, " could not open display\n");
return -1;
}
puts("Now enter something here ('q' to quit):");
while(1)
{
XSetInputFocus(disp, win[0], 0, CurrentTime);
printf("> ");
scanf("%s", &text);
fflush(stdin);
if(strcmp(text, "q") == 0)
{
puts("\nBye.\n");
break;
}
for(i = 1; i <= 2; i++)
{
sendIt(disp, win[i], text);
}
XFlush(disp);
}
XCloseDisplay(disp);
return 0;
}
int getWinID()
{
FILE *foo;
char string[255];
int thewindow = 0;
sprintf(string, "xwininfo");
foo=popen(string, "r");
while(!feof(foo) && fgets(string, 255, foo) &&
(sscanf(string, "xwininfo: Window id: %x", &thewindow) != 1));
pclose(foo);
if(thewindow == 0)
{
puts("Window not recognized.");
exit(1);
}
return thewindow;
}
void sendIt(Display* disp, int win, char* text)
{
int i;
XSetInputFocus(disp, win, 0, CurrentTime);
for(i = 0; i < strlen(text); i++)
{
if(text[i] >= 'A' && text[i] <= 'Z')
{
XTestFakeKeyEvent(disp, XKeysymToKeycode(disp, XK_Shift_L), True, 0);
}
XTestFakeKeyEvent(disp, XKeysymToKeycode(disp, text[i]), True, 0);
XTestFakeKeyEvent(disp, XKeysymToKeycode(disp, text[i]), False, 0);
if(text[i] >= 'A' && text[i] <= 'Z')
{
XTestFakeKeyEvent(disp, XKeysymToKeycode(disp, XK_Shift_L), False, 0);
}
}
XTestFakeKeyEvent(disp, XKeysymToKeycode(disp, XK_Tab), True, 0);
XTestFakeKeyEvent(disp, XKeysymToKeycode(disp, XK_Tab), False, 0);
XFlush(disp);
return;
}