]> git.gir.st - sendHID.git/blob - main.c
changed text to type from parameter to stdin
[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 enum params {//argv-indices:
15 P_EXE, //executable name
16 P_DEV, //device file
17 P_LAY, //layout
18 P_UNI, //unicode method
19 NUM_P //number of parameters
20 };
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
26 };
27 enum errors {
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
33 };
34
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);
37
38 int main (int argc, char** argv) {
39 if (argc != NUM_P) {
40 fprintf (stderr, "Usage: %s <device file> <layout> <unicode>\n", argv[P_EXE]);
41 fprintf (stderr, "Takes string to type from stdin\n");
42 return ERR_ARGCOUNT;
43 }
44 FILE* hid_dev = fopen ("/dev/hidg0", "w");
45 char in_string[256];
46 fgets(in_string, 256, stdin);
47 for (int i = 0; i < strlen (in_string); i++) {
48
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
52 tmp[1] = '\0';
53 } else { // is multi byte
54 if (in_string[i] < 0xe0) {
55 i++; //skip next thing
56 tmp[2] = 0;
57 } else {
58 i+=2; //WARN: fails on utf8 > 3 byte
59 }
60 }
61
62 struct keysym* s = toscan (tmp);
63 if (s == NULL) {
64 fprintf (stderr, "Key Symbol not found.\n");
65 fclose (hid_dev);
66 return ERR_SYMBOL;
67 }
68 struct layout* l = tolay (s, atoi (argv[P_LAY]));
69 if (l == NULL) {
70 fprintf (stderr, "Unrecognised keyboard layout.\n");
71 fclose (hid_dev);
72 return ERR_LAYOUT;
73 }
74 if (l->key != 0x00) {
75 send_key(hid_dev, l->key, l->mod);
76 send_key(hid_dev, '\0', '\0'); //release all keys
77 if (l->is_dead) {
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
81 }
82 } else {
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]));
86 }
87 }
88 fclose (hid_dev);
89
90 return ERR_SUCCESS;
91 }
92
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');
95 }
96
97 enum errors send_unicode (FILE* hid_dev, unsigned int unicode, enum uni_m method, enum kbdl layout) {
98 char buf[10];
99 struct keysym* s;
100 struct layout* l;
101
102 if (unicode == 0x00) {
103 fprintf (stderr, "Symbol not in lookup table!\n");
104 return ERR_SYMBOL;
105 }
106
107 switch (method) {
108 case SKIP:
109 break;
110 case GTK_HOLD:
111 sprintf (buf, "%x", unicode);
112 s = toscan ("u");
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);
119 }
120 send_key (hid_dev, '\0', '\0');
121 break;
122 case GTK_SPACE:
123 sprintf (buf, "%x ", unicode);
124 s = toscan ("u");
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);
131 }
132 send_key (hid_dev, '\0', '\0');
133 break;
134 case WINDOWS:
135 fprintf (stderr, "windows method not implemented!\n");
136 return ERR_LAZY;
137 default:
138 fprintf (stderr, "unknown unicode method!\n");
139 return ERR_LAYOUT; //TODO: better error code
140 }
141 return ERR_SUCCESS;
142 }
Imprint / Impressum