2019-12-09 11:57:37 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-12-09 11:57:37 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::hid {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Global lock. */
|
2021-09-30 05:52:50 +00:00
|
|
|
constinit os::SdkMutex g_hid_lock;
|
|
|
|
constinit bool g_initialized_hid = false;
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-12-01 23:42:13 +00:00
|
|
|
/* 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);
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
/* Helper. */
|
|
|
|
void InitializeHid() {
|
2021-04-14 06:58:10 +00:00
|
|
|
R_ABORT_UNLESS(sm::Initialize());
|
|
|
|
R_ABORT_UNLESS(hidInitialize());
|
|
|
|
hidInitializeNpad();
|
|
|
|
R_ABORT_UNLESS(hidSetSupportedNpadIdType(NpadIdTypes, NumNpadIdTypes));
|
|
|
|
R_ABORT_UNLESS(hidSetSupportedNpadStyleSet(HidNpadStyleSet_NpadStandard | HidNpadStyleTag_NpadSystemExt));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result EnsureHidInitialized() {
|
|
|
|
if (!g_initialized_hid) {
|
|
|
|
if (!serviceIsActive(hidGetServiceSession())) {
|
2021-01-18 06:03:26 +00:00
|
|
|
if (!pm::info::HasLaunchedBootProgram(ncm::SystemProgramId::Hid)) {
|
2019-12-09 11:57:37 +00:00
|
|
|
return MAKERESULT(Module_Libnx, LibnxError_InitFail_HID);
|
|
|
|
}
|
|
|
|
InitializeHid();
|
|
|
|
}
|
|
|
|
g_initialized_hid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2020-12-01 23:42:13 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetKeysHeld(u64 *out) {
|
|
|
|
std::scoped_lock lk(g_hid_lock);
|
|
|
|
|
|
|
|
R_TRY(EnsureHidInitialized());
|
|
|
|
|
2020-12-01 23:42:13 +00:00
|
|
|
*out = ReadHidNpad(HidNpadIdType_Handheld);
|
2019-12-09 11:57:37 +00:00
|
|
|
|
2020-12-01 23:42:13 +00:00
|
|
|
for (size_t controller = 0; controller < 8; controller++) {
|
|
|
|
*out |= ReadHidNpad(static_cast<HidNpadIdType>(controller));
|
2019-12-09 11:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|