]> git.gir.st - sendHID.git/blob - main.c
correct handling of control characters (stopping)
[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 (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.)
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "scancodes.h"
13
14 #define TEXT_LEN 256 //max length of piped text
15 enum params {//argv-indices:
16 P_EXE, //executable name
17 P_DEV, //device file
18 P_LAY, //layout
19 P_UNI, //unicode method
20 NUM_P //number of parameters
21 };
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
27 };
28 enum errors {
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
34 };
35
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);
38
39 int main (int argc, char** argv) {
40 if (argc != NUM_P) {
41 fprintf (stderr, "Usage: %s <device file> <layout> <unicode>\n", argv[P_EXE]);
42 fprintf (stderr, "Takes string to type from stdin\n");
43 return ERR_ARGCOUNT;
44 }
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 fprintf (stderr, "Cannot print control characters!\n(Is there a newline at the end of your string?\n");
51 return ERR_SYMBOL;
52 }
53
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
57 tmp[1] = '\0';
58 } else { // is multi byte
59 if (in_string[i] < 0xe0) {
60 i++; //skip next thing
61 tmp[2] = 0;
62 } else {
63 i+=2; //WARN: fails on utf8 > 3 byte
64 }
65 }
66
67 struct keysym* s = toscan (tmp);
68 if (s == NULL) {
69 fprintf (stderr, "Key Symbol not found.\n");
70 fclose (hid_dev);
71 return ERR_SYMBOL;
72 }
73 struct layout* l = tolay (s, atoi (argv[P_LAY]));
74 if (l == NULL) {
75 fprintf (stderr, "Unrecognised keyboard layout.\n");
76 fclose (hid_dev);
77 return ERR_LAYOUT;
78 }
79 if (l->key != 0x00) {
80 send_key(hid_dev, l->key, l->mod);
81 send_key(hid_dev, '\0', '\0'); //release all keys
82 if (l->is_dead) {
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
86 }
87 } else {
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]));
91 }
92 }
93 fclose (hid_dev);
94
95 return ERR_SUCCESS;
96 }
97
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');
100 }
101
102 enum errors send_unicode (FILE* hid_dev, unsigned int unicode, enum uni_m method, enum kbdl layout) {
103 char buf[10];
104 struct keysym* s;
105 struct layout* l;
106
107 if (unicode == 0x00) {
108 fprintf (stderr, "Symbol not in lookup table!\n");
109 return ERR_SYMBOL;
110 }
111
112 switch (method) {
113 case SKIP:
114 break;
115 case GTK_HOLD:
116 sprintf (buf, "%x", unicode);
117 s = toscan ("u");
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);
124 }
125 send_key (hid_dev, '\0', '\0');
126 break;
127 case GTK_SPACE:
128 sprintf (buf, "%x ", unicode);
129 s = toscan ("u");
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);
136 }
137 send_key (hid_dev, '\0', '\0');
138 break;
139 case WINDOWS:
140 fprintf (stderr, "windows method not implemented!\n");
141 return ERR_LAZY;
142 default:
143 fprintf (stderr, "unknown unicode method!\n");
144 return ERR_LAYOUT; //TODO: better error code
145 }
146 return ERR_SUCCESS;
147 }
Imprint / Impressum