summaryrefslogtreecommitdiffstats
path: root/src/input_common/udp/udp.cpp
blob: eba077a36c694b3b413176918043fad1f4e0b0f8 (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
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include <atomic>
#include <list>
#include <mutex>
#include <utility>
#include "common/assert.h"
#include "common/threadsafe_queue.h"
#include "input_common/udp/client.h"
#include "input_common/udp/udp.h"

namespace InputCommon {

class UDPMotion final : public Input::MotionDevice {
public:
    UDPMotion(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_)
        : ip(ip_), port(port_), pad(pad_), client(client_) {}

    Input::MotionStatus GetStatus() const override {
        return client->GetPadState(pad).motion_status;
    }

private:
    const std::string ip;
    const int port;
    const int pad;
    CemuhookUDP::Client* client;
    mutable std::mutex mutex;
};

/// A motion device factory that creates motion devices from JC Adapter
UDPMotionFactory::UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_)
    : client(std::move(client_)) {}

/**
 * Creates motion device
 * @param params contains parameters for creating the device:
 *     - "port": the nth jcpad on the adapter
 */
std::unique_ptr<Input::MotionDevice> UDPMotionFactory::Create(const Common::ParamPackage& params) {
    const std::string ip = params.Get("ip", "127.0.0.1");
    const int port = params.Get("port", 26760);
    const int pad = params.Get("pad_index", 0);

    return std::make_unique<UDPMotion>(ip, port, pad, client.get());
}

void UDPMotionFactory::BeginConfiguration() {
    polling = true;
    client->BeginConfiguration();
}

void UDPMotionFactory::EndConfiguration() {
    polling = false;
    client->EndConfiguration();
}

Common::ParamPackage UDPMotionFactory::GetNextInput() {
    Common::ParamPackage params;
    CemuhookUDP::UDPPadStatus pad;
    auto& queue = client->GetPadQueue();
    for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) {
        while (queue[pad_number].Pop(pad)) {
            if (pad.motion == CemuhookUDP::PadMotion::Undefined || std::abs(pad.motion_value) < 1) {
                continue;
            }
            params.Set("engine", "cemuhookudp");
            params.Set("ip", "127.0.0.1");
            params.Set("port", 26760);
            params.Set("pad_index", static_cast<int>(pad_number));
            params.Set("motion", static_cast<u16>(pad.motion));
            return params;
        }
    }
    return params;
}

class UDPTouch final : public Input::TouchDevice {
public:
    UDPTouch(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_)
        : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {}

    std::tuple<float, float, bool> GetStatus() const override {
        return client->GetPadState(pad).touch_status;
    }

private:
    const std::string ip;
    const int port;
    const int pad;
    CemuhookUDP::Client* client;
    mutable std::mutex mutex;
};

/// A motion device factory that creates motion devices from JC Adapter
UDPTouchFactory::UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_)
    : client(std::move(client_)) {}

/**
 * Creates motion device
 * @param params contains parameters for creating the device:
 *     - "port": the nth jcpad on the adapter
 */
std::unique_ptr<Input::TouchDevice> UDPTouchFactory::Create(const Common::ParamPackage& params) {
    const std::string ip = params.Get("ip", "127.0.0.1");
    const int port = params.Get("port", 26760);
    const int pad = params.Get("pad_index", 0);

    return std::make_unique<UDPTouch>(ip, port, pad, client.get());
}

void UDPTouchFactory::BeginConfiguration() {
    polling = true;
    client->BeginConfiguration();
}

void UDPTouchFactory::EndConfiguration() {
    polling = false;
    client->EndConfiguration();
}

Common::ParamPackage UDPTouchFactory::GetNextInput() {
    Common::ParamPackage params;
    CemuhookUDP::UDPPadStatus pad;
    auto& queue = client->GetPadQueue();
    for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) {
        while (queue[pad_number].Pop(pad)) {
            if (pad.touch == CemuhookUDP::PadTouch::Undefined) {
                continue;
            }
            params.Set("engine", "cemuhookudp");
            params.Set("ip", "127.0.0.1");
            params.Set("port", 26760);
            params.Set("pad_index", static_cast<int>(pad_number));
            params.Set("touch", static_cast<u16>(pad.touch));
            return params;
        }
    }
    return params;
}

} // namespace InputCommon