2 softwedge - serial to X11 keyboard software wedge
3 Copyright (C) 2007 Yann Ramin
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <X11/Xutil.h>
26 #include <X11/extensions/XTest.h>
27 #include <X11/keysymdef.h>
28 #include <sys/types.h>
33 #include "softwedge.h"
35 static int serialPort
;
38 static void xtest_key_press(unsigned char letter
) {
39 unsigned int shiftcode
= XKeysymToKeycode(dpy
, XStringToKeysym("Shift_L"));
45 KeySym sym
= XStringToKeysym(s
);
59 } else if (sym
== '\t') {
64 keycode
= XKeysymToKeycode(dpy
, sym
);
66 sym
= 0xff00 | letter
;
67 keycode
= XKeysymToKeycode(dpy
, sym
);
72 // Here we try to determine if a keysym
73 // needs a modifier key (shift), such as a
74 // shifted letter or symbol.
75 // The second keysym should be the shifted char
77 int keysyms_per_keycode
;
78 syms
= XGetKeyboardMapping(dpy
, keycode
, 1, &keysyms_per_keycode
);
80 for (i
= 0; i
<= keysyms_per_keycode
; i
++) {
84 if (i
== 0 && syms
[i
] != letter
)
92 XTestFakeKeyEvent(dpy
, shiftcode
, True
, 0);
95 XTestFakeKeyEvent(dpy
, keycode
, True
, 0);
96 XTestFakeKeyEvent(dpy
, keycode
, False
, 0);
99 XTestFakeKeyEvent(dpy
, shiftcode
, False
, 0);
105 static void press_keys(char* string
) {
106 int len
= strlen(string
);
108 for (i
= 0; i
< len
; i
++) {
109 xtest_key_press(string
[i
]);
115 int sw_open_serial(const char *port
) {
116 serialPort
= open(port
, O_RDONLY
);
117 if (serialPort
< 0) {
118 fprintf(stderr
, "Can't open serial port: %s\n", port
);
127 int xtest_major_version
= 0;
128 int xtest_minor_version
= 0;
133 * Open the display using the $DISPLAY environment variable to locate
134 * the X server. See Section 2.1.
136 if ((dpy
= XOpenDisplay(NULL
)) == NULL
) {
137 fprintf(stderr
, "%s: can't open %s\en", "softwedge", XDisplayName(NULL
));
141 Bool success
= XTestQueryExtension(dpy
, &dummy
, &dummy
,
142 &xtest_major_version
, &xtest_minor_version
);
143 if(success
== False
|| xtest_major_version
< 2 ||
144 (xtest_major_version
<= 2 && xtest_minor_version
< 2))
146 fprintf(stderr
,"XTEST extension not supported. Can't continue\n");
153 void sw_read_loop() {
158 while(read(serialPort
, readbuf
, 1) > 0) {
159 if (readbuf
[0] == 0x02 || readbuf
[0] == 0x03)