2015-04-02 13:43:23 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2016-01-10 03:33:01 +00:00
|
|
|
* Copyright (C) 2011-2016 - Daniel De Matteis
|
2015-04-06 02:53:05 +00:00
|
|
|
*
|
2015-04-02 13:43:23 +00:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-11-28 01:31:19 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-02 13:43:23 +00:00
|
|
|
#include <libusb-1.0/libusb.h>
|
2015-09-05 18:53:56 +00:00
|
|
|
|
2015-04-02 13:43:23 +00:00
|
|
|
#include <rthreads/rthreads.h>
|
2015-04-03 03:26:58 +00:00
|
|
|
#include <compat/strl.h>
|
2016-03-20 13:06:21 +00:00
|
|
|
#include <queues/fifo_queue.h>
|
2015-12-26 06:54:17 +00:00
|
|
|
#include <string/stdstring.h>
|
2015-09-05 18:53:56 +00:00
|
|
|
|
2015-04-02 15:49:32 +00:00
|
|
|
#include "../connect/joypad_connection.h"
|
2016-04-17 05:37:00 +00:00
|
|
|
#include "../input_defines.h"
|
2015-04-03 03:26:58 +00:00
|
|
|
#include "../input_autodetect.h"
|
2015-11-28 01:31:19 +00:00
|
|
|
#include "../input_config.h"
|
|
|
|
#include "../input_driver.h"
|
2015-04-02 13:43:23 +00:00
|
|
|
#include "../input_hid_driver.h"
|
2015-11-23 11:03:38 +00:00
|
|
|
#include "../../verbosity.h"
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-06-26 11:06:48 +00:00
|
|
|
#ifndef LIBUSB_CAP_HAS_HOTPLUG
|
|
|
|
#define LIBUSB_CAP_HAS_HOTPLUG 0x0001
|
|
|
|
#endif
|
|
|
|
|
2015-04-02 13:43:23 +00:00
|
|
|
typedef struct libusb_hid
|
|
|
|
{
|
2015-07-11 23:08:53 +00:00
|
|
|
libusb_context *ctx;
|
2015-11-16 02:48:51 +00:00
|
|
|
joypad_connection_t *slots;
|
2015-04-14 00:19:10 +00:00
|
|
|
sthread_t *poll_thread;
|
2015-06-26 11:16:47 +00:00
|
|
|
int hp; /* libusb_hotplug_callback_handle is just int */
|
2015-04-14 00:19:10 +00:00
|
|
|
int quit;
|
2015-04-02 13:43:23 +00:00
|
|
|
} libusb_hid_t;
|
|
|
|
|
|
|
|
struct libusb_adapter
|
|
|
|
{
|
2015-04-14 00:19:10 +00:00
|
|
|
libusb_hid_t *hid;
|
2015-04-02 13:43:23 +00:00
|
|
|
volatile bool quitting;
|
|
|
|
struct libusb_device *device;
|
|
|
|
libusb_device_handle *handle;
|
2015-04-03 16:52:48 +00:00
|
|
|
int interface_number;
|
2015-04-06 02:53:05 +00:00
|
|
|
int endpoint_in;
|
|
|
|
int endpoint_out;
|
|
|
|
int endpoint_in_max_size;
|
|
|
|
int endpoint_out_max_size;
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-04-02 15:16:51 +00:00
|
|
|
uint8_t manufacturer_name[255];
|
|
|
|
uint8_t name[255];
|
2015-04-04 15:30:50 +00:00
|
|
|
uint8_t data[2048];
|
2015-04-02 15:16:51 +00:00
|
|
|
|
2015-06-25 15:25:09 +00:00
|
|
|
int slot;
|
2015-04-03 03:26:58 +00:00
|
|
|
|
2015-04-02 13:43:23 +00:00
|
|
|
sthread_t *thread;
|
2015-04-07 03:03:03 +00:00
|
|
|
slock_t *send_control_lock;
|
|
|
|
fifo_buffer_t *send_control_buffer;
|
2015-04-02 13:43:23 +00:00
|
|
|
struct libusb_adapter *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct libusb_adapter adapters;
|
|
|
|
|
|
|
|
static void adapter_thread(void *data)
|
|
|
|
{
|
2015-07-08 23:51:44 +00:00
|
|
|
uint8_t send_command_buf[4096];
|
2015-04-14 00:19:10 +00:00
|
|
|
struct libusb_adapter *adapter = (struct libusb_adapter*)data;
|
2015-07-09 00:31:46 +00:00
|
|
|
libusb_hid_t *hid = adapter ? adapter->hid : NULL;
|
|
|
|
|
|
|
|
if (!adapter)
|
|
|
|
return;
|
2015-04-14 00:19:10 +00:00
|
|
|
|
2015-04-02 13:43:23 +00:00
|
|
|
while (!adapter->quitting)
|
|
|
|
{
|
2015-04-07 03:03:03 +00:00
|
|
|
size_t send_command_size;
|
|
|
|
int tmp;
|
|
|
|
int report_number;
|
2015-07-08 23:51:44 +00:00
|
|
|
int size = 0;
|
2015-04-07 03:03:03 +00:00
|
|
|
|
|
|
|
slock_lock(adapter->send_control_lock);
|
2016-02-05 13:35:16 +00:00
|
|
|
if (fifo_read_avail(adapter->send_control_buffer)
|
|
|
|
>= sizeof(send_command_size))
|
2015-04-07 03:03:03 +00:00
|
|
|
{
|
2016-02-05 13:35:16 +00:00
|
|
|
fifo_read(adapter->send_control_buffer,
|
|
|
|
&send_command_size, sizeof(send_command_size));
|
|
|
|
|
|
|
|
if (fifo_read_avail(adapter->send_control_buffer)
|
|
|
|
>= sizeof(send_command_size))
|
2015-04-07 03:03:03 +00:00
|
|
|
{
|
2016-02-05 13:35:16 +00:00
|
|
|
fifo_read(adapter->send_control_buffer,
|
|
|
|
send_command_buf, send_command_size);
|
|
|
|
libusb_interrupt_transfer(adapter->handle,
|
|
|
|
adapter->endpoint_out, send_command_buf,
|
|
|
|
send_command_size, &tmp, 1000);
|
2015-04-07 03:03:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
slock_unlock(adapter->send_control_lock);
|
|
|
|
|
2016-02-05 13:35:16 +00:00
|
|
|
libusb_interrupt_transfer(adapter->handle,
|
|
|
|
adapter->endpoint_in, &adapter->data[1],
|
|
|
|
adapter->endpoint_in_max_size, &size, 1000);
|
2015-04-07 03:03:03 +00:00
|
|
|
|
2015-11-16 02:48:51 +00:00
|
|
|
if (adapter && hid && hid->slots && size)
|
|
|
|
pad_connection_packet(&hid->slots[adapter->slot], adapter->slot,
|
2015-04-07 03:03:03 +00:00
|
|
|
adapter->data, size+1);
|
2015-04-02 13:43:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 03:26:58 +00:00
|
|
|
static void libusb_hid_device_send_control(void *data,
|
|
|
|
uint8_t* data_buf, size_t size)
|
|
|
|
{
|
2015-04-03 16:52:48 +00:00
|
|
|
struct libusb_adapter *adapter = (struct libusb_adapter*)data;
|
|
|
|
|
2015-07-09 00:31:46 +00:00
|
|
|
if (!adapter)
|
|
|
|
return;
|
2015-07-08 23:51:44 +00:00
|
|
|
|
2015-07-09 00:31:46 +00:00
|
|
|
slock_lock(adapter->send_control_lock);
|
|
|
|
|
|
|
|
if (fifo_write_avail(adapter->send_control_buffer) >= size + sizeof(size))
|
|
|
|
{
|
|
|
|
fifo_write(adapter->send_control_buffer, &size, sizeof(size));
|
|
|
|
fifo_write(adapter->send_control_buffer, data_buf, size);
|
2015-04-07 03:03:03 +00:00
|
|
|
}
|
2015-07-09 00:31:46 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
RARCH_WARN("adapter write buffer is full, cannot write send control\n");
|
|
|
|
}
|
|
|
|
slock_unlock(adapter->send_control_lock);
|
2015-04-03 03:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void libusb_hid_device_add_autodetect(unsigned idx,
|
|
|
|
const char *device_name, const char *driver_name,
|
|
|
|
uint16_t dev_vid, uint16_t dev_pid)
|
|
|
|
{
|
|
|
|
autoconfig_params_t params = {{0}};
|
|
|
|
|
|
|
|
params.idx = idx;
|
|
|
|
params.vid = dev_vid;
|
|
|
|
params.pid = dev_pid;
|
|
|
|
|
|
|
|
strlcpy(params.name, device_name, sizeof(params.name));
|
|
|
|
strlcpy(params.driver, driver_name, sizeof(params.driver));
|
|
|
|
|
|
|
|
input_config_autoconfigure_joypad(¶ms);
|
|
|
|
}
|
|
|
|
|
2015-04-03 16:52:48 +00:00
|
|
|
static void libusb_get_description(struct libusb_device *device,
|
2015-04-06 02:53:05 +00:00
|
|
|
struct libusb_adapter *adapter)
|
2015-04-03 16:52:48 +00:00
|
|
|
{
|
2015-06-25 15:25:09 +00:00
|
|
|
int j;
|
|
|
|
unsigned i, k;
|
2015-04-03 16:52:48 +00:00
|
|
|
struct libusb_config_descriptor *config;
|
|
|
|
|
|
|
|
libusb_get_config_descriptor(device, 0, &config);
|
|
|
|
|
2015-07-09 00:31:46 +00:00
|
|
|
for (i = 0; i < (int)config->bNumInterfaces; i++)
|
2015-04-03 16:52:48 +00:00
|
|
|
{
|
|
|
|
const struct libusb_interface *inter = &config->interface[i];
|
|
|
|
|
|
|
|
for(j = 0; j < inter->num_altsetting; j++)
|
|
|
|
{
|
2016-02-05 13:35:16 +00:00
|
|
|
const struct libusb_interface_descriptor *interdesc =
|
|
|
|
&inter->altsetting[j];
|
2015-04-03 16:52:48 +00:00
|
|
|
|
2015-06-29 21:19:02 +00:00
|
|
|
#if 0
|
|
|
|
if (interdesc->bInterfaceClass == LIBUSB_CLASS_HID)
|
|
|
|
#endif
|
2015-04-03 16:52:48 +00:00
|
|
|
{
|
2015-04-06 02:53:05 +00:00
|
|
|
adapter->interface_number = (int)interdesc->bInterfaceNumber;
|
|
|
|
|
|
|
|
for(k = 0; k < (int)interdesc->bNumEndpoints; k++)
|
|
|
|
{
|
2016-02-05 13:35:16 +00:00
|
|
|
const struct libusb_endpoint_descriptor *epdesc =
|
|
|
|
&interdesc->endpoint[k];
|
|
|
|
bool is_int = (epdesc->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK)
|
|
|
|
== LIBUSB_TRANSFER_TYPE_INTERRUPT;
|
|
|
|
bool is_out = (epdesc->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK)
|
|
|
|
== LIBUSB_ENDPOINT_OUT;
|
|
|
|
bool is_in = (epdesc->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK)
|
|
|
|
== LIBUSB_ENDPOINT_IN;
|
|
|
|
|
2015-04-06 02:53:05 +00:00
|
|
|
if (is_int)
|
|
|
|
{
|
|
|
|
if (is_in)
|
|
|
|
{
|
|
|
|
adapter->endpoint_in = epdesc->bEndpointAddress;
|
|
|
|
adapter->endpoint_in_max_size = epdesc->wMaxPacketSize;
|
|
|
|
}
|
|
|
|
if (is_out)
|
|
|
|
{
|
|
|
|
adapter->endpoint_out = epdesc->bEndpointAddress;
|
|
|
|
adapter->endpoint_out_max_size = epdesc->wMaxPacketSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 16:52:48 +00:00
|
|
|
}
|
2015-04-06 02:53:05 +00:00
|
|
|
goto ret;
|
2015-04-03 16:52:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-06 02:53:05 +00:00
|
|
|
ret:
|
2015-04-03 16:52:48 +00:00
|
|
|
libusb_free_config_descriptor(config);
|
|
|
|
}
|
|
|
|
|
2015-04-02 15:28:12 +00:00
|
|
|
static int add_adapter(void *data, struct libusb_device *dev)
|
2015-04-02 13:43:23 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
struct libusb_device_descriptor desc;
|
2015-07-09 00:31:46 +00:00
|
|
|
const char *device_name = NULL;
|
2015-04-02 13:43:23 +00:00
|
|
|
struct libusb_adapter *old_head = NULL;
|
2015-04-03 03:26:58 +00:00
|
|
|
struct libusb_hid *hid = (struct libusb_hid*)data;
|
2015-07-08 23:51:44 +00:00
|
|
|
struct libusb_adapter *adapter = (struct libusb_adapter*)
|
|
|
|
calloc(1, sizeof(struct libusb_adapter));
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-07-09 00:31:46 +00:00
|
|
|
if (!adapter)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!hid)
|
2015-04-02 13:43:23 +00:00
|
|
|
{
|
2015-04-15 07:22:27 +00:00
|
|
|
free(adapter);
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR("Allocation of adapter failed.\n");
|
2015-04-02 13:43:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-04-03 17:02:35 +00:00
|
|
|
|
2015-04-02 13:43:23 +00:00
|
|
|
rc = libusb_get_device_descriptor(dev, &desc);
|
|
|
|
|
|
|
|
if (rc != LIBUSB_SUCCESS)
|
|
|
|
{
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR("Error getting device descriptor.\n");
|
2015-04-02 13:43:23 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter->device = dev;
|
|
|
|
|
2015-04-06 02:53:05 +00:00
|
|
|
libusb_get_description(adapter->device, adapter);
|
|
|
|
|
|
|
|
if (adapter->endpoint_in == 0)
|
|
|
|
{
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR("Could not find HID config for device.\n");
|
2015-04-06 02:53:05 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-04-02 13:43:23 +00:00
|
|
|
rc = libusb_open (adapter->device, &adapter->handle);
|
|
|
|
|
|
|
|
if (rc != LIBUSB_SUCCESS)
|
|
|
|
{
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR("Error opening device 0x%p (VID/PID: %04x:%04x).\n",
|
2015-06-29 21:19:02 +00:00
|
|
|
(void*)adapter->device, desc.idVendor, desc.idProduct);
|
2015-04-02 13:43:23 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-04-02 15:16:51 +00:00
|
|
|
if (desc.iManufacturer)
|
|
|
|
{
|
|
|
|
libusb_get_string_descriptor_ascii(adapter->handle,
|
|
|
|
desc.iManufacturer, adapter->manufacturer_name,
|
|
|
|
sizeof(adapter->manufacturer_name));
|
2015-06-29 21:19:02 +00:00
|
|
|
#if 0
|
2016-02-05 13:35:16 +00:00
|
|
|
RARCH_ERR(" Adapter Manufacturer name: %s\n",
|
|
|
|
adapter->manufacturer_name);
|
2015-06-29 21:19:02 +00:00
|
|
|
#endif
|
2015-04-02 15:16:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (desc.iProduct)
|
|
|
|
{
|
|
|
|
libusb_get_string_descriptor_ascii(adapter->handle,
|
|
|
|
desc.iProduct, adapter->name,
|
|
|
|
sizeof(adapter->name));
|
2015-06-29 21:19:02 +00:00
|
|
|
#if 0
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR(" Adapter name: %s\n", adapter->name);
|
2015-06-29 21:19:02 +00:00
|
|
|
#endif
|
2015-04-02 15:16:51 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 03:03:03 +00:00
|
|
|
device_name = (const char*)adapter->name;
|
|
|
|
|
2015-12-26 06:54:17 +00:00
|
|
|
if (string_is_empty((const char*)adapter->name))
|
2015-04-07 03:03:03 +00:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
adapter->send_control_lock = slock_new();
|
|
|
|
adapter->send_control_buffer = fifo_new(4096);
|
|
|
|
|
|
|
|
if (!adapter->send_control_lock || !adapter->send_control_buffer)
|
2015-04-02 13:43:23 +00:00
|
|
|
{
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR("Error creating send control buffer.\n");
|
2015-04-02 13:43:23 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2015-04-06 02:53:05 +00:00
|
|
|
|
2015-11-16 02:48:51 +00:00
|
|
|
adapter->slot = pad_connection_pad_init(hid->slots,
|
2015-07-09 00:31:46 +00:00
|
|
|
device_name, desc.idVendor, desc.idProduct,
|
|
|
|
adapter, &libusb_hid_device_send_control);
|
2015-04-03 03:26:58 +00:00
|
|
|
|
2015-05-06 02:03:00 +00:00
|
|
|
if (adapter->slot == -1)
|
|
|
|
goto error;
|
|
|
|
|
2015-11-16 02:48:51 +00:00
|
|
|
if (!pad_connection_has_interface(hid->slots, adapter->slot))
|
2015-04-03 17:33:47 +00:00
|
|
|
{
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR(" Interface not found (%s).\n", adapter->name);
|
2015-04-03 03:26:58 +00:00
|
|
|
goto error;
|
2015-04-03 17:33:47 +00:00
|
|
|
}
|
2015-04-03 03:26:58 +00:00
|
|
|
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_LOG("Interface found: [%s].\n", adapter->name);
|
2015-04-07 16:24:16 +00:00
|
|
|
|
2015-04-07 03:03:03 +00:00
|
|
|
if (libusb_kernel_driver_active(adapter->handle, 0) == 1
|
|
|
|
&& libusb_detach_kernel_driver(adapter->handle, 0))
|
|
|
|
{
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR("Error detaching handle 0x%p from kernel.\n", adapter->handle);
|
2015-04-03 03:26:58 +00:00
|
|
|
goto error;
|
2015-04-07 03:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rc = libusb_claim_interface(adapter->handle, adapter->interface_number);
|
2015-04-03 03:26:58 +00:00
|
|
|
|
2015-04-07 03:03:03 +00:00
|
|
|
if (rc != LIBUSB_SUCCESS)
|
|
|
|
{
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR("Error claiming interface %d .\n", adapter->interface_number);
|
2015-04-07 03:03:03 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_LOG("Device 0x%p attached (VID/PID: %04x:%04x).\n",
|
2015-04-02 13:43:23 +00:00
|
|
|
adapter->device, desc.idVendor, desc.idProduct);
|
|
|
|
|
2015-04-03 03:26:58 +00:00
|
|
|
libusb_hid_device_add_autodetect(adapter->slot,
|
|
|
|
device_name, libusb_hid.ident, desc.idVendor, desc.idProduct);
|
|
|
|
|
2015-04-14 00:19:10 +00:00
|
|
|
adapter->hid = hid;
|
|
|
|
adapter->thread = sthread_create(adapter_thread, adapter);
|
2015-04-02 13:43:23 +00:00
|
|
|
|
|
|
|
if (!adapter->thread)
|
|
|
|
{
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR("Error initializing adapter thread.\n");
|
2015-04-02 13:43:23 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
old_head = adapters.next;
|
|
|
|
adapters.next = adapter;
|
|
|
|
adapter->next = old_head;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (adapter->thread)
|
|
|
|
sthread_join(adapter->thread);
|
2015-04-07 03:03:03 +00:00
|
|
|
if (adapter->send_control_lock)
|
|
|
|
slock_free(adapter->send_control_lock);
|
|
|
|
if (adapter->send_control_buffer)
|
|
|
|
fifo_free(adapter->send_control_buffer);
|
2015-04-02 13:43:23 +00:00
|
|
|
if (adapter)
|
|
|
|
free(adapter);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-02 15:28:12 +00:00
|
|
|
static int remove_adapter(void *data, struct libusb_device *dev)
|
2015-04-02 13:43:23 +00:00
|
|
|
{
|
2015-07-08 23:51:44 +00:00
|
|
|
struct libusb_adapter *adapter = (struct libusb_adapter*)&adapters;
|
2015-04-03 03:26:58 +00:00
|
|
|
struct libusb_hid *hid = (struct libusb_hid*)data;
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
while (adapter->next == NULL)
|
|
|
|
return -1;
|
2015-04-14 03:36:43 +00:00
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
if (adapter->next->device == dev)
|
|
|
|
{
|
|
|
|
struct libusb_adapter *new_next = NULL;
|
|
|
|
const char *name = (const char*)adapter->next->name;
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
input_config_autoconfigure_disconnect(adapter->slot, name);
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
adapter->next->quitting = true;
|
|
|
|
sthread_join(adapter->next->thread);
|
2015-04-03 03:26:58 +00:00
|
|
|
|
2015-11-16 02:48:51 +00:00
|
|
|
pad_connection_pad_deinit(&hid->slots[adapter->slot], adapter->slot);
|
2015-04-07 03:03:03 +00:00
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
slock_free(adapter->send_control_lock);
|
|
|
|
fifo_free(adapter->send_control_buffer);
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2016-02-05 13:35:16 +00:00
|
|
|
libusb_release_interface(adapter->next->handle,
|
|
|
|
adapter->next->interface_number);
|
2015-07-11 23:08:53 +00:00
|
|
|
libusb_close(adapter->next->handle);
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
new_next = adapter->next->next;
|
|
|
|
free(adapter->next);
|
|
|
|
adapter->next = new_next;
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
return 0;
|
2015-04-02 13:43:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
adapter = adapter->next;
|
|
|
|
|
2015-04-02 13:43:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int libusb_hid_hotplug_callback(struct libusb_context *ctx,
|
|
|
|
struct libusb_device *dev, libusb_hotplug_event event, void *user_data)
|
|
|
|
{
|
2015-04-02 15:28:12 +00:00
|
|
|
libusb_hid_t *hid = (libusb_hid_t*)user_data;
|
2015-04-02 13:43:23 +00:00
|
|
|
|
|
|
|
switch (event)
|
|
|
|
{
|
|
|
|
case LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED:
|
2015-04-02 15:28:12 +00:00
|
|
|
add_adapter(hid, dev);
|
2015-04-02 13:43:23 +00:00
|
|
|
break;
|
|
|
|
case LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT:
|
2015-04-02 15:28:12 +00:00
|
|
|
remove_adapter(hid, dev);
|
2015-04-02 13:43:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_WARN("Unhandled event: %d\n", event);
|
2015-04-02 13:43:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool libusb_hid_joypad_query(void *data, unsigned pad)
|
|
|
|
{
|
|
|
|
return pad < MAX_USERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *libusb_hid_joypad_name(void *data, unsigned pad)
|
|
|
|
{
|
|
|
|
/* TODO/FIXME - implement properly */
|
|
|
|
if (pad >= MAX_USERS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t libusb_hid_joypad_get_buttons(void *data, unsigned port)
|
|
|
|
{
|
2015-04-03 03:26:58 +00:00
|
|
|
libusb_hid_t *hid = (libusb_hid_t*)data;
|
|
|
|
if (hid)
|
2015-11-16 02:48:51 +00:00
|
|
|
return pad_connection_get_buttons(&hid->slots[port], port);
|
2015-04-02 13:43:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-05 13:35:16 +00:00
|
|
|
static bool libusb_hid_joypad_button(void *data,
|
|
|
|
unsigned port, uint16_t joykey)
|
2015-04-02 13:43:23 +00:00
|
|
|
{
|
2015-04-03 03:26:58 +00:00
|
|
|
uint64_t buttons = libusb_hid_joypad_get_buttons(data, port);
|
|
|
|
|
|
|
|
if (joykey == NO_BTN)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* Check hat. */
|
|
|
|
if (GET_HAT_DIR(joykey))
|
|
|
|
return false;
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-04-03 03:26:58 +00:00
|
|
|
/* Check the button. */
|
|
|
|
if ((port < MAX_USERS) && (joykey < 32))
|
|
|
|
return ((buttons & (1 << joykey)) != 0);
|
2015-04-02 13:43:23 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool libusb_hid_joypad_rumble(void *data, unsigned pad,
|
|
|
|
enum retro_rumble_effect effect, uint16_t strength)
|
|
|
|
{
|
2015-04-03 03:26:58 +00:00
|
|
|
libusb_hid_t *hid = (libusb_hid_t*)data;
|
|
|
|
if (!hid)
|
|
|
|
return false;
|
2015-11-16 02:48:51 +00:00
|
|
|
return pad_connection_rumble(&hid->slots[pad], pad, effect, strength);
|
2015-04-02 13:43:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-09 00:31:46 +00:00
|
|
|
static int16_t libusb_hid_joypad_axis(void *data,
|
|
|
|
unsigned port, uint32_t joyaxis)
|
2015-04-02 13:43:23 +00:00
|
|
|
{
|
2015-04-03 03:26:58 +00:00
|
|
|
libusb_hid_t *hid = (libusb_hid_t*)data;
|
|
|
|
int16_t val = 0;
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-04-03 03:26:58 +00:00
|
|
|
if (joyaxis == AXIS_NONE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (AXIS_NEG_GET(joyaxis) < 4)
|
|
|
|
{
|
2015-11-16 02:48:51 +00:00
|
|
|
val = pad_connection_get_axis(&hid->slots[port],
|
2015-07-09 00:31:46 +00:00
|
|
|
port, AXIS_NEG_GET(joyaxis));
|
2015-04-03 03:26:58 +00:00
|
|
|
|
|
|
|
if (val >= 0)
|
|
|
|
val = 0;
|
|
|
|
}
|
|
|
|
else if(AXIS_POS_GET(joyaxis) < 4)
|
|
|
|
{
|
2015-11-16 02:48:51 +00:00
|
|
|
val = pad_connection_get_axis(&hid->slots[port],
|
2015-07-09 00:31:46 +00:00
|
|
|
port, AXIS_POS_GET(joyaxis));
|
2015-04-03 03:26:58 +00:00
|
|
|
|
|
|
|
if (val <= 0)
|
|
|
|
val = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
2015-04-02 13:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void libusb_hid_free(void *data)
|
|
|
|
{
|
|
|
|
libusb_hid_t *hid = (libusb_hid_t*)data;
|
|
|
|
|
|
|
|
while(adapters.next)
|
2015-04-07 04:05:33 +00:00
|
|
|
if (remove_adapter(hid, adapters.next->device) == -1)
|
2015-07-09 00:31:46 +00:00
|
|
|
RARCH_ERR("could not remove device %p\n",
|
|
|
|
adapters.next->device);
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-04-14 00:19:10 +00:00
|
|
|
if (hid->poll_thread)
|
|
|
|
{
|
|
|
|
hid->quit = 1;
|
|
|
|
sthread_join(hid->poll_thread);
|
|
|
|
}
|
|
|
|
|
2015-11-16 02:48:51 +00:00
|
|
|
pad_connection_destroy(hid->slots);
|
2015-04-07 03:24:01 +00:00
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
libusb_hotplug_deregister_callback(hid->ctx, hid->hp);
|
2015-04-02 13:43:23 +00:00
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
libusb_exit(hid->ctx);
|
2016-05-26 15:47:21 +00:00
|
|
|
free(hid);
|
2015-04-02 13:43:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 00:19:10 +00:00
|
|
|
static void poll_thread(void *data)
|
|
|
|
{
|
|
|
|
libusb_hid_t *hid = (libusb_hid_t*)data;
|
|
|
|
|
|
|
|
while (!hid->quit)
|
|
|
|
{
|
|
|
|
struct timeval timeout = {0};
|
2015-07-09 00:31:46 +00:00
|
|
|
libusb_handle_events_timeout_completed(NULL,
|
|
|
|
&timeout, &hid->quit);
|
2015-04-14 00:19:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 13:43:23 +00:00
|
|
|
static void *libusb_hid_init(void)
|
|
|
|
{
|
2015-06-25 15:25:09 +00:00
|
|
|
unsigned i, count;
|
|
|
|
int ret;
|
2015-04-02 13:43:23 +00:00
|
|
|
struct libusb_device **devices;
|
|
|
|
libusb_hid_t *hid = (libusb_hid_t*)calloc(1, sizeof(*hid));
|
|
|
|
|
|
|
|
if (!hid)
|
|
|
|
goto error;
|
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
ret = libusb_init(&hid->ctx);
|
2015-04-02 13:43:23 +00:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG))
|
|
|
|
goto error;
|
|
|
|
|
2015-11-16 02:48:51 +00:00
|
|
|
hid->slots = pad_connection_init(MAX_USERS);
|
|
|
|
|
|
|
|
if (!hid->slots)
|
2015-06-12 23:40:55 +00:00
|
|
|
goto error;
|
2015-04-04 14:32:06 +00:00
|
|
|
|
2015-07-11 23:08:53 +00:00
|
|
|
count = libusb_get_device_list(hid->ctx, &devices);
|
2015-04-02 13:43:23 +00:00
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
struct libusb_device_descriptor desc;
|
|
|
|
libusb_get_device_descriptor(devices[i], &desc);
|
|
|
|
|
|
|
|
if (desc.idVendor > 0 && desc.idProduct > 0)
|
2015-04-02 15:28:12 +00:00
|
|
|
add_adapter(hid, devices[i]);
|
2015-04-02 13:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
libusb_free_device_list(devices, 1);
|
|
|
|
|
2015-06-26 11:16:47 +00:00
|
|
|
ret = libusb_hotplug_register_callback(
|
2015-07-11 23:08:53 +00:00
|
|
|
hid->ctx,
|
2015-06-26 11:16:47 +00:00
|
|
|
(libusb_hotplug_event)(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
|
|
|
|
LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
|
|
|
|
(libusb_hotplug_flag)LIBUSB_HOTPLUG_ENUMERATE,
|
2015-04-02 13:43:23 +00:00
|
|
|
LIBUSB_HOTPLUG_MATCH_ANY,
|
|
|
|
LIBUSB_HOTPLUG_MATCH_ANY,
|
|
|
|
LIBUSB_HOTPLUG_MATCH_ANY,
|
|
|
|
libusb_hid_hotplug_callback,
|
2015-04-02 15:28:12 +00:00
|
|
|
hid,
|
2015-04-02 13:43:23 +00:00
|
|
|
&hid->hp);
|
|
|
|
|
|
|
|
if (ret != LIBUSB_SUCCESS)
|
|
|
|
{
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR("Error creating a hotplug callback.\n");
|
2015-04-02 13:43:23 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2015-04-14 00:19:10 +00:00
|
|
|
hid->poll_thread = sthread_create(poll_thread, hid);
|
|
|
|
|
|
|
|
if (!hid->poll_thread)
|
|
|
|
{
|
2015-06-30 12:48:57 +00:00
|
|
|
RARCH_ERR("Error creating polling thread");
|
2015-04-14 00:19:10 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2015-04-02 20:51:04 +00:00
|
|
|
|
2015-04-02 13:43:23 +00:00
|
|
|
return hid;
|
|
|
|
|
|
|
|
error:
|
2016-06-02 18:51:27 +00:00
|
|
|
if (hid)
|
|
|
|
libusb_hid_free(hid);
|
2015-04-14 00:19:10 +00:00
|
|
|
return NULL;
|
2015-04-02 13:43:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void libusb_hid_poll(void *data)
|
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
}
|
|
|
|
|
|
|
|
hid_driver_t libusb_hid = {
|
|
|
|
libusb_hid_init,
|
|
|
|
libusb_hid_joypad_query,
|
|
|
|
libusb_hid_free,
|
|
|
|
libusb_hid_joypad_button,
|
|
|
|
libusb_hid_joypad_get_buttons,
|
|
|
|
libusb_hid_joypad_axis,
|
|
|
|
libusb_hid_poll,
|
|
|
|
libusb_hid_joypad_rumble,
|
|
|
|
libusb_hid_joypad_name,
|
|
|
|
"libusb",
|
|
|
|
};
|