Move inptu state to android_joypad.c

This commit is contained in:
twinaphex 2015-11-29 16:18:29 +01:00
parent f1fe78eb1a
commit 2d455d78b3
6 changed files with 159 additions and 90 deletions

View File

@ -355,6 +355,7 @@ INPUT
#include "../input/drivers/xenon360_input.c"
#elif defined(ANDROID)
#include "../input/drivers/android_input.c"
#include "../input/drivers_keyboard/keyboard_event_android.c"
#include "../input/drivers_joypad/android_joypad.c"
#elif defined(__QNX__)
#include "../input/drivers/qnx_input.c"

View File

@ -26,13 +26,13 @@
#include "../input_autodetect.h"
#include "../input_config.h"
#include "../input_joypad_driver.h"
#include "../drivers_keyboard/keyboard_event_android.h"
#include "../../performance.h"
#include "../../general.h"
#include "../../driver.h"
#include "../../system.h"
#define MAX_TOUCH 16
#define MAX_PADS 8
#define AKEY_EVENT_NO_ACTION 255
@ -40,8 +40,6 @@
#define AKEYCODE_ASSIST 219
#endif
#define LAST_KEYCODE AKEYCODE_ASSIST
typedef struct
{
float x;
@ -83,10 +81,7 @@ typedef struct android_input
bool blocked;
unsigned pads_connected;
state_device_t pad_states[MAX_PADS];
uint8_t pad_state[MAX_PADS][(LAST_KEYCODE + 7) / 8];
int8_t hat_state[MAX_PADS][2];
int16_t analog_state[MAX_PADS][MAX_AXIS];
sensor_t accelerometer_state;
struct input_pointer pointer[MAX_TOUCH];
unsigned pointer_count;
@ -100,7 +95,8 @@ static void frontend_android_get_version_sdk(int32_t *sdk);
bool (*engine_lookup_name)(char *buf,
int *vendorId, int *productId, size_t size, int id);
void (*engine_handle_dpad)(android_input_t *android, AInputEvent*, int, int);
void (*engine_handle_dpad)(AInputEvent*, int, int);
static bool android_input_set_sensor_state(void *data, unsigned port,
enum retro_sensor_action action, unsigned event_rate);
@ -111,53 +107,6 @@ static typeof(AMotionEvent_getAxisValue) *p_AMotionEvent_getAxisValue;
#define AMotionEvent_getAxisValue (*p_AMotionEvent_getAxisValue)
static void engine_handle_dpad_default(android_input_t *android,
AInputEvent *event, int port, int source)
{
size_t motion_ptr = AMotionEvent_getAction(event) >>
AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
float x = AMotionEvent_getX(event, motion_ptr);
float y = AMotionEvent_getY(event, motion_ptr);
android->analog_state[port][0] = (int16_t)(x * 32767.0f);
android->analog_state[port][1] = (int16_t)(y * 32767.0f);
}
static void engine_handle_dpad_getaxisvalue(android_input_t *android,
AInputEvent *event, int port, int source)
{
size_t motion_ptr = AMotionEvent_getAction(event) >>
AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
float x = AMotionEvent_getAxisValue(event, AXIS_X, motion_ptr);
float y = AMotionEvent_getAxisValue(event, AXIS_Y, motion_ptr);
float z = AMotionEvent_getAxisValue(event, AXIS_Z, motion_ptr);
float rz = AMotionEvent_getAxisValue(event, AXIS_RZ, motion_ptr);
float hatx = AMotionEvent_getAxisValue(event, AXIS_HAT_X, motion_ptr);
float haty = AMotionEvent_getAxisValue(event, AXIS_HAT_Y, motion_ptr);
float ltrig = AMotionEvent_getAxisValue(event, AXIS_LTRIGGER, motion_ptr);
float rtrig = AMotionEvent_getAxisValue(event, AXIS_RTRIGGER, motion_ptr);
float brake = AMotionEvent_getAxisValue(event, AXIS_BRAKE, motion_ptr);
float gas = AMotionEvent_getAxisValue(event, AXIS_GAS, motion_ptr);
android->hat_state[port][0] = (int)hatx;
android->hat_state[port][1] = (int)haty;
/* XXX: this could be a loop instead, but do we really want to
* loop through every axis? */
android->analog_state[port][0] = (int16_t)(x * 32767.0f);
android->analog_state[port][1] = (int16_t)(y * 32767.0f);
android->analog_state[port][2] = (int16_t)(z * 32767.0f);
android->analog_state[port][3] = (int16_t)(rz * 32767.0f);
#if 0
android->analog_state[port][4] = (int16_t)(hatx * 32767.0f);
android->analog_state[port][5] = (int16_t)(haty * 32767.0f);
#endif
android->analog_state[port][6] = (int16_t)(ltrig * 32767.0f);
android->analog_state[port][7] = (int16_t)(rtrig * 32767.0f);
android->analog_state[port][8] = (int16_t)(brake * 32767.0f);
android->analog_state[port][9] = (int16_t)(gas * 32767.0f);
}
static bool android_input_lookup_name_prekitkat(char *buf,
int *vendorId, int *productId, size_t size, int id)
{
@ -514,7 +463,7 @@ static INLINE void android_input_poll_event_type_key(
AInputEvent *event, int port, int keycode, int source,
int type_event, int *handled)
{
uint8_t *buf = android->pad_state[port];
uint8_t *buf = android_keyboard_state_get(port);
int action = AKeyEvent_getAction(event);
/* some controllers send both the up and down events at once
@ -813,7 +762,7 @@ static void android_input_poll_input(void *data)
case AINPUT_EVENT_TYPE_MOTION:
if (android_input_poll_event_type_motion(android, event,
port, source))
engine_handle_dpad(android, event, port, source);
engine_handle_dpad(event, port, source);
break;
case AINPUT_EVENT_TYPE_KEY:
{
@ -924,7 +873,7 @@ static int16_t android_input_state(void *data,
(android->pointer[idx].y != -0x8000);
case RARCH_DEVICE_ID_POINTER_BACK:
if(settings->input.autoconf_binds[0][RARCH_MENU_TOGGLE].joykey == 0)
return BIT_GET(android->pad_state[0], AKEYCODE_BACK);
return android_keyboard_input_pressed(AKEYCODE_BACK);
}
break;
case RARCH_DEVICE_POINTER_SCREEN:
@ -940,7 +889,7 @@ static int16_t android_input_state(void *data,
(android->pointer[idx].full_y != -0x8000);
case RARCH_DEVICE_ID_POINTER_BACK:
if(settings->input.autoconf_binds[0][RARCH_MENU_TOGGLE].joykey == 0)
return BIT_GET(android->pad_state[0], AKEYCODE_BACK);
return android_keyboard_input_pressed(AKEYCODE_BACK);
}
break;
}
@ -975,6 +924,7 @@ static void android_input_free_input(void *data)
ASensorManager_destroyEventQueue(android->sensorManager,
android->sensorEventQueue);
android_keyboard_free();
free(data);
}

View File

@ -4,8 +4,7 @@
* Copyright (C) 2012-2015 - Michael Lelli
* Copyright (C) 2013-2014 - Steven Crowe
*
* 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-
* 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;
@ -18,6 +17,56 @@
#include <dlfcn.h>
#include "../drivers_keyboard/keyboard_event_android.h"
static int16_t analog_state[MAX_PADS][MAX_AXIS];
static int8_t hat_state[MAX_PADS][2];
static void engine_handle_dpad_default(AInputEvent *event, int port, int source)
{
size_t motion_ptr = AMotionEvent_getAction(event) >>
AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
float x = AMotionEvent_getX(event, motion_ptr);
float y = AMotionEvent_getY(event, motion_ptr);
analog_state[port][0] = (int16_t)(x * 32767.0f);
analog_state[port][1] = (int16_t)(y * 32767.0f);
}
static void engine_handle_dpad_getaxisvalue(AInputEvent *event, int port, int source)
{
size_t motion_ptr = AMotionEvent_getAction(event) >>
AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
float x = AMotionEvent_getAxisValue(event, AXIS_X, motion_ptr);
float y = AMotionEvent_getAxisValue(event, AXIS_Y, motion_ptr);
float z = AMotionEvent_getAxisValue(event, AXIS_Z, motion_ptr);
float rz = AMotionEvent_getAxisValue(event, AXIS_RZ, motion_ptr);
float hatx = AMotionEvent_getAxisValue(event, AXIS_HAT_X, motion_ptr);
float haty = AMotionEvent_getAxisValue(event, AXIS_HAT_Y, motion_ptr);
float ltrig = AMotionEvent_getAxisValue(event, AXIS_LTRIGGER, motion_ptr);
float rtrig = AMotionEvent_getAxisValue(event, AXIS_RTRIGGER, motion_ptr);
float brake = AMotionEvent_getAxisValue(event, AXIS_BRAKE, motion_ptr);
float gas = AMotionEvent_getAxisValue(event, AXIS_GAS, motion_ptr);
hat_state[port][0] = (int)hatx;
hat_state[port][1] = (int)haty;
/* XXX: this could be a loop instead, but do we really want to
* loop through every axis? */
analog_state[port][0] = (int16_t)(x * 32767.0f);
analog_state[port][1] = (int16_t)(y * 32767.0f);
analog_state[port][2] = (int16_t)(z * 32767.0f);
analog_state[port][3] = (int16_t)(rz * 32767.0f);
#if 0
analog_state[port][4] = (int16_t)(hatx * 32767.0f);
analog_state[port][5] = (int16_t)(haty * 32767.0f);
#endif
analog_state[port][6] = (int16_t)(ltrig * 32767.0f);
analog_state[port][7] = (int16_t)(rtrig * 32767.0f);
analog_state[port][8] = (int16_t)(brake * 32767.0f);
analog_state[port][9] = (int16_t)(gas * 32767.0f);
}
static const char *android_joypad_name(unsigned pad)
{
settings_t *settings = config_get_ptr();
@ -46,15 +95,11 @@ static bool android_joypad_init(void *data)
static bool android_joypad_button(unsigned port, uint16_t joykey)
{
uint8_t *buf = NULL;
driver_t *driver = driver_get_ptr();
android_input_t *android = driver ? (android_input_t*)driver->input_data : NULL;
uint8_t *buf = android_keyboard_state_get(port);
if (!android || port >= MAX_PADS)
if (port >= MAX_PADS)
return false;
buf = android->pad_state[port];
if (GET_HAT_DIR(joykey))
{
unsigned h = GET_HAT(joykey);
@ -64,13 +109,13 @@ static bool android_joypad_button(unsigned port, uint16_t joykey)
switch (GET_HAT_DIR(joykey))
{
case HAT_LEFT_MASK:
return android->hat_state[port][0] == -1;
return hat_state[port][0] == -1;
case HAT_RIGHT_MASK:
return android->hat_state[port][0] == 1;
return hat_state[port][0] == 1;
case HAT_UP_MASK:
return android->hat_state[port][1] == -1;
return hat_state[port][1] == -1;
case HAT_DOWN_MASK:
return android->hat_state[port][1] == 1;
return hat_state[port][1] == 1;
default:
return false;
}
@ -82,33 +127,23 @@ static bool android_joypad_button(unsigned port, uint16_t joykey)
static int16_t android_joypad_axis(unsigned port, uint32_t joyaxis)
{
int val = 0;
int axis = -1;
bool is_neg = false;
bool is_pos = false;
driver_t *driver = driver_get_ptr();
android_input_t *android = driver ? (android_input_t*)driver->input_data : NULL;
if (!android || joyaxis == AXIS_NONE || port >= MAX_PADS)
if (joyaxis == AXIS_NONE)
return 0;
if (AXIS_NEG_GET(joyaxis) < MAX_AXIS)
{
axis = AXIS_NEG_GET(joyaxis);
is_neg = true;
val = analog_state[port][AXIS_NEG_GET(joyaxis)];
if (val > 0)
val = 0;
}
else if (AXIS_POS_GET(joyaxis) < MAX_AXIS)
{
axis = AXIS_POS_GET(joyaxis);
is_pos = true;
val = analog_state[port][AXIS_POS_GET(joyaxis)];
if (val < 0)
val = 0;
}
val = android->analog_state[port][axis];
if (is_neg && val > 0)
val = 0;
else if (is_pos && val < 0)
val = 0;
return val;
}
@ -126,6 +161,15 @@ static bool android_joypad_query_pad(unsigned pad)
static void android_joypad_destroy(void)
{
unsigned i, j;
for (i = 0; i < MAX_PADS; i++)
{
for (j = 0; j < 2; j++)
hat_state[i][j] = 0;
for (j = 0; j < MAX_AXIS; j++)
analog_state[i][j] = 0;
}
}
input_device_driver_t android_joypad = {

View File

@ -618,12 +618,12 @@ static uint64_t udev_joypad_get_buttons(unsigned port)
static int16_t udev_joypad_axis(unsigned port, uint32_t joyaxis)
{
int16_t val = 0;
const struct udev_joypad *pad;
const struct udev_joypad *pad = (const struct udev_joypad*)
&udev_pads[port];
if (joyaxis == AXIS_NONE)
return 0;
pad = (const struct udev_joypad*)&udev_pads[port];
if (AXIS_NEG_GET(joyaxis) < NUM_AXES)
{
val = pad->axes[AXIS_NEG_GET(joyaxis)];

View File

@ -0,0 +1,41 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2015 - 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 "keyboard_event_android.h"
#define LAST_KEYCODE AKEYCODE_ASSIST
#define MAX_KEYS ((LAST_KEYCODE + 7) / 8)
static uint8_t android_key_state[MAX_PADS][MAX_KEYS];
bool android_keyboard_input_pressed(unsigned key)
{
return BIT_GET(android_key_state[0], key);
}
uint8_t *android_keyboard_state_get(unsigned port)
{
return android_key_state[port];
}
void android_keyboard_free(void)
{
unsigned i, j;
for (i = 0; i < MAX_PADS; i++)
for (j = 0; j < MAX_KEYS; j++)
android_key_state[i][j] = 0;
}

View File

@ -0,0 +1,33 @@
/* RetroArch - A frontend for libretro.
* Copyright (C) 2011-2015 - 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 _KEYBOARD_EVENT_ANDROID_H
#define _KEYBOARD_EVENT_ANDROID_H
#include <stdint.h>
#include <boolean.h>
#ifndef MAX_PADS
#define MAX_PADS 8
#endif
bool android_keyboard_input_pressed(unsigned key);
uint8_t *android_keyboard_state_get(unsigned port);
void android_keyboard_free(void);
#endif