2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2014 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2014-12-10 09:45:45 +00:00
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2015-11-08 17:13:58 +00:00
|
|
|
#include <algorithm>
|
2014-12-10 09:45:45 +00:00
|
|
|
#include <libusb.h>
|
2015-05-27 03:44:51 +00:00
|
|
|
#include <mutex>
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2015-05-08 21:28:03 +00:00
|
|
|
#include "Common/Flag.h"
|
|
|
|
#include "Common/Thread.h"
|
2015-12-31 17:09:47 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2014-12-10 09:45:45 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2014-12-23 23:49:53 +00:00
|
|
|
#include "Core/Core.h"
|
|
|
|
#include "Core/CoreTiming.h"
|
2015-05-08 21:28:03 +00:00
|
|
|
#include "Core/HW/SI.h"
|
2014-12-23 23:49:53 +00:00
|
|
|
#include "Core/HW/SystemTimers.h"
|
2015-12-31 17:09:47 +00:00
|
|
|
|
|
|
|
#include "InputCommon/GCAdapter.h"
|
2015-05-08 21:28:03 +00:00
|
|
|
#include "InputCommon/GCPadStatus.h"
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2015-12-31 17:09:47 +00:00
|
|
|
namespace GCAdapter
|
2014-12-10 09:45:45 +00:00
|
|
|
{
|
2014-12-11 00:15:36 +00:00
|
|
|
enum ControllerTypes
|
|
|
|
{
|
|
|
|
CONTROLLER_NONE = 0,
|
|
|
|
CONTROLLER_WIRED = 1,
|
|
|
|
CONTROLLER_WIRELESS = 2
|
|
|
|
};
|
|
|
|
|
2015-04-26 21:53:36 +00:00
|
|
|
static bool CheckDeviceAccess(libusb_device* device);
|
|
|
|
static void AddGCAdapter(libusb_device* device);
|
|
|
|
|
2014-12-23 23:49:53 +00:00
|
|
|
static bool s_detected = false;
|
2014-12-11 00:15:36 +00:00
|
|
|
static libusb_device_handle* s_handle = nullptr;
|
|
|
|
static u8 s_controller_type[MAX_SI_CHANNELS] = { CONTROLLER_NONE, CONTROLLER_NONE, CONTROLLER_NONE, CONTROLLER_NONE };
|
2014-12-11 03:06:35 +00:00
|
|
|
static u8 s_controller_rumble[4];
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2014-12-11 00:15:36 +00:00
|
|
|
static std::mutex s_mutex;
|
|
|
|
static u8 s_controller_payload[37];
|
2015-01-05 01:43:26 +00:00
|
|
|
static u8 s_controller_payload_swap[37];
|
|
|
|
|
2014-12-11 00:15:36 +00:00
|
|
|
static int s_controller_payload_size = 0;
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2014-12-11 00:15:36 +00:00
|
|
|
static std::thread s_adapter_thread;
|
|
|
|
static Common::Flag s_adapter_thread_running;
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2015-04-26 21:53:36 +00:00
|
|
|
static std::thread s_adapter_detect_thread;
|
|
|
|
static Common::Flag s_adapter_detect_thread_running;
|
|
|
|
|
|
|
|
static std::function<void(void)> s_detect_callback;
|
|
|
|
|
2014-12-11 00:15:36 +00:00
|
|
|
static bool s_libusb_driver_not_supported = false;
|
2015-01-05 00:31:54 +00:00
|
|
|
static libusb_context* s_libusb_context = nullptr;
|
2015-04-26 21:53:36 +00:00
|
|
|
static bool s_libusb_hotplug_enabled = false;
|
2016-01-03 08:53:31 +00:00
|
|
|
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
|
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
|
|
|
|
2014-12-11 00:15:36 +00:00
|
|
|
static u8 s_endpoint_in = 0;
|
|
|
|
static u8 s_endpoint_out = 0;
|
2014-12-10 10:35:30 +00:00
|
|
|
|
2014-12-23 23:49:53 +00:00
|
|
|
static u64 s_last_init = 0;
|
|
|
|
|
2015-01-05 01:43:26 +00:00
|
|
|
static void Read()
|
|
|
|
{
|
|
|
|
while (s_adapter_thread_running.IsSet())
|
|
|
|
{
|
|
|
|
libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap, sizeof(s_controller_payload_swap), &s_controller_payload_size, 16);
|
2014-12-11 00:15:36 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(s_mutex);
|
2015-01-05 01:43:26 +00:00
|
|
|
std::swap(s_controller_payload_swap, s_controller_payload);
|
2014-12-11 00:15:36 +00:00
|
|
|
}
|
|
|
|
|
2014-12-10 09:45:45 +00:00
|
|
|
Common::YieldCPU();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 08:53:31 +00:00
|
|
|
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
|
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)
|
|
|
|
{
|
|
|
|
if (s_handle == nullptr && CheckDeviceAccess(dev))
|
|
|
|
AddGCAdapter(dev);
|
|
|
|
}
|
|
|
|
else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT)
|
|
|
|
{
|
|
|
|
if (s_handle != nullptr && libusb_get_device(s_handle) == dev)
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-03 08:53:31 +00:00
|
|
|
#endif
|
2015-04-26 21:53:36 +00:00
|
|
|
|
|
|
|
static void ScanThreadFunc()
|
|
|
|
{
|
|
|
|
Common::SetCurrentThreadName("GC Adapter Scanning Thread");
|
|
|
|
NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread started");
|
|
|
|
|
2016-01-03 08:53:31 +00:00
|
|
|
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
|
2015-04-26 21:53:36 +00:00
|
|
|
s_libusb_hotplug_enabled = libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG) != 0;
|
|
|
|
if (s_libusb_hotplug_enabled)
|
|
|
|
{
|
2015-10-03 02:06:17 +00:00
|
|
|
if (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) != LIBUSB_SUCCESS)
|
2015-04-26 21:53:36 +00:00
|
|
|
s_libusb_hotplug_enabled = false;
|
|
|
|
if (s_libusb_hotplug_enabled)
|
|
|
|
NOTICE_LOG(SERIALINTERFACE, "Using libUSB hotplug detection");
|
|
|
|
}
|
2016-01-03 08:53:31 +00:00
|
|
|
#endif
|
2015-04-26 21:53:36 +00:00
|
|
|
|
|
|
|
while (s_adapter_detect_thread_running.IsSet())
|
|
|
|
{
|
|
|
|
if (s_libusb_hotplug_enabled)
|
|
|
|
{
|
|
|
|
static timeval tv = {0, 500000};
|
|
|
|
libusb_handle_events_timeout(s_libusb_context, &tv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (s_handle == nullptr)
|
|
|
|
{
|
|
|
|
Setup();
|
|
|
|
if (s_detected && s_detect_callback != nullptr)
|
|
|
|
s_detect_callback();
|
|
|
|
}
|
|
|
|
Common::SleepCurrentThread(500);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NOTICE_LOG(SERIALINTERFACE, "GC Adapter scanning thread stopped");
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAdapterCallback(std::function<void(void)> func)
|
|
|
|
{
|
|
|
|
s_detect_callback = func;
|
|
|
|
}
|
|
|
|
|
2014-12-10 09:45:45 +00:00
|
|
|
void Init()
|
|
|
|
{
|
2014-12-11 00:15:36 +00:00
|
|
|
if (s_handle != nullptr)
|
2014-12-10 09:45:45 +00:00
|
|
|
return;
|
|
|
|
|
2014-12-23 23:49:53 +00:00
|
|
|
if (Core::GetState() != Core::CORE_UNINITIALIZED)
|
|
|
|
{
|
|
|
|
if ((CoreTiming::GetTicks() - s_last_init) < SystemTimers::GetTicksPerSecond())
|
|
|
|
return;
|
|
|
|
|
|
|
|
s_last_init = CoreTiming::GetTicks();
|
|
|
|
}
|
|
|
|
|
2014-12-11 00:15:36 +00:00
|
|
|
s_libusb_driver_not_supported = false;
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2015-01-05 00:31:54 +00:00
|
|
|
int ret = libusb_init(&s_libusb_context);
|
2014-12-10 09:45:45 +00:00
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
ERROR_LOG(SERIALINTERFACE, "libusb_init failed with error: %d", ret);
|
2015-07-14 10:34:16 +00:00
|
|
|
s_libusb_driver_not_supported = true;
|
2014-12-10 09:45:45 +00:00
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-31 17:09:47 +00:00
|
|
|
if (UseAdapter())
|
2015-07-14 15:15:26 +00:00
|
|
|
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;
|
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
s_adapter_detect_thread.join();
|
2015-01-26 12:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Setup()
|
|
|
|
{
|
|
|
|
libusb_device** list;
|
|
|
|
ssize_t cnt = libusb_get_device_list(s_libusb_context, &list);
|
|
|
|
|
|
|
|
for (int i = 0; i < MAX_SI_CHANNELS; i++)
|
|
|
|
{
|
|
|
|
s_controller_type[i] = CONTROLLER_NONE;
|
|
|
|
s_controller_rumble[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int d = 0; d < cnt; d++)
|
|
|
|
{
|
|
|
|
libusb_device* device = list[d];
|
2015-04-26 21:53:36 +00:00
|
|
|
if (CheckDeviceAccess(device))
|
|
|
|
AddGCAdapter(device);
|
|
|
|
}
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2015-04-26 21:53:36 +00:00
|
|
|
libusb_free_device_list(list, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool CheckDeviceAccess(libusb_device* device)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
libusb_device_descriptor desc;
|
|
|
|
int dRet = libusb_get_device_descriptor(device, &desc);
|
|
|
|
if (dRet)
|
|
|
|
{
|
|
|
|
// could not acquire the descriptor, no point in trying to use it.
|
|
|
|
ERROR_LOG(SERIALINTERFACE, "libusb_get_device_descriptor failed with error: %d", dRet);
|
|
|
|
return false;
|
|
|
|
}
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2015-04-26 21:53:36 +00:00
|
|
|
if (desc.idVendor == 0x057e && desc.idProduct == 0x0337)
|
|
|
|
{
|
|
|
|
NOTICE_LOG(SERIALINTERFACE, "Found GC Adapter with Vendor: %X Product: %X Devnum: %d", desc.idVendor, desc.idProduct, 1);
|
|
|
|
|
|
|
|
u8 bus = libusb_get_bus_number(device);
|
|
|
|
u8 port = libusb_get_device_address(device);
|
|
|
|
ret = libusb_open(device, &s_handle);
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (ret == LIBUSB_ERROR_ACCESS)
|
2015-01-26 12:54:54 +00:00
|
|
|
{
|
2015-04-26 21:53:36 +00:00
|
|
|
if (dRet)
|
2014-12-10 09:45:45 +00:00
|
|
|
{
|
2015-04-26 21:53:36 +00:00
|
|
|
ERROR_LOG(SERIALINTERFACE, "Dolphin does not have access to this device: Bus %03d Device %03d: ID ????:???? (couldn't get id).",
|
|
|
|
bus,
|
|
|
|
port
|
|
|
|
);
|
2014-12-10 09:45:45 +00:00
|
|
|
}
|
2015-01-26 12:54:54 +00:00
|
|
|
else
|
2014-12-10 09:45:45 +00:00
|
|
|
{
|
2015-04-26 21:53:36 +00:00
|
|
|
ERROR_LOG(SERIALINTERFACE, "Dolphin does not have access to this device: Bus %03d Device %03d: ID %04X:%04X.",
|
|
|
|
bus,
|
|
|
|
port,
|
|
|
|
desc.idVendor,
|
|
|
|
desc.idProduct
|
|
|
|
);
|
2014-12-10 09:45:45 +00:00
|
|
|
}
|
2015-01-26 12:54:54 +00:00
|
|
|
}
|
2015-04-26 21:53:36 +00:00
|
|
|
else
|
2015-01-26 12:54:54 +00:00
|
|
|
{
|
2015-04-26 21:53:36 +00:00
|
|
|
ERROR_LOG(SERIALINTERFACE, "libusb_open failed to open device with error = %d", ret);
|
|
|
|
if (ret == LIBUSB_ERROR_NOT_SUPPORTED)
|
|
|
|
s_libusb_driver_not_supported = true;
|
2015-01-26 12:54:54 +00:00
|
|
|
}
|
2015-06-30 23:28:41 +00:00
|
|
|
return false;
|
2015-04-26 21:53:36 +00:00
|
|
|
}
|
|
|
|
else if ((ret = libusb_kernel_driver_active(s_handle, 0)) == 1)
|
|
|
|
{
|
|
|
|
if ((ret = libusb_detach_kernel_driver(s_handle, 0)) && ret != LIBUSB_ERROR_NOT_SUPPORTED)
|
2015-01-26 12:54:54 +00:00
|
|
|
{
|
2015-04-26 21:53:36 +00:00
|
|
|
ERROR_LOG(SERIALINTERFACE, "libusb_detach_kernel_driver failed with error: %d", ret);
|
2015-01-26 12:54:54 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-26 21:53:36 +00:00
|
|
|
// this split is needed so that we don't avoid claiming the interface when
|
|
|
|
// detaching the kernel driver is successful
|
|
|
|
if (ret != 0 && ret != LIBUSB_ERROR_NOT_SUPPORTED)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if ((ret = libusb_claim_interface(s_handle, 0)))
|
|
|
|
{
|
|
|
|
ERROR_LOG(SERIALINTERFACE, "libusb_claim_interface failed with error: %d", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-26 12:54:54 +00:00
|
|
|
}
|
2015-04-26 21:53:36 +00:00
|
|
|
return false;
|
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;
|
|
|
|
libusb_get_config_descriptor(device, 0, &config);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int tmp = 0;
|
|
|
|
unsigned char payload = 0x13;
|
|
|
|
libusb_interrupt_transfer(s_handle, s_endpoint_out, &payload, sizeof(payload), &tmp, 16);
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2015-03-05 12:30:43 +00:00
|
|
|
s_adapter_thread_running.Set(true);
|
|
|
|
s_adapter_thread = std::thread(Read);
|
2015-01-26 12:54:54 +00:00
|
|
|
|
|
|
|
s_detected = true;
|
2015-04-26 21:53:36 +00:00
|
|
|
if (s_detect_callback != nullptr)
|
|
|
|
s_detect_callback();
|
2015-11-08 17:13:58 +00:00
|
|
|
ResetRumble();
|
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();
|
2016-01-03 08:53:31 +00:00
|
|
|
#if defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102
|
2015-04-26 21:53:36 +00:00
|
|
|
if (s_libusb_hotplug_enabled)
|
|
|
|
libusb_hotplug_deregister_callback(s_libusb_context, s_hotplug_handle);
|
2016-01-03 08:53:31 +00:00
|
|
|
#endif
|
2015-01-26 12:54:54 +00:00
|
|
|
Reset();
|
|
|
|
|
|
|
|
if (s_libusb_context)
|
|
|
|
{
|
|
|
|
libusb_exit(s_libusb_context);
|
|
|
|
s_libusb_context = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_libusb_driver_not_supported = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reset()
|
2014-12-10 09:45:45 +00:00
|
|
|
{
|
2015-03-10 09:48:25 +00:00
|
|
|
if (!s_detected)
|
2014-12-10 09:45:45 +00:00
|
|
|
return;
|
|
|
|
|
2014-12-11 00:15:36 +00:00
|
|
|
if (s_adapter_thread_running.TestAndClear())
|
2014-12-10 09:45:45 +00:00
|
|
|
{
|
2014-12-11 00:15:36 +00:00
|
|
|
s_adapter_thread.join();
|
2014-12-10 09:45:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 21:53:36 +00:00
|
|
|
for (int i = 0; i < MAX_SI_CHANNELS; i++)
|
|
|
|
s_controller_type[i] = CONTROLLER_NONE;
|
|
|
|
|
|
|
|
s_detected = false;
|
|
|
|
|
2015-01-05 00:31:54 +00:00
|
|
|
if (s_handle)
|
|
|
|
{
|
2015-01-05 01:43:26 +00:00
|
|
|
libusb_release_interface(s_handle, 0);
|
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();
|
|
|
|
NOTICE_LOG(SERIALINTERFACE, "GC Adapter detached");
|
2014-12-10 09:45:45 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 03:06:35 +00:00
|
|
|
void Input(int chan, GCPadStatus* pad)
|
2014-12-10 09:45:45 +00:00
|
|
|
{
|
2016-01-06 06:35:56 +00:00
|
|
|
if (!UseAdapter())
|
2014-12-10 09:45:45 +00:00
|
|
|
return;
|
|
|
|
|
2015-04-26 21:53:36 +00:00
|
|
|
if (s_handle == nullptr || !s_detected)
|
|
|
|
return;
|
2014-12-23 23:49:53 +00:00
|
|
|
|
2014-12-11 00:15:36 +00:00
|
|
|
u8 controller_payload_copy[37];
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lk(s_mutex);
|
|
|
|
std::copy(std::begin(s_controller_payload), std::end(s_controller_payload), std::begin(controller_payload_copy));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s_controller_payload_size != sizeof(controller_payload_copy) || controller_payload_copy[0] != LIBUSB_DT_HID)
|
2014-12-10 09:45:45 +00:00
|
|
|
{
|
2014-12-23 23:49:53 +00:00
|
|
|
INFO_LOG(SERIALINTERFACE, "error reading payload (size: %d, type: %02x)", s_controller_payload_size, controller_payload_copy[0]);
|
2015-04-26 21:53:36 +00:00
|
|
|
Reset();
|
2014-12-10 09:45:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-31 17:09:47 +00:00
|
|
|
bool get_origin = false;
|
2014-12-11 03:06:35 +00:00
|
|
|
u8 type = controller_payload_copy[1 + (9 * chan)] >> 4;
|
|
|
|
if (type != CONTROLLER_NONE && s_controller_type[chan] == CONTROLLER_NONE)
|
2015-12-31 17:09:47 +00:00
|
|
|
{
|
2014-12-11 03:06:35 +00:00
|
|
|
NOTICE_LOG(SERIALINTERFACE, "New device connected to Port %d of Type: %02x", chan + 1, controller_payload_copy[1 + (9 * chan)]);
|
2015-12-31 17:09:47 +00:00
|
|
|
get_origin = true;
|
|
|
|
}
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2014-12-11 03:06:35 +00:00
|
|
|
s_controller_type[chan] = type;
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2015-12-31 17:09:47 +00:00
|
|
|
memset(pad, 0, sizeof(*pad));
|
2014-12-11 03:06:35 +00:00
|
|
|
if (s_controller_type[chan] != CONTROLLER_NONE)
|
|
|
|
{
|
2014-12-13 04:04:03 +00:00
|
|
|
u8 b1 = controller_payload_copy[1 + (9 * chan) + 1];
|
|
|
|
u8 b2 = controller_payload_copy[1 + (9 * chan) + 2];
|
2014-12-11 03:06:35 +00:00
|
|
|
|
2014-12-13 04:04:03 +00:00
|
|
|
if (b1 & (1 << 0)) pad->button |= PAD_BUTTON_A;
|
|
|
|
if (b1 & (1 << 1)) pad->button |= PAD_BUTTON_B;
|
|
|
|
if (b1 & (1 << 2)) pad->button |= PAD_BUTTON_X;
|
2014-12-13 05:14:18 +00:00
|
|
|
if (b1 & (1 << 3)) pad->button |= PAD_BUTTON_Y;
|
2014-12-13 04:04:03 +00:00
|
|
|
|
|
|
|
if (b1 & (1 << 4)) pad->button |= PAD_BUTTON_LEFT;
|
|
|
|
if (b1 & (1 << 5)) pad->button |= PAD_BUTTON_RIGHT;
|
|
|
|
if (b1 & (1 << 6)) pad->button |= PAD_BUTTON_DOWN;
|
|
|
|
if (b1 & (1 << 7)) pad->button |= PAD_BUTTON_UP;
|
|
|
|
|
|
|
|
if (b2 & (1 << 0)) pad->button |= PAD_BUTTON_START;
|
|
|
|
if (b2 & (1 << 1)) pad->button |= PAD_TRIGGER_Z;
|
|
|
|
if (b2 & (1 << 2)) pad->button |= PAD_TRIGGER_R;
|
|
|
|
if (b2 & (1 << 3)) pad->button |= PAD_TRIGGER_L;
|
2014-12-11 03:06:35 +00:00
|
|
|
|
2015-12-31 17:09:47 +00:00
|
|
|
if (get_origin) pad->button |= PAD_GET_ORIGIN;
|
|
|
|
|
2014-12-11 03:06:35 +00:00
|
|
|
pad->stickX = controller_payload_copy[1 + (9 * chan) + 3];
|
|
|
|
pad->stickY = controller_payload_copy[1 + (9 * chan) + 4];
|
|
|
|
pad->substickX = controller_payload_copy[1 + (9 * chan) + 5];
|
|
|
|
pad->substickY = controller_payload_copy[1 + (9 * chan) + 6];
|
|
|
|
pad->triggerLeft = controller_payload_copy[1 + (9 * chan) + 7];
|
|
|
|
pad->triggerRight = controller_payload_copy[1 + (9 * chan) + 8];
|
2014-12-10 09:45:45 +00:00
|
|
|
}
|
2015-12-31 17:09:47 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pad->button = PAD_ERR_STATUS;
|
|
|
|
}
|
2014-12-10 09:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-31 17:09:47 +00:00
|
|
|
bool DeviceConnected(int chan)
|
|
|
|
{
|
|
|
|
return s_controller_type[chan] != CONTROLLER_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UseAdapter()
|
|
|
|
{
|
|
|
|
return SConfig::GetInstance().m_SIDevice[0] == SIDEVICE_WIIU_ADAPTER ||
|
|
|
|
SConfig::GetInstance().m_SIDevice[1] == SIDEVICE_WIIU_ADAPTER ||
|
|
|
|
SConfig::GetInstance().m_SIDevice[2] == SIDEVICE_WIIU_ADAPTER ||
|
|
|
|
SConfig::GetInstance().m_SIDevice[3] == SIDEVICE_WIIU_ADAPTER;
|
|
|
|
}
|
|
|
|
|
2015-11-08 17:13:58 +00:00
|
|
|
void ResetRumble()
|
|
|
|
{
|
2015-12-31 17:09:47 +00:00
|
|
|
if (!UseAdapter())
|
2015-11-08 17:13:58 +00:00
|
|
|
return;
|
|
|
|
if (s_handle == nullptr || !s_detected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::fill(std::begin(s_controller_rumble), std::end(s_controller_rumble), 0);
|
|
|
|
|
|
|
|
unsigned char rumble[5] = {0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2], s_controller_rumble[3]};
|
|
|
|
|
|
|
|
int size = 0;
|
|
|
|
libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble, sizeof(rumble), &size, 16);
|
|
|
|
|
|
|
|
DEBUG_LOG(SERIALINTERFACE, "Rumble state reset");
|
|
|
|
}
|
|
|
|
|
2014-12-13 04:04:03 +00:00
|
|
|
void Output(int chan, u8 rumble_command)
|
2014-12-10 09:45:45 +00:00
|
|
|
{
|
2016-01-06 06:35:56 +00:00
|
|
|
if (s_handle == nullptr || !UseAdapter() || !SConfig::GetInstance().m_AdapterRumble[chan])
|
2014-12-10 09:45:45 +00:00
|
|
|
return;
|
|
|
|
|
2014-12-11 03:06:35 +00:00
|
|
|
// Skip over rumble commands if it has not changed or the controller is wireless
|
2014-12-13 04:04:03 +00:00
|
|
|
if (rumble_command != s_controller_rumble[chan] && s_controller_type[chan] != CONTROLLER_WIRELESS)
|
2014-12-10 09:45:45 +00:00
|
|
|
{
|
2014-12-13 04:04:03 +00:00
|
|
|
s_controller_rumble[chan] = rumble_command;
|
2014-12-10 09:45:45 +00:00
|
|
|
|
2014-12-11 03:06:35 +00:00
|
|
|
unsigned char rumble[5] = { 0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2], s_controller_rumble[3] };
|
2014-12-10 09:45:45 +00:00
|
|
|
int size = 0;
|
|
|
|
|
2015-03-05 12:30:43 +00:00
|
|
|
libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble, sizeof(rumble), &size, 16);
|
|
|
|
// Netplay sends invalid data which results in size = 0x00. Ignore it.
|
|
|
|
if (size != 0x05 && size != 0x00)
|
2015-01-05 01:43:26 +00:00
|
|
|
{
|
2015-03-05 12:30:43 +00:00
|
|
|
INFO_LOG(SERIALINTERFACE, "error writing rumble (size: %d)", size);
|
2015-04-26 21:53:36 +00:00
|
|
|
Reset();
|
2014-12-10 09:45:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsDetected()
|
|
|
|
{
|
2015-03-10 09:48:25 +00:00
|
|
|
return s_detected;
|
2014-12-10 09:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsDriverDetected()
|
|
|
|
{
|
2014-12-11 00:15:36 +00:00
|
|
|
return !s_libusb_driver_not_supported;
|
2014-12-10 09:45:45 +00:00
|
|
|
}
|
|
|
|
|
2015-12-31 17:09:47 +00:00
|
|
|
} // end of namespace GCAdapter
|