Skip to content

Commit

Permalink
(GX) Refactor of the Wii USB HID support. Now it's working!
Browse files Browse the repository at this point in the history
In order to have a controller working you need:
1) Have a matching HID autoconfig file in autoconfig/hid for your controller.
2) Create a "connect" driver for the pad in "input/connect" folder (source code of RA).
3) Once you are in RA, change the joystick driver to HID and restart.
4) You may be now able to use you USB HID compatible pad in RA.

I included some "connect" drivers as an example. It also need to include them for compilation.
  • Loading branch information
netux79 committed Feb 4, 2016
1 parent deb3c73 commit bca4ccb
Show file tree
Hide file tree
Showing 11 changed files with 809 additions and 254 deletions.
5 changes: 5 additions & 0 deletions griffin/griffin.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,11 @@ INPUT (HID)
#include "../input/connect/connect_ps4.c"
#include "../input/connect/connect_wii.c"
#include "../input/connect/connect_wiiupro.c"
#ifdef HAVE_WIIUSB_HID
#include "../input/connect/connect_snesusb.c"
#include "../input/connect/connect_nesusb.c"
#include "../input/connect/connect_wiiugca.c"
#endif
#endif

/*============================================================
Expand Down
146 changes: 146 additions & 0 deletions input/connect/connect_nesusb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2013-2014 - Jason Fetters
* Copyright (C) 2011-2016 - 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"

struct hidpad_nesusb_data
{
struct pad_connection* connection;
uint8_t data[64];
uint32_t slot;
uint64_t buttons;
};

static void* hidpad_nesusb_init(void *data, uint32_t slot, send_control_t ptr)
{
struct pad_connection* connection = (struct pad_connection*)data;
struct hidpad_nesusb_data* device = (struct hidpad_nesusb_data*)
calloc(1, sizeof(struct hidpad_nesusb_data));

if (!device)
return NULL;

if (!connection)
{
free(device);
return NULL;
}

device->connection = connection;
device->slot = slot;

return device;
}

static void hidpad_nesusb_deinit(void *data)
{
struct hidpad_nesusb_data *device = (struct hidpad_nesusb_data*)data;

if (device)
free(device);
}

static uint64_t hidpad_nesusb_get_buttons(void *data)
{
struct hidpad_nesusb_data *device = (struct hidpad_nesusb_data*)data;
if (!device)
return 0;
return device->buttons;
}

static int16_t hidpad_nesusb_get_axis(void *data, unsigned axis)
{
int val;
struct hidpad_nesusb_data *device = (struct hidpad_nesusb_data*)data;

if (!device || axis >= 2)
return 0;

val = device->data[4 + axis];
val = (val << 8) - 0x8000;

return (abs(val) > 0x1000) ? val : 0;
}

static void hidpad_nesusb_packet_handler(void *data, uint8_t *packet, uint16_t size)
{
uint32_t i, pressed_keys;
static const uint32_t button_mapping[17] =
{
NO_BTN,
NO_BTN,
NO_BTN,
NO_BTN,
RETRO_DEVICE_ID_JOYPAD_SELECT,
RETRO_DEVICE_ID_JOYPAD_START,
NO_BTN,
NO_BTN,
NO_BTN,
NO_BTN,
NO_BTN,
NO_BTN,
RETRO_DEVICE_ID_JOYPAD_B,
RETRO_DEVICE_ID_JOYPAD_A,
NO_BTN,
NO_BTN,
16, /* HOME BUTTON when pressing SELECT+START */
};
struct hidpad_nesusb_data *device = (struct hidpad_nesusb_data*)data;

if (!device)
return;

memcpy(device->data, packet, size);

device->buttons = 0;

pressed_keys = device->data[7] | (device->data[6] << 8) |
(((device->data[7] & 0x30) == 0x30) ? (1 << 16) : 0); /* SELECT+START=HOME */

for (i = 0; i < 17; i ++)
if (button_mapping[i] != NO_BTN)
device->buttons |= (pressed_keys & (1 << i)) ? (UINT64_C(1) << button_mapping[i]) : 0;
}

static void hidpad_nesusb_set_rumble(void *data,
enum retro_rumble_effect effect, uint16_t strength)
{
(void)data;
(void)effect;
(void)strength;
}

const char * hidpad_nesusb_get_name(void *data)
{
(void)data;
/* For now we return a single static name */
return "Generic NES USB Controller";
}

pad_connection_interface_t pad_connection_nesusb = {
hidpad_nesusb_init,
hidpad_nesusb_deinit,
hidpad_nesusb_packet_handler,
hidpad_nesusb_set_rumble,
hidpad_nesusb_get_buttons,
hidpad_nesusb_get_axis,
hidpad_nesusb_get_name,
};
30 changes: 23 additions & 7 deletions input/connect/connect_ps3.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2013-2014 - Jason Fetters
* Copyright (C) 2011-2016 - 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.
Expand Down Expand Up @@ -51,13 +51,20 @@ static void hidpad_ps3_send_control(struct hidpad_ps3_data* device)
report_buffer[11] = 1 << ((device->slot % 4) + 1);
report_buffer[4] = device->motors[1] >> 8;
report_buffer[6] = device->motors[0] >> 8;

#ifdef HAVE_WIIUSB_HID
report_buffer[1] = 0x03; /* send control message type */
device->send_control(device->connection, &report_buffer[1], sizeof(report_buffer)-1);
#else
device->send_control(device->connection, report_buffer, sizeof(report_buffer));
#endif
}

