(Input HID) Cleanups/buildfixes

This commit is contained in:
twinaphex 2020-07-24 19:11:20 +02:00
parent 1b24c37919
commit 0003074cfe
3 changed files with 101 additions and 133 deletions

View File

@ -70,43 +70,44 @@ extern pad_connection_interface_t ds3_pad_connection;
static void ds3_update_pad_state(ds3_instance_t *instance); static void ds3_update_pad_state(ds3_instance_t *instance);
static void ds3_update_analog_state(ds3_instance_t *instance); static void ds3_update_analog_state(ds3_instance_t *instance);
#if defined(WIIU)
static int32_t ds3_send_activation_packet(ds3_instance_t *instance) static int32_t ds3_send_activation_packet(ds3_instance_t *instance)
{ {
int32_t result; return HID_SET_REPORT(instance->handle,
#if defined(WIIU) HID_REPORT_FEATURE,
result = HID_SET_REPORT(instance->handle, DS3_ACTIVATION_REPORT_ID,
HID_REPORT_FEATURE, ds3_activation_packet,
DS3_ACTIVATION_REPORT_ID, sizeof(ds3_activation_packet));
ds3_activation_packet,
sizeof(ds3_activation_packet));
#else
HID_SEND_CONTROL(instance->handle,
ds3_activation_packet, sizeof(ds3_activation_packet));
#endif
return result;
} }
static uint32_t set_protocol(ds3_instance_t *instance, int protocol) static uint32_t set_protocol(ds3_instance_t *instance, int protocol)
{ {
uint32_t result = 0; return HID_SET_PROTOCOL(instance->handle, 1);
#if defined(WIIU)
result = HID_SET_PROTOCOL(instance->handle, 1);
#endif
return result;
} }
#else
static int32_t ds3_send_activation_packet(ds3_instance_t *instance)
{
HID_SEND_CONTROL(instance->handle,
ds3_activation_packet, sizeof(ds3_activation_packet));
return 0;
}
static uint32_t set_protocol(ds3_instance_t *instance, int protocol)
{
return 0;
}
#endif
static int32_t ds3_send_control_packet(ds3_instance_t *instance) static int32_t ds3_send_control_packet(ds3_instance_t *instance)
{ {
/* TODO/FIXME - C89-incompatible */
uint8_t packet_buffer[sizeof(ds3_control_packet)]; uint8_t packet_buffer[sizeof(ds3_control_packet)];
int32_t result = 0; int32_t result = 0;
memcpy(packet_buffer, ds3_control_packet, sizeof(ds3_control_packet)); memcpy(packet_buffer, ds3_control_packet, sizeof(ds3_control_packet));
packet_buffer[LED_OFFSET] = 0; packet_buffer[LED_OFFSET] = 0;
if(instance->pad) { if(instance->pad)
packet_buffer[LED_OFFSET] = 1 << ((instance->slot % 4) + 1); packet_buffer[LED_OFFSET] = 1 << ((instance->slot % 4) + 1);
}
packet_buffer[MOTOR1_OFFSET] = instance->motors[1] >> 8; packet_buffer[MOTOR1_OFFSET] = instance->motors[1] >> 8;
packet_buffer[MOTOR2_OFFSET] = instance->motors[0] >> 8; packet_buffer[MOTOR2_OFFSET] = instance->motors[0] >> 8;
@ -130,26 +131,25 @@ static int32_t ds3_send_control_packet(ds3_instance_t *instance)
static void *ds3_init(void *handle) static void *ds3_init(void *handle)
{ {
ds3_instance_t *instance; int errors = 0;
int errors = 0; ds3_isntance_t *instance = (ds3_instance_t *)
RARCH_LOG("[ds3]: init\n"); malloc(sizeof(ds3_instance_t));
instance = (ds3_instance_t *)calloc(1, sizeof(ds3_instance_t)); if (!instance)
if(!instance) return NULL;
goto error;
memset(instance, 0, sizeof(ds3_instance_t)); memset(instance, 0, sizeof(ds3_instance_t));
instance->handle = handle; instance->handle = handle;
RARCH_LOG("[ds3]: setting protocol\n"); /* Setting protocol */
/* this might fail, but we don't care. */ /* this might fail, but we don't care. */
set_protocol(instance, 1); set_protocol(instance, 1);
RARCH_LOG("[ds3]: sending control packet\n"); /* Sending control packet.. */
if(ds3_send_control_packet(instance) < 0) if(ds3_send_control_packet(instance) < 0)
errors++; errors++;
RARCH_LOG("[ds3]: sending activation packet\n"); /* Sending activation packet.. */
if(ds3_send_activation_packet(instance) < 0) if(ds3_send_activation_packet(instance) < 0)
errors++; errors++;
@ -160,14 +160,12 @@ static void *ds3_init(void *handle)
if(!instance->pad) if(!instance->pad)
goto error; goto error;
RARCH_LOG("[ds3]: init complete.\n");
return instance; return instance;
error: error:
RARCH_ERR("[ds3]: init failed.\n"); if(instance)
if(instance) free(instance);
free(instance); return NULL;
return NULL;
} }
static void ds3_free(void *data) static void ds3_free(void *data)
@ -210,7 +208,7 @@ hid_device_t ds3_hid_device = {
static void *ds3_pad_init(void *data, uint32_t slot, hid_driver_t *driver) static void *ds3_pad_init(void *data, uint32_t slot, hid_driver_t *driver)
{ {
ds3_instance_t *pad = (ds3_instance_t *)data; ds3_instance_t *pad = (ds3_instance_t *)data;
pad->slot = slot; pad->slot = slot;
return data; return data;
} }
@ -218,9 +216,9 @@ static void *ds3_pad_init(void *data, uint32_t slot, hid_driver_t *driver)
static void ds3_pad_deinit(void *data) static void ds3_pad_deinit(void *data)
{ {
ds3_instance_t *pad = (ds3_instance_t *)data; ds3_instance_t *pad = (ds3_instance_t *)data;
if(pad) { if(pad)
input_autoconfigure_disconnect(pad->slot, ds3_pad_connection.get_name(pad)); input_autoconfigure_disconnect(pad->slot,
} ds3_pad_connection.get_name(pad));
} }
static void ds3_get_buttons(void *data, input_bits_t *state) static void ds3_get_buttons(void *data, input_bits_t *state)
@ -233,7 +231,9 @@ static void ds3_get_buttons(void *data, input_bits_t *state)
if(pad->buttons & 0x10000) if(pad->buttons & 0x10000)
BIT256_SET_PTR(state, RARCH_MENU_TOGGLE); BIT256_SET_PTR(state, RARCH_MENU_TOGGLE);
} else { }
else
{
BIT256_CLEAR_ALL_PTR(state); BIT256_CLEAR_ALL_PTR(state);
} }
} }
@ -268,8 +268,8 @@ static void ds3_update_analog_state(ds3_instance_t *instance)
for(pad_axis = 0; pad_axis < 4; pad_axis++) for(pad_axis = 0; pad_axis < 4; pad_axis++)
{ {
axis = pad_axis % 2 ? 0 : 1; axis = pad_axis % 2 ? 0 : 1;
stick = pad_axis / 2; stick = pad_axis / 2;
interpolated = instance->data[6+pad_axis]; interpolated = instance->data[6+pad_axis];
instance->analog_state[stick][axis] = (interpolated - 128) * 256; instance->analog_state[stick][axis] = (interpolated - 128) * 256;
} }
@ -302,14 +302,17 @@ static void ds3_update_pad_state(ds3_instance_t *instance)
instance->buttons = 0; instance->buttons = 0;
pressed_keys = instance->data[2]|(instance->data[3] << 8)|((instance->data[4] & 0x01) << 16); pressed_keys = instance->data[2] |
(instance->data[3] << 8) |
((instance->data[4] & 0x01) << 16);
for(i = 0; i < 17; i++) for(i = 0; i < 17; i++)
instance->buttons |= (pressed_keys & (1 << i)) ? instance->buttons |= (pressed_keys & (1 << i)) ?
(1 << button_mapping[i]) : 0; (1 << button_mapping[i]) : 0;
} }
static void ds3_set_rumble(void *data, enum retro_rumble_effect effect, uint16_t strength) static void ds3_set_rumble(void *data,
enum retro_rumble_effect effect, uint16_t strength)
{ {
ds3_instance_t *pad = (ds3_instance_t *)data; ds3_instance_t *pad = (ds3_instance_t *)data;
} }
@ -335,13 +338,11 @@ static const char *ds3_get_name(void *data)
static int16_t ds3_button(void *data, uint16_t joykey) static int16_t ds3_button(void *data, uint16_t joykey)
{ {
ds3_instance_t *pad = (ds3_instance_t *)data; ds3_instance_t *pad = (ds3_instance_t *)data;
if (!pad) if (!pad)
return 0; return 0;
if (joykey < 31) if (joykey < 31)
if (pad->buttons & (1 << joykey)) return (pad->buttons & (1 << joykey));
ret |= (1 << joykey);
return 0; return 0;
} }

