GCAdapter: Use std::array for controller read and write payloads

This commit is contained in:
Pokechu22 2022-04-19 14:24:06 -07:00
parent dd6592698e
commit 36d4ee0939
1 changed files with 37 additions and 30 deletions

View File

@ -87,10 +87,14 @@ static std::array<u8, SerialInterface::MAX_SI_CHANNELS> s_controller_type = {
ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE}; ControllerTypes::CONTROLLER_NONE, ControllerTypes::CONTROLLER_NONE};
static std::array<u8, SerialInterface::MAX_SI_CHANNELS> s_controller_rumble{}; static std::array<u8, SerialInterface::MAX_SI_CHANNELS> s_controller_rumble{};
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;
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
static std::mutex s_mutex; static std::mutex s_mutex;
static u8 s_controller_payload[37]; static std::array<u8, CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE> s_controller_payload;
static u8 s_controller_payload_swap[37]; static std::array<u8, CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE> s_controller_payload_swap;
// Only access with s_mutex held! // Only access with s_mutex held!
static int s_controller_payload_size = {0}; static int s_controller_payload_size = {0};
@ -105,12 +109,12 @@ static std::mutex s_init_mutex;
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
// Input handling // Input handling
static std::mutex s_read_mutex; static std::mutex s_read_mutex;
static std::array<u8, 37> s_controller_payload; static std::array<u8, CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE> s_controller_payload;
static int s_controller_payload_size = {0}; static int s_controller_payload_size = {0};
// Output handling // Output handling
static std::mutex s_write_mutex; static std::mutex s_write_mutex;
static u8 s_controller_write_payload[5]; static std::array<u8, CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE> s_controller_write_payload;
static std::atomic<int> s_controller_write_payload_size{0}; static std::atomic<int> s_controller_write_payload_size{0};
// Adapter running thread // Adapter running thread
@ -161,8 +165,8 @@ static void Read()
int payload_size = 0; int payload_size = 0;
while (s_adapter_thread_running.IsSet()) while (s_adapter_thread_running.IsSet())
{ {
int err = libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap, int err = libusb_interrupt_transfer(s_handle, s_endpoint_in, s_controller_payload_swap.data(),
sizeof(s_controller_payload_swap), &payload_size, 16); CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE, &payload_size, 16);
if (err) if (err)
ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb read failed: err={}", ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb read failed: err={}",
libusb_error_name(err)); libusb_error_name(err));
@ -207,7 +211,8 @@ static void Read()
jbyte* java_data = env->GetByteArrayElements(*java_controller_payload, nullptr); jbyte* java_data = env->GetByteArrayElements(*java_controller_payload, nullptr);
{ {
std::lock_guard<std::mutex> lk(s_read_mutex); std::lock_guard<std::mutex> lk(s_read_mutex);
std::copy(java_data, java_data + s_controller_payload.size(), s_controller_payload.begin()); std::copy(java_data, java_data + CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE,
s_controller_payload.begin());
s_controller_payload_size = read_size; s_controller_payload_size = read_size;
} }
env->ReleaseByteArrayElements(*java_controller_payload, java_data, 0); env->ReleaseByteArrayElements(*java_controller_payload, java_data, 0);
@ -251,15 +256,15 @@ static void Write()
if (!s_adapter_thread_running.IsSet()) if (!s_adapter_thread_running.IsSet())
return; return;
u8 payload[5] = { std::array<u8, CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE> payload = {
0x11, 0x11,
s_controller_rumble[0], s_controller_rumble[0],
s_controller_rumble[1], s_controller_rumble[1],
s_controller_rumble[2], s_controller_rumble[2],
s_controller_rumble[3], s_controller_rumble[3],
}; };
const int err = const int err = libusb_interrupt_transfer(s_handle, s_endpoint_out, payload.data(),
libusb_interrupt_transfer(s_handle, s_endpoint_out, payload, sizeof(payload), &size, 16); CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE, &size, 16);
if (err != 0) if (err != 0)
ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb write failed: err={}", ERROR_LOG_FMT(CONTROLLERINTERFACE, "adapter libusb write failed: err={}",
libusb_error_name(err)); libusb_error_name(err));
@ -276,12 +281,12 @@ static void Write()
int write_size = s_controller_write_payload_size.load(); int write_size = s_controller_write_payload_size.load();
if (write_size) if (write_size)
{ {
jbyteArray jrumble_array = env->NewByteArray(5); jbyteArray jrumble_array = env->NewByteArray(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE);
jbyte* jrumble = env->GetByteArrayElements(jrumble_array, nullptr); jbyte* jrumble = env->GetByteArrayElements(jrumble_array, nullptr);
{ {
std::lock_guard<std::mutex> lk(s_write_mutex); std::lock_guard<std::mutex> lk(s_write_mutex);
memcpy(jrumble, s_controller_write_payload, write_size); memcpy(jrumble, s_controller_write_payload.data(), write_size);
} }
env->ReleaseByteArrayElements(jrumble_array, jrumble, 0); env->ReleaseByteArrayElements(jrumble_array, jrumble, 0);
@ -604,9 +609,10 @@ static void AddGCAdapter(libusb_device* device)
} }
} }
int tmp = 0; int size = 0;
unsigned char payload = 0x13; std::array<u8, CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE> payload = {0x13};
libusb_interrupt_transfer(s_handle, s_endpoint_out, &payload, sizeof(payload), &tmp, 16); libusb_interrupt_transfer(s_handle, s_endpoint_out, payload.data(),
CONTROLER_OUTPUT_INIT_PAYLOAD_SIZE, &size, 16);
s_adapter_thread_running.Set(true); s_adapter_thread_running.Set(true);
s_adapter_input_thread = std::thread(Read); s_adapter_input_thread = std::thread(Read);
@ -700,22 +706,20 @@ GCPadStatus Input(int chan)
#endif #endif
int payload_size = 0; int payload_size = 0;
u8 controller_payload_copy[37]; std::array<u8, CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE> controller_payload_copy{};
{ {
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
std::lock_guard<std::mutex> lk(s_mutex); 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));
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
std::lock_guard<std::mutex> lk(s_read_mutex); std::lock_guard<std::mutex> lk(s_read_mutex);
controller_payload_copy = s_controller_payload;
#endif #endif
controller_payload_copy = s_controller_payload;
payload_size = s_controller_payload_size; payload_size = s_controller_payload_size;
} }
GCPadStatus pad = {}; GCPadStatus pad = {};
if (payload_size != sizeof(controller_payload_copy) if (payload_size != CONTROLER_INPUT_PAYLOAD_EXPECTED_SIZE
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
|| controller_payload_copy[0] != LIBUSB_DT_HID || controller_payload_copy[0] != LIBUSB_DT_HID
#endif #endif
@ -825,11 +829,11 @@ void ResetRumble()
return; return;
ResetRumbleLockNeeded(); ResetRumbleLockNeeded();
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
unsigned char rumble[5] = {0x11, 0, 0, 0, 0}; std::array<u8, CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE> rumble = {0x11, 0, 0, 0, 0};
{ {
std::lock_guard<std::mutex> lk(s_write_mutex); std::lock_guard<std::mutex> lk(s_write_mutex);
memcpy(s_controller_write_payload, rumble, 5); s_controller_write_payload = rumble;
s_controller_write_payload_size.store(5); s_controller_write_payload_size.store(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE);
} }
s_write_happened.Set(); s_write_happened.Set();
#endif #endif
@ -847,11 +851,13 @@ static void ResetRumbleLockNeeded()
std::fill(std::begin(s_controller_rumble), std::end(s_controller_rumble), 0); 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], std::array<u8, CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE> rumble = {
s_controller_rumble[2], s_controller_rumble[3]}; 0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2],
s_controller_rumble[3]};
int size = 0; int size = 0;
libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble, sizeof(rumble), &size, 16); libusb_interrupt_transfer(s_handle, s_endpoint_out, rumble.data(),
CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE, &size, 16);
INFO_LOG_FMT(CONTROLLERINTERFACE, "Rumble state reset"); INFO_LOG_FMT(CONTROLLERINTERFACE, "Rumble state reset");
} }
@ -878,12 +884,13 @@ void Output(int chan, u8 rumble_command)
#if GCADAPTER_USE_LIBUSB_IMPLEMENTATION #if GCADAPTER_USE_LIBUSB_IMPLEMENTATION
s_rumble_data_available.Set(); s_rumble_data_available.Set();
#elif GCADAPTER_USE_ANDROID_IMPLEMENTATION #elif GCADAPTER_USE_ANDROID_IMPLEMENTATION
unsigned char rumble[5] = {0x11, s_controller_rumble[0], s_controller_rumble[1], std::array<u8, CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE> rumble = {
s_controller_rumble[2], s_controller_rumble[3]}; 0x11, s_controller_rumble[0], s_controller_rumble[1], s_controller_rumble[2],
s_controller_rumble[3]};
{ {
std::lock_guard<std::mutex> lk(s_write_mutex); std::lock_guard<std::mutex> lk(s_write_mutex);
memcpy(s_controller_write_payload, rumble, 5); s_controller_write_payload = rumble;
s_controller_write_payload_size.store(5); s_controller_write_payload_size.store(CONTROLER_OUTPUT_RUMBLE_PAYLOAD_SIZE);
} }
s_write_happened.Set(); s_write_happened.Set();
#endif #endif