From e8417a29fb2153ea035a757f36bfa300cf8b480d Mon Sep 17 00:00:00 2001 From: a1xd <68629610+a1xd@users.noreply.github.com> Date: Thu, 30 Jul 2020 17:07:35 -0400 Subject: add tweaks for st-refactor --- common/Error.hpp | 10 ---- common/accel-base.hpp | 50 ++++++++++++++++ common/accel-classic.hpp | 29 ++++++++++ common/accel-error.hpp | 11 ++++ common/accel-linear.hpp | 18 ++++++ common/accel-logarithmic.hpp | 20 +++++++ common/accel-natural.hpp | 31 ++++++++++ common/accel-noaccel.hpp | 15 +++++ common/accel-power.hpp | 33 +++++++++++ common/accel-sigmoid.hpp | 32 +++++++++++ common/accel_classic.cpp | 21 ------- common/accel_linear.cpp | 21 ------- common/accel_logarithmic.cpp | 21 ------- common/accel_natural.cpp | 23 -------- common/accel_noaccel.cpp | 15 ----- common/accel_power.cpp | 24 -------- common/accel_sigmoid.cpp | 21 ------- common/accel_types.hpp | 130 ------------------------------------------ common/common.vcxitems | 20 +++---- common/rawaccel-userspace.hpp | 53 +++++++++-------- common/rawaccel.hpp | 73 +++++++++++------------- 21 files changed, 309 insertions(+), 362 deletions(-) delete mode 100644 common/Error.hpp create mode 100644 common/accel-base.hpp create mode 100644 common/accel-classic.hpp create mode 100644 common/accel-error.hpp create mode 100644 common/accel-linear.hpp create mode 100644 common/accel-logarithmic.hpp create mode 100644 common/accel-natural.hpp create mode 100644 common/accel-noaccel.hpp create mode 100644 common/accel-power.hpp create mode 100644 common/accel-sigmoid.hpp delete mode 100644 common/accel_classic.cpp delete mode 100644 common/accel_linear.cpp delete mode 100644 common/accel_logarithmic.cpp delete mode 100644 common/accel_natural.cpp delete mode 100644 common/accel_noaccel.cpp delete mode 100644 common/accel_power.cpp delete mode 100644 common/accel_sigmoid.cpp delete mode 100644 common/accel_types.hpp diff --git a/common/Error.hpp b/common/Error.hpp deleted file mode 100644 index ed87090..0000000 --- a/common/Error.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include - - -namespace rawaccel { - void error(const char* s) { - throw std::domain_error(s); - } -} diff --git a/common/accel-base.hpp b/common/accel-base.hpp new file mode 100644 index 0000000..ed79bdb --- /dev/null +++ b/common/accel-base.hpp @@ -0,0 +1,50 @@ +#pragma once + +namespace rawaccel { + + // Error throwing calls std libraries which are unavailable in kernel mode. + void error(const char* s); + + using milliseconds = double; + + /// Struct to hold arguments for an acceleration function. + struct accel_args { + double offset = 0; + double accel = 0; + double limit = 2; + double exponent = 2; + double midpoint = 0; + double power_scale = 1; + }; + + /// + /// Struct to hold common acceleration curve implementation details. + /// + struct accel_base { + + /// Generally, the acceleration ramp rate. + double speed_coeff = 0; + + /// + /// Initializes a new instance of the struct. + /// + /// + /// + accel_base(accel_args args) { + verify(args); + + speed_coeff = args.accel; + } + + /// + /// Verifies arguments as valid. Errors if not. + /// + /// Arguments to verified. + void verify(accel_args args) const { + if (args.accel < 0) error("accel can not be negative, use a negative weight to compensate"); + } + + accel_base() = default; + }; + +} diff --git a/common/accel-classic.hpp b/common/accel-classic.hpp new file mode 100644 index 0000000..e2eb494 --- /dev/null +++ b/common/accel-classic.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include "accel-base.hpp" + +namespace rawaccel { + + /// Struct to hold "classic" (linear raised to power) acceleration implementation. + struct accel_classic : accel_base { + double exponent; + + accel_classic(accel_args args) : accel_base(args) { + verify(args); + + exponent = args.exponent - 1; + } + + inline double accelerate(double speed) const { + //f(x) = (mx)^k + return pow(speed_coeff * speed, exponent); + } + + void verify(accel_args args) const { + if (args.exponent <= 1) error("exponent must be greater than 1"); + } + }; + +} diff --git a/common/accel-error.hpp b/common/accel-error.hpp new file mode 100644 index 0000000..fa1f999 --- /dev/null +++ b/common/accel-error.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +namespace rawaccel { + + void error(const char* s) { + throw std::domain_error(s); + } + +} diff --git a/common/accel-linear.hpp b/common/accel-linear.hpp new file mode 100644 index 0000000..28150a8 --- /dev/null +++ b/common/accel-linear.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include "accel-base.hpp" + +namespace rawaccel { + + /// Struct to hold linear acceleration implementation. + struct accel_linear : accel_base { + + accel_linear(accel_args args) : accel_base(args) {} + + inline double accelerate(double speed) const { + //f(x) = mx + return speed_coeff * speed; + } + }; + +} diff --git a/common/accel-logarithmic.hpp b/common/accel-logarithmic.hpp new file mode 100644 index 0000000..b628327 --- /dev/null +++ b/common/accel-logarithmic.hpp @@ -0,0 +1,20 @@ +#pragma once + +#include + +#include "accel-base.hpp" + +namespace rawaccel { + + /// Struct to hold logarithmic acceleration implementation. + struct accel_logarithmic : accel_base { + + accel_logarithmic(accel_args args) : accel_base(args) {} + + inline double accelerate(double speed) const { + //f(x) = log(m*x+1) + return log(speed_coeff * speed + 1); + } + }; + +} diff --git a/common/accel-natural.hpp b/common/accel-natural.hpp new file mode 100644 index 0000000..6ccb193 --- /dev/null +++ b/common/accel-natural.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include + +#include "accel-base.hpp" + +namespace rawaccel { + + /// Struct to hold "natural" (vanishing difference) acceleration implementation. + struct accel_natural : accel_base { + double limit = 1; + double midpoint = 0; + + accel_natural(accel_args args) : accel_base(args) { + verify(args); + + limit = args.limit - 1; + speed_coeff /= limit; + } + + inline double accelerate(double speed) const { + // f(x) = k(1-e^(-mx)) + return limit - (limit * exp(-speed_coeff * speed)); + } + + void verify(accel_args args) const { + if (args.limit <= 1) error("limit must be greater than 1"); + } + }; + +} diff --git a/common/accel-noaccel.hpp b/common/accel-noaccel.hpp new file mode 100644 index 0000000..151002a --- /dev/null +++ b/common/accel-noaccel.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include "accel-base.hpp" + +namespace rawaccel { + + /// Struct to hold acceleration implementation which applies no acceleration. + struct accel_noaccel : accel_base { + + accel_noaccel(accel_args args) : accel_base(args) {} + + inline double accelerate(double) const { return 0; } + }; + +} diff --git a/common/accel-power.hpp b/common/accel-power.hpp new file mode 100644 index 0000000..c8b12cd --- /dev/null +++ b/common/accel-power.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include + +#include "accel-base.hpp" + +namespace rawaccel { + + /// Struct to hold power (non-additive) acceleration implementation. + struct accel_power : accel_base { + double exponent; + double offset; + + accel_power(accel_args args) { + verify(args); + + speed_coeff = args.power_scale; + exponent = args.exponent; + offset = args.offset; + } + + inline double accelerate(double speed) const { + // f(x) = (mx)^k + return (offset > 0 && speed < 1) ? 1 : pow(speed * speed_coeff, exponent); + } + + void verify(accel_args args) const { + if (args.power_scale < 0) error("scale can not be negative"); + if (args.exponent <= 0) error("exponent must be greater than 0"); + } + }; + +} diff --git a/common/accel-sigmoid.hpp b/common/accel-sigmoid.hpp new file mode 100644 index 0000000..e992e0c --- /dev/null +++ b/common/accel-sigmoid.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include + +#include "accel-base.hpp" + +namespace rawaccel { + + /// Struct to hold sigmoid (s-shaped) acceleration implementation. + struct accel_sigmoid : accel_base { + double limit = 1; + double midpoint = 0; + + accel_sigmoid(accel_args args) : accel_base(args) { + verify(args); + + limit = args.limit - 1; + midpoint = args.midpoint; + } + + inline double accelerate(double speed) const { + //f(x) = k/(1+e^(-m(c-x))) + return limit / (exp(-speed_coeff * (speed - midpoint)) + 1); + } + + void verify(accel_args args) const { + if (args.limit <= 1) error("exponent must be greater than 1"); + if (args.midpoint < 0) error("midpoint must not be negative"); + } + }; + +} diff --git a/common/accel_classic.cpp b/common/accel_classic.cpp deleted file mode 100644 index 323cd3b..0000000 --- a/common/accel_classic.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#define _USE_MATH_DEFINES -#include - -#include "accel_types.hpp" - -namespace rawaccel { - inline accel_classic::accel_classic(accel_args args) - : accel_implentation(args) {} - - inline double accel_classic::accelerate(double speed) { - //f(x) = (mx)^k - return pow(curve_constant_one * speed, curve_constant_two); - } - - inline void accel_classic::verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } -} diff --git a/common/accel_linear.cpp b/common/accel_linear.cpp deleted file mode 100644 index 307e33e..0000000 --- a/common/accel_linear.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#define _USE_MATH_DEFINES -#include - -#include "accel_types.hpp" - -namespace rawaccel { - inline accel_linear::accel_linear(accel_args args) - : accel_implentation(args) {} - - inline double accel_linear::accelerate(double speed) { - //f(x) = mx - return curve_constant_one * speed; - } - - inline void accel_linear::verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("limit must be greater than 1"); - } -} diff --git a/common/accel_logarithmic.cpp b/common/accel_logarithmic.cpp deleted file mode 100644 index 64808a1..0000000 --- a/common/accel_logarithmic.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#define _USE_MATH_DEFINES -#include - -#include "accel_types.hpp" - -namespace rawaccel { - inline accel_logarithmic::accel_logarithmic(accel_args args) - : accel_implentation(args) {} - - inline double accel_logarithmic::accelerate(double speed) { - //f(x) = log(m*x+1) - return log(speed * curve_constant_one + 1); - } - - inline void accel_logarithmic::verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } -} diff --git a/common/accel_natural.cpp b/common/accel_natural.cpp deleted file mode 100644 index c6f14b4..0000000 --- a/common/accel_natural.cpp +++ /dev/null @@ -1,23 +0,0 @@ - -#pragma once - -#define _USE_MATH_DEFINES -#include - -#include "accel_types.hpp" - -namespace rawaccel { - inline accel_natural::accel_natural(accel_args args) - : accel_implentation(args) - { curve_constant_one /= curve_constant_two; } - - inline double accel_natural::accelerate(double speed) { - // f(x) = k(1-e^(-mx)) - return curve_constant_two - (curve_constant_two * exp(-curve_constant_one * speed));; - } - - inline void accel_natural::verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } -} diff --git a/common/accel_noaccel.cpp b/common/accel_noaccel.cpp deleted file mode 100644 index fce5dd4..0000000 --- a/common/accel_noaccel.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#define _USE_MATH_DEFINES -#include - -#include "accel_types.hpp" - -namespace rawaccel { - inline accel_noaccel::accel_noaccel(accel_args args) - : accel_implentation(args) {} - - inline double accel_noaccel::accelerate(double speed) { return 0; } - - inline void accel_noaccel::verify(accel_args args) { } -} diff --git a/common/accel_power.cpp b/common/accel_power.cpp deleted file mode 100644 index f104554..0000000 --- a/common/accel_power.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#define _USE_MATH_DEFINES -#include - -#include "accel_types.hpp" - -namespace rawaccel { - inline accel_power::accel_power(accel_args args) - : accel_implentation(args) - { curve_constant_two++; } - - inline double accel_power::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; - } - - inline void accel_power::verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 0) error("exponent must be greater than 0"); - } -} diff --git a/common/accel_sigmoid.cpp b/common/accel_sigmoid.cpp deleted file mode 100644 index d3b8ab7..0000000 --- a/common/accel_sigmoid.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#define _USE_MATH_DEFINES -#include - -#include "accel_types.hpp" - -namespace rawaccel { - inline accel_sigmoid::accel_sigmoid(accel_args args) - : accel_implentation(args) {} - - inline double accel_sigmoid::accelerate(double speed) { - //f(x) = k/(1+e^(-m(c-x))) - return curve_constant_two / (exp(-curve_constant_one * (speed - curve_constant_three)) + 1); - } - - inline void accel_sigmoid::verify(accel_args args) { - accel_implentation::verify(args); - if (args.lim_exp <= 1) error("exponent must be greater than 1"); - } -} diff --git a/common/accel_types.hpp b/common/accel_types.hpp deleted file mode 100644 index 15f5a50..0000000 --- a/common/accel_types.hpp +++ /dev/null @@ -1,130 +0,0 @@ -#pragma once - -#define _USE_MATH_DEFINES -#include - -namespace rawaccel { - -// Error throwing calls std libraries which are unavailable in kernel mode. -#ifdef _KERNEL_MODE - inline void error(const char*) {} -#else - void error(const char* s); -#endif - - using milliseconds = double; - - /// Struct to hold arguments for an acceleration function. - struct accel_args { - milliseconds time_min = 0.4; - double offset = 0; - double accel = 0; - double lim_exp = 2; - double midpoint = 0; - }; - - /// - /// Struct to hold acceleration curve implementation details. - /// - /// Type of acceleration. - template - struct accel_implentation { - - /// First constant for use in acceleration curves. Generally, the acceleration ramp rate. - double curve_constant_one = 0; - - /// Second constant for use in acceleration curves. Generally, the limit or exponent in the curve. - double curve_constant_two = 0; - - /// Third constant for use in acceleration curves. The midpoint in sigmoid mode. - double curve_constant_three = 0; - - /// The offset past which acceleration is applied. Used in power mode. - double offset = 0; - - /// - /// Initializes a new instance of the struct. - /// - /// - /// - 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; - } - - /// - /// Returns accelerated value of speed as a ratio of magnitude. - /// - /// Mouse speed at which to calculate acceleration. - /// Ratio of accelerated movement magnitude to input movement magnitude. - double accelerate(double speed) { return 0; } - - /// - /// Verifies arguments as valid. Errors if not. - /// - /// Arguments to verified. - 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"); - } - - /// - /// - /// - /// - accel_implentation() = default; - }; - - /// Struct to hold linear acceleration implementation. - struct accel_linear : accel_implentation { - accel_linear(accel_args args); - double accelerate(double speed); - void verify(accel_args args); - }; - - /// Struct to hold "classic" (linear raised to power) acceleration implementation. - struct accel_classic : accel_implentation { - accel_classic(accel_args args); - double accelerate(double speed); - void verify(accel_args args); - }; - - /// Struct to hold "natural" (vanishing difference) acceleration implementation. - struct accel_natural : accel_implentation { - accel_natural(accel_args args); - double accelerate(double speed); - void verify(accel_args args); - }; - - /// Struct to hold logarithmic acceleration implementation. - struct accel_logarithmic : accel_implentation { - accel_logarithmic(accel_args args); - double accelerate(double speed); - void verify(accel_args args); - }; - - /// Struct to hold sigmoid (s-shaped) acceleration implementation. - struct accel_sigmoid : accel_implentation { - accel_sigmoid(accel_args args); - double accelerate(double speed); - void verify(accel_args args); - }; - - /// Struct to hold power (non-additive) acceleration implementation. - struct accel_power : accel_implentation { - accel_power(accel_args args); - double accelerate(double speed); - void verify(accel_args args); - }; - - /// Struct to hold acceleration implementation which applies no acceleration. - struct accel_noaccel : accel_implentation { - accel_noaccel(accel_args args); - double accelerate(double speed); - void verify(accel_args args); - }; - -} diff --git a/common/common.vcxitems b/common/common.vcxitems index f33d8e1..d1e8db0 100644 --- a/common/common.vcxitems +++ b/common/common.vcxitems @@ -14,20 +14,18 @@ - - + + + + + + + + + - - - - - - - - - \ No newline at end of file diff --git a/common/rawaccel-userspace.hpp b/common/rawaccel-userspace.hpp index 3e8886f..49c0fcd 100644 --- a/common/rawaccel-userspace.hpp +++ b/common/rawaccel-userspace.hpp @@ -4,6 +4,7 @@ #include "external/clipp.h" +#include #include "rawaccel.hpp" namespace rawaccel { @@ -12,12 +13,14 @@ inline constexpr int SYSTEM_ERROR = -1; inline constexpr int PARSE_ERROR = 1; inline constexpr int INVALID_ARGUMENT = 2; -void error(const char* s) { - throw std::domain_error(s); +template +clipp::parameter make_accel_cmd(modifier_args& args, StrFirst&& first_flag, StrRest&&... rest) { + return clipp::command(first_flag, rest...) + .set(args.acc_fn_args.accel_mode, accel_impl_t::id); } mouse_modifier parse(int argc, char** argv) { - modifier_args modifier_args{}; + modifier_args args{}; auto make_opt_vec = [](vec2d& v, auto first_flag, auto... rest) { return clipp::option(first_flag, rest...) & ( @@ -38,64 +41,64 @@ mouse_modifier parse(int argc, char** argv) { }; // default options - auto opt_sens = "sensitivity (default = 1)" % make_opt_vec(modifier_args.sens, "sens"); + auto opt_sens = "sensitivity (default = 1)" % make_opt_vec(args.sens, "sens"); auto opt_rot = "counter-clockwise rotation (default = 0)" % ( clipp::option("rotate") & - clipp::number("degrees", modifier_args.degrees) + clipp::number("degrees", args.degrees) ); // mode-independent accel options auto opt_weight = "accel multiplier (default = 1)" % - make_opt_vec(modifier_args.acc_fn_args.weight, "weight"); + make_opt_vec(args.acc_fn_args.weight, "weight"); auto opt_offset = "speed (dots/ms) where accel kicks in (default = 0)" % ( - clipp::option("offset") & clipp::number("speed", modifier_args.acc_fn_args.acc_args.offset) + clipp::option("offset") & clipp::number("speed", args.acc_fn_args.acc_args.offset) ); auto opt_cap = "accel scale cap (default = 9)" % - make_opt_vec(modifier_args.acc_fn_args.cap, "cap"); + make_opt_vec(args.acc_fn_args.cap, "cap"); auto opt_tmin = "minimum time between polls (default = 0.4)" % ( clipp::option("tmin") & - clipp::number("ms", modifier_args.acc_fn_args.acc_args.time_min) + clipp::number("ms", args.acc_fn_args.time_min) ); - auto accel_var = (clipp::required("accel") & clipp::number("num", modifier_args.acc_fn_args.acc_args.accel)) % "ramp rate"; - auto limit_var = (clipp::required("limit") & clipp::number("scale", modifier_args.acc_fn_args.acc_args.lim_exp)) % "limit"; + auto accel_var = (clipp::required("accel") & clipp::number("num", args.acc_fn_args.acc_args.accel)) % "ramp rate"; + auto limit_var = (clipp::required("limit") & clipp::number("scale", args.acc_fn_args.acc_args.limit)) % "limit"; + auto exp_var = (clipp::required("exponent") & clipp::number("num", args.acc_fn_args.acc_args.exponent)) % "exponent"; // modes - auto noaccel_mode = "no-accel mode" % ( - clipp::command("off", "noaccel").set(modifier_args.acc_fn_args.accel_mode, accel_implementation_t::id) - ); + auto noaccel_mode = "no-accel mode" % make_accel_cmd(args, "off", "noaccel"); + auto lin_mode = "linear accel mode:" % ( - clipp::command("linear").set(modifier_args.acc_fn_args.accel_mode, accel_implementation_t::id), + make_accel_cmd(args, "linear"), accel_var ); auto classic_mode = "classic accel mode:" % ( - clipp::command("classic").set(modifier_args.acc_fn_args.accel_mode, accel_implementation_t::id), + make_accel_cmd(args, "classic"), accel_var, - (clipp::required("exponent") & clipp::number("num", modifier_args.acc_fn_args.acc_args.lim_exp)) % "exponent" + exp_var ); auto nat_mode = "natural accel mode:" % ( - clipp::command("natural").set(modifier_args.acc_fn_args.accel_mode, accel_implementation_t::id), + make_accel_cmd(args, "natural"), accel_var, limit_var ); auto log_mode = "logarithmic accel mode:" % ( - clipp::command("logarithmic").set(modifier_args.acc_fn_args.accel_mode, accel_implementation_t::id), + make_accel_cmd(args, "logarithmic"), accel_var ); auto sig_mode = "sigmoid accel mode:" % ( - clipp::command("sigmoid").set(modifier_args.acc_fn_args.accel_mode, accel_implementation_t::id), + make_accel_cmd(args, "sigmoid"), accel_var, limit_var, - (clipp::required("midpoint") & clipp::number("speed", modifier_args.acc_fn_args.acc_args.midpoint)) % "midpoint" + (clipp::required("midpoint") & clipp::number("speed", args.acc_fn_args.acc_args.midpoint)) % "midpoint" ); auto pow_mode = "power accel mode:" % ( - clipp::command("power").set(modifier_args.acc_fn_args.accel_mode, accel_implementation_t::id) >> [&] { modifier_args.acc_fn_args.acc_args.accel = 1; }, - (clipp::required("exponent") & clipp::number("num", modifier_args.acc_fn_args.acc_args.lim_exp)) % "exponent", - (clipp::option("scale") & clipp::number("num", modifier_args.acc_fn_args.acc_args.accel)) % "scale factor" + make_accel_cmd(args, "power"), + exp_var, + (clipp::option("scale") & clipp::number("num", args.acc_fn_args.acc_args.power_scale)) % "scale factor" ); auto accel_mode_exclusive = (lin_mode | classic_mode | nat_mode | log_mode | sig_mode | pow_mode); @@ -119,7 +122,7 @@ mouse_modifier parse(int argc, char** argv) { std::exit(0); } - return mouse_modifier(modifier_args); + return mouse_modifier(args); } } // rawaccel diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp index b480e87..0aeb42d 100644 --- a/common/rawaccel.hpp +++ b/common/rawaccel.hpp @@ -7,13 +7,13 @@ #include "x64-util.hpp" #include "external/tagged-union-single.h" -#include "accel_linear.cpp" -#include "accel_classic.cpp" -#include "accel_natural.cpp" -#include "accel_logarithmic.cpp" -#include "accel_sigmoid.cpp" -#include "accel_power.cpp" -#include "accel_noaccel.cpp" +#include "accel-linear.hpp" +#include "accel-classic.hpp" +#include "accel-natural.hpp" +#include "accel-logarithmic.hpp" +#include "accel-sigmoid.hpp" +#include "accel-power.hpp" +#include "accel-noaccel.hpp" namespace rawaccel { @@ -75,11 +75,11 @@ namespace rawaccel { }; /// Tagged union to hold all accel implementations and allow "polymorphism" via a visitor call. - using accel_implementation_t = tagged_union; + using accel_impl_t = tagged_union; struct accel_fn_args { - accel_args acc_args = accel_args{}; - int accel_mode = 0; + accel_args acc_args; + int accel_mode = accel_impl_t::id; milliseconds time_min = 0.4; vec2d weight = { 1, 1 }; vec2d cap = { 0, 0 }; @@ -100,21 +100,24 @@ namespace rawaccel { double speed_offset = 0; /// The acceleration implementation (i.e. curve) - accel_implementation_t accel; + accel_impl_t accel; /// The weight of acceleration applied in {x, y} dimensions. vec2d weight = { 1, 1 }; + /// The constant added to weighted accel values to get the acceleration scale. + double scale_base = 1; + /// The object which sets a min and max for the acceleration scale. vec2 clamp; - accel_function(accel_fn_args args) { + accel_function(accel_fn_args args) : accel_function() { + verify(args); + accel.tag = args.accel_mode; - accel.visit([&](auto& a){ a = {args.acc_args}; }); + accel.visit([&](auto& impl){ impl = { args.acc_args }; }); - // Verification is performed by the accel_implementation object - // and therefore must occur after the object has been instantiated - verify(args.acc_args); + if (accel.tag == accel_impl_t::id) scale_base = 0; time_min = args.time_min; speed_offset = args.acc_args.offset; @@ -124,20 +127,12 @@ namespace rawaccel { } /// - /// Applies mouse acceleration to a given speed, via visitor function to accel_implementation_t - /// - /// Speed from which to determine acceleration - /// Acceleration as a ratio magnitudes, as a double - double apply(double speed) const { - return accel.visit([=](auto accel_t) { return accel_t.accelerate(speed); }); - } - - /// - /// Verifies acceleration arguments, via visitor function to accel_implementation_t + /// Verifies acceleration arguments, via visitor function to accel_impl_t /// /// Arguments to be verified - void verify(accel_args args) const { - return accel.visit([=](auto accel_t) { accel_t.verify(args); }); + void verify(accel_fn_args args) { + if (args.time_min <= 0) error("min time must be positive"); + if (args.acc_args.offset < 0) error("offset must not be negative"); } /// @@ -151,10 +146,12 @@ namespace rawaccel { double time_clamped = clampsd(time, time_min, 100); double speed = maxsd(mag / time_clamped - speed_offset, 0); - double accel_val = apply(speed); + double accel_val = accel.visit([=](auto&& impl) { + return impl.accelerate(speed); + }); - double scale_x = weight.x * accel_val + 1; - double scale_y = weight.y * accel_val + 1; + double scale_x = weight.x * accel_val + scale_base; + double scale_y = weight.y * accel_val + scale_base; return { input.x * clamp.x(scale_x), @@ -165,11 +162,10 @@ namespace rawaccel { accel_function() = default; }; - struct modifier_args - { + struct modifier_args { double degrees = 0; vec2d sens = { 1, 1 }; - accel_fn_args acc_fn_args = accel_fn_args{}; + accel_fn_args acc_fn_args; }; /// Struct to hold variables and methods for modifying mouse input @@ -187,8 +183,8 @@ namespace rawaccel { if (apply_rotate) rotate = rotator(args.degrees); else rotate = rotator(); - apply_accel = (args.acc_fn_args.accel_mode != 0 && - args.acc_fn_args.accel_mode != accel_implementation_t::id); + apply_accel = args.acc_fn_args.acc_args.accel != 0 && + args.acc_fn_args.accel_mode != accel_impl_t::id; if (args.sens.x == 0) args.sens.x = 1; if (args.sens.y == 0) args.sens.y = 1; @@ -226,10 +222,7 @@ namespace rawaccel { input = rotate(input); } - if (apply_accel) - { - input = accel_fn(input, time); - } + input = accel_fn(input, time); input.x *= sensitivity.x; input.y *= sensitivity.y; -- cgit v1.2.3