2020-06-21 19:31:57 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-06-21 16:36:28 +00:00
|
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
2020-06-21 19:31:57 +00:00
|
|
|
#include <mutex>
|
2020-06-22 00:32:43 +00:00
|
|
|
#include <thread>
|
2020-07-07 10:01:08 +00:00
|
|
|
#include <unordered_map>
|
2020-06-21 16:36:28 +00:00
|
|
|
#include "common/common_types.h"
|
2020-06-21 22:43:01 +00:00
|
|
|
#include "common/threadsafe_queue.h"
|
2020-09-01 23:17:59 +00:00
|
|
|
#include "input_common/main.h"
|
2020-06-21 22:43:01 +00:00
|
|
|
|
2020-07-13 18:48:19 +00:00
|
|
|
struct libusb_context;
|
|
|
|
struct libusb_device;
|
|
|
|
struct libusb_device_handle;
|
|
|
|
|
2020-06-21 22:43:01 +00:00
|
|
|
namespace GCAdapter {
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2020-06-24 15:39:30 +00:00
|
|
|
enum class PadButton {
|
2020-10-12 23:11:22 +00:00
|
|
|
Undefined = 0x0000,
|
|
|
|
ButtonLeft = 0x0001,
|
|
|
|
ButtonRight = 0x0002,
|
|
|
|
ButtonDown = 0x0004,
|
|
|
|
ButtonUp = 0x0008,
|
|
|
|
TriggerZ = 0x0010,
|
|
|
|
TriggerR = 0x0020,
|
|
|
|
TriggerL = 0x0040,
|
|
|
|
ButtonA = 0x0100,
|
|
|
|
ButtonB = 0x0200,
|
|
|
|
ButtonX = 0x0400,
|
|
|
|
ButtonY = 0x0800,
|
|
|
|
ButtonStart = 0x1000,
|
2020-06-21 16:36:28 +00:00
|
|
|
// Below is for compatibility with "AxisButton" type
|
2020-10-12 23:11:22 +00:00
|
|
|
Stick = 0x2000,
|
2020-06-21 16:36:28 +00:00
|
|
|
};
|
|
|
|
|
2020-06-21 22:43:01 +00:00
|
|
|
enum class PadAxes : u8 {
|
|
|
|
StickX,
|
|
|
|
StickY,
|
|
|
|
SubstickX,
|
|
|
|
SubstickY,
|
|
|
|
TriggerLeft,
|
|
|
|
TriggerRight,
|
|
|
|
Undefined,
|
|
|
|
};
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
enum class ControllerTypes {
|
|
|
|
None,
|
|
|
|
Wired,
|
|
|
|
Wireless,
|
|
|
|
};
|
|
|
|
|
2020-06-21 16:36:28 +00:00
|
|
|
struct GCPadStatus {
|
2020-10-12 23:11:22 +00:00
|
|
|
std::size_t port{};
|
2020-07-16 17:00:04 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
PadButton button{PadButton::Undefined}; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits
|
2020-07-04 04:40:48 +00:00
|
|
|
|
2020-06-24 15:39:30 +00:00
|
|
|
PadAxes axis{PadAxes::Undefined};
|
2020-10-12 23:11:22 +00:00
|
|
|
s16 axis_value{};
|
|
|
|
u8 axis_threshold{50};
|
2020-06-21 16:36:28 +00:00
|
|
|
};
|
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
struct GCController {
|
|
|
|
ControllerTypes type{};
|
|
|
|
bool enable_vibration{};
|
|
|
|
u8 rumble_amplitude{};
|
|
|
|
u16 buttons{};
|
|
|
|
PadButton last_button{};
|
|
|
|
std::array<s16, 6> axis_values{};
|
|
|
|
std::array<u8, 6> axis_origin{};
|
2020-06-21 16:36:28 +00:00
|
|
|
};
|
|
|
|
|
2020-06-21 22:43:01 +00:00
|
|
|
class Adapter {
|
|
|
|
public:
|
2020-06-22 01:50:58 +00:00
|
|
|
Adapter();
|
|
|
|
~Adapter();
|
2020-10-12 23:11:22 +00:00
|
|
|
|
2020-10-20 17:55:25 +00:00
|
|
|
/// Request a vibration for a controller
|
|
|
|
bool RumblePlay(std::size_t port, u8 amplitude);
|
2020-10-12 23:11:22 +00:00
|
|
|
|
2020-06-21 22:43:01 +00:00
|
|
|
/// Used for polling
|
|
|
|
void BeginConfiguration();
|
|
|
|
void EndConfiguration();
|
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
Common::SPSCQueue<GCPadStatus>& GetPadQueue();
|
|
|
|
const Common::SPSCQueue<GCPadStatus>& GetPadQueue() const;
|
|
|
|
|
|
|
|
GCController& GetPadState(std::size_t port);
|
|
|
|
const GCController& GetPadState(std::size_t port) const;
|
|
|
|
|
|
|
|
/// Returns true if there is a device connected to port
|
|
|
|
bool DeviceConnected(std::size_t port) const;
|
|
|
|
|
|
|
|
/// Used for automapping features
|
2020-09-01 23:17:59 +00:00
|
|
|
std::vector<Common::ParamPackage> GetInputDevices() const;
|
|
|
|
InputCommon::ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) const;
|
|
|
|
InputCommon::AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) const;
|
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
private:
|
|
|
|
using AdapterPayload = std::array<u8, 37>;
|
2020-07-14 15:23:10 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
void UpdatePadType(std::size_t port, ControllerTypes pad_type);
|
|
|
|
void UpdateControllers(const AdapterPayload& adapter_payload);
|
|
|
|
void UpdateYuzuSettings(std::size_t port);
|
|
|
|
void UpdateStateButtons(std::size_t port, u8 b1, u8 b2);
|
|
|
|
void UpdateStateAxes(std::size_t port, const AdapterPayload& adapter_payload);
|
|
|
|
void UpdateVibrations();
|
2020-06-22 01:50:58 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
void AdapterInputThread();
|
2020-06-21 22:43:01 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
void AdapterScanThread();
|
2020-07-07 01:58:31 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
bool IsPayloadCorrect(const AdapterPayload& adapter_payload, s32 payload_size);
|
|
|
|
|
|
|
|
// Updates vibration state of all controllers
|
|
|
|
void SendVibrations();
|
|
|
|
|
|
|
|
/// For use in initialization, querying devices to find the adapter
|
|
|
|
void Setup();
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2021-01-02 14:00:05 +00:00
|
|
|
/// Resets status of all GC controller devices to a disconnected state
|
2020-10-12 23:11:22 +00:00
|
|
|
void ResetDevices();
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2021-01-02 14:00:05 +00:00
|
|
|
/// Resets status of device connected to a disconnected state
|
2020-10-12 23:11:22 +00:00
|
|
|
void ResetDevice(std::size_t port);
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2020-06-21 22:43:01 +00:00
|
|
|
/// Returns true if we successfully gain access to GC Adapter
|
2020-10-12 23:11:22 +00:00
|
|
|
bool CheckDeviceAccess();
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
/// Captures GC Adapter endpoint address
|
2021-01-02 14:00:05 +00:00
|
|
|
/// Returns true if the endpoint was set correctly
|
2020-10-12 23:11:22 +00:00
|
|
|
bool GetGCEndpoint(libusb_device* device);
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2020-06-21 22:43:01 +00:00
|
|
|
/// For shutting down, clear all data, join all threads, release usb
|
|
|
|
void Reset();
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
// Join all threads
|
|
|
|
void JoinThreads();
|
|
|
|
|
|
|
|
// Release usb handles
|
|
|
|
void ClearLibusbHandle();
|
2020-06-21 16:36:28 +00:00
|
|
|
|
2020-06-21 22:43:01 +00:00
|
|
|
libusb_device_handle* usb_adapter_handle = nullptr;
|
2020-10-12 23:11:22 +00:00
|
|
|
std::array<GCController, 4> pads;
|
|
|
|
Common::SPSCQueue<GCPadStatus> pad_queue;
|
2020-06-21 22:43:01 +00:00
|
|
|
|
|
|
|
std::thread adapter_input_thread;
|
2020-10-12 23:11:22 +00:00
|
|
|
std::thread adapter_scan_thread;
|
|
|
|
bool adapter_input_thread_running;
|
|
|
|
bool adapter_scan_thread_running;
|
|
|
|
bool restart_scan_thread;
|
2020-06-21 22:43:01 +00:00
|
|
|
|
|
|
|
libusb_context* libusb_ctx;
|
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
u8 input_endpoint{0};
|
|
|
|
u8 output_endpoint{0};
|
|
|
|
u8 input_error_counter{0};
|
|
|
|
u8 output_error_counter{0};
|
|
|
|
int vibration_counter{0};
|
2020-06-21 22:43:01 +00:00
|
|
|
|
2020-10-12 23:11:22 +00:00
|
|
|
bool configuring{false};
|
|
|
|
bool rumble_enabled{true};
|
|
|
|
bool vibration_changed{true};
|
2020-06-21 22:43:01 +00:00
|
|
|
};
|
2020-06-22 22:11:59 +00:00
|
|
|
} // namespace GCAdapter
|