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
input.h File Reference
#include "includes.h"
#include "usb.h"
+ Include dependency graph for input.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  parameter
 struct parameter contains the values for a fresh started macro_play thread. parameter_t is the typedef for it. More...
 
struct  ptlist
 struct ptlist is one element in the single linked list to store macro_play threads waiting for their execution ptlist_t is the typedef for it. More...
 

Macros

#define IS_MOD(s)   ((s) == KEY_CAPSLOCK || (s) == KEY_NUMLOCK || (s) == KEY_SCROLLLOCK || (s) == KEY_LEFTSHIFT || (s) == KEY_RIGHTSHIFT || (s) == KEY_LEFTCTRL || (s) == KEY_RIGHTCTRL || (s) == KEY_LEFTMETA || (s) == KEY_RIGHTMETA || (s) == KEY_LEFTALT || (s) == KEY_RIGHTALT || (s) == KEY_FN)
 

Typedefs

typedef struct parameter parameter_t
 struct parameter contains the values for a fresh started macro_play thread. parameter_t is the typedef for it. More...
 
typedef struct ptlist ptlist_t
 struct ptlist is one element in the single linked list to store macro_play threads waiting for their execution ptlist_t is the typedef for it. More...
 

Functions

int os_inputopen (usbdevice *kb)
 os_inputopen More...
 
void os_inputclose (usbdevice *kb)
 
void inputupdate (usbdevice *kb)
 
void updateindicators_kb (usbdevice *kb, int force)
 
void initbind (binding *bind)
 
void freebind (binding *bind)
 
void cmd_bind (usbdevice *kb, usbmode *mode, int dummy, int keyindex, const char *to)
 
void cmd_unbind (usbdevice *kb, usbmode *mode, int dummy, int keyindex, const char *ignored)
 
void cmd_rebind (usbdevice *kb, usbmode *mode, int dummy, int keyindex, const char *ignored)
 
void cmd_macro (usbdevice *kb, usbmode *mode, const int notifynumber, const char *keys, const char *assignment)
 
void os_keypress (usbdevice *kb, int scancode, int down)
 
void os_mousemove (usbdevice *kb, int x, int y)
 
int os_setupindicators (usbdevice *kb)
 

Data Structure Documentation

struct parameter

Definition at line 54 of file input.h.

+ Collaboration diagram for parameter:
Data Fields
usbdevice * kb
keymacro * macro
struct ptlist

Definition at line 62 of file input.h.

+ Collaboration diagram for ptlist:
Data Fields
struct ptlist * next
pthread_t thread_id

Macro Definition Documentation

#define IS_MOD (   s)    ((s) == KEY_CAPSLOCK || (s) == KEY_NUMLOCK || (s) == KEY_SCROLLLOCK || (s) == KEY_LEFTSHIFT || (s) == KEY_RIGHTSHIFT || (s) == KEY_LEFTCTRL || (s) == KEY_RIGHTCTRL || (s) == KEY_LEFTMETA || (s) == KEY_RIGHTMETA || (s) == KEY_LEFTALT || (s) == KEY_RIGHTALT || (s) == KEY_FN)

Definition at line 34 of file input.h.

Referenced by inputupdate_keys().

Typedef Documentation

typedef struct parameter parameter_t
typedef struct ptlist ptlist_t

Function Documentation

void cmd_bind ( usbdevice kb,
usbmode mode,
int  dummy,
int  keyindex,
const char *  to 
)

Definition at line 307 of file input.c.

References binding::base, usbmode::bind, imutex, keymap, N_KEYS_INPUT, and key::scan.

