diff options
| -rw-r--r-- | src/core/hle/service/hid/controllers/keyboard.cpp | 56 | ||||
| -rw-r--r-- | src/core/hle/service/hid/controllers/keyboard.h | 53 | ||||
| -rw-r--r-- | src/core/hle/service/hid/controllers/mouse.cpp | 62 | ||||
| -rw-r--r-- | src/core/hle/service/hid/controllers/mouse.h | 58 |
4 files changed, 71 insertions, 158 deletions
diff --git a/src/core/hle/service/hid/controllers/keyboard.cpp b/src/core/hle/service/hid/controllers/keyboard.cpp index c6c620008..db0f56b92 100644 --- a/src/core/hle/service/hid/controllers/keyboard.cpp +++ b/src/core/hle/service/hid/controllers/keyboard.cpp | |||
| @@ -5,14 +5,19 @@ | |||
| 5 | #include <cstring> | 5 | #include <cstring> |
| 6 | #include "common/common_types.h" | 6 | #include "common/common_types.h" |
| 7 | #include "common/settings.h" | 7 | #include "common/settings.h" |
| 8 | #include "core/core.h" | ||
| 8 | #include "core/core_timing.h" | 9 | #include "core/core_timing.h" |
| 10 | #include "core/hid/hid_core.h" | ||
| 9 | #include "core/hle/service/hid/controllers/keyboard.h" | 11 | #include "core/hle/service/hid/controllers/keyboard.h" |
| 10 | 12 | ||
| 11 | namespace Service::HID { | 13 | namespace Service::HID { |
| 12 | constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3800; | 14 | constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3800; |
| 13 | constexpr u8 KEYS_PER_BYTE = 8; | 15 | constexpr u8 KEYS_PER_BYTE = 8; |
| 14 | 16 | ||
| 15 | Controller_Keyboard::Controller_Keyboard(Core::System& system_) : ControllerBase{system_} {} | 17 | Controller_Keyboard::Controller_Keyboard(Core::System& system_) : ControllerBase{system_} { |
| 18 | emulated_devices = system.HIDCore().GetEmulatedDevices(); | ||
| 19 | } | ||
| 20 | |||
| 16 | Controller_Keyboard::~Controller_Keyboard() = default; | 21 | Controller_Keyboard::~Controller_Keyboard() = default; |
| 17 | 22 | ||
| 18 | void Controller_Keyboard::OnInit() {} | 23 | void Controller_Keyboard::OnInit() {} |
| @@ -21,51 +26,26 @@ void Controller_Keyboard::OnRelease() {} | |||
| 21 | 26 | ||
| 22 | void Controller_Keyboard::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, | 27 | void Controller_Keyboard::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, |
| 23 | std::size_t size) { | 28 | std::size_t size) { |
| 24 | shared_memory.header.timestamp = core_timing.GetCPUTicks(); | ||
| 25 | shared_memory.header.total_entry_count = 17; | ||
| 26 | |||
| 27 | if (!IsControllerActivated()) { | 29 | if (!IsControllerActivated()) { |
| 28 | shared_memory.header.entry_count = 0; | 30 | keyboard_lifo.entry_count = 0; |
| 29 | shared_memory.header.last_entry_index = 0; | 31 | keyboard_lifo.last_entry_index = 0; |
| 32 | std::memcpy(data, &keyboard_lifo, sizeof(keyboard_lifo)); | ||
| 30 | return; | 33 | return; |
| 31 | } | 34 | } |
| 32 | shared_memory.header.entry_count = 16; | ||
| 33 | |||
| 34 | const auto& last_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; | ||
| 35 | shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; | ||
| 36 | auto& cur_entry = shared_memory.pad_states[shared_memory.header.last_entry_index]; | ||
| 37 | 35 | ||
| 38 | cur_entry.sampling_number = last_entry.sampling_number + 1; | 36 | const auto& last_entry = keyboard_lifo.ReadCurrentEntry().state; |
| 39 | cur_entry.sampling_number2 = cur_entry.sampling_number; | 37 | next_state.sampling_number = last_entry.sampling_number + 1; |
| 40 | 38 | ||
| 41 | cur_entry.key.fill(0); | ||
| 42 | if (Settings::values.keyboard_enabled) { | 39 | if (Settings::values.keyboard_enabled) { |
| 43 | for (std::size_t i = 0; i < keyboard_keys.size(); ++i) { | 40 | const auto& keyboard_state = emulated_devices->GetKeyboard(); |
| 44 | auto& entry = cur_entry.key[i / KEYS_PER_BYTE]; | 41 | const auto& keyboard_modifier_state = emulated_devices->GetKeyboardModifier(); |
| 45 | entry = static_cast<u8>(entry | (keyboard_keys[i]->GetStatus() << (i % KEYS_PER_BYTE))); | ||
| 46 | } | ||
| 47 | 42 | ||
| 48 | using namespace Settings::NativeKeyboard; | 43 | next_state.key = keyboard_state; |
| 49 | 44 | next_state.modifier = keyboard_modifier_state; | |
| 50 | // TODO: Assign the correct key to all modifiers | ||
| 51 | cur_entry.modifier.control.Assign(keyboard_mods[LeftControl]->GetStatus()); | ||
| 52 | cur_entry.modifier.shift.Assign(keyboard_mods[LeftShift]->GetStatus()); | ||
| 53 | cur_entry.modifier.left_alt.Assign(keyboard_mods[LeftAlt]->GetStatus()); | ||
| 54 | cur_entry.modifier.right_alt.Assign(keyboard_mods[RightAlt]->GetStatus()); | ||
| 55 | cur_entry.modifier.gui.Assign(0); | ||
| 56 | cur_entry.modifier.caps_lock.Assign(keyboard_mods[CapsLock]->GetStatus()); | ||
| 57 | cur_entry.modifier.scroll_lock.Assign(keyboard_mods[ScrollLock]->GetStatus()); | ||
| 58 | cur_entry.modifier.num_lock.Assign(keyboard_mods[NumLock]->GetStatus()); | ||
| 59 | cur_entry.modifier.katakana.Assign(0); | ||
| 60 | cur_entry.modifier.hiragana.Assign(0); | ||
| 61 | } | 45 | } |
| 62 | std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory)); | ||
| 63 | } | ||
| 64 | 46 | ||
| 65 | void Controller_Keyboard::OnLoadInputDevices() { | 47 | keyboard_lifo.WriteNextEntry(next_state); |
| 66 | std::transform(Settings::values.keyboard_keys.begin(), Settings::values.keyboard_keys.end(), | 48 | std::memcpy(data + SHARED_MEMORY_OFFSET, &keyboard_lifo, sizeof(keyboard_lifo)); |
| 67 | keyboard_keys.begin(), Input::CreateDevice<Input::ButtonDevice>); | ||
| 68 | std::transform(Settings::values.keyboard_mods.begin(), Settings::values.keyboard_mods.end(), | ||
| 69 | keyboard_mods.begin(), Input::CreateDevice<Input::ButtonDevice>); | ||
| 70 | } | 49 | } |
| 50 | |||
| 71 | } // namespace Service::HID | 51 | } // namespace Service::HID |
diff --git a/src/core/hle/service/hid/controllers/keyboard.h b/src/core/hle/service/hid/controllers/keyboard.h index 172a80e9c..6919e092a 100644 --- a/src/core/hle/service/hid/controllers/keyboard.h +++ b/src/core/hle/service/hid/controllers/keyboard.h | |||
| @@ -10,8 +10,14 @@ | |||
| 10 | #include "common/common_types.h" | 10 | #include "common/common_types.h" |
| 11 | #include "common/settings.h" | 11 | #include "common/settings.h" |
| 12 | #include "common/swap.h" | 12 | #include "common/swap.h" |
| 13 | #include "core/frontend/input.h" | ||
| 14 | #include "core/hle/service/hid/controllers/controller_base.h" | 13 | #include "core/hle/service/hid/controllers/controller_base.h" |
| 14 | #include "core/hle/service/hid/ring_lifo.h" | ||
| 15 | |||
| 16 | namespace Core::HID { | ||
| 17 | class EmulatedDevices; | ||
| 18 | struct KeyboardModifier; | ||
| 19 | struct KeyboardKey; | ||
| 20 | } // namespace Core::HID | ||
| 15 | 21 | ||
| 16 | namespace Service::HID { | 22 | namespace Service::HID { |
| 17 | class Controller_Keyboard final : public ControllerBase { | 23 | class Controller_Keyboard final : public ControllerBase { |
| @@ -28,47 +34,20 @@ public: | |||
| 28 | // When the controller is requesting an update for the shared memory | 34 | // When the controller is requesting an update for the shared memory |
| 29 | void OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) override; | 35 | void OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) override; |
| 30 | 36 | ||
| 31 | // Called when input devices should be loaded | ||
| 32 | void OnLoadInputDevices() override; | ||
| 33 | |||
| 34 | private: | 37 | private: |
| 35 | struct Modifiers { | 38 | // This is nn::hid::detail::KeyboardState |
| 36 | union { | ||
| 37 | u32_le raw{}; | ||
| 38 | BitField<0, 1, u32> control; | ||
| 39 | BitField<1, 1, u32> shift; | ||
| 40 | BitField<2, 1, u32> left_alt; | ||
| 41 | BitField<3, 1, u32> right_alt; | ||
| 42 | BitField<4, 1, u32> gui; | ||
| 43 | BitField<8, 1, u32> caps_lock; | ||
| 44 | BitField<9, 1, u32> scroll_lock; | ||
| 45 | BitField<10, 1, u32> num_lock; | ||
| 46 | BitField<11, 1, u32> katakana; | ||
| 47 | BitField<12, 1, u32> hiragana; | ||
| 48 | }; | ||
| 49 | }; | ||
| 50 | static_assert(sizeof(Modifiers) == 0x4, "Modifiers is an invalid size"); | ||
| 51 | |||
| 52 | struct KeyboardState { | 39 | struct KeyboardState { |
| 53 | s64_le sampling_number; | 40 | s64_le sampling_number; |
| 54 | s64_le sampling_number2; | 41 | Core::HID::KeyboardModifier modifier; |
| 55 | 42 | Core::HID::KeyboardKey key; | |
| 56 | Modifiers modifier; | ||
| 57 | std::array<u8, 32> key; | ||
| 58 | }; | 43 | }; |
| 59 | static_assert(sizeof(KeyboardState) == 0x38, "KeyboardState is an invalid size"); | 44 | static_assert(sizeof(KeyboardState) == 0x30, "KeyboardState is an invalid size"); |
| 60 | 45 | ||
| 61 | struct SharedMemory { | 46 | // This is nn::hid::detail::KeyboardLifo |
| 62 | CommonHeader header; | 47 | Lifo<KeyboardState> keyboard_lifo{}; |
| 63 | std::array<KeyboardState, 17> pad_states; | 48 | static_assert(sizeof(keyboard_lifo) == 0x3D8, "keyboard_lifo is an invalid size"); |
| 64 | INSERT_PADDING_BYTES(0x28); | 49 | KeyboardState next_state{}; |
| 65 | }; | ||
| 66 | static_assert(sizeof(SharedMemory) == 0x400, "SharedMemory is an invalid size"); | ||
| 67 | SharedMemory shared_memory{}; | ||
| 68 | 50 | ||
| 69 | std::array<std::unique_ptr<Input::ButtonDevice>, Settings::NativeKeyboard::NumKeyboardKeys> | 51 | Core::HID::EmulatedDevices* emulated_devices; |
| 70 | keyboard_keys; | ||
| 71 | std::array<std::unique_ptr<Input::ButtonDevice>, Settings::NativeKeyboard::NumKeyboardMods> | ||
| 72 | keyboard_mods; | ||
| 73 | }; | 52 | }; |
| 74 | } // namespace Service::HID | 53 | } // namespace Service::HID |
diff --git a/src/core/hle/service/hid/controllers/mouse.cpp b/src/core/hle/service/hid/controllers/mouse.cpp index 2211f1144..2f607bfe9 100644 --- a/src/core/hle/service/hid/controllers/mouse.cpp +++ b/src/core/hle/service/hid/controllers/mouse.cpp | |||
| @@ -4,14 +4,19 @@ | |||
| 4 | 4 | ||
| 5 | #include <cstring> | 5 | #include <cstring> |
| 6 | #include "common/common_types.h" | 6 | #include "common/common_types.h" |
| 7 | #include "core/core.h" | ||
| 7 | #include "core/core_timing.h" | 8 | #include "core/core_timing.h" |
| 8 | #include "core/frontend/emu_window.h" | 9 | #include "core/frontend/emu_window.h" |
| 10 | #include "core/hid/hid_core.h" | ||
| 9 | #include "core/hle/service/hid/controllers/mouse.h" | 11 | #include "core/hle/service/hid/controllers/mouse.h" |
| 10 | 12 | ||
| 11 | namespace Service::HID { | 13 | namespace Service::HID { |
| 12 | constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3400; | 14 | constexpr std::size_t SHARED_MEMORY_OFFSET = 0x3400; |
| 13 | 15 | ||
| 14 | Controller_Mouse::Controller_Mouse(Core::System& system_) : ControllerBase{system_} {} | 16 | Controller_Mouse::Controller_Mouse(Core::System& system_) : ControllerBase{system_} { |
| 17 | emulated_devices = system.HIDCore().GetEmulatedDevices(); | ||
| 18 | } | ||
| 19 | |||
| 15 | Controller_Mouse::~Controller_Mouse() = default; | 20 | Controller_Mouse::~Controller_Mouse() = default; |
| 16 | 21 | ||
| 17 | void Controller_Mouse::OnInit() {} | 22 | void Controller_Mouse::OnInit() {} |
| @@ -19,50 +24,35 @@ void Controller_Mouse::OnRelease() {} | |||
| 19 | 24 | ||
| 20 | void Controller_Mouse::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, | 25 | void Controller_Mouse::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, |
| 21 | std::size_t size) { | 26 | std::size_t size) { |
| 22 | shared_memory.header.timestamp = core_timing.GetCPUTicks(); | 27 | mouse_lifo.timestamp = core_timing.GetCPUTicks(); |
| 23 | shared_memory.header.total_entry_count = 17; | ||
| 24 | 28 | ||
| 25 | if (!IsControllerActivated()) { | 29 | if (!IsControllerActivated()) { |
| 26 | shared_memory.header.entry_count = 0; | 30 | mouse_lifo.entry_count = 0; |
| 27 | shared_memory.header.last_entry_index = 0; | 31 | mouse_lifo.last_entry_index = 0; |
| 32 | std::memcpy(data, &mouse_lifo, sizeof(mouse_lifo)); | ||
| 28 | return; | 33 | return; |
| 29 | } | 34 | } |
| 30 | shared_memory.header.entry_count = 16; | ||
| 31 | |||
| 32 | auto& last_entry = shared_memory.mouse_states[shared_memory.header.last_entry_index]; | ||
| 33 | shared_memory.header.last_entry_index = (shared_memory.header.last_entry_index + 1) % 17; | ||
| 34 | auto& cur_entry = shared_memory.mouse_states[shared_memory.header.last_entry_index]; | ||
| 35 | 35 | ||
| 36 | cur_entry.sampling_number = last_entry.sampling_number + 1; | 36 | const auto& last_entry = mouse_lifo.ReadCurrentEntry().state; |
| 37 | cur_entry.sampling_number2 = cur_entry.sampling_number; | 37 | next_state.sampling_number = last_entry.sampling_number + 1; |
| 38 | 38 | ||
| 39 | cur_entry.attribute.raw = 0; | 39 | next_state.attribute.raw = 0; |
| 40 | if (Settings::values.mouse_enabled) { | 40 | if (Settings::values.mouse_enabled) { |
| 41 | const auto [px, py, sx, sy] = mouse_device->GetStatus(); | 41 | const auto& mouse_button_state = emulated_devices->GetMouseButtons(); |
| 42 | const auto x = static_cast<s32>(px * Layout::ScreenUndocked::Width); | 42 | const auto& mouse_position_state = emulated_devices->GetMousePosition(); |
| 43 | const auto y = static_cast<s32>(py * Layout::ScreenUndocked::Height); | 43 | next_state.attribute.is_connected.Assign(1); |
| 44 | cur_entry.x = x; | 44 | next_state.x = mouse_position_state.x; |
| 45 | cur_entry.y = y; | 45 | next_state.y = mouse_position_state.y; |
| 46 | cur_entry.delta_x = x - last_entry.x; | 46 | next_state.delta_x = next_state.x - last_entry.x; |
| 47 | cur_entry.delta_y = y - last_entry.y; | 47 | next_state.delta_y = next_state.y - last_entry.y; |
| 48 | cur_entry.mouse_wheel_x = sx; | 48 | next_state.delta_wheel_x = mouse_position_state.delta_wheel_x; |
| 49 | cur_entry.mouse_wheel_y = sy; | 49 | next_state.delta_wheel_y = mouse_position_state.delta_wheel_y; |
| 50 | cur_entry.attribute.is_connected.Assign(1); | 50 | |
| 51 | 51 | next_state.button = mouse_button_state; | |
| 52 | using namespace Settings::NativeMouseButton; | ||
| 53 | cur_entry.button.left.Assign(mouse_button_devices[Left]->GetStatus()); | ||
| 54 | cur_entry.button.right.Assign(mouse_button_devices[Right]->GetStatus()); | ||
| 55 | cur_entry.button.middle.Assign(mouse_button_devices[Middle]->GetStatus()); | ||
| 56 | cur_entry.button.forward.Assign(mouse_button_devices[Forward]->GetStatus()); | ||
| 57 | cur_entry.button.back.Assign(mouse_button_devices[Back]->GetStatus()); | ||
| 58 | } | 52 | } |
| 59 | 53 | ||
| 60 | std::memcpy(data + SHARED_MEMORY_OFFSET, &shared_memory, sizeof(SharedMemory)); | 54 | mouse_lifo.WriteNextEntry(next_state); |
| 55 | std::memcpy(data + SHARED_MEMORY_OFFSET, &mouse_lifo, sizeof(mouse_lifo)); | ||
| 61 | } | 56 | } |
| 62 | 57 | ||
| 63 | void Controller_Mouse::OnLoadInputDevices() { | ||
| 64 | //mouse_device = Input::CreateDevice<Input::MouseDevice>(Settings::values.mouse_device); | ||
| 65 | std::transform(Settings::values.mouse_buttons.begin(), Settings::values.mouse_buttons.end(), | ||
| 66 | mouse_button_devices.begin(), Input::CreateDevice<Input::ButtonDevice>); | ||
| 67 | } | ||
| 68 | } // namespace Service::HID | 58 | } // namespace Service::HID |
diff --git a/src/core/hle/service/hid/controllers/mouse.h b/src/core/hle/service/hid/controllers/mouse.h index 3d391a798..ce868a247 100644 --- a/src/core/hle/service/hid/controllers/mouse.h +++ b/src/core/hle/service/hid/controllers/mouse.h | |||
| @@ -9,8 +9,13 @@ | |||
| 9 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 10 | #include "common/settings.h" | 10 | #include "common/settings.h" |
| 11 | #include "common/swap.h" | 11 | #include "common/swap.h" |
| 12 | #include "core/frontend/input.h" | ||
| 13 | #include "core/hle/service/hid/controllers/controller_base.h" | 12 | #include "core/hle/service/hid/controllers/controller_base.h" |
| 13 | #include "core/hle/service/hid/ring_lifo.h" | ||
| 14 | |||
| 15 | namespace Core::HID { | ||
| 16 | class EmulatedDevices; | ||
| 17 | struct MouseState; | ||
| 18 | } // namespace Core::HID | ||
| 14 | 19 | ||
| 15 | namespace Service::HID { | 20 | namespace Service::HID { |
| 16 | class Controller_Mouse final : public ControllerBase { | 21 | class Controller_Mouse final : public ControllerBase { |
| @@ -27,53 +32,12 @@ public: | |||
| 27 | // When the controller is requesting an update for the shared memory | 32 | // When the controller is requesting an update for the shared memory |
| 28 | void OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) override; | 33 | void OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data, std::size_t size) override; |
| 29 | 34 | ||
| 30 | // Called when input devices should be loaded | ||
| 31 | void OnLoadInputDevices() override; | ||
| 32 | |||
| 33 | private: | 35 | private: |
| 34 | struct Buttons { | 36 | // This is nn::hid::detail::MouseLifo |
| 35 | union { | 37 | Lifo<Core::HID::MouseState> mouse_lifo{}; |
| 36 | u32_le raw{}; | 38 | static_assert(sizeof(mouse_lifo) == 0x350, "mouse_lifo is an invalid size"); |
| 37 | BitField<0, 1, u32> left; | 39 | Core::HID::MouseState next_state{}; |
| 38 | BitField<1, 1, u32> right; | ||
| 39 | BitField<2, 1, u32> middle; | ||
| 40 | BitField<3, 1, u32> forward; | ||
| 41 | BitField<4, 1, u32> back; | ||
| 42 | }; | ||
| 43 | }; | ||
| 44 | static_assert(sizeof(Buttons) == 0x4, "Buttons is an invalid size"); | ||
| 45 | |||
| 46 | struct Attributes { | ||
| 47 | union { | ||
| 48 | u32_le raw{}; | ||
| 49 | BitField<0, 1, u32> transferable; | ||
| 50 | BitField<1, 1, u32> is_connected; | ||
| 51 | }; | ||
| 52 | }; | ||
| 53 | static_assert(sizeof(Attributes) == 0x4, "Attributes is an invalid size"); | ||
| 54 | |||
| 55 | struct MouseState { | ||
| 56 | s64_le sampling_number; | ||
| 57 | s64_le sampling_number2; | ||
| 58 | s32_le x; | ||
| 59 | s32_le y; | ||
| 60 | s32_le delta_x; | ||
| 61 | s32_le delta_y; | ||
| 62 | s32_le mouse_wheel_x; | ||
| 63 | s32_le mouse_wheel_y; | ||
| 64 | Buttons button; | ||
| 65 | Attributes attribute; | ||
| 66 | }; | ||
| 67 | static_assert(sizeof(MouseState) == 0x30, "MouseState is an invalid size"); | ||
| 68 | |||
| 69 | struct SharedMemory { | ||
| 70 | CommonHeader header; | ||
| 71 | std::array<MouseState, 17> mouse_states; | ||
| 72 | }; | ||
| 73 | SharedMemory shared_memory{}; | ||
| 74 | 40 | ||
| 75 | std::unique_ptr<Input::MouseDevice> mouse_device; | 41 | Core::HID::EmulatedDevices* emulated_devices; |
| 76 | std::array<std::unique_ptr<Input::ButtonDevice>, Settings::NativeMouseButton::NumMouseButtons> | ||
| 77 | mouse_button_devices; | ||
| 78 | }; | 42 | }; |
| 79 | } // namespace Service::HID | 43 | } // namespace Service::HID |