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
profile_keyboard.c
Go to the documentation of this file.
1 #include "profile.h"
2 #include "usb.h"
3 #include "led.h"
4 
5 static int hwloadmode(usbdevice* kb, hwprofile* hw, int mode){
6  // Ask for mode's name
7  uchar data_pkt[MSG_SIZE] = { 0x0e, 0x16, 0x01, mode + 1, 0 };
8  uchar in_pkt[MSG_SIZE];
9  if(!usbrecv(kb, data_pkt, in_pkt))
10  return -1;
11  memcpy(hw->name[mode + 1], in_pkt + 4, MD_NAME_LEN * 2);
12  // Load the RGB setting
13  return loadrgb_kb(kb, hw->light + mode, mode);
14 }
15 
16 int cmd_hwload_kb(usbdevice* kb, usbmode* dummy1, int dummy2, int apply, const char* dummy3){
17  (void)dummy1;
18  (void)dummy2;
19  (void)dummy3;
20 
21  DELAY_LONG(kb);
22  hwprofile* hw = calloc(1, sizeof(hwprofile));
23  // Ask for profile and mode IDs
24  uchar data_pkt[2][MSG_SIZE] = {
25  { 0x0e, 0x15, 0x01, 0 },
26  { 0x0e, 0x16, 0x01, 0 }
27  };
28  uchar in_pkt[MSG_SIZE];
29  int modes = (IS_K95(kb) ? HWMODE_K95 : HWMODE_K70);
30  for(int i = 0; i <= modes; i++){
31  data_pkt[0][3] = i;
32  if(!usbrecv(kb, data_pkt[0], in_pkt)){
33  free(hw);
34  return -1;
35  }
36  memcpy(hw->id + i, in_pkt + 4, sizeof(usbid));
37  }
38  // Ask for profile name
39  if(!usbrecv(kb, data_pkt[1], in_pkt)){
40  free(hw);
41  return -1;
42  }
43  memcpy(hw->name[0], in_pkt + 4, PR_NAME_LEN * 2);
44  // Load modes
45  for(int i = 0; i < modes; i++){
46  if(hwloadmode(kb, hw, i)){
47  free(hw);
48  return -1;
49  }
50  }
51  // Make the profile active (if requested)
52  if(apply)
53  hwtonative(kb->profile, hw, modes);
54  // Free the existing profile (if any)
55  free(kb->hw);
56  kb->hw = hw;
57  DELAY_LONG(kb);
58  return 0;
59 }
60 
61 int cmd_hwsave_kb(usbdevice* kb, usbmode* dummy1, int dummy2, int dummy3, const char* dummy4){
62  (void)dummy1;
63  (void)dummy2;
64  (void)dummy3;
65  (void)dummy4;
66 
67  DELAY_LONG(kb);
68  hwprofile* hw = kb->hw;
69  if(!hw)
70  hw = kb->hw = calloc(1, sizeof(hwprofile));
71  int modes = (IS_K95(kb) ? HWMODE_K95 : HWMODE_K70);
72  nativetohw(kb->profile, hw, modes);
73  // Save the profile and mode names
74  uchar data_pkt[2][MSG_SIZE] = {
75  { 0x07, 0x16, 0x01, 0 },
76  { 0x07, 0x15, 0x01, 0 },
77  };
78  // Save the mode names
79  for(int i = 0; i <= modes; i++){
80  data_pkt[0][3] = i;
81  memcpy(data_pkt[0] + 4, hw->name[i], MD_NAME_LEN * 2);
82  if(!usbsend(kb, data_pkt[0], 1))
83  return -1;
84  }
85  // Save the IDs
86  for(int i = 0; i <= modes; i++){
87  data_pkt[1][3] = i;
88  memcpy(data_pkt[1] + 4, hw->id + i, sizeof(usbid));
89  if(!usbsend(kb, data_pkt[1], 1))
90  return -1;
91  }
92  // Save the RGB data
93  for(int i = 0; i < modes; i++){
94  if(savergb_kb(kb, hw->light + i, i))
95  return -1;
96  }
97  DELAY_LONG(kb);
98  return 0;
99 }
#define HWMODE_K70
Definition: structures.h:115
#define MSG_SIZE
Definition: structures.h:176
usbprofile * profile
Definition: structures.h:221
#define DELAY_LONG(kb)
The longest delay takes place where something went wrong (eg when resetting the device) ...
Definition: usb.h:186
#define MD_NAME_LEN
Definition: structures.h:82
#define PR_NAME_LEN
Definition: structures.h:99
usbid id[3+1]
Definition: structures.h:123
int cmd_hwsave_kb(usbdevice *kb, usbmode *dummy1, int dummy2, int dummy3, const char *dummy4)
unsigned char uchar
Definition: includes.h:24
int loadrgb_kb(usbdevice *kb, lighting *light, int mode)
Definition: led_keyboard.c:190
void hwtonative(usbprofile *profile, hwprofile *hw, int modecount)
Definition: profile.c:260
static int hwloadmode(usbdevice *kb, hwprofile *hw, int mode)
void nativetohw(usbprofile *profile, hwprofile *hw, int modecount)
Definition: profile.c:277
#define HWMODE_K95
Definition: structures.h:116
hwprofile * hw
Definition: structures.h:223
ushort name[3+1][16]
Definition: structures.h:125
#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.
int cmd_hwload_kb(usbdevice *kb, usbmode *dummy1, int dummy2, int apply, const char *dummy3)
int savergb_kb(usbdevice *kb, lighting *light, int mode)
Definition: led_keyboard.c:148
#define IS_K95(kb)
Definition: usb.h:83
#define usbsend(kb, messages, count)
usbsend macro is used to wrap _usbsend() with debugging information (file and lineno) ...
Definition: usb.h:271
lighting light[3]
Definition: structures.h:120