]>
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 P_STR
, //string to type
20 NUM_P
//number of parameters
22 enum uni_m
{ //unicode methods:
23 SKIP
, //ignore any keys not on the layout
24 GTK_HOLD
, //hold ctrl and shift while entering hex values
25 GTK_SPACE
, //end hex sequence with spacebar
26 WINDOWS
//use alt+numpad
29 ERR_SUCCESS
, //no error
30 ERR_ARGCOUNT
, //wrong number of arguments
31 ERR_SYMBOL
, //symbol not in look up table
32 ERR_LAYOUT
, //parameter P_LAY does not contain a correct keyboard layout
33 ERR_LAZY
//i haven't done this
36 void send_key ( FILE * hid_dev
, unsigned short key
, unsigned short mod
);
37 enum errors
send_unicode ( FILE * hid_dev
, unsigned int unicode
, enum uni_m method
, enum kbdl layout
);
39 int main ( int argc
, char ** argv
) {
41 fprintf ( stderr
, "Usage: %s <device file> <layout> <unicode> \" <string> \"\n " , argv
[ P_EXE
]);
44 FILE * hid_dev
= fopen ( "/dev/hidg0" , "w" );
45 for ( int i
= 0 ; i
< strlen ( argv
[ P_STR
]); i
++) {
47 char tmp
[ UTF8_MAX_LENGTH
] = { argv
[ P_STR
][ i
], argv
[ P_STR
][ i
+ 1 ], argv
[ P_STR
][ i
+ 2 ], '\0' };
48 //TODO: replace by something less stupid
49 if ( argv
[ P_STR
][ i
] < 128 ) { // not multi byte
51 } else { // is multi byte
52 if ( argv
[ P_STR
][ i
] < 0xe0 ) {
53 i
++; //skip next thing
56 i
+= 2 ; //WARN: fails on utf8 > 3 byte
60 struct keysym
* s
= toscan ( tmp
);
62 fprintf ( stderr
, "Key Symbol not found. \n " );
66 struct layout
* l
= tolay ( s
, atoi ( argv
[ P_LAY
]));
68 fprintf ( stderr
, "Unrecognised keyboard layout. \n " );
73 send_key ( hid_dev
, l
-> key
, l
-> mod
);
74 send_key ( hid_dev
, '\0' , '\0' ); //release all keys
76 //dead keys need to be pressed twice to show up
77 send_key ( hid_dev
, l
-> key
, l
-> mod
);
78 send_key ( hid_dev
, '\0' , '\0' ); //release all keys
81 //key does not exist in this layout, use unicode method
82 //fprintf (stderr, "Warning: Key '%s'(0x%x) not in this layout!\n", s->sym, s->unicode);
83 send_unicode ( hid_dev
, s
-> unicode
, atoi ( argv
[ P_UNI
]), atoi ( argv
[ P_LAY
]));
91 void send_key ( FILE * hid_dev
, unsigned short key
, unsigned short mod
) {
92 fprintf ( hid_dev
, "%c%c%c%c%c%c%c%c" , mod
, '\0' , key
, '\0' , '\0' , '\0' , '\0' , '\0' );
95 enum errors
send_unicode ( FILE * hid_dev
, unsigned int unicode
, enum uni_m method
, enum kbdl layout
) {
100 if ( unicode
== 0x00 ) {
101 fprintf ( stderr
, "Symbol not in lookup table! \n " );
109 sprintf ( buf
, "%x" , unicode
);
111 l
= tolay ( s
, layout
);
112 send_key ( hid_dev
, l
-> key
, MOD_LCTRL
| MOD_LSHIFT
);
113 for ( int i
= 0 ; i
< strlen ( buf
); i
++) {
114 s
= toscan (( char [ 2 ]){ buf
[ i
], '\0' });
115 l
= tolay ( s
, layout
);
116 send_key ( hid_dev
, l
-> key
, MOD_LCTRL
| MOD_LSHIFT
);
118 send_key ( hid_dev
, '\0' , '\0' );
121 sprintf ( buf
, "%x " , unicode
);
123 l
= tolay ( s
, layout
);
124 send_key ( hid_dev
, l
-> key
, MOD_LCTRL
| MOD_LSHIFT
);
125 for ( int i
= 0 ; i
< strlen ( buf
); i
++) {
126 s
= toscan (( char [ 2 ]){ buf
[ i
], '\0' });
127 l
= tolay ( s
, layout
);
128 send_key ( hid_dev
, l
-> key
, MOD_NONE
);
130 send_key ( hid_dev
, '\0' , '\0' );
133 fprintf ( stderr
, "windows method not implemented! \n " );
136 fprintf ( stderr
, "unknown unicode method! \n " );
137 return ERR_LAYOUT
; //TODO: better error code