core: hid: Finish linking motion from virtual controllers

This commit is contained in:
Narr the Reg 2023-04-11 20:39:05 -06:00 committed by bunnei
parent d015b0db93
commit 5c1310dc5d
5 changed files with 57 additions and 9 deletions

View File

@ -117,11 +117,13 @@ open class EmulationActivity : AppCompatActivity() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
nfcReader.startScanning() nfcReader.startScanning()
startMotionSensorListener()
} }
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
nfcReader.stopScanning() nfcReader.stopScanning()
stopMotionSensorListener()
} }
override fun onNewIntent(intent: Intent) { override fun onNewIntent(intent: Intent) {

View File

@ -13,7 +13,7 @@ EmulatedConsole::~EmulatedConsole() = default;
void EmulatedConsole::ReloadFromSettings() { void EmulatedConsole::ReloadFromSettings() {
// Using first motion device from player 1. No need to assign any unique config at the moment // Using first motion device from player 1. No need to assign any unique config at the moment
const auto& player = Settings::values.players.GetValue()[0]; const auto& player = Settings::values.players.GetValue()[0];
motion_params = Common::ParamPackage(player.motions[0]); motion_params[0] = Common::ParamPackage(player.motions[0]);
ReloadInput(); ReloadInput();
} }
@ -74,14 +74,30 @@ void EmulatedConsole::ReloadInput() {
// If you load any device here add the equivalent to the UnloadInput() function // If you load any device here add the equivalent to the UnloadInput() function
SetTouchParams(); SetTouchParams();
motion_devices = Common::Input::CreateInputDevice(motion_params); motion_params[1] = Common::ParamPackage{"engine:virtual_gamepad,port:8,motion:0"};
if (motion_devices) {
motion_devices->SetCallback({ for (std::size_t index = 0; index < motion_devices.size(); ++index) {
motion_devices[index] = Common::Input::CreateInputDevice(motion_params[index]);
if (!motion_devices[index]) {
continue;
}
motion_devices[index]->SetCallback({
.on_change = .on_change =
[this](const Common::Input::CallbackStatus& callback) { SetMotion(callback); }, [this](const Common::Input::CallbackStatus& callback) { SetMotion(callback); },
}); });
} }
// Restore motion state
auto& emulated_motion = console.motion_values.emulated;
auto& motion = console.motion_state;
emulated_motion.ResetRotations();
emulated_motion.ResetQuaternion();
motion.accel = emulated_motion.GetAcceleration();
motion.gyro = emulated_motion.GetGyroscope();
motion.rotation = emulated_motion.GetRotations();
motion.orientation = emulated_motion.GetOrientation();
motion.is_at_rest = !emulated_motion.IsMoving(motion_sensitivity);
// Unique index for identifying touch device source // Unique index for identifying touch device source
std::size_t index = 0; std::size_t index = 0;
for (auto& touch_device : touch_devices) { for (auto& touch_device : touch_devices) {
@ -100,7 +116,9 @@ void EmulatedConsole::ReloadInput() {
} }
void EmulatedConsole::UnloadInput() { void EmulatedConsole::UnloadInput() {
motion_devices.reset(); for (auto& motion : motion_devices) {
motion.reset();
}
for (auto& touch : touch_devices) { for (auto& touch : touch_devices) {
touch.reset(); touch.reset();
} }
@ -133,11 +151,11 @@ void EmulatedConsole::RestoreConfig() {
} }
Common::ParamPackage EmulatedConsole::GetMotionParam() const { Common::ParamPackage EmulatedConsole::GetMotionParam() const {
return motion_params; return motion_params[0];
} }
void EmulatedConsole::SetMotionParam(Common::ParamPackage param) { void EmulatedConsole::SetMotionParam(Common::ParamPackage param) {
motion_params = std::move(param); motion_params[0] = std::move(param);
ReloadInput(); ReloadInput();
} }

View File

@ -29,10 +29,10 @@ struct ConsoleMotionInfo {
MotionInput emulated{}; MotionInput emulated{};
}; };
using ConsoleMotionDevices = std::unique_ptr<Common::Input::InputDevice>; using ConsoleMotionDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, 2>;
using TouchDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, MaxTouchDevices>; using TouchDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, MaxTouchDevices>;
using ConsoleMotionParams = Common::ParamPackage; using ConsoleMotionParams = std::array<Common::ParamPackage, 2>;
using TouchParams = std::array<Common::ParamPackage, MaxTouchDevices>; using TouchParams = std::array<Common::ParamPackage, MaxTouchDevices>;
using ConsoleMotionValues = ConsoleMotionInfo; using ConsoleMotionValues = ConsoleMotionInfo;

View File

@ -193,6 +193,8 @@ void EmulatedController::LoadDevices() {
Common::Input::CreateInputDevice); Common::Input::CreateInputDevice);
std::ranges::transform(virtual_stick_params, virtual_stick_devices.begin(), std::ranges::transform(virtual_stick_params, virtual_stick_devices.begin(),
Common::Input::CreateInputDevice); Common::Input::CreateInputDevice);
std::ranges::transform(virtual_motion_params, virtual_motion_devices.begin(),
Common::Input::CreateInputDevice);
} }
void EmulatedController::LoadTASParams() { void EmulatedController::LoadTASParams() {
@ -253,6 +255,12 @@ void EmulatedController::LoadVirtualGamepadParams() {
for (auto& param : virtual_stick_params) { for (auto& param : virtual_stick_params) {
param = common_params; param = common_params;
} }
for (auto& param : virtual_stick_params) {
param = common_params;
}
for (auto& param : virtual_motion_params) {
param = common_params;
}
// TODO(german77): Replace this with an input profile or something better // TODO(german77): Replace this with an input profile or something better
virtual_button_params[Settings::NativeButton::A].Set("button", 0); virtual_button_params[Settings::NativeButton::A].Set("button", 0);
@ -284,6 +292,9 @@ void EmulatedController::LoadVirtualGamepadParams() {
virtual_stick_params[Settings::NativeAnalog::LStick].Set("range", 1.0f); virtual_stick_params[Settings::NativeAnalog::LStick].Set("range", 1.0f);
virtual_stick_params[Settings::NativeAnalog::RStick].Set("deadzone", 0.0f); virtual_stick_params[Settings::NativeAnalog::RStick].Set("deadzone", 0.0f);
virtual_stick_params[Settings::NativeAnalog::RStick].Set("range", 1.0f); virtual_stick_params[Settings::NativeAnalog::RStick].Set("range", 1.0f);
virtual_motion_params[Settings::NativeMotion::MotionLeft].Set("motion", 0);
virtual_motion_params[Settings::NativeMotion::MotionRight].Set("motion", 0);
} }
void EmulatedController::ReloadInput() { void EmulatedController::ReloadInput() {
@ -463,6 +474,18 @@ void EmulatedController::ReloadInput() {
}, },
}); });
} }
for (std::size_t index = 0; index < virtual_motion_devices.size(); ++index) {
if (!virtual_motion_devices[index]) {
continue;
}
virtual_motion_devices[index]->SetCallback({
.on_change =
[this, index](const Common::Input::CallbackStatus& callback) {
SetMotion(callback, index);
},
});
}
turbo_button_state = 0; turbo_button_state = 0;
} }
@ -500,6 +523,9 @@ void EmulatedController::UnloadInput() {
for (auto& stick : virtual_stick_devices) { for (auto& stick : virtual_stick_devices) {
stick.reset(); stick.reset();
} }
for (auto& motion : virtual_motion_devices) {
motion.reset();
}
for (auto& camera : camera_devices) { for (auto& camera : camera_devices) {
camera.reset(); camera.reset();
} }

View File

@ -568,8 +568,10 @@ private:
// Virtual gamepad related variables // Virtual gamepad related variables
ButtonParams virtual_button_params; ButtonParams virtual_button_params;
StickParams virtual_stick_params; StickParams virtual_stick_params;
ControllerMotionParams virtual_motion_params;
ButtonDevices virtual_button_devices; ButtonDevices virtual_button_devices;
StickDevices virtual_stick_devices; StickDevices virtual_stick_devices;
ControllerMotionDevices virtual_motion_devices;
mutable std::mutex mutex; mutable std::mutex mutex;
mutable std::mutex callback_mutex; mutable std::mutex callback_mutex;