Start preparing joypad_connection code for cross-platform purposes

This commit is contained in:
twinaphex 2015-04-02 17:49:32 +02:00
parent 0c2548bd25
commit b9e6b014e3
10 changed files with 219 additions and 236 deletions

View File

@ -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)

View File

@ -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
============================================================ */

View File

@ -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;

View File

@ -17,11 +17,162 @@
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <boolean.h>
#include <retro_miscellaneous.h>
#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.

View File

@ -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

View File

@ -14,9 +14,10 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#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)
{

View File

@ -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

View File

@ -15,12 +15,14 @@
#include <libusb-1.0/libusb.h>
#include <rthreads/rthreads.h>
#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

View File

@ -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;
}

View File

@ -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"