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
notify.c
Go to the documentation of this file.
1 #include "device.h"
2 #include "devnode.h"
3 #include "dpi.h"
4 #include "led.h"
5 #include "notify.h"
6 #include "profile.h"
7 
8 void nprintf(usbdevice* kb, int nodenumber, usbmode* mode, const char* format, ...){
9  if(!kb)
10  return;
11  usbprofile* profile = kb->profile;
12  va_list va_args;
13  int fifo;
14  if(nodenumber >= 0){
15  // If node number was given, print to that node (if open)
16  if((fifo = kb->outfifo[nodenumber] - 1) != -1){
17  va_start(va_args, format);
18  if(mode)
19  dprintf(fifo, "mode %d ", INDEX_OF(mode, profile->mode) + 1);
20  vdprintf(fifo, format, va_args);
21  }
22  return;
23  }
24  // Otherwise, print to all nodes
25  for(int i = 0; i < OUTFIFO_MAX; i++){
26  if((fifo = kb->outfifo[i] - 1) != -1){
27  va_start(va_args, format);
28  if(mode)
29  dprintf(fifo, "mode %d ", INDEX_OF(mode, profile->mode) + 1);
30  vdprintf(fifo, format, va_args);
31  }
32  }
33 }
34 
35 void nprintkey(usbdevice* kb, int nnumber, int keyindex, int down){
36  const key* map = keymap + keyindex;
37  if(map->name)
38  nprintf(kb, nnumber, 0, "key %c%s\n", down ? '+' : '-', map->name);
39  else
40  nprintf(kb, nnumber, 0, "key %c#%d\n", down ? '+' : '-', keyindex);
41 }
42 
43 void nprintind(usbdevice* kb, int nnumber, int led, int on){
44  const char* name = 0;
45  switch(led){
46  case I_NUM:
47  name = "num";
48  break;
49  case I_CAPS:
50  name = "caps";
51  break;
52  case I_SCROLL:
53  name = "scroll";
54  break;
55  default:
56  return;
57  }
58  nprintf(kb, nnumber, 0, "i %c%s\n", on ? '+' : '-', name);
59 }
60 
61 void cmd_notify(usbdevice* kb, usbmode* mode, int nnumber, int keyindex, const char* toggle){
62  if(keyindex >= N_KEYS_INPUT)
63  return;
64  pthread_mutex_lock(imutex(kb));
65  if(!strcmp(toggle, "on") || *toggle == 0)
66  SET_KEYBIT(mode->notify[nnumber], keyindex);
67  else if(!strcmp(toggle, "off"))
68  CLEAR_KEYBIT(mode->notify[nnumber], keyindex);
69  pthread_mutex_unlock(imutex(kb));
70 }
71 
72 // Check hardware mode, bail out if it doesn't exist
73 #define HWMODE_OR_RETURN(kb, index) \
74  if(IS_K95(kb)){ \
75  if((index) >= HWMODE_K95) \
76  return; \
77  } else { \
78  if((index) >= HWMODE_K70) \
79  return; \
80  }
81 
82 // Standard check for hardware variables: get index, bail if index is too high or no HW profile
83 #define HW_STANDARD \
84  if(!kb->hw) \
85  return; \
86  unsigned index = INDEX_OF(mode, profile->mode); \
87  /* Make sure the mode number is valid */ \
88  HWMODE_OR_RETURN(kb, index)
89 
90 static void _cmd_get(usbdevice* kb, usbmode* mode, int nnumber, const char* setting){
91  usbprofile* profile = kb->profile;
92  if(!strcmp(setting, ":mode")){
93  // Get the current mode number
94  nprintf(kb, nnumber, mode, "switch\n");
95  return;
96  } else if(!strcmp(setting, ":rgb")){
97  // Get the current RGB settings
98  char* rgb = printrgb(&mode->light, kb);
99  nprintf(kb, nnumber, mode, "rgb %s\n", rgb);
100  free(rgb);
101  return;
102  } else if(!strcmp(setting, ":hwrgb")){
103  // Get the current hardware RGB settings
104  HW_STANDARD;
105  char* rgb = printrgb(kb->hw->light + index, kb);
106  nprintf(kb, nnumber, mode, "hwrgb %s\n", rgb);
107  free(rgb);
108  return;
109  } else if(!strcmp(setting, ":profilename")){
110  // Get the current profile name
111  char* name = getprofilename(profile);
112  nprintf(kb, nnumber, 0, "profilename %s\n", name[0] ? name : "Unnamed");
113  free(name);
114  } else if(!strcmp(setting, ":name")){
115  // Get the current mode name
116  char* name = getmodename(mode);
117  nprintf(kb, nnumber, mode, "name %s\n", name[0] ? name : "Unnamed");
118  free(name);
119  } else if(!strcmp(setting, ":hwprofilename")){
120  // Get the current hardware profile name
121  if(!kb->hw)
122  return;
123  char* name = gethwprofilename(kb->hw);
124  nprintf(kb, nnumber, 0, "hwprofilename %s\n", name[0] ? name : "Unnamed");
125  free(name);
126  } else if(!strcmp(setting, ":hwname")){
127  // Get the current hardware mode name
128  HW_STANDARD;
129  char* name = gethwmodename(kb->hw, index);
130  nprintf(kb, nnumber, mode, "hwname %s\n", name[0] ? name : "Unnamed");
131  free(name);
132  } else if(!strcmp(setting, ":profileid")){
133  // Get the current profile ID
134  char* guid = getid(&profile->id);
135  int modified;
136  memcpy(&modified, &profile->id.modified, sizeof(modified));
137  nprintf(kb, nnumber, 0, "profileid %s %x\n", guid, modified);
138  free(guid);
139  } else if(!strcmp(setting, ":id")){
140  // Get the current mode ID
141  char* guid = getid(&mode->id);
142  int modified;
143  memcpy(&modified, &mode->id.modified, sizeof(modified));
144  nprintf(kb, nnumber, mode, "id %s %x\n", guid, modified);
145  free(guid);
146  } else if(!strcmp(setting, ":hwprofileid")){
147  // Get the current hardware profile ID
148  if(!kb->hw)
149  return;
150  char* guid = getid(&kb->hw->id[0]);
151  int modified;
152  memcpy(&modified, &kb->hw->id[0].modified, sizeof(modified));
153  nprintf(kb, nnumber, 0, "hwprofileid %s %x\n", guid, modified);
154  free(guid);
155  } else if(!strcmp(setting, ":hwid")){
156  // Get the current hardware mode ID
157  HW_STANDARD;
158  char* guid = getid(&kb->hw->id[index + 1]);
159  int modified;
160  memcpy(&modified, &kb->hw->id[index + 1].modified, sizeof(modified));
161  nprintf(kb, nnumber, mode, "hwid %s %x\n", guid, modified);
162  free(guid);
163  } else if(!strcmp(setting, ":keys")){
164  // Get the current state of all keys
165  for(int i = 0; i < N_KEYS_INPUT; i++){
166  if(!keymap[i].name)
167  continue;
168  int byte = i / 8, bit = 1 << (i & 7);
169  uchar state = kb->input.keys[byte] & bit;
170  if(state)
171  nprintkey(kb, nnumber, i, 1);
172  }
173  } else if(!strcmp(setting, ":i")){
174  // Get the current state of all indicator LEDs
175  if(kb->hw_ileds & I_NUM) nprintind(kb, nnumber, I_NUM, 1);
176  if(kb->hw_ileds & I_CAPS) nprintind(kb, nnumber, I_CAPS, 1);
177  if(kb->hw_ileds & I_SCROLL) nprintind(kb, nnumber, I_SCROLL, 1);
178  } else if(!strcmp(setting, ":dpi")){
179  // Get the current DPI levels
180  char* dpi = printdpi(&mode->dpi, kb);
181  nprintf(kb, nnumber, mode, "dpi %s\n", dpi);
182  free(dpi);
183  return;
184  } else if(!strcmp(setting, ":hwdpi")){
185  // Get the current hardware DPI levels
186  HW_STANDARD;
187  char* dpi = printdpi(kb->hw->dpi + index, kb);
188  nprintf(kb, nnumber, mode, "hwdpi %s\n", dpi);
189  free(dpi);
190  return;
191  } else if(!strcmp(setting, ":dpisel")){
192  // Get the currently-selected DPI
193  nprintf(kb, nnumber, mode, "dpisel %d\n", mode->dpi.current);
194  } else if(!strcmp(setting, ":hwdpisel")){
195  // Get the currently-selected hardware DPI
196  HW_STANDARD;
197  nprintf(kb, nnumber, mode, "hwdpisel %d\n", kb->hw->dpi[index].current);
198  } else if(!strcmp(setting, ":lift")){
199  // Get the mouse lift height
200  nprintf(kb, nnumber, mode, "lift %d\n", mode->dpi.lift);
201  } else if(!strcmp(setting, ":hwlift")){
202  // Get the hardware lift height
203  HW_STANDARD;
204  nprintf(kb, nnumber, mode, "hwlift %d\n", kb->hw->dpi[index].lift);
205  } else if(!strcmp(setting, ":snap")){
206  // Get the angle snap status
207  nprintf(kb, nnumber, mode, "snap %s\n", mode->dpi.snap ? "on" : "off");
208  } else if(!strcmp(setting, ":hwsnap")){
209  // Get the hardware angle snap status
210  HW_STANDARD;
211  nprintf(kb, nnumber, mode, "hwsnap %s\n", kb->hw->dpi[index].snap ? "on" : "off");
212  }
213 }
214 
215 void cmd_get(usbdevice* kb, usbmode* mode, int nnumber, int dummy, const char* setting){
216  (void)dummy;
217 
218  pthread_mutex_lock(imutex(kb));
219  _cmd_get(kb, mode, nnumber, setting);
220  pthread_mutex_unlock(imutex(kb));
221 }
222 
223 extern int restart();
224 
225 void cmd_restart(usbdevice* kb, usbmode* mode, int nnumber, int dummy, const char* content) {
226  (void)mode;
227  (void)nnumber;
228  (void)dummy;
229 
230  ckb_info("RESTART called with %s\n", content);
231  nprintf(kb, -1, 0, "RESTART called with %s\n", content);
232  restart();
233 }
void nprintf(usbdevice *kb, int nodenumber, usbmode *mode, const char *format,...)
Definition: notify.c:8
usbprofile * profile
Definition: structures.h:221
char * getprofilename(usbprofile *profile)
Definition: profile.c:156
usbinput input
Definition: structures.h:245
#define CLEAR_KEYBIT(array, index)
Definition: structures.h:16
char * getmodename(usbmode *mode)
Definition: profile.c:152
const key keymap[(((152+22+12)+25)+12)]
Definition: keymap.c:5
usbmode mode[6]
Definition: structures.h:103
static void _cmd_get(usbdevice *kb, usbmode *mode, int nnumber, const char *setting)
Definition: notify.c:90
char modified[4]
Definition: structures.h:10
usbid id
Definition: structures.h:111
Definition: keymap.h:49
lighting light
Definition: structures.h:84
usbid id[3+1]
Definition: structures.h:123
dpiset dpi
Definition: structures.h:86
char * gethwprofilename(hwprofile *profile)
Definition: profile.c:164
#define SET_KEYBIT(array, index)
Definition: structures.h:15
char * gethwmodename(hwprofile *profile, int index)
Definition: profile.c:160
unsigned char uchar
Definition: includes.h:24
void nprintkey(usbdevice *kb, int nnumber, int keyindex, int down)
Definition: notify.c:35
#define HW_STANDARD
Definition: notify.c:83
Definition: main.c:42
void nprintind(usbdevice *kb, int nnumber, int led, int on)
Definition: notify.c:43
int outfifo[10]
Definition: structures.h:227
uchar snap
Definition: structures.h:67
void cmd_notify(usbdevice *kb, usbmode *mode, int nnumber, int keyindex, const char *toggle)
Definition: notify.c:61
#define ckb_info(fmt, args...)
Definition: includes.h:55
char * getid(usbid *id)
Definition: profile.c:79
const char * name
Definition: keymap.h:50
uchar lift
Definition: structures.h:65
#define INDEX_OF(entry, array)
Definition: includes.h:27
dpiset dpi[3]
Definition: structures.h:121
void cmd_get(usbdevice *kb, usbmode *mode, int nnumber, int dummy, const char *setting)
Definition: notify.c:215
#define I_NUM
Definition: structures.h:19
char * printrgb(const lighting *light, const usbdevice *kb)
Definition: led.c:120
int restart()
Definition: main.c:228
#define imutex(kb)
Definition: device.h:22
void cmd_restart(usbdevice *kb, usbmode *mode, int nnumber, int dummy, const char *content)
Definition: notify.c:225
hwprofile * hw
Definition: structures.h:223
#define N_KEYS_INPUT
Definition: keymap.h:36
uchar current
Definition: structures.h:61
uchar notify[10][((((152+22+12)+25)+7)/8)]
Definition: structures.h:91
char * printdpi(const dpiset *dpi, const usbdevice *kb)
Definition: dpi.c:84
uchar hw_ileds
Definition: structures.h:247
uchar keys[((((152+22+12)+25)+7)/8)]
Definition: structures.h:130
lighting light[3]
Definition: structures.h:120
#define I_CAPS
Definition: structures.h:20
#define I_SCROLL
Definition: structures.h:21
usbid id
Definition: structures.h:88
#define OUTFIFO_MAX
Definition: structures.h:24