From 8070de693d4e7e026edb291c5cbb53ec47487129 Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 2 Dec 2020 00:42:13 +0100 Subject: [PATCH 1/4] strat/hid: Update for new libnx HID interface --- .../libstratosphere/source/hid/hid_api.cpp | 36 ++++++++++++++++--- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/libraries/libstratosphere/source/hid/hid_api.cpp b/libraries/libstratosphere/source/hid/hid_api.cpp index da4bc2591..3ef35f95b 100644 --- a/libraries/libstratosphere/source/hid/hid_api.cpp +++ b/libraries/libstratosphere/source/hid/hid_api.cpp @@ -23,12 +23,30 @@ namespace ams::hid { os::Mutex g_hid_lock(false); bool g_initialized_hid = false; + /* Set of supported NpadIds (we want to read from any connected controllers). */ + constexpr const HidNpadIdType NpadIdTypes[] = { + HidNpadIdType_No1, + HidNpadIdType_No2, + HidNpadIdType_No3, + HidNpadIdType_No4, + HidNpadIdType_No5, + HidNpadIdType_No6, + HidNpadIdType_No7, + HidNpadIdType_No8, + HidNpadIdType_Handheld, + }; + + constexpr const size_t NumNpadIdTypes = util::size(NpadIdTypes); + /* Helper. */ void InitializeHid() { R_ABORT_UNLESS(smInitialize()); ON_SCOPE_EXIT { smExit(); }; { R_ABORT_UNLESS(hidInitialize()); + hidInitializeNpad(); + R_ABORT_UNLESS(hidSetSupportedNpadIdType(NpadIdTypes, NumNpadIdTypes)); + R_ABORT_UNLESS(hidSetSupportedNpadStyleSet(HidNpadStyleSet_NpadStandard | HidNpadStyleTag_NpadSystemExt)); } } @@ -46,6 +64,17 @@ namespace ams::hid { return ResultSuccess(); } + u64 ReadHidNpad(HidNpadIdType id) { + HidNpadSystemExtState state; + + size_t count = hidGetNpadStatesSystemExt(id, std::addressof(state), 1); + if (count != 0 && (state.attributes & HidNpadAttribute_IsConnected)) { + return state.buttons; + } + + return 0; + } + } Result GetKeysHeld(u64 *out) { @@ -53,11 +82,10 @@ namespace ams::hid { R_TRY(EnsureHidInitialized()); - hidScanInput(); - *out = 0; + *out = ReadHidNpad(HidNpadIdType_Handheld); - for (size_t controller = 0; controller < 10; controller++) { - *out |= hidKeysHeld(static_cast(controller)); + for (size_t controller = 0; controller < 8; controller++) { + *out |= ReadHidNpad(static_cast(controller)); } return ResultSuccess(); From 09978eafb9bb0311a08ca9ecacff7c6af9231bdd Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 2 Dec 2020 00:50:14 +0100 Subject: [PATCH 2/4] strat/cfg: Update for new libnx HID interface --- .../source/cfg/cfg_override.cpp | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/libraries/libstratosphere/source/cfg/cfg_override.cpp b/libraries/libstratosphere/source/cfg/cfg_override.cpp index 7391059a8..fb819e29a 100644 --- a/libraries/libstratosphere/source/cfg/cfg_override.cpp +++ b/libraries/libstratosphere/source/cfg/cfg_override.cpp @@ -34,7 +34,7 @@ namespace ams::cfg { constexpr ProgramOverrideKey DefaultAppletPhotoViewerOverrideKey = { .override_key = { - .key_combination = KEY_R, + .key_combination = HidNpadButton_R, .override_by_default = true, }, .program_id = ncm::SystemAppletId::PhotoViewer, @@ -58,12 +58,12 @@ namespace ams::cfg { /* Override globals. */ OverrideKey g_default_override_key = { - .key_combination = KEY_L, + .key_combination = HidNpadButton_L, .override_by_default = true, }; OverrideKey g_default_cheat_enable_key = { - .key_combination = KEY_L, + .key_combination = HidNpadButton_L, .override_by_default = true, }; @@ -89,7 +89,7 @@ namespace ams::cfg { impl::OverrideStatusFlag_AddressSpace64Bit, }, .override_any_app_key = { - .key_combination = KEY_R, + .key_combination = HidNpadButton_R, .override_by_default = false, }, .override_any_app_as_flag = impl::OverrideStatusFlag_AddressSpace64Bit, @@ -110,41 +110,41 @@ namespace ams::cfg { /* Parse key combination. */ if (strcasecmp(value, "A") == 0) { - cfg.key_combination = KEY_A; + cfg.key_combination = HidNpadButton_A; } else if (strcasecmp(value, "B") == 0) { - cfg.key_combination = KEY_B; + cfg.key_combination = HidNpadButton_B; } else if (strcasecmp(value, "X") == 0) { - cfg.key_combination = KEY_X; + cfg.key_combination = HidNpadButton_X; } else if (strcasecmp(value, "Y") == 0) { - cfg.key_combination = KEY_Y; + cfg.key_combination = HidNpadButton_Y; } else if (strcasecmp(value, "LS") == 0) { - cfg.key_combination = KEY_LSTICK; + cfg.key_combination = HidNpadButton_StickL; } else if (strcasecmp(value, "RS") == 0) { - cfg.key_combination = KEY_RSTICK; + cfg.key_combination = HidNpadButton_StickR; } else if (strcasecmp(value, "L") == 0) { - cfg.key_combination = KEY_L; + cfg.key_combination = HidNpadButton_L; } else if (strcasecmp(value, "R") == 0) { - cfg.key_combination = KEY_R; + cfg.key_combination = HidNpadButton_R; } else if (strcasecmp(value, "ZL") == 0) { - cfg.key_combination = KEY_ZL; + cfg.key_combination = HidNpadButton_ZL; } else if (strcasecmp(value, "ZR") == 0) { - cfg.key_combination = KEY_ZR; + cfg.key_combination = HidNpadButton_ZR; } else if (strcasecmp(value, "PLUS") == 0) { - cfg.key_combination = KEY_PLUS; + cfg.key_combination = HidNpadButton_Plus; } else if (strcasecmp(value, "MINUS") == 0) { - cfg.key_combination = KEY_MINUS; + cfg.key_combination = HidNpadButton_Minus; } else if (strcasecmp(value, "DLEFT") == 0) { - cfg.key_combination = KEY_DLEFT; + cfg.key_combination = HidNpadButton_Left; } else if (strcasecmp(value, "DUP") == 0) { - cfg.key_combination = KEY_DUP; + cfg.key_combination = HidNpadButton_Up; } else if (strcasecmp(value, "DRIGHT") == 0) { - cfg.key_combination = KEY_DRIGHT; + cfg.key_combination = HidNpadButton_Right; } else if (strcasecmp(value, "DDOWN") == 0) { - cfg.key_combination = KEY_DDOWN; + cfg.key_combination = HidNpadButton_Down; } else if (strcasecmp(value, "SL") == 0) { - cfg.key_combination = KEY_SL; + cfg.key_combination = HidNpadButton_AnySL; } else if (strcasecmp(value, "SR") == 0) { - cfg.key_combination = KEY_SR; + cfg.key_combination = HidNpadButton_AnySR; } return cfg; From b69bf07d9c5bac666cdf5c37e2bf1f0fdaac15cb Mon Sep 17 00:00:00 2001 From: fincs Date: Wed, 2 Dec 2020 00:51:02 +0100 Subject: [PATCH 3/4] reboot_to_payload: Update for new libnx HID interface --- troposphere/reboot_to_payload/source/main.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/troposphere/reboot_to_payload/source/main.c b/troposphere/reboot_to_payload/source/main.c index 80fe79e3c..a6e6af5d8 100644 --- a/troposphere/reboot_to_payload/source/main.c +++ b/troposphere/reboot_to_payload/source/main.c @@ -54,6 +54,11 @@ int main(int argc, char **argv) { consoleInit(NULL); + padConfigureInput(8, HidNpadStyleSet_NpadStandard); + + PadState pad; + padInitializeAny(&pad); + bool can_reboot = true; Result rc = splInitialize(); if (R_FAILED(rc)) { @@ -76,20 +81,13 @@ int main(int argc, char **argv) // Main loop while(appletMainLoop()) { - //Scan all the inputs. This should be done once for each frame - hidScanInput(); + padUpdate(&pad); + u64 kDown = padGetButtonsDown(&pad); - u64 kDown = 0; - - for (int controller = 0; controller < 10; controller++) { - // hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame) - kDown |= hidKeysDown((HidControllerID) controller); - } - - if (can_reboot && kDown & KEY_MINUS) { + if (can_reboot && (kDown & HidNpadButton_Minus)) { reboot_to_payload(); } - if (kDown & KEY_L) { break; } // break in order to return to hbmenu + if (kDown & HidNpadButton_L) { break; } // break in order to return to hbmenu consoleUpdate(NULL); } From 3b18db914cd9213a6a3617d59bc3b2a41445d084 Mon Sep 17 00:00:00 2001 From: Adubbz Date: Wed, 2 Dec 2020 21:21:05 +1100 Subject: [PATCH 4/4] daybreak: update for hid refactor (#1222) --- troposphere/daybreak/source/ui.cpp | 102 +++++++++++++++-------------- 1 file changed, 54 insertions(+), 48 deletions(-) diff --git a/troposphere/daybreak/source/ui.cpp b/troposphere/daybreak/source/ui.cpp index dfbd4151e..e7e726f27 100644 --- a/troposphere/daybreak/source/ui.cpp +++ b/troposphere/daybreak/source/ui.cpp @@ -43,7 +43,6 @@ namespace dbk { static constexpr float VerticalGap = 10.0f; - u32 g_screen_width; u32 g_screen_height; @@ -51,8 +50,10 @@ namespace dbk { bool g_initialized = false; bool g_exit_requested = false; + PadState g_pad; + u32 g_prev_touch_count = -1; - touchPosition g_start_touch_position; + HidTouchScreenState g_start_touch; bool g_started_touching = false; bool g_tapping = false; bool g_touches_moving = false; @@ -67,15 +68,14 @@ namespace dbk { constexpr u32 MaxTapMovement = 20; void UpdateInput() { - /* Update the previous touch count. */ - g_prev_touch_count = hidTouchCount(); - /* Scan for input and update touch state. */ - hidScanInput(); - const u32 touch_count = hidTouchCount(); + padUpdate(&g_pad); + HidTouchScreenState current_touch; + hidGetTouchScreenStates(¤t_touch, 1); + const u32 touch_count = current_touch.count; if (g_prev_touch_count == 0 && touch_count > 0) { - hidTouchRead(&g_start_touch_position, 0); + hidGetTouchScreenStates(&g_start_touch, 1); g_started_touching = true; g_tapping = true; } else { @@ -91,10 +91,7 @@ namespace dbk { /* Check if currently moving. */ if (g_prev_touch_count > 0 && touch_count > 0) { - touchPosition current_touch_position; - hidTouchRead(¤t_touch_position, 0); - - if ((abs(current_touch_position.px - g_start_touch_position.px) > MaxTapMovement || abs(current_touch_position.py - g_start_touch_position.py) > MaxTapMovement)) { + if ((abs(current_touch.touches[0].x - g_start_touch.touches[0].x) > MaxTapMovement || abs(current_touch.touches[0].y - g_start_touch.touches[0].y) > MaxTapMovement)) { g_touches_moving = true; g_tapping = false; } else { @@ -103,6 +100,9 @@ namespace dbk { } else { g_touches_moving = false; } + + /* Update the previous touch count. */ + g_prev_touch_count = current_touch.count; } void ChangeMenu(std::shared_ptr menu) { @@ -241,14 +241,13 @@ namespace dbk { } Button *Menu::GetTouchedButton() { - touchPosition touch; - const u32 touch_count = hidTouchCount(); + HidTouchScreenState current_touch; + hidGetTouchScreenStates(¤t_touch, 1); + const u32 touch_count = current_touch.count; for (u32 i = 0; i < touch_count && g_started_touching; i++) { - hidTouchRead(&touch, i); - for (auto &button : m_buttons) { - if (button && button->enabled && button->IsPositionInBounds(touch.px, touch.py)) { + if (button && button->enabled && button->IsPositionInBounds(current_touch.touches[i].x, current_touch.touches[i].y)) { return &(*button); } } @@ -264,9 +263,9 @@ namespace dbk { return nullptr; } - const u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); + const u64 k_down = padGetButtonsDown(&g_pad); - if (k_down & KEY_A || this->GetTouchedButton() == selected_button) { + if (k_down & HidNpadButton_A || this->GetTouchedButton() == selected_button) { return selected_button; } @@ -274,16 +273,16 @@ namespace dbk { } void Menu::UpdateButtons() { - const u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); + const u64 k_down = padGetButtonsDown(&g_pad); Direction direction = Direction::Invalid; - if (k_down & KEY_DOWN) { + if (k_down & HidNpadButton_AnyDown) { direction = Direction::Down; - } else if (k_down & KEY_UP) { + } else if (k_down & HidNpadButton_AnyUp) { direction = Direction::Up; - } else if (k_down & KEY_LEFT) { + } else if (k_down & HidNpadButton_AnyLeft) { direction = Direction::Left; - } else if (k_down & KEY_RIGHT) { + } else if (k_down & HidNpadButton_AnyRight) { direction = Direction::Right; } @@ -373,10 +372,10 @@ namespace dbk { } void ErrorMenu::Update(u64 ns) { - u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); + u64 k_down = padGetButtonsDown(&g_pad); /* Go back if B is pressed. */ - if (k_down & KEY_B) { + if (k_down & HidNpadButton_B) { g_exit_requested = true; return; } @@ -411,10 +410,10 @@ namespace dbk { } void WarningMenu::Update(u64 ns) { - u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); + u64 k_down = padGetButtonsDown(&g_pad); /* Go back if B is pressed. */ - if (k_down & KEY_B) { + if (k_down & HidNpadButton_B) { ReturnToPreviousMenu(); return; } @@ -449,9 +448,9 @@ namespace dbk { } void MainMenu::Update(u64 ns) { - u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); + u64 k_down = padGetButtonsDown(&g_pad); - if (k_down & KEY_B) { + if (k_down & HidNpadButton_B) { g_exit_requested = true; } @@ -574,16 +573,16 @@ namespace dbk { const float x = g_screen_width / 2.0f - WindowWidth / 2.0f; const float y = g_screen_height / 2.0f - WindowHeight / 2.0f; - touchPosition current_pos; - hidTouchRead(¤t_pos, 0); + HidTouchScreenState current_touch; + hidGetTouchScreenStates(¤t_touch, 1); /* Check if the tap is within the x bounds. */ - if (current_pos.px >= x + TextBackgroundOffset + FileRowHorizontalInset && current_pos.px <= WindowWidth - (TextBackgroundOffset + FileRowHorizontalInset) * 2.0f) { + if (current_touch.touches[0].x >= x + TextBackgroundOffset + FileRowHorizontalInset && current_touch.touches[0].x <= WindowWidth - (TextBackgroundOffset + FileRowHorizontalInset) * 2.0f) { const float y_min = y + TitleGap + FileRowGap + i * (FileRowHeight + FileRowGap) - m_scroll_offset; const float y_max = y_min + FileRowHeight; /* Check if the tap is within the y bounds. */ - if (current_pos.py >= y_min && current_pos.py <= y_max) { + if (current_touch.touches[0].y >= y_min && current_touch.touches[0].y <= y_max) { return true; } } @@ -604,10 +603,10 @@ namespace dbk { /* Scroll based on touch movement. */ if (g_touches_moving) { - touchPosition current_pos; - hidTouchRead(¤t_pos, 0); + HidTouchScreenState current_touch; + hidGetTouchScreenStates(¤t_touch, 1); - const int dist_y = current_pos.py - g_start_touch_position.py; + const int dist_y = current_touch.touches[0].y - g_start_touch.touches[0].y; float new_scroll_offset = m_touch_start_scroll_offset - static_cast(dist_y); float max_scroll = (FileRowHeight + FileRowGap) * static_cast(m_file_entries.size()) - FileListHeight; @@ -688,16 +687,16 @@ namespace dbk { } void FileMenu::Update(u64 ns) { - u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); + u64 k_down = padGetButtonsDown(&g_pad); /* Go back if B is pressed. */ - if (k_down & KEY_B) { + if (k_down & HidNpadButton_B) { ReturnToPreviousMenu(); return; } /* Finalize selection on pressing A. */ - if (k_down & KEY_A) { + if (k_down & HidNpadButton_A) { this->FinalizeSelection(); } @@ -706,14 +705,14 @@ namespace dbk { const u32 prev_index = m_current_index; - if (k_down & KEY_DOWN) { + if (k_down & HidNpadButton_AnyDown) { /* Scroll down. */ if (m_current_index >= (m_file_entries.size() - 1)) { m_current_index = 0; } else { m_current_index++; } - } else if (k_down & KEY_UP) { + } else if (k_down & HidNpadButton_AnyUp) { /* Scroll up. */ if (m_current_index == 0) { m_current_index = m_file_entries.size() - 1; @@ -859,10 +858,10 @@ namespace dbk { this->ValidateUpdate(); } - u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); + u64 k_down = padGetButtonsDown(&g_pad); /* Go back if B is pressed. */ - if (k_down & KEY_B) { + if (k_down & HidNpadButton_B) { ReturnToPreviousMenu(); return; } @@ -923,10 +922,10 @@ namespace dbk { } void ChooseResetMenu::Update(u64 ns) { - u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); + u64 k_down = padGetButtonsDown(&g_pad); /* Go back if B is pressed. */ - if (k_down & KEY_B) { + if (k_down & HidNpadButton_B) { ReturnToPreviousMenu(); return; } @@ -986,10 +985,10 @@ namespace dbk { } void ChooseExfatMenu::Update(u64 ns) { - u64 k_down = hidKeysDown(CONTROLLER_P1_AUTO); + u64 k_down = padGetButtonsDown(&g_pad); /* Go back if B is pressed. */ - if (k_down & KEY_B) { + if (k_down & HidNpadButton_B) { ReturnToPreviousMenu(); return; } @@ -1195,6 +1194,13 @@ namespace dbk { void InitializeMenu(u32 screen_width, u32 screen_height) { Result rc = 0; + /* Configure and initialize the gamepad. */ + padConfigureInput(1, HidNpadStyleSet_NpadStandard); + padInitializeDefault(&g_pad); + + /* Initialize the touch screen. */ + hidInitializeTouchScreen(); + /* Set the screen width and height. */ g_screen_width = screen_width; g_screen_height = screen_height;