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
main.c
Go to the documentation of this file.
1 #include "../ckb/ckb-anim.h"
2 #include <math.h>
3 #include <time.h>
4 #include <stdio.h>
5 void ckb_info(){
6  // Plugin info
7  CKB_NAME("Gradient");
8  CKB_VERSION("0.9");
9  CKB_COPYRIGHT("2014-2016", "MSC");
10  CKB_LICENSE("GPLv2");
11  CKB_GUID("{54DD2975-E192-457D-BCFC-D912A24E33B4}");
12  CKB_DESCRIPTION("A transition from one color to another.");
13 
14  // Effect parameters
15  CKB_PARAM_AGRADIENT("color", "Color:", "", "ffffffff");
16  CKB_PARAM_BOOL("kphold", "Freeze until key is released", TRUE);
17  CKB_PARAM_BOOL("randomize", "Randomly select from gradient", 0);
18 
19  // Timing/input parameters
23 
24  // Presets
25  CKB_PRESET_START("Fade in");
26  CKB_PRESET_PARAM("color", "ff000000");
27  CKB_PRESET_PARAM("duration", "1.0");
28  CKB_PRESET_PARAM("stop", "0");
29  CKB_PRESET_PARAM("kpstop", "0");
31 
32  CKB_PRESET_START("Rainbow");
33  CKB_PRESET_PARAM("color", "0:ffff0000 17:ffffff00 33:ff00ff00 50:ff00ffff 67:ff0000ff 83:ffff00ff 100:ffff0000");
34  CKB_PRESET_PARAM("duration", "2.0");
36 
37  CKB_PRESET_START("Type lighting");
38  CKB_PRESET_PARAM("duration", "1.0");
39  CKB_PRESET_PARAM("trigger", "0");
40  CKB_PRESET_PARAM("kptrigger", "1");
42 }
43 
44 #define NONE -1.f
45 #define HOLD -2.f
46 
47 struct keyAnim {
48  float target;
50 };
51 
53 int kphold = 0, kprelease = 0, randomize = 0;
54 
55 struct keyAnim* anim;
56 
57 void ckb_init(ckb_runctx* context){
58  // Initialize all keys to 100% (animation over)
59 
60  srand((unsigned)time(NULL));
61  unsigned count = context->keycount;
62  anim = malloc(count * sizeof *anim);
63  for(unsigned i = 0; i < count; i++) {
64  anim[i].target = NONE;
65  }
66 }
67 
68 void ckb_parameter(ckb_runctx* context, const char* name, const char* value){
69  CKB_PARSE_AGRADIENT("color", &animcolor){}
70  CKB_PARSE_BOOL("kphold", &kphold){}
71  CKB_PARSE_BOOL("kprelease", &kprelease){}
72  CKB_PARSE_BOOL("randomize", &randomize){}
73 }
74 
75 void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){
76  // Start or stop animation on key
77  int i = key - context->keys;
78  if(state){
79  if (randomize) {
80  float a, r, g, b;
81  float colorChoice = (float)rand()/(float)(RAND_MAX);
82  ckb_grad_color(&a, &r, &g, &b, &animcolor, colorChoice * 100.);
83  ckb_gradient newGradient = { 0 };
84  newGradient.ptcount = 2;
85  newGradient.pts[0] = 0;
86  newGradient.pts[1] = 100;
87  newGradient.a[0] = 255;
88  newGradient.b[0] = b;
89  newGradient.g[0] = g;
90  newGradient.r[0] = r;
91  newGradient.a[1] = 0;
92  newGradient.b[1] = b;
93  newGradient.g[1] = g;
94  newGradient.r[1] = r;
95 
96  anim[i].gradient = newGradient;
97  }
98 
99  if(kphold) {
100  anim[i].target = HOLD;
101  } else {
102  anim[i].target = 0.f;
103  }
104  } else {
105  if(kprelease) {
106  anim[i].target = NONE;
107  }
108  else if(kphold) {
109  anim[i].target = 0.f;
110  }
111  }
112 }
113 
114 void ckb_start(ckb_runctx* context, int state){
115  // Start/stop all keys
116  unsigned count = context->keycount;
117  if(state) {
118  memset(anim, 0, count * sizeof *anim);
119  } else {
120  for(unsigned i = 0; i < count; i++) {
121  anim[i].target = NONE;
122  }
123  }
124 }
125 
126 void ckb_time(ckb_runctx* context, double delta){
127  // Advance animation on each key
128  unsigned count = context->keycount;
129  for(unsigned i = 0; i < count; i++){
130  float phase = anim[i].target;
131  if(phase > 1.f || phase < 0.f)
132  continue;
133  anim[i].target = phase + delta;
134  }
135 }
136 
137 int ckb_frame(ckb_runctx* context){
138  // Draw key colors
139  unsigned count = context->keycount;
140  for(unsigned i = 0; i < count; i++){
141  float phase = anim[i].target;
142 
143  if(phase == HOLD)
144  phase = 0.f;
145  else if(phase < 0.f)
146  phase = 1.f;
147  ckb_key* key = context->keys + i;
148  float a, r, g, b;
149  ckb_gradient thisGradient;
150  if (randomize) {
151  thisGradient = anim[i].gradient;
152  } else {
153  thisGradient = animcolor;
154  }
155  ckb_grad_color(&a, &r, &g, &b, &thisGradient, phase * 100.);
156  key->a = a;
157  key->r = r;
158  key->g = g;
159  key->b = b;
160  }
161  return 0;
162 }
#define CKB_PRESET_END
Definition: ckb-anim.h:87
void ckb_keypress(ckb_runctx *context, ckb_key *key, int x, int y, int state)
Definition: main.c:75
void ckb_parameter(ckb_runctx *context, const char *name, const char *value)
Definition: main.c:68
unsigned char b[100]
Definition: ckb-anim.h:162
#define CKB_VERSION(version)
Definition: ckb-anim.h:64
char pts[100]
Definition: ckb-anim.h:157
#define CKB_KP_NAME
Definition: ckb-anim.h:91
#define CKB_TIMEMODE(mode)
Definition: ckb-anim.h:97
float y
Definition: main.c:66
#define TRUE
Definition: ckb-anim.h:46
#define CKB_GUID(guid)
Definition: ckb-anim.h:60
unsigned char g
Definition: ckb-anim.h:134
ckb_gradient gradient
Definition: main.c:49
#define CKB_NAME(name)
Definition: ckb-anim.h:62
unsigned char g[100]
Definition: ckb-anim.h:161
float x
Definition: main.c:66
ckb_gradient animcolor
Definition: main.c:52
unsigned char b
Definition: ckb-anim.h:134
unsigned char r
Definition: ckb-anim.h:134
#define CKB_PARSE_AGRADIENT(param_name, gradient_ptr)
Definition: ckb-anim.h:121
unsigned char a
Definition: ckb-anim.h:134
#define CKB_LICENSE(license)
Definition: ckb-anim.h:68
#define CKB_COPYRIGHT(year, author)
Definition: ckb-anim.h:66
Definition: keymap.h:49
#define CKB_DESCRIPTION(description)
Definition: ckb-anim.h:70
double phase
Definition: main.c:48
int ckb_frame(ckb_runctx *context)
Definition: main.c:137
unsigned keycount
Definition: ckb-anim.h:141
#define CKB_PRESET_START(name)
Definition: ckb-anim.h:85
void ckb_time(ckb_runctx *context, double delta)
Definition: main.c:126
Definition: main.c:47
#define CKB_KPMODE(mode)
Definition: ckb-anim.h:93
unsigned char r[100]
Definition: ckb-anim.h:160
int randomize
Definition: main.c:53
#define CKB_PARAM_AGRADIENT(name, prefix, postfix, default)
Definition: ckb-anim.h:80
#define CKB_LIVEPARAMS(enable)
Definition: ckb-anim.h:104
unsigned char a[100]
Definition: ckb-anim.h:159
int kphold
Definition: main.c:53
#define NONE
Definition: main.c:44
#define CKB_TIME_DURATION
Definition: ckb-anim.h:95
void ckb_info()
Definition: main.c:5
float target
Definition: main.c:48
#define CKB_PARSE_BOOL(param_name, value_ptr)
Definition: ckb-anim.h:117
void ckb_init(ckb_runctx *context)
Definition: main.c:57
void ckb_start(ckb_runctx *context, int state)
Definition: main.c:114
#define CKB_PRESET_PARAM(name, value)
Definition: ckb-anim.h:86
ckb_key * keys
Definition: ckb-anim.h:140
int kprelease
Definition: main.c:53
#define HOLD
Definition: main.c:45
#define CKB_PARAM_BOOL(name, text, default)
Definition: ckb-anim.h:76
void ckb_grad_color(float *a, float *r, float *g, float *b, const ckb_gradient *grad, float pos)
Definition: ckb-anim.h:254
struct keyAnim * anim
Definition: main.c:55