mirror of https://github.com/yuzu-mirror/yuzu
				
				
				
			Merge pull request #4594 from german77/MotionHID
hid/configuration: Implement motion controls to HIDpull/8/head
						commit
						3f6d83b27c
					
				@ -1,105 +1,144 @@
 | 
				
			|||||||
// Copyright 2018 Citra Emulator Project
 | 
					// Copyright 2020 yuzu Emulator Project
 | 
				
			||||||
// Licensed under GPLv2 or any later version
 | 
					// Licensed under GPLv2 or any later version
 | 
				
			||||||
// Refer to the license.txt file included.
 | 
					// Refer to the license.txt file included.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#include <atomic>
 | 
				
			||||||
 | 
					#include <list>
 | 
				
			||||||
#include <mutex>
 | 
					#include <mutex>
 | 
				
			||||||
#include <optional>
 | 
					#include <utility>
 | 
				
			||||||
#include <tuple>
 | 
					#include "common/assert.h"
 | 
				
			||||||
 | 
					#include "common/threadsafe_queue.h"
 | 
				
			||||||
#include "common/param_package.h"
 | 
					 | 
				
			||||||
#include "core/frontend/input.h"
 | 
					 | 
				
			||||||
#include "core/settings.h"
 | 
					 | 
				
			||||||
#include "input_common/udp/client.h"
 | 
					#include "input_common/udp/client.h"
 | 
				
			||||||
