Some more functional testing - space and other characters seem broken
[softwedge.git] / softwedge.c
blobf96f8264524a4ff91d617617cbf45b571495a539
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <X11/Xlib.h>
6 #include <X11/Xutil.h>
7 #include <X11/extensions/XTest.h>
8 #include <X11/keysymdef.h>
11 void key_press(Display *dpy, unsigned char letter) {
12 unsigned int shiftcode = XKeysymToKeycode(dpy, XStringToKeysym("Shift_L"));
14 char s[2];
15 s[0] = letter;
16 s[1] = 0;
17 KeySym sym = XStringToKeysym(s);
18 KeyCode keycode = XKeysymToKeycode(dpy, sym);
20 if (isupper(letter))
21 XTestFakeKeyEvent(dpy, shiftcode, True, 0);
23 XTestFakeKeyEvent(dpy, keycode, True, 0);
24 XTestFakeKeyEvent(dpy, keycode, False, 0);
26 if (isupper(letter))
27 XTestFakeKeyEvent(dpy, shiftcode, False, 0);
29 XFlush(dpy);
32 void press_keys(Display *dpy, char* string) {
33 int len = strlen(string);
34 int i = 0;
35 for (i = 0; i < len; i++) {
36 key_press(dpy, string[i]);
40 int main(int argc, char**argv)
42 Display *dpy; /* X server connection */
43 int xtest_major_version = 0;
44 int xtest_minor_version = 0;
45 int dummy;
48 * Open the display using the $DISPLAY environment variable to locate
49 * the X server. See Section 2.1.
51 if ((dpy = XOpenDisplay(NULL)) == NULL) {
52 fprintf(stderr, "%s: can't open %s\en", argv[0], XDisplayName(NULL));
53 exit(1);
56 Bool success = XTestQueryExtension(dpy, &dummy, &dummy,
57 &xtest_major_version, &xtest_minor_version);
58 if(success == False || xtest_major_version < 2 ||
59 (xtest_major_version <= 2 && xtest_minor_version < 2))
61 fprintf(stderr,"XTEST extension not supported. Can't continue\n");
62 exit(1);
66 press_keys(dpy, "Hello world!");
73 return 0;