Fix some HID bugs (#14435)

* Fix Sixaxis gamepad operation

== DETAILS
It looks like commit 78e7d23c144c862d65ae1bf7ee41a1228bdfa380 broke
the DualShock. Sorry, @revvv, but this is probably going to break the
Retrode driver. We'll need to figure out a different solution.

* Fix a bunch of HID implementations

== DETAILS
As mentioned in the previous commit, the thing that broke the DS3
driver was a change to the wiiu HID handler, which did some pointer
math on the data buffer.

The thing is.. there's no reason to do that pointer math. Yet, I found
the same thing on almost every other HID driver: OSX, libusb, wiiusb.
The only other HID driver that did not do this was the Bluetooth
HID driver.

It doesn't make any sense. The only reason it doesn't crash is because
the memory right before the buffer is valid memory.

Change summary:
- remove the weird pointer math from all HID controllers
- remove the `#ifdef apple ...` in the ds3 and gamecube adapter
  drivers, because it's no longer needed.
- in the pad packet handlers, decrement data buffer offset references
  to account for the removal of the buffer manipulation.

* Fix DualShock 4

== DETAILS
Should've only subtracted 1, not 2; and now the pad works.

Also, added a PID to cover newer model DS4s. I picked "R2" out of the air
for the constant.

Tested on Mac OS

* Really really fix iohidmanager_set_report

A huge apology to @23rd for insulting your fix. I was wrong to call you
wrong.

That left the question: why did that change break the DS3?

Well, it's because `IOHIDDeviceSetReport` expects the report buffer to be
just the report. All of RA's HID SetReport calls include the report number
in the first byte, so we have to trim that off.

We actually do this in a different invocation of `IOHIDDeviceSetReport`
elsewhere in the file! This commit applies that same logic to
`iohidmanager_set_report`

This has two benefits:

1. The DS3 works
2. The DS3 no longer requres the user to press the PS button to activate
   it the first time. You plug it in and it Just Works, just like on Wii U.
This commit is contained in:
gblues 2022-09-26 05:32:49 -07:00 committed by GitHub
parent e902df3b0d
commit a4b934b71a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 32 additions and 42 deletions

View File

@ -118,7 +118,7 @@ static void hidpad_nesusb_packet_handler(void *data,
memcpy(device->data, packet, size);
device->buttons = 0;
pressed_keys = device->data[7] | (device->data[6] << 8);
pressed_keys = device->data[6] | (device->data[5] << 8);
for (i = 0; i < 16; i ++)
if (button_mapping[i] != NO_BTN)

View File

@ -300,11 +300,6 @@ static void ds3_packet_handler(void *device_data,
return;
}
#if defined(__APPLE__) && defined(HAVE_IOHIDMANAGER)
packet++;
size -= 2;
#endif
memcpy(device->data, packet, size);
ds3_update_pad_state(device);
ds3_update_analog_state(device);

View File

@ -266,7 +266,7 @@ static void hidpad_ps4_packet_handler(void *data,
}
#endif
memcpy(&device->data, &packet[2], sizeof(struct ps4));
memcpy(&device->data, packet+1, sizeof(struct ps4));
}
static void hidpad_ps4_set_rumble(void *data,

View File

@ -86,7 +86,7 @@ static int16_t hidpad_ps4_hori_mini_get_axis(void *data, unsigned axis)
if (!device || axis >= 4)
return 0;
val = (device->data[2 + axis] << 8) - 0x8000;
val = (device->data[1 + axis] << 8) - 0x8000;
if (abs(val) > 0x1000)
return val;
@ -126,7 +126,7 @@ static void hidpad_ps4_hori_mini_packet_handler(void *data,
device->buttons = 0;
dpad = device->data[6] & 0xF;
dpad = device->data[5] & 0xF;
switch(dpad)
{
@ -160,9 +160,9 @@ static void hidpad_ps4_hori_mini_packet_handler(void *data,
break;
}
pressed_keys = ((device->data[6] & 0xF0) >> 4) |
(device->data[7] << 4) |
(device->data[8] << 12);
pressed_keys = ((device->data[5] & 0xF0) >> 4) |
(device->data[6] << 4) |
(device->data[7] << 12);
for (i = 0; i < 15; i++)
{

View File

@ -148,7 +148,7 @@ static void hidpad_psxadapter_packet_handler(void *data,
device->buttons = 0;
pressed_keys = device->data[7] | (device->data[6] << 8);
pressed_keys = device->data[6] | (device->data[5] << 8);
for (i = 0; i < 16; i ++)
if (button_mapping[i] != NO_BTN)
@ -157,15 +157,15 @@ static void hidpad_psxadapter_packet_handler(void *data,
if (device->data[2]==0x7F) /* digital mode detection */
{
/* We're in digital mode, process the dpad values */
device->buttons |= (device->data[4]==0x00) ? (1 << RETRO_DEVICE_ID_JOYPAD_LEFT) : 0;
device->buttons |= (device->data[4]==0xFF) ? (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT) : 0;
device->buttons |= (device->data[5]==0x00) ? (1 << RETRO_DEVICE_ID_JOYPAD_UP) : 0;
device->buttons |= (device->data[5]==0xFF) ? (1 << RETRO_DEVICE_ID_JOYPAD_DOWN) : 0;
device->buttons |= (device->data[3]==0x00) ? (1 << RETRO_DEVICE_ID_JOYPAD_LEFT) : 0;
device->buttons |= (device->data[3]==0xFF) ? (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT) : 0;
device->buttons |= (device->data[4]==0x00) ? (1 << RETRO_DEVICE_ID_JOYPAD_UP) : 0;
device->buttons |= (device->data[4]==0xFF) ? (1 << RETRO_DEVICE_ID_JOYPAD_DOWN) : 0;
}
else
{
/* We're in analog mode, process the hat values as if they were pad buttons */
hat_value = PSX_H_GET(device->data[6]);
hat_value = PSX_H_GET(device->data[5]);
device->buttons |= PSX_H_LEFT(hat_value) ? (1 << RETRO_DEVICE_ID_JOYPAD_LEFT) : 0;
device->buttons |= PSX_H_RIGHT(hat_value) ? (1 << RETRO_DEVICE_ID_JOYPAD_RIGHT) : 0;
device->buttons |= PSX_H_UP(hat_value) ? (1 << RETRO_DEVICE_ID_JOYPAD_UP) : 0;

View File

@ -156,7 +156,7 @@ static void retrode_update_button_state(retrode_pad_data_t *pad)
if (!pad)
return;
pressed_keys = pad->data[4];
pressed_keys = pad->data[3];
pad->buttons = 0;
for (i = 0; i < 8; i ++)
@ -180,14 +180,14 @@ static void hidpad_retrode_packet_handler(void *device_data, uint8_t *packet, ui
memcpy(device->data, packet, size);
/*
* packet[1] contains Retrode port number
* packet[0] contains Retrode port number
* 1 = left SNES
* 2 = right SNES
* 3 = left Genesis/MD
* 4 = right Genesis/MD
*/
hidpad_retrode_pad_packet_handler(&device->pad_data[packet[1] - 1], &device->data[0], size);
hidpad_retrode_pad_packet_handler(&device->pad_data[packet[0] - 1], &device->data[0], size);
}
static void hidpad_retrode_set_rumble(void *data,

View File

@ -118,7 +118,7 @@ static void hidpad_snesusb_packet_handler(void *data, uint8_t *packet, uint16_t
device->buttons = 0;
pressed_keys = device->data[7] | (device->data[6] << 8);
pressed_keys = device->data[6] | (device->data[5] << 8);
for (i = 0; i < 16; i ++)
if (button_mapping[i] != NO_BTN)

View File

@ -649,12 +649,12 @@ static void hidpad_wii_packet_handler(void *data,
uint8_t *packet, uint16_t size)
{
struct connect_wii_wiimote_t* device = (struct connect_wii_wiimote_t*)data;
uint8_t *msg = packet + 2;
uint8_t *msg = packet + 1;
if (!device)
return;
switch (packet[1])
switch (packet[0])
{
case WM_RPT_BTN:
wiimote_pressed_buttons(device, msg);

View File

@ -216,14 +216,6 @@ static void hidpad_wiiugca_packet_handler(void *device_data, uint8_t *packet, ui
if (!device)
return;
/* Mac OSX reads a 39-byte packet which has both a leading and trailing byte from
* the actual packet data.
*/
#if defined(__APPLE__) && defined(HAVE_IOHIDMANAGER)
packet++;
size = 37;
#endif
memcpy(device->data, packet, size);
for (i = 1; i < 37; i += 9)

View File

@ -41,6 +41,11 @@ static joypad_connection_entry_t pad_map[] = {
PID_SONY_DS4,
&pad_connection_ps4
},
{ "Wireless Controller",
VID_SONY,
PID_SONY_DS4_R2,
&pad_connection_ps4
},
{ "PLAYSTATION(R)3 Controller",
VID_SONY,
PID_SONY_DS3,
@ -182,7 +187,7 @@ joypad_connection_entry_t *find_connection_entry(uint16_t vid, uint16_t pid, con
SWAP_IF_BIG(vid), SWAP_IF_BIG(pid), name, pad_map[i].name);
}
else if (!string_is_equal(pad_map[i].name, name))
continue;
continue;
}
if (name_match || (pad_map[i].vid == vid && pad_map[i].pid == pid))

View File

@ -46,6 +46,7 @@
#define PID_NINTENDO_PRO SWAP_IF_BIG(0x0330)
#define PID_SONY_DS3 SWAP_IF_BIG(0x0268)
#define PID_SONY_DS4 SWAP_IF_BIG(0x05c4)
#define PID_SONY_DS4_R2 SWAP_IF_BIG(0x09cc)
#define PID_DS3_CLONE SWAP_IF_BIG(0x20d6)
#define PID_SNES_CLONE SWAP_IF_BIG(0xe401)
#define PID_MICRONTEK_NES SWAP_IF_BIG(0x0011)

View File

@ -701,7 +701,7 @@ static void iohidmanager_hid_device_add(IOHIDDeviceRef device, iohidmanager_hid_
if (pad_connection_has_interface(hid->slots, adapter->slot)) {
IOHIDDeviceRegisterInputReportCallback(device,
adapter->data + 1, sizeof(adapter->data) - 1,
adapter->data, sizeof(adapter->data),
iohidmanager_hid_device_report, adapter);
}
else {
@ -1105,7 +1105,7 @@ static int32_t iohidmanager_set_report(void *handle, uint8_t report_type, uint8_
(struct iohidmanager_hid_adapter*)handle;
if (adapter)
return IOHIDDeviceSetReport(adapter->handle, translate_hid_report_type(report_type), report_id, data_buf, size);
return IOHIDDeviceSetReport(adapter->handle, translate_hid_report_type(report_type), report_id, data_buf + 1, size - 1);
return -1;
}

View File

@ -113,12 +113,12 @@ static void adapter_thread(void *data)
slock_unlock(adapter->send_control_lock);
libusb_interrupt_transfer(adapter->handle,
adapter->endpoint_in, &adapter->data[1],
adapter->endpoint_in, &adapter->data[0],
adapter->endpoint_in_max_size, &size, 1000);
if (adapter && hid && hid->slots && size)
pad_connection_packet(&hid->slots[adapter->slot], adapter->slot,
adapter->data, size+1);
adapter->data, size);
}
}

View File

@ -624,10 +624,7 @@ static void wiiu_hid_read_loop_callback(uint32_t handle, int32_t error,
if (error == 0)
{
/* NOTE: packet_handler() expects that packet[1] is the first byte, so added -1.
* The Wii version puts the slot number in packet[0], which is not possible here:
* packet[0] is undefined! */
adapter->pad_driver->packet_handler(adapter->pad_driver_data, buffer-1, buffer_size+1);
adapter->pad_driver->packet_handler(adapter->pad_driver_data, buffer, buffer_size);
}
}
}

View File

@ -111,7 +111,7 @@ static int32_t wiiusb_hid_read_cb(int32_t size, void *data)
if (hid && hid->connections && size > 0)
pad_connection_packet(&hid->connections[adapter->slot],
adapter->slot, adapter->data-1, size+1);
adapter->slot, adapter->data, size);
if (adapter)
adapter->busy = false;