summaryrefslogtreecommitdiff
path: root/web/web_opts.c
blob: c156598b07e66f8ab7739b082eb7ebf8060578c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "web_opts.h"

#include "comp.h"

#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()
{
	read_delay();
	read_stdev();

	char buf[64];
	sprintf(buf, "%d", delay_ms);
	EM_ASM({elemSetValue($0, $1)}, "#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;
		}
	}
	EM_ASM({elemSetValue($0, $1)}, "#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);
}