-
-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathcontroller_management.cpp
More file actions
367 lines (314 loc) · 13.5 KB
/
controller_management.cpp
File metadata and controls
367 lines (314 loc) · 13.5 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Copyright (c) 2020-2024 ndeadly
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "controller_management.hpp"
#include <stratosphere.hpp>
#include "../utils.hpp"
#include "../mcmitm_config.hpp"
namespace ams::controller {
namespace {
const std::string OfficialGamepadNames[] = {
"NintendoGamepad",
"Joy-Con (L)",
"Joy-Con (R)",
"Pro Controller",
"Lic Pro Controller",
"NES Controller",
"HVC Controller",
"SNES Controller",
"N64 Controller",
"MD/Gen Control Pad",
"Lic2 Pro Controller",
};
constexpr u8 DeviceClassMajorPeripheral = 0x05;
constexpr u8 DeviceClassMinorGamepad = 0x08;
constexpr u8 DeviceClassMinorJoystick = 0x04;
constexpr u8 DeviceClassMinorKeyboard = 0x40;
constinit os::SdkMutex g_controller_lock;
std::vector<std::shared_ptr<SwitchController>> g_controllers;
}
ControllerType Identify(const bluetooth::DevicesSettings *device) {
for (auto hwId : SwitchController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Switch;
}
}
// Additionally check controller name against known official Nintendo controllers, as some controllers (eg. JoyCons paired via rails) don't report the correct vid/pid
if (IsOfficialSwitchControllerName(hos::GetVersion() < hos::Version_13_0_0 ? device->name.name : device->name2))
return ControllerType_Switch;
for (auto hwId : WiiController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Wii;
}
}
for (auto hwId : Dualshock3Controller::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Dualshock3;
}
}
for (auto hwId : Dualshock4Controller::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Dualshock4;
}
}
for (auto hwId : DualsenseController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Dualsense;
}
}
for (auto hwId : XboxOneController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_XboxOne;
}
}
for (auto hwId : OuyaController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Ouya;
}
}
for (auto hwId : GamestickController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Gamestick;
}
}
for (auto hwId : GemboxController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Gembox;
}
}
for (auto hwId : IpegaController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Ipega;
}
}
for (auto hwId : XiaomiController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Xiaomi;
}
}
for (auto hwId : GamesirController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Gamesir;
}
}
for (auto hwId : SteelseriesController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Steelseries;
}
}
for (auto hwId : NvidiaShieldController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_NvidiaShield;
}
}
for (auto hwId : EightBitDoController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_8BitDo;
}
}
for (auto hwId : PowerAController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_PowerA;
}
}
for (auto hwId : MadCatzController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_MadCatz;
}
}
for (auto hwId : MocuteController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Mocute;
}
}
for (auto hwId : RazerController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Razer;
}
}
for (auto hwId : ICadeController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_ICade;
}
}
for (auto hwId : LanShenController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_LanShen;
}
}
for (auto hwId : AtGamesController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_AtGames;
}
}
for (auto hwId : HyperkinController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Hyperkin;
}
}
for (auto hwId : BetopController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Betop;
}
}
for (auto hwId : AtariController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Atari;
}
}
for (auto hwId : BionikController::hardware_ids) {
if ( (device->vid == hwId.vid) && (device->pid == hwId.pid) ) {
return ControllerType_Bionik;
}
}
return ControllerType_Unknown;
}
bool IsAllowedDeviceClass(const bluetooth::DeviceClass *cod) {
return ((cod->class_of_device[1] & 0x0f) == DeviceClassMajorPeripheral) &&
(((cod->class_of_device[2] & 0x0f) == DeviceClassMinorGamepad) || ((cod->class_of_device[2] & 0x0f) == DeviceClassMinorJoystick) || ((cod->class_of_device[2] & 0x40) == DeviceClassMinorKeyboard));
}
bool IsOfficialSwitchControllerName(const std::string& name) {
for (auto n : OfficialGamepadNames) {
if (name.rfind(n, 0) == 0)
return true;
}
return false;
}
bool IsNotJoyconOrProController(const std::string& name) {
std::string JoyconName = "Joy-Con";
std::string ProControllerName = "Pro Controller";
if (name.rfind(JoyconName) == 0 || name.rfind(ProControllerName) == 0) {
return false;
}
return true;
}
void AttachHandler(const bluetooth::Address *address) {
bluetooth::DevicesSettings device_settings;
R_ABORT_UNLESS(btdrvGetPairedDeviceInfo(*address, &device_settings));
HardwareID id = { device_settings.vid, device_settings.pid };
std::shared_ptr<SwitchController> controller;
switch (Identify(&device_settings)) {
case ControllerType_Switch:
if (mitm::GetGlobalConfig()->misc.force_pro_controller && IsNotJoyconOrProController(hos::GetVersion() < hos::Version_13_0_0 ? device_settings.name.name : device_settings.name2))
controller = std::make_shared<ForcedProController>(address, id);
else controller = std::make_shared<SwitchController>(address, id);
break;
case ControllerType_Wii:
controller = std::make_shared<WiiController>(address, id);
break;
case ControllerType_Dualshock3:
controller = std::make_shared<Dualshock3Controller>(address, id);
break;
case ControllerType_Dualshock4:
controller = std::make_shared<Dualshock4Controller>(address, id);
break;
case ControllerType_Dualsense:
controller = std::make_shared<DualsenseController>(address, id);
break;
case ControllerType_XboxOne:
controller = std::make_shared<XboxOneController>(address, id);
break;
case ControllerType_Ouya:
controller = std::make_shared<OuyaController>(address, id);
break;
case ControllerType_Gamestick:
controller = std::make_shared<GamestickController>(address, id);
break;
case ControllerType_Gembox:
controller = std::make_shared<GemboxController>(address, id);
break;
case ControllerType_Ipega:
controller = std::make_shared<IpegaController>(address, id);
break;
case ControllerType_Xiaomi:
controller = std::make_shared<XiaomiController>(address, id);
break;
case ControllerType_Gamesir:
controller = std::make_shared<GamesirController>(address, id);
break;
case ControllerType_Steelseries:
controller = std::make_shared<SteelseriesController>(address, id);
break;
case ControllerType_NvidiaShield:
controller = std::make_shared<NvidiaShieldController>(address, id);
break;
case ControllerType_8BitDo:
controller = std::make_shared<EightBitDoController>(address, id);
break;
case ControllerType_PowerA:
controller = std::make_shared<PowerAController>(address, id);
break;
case ControllerType_MadCatz:
controller = std::make_shared<MadCatzController>(address, id);
break;
case ControllerType_Mocute:
controller = std::make_shared<MocuteController>(address, id);
break;
case ControllerType_Razer:
controller = std::make_shared<RazerController>(address, id);
break;
case ControllerType_ICade:
controller = std::make_shared<ICadeController>(address, id);
break;
case ControllerType_LanShen:
controller = std::make_shared<LanShenController>(address, id);
break;
case ControllerType_AtGames:
controller = std::make_shared<AtGamesController>(address, id);
break;
case ControllerType_Hyperkin:
controller = std::make_shared<HyperkinController>(address, id);
break;
case ControllerType_Betop:
controller = std::make_shared<BetopController>(address, id);
break;
case ControllerType_Atari:
controller = std::make_shared<AtariController>(address, id);
break;
case ControllerType_Bionik:
controller = std::make_shared<BionikController>(address, id);
break;
default:
controller = std::make_shared<UnknownController>(address, id);
break;
}
{
std::scoped_lock lk(g_controller_lock);
g_controllers.push_back(controller);
}
if (R_FAILED(controller->Initialize())) {
// Try to disconnect the controller
btdrvCloseHidConnection(controller->Address());
}
}
void RemoveHandler(const bluetooth::Address *address) {
std::scoped_lock lk(g_controller_lock);
for (auto it = g_controllers.begin(); it < g_controllers.end(); ++it) {
if (utils::BluetoothAddressCompare(&(*it)->Address(), address)) {
g_controllers.erase(it);
return;
}
}
}
std::shared_ptr<SwitchController> LocateHandler(const bluetooth::Address *address) {
std::scoped_lock lk(g_controller_lock);
for (auto it = g_controllers.begin(); it < g_controllers.end(); ++it) {
if (utils::BluetoothAddressCompare(&(*it)->Address(), address)) {
return (*it);
}
}
return nullptr;
}
}