Repository navigation
Expand file tree
/
Copy pathController_05_Inverse_Exponential.cpp
More file actions
146 lines (136 loc) · 5.63 KB
/
Copy pathController_05_Inverse_Exponential.cpp
File metadata and controls
146 lines (136 loc) · 5.63 KB
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
#include "Interceptor.h"
#include <iostream>
#include <string_view>
#include <system_error>
#include <algorithm>
#include <cmath>
using namespace Interceptor;
// HID labels are generic: auto uses X/Y + Z/Rz when present, otherwise X/Y + Rx/Ry.
// --hid-layout xy-rxry or xy-zrz resolves ambiguous descriptors without VID/PID rules.
bool HasAxis(const Hid_State& state, const Hid_Value& axis, unsigned usage) {
for (const auto& other : state.axes) {
if (other.usage_page == 1 && other.usage == usage && other.report_id == axis.report_id &&
other.link_collection == axis.link_collection && other.element == axis.element) {
return true;
}
}
return false;
}
int AxisRole(const Hid_State& state, const Hid_Value& axis, std::string_view layout) {
if (axis.usage_page != 1) {
return 0;
}
if (axis.usage == 0x30) {
return 1; // Horizontal X
}
if (axis.usage == 0x31) {
return 2; // Vertical Y, including repeated Y controls
}
const bool zr = layout == "xy-zrz" ||
(layout == "auto" && HasAxis(state, axis, 0x32) && HasAxis(state, axis, 0x35));
if (axis.usage == (zr ? 0x32 : 0x33)) {
return 1; // Right stick horizontal
}
if (axis.usage == (zr ? 0x35 : 0x34)) {
return 2; // Right stick vertical
}
if ((layout != "auto" || zr) &&
(axis.usage == (zr ? 0x33 : 0x32) || axis.usage == (zr ? 0x34 : 0x35))) {
return 3;
}
return 0; // Leave unknown/combined-trigger encodings untouched.
}
bool Editable(const Hid_Value& axis) {
return axis.normalized_valid && !axis.relative && !axis.is_null;
}
double Unit(short value) {
return value < 0 ? value / 32768.0 : value / 32767.0;
}
short Raw(double value) {
value = std::clamp(value, -1.0, 1.0);
return static_cast<short>(std::lround(value * (value < 0 ? 32768.0 : 32767.0)));
}
double exponent = 2.0; // Inverse of a matching game exponent; 2 means square root.
double Curve(double x) {
return std::copysign(std::pow(std::abs(x), 1.0 / exponent), x);
}
void ApplyInverseCurve(Hid_State& state, std::string_view layout) {
for (auto& axis : state.axes) {
const auto role = AxisRole(state, axis, layout);
if (Editable(axis) && (role == 1 || role == 2)) {
axis.normalized = Curve(axis.normalized);
}
}
}
void ApplyInverseCurve(IC_NATIVE_GAMEPAD_STATE& state, bool) {
state.left_x = Raw(Curve(Unit(state.left_x)));
state.left_y = Raw(Curve(Unit(state.left_y)));
state.right_x = Raw(Curve(Unit(state.right_x)));
state.right_y = Raw(Curve(Unit(state.right_y)));
}
int main(int argc, char** argv) try {
std::cout
<< "Apply an inverse exponential stick curve (square root by default). Supports all families.\n"
"Run one example at a time as administrator. Close this window or press Ctrl+C to stop.\n"
<< std::flush;
std::string_view layout = "auto";
for (int i = 1; i < argc; ++i) {
if (std::string_view(argv[i]) == "--hid-layout" && i + 1 < argc) {
layout = argv[++i];
} else if (std::string_view(argv[i]) == "--exponent" && i + 1 < argc) {
std::size_t used = 0;
const std::string value = argv[++i];
exponent = std::stod(value, &used);
if (used != value.size() || !std::isfinite(exponent) || exponent < 1 || exponent > 16) {
throw std::invalid_argument("Exponent must be between 1 and 16.");
}
} else {
throw std::invalid_argument(
"Use --hid-layout auto|xy-rxry|xy-zrz or --exponent 1..16.");
}
}
if (layout != "auto" && layout != "xy-rxry" && layout != "xy-zrz") {
throw std::invalid_argument("Unknown HID layout.");
}
// Check installation and initialize supported devices before capturing.
Get_Status();
const auto init = Init_AllDevices();
if (!init.ready) {
std::cerr << "Initialization failed. Error: " << init.error
<< ". Restart required: " << init.reboot_required << "\n";
return 1;
}
DWORD consoleMode = 0; // Console only: prevent text selection from blocking console writes.
if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &consoleMode)) {
SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE),
(consoleMode | ENABLE_EXTENDED_FLAGS) & ~ENABLE_QUICK_EDIT_MODE);
}
for (;;) {
// Receive: wait for input without polling.
auto inputs = Get_Input_With_Timeout(INFINITE);
for (auto& input : inputs) {
if (auto* hid = input.Get<Controller_Hid_Input>()) {
ApplyInverseCurve(hid->state, layout);
} else if (auto* xinput = input.Get<Controller_Xinput_Input>()) {
ApplyInverseCurve(xinput->state, false);
} else if (auto* xinputHid = input.Get<Controller_XinputHid_Input>()) {
ApplyInverseCurve(xinputHid->state, layout);
} else if (auto* gip = input.Get<Controller_GIP_Input>()) {
ApplyInverseCurve(gip->state, true);
}
}
// Forward: send all unblocked inputs, including other device types.
const auto sent = Send_Input(inputs);
if (!sent) {
throw std::system_error(sent.error, std::system_category(), "Send_Input");
}
}
} catch (const std::system_error& error) {
std::cerr
<< "Driver error: " << error.code().value()
<< ". Install this edition with InterceptorSetup.exe, restart if requested, and run as administrator.\n";
return 1;
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}