summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/hid/controllers/debug_pad.cpp
blob: dc83f90f3b1508b8c9769c43598b83d45d6c627c (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
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "common/settings.h"
#include "core/core_timing.h"
#include "core/hid/emulated_controller.h"
#include "core/hid/hid_core.h"
#include "core/hid/hid_types.h"
#include "core/hle/service/hid/controllers/applet_resource.h"
#include "core/hle/service/hid/controllers/debug_pad.h"
#include "core/hle/service/hid/controllers/types/shared_memory_format.h"

namespace Service::HID {

DebugPad::DebugPad(Core::HID::HIDCore& hid_core_) : ControllerBase{hid_core_} {
    controller = hid_core.GetEmulatedController(Core::HID::NpadIdType::Other);
}

DebugPad::~DebugPad() = default;

void DebugPad::OnInit() {}

void DebugPad::OnRelease() {}

void DebugPad::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
    std::scoped_lock shared_lock{*shared_mutex};
    const u64 aruid = applet_resource->GetActiveAruid();
    auto* data = applet_resource->GetAruidData(aruid);

    if (data == nullptr || !data->flag.is_assigned) {
        return;
    }

    DebugPadSharedMemoryFormat& shared_memory = data->shared_memory_format->debug_pad;

    if (!IsControllerActivated()) {
        shared_memory.debug_pad_lifo.buffer_count = 0;
        shared_memory.debug_pad_lifo.buffer_tail = 0;
        return;
    }

    const auto& last_entry = shared_memory.debug_pad_lifo.ReadCurrentEntry().state;
    next_state.sampling_number = last_entry.sampling_number + 1;

    if (Settings::values.debug_pad_enabled) {
        next_state.attribute.connected.Assign(1);

        const auto& button_state = controller->GetDebugPadButtons();
        const auto& stick_state = controller->GetSticks();

        next_state.pad_state = button_state;
        next_state.l_stick = stick_state.left;
        next_state.r_stick = stick_state.right;
    }

    shared_memory.debug_pad_lifo.WriteNextEntry(next_state);
}

} // namespace Service::HID