]>
git.gir.st - sendHID.git/blob - main.c
2 description: sends a sequence of keystrokes to the hid device.
4 device file (e.g. /dev/hidg0)
5 keyboard layout (1=en_us, 2=de_at, 3=de_at-nodeadkeys)
6 unicode method: 1=gtk_holddown, 2=gtk_spaceend, 3=windows
7 the string to send (as whitespace is important, the `echo` way of concatenating all parameters is not supported. if your string has white space in it and you are in an interactive session, quote your string.)
12 #include "scancodes.h"
14 enum params
{ //argv-indices:
15 P_EXE
, //executable name
18 P_UNI
, //unicode method
19 NUM_P
//number of parameters
21 enum uni_m
{ //unicode methods:
22 SKIP
, //ignore any keys not on the layout
23 GTK_HOLD
, //hold ctrl and shift while entering hex values
24 GTK_SPACE
, //end hex sequence with spacebar
25 WINDOWS
//use alt+numpad
28 ERR_SUCCESS
, //no error
29 ERR_ARGCOUNT
, //wrong number of arguments
30 ERR_SYMBOL
, //symbol not in look up table
31 ERR_LAYOUT
, //parameter P_LAY does not contain a correct keyboard layout
32 ERR_LAZY
//i haven't done this
35 void send_key ( FILE * hid_dev
, unsigned short key
, unsigned short mod
);
36 enum errors
send_unicode ( FILE * hid_dev
, unsigned int unicode
, enum uni_m method
, enum kbdl layout
);
38 int main ( int argc
, char ** argv
) {
40 fprintf ( stderr
, "Usage: %s <device file> <layout> <unicode> \n " , argv
[ P_EXE
]);
41 fprintf ( stderr
, "Takes string to type from stdin \n " );
44 FILE * hid_dev
= fopen ( "/dev/hidg0" , "w" );
46 fgets ( in_string
, 256 , stdin
);
47 for ( int i
= 0 ; i
< strlen ( in_string
); i
++) {
49 char tmp
[ UTF8_MAX_LENGTH
] = { in_string
[ i
], in_string
[ i
+ 1 ], in_string
[ i
+ 2 ], '\0' };
50 //TODO: replace by something less stupid
51 if ( in_string
[ i
] < 128 ) { // not multi byte
53 } else { // is multi byte
54 if ( in_string
[ i
] < 0xe0 ) {
55 i
++; //skip next thing
58 i
+= 2 ; //WARN: fails on utf8 > 3 byte
62 struct keysym
* s
= toscan ( tmp
);
64 fprintf ( stderr
, "Key Symbol not found. \n " );
68 struct layout
* l
= tolay ( s
, atoi ( argv
[ P_LAY
]));
70 fprintf ( stderr
, "Unrecognised keyboard layout. \n " );
75 send_key ( hid_dev
, l
-> key
, l
-> mod
);
76 send_key ( hid_dev
, '\0' , '\0' ); //release all keys
78 //dead keys need to be pressed twice to show up
79 send_key ( hid_dev
, l
-> key
, l
-> mod
);
80 send_key ( hid_dev
, '\0' , '\0' ); //release all keys
83 //key does not exist in this layout, use unicode method
84 //fprintf (stderr, "Warning: Key '%s'(0x%x) not in this layout!\n", s->sym, s->unicode);
85 send_unicode ( hid_dev
, s
-> unicode
, atoi ( argv
[ P_UNI
]), atoi ( argv
[ P_LAY
]));
93 void send_key ( FILE * hid_dev
, unsigned short key
, unsigned short mod
) {
94 fprintf ( hid_dev
, "%c%c%c%c%c%c%c%c" , mod
, '\0' , key
, '\0' , '\0' , '\0' , '\0' , '\0' );
97 enum errors
send_unicode ( FILE * hid_dev
, unsigned int unicode
, enum uni_m method
, enum kbdl layout
) {
102 if ( unicode
== 0x00 ) {
103 fprintf ( stderr
, "Symbol not in lookup table! \n " );
111 sprintf ( buf
, "%x" , unicode
);
113 l
= tolay ( s
, layout
);
114 send_key ( hid_dev
, l
-> key
, MOD_LCTRL
| MOD_LSHIFT
);
115 for ( int i
= 0 ; i
< strlen ( buf
); i
++) {
116 s
= toscan (( char [ 2 ]){ buf
[ i
], '\0' });
117 l
= tolay ( s
, layout
);
118 send_key ( hid_dev
, l
-> key
, MOD_LCTRL
| MOD_LSHIFT
);
120 send_key ( hid_dev
, '\0' , '\0' );
123 sprintf ( buf
, "%x " , unicode
);
125 l
= tolay ( s
, layout
);
126 send_key ( hid_dev
, l
-> key
, MOD_LCTRL
| MOD_LSHIFT
);
127 for ( int i
= 0 ; i
< strlen ( buf
); i
++) {
128 s
= toscan (( char [ 2 ]){ buf
[ i
], '\0' });
129 l
= tolay ( s
, layout
);
130 send_key ( hid_dev
, l
-> key
, MOD_NONE
);
132 send_key ( hid_dev
, '\0' , '\0' );
135 fprintf ( stderr
, "windows method not implemented! \n " );
138 fprintf ( stderr
, "unknown unicode method! \n " );
139 return ERR_LAYOUT
; //TODO: better error code