Improvements to haptic error handling

- Move gamepad index check to the JS side to simplify the code
- Handle gamepads that don't support haptics
This commit is contained in:
Cameron Gutman 2022-11-06 20:20:45 -06:00
parent ce52398dc5
commit a191915b9d
2 changed files with 4 additions and 9 deletions

View File

@ -132,14 +132,6 @@ void MoonlightInstance::PollGamepads() {
void MoonlightInstance::ClControllerRumble(unsigned short controllerNumber, unsigned short lowFreqMotor, unsigned short highFreqMotor)
{
PP_GamepadsSampleData gamepadData;
g_Instance->m_GamepadApi->Sample(g_Instance->pp_instance(), &gamepadData);
// We must determine which gamepads are connected before sending rumble events.
const unsigned short activeGamepadMask = static_cast<unsigned short>(GetActiveGamepadMask(gamepadData));
if ((activeGamepadMask & (1 << controllerNumber)) == 0)
return;
const float weakMagnitude = static_cast<float>(highFreqMotor) / static_cast<float>(UINT16_MAX);
const float strongMagnitude = static_cast<float>(lowFreqMotor) / static_cast<float>(UINT16_MAX);

View File

@ -87,7 +87,10 @@ function handleMessage(msg) {
const weakMagnitude = parseFloat(eventData[1]);
const strongMagnitude = parseFloat(eventData[2]);
console.log("Playing rumble on gamepad " + gamepadIdx + " with weakMagnitude " + weakMagnitude + " and strongMagnitude " + strongMagnitude);
navigator.getGamepads()[gamepadIdx].vibrationActuator.playEffect('dual-rumble', {
// We may not actually have a gamepad at this index.
// Even if we do have a gamepad, it may not have a vibrationActuator associated with it.
navigator.getGamepads()[gamepadIdx]?.vibrationActuator?.playEffect('dual-rumble', {
startDelay: 0,
duration: 5000, // Moonlight should be sending another rumble event when stopping.
weakMagnitude: weakMagnitude,