summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--web/static/dartboat.js22
-rw-r--r--web/web_opts.c73
-rw-r--r--web/web_opts.h3
3 files changed, 74 insertions, 24 deletions
diff --git a/web/static/dartboat.js b/web/static/dartboat.js
index eaf76da..70e4677 100644
--- a/web/static/dartboat.js
+++ b/web/static/dartboat.js
@@ -7,6 +7,8 @@ function scheduleCCall(f, ms, ...args) {
}
function toCString(str) { // caller must free
+ if (!str) return 0;
+
const size = lengthBytesUTF8(str) + 1;
const ptr = _malloc(size);
stringToUTF8(str, ptr, size);
@@ -90,21 +92,12 @@ function promptHandle(command, data) {
if (str_d) _free(str_d);
}
-function setOpt(opt, val, update_opts = true) {
- if (update_opts) localStorage.setItem(`dartboat_${opt}`, val);
-
- if (opt === 'delay' || opt === 'stdev')
- Module[`_set_${opt}`](Number(val));
+function readOpt(opt) { // caller must free
+ return toCString(localStorage.getItem(UTF8ToString(opt)));
}
-function readOpts() {
- let val;
-
- if ((val = localStorage.getItem('dartboat_delay')))
- setOpt('delay', val, false);
-
- if ((val = localStorage.getItem('dartboat_stdev')))
- setOpt('stdev', val, false);
+function storeOpt(opt, val) {
+ localStorage.setItem(UTF8ToString(opt), UTF8ToString(val));
}
function boatAfloat() {
@@ -121,7 +114,8 @@ document.addEventListener('DOMContentLoaded', () => {
promptHandle(...x.split(':', 2)));
$$('[data-command]').forEach(x => x.addEventListener('click', f));
- f = e => setOpt(e.target.dataset.setting, e.target.value);
+ f = e => _set_opt(toCString(e.target.dataset.setting),
+ toCString(e.target.value));
$$('[data-setting]').forEach(x => x.addEventListener('change', f));
f = e => $(`#${e.target.dataset.modal}`).style.display = 'block';
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);
}
diff --git a/web/web_opts.h b/web/web_opts.h
index f66ac37..897ee0d 100644
--- a/web/web_opts.h
+++ b/web/web_opts.h
@@ -5,7 +5,4 @@ extern int delay_ms;
void opts_init();
-void set_delay(int delay);
-void set_stdev(float stdev);
-
#endif