#include "web_dom.h" #include "web_opts.h" #include "comp.h" #include #include #include #include #include #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() { read_delay(); read_stdev(); char buf[64]; sprintf(buf, "%d", delay_ms); dom_elem_set_value("#delay", buf); int len = sprintf(buf, "%4.2f", horizontal_stdev); char *dot = strchr(buf, '.'); if (dot) { for (char *ptr = buf + len - 1; ptr >= dot; --ptr) { if (*ptr != '0' && *ptr != '.') break; *ptr = 0; } } dom_elem_set_value("#stdev", buf); } void store_opt(char *opt, char *val) { char *s = prefix_opt(opt); EM_ASM({storeOpt($0, $1)}, s, val); free(s); } EMSCRIPTEN_KEEPALIVE void set_opt(char *opt, char *val) { if (!strcmp(opt, "delay")) set_delay(val); else if (!strcmp(opt, "stdev")) set_stdev(val); else return; store_opt(opt, val); }