]> git.gir.st - sendHID.git/blob - main.c
initial code import
[sendHID.git] / main.c
1 /*
2 description: sends a sequence of keystrokes to the hid device.
3 parameters:
4 device file (e.g. /dev/hidg0)
5 keyboard layout (e.g. en_us; only a limited number are supported and they do not follow unix-naming conventions for convenience)
6 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.
7 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "scancodes.h"
12
13 void send_key (FILE* hid_dev, unsigned short key, unsigned short mod);
14
15 int main (int argc, char** argv) {
16 if (argc != 4) {
17 fprintf (stderr, "Usage: %s <device file> <layout> \"<string>\"\n", argv[0]);
18 return 1;
19 }
20 FILE* hid_dev = fopen ("/dev/hidg0", "w");
21 for (int i = 0; i < strlen (argv[3]); i++) {
22
23 char tmp[UTF8_MAX_LENGTH] = {argv[3][i], argv[3][i+1], argv[3][i+2], '\0'};
24 //TODO: replace by something less stupid
25 if (argv[3][i] < 128) { // not multi byte
26 tmp[1] = '\0';
27 } else { // is multi byte
28 if (argv[3][i] < 0xe0) {
29 i++; //skip next thing
30 tmp[2] = 0;
31 } else {
32 i+=2; //WARN: fails on utf8 > 3 byte
33 }
34 }
35
36 struct keysym* s = toscan (tmp);
37 if (s == NULL) {
38 fprintf (stderr, "Key Symbol not found.\n");
39 return 1;
40 }
41 switch (atoi (argv[2])) {
42 case 0:
43 fprintf (stderr, "This keyboard layout is reserved.\n");
44 return 1;
45 case 1: //en_us
46 send_key(hid_dev, s->en_us.key, s->en_us.mod);
47 break;
48 case 2: //de_at
49 send_key(hid_dev, s->de_at.key, s->de_at.mod);//, s->de_at.is_dead);
50 break;
51 case 3: //de_at-nodeadkeys
52 send_key(hid_dev, s->de_at.key, s->de_at.mod);
53 break;
54 default:
55 fprintf (stderr, "Unrecognised keyboard layout.\n");
56 return 1;
57 }
58 send_key(hid_dev, '\0', '\0');
59 }
60 fclose (hid_dev);
61
62 return 0;
63 }
64
65 void send_key (FILE* hid_dev, unsigned short key, unsigned short mod) {
66 fprintf (hid_dev, "%c%c%c%c%c%c%c%c", mod, '\0', key, '\0', '\0', '\0', '\0', '\0');
67 }
Imprint / Impressum