-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.c
1809 lines (1515 loc) · 56.9 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim:set ts=4 sw=4 tw=80 et cindent ai si cino=(0,ml,\:0:
* ( settings from: http://datapax.com.au/code_conventions/ )
*/
/**********************************************************************
RatSlap
Copyright (C) 2016-2020 Todd Harbour
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 ONLY, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program, in the file COPYING or COPYING.txt; if
not, see http://www.gnu.org/licenses/ , or write to:
The Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <libusb-1.0/libusb.h>
#include <linux/hid.h>
#include "app.h"
#include "lang.h"
#include "git.h"
#include "log.h"
#define LOGITECH_G300S_VENDOR_ID 0x046d
#define LOGITECH_G300S_PRODUCT_ID 0xc246
// QB#111 - Older version (eg 1.0.14) didn't support libusb_strerror
#ifndef libusb_strerror
#define libusb_strerror libusb_error_name
#endif
// http://www.tldp.org/LDP/abs/html/exitcodes.html
typedef enum e_exit {
exit_none = 0
,exit_param = 63
,exit_usberr
,exit_modesel
} t_exit;
typedef enum e_mode {
mode_f3 = 0
,mode_f4
,mode_f5
,mode_COUNT
} t_mode;
const char *s_mode[] = {
"F3"
,"F4"
,"F5"
,"INVALID"
};
typedef enum e_colour {
colour_black = 0
,colour_red
,colour_green
,colour_yellow
,colour_blue
,colour_magenta
,colour_cyan
,colour_white
,colour_COUNT
} t_colour;
const char *s_colour[] = {
"black"
,"red"
,"green"
,"yellow"
,"blue"
,"magenta"
,"cyan"
,"white"
,"INVALID"
};
// F5040302 84060844 01000002 00000300 00040000 05000006 00000700 00080000 090000
// ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
// 0 8 11 14 17 20 23 26 29 32
const unsigned char offsets_buttons[] = {
0
, 8
,11
,14
,17
,20
,23
,26
,29
,32
};
const char *s_buttons[] = {
"NONE"
,"Button1"
,"Button2"
,"Button3"
,"Button6"
,"Button7"
,"Button8"
,"Button9"
,"Button10"
,"Button11"
,"DPIUp"
,"DPIDown"
,"DPICycle"
,"ModeSwitch"
,"DPIShift"
,"DPIDefault"
};
// Turns out these are likely HID standard codes!
// ( https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf )
const char *s_keys[] = {
"NONE"
,"UNKNOWN:01" // 01 == 1 // "HID: Keyboard Err: Rollover - not a key
,"UNKNOWN:02" // 02 == 2 // "HID: Keyboard Err: POST Fail - not a key
,"UNKNOWN:03" // 03 == 3 // "HID: Keyboard Err: Undefined - not a key
,"A" // 04 == 4
,"B"
,"C"
,"D"
,"E"
,"F"
,"G"
,"H"
,"I"
,"J"
,"K"
,"L"
,"M"
,"N"
,"O"
,"P"
,"Q"
,"R"
,"S"
,"T"
,"U"
,"V"
,"W"
,"X"
,"Y"
,"Z" // 1D == 29
,"1" // 1E == 30
,"2"
,"3"
,"4"
,"5"
,"6"
,"7"
,"8"
,"9"
,"0" // 27 == 39
,"Enter" // 28 == 40
,"Escape" // 29 == 41
,"Backspace" // 2a == 42
,"Tab" // 2b == 43
,"Space" // 2c == 44
,"-" // 2d == 45
,"=" // 2e == 46
,"[" // 2f == 47
,"]" // 30 == 48
,"\\" // 31 == 49
,"NonUS#" // 32 == 50
,";" // 33 == 51
,"'" // 34 == 52
,"`" // 35 == 53
,"," // 36 == 54
,"." // 37 == 55
,"/" // 38 == 56
,"CapsLock" // 39 == 57
,"F1" // 3a == 58
,"F2" // 3b == 59
,"F3" // 3c == 60
,"F4" // 3d == 61
,"F5" // 3e == 62
,"F6" // 3f == 63
,"F7" // 40 == 64
,"F8" // 41 == 65
,"F9" // 42 == 66
,"F10" // 43 == 67
,"F11" // 44 == 68
,"F12" // 45 == 69
,"PrintScreen" // 46 == 70
,"ScrollLock" // 47 == 71
,"Pause" // 48 == 72
,"Insert" // 49 == 73
,"Home" // 4a == 74
,"PageUp" // 4b == 75
,"Delete" // 4c == 76
,"End" // 4d == 77
,"PageDown" // 4e == 78
,"Right" // 4f == 79
,"Left" // 50 == 80
,"Down" // 51 == 81
,"Up" // 52 == 82
,"NumLock" // 53 == 83
,"Num/" // 54 == 84
,"Num*" // 55 == 85
,"Num-" // 56 == 86
,"Num+" // 57 == 87
,"NumEnter" // 58 == 88
,"Num1" // 59 == 89
,"Num2" // 5a == 90
,"Num3" // 5b == 91
,"Num4" // 5c == 92
,"Num5" // 5d == 93
,"Num6" // 5e == 94
,"Num7" // 5f == 95
,"Num8" // 60 == 96
,"Num9" // 61 == 97
,"Num0" // 62 == 98
,"Num." // 63 == 99
,"NonUS\\" // 64 == 100
,"Application" // 65 == 101
,"Power" // 66 == 102
,"Num=" // 67 == 103
,"F13" // 68 == 104
,"F14" // 69 == 105
,"F15" // 6a == 106
,"F16" // 6b == 107
,"F17" // 6c == 108
,"F18" // 6d == 109
,"F19" // 6e == 110
,"F20" // 6f == 111
,"F21" // 70 == 112
,"F22" // 71 == 113
,"F23" // 72 == 114
,"F24" // 73 == 115
,"Execute" // 74 == 116
,"Help" // 75 == 117
,"Menu" // 76 == 118
,"Select" // 77 == 119
,"Stop" // 78 == 120
,"Again" // 79 == 121
,"Undo" // 7a == 122
,"Cut" // 7b == 123
,"Copy" // 7c == 124
,"Paste" // 7d == 125
,"Find" // 7e == 126
,"Mute" // 7f == 127
,"VolumeUp" // 80 == 128
,"VolumeDown" // 81 == 129
,"UNKNOWN:82" // 82 == 130 // Locking CapsLock but legacy so not defining
,"UNKNOWN:83" // 83 == 131 // Locking CapsLock but legacy so not defining
,"UNKNOWN:84" // 84 == 132 // Locking CapsLock but legacy so not defining
,"Num," // 85 == 133 // Brazillian keypad period (.)?
,"AS400Num=" // 86 == 134 // Keypad Equal Sign on AS/400 keyboards
,"UNKNOWN:87" // 87 == 135 // International 1?
,"UNKNOWN:88" // 88 == 136 // International 2?
,"UNKNOWN:89" // 89 == 137 // International 3?
,"UNKNOWN:8a" // 8a == 138 // International 4?
,"UNKNOWN:8b" // 8b == 139 // International 5?
,"UNKNOWN:8c" // 8c == 140 // International 6?
,"UNKNOWN:8d" // 8d == 141 // International 7?
,"UNKNOWN:8e" // 8e == 142 // International 8?
,"UNKNOWN:8f" // 8f == 143 // International 9?
,"UNKNOWN:90" // 90 == 144 // LANG1 - Hangul/English toggle - Korean?
,"UNKNOWN:91" // 91 == 145 // LANG2 - Hanja conversion key - Korean?
,"UNKNOWN:92" // 92 == 146 // LANG3 - Katakana key - Japanese?
,"UNKNOWN:93" // 93 == 147 // LANG4 - Hiragana key - Japanese?
,"UNKNOWN:94" // 94 == 148 // LANG5 - Zenkaku/Hankaku key - Japanese?
,"UNKNOWN:95" // 95 == 149 // LANG6 - Reserved?
,"UNKNOWN:96" // 96 == 150 // LANG7 - Reserved?
,"UNKNOWN:97" // 97 == 151 // LANG8 - Reserved?
,"UNKNOWN:98" // 98 == 152 // LANG9 - Reserved?
,"UNKNOWN:99" // 99 == 153 // Alternate Erase (Erase-Eaze(tm))?
,"SysReq" // 9a == 154 // SysReq/Attention
,"Cancel" // 9b == 155
,"Clear" // 9c == 156
,"Prior" // 9d == 157
,"Return" // 9e == 158
,"Separator" // 9f == 159
,"Out" // a0 == 160
,"Oper" // a1 == 161
,"ClearAgain" // a2 == 162
,"CrSelProps" // a3 == 163
,"ExSel" // a4 == 164
,"UNKNOWN:a5" // a5 == 165 // Reserved
,"UNKNOWN:a6" // a6 == 166 // Reserved
,"UNKNOWN:a7" // a7 == 167 // Reserved
,"UNKNOWN:a8" // a8 == 168 // Reserved
,"UNKNOWN:a9" // a9 == 169 // Reserved
,"UNKNOWN:aa" // aa == 170 // Reserved
,"UNKNOWN:ab" // ab == 171 // Reserved
,"UNKNOWN:ac" // ac == 172 // Reserved
,"UNKNOWN:ad" // ad == 173 // Reserved
,"UNKNOWN:ae" // ae == 174 // Reserved
,"UNKNOWN:af" // af == 175 // Reserved
,"Num00" // b0 == 176
,"Num000" // b1 == 177
,"Sep1000s" // b2 == 178 // Thousands separator - locale specific?
,"SepDec" // b3 == 179 // Decimal separator - locale specific?
,"CurrUnit" // b4 == 180 // Currency Unit - locale specific?
,"CurrSubUnit" // b5 == 181 // Currency Sub-Unit - locale specific?
,"Num(" // b6 == 182
,"Num)" // b7 == 183
,"Num{" // b8 == 184
,"Num}" // b9 == 185
,"NumTab" // ba == 186
,"NumBackspace"// bb == 187
,"NumA" // bc == 188
,"NumB" // bd == 189
,"NumC" // be == 190
,"NumD" // bf == 191
,"NumE" // c0 == 192
,"NumF" // c1 == 193
,"NumXOR" // c2 == 194
,"Num^" // c3 == 195
,"Num%" // c4 == 196
,"Num<" // c5 == 197
,"Num>" // c6 == 198
,"Num&" // c7 == 199
,"Num&&" // c8 == 200
,"Num|" // c9 == 201
,"Num||" // ca == 202
,"Num:" // cb == 203
,"Num#" // cc == 204
,"NumSpace" // cd == 205
,"Num@" // ce == 206
,"Num!" // cf == 207
,"NumMemStore" // d0 == 208
,"NumMemRecall"// d1 == 209
,"NumMemClear" // d2 == 210
,"NumMemAdd" // d3 == 211
,"NumMemSub" // d4 == 212
,"NumMemMul" // d5 == 213
,"NumMemDiv" // d6 == 214
,"NumPlusMinus"// d7 == 215
,"NumClear" // d8 == 216
,"NumClearEntry"// d9 == 217
,"NumBinary" // da == 218
,"NumOctal" // db == 219
,"NumDecimal" // dc == 220
,"NumHex" // dd == 221
,"UNKNOWN:de" // de == 222 // Reserved
,"UNKNOWN:df" // df == 223 // Reserved
,"LeftCtrl" // e0 == 224
,"LeftShift" // e1 == 225
,"LeftAlt" // e2 == 226
,"Super_L" // e3 == 227 // Left GUI
,"RightCtrl" // e4 == 228
,"RightShift" // e5 == 229
,"RightAlt" // e6 == 230
,"Super_R" // e7 == 231 // Right GUI
,"UNKNOWN:e8" // e8 == 232 // Reserved
,"UNKNOWN:e9" // e9 == 233 // Reserved
,"UNKNOWN:ea" // ea == 234 // Reserved
,"UNKNOWN:eb" // eb == 235 // Reserved
,"UNKNOWN:ec" // ec == 236 // Reserved
,"UNKNOWN:ed" // ed == 237 // Reserved
,"UNKNOWN:ee" // ee == 238 // Reserved
,"UNKNOWN:ef" // ef == 239 // Reserved
,"UNKNOWN:f0" // f0 == 240 // Reserved
,"UNKNOWN:f1" // f1 == 241 // Reserved
,"UNKNOWN:f2" // f2 == 242 // Reserved
,"UNKNOWN:f3" // f3 == 243 // Reserved
,"UNKNOWN:f4" // f4 == 244 // Reserved
,"UNKNOWN:f5" // f5 == 245 // Reserved
,"UNKNOWN:f6" // f6 == 246 // Reserved
,"UNKNOWN:f7" // f7 == 247 // Reserved
,"UNKNOWN:f8" // f8 == 248 // Reserved
,"UNKNOWN:f9" // f9 == 249 // Reserved
,"UNKNOWN:fa" // fa == 250 // Reserved
,"UNKNOWN:fb" // fb == 251 // Reserved
,"UNKNOWN:fc" // fc == 252 // Reserved
,"UNKNOWN:fd" // fd == 253 // Reserved
,"UNKNOWN:fe" // fe == 254 // Reserved
// ff = 255 // Reserved (and even if it weren't, it's not
// used in the loops :P)
};
const int report_rate[] = {
1000
, 125
, 250
, 500
};
const int n_report_rates = 4;
libusb_context *_usb_ctx = NULL;
libusb_device_handle *_usb_dev_handle = NULL;
libusb_device *_usb_device = NULL;
struct libusb_device_descriptor _usb_desc;
int _usb_interface_index = -1;
int _mouse_primed = 0;
static int dpi_point(int dpip);
static void help_version(void);
static void help_usage(void);
static void keylist_print(void);
static libusb_context *usb_init(void);
static int usb_deinit(void);
static libusb_device_handle *mouse_init(const uint16_t vendor_id, const uint16_t product_id, const char* product_name);
static int mouse_deinit(void);
static void display_mouse_hid(const uint16_t vendor_id, const uint16_t product_id);
int mouse_hid_detach_kernel(int iface);
int mouse_hid_attach_kernel(int iface);
static t_mode change_mode(libusb_device_handle *usb_dev_handle, t_mode mode);
static int mode_load(unsigned char *mode_data, libusb_device_handle *usb_dev_handle, t_mode mode);
static int mode_save(unsigned char *mode_data, libusb_device_handle *usb_dev_handle, const t_mode mode);
static int mode_print(unsigned char *mode_data, int len);
static int set_mode_rate(unsigned char *mode_data, const int rate);
static int set_mode_dpi(unsigned char *mode_data, const int idx, const int dpi);
static int set_mode_defdpi(unsigned char *mode_data, const int idx);
static int set_mode_enabledpishift(unsigned char *mode_data);
static int set_mode_dpishift(unsigned char *mode_data, const int dpi);
static int set_mode_nodpishift(unsigned char *mode_data);
static unsigned char set_mode_colour(unsigned char *mode_data, const t_colour colour);
static int set_mode_button(unsigned char *mode_data, const unsigned char button, const char *keys);
static int mouse_editmode(void);
int mouse_prime(void);
int mouse_unprime(void);
static int dpi_point(int dpip) {
return (!dpip) ? 4000 : dpip * 250;
}
static void help_version(void) {
printf("%s v%s (BUILT: %s)\n", (APP_NAME), (APP_VERSION), (BUILD_DATE));
printf("%s\n", (APP_COPYRIGHT));
printf("%s\n", (APP_SUMMARY));
printf("%s\n", (APP_URL));
}
static void help_usage(void) {
printf("\
\n\
%s\n\
\n\
%s: %s -h|--help\n\
%s -V|--version\n\
%s --listkeys\n\
%s [-s|--select <mode>] [-p|--print <mode>]\n\
[-m|--modify <mode>\n\
[-r|--rate <rate>]\n\
[-A|--d1|--D1 <dpi>]\n\
[-B|--d2|--D2 <dpi>]\n\
[-C|--d3|--D3 <dpi>]\n\
[-D|--d4|--D4 <dpi>]\n\
[-F|--default-dpi <level>]\n\
[-S|--dpishift [<dpi>]]\n\
[-U|--no-dpishift]\n\
[-c|--colour|--color <colour>]\n\
[-1|--left <keys>]\n\
[-2|--right <keys>]\n\
[-3|--middle <keys>]\n\
[-4|--g4|--G4 <keys>]\n\
[-5|--g5|--G5 <keys>]\n\
[-6|--g6|--G6 <keys>]\n\
[-7|--g7|--G7 <keys>]\n\
[-8|--g8|--G8 <keys>]\n\
[-9|--g9|--G9 <keys>]\n\
] [...]\n\
\n\
-h|--h[elp] - %s\n\
-V|--v[ersion] - %s %s %s\n\
--li[stkeys] - %s\n\
-s|--s[elect] - %s\n\
-p|--p[rint] - %s\n\
-m|--mo[dify] - %s\n\
-r|--ra[te] - %s\n\
-A|--d1 ... -D|--d4 - %s\n\
-F|--de[fault-dpi] - %s\n\
-S|--dp[ishift] - %s\n\
-U|--n[o-dpishift] - %s\n\
-c|--c[olour]|--c[olor] - %s\n\
-1|--le[ft] - %s\n\
-2|--ri[ght] - %s\n\
-3|--mi[ddle] - %s\n\
-4|--g4|--G4 - %s\n\
-5|--g5|--G5 - %s\n\
-6|--g6|--G6 - %s\n\
-7|--g7|--G7 - %s\n\
-8|--g8|--G8 - %s\n\
-9|--g9|--G9 - %s\n\
\n\
<mode> - %s\n\
<rate> - %s\n\
<dpi> - %s\n\
<level> - %s\n\
<colour> - %s\n\
<keys> - %s\n\
%s\n\
\n\
%s: %s -p f3 -pF4 --selec F3 -m F4 -c bLuE -9LeftCtrl+V\n\
",
_(APP_SUMMARY)
,_("Usage"), BIN_NAME /* -h|--h[elp] */
, BIN_NAME /* -V|--v[ersion] */
, BIN_NAME /* --listkeys */
, BIN_NAME /* -s|--s[elect] ... */
,_("Displays this help")
,_("Displays"), APP_NAME, _("version")
,_("Lists all possible modifiers, buttons and keys for assignment")
,_("Switches to <mode>")
,_("Prints out <mode>'s button configuration")
,_("Sets current <mode> to be modified")
,_("Sets report <rate> of <mode> currently being modified")
,_("Sets DPI sensitivity level 1, 2, 3, or 4")
,_("Sets which DPI sensitivity level is the default")
,_("Enables DPI shift function; with argument, sets the DPI")
,_("Disables DPI shift function")
,_("Sets <colour> of <mode> currently being modified")
,_("Assigns <keys> to button 1 of <mode> currently being modified")
,_("Assigns <keys> to button 2 of <mode> currently being modified")
,_("Assigns <keys> to button 3 of <mode> currently being modified")
,_("Assigns <keys> to button 4 of <mode> currently being modified")
,_("Assigns <keys> to button 5 of <mode> currently being modified")
,_("Assigns <keys> to button 6 of <mode> currently being modified")
,_("Assigns <keys> to button 7 of <mode> currently being modified")
,_("Assigns <keys> to button 8 of <mode> currently being modified")
,_("Assigns <keys> to button 9 of <mode> currently being modified")
,_("A valid mode: F3, F4 or F5")
,_("A valid rate: 125, 250, 500, 1000")
,_("A valid DPI: 250, 500, 750, ..., 3500, 3750, 4000")
,_("A valid DPI level: 1, 2, 3, 4")
,_("A valid colour: black, red, green, yellow, blue, magenta, cyan, white")
,_("A valid combo of keys: Any button or key combo, eg. LeftCtrl+LeftAlt+PageUp")
,_("Run with --listkeys to see the complete list")
,_("Example"), BIN_NAME
);
}
static void keylist_print(void) {
unsigned char bt;
printf("\nMODIFIERS:\n");
// 0xe0 - 0xe7 match modifiers 0x01, 0x02, 0x04 ... 0x80
for (bt = 0xe0; bt <= 0xe7; ++bt) {
printf(" %s\n", s_keys[bt]);
}
printf("\nBUTTONS/SPECIALS:\n");
for (bt = 0x00; bt <= 0x0f; ++bt) {
printf(" %s\n", s_buttons[bt]);
}
printf("\nKEYS:\n");
for (bt = 1; bt < 0xff; ++bt) {
if (strncmp(s_keys[bt], "UNKNOWN", 7) == 0) continue;
if (s_keys[bt][0] == 'A') {
printf(" A ... Z\n");
bt += 25;
continue;
}
if (s_keys[bt][0] == '1') {
printf(" 0 ... 9\n");
bt += 9;
continue;
}
if (strncmp(s_keys[bt], "F1", 2) == 0) {
printf(" F1 ... F12\n");
bt += 11;
continue;
}
if (strncmp(s_keys[bt], "Num1", 4) == 0) {
printf(" Num0 ... Num9\n");
bt += 9;
continue;
}
printf(" %s\n", s_keys[bt]);
}
}
static libusb_context *usb_init(void) {
if (_usb_ctx) return _usb_ctx;
// Initialise the USB context
libusb_init(&_usb_ctx);
if (!_usb_ctx) {
elog("ERROR: Failed to initialise USB interface\n");
return NULL;
}
// TODO: Determine if we need to set debug here
#if LIBUSBX_API_VERSION < 0x01000106
libusb_set_debug(_usb_ctx, 3);
#else
libusb_set_option(_usb_ctx, LIBUSB_OPTION_LOG_LEVEL, 3);
#endif
return _usb_ctx;
}
static int usb_deinit(void) {
// Finish up with USB context
if (_usb_ctx) libusb_exit(_usb_ctx);
_usb_ctx = NULL;
return 1;
}
static libusb_device_handle *mouse_init(const uint16_t vendor_id, const uint16_t product_id, const char* product_name) {
if (!_usb_ctx) return NULL;
if (!_usb_dev_handle) {
// Look at the mouse based on vendor and usb_device id
// TODO: According to doco (
// http://libusb.sourceforge.net/api-1.0/group__dev.html#ga11ba48adb896b1492bbd3d0bf7e0f665
// ):
// > This function is intended for those scenarios where you are using
// > libusb to knock up a quick test application - it allows you to
// > avoid calling libusb_get_device_list() and worrying about
// > traversing/freeing the list.
//
// > This function has limitations and is hence not intended for use
// > in real applications: if multiple devices have the same IDs it
// > will only give you the first one, etc.
_usb_dev_handle = libusb_open_device_with_vid_pid(_usb_ctx, vendor_id, product_id);
if (!_usb_dev_handle) {
elog("Failed to find %s (%.4x:%.4x)\n", product_name, vendor_id, product_id);
return NULL;
}
printf("Found %s (%.4x:%.4x) @ %p\n", product_name, vendor_id, product_id, _usb_dev_handle);
}
if (!_usb_device) {
_usb_device = libusb_get_device(_usb_dev_handle);
if (!_usb_device) {
elog("ERROR: Failed to retrieve usb_device info @ %p\n", _usb_dev_handle);
return NULL;
}
}
// Get usb_device descriptor
if (libusb_get_device_descriptor(_usb_device, &_usb_desc) != 0) {
elog("WARNING: Failed to retrieve usb_device descriptor @ %p\n", _usb_dev_handle);
} else {
dlog(LOG_USB, "USB Device (%.4x:%.4x @ %p) Descriptor:\n", vendor_id, product_id, _usb_dev_handle);
dlog(LOG_USB, " bLength: %d\n", _usb_desc.bLength );
dlog(LOG_USB, " bDescriptorType: %d\n", _usb_desc.bDescriptorType );
dlog(LOG_USB, " bcdUSB: 0x%.4x\n", _usb_desc.bcdUSB );
dlog(LOG_USB, " bDeviceClass: %d\n", _usb_desc.bDeviceClass );
dlog(LOG_USB, " bDeviceSubClass: %d\n", _usb_desc.bDeviceSubClass );
dlog(LOG_USB, " bDeviceProtocol: %d\n", _usb_desc.bDeviceProtocol );
dlog(LOG_USB, " bMaxPacketSize0: %d\n", _usb_desc.bMaxPacketSize0 );
dlog(LOG_USB, " idVendor: 0x%.4x\n", _usb_desc.idVendor );
dlog(LOG_USB, " idProduct: 0x%.4x\n", _usb_desc.idProduct );
dlog(LOG_USB, " bcdDevice: 0x%.4x\n", _usb_desc.bcdDevice );
dlog(LOG_USB, " iManufacturer: %d\n", _usb_desc.iManufacturer );
dlog(LOG_USB, " iProduct: %d\n", _usb_desc.iProduct );
dlog(LOG_USB, " iSerialNumber: %d\n", _usb_desc.iSerialNumber );
dlog(LOG_USB, " bNumConfigurations: %d\n", _usb_desc.bNumConfigurations);
}
return _usb_dev_handle;
}
static int mouse_deinit(void) {
// Finish up with mouse
if (_usb_dev_handle) libusb_close(_usb_dev_handle);
_usb_dev_handle = NULL;
_usb_device = NULL;
return 1;
}
static void display_mouse_hid(const uint16_t vendor_id, const uint16_t product_id) {
uint8_t config_index = 0;
uint8_t iface_index = 0;
if (!_usb_device) return;
if (_usb_desc.idVendor != vendor_id || _usb_desc.idProduct != product_id) {
elog("WARNING: Device descriptor doesn't match expected device\n");
return;
}
for (config_index = 0; config_index < _usb_desc.bNumConfigurations; ++config_index) {
int altsetting_index = 0;
struct libusb_config_descriptor *config = NULL;
libusb_get_config_descriptor(_usb_device, config_index, &config);
dlog(LOG_USB, "USB Device @ %p : Config %d:\n", _usb_dev_handle, config_index);
dlog(LOG_USB, " bLength: %d\n", config->bLength);
dlog(LOG_USB, " bDescriptorType: %d\n", config->bDescriptorType);
dlog(LOG_USB, " wTotalLength: %d\n", config->wTotalLength);
dlog(LOG_USB, " iConfiguration: %d\n", config->iConfiguration);
dlog(LOG_USB, " bmAttributes: %d\n", config->bmAttributes);
dlog(LOG_USB, " MaxPower: %d\n", config->MaxPower);
dlog(LOG_USB, " extra (%d): \"%s\"\n", config->extra_length, config->extra);
dlog(LOG_USB, " bNumInterfaces: %d\n", config->bNumInterfaces);
for (iface_index = 0; iface_index < config->bNumInterfaces; ++iface_index) {
const struct libusb_interface *iface = &config->interface[iface_index];
dlog(LOG_USB, " Interface: %d\n", iface_index);
dlog(LOG_USB, " num_altsetting: %d\n", iface->num_altsetting);
for (altsetting_index = 0; altsetting_index < iface->num_altsetting; ++altsetting_index) {
const struct libusb_interface_descriptor *iface_desc = &iface->altsetting[altsetting_index];
dlog(LOG_USB, " AltSetting: %d\n", altsetting_index);
dlog(LOG_USB, " Length: %d\n", iface_desc->bLength);
dlog(LOG_USB, " DescType: %d\n", iface_desc->bDescriptorType);
dlog(LOG_USB, " IFNum: %d\n", iface_desc->bInterfaceNumber);
dlog(LOG_USB, " AltSetting: %d\n", iface_desc->bAlternateSetting);
dlog(LOG_USB, " NumEndPnts: %d\n", iface_desc->bNumEndpoints);
dlog(LOG_USB, " IFClass: %d\n", iface_desc->bInterfaceClass);
dlog(LOG_USB, " IFSubClass: %d\n", iface_desc->bInterfaceSubClass);
dlog(LOG_USB, " IFProtocol: %d\n", iface_desc->bInterfaceProtocol);
dlog(LOG_USB, " Interface: %d\n", iface_desc->iInterface);
dlog(LOG_USB, " extra: (%d) \"%s\"\n", iface_desc->extra_length, iface_desc->extra);
}
altsetting_index = 0;
}
}
}
int mouse_hid_detach_kernel(int iface) {
int ret = 0;
if (!_usb_dev_handle || iface < 0) return -1;
ret = libusb_detach_kernel_driver(_usb_dev_handle, iface);
if (ret != 0) {
elog("ERROR: Failed to detach kernel driver: %s\n", libusb_strerror(ret));
return ret;
}
ret = libusb_claim_interface(_usb_dev_handle, iface);
if (ret != 0) {
elog("ERROR: Failed to claim interface: %s\n", libusb_strerror(ret));
// Reattach the kernel driver
ret = libusb_attach_kernel_driver(_usb_dev_handle, iface);
if (ret != 0) {
elog("ERROR: Failed to attach kernel driver: %s\n", libusb_strerror(ret));
}
return ret;
}
return 0;
}
int mouse_hid_attach_kernel(int iface) {
int ret = 0;
if (!_usb_dev_handle || iface < 0) return -1;
ret = libusb_release_interface(_usb_dev_handle, iface);
if (ret != 0) {
elog("ERROR: Failed to release interface: %s\n", libusb_strerror(ret));
}
ret = libusb_attach_kernel_driver(_usb_dev_handle, iface);
if (ret != 0) {
elog("ERROR: Failed to attach kernel driver: %s\n", libusb_strerror(ret));
return ret;
}
return 0;
}
static t_mode change_mode(libusb_device_handle *usb_dev_handle, t_mode mode) {
unsigned char payload[] = "\xf0\xff\x00\x00";
int ret;
if (!_mouse_primed || !usb_dev_handle || mode >= mode_COUNT) return mode_COUNT;
if (mode == mode_f3) {
// Top Mode
// S Co:2:039:0 s 21 09 03f0 0001 0004 4 = f0800000
// NOTE: b0, c0, f0 also seem to work
payload[1] = '\x80';
} else
if (mode == mode_f4) {
// (Bottom) Right Mode
// S Co:2:039:0 s 21 09 03f0 0001 0004 4 = f0900000
// NOTE: d0 also seems to work
payload[1] = '\x90';
} else
if (mode == mode_f5) {
// (Bottom) Left Mode
// S Co:2:039:0 s 21 09 03f0 0001 0004 4 = f0a00000
// NOTE: e0 also seems to work
payload[1] = '\xa0';
} else {
return mode_COUNT;
}
ret = libusb_control_transfer(
usb_dev_handle
,LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_OUT
,HID_REQ_SET_REPORT
,0x03f0
,0x0001
,payload
,sizeof(payload) - 1
,1000);
// This process takes time
usleep(10000);
dlog(LOG_USB, " --> %d\n", ret);
return mode;
}
// Expected length: 35
static int mode_load(unsigned char *mode_data, libusb_device_handle *usb_dev_handle, t_mode mode) {
const uint16_t exp_len = 35;
uint16_t mi;
int ret;
if (!_mouse_primed || !mode_data || !usb_dev_handle || mode >= mode_COUNT) return 0;
if (mode == mode_f3) mi = 0xf3;
else if (mode == mode_f4) mi = 0xf4;
else if (mode == mode_f5) mi = 0xf5;
else return 0;
ret = libusb_control_transfer(
usb_dev_handle
,LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_IN
,HID_REQ_GET_REPORT
,0x0300|mi
,0x0001
,mode_data
,exp_len
,1000
);
usleep(10000);
if (ret != exp_len) {
elog("ERROR: Failed to retrieve current mapping for mode 0x%.2x\n", mi);
return 0;
}
int bit;
char bitout[255]
,*po = &bitout[0];
for (bit = 0; bit < exp_len; ++bit) {
sprintf(po, "%.2x", (mode_data)[bit]);
po += strlen(po);
if ((bit+1) % 4 == 0) sprintf(po, " ");
po += strlen(po);
}
dlog(LOG_PARSE, "Mode 0x%.2x: %s\n", mi, bitout);
return exp_len;
}
static int mode_save(unsigned char *mode_data, libusb_device_handle *usb_dev_handle, const t_mode mode) {
const uint16_t exp_len = 35;
uint16_t mi;
int ret;
int bit;
char bitout[255]
,*po = &bitout[0];
unsigned char cmp[255];
if (!_mouse_primed || !mode_data || !usb_dev_handle || mode >= mode_COUNT) return 0;
if (mode == mode_f3) mi = 0xf3;
else if (mode == mode_f4) mi = 0xf4;
else if (mode == mode_f5) mi = 0xf5;
else return 0;
ret = libusb_control_transfer(
usb_dev_handle
,LIBUSB_REQUEST_TYPE_CLASS|LIBUSB_RECIPIENT_INTERFACE|LIBUSB_ENDPOINT_OUT
,HID_REQ_SET_REPORT
,0x0300|mi
,0x0001
,mode_data
,exp_len
,1000
);
usleep(500000); // Writes are SLOW
if (ret != exp_len) {
elog("ERROR: Failed to set current mapping for mode 0x%.2x\n", mi);
return 0;
}
po = &bitout[0];
for (bit = 0; bit < exp_len; ++bit) {
sprintf(po, "%.2x", (mode_data)[bit]);
po += strlen(po);
if ((bit+1) % 4 == 0) sprintf(po, " ");
po += strlen(po);
}
dlog(LOG_PARSE, "Mode 0x%.2x: %s\n", mi, bitout);
dlog(LOG_PARSE, "Comparing to stored:\n");
ret = mode_load(&cmp[0], usb_dev_handle, mode);
if (ret != exp_len) {
elog("ERROR: Failed to retrieve mapping for mode 0x%.2x\n", mi);
return 0;
}
po = &bitout[0];
for (bit = 0; bit < exp_len; ++bit) {
sprintf(po, "%.2x", (cmp)[bit]);
po += strlen(po);
if ((bit+1) % 4 == 0) sprintf(po, " ");
po += strlen(po);
}
dlog(LOG_PARSE, "Mode 0x%.2x: %s\n", mi, bitout);
if (memcmp(mode_data, cmp, exp_len) != 0) {
elog("ERROR: Mapping retrieved not equal to mapping saved for mode 0x%.2x\n", mi);
return 0;
}
return exp_len;
}
static int mode_print(unsigned char *mode_data, int len) {
unsigned char bit = 0;
unsigned char but[3] = {0,0,0};
int i = 0;
int x = 0;
char rawout[255]
,*po = &rawout[0];
for (i = 0; i < len; ++i) {
sprintf(po, "%.2x", (mode_data)[i]);
po += strlen(po);
if ((i+1) % 4 == 0) sprintf(po, " ");
po += strlen(po);
}
dlog(LOG_PARSE, "RAW: %s\n", rawout);
i = 0;
// F5040302 84060844 01000002 00000300 00040000 05000006 00000700 00080000 090000
// ^^
//printf("MODE: %s\n", s_mode[mode]);
++i;
// F5040302 84060844 01000002 00000300 00040000 05000006 00000700 00080000 090000
// ^^
printf(" Colour: %s\n", s_colour[ (mode_data)[i++] ]);
// F5040302 84060844 01000002 00000300 00040000 05000006 00000700 00080000 090000
// ^^
bit = (mode_data)[i++];
printf(" Report Rate: %4d\n",
bit < n_report_rates
? report_rate[bit]
: -1
);
// F5040302 84060844 01000002 00000300 00040000 05000006 00000700 00080000 090000
// ^^ ^^^^^^
for (x = 1; x <= 4; ++x) {
bit = (mode_data)[i++];
printf(" DPI #%d: %s %4d\n",
x
,bit & 0x80 ? "(DEF)" : " "
,dpi_point(bit & 0x0f)