summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Palecki <[email protected]>2020-07-29 00:35:39 -0700
committerJacob Palecki <[email protected]>2020-07-29 00:35:39 -0700
commit33317e79489848ae537ac78b9c9e70372857aee8 (patch)
tree2284bcec0ded710a61d49ba3fc52ff6096dd06c6
parentadd per-class args structs (diff)
downloadrawaccel-33317e79489848ae537ac78b9c9e70372857aee8.tar.xz
rawaccel-33317e79489848ae537ac78b9c9e70372857aee8.zip
Separate accel implementations into files
-rw-r--r--common/accel_classic.cpp21
-rw-r--r--common/accel_linear.cpp21
-rw-r--r--common/accel_logarithmic.cpp21
-rw-r--r--common/accel_natural.cpp23
-rw-r--r--common/accel_noaccel.cpp15
-rw-r--r--common/accel_power.cpp24
-rw-r--r--common/accel_sigmoid.cpp21
-rw-r--r--common/accel_types.hpp130
-rw-r--r--common/common.vcxitems10
-rw-r--r--common/rawaccel.hpp212
-rw-r--r--driver/driver.cpp8
11 files changed, 314 insertions, 192 deletions
diff --git a/common/accel_classic.cpp b/common/accel_classic.cpp
new file mode 100644
index 0000000..e4e7ab9
--- /dev/null
+++ b/common/accel_classic.cpp
@@ -0,0 +1,21 @@
+#pragma once
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+
+#include "accel_types.hpp"
+
+namespace rawaccel {
+ accel_classic::accel_classic(accel_args args)
+ : accel_implentation(args) {}
+
+ double accel_classic::accelerate(double speed) {
+ //f(x) = (mx)^k
+ return pow(curve_constant_one * speed, curve_constant_two);
+ }
+
+ 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
new file mode 100644
index 0000000..d12e798
--- /dev/null
+++ b/common/accel_linear.cpp
@@ -0,0 +1,21 @@
+#pragma once
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+
+#include "accel_types.hpp"
+
+namespace rawaccel {
+ accel_linear::accel_linear(accel_args args)
+ : accel_implentation(args) {}
+
+ double accel_linear::accelerate(double speed) {
+ //f(x) = mx
+ return curve_constant_one * speed;
+ }
+
+ 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
new file mode 100644
index 0000000..c127bcb
--- /dev/null
+++ b/common/accel_logarithmic.cpp
@@ -0,0 +1,21 @@
+#pragma once
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+
+#include "accel_types.hpp"
+
+namespace rawaccel {
+ accel_logarithmic::accel_logarithmic(accel_args args)
+ : accel_implentation(args) {}
+
+ double accel_logarithmic::accelerate(double speed) {
+ //f(x) = log(m*x+1)
+ return log(speed * curve_constant_one + 1);
+ }
+
+ 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
new file mode 100644
index 0000000..ba9bc02
--- /dev/null
+++ b/common/accel_natural.cpp
@@ -0,0 +1,23 @@
+
+#pragma once
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+
+#include "accel_types.hpp"
+
+namespace rawaccel {
+ accel_natural::accel_natural(accel_args args)
+ : accel_implentation(args)
+ { curve_constant_one /= curve_constant_two; }
+
+ double accel_natural::accelerate(double speed) {
+ // f(x) = k(1-e^(-mx))
+ return curve_constant_two - (curve_constant_two * exp(-curve_constant_one * speed));;
+ }
+
+ 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
new file mode 100644
index 0000000..50506a7
--- /dev/null
+++ b/common/accel_noaccel.cpp
@@ -0,0 +1,15 @@
+#pragma once
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+
+#include "accel_types.hpp"
+
+namespace rawaccel {
+ accel_noaccel::accel_noaccel(accel_args args)
+ : accel_implentation(args) {}
+
+ double accel_noaccel::accelerate(double speed) { return 0; }
+
+ void accel_noaccel::verify(accel_args args) { }
+}
diff --git a/common/accel_power.cpp b/common/accel_power.cpp
new file mode 100644
index 0000000..26f800d
--- /dev/null
+++ b/common/accel_power.cpp
@@ -0,0 +1,24 @@
+#pragma once
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+
+#include "accel_types.hpp"
+
+namespace rawaccel {
+ accel_power::accel_power(accel_args args)
+ : accel_implentation(args)
+ { curve_constant_two++; }
+
+ 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;
+ }
+
+ 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
new file mode 100644
index 0000000..c5280bc
--- /dev/null
+++ b/common/accel_sigmoid.cpp
@@ -0,0 +1,21 @@
+#pragma once
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+
+#include "accel_types.hpp"
+
+namespace rawaccel {
+ accel_sigmoid::accel_sigmoid(accel_args args)
+ : accel_implentation(args) {}
+
+ 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);
+ }
+
+ 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
new file mode 100644
index 0000000..c931097
--- /dev/null
+++ b/common/accel_types.hpp
@@ -0,0 +1,130 @@
+#pragma once
+
+#define _USE_MATH_DEFINES
+#include <math.h>
+
+namespace rawaccel {
+
+// 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 {
+ milliseconds time_min = 0.4;
+ double offset = 0;
+ double accel = 0;
+ double lim_exp = 2;
+ double midpoint = 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);
+ double accelerate(double speed);
+ void verify(accel_args args);
+ };
+
+ /// <summary> Struct to hold "classic" (linear raised to power) acceleration implementation. </summary>
+ struct accel_classic : accel_implentation<accel_classic> {
+ accel_classic(accel_args args);
+ double accelerate(double speed);
+ void verify(accel_args args);
+ };
+
+ /// <summary> Struct to hold "natural" (vanishing difference) acceleration implementation. </summary>
+ struct accel_natural : accel_implentation<accel_natural> {
+ accel_natural(accel_args args);
+ double accelerate(double speed);
+ void verify(accel_args args);
+ };
+
+ /// <summary> Struct to hold logarithmic acceleration implementation. </summary>
+ struct accel_logarithmic : accel_implentation<accel_logarithmic> {
+ accel_logarithmic(accel_args args);
+ double accelerate(double speed);
+ void verify(accel_args args);
+ };
+
+ /// <summary> Struct to hold sigmoid (s-shaped) acceleration implementation. </summary>
+ struct accel_sigmoid : accel_implentation<accel_sigmoid> {
+ accel_sigmoid(accel_args args);
+ double accelerate(double speed);
+ void verify(accel_args args);
+ };
+
+ /// <summary> Struct to hold power (non-additive) acceleration implementation. </summary>
+ struct accel_power : accel_implentation<accel_power> {
+ accel_power(accel_args args);
+ double accelerate(double speed);
+ void verify(accel_args args);
+ };
+
+ /// <summary> Struct to hold acceleration implementation which applies no acceleration. </summary>
+ struct accel_noaccel : accel_implentation<accel_noaccel> {
+ 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 224792c..f33d8e1 100644
--- a/common/common.vcxitems
+++ b/common/common.vcxitems
@@ -14,10 +14,20 @@
<ProjectCapability Include="SourceItemsFromImports" />
</ItemGroup>
<ItemGroup>
+ <ClInclude Include="$(MSBuildThisFileDirectory)accel_types.hpp" />
<ClInclude Include="$(MSBuildThisFileDirectory)Error.hpp" />
<ClInclude Include="$(MSBuildThisFileDirectory)rawaccel-userspace.hpp" />
<ClInclude Include="$(MSBuildThisFileDirectory)rawaccel.hpp" />
<ClInclude Include="$(MSBuildThisFileDirectory)x64-util.hpp" />
<ClInclude Include="$(MSBuildThisFileDirectory)vec2.h" />
</ItemGroup>
+ <ItemGroup>
+ <ClCompile Include="$(MSBuildThisFileDirectory)accel_classic.cpp" />
+ <ClCompile Include="$(MSBuildThisFileDirectory)accel_linear.cpp" />
+ <ClCompile Include="$(MSBuildThisFileDirectory)accel_logarithmic.cpp" />
+ <ClCompile Include="$(MSBuildThisFileDirectory)accel_natural.cpp" />
+ <ClCompile Include="$(MSBuildThisFileDirectory)accel_noaccel.cpp" />
+ <ClCompile Include="$(MSBuildThisFileDirectory)accel_power.cpp" />
+ <ClCompile Include="$(MSBuildThisFileDirectory)accel_sigmoid.cpp" />
+ </ItemGroup>
</Project> \ No newline at end of file
diff --git a/common/rawaccel.hpp b/common/rawaccel.hpp
index 9cfa5e7..d0c1b66 100644
--- a/common/rawaccel.hpp
+++ b/common/rawaccel.hpp
@@ -7,14 +7,15 @@
#include "x64-util.hpp"
#include "external/tagged-union-single.h"
-namespace rawaccel {
+#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"
-// 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
+namespace rawaccel {
/// <summary> Struct to hold vector rotation details. </summary>
struct rotator {
@@ -73,182 +74,6 @@ namespace rawaccel {
accel_scale_clamp() = default;
};
- using milliseconds = double;
-
- /// <summary> Struct to hold arguments for an acceleration function. </summary>
- struct accel_args {
- milliseconds time_min = 0.4;
- double offset = 0;
- double accel = 0;
- double lim_exp = 2;
- double midpoint = 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>;
@@ -371,18 +196,20 @@ namespace rawaccel {
}
/// <summary>
- /// Applies modification without acceleration. Rotation is the only
- /// modification currently implemented.
+ /// Applies modification without acceleration.
/// </summary>
/// <param name="input">Input to be modified.</param>
/// <returns>2d vector of modified input.</returns>
- inline vec2d modify(vec2d input)
+ inline vec2d modify_without_accel(vec2d input)
{
if (apply_rotate)
{
return rotate(input);
}
+ input.x *= sensitivity.x;
+ input.y *= sensitivity.y;
+
return input;
}
@@ -392,9 +219,18 @@ namespace rawaccel {
/// <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)
+ inline vec2d modify_with_accel(vec2d input, milliseconds time)
{
- return accel_fn(modify(input), time);
+ if (apply_rotate)
+ {
+ return rotate(input);
+ }
+
+ input = accel_fn(input, time);
+ input.x *= sensitivity.x;
+ input.y *= sensitivity.y;
+
+ return input;
}
mouse_modifier() = default;
diff --git a/driver/driver.cpp b/driver/driver.cpp
index 9704b21..1f9cebd 100644
--- a/driver/driver.cpp
+++ b/driver/driver.cpp
@@ -79,15 +79,15 @@ Arguments:
DebugPrint(("RA time < min with %d ticks\n", ticks));
}
- input = global.modifier.modify(input, time);
+ input = global.modifier.modify_with_accel(input, time);
}
else
{
- input = global.modifier.modify(input);
+ input = global.modifier.modify_without_accel(input);
}
- double result_x = input.x * global.modifier.sensitivity.x + local_carry.x;
- double result_y = input.y * global.modifier.sensitivity.y + local_carry.y;
+ double result_x = input.x + local_carry.x;
+ double result_y = input.y + local_carry.y;
LONG out_x = static_cast<LONG>(result_x);
LONG out_y = static_cast<LONG>(result_y);