ckb-next  beta-v0.2.8 at branch testing
ckb-next driver for corsair devices
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
profile.c File Reference
#include "command.h"
#include "device.h"
#include "input.h"
#include "led.h"
#include "profile.h"
+ Include dependency graph for profile.c:

Go to the source code of this file.

Functions

void urldecode2 (char *dst, const char *src)
 
void urlencode2 (char *dst, const char *src)
 
int setid (usbid *id, const char *guid)
 
char * getid (usbid *id)
 
void u16enc (char *in, ushort *out, size_t *srclen, size_t *dstlen)
 
void u16dec (ushort *in, char *out, size_t *srclen, size_t *dstlen)
 
void cmd_name (usbdevice *kb, usbmode *mode, int dummy1, int dummy2, const char *name)
 
void cmd_profilename (usbdevice *kb, usbmode *dummy1, int dummy2, int dummy3, const char *name)
 
char * printname (ushort *name, int length)
 
char * getmodename (usbmode *mode)
 
char * getprofilename (usbprofile *profile)
 
char * gethwmodename (hwprofile *profile, int index)
 
char * gethwprofilename (hwprofile *profile)
 
void cmd_id (usbdevice *kb, usbmode *mode, int dummy1, int dummy2, const char *id)
 
void cmd_profileid (usbdevice *kb, usbmode *mode, int dummy1, int dummy2, const char *id)
 
static void initmode (usbmode *mode)
 
void allocprofile (usbdevice *kb)
 
int loadprofile (usbdevice *kb)
 
static void freemode (usbmode *mode)
 
void cmd_erase (usbdevice *kb, usbmode *mode, int dummy1, int dummy2, const char *dummy3)
 
static void _freeprofile (usbdevice *kb)
 
void cmd_eraseprofile (usbdevice *kb, usbmode *dummy1, int dummy2, int dummy3, const char *dummy4)
 
void freeprofile (usbdevice *kb)
 
void hwtonative (usbprofile *profile, hwprofile *hw, int modecount)
 
void nativetohw (usbprofile *profile, hwprofile *hw, int modecount)
 

Variables

static iconv_t utf8to16 = 0
 
static iconv_t utf16to8 = 0
 

Function Documentation

static void _freeprofile ( usbdevice kb)
static

Definition at line 210 of file profile.c.

References freemode(), usbprofile::mode, MODE_COUNT, and usbdevice::profile.

Referenced by cmd_eraseprofile(), and freeprofile().

210  {
211  usbprofile* profile = kb->profile;
212  if(!profile)
213  return;
214  // Clear all mode data
215  for(int i = 0; i < MODE_COUNT; i++)
216  freemode(profile->mode + i);
217  free(profile);
218  kb->profile = 0;
219 }
#define MODE_COUNT
Definition: structures.h:100
usbprofile * profile
Definition: structures.h:221
usbmode mode[6]
Definition: structures.h:103
static void freemode(usbmode *mode)
Definition: profile.c:198

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void allocprofile ( usbdevice kb)

Definition at line 182 of file profile.c.

References usbprofile::currentmode, dpiset::forceupdate, lighting::forceupdate, initmode(), usbprofile::lastdpi, usbprofile::lastlight, usbprofile::mode, MODE_COUNT, and usbdevice::profile.

Referenced by cmd_eraseprofile().

182  {
183  if(kb->profile)
184  return;
185  usbprofile* profile = kb->profile = calloc(1, sizeof(usbprofile));
186  for(int i = 0; i < MODE_COUNT; i++)
187  initmode(profile->mode + i);
188  profile->currentmode = profile->mode;
189  profile->lastlight.forceupdate = profile->lastdpi.forceupdate = 1;
190 }
lighting lastlight
Definition: structures.h:107
#define MODE_COUNT
Definition: structures.h:100
usbprofile * profile
Definition: structures.h:221
usbmode * currentmode
Definition: structures.h:105
usbmode mode[6]
Definition: structures.h:103
dpiset lastdpi
Definition: structures.h:108
uchar forceupdate
Definition: structures.h:77
static void initmode(usbmode *mode)
Definition: profile.c:175
uchar forceupdate
Definition: structures.h:69

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void cmd_erase ( usbdevice kb,
usbmode mode,
int  dummy1,
int  dummy2,
const char *  dummy3 
)

Definition at line 203 of file profile.c.

References freemode(), imutex, and initmode().

