[Android] Hotplugging support for joysticks

This commit is contained in:
Gabriel Jacobo 2013-12-10 16:24:11 -03:00
parent 924207a299
commit ae184f5e89
6 changed files with 440 additions and 300 deletions

View file

@ -23,15 +23,18 @@
#ifdef SDL_JOYSTICK_ANDROID
/* This is the system specific header for the SDL joystick API */
#include <stdio.h> /* For the definition of NULL */
#include "SDL_error.h"
#include "SDL_events.h"
#if !SDL_EVENTS_DISABLED
#include "../../events/SDL_events_c.h"
#endif
#include "SDL_joystick.h"
#include "SDL_hints.h"
#include "SDL_assert.h"
#include "../SDL_sysjoystick.h"
#include "SDL_sysjoystick_c.h"
#include "../SDL_joystick_c.h"
#include "../../core/android/SDL_android.h"
@ -57,14 +60,17 @@
#define AKEYCODE_BUTTON_16 203
#endif
#define ANDROID_ACCELEROMETER_INDEX (SYS_numjoysticks - 1)
#define ANDROID_ACCELEROMETER_NAME "Android Accelerometer"
#define ANDROID_ACCELEROMETER_DEVICE_ID INT_MIN
#define ANDROID_MAX_NBUTTONS 36
static SDL_Joystick **SYS_Joysticks;
static char **SYS_JoystickNames;
static int SYS_numjoysticks;
static SDL_bool SYS_accelAsJoy;
static SDL_joylist_item * JoystickByDeviceId(int device_id);
static SDL_joylist_item *SDL_joylist = NULL;
static SDL_joylist_item *SDL_joylist_tail = NULL;
static int numjoysticks = 0;
static int instance_counter = 0;
/* Function to convert Android keyCodes into SDL ones.
* This code manipulation is done to get a sequential list of codes.
@ -139,78 +145,265 @@ keycode_to_SDL(int keycode)
}
/* Function to scan the system for joysticks.
* This function should set SDL_numjoysticks to the number of available
* joysticks. Joystick 0 should be the system default joystick.
* It should return 0, or -1 on an unrecoverable fatal error.
*/
int
Android_OnPadDown(int device_id, int keycode)
{
SDL_joylist_item *item;
int button = keycode_to_SDL(keycode);
if (button >= 0) {
item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickButton(item->joystick, button , SDL_PRESSED);
}
return 0;
}
return -1;
}
int
Android_OnPadUp(int device_id, int keycode)
{
SDL_joylist_item *item;
int button = keycode_to_SDL(keycode);
if (button >= 0) {
item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickButton(item->joystick, button, SDL_RELEASED);
}
return 0;
}
return -1;
}
int
Android_OnJoy(int device_id, int axis, float value)
{
/* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
SDL_joylist_item *item = JoystickByDeviceId(device_id);
if (item && item->joystick) {
SDL_PrivateJoystickAxis(item->joystick, axis, (Sint16) (32767.*value) );
}
return 0;
}
int
Android_AddJoystick(int device_id, const char *name, SDL_bool is_accelerometer, int nbuttons, int naxes, int nhats, int nballs)
{
SDL_JoystickGUID guid;
SDL_joylist_item *item;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
#endif
if(JoystickByDeviceId(device_id) != NULL || name == NULL) {
return -1;
}
/* the GUID is just the first 16 chars of the name for now */
SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
item = (SDL_joylist_item *) SDL_malloc(sizeof (SDL_joylist_item));
if (item == NULL) {
return -1;
}
SDL_zerop(item);
item->guid = guid;
item->device_id = device_id;
item->name = SDL_strdup(name);
if ( item->name == NULL ) {
SDL_free(item);
return -1;
}
item->is_accelerometer = is_accelerometer;
if (nbuttons > -1) {
item->nbuttons = nbuttons;
}
else {
item->nbuttons = ANDROID_MAX_NBUTTONS;
}
item->naxes = naxes;
item->nhats = nhats;
item->nballs = nballs;
item->device_instance = instance_counter++;
if (SDL_joylist_tail == NULL) {
SDL_joylist = SDL_joylist_tail = item;
} else {
SDL_joylist_tail->next = item;
SDL_joylist_tail = item;
}
/* Need to increment the joystick count before we post the event */
++numjoysticks;
#if !SDL_EVENTS_DISABLED
event.type = SDL_JOYDEVICEADDED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = (numjoysticks - 1);
if ( (SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event) ) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
SDL_Log("Added joystick %s with device_id %d", name, device_id);
return numjoysticks;
}
int
Android_RemoveJoystick(int device_id)
{
SDL_joylist_item *item = SDL_joylist;
SDL_joylist_item *prev = NULL;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
#endif
/* Don't call JoystickByDeviceId here or there'll be an infinite loop! */
while (item != NULL) {
if (item->device_id == device_id) {
break;
}
prev = item;
item = item->next;
}
if (item == NULL) {
return -1;
}
const int retval = item->device_instance;
if (prev != NULL) {
prev->next = item->next;
} else {
SDL_assert(SDL_joylist == item);
SDL_joylist = item->next;
}
if (item == SDL_joylist_tail) {
SDL_joylist_tail = prev;
}
/* Need to decrement the joystick count before we post the event */
--numjoysticks;
#if !SDL_EVENTS_DISABLED
event.type = SDL_JOYDEVICEREMOVED;
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jdevice.which = item->device_instance;
if ( (SDL_EventOK == NULL) ||
(*SDL_EventOK) (SDL_EventOKParam, &event) ) {
SDL_PushEvent(&event);
}
}
#endif /* !SDL_EVENTS_DISABLED */
SDL_Log("Removed joystick with device_id %d", device_id);
SDL_free(item->name);
SDL_free(item);
return retval;
}
int
SDL_SYS_JoystickInit(void)
{
int i = 0;
const char *env;
SDL_SYS_JoystickDetect();
env = SDL_GetHint(SDL_HINT_ACCEL_AS_JOY);
if (env && !SDL_atoi(env))
SYS_accelAsJoy = SDL_FALSE;
else
SYS_accelAsJoy = SDL_TRUE; /* Default behavior */
SYS_numjoysticks = Android_JNI_GetNumJoysticks();
if (SYS_accelAsJoy) {
SYS_numjoysticks++;
}
SYS_Joysticks = (SDL_Joystick **)SDL_calloc(1, SYS_numjoysticks*sizeof(SDL_Joystick *));
if (SYS_Joysticks == NULL)
{
return SDL_OutOfMemory();
}
SYS_JoystickNames = (char **)SDL_calloc(1, SYS_numjoysticks*sizeof(char *));
if (SYS_JoystickNames == NULL)
{
SDL_free(SYS_Joysticks);
SYS_Joysticks = NULL;
return SDL_OutOfMemory();
}
for (i = 0; i < SYS_numjoysticks; i++)
{
if ( SYS_accelAsJoy && i == ANDROID_ACCELEROMETER_INDEX ) {
SYS_JoystickNames[i] = ANDROID_ACCELEROMETER_NAME;
} else {
SYS_JoystickNames[i] = Android_JNI_GetJoystickName(i);
}
if (!env || SDL_atoi(env)) {
/* Default behavior, accelerometer as joystick */
Android_AddJoystick(ANDROID_ACCELEROMETER_DEVICE_ID, ANDROID_ACCELEROMETER_NAME, SDL_TRUE, 0, 3, 0, 0);
}
return (SYS_numjoysticks);
return (numjoysticks);
}
int SDL_SYS_NumJoysticks()
{
return SYS_numjoysticks;
return numjoysticks;
}
void SDL_SYS_JoystickDetect()
{
/* Support for device connect/disconnect is API >= 16 only,
* so we have to poll ever few seconds.
* Ref: http://developer.android.com/reference/android/hardware/input/InputManager.InputDeviceListener.html
*/
Android_JNI_PollInputDevices();
}
/* TODO: Hotplugging support */
SDL_bool SDL_SYS_JoystickNeedsPolling()
{
return SDL_FALSE;
return SDL_TRUE;
}
static SDL_joylist_item *
JoystickByDevIndex(int device_index)
{
SDL_joylist_item *item = SDL_joylist;
if ((device_index < 0) || (device_index >= numjoysticks)) {
return NULL;
}
while (device_index > 0) {
SDL_assert(item != NULL);
device_index--;
item = item->next;
}
return item;
}
static SDL_joylist_item *
JoystickByDeviceId(int device_id)
{
SDL_joylist_item *item = SDL_joylist;
while (item != NULL) {
if (item->device_id == device_id) {
return item;
}
item = item->next;
}
/* Joystick not found, try adding it */
SDL_SYS_JoystickDetect();
while (item != NULL) {
if (item->device_id == device_id) {
return item;
}
item = item->next;
}
return NULL;
}
/* Function to get the device-dependent name of a joystick */
const char *
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
{
return SYS_JoystickNames[device_index];
return JoystickByDevIndex(device_index)->name;
}
/* Function to perform the mapping from device index to the instance id for this index */
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
{
return device_index;
return JoystickByDevIndex(device_index)->device_instance;
}
/* Function to open a joystick for use.
@ -221,49 +414,54 @@ SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
int
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
{
if (device_index < SYS_numjoysticks) {
joystick->nhats = 0;
joystick->nballs = 0;
if (SYS_accelAsJoy && device_index == ANDROID_ACCELEROMETER_INDEX) {
joystick->nbuttons = 0;
joystick->naxes = 3;
} else {
/* FIXME: Get the real number of buttons in the device? */
joystick->nbuttons = ANDROID_MAX_NBUTTONS;
joystick->naxes = Android_JNI_GetJoystickAxes(device_index);
}
SYS_Joysticks[device_index] = joystick;
return 0;
} else {
return SDL_SetError("No joystick available with that index");
SDL_joylist_item *item = JoystickByDevIndex(device_index);
char *fname = NULL;
if (item == NULL ) {
return SDL_SetError("No such device");
}
if (item->joystick != NULL) {
return SDL_SetError("Joystick already opened");
}
joystick->instance_id = item->device_instance;
joystick->hwdata = (struct joystick_hwdata *) item;
item->joystick = joystick;
joystick->nhats = item->nhats;
joystick->nballs = item->nballs;
joystick->nbuttons = item->nbuttons;
joystick->naxes = item->naxes;
return (0);
}
/* Function to determine is this joystick is attached to the system right now */
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
{
return SDL_TRUE;
return !joystick->closed && (joystick->hwdata != NULL);
}
/* Function to update the state of a joystick - called as a device poll.
* This function shouldn't update the joystick structure directly,
* but instead should call SDL_PrivateJoystick*() to deliver events
* and update joystick device state.
*/
void
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
{
int i;
Sint16 value;
float values[3];
SDL_joylist_item *item = SDL_joylist;
if (SYS_accelAsJoy && Android_JNI_GetAccelerometerValues(values) &&
joystick->instance_id == ANDROID_ACCELEROMETER_INDEX) {
for ( i = 0; i < 3; i++ ) {
value = (Sint16)(values[i] * 32767.0f);
SDL_PrivateJoystickAxis(joystick, i, value);
while (item) {
if (item->is_accelerometer) {
if (item->joystick) {
Android_JNI_GetAccelerometerValues(values);
for ( i = 0; i < 3; i++ ) {
value = (Sint16)(values[i] * 32767.0f);
SDL_PrivateJoystickAxis(item->joystick, i, value);
}
}
break;
}
item = item->next;
}
}
@ -271,14 +469,10 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
void
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
{
int device_index;
for (device_index = 0; device_index < SYS_numjoysticks; device_index++) {
if ( SYS_Joysticks[device_index] == joystick ) {
SYS_Joysticks[device_index] = NULL;
}
if (joystick->hwdata) {
((SDL_joylist_item*)joystick->hwdata)->joystick = NULL;
joystick->hwdata = NULL;
}
joystick->closed = 1;
}
@ -286,70 +480,29 @@ SDL_SYS_JoystickClose(SDL_Joystick * joystick)
void
SDL_SYS_JoystickQuit(void)
{
SDL_free(SYS_JoystickNames);
SDL_free(SYS_Joysticks);
SYS_JoystickNames = NULL;
SYS_Joysticks = NULL;
SDL_joylist_item *item = NULL;
SDL_joylist_item *next = NULL;
for (item = SDL_joylist; item; item = next) {
next = item->next;
SDL_free(item->name);
SDL_free(item);
}
SDL_joylist = SDL_joylist_tail = NULL;
numjoysticks = 0;
instance_counter = 0;
}
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
{
SDL_JoystickGUID guid;
/* the GUID is just the first 16 chars of the name for now */
const char *name = SDL_SYS_JoystickNameForDeviceIndex( device_index );
SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid;
return JoystickByDevIndex(device_index)->guid;
}
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
{
SDL_JoystickGUID guid;
/* the GUID is just the first 16 chars of the name for now */
const char *name = joystick->name;
SDL_zero( guid );
SDL_memcpy( &guid, name, SDL_min( sizeof(guid), SDL_strlen( name ) ) );
return guid;
}
int
Android_OnPadDown(int padId, int keycode)
{
int button = keycode_to_SDL(keycode);
if (button >= 0) {
if (SYS_Joysticks[padId]) {
SDL_PrivateJoystickButton(SYS_Joysticks[padId], button , SDL_PRESSED);
}
return 0;
}
return -1;
}
int
Android_OnPadUp(int padId, int keycode)
{
int button = keycode_to_SDL(keycode);
if (button >= 0) {
if (SYS_Joysticks[padId]) {
SDL_PrivateJoystickButton(SYS_Joysticks[padId], button, SDL_RELEASED);
}
return 0;
}
return -1;
}
int
Android_OnJoy(int joyId, int axis, float value)
{
/* Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0] */
/* TODO: Are the reported values right? */
if (SYS_Joysticks[joyId]) {
SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axis, (Sint16) (32767.*value) );
}
return 0;
return ((SDL_joylist_item*)joystick->hwdata)->guid;
}
#endif /* SDL_JOYSTICK_ANDROID */