307  {
308  (void)dummy;
309 
310  if(keyindex >= N_KEYS_INPUT)
311  return;
312  // Find the key to bind to
313  int tocode = 0;
314  if(sscanf(to, "#x%ux", &tocode) != 1 && sscanf(to, "#%u", &tocode) == 1 && tocode < N_KEYS_INPUT){
315  pthread_mutex_lock(imutex(kb));
316  mode->bind.base[keyindex] = tocode;
317  pthread_mutex_unlock(imutex(kb));
318  return;
319  }
320  // If not numeric, look it up
321  for(int i = 0; i < N_KEYS_INPUT; i++){
322  if(keymap[i].name && !strcmp(to, keymap[i].name)){
323  pthread_mutex_lock(imutex(kb));
324  mode->bind.base[keyindex] = keymap[i].scan;
325  pthread_mutex_unlock(imutex(kb));
326  return;
327  }
328  }
329 }
short scan
Definition: keymap.h:52
const key keymap[(((152+22+12)+25)+12)]
Definition: keymap.c:5
#define imutex(kb)
Definition: device.h:22
int base[((152+22+12)+25)]
Definition: structures.h:45
binding bind
Definition: structures.h:85
#define N_KEYS_INPUT
Definition: keymap.h:36
void cmd_macro ( usbdevice kb,
usbmode mode,
const int  notifynumber,
const char *  keys,
const char *  assignment 
)

Definition at line 475 of file input.c.

References _cmd_macro(), and imutex.

475  {
476  (void)notifynumber;
477 
478  pthread_mutex_lock(imutex(kb));
479  _cmd_macro(mode, keys, assignment);
480  pthread_mutex_unlock(imutex(kb));
481 }
static void _cmd_macro(usbmode *mode, const char *keys, const char *assignment)
Definition: input.c:353
#define imutex(kb)
Definition: device.h:22

+ Here is the call graph for this function:

void cmd_rebind ( usbdevice kb,
usbmode mode,
int  dummy,
int  keyindex,
const char *  ignored 
)

Definition at line 342 of file input.c.

References binding::base, usbmode::bind, imutex, keymap, N_KEYS_INPUT, and key::scan.

342  {
343  (void)dummy;
344  (void)to;
345 
346  if(keyindex >= N_KEYS_INPUT)
347  return;
348  pthread_mutex_lock(imutex(kb));
349  mode->bind.base[keyindex] = keymap[keyindex].scan;
350  pthread_mutex_unlock(imutex(kb));
351 }
short scan
Definition: keymap.h:52
const key keymap[(((152+22+12)+25)+12)]
Definition: keymap.c:5
#define imutex(kb)
Definition: device.h:22
int base[((152+22+12)+25)]
Definition: structures.h:45
binding bind
Definition: structures.h:85
#define N_KEYS_INPUT
Definition: keymap.h:36
void cmd_unbind ( usbdevice kb,
usbmode mode,
int  dummy,
int  keyindex,
const char *  ignored 
)

Definition at line 331 of file input.c.

References binding::base, usbmode::bind, imutex, KEY_UNBOUND, and N_KEYS_INPUT.

331  {
332  (void)dummy;
333  (void)to;
334 
335  if(keyindex >= N_KEYS_INPUT)
336  return;
337  pthread_mutex_lock(imutex(kb));
338  mode->bind.base[keyindex] = KEY_UNBOUND;
339  pthread_mutex_unlock(imutex(kb));
340 }
#define imutex(kb)
Definition: device.h:22
int base[((152+22+12)+25)]
Definition: structures.h:45
binding bind
Definition: structures.h:85
#define N_KEYS_INPUT
Definition: keymap.h:36
#define KEY_UNBOUND
Definition: keymap.h:9
void freebind ( binding bind)

Definition at line 300 of file input.c.

References keymacro::actions, binding::macrocount, and binding::macros.

Referenced by freemode().

300  {
301  for(int i = 0; i < bind->macrocount; i++)
302  free(bind->macros[i].actions);
303  free(bind->macros);
304  memset(bind, 0, sizeof(*bind));
305 }
int macrocount
Definition: structures.h:48
keymacro * macros
Definition: structures.h:47
macroaction * actions
Definition: structures.h:36

+ Here is the caller graph for this function:

void initbind ( binding bind)

Definition at line 292 of file input.c.

References binding::base, keymap, binding::macrocap, binding::macrocount, binding::macros, N_KEYS_INPUT, and key::scan.

Referenced by initmode().

292  {
293  for(int i = 0; i < N_KEYS_INPUT; i++)
294  bind->base[i] = keymap[i].scan;
295  bind->macros = calloc(32, sizeof(keymacro));
296  bind->macrocap = 32;
297  bind->macrocount = 0;
298 }
short scan
Definition: keymap.h:52
const key keymap[(((152+22+12)+25)+12)]
Definition: keymap.c:5
int macrocap
Definition: structures.h:49
int macrocount
Definition: structures.h:48
int base[((152+22+12)+25)]
Definition: structures.h:45
#define N_KEYS_INPUT
Definition: keymap.h:36
keymacro * macros
Definition: structures.h:47