203  {
204  pthread_mutex_lock(imutex(kb));
205  freemode(mode);
206  initmode(mode);
207  pthread_mutex_unlock(imutex(kb));
208 }
static void freemode(usbmode *mode)
Definition: profile.c:198
#define imutex(kb)
Definition: device.h:22
static void initmode(usbmode *mode)
Definition: profile.c:175

+ Here is the call graph for this function:

void cmd_eraseprofile ( usbdevice kb,
usbmode dummy1,
int  dummy2,
int  dummy3,
const char *  dummy4 
)

Definition at line 221 of file profile.c.

References _freeprofile(), allocprofile(), and imutex.

221  {
222  pthread_mutex_lock(imutex(kb));
223  _freeprofile(kb);
224  allocprofile(kb);
225  pthread_mutex_unlock(imutex(kb));
226 }
static void _freeprofile(usbdevice *kb)
Definition: profile.c:210
void allocprofile(usbdevice *kb)
Definition: profile.c:182
#define imutex(kb)
Definition: device.h:22

+ Here is the call graph for this function:

void cmd_id ( usbdevice kb,
usbmode mode,
int  dummy1,
int  dummy2,
const char *  id 
)

Definition at line 160 of file profile.c.

References usbmode::id, usbid::modified, and setid().

160  {
161  // ID is either a GUID or an 8-digit hex number
162  int newmodified;
163  if(!setid(&mode->id, id) && sscanf(id, "%08x", &newmodified) == 1)
164  memcpy(mode->id.modified, &newmodified, sizeof(newmodified));
165 }
int setid(usbid *id, const char *guid)
Definition: profile.c:64
char modified[4]
Definition: structures.h:10
usbid id
Definition: structures.h:88

+ Here is the call graph for this function:

void cmd_name ( usbdevice kb,
usbmode mode,
int  dummy1,
int  dummy2,
const char *  name 
)

Definition at line 117 of file profile.c.

References MD_NAME_LEN, usbmode::name, u16enc(), and urldecode2().

117  {
118  char decoded[strlen(name) + 1];
119  urldecode2(decoded, name);
120  size_t srclen = strlen(decoded), dstlen = MD_NAME_LEN;
121  u16enc(decoded, mode->name, &srclen, &dstlen);
122 }
void u16enc(char *in, ushort *out, size_t *srclen, size_t *dstlen)
Definition: profile.c:97
ushort name[16]
Definition: structures.h:89
#define MD_NAME_LEN
Definition: structures.h:82
void urldecode2(char *dst, const char *src)
Definition: profile.c:8

+ Here is the call graph for this function:

void cmd_profileid ( usbdevice kb,
usbmode mode,
int  dummy1,
int  dummy2,
const char *  id 
)

Definition at line 167 of file profile.c.

References usbprofile::id, usbid::modified, usbdevice::profile, and setid().

167  {
168  usbprofile* profile = kb->profile;
169  int newmodified;
170  if(!setid(&profile->id, id) && sscanf(id, "%08x", &newmodified) == 1)
171  memcpy(profile->id.modified, &newmodified, sizeof(newmodified));
172 
173 }
usbprofile * profile
Definition: structures.h:221
int setid(usbid *id, const char *guid)
Definition: profile.c:64
char modified[4]
Definition: structures.h:10
usbid id
Definition: structures.h:111

+ Here is the call graph for this function:

void cmd_profilename ( usbdevice kb,
usbmode dummy1,
int  dummy2,
int  dummy3,
const char *  name 
)

Definition at line 124 of file profile.c.

References usbprofile::name, PR_NAME_LEN, usbdevice::profile, u16enc(), and urldecode2().

124  {
125  usbprofile* profile = kb->profile;
126  char decoded[strlen(name) + 1];
127  urldecode2(decoded, name);
128  size_t srclen = strlen(decoded), dstlen = PR_NAME_LEN;
129  u16enc(decoded, profile->name, &srclen, &dstlen);
130 }
usbprofile * profile
Definition: structures.h:221
void u16enc(char *in, ushort *out, size_t *srclen, size_t *dstlen)
Definition: profile.c:97
#define PR_NAME_LEN
Definition: structures.h:99
ushort name[16]
Definition: structures.h:110
void urldecode2(char *dst, const char *src)
Definition: profile.c:8

+ Here is the call graph for this function:

static void freemode ( usbmode mode)
static

Definition at line 198 of file profile.c.

References usbmode::bind, and freebind().

Referenced by _freeprofile(), and cmd_erase().

198  {
199  freebind(&mode->bind);
200  memset(mode, 0, sizeof(*mode));
201 }
binding bind
Definition: structures.h:85
void freebind(binding *bind)
Definition: input.c:300

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void freeprofile ( usbdevice kb)

Definition at line 228 of file profile.c.

References _freeprofile(), and usbdevice::hw.

228  {
229  _freeprofile(kb);
230  // Also free HW profile
231  free(kb->hw);
232  kb->hw = 0;
233 }
static void _freeprofile(usbdevice *kb)
Definition: profile.c:210
hwprofile * hw
Definition: structures.h:223

+ Here is the call graph for this function:

char* gethwmodename ( hwprofile profile,
int  index 
)

Definition at line 152 of file profile.c.

References MD_NAME_LEN, hwprofile::name, and printname().

Referenced by _cmd_get().

152  {
153  return printname(profile->name[index + 1], MD_NAME_LEN);
154 }
#define MD_NAME_LEN
Definition: structures.h:82
ushort name[3+1][16]
Definition: structures.h:125
char * printname(ushort *name, int length)
Definition: profile.c:132

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char* gethwprofilename ( hwprofile profile)

Definition at line 156 of file profile.c.

References MD_NAME_LEN, hwprofile::name, and printname().

Referenced by _cmd_get().

156  {
157  return printname(profile->name[0], MD_NAME_LEN);
158 }
#define MD_NAME_LEN
Definition: structures.h:82
ushort name[3+1][16]
Definition: structures.h:125
char * printname(ushort *name, int length)
Definition: profile.c:132

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char* getid ( usbid id)

Definition at line 79 of file profile.c.

References usbid::guid.

Referenced by _cmd_get().

79  {
80  int32_t data1;
81  int16_t data2, data3, data4a;
82  char data4b[6];
83  memcpy(&data1, id->guid + 0x0, 4);
84  memcpy(&data2, id->guid + 0x4, 2);
85  memcpy(&data3, id->guid + 0x6, 2);
86  memcpy(&data4a, id->guid + 0x8, 2);
87  memcpy(data4b, id->guid + 0xA, 6);
88  char* guid = malloc(39);
89  snprintf(guid, 39, "{%08X-%04hX-%04hX-%04hX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}",
90  data1, data2, data3, data4a, data4b[0], data4b[1], data4b[2], data4b[3], data4b[4], data4b[5]);
91  return guid;
92 }
char guid[16]
Definition: structures.h:9

+ Here is the caller graph for this function:

char* getmodename ( usbmode mode)

Definition at line 144 of file profile.c.

References MD_NAME_LEN, usbmode::name, and printname().

Referenced by _cmd_get().

144  {
145  return printname(mode->name, MD_NAME_LEN);
146 }
ushort name[16]
Definition: structures.h:89
#define MD_NAME_LEN
Definition: structures.h:82
char * printname(ushort *name, int length)
Definition: profile.c:132

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char* getprofilename ( usbprofile profile)

Definition at line 148 of file profile.c.

References usbprofile::name, PR_NAME_LEN, and printname().

Referenced by _cmd_get().

148  {
149  return printname(profile->name, PR_NAME_LEN);
150 }
#define PR_NAME_LEN
Definition: structures.h:99
ushort name[16]
Definition: structures.h:110
char * printname(ushort *name, int length)
Definition: profile.c:132

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void hwtonative ( usbprofile profile,
hwprofile hw,
int  modecount 
)

Definition at line 235 of file profile.c.

References usbmode::dpi, hwprofile::dpi, dpiset::forceupdate, lighting::forceupdate, usbmode::id, usbprofile::id, hwprofile::id, usbprofile::lastdpi, usbprofile::lastlight, usbmode::light, hwprofile::light, MD_NAME_LEN, usbprofile::mode, usbmode::name, usbprofile::name, hwprofile::name, and PR_NAME_LEN.

Referenced by cmd_hwload_kb(), and cmd_hwload_mouse().

