summaryrefslogtreecommitdiff
path: root/common/rawaccel-validate.hpp
blob: b724bf24c02ef8aae15ac830b3605e4a134e00b4 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#pragma once

#include "rawaccel-base.hpp"
#include "utility.hpp"

namespace rawaccel {

	struct valid_ret_t {
		int last_x = 0;
		int last_y = 0;
		int count = 0;

		explicit operator bool() const
		{
			return count == 0;
		}
	};

	template <typename MsgHandler = noop>
	valid_ret_t valid(const settings& args, MsgHandler fn = {})
	{
		valid_ret_t ret;

		auto error = [&](auto msg) {
			++ret.count;
			fn(msg);
		};

		auto check_accel = [&error](const accel_args& args) {
			static_assert(SPACED_LUT_CAPACITY == 1025, "update error msg");

			const auto& lut_args = args.spaced_args;

			if (lut_args.partitions <= 0) {
				error("lut partitions"" must be positive");
			}

			if (lut_args.mode == spaced_lut_mode::linear) {
				if (lut_args.start <= 0) {
					error("start"" must be positive");
				}

				if (lut_args.stop <= lut_args.start) {
					error("stop must be greater than start");
				}

				if (lut_args.num_elements < 2 ||
					lut_args.num_elements > 1025) {
					error("num must be between 2 and 1025");
				}
			}
			else if (lut_args.mode == spaced_lut_mode::binlog) {
				int istart = static_cast<int>(lut_args.start);
				int istop = static_cast<int>(lut_args.stop);

				if (lut_args.start < -99) {
					error("start is too small");
				}
				else if (lut_args.stop > 99) {
					error("stop is too large");
				}
				else if (istart != lut_args.start || istop != lut_args.stop) {
					error("start and stop must be integers");
				}
				else if (istop <= istart) {
					error("stop must be greater than start");
				}
				else if (lut_args.num_elements <= 0) {
					error("num"" must be positive");
				}
				else if (((lut_args.stop - lut_args.start) * lut_args.num_elements) >= 1025) {
					error("binlog mode requires (num * (stop - start)) < 1025");
				}
			}

			if (args.mode == accel_mode::arb_lookup) {
				if (args.arb_args.length < 2) {
					error("lookup mode requires at least 2 points");
				}
			}

			if (args.offset < 0) {
				error("offset can not be negative");
			}
			else if (args.mode == accel_mode::jump && args.offset == 0) {
				error("offset can not be 0");
			}

			if (args.cap < 0) {
				error("cap"" must not be negative");
			}
			else if (args.mode == accel_mode::jump && args.cap == 0) {
				error("cap can not be 0");
			}

			if (args.growth_rate <= 0 ||
				args.decay_rate <= 0 ||
				args.accel_classic <= 0) {
				error("acceleration"" must be positive");
			}

			if (args.motivity <= 1) {
				error("motivity must be greater than 1");
			}

			if (args.power <= 1) {
				error("power must be greater than 1");
			}

			if (args.scale <= 0) {
				error("scale"" must be positive");
			}

			if (args.weight <= 0) {
				error("weight"" must be positive");
			}

			if (args.exponent <= 0) {
				error("exponent"" must be positive");
			}

			if (args.limit <= 0) {
				error("limit"" must be positive");
			}

			if (args.midpoint <= 0) {
				error("midpoint"" must be positive");
			}

			if (args.smooth < 0 || args.smooth > 1) {
				error("smooth must be between 0 and 1");
			}

		};

		check_accel(args.argsv.x);

		if (!args.combine_mags) {
			ret.last_x = ret.count;
			check_accel(args.argsv.y);
			ret.last_y = ret.count;
		}

		if (args.dpi <= 0) {
			error("dpi"" must be positive");
		}

		if (args.speed_max < 0) {
			error("speed cap is negative");
		}
		else if (args.speed_max < args.speed_min) {
			error("max speed is less than min speed");
		}

		if (args.degrees_snap < 0 || args.degrees_snap > 45) {
			error("snap angle must be between 0 and 45 degrees");
		}

		if (args.sens.x == 0 || args.sens.y == 0) {
			error("sens multiplier is 0");
		}

		if (args.dom_args.domain_weights.x <= 0 ||
			args.dom_args.domain_weights.y <= 0) {
			error("domain weights"" must be positive");
		}

		if (args.dir_multipliers.x <= 0 || args.dir_multipliers.y <= 0) {
			error("directional multipliers must be positive");
		}

		if (args.dom_args.lp_norm <= 0) {
			error("Lp norm must be positive (default=2)");
		}

		if (args.range_weights.x <= 0 || args.range_weights.y <= 0) {
			error("range weights"" must be positive");
		}

		if (args.time_min <= 0) {
			error("minimum time"" must be positive");
		}

		if (args.time_max < args.time_min) {
			error("max time is less than min time");
		}

		return ret;
	}

}