]> git.gir.st - sendHID.git/blob - main.c
moved tolay() from main.c to scancodes.c
[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 P_STR, //string to type
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> \"<string>\"\n", argv[P_EXE]);
42 return ERR_ARGCOUNT;
43 }
44 FILE* hid_dev = fopen ("/dev/hidg0", "w");
45 for (int i = 0; i < strlen (argv[P_STR]); i++) {
46
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
50 tmp[1] = '\0';
51 } else { // is multi byte
52 if (argv[P_STR][i] < 0xe0) {
53 i++; //skip next thing
54 tmp[2] = 0;
55 } else {
56 i+=2; //WARN: fails on utf8 > 3 byte
57 }
58 }
59
60 struct keysym* s = toscan (tmp);
61 if (s == NULL) {
62 fprintf (stderr, "Key Symbol not found.\n");
63 fclose (hid_dev);
64 return ERR_SYMBOL;
65 }
66 struct layout* l = tolay (s, atoi (argv[P_LAY]));
67 if (l == NULL) {
68 fprintf (stderr, "Unrecognised keyboard layout.\n");
69 fclose (hid_dev);
70 return ERR_LAYOUT;
71 }
72 if (l->key != 0x00) {
73 send_key(hid_dev, l->key, l->mod);
74 send_key(hid_dev, '\0', '\0'); //release all keys
75 if (l->is_dead) {
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
79 }
80 } else {
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]));
84 }
85 }
86 fclose (hid_dev);
87
88 return ERR_SUCCESS;
89 }
90
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');
93 }
94
95 enum errors send_unicode (FILE* hid_dev, unsigned int unicode, enum uni_m method, enum kbdl layout) {
96 char buf[10];
97 struct keysym* s;
98 struct layout* l;
99
100 if (unicode == 0x00) {
101 fprintf (stderr, "Symbol not in lookup table!\n");
102 return ERR_SYMBOL;
103 }
104
105 switch (method) {
106 case SKIP:
107 break;
108 case GTK_HOLD:
109 sprintf (buf, "%x", unicode);
110 s = toscan ("u");
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);
117 }
118 send_key (hid_dev, '\0', '\0');
119 break;
120 case GTK_SPACE:
121 sprintf (buf, "%x ", unicode);
122 s = toscan ("u");
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);
129 }
130 send_key (hid_dev, '\0', '\0');
131 break;
132 case WINDOWS:
133 fprintf (stderr, "windows method not implemented!\n");
134 return ERR_LAZY;
135 default:
136 fprintf (stderr, "unknown unicode method!\n");
137 return ERR_LAYOUT; //TODO: better error code
138 }
139 return ERR_SUCCESS;
140 }
Imprint / Impressum