From 3b004e7a4b9810447e2dc936c18c1ad08e8f6295 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 27 Sep 2023 11:29:42 +0200 Subject: [PATCH 1/2] Remove the last use of accelerometer axis events (calibration) --- Core/TiltEventProcessor.cpp | 7 +++++++ Core/TiltEventProcessor.h | 4 ++++ UI/TiltAnalogSettingsScreen.cpp | 16 +--------------- UI/TiltAnalogSettingsScreen.h | 1 - 4 files changed, 12 insertions(+), 16 deletions(-) diff --git a/Core/TiltEventProcessor.cpp b/Core/TiltEventProcessor.cpp index 14486e84bd64..571f58f84f55 100644 --- a/Core/TiltEventProcessor.cpp +++ b/Core/TiltEventProcessor.cpp @@ -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); @@ -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); diff --git a/Core/TiltEventProcessor.h b/Core/TiltEventProcessor.h index 3eda969e17c4..d16f0020d6f4 100644 --- a/Core/TiltEventProcessor.h +++ b/Core/TiltEventProcessor.h @@ -1,5 +1,7 @@ #pragma once +#include "Common/Math/lin/vec3.h" + namespace TiltEventProcessor { // generates a tilt in the correct coordinate system based on @@ -7,6 +9,8 @@ namespace TiltEventProcessor { 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; diff --git a/UI/TiltAnalogSettingsScreen.cpp b/UI/TiltAnalogSettingsScreen.cpp index 716baf528995..af4456a54599 100644 --- a/UI/TiltAnalogSettingsScreen.cpp +++ b/UI/TiltAnalogSettingsScreen.cpp @@ -137,22 +137,8 @@ void TiltAnalogSettingsScreen::CreateViews() { settings->Add(new Choice(di->T("Back")))->OnClick.Handle(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; } diff --git a/UI/TiltAnalogSettingsScreen.h b/UI/TiltAnalogSettingsScreen.h index 1c338e5e944c..b1416cfb69ea 100644 --- a/UI/TiltAnalogSettingsScreen.h +++ b/UI/TiltAnalogSettingsScreen.h @@ -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"; } From c28dc9e4f2c5f7dfa1a4f6b7bcb7cca57cfe6b1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 27 Sep 2023 11:34:31 +0200 Subject: [PATCH 2/2] Pass in accelerometer readings using NativeAccelerometer instead of NativeAxis --- Common/Input/InputState.h | 2 +- Common/Input/KeyCodes.h | 2 +- Common/System/NativeApp.h | 1 + Qt/QtMain.cpp | 13 +------------ UI/NativeApp.cpp | 18 ++++++------------ android/jni/app-android.cpp | 13 +------------ 6 files changed, 11 insertions(+), 38 deletions(-) diff --git a/Common/Input/InputState.h b/Common/Input/InputState.h index c01680a0ae64..a94547443d23 100644 --- a/Common/Input/InputState.h +++ b/Common/Input/InputState.h @@ -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, diff --git a/Common/Input/KeyCodes.h b/Common/Input/KeyCodes.h index dc2b9897ce37..8614e5a2d287 100644 --- a/Common/Input/KeyCodes.h +++ b/Common/Input/KeyCodes.h @@ -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, diff --git a/Common/System/NativeApp.h b/Common/System/NativeApp.h index 94a67c9eb9c8..4799fa6f11a8 100644 --- a/Common/System/NativeApp.h +++ b/Common/System/NativeApp.h @@ -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. diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index ea99d78ff5a8..9991d78a18ed 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -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 } diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index cd622c087388..f4056c10b32b 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -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; @@ -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) { diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index 8433de74426e..79048d959508 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -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) {