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
device_keyboard.c File Reference
#include "command.h"
#include "device.h"
#include "devnode.h"
#include "firmware.h"
#include "input.h"
#include "profile.h"
#include "usb.h"
+ Include dependency graph for device_keyboard.c:

Go to the source code of this file.

Functions

int start_kb_nrgb (usbdevice *kb, int makeactive)
 
int setactive_kb (usbdevice *kb, int active)
 
int cmd_active_kb (usbdevice *kb, usbmode *dummy1, int dummy2, int dummy3, const char *dummy4)
 
int cmd_idle_kb (usbdevice *kb, usbmode *dummy1, int dummy2, int dummy3, const char *dummy4)
 
void setmodeindex_nrgb (usbdevice *kb, int index)
 

Function Documentation

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

Definition at line 112 of file device_keyboard.c.

References setactive_kb().

112  {
113  return setactive_kb(kb, 1);
114 }
int setactive_kb(usbdevice *kb, int active)

+ Here is the call graph for this function:

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

Definition at line 116 of file device_keyboard.c.

References setactive_kb().

116  {
117  return setactive_kb(kb, 0);
118 }
int setactive_kb(usbdevice *kb, int active)

+ Here is the call graph for this function:

int setactive_kb ( usbdevice kb,
int  active 
)

Definition at line 18 of file device_keyboard.c.

References ACT_LIGHT, ACT_LOCK, ACT_M1, ACT_M2, ACT_M3, ACT_MR_RING, usbdevice::active, DELAY_MEDIUM, lighting::forceupdate, imutex, IN_CORSAIR, IN_HID, usbdevice::input, inputupdate(), keymap, usbinput::keys, usbprofile::lastlight, MSG_SIZE, N_KEYS_HW, NEEDS_FW_UPDATE, usbdevice::profile, usbsend, and usbdevice::vtable.

Referenced by cmd_active_kb(), and cmd_idle_kb().

18  {
19  if(NEEDS_FW_UPDATE(kb))
20  return 0;
21 
22  pthread_mutex_lock(imutex(kb));
23  kb->active = !!active;
24  kb->profile->lastlight.forceupdate = 1;
25  // Clear input
26  memset(&kb->input.keys, 0, sizeof(kb->input.keys));
27  inputupdate(kb);
28  pthread_mutex_unlock(imutex(kb));
29 
30  uchar msg[3][MSG_SIZE] = {
31  { 0x07, 0x04, 0 }, // Disables or enables HW control for top row
32  { 0x07, 0x40, 0 }, // Selects key input
33  { 0x07, 0x05, 2, 0, 0x03, 0x00 } // Commits key input selection
34  };
35  if(active){
36  // Put the M-keys (K95) as well as the Brightness/Lock keys into software-controlled mode.
37  msg[0][2] = 2;
38  if(!usbsend(kb, msg[0], 1))
39  return -1;
40  DELAY_MEDIUM(kb);
41  // Set input mode on the keys. They must be grouped into packets of 60 bytes (+ 4 bytes header)
42  // Keys are referenced in byte pairs, with the first byte representing the key and the second byte representing the mode.
43  for(int key = 0; key < N_KEYS_HW; ){
44  int pair;
45  for(pair = 0; pair < 30 && key < N_KEYS_HW; pair++, key++){
46  // Select both standard and Corsair input. The standard input will be ignored except in BIOS mode.
47  uchar action = IN_HID | IN_CORSAIR;
48  // Additionally, make MR activate the MR ring (this is disabled for now, may be back later)
49  //if(keymap[key].name && !strcmp(keymap[key].name, "mr"))
50  // action |= ACT_MR_RING;
51  msg[1][4 + pair * 2] = key;
52  msg[1][5 + pair * 2] = action;
53  }
54  // Byte 2 = pair count (usually 30, less on final message)
55  msg[1][2] = pair;
56  if(!usbsend(kb, msg[1], 1))
57  return -1;
58  }
59  // Commit new input settings
60  if(!usbsend(kb, msg[2], 1))
61  return -1;
62  DELAY_MEDIUM(kb);
63  } else {
64  // Set the M-keys back into hardware mode, restore hardware RGB profile. It has to be sent twice for some reason.
65  msg[0][2] = 1;
66  if(!usbsend(kb, msg[0], 1))
67  return -1;
68  DELAY_MEDIUM(kb);
69  if(!usbsend(kb, msg[0], 1))
70  return -1;
71  DELAY_MEDIUM(kb);
72 #ifdef OS_LINUX
73  // On OSX the default key mappings are fine. On Linux, the G keys will freeze the keyboard. Set the keyboard entirely to HID input.
74  for(int key = 0; key < N_KEYS_HW; ){
75  int pair;
76  for(pair = 0; pair < 30 && key < N_KEYS_HW; pair++, key++){
77  uchar action = IN_HID;
78  // Enable hardware actions
79  if(keymap[key].name){
80  if(!strcmp(keymap[key].name, "mr"))
81  action = ACT_MR_RING;
82  else if(!strcmp(keymap[key].name, "m1"))
83  action = ACT_M1;
84  else if(!strcmp(keymap[key].name, "m2"))
85  action = ACT_M2;
86  else if(!strcmp(keymap[key].name, "m3"))
87  action = ACT_M3;
88  else if(!strcmp(keymap[key].name, "light"))
89  action = ACT_LIGHT;
90  else if(!strcmp(keymap[key].name, "lock"))
91  action = ACT_LOCK;
92  }
93  msg[1][4 + pair * 2] = key;
94  msg[1][5 + pair * 2] = action;
95  }
96  // Byte 2 = pair count (usually 30, less on final message)
97  msg[1][2] = pair;
98  if(!usbsend(kb, msg[1], 1))
99  return -1;
100  }
101  // Commit new input settings
102  if(!usbsend(kb, msg[2], 1))
103  return -1;
104  DELAY_MEDIUM(kb);
105 #endif
106  }
107  // Update indicator LEDs if the profile contains settings for them
108  kb->vtable->updateindicators(kb, 0);
109  return 0;
110 }
#define MSG_SIZE
Definition: structures.h:176
lighting lastlight
Definition: structures.h:107
usbprofile * profile
Definition: structures.h:221
#define ACT_MR_RING
Definition: device.h:72
#define DELAY_MEDIUM(kb)
the medium delay is used after sending a command before waiting for the answer.
Definition: usb.h:152
#define ACT_LIGHT
Definition: device.h:68
usbinput input
Definition: structures.h:245
#define ACT_LOCK
Definition: device.h:71
#define ACT_M2
Definition: device.h:74
uchar keys[((((152+3+12)+25)+7)/8)]
Definition: structures.h:130
char active
Definition: structures.h:231
#define N_KEYS_HW
Definition: keymap.h:24
void inputupdate(usbdevice *kb)
Definition: input.c:241
unsigned char uchar
Definition: includes.h:24
uchar forceupdate
Definition: structures.h:77
#define IN_HID
Definition: device.h:64
#define NEEDS_FW_UPDATE(kb)
Definition: structures.h:161
const union devcmd * vtable
Definition: structures.h:180
#define imutex(kb)
Definition: device.h:22
#define ACT_M3
Definition: device.h:75
const key keymap[(((152+3+12)+25)+11)]
Definition: keymap.c:5
#define ACT_M1
Definition: device.h:73
#define usbsend(kb, messages, count)
usbsend macro is used to wrap _usbsend() with debugging information (file and lineno) ...
Definition: usb.h:239
Definition: keymap.h:49
#define IN_CORSAIR
Definition: device.h:65

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void setmodeindex_nrgb ( usbdevice kb,
int  index 
)

