ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
led_mouse.c
Go to the documentation of this file.
1 #include "led.h"
2 #include "notify.h"
3 #include "profile.h"
4 #include "usb.h"
5 
6 // Compare two light structures, ignore keys
7 static int rgbcmp(const lighting* lhs, const lighting* rhs){
8  return memcmp(lhs->r + LED_MOUSE, rhs->r + LED_MOUSE, N_MOUSE_ZONES) || memcmp(lhs->g + LED_MOUSE, rhs->g + LED_MOUSE, N_MOUSE_ZONES) || memcmp(lhs->b + LED_MOUSE, rhs->b + LED_MOUSE, N_MOUSE_ZONES);
9 }
10 
11 // Return true if all mouse zones are black
12 // Automatically returns false for all devices except M65, because they don't like this packet
13 static int isblack(const usbdevice* kb, const lighting* light){
14  if(!IS_M65(kb))
15  return 0;
16  uchar black[N_MOUSE_ZONES] = { 0 };
17  return !memcmp(light->r + LED_MOUSE, black, sizeof(black)) && !memcmp(light->g + LED_MOUSE, black, sizeof(black)) && !memcmp(light->b + LED_MOUSE, black, sizeof(black));
18 }
19 
20 int updatergb_mouse(usbdevice* kb, int force){
21  if(!kb->active)
22  return 0;
23  lighting* lastlight = &kb->profile->lastlight;
24  lighting* newlight = &kb->profile->currentmode->light;
25  // Don't do anything if the lighting hasn't changed
26  if(!force && !lastlight->forceupdate && !newlight->forceupdate
27  && !rgbcmp(lastlight, newlight))
28  return 0;
29  lastlight->forceupdate = newlight->forceupdate = 0;
30 
31  // Prevent writing to DPI LEDs or non-existent LED zones for the Glaive.
32  int num_zones = IS_GLAIVE(kb) ? 3 : N_MOUSE_ZONES;
33  // Send the RGB values for each zone to the mouse
34  uchar data_pkt[2][MSG_SIZE] = {
35  { 0x07, 0x22, num_zones, 0x01, 0 }, // RGB colors
36  { 0x07, 0x05, 0x02, 0 } // Lighting on/off
37  };
38  uchar* rgb_data = &data_pkt[0][4];
39  for(int i = 0; i < N_MOUSE_ZONES; i++){
40  if (IS_GLAIVE(kb) && i != 0 && i != 1 && i != 5)
41  continue;
42  *rgb_data++ = i + 1;
43  *rgb_data++ = newlight->r[LED_MOUSE + i];
44  *rgb_data++ = newlight->g[LED_MOUSE + i];
45  *rgb_data++ = newlight->b[LED_MOUSE + i];
46  }
47  // Send RGB data
48  if(!usbsend(kb, data_pkt[0], 1))
49  return -1;
50  int was_black = isblack(kb, lastlight), is_black = isblack(kb, newlight);
51  if(is_black){
52  // If the lighting is black, send the deactivation packet (M65 only)
53  if(!usbsend(kb, data_pkt[1], 1))
54  return -1;
55  } else if(was_black || force){
56  // If the lighting WAS black, or if we're on forced update, send the activation packet
57  data_pkt[1][4] = 1;
58  if(!usbsend(kb, data_pkt[1], 1))
59  return -1;
60  }
61 
62  memcpy(lastlight, newlight, sizeof(lighting));
63  return 0;
64 }
65 
66 int savergb_mouse(usbdevice* kb, lighting* light, int mode){
67  (void)mode;
68 
69  uchar data_pkt[MSG_SIZE] = { 0x07, 0x13, 0x10, 1, 0 };
70  // Save each RGB zone, minus the DPI light which is sent in the DPI packets
71  int zonecount = IS_SCIMITAR(kb) ? 4 : IS_SABRE(kb) ? 3 : 2;
72  for(int i = 0; i < zonecount; i++){
73  int led = LED_MOUSE + i;
74  if(led >= LED_DPI)
75  led++; // Skip DPI light
76  data_pkt[4] = light->r[led];
77  data_pkt[5] = light->g[led];
78  data_pkt[6] = light->b[led];
79  if(!usbsend(kb, data_pkt, 1))
80  return -1;
81  // Set packet for next zone
82  data_pkt[2]++;
83  }
84  return 0;
85 }
86 
87 int loadrgb_mouse(usbdevice* kb, lighting* light, int mode){
88  (void)mode;
89 
90  uchar data_pkt[MSG_SIZE] = { 0x0e, 0x13, 0x10, 1, 0 };
91  uchar in_pkt[MSG_SIZE] = { 0 };
92  // Load each RGB zone
93  int zonecount = IS_SCIMITAR(kb) ? 4 : IS_SABRE(kb) ? 3 : 2;
94  for(int i = 0; i < zonecount; i++){
95  if(!usbrecv(kb, data_pkt, in_pkt))
96  return -1;
97  if(memcmp(in_pkt, data_pkt, 4)){
98  ckb_err("Bad input header\n");
99  return -2;
100  }
101  // Copy data
102  int led = LED_MOUSE + i;
103  if(led >= LED_DPI)
104  led++; // Skip DPI light
105  light->r[led] = in_pkt[4];
106  light->g[led] = in_pkt[5];
107  light->b[led] = in_pkt[6];
108  // Set packet for next zone
109  data_pkt[2]++;
110  }
111  return 0;
112 }
#define MSG_SIZE
Definition: structures.h:176
lighting lastlight
Definition: structures.h:107
static int isblack(const usbdevice *kb, const lighting *light)
Definition: led_mouse.c:13
usbprofile * profile
Definition: structures.h:221
#define LED_MOUSE
Definition: keymap.h:39
int savergb_mouse(usbdevice *kb, lighting *light, int mode)
Definition: led_mouse.c:66
int loadrgb_mouse(usbdevice *kb, lighting *light, int mode)
Definition: led_mouse.c:87
usbmode * currentmode
Definition: structures.h:105
#define IS_SCIMITAR(kb)
Definition: usb.h:113
#define IS_M65(kb)
Definition: usb.h:97
#define ckb_err(fmt, args...)
Definition: includes.h:49
int updatergb_mouse(usbdevice *kb, int force)
Definition: led_mouse.c:20
#define N_MOUSE_ZONES
Definition: keymap.h:40
#define IS_SABRE(kb)
Definition: usb.h:107
char active
Definition: structures.h:231
lighting light
Definition: structures.h:84
#define LED_DPI
Definition: keymap.h:43
static int rgbcmp(const lighting *lhs, const lighting *rhs)
Definition: led_mouse.c:7
unsigned char uchar
Definition: includes.h:24
uchar forceupdate
Definition: structures.h:77
uchar g[152+12]
Definition: structures.h:75
#define usbrecv(kb, out_msg, in_msg)
usbrecv macro is used to wrap _usbrecv() with debugging information (file and lineno) ...
Definition: usb.h:288
Definitions for using USB interface.
uchar b[152+12]
Definition: structures.h:76
#define usbsend(kb, messages, count)
usbsend macro is used to wrap _usbsend() with debugging information (file and lineno) ...
Definition: usb.h:271
#define IS_GLAIVE(kb)
Definition: usb.h:121
uchar r[152+12]
Definition: structures.h:74