2019-12-09 12:57:37 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-09 12:57:37 +01: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 {
|
|
|
|
|
2022-03-06 21:08:20 +01:00
|
|
|
#if defined(ATMOSPHERE_OS_HORIZON)
|
2019-12-09 12:57:37 +01:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
/* Global lock. */
|
2021-09-30 07:52:50 +02:00
|
|
|
constinit os::SdkMutex g_hid_lock;
|
|
|
|
constinit bool g_initialized_hid = false;
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-12-02 00:42:13 +01: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 12:57:37 +01:00
|
|
|
/* Helper. */
|
|
|
|
void InitializeHid() {
|
2021-04-14 08:58:10 +02: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 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result EnsureHidInitialized() {
|
|
|
|
if (!g_initialized_hid) {
|
|
|
|
if (!serviceIsActive(hidGetServiceSession())) {
|
2021-01-18 07:03:26 +01:00
|
|
|
if (!pm::info::HasLaunchedBootProgram(ncm::SystemProgramId::Hid)) {
|
2022-03-26 22:48:33 +01:00
|
|
|
R_THROW(MAKERESULT(Module_Libnx, LibnxError_InitFail_HID));
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
InitializeHid();
|
|
|
|
}
|
|
|
|
g_initialized_hid = true;
|
|
|
|
}
|
|
|
|
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 00:42:13 +01: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 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result GetKeysHeld(u64 *out) {
|
|
|
|
std::scoped_lock lk(g_hid_lock);
|
|
|
|
|
|
|
|
R_TRY(EnsureHidInitialized());
|
|
|
|
|
2020-12-02 00:42:13 +01:00
|
|
|
*out = ReadHidNpad(HidNpadIdType_Handheld);
|
2019-12-09 12:57:37 +01:00
|
|
|
|
2020-12-02 00:42:13 +01:00
|
|
|
for (size_t controller = 0; controller < 8; controller++) {
|
|
|
|
*out |= ReadHidNpad(static_cast<HidNpadIdType>(controller));
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 08:14:36 +01:00
|
|
|
R_SUCCEED();
|
2019-12-09 12:57:37 +01:00
|
|
|
}
|
2022-03-06 21:08:20 +01:00
|
|
|
#endif
|
2019-12-09 12:57:37 +01:00
|
|
|
|
|
|
|
}
|