summaryrefslogtreecommitdiffstats
path: root/src/hid_core/hid_util.h
diff options
context:
space:
mode:
authorNarr the Reg <juangerman-13@hotmail.com>2024-01-05 03:37:43 +0100
committerNarr the Reg <juangerman-13@hotmail.com>2024-01-05 18:41:15 +0100
commitee847f8ff0b1b0aec39c1b78c010bc0c08a0a613 (patch)
tree3b95cbb74be05f0ce7a007353f1f9f95e1ed3901 /src/hid_core/hid_util.h
parentMerge pull request #12437 from ameerj/gl-amd-fixes (diff)
downloadyuzu-ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613.tar
yuzu-ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613.tar.gz
yuzu-ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613.tar.bz2
yuzu-ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613.tar.lz
yuzu-ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613.tar.xz
yuzu-ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613.tar.zst
yuzu-ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613.zip
Diffstat (limited to 'src/hid_core/hid_util.h')
-rw-r--r--src/hid_core/hid_util.h146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/hid_core/hid_util.h b/src/hid_core/hid_util.h
new file mode 100644
index 000000000..94ff2d23a
--- /dev/null
+++ b/src/hid_core/hid_util.h
@@ -0,0 +1,146 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+#pragma once
+
+#include "hid_core/hid_result.h"
+#include "hid_core/hid_types.h"
+
+namespace Service::HID {
+
+constexpr bool IsNpadIdValid(const Core::HID::NpadIdType npad_id) {
+ switch (npad_id) {
+ case Core::HID::NpadIdType::Player1:
+ case Core::HID::NpadIdType::Player2:
+ case Core::HID::NpadIdType::Player3:
+ case Core::HID::NpadIdType::Player4:
+ case Core::HID::NpadIdType::Player5:
+ case Core::HID::NpadIdType::Player6:
+ case Core::HID::NpadIdType::Player7:
+ case Core::HID::NpadIdType::Player8:
+ case Core::HID::NpadIdType::Other:
+ case Core::HID::NpadIdType::Handheld:
+ return true;
+ default:
+ return false;
+ }
+}
+
+constexpr Result IsSixaxisHandleValid(const Core::HID::SixAxisSensorHandle& handle) {
+ const auto npad_id = IsNpadIdValid(static_cast<Core::HID::NpadIdType>(handle.npad_id));
+ const bool device_index = handle.device_index < Core::HID::DeviceIndex::MaxDeviceIndex;
+
+ if (!npad_id) {
+ return ResultInvalidNpadId;
+ }
+ if (!device_index) {
+ return NpadDeviceIndexOutOfRange;
+ }
+
+ return ResultSuccess;
+}
+
+constexpr Result IsVibrationHandleValid(const Core::HID::VibrationDeviceHandle& handle) {
+ switch (handle.npad_type) {
+ case Core::HID::NpadStyleIndex::ProController:
+ case Core::HID::NpadStyleIndex::Handheld:
+ case Core::HID::NpadStyleIndex::JoyconDual:
+ case Core::HID::NpadStyleIndex::JoyconLeft:
+ case Core::HID::NpadStyleIndex::JoyconRight:
+ case Core::HID::NpadStyleIndex::GameCube:
+ case Core::HID::NpadStyleIndex::N64:
+ case Core::HID::NpadStyleIndex::SystemExt:
+ case Core::HID::NpadStyleIndex::System:
+ // These support vibration
+ break;
+ default:
+ return ResultVibrationInvalidStyleIndex;
+ }
+
+ if (!IsNpadIdValid(static_cast<Core::HID::NpadIdType>(handle.npad_id))) {
+ return ResultVibrationInvalidNpadId;
+ }
+
+ if (handle.device_index >= Core::HID::DeviceIndex::MaxDeviceIndex) {
+ return ResultVibrationDeviceIndexOutOfRange;
+ }
+
+ return ResultSuccess;
+}
+
+/// Converts a Core::HID::NpadIdType to an array index.
+constexpr size_t NpadIdTypeToIndex(Core::HID::NpadIdType npad_id_type) {
+ switch (npad_id_type) {
+ case Core::HID::NpadIdType::Player1:
+ return 0;
+ case Core::HID::NpadIdType::Player2:
+ return 1;
+ case Core::HID::NpadIdType::Player3:
+ return 2;
+ case Core::HID::NpadIdType::Player4:
+ return 3;
+ case Core::HID::NpadIdType::Player5:
+ return 4;
+ case Core::HID::NpadIdType::Player6:
+ return 5;
+ case Core::HID::NpadIdType::Player7:
+ return 6;
+ case Core::HID::NpadIdType::Player8:
+ return 7;
+ case Core::HID::NpadIdType::Handheld:
+ return 8;
+ case Core::HID::NpadIdType::Other:
+ return 9;
+ default:
+ return 8;
+ }
+}
+
+/// Converts an array index to a Core::HID::NpadIdType
+constexpr Core::HID::NpadIdType IndexToNpadIdType(size_t index) {
+ switch (index) {
+ case 0:
+ return Core::HID::NpadIdType::Player1;
+ case 1:
+ return Core::HID::NpadIdType::Player2;
+ case 2:
+ return Core::HID::NpadIdType::Player3;
+ case 3:
+ return Core::HID::NpadIdType::Player4;
+ case 4:
+ return Core::HID::NpadIdType::Player5;
+ case 5:
+ return Core::HID::NpadIdType::Player6;
+ case 6:
+ return Core::HID::NpadIdType::Player7;
+ case 7:
+ return Core::HID::NpadIdType::Player8;
+ case 8:
+ return Core::HID::NpadIdType::Handheld;
+ case 9:
+ return Core::HID::NpadIdType::Other;
+ default:
+ return Core::HID::NpadIdType::Invalid;
+ }
+}
+
+constexpr Core::HID::NpadStyleSet GetStylesetByIndex(std::size_t index) {
+ switch (index) {
+ case 0:
+ return Core::HID::NpadStyleSet::Fullkey;
+ case 1:
+ return Core::HID::NpadStyleSet::Handheld;
+ case 2:
+ return Core::HID::NpadStyleSet::JoyDual;
+ case 3:
+ return Core::HID::NpadStyleSet::JoyLeft;
+ case 4:
+ return Core::HID::NpadStyleSet::JoyRight;
+ case 5:
+ return Core::HID::NpadStyleSet::Palma;
+ default:
+ return Core::HID::NpadStyleSet::None;
+ }
+}
+
+} // namespace Service::HID