From 5857aea94ef52186f64a6794adf418ba92329329 Mon Sep 17 00:00:00 2001 From: David Marcec Date: Sat, 6 Oct 2018 13:14:42 +1000 Subject: [PATCH] Addressed changes for better hid --- .../hid/controllers/controller_base.cpp | 5 +- .../service/hid/controllers/controller_base.h | 6 +- .../hle/service/hid/controllers/debug_pad.cpp | 13 ++- .../hle/service/hid/controllers/debug_pad.h | 7 +- .../hle/service/hid/controllers/gesture.cpp | 15 ++- .../hle/service/hid/controllers/gesture.h | 7 +- .../hle/service/hid/controllers/keyboard.cpp | 15 ++- .../hle/service/hid/controllers/keyboard.h | 9 +- .../hle/service/hid/controllers/mouse.cpp | 13 ++- src/core/hle/service/hid/controllers/mouse.h | 7 +- src/core/hle/service/hid/controllers/npad.cpp | 101 +++++++++--------- src/core/hle/service/hid/controllers/npad.h | 63 ++++++----- .../hle/service/hid/controllers/stubbed.cpp | 13 ++- .../hle/service/hid/controllers/stubbed.h | 11 +- .../service/hid/controllers/touchscreen.cpp | 17 ++- .../hle/service/hid/controllers/touchscreen.h | 14 ++- src/core/hle/service/hid/controllers/xpad.cpp | 15 ++- src/core/hle/service/hid/controllers/xpad.h | 7 +- src/core/hle/service/hid/hid.cpp | 67 ++++++------ 19 files changed, 238 insertions(+), 167 deletions(-) diff --git a/src/core/hle/service/hid/controllers/controller_base.cpp b/src/core/hle/service/hid/controllers/controller_base.cpp index f2aef56421..1625e9c3d0 100644 --- a/src/core/hle/service/hid/controllers/controller_base.cpp +++ b/src/core/hle/service/hid/controllers/controller_base.cpp @@ -5,6 +5,8 @@ #include "core/hle/service/hid/controllers/controller_base.h" namespace Service::HID { +ControllerBase::~ControllerBase() = default; + void ControllerBase::ActivateController() { if (is_activated) { OnRelease(); @@ -23,5 +25,4 @@ void ControllerBase::DeactivateController() { bool ControllerBase::IsControllerActivated() const { return is_activated; } -// ControllerBase::~ControllerBase() = default; -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/controller_base.h b/src/core/hle/service/hid/controllers/controller_base.h index 3b1c8aad6b..fa98e2354c 100644 --- a/src/core/hle/service/hid/controllers/controller_base.h +++ b/src/core/hle/service/hid/controllers/controller_base.h @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #pragma once + #include "common/common_types.h" #include "common/swap.h" @@ -10,6 +11,7 @@ namespace Service::HID { class ControllerBase { public: ControllerBase() = default; + virtual ~ControllerBase() = 0; // Called when the controller is initialized virtual void OnInit() = 0; @@ -18,7 +20,7 @@ public: virtual void OnRelease() = 0; // When the controller is requesting an update for the shared memory - virtual void OnUpdate(u8* data, size_t size) = 0; + virtual void OnUpdate(u8* data, std::size_t size) = 0; // Called when input devices should be loaded virtual void OnLoadInputDevices() = 0; @@ -40,4 +42,4 @@ protected: }; static_assert(sizeof(CommonHeader) == 0x20, "CommonHeader is an invalid size"); }; -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/debug_pad.cpp b/src/core/hle/service/hid/controllers/debug_pad.cpp index 04799b233b..6f8ef6e3fb 100644 --- a/src/core/hle/service/hid/controllers/debug_pad.cpp +++ b/src/core/hle/service/hid/controllers/debug_pad.cpp @@ -2,15 +2,21 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "common/common_types.h" #include "common/swap.h" #include "core/core_timing.h" #include "core/hle/service/hid/controllers/debug_pad.h" namespace Service::HID { + +Controller_DebugPad::Controller_DebugPad() = default; + void Controller_DebugPad::OnInit() {} + void Controller_DebugPad::OnRelease() {} -void Controller_DebugPad::OnUpdate(u8* data, size_t size) { + +void Controller_DebugPad::OnUpdate(u8* data, std::size_t size) { shared_memory.header.timestamp = CoreTiming::GetTicks(); shared_memory.header.total_entry_count = 17; @@ -21,7 +27,7 @@ void Controller_DebugPad::OnUpdate(u8* data, size_t size) { } shared_memory.header.entry_count = 16; - auto& last_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; + const auto& last_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; auto& cur_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; @@ -31,5 +37,6 @@ void Controller_DebugPad::OnUpdate(u8* data, size_t size) { std::memcpy(data, &shared_memory, sizeof(SharedMemory)); } + void Controller_DebugPad::OnLoadInputDevices() {} -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/debug_pad.h b/src/core/hle/service/hid/controllers/debug_pad.h index fc6f99a0b1..e35675fa16 100644 --- a/src/core/hle/service/hid/controllers/debug_pad.h +++ b/src/core/hle/service/hid/controllers/debug_pad.h @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #pragma once + #include #include "common/common_funcs.h" #include "common/common_types.h" @@ -12,7 +13,7 @@ namespace Service::HID { class Controller_DebugPad final : public ControllerBase { public: - Controller_DebugPad() = default; + Controller_DebugPad(); // Called when the controller is initialized void OnInit() override; @@ -21,7 +22,7 @@ public: void OnRelease() override; // When the controller is requesting an update for the shared memory - void OnUpdate(u8* data, size_t size) override; + void OnUpdate(u8* data, std::size_t size) override; // Called when input devices should be loaded void OnLoadInputDevices() override; @@ -51,4 +52,4 @@ private: static_assert(sizeof(SharedMemory) == 0x400, "SharedMemory is an invalid size"); SharedMemory shared_memory{}; }; -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp index 851ea66f34..b473b9e2b6 100644 --- a/src/core/hle/service/hid/controllers/gesture.cpp +++ b/src/core/hle/service/hid/controllers/gesture.cpp @@ -2,16 +2,22 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "common/common_types.h" #include "common/swap.h" #include "core/core_timing.h" #include "core/hle/service/hid/controllers/gesture.h" namespace Service::HID { -constexpr size_t SHARED_MEMORY_OFFSET = 0x3BA00; +constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3BA00; + +Controller_Gesture::Controller_Gesture() = default; + void Controller_Gesture::OnInit() {} + void Controller_Gesture::OnRelease() {} -void Controller_Gesture::OnUpdate(u8* data, size_t size) { + +void Controller_Gesture::OnUpdate(u8* data, std::size_t size) { shared_memory.header.timestamp = CoreTiming::GetTicks(); shared_memory.header.total_entry_count = 17; @@ -22,7 +28,7 @@ void Controller_Gesture::OnUpdate(u8* data, size_t size) { } shared_memory.header.entry_count = 16; - auto& last_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; + const auto& last_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; auto& cur_entry = shared_memory.gesture_states[shared_memory.header.last_entry_index]; @@ -32,5 +38,6 @@ void Controller_Gesture::OnUpdate(u8* data, size_t size) { std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory)); } + void Controller_Gesture::OnLoadInputDevices() {} -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/gesture.h b/src/core/hle/service/hid/controllers/gesture.h index f468198296..0ced50dfda 100644 --- a/src/core/hle/service/hid/controllers/gesture.h +++ b/src/core/hle/service/hid/controllers/gesture.h @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #pragma once + #include #include "common/common_types.h" #include "common/swap.h" @@ -11,7 +12,7 @@ namespace Service::HID { class Controller_Gesture final : public ControllerBase { public: - Controller_Gesture() = default; + Controller_Gesture(); // Called when the controller is initialized void OnInit() override; @@ -48,7 +49,7 @@ private: f32 scale; f32 rotation; s32_le location_count; - std::array locations{}; + std::array locations; }; static_assert(sizeof(GestureState) == 0x68, "GestureState is an invalid size"); @@ -58,4 +59,4 @@ private: }; SharedMemory shared_memory{}; }; -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/keyboard.cpp b/src/core/hle/service/hid/controllers/keyboard.cpp index 27c39ad08b..089c02ac41 100644 --- a/src/core/hle/service/hid/controllers/keyboard.cpp +++ b/src/core/hle/service/hid/controllers/keyboard.cpp @@ -2,16 +2,22 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "common/common_types.h" #include "common/swap.h" #include "core/core_timing.h" #include "core/hle/service/hid/controllers/keyboard.h" namespace Service::HID { -constexpr size_t SHARED_MEMORY_OFFSET = 0x3800; +constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3800; + +Controller_Keyboard::Controller_Keyboard() = default; + void Controller_Keyboard::OnInit() {} + void Controller_Keyboard::OnRelease() {} -void Controller_Keyboard::OnUpdate(u8* data, size_t size) { + +void Controller_Keyboard::OnUpdate(u8* data, std::size_t size) { shared_memory.header.timestamp = CoreTiming::GetTicks(); shared_memory.header.total_entry_count = 17; @@ -22,7 +28,7 @@ void Controller_Keyboard::OnUpdate(u8* data, size_t size) { } shared_memory.header.entry_count = 16; - auto& last_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; + const auto& last_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; auto& cur_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; @@ -32,5 +38,6 @@ void Controller_Keyboard::OnUpdate(u8* data, size_t size) { std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory)); } + void Controller_Keyboard::OnLoadInputDevices() {} -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/keyboard.h b/src/core/hle/service/hid/controllers/keyboard.h index 0463e16197..778e14f7e5 100644 --- a/src/core/hle/service/hid/controllers/keyboard.h +++ b/src/core/hle/service/hid/controllers/keyboard.h @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #pragma once + #include #include "common/common_funcs.h" #include "common/common_types.h" @@ -12,7 +13,7 @@ namespace Service::HID { class Controller_Keyboard final : public ControllerBase { public: - Controller_Keyboard() = default; + Controller_Keyboard(); // Called when the controller is initialized void OnInit() override; @@ -21,7 +22,7 @@ public: void OnRelease() override; // When the controller is requesting an update for the shared memory - void OnUpdate(u8* data, size_t size) override; + void OnUpdate(u8* data, std::size_t size) override; // Called when input devices should be loaded void OnLoadInputDevices() override; @@ -33,7 +34,7 @@ private: s32_le modifier; s32_le attribute; - std::array key{}; + std::array key; }; static_assert(sizeof(KeyboardState) == 0x38, "KeyboardState is an invalid size"); @@ -45,4 +46,4 @@ private: static_assert(sizeof(SharedMemory) == 0x400, "SharedMemory is an invalid size"); SharedMemory shared_memory{}; }; -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/mouse.cpp b/src/core/hle/service/hid/controllers/mouse.cpp index df30239e20..78e9b5e9e5 100644 --- a/src/core/hle/service/hid/controllers/mouse.cpp +++ b/src/core/hle/service/hid/controllers/mouse.cpp @@ -2,16 +2,22 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "common/common_types.h" #include "common/swap.h" #include "core/core_timing.h" #include "core/hle/service/hid/controllers/mouse.h" namespace Service::HID { -constexpr size_t SHARED_MEMORY_OFFSET = 0x3400; +constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3400; + +Controller_Mouse::Controller_Mouse() = default; + void Controller_Mouse::OnInit() {} + void Controller_Mouse::OnRelease() {} -void Controller_Mouse::OnUpdate(u8* data, size_t size) { + +void Controller_Mouse::OnUpdate(u8* data, std::size_t size) { shared_memory.header.timestamp = CoreTiming::GetTicks(); shared_memory.header.total_entry_count = 17; @@ -32,5 +38,6 @@ void Controller_Mouse::OnUpdate(u8* data, size_t size) { std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory)); } + void Controller_Mouse::OnLoadInputDevices() {} -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/mouse.h b/src/core/hle/service/hid/controllers/mouse.h index 0d7fd372d2..05358a4f52 100644 --- a/src/core/hle/service/hid/controllers/mouse.h +++ b/src/core/hle/service/hid/controllers/mouse.h @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #pragma once + #include #include "common/common_types.h" #include "common/swap.h" @@ -11,7 +12,7 @@ namespace Service::HID { class Controller_Mouse final : public ControllerBase { public: - Controller_Mouse() = default; + Controller_Mouse(); // Called when the controller is initialized void OnInit() override; @@ -20,7 +21,7 @@ public: void OnRelease() override; // When the controller is requesting an update for the shared memory - void OnUpdate(u8* data, size_t size) override; + void OnUpdate(u8* data, std::size_t size) override; // Called when input devices should be loaded void OnLoadInputDevices() override; @@ -45,4 +46,4 @@ private: }; SharedMemory shared_memory{}; }; -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp index a53687b91e..8a713a4078 100644 --- a/src/core/hle/service/hid/controllers/npad.cpp +++ b/src/core/hle/service/hid/controllers/npad.cpp @@ -3,10 +3,14 @@ // Refer to the license.txt file included. #pragma once + +#include #include +#include #include "common/assert.h" #include "common/bit_field.h" #include "common/common_types.h" +#include "common/logging/log.h" #include "common/swap.h" #include "core/core.h" #include "core/core_timing.h" @@ -22,15 +26,13 @@ constexpr u32 JOYCON_BODY_NEON_BLUE = 0x0AB9E6; constexpr u32 JOYCON_BUTTONS_NEON_BLUE = 0x001E1E; constexpr s32 HID_JOYSTICK_MAX = 0x7fff; constexpr s32 HID_JOYSTICK_MIN = -0x7fff; -constexpr size_t NPAD_OFFSET = 0x9A00; -constexpr size_t MAX_CONTROLLER_COUNT = 9; +constexpr std::size_t NPAD_OFFSET = 0x9A00; -enum class JoystickId : size_t { Joystick_Left, Joystick_Right }; -constexpr std::array NPAD_ID_LIST{0, 1, 2, 3, 4, 5, 6, 7, 32}; -size_t CONTROLLER_COUNT{}; -std::array CONNECTED_CONTROLLERS{}; +enum class JoystickId : std::size_t { Joystick_Left, Joystick_Right }; -void Controller_NPad::InitNewlyAddedControler(size_t controller_idx) { +Controller_NPad::Controller_NPad() = default; + +void Controller_NPad::InitNewlyAddedControler(std::size_t controller_idx) { const auto controller_type = CONNECTED_CONTROLLERS[controller_idx]; auto& controller = shared_memory_entries[controller_idx]; if (controller_type == NPadControllerType::None) { @@ -79,7 +81,7 @@ void Controller_NPad::InitNewlyAddedControler(size_t controller_idx) { controller.right_color.body_color = JOYCON_BODY_NEON_RED; controller.right_color.button_color = JOYCON_BUTTONS_NEON_RED; - controller.properties.is_verticle.Assign(1); // TODO(ogniK): Swap joycons orientations + controller.properties.is_vertical.Assign(1); // TODO(ogniK): Swap joycons orientations } void Controller_NPad::OnInit() { @@ -89,7 +91,7 @@ void Controller_NPad::OnInit() { if (!IsControllerActivated()) return; - size_t controller{}; + std::size_t controller{}; supported_npad_id_types.resize(NPAD_ID_LIST.size()); if (style.raw == 0) { // We want to support all controllers @@ -118,10 +120,10 @@ void Controller_NPad::OnLoadInputDevices() { void Controller_NPad::OnRelease() {} -void Controller_NPad::OnUpdate(u8* data, size_t data_len) { +void Controller_NPad::OnUpdate(u8* data, std::size_t data_len) { if (!IsControllerActivated()) return; - for (size_t i = 0; i < shared_memory_entries.size(); i++) { + for (std::size_t i = 0; i < shared_memory_entries.size(); i++) { auto& npad = shared_memory_entries[i]; const std::array controller_npads{&npad.main_controller_states, &npad.handheld_states, @@ -131,11 +133,12 @@ void Controller_NPad::OnUpdate(u8* data, size_t data_len) { &npad.pokeball_states, &npad.libnx}; - for (auto main_controller : controller_npads) { + for (auto* main_controller : controller_npads) { main_controller->common.entry_count = 16; main_controller->common.total_entry_count = 17; - auto& last_entry = main_controller->npad[main_controller->common.last_entry_index]; + const auto& last_entry = + main_controller->npad[main_controller->common.last_entry_index]; main_controller->common.timestamp = CoreTiming::GetTicks(); main_controller->common.last_entry_index = @@ -154,7 +157,7 @@ void Controller_NPad::OnUpdate(u8* data, size_t data_len) { const auto& controller_type = CONNECTED_CONTROLLERS[i]; // Pad states - auto pad_state = ControllerPadState{}; + ControllerPadState pad_state{}; using namespace Settings::NativeButton; pad_state.a.Assign(buttons[A - BUTTON_HID_BEGIN]->GetStatus()); pad_state.b.Assign(buttons[B - BUTTON_HID_BEGIN]->GetStatus()); @@ -169,20 +172,20 @@ void Controller_NPad::OnUpdate(u8* data, size_t data_len) { pad_state.plus.Assign(buttons[Plus - BUTTON_HID_BEGIN]->GetStatus()); pad_state.minus.Assign(buttons[Minus - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.dleft.Assign(buttons[DLeft - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.dup.Assign(buttons[DUp - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.dright.Assign(buttons[DRight - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.ddown.Assign(buttons[DDown - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.d_left.Assign(buttons[DLeft - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.d_up.Assign(buttons[DUp - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.d_right.Assign(buttons[DRight - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.d_down.Assign(buttons[DDown - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.lstickleft.Assign(buttons[LStick_Left - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.lstickup.Assign(buttons[LStick_Up - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.lstickright.Assign(buttons[LStick_Right - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.lstickdown.Assign(buttons[LStick_Down - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.l_stick_left.Assign(buttons[LStick_Left - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.l_stick_up.Assign(buttons[LStick_Up - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.l_stick_right.Assign(buttons[LStick_Right - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.l_stick_down.Assign(buttons[LStick_Down - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.rstickleft.Assign(buttons[RStick_Left - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.rstickup.Assign(buttons[RStick_Up - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.rstickright.Assign(buttons[RStick_Right - BUTTON_HID_BEGIN]->GetStatus()); - pad_state.rstickdown.Assign(buttons[RStick_Down - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.r_stick_left.Assign(buttons[RStick_Left - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.r_stick_up.Assign(buttons[RStick_Up - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.r_stick_right.Assign(buttons[RStick_Right - BUTTON_HID_BEGIN]->GetStatus()); + pad_state.r_stick_down.Assign(buttons[RStick_Down - BUTTON_HID_BEGIN]->GetStatus()); pad_state.sl.Assign(buttons[SL - BUTTON_HID_BEGIN]->GetStatus()); pad_state.sr.Assign(buttons[SR - BUTTON_HID_BEGIN]->GetStatus()); @@ -191,9 +194,9 @@ void Controller_NPad::OnUpdate(u8* data, size_t data_len) { AnalogPosition rstick_entry{}; const auto [stick_l_x_f, stick_l_y_f] = - sticks[static_cast(JoystickId::Joystick_Left)]->GetStatus(); + sticks[static_cast(JoystickId::Joystick_Left)]->GetStatus(); const auto [stick_r_x_f, stick_r_y_f] = - sticks[static_cast(JoystickId::Joystick_Right)]->GetStatus(); + sticks[static_cast(JoystickId::Joystick_Right)]->GetStatus(); lstick_entry.x = static_cast(stick_l_x_f * HID_JOYSTICK_MAX); lstick_entry.y = static_cast(stick_l_y_f * HID_JOYSTICK_MAX); rstick_entry.x = static_cast(stick_r_x_f * HID_JOYSTICK_MAX); @@ -220,26 +223,26 @@ void Controller_NPad::OnUpdate(u8* data, size_t data_len) { handheld_entry.connection_status.IsConnected.Assign(1); handheld_entry.connection_status.IsWired.Assign(1); handheld_entry.pad_states.raw = pad_state.raw; - handheld_entry.lstick = lstick_entry; - handheld_entry.rstick = rstick_entry; + handheld_entry.l_stick = lstick_entry; + handheld_entry.r_stick = rstick_entry; break; case NPadControllerType::JoyLeft: left_entry.connection_status.IsConnected.Assign(1); left_entry.pad_states.raw = pad_state.raw; - left_entry.lstick = lstick_entry; - left_entry.rstick = rstick_entry; + left_entry.l_stick = lstick_entry; + left_entry.r_stick = rstick_entry; break; case NPadControllerType::JoyRight: right_entry.connection_status.IsConnected.Assign(1); right_entry.pad_states.raw = pad_state.raw; - right_entry.lstick = lstick_entry; - right_entry.rstick = rstick_entry; + right_entry.l_stick = lstick_entry; + right_entry.r_stick = rstick_entry; break; case NPadControllerType::Tabletop: // TODO(ogniK): Figure out how to add proper tabletop support dual_entry.pad_states.raw = pad_state.raw; - dual_entry.lstick = lstick_entry; - dual_entry.rstick = rstick_entry; + dual_entry.l_stick = lstick_entry; + dual_entry.r_stick = rstick_entry; dual_entry.connection_status.IsConnected.Assign(1); break; case NPadControllerType::Pokeball: @@ -247,13 +250,13 @@ void Controller_NPad::OnUpdate(u8* data, size_t data_len) { pokeball_entry.connection_status.IsWired.Assign(1); pokeball_entry.pad_states.raw = pad_state.raw; - pokeball_entry.lstick = lstick_entry; - pokeball_entry.rstick = rstick_entry; + pokeball_entry.l_stick = lstick_entry; + pokeball_entry.r_stick = rstick_entry; break; case NPadControllerType::ProController: main_controller.pad_states.raw = pad_state.raw; - main_controller.lstick = lstick_entry; - main_controller.rstick = rstick_entry; + main_controller.l_stick = lstick_entry; + main_controller.r_stick = rstick_entry; main_controller.connection_status.IsConnected.Assign(1); main_controller.connection_status.IsWired.Assign(1); break; @@ -264,8 +267,8 @@ void Controller_NPad::OnUpdate(u8* data, size_t data_len) { libnx_entry.connection_status.IsConnected.Assign(1); libnx_entry.connection_status.IsWired.Assign(1); libnx_entry.pad_states.raw = pad_state.raw; - libnx_entry.lstick = lstick_entry; - libnx_entry.rstick = rstick_entry; + libnx_entry.l_stick = lstick_entry; + libnx_entry.r_stick = rstick_entry; } std::memcpy(data + NPAD_OFFSET, shared_memory_entries.data(), shared_memory_entries.size() * sizeof(NPadEntry)); @@ -279,18 +282,18 @@ Controller_NPad::NPadType Controller_NPad::GetSupportedStyleSet() const { return style; } -void Controller_NPad::SetSupportedNPadIdTypes(u8* data, size_t length) { +void Controller_NPad::SetSupportedNPadIdTypes(u8* data, std::size_t length) { ASSERT(length > 0 && (length % sizeof(u32)) == 0); - supported_npad_id_types.resize(length / 4); + supported_npad_id_types.resize(length / sizeof(u32)); std::memcpy(supported_npad_id_types.data(), data, length); } -void Controller_NPad::GetSupportedNpadIdTypes(u32* data, size_t max_length) { +const void Controller_NPad::GetSupportedNpadIdTypes(u32* data, std::size_t max_length) { ASSERT(max_length < supported_npad_id_types.size()); std::memcpy(data, supported_npad_id_types.data(), supported_npad_id_types.size()); } -size_t Controller_NPad::GetSupportedNPadIdTypesSize() const { +std::size_t Controller_NPad::GetSupportedNPadIdTypesSize() const { return supported_npad_id_types.size(); } @@ -308,7 +311,7 @@ void Controller_NPad::SetNpadMode(u32 npad_id, NPadAssignments assignment_mode) void Controller_NPad::VibrateController(const std::vector& controller_ids, const std::vector& vibrations) { - for (size_t i = 0; i < controller_ids.size(); i++) { + for (std::size_t i = 0; i < controller_ids.size(); i++) { if (i >= CONTROLLER_COUNT) { continue; } @@ -326,11 +329,11 @@ Controller_NPad::Vibration Controller_NPad::GetLastVibration() const { return last_processed_vibration; } void Controller_NPad::AddNewController(NPadControllerType controller) { - if (CONTROLLER_COUNT >= MAX_CONTROLLER_COUNT) { + if (CONTROLLER_COUNT >= CONNECTED_CONTROLLERS.size()) { LOG_ERROR(Service_HID, "Cannot connect any more controllers!"); return; } CONNECTED_CONTROLLERS[CONTROLLER_COUNT] = controller; InitNewlyAddedControler(CONTROLLER_COUNT++); } -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h index e57b67da4b..e5c212aa58 100644 --- a/src/core/hle/service/hid/controllers/npad.h +++ b/src/core/hle/service/hid/controllers/npad.h @@ -3,17 +3,24 @@ // Refer to the license.txt file included. #pragma once + #include #include "common/common_types.h" -#include "core/frontend/input.h" #include "core/hle/service/hid/controllers/controller_base.h" #include "core/settings.h" +namespace Input { +template +class InputDevice; +using ButtonDevice = InputDevice; +using AnalogDevice = InputDevice>; +} // namespace Input + namespace Service::HID { class Controller_NPad final : public ControllerBase { public: - Controller_NPad() = default; + Controller_NPad(); // Called when the controller is initialized void OnInit() override; @@ -22,7 +29,7 @@ public: void OnRelease() override; // When the controller is requesting an update for the shared memory - void OnUpdate(u8* data, size_t size) override; + void OnUpdate(u8* data, std::size_t size) override; // Called when input devices should be loaded void OnLoadInputDevices() override; @@ -67,15 +74,15 @@ public: JoyLeft, JoyRight, Tabletop, - Pokeball + Pokeball, }; void SetSupportedStyleSet(NPadType style_set); NPadType GetSupportedStyleSet() const; - void SetSupportedNPadIdTypes(u8* data, size_t length); - void GetSupportedNpadIdTypes(u32* data, size_t max_length); - size_t GetSupportedNPadIdTypesSize() const; + void SetSupportedNPadIdTypes(u8* data, std::size_t length); + const void GetSupportedNpadIdTypes(u32* data, std::size_t max_length); + std::size_t GetSupportedNPadIdTypesSize() const; void SetHoldType(NpadHoldType joy_hold_type); NpadHoldType GetHoldType() const; @@ -123,22 +130,22 @@ private: BitField<11, 1, u64_le> minus; // D-Pad - BitField<12, 1, u64_le> dleft; - BitField<13, 1, u64_le> dup; - BitField<14, 1, u64_le> dright; - BitField<15, 1, u64_le> ddown; + BitField<12, 1, u64_le> d_left; + BitField<13, 1, u64_le> d_up; + BitField<14, 1, u64_le> d_right; + BitField<15, 1, u64_le> d_down; // Left JoyStick - BitField<16, 1, u64_le> lstickleft; - BitField<17, 1, u64_le> lstickup; - BitField<18, 1, u64_le> lstickright; - BitField<19, 1, u64_le> lstickdown; + BitField<16, 1, u64_le> l_stick_left; + BitField<17, 1, u64_le> l_stick_up; + BitField<18, 1, u64_le> l_stick_right; + BitField<19, 1, u64_le> l_stick_down; // Right JoyStick - BitField<20, 1, u64_le> rstickleft; - BitField<21, 1, u64_le> rstickup; - BitField<22, 1, u64_le> rstickright; - BitField<23, 1, u64_le> rstickdown; + BitField<20, 1, u64_le> r_stick_left; + BitField<21, 1, u64_le> r_stick_up; + BitField<22, 1, u64_le> r_stick_right; + BitField<23, 1, u64_le> r_stick_down; // Not always active? BitField<24, 1, u64_le> sl; @@ -166,8 +173,8 @@ private: s64_le timestamp; s64_le timestamp2; ControllerPadState pad_states; - AnalogPosition lstick; - AnalogPosition rstick; + AnalogPosition l_stick; + AnalogPosition r_stick; ConnectionState connection_status; }; static_assert(sizeof(GenericStates) == 0x30, "NPadGenericStates is an invalid size"); @@ -187,7 +194,7 @@ private: struct NPadProperties { union { s64_le raw{}; - BitField<11, 1, s64_le> is_verticle; + BitField<11, 1, s64_le> is_vertical; BitField<12, 1, s64_le> is_horizontal; }; }; @@ -226,7 +233,7 @@ private: // relying on this for the time being INSERT_PADDING_BYTES( 0x708 * - 6); // TODO(ogniK)L SixAxis states, require more information before implementation + 6); // TODO(ogniK): SixAxis states, require more information before implementation NPadDevice device_type; NPadProperties properties; INSERT_PADDING_WORDS(4); @@ -242,8 +249,12 @@ private: std::vector supported_npad_id_types{}; NpadHoldType hold_type{NpadHoldType::Vertical}; Kernel::SharedPtr styleset_changed_event; - size_t dump_idx{}; + std::size_t dump_idx{}; Vibration last_processed_vibration{}; - void InitNewlyAddedControler(size_t controller_idx); + std::size_t CONTROLLER_COUNT{}; + const std::array NPAD_ID_LIST{0, 1, 2, 3, 4, 5, 6, 7, 32}; + std::array CONNECTED_CONTROLLERS{}; + + void InitNewlyAddedControler(std::size_t controller_idx); }; -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/stubbed.cpp b/src/core/hle/service/hid/controllers/stubbed.cpp index 0a602fce2b..3a13d5991e 100644 --- a/src/core/hle/service/hid/controllers/stubbed.cpp +++ b/src/core/hle/service/hid/controllers/stubbed.cpp @@ -2,15 +2,21 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "common/common_types.h" #include "common/swap.h" #include "core/core_timing.h" #include "core/hle/service/hid/controllers/stubbed.h" namespace Service::HID { + +Controller_Stubbed::Controller_Stubbed() = default; + void Controller_Stubbed::OnInit() {} + void Controller_Stubbed::OnRelease() {} -void Controller_Stubbed::OnUpdate(u8* data, size_t size) { + +void Controller_Stubbed::OnUpdate(u8* data, std::size_t size) { if (!smart_update) { return; } @@ -23,10 +29,11 @@ void Controller_Stubbed::OnUpdate(u8* data, size_t size) { std::memcpy(data + common_offset, &header, sizeof(CommonHeader)); } + void Controller_Stubbed::OnLoadInputDevices() {} -void Controller_Stubbed::SetCommonHeaderOffset(size_t off) { +void Controller_Stubbed::SetCommonHeaderOffset(std::size_t off) { common_offset = off; smart_update = true; } -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/stubbed.h b/src/core/hle/service/hid/controllers/stubbed.h index ec7adacb35..9c1b57f83e 100644 --- a/src/core/hle/service/hid/controllers/stubbed.h +++ b/src/core/hle/service/hid/controllers/stubbed.h @@ -3,13 +3,14 @@ // Refer to the license.txt file included. #pragma once + #include "common/common_types.h" #include "core/hle/service/hid/controllers/controller_base.h" namespace Service::HID { class Controller_Stubbed final : public ControllerBase { public: - Controller_Stubbed() = default; + Controller_Stubbed(); // Called when the controller is initialized void OnInit() override; @@ -18,15 +19,15 @@ public: void OnRelease() override; // When the controller is requesting an update for the shared memory - void OnUpdate(u8* data, size_t size) override; + void OnUpdate(u8* data, std::size_t size) override; // Called when input devices should be loaded void OnLoadInputDevices() override; - void SetCommonHeaderOffset(size_t off); + void SetCommonHeaderOffset(std::size_t off); private: bool smart_update{}; - size_t common_offset{}; + std::size_t common_offset{}; }; -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/touchscreen.cpp b/src/core/hle/service/hid/controllers/touchscreen.cpp index b675dec8e5..e97f84ea14 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.cpp +++ b/src/core/hle/service/hid/controllers/touchscreen.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "common/common_types.h" #include "common/swap.h" #include "core/core_timing.h" @@ -11,10 +12,15 @@ #include "core/settings.h" namespace Service::HID { -constexpr size_t SHARED_MEMORY_OFFSET = 0x400; +constexpr std::size_t SHARED_MEMORY_OFFSET = 0x400; + +Controller_Touchscreen::Controller_Touchscreen() = default; + void Controller_Touchscreen::OnInit() {} + void Controller_Touchscreen::OnRelease() {} -void Controller_Touchscreen::OnUpdate(u8* data, size_t size) { + +void Controller_Touchscreen::OnUpdate(u8* data, std::size_t size) { shared_memory.header.timestamp = CoreTiming::GetTicks(); shared_memory.header.total_entry_count = 17; @@ -25,14 +31,15 @@ void Controller_Touchscreen::OnUpdate(u8* data, size_t size) { } shared_memory.header.entry_count = 16; - auto& last_entry = shared_memory.shared_memory_entries[shared_memory.header.last_entry_index]; + const auto& last_entry = + shared_memory.shared_memory_entries[shared_memory.header.last_entry_index]; shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; auto& cur_entry = shared_memory.shared_memory_entries[shared_memory.header.last_entry_index]; cur_entry.sampling_number = last_entry.sampling_number + 1; cur_entry.sampling_number2 = cur_entry.sampling_number; - auto [x, y, pressed] = touch_device->GetStatus(); + const auto [x, y, pressed] = touch_device->GetStatus(); auto& touch_entry = cur_entry.states[0]; if (pressed) { touch_entry.x = static_cast(x * Layout::ScreenUndocked::Width); @@ -55,4 +62,4 @@ void Controller_Touchscreen::OnUpdate(u8* data, size_t size) { void Controller_Touchscreen::OnLoadInputDevices() { touch_device = Input::CreateDevice(Settings::values.touch_device); } -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/touchscreen.h b/src/core/hle/service/hid/controllers/touchscreen.h index a516128aca..82a3d4a1a1 100644 --- a/src/core/hle/service/hid/controllers/touchscreen.h +++ b/src/core/hle/service/hid/controllers/touchscreen.h @@ -3,16 +3,22 @@ // Refer to the license.txt file included. #pragma once + #include "common/common_funcs.h" #include "common/common_types.h" #include "common/swap.h" -#include "core/frontend/input.h" #include "core/hle/service/hid/controllers/controller_base.h" +namespace Input { +template +class InputDevice; +using TouchDevice = InputDevice>; +} // namespace Input + namespace Service::HID { class Controller_Touchscreen final : public ControllerBase { public: - Controller_Touchscreen() = default; + Controller_Touchscreen(); // Called when the controller is initialized void OnInit() override; @@ -21,7 +27,7 @@ public: void OnRelease() override; // When the controller is requesting an update for the shared memory - void OnUpdate(u8* data, size_t size) override; + void OnUpdate(u8* data, std::size_t size) override; // Called when input devices should be loaded void OnLoadInputDevices() override; @@ -58,4 +64,4 @@ private: std::unique_ptr touch_device; s64_le last_touch{}; }; -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/xpad.cpp b/src/core/hle/service/hid/controllers/xpad.cpp index 521e925b7c..df0b484519 100644 --- a/src/core/hle/service/hid/controllers/xpad.cpp +++ b/src/core/hle/service/hid/controllers/xpad.cpp @@ -2,16 +2,22 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include "common/common_types.h" #include "common/swap.h" #include "core/core_timing.h" #include "core/hle/service/hid/controllers/xpad.h" namespace Service::HID { -constexpr size_t SHARED_MEMORY_OFFSET = 0x3C00; +constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3C00; + +Controller_XPad::Controller_XPad() = default; + void Controller_XPad::OnInit() {} + void Controller_XPad::OnRelease() {} -void Controller_XPad::OnUpdate(u8* data, size_t size) { + +void Controller_XPad::OnUpdate(u8* data, std::size_t size) { for (auto& xpad_entry : shared_memory.shared_memory_entries) { xpad_entry.header.timestamp = CoreTiming::GetTicks(); xpad_entry.header.total_entry_count = 17; @@ -23,7 +29,7 @@ void Controller_XPad::OnUpdate(u8* data, size_t size) { } xpad_entry.header.entry_count = 16; - auto& last_entry = xpad_entry.pad_states[xpad_entry.header.last_entry_index]; + const auto& last_entry = xpad_entry.pad_states[xpad_entry.header.last_entry_index]; xpad_entry.header.last_entry_index = (xpad_entry.header.last_entry_index + 1) % 17; auto& cur_entry = xpad_entry.pad_states[xpad_entry.header.last_entry_index]; @@ -34,5 +40,6 @@ void Controller_XPad::OnUpdate(u8* data, size_t size) { std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory)); } + void Controller_XPad::OnLoadInputDevices() {} -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/controllers/xpad.h b/src/core/hle/service/hid/controllers/xpad.h index 898fadfdc9..e2007183d0 100644 --- a/src/core/hle/service/hid/controllers/xpad.h +++ b/src/core/hle/service/hid/controllers/xpad.h @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #pragma once + #include "common/common_funcs.h" #include "common/common_types.h" #include "common/swap.h" @@ -12,7 +13,7 @@ namespace Service::HID { class Controller_XPad final : public ControllerBase { public: - Controller_XPad() = default; + Controller_XPad(); // Called when the controller is initialized void OnInit() override; @@ -21,7 +22,7 @@ public: void OnRelease() override; // When the controller is requesting an update for the shared memory - void OnUpdate(u8* data, size_t size) override; + void OnUpdate(u8* data, std::size_t size) override; // Called when input devices should be loaded void OnLoadInputDevices() override; @@ -56,4 +57,4 @@ private: static_assert(sizeof(SharedMemory) == 0x1000, "SharedMemory is an invalid size"); SharedMemory shared_memory{}; }; -}; // namespace Service::HID +} // namespace Service::HID diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index 757b3b7702..ba4aefa6c0 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -38,8 +38,8 @@ namespace Service::HID { constexpr u64 pad_update_ticks = CoreTiming::BASE_CLOCK_RATE / 100; constexpr u64 accelerometer_update_ticks = CoreTiming::BASE_CLOCK_RATE / 100; constexpr u64 gyroscope_update_ticks = CoreTiming::BASE_CLOCK_RATE / 100; -constexpr size_t SHARED_MEMORY_SIZE = 0x40000; -enum class HidController : size_t { +constexpr std::size_t SHARED_MEMORY_SIZE = 0x40000; +enum class HidController : std::size_t { DebugPad, Touchscreen, Mouse, @@ -52,7 +52,7 @@ enum class HidController : size_t { NPad, Gesture, - MaxControllers + MaxControllers, }; class IAppletResource final : public ServiceFramework { @@ -68,33 +68,21 @@ public: kernel, nullptr, SHARED_MEMORY_SIZE, Kernel::MemoryPermission::ReadWrite, Kernel::MemoryPermission::Read, 0, Kernel::MemoryRegion::BASE, "HID:SharedMemory"); - controllers[static_cast(HidController::DebugPad)] = - std::make_unique(); - controllers[static_cast(HidController::Touchscreen)] = - std::make_unique(); - controllers[static_cast(HidController::Mouse)] = - std::make_unique(); - controllers[static_cast(HidController::Keyboard)] = - std::make_unique(); - controllers[static_cast(HidController::XPad)] = std::make_unique(); - - controllers[static_cast(HidController::Unknown1)] = - std::make_unique(); - controllers[static_cast(HidController::Unknown2)] = - std::make_unique(); - controllers[static_cast(HidController::Unknown3)] = - std::make_unique(); - - controllers[static_cast(HidController::SixAxisSensor)] = - std::make_unique(); - - controllers[static_cast(HidController::NPad)] = std::make_unique(); - controllers[static_cast(HidController::Gesture)] = - std::make_unique(); + MakeController(HidController::DebugPad); + MakeController(HidController::Touchscreen); + MakeController(HidController::Mouse); + MakeController(HidController::Keyboard); + MakeController(HidController::XPad); + MakeController(HidController::Unknown1); + MakeController(HidController::Unknown2); + MakeController(HidController::Unknown3); + MakeController(HidController::SixAxisSensor); + MakeController(HidController::NPad); + MakeController(HidController::Gesture); // Homebrew doesn't try to activate some controllers, so we activate them by default - controllers[static_cast(HidController::NPad)]->ActivateController(); - controllers[static_cast(HidController::Touchscreen)]->ActivateController(); + GetController(HidController::NPad).ActivateController(); + GetController(HidController::Touchscreen).ActivateController(); GetController(HidController::Unknown1).SetCommonHeaderOffset(0x4c00); GetController(HidController::Unknown2).SetCommonHeaderOffset(0x4e00); @@ -118,6 +106,11 @@ public: controllers[static_cast(controller)]->DeactivateController(); } + template + void MakeController(HidController controller) { + controllers[static_cast(controller)] = std::make_unique(); + } + template T& GetController(HidController controller) { return static_cast(*controllers[static_cast(controller)]); @@ -136,7 +129,7 @@ private: } void UpdateControllers(u64 userdata, int cycles_late) { - bool should_reload = Settings::values.is_device_reload_pending.exchange(false); + const bool should_reload = Settings::values.is_device_reload_pending.exchange(false); for (const auto& controller : controllers) { if (should_reload) { controller->OnLoadInputDevices(); @@ -386,7 +379,6 @@ private: } void GetSupportedNpadStyleSet(Kernel::HLERequestContext& ctx) { - std::string blah = ctx.Description(); auto& controller = applet_resource->GetController(HidController::NPad); IPC::ResponseBuilder rb{ctx, 3}; @@ -434,7 +426,7 @@ private: void SetNpadJoyHoldType(Kernel::HLERequestContext& ctx) { auto& controller = applet_resource->GetController(HidController::NPad); IPC::RequestParser rp{ctx}; - auto hold_type = rp.PopRaw(); + const auto hold_type = rp.PopRaw(); controller.SetHoldType(Controller_NPad::NpadHoldType{hold_type}); IPC::ResponseBuilder rb{ctx, 2}; @@ -443,7 +435,8 @@ private: } void GetNpadJoyHoldType(Kernel::HLERequestContext& ctx) { - auto& controller = applet_resource->GetController(HidController::NPad); + const auto& controller = + applet_resource->GetController(HidController::NPad); IPC::ResponseBuilder rb{ctx, 4}; rb.Push(RESULT_SUCCESS); rb.Push(static_cast(controller.GetHoldType())); @@ -458,8 +451,8 @@ private: void SendVibrationValue(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - auto controller_id = rp.PopRaw(); - auto vibration_values = rp.PopRaw(); + const auto controller_id = rp.PopRaw(); + const auto vibration_values = rp.PopRaw(); IPC::ResponseBuilder rb{ctx, 2}; rb.Push(RESULT_SUCCESS); @@ -470,8 +463,8 @@ private: } void SendVibrationValues(Kernel::HLERequestContext& ctx) { - auto controllers = ctx.ReadBuffer(0); - auto vibrations = ctx.ReadBuffer(1); + const auto controllers = ctx.ReadBuffer(0); + const auto vibrations = ctx.ReadBuffer(1); std::vector controller_list(controllers.size() / sizeof(u32)); std::vector vibration_list(vibrations.size() / @@ -501,7 +494,7 @@ private: void SetNpadJoyAssignmentModeDual(Kernel::HLERequestContext& ctx) { IPC::RequestParser rp{ctx}; - auto npad_id = rp.PopRaw(); + const auto npad_id = rp.PopRaw(); auto& controller = applet_resource->GetController(HidController::NPad); controller.SetNpadMode(npad_id, Controller_NPad::NPadAssignments::Dual);