Skip to content

Commit

Permalink
Merge pull request #18250 from hrydgard/separate-accelerometer-events
Browse files Browse the repository at this point in the history
Separate out accelerometer events from joystick axis events
  • Loading branch information
hrydgard committed Sep 27, 2023
2 parents c8d7226 + c28dc9e commit 1fff976
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 54 deletions.
2 changes: 1 addition & 1 deletion Common/Input/InputState.h
Expand Up @@ -31,7 +31,7 @@ enum InputDeviceID {
DEVICE_ID_XINPUT_1 = 21,
DEVICE_ID_XINPUT_2 = 22,
DEVICE_ID_XINPUT_3 = 23,
DEVICE_ID_ACCELEROMETER = 30,
DEVICE_ID_ACCELEROMETER = 30, // no longer used
DEVICE_ID_XR_HMD = 39,
DEVICE_ID_XR_CONTROLLER_LEFT = 40,
DEVICE_ID_XR_CONTROLLER_RIGHT = 41,
Expand Down
2 changes: 1 addition & 1 deletion Common/Input/KeyCodes.h
Expand Up @@ -305,7 +305,7 @@ enum InputAxis {
JOYSTICK_AXIS_MOUSE_REL_X = 26,
JOYSTICK_AXIS_MOUSE_REL_Y = 27,

// Mobile device accelerometer/gyro
// Mobile device accelerometer/gyro. NOTE: These are no longer passed around internally, only used for the plugin API.
JOYSTICK_AXIS_ACCELEROMETER_X = 40,
JOYSTICK_AXIS_ACCELEROMETER_Y = 41,
JOYSTICK_AXIS_ACCELEROMETER_Z = 42,
Expand Down
1 change: 1 addition & 0 deletions Common/System/NativeApp.h
Expand Up @@ -55,6 +55,7 @@ bool NativeIsRestarting();
void NativeTouch(const TouchInput &touch);
bool NativeKey(const KeyInput &key);
void NativeAxis(const AxisInput *axis, size_t count);
void NativeAccelerometer(float tiltX, float tiltY, float tiltZ);

// Called when it's process a frame, including rendering. If the device can keep up, this
// will be called sixty times per second. Main thread.
Expand Down
7 changes: 7 additions & 0 deletions Core/TiltEventProcessor.cpp
Expand Up @@ -19,6 +19,12 @@ static u32 tiltButtonsDown = 0;
float rawTiltAnalogX;
float rawTiltAnalogY;

float g_currentYAngle = 0.0f;

float GetCurrentYAngle() {
return g_currentYAngle;
}

// These functions generate tilt events given the current Tilt amount,
// and the deadzone radius.
void GenerateAnalogStickEvent(float analogX, float analogY);
Expand Down Expand Up @@ -73,6 +79,7 @@ void ProcessTilt(bool landscape, float calibrationAngle, float x, float y, float
Lin::Vec3 down = Lin::Vec3(x, y, z).normalized();

float angleAroundX = atan2(down.z, down.y);
g_currentYAngle = angleAroundX; // TODO: Should smooth this out over time a bit.
float yAngle = angleAroundX - calibrationAngle;
float xAngle = asinf(down.x);

Expand Down
4 changes: 4 additions & 0 deletions Core/TiltEventProcessor.h
@@ -1,12 +1,16 @@
#pragma once

#include "Common/Math/lin/vec3.h"

namespace TiltEventProcessor {

// generates a tilt in the correct coordinate system based on
// calibration. x, y, z is the current accelerometer reading (with no conversion).
void ProcessTilt(bool landscape, const float calibrationAngle, float x, float y, float z, bool invertX, bool invertY, float xSensitivity, float ySensitivity);
void ResetTiltEvents();

float GetCurrentYAngle();

// Lets you preview the amount of tilt in TiltAnalogSettingsScreen.
extern float rawTiltAnalogX;
extern float rawTiltAnalogY;
Expand Down
13 changes: 1 addition & 12 deletions Qt/QtMain.cpp
Expand Up @@ -731,18 +731,7 @@ void MainUI::updateAccelerometer() {
// TODO: Toggle it depending on whether it is enabled
QAccelerometerReading *reading = acc->reading();
if (reading) {
AxisInput axis[3];
for (int i = 0; i < 3; i++) {
axis[i].deviceId = DEVICE_ID_ACCELEROMETER;
}

axis[0].axisId = JOYSTICK_AXIS_ACCELEROMETER_X;
axis[0].value = reading->x();
axis[1].axisId = JOYSTICK_AXIS_ACCELEROMETER_Y;
axis[1].value = reading->y();
axis[2].axisId = JOYSTICK_AXIS_ACCELEROMETER_Z;
axis[2].value = reading->z();
NativeAxis(axis, 3);
NativeAccelerometer(reading->x(), reading->y(), reading->z());
}
#endif
}
Expand Down
18 changes: 6 additions & 12 deletions UI/NativeApp.cpp
Expand Up @@ -1336,22 +1336,12 @@ static void ProcessOneAxisEvent(const AxisInput &axis) {
}

void NativeAxis(const AxisInput *axes, size_t count) {
// figure out what the current tilt orientation is by checking the axis event
// This is static, since we need to remember where we last were (in terms of orientation)
static float tiltX;
static float tiltY;
static float tiltZ;

for (size_t i = 0; i < count; i++) {
ProcessOneAxisEvent(axes[i]);
switch (axes[i].axisId) {
case JOYSTICK_AXIS_ACCELEROMETER_X: tiltX = axes[i].value; break;
case JOYSTICK_AXIS_ACCELEROMETER_Y: tiltY = axes[i].value; break;
case JOYSTICK_AXIS_ACCELEROMETER_Z: tiltZ = axes[i].value; break;
default: break;
}
}
}

void NativeAccelerometer(float tiltX, float tiltY, float tiltZ) {
if (g_Config.iTiltInputType == TILT_NULL) {
// if tilt events are disabled, don't do anything special.
return;
Expand All @@ -1377,6 +1367,10 @@ void NativeAxis(const AxisInput *axes, size_t count) {
TiltEventProcessor::ProcessTilt(landscape, tiltBaseAngleY, tiltX, tiltY, tiltZ,
g_Config.bInvertTiltX, g_Config.bInvertTiltY,
xSensitivity, ySensitivity);

HLEPlugins::PluginDataAxis[JOYSTICK_AXIS_ACCELEROMETER_X] = tiltX;
HLEPlugins::PluginDataAxis[JOYSTICK_AXIS_ACCELEROMETER_Y] = tiltY;
HLEPlugins::PluginDataAxis[JOYSTICK_AXIS_ACCELEROMETER_Z] = tiltZ;
}

void System_PostUIMessage(const std::string &message, const std::string &value) {
Expand Down
16 changes: 1 addition & 15 deletions UI/TiltAnalogSettingsScreen.cpp
Expand Up @@ -137,22 +137,8 @@ void TiltAnalogSettingsScreen::CreateViews() {
settings->Add(new Choice(di->T("Back")))->OnClick.Handle<UIScreen>(this, &UIScreen::OnBack);
}

void TiltAnalogSettingsScreen::axis(const AxisInput &axis) {
UIDialogScreenWithGameBackground::axis(axis);

if (axis.deviceId == DEVICE_ID_ACCELEROMETER) {
switch (axis.axisId) {
case JOYSTICK_AXIS_ACCELEROMETER_X: down_.x = axis.value; break;
case JOYSTICK_AXIS_ACCELEROMETER_Y: down_.y = axis.value; break;
case JOYSTICK_AXIS_ACCELEROMETER_Z: down_.z = axis.value; break;
default: break;
}
}
}

UI::EventReturn TiltAnalogSettingsScreen::OnCalibrate(UI::EventParams &e) {
Lin::Vec3 down = down_.normalized();
g_Config.fTiltBaseAngleY = atan2(down.z, down.x);
g_Config.fTiltBaseAngleY = TiltEventProcessor::GetCurrentYAngle();
return UI::EVENT_DONE;
}

Expand Down
1 change: 0 additions & 1 deletion UI/TiltAnalogSettingsScreen.h
Expand Up @@ -29,7 +29,6 @@ class TiltAnalogSettingsScreen : public UIDialogScreenWithGameBackground {
TiltAnalogSettingsScreen(const Path &gamePath) : UIDialogScreenWithGameBackground(gamePath) {}

void CreateViews() override;
void axis(const AxisInput &axis) override;
void update() override;
const char *tag() const override { return "TiltAnalogSettings"; }

Expand Down
13 changes: 1 addition & 12 deletions android/jni/app-android.cpp
Expand Up @@ -1240,18 +1240,7 @@ extern "C" jboolean Java_org_ppsspp_ppsspp_NativeApp_mouseWheelEvent(
extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_accelerometer(JNIEnv *, jclass, float x, float y, float z) {
if (!renderer_inited)
return;

AxisInput axis[3];
for (int i = 0; i < 3; i++) {
axis[i].deviceId = DEVICE_ID_ACCELEROMETER;
}
axis[0].axisId = JOYSTICK_AXIS_ACCELEROMETER_X;
axis[0].value = x;
axis[1].axisId = JOYSTICK_AXIS_ACCELEROMETER_Y;
axis[1].value = y;
axis[2].axisId = JOYSTICK_AXIS_ACCELEROMETER_Z;
axis[2].value = z;
NativeAxis(axis, 3);
NativeAccelerometer(x, y, z);
}

extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendMessageFromJava(JNIEnv *env, jclass, jstring message, jstring param) {
Expand Down

0 comments on commit 1fff976

Please sign in to comment.