summaryrefslogtreecommitdiffstats
path: root/src/hid_core/resources/abstracted_pad/abstract_sixaxis_handler.cpp
blob: 0dde244ef8429d88461b1d02d155effcc99e87fa (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
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

#include "hid_core/hid_result.h"
#include "hid_core/hid_util.h"
#include "hid_core/resources/abstracted_pad/abstract_pad_holder.h"
#include "hid_core/resources/abstracted_pad/abstract_properties_handler.h"
#include "hid_core/resources/abstracted_pad/abstract_sixaxis_handler.h"
#include "hid_core/resources/applet_resource.h"
#include "hid_core/resources/npad/npad_types.h"
#include "hid_core/resources/shared_memory_format.h"

namespace Service::HID {

NpadAbstractSixAxisHandler::NpadAbstractSixAxisHandler() {}

NpadAbstractSixAxisHandler::~NpadAbstractSixAxisHandler() = default;

void NpadAbstractSixAxisHandler::SetAbstractPadHolder(NpadAbstractedPadHolder* holder) {
    abstract_pad_holder = holder;
}

void NpadAbstractSixAxisHandler::SetAppletResource(AppletResourceHolder* applet_resource) {
    applet_resource_holder = applet_resource;
}

void NpadAbstractSixAxisHandler::SetPropertiesHandler(NpadAbstractPropertiesHandler* handler) {
    properties_handler = handler;
}

void NpadAbstractSixAxisHandler::SetSixaxisResource(SixAxisResource* resource) {
    six_axis_resource = resource;
}

Result NpadAbstractSixAxisHandler::IncrementRefCounter() {
    if (ref_counter == std::numeric_limits<s32>::max() - 1) {
        return ResultNpadHandlerOverflow;
    }
    ref_counter++;
    return ResultSuccess;
}

Result NpadAbstractSixAxisHandler::DecrementRefCounter() {
    if (ref_counter == 0) {
        return ResultNpadHandlerNotInitialized;
    }
    ref_counter--;
    return ResultSuccess;
}

u64 NpadAbstractSixAxisHandler::IsFirmwareUpdateAvailable() {
    // TODO
    return false;
}

Result NpadAbstractSixAxisHandler::UpdateSixAxisState() {
    Core::HID::NpadIdType npad_id = properties_handler->GetNpadId();
    for (std::size_t i = 0; i < AruidIndexMax; i++) {
        auto* data = applet_resource_holder->applet_resource->GetAruidDataByIndex(i);
        if (data == nullptr || !data->flag.is_assigned) {
            continue;
        }
        auto& npad_entry = data->shared_memory_format->npad.npad_entry[NpadIdTypeToIndex(npad_id)];
        UpdateSixaxisInternalState(npad_entry, data->aruid,
                                   data->flag.enable_six_axis_sensor.As<bool>());
    }
    return ResultSuccess;
}

Result NpadAbstractSixAxisHandler::UpdateSixAxisState(u64 aruid) {
    Core::HID::NpadIdType npad_id = properties_handler->GetNpadId();
    auto* data = applet_resource_holder->applet_resource->GetAruidData(aruid);
    if (data == nullptr) {
        return ResultSuccess;
    }
    auto& npad_entry = data->shared_memory_format->npad.npad_entry[NpadIdTypeToIndex(npad_id)];
    UpdateSixaxisInternalState(npad_entry, data->aruid,
                               data->flag.enable_six_axis_sensor.As<bool>());
    return ResultSuccess;
}

Result NpadAbstractSixAxisHandler::UpdateSixAxisState2(u64 aruid) {
    const auto npad_index = NpadIdTypeToIndex(properties_handler->GetNpadId());
    AruidData* aruid_data = applet_resource_holder->applet_resource->GetAruidData(aruid);
    if (aruid_data == nullptr) {
        return ResultSuccess;
    }
    auto& npad_internal_state = aruid_data->shared_memory_format->npad.npad_entry[npad_index];
    UpdateSixaxisInternalState(npad_internal_state, aruid,
                               aruid_data->flag.enable_six_axis_sensor.As<bool>());
    return ResultSuccess;
}

void NpadAbstractSixAxisHandler::UpdateSixaxisInternalState(NpadSharedMemoryEntry& npad_entry,
                                                            u64 aruid, bool is_sensor_enabled) {
    const Core::HID::NpadStyleTag style_tag{properties_handler->GetStyleSet(aruid)};

    if (!style_tag.palma) {
        UpdateSixaxisFullkeyLifo(style_tag, npad_entry.internal_state.sixaxis_fullkey_lifo,
                                 is_sensor_enabled);
    } else {
        UpdateSixAxisPalmaLifo(style_tag, npad_entry.internal_state.sixaxis_fullkey_lifo,
                               is_sensor_enabled);
    }
    UpdateSixaxisHandheldLifo(style_tag, npad_entry.internal_state.sixaxis_handheld_lifo,
                              is_sensor_enabled);
    UpdateSixaxisDualLifo(style_tag, npad_entry.internal_state.sixaxis_dual_left_lifo,
                          is_sensor_enabled);
    UpdateSixaxisDualLifo(style_tag, npad_entry.internal_state.sixaxis_dual_right_lifo,
                          is_sensor_enabled);
    UpdateSixaxisLeftLifo(style_tag, npad_entry.internal_state.sixaxis_left_lifo,
                          is_sensor_enabled);
    UpdateSixaxisRightLifo(style_tag, npad_entry.internal_state.sixaxis_right_lifo,
                           is_sensor_enabled);
    // TODO: Set sixaxis properties
}

void NpadAbstractSixAxisHandler::UpdateSixaxisFullkeyLifo(Core::HID::NpadStyleTag style_tag,
                                                          NpadSixAxisSensorLifo& sensor_lifo,
                                                          bool is_sensor_enabled) {
    // TODO
}

void NpadAbstractSixAxisHandler::UpdateSixAxisPalmaLifo(Core::HID::NpadStyleTag style_tag,
                                                        NpadSixAxisSensorLifo& sensor_lifo,
                                                        bool is_sensor_enabled) {
    // TODO
}

void NpadAbstractSixAxisHandler::UpdateSixaxisHandheldLifo(Core::HID::NpadStyleTag style_tag,
                                                           NpadSixAxisSensorLifo& sensor_lifo,
                                                           bool is_sensor_enabled) {
    // TODO
}

void NpadAbstractSixAxisHandler::UpdateSixaxisDualLifo(Core::HID::NpadStyleTag style_tag,
                                                       NpadSixAxisSensorLifo& sensor_lifo,
                                                       bool is_sensor_enabled) {
    // TODO
}

void NpadAbstractSixAxisHandler::UpdateSixaxisLeftLifo(Core::HID::NpadStyleTag style_tag,
                                                       NpadSixAxisSensorLifo& sensor_lifo,
                                                       bool is_sensor_enabled) {
    // TODO
}

void NpadAbstractSixAxisHandler::UpdateSixaxisRightLifo(Core::HID::NpadStyleTag style_tag,
                                                        NpadSixAxisSensorLifo& sensor_lifo,
                                                        bool is_sensor_enabled) {
    // TODO
}

} // namespace Service::HID