235  {
236  // Copy the profile name and ID
237  memcpy(profile->name, hw->name[0], PR_NAME_LEN * 2);
238  memcpy(&profile->id, hw->id, sizeof(usbid));
239  // Copy the mode settings
240  for(int i = 0; i < modecount; i++){
241  usbmode* mode = profile->mode + i;
242  memcpy(mode->name, hw->name[i + 1], MD_NAME_LEN * 2);
243  memcpy(&mode->id, hw->id + i + 1, sizeof(usbid));
244  memcpy(&mode->light, hw->light + i, sizeof(lighting));
245  memcpy(&mode->dpi, hw->dpi + i, sizeof(dpiset));
246  // Set a force update on the light/DPI since they've been overwritten
247  mode->light.forceupdate = mode->dpi.forceupdate = 1;
248  }
249  profile->lastlight.forceupdate = profile->lastdpi.forceupdate = 1;
250 }
lighting lastlight
Definition: structures.h:107
usbmode mode[6]
Definition: structures.h:103
ushort name[16]
Definition: structures.h:89
#define MD_NAME_LEN
Definition: structures.h:82
usbid id
Definition: structures.h:111
#define PR_NAME_LEN
Definition: structures.h:99
dpiset lastdpi
Definition: structures.h:108
lighting light
Definition: structures.h:84
usbid id[3+1]
Definition: structures.h:123
dpiset dpi
Definition: structures.h:86
ushort name[16]
Definition: structures.h:110
uchar forceupdate
Definition: structures.h:77
dpiset dpi[3]
Definition: structures.h:121
ushort name[3+1][16]
Definition: structures.h:125
lighting light[3]
Definition: structures.h:120
uchar forceupdate
Definition: structures.h:69
usbid id
Definition: structures.h:88

+ Here is the caller graph for this function:

static void initmode ( usbmode mode)
static

Definition at line 175 of file profile.c.

References usbmode::bind, usbmode::dpi, dpiset::forceupdate, lighting::forceupdate, initbind(), and usbmode::light.

Referenced by allocprofile(), and cmd_erase().

175  {
176  memset(mode, 0, sizeof(*mode));
177  mode->light.forceupdate = 1;
178  mode->dpi.forceupdate = 1;
179  initbind(&mode->bind);
180 }
void initbind(binding *bind)
Definition: input.c:292
lighting light
Definition: structures.h:84
dpiset dpi
Definition: structures.h:86
uchar forceupdate
Definition: structures.h:77
binding bind
Definition: structures.h:85
uchar forceupdate
Definition: structures.h:69

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int loadprofile ( usbdevice kb)

Definition at line 192 of file profile.c.

References hwloadprofile.

192  {
193  if(hwloadprofile(kb, 1))
194  return -1;
195  return 0;
196 }
#define hwloadprofile(kb, apply)
Definition: profile.h:52
void nativetohw ( usbprofile profile,
hwprofile hw,
int  modecount 
)

Definition at line 252 of file profile.c.

References usbmode::dpi, hwprofile::dpi, usbmode::id, usbprofile::id, hwprofile::id, usbmode::light, hwprofile::light, MD_NAME_LEN, usbprofile::mode, usbmode::name, usbprofile::name, hwprofile::name, and PR_NAME_LEN.

Referenced by cmd_hwsave_kb(), and cmd_hwsave_mouse().

252  {
253  // Copy name and ID
254  memcpy(hw->name[0], profile->name, PR_NAME_LEN * 2);
255  memcpy(hw->id, &profile->id, sizeof(usbid));
256  // Copy the mode settings
257  for(int i = 0; i < modecount; i++){
258  usbmode* mode = profile->mode + i;
259  memcpy(hw->name[i + 1], mode->name, MD_NAME_LEN * 2);
260  memcpy(hw->id + i + 1, &mode->id, sizeof(usbid));
261  memcpy(hw->light + i, &mode->light, sizeof(lighting));
262  memcpy(hw->dpi + i, &mode->dpi, sizeof(dpiset));
263  }
264 }
usbmode mode[6]
Definition: structures.h:103
ushort name[16]
Definition: structures.h:89
#define MD_NAME_LEN
Definition: structures.h:82
usbid id
Definition: structures.h:111
#define PR_NAME_LEN
Definition: structures.h:99
lighting light
Definition: structures.h:84
usbid id[3+1]
Definition: structures.h:123
dpiset dpi
Definition: structures.h:86
ushort name[16]
Definition: structures.h:110
dpiset dpi[3]
Definition: structures.h:121
ushort name[3+1][16]
Definition: structures.h:125
lighting light[3]
Definition: structures.h:120
usbid id
Definition: structures.h:88

+ Here is the caller graph for this function:

char* printname ( ushort name,
int  length 
)

Definition at line 132 of file profile.c.

References u16dec(), and urlencode2().

Referenced by gethwmodename(), gethwprofilename(), getmodename(), and getprofilename().

132  {
133  // Convert the name to UTF-8
134  char* buffer = calloc(1, length * 4 - 3);
135  size_t srclen = length, dstlen = length * 4 - 4;
136  u16dec(name, buffer, &srclen, &dstlen);
137  // URL-encode it
138  char* buffer2 = malloc(strlen(buffer) * 3 + 1);
139  urlencode2(buffer2, buffer);
140  free(buffer);
141  return buffer2;
142 }
void urlencode2(char *dst, const char *src)
Definition: profile.c:37
void u16dec(ushort *in, char *out, size_t *srclen, size_t *dstlen)
Definition: profile.c:105

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int setid ( usbid id,
const char *  guid 
)

