Code clean-up

== DETAILS

Now that I have a working implementation, it's time to tidy up a bit:

- there was no need for the HID subsystem's object data to have a reference
  to the global hid state (since it's global), so removed it.
- refactored the users of that member to use the global state, defining
  reusable macros.
- reorganized the information in *.h files
- removing the hid state also made the constructor changes to the hid driver
  unneeded, so I reverted those changes.

== TESTING
Confirmed clean build. Haven't tested the build yet to make sure everything
still works, though.
This commit is contained in:
gblues 2018-03-31 22:25:30 -07:00
parent 39e4167df6
commit 2cf89feb86
19 changed files with 229 additions and 264 deletions

View File

@ -76,7 +76,7 @@ bool hid_init(hid_driver_instance_t *instance,
return false;
RARCH_LOG("[hid]: initializing HID subsystem driver...\n");
instance->os_driver_data = hid_driver->init(instance);
instance->os_driver_data = hid_driver->init();
if(!instance->os_driver_data)
return false;
@ -122,39 +122,3 @@ void hid_deinit(hid_driver_instance_t *instance)
RARCH_LOG("[hid]: wiping instance data...\n");
memset(instance, 0, sizeof(hid_driver_instance_t));
}
static void hid_device_log_buffer(uint8_t *data, uint32_t len)
{
#if 0
int i, offset;
int padding = len % 0x0F;
uint8_t buf[16];
RARCH_LOG("%d bytes read:\n", len);
for(i = 0, offset = 0; i < len; i++)
{
buf[offset] = data[i];
offset++;
if(offset == 16)
{
offset = 0;
RARCH_LOG("%02x%02x%02x%02x%02x%02x%02x%02x %02x%02x%02x%02x%02x%02x%02x%02x\n",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
}
}
if(padding)
{
for(i = padding; i < 16; i++)
buf[i] = 0xff;
RARCH_LOG("%02x%02x%02x%02x%02x%02x%02x%02x %02x%02x%02x%02x%02x%02x%02x%02x\n",
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
}
RARCH_LOG("=================================\n");
#endif
}

View File

@ -1439,17 +1439,14 @@ static void btstack_hid_free(const void *data)
free(hid);
}
static void *btstack_hid_init(joypad_connection_t *connections)
static void *btstack_hid_init(void)
{
btstack_hid_t *hid = (btstack_hid_t*)calloc(1, sizeof(btstack_hid_t));
if (!hid)
goto error;
if(connections == NULL)
connections = pad_connection_init(MAX_USERS);
hid->slots = connections;
hid->slots = pad_connection_init(MAX_USERS);
if (!hid->slots)
goto error;

View File

@ -854,7 +854,7 @@ static int iohidmanager_hid_manager_set_device_matching(
return 0;
}
static void *iohidmanager_hid_init(joypad_connection_t *connections)
static void *iohidmanager_hid_init(void)
{
iohidmanager_hid_t *hid_apple = (iohidmanager_hid_t*)
calloc(1, sizeof(*hid_apple));
@ -862,10 +862,7 @@ static void *iohidmanager_hid_init(joypad_connection_t *connections)
if (!hid_apple)
goto error;
if (connections == NULL)
connections = pad_connection_init(MAX_USERS);
hid_apple->slots = connections;
hid_apple->slots = pad_connection_init(MAX_USERS);
if (!hid_apple->slots)
goto error;

View File

@ -546,7 +546,7 @@ static void poll_thread(void *data)
}
}
static void *libusb_hid_init(joypad_connection_t *connections)
static void *libusb_hid_init(void)
{
unsigned i, count;
int ret;
@ -578,10 +578,7 @@ static void *libusb_hid_init(joypad_connection_t *connections)
hid->can_hotplug = 0;
#endif
if (connections == NULL)
connections = pad_connection_init(MAX_USERS);
hid->slots = connections;
hid->slots = pad_connection_init(MAX_USERS);
if (!hid->slots)
goto error;

View File

@ -76,10 +76,8 @@ static int16_t null_hid_joypad_axis(void *data, unsigned port, uint32_t joyaxis)
return 0;
}
static void *null_hid_init(hid_driver_instance_t *instance)
static void *null_hid_init(void)
{
(void)instance;
return (null_hid_t*)calloc(1, sizeof(null_hid_t));
}

View File

@ -574,15 +574,15 @@ static void wiiusb_hid_free(const void *data)
free(hid);
}
static void *wiiusb_hid_init(joypad_connection_t *connections)
static void *wiiusb_hid_init(void)
{
joypad_connection_t *connections = NULL;
wiiusb_hid_t *hid = (wiiusb_hid_t*)calloc(1, sizeof(*hid));
if (!hid)
goto error;
if(connections == NULL)
connections = pad_connection_init(MAX_USERS);
connections = pad_connection_init(MAX_USERS);
if (!connections)
goto error;

View File

@ -14,7 +14,7 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <wiiu/pad_driver.h>
#include "../../wiiu/input/wiiu_input.h"
#include "wiiu_dbg.h"
@ -40,10 +40,10 @@ static input_device_driver_t *get_driver_for_pad(unsigned pad)
{
if(wpad_driver.query_pad(pad))
return &wpad_driver;
/*
if(kpad_driver.query_pad(pad))
return &kpad_driver;
*/
#ifdef WIIU_HID
return &hidpad_driver;
#else
@ -135,7 +135,7 @@ static void wiiu_joypad_poll(void)
static const char* wiiu_joypad_name(unsigned pad)
{
if(!wiiu_joypad_query_pad(pad))
return "Snuffleupagus";
return "N/A";
return pad_drivers[pad]->name(pad);
}

View File

@ -35,7 +35,7 @@
struct hid_driver
{
void *(*init)(hid_driver_instance_t *);
void *(*init)(void);
bool (*query_pad)(void *handle, unsigned pad);
void (*free)(const void *handle);
bool (*button)(void *handle, unsigned pad, uint16_t button);
@ -62,6 +62,8 @@ struct hid_driver
hid_instance.os_driver_data, pad)
#define HID_POLL() hid_instance.os_driver->poll( \
hid_instance.os_driver_data)
#define HID_MAX_SLOT() hid_instance.max_slot
#define HID_PAD_CONNECTION_PTR(slot) &(hid_instance.pad_list[(slot)])

View File

@ -29,7 +29,7 @@
#endif
#ifdef WIIU
#include <wiiu/pad_driver.h>
#include <wiiu/pad_strings.h>
#endif
#define DECL_BTN(btn, bind) "input_" #btn "_btn = " #bind "\n"

View File

@ -1,186 +0,0 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2014-2017 - Ali Bouhlel
* 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/>.
*/
#ifndef __PAD_DRIVER__H
#define __PAD_DRIVER__H
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif /* HAVE_CONFIG_H */
#include <string.h>
#include <malloc.h>
#include <unistd.h>
#include <wiiu/os.h>
#include <wiiu/syshid.h>
#include <wiiu/vpad.h>
#include <wiiu/kpad.h>
#include <string.h>
#include "../../input/input_driver.h"
#include "../../input/common/hid/hid_device_driver.h"
#include "../../tasks/tasks_internal.h"
#include "../../input/connect/joypad_connection.h"
#include "../../retroarch.h"
#include "../../verbosity.h"
#include "../../command.h"
#include "../../gfx/video_driver.h"
/**
* Magic button sequence that triggers an exit. Useful for if the visuals are
* corrupted, but won't work in the case of a hard lock.
*/
#define PANIC_BUTTON_MASK (VPAD_BUTTON_R | VPAD_BUTTON_L | VPAD_BUTTON_STICK_R | VPAD_BUTTON_STICK_L)
/**
* Applies a standard transform to the Wii U gamepad's analog stick.
* No idea where 0x7ff0 comes from.
*/
#define WIIU_ANALOG_FACTOR 0x7ff0
#define WIIU_READ_STICK(stick) ((stick) * WIIU_ANALOG_FACTOR)
/**
* the wiimote driver uses these to delimit which pads correspond to the
* wiimotes.
*/
#define PAD_GAMEPAD 0
#define WIIU_WIIMOTE_CHANNELS 4
/**
* These are used by the wiimote driver to identify the wiimote configuration
* attached to the channel.
*/
/* wiimote with Wii U Pro controller */
#define WIIMOTE_TYPE_PRO 0x1f
/* wiimote with Classic Controller */
#define WIIMOTE_TYPE_CLASSIC 0x02
/* wiimote with nunchuk */
#define WIIMOTE_TYPE_NUNCHUK 0x01
/* wiimote plus (no accessory attached) */
#define WIIMOTE_TYPE_WIIPLUS 0x00
/* wiimote not attached on this channel */
#define WIIMOTE_TYPE_NONE 0xFD
/**
* These are used to map pad names to controller mappings. You can
* change these relatively free-form.
*/
#define PAD_NAME_WIIU_GAMEPAD "WiiU Gamepad"
#define PAD_NAME_WIIU_PRO "WiiU Pro Controller"
#define PAD_NAME_WIIMOTE "Wiimote Controller"
#define PAD_NAME_NUNCHUK "Wiimote+Nunchuk Controller"
#define PAD_NAME_CLASSIC "Classic Controller"
#define PAD_NAME_HID "HID Controller"
/**
* The Wii U gamepad and wiimotes have 3 sets of x/y axes. The third
* is used by the gamepad for the touchpad driver; the wiimotes is
* currently unimplemented, but could be used for future IR pointer
* support.
*/
#define WIIU_DEVICE_INDEX_TOUCHPAD 2
typedef struct _axis_data axis_data;
struct _axis_data {
int32_t axis;
bool is_negative;
};
typedef struct _wiiu_pad_functions wiiu_pad_functions_t;
struct _wiiu_pad_functions {
int16_t (*get_axis_value)(int32_t axis, int16_t state[3][2], bool is_negative);
void (*set_axis_value)(int16_t state[3][2], int16_t left_x, int16_t left_y,
int16_t right_x, int16_t right_y, int16_t touch_x, int16_t touch_y);
void (*read_axis_data)(uint32_t axis, axis_data *data);
void (*connect)(unsigned pad, input_device_driver_t *driver);
};
/**
* HID driver data structures
*/
typedef struct wiiu_hid {
/* used to register for HID notifications */
HIDClient *client;
/* pointer to HID driver state */
hid_driver_instance_t *driver;
/* size of connections list */
unsigned connections_size;
/* thread state data for HID polling thread */
OSThread *polling_thread;
/* stack space for polling thread */
void *polling_thread_stack;
/* watch variable to tell the polling thread to terminate */
volatile bool polling_thread_quit;
} wiiu_hid_t;
typedef struct wiiu_adapter wiiu_adapter_t;
struct wiiu_adapter {
wiiu_adapter_t *next;
hid_device_t *driver;
void *driver_handle;
wiiu_hid_t *hid;
uint8_t state;
uint8_t *rx_buffer;
int32_t rx_size;
uint8_t *tx_buffer;
int32_t tx_size;
uint32_t handle;
uint8_t interface_index;
};
typedef struct wiiu_attach wiiu_attach_event;
struct wiiu_attach {
wiiu_attach_event *next;
hid_device_t *driver;
uint32_t type;
uint32_t handle;
uint16_t vendor_id;
uint16_t product_id;
uint8_t interface_index;
uint8_t is_keyboard;
uint8_t is_mouse;
uint16_t max_packet_size_rx;
uint16_t max_packet_size_tx;
};
typedef struct _wiiu_event_list wiiu_event_list;
typedef struct _wiiu_adapter_list wiiu_adapter_list;
struct _wiiu_event_list {
OSFastMutex lock;
wiiu_attach_event *list;
};
struct _wiiu_adapter_list {
OSFastMutex lock;
wiiu_adapter_t *list;
};
extern wiiu_pad_functions_t pad_functions;
extern input_device_driver_t wiiu_joypad;
extern input_device_driver_t wpad_driver;
extern input_device_driver_t kpad_driver;
extern input_device_driver_t hidpad_driver;
extern hid_driver_t wiiu_hid;
#endif /* __PAD_DRIVER__H */

View File

@ -0,0 +1,32 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2014-2017 - Ali Bouhlel
* 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/>.
*/
#ifndef __PAD_DRIVER__H
#define __PAD_DRIVER__H
/**
* These are used to map pad names to controller mappings. You can
* change these relatively free-form.
*/
#define PAD_NAME_WIIU_GAMEPAD "WiiU Gamepad"
#define PAD_NAME_WIIU_PRO "WiiU Pro Controller"
#define PAD_NAME_WIIMOTE "Wiimote Controller"
#define PAD_NAME_NUNCHUK "Wiimote+Nunchuk Controller"
#define PAD_NAME_CLASSIC "Classic Controller"
#define PAD_NAME_HID "HID Controller"
#endif /* __PAD_DRIVER__H */

View File

@ -14,9 +14,8 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <wiiu/pad_driver.h>
#include "../../input/include/hid_driver.h"
#include "../../input/common/hid/hid_device_driver.h"
#include "wiiu_input.h"
#include "wiiu_hid.h"
static bool hidpad_init(void *data);
static bool hidpad_query_pad(unsigned pad);

View File

@ -20,7 +20,7 @@
* controllers.
*/
#include <wiiu/pad_driver.h>
#include "wiiu_input.h"
static bool kpad_init(void *data);
static bool kpad_query_pad(unsigned pad);

View File

@ -14,7 +14,7 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <wiiu/pad_driver.h>
#include "wiiu_input.h"
enum wiiu_pad_axes {
AXIS_LEFT_ANALOG_X,

View File

@ -22,10 +22,10 @@ static wiiu_adapter_list adapters;
static bool wiiu_hid_joypad_query(void *data, unsigned slot)
{
wiiu_hid_t *hid = (wiiu_hid_t *)data;
if (!hid || !hid->driver)
if (!hid)
return false;
return slot < hid->driver->max_slot;
return slot < HID_MAX_SLOT();
}
static joypad_connection_t *get_pad(wiiu_hid_t *hid, unsigned slot)
@ -33,7 +33,7 @@ static joypad_connection_t *get_pad(wiiu_hid_t *hid, unsigned slot)
if(!wiiu_hid_joypad_query(hid, slot))
return NULL;
joypad_connection_t *result = &(hid->driver->pad_list[slot]);
joypad_connection_t *result = HID_PAD_CONNECTION_PTR(slot);
if(!result || !result->connected || !result->iface || !result->data)
return NULL;
@ -90,7 +90,7 @@ static int16_t wiiu_hid_joypad_axis(void *data, unsigned slot, uint32_t joyaxis)
return pad->iface->get_axis(pad->data, joyaxis);
}
static void *wiiu_hid_init(hid_driver_instance_t *driver)
static void *wiiu_hid_init(void)
{
RARCH_LOG("[hid]: initializing HID subsystem\n");
wiiu_hid_t *hid = new_hid();
@ -99,8 +99,6 @@ static void *wiiu_hid_init(hid_driver_instance_t *driver)
if (!hid || !client)
goto error;
hid->driver = driver;
wiiu_hid_init_lists();
start_polling_thread(hid);
if (!hid->polling_thread)

View File

@ -17,8 +17,9 @@
#ifndef __WIIU_HID__H
#define __WIIU_HID__H
#include <wiiu/pad_driver.h>
#include "../../input/include/hid_driver.h"
#include "wiiu_hid_types.h"
#include "wiiu_input.h"
#define DEVICE_UNUSED 0
#define DEVICE_USED 1
@ -28,6 +29,70 @@
#define ADAPTER_STATE_READING 2
#define ADAPTER_STATE_DONE 3
struct wiiu_hid {
/* used to register for HID notifications */
HIDClient *client;
/* thread state data for the HID input polling thread */
OSThread *polling_thread;
/* stack space for polling thread */
void *polling_thread_stack;
/* watch variable for telling the polling thread to terminate */
volatile bool polling_thread_quit;
};
/**
* Each HID device attached to the WiiU gets its own adapter, which
* connects the HID subsystem with the HID device driver.
*/
struct wiiu_adapter {
wiiu_adapter_t *next;
hid_device_t *driver;
void *driver_handle;
wiiu_hid_t *hid;
uint8_t state;
uint8_t *rx_buffer;
int32_t rx_size;
uint8_t *tx_buffer;
int32_t tx_size;
uint32_t handle;
uint8_t interface_index;
};
/**
* When a HID device is connected, the OS generates an attach
* event; the attach event handler translate them into these
* structures.
*/
struct wiiu_attach {
wiiu_attach_event *next;
hid_device_t *driver;
uint32_t type;
uint32_t handle;
uint16_t vendor_id;
uint16_t product_id;
uint8_t interface_index;
uint8_t is_keyboard;
uint8_t is_mouse;
uint16_t max_packet_size_rx;
uint16_t max_packet_size_tx;
};
struct _wiiu_event_list {
OSFastMutex lock;
wiiu_attach_event *list;
};
struct _wiiu_adapter_list {
OSFastMutex lock;
wiiu_adapter_t *list;
};
extern wiiu_pad_functions_t pad_functions;
extern input_device_driver_t wiiu_joypad;
extern input_device_driver_t wpad_driver;
extern input_device_driver_t kpad_driver;
extern input_device_driver_t hidpad_driver;
extern hid_driver_t wiiu_hid;
static void *alloc_zeroed(size_t alignment, size_t size);
static OSThread *new_thread(void);

View File

@ -0,0 +1,28 @@
/* 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/>.
*/
#ifndef __WIIU_HID_TYPES__H
#define __WIIU_HID_TYPES__H
typedef struct wiiu_hid wiiu_hid_t;
typedef struct wiiu_adapter wiiu_adapter_t;
typedef struct wiiu_attach wiiu_attach_event;
typedef struct _wiiu_event_list wiiu_event_list;
typedef struct _wiiu_adapter_list wiiu_adapter_list;
typedef struct _axis_data axis_data;
typedef struct _wiiu_pad_functions wiiu_pad_functions_t;
#endif /* __WIIU_HID_TYPES__H */

72
wiiu/input/wiiu_input.h Normal file
View File

@ -0,0 +1,72 @@
/* 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/>.
*/
#ifndef __WIIU_INPUT__H
#define __WIIU_INPUT__H
#include "wiiu_hid_types.h"
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif /* HAVE_CONFIG_H */
#include <string.h>
#include <malloc.h>
#include <unistd.h>
#include <wiiu/os.h>
#include <wiiu/syshid.h>
#include <wiiu/vpad.h>
#include <wiiu/kpad.h>
#include <wiiu/pad_strings.h>
#include "../../input/input_driver.h"
#include "../../input/common/hid/hid_device_driver.h"
#include "../../tasks/tasks_internal.h"
#include "../../input/connect/joypad_connection.h"
#include "../../retroarch.h"
#include "../../verbosity.h"
#include "../../command.h"
#include "../../gfx/video_driver.h"
#include "wiiu_hid.h"
#define WIIMOTE_TYPE_WIIPLUS 0x00
#define WIIMOTE_TYPE_NUNCHUK 0x01
#define WIIMOTE_TYPE_CLASSIC 0x02
#define WIIMOTE_TYPE_PRO 0x1f
#define WIIMOTE_TYPE_NONE 0xfd
#define WIIU_DEVICE_INDEX_TOUCHPAD 2
#define PAD_GAMEPAD 0
#define WIIU_WIIMOTE_CHANNELS 4
#define WIIU_ANALOG_FACTOR 0x7ff0
#define WIIU_READ_STICK(stick) ((stick) * WIIU_ANALOG_FACTOR)
struct _axis_data {
int32_t axis;
bool is_negative;
};
struct _wiiu_pad_functions {
int16_t (*get_axis_value)(int32_t axis, int16_t state[3][2], bool is_negative);
void (*set_axis_value)(int16_t state[3][2], int16_t left_x, int16_t left_y,
int16_t right_x, int16_t right_y, int16_t touch_x, int16_t touch_y);
void (*read_axis_data)(uint32_t axis, axis_data *data);
void (*connect)(unsigned pad, input_device_driver_t *driver);
};
#endif /* __WIIU_INPUT__H */

View File

@ -21,7 +21,9 @@
* - For HID controllers, see hid_driver.c
*/
#include <wiiu/pad_driver.h>
#include "wiiu_input.h"
#define PANIC_BUTTON_MASK (VPAD_BUTTON_R | VPAD_BUTTON_L | VPAD_BUTTON_STICK_R | VPAD_BUTTON_STICK_L)
static bool ready = false;
static uint64_t button_state = 0;