Definition at line 120 of file device_keyboard.c.

References NK95_M1, NK95_M2, NK95_M3, and nk95cmd.

120  {
121  switch(index % 3){
122  case 0:
123  nk95cmd(kb, NK95_M1);
124  break;
125  case 1:
126  nk95cmd(kb, NK95_M2);
127  break;
128  case 2:
129  nk95cmd(kb, NK95_M3);
130  break;
131  }
132 }
#define nk95cmd(kb, command)
nk95cmd() macro is used to wrap _nk95cmd() with debugging information (file and lineno). the command structure is different: Just the bits 23..16 are used as bits 7..0 for bRequest Bits 15..0 are used as wValue
Definition: usb.h:296
#define NK95_M1
Switch to mode 1.
Definition: usb.h:307
#define NK95_M3
Switch to mode 3.
Definition: usb.h:313
#define NK95_M2
Switch to mode 2.
Definition: usb.h:310
int start_kb_nrgb ( usbdevice kb,
int  makeactive 
)

Definition at line 9 of file device_keyboard.c.

References usbdevice::active, NK95_HWOFF, nk95cmd, and usbdevice::pollrate.

9  {
10  // Put the non-RGB K95 into software mode. Nothing else needs to be done hardware wise
11  nk95cmd(kb, NK95_HWOFF);
12  // Fill out RGB features for consistency, even though the keyboard doesn't have them
13  kb->active = 1;
14  kb->pollrate = -1;
15  return 0;
16 }
#define nk95cmd(kb, command)
nk95cmd() macro is used to wrap _nk95cmd() with debugging information (file and lineno). the command structure is different: Just the bits 23..16 are used as bits 7..0 for bRequest Bits 15..0 are used as wValue
Definition: usb.h:296
char active
Definition: structures.h:231
#define NK95_HWOFF
Hardware-specific commands for the K95 nonRGB,.
Definition: usb.h:301
char pollrate
Definition: structures.h:241