forked from RPCS3/rpcs3
/
mm_joystick_handler.cpp
509 lines (426 loc) · 15.3 KB
/
mm_joystick_handler.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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#ifdef _WIN32
#include "mm_joystick_handler.h"
mm_joystick_handler::mm_joystick_handler() : PadHandlerBase(pad_handler::mm)
{
init_configs();
// Define border values
thumb_min = 0;
thumb_max = 255;
trigger_min = 0;
trigger_max = 255;
vibration_min = 0;
vibration_max = 65535;
// set capabilities
b_has_config = true;
b_has_rumble = true;
b_has_deadzones = true;
m_name_string = "Joystick #";
m_trigger_threshold = trigger_max / 2;
m_thumb_threshold = thumb_max / 2;
}
mm_joystick_handler::~mm_joystick_handler()
{
}
void mm_joystick_handler::init_config(pad_config* cfg, const std::string& name)
{
// Set this profile's save location
cfg->cfg_name = name;
// Set default button mapping
cfg->ls_left.def = axis_list.at(mmjoy_axis::joy_x_neg);
cfg->ls_down.def = axis_list.at(mmjoy_axis::joy_y_neg);
cfg->ls_right.def = axis_list.at(mmjoy_axis::joy_x_pos);
cfg->ls_up.def = axis_list.at(mmjoy_axis::joy_y_pos);
cfg->rs_left.def = axis_list.at(mmjoy_axis::joy_z_neg);
cfg->rs_down.def = axis_list.at(mmjoy_axis::joy_r_neg);
cfg->rs_right.def = axis_list.at(mmjoy_axis::joy_z_pos);
cfg->rs_up.def = axis_list.at(mmjoy_axis::joy_r_pos);
cfg->start.def = button_list.at(JOY_BUTTON9);
cfg->select.def = button_list.at(JOY_BUTTON10);
cfg->ps.def = button_list.at(JOY_BUTTON17);
cfg->square.def = button_list.at(JOY_BUTTON4);
cfg->cross.def = button_list.at(JOY_BUTTON3);
cfg->circle.def = button_list.at(JOY_BUTTON2);
cfg->triangle.def = button_list.at(JOY_BUTTON1);
cfg->left.def = pov_list.at(JOY_POVLEFT);
cfg->down.def = pov_list.at(JOY_POVBACKWARD);
cfg->right.def = pov_list.at(JOY_POVRIGHT);
cfg->up.def = pov_list.at(JOY_POVFORWARD);
cfg->r1.def = button_list.at(JOY_BUTTON8);
cfg->r2.def = button_list.at(JOY_BUTTON6);
cfg->r3.def = button_list.at(JOY_BUTTON12);
cfg->l1.def = button_list.at(JOY_BUTTON7);
cfg->l2.def = button_list.at(JOY_BUTTON5);
cfg->l3.def = button_list.at(JOY_BUTTON11);
// Set default misc variables
cfg->lstickdeadzone.def = 0; // between 0 and 255
cfg->rstickdeadzone.def = 0; // between 0 and 255
cfg->ltriggerthreshold.def = 0; // between 0 and 255
cfg->rtriggerthreshold.def = 0; // between 0 and 255
cfg->padsquircling.def = 8000;
// apply defaults
cfg->from_default();
}
bool mm_joystick_handler::Init()
{
if (is_init)
return true;
m_devices.clear();
u32 supported_joysticks = joyGetNumDevs();
if (supported_joysticks <= 0)
{
LOG_ERROR(GENERAL, "mmjoy: Driver doesn't support Joysticks");
return false;
}
LOG_NOTICE(GENERAL, "mmjoy: Driver supports %u joysticks", supported_joysticks);
for (u32 i = 0; i < supported_joysticks; i++)
{
MMJOYDevice dev;
if (GetMMJOYDevice(i, &dev) == false)
continue;
m_devices.emplace(i, dev);
}
is_init = true;
return true;
}
std::vector<std::string> mm_joystick_handler::ListDevices()
{
std::vector<std::string> devices;
if (!Init())
return devices;
for (auto dev : m_devices)
{
devices.emplace_back(dev.second.device_name);
}
return devices;
}
std::array<u32, PadHandlerBase::button::button_count> mm_joystick_handler::get_mapped_key_codes(const std::shared_ptr<PadDevice>& device, const pad_config* profile)
{
std::array<u32, button::button_count> mapping{ 0 };
auto joy_device = std::static_pointer_cast<MMJOYDevice>(device);
if (!joy_device)
return mapping;
auto find_key = [=](const cfg::string& name)
{
long key = FindKeyCode(button_list, name, false);
if (key < 0)
key = FindKeyCode(pov_list, name, false);
if (key < 0)
key = FindKeyCode(axis_list, name);
return static_cast<u64>(key);
};
joy_device->trigger_left = find_key(profile->l2);
joy_device->trigger_right = find_key(profile->r2);
joy_device->axis_left[0] = find_key(profile->ls_left);
joy_device->axis_left[1] = find_key(profile->ls_right);
joy_device->axis_left[2] = find_key(profile->ls_down);
joy_device->axis_left[3] = find_key(profile->ls_up);
joy_device->axis_right[0] = find_key(profile->rs_left);
joy_device->axis_right[1] = find_key(profile->rs_right);
joy_device->axis_right[2] = find_key(profile->rs_down);
joy_device->axis_right[3] = find_key(profile->rs_up);
mapping[button::up] = static_cast<u32>(find_key(profile->up));
mapping[button::down] = static_cast<u32>(find_key(profile->down));
mapping[button::left] = static_cast<u32>(find_key(profile->left));
mapping[button::right] = static_cast<u32>(find_key(profile->right));
mapping[button::cross] = static_cast<u32>(find_key(profile->cross));
mapping[button::square] = static_cast<u32>(find_key(profile->square));
mapping[button::circle] = static_cast<u32>(find_key(profile->circle));
mapping[button::triangle] = static_cast<u32>(find_key(profile->triangle));
mapping[button::l1] = static_cast<u32>(find_key(profile->l1));
mapping[button::l2] = static_cast<u32>(joy_device->trigger_left);
mapping[button::l3] = static_cast<u32>(find_key(profile->l3));
mapping[button::r1] = static_cast<u32>(find_key(profile->r1));
mapping[button::r2] = static_cast<u32>(joy_device->trigger_right);
mapping[button::r3] = static_cast<u32>(find_key(profile->r3));
mapping[button::start] = static_cast<u32>(find_key(profile->start));
mapping[button::select] = static_cast<u32>(find_key(profile->select));
mapping[button::ps] = static_cast<u32>(find_key(profile->ps));
mapping[button::ls_left] = static_cast<u32>(joy_device->axis_left[0]);
mapping[button::ls_right] = static_cast<u32>(joy_device->axis_left[1]);
mapping[button::ls_down] = static_cast<u32>(joy_device->axis_left[2]);
mapping[button::ls_up] = static_cast<u32>(joy_device->axis_left[3]);
mapping[button::rs_left] = static_cast<u32>(joy_device->axis_right[0]);
mapping[button::rs_right] = static_cast<u32>(joy_device->axis_right[1]);
mapping[button::rs_down] = static_cast<u32>(joy_device->axis_right[2]);
mapping[button::rs_up] = static_cast<u32>(joy_device->axis_right[3]);
return mapping;
}
void mm_joystick_handler::get_next_button_press(const std::string& padId, const std::function<void(u16, std::string, std::string, std::array<int, 6>)>& callback, const std::function<void(std::string)>& fail_callback, bool get_blacklist, const std::vector<std::string>& buttons)
{
if (get_blacklist)
blacklist.clear();
if (!Init())
return fail_callback(padId);
static std::string cur_pad = "";
static int id = -1;
if (cur_pad != padId)
{
cur_pad = padId;
id = GetIDByName(padId);
if (id < 0)
{
LOG_ERROR(GENERAL, "MMJOY get_next_button_press for device [%s] failed with id = %d", padId, id);
return fail_callback(padId);
}
}
JOYINFOEX js_info;
JOYCAPS js_caps;
js_info.dwSize = sizeof(js_info);
js_info.dwFlags = JOY_RETURNALL;
joyGetDevCaps(id, &js_caps, sizeof(js_caps));
MMRESULT status = joyGetPosEx(id, &js_info);
switch (status)
{
case JOYERR_UNPLUGGED:
{
return fail_callback(padId);
}
case JOYERR_NOERROR:
{
auto data = GetButtonValues(js_info, js_caps);
// Check for each button in our list if its corresponding (maybe remapped) button or axis was pressed.
// Return the new value if the button was pressed (aka. its value was bigger than 0 or the defined threshold)
// Use a pair to get all the legally pressed buttons and use the one with highest value (prioritize first)
std::pair<u16, std::string> pressed_button = { 0, "" };
for (const auto& button : axis_list)
{
u64 keycode = button.first;
u16 value = data[keycode];
if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), keycode) != blacklist.end())
continue;
if (value > m_thumb_threshold)
{
if (get_blacklist)
{
blacklist.emplace_back(keycode);
LOG_ERROR(HLE, "MMJOY Calibration: Added axis [ %d = %s ] to blacklist. Value = %d", keycode, button.second, value);
}
else if (value > pressed_button.first)
pressed_button = { value, button.second };
}
}
for (const auto& button : pov_list)
{
u64 keycode = button.first;
u16 value = data[keycode];
if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), keycode) != blacklist.end())
continue;
if (value > 0)
{
if (get_blacklist)
{
blacklist.emplace_back(keycode);
LOG_ERROR(HLE, "MMJOY Calibration: Added pov [ %d = %s ] to blacklist. Value = %d", keycode, button.second, value);
}
else if (value > pressed_button.first)
pressed_button = { value, button.second };
}
}
for (const auto& button : button_list)
{
u64 keycode = button.first;
u16 value = data[keycode];
if (!get_blacklist && std::find(blacklist.begin(), blacklist.end(), keycode) != blacklist.end())
continue;
if (value > 0)
{
if (get_blacklist)
{
blacklist.emplace_back(keycode);
LOG_ERROR(HLE, "MMJOY Calibration: Added button [ %d = %s ] to blacklist. Value = %d", keycode, button.second, value);
}
else if (value > pressed_button.first)
pressed_button = { value, button.second };
}
}
if (get_blacklist)
{
if (blacklist.empty())
LOG_SUCCESS(HLE, "MMJOY Calibration: Blacklist is clear. No input spam detected");
return;
}
auto find_key = [=](const std::string& name)
{
long key = FindKeyCodeByString(axis_list, name, false);
if (key < 0)
key = FindKeyCodeByString(pov_list, name, false);
if (key < 0)
key = FindKeyCodeByString(button_list, name);
return static_cast<u64>(key);
};
std::array<int, 6> preview_values = { 0, 0, 0, 0, 0, 0 };
if (buttons.size() == 10)
{
preview_values[0] = data[find_key(buttons[0])];
preview_values[1] = data[find_key(buttons[1])];
preview_values[2] = data[find_key(buttons[3])] - data[find_key(buttons[2])];
preview_values[3] = data[find_key(buttons[5])] - data[find_key(buttons[4])];
preview_values[4] = data[find_key(buttons[7])] - data[find_key(buttons[6])];
preview_values[5] = data[find_key(buttons[9])] - data[find_key(buttons[8])];
}
if (pressed_button.first > 0)
return callback(pressed_button.first, pressed_button.second, padId, preview_values);
else
return callback(0, "", padId, preview_values);
break;
}
default:
break;
}
}
std::unordered_map<u64, u16> mm_joystick_handler::GetButtonValues(const JOYINFOEX& js_info, const JOYCAPS& js_caps)
{
std::unordered_map<u64, u16> button_values;
for (auto entry : button_list)
{
button_values.emplace(entry.first, js_info.dwButtons & entry.first ? 255 : 0);
}
if (js_caps.wCaps & JOYCAPS_HASPOV)
{
if (js_caps.wCaps & JOYCAPS_POVCTS)
{
if (js_info.dwPOV == JOY_POVCENTERED)
{
button_values.emplace(JOY_POVFORWARD, 0);
button_values.emplace(JOY_POVRIGHT, 0);
button_values.emplace(JOY_POVBACKWARD, 0);
button_values.emplace(JOY_POVLEFT, 0);
}
else
{
auto emplacePOVs = [&](float val, u64 pov_neg, u64 pov_pos)
{
if (val < 0)
{
button_values.emplace(pov_neg, static_cast<u16>(std::abs(val)));
button_values.emplace(pov_pos, 0);
}
else
{
button_values.emplace(pov_neg, 0);
button_values.emplace(pov_pos, static_cast<u16>(val));
}
};
float rad = static_cast<float>(js_info.dwPOV / 100 * acos(-1) / 180);
emplacePOVs(cosf(rad) * 255.0f, JOY_POVBACKWARD, JOY_POVFORWARD);
emplacePOVs(sinf(rad) * 255.0f, JOY_POVLEFT, JOY_POVRIGHT);
}
}
else if (js_caps.wCaps & JOYCAPS_POV4DIR)
{
int val = static_cast<int>(js_info.dwPOV);
auto emplacePOV = [&button_values, &val](int pov)
{
int cw = pov + 4500, ccw = pov - 4500;
bool pressed = (val == pov) || (val == cw) || (ccw < 0 ? val == 36000 - std::abs(ccw) : val == ccw);
button_values.emplace(pov, pressed ? 255 : 0);
};
emplacePOV(JOY_POVFORWARD);
emplacePOV(JOY_POVRIGHT);
emplacePOV(JOY_POVBACKWARD);
emplacePOV(JOY_POVLEFT);
}
}
auto add_axis_value = [&](DWORD axis, UINT min, UINT max, u64 pos, u64 neg)
{
float val = ScaleStickInput2(axis, min, max);
if (val < 0)
{
button_values.emplace(pos, 0);
button_values.emplace(neg, static_cast<u16>(std::abs(val)));
}
else
{
button_values.emplace(pos, static_cast<u16>(val));
button_values.emplace(neg, 0);
}
};
add_axis_value(js_info.dwXpos, js_caps.wXmin, js_caps.wXmax, mmjoy_axis::joy_x_pos, mmjoy_axis::joy_x_neg);
add_axis_value(js_info.dwYpos, js_caps.wYmin, js_caps.wYmax, mmjoy_axis::joy_y_pos, mmjoy_axis::joy_y_neg);
if (js_caps.wCaps & JOYCAPS_HASZ)
add_axis_value(js_info.dwZpos, js_caps.wZmin, js_caps.wZmax, mmjoy_axis::joy_z_pos, mmjoy_axis::joy_z_neg);
if (js_caps.wCaps & JOYCAPS_HASR)
add_axis_value(js_info.dwRpos, js_caps.wRmin, js_caps.wRmax, mmjoy_axis::joy_r_pos, mmjoy_axis::joy_r_neg);
if (js_caps.wCaps & JOYCAPS_HASU)
add_axis_value(js_info.dwUpos, js_caps.wUmin, js_caps.wUmax, mmjoy_axis::joy_u_pos, mmjoy_axis::joy_u_neg);
if (js_caps.wCaps & JOYCAPS_HASV)
add_axis_value(js_info.dwVpos, js_caps.wVmin, js_caps.wVmax, mmjoy_axis::joy_v_pos, mmjoy_axis::joy_v_neg);
return button_values;
}
std::unordered_map<u64, u16> mm_joystick_handler::get_button_values(const std::shared_ptr<PadDevice>& device)
{
auto dev = std::static_pointer_cast<MMJOYDevice>(device);
if (!dev)
return std::unordered_map<u64, u16>();
return GetButtonValues(dev->device_info, dev->device_caps);
}
int mm_joystick_handler::GetIDByName(const std::string& name)
{
for (auto dev : m_devices)
{
if (dev.second.device_name == name)
return dev.first;
}
return -1;
}
bool mm_joystick_handler::GetMMJOYDevice(int index, MMJOYDevice* dev)
{
if (!dev)
return false;
JOYINFOEX js_info;
JOYCAPS js_caps;
js_info.dwSize = sizeof(js_info);
js_info.dwFlags = JOY_RETURNALL;
joyGetDevCaps(index, &js_caps, sizeof(js_caps));
dev->device_status = joyGetPosEx(index, &js_info);
if (dev->device_status != JOYERR_NOERROR)
return false;
char drv[32];
wcstombs(drv, js_caps.szPname, 31);
LOG_NOTICE(GENERAL, "Joystick nr.%d found. Driver: %s", index, drv);
dev->device_id = index;
dev->device_name = m_name_string + std::to_string(index + 1); // Controllers 1-n in GUI
dev->device_info = js_info;
dev->device_caps = js_caps;
return true;
}
std::shared_ptr<PadDevice> mm_joystick_handler::get_device(const std::string& device)
{
if (!Init())
return false;
int id = GetIDByName(device);
if (id < 0)
return false;
std::shared_ptr<MMJOYDevice> joy_device = std::make_shared<MMJOYDevice>(m_devices.at(id));
return joy_device;
}
bool mm_joystick_handler::get_is_left_trigger(u64 keyCode)
{
return m_dev && m_dev->trigger_left == keyCode;
}
bool mm_joystick_handler::get_is_right_trigger(u64 keyCode)
{
return m_dev && m_dev->trigger_right == keyCode;
}
bool mm_joystick_handler::get_is_left_stick(u64 keyCode)
{
return m_dev && std::find(m_dev->axis_left.begin(), m_dev->axis_left.end(), keyCode) != m_dev->axis_left.end();
}
bool mm_joystick_handler::get_is_right_stick(u64 keyCode)
{
return m_dev && std::find(m_dev->axis_right.begin(), m_dev->axis_right.end(), keyCode) != m_dev->axis_right.end();
}
PadHandlerBase::connection mm_joystick_handler::update_connection(const std::shared_ptr<PadDevice>& device)
{
auto dev = std::static_pointer_cast<MMJOYDevice>(device);
if (!dev)
return connection::disconnected;
const auto old_status = dev->device_status;
dev->device_status = joyGetPosEx(dev->device_id, &dev->device_info);
if (dev->device_status == JOYERR_NOERROR && (old_status == JOYERR_NOERROR || GetMMJOYDevice(dev->device_id, dev.get())))
{
return connection::connected;
}
return connection::disconnected;
}
#endif