-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathharpoond.c
298 lines (248 loc) · 8.54 KB
/
harpoond.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <libusb.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#define VENDOR_ID 0x1b1c
#define WIRED_DEVICE_ID 0x1b5e
#define DONGLE_DEVICE_ID 0x1b65
#define BUFFER_SIZE 64
#define ENDPOINT_OUT 0x04
#define ENDPOINT_IN 0x84
#define WIRED_COMMAND_PREFIX 0x08
#define DONGLE_COMMAND_PREFIX 0x09
static volatile short RUNNING = 1;
static struct timeval zero_tv = {0};
typedef enum {NONE = 0, WIRED, DONGLE} DeviceType;
typedef struct
{
DeviceType type;
unsigned char command_prefix;
libusb_device_handle *handle;
short initialized;
} Device;
static void signal_handler()
{
RUNNING = 0;
}
static int transfer(Device *device, unsigned int length, ...)
{
int r;
int transferred;
unsigned char buffer[BUFFER_SIZE];
va_list valist;
va_start(valist, length);
for (unsigned int i = 0; i < BUFFER_SIZE; i++)
buffer[i] = i < length ? va_arg(valist, int) : 0x00;
va_end(valist);
r = libusb_interrupt_transfer(device->handle, ENDPOINT_OUT, buffer, BUFFER_SIZE, &transferred, 100);
if (transferred < BUFFER_SIZE) {
fprintf(stderr, "short write (%d, error %d)\n", transferred, r);
return -1;
}
r = libusb_interrupt_transfer(device->handle, ENDPOINT_IN, buffer, BUFFER_SIZE, &transferred, 100);
if (transferred < BUFFER_SIZE) {
fprintf(stderr, "short read (%d, error %d)\n", transferred, r);
return -1;
}
return 0;
}
static int grab_device(Device *device)
{
int r;
if (libusb_kernel_driver_active(device->handle, 1) == 1) {
r = libusb_detach_kernel_driver(device->handle, 1);
if (r < 0) {
fprintf(stderr, "Failed to detach kernel driver\n");
return r;
}
}
r = libusb_claim_interface(device->handle, 1);
if (r < 0) {
fprintf(stderr, "Failed to claim interface\n");
return r;
}
return 0;
}
static int ungrab_device(Device *device)
{
int r;
r = libusb_release_interface(device->handle, 1);
if (r < 0) {
fprintf(stderr, "Failed to release interface\n");
return r;
}
r = libusb_attach_kernel_driver(device->handle, 1);
if (r < 0) {
fprintf(stderr, "Failed to attach kernel driver\n");
return r;
}
return 0;
}
static void init_device(Device *device)
{
if (device->handle == NULL || device->type == NONE) {
fprintf(stderr, "Cannot initialize invalid device\n");
return;
}
grab_device(device);
/* Init */
transfer(device, 5, 0x08, 0x01, 0x03, 0x00, 0x02);
if (device->type == DONGLE)
transfer(device, 5, 0x09, 0x01, 0x03, 0x00, 0x02);
transfer(device, 4, device->command_prefix, 0x0d, 0x00, 0x01);
/* Set custom configuration */
transfer(device, 13, device->command_prefix,
0x06, 0x00, 0x06, 0x00, 0x00, 0x00, /* Do not change */
0x00, /* Indicator LED's red */
0x00, /* Main LED's red */
0xff, /* Indicator LED's green */
0x00, /* Main LED's green */
0x00, /* Indicator LED's blue */
0x00); /* Main LED's blue */
transfer(device, 6, device->command_prefix,
0x01, 0x20, 0x00, /* Do not change */
0x08, 0x70); /* DPI, (708 hex = 1800) */
/*
* Note that values are encoded in little-endian system, meaning the least
* significant portion (bytes) of a number comes first. For example, if you
* want to set 3200 DPI, first convert it to hexadecimal, which is 0xC80,
* then split it in half and reverse the order: 0x00, 0xC8, finally pass it
* to the function call above.
* More examples:
* 3000 DPI -> 0xBB8 -> 0xBB, 0x08 -> 0x08, 0xBB
* 10000 DPI -> 0x2710 -> 0x27, 0x10 -> 0x10, 0x27
*
* Once you set a new DPI value and recompile harpoond, please stop it, turn
* the mouse off and on then start harpoond again.
*
* Also, note that the mouse is kind of picky with these values, some values
* are completely ignored. I couldn't figure out any pattern here, so you
* need to spend some time on trial & error until you get a value that works
* and is close to what you want.
*/
ungrab_device(device);
device->initialized = 1;
}
static int LIBUSB_CALL attach_cb(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
int r;
Device *device;
struct libusb_device_descriptor desc;
(void)ctx;
(void)dev;
(void)event;
printf("Device attached\n");
device = (Device*)user_data;
r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
fprintf(stderr, "Error getting device descriptor\n");
return 0;
}
r = libusb_open(dev, &device->handle);
if (r < 0) {
fprintf(stderr, "Error opening device\n");
return 0;
}
device->type = desc.idProduct == WIRED_DEVICE_ID ? WIRED : DONGLE;
device->command_prefix = desc.idProduct == WIRED_DEVICE_ID ? WIRED_COMMAND_PREFIX : DONGLE_COMMAND_PREFIX;
device->initialized = 0;
return 0;
}
static int LIBUSB_CALL detach_cb(libusb_context *ctx, libusb_device *dev, libusb_hotplug_event event, void *user_data)
{
Device *device;
(void)ctx;
(void)dev;
(void)event;
printf("Device detached\n");
device = (Device*)user_data;
device->type = NONE;
libusb_close(device->handle);
device->handle = NULL;
return 0;
}
static void keep_alive(Device *device)
{
if (device->type == NONE) return;
if (!device->initialized) init_device(device);
if (grab_device(device) < 0) return;
transfer(device, 2, device->command_prefix, 0x12);
ungrab_device(device);
}
int main()
{
int r;
Device device;
libusb_hotplug_callback_handle hp[4];
/*
* INIT LIBUSB
*/
r = libusb_init(NULL);
if (r < 0) return r;
/*
* DEVICE LOOKUP
*/
device.initialized = 0;
device.command_prefix = WIRED_COMMAND_PREFIX;
device.type = WIRED;
device.handle = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, WIRED_DEVICE_ID);
if (device.handle == NULL) {
device.command_prefix = DONGLE_COMMAND_PREFIX;
device.type = DONGLE;
device.handle = libusb_open_device_with_vid_pid(NULL, VENDOR_ID, DONGLE_DEVICE_ID);
}
if (device.handle == NULL) {
device.type = NONE;
printf("Device not found, waiting for it to be plugged\n");
}
/*
* REGISTER HOTPLUG CALLBACKS
*/
if (libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) {
libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
0, VENDOR_ID, WIRED_DEVICE_ID,
LIBUSB_HOTPLUG_MATCH_ANY,
attach_cb, &device, &hp[0]);
libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
0, VENDOR_ID, DONGLE_DEVICE_ID,
LIBUSB_HOTPLUG_MATCH_ANY,
attach_cb, &device, &hp[1]);
libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
0, VENDOR_ID, WIRED_DEVICE_ID,
LIBUSB_HOTPLUG_MATCH_ANY,
detach_cb, &device, &hp[2]);
libusb_hotplug_register_callback(NULL, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
0, VENDOR_ID, DONGLE_DEVICE_ID,
LIBUSB_HOTPLUG_MATCH_ANY,
detach_cb, &device, &hp[3]);
} else {
printf("Hotplug capabilites are not supported on this platform\n");
}
/*
* REGISTER SIGNAL HANDLERS
*/
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
/*
* KEEP ALIVE AND EVENT LOOP
*/
while (RUNNING) {
keep_alive(&device);
r = libusb_handle_events_timeout_completed(NULL, &zero_tv, NULL);
if (r < 0)
fprintf(stderr, "libusb failed to handle events: %s\n", libusb_error_name(r));
sleep(2);
}
/*
* TEAR DOWN
*/
printf("Cleaning up...\n");
libusb_hotplug_deregister_callback(NULL, hp[0]);
libusb_hotplug_deregister_callback(NULL, hp[1]);
libusb_hotplug_deregister_callback(NULL, hp[2]);
libusb_hotplug_deregister_callback(NULL, hp[3]);
libusb_close(device.handle);
libusb_exit(NULL);
return 0;
}