aboutsummaryrefslogtreecommitdiff
path: root/src/input_common/drivers/virtual_gamepad.cpp
blob: c15cbbe58ee8b5ff16c1255a80762f0da7805575 (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
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "input_common/drivers/virtual_gamepad.h"

namespace InputCommon {
constexpr std::size_t PlayerIndexCount = 10;

VirtualGamepad::VirtualGamepad(std::string input_engine_) : InputEngine(std::move(input_engine_)) {
    for (std::size_t i = 0; i < PlayerIndexCount; i++) {
        PreSetController(GetIdentifier(i));
    }
}

void VirtualGamepad::SetButtonState(std::size_t player_index, int button_id, bool value) {
    if (player_index > PlayerIndexCount) {
        return;
    }
    const auto identifier = GetIdentifier(player_index);
    SetButton(identifier, button_id, value);
}

void VirtualGamepad::SetButtonState(std::size_t player_index, VirtualButton button_id, bool value) {
    SetButtonState(player_index, static_cast<int>(button_id), value);
}

void VirtualGamepad::SetStickPosition(std::size_t player_index, int axis_id, float x_value,
                                      float y_value) {
    if (player_index > PlayerIndexCount) {
        return;
    }
    const auto identifier = GetIdentifier(player_index);
    SetAxis(identifier, axis_id * 2, x_value);
    SetAxis(identifier, (axis_id * 2) + 1, y_value);
}

void VirtualGamepad::SetStickPosition(std::size_t player_index, VirtualStick axis_id, float x_value,
                                      float y_value) {
    SetStickPosition(player_index, static_cast<int>(axis_id), x_value, y_value);
}

void VirtualGamepad::SetMotionState(std::size_t player_index, u64 delta_timestamp, float gyro_x,
                                    float gyro_y, float gyro_z, float accel_x, float accel_y,
                                    float accel_z) {
    const auto identifier = GetIdentifier(player_index);
    const BasicMotion motion_data{
        .gyro_x = gyro_x,
        .gyro_y = gyro_y,
        .gyro_z = gyro_z,
        .accel_x = accel_x,
        .accel_y = accel_y,
        .accel_z = accel_z,
        .delta_timestamp = delta_timestamp,
    };
    SetMotion(identifier, 0, motion_data);
}

void VirtualGamepad::ResetControllers() {
    for (std::size_t i = 0; i < PlayerIndexCount; i++) {
        SetStickPosition(i, VirtualStick::Left, 0.0f, 0.0f);
        SetStickPosition(i, VirtualStick::Right, 0.0f, 0.0f);

        SetButtonState(i, VirtualButton::ButtonA, false);
        SetButtonState(i, VirtualButton::ButtonB, false);
        SetButtonState(i, VirtualButton::ButtonX, false);
        SetButtonState(i, VirtualButton::ButtonY, false);
        SetButtonState(i, VirtualButton::StickL, false);
        SetButtonState(i, VirtualButton::StickR, false);
        SetButtonState(i, VirtualButton::TriggerL, false);
        SetButtonState(i, VirtualButton::TriggerR, false);
        SetButtonState(i, VirtualButton::TriggerZL, false);
        SetButtonState(i, VirtualButton::TriggerZR, false);
        SetButtonState(i, VirtualButton::ButtonPlus, false);
        SetButtonState(i, VirtualButton::ButtonMinus, false);
        SetButtonState(i, VirtualButton::ButtonLeft, false);
        SetButtonState(i, VirtualButton::ButtonUp, false);
        SetButtonState(i, VirtualButton::ButtonRight, false);
        SetButtonState(i, VirtualButton::ButtonDown, false);
        SetButtonState(i, VirtualButton::ButtonSL, false);
        SetButtonState(i, VirtualButton::ButtonSR, false);
        SetButtonState(i, VirtualButton::ButtonHome, false);
        SetButtonState(i, VirtualButton::ButtonCapture, false);
    }
}

PadIdentifier VirtualGamepad::GetIdentifier(std::size_t player_index) const {
    return {
        .guid = Common::UUID{},
        .port = player_index,
        .pad = 0,
    };
}

} // namespace InputCommon