-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain-linux.c
210 lines (181 loc) · 5.39 KB
/
main-linux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <hid.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h> /* for getopt() */
#include <signal.h>
#include <suinput.h>
#include "gkos.h"
// uinput device (see gkos.c for my explanation of why a global var)
static int uinput_fd;
bool match_serial_number(struct usb_dev_handle* usbdev, void* custom, unsigned int len)
{
bool ret;
char* buffer = (char*)malloc(len);
usb_get_string_simple(usbdev, usb_device(usbdev)->descriptor.iSerialNumber,
buffer, len);
ret = strncmp(buffer, (char*)custom, len) == 0;
free(buffer);
return ret;
}
char keys_to_keymap(char *keys, int keylength) {
// TODO: make this configurable, rather than being hardcoded to my keypad
char keycode = 0;
int i;
for(i=0; i< keylength; i++) {
switch(keys[i]) {
case 0x2b:
keycode |= GKOS_A;
break;
case 0x4b:
keycode |= GKOS_B;
break;
case 0x4e:
keycode |= GKOS_C;
break;
case 0x5f:
keycode |= GKOS_D;
break;
case 0x60:
keycode |= GKOS_E;
break;
case 0x61:
keycode |= GKOS_F;
break;
default:
break;
}
}
return keycode;
}
bool gkos_send_keydown(uint16_t keycode) {
return suinput_press(uinput_fd, keycode);
}
bool gkos_send_keyup(uint16_t keycode) {
return suinput_release(uinput_fd, keycode);
}
bool done = false;
void sighandler(int sig) {
done = true;
}
int main(int argc, char *argv[])
{
struct input_id in_id = {
BUS_BLUETOOTH, /* Bus type. */
13, /* Vendor id. */
3, /* Product id. */
7 /* Version id. */
};
HIDInterface* hid;
int iface_num = 0;
hid_return ret;
unsigned short vendor_id = 0x04b4;
unsigned short product_id = 0x0101;
char *vendor, *product;
int flag;
signal(SIGABRT, &sighandler);
signal(SIGTERM, &sighandler);
signal(SIGINT, &sighandler);
/* Parse command-line options.
*
* Currently, we only accept the "-d" flag, which works like "lsusb", and the
* "-i" flag to select the interface (default 0). The syntax is one of the
* following:
*
* $ gkos -d 1234:
* $ gkos -d :5678
* $ gkos -d 1234:5678
*
* Product and vendor IDs are assumed to be in hexadecimal.
*
* TODO: error checking and reporting.
*/
while((flag = getopt(argc, argv, "d:i:")) != -1) {
switch (flag) {
case 'd':
product = optarg;
vendor = strsep(&product, ":");
if(vendor && *vendor) {
vendor_id = strtol(vendor, NULL, 16);
}
if(product && *product) {
product_id = strtol(product, NULL, 16);
}
break;
case 'i':
iface_num = atoi(optarg);
break;
}
}
HIDInterfaceMatcher matcher = { vendor_id, product_id, NULL, NULL, 0 };
/* see include/debug.h for possible values */
//hid_set_debug(HID_DEBUG_ALL);
//hid_set_debug_stream(stderr);
/* passed directly to libusb */
//hid_set_usb_debug(0);
ret = hid_init();
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_init failed with return code %d\n", ret);
return 1;
}
hid = hid_new_HIDInterface();
if (hid == 0) {
fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
return 1;
}
ret = hid_force_open(hid, iface_num, &matcher, 3);
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
return 1;
}
// TODO: Read PATHLEN, PATH_IN, RECV_PACKET_LEN and SLEEP_LENGTH from the actual device
const int PATHLEN = 2;
const int PATH_IN[2] = { 0x00010006, 0x00000000 };
const int RECV_PACKET_LEN = 8;
const int SLEEP_LENGTH = 10000; // 10 milliseconds
char *packet = malloc(RECV_PACKET_LEN);
char keymap;
uinput_fd = suinput_open("GKOSInput", &in_id);
if (!packet) {
perror("Couldn't allocate input buffer.");
done = true;
}
if (!uinput_fd) {
perror("Couldn't create virtual input device.");
done = true;
}
if (!gkos_init()) {
perror("Couldn't initialize gkos.");
done = true;
}
while (!done) {
usleep(SLEEP_LENGTH);
ret = hid_get_input_report(hid, PATH_IN, PATHLEN, packet, RECV_PACKET_LEN);
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_get_input_report failed with return code %d\n", ret);
return 1;
}
/*
TODO: make this more generic, rather than assuming all
devices work like mine check the HID standard for how
keyboards send their input
*/
keymap = keys_to_keymap(packet, RECV_PACKET_LEN);
if (!gkos_handle_keys(keymap)) {
done = true;
}
}
if (packet) free(packet);
if (uinput_fd) suinput_close(uinput_fd);
ret = hid_close(hid);
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_close failed with return code %d\n", ret);
return 1;
}
hid_delete_HIDInterface(&hid);
ret = hid_cleanup();
if (ret != HID_RET_SUCCESS) {
fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
return 1;
}
return 0;
}