#include "input_common/udp/udp.h"
 | 
					#include "input_common/udp/udp.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace InputCommon::CemuhookUDP {
 | 
					namespace InputCommon {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UDPTouchDevice final : public Input::TouchDevice {
 | 
					class UDPMotion final : public Input::MotionDevice {
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    explicit UDPTouchDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {}
 | 
					    UDPMotion(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_)
 | 
				
			||||||
    std::tuple<float, float, bool> GetStatus() const override {
 | 
					        : ip(ip_), port(port_), pad(pad_), client(client_) {}
 | 
				
			||||||
        std::lock_guard guard(status->update_mutex);
 | 
					
 | 
				
			||||||
        return status->touch_status;
 | 
					    Input::MotionStatus GetStatus() const override {
 | 
				
			||||||
 | 
					        return client->GetPadState(pad).motion_status;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    std::shared_ptr<DeviceStatus> status;
 | 
					    const std::string ip;
 | 
				
			||||||
 | 
					    const int port;
 | 
				
			||||||
 | 
					    const int pad;
 | 
				
			||||||
 | 
					    CemuhookUDP::Client* client;
 | 
				
			||||||
 | 
					    mutable std::mutex mutex;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UDPMotionDevice final : public Input::MotionDevice {
 | 
					/// A motion device factory that creates motion devices from JC Adapter
 | 
				
			||||||
public:
 | 
					UDPMotionFactory::UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_)
 | 
				
			||||||
    explicit UDPMotionDevice(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {}
 | 
					    : client(std::move(client_)) {}
 | 
				
			||||||
    std::tuple<Common::Vec3<float>, Common::Vec3<float>> GetStatus() const override {
 | 
					
 | 
				
			||||||
        std::lock_guard guard(status->update_mutex);
 | 
					/**
 | 
				
			||||||
        return status->motion_status;
 | 
					 * 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());
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					void UDPMotionFactory::BeginConfiguration() {
 | 
				
			||||||
    std::shared_ptr<DeviceStatus> status;
 | 
					    polling = true;
 | 
				
			||||||
};
 | 
					    client->BeginConfiguration();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> {
 | 
					void UDPMotionFactory::EndConfiguration() {
 | 
				
			||||||
public:
 | 
					    polling = false;
 | 
				
			||||||
    explicit UDPTouchFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {}
 | 
					    client->EndConfiguration();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
    std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override {
 | 
					
 | 
				
			||||||
        {
 | 
					Common::ParamPackage UDPMotionFactory::GetNextInput() {
 | 
				
			||||||
            std::lock_guard guard(status->update_mutex);
 | 
					    Common::ParamPackage params;
 | 
				
			||||||
            status->touch_calibration = DeviceStatus::CalibrationData{};
 | 
					    CemuhookUDP::UDPPadStatus pad;
 | 
				
			||||||
            // These default values work well for DS4 but probably not other touch inputs
 | 
					    auto& queue = client->GetPadQueue();
 | 
				
			||||||
            status->touch_calibration->min_x = params.Get("min_x", 100);
 | 
					    for (std::size_t pad_number = 0; pad_number < queue.size(); ++pad_number) {
 | 
				
			||||||
            status->touch_calibration->min_y = params.Get("min_y", 50);
 | 
					        while (queue[pad_number].Pop(pad)) {
 | 
				
			||||||
            status->touch_calibration->max_x = params.Get("max_x", 1800);
 | 
					            if (pad.motion == CemuhookUDP::PadMotion::Undefined || std::abs(pad.motion_value) < 1) {
 | 
				
			||||||
            status->touch_calibration->max_y = params.Get("max_y", 850);
 | 
					                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 std::make_unique<UDPTouchDevice>(status);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    return params;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					class UDPTouch final : public Input::TouchDevice {
 | 
				
			||||||
    std::shared_ptr<DeviceStatus> status;
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> {
 | 
					 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    explicit UDPMotionFactory(std::shared_ptr<DeviceStatus> status_) : status(std::move(status_)) {}
 | 
					    UDPTouch(std::string ip_, int port_, int pad_, CemuhookUDP::Client* client_)
 | 
				
			||||||
 | 
					        : ip(std::move(ip_)), port(port_), pad(pad_), client(client_) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override {
 | 
					    std::tuple<float, float, bool> GetStatus() const override {
 | 
				
			||||||
        return std::make_unique<UDPMotionDevice>(status);
 | 
					        return client->GetPadState(pad).touch_status;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    std::shared_ptr<DeviceStatus> status;
 | 
					    const std::string ip;
 | 
				
			||||||
 | 
					    const int port;
 | 
				
			||||||
 | 
					    const int pad;
 | 
				
			||||||
 | 
					    CemuhookUDP::Client* client;
 | 
				
			||||||
 | 
					    mutable std::mutex mutex;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
State::State() {
 | 
					/// A motion device factory that creates motion devices from JC Adapter
 | 
				
			||||||
    auto status = std::make_shared<DeviceStatus>();
 | 
					UDPTouchFactory::UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_)
 | 
				
			||||||
    client =
 | 
					    : client(std::move(client_)) {}
 | 
				
			||||||
        std::make_unique<Client>(status, Settings::values.udp_input_address,
 | 
					
 | 
				
			||||||
                                 Settings::values.udp_input_port, Settings::values.udp_pad_index);
 | 
					/**
 | 
				
			||||||
 | 
					 * Creates motion device
 | 
				
			||||||
    motion_factory = std::make_shared<UDPMotionFactory>(status);
 | 
					 * @param params contains parameters for creating the device:
 | 
				
			||||||
    touch_factory = std::make_shared<UDPTouchFactory>(status);
 | 
					 *     - "port": the nth jcpad on the adapter
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
    Input::RegisterFactory<Input::MotionDevice>("cemuhookudp", motion_factory);
 | 
					std::unique_ptr<Input::TouchDevice> UDPTouchFactory::Create(const Common::ParamPackage& params) {
 | 
				
			||||||
    Input::RegisterFactory<Input::TouchDevice>("cemuhookudp", touch_factory);
 | 
					    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());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
State::~State() {
 | 
					void UDPTouchFactory::BeginConfiguration() {
 | 
				
			||||||
    Input::UnregisterFactory<Input::TouchDevice>("cemuhookudp");
 | 
					    polling = true;
 | 
				
			||||||
    Input::UnregisterFactory<Input::MotionDevice>("cemuhookudp");
 | 
					    client->BeginConfiguration();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
std::vector<Common::ParamPackage> State::GetInputDevices() const {
 | 
					void UDPTouchFactory::EndConfiguration() {
 | 
				
			||||||
    // TODO support binding udp devices
 | 
					    polling = false;
 | 
				
			||||||
    return {};
 | 
					    client->EndConfiguration();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void State::ReloadUDPClient() {
 | 
					Common::ParamPackage UDPTouchFactory::GetNextInput() {
 | 
				
			||||||
    client->ReloadSocket(Settings::values.udp_input_address, Settings::values.udp_input_port,
 | 
					    Common::ParamPackage params;
 | 
				
			||||||
                         Settings::values.udp_pad_index);
 | 
					    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;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
std::unique_ptr<State> Init() {
 | 
					} // namespace InputCommon
 | 
				
			||||||
    return std::make_unique<State>();
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
} // namespace InputCommon::CemuhookUDP
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -1,32 +1,57 @@
 | 
				
			|||||||
// Copyright 2018 Citra Emulator Project
 | 
					// Copyright 2020 yuzu Emulator Project
 | 
				
			||||||
// Licensed under GPLv2 or any later version
 | 
					// Licensed under GPLv2 or any later version
 | 
				
			||||||
// Refer to the license.txt file included.
 | 
					// Refer to the license.txt file included.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <memory>
 | 
					#include <memory>
 | 
				
			||||||
#include <vector>
 | 
					#include "core/frontend/input.h"
 | 
				
			||||||
#include "common/param_package.h"
 | 
					#include "input_common/udp/client.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace InputCommon::CemuhookUDP {
 | 
					namespace InputCommon {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Client;
 | 
					/// A motion device factory that creates motion devices from udp clients
 | 
				
			||||||
class UDPMotionFactory;
 | 
					class UDPMotionFactory final : public Input::Factory<Input::MotionDevice> {
 | 
				
			||||||
class UDPTouchFactory;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
class State {
 | 
					 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
    State();
 | 
					    explicit UDPMotionFactory(std::shared_ptr<CemuhookUDP::Client> client_);
 | 
				
			||||||
    ~State();
 | 
					
 | 
				
			||||||
    void ReloadUDPClient();
 | 
					    std::unique_ptr<Input::MotionDevice> Create(const Common::ParamPackage& params) override;
 | 
				
			||||||
    std::vector<Common::ParamPackage> GetInputDevices() const;
 | 
					
 | 
				
			||||||
 | 
					    Common::ParamPackage GetNextInput();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// For device input configuration/polling
 | 
				
			||||||
 | 
					    void BeginConfiguration();
 | 
				
			||||||
 | 
					    void EndConfiguration();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    bool IsPolling() const {
 | 
				
			||||||
 | 
					        return polling;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
private:
 | 
					private:
 | 
				
			||||||
    std::unique_ptr<Client> client;
 | 
					    std::shared_ptr<CemuhookUDP::Client> client;
 | 
				
			||||||
    std::shared_ptr<UDPMotionFactory> motion_factory;
 | 
					    bool polling = false;
 | 
				
			||||||
    std::shared_ptr<UDPTouchFactory> touch_factory;
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
std::unique_ptr<State> Init();
 | 
					/// A touch device factory that creates touch devices from udp clients
 | 
				
			||||||
 | 
					class UDPTouchFactory final : public Input::Factory<Input::TouchDevice> {
 | 
				
			||||||
 | 
					public:
 | 
				
			||||||
 | 
					    explicit UDPTouchFactory(std::shared_ptr<CemuhookUDP::Client> client_);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    std::unique_ptr<Input::TouchDevice> Create(const Common::ParamPackage& params) override;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    Common::ParamPackage GetNextInput();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// For device input configuration/polling
 | 
				
			||||||
 | 
					    void BeginConfiguration();
 | 
				
			||||||
 | 
					    void EndConfiguration();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    bool IsPolling() const {
 | 
				
			||||||
 | 
					        return polling;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private:
 | 
				
			||||||
 | 
					    std::shared_ptr<CemuhookUDP::Client> client;
 | 
				
			||||||
 | 
					    bool polling = false;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
} // namespace InputCommon::CemuhookUDP
 | 
					} // namespace InputCommon
 | 
				
			||||||
 | 
				
			|||||||
					Loading…
					
					
				
		Reference in New Issue