42 #define M_PI 3.14159265358979323846
57 #define CKB_CONTAINER(macro) do { macro } while(0)
60 #define CKB_GUID(guid) CKB_CONTAINER( printf("guid "); printurl(guid); printf("\n"); )
62 #define CKB_NAME(name) CKB_CONTAINER( printf("name "); printurl(name); printf("\n"); )
64 #define CKB_VERSION(version) CKB_CONTAINER( printf("version "); printurl(version); printf("\n"); )
66 #define CKB_COPYRIGHT(year, author) CKB_CONTAINER( printf("author "); printurl(author); printf("\nyear %s\n", year); )
68 #define CKB_LICENSE(license) CKB_CONTAINER( printf("license "); printurl(license); printf("\n"); )
70 #define CKB_DESCRIPTION(description) CKB_CONTAINER( printf("description "); printurl(description); printf("\n"); )
73 #define CKB_PARAM(type, name, prefix, postfix, extra) CKB_CONTAINER( printf("param %s %s ", type, name); printurl(prefix); printf(" "); printurl(postfix); printf(" "); extra; printf("\n"); )
74 #define CKB_PARAM_LONG(name, prefix, postfix, default, min, max) CKB_PARAM("long", name, prefix, postfix, printf("%ld %ld %ld", (long)(default), (long)(min), (long)(max)))
75 #define CKB_PARAM_DOUBLE(name, prefix, postfix, default, min, max) CKB_PARAM("double", name, prefix, postfix, printf("%lf %lf %lf", (double)(default), (double)(min), (double)(max)))
76 #define CKB_PARAM_BOOL(name, text, default) CKB_PARAM("bool", name, text, "", printf((default) ? "1" : "0"))
77 #define CKB_PARAM_RGB(name, prefix, postfix, r, g, b) CKB_PARAM("rgb", name, prefix, postfix, printf("%02x%02x%02x", (unsigned char)(r), (unsigned char)(g), (unsigned char)(b)))
78 #define CKB_PARAM_ARGB(name, prefix, postfix, a, r, g, b) CKB_PARAM("argb", name, prefix, postfix, printf("%02x%02x%02x%02x", (unsigned char)(a), (unsigned char)(r), (unsigned char)(g), (unsigned char)(b)))
79 #define CKB_PARAM_GRADIENT(name, prefix, postfix, default) CKB_PARAM("gradient", name, prefix, postfix, printurl(default))
80 #define CKB_PARAM_AGRADIENT(name, prefix, postfix, default) CKB_PARAM("agradient", name, prefix, postfix, printurl(default))
81 #define CKB_PARAM_ANGLE(name, prefix, postfix, default) CKB_PARAM("angle", name, prefix, postfix, printf("%ld", (long)(default)))
82 #define CKB_PARAM_STRING(name, prefix, postfix, default) CKB_PARAM("string", name, prefix, postfix, printurl(default))
83 #define CKB_PARAM_LABEL(name, text) CKB_PARAM("label", name, text, "", )
85 #define CKB_PRESET_START(name) CKB_CONTAINER( printf("preset "); printurl(name); )
86 #define CKB_PRESET_PARAM(name, value) CKB_CONTAINER( printf(" %s=", name); printurl(value); )
87 #define CKB_PRESET_END CKB_CONTAINER( printf("\n"); )
90 #define CKB_KP_NONE "none"
91 #define CKB_KP_NAME "name"
92 #define CKB_KP_POSITION "position"
93 #define CKB_KPMODE(mode) CKB_CONTAINER( printf("kpmode %s\n", mode); )
95 #define CKB_TIME_DURATION "duration"
96 #define CKB_TIME_ABSOLUTE "absolute"
97 #define CKB_TIMEMODE(mode) CKB_CONTAINER( printf("time %s\n", mode); )
99 #define CKB_REPEAT(enable) CKB_CONTAINER( printf("repeat %s\n", (enable) ? "on" : "off"); )
102 #define CKB_PREEMPT(enable) CKB_CONTAINER( printf("preempt %s\n", (enable) ? "on" : "off"); )
104 #define CKB_LIVEPARAMS(enable) CKB_CONTAINER( printf("parammode %s\n", (enable) ? "live" : "static"); )
115 #define CKB_PARSE_LONG(param_name, value_ptr) if(!strcmp(name, param_name) && sscanf(value, "%ld", value_ptr) == 1)
116 #define CKB_PARSE_DOUBLE(param_name, value_ptr) if(!strcmp(name, param_name) && sscanf(value, "%lf", value_ptr) == 1)
117 #define CKB_PARSE_BOOL(param_name, value_ptr) if(!strcmp(name, param_name) && sscanf(value, "%u", value_ptr) == 1)
118 #define CKB_PARSE_RGB(param_name, r_ptr, g_ptr, b_ptr) if(!strcmp(name, param_name) && sscanf(value, "%2hhx%2hhx%2hhx", r_ptr, g_ptr, b_ptr) == 3)
119 #define CKB_PARSE_ARGB(param_name, a_ptr, r_ptr, g_ptr, b_ptr) if(!strcmp(name, param_name) && sscanf(value, "%2hhx%2hhx%2hhx%2hhx", a_ptr, r_ptr, g_ptr, b_ptr) == 4)
120 #define CKB_PARSE_GRADIENT(param_name, gradient_ptr) if(!strcmp(name, param_name) && ckb_scan_grad(value, gradient_ptr, 0))
121 #define CKB_PARSE_AGRADIENT(param_name, gradient_ptr) if(!strcmp(name, param_name) && ckb_scan_grad(value, gradient_ptr, 1))
122 #define CKB_PARSE_ANGLE(param_name, value_ptr) if(!strcmp(name, param_name) && sscanf(value, "%ld", value_ptr) == 1)
123 #define CKB_PARSE_STRING(param_name) if(!strcmp(name, param_name))
127 #define CKB_REAL_ANGLE(angle) fmod((-(angle) + 90.) * M_PI / 180. + M_PI * 2., M_PI * 2.)
130 #define CKB_KEYNAME_MAX 12
134 unsigned char a,
r, g, b;
148 #define CKB_KEYCLEAR(context) CKB_CONTAINER( ckb_key* key = context->keys; unsigned count = context->keycount; unsigned i = 0; for(; i < count; i++) key[i].a = key[i].r = key[i].g = key[i].b = 0; )
151 #define CKB_GRAD_MAX 100
179 char out[strlen(src) * 3 + 1];
183 if(s <=
',' || s ==
'/' ||
184 (s >=
':' && s <=
'@') ||
185 s ==
'[' || s ==
']' ||
187 char a = s >> 4, b = s & 0xF;
213 ((a = src[1]) && (b = src[2])) &&
214 (isxdigit(a) && isxdigit(b))){
238 #define CKB_MAX_WORD (4 * 1024)
240 char line[CKB_MAX_WORD * 3 + 3];
241 fgets(line,
sizeof(line), stdin);
242 word1[0] = word1[1] = word1[2] = 0;
243 int res = sscanf(line,
"%s %s %s", word1, word2, word3);
250 line[strlen(line) - 1] = 0;
258 *a = *r = *g = *b = 0.f;
262 for(; i < count; i++){
263 if(grad->
pts[i] >= pos)
268 float distance = grad->
pts[i] - grad->
pts[i - 1];
269 float dx = (pos - grad->
pts[i - 1]) / distance;
270 float a1 = grad->
a[i] / 255., a2 = grad->
a[i - 1] / 255.;
271 float a3 = *a = a1 * dx + a2 * (1.f - dx);
273 *a = *r = *g = *b = 0.f;
277 *r = (grad->
r[i] * a1 * dx + grad->
r[i - 1] * a2 * (1.f - dx)) / a3;
278 *g = (grad->
g[i] * a1 * dx + grad->
g[i - 1] * a2 * (1.f - dx)) / a3;
279 *b = (grad->
b[i] * a1 * dx + grad->
b[i - 1] * a2 * (1.f - dx)) / a3;
285 float ka = key->
a / 255.f;
286 float a2 = a + (1.f - a) * ka;
288 key->
a = key->
r = key->
g = key->
b = 0;
291 key->
a = round(a2 * 255.f);
292 key->
r = round((r * a + key->
r * ka * (1.f - a)) / a2);
293 key->
g = round((g * a + key->
g * ka * (1.f - a)) / a2);
294 key->
b = round((b * a + key->
b * ka * (1.f - a)) / a2);
300 unsigned char a = 255, r, g, b;
305 if(sscanf(
string,
"%hhd:%2hhx%2hhx%2hhx%2hhx%n", &newpos, &a, &r, &g, &b, &scanned) != 5)
309 if(newpos <= pos || newpos > 100)
314 gradient->
pts[count] = pos;
315 gradient->
a[count] = a;
316 gradient->
r[count] = r;
317 gradient->
g[count] = g;
318 gradient->
b[count] = b;
323 if(sscanf(
string,
"%2hhx%2hhx%2hhx%2hhx", &a, &r, &g, &b) != 4)
326 gradient->
pts[0] = 0;
327 gradient->
pts[1] = 100;
330 gradient->
r[0] = gradient->
r[1] = r;
331 gradient->
g[0] = gradient->
g[1] = g;
332 gradient->
b[0] = gradient->
b[1] = b;
354 printf(
"Error [ckb-main]: Reached EOF reading parameters");
357 if(!strcmp(cmd,
"end") && !strcmp(param,
"params"))
359 if(strcmp(cmd,
"param"))
365 int main(
int argc,
char *argv[]){
367 if(!strcmp(argv[1],
"--ckb-info")){
371 }
else if(!strcmp(argv[1],
"--ckb-run")){
380 printf(
"Error [ckb-main]: Reached EOF looking for \"begin keymap\"");
383 }
while(strcmp(cmd,
"begin") || strcmp(param,
"keymap"));
386 if(strcmp(cmd,
"keycount") || sscanf(param,
"%u", &keycount) != 1 || keycount == 0){
388 printf(
"Error [ckb-main]: \"begin keymap\" not followed with \"keycount\"");
393 unsigned max_x = 0, max_y = 0;
398 if(strcmp(cmd,
"key") || !*param || !sscanf(value,
"%u,%u", &x, &y)){
411 ctx.
width = max_x + 1;
417 printf(
"Error [ckb-main]: Reached EOF looking for \"end keymap\"");
420 }
while(strcmp(cmd,
"end") || strcmp(param,
"keymap"));
427 printf(
"Error [ckb-main]: Reached EOF looking for \"begin params\"");
430 }
while(strcmp(cmd,
"begin") || strcmp(param,
"params"));
437 printf(
"Error [ckb-main]: Reached EOF looking for \"begin run\"");
440 }
while(strcmp(cmd,
"begin") || strcmp(param,
"run"));
442 printf(
"begin run\n");
446 if(!*cmd || (!strcmp(cmd,
"end") && !strcmp(param,
"run")))
449 if(!strcmp(cmd,
"start"))
451 else if(!strcmp(cmd,
"stop"))
453 else if(!strcmp(cmd,
"begin") && !strcmp(param,
"params"))
455 else if(!strcmp(cmd,
"key")){
457 if(sscanf(param,
"%d,%d", &x, &y) == 2){
460 for(i = 0; i < keycount; i++){
473 for(i = 0; i < keycount; i++){
474 if(!strcmp(ctx.
keys[i].
name, param)){
482 }
else if(!strcmp(cmd,
"frame")){
485 printf(
"begin frame\n");
488 printf(
"argb %s %02hhx%02hhx%02hhx%02hhx\n", key->
name, key->
a, key->
r, key->
g, key->
b);
490 printf(
"end frame\n");
496 if(!strcmp(cmd,
"time") && sscanf(param,
"%lf", &delta) == 1)
506 printf(
"This program must be run from within ckb\n");
510 #endif // CKB_NO_MAIN
void printurl(const char *src)
int ckb_scan_grad(const char *string, ckb_gradient *gradient, int alpha)
void ckb_parameter(ckb_runctx *, const char *, const char *)
void ckb_keypress(ckb_runctx *, ckb_key *, int, int, int)
void ckb_init(ckb_runctx *context)
void ckb_time(ckb_runctx *, double)
int main(int argc, char *argv[])
void ckb_start(ckb_runctx *, int)
void urldecode(char *dst, const char *src)
void ckb_getline(char word1[(4 *1024)], char word2[(4 *1024)], char word3[(4 *1024)])
void ckb_read_params(ckb_runctx *ctx)
int ckb_frame(ckb_runctx *)
void ckb_alpha_blend(ckb_key *key, float a, float r, float g, float b)
void ckb_grad_color(float *a, float *r, float *g, float *b, const ckb_gradient *grad, float pos)