aboutsummaryrefslogtreecommitdiff
path: root/src/hid_core/hid_util.h
blob: 397a87472fe860fffbc01079064954f9dbaf98bc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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::Fullkey:
    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