diff --git a/Makefile.common b/Makefile.common index 0e4846db3f..164085f4b1 100644 --- a/Makefile.common +++ b/Makefile.common @@ -398,6 +398,15 @@ ifeq ($(HAVE_LIBUSB), 1) OBJ += input/drivers_hid/libusb_hid.o LIBS += -lusb-1.0 JOYCONFIG_LIBS += -lusb-1.0 + HAVE_HID = 1 +endif + +ifeq ($(HAVE_HID), 1) + DEFINES += -DHAVE_HID + OBJ += input/connect/joypad_connection.o \ + input/connect/connect_ps3.o \ + input/connect/connect_ps4.o \ + input/connect/connect_wii.o endif ifeq ($(HAVE_PARPORT), 1) diff --git a/griffin/griffin.c b/griffin/griffin.c index 47a74bd9ea..911c55ba6a 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -341,24 +341,6 @@ INPUT #include "../input/drivers/rwebinput_input.c" #endif -#include "../input/drivers_joypad/hid_joypad.c" - -#include "../input/drivers_hid/null_hid.c" -#ifdef HAVE_LIBUSB -#include "../input/drivers_hid/libusb_hid.c" -#endif - -#ifdef HAVE_HID -#include "../input/drivers_hid/apple_hid.c" -#endif - -#if defined(__APPLE__) -#include "../input/connect/joypad_connection.c" -#include "../input/connect/connect_ps3.c" -#include "../input/connect/connect_ps4.c" -#include "../input/connect/connect_wii.c" - - #ifdef IOS #include "../apple/iOS/bluetooth/btdynamic.c" #include "../apple/iOS/bluetooth/btpad.c" @@ -366,8 +348,6 @@ INPUT #include "../input/drivers_joypad/apple_joypad_ios.c" #endif -#endif - #ifdef HAVE_DINPUT #include "../input/drivers/dinput.c" #endif @@ -393,6 +373,26 @@ INPUT #include "../input/drivers/nullinput.c" #include "../input/drivers_joypad/nullinput_joypad.c" +/*============================================================ +INPUT (HID) +============================================================ */ +#include "../input/drivers_joypad/hid_joypad.c" + +#include "../input/drivers_hid/null_hid.c" + +#if defined(HAVE_LIBUSB) +#include "../input/drivers_hid/libusb_hid.c" +#elif defined(__APPLE__) +#include "../input/drivers_hid/apple_hid.c" +#endif + +#ifdef HAVE_HID +#include "../input/connect/joypad_connection.c" +#include "../input/connect/connect_ps3.c" +#include "../input/connect/connect_ps4.c" +#include "../input/connect/connect_wii.c" +#endif + /*============================================================ KEYBOARD EVENT ============================================================ */ diff --git a/input/connect/connect_ps4.c b/input/connect/connect_ps4.c index f48209f020..95d37bc095 100644 --- a/input/connect/connect_ps4.c +++ b/input/connect/connect_ps4.c @@ -137,13 +137,13 @@ static bool hidpad_ps4_check_dpad(struct ps4 *rpt, unsigned id) switch (id) { case RETRO_DEVICE_ID_JOYPAD_UP: - return rpt->btn.dpad == DPAD_LEFT_UP || rpt->btn.dpad == DPAD_UP || rpt->btn.dpad == DPAD_UP_RIGHT; + return (rpt->btn.dpad == DPAD_LEFT_UP) || (rpt->btn.dpad == DPAD_UP) || (rpt->btn.dpad == DPAD_UP_RIGHT); case RETRO_DEVICE_ID_JOYPAD_RIGHT: - return rpt->btn.dpad == DPAD_UP_RIGHT || rpt->btn.dpad == DPAD_RIGHT || rpt->btn.dpad == DPAD_RIGHT_DOWN; + return (rpt->btn.dpad == DPAD_UP_RIGHT) || (rpt->btn.dpad == DPAD_RIGHT) || (rpt->btn.dpad == DPAD_RIGHT_DOWN); case RETRO_DEVICE_ID_JOYPAD_DOWN: - return rpt->btn.dpad == DPAD_RIGHT_DOWN | rpt->btn.dpad == DPAD_DOWN || rpt->btn.dpad == DPAD_DOWN_LEFT; + return (rpt->btn.dpad == DPAD_RIGHT_DOWN) | (rpt->btn.dpad == DPAD_DOWN) || (rpt->btn.dpad == DPAD_DOWN_LEFT); case RETRO_DEVICE_ID_JOYPAD_LEFT: - return rpt->btn.dpad == DPAD_DOWN_LEFT || rpt->btn.dpad == DPAD_LEFT || rpt->btn.dpad == DPAD_LEFT_UP; + return (rpt->btn.dpad == DPAD_DOWN_LEFT) || (rpt->btn.dpad == DPAD_LEFT) || (rpt->btn.dpad == DPAD_LEFT_UP); } return false; diff --git a/input/connect/connect_wii.c b/input/connect/connect_wii.c index 2394ce3a2a..cf02b26a86 100644 --- a/input/connect/connect_wii.c +++ b/input/connect/connect_wii.c @@ -17,11 +17,162 @@ #include #include #include +#include #include -#include "connect_wii.h" #include "joypad_connection.h" +typedef unsigned char byte; +typedef char sbyte; + +/* Convert to big endian */ +#define BIG_ENDIAN_LONG(i) (htonl(i)) +#define BIG_ENDIAN_SHORT(i) (htons(i)) + +#define absf(x) ((x >= 0) ? (x) : (x * -1.0f)) +#define diff_f(x, y) ((x >= y) ? (absf(x - y)) : (absf(y - x))) + +/* wiimote state flags*/ +#define WIIMOTE_STATE_DEV_FOUND 0x0001 +#define WIIMOTE_STATE_HANDSHAKE 0x0002 /* Actual connection exists but no handshake yet */ +#define WIIMOTE_STATE_HANDSHAKE_COMPLETE 0x0004 +#define WIIMOTE_STATE_CONNECTED 0x0008 +#define WIIMOTE_STATE_EXP 0x0040 + +/* Communication channels */ + +#define WM_SET_REPORT 0x50 + +/* Commands */ +#define WM_CMD_LED 0x11 +#define WM_CMD_REPORT_TYPE 0x12 +#define WM_CMD_RUMBLE 0x13 +#define WM_CMD_IR 0x13 +#define WM_CMD_CTRL_STATUS 0x15 +#define WM_CMD_WRITE_DATA 0x16 +#define WM_CMD_READ_DATA 0x17 +#define WM_CMD_IR_2 0x1A + +/* Input report IDs */ +#define WM_RPT_CTRL_STATUS 0x20 +#define WM_RPT_READ 0x21 +#define WM_RPT_WRITE 0x22 +#define WM_RPT_BTN 0x30 +#define WM_RPT_BTN_ACC 0x31 +#define WM_RPT_BTN_ACC_IR 0x33 +#define WM_RPT_BTN_EXP 0x34 +#define WM_RPT_BTN_ACC_EXP 0x35 +#define WM_RPT_BTN_IR_EXP 0x36 +#define WM_RPT_BTN_ACC_IR_EXP 0x37 + +#define WM_BT_INPUT 0x01 +#define WM_BT_OUTPUT 0x02 + +/* controller status stuff */ +#define WM_MAX_BATTERY_CODE 0xC8 + +#define EXP_ID_CODE_CLASSIC_CONTROLLER 0x9A1EFDFD + +/* offsets in wiimote memory */ +#define WM_MEM_OFFSET_CALIBRATION 0x16 +#define WM_EXP_MEM_BASE 0x04A40000 +#define WM_EXP_MEM_ENABLE 0x04A40040 +#define WM_EXP_MEM_CALIBR 0x04A40020 + +#define EXP_HANDSHAKE_LEN 224 + +/* controller status flags for the first message byte */ +/* bit 1 is unknown */ +#define WM_CTRL_STATUS_BYTE1_ATTACHMENT 0x02 +#define WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED 0x04 +#define WM_CTRL_STATUS_BYTE1_IR_ENABLED 0x08 +#define WM_CTRL_STATUS_BYTE1_LED_1 0x10 +#define WM_CTRL_STATUS_BYTE1_LED_2 0x20 +#define WM_CTRL_STATUS_BYTE1_LED_3 0x40 +#define WM_CTRL_STATUS_BYTE1_LED_4 0x80 + +/* LED bit masks */ +#define WIIMOTE_LED_NONE 0x00 +#define WIIMOTE_LED_1 0x10 +#define WIIMOTE_LED_2 0x20 +#define WIIMOTE_LED_3 0x40 +#define WIIMOTE_LED_4 0x80 + +/* button masks */ +#define WIIMOTE_BUTTON_ALL 0x1F9F +#define CLASSIC_CTRL_BUTTON_ALL 0xFEFF + +/* expansion codes */ +#define EXP_NONE 0 +#define EXP_CLASSIC 2 + +typedef struct axis_t +{ + bool has_center; + + byte min; + byte center; + byte max; + byte raw_value; + float value; +} axis_t; + +typedef struct joystick_t +{ + axis_t x; + axis_t y; +} joystick_t; + +typedef struct classic_ctrl_t +{ + short btns; + struct joystick_t ljs; + struct joystick_t rjs; +} classic_ctrl_t; + +/* + * Generic expansion device plugged into wiimote. + */ +typedef struct expansion_t +{ + /* Type of expansion attached. */ + int type; + + union { + struct classic_ctrl_t classic; + } cc; +} expansion_t; + +/* Wiimote structure. */ +typedef struct wiimote_t +{ + /* User specified ID. */ + int unid; + + struct pad_connection* connection; + send_control_t send_control; + + /* Various state flags. */ + int state; + /* Currently lit LEDs. */ + byte leds; + /* Battery level. */ + float battery_level; + /* The state of the connection handshake. */ + byte handshake_state; + /* Wiimote expansion device. */ + struct expansion_t exp; + /* What buttons have just been pressed. */ + unsigned short btns; +} wiimote; + +/* Macro to manage states */ +#define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s)) +#define WIIMOTE_ENABLE_STATE(wm, s) (wm->state |= (s)) +#define WIIMOTE_DISABLE_STATE(wm, s) (wm->state &= ~(s)) +#define WIIMOTE_TOGGLE_STATE(wm, s) ((wm->state & (s)) ? WIIMOTE_DISABLE_STATE(wm, s) : WIIMOTE_ENABLE_STATE(wm, s)) +#define WIIMOTE_IS_CONNECTED(wm) (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED)) + #ifndef NO_BAKED_IN_WIIMOTE /* * Send a packet to the wiimote. diff --git a/input/connect/connect_wii.h b/input/connect/connect_wii.h deleted file mode 100644 index e831be0378..0000000000 --- a/input/connect/connect_wii.h +++ /dev/null @@ -1,203 +0,0 @@ -/* - * This file is part of iMAME4all. - * - * Copyright (C) 2010 David Valdeita (Seleuco) - * - * based on: - * - * wiiuse - * - * Written By: - * Michael Laforest < para > - * Email: < thepara (--AT--) g m a i l [--DOT--] com > - * - * Copyright 2006-2007 - * - * This program 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 Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program 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 this program; if not, write to the Free Software - * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - * In addition, as a special exception, Seleuco - * gives permission to link the code of this program with - * the MAME library (or with modified versions of MAME that use the - * same license as MAME), and distribute linked combinations including - * the two. You must obey the GNU General Public License in all - * respects for all of the code used other than MAME. If you modify - * this file, you may extend this exception to your version of the - * file, but you are not obligated to do so. If you do not wish to - * do so, delete this exception statement from your version. - */ - -#ifndef __WIIMOTE_H__ -#define __WIIMOTE_H__ - -#if defined(__cplusplus) -extern "C" { -#endif - -typedef unsigned char byte; -typedef char sbyte; - -/* Convert to big endian */ -#define BIG_ENDIAN_LONG(i) (htonl(i)) -#define BIG_ENDIAN_SHORT(i) (htons(i)) - -#define absf(x) ((x >= 0) ? (x) : (x * -1.0f)) -#define diff_f(x, y) ((x >= y) ? (absf(x - y)) : (absf(y - x))) - -/* wiimote state flags*/ -#define WIIMOTE_STATE_DEV_FOUND 0x0001 -#define WIIMOTE_STATE_HANDSHAKE 0x0002 /* Actual connection exists but no handshake yet */ -#define WIIMOTE_STATE_HANDSHAKE_COMPLETE 0x0004 -#define WIIMOTE_STATE_CONNECTED 0x0008 -#define WIIMOTE_STATE_EXP 0x0040 - -/* Communication channels */ - -#define WM_SET_REPORT 0x50 - -/* Commands */ -#define WM_CMD_LED 0x11 -#define WM_CMD_REPORT_TYPE 0x12 -#define WM_CMD_RUMBLE 0x13 -#define WM_CMD_IR 0x13 -#define WM_CMD_CTRL_STATUS 0x15 -#define WM_CMD_WRITE_DATA 0x16 -#define WM_CMD_READ_DATA 0x17 -#define WM_CMD_IR_2 0x1A - -/* Input report IDs */ -#define WM_RPT_CTRL_STATUS 0x20 -#define WM_RPT_READ 0x21 -#define WM_RPT_WRITE 0x22 -#define WM_RPT_BTN 0x30 -#define WM_RPT_BTN_ACC 0x31 -#define WM_RPT_BTN_ACC_IR 0x33 -#define WM_RPT_BTN_EXP 0x34 -#define WM_RPT_BTN_ACC_EXP 0x35 -#define WM_RPT_BTN_IR_EXP 0x36 -#define WM_RPT_BTN_ACC_IR_EXP 0x37 - -#define WM_BT_INPUT 0x01 -#define WM_BT_OUTPUT 0x02 - -/* controller status stuff */ -#define WM_MAX_BATTERY_CODE 0xC8 - -#define EXP_ID_CODE_CLASSIC_CONTROLLER 0x9A1EFDFD - -/* offsets in wiimote memory */ -#define WM_MEM_OFFSET_CALIBRATION 0x16 -#define WM_EXP_MEM_BASE 0x04A40000 -#define WM_EXP_MEM_ENABLE 0x04A40040 -#define WM_EXP_MEM_CALIBR 0x04A40020 - -#define EXP_HANDSHAKE_LEN 224 - -/* controller status flags for the first message byte */ -/* bit 1 is unknown */ -#define WM_CTRL_STATUS_BYTE1_ATTACHMENT 0x02 -#define WM_CTRL_STATUS_BYTE1_SPEAKER_ENABLED 0x04 -#define WM_CTRL_STATUS_BYTE1_IR_ENABLED 0x08 -#define WM_CTRL_STATUS_BYTE1_LED_1 0x10 -#define WM_CTRL_STATUS_BYTE1_LED_2 0x20 -#define WM_CTRL_STATUS_BYTE1_LED_3 0x40 -#define WM_CTRL_STATUS_BYTE1_LED_4 0x80 - -/* LED bit masks */ -#define WIIMOTE_LED_NONE 0x00 -#define WIIMOTE_LED_1 0x10 -#define WIIMOTE_LED_2 0x20 -#define WIIMOTE_LED_3 0x40 -#define WIIMOTE_LED_4 0x80 - -/* button masks */ -#define WIIMOTE_BUTTON_ALL 0x1F9F -#define CLASSIC_CTRL_BUTTON_ALL 0xFEFF - -/* expansion codes */ -#define EXP_NONE 0 -#define EXP_CLASSIC 2 - -typedef struct axis_t -{ - bool has_center; - - byte min; - byte center; - byte max; - byte raw_value; - float value; -} axis_t; - -typedef struct joystick_t -{ - axis_t x; - axis_t y; -} joystick_t; - -typedef struct classic_ctrl_t -{ - short btns; - struct joystick_t ljs; - struct joystick_t rjs; -} classic_ctrl_t; - -/* - * Generic expansion device plugged into wiimote. - */ -typedef struct expansion_t -{ - /* Type of expansion attached. */ - int type; - - union { - struct classic_ctrl_t classic; - } cc; -} expansion_t; - -/* Wiimote structure. */ -typedef struct wiimote_t -{ - /* User specified ID. */ - int unid; - - struct pad_connection* connection; - send_control_t send_control; - - /* Various state flags. */ - int state; - /* Currently lit LEDs. */ - byte leds; - /* Battery level. */ - float battery_level; - /* The state of the connection handshake. */ - byte handshake_state; - /* Wiimote expansion device. */ - struct expansion_t exp; - /* What buttons have just been pressed. */ - unsigned short btns; -} wiimote; - -/* Macro to manage states */ -#define WIIMOTE_IS_SET(wm, s) ((wm->state & (s)) == (s)) -#define WIIMOTE_ENABLE_STATE(wm, s) (wm->state |= (s)) -#define WIIMOTE_DISABLE_STATE(wm, s) (wm->state &= ~(s)) -#define WIIMOTE_TOGGLE_STATE(wm, s) ((wm->state & (s)) ? WIIMOTE_DISABLE_STATE(wm, s) : WIIMOTE_ENABLE_STATE(wm, s)) -#define WIIMOTE_IS_CONNECTED(wm) (WIIMOTE_IS_SET(wm, WIIMOTE_STATE_CONNECTED)) - -#if defined(__cplusplus) -} -#endif - -#endif diff --git a/input/connect/joypad_connection.c b/input/connect/joypad_connection.c index 414b5281f0..6de5d8503d 100644 --- a/input/connect/joypad_connection.c +++ b/input/connect/joypad_connection.c @@ -14,9 +14,10 @@ * If not, see . */ +#include #include "joypad_connection.h" -static int find_vacant_pad(joypad_connection_t *joyconn) +static int pad_connection_find_vacant_pad(joypad_connection_t *joyconn) { unsigned i; @@ -59,7 +60,7 @@ void *pad_connection_init(unsigned pads) int32_t pad_connection_pad_init(joypad_connection_t *joyconn, const char* name, void *data, send_control_t ptr) { - int pad = find_vacant_pad(joyconn); + int pad = pad_connection_find_vacant_pad(joyconn); if (pad != -1) { @@ -101,7 +102,7 @@ int32_t pad_connection_pad_init(joypad_connection_t *joyconn, int32_t apple_joypad_connect_gcapi(joypad_connection_t *joyconn) { - int pad = find_vacant_pad(joyconn); + int pad = pad_connection_find_vacant_pad(joyconn); if (pad >= 0 && pad < MAX_USERS) { diff --git a/input/drivers_hid/apple_hid.c b/input/drivers_hid/apple_hid.c index 880984838a..4bf117cb15 100644 --- a/input/drivers_hid/apple_hid.c +++ b/input/drivers_hid/apple_hid.c @@ -22,8 +22,8 @@ typedef struct apple_hid { - IOHIDManagerRef ptr; - joypad_connection_t *slots; + IOHIDManagerRef ptr; + joypad_connection_t *slots; } apple_hid_t; struct apple_hid_adapter diff --git a/input/drivers_hid/libusb_hid.c b/input/drivers_hid/libusb_hid.c index 87c585e057..e3601caa20 100644 --- a/input/drivers_hid/libusb_hid.c +++ b/input/drivers_hid/libusb_hid.c @@ -15,12 +15,14 @@ #include #include +#include "../connect/joypad_connection.h" #include "../../driver.h" #include "../input_hid_driver.h" typedef struct libusb_hid { libusb_hotplug_callback_handle hp; + joypad_connection_t *slots; } libusb_hid_t; struct libusb_adapter diff --git a/libretro-common/include/retro_endianness.h b/libretro-common/include/retro_endianness.h index c3db85e13d..d07227eca2 100644 --- a/libretro-common/include/retro_endianness.h +++ b/libretro-common/include/retro_endianness.h @@ -75,6 +75,15 @@ static INLINE uint32_t swap_if_big32(uint32_t val) ((val << 8) & 0xFF0000) | (val << 24); } +static INLINE uint32_t swap_little32(uint32_t val) +{ + return + (val >> 24) + | ((val >> 8) & 0xFF00) + | ((val << 8) & 0xFF0000) + | (val << 24); +} + /** * swap_if_little32: * @val : unsigned 32-bit value @@ -87,11 +96,15 @@ static INLINE uint32_t swap_if_big32(uint32_t val) static INLINE uint32_t swap_if_little32(uint32_t val) { if (is_little_endian()) - return (val >> 24) | ((val >> 8) & 0xFF00) | - ((val << 8) & 0xFF0000) | (val << 24); + return swap_little32(val); return val; } +static INLINE uint16_t swap_big16(uint16_t val) +{ + return (val >> 8) | (val << 8); +} + /** * swap_if_big16: * @val : unsigned 16-bit value @@ -105,6 +118,11 @@ static INLINE uint16_t swap_if_big16(uint16_t val) { if (is_little_endian()) return val; + return swap_big16(val); +} + +static INLINE uint16_t swap_little16(uint16_t val) +{ return (val >> 8) | (val << 8); } @@ -120,7 +138,7 @@ static INLINE uint16_t swap_if_big16(uint16_t val) static INLINE uint16_t swap_if_little16(uint16_t val) { if (is_little_endian()) - return (val >> 8) | (val << 8); + return swap_little16(val); return val; } diff --git a/tools/retroarch-joyconfig-griffin.c b/tools/retroarch-joyconfig-griffin.c index 404aaf9e50..e830075563 100644 --- a/tools/retroarch-joyconfig-griffin.c +++ b/tools/retroarch-joyconfig-griffin.c @@ -53,6 +53,11 @@ #ifdef HAVE_LIBUSB #include "../input/drivers_hid/libusb_hid.c" #include "../libretro-common/rthreads/rthreads.c" + +#include "../input/connect/connect_ps3.c" +#include "../input/connect/connect_ps4.c" +#include "../input/connect/connect_wii.c" +#include "../input/connect/joypad_connection.c" #endif #include "../input/drivers_joypad/hid_joypad.c"