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 <stdlib.h>
4 #include <time.h>
5 
6 void ckb_info(){
7  // Plugin info
8  CKB_NAME("Random");
9  CKB_VERSION("0.9");
10  CKB_COPYRIGHT("2014-2016", "MSC");
11  CKB_LICENSE("GPLv2");
12  CKB_GUID("{22418DA4-A181-4B93-A4D3-03682BA283D2}");
13  CKB_DESCRIPTION("An effect that changes key colors randomly.");
14 
15  // Effect parameters
16  CKB_PARAM_BOOL("fade", "Fade in", 0);
17  CKB_PARAM_BOOL("useopacity", "Randomize opacity", 0);
18 
19  // Timing/input parameters
24 
25  // Presets
26  CKB_PRESET_START("Default");
27  CKB_PRESET_PARAM("duration", "1.");
29 }
30 
31 int fadein = 0, useopacity = 0;
32 
33 void ckb_parameter(ckb_runctx* context, const char* name, const char* value){
34  CKB_PARSE_BOOL("fade", &fadein) {}
35  CKB_PARSE_BOOL("useopacity", &useopacity) {}
36 }
37 
38 void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){
39  // Unused
40 }
41 
42 typedef struct {
43  unsigned char a, r, g, b;
44 } rgb;
45 
46 rgb* current = 0;
47 rgb* target = 0;
48 double phase = -1.;
49 
50 // Make a new target color for each key
51 void newtarget(rgb* data, unsigned count){
52  for(unsigned i = 0; i < count; i++){
53  rgb* key = data + i;
54  key->a = useopacity ? rand() % 256 : 255;
55  key->r = rand() % 256;
56  key->g = rand() % 256;
57  key->b = rand() % 256;
58  }
59 }
60 
61 void ckb_init(ckb_runctx* context){
62  unsigned count = context->keycount;
63  current = malloc(count * sizeof(rgb));
64  target = malloc(count * sizeof(rgb));
65  srand(time(NULL));
66 }
67 
68 void ckb_start(ckb_runctx* context, int state){
69  if(state == 0){
70  // Stop animation
71  phase = -1.;
72  return;
73  }
74  // Start animation
75  phase = 0.;
76  ckb_key* keys = context->keys;
77  unsigned count = context->keycount;
78  // Randomize starting colors and pick a random target
79  newtarget(current, count);
80  // Over the course of the animation, the keys fade between the current pattern and the target pattern
81  newtarget(target, count);
82  // Set all keys to current
83  for(unsigned i = 0; i < count; i++){
84  ckb_key* key = keys + i;
85  if(fadein)
86  current[i].a = 0;
87  key->a = current[i].a;
88  key->r = current[i].r;
89  key->g = current[i].g;
90  key->b = current[i].b;
91  }
92 }
93 
94 void ckb_time(ckb_runctx* context, double delta){
95  if(phase < 0.)
96  return;
97  // Advance animation
98  phase += delta;
99  if(phase > 1.){
100  // If the animation is complete, pick a new target pattern and start again
101  phase -= 1.;
102  rgb* temp = target;
103  target = current;
104  current = temp;
105  newtarget(target, context->keycount);
106  }
107 }
108 
109 int ckb_frame(ckb_runctx* context){
110  if(phase < 0.){
111  CKB_KEYCLEAR(context);
112  return 0;
113  }
114  ckb_key* keys = context->keys;
115  unsigned count = context->keycount;
116  for(unsigned i = 0; i < count; i++){
117  // Color each key according to the position between the last color set and the new color set
118  ckb_key* key = keys + i;
119  key->a = round(current[i].a * (1. - phase) + target[i].a * phase);
120  key->r = round(current[i].r * (1. - phase) + target[i].r * phase);
121  key->g = round(current[i].g * (1. - phase) + target[i].g * phase);
122  key->b = round(current[i].b * (1. - phase) + target[i].b * phase);
123  }
124  return 0;
125 }
#define CKB_PRESET_END
Definition: ckb-anim.h:87
#define FALSE
Definition: ckb-anim.h:49
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
Definition: main.c:43
rgb * current
Definition: main.c:46
#define CKB_VERSION(version)
Definition: ckb-anim.h:64
unsigned char r
Definition: main.c:43
#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
#define CKB_NAME(name)
Definition: ckb-anim.h:62
int useopacity
Definition: main.c:31
#define CKB_KP_NONE
Definition: ckb-anim.h:90
float x
Definition: main.c:66
rgb * target
Definition: main.c:47
unsigned char b
Definition: ckb-anim.h:134
unsigned char r
Definition: ckb-anim.h:134
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
int fadein
Definition: main.c:31
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
Definition: main.c:42
void ckb_time(ckb_runctx *context, double delta)
Definition: main.c:126
#define CKB_KPMODE(mode)
Definition: ckb-anim.h:93
void newtarget(rgb *data, unsigned count)
Definition: main.c:51
#define CKB_LIVEPARAMS(enable)
Definition: ckb-anim.h:104
unsigned char a
Definition: main.c:43
#define CKB_TIME_DURATION
Definition: ckb-anim.h:95
void ckb_info()
Definition: main.c:5
#define CKB_PARSE_BOOL(param_name, value_ptr)
Definition: ckb-anim.h:117
void ckb_init(ckb_runctx *context)
Definition: main.c:57
#define CKB_REPEAT(enable)
Definition: ckb-anim.h:99
#define CKB_KEYCLEAR(context)
Definition: ckb-anim.h:148
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
#define CKB_PARAM_BOOL(name, text, default)
Definition: ckb-anim.h:76
unsigned char g
Definition: main.c:43