8 #include <X11/extensions/XTest.h>
9 #include <X11/keysymdef.h>
10 #include <sys/types.h>
15 #include "softwedge.h"
19 void xtest_key_press(Display
*dpy
, unsigned char letter
) {
20 unsigned int shiftcode
= XKeysymToKeycode(dpy
, XStringToKeysym("Shift_L"));
26 KeySym sym
= XStringToKeysym(s
);
40 } else if (sym
== '\t') {
45 keycode
= XKeysymToKeycode(dpy
, sym
);
47 sym
= 0xff00 | letter
;
48 keycode
= XKeysymToKeycode(dpy
, sym
);
53 // Here we try to determine if a keysym
54 // needs a modifier key (shift), such as a
55 // shifted letter or symbol.
56 // The second keysym should be the shifted char
58 int keysyms_per_keycode
;
59 syms
= XGetKeyboardMapping(dpy
, keycode
, 1, &keysyms_per_keycode
);
61 for (i
= 0; i
<= keysyms_per_keycode
; i
++) {
65 if (i
== 0 && syms
[i
] != letter
)
73 XTestFakeKeyEvent(dpy
, shiftcode
, True
, 0);
76 XTestFakeKeyEvent(dpy
, keycode
, True
, 0);
77 XTestFakeKeyEvent(dpy
, keycode
, False
, 0);
80 XTestFakeKeyEvent(dpy
, shiftcode
, False
, 0);
86 void press_keys(Display
*dpy
, char* string
) {
87 int len
= strlen(string
);
89 for (i
= 0; i
< len
; i
++) {
90 xtest_key_press(dpy
, string
[i
]);
96 int sw_open_serial(const char *port
) {
97 serialPort
= open(port
, O_RDONLY
);
99 fprintf(stderr
, "Can't open serial port: %s\n", port
);
107 int main(int argc
, char**argv
)
109 Display
*dpy
; /* X server connection */
110 int xtest_major_version
= 0;
111 int xtest_minor_version
= 0;
116 while ((c
= getopt (argc
, argv
, "vc:")) != -1)
120 fprintf(stderr
, "softwedge v %s: The serial softwedge X11 helper. ", SOFTWEDGE_VERSION
);
121 fprintf(stderr
, "(c) 2007 Yann Ramin <atrus@stackworks.net>\n(Exiting...)\n");
128 fprintf (stderr
, "Option -%c requires an argument.\n", optopt
);
129 else if (isprint (optopt
))
130 fprintf (stderr
, "Unknown option `-%c'.\n", optopt
);
133 "Unknown option character `\\x%x'.\n",
141 sport
= DEFAULT_SERIAL
;
146 * Open the display using the $DISPLAY environment variable to locate
147 * the X server. See Section 2.1.
149 if ((dpy
= XOpenDisplay(NULL
)) == NULL
) {
150 fprintf(stderr
, "%s: can't open %s\en", argv
[0], XDisplayName(NULL
));
154 Bool success
= XTestQueryExtension(dpy
, &dummy
, &dummy
,
155 &xtest_major_version
, &xtest_minor_version
);
156 if(success
== False
|| xtest_major_version
< 2 ||
157 (xtest_major_version
<= 2 && xtest_minor_version
< 2))
159 fprintf(stderr
,"XTEST extension not supported. Can't continue\n");
164 sw_open_serial(sport
);
181 while(read(serialPort
, readbuf
, 1) > 0) {
183 press_keys(dpy
, readbuf
);