Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
stenzek
GitHub Repository: stenzek/duckstation
Path: blob/master/src/core/analog_controller.cpp
4214 views
1
// SPDX-FileCopyrightText: 2019-2025 Connor McLaughlin <[email protected]> and contributors.
2
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
3
4
#include "analog_controller.h"
5
#include "host.h"
6
#include "settings.h"
7
#include "system.h"
8
9
#include "util/imgui_manager.h"
10
#include "util/input_manager.h"
11
#include "util/state_wrapper.h"
12
13
#include "common/bitutils.h"
14
#include "common/log.h"
15
#include "common/string_util.h"
16
17
#include "IconsFontAwesome6.h"
18
#include "IconsPromptFont.h"
19
#include "fmt/format.h"
20
21
#include <cmath>
22
23
LOG_CHANNEL(Controller);
24
25
AnalogController::AnalogController(u32 index) : Controller(index)
26
{
27
m_status_byte = 0x5A;
28
m_axis_state.fill(0x80);
29
m_rumble_config.fill(0xFF);
30
}
31
32
AnalogController::~AnalogController() = default;
33
34
ControllerType AnalogController::GetType() const
35
{
36
return ControllerType::AnalogController;
37
}
38
39
bool AnalogController::InAnalogMode() const
40
{
41
return m_analog_mode;
42
}
43
44
void AnalogController::Reset()
45
{
46
m_command = Command::Idle;
47
m_command_step = 0;
48
m_rx_buffer.fill(0x00);
49
m_tx_buffer.fill(0x00);
50
m_analog_mode = false;
51
m_configuration_mode = false;
52
53
for (u32 i = 0; i < NUM_MOTORS; i++)
54
{
55
if (m_motor_state[i] != 0)
56
SetMotorState(i, 0);
57
}
58
59
m_dualshock_enabled = false;
60
ResetRumbleConfig();
61
62
m_status_byte = 0x5A;
63
64
if (m_force_analog_on_reset)
65
{
66
if (CanStartInAnalogMode(ControllerType::AnalogController))
67
SetAnalogMode(true, false);
68
}
69
}
70
71
bool AnalogController::DoState(StateWrapper& sw, bool apply_input_state)
72
{
73
if (!Controller::DoState(sw, apply_input_state))
74
return false;
75
76
const bool old_analog_mode = m_analog_mode;
77
MotorState motor_state = m_motor_state;
78
79
if (sw.GetVersion() < 76) [[unlikely]]
80
{
81
u8 unused_command_param = 0;
82
bool unused_legacy_rumble_unlocked = false;
83
84
sw.Do(&m_analog_mode);
85
sw.Do(&m_dualshock_enabled);
86
sw.DoEx(&unused_legacy_rumble_unlocked, 44, false);
87
sw.Do(&m_configuration_mode);
88
sw.Do(&unused_command_param);
89
sw.DoEx(&m_status_byte, 55, static_cast<u8>(0x5A));
90
91
u16 button_state = m_button_state;
92
sw.DoEx(&button_state, 44, static_cast<u16>(0xFFFF));
93
if (apply_input_state)
94
m_button_state = button_state;
95
96
sw.Do(&m_command);
97
98
int unused_rumble_config_large_motor_index = -1;
99
int unused_rumble_config_small_motor_index = -1;
100
101
sw.DoEx(&m_rumble_config, 45, {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF});
102
sw.DoEx(&unused_rumble_config_large_motor_index, 45, -1);
103
sw.DoEx(&unused_rumble_config_small_motor_index, 45, -1);
104
sw.DoEx(&m_analog_toggle_queued, 45, false);
105
106
sw.Do(&motor_state);
107
}
108
else
109
{
110
sw.Do(&m_command);
111
sw.Do(&m_command_step);
112
sw.Do(&m_response_length);
113
sw.DoBytes(m_rx_buffer.data(), m_rx_buffer.size());
114
sw.DoBytes(m_tx_buffer.data(), m_tx_buffer.size());
115
sw.Do(&m_analog_mode);
116
sw.Do(&m_analog_locked);
117
sw.Do(&m_dualshock_enabled);
118
sw.Do(&m_configuration_mode);
119
sw.DoBytes(m_rumble_config.data(), m_rumble_config.size());
120
sw.Do(&m_status_byte);
121
// sw.Do(&m_digital_mode_extra_halfwords); // always zero
122
123
auto axis_state = m_axis_state;
124
u16 button_state = m_button_state;
125
sw.DoBytes(axis_state.data(), axis_state.size());
126
sw.Do(&button_state);
127
sw.Do(&motor_state);
128
129
if (apply_input_state)
130
{
131
m_axis_state = axis_state;
132
m_button_state = button_state;
133
}
134
}
135
136
if (sw.IsReading())
137
{
138
for (u8 i = 0; i < NUM_MOTORS; i++)
139
SetMotorState(i, motor_state[i]);
140
141
if (old_analog_mode != m_analog_mode)
142
{
143
Host::AddIconOSDMessage(fmt::format("Controller{}AnalogMode", m_index), ICON_FA_GAMEPAD,
144
fmt::format(m_analog_mode ?
145
TRANSLATE_FS("AnalogController", "Controller {} switched to analog mode.") :
146
TRANSLATE_FS("AnalogController", "Controller {} switched to digital mode."),
147
m_index + 1u),
148
5.0f);
149
}
150
}
151
return true;
152
}
153
154
float AnalogController::GetBindState(u32 index) const
155
{
156
if (index >= static_cast<u32>(Button::Count))
157
{
158
const u32 sub_index = index - static_cast<u32>(Button::Count);
159
if (sub_index >= static_cast<u32>(m_half_axis_state.size()))
160
return 0.0f;
161
162
return static_cast<float>(m_half_axis_state[sub_index]) * (1.0f / 255.0f);
163
}
164
else if (index < static_cast<u32>(Button::Analog))
165
{
166
return static_cast<float>(((m_button_state >> index) & 1u) ^ 1u);
167
}
168
else
169
{
170
return 0.0f;
171
}
172
}
173
174
float AnalogController::GetVibrationMotorState(u32 index) const
175
{
176
return ((index < m_motor_state.size()) ? m_motor_state[index] : 0) * (1.0f / 255.0f);
177
}
178
179
void AnalogController::SetBindState(u32 index, float value)
180
{
181
if (index == static_cast<s32>(Button::Analog))
182
{
183
// analog toggle
184
if (value >= m_button_deadzone)
185
{
186
if (m_command == Command::Idle)
187
ProcessAnalogModeToggle();
188
else
189
m_analog_toggle_queued = true;
190
}
191
192
return;
193
}
194
else if (index >= static_cast<u32>(Button::Count))
195
{
196
const u32 sub_index = index - static_cast<u32>(Button::Count);
197
if (sub_index >= static_cast<u32>(m_half_axis_state.size()))
198
return;
199
200
const u8 u8_value = static_cast<u8>(std::clamp(value * m_analog_sensitivity * 255.0f, 0.0f, 255.0f));
201
if (u8_value == m_half_axis_state[sub_index])
202
return;
203
204
m_half_axis_state[sub_index] = u8_value;
205
System::SetRunaheadReplayFlag();
206
207
#define MERGE(pos, neg) \
208
((m_half_axis_state[static_cast<u32>(pos)] != 0) ? (127u + ((m_half_axis_state[static_cast<u32>(pos)] + 1u) / 2u)) : \
209
(127u - (m_half_axis_state[static_cast<u32>(neg)] / 2u)))
210
switch (static_cast<HalfAxis>(sub_index))
211
{
212
case HalfAxis::LLeft:
213
case HalfAxis::LRight:
214
m_axis_state[static_cast<u8>(Axis::LeftX)] = ((m_invert_left_stick & 1u) != 0u) ?
215
MERGE(HalfAxis::LLeft, HalfAxis::LRight) :
216
MERGE(HalfAxis::LRight, HalfAxis::LLeft);
217
break;
218
219
case HalfAxis::LDown:
220
case HalfAxis::LUp:
221
m_axis_state[static_cast<u8>(Axis::LeftY)] = ((m_invert_left_stick & 2u) != 0u) ?
222
MERGE(HalfAxis::LUp, HalfAxis::LDown) :
223
MERGE(HalfAxis::LDown, HalfAxis::LUp);
224
break;
225
226
case HalfAxis::RLeft:
227
case HalfAxis::RRight:
228
m_axis_state[static_cast<u8>(Axis::RightX)] = ((m_invert_right_stick & 1u) != 0u) ?
229
MERGE(HalfAxis::RLeft, HalfAxis::RRight) :
230
MERGE(HalfAxis::RRight, HalfAxis::RLeft);
231
break;
232
233
case HalfAxis::RDown:
234
case HalfAxis::RUp:
235
m_axis_state[static_cast<u8>(Axis::RightY)] = ((m_invert_right_stick & 2u) != 0u) ?
236
MERGE(HalfAxis::RUp, HalfAxis::RDown) :
237
MERGE(HalfAxis::RDown, HalfAxis::RUp);
238
break;
239
240
default:
241
break;
242
}
243
244
if (m_analog_deadzone > 0.0f)
245
{
246
#define MERGE_F(pos, neg) \
247
((m_half_axis_state[static_cast<u32>(pos)] != 0) ? \
248
(static_cast<float>(m_half_axis_state[static_cast<u32>(pos)]) / 255.0f) : \
249
(static_cast<float>(m_half_axis_state[static_cast<u32>(neg)]) / -255.0f))
250
251
float pos_x, pos_y;
252
if (static_cast<HalfAxis>(sub_index) < HalfAxis::RLeft)
253
{
254
pos_x = ((m_invert_left_stick & 1u) != 0u) ? MERGE_F(HalfAxis::LLeft, HalfAxis::LRight) :
255
MERGE_F(HalfAxis::LRight, HalfAxis::LLeft);
256
pos_y = ((m_invert_left_stick & 2u) != 0u) ? MERGE_F(HalfAxis::LUp, HalfAxis::LDown) :
257
MERGE_F(HalfAxis::LDown, HalfAxis::LUp);
258
}
259
else
260
{
261
pos_x = ((m_invert_right_stick & 1u) != 0u) ? MERGE_F(HalfAxis::RLeft, HalfAxis::RRight) :
262
MERGE_F(HalfAxis::RRight, HalfAxis::RLeft);
263
;
264
pos_y = ((m_invert_right_stick & 2u) != 0u) ? MERGE_F(HalfAxis::RUp, HalfAxis::RDown) :
265
MERGE_F(HalfAxis::RDown, HalfAxis::RUp);
266
}
267
268
if (InCircularDeadzone(m_analog_deadzone, pos_x, pos_y))
269
{
270
// Set to 127 (center).
271
if (static_cast<HalfAxis>(sub_index) < HalfAxis::RLeft)
272
m_axis_state[static_cast<u8>(Axis::LeftX)] = m_axis_state[static_cast<u8>(Axis::LeftY)] = 127;
273
else
274
m_axis_state[static_cast<u8>(Axis::RightX)] = m_axis_state[static_cast<u8>(Axis::RightY)] = 127;
275
}
276
#undef MERGE_F
277
}
278
279
#undef MERGE
280
281
return;
282
}
283
284
const u16 bit = u16(1) << static_cast<u8>(index);
285
286
if (value >= m_button_deadzone)
287
{
288
if (m_button_state & bit)
289
System::SetRunaheadReplayFlag();
290
291
m_button_state &= ~(bit);
292
}
293
else
294
{
295
if (!(m_button_state & bit))
296
System::SetRunaheadReplayFlag();
297
298
m_button_state |= bit;
299
}
300
}
301
302
u32 AnalogController::GetButtonStateBits() const
303
{
304
// flip bits, native data is active low
305
return m_button_state ^ 0xFFFF;
306
}
307
308
std::optional<u32> AnalogController::GetAnalogInputBytes() const
309
{
310
return m_axis_state[static_cast<size_t>(Axis::LeftY)] << 24 | m_axis_state[static_cast<size_t>(Axis::LeftX)] << 16 |
311
m_axis_state[static_cast<size_t>(Axis::RightY)] << 8 | m_axis_state[static_cast<size_t>(Axis::RightX)];
312
}
313
314
u32 AnalogController::GetInputOverlayIconColor() const
315
{
316
return m_analog_mode ? 0xFF2534F0u : 0xFFCCCCCCu;
317
}
318
319
void AnalogController::ResetTransferState()
320
{
321
if (m_analog_toggle_queued)
322
{
323
ProcessAnalogModeToggle();
324
m_analog_toggle_queued = false;
325
}
326
327
m_command = Command::Idle;
328
m_command_step = 0;
329
}
330
331
void AnalogController::SetAnalogMode(bool enabled, bool show_message)
332
{
333
if (m_analog_mode == enabled)
334
return;
335
336
m_analog_mode = enabled;
337
338
INFO_LOG("Controller {} switched to {} mode.", m_index + 1u, m_analog_mode ? "analog" : "digital");
339
if (show_message)
340
{
341
Host::AddIconOSDMessage(
342
fmt::format("Controller{}AnalogMode", m_index), ICON_PF_GAMEPAD_ALT,
343
m_analog_mode ? fmt::format(TRANSLATE_FS("Controller", "Controller {} switched to analog mode."), m_index + 1u) :
344
fmt::format(TRANSLATE_FS("Controller", "Controller {} switched to digital mode."), m_index + 1u),
345
Host::OSD_QUICK_DURATION);
346
}
347
}
348
349
void AnalogController::ProcessAnalogModeToggle()
350
{
351
if (m_analog_locked)
352
{
353
Host::AddIconOSDMessage(
354
fmt::format("Controller{}AnalogMode", m_index), ICON_PF_GAMEPAD_ALT,
355
fmt::format(m_analog_mode ?
356
TRANSLATE_FS("AnalogController", "Controller {} is locked to analog mode by the game.") :
357
TRANSLATE_FS("AnalogController", "Controller {} is locked to digital mode by the game."),
358
m_index + 1u),
359
Host::OSD_QUICK_DURATION);
360
}
361
else
362
{
363
SetAnalogMode(!m_analog_mode, true);
364
ResetRumbleConfig();
365
366
// Set status byte to 0 if we were previously in configuration mode, so that the game knows about the mode change.
367
if (m_dualshock_enabled)
368
{
369
// However, the problem with doing this unconditionally is that games like Tomb Raider have the loader menu
370
// put the pad into configuration mode, but not analog mode. So if the user toggles analog mode, the status
371
// byte ends up stuck at 0x00. As a workaround, clear out config/dualshock mode when the game isn't flagged
372
// as supporting the dualshock.
373
if (!m_analog_mode && !CanStartInAnalogMode(ControllerType::AnalogController))
374
{
375
WARNING_LOG("Resetting pad on digital->analog switch.");
376
m_configuration_mode = false;
377
m_dualshock_enabled = false;
378
m_status_byte = 0x5A;
379
}
380
else
381
{
382
m_status_byte = 0x00;
383
}
384
}
385
}
386
}
387
388
void AnalogController::SetMotorState(u32 motor, u8 value)
389
{
390
DebugAssert(motor < NUM_MOTORS);
391
if (m_motor_state[motor] != value)
392
{
393
m_motor_state[motor] = value;
394
UpdateHostVibration();
395
}
396
}
397
398
void AnalogController::UpdateHostVibration()
399
{
400
std::array<float, NUM_MOTORS> hvalues;
401
for (u32 motor = 0; motor < NUM_MOTORS; motor++)
402
{
403
// Small motor is only 0/1.
404
const u8 state =
405
(motor == SmallMotor) ? (((m_motor_state[SmallMotor] & 0x01) != 0x00) ? 255 : 0) : m_motor_state[LargeMotor];
406
407
// Curve from https://github.com/KrossX/Pokopom/blob/master/Pokopom/Input_XInput.cpp#L210
408
const double x = static_cast<double>(std::clamp<s32>(static_cast<s32>(state) + m_vibration_bias[motor], 0, 255));
409
const double strength = 0.006474549734772402 * std::pow(x, 3.0) - 1.258165252213538 * std::pow(x, 2.0) +
410
156.82454281087692 * x + 3.637978807091713e-11;
411
412
hvalues[motor] = (state != 0) ? static_cast<float>(strength / 65535.0) : 0.0f;
413
}
414
415
DEV_LOG("Set small motor to {}, large motor to {}", hvalues[SmallMotor], hvalues[LargeMotor]);
416
InputManager::SetPadVibrationIntensity(m_index, hvalues[LargeMotor], hvalues[SmallMotor]);
417
}
418
419
u16 AnalogController::GetExtraButtonMask() const
420
{
421
u16 mask = 0xFFFF;
422
423
static constexpr u8 NEG_THRESHOLD = static_cast<u8>(128.0f - (127.0 * 0.5f));
424
static constexpr u8 POS_THRESHOLD = static_cast<u8>(128.0f + (127.0 * 0.5f));
425
426
if (m_analog_dpad_in_digital_mode && !m_analog_mode && !m_configuration_mode)
427
{
428
const bool left = (m_axis_state[static_cast<u8>(Axis::LeftX)] <= NEG_THRESHOLD);
429
const bool right = (m_axis_state[static_cast<u8>(Axis::LeftX)] >= POS_THRESHOLD);
430
const bool up = (m_axis_state[static_cast<u8>(Axis::LeftY)] <= NEG_THRESHOLD);
431
const bool down = (m_axis_state[static_cast<u8>(Axis::LeftY)] >= POS_THRESHOLD);
432
433
mask = ~((static_cast<u16>(left) << static_cast<u8>(Button::Left)) |
434
(static_cast<u16>(right) << static_cast<u8>(Button::Right)) |
435
(static_cast<u16>(up) << static_cast<u8>(Button::Up)) |
436
(static_cast<u16>(down) << static_cast<u8>(Button::Down)));
437
}
438
439
if (m_analog_shoulder_buttons == 2 || (m_analog_shoulder_buttons == 1 && !m_analog_mode && !m_configuration_mode))
440
{
441
const bool left = (m_axis_state[static_cast<u8>(Axis::RightX)] <= NEG_THRESHOLD);
442
const bool right = (m_axis_state[static_cast<u8>(Axis::RightX)] >= POS_THRESHOLD);
443
444
mask &= ~((static_cast<u16>(left) << static_cast<u8>(Button::L1)) |
445
(static_cast<u16>(right) << static_cast<u8>(Button::R1)));
446
}
447
448
return mask;
449
}
450
451
void AnalogController::ResetRumbleConfig()
452
{
453
m_rumble_config.fill(0xFF);
454
SetMotorState(SmallMotor, 0);
455
SetMotorState(LargeMotor, 0);
456
}
457
458
u8 AnalogController::GetResponseNumHalfwords() const
459
{
460
if (m_configuration_mode || m_analog_mode)
461
return 0x3;
462
463
return (0x1 + m_digital_mode_extra_halfwords);
464
}
465
466
u8 AnalogController::GetModeID() const
467
{
468
if (m_configuration_mode)
469
return 0xF;
470
471
if (m_analog_mode)
472
return 0x7;
473
474
return 0x4;
475
}
476
477
u8 AnalogController::GetIDByte() const
478
{
479
return Truncate8((GetModeID() << 4) | GetResponseNumHalfwords());
480
}
481
482
void AnalogController::Poll()
483
{
484
// m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
485
m_tx_buffer[0] = GetIDByte();
486
m_tx_buffer[1] = m_status_byte;
487
488
const u16 button_state = m_button_state & GetExtraButtonMask();
489
m_tx_buffer[2] = Truncate8(button_state);
490
m_tx_buffer[3] = Truncate8(button_state >> 8);
491
492
if (m_analog_mode || m_configuration_mode)
493
{
494
m_tx_buffer[4] = m_axis_state[static_cast<u8>(Axis::RightX)];
495
m_tx_buffer[5] = m_axis_state[static_cast<u8>(Axis::RightY)];
496
m_tx_buffer[6] = m_axis_state[static_cast<u8>(Axis::LeftX)];
497
m_tx_buffer[7] = m_axis_state[static_cast<u8>(Axis::LeftY)];
498
}
499
else
500
{
501
m_tx_buffer[4] = 0;
502
m_tx_buffer[5] = 0;
503
m_tx_buffer[6] = 0;
504
m_tx_buffer[7] = 0;
505
}
506
}
507
508
bool AnalogController::Transfer(const u8 data_in, u8* data_out)
509
{
510
bool ack;
511
m_rx_buffer[m_command_step] = data_in;
512
513
switch (m_command)
514
{
515
case Command::Idle:
516
{
517
*data_out = 0xFF;
518
519
if (data_in == 0x01)
520
{
521
DEBUG_LOG("ACK controller access");
522
m_command = Command::Ready;
523
return true;
524
}
525
526
DEV_LOG("Unknown data_in = 0x{:02X}", data_in);
527
return false;
528
}
529
break;
530
531
case Command::Ready:
532
{
533
if (data_in == 0x42)
534
{
535
Assert(m_command_step == 0);
536
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
537
m_command = Command::ReadPad;
538
Poll();
539
}
540
else if (data_in == 0x43)
541
{
542
Assert(m_command_step == 0);
543
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
544
m_command = Command::ConfigModeSetMode;
545
if (!m_configuration_mode)
546
Poll();
547
else
548
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
549
}
550
else if (m_configuration_mode && data_in == 0x44)
551
{
552
Assert(m_command_step == 0);
553
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
554
m_command = Command::SetAnalogMode;
555
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
556
557
ResetRumbleConfig();
558
}
559
else if (m_configuration_mode && data_in == 0x45)
560
{
561
Assert(m_command_step == 0);
562
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
563
m_command = Command::GetAnalogMode;
564
m_tx_buffer = {GetIDByte(), m_status_byte, 0x01, 0x02, BoolToUInt8(m_analog_mode), 0x02, 0x01, 0x00};
565
}
566
else if (m_configuration_mode && data_in == 0x46)
567
{
568
Assert(m_command_step == 0);
569
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
570
m_command = Command::Command46;
571
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
572
}
573
else if (m_configuration_mode && data_in == 0x47)
574
{
575
Assert(m_command_step == 0);
576
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
577
m_command = Command::Command47;
578
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x02, 0x00, 0x01, 0x00};
579
}
580
else if (m_configuration_mode && data_in == 0x4C)
581
{
582
Assert(m_command_step == 0);
583
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
584
m_command = Command::Command4C;
585
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
586
}
587
else if (m_configuration_mode && data_in == 0x4D)
588
{
589
Assert(m_command_step == 0);
590
m_response_length = (GetResponseNumHalfwords() + 1) * 2;
591
m_command = Command::GetSetRumble;
592
m_tx_buffer = {GetIDByte(), m_status_byte, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
593
}
594
else
595
{
596
if (m_configuration_mode)
597
ERROR_LOG("Unimplemented config mode command 0x{:02X}", data_in);
598
599
*data_out = 0xFF;
600
return false;
601
}
602
}
603
break;
604
605
case Command::ReadPad:
606
{
607
if (m_dualshock_enabled)
608
{
609
if (m_command_step >= 2 && m_command_step < 7)
610
{
611
const u8 motor_to_set = m_rumble_config[m_command_step - 2];
612
if (motor_to_set <= LargeMotor)
613
SetMotorState(motor_to_set, data_in);
614
}
615
}
616
else if (m_command_step == 3)
617
{
618
const bool legacy_rumble_on = (m_rx_buffer[2] & 0xC0) == 0x40 && (m_rx_buffer[3] & 0x01) != 0;
619
SetMotorState(SmallMotor, legacy_rumble_on ? 255 : 0);
620
}
621
}
622
break;
623
624
case Command::ConfigModeSetMode:
625
{
626
if (m_command_step == (static_cast<s32>(m_response_length) - 1))
627
{
628
m_configuration_mode = (m_rx_buffer[2] == 1);
629
630
if (m_configuration_mode)
631
{
632
m_dualshock_enabled = true;
633
m_status_byte = 0x5A;
634
}
635
636
DEBUG_LOG("0x{:02x}({}) config mode", m_rx_buffer[2], m_configuration_mode ? "enter" : "leave");
637
}
638
}
639
break;
640
641
case Command::SetAnalogMode:
642
{
643
if (m_command_step == 2)
644
{
645
DEV_LOG("analog mode val 0x{:02x}", data_in);
646
647
if (data_in == 0x00 || data_in == 0x01)
648
SetAnalogMode((data_in == 0x01), true);
649
}
650
else if (m_command_step == 3)
651
{
652
DEV_LOG("analog mode lock 0x{:02x}", data_in);
653
654
if (data_in == 0x02 || data_in == 0x03)
655
m_analog_locked = (data_in == 0x03);
656
}
657
}
658
break;
659
660
case Command::GetAnalogMode:
661
{
662
// Intentionally empty, analog mode byte is set in reply buffer when command is first received
663
}
664
break;
665
666
case Command::Command46:
667
{
668
if (m_command_step == 2)
669
{
670
if (data_in == 0x00)
671
{
672
m_tx_buffer[4] = 0x01;
673
m_tx_buffer[5] = 0x02;
674
m_tx_buffer[6] = 0x00;
675
m_tx_buffer[7] = 0x0A;
676
}
677
else if (data_in == 0x01)
678
{
679
m_tx_buffer[4] = 0x01;
680
m_tx_buffer[5] = 0x01;
681
m_tx_buffer[6] = 0x01;
682
m_tx_buffer[7] = 0x14;
683
}
684
}
685
}
686
break;
687
688
case Command::Command47:
689
{
690
if (m_command_step == 2 && data_in != 0x00)
691
{
692
m_tx_buffer[4] = 0x00;
693
m_tx_buffer[5] = 0x00;
694
m_tx_buffer[6] = 0x00;
695
m_tx_buffer[7] = 0x00;
696
}
697
}
698
break;
699
700
case Command::Command4C:
701
{
702
if (m_command_step == 2)
703
{
704
if (data_in == 0x00)
705
m_tx_buffer[5] = 0x04;
706
else if (data_in == 0x01)
707
m_tx_buffer[5] = 0x07;
708
}
709
}
710
break;
711
712
case Command::GetSetRumble:
713
{
714
if (m_command_step >= 2 && m_command_step < 7)
715
{
716
const u8 index = m_command_step - 2;
717
m_tx_buffer[m_command_step] = m_rumble_config[index];
718
m_rumble_config[index] = data_in;
719
720
if (data_in == LargeMotor)
721
DEBUG_LOG("Large motor mapped to byte index {}", index);
722
else if (data_in == SmallMotor)
723
DEBUG_LOG("Small motor mapped to byte index {}", index);
724
}
725
else if (m_command_step == 7)
726
{
727
// reset motor value if we're no longer mapping it
728
bool has_small = false;
729
bool has_large = false;
730
for (size_t i = 0; i < m_rumble_config.size(); i++)
731
{
732
has_small |= (m_rumble_config[i] == SmallMotor);
733
has_large |= (m_rumble_config[i] == LargeMotor);
734
}
735
if (!has_small)
736
SetMotorState(SmallMotor, 0);
737
if (!has_large)
738
SetMotorState(LargeMotor, 0);
739
}
740
}
741
break;
742
743
DefaultCaseIsUnreachable();
744
}
745
746
*data_out = m_tx_buffer[m_command_step];
747
748
m_command_step = (m_command_step + 1) % m_response_length;
749
ack = (m_command_step == 0) ? false : true;
750
751
if (m_command_step == 0)
752
{
753
m_command = Command::Idle;
754
755
DEBUG_LOG("Rx: {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x}", m_rx_buffer[0], m_rx_buffer[1],
756
m_rx_buffer[2], m_rx_buffer[3], m_rx_buffer[4], m_rx_buffer[5], m_rx_buffer[6], m_rx_buffer[7]);
757
DEBUG_LOG("Tx: {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x} {:02x}", m_tx_buffer[0], m_tx_buffer[1],
758
m_tx_buffer[2], m_tx_buffer[3], m_tx_buffer[4], m_tx_buffer[5], m_tx_buffer[6], m_tx_buffer[7]);
759
760
m_rx_buffer.fill(0x00);
761
m_tx_buffer.fill(0x00);
762
}
763
764
return ack;
765
}
766
767
std::unique_ptr<AnalogController> AnalogController::Create(u32 index)
768
{
769
return std::make_unique<AnalogController>(index);
770
}
771
772
static const Controller::ControllerBindingInfo s_binding_info[] = {
773
#define BUTTON(name, display_name, icon_name, button, genb) \
774
{name, display_name, icon_name, static_cast<u32>(button), InputBindingInfo::Type::Button, genb}
775
#define AXIS(name, display_name, icon_name, halfaxis, genb) \
776
{name, \
777
display_name, \
778
icon_name, \
779
static_cast<u32>(AnalogController::Button::Count) + static_cast<u32>(halfaxis), \
780
InputBindingInfo::Type::HalfAxis, \
781
genb}
782
#define MOTOR(name, display_name, icon_name, index, genb) \
783
{name, display_name, icon_name, index, InputBindingInfo::Type::Motor, genb}
784
785
// clang-format off
786
BUTTON("Up", TRANSLATE_NOOP("AnalogController", "D-Pad Up"), ICON_PF_DPAD_UP, AnalogController::Button::Up, GenericInputBinding::DPadUp),
787
BUTTON("Right", TRANSLATE_NOOP("AnalogController", "D-Pad Right"), ICON_PF_DPAD_RIGHT, AnalogController::Button::Right, GenericInputBinding::DPadRight),
788
BUTTON("Down", TRANSLATE_NOOP("AnalogController", "D-Pad Down"), ICON_PF_DPAD_DOWN, AnalogController::Button::Down, GenericInputBinding::DPadDown),
789
BUTTON("Left", TRANSLATE_NOOP("AnalogController", "D-Pad Left"), ICON_PF_DPAD_LEFT, AnalogController::Button::Left, GenericInputBinding::DPadLeft),
790
BUTTON("Triangle", TRANSLATE_NOOP("AnalogController", "Triangle"), ICON_PF_BUTTON_TRIANGLE, AnalogController::Button::Triangle, GenericInputBinding::Triangle),
791
BUTTON("Circle", TRANSLATE_NOOP("AnalogController", "Circle"), ICON_PF_BUTTON_CIRCLE, AnalogController::Button::Circle, GenericInputBinding::Circle),
792
BUTTON("Cross", TRANSLATE_NOOP("AnalogController", "Cross"), ICON_PF_BUTTON_CROSS, AnalogController::Button::Cross, GenericInputBinding::Cross),
793
BUTTON("Square", TRANSLATE_NOOP("AnalogController", "Square"), ICON_PF_BUTTON_SQUARE, AnalogController::Button::Square, GenericInputBinding::Square),
794
BUTTON("Select", TRANSLATE_NOOP("AnalogController", "Select"), ICON_PF_SELECT_SHARE, AnalogController::Button::Select, GenericInputBinding::Select),
795
BUTTON("Start", TRANSLATE_NOOP("AnalogController", "Start"),ICON_PF_START, AnalogController::Button::Start, GenericInputBinding::Start),
796
BUTTON("Analog", TRANSLATE_NOOP("AnalogController", "Analog Toggle"), ICON_PF_ANALOG_LEFT_RIGHT, AnalogController::Button::Analog, GenericInputBinding::System),
797
BUTTON("L1", TRANSLATE_NOOP("AnalogController", "L1"), ICON_PF_LEFT_SHOULDER_L1, AnalogController::Button::L1, GenericInputBinding::L1),
798
BUTTON("R1", TRANSLATE_NOOP("AnalogController", "R1"), ICON_PF_RIGHT_SHOULDER_R1, AnalogController::Button::R1, GenericInputBinding::R1),
799
BUTTON("L2", TRANSLATE_NOOP("AnalogController", "L2"), ICON_PF_LEFT_TRIGGER_L2, AnalogController::Button::L2, GenericInputBinding::L2),
800
BUTTON("R2", TRANSLATE_NOOP("AnalogController", "R2"), ICON_PF_RIGHT_TRIGGER_R2, AnalogController::Button::R2, GenericInputBinding::R2),
801
BUTTON("L3", TRANSLATE_NOOP("AnalogController", "L3"), ICON_PF_LEFT_ANALOG_CLICK, AnalogController::Button::L3, GenericInputBinding::L3),
802
BUTTON("R3", TRANSLATE_NOOP("AnalogController", "R3"), ICON_PF_RIGHT_ANALOG_CLICK, AnalogController::Button::R3, GenericInputBinding::R3),
803
804
AXIS("LLeft", TRANSLATE_NOOP("AnalogController", "Left Stick Left"), ICON_PF_LEFT_ANALOG_LEFT, AnalogController::HalfAxis::LLeft, GenericInputBinding::LeftStickLeft),
805
AXIS("LRight", TRANSLATE_NOOP("AnalogController", "Left Stick Right"), ICON_PF_LEFT_ANALOG_RIGHT, AnalogController::HalfAxis::LRight, GenericInputBinding::LeftStickRight),
806
AXIS("LDown", TRANSLATE_NOOP("AnalogController", "Left Stick Down"), ICON_PF_LEFT_ANALOG_DOWN, AnalogController::HalfAxis::LDown, GenericInputBinding::LeftStickDown),
807
AXIS("LUp", TRANSLATE_NOOP("AnalogController", "Left Stick Up"), ICON_PF_LEFT_ANALOG_UP, AnalogController::HalfAxis::LUp, GenericInputBinding::LeftStickUp),
808
AXIS("RLeft", TRANSLATE_NOOP("AnalogController", "Right Stick Left"), ICON_PF_RIGHT_ANALOG_LEFT, AnalogController::HalfAxis::RLeft, GenericInputBinding::RightStickLeft),
809
AXIS("RRight", TRANSLATE_NOOP("AnalogController", "Right Stick Right"), ICON_PF_RIGHT_ANALOG_RIGHT, AnalogController::HalfAxis::RRight, GenericInputBinding::RightStickRight),
810
AXIS("RDown", TRANSLATE_NOOP("AnalogController", "Right Stick Down"), ICON_PF_RIGHT_ANALOG_DOWN, AnalogController::HalfAxis::RDown, GenericInputBinding::RightStickDown),
811
AXIS("RUp", TRANSLATE_NOOP("AnalogController", "Right Stick Up"), ICON_PF_RIGHT_ANALOG_UP, AnalogController::HalfAxis::RUp, GenericInputBinding::RightStickUp),
812
813
MOTOR("LargeMotor", TRANSLATE_NOOP("AnalogController", "Large Motor"), ICON_PF_VIBRATION_L, 0, GenericInputBinding::LargeMotor),
814
MOTOR("SmallMotor", TRANSLATE_NOOP("AnalogController", "Small Motor"), ICON_PF_VIBRATION, 1, GenericInputBinding::SmallMotor),
815
// clang-format on
816
817
#undef MOTOR
818
#undef AXIS
819
#undef BUTTON
820
};
821
822
static constexpr const char* s_invert_settings[] = {
823
TRANSLATE_NOOP("AnalogController", "Not Inverted"), TRANSLATE_NOOP("AnalogController", "Invert Left/Right"),
824
TRANSLATE_NOOP("AnalogController", "Invert Up/Down"),
825
TRANSLATE_NOOP("AnalogController", "Invert Left/Right + Up/Down"), nullptr};
826
827
static constexpr const char* s_shoulder_settings[] = {
828
TRANSLATE_NOOP("AnalogController", "Never"), TRANSLATE_NOOP("AnalogController", "Digital Mode Only"),
829
TRANSLATE_NOOP("AnalogController", "Analog and Digital Modes"), nullptr};
830
831
static const SettingInfo s_settings[] = {
832
{SettingInfo::Type::Boolean, "ForceAnalogOnReset", TRANSLATE_NOOP("AnalogController", "Force Analog Mode on Reset"),
833
TRANSLATE_NOOP("AnalogController", "Forces the controller to analog mode when the console is reset/powered on."),
834
"true", nullptr, nullptr, nullptr, nullptr, nullptr, 0.0f},
835
{SettingInfo::Type::Boolean, "AnalogDPadInDigitalMode",
836
TRANSLATE_NOOP("AnalogController", "Use Left Analog for D-Pad in Digital Mode"),
837
TRANSLATE_NOOP(
838
"AnalogController",
839
"Allows you to use the left analog stick to control the d-pad in digital mode, as well as the buttons."),
840
"true", nullptr, nullptr, nullptr, nullptr, nullptr, 0.0f},
841
{SettingInfo::Type::IntegerList, "AnalogShoulderButtons",
842
TRANSLATE_NOOP("AnalogController", "Use Right Analog for Shoulder Buttons"),
843
TRANSLATE_NOOP("AnalogController",
844
"Allows you to use the right analog stick to control the shoulder buttons, as well as the buttons."),
845
"0", "0", "2", nullptr, nullptr, s_shoulder_settings, 0.0f},
846
{SettingInfo::Type::Float, "AnalogDeadzone", TRANSLATE_NOOP("AnalogController", "Analog Deadzone"),
847
TRANSLATE_NOOP("AnalogController",
848
"Sets the analog stick deadzone, i.e. the fraction of the stick movement which will be ignored."),
849
"0", "0", "1", "0.01", "%.0f%%", nullptr, 100.0f},
850
{SettingInfo::Type::Float, "AnalogSensitivity", TRANSLATE_NOOP("AnalogController", "Analog Sensitivity"),
851
TRANSLATE_NOOP(
852
"AnalogController",
853
"Sets the analog stick axis scaling factor. A value between 130% and 140% is recommended when using recent "
854
"controllers, e.g. DualShock 4, Xbox One Controller."),
855
"1.33", "0.01", "2", "0.01", "%.0f%%", nullptr, 100.0f},
856
{SettingInfo::Type::Float, "ButtonDeadzone", TRANSLATE_NOOP("AnalogController", "Button/Trigger Deadzone"),
857
TRANSLATE_NOOP("AnalogController", "Sets the deadzone for activating buttons/triggers, "
858
"i.e. the fraction of the trigger which will be ignored."),
859
"0.25", "0.01", "1", "0.01", "%.0f%%", nullptr, 100.0f},
860
{SettingInfo::Type::Integer, "LargeMotorVibrationBias",
861
TRANSLATE_NOOP("AnalogController", "Large Motor Vibration Bias"),
862
TRANSLATE_NOOP("AnalogController",
863
"Sets the bias value for the large vibration motor. If vibration in some games is too weak or not "
864
"functioning, try increasing this value. Negative values will decrease the intensity of vibration."),
865
"8", "-255", "255", "1", "%d", nullptr, 1.0f},
866
{SettingInfo::Type::Integer, "SmallMotorVibrationBias",
867
TRANSLATE_NOOP("AnalogController", "Small Motor Vibration Bias"),
868
TRANSLATE_NOOP("AnalogController",
869
"Sets the bias value for the small vibration motor. If vibration in some games is too weak or not "
870
"functioning, try increasing this value. Negative values will decrease the intensity of vibration."),
871
"8", "-255", "255", "1", "%d", nullptr, 1.0f},
872
{SettingInfo::Type::IntegerList, "InvertLeftStick", TRANSLATE_NOOP("AnalogController", "Invert Left Stick"),
873
TRANSLATE_NOOP("AnalogController", "Inverts the direction of the left analog stick."), "0", "0", "3", nullptr,
874
nullptr, s_invert_settings, 0.0f},
875
{SettingInfo::Type::IntegerList, "InvertRightStick", TRANSLATE_NOOP("AnalogController", "Invert Right Stick"),
876
TRANSLATE_NOOP("AnalogController", "Inverts the direction of the right analog stick."), "0", "0", "3", nullptr,
877
nullptr, s_invert_settings, 0.0f},
878
};
879
880
const Controller::ControllerInfo AnalogController::INFO = {ControllerType::AnalogController,
881
"AnalogController",
882
TRANSLATE_NOOP("ControllerType", "Analog Controller"),
883
ICON_PF_GAMEPAD_ALT,
884
s_binding_info,
885
s_settings};
886
887
void AnalogController::LoadSettings(const SettingsInterface& si, const char* section, bool initial)
888
{
889
Controller::LoadSettings(si, section, initial);
890
m_force_analog_on_reset = si.GetBoolValue(section, "ForceAnalogOnReset", true);
891
m_analog_dpad_in_digital_mode = si.GetBoolValue(section, "AnalogDPadInDigitalMode", true);
892
m_analog_shoulder_buttons = static_cast<u8>(si.GetUIntValue(section, "AnalogShoulderButtons", 0u));
893
m_analog_deadzone = std::clamp(si.GetFloatValue(section, "AnalogDeadzone", DEFAULT_STICK_DEADZONE), 0.0f, 1.0f);
894
m_analog_sensitivity =
895
std::clamp(si.GetFloatValue(section, "AnalogSensitivity", DEFAULT_STICK_SENSITIVITY), 0.01f, 3.0f);
896
m_button_deadzone = std::clamp(si.GetFloatValue(section, "ButtonDeadzone", DEFAULT_BUTTON_DEADZONE), 0.01f, 1.0f);
897
m_vibration_bias[0] = static_cast<s16>(
898
std::clamp(si.GetIntValue(section, "LargeMotorVibrationBias", DEFAULT_LARGE_MOTOR_VIBRATION_BIAS), -255, 255));
899
m_vibration_bias[1] = static_cast<s16>(
900
std::clamp(si.GetIntValue(section, "SmallMotorVibrationBias", DEFAULT_SMALL_MOTOR_VIBRATION_BIAS), -255, 255));
901
m_invert_left_stick = static_cast<u8>(si.GetUIntValue(section, "InvertLeftStick", 0u));
902
m_invert_right_stick = static_cast<u8>(si.GetUIntValue(section, "InvertRightStick", 0u));
903
}
904
905