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
structures.h
Go to the documentation of this file.
1 #ifndef STRUCTURES_H
2 #define STRUCTURES_H
3 
4 #include "includes.h"
5 #include "keymap.h"
6 
7 // Profile ID structure
8 typedef struct {
9  char guid[16];
10  char modified[4];
11 } usbid;
12 
13 // Used to manipulate key bitfields
14 // The do-while business is a hack to make statements like "if(a) SET_KEYBIT(...); else CLEAR_KEYBIT(...);" work
15 #define SET_KEYBIT(array, index) do { (array)[(index) / 8] |= 1 << ((index) % 8); } while(0)
16 #define CLEAR_KEYBIT(array, index) do { (array)[(index) / 8] &= ~(1 << ((index) % 8)); } while(0)
17 
18 // Indicator LEDs
19 #define I_NUM 1
20 #define I_CAPS 2
21 #define I_SCROLL 4
22 
23 // Maximum number of notification nodes
24 #define OUTFIFO_MAX 10
25 
26 // Action triggered when activating a macro
27 typedef struct {
28  short scan; // Key scancode, OR
29  short rel_x, rel_y; // Mouse movement
30  char down; // 0 for keyup, 1 for keydown (ignored if rel_x != 0 || rel_y != 0)
31  uint delay; // us delay after action; UINT_MAX for use global delay
32 } macroaction;
33 
34 // Key macro
35 typedef struct {
39  char triggered;
40 } keymacro;
41 
42 // Key bindings for a mode (keyboard + mouse)
43 typedef struct {
44  // Base bindings
45  int base[N_KEYS_INPUT];
46  // Macros
49  int macrocap;
50 } binding;
51 #define MACRO_MAX 1024
52 
53 // DPI settings for mice
54 #define DPI_COUNT 6
55 #define LIFT_MIN 1
56 #define LIFT_MAX 5
57 typedef struct {
58  // DPI levels
62  // Enabled modes (bitfield)
64  // Lift height (lowest to highest)
66  // Angle snap enabled?
68  // Send to device even if unchanged? (used when initializing profiles)
70 } dpiset;
71 
72 // Lighting structure for a mode
73 typedef struct {
78  uchar sidelight; // strafe sidelight
79 } lighting;
80 
81 // Native mode structure
82 #define MD_NAME_LEN 16
83 typedef struct {
87  // Name and UUID
90  // Key notification settings (bitfield - 0: off, 1: on)
92  // Indicators permanently off/on
93  uchar ioff, ion;
94  // Notify mask for indicator LEDs
95  uchar inotify[OUTFIFO_MAX];
96 } usbmode;
97 
98 // Native profile structure
99 #define PR_NAME_LEN 16
100 #define MODE_COUNT 6
101 typedef struct {
102  // Modes
104  // Currently-selected mode
106  // Last data sent to the device
109  // Profile name and UUID
112 } usbprofile;
113 
114 // Hardware profile structure
115 #define HWMODE_K70 1
116 #define HWMODE_K95 3
117 #define HWMODE_MAX 3
118 typedef struct {
119  // RGB settings
122  // Profile (0) and mode (1...HWMODE_MAX) IDs
123  usbid id[HWMODE_MAX + 1];
124  // Profile and mode names
126 } hwprofile;
127 
128 // Keyboard/mouse input tracking
129 typedef struct {
132  short rel_x, rel_y;
133 } usbinput;
134 
135 // Device features
136 #define FEAT_RGB 0x001 // RGB backlighting?
137 #define FEAT_MONOCHROME 0x002 // RGB protocol but single-color only?
138 #define FEAT_POLLRATE 0x004 // Known poll rate?
139 #define FEAT_ADJRATE 0x008 // Adjustable poll rate?
140 #define FEAT_BIND 0x010 // Rebindable keys?
141 #define FEAT_NOTIFY 0x020 // Key notifications?
142 #define FEAT_FWVERSION 0x040 // Has firmware version?
143 #define FEAT_FWUPDATE 0x080 // Has firmware update?
144 #define FEAT_HWLOAD 0x100 // Hardware load enabled?
145 
146 #define FEAT_ANSI 0x200 // ANSI/ISO layout toggle (Mac only - not needed on Linux)
147 #define FEAT_ISO 0x400
148 #define FEAT_MOUSEACCEL 0x800 // Mouse acceleration (also Mac only)
149 
150 // Standard feature sets
151 #define FEAT_COMMON (FEAT_BIND | FEAT_NOTIFY | FEAT_FWVERSION | FEAT_MOUSEACCEL | FEAT_HWLOAD)
152 #define FEAT_STD_RGB (FEAT_COMMON | FEAT_RGB | FEAT_POLLRATE | FEAT_FWUPDATE)
153 #define FEAT_STD_NRGB (FEAT_COMMON)
154 #define FEAT_LMASK (FEAT_ANSI | FEAT_ISO)
155 
156 // Feature test (usbdevice* kb, int feat)
157 #define HAS_FEATURES(kb, feat) (((kb)->features & (feat)) == (feat))
158 #define HAS_ANY_FEATURE(kb, feat) (!!((kb)->features & (feat)))
159 
160 // Bricked firmware?
161 #define NEEDS_FW_UPDATE(kb) ((kb)->fwversion == 0 && HAS_FEATURES((kb), FEAT_FWUPDATE | FEAT_FWVERSION))
162 
163 // Lines per scroll (OSX only)
164 #define SCROLL_ACCELERATED 0
165 #define SCROLL_MIN 1
166 #define SCROLL_MAX 10
167 
168 // vtables for keyboards/mice (see command.h)
169 extern const union devcmd vtable_keyboard;
170 extern const union devcmd vtable_keyboard_nonrgb;
171 extern const union devcmd vtable_mouse;
172 
173 // Structure for tracking keyboard/mouse devices
174 #define KB_NAME_LEN 40
175 #define SERIAL_LEN 34
176 #define MSG_SIZE 64
177 #define IFACE_MAX 4
178 typedef struct {
179  // Function table (see command.h)
180  const union devcmd* vtable;
181  // I/O devices
182  // Note that FDs have 1 added to them, because these are initialized to zero when the program starts but zero is technically a valid FD
183  // So the actual value is (fd - 1)
184 #ifdef OS_LINUX
185  // USB device
186  struct udev_device* udev;
187  int handle;
188  // uinput handles
189  int uinput_kb, uinput_mouse;
190 #else
191  // USB identifier
192  uint32_t location_id[IFACE_MAX + 1];
193  // Device handles
194  usb_dev_t handle;
195  usb_iface_t ifusb[IFACE_MAX];
196  hid_dev_t ifhid[IFACE_MAX];
197  io_object_t rm_notify[IFACE_MAX * 2 + 1]; // one for each: handle, ifusb, ifhid
198  int epcount_hid, epcount_usb;
199  // Result code from the last USB transfer
200  kern_return_t lastresult;
201  // Input handle
202  io_connect_t event;
203  // Key-repeat info
204  CFRunLoopRef input_loop;
205  CFRunLoopTimerRef krtimer;
206  struct timespec keyrepeat;
207  short lastkeypress;
208  // Modifier/mouse button state
209  pthread_t indicthread;
210  IOOptionBits modifiers;
211  uchar mousestate;
212  char scroll_rate;
213 #endif
214  // Number of endpoints on the USB device
215  int epcount;
216  // Thread used for USB/devnode communication. To close: lock mutexes, set handle to zero, unlock, then wait for thread to stop
217  pthread_t thread;
218  // Thread for device input. Will close on its own when the device is terminated.
219  pthread_t inputthread;
220  // Keyboard settings
222  // Hardware modes. Null if not read yet
224  // Command FIFO
225  int infifo;
226  // Notification FIFOs, or zero if a FIFO is closed
227  int outfifo[OUTFIFO_MAX];
228  // Features (see F_ macros)
230  // Whether the keyboard is being actively controlled by the driver
231  char active;
232  // Device name
233  char name[KB_NAME_LEN+1]; // increase by 1 for the trailing \0 for names that are exactly KB_NAME_LEN, e.g. "Corsair STRAFE RGB Gaming Keyboard"
234  // Device serial number
235  char serial[SERIAL_LEN];
236  // USB vendor and product IDs
237  short vendor, product;
238  // Firmware version
240  // Poll rate (ms), or -1 if unsupported
241  char pollrate;
242  // USB protocol delay (ms)
243  char usbdelay;
244  // Current input state
246  // Indicator LED state
247  uchar hw_ileds, hw_ileds_old, ileds;
248  // Color dithering in use
249  char dither;
250  // Flag to check if large macros should be sent delayed
251  uint delay;
252 } usbdevice;
253 
254 #endif // STRUCTURES_H
short scan
Definition: structures.h:28
#define KB_NAME_LEN
Definition: structures.h:174
uchar sidelight
Definition: structures.h:78
lighting lastlight
Definition: structures.h:107
Definition: command.h:73
#define MODE_COUNT
Definition: structures.h:100
uint delay
Definition: structures.h:251
float y
Definition: main.c:66
usbprofile * profile
Definition: structures.h:221
uchar enabled
Definition: structures.h:63
uchar ion
Definition: structures.h:93
usbinput input
Definition: structures.h:245
#define HWMODE_MAX
Definition: structures.h:117
#define N_MOUSE_ZONES_EXTENDED
Definition: keymap.h:41
usbmode * currentmode
Definition: structures.h:105
pthread_t inputthread
Definition: structures.h:219
float x
Definition: main.c:66
ushort fwversion
Definition: structures.h:239
short rel_y
Definition: structures.h:29
struct udev_device * udev
Definition: structures.h:186
char dither
Definition: structures.h:249
#define MD_NAME_LEN
Definition: structures.h:82
usbid id
Definition: structures.h:111
#define PR_NAME_LEN
Definition: structures.h:99
uchar ileds
Definition: structures.h:247
char active
Definition: structures.h:231
int actioncount
Definition: structures.h:37
dpiset lastdpi
Definition: structures.h:108
lighting light
Definition: structures.h:84
#define IFACE_MAX
Definition: structures.h:177
int epcount
Definition: structures.h:215
dpiset dpi
Definition: structures.h:86
int infifo
Definition: structures.h:225
#define N_KEYBYTES_INPUT
Definition: keymap.h:37
uint delay
Definition: structures.h:31
unsigned char uchar
Definition: includes.h:24
int uinput_mouse
Definition: structures.h:189
uchar forceupdate
Definition: structures.h:77
uchar snap
Definition: structures.h:67
#define N_KEYS_HW
Definition: keymap.h:24
uchar lift
Definition: structures.h:65
char pollrate
Definition: structures.h:241
short rel_y
Definition: structures.h:132
unsigned short ushort
Definition: includes.h:25
const union devcmd * vtable
Definition: structures.h:180
int macrocap
Definition: structures.h:49
char triggered
Definition: structures.h:39
#define DPI_COUNT
Definition: structures.h:54
int macrocount
Definition: structures.h:48
binding bind
Definition: structures.h:85
hwprofile * hw
Definition: structures.h:223
int handle
Definition: structures.h:187
#define N_KEYS_INPUT
Definition: keymap.h:36
uchar current
Definition: structures.h:61
ushort features
Definition: structures.h:229
char usbdelay
Definition: structures.h:243
const union devcmd vtable_keyboard_nonrgb
Definition: device_vtable.c:99
pthread_t thread
Definition: structures.h:217
keymacro * macros
Definition: structures.h:47
short vendor
Definition: structures.h:237
uchar forceupdate
Definition: structures.h:69
usbid id
Definition: structures.h:88
#define SERIAL_LEN
Definition: structures.h:175
macroaction * actions
Definition: structures.h:36
const union devcmd vtable_keyboard
RGB keyboard vtable holds functions for each device type.
Definition: device_vtable.c:52
#define OUTFIFO_MAX
Definition: structures.h:24
const union devcmd vtable_mouse