1
0
mirror of https://git.suyu.dev/suyu/suyu synced 2025-09-02 09:26:30 -05:00

Initial commit

This commit is contained in:
Crimson-Hawk
2024-03-05 16:42:40 +08:00
commit f1e4595ebf
39576 changed files with 7006612 additions and 0 deletions

BIN
src/hid_core/.DS_Store vendored Normal file

Binary file not shown.

166
src/hid_core/CMakeLists.txt Executable file
View File

@@ -0,0 +1,166 @@
# SPDX-FileCopyrightText: 2018 yuzu Emulator Project
# SPDX-License-Identifier: GPL-2.0-or-later
add_library(hid_core STATIC
frontend/emulated_console.cpp
frontend/emulated_console.h
frontend/emulated_controller.cpp
frontend/emulated_controller.h
frontend/emulated_devices.cpp
frontend/emulated_devices.h
frontend/input_converter.cpp
frontend/input_converter.h
frontend/input_interpreter.cpp
frontend/input_interpreter.h
frontend/motion_input.cpp
frontend/motion_input.h
hidbus/hidbus_base.cpp
hidbus/hidbus_base.h
hidbus/ringcon.cpp
hidbus/ringcon.h
hidbus/starlink.cpp
hidbus/starlink.h
hidbus/stubbed.cpp
hidbus/stubbed.h
irsensor/clustering_processor.cpp
irsensor/clustering_processor.h
irsensor/image_transfer_processor.cpp
irsensor/image_transfer_processor.h
irsensor/ir_led_processor.cpp
irsensor/ir_led_processor.h
irsensor/moment_processor.cpp
irsensor/moment_processor.h
irsensor/pointing_processor.cpp
irsensor/pointing_processor.h
irsensor/processor_base.cpp
irsensor/processor_base.h
irsensor/tera_plugin_processor.cpp
irsensor/tera_plugin_processor.h
resources/abstracted_pad/abstract_battery_handler.cpp
resources/abstracted_pad/abstract_battery_handler.h
resources/abstracted_pad/abstract_button_handler.cpp
resources/abstracted_pad/abstract_button_handler.h
resources/abstracted_pad/abstract_ir_sensor_handler.cpp
resources/abstracted_pad/abstract_ir_sensor_handler.h
resources/abstracted_pad/abstract_led_handler.cpp
resources/abstracted_pad/abstract_led_handler.h
resources/abstracted_pad/abstract_mcu_handler.cpp
resources/abstracted_pad/abstract_mcu_handler.h
resources/abstracted_pad/abstract_nfc_handler.cpp
resources/abstracted_pad/abstract_nfc_handler.h
resources/abstracted_pad/abstract_pad.cpp
resources/abstracted_pad/abstract_pad.h
resources/abstracted_pad/abstract_pad_holder.cpp
resources/abstracted_pad/abstract_pad_holder.h
resources/abstracted_pad/abstract_palma_handler.cpp
resources/abstracted_pad/abstract_palma_handler.h
resources/abstracted_pad/abstract_properties_handler.cpp
resources/abstracted_pad/abstract_properties_handler.h
resources/abstracted_pad/abstract_sixaxis_handler.cpp
resources/abstracted_pad/abstract_sixaxis_handler.h
resources/abstracted_pad/abstract_vibration_handler.cpp
resources/abstracted_pad/abstract_vibration_handler.h
resources/debug_pad/debug_pad.cpp
resources/debug_pad/debug_pad.h
resources/debug_pad/debug_pad_types.h
resources/digitizer/digitizer.cpp
resources/digitizer/digitizer.h
resources/keyboard/keyboard.cpp
resources/keyboard/keyboard.h
resources/keyboard/keyboard_types.h
resources/mouse/debug_mouse.cpp
resources/mouse/debug_mouse.h
resources/mouse/mouse.cpp
resources/mouse/mouse.h
resources/mouse/mouse_types.h
resources/npad/npad.cpp
resources/npad/npad.h
resources/npad/npad_data.cpp
resources/npad/npad_data.h
resources/npad/npad_resource.cpp
resources/npad/npad_resource.h
resources/npad/npad_types.h
resources/npad/npad_vibration.cpp
resources/npad/npad_vibration.h
resources/palma/palma.cpp
resources/palma/palma.h
resources/six_axis/console_six_axis.cpp
resources/six_axis/console_six_axis.h
resources/six_axis/seven_six_axis.cpp
resources/six_axis/seven_six_axis.h
resources/six_axis/six_axis.cpp
resources/six_axis/six_axis.h
resources/system_buttons/capture_button.cpp
resources/system_buttons/capture_button.h
resources/system_buttons/home_button.cpp
resources/system_buttons/home_button.h
resources/system_buttons/sleep_button.cpp
resources/system_buttons/sleep_button.h
resources/system_buttons/system_button_types.h
resources/touch_screen/gesture.cpp
resources/touch_screen/gesture.h
resources/touch_screen/gesture_handler.cpp
resources/touch_screen/gesture_handler.h
resources/touch_screen/touch_screen.cpp
resources/touch_screen/touch_screen.h
resources/touch_screen/touch_screen_driver.cpp
resources/touch_screen/touch_screen_driver.h
resources/touch_screen/touch_screen_resource.cpp
resources/touch_screen/touch_screen_resource.h
resources/touch_screen/touch_types.h
resources/unique_pad/unique_pad.cpp
resources/unique_pad/unique_pad.h
resources/vibration/gc_vibration_device.h
resources/vibration/gc_vibration_device.cpp
resources/vibration/n64_vibration_device.h
resources/vibration/n64_vibration_device.cpp
resources/vibration/vibration_base.h
resources/vibration/vibration_base.cpp
resources/vibration/vibration_device.h
resources/vibration/vibration_device.cpp
resources/applet_resource.cpp
resources/applet_resource.h
resources/controller_base.cpp
resources/controller_base.h
resources/hid_firmware_settings.cpp
resources/hid_firmware_settings.h
resources/irs_ring_lifo.h
resources/ring_lifo.h
resources/shared_memory_format.h
resources/shared_memory_holder.cpp
resources/shared_memory_holder.h
hid_core.cpp
hid_core.h
hid_result.h
hid_types.h
hid_util.h
precompiled_headers.h
resource_manager.cpp
resource_manager.h
)
if (MSVC)
target_compile_options(hid_core PRIVATE
/we4242 # 'identifier': conversion from 'type1' to 'type2', possible loss of data
/we4244 # 'conversion': conversion from 'type1' to 'type2', possible loss of data
/we4245 # 'conversion': conversion from 'type1' to 'type2', signed/unsigned mismatch
/we4254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data
/we4800 # Implicit conversion from 'type' to bool. Possible information loss
)
else()
target_compile_options(hid_core PRIVATE
-Werror=conversion
-Wno-sign-conversion
-Wno-cast-function-type
$<$<CXX_COMPILER_ID:Clang>:-fsized-deallocation>
)
endif()
create_target_directory_groups(hid_core)
target_link_libraries(hid_core PUBLIC core)
if (YUZU_USE_PRECOMPILED_HEADERS)
target_precompile_headers(hid_core PRIVATE precompiled_headers.h)
endif()

View File

@@ -0,0 +1,324 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "common/settings.h"
#include "hid_core/frontend/emulated_console.h"
#include "hid_core/frontend/input_converter.h"
namespace Core::HID {
EmulatedConsole::EmulatedConsole() = default;
EmulatedConsole::~EmulatedConsole() = default;
void EmulatedConsole::ReloadFromSettings() {
// 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];
motion_params[0] = Common::ParamPackage(player.motions[0]);
ReloadInput();
}
void EmulatedConsole::SetTouchParams() {
std::size_t index = 0;
// We can't use mouse as touch if native mouse is enabled
if (!Settings::values.mouse_enabled) {
touch_params[index++] =
Common::ParamPackage{"engine:mouse,axis_x:0,axis_y:1,button:0,port:2"};
}
touch_params[index++] =
Common::ParamPackage{"engine:cemuhookudp,axis_x:17,axis_y:18,button:65536"};
touch_params[index++] =
Common::ParamPackage{"engine:cemuhookudp,axis_x:19,axis_y:20,button:131072"};
for (int i = 0; i < static_cast<int>(MaxActiveTouchInputs); i++) {
Common::ParamPackage touchscreen_param{};
touchscreen_param.Set("engine", "touch");
touchscreen_param.Set("axis_x", i * 2);
touchscreen_param.Set("axis_y", (i * 2) + 1);
touchscreen_param.Set("button", i);
touch_params[index++] = std::move(touchscreen_param);
}
if (Settings::values.touch_from_button_maps.empty()) {
LOG_WARNING(Input, "touch_from_button_maps is unset by frontend config");
return;
}
const auto button_index =
static_cast<u64>(Settings::values.touch_from_button_map_index.GetValue());
const auto& touch_buttons = Settings::values.touch_from_button_maps[button_index].buttons;
// Map the rest of the fingers from touch from button configuration
for (const auto& config_entry : touch_buttons) {
if (index >= MaxTouchDevices) {
continue;
}
Common::ParamPackage params{config_entry};
Common::ParamPackage touch_button_params;
const int x = params.Get("x", 0);
const int y = params.Get("y", 0);
params.Erase("x");
params.Erase("y");
touch_button_params.Set("engine", "touch_from_button");
touch_button_params.Set("button", params.Serialize());
touch_button_params.Set("x", x);
touch_button_params.Set("y", y);
touch_params[index] = std::move(touch_button_params);
index++;
}
}
void EmulatedConsole::ReloadInput() {
// If you load any device here add the equivalent to the UnloadInput() function
SetTouchParams();
motion_params[1] = Common::ParamPackage{"engine:virtual_gamepad,port:8,motion:0"};
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 =
[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
std::size_t index = 0;
for (auto& touch_device : touch_devices) {
touch_device = Common::Input::CreateInputDevice(touch_params[index]);
if (!touch_device) {
continue;
}
touch_device->SetCallback({
.on_change =
[this, index](const Common::Input::CallbackStatus& callback) {
SetTouch(callback, index);
},
});
index++;
}
}
void EmulatedConsole::UnloadInput() {
for (auto& motion : motion_devices) {
motion.reset();
}
for (auto& touch : touch_devices) {
touch.reset();
}
}
void EmulatedConsole::EnableConfiguration() {
is_configuring = true;
SaveCurrentConfig();
}
void EmulatedConsole::DisableConfiguration() {
is_configuring = false;
}
bool EmulatedConsole::IsConfiguring() const {
return is_configuring;
}
void EmulatedConsole::SaveCurrentConfig() {
if (!is_configuring) {
return;
}
}
void EmulatedConsole::RestoreConfig() {
if (!is_configuring) {
return;
}
ReloadFromSettings();
}
Common::ParamPackage EmulatedConsole::GetMotionParam() const {
return motion_params[0];
}
void EmulatedConsole::SetMotionParam(Common::ParamPackage param) {
motion_params[0] = std::move(param);
ReloadInput();
}
void EmulatedConsole::SetMotion(const Common::Input::CallbackStatus& callback) {
std::unique_lock lock{mutex};
auto& raw_status = console.motion_values.raw_status;
auto& emulated = console.motion_values.emulated;
raw_status = TransformToMotion(callback);
emulated.SetAcceleration(Common::Vec3f{
raw_status.accel.x.value,
raw_status.accel.y.value,
raw_status.accel.z.value,
});
emulated.SetGyroscope(Common::Vec3f{
raw_status.gyro.x.value,
raw_status.gyro.y.value,
raw_status.gyro.z.value,
});
emulated.UpdateRotation(raw_status.delta_timestamp);
emulated.UpdateOrientation(raw_status.delta_timestamp);
if (is_configuring) {
lock.unlock();
TriggerOnChange(ConsoleTriggerType::Motion);
return;
}
auto& motion = console.motion_state;
motion.accel = emulated.GetAcceleration();
motion.gyro = emulated.GetGyroscope();
motion.rotation = emulated.GetRotations();
motion.orientation = emulated.GetOrientation();
motion.quaternion = emulated.GetQuaternion();
motion.gyro_bias = emulated.GetGyroBias();
motion.is_at_rest = !emulated.IsMoving(motion_sensitivity);
// Find what is this value
motion.verticalization_error = 0.0f;
lock.unlock();
TriggerOnChange(ConsoleTriggerType::Motion);
}
void EmulatedConsole::SetTouch(const Common::Input::CallbackStatus& callback, std::size_t index) {
if (index >= MaxTouchDevices) {
return;
}
std::unique_lock lock{mutex};
const auto touch_input = TransformToTouch(callback);
auto touch_index = GetIndexFromFingerId(index);
bool is_new_input = false;
if (!touch_index.has_value() && touch_input.pressed.value) {
touch_index = GetNextFreeIndex();
is_new_input = true;
}
// No free entries or invalid state. Ignore input
if (!touch_index.has_value()) {
return;
}
auto& touch_value = console.touch_values[touch_index.value()];
if (is_new_input) {
touch_value.pressed.value = true;
touch_value.id = static_cast<int>(index);
}
touch_value.x = touch_input.x;
touch_value.y = touch_input.y;
if (!touch_input.pressed.value) {
touch_value.pressed.value = false;
}
if (is_configuring) {
lock.unlock();
TriggerOnChange(ConsoleTriggerType::Touch);
return;
}
// Touch outside allowed range. Ignore input
if (touch_index.value() >= MaxActiveTouchInputs) {
return;
}
console.touch_state[touch_index.value()] = {
.position = {touch_value.x.value, touch_value.y.value},
.id = static_cast<u32>(touch_index.value()),
.pressed = touch_input.pressed.value,
};
lock.unlock();
TriggerOnChange(ConsoleTriggerType::Touch);
}
ConsoleMotionValues EmulatedConsole::GetMotionValues() const {
std::scoped_lock lock{mutex};
return console.motion_values;
}
TouchValues EmulatedConsole::GetTouchValues() const {
std::scoped_lock lock{mutex};
return console.touch_values;
}
ConsoleMotion EmulatedConsole::GetMotion() const {
std::scoped_lock lock{mutex};
return console.motion_state;
}
TouchFingerState EmulatedConsole::GetTouch() const {
std::scoped_lock lock{mutex};
return console.touch_state;
}
std::optional<std::size_t> EmulatedConsole::GetIndexFromFingerId(std::size_t finger_id) const {
for (std::size_t index = 0; index < MaxTouchDevices; ++index) {
const auto& finger = console.touch_values[index];
if (!finger.pressed.value) {
continue;
}
if (finger.id == static_cast<int>(finger_id)) {
return index;
}
}
return std::nullopt;
}
std::optional<std::size_t> EmulatedConsole::GetNextFreeIndex() const {
for (std::size_t index = 0; index < MaxTouchDevices; ++index) {
if (!console.touch_values[index].pressed.value) {
return index;
}
}
return std::nullopt;
}
void EmulatedConsole::TriggerOnChange(ConsoleTriggerType type) {
std::scoped_lock lock{callback_mutex};
for (const auto& poller_pair : callback_list) {
const ConsoleUpdateCallback& poller = poller_pair.second;
if (poller.on_change) {
poller.on_change(type);
}
}
}
int EmulatedConsole::SetCallback(ConsoleUpdateCallback update_callback) {
std::scoped_lock lock{callback_mutex};
callback_list.insert_or_assign(last_callback_key, std::move(update_callback));
return last_callback_key++;
}
void EmulatedConsole::DeleteCallback(int key) {
std::scoped_lock lock{callback_mutex};
const auto& iterator = callback_list.find(key);
if (iterator == callback_list.end()) {
LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);
return;
}
callback_list.erase(iterator);
}
} // namespace Core::HID

View File

@@ -0,0 +1,192 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <array>
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <unordered_map>
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "common/input.h"
#include "common/param_package.h"
#include "common/point.h"
#include "common/quaternion.h"
#include "common/vector_math.h"
#include "hid_core/frontend/motion_input.h"
#include "hid_core/hid_types.h"
namespace Core::HID {
static constexpr std::size_t MaxTouchDevices = 32;
static constexpr std::size_t MaxActiveTouchInputs = 16;
struct ConsoleMotionInfo {
Common::Input::MotionStatus raw_status{};
MotionInput emulated{};
};
using ConsoleMotionDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, 2>;
using TouchDevices = std::array<std::unique_ptr<Common::Input::InputDevice>, MaxTouchDevices>;
using ConsoleMotionParams = std::array<Common::ParamPackage, 2>;
using TouchParams = std::array<Common::ParamPackage, MaxTouchDevices>;
using ConsoleMotionValues = ConsoleMotionInfo;
using TouchValues = std::array<Common::Input::TouchStatus, MaxTouchDevices>;
// Contains all motion related data that is used on the services
struct ConsoleMotion {
Common::Vec3f accel{};
Common::Vec3f gyro{};
Common::Vec3f rotation{};
std::array<Common::Vec3f, 3> orientation{};
Common::Quaternion<f32> quaternion{};
Common::Vec3f gyro_bias{};
f32 verticalization_error{};
bool is_at_rest{};
};
using TouchFingerState = std::array<TouchFinger, MaxActiveTouchInputs>;
struct ConsoleStatus {
// Data from input_common
ConsoleMotionValues motion_values{};
TouchValues touch_values{};
// Data for HID services
ConsoleMotion motion_state{};
TouchFingerState touch_state{};
};
enum class ConsoleTriggerType {
Motion,
Touch,
All,
};
struct ConsoleUpdateCallback {
std::function<void(ConsoleTriggerType)> on_change;
};
class EmulatedConsole {
public:
/**
* Contains all input data within the emulated switch console tablet such as touch and motion
*/
explicit EmulatedConsole();
~EmulatedConsole();
YUZU_NON_COPYABLE(EmulatedConsole);
YUZU_NON_MOVEABLE(EmulatedConsole);
/// Removes all callbacks created from input devices
void UnloadInput();
/**
* Sets the emulated console into configuring mode
* This prevents the modification of the HID state of the emulated console by input commands
*/
void EnableConfiguration();
/// Returns the emulated console into normal mode, allowing the modification of the HID state
void DisableConfiguration();
/// Returns true if the emulated console is in configuring mode
bool IsConfiguring() const;
/// Reload all input devices
void ReloadInput();
/// Overrides current mapped devices with the stored configuration and reloads all input devices
void ReloadFromSettings();
/// Saves the current mapped configuration
void SaveCurrentConfig();
/// Reverts any mapped changes made that weren't saved
void RestoreConfig();
// Returns the current mapped motion device
Common::ParamPackage GetMotionParam() const;
/**
* Updates the current mapped motion device
* @param param ParamPackage with controller data to be mapped
*/
void SetMotionParam(Common::ParamPackage param);
/// Returns the latest status of motion input from the console with parameters
ConsoleMotionValues GetMotionValues() const;
/// Returns the latest status of touch input from the console with parameters
TouchValues GetTouchValues() const;
/// Returns the latest status of motion input from the console
ConsoleMotion GetMotion() const;
/// Returns the latest status of touch input from the console
TouchFingerState GetTouch() const;
/**
* Adds a callback to the list of events
* @param update_callback A ConsoleUpdateCallback that will be triggered
* @return an unique key corresponding to the callback index in the list
*/
int SetCallback(ConsoleUpdateCallback update_callback);
/**
* Removes a callback from the list stopping any future events to this object
* @param key Key corresponding to the callback index in the list
*/
void DeleteCallback(int key);
private:
/// Creates and stores the touch params
void SetTouchParams();
/**
* Updates the motion status of the console
* @param callback A CallbackStatus containing gyro and accelerometer data
*/
void SetMotion(const Common::Input::CallbackStatus& callback);
/**
* Updates the touch status of the console
* @param callback A CallbackStatus containing the touch position
* @param index Finger ID to be updated
*/
void SetTouch(const Common::Input::CallbackStatus& callback, std::size_t index);
std::optional<std::size_t> GetIndexFromFingerId(std::size_t finger_id) const;
std::optional<std::size_t> GetNextFreeIndex() const;
/**
* Triggers a callback that something has changed on the console status
* @param type Input type of the event to trigger
*/
void TriggerOnChange(ConsoleTriggerType type);
bool is_configuring{false};
f32 motion_sensitivity{0.01f};
ConsoleMotionParams motion_params;
TouchParams touch_params;
ConsoleMotionDevices motion_devices;
TouchDevices touch_devices;
mutable std::mutex mutex;
mutable std::mutex callback_mutex;
std::unordered_map<int, ConsoleUpdateCallback> callback_list;
int last_callback_key = 0;
// Stores the current status of all console input
ConsoleStatus console;
};
} // namespace Core::HID

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,640 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <array>
#include <functional>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>
#include "common/common_types.h"
#include "common/input.h"
#include "common/param_package.h"
#include "common/settings.h"
#include "common/vector_math.h"
#include "hid_core/frontend/motion_input.h"
#include "hid_core/hid_types.h"
#include "hid_core/irsensor/irs_types.h"
namespace Core::HID {
const std::size_t max_emulated_controllers = 2;
const std::size_t output_devices_size = 5;
struct ControllerMotionInfo {
Common::Input::MotionStatus raw_status{};
MotionInput emulated{};
};
using ButtonDevices =
std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeButton::NumButtons>;
using StickDevices =
std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeAnalog::NumAnalogs>;
using ControllerMotionDevices =
std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeMotion::NumMotions>;
using TriggerDevices =
std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeTrigger::NumTriggers>;
using ColorDevices =
std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
using BatteryDevices =
std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
using CameraDevices =
std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
using RingAnalogDevices =
std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
using NfcDevices =
std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
using OutputDevices = std::array<std::unique_ptr<Common::Input::OutputDevice>, output_devices_size>;
using ButtonParams = std::array<Common::ParamPackage, Settings::NativeButton::NumButtons>;
using StickParams = std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs>;
using ControllerMotionParams = std::array<Common::ParamPackage, Settings::NativeMotion::NumMotions>;
using TriggerParams = std::array<Common::ParamPackage, Settings::NativeTrigger::NumTriggers>;
using ColorParams = std::array<Common::ParamPackage, max_emulated_controllers>;
using BatteryParams = std::array<Common::ParamPackage, max_emulated_controllers>;
using CameraParams = std::array<Common::ParamPackage, max_emulated_controllers>;
using RingAnalogParams = std::array<Common::ParamPackage, max_emulated_controllers>;
using NfcParams = std::array<Common::ParamPackage, max_emulated_controllers>;
using OutputParams = std::array<Common::ParamPackage, output_devices_size>;
using ButtonValues = std::array<Common::Input::ButtonStatus, Settings::NativeButton::NumButtons>;
using SticksValues = std::array<Common::Input::StickStatus, Settings::NativeAnalog::NumAnalogs>;
using TriggerValues =
std::array<Common::Input::TriggerStatus, Settings::NativeTrigger::NumTriggers>;
using ControllerMotionValues = std::array<ControllerMotionInfo, Settings::NativeMotion::NumMotions>;
using ColorValues = std::array<Common::Input::BodyColorStatus, max_emulated_controllers>;
using BatteryValues = std::array<Common::Input::BatteryStatus, max_emulated_controllers>;
using CameraValues = Common::Input::CameraStatus;
using RingAnalogValue = Common::Input::AnalogStatus;
using NfcValues = Common::Input::NfcStatus;
using VibrationValues = std::array<Common::Input::VibrationStatus, max_emulated_controllers>;
struct AnalogSticks {
AnalogStickState left{};
AnalogStickState right{};
};
struct ControllerColors {
NpadControllerColor fullkey{};
NpadControllerColor left{};
NpadControllerColor right{};
};
struct BatteryLevelState {
NpadPowerInfo dual{};
NpadPowerInfo left{};
NpadPowerInfo right{};
};
struct CameraState {
Core::IrSensor::ImageTransferProcessorFormat format{};
std::vector<u8> data{};
std::size_t sample{};
};
struct RingSensorForce {
f32 force;
};
using NfcState = Common::Input::NfcStatus;
struct ControllerMotion {
Common::Vec3f accel{};
Common::Vec3f gyro{};
Common::Vec3f rotation{};
Common::Vec3f euler{};
std::array<Common::Vec3f, 3> orientation{};
bool is_at_rest{};
};
enum EmulatedDeviceIndex : u8 {
LeftIndex,
RightIndex,
DualIndex,
AllDevices,
};
using MotionState = std::array<ControllerMotion, 2>;
struct ControllerStatus {
// Data from input_common
ButtonValues button_values{};
SticksValues stick_values{};
ControllerMotionValues motion_values{};
TriggerValues trigger_values{};
ColorValues color_values{};
BatteryValues battery_values{};
VibrationValues vibration_values{};
CameraValues camera_values{};
RingAnalogValue ring_analog_value{};
NfcValues nfc_values{};
// Data for HID services
HomeButtonState home_button_state{};
CaptureButtonState capture_button_state{};
NpadButtonState npad_button_state{};
DebugPadButton debug_pad_button_state{};
AnalogSticks analog_stick_state{};
MotionState motion_state{};
NpadGcTriggerState gc_trigger_state{};
ControllerColors colors_state{};
BatteryLevelState battery_state{};
CameraState camera_state{};
RingSensorForce ring_analog_state{};
NfcState nfc_state{};
Common::Input::PollingMode left_polling_mode{};
Common::Input::PollingMode right_polling_mode{};
};
enum class ControllerTriggerType {
Button,
Stick,
Trigger,
Motion,
Color,
Battery,
Vibration,
IrSensor,
RingController,
Nfc,
Connected,
Disconnected,
Type,
All,
};
struct ControllerUpdateCallback {
std::function<void(ControllerTriggerType)> on_change;
bool is_npad_service;
};
class EmulatedController {
public:
/**
* Contains all input data (buttons, joysticks, vibration, and motion) within this controller.
* @param npad_id_type npad id type for this specific controller
*/
explicit EmulatedController(NpadIdType npad_id_type_);
~EmulatedController();
YUZU_NON_COPYABLE(EmulatedController);
YUZU_NON_MOVEABLE(EmulatedController);
/// Converts the controller type from settings to npad type
static NpadStyleIndex MapSettingsTypeToNPad(Settings::ControllerType type);
/// Converts npad type to the equivalent of controller type from settings
static Settings::ControllerType MapNPadToSettingsType(NpadStyleIndex type);
/// Gets the NpadIdType for this controller
NpadIdType GetNpadIdType() const;
/// Sets the NpadStyleIndex for this controller
void SetNpadStyleIndex(NpadStyleIndex npad_type_);
/**
* Gets the NpadStyleIndex for this controller
* @param get_temporary_value If true tmp_npad_type will be returned
* @return NpadStyleIndex set on the controller
*/
NpadStyleIndex GetNpadStyleIndex(bool get_temporary_value = false) const;
/**
* Sets the supported controller types. Disconnects the controller if current type is not
* supported
* @param supported_styles bitflag with supported types
*/
void SetSupportedNpadStyleTag(NpadStyleTag supported_styles);
/**
* Sets the connected status to true
* @param use_temporary_value If true tmp_npad_type will be used
*/
void Connect(bool use_temporary_value = false);
/// Sets the connected status to false
void Disconnect();
/**
* Is the emulated connected
* @param get_temporary_value If true tmp_is_connected will be returned
* @return true if the controller has the connected status
*/
bool IsConnected(bool get_temporary_value = false) const;
/// Removes all callbacks created from input devices
void UnloadInput();
/**
* Sets the emulated controller into configuring mode
* This prevents the modification of the HID state of the emulated controller by input commands
*/
void EnableConfiguration();
/// Returns the emulated controller into normal mode, allowing the modification of the HID state
void DisableConfiguration();
/// Enables Home and Screenshot buttons
void EnableSystemButtons();
/// Disables Home and Screenshot buttons
void DisableSystemButtons();
/// Sets Home and Screenshot buttons to false
void ResetSystemButtons();
/// Returns true if the emulated controller is in configuring mode
bool IsConfiguring() const;
/// Reload all input devices
void ReloadInput();
/// Overrides current mapped devices with the stored configuration and reloads all input devices
void ReloadFromSettings();
/// Updates current colors with the ones stored in the configuration
void ReloadColorsFromSettings();
/// Saves the current mapped configuration
void SaveCurrentConfig();
/// Reverts any mapped changes made that weren't saved
void RestoreConfig();
/// Returns a vector of mapped devices from the mapped button and stick parameters
std::vector<Common::ParamPackage> GetMappedDevices() const;
// Returns the current mapped button device
Common::ParamPackage GetButtonParam(std::size_t index) const;
// Returns the current mapped stick device
Common::ParamPackage GetStickParam(std::size_t index) const;
// Returns the current mapped motion device
Common::ParamPackage GetMotionParam(std::size_t index) const;
/**
* Updates the current mapped button device
* @param param ParamPackage with controller data to be mapped
*/
void SetButtonParam(std::size_t index, Common::ParamPackage param);
/**
* Updates the current mapped stick device
* @param param ParamPackage with controller data to be mapped
*/
void SetStickParam(std::size_t index, Common::ParamPackage param);
/**
* Updates the current mapped motion device
* @param param ParamPackage with controller data to be mapped
*/
void SetMotionParam(std::size_t index, Common::ParamPackage param);
/// Auto calibrates the current motion devices
void StartMotionCalibration();
/// Returns the latest button status from the controller with parameters
ButtonValues GetButtonsValues() const;
/// Returns the latest analog stick status from the controller with parameters
SticksValues GetSticksValues() const;
/// Returns the latest trigger status from the controller with parameters
TriggerValues GetTriggersValues() const;
/// Returns the latest motion status from the controller with parameters
ControllerMotionValues GetMotionValues() const;
/// Returns the latest color status from the controller with parameters
ColorValues GetColorsValues() const;
/// Returns the latest battery status from the controller with parameters
BatteryValues GetBatteryValues() const;
/// Returns the latest camera status from the controller with parameters
CameraValues GetCameraValues() const;
/// Returns the latest status of analog input from the ring sensor with parameters
RingAnalogValue GetRingSensorValues() const;
/// Returns the latest status of button input for the hid::HomeButton service
HomeButtonState GetHomeButtons() const;
/// Returns the latest status of button input for the hid::CaptureButton service
CaptureButtonState GetCaptureButtons() const;
/// Returns the latest status of button input for the hid::Npad service
NpadButtonState GetNpadButtons() const;
/// Returns the latest status of button input for the debug pad service
DebugPadButton GetDebugPadButtons() const;
/// Returns the latest status of stick input from the mouse
AnalogSticks GetSticks() const;
/// Returns the latest status of trigger input from the mouse
NpadGcTriggerState GetTriggers() const;
/// Returns the latest status of motion input from the mouse
MotionState GetMotions() const;
/// Returns the latest color value from the controller
ControllerColors GetColors() const;
/// Returns the latest battery status from the controller
BatteryLevelState GetBattery() const;
/// Returns the latest camera status from the controller
const CameraState& GetCamera() const;
/// Returns the latest ringcon force sensor value
RingSensorForce GetRingSensorForce() const;
/// Returns the latest ntag status from the controller
const NfcState& GetNfc() const;
/**
* Sends an on/off vibration to the left device
* @return true if vibration had no errors
*/
bool SetVibration(bool should_vibrate);
/**
* Sends an GC vibration to the left device
* @return true if vibration had no errors
*/
bool SetVibration(u32 slot, Core::HID::VibrationGcErmCommand erm_command);
/**
* Sends a specific vibration to the output device
* @return true if vibration had no errors
*/
bool SetVibration(DeviceIndex device_index, const VibrationValue& vibration);
/**
* @return The last sent vibration
*/
VibrationValue GetActualVibrationValue(DeviceIndex device_index) const;
/**
* Sends a small vibration to the output device
* @return true if SetVibration was successful
*/
bool IsVibrationEnabled(std::size_t device_index);
/**
* Sets the desired data to be polled from a controller
* @param device_index index of the controller to set the polling mode
* @param polling_mode type of input desired buttons, gyro, nfc, ir, etc.
* @return driver result from this command
*/
Common::Input::DriverResult SetPollingMode(EmulatedDeviceIndex device_index,
Common::Input::PollingMode polling_mode);
/**
* Get the current polling mode from a controller
* @param device_index index of the controller to set the polling mode
* @return current polling mode
*/
Common::Input::PollingMode GetPollingMode(EmulatedDeviceIndex device_index) const;
/**
* Sets the desired camera format to be polled from a controller
* @param camera_format size of each frame
* @return true if SetCameraFormat was successful
*/
bool SetCameraFormat(Core::IrSensor::ImageTransferProcessorFormat camera_format);
// Returns the current mapped ring device
Common::ParamPackage GetRingParam() const;
/**
* Updates the current mapped ring device
* @param param ParamPackage with ring sensor data to be mapped
*/
void SetRingParam(Common::ParamPackage param);
/// Returns true if the device has nfc support
bool HasNfc() const;
/// Sets the joycon in nfc mode and increments the handle count
bool AddNfcHandle();
/// Decrements the handle count if zero sets the joycon in active mode
bool RemoveNfcHandle();
/// Start searching for nfc tags
bool StartNfcPolling();
/// Stop searching for nfc tags
bool StopNfcPolling();
/// Returns true if the nfc tag was readable
bool ReadAmiiboData(std::vector<u8>& data);
/// Returns true if the nfc tag was written
bool WriteNfc(const std::vector<u8>& data);
/// Returns true if the nfc tag was readable
bool ReadMifareData(const Common::Input::MifareRequest& request,
Common::Input::MifareRequest& out_data);
/// Returns true if the nfc tag was written
bool WriteMifareData(const Common::Input::MifareRequest& request);
/// Returns the led pattern corresponding to this emulated controller
LedPattern GetLedPattern() const;
/// Asks the output device to change the player led pattern
void SetLedPattern();
/// Changes sensitivity of the motion sensor
void SetGyroscopeZeroDriftMode(GyroscopeZeroDriftMode mode);
/**
* Adds a callback to the list of events
* @param update_callback A ConsoleUpdateCallback that will be triggered
* @return an unique key corresponding to the callback index in the list
*/
int SetCallback(ControllerUpdateCallback update_callback);
/**
* Removes a callback from the list stopping any future events to this object
* @param key Key corresponding to the callback index in the list
*/
void DeleteCallback(int key);
/// Swaps the state of the turbo buttons and updates motion input
void StatusUpdate();
private:
/// creates input devices from params
void LoadDevices();
/// Set the params for TAS devices
void LoadTASParams();
/// Set the params for virtual pad devices
void LoadVirtualGamepadParams();
/**
* @param use_temporary_value If true tmp_npad_type will be used
* @return true if the controller style is fullkey
*/
bool IsControllerFullkey(bool use_temporary_value = false) const;
/**
* Checks the current controller type against the supported_style_tag
* @param use_temporary_value If true tmp_npad_type will be used
* @return true if the controller is supported
*/
bool IsControllerSupported(bool use_temporary_value = false) const;
/**
* Updates the button status of the controller
* @param callback A CallbackStatus containing the button status
* @param index Button ID of the to be updated
*/
void SetButton(const Common::Input::CallbackStatus& callback, std::size_t index,
Common::UUID uuid);
/**
* Updates the analog stick status of the controller
* @param callback A CallbackStatus containing the analog stick status
* @param index stick ID of the to be updated
*/
void SetStick(const Common::Input::CallbackStatus& callback, std::size_t index,
Common::UUID uuid);
/**
* Updates the trigger status of the controller
* @param callback A CallbackStatus containing the trigger status
* @param index trigger ID of the to be updated
*/
void SetTrigger(const Common::Input::CallbackStatus& callback, std::size_t index,
Common::UUID uuid);
/**
* Updates the motion status of the controller
* @param callback A CallbackStatus containing gyro and accelerometer data
* @param index motion ID of the to be updated
*/
void SetMotion(const Common::Input::CallbackStatus& callback, std::size_t index);
/**
* Updates the color status of the controller
* @param callback A CallbackStatus containing the color status
* @param index color ID of the to be updated
*/
void SetColors(const Common::Input::CallbackStatus& callback, std::size_t index);
/**
* Updates the battery status of the controller
* @param callback A CallbackStatus containing the battery status
* @param index battery ID of the to be updated
*/
void SetBattery(const Common::Input::CallbackStatus& callback, std::size_t index);
/**
* Updates the camera status of the controller
* @param callback A CallbackStatus containing the camera status
*/
void SetCamera(const Common::Input::CallbackStatus& callback);
/**
* Updates the ring analog sensor status of the ring controller
* @param callback A CallbackStatus containing the force status
*/
void SetRingAnalog(const Common::Input::CallbackStatus& callback);
/**
* Updates the nfc status of the controller
* @param callback A CallbackStatus containing the nfc status
*/
void SetNfc(const Common::Input::CallbackStatus& callback);
/**
* Converts a color format from bgra to rgba
* @param color in bgra format
* @return NpadColor in rgba format
*/
NpadColor GetNpadColor(u32 color);
/**
* Triggers a callback that something has changed on the controller status
* @param type Input type of the event to trigger
* @param is_service_update indicates if this event should only be sent to HID services
*/
void TriggerOnChange(ControllerTriggerType type, bool is_service_update);
NpadButton GetTurboButtonMask() const;
const NpadIdType npad_id_type;
NpadStyleIndex npad_type{NpadStyleIndex::None};
NpadStyleIndex original_npad_type{NpadStyleIndex::None};
NpadStyleTag supported_style_tag{NpadStyleSet::All};
bool is_connected{false};
bool is_configuring{false};
bool is_initialized{false};
bool system_buttons_enabled{true};
f32 motion_sensitivity{Core::HID::MotionInput::IsAtRestStandard};
u32 turbo_button_state{0};
std::size_t nfc_handles{0};
std::array<VibrationValue, 2> last_vibration_value{DEFAULT_VIBRATION_VALUE,
DEFAULT_VIBRATION_VALUE};
std::array<std::chrono::steady_clock::time_point, 2> last_vibration_timepoint{};
// Temporary values to avoid doing changes while the controller is in configuring mode
NpadStyleIndex tmp_npad_type{NpadStyleIndex::None};
bool tmp_is_connected{false};
ButtonParams button_params;
StickParams stick_params;
ControllerMotionParams motion_params;
TriggerParams trigger_params;
BatteryParams battery_params;
ColorParams color_params;
CameraParams camera_params;
RingAnalogParams ring_params;
NfcParams nfc_params;
Common::ParamPackage android_params;
OutputParams output_params;
ButtonDevices button_devices;
StickDevices stick_devices;
ControllerMotionDevices motion_devices;
TriggerDevices trigger_devices;
BatteryDevices battery_devices;
ColorDevices color_devices;
CameraDevices camera_devices;
RingAnalogDevices ring_analog_devices;
NfcDevices nfc_devices;
OutputDevices output_devices;
// TAS related variables
ButtonParams tas_button_params;
StickParams tas_stick_params;
ButtonDevices tas_button_devices;
StickDevices tas_stick_devices;
// Virtual gamepad related variables
ButtonParams virtual_button_params;
StickParams virtual_stick_params;
ControllerMotionParams virtual_motion_params;
ButtonDevices virtual_button_devices;
StickDevices virtual_stick_devices;
ControllerMotionDevices virtual_motion_devices;
mutable std::mutex mutex;
mutable std::mutex callback_mutex;
mutable std::mutex npad_mutex;
mutable std::mutex connect_mutex;
std::unordered_map<int, ControllerUpdateCallback> callback_list;
int last_callback_key = 0;
// Stores the current status of all controller input
ControllerStatus controller;
};
} // namespace Core::HID

View File

@@ -0,0 +1,483 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <algorithm>
#include <fmt/format.h>
#include "hid_core/frontend/emulated_devices.h"
#include "hid_core/frontend/input_converter.h"
namespace Core::HID {
EmulatedDevices::EmulatedDevices() = default;
EmulatedDevices::~EmulatedDevices() = default;
void EmulatedDevices::ReloadFromSettings() {
ReloadInput();
}
void EmulatedDevices::ReloadInput() {
// If you load any device here add the equivalent to the UnloadInput() function
// Native Mouse is mapped on port 1, pad 0
const Common::ParamPackage mouse_params{"engine:mouse,port:1,pad:0"};
// Keyboard keys is mapped on port 1, pad 0 for normal keys, pad 1 for moddifier keys
const Common::ParamPackage keyboard_params{"engine:keyboard,port:1"};
std::size_t key_index = 0;
for (auto& mouse_device : mouse_button_devices) {
Common::ParamPackage mouse_button_params = mouse_params;
mouse_button_params.Set("button", static_cast<int>(key_index));
mouse_device = Common::Input::CreateInputDevice(mouse_button_params);
key_index++;
}
Common::ParamPackage mouse_position_params = mouse_params;
mouse_position_params.Set("axis_x", 0);
mouse_position_params.Set("axis_y", 1);
mouse_position_params.Set("deadzone", 0.0f);
mouse_position_params.Set("range", 1.0f);
mouse_position_params.Set("threshold", 0.0f);
mouse_stick_device = Common::Input::CreateInputDevice(mouse_position_params);
// First two axis are reserved for mouse position
key_index = 2;
for (auto& mouse_device : mouse_wheel_devices) {
Common::ParamPackage mouse_wheel_params = mouse_params;
mouse_wheel_params.Set("axis", static_cast<int>(key_index));
mouse_device = Common::Input::CreateInputDevice(mouse_wheel_params);
key_index++;
}
key_index = 0;
for (auto& keyboard_device : keyboard_devices) {
Common::ParamPackage keyboard_key_params = keyboard_params;
keyboard_key_params.Set("button", static_cast<int>(key_index));
keyboard_key_params.Set("pad", 0);
keyboard_device = Common::Input::CreateInputDevice(keyboard_key_params);
key_index++;
}
key_index = 0;
for (auto& keyboard_device : keyboard_modifier_devices) {
Common::ParamPackage keyboard_moddifier_params = keyboard_params;
keyboard_moddifier_params.Set("button", static_cast<int>(key_index));
keyboard_moddifier_params.Set("pad", 1);
keyboard_device = Common::Input::CreateInputDevice(keyboard_moddifier_params);
key_index++;
}
for (std::size_t index = 0; index < mouse_button_devices.size(); ++index) {
if (!mouse_button_devices[index]) {
continue;
}
mouse_button_devices[index]->SetCallback({
.on_change =
[this, index](const Common::Input::CallbackStatus& callback) {
SetMouseButton(callback, index);
},
});
}
for (std::size_t index = 0; index < mouse_wheel_devices.size(); ++index) {
if (!mouse_wheel_devices[index]) {
continue;
}
mouse_wheel_devices[index]->SetCallback({
.on_change =
[this, index](const Common::Input::CallbackStatus& callback) {
SetMouseWheel(callback, index);
},
});
}
if (mouse_stick_device) {
mouse_stick_device->SetCallback({
.on_change =
[this](const Common::Input::CallbackStatus& callback) {
SetMousePosition(callback);
},
});
}
for (std::size_t index = 0; index < keyboard_devices.size(); ++index) {
if (!keyboard_devices[index]) {
continue;
}
keyboard_devices[index]->SetCallback({
.on_change =
[this, index](const Common::Input::CallbackStatus& callback) {
SetKeyboardButton(callback, index);
},
});
}
for (std::size_t index = 0; index < keyboard_modifier_devices.size(); ++index) {
if (!keyboard_modifier_devices[index]) {
continue;
}
keyboard_modifier_devices[index]->SetCallback({
.on_change =
[this, index](const Common::Input::CallbackStatus& callback) {
SetKeyboardModifier(callback, index);
},
});
}
}
void EmulatedDevices::UnloadInput() {
for (auto& button : mouse_button_devices) {
button.reset();
}
for (auto& analog : mouse_wheel_devices) {
analog.reset();
}
mouse_stick_device.reset();
for (auto& button : keyboard_devices) {
button.reset();
}
for (auto& button : keyboard_modifier_devices) {
button.reset();
}
}
void EmulatedDevices::EnableConfiguration() {
is_configuring = true;
SaveCurrentConfig();
}
void EmulatedDevices::DisableConfiguration() {
is_configuring = false;
}
bool EmulatedDevices::IsConfiguring() const {
return is_configuring;
}
void EmulatedDevices::SaveCurrentConfig() {
if (!is_configuring) {
return;
}
}
void EmulatedDevices::RestoreConfig() {
if (!is_configuring) {
return;
}
ReloadFromSettings();
}
void EmulatedDevices::SetKeyboardButton(const Common::Input::CallbackStatus& callback,
std::size_t index) {
if (index >= device_status.keyboard_values.size()) {
return;
}
std::unique_lock lock{mutex};
bool value_changed = false;
const auto new_status = TransformToButton(callback);
auto& current_status = device_status.keyboard_values[index];
current_status.toggle = new_status.toggle;
// Update button status with current status
if (!current_status.toggle) {
current_status.locked = false;
if (current_status.value != new_status.value) {
current_status.value = new_status.value;
value_changed = true;
}
} else {
// Toggle button and lock status
if (new_status.value && !current_status.locked) {
current_status.locked = true;
current_status.value = !current_status.value;
value_changed = true;
}
// Unlock button, ready for next press
if (!new_status.value && current_status.locked) {
current_status.locked = false;
}
}
if (!value_changed) {
return;
}
if (is_configuring) {
lock.unlock();
TriggerOnChange(DeviceTriggerType::Keyboard);
return;
}
// Index should be converted from NativeKeyboard to KeyboardKeyIndex
UpdateKey(index, current_status.value);
lock.unlock();
TriggerOnChange(DeviceTriggerType::Keyboard);
}
void EmulatedDevices::UpdateKey(std::size_t key_index, bool status) {
constexpr std::size_t KEYS_PER_BYTE = 8;
auto& entry = device_status.keyboard_state.key[key_index / KEYS_PER_BYTE];
const u8 mask = static_cast<u8>(1 << (key_index % KEYS_PER_BYTE));
if (status) {
entry = entry | mask;
} else {
entry = static_cast<u8>(entry & ~mask);
}
}
void EmulatedDevices::SetKeyboardModifier(const Common::Input::CallbackStatus& callback,
std::size_t index) {
if (index >= device_status.keyboard_moddifier_values.size()) {
return;
}
std::unique_lock lock{mutex};
bool value_changed = false;
const auto new_status = TransformToButton(callback);
auto& current_status = device_status.keyboard_moddifier_values[index];
current_status.toggle = new_status.toggle;
// Update button status with current
if (!current_status.toggle) {
current_status.locked = false;
if (current_status.value != new_status.value) {
current_status.value = new_status.value;
value_changed = true;
}
} else {
// Toggle button and lock status
if (new_status.value && !current_status.locked) {
current_status.locked = true;
current_status.value = !current_status.value;
value_changed = true;
}
// Unlock button ready for next press
if (!new_status.value && current_status.locked) {
current_status.locked = false;
}
}
if (!value_changed) {
return;
}
if (is_configuring) {
lock.unlock();
TriggerOnChange(DeviceTriggerType::KeyboardModdifier);
return;
}
switch (index) {
case Settings::NativeKeyboard::LeftControl:
case Settings::NativeKeyboard::RightControl:
device_status.keyboard_moddifier_state.control.Assign(current_status.value);
break;
case Settings::NativeKeyboard::LeftShift:
case Settings::NativeKeyboard::RightShift:
device_status.keyboard_moddifier_state.shift.Assign(current_status.value);
break;
case Settings::NativeKeyboard::LeftAlt:
device_status.keyboard_moddifier_state.left_alt.Assign(current_status.value);
break;
case Settings::NativeKeyboard::RightAlt:
device_status.keyboard_moddifier_state.right_alt.Assign(current_status.value);
break;
case Settings::NativeKeyboard::CapsLock:
device_status.keyboard_moddifier_state.caps_lock.Assign(current_status.value);
break;
case Settings::NativeKeyboard::ScrollLock:
device_status.keyboard_moddifier_state.scroll_lock.Assign(current_status.value);
break;
case Settings::NativeKeyboard::NumLock:
device_status.keyboard_moddifier_state.num_lock.Assign(current_status.value);
break;
}
lock.unlock();
TriggerOnChange(DeviceTriggerType::KeyboardModdifier);
}
void EmulatedDevices::SetMouseButton(const Common::Input::CallbackStatus& callback,
std::size_t index) {
if (index >= device_status.mouse_button_values.size()) {
return;
}
std::unique_lock lock{mutex};
bool value_changed = false;
const auto new_status = TransformToButton(callback);
auto& current_status = device_status.mouse_button_values[index];
current_status.toggle = new_status.toggle;
// Update button status with current
if (!current_status.toggle) {
current_status.locked = false;
if (current_status.value != new_status.value) {
current_status.value = new_status.value;
value_changed = true;
}
} else {
// Toggle button and lock status
if (new_status.value && !current_status.locked) {
current_status.locked = true;
current_status.value = !current_status.value;
value_changed = true;
}
// Unlock button ready for next press
if (!new_status.value && current_status.locked) {
current_status.locked = false;
}
}
if (!value_changed) {
return;
}
if (is_configuring) {
lock.unlock();
TriggerOnChange(DeviceTriggerType::Mouse);
return;
}
switch (index) {
case Settings::NativeMouseButton::Left:
device_status.mouse_button_state.left.Assign(current_status.value);
break;
case Settings::NativeMouseButton::Right:
device_status.mouse_button_state.right.Assign(current_status.value);
break;
case Settings::NativeMouseButton::Middle:
device_status.mouse_button_state.middle.Assign(current_status.value);
break;
case Settings::NativeMouseButton::Forward:
device_status.mouse_button_state.forward.Assign(current_status.value);
break;
case Settings::NativeMouseButton::Back:
device_status.mouse_button_state.back.Assign(current_status.value);
break;
}
lock.unlock();
TriggerOnChange(DeviceTriggerType::Mouse);
}
void EmulatedDevices::SetMouseWheel(const Common::Input::CallbackStatus& callback,
std::size_t index) {
if (index >= device_status.mouse_wheel_values.size()) {
return;
}
std::unique_lock lock{mutex};
const auto analog_value = TransformToAnalog(callback);
device_status.mouse_wheel_values[index] = analog_value;
if (is_configuring) {
device_status.mouse_wheel_state = {};
lock.unlock();
TriggerOnChange(DeviceTriggerType::Mouse);
return;
}
switch (index) {
case Settings::NativeMouseWheel::X:
device_status.mouse_wheel_state.x = static_cast<s32>(analog_value.value);
break;
case Settings::NativeMouseWheel::Y:
device_status.mouse_wheel_state.y = static_cast<s32>(analog_value.value);
break;
}
lock.unlock();
TriggerOnChange(DeviceTriggerType::Mouse);
}
void EmulatedDevices::SetMousePosition(const Common::Input::CallbackStatus& callback) {
std::unique_lock lock{mutex};
const auto touch_value = TransformToTouch(callback);
device_status.mouse_stick_value = touch_value;
if (is_configuring) {
device_status.mouse_position_state = {};
lock.unlock();
TriggerOnChange(DeviceTriggerType::Mouse);
return;
}
device_status.mouse_position_state.x = touch_value.x.value;
device_status.mouse_position_state.y = touch_value.y.value;
lock.unlock();
TriggerOnChange(DeviceTriggerType::Mouse);
}
KeyboardValues EmulatedDevices::GetKeyboardValues() const {
std::scoped_lock lock{mutex};
return device_status.keyboard_values;
}
KeyboardModifierValues EmulatedDevices::GetKeyboardModdifierValues() const {
std::scoped_lock lock{mutex};
return device_status.keyboard_moddifier_values;
}
MouseButtonValues EmulatedDevices::GetMouseButtonsValues() const {
std::scoped_lock lock{mutex};
return device_status.mouse_button_values;
}
KeyboardKey EmulatedDevices::GetKeyboard() const {
std::scoped_lock lock{mutex};
return device_status.keyboard_state;
}
KeyboardModifier EmulatedDevices::GetKeyboardModifier() const {
std::scoped_lock lock{mutex};
return device_status.keyboard_moddifier_state;
}
MouseButton EmulatedDevices::GetMouseButtons() const {
std::scoped_lock lock{mutex};
return device_status.mouse_button_state;
}
MousePosition EmulatedDevices::GetMousePosition() const {
std::scoped_lock lock{mutex};
return device_status.mouse_position_state;
}
AnalogStickState EmulatedDevices::GetMouseWheel() const {
std::scoped_lock lock{mutex};
return device_status.mouse_wheel_state;
}
void EmulatedDevices::TriggerOnChange(DeviceTriggerType type) {
std::scoped_lock lock{callback_mutex};
for (const auto& poller_pair : callback_list) {
const InterfaceUpdateCallback& poller = poller_pair.second;
if (poller.on_change) {
poller.on_change(type);
}
}
}
int EmulatedDevices::SetCallback(InterfaceUpdateCallback update_callback) {
std::scoped_lock lock{callback_mutex};
callback_list.insert_or_assign(last_callback_key, std::move(update_callback));
return last_callback_key++;
}
void EmulatedDevices::DeleteCallback(int key) {
std::scoped_lock lock{callback_mutex};
const auto& iterator = callback_list.find(key);
if (iterator == callback_list.end()) {
LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);
return;
}
callback_list.erase(iterator);
}
} // namespace Core::HID

View File

@@ -0,0 +1,212 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <array>
#include <functional>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>
#include "common/common_types.h"
#include "common/input.h"
#include "common/param_package.h"
#include "common/settings.h"
#include "hid_core/hid_types.h"
namespace Core::HID {
using KeyboardDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
Settings::NativeKeyboard::NumKeyboardKeys>;
using KeyboardModifierDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
Settings::NativeKeyboard::NumKeyboardMods>;
using MouseButtonDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
Settings::NativeMouseButton::NumMouseButtons>;
using MouseWheelDevices = std::array<std::unique_ptr<Common::Input::InputDevice>,
Settings::NativeMouseWheel::NumMouseWheels>;
using MouseStickDevice = std::unique_ptr<Common::Input::InputDevice>;
using MouseButtonParams =
std::array<Common::ParamPackage, Settings::NativeMouseButton::NumMouseButtons>;
using KeyboardValues =
std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardKeys>;
using KeyboardModifierValues =
std::array<Common::Input::ButtonStatus, Settings::NativeKeyboard::NumKeyboardMods>;
using MouseButtonValues =
std::array<Common::Input::ButtonStatus, Settings::NativeMouseButton::NumMouseButtons>;
using MouseWheelValues =
std::array<Common::Input::AnalogStatus, Settings::NativeMouseWheel::NumMouseWheels>;
using MouseStickValue = Common::Input::TouchStatus;
struct MousePosition {
f32 x;
f32 y;
};
struct DeviceStatus {
// Data from input_common
KeyboardValues keyboard_values{};
KeyboardModifierValues keyboard_moddifier_values{};
MouseButtonValues mouse_button_values{};
MouseWheelValues mouse_wheel_values{};
MouseStickValue mouse_stick_value{};
// Data for HID services
KeyboardKey keyboard_state{};
KeyboardModifier keyboard_moddifier_state{};
MouseButton mouse_button_state{};
MousePosition mouse_position_state{};
AnalogStickState mouse_wheel_state{};
};
enum class DeviceTriggerType {
Keyboard,
KeyboardModdifier,
Mouse,
RingController,
};
struct InterfaceUpdateCallback {
std::function<void(DeviceTriggerType)> on_change;
};
class EmulatedDevices {
public:
/**
* Contains all input data related to external devices that aren't necessarily a controller
* This includes devices such as the keyboard or mouse
*/
explicit EmulatedDevices();
~EmulatedDevices();
YUZU_NON_COPYABLE(EmulatedDevices);
YUZU_NON_MOVEABLE(EmulatedDevices);
/// Removes all callbacks created from input devices
void UnloadInput();
/**
* Sets the emulated devices into configuring mode
* This prevents the modification of the HID state of the emulated devices by input commands
*/
void EnableConfiguration();
/// Returns the emulated devices into normal mode, allowing the modification of the HID state
void DisableConfiguration();
/// Returns true if the emulated device is in configuring mode
bool IsConfiguring() const;
/// Reload all input devices
void ReloadInput();
/// Overrides current mapped devices with the stored configuration and reloads all input devices
void ReloadFromSettings();
/// Saves the current mapped configuration
void SaveCurrentConfig();
/// Reverts any mapped changes made that weren't saved
void RestoreConfig();
/// Returns the latest status of button input from the keyboard with parameters
KeyboardValues GetKeyboardValues() const;
/// Returns the latest status of button input from the keyboard modifiers with parameters
KeyboardModifierValues GetKeyboardModdifierValues() const;
/// Returns the latest status of button input from the mouse with parameters
MouseButtonValues GetMouseButtonsValues() const;
/// Returns the latest status of button input from the keyboard
KeyboardKey GetKeyboard() const;
/// Returns the latest status of button input from the keyboard modifiers
KeyboardModifier GetKeyboardModifier() const;
/// Returns the latest status of button input from the mouse
MouseButton GetMouseButtons() const;
/// Returns the latest mouse coordinates
MousePosition GetMousePosition() const;
/// Returns the latest mouse wheel change
AnalogStickState GetMouseWheel() const;
/**
* Adds a callback to the list of events
* @param update_callback InterfaceUpdateCallback that will be triggered
* @return an unique key corresponding to the callback index in the list
*/
int SetCallback(InterfaceUpdateCallback update_callback);
/**
* Removes a callback from the list stopping any future events to this object
* @param key Key corresponding to the callback index in the list
*/
void DeleteCallback(int key);
private:
/// Helps assigning a value to keyboard_state
void UpdateKey(std::size_t key_index, bool status);
/**
* Updates the touch status of the keyboard device
* @param callback A CallbackStatus containing the key status
* @param index key ID to be updated
*/
void SetKeyboardButton(const Common::Input::CallbackStatus& callback, std::size_t index);
/**
* Updates the keyboard status of the keyboard device
* @param callback A CallbackStatus containing the modifier key status
* @param index modifier key ID to be updated
*/
void SetKeyboardModifier(const Common::Input::CallbackStatus& callback, std::size_t index);
/**
* Updates the mouse button status of the mouse device
* @param callback A CallbackStatus containing the button status
* @param index Button ID to be updated
*/
void SetMouseButton(const Common::Input::CallbackStatus& callback, std::size_t index);
/**
* Updates the mouse wheel status of the mouse device
* @param callback A CallbackStatus containing the wheel status
* @param index wheel ID to be updated
*/
void SetMouseWheel(const Common::Input::CallbackStatus& callback, std::size_t index);
/**
* Updates the mouse position status of the mouse device
* @param callback A CallbackStatus containing the position status
*/
void SetMousePosition(const Common::Input::CallbackStatus& callback);
/**
* Triggers a callback that something has changed on the device status
* @param type Input type of the event to trigger
*/
void TriggerOnChange(DeviceTriggerType type);
bool is_configuring{false};
KeyboardDevices keyboard_devices;
KeyboardModifierDevices keyboard_modifier_devices;
MouseButtonDevices mouse_button_devices;
MouseWheelDevices mouse_wheel_devices;
MouseStickDevice mouse_stick_device;
mutable std::mutex mutex;
mutable std::mutex callback_mutex;
std::unordered_map<int, InterfaceUpdateCallback> callback_list;
int last_callback_key = 0;
// Stores the current status of all external device input
DeviceStatus device_status;
};
} // namespace Core::HID

View File

@@ -0,0 +1,436 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <algorithm>
#include <random>
#include "common/input.h"
#include "hid_core/frontend/input_converter.h"
namespace Core::HID {
Common::Input::BatteryStatus TransformToBattery(const Common::Input::CallbackStatus& callback) {
Common::Input::BatteryStatus battery{Common::Input::BatteryStatus::None};
switch (callback.type) {
case Common::Input::InputType::Analog:
case Common::Input::InputType::Trigger: {
const auto value = TransformToTrigger(callback).analog.value;
battery = Common::Input::BatteryLevel::Empty;
if (value > 0.2f) {
battery = Common::Input::BatteryLevel::Critical;
}
if (value > 0.4f) {
battery = Common::Input::BatteryLevel::Low;
}
if (value > 0.6f) {
battery = Common::Input::BatteryLevel::Medium;
}
if (value > 0.8f) {
battery = Common::Input::BatteryLevel::Full;
}
if (value >= 0.95f) {
battery = Common::Input::BatteryLevel::Charging;
}
break;
}
case Common::Input::InputType::Button:
battery = callback.button_status.value ? Common::Input::BatteryLevel::Charging
: Common::Input::BatteryLevel::Critical;
break;
case Common::Input::InputType::Battery:
battery = callback.battery_status;
break;
default:
LOG_ERROR(Input, "Conversion from type {} to battery not implemented", callback.type);
break;
}
return battery;
}
Common::Input::ButtonStatus TransformToButton(const Common::Input::CallbackStatus& callback) {
Common::Input::ButtonStatus status{};
switch (callback.type) {
case Common::Input::InputType::Analog:
status.value = TransformToTrigger(callback).pressed.value;
status.toggle = callback.analog_status.properties.toggle;
status.inverted = callback.analog_status.properties.inverted_button;
break;
case Common::Input::InputType::Trigger:
status.value = TransformToTrigger(callback).pressed.value;
break;
case Common::Input::InputType::Button:
status = callback.button_status;
break;
case Common::Input::InputType::Motion:
status.value = std::abs(callback.motion_status.gyro.x.raw_value) > 1.0f;
break;
default:
LOG_ERROR(Input, "Conversion from type {} to button not implemented", callback.type);
break;
}
if (status.inverted) {
status.value = !status.value;
}
return status;
}
Common::Input::MotionStatus TransformToMotion(const Common::Input::CallbackStatus& callback) {
Common::Input::MotionStatus status{};
switch (callback.type) {
case Common::Input::InputType::Button: {
Common::Input::AnalogProperties properties{
.deadzone = 0.0f,
.range = 1.0f,
.offset = 0.0f,
};
status.delta_timestamp = 1000;
status.force_update = true;
status.accel.x = {
.value = 0.0f,
.raw_value = 0.0f,
.properties = properties,
};
status.accel.y = {
.value = 0.0f,
.raw_value = 0.0f,
.properties = properties,
};
status.accel.z = {
.value = 0.0f,
.raw_value = -1.0f,
.properties = properties,
};
status.gyro.x = {
.value = 0.0f,
.raw_value = 0.0f,
.properties = properties,
};
status.gyro.y = {
.value = 0.0f,
.raw_value = 0.0f,
.properties = properties,
};
status.gyro.z = {
.value = 0.0f,
.raw_value = 0.0f,
.properties = properties,
};
if (TransformToButton(callback).value) {
std::random_device device;
std::mt19937 gen(device());
std::uniform_int_distribution<s16> distribution(-5000, 5000);
status.accel.x.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
status.accel.y.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
status.accel.z.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
status.gyro.x.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
status.gyro.y.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
status.gyro.z.raw_value = static_cast<f32>(distribution(gen)) * 0.001f;
}
break;
}
case Common::Input::InputType::Motion:
status = callback.motion_status;
break;
default:
LOG_ERROR(Input, "Conversion from type {} to motion not implemented", callback.type);
break;
}
SanitizeAnalog(status.accel.x, false);
SanitizeAnalog(status.accel.y, false);
SanitizeAnalog(status.accel.z, false);
SanitizeAnalog(status.gyro.x, false);
SanitizeAnalog(status.gyro.y, false);
SanitizeAnalog(status.gyro.z, false);
return status;
}
Common::Input::StickStatus TransformToStick(const Common::Input::CallbackStatus& callback) {
Common::Input::StickStatus status{};
switch (callback.type) {
case Common::Input::InputType::Stick:
status = callback.stick_status;
break;
default:
LOG_ERROR(Input, "Conversion from type {} to stick not implemented", callback.type);
break;
}
SanitizeStick(status.x, status.y, true);
const auto& properties_x = status.x.properties;
const auto& properties_y = status.y.properties;
const float x = status.x.value;
const float y = status.y.value;
// Set directional buttons
status.right = x > properties_x.threshold;
status.left = x < -properties_x.threshold;
status.up = y > properties_y.threshold;
status.down = y < -properties_y.threshold;
return status;
}
Common::Input::TouchStatus TransformToTouch(const Common::Input::CallbackStatus& callback) {
Common::Input::TouchStatus status{};
switch (callback.type) {
case Common::Input::InputType::Touch:
status = callback.touch_status;
break;
case Common::Input::InputType::Stick:
status.x = callback.stick_status.x;
status.y = callback.stick_status.y;
break;
default:
LOG_ERROR(Input, "Conversion from type {} to touch not implemented", callback.type);
break;
}
SanitizeAnalog(status.x, true);
SanitizeAnalog(status.y, true);
float& x = status.x.value;
float& y = status.y.value;
// Adjust if value is inverted
x = status.x.properties.inverted ? 1.0f + x : x;
y = status.y.properties.inverted ? 1.0f + y : y;
// clamp value
x = std::clamp(x, 0.0f, 1.0f);
y = std::clamp(y, 0.0f, 1.0f);
if (status.pressed.inverted) {
status.pressed.value = !status.pressed.value;
}
return status;
}
Common::Input::TriggerStatus TransformToTrigger(const Common::Input::CallbackStatus& callback) {
Common::Input::TriggerStatus status{};
float& raw_value = status.analog.raw_value;
bool calculate_button_value = true;
switch (callback.type) {
case Common::Input::InputType::Analog:
status.analog.properties = callback.analog_status.properties;
raw_value = callback.analog_status.raw_value;
break;
case Common::Input::InputType::Button:
status.analog.properties.range = 1.0f;
status.analog.properties.inverted = callback.button_status.inverted;
raw_value = callback.button_status.value ? 1.0f : 0.0f;
break;
case Common::Input::InputType::Trigger:
status = callback.trigger_status;
calculate_button_value = false;
break;
case Common::Input::InputType::Motion:
status.analog.properties.range = 1.0f;
raw_value = callback.motion_status.accel.x.raw_value;
break;
default:
LOG_ERROR(Input, "Conversion from type {} to trigger not implemented", callback.type);
break;
}
SanitizeAnalog(status.analog, true);
const auto& properties = status.analog.properties;
float& value = status.analog.value;
// Set button status
if (calculate_button_value) {
status.pressed.value = value > properties.threshold;
}
// Adjust if value is inverted
value = properties.inverted ? 1.0f + value : value;
// clamp value
value = std::clamp(value, 0.0f, 1.0f);
return status;
}
Common::Input::AnalogStatus TransformToAnalog(const Common::Input::CallbackStatus& callback) {
Common::Input::AnalogStatus status{};
switch (callback.type) {
case Common::Input::InputType::Analog:
status.properties = callback.analog_status.properties;
status.raw_value = callback.analog_status.raw_value;
break;
default:
LOG_ERROR(Input, "Conversion from type {} to analog not implemented", callback.type);
break;
}
SanitizeAnalog(status, false);
// Adjust if value is inverted
status.value = status.properties.inverted ? -status.value : status.value;
return status;
}
Common::Input::CameraStatus TransformToCamera(const Common::Input::CallbackStatus& callback) {
Common::Input::CameraStatus camera{};
switch (callback.type) {
case Common::Input::InputType::IrSensor:
camera = {
.format = callback.camera_status,
.data = callback.raw_data,
};
break;
default:
LOG_ERROR(Input, "Conversion from type {} to camera not implemented", callback.type);
break;
}
return camera;
}
Common::Input::NfcStatus TransformToNfc(const Common::Input::CallbackStatus& callback) {
Common::Input::NfcStatus nfc{};
switch (callback.type) {
case Common::Input::InputType::Nfc:
return callback.nfc_status;
default:
LOG_ERROR(Input, "Conversion from type {} to NFC not implemented", callback.type);
break;
}
return nfc;
}
Common::Input::BodyColorStatus TransformToColor(const Common::Input::CallbackStatus& callback) {
switch (callback.type) {
case Common::Input::InputType::Color:
return callback.color_status;
break;
default:
LOG_ERROR(Input, "Conversion from type {} to color not implemented", callback.type);
return {};
break;
}
}
void SanitizeAnalog(Common::Input::AnalogStatus& analog, bool clamp_value) {
const auto& properties = analog.properties;
float& raw_value = analog.raw_value;
float& value = analog.value;
if (!std::isnormal(raw_value)) {
raw_value = 0;
}
// Apply center offset
raw_value -= properties.offset;
// Set initial values to be formatted
value = raw_value;
// Calculate vector size
const float r = std::abs(value);
// Return zero if value is smaller than the deadzone
if (r <= properties.deadzone || properties.deadzone == 1.0f) {
analog.value = 0;
return;
}
// Adjust range of value
const float deadzone_factor =
1.0f / r * (r - properties.deadzone) / (1.0f - properties.deadzone);
value = value * deadzone_factor / properties.range;
// Invert direction if needed
if (properties.inverted) {
value = -value;
}
// Clamp value
if (clamp_value) {
value = std::clamp(value, -1.0f, 1.0f);
}
}
void SanitizeStick(Common::Input::AnalogStatus& analog_x, Common::Input::AnalogStatus& analog_y,
bool clamp_value) {
const auto& properties_x = analog_x.properties;
const auto& properties_y = analog_y.properties;
float& raw_x = analog_x.raw_value;
float& raw_y = analog_y.raw_value;
float& x = analog_x.value;
float& y = analog_y.value;
if (!std::isnormal(raw_x)) {
raw_x = 0;
}
if (!std::isnormal(raw_y)) {
raw_y = 0;
}
// Apply center offset
raw_x += properties_x.offset;
raw_y += properties_y.offset;
// Apply X scale correction from offset
if (std::abs(properties_x.offset) < 0.75f) {
if (raw_x > 0) {
raw_x /= 1 + properties_x.offset;
} else {
raw_x /= 1 - properties_x.offset;
}
}
// Apply Y scale correction from offset
if (std::abs(properties_y.offset) < 0.75f) {
if (raw_y > 0) {
raw_y /= 1 + properties_y.offset;
} else {
raw_y /= 1 - properties_y.offset;
}
}
// Invert direction if needed
raw_x = properties_x.inverted ? -raw_x : raw_x;
raw_y = properties_y.inverted ? -raw_y : raw_y;
// Set initial values to be formatted
x = raw_x;
y = raw_y;
// Calculate vector size
float r = x * x + y * y;
r = std::sqrt(r);
// TODO(German77): Use deadzone and range of both axis
// Return zero if values are smaller than the deadzone
if (r <= properties_x.deadzone || properties_x.deadzone >= 1.0f) {
x = 0;
y = 0;
return;
}
// Adjust range of joystick
const float deadzone_factor =
1.0f / r * (r - properties_x.deadzone) / (1.0f - properties_x.deadzone);
x = x * deadzone_factor / properties_x.range;
y = y * deadzone_factor / properties_x.range;
r = r * deadzone_factor / properties_x.range;
// Normalize joystick
if (clamp_value && r > 1.0f) {
x /= r;
y /= r;
}
}
} // namespace Core::HID

View File

@@ -0,0 +1,119 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
namespace Common::Input {
struct CallbackStatus;
enum class BatteryLevel : u32;
using BatteryStatus = BatteryLevel;
struct AnalogStatus;
struct ButtonStatus;
struct MotionStatus;
struct StickStatus;
struct TouchStatus;
struct TriggerStatus;
}; // namespace Common::Input
namespace Core::HID {
/**
* Converts raw input data into a valid battery status.
*
* @param callback Supported callbacks: Analog, Battery, Trigger.
* @return A valid BatteryStatus object.
*/
Common::Input::BatteryStatus TransformToBattery(const Common::Input::CallbackStatus& callback);
/**
* Converts raw input data into a valid button status. Applies invert properties to the output.
*
* @param callback Supported callbacks: Analog, Button, Trigger.
* @return A valid TouchStatus object.
*/
Common::Input::ButtonStatus TransformToButton(const Common::Input::CallbackStatus& callback);
/**
* Converts raw input data into a valid motion status.
*
* @param callback Supported callbacks: Motion.
* @return A valid TouchStatus object.
*/
Common::Input::MotionStatus TransformToMotion(const Common::Input::CallbackStatus& callback);
/**
* Converts raw input data into a valid stick status. Applies offset, deadzone, range and invert
* properties to the output.
*
* @param callback Supported callbacks: Stick.
* @return A valid StickStatus object.
*/
Common::Input::StickStatus TransformToStick(const Common::Input::CallbackStatus& callback);
/**
* Converts raw input data into a valid touch status.
*
* @param callback Supported callbacks: Touch.
* @return A valid TouchStatus object.
*/
Common::Input::TouchStatus TransformToTouch(const Common::Input::CallbackStatus& callback);
/**
* Converts raw input data into a valid trigger status. Applies offset, deadzone, range and
* invert properties to the output. Button status uses the threshold property if necessary.
*
* @param callback Supported callbacks: Analog, Button, Trigger.
* @return A valid TriggerStatus object.
*/
Common::Input::TriggerStatus TransformToTrigger(const Common::Input::CallbackStatus& callback);
/**
* Converts raw input data into a valid analog status. Applies offset, deadzone, range and
* invert properties to the output.
*
* @param callback Supported callbacks: Analog.
* @return A valid AnalogStatus object.
*/
Common::Input::AnalogStatus TransformToAnalog(const Common::Input::CallbackStatus& callback);
/**
* Converts raw input data into a valid camera status.
*
* @param callback Supported callbacks: Camera.
* @return A valid CameraObject object.
*/
Common::Input::CameraStatus TransformToCamera(const Common::Input::CallbackStatus& callback);
/**
* Converts raw input data into a valid nfc status.
*
* @param callback Supported callbacks: Nfc.
* @return A valid data tag vector.
*/
Common::Input::NfcStatus TransformToNfc(const Common::Input::CallbackStatus& callback);
/**
* Converts raw input data into a valid color status.
*
* @param callback Supported callbacks: Color.
* @return A valid Color object.
*/
Common::Input::BodyColorStatus TransformToColor(const Common::Input::CallbackStatus& callback);
/**
* Converts raw analog data into a valid analog value
* @param analog An analog object containing raw data and properties
* @param clamp_value determines if the value needs to be clamped between -1.0f and 1.0f.
*/
void SanitizeAnalog(Common::Input::AnalogStatus& analog, bool clamp_value);
/**
* Converts raw stick data into a valid stick value
* @param analog_x raw analog data and properties for the x-axis
* @param analog_y raw analog data and properties for the y-axis
* @param clamp_value bool that determines if the value needs to be clamped into the unit circle.
*/
void SanitizeStick(Common::Input::AnalogStatus& analog_x, Common::Input::AnalogStatus& analog_y,
bool clamp_value);
} // namespace Core::HID

View File

@@ -0,0 +1,64 @@
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "core/core.h"
#include "core/hle/service/hid/hid_server.h"
#include "core/hle/service/sm/sm.h"
#include "hid_core/frontend/input_interpreter.h"
#include "hid_core/hid_types.h"
#include "hid_core/resource_manager.h"
#include "hid_core/resources/npad/npad.h"
InputInterpreter::InputInterpreter(Core::System& system)
: npad{system.ServiceManager()
.GetService<Service::HID::IHidServer>("hid")
->GetResourceManager()
->GetNpad()} {
ResetButtonStates();
}
InputInterpreter::~InputInterpreter() = default;
void InputInterpreter::PollInput() {
if (npad == nullptr) {
return;
}
const auto button_state = npad->GetAndResetPressState();
previous_index = current_index;
current_index = (current_index + 1) % button_states.size();
button_states[current_index] = button_state;
}
void InputInterpreter::ResetButtonStates() {
previous_index = 0;
current_index = 0;
button_states[0] = Core::HID::NpadButton::All;
for (std::size_t i = 1; i < button_states.size(); ++i) {
button_states[i] = Core::HID::NpadButton::None;
}
}
bool InputInterpreter::IsButtonPressed(Core::HID::NpadButton button) const {
return True(button_states[current_index] & button);
}
bool InputInterpreter::IsButtonPressedOnce(Core::HID::NpadButton button) const {
const bool current_press = True(button_states[current_index] & button);
const bool previous_press = True(button_states[previous_index] & button);
return current_press && !previous_press;
}
bool InputInterpreter::IsButtonHeld(Core::HID::NpadButton button) const {
Core::HID::NpadButton held_buttons{button_states[0]};
for (std::size_t i = 1; i < button_states.size(); ++i) {
held_buttons &= button_states[i];
}
return True(held_buttons & button);
}

View File

@@ -0,0 +1,111 @@
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <array>
#include "common/common_types.h"
namespace Core {
class System;
}
namespace Core::HID {
enum class NpadButton : u64;
}
namespace Service::HID {
class NPad;
}
/**
* The InputInterpreter class interfaces with HID to retrieve button press states.
* Input is intended to be polled every 50ms so that a button is considered to be
* held down after 400ms has elapsed since the initial button press and subsequent
* repeated presses occur every 50ms.
*/
class InputInterpreter {
public:
explicit InputInterpreter(Core::System& system);
virtual ~InputInterpreter();
/// Gets a button state from HID and inserts it into the array of button states.
void PollInput();
/// Resets all the button states to their defaults.
void ResetButtonStates();
/**
* Checks whether the button is pressed.
*
* @param button The button to check.
*
* @returns True when the button is pressed.
*/
[[nodiscard]] bool IsButtonPressed(Core::HID::NpadButton button) const;
/**
* Checks whether any of the buttons in the parameter list is pressed.
*
* @tparam HIDButton The buttons to check.
*
* @returns True when at least one of the buttons is pressed.
*/
template <Core::HID::NpadButton... T>
[[nodiscard]] bool IsAnyButtonPressed() {
return (IsButtonPressed(T) || ...);
}
/**
* The specified button is considered to be pressed once
* if it is currently pressed and not pressed previously.
*
* @param button The button to check.
*
* @returns True when the button is pressed once.
*/
[[nodiscard]] bool IsButtonPressedOnce(Core::HID::NpadButton button) const;
/**
* Checks whether any of the buttons in the parameter list is pressed once.
*
* @tparam T The buttons to check.
*
* @returns True when at least one of the buttons is pressed once.
*/
template <Core::HID::NpadButton... T>
[[nodiscard]] bool IsAnyButtonPressedOnce() const {
return (IsButtonPressedOnce(T) || ...);
}
/**
* The specified button is considered to be held down if it is pressed in all 9 button states.
*
* @param button The button to check.
*
* @returns True when the button is held down.
*/
[[nodiscard]] bool IsButtonHeld(Core::HID::NpadButton button) const;
/**
* Checks whether any of the buttons in the parameter list is held down.
*
* @tparam T The buttons to check.
*
* @returns True when at least one of the buttons is held down.
*/
template <Core::HID::NpadButton... T>
[[nodiscard]] bool IsAnyButtonHeld() const {
return (IsButtonHeld(T) || ...);
}
private:
std::shared_ptr<Service::HID::NPad> npad;
/// Stores 9 consecutive button states polled from HID.
std::array<Core::HID::NpadButton, 9> button_states{};
std::size_t previous_index{};
std::size_t current_index{};
};

View File

@@ -0,0 +1,357 @@
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include <cmath>
#include "common/math_util.h"
#include "hid_core/frontend/motion_input.h"
namespace Core::HID {
MotionInput::MotionInput() {
// Initialize PID constants with default values
SetPID(0.3f, 0.005f, 0.0f);
SetGyroThreshold(ThresholdStandard);
ResetQuaternion();
ResetRotations();
}
void MotionInput::SetPID(f32 new_kp, f32 new_ki, f32 new_kd) {
kp = new_kp;
ki = new_ki;
kd = new_kd;
}
void MotionInput::SetAcceleration(const Common::Vec3f& acceleration) {
accel = acceleration;
accel.x = std::clamp(accel.x, -AccelMaxValue, AccelMaxValue);
accel.y = std::clamp(accel.y, -AccelMaxValue, AccelMaxValue);
accel.z = std::clamp(accel.z, -AccelMaxValue, AccelMaxValue);
}
void MotionInput::SetGyroscope(const Common::Vec3f& gyroscope) {
gyro = gyroscope - gyro_bias;
gyro.x = std::clamp(gyro.x, -GyroMaxValue, GyroMaxValue);
gyro.y = std::clamp(gyro.y, -GyroMaxValue, GyroMaxValue);
gyro.z = std::clamp(gyro.z, -GyroMaxValue, GyroMaxValue);
// Auto adjust gyro_bias to minimize drift
if (!IsMoving(IsAtRestRelaxed)) {
gyro_bias = (gyro_bias * 0.9999f) + (gyroscope * 0.0001f);
}
// Adjust drift when calibration mode is enabled
if (calibration_mode) {
gyro_bias = (gyro_bias * 0.99f) + (gyroscope * 0.01f);
StopCalibration();
}
if (gyro.Length() < gyro_threshold * user_gyro_threshold) {
gyro = {};
} else {
only_accelerometer = false;
}
}
void MotionInput::SetQuaternion(const Common::Quaternion<f32>& quaternion) {
quat = quaternion;
}
void MotionInput::SetEulerAngles(const Common::Vec3f& euler_angles) {
const float cr = std::cos(euler_angles.x * 0.5f);
const float sr = std::sin(euler_angles.x * 0.5f);
const float cp = std::cos(euler_angles.y * 0.5f);
const float sp = std::sin(euler_angles.y * 0.5f);
const float cy = std::cos(euler_angles.z * 0.5f);
const float sy = std::sin(euler_angles.z * 0.5f);
quat.w = cr * cp * cy + sr * sp * sy;
quat.xyz.x = sr * cp * cy - cr * sp * sy;
quat.xyz.y = cr * sp * cy + sr * cp * sy;
quat.xyz.z = cr * cp * sy - sr * sp * cy;
}
void MotionInput::SetGyroBias(const Common::Vec3f& bias) {
gyro_bias = bias;
}
void MotionInput::SetGyroThreshold(f32 threshold) {
gyro_threshold = threshold;
}
void MotionInput::SetUserGyroThreshold(f32 threshold) {
user_gyro_threshold = threshold / ThresholdStandard;
}
void MotionInput::EnableReset(bool reset) {
reset_enabled = reset;
}
void MotionInput::ResetRotations() {
rotations = {};
}
void MotionInput::ResetQuaternion() {
quat = {{0.0f, 0.0f, -1.0f}, 0.0f};
}
bool MotionInput::IsMoving(f32 sensitivity) const {
return gyro.Length() >= sensitivity || accel.Length() <= 0.9f || accel.Length() >= 1.1f;
}
bool MotionInput::IsCalibrated(f32 sensitivity) const {
return real_error.Length() < sensitivity;
}
void MotionInput::UpdateRotation(u64 elapsed_time) {
const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
if (sample_period > 0.1f) {
return;
}
rotations += gyro * sample_period;
}
void MotionInput::Calibrate() {
calibration_mode = true;
calibration_counter = 0;
}
void MotionInput::StopCalibration() {
if (calibration_counter++ > CalibrationSamples) {
calibration_mode = false;
ResetQuaternion();
ResetRotations();
}
}
// Based on Madgwick's implementation of Mayhony's AHRS algorithm.
// https://github.com/xioTechnologies/Open-Source-AHRS-With-x-IMU/blob/master/x-IMU%20IMU%20and%20AHRS%20Algorithms/x-IMU%20IMU%20and%20AHRS%20Algorithms/AHRS/MahonyAHRS.cs
void MotionInput::UpdateOrientation(u64 elapsed_time) {
if (!IsCalibrated(0.1f)) {
ResetOrientation();
}
// Short name local variable for readability
f32 q1 = quat.w;
f32 q2 = quat.xyz[0];
f32 q3 = quat.xyz[1];
f32 q4 = quat.xyz[2];
const auto sample_period = static_cast<f32>(elapsed_time) / 1000000.0f;
// Ignore invalid elapsed time
if (sample_period > 0.1f) {
return;
}
const auto normal_accel = accel.Normalized();
auto rad_gyro = gyro * Common::PI * 2;
const f32 swap = rad_gyro.x;
rad_gyro.x = rad_gyro.y;
rad_gyro.y = -swap;
rad_gyro.z = -rad_gyro.z;
// Clear gyro values if there is no gyro present
if (only_accelerometer) {
rad_gyro.x = 0;
rad_gyro.y = 0;
rad_gyro.z = 0;
}
// Ignore drift correction if acceleration is not reliable
if (accel.Length() >= 0.75f && accel.Length() <= 1.25f) {
const f32 ax = -normal_accel.x;
const f32 ay = normal_accel.y;
const f32 az = -normal_accel.z;
// Estimated direction of gravity
const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
// Error is cross product between estimated direction and measured direction of gravity
const Common::Vec3f new_real_error = {
az * vx - ax * vz,
ay * vz - az * vy,
ax * vy - ay * vx,
};
derivative_error = new_real_error - real_error;
real_error = new_real_error;
// Prevent integral windup
if (ki != 0.0f && !IsCalibrated(0.05f)) {
integral_error += real_error;
} else {
integral_error = {};
}
// Apply feedback terms
if (!only_accelerometer) {
rad_gyro += kp * real_error;
rad_gyro += ki * integral_error;
rad_gyro += kd * derivative_error;
} else {
// Give more weight to accelerometer values to compensate for the lack of gyro
rad_gyro += 35.0f * kp * real_error;
rad_gyro += 10.0f * ki * integral_error;
rad_gyro += 10.0f * kd * derivative_error;
// Emulate gyro values for games that need them
gyro.x = -rad_gyro.y;
gyro.y = rad_gyro.x;
gyro.z = -rad_gyro.z;
UpdateRotation(elapsed_time);
}
}
const f32 gx = rad_gyro.y;
const f32 gy = rad_gyro.x;
const f32 gz = rad_gyro.z;
// Integrate rate of change of quaternion
const f32 pa = q2;
const f32 pb = q3;
const f32 pc = q4;
q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
quat.w = q1;
quat.xyz[0] = q2;
quat.xyz[1] = q3;
quat.xyz[2] = q4;
quat = quat.Normalized();
}
std::array<Common::Vec3f, 3> MotionInput::GetOrientation() const {
const Common::Quaternion<float> quad{
.xyz = {-quat.xyz[1], -quat.xyz[0], -quat.w},
.w = -quat.xyz[2],
};
const std::array<float, 16> matrix4x4 = quad.ToMatrix();
return {Common::Vec3f(matrix4x4[0], matrix4x4[1], -matrix4x4[2]),
Common::Vec3f(matrix4x4[4], matrix4x4[5], -matrix4x4[6]),
Common::Vec3f(-matrix4x4[8], -matrix4x4[9], matrix4x4[10])};
}
Common::Vec3f MotionInput::GetAcceleration() const {
return accel;
}
Common::Vec3f MotionInput::GetGyroscope() const {
return gyro;
}
Common::Vec3f MotionInput::GetGyroBias() const {
return gyro_bias;
}
Common::Quaternion<f32> MotionInput::GetQuaternion() const {
return quat;
}
Common::Vec3f MotionInput::GetRotations() const {
return rotations;
}
Common::Vec3f MotionInput::GetEulerAngles() const {
// roll (x-axis rotation)
const float sinr_cosp = 2 * (quat.w * quat.xyz.x + quat.xyz.y * quat.xyz.z);
const float cosr_cosp = 1 - 2 * (quat.xyz.x * quat.xyz.x + quat.xyz.y * quat.xyz.y);
// pitch (y-axis rotation)
const float sinp = std::sqrt(1 + 2 * (quat.w * quat.xyz.y - quat.xyz.x * quat.xyz.z));
const float cosp = std::sqrt(1 - 2 * (quat.w * quat.xyz.y - quat.xyz.x * quat.xyz.z));
// yaw (z-axis rotation)
const float siny_cosp = 2 * (quat.w * quat.xyz.z + quat.xyz.x * quat.xyz.y);
const float cosy_cosp = 1 - 2 * (quat.xyz.y * quat.xyz.y + quat.xyz.z * quat.xyz.z);
return {
std::atan2(sinr_cosp, cosr_cosp),
2 * std::atan2(sinp, cosp) - Common::PI / 2,
std::atan2(siny_cosp, cosy_cosp),
};
}
void MotionInput::ResetOrientation() {
if (!reset_enabled || only_accelerometer) {
return;
}
if (!IsMoving(IsAtRestRelaxed) && accel.z <= -0.9f) {
++reset_counter;
if (reset_counter > 900) {
quat.w = 0;
quat.xyz[0] = 0;
quat.xyz[1] = 0;
quat.xyz[2] = -1;
SetOrientationFromAccelerometer();
integral_error = {};
reset_counter = 0;
}
} else {
reset_counter = 0;
}
}
void MotionInput::SetOrientationFromAccelerometer() {
int iterations = 0;
const f32 sample_period = 0.015f;
const auto normal_accel = accel.Normalized();
while (!IsCalibrated(0.01f) && ++iterations < 100) {
// Short name local variable for readability
f32 q1 = quat.w;
f32 q2 = quat.xyz[0];
f32 q3 = quat.xyz[1];
f32 q4 = quat.xyz[2];
Common::Vec3f rad_gyro;
const f32 ax = -normal_accel.x;
const f32 ay = normal_accel.y;
const f32 az = -normal_accel.z;
// Estimated direction of gravity
const f32 vx = 2.0f * (q2 * q4 - q1 * q3);
const f32 vy = 2.0f * (q1 * q2 + q3 * q4);
const f32 vz = q1 * q1 - q2 * q2 - q3 * q3 + q4 * q4;
// Error is cross product between estimated direction and measured direction of gravity
const Common::Vec3f new_real_error = {
az * vx - ax * vz,
ay * vz - az * vy,
ax * vy - ay * vx,
};
derivative_error = new_real_error - real_error;
real_error = new_real_error;
rad_gyro += 10.0f * kp * real_error;
rad_gyro += 5.0f * ki * integral_error;
rad_gyro += 10.0f * kd * derivative_error;
const f32 gx = rad_gyro.y;
const f32 gy = rad_gyro.x;
const f32 gz = rad_gyro.z;
// Integrate rate of change of quaternion
const f32 pa = q2;
const f32 pb = q3;
const f32 pc = q4;
q1 = q1 + (-q2 * gx - q3 * gy - q4 * gz) * (0.5f * sample_period);
q2 = pa + (q1 * gx + pb * gz - pc * gy) * (0.5f * sample_period);
q3 = pb + (q1 * gy - pa * gz + pc * gx) * (0.5f * sample_period);
q4 = pc + (q1 * gz + pa * gy - pb * gx) * (0.5f * sample_period);
quat.w = q1;
quat.xyz[0] = q2;
quat.xyz[1] = q3;
quat.xyz[2] = q4;
quat = quat.Normalized();
}
}
} // namespace Core::HID

View File

@@ -0,0 +1,119 @@
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "common/common_types.h"
#include "common/quaternion.h"
#include "common/vector_math.h"
namespace Core::HID {
class MotionInput {
public:
static constexpr float ThresholdLoose = 0.01f;
static constexpr float ThresholdStandard = 0.007f;
static constexpr float ThresholdTight = 0.002f;
static constexpr float IsAtRestRelaxed = 0.05f;
static constexpr float IsAtRestLoose = 0.02f;
static constexpr float IsAtRestStandard = 0.01f;
static constexpr float IsAtRestTight = 0.005f;
static constexpr float GyroMaxValue = 5.0f;
static constexpr float AccelMaxValue = 7.0f;
static constexpr std::size_t CalibrationSamples = 300;
explicit MotionInput();
MotionInput(const MotionInput&) = default;
MotionInput& operator=(const MotionInput&) = default;
MotionInput(MotionInput&&) = default;
MotionInput& operator=(MotionInput&&) = default;
void SetPID(f32 new_kp, f32 new_ki, f32 new_kd);
void SetAcceleration(const Common::Vec3f& acceleration);
void SetGyroscope(const Common::Vec3f& gyroscope);
void SetQuaternion(const Common::Quaternion<f32>& quaternion);
void SetEulerAngles(const Common::Vec3f& euler_angles);
void SetGyroBias(const Common::Vec3f& bias);
void SetGyroThreshold(f32 threshold);
/// Applies a modifier on top of the normal gyro threshold
void SetUserGyroThreshold(f32 threshold);
void EnableReset(bool reset);
void ResetRotations();
void ResetQuaternion();
void UpdateRotation(u64 elapsed_time);
void UpdateOrientation(u64 elapsed_time);
void Calibrate();
[[nodiscard]] std::array<Common::Vec3f, 3> GetOrientation() const;
[[nodiscard]] Common::Vec3f GetAcceleration() const;
[[nodiscard]] Common::Vec3f GetGyroscope() const;
[[nodiscard]] Common::Vec3f GetGyroBias() const;
[[nodiscard]] Common::Vec3f GetRotations() const;
[[nodiscard]] Common::Quaternion<f32> GetQuaternion() const;
[[nodiscard]] Common::Vec3f GetEulerAngles() const;
[[nodiscard]] bool IsMoving(f32 sensitivity) const;
[[nodiscard]] bool IsCalibrated(f32 sensitivity) const;
private:
void StopCalibration();
void ResetOrientation();
void SetOrientationFromAccelerometer();
// PID constants
f32 kp;
f32 ki;
f32 kd;
// PID errors
Common::Vec3f real_error;
Common::Vec3f integral_error;
Common::Vec3f derivative_error;
// Quaternion containing the device orientation
Common::Quaternion<f32> quat;
// Number of full rotations in each axis
Common::Vec3f rotations;
// Acceleration vector measurement in G force
Common::Vec3f accel;
// Gyroscope vector measurement in radians/s.
Common::Vec3f gyro;
// Vector to be subtracted from gyro measurements
Common::Vec3f gyro_bias;
// Minimum gyro amplitude to detect if the device is moving
f32 gyro_threshold = 0.0f;
// Multiplies gyro_threshold by this value
f32 user_gyro_threshold = 0.0f;
// Number of invalid sequential data
u32 reset_counter = 0;
// If the provided data is invalid the device will be autocalibrated
bool reset_enabled = true;
// Use accelerometer values to calculate position
bool only_accelerometer = true;
// When enabled it will aggressively adjust for gyro drift
bool calibration_mode = false;
// Used to auto disable calibration mode
std::size_t calibration_counter = 0;
};
} // namespace Core::HID

222
src/hid_core/hid_core.cpp Executable file
View File

@@ -0,0 +1,222 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "common/assert.h"
#include "hid_core/frontend/emulated_console.h"
#include "hid_core/frontend/emulated_controller.h"
#include "hid_core/frontend/emulated_devices.h"
#include "hid_core/hid_core.h"
#include "hid_core/hid_util.h"
namespace Core::HID {
HIDCore::HIDCore()
: player_1{std::make_unique<EmulatedController>(NpadIdType::Player1)},
player_2{std::make_unique<EmulatedController>(NpadIdType::Player2)},
player_3{std::make_unique<EmulatedController>(NpadIdType::Player3)},
player_4{std::make_unique<EmulatedController>(NpadIdType::Player4)},
player_5{std::make_unique<EmulatedController>(NpadIdType::Player5)},
player_6{std::make_unique<EmulatedController>(NpadIdType::Player6)},
player_7{std::make_unique<EmulatedController>(NpadIdType::Player7)},
player_8{std::make_unique<EmulatedController>(NpadIdType::Player8)},
other{std::make_unique<EmulatedController>(NpadIdType::Other)},
handheld{std::make_unique<EmulatedController>(NpadIdType::Handheld)},
console{std::make_unique<EmulatedConsole>()}, devices{std::make_unique<EmulatedDevices>()} {}
HIDCore::~HIDCore() = default;
EmulatedController* HIDCore::GetEmulatedController(NpadIdType npad_id_type) {
switch (npad_id_type) {
case NpadIdType::Player1:
return player_1.get();
case NpadIdType::Player2:
return player_2.get();
case NpadIdType::Player3:
return player_3.get();
case NpadIdType::Player4:
return player_4.get();
case NpadIdType::Player5:
return player_5.get();
case NpadIdType::Player6:
return player_6.get();
case NpadIdType::Player7:
return player_7.get();
case NpadIdType::Player8:
return player_8.get();
case NpadIdType::Other:
return other.get();
case NpadIdType::Handheld:
return handheld.get();
case NpadIdType::Invalid:
default:
ASSERT_MSG(false, "Invalid NpadIdType={}", npad_id_type);
return nullptr;
}
}
const EmulatedController* HIDCore::GetEmulatedController(NpadIdType npad_id_type) const {
switch (npad_id_type) {
case NpadIdType::Player1:
return player_1.get();
case NpadIdType::Player2:
return player_2.get();
case NpadIdType::Player3:
return player_3.get();
case NpadIdType::Player4:
return player_4.get();
case NpadIdType::Player5:
return player_5.get();
case NpadIdType::Player6:
return player_6.get();
case NpadIdType::Player7:
return player_7.get();
case NpadIdType::Player8:
return player_8.get();
case NpadIdType::Other:
return other.get();
case NpadIdType::Handheld:
return handheld.get();
case NpadIdType::Invalid:
default:
ASSERT_MSG(false, "Invalid NpadIdType={}", npad_id_type);
return nullptr;
}
}
EmulatedConsole* HIDCore::GetEmulatedConsole() {
return console.get();
}
const EmulatedConsole* HIDCore::GetEmulatedConsole() const {
return console.get();
}
EmulatedDevices* HIDCore::GetEmulatedDevices() {
return devices.get();
}
const EmulatedDevices* HIDCore::GetEmulatedDevices() const {
return devices.get();
}
EmulatedController* HIDCore::GetEmulatedControllerByIndex(std::size_t index) {
return GetEmulatedController(Service::HID::IndexToNpadIdType(index));
}
const EmulatedController* HIDCore::GetEmulatedControllerByIndex(std::size_t index) const {
return GetEmulatedController(Service::HID::IndexToNpadIdType(index));
}
void HIDCore::SetSupportedStyleTag(NpadStyleTag style_tag) {
supported_style_tag.raw = style_tag.raw;
player_1->SetSupportedNpadStyleTag(supported_style_tag);
player_2->SetSupportedNpadStyleTag(supported_style_tag);
player_3->SetSupportedNpadStyleTag(supported_style_tag);
player_4->SetSupportedNpadStyleTag(supported_style_tag);
player_5->SetSupportedNpadStyleTag(supported_style_tag);
player_6->SetSupportedNpadStyleTag(supported_style_tag);
player_7->SetSupportedNpadStyleTag(supported_style_tag);
player_8->SetSupportedNpadStyleTag(supported_style_tag);
other->SetSupportedNpadStyleTag(supported_style_tag);
handheld->SetSupportedNpadStyleTag(supported_style_tag);
}
NpadStyleTag HIDCore::GetSupportedStyleTag() const {
return supported_style_tag;
}
s8 HIDCore::GetPlayerCount() const {
s8 active_players = 0;
for (std::size_t player_index = 0; player_index < available_controllers - 2; ++player_index) {
const auto* const controller = GetEmulatedControllerByIndex(player_index);
if (controller->IsConnected()) {
active_players++;
}
}
return active_players;
}
NpadIdType HIDCore::GetFirstNpadId() const {
for (std::size_t player_index = 0; player_index < available_controllers; ++player_index) {
const auto* const controller = GetEmulatedControllerByIndex(player_index);
if (controller->IsConnected()) {
return controller->GetNpadIdType();
}
}
return NpadIdType::Player1;
}
NpadIdType HIDCore::GetFirstDisconnectedNpadId() const {
for (std::size_t player_index = 0; player_index < available_controllers; ++player_index) {
const auto* const controller = GetEmulatedControllerByIndex(player_index);
if (!controller->IsConnected()) {
return controller->GetNpadIdType();
}
}
return NpadIdType::Player1;
}
void HIDCore::SetLastActiveController(NpadIdType npad_id) {
last_active_controller = npad_id;
}
NpadIdType HIDCore::GetLastActiveController() const {
return last_active_controller;
}
void HIDCore::EnableAllControllerConfiguration() {
player_1->EnableConfiguration();
player_2->EnableConfiguration();
player_3->EnableConfiguration();
player_4->EnableConfiguration();
player_5->EnableConfiguration();
player_6->EnableConfiguration();
player_7->EnableConfiguration();
player_8->EnableConfiguration();
other->EnableConfiguration();
handheld->EnableConfiguration();
}
void HIDCore::DisableAllControllerConfiguration() {
player_1->DisableConfiguration();
player_2->DisableConfiguration();
player_3->DisableConfiguration();
player_4->DisableConfiguration();
player_5->DisableConfiguration();
player_6->DisableConfiguration();
player_7->DisableConfiguration();
player_8->DisableConfiguration();
other->DisableConfiguration();
handheld->DisableConfiguration();
}
void HIDCore::ReloadInputDevices() {
player_1->ReloadFromSettings();
player_2->ReloadFromSettings();
player_3->ReloadFromSettings();
player_4->ReloadFromSettings();
player_5->ReloadFromSettings();
player_6->ReloadFromSettings();
player_7->ReloadFromSettings();
player_8->ReloadFromSettings();
other->ReloadFromSettings();
handheld->ReloadFromSettings();
console->ReloadFromSettings();
devices->ReloadFromSettings();
}
void HIDCore::UnloadInputDevices() {
player_1->UnloadInput();
player_2->UnloadInput();
player_3->UnloadInput();
player_4->UnloadInput();
player_5->UnloadInput();
player_6->UnloadInput();
player_7->UnloadInput();
player_8->UnloadInput();
other->UnloadInput();
handheld->UnloadInput();
console->UnloadInput();
devices->UnloadInput();
}
} // namespace Core::HID

89
src/hid_core/hid_core.h Executable file
View File

@@ -0,0 +1,89 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <memory>
#include "common/common_funcs.h"
#include "hid_core/hid_types.h"
namespace Core::HID {
class EmulatedConsole;
class EmulatedController;
class EmulatedDevices;
} // namespace Core::HID
namespace Core::HID {
class HIDCore {
public:
explicit HIDCore();
~HIDCore();
YUZU_NON_COPYABLE(HIDCore);
YUZU_NON_MOVEABLE(HIDCore);
EmulatedController* GetEmulatedController(NpadIdType npad_id_type);
const EmulatedController* GetEmulatedController(NpadIdType npad_id_type) const;
EmulatedController* GetEmulatedControllerByIndex(std::size_t index);
const EmulatedController* GetEmulatedControllerByIndex(std::size_t index) const;
EmulatedConsole* GetEmulatedConsole();
const EmulatedConsole* GetEmulatedConsole() const;
EmulatedDevices* GetEmulatedDevices();
const EmulatedDevices* GetEmulatedDevices() const;
void SetSupportedStyleTag(NpadStyleTag style_tag);
NpadStyleTag GetSupportedStyleTag() const;
/// Counts the connected players from P1-P8
s8 GetPlayerCount() const;
/// Returns the first connected npad id
NpadIdType GetFirstNpadId() const;
/// Returns the first disconnected npad id
NpadIdType GetFirstDisconnectedNpadId() const;
/// Sets the npad id of the last active controller
void SetLastActiveController(NpadIdType npad_id);
/// Returns the npad id of the last controller that pushed a button
NpadIdType GetLastActiveController() const;
/// Sets all emulated controllers into configuring mode.
void EnableAllControllerConfiguration();
/// Sets all emulated controllers into normal mode.
void DisableAllControllerConfiguration();
/// Reloads all input devices from settings
void ReloadInputDevices();
/// Removes all callbacks from input common
void UnloadInputDevices();
/// Number of emulated controllers
static constexpr std::size_t available_controllers{10};
private:
std::unique_ptr<EmulatedController> player_1;
std::unique_ptr<EmulatedController> player_2;
std::unique_ptr<EmulatedController> player_3;
std::unique_ptr<EmulatedController> player_4;
std::unique_ptr<EmulatedController> player_5;
std::unique_ptr<EmulatedController> player_6;
std::unique_ptr<EmulatedController> player_7;
std::unique_ptr<EmulatedController> player_8;
std::unique_ptr<EmulatedController> other;
std::unique_ptr<EmulatedController> handheld;
std::unique_ptr<EmulatedConsole> console;
std::unique_ptr<EmulatedDevices> devices;
NpadStyleTag supported_style_tag{NpadStyleSet::All};
NpadIdType last_active_controller{NpadIdType::Handheld};
};
} // namespace Core::HID

67
src/hid_core/hid_result.h Executable file
View File

@@ -0,0 +1,67 @@
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "core/hle/result.h"
namespace Service::HID {
constexpr Result PalmaResultSuccess{ErrorModule::HID, 0};
constexpr Result ResultTouchNotInitialized{ErrorModule::HID, 41};
constexpr Result ResultTouchOverflow{ErrorModule::HID, 42};
constexpr Result NpadInvalidHandle{ErrorModule::HID, 100};
constexpr Result NpadDeviceIndexOutOfRange{ErrorModule::HID, 107};
constexpr Result ResultVibrationNotInitialized{ErrorModule::HID, 121};
constexpr Result ResultVibrationInvalidStyleIndex{ErrorModule::HID, 122};
constexpr Result ResultVibrationInvalidNpadId{ErrorModule::HID, 123};
constexpr Result ResultVibrationDeviceIndexOutOfRange{ErrorModule::HID, 124};
constexpr Result ResultVibrationStrengthOutOfRange{ErrorModule::HID, 126};
constexpr Result ResultVibrationArraySizeMismatch{ErrorModule::HID, 131};
constexpr Result InvalidSixAxisFusionRange{ErrorModule::HID, 423};
constexpr Result ResultNfcIsNotReady{ErrorModule::HID, 461};
constexpr Result ResultNfcXcdHandleIsNotInitialized{ErrorModule::HID, 464};
constexpr Result ResultIrSensorIsNotReady{ErrorModule::HID, 501};
constexpr Result ResultGestureOverflow{ErrorModule::HID, 522};
constexpr Result ResultGestureNotInitialized{ErrorModule::HID, 523};
constexpr Result ResultMcuIsNotReady{ErrorModule::HID, 541};
constexpr Result NpadIsDualJoycon{ErrorModule::HID, 601};
constexpr Result NpadIsSameType{ErrorModule::HID, 602};
constexpr Result ResultNpadIsNotProController{ErrorModule::HID, 604};
constexpr Result ResultInvalidNpadId{ErrorModule::HID, 709};
constexpr Result ResultNpadNotConnected{ErrorModule::HID, 710};
constexpr Result ResultNpadHandlerOverflow{ErrorModule::HID, 711};
constexpr Result ResultNpadHandlerNotInitialized{ErrorModule::HID, 712};
constexpr Result ResultInvalidArraySize{ErrorModule::HID, 715};
constexpr Result ResultUndefinedStyleset{ErrorModule::HID, 716};
constexpr Result ResultMultipleStyleSetSelected{ErrorModule::HID, 717};
constexpr Result ResultAppletResourceOverflow{ErrorModule::HID, 1041};
constexpr Result ResultAppletResourceNotInitialized{ErrorModule::HID, 1042};
constexpr Result ResultSharedMemoryNotInitialized{ErrorModule::HID, 1043};
constexpr Result ResultAruidNoAvailableEntries{ErrorModule::HID, 1044};
constexpr Result ResultAruidAlreadyRegistered{ErrorModule::HID, 1046};
constexpr Result ResultAruidNotRegistered{ErrorModule::HID, 1047};
constexpr Result ResultNpadResourceOverflow{ErrorModule::HID, 2001};
constexpr Result ResultNpadResourceNotInitialized{ErrorModule::HID, 2002};
constexpr Result InvalidPalmaHandle{ErrorModule::HID, 3302};
} // namespace Service::HID
namespace Service::IRS {
constexpr Result InvalidProcessorState{ErrorModule::Irsensor, 78};
constexpr Result InvalidIrCameraHandle{ErrorModule::Irsensor, 204};
} // namespace Service::IRS

747
src/hid_core/hid_types.h Executable file
View File

@@ -0,0 +1,747 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "common/bit_field.h"
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "common/point.h"
#include "common/uuid.h"
#include "common/vector_math.h"
namespace Core::HID {
enum class DeviceIndex : u8 {
Left = 0,
Right = 1,
None = 2,
MaxDeviceIndex = 3,
};
// This is nn::hid::NpadButton
enum class NpadButton : u64 {
None = 0,
A = 1U << 0,
B = 1U << 1,
X = 1U << 2,
Y = 1U << 3,
StickL = 1U << 4,
StickR = 1U << 5,
L = 1U << 6,
R = 1U << 7,
ZL = 1U << 8,
ZR = 1U << 9,
Plus = 1U << 10,
Minus = 1U << 11,
Left = 1U << 12,
Up = 1U << 13,
Right = 1U << 14,
Down = 1U << 15,
StickLLeft = 1U << 16,
StickLUp = 1U << 17,
StickLRight = 1U << 18,
StickLDown = 1U << 19,
StickRLeft = 1U << 20,
StickRUp = 1U << 21,
StickRRight = 1U << 22,
StickRDown = 1U << 23,
LeftSL = 1U << 24,
LeftSR = 1U << 25,
RightSL = 1U << 26,
RightSR = 1U << 27,
Palma = 1U << 28,
Verification = 1U << 29,
HandheldLeftB = 1U << 30,
LagonCLeft = 1U << 31,
LagonCUp = 1ULL << 32,
LagonCRight = 1ULL << 33,
LagonCDown = 1ULL << 34,
All = 0xFFFFFFFFFFFFFFFFULL,
};
DECLARE_ENUM_FLAG_OPERATORS(NpadButton);
enum class KeyboardKeyIndex : u32 {
A = 4,
B = 5,
C = 6,
D = 7,
E = 8,
F = 9,
G = 10,
H = 11,
I = 12,
J = 13,
K = 14,
L = 15,
M = 16,
N = 17,
O = 18,
P = 19,
Q = 20,
R = 21,
S = 22,
T = 23,
U = 24,
V = 25,
W = 26,
X = 27,
Y = 28,
Z = 29,
D1 = 30,
D2 = 31,
D3 = 32,
D4 = 33,
D5 = 34,
D6 = 35,
D7 = 36,
D8 = 37,
D9 = 38,
D0 = 39,
Return = 40,
Escape = 41,
Backspace = 42,
Tab = 43,
Space = 44,
Minus = 45,
Plus = 46,
OpenBracket = 47,
CloseBracket = 48,
Pipe = 49,
Tilde = 50,
Semicolon = 51,
Quote = 52,
Backquote = 53,
Comma = 54,
Period = 55,
Slash = 56,
CapsLock = 57,
F1 = 58,
F2 = 59,
F3 = 60,
F4 = 61,
F5 = 62,
F6 = 63,
F7 = 64,
F8 = 65,
F9 = 66,
F10 = 67,
F11 = 68,
F12 = 69,
PrintScreen = 70,
ScrollLock = 71,
Pause = 72,
Insert = 73,
Home = 74,
PageUp = 75,
Delete = 76,
End = 77,
PageDown = 78,
RightArrow = 79,
LeftArrow = 80,
DownArrow = 81,
UpArrow = 82,
NumLock = 83,
NumPadDivide = 84,
NumPadMultiply = 85,
NumPadSubtract = 86,
NumPadAdd = 87,
NumPadEnter = 88,
NumPad1 = 89,
NumPad2 = 90,
NumPad3 = 91,
NumPad4 = 92,
NumPad5 = 93,
NumPad6 = 94,
NumPad7 = 95,
NumPad8 = 96,
NumPad9 = 97,
NumPad0 = 98,
NumPadDot = 99,
Backslash = 100,
Application = 101,
Power = 102,
NumPadEquals = 103,
F13 = 104,
F14 = 105,
F15 = 106,
F16 = 107,
F17 = 108,
F18 = 109,
F19 = 110,
F20 = 111,
F21 = 112,
F22 = 113,
F23 = 114,
F24 = 115,
NumPadComma = 133,
Ro = 135,
KatakanaHiragana = 136,
Yen = 137,
Henkan = 138,
Muhenkan = 139,
NumPadCommaPc98 = 140,
HangulEnglish = 144,
Hanja = 145,
Katakana = 146,
Hiragana = 147,
ZenkakuHankaku = 148,
LeftControl = 224,
LeftShift = 225,
LeftAlt = 226,
LeftGui = 227,
RightControl = 228,
RightShift = 229,
RightAlt = 230,
RightGui = 231,
};
// This is nn::hid::NpadIdType
enum class NpadIdType : u32 {
Player1 = 0x0,
Player2 = 0x1,
Player3 = 0x2,
Player4 = 0x3,
Player5 = 0x4,
Player6 = 0x5,
Player7 = 0x6,
Player8 = 0x7,
Other = 0x10,
Handheld = 0x20,
Invalid = 0xFFFFFFFF,
};
enum class NpadInterfaceType : u8 {
None = 0,
Bluetooth = 1,
Rail = 2,
Usb = 3,
Embedded = 4,
};
// This is nn::hid::NpadStyleIndex
enum class NpadStyleIndex : u8 {
None = 0,
Fullkey = 3,
Handheld = 4,
HandheldNES = 4,
JoyconDual = 5,
JoyconLeft = 6,
JoyconRight = 7,
GameCube = 8,
Pokeball = 9,
NES = 10,
SNES = 12,
N64 = 13,
SegaGenesis = 14,
SystemExt = 32,
System = 33,
MaxNpadType = 34,
};
// This is nn::hid::NpadStyleSet
enum class NpadStyleSet : u32 {
None = 0,
Fullkey = 1U << 0,
Handheld = 1U << 1,
JoyDual = 1U << 2,
JoyLeft = 1U << 3,
JoyRight = 1U << 4,
Gc = 1U << 5,
Palma = 1U << 6,
Lark = 1U << 7,
HandheldLark = 1U << 8,
Lucia = 1U << 9,
Lagoon = 1U << 10,
Lager = 1U << 11,
SystemExt = 1U << 29,
System = 1U << 30,
All = 0xFFFFFFFFU,
};
static_assert(sizeof(NpadStyleSet) == 4, "NpadStyleSet is an invalid size");
DECLARE_ENUM_FLAG_OPERATORS(NpadStyleSet)
// This is nn::hid::VibrationDevicePosition
enum class VibrationDevicePosition : u32 {
None = 0,
Left = 1,
Right = 2,
};
// This is nn::hid::VibrationDeviceType
enum class VibrationDeviceType : u32 {
Unknown = 0,
LinearResonantActuator = 1,
GcErm = 2,
N64 = 3,
};
// This is nn::hid::VibrationGcErmCommand
enum class VibrationGcErmCommand : u64 {
Stop = 0,
Start = 1,
StopHard = 2,
};
// This is nn::hid::GyroscopeZeroDriftMode
enum class GyroscopeZeroDriftMode : u32 {
Loose = 0,
Standard = 1,
Tight = 2,
};
// This is nn::hid::TouchScreenModeForNx
enum class TouchScreenModeForNx : u8 {
UseSystemSetting,
Finger,
Heat2,
};
// This is nn::hid::system::NpadBatteryLevel
enum class NpadBatteryLevel : u32 {
Empty,
Critical,
Low,
High,
Full,
};
// This is nn::hid::NpadStyleTag
struct NpadStyleTag {
union {
NpadStyleSet raw{};
BitField<0, 1, u32> fullkey;
BitField<1, 1, u32> handheld;
BitField<2, 1, u32> joycon_dual;
BitField<3, 1, u32> joycon_left;
BitField<4, 1, u32> joycon_right;
BitField<5, 1, u32> gamecube;
BitField<6, 1, u32> palma;
BitField<7, 1, u32> lark;
BitField<8, 1, u32> handheld_lark;
BitField<9, 1, u32> lucia;
BitField<10, 1, u32> lagoon;
BitField<11, 1, u32> lager;
BitField<29, 1, u32> system_ext;
BitField<30, 1, u32> system;
};
};
static_assert(sizeof(NpadStyleTag) == 4, "NpadStyleTag is an invalid size");
// This is nn::hid::TouchAttribute
struct TouchAttribute {
union {
u32 raw{};
BitField<0, 1, u32> start_touch;
BitField<1, 1, u32> end_touch;
};
};
static_assert(sizeof(TouchAttribute) == 0x4, "TouchAttribute is an invalid size");
struct TouchFinger {
u64 last_touch{};
Common::Point<float> position{};
u32 id{};
TouchAttribute attribute{};
bool pressed{};
};
// This is nn::hid::TouchScreenConfigurationForNx
struct TouchScreenConfigurationForNx {
TouchScreenModeForNx mode{TouchScreenModeForNx::UseSystemSetting};
INSERT_PADDING_BYTES(0xF);
};
static_assert(sizeof(TouchScreenConfigurationForNx) == 0x10,
"TouchScreenConfigurationForNx is an invalid size");
struct NpadColor {
u8 r{};
u8 g{};
u8 b{};
u8 a{};
};
static_assert(sizeof(NpadColor) == 4, "NpadColor is an invalid size");
// This is nn::hid::NpadControllerColor
struct NpadControllerColor {
NpadColor body{};
NpadColor button{};
};
static_assert(sizeof(NpadControllerColor) == 8, "NpadControllerColor is an invalid size");
// This is nn::hid::AnalogStickState
struct AnalogStickState {
s32 x{};
s32 y{};
};
static_assert(sizeof(AnalogStickState) == 8, "AnalogStickState is an invalid size");
// This is nn::hid::server::NpadGcTriggerState
struct NpadGcTriggerState {
s64 sampling_number{};
s32 left{};
s32 right{};
};
static_assert(sizeof(NpadGcTriggerState) == 0x10, "NpadGcTriggerState is an invalid size");
// This is nn::hid::system::NpadPowerInfo
struct NpadPowerInfo {
bool is_powered{};
bool is_charging{};
INSERT_PADDING_BYTES(0x6);
NpadBatteryLevel battery_level{NpadBatteryLevel::Full};
};
static_assert(sizeof(NpadPowerInfo) == 0xC, "NpadPowerInfo is an invalid size");
struct LedPattern {
LedPattern() {
raw = 0;
}
LedPattern(u64 light1, u64 light2, u64 light3, u64 light4) {
position1.Assign(light1);
position2.Assign(light2);
position3.Assign(light3);
position4.Assign(light4);
}
union {
u64 raw{};
BitField<0, 1, u64> position1;
BitField<1, 1, u64> position2;
BitField<2, 1, u64> position3;
BitField<3, 1, u64> position4;
};
};
struct SleepButtonState {
union {
u64 raw{};
// Buttons
BitField<0, 1, u64> sleep;
};
};
static_assert(sizeof(SleepButtonState) == 0x8, "SleepButtonState has incorrect size.");
struct HomeButtonState {
union {
u64 raw{};
// Buttons
BitField<0, 1, u64> home;
};
};
static_assert(sizeof(HomeButtonState) == 0x8, "HomeButtonState has incorrect size.");
struct CaptureButtonState {
union {
u64 raw{};
// Buttons
BitField<0, 1, u64> capture;
};
};
static_assert(sizeof(CaptureButtonState) == 0x8, "CaptureButtonState has incorrect size.");
struct NpadButtonState {
union {
NpadButton raw{};
// Buttons
BitField<0, 1, u64> a;
BitField<1, 1, u64> b;
BitField<2, 1, u64> x;
BitField<3, 1, u64> y;
BitField<4, 1, u64> stick_l;
BitField<5, 1, u64> stick_r;
BitField<6, 1, u64> l;
BitField<7, 1, u64> r;
BitField<8, 1, u64> zl;
BitField<9, 1, u64> zr;
BitField<10, 1, u64> plus;
BitField<11, 1, u64> minus;
// D-Pad
BitField<12, 1, u64> left;
BitField<13, 1, u64> up;
BitField<14, 1, u64> right;
BitField<15, 1, u64> down;
// Left JoyStick
BitField<16, 1, u64> stick_l_left;
BitField<17, 1, u64> stick_l_up;
BitField<18, 1, u64> stick_l_right;
BitField<19, 1, u64> stick_l_down;
// Right JoyStick
BitField<20, 1, u64> stick_r_left;
BitField<21, 1, u64> stick_r_up;
BitField<22, 1, u64> stick_r_right;
BitField<23, 1, u64> stick_r_down;
BitField<24, 1, u64> left_sl;
BitField<25, 1, u64> left_sr;
BitField<26, 1, u64> right_sl;
BitField<27, 1, u64> right_sr;
BitField<28, 1, u64> palma;
BitField<29, 1, u64> verification;
BitField<30, 1, u64> handheld_left_b;
BitField<31, 1, u64> lagon_c_left;
BitField<32, 1, u64> lagon_c_up;
BitField<33, 1, u64> lagon_c_right;
BitField<34, 1, u64> lagon_c_down;
};
};
static_assert(sizeof(NpadButtonState) == 0x8, "NpadButtonState has incorrect size.");
// This is nn::hid::DebugPadButton
struct DebugPadButton {
union {
u32 raw{};
BitField<0, 1, u32> a;
BitField<1, 1, u32> b;
BitField<2, 1, u32> x;
BitField<3, 1, u32> y;
BitField<4, 1, u32> l;
BitField<5, 1, u32> r;
BitField<6, 1, u32> zl;
BitField<7, 1, u32> zr;
BitField<8, 1, u32> plus;
BitField<9, 1, u32> minus;
BitField<10, 1, u32> d_left;
BitField<11, 1, u32> d_up;
BitField<12, 1, u32> d_right;
BitField<13, 1, u32> d_down;
};
};
static_assert(sizeof(DebugPadButton) == 0x4, "DebugPadButton is an invalid size");
// This is nn::hid::ConsoleSixAxisSensorHandle
struct ConsoleSixAxisSensorHandle {
u8 unknown_1{};
u8 unknown_2{};
INSERT_PADDING_BYTES_NOINIT(2);
};
static_assert(sizeof(ConsoleSixAxisSensorHandle) == 4,
"ConsoleSixAxisSensorHandle is an invalid size");
// This is nn::hid::SixAxisSensorHandle
struct SixAxisSensorHandle {
NpadStyleIndex npad_type{NpadStyleIndex::None};
u8 npad_id{};
DeviceIndex device_index{DeviceIndex::None};
INSERT_PADDING_BYTES_NOINIT(1);
};
static_assert(sizeof(SixAxisSensorHandle) == 4, "SixAxisSensorHandle is an invalid size");
// These parameters seem related to how much gyro/accelerometer is used
struct SixAxisSensorFusionParameters {
f32 parameter1{0.03f}; // Range 0.0 to 1.0, default 0.03
f32 parameter2{0.4f}; // Default 0.4
};
static_assert(sizeof(SixAxisSensorFusionParameters) == 8,
"SixAxisSensorFusionParameters is an invalid size");
// This is nn::hid::server::SixAxisSensorProperties
struct SixAxisSensorProperties {
union {
u8 raw{};
BitField<0, 1, u8> is_newly_assigned;
BitField<1, 1, u8> is_firmware_update_available;
};
};
static_assert(sizeof(SixAxisSensorProperties) == 1, "SixAxisSensorProperties is an invalid size");
// This is nn::hid::SixAxisSensorCalibrationParameter
struct SixAxisSensorCalibrationParameter {
std::array<u8, 0x744> unknown_data;
};
static_assert(sizeof(SixAxisSensorCalibrationParameter) == 0x744,
"SixAxisSensorCalibrationParameter is an invalid size");
static_assert(std::is_trivial_v<SixAxisSensorCalibrationParameter>,
"SixAxisSensorCalibrationParameter must be trivial.");
// This is nn::hid::SixAxisSensorIcInformation
struct SixAxisSensorIcInformation {
f32 angular_rate; // dps
std::array<f32, 6> unknown_gyro_data1; // dps
std::array<f32, 9> unknown_gyro_data2;
std::array<f32, 9> unknown_gyro_data3;
f32 acceleration_range; // g force
std::array<f32, 6> unknown_accel_data1; // g force
std::array<f32, 9> unknown_accel_data2;
std::array<f32, 9> unknown_accel_data3;
};
static_assert(sizeof(SixAxisSensorIcInformation) == 0xC8,
"SixAxisSensorIcInformation is an invalid size");
static_assert(std::is_trivial_v<SixAxisSensorIcInformation>,
"SixAxisSensorIcInformation must be trivial.");
// This is nn::hid::SixAxisSensorAttribute
struct SixAxisSensorAttribute {
union {
u32 raw{};
BitField<0, 1, u32> is_connected;
BitField<1, 1, u32> is_interpolated;
};
};
static_assert(sizeof(SixAxisSensorAttribute) == 4, "SixAxisSensorAttribute is an invalid size");
// This is nn::hid::SixAxisSensorState
struct SixAxisSensorState {
s64 delta_time{};
s64 sampling_number{};
Common::Vec3f accel{};
Common::Vec3f gyro{};
Common::Vec3f rotation{};
std::array<Common::Vec3f, 3> orientation{};
SixAxisSensorAttribute attribute{};
INSERT_PADDING_BYTES(4); // Reserved
};
static_assert(sizeof(SixAxisSensorState) == 0x60, "SixAxisSensorState is an invalid size");
// This is nn::hid::VibrationDeviceHandle
struct VibrationDeviceHandle {
NpadStyleIndex npad_type{NpadStyleIndex::None};
u8 npad_id{};
DeviceIndex device_index{DeviceIndex::None};
INSERT_PADDING_BYTES_NOINIT(1);
};
static_assert(sizeof(VibrationDeviceHandle) == 4, "SixAxisSensorHandle is an invalid size");
// This is nn::hid::VibrationValue
struct VibrationValue {
f32 low_amplitude{};
f32 low_frequency{};
f32 high_amplitude{};
f32 high_frequency{};
bool operator==(const VibrationValue& b) {
if (low_amplitude != b.low_amplitude || high_amplitude != b.high_amplitude) {
return false;
}
// Changes in frequency without amplitude don't have any effect
if (low_amplitude == 0 && high_amplitude == 0) {
return true;
}
if (low_frequency != b.low_frequency || high_frequency != b.high_frequency) {
return false;
}
return true;
}
};
static_assert(sizeof(VibrationValue) == 0x10, "VibrationValue has incorrect size.");
constexpr VibrationValue DEFAULT_VIBRATION_VALUE{
.low_amplitude = 0.0f,
.low_frequency = 160.0f,
.high_amplitude = 0.0f,
.high_frequency = 320.0f,
};
// This is nn::hid::VibrationDeviceInfo
struct VibrationDeviceInfo {
VibrationDeviceType type{};
VibrationDevicePosition position{};
};
static_assert(sizeof(VibrationDeviceInfo) == 0x8, "VibrationDeviceInfo has incorrect size.");
// This is nn::hid::KeyboardModifier
struct KeyboardModifier {
union {
u32 raw{};
BitField<0, 1, u32> control;
BitField<1, 1, u32> shift;
BitField<2, 1, u32> left_alt;
BitField<3, 1, u32> right_alt;
BitField<4, 1, u32> gui;
BitField<8, 1, u32> caps_lock;
BitField<9, 1, u32> scroll_lock;
BitField<10, 1, u32> num_lock;
BitField<11, 1, u32> katakana;
BitField<12, 1, u32> hiragana;
};
};
static_assert(sizeof(KeyboardModifier) == 0x4, "KeyboardModifier is an invalid size");
// This is nn::hid::KeyboardAttribute
struct KeyboardAttribute {
union {
u32 raw{};
BitField<0, 1, u32> is_connected;
};
};
static_assert(sizeof(KeyboardAttribute) == 0x4, "KeyboardAttribute is an invalid size");
// This is nn::hid::KeyboardKey
struct KeyboardKey {
// This should be a 256 bit flag
std::array<u8, 32> key{};
};
static_assert(sizeof(KeyboardKey) == 0x20, "KeyboardKey is an invalid size");
// This is nn::hid::MouseButton
struct MouseButton {
union {
u32_le raw{};
BitField<0, 1, u32> left;
BitField<1, 1, u32> right;
BitField<2, 1, u32> middle;
BitField<3, 1, u32> forward;
BitField<4, 1, u32> back;
};
};
static_assert(sizeof(MouseButton) == 0x4, "MouseButton is an invalid size");
// This is nn::hid::MouseAttribute
struct MouseAttribute {
union {
u32 raw{};
BitField<0, 1, u32> transferable;
BitField<1, 1, u32> is_connected;
};
};
static_assert(sizeof(MouseAttribute) == 0x4, "MouseAttribute is an invalid size");
// This is nn::hid::detail::MouseState
struct MouseState {
s64 sampling_number{};
s32 x{};
s32 y{};
s32 delta_x{};
s32 delta_y{};
// Axis Order in HW is switched for the wheel
s32 delta_wheel_y{};
s32 delta_wheel_x{};
MouseButton button{};
MouseAttribute attribute{};
};
static_assert(sizeof(MouseState) == 0x28, "MouseState is an invalid size");
struct UniquePadId {
u64 id;
};
static_assert(sizeof(UniquePadId) == 0x8, "UniquePadId is an invalid size");
// This is nn::hid::system::FirmwareVersion
struct FirmwareVersion {
u8 major;
u8 minor;
u8 micro;
u8 revision;
std::array<char, 0xc> device_identifier;
};
static_assert(sizeof(FirmwareVersion) == 0x10, "FirmwareVersion is an invalid size");
} // namespace Core::HID

146
src/hid_core/hid_util.h Executable file
View File

@@ -0,0 +1,146 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "hid_core/hid_result.h"
#include "hid_core/hid_types.h"
namespace Service::HID {
constexpr bool IsNpadIdValid(const Core::HID::NpadIdType npad_id) {
switch (npad_id) {
case Core::HID::NpadIdType::Player1:
case Core::HID::NpadIdType::Player2:
case Core::HID::NpadIdType::Player3:
case Core::HID::NpadIdType::Player4:
case Core::HID::NpadIdType::Player5:
case Core::HID::NpadIdType::Player6:
case Core::HID::NpadIdType::Player7:
case Core::HID::NpadIdType::Player8:
case Core::HID::NpadIdType::Other:
case Core::HID::NpadIdType::Handheld:
return true;
default:
return false;
}
}
constexpr Result IsSixaxisHandleValid(const Core::HID::SixAxisSensorHandle& handle) {
const auto npad_id = IsNpadIdValid(static_cast<Core::HID::NpadIdType>(handle.npad_id));
const bool device_index = handle.device_index < Core::HID::DeviceIndex::MaxDeviceIndex;
if (!npad_id) {
return ResultInvalidNpadId;
}
if (!device_index) {
return NpadDeviceIndexOutOfRange;
}
return ResultSuccess;
}
constexpr Result IsVibrationHandleValid(const Core::HID::VibrationDeviceHandle& handle) {
switch (handle.npad_type) {
case Core::HID::NpadStyleIndex::Fullkey:
case Core::HID::NpadStyleIndex::Handheld:
case Core::HID::NpadStyleIndex::JoyconDual:
case Core::HID::NpadStyleIndex::JoyconLeft:
case Core::HID::NpadStyleIndex::JoyconRight:
case Core::HID::NpadStyleIndex::GameCube:
case Core::HID::NpadStyleIndex::N64:
case Core::HID::NpadStyleIndex::SystemExt:
case Core::HID::NpadStyleIndex::System:
// These support vibration
break;
default:
return ResultVibrationInvalidStyleIndex;
}
if (!IsNpadIdValid(static_cast<Core::HID::NpadIdType>(handle.npad_id))) {
return ResultVibrationInvalidNpadId;
}
if (handle.device_index >= Core::HID::DeviceIndex::MaxDeviceIndex) {
return ResultVibrationDeviceIndexOutOfRange;
}
return ResultSuccess;
}
/// Converts a Core::HID::NpadIdType to an array index.
constexpr size_t NpadIdTypeToIndex(Core::HID::NpadIdType npad_id_type) {
switch (npad_id_type) {
case Core::HID::NpadIdType::Player1:
return 0;
case Core::HID::NpadIdType::Player2:
return 1;
case Core::HID::NpadIdType::Player3:
return 2;
case Core::HID::NpadIdType::Player4:
return 3;
case Core::HID::NpadIdType::Player5:
return 4;
case Core::HID::NpadIdType::Player6:
return 5;
case Core::HID::NpadIdType::Player7:
return 6;
case Core::HID::NpadIdType::Player8:
return 7;
case Core::HID::NpadIdType::Handheld:
return 8;
case Core::HID::NpadIdType::Other:
return 9;
default:
return 8;
}
}
/// Converts an array index to a Core::HID::NpadIdType
constexpr Core::HID::NpadIdType IndexToNpadIdType(size_t index) {
switch (index) {
case 0:
return Core::HID::NpadIdType::Player1;
case 1:
return Core::HID::NpadIdType::Player2;
case 2:
return Core::HID::NpadIdType::Player3;
case 3:
return Core::HID::NpadIdType::Player4;
case 4:
return Core::HID::NpadIdType::Player5;
case 5:
return Core::HID::NpadIdType::Player6;
case 6:
return Core::HID::NpadIdType::Player7;
case 7:
return Core::HID::NpadIdType::Player8;
case 8:
return Core::HID::NpadIdType::Handheld;
case 9:
return Core::HID::NpadIdType::Other;
default:
return Core::HID::NpadIdType::Invalid;
}
}
constexpr Core::HID::NpadStyleSet GetStylesetByIndex(std::size_t index) {
switch (index) {
case 0:
return Core::HID::NpadStyleSet::Fullkey;
case 1:
return Core::HID::NpadStyleSet::Handheld;
case 2:
return Core::HID::NpadStyleSet::JoyDual;
case 3:
return Core::HID::NpadStyleSet::JoyLeft;
case 4:
return Core::HID::NpadStyleSet::JoyRight;
case 5:
return Core::HID::NpadStyleSet::Palma;
default:
return Core::HID::NpadStyleSet::None;
}
}
} // namespace Service::HID

View File

@@ -0,0 +1,73 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_readable_event.h"
#include "core/hle/service/kernel_helpers.h"
#include "hid_core/hid_core.h"
#include "hid_core/hidbus/hidbus_base.h"
namespace Service::HID {
HidbusBase::HidbusBase(Core::System& system_, KernelHelpers::ServiceContext& service_context_)
: system(system_), service_context(service_context_) {
send_command_async_event = service_context.CreateEvent("hidbus:SendCommandAsyncEvent");
}
HidbusBase::~HidbusBase() {
service_context.CloseEvent(send_command_async_event);
};
void HidbusBase::ActivateDevice() {
if (is_activated) {
return;
}
is_activated = true;
OnInit();
}
void HidbusBase::DeactivateDevice() {
if (is_activated) {
OnRelease();
}
is_activated = false;
}
bool HidbusBase::IsDeviceActivated() const {
return is_activated;
}
void HidbusBase::Enable(bool enable) {
device_enabled = enable;
}
bool HidbusBase::IsEnabled() const {
return device_enabled;
}
bool HidbusBase::IsPollingMode() const {
return polling_mode_enabled;
}
JoyPollingMode HidbusBase::GetPollingMode() const {
return polling_mode;
}
void HidbusBase::SetPollingMode(JoyPollingMode mode) {
polling_mode = mode;
polling_mode_enabled = true;
}
void HidbusBase::DisablePollingMode() {
polling_mode_enabled = false;
}
void HidbusBase::SetTransferMemoryAddress(Common::ProcessAddress t_mem) {
transfer_memory = t_mem;
}
Kernel::KReadableEvent& HidbusBase::GetSendCommandAsycEvent() const {
return send_command_async_event->GetReadableEvent();
}
} // namespace Service::HID

183
src/hid_core/hidbus/hidbus_base.h Executable file
View File

@@ -0,0 +1,183 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <array>
#include <span>
#include "common/typed_address.h"
#include "core/hle/result.h"
namespace Core {
class System;
}
namespace Kernel {
class KEvent;
class KReadableEvent;
} // namespace Kernel
namespace Service::KernelHelpers {
class ServiceContext;
}
namespace Service::HID {
// This is nn::hidbus::JoyPollingMode
enum class JoyPollingMode : u32 {
SixAxisSensorDisable,
SixAxisSensorEnable,
ButtonOnly,
};
struct DataAccessorHeader {
Result result{ResultUnknown};
INSERT_PADDING_WORDS(0x1);
std::array<u8, 0x18> unused{};
u64 latest_entry{};
u64 total_entries{};
};
static_assert(sizeof(DataAccessorHeader) == 0x30, "DataAccessorHeader is an invalid size");
struct JoyDisableSixAxisPollingData {
std::array<u8, 0x26> data;
u8 out_size;
INSERT_PADDING_BYTES(0x1);
u64 sampling_number;
};
static_assert(sizeof(JoyDisableSixAxisPollingData) == 0x30,
"JoyDisableSixAxisPollingData is an invalid size");
struct JoyEnableSixAxisPollingData {
std::array<u8, 0x8> data;
u8 out_size;
INSERT_PADDING_BYTES(0x7);
u64 sampling_number;
};
static_assert(sizeof(JoyEnableSixAxisPollingData) == 0x18,
"JoyEnableSixAxisPollingData is an invalid size");
struct JoyButtonOnlyPollingData {
std::array<u8, 0x2c> data;
u8 out_size;
INSERT_PADDING_BYTES(0x3);
u64 sampling_number;
};
static_assert(sizeof(JoyButtonOnlyPollingData) == 0x38,
"JoyButtonOnlyPollingData is an invalid size");
struct JoyDisableSixAxisPollingEntry {
u64 sampling_number;
JoyDisableSixAxisPollingData polling_data;
};
static_assert(sizeof(JoyDisableSixAxisPollingEntry) == 0x38,
"JoyDisableSixAxisPollingEntry is an invalid size");
struct JoyEnableSixAxisPollingEntry {
u64 sampling_number;
JoyEnableSixAxisPollingData polling_data;
};
static_assert(sizeof(JoyEnableSixAxisPollingEntry) == 0x20,
"JoyEnableSixAxisPollingEntry is an invalid size");
struct JoyButtonOnlyPollingEntry {
u64 sampling_number;
JoyButtonOnlyPollingData polling_data;
};
static_assert(sizeof(JoyButtonOnlyPollingEntry) == 0x40,
"JoyButtonOnlyPollingEntry is an invalid size");
struct JoyDisableSixAxisDataAccessor {
DataAccessorHeader header{};
std::array<JoyDisableSixAxisPollingEntry, 0xb> entries{};
};
static_assert(sizeof(JoyDisableSixAxisDataAccessor) == 0x298,
"JoyDisableSixAxisDataAccessor is an invalid size");
struct JoyEnableSixAxisDataAccessor {
DataAccessorHeader header{};
std::array<JoyEnableSixAxisPollingEntry, 0xb> entries{};
};
static_assert(sizeof(JoyEnableSixAxisDataAccessor) == 0x190,
"JoyEnableSixAxisDataAccessor is an invalid size");
struct ButtonOnlyPollingDataAccessor {
DataAccessorHeader header;
std::array<JoyButtonOnlyPollingEntry, 0xb> entries;
};
static_assert(sizeof(ButtonOnlyPollingDataAccessor) == 0x2F0,
"ButtonOnlyPollingDataAccessor is an invalid size");
class HidbusBase {
public:
explicit HidbusBase(Core::System& system_, KernelHelpers::ServiceContext& service_context_);
virtual ~HidbusBase();
void ActivateDevice();
void DeactivateDevice();
bool IsDeviceActivated() const;
// Enables/disables the device
void Enable(bool enable);
// returns true if device is enabled
bool IsEnabled() const;
// returns true if polling mode is enabled
bool IsPollingMode() const;
// returns polling mode
JoyPollingMode GetPollingMode() const;
// Sets and enables JoyPollingMode
void SetPollingMode(JoyPollingMode mode);
// Disables JoyPollingMode
void DisablePollingMode();
// Called on EnableJoyPollingReceiveMode
void SetTransferMemoryAddress(Common::ProcessAddress t_mem);
Kernel::KReadableEvent& GetSendCommandAsycEvent() const;
virtual void OnInit() {}
virtual void OnRelease() {}
// Updates device transfer memory
virtual void OnUpdate() {}
// Returns the device ID of the joycon
virtual u8 GetDeviceId() const {
return {};
}
// Assigns a command from data
virtual bool SetCommand(std::span<const u8> data) {
return {};
}
// Returns a reply from a command
virtual u64 GetReply(std::span<u8> out_data) const {
return {};
}
protected:
bool is_activated{};
bool device_enabled{};
bool polling_mode_enabled{};
JoyPollingMode polling_mode = {};
// TODO(German77): All data accessors need to be replaced with a ring lifo object
JoyDisableSixAxisDataAccessor disable_sixaxis_data{};
JoyEnableSixAxisDataAccessor enable_sixaxis_data{};
ButtonOnlyPollingDataAccessor button_only_data{};
Common::ProcessAddress transfer_memory{};
Core::System& system;
Kernel::KEvent* send_command_async_event;
KernelHelpers::ServiceContext& service_context;
};
} // namespace Service::HID

291
src/hid_core/hidbus/ringcon.cpp Executable file
View File

@@ -0,0 +1,291 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "core/core.h"
#include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_readable_event.h"
#include "core/memory.h"
#include "hid_core/frontend/emulated_controller.h"
#include "hid_core/hid_core.h"
#include "hid_core/hidbus/ringcon.h"
namespace Service::HID {
RingController::RingController(Core::System& system_,
KernelHelpers::ServiceContext& service_context_)
: HidbusBase(system_, service_context_) {
input = system.HIDCore().GetEmulatedController(Core::HID::NpadIdType::Player1);
}
RingController::~RingController() = default;
void RingController::OnInit() {
input->SetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex,
Common::Input::PollingMode::Ring);
return;
}
void RingController::OnRelease() {
input->SetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex,
Common::Input::PollingMode::Active);
return;
};
void RingController::OnUpdate() {
if (!is_activated) {
return;
}
if (!device_enabled) {
return;
}
if (!polling_mode_enabled || transfer_memory == 0) {
return;
}
// TODO: Increment multitasking counters from motion and sensor data
switch (polling_mode) {
case JoyPollingMode::SixAxisSensorEnable: {
enable_sixaxis_data.header.total_entries = 10;
enable_sixaxis_data.header.result = ResultSuccess;
const auto& last_entry =
enable_sixaxis_data.entries[enable_sixaxis_data.header.latest_entry];
enable_sixaxis_data.header.latest_entry =
(enable_sixaxis_data.header.latest_entry + 1) % 10;
auto& curr_entry = enable_sixaxis_data.entries[enable_sixaxis_data.header.latest_entry];
curr_entry.sampling_number = last_entry.sampling_number + 1;
curr_entry.polling_data.sampling_number = curr_entry.sampling_number;
const RingConData ringcon_value = GetSensorValue();
curr_entry.polling_data.out_size = sizeof(ringcon_value);
std::memcpy(curr_entry.polling_data.data.data(), &ringcon_value, sizeof(ringcon_value));
system.ApplicationMemory().WriteBlock(transfer_memory, &enable_sixaxis_data,
sizeof(enable_sixaxis_data));
break;
}
default:
LOG_ERROR(Service_HID, "Polling mode not supported {}", polling_mode);
break;
}
}
RingController::RingConData RingController::GetSensorValue() const {
RingConData ringcon_sensor_value{
.status = DataValid::Valid,
.data = 0,
};
const f32 force_value = input->GetRingSensorForce().force * range;
ringcon_sensor_value.data = static_cast<s16>(force_value) + idle_value;
return ringcon_sensor_value;
}
u8 RingController::GetDeviceId() const {
return device_id;
}
u64 RingController::GetReply(std::span<u8> out_data) const {
const RingConCommands current_command = command;
switch (current_command) {
case RingConCommands::GetFirmwareVersion:
return GetFirmwareVersionReply(out_data);
case RingConCommands::ReadId:
return GetReadIdReply(out_data);
case RingConCommands::c20105:
return GetC020105Reply(out_data);
case RingConCommands::ReadUnkCal:
return GetReadUnkCalReply(out_data);
case RingConCommands::ReadFactoryCal:
return GetReadFactoryCalReply(out_data);
case RingConCommands::ReadUserCal:
return GetReadUserCalReply(out_data);
case RingConCommands::ReadRepCount:
return GetReadRepCountReply(out_data);
case RingConCommands::ReadTotalPushCount:
return GetReadTotalPushCountReply(out_data);
case RingConCommands::ResetRepCount:
return GetResetRepCountReply(out_data);
case RingConCommands::SaveCalData:
return GetSaveDataReply(out_data);
default:
return GetErrorReply(out_data);
}
}
bool RingController::SetCommand(std::span<const u8> data) {
if (data.size() < 4) {
LOG_ERROR(Service_HID, "Command size not supported {}", data.size());
command = RingConCommands::Error;
return false;
}
std::memcpy(&command, data.data(), sizeof(RingConCommands));
switch (command) {
case RingConCommands::GetFirmwareVersion:
case RingConCommands::ReadId:
case RingConCommands::c20105:
case RingConCommands::ReadUnkCal:
case RingConCommands::ReadFactoryCal:
case RingConCommands::ReadUserCal:
case RingConCommands::ReadRepCount:
case RingConCommands::ReadTotalPushCount:
ASSERT_MSG(data.size() == 0x4, "data.size is not 0x4 bytes");
send_command_async_event->Signal();
return true;
case RingConCommands::ResetRepCount:
ASSERT_MSG(data.size() == 0x4, "data.size is not 0x4 bytes");
total_rep_count = 0;
send_command_async_event->Signal();
return true;
case RingConCommands::SaveCalData: {
ASSERT_MSG(data.size() == 0x14, "data.size is not 0x14 bytes");
SaveCalData save_info{};
std::memcpy(&save_info, data.data(), sizeof(SaveCalData));
user_calibration = save_info.calibration;
send_command_async_event->Signal();
return true;
}
default:
LOG_ERROR(Service_HID, "Command not implemented {}", command);
command = RingConCommands::Error;
// Signal a reply to avoid softlocking the game
send_command_async_event->Signal();
return false;
}
}
u64 RingController::GetFirmwareVersionReply(std::span<u8> out_data) const {
const FirmwareVersionReply reply{
.status = DataValid::Valid,
.firmware = version,
};
return GetData(reply, out_data);
}
u64 RingController::GetReadIdReply(std::span<u8> out_data) const {
// The values are hardcoded from a real joycon
const ReadIdReply reply{
.status = DataValid::Valid,
.id_l_x0 = 8,
.id_l_x0_2 = 41,
.id_l_x4 = 22294,
.id_h_x0 = 19777,
.id_h_x0_2 = 13621,
.id_h_x4 = 8245,
};
return GetData(reply, out_data);
}
u64 RingController::GetC020105Reply(std::span<u8> out_data) const {
const Cmd020105Reply reply{
.status = DataValid::Valid,
.data = 1,
};
return GetData(reply, out_data);
}
u64 RingController::GetReadUnkCalReply(std::span<u8> out_data) const {
const ReadUnkCalReply reply{
.status = DataValid::Valid,
.data = 0,
};
return GetData(reply, out_data);
}
u64 RingController::GetReadFactoryCalReply(std::span<u8> out_data) const {
const ReadFactoryCalReply reply{
.status = DataValid::Valid,
.calibration = factory_calibration,
};
return GetData(reply, out_data);
}
u64 RingController::GetReadUserCalReply(std::span<u8> out_data) const {
const ReadUserCalReply reply{
.status = DataValid::Valid,
.calibration = user_calibration,
};
return GetData(reply, out_data);
}
u64 RingController::GetReadRepCountReply(std::span<u8> out_data) const {
const GetThreeByteReply reply{
.status = DataValid::Valid,
.data = {total_rep_count, 0, 0},
.crc = GetCrcValue({total_rep_count, 0, 0, 0}),
};
return GetData(reply, out_data);
}
u64 RingController::GetReadTotalPushCountReply(std::span<u8> out_data) const {
const GetThreeByteReply reply{
.status = DataValid::Valid,
.data = {total_push_count, 0, 0},
.crc = GetCrcValue({total_push_count, 0, 0, 0}),
};
return GetData(reply, out_data);
}
u64 RingController::GetResetRepCountReply(std::span<u8> out_data) const {
return GetReadRepCountReply(out_data);
}
u64 RingController::GetSaveDataReply(std::span<u8> out_data) const {
const StatusReply reply{
.status = DataValid::Valid,
};
return GetData(reply, out_data);
}
u64 RingController::GetErrorReply(std::span<u8> out_data) const {
const ErrorReply reply{
.status = DataValid::BadCRC,
};
return GetData(reply, out_data);
}
u8 RingController::GetCrcValue(const std::vector<u8>& data) const {
u8 crc = 0;
for (std::size_t index = 0; index < data.size(); index++) {
for (u8 i = 0x80; i > 0; i >>= 1) {
bool bit = (crc & 0x80) != 0;
if ((data[index] & i) != 0) {
bit = !bit;
}
crc <<= 1;
if (bit) {
crc ^= 0x8d;
}
}
}
return crc;
}
template <typename T>
u64 RingController::GetData(const T& reply, std::span<u8> out_data) const {
static_assert(std::is_trivially_copyable_v<T>);
const auto data_size = static_cast<u64>(std::min(sizeof(reply), out_data.size()));
std::memcpy(out_data.data(), &reply, data_size);
return data_size;
}
} // namespace Service::HID

253
src/hid_core/hidbus/ringcon.h Executable file
View File

@@ -0,0 +1,253 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <array>
#include <span>
#include "common/common_types.h"
#include "hid_core/hidbus/hidbus_base.h"
namespace Core::HID {
class EmulatedController;
} // namespace Core::HID
namespace Service::HID {
class RingController final : public HidbusBase {
public:
explicit RingController(Core::System& system_, KernelHelpers::ServiceContext& service_context_);
~RingController() override;
void OnInit() override;
void OnRelease() override;
// Updates ringcon transfer memory
void OnUpdate() override;
// Returns the device ID of the joycon
u8 GetDeviceId() const override;
// Assigns a command from data
bool SetCommand(std::span<const u8> data) override;
// Returns a reply from a command
u64 GetReply(std::span<u8> data) const override;
private:
// These values are obtained from a real ring controller
static constexpr s16 idle_value = 2280;
static constexpr s16 idle_deadzone = 120;
static constexpr s16 range = 2500;
// Most missing command names are leftovers from other firmware versions
enum class RingConCommands : u32 {
GetFirmwareVersion = 0x00020000,
ReadId = 0x00020100,
JoyPolling = 0x00020101,
Unknown1 = 0x00020104,
c20105 = 0x00020105,
Unknown2 = 0x00020204,
Unknown3 = 0x00020304,
Unknown4 = 0x00020404,
ReadUnkCal = 0x00020504,
ReadFactoryCal = 0x00020A04,
Unknown5 = 0x00021104,
Unknown6 = 0x00021204,
Unknown7 = 0x00021304,
ReadUserCal = 0x00021A04,
ReadRepCount = 0x00023104,
ReadTotalPushCount = 0x00023204,
ResetRepCount = 0x04013104,
Unknown8 = 0x04011104,
Unknown9 = 0x04011204,
Unknown10 = 0x04011304,
SaveCalData = 0x10011A04,
Error = 0xFFFFFFFF,
};
enum class DataValid : u32 {
Valid,
BadCRC,
Cal,
};
struct FirmwareVersion {
u8 sub;
u8 main;
};
static_assert(sizeof(FirmwareVersion) == 0x2, "FirmwareVersion is an invalid size");
struct FactoryCalibration {
s32_le os_max;
s32_le hk_max;
s32_le zero_min;
s32_le zero_max;
};
static_assert(sizeof(FactoryCalibration) == 0x10, "FactoryCalibration is an invalid size");
struct CalibrationValue {
s16 value;
u16 crc;
};
static_assert(sizeof(CalibrationValue) == 0x4, "CalibrationValue is an invalid size");
struct UserCalibration {
CalibrationValue os_max;
CalibrationValue hk_max;
CalibrationValue zero;
};
static_assert(sizeof(UserCalibration) == 0xC, "UserCalibration is an invalid size");
struct SaveCalData {
RingConCommands command;
UserCalibration calibration;
INSERT_PADDING_BYTES_NOINIT(4);
};
static_assert(sizeof(SaveCalData) == 0x14, "SaveCalData is an invalid size");
static_assert(std::is_trivially_copyable_v<SaveCalData>,
"SaveCalData must be trivially copyable");
struct FirmwareVersionReply {
DataValid status;
FirmwareVersion firmware;
INSERT_PADDING_BYTES(0x2);
};
static_assert(sizeof(FirmwareVersionReply) == 0x8, "FirmwareVersionReply is an invalid size");
struct Cmd020105Reply {
DataValid status;
u8 data;
INSERT_PADDING_BYTES(0x3);
};
static_assert(sizeof(Cmd020105Reply) == 0x8, "Cmd020105Reply is an invalid size");
struct StatusReply {
DataValid status;
};
static_assert(sizeof(StatusReply) == 0x4, "StatusReply is an invalid size");
struct GetThreeByteReply {
DataValid status;
std::array<u8, 3> data;
u8 crc;
};
static_assert(sizeof(GetThreeByteReply) == 0x8, "GetThreeByteReply is an invalid size");
struct ReadUnkCalReply {
DataValid status;
u16 data;
INSERT_PADDING_BYTES(0x2);
};
static_assert(sizeof(ReadUnkCalReply) == 0x8, "ReadUnkCalReply is an invalid size");
struct ReadFactoryCalReply {
DataValid status;
FactoryCalibration calibration;
};
static_assert(sizeof(ReadFactoryCalReply) == 0x14, "ReadFactoryCalReply is an invalid size");
struct ReadUserCalReply {
DataValid status;
UserCalibration calibration;
INSERT_PADDING_BYTES(0x4);
};
static_assert(sizeof(ReadUserCalReply) == 0x14, "ReadUserCalReply is an invalid size");
struct ReadIdReply {
DataValid status;
u16 id_l_x0;
u16 id_l_x0_2;
u16 id_l_x4;
u16 id_h_x0;
u16 id_h_x0_2;
u16 id_h_x4;
};
static_assert(sizeof(ReadIdReply) == 0x10, "ReadIdReply is an invalid size");
struct ErrorReply {
DataValid status;
INSERT_PADDING_BYTES(0x3);
};
static_assert(sizeof(ErrorReply) == 0x8, "ErrorReply is an invalid size");
struct RingConData {
DataValid status;
s16_le data;
INSERT_PADDING_BYTES(0x2);
};
static_assert(sizeof(RingConData) == 0x8, "RingConData is an invalid size");
// Returns RingConData struct with pressure sensor values
RingConData GetSensorValue() const;
// Returns 8 byte reply with firmware version
u64 GetFirmwareVersionReply(std::span<u8> out_data) const;
// Returns 16 byte reply with ID values
u64 GetReadIdReply(std::span<u8> out_data) const;
// (STUBBED) Returns 8 byte reply
u64 GetC020105Reply(std::span<u8> out_data) const;
// (STUBBED) Returns 8 byte empty reply
u64 GetReadUnkCalReply(std::span<u8> out_data) const;
// Returns 20 byte reply with factory calibration values
u64 GetReadFactoryCalReply(std::span<u8> out_data) const;
// Returns 20 byte reply with user calibration values
u64 GetReadUserCalReply(std::span<u8> out_data) const;
// Returns 8 byte reply
u64 GetReadRepCountReply(std::span<u8> out_data) const;
// Returns 8 byte reply
u64 GetReadTotalPushCountReply(std::span<u8> out_data) const;
// Returns 8 byte reply
u64 GetResetRepCountReply(std::span<u8> out_data) const;
// Returns 4 byte save data reply
u64 GetSaveDataReply(std::span<u8> out_data) const;
// Returns 8 byte error reply
u64 GetErrorReply(std::span<u8> out_data) const;
// Returns 8 bit redundancy check from provided data
u8 GetCrcValue(const std::vector<u8>& data) const;
// Converts structs to an u8 vector equivalent
template <typename T>
u64 GetData(const T& reply, std::span<u8> out_data) const;
RingConCommands command{RingConCommands::Error};
// These counters are used in multitasking mode while the switch is sleeping
// Total steps taken
u8 total_rep_count = 0;
// Total times the ring was pushed
u8 total_push_count = 0;
const u8 device_id = 0x20;
const FirmwareVersion version = {
.sub = 0x0,
.main = 0x2c,
};
const FactoryCalibration factory_calibration = {
.os_max = idle_value + range + idle_deadzone,
.hk_max = idle_value - range - idle_deadzone,
.zero_min = idle_value - idle_deadzone,
.zero_max = idle_value + idle_deadzone,
};
UserCalibration user_calibration = {
.os_max = {.value = range, .crc = 228},
.hk_max = {.value = -range, .crc = 239},
.zero = {.value = idle_value, .crc = 225},
};
Core::HID::EmulatedController* input;
};
} // namespace Service::HID

View File

@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "hid_core/frontend/emulated_controller.h"
#include "hid_core/hid_core.h"
#include "hid_core/hidbus/starlink.h"
namespace Service::HID {
constexpr u8 DEVICE_ID = 0x28;
Starlink::Starlink(Core::System& system_, KernelHelpers::ServiceContext& service_context_)
: HidbusBase(system_, service_context_) {}
Starlink::~Starlink() = default;
void Starlink::OnInit() {
return;
}
void Starlink::OnRelease() {
return;
};
void Starlink::OnUpdate() {
if (!is_activated) {
return;
}
if (!device_enabled) {
return;
}
if (!polling_mode_enabled || transfer_memory == 0) {
return;
}
LOG_ERROR(Service_HID, "Polling mode not supported {}", polling_mode);
}
u8 Starlink::GetDeviceId() const {
return DEVICE_ID;
}
u64 Starlink::GetReply(std::span<u8> out_data) const {
return {};
}
bool Starlink::SetCommand(std::span<const u8> data) {
LOG_ERROR(Service_HID, "Command not implemented");
return false;
}
} // namespace Service::HID

37
src/hid_core/hidbus/starlink.h Executable file
View File

@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "common/common_types.h"
#include "hid_core/hidbus/hidbus_base.h"
namespace Core::HID {
class EmulatedController;
} // namespace Core::HID
namespace Service::HID {
class Starlink final : public HidbusBase {
public:
explicit Starlink(Core::System& system_, KernelHelpers::ServiceContext& service_context_);
~Starlink() override;
void OnInit() override;
void OnRelease() override;
// Updates ringcon transfer memory
void OnUpdate() override;
// Returns the device ID of the joycon
u8 GetDeviceId() const override;
// Assigns a command from data
bool SetCommand(std::span<const u8> data) override;
// Returns a reply from a command
u64 GetReply(std::span<u8> out_data) const override;
};
} // namespace Service::HID

50
src/hid_core/hidbus/stubbed.cpp Executable file
View File

@@ -0,0 +1,50 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "hid_core/frontend/emulated_controller.h"
#include "hid_core/hid_core.h"
#include "hid_core/hidbus/stubbed.h"
namespace Service::HID {
constexpr u8 DEVICE_ID = 0xFF;
HidbusStubbed::HidbusStubbed(Core::System& system_, KernelHelpers::ServiceContext& service_context_)
: HidbusBase(system_, service_context_) {}
HidbusStubbed::~HidbusStubbed() = default;
void HidbusStubbed::OnInit() {
return;
}
void HidbusStubbed::OnRelease() {
return;
};
void HidbusStubbed::OnUpdate() {
if (!is_activated) {
return;
}
if (!device_enabled) {
return;
}
if (!polling_mode_enabled || transfer_memory == 0) {
return;
}
LOG_ERROR(Service_HID, "Polling mode not supported {}", polling_mode);
}
u8 HidbusStubbed::GetDeviceId() const {
return DEVICE_ID;
}
u64 HidbusStubbed::GetReply(std::span<u8> out_data) const {
return {};
}
bool HidbusStubbed::SetCommand(std::span<const u8> data) {
LOG_ERROR(Service_HID, "Command not implemented");
return false;
}
} // namespace Service::HID

37
src/hid_core/hidbus/stubbed.h Executable file
View File

@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "common/common_types.h"
#include "hid_core/hidbus/hidbus_base.h"
namespace Core::HID {
class EmulatedController;
} // namespace Core::HID
namespace Service::HID {
class HidbusStubbed final : public HidbusBase {
public:
explicit HidbusStubbed(Core::System& system_, KernelHelpers::ServiceContext& service_context_);
~HidbusStubbed() override;
void OnInit() override;
void OnRelease() override;
// Updates ringcon transfer memory
void OnUpdate() override;
// Returns the device ID of the joycon
u8 GetDeviceId() const override;
// Assigns a command from data
bool SetCommand(std::span<const u8> data) override;
// Returns a reply from a command
u64 GetReply(std::span<u8> out_data) const override;
};
} // namespace Service::HID

View File

@@ -0,0 +1,267 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include <queue>
#include "core/core.h"
#include "core/core_timing.h"
#include "hid_core/frontend/emulated_controller.h"
#include "hid_core/hid_core.h"
#include "hid_core/irsensor/clustering_processor.h"
namespace Service::IRS {
ClusteringProcessor::ClusteringProcessor(Core::System& system_,
Core::IrSensor::DeviceFormat& device_format,
std::size_t npad_index)
: device{device_format}, system{system_} {
npad_device = system.HIDCore().GetEmulatedControllerByIndex(npad_index);
device.mode = Core::IrSensor::IrSensorMode::ClusteringProcessor;
device.camera_status = Core::IrSensor::IrCameraStatus::Unconnected;
device.camera_internal_status = Core::IrSensor::IrCameraInternalStatus::Stopped;
SetDefaultConfig();
shared_memory = std::construct_at(
reinterpret_cast<ClusteringSharedMemory*>(&device_format.state.processor_raw_data));
Core::HID::ControllerUpdateCallback engine_callback{
.on_change = [this](Core::HID::ControllerTriggerType type) { OnControllerUpdate(type); },
.is_npad_service = true,
};
callback_key = npad_device->SetCallback(engine_callback);
}
ClusteringProcessor::~ClusteringProcessor() {
npad_device->DeleteCallback(callback_key);
};
void ClusteringProcessor::StartProcessor() {
device.camera_status = Core::IrSensor::IrCameraStatus::Available;
device.camera_internal_status = Core::IrSensor::IrCameraInternalStatus::Ready;
}
void ClusteringProcessor::SuspendProcessor() {}
void ClusteringProcessor::StopProcessor() {}
void ClusteringProcessor::OnControllerUpdate(Core::HID::ControllerTriggerType type) {
if (type != Core::HID::ControllerTriggerType::IrSensor) {
return;
}
next_state = {};
const auto& camera_data = npad_device->GetCamera();
auto filtered_image = camera_data.data;
RemoveLowIntensityData(filtered_image);
const auto window_start_x = static_cast<std::size_t>(current_config.window_of_interest.x);
const auto window_start_y = static_cast<std::size_t>(current_config.window_of_interest.y);
const auto window_end_x =
window_start_x + static_cast<std::size_t>(current_config.window_of_interest.width);
const auto window_end_y =
window_start_y + static_cast<std::size_t>(current_config.window_of_interest.height);
for (std::size_t y = window_start_y; y < window_end_y; y++) {
for (std::size_t x = window_start_x; x < window_end_x; x++) {
u8 pixel = GetPixel(filtered_image, x, y);
if (pixel == 0) {
continue;
}
const auto cluster = GetClusterProperties(filtered_image, x, y);
if (cluster.pixel_count > current_config.pixel_count_max) {
continue;
}
if (cluster.pixel_count < current_config.pixel_count_min) {
continue;
}
// Cluster object limit reached
if (next_state.object_count >= next_state.data.size()) {
continue;
}
next_state.data[next_state.object_count] = cluster;
next_state.object_count++;
}
}
next_state.sampling_number = camera_data.sample;
next_state.timestamp = system.CoreTiming().GetGlobalTimeNs().count();
next_state.ambient_noise_level = Core::IrSensor::CameraAmbientNoiseLevel::Low;
shared_memory->clustering_lifo.WriteNextEntry(next_state);
if (!IsProcessorActive()) {
StartProcessor();
}
}
void ClusteringProcessor::RemoveLowIntensityData(std::vector<u8>& data) {
for (u8& pixel : data) {
if (pixel < current_config.pixel_count_min) {
pixel = 0;
}
}
}
ClusteringProcessor::ClusteringData ClusteringProcessor::GetClusterProperties(std::vector<u8>& data,
std::size_t x,
std::size_t y) {
using DataPoint = Common::Point<std::size_t>;
std::queue<DataPoint> search_points{};
ClusteringData current_cluster = GetPixelProperties(data, x, y);
SetPixel(data, x, y, 0);
search_points.emplace<DataPoint>({x, y});
while (!search_points.empty()) {
const auto point = search_points.front();
search_points.pop();
// Avoid negative numbers
if (point.x == 0 || point.y == 0) {
continue;
}
std::array<DataPoint, 4> new_points{
DataPoint{point.x - 1, point.y},
{point.x, point.y - 1},
{point.x + 1, point.y},
{point.x, point.y + 1},
};
for (const auto new_point : new_points) {
if (new_point.x >= width) {
continue;
}
if (new_point.y >= height) {
continue;
}
if (GetPixel(data, new_point.x, new_point.y) < current_config.object_intensity_min) {
continue;
}
const ClusteringData cluster = GetPixelProperties(data, new_point.x, new_point.y);
current_cluster = MergeCluster(current_cluster, cluster);
SetPixel(data, new_point.x, new_point.y, 0);
search_points.emplace<DataPoint>({new_point.x, new_point.y});
}
}
return current_cluster;
}
ClusteringProcessor::ClusteringData ClusteringProcessor::GetPixelProperties(
const std::vector<u8>& data, std::size_t x, std::size_t y) const {
return {
.average_intensity = GetPixel(data, x, y) / 255.0f,
.centroid =
{
.x = static_cast<f32>(x),
.y = static_cast<f32>(y),
},
.pixel_count = 1,
.bound =
{
.x = static_cast<s16>(x),
.y = static_cast<s16>(y),
.width = 1,
.height = 1,
},
};
}
ClusteringProcessor::ClusteringData ClusteringProcessor::MergeCluster(
const ClusteringData a, const ClusteringData b) const {
const f32 a_pixel_count = static_cast<f32>(a.pixel_count);
const f32 b_pixel_count = static_cast<f32>(b.pixel_count);
const f32 pixel_count = a_pixel_count + b_pixel_count;
const f32 average_intensity =
(a.average_intensity * a_pixel_count + b.average_intensity * b_pixel_count) / pixel_count;
const Core::IrSensor::IrsCentroid centroid = {
.x = (a.centroid.x * a_pixel_count + b.centroid.x * b_pixel_count) / pixel_count,
.y = (a.centroid.y * a_pixel_count + b.centroid.y * b_pixel_count) / pixel_count,
};
s16 bound_start_x = a.bound.x < b.bound.x ? a.bound.x : b.bound.x;
s16 bound_start_y = a.bound.y < b.bound.y ? a.bound.y : b.bound.y;
s16 a_bound_end_x = a.bound.x + a.bound.width;
s16 a_bound_end_y = a.bound.y + a.bound.height;
s16 b_bound_end_x = b.bound.x + b.bound.width;
s16 b_bound_end_y = b.bound.y + b.bound.height;
const Core::IrSensor::IrsRect bound = {
.x = bound_start_x,
.y = bound_start_y,
.width = a_bound_end_x > b_bound_end_x ? static_cast<s16>(a_bound_end_x - bound_start_x)
: static_cast<s16>(b_bound_end_x - bound_start_x),
.height = a_bound_end_y > b_bound_end_y ? static_cast<s16>(a_bound_end_y - bound_start_y)
: static_cast<s16>(b_bound_end_y - bound_start_y),
};
return {
.average_intensity = average_intensity,
.centroid = centroid,
.pixel_count = static_cast<u32>(pixel_count),
.bound = bound,
};
}
u8 ClusteringProcessor::GetPixel(const std::vector<u8>& data, std::size_t x, std::size_t y) const {
if ((y * width) + x >= data.size()) {
return 0;
}
return data[(y * width) + x];
}
void ClusteringProcessor::SetPixel(std::vector<u8>& data, std::size_t x, std::size_t y, u8 value) {
if ((y * width) + x >= data.size()) {
return;
}
data[(y * width) + x] = value;
}
void ClusteringProcessor::SetDefaultConfig() {
using namespace std::literals::chrono_literals;
current_config.camera_config.exposure_time = std::chrono::microseconds(200ms).count();
current_config.camera_config.gain = 2;
current_config.camera_config.is_negative_used = false;
current_config.camera_config.light_target = Core::IrSensor::CameraLightTarget::BrightLeds;
current_config.window_of_interest = {
.x = 0,
.y = 0,
.width = width,
.height = height,
};
current_config.pixel_count_min = 3;
current_config.pixel_count_max = static_cast<u32>(GetDataSize(format));
current_config.is_external_light_filter_enabled = true;
current_config.object_intensity_min = 150;
npad_device->SetCameraFormat(format);
}
void ClusteringProcessor::SetConfig(Core::IrSensor::PackedClusteringProcessorConfig config) {
current_config.camera_config.exposure_time = config.camera_config.exposure_time;
current_config.camera_config.gain = config.camera_config.gain;
current_config.camera_config.is_negative_used = config.camera_config.is_negative_used;
current_config.camera_config.light_target =
static_cast<Core::IrSensor::CameraLightTarget>(config.camera_config.light_target);
current_config.window_of_interest = config.window_of_interest;
current_config.pixel_count_min = config.pixel_count_min;
current_config.pixel_count_max = config.pixel_count_max;
current_config.is_external_light_filter_enabled = config.is_external_light_filter_enabled;
current_config.object_intensity_min = config.object_intensity_min;
LOG_INFO(Service_IRS,
"Processor config, exposure_time={}, gain={}, is_negative_used={}, "
"light_target={}, window_of_interest=({}, {}, {}, {}), pixel_count_min={}, "
"pixel_count_max={}, is_external_light_filter_enabled={}, object_intensity_min={}",
current_config.camera_config.exposure_time, current_config.camera_config.gain,
current_config.camera_config.is_negative_used,
current_config.camera_config.light_target, current_config.window_of_interest.x,
current_config.window_of_interest.y, current_config.window_of_interest.width,
current_config.window_of_interest.height, current_config.pixel_count_min,
current_config.pixel_count_max, current_config.is_external_light_filter_enabled,
current_config.object_intensity_min);
npad_device->SetCameraFormat(format);
}
} // namespace Service::IRS

View File

@@ -0,0 +1,115 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "hid_core/irsensor/irs_types.h"
#include "hid_core/irsensor/processor_base.h"
#include "hid_core/resources/irs_ring_lifo.h"
namespace Core {
class System;
}
namespace Core::HID {
class EmulatedController;
} // namespace Core::HID
namespace Service::IRS {
class ClusteringProcessor final : public ProcessorBase {
public:
explicit ClusteringProcessor(Core::System& system_, Core::IrSensor::DeviceFormat& device_format,
std::size_t npad_index);
~ClusteringProcessor() override;
// Called when the processor is initialized
void StartProcessor() override;
// Called when the processor is suspended
void SuspendProcessor() override;
// Called when the processor is stopped
void StopProcessor() override;
// Sets config parameters of the camera
void SetConfig(Core::IrSensor::PackedClusteringProcessorConfig config);
private:
static constexpr auto format = Core::IrSensor::ImageTransferProcessorFormat::Size320x240;
static constexpr std::size_t width = 320;
static constexpr std::size_t height = 240;
// This is nn::irsensor::ClusteringProcessorConfig
struct ClusteringProcessorConfig {
Core::IrSensor::CameraConfig camera_config;
Core::IrSensor::IrsRect window_of_interest;
u32 pixel_count_min;
u32 pixel_count_max;
u32 object_intensity_min;
bool is_external_light_filter_enabled;
INSERT_PADDING_BYTES(3);
};
static_assert(sizeof(ClusteringProcessorConfig) == 0x30,
"ClusteringProcessorConfig is an invalid size");
// This is nn::irsensor::AdaptiveClusteringProcessorConfig
struct AdaptiveClusteringProcessorConfig {
Core::IrSensor::AdaptiveClusteringMode mode;
Core::IrSensor::AdaptiveClusteringTargetDistance target_distance;
};
static_assert(sizeof(AdaptiveClusteringProcessorConfig) == 0x8,
"AdaptiveClusteringProcessorConfig is an invalid size");
// This is nn::irsensor::ClusteringData
struct ClusteringData {
f32 average_intensity;
Core::IrSensor::IrsCentroid centroid;
u32 pixel_count;
Core::IrSensor::IrsRect bound;
};
static_assert(sizeof(ClusteringData) == 0x18, "ClusteringData is an invalid size");
// This is nn::irsensor::ClusteringProcessorState
struct ClusteringProcessorState {
s64 sampling_number;
u64 timestamp;
u8 object_count;
INSERT_PADDING_BYTES(3);
Core::IrSensor::CameraAmbientNoiseLevel ambient_noise_level;
std::array<ClusteringData, 0x10> data;
};
static_assert(sizeof(ClusteringProcessorState) == 0x198,
"ClusteringProcessorState is an invalid size");
struct ClusteringSharedMemory {
Service::IRS::Lifo<ClusteringProcessorState, 6> clustering_lifo;
static_assert(sizeof(clustering_lifo) == 0x9A0, "clustering_lifo is an invalid size");
INSERT_PADDING_WORDS(0x11F);
};
static_assert(sizeof(ClusteringSharedMemory) == 0xE20,
"ClusteringSharedMemory is an invalid size");
void OnControllerUpdate(Core::HID::ControllerTriggerType type);
void RemoveLowIntensityData(std::vector<u8>& data);
ClusteringData GetClusterProperties(std::vector<u8>& data, std::size_t x, std::size_t y);
ClusteringData GetPixelProperties(const std::vector<u8>& data, std::size_t x,
std::size_t y) const;
ClusteringData MergeCluster(const ClusteringData a, const ClusteringData b) const;
u8 GetPixel(const std::vector<u8>& data, std::size_t x, std::size_t y) const;
void SetPixel(std::vector<u8>& data, std::size_t x, std::size_t y, u8 value);
// Sets config parameters of the camera
void SetDefaultConfig();
ClusteringSharedMemory* shared_memory = nullptr;
ClusteringProcessorState next_state{};
ClusteringProcessorConfig current_config{};
Core::IrSensor::DeviceFormat& device;
Core::HID::EmulatedController* npad_device;
int callback_key{};
Core::System& system;
};
} // namespace Service::IRS

View File

@@ -0,0 +1,154 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/core.h"
#include "core/memory.h"
#include "hid_core/frontend/emulated_controller.h"
#include "hid_core/hid_core.h"
#include "hid_core/irsensor/image_transfer_processor.h"
namespace Service::IRS {
ImageTransferProcessor::ImageTransferProcessor(Core::System& system_,
Core::IrSensor::DeviceFormat& device_format,
std::size_t npad_index)
: device{device_format}, system{system_} {
npad_device = system.HIDCore().GetEmulatedControllerByIndex(npad_index);
Core::HID::ControllerUpdateCallback engine_callback{
.on_change = [this](Core::HID::ControllerTriggerType type) { OnControllerUpdate(type); },
.is_npad_service = true,
};
callback_key = npad_device->SetCallback(engine_callback);
device.mode = Core::IrSensor::IrSensorMode::ImageTransferProcessor;
device.camera_status = Core::IrSensor::IrCameraStatus::Unconnected;
device.camera_internal_status = Core::IrSensor::IrCameraInternalStatus::Stopped;
}
ImageTransferProcessor::~ImageTransferProcessor() {
npad_device->DeleteCallback(callback_key);
};
void ImageTransferProcessor::StartProcessor() {
is_active = true;
device.camera_status = Core::IrSensor::IrCameraStatus::Available;
device.camera_internal_status = Core::IrSensor::IrCameraInternalStatus::Ready;
processor_state.sampling_number = 0;
processor_state.ambient_noise_level = Core::IrSensor::CameraAmbientNoiseLevel::Low;
}
void ImageTransferProcessor::SuspendProcessor() {}
void ImageTransferProcessor::StopProcessor() {}
void ImageTransferProcessor::OnControllerUpdate(Core::HID::ControllerTriggerType type) {
if (type != Core::HID::ControllerTriggerType::IrSensor) {
return;
}
if (transfer_memory == 0) {
return;
}
const auto& camera_data = npad_device->GetCamera();
// This indicates how much ambient light is present
processor_state.ambient_noise_level = Core::IrSensor::CameraAmbientNoiseLevel::Low;
processor_state.sampling_number = camera_data.sample;
if (camera_data.format != current_config.origin_format) {
LOG_WARNING(Service_IRS, "Wrong Input format {} expected {}", camera_data.format,
current_config.origin_format);
system.ApplicationMemory().ZeroBlock(transfer_memory,
GetDataSize(current_config.trimming_format));
return;
}
if (current_config.origin_format > current_config.trimming_format) {
LOG_WARNING(Service_IRS, "Origin format {} is smaller than trimming format {}",
current_config.origin_format, current_config.trimming_format);
system.ApplicationMemory().ZeroBlock(transfer_memory,
GetDataSize(current_config.trimming_format));
return;
}
std::vector<u8> window_data{};
const auto origin_width = GetDataWidth(current_config.origin_format);
const auto origin_height = GetDataHeight(current_config.origin_format);
const auto trimming_width = GetDataWidth(current_config.trimming_format);
const auto trimming_height = GetDataHeight(current_config.trimming_format);
window_data.resize(GetDataSize(current_config.trimming_format));
if (trimming_width + current_config.trimming_start_x > origin_width ||
trimming_height + current_config.trimming_start_y > origin_height) {
LOG_WARNING(Service_IRS,
"Trimming area ({}, {}, {}, {}) is outside of origin area ({}, {})",
current_config.trimming_start_x, current_config.trimming_start_y,
trimming_width, trimming_height, origin_width, origin_height);
system.ApplicationMemory().ZeroBlock(transfer_memory,
GetDataSize(current_config.trimming_format));
return;
}
for (std::size_t y = 0; y < trimming_height; y++) {
for (std::size_t x = 0; x < trimming_width; x++) {
const std::size_t window_index = (y * trimming_width) + x;
const std::size_t origin_index =
((y + current_config.trimming_start_y) * origin_width) + x +
current_config.trimming_start_x;
window_data[window_index] = camera_data.data[origin_index];
}
}
system.ApplicationMemory().WriteBlock(transfer_memory, window_data.data(),
GetDataSize(current_config.trimming_format));
if (!IsProcessorActive()) {
StartProcessor();
}
}
void ImageTransferProcessor::SetConfig(Core::IrSensor::PackedImageTransferProcessorConfig config) {
current_config.camera_config.exposure_time = config.camera_config.exposure_time;
current_config.camera_config.gain = config.camera_config.gain;
current_config.camera_config.is_negative_used = config.camera_config.is_negative_used;
current_config.camera_config.light_target =
static_cast<Core::IrSensor::CameraLightTarget>(config.camera_config.light_target);
current_config.origin_format =
static_cast<Core::IrSensor::ImageTransferProcessorFormat>(config.format);
current_config.trimming_format =
static_cast<Core::IrSensor::ImageTransferProcessorFormat>(config.format);
current_config.trimming_start_x = 0;
current_config.trimming_start_y = 0;
npad_device->SetCameraFormat(current_config.origin_format);
}
void ImageTransferProcessor::SetConfig(
Core::IrSensor::PackedImageTransferProcessorExConfig config) {
current_config.camera_config.exposure_time = config.camera_config.exposure_time;
current_config.camera_config.gain = config.camera_config.gain;
current_config.camera_config.is_negative_used = config.camera_config.is_negative_used;
current_config.camera_config.light_target =
static_cast<Core::IrSensor::CameraLightTarget>(config.camera_config.light_target);
current_config.origin_format =
static_cast<Core::IrSensor::ImageTransferProcessorFormat>(config.origin_format);
current_config.trimming_format =
static_cast<Core::IrSensor::ImageTransferProcessorFormat>(config.trimming_format);
current_config.trimming_start_x = config.trimming_start_x;
current_config.trimming_start_y = config.trimming_start_y;
npad_device->SetCameraFormat(current_config.origin_format);
}
void ImageTransferProcessor::SetTransferMemoryAddress(Common::ProcessAddress t_mem) {
transfer_memory = t_mem;
}
Core::IrSensor::ImageTransferProcessorState ImageTransferProcessor::GetState(
std::span<u8> data) const {
const auto size = std::min(GetDataSize(current_config.trimming_format), data.size());
system.ApplicationMemory().ReadBlock(transfer_memory, data.data(), size);
return processor_state;
}
} // namespace Service::IRS

View File

@@ -0,0 +1,79 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <span>
#include "common/typed_address.h"
#include "hid_core/irsensor/irs_types.h"
#include "hid_core/irsensor/processor_base.h"
namespace Core {
class System;
}
namespace Core::HID {
class EmulatedController;
} // namespace Core::HID
namespace Service::IRS {
class ImageTransferProcessor final : public ProcessorBase {
public:
explicit ImageTransferProcessor(Core::System& system_,
Core::IrSensor::DeviceFormat& device_format,
std::size_t npad_index);
~ImageTransferProcessor() override;
// Called when the processor is initialized
void StartProcessor() override;
// Called when the processor is suspended
void SuspendProcessor() override;
// Called when the processor is stopped
void StopProcessor() override;
// Sets config parameters of the camera
void SetConfig(Core::IrSensor::PackedImageTransferProcessorConfig config);
void SetConfig(Core::IrSensor::PackedImageTransferProcessorExConfig config);
// Transfer memory where the image data will be stored
void SetTransferMemoryAddress(Common::ProcessAddress t_mem);
Core::IrSensor::ImageTransferProcessorState GetState(std::span<u8> data) const;
private:
// This is nn::irsensor::ImageTransferProcessorConfig
struct ImageTransferProcessorConfig {
Core::IrSensor::CameraConfig camera_config;
Core::IrSensor::ImageTransferProcessorFormat format;
};
static_assert(sizeof(ImageTransferProcessorConfig) == 0x20,
"ImageTransferProcessorConfig is an invalid size");
// This is nn::irsensor::ImageTransferProcessorExConfig
struct ImageTransferProcessorExConfig {
Core::IrSensor::CameraConfig camera_config;
Core::IrSensor::ImageTransferProcessorFormat origin_format;
Core::IrSensor::ImageTransferProcessorFormat trimming_format;
u16 trimming_start_x;
u16 trimming_start_y;
bool is_external_light_filter_enabled;
INSERT_PADDING_BYTES(3);
};
static_assert(sizeof(ImageTransferProcessorExConfig) == 0x28,
"ImageTransferProcessorExConfig is an invalid size");
void OnControllerUpdate(Core::HID::ControllerTriggerType type);
ImageTransferProcessorExConfig current_config{};
Core::IrSensor::ImageTransferProcessorState processor_state{};
Core::IrSensor::DeviceFormat& device;
Core::HID::EmulatedController* npad_device;
int callback_key{};
Core::System& system;
Common::ProcessAddress transfer_memory{};
};
} // namespace Service::IRS

View File

@@ -0,0 +1,27 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/irsensor/ir_led_processor.h"
namespace Service::IRS {
IrLedProcessor::IrLedProcessor(Core::IrSensor::DeviceFormat& device_format)
: device(device_format) {
device.mode = Core::IrSensor::IrSensorMode::IrLedProcessor;
device.camera_status = Core::IrSensor::IrCameraStatus::Unconnected;
device.camera_internal_status = Core::IrSensor::IrCameraInternalStatus::Stopped;
}
IrLedProcessor::~IrLedProcessor() = default;
void IrLedProcessor::StartProcessor() {}
void IrLedProcessor::SuspendProcessor() {}
void IrLedProcessor::StopProcessor() {}
void IrLedProcessor::SetConfig(Core::IrSensor::PackedIrLedProcessorConfig config) {
current_config.light_target =
static_cast<Core::IrSensor::CameraLightTarget>(config.light_target);
}
} // namespace Service::IRS

View File

@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/bit_field.h"
#include "common/common_types.h"
#include "hid_core/irsensor/irs_types.h"
#include "hid_core/irsensor/processor_base.h"
namespace Service::IRS {
class IrLedProcessor final : public ProcessorBase {
public:
explicit IrLedProcessor(Core::IrSensor::DeviceFormat& device_format);
~IrLedProcessor() override;
// Called when the processor is initialized
void StartProcessor() override;
// Called when the processor is suspended
void SuspendProcessor() override;
// Called when the processor is stopped
void StopProcessor() override;
// Sets config parameters of the camera
void SetConfig(Core::IrSensor::PackedIrLedProcessorConfig config);
private:
// This is nn::irsensor::IrLedProcessorConfig
struct IrLedProcessorConfig {
Core::IrSensor::CameraLightTarget light_target;
};
static_assert(sizeof(IrLedProcessorConfig) == 0x4, "IrLedProcessorConfig is an invalid size");
struct IrLedProcessorState {
s64 sampling_number;
u64 timestamp;
std::array<u8, 0x8> data;
};
static_assert(sizeof(IrLedProcessorState) == 0x18, "IrLedProcessorState is an invalid size");
IrLedProcessorConfig current_config{};
Core::IrSensor::DeviceFormat& device;
};
} // namespace Service::IRS

301
src/hid_core/irsensor/irs_types.h Executable file
View File

@@ -0,0 +1,301 @@
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "hid_core/hid_types.h"
namespace Core::IrSensor {
// This is nn::irsensor::CameraAmbientNoiseLevel
enum class CameraAmbientNoiseLevel : u32 {
Low,
Medium,
High,
Unknown3, // This level can't be reached
};
// This is nn::irsensor::CameraLightTarget
enum class CameraLightTarget : u32 {
AllLeds,
BrightLeds,
DimLeds,
None,
};
// This is nn::irsensor::PackedCameraLightTarget
enum class PackedCameraLightTarget : u8 {
AllLeds,
BrightLeds,
DimLeds,
None,
};
// This is nn::irsensor::AdaptiveClusteringMode
enum class AdaptiveClusteringMode : u32 {
StaticFov,
DynamicFov,
};
// This is nn::irsensor::AdaptiveClusteringTargetDistance
enum class AdaptiveClusteringTargetDistance : u32 {
Near,
Middle,
Far,
};
// This is nn::irsensor::ImageTransferProcessorFormat
enum class ImageTransferProcessorFormat : u32 {
Size320x240,
Size160x120,
Size80x60,
Size40x30,
Size20x15,
};
// This is nn::irsensor::PackedImageTransferProcessorFormat
enum class PackedImageTransferProcessorFormat : u8 {
Size320x240,
Size160x120,
Size80x60,
Size40x30,
Size20x15,
};
// This is nn::irsensor::IrCameraStatus
enum class IrCameraStatus : u32 {
Available,
Unsupported,
Unconnected,
};
// This is nn::irsensor::IrCameraInternalStatus
enum class IrCameraInternalStatus : u32 {
Stopped,
FirmwareUpdateNeeded,
Unknown2,
Unknown3,
Unknown4,
FirmwareVersionRequested,
FirmwareVersionIsInvalid,
Ready,
Setting,
};
// This is nn::irsensor::detail::StatusManager::IrSensorMode
enum class IrSensorMode : u64 {
None,
MomentProcessor,
ClusteringProcessor,
ImageTransferProcessor,
PointingProcessorMarker,
TeraPluginProcessor,
IrLedProcessor,
};
// This is nn::irsensor::ImageProcessorStatus
enum ImageProcessorStatus : u32 {
Stopped,
Running,
};
// This is nn::irsensor::HandAnalysisMode
enum class HandAnalysisMode : u32 {
None,
Silhouette,
Image,
SilhouetteAndImage,
SilhouetteOnly,
};
// This is nn::irsensor::IrSensorFunctionLevel
enum class IrSensorFunctionLevel : u8 {
unknown0,
unknown1,
unknown2,
unknown3,
unknown4,
};
// This is nn::irsensor::MomentProcessorPreprocess
enum class MomentProcessorPreprocess : u32 {
Unknown0,
Unknown1,
};
// This is nn::irsensor::PackedMomentProcessorPreprocess
enum class PackedMomentProcessorPreprocess : u8 {
Unknown0,
Unknown1,
};
// This is nn::irsensor::PointingStatus
enum class PointingStatus : u32 {
Unknown0,
Unknown1,
};
struct IrsRect {
s16 x;
s16 y;
s16 width;
s16 height;
};
struct IrsCentroid {
f32 x;
f32 y;
};
struct CameraConfig {
u64 exposure_time;
CameraLightTarget light_target;
u32 gain;
bool is_negative_used;
INSERT_PADDING_BYTES(7);
};
static_assert(sizeof(CameraConfig) == 0x18, "CameraConfig is an invalid size");
struct PackedCameraConfig {
u64 exposure_time;
PackedCameraLightTarget light_target;
u8 gain;
bool is_negative_used;
INSERT_PADDING_BYTES(5);
};
static_assert(sizeof(PackedCameraConfig) == 0x10, "PackedCameraConfig is an invalid size");
// This is nn::irsensor::IrCameraHandle
struct IrCameraHandle {
u8 npad_id{};
Core::HID::NpadStyleIndex npad_type{Core::HID::NpadStyleIndex::None};
INSERT_PADDING_BYTES(2);
};
static_assert(sizeof(IrCameraHandle) == 4, "IrCameraHandle is an invalid size");
// This is nn::irsensor::PackedMcuVersion
struct PackedMcuVersion {
u16 major;
u16 minor;
};
static_assert(sizeof(PackedMcuVersion) == 4, "PackedMcuVersion is an invalid size");
// This is nn::irsensor::PackedMomentProcessorConfig
struct PackedMomentProcessorConfig {
PackedCameraConfig camera_config;
IrsRect window_of_interest;
PackedMcuVersion required_mcu_version;
PackedMomentProcessorPreprocess preprocess;
u8 preprocess_intensity_threshold;
INSERT_PADDING_BYTES(2);
};
static_assert(sizeof(PackedMomentProcessorConfig) == 0x20,
"PackedMomentProcessorConfig is an invalid size");
// This is nn::irsensor::PackedClusteringProcessorConfig
struct PackedClusteringProcessorConfig {
PackedCameraConfig camera_config;
IrsRect window_of_interest;
PackedMcuVersion required_mcu_version;
u32 pixel_count_min;
u32 pixel_count_max;
u8 object_intensity_min;
bool is_external_light_filter_enabled;
INSERT_PADDING_BYTES(2);
};
static_assert(sizeof(PackedClusteringProcessorConfig) == 0x28,
"PackedClusteringProcessorConfig is an invalid size");
// This is nn::irsensor::PackedImageTransferProcessorConfig
struct PackedImageTransferProcessorConfig {
PackedCameraConfig camera_config;
PackedMcuVersion required_mcu_version;
PackedImageTransferProcessorFormat format;
INSERT_PADDING_BYTES(3);
};
static_assert(sizeof(PackedImageTransferProcessorConfig) == 0x18,
"PackedImageTransferProcessorConfig is an invalid size");
// This is nn::irsensor::PackedTeraPluginProcessorConfig
struct PackedTeraPluginProcessorConfig {
PackedMcuVersion required_mcu_version;
u8 mode;
u8 unknown_1;
u8 unknown_2;
u8 unknown_3;
};
static_assert(sizeof(PackedTeraPluginProcessorConfig) == 0x8,
"PackedTeraPluginProcessorConfig is an invalid size");
// This is nn::irsensor::PackedPointingProcessorConfig
struct PackedPointingProcessorConfig {
IrsRect window_of_interest;
PackedMcuVersion required_mcu_version;
};
static_assert(sizeof(PackedPointingProcessorConfig) == 0xC,
"PackedPointingProcessorConfig is an invalid size");
// This is nn::irsensor::PackedFunctionLevel
struct PackedFunctionLevel {
IrSensorFunctionLevel function_level;
INSERT_PADDING_BYTES(3);
};
static_assert(sizeof(PackedFunctionLevel) == 0x4, "PackedFunctionLevel is an invalid size");
// This is nn::irsensor::PackedImageTransferProcessorExConfig
struct PackedImageTransferProcessorExConfig {
PackedCameraConfig camera_config;
PackedMcuVersion required_mcu_version;
PackedImageTransferProcessorFormat origin_format;
PackedImageTransferProcessorFormat trimming_format;
u16 trimming_start_x;
u16 trimming_start_y;
bool is_external_light_filter_enabled;
INSERT_PADDING_BYTES(5);
};
static_assert(sizeof(PackedImageTransferProcessorExConfig) == 0x20,
"PackedImageTransferProcessorExConfig is an invalid size");
// This is nn::irsensor::PackedIrLedProcessorConfig
struct PackedIrLedProcessorConfig {
PackedMcuVersion required_mcu_version;
u8 light_target;
INSERT_PADDING_BYTES(3);
};
static_assert(sizeof(PackedIrLedProcessorConfig) == 0x8,
"PackedIrLedProcessorConfig is an invalid size");
// This is nn::irsensor::HandAnalysisConfig
struct HandAnalysisConfig {
HandAnalysisMode mode;
};
static_assert(sizeof(HandAnalysisConfig) == 0x4, "HandAnalysisConfig is an invalid size");
// This is nn::irsensor::detail::ProcessorState contents are different for each processor
struct ProcessorState {
std::array<u8, 0xE20> processor_raw_data{};
};
static_assert(sizeof(ProcessorState) == 0xE20, "ProcessorState is an invalid size");
// This is nn::irsensor::detail::DeviceFormat
struct DeviceFormat {
Core::IrSensor::IrCameraStatus camera_status{Core::IrSensor::IrCameraStatus::Unconnected};
Core::IrSensor::IrCameraInternalStatus camera_internal_status{
Core::IrSensor::IrCameraInternalStatus::Ready};
Core::IrSensor::IrSensorMode mode{Core::IrSensor::IrSensorMode::None};
ProcessorState state{};
};
static_assert(sizeof(DeviceFormat) == 0xE30, "DeviceFormat is an invalid size");
// This is nn::irsensor::ImageTransferProcessorState
struct ImageTransferProcessorState {
u64 sampling_number;
Core::IrSensor::CameraAmbientNoiseLevel ambient_noise_level;
INSERT_PADDING_BYTES(4);
};
static_assert(sizeof(ImageTransferProcessorState) == 0x10,
"ImageTransferProcessorState is an invalid size");
} // namespace Core::IrSensor

View File

@@ -0,0 +1,149 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/core.h"
#include "core/core_timing.h"
#include "hid_core/frontend/emulated_controller.h"
#include "hid_core/hid_core.h"
#include "hid_core/irsensor/moment_processor.h"
namespace Service::IRS {
static constexpr auto format = Core::IrSensor::ImageTransferProcessorFormat::Size40x30;
static constexpr std::size_t ImageWidth = 40;
static constexpr std::size_t ImageHeight = 30;
MomentProcessor::MomentProcessor(Core::System& system_, Core::IrSensor::DeviceFormat& device_format,
std::size_t npad_index)
: device(device_format), system{system_} {
npad_device = system.HIDCore().GetEmulatedControllerByIndex(npad_index);
device.mode = Core::IrSensor::IrSensorMode::MomentProcessor;
device.camera_status = Core::IrSensor::IrCameraStatus::Unconnected;
device.camera_internal_status = Core::IrSensor::IrCameraInternalStatus::Stopped;
shared_memory = std::construct_at(
reinterpret_cast<MomentSharedMemory*>(&device_format.state.processor_raw_data));
Core::HID::ControllerUpdateCallback engine_callback{
.on_change = [this](Core::HID::ControllerTriggerType type) { OnControllerUpdate(type); },
.is_npad_service = true,
};
callback_key = npad_device->SetCallback(engine_callback);
}
MomentProcessor::~MomentProcessor() {
npad_device->DeleteCallback(callback_key);
};
void MomentProcessor::StartProcessor() {
device.camera_status = Core::IrSensor::IrCameraStatus::Available;
device.camera_internal_status = Core::IrSensor::IrCameraInternalStatus::Ready;
}
void MomentProcessor::SuspendProcessor() {}
void MomentProcessor::StopProcessor() {}
void MomentProcessor::OnControllerUpdate(Core::HID::ControllerTriggerType type) {
if (type != Core::HID::ControllerTriggerType::IrSensor) {
return;
}
next_state = {};
const auto& camera_data = npad_device->GetCamera();
const auto window_width = static_cast<std::size_t>(current_config.window_of_interest.width);
const auto window_height = static_cast<std::size_t>(current_config.window_of_interest.height);
const auto window_start_x = static_cast<std::size_t>(current_config.window_of_interest.x);
const auto window_start_y = static_cast<std::size_t>(current_config.window_of_interest.y);
const std::size_t block_width = window_width / Columns;
const std::size_t block_height = window_height / Rows;
for (std::size_t row = 0; row < Rows; row++) {
for (std::size_t column = 0; column < Columns; column++) {
const size_t x_pos = (column * block_width) + window_start_x;
const size_t y_pos = (row * block_height) + window_start_y;
auto& statistic = next_state.statistic[column + (row * Columns)];
statistic = GetStatistic(camera_data.data, x_pos, y_pos, block_width, block_height);
}
}
next_state.sampling_number = camera_data.sample;
next_state.timestamp = system.CoreTiming().GetGlobalTimeNs().count();
next_state.ambient_noise_level = Core::IrSensor::CameraAmbientNoiseLevel::Low;
shared_memory->moment_lifo.WriteNextEntry(next_state);
if (!IsProcessorActive()) {
StartProcessor();
}
}
u8 MomentProcessor::GetPixel(const std::vector<u8>& data, std::size_t x, std::size_t y) const {
if ((y * ImageWidth) + x >= data.size()) {
return 0;
}
return data[(y * ImageWidth) + x];
}
MomentProcessor::MomentStatistic MomentProcessor::GetStatistic(const std::vector<u8>& data,
std::size_t start_x,
std::size_t start_y,
std::size_t width,
std::size_t height) const {
// The actual implementation is always 320x240
static constexpr std::size_t RealWidth = 320;
static constexpr std::size_t RealHeight = 240;
static constexpr std::size_t Threshold = 30;
MomentStatistic statistic{};
std::size_t active_points{};
// Sum all data points on the block that meet with the threshold
for (std::size_t y = 0; y < width; y++) {
for (std::size_t x = 0; x < height; x++) {
const size_t x_pos = x + start_x;
const size_t y_pos = y + start_y;
const auto pixel =
GetPixel(data, x_pos * ImageWidth / RealWidth, y_pos * ImageHeight / RealHeight);
if (pixel < Threshold) {
continue;
}
statistic.average_intensity += pixel;
statistic.centroid.x += static_cast<float>(x_pos);
statistic.centroid.y += static_cast<float>(y_pos);
active_points++;
}
}
// Return an empty field if no points were available
if (active_points == 0) {
return {};
}
// Finally calculate the actual centroid and average intensity
statistic.centroid.x /= static_cast<float>(active_points);
statistic.centroid.y /= static_cast<float>(active_points);
statistic.average_intensity /= static_cast<f32>(width * height);
return statistic;
}
void MomentProcessor::SetConfig(Core::IrSensor::PackedMomentProcessorConfig config) {
current_config.camera_config.exposure_time = config.camera_config.exposure_time;
current_config.camera_config.gain = config.camera_config.gain;
current_config.camera_config.is_negative_used = config.camera_config.is_negative_used;
current_config.camera_config.light_target =
static_cast<Core::IrSensor::CameraLightTarget>(config.camera_config.light_target);
current_config.window_of_interest = config.window_of_interest;
current_config.preprocess =
static_cast<Core::IrSensor::MomentProcessorPreprocess>(config.preprocess);
current_config.preprocess_intensity_threshold = config.preprocess_intensity_threshold;
npad_device->SetCameraFormat(format);
}
} // namespace Service::IRS

View File

@@ -0,0 +1,91 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/bit_field.h"
#include "common/common_types.h"
#include "hid_core/irsensor/irs_types.h"
#include "hid_core/irsensor/processor_base.h"
#include "hid_core/resources/irs_ring_lifo.h"
namespace Core {
class System;
}
namespace Core::HID {
class EmulatedController;
} // namespace Core::HID
namespace Service::IRS {
class MomentProcessor final : public ProcessorBase {
public:
explicit MomentProcessor(Core::System& system_, Core::IrSensor::DeviceFormat& device_format,
std::size_t npad_index);
~MomentProcessor() override;
// Called when the processor is initialized
void StartProcessor() override;
// Called when the processor is suspended
void SuspendProcessor() override;
// Called when the processor is stopped
void StopProcessor() override;
// Sets config parameters of the camera
void SetConfig(Core::IrSensor::PackedMomentProcessorConfig config);
private:
static constexpr std::size_t Columns = 8;
static constexpr std::size_t Rows = 6;
// This is nn::irsensor::MomentProcessorConfig
struct MomentProcessorConfig {
Core::IrSensor::CameraConfig camera_config;
Core::IrSensor::IrsRect window_of_interest;
Core::IrSensor::MomentProcessorPreprocess preprocess;
u32 preprocess_intensity_threshold;
};
static_assert(sizeof(MomentProcessorConfig) == 0x28,
"MomentProcessorConfig is an invalid size");
// This is nn::irsensor::MomentStatistic
struct MomentStatistic {
f32 average_intensity;
Core::IrSensor::IrsCentroid centroid;
};
static_assert(sizeof(MomentStatistic) == 0xC, "MomentStatistic is an invalid size");
// This is nn::irsensor::MomentProcessorState
struct MomentProcessorState {
s64 sampling_number;
u64 timestamp;
Core::IrSensor::CameraAmbientNoiseLevel ambient_noise_level;
INSERT_PADDING_BYTES(4);
std::array<MomentStatistic, Columns * Rows> statistic;
};
static_assert(sizeof(MomentProcessorState) == 0x258, "MomentProcessorState is an invalid size");
struct MomentSharedMemory {
Service::IRS::Lifo<MomentProcessorState, 6> moment_lifo;
};
static_assert(sizeof(MomentSharedMemory) == 0xE20, "MomentSharedMemory is an invalid size");
void OnControllerUpdate(Core::HID::ControllerTriggerType type);
u8 GetPixel(const std::vector<u8>& data, std::size_t x, std::size_t y) const;
MomentStatistic GetStatistic(const std::vector<u8>& data, std::size_t start_x,
std::size_t start_y, std::size_t width, std::size_t height) const;
MomentSharedMemory* shared_memory = nullptr;
MomentProcessorState next_state{};
MomentProcessorConfig current_config{};
Core::IrSensor::DeviceFormat& device;
Core::HID::EmulatedController* npad_device;
int callback_key{};
Core::System& system;
};
} // namespace Service::IRS

View File

@@ -0,0 +1,26 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/irsensor/pointing_processor.h"
namespace Service::IRS {
PointingProcessor::PointingProcessor(Core::IrSensor::DeviceFormat& device_format)
: device(device_format) {
device.mode = Core::IrSensor::IrSensorMode::PointingProcessorMarker;
device.camera_status = Core::IrSensor::IrCameraStatus::Unconnected;
device.camera_internal_status = Core::IrSensor::IrCameraInternalStatus::Stopped;
}
PointingProcessor::~PointingProcessor() = default;
void PointingProcessor::StartProcessor() {}
void PointingProcessor::SuspendProcessor() {}
void PointingProcessor::StopProcessor() {}
void PointingProcessor::SetConfig(Core::IrSensor::PackedPointingProcessorConfig config) {
current_config.window_of_interest = config.window_of_interest;
}
} // namespace Service::IRS

View File

@@ -0,0 +1,61 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "hid_core/irsensor/irs_types.h"
#include "hid_core/irsensor/processor_base.h"
namespace Service::IRS {
class PointingProcessor final : public ProcessorBase {
public:
explicit PointingProcessor(Core::IrSensor::DeviceFormat& device_format);
~PointingProcessor() override;
// Called when the processor is initialized
void StartProcessor() override;
// Called when the processor is suspended
void SuspendProcessor() override;
// Called when the processor is stopped
void StopProcessor() override;
// Sets config parameters of the camera
void SetConfig(Core::IrSensor::PackedPointingProcessorConfig config);
private:
// This is nn::irsensor::PointingProcessorConfig
struct PointingProcessorConfig {
Core::IrSensor::IrsRect window_of_interest;
};
static_assert(sizeof(PointingProcessorConfig) == 0x8,
"PointingProcessorConfig is an invalid size");
struct PointingProcessorMarkerData {
u8 pointing_status;
INSERT_PADDING_BYTES(3);
u32 unknown;
float unknown_float1;
float position_x;
float position_y;
float unknown_float2;
Core::IrSensor::IrsRect window_of_interest;
};
static_assert(sizeof(PointingProcessorMarkerData) == 0x20,
"PointingProcessorMarkerData is an invalid size");
struct PointingProcessorMarkerState {
s64 sampling_number;
u64 timestamp;
std::array<PointingProcessorMarkerData, 0x3> data;
};
static_assert(sizeof(PointingProcessorMarkerState) == 0x70,
"PointingProcessorMarkerState is an invalid size");
PointingProcessorConfig current_config{};
Core::IrSensor::DeviceFormat& device;
};
} // namespace Service::IRS

View File

@@ -0,0 +1,67 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/irsensor/processor_base.h"
namespace Service::IRS {
ProcessorBase::ProcessorBase() {}
ProcessorBase::~ProcessorBase() = default;
bool ProcessorBase::IsProcessorActive() const {
return is_active;
}
std::size_t ProcessorBase::GetDataSize(Core::IrSensor::ImageTransferProcessorFormat format) const {
switch (format) {
case Core::IrSensor::ImageTransferProcessorFormat::Size320x240:
return 320 * 240;
case Core::IrSensor::ImageTransferProcessorFormat::Size160x120:
return 160 * 120;
case Core::IrSensor::ImageTransferProcessorFormat::Size80x60:
return 80 * 60;
case Core::IrSensor::ImageTransferProcessorFormat::Size40x30:
return 40 * 30;
case Core::IrSensor::ImageTransferProcessorFormat::Size20x15:
return 20 * 15;
default:
return 0;
}
}
std::size_t ProcessorBase::GetDataWidth(Core::IrSensor::ImageTransferProcessorFormat format) const {
switch (format) {
case Core::IrSensor::ImageTransferProcessorFormat::Size320x240:
return 320;
case Core::IrSensor::ImageTransferProcessorFormat::Size160x120:
return 160;
case Core::IrSensor::ImageTransferProcessorFormat::Size80x60:
return 80;
case Core::IrSensor::ImageTransferProcessorFormat::Size40x30:
return 40;
case Core::IrSensor::ImageTransferProcessorFormat::Size20x15:
return 20;
default:
return 0;
}
}
std::size_t ProcessorBase::GetDataHeight(
Core::IrSensor::ImageTransferProcessorFormat format) const {
switch (format) {
case Core::IrSensor::ImageTransferProcessorFormat::Size320x240:
return 240;
case Core::IrSensor::ImageTransferProcessorFormat::Size160x120:
return 120;
case Core::IrSensor::ImageTransferProcessorFormat::Size80x60:
return 60;
case Core::IrSensor::ImageTransferProcessorFormat::Size40x30:
return 30;
case Core::IrSensor::ImageTransferProcessorFormat::Size20x15:
return 15;
default:
return 0;
}
}
} // namespace Service::IRS

View File

@@ -0,0 +1,33 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "hid_core/irsensor/irs_types.h"
namespace Service::IRS {
class ProcessorBase {
public:
explicit ProcessorBase();
virtual ~ProcessorBase();
virtual void StartProcessor() = 0;
virtual void SuspendProcessor() = 0;
virtual void StopProcessor() = 0;
bool IsProcessorActive() const;
protected:
/// Returns the number of bytes the image uses
std::size_t GetDataSize(Core::IrSensor::ImageTransferProcessorFormat format) const;
/// Returns the width of the image
std::size_t GetDataWidth(Core::IrSensor::ImageTransferProcessorFormat format) const;
/// Returns the height of the image
std::size_t GetDataHeight(Core::IrSensor::ImageTransferProcessorFormat format) const;
bool is_active{false};
};
} // namespace Service::IRS

View File

@@ -0,0 +1,29 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/irsensor/tera_plugin_processor.h"
namespace Service::IRS {
TeraPluginProcessor::TeraPluginProcessor(Core::IrSensor::DeviceFormat& device_format)
: device(device_format) {
device.mode = Core::IrSensor::IrSensorMode::TeraPluginProcessor;
device.camera_status = Core::IrSensor::IrCameraStatus::Unconnected;
device.camera_internal_status = Core::IrSensor::IrCameraInternalStatus::Stopped;
}
TeraPluginProcessor::~TeraPluginProcessor() = default;
void TeraPluginProcessor::StartProcessor() {}
void TeraPluginProcessor::SuspendProcessor() {}
void TeraPluginProcessor::StopProcessor() {}
void TeraPluginProcessor::SetConfig(Core::IrSensor::PackedTeraPluginProcessorConfig config) {
current_config.mode = config.mode;
current_config.unknown_1 = config.unknown_1;
current_config.unknown_2 = config.unknown_2;
current_config.unknown_3 = config.unknown_3;
}
} // namespace Service::IRS

View File

@@ -0,0 +1,53 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/bit_field.h"
#include "common/common_types.h"
#include "hid_core/irsensor/irs_types.h"
#include "hid_core/irsensor/processor_base.h"
namespace Service::IRS {
class TeraPluginProcessor final : public ProcessorBase {
public:
explicit TeraPluginProcessor(Core::IrSensor::DeviceFormat& device_format);
~TeraPluginProcessor() override;
// Called when the processor is initialized
void StartProcessor() override;
// Called when the processor is suspended
void SuspendProcessor() override;
// Called when the processor is stopped
void StopProcessor() override;
// Sets config parameters of the camera
void SetConfig(Core::IrSensor::PackedTeraPluginProcessorConfig config);
private:
// This is nn::irsensor::TeraPluginProcessorConfig
struct TeraPluginProcessorConfig {
u8 mode;
u8 unknown_1;
u8 unknown_2;
u8 unknown_3;
};
static_assert(sizeof(TeraPluginProcessorConfig) == 0x4,
"TeraPluginProcessorConfig is an invalid size");
struct TeraPluginProcessorState {
s64 sampling_number;
u64 timestamp;
Core::IrSensor::CameraAmbientNoiseLevel ambient_noise_level;
std::array<u8, 0x12c> data;
};
static_assert(sizeof(TeraPluginProcessorState) == 0x140,
"TeraPluginProcessorState is an invalid size");
TeraPluginProcessorConfig current_config{};
Core::IrSensor::DeviceFormat& device;
};
} // namespace Service::IRS

View File

@@ -0,0 +1,6 @@
// SPDX-FileCopyrightText: 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "common/common_precompiled_headers.h"

503
src/hid_core/resource_manager.cpp Executable file
View File

@@ -0,0 +1,503 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "common/logging/log.h"
#include "core/core.h"
#include "core/core_timing.h"
#include "core/hle/service/ipc_helpers.h"
#include "core/hle/service/set/system_settings_server.h"
#include "core/hle/service/sm/sm.h"
#include "hid_core/hid_core.h"
#include "hid_core/hid_util.h"
#include "hid_core/resource_manager.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/debug_pad/debug_pad.h"
#include "hid_core/resources/digitizer/digitizer.h"
#include "hid_core/resources/hid_firmware_settings.h"
#include "hid_core/resources/keyboard/keyboard.h"
#include "hid_core/resources/mouse/debug_mouse.h"
#include "hid_core/resources/mouse/mouse.h"
#include "hid_core/resources/npad/npad.h"
#include "hid_core/resources/palma/palma.h"
#include "hid_core/resources/shared_memory_format.h"
#include "hid_core/resources/six_axis/console_six_axis.h"
#include "hid_core/resources/six_axis/seven_six_axis.h"
#include "hid_core/resources/six_axis/six_axis.h"
#include "hid_core/resources/system_buttons/capture_button.h"
#include "hid_core/resources/system_buttons/home_button.h"
#include "hid_core/resources/system_buttons/sleep_button.h"
#include "hid_core/resources/touch_screen/gesture.h"
#include "hid_core/resources/touch_screen/touch_screen.h"
#include "hid_core/resources/touch_screen/touch_screen_driver.h"
#include "hid_core/resources/touch_screen/touch_screen_resource.h"
#include "hid_core/resources/unique_pad/unique_pad.h"
#include "hid_core/resources/vibration/gc_vibration_device.h"
#include "hid_core/resources/vibration/n64_vibration_device.h"
#include "hid_core/resources/vibration/vibration_base.h"
#include "hid_core/resources/vibration/vibration_device.h"
namespace Service::HID {
// Updating period for each HID device.
// Period time is obtained by measuring the number of samples in a second on HW using a homebrew
// Correct npad_update_ns is 4ms this is overclocked to lower input lag
constexpr auto npad_update_ns = std::chrono::nanoseconds{1 * 1000 * 1000}; // (1ms, 1000Hz)
constexpr auto default_update_ns = std::chrono::nanoseconds{4 * 1000 * 1000}; // (4ms, 1000Hz)
constexpr auto mouse_keyboard_update_ns = std::chrono::nanoseconds{8 * 1000 * 1000}; // (8ms, 125Hz)
constexpr auto motion_update_ns = std::chrono::nanoseconds{5 * 1000 * 1000}; // (5ms, 200Hz)
ResourceManager::ResourceManager(Core::System& system_,
std::shared_ptr<HidFirmwareSettings> settings)
: firmware_settings{settings}, system{system_}, service_context{system_, "hid"} {
applet_resource = std::make_shared<AppletResource>(system);
// Register update callbacks
npad_update_event = Core::Timing::CreateEvent("HID::UpdatePadCallback",
[this](s64 time, std::chrono::nanoseconds ns_late)
-> std::optional<std::chrono::nanoseconds> {
UpdateNpad(ns_late);
return std::nullopt;
});
default_update_event = Core::Timing::CreateEvent(
"HID::UpdateDefaultCallback",
[this](s64 time,
std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
UpdateControllers(ns_late);
return std::nullopt;
});
mouse_keyboard_update_event = Core::Timing::CreateEvent(
"HID::UpdateMouseKeyboardCallback",
[this](s64 time,
std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
UpdateMouseKeyboard(ns_late);
return std::nullopt;
});
motion_update_event = Core::Timing::CreateEvent(
"HID::UpdateMotionCallback",
[this](s64 time,
std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
UpdateMotion(ns_late);
return std::nullopt;
});
}
ResourceManager::~ResourceManager() {
system.CoreTiming().UnscheduleEvent(npad_update_event);
system.CoreTiming().UnscheduleEvent(default_update_event);
system.CoreTiming().UnscheduleEvent(mouse_keyboard_update_event);
system.CoreTiming().UnscheduleEvent(motion_update_event);
system.CoreTiming().UnscheduleEvent(touch_update_event);
input_event->Finalize();
};
void ResourceManager::Initialize() {
if (is_initialized) {
return;
}
system.HIDCore().ReloadInputDevices();
input_event = service_context.CreateEvent("ResourceManager:InputEvent");
InitializeHandheldConfig();
InitializeHidCommonSampler();
InitializeTouchScreenSampler();
InitializeConsoleSixAxisSampler();
InitializeAHidSampler();
is_initialized = true;
}
std::shared_ptr<AppletResource> ResourceManager::GetAppletResource() const {
return applet_resource;
}
std::shared_ptr<CaptureButton> ResourceManager::GetCaptureButton() const {
return capture_button;
}
std::shared_ptr<ConsoleSixAxis> ResourceManager::GetConsoleSixAxis() const {
return console_six_axis;
}
std::shared_ptr<DebugMouse> ResourceManager::GetDebugMouse() const {
return debug_mouse;
}
std::shared_ptr<DebugPad> ResourceManager::GetDebugPad() const {
return debug_pad;
}
std::shared_ptr<Digitizer> ResourceManager::GetDigitizer() const {
return digitizer;
}
std::shared_ptr<Gesture> ResourceManager::GetGesture() const {
return gesture;
}
std::shared_ptr<HomeButton> ResourceManager::GetHomeButton() const {
return home_button;
}
std::shared_ptr<Keyboard> ResourceManager::GetKeyboard() const {
return keyboard;
}
std::shared_ptr<Mouse> ResourceManager::GetMouse() const {
return mouse;
}
std::shared_ptr<NPad> ResourceManager::GetNpad() const {
return npad;
}
std::shared_ptr<Palma> ResourceManager::GetPalma() const {
return palma;
}
std::shared_ptr<SevenSixAxis> ResourceManager::GetSevenSixAxis() const {
return seven_six_axis;
}
std::shared_ptr<SixAxis> ResourceManager::GetSixAxis() const {
return six_axis;
}
std::shared_ptr<SleepButton> ResourceManager::GetSleepButton() const {
return sleep_button;
}
std::shared_ptr<TouchScreen> ResourceManager::GetTouchScreen() const {
return touch_screen;
}
std::shared_ptr<UniquePad> ResourceManager::GetUniquePad() const {
return unique_pad;
}
Result ResourceManager::CreateAppletResource(u64 aruid) {
if (aruid == SystemAruid) {
const auto result = RegisterCoreAppletResource();
if (result.IsError()) {
return result;
}
return GetNpad()->ActivateNpadResource();
}
const auto result = CreateAppletResourceImpl(aruid);
if (result.IsError()) {
return result;
}
// Homebrew doesn't try to activate some controllers, so we activate them by default
npad->Activate();
six_axis->Activate();
touch_screen->Activate();
gesture->Activate();
return GetNpad()->ActivateNpadResource(aruid);
}
Result ResourceManager::CreateAppletResourceImpl(u64 aruid) {
std::scoped_lock lock{shared_mutex};
return applet_resource->CreateAppletResource(aruid);
}
void ResourceManager::InitializeHandheldConfig() {
handheld_config = std::make_shared<HandheldConfig>();
handheld_config->is_handheld_hid_enabled = true;
handheld_config->is_joycon_rail_enabled = true;
handheld_config->is_force_handheld_style_vibration = false;
handheld_config->is_force_handheld = false;
if (firmware_settings->IsHandheldForced()) {
handheld_config->is_joycon_rail_enabled = false;
}
}
void ResourceManager::InitializeHidCommonSampler() {
debug_pad = std::make_shared<DebugPad>(system.HIDCore());
mouse = std::make_shared<Mouse>(system.HIDCore());
debug_mouse = std::make_shared<DebugMouse>(system.HIDCore());
keyboard = std::make_shared<Keyboard>(system.HIDCore());
unique_pad = std::make_shared<UniquePad>(system.HIDCore());
npad = std::make_shared<NPad>(system.HIDCore(), service_context);
home_button = std::make_shared<HomeButton>(system.HIDCore());
sleep_button = std::make_shared<SleepButton>(system.HIDCore());
capture_button = std::make_shared<CaptureButton>(system.HIDCore());
digitizer = std::make_shared<Digitizer>(system.HIDCore());
palma = std::make_shared<Palma>(system.HIDCore(), service_context);
six_axis = std::make_shared<SixAxis>(system.HIDCore(), npad);
debug_pad->SetAppletResource(applet_resource, &shared_mutex);
digitizer->SetAppletResource(applet_resource, &shared_mutex);
unique_pad->SetAppletResource(applet_resource, &shared_mutex);
keyboard->SetAppletResource(applet_resource, &shared_mutex);
const auto settings =
system.ServiceManager().GetService<Service::Set::ISystemSettingsServer>("set:sys", true);
npad->SetNpadExternals(applet_resource, &shared_mutex, handheld_config, input_event,
&input_mutex, settings);
six_axis->SetAppletResource(applet_resource, &shared_mutex);
mouse->SetAppletResource(applet_resource, &shared_mutex);
debug_mouse->SetAppletResource(applet_resource, &shared_mutex);
home_button->SetAppletResource(applet_resource, &shared_mutex);
sleep_button->SetAppletResource(applet_resource, &shared_mutex);
capture_button->SetAppletResource(applet_resource, &shared_mutex);
system.CoreTiming().ScheduleLoopingEvent(npad_update_ns, npad_update_ns, npad_update_event);
system.CoreTiming().ScheduleLoopingEvent(default_update_ns, default_update_ns,
default_update_event);
system.CoreTiming().ScheduleLoopingEvent(mouse_keyboard_update_ns, mouse_keyboard_update_ns,
mouse_keyboard_update_event);
system.CoreTiming().ScheduleLoopingEvent(motion_update_ns, motion_update_ns,
motion_update_event);
}
void ResourceManager::InitializeTouchScreenSampler() {
// This is nn.hid.TouchScreenSampler
touch_resource = std::make_shared<TouchResource>(system);
touch_driver = std::make_shared<TouchDriver>(system.HIDCore());
touch_screen = std::make_shared<TouchScreen>(touch_resource);
gesture = std::make_shared<Gesture>(touch_resource);
touch_update_event = Core::Timing::CreateEvent(
"HID::TouchUpdateCallback",
[this](s64 time,
std::chrono::nanoseconds ns_late) -> std::optional<std::chrono::nanoseconds> {
touch_resource->OnTouchUpdate(time);
return std::nullopt;
});
touch_resource->SetTouchDriver(touch_driver);
touch_resource->SetAppletResource(applet_resource, &shared_mutex);
touch_resource->SetInputEvent(input_event, &input_mutex);
touch_resource->SetHandheldConfig(handheld_config);
touch_resource->SetTimerEvent(touch_update_event);
}
void ResourceManager::InitializeConsoleSixAxisSampler() {
console_six_axis = std::make_shared<ConsoleSixAxis>(system.HIDCore());
seven_six_axis = std::make_shared<SevenSixAxis>(system);
console_six_axis->SetAppletResource(applet_resource, &shared_mutex);
}
void ResourceManager::InitializeAHidSampler() {
// TODO
}
Result ResourceManager::RegisterCoreAppletResource() {
std::scoped_lock lock{shared_mutex};
return applet_resource->RegisterCoreAppletResource();
}
Result ResourceManager::UnregisterCoreAppletResource() {
std::scoped_lock lock{shared_mutex};
return applet_resource->UnregisterCoreAppletResource();
}
Result ResourceManager::RegisterAppletResourceUserId(u64 aruid, bool bool_value) {
std::scoped_lock lock{shared_mutex};
auto result = applet_resource->RegisterAppletResourceUserId(aruid, bool_value);
if (result.IsSuccess()) {
result = npad->RegisterAppletResourceUserId(aruid);
}
return result;
}
void ResourceManager::UnregisterAppletResourceUserId(u64 aruid) {
std::scoped_lock lock{shared_mutex};
applet_resource->UnregisterAppletResourceUserId(aruid);
npad->UnregisterAppletResourceUserId(aruid);
// palma->UnregisterAppletResourceUserId(aruid);
}
Result ResourceManager::GetSharedMemoryHandle(Kernel::KSharedMemory** out_handle, u64 aruid) {
std::scoped_lock lock{shared_mutex};
return applet_resource->GetSharedMemoryHandle(out_handle, aruid);
}
void ResourceManager::FreeAppletResourceId(u64 aruid) {
std::scoped_lock lock{shared_mutex};
applet_resource->FreeAppletResourceId(aruid);
npad->FreeAppletResourceId(aruid);
}
void ResourceManager::EnableInput(u64 aruid, bool is_enabled) {
std::scoped_lock lock{shared_mutex};
applet_resource->EnableInput(aruid, is_enabled);
}
void ResourceManager::EnableSixAxisSensor(u64 aruid, bool is_enabled) {
std::scoped_lock lock{shared_mutex};
applet_resource->EnableSixAxisSensor(aruid, is_enabled);
}
void ResourceManager::EnablePadInput(u64 aruid, bool is_enabled) {
std::scoped_lock lock{shared_mutex};
applet_resource->EnablePadInput(aruid, is_enabled);
}
void ResourceManager::EnableTouchScreen(u64 aruid, bool is_enabled) {
std::scoped_lock lock{shared_mutex};
applet_resource->EnableTouchScreen(aruid, is_enabled);
}
NpadVibrationBase* ResourceManager::GetVibrationDevice(
const Core::HID::VibrationDeviceHandle& handle) {
return npad->GetVibrationDevice(handle);
}
NpadN64VibrationDevice* ResourceManager::GetN64VibrationDevice(
const Core::HID::VibrationDeviceHandle& handle) {
return npad->GetN64VibrationDevice(handle);
}
NpadVibrationDevice* ResourceManager::GetNSVibrationDevice(
const Core::HID::VibrationDeviceHandle& handle) {
return npad->GetNSVibrationDevice(handle);
}
NpadGcVibrationDevice* ResourceManager::GetGcVibrationDevice(
const Core::HID::VibrationDeviceHandle& handle) {
return npad->GetGcVibrationDevice(handle);
}
Result ResourceManager::SetAruidValidForVibration(u64 aruid, bool is_enabled) {
std::scoped_lock lock{shared_mutex};
const bool has_changed = applet_resource->SetAruidValidForVibration(aruid, is_enabled);
if (has_changed) {
auto devices = npad->GetAllVibrationDevices();
for ([[maybe_unused]] auto* device : devices) {
// TODO
}
}
auto* vibration_handler = npad->GetVibrationHandler();
if (aruid != vibration_handler->GetSessionAruid()) {
vibration_handler->EndPermitVibrationSession();
}
return ResultSuccess;
}
void ResourceManager::SetForceHandheldStyleVibration(bool is_forced) {
handheld_config->is_force_handheld_style_vibration = is_forced;
}
Result ResourceManager::IsVibrationAruidActive(u64 aruid, bool& is_active) const {
std::scoped_lock lock{shared_mutex};
is_active = applet_resource->IsVibrationAruidActive(aruid);
return ResultSuccess;
}
Result ResourceManager::GetVibrationDeviceInfo(Core::HID::VibrationDeviceInfo& device_info,
const Core::HID::VibrationDeviceHandle& handle) {
bool check_device_index = false;
const Result is_valid = IsVibrationHandleValid(handle);
if (is_valid.IsError()) {
return is_valid;
}
switch (handle.npad_type) {
case Core::HID::NpadStyleIndex::Fullkey:
case Core::HID::NpadStyleIndex::Handheld:
case Core::HID::NpadStyleIndex::JoyconDual:
case Core::HID::NpadStyleIndex::JoyconLeft:
case Core::HID::NpadStyleIndex::JoyconRight:
device_info.type = Core::HID::VibrationDeviceType::LinearResonantActuator;
check_device_index = true;
break;
case Core::HID::NpadStyleIndex::GameCube:
device_info.type = Core::HID::VibrationDeviceType::GcErm;
break;
case Core::HID::NpadStyleIndex::N64:
device_info.type = Core::HID::VibrationDeviceType::N64;
break;
default:
device_info.type = Core::HID::VibrationDeviceType::Unknown;
break;
}
device_info.position = Core::HID::VibrationDevicePosition::None;
if (check_device_index) {
switch (handle.device_index) {
case Core::HID::DeviceIndex::Left:
device_info.position = Core::HID::VibrationDevicePosition::Left;
break;
case Core::HID::DeviceIndex::Right:
device_info.position = Core::HID::VibrationDevicePosition::Right;
break;
case Core::HID::DeviceIndex::None:
default:
ASSERT_MSG(false, "DeviceIndex should never be None!");
break;
}
}
return ResultSuccess;
}
Result ResourceManager::SendVibrationValue(u64 aruid,
const Core::HID::VibrationDeviceHandle& handle,
const Core::HID::VibrationValue& value) {
bool has_active_aruid{};
NpadVibrationDevice* device{nullptr};
Result result = IsVibrationAruidActive(aruid, has_active_aruid);
if (result.IsSuccess() && has_active_aruid) {
result = IsVibrationHandleValid(handle);
}
if (result.IsSuccess() && has_active_aruid) {
device = GetNSVibrationDevice(handle);
}
if (device != nullptr) {
// Prevent sending vibrations to an inactive vibration handle
if (!device->IsActive()) {
return ResultSuccess;
}
result = device->SendVibrationValue(value);
}
return result;
}
Result ResourceManager::GetTouchScreenFirmwareVersion(Core::HID::FirmwareVersion& firmware) const {
return ResultSuccess;
}
void ResourceManager::UpdateControllers(std::chrono::nanoseconds ns_late) {
auto& core_timing = system.CoreTiming();
debug_pad->OnUpdate(core_timing);
digitizer->OnUpdate(core_timing);
unique_pad->OnUpdate(core_timing);
palma->OnUpdate(core_timing);
home_button->OnUpdate(core_timing);
sleep_button->OnUpdate(core_timing);
capture_button->OnUpdate(core_timing);
}
void ResourceManager::UpdateNpad(std::chrono::nanoseconds ns_late) {
auto& core_timing = system.CoreTiming();
npad->OnUpdate(core_timing);
}
void ResourceManager::UpdateMouseKeyboard(std::chrono::nanoseconds ns_late) {
auto& core_timing = system.CoreTiming();
mouse->OnUpdate(core_timing);
debug_mouse->OnUpdate(core_timing);
keyboard->OnUpdate(core_timing);
}
void ResourceManager::UpdateMotion(std::chrono::nanoseconds ns_late) {
auto& core_timing = system.CoreTiming();
six_axis->OnUpdate(core_timing);
seven_six_axis->OnUpdate(core_timing);
console_six_axis->OnUpdate(core_timing);
}
} // namespace Service::HID

177
src/hid_core/resource_manager.h Executable file
View File

@@ -0,0 +1,177 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "core/hle/service/kernel_helpers.h"
#include "core/hle/service/service.h"
namespace Core {
class System;
}
namespace Core::HID {
struct FirmwareVersion;
struct VibrationDeviceHandle;
struct VibrationValue;
struct VibrationDeviceInfo;
} // namespace Core::HID
namespace Core::Timing {
struct EventType;
}
namespace Kernel {
class KEvent;
class KSharedMemory;
} // namespace Kernel
namespace Service::HID {
class AppletResource;
class CaptureButton;
class Controller_Stubbed;
class ConsoleSixAxis;
class DebugMouse;
class DebugPad;
class Digitizer;
class Gesture;
class HidFirmwareSettings;
class HomeButton;
class Keyboard;
class Mouse;
class NPad;
class Palma;
class SevenSixAxis;
class SixAxis;
class SleepButton;
class TouchScreen;
class TouchDriver;
class TouchResource;
class UniquePad;
class NpadVibrationBase;
class NpadN64VibrationDevice;
class NpadGcVibrationDevice;
class NpadVibrationDevice;
struct HandheldConfig;
class ResourceManager {
public:
explicit ResourceManager(Core::System& system_, std::shared_ptr<HidFirmwareSettings> settings);
~ResourceManager();
void Initialize();
std::shared_ptr<AppletResource> GetAppletResource() const;
std::shared_ptr<CaptureButton> GetCaptureButton() const;
std::shared_ptr<ConsoleSixAxis> GetConsoleSixAxis() const;
std::shared_ptr<DebugMouse> GetDebugMouse() const;
std::shared_ptr<DebugPad> GetDebugPad() const;
std::shared_ptr<Digitizer> GetDigitizer() const;
std::shared_ptr<Gesture> GetGesture() const;
std::shared_ptr<HomeButton> GetHomeButton() const;
std::shared_ptr<Keyboard> GetKeyboard() const;
std::shared_ptr<Mouse> GetMouse() const;
std::shared_ptr<NPad> GetNpad() const;
std::shared_ptr<Palma> GetPalma() const;
std::shared_ptr<SevenSixAxis> GetSevenSixAxis() const;
std::shared_ptr<SixAxis> GetSixAxis() const;
std::shared_ptr<SleepButton> GetSleepButton() const;
std::shared_ptr<TouchScreen> GetTouchScreen() const;
std::shared_ptr<UniquePad> GetUniquePad() const;
Result CreateAppletResource(u64 aruid);
Result RegisterCoreAppletResource();
Result UnregisterCoreAppletResource();
Result RegisterAppletResourceUserId(u64 aruid, bool bool_value);
void UnregisterAppletResourceUserId(u64 aruid);
Result GetSharedMemoryHandle(Kernel::KSharedMemory** out_handle, u64 aruid);
void FreeAppletResourceId(u64 aruid);
void EnableInput(u64 aruid, bool is_enabled);
void EnableSixAxisSensor(u64 aruid, bool is_enabled);
void EnablePadInput(u64 aruid, bool is_enabled);
void EnableTouchScreen(u64 aruid, bool is_enabled);
NpadVibrationBase* GetVibrationDevice(const Core::HID::VibrationDeviceHandle& handle);
NpadN64VibrationDevice* GetN64VibrationDevice(const Core::HID::VibrationDeviceHandle& handle);
NpadVibrationDevice* GetNSVibrationDevice(const Core::HID::VibrationDeviceHandle& handle);
NpadGcVibrationDevice* GetGcVibrationDevice(const Core::HID::VibrationDeviceHandle& handle);
Result SetAruidValidForVibration(u64 aruid, bool is_enabled);
void SetForceHandheldStyleVibration(bool is_forced);
Result IsVibrationAruidActive(u64 aruid, bool& is_active) const;
Result GetVibrationDeviceInfo(Core::HID::VibrationDeviceInfo& device_info,
const Core::HID::VibrationDeviceHandle& handle);
Result SendVibrationValue(u64 aruid, const Core::HID::VibrationDeviceHandle& handle,
const Core::HID::VibrationValue& value);
Result GetTouchScreenFirmwareVersion(Core::HID::FirmwareVersion& firmware) const;
void UpdateControllers(std::chrono::nanoseconds ns_late);
void UpdateNpad(std::chrono::nanoseconds ns_late);
void UpdateMouseKeyboard(std::chrono::nanoseconds ns_late);
void UpdateMotion(std::chrono::nanoseconds ns_late);
private:
Result CreateAppletResourceImpl(u64 aruid);
void InitializeHandheldConfig();
void InitializeHidCommonSampler();
void InitializeTouchScreenSampler();
void InitializeConsoleSixAxisSampler();
void InitializeAHidSampler();
bool is_initialized{false};
mutable std::recursive_mutex shared_mutex;
std::shared_ptr<AppletResource> applet_resource{nullptr};
mutable std::mutex input_mutex;
Kernel::KEvent* input_event{nullptr};
std::shared_ptr<HandheldConfig> handheld_config{nullptr};
std::shared_ptr<HidFirmwareSettings> firmware_settings{nullptr};
std::shared_ptr<CaptureButton> capture_button{nullptr};
std::shared_ptr<ConsoleSixAxis> console_six_axis{nullptr};
std::shared_ptr<DebugMouse> debug_mouse{nullptr};
std::shared_ptr<DebugPad> debug_pad{nullptr};
std::shared_ptr<Digitizer> digitizer{nullptr};
std::shared_ptr<HomeButton> home_button{nullptr};
std::shared_ptr<Keyboard> keyboard{nullptr};
std::shared_ptr<Mouse> mouse{nullptr};
std::shared_ptr<NPad> npad{nullptr};
std::shared_ptr<Palma> palma{nullptr};
std::shared_ptr<SevenSixAxis> seven_six_axis{nullptr};
std::shared_ptr<SixAxis> six_axis{nullptr};
std::shared_ptr<SleepButton> sleep_button{nullptr};
std::shared_ptr<UniquePad> unique_pad{nullptr};
std::shared_ptr<Core::Timing::EventType> npad_update_event;
std::shared_ptr<Core::Timing::EventType> default_update_event;
std::shared_ptr<Core::Timing::EventType> mouse_keyboard_update_event;
std::shared_ptr<Core::Timing::EventType> motion_update_event;
// TODO: Create these resources
// std::shared_ptr<AudioControl> audio_control{nullptr};
// std::shared_ptr<ButtonConfig> button_config{nullptr};
// std::shared_ptr<Config> config{nullptr};
// std::shared_ptr<Connection> connection{nullptr};
// std::shared_ptr<CustomConfig> custom_config{nullptr};
// std::shared_ptr<Digitizer> digitizer{nullptr};
// std::shared_ptr<Hdls> hdls{nullptr};
// std::shared_ptr<PlayReport> play_report{nullptr};
// std::shared_ptr<Rail> rail{nullptr};
// Touch Resources
std::shared_ptr<Gesture> gesture{nullptr};
std::shared_ptr<TouchScreen> touch_screen{nullptr};
std::shared_ptr<TouchResource> touch_resource{nullptr};
std::shared_ptr<TouchDriver> touch_driver{nullptr};
std::shared_ptr<Core::Timing::EventType> touch_update_event{nullptr};
Core::System& system;
KernelHelpers::ServiceContext service_context;
};
} // namespace Service::HID

View File

@@ -0,0 +1,197 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/core_timing.h"
#include "hid_core/hid_result.h"
#include "hid_core/hid_util.h"
#include "hid_core/resources/abstracted_pad/abstract_battery_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/npad/npad_types.h"
#include "hid_core/resources/shared_memory_format.h"
namespace Service::HID {
NpadAbstractBatteryHandler::NpadAbstractBatteryHandler() {}
NpadAbstractBatteryHandler::~NpadAbstractBatteryHandler() = default;
void NpadAbstractBatteryHandler::SetAbstractPadHolder(NpadAbstractedPadHolder* holder) {
abstract_pad_holder = holder;
}
void NpadAbstractBatteryHandler::SetAppletResource(AppletResourceHolder* applet_resource) {
applet_resource_holder = applet_resource;
}
void NpadAbstractBatteryHandler::SetPropertiesHandler(NpadAbstractPropertiesHandler* handler) {
properties_handler = handler;
}
Result NpadAbstractBatteryHandler::IncrementRefCounter() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultNpadHandlerOverflow;
}
ref_counter++;
return ResultSuccess;
}
Result NpadAbstractBatteryHandler::DecrementRefCounter() {
if (ref_counter == 0) {
return ResultNpadHandlerNotInitialized;
}
ref_counter--;
return ResultSuccess;
}
Result NpadAbstractBatteryHandler::UpdateBatteryState(u64 aruid) {
const auto npad_index = NpadIdTypeToIndex(properties_handler->GetNpadId());
AruidData* aruid_data = applet_resource_holder->applet_resource->GetAruidData(aruid);
if (aruid_data == nullptr) {
return ResultSuccess;
}
auto& npad_internal_state =
aruid_data->shared_memory_format->npad.npad_entry[npad_index].internal_state;
auto& system_properties = npad_internal_state.system_properties;
system_properties.is_charging_joy_dual.Assign(dual_battery.is_charging);
system_properties.is_powered_joy_dual.Assign(dual_battery.is_powered);
system_properties.is_charging_joy_left.Assign(left_battery.is_charging);
system_properties.is_powered_joy_left.Assign(left_battery.is_powered);
system_properties.is_charging_joy_right.Assign(right_battery.is_charging);
system_properties.is_powered_joy_right.Assign(right_battery.is_powered);
npad_internal_state.battery_level_dual = dual_battery.battery_level;
npad_internal_state.battery_level_left = left_battery.battery_level;
npad_internal_state.battery_level_right = right_battery.battery_level;
return ResultSuccess;
}
void NpadAbstractBatteryHandler::UpdateBatteryState() {
if (ref_counter == 0) {
return;
}
has_new_battery_data = GetNewBatteryState();
}
bool NpadAbstractBatteryHandler::GetNewBatteryState() {
bool has_changed = false;
Core::HID::NpadPowerInfo new_dual_battery_state{};
Core::HID::NpadPowerInfo new_left_battery_state{};
Core::HID::NpadPowerInfo new_right_battery_state{};
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = abstract_pad_holder->GetAbstractedPads(abstract_pads);
for (std::size_t i = 0; i < count; i++) {
auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
const auto power_info = abstract_pad->power_info;
if (power_info.battery_level > Core::HID::NpadBatteryLevel::Full) {
// Abort
continue;
}
const auto style = abstract_pad->assignment_style;
if (style.is_external_assigned || style.is_handheld_assigned) {
new_dual_battery_state = power_info;
}
if (style.is_external_left_assigned || style.is_handheld_left_assigned) {
new_left_battery_state = power_info;
}
if (style.is_external_right_assigned || style.is_handheld_right_assigned) {
new_right_battery_state = power_info;
}
if (abstract_pad->internal_flags.is_battery_low_ovln_required) {
if (abstract_pad->interface_type == Core::HID::NpadInterfaceType::Rail) {
// TODO
}
abstract_pad->internal_flags.is_battery_low_ovln_required.Assign(false);
}
}
if (dual_battery.battery_level != new_dual_battery_state.battery_level ||
dual_battery.is_charging != new_dual_battery_state.is_charging ||
dual_battery.is_powered != new_dual_battery_state.is_powered) {
has_changed = true;
dual_battery = new_dual_battery_state;
}
if (left_battery.battery_level != new_left_battery_state.battery_level ||
left_battery.is_charging != new_left_battery_state.is_charging ||
left_battery.is_powered != new_left_battery_state.is_powered) {
has_changed = true;
left_battery = new_left_battery_state;
}
if (right_battery.battery_level != new_right_battery_state.battery_level ||
right_battery.is_charging != new_right_battery_state.is_charging ||
right_battery.is_powered != new_right_battery_state.is_powered) {
has_changed = true;
right_battery = new_right_battery_state;
}
return has_changed;
}
void NpadAbstractBatteryHandler::UpdateCoreBatteryState() {
if (ref_counter == 0) {
return;
}
if (!has_new_battery_data) {
return;
}
UpdateBatteryState(0);
}
void NpadAbstractBatteryHandler::InitializeBatteryState(u64 aruid) {
UpdateBatteryState(aruid);
}
bool NpadAbstractBatteryHandler::HasBattery() const {
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = abstract_pad_holder->GetAbstractedPads(abstract_pads);
for (std::size_t i = 0; i < count; i++) {
const auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
return abstract_pad->disabled_feature_set.has_fullkey_battery ||
abstract_pad->disabled_feature_set.has_left_right_joy_battery;
}
return false;
}
void NpadAbstractBatteryHandler::HasLeftRightBattery(bool& has_left, bool& has_right) const {
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = abstract_pad_holder->GetAbstractedPads(abstract_pads);
has_left = false;
has_right = false;
for (std::size_t i = 0; i < count; i++) {
const auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
if (!abstract_pad->disabled_feature_set.has_fullkey_battery &&
!abstract_pad->disabled_feature_set.has_left_right_joy_battery) {
continue;
}
has_left = abstract_pad->assignment_style.is_external_left_assigned ||
abstract_pad->assignment_style.is_handheld_left_assigned;
has_right = abstract_pad->assignment_style.is_external_right_assigned ||
abstract_pad->assignment_style.is_handheld_right_assigned;
}
}
} // namespace Service::HID

View File

@@ -0,0 +1,49 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
namespace Service::HID {
struct AppletResourceHolder;
class NpadAbstractedPadHolder;
class NpadAbstractPropertiesHandler;
/// Handles Npad request from HID interfaces
class NpadAbstractBatteryHandler final {
public:
explicit NpadAbstractBatteryHandler();
~NpadAbstractBatteryHandler();
void SetAbstractPadHolder(NpadAbstractedPadHolder* holder);
void SetAppletResource(AppletResourceHolder* applet_resource);
void SetPropertiesHandler(NpadAbstractPropertiesHandler* handler);
Result IncrementRefCounter();
Result DecrementRefCounter();
Result UpdateBatteryState(u64 aruid);
void UpdateBatteryState();
bool GetNewBatteryState();
void UpdateCoreBatteryState();
void InitializeBatteryState(u64 aruid);
bool HasBattery() const;
void HasLeftRightBattery(bool& has_left, bool& has_right) const;
private:
AppletResourceHolder* applet_resource_holder{nullptr};
NpadAbstractedPadHolder* abstract_pad_holder{nullptr};
NpadAbstractPropertiesHandler* properties_handler{nullptr};
s32 ref_counter{};
Core::HID::NpadPowerInfo dual_battery{};
Core::HID::NpadPowerInfo left_battery{};
Core::HID::NpadPowerInfo right_battery{};
bool has_new_battery_data{};
};
} // namespace Service::HID

View File

@@ -0,0 +1,199 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/hid_result.h"
#include "hid_core/hid_util.h"
#include "hid_core/resources/abstracted_pad/abstract_button_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/npad/npad_resource.h"
#include "hid_core/resources/npad/npad_types.h"
#include "hid_core/resources/shared_memory_format.h"
namespace Service::HID {
NpadAbstractButtonHandler::NpadAbstractButtonHandler() {}
NpadAbstractButtonHandler::~NpadAbstractButtonHandler() = default;
void NpadAbstractButtonHandler::SetAbstractPadHolder(NpadAbstractedPadHolder* holder) {
abstract_pad_holder = holder;
}
void NpadAbstractButtonHandler::SetAppletResource(AppletResourceHolder* applet_resource) {
applet_resource_holder = applet_resource;
}
void NpadAbstractButtonHandler::SetPropertiesHandler(NpadAbstractPropertiesHandler* handler) {
properties_handler = handler;
}
Result NpadAbstractButtonHandler::IncrementRefCounter() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultNpadHandlerOverflow;
}
ref_counter++;
return ResultSuccess;
}
Result NpadAbstractButtonHandler::DecrementRefCounter() {
if (ref_counter == 0) {
return ResultNpadHandlerNotInitialized;
}
ref_counter--;
return ResultSuccess;
}
Result NpadAbstractButtonHandler::UpdateAllButtonWithHomeProtection(u64 aruid) {
const Core::HID::NpadIdType npad_id = properties_handler->GetNpadId();
auto* data = applet_resource_holder->applet_resource->GetAruidData(aruid);
if (data == nullptr) {
return ResultSuccess;
}
auto& npad_entry = data->shared_memory_format->npad.npad_entry[NpadIdTypeToIndex(npad_id)];
UpdateButtonLifo(npad_entry, aruid);
bool is_home_button_protection_enabled{};
const auto result = applet_resource_holder->shared_npad_resource->GetHomeProtectionEnabled(
is_home_button_protection_enabled, aruid, npad_id);
if (result.IsError()) {
return ResultSuccess;
}
npad_entry.internal_state.button_properties.is_home_button_protection_enabled.Assign(
is_home_button_protection_enabled);
return ResultSuccess;
}
void NpadAbstractButtonHandler::UpdateAllButtonLifo() {
Core::HID::NpadIdType npad_id = properties_handler->GetNpadId();
for (std::size_t i = 0; i < AruidIndexMax; i++) {
auto* data = applet_resource_holder->applet_resource->GetAruidDataByIndex(i);
auto& npad_entry = data->shared_memory_format->npad.npad_entry[NpadIdTypeToIndex(npad_id)];
UpdateButtonLifo(npad_entry, data->aruid);
}
}
void NpadAbstractButtonHandler::UpdateCoreBatteryState() {
Core::HID::NpadIdType npad_id = properties_handler->GetNpadId();
for (std::size_t i = 0; i < AruidIndexMax; i++) {
auto* data = applet_resource_holder->applet_resource->GetAruidDataByIndex(i);
auto& npad_entry = data->shared_memory_format->npad.npad_entry[NpadIdTypeToIndex(npad_id)];
UpdateButtonLifo(npad_entry, data->aruid);
}
}
void NpadAbstractButtonHandler::UpdateButtonState(u64 aruid) {
Core::HID::NpadIdType npad_id = properties_handler->GetNpadId();
auto* data = applet_resource_holder->applet_resource->GetAruidData(aruid);
if (data == nullptr) {
return;
}
auto& npad_entry = data->shared_memory_format->npad.npad_entry[NpadIdTypeToIndex(npad_id)];
UpdateButtonLifo(npad_entry, aruid);
}
Result NpadAbstractButtonHandler::SetHomeProtection(bool is_enabled, u64 aruid) {
const Core::HID::NpadIdType npad_id = properties_handler->GetNpadId();
auto result = applet_resource_holder->shared_npad_resource->SetHomeProtectionEnabled(
aruid, npad_id, is_enabled);
if (result.IsError()) {
return result;
}
auto* data = applet_resource_holder->applet_resource->GetAruidData(aruid);
if (data == nullptr) {
return ResultSuccess;
}
bool is_home_protection_enabled{};
result = applet_resource_holder->shared_npad_resource->GetHomeProtectionEnabled(
is_home_protection_enabled, aruid, npad_id);
if (result.IsError()) {
return ResultSuccess;
}
auto& npad_entry = data->shared_memory_format->npad.npad_entry[NpadIdTypeToIndex(npad_id)];
npad_entry.internal_state.button_properties.is_home_button_protection_enabled.Assign(
is_home_protection_enabled);
return ResultSuccess;
}
bool NpadAbstractButtonHandler::IsButtonPressedOnConsoleMode() {
return is_button_pressed_on_console_mode;
}
void NpadAbstractButtonHandler::EnableCenterClamp() {
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = abstract_pad_holder->GetAbstractedPads(abstract_pads);
for (std::size_t i = 0; i < count; i++) {
auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
abstract_pad->internal_flags.use_center_clamp.Assign(true);
}
}
void NpadAbstractButtonHandler::UpdateButtonLifo(NpadSharedMemoryEntry& shared_memory, u64 aruid) {
auto* npad_resource = applet_resource_holder->shared_npad_resource;
Core::HID::NpadStyleTag style_tag = {properties_handler->GetStyleSet(aruid)};
style_tag.system_ext.Assign(npad_resource->GetActiveData()->GetNpadSystemExtState());
UpdateNpadFullkeyLifo(style_tag, 0, aruid, shared_memory);
UpdateHandheldLifo(style_tag, 1, aruid, shared_memory);
UpdateJoyconDualLifo(style_tag, 2, aruid, shared_memory);
UpdateJoyconLeftLifo(style_tag, 3, aruid, shared_memory);
UpdateJoyconRightLifo(style_tag, 4, aruid, shared_memory);
UpdatePalmaLifo(style_tag, 5, aruid, shared_memory);
UpdateSystemExtLifo(style_tag, 6, aruid, shared_memory);
}
void NpadAbstractButtonHandler::UpdateNpadFullkeyLifo(Core::HID::NpadStyleTag style_tag,
int style_index, u64 aruid,
NpadSharedMemoryEntry& shared_memory) {
// TODO
}
void NpadAbstractButtonHandler::UpdateHandheldLifo(Core::HID::NpadStyleTag style_tag,
int style_index, u64 aruid,
NpadSharedMemoryEntry& shared_memory) {
// TODO
}
void NpadAbstractButtonHandler::UpdateJoyconDualLifo(Core::HID::NpadStyleTag style_tag,
int style_index, u64 aruid,
NpadSharedMemoryEntry& shared_memory) {
// TODO
}
void NpadAbstractButtonHandler::UpdateJoyconLeftLifo(Core::HID::NpadStyleTag style_tag,
int style_index, u64 aruid,
NpadSharedMemoryEntry& shared_memory) {
// TODO
}
void NpadAbstractButtonHandler::UpdateJoyconRightLifo(Core::HID::NpadStyleTag style_tag,
int style_index, u64 aruid,
NpadSharedMemoryEntry& shared_memory) {
// TODO
}
void NpadAbstractButtonHandler::UpdateSystemExtLifo(Core::HID::NpadStyleTag style_tag,
int style_index, u64 aruid,
NpadSharedMemoryEntry& shared_memory) {
// TODO
}
void NpadAbstractButtonHandler::UpdatePalmaLifo(Core::HID::NpadStyleTag style_tag, int style_index,
u64 aruid, NpadSharedMemoryEntry& shared_memory) {
// TODO
}
} // namespace Service::HID

View File

@@ -0,0 +1,75 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
namespace Service::HID {
struct NpadSharedMemoryEntry;
struct AppletResourceHolder;
class NpadAbstractedPadHolder;
class NpadAbstractPropertiesHandler;
/// Handles Npad request from HID interfaces
class NpadAbstractButtonHandler final {
public:
explicit NpadAbstractButtonHandler();
~NpadAbstractButtonHandler();
void SetAbstractPadHolder(NpadAbstractedPadHolder* holder);
void SetAppletResource(AppletResourceHolder* applet_resource);
void SetPropertiesHandler(NpadAbstractPropertiesHandler* handler);
Result IncrementRefCounter();
Result DecrementRefCounter();
Result UpdateAllButtonWithHomeProtection(u64 aruid);
void UpdateAllButtonLifo();
void UpdateCoreBatteryState();
void UpdateButtonState(u64 aruid);
Result SetHomeProtection(bool is_enabled, u64 aruid);
bool IsButtonPressedOnConsoleMode();
void EnableCenterClamp();
void UpdateButtonLifo(NpadSharedMemoryEntry& shared_memory, u64 aruid);
void UpdateNpadFullkeyLifo(Core::HID::NpadStyleTag style_tag, int index, u64 aruid,
NpadSharedMemoryEntry& shared_memory);
void UpdateHandheldLifo(Core::HID::NpadStyleTag style_tag, int index, u64 aruid,
NpadSharedMemoryEntry& shared_memory);
void UpdateJoyconDualLifo(Core::HID::NpadStyleTag style_tag, int index, u64 aruid,
NpadSharedMemoryEntry& shared_memory);
void UpdateJoyconLeftLifo(Core::HID::NpadStyleTag style_tag, int index, u64 aruid,
NpadSharedMemoryEntry& shared_memory);
void UpdateJoyconRightLifo(Core::HID::NpadStyleTag style_tag, int index, u64 aruid,
NpadSharedMemoryEntry& shared_memory);
void UpdateSystemExtLifo(Core::HID::NpadStyleTag style_tag, int index, u64 aruid,
NpadSharedMemoryEntry& shared_memory);
void UpdatePalmaLifo(Core::HID::NpadStyleTag style_tag, int index, u64 aruid,
NpadSharedMemoryEntry& shared_memory);
private:
struct GcTrigger {
float left;
float right;
};
AppletResourceHolder* applet_resource_holder{nullptr};
NpadAbstractedPadHolder* abstract_pad_holder{nullptr};
NpadAbstractPropertiesHandler* properties_handler{nullptr};
s32 ref_counter{};
bool is_button_pressed_on_console_mode{};
u64 gc_sampling_number{};
GcTrigger gc_trigger_state{};
};
} // namespace Service::HID

View File

@@ -0,0 +1,126 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_readable_event.h"
#include "hid_core/hid_result.h"
#include "hid_core/resources/abstracted_pad/abstract_ir_sensor_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
#include "hid_core/resources/npad/npad_types.h"
namespace Service::HID {
NpadAbstractIrSensorHandler::NpadAbstractIrSensorHandler() {}
NpadAbstractIrSensorHandler::~NpadAbstractIrSensorHandler() = default;
void NpadAbstractIrSensorHandler::SetAbstractPadHolder(NpadAbstractedPadHolder* holder) {
abstract_pad_holder = holder;
}
void NpadAbstractIrSensorHandler::SetPropertiesHandler(NpadAbstractPropertiesHandler* handler) {
properties_handler = handler;
}
Result NpadAbstractIrSensorHandler::IncrementRefCounter() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultNpadHandlerOverflow;
}
ref_counter++;
return ResultSuccess;
}
Result NpadAbstractIrSensorHandler::DecrementRefCounter() {
if (ref_counter == 0) {
return ResultNpadHandlerNotInitialized;
}
ref_counter--;
return ResultSuccess;
}
void NpadAbstractIrSensorHandler::UpdateIrSensorState() {
const auto previous_state = sensor_state;
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = abstract_pad_holder->GetAbstractedPads(abstract_pads);
if (count == 0) {
sensor_state = NpadIrSensorState::Disabled;
if (sensor_state == previous_state) {
return;
}
ir_sensor_event->Signal();
return;
}
bool is_found{};
for (std::size_t i = 0; i < count; i++) {
auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
if (!abstract_pad->disabled_feature_set.has_bluetooth_address) {
continue;
}
is_found = true;
xcd_handle = abstract_pad->xcd_handle;
}
if (is_found) {
if (sensor_state == NpadIrSensorState::Active) {
return;
}
sensor_state = NpadIrSensorState::Available;
if (sensor_state == previous_state) {
return;
}
ir_sensor_event->Signal();
return;
}
sensor_state = NpadIrSensorState::Unavailable;
if (sensor_state == previous_state) {
return;
}
ir_sensor_event->Signal();
return;
}
Result NpadAbstractIrSensorHandler::ActivateIrSensor(bool is_enabled) {
if (sensor_state == NpadIrSensorState::Unavailable) {
return ResultIrSensorIsNotReady;
}
if (is_enabled && sensor_state == NpadIrSensorState::Available) {
sensor_state = NpadIrSensorState::Active;
} else {
if (is_enabled) {
return ResultSuccess;
}
if (sensor_state != NpadIrSensorState::Active) {
return ResultSuccess;
}
sensor_state = NpadIrSensorState::Available;
}
ir_sensor_event->Signal();
return ResultSuccess;
}
Result NpadAbstractIrSensorHandler::GetIrSensorEventHandle(Kernel::KReadableEvent** out_event) {
*out_event = &ir_sensor_event->GetReadableEvent();
return ResultSuccess;
}
Result NpadAbstractIrSensorHandler::GetXcdHandleForNpadWithIrSensor(u64& handle) const {
if (sensor_state < NpadIrSensorState::Available) {
return ResultIrSensorIsNotReady;
}
// handle = xcd_handle;
return ResultSuccess;
}
NpadIrSensorState NpadAbstractIrSensorHandler::GetSensorState() const {
return sensor_state;
}
} // namespace Service::HID

View File

@@ -0,0 +1,60 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
namespace Core::HID {
class EmulatedController;
}
namespace Kernel {
class KEvent;
class KReadableEvent;
} // namespace Kernel
enum class NpadIrSensorState : u32 {
Disabled,
Unavailable,
Available,
Active,
};
namespace Service::HID {
class NpadAbstractedPadHolder;
class NpadAbstractPropertiesHandler;
/// Handles Npad request from HID interfaces
class NpadAbstractIrSensorHandler final {
public:
explicit NpadAbstractIrSensorHandler();
~NpadAbstractIrSensorHandler();
void SetAbstractPadHolder(NpadAbstractedPadHolder* holder);
void SetPropertiesHandler(NpadAbstractPropertiesHandler* handler);
Result IncrementRefCounter();
Result DecrementRefCounter();
void UpdateIrSensorState();
Result ActivateIrSensor(bool param_2);
Result GetIrSensorEventHandle(Kernel::KReadableEvent** out_event);
Result GetXcdHandleForNpadWithIrSensor(u64& handle) const;
NpadIrSensorState GetSensorState() const;
private:
NpadAbstractedPadHolder* abstract_pad_holder{nullptr};
NpadAbstractPropertiesHandler* properties_handler{nullptr};
s32 ref_counter{};
Kernel::KEvent* ir_sensor_event{nullptr};
Core::HID::EmulatedController* xcd_handle{};
NpadIrSensorState sensor_state{};
};
} // namespace Service::HID

View File

@@ -0,0 +1,123 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/core_timing.h"
#include "hid_core/hid_result.h"
#include "hid_core/hid_util.h"
#include "hid_core/resources/abstracted_pad/abstract_led_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/npad/npad_types.h"
namespace Service::HID {
NpadAbstractLedHandler::NpadAbstractLedHandler() {}
NpadAbstractLedHandler::~NpadAbstractLedHandler() = default;
void NpadAbstractLedHandler::SetAbstractPadHolder(NpadAbstractedPadHolder* holder) {
abstract_pad_holder = holder;
}
void NpadAbstractLedHandler::SetAppletResource(AppletResourceHolder* applet_resource) {
applet_resource_holder = applet_resource;
}
void NpadAbstractLedHandler::SetPropertiesHandler(NpadAbstractPropertiesHandler* handler) {
properties_handler = handler;
}
Result NpadAbstractLedHandler::IncrementRefCounter() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultNpadHandlerOverflow;
}
ref_counter++;
return ResultSuccess;
}
Result NpadAbstractLedHandler::DecrementRefCounter() {
if (ref_counter == 0) {
return ResultNpadHandlerNotInitialized;
}
ref_counter--;
return ResultSuccess;
}
void NpadAbstractLedHandler::SetNpadLedHandlerLedPattern() {
const auto npad_id = properties_handler->GetNpadId();
switch (npad_id) {
case Core::HID::NpadIdType::Player1:
left_pattern = Core::HID::LedPattern{1, 0, 0, 0};
break;
case Core::HID::NpadIdType::Player2:
left_pattern = Core::HID::LedPattern{1, 1, 0, 0};
break;
case Core::HID::NpadIdType::Player3:
left_pattern = Core::HID::LedPattern{1, 1, 1, 0};
break;
case Core::HID::NpadIdType::Player4:
left_pattern = Core::HID::LedPattern{1, 1, 1, 1};
break;
case Core::HID::NpadIdType::Player5:
left_pattern = Core::HID::LedPattern{1, 0, 0, 1};
break;
case Core::HID::NpadIdType::Player6:
left_pattern = Core::HID::LedPattern{1, 0, 1, 0};
break;
case Core::HID::NpadIdType::Player7:
left_pattern = Core::HID::LedPattern{1, 0, 1, 1};
break;
case Core::HID::NpadIdType::Player8:
left_pattern = Core::HID::LedPattern{0, 1, 1, 0};
break;
case Core::HID::NpadIdType::Other:
case Core::HID::NpadIdType::Handheld:
left_pattern = Core::HID::LedPattern{0, 0, 0, 0};
break;
default:
ASSERT_MSG(false, "Invalid npad id type");
break;
}
switch (npad_id) {
case Core::HID::NpadIdType::Player1:
right_pattern = Core::HID::LedPattern{0, 0, 0, 1};
break;
case Core::HID::NpadIdType::Player2:
right_pattern = Core::HID::LedPattern{0, 1, 1, 1};
break;
case Core::HID::NpadIdType::Player3:
right_pattern = Core::HID::LedPattern{0, 1, 1, 1};
break;
case Core::HID::NpadIdType::Player4:
right_pattern = Core::HID::LedPattern{1, 1, 1, 1};
break;
case Core::HID::NpadIdType::Player5:
right_pattern = Core::HID::LedPattern{1, 0, 0, 1};
break;
case Core::HID::NpadIdType::Player6:
right_pattern = Core::HID::LedPattern{0, 1, 0, 1};
break;
case Core::HID::NpadIdType::Player7:
right_pattern = Core::HID::LedPattern{1, 1, 0, 1};
break;
case Core::HID::NpadIdType::Player8:
right_pattern = Core::HID::LedPattern{0, 1, 1, 0};
break;
case Core::HID::NpadIdType::Other:
case Core::HID::NpadIdType::Handheld:
right_pattern = Core::HID::LedPattern{0, 0, 0, 0};
break;
default:
ASSERT_MSG(false, "Invalid npad id type");
break;
}
}
void NpadAbstractLedHandler::SetLedBlinkingDevice(Core::HID::LedPattern pattern) {
led_blinking = pattern;
}
} // namespace Service::HID

View File

@@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
namespace Service::HID {
struct AppletResourceHolder;
class NpadAbstractedPadHolder;
class NpadAbstractPropertiesHandler;
/// Handles Npad request from HID interfaces
class NpadAbstractLedHandler final {
public:
explicit NpadAbstractLedHandler();
~NpadAbstractLedHandler();
void SetAbstractPadHolder(NpadAbstractedPadHolder* holder);
void SetAppletResource(AppletResourceHolder* applet_resource);
void SetPropertiesHandler(NpadAbstractPropertiesHandler* handler);
Result IncrementRefCounter();
Result DecrementRefCounter();
void SetNpadLedHandlerLedPattern();
void SetLedBlinkingDevice(Core::HID::LedPattern pattern);
private:
AppletResourceHolder* applet_resource_holder{nullptr};
NpadAbstractedPadHolder* abstract_pad_holder{nullptr};
NpadAbstractPropertiesHandler* properties_handler{nullptr};
s32 ref_counter{};
Core::HID::LedPattern led_blinking{0, 0, 0, 0};
Core::HID::LedPattern left_pattern{0, 0, 0, 0};
Core::HID::LedPattern right_pattern{0, 0, 0, 0};
u64 led_interval{};
};
} // namespace Service::HID

View File

@@ -0,0 +1,108 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/hid_result.h"
#include "hid_core/resources/abstracted_pad/abstract_mcu_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
#include "hid_core/resources/npad/npad_types.h"
namespace Service::HID {
NpadAbstractMcuHandler::NpadAbstractMcuHandler() {}
NpadAbstractMcuHandler::~NpadAbstractMcuHandler() = default;
void NpadAbstractMcuHandler::SetAbstractPadHolder(NpadAbstractedPadHolder* holder) {
abstract_pad_holder = holder;
}
void NpadAbstractMcuHandler::SetPropertiesHandler(NpadAbstractPropertiesHandler* handler) {
properties_handler = handler;
}
Result NpadAbstractMcuHandler::IncrementRefCounter() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultNpadHandlerOverflow;
}
ref_counter++;
return ResultSuccess;
}
Result NpadAbstractMcuHandler::DecrementRefCounter() {
if (ref_counter == 0) {
return ResultNpadHandlerNotInitialized;
}
ref_counter--;
return ResultSuccess;
}
void NpadAbstractMcuHandler::UpdateMcuState() {
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = properties_handler->GetAbstractedPads(abstract_pads);
if (count == 0) {
mcu_holder = {};
return;
}
for (std::size_t i = 0; i < count; i++) {
auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
if (!abstract_pad->disabled_feature_set.has_left_joy_rail_bus) {
if (!abstract_pad->disabled_feature_set.has_left_joy_six_axis_sensor &&
!abstract_pad->disabled_feature_set.has_right_joy_six_axis_sensor) {
continue;
}
if (mcu_holder[1].state != NpadMcuState::Active) {
mcu_holder[1].state = NpadMcuState::Available;
}
mcu_holder[1].abstracted_pad = abstract_pad;
continue;
}
if (mcu_holder[0].state != NpadMcuState::Active) {
mcu_holder[0].state = NpadMcuState::Available;
}
mcu_holder[0].abstracted_pad = abstract_pad;
}
}
Result NpadAbstractMcuHandler::GetAbstractedPad(IAbstractedPad** data, u32 mcu_index) {
if (mcu_holder[mcu_index].state == NpadMcuState::None ||
mcu_holder[mcu_index].abstracted_pad == nullptr) {
return ResultMcuIsNotReady;
}
*data = mcu_holder[mcu_index].abstracted_pad;
return ResultSuccess;
}
NpadMcuState NpadAbstractMcuHandler::GetMcuState(u32 mcu_index) {
return mcu_holder[mcu_index].state;
}
Result NpadAbstractMcuHandler::SetMcuState(bool is_enabled, u32 mcu_index) {
NpadMcuState& state = mcu_holder[mcu_index].state;
if (state == NpadMcuState::None) {
return ResultMcuIsNotReady;
}
if ((is_enabled) && (state == NpadMcuState::Available)) {
state = NpadMcuState::Active;
return ResultSuccess;
}
if (is_enabled) {
return ResultSuccess;
}
if (state != NpadMcuState::Active) {
return ResultSuccess;
}
state = NpadMcuState::Available;
return ResultSuccess;
}
} // namespace Service::HID

View File

@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
namespace Service::HID {
struct IAbstractedPad;
class NpadAbstractedPadHolder;
class NpadAbstractPropertiesHandler;
enum class NpadMcuState : u32 {
None,
Available,
Active,
};
struct NpadMcuHolder {
NpadMcuState state;
INSERT_PADDING_BYTES(0x4);
IAbstractedPad* abstracted_pad;
};
static_assert(sizeof(NpadMcuHolder) == 0x10, "NpadMcuHolder is an invalid size");
/// Handles Npad request from HID interfaces
class NpadAbstractMcuHandler final {
public:
explicit NpadAbstractMcuHandler();
~NpadAbstractMcuHandler();
void SetAbstractPadHolder(NpadAbstractedPadHolder* holder);
void SetPropertiesHandler(NpadAbstractPropertiesHandler* handler);
Result IncrementRefCounter();
Result DecrementRefCounter();
void UpdateMcuState();
Result GetAbstractedPad(IAbstractedPad** data, u32 mcu_index);
NpadMcuState GetMcuState(u32 mcu_index);
Result SetMcuState(bool is_enabled, u32 mcu_index);
private:
NpadAbstractedPadHolder* abstract_pad_holder{nullptr};
NpadAbstractPropertiesHandler* properties_handler{nullptr};
s32 ref_counter{};
std::array<NpadMcuHolder, 2> mcu_holder{};
};
} // namespace Service::HID

View File

@@ -0,0 +1,140 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_readable_event.h"
#include "hid_core/hid_result.h"
#include "hid_core/resources/abstracted_pad/abstract_nfc_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
#include "hid_core/resources/npad/npad_types.h"
namespace Service::HID {
NpadAbstractNfcHandler::NpadAbstractNfcHandler() {}
NpadAbstractNfcHandler::~NpadAbstractNfcHandler() = default;
void NpadAbstractNfcHandler::SetAbstractPadHolder(NpadAbstractedPadHolder* holder) {
abstract_pad_holder = holder;
}
void NpadAbstractNfcHandler::SetPropertiesHandler(NpadAbstractPropertiesHandler* handler) {
properties_handler = handler;
}
Result NpadAbstractNfcHandler::IncrementRefCounter() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultNpadHandlerOverflow;
}
ref_counter++;
return ResultSuccess;
}
Result NpadAbstractNfcHandler::DecrementRefCounter() {
if (ref_counter == 0) {
return ResultNpadHandlerNotInitialized;
}
ref_counter--;
return ResultSuccess;
}
void NpadAbstractNfcHandler::UpdateNfcState() {
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = properties_handler->GetAbstractedPads(abstract_pads);
if (count == 0) {
if (sensor_state == NpadNfcState::Active) {
nfc_activate_event->Signal();
}
if (sensor_state == NpadNfcState::Unavailable) {
return;
}
sensor_state = NpadNfcState::Unavailable;
input_event->Signal();
return;
}
bool is_found{};
for (std::size_t i = 0; i < count; i++) {
auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
if (!abstract_pad->disabled_feature_set.has_nfc) {
continue;
}
is_found = true;
xcd_handle = 0;
}
if (is_found) {
if (sensor_state == NpadNfcState::Active) {
return;
}
if (sensor_state == NpadNfcState::Available) {
return;
}
sensor_state = NpadNfcState::Available;
input_event->Signal();
return;
}
if (sensor_state == NpadNfcState::Active) {
nfc_activate_event->Signal();
}
if (sensor_state == NpadNfcState::Unavailable) {
return;
}
sensor_state = NpadNfcState::Unavailable;
input_event->Signal();
return;
}
bool NpadAbstractNfcHandler::HasNfcSensor() {
return sensor_state != NpadNfcState::Unavailable;
}
bool NpadAbstractNfcHandler::IsNfcActivated() {
return sensor_state == NpadNfcState::Active;
}
Result NpadAbstractNfcHandler::GetAcquireNfcActivateEventHandle(
Kernel::KReadableEvent** out_event) {
*out_event = &nfc_activate_event->GetReadableEvent();
return ResultSuccess;
}
void NpadAbstractNfcHandler::SetInputEvent(Kernel::KEvent* event) {
input_event = event;
}
Result NpadAbstractNfcHandler::ActivateNfc(bool is_enabled) {
if (sensor_state == NpadNfcState::Active) {
return ResultNfcIsNotReady;
}
NpadNfcState new_state = NpadNfcState::Available;
if (is_enabled) {
new_state = NpadNfcState::Active;
}
if (sensor_state != new_state) {
sensor_state = new_state;
nfc_activate_event->Signal();
}
return ResultSuccess;
}
Result NpadAbstractNfcHandler::GetXcdHandleWithNfc(u64& out_xcd_handle) const {
if (sensor_state == NpadNfcState::Unavailable) {
return ResultNfcIsNotReady;
}
if (xcd_handle == 0) {
return ResultNfcXcdHandleIsNotInitialized;
}
out_xcd_handle = xcd_handle;
return ResultSuccess;
}
} // namespace Service::HID

View File

@@ -0,0 +1,57 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
namespace Kernel {
class KReadableEvent;
}
enum class NpadNfcState : u32 {
Unavailable,
Available,
Active,
};
namespace Service::HID {
class NpadAbstractedPadHolder;
class NpadAbstractPropertiesHandler;
/// Handles Npad request from HID interfaces
class NpadAbstractNfcHandler final {
public:
explicit NpadAbstractNfcHandler();
~NpadAbstractNfcHandler();
void SetAbstractPadHolder(NpadAbstractedPadHolder* holder);
void SetPropertiesHandler(NpadAbstractPropertiesHandler* handler);
Result IncrementRefCounter();
Result DecrementRefCounter();
void UpdateNfcState();
bool HasNfcSensor();
bool IsNfcActivated();
Result GetAcquireNfcActivateEventHandle(Kernel::KReadableEvent** out_event);
void SetInputEvent(Kernel::KEvent* event);
Result ActivateNfc(bool is_enabled);
Result GetXcdHandleWithNfc(u64& out_xcd_handle) const;
private:
NpadAbstractedPadHolder* abstract_pad_holder{nullptr};
NpadAbstractPropertiesHandler* properties_handler{nullptr};
s32 ref_counter{};
Kernel::KEvent* nfc_activate_event{nullptr};
Kernel::KEvent* input_event{nullptr};
u64 xcd_handle{};
NpadNfcState sensor_state{NpadNfcState::Unavailable};
};
} // namespace Service::HID

View File

@@ -0,0 +1,291 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/hid_core.h"
#include "hid_core/hid_result.h"
#include "hid_core/resources/abstracted_pad/abstract_pad.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/npad/npad_types.h"
namespace Service::HID {
AbstractPad::AbstractPad() {}
AbstractPad::~AbstractPad() = default;
void AbstractPad::SetExternals(AppletResourceHolder* applet_resource,
CaptureButtonResource* capture_button_resource,
HomeButtonResource* home_button_resource,
SixAxisResource* sixaxis_resource, PalmaResource* palma_resource,
NpadVibration* vibration, Core::HID::HIDCore* core) {
applet_resource_holder = applet_resource;
properties_handler.SetAppletResource(applet_resource_holder);
properties_handler.SetAbstractPadHolder(&abstract_pad_holder);
led_handler.SetAppletResource(applet_resource_holder);
led_handler.SetAbstractPadHolder(&abstract_pad_holder);
led_handler.SetPropertiesHandler(&properties_handler);
ir_sensor_handler.SetAbstractPadHolder(&abstract_pad_holder);
ir_sensor_handler.SetPropertiesHandler(&properties_handler);
nfc_handler.SetAbstractPadHolder(&abstract_pad_holder);
nfc_handler.SetPropertiesHandler(&properties_handler);
mcu_handler.SetAbstractPadHolder(&abstract_pad_holder);
mcu_handler.SetPropertiesHandler(&properties_handler);
vibration_handler.SetAppletResource(applet_resource_holder);
vibration_handler.SetAbstractPadHolder(&abstract_pad_holder);
vibration_handler.SetPropertiesHandler(&properties_handler);
vibration_handler.SetN64Vibration(&vibration_n64);
vibration_handler.SetVibration(&vibration_left, &vibration_right);
vibration_handler.SetGcVibration(&vibration_gc);
vibration_handler.SetVibrationHandler(vibration);
vibration_handler.SetHidCore(core);
sixaxis_handler.SetAppletResource(applet_resource_holder);
sixaxis_handler.SetAbstractPadHolder(&abstract_pad_holder);
sixaxis_handler.SetPropertiesHandler(&properties_handler);
sixaxis_handler.SetSixaxisResource(sixaxis_resource);
button_handler.SetAppletResource(applet_resource_holder);
button_handler.SetAbstractPadHolder(&abstract_pad_holder);
button_handler.SetPropertiesHandler(&properties_handler);
battery_handler.SetAppletResource(applet_resource_holder);
battery_handler.SetAbstractPadHolder(&abstract_pad_holder);
battery_handler.SetPropertiesHandler(&properties_handler);
palma_handler.SetAbstractPadHolder(&abstract_pad_holder);
palma_handler.SetPropertiesHandler(&properties_handler);
palma_handler.SetPalmaResource(palma_resource);
}
void AbstractPad::SetNpadId(Core::HID::NpadIdType npad_id) {
properties_handler.SetNpadId(npad_id);
}
Result AbstractPad::Activate() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultNpadHandlerOverflow;
}
if (ref_counter != 0) {
ref_counter++;
return ResultSuccess;
}
std::size_t stage = 0;
Result result = ResultSuccess;
if (result.IsSuccess()) {
stage++;
result = properties_handler.IncrementRefCounter();
}
if (result.IsSuccess()) {
stage++;
result = led_handler.IncrementRefCounter();
}
if (result.IsSuccess()) {
stage++;
result = ir_sensor_handler.IncrementRefCounter();
}
if (result.IsSuccess()) {
stage++;
result = mcu_handler.IncrementRefCounter();
}
if (result.IsSuccess()) {
stage++;
result = nfc_handler.IncrementRefCounter();
}
if (result.IsSuccess()) {
stage++;
result = vibration_handler.IncrementRefCounter();
}
if (result.IsSuccess()) {
stage++;
result = sixaxis_handler.IncrementRefCounter();
}
if (result.IsSuccess()) {
stage++;
result = button_handler.IncrementRefCounter();
}
if (result.IsSuccess()) {
stage++;
result = battery_handler.IncrementRefCounter();
}
if (result.IsSuccess()) {
stage++;
result = palma_handler.IncrementRefCounter();
}
if (result.IsSuccess()) {
ref_counter++;
return result;
}
if (stage > 9) {
battery_handler.DecrementRefCounter();
}
if (stage > 8) {
button_handler.DecrementRefCounter();
}
if (stage > 7) {
sixaxis_handler.DecrementRefCounter();
}
if (stage > 6) {
vibration_handler.DecrementRefCounter();
}
if (stage > 5) {
nfc_handler.DecrementRefCounter();
}
if (stage > 4) {
mcu_handler.DecrementRefCounter();
}
if (stage > 3) {
ir_sensor_handler.DecrementRefCounter();
}
if (stage > 2) {
led_handler.DecrementRefCounter();
}
if (stage > 1) {
properties_handler.DecrementRefCounter();
}
return result;
}
Result AbstractPad::Deactivate() {
if (ref_counter == 0) {
return ResultNpadResourceNotInitialized;
}
ref_counter--;
battery_handler.DecrementRefCounter();
button_handler.DecrementRefCounter();
sixaxis_handler.DecrementRefCounter();
vibration_handler.DecrementRefCounter();
nfc_handler.DecrementRefCounter();
ir_sensor_handler.DecrementRefCounter();
mcu_handler.DecrementRefCounter();
led_handler.DecrementRefCounter();
properties_handler.DecrementRefCounter();
palma_handler.DecrementRefCounter();
return ResultSuccess;
}
Result AbstractPad::ActivateNpad(u64 aruid) {
Result result = ResultSuccess;
if (result.IsSuccess()) {
result = properties_handler.ActivateNpadUnknown0x88(aruid);
}
if (result.IsSuccess()) {
result = sixaxis_handler.UpdateSixAxisState2(aruid);
}
if (result.IsSuccess()) {
result = battery_handler.UpdateBatteryState(aruid);
}
return result;
}
NpadAbstractedPadHolder* AbstractPad::GetAbstractedPadHolder() {
return &abstract_pad_holder;
}
NpadAbstractPropertiesHandler* AbstractPad::GetAbstractPropertiesHandler() {
return &properties_handler;
}
NpadAbstractLedHandler* AbstractPad::GetAbstractLedHandler() {
return &led_handler;
}
NpadAbstractIrSensorHandler* AbstractPad::GetAbstractIrSensorHandler() {
return &ir_sensor_handler;
}
NpadAbstractMcuHandler* AbstractPad::GetAbstractMcuHandler() {
return &mcu_handler;
}
NpadAbstractNfcHandler* AbstractPad::GetAbstractNfcHandler() {
return &nfc_handler;
}
NpadAbstractVibrationHandler* AbstractPad::GetAbstractVibrationHandler() {
return &vibration_handler;
}
NpadAbstractSixAxisHandler* AbstractPad::GetAbstractSixAxisHandler() {
return &sixaxis_handler;
}
NpadAbstractButtonHandler* AbstractPad::GetAbstractButtonHandler() {
return &button_handler;
}
NpadAbstractBatteryHandler* AbstractPad::GetAbstractBatteryHandler() {
return &battery_handler;
}
NpadN64VibrationDevice* AbstractPad::GetN64VibrationDevice() {
return &vibration_n64;
}
NpadVibrationDevice* AbstractPad::GetVibrationDevice(Core::HID::DeviceIndex device_index) {
if (device_index == Core::HID::DeviceIndex::Right) {
return &vibration_right;
}
return &vibration_left;
}
NpadGcVibrationDevice* AbstractPad::GetGCVibrationDevice() {
return &vibration_gc;
}
Core::HID::NpadIdType AbstractPad::GetLastActiveNpad() {
return properties_handler.GetNpadId();
}
void AbstractPad::UpdateInterfaceType() {
if (interface_type != properties_handler.GetInterfaceType()) {
Update();
}
battery_handler.UpdateBatteryState();
}
void AbstractPad::Update() {
properties_handler.UpdateDeviceType();
led_handler.SetNpadLedHandlerLedPattern();
vibration_handler.UpdateVibrationState();
sixaxis_handler.UpdateSixAxisState();
nfc_handler.UpdateNfcState();
ir_sensor_handler.UpdateIrSensorState();
mcu_handler.UpdateMcuState();
palma_handler.UpdatePalmaState();
battery_handler.UpdateBatteryState();
button_handler.EnableCenterClamp();
interface_type = properties_handler.GetInterfaceType();
std::scoped_lock lock{*applet_resource_holder->shared_mutex};
properties_handler.UpdateAllDeviceProperties();
battery_handler.UpdateCoreBatteryState();
button_handler.UpdateCoreBatteryState();
}
void AbstractPad::UpdatePadState() {
button_handler.UpdateAllButtonLifo();
sixaxis_handler.UpdateSixAxisState();
battery_handler.UpdateCoreBatteryState();
}
void AbstractPad::EnableAppletToGetInput(u64 aruid) {
button_handler.UpdateButtonState(aruid);
sixaxis_handler.UpdateSixAxisState(aruid);
battery_handler.UpdateBatteryState(aruid);
}
} // namespace Service::HID

View File

@@ -0,0 +1,121 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <array>
#include <mutex>
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/npad/npad_types.h"
#include "hid_core/resources/abstracted_pad/abstract_battery_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_button_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_ir_sensor_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_led_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_mcu_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_nfc_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/abstracted_pad/abstract_palma_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_sixaxis_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_vibration_handler.h"
#include "hid_core/resources/vibration/gc_vibration_device.h"
#include "hid_core/resources/vibration/n64_vibration_device.h"
#include "hid_core/resources/vibration/vibration_device.h"
namespace Service::HID {
class AppletResource;
class SixAxisResource;
class PalmaResource;
class NPadResource;
class NpadLastActiveHandler;
class NpadIrNfcHandler;
class UniquePads;
class NpadPalmaHandler;
class FirmwareResource;
class NpadVibration;
class NpadHighestBattery;
class NpadGcVibration;
class CaptureButtonResource;
class HomeButtonResource;
struct HandheldConfig;
/// Handles Npad request from HID interfaces
class AbstractPad final {
public:
explicit AbstractPad();
~AbstractPad();
void SetExternals(AppletResourceHolder* applet_resource,
CaptureButtonResource* capture_button_resource,
HomeButtonResource* home_button_resource, SixAxisResource* sixaxis_resource,
PalmaResource* palma_resource, NpadVibration* vibration,
Core::HID::HIDCore* core);
void SetNpadId(Core::HID::NpadIdType npad_id);
Result Activate();
Result Deactivate();
Result ActivateNpad(u64 aruid);
NpadAbstractedPadHolder* GetAbstractedPadHolder();
NpadAbstractPropertiesHandler* GetAbstractPropertiesHandler();
NpadAbstractLedHandler* GetAbstractLedHandler();
NpadAbstractIrSensorHandler* GetAbstractIrSensorHandler();
NpadAbstractMcuHandler* GetAbstractMcuHandler();
NpadAbstractNfcHandler* GetAbstractNfcHandler();
NpadAbstractVibrationHandler* GetAbstractVibrationHandler();
NpadAbstractSixAxisHandler* GetAbstractSixAxisHandler();
NpadAbstractButtonHandler* GetAbstractButtonHandler();
NpadAbstractBatteryHandler* GetAbstractBatteryHandler();
NpadN64VibrationDevice* GetN64VibrationDevice();
NpadVibrationDevice* GetVibrationDevice(Core::HID::DeviceIndex device_index);
NpadGcVibrationDevice* GetGCVibrationDevice();
Core::HID::NpadIdType GetLastActiveNpad();
void UpdateInterfaceType();
void Update();
void UpdatePadState();
void EnableAppletToGetInput(u64 aruid);
private:
AppletResourceHolder* applet_resource_holder{nullptr};
NpadAbstractedPadHolder abstract_pad_holder{};
NpadAbstractPropertiesHandler properties_handler{};
NpadAbstractLedHandler led_handler{};
NpadAbstractIrSensorHandler ir_sensor_handler{};
NpadAbstractNfcHandler nfc_handler{};
NpadAbstractMcuHandler mcu_handler{};
NpadAbstractVibrationHandler vibration_handler{};
NpadAbstractSixAxisHandler sixaxis_handler{};
NpadAbstractButtonHandler button_handler{};
NpadAbstractBatteryHandler battery_handler{};
NpadAbstractPalmaHandler palma_handler{};
NpadN64VibrationDevice vibration_n64{};
NpadVibrationDevice vibration_left{};
NpadVibrationDevice vibration_right{};
NpadGcVibrationDevice vibration_gc{};
// SixAxisConfigHolder fullkey_config;
// SixAxisConfigHolder handheld_config;
// SixAxisConfigHolder dual_left_config;
// SixAxisConfigHolder dual_right_config;
// SixAxisConfigHolder left_config;
// SixAxisConfigHolder right_config;
s32 ref_counter{};
Core::HID::NpadInterfaceType interface_type{Core::HID::NpadInterfaceType::None};
};
using FullAbstractPad = std::array<AbstractPad, MaxSupportedNpadIdTypes>;
} // namespace Service::HID

View File

@@ -0,0 +1,99 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/hid_result.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/npad/npad_types.h"
namespace Service::HID {
Result NpadAbstractedPadHolder::RegisterAbstractPad(IAbstractedPad* abstracted_pad) {
if (list_size >= assignment_list.size()) {
return ResultNpadIsNotProController;
}
for (std::size_t i = 0; i < list_size; i++) {
if (assignment_list[i].device_type == abstracted_pad->device_type) {
return ResultNpadIsNotProController;
}
}
assignment_list[list_size] = {
.abstracted_pad = abstracted_pad,
.device_type = abstracted_pad->device_type,
.interface_type = abstracted_pad->interface_type,
.controller_id = abstracted_pad->controller_id,
};
list_size++;
return ResultSuccess;
}
void NpadAbstractedPadHolder::RemoveAbstractPadByControllerId(u64 controller_id) {
if (list_size == 0) {
return;
}
if (controller_id == 0) {
return;
}
for (std::size_t i = 0; i < list_size; i++) {
if (assignment_list[i].controller_id != controller_id) {
continue;
}
for (std::size_t e = i + 1; e < list_size; e++) {
assignment_list[e - 1] = assignment_list[e];
}
list_size--;
return;
}
}
void NpadAbstractedPadHolder::DetachAbstractedPad() {
while (list_size > 0) {
for (std::size_t i = 1; i < list_size; i++) {
assignment_list[i - 1] = assignment_list[i];
}
list_size--;
}
}
u64 NpadAbstractedPadHolder::RemoveAbstractPadByAssignmentStyle(
Service::HID::AssignmentStyle assignment_style) {
for (std::size_t i = 0; i < list_size; i++) {
if ((assignment_style.raw & assignment_list[i].abstracted_pad->assignment_style.raw) == 0) {
continue;
}
for (std::size_t e = i + 1; e < list_size; e++) {
assignment_list[e - 1] = assignment_list[e];
}
list_size--;
return list_size;
}
return list_size;
}
u32 NpadAbstractedPadHolder::GetAbstractedPads(std::span<IAbstractedPad*> list) const {
u32 num_elements = std::min(static_cast<u32>(list.size()), list_size);
for (std::size_t i = 0; i < num_elements; i++) {
list[i] = assignment_list[i].abstracted_pad;
}
return num_elements;
}
void NpadAbstractedPadHolder::SetAssignmentMode(const NpadJoyAssignmentMode& mode) {
assignment_mode = mode;
}
NpadJoyAssignmentMode NpadAbstractedPadHolder::GetAssignmentMode() const {
return assignment_mode;
}
std::size_t NpadAbstractedPadHolder::GetStyleIndexList(
std::span<Core::HID::NpadStyleIndex> list) const {
for (std::size_t i = 0; i < list_size; i++) {
list[i] = assignment_list[i].device_type;
}
return list_size;
}
} // namespace Service::HID

View File

@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <array>
#include <mutex>
#include <span>
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
#include "hid_core/resources/npad/npad_types.h"
namespace Service::HID {
struct IAbstractedPad;
struct AbstractAssignmentHolder {
IAbstractedPad* abstracted_pad;
Core::HID::NpadStyleIndex device_type;
Core::HID::NpadInterfaceType interface_type;
INSERT_PADDING_BYTES(0x6);
u64 controller_id;
};
static_assert(sizeof(AbstractAssignmentHolder) == 0x18,
"AbstractAssignmentHolder is an invalid size");
/// This is nn::hid::server::NpadAbstractedPadHolder
class NpadAbstractedPadHolder final {
public:
Result RegisterAbstractPad(IAbstractedPad* abstracted_pad);
void RemoveAbstractPadByControllerId(u64 controller_id);
void DetachAbstractedPad();
u64 RemoveAbstractPadByAssignmentStyle(Service::HID::AssignmentStyle assignment_style);
u32 GetAbstractedPads(std::span<IAbstractedPad*> list) const;
void SetAssignmentMode(const NpadJoyAssignmentMode& mode);
NpadJoyAssignmentMode GetAssignmentMode() const;
std::size_t GetStyleIndexList(std::span<Core::HID::NpadStyleIndex> list) const;
private:
std::array<AbstractAssignmentHolder, 5> assignment_list{};
u32 list_size{};
NpadJoyAssignmentMode assignment_mode{NpadJoyAssignmentMode::Dual};
};
} // namespace Service::HID

View File

@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/hid_result.h"
#include "hid_core/resources/abstracted_pad/abstract_palma_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
namespace Service::HID {
NpadAbstractPalmaHandler::NpadAbstractPalmaHandler() {}
NpadAbstractPalmaHandler::~NpadAbstractPalmaHandler() = default;
void NpadAbstractPalmaHandler::SetAbstractPadHolder(NpadAbstractedPadHolder* holder) {
abstract_pad_holder = holder;
}
void NpadAbstractPalmaHandler::SetPropertiesHandler(NpadAbstractPropertiesHandler* handler) {
properties_handler = handler;
return;
}
void NpadAbstractPalmaHandler::SetPalmaResource(PalmaResource* resource) {
palma_resource = resource;
}
Result NpadAbstractPalmaHandler::IncrementRefCounter() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultNpadHandlerOverflow;
}
ref_counter++;
return ResultSuccess;
}
Result NpadAbstractPalmaHandler::DecrementRefCounter() {
if (ref_counter == 0) {
return ResultNpadHandlerNotInitialized;
}
ref_counter--;
return ResultSuccess;
}
void NpadAbstractPalmaHandler::UpdatePalmaState() {
// TODO
}
} // namespace Service::HID

View File

@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
namespace Service::HID {
class NpadAbstractedPadHolder;
class NpadAbstractPropertiesHandler;
class PalmaResource;
class NpadAbstractPalmaHandler final {
public:
explicit NpadAbstractPalmaHandler();
~NpadAbstractPalmaHandler();
void SetAbstractPadHolder(NpadAbstractedPadHolder* holder);
void SetPropertiesHandler(NpadAbstractPropertiesHandler* handler);
void SetPalmaResource(PalmaResource* resource);
Result IncrementRefCounter();
Result DecrementRefCounter();
void UpdatePalmaState();
private:
NpadAbstractedPadHolder* abstract_pad_holder{nullptr};
NpadAbstractPropertiesHandler* properties_handler{nullptr};
PalmaResource* palma_resource{nullptr};
s32 ref_counter{};
};
} // namespace Service::HID

View File

@@ -0,0 +1,322 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/hid_util.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/npad/npad_resource.h"
#include "hid_core/resources/npad/npad_types.h"
#include "hid_core/resources/shared_memory_format.h"
namespace Service::HID {
NpadAbstractPropertiesHandler::NpadAbstractPropertiesHandler() {}
NpadAbstractPropertiesHandler::~NpadAbstractPropertiesHandler() = default;
void NpadAbstractPropertiesHandler::SetAbstractPadHolder(NpadAbstractedPadHolder* holder) {
abstract_pad_holder = holder;
return;
}
void NpadAbstractPropertiesHandler::SetAppletResource(AppletResourceHolder* applet_resource) {
applet_resource_holder = applet_resource;
return;
}
void NpadAbstractPropertiesHandler::SetNpadId(Core::HID::NpadIdType npad_id) {
if (!IsNpadIdValid(npad_id)) {
ASSERT_MSG(false, "Invalid npad id");
}
npad_id_type = npad_id;
}
Core::HID::NpadIdType NpadAbstractPropertiesHandler::GetNpadId() const {
return npad_id_type;
}
Result NpadAbstractPropertiesHandler::IncrementRefCounter() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultNpadHandlerOverflow;
}
if (ref_counter != 0) {
ref_counter++;
return ResultSuccess;
}
const auto npad_index = NpadIdTypeToIndex(npad_id_type);
for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
auto* data = applet_resource_holder->applet_resource->GetAruidData(aruid_index);
auto& internal_state =
data->shared_memory_format->npad.npad_entry[npad_index].internal_state;
if (!data->flag.is_assigned) {
continue;
}
internal_state.fullkey_lifo.buffer_count = 0;
internal_state.handheld_lifo.buffer_count = 0;
internal_state.joy_dual_lifo.buffer_count = 0;
internal_state.joy_left_lifo.buffer_count = 0;
internal_state.joy_right_lifo.buffer_count = 0;
internal_state.palma_lifo.buffer_count = 0;
internal_state.system_ext_lifo.buffer_count = 0;
internal_state.gc_trigger_lifo.buffer_count = 0;
internal_state.sixaxis_fullkey_lifo.lifo.buffer_count = 0;
internal_state.sixaxis_handheld_lifo.lifo.buffer_count = 0;
internal_state.sixaxis_dual_left_lifo.lifo.buffer_count = 0;
internal_state.sixaxis_dual_right_lifo.lifo.buffer_count = 0;
internal_state.sixaxis_left_lifo.lifo.buffer_count = 0;
internal_state.sixaxis_right_lifo.lifo.buffer_count = 0;
internal_state.style_tag = {Core::HID::NpadStyleSet::None};
internal_state.assignment_mode = NpadJoyAssignmentMode::Dual;
internal_state.joycon_color = {};
internal_state.fullkey_color = {};
internal_state.system_properties.raw = 0;
internal_state.button_properties.raw = 0;
internal_state.device_type.raw = 0;
internal_state.battery_level_dual = Core::HID::NpadBatteryLevel::Empty;
internal_state.battery_level_left = Core::HID::NpadBatteryLevel::Empty;
internal_state.battery_level_right = Core::HID::NpadBatteryLevel::Empty;
internal_state.applet_footer_type = AppletFooterUiType::None;
internal_state.applet_footer_attributes = {};
internal_state.lark_type_l_and_main = {};
internal_state.lark_type_r = {};
internal_state.sixaxis_fullkey_properties.is_newly_assigned.Assign(true);
internal_state.sixaxis_handheld_properties.is_newly_assigned.Assign(true);
internal_state.sixaxis_dual_left_properties.is_newly_assigned.Assign(true);
internal_state.sixaxis_dual_right_properties.is_newly_assigned.Assign(true);
internal_state.sixaxis_left_properties.is_newly_assigned.Assign(true);
internal_state.sixaxis_right_properties.is_newly_assigned.Assign(true);
}
ref_counter++;
return ResultSuccess;
}
Result NpadAbstractPropertiesHandler::DecrementRefCounter() {
if (ref_counter == 0) {
return ResultNpadHandlerNotInitialized;
}
ref_counter--;
return ResultSuccess;
}
Result NpadAbstractPropertiesHandler::ActivateNpadUnknown0x88(u64 aruid) {
const auto npad_index = NpadIdTypeToIndex(npad_id_type);
for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
auto* data = applet_resource_holder->applet_resource->GetAruidData(aruid_index);
if (!data->flag.is_assigned || data->aruid != aruid) {
continue;
}
UpdateDeviceProperties(aruid, data->shared_memory_format->npad.npad_entry[npad_index]);
return ResultSuccess;
}
return ResultSuccess;
}
void NpadAbstractPropertiesHandler::UpdateDeviceType() {
// TODO
}
void NpadAbstractPropertiesHandler::UpdateDeviceColor() {
// TODO
}
void NpadAbstractPropertiesHandler::UpdateFooterAttributes() {
// TODO
}
void NpadAbstractPropertiesHandler::UpdateAllDeviceProperties() {
const auto npad_index = NpadIdTypeToIndex(npad_id_type);
for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
auto* data = applet_resource_holder->applet_resource->GetAruidData(aruid_index);
if (data == nullptr || !data->flag.is_assigned) {
continue;
}
auto& npad_entry = data->shared_memory_format->npad.npad_entry[npad_index];
UpdateDeviceProperties(data->aruid, npad_entry);
}
}
Core::HID::NpadInterfaceType NpadAbstractPropertiesHandler::GetFullkeyInterfaceType() {
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = abstract_pad_holder->GetAbstractedPads(abstract_pads);
for (std::size_t i = 0; i < count; i++) {
auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
if (abstract_pad->device_type != Core::HID::NpadStyleIndex::Fullkey) {
continue;
}
if (abstract_pad->interface_type >= Core::HID::NpadInterfaceType::Embedded) {
// Abort
continue;
}
return abstract_pad->interface_type;
}
return Core::HID::NpadInterfaceType::None;
}
Core::HID::NpadInterfaceType NpadAbstractPropertiesHandler::GetInterfaceType() {
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = abstract_pad_holder->GetAbstractedPads(abstract_pads);
for (std::size_t i = 0; i < count; i++) {
auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
if (!abstract_pad->disabled_feature_set.has_identification_code) {
continue;
}
if (abstract_pad->interface_type >= Core::HID::NpadInterfaceType::Embedded) {
// Abort
continue;
}
return abstract_pad->interface_type;
}
return Core::HID::NpadInterfaceType::None;
}
Core::HID::NpadStyleSet NpadAbstractPropertiesHandler::GetStyleSet(u64 aruid) {
// TODO
return Core::HID::NpadStyleSet::None;
}
std::size_t NpadAbstractPropertiesHandler::GetAbstractedPadsWithStyleTag(
std::span<IAbstractedPad*> list, Core::HID::NpadStyleTag style) {
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = abstract_pad_holder->GetAbstractedPads(abstract_pads);
if (count == 0) {
return count;
}
bool is_supported_style_set{};
const auto result = applet_resource_holder->shared_npad_resource->IsSupportedNpadStyleSet(
is_supported_style_set, applet_resource_holder->applet_resource->GetActiveAruid());
if (!is_supported_style_set || result.IsError()) {
for (std::size_t i = 0; i < count; i++) {
// TODO
}
return count;
}
std::size_t filtered_count{};
for (std::size_t i = 0; i < count; i++) {
auto* abstract_pad = abstract_pads[i];
const bool is_enabled = true;
if (is_enabled) {
list[filtered_count] = abstract_pad;
filtered_count++;
}
}
return filtered_count;
}
std::size_t NpadAbstractPropertiesHandler::GetAbstractedPads(std::span<IAbstractedPad*> list) {
Core::HID::NpadStyleTag style{
GetStyleSet(applet_resource_holder->applet_resource->GetActiveAruid())};
return GetAbstractedPadsWithStyleTag(list, style);
}
AppletFooterUiType NpadAbstractPropertiesHandler::GetAppletFooterUiType() {
return applet_ui_type.footer;
}
AppletDetailedUiType NpadAbstractPropertiesHandler::GetAppletDetailedUiType() {
return applet_ui_type;
}
void NpadAbstractPropertiesHandler::UpdateDeviceProperties(u64 aruid,
NpadSharedMemoryEntry& internal_state) {
// TODO
}
Core::HID::NpadInterfaceType NpadAbstractPropertiesHandler::GetNpadInterfaceType() {
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = abstract_pad_holder->GetAbstractedPads(abstract_pads);
for (std::size_t i = 0; i < count; i++) {
auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
if (abstract_pad->interface_type >= Core::HID::NpadInterfaceType::Embedded) {
// Abort
continue;
}
return abstract_pad->interface_type;
}
return Core::HID::NpadInterfaceType::None;
}
Result NpadAbstractPropertiesHandler::GetNpadFullKeyGripColor(
Core::HID::NpadColor& main_color, Core::HID::NpadColor& sub_color) const {
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = abstract_pad_holder->GetAbstractedPads(abstract_pads);
if (applet_ui_type.footer != AppletFooterUiType::SwitchProController) {
return ResultNpadIsNotProController;
}
for (std::size_t i = 0; i < count; i++) {
auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
return ResultSuccess;
}
return ResultNpadIsNotProController;
}
void NpadAbstractPropertiesHandler::GetNpadLeftRightInterfaceType(
Core::HID::NpadInterfaceType& out_left_interface,
Core::HID::NpadInterfaceType& out_right_interface) const {
out_left_interface = Core::HID::NpadInterfaceType::None;
out_right_interface = Core::HID::NpadInterfaceType::None;
std::array<IAbstractedPad*, 5> abstract_pads{};
const std::size_t count = abstract_pad_holder->GetAbstractedPads(abstract_pads);
for (std::size_t i = 0; i < count; i++) {
auto* abstract_pad = abstract_pads[i];
if (!abstract_pad->internal_flags.is_connected) {
continue;
}
if (abstract_pad->assignment_style.is_external_left_assigned &&
abstract_pad->assignment_style.is_handheld_left_assigned) {
if (abstract_pad->interface_type > Core::HID::NpadInterfaceType::Embedded) {
// Abort
continue;
}
out_left_interface = abstract_pad->interface_type;
continue;
}
if (abstract_pad->assignment_style.is_external_right_assigned &&
abstract_pad->assignment_style.is_handheld_right_assigned) {
if (abstract_pad->interface_type > Core::HID::NpadInterfaceType::Embedded) {
// Abort
continue;
}
out_right_interface = abstract_pad->interface_type;
continue;
}
}
}
} // namespace Service::HID

View File

@@ -0,0 +1,86 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <span>
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
#include "hid_core/resources/npad/npad_types.h"
namespace Service::HID {
struct NpadSharedMemoryEntry;
struct AppletResourceHolder;
class NpadAbstractedPadHolder;
struct ColorProperties {
ColorAttribute attribute;
Core::HID::NpadControllerColor color;
INSERT_PADDING_BYTES(0x4);
};
/// Handles Npad request from HID interfaces
class NpadAbstractPropertiesHandler final {
public:
explicit NpadAbstractPropertiesHandler();
~NpadAbstractPropertiesHandler();
void SetAbstractPadHolder(NpadAbstractedPadHolder* holder);
void SetAppletResource(AppletResourceHolder* applet_resource);
void SetNpadId(Core::HID::NpadIdType npad_id);
Core::HID::NpadIdType GetNpadId() const;
Result IncrementRefCounter();
Result DecrementRefCounter();
Result ActivateNpadUnknown0x88(u64 aruid);
void UpdateDeviceType();
void UpdateDeviceColor();
void UpdateFooterAttributes();
void UpdateAllDeviceProperties();
Core::HID::NpadInterfaceType GetFullkeyInterfaceType();
Core::HID::NpadInterfaceType GetInterfaceType();
Core::HID::NpadStyleSet GetStyleSet(u64 aruid);
std::size_t GetAbstractedPadsWithStyleTag(std::span<IAbstractedPad*> list,
Core::HID::NpadStyleTag style);
std::size_t GetAbstractedPads(std::span<IAbstractedPad*> list);
AppletFooterUiType GetAppletFooterUiType();
AppletDetailedUiType GetAppletDetailedUiType();
void UpdateDeviceProperties(u64 aruid, NpadSharedMemoryEntry& internal_state);
Core::HID::NpadInterfaceType GetNpadInterfaceType();
Result GetNpadFullKeyGripColor(Core::HID::NpadColor& main_color,
Core::HID::NpadColor& sub_color) const;
void GetNpadLeftRightInterfaceType(Core::HID::NpadInterfaceType& param_2,
Core::HID::NpadInterfaceType& param_3) const;
private:
AppletResourceHolder* applet_resource_holder{nullptr};
NpadAbstractedPadHolder* abstract_pad_holder{nullptr};
Core::HID::NpadIdType npad_id_type{Core::HID::NpadIdType::Invalid};
s32 ref_counter{};
Core::HID::DeviceIndex device_type{};
AppletDetailedUiType applet_ui_type{};
AppletFooterUiAttributes applet_ui_attributes{};
bool is_vertical{};
bool is_horizontal{};
bool use_plus{};
bool use_minus{};
bool has_directional_buttons{};
ColorProperties fullkey_color{};
ColorProperties left_color{};
ColorProperties right_color{};
};
} // namespace Service::HID

View File

@@ -0,0 +1,154 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/hid_result.h"
#include "hid_core/hid_util.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_sixaxis_handler.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/npad/npad_types.h"
#include "hid_core/resources/shared_memory_format.h"
namespace Service::HID {
NpadAbstractSixAxisHandler::NpadAbstractSixAxisHandler() {}
NpadAbstractSixAxisHandler::~NpadAbstractSixAxisHandler() = default;
void NpadAbstractSixAxisHandler::SetAbstractPadHolder(NpadAbstractedPadHolder* holder) {
abstract_pad_holder = holder;
}
void NpadAbstractSixAxisHandler::SetAppletResource(AppletResourceHolder* applet_resource) {
applet_resource_holder = applet_resource;
}
void NpadAbstractSixAxisHandler::SetPropertiesHandler(NpadAbstractPropertiesHandler* handler) {
properties_handler = handler;
}
void NpadAbstractSixAxisHandler::SetSixaxisResource(SixAxisResource* resource) {
six_axis_resource = resource;
}
Result NpadAbstractSixAxisHandler::IncrementRefCounter() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultNpadHandlerOverflow;
}
ref_counter++;
return ResultSuccess;
}
Result NpadAbstractSixAxisHandler::DecrementRefCounter() {
if (ref_counter == 0) {
return ResultNpadHandlerNotInitialized;
}
ref_counter--;
return ResultSuccess;
}
u64 NpadAbstractSixAxisHandler::IsFirmwareUpdateAvailable() {
// TODO
return false;
}
Result NpadAbstractSixAxisHandler::UpdateSixAxisState() {
Core::HID::NpadIdType npad_id = properties_handler->GetNpadId();
for (std::size_t i = 0; i < AruidIndexMax; i++) {
auto* data = applet_resource_holder->applet_resource->GetAruidDataByIndex(i);
if (data == nullptr || !data->flag.is_assigned) {
continue;
}
auto& npad_entry = data->shared_memory_format->npad.npad_entry[NpadIdTypeToIndex(npad_id)];
UpdateSixaxisInternalState(npad_entry, data->aruid,
data->flag.enable_six_axis_sensor.As<bool>());
}
return ResultSuccess;
}
Result NpadAbstractSixAxisHandler::UpdateSixAxisState(u64 aruid) {
Core::HID::NpadIdType npad_id = properties_handler->GetNpadId();
auto* data = applet_resource_holder->applet_resource->GetAruidData(aruid);
if (data == nullptr) {
return ResultSuccess;
}
auto& npad_entry = data->shared_memory_format->npad.npad_entry[NpadIdTypeToIndex(npad_id)];
UpdateSixaxisInternalState(npad_entry, data->aruid,
data->flag.enable_six_axis_sensor.As<bool>());
return ResultSuccess;
}
Result NpadAbstractSixAxisHandler::UpdateSixAxisState2(u64 aruid) {
const auto npad_index = NpadIdTypeToIndex(properties_handler->GetNpadId());
AruidData* aruid_data = applet_resource_holder->applet_resource->GetAruidData(aruid);
if (aruid_data == nullptr) {
return ResultSuccess;
}
auto& npad_internal_state = aruid_data->shared_memory_format->npad.npad_entry[npad_index];
UpdateSixaxisInternalState(npad_internal_state, aruid,
aruid_data->flag.enable_six_axis_sensor.As<bool>());
return ResultSuccess;
}
void NpadAbstractSixAxisHandler::UpdateSixaxisInternalState(NpadSharedMemoryEntry& npad_entry,
u64 aruid, bool is_sensor_enabled) {
const Core::HID::NpadStyleTag style_tag{properties_handler->GetStyleSet(aruid)};
if (!style_tag.palma) {
UpdateSixaxisFullkeyLifo(style_tag, npad_entry.internal_state.sixaxis_fullkey_lifo,
is_sensor_enabled);
} else {
UpdateSixAxisPalmaLifo(style_tag, npad_entry.internal_state.sixaxis_fullkey_lifo,
is_sensor_enabled);
}
UpdateSixaxisHandheldLifo(style_tag, npad_entry.internal_state.sixaxis_handheld_lifo,
is_sensor_enabled);
UpdateSixaxisDualLifo(style_tag, npad_entry.internal_state.sixaxis_dual_left_lifo,
is_sensor_enabled);
UpdateSixaxisDualLifo(style_tag, npad_entry.internal_state.sixaxis_dual_right_lifo,
is_sensor_enabled);
UpdateSixaxisLeftLifo(style_tag, npad_entry.internal_state.sixaxis_left_lifo,
is_sensor_enabled);
UpdateSixaxisRightLifo(style_tag, npad_entry.internal_state.sixaxis_right_lifo,
is_sensor_enabled);
// TODO: Set sixaxis properties
}
void NpadAbstractSixAxisHandler::UpdateSixaxisFullkeyLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo,
bool is_sensor_enabled) {
// TODO
}
void NpadAbstractSixAxisHandler::UpdateSixAxisPalmaLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo,
bool is_sensor_enabled) {
// TODO
}
void NpadAbstractSixAxisHandler::UpdateSixaxisHandheldLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo,
bool is_sensor_enabled) {
// TODO
}
void NpadAbstractSixAxisHandler::UpdateSixaxisDualLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo,
bool is_sensor_enabled) {
// TODO
}
void NpadAbstractSixAxisHandler::UpdateSixaxisLeftLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo,
bool is_sensor_enabled) {
// TODO
}
void NpadAbstractSixAxisHandler::UpdateSixaxisRightLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo,
bool is_sensor_enabled) {
// TODO
}
} // namespace Service::HID

View File

@@ -0,0 +1,61 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
namespace Service::HID {
class SixAxisResource;
struct AppletResourceHolder;
class NpadAbstractedPadHolder;
class NpadAbstractPropertiesHandler;
struct NpadSixAxisSensorLifo;
/// Handles Npad request from HID interfaces
class NpadAbstractSixAxisHandler final {
public:
explicit NpadAbstractSixAxisHandler();
~NpadAbstractSixAxisHandler();
void SetAbstractPadHolder(NpadAbstractedPadHolder* holder);
void SetAppletResource(AppletResourceHolder* applet_resource);
void SetPropertiesHandler(NpadAbstractPropertiesHandler* handler);
void SetSixaxisResource(SixAxisResource* resource);
Result IncrementRefCounter();
Result DecrementRefCounter();
u64 IsFirmwareUpdateAvailable();
Result UpdateSixAxisState();
Result UpdateSixAxisState(u64 aruid);
Result UpdateSixAxisState2(u64 aruid);
private:
void UpdateSixaxisInternalState(NpadSharedMemoryEntry& npad_entry, u64 aruid,
bool is_sensor_enabled);
void UpdateSixaxisFullkeyLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo, bool is_sensor_enabled);
void UpdateSixAxisPalmaLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo, bool is_sensor_enabled);
void UpdateSixaxisHandheldLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo, bool is_sensor_enabled);
void UpdateSixaxisDualLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo, bool is_sensor_enabled);
void UpdateSixaxisLeftLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo, bool is_sensor_enabled);
void UpdateSixaxisRightLifo(Core::HID::NpadStyleTag style_tag,
NpadSixAxisSensorLifo& sensor_lifo, bool is_sensor_enabled);
AppletResourceHolder* applet_resource_holder{nullptr};
NpadAbstractedPadHolder* abstract_pad_holder{nullptr};
NpadAbstractPropertiesHandler* properties_handler{nullptr};
SixAxisResource* six_axis_resource{nullptr};
s32 ref_counter{};
};
} // namespace Service::HID

View File

@@ -0,0 +1,107 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/frontend/emulated_controller.h"
#include "hid_core/hid_core.h"
#include "hid_core/hid_result.h"
#include "hid_core/hid_util.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_vibration_handler.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/npad/npad_vibration.h"
#include "hid_core/resources/vibration/gc_vibration_device.h"
#include "hid_core/resources/vibration/n64_vibration_device.h"
#include "hid_core/resources/vibration/vibration_device.h"
namespace Service::HID {
NpadAbstractVibrationHandler::NpadAbstractVibrationHandler() {}
NpadAbstractVibrationHandler::~NpadAbstractVibrationHandler() = default;
void NpadAbstractVibrationHandler::SetAbstractPadHolder(NpadAbstractedPadHolder* holder) {
abstract_pad_holder = holder;
}
void NpadAbstractVibrationHandler::SetAppletResource(AppletResourceHolder* applet_resource) {
applet_resource_holder = applet_resource;
}
void NpadAbstractVibrationHandler::SetPropertiesHandler(NpadAbstractPropertiesHandler* handler) {
properties_handler = handler;
}
void NpadAbstractVibrationHandler::SetVibrationHandler(NpadVibration* handler) {
vibration_handler = handler;
}
void NpadAbstractVibrationHandler::SetHidCore(Core::HID::HIDCore* core) {
hid_core = core;
}
void NpadAbstractVibrationHandler::SetN64Vibration(NpadN64VibrationDevice* n64_device) {
n64_vibration_device = n64_device;
}
void NpadAbstractVibrationHandler::SetVibration(NpadVibrationDevice* left_device,
NpadVibrationDevice* right_device) {
left_vibration_device = left_device;
right_vibration_device = right_device;
}
void NpadAbstractVibrationHandler::SetGcVibration(NpadGcVibrationDevice* gc_device) {
gc_vibration_device = gc_device;
}
Result NpadAbstractVibrationHandler::IncrementRefCounter() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultNpadHandlerOverflow;
}
ref_counter++;
return ResultSuccess;
}
Result NpadAbstractVibrationHandler::DecrementRefCounter() {
if (ref_counter == 0) {
return ResultNpadHandlerNotInitialized;
}
ref_counter--;
return ResultSuccess;
}
void NpadAbstractVibrationHandler::UpdateVibrationState() {
const bool is_handheld_hid_enabled =
applet_resource_holder->handheld_config->is_handheld_hid_enabled;
const bool is_force_handheld_style_vibration =
applet_resource_holder->handheld_config->is_force_handheld_style_vibration;
if (!is_handheld_hid_enabled && is_force_handheld_style_vibration) {
// TODO
}
// TODO: This function isn't accurate. It's supposed to get 5 abstracted pads from the
// NpadAbstractPropertiesHandler but this handler isn't fully implemented yet
IAbstractedPad abstracted_pad{};
const auto npad_id = properties_handler->GetNpadId();
abstracted_pad.xcd_handle = hid_core->GetEmulatedController(npad_id);
abstracted_pad.internal_flags.is_connected.Assign(abstracted_pad.xcd_handle->IsConnected());
if (abstracted_pad.internal_flags.is_connected) {
left_vibration_device->Mount(abstracted_pad, Core::HID::DeviceIndex::Left,
vibration_handler);
right_vibration_device->Mount(abstracted_pad, Core::HID::DeviceIndex::Right,
vibration_handler);
gc_vibration_device->Mount(abstracted_pad, 0, vibration_handler);
gc_vibration_device->Mount(abstracted_pad, 0, vibration_handler);
n64_vibration_device->Mount(abstracted_pad, vibration_handler);
return;
}
left_vibration_device->Unmount();
right_vibration_device->Unmount();
gc_vibration_device->Unmount();
gc_vibration_device->Unmount();
n64_vibration_device->Unmount();
}
} // namespace Service::HID

View File

@@ -0,0 +1,59 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <span>
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
namespace Core::HID {
class HIDCore;
}
namespace Service::HID {
struct AppletResourceHolder;
class NpadAbstractedPadHolder;
class NpadAbstractPropertiesHandler;
class NpadGcVibrationDevice;
class NpadVibrationDevice;
class NpadN64VibrationDevice;
class NpadVibration;
/// Keeps track of battery levels and updates npad battery shared memory values
class NpadAbstractVibrationHandler final {
public:
explicit NpadAbstractVibrationHandler();
~NpadAbstractVibrationHandler();
void SetAbstractPadHolder(NpadAbstractedPadHolder* holder);
void SetAppletResource(AppletResourceHolder* applet_resource);
void SetPropertiesHandler(NpadAbstractPropertiesHandler* handler);
void SetVibrationHandler(NpadVibration* handler);
void SetHidCore(Core::HID::HIDCore* core);
void SetN64Vibration(NpadN64VibrationDevice* n64_device);
void SetVibration(NpadVibrationDevice* left_device, NpadVibrationDevice* right_device);
void SetGcVibration(NpadGcVibrationDevice* gc_device);
Result IncrementRefCounter();
Result DecrementRefCounter();
void UpdateVibrationState();
private:
AppletResourceHolder* applet_resource_holder{nullptr};
NpadAbstractedPadHolder* abstract_pad_holder{nullptr};
NpadAbstractPropertiesHandler* properties_handler{nullptr};
Core::HID::HIDCore* hid_core{nullptr};
NpadN64VibrationDevice* n64_vibration_device{nullptr};
NpadVibrationDevice* left_vibration_device{};
NpadVibrationDevice* right_vibration_device{};
NpadGcVibrationDevice* gc_vibration_device{nullptr};
NpadVibration* vibration_handler{nullptr};
s32 ref_counter{};
};
} // namespace Service::HID

View File

@@ -0,0 +1,349 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/core.h"
#include "core/hle/kernel/k_shared_memory.h"
#include "hid_core/hid_result.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/shared_memory_format.h"
namespace Service::HID {
AppletResource::AppletResource(Core::System& system_) : system{system_} {}
AppletResource::~AppletResource() = default;
Result AppletResource::CreateAppletResource(u64 aruid) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return ResultAruidNotRegistered;
}
if (data[index].flag.is_assigned) {
return ResultAruidAlreadyRegistered;
}
auto& shared_memory = shared_memory_holder[index];
if (!shared_memory.IsMapped()) {
const Result result = shared_memory.Initialize(system);
if (result.IsError()) {
return result;
}
if (shared_memory.GetAddress() == nullptr) {
shared_memory.Finalize();
return ResultSharedMemoryNotInitialized;
}
}
auto* shared_memory_format = shared_memory.GetAddress();
if (shared_memory_format != nullptr) {
shared_memory_format->Initialize();
}
data[index].shared_memory_format = shared_memory_format;
data[index].flag.is_assigned.Assign(true);
// TODO: InitializeSixAxisControllerConfig(false);
active_aruid = aruid;
return ResultSuccess;
}
Result AppletResource::RegisterAppletResourceUserId(u64 aruid, bool enable_input) {
const u64 index = GetIndexFromAruid(aruid);
if (index < AruidIndexMax) {
return ResultAruidAlreadyRegistered;
}
std::size_t data_index = AruidIndexMax;
for (std::size_t i = 0; i < AruidIndexMax; i++) {
if (!data[i].flag.is_initialized) {
data_index = i;
break;
}
}
if (data_index == AruidIndexMax) {
return ResultAruidNoAvailableEntries;
}
AruidData& aruid_data = data[data_index];
aruid_data.aruid = aruid;
aruid_data.flag.is_initialized.Assign(true);
if (enable_input) {
aruid_data.flag.enable_pad_input.Assign(true);
aruid_data.flag.enable_six_axis_sensor.Assign(true);
aruid_data.flag.bit_18.Assign(true);
aruid_data.flag.enable_touchscreen.Assign(true);
}
data_index = AruidIndexMax;
for (std::size_t i = 0; i < AruidIndexMax; i++) {
if (registration_list.flag[i] == RegistrationStatus::Initialized) {
if (registration_list.aruid[i] != aruid) {
continue;
}
data_index = i;
break;
}
// TODO: Don't Handle pending delete here
if (registration_list.flag[i] == RegistrationStatus::None ||
registration_list.flag[i] == RegistrationStatus::PendingDelete) {
data_index = i;
break;
}
}
if (data_index == AruidIndexMax) {
return ResultSuccess;
}
registration_list.flag[data_index] = RegistrationStatus::Initialized;
registration_list.aruid[data_index] = aruid;
return ResultSuccess;
}
void AppletResource::UnregisterAppletResourceUserId(u64 aruid) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return;
}
FreeAppletResourceId(aruid);
DestroySevenSixAxisTransferMemory();
data[index].flag.raw = 0;
data[index].aruid = 0;
registration_list.flag[index] = RegistrationStatus::PendingDelete;
for (std::size_t i = 0; i < AruidIndexMax; i++) {
if (registration_list.flag[i] == RegistrationStatus::Initialized) {
active_aruid = registration_list.aruid[i];
}
}
}
void AppletResource::FreeAppletResourceId(u64 aruid) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return;
}
auto& aruid_data = data[index];
if (aruid_data.flag.is_assigned) {
aruid_data.shared_memory_format = nullptr;
aruid_data.flag.is_assigned.Assign(false);
shared_memory_holder[index].Finalize();
}
}
u64 AppletResource::GetActiveAruid() {
return active_aruid;
}
Result AppletResource::GetSharedMemoryHandle(Kernel::KSharedMemory** out_handle, u64 aruid) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return ResultAruidNotRegistered;
}
*out_handle = shared_memory_holder[index].GetHandle();
return ResultSuccess;
}
Result AppletResource::GetSharedMemoryFormat(SharedMemoryFormat** out_shared_memory_format,
u64 aruid) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return ResultAruidNotRegistered;
}
*out_shared_memory_format = data[index].shared_memory_format;
return ResultSuccess;
}
AruidData* AppletResource::GetAruidData(u64 aruid) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index == AruidIndexMax) {
return nullptr;
}
return &data[aruid_index];
}
AruidData* AppletResource::GetAruidDataByIndex(std::size_t aruid_index) {
return &data[aruid_index];
}
bool AppletResource::IsVibrationAruidActive(u64 aruid) const {
return aruid == 0 || aruid == active_vibration_aruid;
}
u64 AppletResource::GetIndexFromAruid(u64 aruid) {
for (std::size_t i = 0; i < AruidIndexMax; i++) {
if (registration_list.flag[i] == RegistrationStatus::Initialized &&
registration_list.aruid[i] == aruid) {
return i;
}
}
return AruidIndexMax;
}
Result AppletResource::DestroySevenSixAxisTransferMemory() {
// TODO
return ResultSuccess;
}
void AppletResource::EnableInput(u64 aruid, bool is_enabled) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return;
}
data[index].flag.enable_pad_input.Assign(is_enabled);
data[index].flag.enable_touchscreen.Assign(is_enabled);
}
bool AppletResource::SetAruidValidForVibration(u64 aruid, bool is_enabled) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return false;
}
if (!is_enabled && aruid == active_vibration_aruid) {
active_vibration_aruid = SystemAruid;
return true;
}
if (is_enabled && aruid != active_vibration_aruid) {
active_vibration_aruid = aruid;
return true;
}
return false;
}
void AppletResource::EnableSixAxisSensor(u64 aruid, bool is_enabled) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return;
}
data[index].flag.enable_six_axis_sensor.Assign(is_enabled);
}
void AppletResource::EnablePadInput(u64 aruid, bool is_enabled) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return;
}
data[index].flag.enable_pad_input.Assign(is_enabled);
}
void AppletResource::EnableTouchScreen(u64 aruid, bool is_enabled) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return;
}
data[index].flag.enable_touchscreen.Assign(is_enabled);
}
void AppletResource::SetIsPalmaConnectable(u64 aruid, bool is_connectable) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return;
}
data[index].flag.is_palma_connectable.Assign(is_connectable);
}
void AppletResource::EnablePalmaBoostMode(u64 aruid, bool is_enabled) {
const u64 index = GetIndexFromAruid(aruid);
if (index >= AruidIndexMax) {
return;
}
data[index].flag.enable_palma_boost_mode.Assign(is_enabled);
}
Result AppletResource::RegisterCoreAppletResource() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultAppletResourceOverflow;
}
if (ref_counter == 0) {
const u64 index = GetIndexFromAruid(0);
if (index < AruidIndexMax) {
return ResultAruidAlreadyRegistered;
}
std::size_t data_index = AruidIndexMax;
for (std::size_t i = 0; i < AruidIndexMax; i++) {
if (!data[i].flag.is_initialized) {
data_index = i;
break;
}
}
if (data_index == AruidIndexMax) {
return ResultAruidNoAvailableEntries;
}
AruidData& aruid_data = data[data_index];
aruid_data.aruid = 0;
aruid_data.flag.is_initialized.Assign(true);
aruid_data.flag.enable_pad_input.Assign(true);
aruid_data.flag.enable_six_axis_sensor.Assign(true);
aruid_data.flag.bit_18.Assign(true);
aruid_data.flag.enable_touchscreen.Assign(true);
data_index = AruidIndexMax;
for (std::size_t i = 0; i < AruidIndexMax; i++) {
if (registration_list.flag[i] == RegistrationStatus::Initialized) {
if (registration_list.aruid[i] != 0) {
continue;
}
data_index = i;
break;
}
if (registration_list.flag[i] == RegistrationStatus::None) {
data_index = i;
break;
}
}
Result result = ResultSuccess;
if (data_index == AruidIndexMax) {
result = CreateAppletResource(0);
} else {
registration_list.flag[data_index] = RegistrationStatus::Initialized;
registration_list.aruid[data_index] = 0;
}
if (result.IsError()) {
UnregisterAppletResourceUserId(0);
return result;
}
}
ref_counter++;
return ResultSuccess;
}
Result AppletResource::UnregisterCoreAppletResource() {
if (ref_counter == 0) {
return ResultAppletResourceNotInitialized;
}
if (--ref_counter == 0) {
UnregisterAppletResourceUserId(0);
}
return ResultSuccess;
}
} // namespace Service::HID

View File

@@ -0,0 +1,126 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <array>
#include <mutex>
#include "common/bit_field.h"
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/resources/shared_memory_holder.h"
namespace Core {
class System;
} // namespace Core
namespace Kernel {
class KEvent;
class KSharedMemory;
} // namespace Kernel
namespace Service::HID {
struct SharedMemoryFormat;
class AppletResource;
class NPadResource;
static constexpr std::size_t AruidIndexMax = 0x20;
static constexpr u64 SystemAruid = 0;
enum class RegistrationStatus : u32 {
None,
Initialized,
PendingDelete,
};
struct DataStatusFlag {
union {
u32 raw{};
BitField<0, 1, u32> is_initialized;
BitField<1, 1, u32> is_assigned;
BitField<16, 1, u32> enable_pad_input;
BitField<17, 1, u32> enable_six_axis_sensor;
BitField<18, 1, u32> bit_18;
BitField<19, 1, u32> is_palma_connectable;
BitField<20, 1, u32> enable_palma_boost_mode;
BitField<21, 1, u32> enable_touchscreen;
};
};
struct AruidRegisterList {
std::array<RegistrationStatus, AruidIndexMax> flag{};
std::array<u64, AruidIndexMax> aruid{};
};
static_assert(sizeof(AruidRegisterList) == 0x180, "AruidRegisterList is an invalid size");
struct AruidData {
DataStatusFlag flag{};
u64 aruid{};
SharedMemoryFormat* shared_memory_format{nullptr};
};
struct HandheldConfig {
bool is_handheld_hid_enabled;
bool is_force_handheld;
bool is_joycon_rail_enabled;
bool is_force_handheld_style_vibration;
};
static_assert(sizeof(HandheldConfig) == 0x4, "HandheldConfig is an invalid size");
struct AppletResourceHolder {
std::shared_ptr<AppletResource> applet_resource{nullptr};
std::recursive_mutex* shared_mutex{nullptr};
NPadResource* shared_npad_resource{nullptr};
std::shared_ptr<HandheldConfig> handheld_config{nullptr};
Kernel::KEvent* input_event{nullptr};
std::mutex* input_mutex{nullptr};
};
class AppletResource {
public:
explicit AppletResource(Core::System& system_);
~AppletResource();
Result CreateAppletResource(u64 aruid);
Result RegisterAppletResourceUserId(u64 aruid, bool enable_input);
void UnregisterAppletResourceUserId(u64 aruid);
void FreeAppletResourceId(u64 aruid);
u64 GetActiveAruid();
Result GetSharedMemoryHandle(Kernel::KSharedMemory** out_handle, u64 aruid);
Result GetSharedMemoryFormat(SharedMemoryFormat** out_shared_memory_format, u64 aruid);
AruidData* GetAruidData(u64 aruid);
AruidData* GetAruidDataByIndex(std::size_t aruid_index);
bool IsVibrationAruidActive(u64 aruid) const;
u64 GetIndexFromAruid(u64 aruid);
Result DestroySevenSixAxisTransferMemory();
void EnableInput(u64 aruid, bool is_enabled);
bool SetAruidValidForVibration(u64 aruid, bool is_enabled);
void EnableSixAxisSensor(u64 aruid, bool is_enabled);
void EnablePadInput(u64 aruid, bool is_enabled);
void EnableTouchScreen(u64 aruid, bool is_enabled);
void SetIsPalmaConnectable(u64 aruid, bool is_connectable);
void EnablePalmaBoostMode(u64 aruid, bool is_enabled);
Result RegisterCoreAppletResource();
Result UnregisterCoreAppletResource();
private:
u64 active_aruid{};
AruidRegisterList registration_list{};
std::array<AruidData, AruidIndexMax> data{};
std::array<SharedMemoryHolder, AruidIndexMax> shared_memory_holder{};
s32 ref_counter{};
u64 active_vibration_aruid;
Core::System& system;
};
} // namespace Service::HID

View File

@@ -0,0 +1,41 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "hid_core/resources/controller_base.h"
namespace Service::HID {
ControllerBase::ControllerBase(Core::HID::HIDCore& hid_core_) : hid_core(hid_core_) {}
ControllerBase::~ControllerBase() = default;
Result ControllerBase::Activate() {
if (is_activated) {
return ResultSuccess;
}
is_activated = true;
OnInit();
return ResultSuccess;
}
Result ControllerBase::Activate(u64 aruid) {
return Activate();
}
void ControllerBase::DeactivateController() {
if (is_activated) {
OnRelease();
}
is_activated = false;
}
bool ControllerBase::IsControllerActivated() const {
return is_activated;
}
void ControllerBase::SetAppletResource(std::shared_ptr<AppletResource> resource,
std::recursive_mutex* resource_mutex) {
applet_resource = resource;
shared_mutex = resource_mutex;
}
} // namespace Service::HID

View File

@@ -0,0 +1,55 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <memory>
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/resources/applet_resource.h"
namespace Core::Timing {
class CoreTiming;
}
namespace Core::HID {
class HIDCore;
} // namespace Core::HID
namespace Service::HID {
class ControllerBase {
public:
explicit ControllerBase(Core::HID::HIDCore& hid_core_);
virtual ~ControllerBase();
// Called when the controller is initialized
virtual void OnInit() = 0;
// When the controller is released
virtual void OnRelease() = 0;
// When the controller is requesting an update for the shared memory
virtual void OnUpdate(const Core::Timing::CoreTiming& core_timing) = 0;
// When the controller is requesting a motion update for the shared memory
virtual void OnMotionUpdate(const Core::Timing::CoreTiming& core_timing) {}
Result Activate();
Result Activate(u64 aruid);
void DeactivateController();
bool IsControllerActivated() const;
void SetAppletResource(std::shared_ptr<AppletResource> resource,
std::recursive_mutex* resource_mutex);
protected:
bool is_activated{false};
std::shared_ptr<AppletResource> applet_resource{nullptr};
std::recursive_mutex* shared_mutex{nullptr};
Core::HID::HIDCore& hid_core;
};
} // namespace Service::HID

View File

@@ -0,0 +1,59 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "common/settings.h"
#include "core/core_timing.h"
#include "hid_core/frontend/emulated_controller.h"
#include "hid_core/hid_core.h"
#include "hid_core/hid_types.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/debug_pad/debug_pad.h"
#include "hid_core/resources/shared_memory_format.h"
namespace Service::HID {
DebugPad::DebugPad(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {
controller = hid_core.GetEmulatedController(Core::HID::NpadIdType::Other);
}
DebugPad::~DebugPad() = default;
void DebugPad::OnInit() {}
void DebugPad::OnRelease() {}
void DebugPad::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
std::scoped_lock shared_lock{*shared_mutex};
const u64 aruid = applet_resource->GetActiveAruid();
auto* data = applet_resource->GetAruidData(aruid);
if (data == nullptr || !data->flag.is_assigned) {
return;
}
DebugPadSharedMemoryFormat& shared_memory = data->shared_memory_format->debug_pad;
if (!IsControllerActivated()) {
shared_memory.debug_pad_lifo.buffer_count = 0;
shared_memory.debug_pad_lifo.buffer_tail = 0;
return;
}
const auto& last_entry = shared_memory.debug_pad_lifo.ReadCurrentEntry().state;
next_state.sampling_number = last_entry.sampling_number + 1;
if (Settings::values.debug_pad_enabled) {
next_state.attribute.connected.Assign(1);
const auto& button_state = controller->GetDebugPadButtons();
const auto& stick_state = controller->GetSticks();
next_state.pad_state = button_state;
next_state.l_stick = stick_state.left;
next_state.r_stick = stick_state.right;
}
shared_memory.debug_pad_lifo.WriteNextEntry(next_state);
}
} // namespace Service::HID

View File

@@ -0,0 +1,37 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "hid_core/resources/controller_base.h"
#include "hid_core/resources/debug_pad/debug_pad_types.h"
namespace Core::HID {
class HIDCore;
class EmulatedController;
} // namespace Core::HID
namespace Core::Timing {
class CoreTiming;
}
namespace Service::HID {
class DebugPad final : public ControllerBase {
public:
explicit DebugPad(Core::HID::HIDCore& hid_core_);
~DebugPad() override;
// Called when the controller is initialized
void OnInit() override;
// When the controller is released
void OnRelease() override;
// When the controller is requesting an update for the shared memory
void OnUpdate(const Core::Timing::CoreTiming& core_timing) override;
private:
DebugPadState next_state{};
Core::HID::EmulatedController* controller = nullptr;
};
} // namespace Service::HID

View File

@@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/bit_field.h"
#include "common/common_types.h"
#include "hid_core/hid_types.h"
namespace Service::HID {
// This is nn::hid::DebugPadAttribute
struct DebugPadAttribute {
union {
u32 raw{};
BitField<0, 1, u32> connected;
};
};
static_assert(sizeof(DebugPadAttribute) == 0x4, "DebugPadAttribute is an invalid size");
// This is nn::hid::DebugPadState
struct DebugPadState {
s64 sampling_number{};
DebugPadAttribute attribute{};
Core::HID::DebugPadButton pad_state{};
Core::HID::AnalogStickState r_stick{};
Core::HID::AnalogStickState l_stick{};
};
static_assert(sizeof(DebugPadState) == 0x20, "DebugPadState is an invalid state");
} // namespace Service::HID

View File

@@ -0,0 +1,35 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "core/core_timing.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/digitizer/digitizer.h"
#include "hid_core/resources/shared_memory_format.h"
namespace Service::HID {
Digitizer::Digitizer(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {}
Digitizer::~Digitizer() = default;
void Digitizer::OnInit() {}
void Digitizer::OnRelease() {}
void Digitizer::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
std::scoped_lock shared_lock{*shared_mutex};
const u64 aruid = applet_resource->GetActiveAruid();
auto* data = applet_resource->GetAruidData(aruid);
if (data == nullptr || !data->flag.is_assigned) {
return;
}
auto& header = data->shared_memory_format->digitizer.header;
header.timestamp = core_timing.GetGlobalTimeNs().count();
header.total_entry_count = 17;
header.entry_count = 0;
header.last_entry_index = 0;
}
} // namespace Service::HID

View File

@@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "hid_core/resources/controller_base.h"
namespace Service::HID {
class Digitizer final : public ControllerBase {
public:
explicit Digitizer(Core::HID::HIDCore& hid_core_);
~Digitizer() override;
// Called when the controller is initialized
void OnInit() override;
// When the controller is released
void OnRelease() override;
// When the controller is requesting an update for the shared memory
void OnUpdate(const Core::Timing::CoreTiming& core_timing) override;
};
} // namespace Service::HID

View File

@@ -0,0 +1,120 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/hle/service/set/system_settings_server.h"
#include "core/hle/service/sm/sm.h"
#include "hid_core/resources/hid_firmware_settings.h"
namespace Service::HID {
HidFirmwareSettings::HidFirmwareSettings(Core::System& system) {
m_set_sys =
system.ServiceManager().GetService<Service::Set::ISystemSettingsServer>("set:sys", true);
LoadSettings(true);
}
void HidFirmwareSettings::Reload() {
LoadSettings(true);
}
void HidFirmwareSettings::LoadSettings(bool reload_config) {
if (is_initialized && !reload_config) {
return;
}
m_set_sys->GetSettingsItemValueImpl<bool>(is_debug_pad_enabled, "hid_debug",
"enables_debugpad");
m_set_sys->GetSettingsItemValueImpl<bool>(is_device_managed, "hid_debug", "manages_devices");
m_set_sys->GetSettingsItemValueImpl<bool>(is_touch_i2c_managed, "hid_debug",
"manages_touch_ic_i2c");
m_set_sys->GetSettingsItemValueImpl<bool>(is_future_devices_emulated, "hid_debug",
"emulate_future_device");
m_set_sys->GetSettingsItemValueImpl<bool>(is_mcu_hardware_error_emulated, "hid_debug",
"emulate_mcu_hardware_error");
m_set_sys->GetSettingsItemValueImpl<bool>(is_rail_enabled, "hid_debug", "enables_rail");
m_set_sys->GetSettingsItemValueImpl<bool>(is_firmware_update_failure_emulated, "hid_debug",
"emulate_firmware_update_failure");
is_firmware_update_failure = {};
m_set_sys->GetSettingsItemValueImpl<bool>(is_ble_disabled, "hid_debug", "ble_disabled");
m_set_sys->GetSettingsItemValueImpl<bool>(is_dscale_disabled, "hid_debug", "dscale_disabled");
m_set_sys->GetSettingsItemValueImpl<bool>(is_handheld_forced, "hid_debug", "force_handheld");
features_per_id_disabled = {};
m_set_sys->GetSettingsItemValueImpl<bool>(is_touch_firmware_auto_update_disabled, "hid_debug",
"touch_firmware_auto_update_disabled");
bool has_rail_interface{};
bool has_sio_mcu{};
m_set_sys->GetSettingsItemValueImpl<bool>(has_rail_interface, "hid", "has_rail_interface");
m_set_sys->GetSettingsItemValueImpl<bool>(has_sio_mcu, "hid", "has_sio_mcu");
platform_config.has_rail_interface.Assign(has_rail_interface);
platform_config.has_sio_mcu.Assign(has_sio_mcu);
is_initialized = true;
}
bool HidFirmwareSettings::IsDebugPadEnabled() {
LoadSettings(false);
return is_debug_pad_enabled;
}
bool HidFirmwareSettings::IsDeviceManaged() {
LoadSettings(false);
return is_device_managed;
}
bool HidFirmwareSettings::IsEmulateFutureDevice() {
LoadSettings(false);
return is_future_devices_emulated;
}
bool HidFirmwareSettings::IsTouchI2cManaged() {
LoadSettings(false);
return is_touch_i2c_managed;
}
bool HidFirmwareSettings::IsHandheldForced() {
LoadSettings(false);
return is_handheld_forced;
}
bool HidFirmwareSettings::IsRailEnabled() {
LoadSettings(false);
return is_rail_enabled;
}
bool HidFirmwareSettings::IsHardwareErrorEmulated() {
LoadSettings(false);
return is_mcu_hardware_error_emulated;
}
bool HidFirmwareSettings::IsBleDisabled() {
LoadSettings(false);
return is_ble_disabled;
}
bool HidFirmwareSettings::IsDscaleDisabled() {
LoadSettings(false);
return is_dscale_disabled;
}
bool HidFirmwareSettings::IsTouchAutoUpdateDisabled() {
LoadSettings(false);
return is_touch_firmware_auto_update_disabled;
}
HidFirmwareSettings::FirmwareSetting HidFirmwareSettings::GetFirmwareUpdateFailure() {
LoadSettings(false);
return is_firmware_update_failure;
}
HidFirmwareSettings::FeaturesPerId HidFirmwareSettings::FeaturesDisabledPerId() {
LoadSettings(false);
return features_per_id_disabled;
}
Set::PlatformConfig HidFirmwareSettings::GetPlatformConfig() {
LoadSettings(false);
return platform_config;
}
} // namespace Service::HID

View File

@@ -0,0 +1,67 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "core/hle/service/set/settings_types.h"
namespace Core {
class System;
}
namespace Service::Set {
class ISystemSettingsServer;
}
namespace Service::HID {
/// Loads firmware config from nn::settings::fwdbg
class HidFirmwareSettings {
public:
using FirmwareSetting = std::array<u8, 4>;
using FeaturesPerId = std::array<bool, 0xA8>;
HidFirmwareSettings(Core::System& system);
void Reload();
void LoadSettings(bool reload_config);
bool IsDebugPadEnabled();
bool IsDeviceManaged();
bool IsEmulateFutureDevice();
bool IsTouchI2cManaged();
bool IsHandheldForced();
bool IsRailEnabled();
bool IsHardwareErrorEmulated();
bool IsBleDisabled();
bool IsDscaleDisabled();
bool IsTouchAutoUpdateDisabled();
FirmwareSetting GetFirmwareUpdateFailure();
FeaturesPerId FeaturesDisabledPerId();
Set::PlatformConfig GetPlatformConfig();
private:
bool is_initialized{};
// Debug settings
bool is_debug_pad_enabled{};
bool is_device_managed{};
bool is_touch_i2c_managed{};
bool is_future_devices_emulated{};
bool is_mcu_hardware_error_emulated{};
bool is_rail_enabled{};
bool is_firmware_update_failure_emulated{};
bool is_ble_disabled{};
bool is_dscale_disabled{};
bool is_handheld_forced{};
bool is_touch_firmware_auto_update_disabled{};
FirmwareSetting is_firmware_update_failure{};
FeaturesPerId features_per_id_disabled{};
Set::PlatformConfig platform_config{};
std::shared_ptr<Service::Set::ISystemSettingsServer> m_set_sys;
};
} // namespace Service::HID

View File

@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <array>
#include "common/common_types.h"
namespace Service::IRS {
template <typename State, std::size_t max_buffer_size>
struct Lifo {
s64 sampling_number{};
s64 buffer_count{};
std::array<State, max_buffer_size> entries{};
const State& ReadCurrentEntry() const {
return entries[GetBufferTail()];
}
const State& ReadPreviousEntry() const {
return entries[GetPreviousEntryIndex()];
}
s64 GetBufferTail() const {
return sampling_number % max_buffer_size;
}
std::size_t GetPreviousEntryIndex() const {
return static_cast<size_t>((GetBufferTail() + max_buffer_size - 1) % max_buffer_size);
}
std::size_t GetNextEntryIndex() const {
return static_cast<size_t>((GetBufferTail() + 1) % max_buffer_size);
}
void WriteNextEntry(const State& new_state) {
if (buffer_count < static_cast<s64>(max_buffer_size)) {
buffer_count++;
}
sampling_number++;
entries[GetBufferTail()] = new_state;
}
};
} // namespace Service::IRS

View File

@@ -0,0 +1,56 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "common/settings.h"
#include "core/core_timing.h"
#include "hid_core/frontend/emulated_devices.h"
#include "hid_core/hid_core.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/keyboard/keyboard.h"
#include "hid_core/resources/shared_memory_format.h"
namespace Service::HID {
Keyboard::Keyboard(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {
emulated_devices = hid_core.GetEmulatedDevices();
}
Keyboard::~Keyboard() = default;
void Keyboard::OnInit() {}
void Keyboard::OnRelease() {}
void Keyboard::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
std::scoped_lock shared_lock{*shared_mutex};
const u64 aruid = applet_resource->GetActiveAruid();
auto* data = applet_resource->GetAruidData(aruid);
if (data == nullptr || !data->flag.is_assigned) {
return;
}
KeyboardSharedMemoryFormat& shared_memory = data->shared_memory_format->keyboard;
if (!IsControllerActivated()) {
shared_memory.keyboard_lifo.buffer_count = 0;
shared_memory.keyboard_lifo.buffer_tail = 0;
return;
}
const auto& last_entry = shared_memory.keyboard_lifo.ReadCurrentEntry().state;
next_state.sampling_number = last_entry.sampling_number + 1;
if (Settings::values.keyboard_enabled) {
const auto& keyboard_state = emulated_devices->GetKeyboard();
const auto& keyboard_modifier_state = emulated_devices->GetKeyboardModifier();
next_state.key = keyboard_state;
next_state.modifier = keyboard_modifier_state;
next_state.attribute.is_connected.Assign(1);
}
shared_memory.keyboard_lifo.WriteNextEntry(next_state);
}
} // namespace Service::HID

View File

@@ -0,0 +1,33 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "hid_core/resources/controller_base.h"
#include "hid_core/resources/keyboard/keyboard_types.h"
namespace Core::HID {
class HIDCore;
class EmulatedDevices;
} // namespace Core::HID
namespace Service::HID {
class Keyboard final : public ControllerBase {
public:
explicit Keyboard(Core::HID::HIDCore& hid_core_);
~Keyboard() override;
// Called when the controller is initialized
void OnInit() override;
// When the controller is released
void OnRelease() override;
// When the controller is requesting an update for the shared memory
void OnUpdate(const Core::Timing::CoreTiming& core_timing) override;
private:
KeyboardState next_state{};
Core::HID::EmulatedDevices* emulated_devices = nullptr;
};
} // namespace Service::HID

View File

@@ -0,0 +1,20 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
#include "hid_core/hid_types.h"
namespace Service::HID {
// This is nn::hid::detail::KeyboardState
struct KeyboardState {
s64 sampling_number{};
Core::HID::KeyboardModifier modifier{};
Core::HID::KeyboardAttribute attribute{};
Core::HID::KeyboardKey key{};
};
static_assert(sizeof(KeyboardState) == 0x30, "KeyboardState is an invalid size");
} // namespace Service::HID

View File

@@ -0,0 +1,64 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "core/core_timing.h"
#include "core/frontend/emu_window.h"
#include "hid_core/frontend/emulated_devices.h"
#include "hid_core/hid_core.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/mouse/debug_mouse.h"
#include "hid_core/resources/shared_memory_format.h"
namespace Service::HID {
DebugMouse::DebugMouse(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {
emulated_devices = hid_core.GetEmulatedDevices();
}
DebugMouse::~DebugMouse() = default;
void DebugMouse::OnInit() {}
void DebugMouse::OnRelease() {}
void DebugMouse::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
std::scoped_lock shared_lock{*shared_mutex};
const u64 aruid = applet_resource->GetActiveAruid();
auto* data = applet_resource->GetAruidData(aruid);
if (data == nullptr || !data->flag.is_assigned) {
return;
}
MouseSharedMemoryFormat& shared_memory = data->shared_memory_format->debug_mouse;
if (!IsControllerActivated()) {
shared_memory.mouse_lifo.buffer_count = 0;
shared_memory.mouse_lifo.buffer_tail = 0;
return;
}
next_state = {};
const auto& last_entry = shared_memory.mouse_lifo.ReadCurrentEntry().state;
next_state.sampling_number = last_entry.sampling_number + 1;
if (Settings::values.mouse_enabled) {
const auto& mouse_button_state = emulated_devices->GetMouseButtons();
const auto& mouse_position_state = emulated_devices->GetMousePosition();
const auto& mouse_wheel_state = emulated_devices->GetMouseWheel();
next_state.attribute.is_connected.Assign(1);
next_state.x = static_cast<s32>(mouse_position_state.x * Layout::ScreenUndocked::Width);
next_state.y = static_cast<s32>(mouse_position_state.y * Layout::ScreenUndocked::Height);
next_state.delta_x = next_state.x - last_entry.x;
next_state.delta_y = next_state.y - last_entry.y;
next_state.delta_wheel_x = mouse_wheel_state.x - last_mouse_wheel_state.x;
next_state.delta_wheel_y = mouse_wheel_state.y - last_mouse_wheel_state.y;
last_mouse_wheel_state = mouse_wheel_state;
next_state.button = mouse_button_state;
}
shared_memory.mouse_lifo.WriteNextEntry(next_state);
}
} // namespace Service::HID

View File

@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "hid_core/hid_types.h"
#include "hid_core/resources/controller_base.h"
namespace Core::HID {
class HIDCore;
class EmulatedDevices;
} // namespace Core::HID
namespace Service::HID {
class DebugMouse final : public ControllerBase {
public:
explicit DebugMouse(Core::HID::HIDCore& hid_core_);
~DebugMouse() override;
// Called when the controller is initialized
void OnInit() override;
// When the controller is released
void OnRelease() override;
// When the controller is requesting an update for the shared memory
void OnUpdate(const Core::Timing::CoreTiming& core_timing) override;
private:
Core::HID::MouseState next_state{};
Core::HID::AnalogStickState last_mouse_wheel_state{};
Core::HID::EmulatedDevices* emulated_devices = nullptr;
};
} // namespace Service::HID

View File

@@ -0,0 +1,64 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "core/core_timing.h"
#include "core/frontend/emu_window.h"
#include "hid_core/frontend/emulated_devices.h"
#include "hid_core/hid_core.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/mouse/mouse.h"
#include "hid_core/resources/shared_memory_format.h"
namespace Service::HID {
Mouse::Mouse(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {
emulated_devices = hid_core.GetEmulatedDevices();
}
Mouse::~Mouse() = default;
void Mouse::OnInit() {}
void Mouse::OnRelease() {}
void Mouse::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
std::scoped_lock shared_lock{*shared_mutex};
const u64 aruid = applet_resource->GetActiveAruid();
auto* data = applet_resource->GetAruidData(aruid);
if (data == nullptr || !data->flag.is_assigned) {
return;
}
MouseSharedMemoryFormat& shared_memory = data->shared_memory_format->mouse;
if (!IsControllerActivated()) {
shared_memory.mouse_lifo.buffer_count = 0;
shared_memory.mouse_lifo.buffer_tail = 0;
return;
}
next_state = {};
const auto& last_entry = shared_memory.mouse_lifo.ReadCurrentEntry().state;
next_state.sampling_number = last_entry.sampling_number + 1;
if (Settings::values.mouse_enabled) {
const auto& mouse_button_state = emulated_devices->GetMouseButtons();
const auto& mouse_position_state = emulated_devices->GetMousePosition();
const auto& mouse_wheel_state = emulated_devices->GetMouseWheel();
next_state.attribute.is_connected.Assign(1);
next_state.x = static_cast<s32>(mouse_position_state.x * Layout::ScreenUndocked::Width);
next_state.y = static_cast<s32>(mouse_position_state.y * Layout::ScreenUndocked::Height);
next_state.delta_x = next_state.x - last_entry.x;
next_state.delta_y = next_state.y - last_entry.y;
next_state.delta_wheel_x = mouse_wheel_state.x - last_mouse_wheel_state.x;
next_state.delta_wheel_y = mouse_wheel_state.y - last_mouse_wheel_state.y;
last_mouse_wheel_state = mouse_wheel_state;
next_state.button = mouse_button_state;
}
shared_memory.mouse_lifo.WriteNextEntry(next_state);
}
} // namespace Service::HID

View File

@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include "hid_core/hid_types.h"
#include "hid_core/resources/controller_base.h"
namespace Core::HID {
class HIDCore;
class EmulatedDevices;
} // namespace Core::HID
namespace Service::HID {
class Mouse final : public ControllerBase {
public:
explicit Mouse(Core::HID::HIDCore& hid_core_);
~Mouse() override;
// Called when the controller is initialized
void OnInit() override;
// When the controller is released
void OnRelease() override;
// When the controller is requesting an update for the shared memory
void OnUpdate(const Core::Timing::CoreTiming& core_timing) override;
private:
Core::HID::MouseState next_state{};
Core::HID::AnalogStickState last_mouse_wheel_state{};
Core::HID::EmulatedDevices* emulated_devices = nullptr;
};
} // namespace Service::HID

View File

@@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/common_types.h"
namespace Service::HID {} // namespace Service::HID

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,215 @@
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <array>
#include <atomic>
#include <mutex>
#include <span>
#include "common/common_types.h"
#include "hid_core/hid_types.h"
#include "hid_core/resources/abstracted_pad/abstract_pad.h"
#include "hid_core/resources/controller_base.h"
#include "hid_core/resources/npad/npad_resource.h"
#include "hid_core/resources/npad/npad_types.h"
#include "hid_core/resources/npad/npad_vibration.h"
#include "hid_core/resources/vibration/gc_vibration_device.h"
#include "hid_core/resources/vibration/n64_vibration_device.h"
#include "hid_core/resources/vibration/vibration_base.h"
#include "hid_core/resources/vibration/vibration_device.h"
namespace Core::HID {
class EmulatedController;
enum class ControllerTriggerType;
} // namespace Core::HID
namespace Kernel {
class KEvent;
class KReadableEvent;
} // namespace Kernel
namespace Service::KernelHelpers {
class ServiceContext;
} // namespace Service::KernelHelpers
namespace Service::Set {
class ISystemSettingsServer;
}
union Result;
namespace Service::HID {
class AppletResource;
struct HandheldConfig;
struct NpadInternalState;
struct NpadSixAxisSensorLifo;
struct NpadSharedMemoryFormat;
class NPad final {
public:
explicit NPad(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_);
~NPad();
Result Activate();
Result Activate(u64 aruid);
Result ActivateNpadResource();
Result ActivateNpadResource(u64 aruid);
void FreeAppletResourceId(u64 aruid);
// When the controller is requesting an update for the shared memory
void OnUpdate(const Core::Timing::CoreTiming& core_timing);
Result SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet supported_style_set);
Result GetSupportedNpadStyleSet(u64 aruid,
Core::HID::NpadStyleSet& out_supported_style_set) const;
Result GetMaskedSupportedNpadStyleSet(u64 aruid,
Core::HID::NpadStyleSet& out_supported_style_set) const;
Result SetSupportedNpadIdType(u64 aruid,
std::span<const Core::HID::NpadIdType> supported_npad_list);
Result SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type);
Result GetNpadJoyHoldType(u64 aruid, NpadJoyHoldType& out_hold_type) const;
Result SetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode mode);
Result GetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode& out_mode) const;
bool SetNpadMode(u64 aruid, Core::HID::NpadIdType& new_npad_id, Core::HID::NpadIdType npad_id,
NpadJoyDeviceType npad_device_type, NpadJoyAssignmentMode assignment_mode);
Result AcquireNpadStyleSetUpdateEventHandle(u64 aruid, Kernel::KReadableEvent** out_event,
Core::HID::NpadIdType npad_id);
// Adds a new controller at an index.
void AddNewControllerAt(u64 aruid, Core::HID::NpadStyleIndex controller,
Core::HID::NpadIdType npad_id);
// Adds a new controller at an index with connection status.
void UpdateControllerAt(u64 aruid, Core::HID::NpadStyleIndex controller,
Core::HID::NpadIdType npad_id, bool connected);
Result DisconnectNpad(u64 aruid, Core::HID::NpadIdType npad_id);
Result IsFirmwareUpdateAvailableForSixAxisSensor(
u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle,
bool& is_firmware_available) const;
Result ResetIsSixAxisSensorDeviceNewlyAssigned(
u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle);
Result IsUnintendedHomeButtonInputProtectionEnabled(bool& out_is_enabled, u64 aruid,
Core::HID::NpadIdType npad_id) const;
Result EnableUnintendedHomeButtonInputProtection(u64 aruid, Core::HID::NpadIdType npad_id,
bool is_enabled);
void SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled);
void ClearAllConnectedControllers();
void DisconnectAllConnectedControllers();
void ConnectAllDisconnectedControllers();
void ClearAllControllers();
Result MergeSingleJoyAsDualJoy(u64 aruid, Core::HID::NpadIdType npad_id_1,
Core::HID::NpadIdType npad_id_2);
Result StartLrAssignmentMode(u64 aruid);
Result StopLrAssignmentMode(u64 aruid);
Result SwapNpadAssignment(u64 aruid, Core::HID::NpadIdType npad_id_1,
Core::HID::NpadIdType npad_id_2);
// Logical OR for all buttons presses on all controllers
// Specifically for cheat engine and other features.
Core::HID::NpadButton GetAndResetPressState();
Result ApplyNpadSystemCommonPolicy(u64 aruid);
Result ApplyNpadSystemCommonPolicyFull(u64 aruid);
Result ClearNpadSystemCommonPolicy(u64 aruid);
void SetRevision(u64 aruid, NpadRevision revision);
NpadRevision GetRevision(u64 aruid);
Result RegisterAppletResourceUserId(u64 aruid);
void UnregisterAppletResourceUserId(u64 aruid);
void SetNpadExternals(std::shared_ptr<AppletResource> resource,
std::recursive_mutex* shared_mutex,
std::shared_ptr<HandheldConfig> handheld_config,
Kernel::KEvent* input_event, std::mutex* input_mutex,
std::shared_ptr<Service::Set::ISystemSettingsServer> settings);
AppletDetailedUiType GetAppletDetailedUiType(Core::HID::NpadIdType npad_id);
Result SetNpadCaptureButtonAssignment(u64 aruid, Core::HID::NpadStyleSet npad_style_set,
Core::HID::NpadButton button_assignment);
Result ClearNpadCaptureButtonAssignment(u64 aruid);
std::size_t GetNpadCaptureButtonAssignment(std::span<Core::HID::NpadButton> out_list,
u64 aruid) const;
Result SetNpadSystemExtStateEnabled(u64 aruid, bool is_enabled);
Result AssigningSingleOnSlSrPress(u64 aruid, bool is_enabled);
Result GetLastActiveNpad(Core::HID::NpadIdType& out_npad_id) const;
NpadVibration* GetVibrationHandler();
std::vector<NpadVibrationBase*> GetAllVibrationDevices();
NpadVibrationBase* GetVibrationDevice(const Core::HID::VibrationDeviceHandle& handle);
NpadN64VibrationDevice* GetN64VibrationDevice(const Core::HID::VibrationDeviceHandle& handle);
NpadVibrationDevice* GetNSVibrationDevice(const Core::HID::VibrationDeviceHandle& handle);
NpadGcVibrationDevice* GetGcVibrationDevice(const Core::HID::VibrationDeviceHandle& handle);
void UpdateHandheldAbstractState();
void EnableAppletToGetInput(u64 aruid);
private:
struct NpadControllerData {
NpadInternalState* shared_memory = nullptr;
Core::HID::EmulatedController* device = nullptr;
bool is_active{};
bool is_connected{};
// Dual joycons can have only one side connected
bool is_dual_left_connected{true};
bool is_dual_right_connected{true};
// Current pad state
NPadGenericState npad_pad_state{};
NPadGenericState npad_libnx_state{};
NpadGcTriggerState npad_trigger_state{};
int callback_key{};
};
void ControllerUpdate(Core::HID::ControllerTriggerType type, std::size_t controller_idx);
void InitNewlyAddedController(u64 aruid, Core::HID::NpadIdType npad_id);
void RequestPadStateUpdate(u64 aruid, Core::HID::NpadIdType npad_id);
void WriteEmptyEntry(NpadInternalState* npad);
NpadControllerData& GetControllerFromHandle(
u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle);
const NpadControllerData& GetControllerFromHandle(
u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) const;
NpadControllerData& GetControllerFromNpadIdType(u64 aruid, Core::HID::NpadIdType npad_id);
const NpadControllerData& GetControllerFromNpadIdType(u64 aruid,
Core::HID::NpadIdType npad_id) const;
Core::HID::SixAxisSensorProperties& GetSixaxisProperties(
u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle);
const Core::HID::SixAxisSensorProperties& GetSixaxisProperties(
u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) const;
Core::HID::HIDCore& hid_core;
KernelHelpers::ServiceContext& service_context;
s32 ref_counter{};
mutable std::mutex mutex;
NPadResource npad_resource;
AppletResourceHolder applet_resource_holder{};
std::array<AbstractPad, MaxSupportedNpadIdTypes> abstracted_pads;
NpadVibration vibration_handler{};
std::atomic<u64> press_state{};
std::array<std::array<NpadControllerData, MaxSupportedNpadIdTypes>, AruidIndexMax>
controller_data{};
};
} // namespace Service::HID

View File

@@ -0,0 +1,228 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "hid_core/hid_util.h"
#include "hid_core/resources/npad/npad_data.h"
namespace Service::HID {
NPadData::NPadData() {
ClearNpadSystemCommonPolicy();
}
NPadData::~NPadData() = default;
NpadStatus NPadData::GetNpadStatus() const {
return status;
}
void NPadData::SetNpadAnalogStickUseCenterClamp(bool is_enabled) {
status.use_center_clamp.Assign(is_enabled);
}
bool NPadData::GetNpadAnalogStickUseCenterClamp() const {
return status.use_center_clamp.As<bool>();
}
void NPadData::SetNpadSystemExtStateEnabled(bool is_enabled) {
status.system_ext_state.Assign(is_enabled);
}
bool NPadData::GetNpadSystemExtState() const {
return status.system_ext_state.As<bool>();
}
Result NPadData::SetSupportedNpadIdType(std::span<const Core::HID::NpadIdType> list) {
// Note: Real limit is 11. But array size is 10. N's bug?
if (list.size() > MaxSupportedNpadIdTypes) {
return ResultInvalidArraySize;
}
supported_npad_id_types_count = list.size();
memcpy(supported_npad_id_types.data(), list.data(),
list.size() * sizeof(Core::HID::NpadIdType));
return ResultSuccess;
}
std::size_t NPadData::GetSupportedNpadIdType(std::span<Core::HID::NpadIdType> out_list) const {
std::size_t out_size = std::min(supported_npad_id_types_count, out_list.size());
memcpy(out_list.data(), supported_npad_id_types.data(),
out_size * sizeof(Core::HID::NpadIdType));
return out_size;
}
bool NPadData::IsNpadIdTypeSupported(Core::HID::NpadIdType npad_id) const {
for (std::size_t i = 0; i < supported_npad_id_types_count; i++) {
if (supported_npad_id_types[i] == npad_id) {
return true;
}
}
return false;
}
void NPadData::SetNpadSystemCommonPolicy(bool is_full_policy) {
supported_npad_style_set = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::JoyDual |
Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
handheld_activation_mode = NpadHandheldActivationMode::Dual;
status.is_supported_styleset_set.Assign(true);
status.is_hold_type_set.Assign(true);
status.lr_assignment_mode.Assign(false);
status.is_policy.Assign(true);
if (is_full_policy) {
status.is_full_policy.Assign(true);
}
supported_npad_id_types_count = 10;
supported_npad_id_types[0] = Core::HID::NpadIdType::Player1;
supported_npad_id_types[1] = Core::HID::NpadIdType::Player2;
supported_npad_id_types[2] = Core::HID::NpadIdType::Player3;
supported_npad_id_types[3] = Core::HID::NpadIdType::Player4;
supported_npad_id_types[4] = Core::HID::NpadIdType::Player5;
supported_npad_id_types[5] = Core::HID::NpadIdType::Player6;
supported_npad_id_types[6] = Core::HID::NpadIdType::Player7;
supported_npad_id_types[7] = Core::HID::NpadIdType::Player8;
supported_npad_id_types[8] = Core::HID::NpadIdType::Other;
supported_npad_id_types[9] = Core::HID::NpadIdType::Handheld;
for (auto& input_protection : is_unintended_home_button_input_protection) {
input_protection = true;
}
}
void NPadData::ClearNpadSystemCommonPolicy() {
status.raw = 0;
supported_npad_style_set = Core::HID::NpadStyleSet::All;
npad_hold_type = NpadJoyHoldType::Vertical;
handheld_activation_mode = NpadHandheldActivationMode::Dual;
for (auto& button_assignment : npad_button_assignment) {
button_assignment = Core::HID::NpadButton::None;
}
supported_npad_id_types_count = 10;
supported_npad_id_types[0] = Core::HID::NpadIdType::Player1;
supported_npad_id_types[1] = Core::HID::NpadIdType::Player2;
supported_npad_id_types[2] = Core::HID::NpadIdType::Player3;
supported_npad_id_types[3] = Core::HID::NpadIdType::Player4;
supported_npad_id_types[4] = Core::HID::NpadIdType::Player5;
supported_npad_id_types[5] = Core::HID::NpadIdType::Player6;
supported_npad_id_types[6] = Core::HID::NpadIdType::Player7;
supported_npad_id_types[7] = Core::HID::NpadIdType::Player8;
supported_npad_id_types[8] = Core::HID::NpadIdType::Other;
supported_npad_id_types[9] = Core::HID::NpadIdType::Handheld;
for (auto& input_protection : is_unintended_home_button_input_protection) {
input_protection = true;
}
}
void NPadData::SetNpadJoyHoldType(NpadJoyHoldType hold_type) {
npad_hold_type = hold_type;
status.is_hold_type_set.Assign(true);
}
NpadJoyHoldType NPadData::GetNpadJoyHoldType() const {
return npad_hold_type;
}
void NPadData::SetHandheldActivationMode(NpadHandheldActivationMode activation_mode) {
handheld_activation_mode = activation_mode;
}
NpadHandheldActivationMode NPadData::GetHandheldActivationMode() const {
return handheld_activation_mode;
}
void NPadData::SetSupportedNpadStyleSet(Core::HID::NpadStyleSet style_set) {
supported_npad_style_set = style_set;
status.is_supported_styleset_set.Assign(true);
status.is_hold_type_set.Assign(true);
}
Core::HID::NpadStyleSet NPadData::GetSupportedNpadStyleSet() const {
return supported_npad_style_set;
}
bool NPadData::IsNpadStyleIndexSupported(Core::HID::NpadStyleIndex style_index) const {
Core::HID::NpadStyleTag style = {supported_npad_style_set};
switch (style_index) {
case Core::HID::NpadStyleIndex::Fullkey:
return style.fullkey.As<bool>();
case Core::HID::NpadStyleIndex::Handheld:
return style.handheld.As<bool>();
case Core::HID::NpadStyleIndex::JoyconDual:
return style.joycon_dual.As<bool>();
case Core::HID::NpadStyleIndex::JoyconLeft:
return style.joycon_left.As<bool>();
case Core::HID::NpadStyleIndex::JoyconRight:
return style.joycon_right.As<bool>();
case Core::HID::NpadStyleIndex::GameCube:
return style.gamecube.As<bool>();
case Core::HID::NpadStyleIndex::Pokeball:
return style.palma.As<bool>();
case Core::HID::NpadStyleIndex::NES:
return style.lark.As<bool>();
case Core::HID::NpadStyleIndex::SNES:
return style.lucia.As<bool>();
case Core::HID::NpadStyleIndex::N64:
return style.lagoon.As<bool>();
case Core::HID::NpadStyleIndex::SegaGenesis:
return style.lager.As<bool>();
default:
return false;
}
}
void NPadData::SetLrAssignmentMode(bool is_enabled) {
status.lr_assignment_mode.Assign(is_enabled);
}
bool NPadData::GetLrAssignmentMode() const {
return status.lr_assignment_mode.As<bool>();
}
void NPadData::SetAssigningSingleOnSlSrPress(bool is_enabled) {
status.assigning_single_on_sl_sr_press.Assign(is_enabled);
}
bool NPadData::GetAssigningSingleOnSlSrPress() const {
return status.assigning_single_on_sl_sr_press.As<bool>();
}
void NPadData::SetHomeProtectionEnabled(bool is_enabled, Core::HID::NpadIdType npad_id) {
is_unintended_home_button_input_protection[NpadIdTypeToIndex(npad_id)] = is_enabled;
}
bool NPadData::GetHomeProtectionEnabled(Core::HID::NpadIdType npad_id) const {
return is_unintended_home_button_input_protection[NpadIdTypeToIndex(npad_id)];
}
void NPadData::SetCaptureButtonAssignment(Core::HID::NpadButton button_assignment,
std::size_t style_index) {
npad_button_assignment[style_index] = button_assignment;
}
Core::HID::NpadButton NPadData::GetCaptureButtonAssignment(std::size_t style_index) const {
return npad_button_assignment[style_index];
}
std::size_t NPadData::GetNpadCaptureButtonAssignmentList(
std::span<Core::HID::NpadButton> out_list) const {
for (std::size_t i = 0; i < out_list.size(); i++) {
Core::HID::NpadStyleSet style_set = GetStylesetByIndex(i);
if ((style_set & supported_npad_style_set) == Core::HID::NpadStyleSet::None ||
npad_button_assignment[i] == Core::HID::NpadButton::None) {
return i;
}
out_list[i] = npad_button_assignment[i];
}
return out_list.size();
}
} // namespace Service::HID

View File

@@ -0,0 +1,88 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <array>
#include <span>
#include "common/common_types.h"
#include "core/hle/result.h"
#include "hid_core/hid_types.h"
#include "hid_core/resources/npad/npad_types.h"
namespace Service::HID {
struct NpadStatus {
union {
u32 raw{};
BitField<0, 1, u32> is_supported_styleset_set;
BitField<1, 1, u32> is_hold_type_set;
BitField<2, 1, u32> lr_assignment_mode;
BitField<3, 1, u32> assigning_single_on_sl_sr_press;
BitField<4, 1, u32> is_full_policy;
BitField<5, 1, u32> is_policy;
BitField<6, 1, u32> use_center_clamp;
BitField<7, 1, u32> system_ext_state;
};
};
static_assert(sizeof(NpadStatus) == 4, "NpadStatus is an invalid size");
/// Handles Npad request from HID interfaces
class NPadData final {
public:
explicit NPadData();
~NPadData();
NpadStatus GetNpadStatus() const;
void SetNpadAnalogStickUseCenterClamp(bool is_enabled);
bool GetNpadAnalogStickUseCenterClamp() const;
void SetNpadSystemExtStateEnabled(bool is_enabled);
bool GetNpadSystemExtState() const;
Result SetSupportedNpadIdType(std::span<const Core::HID::NpadIdType> list);
std::size_t GetSupportedNpadIdType(std::span<Core::HID::NpadIdType> out_list) const;
bool IsNpadIdTypeSupported(Core::HID::NpadIdType npad_id) const;
void SetNpadSystemCommonPolicy(bool is_full_policy);
void ClearNpadSystemCommonPolicy();
void SetNpadJoyHoldType(NpadJoyHoldType hold_type);
NpadJoyHoldType GetNpadJoyHoldType() const;
void SetHandheldActivationMode(NpadHandheldActivationMode activation_mode);
NpadHandheldActivationMode GetHandheldActivationMode() const;
void SetSupportedNpadStyleSet(Core::HID::NpadStyleSet style_set);
Core::HID::NpadStyleSet GetSupportedNpadStyleSet() const;
bool IsNpadStyleIndexSupported(Core::HID::NpadStyleIndex style_index) const;
void SetLrAssignmentMode(bool is_enabled);
bool GetLrAssignmentMode() const;
void SetAssigningSingleOnSlSrPress(bool is_enabled);
bool GetAssigningSingleOnSlSrPress() const;
void SetHomeProtectionEnabled(bool is_enabled, Core::HID::NpadIdType npad_id);
bool GetHomeProtectionEnabled(Core::HID::NpadIdType npad_id) const;
void SetCaptureButtonAssignment(Core::HID::NpadButton button_assignment,
std::size_t style_index);
Core::HID::NpadButton GetCaptureButtonAssignment(std::size_t style_index) const;
std::size_t GetNpadCaptureButtonAssignmentList(std::span<Core::HID::NpadButton> out_list) const;
private:
NpadStatus status{};
Core::HID::NpadStyleSet supported_npad_style_set{Core::HID::NpadStyleSet::All};
NpadJoyHoldType npad_hold_type{NpadJoyHoldType::Vertical};
NpadHandheldActivationMode handheld_activation_mode{};
std::array<Core::HID::NpadIdType, MaxSupportedNpadIdTypes> supported_npad_id_types{};
std::array<Core::HID::NpadButton, StyleIndexCount> npad_button_assignment{};
std::size_t supported_npad_id_types_count{};
std::array<bool, MaxSupportedNpadIdTypes> is_unintended_home_button_input_protection{};
};
} // namespace Service::HID

View File

@@ -0,0 +1,697 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_readable_event.h"
#include "hid_core/hid_result.h"
#include "hid_core/hid_util.h"
#include "hid_core/resources/npad/npad_resource.h"
#include "hid_core/resources/npad/npad_types.h"
namespace Service::HID {
NPadResource::NPadResource(KernelHelpers::ServiceContext& context) : service_context{context} {}
NPadResource::~NPadResource() = default;
Result NPadResource::RegisterAppletResourceUserId(u64 aruid) {
const auto aruid_index = GetIndexFromAruid(aruid);
if (aruid_index < AruidIndexMax) {
return ResultAruidAlreadyRegistered;
}
std::size_t data_index = AruidIndexMax;
for (std::size_t i = 0; i < AruidIndexMax; i++) {
if (!state[i].flag.is_initialized) {
data_index = i;
break;
}
}
if (data_index == AruidIndexMax) {
return ResultAruidNoAvailableEntries;
}
auto& aruid_data = state[data_index];
aruid_data.aruid = aruid;
aruid_data.flag.is_initialized.Assign(true);
data_index = AruidIndexMax;
for (std::size_t i = 0; i < AruidIndexMax; i++) {
if (registration_list.flag[i] == RegistrationStatus::Initialized) {
if (registration_list.aruid[i] != aruid) {
continue;
}
data_index = i;
break;
}
// TODO: Don't Handle pending delete here
if (registration_list.flag[i] == RegistrationStatus::None ||
registration_list.flag[i] == RegistrationStatus::PendingDelete) {
data_index = i;
break;
}
}
if (data_index == AruidIndexMax) {
return ResultSuccess;
}
registration_list.flag[data_index] = RegistrationStatus::Initialized;
registration_list.aruid[data_index] = aruid;
return ResultSuccess;
}
void NPadResource::UnregisterAppletResourceUserId(u64 aruid) {
const u64 aruid_index = GetIndexFromAruid(aruid);
FreeAppletResourceId(aruid);
if (aruid_index < AruidIndexMax) {
state[aruid_index] = {};
registration_list.flag[aruid_index] = RegistrationStatus::PendingDelete;
}
for (std::size_t i = 0; i < AruidIndexMax; i++) {
if (registration_list.flag[i] == RegistrationStatus::Initialized) {
active_data_aruid = registration_list.aruid[i];
}
}
}
void NPadResource::FreeAppletResourceId(u64 aruid) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return;
}
auto& aruid_data = state[aruid_index];
aruid_data.flag.is_assigned.Assign(false);
for (auto& controller_state : aruid_data.controller_state) {
if (!controller_state.is_styleset_update_event_initialized) {
continue;
}
service_context.CloseEvent(controller_state.style_set_update_event);
controller_state.is_styleset_update_event_initialized = false;
}
}
Result NPadResource::Activate(u64 aruid) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultSuccess;
}
auto& state_data = state[aruid_index];
if (state_data.flag.is_assigned) {
return ResultAruidAlreadyRegistered;
}
state_data.flag.is_assigned.Assign(true);
state_data.data.ClearNpadSystemCommonPolicy();
state_data.npad_revision = NpadRevision::Revision0;
state_data.button_config = {};
if (active_data_aruid == aruid) {
default_hold_type = active_data.GetNpadJoyHoldType();
active_data.SetNpadJoyHoldType(default_hold_type);
}
return ResultSuccess;
}
Result NPadResource::Activate() {
if (ref_counter == std::numeric_limits<s32>::max() - 1) {
return ResultAppletResourceOverflow;
}
if (ref_counter == 0) {
RegisterAppletResourceUserId(SystemAruid);
Activate(SystemAruid);
}
ref_counter++;
return ResultSuccess;
}
Result NPadResource::Deactivate() {
if (ref_counter == 0) {
return ResultAppletResourceNotInitialized;
}
UnregisterAppletResourceUserId(SystemAruid);
ref_counter--;
return ResultSuccess;
}
NPadData* NPadResource::GetActiveData() {
return &active_data;
}
u64 NPadResource::GetActiveDataAruid() {
return active_data_aruid;
}
void NPadResource::SetAppletResourceUserId(u64 aruid) {
if (active_data_aruid == aruid) {
return;
}
active_data_aruid = aruid;
default_hold_type = active_data.GetNpadJoyHoldType();
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return;
}
auto& data = state[aruid_index].data;
if (data.GetNpadStatus().is_policy || data.GetNpadStatus().is_full_policy) {
data.SetNpadJoyHoldType(default_hold_type);
}
active_data = data;
if (data.GetNpadStatus().is_hold_type_set) {
active_data.SetNpadJoyHoldType(default_hold_type);
}
}
std::size_t NPadResource::GetIndexFromAruid(u64 aruid) const {
for (std::size_t i = 0; i < AruidIndexMax; i++) {
if (registration_list.flag[i] == RegistrationStatus::Initialized &&
registration_list.aruid[i] == aruid) {
return i;
}
}
return AruidIndexMax;
}
Result NPadResource::ApplyNpadSystemCommonPolicy(u64 aruid, bool is_full_policy) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
auto& data = state[aruid_index].data;
data.SetNpadSystemCommonPolicy(is_full_policy);
data.SetNpadJoyHoldType(default_hold_type);
if (active_data_aruid == aruid) {
active_data.SetNpadSystemCommonPolicy(is_full_policy);
active_data.SetNpadJoyHoldType(default_hold_type);
}
return ResultSuccess;
}
Result NPadResource::ClearNpadSystemCommonPolicy(u64 aruid) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
state[aruid_index].data.ClearNpadSystemCommonPolicy();
if (active_data_aruid == aruid) {
active_data.ClearNpadSystemCommonPolicy();
}
return ResultSuccess;
}
Result NPadResource::SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet style_set) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
auto& data = state[aruid_index].data;
data.SetSupportedNpadStyleSet(style_set);
if (active_data_aruid == aruid) {
active_data.SetSupportedNpadStyleSet(style_set);
active_data.SetNpadJoyHoldType(data.GetNpadJoyHoldType());
}
return ResultSuccess;
}
Result NPadResource::GetSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_Set,
u64 aruid) const {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
auto& data = state[aruid_index].data;
if (!data.GetNpadStatus().is_supported_styleset_set) {
return ResultUndefinedStyleset;
}
out_style_Set = data.GetSupportedNpadStyleSet();
return ResultSuccess;
}
Result NPadResource::GetMaskedSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_set,
u64 aruid) const {
if (aruid == SystemAruid) {
out_style_set = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Palma |
Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
return ResultSuccess;
}
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
auto& data = state[aruid_index].data;
if (!data.GetNpadStatus().is_supported_styleset_set) {
return ResultUndefinedStyleset;
}
Core::HID::NpadStyleSet mask{Core::HID::NpadStyleSet::None};
out_style_set = data.GetSupportedNpadStyleSet();
switch (state[aruid_index].npad_revision) {
case NpadRevision::Revision1:
mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::SystemExt |
Core::HID::NpadStyleSet::System;
break;
case NpadRevision::Revision2:
mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
break;
case NpadRevision::Revision3:
mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
Core::HID::NpadStyleSet::HandheldLark | Core::HID::NpadStyleSet::Lucia |
Core::HID::NpadStyleSet::Lagoon | Core::HID::NpadStyleSet::Lager |
Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
break;
default:
mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::SystemExt |
Core::HID::NpadStyleSet::System;
break;
}
out_style_set = out_style_set & mask;
return ResultSuccess;
}
Result NPadResource::GetAvailableStyleset(Core::HID::NpadStyleSet& out_style_set, u64 aruid) const {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
auto& data = state[aruid_index].data;
if (!data.GetNpadStatus().is_supported_styleset_set) {
return ResultUndefinedStyleset;
}
Core::HID::NpadStyleSet mask{Core::HID::NpadStyleSet::None};
out_style_set = data.GetSupportedNpadStyleSet();
switch (state[aruid_index].npad_revision) {
case NpadRevision::Revision1:
mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::SystemExt |
Core::HID::NpadStyleSet::System;
break;
case NpadRevision::Revision2:
mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
break;
case NpadRevision::Revision3:
mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
Core::HID::NpadStyleSet::HandheldLark | Core::HID::NpadStyleSet::Lucia |
Core::HID::NpadStyleSet::Lagoon | Core::HID::NpadStyleSet::Lager |
Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
break;
default:
mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::SystemExt |
Core::HID::NpadStyleSet::System;
break;
}
out_style_set = out_style_set & mask;
return ResultSuccess;
}
NpadRevision NPadResource::GetNpadRevision(u64 aruid) const {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return NpadRevision::Revision0;
}
return state[aruid_index].npad_revision;
}
Result NPadResource::IsSupportedNpadStyleSet(bool& is_set, u64 aruid) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
is_set = state[aruid_index].data.GetNpadStatus().is_supported_styleset_set.Value() != 0;
return ResultSuccess;
}
Result NPadResource::SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
state[aruid_index].data.SetNpadJoyHoldType(hold_type);
if (active_data_aruid == aruid) {
active_data.SetNpadJoyHoldType(hold_type);
}
return ResultSuccess;
}
Result NPadResource::GetNpadJoyHoldType(NpadJoyHoldType& hold_type, u64 aruid) const {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
auto& data = state[aruid_index].data;
if (data.GetNpadStatus().is_policy || data.GetNpadStatus().is_full_policy) {
hold_type = active_data.GetNpadJoyHoldType();
return ResultSuccess;
}
hold_type = data.GetNpadJoyHoldType();
return ResultSuccess;
}
Result NPadResource::SetNpadHandheldActivationMode(u64 aruid,
NpadHandheldActivationMode activation_mode) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
state[aruid_index].data.SetHandheldActivationMode(activation_mode);
if (active_data_aruid == aruid) {
active_data.SetHandheldActivationMode(activation_mode);
}
return ResultSuccess;
}
Result NPadResource::GetNpadHandheldActivationMode(NpadHandheldActivationMode& activation_mode,
u64 aruid) const {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
activation_mode = state[aruid_index].data.GetHandheldActivationMode();
return ResultSuccess;
}
Result NPadResource::SetSupportedNpadIdType(
u64 aruid, std::span<const Core::HID::NpadIdType> supported_npad_list) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
if (supported_npad_list.size() > MaxSupportedNpadIdTypes) {
return ResultInvalidArraySize;
}
Result result = state[aruid_index].data.SetSupportedNpadIdType(supported_npad_list);
if (result.IsSuccess() && active_data_aruid == aruid) {
result = active_data.SetSupportedNpadIdType(supported_npad_list);
}
return result;
}
bool NPadResource::IsControllerSupported(u64 aruid, Core::HID::NpadStyleIndex style_index) const {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return false;
}
return state[aruid_index].data.IsNpadStyleIndexSupported(style_index);
}
Result NPadResource::SetLrAssignmentMode(u64 aruid, bool is_enabled) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
state[aruid_index].data.SetLrAssignmentMode(is_enabled);
if (active_data_aruid == aruid) {
active_data.SetLrAssignmentMode(is_enabled);
}
return ResultSuccess;
}
Result NPadResource::GetLrAssignmentMode(bool& is_enabled, u64 aruid) const {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
is_enabled = state[aruid_index].data.GetLrAssignmentMode();
return ResultSuccess;
}
Result NPadResource::SetAssigningSingleOnSlSrPress(u64 aruid, bool is_enabled) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
state[aruid_index].data.SetAssigningSingleOnSlSrPress(is_enabled);
if (active_data_aruid == aruid) {
active_data.SetAssigningSingleOnSlSrPress(is_enabled);
}
return ResultSuccess;
}
Result NPadResource::IsAssigningSingleOnSlSrPressEnabled(bool& is_enabled, u64 aruid) const {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
is_enabled = state[aruid_index].data.GetAssigningSingleOnSlSrPress();
return ResultSuccess;
}
Result NPadResource::AcquireNpadStyleSetUpdateEventHandle(u64 aruid,
Kernel::KReadableEvent** out_event,
Core::HID::NpadIdType npad_id) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
auto& controller_state = state[aruid_index].controller_state[NpadIdTypeToIndex(npad_id)];
if (!controller_state.is_styleset_update_event_initialized) {
// Auto clear = true
controller_state.style_set_update_event =
service_context.CreateEvent("NpadResource:StylesetUpdateEvent");
// Assume creating the event succeeds otherwise crash the system here
controller_state.is_styleset_update_event_initialized = true;
}
*out_event = &controller_state.style_set_update_event->GetReadableEvent();
if (controller_state.is_styleset_update_event_initialized) {
controller_state.style_set_update_event->Signal();
}
return ResultSuccess;
}
Result NPadResource::SignalStyleSetUpdateEvent(u64 aruid, Core::HID::NpadIdType npad_id) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
auto controller = state[aruid_index].controller_state[NpadIdTypeToIndex(npad_id)];
if (controller.is_styleset_update_event_initialized) {
controller.style_set_update_event->Signal();
}
return ResultSuccess;
}
Result NPadResource::GetHomeProtectionEnabled(bool& is_enabled, u64 aruid,
Core::HID::NpadIdType npad_id) const {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
is_enabled = state[aruid_index].data.GetHomeProtectionEnabled(npad_id);
return ResultSuccess;
}
Result NPadResource::SetHomeProtectionEnabled(u64 aruid, Core::HID::NpadIdType npad_id,
bool is_enabled) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
state[aruid_index].data.SetHomeProtectionEnabled(is_enabled, npad_id);
if (active_data_aruid == aruid) {
active_data.SetHomeProtectionEnabled(is_enabled, npad_id);
}
return ResultSuccess;
}
Result NPadResource::SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
state[aruid_index].data.SetNpadAnalogStickUseCenterClamp(is_enabled);
if (active_data_aruid == aruid) {
active_data.SetNpadAnalogStickUseCenterClamp(is_enabled);
}
return ResultSuccess;
}
Result NPadResource::SetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id, std::size_t index,
Core::HID::NpadButton button_config) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
state[aruid_index].button_config[NpadIdTypeToIndex(npad_id)][index] = button_config;
return ResultSuccess;
}
Core::HID::NpadButton NPadResource::GetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id,
std::size_t index, Core::HID::NpadButton mask,
bool is_enabled) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return Core::HID::NpadButton::None;
}
auto& button_config = state[aruid_index].button_config[NpadIdTypeToIndex(npad_id)][index];
if (is_enabled) {
button_config = button_config | mask;
return button_config;
}
button_config = Core::HID::NpadButton::None;
return Core::HID::NpadButton::None;
}
void NPadResource::ResetButtonConfig() {
for (auto& selected_state : state) {
selected_state.button_config = {};
}
}
Result NPadResource::SetNpadCaptureButtonAssignment(u64 aruid,
Core::HID::NpadStyleSet npad_style_set,
Core::HID::NpadButton button_assignment) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
// Must be a power of two
const auto raw_styleset = static_cast<u32>(npad_style_set);
if (raw_styleset == 0 && (raw_styleset & (raw_styleset - 1)) != 0) {
return ResultMultipleStyleSetSelected;
}
std::size_t style_index{};
Core::HID::NpadStyleSet style_selected{};
for (style_index = 0; style_index < StyleIndexCount; ++style_index) {
style_selected = GetStylesetByIndex(style_index);
if (npad_style_set == style_selected) {
break;
}
}
if (style_selected == Core::HID::NpadStyleSet::None) {
return ResultMultipleStyleSetSelected;
}
state[aruid_index].data.SetCaptureButtonAssignment(button_assignment, style_index);
if (active_data_aruid == aruid) {
active_data.SetCaptureButtonAssignment(button_assignment, style_index);
}
return ResultSuccess;
}
Result NPadResource::ClearNpadCaptureButtonAssignment(u64 aruid) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
for (std::size_t i = 0; i < StyleIndexCount; i++) {
state[aruid_index].data.SetCaptureButtonAssignment(Core::HID::NpadButton::None, i);
if (active_data_aruid == aruid) {
active_data.SetCaptureButtonAssignment(Core::HID::NpadButton::None, i);
}
}
return ResultSuccess;
}
std::size_t NPadResource::GetNpadCaptureButtonAssignment(std::span<Core::HID::NpadButton> out_list,
u64 aruid) const {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return 0;
}
return state[aruid_index].data.GetNpadCaptureButtonAssignmentList(out_list);
}
void NPadResource::SetNpadRevision(u64 aruid, NpadRevision revision) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return;
}
state[aruid_index].npad_revision = revision;
}
Result NPadResource::SetNpadSystemExtStateEnabled(u64 aruid, bool is_enabled) {
const u64 aruid_index = GetIndexFromAruid(aruid);
if (aruid_index >= AruidIndexMax) {
return ResultNpadNotConnected;
}
state[aruid_index].data.SetNpadAnalogStickUseCenterClamp(is_enabled);
if (active_data_aruid == aruid) {
active_data.SetNpadAnalogStickUseCenterClamp(is_enabled);
}
return ResultSuccess;
}
} // namespace Service::HID

View File

@@ -0,0 +1,132 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <array>
#include <mutex>
#include <span>
#include "common/common_types.h"
#include "core/hle/result.h"
#include "core/hle/service/kernel_helpers.h"
#include "hid_core/hid_types.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/npad/npad_data.h"
#include "hid_core/resources/npad/npad_types.h"
namespace Core {
class System;
}
namespace Kernel {
class KReadableEvent;
}
namespace Service::HID {
struct DataStatusFlag;
struct NpadControllerState {
bool is_styleset_update_event_initialized{};
INSERT_PADDING_BYTES(0x7);
Kernel::KEvent* style_set_update_event{nullptr};
INSERT_PADDING_BYTES(0x27);
};
struct NpadState {
DataStatusFlag flag{};
u64 aruid{};
NPadData data{};
std::array<std::array<Core::HID::NpadButton, StyleIndexCount>, MaxSupportedNpadIdTypes>
button_config;
std::array<NpadControllerState, MaxSupportedNpadIdTypes> controller_state;
NpadRevision npad_revision;
};
/// Handles Npad request from HID interfaces
class NPadResource final {
public:
explicit NPadResource(KernelHelpers::ServiceContext& context);
~NPadResource();
NPadData* GetActiveData();
u64 GetActiveDataAruid();
Result RegisterAppletResourceUserId(u64 aruid);
void UnregisterAppletResourceUserId(u64 aruid);
void FreeAppletResourceId(u64 aruid);
Result Activate(u64 aruid);
Result Activate();
Result Deactivate();
void SetAppletResourceUserId(u64 aruid);
std::size_t GetIndexFromAruid(u64 aruid) const;
Result ApplyNpadSystemCommonPolicy(u64 aruid, bool is_full_policy);
Result ClearNpadSystemCommonPolicy(u64 aruid);
Result SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet style_set);
Result GetSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_Set, u64 aruid) const;
Result GetMaskedSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_set, u64 aruid) const;
Result GetAvailableStyleset(Core::HID::NpadStyleSet& out_style_set, u64 aruid) const;
NpadRevision GetNpadRevision(u64 aruid) const;
void SetNpadRevision(u64 aruid, NpadRevision revision);
Result IsSupportedNpadStyleSet(bool& is_set, u64 aruid);
Result SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type);
Result GetNpadJoyHoldType(NpadJoyHoldType& hold_type, u64 aruid) const;
Result SetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode activation_mode);
Result GetNpadHandheldActivationMode(NpadHandheldActivationMode& activation_mode,
u64 aruid) const;
Result SetSupportedNpadIdType(u64 aruid,
std::span<const Core::HID::NpadIdType> supported_npad_list);
bool IsControllerSupported(u64 aruid, Core::HID::NpadStyleIndex style_index) const;
Result SetLrAssignmentMode(u64 aruid, bool is_enabled);
Result GetLrAssignmentMode(bool& is_enabled, u64 aruid) const;
Result SetAssigningSingleOnSlSrPress(u64 aruid, bool is_enabled);
Result IsAssigningSingleOnSlSrPressEnabled(bool& is_enabled, u64 aruid) const;
Result AcquireNpadStyleSetUpdateEventHandle(u64 aruid, Kernel::KReadableEvent** out_event,
Core::HID::NpadIdType npad_id);
Result SignalStyleSetUpdateEvent(u64 aruid, Core::HID::NpadIdType npad_id);
Result GetHomeProtectionEnabled(bool& is_enabled, u64 aruid,
Core::HID::NpadIdType npad_id) const;
Result SetHomeProtectionEnabled(u64 aruid, Core::HID::NpadIdType npad_id, bool is_enabled);
Result SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled);
Result SetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id, std::size_t index,
Core::HID::NpadButton button_config);
Core::HID::NpadButton GetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id,
std::size_t index, Core::HID::NpadButton mask,
bool is_enabled);
void ResetButtonConfig();
Result SetNpadCaptureButtonAssignment(u64 aruid, Core::HID::NpadStyleSet npad_style_set,
Core::HID::NpadButton button_assignment);
Result ClearNpadCaptureButtonAssignment(u64 aruid);
std::size_t GetNpadCaptureButtonAssignment(std::span<Core::HID::NpadButton> out_list,
u64 aruid) const;
Result SetNpadSystemExtStateEnabled(u64 aruid, bool is_enabled);
private:
NPadData active_data{};
AruidRegisterList registration_list{};
std::array<NpadState, AruidIndexMax> state{};
u64 active_data_aruid{};
NpadJoyHoldType default_hold_type{};
s32 ref_counter{};
KernelHelpers::ServiceContext& service_context;
};
} // namespace Service::HID

View File

@@ -0,0 +1,358 @@
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include "common/bit_field.h"
#include "common/common_funcs.h"
#include "common/common_types.h"
#include "hid_core/hid_types.h"
namespace Core::HID {
class EmulatedController;
}
namespace Service::HID {
static constexpr std::size_t MaxSupportedNpadIdTypes = 10;
static constexpr std::size_t StyleIndexCount = 7;
// This is nn::hid::NpadJoyHoldType
enum class NpadJoyHoldType : u64 {
Vertical = 0,
Horizontal = 1,
};
// This is nn::hid::NpadJoyAssignmentMode
enum class NpadJoyAssignmentMode : u32 {
Dual = 0,
Single = 1,
};
// This is nn::hid::NpadJoyDeviceType
enum class NpadJoyDeviceType : s64 {
Left = 0,
Right = 1,
};
// This is nn::hid::NpadHandheldActivationMode
enum class NpadHandheldActivationMode : u64 {
Dual = 0,
Single = 1,
None = 2,
MaxActivationMode = 3,
};
// This is nn::hid::system::AppletFooterUiAttributesSet
struct AppletFooterUiAttributes {
INSERT_PADDING_BYTES(0x4);
};
// This is nn::hid::system::AppletFooterUiType
enum class AppletFooterUiType : u8 {
None = 0,
HandheldNone = 1,
HandheldJoyConLeftOnly = 2,
HandheldJoyConRightOnly = 3,
HandheldJoyConLeftJoyConRight = 4,
JoyDual = 5,
JoyDualLeftOnly = 6,
JoyDualRightOnly = 7,
JoyLeftHorizontal = 8,
JoyLeftVertical = 9,
JoyRightHorizontal = 10,
JoyRightVertical = 11,
SwitchProController = 12,
CompatibleProController = 13,
CompatibleJoyCon = 14,
LarkHvc1 = 15,
LarkHvc2 = 16,
LarkNesLeft = 17,
LarkNesRight = 18,
Lucia = 19,
Verification = 20,
Lagon = 21,
};
using AppletFooterUiVariant = u8;
// This is "nn::hid::system::AppletDetailedUiType".
struct AppletDetailedUiType {
AppletFooterUiVariant ui_variant;
INSERT_PADDING_BYTES(0x2);
AppletFooterUiType footer;
};
static_assert(sizeof(AppletDetailedUiType) == 0x4, "AppletDetailedUiType is an invalid size");
// This is nn::hid::NpadCommunicationMode
enum class NpadCommunicationMode : u64 {
Mode_5ms = 0,
Mode_10ms = 1,
Mode_15ms = 2,
Default = 3,
};
enum class NpadRevision : u32 {
Revision0 = 0,
Revision1 = 1,
Revision2 = 2,
Revision3 = 3,
};
// This is nn::hid::detail::ColorAttribute
enum class ColorAttribute : u32 {
Ok = 0,
ReadError = 1,
NoController = 2,
};
static_assert(sizeof(ColorAttribute) == 4, "ColorAttribute is an invalid size");
// This is nn::hid::detail::NpadFullKeyColorState
struct NpadFullKeyColorState {
ColorAttribute attribute{ColorAttribute::NoController};
Core::HID::NpadControllerColor fullkey{};
};
static_assert(sizeof(NpadFullKeyColorState) == 0xC, "NpadFullKeyColorState is an invalid size");
// This is nn::hid::detail::NpadJoyColorState
struct NpadJoyColorState {
ColorAttribute attribute{ColorAttribute::NoController};
Core::HID::NpadControllerColor left{};
Core::HID::NpadControllerColor right{};
};
static_assert(sizeof(NpadJoyColorState) == 0x14, "NpadJoyColorState is an invalid size");
// This is nn::hid::NpadAttribute
struct NpadAttribute {
union {
u32 raw{};
BitField<0, 1, u32> is_connected;
BitField<1, 1, u32> is_wired;
BitField<2, 1, u32> is_left_connected;
BitField<3, 1, u32> is_left_wired;
BitField<4, 1, u32> is_right_connected;
BitField<5, 1, u32> is_right_wired;
};
};
static_assert(sizeof(NpadAttribute) == 4, "NpadAttribute is an invalid size");
// This is nn::hid::NpadFullKeyState
// This is nn::hid::NpadHandheldState
// This is nn::hid::NpadJoyDualState
// This is nn::hid::NpadJoyLeftState
// This is nn::hid::NpadJoyRightState
// This is nn::hid::NpadPalmaState
// This is nn::hid::NpadSystemExtState
struct NPadGenericState {
s64_le sampling_number{};
Core::HID::NpadButtonState npad_buttons{};
Core::HID::AnalogStickState l_stick{};
Core::HID::AnalogStickState r_stick{};
NpadAttribute connection_status{};
INSERT_PADDING_BYTES(4); // Reserved
};
static_assert(sizeof(NPadGenericState) == 0x28, "NPadGenericState is an invalid size");
// This is nn::hid::server::NpadGcTriggerState
struct NpadGcTriggerState {
s64 sampling_number{};
s32 l_analog{};
s32 r_analog{};
};
static_assert(sizeof(NpadGcTriggerState) == 0x10, "NpadGcTriggerState is an invalid size");
// This is nn::hid::NpadSystemProperties
struct NPadSystemProperties {
union {
s64 raw{};
BitField<0, 1, s64> is_charging_joy_dual;
BitField<1, 1, s64> is_charging_joy_left;
BitField<2, 1, s64> is_charging_joy_right;
BitField<3, 1, s64> is_powered_joy_dual;
BitField<4, 1, s64> is_powered_joy_left;
BitField<5, 1, s64> is_powered_joy_right;
BitField<9, 1, s64> is_system_unsupported_button;
BitField<10, 1, s64> is_system_ext_unsupported_button;
BitField<11, 1, s64> is_vertical;
BitField<12, 1, s64> is_horizontal;
BitField<13, 1, s64> use_plus;
BitField<14, 1, s64> use_minus;
BitField<15, 1, s64> use_directional_buttons;
};
};
static_assert(sizeof(NPadSystemProperties) == 0x8, "NPadSystemProperties is an invalid size");
// This is nn::hid::NpadSystemButtonProperties
struct NpadSystemButtonProperties {
union {
s32 raw{};
BitField<0, 1, s32> is_home_button_protection_enabled;
};
};
static_assert(sizeof(NpadSystemButtonProperties) == 0x4, "NPadButtonProperties is an invalid size");
// This is nn::hid::system::DeviceType
struct DeviceType {
union {
u32 raw{};
BitField<0, 1, s32> fullkey;
BitField<1, 1, s32> debug_pad;
BitField<2, 1, s32> handheld_left;
BitField<3, 1, s32> handheld_right;
BitField<4, 1, s32> joycon_left;
BitField<5, 1, s32> joycon_right;
BitField<6, 1, s32> palma;
BitField<7, 1, s32> lark_hvc_left;
BitField<8, 1, s32> lark_hvc_right;
BitField<9, 1, s32> lark_nes_left;
BitField<10, 1, s32> lark_nes_right;
BitField<11, 1, s32> handheld_lark_hvc_left;
BitField<12, 1, s32> handheld_lark_hvc_right;
BitField<13, 1, s32> handheld_lark_nes_left;
BitField<14, 1, s32> handheld_lark_nes_right;
BitField<15, 1, s32> lucia;
BitField<16, 1, s32> lagon;
BitField<17, 1, s32> lager;
BitField<31, 1, s32> system;
};
};
// This is nn::hid::detail::NfcXcdDeviceHandleStateImpl
struct NfcXcdDeviceHandleStateImpl {
u64 handle{};
bool is_available{};
bool is_activated{};
INSERT_PADDING_BYTES(0x6); // Reserved
u64 sampling_number{};
};
static_assert(sizeof(NfcXcdDeviceHandleStateImpl) == 0x18,
"NfcXcdDeviceHandleStateImpl is an invalid size");
// This is nn::hid::NpadLarkType
enum class NpadLarkType : u32 {
Invalid,
H1,
H2,
NL,
NR,
};
// This is nn::hid::NpadLuciaType
enum class NpadLuciaType : u32 {
Invalid,
J,
E,
U,
};
// This is nn::hid::NpadLagonType
enum class NpadLagonType : u32 {
Invalid,
};
// This is nn::hid::NpadLagerType
enum class NpadLagerType : u32 {
Invalid,
J,
E,
U,
};
// nn::hidtypes::FeatureType
struct FeatureType {
union {
u64 raw{};
BitField<0, 1, u64> has_left_analog_stick;
BitField<1, 1, u64> has_right_analog_stick;
BitField<2, 1, u64> has_left_joy_six_axis_sensor;
BitField<3, 1, u64> has_right_joy_six_axis_sensor;
BitField<4, 1, u64> has_fullkey_joy_six_axis_sensor;
BitField<5, 1, u64> has_left_lra_vibration_device;
BitField<6, 1, u64> has_right_lra_vibration_device;
BitField<7, 1, u64> has_gc_vibration_device;
BitField<8, 1, u64> has_erm_vibration_device;
BitField<9, 1, u64> has_left_joy_rail_bus;
BitField<10, 1, u64> has_right_joy_rail_bus;
BitField<11, 1, u64> has_internal_bus;
BitField<12, 1, u64> is_palma;
BitField<13, 1, u64> has_nfc;
BitField<14, 1, u64> has_ir_sensor;
BitField<15, 1, u64> is_analog_stick_calibration_supported;
BitField<16, 1, u64> is_six_axis_Sensor_user_calibration_supported;
BitField<17, 1, u64> has_left_right_joy_battery;
BitField<18, 1, u64> has_fullkey_battery;
BitField<19, 1, u64> is_disconnect_controller_if_battery_none;
BitField<20, 1, u64> has_controller_color;
BitField<21, 1, u64> has_grip_color;
BitField<22, 1, u64> has_identification_code;
BitField<23, 1, u64> has_bluetooth_address;
BitField<24, 1, u64> has_mcu;
BitField<25, 1, u64> has_notification_led;
BitField<26, 1, u64> has_directional_buttons;
BitField<27, 1, u64> has_indicator_led;
BitField<28, 1, u64> is_button_config_embedded_supported;
BitField<29, 1, u64> is_button_config_full_supported;
BitField<30, 1, u64> is_button_config_left_supported;
BitField<31, 1, u64> is_button_config_right_supported;
BitField<32, 1, u64> is_usb_hid_device;
BitField<33, 1, u64> is_kuina_device;
BitField<34, 1, u64> is_direct_usb_to_bt_switching_device;
BitField<35, 1, u64> is_normalize_analog_stick_with_inner_cross;
};
};
static_assert(sizeof(FeatureType) == 8, "FeatureType is an invalid size");
// This is nn::hid::AssignmentStyle
struct AssignmentStyle {
union {
u32 raw{};
BitField<0, 1, u32> is_external_assigned;
BitField<1, 1, u32> is_external_left_assigned;
BitField<2, 1, u32> is_external_right_assigned;
BitField<3, 1, u32> is_handheld_assigned;
BitField<4, 1, u32> is_handheld_left_assigned;
BitField<5, 1, u32> is_handheld_right_assigned;
};
};
static_assert(sizeof(AssignmentStyle) == 4, "AssignmentStyle is an invalid size");
// This is nn::hid::server::IAbstractedPad::InternalFlags
struct InternalFlags {
union {
u32 raw{};
BitField<0, 1, u32> is_bound;
BitField<1, 1, u32> is_connected;
BitField<2, 1, u32> is_battery_low_ovln_required;
BitField<3, 1, u32> is_battery_low_ovln_delay_required;
BitField<4, 1, u32> is_sample_received;
BitField<5, 1, u32> is_virtual_input;
BitField<6, 1, u32> is_wired;
BitField<8, 1, u32> use_center_clamp;
BitField<9, 1, u32> has_virtual_six_axis_sensor_acceleration;
BitField<10, 1, u32> has_virtual_six_axis_sensor_angle;
BitField<11, 1, u32> is_debug_pad;
};
};
static_assert(sizeof(InternalFlags) == 4, "InternalFlags is an invalid size");
/// This is nn::hid::server::IAbstractedPad
struct IAbstractedPad {
InternalFlags internal_flags;
u64 controller_id;
u32 controller_number;
u64 low_battery_display_delay_time;
u64 low_battery_display_delay_interval;
FeatureType feature_set;
FeatureType disabled_feature_set;
AssignmentStyle assignment_style;
Core::HID::NpadStyleIndex device_type;
Core::HID::NpadInterfaceType interface_type;
Core::HID::NpadPowerInfo power_info;
u32 pad_state;
u32 button_mask;
u32 system_button_mask;
u8 indicator;
std::vector<f32> virtual_six_axis_sensor_acceleration;
std::vector<f32> virtual_six_axis_sensor_angle;
Core::HID::EmulatedController* xcd_handle;
u64 color;
};
} // namespace Service::HID

View File

@@ -0,0 +1,94 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#include "core/hle/service/set/system_settings_server.h"
#include "hid_core/hid_result.h"
#include "hid_core/resources/npad/npad_vibration.h"
namespace Service::HID {
NpadVibration::NpadVibration() {}
NpadVibration::~NpadVibration() = default;
Result NpadVibration::Activate() {
std::scoped_lock lock{mutex};
f32 master_volume = 1.0f;
m_set_sys->GetVibrationMasterVolume(&master_volume);
if (master_volume < 0.0f || master_volume > 1.0f) {
return ResultVibrationStrengthOutOfRange;
}
volume = master_volume;
return ResultSuccess;
}
Result NpadVibration::Deactivate() {
return ResultSuccess;
}
Result NpadVibration::SetSettingsService(
std::shared_ptr<Service::Set::ISystemSettingsServer> settings) {
m_set_sys = settings;
return ResultSuccess;
}
Result NpadVibration::SetVibrationMasterVolume(f32 master_volume) {
std::scoped_lock lock{mutex};
if (master_volume < 0.0f && master_volume > 1.0f) {
return ResultVibrationStrengthOutOfRange;
}
volume = master_volume;
m_set_sys->SetVibrationMasterVolume(master_volume);
return ResultSuccess;
}
Result NpadVibration::GetVibrationVolume(f32& out_volume) const {
std::scoped_lock lock{mutex};
out_volume = volume;
return ResultSuccess;
}
Result NpadVibration::GetVibrationMasterVolume(f32& out_volume) const {
std::scoped_lock lock{mutex};
f32 master_volume = 1.0f;
m_set_sys->GetVibrationMasterVolume(&master_volume);
if (master_volume < 0.0f || master_volume > 1.0f) {
return ResultVibrationStrengthOutOfRange;
}
out_volume = master_volume;
return ResultSuccess;
}
Result NpadVibration::BeginPermitVibrationSession(u64 aruid) {
std::scoped_lock lock{mutex};
session_aruid = aruid;
volume = 1.0;
return ResultSuccess;
}
Result NpadVibration::EndPermitVibrationSession() {
std::scoped_lock lock{mutex};
f32 master_volume = 1.0f;
m_set_sys->GetVibrationMasterVolume(&master_volume);
if (master_volume < 0.0f || master_volume > 1.0f) {
return ResultVibrationStrengthOutOfRange;
}
volume = master_volume;
session_aruid = 0;
return ResultSuccess;
}
u64 NpadVibration::GetSessionAruid() const {
return session_aruid;
}
} // namespace Service::HID

View File

@@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <mutex>
#include "common/common_types.h"
#include "core/hle/result.h"
namespace Service::Set {
class ISystemSettingsServer;
}
namespace Service::HID {
class NpadVibration final {
public:
explicit NpadVibration();
~NpadVibration();
Result Activate();
Result Deactivate();
Result SetSettingsService(std::shared_ptr<Service::Set::ISystemSettingsServer> settings);
Result SetVibrationMasterVolume(f32 master_volume);
Result GetVibrationVolume(f32& out_volume) const;
Result GetVibrationMasterVolume(f32& out_volume) const;
Result BeginPermitVibrationSession(u64 aruid);
Result EndPermitVibrationSession();
u64 GetSessionAruid() const;
private:
f32 volume{};
u64 session_aruid{};
mutable std::mutex mutex;
std::shared_ptr<Service::Set::ISystemSettingsServer> m_set_sys;
};
} // namespace Service::HID

View File

@@ -0,0 +1,227 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "core/core_timing.h"
#include "core/hle/kernel/k_event.h"
#include "core/hle/kernel/k_readable_event.h"
#include "core/hle/service/kernel_helpers.h"
#include "hid_core/frontend/emulated_controller.h"
#include "hid_core/hid_core.h"
#include "hid_core/resources/palma/palma.h"
namespace Service::HID {
Palma::Palma(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_)
: ControllerBase{hid_core_}, service_context{service_context_} {
controller = hid_core.GetEmulatedController(Core::HID::NpadIdType::Other);
operation_complete_event = service_context.CreateEvent("hid:PalmaOperationCompleteEvent");
}
Palma::~Palma() {
service_context.CloseEvent(operation_complete_event);
};
void Palma::OnInit() {}
void Palma::OnRelease() {}
void Palma::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
if (!IsControllerActivated()) {
return;
}
}
Result Palma::GetPalmaConnectionHandle(Core::HID::NpadIdType npad_id,
PalmaConnectionHandle& handle) {
active_handle.npad_id = npad_id;
handle = active_handle;
return ResultSuccess;
}
Result Palma::InitializePalma(const PalmaConnectionHandle& handle) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
Activate();
return ResultSuccess;
}
Kernel::KReadableEvent& Palma::AcquirePalmaOperationCompleteEvent(
const PalmaConnectionHandle& handle) const {
if (handle.npad_id != active_handle.npad_id) {
LOG_ERROR(Service_HID, "Invalid npad id {}", handle.npad_id);
}
return operation_complete_event->GetReadableEvent();
}
Result Palma::GetPalmaOperationInfo(const PalmaConnectionHandle& handle,
PalmaOperationType& operation_type,
std::span<u8> out_data) const {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
operation_type = static_cast<PalmaOperationType>(operation.operation);
std::memcpy(out_data.data(), operation.data.data(),
std::min(out_data.size(), operation.data.size()));
return ResultSuccess;
}
Result Palma::PlayPalmaActivity(const PalmaConnectionHandle& handle, u64 palma_activity) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
operation.operation = PackedPalmaOperationType::PlayActivity;
operation.result = PalmaResultSuccess;
operation.data = {};
operation_complete_event->Signal();
return ResultSuccess;
}
Result Palma::SetPalmaFrModeType(const PalmaConnectionHandle& handle, PalmaFrModeType fr_mode_) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
fr_mode = fr_mode_;
return ResultSuccess;
}
Result Palma::ReadPalmaStep(const PalmaConnectionHandle& handle) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
operation.operation = PackedPalmaOperationType::ReadStep;
operation.result = PalmaResultSuccess;
operation.data = {};
operation_complete_event->Signal();
return ResultSuccess;
}
Result Palma::EnablePalmaStep(const PalmaConnectionHandle& handle, bool is_enabled) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
return ResultSuccess;
}
Result Palma::ResetPalmaStep(const PalmaConnectionHandle& handle) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
return ResultSuccess;
}
void Palma::ReadPalmaApplicationSection() {}
void Palma::WritePalmaApplicationSection() {}
Result Palma::ReadPalmaUniqueCode(const PalmaConnectionHandle& handle) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
operation.operation = PackedPalmaOperationType::ReadUniqueCode;
operation.result = PalmaResultSuccess;
operation.data = {};
operation_complete_event->Signal();
return ResultSuccess;
}
Result Palma::SetPalmaUniqueCodeInvalid(const PalmaConnectionHandle& handle) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
operation.operation = PackedPalmaOperationType::SetUniqueCodeInvalid;
operation.result = PalmaResultSuccess;
operation.data = {};
operation_complete_event->Signal();
return ResultSuccess;
}
void Palma::WritePalmaActivityEntry() {}
Result Palma::WritePalmaRgbLedPatternEntry(const PalmaConnectionHandle& handle, u64 unknown) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
operation.operation = PackedPalmaOperationType::WriteRgbLedPatternEntry;
operation.result = PalmaResultSuccess;
operation.data = {};
operation_complete_event->Signal();
return ResultSuccess;
}
Result Palma::WritePalmaWaveEntry(const PalmaConnectionHandle& handle, PalmaWaveSet wave,
Common::ProcessAddress t_mem, u64 size) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
operation.operation = PackedPalmaOperationType::WriteWaveEntry;
operation.result = PalmaResultSuccess;
operation.data = {};
operation_complete_event->Signal();
return ResultSuccess;
}
Result Palma::SetPalmaDataBaseIdentificationVersion(const PalmaConnectionHandle& handle,
s32 database_id_version_) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
database_id_version = database_id_version_;
operation.operation = PackedPalmaOperationType::ReadDataBaseIdentificationVersion;
operation.result = PalmaResultSuccess;
operation.data[0] = {};
operation_complete_event->Signal();
return ResultSuccess;
}
Result Palma::GetPalmaDataBaseIdentificationVersion(const PalmaConnectionHandle& handle) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
operation.operation = PackedPalmaOperationType::ReadDataBaseIdentificationVersion;
operation.result = PalmaResultSuccess;
operation.data = {};
operation.data[0] = static_cast<u8>(database_id_version);
operation_complete_event->Signal();
return ResultSuccess;
}
void Palma::SuspendPalmaFeature() {}
Result Palma::GetPalmaOperationResult(const PalmaConnectionHandle& handle) const {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
return operation.result;
}
void Palma::ReadPalmaPlayLog() {}
void Palma::ResetPalmaPlayLog() {}
void Palma::SetIsPalmaAllConnectable(bool is_all_connectable) {
// If true controllers are able to be paired
is_connectable = is_all_connectable;
}
void Palma::SetIsPalmaPairedConnectable() {}
Result Palma::PairPalma(const PalmaConnectionHandle& handle) {
if (handle.npad_id != active_handle.npad_id) {
return InvalidPalmaHandle;
}
// TODO: Do something
return ResultSuccess;
}
void Palma::SetPalmaBoostMode(bool boost_mode) {}
void Palma::CancelWritePalmaWaveEntry() {}
void Palma::EnablePalmaBoostMode() {}
void Palma::GetPalmaBluetoothAddress() {}
void Palma::SetDisallowedPalmaConnection() {}
} // namespace Service::HID

View File

@@ -0,0 +1,185 @@
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <array>
#include <span>
#include "common/common_funcs.h"
#include "common/typed_address.h"
#include "hid_core/hid_result.h"
#include "hid_core/hid_types.h"
#include "hid_core/resources/controller_base.h"
namespace Kernel {
class KEvent;
class KReadableEvent;
} // namespace Kernel
namespace Service::KernelHelpers {
class ServiceContext;
}
namespace Core::HID {
class EmulatedController;
} // namespace Core::HID
namespace Service::HID {
class Palma final : public ControllerBase {
public:
using PalmaOperationData = std::array<u8, 0x140>;
using PalmaApplicationSection = std::array<u8, 0x100>;
using Address = std::array<u8, 0x6>;
// This is nn::hid::PalmaOperationType
enum class PalmaOperationType : u64 {
PlayActivity,
SetFrModeType,
ReadStep,
EnableStep,
ResetStep,
ReadApplicationSection,
WriteApplicationSection,
ReadUniqueCode,
SetUniqueCodeInvalid,
WriteActivityEntry,
WriteRgbLedPatternEntry,
WriteWaveEntry,
ReadDataBaseIdentificationVersion,
WriteDataBaseIdentificationVersion,
SuspendFeature,
ReadPlayLog,
ResetPlayLog,
};
enum class PackedPalmaOperationType : u32 {
PlayActivity,
SetFrModeType,
ReadStep,
EnableStep,
ResetStep,
ReadApplicationSection,
WriteApplicationSection,
ReadUniqueCode,
SetUniqueCodeInvalid,
WriteActivityEntry,
WriteRgbLedPatternEntry,
WriteWaveEntry,
ReadDataBaseIdentificationVersion,
WriteDataBaseIdentificationVersion,
SuspendFeature,
ReadPlayLog,
ResetPlayLog,
};
// This is nn::hid::PalmaWaveSet
enum class PalmaWaveSet : u64 {
Small,
Medium,
Large,
};
// This is nn::hid::PalmaFrModeType
enum class PalmaFrModeType : u64 {
Off,
B01,
B02,
B03,
Downloaded,
};
// This is nn::hid::PalmaFeature
enum class PalmaFeature : u64 {
FrMode,
RumbleFeedback,
Step,
MuteSwitch,
};
// This is nn::hid::PalmaOperationInfo
struct PalmaOperationInfo {
PackedPalmaOperationType operation{};
Result result{PalmaResultSuccess};
PalmaOperationData data{};
};
static_assert(sizeof(PalmaOperationInfo) == 0x148, "PalmaOperationInfo is an invalid size");
// This is nn::hid::PalmaActivityEntry
struct PalmaActivityEntry {
u32 rgb_led_pattern_index;
INSERT_PADDING_BYTES(2);
PalmaWaveSet wave_set;
u32 wave_index;
INSERT_PADDING_BYTES(12);
};
static_assert(sizeof(PalmaActivityEntry) == 0x20, "PalmaActivityEntry is an invalid size");
struct PalmaConnectionHandle {
alignas(8) Core::HID::NpadIdType npad_id;
};
static_assert(sizeof(PalmaConnectionHandle) == 0x8,
"PalmaConnectionHandle has incorrect size.");
explicit Palma(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_);
~Palma() override;
// Called when the controller is initialized
void OnInit() override;
// When the controller is released
void OnRelease() override;
// When the controller is requesting an update for the shared memory
void OnUpdate(const Core::Timing::CoreTiming& core_timing) override;
Result GetPalmaConnectionHandle(Core::HID::NpadIdType npad_id, PalmaConnectionHandle& handle);
Result InitializePalma(const PalmaConnectionHandle& handle);
Kernel::KReadableEvent& AcquirePalmaOperationCompleteEvent(
const PalmaConnectionHandle& handle) const;
Result GetPalmaOperationInfo(const PalmaConnectionHandle& handle,
PalmaOperationType& operation_type, std::span<u8> out_data) const;
Result PlayPalmaActivity(const PalmaConnectionHandle& handle, u64 palma_activity);
Result SetPalmaFrModeType(const PalmaConnectionHandle& handle, PalmaFrModeType fr_mode_);
Result ReadPalmaStep(const PalmaConnectionHandle& handle);
Result EnablePalmaStep(const PalmaConnectionHandle& handle, bool is_enabled);
Result ResetPalmaStep(const PalmaConnectionHandle& handle);
Result ReadPalmaUniqueCode(const PalmaConnectionHandle& handle);
Result SetPalmaUniqueCodeInvalid(const PalmaConnectionHandle& handle);
Result WritePalmaRgbLedPatternEntry(const PalmaConnectionHandle& handle, u64 unknown);
Result WritePalmaWaveEntry(const PalmaConnectionHandle& handle, PalmaWaveSet wave,
Common::ProcessAddress t_mem, u64 size);
Result SetPalmaDataBaseIdentificationVersion(const PalmaConnectionHandle& handle,
s32 database_id_version_);
Result GetPalmaDataBaseIdentificationVersion(const PalmaConnectionHandle& handle);
Result GetPalmaOperationResult(const PalmaConnectionHandle& handle) const;
void SetIsPalmaAllConnectable(bool is_all_connectable);
Result PairPalma(const PalmaConnectionHandle& handle);
void SetPalmaBoostMode(bool boost_mode);
private:
void ReadPalmaApplicationSection();
void WritePalmaApplicationSection();
void WritePalmaActivityEntry();
void SuspendPalmaFeature();
void ReadPalmaPlayLog();
void ResetPalmaPlayLog();
void SetIsPalmaPairedConnectable();
void CancelWritePalmaWaveEntry();
void EnablePalmaBoostMode();
void GetPalmaBluetoothAddress();
void SetDisallowedPalmaConnection();
bool is_connectable{};
s32 database_id_version{};
PalmaOperationInfo operation{};
PalmaFrModeType fr_mode{};
PalmaConnectionHandle active_handle{};
Core::HID::EmulatedController* controller;
Kernel::KEvent* operation_complete_event;
KernelHelpers::ServiceContext& service_context;
};
} // namespace Service::HID

Some files were not shown because too many files have changed in this diff Show More