Skip to content
Permalink
Browse files

Adds Joystick support for Android

This bumps the build SDK level to 12 (up from 10). Runtime requirements remain
the same (at API level < 12 joystick support is disabled).

Also enables building SDL for armv7 and x86.
  • Loading branch information
gabomdq committed Nov 5, 2013
1 parent 61068a3 commit 0b7c69fe1207b1c997ce2699211ffc65fcf2ef45
@@ -4,13 +4,14 @@ Simple DirectMedia Layer for Android

Requirements:

Android SDK (version 10 or later)
Android SDK (version 12 or later)
http://developer.android.com/sdk/index.html

Android NDK r7 or later
http://developer.android.com/sdk/ndk/index.html

Minimum API level supported by SDL: 10 (Android 2.3.3)
Joystick support is available for API level >=12 devices.

================================================================================
How the port works
@@ -396,8 +397,11 @@ When you're done instrumenting with valgrind, you can disable the wrapper:
Why is API level 10 the minimum required?
================================================================================

API level 10 is required because SDL requires some functionality for running not
available on older devices and some for building which is not in older NDK/SDKs.
API level 10 is the minimum required level at runtime (that is, on the device)
because SDL requires some functionality for running not
available on older devices. Since the incorporation of joystick support into SDL,
the minimum SDK required to *build* SDL is version 12. Devices running API levels
10-11 are still supported, only with the joystick functionality disabled.

Support for native OpenGL ES and ES2 applications was introduced in the NDK for
API level 4 and 8. EGL was made a stable API in the NDK for API level 9, which
@@ -1,6 +1,16 @@

This is a list of major changes in SDL's version history.

---------------------------------------------------------------------------
2.0.2:
---------------------------------------------------------------------------

Android:
* Joystick support (minimum SDK version required to build SDL is now 12,
the required runtime version remains at 10, but on such devices joystick
support won't be available).


---------------------------------------------------------------------------
2.0.1:
---------------------------------------------------------------------------
@@ -31,7 +31,7 @@
</application>

<!-- Android 2.3.3 -->
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" />
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="12" />

<!-- OpenGL ES 2.0 -->
<uses-feature android:glEsVersion="0x00020000" />
@@ -8,4 +8,4 @@
# project structure.

# Project target.
target=android-7
target=android-12
@@ -2,3 +2,5 @@
# Uncomment this if you're using STL in your project
# See CPLUSPLUS-SUPPORT.html in the NDK documentation for more information
# APP_STL := stlport_static

APP_ABI := armeabi armeabi-v7a x86
@@ -11,4 +11,4 @@
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

# Project target.
target=android-10
target=android-12
@@ -16,6 +16,10 @@
import android.media.*;
import android.hardware.*;

import java.lang.*;
import java.util.List;
import java.util.ArrayList;


/**
SDL Activity
@@ -31,10 +35,11 @@
protected static SDLSurface mSurface;
protected static View mTextEdit;
protected static ViewGroup mLayout;
protected static SDLJoystickHandler mJoystickHandler;

// This is what SDL runs in. It invokes SDL_main(), eventually
protected static Thread mSDLThread;

// Audio
protected static Thread mAudioThread;
protected static AudioTrack mAudioTrack;
@@ -60,6 +65,13 @@ protected void onCreate(Bundle savedInstanceState) {

// Set up the surface
mSurface = new SDLSurface(getApplication());

if(Build.VERSION.SDK_INT >= 12) {
mJoystickHandler = new SDLJoystickHandler_API12();
}
else {
mJoystickHandler = new SDLJoystickHandler();
}

mLayout = new AbsoluteLayout(this);
mLayout.addView(mSurface);
@@ -236,6 +248,10 @@ boolean sendCommand(int command, Object data) {
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 axis,
float value);
public static native void onNativeKeyDown(int keycode);
public static native void onNativeKeyUp(int keycode);
public static native void onNativeKeyboardFocusLost();
@@ -406,6 +422,23 @@ public static void audioQuit() {
}
return Arrays.copyOf(filtered, used);
}

// Joystick glue code, just a series of stubs that redirect to the SDLJoystickHandler instance
public static int getNumJoysticks() {
return mJoystickHandler.getNumJoysticks();
}

public static String getJoystickName(int joy) {
return mJoystickHandler.getJoystickName(joy);
}

public static int getJoystickAxes(int joy) {
return mJoystickHandler.getJoystickAxes(joy);
}

public static int getJoyId(int devId) {
return mJoystickHandler.getJoyId(devId);
}
}

/**
@@ -451,6 +484,10 @@ public SDLSurface(Context context) {

mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);

if(Build.VERSION.SDK_INT >= 12) {
setOnGenericMotionListener(new SDLGenericMotionHandler_API12());
}

// Some arbitrary defaults to avoid a potential division by zero
mWidth = 1.0f;
@@ -557,16 +594,26 @@ public void onDraw(Canvas canvas) {}
// Key events
@Override
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(event.getSource() == InputDevice.SOURCE_KEYBOARD) {
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;
}
} else if ( (event.getSource() & 0x00000401) != 0 || /* API 12: SOURCE_GAMEPAD */
(event.getSource() & InputDevice.SOURCE_DPAD) != 0 ) {
int id = SDLActivity.getJoyId( event.getDeviceId() );
if (event.getAction() == KeyEvent.ACTION_DOWN) {
SDLActivity.onNativePadDown(id, keyCode);
} else if (event.getAction() == KeyEvent.ACTION_UP) {
SDLActivity.onNativePadUp(id, keyCode);
}
}

