diff options
Diffstat (limited to 'web/web_opts.c')
-rw-r--r-- | web/web_opts.c | 73 |
1 files changed, 66 insertions, 7 deletions
diff --git a/web/web_opts.c b/web/web_opts.c index 945909f..c156598 100644 --- a/web/web_opts.c +++ b/web/web_opts.c @@ -4,15 +4,67 @@ #include <math.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <emscripten/emscripten.h> +#define PREFIX "dartboat_" + int delay_ms = 1000; +void set_delay(char *val) +{ + delay_ms = atoi(val); +} + +void set_stdev(char *val) +{ + double stdev = strtod(val, NULL); + if (isfinite(stdev)) + horizontal_stdev = vertical_stdev = stdev; +} + +char *prefix_opt(char *opt) +{ + char len = strlen(opt); + char *s = malloc(len + sizeof(PREFIX)); + memcpy(s, PREFIX, sizeof(PREFIX) - 1); + memcpy(s + sizeof(PREFIX) - 1, opt, len + 1); + + return s; +} + +char *read_opt(char *opt) +{ + char *s = prefix_opt(opt); + char *val = (char *)EM_ASM_INT({return readOpt($0)}, s); + free(s); + return val; +} + +void read_delay() +{ + char *val = read_opt("delay"); + if (!val) return; + + set_delay(val); + free(val); +} + +void read_stdev() +{ + char *val = read_opt("stdev"); + if (!val) return; + + set_stdev(val); + free(val); +} + void opts_init() { - EM_ASM(readOpts()); + read_delay(); + read_stdev(); char buf[64]; sprintf(buf, "%d", delay_ms); @@ -30,15 +82,22 @@ void opts_init() EM_ASM({elemSetValue($0, $1)}, "#stdev", buf); } -EMSCRIPTEN_KEEPALIVE -void set_delay(int delay) +void store_opt(char *opt, char *val) { - delay_ms = delay; + char *s = prefix_opt(opt); + EM_ASM({storeOpt($0, $1)}, s, val); + free(s); } EMSCRIPTEN_KEEPALIVE -void set_stdev(float stdev) +void set_opt(char *opt, char *val) { - if (!isnan(stdev)) - horizontal_stdev = vertical_stdev = stdev; + if (!strcmp(opt, "delay")) + set_delay(val); + else if (!strcmp(opt, "stdev")) + set_stdev(val); + else + return; + + store_opt(opt, val); } |