-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathmain.cpp
More file actions
177 lines (156 loc) · 4.58 KB
/
main.cpp
File metadata and controls
177 lines (156 loc) · 4.58 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
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
#include <iostream> // cout
#include <thread> // this_thread::sleep_for, this_thread::yield
#include <chrono> // milliseconds
#include <vector>
#include <array>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <Windows.h>
#include <conio.h> // _kbhit, _getch_nolock
#include "XOutput.hpp"
#include "Common.hpp"
#include "Controller.hpp"
#include "Cerberus.hpp"
#include "Version.hpp"
#include "Config.hpp"
namespace {
bool hasBroke{ false };
void unsetBreakHandler();
BOOL __stdcall breakHandler(DWORD type) {
switch (type) {
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_BREAK_EVENT:
hasBroke = true;
unsetBreakHandler();
return TRUE;
}
return FALSE;
}
void setBreakHandler() {
SetConsoleCtrlHandler(breakHandler, TRUE);
}
void unsetBreakHandler() {
SetConsoleCtrlHandler(breakHandler, FALSE);
}
constexpr bool SUCCESS(DWORD e) {
return e == XOutput::XOUTPUT_SUCCESS;
}
void pause() {
while (_kbhit() != 0) _getch(); // Eat any buffered input
std::cout << "Press any key to continue..." << std::endl; // Intentional use of endl to flush output buffer
while (_kbhit() == 0) { // Wait for any input
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}
}
// argc and argv are unused
int main(int, char*[]) {
using std::cout;
using std::this_thread::yield;
using namespace Procon;
// Pause before exiting
auto pause = make_scoped(::pause);
cout << ProgramName << ' ' << ProgramVersion << ' ' << Platform << ' ' << BuildType << "\n\n";
try {
Config::readConfigFile("config.txt");
}
catch (const ConfigError &e) {
cout << "Error reading config file: " << e.what() << '\n';
return -1;
}
try {
XOutput::XOutputInitialize();
}
catch (XOutput::XOutputError &e) {
cout << e.what() << '\n';
return -1;
}
DWORD unused;
if (!SUCCESS(XOutput::XOutputGetRealUserIndex(0, &unused))) {
cout << "Unable to connect to ScpVBus.\n";
return -1;
}
#ifndef NO_CERBERUS
Cerberus cerb;
try {
cerb.init();
cout << "Initialized HidCerberus.\n";
}
catch (CerberusError &e) {
cout << "Unable to initialize Cerberus.\n";
cout << "Error: " << e.what() << '\n';
cout << "Continuing, may not find the controller if it's hidden by HidGuardian.\n";
}
#endif
std::vector<Controller> cs;
uchar port{ 0 };
{
constexpr auto id = Procon_ID; // Procon only for now
constexpr auto vendorId = NintendoID;
hid_device_info * const devs = hid_enumerate(vendorId, id); // Don't trust hidapi, returns non-matching devices sometimes (*const to prevent compiler from optimizing away)
hid_device_info *iter = devs;
do {
if (iter != nullptr) {
if (iter->product_id == id) { // Check the id!
try {
cs.emplace_back(Controller(port++));
cs.back().openDevice(iter);
}
catch (ControllerException &e) {
cout << "Exception connecting to controller: " << e.what() << '\n';
return -1;
}
}
iter = iter->next;
}
} while (iter != nullptr && port < 4);
hid_free_enumeration(devs);
}
if (cs.size() == 0) {
cout << "Unable to find controller.\n";
return -1;
}
cout << "\nConnected to " << static_cast<int>(port) << " controller(s). Beginning xInput emulation.\n\n";
cout << "Doing calibration, stick min/maxes will be updated automatically.\n";
cout << "Move the sticks some, then let them reset to neutral.\n";
cout << "Press the Share button to set stick center. This only works once per controller currently!\n";
cout << "XInput may be laggier before stick center for all controllers is set!\n\n";
cout << "Press CTRL+C to exit.\n\n";
::setBreakHandler();
std::array<bool, 4> hasCentered;
hasCentered.fill(false);
try {
// Testing to set centers, additional comparisons = slower so make it a separate loop
size_t countCentered{ 0 };
while (!::hasBroke && countCentered < port) {
for (size_t i = 0; i < port; ++i) {
cs[i].pollInput();
if (!hasCentered[i]) {
const Procon::ExpandedPadState &state = cs[i].getState();
if (state.sharePressed) {
cs[i].setCalibrationCenter(state.leftStick, state.rightStick);
hasCentered[i] = true;
++countCentered;
cout << "Set stick centers for controller LED " << i + 1 << '\n';
}
}
}
yield();
}
cout << "\nAll controller stick centers set, entering fast input loop. Enjoy your games!\n";
// Centers set, main input loop
while(!::hasBroke){
for (size_t i = 0; i < port; ++i) {
cs[i].pollInput();
}
yield(); // sleep_for causes big lag and not yielding eats way more processor
}
}
catch (ControllerException &e) {
cout << "ControllerException: " << e.what() << '\n';
return -1;
}
return 0;
}