2020-09-05 04:35:42 +02:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
2019-08-24 15:57:49 +02:00
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
#include <atomic>
|
|
|
|
#include <list>
|
2020-02-03 15:10:59 +01:00
|
|
|
#include <mutex>
|
2020-09-05 04:35:42 +02:00
|
|
|
#include <utility>
|
|
|
|
#include "common/assert.h"
|
|
|
|
#include "common/threadsafe_queue.h"
|
2019-08-24 15:57:49 +02:00
|
|
|
#include "input_common/udp/client.h"
|
|
|
|
#include "input_common/udp/udp.h"
|
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
namespace InputCommon {
|
2019-08-24 15:57:49 +02:00
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
class UDPMotion final : public Input::MotionDevice {
|
2019-08-24 15:57:49 +02:00
|
|
|
public:
|
2020-09-05 04:35:42 +02:00
|
|
|
UDPMotion(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_)
|
|
|
|
: ip(ip_), port(port_), pad(pad_), client(client_) {}
|
2019-08-24 15:57:49 +02:00
|
|
|
|
2020-09-03 02:59:34 +02:00
|
|
|
Input::MotionStatus GetStatus() const override {
|
2020-09-05 04:35:42 +02:00
|
|
|
return client->GetPadState(pad).motion_status;
|
2019-08-24 15:57:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-05 04:35:42 +02:00
|
|
|
const std::string ip;
|
|
|
|
const int port;
|
|
|
|
const int pad;
|
|
|
|
CemuhookUDP::Client* client;
|
|
|
|
mutable std::mutex mutex;
|
2019-08-24 15:57:49 +02:00
|
|
|
};
|
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
/// 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;
|
2019-08-24 15:57:49 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-05 04:35:42 +02:00
|
|
|
return params;
|
|
|
|
}
|
2019-08-24 15:57:49 +02:00
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
class UDPTouch final : public Input::TouchDevice {
|
2019-08-24 15:57:49 +02:00
|
|
|
public:
|
2020-09-05 04:35:42 +02:00
|
|
|
UDPTouch(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_)
|
|
|
|
: ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {}
|
2019-08-24 15:57:49 +02:00
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
std::tuple<float, float, bool> GetStatus() const override {
|
|
|
|
return client->GetPadState(pad).touch_status;
|
2019-08-24 15:57:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-09-05 04:35:42 +02:00
|
|
|
const std::string ip;
|
|
|
|
const int port;
|
|
|
|
const int pad;
|
|
|
|
CemuhookUDP::Client* client;
|
|
|
|
mutable std::mutex mutex;
|
2019-08-24 15:57:49 +02:00
|
|
|
};
|
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
/// 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());
|
2019-08-24 15:57:49 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
void UDPTouchFactory::BeginConfiguration() {
|
|
|
|
polling = true;
|
|
|
|
client->BeginConfiguration();
|
2019-08-24 15:57:49 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
void UDPTouchFactory::EndConfiguration() {
|
|
|
|
polling = false;
|
|
|
|
client->EndConfiguration();
|
2020-07-22 16:39:53 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
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;
|
2019-08-24 15:57:49 +02:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:35:42 +02:00
|
|
|
} // namespace InputCommon
|