diff -r 7c2eb015a6d7 Android.mk
--- a/Android.mk Tue Mar 19 22:25:02 2013 -0700
+++ b/Android.mk Sat Mar 23 12:43:02 2013 +0200
@@ -42,6 +42,6 @@
$(wildcard $(LOCAL_PATH)/src/video/android/*.c))
LOCAL_CFLAGS += -DGL_GLEXT_PROTOTYPES
-LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog
+LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
include $(BUILD_SHARED_LIBRARY)
diff -r 7c2eb015a6d7 android-project/AndroidManifest.xml
--- a/android-project/AndroidManifest.xml Tue Mar 19 22:25:02 2013 -0700
+++ b/android-project/AndroidManifest.xml Sat Mar 23 12:43:02 2013 +0200
@@ -29,8 +29,8 @@
-
-
+
+
diff -r 7c2eb015a6d7 android-project/project.properties
--- a/android-project/project.properties Tue Mar 19 22:25:02 2013 -0700
+++ b/android-project/project.properties Sat Mar 23 12:43:02 2013 +0200
@@ -11,4 +11,4 @@
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
# Project target.
-target=android-10
+target=android-12
diff -r 7c2eb015a6d7 android-project/src/org/libsdl/app/SDLActivity.java
--- a/android-project/src/org/libsdl/app/SDLActivity.java Tue Mar 19 22:25:02 2013 -0700
+++ b/android-project/src/org/libsdl/app/SDLActivity.java Sat Mar 23 12:43:02 2013 +0200
@@ -24,6 +24,8 @@
import android.content.*;
import java.lang.*;
+import java.util.List;
+import java.util.ArrayList;
/**
@@ -42,6 +44,11 @@
// This is what SDL runs in. It invokes SDL_main(), eventually
private static Thread mSDLThread;
+
+ // Joystick
+ private static List mJoyIdList;
+ // TODO: Have a (somewhat) more efficient way of storing these?
+ private static List> mJoyAxesLists;
// Audio
private static Thread mAudioThread;
@@ -156,6 +163,9 @@
public static native void nativePause();
public static native void nativeResume();
public static native void onNativeResize(int x, int y, int format);
+ public static native void onNativePadDown(int padId, int keycode);
+ public static native void onNativePadUp(int padId, int keycode);
+ public static native void onNativeJoy(int joyId, int axisNum, float value);
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);
public static native void onNativeTouch(int touchDevId, int pointerFingerId,
@@ -180,6 +190,74 @@
mSingleton.sendCommand(COMMAND_CHANGE_TITLE, title);
}
+ // Call when initializing the joystick subsystem
+ public static void joystickInit() {
+ mJoyIdList = new ArrayList();
+ mJoyAxesLists = new ArrayList>();
+
+ int[] deviceIds = InputDevice.getDeviceIds();
+ for(int i=0; i axesList = new ArrayList();
+ /* With API 12 and above we can get a list of all motion
+ * ranges, hence all axes. Some of them may be irrelevant
+ * (e.g. an embedded trackpad). We filter the desired axes.
+ */
+ if(Build.VERSION.SDK_INT >= 12) {
+ List rangesList = InputDevice.getDevice(deviceIds[i]).getMotionRanges();
+ for (InputDevice.MotionRange range : rangesList) {
+ // Skip any possibly unrelated axis
+ if ( (range.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
+ axesList.add(range.getAxis());
+ }
+ }
+ } else {
+ // In older versions, we can assume a sane X-Y default configuration
+ axesList.add(MotionEvent.AXIS_X);
+ axesList.add(MotionEvent.AXIS_Y);
+ }
+ mJoyAxesLists.add(axesList);
+ }
+ }
+ }
+
+ // Call when one clears joystick subsystem resources
+ public static void joystickQuit() {
+ mJoyIdList = null;
+ mJoyAxesLists = null;
+ }
+
+ public static int getNumJoysticks() {
+ if (mJoyIdList == null)
+ return -1;
+ return mJoyIdList.size();
+ }
+
+ public static String getJoystickName(int joy) {
+ if (mJoyIdList == null)
+ return null;
+ return InputDevice.getDevice(mJoyIdList.get(joy)).getName();
+ }
+
+ public static List getJoystickAxesList(int joy) {
+ if (mJoyIdList == null)
+ return null;
+ return mJoyAxesLists.get(joy);
+ }
+
+ public static int getJoystickNumOfAxes(int joy) {
+ if (mJoyIdList == null)
+ return -1;
+ return mJoyAxesLists.get(joy).size();
+ }
+
+ public static int getJoyId(int devId) {
+ if (mJoyIdList == null)
+ return -1;
+ return mJoyIdList.indexOf(devId);
+ }
+
public static void sendMessage(int command, int param) {
mSingleton.sendCommand(command, Integer.valueOf(param));
}
@@ -478,7 +556,12 @@
setFocusableInTouchMode(true);
requestFocus();
setOnKeyListener(this);
- setOnTouchListener(this);
+ setOnTouchListener(this);
+
+ // Listen to joystick motion events if supported
+ if (Build.VERSION.SDK_INT >= 12) {
+ setOnGenericMotionListener(new SDLOnGenericMotionListener());
+ }
mSensorManager = (SensorManager)context.getSystemService("sensor");
@@ -568,18 +651,65 @@
+ // Listen to joystick motion events if supported (API >= 12)
+ private static class SDLOnGenericMotionListener implements View.OnGenericMotionListener {
+ @Override
+ public boolean onGenericMotion(View view, MotionEvent event) {
+ int actionPointerIndex = event.getActionIndex();
+ int action = event.getActionMasked();
+
+ if ( (event.getSource() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
+ switch(action) {
+ case MotionEvent.ACTION_MOVE:
+ int id = SDLActivity.getJoyId( event.getDeviceId() );
+ // The joystick subsystem may be uninitialized, so ignore
+ if (id < 0)
+ return true;
+ // Update values for all joystick axes
+ List axes = SDLActivity.getJoystickAxesList(id);
+ for (int axisIndex = 0; axisIndex < axes.size(); axisIndex++) {
+ SDLActivity.onNativeJoy(id, axisIndex, event.getAxisValue(axes.get(axisIndex), actionPointerIndex));
+ }
+
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
// Key events
public boolean onKey(View v, int keyCode, KeyEvent event) {
-
- if (event.getAction() == KeyEvent.ACTION_DOWN) {
- //Log.v("SDL", "key down: " + keyCode);
- SDLActivity.onNativeKeyDown(keyCode);
- return true;
- }
- else if (event.getAction() == KeyEvent.ACTION_UP) {
- //Log.v("SDL", "key up: " + keyCode);
- SDLActivity.onNativeKeyUp(keyCode);
- return true;
+ /* Dispatch the different events depending on where they come from:
+ * If the input device has some joystick source (probably differing
+ * from the source to which the given key belongs), assume it is a
+ * game controller button. Otherwise, assume a keyboard key.
+ * This should also take care of some kinds of manually toggled soft
+ * keyboards (i.e. not via the SDL text input API).
+ */
+ if ( (event.getDevice().getSources() & 0x00000010 /* API 12: InputDevice.SOURCE_CLASS_JOYSTICK*/) != 0) {
+ int id = SDLActivity.getJoyId( event.getDeviceId() );
+ // The joystick subsystem may be uninitialized, so ignore
+ if (id < 0)
+ return true;
+ if (event.getAction() == KeyEvent.ACTION_DOWN) {
+ SDLActivity.onNativePadDown(id, keyCode);
+ return true;
+ } else if (event.getAction() == KeyEvent.ACTION_UP) {
+ SDLActivity.onNativePadUp(id, keyCode);
+ return true;
+ }
+ } else {
+ if (event.getAction() == KeyEvent.ACTION_DOWN) {
+ //Log.v("SDL", "key down: " + keyCode);
+ SDLActivity.onNativeKeyDown(keyCode);
+ return true;
+ }
+ else if (event.getAction() == KeyEvent.ACTION_UP) {
+ //Log.v("SDL", "key up: " + keyCode);
+ SDLActivity.onNativeKeyUp(keyCode);
+ return true;
+ }
}
return false;
@@ -614,7 +744,7 @@
}
}
return true;
- }
+ }
// Sensor events
public void enableSensor(int sensortype, boolean enabled) {
@@ -640,7 +770,6 @@
event.values[2] / SensorManager.GRAVITY_EARTH);
}
}
-
}
/* This is a fake invisible editor view that receives the input and defines the
diff -r 7c2eb015a6d7 include/SDL_hints.h
--- a/include/SDL_hints.h Tue Mar 19 22:25:02 2013 -0700
+++ b/include/SDL_hints.h Sat Mar 23 12:43:02 2013 +0200
@@ -195,6 +195,17 @@
*/
#define SDL_HINT_ORIENTATIONS "SDL_IOS_ORIENTATIONS"
+/**
+ * \brief A variable controlling whether a built-in accelerometer should be
+ * listed as a joystick device (and the only one), rather than listing actual
+ * joysticks only. Currently has an effect on Android.
+ *
+ * This variable can be set to the following values:
+ * "0" - List actual joysticks and accept input from them
+ * "1" - Emulate a built-in accelerometer as a 3-axis joystick and disable actual joystick support (the default)
+ */
+#define SDL_HINT_EMULATE_ACCELEROMETER_AS_JOYSTICK "SDL_ANDROID_EMULATE_ACCELEROMETER_AS_JOYSTICK"
+
/**
* \brief A variable that lets you disable the detection and use of Xinput gamepad devices
diff -r 7c2eb015a6d7 src/core/android/SDL_android.cpp
--- a/src/core/android/SDL_android.cpp Tue Mar 19 22:25:02 2013 -0700
+++ b/src/core/android/SDL_android.cpp Sat Mar 23 12:43:02 2013 +0200
@@ -31,11 +31,13 @@
extern "C" {
#include "../../events/SDL_events_c.h"
+#include "../../joystick/android/SDL_androidjoystick.h"
#include "../../video/android/SDL_androidkeyboard.h"
#include "../../video/android/SDL_androidtouch.h"
#include "../../video/android/SDL_androidvideo.h"
#include
+#include
#include
#include
#include
@@ -78,7 +80,7 @@
// Accelerometer data storage
static float fLastAccelerometer[3];
-static bool bHasNewData;
+static bool bHasNewAccelerometerData;
/*******************************************************************************
Functions called by JNI
@@ -130,7 +132,7 @@
midAudioQuit = mEnv->GetStaticMethodID(mActivityClass,
"audioQuit", "()V");
- bHasNewData = false;
+ bHasNewAccelerometerData = false;
if(!midCreateGLContext || !midFlipBuffers || !midAudioInit ||
!midAudioWriteShortBuffer || !midAudioWriteByteBuffer || !midAudioQuit) {
@@ -147,6 +149,27 @@
Android_SetScreenResolution(width, height, format);
}
+// Paddown
+extern "C" void Java_org_libsdl_app_SDLActivity_onNativePadDown(
+ JNIEnv* env, jclass jcls, jint padId, jint keycode)
+{
+ Android_OnPadDown(padId, keycode);
+}
+
+// Padup
+extern "C" void Java_org_libsdl_app_SDLActivity_onNativePadUp(
+ JNIEnv* env, jclass jcls, jint padId, jint keycode)
+{
+ Android_OnPadUp(padId, keycode);
+}
+
+// Joysticks
+extern "C" void Java_org_libsdl_app_SDLActivity_onNativeJoy(
+ JNIEnv* env, jclass jcls, jint joyId, jint axisNum, jfloat value)
+{
+ Android_OnJoy(joyId, axisNum, value);
+}
+
// Keydown
extern "C" void Java_org_libsdl_app_SDLActivity_onNativeKeyDown(
JNIEnv* env, jclass jcls, jint keycode)
@@ -178,7 +201,7 @@
fLastAccelerometer[0] = x;
fLastAccelerometer[1] = y;
fLastAccelerometer[2] = z;
- bHasNewData = true;
+ bHasNewAccelerometerData = true;
}
// Quit
@@ -352,11 +375,11 @@
int i;
SDL_bool retval = SDL_FALSE;
- if (bHasNewData) {
+ if (bHasNewAccelerometerData) {
for (i = 0; i < 3; ++i) {
values[i] = fLastAccelerometer[i];
}
- bHasNewData = false;
+ bHasNewAccelerometerData = false;
retval = SDL_TRUE;
}
@@ -1082,6 +1105,101 @@
return 0;
}
+// Initialize the joystick subsystem on the Java side
+int Android_JNI_JoystickInit()
+{
+ JNIEnv* env = Android_JNI_GetEnv();
+ if (!env) {
+ return -1;
+ }
+ jmethodID mid = env->GetStaticMethodID(mActivityClass, "joystickInit", "()V");
+ if (!mid) {
+ return -1;
+ }
+ env->CallStaticVoidMethod(mActivityClass, mid);
+ return 0;
+}
+
+// Quit the joystick subsystem on the Java side
+int Android_JNI_JoystickQuit()
+{
+ JNIEnv* env = Android_JNI_GetEnv();
+ if (!env) {
+ return -1;
+ }
+ jmethodID mid = env->GetStaticMethodID(mActivityClass, "joystickQuit", "()V");
+ if (!mid) {
+ return -1;
+ }
+ env->CallStaticVoidMethod(mActivityClass, mid);
+ return 0;
+}
+
+// return the total number of plugged in joysticks
+extern "C" int Android_JNI_GetNumJoysticks()
+{
+ JNIEnv* env = Android_JNI_GetEnv();
+ if (!env) {
+ return -1;
+ }
+ jmethodID mid = env->GetStaticMethodID(mActivityClass, "getNumJoysticks", "()I");
+ if (!mid) {
+ return -1;
+ }
+
+ return env->CallStaticIntMethod(mActivityClass, mid);
+}
+
+// Return the name of joystick number "index".
+// Should be freed later using SDL_free.
+extern "C" char* Android_JNI_GetJoystickName(int index)
+{
+ JNIEnv* env = Android_JNI_GetEnv();
+ if (!env) {
+ return SDL_strdup("");
+ }
+
+ jmethodID mid = env->GetStaticMethodID(mActivityClass, "getJoystickName", "(I)Ljava/lang/String;");
+ if (!mid) {
+ return SDL_strdup("");
+ }
+ jstring string = reinterpret_cast(env->CallStaticObjectMethod(mActivityClass, mid, index));
+ const char* utf = env->GetStringUTFChars(string, 0);
+ if (!utf) {
+ return SDL_strdup("");
+ }
+
+ char* text = SDL_strdup(utf);
+ env->ReleaseStringUTFChars(string, utf);
+ return text;
+}
+
+// return the number of axes in the given joystick
+extern "C" int Android_JNI_GetJoystickNumOfAxes(int index)
+{
+ JNIEnv* env = Android_JNI_GetEnv();
+ if (!env) {
+ return -1;
+ }
+ jmethodID mid = env->GetStaticMethodID(mActivityClass, "getJoystickNumOfAxes", "(I)I");
+ if (!mid) {
+ return -1;
+ }
+
+ return env->CallStaticIntMethod(mActivityClass, mid, index);
+}
+
+// Return the name of the default accelerometer.
+// This is much easier to be done with NDK than with JNI.
+// Should be freed later using SDL_free.
+extern "C" char* Android_GetAccelName()
+{
+ ASensorManager* mSensorManager = ASensorManager_getInstance();
+ ASensor const* mAccelerometer = ASensorManager_getDefaultSensor(mSensorManager, ASENSOR_TYPE_ACCELEROMETER);
+
+ return SDL_strdup(ASensor_getName(mAccelerometer));
+}
+
// sends message to be handled on the UI event dispatch thread
extern "C" int Android_JNI_SendMessage(int command, int param)
{
diff -r 7c2eb015a6d7 src/core/android/SDL_android.h
--- a/src/core/android/SDL_android.h Tue Mar 19 22:25:02 2013 -0700
+++ b/src/core/android/SDL_android.h Sat Mar 23 12:43:02 2013 +0200
@@ -60,6 +60,14 @@
/* Power support */
int Android_JNI_GetPowerInfo(int* plugged, int* charged, int* battery, int* seconds, int* percent);
+/* Joystick/accelerometer support */
+int Android_JNI_JoystickInit();
+int Android_JNI_JoystickQuit();
+int Android_JNI_GetNumJoysticks();
+char* Android_JNI_GetJoystickName(int i);
+int Android_JNI_GetJoystickNumOfAxes(int index);
+char* Android_GetAccelName();
+
// Threads
#include
static void Android_JNI_ThreadDestroyed(void*);
diff -r 7c2eb015a6d7 src/joystick/android/SDL_androidjoystick.c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/joystick/android/SDL_androidjoystick.c Sat Mar 23 12:43:02 2013 +0200
@@ -0,0 +1,307 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2012 Sam Lantinga
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+
+#include "SDL_config.h"
+
+#ifdef SDL_JOYSTICK_ANDROID
+
+/* This is the system specific header for the SDL joystick API */
+#include /* For the definition of NULL */
+
+#include "SDL_error.h"
+#include "SDL_events.h"
+#include "SDL_joystick.h"
+#include "SDL_hints.h"
+#include "../SDL_sysjoystick.h"
+#include "../SDL_joystick_c.h"
+#include "../../core/android/SDL_android.h"
+
+//HACK!!
+static SDL_Joystick **SYS_Joysticks;
+static char **SYS_JoystickNames;
+static int SYS_numjoysticks;
+static SDL_bool SYS_isAccelerometerEmulatedAsJoystick;
+
+/* Function to convert Android keyCodes into SDL ones.
+ * This code manipulation is done to get a sequential list of codes.
+ */
+int
+keycode_to_SDL(int keycode)
+{
+ /* D-Pad key codes (API 1):
+ * KEYCODE_DPAD_UP=19, KEYCODE_DPAD_DOWN
+ * KEYCODE_DPAD_LEFT, KEYCODE_DPAD_RIGHT, KEYCODE_DPAD_CENTER
+ */
+ if(keycode < 96)
+ return keycode-19;
+ /* Some gamepad buttons (API 9):
+ * KEYCODE_BUTTON_A=96, KEYCODE_BUTTON_B, KEYCODE_BUTTON_C,
+ * KEYCODE_BUTTON_X, KEYCODE_BUTTON_Y, KEYCODE_BUTTON_Z,
+ * KEYCODE_BUTTON_L1, KEYCODE_BUTTON_L2,
+ * KEYCODE_BUTTON_R1, KEYCODE_BUTTON_R2,
+ * KEYCODE_BUTTON_THUMBL, KEYCODE_BUTTON_THUMBR,
+ * KEYCODE_BUTTON_START, KEYCODE_BUTTON_SELECT, KEYCODE_BUTTON_MODE
+ */
+ else if(keycode < 188)
+ return keycode-91;
+ /* More gamepad buttons (API 12):
+ * KEYCODE_BUTTON_1=188 to KEYCODE_BUTTON_16
+ */
+ else
+ return keycode-168;
+}
+
+/* Function to scan the system for joysticks.
+ * This function should set SYS_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
+SDL_SYS_JoystickInit(void)
+{
+ int i;
+ const char *env;
+
+ if (Android_JNI_JoystickInit() < 0)
+ return (-1);
+ env = SDL_GetHint(SDL_HINT_EMULATE_ACCELEROMETER_AS_JOYSTICK);
+ if (env && !SDL_atoi(env))
+ SYS_isAccelerometerEmulatedAsJoystick = SDL_FALSE;
+ else
+ SYS_isAccelerometerEmulatedAsJoystick = SDL_TRUE; /* Default behavior */
+
+ if (SYS_isAccelerometerEmulatedAsJoystick)
+ {
+ SYS_numjoysticks = 1;
+ }
+ else
+ {
+ SYS_numjoysticks = Android_JNI_GetNumJoysticks();
+ if (SYS_numjoysticks < 0)
+ return (-1);
+ }
+ SYS_Joysticks = (SDL_Joystick **)SDL_malloc(SYS_numjoysticks*sizeof(SDL_Joystick *));
+ if (SYS_Joysticks == NULL)
+ {
+ SDL_OutOfMemory();
+ return (-1);
+ }
+ SYS_JoystickNames = (char **)SDL_malloc(SYS_numjoysticks*sizeof(char *));
+ if (SYS_JoystickNames == NULL)
+ {
+ SDL_free(SYS_Joysticks);
+ SYS_Joysticks = NULL;
+ SDL_OutOfMemory();
+ return (-1);
+ }
+ SDL_memset(SYS_JoystickNames, 0, (SYS_numjoysticks*sizeof(char *)));
+ SDL_memset(SYS_Joysticks, 0, (SYS_numjoysticks*sizeof(SDL_Joystick *)));
+
+ if (SYS_isAccelerometerEmulatedAsJoystick)
+ {
+ SYS_JoystickNames[0] = Android_GetAccelName();
+ }
+ else
+ {
+ for (i = 0; i < SYS_numjoysticks; i++)
+ {
+ SYS_JoystickNames[i] = Android_JNI_GetJoystickName(i);
+ }
+ }
+
+ return (SYS_numjoysticks);
+}
+
+int SDL_SYS_NumJoysticks()
+{
+ return SYS_numjoysticks;
+}
+
+void SDL_SYS_JoystickDetect()
+{
+}
+
+SDL_bool SDL_SYS_JoystickNeedsPolling()
+{
+ return SDL_FALSE;
+}
+
+/* Function to get the device-dependent name of a joystick */
+const char *
+SDL_SYS_JoystickNameForDeviceIndex(int device_index)
+{
+ return SYS_JoystickNames[device_index];
+}
+
+/* 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;
+}
+
+/* Function to open a joystick for use.
+ The joystick to open is specified by the index field of the joystick.
+ This should fill the nbuttons and naxes fields of the joystick structure.
+ It returns 0, or -1 if there is an error.
+ */
+int
+SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
+{
+ if (SYS_isAccelerometerEmulatedAsJoystick)
+ {
+ if (device_index != 0)
+ return -1;
+ joystick->nbuttons = 0;
+ joystick->nhats = 0;
+ joystick->nballs = 0;
+ joystick->naxes = 3;
+ }
+ else
+ {
+ if( (device_index >= 0) && (device_index < SYS_numjoysticks) )
+ {
+ // TODO: How to get the rest of the info??
+ // 36 is the maximum number of handled buttons
+ joystick->nbuttons = 36;
+ joystick->nhats = 0;
+ joystick->nballs = 0;
+ joystick->naxes = Android_JNI_GetJoystickNumOfAxes(device_index);
+ if (joystick->naxes < 0)
+ return -1;
+ }
+ else
+ {
+ return -1;
+ }
+ }
+
+ // Extremely hacky
+ SYS_Joysticks[device_index] = joystick;
+
+ return 0;
+}
+
+
+/* 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];
+
+ if (!SYS_isAccelerometerEmulatedAsJoystick)
+ return;
+
+ if( joystick->instance_id == 0 )
+ {
+ if (Android_JNI_GetAccelerometerValues(values))
+ {
+ for ( i = 0; i < 3; i++ )
+ {
+ value = (Sint16)(values[i] * 32767.0f);
+ SDL_PrivateJoystickAxis(joystick, i, value);
+ }
+ }
+ }
+}
+
+/* Function to determine is this joystick is attached to the system right now */
+SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
+{
+ return SDL_TRUE;
+}
+
+/* Function to close a joystick after use */
+void
+SDL_SYS_JoystickClose(SDL_Joystick * joystick)
+{
+}
+
+/* Function to perform any system-specific joystick related cleanup */
+void
+SDL_SYS_JoystickQuit(void)
+{
+ int i;
+ for (i = 0; i < SYS_numjoysticks; i++)
+ {
+ SDL_free(SYS_JoystickNames[i]);
+ }
+ SDL_free(SYS_JoystickNames);
+ SDL_free(SYS_Joysticks);
+ SYS_JoystickNames = NULL;
+ SYS_Joysticks = NULL;
+ Android_JNI_JoystickQuit();
+}
+
+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;
+}
+
+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)
+{
+ SDL_PrivateJoystickButton(SYS_Joysticks[padId], keycode_to_SDL(keycode), SDL_PRESSED);
+
+ return 0;
+}
+
+int
+Android_OnPadUp(int padId, int keycode)
+{
+ SDL_PrivateJoystickButton(SYS_Joysticks[padId], keycode_to_SDL(keycode), SDL_RELEASED);
+
+ return 0;
+}
+
+int
+Android_OnJoy(int joyId, int axisnum, float value)
+{
+ // Android gives joy info normalized as [-1.0, 1.0] or [0.0, 1.0]
+ // TODO: Are the reported values right?
+ SDL_PrivateJoystickAxis(SYS_Joysticks[joyId], axisnum, (Sint16) (32767.*value) );
+
+ return 0;
+}
+
+#endif /* SDL_JOYSTICK_ANDROID */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff -r 7c2eb015a6d7 src/joystick/android/SDL_androidjoystick.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/joystick/android/SDL_androidjoystick.h Sat Mar 23 12:43:02 2013 +0200
@@ -0,0 +1,27 @@
+/*
+ Simple DirectMedia Layer
+ Copyright (C) 1997-2012 Sam Lantinga
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+*/
+#include "SDL_config.h"
+
+extern int Android_OnPadDown(int padId, int keycode);
+extern int Android_OnPadUp(int padId, int keycode);
+extern int Android_OnJoy(int joyId, int axisnum, float value);
+
+/* vi: set ts=4 sw=4 expandtab: */
diff -r 7c2eb015a6d7 src/joystick/android/SDL_sysjoystick.c
--- a/src/joystick/android/SDL_sysjoystick.c Tue Mar 19 22:25:02 2013 -0700
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,156 +0,0 @@
-/*
- Simple DirectMedia Layer
- Copyright (C) 1997-2013 Sam Lantinga
-
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any damages
- arising from the use of this software.
-
- Permission is granted to anyone to use this software for any purpose,
- including commercial applications, and to alter it and redistribute it
- freely, subject to the following restrictions:
-
- 1. The origin of this software must not be misrepresented; you must not
- claim that you wrote the original software. If you use this software
- in a product, an acknowledgment in the product documentation would be
- appreciated but is not required.
- 2. Altered source versions must be plainly marked as such, and must not be
- misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source distribution.
-*/
-
-#include "SDL_config.h"
-
-#ifdef SDL_JOYSTICK_ANDROID
-
-/* This is the system specific header for the SDL joystick API */
-#include /* For the definition of NULL */
-
-#include "SDL_error.h"
-#include "SDL_events.h"
-#include "SDL_joystick.h"
-#include "../SDL_sysjoystick.h"
-#include "../SDL_joystick_c.h"
-#include "../../core/android/SDL_android.h"
-
-static const char *accelerometerName = "Android accelerometer";
-
-/* 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
-SDL_SYS_JoystickInit(void)
-{
- return (1);
-}
-
-int SDL_SYS_NumJoysticks()
-{
- return 1;
-}
-
-void SDL_SYS_JoystickDetect()
-{
-}
-
-SDL_bool SDL_SYS_JoystickNeedsPolling()
-{
- return SDL_FALSE;
-}
-
-/* Function to get the device-dependent name of a joystick */
-const char *
-SDL_SYS_JoystickNameForDeviceIndex(int device_index)
-{
- return accelerometerName;
-}
-
-/* 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;
-}
-
-/* Function to open a joystick for use.
- The joystick to open is specified by the index field of the joystick.
- This should fill the nbuttons and naxes fields of the joystick structure.
- It returns 0, or -1 if there is an error.
- */
-int
-SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
-{
- if (device_index == 0) {
- joystick->nbuttons = 0;
- joystick->nhats = 0;
- joystick->nballs = 0;
- joystick->naxes = 3;
- return 0;
- } else {
- SDL_SetError("No joystick available with that index");
- return (-1);
- }
-}
-
-/* Function to determine is this joystick is attached to the system right now */
-SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
-{
- return SDL_TRUE;
-}
-
-/* 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];
-
- if (Android_JNI_GetAccelerometerValues(values)) {
- for ( i = 0; i < 3; i++ ) {
- value = (Sint16)(values[i] * 32767.0f);
- SDL_PrivateJoystickAxis(joystick, i, value);
- }
- }
-}
-
-/* Function to close a joystick after use */
-void
-SDL_SYS_JoystickClose(SDL_Joystick * joystick)
-{
-}
-
-/* Function to perform any system-specific joystick related cleanup */
-void
-SDL_SYS_JoystickQuit(void)
-{
-}
-
-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;
-}
-
-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;
-}
-
-#endif /* SDL_JOYSTICK_ANDROID */
-
-/* vi: set ts=4 sw=4 expandtab: */