RetroArch/input/connect/connect_psxadapter.c
gblues a4b934b71a
Fix some HID bugs (#14435)
* Fix Sixaxis gamepad operation

== DETAILS
It looks like commit 78e7d23c14 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.
2022-09-26 14:32:49 +02:00

211 lines
5.9 KiB
C

/* RetroArch - A frontend for libretro.
* Copyright (C) 2013-2014 - Jason Fetters
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* 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/>.
*/
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <boolean.h>
#include "joypad_connection.h"
#include "../input_defines.h"
struct hidpad_psxadapter_data
{
struct pad_connection* connection;
uint32_t slot;
uint32_t buttons;
uint8_t data[64];
};
static void* hidpad_psxadapter_init(void *data, uint32_t slot, hid_driver_t *driver)
{
struct pad_connection* connection = (struct pad_connection*)data;
struct hidpad_psxadapter_data* device = (struct hidpad_psxadapter_data*)
calloc(1, sizeof(struct hidpad_psxadapter_data));
if (!device)
return NULL;
if (!connection)
{
free(device);
return NULL;
}
device->connection = connection;
device->slot = slot;
return device;
}
static void hidpad_psxadapter_deinit(void *data)
{
struct hidpad_psxadapter_data *device =
(struct hidpad_psxadapter_data*)data;
if (device)
free(device);
}
static void hidpad_psxadapter_get_buttons(void *data, input_bits_t *state)
{
struct hidpad_psxadapter_data *device =
(struct hidpad_psxadapter_data*)data;
if (device)
{
BITS_COPY16_PTR(state, device->buttons);
}
else
BIT256_CLEAR_ALL_PTR(state);
}
static int16_t hidpad_psxadapter_get_axis(void *data, unsigned axis)
{
int val = 0;
struct hidpad_psxadapter_data *device =
(struct hidpad_psxadapter_data*)data;
if ( !device
|| axis >= 4
|| (device->data[2]==0x7F) ) /* digital mode detection */
return 0;
switch (axis)
{
case 0:
val = device->data[4];
break;
case 1:
val = device->data[5];
break;
case 2:
val = device->data[3];
break;
case 3:
val = device->data[2];
break;
}
val = (val << 8) - 0x8000;
/* hard coded deadzone */
if (abs(val) > 0x1000)
return val;
return 0;
}
#define PSX_H_GET(a) (a & 0x0F) /*HAT MASK = 0x0F */
#define PSX_H_LEFT(a) (a == 0x05) || (a == 0x06) || (a == 0x07)
#define PSX_H_RIGHT(a) (a == 0x01) || (a == 0x02) || (a == 0x03)
#define PSX_H_UP(a) (a == 0x07) || (a == 0x00) || (a == 0x01)
#define PSX_H_DOWN(a) (a == 0x03) || (a == 0x04) || (a == 0x05)
static void hidpad_psxadapter_packet_handler(void *data,
uint8_t *packet, uint16_t size)
{
uint32_t i, pressed_keys;
int16_t hat_value;
static const uint32_t button_mapping[16] =
{
RETRO_DEVICE_ID_JOYPAD_L2,
RETRO_DEVICE_ID_JOYPAD_R2,
RETRO_DEVICE_ID_JOYPAD_L,
RETRO_DEVICE_ID_JOYPAD_R,
RETRO_DEVICE_ID_JOYPAD_SELECT,
RETRO_DEVICE_ID_JOYPAD_START,
RETRO_DEVICE_ID_JOYPAD_L3,
RETRO_DEVICE_ID_JOYPAD_R3,
NO_BTN,
NO_BTN,
NO_BTN,
NO_BTN,
RETRO_DEVICE_ID_JOYPAD_X,
RETRO_DEVICE_ID_JOYPAD_A,
RETRO_DEVICE_ID_JOYPAD_B,
RETRO_DEVICE_ID_JOYPAD_Y,
};
struct hidpad_psxadapter_data *device =
(struct hidpad_psxadapter_data*)data;
if (!device)
return;
memcpy(device->data, packet, size);
device->buttons = 0;
pressed_keys = device->data[6] | (device->data[5] << 8);
for (i = 0; i < 16; i ++)
if (button_mapping[i] != NO_BTN)
device->buttons |= (pressed_keys & (1 << i)) ? (1 << button_mapping[i]) : 0;
if (device->data[2]==0x7F) /* digital mode detection */
{
/* We're in digital mode, process the dpad values */
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[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;
device->buttons |= PSX_H_DOWN(hat_value) ? (1 << RETRO_DEVICE_ID_JOYPAD_DOWN) : 0;
}
}
static void hidpad_psxadapter_set_rumble(void *data,
enum retro_rumble_effect effect, uint16_t strength)
{
(void)data;
(void)effect;
(void)strength;
}
const char * hidpad_psxadapter_get_name(void *data)
{
(void)data;
/* For now we return a single static name */
return "PSX to PS3 Controller Adapter";
}
static int32_t hidpad_psxadapter_button(void *data, uint16_t joykey)
{
struct hidpad_psxadapter_data *pad =
(struct hidpad_psxadapter_data*)data;
if (!pad || joykey > 31)
return 0;
return pad->buttons & (1 << joykey);
}
pad_connection_interface_t pad_connection_psxadapter = {
hidpad_psxadapter_init,
hidpad_psxadapter_deinit,
hidpad_psxadapter_packet_handler,
hidpad_psxadapter_set_rumble,
hidpad_psxadapter_get_buttons,
hidpad_psxadapter_get_axis,
hidpad_psxadapter_get_name,
hidpad_psxadapter_button,
false
};