Files
RetroArch/input/common/pad_motion.h
LibretroAdmin fa433ba816 Input: motion from the hid DualShock 4, DualSense and DualShock 3
The PlayStation pads' hid handlers now give a core their motion sensors
while it asks for them, through the same handler ops as the Switch Pro
Controller.

- DualShock 4: the gyroscope and accelerometer after the triggers, in
  USB's report 0x01 and Bluetooth's full report 0x11.
- DualSense: from byte 16 of USB's report and Bluetooth's 0x31.
- Both use the nominal scale and libretro's axes, through
  sony_pad_motion.h, as winraw reads the same pads.
- DualShock 3: the accelerometer, as SDL reads it: big-endian from byte
  41, 113 counts per g around 511. Its one-axis gyroscope is not given,
  as in SDL.

input/common/pad_motion.h holds what the handlers share: a core's ask
as a mask, and SDL's axes turned into libretro's as the sdl3 joypad
driver turns them. The Switch Pro handler now uses it too.
2026-10-22 02:50:30 +02:00

90 lines
2.9 KiB
C

/* RetroArch - A frontend for libretro.
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _PAD_MOTION_H
#define _PAD_MOTION_H
#include <stdint.h>
#include <boolean.h>
#include <retro_inline.h>
#include <libretro.h>
/* What the pad handlers share for a pad's motion sensors.
*
* A handler turns its pad's own axes into SDL's - the PlayStation
* pads', SDL's de facto standard - in g and radians a second, and
* these give a core the value it asks for on libretro's, as the sdl3
* joypad driver does: so a core sees a pad the same way whichever
* driver reads it. */
/* bit 0 the accelerometer, 1 the gyroscope */
#define PAD_MOTION_ACCEL 1
#define PAD_MOTION_GYRO 2
/* A core's ask, into @want; false for an action that is neither. */
static INLINE bool pad_motion_want(uint8_t *want,
enum retro_sensor_action action)
{
switch (action)
{
case RETRO_SENSOR_ACCELEROMETER_ENABLE:
*want |= PAD_MOTION_ACCEL;
return true;
case RETRO_SENSOR_GYROSCOPE_ENABLE:
*want |= PAD_MOTION_GYRO;
return true;
case RETRO_SENSOR_ACCELEROMETER_DISABLE:
*want &= (uint8_t)~PAD_MOTION_ACCEL;
return true;
case RETRO_SENSOR_GYROSCOPE_DISABLE:
*want &= (uint8_t)~PAD_MOTION_GYRO;
return true;
default:
break;
}
return false;
}
/* Sensor @id from @accel and @gyro on SDL's axes - @gyro NULL for a pad
* with none - where @want has it; false otherwise. */
static INLINE bool pad_motion_value(const float *accel, const float *gyro,
uint8_t want, unsigned id, float *value)
{
switch (id)
{
case RETRO_SENSOR_ACCELEROMETER_X:
case RETRO_SENSOR_ACCELEROMETER_Y:
case RETRO_SENSOR_ACCELEROMETER_Z:
if (!(want & PAD_MOTION_ACCEL))
return false;
*value = id == RETRO_SENSOR_ACCELEROMETER_X ? accel[0]
: id == RETRO_SENSOR_ACCELEROMETER_Y ? accel[2] : accel[1];
return true;
case RETRO_SENSOR_GYROSCOPE_X:
case RETRO_SENSOR_GYROSCOPE_Y:
case RETRO_SENSOR_GYROSCOPE_Z:
if (!gyro || !(want & PAD_MOTION_GYRO))
return false;
*value = id == RETRO_SENSOR_GYROSCOPE_X ? gyro[0]
: id == RETRO_SENSOR_GYROSCOPE_Y ? -gyro[2] : gyro[1];
return true;
default:
break;
}
return false;
}
#endif