forked from TheWaveWarden/SwitchProConLinuxUSB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
271 lines (247 loc) · 7.78 KB
/
main.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "procon.hpp"
//#define DEBUG
#define TEST_BAD_DATA_CYCLES 10
int main(int argc, char *argv[]) {
bool help = false;
bool force_calibration = false;
bool show_version = false;
bool invert_lx = false;
bool invert_ly = false;
bool invert_rx = false;
bool invert_ry = false;
bool invert_dx = false;
bool invert_dy = false;
bool swap_buttons = false;
#ifdef DRIBBLE_MODE
int dribble_cam_value;
bool found_dribble_cam_value = false;
#endif
for (size_t i = 1; i < argc; ++i) {
// printf("argv: %d\n",argv[i]);
// std::cout << argv[i] << std::endl;
bool option_found = false;
if (std::string(argv[i]) == "-h" || std::string(argv[i]) == "--help") {
help = true;
option_found = true;
}
if (std::string(argv[i]) == "-c" ||
std::string(argv[i]) == "--calibration") {
force_calibration = true;
option_found = true;
}
if (std::string(argv[i]) == "--version") {
show_version = true;
option_found = true;
}
if (std::string(argv[i]) == "--invert-axis" ||
std::string(argv[i]) == "-i") {
if (i + 1 >= argc) {
ProController::red();
printf("Expected axis parameter. use --help for options!\n");
ProController::normal();
return -1;
}
option_found = true;
bool valid_axis_name;
do {
valid_axis_name = false;
if (std::string(argv[i + 1]) == "lx") {
invert_lx = true;
valid_axis_name = true;
} else if (std::string(argv[i + 1]) == "ly") {
invert_ly = true;
valid_axis_name = true;
} else if (std::string(argv[i + 1]) == "rx") {
invert_rx = true;
valid_axis_name = true;
} else if (std::string(argv[i + 1]) == "ry") {
invert_ry = true;
valid_axis_name = true;
} else if (std::string(argv[i + 1]) == "dx") {
invert_dx = true;
valid_axis_name = true;
} else if (std::string(argv[i + 1]) == "dy") {
invert_dy = true;
valid_axis_name = true;
}
if (valid_axis_name) {
++i;
}
} while (valid_axis_name && i + 1 < argc);
}
if (std::string(argv[i]) == "--swap_buttons" ||
std::string(argv[i]) == "-s") {
option_found = true;
swap_buttons = true;
}
#ifdef DRIBBLE_MODE
if (std::string(argv[i]) == "-d") {
option_found = true;
i++;
dribble_cam_value = std::stoi(argv[i]);
found_dribble_cam_value = true;
}
#endif
if (!option_found) {
std::cout << "Unknown option " << argv[i]
<< ". For usage, type 'procon_driver --help'" << std::endl;
return -1;
}
}
if (help) {
printf("Usage: procon_driver [OPTIONS]\noptions are:\n");
printf(" -h --help get help on usage at start\n");
printf(" -c --calibration force calibration at start\n");
printf(" -s --swap_buttons Swap A and B buttons and X and Y "
"buttons\n");
printf(" -i --invert-axis [AXIS] invert axis, possible axis: lx, ly, "
"rx, ry, dx, dy\n");
#ifdef DRIBBLE_MODE
printf(" -d [VALUE] pass parameter for dribble cam. Range "
"0 to 255\n");
#endif
printf("\n");
return 0;
}
if (show_version) {
std::cout << "Version is " << PROCON_DRIVER_VERSION << std::endl;
return 0;
}
printf("\n--------------------------------------------------------------------"
"------\n");
printf("| ");
printf("%c[%d;%dmNintendo Switch Pro-Controller USB uinput driver"
".%c[%dm ",
27, 1, 32, 27, 0);
printf("%c[%d;%dmVersion: ", 27, 1, 36);
printf(PROCON_DRIVER_VERSION);
printf("%c[%dm ", 27, 0);
printf("%s "
"|\n-------------------------------------------------------------------"
"-------",
KNRM);
printf("\n\n%s", KNRM);
#ifdef DRIBBLE_MODE
printf("%c[%d;%dmDribble mode enabled!%c[%dm \n\n", 27, 1, 36, 27, 0);
// if(found_dribble_cam_value) {
// printf("VALUE: %i", dribble_cam_value);
// }
#endif
fflush(stdout);
ProController controller;
hid_init();
hid_device *controller_ptr;
hid_device_info *devs =
hid_enumerate(NINTENDO_ID, PROCON_ID); // Don't trust hidapi, returns
// non-matching devices sometimes
// (*const to prevent compiler from
// optimizing away)
hid_device_info *iter = devs;
unsigned short n_controller = 0;
bool controller_found = false;
bool opened = false;
bool bad_data = false;
// pass arguments to controller
if (force_calibration) {
controller.read_calibration_from_file = false;
}
controller.invert_LX = invert_lx;
controller.invert_LY = invert_ly;
controller.invert_RX = invert_rx;
controller.invert_RY = invert_ry;
controller.invert_DX = invert_dx;
controller.invert_DY = invert_dy;
controller.swap_buttons = swap_buttons;
#ifdef DRIBBLE_MODE
if (found_dribble_cam_value) {
controller.dribble_mode_value = dribble_cam_value;
}
#endif
// OPEN PHASE
do {
opened = false;
bad_data = false;
if (iter != nullptr) {
if (iter->product_id == PROCON_ID && iter->vendor_id == NINTENDO_ID) {
// open & test for timeout in read!
int ret = controller.open_device(iter->vendor_id, iter->product_id,
iter->serial_number, n_controller + 1);
opened = ret == 0;
if (!opened) { // read timed out
if (ret == -1) {
ProController::red();
printf("Invalid device pointer. Aborting!\n");
ProController::normal();
return -1;
}
ProController::magenta();
printf("Failed to open controller, error code %d, trying again...\n",
ret);
ProController::normal();
controller.close_device();
usleep(1000 * 10);
continue;
} else {
// TEST FOR BAD DATA
for (size_t i = 0; i < TEST_BAD_DATA_CYCLES; ++i) {
if (controller.try_read_bad_data() != 0) {
ProController::magenta();
printf("Detected bad data stream. Trying again...\n");
ProController::normal();
controller.close_device();
bad_data = true;
usleep(1000 * 10);
break;
}
}
}
}
} else {
ProController::red();
printf("No controller found...\n");
ProController::normal();
return -1;
}
} while (!opened || bad_data);
if (controller.is_opened) {
ProController::green();
printf("Opened controller!\n");
if (controller.uinput_create() < 0) {
ProController::red();
printf("Failed to open uinput device!\n");
ProController::normal();
}
if (!controller.read_calibration_from_file ||
!controller.calibration_file_exists()) {
ProController::blue();
printf("Now entering calibration mode. \n");
ProController::cyan();
printf("%c[%d;%dmMove your analog sticks to the maxima, then press the "
"square 'share' button!\n%c[%dm",
27, 1, 36, 27, 0);
ProController::normal();
}
}
// controller.u_setup();
while (true) {
if (!controller.calibrated) {
while (!controller.calibrated) {
controller.calibrate();
}
ProController::green();
printf("Calibrated Controller! Now entering input mode!\n");
ProController::normal();
}
if (controller.is_opened) {
if (controller.poll_input() < 0)
return -1;
}
}
// hid_exit;
for (short unsigned i = 0; i < MAX_N_CONTROLLERS; ++i) {
controller.close_device();
controller.uinput_destroy();
}
printf("\n");
return 0;
}