Definition at line 64 of file profile.c.

References usbid::guid.

Referenced by cmd_id(), and cmd_profileid().

64  {
65  int32_t data1;
66  int16_t data2, data3, data4a;
67  char data4b[6];
68  if(sscanf(guid, "{%08X-%04hX-%04hX-%04hX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX}",
69  &data1, &data2, &data3, &data4a, data4b, data4b + 1, data4b + 2, data4b + 3, data4b + 4, data4b + 5) != 10)
70  return 0;
71  memcpy(id->guid + 0x0, &data1, 4);
72  memcpy(id->guid + 0x4, &data2, 2);
73  memcpy(id->guid + 0x6, &data3, 2);
74  memcpy(id->guid + 0x8, &data4a, 2);
75  memcpy(id->guid + 0xA, data4b, 6);
76  return 1;
77 }
char guid[16]
Definition: structures.h:9

+ Here is the caller graph for this function:

void u16dec ( ushort in,
char *  out,
size_t *  srclen,
size_t *  dstlen 
)

Definition at line 105 of file profile.c.

References utf16to8.

Referenced by printname().

105  {
106  if(!utf16to8)
107  utf16to8 = iconv_open("UTF-8", "UTF-16LE");
108  size_t srclen2 = 0, srclenmax = *srclen;
109  for(; srclen2 < srclenmax; srclen2++){
110  if(!in[srclen2])
111  break;
112  }
113  *srclen = srclen2 * 2;
114  iconv(utf16to8, (char**)&in, srclen, &out, dstlen);
115 }
static iconv_t utf16to8
Definition: profile.c:95

+ Here is the caller graph for this function:

void u16enc ( char *  in,
ushort out,
size_t *  srclen,
size_t *  dstlen 
)

Definition at line 97 of file profile.c.

References utf8to16.

Referenced by cmd_name(), and cmd_profilename().

97  {
98  if(!utf8to16)
99  utf8to16 = iconv_open("UTF-16LE", "UTF-8");
100  memset(out, 0, *dstlen * 2);
101  *dstlen = *dstlen * 2 - 2;
102  iconv(utf8to16, &in, srclen, (char**)&out, dstlen);
103 }
static iconv_t utf8to16
Definition: profile.c:95

+ Here is the caller graph for this function:

void urldecode2 ( char *  dst,
const char *  src 
)

Definition at line 8 of file profile.c.

Referenced by cmd_name(), and cmd_profilename().

8  {
9  char a, b;
10  char s;
11  while((s = *src)){
12  if((s == '%') &&
13  ((a = src[1]) && (b = src[2])) &&
14  (isxdigit(a) && isxdigit(b))){
15  if(a >= 'a')
16  a -= 'a'-'A';
17  if(a >= 'A')
18  a -= 'A' - 10;
19  else
20  a -= '0';
21  if(b >= 'a')
22  b -= 'a'-'A';
23  if(b >= 'A')
24  b -= 'A' - 10;
25  else
26  b -= '0';
27  *dst++ = 16 * a + b;
28  src += 3;
29  } else {
30  *dst++ = s;
31  src++;
32  }
33  }
34  *dst = '\0';
35 }

+ Here is the caller graph for this function:

void urlencode2 ( char *  dst,
const char *  src 
)

Definition at line 37 of file profile.c.

Referenced by printname().

37  {
38  char s;
39  while((s = *src++)){
40  if(s <= ',' || s == '/' ||
41  (s >= ':' && s <= '@') ||
42  s == '[' || s == ']' ||
43  s >= 0x7F){
44  char a = s >> 4, b = s & 0xF;
45  if(a >= 10)
46  a += 'A' - 10;
47  else
48  a += '0';
49  if(b >= 10)
50  b += 'A' - 10;
51  else
52  b += '0';
53  dst[0] = '%';
54  dst[1] = a;
55  dst[2] = b;
56  dst += 3;
57  } else
58  *dst++ = s;
59  }
60  *dst = '\0';
61 }

+ Here is the caller graph for this function:

Variable Documentation

iconv_t utf16to8 = 0
static

Definition at line 95 of file profile.c.

Referenced by u16dec().

iconv_t utf8to16 = 0
static

Definition at line 95 of file profile.c.

Referenced by u16enc().