Advertisement
Guest User

libg15.patch

a guest
Jul 11th, 2014
2,057
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 26.92 KB | None | 0 0
  1. --- a/libg15.c  2008-11-11 13:51:26.000000000 +0000
  2. +++ b/libg15.c  2014-02-08 00:57:28.000000000 +0000
  3. @@ -16,8 +16,8 @@
  4.      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  5.  
  6.      (c) 2006-2007 The G15tools Project - g15tools.sf.net
  7. -    
  8. -    $Revision: 292 $ -  $Date: 2008-11-11 08:51:25 -0500 (Tue, 11 Nov 2008) $ $Author: aneurysm9 $
  9. +
  10. +    $Revision$ -  $Date$ $Author$
  11.  */
  12.  
  13.  #include <pthread.h>
  14. @@ -42,11 +42,8 @@
  15.  /* to add a new device, simply create a new DEVICE() in this list */
  16.  /* Fields are: "Name",VendorID,ProductID,Capabilities */
  17.  const libg15_devices_t g15_devices[] = {
  18. -    DEVICE("Logitech G15",0x46d,0xc222,G15_LCD|G15_KEYS),
  19. -    DEVICE("Logitech G11",0x46d,0xc225,G15_KEYS),
  20. -    DEVICE("Logitech Z-10",0x46d,0x0a07,G15_LCD|G15_KEYS|G15_DEVICE_IS_SHARED),
  21. -    DEVICE("Logitech G15 v2",0x46d,0xc227,G15_LCD|G15_KEYS|G15_DEVICE_5BYTE_RETURN),
  22. -    DEVICE("Logitech Gamepanel",0x46d,0xc251,G15_LCD|G15_KEYS|G15_DEVICE_IS_SHARED),
  23. +    DEVICE("Logitech G510",0x46d,0xc22d,G15_LCD|G15_KEYS|G15_DEVICE_IS_SHARED|G15_DEVICE_G510), /* without audio activated */
  24. +    DEVICE("Logitech G510",0x46d,0xc22e,G15_LCD|G15_KEYS|G15_DEVICE_IS_SHARED|G15_DEVICE_G510), /* with audio activated */
  25.      DEVICE(NULL,0,0,0)
  26.  };
  27.  
  28. @@ -88,11 +85,11 @@
  29.      unsigned int found = 0;
  30.  
  31.      for (i=0; g15_devices[i].name !=NULL;i++)
  32. -        for (bus = usb_busses; bus; bus = bus->next)
  33. +        for (bus = usb_busses; bus; bus = bus->next)
  34.      {
  35.          for (dev = bus->devices; dev; dev = dev->next)
  36.          {
  37. -            if ((dev->descriptor.idVendor == g15_devices[i].vendorid && dev->descriptor.idProduct == g15_devices[i].productid))
  38. +            if ((dev->descriptor.idVendor == g15_devices[i].vendorid && dev->descriptor.idProduct == g15_devices[i].productid))
  39.                  found++;
  40.          }
  41.      }
  42. @@ -108,7 +105,7 @@
  43.       *  usb_find_busses and usb_find_devices both report the number of devices
  44.       *  / busses added / removed since the last call. since this is the first
  45.     *  call we have to return values != 0 or else we didnt find anything */
  46. -    
  47. +
  48.      if (!usb_find_busses())
  49.          return G15_ERROR_OPENING_USB_DEVICE;
  50.  
  51. @@ -117,20 +114,40 @@
  52.  
  53.      return G15_NO_ERROR;
  54.  }
  55. +/*Extra code 1*/
  56. +int initG510() {
  57. +    unsigned char cmd_buf_1[3] = {7, 3, 0};
  58. +    unsigned char cmd_buf_2[19] = {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  59. +    int ret = 0;
  60.  
  61. +    pthread_mutex_lock(&libusb_mutex);
  62. +    ret = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x301, 1, (char*)cmd_buf_2, 19, 10000);
  63. +
  64. +    ret = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x307, 1, (char*)cmd_buf_1, 3, 10000);
  65. +    pthread_mutex_unlock(&libusb_mutex);
  66. +
  67. +/* this command is sent twice by the Logitech driver with some time distance - not 100% sure this is necessary */
  68. +    usleep(200);
  69. +
  70. +    pthread_mutex_lock(&libusb_mutex);
  71. +    ret = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x307, 1, (char*)cmd_buf_1, 3, 10000);
  72. +    pthread_mutex_unlock(&libusb_mutex);
  73. +
  74. +    return 0;
  75. +}
  76.  static usb_dev_handle * findAndOpenDevice(libg15_devices_t handled_device, int device_index)
  77.  {
  78.      struct usb_bus *bus = 0;
  79.      struct usb_device *dev = 0;
  80.      int retries=0;
  81. -    int j,i,k,l;
  82. +    int j,i,k,l,m;
  83.      int interface=0;
  84. -  
  85. -    for (bus = usb_busses; bus; bus = bus->next)
  86. +
  87. +    for (bus = usb_busses; bus; bus = bus->next)
  88.      {
  89.          for (dev = bus->devices; dev; dev = dev->next)
  90.          {
  91. -            if ((dev->descriptor.idVendor == handled_device.vendorid && dev->descriptor.idProduct == handled_device.productid))
  92. +            if ((dev->descriptor.idVendor == handled_device.vendorid && dev->descriptor.idProduct == handled_device.productid))
  93.              {
  94.                  int ret=0;
  95.                  char name_buffer[65535];
  96. @@ -164,6 +181,10 @@
  97.                      struct usb_config_descriptor *cfg = &dev->config[j];
  98.  
  99.                      for (i=0;i<cfg->bNumInterfaces; i++){
  100. +                        if (g15DeviceCapabilities()&G15_DEVICE_G510){
  101. +                            if (i==G510_STANDARD_KEYBOARD_INTERFACE) continue;
  102. +                        }
  103. +
  104.                          struct usb_interface *ifp = &cfg->interface[i];
  105.                          /* if endpoints are already known, finish up */
  106.                          if(g15_keys_endpoint && g15_lcd_endpoint)
  107. @@ -176,9 +197,9 @@
  108.                              if(as->bInterfaceClass==USB_CLASS_HID){
  109.                                  g15_log(stderr, G15_LOG_INFO, "Interface %i has %i Endpoints\n", i, as->bNumEndpoints);
  110.                                  usleep(50*1000);
  111. -                   /* libusb functions ending in _np are not portable between OS's
  112. +                   /* libusb functions ending in _np are not portable between OS's
  113.                                  * Non-linux users will need some way to detach the HID driver from
  114. -                                * the G15 until we work out how to do this for other OS's automatically.
  115. +                                * the G15 until we work out how to do this for other OS's automatically.
  116.                                  * For the moment, we just skip this code..
  117.                                  */
  118.  #ifdef LIBUSB_HAS_GET_DRIVER_NP
  119. @@ -202,7 +223,7 @@
  120.                                      }
  121.  
  122.                                  }
  123. -#endif  
  124. +#endif
  125.                                  /* don't set configuration if device is shared */
  126.                                  if(0 == shared_device) {
  127.                                    ret = usb_set_configuration(devh, 1);
  128. @@ -235,7 +256,7 @@
  129.                                      if(0x80 & ep->bEndpointAddress) {
  130.                                          g15_keys_endpoint = ep->bEndpointAddress;
  131.                                      } else {
  132. -                                        g15_lcd_endpoint = ep->bEndpointAddress;
  133. +                                        g15_lcd_endpoint = ep->bEndpointAddress;
  134.                                      }
  135.  #if 0
  136.                                      usb_resetep(devh,ep->bEndpointAddress);
  137. @@ -253,29 +274,32 @@
  138.  
  139.  
  140.                  g15_log(stderr,G15_LOG_INFO,"Done opening the keyboard\n");
  141. -                usleep(500*1000); // sleep a bit for good measure
  142. +                usleep(500*1000); // sleep a bit for good measure
  143.                  return devh;
  144.              }
  145. -        }  
  146. +        }
  147.      }
  148.      return 0;
  149.  }
  150.  
  151. -
  152. +/*Edited Code 1*/
  153.  static usb_dev_handle * findAndOpenG15() {
  154.      int i;
  155. -    for (i=0; g15_devices[i].name !=NULL  ;i++){
  156. -        g15_log(stderr,G15_LOG_INFO,"Trying to find %s\n",g15_devices[i].name);
  157. -        if(keyboard_device = findAndOpenDevice(g15_devices[i],i)){
  158. -            break;
  159. -        }
  160. -        else
  161. -            g15_log(stderr,G15_LOG_INFO,"%s not found\n",g15_devices[i].name);
  162. +    for (i=0; g15_devices[i].name !=NULL ;i++){
  163. +   g15_log(stderr,G15_LOG_INFO,"Trying to find %s\n",g15_devices[i].name);
  164. +   if(keyboard_device = findAndOpenDevice(g15_devices[i],i)){
  165. +       if (g15DeviceCapabilities() & G15_DEVICE_G510) {
  166. +       g15_log(stderr, G15_LOG_INFO, "Initializing G510\n");
  167. +       initG510();
  168. +       }
  169. +       break;
  170. +   }
  171. +    else
  172. +   g15_log(stderr,G15_LOG_INFO,"%s not found\n",g15_devices[i].name);
  173.      }
  174.      return keyboard_device;
  175.  }
  176.  
  177. -
  178.  int re_initLibG15()
  179.  {
  180.  
  181. @@ -285,14 +309,14 @@
  182.       *  usb_find_busses and usb_find_devices both report the number of devices
  183.       *  / busses added / removed since the last call. since this is the first
  184.     *  call we have to return values != 0 or else we didnt find anything */
  185. -    
  186. +
  187.      if (!usb_find_devices())
  188.          return G15_ERROR_OPENING_USB_DEVICE;
  189. -  
  190. +
  191.      keyboard_device = findAndOpenG15();
  192.      if (!keyboard_device)
  193.          return G15_ERROR_OPENING_USB_DEVICE;
  194. -
  195. +
  196.      return G15_NO_ERROR;
  197.  }
  198.  
  199. @@ -304,18 +328,18 @@
  200.          return retval;
  201.  
  202.      g15_log(stderr,G15_LOG_INFO,"%s\n",PACKAGE_STRING);
  203. -    
  204. +
  205.  #ifdef SUN_LIBUSB
  206.      g15_log(stderr,G15_LOG_INFO,"Using Sun libusb.\n");
  207.  #endif
  208.  
  209.      g15NumberOfConnectedDevices();
  210. -  
  211. +
  212.      keyboard_device = findAndOpenG15();
  213.      if (!keyboard_device)
  214.          return G15_ERROR_OPENING_USB_DEVICE;
  215.  
  216. -    pthread_mutex_init(&libusb_mutex, NULL);
  217. +    pthread_mutex_init(&libusb_mutex, NULL);
  218.      return retval;
  219.  }
  220.  
  221. @@ -346,7 +370,7 @@
  222.  /*
  223.  
  224.    For a set of bytes (A, B, C, etc.) the bits representing pixels will appear on the LCD like this:
  225. -  
  226. +
  227.     A0 B0 C0
  228.     A1 B1 C1
  229.     A2 B2 C2
  230. @@ -355,7 +379,7 @@
  231.     A5 B5 C5
  232.     A6 B6 C6
  233.     A7 B7 C7
  234. -  
  235. +
  236.     A0
  237.     A1  <- second 8-pixel-high row starts straight after the last byte on
  238.     A2     the previous row
  239. @@ -375,7 +399,7 @@
  240.     A0
  241.     A1 <- only the first three bits are shown on the bottom row (the last three
  242.     A2    pixels of the 43-pixel high display.)
  243. -  
  244. +
  245.  
  246.  */
  247.  
  248. @@ -398,7 +422,7 @@
  249.          {
  250.              unsigned int bit = curr_col % 8;
  251.         /* Copy a 1x8 column of pixels across from the source image to the LCD buffer. */
  252. -      
  253. +
  254.              lcd_buffer[output_offset] =
  255.             (((data[base_offset                        ] << bit) & 0x80) >> 7) |
  256.             (((data[base_offset +  G15_LCD_WIDTH/8     ] << bit) & 0x80) >> 6) |
  257. @@ -435,10 +459,10 @@
  258.              case -EAGAIN: /* try again */
  259.              case -EFBIG: /* too many frames to handle */
  260.              case -EMSGSIZE: /* msgsize is invalid */
  261. -                 g15_log(stderr,G15_LOG_INFO,"usb error: %s %s (%i)\n",prefix,usb_strerror(),ret);    
  262. +                 g15_log(stderr,G15_LOG_INFO,"usb error: %s %s (%i)\n",prefix,usb_strerror(),ret);
  263.                   break;
  264.              case -EPIPE: /* endpoint is stalled */
  265. -                 g15_log(stderr,G15_LOG_INFO,"usb error: %s EPIPE! clearing...\n",prefix);    
  266. +                 g15_log(stderr,G15_LOG_INFO,"usb error: %s EPIPE! clearing...\n",prefix);
  267.                   pthread_mutex_lock(&libusb_mutex);
  268.                   usb_clear_halt(keyboard_device, 0x81);
  269.                   pthread_mutex_unlock(&libusb_mutex);
  270. @@ -462,17 +486,17 @@
  271.  
  272.      if(!(g15_devices[found_devicetype].caps & G15_LCD))
  273.          return 0;
  274. -  
  275. +
  276.      /* the keyboard needs this magic byte */
  277.      lcd_buffer[0] = 0x03;
  278.    /* in an attempt to reduce peak bus utilisation, we break the transfer into 32 byte chunks and sleep a bit in between.
  279. -    It shouldnt make much difference, but then again, the g15 shouldnt be flooding the bus enough to cause ENOSPC, yet
  280. +    It shouldnt make much difference, but then again, the g15 shouldnt be flooding the bus enough to cause ENOSPC, yet
  281.      apparently does on some machines...
  282.      I'm not sure how successful this will be in combatting ENOSPC, but we'll give it try in the real-world. */
  283.  
  284.      if(enospc_slowdown != 0){
  285.  #ifndef LIBUSB_BLOCKS
  286. -        pthread_mutex_lock(&libusb_mutex);  
  287. +        pthread_mutex_lock(&libusb_mutex);
  288.  #endif
  289.          for(transfercount = 0;transfercount<=31;transfercount++){
  290.              ret = usb_interrupt_write(keyboard_device, g15_lcd_endpoint, (char*)lcd_buffer+(32*transfercount), 32, 1000);
  291. @@ -485,7 +509,7 @@
  292.          }
  293.  #ifndef LIBUSB_BLOCKS
  294.          pthread_mutex_unlock(&libusb_mutex);
  295. -#endif        
  296. +#endif
  297.      }else{
  298.          /* transfer entire buffer in one hit */
  299.  #ifdef LIBUSB_BLOCKS
  300. @@ -510,21 +534,21 @@
  301.  {
  302.      int retval = 0;
  303.      unsigned char usb_data[] = { 2, 32, 129, 0 };
  304. -  
  305. +
  306.      if(shared_device>0)
  307.          return G15_ERROR_UNSUPPORTED;
  308. -  
  309. -    switch(level)
  310. +
  311. +    switch(level)
  312.      {
  313. -        case 1:
  314. -            usb_data[3] = 22;
  315. +        case 1:
  316. +            usb_data[3] = 22;
  317.              break;
  318. -        case 2:
  319. +        case 2:
  320.              usb_data[3] = 26;
  321.              break;
  322.          default:
  323.              usb_data[3] = 18;
  324. -    }  
  325. +    }
  326.      pthread_mutex_lock(&libusb_mutex);
  327.      retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x302, 0, (char*)usb_data, 4, 10000);
  328.      pthread_mutex_unlock(&libusb_mutex);
  329. @@ -535,38 +559,67 @@
  330.  {
  331.      int retval = 0;
  332.      unsigned char m_led_buf[4] = { 2, 4, 0, 0 };
  333. +    unsigned char g510_led_buf[2] = {4, 0};
  334.      m_led_buf[2] = ~(unsigned char)leds;
  335. -  
  336. +
  337. +    if(g15DeviceCapabilities() & G15_DEVICE_G510) {
  338. +//Set keyboard color based on M led state.
  339. +   switch (leds & 0x07) {
  340. +       case 1:
  341. +       setG510LEDColor(0, 0, 220);
  342. +       break;
  343. +       case 2:
  344. +       setG510LEDColor(220, 0, 0);
  345. +       break;
  346. +       case 4:
  347. +       setG510LEDColor(0, 220, 0);
  348. +       break;
  349. +       default:
  350. +       setG510LEDColor(0, 0, 220);
  351. +       break;
  352. +   }
  353. +/* A little conversion is needed because the led bitmap for the G510 is completely different from the other keyboards.
  354. +for g510 it is: bits 0-3 = N/A; bit 4 = MR; bit 5 = M1; bit 6 = M2; bit 7 = M3; */
  355. +   g510_led_buf[1] |= (leds & 0x01) << 7;
  356. +   g510_led_buf[1] |= (leds & 0x02) << 5;
  357. +   g510_led_buf[1] |= (leds & 0x04) << 3;
  358. +   g510_led_buf[1] |= (leds & 0x08) << 1;
  359. +   pthread_mutex_lock(&libusb_mutex);
  360. +   retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x304, 1, (char*)g510_led_buf, 2, 10000);
  361. +   pthread_mutex_unlock(&libusb_mutex);
  362. +    }
  363. +
  364.      if(shared_device>0)
  365. -        return G15_ERROR_UNSUPPORTED;
  366. +   return G15_ERROR_UNSUPPORTED;
  367.  
  368. -    pthread_mutex_lock(&libusb_mutex);
  369. -    retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x302, 0, (char*)m_led_buf, 4, 10000);
  370. -    pthread_mutex_unlock(&libusb_mutex);
  371. -    return retval;
  372. +   pthread_mutex_lock(&libusb_mutex);
  373. +   retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x302, 0, (char*)m_led_buf, 4, 10000);
  374. +   pthread_mutex_unlock(&libusb_mutex);
  375. +
  376. +   return retval;
  377.  }
  378.  
  379.  int setLCDBrightness(unsigned int level)
  380.  {
  381.      int retval = 0;
  382.      unsigned char usb_data[] = { 2, 2, 0, 0 };
  383. -  
  384. +
  385.      if(shared_device>0)
  386.          return G15_ERROR_UNSUPPORTED;
  387. -  
  388. -    switch(level)
  389. +
  390. +    switch(level)
  391.      {
  392. -        case 1 :
  393. -            usb_data[2] = 0x10;
  394. +        case 1 :
  395. +            usb_data[2] = 0x10;
  396.              break;
  397. -        case 2 :
  398. -            usb_data[2] = 0x20;
  399. +        case 2 :
  400. +            usb_data[2] = 0x20;
  401.              break;
  402.          default:
  403.              usb_data[2] = 0x00;
  404.      }
  405.      pthread_mutex_lock(&libusb_mutex);
  406. -    retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x302, 0, (char*)usb_data, 4, 10000);
  407. +    retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x302, 0, (char*)usb_data, 4, 10000);
  408.      pthread_mutex_unlock(&libusb_mutex);
  409.      return retval;
  410.  }
  411. @@ -576,23 +629,38 @@
  412.  {
  413.      int retval = 0;
  414.      unsigned char usb_data[] = { 2, 1, 0, 0 };
  415. -  
  416. +
  417.      if(shared_device>0)
  418.          return G15_ERROR_UNSUPPORTED;
  419.  
  420. -    switch(level)
  421. +    switch(level)
  422.      {
  423. -        case 1 :
  424. -            usb_data[2] = 0x1;
  425. +        case 1 :
  426. +            usb_data[2] = 0x1;
  427.              break;
  428. -        case 2 :
  429. -            usb_data[2] = 0x2;
  430. +        case 2 :
  431. +            usb_data[2] = 0x2;
  432.              break;
  433.          default:
  434.              usb_data[2] = 0x0;
  435.      }
  436.      pthread_mutex_lock(&libusb_mutex);
  437. -    retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x302, 0, (char*)usb_data, 4, 10000);
  438. +    retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x302, 0, (char*)usb_data, 4, 10000);
  439. +    pthread_mutex_unlock(&libusb_mutex);
  440. +    return retval;
  441. +}
  442. +
  443. +int setG510LEDColor(unsigned char r, unsigned char g, unsigned char b)
  444. +{
  445. +    int retval = 0;
  446. +    unsigned char usb_data[] = { 4, 0, 0, 0 };
  447. +
  448. +    usb_data[1] = r;
  449. +    usb_data[2] = g;
  450. +    usb_data[3] = b;
  451. +
  452. +    pthread_mutex_lock(&libusb_mutex);
  453. +    retval = usb_control_msg(keyboard_device, USB_TYPE_CLASS + USB_RECIP_INTERFACE, 9, 0x305, 1, (char*)usb_data, 4, 10000);
  454.      pthread_mutex_unlock(&libusb_mutex);
  455.      return retval;
  456.  }
  457. @@ -614,68 +682,68 @@
  458.  static void processKeyEvent9Byte(unsigned int *pressed_keys, unsigned char *buffer)
  459.  {
  460.      int i;
  461. -  
  462. +
  463.      *pressed_keys = 0;
  464. -  
  465. +
  466.      g15_log(stderr,G15_LOG_WARN,"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]);
  467. -  
  468. +
  469.      if (buffer[0] == 0x02)
  470.      {
  471.          if (buffer[1]&0x01)
  472.              *pressed_keys |= G15_KEY_G1;
  473. -    
  474. +
  475.          if (buffer[2]&0x02)
  476.              *pressed_keys |= G15_KEY_G2;
  477.  
  478.          if (buffer[3]&0x04)
  479.              *pressed_keys |= G15_KEY_G3;
  480. -    
  481. +
  482.          if (buffer[4]&0x08)
  483.              *pressed_keys |= G15_KEY_G4;
  484. -    
  485. +
  486.          if (buffer[5]&0x10)
  487.              *pressed_keys |= G15_KEY_G5;
  488.  
  489.          if (buffer[6]&0x20)
  490.              *pressed_keys |= G15_KEY_G6;
  491.  
  492. -    
  493. +
  494.          if (buffer[2]&0x01)
  495.              *pressed_keys |= G15_KEY_G7;
  496. -    
  497. +
  498.          if (buffer[3]&0x02)
  499.              *pressed_keys |= G15_KEY_G8;
  500. -    
  501. +
  502.          if (buffer[4]&0x04)
  503.              *pressed_keys |= G15_KEY_G9;
  504. -    
  505. +
  506.          if (buffer[5]&0x08)
  507.              *pressed_keys |= G15_KEY_G10;
  508. -    
  509. +
  510.          if (buffer[6]&0x10)
  511.              *pressed_keys |= G15_KEY_G11;
  512. -    
  513. +
  514.          if (buffer[7]&0x20)
  515.              *pressed_keys |= G15_KEY_G12;
  516. -    
  517. +
  518.          if (buffer[1]&0x04)
  519.              *pressed_keys |= G15_KEY_G13;
  520. -    
  521. +
  522.          if (buffer[2]&0x08)
  523.              *pressed_keys |= G15_KEY_G14;
  524. -    
  525. +
  526.          if (buffer[3]&0x10)
  527.              *pressed_keys |= G15_KEY_G15;
  528. -    
  529. +
  530.          if (buffer[4]&0x20)
  531.              *pressed_keys |= G15_KEY_G16;
  532. -    
  533. +
  534.          if (buffer[5]&0x40)
  535.              *pressed_keys |= G15_KEY_G17;
  536. -    
  537. +
  538.          if (buffer[8]&0x40)
  539.              *pressed_keys |= G15_KEY_G18;
  540. -    
  541. +
  542.          if (buffer[6]&0x01)
  543.              *pressed_keys |= G15_KEY_M1;
  544.          if (buffer[7]&0x02)
  545. @@ -705,16 +773,18 @@
  546.  static void processKeyEvent5Byte(unsigned int *pressed_keys, unsigned char *buffer)
  547.  {
  548.      int i;
  549. -  
  550. +
  551.      *pressed_keys = 0;
  552. -  
  553. +
  554.      g15_log(stderr,G15_LOG_WARN,"Keyboard: %x, %x, %x, %x, %x\n",buffer[0],buffer[1],buffer[2],buffer[3],buffer[4]);
  555. -  
  556. -    if (buffer[0] == 0x02)
  557. +
  558. +    
  559. +    // G510
  560. +    if (buffer[0] == 0x03)
  561.      {
  562.          if (buffer[1]&0x01)
  563.              *pressed_keys |= G15_KEY_G1;
  564. -    
  565. +
  566.          if (buffer[1]&0x02)
  567.              *pressed_keys |= G15_KEY_G2;
  568.  
  569. @@ -729,44 +799,214 @@
  570.  
  571.          if (buffer[1]&0x20)
  572.              *pressed_keys |= G15_KEY_G6;
  573. -            
  574. +
  575.          if (buffer[1]&0x40)
  576. -            *pressed_keys |= G15_KEY_M1;
  577. -            
  578. +            *pressed_keys |= G15_KEY_G7;
  579. +
  580.          if (buffer[1]&0x80)
  581. -            *pressed_keys |= G15_KEY_M2;
  582. -            
  583. +            *pressed_keys |= G15_KEY_G8;
  584. +
  585. +        if (buffer[2]&0x01)
  586. +            *pressed_keys |= G15_KEY_G9;
  587. +
  588. +        if (buffer[2]&0x02)
  589. +            *pressed_keys |= G15_KEY_G10;
  590. +
  591. +        if (buffer[2]&0x04)
  592. +            *pressed_keys |= G15_KEY_G11;
  593. +
  594. +        if (buffer[2]&0x08)
  595. +            *pressed_keys |= G15_KEY_G12;
  596. +
  597. +        if (buffer[2]&0x10)
  598. +            *pressed_keys |= G15_KEY_G13;
  599. +
  600.          if (buffer[2]&0x20)
  601. -            *pressed_keys |= G15_KEY_M3;
  602. -            
  603. +            *pressed_keys |= G15_KEY_G14;
  604. +
  605.          if (buffer[2]&0x40)
  606. -            *pressed_keys |= G15_KEY_MR;
  607. +            *pressed_keys |= G15_KEY_G15;
  608.  
  609.          if (buffer[2]&0x80)
  610. +            *pressed_keys |= G15_KEY_G16;
  611. +
  612. +        if (buffer[3]&0x01)
  613. +            *pressed_keys |= G15_KEY_G17;
  614. +
  615. +        if (buffer[3]&0x02)
  616. +            *pressed_keys |= G15_KEY_G18;
  617. +
  618. +        if (buffer[3]&0x10)
  619. +            *pressed_keys |= G15_KEY_M1;
  620. +
  621. +        if (buffer[3]&0x20)
  622. +            *pressed_keys |= G15_KEY_M2;
  623. +
  624. +        if (buffer[3]&0x40)
  625. +            *pressed_keys |= G15_KEY_M3;
  626. +
  627. +        if (buffer[3]&0x80)
  628. +            *pressed_keys |= G15_KEY_MR;
  629. +
  630. +        if (buffer[4]&0x1)
  631.              *pressed_keys |= G15_KEY_L1;
  632. -            
  633. -        if (buffer[2]&0x2)
  634. +
  635. +        if (buffer[4]&0x2)
  636.              *pressed_keys |= G15_KEY_L2;
  637. -            
  638. -        if (buffer[2]&0x4)
  639. +
  640. +        if (buffer[4]&0x4)
  641.              *pressed_keys |= G15_KEY_L3;
  642.  
  643. -        if (buffer[2]&0x8)
  644. +        if (buffer[4]&0x8)
  645.              *pressed_keys |= G15_KEY_L4;
  646. -            
  647. -        if (buffer[2]&0x10)
  648. +
  649. +        if (buffer[4]&0x10)
  650.              *pressed_keys |= G15_KEY_L5;
  651. -            
  652. -        if (buffer[2]&0x1)
  653. +
  654. +        if (buffer[3]&0x8)
  655.              *pressed_keys |= G15_KEY_LIGHT;
  656.      }
  657.  }
  658.  
  659. +static void processKeyEvent4Byte(unsigned int *pressed_keys, unsigned char *buffer)
  660. +{
  661. +   int i;
  662. +
  663. +   *pressed_keys = 0;
  664. +
  665. +   g15_log(stderr,G15_LOG_WARN,"Keyboard: %x, %x, %x, %x\n",buffer[0],buffer[1],buffer[2],buffer[3]);
  666. +
  667. +   if (buffer[0] == 0x02)
  668. +   {
  669. +       if (buffer[1]&0x01)
  670. +           *pressed_keys |= G15_KEY_G1;
  671. +
  672. +       if (buffer[1]&0x02)
  673. +           *pressed_keys |= G15_KEY_G2;
  674. +
  675. +       if (buffer[1]&0x04)
  676. +           *pressed_keys |= G15_KEY_G3;
  677. +
  678. +       if (buffer[1]&0x08)
  679. +           *pressed_keys |= G15_KEY_G4;
  680. +
  681. +       if (buffer[1]&0x10)
  682. +           *pressed_keys |= G15_KEY_G5;
  683. +
  684. +       if (buffer[1]&0x20)
  685. +           *pressed_keys |= G15_KEY_G6;
  686. +
  687. +       if (buffer[1]&0x40)
  688. +           *pressed_keys |= G15_KEY_G7;
  689. +
  690. +       if (buffer[1]&0x80)
  691. +           *pressed_keys |= G15_KEY_G8;
  692. +
  693. +       if (buffer[2]&0x01)
  694. +           *pressed_keys |= G15_KEY_G9;
  695. +
  696. +       if (buffer[2]&0x02)
  697. +           *pressed_keys |= G15_KEY_G10;
  698. +
  699. +       if (buffer[2]&0x04)
  700. +           *pressed_keys |= G15_KEY_G11;
  701. +
  702. +       if (buffer[2]&0x08)
  703. +           *pressed_keys |= G15_KEY_G12;
  704. +
  705. +       if (buffer[2]&0x10)
  706. +           *pressed_keys |= G15_KEY_M1;
  707. +
  708. +       if (buffer[2]&0x20)
  709. +           *pressed_keys |= G15_KEY_M2;
  710. +
  711. +       if (buffer[2]&0x40)
  712. +           *pressed_keys |= G15_KEY_M3;
  713. +
  714. +       if (buffer[2]&0x80)
  715. +           *pressed_keys |= G15_KEY_MR;
  716. +
  717. +       if (buffer[3]&0x1)
  718. +           *pressed_keys |= G15_KEY_LIGHT;
  719. +   }
  720. +}
  721. +
  722. +// Logitech G510 Media Keys implementation. Unknown if this will work for other
  723. +// models. Using the backlight key as a modifier. The assumption is you would
  724. +// normally not be holding down the backlight key while pressing G-keys.
  725. +static void processKeyEvent2Byte(unsigned int *pressed_keys, unsigned char *buffer)
  726. +{
  727. +    // Key modifier
  728. +    *pressed_keys |= G15_KEY_LIGHT;
  729. +
  730. +    // XF86AudioPlay 175
  731. +    if (*pressed_keys & G15_KEY_G1)
  732. +        *pressed_keys -= G15_KEY_G1;
  733. +
  734. +    // XF86AudioStop 176
  735. +    if (*pressed_keys & G15_KEY_G2)
  736. +        *pressed_keys -= G15_KEY_G2;
  737. +    // XF86AudioPrev 177
  738. +    else if (*pressed_keys & G15_KEY_G3)
  739. +        *pressed_keys -= G15_KEY_G3;
  740. +    // XF86AudioMute 179
  741. +    else if (*pressed_keys & G15_KEY_G5)
  742. +        *pressed_keys -= G15_KEY_G5;
  743. +    // XF86AudioRaiseVolume 180
  744. +    else if (*pressed_keys & G15_KEY_G6){
  745. +        *pressed_keys -= G15_KEY_G6;
  746. +        if (*pressed_keys & G15_KEY_G7){
  747. +        }
  748. +    }
  749. +    // XF86AudioLowerVolume 181
  750. +    else if (*pressed_keys & G15_KEY_G7)
  751. +        *pressed_keys -= G15_KEY_G7;
  752. +
  753. +
  754. +    // XF86AudioNext 178
  755. +    if (*pressed_keys & G15_KEY_G4)
  756. +        *pressed_keys -= G15_KEY_G4;
  757. +
  758. +    g15_log(stderr,G15_LOG_WARN,"Keyboard: %x, %x\n", buffer[0], buffer[1]);
  759. +
  760. +    if (buffer[0] == 0x02)
  761. +    {
  762. +        // XF86AudioPlay
  763. +        if (buffer[1] & 0x08)
  764. +            *pressed_keys |= G15_KEY_G1;
  765. +
  766. +        // XF86AudioStop
  767. +        if (buffer[1] & 0x04)
  768. +            *pressed_keys |= G15_KEY_G2;
  769. +        // XF86AudioPrev
  770. +        else if (buffer[1] & 0x02)
  771. +            *pressed_keys |= G15_KEY_G3;
  772. +        // XF86AudioMute
  773. +        else if (buffer[1] & 0x16)
  774. +            *pressed_keys |= G15_KEY_G5;
  775. +        // XF86AudioRaiseVolume
  776. +        else if (buffer[1] & 0x32){
  777. +            *pressed_keys |= G15_KEY_G6;
  778. +       if (buffer[1] & 0x64){
  779. +       }
  780. +   }
  781. +        // XF86AudioLowerVolume
  782. +        else if (buffer[1] & 0x64)
  783. +            *pressed_keys |= G15_KEY_G7;
  784. +
  785. +        // XF86AudioNext
  786. +        if (buffer[1] & 0x01)
  787. +            *pressed_keys |= G15_KEY_G4;
  788. +;
  789. +    }
  790. +}
  791.  
  792.  int getPressedKeys(unsigned int *pressed_keys, unsigned int timeout)
  793.  {
  794.      unsigned char buffer[G15_KEY_READ_LENGTH];
  795.      int ret = 0;
  796. +    int caps = 0;
  797. +
  798.  #ifdef LIBUSB_BLOCKS
  799.      ret = usb_interrupt_read(keyboard_device, g15_keys_endpoint, (char*)buffer, G15_KEY_READ_LENGTH, timeout);
  800.  #else
  801. @@ -776,16 +1016,28 @@
  802.  #endif
  803.      if(ret>0) {
  804.        if(buffer[0] == 1)
  805. -        return G15_ERROR_TRY_AGAIN;    
  806. +        return G15_ERROR_TRY_AGAIN;
  807.      }
  808.  
  809. +    caps = g15DeviceCapabilities();
  810. +
  811.      switch(ret) {
  812. +      case 4:
  813. +          processKeyEvent4Byte(pressed_keys, buffer);
  814. +          return G15_NO_ERROR;
  815.        case 5:
  816.            processKeyEvent5Byte(pressed_keys, buffer);
  817.            return G15_NO_ERROR;
  818.        case 9:
  819.            processKeyEvent9Byte(pressed_keys, buffer);
  820.            return G15_NO_ERROR;
  821. +      case 2:
  822. +          if (g15DeviceCapabilities() & G15_DEVICE_G510)
  823. +            {
  824. +              processKeyEvent2Byte(pressed_keys, buffer);
  825. +              return G15_NO_ERROR;
  826. +           }
  827. +            // Deliberate fallthrough
  828.        default:
  829.            return handle_usb_errors("Keyboard Read", ret); /* allow the app to deal with errors */
  830.      }
  831.      
  832.      
  833.      
  834.      
  835.      
  836.      
  837.      
  838.      
  839.      
  840.      
  841.      
  842. --- a/libg15.h  2008-11-11 13:51:26.000000000 +0000
  843. +++ b/libg15.h  2013-04-09 19:42:50.000000000 +0000
  844. @@ -17,7 +17,7 @@
  845.      
  846.      (c) 2006 - 2007 The G15tools Project - g15tools.sf.net
  847.      
  848. -    $Revision: 292 $ -  $Date: 2008-11-11 08:51:25 -0500 (Tue, 11 Nov 2008) $ $Author: aneurysm9 $
  849. +    $Revision$ -  $Date$ $Author$
  850.  */
  851.  
  852.  #ifndef _LIBG15_H_
  853. @@ -32,8 +32,10 @@
  854.  #define G15_KEYS 2
  855.  #define G15_DEVICE_IS_SHARED 4
  856.  #define G15_DEVICE_5BYTE_RETURN 8
  857. +#define G15_DEVICE_G510 16
  858.  
  859.  #define G15_KEY_READ_LENGTH 9
  860. +#define G510_STANDARD_KEYBOARD_INTERFACE   0x0
  861.  
  862.  typedef struct libg15_devices_t libg15_devices_t;
  863.  
  864. @@ -126,22 +128,31 @@
  865.      G15_KEY_G16 = 1<<15,
  866.      G15_KEY_G17 = 1<<16,
  867.      G15_KEY_G18 = 1<<17,
  868. -    
  869. +    G15_KEY_G19 = 1<<28,
  870. +    G15_KEY_G20 = 1<<29,
  871. +    G15_KEY_G21 = 1<<30,
  872. +    G15_KEY_G22 = 1<<31,
  873. +
  874.      G15_KEY_M1  = 1<<18,
  875.      G15_KEY_M2  = 1<<19,
  876.      G15_KEY_M3  = 1<<20,
  877.      G15_KEY_MR  = 1<<21,
  878. -    
  879. +
  880.      G15_KEY_L1  = 1<<22,
  881.      G15_KEY_L2  = 1<<23,
  882.      G15_KEY_L3  = 1<<24,
  883.      G15_KEY_L4  = 1<<25,
  884.      G15_KEY_L5  = 1<<26,
  885. -    
  886. +
  887.      G15_KEY_LIGHT = 1<<27
  888. +
  889. +    //need to add them to the enum but not enough positions left
  890. +    //  G15_KEY_JOYBL = 1<<32,
  891. +    //  G15_KEY_JOYBD = 1<<33,
  892. +    //  G15_KEY_JOYBS = 1<<34
  893.    };
  894.  
  895. -  
  896. +
  897.    /* this one return G15_NO_ERROR on success, something
  898.     * else otherwise (for instance G15_ERROR_OPENING_USB_DEVICE */
  899.    int initLibG15();
  900. @@ -157,6 +168,7 @@
  901.    int setLEDs(unsigned int leds);
  902.    int setLCDBrightness(unsigned int level);
  903.    int setKBBrightness(unsigned int level);  
  904. +  int setG510LEDColor(unsigned char r, unsigned char g, unsigned char b);  
  905.  
  906.    /* Please be warned
  907.     * the g15 sends two different usb msgs for each key press
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement