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();