summaryrefslogtreecommitdiff
path: root/common/rawaccel.hpp
blob: 08829ddd3348e022559605f5bcce1fd8d3c399cf (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#pragma once

#define _USE_MATH_DEFINES
#include <math.h>

#include "vec2.h"
#include "x64-util.hpp"
#include "external/tagged-union-single.h"

namespace rawaccel {

    /// <summary> Struct to hold vector rotation details. </summary>
    struct rotator {

        /// <summary> Rotational vector, which points in the direction of the post-rotation positive y axis. </summary>
        vec2d rot_vec = { 1, 0 };

        /// <summary>
        /// Rotates given input vector according to struct's rotational vector.
        /// </summary>
        /// <param name="input">Input vector to be rotated</param>
        /// <returns>2d vector of rotated input.</returns>
        inline vec2d operator()(const vec2d& input) const {
            return {
                input.x * rot_vec.x - input.y * rot_vec.y,
                input.x * rot_vec.y + input.y * rot_vec.x
            };
        }

        rotator(double degrees) {
            double rads = degrees * M_PI / 180;
            rot_vec = { cos(rads), sin(rads) };
        }

        rotator() = default;
    };

    /// <summary> Struct to hold clamp (min and max) details for acceleration application </summary>
    struct accel_scale_clamp {
        double lo = 0;
        double hi = 9;

        /// <summary>
        /// Clamps given input to min at lo, max at hi.
        /// </summary>
        /// <param name="scale">Double to be clamped</param>
        /// <returns>Clamped input as double</returns>
        inline double operator()(double scale) const {
            return clampsd(scale, lo, hi);
        }

        accel_scale_clamp(double cap) : accel_scale_clamp() {
            if (cap <= 0) {
                // use default, effectively uncapped accel
                return;
            }

            if (cap < 1) {
                // assume negative accel
                lo = cap;
                hi = 1;
            }
            else hi = cap;
        }

        accel_scale_clamp() = default;
    };

// Error throwing calls std libraries which are unavailable in kernel mode.
#ifdef _KERNEL_MODE
    void error(const char*) {}
#else
    void error(const char* s);
#endif

    using milliseconds = double;

    /// <summary> Struct to hold arguments for an acceleration function. </summary>
    struct accel_args {
        int accel_mode = 0;
        milliseconds time_min = 0.4;
        double offset = 0;
        double accel = 0;
        double lim_exp = 2;
        double midpoint = 0;
        vec2d weight = { 1, 1 };
        vec2d cap = { 0, 0 };
    };

    /// <summary>
    /// Struct to hold acceleration curve implementation details.
    /// </summary>
    /// <typeparam name="T">Type of acceleration.</typeparam>
    template <typename T>
    struct accel_implentation {
        
        /// <summary> First constant for use in acceleration curves. Generally, the acceleration ramp rate.</summary>
        double curve_constant_one = 0;

        /// <summary> Second constant for use in acceleration curves. Generally, the limit or exponent in the curve. </summary>
        double curve_constant_two = 0;

        /// <summary> Third constant for use in acceleration curves. The midpoint in sigmoid mode. </summary>
        double curve_constant_three = 0;

        /// <summary> The offset past which acceleration is applied. Used in power mode. </summary>
        double offset = 0;

        /// <summary>
        /// Initializes a new instance of the <see cref="accel_implementation{T}"/> struct.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        accel_implentation(accel_args args)
        {
            curve_constant_one = args.accel;
            curve_constant_two = args.lim_exp - 1;
            curve_constant_three = args.midpoint;
            offset = args.offset;
        }

        /// <summary>
        /// Returns accelerated value of speed as a ratio of magnitude.
        /// </summary>
        /// <param name="speed">Mouse speed at which to calculate acceleration.</param>
        /// <returns>Ratio of accelerated movement magnitude to input movement magnitude.</returns>
        double accelerate(double speed) { return 0; }

        /// <summary>
        /// Verifies arguments as valid. Errors if not.
        /// </summary>
        /// <param name="args">Arguments to verified.</param>
        void verify(accel_args args) {
            if (args.accel < 0) error("accel can not be negative, use a negative weight to compensate");
            if (args.time_min <= 0) error("min time must be positive");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        accel_implentation() = default;
    };

    /// <summary> Struct to hold linear acceleration implementation. </summary>
    struct accel_linear : accel_implentation<accel_linear> {

        accel_linear(accel_args args)
            : accel_implentation(args) {}

        double accelerate(double speed) {
            //f(x) = mx
            return curve_constant_one * speed;
        }

        void verify(accel_args args) {
            accel_implentation::verify(args);
            if (args.lim_exp <= 1) error("limit must be greater than 1");
        }
    };

    /// <summary> Struct to hold "classic" (linear raised to power) acceleration implementation. </summary>
    struct accel_classic : accel_implentation<accel_classic> {
        accel_classic(accel_args args)
            : accel_implentation(args) {}

        double accelerate(double speed) {
            //f(x) = (mx)^k
            return pow(curve_constant_one * speed, curve_constant_two);
        }

        void verify(accel_args args) {
            accel_implentation::verify(args);
            if (args.lim_exp <= 1) error("exponent must be greater than 1");
        }
    };

    /// <summary> Struct to hold "natural" (vanishing difference) acceleration implementation. </summary>
    struct accel_natural : accel_implentation<accel_natural> {
        accel_natural(accel_args args)
            : accel_implentation(args) 
        { curve_constant_one /= curve_constant_two; }

        double accelerate(double speed) {
            // f(x) = k(1-e^(-mx))
            return curve_constant_two - (curve_constant_two * exp(-curve_constant_one * speed));;
        }

        void verify(accel_args args) {
            accel_implentation::verify(args);
            if (args.lim_exp <= 1) error("exponent must be greater than 1");
        }
    };

    /// <summary> Struct to hold logarithmic acceleration implementation. </summary>
    struct accel_logarithmic : accel_implentation<accel_logarithmic> {
        accel_logarithmic(accel_args args)
            : accel_implentation(args) {}

        double accelerate(double speed) {
            return log(speed * curve_constant_one + 1);
        }

        void verify(accel_args args) {
            accel_implentation::verify(args);
            if (args.lim_exp <= 1) error("exponent must be greater than 1");
        }
    };

    /// <summary> Struct to hold sigmoid (s-shaped) acceleration implementation. </summary>
    struct accel_sigmoid : accel_implentation<accel_sigmoid> {
        accel_sigmoid(accel_args args)
            : accel_implentation(args) {}

        double accelerate(double speed) {
            //f(x) = k/(1+e^(-m(c-x)))
            return curve_constant_two / (exp(-curve_constant_one * (speed - curve_constant_three)) + 1);
        }

        void verify(accel_args args) {
            accel_implentation::verify(args);
            if (args.lim_exp <= 1) error("exponent must be greater than 1");
        }
    };

    /// <summary> Struct to hold power (non-additive) acceleration implementation. </summary>
    struct accel_power : accel_implentation<accel_power> {
        accel_power(accel_args args)
            : accel_implentation(args)
        { curve_constant_two++; }

        double accelerate(double speed) {
            // f(x) = (mx)^k - 1
            // The subtraction of 1 occurs with later addition of 1 in mind, 
            // so that the input vector is directly multiplied by (mx)^k (if unweighted)
            return (offset > 0 && speed < 1) ? 0 : pow(speed * curve_constant_one, curve_constant_two) - 1;
        }

        void verify(accel_args args) {
            accel_implentation::verify(args);
            if (args.lim_exp <= 0) error("exponent must be greater than 0");
        }
    };

    /// <summary> Struct to hold acceleration implementation which applies no acceleration. </summary>
    struct accel_noaccel : accel_implentation<accel_noaccel> {
        accel_noaccel(accel_args args)
            : accel_implentation(args) {}

        double accelerate(double speed) { return 0; }

        void verify(accel_args args) {}
    };
    
    /// <summary> Tagged union to hold all accel implementations and allow "polymorphism" via a visitor call. </summary>
    using accel_implementation_t = tagged_union<accel_linear, accel_classic, accel_natural, accel_logarithmic, accel_sigmoid, accel_power, accel_noaccel>;

    /// <summary> Struct for holding acceleration application details. </summary>
    struct accel_function {

        /*
        This value is ideally a few microseconds lower than
        the user's mouse polling interval, though it should
        not matter if the system is stable.
        */
        /// <summary> The minimum time period for one mouse movement. </summary>
        milliseconds time_min = 0.4;

        /// <summary> The offset past which acceleration is applied. </summary>
        double speed_offset = 0;

        /// <summary> The acceleration implementation (i.e. curve) </summary>
        accel_implementation_t accel;

        /// <summary> The weight of acceleration applied in {x, y} dimensions. </summary>
        vec2d weight = { 1, 1 };

        /// <summary> The object which sets a min and max for the acceleration scale. </summary>
        vec2<accel_scale_clamp> clamp;

        accel_function(accel_args args) {
            accel.tag = args.accel_mode;
            accel.visit([&](auto& a){ a = {args}; });

            // Verification is performed by the accel_implementation object
            // and therefore must occur after the object has been instantiated
            verify(args);

            time_min = args.time_min;
            speed_offset = args.offset;
            weight = args.weight;
            clamp.x = accel_scale_clamp(args.cap.x);
            clamp.y = accel_scale_clamp(args.cap.y);
        }

        /// <summary>
        /// Applies mouse acceleration to a given speed, via visitor function to accel_implementation_t
        /// </summary>
        /// <param name="speed">Speed from which to determine acceleration</param>
        /// <returns>Acceleration as a ratio magnitudes, as a double</returns>
        double apply(double speed) const {
            return accel.visit([=](auto accel_t) { return accel_t.accelerate(speed); });
        }

        /// <summary>
        /// Verifies acceleration arguments, via visitor function to accel_implementation_t
        /// </summary>
        /// <param name="args">Arguments to be verified</param>
        void verify(accel_args args) const {
            return accel.visit([=](auto accel_t) { accel_t.verify(args); });
        }

        /// <summary>
        /// Applies weighted acceleration to given input for given time period.
        /// </summary>
        /// <param name="input">2d vector of {x, y} mouse movement to be accelerated</param>
        /// <param name="time">Time period over which input movement was accumulated</param>
        /// <returns></returns>
        inline vec2d operator()(const vec2d& input, milliseconds time) const {
            double mag = sqrtsd(input.x * input.x + input.y * input.y);
            double time_clamped = clampsd(time, time_min, 100);
            double speed = maxsd(mag / time_clamped - speed_offset, 0);

            double accel_val = apply(speed);

            double scale_x = weight.x * accel_val + 1;
            double scale_y = weight.y * accel_val + 1;

            return {
                input.x * clamp.x(scale_x),
                input.y * clamp.y(scale_y)
            };
        }

        accel_function() = default;
    };

    /// <summary> Struct to hold variables and methods for modifying mouse input </summary>
    struct mouse_modifier {
        bool apply_rotate = false;
        bool apply_accel = false;
        rotator rotate;
        accel_function accel_fn;
        vec2d sensitivity = { 1, 1 };

        mouse_modifier(double degrees, vec2d sens, accel_args accel_args)
            : accel_fn(accel_args)
        {
            apply_rotate = degrees != 0;
            if (apply_rotate) rotate = rotator(degrees);
            else rotate = rotator();

            apply_accel = (accel_args.accel_mode != 0 &&
						   accel_args.accel_mode != accel_implementation_t::id<accel_noaccel>);

            if (sens.x == 0) sens.x = 1;
            if (sens.y == 0) sens.y = 1;
            sensitivity = sens;
        }

        /// <summary>
        /// Applies modification without acceleration. Rotation is the only
        /// modification currently implemented.
        /// </summary>
        /// <param name="input">Input to be modified.</param>
        /// <returns>2d vector of modified input.</returns>
        inline vec2d modify(vec2d input)
        {
            if (apply_rotate)
            {
                return rotate(input);
            }

            return input;
        }

        /// <summary>
        /// Applies modification, including acceleration.
        /// </summary>
        /// <param name="input">Input to be modified</param>
        /// <param name="time">Time period for determining acceleration.</param>
        /// <returns>2d vector with modified input.</returns>
        inline vec2d modify(vec2d input, milliseconds time)
        {
            return accel_fn(modify(input), time);
        }

        mouse_modifier() = default;
    };

} // rawaccel