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 /*
2  *
3  * Copyright (C) 2017 Devon Richards
4  * ckb-heat is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * ckb-heat is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with ckb-heat. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include "../ckb/ckb-anim.h"
18 #include <math.h>
19 
20 void ckb_info(){
21  // Plugin info
22  CKB_NAME("Heat Map");
23  CKB_VERSION("0.2");
24  CKB_COPYRIGHT("2017", "RULER501");
25  CKB_LICENSE("GPLv2");
26  CKB_GUID("{097D69F0-70B2-48B8-AFE2-25A1CDB0D92C}");
27  CKB_DESCRIPTION("A spot effect on pressed keys that shows usage");
28 
29  // Effect parameters
30  CKB_PARAM_AGRADIENT("color", "Fade color:", "", "ffffffff");
31  CKB_PARAM_BOOL("random", "Random Brightness", 0);
32  CKB_PARAM_LONG("ffade", "Frames to fade", "frames", 30, 10, 1000);
33  CKB_PARAM_DOUBLE("pressestofull", "Presses to full", "keypresses", 10.f, 1.f, 100.f);
34 
35  // Timing/input parameters
40 
41  // Presets
42  CKB_PRESET_START("Single Spot");
43  CKB_PRESET_PARAM("random", "0");
44  CKB_PRESET_PARAM("ffade", "30");
45  CKB_PRESET_PARAM("pressestofull", "10");
46  CKB_PRESET_PARAM("trigger", "0");
47  CKB_PRESET_PARAM("kptrigger", "1");
49 }
50 
52 int randomBright = 0;
53 long ffade = 30;
54 double pressestofull = 10.f;
55 
56 typedef struct{
57  unsigned int usages;
58  int pressed;
59  double timing;
60  int x;
61  int y;
62 } keyanim;
63 
64 keyanim* anims = NULL;
65 
66 void ckb_init(ckb_runctx* context){
67  anims = calloc(context->keycount, sizeof(keyanim));
68  for(int i=0; i < context->keycount; i++){
69  anims[i].x = context->keys[i].x;
70  anims[i].y = context->keys[i].y;
71  }
72 }
73 
74 void ckb_parameter(ckb_runctx* context, const char* name, const char* value){
75  CKB_PARSE_AGRADIENT("color", &animcolor){}
76  CKB_PARSE_BOOL("random", &randomBright){}
77  CKB_PARSE_LONG("ffade", &ffade){}
78  CKB_PARSE_DOUBLE("pressestofull", &pressestofull){}
79 }
80 
81 void anim_add(int index){
82  anims[index].usages += ffade;
83  anims[index].pressed = 1;
84  anims[index].timing = 0.f;
85 }
86 
87 void anim_remove(int index){
88  anims[index].pressed = 0;
89 }
90 
91 void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){
92  // Add or remove a spot on this key
93  if(state)
94  anim_add(key - context->keys);
95  else
96  anim_remove(key - context->keys);
97 }
98 
99 void ckb_start(ckb_runctx* context, int state){
100  return;
101 }
102 
103 void ckb_time(ckb_runctx* context, double delta){
104  unsigned int count = context->keycount;
105  for(unsigned int i=0; i < count; i++){
106  if(anims[i].usages && !anims[i].pressed){
107  anims[i].timing -= delta;
108  while(anims[i].timing < 0){
109  anims[i].timing += 1.f/30.f;
110  if(anims[i].usages > 0)
111  anims[i].usages--;
112  }
113  }
114  }
115 }
116 
117 int ckb_frame(ckb_runctx* context){
118  CKB_KEYCLEAR(context);
119  // Draw spots
120  unsigned count = context->keycount;
121  ckb_key* keys = context->keys;
122  unsigned int full = pressestofull*ffade;
123  for(int i =0; i < count; i++){
124  if(!anims[i].usages)
125  continue;
126  float a, r, g, b;
127  if(randomBright)
128  ckb_grad_color(&a, &r, &g, &b, &animcolor, ((float)(rand() % 1024))/10.f);
129  else
130  ckb_grad_color(&a, &r, &g, &b, &animcolor, (float)fmin(anims[i].usages,full)*100.f/full);
131  ckb_alpha_blend(keys+i, a, r, g, b);
132  }
133  return 0;
134 }
#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
#define CKB_PARAM_DOUBLE(name, prefix, postfix, default, min, max)
Definition: ckb-anim.h:75
int x
Definition: ckb-anim.h:133
#define CKB_VERSION(version)
Definition: ckb-anim.h:64
#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
double timing
Definition: main.c:59
#define CKB_NAME(name)
Definition: ckb-anim.h:62
#define CKB_PARAM_LONG(name, prefix, postfix, default, min, max)
Definition: ckb-anim.h:74
float x
Definition: main.c:66
ckb_gradient animcolor
Definition: main.c:52
Definition: main.c:56
#define CKB_PARSE_AGRADIENT(param_name, gradient_ptr)
Definition: ckb-anim.h:121
#define CKB_TIME_ABSOLUTE
Definition: ckb-anim.h:96
int randomBright
Definition: main.c:52
#define CKB_LICENSE(license)
Definition: ckb-anim.h:68
#define CKB_KP_POSITION
Definition: ckb-anim.h:92
keyanim * anims
Definition: main.c:64
#define CKB_COPYRIGHT(year, author)
Definition: ckb-anim.h:66
Definition: keymap.h:49
#define CKB_DESCRIPTION(description)
Definition: ckb-anim.h:70
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
#define CKB_KPMODE(mode)
Definition: ckb-anim.h:93
int pressed
Definition: main.c:58
#define CKB_PARAM_AGRADIENT(name, prefix, postfix, default)
Definition: ckb-anim.h:80
#define CKB_LIVEPARAMS(enable)
Definition: ckb-anim.h:104
void anim_remove(int index)
Definition: main.c:87
int y
Definition: ckb-anim.h:133
int x
Definition: main.c:60
void ckb_info()
Definition: main.c:5
double pressestofull
Definition: main.c:54
#define CKB_PARSE_LONG(param_name, value_ptr)
Definition: ckb-anim.h:115
#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
void anim_add(int index)
Definition: main.c:81
#define CKB_KEYCLEAR(context)
Definition: ckb-anim.h:148
long ffade
Definition: main.c:53
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_PARSE_DOUBLE(param_name, value_ptr)
Definition: ckb-anim.h:116
unsigned int usages
Definition: main.c:57
#define CKB_PARAM_BOOL(name, text, default)
Definition: ckb-anim.h:76
void ckb_alpha_blend(ckb_key *key, float a, float r, float g, float b)
Definition: ckb-anim.h:283
int y
Definition: main.c:61
void ckb_grad_color(float *a, float *r, float *g, float *b, const ckb_gradient *grad, float pos)
Definition: ckb-anim.h:254