From 266703b50ea61433fa6bba200433040ffd5f7377 Mon Sep 17 00:00:00 2001 From: Max Thomas Date: Mon, 7 May 2018 09:06:02 -0600 Subject: [PATCH] hid: Tweaks, Analog Sticks (#435) * hid: Update mouse/keyboard state * hid: Working analog sticks * hid: Nits * hid: Nits * hid: Update mystery sections * hid: Tweaks --- src/core/hle/service/hid/hid.cpp | 172 ++++++++++++++++++++++--------- src/core/hle/service/hid/hid.h | 98 +++++++++++++++--- src/core/settings.h | 4 + 3 files changed, 215 insertions(+), 59 deletions(-) diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index dc7ff5c317..1891255cb5 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -60,7 +60,10 @@ private: std::transform(Settings::values.buttons.begin() + Settings::NativeButton::BUTTON_HID_BEGIN, Settings::values.buttons.begin() + Settings::NativeButton::BUTTON_HID_END, buttons.begin(), Input::CreateDevice); - // TODO(shinyquagsire23): sticks, gyro, touch, mouse, keyboard + std::transform(Settings::values.analogs.begin() + Settings::NativeAnalog::STICK_HID_BEGIN, + Settings::values.analogs.begin() + Settings::NativeAnalog::STICK_HID_END, + sticks.begin(), Input::CreateDevice); + // TODO(shinyquagsire23): gyro, touch, mouse, keyboard } void UpdatePadCallback(u64 userdata, int cycles_late) { @@ -79,61 +82,70 @@ private: controller_header.left_color_body = JOYCON_BODY_NEON_BLUE; controller_header.left_color_buttons = JOYCON_BUTTONS_NEON_BLUE; - for (int index = 0; index < HID_NUM_LAYOUTS; index++) { - ControllerLayout& layout = mem.controllers[Controller_Handheld].layouts[index]; - layout.header.num_entries = HID_NUM_ENTRIES; - layout.header.max_entry_index = HID_NUM_ENTRIES - 1; + for (size_t controller = 0; controller < mem.controllers.size(); controller++) { + for (int index = 0; index < HID_NUM_LAYOUTS; index++) { + ControllerLayout& layout = mem.controllers[controller].layouts[index]; + layout.header.num_entries = HID_NUM_ENTRIES; + layout.header.max_entry_index = HID_NUM_ENTRIES - 1; - // HID shared memory stores the state of the past 17 samples in a circlular buffer, - // each with a timestamp in number of samples since boot. - layout.header.timestamp_ticks = CoreTiming::GetTicks(); - layout.header.latest_entry = (layout.header.latest_entry + 1) % HID_NUM_ENTRIES; + // HID shared memory stores the state of the past 17 samples in a circlular buffer, + // each with a timestamp in number of samples since boot. + layout.header.timestamp_ticks = CoreTiming::GetTicks(); + layout.header.latest_entry = (layout.header.latest_entry + 1) % HID_NUM_ENTRIES; - ControllerInputEntry& entry = layout.entries[layout.header.latest_entry]; - entry.connection_state = ConnectionState_Connected | ConnectionState_Wired; - entry.timestamp++; - entry.timestamp_2++; // TODO(shinyquagsire23): Is this always identical to timestamp? + ControllerInputEntry& entry = layout.entries[layout.header.latest_entry]; + entry.connection_state = ConnectionState_Connected | ConnectionState_Wired; + entry.timestamp++; + // TODO(shinyquagsire23): Is this always identical to timestamp? + entry.timestamp_2++; - // TODO(shinyquagsire23): Set up some LUTs for each layout mapping in the future? - // For now everything is just the default handheld layout, but split Joy-Con will - // rotate the face buttons and directions for certain layouts. - ControllerPadState& state = entry.buttons; - using namespace Settings::NativeButton; - state.a.Assign(buttons[A - BUTTON_HID_BEGIN]->GetStatus()); - state.b.Assign(buttons[B - BUTTON_HID_BEGIN]->GetStatus()); - state.x.Assign(buttons[X - BUTTON_HID_BEGIN]->GetStatus()); - state.y.Assign(buttons[Y - BUTTON_HID_BEGIN]->GetStatus()); - state.lstick.Assign(buttons[LStick - BUTTON_HID_BEGIN]->GetStatus()); - state.rstick.Assign(buttons[RStick - BUTTON_HID_BEGIN]->GetStatus()); - state.l.Assign(buttons[L - BUTTON_HID_BEGIN]->GetStatus()); - state.r.Assign(buttons[R - BUTTON_HID_BEGIN]->GetStatus()); - state.zl.Assign(buttons[ZL - BUTTON_HID_BEGIN]->GetStatus()); - state.zr.Assign(buttons[ZR - BUTTON_HID_BEGIN]->GetStatus()); - state.plus.Assign(buttons[Plus - BUTTON_HID_BEGIN]->GetStatus()); - state.minus.Assign(buttons[Minus - BUTTON_HID_BEGIN]->GetStatus()); + // TODO(shinyquagsire23): More than just handheld input + if (controller != Controller_Handheld) + continue; - state.dleft.Assign(buttons[DLeft - BUTTON_HID_BEGIN]->GetStatus()); - state.dup.Assign(buttons[DUp - BUTTON_HID_BEGIN]->GetStatus()); - state.dright.Assign(buttons[DRight - BUTTON_HID_BEGIN]->GetStatus()); - state.ddown.Assign(buttons[DDown - BUTTON_HID_BEGIN]->GetStatus()); + // TODO(shinyquagsire23): Set up some LUTs for each layout mapping in the future? + // For now everything is just the default handheld layout, but split Joy-Con will + // rotate the face buttons and directions for certain layouts. + ControllerPadState& state = entry.buttons; + using namespace Settings::NativeButton; + state.a.Assign(buttons[A - BUTTON_HID_BEGIN]->GetStatus()); + state.b.Assign(buttons[B - BUTTON_HID_BEGIN]->GetStatus()); + state.x.Assign(buttons[X - BUTTON_HID_BEGIN]->GetStatus()); + state.y.Assign(buttons[Y - BUTTON_HID_BEGIN]->GetStatus()); + state.lstick.Assign(buttons[LStick - BUTTON_HID_BEGIN]->GetStatus()); + state.rstick.Assign(buttons[RStick - BUTTON_HID_BEGIN]->GetStatus()); + state.l.Assign(buttons[L - BUTTON_HID_BEGIN]->GetStatus()); + state.r.Assign(buttons[R - BUTTON_HID_BEGIN]->GetStatus()); + state.zl.Assign(buttons[ZL - BUTTON_HID_BEGIN]->GetStatus()); + state.zr.Assign(buttons[ZR - BUTTON_HID_BEGIN]->GetStatus()); + state.plus.Assign(buttons[Plus - BUTTON_HID_BEGIN]->GetStatus()); + state.minus.Assign(buttons[Minus - BUTTON_HID_BEGIN]->GetStatus()); - state.lstick_left.Assign(buttons[LStick_Left - BUTTON_HID_BEGIN]->GetStatus()); - state.lstick_up.Assign(buttons[LStick_Up - BUTTON_HID_BEGIN]->GetStatus()); - state.lstick_right.Assign(buttons[LStick_Right - BUTTON_HID_BEGIN]->GetStatus()); - state.lstick_down.Assign(buttons[LStick_Down - BUTTON_HID_BEGIN]->GetStatus()); + state.dleft.Assign(buttons[DLeft - BUTTON_HID_BEGIN]->GetStatus()); + state.dup.Assign(buttons[DUp - BUTTON_HID_BEGIN]->GetStatus()); + state.dright.Assign(buttons[DRight - BUTTON_HID_BEGIN]->GetStatus()); + state.ddown.Assign(buttons[DDown - BUTTON_HID_BEGIN]->GetStatus()); - state.rstick_left.Assign(buttons[RStick_Left - BUTTON_HID_BEGIN]->GetStatus()); - state.rstick_up.Assign(buttons[RStick_Up - BUTTON_HID_BEGIN]->GetStatus()); - state.rstick_right.Assign(buttons[RStick_Right - BUTTON_HID_BEGIN]->GetStatus()); - state.rstick_down.Assign(buttons[RStick_Down - BUTTON_HID_BEGIN]->GetStatus()); + state.lstick_left.Assign(buttons[LStick_Left - BUTTON_HID_BEGIN]->GetStatus()); + state.lstick_up.Assign(buttons[LStick_Up - BUTTON_HID_BEGIN]->GetStatus()); + state.lstick_right.Assign(buttons[LStick_Right - BUTTON_HID_BEGIN]->GetStatus()); + state.lstick_down.Assign(buttons[LStick_Down - BUTTON_HID_BEGIN]->GetStatus()); - state.sl.Assign(buttons[SL - BUTTON_HID_BEGIN]->GetStatus()); - state.sr.Assign(buttons[SR - BUTTON_HID_BEGIN]->GetStatus()); + state.rstick_left.Assign(buttons[RStick_Left - BUTTON_HID_BEGIN]->GetStatus()); + state.rstick_up.Assign(buttons[RStick_Up - BUTTON_HID_BEGIN]->GetStatus()); + state.rstick_right.Assign(buttons[RStick_Right - BUTTON_HID_BEGIN]->GetStatus()); + state.rstick_down.Assign(buttons[RStick_Down - BUTTON_HID_BEGIN]->GetStatus()); - // TODO(shinyquagsire23): Analog stick vals + state.sl.Assign(buttons[SL - BUTTON_HID_BEGIN]->GetStatus()); + state.sr.Assign(buttons[SR - BUTTON_HID_BEGIN]->GetStatus()); - // TODO(shinyquagsire23): Update pad info proper, (circular buffers, timestamps, - // layouts) + const auto [stick_l_x_f, stick_l_y_f] = sticks[Joystick_Left]->GetStatus(); + const auto [stick_r_x_f, stick_r_y_f] = sticks[Joystick_Right]->GetStatus(); + entry.joystick_left_x = static_cast(stick_l_x_f * HID_JOYSTICK_MAX); + entry.joystick_left_y = static_cast(stick_l_y_f * HID_JOYSTICK_MAX); + entry.joystick_right_x = static_cast(stick_r_x_f * HID_JOYSTICK_MAX); + entry.joystick_right_y = static_cast(stick_r_y_f * HID_JOYSTICK_MAX); + } } // TODO(bunnei): Properly implement the touch screen, the below will just write empty data @@ -151,6 +163,71 @@ private: touchscreen.entries[curr_entry].header.timestamp = sample_counter; touchscreen.entries[curr_entry].header.num_touches = 0; + // TODO(shinyquagsire23): Properly implement mouse + Mouse& mouse = mem.mouse; + const u64 last_mouse_entry = mouse.header.latest_entry; + const u64 curr_mouse_entry = (mouse.header.latest_entry + 1) % mouse.entries.size(); + const u64 mouse_sample_counter = mouse.entries[last_mouse_entry].timestamp + 1; + mouse.header.timestamp_ticks = timestamp; + mouse.header.num_entries = mouse.entries.size(); + mouse.header.max_entry_index = mouse.entries.size(); + mouse.header.latest_entry = curr_mouse_entry; + + mouse.entries[curr_mouse_entry].timestamp = mouse_sample_counter; + mouse.entries[curr_mouse_entry].timestamp_2 = mouse_sample_counter; + + // TODO(shinyquagsire23): Properly implement keyboard + Keyboard& keyboard = mem.keyboard; + const u64 last_keyboard_entry = keyboard.header.latest_entry; + const u64 curr_keyboard_entry = + (keyboard.header.latest_entry + 1) % keyboard.entries.size(); + const u64 keyboard_sample_counter = keyboard.entries[last_keyboard_entry].timestamp + 1; + keyboard.header.timestamp_ticks = timestamp; + keyboard.header.num_entries = keyboard.entries.size(); + keyboard.header.latest_entry = last_keyboard_entry; + keyboard.header.max_entry_index = keyboard.entries.size(); + + keyboard.entries[curr_keyboard_entry].timestamp = keyboard_sample_counter; + keyboard.entries[curr_keyboard_entry].timestamp_2 = keyboard_sample_counter; + + // TODO(shinyquagsire23): Figure out what any of these are + for (size_t i = 0; i < mem.unk_input_1.size(); i++) { + UnkInput1& input = mem.unk_input_1[i]; + const u64 last_input_entry = input.header.latest_entry; + const u64 curr_input_entry = (input.header.latest_entry + 1) % input.entries.size(); + const u64 input_sample_counter = input.entries[last_input_entry].timestamp + 1; + + input.header.timestamp_ticks = timestamp; + input.header.num_entries = input.entries.size(); + input.header.latest_entry = last_input_entry; + input.header.max_entry_index = input.entries.size(); + + input.entries[curr_input_entry].timestamp = input_sample_counter; + input.entries[curr_input_entry].timestamp_2 = input_sample_counter; + } + + for (size_t i = 0; i < mem.unk_input_2.size(); i++) { + UnkInput2& input = mem.unk_input_2[i]; + + input.header.timestamp_ticks = timestamp; + input.header.num_entries = 17; + input.header.latest_entry = 0; + input.header.max_entry_index = 0; + } + + UnkInput3& input = mem.unk_input_3; + const u64 last_input_entry = input.header.latest_entry; + const u64 curr_input_entry = (input.header.latest_entry + 1) % input.entries.size(); + const u64 input_sample_counter = input.entries[last_input_entry].timestamp + 1; + + input.header.timestamp_ticks = timestamp; + input.header.num_entries = input.entries.size(); + input.header.latest_entry = last_input_entry; + input.header.max_entry_index = input.entries.size(); + + input.entries[curr_input_entry].timestamp = input_sample_counter; + input.entries[curr_input_entry].timestamp_2 = input_sample_counter; + // TODO(shinyquagsire23): Signal events std::memcpy(shared_mem->GetPointer(), &mem, sizeof(SharedMemory)); @@ -169,6 +246,7 @@ private: std::atomic is_device_reload_pending{true}; std::array, Settings::NativeButton::NUM_BUTTONS_HID> buttons; + std::array, Settings::NativeAnalog::NUM_STICKS_HID> sticks; }; class IActiveVibrationDeviceList final : public ServiceFramework { diff --git a/src/core/hle/service/hid/hid.h b/src/core/hle/service/hid/hid.h index 350174ccdb..b499308d63 100644 --- a/src/core/hle/service/hid/hid.h +++ b/src/core/hle/service/hid/hid.h @@ -48,6 +48,11 @@ enum ControllerConnectionState { ConnectionState_Wired = 1 << 1, }; +enum ControllerJoystick { + Joystick_Left = 0, + Joystick_Right = 1, +}; + enum ControllerID { Controller_Player1 = 0, Controller_Player2 = 1, @@ -63,6 +68,34 @@ enum ControllerID { // End enums and output structs +// Begin UnkInput3 + +struct UnkInput3Header { + u64 timestamp_ticks; + u64 num_entries; + u64 latest_entry; + u64 max_entry_index; +}; +static_assert(sizeof(UnkInput3Header) == 0x20, "HID UnkInput3 header structure has incorrect size"); + +struct UnkInput3Entry { + u64 timestamp; + u64 timestamp_2; + u64 unk_8; + u64 unk_10; + u64 unk_18; +}; +static_assert(sizeof(UnkInput3Entry) == 0x28, "HID UnkInput3 entry structure has incorrect size"); + +struct UnkInput3 { + UnkInput3Header header; + std::array entries; + std::array padding; +}; +static_assert(sizeof(UnkInput3) == 0x400, "HID UnkInput3 structure has incorrect size"); + +// End UnkInput3 + // Begin TouchScreen struct TouchScreenHeader { @@ -204,6 +237,52 @@ static_assert(sizeof(Keyboard) == 0x400, "HID keyboard structure has incorrect s // End Keyboard +// Begin UnkInput1 + +struct UnkInput1Header { + u64 timestamp_ticks; + u64 num_entries; + u64 latest_entry; + u64 max_entry_index; +}; +static_assert(sizeof(UnkInput1Header) == 0x20, "HID UnkInput1 header structure has incorrect size"); + +struct UnkInput1Entry { + u64 timestamp; + u64 timestamp_2; + u64 unk_8; + u64 unk_10; + u64 unk_18; +}; +static_assert(sizeof(UnkInput1Entry) == 0x28, "HID UnkInput1 entry structure has incorrect size"); + +struct UnkInput1 { + UnkInput1Header header; + std::array entries; + std::array padding; +}; +static_assert(sizeof(UnkInput1) == 0x400, "HID UnkInput1 structure has incorrect size"); + +// End UnkInput1 + +// Begin UnkInput2 + +struct UnkInput2Header { + u64 timestamp_ticks; + u64 num_entries; + u64 latest_entry; + u64 max_entry_index; +}; +static_assert(sizeof(UnkInput2Header) == 0x20, "HID UnkInput2 header structure has incorrect size"); + +struct UnkInput2 { + UnkInput2Header header; + std::array padding; +}; +static_assert(sizeof(UnkInput2) == 0x200, "HID UnkInput2 structure has incorrect size"); + +// End UnkInput2 + // Begin Controller struct ControllerMAC { @@ -283,10 +362,10 @@ struct ControllerInputEntry { u64 timestamp; u64 timestamp_2; ControllerPadState buttons; - u32 joystick_left_x; - u32 joystick_left_y; - u32 joystick_right_x; - u32 joystick_right_y; + s32 joystick_left_x; + s32 joystick_left_y; + s32 joystick_right_x; + s32 joystick_right_y; u64 connection_state; }; static_assert(sizeof(ControllerInputEntry) == 0x30, @@ -312,17 +391,12 @@ static_assert(sizeof(Controller) == 0x5000, "HID controller structure has incorr // End Controller struct SharedMemory { - std::array header; + UnkInput3 unk_input_3; TouchScreen touchscreen; Mouse mouse; Keyboard keyboard; - std::array unk_section_1; - std::array unk_section_2; - std::array unk_section_3; - std::array unk_section_4; - std::array unk_section_5; - std::array unk_section_6; - std::array unk_section_7; + std::array unk_input_1; + std::array unk_input_2; std::array unk_section_8; std::array controller_serials; std::array controllers; diff --git a/src/core/settings.h b/src/core/settings.h index 2c94caab7c..cfec63c210 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -99,6 +99,10 @@ enum Values { NumAnalogs, }; +constexpr int STICK_HID_BEGIN = LStick; +constexpr int STICK_HID_END = NumAnalogs; +constexpr int NUM_STICKS_HID = NumAnalogs; + static const std::array mapping = {{ "lstick", "rstick",