RetroArch/android/native/jni/input_android.c

431 lines
14 KiB
C
Raw Normal View History

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
2012-10-18 06:06:55 +02:00
* Copyright (C) 2011-2012 - 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 <dlfcn.h>
2012-10-28 22:20:22 +01:00
#include <android/keycodes.h>
#include "android-general.h"
#include "../../../general.h"
#include "../../../driver.h"
2012-10-28 22:20:22 +01:00
#include "input_android.h"
enum {
AKEYCODE_BUTTON_1 = 188,
AKEYCODE_BUTTON_2 = 189,
AKEYCODE_BUTTON_3 = 190,
AKEYCODE_BUTTON_4 = 191,
AKEYCODE_BUTTON_5 = 192,
AKEYCODE_BUTTON_6 = 193,
AKEYCODE_BUTTON_7 = 194,
AKEYCODE_BUTTON_8 = 195,
AKEYCODE_BUTTON_9 = 196,
AKEYCODE_BUTTON_10 = 197,
AKEYCODE_BUTTON_11 = 198,
AKEYCODE_BUTTON_12 = 199,
AKEYCODE_BUTTON_13 = 200,
AKEYCODE_BUTTON_14 = 201,
AKEYCODE_BUTTON_15 = 202,
AKEYCODE_BUTTON_16 = 203,
AKEYCODE_ASSIST = 219,
2012-10-28 22:20:22 +01:00
};
2012-10-28 22:20:22 +01:00
enum {
AKEYSTATE_DONT_PROCESS = 0,
AKEYSTATE_PROCESS = 1,
};
2012-10-28 02:20:25 +01:00
2012-10-29 14:45:25 +01:00
typedef struct {
int32_t a_keycode;
uint16_t r_keycode;
} rarch_android_bind_t;
2012-10-29 14:45:25 +01:00
2012-10-29 01:28:15 +01:00
//#define RARCH_INPUT_DEBUG
2012-10-28 22:20:22 +01:00
static unsigned pads_connected;
static android_input_state_t state[MAX_PADS];
2012-10-18 06:06:55 +02:00
#define LAST_KEYCODE AKEYCODE_ASSIST
int32_t keycode_lut[LAST_KEYCODE];
2012-10-30 05:57:12 +01:00
#define PRESSED_UP(x, y) ((-0.80f > y) && (x >= -1.00f))
#define PRESSED_DOWN(x, y) ((0.80f < y) && (y <= 1.00f))
#define PRESSED_LEFT(x, y) ((-0.80f > x) && (x >= -1.00f))
#define PRESSED_RIGHT(x, y) ((0.80f < x) && (x <= 1.00f))
static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
{
2012-10-28 22:20:22 +01:00
int id, i;
bool found_existing_id = false;
2012-10-28 02:20:25 +01:00
2012-10-28 22:20:22 +01:00
id = AInputEvent_getDeviceId(event);
2012-10-28 02:20:25 +01:00
2012-10-28 22:20:22 +01:00
for (i = 0; i < pads_connected; i++)
{
2012-10-30 05:57:12 +01:00
if (state[i].id == id)
2012-10-28 22:20:22 +01:00
{
found_existing_id = true;
2012-10-28 02:20:25 +01:00
break;
2012-10-28 22:20:22 +01:00
}
}
2012-10-28 02:20:25 +01:00
2012-10-28 22:20:22 +01:00
if(!found_existing_id)
2012-10-28 02:20:25 +01:00
{
2012-10-28 22:20:22 +01:00
state[pads_connected++].id = id;
i = pads_connected;
}
{
2012-10-29 01:28:15 +01:00
bool do_keydown = false;
bool do_keyrelease = false;
2012-10-30 05:57:12 +01:00
int action = AKEY_EVENT_NO_ACTION;
int type = AInputEvent_getType(event);
int keycode = AKeyEvent_getKeyCode(event);
2012-10-28 22:20:22 +01:00
2012-10-29 01:28:15 +01:00
#ifdef RARCH_INPUT_DEBUG
int source = AInputEvent_getSource(event);
2012-10-28 22:20:22 +01:00
switch(source)
2012-10-28 02:20:25 +01:00
{
2012-10-28 22:20:22 +01:00
case AINPUT_SOURCE_DPAD:
RARCH_LOG("AINPUT_SOURCE_DPAD, pad: %d, keycode: %d.\n", i, keycode);
2012-10-28 02:20:25 +01:00
break;
2012-10-28 22:20:22 +01:00
case AINPUT_SOURCE_TOUCHSCREEN:
RARCH_LOG("AINPUT_SOURCE_TOUCHSCREEN, pad: %d, keycode: %d.\n", i, keycode);
break;
case AINPUT_SOURCE_TOUCHPAD:
RARCH_LOG("AINPUT_SOURCE_TOUCHPAD, pad: %d, keycode: %d.\n", i, keycode);
2012-10-28 02:20:25 +01:00
break;
2012-10-28 22:20:22 +01:00
case AINPUT_SOURCE_ANY:
RARCH_LOG("AINPUT_SOURCE_ANY, pad: %d, keycode: %d.\n", i, keycode);
2012-10-28 02:20:25 +01:00
break;
2012-10-28 22:20:22 +01:00
case 0:
default:
RARCH_LOG("AINPUT_SOURCE_DEFAULT, pad: %d, keycode: %d.\n", i, keycode);
2012-10-28 02:20:25 +01:00
break;
}
2012-10-29 01:28:15 +01:00
#endif
action = AKeyEvent_getAction(event);
2012-10-28 02:20:25 +01:00
2012-10-30 05:57:12 +01:00
if(type == AINPUT_EVENT_TYPE_MOTION)
2012-10-28 22:20:22 +01:00
{
2012-10-30 05:57:12 +01:00
float x = AMotionEvent_getX(event, 0);
float y = AMotionEvent_getY(event, 0);
2012-10-29 01:28:15 +01:00
#ifdef RARCH_INPUT_DEBUG
2012-10-30 05:57:12 +01:00
RARCH_LOG("AINPUT_EVENT_TYPE_MOTION, pad: %d, x: %f, y: %f.\n", i, x, y);
2012-10-29 01:28:15 +01:00
#endif
state[i].state &= ~(ANDROID_GAMEPAD_DPAD_LEFT);
state[i].state &= ~(ANDROID_GAMEPAD_DPAD_RIGHT);
state[i].state &= ~(ANDROID_GAMEPAD_DPAD_UP);
state[i].state &= ~(ANDROID_GAMEPAD_DPAD_DOWN);
state[i].state |= PRESSED_LEFT(x, y) ? ANDROID_GAMEPAD_DPAD_LEFT : 0;
state[i].state |= PRESSED_RIGHT(x, y) ? ANDROID_GAMEPAD_DPAD_RIGHT : 0;
state[i].state |= PRESSED_UP(x, y) ? ANDROID_GAMEPAD_DPAD_UP : 0;
state[i].state |= PRESSED_DOWN(x, y) ? ANDROID_GAMEPAD_DPAD_DOWN : 0;
2012-10-28 22:20:22 +01:00
}
if(action != AKEY_EVENT_NO_ACTION)
{
switch(action)
{
case AKEY_EVENT_ACTION_DOWN:
2012-10-29 01:28:15 +01:00
case AKEY_EVENT_ACTION_MULTIPLE:
#ifdef RARCH_INPUT_DEBUG
2012-10-28 22:20:22 +01:00
RARCH_LOG("AKEY_EVENT_ACTION_DOWN, pad: %d, keycode: %d.\n", i, keycode);
2012-10-29 01:28:15 +01:00
#endif
do_keydown = true;
do_keyrelease = false;
2012-10-28 22:20:22 +01:00
break;
case AKEY_EVENT_ACTION_UP:
2012-10-29 01:28:15 +01:00
#ifdef RARCH_INPUT_DEBUG
2012-10-28 22:20:22 +01:00
RARCH_LOG("AKEY_EVENT_ACTION_UP, pad: %d, keycode: %d.\n", i, keycode);
2012-10-29 01:28:15 +01:00
#endif
do_keydown = false;
do_keyrelease = true;
2012-10-28 22:20:22 +01:00
break;
}
}
2012-10-29 01:28:15 +01:00
if(do_keydown)
state[i].state |= keycode_lut[keycode];
2012-10-29 01:28:15 +01:00
if(do_keyrelease)
state[i].state &= ~(keycode_lut[keycode]);
2012-10-28 22:20:22 +01:00
2012-10-28 02:20:25 +01:00
}
return 1;
}
static void *android_input_init(void)
{
g_android.app->onInputEvent = engine_handle_input;
2012-10-28 22:20:22 +01:00
pads_connected = 0;
for(int i = 0; i < LAST_KEYCODE; i++)
keycode_lut[i] = 0;
for(unsigned player = 0; player < 4; player++)
for(unsigned i = 0; i < RARCH_FIRST_META_KEY; i++)
{
g_settings.input.binds[player][i].id = i;
g_settings.input.binds[player][i].joykey = 0;
}
/* Control scheme 1
* fd=196
* path='/dev/input/event4'
* name='Logitech Logitech RumblePad 2 USB'
* classes=0x80000141
* configuration=''
* keyLayout='/system/usr/keylayout/Generic.kl'
* keyCharacterMap='/system/usr/keychars/Generic.kcm'
* builtinKeyboard=false
*/
keycode_lut[AKEYCODE_BUTTON_2] = ANDROID_GAMEPAD_CROSS;
keycode_lut[AKEYCODE_BUTTON_1] = ANDROID_GAMEPAD_SQUARE;
keycode_lut[AKEYCODE_BUTTON_9] = ANDROID_GAMEPAD_SELECT;
keycode_lut[AKEYCODE_BUTTON_10] = ANDROID_GAMEPAD_START;
keycode_lut[AKEYCODE_BUTTON_3] = ANDROID_GAMEPAD_CIRCLE;
keycode_lut[AKEYCODE_BUTTON_4] = ANDROID_GAMEPAD_TRIANGLE;
keycode_lut[AKEYCODE_BUTTON_5] = ANDROID_GAMEPAD_L1;
keycode_lut[AKEYCODE_BUTTON_6] = ANDROID_GAMEPAD_R1;
keycode_lut[AKEYCODE_BUTTON_7] = ANDROID_GAMEPAD_L2;
keycode_lut[AKEYCODE_BUTTON_8] = ANDROID_GAMEPAD_R2;
keycode_lut[AKEYCODE_BUTTON_11] = ANDROID_GAMEPAD_L3;
keycode_lut[AKEYCODE_BUTTON_12] = ANDROID_GAMEPAD_R3;
for(int player = 0; player < 4; player++)
{
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_B].joykey = ANDROID_GAMEPAD_CROSS;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_Y].joykey = ANDROID_GAMEPAD_SQUARE;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_SELECT].joykey = ANDROID_GAMEPAD_SELECT;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_START].joykey = ANDROID_GAMEPAD_START;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_UP].joykey = ANDROID_GAMEPAD_DPAD_UP;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_DOWN].joykey = ANDROID_GAMEPAD_DPAD_DOWN;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_LEFT].joykey = ANDROID_GAMEPAD_DPAD_LEFT;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_RIGHT].joykey = ANDROID_GAMEPAD_DPAD_RIGHT;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_A].joykey = ANDROID_GAMEPAD_CIRCLE;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_X].joykey = ANDROID_GAMEPAD_TRIANGLE;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_L].joykey = ANDROID_GAMEPAD_L1;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_R].joykey = ANDROID_GAMEPAD_R1;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_L2].joykey = ANDROID_GAMEPAD_L2;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_R2].joykey = ANDROID_GAMEPAD_R2;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_L3].joykey = ANDROID_GAMEPAD_L3;
g_settings.input.binds[player][RETRO_DEVICE_ID_JOYPAD_R3].joykey = ANDROID_GAMEPAD_R3;
}
/* Control scheme 2
* Tested with: SNES Pad USB converter
* fd=196
* path='/dev/input/event4'
* name='HuiJia USB GamePad'
* classes=0x80000141
* configuration=''
* keyLayout='/system/usr/keylayout/Generic.kl'
* keyCharacterMap='/system/usr/keychars/Generic.kcm'
* builtinKeyboard=false
*/
keycode_lut[AKEYCODE_BUTTON_C] = ANDROID_GAMEPAD_CROSS;
keycode_lut[AKEYCODE_BUTTON_X] = ANDROID_GAMEPAD_SQUARE;
keycode_lut[AKEYCODE_BUTTON_L2] = ANDROID_GAMEPAD_SELECT;
keycode_lut[AKEYCODE_BUTTON_R2] = ANDROID_GAMEPAD_START;
keycode_lut[AKEYCODE_BUTTON_B] = ANDROID_GAMEPAD_CIRCLE;
keycode_lut[AKEYCODE_BUTTON_A] = ANDROID_GAMEPAD_TRIANGLE;
keycode_lut[AKEYCODE_BUTTON_L1] = ANDROID_GAMEPAD_L1;
keycode_lut[AKEYCODE_BUTTON_R1] = ANDROID_GAMEPAD_R1;
/* Control scheme 3
* fd=196
* path='/dev/input/event4'
* name='Microsoft® Microsoft® SideWinder® Game Pad USB'
* classes=0x80000141
* configuration=''
* keyLayout='/system/usr/keylayout/Generic.kl'
* keyCharacterMap='/system/usr/keychars/Generic.kcm'
* builtinKeyboard=false
*/
/*
keycode_lut[AKEYCODE_BUTTON_A] = ANDROID_GAMEPAD_CROSS;
keycode_lut[AKEYCODE_BUTTON_X] = ANDROID_GAMPAD_SQUARE:
keycode_lut[AKEYCODE_BUTTON_R2] = ANDROID_GAMEPAD_SELECT;
keycode_lut[AKEYCODE_BUTTON_L2] = ANDROID_GAMEPAD_START;
keycode_lut[AKEYCODE_BUTTON_B] = ANDROID_GAMEPAD_CIRCLE;
keycode_lut[AKEYCODE_BUTTON_Y] = ANDROID_GAMEPAD_TRIANGLE;
keycode_lut[AKEYCODE_BUTTON_L1] = ANDROID_GAMEPAD_L1;
keycode_lut[AKEYCODE_BUTTON_R1] = ANDROID_GAMEPAD_R1;
keycode_lut[AKEYCODE_BUTTON_Z] = ANDROID_GAMEPAD_L2;
keycode_lut[AKEYCODE_BUTTON_C] = ANDROID_GAMEPAD_R2;
keycode_lut[AKEYCODE_BUTTON_11] = ANDROID_GAMEPAD_L3;
keycode_lut[AKEYCODE_BUTTON_12] = ANDROID_GAMEPAD_R3;
*/
/* Control scheme 4
* Tested with: Sidewinder Dual Strike
* fd=196
* path='/dev/input/event4'
* name='Microsoft SideWinder Dual Strike USB version 1.0'
* classes=0x80000141
* configuration=''
* keyLayout='/system/usr/keylayout/Generic.kl'
* keyCharacterMap='/system/usr/keychars/Generic.kcm'
* builtinKeyboard=false
*/
/*
keycode_lut[AKEYCODE_BUTTON_4] = ANDROID_GAMEPAD_CROSS;
keycode_lut[AKEYCODE_BUTTON_2] = ANDROID_GAMPAD_SQUARE:
keycode_lut[AKEYCODE_BUTTON_6] = ANDROID_GAMEPAD_SELECT;
keycode_lut[AKEYCODE_BUTTON_5] = ANDROID_GAMEPAD_START;
keycode_lut[AKEYCODE_BUTTON_3] = ANDROID_GAMEPAD_CIRCLE;
keycode_lut[AKEYCODE_BUTTON_1] = ANDROID_GAMEPAD_TRIANGLE;
keycode_lut[AKEYCODE_BUTTON_7] = ANDROID_GAMEPAD_L1;
keycode_lut[AKEYCODE_BUTTON_8] = ANDROID_GAMEPAD_R1;
keycode_lut[AKEYCODE_BUTTON_9] = ANDROID_GAMEPAD_L2;
*/
/* Control scheme 5
* fd=196
* path='/dev/input/event4'
* name='WiseGroup.,Ltd MP-8866 Dual USB Joypad'
* classes=0x80000141
* configuration=''
* keyLayout='/system/usr/keylayout/Generic.kl'
* keyCharacterMap='/system/usr/keychars/Generic.kcm'
* builtinKeyboard=false
*/
/*
keycode_lut[AKEYCODE_BUTTON_3] = ANDROID_GAMEPAD_CROSS;
keycode_lut[AKEYCODE_BUTTON_4] = ANDROID_GAMPAD_SQUARE:
keycode_lut[AKEYCODE_BUTTON_10] = ANDROID_GAMEPAD_SELECT;
keycode_lut[AKEYCODE_BUTTON_9] = ANDROID_GAMEPAD_START;
keycode_lut[AKEYCODE_BUTTON_2] = ANDROID_GAMEPAD_CIRCLE;
keycode_lut[AKEYCODE_BUTTON_1] = ANDROID_GAMEPAD_TRIANGLE;
keycode_lut[AKEYCODE_BUTTON_7] = ANDROID_GAMEPAD_L1;
keycode_lut[AKEYCODE_BUTTON_8] = ANDROID_GAMEPAD_R1;
keycode_lut[AKEYCODE_BUTTON_5] = ANDROID_GAMEPAD_L2;
keycode_lut[AKEYCODE_BUTTON_6] = ANDROID_GAMEPAD_R2;
keycode_lut[AKEYCODE_BUTTON_11] = ANDROID_GAMEPAD_L3;
keycode_lut[AKEYCODE_BUTTON_12] = ANDROID_GAMEPAD_R3;
*/
/* Control scheme 6
* Keyboard
* TODO: Map L2/R2/L3/R3
* */
keycode_lut[AKEYCODE_Z] = ANDROID_GAMEPAD_CROSS;
keycode_lut[AKEYCODE_A] = ANDROID_GAMEPAD_SQUARE;
keycode_lut[AKEYCODE_SHIFT_RIGHT] = ANDROID_GAMEPAD_SELECT;
keycode_lut[AKEYCODE_ENTER] = ANDROID_GAMEPAD_START;
keycode_lut[AKEYCODE_DPAD_UP] = ANDROID_GAMEPAD_DPAD_UP;
keycode_lut[AKEYCODE_DPAD_DOWN] = ANDROID_GAMEPAD_DPAD_DOWN;
keycode_lut[AKEYCODE_DPAD_LEFT] = ANDROID_GAMEPAD_DPAD_LEFT;
keycode_lut[AKEYCODE_DPAD_RIGHT] = ANDROID_GAMEPAD_DPAD_RIGHT;
keycode_lut[AKEYCODE_X] = ANDROID_GAMEPAD_CIRCLE;
keycode_lut[AKEYCODE_S] = ANDROID_GAMEPAD_TRIANGLE;
keycode_lut[AKEYCODE_Q] = ANDROID_GAMEPAD_L1;
keycode_lut[AKEYCODE_W] = ANDROID_GAMEPAD_R1;
return (void*)-1;
}
static void android_input_poll(void *data)
{
(void)data;
// Read all pending events.
int ident;
int events;
struct android_poll_source* source;
struct android_app* state = g_android.app;
// If not animating, we will block forever waiting for events.
// If animating, we loop until all events are read, then continue
// to draw the next frame of animation.
ident= ALooper_pollAll(0, NULL, &events, (void**)&source);
// Process this event.
if (ident && source != NULL)
source->process(state, source);
}
2012-10-28 22:20:22 +01:00
static int16_t android_input_state(void *data, const struct retro_keybind **binds, unsigned port, unsigned device, unsigned index, unsigned id)
{
2012-10-28 22:20:22 +01:00
unsigned player = port;
uint64_t button = binds[player][id].joykey;
2012-10-28 22:20:22 +01:00
int16_t retval = 0;
if((player < pads_connected))
{
switch (device)
{
case RETRO_DEVICE_JOYPAD:
retval = (state[player].state & button) ? 1 : 0;
2012-10-29 01:28:15 +01:00
#ifdef RARCH_INPUT_DEBUG
2012-10-28 22:20:22 +01:00
if(retval != 0)
{
RARCH_LOG("state: %d, player: %d.\n", retval, player);
}
2012-10-29 01:28:15 +01:00
#endif
2012-10-28 22:20:22 +01:00
break;
}
}
2012-10-28 22:20:22 +01:00
return retval;
}
static bool android_input_key_pressed(void *data, int key)
{
(void)data;
(void)key;
switch (key)
{
case RARCH_QUIT_KEY:
if(g_android.init_quit)
return true;
else
return false;
break;
default:
(void)0;
}
return false;
}
static void android_input_free(void *data)
{
(void)data;
}
const input_driver_t input_android = {
android_input_init,
android_input_poll,
android_input_state,
android_input_key_pressed,
android_input_free,
"android_input",
};