View File

@ -18,7 +18,8 @@
extern pad_connection_interface_t ds4_pad_connection; extern pad_connection_interface_t ds4_pad_connection;
typedef struct ds4_instance { typedef struct ds4_instance
{
void *handle; void *handle;
joypad_connection_t *pad; joypad_connection_t *pad;
int slot; int slot;
@ -37,43 +38,40 @@ typedef struct ds4_instance {
*/ */
static void *ds4_init(void *handle) static void *ds4_init(void *handle)
{ {
ds4_instance_t *instance; ds4_instance_t *instance = (ds4_instance_t *)
instance = (ds4_instance_t *)calloc(1, sizeof(ds4_instance_t)); malloc(sizeof(ds4_instance_t));
if(!instance) if (!instance)
goto error; return NULL;
memset(instance, 0, sizeof(ds4_instance_t)); memset(instance, 0, sizeof(ds4_instance_t));
instance->handle = handle; instance->handle = handle;
instance->pad = hid_pad_register(instance, &ds4_pad_connection); instance->pad = hid_pad_register(instance, &ds4_pad_connection);
if(!instance->pad)
goto error;
RARCH_LOG("[ds4]: init complete.\n");
return instance;
error:
RARCH_ERR("[ds4]: init failed.\n");
if(instance)
free(instance);
if (!instance->pad)
{
free(instance);
return NULL; return NULL;
}
return instance;
} }
static void ds4_free(void *data) static void ds4_free(void *data)
{ {
ds4_instance_t *instance = (ds4_instance_t *)data; ds4_instance_t *instance = (ds4_instance_t *)data;
if(instance) { if (!instance)
hid_pad_deregister(instance->pad); return;
free(instance);
} hid_pad_deregister(instance->pad);
free(instance);
} }
static void ds4_handle_packet(void *data, uint8_t *buffer, size_t size) static void ds4_handle_packet(void *data, uint8_t *buffer, size_t size)
{ {
ds4_instance_t *instance = (ds4_instance_t *)data; ds4_instance_t *instance = (ds4_instance_t *)data;
if(instance && instance->pad) if (instance && instance->pad)
instance->pad->iface->packet_handler(instance->pad->data, buffer, size); instance->pad->iface->packet_handler(instance->pad->data, buffer, size);
} }
@ -94,53 +92,28 @@ static void *ds4_pad_init(void *data, uint32_t slot, hid_driver_t *driver)
{ {
ds4_instance_t *instance = (ds4_instance_t *)data; ds4_instance_t *instance = (ds4_instance_t *)data;
if(!instance) if (!instance)
return NULL; return NULL;
instance->slot = slot; instance->slot = slot;
return instance; return instance;
} }
static void ds4_pad_deinit(void *data)
{
}
static void ds4_get_buttons(void *data, input_bits_t *state)
{
ds4_instance_t *instance = (ds4_instance_t *)data;
if(!instance)
return;
/* TODO: get buttons */
}
static void ds4_packet_handler(void *data, uint8_t *packet, uint16_t size) static void ds4_packet_handler(void *data, uint8_t *packet, uint16_t size)
{ {
ds4_instance_t *instance = (ds4_instance_t *)data; ds4_instance_t *instance = (ds4_instance_t *)data;
if(!instance) if (!instance)
return; return;
RARCH_LOG_BUFFER(packet, size); RARCH_LOG_BUFFER(packet, size);
} }
static void ds4_set_rumble(void *data, enum retro_rumble_effect effect, uint16_t strength) static void ds4_set_rumble(void *data, enum retro_rumble_effect effect, uint16_t strength) { }
{ static int16_t ds4_get_axis(void *data, unsigned axis) { return 0; }
} static const char *ds4_get_name(void *data) { return "Sony DualShock 4"; }
static int16_t ds4_button(void *data, uint16_t joykey) { return 0; }
static int16_t ds4_get_axis(void *data, unsigned axis) static void ds4_pad_deinit(void *data) { }
{ static void ds4_get_buttons(void *data, input_bits_t *state) { }
return 0;
}
static const char *ds4_get_name(void *data)
{
return "Sony DualShock 4";
}
static int16_t ds4_button(void *data, uint16_t joykey)
{
return 0;
}
pad_connection_interface_t ds4_pad_connection = { pad_connection_interface_t ds4_pad_connection = {
ds4_pad_init, ds4_pad_init,

View File

@ -48,11 +48,12 @@ joypad_connection_t *hid_pad_register(
int slot; int slot;
joypad_connection_t *result; joypad_connection_t *result;
if(!pad_handle) if (!pad_handle)
return NULL; return NULL;
slot = pad_connection_find_vacant_pad(hid_instance.pad_list); slot = pad_connection_find_vacant_pad(hid_instance.pad_list);
if(slot < 0)
if (slot < 0)
{ {
RARCH_ERR("[hid]: failed to find a vacant pad.\n"); RARCH_ERR("[hid]: failed to find a vacant pad.\n");
return NULL; return NULL;
@ -69,13 +70,13 @@ joypad_connection_t *hid_pad_register(
void hid_pad_deregister(joypad_connection_t *pad) void hid_pad_deregister(joypad_connection_t *pad)
{ {
if(!pad) if (!pad)
return; return;
if(pad->data) if (pad->data)
{ {
pad->iface->deinit(pad->data); pad->iface->deinit(pad->data);
pad->data = NULL; pad->data = NULL;
} }
pad->iface = NULL; pad->iface = NULL;
@ -84,15 +85,15 @@ void hid_pad_deregister(joypad_connection_t *pad)
static bool init_pad_list(hid_driver_instance_t *instance, unsigned slots) static bool init_pad_list(hid_driver_instance_t *instance, unsigned slots)
{ {
if(!instance || slots > MAX_USERS) if (!instance || slots > MAX_USERS)
return false; return false;
if(instance->pad_list) if (instance->pad_list)
return true; return true;
RARCH_LOG("[hid]: initializing pad list...\n"); /* Initializing pad list.. */
instance->pad_list = pad_connection_init(slots); instance->pad_list = pad_connection_init(slots);
if(!instance->pad_list) if (!instance->pad_list)
return false; return false;
instance->max_slot = slots; instance->max_slot = slots;
@ -115,26 +116,24 @@ bool hid_init(hid_driver_instance_t *instance,
input_device_driver_t *pad_driver, input_device_driver_t *pad_driver,
unsigned slots) unsigned slots)
{ {
RARCH_LOG("[hid]: initializing instance with %d pad slots\n", slots); void *os_driver_data = NULL;
if(!instance || !hid_driver || !pad_driver || slots > MAX_USERS) if (!instance || !hid_driver || !pad_driver || slots > MAX_USERS)
return false; return false;
RARCH_LOG("[hid]: initializing HID subsystem driver...\n"); /* Initializing HID subsystem driver */
instance->os_driver_data = hid_driver->init(); os_driver_data = hid_driver->init();
if(!instance->os_driver_data) if (!os_driver_data)
return false; return false;
if(!init_pad_list(instance, slots)) if (!init_pad_list(instance, slots))
{ {
hid_driver->free(instance->os_driver_data); hid_driver->free(os_driver_data);
instance->os_driver_data = NULL;
return false; return false;
} }
instance->os_driver = hid_driver; instance->os_driver_data = os_driver_data;
instance->pad_driver = pad_driver; instance->os_driver = hid_driver;
instance->pad_driver = pad_driver;
RARCH_LOG("[hid]: instance initialization complete.\n");
return true; return true;
} }
@ -146,20 +145,15 @@ bool hid_init(hid_driver_instance_t *instance,
*/ */
void hid_deinit(hid_driver_instance_t *instance) void hid_deinit(hid_driver_instance_t *instance)
{ {
if(!instance) if (!instance)
return; return;
RARCH_LOG("[hid]: destroying instance\n"); if (instance->os_driver && instance->os_driver_data)
if(instance->os_driver && instance->os_driver_data)
{
RARCH_LOG("[hid]: tearing down HID subsystem driver...\n");
instance->os_driver->free(instance->os_driver_data); instance->os_driver->free(instance->os_driver_data);
}
RARCH_LOG("[hid]: destroying pad data...\n"); /* Destroying pad data.. */
pad_connection_destroy(instance->pad_list); pad_connection_destroy(instance->pad_list);
RARCH_LOG("[hid]: wiping instance data...\n"); /* Wiping instance data.. */
memset(instance, 0, sizeof(hid_driver_instance_t)); memset(instance, 0, sizeof(hid_driver_instance_t));
} }