summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/hid/controllers/npad/npad_data.cpp
blob: d2423b6d3ddad633c6ad36c3696cd497747679ce (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

#include "core/hle/service/hid/controllers/npad/npad_data.h"
#include "core/hle/service/hid/hid_util.h"

namespace Service::HID {

NPadData::NPadData() {
    ClearNpadSystemCommonPolicy();
}

NPadData::~NPadData() = default;

NpadStatus NPadData::GetNpadStatus() const {
    return status;
}

void NPadData::SetNpadAnalogStickUseCenterClamp(bool is_enabled) {
    status.use_center_clamp.Assign(is_enabled);
}

bool NPadData::GetNpadAnalogStickUseCenterClamp() const {
    return status.use_center_clamp.As<bool>();
}

void NPadData::SetNpadSystemExtStateEnabled(bool is_enabled) {
    status.system_ext_state.Assign(is_enabled);
}

bool NPadData::GetNpadSystemExtState() const {
    return status.system_ext_state.As<bool>();
}

Result NPadData::SetSupportedNpadIdType(std::span<const Core::HID::NpadIdType> list) {
    // Note: Real limit is 11. But array size is 10. N's bug?
    if (list.size() > MaxSupportedNpadIdTypes) {
        return ResultInvalidArraySize;
    }

    supported_npad_id_types_count = list.size();
    memcpy(supported_npad_id_types.data(), list.data(),
           list.size() * sizeof(Core::HID::NpadIdType));

    return ResultSuccess;
}

std::size_t NPadData::GetSupportedNpadIdType(std::span<Core::HID::NpadIdType> out_list) const {
    std::size_t out_size = std::min(supported_npad_id_types_count, out_list.size());

    memcpy(out_list.data(), supported_npad_id_types.data(),
           out_size * sizeof(Core::HID::NpadIdType));

    return out_size;
}

bool NPadData::IsNpadIdTypeSupported(Core::HID::NpadIdType npad_id) const {
    for (std::size_t i = 0; i < supported_npad_id_types_count; i++) {
        if (supported_npad_id_types[i] == npad_id) {
            return true;
        }
    }

    return false;
}

void NPadData::SetNpadSystemCommonPolicy(bool is_full_policy) {
    supported_npad_style_set = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::JoyDual |
                               Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
    handheld_activation_mode = NpadHandheldActivationMode::Dual;

    status.is_supported_styleset_set.Assign(true);
    status.is_hold_type_set.Assign(true);
    status.lr_assignment_mode.Assign(false);
    status.is_policy.Assign(true);
    if (is_full_policy) {
        status.is_full_policy.Assign(true);
    }

    supported_npad_id_types_count = 10;
    supported_npad_id_types[0] = Core::HID::NpadIdType::Player1;
    supported_npad_id_types[1] = Core::HID::NpadIdType::Player2;
    supported_npad_id_types[2] = Core::HID::NpadIdType::Player3;
    supported_npad_id_types[3] = Core::HID::NpadIdType::Player4;
    supported_npad_id_types[4] = Core::HID::NpadIdType::Player5;
    supported_npad_id_types[5] = Core::HID::NpadIdType::Player6;
    supported_npad_id_types[6] = Core::HID::NpadIdType::Player7;
    supported_npad_id_types[7] = Core::HID::NpadIdType::Player8;
    supported_npad_id_types[8] = Core::HID::NpadIdType::Other;
    supported_npad_id_types[9] = Core::HID::NpadIdType::Handheld;

    for (auto& input_protection : is_unintended_home_button_input_protection) {
        input_protection = true;
    }
}

void NPadData::ClearNpadSystemCommonPolicy() {
    status.raw = 0;
    supported_npad_style_set = Core::HID::NpadStyleSet::All;
    npad_hold_type = NpadJoyHoldType::Vertical;
    handheld_activation_mode = NpadHandheldActivationMode::Dual;

    for (auto& button_assignment : npad_button_assignment) {
        button_assignment = Core::HID::NpadButton::None;
    }

    supported_npad_id_types_count = 10;
    supported_npad_id_types[0] = Core::HID::NpadIdType::Player1;
    supported_npad_id_types[1] = Core::HID::NpadIdType::Player2;
    supported_npad_id_types[2] = Core::HID::NpadIdType::Player3;
    supported_npad_id_types[3] = Core::HID::NpadIdType::Player4;
    supported_npad_id_types[4] = Core::HID::NpadIdType::Player5;
    supported_npad_id_types[5] = Core::HID::NpadIdType::Player6;
    supported_npad_id_types[6] = Core::HID::NpadIdType::Player7;
    supported_npad_id_types[7] = Core::HID::NpadIdType::Player8;
    supported_npad_id_types[8] = Core::HID::NpadIdType::Other;
    supported_npad_id_types[9] = Core::HID::NpadIdType::Handheld;

    for (auto& input_protection : is_unintended_home_button_input_protection) {
        input_protection = true;
    }
}

void NPadData::SetNpadJoyHoldType(NpadJoyHoldType hold_type) {
    npad_hold_type = hold_type;
    status.is_hold_type_set.Assign(true);
}

NpadJoyHoldType NPadData::GetNpadJoyHoldType() const {
    return npad_hold_type;
}

void NPadData::SetHandheldActivationMode(NpadHandheldActivationMode activation_mode) {
    handheld_activation_mode = activation_mode;
}

NpadHandheldActivationMode NPadData::GetHandheldActivationMode() const {
    return handheld_activation_mode;
}

void NPadData::SetSupportedNpadStyleSet(Core::HID::NpadStyleSet style_set) {
    supported_npad_style_set = style_set;
    status.is_supported_styleset_set.Assign(true);
    status.is_hold_type_set.Assign(true);
}

Core::HID::NpadStyleSet NPadData::GetSupportedNpadStyleSet() const {
    return supported_npad_style_set;
}

bool NPadData::IsNpadStyleIndexSupported(Core::HID::NpadStyleIndex style_index) const {
    Core::HID::NpadStyleTag style = {supported_npad_style_set};
    switch (style_index) {
    case Core::HID::NpadStyleIndex::ProController:
        return style.fullkey.As<bool>();
    case Core::HID::NpadStyleIndex::Handheld:
        return style.handheld.As<bool>();
    case Core::HID::NpadStyleIndex::JoyconDual:
        return style.joycon_dual.As<bool>();
    case Core::HID::NpadStyleIndex::JoyconLeft:
        return style.joycon_left.As<bool>();
    case Core::HID::NpadStyleIndex::JoyconRight:
        return style.joycon_right.As<bool>();
    case Core::HID::NpadStyleIndex::GameCube:
        return style.gamecube.As<bool>();
    case Core::HID::NpadStyleIndex::Pokeball:
        return style.palma.As<bool>();
    case Core::HID::NpadStyleIndex::NES:
        return style.lark.As<bool>();
    case Core::HID::NpadStyleIndex::SNES:
        return style.lucia.As<bool>();
    case Core::HID::NpadStyleIndex::N64:
        return style.lagoon.As<bool>();
    case Core::HID::NpadStyleIndex::SegaGenesis:
        return style.lager.As<bool>();
    default:
        return false;
    }
}

void NPadData::SetLrAssignmentMode(bool is_enabled) {
    status.lr_assignment_mode.Assign(is_enabled);
}

bool NPadData::GetLrAssignmentMode() const {
    return status.lr_assignment_mode.As<bool>();
}

void NPadData::SetAssigningSingleOnSlSrPress(bool is_enabled) {
    status.assigning_single_on_sl_sr_press.Assign(is_enabled);
}

bool NPadData::GetAssigningSingleOnSlSrPress() const {
    return status.assigning_single_on_sl_sr_press.As<bool>();
}

void NPadData::SetHomeProtectionEnabled(bool is_enabled, Core::HID::NpadIdType npad_id) {
    is_unintended_home_button_input_protection[NpadIdTypeToIndex(npad_id)] = is_enabled;
}

bool NPadData::GetHomeProtectionEnabled(Core::HID::NpadIdType npad_id) const {
    return is_unintended_home_button_input_protection[NpadIdTypeToIndex(npad_id)];
}

void NPadData::SetCaptureButtonAssignment(Core::HID::NpadButton button_assignment,
                                          std::size_t style_index) {
    npad_button_assignment[style_index] = button_assignment;
}

Core::HID::NpadButton NPadData::GetCaptureButtonAssignment(std::size_t style_index) const {
    return npad_button_assignment[style_index];
}

std::size_t NPadData::GetNpadCaptureButtonAssignmentList(
    std::span<Core::HID::NpadButton> out_list) const {
    for (std::size_t i = 0; i < out_list.size(); i++) {
        Core::HID::NpadStyleSet style_set = GetStylesetByIndex(i);
        if ((style_set & supported_npad_style_set) == Core::HID::NpadStyleSet::None ||
            npad_button_assignment[i] == Core::HID::NpadButton::None) {
            return i;
        }
        out_list[i] = npad_button_assignment[i];
    }

    return out_list.size();
}

} // namespace Service::HID