Repository navigation
Expand file tree
/
Copy pathController_01_Invert_Vertical.cpp
More file actions
129 lines (121 loc) · 4.83 KB
/
Copy pathController_01_Invert_Vertical.cpp
File metadata and controls
129 lines (121 loc) · 4.83 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
#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)));
}
void InvertVertical(Hid_State& state, std::string_view layout) {
for (auto& axis : state.axes) {
if (Editable(axis) && AxisRole(state, axis, layout) == 2) {
axis.normalized = -axis.normalized;
}
}
}
void InvertVertical(IC_NATIVE_GAMEPAD_STATE& state, bool) {
state.left_y = Raw(-Unit(state.left_y));
state.right_y = Raw(-Unit(state.right_y));
}
int main(int argc, char** argv) try {
std::cout
<< "Invert every identified stick Y axis. Supports HID, XInput/XUSB, XInputHID and GIP.\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 {
throw std::invalid_argument("Use --hid-layout auto|xy-rxry|xy-zrz.");
}
}
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>()) {
InvertVertical(hid->state, layout);
} else if (auto* xinput = input.Get<Controller_Xinput_Input>()) {
InvertVertical(xinput->state, false);
} else if (auto* xinputHid = input.Get<Controller_XinputHid_Input>()) {
InvertVertical(xinputHid->state, layout);
} else if (auto* gip = input.Get<Controller_GIP_Input>()) {
InvertVertical(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;
}