static void* hidpad_ps3_init(void *data, uint32_t slot, send_control_t ptr)
{
#ifdef IOS
#ifdef HAVE_WIIUSB_HID
/* Special command to enable Sixaxis, first byte defines the message type */
static uint8_t magic_data[] = {0x02, 0x42, 0x0c, 0x00, 0x00};
#elif defined(IOS)
/* Magic packet to start reports. */
static uint8_t magic_data[] = {0x53, 0xF4, 0x42, 0x03, 0x00, 0x00};
#endif
Expand All @@ -74,17 +81,18 @@ static void* hidpad_ps3_init(void *data, uint32_t slot, send_control_t ptr)
return NULL;
}

device->connection = connection;
device->connection = connection;
device->slot = slot;
device->send_control = ptr;

#ifdef IOS
device->send_control(device->connection, magic_data, 6);
#if defined(IOS) || defined(HAVE_WIIUSB_HID)
device->send_control(device->connection, magic_data, sizeof(magic_data));
#endif

#ifndef HAVE_WIIUSB_HID
/* Without this, the digital buttons won't be reported. */
hidpad_ps3_send_control(device);

#endif
return device;
}

Expand Down Expand Up @@ -180,11 +188,19 @@ static void hidpad_ps3_set_rumble(void *data,
hidpad_ps3_send_control(device);
}

const char * hidpad_ps3_get_name(void *data)
{
(void)data;
/* For now we return a single static name */
return "PLAYSTATION(R)3 Controller";
}

pad_connection_interface_t pad_connection_ps3 = {
hidpad_ps3_init,
hidpad_ps3_deinit,
hidpad_ps3_packet_handler,
hidpad_ps3_set_rumble,
hidpad_ps3_get_buttons,
hidpad_ps3_get_axis,
hidpad_ps3_get_name,
};
1 change: 1 addition & 0 deletions input/connect/connect_ps4.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,5 @@ pad_connection_interface_t pad_connection_ps4 = {
hidpad_ps4_set_rumble,
hidpad_ps4_get_buttons,
hidpad_ps4_get_axis,
NULL,
};
146 changes: 146 additions & 0 deletions input/connect/connect_snesusb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2013-2014 - Jason Fetters
* Copyright (C) 2011-2016 - 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"

struct hidpad_snesusb_data
{
struct pad_connection* connection;
uint8_t data[64];
uint32_t slot;
uint64_t buttons;
};

static void* hidpad_snesusb_init(void *data, uint32_t slot, send_control_t ptr)
{
struct pad_connection* connection = (struct pad_connection*)data;
struct hidpad_snesusb_data* device = (struct hidpad_snesusb_data*)
calloc(1, sizeof(struct hidpad_snesusb_data));

if (!device)
return NULL;

if (!connection)
{
free(device);
return NULL;
}

device->connection = connection;
device->slot = slot;

return device;
}

static void hidpad_snesusb_deinit(void *data)
{
struct hidpad_snesusb_data *device = (struct hidpad_snesusb_data*)data;

if (device)
free(device);
}

static uint64_t hidpad_snesusb_get_buttons(void *data)
{
struct hidpad_snesusb_data *device = (struct hidpad_snesusb_data*)data;
if (!device)
return 0;
return device->buttons;
}

static int16_t hidpad_snesusb_get_axis(void *data, unsigned axis)
{
int val;
struct hidpad_snesusb_data *device = (struct hidpad_snesusb_data*)data;

if (!device || axis >= 2)
return 0;

val = device->data[1 + axis];
val = (val << 8) - 0x8000;

return (abs(val) > 0x1000) ? val : 0;
}

static void hidpad_snesusb_packet_handler(void *data, uint8_t *packet, uint16_t size)
{
uint32_t i, pressed_keys;
static const uint32_t button_mapping[17] =
{
RETRO_DEVICE_ID_JOYPAD_L,
RETRO_DEVICE_ID_JOYPAD_R,
NO_BTN,
NO_BTN,
RETRO_DEVICE_ID_JOYPAD_SELECT,
RETRO_DEVICE_ID_JOYPAD_START,
NO_BTN,
NO_BTN,
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,
16, /* HOME BUTTON when pressing SELECT+START */
};
struct hidpad_snesusb_data *device = (struct hidpad_snesusb_data*)data;

if (!device)
return;

memcpy(device->data, packet, size);

device->buttons = 0;

pressed_keys = device->data[7] | (device->data[6] << 8) |
(((device->data[7] & 0x30) == 0x30) ? (1 << 16) : 0); /* SELECT+START = MENU TOGGLE */

for (i = 0; i < 17; i ++)
if (button_mapping[i] != NO_BTN)
device->buttons |= (pressed_keys & (1 << i)) ? (UINT64_C(1) << button_mapping[i]) : 0;
}

static void hidpad_snesusb_set_rumble(void *data,
enum retro_rumble_effect effect, uint16_t strength)
{
(void)data;
(void)effect;
(void)strength;
}

const char * hidpad_snesusb_get_name(void *data)
{
(void)data;
/* For now we return a single static name */
return "Generic SNES USB Controller";
}

pad_connection_interface_t pad_connection_snesusb = {
hidpad_snesusb_init,
hidpad_snesusb_deinit,
hidpad_snesusb_packet_handler,
hidpad_snesusb_set_rumble,
hidpad_snesusb_get_buttons,
hidpad_snesusb_get_axis,
hidpad_snesusb_get_name,
};
1 change: 1 addition & 0 deletions input/connect/connect_wii.c
Original file line number Diff line number Diff line change
Expand Up @@ -724,4 +724,5 @@ pad_connection_interface_t pad_connection_wii = {
hidpad_wii_set_rumble,
hidpad_wii_get_buttons,
hidpad_wii_get_axis,
NULL,
};
Loading

0 comments on commit bca4ccb

Please sign in to comment.