strat/hid: Update for new libnx HID interface

This commit is contained in:
fincs 2020-12-02 00:42:13 +01:00 committed by SciresM
parent 8b2ed36698
commit a03ee7b148

View file

@ -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<HidControllerID>(controller));
for (size_t controller = 0; controller < 8; controller++) {
*out |= ReadHidNpad(static_cast<HidNpadIdType>(controller));
}
return ResultSuccess();