2015-05-24 04:55:12 +00:00
|
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-12-10 09:45:45 +00:00
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#include "InputCommon/GCAdapter.h"
|
|
|
|
|
|
|
|
|
|
#ifndef ANDROID
|
|
|
|
|
#define GCADAPTER_USE_LIBUSB_IMPLEMENTATION true
|
|
|
|
|
#define GCADAPTER_USE_ANDROID_IMPLEMENTATION false
|
|
|
|
|
#else
|
|
|
|
|
#define GCADAPTER_USE_LIBUSB_IMPLEMENTATION false
|
|
|
|
|
#define GCADAPTER_USE_ANDROID_IMPLEMENTATION true
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-11-08 17:13:58 +00:00
|
|
|
|
#include <algorithm>
|
2022-01-05 02:25:19 +00:00
|
|
|
|
#include <array>
|
2015-05-27 03:44:51 +00:00
|
|
|
|
#include <mutex>
|
2022-01-05 02:25:19 +00:00
|
|
|
|
#include <optional>
|
2014-12-10 09:45:45 +00:00
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
|
|
|
|
#include <libusb.h>
|
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
#include <jni.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
#include "Common/BitUtils.h"
|
2017-07-22 18:25:11 +00:00
|
|
|
|
#include "Common/Event.h"
|
2015-05-08 21:28:03 +00:00
|
|
|
|
#include "Common/Flag.h"
|
2015-12-31 17:09:47 +00:00
|
|
|
|
#include "Common/Logging/Log.h"
|
2015-05-08 21:28:03 +00:00
|
|
|
|
#include "Common/Thread.h"
|
2022-01-05 02:25:19 +00:00
|
|
|
|
#include "Core/Config/MainSettings.h"
|
2014-12-23 23:49:53 +00:00
|
|
|
|
#include "Core/Core.h"
|
|
|
|
|
#include "Core/CoreTiming.h"
|
2017-01-20 23:45:11 +00:00
|
|
|
|
#include "Core/HW/SI/SI.h"
|
2019-06-07 22:25:32 +00:00
|
|
|
|
#include "Core/HW/SI/SI_Device.h"
|
2014-12-23 23:49:53 +00:00
|
|
|
|
#include "Core/HW/SystemTimers.h"
|
2022-11-26 08:29:46 +00:00
|
|
|
|
#include "Core/System.h"
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#include "InputCommon/GCPadStatus.h"
|
|
|
|
|
|
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
|
|
|
|
#include "Common/ScopeGuard.h"
|
2019-05-12 18:19:31 +00:00
|
|
|
|
#include "Core/LibusbUtils.h"
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
#include "jni/AndroidCommon/IDCache.h"
|
|
|
|
|
#endif
|
2015-12-31 17:09:47 +00:00
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-04-22 14:36:56 +00:00
|
|
|
|
#if defined(LIBUSB_API_VERSION)
|
|
|
|
|
#define LIBUSB_API_VERSION_EXIST 1
|
|
|
|
|
#else
|
|
|
|
|
#define LIBUSB_API_VERSION_EXIST 0
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define LIBUSB_API_VERSION_ATLEAST(v) (LIBUSB_API_VERSION_EXIST && LIBUSB_API_VERSION >= (v))
|
|
|
|
|
#define LIBUSB_API_HAS_HOTPLUG LIBUSB_API_VERSION_ATLEAST(0x01000102)
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2022-04-22 14:36:56 +00:00
|
|
|
|
|
2015-12-31 17:09:47 +00:00
|
|
|
|
namespace GCAdapter
|
2014-12-10 09:45:45 +00:00
|
|
|
|
{
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
|
|
|
|
|
constexpr unsigned int USB_TIMEOUT_MS = 16;
|
|
|
|
|
|
2015-04-26 21:53:36 +00:00
|
|
|
|
static bool CheckDeviceAccess(libusb_device* device);
|
|
|
|
|
static void AddGCAdapter(libusb_device* device);
|
2016-04-30 11:29:11 +00:00
|
|
|
|
static void ResetRumbleLockNeeded();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2022-10-10 04:28:14 +00:00
|
|
|
|
|
2016-05-19 19:52:44 +00:00
|
|
|
|
static void Reset();
|
|
|
|
|
static void Setup();
|
2022-10-10 04:28:14 +00:00
|
|
|
|
static void ProcessInputPayload(const u8* data, std::size_t size);
|
|
|
|
|
static void ReadThreadFunc();
|
|
|
|
|
static void WriteThreadFunc();
|
2015-04-26 21:53:36 +00:00
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
enum class AdapterStatus
|
2018-07-17 20:31:18 +00:00
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
NotDetected,
|
|
|
|
|
Detected,
|
|
|
|
|
Error,
|
2018-07-17 20:31:18 +00:00
|
|
|
|
};
|
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
static std::atomic<AdapterStatus> s_status = AdapterStatus::NotDetected;
|
|
|
|
|
static std::atomic<libusb_error> s_adapter_error = LIBUSB_SUCCESS;
|
2014-12-11 00:15:36 +00:00
|
|
|
|
static libusb_device_handle* s_handle = nullptr;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
// Java classes
|
|
|
|
|
static jclass s_adapter_class;
|
|
|
|
|
|
|
|
|
|
static bool s_detected = false;
|
|
|
|
|
static int s_fd = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-04-19 22:21:23 +00:00
|
|
|
|
enum class ControllerType : u8
|
|
|
|
|
{
|
|
|
|
|
None = 0,
|
|
|
|
|
Wired = 1,
|
|
|
|
|
Wireless = 2,
|
|
|
|
|
};
|
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
static std::array<u8, SerialInterface::MAX_SI_CHANNELS> s_controller_rumble;
|
2014-12-10 09:45:45 +00:00
|
|
|
|
|
2022-04-19 21:24:06 +00:00
|
|
|
|
constexpr size_t CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE = 37;
|
|
|
|
|
constexpr size_t CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE = 1;
|
|
|
|
|
constexpr size_t CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE = 5;
|
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
struct PortState
|
|
|
|
|
{
|
|
|
|
|
GCPadStatus origin = {};
|
|
|
|
|
GCPadStatus status = {};
|
|
|
|
|
|
|
|
|
|
ControllerType controller_type = ControllerType::None;
|
|
|
|
|
bool is_new_connection = false;
|
|
|
|
|
};
|
2015-01-05 01:43:26 +00:00
|
|
|
|
|
2022-03-28 05:25:40 +00:00
|
|
|
|
// Only access with s_mutex held!
|
2022-10-10 04:28:14 +00:00
|
|
|
|
static std::array<PortState, SerialInterface::MAX_SI_CHANNELS> s_port_states;
|
2014-12-10 09:45:45 +00:00
|
|
|
|
|
2022-04-21 00:21:05 +00:00
|
|
|
|
static std::array<u8, CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE> s_controller_write_payload;
|
|
|
|
|
static std::atomic<int> s_controller_write_payload_size{0};
|
2014-12-10 09:45:45 +00:00
|
|
|
|
|
2022-04-21 00:21:05 +00:00
|
|
|
|
static std::thread s_read_adapter_thread;
|
2022-04-21 04:36:07 +00:00
|
|
|
|
static Common::Flag s_read_adapter_thread_running;
|
2022-04-21 00:21:05 +00:00
|
|
|
|
static std::thread s_write_adapter_thread;
|
2022-04-21 04:36:07 +00:00
|
|
|
|
static Common::Flag s_write_adapter_thread_running;
|
2022-04-21 00:21:05 +00:00
|
|
|
|
static Common::Event s_write_happened;
|
2017-07-22 18:25:11 +00:00
|
|
|
|
|
2022-04-21 19:03:58 +00:00
|
|
|
|
static std::mutex s_read_mutex;
|
2022-04-21 00:21:05 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2016-04-30 11:29:11 +00:00
|
|
|
|
static std::mutex s_init_mutex;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
static std::mutex s_write_mutex;
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-04-26 21:53:36 +00:00
|
|
|
|
static std::thread s_adapter_detect_thread;
|
|
|
|
|
static Common::Flag s_adapter_detect_thread_running;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2020-07-05 01:03:20 +00:00
|
|
|
|
static Common::Event s_hotplug_event;
|
2015-04-26 21:53:36 +00:00
|
|
|
|
|
|
|
|
|
static std::function<void(void)> s_detect_callback;
|
|
|
|
|
|
2016-12-27 06:50:36 +00:00
|
|
|
|
#if defined(__FreeBSD__) && __FreeBSD__ >= 11
|
|
|
|
|
static bool s_libusb_hotplug_enabled = true;
|
|
|
|
|
#else
|
2015-04-26 21:53:36 +00:00
|
|
|
|
static bool s_libusb_hotplug_enabled = false;
|
2016-12-27 06:50:36 +00:00
|
|
|
|
#endif
|
2022-04-22 14:36:56 +00:00
|
|
|
|
#if LIBUSB_API_HAS_HOTPLUG
|
2015-04-26 21:53:36 +00:00
|
|
|
|
static libusb_hotplug_callback_handle s_hotplug_handle;
|
2016-01-03 08:53:31 +00:00
|
|
|
|
#endif
|
2014-12-10 09:45:45 +00:00
|
|
|
|
|
2022-03-08 21:16:54 +00:00
|
|
|
|
static std::unique_ptr<LibusbUtils::Context> s_libusb_context;
|
2019-06-25 15:31:35 +00:00
|
|
|
|
|
2014-12-11 00:15:36 +00:00
|
|
|
|
static u8 s_endpoint_in = 0;
|
|
|
|
|
static u8 s_endpoint_out = 0;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2014-12-10 10:35:30 +00:00
|
|
|
|
|
2014-12-23 23:49:53 +00:00
|
|
|
|
static u64 s_last_init = 0;
|
|
|
|
|
|
2022-01-05 02:25:19 +00:00
|
|
|
|
static std::optional<size_t> s_config_callback_id = std::nullopt;
|
2022-10-10 04:28:14 +00:00
|
|
|
|
|
|
|
|
|
static bool s_is_adapter_wanted = false;
|
2022-01-05 02:25:19 +00:00
|
|
|
|
static std::array<bool, SerialInterface::MAX_SI_CHANNELS> s_config_rumble_enabled{};
|
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
static void ReadThreadFunc()
|
2015-01-05 01:43:26 +00:00
|
|
|
|
{
|
2022-04-22 14:12:09 +00:00
|
|
|
|
Common::SetCurrentThreadName("GCAdapter Read Thread");
|
2022-04-21 04:36:07 +00:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GCAdapter read thread started");
|
2022-04-22 14:12:09 +00:00
|
|
|
|
|
2022-04-21 19:03:58 +00:00
|
|
|
|
#if GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
2022-04-19 21:41:13 +00:00
|
|
|
|
bool first_read = true;
|
2022-06-02 20:19:32 +00:00
|
|
|
|
JNIEnv* const env = IDCache::GetEnvForThread();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
2022-06-02 20:19:32 +00:00
|
|
|
|
const jfieldID payload_field = env->GetStaticFieldID(s_adapter_class, "controller_payload", "[B");
|
2022-04-19 21:41:13 +00:00
|
|
|
|
jobject payload_object = env->GetStaticObjectField(s_adapter_class, payload_field);
|
2022-06-02 20:19:32 +00:00
|
|
|
|
auto* const java_controller_payload = reinterpret_cast<jbyteArray*>(&payload_object);
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
|
|
|
|
// Get function pointers
|
2022-06-02 20:19:32 +00:00
|
|
|
|
const jmethodID getfd_func = env->GetStaticMethodID(s_adapter_class, "GetFD", "()I");
|
|
|
|
|
const jmethodID input_func = env->GetStaticMethodID(s_adapter_class, "Input", "()I");
|
|
|
|
|
const jmethodID openadapter_func = env->GetStaticMethodID(s_adapter_class, "OpenAdapter", "()Z");
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
2022-06-02 20:19:32 +00:00
|
|
|
|
const bool connected = env->CallStaticBooleanMethod(s_adapter_class, openadapter_func);
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
2022-04-21 01:52:04 +00:00
|
|
|
|
if (!connected)
|
2022-04-19 21:41:13 +00:00
|
|
|
|
{
|
2022-04-21 01:52:04 +00:00
|
|
|
|
s_fd = 0;
|
|
|
|
|
s_detected = false;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
2022-04-21 01:52:04 +00:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter failed to open!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-04-21 04:36:07 +00:00
|
|
|
|
#endif
|
2022-04-21 01:52:04 +00:00
|
|
|
|
|
|
|
|
|
s_write_adapter_thread_running.Set(true);
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_write_adapter_thread = std::thread(WriteThreadFunc);
|
2022-04-21 01:52:04 +00:00
|
|
|
|
|
|
|
|
|
// Reset rumble once on initial reading
|
|
|
|
|
ResetRumble();
|
|
|
|
|
|
|
|
|
|
while (s_read_adapter_thread_running.IsSet())
|
|
|
|
|
{
|
2022-04-21 04:36:07 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
std::array<u8, CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE> input_buffer;
|
|
|
|
|
|
2022-04-21 19:03:58 +00:00
|
|
|
|
int payload_size = 0;
|
2022-06-02 20:56:17 +00:00
|
|
|
|
const int error =
|
2022-10-10 04:28:14 +00:00
|
|
|
|
libusb_interrupt_transfer(s_handle, s_endpoint_in, input_buffer.data(),
|
|
|
|
|
int(input_buffer.size()), &payload_size, USB_TIMEOUT_MS);
|
2022-06-02 20:56:17 +00:00
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Read: libusb_interrupt_transfer failed: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(error));
|
|
|
|
|
}
|
2022-10-10 04:28:14 +00:00
|
|
|
|
|
|
|
|
|
ProcessInputPayload(input_buffer.data(), payload_size);
|
|
|
|
|
|
2022-04-21 04:36:07 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
2022-06-02 20:19:32 +00:00
|
|
|
|
const int payload_size = env->CallStaticIntMethod(s_adapter_class, input_func);
|
|
|
|
|
jbyte* const java_data = env->GetByteArrayElements(*java_controller_payload, nullptr);
|
2022-10-10 04:28:14 +00:00
|
|
|
|
|
|
|
|
|
ProcessInputPayload(reinterpret_cast<const u8*>(java_data), payload_size);
|
|
|
|
|
|
2022-04-21 01:52:04 +00:00
|
|
|
|
env->ReleaseByteArrayElements(*java_controller_payload, java_data, 0);
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
2022-04-21 01:52:04 +00:00
|
|
|
|
if (first_read)
|
2022-04-19 21:41:13 +00:00
|
|
|
|
{
|
2022-04-21 01:52:04 +00:00
|
|
|
|
first_read = false;
|
|
|
|
|
s_fd = env->CallStaticIntMethod(s_adapter_class, getfd_func);
|
2022-04-19 21:41:13 +00:00
|
|
|
|
}
|
2022-04-21 04:36:07 +00:00
|
|
|
|
#endif
|
2022-04-21 01:52:04 +00:00
|
|
|
|
|
|
|
|
|
Common::YieldCPU();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Terminate the write thread on leaving
|
|
|
|
|
if (s_write_adapter_thread_running.TestAndClear())
|
|
|
|
|
{
|
|
|
|
|
s_controller_write_payload_size.store(0);
|
2022-10-10 04:28:14 +00:00
|
|
|
|
// Kick the waiting event
|
|
|
|
|
s_write_happened.Set();
|
2022-04-21 04:36:07 +00:00
|
|
|
|
s_write_adapter_thread.join();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 04:36:07 +00:00
|
|
|
|
#if GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
2022-04-19 21:41:13 +00:00
|
|
|
|
s_fd = 0;
|
|
|
|
|
s_detected = false;
|
|
|
|
|
#endif
|
2022-04-21 04:36:07 +00:00
|
|
|
|
|
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GCAdapter read thread stopped");
|
2014-12-10 09:45:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
static void WriteThreadFunc()
|
2017-07-22 18:25:11 +00:00
|
|
|
|
{
|
2022-04-22 14:12:09 +00:00
|
|
|
|
Common::SetCurrentThreadName("GCAdapter Write Thread");
|
2022-04-21 04:36:07 +00:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GCAdapter write thread started");
|
2022-04-22 14:12:09 +00:00
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2017-07-22 18:25:11 +00:00
|
|
|
|
int size = 0;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
2022-06-02 20:19:32 +00:00
|
|
|
|
JNIEnv* const env = IDCache::GetEnvForThread();
|
|
|
|
|
const jmethodID output_func = env->GetStaticMethodID(s_adapter_class, "Output", "([B)I");
|
2022-04-21 04:36:07 +00:00
|
|
|
|
#endif
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
|
|
|
|
while (s_write_adapter_thread_running.IsSet())
|
|
|
|
|
{
|
|
|
|
|
s_write_happened.Wait();
|
2022-04-21 04:36:07 +00:00
|
|
|
|
|
2022-06-02 20:19:32 +00:00
|
|
|
|
const int write_size = s_controller_write_payload_size.load();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
if (write_size)
|
|
|
|
|
{
|
2022-04-21 04:36:07 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
const int error =
|
|
|
|
|
libusb_interrupt_transfer(s_handle, s_endpoint_out, s_controller_write_payload.data(),
|
|
|
|
|
write_size, &size, USB_TIMEOUT_MS);
|
2022-06-02 20:56:17 +00:00
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
2022-04-21 04:36:07 +00:00
|
|
|
|
{
|
2022-06-02 20:56:17 +00:00
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Write: libusb_interrupt_transfer failed: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(error));
|
2022-04-21 04:36:07 +00:00
|
|
|
|
}
|
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
2022-06-02 20:19:32 +00:00
|
|
|
|
const jbyteArray jrumble_array = env->NewByteArray(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE);
|
|
|
|
|
jbyte* const jrumble = env->GetByteArrayElements(jrumble_array, nullptr);
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
std::lock_guard lk(s_write_mutex);
|
2022-04-19 21:24:06 +00:00
|
|
|
|
memcpy(jrumble, s_controller_write_payload.data(), write_size);
|
2022-04-19 21:41:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
env->ReleaseByteArrayElements(jrumble_array, jrumble, 0);
|
2022-04-21 01:42:47 +00:00
|
|
|
|
env->CallStaticIntMethod(s_adapter_class, output_func, jrumble_array);
|
2022-04-21 04:36:07 +00:00
|
|
|
|
#endif
|
2022-04-19 21:41:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Common::YieldCPU();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-21 04:36:07 +00:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GCAdapter write thread stopped");
|
2017-07-22 18:25:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-04-22 14:36:56 +00:00
|
|
|
|
#if LIBUSB_API_HAS_HOTPLUG
|
2015-04-26 21:53:36 +00:00
|
|
|
|
static int HotplugCallback(libusb_context* ctx, libusb_device* dev, libusb_hotplug_event event,
|
|
|
|
|
void* user_data)
|
|
|
|
|
{
|
|
|
|
|
if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED)
|
|
|
|
|
{
|
2020-07-05 01:03:20 +00:00
|
|
|
|
if (s_handle == nullptr)
|
|
|
|
|
s_hotplug_event.Set();
|
2015-04-26 21:53:36 +00:00
|
|
|
|
}
|
|
|
|
|
else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
|
|
|
|
|
{
|
|
|
|
|
if (s_handle != nullptr && libusb_get_device(s_handle) == dev)
|
|
|
|
|
Reset();
|
2018-07-17 20:31:18 +00:00
|
|
|
|
|
|
|
|
|
// Reset a potential error status now that the adapter is unplugged
|
2022-10-10 04:28:14 +00:00
|
|
|
|
if (s_status == AdapterStatus::Error)
|
2018-07-19 06:23:16 +00:00
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_status = AdapterStatus::NotDetected;
|
2018-07-19 06:23:16 +00:00
|
|
|
|
if (s_detect_callback != nullptr)
|
|
|
|
|
s_detect_callback();
|
|
|
|
|
}
|
2015-04-26 21:53:36 +00:00
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2016-01-03 08:53:31 +00:00
|
|
|
|
#endif
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2015-04-26 21:53:36 +00:00
|
|
|
|
|
|
|
|
|
static void ScanThreadFunc()
|
|
|
|
|
{
|
|
|
|
|
Common::SetCurrentThreadName("GC Adapter Scanning Thread");
|
2021-05-04 20:37:06 +00:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter scanning thread started");
|
2015-04-26 21:53:36 +00:00
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-04-22 14:36:56 +00:00
|
|
|
|
#if LIBUSB_API_HAS_HOTPLUG
|
2016-12-27 06:50:36 +00:00
|
|
|
|
#ifndef __FreeBSD__
|
2015-04-26 21:53:36 +00:00
|
|
|
|
s_libusb_hotplug_enabled = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 0;
|
2016-12-27 06:50:36 +00:00
|
|
|
|
#endif
|
2015-04-26 21:53:36 +00:00
|
|
|
|
if (s_libusb_hotplug_enabled)
|
|
|
|
|
{
|
2022-06-02 20:56:17 +00:00
|
|
|
|
const int error = libusb_hotplug_register_callback(
|
|
|
|
|
*s_libusb_context,
|
|
|
|
|
(libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
|
|
|
|
|
LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
|
|
|
|
|
LIBUSB_HOTPLUG_ENUMERATE, 0x057e, 0x0337, LIBUSB_HOTPLUG_MATCH_ANY, HotplugCallback,
|
|
|
|
|
nullptr, &s_hotplug_handle);
|
|
|
|
|
if (error == LIBUSB_SUCCESS)
|
|
|
|
|
{
|
2021-05-04 20:37:06 +00:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Using libUSB hotplug detection");
|
2022-06-02 20:56:17 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
s_libusb_hotplug_enabled = false;
|
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "Failed to add libUSB hotplug detection callback: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(error));
|
|
|
|
|
}
|
2015-04-26 21:53:36 +00:00
|
|
|
|
}
|
2016-01-03 08:53:31 +00:00
|
|
|
|
#endif
|
2015-04-26 21:53:36 +00:00
|
|
|
|
|
|
|
|
|
while (s_adapter_detect_thread_running.IsSet())
|
|
|
|
|
{
|
2019-05-12 18:19:31 +00:00
|
|
|
|
if (s_handle == nullptr)
|
2015-04-26 21:53:36 +00:00
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
std::lock_guard lk(s_init_mutex);
|
2019-05-12 18:19:31 +00:00
|
|
|
|
Setup();
|
2015-04-26 21:53:36 +00:00
|
|
|
|
}
|
2020-07-05 01:03:20 +00:00
|
|
|
|
|
|
|
|
|
if (s_libusb_hotplug_enabled)
|
|
|
|
|
s_hotplug_event.Wait();
|
|
|
|
|
else
|
|
|
|
|
Common::SleepCurrentThread(500);
|
2015-04-26 21:53:36 +00:00
|
|
|
|
}
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
2022-06-02 20:19:32 +00:00
|
|
|
|
JNIEnv* const env = IDCache::GetEnvForThread();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
2022-06-02 20:19:32 +00:00
|
|
|
|
const jmethodID queryadapter_func =
|
|
|
|
|
env->GetStaticMethodID(s_adapter_class, "QueryAdapter", "()Z");
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
|
|
|
|
while (s_adapter_detect_thread_running.IsSet())
|
|
|
|
|
{
|
|
|
|
|
if (!s_detected && UseAdapter() &&
|
|
|
|
|
env->CallStaticBooleanMethod(s_adapter_class, queryadapter_func))
|
|
|
|
|
Setup();
|
|
|
|
|
Common::SleepCurrentThread(1000);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-05-04 20:37:06 +00:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter scanning thread stopped");
|
2015-04-26 21:53:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
void SetAdapterCallback(std::function<void(void)> func)
|
2015-04-26 21:53:36 +00:00
|
|
|
|
{
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2015-04-26 21:53:36 +00:00
|
|
|
|
s_detect_callback = func;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2015-04-26 21:53:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 02:25:19 +00:00
|
|
|
|
static void RefreshConfig()
|
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_is_adapter_wanted = false;
|
|
|
|
|
|
2022-01-05 02:25:19 +00:00
|
|
|
|
for (int i = 0; i < SerialInterface::MAX_SI_CHANNELS; ++i)
|
2022-01-06 07:08:02 +00:00
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_is_adapter_wanted |= Config::Get(Config::GetInfoForSIDevice(i)) ==
|
|
|
|
|
SerialInterface::SIDevices::SIDEVICE_WIIU_ADAPTER;
|
2022-01-05 02:25:19 +00:00
|
|
|
|
s_config_rumble_enabled[i] = Config::Get(Config::GetInfoForAdapterRumble(i));
|
2022-01-06 07:08:02 +00:00
|
|
|
|
}
|
2022-01-05 02:25:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-10 09:45:45 +00:00
|
|
|
|
void Init()
|
|
|
|
|
{
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2014-12-11 00:15:36 +00:00
|
|
|
|
if (s_handle != nullptr)
|
2014-12-10 09:45:45 +00:00
|
|
|
|
return;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-03-08 21:16:54 +00:00
|
|
|
|
s_libusb_context = std::make_unique<LibusbUtils::Context>();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
if (s_fd)
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
2022-03-08 21:16:54 +00:00
|
|
|
|
|
2017-07-01 21:08:58 +00:00
|
|
|
|
if (Core::GetState() != Core::State::Uninitialized && Core::GetState() != Core::State::Starting)
|
2014-12-23 23:49:53 +00:00
|
|
|
|
{
|
2022-11-26 08:29:46 +00:00
|
|
|
|
auto& system = Core::System::GetInstance();
|
|
|
|
|
auto& core_timing = system.GetCoreTiming();
|
|
|
|
|
if ((core_timing.GetTicks() - s_last_init) < SystemTimers::GetTicksPerSecond())
|
2014-12-23 23:49:53 +00:00
|
|
|
|
return;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-11-26 08:29:46 +00:00
|
|
|
|
s_last_init = core_timing.GetTicks();
|
2014-12-23 23:49:53 +00:00
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_status = AdapterStatus::NotDetected;
|
|
|
|
|
s_adapter_error = LIBUSB_SUCCESS;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
2022-06-02 20:19:32 +00:00
|
|
|
|
JNIEnv* const env = IDCache::GetEnvForThread();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
2022-06-02 20:19:32 +00:00
|
|
|
|
const jclass adapter_class = env->FindClass("org/dolphinemu/dolphinemu/utils/Java_GCAdapter");
|
2022-04-19 21:41:13 +00:00
|
|
|
|
s_adapter_class = reinterpret_cast<jclass>(env->NewGlobalRef(adapter_class));
|
|
|
|
|
#endif
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-01-05 02:25:19 +00:00
|
|
|
|
if (!s_config_callback_id)
|
|
|
|
|
s_config_callback_id = Config::AddConfigChangedCallback(RefreshConfig);
|
|
|
|
|
RefreshConfig();
|
|
|
|
|
|
2017-02-06 22:04:14 +00:00
|
|
|
|
if (UseAdapter())
|
|
|
|
|
StartScanThread();
|
2015-04-26 21:53:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StartScanThread()
|
|
|
|
|
{
|
2015-12-31 17:09:47 +00:00
|
|
|
|
if (s_adapter_detect_thread_running.IsSet())
|
|
|
|
|
return;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-03-08 21:16:54 +00:00
|
|
|
|
if (!s_libusb_context->IsValid())
|
2017-02-06 22:04:14 +00:00
|
|
|
|
return;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2015-04-26 21:53:36 +00:00
|
|
|
|
s_adapter_detect_thread_running.Set(true);
|
|
|
|
|
s_adapter_detect_thread = std::thread(ScanThreadFunc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StopScanThread()
|
|
|
|
|
{
|
|
|
|
|
if (s_adapter_detect_thread_running.TestAndClear())
|
|
|
|
|
{
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2020-07-05 01:03:20 +00:00
|
|
|
|
s_hotplug_event.Set();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2015-04-26 21:53:36 +00:00
|
|
|
|
s_adapter_detect_thread.join();
|
2015-01-26 12:54:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 19:52:44 +00:00
|
|
|
|
static void Setup()
|
2015-01-26 12:54:54 +00:00
|
|
|
|
{
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
const AdapterStatus prev_status = s_status;
|
2018-07-25 19:44:57 +00:00
|
|
|
|
|
|
|
|
|
// Reset the error status in case the adapter gets unplugged
|
2022-10-10 04:28:14 +00:00
|
|
|
|
if (s_status == AdapterStatus::Error)
|
|
|
|
|
s_status = AdapterStatus::NotDetected;
|
2018-07-25 19:44:57 +00:00
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_port_states.fill({});
|
2019-02-01 18:47:07 +00:00
|
|
|
|
s_controller_rumble.fill(0);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-06-08 19:17:37 +00:00
|
|
|
|
const int ret = s_libusb_context->GetDeviceList([](libusb_device* device) {
|
2015-04-26 21:53:36 +00:00
|
|
|
|
if (CheckDeviceAccess(device))
|
2016-01-20 21:20:17 +00:00
|
|
|
|
{
|
|
|
|
|
// Only connect to a single adapter in case the user has multiple connected
|
2015-04-26 21:53:36 +00:00
|
|
|
|
AddGCAdapter(device);
|
2019-05-12 18:19:31 +00:00
|
|
|
|
return false;
|
2016-01-20 21:20:17 +00:00
|
|
|
|
}
|
2019-05-12 18:19:31 +00:00
|
|
|
|
return true;
|
|
|
|
|
});
|
2022-06-08 19:17:37 +00:00
|
|
|
|
if (ret != LIBUSB_SUCCESS)
|
|
|
|
|
WARN_LOG_FMT(CONTROLLERINTERFACE, "Failed to get device list: {}", LibusbUtils::ErrorWrap(ret));
|
2018-07-25 19:44:57 +00:00
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
if (s_status != AdapterStatus::Detected && prev_status != s_status &&
|
|
|
|
|
s_detect_callback != nullptr)
|
2018-07-25 19:44:57 +00:00
|
|
|
|
s_detect_callback();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
s_fd = 0;
|
|
|
|
|
s_detected = true;
|
|
|
|
|
|
|
|
|
|
// Make sure the thread isn't in the middle of shutting down while starting a new one
|
|
|
|
|
if (s_read_adapter_thread_running.TestAndClear())
|
|
|
|
|
s_read_adapter_thread.join();
|
|
|
|
|
|
|
|
|
|
s_read_adapter_thread_running.Set(true);
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_read_adapter_thread = std::thread(ReadThreadFunc);
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2015-04-26 21:53:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2015-04-26 21:53:36 +00:00
|
|
|
|
static bool CheckDeviceAccess(libusb_device* device)
|
|
|
|
|
{
|
|
|
|
|
libusb_device_descriptor desc;
|
2019-01-15 21:01:48 +00:00
|
|
|
|
int ret = libusb_get_device_descriptor(device, &desc);
|
2022-06-02 20:56:17 +00:00
|
|
|
|
if (ret != LIBUSB_SUCCESS)
|
2015-04-26 21:53:36 +00:00
|
|
|
|
{
|
|
|
|
|
// could not acquire the descriptor, no point in trying to use it.
|
2022-06-02 20:56:17 +00:00
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_get_device_descriptor failed: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(ret));
|
2015-04-26 21:53:36 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2019-01-15 21:01:48 +00:00
|
|
|
|
if (desc.idVendor != 0x057e || desc.idProduct != 0x0337)
|
2015-04-26 21:53:36 +00:00
|
|
|
|
{
|
2019-01-15 21:01:48 +00:00
|
|
|
|
// This isn’t the device we are looking for.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2021-05-04 20:37:06 +00:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "Found GC Adapter with Vendor: {:X} Product: {:X} Devnum: {}",
|
2020-10-22 21:49:29 +00:00
|
|
|
|
desc.idVendor, desc.idProduct, 1);
|
2019-01-15 21:01:48 +00:00
|
|
|
|
|
2018-07-17 20:31:18 +00:00
|
|
|
|
// In case of failure, capture the libusb error code into the adapter status
|
2022-10-10 04:28:14 +00:00
|
|
|
|
Common::ScopeGuard status_guard([&ret] {
|
|
|
|
|
s_adapter_error = static_cast<libusb_error>(ret);
|
|
|
|
|
s_status = AdapterStatus::Error;
|
|
|
|
|
});
|
2018-07-17 20:31:18 +00:00
|
|
|
|
|
2020-10-22 21:49:29 +00:00
|
|
|
|
const u8 bus = libusb_get_bus_number(device);
|
|
|
|
|
const u8 port = libusb_get_device_address(device);
|
2019-01-15 21:01:48 +00:00
|
|
|
|
ret = libusb_open(device, &s_handle);
|
2022-06-02 20:56:17 +00:00
|
|
|
|
if (ret != LIBUSB_SUCCESS)
|
2019-01-15 21:01:48 +00:00
|
|
|
|
{
|
2022-06-02 20:56:17 +00:00
|
|
|
|
if (ret == LIBUSB_ERROR_ACCESS)
|
|
|
|
|
{
|
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE,
|
|
|
|
|
"Dolphin does not have access to this device: Bus {:03d} Device {:03d}: ID "
|
|
|
|
|
"{:04X}:{:04X}.",
|
|
|
|
|
bus, port, desc.idVendor, desc.idProduct);
|
|
|
|
|
}
|
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_open failed to open device: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(ret));
|
2019-01-15 21:01:48 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 15:48:28 +00:00
|
|
|
|
bool detach_failed = false;
|
2019-01-15 21:01:48 +00:00
|
|
|
|
ret = libusb_kernel_driver_active(s_handle, 0);
|
2022-06-02 20:56:17 +00:00
|
|
|
|
if (ret == 1) // 1: kernel driver is active
|
2019-01-15 21:01:48 +00:00
|
|
|
|
{
|
2022-04-22 15:58:38 +00:00
|
|
|
|
// On macos detaching would fail without root or entitlement.
|
|
|
|
|
// We assume user is using GCAdapterDriver and therefor don't want to detach anything
|
|
|
|
|
#if !defined(__APPLE__)
|
2019-01-15 21:01:48 +00:00
|
|
|
|
ret = libusb_detach_kernel_driver(s_handle, 0);
|
2022-06-08 22:27:55 +00:00
|
|
|
|
detach_failed =
|
|
|
|
|
ret < LIBUSB_SUCCESS && ret != LIBUSB_ERROR_NOT_FOUND && ret != LIBUSB_ERROR_NOT_SUPPORTED;
|
2022-04-22 15:58:38 +00:00
|
|
|
|
#endif
|
2022-04-22 15:48:28 +00:00
|
|
|
|
if (detach_failed)
|
2022-06-02 20:56:17 +00:00
|
|
|
|
{
|
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_detach_kernel_driver failed: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(ret));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (ret != 0) // 0: kernel driver is not active, but otherwise no error.
|
|
|
|
|
{
|
|
|
|
|
// Neither 0 nor 1 means an error occured.
|
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_kernel_driver_active failed: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(ret));
|
2019-01-15 21:01:48 +00:00
|
|
|
|
}
|
2017-06-07 11:42:41 +00:00
|
|
|
|
|
2020-07-09 22:17:44 +00:00
|
|
|
|
// This call makes Nyko-brand (and perhaps other) adapters work.
|
|
|
|
|
// However it returns LIBUSB_ERROR_PIPE with Mayflash adapters.
|
|
|
|
|
const int transfer = libusb_control_transfer(s_handle, 0x21, 11, 0x0001, 0, nullptr, 0, 1000);
|
2022-06-08 22:27:55 +00:00
|
|
|
|
if (transfer < LIBUSB_SUCCESS)
|
2022-06-02 20:56:17 +00:00
|
|
|
|
{
|
|
|
|
|
WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_control_transfer failed: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(transfer));
|
|
|
|
|
}
|
2020-07-05 01:03:20 +00:00
|
|
|
|
|
2019-01-15 21:01:48 +00:00
|
|
|
|
// this split is needed so that we don't avoid claiming the interface when
|
|
|
|
|
// detaching the kernel driver is successful
|
2022-04-22 15:48:28 +00:00
|
|
|
|
if (detach_failed)
|
2018-07-17 20:29:48 +00:00
|
|
|
|
{
|
|
|
|
|
libusb_close(s_handle);
|
|
|
|
|
s_handle = nullptr;
|
2019-01-15 21:01:48 +00:00
|
|
|
|
return false;
|
2018-07-17 20:29:48 +00:00
|
|
|
|
}
|
2019-01-15 21:01:48 +00:00
|
|
|
|
|
|
|
|
|
ret = libusb_claim_interface(s_handle, 0);
|
2022-06-02 20:56:17 +00:00
|
|
|
|
if (ret != LIBUSB_SUCCESS)
|
2019-01-15 21:01:48 +00:00
|
|
|
|
{
|
2022-06-02 20:56:17 +00:00
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "libusb_claim_interface failed: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(ret));
|
2018-07-17 20:29:48 +00:00
|
|
|
|
libusb_close(s_handle);
|
|
|
|
|
s_handle = nullptr;
|
2019-01-15 21:01:48 +00:00
|
|
|
|
return false;
|
2015-01-26 12:54:54 +00:00
|
|
|
|
}
|
2019-01-15 21:01:48 +00:00
|
|
|
|
|
2018-07-17 20:31:18 +00:00
|
|
|
|
// Updating the adapter status will be done in AddGCAdapter
|
|
|
|
|
status_guard.Dismiss();
|
|
|
|
|
|
2019-01-15 21:01:48 +00:00
|
|
|
|
return true;
|
2015-01-26 12:54:54 +00:00
|
|
|
|
}
|
2015-01-05 01:43:26 +00:00
|
|
|
|
|
2015-04-26 21:53:36 +00:00
|
|
|
|
static void AddGCAdapter(libusb_device* device)
|
2015-01-26 12:54:54 +00:00
|
|
|
|
{
|
|
|
|
|
libusb_config_descriptor* config = nullptr;
|
2022-06-02 20:56:17 +00:00
|
|
|
|
if (const int error = libusb_get_config_descriptor(device, 0, &config); error != LIBUSB_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_get_config_descriptor failed: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(error));
|
|
|
|
|
}
|
2015-01-26 12:54:54 +00:00
|
|
|
|
for (u8 ic = 0; ic < config->bNumInterfaces; ic++)
|
|
|
|
|
{
|
|
|
|
|
const libusb_interface* interfaceContainer = &config->interface[ic];
|
|
|
|
|
for (int i = 0; i < interfaceContainer->num_altsetting; i++)
|
|
|
|
|
{
|
|
|
|
|
const libusb_interface_descriptor* interface = &interfaceContainer->altsetting[i];
|
|
|
|
|
for (u8 e = 0; e < interface->bNumEndpoints; e++)
|
|
|
|
|
{
|
|
|
|
|
const libusb_endpoint_descriptor* endpoint = &interface->endpoint[e];
|
|
|
|
|
if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN)
|
|
|
|
|
s_endpoint_in = endpoint->bEndpointAddress;
|
|
|
|
|
else
|
|
|
|
|
s_endpoint_out = endpoint->bEndpointAddress;
|
2014-12-10 09:45:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-26 12:54:54 +00:00
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-04-19 21:24:06 +00:00
|
|
|
|
int size = 0;
|
|
|
|
|
std::array<u8, CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE> payload = {0x13};
|
2022-10-10 04:28:14 +00:00
|
|
|
|
const int error =
|
|
|
|
|
libusb_interrupt_transfer(s_handle, s_endpoint_out, payload.data(),
|
|
|
|
|
CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE, &size, USB_TIMEOUT_MS);
|
2022-06-02 20:56:17 +00:00
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
WARN_LOG_FMT(CONTROLLERINTERFACE, "AddGCAdapter: libusb_interrupt_transfer failed: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(error));
|
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-04-21 04:36:07 +00:00
|
|
|
|
s_read_adapter_thread_running.Set(true);
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_read_adapter_thread = std::thread(ReadThreadFunc);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_status = AdapterStatus::Detected;
|
2015-04-26 21:53:36 +00:00
|
|
|
|
if (s_detect_callback != nullptr)
|
|
|
|
|
s_detect_callback();
|
2016-04-30 11:29:11 +00:00
|
|
|
|
ResetRumbleLockNeeded();
|
2014-12-10 09:45:45 +00:00
|
|
|
|
}
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2014-12-10 09:45:45 +00:00
|
|
|
|
|
|
|
|
|
void Shutdown()
|
2015-01-26 12:54:54 +00:00
|
|
|
|
{
|
2015-04-26 21:53:36 +00:00
|
|
|
|
StopScanThread();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-04-22 14:36:56 +00:00
|
|
|
|
#if LIBUSB_API_HAS_HOTPLUG
|
2022-03-08 21:16:54 +00:00
|
|
|
|
if (s_libusb_context->IsValid() && s_libusb_hotplug_enabled)
|
|
|
|
|
libusb_hotplug_deregister_callback(*s_libusb_context, s_hotplug_handle);
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2016-01-03 08:53:31 +00:00
|
|
|
|
#endif
|
2015-01-26 12:54:54 +00:00
|
|
|
|
Reset();
|
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
|
|
|
|
s_libusb_context.reset();
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_status = AdapterStatus::NotDetected;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2022-01-05 02:25:19 +00:00
|
|
|
|
|
|
|
|
|
if (s_config_callback_id)
|
|
|
|
|
{
|
|
|
|
|
Config::RemoveConfigChangedCallback(*s_config_callback_id);
|
|
|
|
|
s_config_callback_id = std::nullopt;
|
|
|
|
|
}
|
2015-01-26 12:54:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-19 19:52:44 +00:00
|
|
|
|
static void Reset()
|
2014-12-10 09:45:45 +00:00
|
|
|
|
{
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
std::unique_lock lock(s_init_mutex, std::defer_lock);
|
2016-04-30 11:29:11 +00:00
|
|
|
|
if (!lock.try_lock())
|
|
|
|
|
return;
|
2022-10-10 04:28:14 +00:00
|
|
|
|
if (s_status != AdapterStatus::Detected)
|
2014-12-10 09:45:45 +00:00
|
|
|
|
return;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
if (!s_detected)
|
|
|
|
|
return;
|
2022-04-21 04:36:07 +00:00
|
|
|
|
#endif
|
2022-04-19 21:41:13 +00:00
|
|
|
|
|
|
|
|
|
if (s_read_adapter_thread_running.TestAndClear())
|
|
|
|
|
s_read_adapter_thread.join();
|
2022-04-21 04:36:07 +00:00
|
|
|
|
// The read thread will close the write thread
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_port_states.fill({});
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_status = AdapterStatus::NotDetected;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2015-01-05 00:31:54 +00:00
|
|
|
|
if (s_handle)
|
|
|
|
|
{
|
2022-06-02 20:56:17 +00:00
|
|
|
|
const int error = libusb_release_interface(s_handle, 0);
|
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
WARN_LOG_FMT(CONTROLLERINTERFACE, "libusb_release_interface failed: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(error));
|
|
|
|
|
}
|
2015-01-05 00:31:54 +00:00
|
|
|
|
libusb_close(s_handle);
|
|
|
|
|
s_handle = nullptr;
|
|
|
|
|
}
|
2015-04-26 21:53:36 +00:00
|
|
|
|
if (s_detect_callback != nullptr)
|
|
|
|
|
s_detect_callback();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
s_detected = false;
|
|
|
|
|
s_fd = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-05-04 20:37:06 +00:00
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "GC Adapter detached");
|
2014-12-10 09:45:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-02 01:16:00 +00:00
|
|
|
|
GCPadStatus Input(int chan)
|
2014-12-10 09:45:45 +00:00
|
|
|
|
{
|
2016-01-06 06:35:56 +00:00
|
|
|
|
if (!UseAdapter())
|
2016-08-02 01:16:00 +00:00
|
|
|
|
return {};
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
if (s_handle == nullptr || s_status != AdapterStatus::Detected)
|
2016-08-02 01:16:00 +00:00
|
|
|
|
return {};
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
if (!s_detected || !s_fd)
|
|
|
|
|
return {};
|
|
|
|
|
#endif
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
std::lock_guard lk(s_read_mutex);
|
|
|
|
|
|
|
|
|
|
auto& pad_state = s_port_states[chan];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
// Return the "origin" state for the first input on a new connection.
|
|
|
|
|
if (pad_state.is_new_connection)
|
2014-12-11 00:15:36 +00:00
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
pad_state.is_new_connection = false;
|
|
|
|
|
return pad_state.origin;
|
2014-12-11 00:15:36 +00:00
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
return pad_state.status;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get ControllerType from first byte in input payload.
|
|
|
|
|
ControllerType IdentifyControllerType(u8 data)
|
|
|
|
|
{
|
|
|
|
|
if (Common::ExtractBit<4>(data))
|
|
|
|
|
return ControllerType::Wired;
|
|
|
|
|
|
|
|
|
|
if (Common::ExtractBit<5>(data))
|
|
|
|
|
return ControllerType::Wireless;
|
|
|
|
|
|
|
|
|
|
return ControllerType::None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ProcessInputPayload(const u8* data, std::size_t size)
|
|
|
|
|
{
|
|
|
|
|
if (size != CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
|| data[0] != LIBUSB_DT_HID
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
|
|
|
|
)
|
2014-12-10 09:45:45 +00:00
|
|
|
|
{
|
2020-07-05 01:03:20 +00:00
|
|
|
|
// This can occur for a few frames on initialization.
|
2022-10-10 04:28:14 +00:00
|
|
|
|
ERROR_LOG_FMT(CONTROLLERINTERFACE, "error reading payload (size: {}, type: {:02x})", size,
|
|
|
|
|
data[0]);
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
Reset();
|
|
|
|
|
#endif
|
2014-12-10 09:45:45 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
std::lock_guard lk(s_read_mutex);
|
|
|
|
|
|
|
|
|
|
for (int chan = 0; chan != SerialInterface::MAX_SI_CHANNELS; ++chan)
|
2015-12-31 17:09:47 +00:00
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
const u8* const channel_data = &data[1 + (9 * chan)];
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
const auto type = IdentifyControllerType(channel_data[0]);
|
|
|
|
|
|
|
|
|
|
auto& pad_state = s_port_states[chan];
|
|
|
|
|
|
|
|
|
|
GCPadStatus pad = {};
|
|
|
|
|
|
|
|
|
|
if (type != ControllerType::None)
|
|
|
|
|
{
|
|
|
|
|
const u8 b1 = channel_data[1];
|
|
|
|
|
const u8 b2 = channel_data[2];
|
|
|
|
|
|
|
|
|
|
if (Common::ExtractBit<0>(b1))
|
|
|
|
|
pad.button |= PAD_BUTTON_A;
|
|
|
|
|
if (Common::ExtractBit<1>(b1))
|
|
|
|
|
pad.button |= PAD_BUTTON_B;
|
|
|
|
|
if (Common::ExtractBit<2>(b1))
|
|
|
|
|
pad.button |= PAD_BUTTON_X;
|
|
|
|
|
if (Common::ExtractBit<3>(b1))
|
|
|
|
|
pad.button |= PAD_BUTTON_Y;
|
|
|
|
|
|
|
|
|
|
if (Common::ExtractBit<4>(b1))
|
|
|
|
|
pad.button |= PAD_BUTTON_LEFT;
|
|
|
|
|
if (Common::ExtractBit<5>(b1))
|
|
|
|
|
pad.button |= PAD_BUTTON_RIGHT;
|
|
|
|
|
if (Common::ExtractBit<6>(b1))
|
|
|
|
|
pad.button |= PAD_BUTTON_DOWN;
|
|
|
|
|
if (Common::ExtractBit<7>(b1))
|
|
|
|
|
pad.button |= PAD_BUTTON_UP;
|
|
|
|
|
|
|
|
|
|
if (Common::ExtractBit<0>(b2))
|
|
|
|
|
pad.button |= PAD_BUTTON_START;
|
|
|
|
|
if (Common::ExtractBit<1>(b2))
|
|
|
|
|
pad.button |= PAD_TRIGGER_Z;
|
|
|
|
|
if (Common::ExtractBit<2>(b2))
|
|
|
|
|
pad.button |= PAD_TRIGGER_R;
|
|
|
|
|
if (Common::ExtractBit<3>(b2))
|
|
|
|
|
pad.button |= PAD_TRIGGER_L;
|
|
|
|
|
|
|
|
|
|
pad.stickX = channel_data[3];
|
|
|
|
|
pad.stickY = channel_data[4];
|
|
|
|
|
pad.substickX = channel_data[5];
|
|
|
|
|
pad.substickY = channel_data[6];
|
|
|
|
|
pad.triggerLeft = channel_data[7];
|
|
|
|
|
pad.triggerRight = channel_data[8];
|
|
|
|
|
}
|
|
|
|
|
else if (!Core::WantsDeterminism())
|
|
|
|
|
{
|
|
|
|
|
// This is a hack to prevent a desync due to SI devices
|
|
|
|
|
// being different and returning different values.
|
|
|
|
|
// The corresponding code in DeviceGCAdapter has the same check
|
|
|
|
|
pad.button = PAD_ERR_STATUS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type != ControllerType::None && pad_state.controller_type == ControllerType::None)
|
|
|
|
|
{
|
|
|
|
|
NOTICE_LOG_FMT(CONTROLLERINTERFACE, "New device connected to Port {} of Type: {:02x}",
|
|
|
|
|
chan + 1, channel_data[0]);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2016-08-02 01:16:00 +00:00
|
|
|
|
pad.button |= PAD_GET_ORIGIN;
|
2022-10-10 04:28:14 +00:00
|
|
|
|
pad_state.origin = pad;
|
|
|
|
|
pad_state.is_new_connection = true;
|
|
|
|
|
}
|
2016-08-02 01:16:00 +00:00
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
pad_state.controller_type = type;
|
|
|
|
|
pad_state.status = pad;
|
2015-12-31 17:09:47 +00:00
|
|
|
|
}
|
2014-12-10 09:45:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 17:09:47 +00:00
|
|
|
|
bool DeviceConnected(int chan)
|
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
std::lock_guard lk(s_read_mutex);
|
|
|
|
|
return s_port_states[chan].controller_type != ControllerType::None;
|
2015-12-31 17:09:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-29 11:15:58 +00:00
|
|
|
|
void ResetDeviceType(int chan)
|
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
std::lock_guard lk(s_read_mutex);
|
|
|
|
|
s_port_states[chan].controller_type = ControllerType::None;
|
2019-03-29 11:15:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 17:09:47 +00:00
|
|
|
|
bool UseAdapter()
|
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
return s_is_adapter_wanted;
|
2015-12-31 17:09:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-08 17:13:58 +00:00
|
|
|
|
void ResetRumble()
|
|
|
|
|
{
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
std::unique_lock lock(s_init_mutex, std::defer_lock);
|
2016-04-30 11:29:11 +00:00
|
|
|
|
if (!lock.try_lock())
|
2015-11-08 17:13:58 +00:00
|
|
|
|
return;
|
2016-04-30 11:29:11 +00:00
|
|
|
|
ResetRumbleLockNeeded();
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
2022-04-19 21:24:06 +00:00
|
|
|
|
std::array<u8, CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE> rumble = {0x11, 0, 0, 0, 0};
|
2022-04-19 21:41:13 +00:00
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
std::lock_guard lk(s_write_mutex);
|
2022-04-19 21:24:06 +00:00
|
|
|
|
s_controller_write_payload = rumble;
|
|
|
|
|
s_controller_write_payload_size.store(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE);
|
2022-04-19 21:41:13 +00:00
|
|
|
|
}
|
|
|
|
|
s_write_happened.Set();
|
|
|
|
|
#endif
|
2016-04-30 11:29:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2016-04-30 11:29:11 +00:00
|
|
|
|
// Needs to be called when s_init_mutex is locked in order to avoid
|
|
|
|
|
// being called while the libusb state is being reset
|
|
|
|
|
static void ResetRumbleLockNeeded()
|
|
|
|
|
{
|
2022-10-10 04:28:14 +00:00
|
|
|
|
if (!UseAdapter() || (s_handle == nullptr || s_status != AdapterStatus::Detected))
|
2016-04-30 11:29:11 +00:00
|
|
|
|
{
|
2015-11-08 17:13:58 +00:00
|
|
|
|
return;
|
2016-04-30 11:29:11 +00:00
|
|
|
|
}
|
2015-11-08 17:13:58 +00:00
|
|
|
|
|
|
|
|
|
std::fill(std::begin(s_controller_rumble), std::end(s_controller_rumble), 0);
|
|
|
|
|
|
2022-04-19 21:24:06 +00:00
|
|
|
|
std::array<u8, CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE> rumble = {
|
|
|
|
|
0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2],
|
|
|
|
|
s_controller_rumble[3]};
|
2015-11-08 17:13:58 +00:00
|
|
|
|
|
|
|
|
|
int size = 0;
|
2022-10-10 04:28:14 +00:00
|
|
|
|
const int error =
|
|
|
|
|
libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble.data(),
|
|
|
|
|
CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE, &size, USB_TIMEOUT_MS);
|
2022-06-02 20:56:17 +00:00
|
|
|
|
if (error != LIBUSB_SUCCESS)
|
|
|
|
|
{
|
|
|
|
|
WARN_LOG_FMT(CONTROLLERINTERFACE, "ResetRumbleLockNeeded: libusb_interrupt_transfer failed: {}",
|
|
|
|
|
LibusbUtils::ErrorWrap(error));
|
|
|
|
|
}
|
2015-11-08 17:13:58 +00:00
|
|
|
|
|
2021-05-04 20:37:06 +00:00
|
|
|
|
INFO_LOG_FMT(CONTROLLERINTERFACE, "Rumble state reset");
|
2015-11-08 17:13:58 +00:00
|
|
|
|
}
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#endif
|
2015-11-08 17:13:58 +00:00
|
|
|
|
|
2014-12-13 04:04:03 +00:00
|
|
|
|
void Output(int chan, u8 rumble_command)
|
2014-12-10 09:45:45 +00:00
|
|
|
|
{
|
2022-04-19 21:57:08 +00:00
|
|
|
|
if (!UseAdapter() || !s_config_rumble_enabled[chan])
|
2014-12-10 09:45:45 +00:00
|
|
|
|
return;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-04-19 21:57:08 +00:00
|
|
|
|
if (s_handle == nullptr)
|
2022-04-19 21:41:13 +00:00
|
|
|
|
return;
|
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
if (!s_detected || !s_fd)
|
|
|
|
|
return;
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-12-11 03:06:35 +00:00
|
|
|
|
// Skip over rumble commands if it has not changed or the controller is wireless
|
2016-01-06 06:57:08 +00:00
|
|
|
|
if (rumble_command != s_controller_rumble[chan] &&
|
2022-10-10 04:28:14 +00:00
|
|
|
|
s_port_states[chan].controller_type != ControllerType::Wireless)
|
2014-12-10 09:45:45 +00:00
|
|
|
|
{
|
2014-12-13 04:04:03 +00:00
|
|
|
|
s_controller_rumble[chan] = rumble_command;
|
2022-04-19 21:24:06 +00:00
|
|
|
|
std::array<u8, CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE> rumble = {
|
|
|
|
|
0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2],
|
|
|
|
|
s_controller_rumble[3]};
|
2022-04-19 21:41:13 +00:00
|
|
|
|
{
|
2022-04-21 04:36:07 +00:00
|
|
|
|
#if GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
std::lock_guard lk(s_write_mutex);
|
2022-04-21 04:36:07 +00:00
|
|
|
|
#endif
|
2022-04-19 21:24:06 +00:00
|
|
|
|
s_controller_write_payload = rumble;
|
|
|
|
|
s_controller_write_payload_size.store(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE);
|
2022-04-19 21:41:13 +00:00
|
|
|
|
}
|
2022-04-21 00:21:05 +00:00
|
|
|
|
s_write_happened.Set();
|
2014-12-10 09:45:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-17 20:31:18 +00:00
|
|
|
|
bool IsDetected(const char** error_message)
|
2014-12-10 09:45:45 +00:00
|
|
|
|
{
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|
2022-10-10 04:28:14 +00:00
|
|
|
|
if (s_status != AdapterStatus::Error)
|
2018-07-17 20:31:18 +00:00
|
|
|
|
{
|
|
|
|
|
if (error_message)
|
|
|
|
|
*error_message = nullptr;
|
2014-12-10 09:45:45 +00:00
|
|
|
|
|
2022-10-10 04:28:14 +00:00
|
|
|
|
return s_status == AdapterStatus::Detected;
|
2018-07-17 20:31:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (error_message)
|
2022-10-10 04:28:14 +00:00
|
|
|
|
*error_message = libusb_strerror(s_adapter_error.load());
|
2018-07-17 20:31:18 +00:00
|
|
|
|
|
|
|
|
|
return false;
|
2022-04-19 21:41:13 +00:00
|
|
|
|
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
|
|
|
|
|
return s_detected;
|
|
|
|
|
#endif
|
2014-12-10 09:45:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 08:55:52 +00:00
|
|
|
|
} // namespace GCAdapter
|