-
Notifications
You must be signed in to change notification settings - Fork 3
/
libg15.c
1415 lines (1175 loc) · 41.5 KB
/
libg15.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
/*
This file is part of g15tools.
g15tools is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
g15tools 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 libg15; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
(c) 2006-2007 The G15tools Project - g15tools.sf.net
$Revision: 324 $ - $Date: 2011-02-24 00:13:57 +0000 (Thu, 24 Feb 2011) $ $Author: SteelSide $
*/
#include <pthread.h>
#include "libg15.h"
#include <stdio.h>
#include <stdarg.h>
#include <usb.h>
#include <string.h>
#include <errno.h>
#include "config.h"
static usb_dev_handle *keyboard_device = 0;
static int libg15_debugging_enabled = 0;
static int enospc_slowdown = 0;
static int found_devicetype = -1;
static int shared_device = 0;
static int g15_keys_endpoint = 0;
static int g15_lcd_endpoint = 0;
static pthread_mutex_t libusb_mutex;
static int light_state = 0;
static int joystick_x = 0;
static int joystick_y = 0;
static int last_pressed_keys = -1;
/* to add a new device, simply create a new DEVICE() in this list */
/* Fields are: "Name",VendorID,ProductID,Capabilities */
const libg15_devices_t g15_devices[] = {
DEVICE("Logitech G15",0x46d,0xc222,G15_LCD|G15_KEYS),
DEVICE("Logitech G11",0x46d,0xc225,G15_KEYS),
DEVICE("Logitech Z-10",0x46d,0x0a07,G15_LCD|G15_KEYS|G15_DEVICE_IS_SHARED),
DEVICE("Logitech G15 v2",0x46d,0xc227,G15_LCD|G15_KEYS|G15_DEVICE_5BYTE_RETURN),
DEVICE("Logitech Gamepanel",0x46d,0xc251,G15_LCD|G15_KEYS|G15_DEVICE_IS_SHARED),
DEVICE("Logitech G13",0x46d,0xc21c,G15_LCD|G15_KEYS|G15_DEVICE_G13|G15_DEVICE_COLOUR|G15_STORAGE),
DEVICE("Logitech G110",0x46d,0xc22b,G15_KEYS|G15_DEVICE_G110|G15_DEVICE_COLOUR),
DEVICE("Logitech G510",0x46d,0xc22d,G15_LCD|G15_KEYS|G15_DEVICE_IS_SHARED|G15_DEVICE_G510|G15_DEVICE_COLOUR), /* without audio activated */
DEVICE("Logitech G510",0x46d,0xc22e,G15_LCD|G15_KEYS|G15_DEVICE_IS_SHARED|G15_DEVICE_G510|G15_DEVICE_COLOUR), /* with audio activated */
DEVICE(NULL,0,0,0)
};
/* return device capabilities */
int g15DeviceCapabilities() {
if(found_devicetype>-1)
return g15_devices[found_devicetype].caps;
else
return -1;
}
/* get the current state of the backlight */
int getBacklightState() {
return light_state;
}
/* get the current joystick X position */
int getJoystickX() {
return joystick_x;
}
/* get the current joystick X position */
int getJoystickY() {
return joystick_y;
}
/* enable or disable debugging */
void libg15Debug(int option) {
libg15_debugging_enabled = option;
usb_set_debug(option);
}
/* debugging wrapper */
static int g15_log (FILE *fd, unsigned int level, const char *fmt, ...) {
if (libg15_debugging_enabled && libg15_debugging_enabled>=level) {
fprintf(fd,"libg15: ");
va_list argp;
va_start (argp, fmt);
vfprintf(fd,fmt,argp);
va_end (argp);
}
return 0;
}
/* return number of connected and supported devices */
int g15NumberOfConnectedDevices() {
struct usb_bus *bus = 0;
struct usb_device *dev = 0;
int i=0;
unsigned int found = 0;
for (i=0; g15_devices[i].name !=NULL;i++)
for (bus = usb_busses; bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
if ((dev->descriptor.idVendor == g15_devices[i].vendorid && dev->descriptor.idProduct == g15_devices[i].productid))
found++;
}
}
g15_log(stderr,G15_LOG_INFO,"Found %i supported devices\n",found);
return found;
}
static int initLibUsb()
{
g15_log(stderr,G15_LOG_INFO,"Initialising USB\n");
usb_init();
/**
* usb_find_busses and usb_find_devices both report the number of devices
* / busses added / removed since the last call. since this is the first
* call we have to return values != 0 or else we didnt find anything */
if (!usb_find_busses())
return G15_ERROR_OPENING_USB_DEVICE;
if (!usb_find_devices())
return G15_ERROR_OPENING_USB_DEVICE;
return G15_NO_ERROR;
}
static usb_dev_handle * findAndOpenDevice(libg15_devices_t handled_device, int device_index)
{
struct usb_bus *bus = 0;
struct usb_device *dev = 0;
int retries=0;
int j,i,k,l;
int interface=0;
for (bus = usb_busses; bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
if ((dev->descriptor.idVendor == handled_device.vendorid && dev->descriptor.idProduct == handled_device.productid))
{
int ret=0;
char name_buffer[65535];
name_buffer[0] = 0;
usb_dev_handle *devh = 0;
found_devicetype = device_index;
g15_log(stderr,G15_LOG_INFO,"Found %s, trying to open it\n",handled_device.name);
#if 0
devh = usb_open(dev);
usb_reset(devh);
usleep(50*1000);
usb_close(devh);
#endif
devh = usb_open(dev);
if (!devh)
{
g15_log(stderr,G15_LOG_INFO, "Error, could not open the keyboard\n");
g15_log(stderr,G15_LOG_INFO, "Perhaps you dont have enough permissions to access it\n");
return 0;
}
usleep(50*1000);
g15_log(stderr, G15_LOG_INFO, "Device has %i possible configurations\n",dev->descriptor.bNumConfigurations);
/* if device is shared with another driver, such as the Z-10 speakers sharing with alsa, we have to disable some calls */
if(g15DeviceCapabilities() & G15_DEVICE_IS_SHARED)
shared_device = 1;
for (j = 0; j<dev->descriptor.bNumConfigurations;j++){
struct usb_config_descriptor *cfg = &dev->config[j];
for (i=0;i<cfg->bNumInterfaces; i++){
if (g15DeviceCapabilities()&G15_DEVICE_G510){
if (i==G510_STANDARD_KEYBOARD_INTERFACE) continue;
}
struct usb_interface *ifp = &cfg->interface[i];
/* if endpoints are already known, finish up */
if(g15_keys_endpoint && g15_lcd_endpoint)
break;
g15_log(stderr, G15_LOG_INFO, "Device has %i Alternate Settings\n", ifp->num_altsetting);
for(k=0;k<ifp->num_altsetting;k++){
struct usb_interface_descriptor *as = &ifp->altsetting[k];
/* verify that the interface is for a HID device */
if(as->bInterfaceClass==USB_CLASS_HID){
g15_log(stderr, G15_LOG_INFO, "Interface %i has %i Endpoints\n", i, as->bNumEndpoints);
usleep(50*1000);
/* libusb functions ending in _np are not portable between OS's
* Non-linux users will need some way to detach the HID driver from
* the G15 until we work out how to do this for other OS's automatically.
* For the moment, we just skip this code..
*/
#ifdef LIBUSB_HAS_GET_DRIVER_NP
ret = usb_get_driver_np(devh, i, name_buffer, 65535);
/* some kernel versions say that a driver is attached even though there is none
in this case the name buffer has not been changed
thanks to RobEngle for pointing this out */
if (!ret && name_buffer[0])
{
g15_log(stderr,G15_LOG_INFO,"Trying to detach driver currently attached: \"%s\"\n",name_buffer);
ret = usb_detach_kernel_driver_np(devh, i);
if (!ret)
{
g15_log(stderr,G15_LOG_INFO,"Success, detached the driver\n");
}
else
{
g15_log(stderr,G15_LOG_INFO,"Sorry, I could not detach the driver, giving up\n");
return 0;
}
}
#endif
/* don't set configuration if device is shared */
if(0 == shared_device) {
ret = usb_set_configuration(devh, 1);
if (ret)
{
g15_log(stderr,G15_LOG_INFO,"Error setting the configuration, this is fatal. Returned %d\n", ret);
return 0;
}
}
usleep(50*1000);
while((ret = usb_claim_interface(devh,i)) && retries <10) {
usleep(50*1000);
retries++;
g15_log(stderr,G15_LOG_INFO,"Trying to claim interface\n");
}
if (ret)
{
g15_log(stderr,G15_LOG_INFO,"Error claiming interface, good day cruel world\n");
return 0;
}
for (l=0; l< as->bNumEndpoints;l++){
struct usb_endpoint_descriptor *ep=&as->endpoint[l];
g15_log(stderr, G15_LOG_INFO, "Found %s endpoint %i with address 0x%X maxtransfersize=%i \n",
0x80&ep->bEndpointAddress?"\"Extra Keys\"":"\"LCD\"",
ep->bEndpointAddress&0x0f,ep->bEndpointAddress, ep->wMaxPacketSize
);
if(0x80 & ep->bEndpointAddress) {
g15_keys_endpoint = ep->bEndpointAddress;
} else {
g15_lcd_endpoint = ep->bEndpointAddress;
}
#if 0
usb_resetep(devh,ep->bEndpointAddress);
#endif
}
if (ret)
{
g15_log(stderr, G15_LOG_INFO, "Error setting Alternate Interface\n");
}
}
}
}
}
g15_log(stderr,G15_LOG_INFO,"Done opening the keyboard\n");
usleep(500*1000); // sleep a bit for good measure
return devh;
}
}
}
return 0;
}
static usb_dev_handle * findAndOpen(unsigned int vendorid, unsigned int productid) {
int i;
for (i=0; g15_devices[i].name != NULL ;i++){
if( ( vendorid == 0 || g15_devices[i].vendorid == vendorid ) &
( productid == 0 || g15_devices[i].productid == productid ) ) {
g15_log(stderr,G15_LOG_INFO,"Trying to find %s\n",g15_devices[i].name);
keyboard_device = findAndOpenDevice(g15_devices[i],i);
if(keyboard_device){
break;
}
else {
g15_log(stderr,G15_LOG_INFO,"%s not found\n",g15_devices[i].name);
}
}
else {
g15_log(stderr,G15_LOG_INFO,"%s skipped\n",g15_devices[i].name);
}
}
return keyboard_device;
}
static usb_dev_handle * findAndOpenG15() {
return findAndOpen(0, 0);
}
int re_initLibG15()
{
usb_init();
/**
* usb_find_busses and usb_find_devices both report the number of devices
* / busses added / removed since the last call. since this is the first
* call we have to return values != 0 or else we didnt find anything */
if (!usb_find_devices())
return G15_ERROR_OPENING_USB_DEVICE;
keyboard_device = findAndOpenG15();
if (!keyboard_device)
return G15_ERROR_OPENING_USB_DEVICE;
return G15_NO_ERROR;
}
int setupLibG15(unsigned int vendorId, unsigned int productId, unsigned int init_usb)
{
int retval = initLibUsb();
if(init_usb && retval) {
return retval;
}
else {
g15_log(stderr,G15_LOG_INFO,"Skipping libusb initialise\n");
}
usb_init();
usb_find_busses();
usb_find_devices();
g15_log(stderr,G15_LOG_INFO,"%s\n",PACKAGE_STRING);
#ifdef SUN_LIBUSB
g15_log(stderr,G15_LOG_INFO,"Using Sun libusb.\n");
#endif
g15NumberOfConnectedDevices();
last_pressed_keys = -1;
keyboard_device = findAndOpen(vendorId, productId);
if (!keyboard_device)
return G15_ERROR_OPENING_USB_DEVICE;
pthread_mutex_init(&libusb_mutex, NULL);
if (g15DeviceCapabilities()&G15_DEVICE_G13){
unsigned int *pk;
getPressedKeys(pk, 2000);
g15_log(stderr,G15_LOG_INFO,"Initial keypress %d\n", pk);
}
if (g15DeviceCapabilities()&G15_DEVICE_G510){
g15_log(stderr,G15_LOG_INFO,"Sending G510 initialisation.\n");
unsigned char usb_data[] = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0 };
pthread_mutex_lock(&libusb_mutex);
int retval = 0;
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x301, 1, (char*)usb_data, 19, 10000);
unsigned char usb_data_2[] = { 0x09, 0x02, 0, 0, 0, 0, 0, 0 };
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x309, 1, (char*)usb_data_2, 8, 10000);
pthread_mutex_unlock(&libusb_mutex);
}
return G15_NO_ERROR;
}
int initLibG15()
{
return setupLibG15(0, 0, 1);
}
/* reset the keyboard, returning it to a known state */
int exitLibG15()
{
int retval = G15_NO_ERROR;
g15_keys_endpoint = 0;
g15_lcd_endpoint = 0;
if (keyboard_device){
#ifndef SUN_LIBUSB
retval = usb_release_interface (keyboard_device, 0);
usleep(50*1000);
#endif
#if 0
retval = usb_reset(keyboard_device);
usleep(50*1000);
#endif
usb_close(keyboard_device);
keyboard_device=0;
pthread_mutex_destroy(&libusb_mutex);
return retval;
}
return -1;
}
static void dumpPixmapIntoLCDFormat(unsigned char *lcd_buffer, unsigned char const *data)
{
/*
For a set of bytes (A, B, C, etc.) the bits representing pixels will appear on the LCD like this:
A0 B0 C0
A1 B1 C1
A2 B2 C2
A3 B3 C3 ... and across for G15_LCD_WIDTH bytes
A4 B4 C4
A5 B5 C5
A6 B6 C6
A7 B7 C7
A0
A1 <- second 8-pixel-high row starts straight after the last byte on
A2 the previous row
A3
A4
A5
A6
A7
A8
A0
...
A0
...
A0
...
A0
A1 <- only the first three bits are shown on the bottom row (the last three
A2 pixels of the 43-pixel high display.)
*/
unsigned int output_offset = G15_LCD_OFFSET;
unsigned int base_offset = 0;
unsigned int curr_row = 0;
unsigned int curr_col = 0;
/* Five 8-pixel rows + a little 3-pixel row. This formula will calculate
the minimum number of bytes required to hold a complete column. (It
basically divides by eight and rounds up the result to the nearest byte,
but at compile time.
*/
#define G15_LCD_HEIGHT_IN_BYTES ((G15_LCD_HEIGHT + ((8 - (G15_LCD_HEIGHT % 8)) % 8)) / 8)
for (curr_row = 0; curr_row < G15_LCD_HEIGHT_IN_BYTES; ++curr_row)
{
for (curr_col = 0; curr_col < G15_LCD_WIDTH; ++curr_col)
{
unsigned int bit = curr_col % 8;
/* Copy a 1x8 column of pixels across from the source image to the LCD buffer. */
lcd_buffer[output_offset] =
(((data[base_offset ] << bit) & 0x80) >> 7) |
(((data[base_offset + G15_LCD_WIDTH/8 ] << bit) & 0x80) >> 6) |
(((data[base_offset + (G15_LCD_WIDTH/8 * 2)] << bit) & 0x80) >> 5) |
(((data[base_offset + (G15_LCD_WIDTH/8 * 3)] << bit) & 0x80) >> 4) |
(((data[base_offset + (G15_LCD_WIDTH/8 * 4)] << bit) & 0x80) >> 3) |
(((data[base_offset + (G15_LCD_WIDTH/8 * 5)] << bit) & 0x80) >> 2) |
(((data[base_offset + (G15_LCD_WIDTH/8 * 6)] << bit) & 0x80) >> 1) |
(((data[base_offset + (G15_LCD_WIDTH/8 * 7)] << bit) & 0x80) >> 0);
++output_offset;
if (bit == 7)
base_offset++;
}
/* Jump down seven pixel-rows in the source image, since we've just
done a row of eight pixels in one pass (and we counted one pixel-row
while we were going, so now we skip the next seven pixel-rows.) */
base_offset += G15_LCD_WIDTH - (G15_LCD_WIDTH / 8);
}
}
int handle_usb_errors(const char *prefix, int ret) {
switch (ret){
case -ETIMEDOUT:
return G15_ERROR_READING_USB_DEVICE; /* backward-compatibility */
break;
case -ENOSPC: /* the we dont have enough bandwidth, apparently.. something has to give here.. */
g15_log(stderr,G15_LOG_INFO,"usb error: ENOSPC.. reducing speed\n");
enospc_slowdown = 1;
break;
case -ENODEV: /* the device went away - we probably should attempt to reattach */
case -ENXIO: /* host controller bug */
case -EINVAL: /* invalid request */
case -EAGAIN: /* try again */
case -EFBIG: /* too many frames to handle */
case -EMSGSIZE: /* msgsize is invalid */
g15_log(stderr,G15_LOG_INFO,"usb error: %s %s (%i)\n",prefix,usb_strerror(),ret);
break;
case -EPIPE: /* endpoint is stalled */
g15_log(stderr,G15_LOG_INFO,"usb error: %s EPIPE! clearing...\n",prefix);
pthread_mutex_lock(&libusb_mutex);
usb_clear_halt(keyboard_device, 0x81);
pthread_mutex_unlock(&libusb_mutex);
break;
default: /* timed out */
g15_log(stderr,G15_LOG_INFO,"Unknown usb error: %s !! (err is %i (%s))\n",prefix,ret,usb_strerror());
break;
}
return ret;
}
int writePixmapToLCD(unsigned char const *data)
{
int ret = 0;
int transfercount=0;
unsigned char lcd_buffer[G15_BUFFER_LEN];
/* The pixmap conversion function will overwrite everything after G15_LCD_OFFSET, so we only need to blank
the buffer up to this point. (Even though the keyboard only cares about bytes 0-23.) */
memset(lcd_buffer, 0, G15_LCD_OFFSET); /* G15_BUFFER_LEN); */
dumpPixmapIntoLCDFormat(lcd_buffer, data);
if(!(g15_devices[found_devicetype].caps & G15_LCD))
return 0;
/* the keyboard needs this magic byte */
lcd_buffer[0] = 0x03;
/* in an attempt to reduce peak bus utilisation, we break the transfer into 32 byte chunks and sleep a bit in between.
It shouldnt make much difference, but then again, the g15 shouldnt be flooding the bus enough to cause ENOSPC, yet
apparently does on some machines...
I'm not sure how successful this will be in combatting ENOSPC, but we'll give it try in the real-world. */
if(enospc_slowdown != 0){
#ifndef LIBUSB_BLOCKS
pthread_mutex_lock(&libusb_mutex);
#endif
for(transfercount = 0;transfercount<=31;transfercount++){
ret = usb_interrupt_write(keyboard_device, g15_lcd_endpoint, (char*)lcd_buffer+(32*transfercount), 32, 1000);
if (ret != 32)
{
handle_usb_errors ("LCDPixmap Slow Write",ret);
return G15_ERROR_WRITING_PIXMAP;
}
usleep(100);
}
#ifndef LIBUSB_BLOCKS
pthread_mutex_unlock(&libusb_mutex);
#endif
}else{
/* transfer entire buffer in one hit */
#ifdef LIBUSB_BLOCKS
ret = usb_interrupt_write(keyboard_device, g15_lcd_endpoint, (char*)lcd_buffer, G15_BUFFER_LEN, 1000);
#else
pthread_mutex_lock(&libusb_mutex);
ret = usb_interrupt_write(keyboard_device, g15_lcd_endpoint, (char*)lcd_buffer, G15_BUFFER_LEN, 1000);
pthread_mutex_unlock(&libusb_mutex);
#endif
if (ret != G15_BUFFER_LEN)
{
handle_usb_errors ("LCDPixmap Write",ret);
return G15_ERROR_WRITING_PIXMAP;
}
usleep(100);
}
return 0;
}
//int readProfile(unsigned char const *data)
//{
// int retval = 0;
// unsigned char usb_data[] = { 4, 2, 0, 0, 0 };
//
// if(shared_device>0 || !( g15DeviceCapabilities() & G15_STORAGE ))
// return G15_ERROR_UNSUPPORTED;
//
// // Send request
// g15_log(stderr,G15_LOG_INFO,"Send profile data read request\n");
// pthread_mutex_lock(&libusb_mutex);
// retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x304, 0, (char*)usb_data, 4, 10000);
// pthread_mutex_unlock(&libusb_mutex);
//
// unsigned char buffer[258];
// for(int i = 0 ; i < 128; i++) {
// g15_log(stderr,G15_LOG_INFO,"Reading block %i\n", i);
// int realNumBytes = usb_control_msg(
// keyboard_device, // handle obtained with usb_open()
// USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, // bRequestType
// 1, // bRequest
// 0x0306, // wValue
// 0, // wIndex
// buffer, // pointer to destination buffer
// 258, // wLength
// 10000
// );
// g15_log(stderr,G15_LOG_INFO,"Got back %i\n", realNumBytes);
// }
//
// usleep(100);
//
// return 0;
//}
int setLCDContrast(unsigned int level)
{
int retval = 0;
unsigned char usb_data[] = { 2, 32, 129, 0 };
if(shared_device>0)
return G15_ERROR_UNSUPPORTED;
switch(level)
{
case 1:
usb_data[3] = 22;
break;
case 2:
usb_data[3] = 26;
break;
default:
usb_data[3] = 18;
break;
}
pthread_mutex_lock(&libusb_mutex);
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x302, 0, (char*)usb_data, 4, 10000);
pthread_mutex_unlock(&libusb_mutex);
return retval;
}
int setLEDs(unsigned int leds)
{
int retval = 0;
pthread_mutex_lock(&libusb_mutex);
if (g15DeviceCapabilities()&G15_DEVICE_G13){
unsigned char m_led_buf[5] = { 5, (unsigned char)leds, 0, 0, 0 };
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x305, 0, (char*)m_led_buf, 5, 10000);
}
else if (g15DeviceCapabilities()&G15_DEVICE_G110){
unsigned char m_led_buf[2] = { 3, (unsigned char)leds };
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x303, 1, (char*)m_led_buf, 2, 10000);
}
else if (g15DeviceCapabilities()&G15_DEVICE_G510){
// M-key light mask is different on this model
int new_leds = 0;
if(leds & 0x01) {
new_leds += 0x80;
}
if(leds & 0x02) {
new_leds += 0x40;
}
if(leds & 0x04) {
new_leds += 0x20;
}
if(leds & 0x08) {
new_leds += 0x10;
}
unsigned char m_led_buf[2] = { 4, (unsigned char)new_leds };
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x304, 1, (char*)m_led_buf, 2, 10000);
}
else {
if(shared_device>0) {
return G15_ERROR_UNSUPPORTED;
}
else {
unsigned char m_led_buf[4] = { 2, 4, ~(unsigned char)leds, 0 };
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x302, 0, (char*)m_led_buf, 4, 10000);
}
}
pthread_mutex_unlock(&libusb_mutex);
return retval;
}
int setLCDBrightness(unsigned int level)
{
int retval = 0;
unsigned char usb_data[] = { 2, 2, 0, 0 };
if(shared_device>0 || ( g15DeviceCapabilities() & G15_DEVICE_COLOUR ))
return G15_ERROR_UNSUPPORTED;
switch(level)
{
case 1 :
usb_data[2] = 0x10;
break;
case 2 :
usb_data[2] = 0x20;
break;
default:
usb_data[2] = 0x00;
break;
}
pthread_mutex_lock(&libusb_mutex);
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x302, 0, (char*)usb_data, 4, 10000);
pthread_mutex_unlock(&libusb_mutex);
return retval;
}
/* set the keyboard backlight. doesnt affect lcd backlight. 0==off,1==medium,2==high */
int setKBBrightness(unsigned int level)
{
int retval = 0;
unsigned char usb_data[] = { 2, 1, 0, 0 };
if(shared_device>0 || ( g15DeviceCapabilities() & G15_DEVICE_COLOUR ) )
return G15_ERROR_UNSUPPORTED;
switch(level)
{
case 1 :
usb_data[2] = 0x1;
break;
case 2 :
usb_data[2] = 0x2;
break;
default:
usb_data[2] = 0x0;
break;
}
pthread_mutex_lock(&libusb_mutex);
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x302, 0, (char*)usb_data, 4, 10000);
pthread_mutex_unlock(&libusb_mutex);
return retval;
}
int setG510LEDColor(unsigned char r, unsigned char g, unsigned char b)
{
if((!g15DeviceCapabilities() & G15_DEVICE_COLOUR))
return G15_ERROR_UNSUPPORTED;
int retval = 0;
unsigned char usb_data[] = { 4, 0, 0, 0, 0 };
usb_data[1] = r;
usb_data[2] = g;
usb_data[3] = b;
pthread_mutex_lock(&libusb_mutex);
if (g15DeviceCapabilities()&G15_DEVICE_G110){
usb_data[0] = 7;
// If the intensities are the same, "colour" is 0x80
if ( r == b ) {
usb_data[1] = 0x80;
usb_data[4] = b>>4;
}
// If the blue value is higher
else if ( b > r ) {
usb_data[1] = 0xff - ( 0x80 * r ) / b;
usb_data[4] = b>>4;
}
// If the red value is higher
else {
usb_data[1] = ( 0x80 * b ) / r;
usb_data[4] = r>>4;
}
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x307, 0, (char*)usb_data, 5, 10000);
}
else if (g15DeviceCapabilities()&G15_DEVICE_G13){
usb_data[0] = 5;
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x307, 0, (char*)usb_data, 4, 10000);
}
else {
usb_data[0] = 5;
retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x305, 1, (char*)usb_data, 4, 10000);
}
pthread_mutex_unlock(&libusb_mutex);
return retval;
}
static unsigned char g15KeyToLogitechKeyCode(int key)
{
// first 12 G keys produce F1 - F12, thats 0x3a + key
if (key < 12)
{
return 0x3a + key;
}
// the other keys produce Key '1' (above letters) + key, thats 0x1e + key
else
{
return 0x1e + key - 12; // sigh, half an hour to find -12 ....
}
}
static void processKeyEventG13(unsigned int *pressed_keys, unsigned char *buffer)
{
int i;
*pressed_keys = 0;
if (buffer[0] == 0x01)
{
if (buffer[3]&0x01)
*pressed_keys |= G15_KEY_G1;
if (buffer[3]&0x02)
*pressed_keys |= G15_KEY_G2;
if (buffer[3]&0x04)
*pressed_keys |= G15_KEY_G3;
if (buffer[3]&0x08)
*pressed_keys |= G15_KEY_G4;
if (buffer[3]&0x10)
*pressed_keys |= G15_KEY_G5;
if (buffer[3]&0x20)
*pressed_keys |= G15_KEY_G6;
if (buffer[3]&0x40)
*pressed_keys |= G15_KEY_G7;
if (buffer[3]&0x80)
*pressed_keys |= G15_KEY_G8;
if (buffer[4]&0x01)
*pressed_keys |= G15_KEY_G9;
if (buffer[4]&0x02)
*pressed_keys |= G15_KEY_G10;
if (buffer[4]&0x04)
*pressed_keys |= G15_KEY_G11;
if (buffer[4]&0x08)
*pressed_keys |= G15_KEY_G12;
if (buffer[4]&0x10)
*pressed_keys |= G15_KEY_G13;
if (buffer[4]&0x20)
*pressed_keys |= G15_KEY_G14;
if (buffer[4]&0x40)
*pressed_keys |= G15_KEY_G15;
if (buffer[4]&0x80)
*pressed_keys |= G15_KEY_G16;
if (buffer[5]&0x01)
*pressed_keys |= G15_KEY_G17;
if (buffer[5]&0x02)
*pressed_keys |= G15_KEY_G18;
if (buffer[5]&0x80)
*pressed_keys |= G15_KEY_LIGHT;
if (buffer[6]&0x01)
*pressed_keys |= G15_KEY_L1;
if (buffer[6]&0x02)
*pressed_keys |= G15_KEY_L2;
if (buffer[6]&0x04)
*pressed_keys |= G15_KEY_L3;
if (buffer[6]&0x08)
*pressed_keys |= G15_KEY_L4;
if (buffer[6]&0x10)
*pressed_keys |= G15_KEY_L5;
if (buffer[6]&0x20)
*pressed_keys |= G15_KEY_M1;
if (buffer[6]&0x40)
*pressed_keys |= G15_KEY_M2;
if (buffer[6]&0x80)
*pressed_keys |= G15_KEY_M3;
if (buffer[7]&0x01)
*pressed_keys |= G15_KEY_MR;
}
}
static void processKeyEventG13Extended(unsigned int *pressed_keys, unsigned char *buffer)
{
int i;
*pressed_keys = 0;
if (buffer[0] == 0x01)
{
if (buffer[5]&0x04) {
*pressed_keys |= G15_KEY_G19;
*pressed_keys |= G15_EXTENDED_KEY;
}
if (buffer[5]&0x08) {
*pressed_keys |= G15_KEY_G20;
*pressed_keys |= G15_EXTENDED_KEY;
}
if (buffer[5]&0x10) {
*pressed_keys |= G15_KEY_G21;
*pressed_keys |= G15_EXTENDED_KEY;
}
if (buffer[5]&0x20) {
*pressed_keys |= G15_KEY_G22;
*pressed_keys |= G15_EXTENDED_KEY;
}
if (buffer[7]&0x02) {
*pressed_keys |= G15_KEY_JOYBL;
*pressed_keys |= G15_EXTENDED_KEY;
}
if (buffer[7]&0x04) {
*pressed_keys |= G15_KEY_JOYBD;
*pressed_keys |= G15_EXTENDED_KEY;
}
if (buffer[7]&0x08) {
*pressed_keys |= G15_KEY_JOYBS;
*pressed_keys |= G15_EXTENDED_KEY;
}
}
// Bytes 1 and 2 are the joystick positions
int jx = buffer[1];
int jy = buffer[2];
if(jx != joystick_x || jy != joystick_y) {
joystick_x = jx;
joystick_y = jy;
*pressed_keys |= G15_JOY;
*pressed_keys |= G15_EXTENDED_KEY;
}
}
static void processG510AudioKeyEvent(unsigned int *pressed_keys, unsigned char *buffer)
{
int i;
*pressed_keys = 0;
if (buffer[0] == 0x03)
{
if (buffer[4]&0x20) {
*pressed_keys |= G15_KEY_MUTE_OUTPUT;
*pressed_keys |= G15_EXTENDED_KEY;
}
if (buffer[4]&0x40) {
*pressed_keys |= G15_KEY_MUTE_INPUT;
*pressed_keys |= G15_EXTENDED_KEY;
}
}
}
static void processKeyEvent9Byte(unsigned int *pressed_keys, unsigned char *buffer)
{
int i;
*pressed_keys = 0;
g15_log(stderr,G15_LOG_INFO,"Keyboard: %x, %x, %x, %x, %x, %x, %x, %x, %x\n",buffer[0],buffer[1],buffer[2],buffer[3],buffer[4],buffer[5],buffer[6],buffer[7],buffer[8]);
if (buffer[0] == 0x02)
{
if (buffer[1]&0x01)
*pressed_keys |= G15_KEY_G1;
if (buffer[2]&0x02)
*pressed_keys |= G15_KEY_G2;
if (buffer[3]&0x04)
*pressed_keys |= G15_KEY_G3;
if (buffer[4]&0x08)
*pressed_keys |= G15_KEY_G4;
if (buffer[5]&0x10)
*pressed_keys |= G15_KEY_G5;
if (buffer[6]&0x20)
*pressed_keys |= G15_KEY_G6;
if (buffer[2]&0x01)
*pressed_keys |= G15_KEY_G7;
if (buffer[3]&0x02)
*pressed_keys |= G15_KEY_G8;
if (buffer[4]&0x04)
*pressed_keys |= G15_KEY_G9;
if (buffer[5]&0x08)
*pressed_keys |= G15_KEY_G10;
if (buffer[6]&0x10)
*pressed_keys |= G15_KEY_G11;
if (buffer[7]&0x20)
*pressed_keys |= G15_KEY_G12;
if (buffer[1]&0x04)
*pressed_keys |= G15_KEY_G13;
if (buffer[2]&0x08)
*pressed_keys |= G15_KEY_G14;
if (buffer[3]&0x10)
*pressed_keys |= G15_KEY_G15;
if (buffer[4]&0x20)
*pressed_keys |= G15_KEY_G16;
if (buffer[5]&0x40)