-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.c
334 lines (278 loc) · 10 KB
/
main.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Copyright 2020 Mikhail ysph Subbotin
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
#include <libusb-1.0/libusb.h>
#include "mouselist.h"
#include "miscellaneous.h"
#define LIBUSB_OPTION_LOG_LEVEL 0
#define LIBUSB_LOG_LEVEL_ERROR 1
static libusb_device_handle *devh = NULL;
libusb_context *global_context;
static int source, type, R, G, B;
static const int bmRequestType = 0x21; // type and recipient: 0b00100001
static const int bRequest = 0x09; // type of request: set_report
static const int wValue = 0x0211; // report type and id: output, 0x11
int wIndex, returnCode, found = 0;
Item* available_head; // the list contains available devices
//temporary
int temp_id;
void CloseDeviceAndExit(void) {
if (devh)
libusb_close(devh);
libusb_exit(NULL);
}
void DetachKernel(void) {
if (libusb_kernel_driver_active(devh, wIndex)) {
libusb_detach_kernel_driver(devh, wIndex);
}
returnCode = libusb_claim_interface(devh, wIndex);
if (returnCode < 0) {
fprintf(stderr, "Error: Cannot claim interface: %s\n",
libusb_error_name(returnCode));
CloseDeviceAndExit();
return;
}
}
void AttachKernel(void) {
libusb_release_interface(devh, wIndex);
if (!libusb_kernel_driver_active(devh, wIndex)) {
libusb_attach_kernel_driver(devh, wIndex);
}
}
int openDevice(void) {
const int available = getSize(available_head);
int choice;
char input_string[20];
printf("\nChoose what device you would like to operate on. Available devices:\n");
printAllItems(available_head);
printf("Enter [0] to exit.\n");
LOOP:
fgets(input_string, 20, stdin);
choice = strtol(input_string, NULL, 0);
if ((choice < 0) || (choice > available)) {
printf("Choose correct number or exit!\n");
fflush(stdin);
goto LOOP;
} else if (choice == 0) {
printf("Exiting...\n");
fflush(stdin);
return 2;
}
const int needed_id = getNthId(available_head, choice);
const char* temp_name = getName(available_head, needed_id);
//open device
devh = libusb_open_device_with_vid_pid(NULL, ID_VENDOR, needed_id);
if (!devh) {
fprintf(stderr, "Error: Cannot open %s\n", temp_name);
return -1;
}
printf("\nDevice %s is operating...\n", temp_name);
//process
srand((unsigned)time(NULL));
wIndex = getInterface(available_head, needed_id);
int devByte[4];
// we dont choose what we change yet
// devByte[0] is changed to 0x10 when we change dpi or response rate
// devByte[3] is changing as well
devByte[0] = 0x11;
devByte[2] = getByte3(available_head, needed_id);
devByte[3] = 0x3b;
// exclusive option for logitech pro wireless
switch (wIndex) {
case 1:
devByte[1] = 0xff;
break;
case 2:
devByte[1] = 0x01;
break;
default:
printf("Error: Wrong interface!\n");
return -1;
}
if (needed_id == 0xc088) {
wIndex = 2;
devByte[1] = 0xff;
}
uint32_t random = (uint32_t)rand();
type = 0x01; // static
source = random & 0x01; // 0 - primary, 1 - logo
R = random & 0xff;
G = (random >> 8) & 0xff;
B = (random >> 16) & 0xff;
unsigned char data[20] = {devByte[0], devByte[1], devByte[2], devByte[3], source, type, R, G, B, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
/* detach kernel
&
claim the interface on a given device handle */
DetachKernel();
returnCode = libusb_control_transfer(devh, bmRequestType, bRequest, wValue,
wIndex, data, sizeof(data), 2000);
if (returnCode < 0) {
fprintf(stderr, "Error: Cannot transfer control data: %s\n", libusb_error_name(returnCode));
}
/* release the interface previously claimed
&
attach kernel */
AttachKernel();
if (devh)
libusb_close(devh);
return EXIT_SUCCESS;
}
int getDevice(Item* head) {
libusb_device **list;
struct libusb_device_descriptor desc;
int i;
ssize_t count = libusb_get_device_list(global_context, &list);
for (i = 0; i < count; ++i) {
libusb_device *device = list[i];
if (!libusb_get_device_descriptor(device, &desc)) {
if (desc.idProduct == ID_PRODUCT_UNIDENTIFIED) {
printf("Found wireless logitech device, but it's UNIDENTIFIED.\n");
printf("Consider upgrading the kernel to at least version of 5.2.\nOr use wired option of your mouse.\n\n");
continue;
}
if (ID_VENDOR == desc.idVendor && searchItem(head, desc.idProduct)) {
const char* temp_name = getName(head, desc.idProduct);
const int temp_interface = getInterface(head, desc.idProduct);
const int temp_byte3 = getByte3(head, desc.idProduct);
pushItem(&available_head, desc.idProduct, temp_name, temp_interface, temp_byte3);
printf("\nDevice id=0x%x, name=%s, interface=%x - has been found!\n", desc.idProduct, temp_name, temp_interface);
found++;
}
}
}
if (!found) return found;
libusb_free_device_list(list, 1);
return 1;
}
/**
* @brief Check if the device is in the list of unsupported devices
* @param head - the list of unsupported devices, which is **deleted** after the check
* @return EXIT_SUCCESS if the device is supported, EXIT_FAILURE otherwise
*/
int unsupportedDevice(Item* head) {
libusb_device **list;
struct libusb_device_descriptor desc;
int i;
ssize_t count = libusb_get_device_list(global_context, &list);
for (i = 0; i < count; ++i) {
libusb_device *device = list[i];
if (!libusb_get_device_descriptor(device, &desc)) {
if (ID_VENDOR == desc.idVendor && searchItem(head, desc.idProduct)) {
const char* temp_name = getName(head, desc.idProduct);
printf("\nDevice id=0x%x, name=%s - is not supported yet!\n", desc.idProduct, temp_name);
libusb_free_device_list(list, 1);
deleteLinkedList(&head);
return EXIT_FAILURE;
}
}
}
libusb_free_device_list(list, 1);
deleteLinkedList(&head);
return EXIT_SUCCESS;
}
int main(void) {
// init
returnCode = libusb_init(NULL);
#if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01000106) // >=1.0.22
libusb_set_option(global_context, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_ERROR);
#else
libusb_set_debug(global_context, LIBUSB_LOG_LEVEL_ERROR);
#endif
if (returnCode < 0) {
fprintf(stderr, "Error: Cannot initialize libusb. %s\n", libusb_error_name(returnCode));
return returnCode;
}
// add known devices
Item* head = (Item*)malloc(size_of_Item);
head->next = NULL;
Item* unsuported = (Item*)malloc(size_of_Item);
unsuported->next = NULL;
pushItem(&head, 0xc092, "G102-G203 LIGHTSYNC", WIRED_OR_CABLE, 0x0e);
pushItem(&head, 0xc084, "G203 Prodigy", WIRED_OR_CABLE, 0x0e);
pushItem(&head, 0xc083, "G403 Prodigy", WIRED_OR_CABLE, 0x0e);
pushItem(&unsuported, 0xc07f, "G302 Daedalus Prime", WIRED_OR_CABLE,-1);
pushItem(&unsuported, 0xc080, "G303 Daedalus Apex", WIRED_OR_CABLE,-1);
pushItem(&unsuported, 0x4074, "G305 Lightspeed Wireless", WIRELESS_RECEIVER,-1);
pushItem(&unsuported, 0xc07e, "G402 Hyperion Fury", WIRED_OR_CABLE,-1);
pushItem(&unsuported, 0xc08f, "G403 Hero", WIRED_OR_CABLE, -1);
pushItem(&head, 0xc082, "G403 Wireless", WIRED_OR_CABLE, 0x18);
pushItem(&head, 0x405d, "G403 Wireless", WIRELESS_RECEIVER, 0x18);
pushItem(&unsuported, 0xc07d, "G502 Proteus Core", WIRED_OR_CABLE, -1);
pushItem(&unsuported, 0xc08b, "G502 Hero", WIRED_OR_CABLE,-1);
pushItem(&head, 0xc332, "G502 Proteus Spectrum", WIRED_OR_CABLE, 0x02);
pushItem(&unsuported, 0xc08d, "G502 Lightspeed Wireless", WIRED_OR_CABLE, -1);
pushItem(&unsuported, 0x407f, "G502 Lightspeed Wireless", WIRELESS_RECEIVER, -1);
pushItem(&unsuported, 0xc08e, "MX518", WIRED_OR_CABLE, -1);
pushItem(&unsuported, 0xc24a, "G600 MMO", WIRED_OR_CABLE, -1);
pushItem(&unsuported, 0xc537, "G602 Wireless", WIRELESS_RECEIVER, -1);
pushItem(&unsuported, 0x406c, "G603 Lightspeed Wireless", WIRELESS_RECEIVER, -1);
pushItem(&unsuported, 0xb024, "G604 Lightspeed Wireless", WIRED_OR_CABLE, -1);
pushItem(&unsuported, 0x4085, "G604 Lightspeed Wireless", WIRELESS_RECEIVER, -1);
pushItem(&head, 0xc087, "G703 Lightspeed Wireless", WIRED_OR_CABLE, 0x18);
pushItem(&head, 0x4070, "G703 Lightspeed Wireless", WIRELESS_RECEIVER, 0x18);
pushItem(&unsuported, 0xc090, "G703 Lightspeed Hero Wireless", WIRED_OR_CABLE, -1);
pushItem(&unsuported, 0x4086, "G703 Lightspeed Hero Wireless", WIRELESS_RECEIVER, -1);
pushItem(&unsuported, 0xc081, "G900 Chaos Spectrum Wireless", WIRED_OR_CABLE, -1);
pushItem(&unsuported, 0x4053, "G900 Chaos Spectrum Wireless", WIRELESS_RECEIVER, -1);
pushItem(&unsuported, 0xc086, "G903 Lightspeed Wireless", WIRED_OR_CABLE, -1);
pushItem(&unsuported, 0x4067, "G903 Lightspeed Wireless", WIRELESS_RECEIVER, -1);
pushItem(&unsuported, 0xc091, "G903 Lightspeed Hero Wireless", WIRED_OR_CABLE, -1);
pushItem(&unsuported, 0x4087, "G903 Lightspeed Hero Wireless", WIRELESS_RECEIVER, -1);
pushItem(&unsuported, 0xc085, "PRO", WIRED_OR_CABLE, -1);
pushItem(&unsuported, 0xc08c, "PRO HERO", WIRED_OR_CABLE, -1);
pushItem(&head, 0xc088, "PRO Wireless", WIRED_OR_CABLE, 0x07);
pushItem(&head, 0x4079, "PRO Wireless", WIRELESS_RECEIVER, 0x07);
// list for available devices
available_head = (Item*)malloc(size_of_Item);
available_head->next = NULL;
// check unsupported devices
returnCode = unsupportedDevice(unsuported);
if (returnCode == EXIT_FAILURE){
deleteLinkedList(&head);
deleteLinkedList(&available_head);
CloseDeviceAndExit();
return EXIT_FAILURE;
}
// find device
returnCode = getDevice(head);
if (!returnCode) {
fprintf(stderr, "Error: Cannot find any logitech mouse. %s\n", libusb_error_name(returnCode));
CloseDeviceAndExit();
return returnCode;
}
returnCode = openDevice();
if (returnCode == 2) {
deleteLinkedList(&head);
deleteLinkedList(&available_head);
CloseDeviceAndExit();
return EXIT_SUCCESS;
}
if (returnCode < 0) {
fprintf(stderr, "Error: Cannot operate logitech mouse. %s\n", libusb_error_name(returnCode));
CloseDeviceAndExit();
return EXIT_FAILURE;
}
if (returnCode >= 0) {
printf("Now, the color of your ");
switch (source) {
case 0:
printf("primary ");
break;
case 1:
printf("logo ");
break;
default:
printf("undefined!\n");
deleteLinkedList(&head);
deleteLinkedList(&available_head);
exit(EXIT_FAILURE);
}
printf("is #%02x%02x%02x\n",R,G,B);
}
deleteLinkedList(&head);
deleteLinkedList(&available_head);
libusb_exit(NULL);
return EXIT_SUCCESS;
}