+ Here is the caller graph for this function:

void inputupdate ( usbdevice kb)

Definition at line 241 of file input.c.

References usbdevice::input, inputupdate_keys(), os_mousemove(), usbdevice::profile, usbinput::rel_x, usbinput::rel_y, usbdevice::uinput_kb, and usbdevice::uinput_mouse.

Referenced by os_inputmain(), setactive_kb(), and setactive_mouse().

241  {
242 #ifdef OS_LINUX
243  if((!kb->uinput_kb || !kb->uinput_mouse)
244 #else
245  if(!kb->event
246 #endif
247  || !kb->profile)
248  return;
249  // Process key/button input
250  inputupdate_keys(kb);
251  // Process mouse movement
252  usbinput* input = &kb->input;
253  if(input->rel_x != 0 || input->rel_y != 0){
254  os_mousemove(kb, input->rel_x, input->rel_y);
255  input->rel_x = input->rel_y = 0;
256  }
257  // Finish up
258  memcpy(input->prevkeys, input->keys, N_KEYBYTES_INPUT);
259 }
usbprofile * profile
Definition: structures.h:221
int uinput_kb
Definition: structures.h:189
usbinput input
Definition: structures.h:245
void os_mousemove(usbdevice *kb, int x, int y)
Definition: input_linux.c:143
uchar prevkeys[((((152+22+12)+25)+7)/8)]
Definition: structures.h:131
#define N_KEYBYTES_INPUT
Definition: keymap.h:37
short rel_x
Definition: structures.h:132
int uinput_mouse
Definition: structures.h:189
short rel_y
Definition: structures.h:132
static void inputupdate_keys(usbdevice *kb)
inputupdate_keys Handle input from Keyboard or mouse; start Macrof if detected.
Definition: input.c:134
uchar keys[((((152+22+12)+25)+7)/8)]
Definition: structures.h:130

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void os_inputclose ( usbdevice kb)

Definition at line 76 of file input_linux.c.

References ckb_warn, usbdevice::uinput_kb, and usbdevice::uinput_mouse.

Referenced by closeusb().

76  {
77  if(kb->uinput_kb <= 0 || kb->uinput_mouse <= 0)
78  return;
79  // Set all keys released
80  struct input_event event;
81  memset(&event, 0, sizeof(event));
82  event.type = EV_KEY;
83  for(int key = 0; key < KEY_CNT; key++){
84  event.code = key;
85  if(write(kb->uinput_kb - 1, &event, sizeof(event)) <= 0)
86  ckb_warn("uinput write failed: %s\n", strerror(errno));
87  if(write(kb->uinput_mouse - 1, &event, sizeof(event)) <= 0)
88  ckb_warn("uinput write failed: %s\n", strerror(errno));
89  }
90  event.type = EV_SYN;
91  event.code = SYN_REPORT;
92  if(write(kb->uinput_kb - 1, &event, sizeof(event)) <= 0)
93  ckb_warn("uinput write failed: %s\n", strerror(errno));
94  if(write(kb->uinput_mouse - 1, &event, sizeof(event)) <= 0)
95  ckb_warn("uinput write failed: %s\n", strerror(errno));
96  // Close the keyboard
97  ioctl(kb->uinput_kb - 1, UI_DEV_DESTROY);
98  close(kb->uinput_kb - 1);
99  kb->uinput_kb = 0;
100  // Close the mouse
101  ioctl(kb->uinput_mouse - 1, UI_DEV_DESTROY);
102  close(kb->uinput_mouse - 1);
103  kb->uinput_mouse = 0;
104 }
int uinput_kb
Definition: structures.h:189
Definition: keymap.h:49
int uinput_mouse
Definition: structures.h:189
#define ckb_warn(fmt, args...)
Definition: includes.h:52

+ Here is the caller graph for this function:

int os_inputopen ( usbdevice kb)
Parameters
kb
Returns

Some tips on using uinput_user_dev in

Definition at line 55 of file input_linux.c.

References usbdevice::fwversion, INDEX_OF, keyboard, usbdevice::name, usbdevice::product, usbdevice::uinput_kb, usbdevice::uinput_mouse, uinputopen(), and usbdevice::vendor.

Referenced by _setupusb().

55  {
56  // Create the new input device
57  int index = INDEX_OF(kb, keyboard);
58  struct uinput_user_dev indev;
59  memset(&indev, 0, sizeof(indev));
60  snprintf(indev.name, UINPUT_MAX_NAME_SIZE, "ckb%d: %s", index, kb->name);
61  indev.id.bustype = BUS_USB;
62  indev.id.vendor = kb->vendor;
63  indev.id.product = kb->product;
64  indev.id.version = kb->fwversion;
65  // Open keyboard
66  int fd = uinputopen(&indev, 0);
67  kb->uinput_kb = fd;
68  if(fd <= 0)
69  return 0;
70  // Open mouse
71  fd = uinputopen(&indev, 1);
72  kb->uinput_mouse = fd;
73  return fd <= 0;
74 }
char name[40+1]
Definition: structures.h:233
int uinput_kb
Definition: structures.h:189
ushort fwversion
Definition: structures.h:239
usbdevice keyboard[9]
remember all usb devices. Needed for closeusb().
Definition: device.c:10
int uinput_mouse
Definition: structures.h:189
short product
Definition: structures.h:237
#define INDEX_OF(entry, array)
Definition: includes.h:27
short vendor
Definition: structures.h:237
int uinputopen(struct uinput_user_dev *indev, int mouse)
Definition: input_linux.c:9

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void os_keypress ( usbdevice kb,
int  scancode,
int  down 
)

Definition at line 118 of file input_linux.c.

References BTN_WHEELDOWN, BTN_WHEELUP, ckb_warn, isync(), SCAN_MOUSE, usbdevice::uinput_kb, and usbdevice::uinput_mouse.

Referenced by inputupdate_keys(), and play_macro().

118  {
119  struct input_event event;
120  memset(&event, 0, sizeof(event));
121  int is_mouse = 0;
122  if(scancode == BTN_WHEELUP || scancode == BTN_WHEELDOWN){
123  // The mouse wheel is a relative axis
124  if(!down)
125  return;
126  event.type = EV_REL;
127  event.code = REL_WHEEL;
128  event.value = (scancode == BTN_WHEELUP ? 1 : -1);
129  is_mouse = 1;
130  } else {
131  // Mouse buttons and key events are both EV_KEY. The scancodes are already correct, just remove the ckb bit
132  event.type = EV_KEY;
133  event.code = scancode & ~SCAN_MOUSE;
134  event.value = down;
135  is_mouse = !!(scancode & SCAN_MOUSE);
136  }
137  if(write((is_mouse ? kb->uinput_mouse : kb->uinput_kb) - 1, &event, sizeof(event)) <= 0)
138  ckb_warn("uinput write failed: %s\n", strerror(errno));
139  else
140  isync(kb);
141 }
int uinput_kb
Definition: structures.h:189
static void isync(usbdevice *kb)
Definition: input_linux.c:107
#define BTN_WHEELUP
Definition: keymap.h:12
int uinput_mouse
Definition: structures.h:189
#define ckb_warn(fmt, args...)
Definition: includes.h:52
#define SCAN_MOUSE
Definition: keymap.h:58
#define BTN_WHEELDOWN
Definition: keymap.h:13

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void os_mousemove ( usbdevice kb,
int  x,
int  y 
)

Definition at line 143 of file input_linux.c.

References ckb_warn, isync(), usbdevice::uinput_mouse, x, and y.

Referenced by inputupdate(), and play_macro().

143  {
144  struct input_event event;
145  memset(&event, 0, sizeof(event));
146  event.type = EV_REL;
147  if(x != 0){
148  event.code = REL_X;
149  event.value = x;
150  if(write(kb->uinput_mouse - 1, &event, sizeof(event)) <= 0)
151  ckb_warn("uinput write failed: %s\n", strerror(errno));
152  else
153  isync(kb);
154  }
155  if(y != 0){
156  event.code = REL_Y;
157  event.value = y;
158  if(write(kb->uinput_mouse - 1, &event, sizeof(event)) <= 0)
159  ckb_warn("uinput write failed: %s\n", strerror(errno));
160  else
161  isync(kb);
162  }
163 }
float y
Definition: main.c:66
static void isync(usbdevice *kb)
Definition: input_linux.c:107
float x
Definition: main.c:66
int uinput_mouse
Definition: structures.h:189
#define ckb_warn(fmt, args...)
Definition: includes.h:52

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int os_setupindicators ( usbdevice kb)

Definition at line 189 of file input_linux.c.

References _ledthread(), usbdevice::hw_ileds, usbdevice::hw_ileds_old, and usbdevice::ileds.

Referenced by _setupusb().

189  {
190  // Initialize LEDs to all off
191  kb->hw_ileds = kb->hw_ileds_old = kb->ileds = 0;
192  // Create and detach thread to read LED events
193  pthread_t thread;
194  int err = pthread_create(&thread, 0, _ledthread, kb);
195  if(err != 0)
196  return err;
197  pthread_detach(thread);
198  return 0;
199 }
void * _ledthread(void *ctx)
Definition: input_linux.c:165
uchar hw_ileds_old
Definition: structures.h:247
uchar ileds
Definition: structures.h:247
uchar hw_ileds
Definition: structures.h:247

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void updateindicators_kb ( usbdevice kb,
int  force 
)

Definition at line 261 of file input.c.

References usbdevice::active, usbprofile::currentmode, DELAY_SHORT, usbdevice::hw_ileds, usbdevice::hw_ileds_old, I_CAPS, I_NUM, I_SCROLL, usbdevice::ileds, usbmode::inotify, usbmode::ioff, usbmode::ion, nprintind(), os_sendindicators(), OUTFIFO_MAX, and usbdevice::profile.

261  {
262  // Read current hardware indicator state (set externally)
263  uchar old = kb->ileds, hw_old = kb->hw_ileds_old;
264  uchar new = kb->hw_ileds, hw_new = new;
265  // Update them if needed
266  if(kb->active){
267  usbmode* mode = kb->profile->currentmode;
268  new = (new & ~mode->ioff) | mode->ion;
269  }
270  kb->ileds = new;
271  kb->hw_ileds_old = hw_new;
272  if(old != new || force){
273  DELAY_SHORT(kb);
274  os_sendindicators(kb);
275  }
276  // Print notifications if desired
277  if(!kb->active)
278  return;
279  usbmode* mode = kb->profile->currentmode;
280  uchar indicators[] = { I_NUM, I_CAPS, I_SCROLL };
281  for(unsigned i = 0; i < sizeof(indicators) / sizeof(uchar); i++){
282  uchar mask = indicators[i];
283  if((hw_old & mask) == (hw_new & mask))
284  continue;
285  for(int notify = 0; notify < OUTFIFO_MAX; notify++){
286  if(mode->inotify[notify] & mask)
287  nprintind(kb, notify, mask, hw_new & mask);
288  }
289  }
290 }
usbprofile * profile
Definition: structures.h:221
uchar ion
Definition: structures.h:93
usbmode * currentmode
Definition: structures.h:105
#define DELAY_SHORT(kb)
USB delays for when the keyboards get picky about timing That was the original comment, but it is used anytime.
Definition: usb.h:178
uchar hw_ileds_old
Definition: structures.h:247
uchar ileds
Definition: structures.h:247
char active
Definition: structures.h:231
uchar ioff
Definition: structures.h:93
unsigned char uchar
Definition: includes.h:24
void nprintind(usbdevice *kb, int nnumber, int led, int on)
Definition: notify.c:43
#define I_NUM
Definition: structures.h:19
uchar inotify[10]
Definition: structures.h:95
uchar hw_ileds
Definition: structures.h:247
void os_sendindicators(usbdevice *kb)
os_sendindicators update the indicators for the special keys (Numlock, Capslock and what else...
Definition: usb_linux.c:214
#define I_CAPS
Definition: structures.h:20
#define I_SCROLL
Definition: structures.h:21
#define OUTFIFO_MAX
Definition: structures.h:24

+ Here is the call graph for this function: