]>
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 #define TEXT_LEN 256 //max length of piped text
15 enum params
{ //argv-indices:
16 P_EXE
, //executable name
19 P_UNI
, //unicode method
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> \n " , argv
[ P_EXE
]);
42 fprintf ( stderr
, "Takes string to type from stdin \n " );
45 FILE * hid_dev
= fopen ( "/dev/hidg0" , "w" );
46 char in_string
[ TEXT_LEN
];
47 fgets ( in_string
, TEXT_LEN
, stdin
);
48 for ( int i
= 0 ; i
< strlen ( in_string
); i
++) {
49 if ( in_string
[ i
] < 32 ) {
50 if ( in_string
[ i
] != ' \n ' ) fprintf ( stderr
, "Cannot print control characters! \n (Is there a newline at the end of your string? \n " );
54 char tmp
[ UTF8_MAX_LENGTH
] = { in_string
[ i
], in_string
[ i
+ 1 ], in_string
[ i
+ 2 ], '\0' };
55 //TODO: replace by something less stupid
56 if ( in_string
[ i
] < 128 ) { // not multi byte
58 } else { // is multi byte
59 if ( in_string
[ i
] < 0xe0 ) {
60 i
++; //skip next thing
63 i
+= 2 ; //WARN: fails on utf8 > 3 byte
67 struct keysym
* s
= toscan ( tmp
);
69 fprintf ( stderr
, "Key Symbol not found. \n " );
73 struct layout
* l
= tolay ( s
, atoi ( argv
[ P_LAY
]));
75 fprintf ( stderr
, "Unrecognised keyboard layout. \n " );
80 send_key ( hid_dev
, l
-> key
, l
-> mod
);
81 send_key ( hid_dev
, '\0' , '\0' ); //release all keys
83 //dead keys need to be pressed twice to show up
84 send_key ( hid_dev
, l
-> key
, l
-> mod
);
85 send_key ( hid_dev
, '\0' , '\0' ); //release all keys
88 //key does not exist in this layout, use unicode method
89 //fprintf (stderr, "Warning: Key '%s'(0x%x) not in this layout!\n", s->sym, s->unicode);
90 send_unicode ( hid_dev
, s
-> unicode
, atoi ( argv
[ P_UNI
]), atoi ( argv
[ P_LAY
]));
98 void send_key ( FILE * hid_dev
, unsigned short key
, unsigned short mod
) {
99 fprintf ( hid_dev
, "%c%c%c%c%c%c%c%c" , mod
, '\0' , key
, '\0' , '\0' , '\0' , '\0' , '\0' );
102 enum errors
send_unicode ( FILE * hid_dev
, unsigned int unicode
, enum uni_m method
, enum kbdl layout
) {
107 if ( unicode
== 0x00 ) {
108 fprintf ( stderr
, "Symbol not in lookup table! \n " );
116 sprintf ( buf
, "%x" , unicode
);
118 l
= tolay ( s
, layout
);
119 send_key ( hid_dev
, l
-> key
, MOD_LCTRL
| MOD_LSHIFT
);
120 for ( int i
= 0 ; i
< strlen ( buf
); i
++) {
121 s
= toscan (( char [ 2 ]){ buf
[ i
], '\0' });
122 l
= tolay ( s
, layout
);
123 send_key ( hid_dev
, l
-> key
, MOD_LCTRL
| MOD_LSHIFT
);
125 send_key ( hid_dev
, '\0' , '\0' );
128 sprintf ( buf
, "%x " , unicode
);
130 l
= tolay ( s
, layout
);
131 send_key ( hid_dev
, l
-> key
, MOD_LCTRL
| MOD_LSHIFT
);
132 for ( int i
= 0 ; i
< strlen ( buf
); i
++) {
133 s
= toscan (( char [ 2 ]){ buf
[ i
], '\0' });
134 l
= tolay ( s
, layout
);
135 send_key ( hid_dev
, l
-> key
, MOD_NONE
);
137 send_key ( hid_dev
, '\0' , '\0' );
140 fprintf ( stderr
, "windows method not implemented! \n " );
143 fprintf ( stderr
, "unknown unicode method! \n " );
144 return ERR_LAYOUT
; //TODO: better error code