2020-06-21 21:31:57 +02:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2020-06-21 18:36:28 +02:00
|
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
2020-06-21 21:31:57 +02:00
|
|
|
#include <mutex>
|
2020-06-22 02:32:43 +02:00
|
|
|
#include <thread>
|
2020-07-07 12:01:08 +02:00
|
|
|
#include <unordered_map>
|
2020-06-21 18:36:28 +02:00
|
|
|
#include "common/common_types.h"
|
2020-06-22 00:43:01 +02:00
|
|
|
#include "common/threadsafe_queue.h"
|
2020-09-02 01:17:59 +02:00
|
|
|
#include "input_common/main.h"
|
2020-06-22 00:43:01 +02:00
|
|
|
|
2020-07-13 20:48:19 +02:00
|
|
|
struct libusb_context;
|
|
|
|
struct libusb_device;
|
|
|
|
struct libusb_device_handle;
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
namespace GCAdapter {
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-24 17:39:30 +02:00
|
|
|
enum class PadButton {
|
2020-06-21 18:36:28 +02:00
|
|
|
PAD_BUTTON_LEFT = 0x0001,
|
|
|
|
PAD_BUTTON_RIGHT = 0x0002,
|
|
|
|
PAD_BUTTON_DOWN = 0x0004,
|
|
|
|
PAD_BUTTON_UP = 0x0008,
|
|
|
|
PAD_TRIGGER_Z = 0x0010,
|
|
|
|
PAD_TRIGGER_R = 0x0020,
|
|
|
|
PAD_TRIGGER_L = 0x0040,
|
|
|
|
PAD_BUTTON_A = 0x0100,
|
|
|
|
PAD_BUTTON_B = 0x0200,
|
|
|
|
PAD_BUTTON_X = 0x0400,
|
|
|
|
PAD_BUTTON_Y = 0x0800,
|
|
|
|
PAD_BUTTON_START = 0x1000,
|
|
|
|
// Below is for compatibility with "AxisButton" type
|
|
|
|
PAD_STICK = 0x2000,
|
|
|
|
};
|
|
|
|
|
2020-07-03 17:52:07 +02:00
|
|
|
extern const std::array<PadButton, 12> PadButtonArray;
|
2020-06-23 23:37:15 +02:00
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
enum class PadAxes : u8 {
|
|
|
|
StickX,
|
|
|
|
StickY,
|
|
|
|
SubstickX,
|
|
|
|
SubstickY,
|
|
|
|
TriggerLeft,
|
|
|
|
TriggerRight,
|
|
|
|
Undefined,
|
|
|
|
};
|
2020-06-21 18:36:28 +02:00
|
|
|
|
|
|
|
struct GCPadStatus {
|
2020-07-16 19:00:04 +02:00
|
|
|
u16 button{}; // Or-ed PAD_BUTTON_* and PAD_TRIGGER_* bits
|
|
|
|
|
2020-07-19 17:49:26 +02:00
|
|
|
std::array<u8, 6> axis_values{}; // Triggers and sticks, following indices defined in PadAxes
|
|
|
|
static constexpr u8 THRESHOLD = 50; // Threshold for axis press for polling
|
2020-07-04 06:40:48 +02:00
|
|
|
|
2020-06-24 17:39:30 +02:00
|
|
|
u8 port{};
|
|
|
|
PadAxes axis{PadAxes::Undefined};
|
|
|
|
u8 axis_value{255};
|
2020-06-21 18:36:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct GCState {
|
|
|
|
std::unordered_map<int, bool> buttons;
|
|
|
|
std::unordered_map<int, u16> axes;
|
|
|
|
};
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
enum class ControllerTypes { None, Wired, Wireless };
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
class Adapter {
|
|
|
|
public:
|
2020-06-22 03:50:58 +02:00
|
|
|
/// Initialize the GC Adapter capture and read sequence
|
|
|
|
Adapter();
|
2020-06-22 00:43:01 +02:00
|
|
|
|
2020-06-22 03:50:58 +02:00
|
|
|
/// Close the adapter read thread and release the adapter
|
|
|
|
~Adapter();
|
2020-06-22 00:43:01 +02:00
|
|
|
/// Used for polling
|
|
|
|
void BeginConfiguration();
|
|
|
|
void EndConfiguration();
|
|
|
|
|
2020-09-02 01:17:59 +02:00
|
|
|
std::vector<Common::ParamPackage> GetInputDevices() const;
|
|
|
|
InputCommon::ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) const;
|
|
|
|
InputCommon::AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) const;
|
|
|
|
|
2020-07-14 17:23:10 +02:00
|
|
|
/// Returns true if there is a device connected to port
|
2020-09-07 08:45:04 +02:00
|
|
|
bool DeviceConnected(std::size_t port) const;
|
2020-07-14 17:23:10 +02:00
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue();
|
2020-06-22 03:50:58 +02:00
|
|
|
const std::array<Common::SPSCQueue<GCPadStatus>, 4>& GetPadQueue() const;
|
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
std::array<GCState, 4>& GetPadState();
|
2020-06-22 03:50:58 +02:00
|
|
|
const std::array<GCState, 4>& GetPadState() const;
|
2020-06-22 00:43:01 +02:00
|
|
|
|
2020-07-07 18:20:59 +02:00
|
|
|
int GetOriginValue(int port, int axis) const;
|
2020-07-07 03:58:31 +02:00
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
private:
|
2020-07-12 21:36:27 +02:00
|
|
|
GCPadStatus GetPadStatus(std::size_t port, const std::array<u8, 37>& adapter_payload);
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-23 00:11:59 +02:00
|
|
|
void PadToState(const GCPadStatus& pad, GCState& state);
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
void Read();
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
/// Resets status of device connected to port
|
2020-07-12 21:36:27 +02:00
|
|
|
void ResetDeviceType(std::size_t port);
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
/// Returns true if we successfully gain access to GC Adapter
|
|
|
|
bool CheckDeviceAccess(libusb_device* device);
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
/// Captures GC Adapter endpoint address,
|
|
|
|
void GetGCEndpoint(libusb_device* device);
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
/// For shutting down, clear all data, join all threads, release usb
|
|
|
|
void Reset();
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
/// For use in initialization, querying devices to find the adapter
|
|
|
|
void Setup();
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-22 00:43:01 +02:00
|
|
|
libusb_device_handle* usb_adapter_handle = nullptr;
|
|
|
|
|
|
|
|
std::thread adapter_input_thread;
|
|
|
|
bool adapter_thread_running;
|
|
|
|
|
|
|
|
libusb_context* libusb_ctx;
|
|
|
|
|
|
|
|
u8 input_endpoint = 0;
|
2020-07-01 18:52:50 +02:00
|
|
|
u8 output_endpoint = 0;
|
2020-06-22 00:43:01 +02:00
|
|
|
|
|
|
|
bool configuring = false;
|
|
|
|
|
|
|
|
std::array<GCState, 4> state;
|
2020-07-07 03:58:31 +02:00
|
|
|
std::array<bool, 4> get_origin;
|
|
|
|
std::array<GCPadStatus, 4> origin_status;
|
2020-07-16 19:00:04 +02:00
|
|
|
std::array<Common::SPSCQueue<GCPadStatus>, 4> pad_queue;
|
|
|
|
std::array<ControllerTypes, 4> adapter_controllers_status{};
|
2020-06-22 00:43:01 +02:00
|
|
|
};
|
2020-06-21 18:36:28 +02:00
|
|
|
|
2020-06-23 00:11:59 +02:00
|
|
|
} // namespace GCAdapter
|