return false;
@@ -646,8 +693,7 @@ public void onSensorChanged(SensorEvent event) {
y / SensorManager.GRAVITY_EARTH,
event.values[2] / SensorManager.GRAVITY_EARTH - 1);
}
}

}
}

/* This is a fake invisible editor view that receives the input and defines the
@@ -769,3 +815,95 @@ public boolean setComposingText(CharSequence text, int newCursorPosition) {

}

/* A null joystick handler for API level < 12 devices (the accelerometer is handled separately) */
class SDLJoystickHandler {
public int getNumJoysticks() {
return 0;
}

public String getJoystickName(int joy) {
return "";
}

public int getJoystickAxes(int joy) {
return 0;
}

public int getJoyId(int devId) {
return 0;
}
}

/* Actual joystick functionality available for API >= 12 devices */
class SDLJoystickHandler_API12 extends SDLJoystickHandler {
private List<Integer> mJoyIdList;

// Create a list of valid ID's the first time this function is called
private void createJoystickList() {
if(mJoyIdList != null) {
return;
}

mJoyIdList = new ArrayList<Integer>();
int[] deviceIds = InputDevice.getDeviceIds();
for(int i=0; i<deviceIds.length; i++) {
if( (InputDevice.getDevice(deviceIds[i]).getSources() & InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
mJoyIdList.add(deviceIds[i]);
}
}
}

public int getNumJoysticks() {
createJoystickList();

return mJoyIdList.size();
}

public String getJoystickName(int joy) {
createJoystickList();
return InputDevice.getDevice(mJoyIdList.get(joy)).getName();
}

public int getJoystickAxes(int joy) {
createJoystickList();
return InputDevice.getDevice(mJoyIdList.get(joy)).getMotionRanges().size();
}

public int getJoyId(int devId) {
int i=0;

createJoystickList();

for(i=0; i<mJoyIdList.size(); i++) {
if(mJoyIdList.get(i) == devId) {
return i;
}
}

return -1;
}

}

class SDLGenericMotionHandler_API12 extends Activity implements View.OnGenericMotionListener {
// Generic Motion (mouse hover, joystick...) events go here
// We only have joysticks yet
@Override
public boolean onGenericMotion(View v, MotionEvent event) {
int actionPointerIndex = event.getActionIndex();
int action = event.getActionMasked();

if ( (event.getSource() & InputDevice.SOURCE_JOYSTICK) != 0) {
switch(action) {
case MotionEvent.ACTION_MOVE:
int id = SDLActivity.getJoyId( event.getDeviceId() );
float x = event.getAxisValue(MotionEvent.AXIS_X, actionPointerIndex);
float y = event.getAxisValue(MotionEvent.AXIS_Y, actionPointerIndex);
SDLActivity.onNativeJoy(id, 0, x);
SDLActivity.onNativeJoy(id, 1, y);
break;
}
}
return true;
}
}
@@ -24,6 +24,7 @@ fi
if [ -z "$1" ] || [ -z "$SOURCES" ]; then
echo "Usage: androidbuild.sh com.yourcompany.yourapp < sources.list"
echo "Usage: androidbuild.sh com.yourcompany.yourapp source1.c source2.c ...sourceN.c"
echo "To copy SDL source instead of symlinking: COPYSOURCE=1 androidbuild.sh ... "
exit 1
fi

@@ -63,9 +64,15 @@ cp -r $SDLPATH/android-project/* $BUILDPATH

# Copy SDL sources
mkdir -p $BUILDPATH/jni/SDL
cp -r $SDLPATH/src $BUILDPATH/jni/SDL
cp -r $SDLPATH/include $BUILDPATH/jni/SDL
cp $SDLPATH/Android.mk $BUILDPATH/jni/SDL
if [ -z "$COPYSOURCE" ]; then
ln -s $SDLPATH/src $BUILDPATH/jni/SDL
ln -s $SDLPATH/include $BUILDPATH/jni/SDL
else
cp -r $SDLPATH/src $BUILDPATH/jni/SDL
cp -r $SDLPATH/include $BUILDPATH/jni/SDL
fi

cp -r $SDLPATH/Android.mk $BUILDPATH/jni/SDL
sed -i "s|YourSourceHere.c|$MKSOURCES|g" $BUILDPATH/jni/src/Android.mk
sed -i "s|org\.libsdl\.app|$APP|g" $BUILDPATH/AndroidManifest.xml

@@ -203,6 +203,16 @@ extern "C" {
* "LandscapeLeft", "LandscapeRight", "Portrait" "PortraitUpsideDown"
*/
#define SDL_HINT_ORIENTATIONS "SDL_IOS_ORIENTATIONS"

/**
* \brief A variable controlling whether an Android built-in accelerometer should be
* listed as a joystick device, rather than listing actual joysticks only.
*
* This variable can be set to the following values:
* "0" - List only real joysticks and accept input from them
* "1" - List real joysticks along with the accelerometer as if it were a 3 axis joystick (the default).
*/
#define SDL_HINT_ACCEL_AS_JOY "SDL_ACCEL_AS_JOY"


/**

0 comments on commit 0b7c69f

Please sign in to comment.